Annotation of src/usr.bin/snmp/smi.c, Revision 1.4
1.4 ! martijn 1: /* $OpenBSD: smi.c,v 1.3 2019/08/11 15:52:46 deraadt Exp $ */
1.1 martijn 2:
3: /*
4: * Copyright (c) 2019 Martijn van Duren <martijn@openbsd.org>
5: * Copyright (c) 2007, 2008 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/tree.h>
22: #include <sys/queue.h>
23:
24: #include <arpa/inet.h>
25:
26: #include <ctype.h>
27: #include <stdlib.h>
28: #include <stdio.h>
29: #include <string.h>
30: #include <strings.h>
31:
32: #include "ber.h"
33: #include "mib.h"
34: #include "snmp.h"
35: #include "smi.h"
36:
37: #define MINIMUM(a, b) (((a) < (b)) ? (a) : (b))
38:
39: int smi_oid_cmp(struct oid *, struct oid *);
40: int smi_key_cmp(struct oid *, struct oid *);
41: struct oid * smi_findkey(char *);
42:
43: RB_HEAD(oidtree, oid);
44: RB_PROTOTYPE(oidtree, oid, o_element, smi_oid_cmp)
45: struct oidtree smi_oidtree;
46:
47: RB_HEAD(keytree, oid);
48: RB_PROTOTYPE(keytree, oid, o_keyword, smi_key_cmp)
49: struct keytree smi_keytree;
50:
51: int
52: smi_init(void)
53: {
54: /* Initialize the Structure of Managed Information (SMI) */
55: RB_INIT(&smi_oidtree);
56: mib_init();
57: return (0);
58: }
59:
60: void
61: smi_debug_elements(struct ber_element *root)
62: {
63: static int indent = 0;
64: char *value;
65: int constructed;
66:
67: /* calculate lengths */
68: ber_calc_len(root);
69:
70: switch (root->be_encoding) {
71: case BER_TYPE_SEQUENCE:
72: case BER_TYPE_SET:
73: constructed = root->be_encoding;
74: break;
75: default:
76: constructed = 0;
77: break;
78: }
79:
80: fprintf(stderr, "%*slen %lu ", indent, "", root->be_len);
81: switch (root->be_class) {
82: case BER_CLASS_UNIVERSAL:
83: fprintf(stderr, "class: universal(%u) type: ", root->be_class);
84: switch (root->be_type) {
85: case BER_TYPE_EOC:
86: fprintf(stderr, "end-of-content");
87: break;
88: case BER_TYPE_BOOLEAN:
89: fprintf(stderr, "boolean");
90: break;
91: case BER_TYPE_INTEGER:
92: fprintf(stderr, "integer");
93: break;
94: case BER_TYPE_BITSTRING:
95: fprintf(stderr, "bit-string");
96: break;
97: case BER_TYPE_OCTETSTRING:
98: fprintf(stderr, "octet-string");
99: break;
100: case BER_TYPE_NULL:
101: fprintf(stderr, "null");
102: break;
103: case BER_TYPE_OBJECT:
104: fprintf(stderr, "object");
105: break;
106: case BER_TYPE_ENUMERATED:
107: fprintf(stderr, "enumerated");
108: break;
109: case BER_TYPE_SEQUENCE:
110: fprintf(stderr, "sequence");
111: break;
112: case BER_TYPE_SET:
113: fprintf(stderr, "set");
114: break;
115: }
116: break;
117: case BER_CLASS_APPLICATION:
118: fprintf(stderr, "class: application(%u) type: ",
119: root->be_class);
120: switch (root->be_type) {
121: case SNMP_T_IPADDR:
122: fprintf(stderr, "ipaddr");
123: break;
124: case SNMP_T_COUNTER32:
125: fprintf(stderr, "counter32");
126: break;
127: case SNMP_T_GAUGE32:
128: fprintf(stderr, "gauge32");
129: break;
130: case SNMP_T_TIMETICKS:
131: fprintf(stderr, "timeticks");
132: break;
133: case SNMP_T_OPAQUE:
134: fprintf(stderr, "opaque");
135: break;
136: case SNMP_T_COUNTER64:
137: fprintf(stderr, "counter64");
138: break;
139: }
140: break;
141: case BER_CLASS_CONTEXT:
142: fprintf(stderr, "class: context(%u) type: ",
143: root->be_class);
144: switch (root->be_type) {
145: case SNMP_C_GETREQ:
146: fprintf(stderr, "getreq");
147: break;
148: case SNMP_C_GETNEXTREQ:
149: fprintf(stderr, "nextreq");
150: break;
151: case SNMP_C_GETRESP:
152: fprintf(stderr, "getresp");
153: break;
154: case SNMP_C_SETREQ:
155: fprintf(stderr, "setreq");
156: break;
157: case SNMP_C_TRAP:
158: fprintf(stderr, "trap");
159: break;
160: case SNMP_C_GETBULKREQ:
161: fprintf(stderr, "getbulkreq");
162: break;
163: case SNMP_C_INFORMREQ:
164: fprintf(stderr, "informreq");
165: break;
166: case SNMP_C_TRAPV2:
167: fprintf(stderr, "trapv2");
168: break;
169: case SNMP_C_REPORT:
170: fprintf(stderr, "report");
171: break;
172: }
173: break;
174: case BER_CLASS_PRIVATE:
175: fprintf(stderr, "class: private(%u) type: ", root->be_class);
176: break;
177: default:
178: fprintf(stderr, "class: <INVALID>(%u) type: ", root->be_class);
179: break;
180: }
181: fprintf(stderr, "(%u) encoding %u ",
182: root->be_type, root->be_encoding);
183:
1.3 deraadt 184: if ((value = smi_print_element(root, 1, smi_os_default,
185: smi_oidl_numeric)) == NULL)
1.1 martijn 186: goto invalid;
187:
188: switch (root->be_encoding) {
189: case BER_TYPE_BOOLEAN:
190: fprintf(stderr, "%s", value);
191: break;
192: case BER_TYPE_INTEGER:
193: case BER_TYPE_ENUMERATED:
194: fprintf(stderr, "value %s", value);
195: break;
196: case BER_TYPE_BITSTRING:
197: fprintf(stderr, "hexdump %s", value);
198: break;
199: case BER_TYPE_OBJECT:
200: fprintf(stderr, "oid %s", value);
201: break;
202: case BER_TYPE_OCTETSTRING:
203: if (root->be_class == BER_CLASS_APPLICATION &&
204: root->be_type == SNMP_T_IPADDR) {
205: fprintf(stderr, "addr %s", value);
206: } else {
207: fprintf(stderr, "string %s", value);
208: }
209: break;
210: case BER_TYPE_NULL: /* no payload */
211: case BER_TYPE_EOC:
212: case BER_TYPE_SEQUENCE:
213: case BER_TYPE_SET:
214: default:
215: fprintf(stderr, "%s", value);
216: break;
217: }
218:
219: invalid:
220: if (value == NULL)
221: fprintf(stderr, "<INVALID>");
222: else
223: free(value);
224: fprintf(stderr, "\n");
225:
226: if (constructed)
227: root->be_encoding = constructed;
228:
229: if (constructed && root->be_sub) {
230: indent += 2;
231: smi_debug_elements(root->be_sub);
232: indent -= 2;
233: }
234: if (root->be_next)
235: smi_debug_elements(root->be_next);
236: }
237:
238: char *
239: smi_print_element(struct ber_element *root, int print_hint,
240: enum smi_output_string output_string, enum smi_oid_lookup lookup)
241: {
242: char *str = NULL, *buf, *p;
243: size_t len, i;
244: long long v, ticks;
245: int d;
1.4 ! martijn 246: int is_hex = 0, ret;
1.1 martijn 247: struct ber_oid o;
248: char strbuf[BUFSIZ];
249: char *hint;
250: int days, hours, min, sec, csec;
251:
252: switch (root->be_encoding) {
253: case BER_TYPE_BOOLEAN:
254: if (ber_get_boolean(root, &d) == -1)
255: goto fail;
256: if (print_hint) {
257: if (asprintf(&str, "INTEGER: %s(%d)",
258: d ? "true" : "false", d) == -1)
259: goto fail;
1.3 deraadt 260: } else
1.1 martijn 261: if (asprintf(&str, "%s", d ? "true" : "false") == -1)
262: goto fail;
263: break;
264: case BER_TYPE_INTEGER:
265: case BER_TYPE_ENUMERATED:
266: if (ber_get_integer(root, &v) == -1)
267: goto fail;
268: if (root->be_class == BER_CLASS_APPLICATION &&
269: root->be_type == SNMP_T_TIMETICKS) {
270: ticks = v;
271: days = ticks / (60 * 60 * 24 * 100);
272: ticks %= (60 * 60 * 24 * 100);
273: hours = ticks / (60 * 60 * 100);
274: ticks %= (60 * 60 * 100);
275: min = ticks / (60 * 100);
276: ticks %= (60 * 100);
277: sec = ticks / 100;
278: ticks %= 100;
279: csec = ticks;
280:
281: if (print_hint) {
282: if (days == 0) {
283: if (asprintf(&str,
284: "Timeticks: (%lld) "
285: "%d:%02d:%02d.%02d",
286: v, hours, min, sec, csec) == -1)
287: goto fail;
288: } else if (days == 1) {
289: if (asprintf(&str,
290: "Timeticks: (%lld) "
291: "1 day %d:%02d:%02d.%02d",
292: v, hours, min, sec, csec) == -1)
293: goto fail;
294: } else {
295: if (asprintf(&str,
296: "Timeticks: (%lld) "
297: "%d day %d:%02d:%02d.%02d",
298: v, days, hours, min, sec, csec) ==
299: -1)
300: goto fail;
301: }
302: } else {
303: if (days == 0) {
304: if (asprintf(&str, "%d:%02d:%02d.%02d",
305: hours, min, sec, csec) == -1)
306: goto fail;
307: } else if (days == 1) {
308: if (asprintf(&str,
309: "1 day %d:%02d:%02d.%02d",
310: hours, min, sec, csec) == -1)
311: goto fail;
312: } else {
313: if (asprintf(&str,
314: "%d day %d:%02d:%02d.%02d",
315: days, hours, min, sec, csec) == -1)
316: goto fail;
317: }
318: }
319: break;
320: }
321: hint = "INTEGER: ";
322: if (root->be_class == BER_CLASS_APPLICATION) {
323: if (root->be_type == SNMP_T_COUNTER32)
324: hint = "Counter32: ";
325: else if (root->be_type == SNMP_T_GAUGE32)
326: hint = "Gauge32: ";
327: else if (root->be_type == SNMP_T_OPAQUE)
328: hint = "Opaque: ";
329: else if (root->be_type == SNMP_T_COUNTER64)
330: hint = "Counter64: ";
331: }
1.3 deraadt 332: if (asprintf(&str, "%s%lld", print_hint ? hint : "", v) == -1)
1.1 martijn 333: goto fail;
334: break;
335: case BER_TYPE_BITSTRING:
336: if (ber_get_bitstring(root, (void *)&buf, &len) == -1)
337: goto fail;
338: if ((str = calloc(1, len * 2 + 1 + sizeof("BITS: "))) == NULL)
339: goto fail;
340: p = str;
341: if (print_hint) {
342: strlcpy(str, "BITS: ", sizeof(str));
343: p += sizeof("BITS: ");
344: }
345: for (i = 0; i < len; i++) {
346: snprintf(p, 3, "%02x", buf[i]);
347: p += 2;
348: }
349: break;
350: case BER_TYPE_OBJECT:
351: if (ber_get_oid(root, &o) == -1)
352: goto fail;
353: if (asprintf(&str, "%s%s",
354: print_hint ? "OID: " : "",
355: smi_oid2string(&o, strbuf, sizeof(strbuf), lookup)) == -1)
356: goto fail;
357: break;
358: case BER_TYPE_OCTETSTRING:
359: if (ber_get_string(root, &buf) == -1)
360: goto fail;
361: if (root->be_class == BER_CLASS_APPLICATION &&
362: root->be_type == SNMP_T_IPADDR) {
363: if (asprintf(&str, "%s%s",
364: print_hint ? "IpAddress: " : "",
365: inet_ntoa(*(struct in_addr *)buf)) == -1)
366: goto fail;
367: } else if (root->be_class == BER_CLASS_CONTEXT &&
368: root->be_type == BER_TYPE_EOC) {
369: str = strdup("No Such Object available on this agent at this OID");
1.2 deraadt 370: } else {
1.1 martijn 371: for (i = 0; i < root->be_len; i++) {
372: if (!isprint(buf[i])) {
373: if (output_string == smi_os_default)
374: output_string = smi_os_hex;
375: else if (output_string == smi_os_ascii)
376: is_hex = 1;
377: break;
378: }
379: }
380: /*
381: * hex is 3 * n (2 digits + n - 1 spaces + NUL-byte)
382: * ascii can be max (2 * n) + 2 quotes + NUL-byte
383: */
1.4 ! martijn 384: len = output_string == smi_os_hex ? 3 : 2;
! 385: p = str = reallocarray(NULL, root->be_len + 2, len);
! 386: if (p == NULL)
1.1 martijn 387: goto fail;
1.4 ! martijn 388: len *= root->be_len + 2;
! 389: if (is_hex) {
1.1 martijn 390: *str++ = '"';
1.4 ! martijn 391: len--;
! 392: }
1.1 martijn 393: for (i = 0; i < root->be_len; i++) {
394: switch (output_string) {
395: case smi_os_default:
396: /* FALLTHROUGH */
397: case smi_os_ascii:
398: /*
399: * There's probably more edgecases here,
400: * not fully investigated
401: */
1.4 ! martijn 402: if (len < 2)
! 403: goto fail;
! 404: if (is_hex && buf[i] == '\\') {
1.1 martijn 405: *str++ = '\\';
1.4 ! martijn 406: len--;
! 407: }
1.1 martijn 408: *str++ = isprint(buf[i]) ? buf[i] : '.';
1.4 ! martijn 409: len--;
1.1 martijn 410: break;
411: case smi_os_hex:
1.4 ! martijn 412: ret = snprintf(str, len, "%s%02hhX",
1.1 martijn 413: i == 0 ? "" :
414: i % 16 == 0 ? "\n" : " ", buf[i]);
1.4 ! martijn 415: if (ret == -1 || ret > (int) len)
! 416: goto fail;
! 417: len -= ret;
! 418: str += ret;
1.1 martijn 419: break;
420: }
421: }
1.4 ! martijn 422: if (is_hex) {
! 423: if (len < 2)
! 424: goto fail;
1.1 martijn 425: *str++ = '"';
1.4 ! martijn 426: len--;
! 427: }
! 428: if (len == 0)
! 429: goto fail;
1.1 martijn 430: *str = '\0';
431: str = NULL;
432: if (asprintf(&str, "%s%s",
433: print_hint ?
434: output_string == smi_os_hex ? "Hex-STRING: " :
435: "STRING: " :
436: "", p) == -1) {
437: free(p);
438: goto fail;
439: }
440: free(p);
441: }
442: break;
443: case BER_TYPE_NULL: /* no payload */
444: case BER_TYPE_EOC:
445: case BER_TYPE_SEQUENCE:
446: case BER_TYPE_SET:
447: default:
448: str = strdup("");
449: break;
450: }
451:
452: return (str);
453:
454: fail:
455: free(str);
456: return (NULL);
457: }
458:
459: int
460: smi_string2oid(const char *oidstr, struct ber_oid *o)
461: {
462: char *sp, *p, str[BUFSIZ];
463: const char *errstr;
464: struct oid *oid;
465: struct ber_oid ko;
466:
467: if (strlcpy(str, oidstr, sizeof(str)) >= sizeof(str))
468: return (-1);
469: bzero(o, sizeof(*o));
470:
471: /*
472: * Parse OID strings in the common form n.n.n or n-n-n.
473: * Based on ber_string2oid with additional support for symbolic names.
474: */
475: p = sp = str[0] == '.' ? str + 1 : str;
476: for (; p != NULL; sp = p) {
477: if ((p = strpbrk(p, ".-")) != NULL)
478: *p++ = '\0';
479: if ((oid = smi_findkey(sp)) != NULL) {
480: bcopy(&oid->o_id, &ko, sizeof(ko));
481: if (o->bo_n && ber_oid_cmp(o, &ko) != 2)
482: return (-1);
483: bcopy(&ko, o, sizeof(*o));
484: errstr = NULL;
485: } else {
486: o->bo_id[o->bo_n++] =
487: strtonum(sp, 0, UINT_MAX, &errstr);
488: }
489: if (errstr || o->bo_n > BER_MAX_OID_LEN)
490: return (-1);
491: }
492:
493: return (0);
494: }
495:
496: unsigned int
497: smi_application(struct ber_element *elm)
498: {
499: if (elm->be_class != BER_CLASS_APPLICATION)
500: return (BER_TYPE_OCTETSTRING);
501:
502: switch (elm->be_type) {
503: case SNMP_T_IPADDR:
504: return (BER_TYPE_OCTETSTRING);
505: case SNMP_T_COUNTER32:
506: case SNMP_T_GAUGE32:
507: case SNMP_T_TIMETICKS:
508: case SNMP_T_OPAQUE:
509: case SNMP_T_COUNTER64:
510: return (BER_TYPE_INTEGER);
511: default:
512: break;
513: }
514: return (BER_TYPE_OCTETSTRING);
515:
516: }
517:
518: char *
519: smi_oid2string(struct ber_oid *o, char *buf, size_t len,
520: enum smi_oid_lookup lookup)
521: {
522: char str[256];
523: struct oid *value, key;
524: size_t i;
525:
526: bzero(buf, len);
527: bzero(&key, sizeof(key));
528: bcopy(o, &key.o_id, sizeof(struct ber_oid));
529: key.o_flags |= OID_KEY; /* do not match wildcards */
530:
531: for (i = 0; i < o->bo_n; i++) {
532: key.o_oidlen = i + 1;
533: if (lookup != smi_oidl_numeric &&
534: (value = RB_FIND(oidtree, &smi_oidtree, &key)) != NULL) {
535: snprintf(str, sizeof(str), "%s", value->o_name);
536: if (lookup == smi_oidl_short && i + 1 < o->bo_n) {
537: key.o_oidlen = i + 2;
538: if (RB_FIND(oidtree, &smi_oidtree, &key) != NULL)
539: continue;
540: }
541: } else
542: snprintf(str, sizeof(str), "%d", key.o_oid[i]);
543: if (*buf != '\0' || i == 0)
544: strlcat(buf, ".", len);
545: strlcat(buf, str, len);
546: }
547:
548: return (buf);
549: }
550:
551: void
552: smi_mibtree(struct oid *oids)
553: {
554: struct oid *oid, *decl;
555: size_t i;
556:
557: for (i = 0; oids[i].o_oid[0] != 0; i++) {
558: oid = &oids[i];
559: if (oid->o_name != NULL) {
560: RB_INSERT(oidtree, &smi_oidtree, oid);
561: RB_INSERT(keytree, &smi_keytree, oid);
562: continue;
563: }
564: decl = RB_FIND(oidtree, &smi_oidtree, oid);
565: decl->o_flags = oid->o_flags;
566: decl->o_get = oid->o_get;
567: decl->o_set = oid->o_set;
568: decl->o_table = oid->o_table;
569: decl->o_val = oid->o_val;
570: decl->o_data = oid->o_data;
571: }
572: }
573:
574: struct oid *
575: smi_findkey(char *name)
576: {
577: struct oid oid;
578: if (name == NULL)
579: return (NULL);
580: oid.o_name = name;
581: return (RB_FIND(keytree, &smi_keytree, &oid));
582: }
583:
584: struct oid *
585: smi_foreach(struct oid *oid, u_int flags)
586: {
587: /*
588: * Traverse the tree of MIBs with the option to check
589: * for specific OID flags.
590: */
591: if (oid == NULL) {
592: oid = RB_MIN(oidtree, &smi_oidtree);
593: if (oid == NULL)
594: return (NULL);
595: if (flags == 0 || (oid->o_flags & flags))
596: return (oid);
597: }
598: for (;;) {
599: oid = RB_NEXT(oidtree, &smi_oidtree, oid);
600: if (oid == NULL)
601: break;
602: if (flags == 0 || (oid->o_flags & flags))
603: return (oid);
604: }
605:
606: return (oid);
607: }
608:
609: int
610: smi_oid_cmp(struct oid *a, struct oid *b)
611: {
612: size_t i;
613:
614: for (i = 0; i < MINIMUM(a->o_oidlen, b->o_oidlen); i++) {
615: if (a->o_oid[i] != b->o_oid[i])
616: return (a->o_oid[i] - b->o_oid[i]);
617: }
618:
619: /*
620: * Return success if the matched object is a table
621: * or a MIB registered by a subagent
622: * (it will match any sub-elements)
623: */
624: if ((b->o_flags & OID_TABLE ||
625: b->o_flags & OID_REGISTERED) &&
626: (a->o_flags & OID_KEY) == 0 &&
627: (a->o_oidlen > b->o_oidlen))
628: return (0);
629:
630: return (a->o_oidlen - b->o_oidlen);
631: }
632:
633: int
634: smi_key_cmp(struct oid *a, struct oid *b)
635: {
636: if (a->o_name == NULL || b->o_name == NULL)
637: return (-1);
638: return (strcasecmp(a->o_name, b->o_name));
639: }
640:
641: RB_GENERATE(oidtree, oid, o_element, smi_oid_cmp)
642: RB_GENERATE(keytree, oid, o_keyword, smi_key_cmp)