Annotation of src/usr.bin/snmp/snmpc.c, Revision 1.20
1.20 ! martijn 1: /* $OpenBSD: snmpc.c,v 1.19 2020/01/17 10:03:39 martijn Exp $ */
1.1 martijn 2:
3: /*
4: * Copyright (c) 2019 Martijn van Duren <martijn@openbsd.org>
5: * Copyright (c) 2013 Reyk Floeter <reyk@openbsd.org>
6: *
7: * Permission to use, copy, modify, and distribute this software for any
8: * purpose with or without fee is hereby granted, provided that the above
9: * copyright notice and this permission notice appear in all copies.
10: *
11: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18: */
19:
20: #include <sys/limits.h>
21: #include <sys/types.h>
22: #include <sys/socket.h>
23: #include <sys/un.h>
24:
25: #include <arpa/inet.h>
1.10 martijn 26: #include <openssl/evp.h>
1.1 martijn 27:
28: #include <ber.h>
1.9 martijn 29: #include <ctype.h>
1.1 martijn 30: #include <err.h>
31: #include <errno.h>
32: #include <netdb.h>
33: #include <poll.h>
34: #include <stdio.h>
35: #include <stdlib.h>
36: #include <stdint.h>
37: #include <string.h>
38: #include <time.h>
39: #include <unistd.h>
1.19 martijn 40: #include <util.h>
1.1 martijn 41:
42: #include "smi.h"
43: #include "snmp.h"
1.9 martijn 44: #include "usm.h"
1.1 martijn 45:
1.11 martijn 46: #define GETOPT_COMMON "A:a:c:E:e:K:k:l:n:O:r:t:u:v:X:x:Z:"
1.1 martijn 47:
48: int snmpc_get(int, char *[]);
49: int snmpc_walk(int, char *[]);
1.13 martijn 50: int snmpc_set(int, char *[]);
1.1 martijn 51: int snmpc_trap(int, char *[]);
1.19 martijn 52: int snmpc_df(int, char *[]);
1.1 martijn 53: int snmpc_mibtree(int, char *[]);
1.8 martijn 54: struct snmp_agent *snmpc_connect(char *, char *);
1.1 martijn 55: int snmpc_parseagent(char *, char *);
56: int snmpc_print(struct ber_element *);
1.15 martijn 57: __dead void snmpc_printerror(enum snmp_error, struct ber_element *, int,
58: const char *);
1.9 martijn 59: char *snmpc_hex2bin(char *, size_t *);
1.13 martijn 60: struct ber_element *snmpc_varbindparse(int, char *[]);
1.1 martijn 61: void usage(void);
62:
63: struct snmp_app {
64: const char *name;
65: const int usecommonopt;
66: const char *optstring;
67: const char *usage;
1.2 deraadt 68: int (*exec)(int, char *[]);
1.1 martijn 69: };
70:
71: struct snmp_app snmp_apps[] = {
1.2 deraadt 72: { "get", 1, NULL, "agent oid ...", snmpc_get },
73: { "getnext", 1, NULL, "agent oid ...", snmpc_get },
1.20 ! martijn 74: { "walk", 1, "C:", "[-C cIipt] [-C E endoid] [-C s skipoid] agent [oid]", snmpc_walk },
1.2 deraadt 75: { "bulkget", 1, "C:", "[-C n<nonrep>r<maxrep>] agent oid ...", snmpc_get },
1.20 ! martijn 76: { "bulkwalk", 1, "C:", "[-C cipn<nonrep>r<maxrep>] [-C s skipoid] agent [oid]", snmpc_walk },
1.13 martijn 77: { "set", 1, NULL, "agent oid type value [oid type value] ...", snmpc_set },
1.2 deraadt 78: { "trap", 1, NULL, "agent uptime oid [oid type value] ...", snmpc_trap },
1.20 ! martijn 79: { "df", 1, "C:", "[-Ch] [-Cr<maxrep>] agent", snmpc_df },
1.2 deraadt 80: { "mibtree", 0, "O:", "[-O fnS]", snmpc_mibtree }
1.1 martijn 81: };
82: struct snmp_app *snmp_app = NULL;
83:
84: char *community = "public";
1.9 martijn 85: struct snmp_v3 *v3;
1.1 martijn 86: char *mib = "mib_2";
87: int retries = 5;
88: int timeout = 1;
1.9 martijn 89: enum snmp_version version = SNMP_V2C;
1.1 martijn 90: int print_equals = 1;
91: int print_varbind_only = 0;
92: int print_summary = 0;
93: int print_time = 0;
1.19 martijn 94: int print_human = 0;
1.1 martijn 95: int walk_check_increase = 1;
96: int walk_fallback_oid = 1;
97: int walk_include_oid = 0;
98: int smi_print_hint = 1;
99: int non_repeaters = 0;
100: int max_repetitions = 10;
101: struct ber_oid walk_end = {{0}, 0};
1.18 martijn 102: struct ber_oid *walk_skip = NULL;
103: size_t walk_skip_len = 0;
1.1 martijn 104: enum smi_oid_lookup oid_lookup = smi_oidl_short;
105: enum smi_output_string output_string = smi_os_default;
106:
107: int
108: main(int argc, char *argv[])
109: {
1.10 martijn 110: const EVP_MD *md = NULL;
1.11 martijn 111: const EVP_CIPHER *cipher = NULL;
1.9 martijn 112: struct snmp_sec *sec;
113: char *user = NULL;
1.10 martijn 114: enum usm_key_level authkeylevel;
115: char *authkey = NULL;
116: size_t authkeylen = 0;
1.11 martijn 117: enum usm_key_level privkeylevel;
118: char *privkey = NULL;
119: size_t privkeylen = 0;
1.9 martijn 120: int seclevel = SNMP_MSGFLAG_REPORT;
121: char *ctxname = NULL;
122: char *ctxengineid = NULL, *secengineid = NULL;
123: size_t ctxengineidlen, secengineidlen;
124: int zflag = 0;
125: long long boots, time;
1.1 martijn 126: char optstr[BUFSIZ];
127: const char *errstr;
128: char *strtolp;
129: int ch;
130: size_t i;
131:
132: if (pledge("stdio inet dns", NULL) == -1)
133: err(1, "pledge");
1.2 deraadt 134:
1.1 martijn 135: if (argc <= 1)
136: usage();
137:
1.14 bluhm 138: optstr[0] = '\0';
1.1 martijn 139: for (i = 0; i < sizeof(snmp_apps)/sizeof(*snmp_apps); i++) {
140: if (strcmp(snmp_apps[i].name, argv[1]) == 0) {
141: snmp_app = &snmp_apps[i];
142: if (snmp_app->optstring != NULL) {
143: if (strlcpy(optstr, snmp_app->optstring,
144: sizeof(optstr)) > sizeof(optstr))
145: errx(1, "strlcat");
146: }
147: break;
148: }
149: }
150: if (snmp_app == NULL)
151: usage();
152:
153: if (snmp_app->usecommonopt) {
154: if (strlcat(optstr, GETOPT_COMMON, sizeof(optstr)) >
155: sizeof(optstr))
156: errx(1, "strlcpy");
157: }
158:
159: argc--;
160: argv++;
161:
162: smi_init();
163:
164: while ((ch = getopt(argc, argv, optstr)) != -1) {
165: switch (ch) {
1.10 martijn 166: case 'A':
167: authkey = optarg;
168: authkeylen = strlen(authkey);
169: authkeylevel = USM_KEY_PASSWORD;
170: break;
171: case 'a':
172: if (strcasecmp(optarg, "MD5") == 0)
173: md = EVP_md5();
174: else if (strcasecmp(optarg, "SHA") == 0)
175: md = EVP_sha1();
176: else if (strcasecmp(optarg, "SHA-224") == 0)
177: md = EVP_sha224();
178: else if (strcasecmp(optarg, "SHA-256") == 0)
179: md = EVP_sha256();
180: else if (strcasecmp(optarg, "SHA-384") == 0)
181: md = EVP_sha384();
182: else if (strcasecmp(optarg, "SHA-512") == 0)
183: md = EVP_sha512();
184: else
185: errx(1, "Invalid authentication protocol "
186: "specified after -a flag: %s", optarg);
187: break;
1.1 martijn 188: case 'c':
189: community = optarg;
190: break;
1.9 martijn 191: case 'E':
192: ctxengineid = snmpc_hex2bin(optarg,
193: &ctxengineidlen);
194: if (ctxengineid == NULL) {
195: if (errno == EINVAL)
196: errx(1, "Bad engine ID value "
197: "after -3E flag.");
198: err(1, "-3E");
199: }
200: break;
201: case 'e':
202: secengineid = snmpc_hex2bin(optarg,
203: &secengineidlen);
204: if (secengineid == NULL) {
205: if (errno == EINVAL)
206: errx(1, "Bad engine ID value "
207: "after -3e flag.");
208: err(1, "-3e");
209: }
210: break;
1.11 martijn 211: case 'K':
212: privkey = snmpc_hex2bin(optarg, &privkeylen);
213: if (privkey == NULL) {
214: if (errno == EINVAL)
215: errx(1, "Bad key value after "
216: "-3K flag.");
217: errx(1, "-3K");
218: }
219: privkeylevel = USM_KEY_LOCALIZED;
220: break;
1.10 martijn 221: case 'k':
222: authkey = snmpc_hex2bin(optarg, &authkeylen);
223: if (authkey == NULL) {
224: if (errno == EINVAL)
225: errx(1, "Bad key value after -k flag.");
226: err(1, "-k");
227: }
228: authkeylevel = USM_KEY_LOCALIZED;
229: break;
230: case 'l':
231: if (strcasecmp(optarg, "noAuthNoPriv") == 0)
232: seclevel = SNMP_MSGFLAG_REPORT;
233: else if (strcasecmp(optarg, "authNoPriv") == 0)
234: seclevel = SNMP_MSGFLAG_AUTH |
235: SNMP_MSGFLAG_REPORT;
1.11 martijn 236: else if (strcasecmp(optarg, "authPriv") == 0)
237: seclevel = SNMP_MSGFLAG_AUTH |
238: SNMP_MSGFLAG_PRIV | SNMP_MSGFLAG_REPORT;
1.10 martijn 239: else
240: errx(1, "Invalid security level specified "
241: "after -l flag: %s", optarg);
242: break;
1.9 martijn 243: case 'n':
244: ctxname = optarg;
245: break;
1.1 martijn 246: case 'r':
247: if ((retries = strtonum(optarg, 0, INT_MAX,
248: &errstr)) == 0) {
249: if (errstr != NULL)
250: errx(1, "-r: %s argument", errstr);
251: }
252: break;
253: case 't':
254: if ((timeout = strtonum(optarg, 1, INT_MAX,
255: &errstr)) == 0) {
256: if (errstr != NULL)
257: errx(1, "-t: %s argument", errstr);
258: }
259: break;
1.9 martijn 260: case 'u':
261: user = optarg;
262: break;
1.1 martijn 263: case 'v':
264: if (strcmp(optarg, "1") == 0)
265: version = SNMP_V1;
266: else if (strcmp(optarg, "2c") == 0)
267: version = SNMP_V2C;
1.9 martijn 268: else if (strcmp(optarg, "3") == 0)
269: version = SNMP_V3;
1.1 martijn 270: else
271: errc(1, EINVAL, "-v");
272: break;
273: case 'C':
274: for (i = 0; i < strlen(optarg); i++) {
275: switch (optarg[i]) {
276: case 'c':
277: if (strcmp(snmp_app->name, "walk") &&
278: strcmp(snmp_app->name, "bulkwalk"))
279: usage();
280: walk_check_increase = 0;
281: break;
1.19 martijn 282: case 'h':
283: if (strcmp(snmp_app->name, "df"))
284: usage();
285: print_human = 1;
286: break;
1.1 martijn 287: case 'i':
288: if (strcmp(snmp_app->name, "walk") &&
289: strcmp(snmp_app->name, "bulkwalk"))
290: usage();
291: walk_include_oid = 1;
292: break;
293: case 'n':
294: if (strcmp(snmp_app->name, "bulkget") &&
295: strcmp(snmp_app->name, "bulkwalk"))
296: usage();
297: errno = 0;
298: non_repeaters = strtol(&optarg[i + 1],
299: &strtolp, 10);
300: if (non_repeaters < 0 ||
301: errno == ERANGE) {
302: if (non_repeaters < 0)
303: errx(1, "%s%s",
304: "-Cn: too small ",
305: "argument");
306: else
307: errx(1, "%s%s",
308: "-Cn: too large",
309: "argument");
310: } else if (&optarg[i + 1] == strtolp)
311: errx(1, "-Cn invalid argument");
312: i = strtolp - optarg - 1;
313: break;
314: case 'p':
315: if (strcmp(snmp_app->name, "walk") &&
316: strcmp(snmp_app->name, "bulkwalk"))
317: usage();
318: print_summary = 1;
319: break;
320: case 'r':
321: if (strcmp(snmp_app->name, "bulkget") &&
1.19 martijn 322: strcmp(snmp_app->name, "bulkwalk") &&
323: strcmp(snmp_app->name, "df"))
1.1 martijn 324: usage();
325: errno = 0;
326: max_repetitions = strtol(&optarg[i + 1],
327: &strtolp, 10);
328: if (max_repetitions < 0 ||
329: errno == ERANGE) {
330: if (max_repetitions < 0)
331: errx(1, "%s%s",
332: "-Cr: too small ",
333: "argument");
334: else
335: errx(1, "%s%s",
336: "-Cr: too large",
337: "argument");
338: } else if (&optarg[i + 1] == strtolp)
339: errx(1, "-Cr invalid argument");
340: i = strtolp - optarg - 1;
341: break;
1.18 martijn 342: case 's':
343: if (strcmp(snmp_app->name, "walk") &&
344: strcmp(snmp_app->name, "bulkwalk"))
345: usage();
346: if ((walk_skip = recallocarray(
347: walk_skip, walk_skip_len,
348: walk_skip_len + 1,
349: sizeof(*walk_skip))) == NULL)
350: errx(1, "malloc");
351: if (smi_string2oid(argv[optind],
352: &(walk_skip[walk_skip_len])) != 0)
353: errx(1, "%s: %s",
354: "Unknown Object Identifier",
355: argv[optind]);
356: walk_skip_len++;
357: optind++;
358: break;
1.1 martijn 359: case 't':
360: if (strcmp(snmp_app->name, "walk"))
361: usage();
362: print_time = 1;
363: break;
364: case 'E':
365: if (strcmp(snmp_app->name, "walk"))
366: usage();
367: if (smi_string2oid(argv[optind],
368: &walk_end) != 0)
369: errx(1, "%s: %s",
370: "Unknown Object Identifier",
371: argv[optind]);
372: optind++;
373: continue;
374: case 'I':
375: if (strcmp(snmp_app->name, "walk"))
376: usage();
377: walk_fallback_oid = 0;
378: break;
379: default:
380: usage();
381: }
382: if (optarg[i] == 'E')
383: break;
384: }
385: break;
386: case 'O':
387: for (i = 0; i < strlen(optarg); i++) {
388: if (strcmp(snmp_app->name, "mibtree") == 0 &&
389: optarg[i] != 'f' && optarg[i] != 'n' &&
390: optarg[i] != 'S')
391: usage();
392: switch (optarg[i]) {
393: case 'a':
394: output_string = smi_os_ascii;
395: break;
396: case 'f':
397: oid_lookup = smi_oidl_full;
398: break;
399: case 'n':
400: oid_lookup = smi_oidl_numeric;
401: break;
402: case 'q':
403: print_equals = 0;
404: smi_print_hint = 0;
405: break;
406: case 'v':
1.2 deraadt 407: print_varbind_only = 1;
1.1 martijn 408: break;
409: case 'x':
410: output_string = smi_os_hex;
411: break;
412: case 'S':
413: oid_lookup = smi_oidl_short;
414: break;
415: case 'Q':
416: smi_print_hint = 0;
417: break;
418: default:
419: usage();
420: }
421: }
422: break;
1.11 martijn 423: case 'X':
424: privkey = optarg;
425: privkeylen = strlen(privkey);
426: privkeylevel = USM_KEY_PASSWORD;
427: break;
428: case 'x':
429: if (strcasecmp(optarg, "DES") == 0)
430: cipher = EVP_des_cbc();
431: else if (strcasecmp(optarg, "AES") == 0)
432: cipher = EVP_aes_128_cfb128();
433: else
434: errx(1, "Invalid privacy protocol "
435: "specified after -3x flag: %s",
436: optarg);
437: break;
1.9 martijn 438: case 'Z':
439: boots = strtoll(optarg, &strtolp, 10);
440: if (boots < 0 || strtolp == optarg || strtolp[0] != ',')
441: usage();
442: strtolp++;
443: while (strtolp[0] == ' ' && strtolp[0] == '\t')
444: strtolp++;
445: time = strtoll(strtolp, &strtolp, 10);
446: if (boots < 0 || strtolp == optarg)
447: usage();
448: zflag = 1;
449: break;
1.1 martijn 450: default:
451: usage();
452: }
453: }
454: argc -= optind;
455: argv += optind;
456:
1.9 martijn 457: if (version == SNMP_V3) {
458: /* Setup USM */
459: if (user == NULL || user[0] == '\0')
460: errx(1, "No securityName specified");
461: if ((sec = usm_init(user, strlen(user))) == NULL)
462: err(1, "usm_init");
1.10 martijn 463: if (seclevel & SNMP_MSGFLAG_AUTH) {
464: if (md == NULL)
465: md = EVP_md5();
466: if (authkey == NULL)
467: errx(1, "No authKey or authPassword specified");
468: if (usm_setauth(sec, md, authkey, authkeylen,
469: authkeylevel) == -1)
470: err(1, "Can't set authkey");
471: }
1.11 martijn 472: if (seclevel & SNMP_MSGFLAG_PRIV) {
473: if (cipher == NULL)
474: cipher = EVP_des_cbc();
475: if (privkey == NULL)
476: errx(1, "No privKey or privPassword specified");
477: if (usm_setpriv(sec, cipher, privkey, privkeylen,
478: privkeylevel) == -1)
479: err(1, "Can't set authkey");
480: }
1.9 martijn 481: if (secengineid != NULL) {
482: if (usm_setengineid(sec, secengineid,
483: secengineidlen) == -1)
484: err(1, "Can't set secengineid");
485: }
486: if (zflag)
487: if (usm_setbootstime(sec, boots, time) == -1)
488: err(1, "Can't set boots/time");
489: v3 = snmp_v3_init(seclevel, ctxname, ctxname == NULL ? 0 :
490: strlen(ctxname), sec);
491: if (v3 == NULL)
492: err(1, "snmp_v3_init");
493: if (ctxengineid != NULL) {
494: if (snmp_v3_setengineid(v3, ctxengineid,
495: ctxengineidlen) == -1)
496: err(1, "Can't set ctxengineid");
497: }
498: }
499:
500:
1.1 martijn 501: return snmp_app->exec(argc, argv);
502: }
503:
504: int
505: snmpc_get(int argc, char *argv[])
506: {
507: struct ber_oid *oid;
508: struct ber_element *pdu, *varbind;
509: struct snmp_agent *agent;
510: int errorstatus, errorindex;
511: int i;
1.9 martijn 512: int class;
513: unsigned type;
1.15 martijn 514: char *hint = NULL;
1.1 martijn 515:
516: if (argc < 2)
517: usage();
518:
1.8 martijn 519: if ((agent = snmpc_connect(argv[0], "161")) == NULL)
1.1 martijn 520: err(1, "%s", snmp_app->name);
521: agent->timeout = timeout;
522: agent->retries = retries;
523:
524: if (pledge("stdio", NULL) == -1)
525: err(1, "pledge");
526: argc--;
527: argv++;
528:
529: oid = reallocarray(NULL, argc, sizeof(*oid));
1.3 deraadt 530: if (oid == NULL)
531: err(1, "malloc");
1.1 martijn 532: for (i = 0; i < argc; i++) {
533: if (smi_string2oid(argv[i], &oid[i]) == -1)
1.12 semarie 534: errx(1, "%s: Unknown object identifier", argv[i]);
1.1 martijn 535: }
536: if (strcmp(snmp_app->name, "getnext") == 0) {
537: if ((pdu = snmp_getnext(agent, oid, argc)) == NULL)
538: err(1, "getnext");
539: } else if (strcmp(snmp_app->name, "bulkget") == 0) {
540: if (version < SNMP_V2C)
541: errx(1, "Cannot send V2 PDU on V1 session");
542: if (non_repeaters > argc)
543: errx(1, "need more objects than -Cn<num>");
544: if ((pdu = snmp_getbulk(agent, oid, argc, non_repeaters,
545: max_repetitions)) == NULL)
546: err(1, "bulkget");
547: } else {
548: if ((pdu = snmp_get(agent, oid, argc)) == NULL)
549: err(1, "get");
550: }
551:
1.16 tb 552: (void) ober_scanf_elements(pdu, "t{Sdd{e", &class, &type, &errorstatus,
1.9 martijn 553: &errorindex, &varbind);
1.15 martijn 554: if (errorstatus != 0) {
555: if (errorindex >= 1 && errorindex <= argc)
556: hint = argv[errorindex - 1];
557: snmpc_printerror((enum snmp_error) errorstatus, varbind,
558: errorindex, hint);
559: }
1.1 martijn 560:
1.9 martijn 561: if (class == BER_CLASS_CONTEXT && type == SNMP_C_REPORT)
562: printf("Received report:\n");
1.1 martijn 563: for (; varbind != NULL; varbind = varbind->be_next) {
564: if (!snmpc_print(varbind))
565: err(1, "Can't print response");
566: }
1.19 martijn 567: sleep(10);
1.16 tb 568: ober_free_elements(pdu);
1.1 martijn 569: snmp_free_agent(agent);
570: return 0;
571: }
572:
573: int
574: snmpc_walk(int argc, char *argv[])
575: {
576: struct ber_oid oid, loid, noid;
577: struct ber_element *pdu, *varbind, *value;
578: struct timespec start, finish;
579: struct snmp_agent *agent;
580: const char *oids;
1.18 martijn 581: int n = 0, prev_cmp, skip_cmp;
1.1 martijn 582: int errorstatus, errorindex;
1.9 martijn 583: int class;
1.18 martijn 584: size_t i;
1.9 martijn 585: unsigned type;
1.1 martijn 586:
587: if (strcmp(snmp_app->name, "bulkwalk") == 0 && version < SNMP_V2C)
588: errx(1, "Cannot send V2 PDU on V1 session");
589: if (argc < 1 || argc > 2)
590: usage();
591: oids = argc == 1 ? mib : argv[1];
592:
1.8 martijn 593: if ((agent = snmpc_connect(argv[0], "161"))== NULL)
1.1 martijn 594: err(1, "%s", snmp_app->name);
595: agent->timeout = timeout;
596: agent->retries = retries;
597: if (pledge("stdio", NULL) == -1)
598: err(1, "pledge");
599:
600: if (smi_string2oid(oids, &oid) == -1)
601: errx(1, "%s: Unknown object identifier", oids);
602: bcopy(&oid, &noid, sizeof(noid));
603: if (print_time)
604: clock_gettime(CLOCK_MONOTONIC, &start);
605:
606: if (walk_include_oid) {
607: if ((pdu = snmp_get(agent, &oid, 1)) == NULL)
608: err(1, "%s", snmp_app->name);
609:
1.16 tb 610: (void) ober_scanf_elements(pdu, "t{Sdd{e", &class, &type,
1.9 martijn 611: &errorstatus, &errorindex, &varbind);
1.1 martijn 612: if (errorstatus != 0)
1.15 martijn 613: snmpc_printerror((enum snmp_error) errorstatus, varbind,
614: errorindex, oids);
1.1 martijn 615:
1.9 martijn 616: if (class == BER_CLASS_CONTEXT && type == SNMP_C_REPORT)
617: printf("Received report:\n");
1.1 martijn 618: if (!snmpc_print(varbind))
619: err(1, "Can't print response");
1.16 tb 620: ober_free_element(pdu);
1.9 martijn 621: if (class == BER_CLASS_CONTEXT && type == SNMP_C_REPORT)
622: return 1;
1.1 martijn 623: n++;
624: }
625: while (1) {
1.18 martijn 626: for (i = 0; i < walk_skip_len; i++) {
627: skip_cmp = ober_oid_cmp(&(walk_skip[i]), &noid);
628: if (skip_cmp == 0 || skip_cmp == 2) {
629: bcopy(&(walk_skip[i]), &noid, sizeof(noid));
630: noid.bo_id[noid.bo_n -1]++;
631: break;
632: }
633: }
1.1 martijn 634: bcopy(&noid, &loid, sizeof(loid));
635: if (strcmp(snmp_app->name, "bulkwalk") == 0) {
636: if ((pdu = snmp_getbulk(agent, &noid, 1,
637: non_repeaters, max_repetitions)) == NULL)
638: err(1, "bulkwalk");
639: } else {
640: if ((pdu = snmp_getnext(agent, &noid, 1)) == NULL)
641: err(1, "walk");
642: }
643:
1.16 tb 644: (void) ober_scanf_elements(pdu, "t{Sdd{e", &class, &type,
1.9 martijn 645: &errorstatus, &errorindex, &varbind);
1.1 martijn 646: if (errorstatus != 0) {
1.15 martijn 647: snmpc_printerror((enum snmp_error) errorstatus, varbind,
648: errorindex, NULL);
1.1 martijn 649: }
650:
1.9 martijn 651: if (class == BER_CLASS_CONTEXT && type == SNMP_C_REPORT)
652: printf("Received report:\n");
1.2 deraadt 653: for (; varbind != NULL; varbind = varbind->be_next) {
1.16 tb 654: (void) ober_scanf_elements(varbind, "{oe}", &noid,
1.1 martijn 655: &value);
656: if (value->be_class == BER_CLASS_CONTEXT &&
657: value->be_type == BER_TYPE_EOC)
658: break;
1.18 martijn 659: for (i = 0; i < walk_skip_len; i++) {
660: skip_cmp = ober_oid_cmp(&(walk_skip[i]), &noid);
661: if (skip_cmp == 0 || skip_cmp == 2)
662: break;
663: }
664: if (i < walk_skip_len)
665: continue;
1.16 tb 666: prev_cmp = ober_oid_cmp(&loid, &noid);
1.1 martijn 667: if (walk_check_increase && prev_cmp == -1)
668: errx(1, "OID not increasing");
1.16 tb 669: if (prev_cmp == 0 || ober_oid_cmp(&oid, &noid) != 2)
1.1 martijn 670: break;
671: if (walk_end.bo_n != 0 &&
1.16 tb 672: ober_oid_cmp(&walk_end, &noid) != -1)
1.1 martijn 673: break;
674:
675: if (!snmpc_print(varbind))
676: err(1, "Can't print response");
677: n++;
678: }
1.16 tb 679: ober_free_elements(pdu);
1.9 martijn 680: if (class == BER_CLASS_CONTEXT && type == SNMP_C_REPORT)
681: return 1;
1.1 martijn 682: if (varbind != NULL)
683: break;
684: }
685: if (walk_fallback_oid && n == 0) {
686: if ((pdu = snmp_get(agent, &oid, 1)) == NULL)
687: err(1, "%s", snmp_app->name);
688:
1.16 tb 689: (void) ober_scanf_elements(pdu, "t{Sdd{e", &class, &type,
1.9 martijn 690: &errorstatus, &errorindex, &varbind);
1.1 martijn 691: if (errorstatus != 0)
1.15 martijn 692: snmpc_printerror((enum snmp_error) errorstatus, varbind,
693: errorindex, oids);
1.1 martijn 694:
1.9 martijn 695: if (class == BER_CLASS_CONTEXT && type == SNMP_C_REPORT)
696: printf("Received report:\n");
1.1 martijn 697: if (!snmpc_print(varbind))
698: err(1, "Can't print response");
1.16 tb 699: ober_free_element(pdu);
1.9 martijn 700: if (class == BER_CLASS_CONTEXT && type == SNMP_C_REPORT)
701: return 1;
1.1 martijn 702: n++;
703: }
704: if (print_time)
705: clock_gettime(CLOCK_MONOTONIC, &finish);
706: if (print_summary)
707: printf("Variables found: %d\n", n);
708: if (print_time) {
709: if ((finish.tv_nsec -= start.tv_nsec) < 0) {
710: finish.tv_sec -= 1;
711: finish.tv_nsec += 1000000000;
712: }
713: finish.tv_sec -= start.tv_sec;
714: fprintf(stderr, "Total traversal time: %lld.%09ld seconds\n",
715: finish.tv_sec, finish.tv_nsec);
716: }
717: snmp_free_agent(agent);
718: return 0;
719: }
720:
721: int
1.13 martijn 722: snmpc_set(int argc, char *argv[])
723: {
724: struct snmp_agent *agent;
725: struct ber_element *pdu, *varbind;
726: int errorstatus, errorindex;
727: int class;
728: unsigned type;
1.15 martijn 729: char *hint = NULL;
1.13 martijn 730:
731: if (argc < 4)
732: usage();
733: if ((agent = snmpc_connect(argv[0], "161")) == NULL)
734: err(1, "%s", snmp_app->name);
735: argc--;
736: argv++;
737:
738: if (pledge("stdio", NULL) == -1)
739: err(1, "pledge");
740:
1.15 martijn 741: if ((pdu = snmp_set(agent, snmpc_varbindparse(argc, argv))) == NULL)
742: err(1, "set");
1.13 martijn 743:
1.16 tb 744: (void) ober_scanf_elements(pdu, "t{Sdd{e", &class, &type, &errorstatus,
1.13 martijn 745: &errorindex, &varbind);
1.15 martijn 746: if (errorstatus != 0) {
747: if (errorindex >= 1 && errorindex <= argc / 3)
748: hint = argv[(errorindex - 1) * 3];
749: snmpc_printerror((enum snmp_error) errorstatus, varbind,
750: errorindex, hint);
751: }
1.13 martijn 752:
753: if (class == BER_CLASS_CONTEXT && type == SNMP_C_REPORT)
754: printf("Received report:\n");
755: for (; varbind != NULL; varbind = varbind->be_next) {
756: if (!snmpc_print(varbind))
757: err(1, "Can't print response");
758: }
1.16 tb 759: ober_free_elements(pdu);
1.13 martijn 760: snmp_free_agent(agent);
761: return 0;
762: }
763:
764: int
1.1 martijn 765: snmpc_trap(int argc, char *argv[])
766: {
767: struct snmp_agent *agent;
768: struct timespec ts;
1.13 martijn 769: struct ber_oid trapoid;
1.1 martijn 770: const char *errstr = NULL;
771: long long lval;
772:
773: if (version == SNMP_V1)
774: errx(1, "trap is not supported for snmp v1");
775:
1.8 martijn 776: if ((agent = snmpc_connect(argv[0], "162")) == NULL)
1.1 martijn 777: err(1, "%s", snmp_app->name);
778:
779: if (pledge("stdio", NULL) == -1)
780: err(1, "pledge");
781:
782: if (argv[1][0] == '\0') {
783: if (clock_gettime(CLOCK_UPTIME, &ts) == -1)
784: err(1, "clock_gettime");
785: } else {
786: lval = strtonum(argv[1], 0, LLONG_MAX, &errstr);
787: if (errstr != NULL)
788: errx(1, "Bad value notation (%s)", argv[1]);
789: ts.tv_sec = lval / 100;
790: ts.tv_nsec = (lval % 100) * 10000000;
791: }
792: if (smi_string2oid(argv[2], &trapoid) == -1)
793: errx(1, "Invalid oid: %s\n", argv[2]);
794:
795: argc -= 3;
796: argv += 3;
797:
1.13 martijn 798: snmp_trap(agent, &ts, &trapoid, snmpc_varbindparse(argc, argv));
1.19 martijn 799:
800: return 0;
801: }
802:
803: #define INCR_NEXTTAB(x) ((x + 8) & ~7)
804: #define NEXTTAB(x) (8 - (x & 7))
805: int
806: snmpc_df(int argc, char *argv[])
807: {
808: struct snmpc_df {
809: uint32_t index;
810: /* DisplayString is 255a DISPLAY-HINT */
811: char descr[256];
812: /* Theoretical maximum for 2 32 bit values multiplied */
813: char size[21];
814: char used[21];
815: char avail[21];
816: char proc[5];
817: } *df = NULL;
818: struct ber_oid descroid = {{ 1, 3, 6, 1, 2, 1, 25, 2, 3, 1, 3 }, 11};
819: struct ber_oid unitsoid = {{ 1, 3, 6, 1, 2, 1, 25, 2, 3, 1, 4 }, 11};
820: struct ber_oid sizeoid = {{ 1, 3, 6, 1, 2, 1, 25, 2, 3, 1, 5 }, 11};
821: struct ber_oid usedoid = {{ 1, 3, 6, 1, 2, 1, 25, 2, 3, 1, 6 }, 11};
822: struct ber_oid oid, *reqoid;
823: struct ber_element *pdu, *varbind;
824: struct snmp_agent *agent;
825: int errorstatus, errorindex;
826: int class;
827: size_t i, j, rows = 0;
828: unsigned type;
829: char *string;
830: int descrlen = 0, sizelen = 0, usedlen = 0, availlen = 0, proclen = 0;
831: int len;
832: long long units, size, used;
833: int fmtret;
834:
835: if (argc != 1)
836: usage();
837:
838: if ((agent = snmpc_connect(argv[0], "161")) == NULL)
839: err(1, "%s", snmp_app->name);
840: agent->timeout = timeout;
841: agent->retries = retries;
842:
843: if (pledge("stdio", NULL) == -1)
844: err(1, "pledge");
845:
846: descrlen = sizeof("Description") - 1;
847: sizelen = sizeof("Size") - 1;
848: usedlen = sizeof("Used") - 1;
849: availlen = sizeof("Available") - 1;
850: proclen = sizeof("Used%") - 1;
851:
852: bcopy(&descroid, &oid, sizeof(descroid));
853:
854: i = 0;
855: while(1) {
856: if (version < SNMP_V2C) {
857: if ((pdu = snmp_getnext(agent, &oid, 1)) == NULL)
858: err(1, "df");
859: } else {
860: if ((pdu = snmp_getbulk(agent, &oid, 1, 0,
861: max_repetitions)) == NULL)
862: err(1, "df");
863: }
864:
865: (void) ober_scanf_elements(pdu, "t{Sdd{e", &class, &type,
866: &errorstatus, &errorindex, &varbind);
867: if (errorstatus != 0)
868: snmpc_printerror((enum snmp_error) errorstatus, varbind,
869: errorindex, NULL);
870:
871: if (class == BER_CLASS_CONTEXT && type == SNMP_C_REPORT) {
872: printf("Received report:\n");
873: for (; varbind != NULL; varbind = varbind->be_next) {
874: if (!snmpc_print(varbind))
875: err(1, "Can't print response");
876: }
877: return 1;
878: }
879: for (; varbind != NULL; varbind = varbind->be_next) {
880: (void) ober_scanf_elements(varbind, "{oS", &oid);
881: if (ober_oid_cmp(&descroid, &oid) != 2)
882: break;
883: rows++;
884: }
885: if ((df = reallocarray(df, rows, sizeof(*df))) == NULL)
886: err(1, "malloc");
887: (void) ober_scanf_elements(pdu, "{SSS{e", &varbind);
888: for (; i < rows; varbind = varbind->be_next, i++) {
889: if (ober_scanf_elements(varbind, "{os", &oid,
890: &string) == -1) {
891: i--;
892: rows--;
893: continue;
894: }
895: df[i].index = oid.bo_id[oid.bo_n - 1];
896: len = strlcpy(df[i].descr, string,
897: sizeof(df[i].descr));
898: if (len > (int) sizeof(df[i].descr))
899: len = (int) sizeof(df[i].descr) - 1;
900: if (len > descrlen)
901: descrlen = len;
902: }
903: ober_free_elements(pdu);
904: if (varbind != NULL)
905: break;
906: }
907:
908: if (max_repetitions < 3)
909: max_repetitions = 3;
910: if ((reqoid = reallocarray(NULL, max_repetitions, sizeof(*reqoid))) == NULL)
911: err(1, "malloc");
912: for (i = 0; i < rows;) {
913: for (j = 0; i + j < rows && j < (size_t)max_repetitions / 3;
914: j++) {
915: bcopy(&unitsoid, &(reqoid[(j * 3) + 0]),
916: sizeof(unitsoid));
917: reqoid[(j * 3) + 0].bo_id[
918: reqoid[(j * 3) + 0].bo_n++] = df[i + j].index;
919: bcopy(&sizeoid, &(reqoid[(j * 3) + 1]),
920: sizeof(sizeoid));
921: reqoid[(j * 3) + 1].bo_id[
922: reqoid[(j * 3) + 1].bo_n++] = df[i + j].index;
923: bcopy(&usedoid, &(reqoid[(j * 3) + 2]),
924: sizeof(usedoid));
925: reqoid[(j * 3) + 2].bo_id[
926: reqoid[(j * 3) + 2].bo_n++] = df[i + j].index;
927: }
928: if ((pdu = snmp_get(agent, reqoid, j * 3)) == NULL)
929: err(1, "df");
930: (void) ober_scanf_elements(pdu, "t{Sdd{e", &class, &type,
931: &errorstatus, &errorindex, &varbind);
932: if (errorstatus != 0)
933: snmpc_printerror((enum snmp_error) errorstatus, varbind,
934: errorindex, NULL);
935: if (class == BER_CLASS_CONTEXT && type == SNMP_C_REPORT) {
936: printf("Received report:\n");
937: for (; varbind != NULL; varbind = varbind->be_next) {
938: if (!snmpc_print(varbind))
939: err(1, "Can't print response");
940: }
941: }
942: for (j = 0; varbind != NULL; i++) {
943: if (ober_scanf_elements(varbind, "{oi}{oi}{oi}",
944: &(reqoid[0]), &units, &(reqoid[1]), &size,
945: &(reqoid[2]), &used, &varbind) == -1) {
946: break;
947: }
948: varbind = varbind->be_next->be_next->be_next;
949:
950: unitsoid.bo_id[unitsoid.bo_n++] = df[i].index;
951: if (ober_oid_cmp(&unitsoid, &(reqoid[0])) != 0) {
952: warnx("df: received invalid object");
953: break;
954: }
955: unitsoid.bo_n--;
956: sizeoid.bo_id[sizeoid.bo_n++] = df[i].index;
957: if (ober_oid_cmp(&sizeoid, &(reqoid[1])) != 0) {
958: warnx("df: received invalid object");
959: break;
960: }
961: sizeoid.bo_n--;
962: usedoid.bo_id[usedoid.bo_n++] = df[i].index;
963: if (ober_oid_cmp(&usedoid, &(reqoid[2])) != 0) {
964: warnx("df: received invalid object");
965: break;
966: }
967: usedoid.bo_n--;
968: if (print_human)
969: fmtret = fmt_scaled((units * size), df[i].size);
970: if (!print_human || fmtret == -1)
971: snprintf(df[i].size, sizeof(df[i].size), "%lld",
972: (units * size) / 1024);
973: len = (int) strlen(df[i].size);
974: if (len > sizelen)
975: sizelen = len;
976: if (print_human)
977: fmtret = fmt_scaled(units * used, df[i].used);
978: if (!print_human || fmtret == -1)
979: snprintf(df[i].used, sizeof(df[i].used), "%lld",
980: (units * used) / 1024);
981: len = (int) strlen(df[i].used);
982: if (len > usedlen)
983: usedlen = len;
984: if (print_human)
985: fmtret = fmt_scaled(units * (size - used),
986: df[i].avail);
987: if (!print_human || fmtret == -1)
988: snprintf(df[i].avail, sizeof(df[i].avail),
989: "%lld", (units * (size - used)) / 1024);
990: len = (int) strlen(df[i].avail);
991: if (len > usedlen)
992: availlen = len;
993: if (size == 0)
994: strlcpy(df[i].proc, "0%", sizeof(df[i].proc));
995: else {
996: snprintf(df[i].proc, sizeof(df[i].proc),
997: "%lld%%", (used * 100) / size);
998: }
999: len = (int) strlen(df[i].proc);
1000: if (len > proclen)
1001: proclen = len;
1002: j++;
1003: }
1004: if (j == 0) {
1005: warnx("Failed to retrieve information for %s",
1006: df[i].descr);
1007: memmove(df + i, df + i + 1,
1008: (rows - i - 1) * sizeof(*df));
1009: rows--;
1010: i--;
1011: }
1012: }
1013:
1014: printf("%-*s%*s%*s%*s%*s\n",
1015: descrlen, "Description",
1016: NEXTTAB(descrlen) + sizelen, "Size",
1017: NEXTTAB(sizelen) + usedlen, "Used",
1018: NEXTTAB(usedlen) + availlen, "Available",
1019: NEXTTAB(availlen) + proclen, "Used%");
1020: for (i = 0; i < rows; i++) {
1021: printf("%-*s%*s%*s%*s%*s\n",
1022: descrlen, df[i].descr,
1023: NEXTTAB(descrlen) + sizelen, df[i].size,
1024: NEXTTAB(sizelen) + usedlen, df[i].used,
1025: NEXTTAB(usedlen) + availlen, df[i].avail,
1026: NEXTTAB(availlen) + proclen, df[i].proc);
1027: }
1.1 martijn 1028:
1029: return 0;
1030: }
1031:
1032: int
1033: snmpc_mibtree(int argc, char *argv[])
1034: {
1035: struct oid *oid;
1036: char buf[BUFSIZ];
1037:
1038: for (oid = NULL; (oid = smi_foreach(oid, 0)) != NULL;) {
1039: smi_oid2string(&oid->o_id, buf, sizeof(buf), oid_lookup);
1040: printf("%s\n", buf);
1041: }
1042: return 0;
1.8 martijn 1043: }
1044:
1045: struct snmp_agent *
1046: snmpc_connect(char *host, char *port)
1047: {
1048: switch (version) {
1049: case SNMP_V1:
1050: case SNMP_V2C:
1051: return snmp_connect_v12(snmpc_parseagent(host, port), version,
1052: community);
1.9 martijn 1053: case SNMP_V3:
1054: return snmp_connect_v3(snmpc_parseagent(host, port), v3);
1.8 martijn 1055: }
1056: return NULL;
1.1 martijn 1057: }
1058:
1059: int
1060: snmpc_print(struct ber_element *elm)
1061: {
1062: struct ber_oid oid;
1063: char oids[SNMP_MAX_OID_STRLEN];
1064: char *value;
1065:
1066: elm = elm->be_sub;
1.16 tb 1067: if (ober_get_oid(elm, &oid) != 0) {
1.1 martijn 1068: errno = EINVAL;
1069: return 0;
1070: }
1071:
1072: elm = elm->be_next;
1073: value = smi_print_element(elm, smi_print_hint, output_string, oid_lookup);
1074: if (value == NULL)
1075: return 0;
1076:
1077: if (print_varbind_only)
1078: printf("%s\n", value);
1079: else if (print_equals) {
1080: smi_oid2string(&oid, oids, sizeof(oids), oid_lookup);
1081: printf("%s = %s\n", oids, value);
1082: } else {
1083: smi_oid2string(&oid, oids, sizeof(oids), oid_lookup);
1084: printf("%s %s\n", oids, value);
1085: }
1086: free(value);
1.2 deraadt 1087:
1.1 martijn 1088: return 1;
1089: }
1090:
1091: __dead void
1.15 martijn 1092: snmpc_printerror(enum snmp_error error, struct ber_element *varbind,
1093: int index, const char *hint)
1.1 martijn 1094: {
1.15 martijn 1095: struct ber_oid hoid, vboid;
1096: char oids[SNMP_MAX_OID_STRLEN];
1097: const char *oid = NULL;
1098: int i;
1099:
1100: if (index >= 1) {
1101: /* Only print if the index is in the reply */
1102: for (i = 1; varbind != NULL && i <= index;
1103: varbind = varbind->be_next)
1104: i++;
1105: if (varbind != NULL &&
1.16 tb 1106: ober_get_oid(varbind->be_sub, &vboid) == 0) {
1.15 martijn 1107: /* If user and reply conform print user input */
1108: if (hint != NULL &&
1109: smi_string2oid(hint, &hoid) == 0 &&
1.16 tb 1110: ober_oid_cmp(&hoid, &vboid) == 0)
1.15 martijn 1111: oid = hint;
1112: else
1113: oid = smi_oid2string(&vboid, oids,
1114: sizeof(oids), oid_lookup);
1115: }
1116: }
1117: if (oid == NULL)
1118: oid = "?";
1119:
1.2 deraadt 1120: switch (error) {
1.1 martijn 1121: case SNMP_ERROR_NONE:
1122: errx(1, "No error, how did I get here?");
1123: case SNMP_ERROR_TOOBIG:
1124: errx(1, "Can't parse oid %s: Response too big", oid);
1125: case SNMP_ERROR_NOSUCHNAME:
1126: errx(1, "Can't parse oid %s: No such object", oid);
1127: case SNMP_ERROR_BADVALUE:
1128: errx(1, "Can't parse oid %s: Bad value", oid);
1129: case SNMP_ERROR_READONLY:
1130: errx(1, "Can't parse oid %s: Read only", oid);
1131: case SNMP_ERROR_GENERR:
1132: errx(1, "Can't parse oid %s: Generic error", oid);
1133: case SNMP_ERROR_NOACCESS:
1134: errx(1, "Can't parse oid %s: Access denied", oid);
1135: case SNMP_ERROR_WRONGTYPE:
1136: errx(1, "Can't parse oid %s: Wrong type", oid);
1137: case SNMP_ERROR_WRONGLENGTH:
1138: errx(1, "Can't parse oid %s: Wrong length", oid);
1139: case SNMP_ERROR_WRONGENC:
1140: errx(1, "Can't parse oid %s: Wrong encoding", oid);
1141: case SNMP_ERROR_WRONGVALUE:
1142: errx(1, "Can't parse oid %s: Wrong value", oid);
1143: case SNMP_ERROR_NOCREATION:
1144: errx(1, "Can't parse oid %s: Can't be created", oid);
1145: case SNMP_ERROR_INCONVALUE:
1146: errx(1, "Can't parse oid %s: Inconsistent value", oid);
1147: case SNMP_ERROR_RESUNAVAIL:
1148: errx(1, "Can't parse oid %s: Resource unavailable", oid);
1149: case SNMP_ERROR_COMMITFAILED:
1150: errx(1, "Can't parse oid %s: Commit failed", oid);
1151: case SNMP_ERROR_UNDOFAILED:
1152: errx(1, "Can't parse oid %s: Undo faild", oid);
1153: case SNMP_ERROR_AUTHERROR:
1154: errx(1, "Can't parse oid %s: Authorization error", oid);
1155: case SNMP_ERROR_NOTWRITABLE:
1156: errx(1, "Can't parse oid %s: Not writable", oid);
1157: case SNMP_ERROR_INCONNAME:
1158: errx(1, "Can't parse oid %s: Inconsistent name", oid);
1159: }
1160: errx(1, "Can't parse oid %s: Unknown error (%d)", oid, error);
1161: }
1162:
1163: int
1164: snmpc_parseagent(char *agent, char *defaultport)
1165: {
1166: struct addrinfo hints, *ai, *ai0 = NULL;
1167: struct sockaddr_un saddr;
1168: char *agentdup, *specifier, *hostname, *port = NULL;
1169: int error;
1170: int s;
1171:
1172: if ((agentdup = specifier = strdup(agent)) == NULL)
1173: err(1, NULL);
1174:
1175: bzero(&hints, sizeof(hints));
1176: if ((hostname = strchr(specifier, ':')) != NULL) {
1177: *hostname++ = '\0';
1178: if (strcasecmp(specifier, "udp") == 0) {
1179: hints.ai_family = AF_INET;
1180: hints.ai_socktype = SOCK_DGRAM;
1181: } else if (strcasecmp(specifier, "tcp") == 0) {
1182: hints.ai_family = AF_INET;
1183: hints.ai_socktype = SOCK_STREAM;
1184: } else if (strcasecmp(specifier, "udp6") == 0 ||
1185: strcasecmp(specifier, "udpv6") == 0 ||
1186: strcasecmp(specifier, "udpipv6") == 0) {
1187: hints.ai_family = AF_INET6;
1188: hints.ai_socktype = SOCK_DGRAM;
1189: } else if (strcasecmp(specifier, "tcp6") == 0 ||
1190: strcasecmp(specifier, "tcpv6") == 0 ||
1191: strcasecmp(specifier, "tcpipv6") == 0) {
1192: hints.ai_family = AF_INET6;
1193: hints.ai_socktype = SOCK_STREAM;
1194: } else if (strcasecmp(specifier, "unix") == 0) {
1195: hints.ai_family = AF_UNIX;
1196: hints.ai_socktype = SOCK_STREAM;
1197: hints.ai_addr = (struct sockaddr *)&saddr;
1198: hints.ai_addrlen = sizeof(saddr);
1199: saddr.sun_len = sizeof(saddr);
1200: saddr.sun_family = AF_UNIX;
1201: if (strlcpy(saddr.sun_path, hostname,
1202: sizeof(saddr.sun_path)) > sizeof(saddr.sun_path))
1203: errx(1, "Hostname path too long");
1204: ai = &hints;
1205: } else {
1206: port = hostname;
1207: hostname = specifier;
1208: specifier = NULL;
1209: hints.ai_family = AF_INET;
1210: hints.ai_socktype = SOCK_DGRAM;
1211: }
1212: if (port == NULL) {
1213: if (hints.ai_family == AF_INET) {
1214: if ((port = strchr(hostname, ':')) != NULL)
1215: *port++ = '\0';
1216: } else if (hints.ai_family == AF_INET6) {
1217: if (hostname[0] == '[') {
1218: hostname++;
1219: if ((port = strchr(hostname, ']')) == NULL)
1220: errx(1, "invalid agent");
1221: *port++ = '\0';
1222: if (port[0] == ':')
1223: *port++ = '\0';
1224: else
1225: port = NULL;
1226: } else {
1227: if ((port = strrchr(hostname, ':')) == NULL)
1228: errx(1, "invalid agent");
1229: *port++ = '\0';
1230: }
1231: }
1232: }
1233: } else {
1234: hostname = specifier;
1235: hints.ai_family = AF_INET;
1236: hints.ai_socktype = SOCK_DGRAM;
1237: }
1238:
1239: if (hints.ai_family != AF_UNIX) {
1240: if (port == NULL)
1241: port = defaultport;
1242: error = getaddrinfo(hostname, port, &hints, &ai0);
1243: if (error)
1244: errx(1, "%s", gai_strerror(error));
1245: s = -1;
1246: for (ai = ai0; ai != NULL; ai = ai->ai_next) {
1247: if ((s = socket(ai->ai_family, ai->ai_socktype,
1248: ai->ai_protocol)) == -1)
1249: continue;
1250: break;
1251: }
1252: } else
1253: s = socket(hints.ai_family, hints.ai_socktype,
1254: hints.ai_protocol);
1255: if (s == -1)
1256: err(1, "socket");
1257:
1258: if (connect(s, (struct sockaddr *)ai->ai_addr, ai->ai_addrlen) == -1)
1259: err(1, "Can't connect to %s", agent);
1260:
1261: if (ai0 != NULL)
1262: freeaddrinfo(ai0);
1263: free(agentdup);
1264: return s;
1265: }
1266:
1.9 martijn 1267: char *
1268: snmpc_hex2bin(char *hexstr, size_t *binlen)
1269: {
1270: char *decstr;
1271:
1272: if (hexstr[0] == '0' && hexstr[1] == 'x')
1273: hexstr += 2;
1274: while (hexstr[0] == ' ' || hexstr[0] == '\t')
1275: hexstr++;
1276:
1277: if ((decstr = malloc((strlen(hexstr) / 2) + 1)) == NULL)
1278: return NULL;
1279:
1280: for (*binlen = 0; hexstr[0] != '\0'; (*binlen)++) {
1281: hexstr[0] = toupper(hexstr[0]);
1282: hexstr[1] = toupper(hexstr[1]);
1283: if (hexstr[0] >= '0' && hexstr[0] <= '9')
1284: decstr[*binlen] = (hexstr[0] - '0') << 4;
1285: else if (hexstr[0] >= 'A' && hexstr[0] <= 'F')
1286: decstr[*binlen] = ((hexstr[0] - 'A') + 10) << 4;
1287: else
1288: goto fail;
1289: if (hexstr[1] >= '0' && hexstr[1] <= '9')
1290: decstr[*binlen] |= (hexstr[1] - '0');
1291: else if (hexstr[1] >= 'A' && hexstr[1] <= 'F')
1292: decstr[*binlen] |= (hexstr[1] - 'A') + 10;
1293: else
1294: goto fail;
1295:
1296: hexstr += 2;
1297: while (hexstr[0] == ' ' || hexstr[0] == '\t')
1298: hexstr++;
1299: }
1300:
1301: return decstr;
1302: fail:
1303: errno = EINVAL;
1304: free(decstr);
1305: return NULL;
1.13 martijn 1306: }
1307:
1308: struct ber_element *
1309: snmpc_varbindparse(int argc, char *argv[])
1310: {
1311: struct ber_oid oid, oidval;
1312: struct in_addr addr4;
1313: char *addr = (char *)&addr4;
1314: char *str = NULL, *tmpstr, *endstr;
1315: const char *errstr = NULL;
1316: struct ber_element *varbind = NULL, *vblist = NULL;
1317: int i, ret;
1318: size_t strl, byte;
1319: long long lval;
1320:
1321: if (argc % 3 != 0)
1322: usage();
1323: for (i = 0; i < argc; i += 3) {
1324: if (smi_string2oid(argv[i], &oid) == -1)
1325: errx(1, "Invalid oid: %s\n", argv[i]);
1326: switch (argv[i + 1][0]) {
1327: case 'a':
1328: ret = inet_pton(AF_INET, argv[i + 2], &addr4);
1329: if (ret == -1)
1330: err(1, "inet_pton");
1331: if (ret == 0)
1332: errx(1, "%s: Bad value notation (%s)", argv[i],
1333: argv[i + 2]);
1.16 tb 1334: if ((varbind = ober_printf_elements(varbind, "{Oxt}",
1.13 martijn 1335: &oid, addr, sizeof(addr4), BER_CLASS_APPLICATION,
1336: SNMP_T_IPADDR)) == NULL)
1.16 tb 1337: err(1, "ober_printf_elements");
1.13 martijn 1338: break;
1339: case 'b':
1340: tmpstr = argv[i + 2];
1341: strl = 0;
1342: do {
1343: lval = strtoll(tmpstr, &endstr, 10);
1344: if (endstr[0] != ' ' && endstr[0] != '\t' &&
1345: endstr[0] != ',' && endstr[0] != '\0')
1346: errx(1, "%s: Bad value notation (%s)",
1347: argv[i], argv[i + 2]);
1348: if (tmpstr == endstr) {
1349: tmpstr++;
1350: continue;
1351: }
1352: if (lval < 0)
1353: errx(1, "%s: Bad value notation (%s)",
1354: argv[i], argv[i + 2]);
1355: byte = lval / 8;
1356: if (byte >= strl) {
1357: if ((str = recallocarray(str, strl,
1358: byte + 1, 1)) == NULL)
1359: err(1, "malloc");
1360: strl = byte + 1;
1361: }
1362: str[byte] |= 0x80 >> (lval % 8);
1363: tmpstr = endstr + 1;
1364: } while (endstr[0] != '\0');
1365: /*
1366: * RFC3416 Section 2.5
1367: * A BITS value is encoded as an OCTET STRING
1368: */
1369: goto pastestring;
1370: case 'c':
1371: lval = strtonum(argv[i + 2], INT32_MIN, INT32_MAX,
1372: &errstr);
1373: if (errstr != NULL)
1374: errx(1, "%s: Bad value notation (%s)", argv[i],
1375: argv[i + 2]);
1.16 tb 1376: if ((varbind = ober_printf_elements(varbind, "{Oit}",
1.13 martijn 1377: &oid, lval, BER_CLASS_APPLICATION,
1378: SNMP_T_COUNTER32)) == NULL)
1.16 tb 1379: err(1, "ober_printf_elements");
1.13 martijn 1380: break;
1381: case 'd':
1382: /* String always shrinks */
1383: if ((str = malloc(strlen(argv[i + 2]))) == NULL)
1384: err(1, "malloc");
1385: tmpstr = argv[i + 2];
1386: strl = 0;
1387: do {
1388: lval = strtoll(tmpstr, &endstr, 10);
1389: if (endstr[0] != ' ' && endstr[0] != '\t' &&
1390: endstr[0] != '\0')
1391: errx(1, "%s: Bad value notation (%s)",
1392: argv[i], argv[i + 2]);
1393: if (tmpstr == endstr) {
1394: tmpstr++;
1395: continue;
1396: }
1397: if (lval < 0 || lval > 0xff)
1398: errx(1, "%s: Bad value notation (%s)",
1399: argv[i], argv[i + 2]);
1400: str[strl++] = (unsigned char) lval;
1401: tmpstr = endstr + 1;
1402: } while (endstr[0] != '\0');
1403: goto pastestring;
1404: case 'u':
1405: case 'i':
1406: lval = strtonum(argv[i + 2], LLONG_MIN, LLONG_MAX,
1407: &errstr);
1408: if (errstr != NULL)
1409: errx(1, "%s: Bad value notation (%s)", argv[i],
1410: argv[i + 2]);
1.16 tb 1411: if ((varbind = ober_printf_elements(varbind, "{Oi}",
1.13 martijn 1412: &oid, lval)) == NULL)
1.16 tb 1413: err(1, "ober_printf_elements");
1.13 martijn 1414: break;
1415: case 'n':
1.16 tb 1416: if ((varbind = ober_printf_elements(varbind, "{O0}",
1.13 martijn 1417: &oid)) == NULL)
1.16 tb 1418: err(1, "ober_printf_elements");
1.13 martijn 1419: break;
1420: case 'o':
1421: if (smi_string2oid(argv[i + 2], &oidval) == -1)
1422: errx(1, "%s: Unknown Object Identifier (Sub-id "
1423: "not found: (top) -> %s)", argv[i],
1424: argv[i + 2]);
1.16 tb 1425: if ((varbind = ober_printf_elements(varbind, "{OO}",
1.13 martijn 1426: &oid, &oidval)) == NULL)
1.16 tb 1427: err(1, "ober_printf_elements");
1.13 martijn 1428: break;
1429: case 's':
1430: if ((str = strdup(argv[i + 2])) == NULL)
1431: err(1, NULL);
1432: strl = strlen(argv[i + 2]);
1433: pastestring:
1.16 tb 1434: if ((varbind = ober_printf_elements(varbind, "{Ox}",
1.13 martijn 1435: &oid, str, strl)) == NULL)
1.16 tb 1436: err(1, "ober_printf_elements");
1.13 martijn 1437: free(str);
1438: break;
1439: case 't':
1440: lval = strtonum(argv[i + 2], LLONG_MIN, LLONG_MAX,
1441: &errstr);
1442: if (errstr != NULL)
1443: errx(1, "%s: Bad value notation (%s)", argv[i],
1444: argv[i + 2]);
1.16 tb 1445: if ((varbind = ober_printf_elements(varbind, "{Oit}",
1.13 martijn 1446: &oid, lval, BER_CLASS_APPLICATION,
1447: SNMP_T_TIMETICKS)) == NULL)
1.16 tb 1448: err(1, "ober_printf_elements");
1.13 martijn 1449: break;
1450: case 'x':
1451: /* String always shrinks */
1452: if ((str = malloc(strlen(argv[i + 2]))) == NULL)
1453: err(1, "malloc");
1454: tmpstr = argv[i + 2];
1455: strl = 0;
1456: do {
1457: lval = strtoll(tmpstr, &endstr, 16);
1458: if (endstr[0] != ' ' && endstr[0] != '\t' &&
1459: endstr[0] != '\0')
1460: errx(1, "%s: Bad value notation (%s)",
1461: argv[i], argv[i + 2]);
1462: if (tmpstr == endstr) {
1463: tmpstr++;
1464: continue;
1465: }
1466: if (lval < 0 || lval > 0xff)
1467: errx(1, "%s: Bad value notation (%s)",
1468: argv[i], argv[i + 2]);
1469: str[strl++] = (unsigned char) lval;
1470: tmpstr = endstr + 1;
1471: } while (endstr[0] != '\0');
1472: goto pastestring;
1473: default:
1474: usage();
1475: }
1476: if (vblist == NULL)
1477: vblist = varbind;
1478: }
1479:
1480: return vblist;
1.9 martijn 1481: }
1482:
1.1 martijn 1483: __dead void
1484: usage(void)
1485: {
1486: size_t i;
1487:
1488: if (snmp_app != NULL) {
1.9 martijn 1489: fprintf(stderr, "usage: snmp %s%s%s\n",
1.4 martijn 1490: snmp_app->name,
1.1 martijn 1491: snmp_app->usecommonopt ?
1.10 martijn 1492: " [-A authpass] [-a digest] [-c community] [-e secengineid]\n"
1.11 martijn 1493: " [-E ctxengineid] [-K localpriv] [-k localauth] [-l seclevel]\n"
1494: " [-n ctxname] [-O afnqvxSQ] [-r retries] [-t timeout] [-u user]\n"
1495: " [-v version] [-X privpass] [-x cipher] [-Z boots,time]\n"
1.9 martijn 1496: " " : "",
1.1 martijn 1497: snmp_app->usage == NULL ? "" : snmp_app->usage);
1498: exit(1);
1499: }
1500: for (i = 0; i < (sizeof(snmp_apps)/sizeof(*snmp_apps)); i++) {
1.4 martijn 1501: if (i == 0)
1502: fprintf(stderr, "usage: ");
1503: else
1504: fprintf(stderr, " ");
1505: fprintf(stderr, "snmp %s%s %s\n",
1506: snmp_apps[i].name,
1.1 martijn 1507: snmp_apps[i].usecommonopt ?
1.17 martijn 1508: " [options]" : "",
1.4 martijn 1509: snmp_apps[i].usage ? snmp_apps[i].usage : "");
1.1 martijn 1510: }
1511: exit(1);
1512: }