Commit Graph

186 Commits

Author SHA1 Message Date
Gleb Smirnoff
1897a4f168 Fix regression from r310655, which broke operation of bsnmpd if it is bound
to a non-wildcard address.  As documented in ip(4), doing sendmsg(2) with
IP_SENDSRCADDR on a socket that is bound to non-wildcard address is
completely different to using this control message on a wildcard one.

A fix is to add a bool to mark whether we did setsockopt(IP_RECVDSTADDR)
on the socket, and use IP_SENDSRCADDR control message only if we did.

While here, garbage collect absolutely useless udp_recv() function that
establishes some structures on stack to never use them later.
2017-01-17 03:52:57 +00:00
Enji Cooper
818d3ff977 Add a REVISION section to track changes for the BEGEMOT-MIB MIB file
There haven't been any changes to the MIB definition, so the REVISION
remains static at the version it was imported at

MFC after:	1 week
2017-01-09 06:27:30 +00:00
Enji Cooper
0c72a43f3f Add a REVISION section to track changes for the FOKUS-MIB MIB file
There haven't been any changes to the MIB definition, so the REVISION
remains static at the version it was imported at

MFC after:	1 week
2017-01-09 06:24:28 +00:00
Enji Cooper
b55d259a14 Similar to r311750, check for the result from smiGetModule to avoid a segfault
when dereferencing a NULL pointer later on.

Choose to just check for the NULL pointer in the next for-loop for now to fix
the issue with a minimal amount of code churn

sys/queue.h use here would make more sense than using a static table

MFC after:	5 days
2017-01-09 06:13:27 +00:00
Enji Cooper
8406423362 Use calloc instead of malloc + memset(.., 0, ..)
MFC after:	5 days
2017-01-09 06:03:49 +00:00
Enji Cooper
c5f8d75113 Check result from smiGetFirstNode and smiGetNodeByOID
This avoids a segfault with malformed or unanticipated files,
like IPV6-TC.txt (a file containing just TEXTUAL-CONVENTIONS).

MFC after:	5 days
Found with:	gensnmpdef /usr/local/share/snmp/mibs/IPV6-TC.txt
2017-01-09 05:51:38 +00:00
Enji Cooper
fbd43e4c99 Add a REVISION section to track changes for the BEGEMOT-IP-MIB MIB file
There haven't been any changes to the MIB definition, so the REVISION
remains static at the version it was imported at

MFC after:	1 week
2017-01-09 03:21:21 +00:00
Enji Cooper
0ab7046959 Use nitems(mib) instead of hardcoding mib's length when calling sysctl(3)
MFC after:	3 days
2017-01-09 01:47:00 +00:00
Enji Cooper
08235f2408 Remove unnecessary __unused attribute attached to ctx in op_begemot_mibII(..)
MFC after:	3 days
2017-01-06 07:57:45 +00:00
Enji Cooper
60c2226f89 op_usm_users: don't deref uusers if it's NULL when SETting the value
Add an XXX comment to note that the conditional seems suspect given
how it's handled elsewhere in the SNMP_OP_SET case.

MFC after:	2 weeks
Reported by:	Coverity
CID:		1008573
2017-01-05 09:46:36 +00:00
Enji Cooper
3daec7ae72 snmp_table_fetch_async: don't leak work if snmp_pdu_send(..) fails
MFC after:	1 week
Reported by:	Coverity
CID:		1017276
2017-01-05 08:49:06 +00:00
Enji Cooper
994c8618ec op_usm_users: fix indentation in SNMP_OP_SET block
MFC after:	3 days
2017-01-05 08:27:23 +00:00
Enji Cooper
446bd8a476 Use calloc instead of malloc + memset(.., 0, ..)
MFC after:	3 days
2017-01-05 08:17:17 +00:00
Enji Cooper
70157df618 lsock_init_port: address issues with initializing sockaddr_un object
- Use strlcpy to ensure p->name doesn't overflow sa.sun_path [*].
- Use SUN_LEN(..) instead of spelling out calculation longhand (inspired
  by comment by jmallett).

Tested with:	dgram and stream support with both bsnmpwalk and snmpwalk

MFC after:	1 week
Reported by:	Coverity
CID:		1006825
2017-01-05 08:14:20 +00:00
Enji Cooper
10a0306a2e lm_load: fix string copying issues
- Ensure `section` doesn't overrun section by using strlcpy instead of
  strcpy [*].
- Use strdup instead of malloc + strcpy (this wasn't flagged by Coverity,
  but is an opportunistic change).

MFC after:	1 week
Reported by:	Coverity
CID:		1006826 [*]
2017-01-05 07:55:17 +00:00
Enji Cooper
ccd0cf8ffa snmp_mibII(3) requires net/if.h and net/if_mib.h
Document that requirement

MFC after:	1 week
2017-01-04 10:08:18 +00:00
Enji Cooper
2e590d595e Use calloc instead of malloc with buffers in snmp_{recv,send}_packet
This doesn't fix the issue noted in the PR, but at the very least it
cleans up the error so it looks a bit more sane, and in the event
that bsnmp did wander off into the weeds, the likelihood of it
crashing with more sensible output is greater, in my opinion

MFC counter set high so I have enough time to resolve the real
underlying bug in bsnmpwalk

MFC after:	1 month
PR:		215721
2017-01-04 07:53:01 +00:00
Enji Cooper
1a55af1fb9 Initialize msg.msg_flags to 0
This mutes a valid coverity warning about it being uninitialized
when passed in to sendmsg(2).

MFC after:	2 weeks
Reported by:	Coverity
CID:		1368202
2017-01-04 01:38:07 +00:00
Enji Cooper
84d0b89e96 Fix spelling errors; bump .Dd for the change
MFC after:	3 days
2017-01-01 05:23:01 +00:00
Enji Cooper
d1b4c796ac Bump .Dd for the spelling and .Nm updates
MFC after:	3 days
2017-01-01 05:16:24 +00:00
Enji Cooper
d075380571 Fix spelling errors
MFC after:	3 days
Reported by:	igor
2017-01-01 05:14:58 +00:00
Enji Cooper
ad52f0d10c bsnmpclient(3) also documents snmp_client_init, snmp_client_set_host,
and snmp_client_set_port. Add them to the NAME section

MFC after:	3 days
2017-01-01 05:13:54 +00:00
Enji Cooper
1cc49661ec snmp_discover_engine: fix up req/resp (PDU object) handling a bit
- Call snmp_pdu_free on req and resp when done with the objects
- Call snmp_pdu_free on req before calling snmp_pdu_create on it
  again

MFC after:	1 week
2016-12-31 23:20:57 +00:00
Enji Cooper
560c5ef9a2 Similar to r310954, set .len to 0 on malloc failure and to len only
on success

MFC after:	1 week
2016-12-31 12:37:53 +00:00
Enji Cooper
39ebb4e1e0 Initialize ret to SNMPD_INPUT_OK at the top of snmp_input_start(..) to
avoid returning an uninitialized value

There are some really complicated, snakey if-statements combined with
switch statements that could result in an invalid value being returned
as `ret`

MFC after:	1 week
Reported by:	Coverity
CID:		1006551
2016-12-31 12:30:14 +00:00
Enji Cooper
8e02b381d3 Use strlcpy when copying com to pdu->community to avoid potential
buffer overruns

MFC after:	1 week
Reported by:	Coverity
CID:		1006823, 1006824
2016-12-31 12:18:17 +00:00
Enji Cooper
a0e0e1ffa5 MIB-II: use strlcpy instead of strcpy when copying {descr,name}
This is of course to avoid buffer overruns

The remaining strcpy instance in the module needs to be audited for
correctness

MFC after:	1 week
Reported by:	Coverity
CID:		1006827, 1006828
2016-12-31 12:03:25 +00:00
Enji Cooper
02ff676c4d MIB-II: use strlcpy when copying interface names to .ifr_name
.ifra_name is assumed to be NUL terminated; using strlcpy(3)
ensures that it's indeed NUL terminated whereas strncpy does
not.

Tested and verified as follows with a combination of ifconfig,
snmpget, and snmpset:

  % ifconfig create lo1 127.0.0.2/8
  % SNMPARGS="-v 3 -n '' -u bsnmp -A bsnmptest -l authPriv -a sha -x des -X bsnmptest localhost"
  % snmpget $SNMPARGS IF-MIB::ifAdminStatus.4
  IF-MIB::ifAdminStatus.4 = INTEGER: up(1)
  % snmpset $SNMPARGS IF-MIB::ifAdminStatus.4 i 2
  IF-MIB::ifAdminStatus.4 = INTEGER: down(2)
  % snmpget $SNMPARGS IF-MIB::ifAdminStatus.4
  IF-MIB::ifAdminStatus.4 = INTEGER: down(2)
  % snmpset $SNMPARGS IF-MIB::ifAdminStatus.4 i 1
  IF-MIB::ifAdminStatus.4 = INTEGER: up(1)
  % snmpget $SNMPARGS IF-MIB::ifAdminStatus.4
  IF-MIB::ifAdminStatus.4 = INTEGER: up(1)

MFC after:	2 weeks
Reported by:	Coverity
CID:		1009652-1009656, 1349850
2016-12-31 11:50:36 +00:00
Enji Cooper
1e5211d238 Unbreak the build by passing the string to strdup, not its length
MFC after:	1 week
X-MFC with:	r310931
Pointyhat to:	ngie
2016-12-31 11:24:12 +00:00
Enji Cooper
bfb81e6524 Use strdup in snmp_parse_server(..) when possible instead of malloc+strcpy
This simplifies the code and mutes a Coverity warning about sc->cport being
improperly allocated

Reported by:	Coverity
CID:		1018247
MFC after:	1 week
2016-12-31 11:13:00 +00:00
Enji Cooper
8373993535 Guard against use-after-free after calling mibif_free(..)
Set variables to NULL after calling free.

Also, remove unnecessary if (x != NULL) checks before calling free(x)

MFC after:	1 week
2016-12-30 23:44:39 +00:00
Enji Cooper
2fd30e016e Fix whitespace in a comment and fixing a spelling error in a comment
MFC after:	3 days
2016-12-30 21:41:01 +00:00
Enji Cooper
b8882958b3 Use uint32_t instead of u_int32_t for or_last_change and services in "struct systemg"
This is being done to match "struct systemg" in snmpmod(3)

No functional change

MFC after:	3 days
2016-12-29 08:16:43 +00:00
Enji Cooper
6c9b7542ef Prevent improper memory accesses after calling snmp_pdu_free and snmp_value_free
snmp_pdu_free: set pdu->nbindings to 0 to limit the damage that
could happen if a pdu was reused after calling the function, and
as both stack and heap allocation types are used in contrib/bsnmp
and usr.sbin/bsnmpd.

snmp_value_free: NULL out value->v.octetstring.octets after calling
free on it to prevent a double-free from occurring.

MFC after:      2 weeks
2016-12-29 00:20:03 +00:00
Enji Cooper
b3972edb64 style(9): ip_get(..): clean up whitespace
MFC after:	3 days
2016-12-28 05:05:08 +00:00
Enji Cooper
700d391fb8 style(9): snmp_send_packet(..): fix whitespace
MFC after:	3 days
2016-12-28 04:56:15 +00:00
Enji Cooper
bc54857ed0 style(9): fix whitespace in pdu_encode_secparams(..)
MFC after:	3 days
2016-12-28 04:53:52 +00:00
Enji Cooper
5120d21c63 style(9): sort alignment in udp_recv(..)
MFC after:	3 weeks
2016-12-28 04:31:07 +00:00
Enji Cooper
8d7f605b6c Fix bsnmpd sending/receiving with multi-homed configurations or INADDR_ANY used
as the listening address in snmpd_input(..)

Stash the IPv4 address of the receiver via the recv(..) callback and use it in
the send(..) callback for the transport by specifying IP_SENDSRCADDR for the
control message type.

Add sendmsg logic to the UDP transport's send(..) callback and use the
respective send(..) callback for the transport instead of calling sendto in
snmpd_input(..).

MFC after:      3 weeks
Obtained from:  Isilon OneFS (^/onefs/branches/BR_8_0_0_DEV@r507595)
Submitted by:   Thor Steingrimsson <thor.steingrimsson@isilon.com>
Sponsored by:   Dell EMC Isilon
2016-12-28 04:29:09 +00:00
Enji Cooper
e1d581b289 style(9): clean up trailing whitespace
MFC after:	3 weeks
2016-12-27 23:32:54 +00:00
Enji Cooper
23516259fd style(9): fix trailing whitespace
MFC after:	3 days
2016-12-26 11:16:55 +00:00
Enji Cooper
3b7e3b0ae5 Update engine time using update_snmpd_engine_time(..)
MFC after:	6 days
X-MFC with:	r310498
Sponsored by:	Dell EMC Isilon
2016-12-26 11:11:30 +00:00
Enji Cooper
0077de5654 Fix return type for ret (recv callback) and sort variables by alignment
Again, for reasons I don't yet understand, this is not being flagged by the
compiler. Unlike the issue addressed in r310587, this problem existed prior
to r310586

MFC after:	2 weeks
X-MFC with:	r310586, r310587
2016-12-26 10:24:48 +00:00
Enji Cooper
2bc1d16ea7 Fix definition for recv_dgram(..); it should be "ssize_t", not "int"
I'm not sure why this wasn't flagged as an issue by the compiler, yet

MFC after:	3 weeks
X-MFC with:	r310586
2016-12-26 10:21:28 +00:00
Enji Cooper
0ba351ef58 Refactor transport sources a bit to facilitate changes coming down pipeline
Add recv callback to transport layer to better facilitate code reuse and
readability and for symmetry with send callback. Move recv_dgram and
recv_stream to udp_recv and lsock_recv, respectively, and make the
beforementioned functions recv callbacks for the udp and lsock transports,
respectively.

Consolidate the check_priv* functions in their relevant trans*.c source to
limit scope/use.

Note: this code is roughly based content from the submitter, although this
was modified to be more of a direct move from snmpd/main.c to the trans_*.c
sources, and to reduce unnecessary static function declarations.

MFC after:	2 weeks
Submitted by:	Thor Steingrimsson <thor.steingrimsson@isilon.com>
Sponsored by:	Dell EMC Isilon
2016-12-26 10:17:22 +00:00
Enji Cooper
0327a0e823 Fix style(9)
- Sort #includes
- Delete trailing whitespace

No functional change

MFC after:	3 days
2016-12-26 07:31:16 +00:00
Enji Cooper
9ca3777288 style(9): delete stray trailing whitespace after break statement
MFC after:	3 days
2016-12-24 11:49:25 +00:00
Enji Cooper
6521e5f846 Be more strict about IpAddress type in snmp_value_parse(..)
- Use inet_pton with AF_INET instead of doing longhand with sscanf.
- Use gethostbyname2 with AF_INET to ensure that the hostname isn't
  accidentally parsed with another address family, e.g. AF_INET6.

NB: IpAddress per RFC-2578 is IPv4 only. Work is in progress to add
    the InetAddress type and friends documented in RFC-4001 and
    elsewhere (which supports IPv4, IPv6, and more).

MFC after:	2 weeks
2016-12-24 11:41:16 +00:00
Enji Cooper
3b9712fa95 Minor style(9) fixes
- Trailing whitespace cleanup
- Sort variables in snmp_dialog(..) by alignment

No functional change

MFC after:	1 week
2016-12-24 11:30:24 +00:00
Enji Cooper
846157438d Sort #includes
No functional change

MFC after:	3 days
2016-12-24 11:26:55 +00:00