/src/net-snmp/snmplib/snmpv3.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * snmpv3.c |
3 | | * |
4 | | * Portions of this file are subject to the following copyright(s). See |
5 | | * the Net-SNMP's COPYING file for more details and other copyrights |
6 | | * that may apply: |
7 | | * |
8 | | * Portions of this file are copyrighted by: |
9 | | * Copyright (c) 2016 VMware, Inc. All rights reserved. |
10 | | * Use is subject to license terms specified in the COPYING file |
11 | | * distributed with the Net-SNMP package. |
12 | | */ |
13 | | |
14 | | #include <net-snmp/net-snmp-config.h> |
15 | | #include <errno.h> |
16 | | #ifdef HAVE_INTTYPES_H |
17 | | #include <inttypes.h> |
18 | | #endif |
19 | | #ifdef HAVE_LIMITS_H |
20 | | #include <limits.h> |
21 | | #endif |
22 | | #include <stdio.h> |
23 | | #include <sys/types.h> |
24 | | |
25 | | #ifdef TIME_WITH_SYS_TIME |
26 | | # include <sys/time.h> |
27 | | # include <time.h> |
28 | | #else |
29 | | # ifdef HAVE_SYS_TIME_H |
30 | | # include <sys/time.h> |
31 | | # else |
32 | | # include <time.h> |
33 | | # endif |
34 | | #endif |
35 | | #ifdef HAVE_SYS_TIMES_H |
36 | | #include <sys/times.h> |
37 | | #endif |
38 | | #ifdef HAVE_STRING_H |
39 | | #include <string.h> |
40 | | #else |
41 | | #include <strings.h> |
42 | | #endif |
43 | | #include <ctype.h> |
44 | | #ifdef HAVE_NETINET_IN_H |
45 | | #include <netinet/in.h> |
46 | | #endif |
47 | | #ifdef HAVE_UNISTD_H |
48 | | #include <unistd.h> |
49 | | #endif |
50 | | #ifdef HAVE_SYS_SOCKET_H |
51 | | #include <sys/socket.h> |
52 | | #endif |
53 | | #ifdef HAVE_NETDB_H |
54 | | #include <netdb.h> |
55 | | #endif |
56 | | #ifdef HAVE_STDLIB_H |
57 | | # include <stdlib.h> |
58 | | #endif |
59 | | |
60 | | /* |
61 | | * Stuff needed for getHwAddress(...) |
62 | | */ |
63 | | #ifdef HAVE_SYS_IOCTL_H |
64 | | # include <sys/ioctl.h> |
65 | | #endif |
66 | | #ifdef HAVE_NET_IF_H |
67 | | # include <net/if.h> |
68 | | #endif |
69 | | |
70 | | #include <net-snmp/types.h> |
71 | | #include <net-snmp/output_api.h> |
72 | | #include <net-snmp/config_api.h> |
73 | | #include <net-snmp/utilities.h> |
74 | | |
75 | | #include <net-snmp/library/snmpv3.h> |
76 | | #include <net-snmp/library/callback.h> |
77 | | #include <net-snmp/library/snmp_api.h> |
78 | | #include <net-snmp/library/lcd_time.h> |
79 | | #include <net-snmp/library/scapi.h> |
80 | | #include <net-snmp/library/keytools.h> |
81 | | #include <net-snmp/library/lcd_time.h> |
82 | | #include <net-snmp/library/snmp_secmod.h> |
83 | | #include <net-snmp/library/snmpusm.h> |
84 | | #include <net-snmp/library/transform_oids.h> |
85 | | |
86 | | #include <net-snmp/net-snmp-features.h> |
87 | | |
88 | | static u_long engineBoots = 1; |
89 | | static unsigned int engineIDType = ENGINEID_TYPE_NETSNMP_RND; |
90 | | static unsigned char *engineID = NULL; |
91 | | static size_t engineIDLength = 0; |
92 | | static unsigned char *engineIDNic = NULL; |
93 | | static unsigned int engineIDIsSet = 0; /* flag if ID set by config */ |
94 | | static unsigned char *oldEngineID = NULL; |
95 | | static size_t oldEngineIDLength = 0; |
96 | | static struct timeval snmpv3starttime; |
97 | | |
98 | | #if defined(IFHWADDRLEN) && defined(SIOCGIFHWADDR) |
99 | | static int getHwAddress(const char *networkDevice, char *addressOut); |
100 | | #endif |
101 | | |
102 | | /*******************************************************************-o-****** |
103 | | * snmpv3_secLevel_conf |
104 | | * |
105 | | * Parameters: |
106 | | * *word |
107 | | * *cptr |
108 | | * |
109 | | * Line syntax: |
110 | | * defSecurityLevel "noAuthNoPriv" | "authNoPriv" | "authPriv" |
111 | | */ |
112 | | |
113 | | int |
114 | 0 | parse_secLevel_conf(const char *word, char *cptr) { |
115 | 0 | if (strcasecmp(cptr, "noAuthNoPriv") == 0 || strcmp(cptr, "1") == 0 || |
116 | 0 | strcasecmp(cptr, "nanp") == 0) { |
117 | 0 | return SNMP_SEC_LEVEL_NOAUTH; |
118 | 0 | } else if (strcasecmp(cptr, "authNoPriv") == 0 || strcmp(cptr, "2") == 0 || |
119 | 0 | strcasecmp(cptr, "anp") == 0) { |
120 | 0 | return SNMP_SEC_LEVEL_AUTHNOPRIV; |
121 | 0 | } else if (strcasecmp(cptr, "authPriv") == 0 || strcmp(cptr, "3") == 0 || |
122 | 0 | strcasecmp(cptr, "ap") == 0) { |
123 | 0 | return SNMP_SEC_LEVEL_AUTHPRIV; |
124 | 0 | } else { |
125 | 0 | return -1; |
126 | 0 | } |
127 | 0 | } |
128 | | |
129 | | void |
130 | | snmpv3_secLevel_conf(const char *word, char *cptr) |
131 | 0 | { |
132 | 0 | int secLevel; |
133 | |
|
134 | 0 | if ((secLevel = parse_secLevel_conf( word, cptr )) >= 0 ) { |
135 | 0 | netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, |
136 | 0 | NETSNMP_DS_LIB_SECLEVEL, secLevel); |
137 | 0 | } else { |
138 | 0 | netsnmp_config_error("Unknown security level: %s", cptr); |
139 | 0 | } |
140 | 0 | DEBUGMSGTL(("snmpv3", "default secLevel set to: %s = %d\n", cptr, |
141 | 0 | netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, |
142 | 0 | NETSNMP_DS_LIB_SECLEVEL))); |
143 | 0 | } |
144 | | |
145 | | |
146 | | int |
147 | | snmpv3_parse_arg(int arg, char *optarg, netsnmp_session *session, char **Apsz, |
148 | | char **Xpsz, int argc, char *const *argv, int flags) |
149 | 0 | { |
150 | 0 | int priv_type; |
151 | 0 | char *cp; |
152 | 0 | int zero_sensitive = !( flags & NETSNMP_PARSE_ARGS_NOZERO ); |
153 | |
|
154 | 0 | switch (arg) { |
155 | | |
156 | 0 | case 'Z': |
157 | 0 | errno=0; |
158 | 0 | session->engineBoots = strtoul(optarg, &cp, 10); |
159 | 0 | if (errno || cp == optarg) { |
160 | 0 | fprintf(stderr, "Need engine boots value after -3Z flag.\n"); |
161 | 0 | return (-1); |
162 | 0 | } |
163 | 0 | if (*cp == ',') { |
164 | 0 | char *endptr; |
165 | 0 | cp++; |
166 | 0 | session->engineTime = strtoul(cp, &endptr, 10); |
167 | 0 | if (errno || cp == endptr) { |
168 | 0 | fprintf(stderr, "Need engine time after \"-3Z engineBoot,\".\n"); |
169 | 0 | return (-1); |
170 | 0 | } |
171 | 0 | } |
172 | | /* |
173 | | * Handle previous '-Z boot time' syntax |
174 | | */ |
175 | 0 | else if (optind < argc) { |
176 | 0 | session->engineTime = strtoul(argv[optind], &cp, 10); |
177 | 0 | if (errno || cp == argv[optind]) { |
178 | 0 | fprintf(stderr, "Need engine time after \"-Z engineBoot\".\n"); |
179 | 0 | return (-1); |
180 | 0 | } |
181 | 0 | } else { |
182 | 0 | fprintf(stderr, "Need engine time after \"-3Z engineBoot,\".\n"); |
183 | 0 | return (-1); |
184 | 0 | } |
185 | 0 | break; |
186 | | |
187 | 0 | case 'e':{ |
188 | 0 | size_t ebuf_len = 32, eout_len = 0; |
189 | 0 | u_char *ebuf = (u_char *) malloc(ebuf_len); |
190 | |
|
191 | 0 | if (ebuf == NULL) { |
192 | 0 | fprintf(stderr, "malloc failure processing -3e flag.\n"); |
193 | 0 | return (-1); |
194 | 0 | } |
195 | 0 | if (!snmp_hex_to_binary |
196 | 0 | (&ebuf, &ebuf_len, &eout_len, 1, optarg)) { |
197 | 0 | fprintf(stderr, "Bad engine ID value after -3e flag.\n"); |
198 | 0 | SNMP_FREE(ebuf); |
199 | 0 | return (-1); |
200 | 0 | } |
201 | 0 | if ((eout_len < 5) || (eout_len > 32)) { |
202 | 0 | fprintf(stderr, "Invalid engine ID value after -e flag.\n"); |
203 | 0 | free(ebuf); |
204 | 0 | return (-1); |
205 | 0 | } |
206 | 0 | free(session->securityEngineID); |
207 | 0 | session->securityEngineID = ebuf; |
208 | 0 | session->securityEngineIDLen = eout_len; |
209 | 0 | break; |
210 | 0 | } |
211 | | |
212 | 0 | case 'E':{ |
213 | 0 | size_t ebuf_len = 32, eout_len = 0; |
214 | 0 | u_char *ebuf = (u_char *) malloc(ebuf_len); |
215 | |
|
216 | 0 | if (ebuf == NULL) { |
217 | 0 | fprintf(stderr, "malloc failure processing -3E flag.\n"); |
218 | 0 | return (-1); |
219 | 0 | } |
220 | 0 | if (!snmp_hex_to_binary |
221 | 0 | (&ebuf, &ebuf_len, &eout_len, 1, optarg)) { |
222 | 0 | fprintf(stderr, "Bad engine ID value after -3E flag.\n"); |
223 | 0 | SNMP_FREE(ebuf); |
224 | 0 | return (-1); |
225 | 0 | } |
226 | 0 | if ((eout_len < 5) || (eout_len > 32)) { |
227 | 0 | fprintf(stderr, "Invalid engine ID value after -E flag.\n"); |
228 | 0 | free(ebuf); |
229 | 0 | return (-1); |
230 | 0 | } |
231 | 0 | free(session->contextEngineID); |
232 | 0 | session->contextEngineID = ebuf; |
233 | 0 | session->contextEngineIDLen = eout_len; |
234 | 0 | break; |
235 | 0 | } |
236 | | |
237 | 0 | case 'n': |
238 | 0 | free(session->contextName); |
239 | 0 | session->contextName = strdup(optarg); |
240 | 0 | session->contextNameLen = strlen(optarg); |
241 | 0 | break; |
242 | | |
243 | 0 | case 'u': |
244 | 0 | free(session->securityName); |
245 | 0 | session->securityName = strdup(optarg); |
246 | 0 | session->securityNameLen = strlen(optarg); |
247 | 0 | break; |
248 | | |
249 | 0 | case 'l': |
250 | 0 | if (!strcasecmp(optarg, "noAuthNoPriv") || !strcmp(optarg, "1") || |
251 | 0 | !strcasecmp(optarg, "noauth") || !strcasecmp(optarg, "nanp")) { |
252 | 0 | session->securityLevel = SNMP_SEC_LEVEL_NOAUTH; |
253 | 0 | } else if (!strcasecmp(optarg, "authNoPriv") |
254 | 0 | || !strcasecmp(optarg, "auth") |
255 | 0 | || !strcmp(optarg, "2") || !strcasecmp(optarg, "anp")) { |
256 | 0 | session->securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV; |
257 | 0 | } else if (!strcasecmp(optarg, "authPriv") || !strcmp(optarg, "3") |
258 | 0 | || !strcasecmp(optarg, "priv") |
259 | 0 | || !strcasecmp(optarg, "ap")) { |
260 | 0 | session->securityLevel = SNMP_SEC_LEVEL_AUTHPRIV; |
261 | 0 | } else { |
262 | 0 | fprintf(stderr, |
263 | 0 | "Invalid security level specified after -3l flag: %s\n", |
264 | 0 | optarg); |
265 | 0 | return (-1); |
266 | 0 | } |
267 | 0 | break; |
268 | | |
269 | 0 | #ifdef NETSNMP_SECMOD_USM |
270 | 0 | case 'a': { |
271 | 0 | int auth_type = usm_lookup_auth_type(optarg); |
272 | 0 | if (auth_type > 0) { |
273 | 0 | const oid *auth_proto; |
274 | |
|
275 | 0 | auth_proto = sc_get_auth_oid(auth_type, |
276 | 0 | &session->securityAuthProtoLen); |
277 | 0 | session->securityAuthProto = snmp_duplicate_objid(auth_proto, |
278 | 0 | session->securityAuthProtoLen); |
279 | 0 | } else { |
280 | 0 | fprintf(stderr, |
281 | 0 | "Invalid authentication protocol specified after -3a flag: %s\n", |
282 | 0 | optarg); |
283 | 0 | return (-1); |
284 | 0 | } |
285 | 0 | } |
286 | 0 | break; |
287 | | |
288 | 0 | case 'x': { |
289 | 0 | const oid *priv_proto; |
290 | |
|
291 | 0 | priv_type = usm_lookup_priv_type(optarg); |
292 | 0 | if (priv_type < 0) { |
293 | 0 | fprintf(stderr, |
294 | 0 | "Invalid privacy protocol specified after -3x flag: %s\n", |
295 | 0 | optarg); |
296 | 0 | return (-1); |
297 | 0 | } |
298 | 0 | priv_proto = sc_get_priv_oid(priv_type, &session->securityPrivProtoLen); |
299 | 0 | session->securityPrivProto = snmp_duplicate_objid(priv_proto, |
300 | 0 | session->securityPrivProtoLen); |
301 | 0 | break; |
302 | 0 | } |
303 | 0 | case 'A': |
304 | 0 | if (*Apsz && zero_sensitive) { |
305 | 0 | memset(*Apsz, 0x0, strlen(*Apsz)); |
306 | 0 | } |
307 | 0 | free(*Apsz); |
308 | 0 | *Apsz = strdup(optarg); |
309 | 0 | if (NULL == *Apsz) { |
310 | 0 | fprintf(stderr, "malloc failure processing -%c flag.\n", |
311 | 0 | (char)arg); |
312 | 0 | return -1; |
313 | 0 | } |
314 | 0 | if (zero_sensitive) |
315 | 0 | memset(optarg, 0x0, strlen(optarg)); |
316 | 0 | break; |
317 | | |
318 | 0 | case 'X': |
319 | 0 | if (*Xpsz && zero_sensitive) { |
320 | 0 | memset(*Xpsz, 0x0, strlen(*Xpsz)); |
321 | 0 | } |
322 | 0 | free(*Xpsz); |
323 | 0 | *Xpsz = strdup(optarg); |
324 | 0 | if (NULL == *Xpsz) { |
325 | 0 | fprintf(stderr, "malloc failure processing -%c flag.\n", |
326 | 0 | (char)arg); |
327 | 0 | return -1; |
328 | 0 | } |
329 | 0 | if (zero_sensitive) |
330 | 0 | memset(optarg, 0x0, strlen(optarg)); |
331 | 0 | break; |
332 | 0 | #endif /* NETSNMP_SECMOD_USM */ |
333 | | |
334 | 0 | case 'm': { |
335 | 0 | size_t bufSize = sizeof(session->securityAuthKey); |
336 | 0 | u_char *tmpp = session->securityAuthKey; |
337 | 0 | if (!snmp_hex_to_binary(&tmpp, &bufSize, |
338 | 0 | &session->securityAuthKeyLen, 0, optarg)) { |
339 | 0 | fprintf(stderr, "Bad key value after -3m flag.\n"); |
340 | 0 | return (-1); |
341 | 0 | } |
342 | 0 | break; |
343 | 0 | } |
344 | | |
345 | 0 | case 'M': { |
346 | 0 | size_t bufSize = sizeof(session->securityPrivKey); |
347 | 0 | u_char *tmpp = session->securityPrivKey; |
348 | 0 | if (!snmp_hex_to_binary(&tmpp, &bufSize, |
349 | 0 | &session->securityPrivKeyLen, 0, optarg)) { |
350 | 0 | fprintf(stderr, "Bad key value after -3M flag.\n"); |
351 | 0 | return (-1); |
352 | 0 | } |
353 | 0 | break; |
354 | 0 | } |
355 | | |
356 | 0 | case 'k': { |
357 | 0 | size_t kbuf_len = 32, kout_len = 0; |
358 | 0 | u_char *kbuf = (u_char *) malloc(kbuf_len); |
359 | |
|
360 | 0 | if (kbuf == NULL) { |
361 | 0 | fprintf(stderr, "malloc failure processing -3k flag.\n"); |
362 | 0 | return (-1); |
363 | 0 | } |
364 | 0 | if (!snmp_hex_to_binary |
365 | 0 | (&kbuf, &kbuf_len, &kout_len, 1, optarg)) { |
366 | 0 | fprintf(stderr, "Bad key value after -3k flag.\n"); |
367 | 0 | SNMP_FREE(kbuf); |
368 | 0 | return (-1); |
369 | 0 | } |
370 | 0 | free(session->securityAuthLocalKey); |
371 | 0 | session->securityAuthLocalKey = kbuf; |
372 | 0 | session->securityAuthLocalKeyLen = kout_len; |
373 | 0 | break; |
374 | 0 | } |
375 | | |
376 | 0 | case 'K': { |
377 | 0 | size_t kbuf_len = 32, kout_len = 0; |
378 | 0 | u_char *kbuf = (u_char *) malloc(kbuf_len); |
379 | |
|
380 | 0 | if (kbuf == NULL) { |
381 | 0 | fprintf(stderr, "malloc failure processing -3K flag.\n"); |
382 | 0 | return (-1); |
383 | 0 | } |
384 | 0 | if (!snmp_hex_to_binary |
385 | 0 | (&kbuf, &kbuf_len, &kout_len, 1, optarg)) { |
386 | 0 | fprintf(stderr, "Bad key value after -3K flag.\n"); |
387 | 0 | SNMP_FREE(kbuf); |
388 | 0 | return (-1); |
389 | 0 | } |
390 | 0 | free(session->securityPrivLocalKey); |
391 | 0 | session->securityPrivLocalKey = kbuf; |
392 | 0 | session->securityPrivLocalKeyLen = kout_len; |
393 | 0 | break; |
394 | 0 | } |
395 | | |
396 | 0 | default: |
397 | 0 | fprintf(stderr, "Unknown SNMPv3 option passed to -3: %c.\n", arg); |
398 | 0 | return -1; |
399 | 0 | } |
400 | 0 | return 0; |
401 | 0 | } |
402 | | |
403 | | int |
404 | | snmpv3_parse_args(char *optarg, netsnmp_session * session, char **Apsz, |
405 | | char **Xpsz, int argc, char *const *argv, int flags) |
406 | 0 | { |
407 | 0 | char *cp = optarg; |
408 | 0 | optarg++; |
409 | | /* |
410 | | * Support '... -3x=value ....' syntax |
411 | | */ |
412 | 0 | if (*optarg == '=') { |
413 | 0 | optarg++; |
414 | 0 | } |
415 | | /* |
416 | | * and '.... "-3x value" ....' (*with* the quotes) |
417 | | */ |
418 | 0 | while (*optarg && isspace((unsigned char)(*optarg))) { |
419 | 0 | optarg++; |
420 | 0 | } |
421 | | /* |
422 | | * Finally, handle ".... -3x value ...." syntax |
423 | | * (*without* surrounding quotes) |
424 | | */ |
425 | 0 | if (!*optarg) { |
426 | | /* |
427 | | * We've run off the end of the argument |
428 | | * so move on the the next. |
429 | | */ |
430 | 0 | if (optind >= argc) { |
431 | 0 | fprintf(stderr, |
432 | 0 | "Missing argument after SNMPv3 '-3%c' option.\n", *cp); |
433 | 0 | return (-1); |
434 | 0 | } |
435 | 0 | optarg = argv[optind++]; |
436 | 0 | } |
437 | | |
438 | 0 | return snmpv3_parse_arg(*cp, optarg, session, Apsz, Xpsz, argc, argv, |
439 | 0 | flags); |
440 | 0 | } |
441 | | |
442 | | /*******************************************************************-o-****** |
443 | | * setup_engineID |
444 | | * |
445 | | * Parameters: |
446 | | * **eidp |
447 | | * *text Printable (?) text to be plugged into the snmpEngineID. |
448 | | * |
449 | | * Return: |
450 | | * Length of allocated engineID string in bytes, -OR- |
451 | | * -1 on error. |
452 | | * |
453 | | * |
454 | | * Create an snmpEngineID using text and the local IP address. If eidp |
455 | | * is defined, use it to return a pointer to the newly allocated data. |
456 | | * Otherwise, use the result to define engineID defined in this module. |
457 | | * |
458 | | * Line syntax: |
459 | | * engineID <text> | NULL |
460 | | * |
461 | | * XXX What if a node has multiple interfaces? |
462 | | * XXX What if multiple engines all choose the same address? |
463 | | * (answer: You're screwed, because you might need a kul database |
464 | | * which is dependent on the current engineID. Enumeration and other |
465 | | * tricks won't work). |
466 | | */ |
467 | | int |
468 | | setup_engineID(u_char ** eidp, const char *text) |
469 | 3.06k | { |
470 | 3.06k | int enterpriseid = htonl(NETSNMP_ENTERPRISE_OID), |
471 | 3.06k | netsnmpoid = htonl(NETSNMP_OID), |
472 | 3.06k | localsetup = (eidp) ? 0 : 1; |
473 | | |
474 | | /* |
475 | | * Use local engineID if *eidp == NULL. |
476 | | */ |
477 | 3.06k | #ifdef HAVE_GETHOSTNAME |
478 | 3.06k | u_char buf[SNMP_MAXBUF_SMALL]; |
479 | 3.06k | struct hostent *hent = NULL; |
480 | 3.06k | #endif |
481 | 3.06k | u_char *bufp = NULL; |
482 | 3.06k | size_t len; |
483 | 3.06k | int localEngineIDType = engineIDType; |
484 | 3.06k | int tmpint; |
485 | 3.06k | time_t tmptime; |
486 | | |
487 | 3.06k | engineIDIsSet = 1; |
488 | | |
489 | 3.06k | #ifdef HAVE_GETHOSTNAME |
490 | 3.06k | #ifdef AF_INET6 |
491 | | /* |
492 | | * see if they selected IPV4 or IPV6 support |
493 | | */ |
494 | 3.06k | if ((ENGINEID_TYPE_IPV6 == localEngineIDType) || |
495 | 3.06k | (ENGINEID_TYPE_IPV4 == localEngineIDType)) { |
496 | | /* |
497 | | * get the host name and save the information |
498 | | */ |
499 | 0 | gethostname((char *) buf, sizeof(buf)); |
500 | 0 | hent = netsnmp_gethostbyname((char *) buf); |
501 | 0 | if (hent && hent->h_addrtype == AF_INET6) { |
502 | 0 | localEngineIDType = ENGINEID_TYPE_IPV6; |
503 | 0 | } else { |
504 | | /* |
505 | | * Not IPV6 so we go with default |
506 | | */ |
507 | 0 | localEngineIDType = ENGINEID_TYPE_IPV4; |
508 | 0 | } |
509 | 0 | } |
510 | | #else |
511 | | /* |
512 | | * No IPV6 support. Check if they selected IPV6 engineID type. |
513 | | * If so make it IPV4 instead |
514 | | */ |
515 | | if (ENGINEID_TYPE_IPV6 == localEngineIDType) { |
516 | | localEngineIDType = ENGINEID_TYPE_IPV4; |
517 | | } |
518 | | if (ENGINEID_TYPE_IPV4 == localEngineIDType) { |
519 | | /* |
520 | | * get the host name and save the information |
521 | | */ |
522 | | gethostname((char *) buf, sizeof(buf)); |
523 | | hent = netsnmp_gethostbyname((char *) buf); |
524 | | } |
525 | | #endif |
526 | 3.06k | #endif /* HAVE_GETHOSTNAME */ |
527 | | |
528 | | /* |
529 | | * Determine if we have text and if so setup our localEngineIDType |
530 | | * * appropriately. |
531 | | */ |
532 | 3.06k | if (NULL != text) { |
533 | 0 | engineIDType = localEngineIDType = ENGINEID_TYPE_TEXT; |
534 | 0 | } |
535 | | /* |
536 | | * Determine length of the engineID string. |
537 | | */ |
538 | 3.06k | len = 5; /* always have 5 leading bytes */ |
539 | 3.06k | switch (localEngineIDType) { |
540 | 0 | case ENGINEID_TYPE_TEXT: |
541 | 0 | if (NULL == text) { |
542 | 0 | snmp_log(LOG_ERR, |
543 | 0 | "Can't set up engineID of type text from an empty string.\n"); |
544 | 0 | return -1; |
545 | 0 | } |
546 | 0 | len += strlen(text); /* 5 leading bytes+text. No NULL char */ |
547 | 0 | break; |
548 | 0 | #if defined(IFHWADDRLEN) && defined(SIOCGIFHWADDR) |
549 | 0 | case ENGINEID_TYPE_MACADDR: /* MAC address */ |
550 | 0 | len += 6; /* + 6 bytes for MAC address */ |
551 | 0 | break; |
552 | 0 | #endif |
553 | 0 | case ENGINEID_TYPE_IPV4: /* IPv4 */ |
554 | 0 | len += 4; /* + 4 byte IPV4 address */ |
555 | 0 | break; |
556 | 0 | case ENGINEID_TYPE_IPV6: /* IPv6 */ |
557 | 0 | len += 16; /* + 16 byte IPV6 address */ |
558 | 0 | break; |
559 | 3.06k | case ENGINEID_TYPE_NETSNMP_RND: /* Net-SNMP specific encoding */ |
560 | 3.06k | if (engineID) /* already setup, keep current value */ |
561 | 0 | return engineIDLength; |
562 | 3.06k | if (oldEngineID) { |
563 | 3.06k | len = oldEngineIDLength; |
564 | 3.06k | } else { |
565 | 2 | len += sizeof(int) + sizeof(time_t); |
566 | 2 | } |
567 | 3.06k | break; |
568 | 0 | default: |
569 | 0 | snmp_log(LOG_ERR, |
570 | 0 | "Unknown EngineID type requested for setup (%d). Using IPv4.\n", |
571 | 0 | localEngineIDType); |
572 | 0 | localEngineIDType = ENGINEID_TYPE_IPV4; /* make into IPV4 */ |
573 | 0 | len += 4; /* + 4 byte IPv4 address */ |
574 | 0 | break; |
575 | 3.06k | } /* switch */ |
576 | | |
577 | | |
578 | | /* |
579 | | * Allocate memory and store enterprise ID. |
580 | | */ |
581 | 3.06k | if ((bufp = calloc(1, len)) == NULL) { |
582 | 0 | snmp_log_perror("setup_engineID malloc"); |
583 | 0 | return -1; |
584 | 0 | } |
585 | 3.06k | if (localEngineIDType == ENGINEID_TYPE_NETSNMP_RND) |
586 | | /* |
587 | | * we must use the net-snmp enterprise id here, regardless |
588 | | */ |
589 | 3.06k | memcpy(bufp, &netsnmpoid, sizeof(netsnmpoid)); /* XXX Must be 4 bytes! */ |
590 | 0 | else |
591 | 0 | memcpy(bufp, &enterpriseid, sizeof(enterpriseid)); /* XXX Must be 4 bytes! */ |
592 | | |
593 | 3.06k | bufp[0] |= 0x80; |
594 | | |
595 | | |
596 | | /* |
597 | | * Store the given text -OR- the first found IP address |
598 | | * -OR- the MAC address -OR- random elements |
599 | | * (the latter being the recommended default) |
600 | | */ |
601 | 3.06k | switch (localEngineIDType) { |
602 | 3.06k | case ENGINEID_TYPE_NETSNMP_RND: |
603 | 3.06k | if (oldEngineID) { |
604 | | /* |
605 | | * keep our previous notion of the engineID |
606 | | */ |
607 | 3.06k | memcpy(bufp, oldEngineID, oldEngineIDLength); |
608 | 3.06k | } else { |
609 | | /* |
610 | | * Here we've desigend our own ENGINEID that is not based on |
611 | | * an address which may change and may even become conflicting |
612 | | * in the future like most of the default v3 engineID types |
613 | | * suffer from. |
614 | | * |
615 | | * Ours is built from 2 fairly random elements: a random number and |
616 | | * the current time in seconds. This method suffers from boxes |
617 | | * that may not have a correct clock setting and random number |
618 | | * seed at startup, but few OSes should have that problem. |
619 | | */ |
620 | 2 | bufp[4] = ENGINEID_TYPE_NETSNMP_RND; |
621 | 2 | tmpint = netsnmp_random(); |
622 | 2 | memcpy(bufp + 5, &tmpint, sizeof(tmpint)); |
623 | 2 | tmptime = time(NULL); |
624 | 2 | memcpy(bufp + 5 + sizeof(tmpint), &tmptime, sizeof(tmptime)); |
625 | 2 | } |
626 | 3.06k | break; |
627 | 0 | case ENGINEID_TYPE_TEXT: |
628 | 0 | bufp[4] = ENGINEID_TYPE_TEXT; |
629 | 0 | memcpy((char *) bufp + 5, (text), strlen(text)); |
630 | 0 | break; |
631 | 0 | #ifdef HAVE_GETHOSTNAME |
632 | 0 | #ifdef AF_INET6 |
633 | 0 | case ENGINEID_TYPE_IPV6: |
634 | 0 | bufp[4] = ENGINEID_TYPE_IPV6; |
635 | 0 | if (hent) |
636 | 0 | memcpy(bufp + 5, hent->h_addr_list[0], hent->h_length); |
637 | 0 | break; |
638 | 0 | #endif |
639 | 0 | #endif |
640 | 0 | #if defined(IFHWADDRLEN) && defined(SIOCGIFHWADDR) |
641 | 0 | case ENGINEID_TYPE_MACADDR: |
642 | 0 | { |
643 | 0 | int x; |
644 | 0 | bufp[4] = ENGINEID_TYPE_MACADDR; |
645 | | /* |
646 | | * use default NIC if none provided |
647 | | */ |
648 | 0 | if (NULL == engineIDNic) { |
649 | 0 | x = getHwAddress(DEFAULT_NIC, (char *)&bufp[5]); |
650 | 0 | } else { |
651 | 0 | x = getHwAddress((char *)engineIDNic, (char *)&bufp[5]); |
652 | 0 | } |
653 | 0 | if (0 != x) |
654 | | /* |
655 | | * function failed fill MAC address with zeros |
656 | | */ |
657 | 0 | { |
658 | 0 | memset(&bufp[5], 0, 6); |
659 | 0 | } |
660 | 0 | } |
661 | 0 | break; |
662 | 0 | #endif |
663 | 0 | case ENGINEID_TYPE_IPV4: |
664 | 0 | default: |
665 | 0 | bufp[4] = ENGINEID_TYPE_IPV4; |
666 | 0 | #ifdef HAVE_GETHOSTNAME |
667 | 0 | if (hent && hent->h_addrtype == AF_INET) { |
668 | 0 | memcpy(bufp + 5, hent->h_addr_list[0], hent->h_length); |
669 | 0 | } else { /* Unknown address type. Default to 127.0.0.1. */ |
670 | |
|
671 | 0 | bufp[5] = 127; |
672 | 0 | bufp[6] = 0; |
673 | 0 | bufp[7] = 0; |
674 | 0 | bufp[8] = 1; |
675 | 0 | } |
676 | | #else /* HAVE_GETHOSTNAME */ |
677 | | /* |
678 | | * Unknown address type. Default to 127.0.0.1. |
679 | | */ |
680 | | bufp[5] = 127; |
681 | | bufp[6] = 0; |
682 | | bufp[7] = 0; |
683 | | bufp[8] = 1; |
684 | | #endif /* HAVE_GETHOSTNAME */ |
685 | 0 | break; |
686 | 3.06k | } |
687 | | |
688 | | /* |
689 | | * Pass the string back to the calling environment, or use it for |
690 | | * our local engineID. |
691 | | */ |
692 | 3.06k | if (localsetup) { |
693 | 3.06k | SNMP_FREE(engineID); |
694 | 3.06k | engineID = bufp; |
695 | 3.06k | engineIDLength = len; |
696 | | |
697 | 3.06k | } else { |
698 | 0 | *eidp = bufp; |
699 | 0 | } |
700 | | |
701 | | |
702 | 3.06k | return len; |
703 | | |
704 | 3.06k | } /* end setup_engineID() */ |
705 | | |
706 | | int |
707 | | free_engineID(int majorid, int minorid, void *serverarg, |
708 | | void *clientarg) |
709 | 3.06k | { |
710 | 3.06k | SNMP_FREE(engineID); |
711 | 3.06k | SNMP_FREE(engineIDNic); |
712 | 3.06k | SNMP_FREE(oldEngineID); |
713 | 3.06k | engineIDIsSet = 0; |
714 | 3.06k | return 0; |
715 | 3.06k | } |
716 | | |
717 | | /*******************************************************************-o-****** |
718 | | * engineBoots_conf |
719 | | * |
720 | | * Parameters: |
721 | | * *word |
722 | | * *cptr |
723 | | * |
724 | | * Line syntax: |
725 | | * engineBoots <num_boots> |
726 | | */ |
727 | | void |
728 | | engineBoots_conf(const char *word, char *cptr) |
729 | 3.06k | { |
730 | 3.06k | engineBoots = atoi(cptr) + 1; |
731 | 3.06k | DEBUGMSGTL(("snmpv3", "engineBoots: %lu\n", engineBoots)); |
732 | 3.06k | } |
733 | | |
734 | | /*******************************************************************-o-****** |
735 | | * engineIDType_conf |
736 | | * |
737 | | * Parameters: |
738 | | * *word |
739 | | * *cptr |
740 | | * |
741 | | * Line syntax: |
742 | | * engineIDType <1 or 3> |
743 | | * 1 is default for IPv4 engine ID type. Will automatically |
744 | | * chose between IPv4 & IPv6 if either 1 or 2 is specified. |
745 | | * 2 is for IPv6. |
746 | | * 3 is hardware (MAC) address, currently supported under Linux |
747 | | */ |
748 | | void |
749 | | engineIDType_conf(const char *word, char *cptr) |
750 | 0 | { |
751 | 0 | engineIDType = atoi(cptr); |
752 | | /* |
753 | | * verify valid type selected |
754 | | */ |
755 | 0 | switch (engineIDType) { |
756 | 0 | case ENGINEID_TYPE_IPV4: /* IPv4 */ |
757 | 0 | case ENGINEID_TYPE_IPV6: /* IPv6 */ |
758 | | /* |
759 | | * IPV? is always good |
760 | | */ |
761 | 0 | break; |
762 | 0 | #if defined(IFHWADDRLEN) && defined(SIOCGIFHWADDR) |
763 | 0 | case ENGINEID_TYPE_MACADDR: /* MAC address */ |
764 | 0 | break; |
765 | 0 | #endif |
766 | 0 | default: |
767 | | /* |
768 | | * unsupported one chosen |
769 | | */ |
770 | 0 | config_perror("Unsupported enginedIDType, forcing IPv4"); |
771 | 0 | engineIDType = ENGINEID_TYPE_IPV4; |
772 | 0 | } |
773 | 0 | DEBUGMSGTL(("snmpv3", "engineIDType: %d\n", engineIDType)); |
774 | 0 | } |
775 | | |
776 | | /*******************************************************************-o-****** |
777 | | * engineIDNic_conf |
778 | | * |
779 | | * Parameters: |
780 | | * *word |
781 | | * *cptr |
782 | | * |
783 | | * Line syntax: |
784 | | * engineIDNic <string> |
785 | | * eth0 is default |
786 | | */ |
787 | | void |
788 | | engineIDNic_conf(const char *word, char *cptr) |
789 | 0 | { |
790 | | /* |
791 | | * Make sure they haven't already specified the engineID via the |
792 | | * * configuration file |
793 | | */ |
794 | 0 | if (0 == engineIDIsSet) |
795 | | /* |
796 | | * engineID has NOT been set via configuration file |
797 | | */ |
798 | 0 | { |
799 | | /* |
800 | | * See if already set if so erase & release it |
801 | | */ |
802 | 0 | SNMP_FREE(engineIDNic); |
803 | 0 | engineIDNic = (u_char *) malloc(strlen(cptr) + 1); |
804 | 0 | if (NULL != engineIDNic) { |
805 | 0 | strcpy((char *) engineIDNic, cptr); |
806 | 0 | DEBUGMSGTL(("snmpv3", "Initializing engineIDNic: %s\n", |
807 | 0 | engineIDNic)); |
808 | 0 | } else { |
809 | 0 | DEBUGMSGTL(("snmpv3", |
810 | 0 | "Error allocating memory for engineIDNic!\n")); |
811 | 0 | } |
812 | 0 | } else { |
813 | 0 | DEBUGMSGTL(("snmpv3", |
814 | 0 | "NOT setting engineIDNic, engineID already set\n")); |
815 | 0 | } |
816 | 0 | } |
817 | | |
818 | | /*******************************************************************-o-****** |
819 | | * engineID_conf |
820 | | * |
821 | | * Parameters: |
822 | | * *word |
823 | | * *cptr |
824 | | * |
825 | | * This function reads a string from the configuration file and uses that |
826 | | * string to initialize the engineID. It's assumed to be human readable. |
827 | | */ |
828 | | void |
829 | | engineID_conf(const char *word, char *cptr) |
830 | 0 | { |
831 | 0 | setup_engineID(NULL, cptr); |
832 | 0 | DEBUGMSGTL(("snmpv3", "initialized engineID with: %s\n", cptr)); |
833 | 0 | } |
834 | | |
835 | | void |
836 | | version_conf(const char *word, char *cptr) |
837 | 0 | { |
838 | 0 | int valid = 0; |
839 | 0 | #ifndef NETSNMP_DISABLE_SNMPV1 |
840 | 0 | if ((strcmp(cptr, "1") == 0) || |
841 | 0 | (strcmp(cptr, "v1") == 0)) { |
842 | 0 | netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_SNMPVERSION, |
843 | 0 | NETSNMP_DS_SNMP_VERSION_1); /* bogus value */ |
844 | 0 | valid = 1; |
845 | 0 | } |
846 | 0 | #endif |
847 | 0 | #ifndef NETSNMP_DISABLE_SNMPV2C |
848 | 0 | if ((strcasecmp(cptr, "2c") == 0) || |
849 | 0 | (strcasecmp(cptr, "v2c") == 0)) { |
850 | 0 | netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_SNMPVERSION, |
851 | 0 | NETSNMP_DS_SNMP_VERSION_2c); |
852 | 0 | valid = 1; |
853 | 0 | } |
854 | 0 | #endif |
855 | 0 | if ((strcasecmp(cptr, "3" ) == 0) || |
856 | 0 | (strcasecmp(cptr, "v3" ) == 0)) { |
857 | 0 | netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_SNMPVERSION, |
858 | 0 | NETSNMP_DS_SNMP_VERSION_3); |
859 | 0 | valid = 1; |
860 | 0 | } |
861 | 0 | if (!valid) { |
862 | 0 | config_perror("Unknown version specification"); |
863 | 0 | return; |
864 | 0 | } |
865 | 0 | DEBUGMSGTL(("snmpv3", "set default version to %d\n", |
866 | 0 | netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, |
867 | 0 | NETSNMP_DS_LIB_SNMPVERSION))); |
868 | 0 | } |
869 | | |
870 | | /* |
871 | | * oldengineID_conf(const char *, char *): |
872 | | * |
873 | | * Reads a octet string encoded engineID into the oldEngineID and |
874 | | * oldEngineIDLen pointers. |
875 | | */ |
876 | | void |
877 | | oldengineID_conf(const char *word, char *cptr) |
878 | 3.06k | { |
879 | 3.06k | if (oldEngineID) { |
880 | 0 | free(oldEngineID); |
881 | 0 | oldEngineID = NULL; |
882 | 0 | oldEngineIDLength = 0; |
883 | 0 | } |
884 | 3.06k | read_config_read_octet_string(cptr, &oldEngineID, &oldEngineIDLength); |
885 | 3.06k | } |
886 | | |
887 | | /* |
888 | | * set_exact_engineID_conf(const oid *, size_t): |
889 | | * |
890 | | * Specifies an exact engineID OID. |
891 | | */ |
892 | | int |
893 | | set_exact_engineID(const u_char *id, size_t len) |
894 | 0 | { |
895 | 0 | int rc = SNMPERR_SUCCESS; |
896 | 0 | u_char *newID = NULL; |
897 | |
|
898 | 0 | if (NULL == id || 0 == len) |
899 | 0 | return SNMPERR_GENERR; |
900 | | |
901 | 0 | if (len > MAX_ENGINEID_LENGTH) |
902 | 0 | return SNMPERR_TOO_LONG; |
903 | | |
904 | 0 | newID = malloc(len+1); |
905 | 0 | if (NULL == newID) { |
906 | 0 | snmp_log(LOG_ERR, "malloc failed for engineID\n"); |
907 | 0 | return SNMPERR_GENERR; |
908 | 0 | } |
909 | 0 | if (NULL != engineID) |
910 | 0 | free(engineID); |
911 | |
|
912 | 0 | memcpy(newID, id, len); |
913 | 0 | newID[len] = 0; |
914 | |
|
915 | 0 | engineID = newID; |
916 | 0 | engineIDLength = len; |
917 | 0 | engineIDIsSet = 1; |
918 | 0 | engineIDType = ENGINEID_TYPE_EXACT; |
919 | |
|
920 | 0 | return rc; |
921 | 0 | } |
922 | | |
923 | | /* |
924 | | * exactEngineID_conf(const char *, char *): |
925 | | * |
926 | | * Reads a octet string encoded engineID into the engineID and |
927 | | * engineIDLen pointers. |
928 | | */ |
929 | | void |
930 | | exactEngineID_conf(const char *word, char *cptr) |
931 | 0 | { |
932 | | /** we want buf > max so we know if there is truncation */ |
933 | 0 | u_char new_engineID[MAX_ENGINEID_LENGTH+2], |
934 | 0 | *new_engineIDptr = new_engineID; |
935 | 0 | size_t new_engineIDLength = sizeof(new_engineID); |
936 | |
|
937 | 0 | read_config_read_octet_string(cptr, &new_engineIDptr, &new_engineIDLength); |
938 | 0 | if (new_engineIDLength > MAX_ENGINEID_LENGTH) { |
939 | 0 | new_engineIDLength = MAX_ENGINEID_LENGTH; |
940 | 0 | netsnmp_config_error( |
941 | 0 | "exactEngineID '%s' too long; truncating to %d bytes", |
942 | 0 | cptr, MAX_ENGINEID_LENGTH); |
943 | 0 | } |
944 | |
|
945 | 0 | set_exact_engineID( new_engineIDptr, new_engineIDLength); |
946 | 0 | } |
947 | | |
948 | | |
949 | | /* |
950 | | * merely call |
951 | | */ |
952 | | netsnmp_feature_child_of(get_enginetime_alarm, netsnmp_unused); |
953 | | #ifndef NETSNMP_FEATURE_REMOVE_GET_ENGINETIME_ALARM |
954 | | void |
955 | | get_enginetime_alarm(unsigned int regnum, void *clientargs) |
956 | 0 | { |
957 | | /* we do this every so (rarely) often just to make sure we watch |
958 | | wrapping of the times() output */ |
959 | 0 | snmpv3_local_snmpEngineTime(); |
960 | 0 | } |
961 | | #endif /* NETSNMP_FEATURE_REMOVE_GET_ENGINETIME_ALARM */ |
962 | | |
963 | | /*******************************************************************-o-****** |
964 | | * init_snmpv3 |
965 | | * |
966 | | * Parameters: |
967 | | * *type Label for the config file "type" used by calling entity. |
968 | | * |
969 | | * Set time and engineID. |
970 | | * Set parsing functions for config file tokens. |
971 | | * Initialize SNMP Crypto API (SCAPI). |
972 | | */ |
973 | | void |
974 | | init_snmpv3(const char *type) |
975 | 3.06k | { |
976 | 3.06k | netsnmp_get_monotonic_clock(&snmpv3starttime); |
977 | | |
978 | 3.06k | if (!type) |
979 | 0 | type = "__snmpapp__"; |
980 | | |
981 | | /* |
982 | | * we need to be called back later |
983 | | */ |
984 | 3.06k | snmp_register_callback(SNMP_CALLBACK_LIBRARY, |
985 | 3.06k | SNMP_CALLBACK_POST_READ_CONFIG, |
986 | 3.06k | init_snmpv3_post_config, NULL); |
987 | | |
988 | 3.06k | snmp_register_callback(SNMP_CALLBACK_LIBRARY, |
989 | 3.06k | SNMP_CALLBACK_POST_PREMIB_READ_CONFIG, |
990 | 3.06k | init_snmpv3_post_premib_config, NULL); |
991 | | /* |
992 | | * we need to be called back later |
993 | | */ |
994 | 3.06k | snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA, |
995 | 3.06k | snmpv3_store, (void *) strdup(type)); |
996 | | |
997 | | /* |
998 | | * initialize submodules |
999 | | */ |
1000 | | /* |
1001 | | * NOTE: this must be after the callbacks are registered above, |
1002 | | * since they need to be called before the USM callbacks. |
1003 | | */ |
1004 | 3.06k | init_secmod(); |
1005 | | |
1006 | | /* |
1007 | | * register all our configuration handlers (ack, there's a lot) |
1008 | | */ |
1009 | | |
1010 | | /* |
1011 | | * handle engineID setup before everything else which may depend on it |
1012 | | */ |
1013 | 3.06k | register_prenetsnmp_mib_handler(type, "engineID", engineID_conf, NULL, |
1014 | 3.06k | "string"); |
1015 | 3.06k | register_prenetsnmp_mib_handler(type, "oldEngineID", oldengineID_conf, |
1016 | 3.06k | NULL, NULL); |
1017 | 3.06k | register_prenetsnmp_mib_handler(type, "exactEngineID", exactEngineID_conf, |
1018 | 3.06k | NULL, NULL); |
1019 | 3.06k | register_prenetsnmp_mib_handler(type, "engineIDType", |
1020 | 3.06k | engineIDType_conf, NULL, "num"); |
1021 | 3.06k | register_prenetsnmp_mib_handler(type, "engineIDNic", engineIDNic_conf, |
1022 | 3.06k | NULL, "string"); |
1023 | 3.06k | register_config_handler(type, "engineBoots", engineBoots_conf, NULL, |
1024 | 3.06k | NULL); |
1025 | | |
1026 | | /* |
1027 | | * default store config entries |
1028 | | */ |
1029 | 3.06k | netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "defSecurityName", |
1030 | 3.06k | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_SECNAME); |
1031 | 3.06k | netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "defContext", |
1032 | 3.06k | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_CONTEXT); |
1033 | 3.06k | netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "defPassphrase", |
1034 | 3.06k | NETSNMP_DS_LIBRARY_ID, |
1035 | 3.06k | NETSNMP_DS_LIB_PASSPHRASE); |
1036 | 3.06k | netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "defAuthPassphrase", |
1037 | 3.06k | NETSNMP_DS_LIBRARY_ID, |
1038 | 3.06k | NETSNMP_DS_LIB_AUTHPASSPHRASE); |
1039 | 3.06k | netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "defPrivPassphrase", |
1040 | 3.06k | NETSNMP_DS_LIBRARY_ID, |
1041 | 3.06k | NETSNMP_DS_LIB_PRIVPASSPHRASE); |
1042 | 3.06k | netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "defAuthMasterKey", |
1043 | 3.06k | NETSNMP_DS_LIBRARY_ID, |
1044 | 3.06k | NETSNMP_DS_LIB_AUTHMASTERKEY); |
1045 | 3.06k | netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "defPrivMasterKey", |
1046 | 3.06k | NETSNMP_DS_LIBRARY_ID, |
1047 | 3.06k | NETSNMP_DS_LIB_PRIVMASTERKEY); |
1048 | 3.06k | netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "defAuthLocalizedKey", |
1049 | 3.06k | NETSNMP_DS_LIBRARY_ID, |
1050 | 3.06k | NETSNMP_DS_LIB_AUTHLOCALIZEDKEY); |
1051 | 3.06k | netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "defPrivLocalizedKey", |
1052 | 3.06k | NETSNMP_DS_LIBRARY_ID, |
1053 | 3.06k | NETSNMP_DS_LIB_PRIVLOCALIZEDKEY); |
1054 | 3.06k | register_config_handler("snmp", "defVersion", version_conf, NULL, |
1055 | 3.06k | "1|2c|3"); |
1056 | | |
1057 | 3.06k | register_config_handler("snmp", "defSecurityLevel", |
1058 | 3.06k | snmpv3_secLevel_conf, NULL, |
1059 | 3.06k | "noAuthNoPriv|authNoPriv|authPriv"); |
1060 | 3.06k | } |
1061 | | |
1062 | | /* |
1063 | | * initializations for SNMPv3 to be called after the configuration files |
1064 | | * have been read. |
1065 | | */ |
1066 | | |
1067 | | int |
1068 | | init_snmpv3_post_config(int majorid, int minorid, void *serverarg, |
1069 | | void *clientarg) |
1070 | 3.06k | { |
1071 | | |
1072 | 3.06k | size_t engineIDLen; |
1073 | 3.06k | u_char *c_engineID; |
1074 | 3.06k | u_long localEngineTime; |
1075 | 3.06k | u_long localEngineBoots; |
1076 | | |
1077 | 3.06k | c_engineID = snmpv3_generate_engineID(&engineIDLen); |
1078 | | |
1079 | 3.06k | if (!c_engineID || engineIDLen == 0) { |
1080 | | /* |
1081 | | * Something went wrong - help! |
1082 | | */ |
1083 | 0 | SNMP_FREE(c_engineID); |
1084 | 0 | return SNMPERR_GENERR; |
1085 | 0 | } |
1086 | | |
1087 | | /* |
1088 | | * if our engineID has changed at all, the boots record must be set to 1 |
1089 | | */ |
1090 | 3.06k | if (engineIDLen != oldEngineIDLength || |
1091 | 3.06k | oldEngineID == NULL || c_engineID == NULL || |
1092 | 3.06k | memcmp(oldEngineID, c_engineID, engineIDLen) != 0) { |
1093 | 2 | engineBoots = 1; |
1094 | 2 | } |
1095 | | |
1096 | 3.06k | #ifdef NETSNMP_SECMOD_USM |
1097 | | /* |
1098 | | * for USM set our local engineTime in the LCD timing cache |
1099 | | */ |
1100 | 3.06k | localEngineTime = snmpv3_local_snmpEngineTime(); |
1101 | 3.06k | localEngineBoots = snmpv3_local_snmpEngineBoots(); |
1102 | 3.06k | set_enginetime(c_engineID, engineIDLen, |
1103 | 3.06k | localEngineBoots, |
1104 | 3.06k | localEngineTime, TRUE); |
1105 | 3.06k | #endif /* NETSNMP_SECMOD_USM */ |
1106 | | |
1107 | 3.06k | SNMP_FREE(c_engineID); |
1108 | 3.06k | return SNMPERR_SUCCESS; |
1109 | 3.06k | } |
1110 | | |
1111 | | int |
1112 | | init_snmpv3_post_premib_config(int majorid, int minorid, void *serverarg, |
1113 | | void *clientarg) |
1114 | 3.06k | { |
1115 | 3.06k | if (!engineIDIsSet) |
1116 | 3.06k | setup_engineID(NULL, NULL); |
1117 | | |
1118 | 3.06k | return SNMPERR_SUCCESS; |
1119 | 3.06k | } |
1120 | | |
1121 | | /*******************************************************************-o-****** |
1122 | | * store_snmpv3 |
1123 | | * |
1124 | | * Parameters: |
1125 | | * *type |
1126 | | */ |
1127 | | int |
1128 | | snmpv3_store(int majorID, int minorID, void *serverarg, void *clientarg) |
1129 | 3.06k | { |
1130 | 3.06k | char line[SNMP_MAXBUF_SMALL]; |
1131 | 3.06k | u_char c_engineID[SNMP_MAXBUF_SMALL]; |
1132 | 3.06k | int engineIDLen; |
1133 | 3.06k | const char *type = (const char *) clientarg; |
1134 | | |
1135 | 3.06k | if (type == NULL) /* should never happen, since the arg is ours */ |
1136 | 0 | type = "unknown"; |
1137 | | |
1138 | 3.06k | sprintf(line, "engineBoots %ld", engineBoots); |
1139 | 3.06k | read_config_store(type, line); |
1140 | | |
1141 | 3.06k | engineIDLen = snmpv3_get_engineID(c_engineID, SNMP_MAXBUF_SMALL); |
1142 | | |
1143 | 3.06k | if (engineIDLen) { |
1144 | | /* |
1145 | | * store the engineID used for this run |
1146 | | */ |
1147 | 3.06k | sprintf(line, "oldEngineID "); |
1148 | 3.06k | read_config_save_octet_string(line + strlen(line), c_engineID, |
1149 | 3.06k | engineIDLen); |
1150 | 3.06k | read_config_store(type, line); |
1151 | 3.06k | } |
1152 | 3.06k | return SNMPERR_SUCCESS; |
1153 | 3.06k | } /* snmpv3_store() */ |
1154 | | |
1155 | | u_long |
1156 | | snmpv3_local_snmpEngineBoots(void) |
1157 | 3.06k | { |
1158 | 3.06k | return engineBoots; |
1159 | 3.06k | } |
1160 | | |
1161 | | |
1162 | | /*******************************************************************-o-****** |
1163 | | * snmpv3_get_engineID |
1164 | | * |
1165 | | * Parameters: |
1166 | | * *buf |
1167 | | * buflen |
1168 | | * |
1169 | | * Returns: |
1170 | | * Length of engineID On Success |
1171 | | * SNMPERR_GENERR Otherwise. |
1172 | | * |
1173 | | * |
1174 | | * Store engineID in buf; return the length. |
1175 | | * |
1176 | | */ |
1177 | | size_t |
1178 | | snmpv3_get_engineID(u_char * buf, size_t buflen) |
1179 | 12.2k | { |
1180 | | /* |
1181 | | * Sanity check. |
1182 | | */ |
1183 | 12.2k | if (!buf || (buflen < engineIDLength)) { |
1184 | 0 | return 0; |
1185 | 0 | } |
1186 | 12.2k | if (!engineID) { |
1187 | 3.07k | return 0; |
1188 | 3.07k | } |
1189 | | |
1190 | 9.19k | memcpy(buf, engineID, engineIDLength); |
1191 | 9.19k | return engineIDLength; |
1192 | | |
1193 | 12.2k | } /* end snmpv3_get_engineID() */ |
1194 | | |
1195 | | /*******************************************************************-o-****** |
1196 | | * snmpv3_clone_engineID |
1197 | | * |
1198 | | * Parameters: |
1199 | | * **dest |
1200 | | * *dest_len |
1201 | | * src |
1202 | | * srclen |
1203 | | * |
1204 | | * Returns: |
1205 | | * Length of engineID On Success |
1206 | | * 0 Otherwise. |
1207 | | * |
1208 | | * |
1209 | | * Clones engineID, creates memory |
1210 | | * |
1211 | | */ |
1212 | | int |
1213 | | snmpv3_clone_engineID(u_char ** dest, size_t * destlen, u_char * src, |
1214 | | size_t srclen) |
1215 | 0 | { |
1216 | 0 | if (!dest || !destlen) |
1217 | 0 | return 0; |
1218 | | |
1219 | 0 | SNMP_FREE(*dest); |
1220 | 0 | *destlen = 0; |
1221 | |
|
1222 | 0 | if (srclen && src) { |
1223 | 0 | *dest = netsnmp_memdup(src, srclen); |
1224 | 0 | if (*dest == NULL) |
1225 | 0 | return 0; |
1226 | 0 | *destlen = srclen; |
1227 | 0 | } |
1228 | 0 | return *destlen; |
1229 | 0 | } /* end snmpv3_clone_engineID() */ |
1230 | | |
1231 | | |
1232 | | /*******************************************************************-o-****** |
1233 | | * snmpv3_generate_engineID |
1234 | | * |
1235 | | * Parameters: |
1236 | | * *length |
1237 | | * |
1238 | | * Returns: |
1239 | | * Pointer to copy of engineID On Success. |
1240 | | * NULL If malloc() or snmpv3_get_engineID() |
1241 | | * fail. |
1242 | | * |
1243 | | * Generates a malloced copy of our engineID. |
1244 | | * |
1245 | | * 'length' is set to the length of engineID -OR- < 0 on failure. |
1246 | | */ |
1247 | | u_char * |
1248 | | snmpv3_generate_engineID(size_t * length) |
1249 | 6.14k | { |
1250 | 6.14k | u_char *newID; |
1251 | 6.14k | newID = (u_char *) malloc(engineIDLength); |
1252 | | |
1253 | 6.14k | if (newID) { |
1254 | 6.14k | *length = snmpv3_get_engineID(newID, engineIDLength); |
1255 | 6.14k | if (*length == 0) { |
1256 | 14 | SNMP_FREE(newID); |
1257 | 14 | newID = NULL; |
1258 | 14 | } |
1259 | 6.14k | } |
1260 | 6.14k | return newID; |
1261 | | |
1262 | 6.14k | } /* end snmpv3_generate_engineID() */ |
1263 | | |
1264 | | /** |
1265 | | * Return the value of snmpEngineTime. According to RFC 3414 snmpEngineTime |
1266 | | * is a 31-bit counter. engineBoots must be incremented every time that |
1267 | | * counter wraps around. |
1268 | | * |
1269 | | * @see See also <a href="http://tools.ietf.org/html/rfc3414">RFC 3414</a>. |
1270 | | * |
1271 | | * @note It is assumed that this function is called at least once every |
1272 | | * 2**31 seconds. |
1273 | | */ |
1274 | | u_long |
1275 | | snmpv3_local_snmpEngineTime(void) |
1276 | 6.12k | { |
1277 | | #ifdef NETSNMP_FEATURE_CHECKING |
1278 | | netsnmp_feature_require(calculate_sectime_diff) |
1279 | | #endif /* NETSNMP_FEATURE_CHECKING */ |
1280 | | |
1281 | 6.12k | static uint32_t last_engineTime; |
1282 | 6.12k | struct timeval now; |
1283 | 6.12k | uint32_t engineTime; |
1284 | | |
1285 | 6.12k | netsnmp_get_monotonic_clock(&now); |
1286 | 6.12k | engineTime = calculate_sectime_diff(&now, &snmpv3starttime) & 0x7fffffffL; |
1287 | 6.12k | if (engineTime < last_engineTime) |
1288 | 0 | engineBoots++; |
1289 | 6.12k | last_engineTime = engineTime; |
1290 | 6.12k | return engineTime; |
1291 | 6.12k | } |
1292 | | |
1293 | | |
1294 | | |
1295 | | /* |
1296 | | * Code only for Linux systems |
1297 | | */ |
1298 | | #if defined(IFHWADDRLEN) && defined(SIOCGIFHWADDR) |
1299 | | static int |
1300 | | getHwAddress(const char *networkDevice, /* e.g. "eth0", "eth1" */ |
1301 | | char *addressOut) |
1302 | 0 | { /* return address. Len=IFHWADDRLEN */ |
1303 | | /* |
1304 | | * getHwAddress(...) |
1305 | | * * |
1306 | | * * This function will return a Network Interfaces Card's Hardware |
1307 | | * * address (aka MAC address). |
1308 | | * * |
1309 | | * * Input Parameter(s): |
1310 | | * * networkDevice - a null terminated string with the name of a network |
1311 | | * * device. Examples: eth0, eth1, etc... |
1312 | | * * |
1313 | | * * Output Parameter(s): |
1314 | | * * addressOut - This is the binary value of the hardware address. |
1315 | | * * This value is NOT converted into a hexadecimal string. |
1316 | | * * The caller must pre-allocate for a return value of |
1317 | | * * length IFHWADDRLEN |
1318 | | * * |
1319 | | * * Return value: This function will return zero (0) for success. If |
1320 | | * * an error occurred the function will return -1. |
1321 | | * * |
1322 | | * * Caveats: This has only been tested on Ethernet networking cards. |
1323 | | */ |
1324 | 0 | int sock; /* our socket */ |
1325 | 0 | struct ifreq request; /* struct which will have HW address */ |
1326 | |
|
1327 | 0 | if ((NULL == networkDevice) || (NULL == addressOut)) { |
1328 | 0 | return -1; |
1329 | 0 | } |
1330 | | /* |
1331 | | * In order to find out the hardware (MAC) address of our system under |
1332 | | * * Linux we must do the following: |
1333 | | * * 1. Create a socket |
1334 | | * * 2. Do an ioctl(...) call with the SIOCGIFHWADDRLEN operation. |
1335 | | */ |
1336 | 0 | sock = socket(AF_INET, SOCK_DGRAM, 0); |
1337 | 0 | if (sock < 0) { |
1338 | 0 | return -1; |
1339 | 0 | } |
1340 | | /* |
1341 | | * erase the request block |
1342 | | */ |
1343 | 0 | memset(&request, 0, sizeof(request)); |
1344 | | /* |
1345 | | * copy the name of the net device we want to find the HW address for |
1346 | | */ |
1347 | 0 | strlcpy(request.ifr_name, networkDevice, IFNAMSIZ); |
1348 | | /* |
1349 | | * Get the HW address |
1350 | | */ |
1351 | 0 | if (ioctl(sock, SIOCGIFHWADDR, &request)) { |
1352 | 0 | close(sock); |
1353 | 0 | return -1; |
1354 | 0 | } |
1355 | 0 | close(sock); |
1356 | 0 | memcpy(addressOut, request.ifr_hwaddr.sa_data, IFHWADDRLEN); |
1357 | 0 | return 0; |
1358 | 0 | } |
1359 | | #endif |
1360 | | |
1361 | | #ifdef NETSNMP_ENABLE_TESTING_CODE |
1362 | | /** |
1363 | | * Set SNMPv3 engineBoots and start time. |
1364 | | * |
1365 | | * @note This function does not exist. Go away. It certainly should never be |
1366 | | * used, unless in a testing scenario, which is why it was created |
1367 | | */ |
1368 | | void |
1369 | | snmpv3_set_engineBootsAndTime(int boots, int ttime) |
1370 | | { |
1371 | | engineBoots = boots; |
1372 | | netsnmp_get_monotonic_clock(&snmpv3starttime); |
1373 | | snmpv3starttime.tv_sec -= ttime; |
1374 | | } |
1375 | | #endif |