2 * Copyright (c) 2005-2006 The FreeBSD Project
5 * Author: Shteryana Shopova <syrinx@FreeBSD.org>
7 * Redistribution of this software and documentation and use in source and
8 * binary forms, with or without modification, are permitted provided that
9 * the following conditions are met:
11 * 1. Redistributions of source code or documentation must retain the above
12 * copyright notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Helper functions for snmp client tools
34 #include <sys/param.h>
35 #include <sys/queue.h>
49 #include <bsnmp/asn1.h>
50 #include <bsnmp/snmp.h>
51 #include <bsnmp/snmpclient.h>
53 #include "bsnmptools.h"
55 /* Internal varibale to turn on library debugging for testing and to
56 * find bugs. It is not exported via the header file.
57 * XXX should we cover it by some #ifdef BSNMPTOOLS_DEBUG? */
58 int _bsnmptools_debug = 0;
60 /* Default files to import mapping from if none explicitly provided. */
61 #define bsnmpd_defs "/usr/share/snmp/defs/tree.def"
62 #define mibII_defs "/usr/share/snmp/defs/mibII_tree.def"
65 * The .iso.org.dod oid that has to be prepended to every OID when requesting
68 const struct asn_oid IsoOrgDod_OID = {
73 #define SNMP_ERR_UNKNOWN 0
76 * An array of error strings corresponding to error definitions from libbsnmp.
82 { "Unknown", SNMP_ERR_UNKNOWN },
83 { "Too big ", SNMP_ERR_TOOBIG },
84 { "No such Name", SNMP_ERR_NOSUCHNAME },
85 { "Bad Value", SNMP_ERR_BADVALUE },
86 { "Readonly", SNMP_ERR_READONLY },
87 { "General error", SNMP_ERR_GENERR },
88 { "No access", SNMP_ERR_NO_ACCESS },
89 { "Wrong type", SNMP_ERR_WRONG_TYPE },
90 { "Wrong length", SNMP_ERR_WRONG_LENGTH },
91 { "Wrong encoding", SNMP_ERR_WRONG_ENCODING },
92 { "Wrong value", SNMP_ERR_WRONG_VALUE },
93 { "No creation", SNMP_ERR_NO_CREATION },
94 { "Inconsistent value", SNMP_ERR_INCONS_VALUE },
95 { "Resource unavailable", SNMP_ERR_RES_UNAVAIL },
96 { "Commit failed", SNMP_ERR_COMMIT_FAILED },
97 { "Undo failed", SNMP_ERR_UNDO_FAILED },
98 { "Authorization error", SNMP_ERR_AUTH_ERR },
99 { "Not writable", SNMP_ERR_NOT_WRITEABLE },
100 { "Inconsistent name", SNMP_ERR_INCONS_NAME },
104 /* This one and any following are exceptions. */
105 #define SNMP_SYNTAX_UNKNOWN SNMP_SYNTAX_NOSUCHOBJECT
107 static const struct {
109 enum snmp_syntax stx;
110 } syntax_strings[] = {
111 { "Null", SNMP_SYNTAX_NULL },
112 { "Integer", SNMP_SYNTAX_INTEGER },
113 { "OctetString", SNMP_SYNTAX_OCTETSTRING },
114 { "OID", SNMP_SYNTAX_OID },
115 { "IpAddress", SNMP_SYNTAX_IPADDRESS },
116 { "Counter32", SNMP_SYNTAX_COUNTER },
117 { "Gauge", SNMP_SYNTAX_GAUGE },
118 { "TimeTicks", SNMP_SYNTAX_TIMETICKS },
119 { "Counter64", SNMP_SYNTAX_COUNTER64 },
120 { "Unknown", SNMP_SYNTAX_UNKNOWN },
124 snmptool_init(struct snmp_toolinfo *snmptoolctx)
129 memset(snmptoolctx, 0, sizeof(struct snmp_toolinfo));
130 snmptoolctx->objects = 0;
131 snmptoolctx->mappings = NULL;
132 snmptoolctx->flags = SNMP_PDU_GET; /* XXX */
133 SLIST_INIT(&snmptoolctx->filelist);
134 snmp_client_init(&snmp_client);
135 SET_MAXREP(snmptoolctx, SNMP_MAX_REPETITIONS);
137 if (add_filename(snmptoolctx, bsnmpd_defs, &IsoOrgDod_OID, 0) < 0)
138 warnx("Error adding file %s to list", bsnmpd_defs);
140 if (add_filename(snmptoolctx, mibII_defs, &IsoOrgDod_OID, 0) < 0)
141 warnx("Error adding file %s to list", mibII_defs);
143 /* Read the environment */
144 if ((str = getenv("SNMPAUTH")) != NULL) {
146 if (slen == strlen("md5") && strcasecmp(str, "md5") == 0)
147 snmp_client.user.auth_proto = SNMP_AUTH_HMAC_MD5;
148 else if (slen == strlen("sha")&& strcasecmp(str, "sha") == 0)
149 snmp_client.user.auth_proto = SNMP_AUTH_HMAC_SHA;
151 warnx("Bad authentication type - %s in SNMPAUTH", str);
154 if ((str = getenv("SNMPPRIV")) != NULL) {
156 if (slen == strlen("des") && strcasecmp(str, "des") == 0)
157 snmp_client.user.priv_proto = SNMP_PRIV_DES;
158 else if (slen == strlen("aes")&& strcasecmp(str, "aes") == 0)
159 snmp_client.user.priv_proto = SNMP_PRIV_AES;
161 warnx("Bad privacy type - %s in SNMPPRIV", str);
164 if ((str = getenv("SNMPUSER")) != NULL) {
165 if ((slen = strlen(str)) > sizeof(snmp_client.user.sec_name)) {
166 warnx("Username too long - %s in SNMPUSER", str);
170 strlcpy(snmp_client.user.sec_name, str,
171 sizeof(snmp_client.user.sec_name));
172 snmp_client.version = SNMP_V3;
176 if ((str = getenv("SNMPPASSWD")) != NULL) {
177 if ((slen = strlen(str)) > MAXSTR)
179 if ((snmptoolctx->passwd = malloc(slen + 1)) == NULL) {
180 warnx("malloc() failed - %s", strerror(errno));
184 strlcpy(snmptoolctx->passwd, str, slen + 1);
190 #define OBJECT_IDX_LIST(o) o->info->table_idx->index_list
193 * Walk through the file list and import string<->oid mappings from each file.
196 snmp_import_all(struct snmp_toolinfo *snmptoolctx)
201 if (snmptoolctx == NULL)
204 if (ISSET_NUMERIC(snmptoolctx))
207 if ((snmptoolctx->mappings = snmp_mapping_init()) == NULL)
211 if (SLIST_EMPTY(&snmptoolctx->filelist)) {
212 warnx("No files to read OID <-> string conversions from");
215 SLIST_FOREACH(tmp, &snmptoolctx->filelist, link) {
218 if (snmp_import_file(snmptoolctx, tmp) < 0) {
226 snmp_mapping_dump(snmptoolctx);
231 * Add a filename to the file list - the initial idea of keeping a list with all
232 * files to read OIDs from was that an application might want to have loaded in
233 * memory the OIDs from a single file only and when done with them read the OIDs
234 * from another file. This is not used yet but might be a good idea at some
235 * point. Size argument is number of bytes in string including trailing '\0',
239 add_filename(struct snmp_toolinfo *snmptoolctx, const char *filename,
240 const struct asn_oid *cut, int32_t done)
245 if (snmptoolctx == NULL)
248 /* Make sure file was not in list. */
249 SLIST_FOREACH(entry, &snmptoolctx->filelist, link) {
250 if (strncmp(entry->name, filename, strlen(entry->name)) == 0)
254 if ((fstring = strdup(filename)) == NULL) {
255 warnx("strdup() failed - %s", strerror(errno));
259 if ((entry = calloc(1, sizeof(struct fname))) == NULL) {
260 warnx("calloc() failed - %s", strerror(errno));
266 asn_append_oid(&(entry->cut), cut);
267 entry->name = fstring;
269 SLIST_INSERT_HEAD(&snmptoolctx->filelist, entry, link);
275 free_filelist(struct snmp_toolinfo *snmptoolctx)
279 if (snmptoolctx == NULL)
280 return; /* XXX error handling */
282 while ((f = SLIST_FIRST(&snmptoolctx->filelist)) != NULL) {
283 SLIST_REMOVE_HEAD(&snmptoolctx->filelist, link);
291 isvalid_fchar(char c, int pos)
293 if (isalpha(c)|| c == '/'|| c == '_' || c == '.' || c == '~' ||
294 (pos != 0 && isdigit(c))){
301 if (!isascii(c) || !isprint(c))
302 warnx("Unexpected character %#2x", (u_int) c);
304 warnx("Illegal character '%c'", c);
310 * Re-implement getsubopt from scratch, because the second argument is broken
311 * and will not compile with WARNS=5.
312 * Copied from src/contrib/bsnmp/snmpd/main.c.
315 getsubopt1(char **arg, const char *const *options, char **valp, char **optp)
317 static const char *const delim = ",\t ";
323 /* Skip leading junk. */
324 for (ptr = *arg; *ptr != '\0'; ptr++)
325 if (strchr(delim, *ptr) == NULL)
333 /* Find the end of the option. */
334 while (*++ptr != '\0')
335 if (strchr(delim, *ptr) != NULL || *ptr == '=')
342 while (*ptr != '\0' && strchr(delim, *ptr) == NULL)
352 for (i = 0; *options != NULL; options++, i++)
353 if (strcmp(*optp, *options) == 0)
359 parse_path(char *value)
366 for (len = 0; len < MAXPATHLEN; len++) {
367 i = isvalid_fchar(*(value + len), len) ;
375 if (len >= MAXPATHLEN || value[len] != '\0') {
376 warnx("Bad pathname - '%s'", value);
384 parse_flist(struct snmp_toolinfo *snmptoolctx, char *value, char *path,
385 const struct asn_oid *cut)
388 char filename[MAXPATHLEN + 1];
394 memset(filename, 0, MAXPATHLEN + 1);
396 if (isalpha(*value) && (path == NULL || path[0] == '\0')) {
397 strlcpy(filename, SNMP_DEFS_DIR, MAXPATHLEN + 1);
398 namelen = strlen(SNMP_DEFS_DIR);
399 } else if (path != NULL){
400 strlcpy(filename, path, MAXPATHLEN + 1);
401 namelen = strlen(path);
405 for ( ; namelen < MAXPATHLEN; value++) {
406 if (isvalid_fchar(*value, namelen) > 0) {
407 filename[namelen++] = *value;
413 else if (*value == '\0')
416 if (!isascii(*value) || !isprint(*value))
417 warnx("Unexpected character %#2x in"
418 " filename", (u_int) *value);
420 warnx("Illegal character '%c' in"
421 " filename", *value);
425 filename[namelen]='\0';
429 if ((namelen == MAXPATHLEN) && (filename[MAXPATHLEN] != '\0')) {
430 warnx("Filename %s too long", filename);
434 if (add_filename(snmptoolctx, filename, cut, 0) < 0) {
435 warnx("Error adding file %s to list", filename);
438 } while (*value != '\0');
444 parse_ascii(char *ascii, uint8_t *binstr, size_t binlen)
448 int32_t alen, i, saved_errno;
451 /* Filter 0x at the beginning */
452 if ((alen = strlen(ascii)) > 2 && ascii[0] == '0' && ascii[1] == 'x')
459 for (count = 0; i < alen; i += 2) {
460 /* XXX: consider strlen(ascii) % 2 != 0 */
462 dptr[1] = ascii[i + 1];
464 if ((val = strtoul(dptr, NULL, 16)) > 0xFF || errno != 0) {
468 binstr[count] = (uint8_t) val;
469 if (++count >= binlen) {
470 warnx("Key %s too long - truncating to %zu octets",
480 * Functions to parse common input options for client tools and fill in the
481 * snmp_client structure.
484 parse_authentication(struct snmp_toolinfo *snmptoolctx __unused, char *opt_arg)
486 int32_t count, subopt;
488 const char *const subopts[] = {
494 assert(opt_arg != NULL);
496 while ((subopt = getsubopt1(&opt_arg, subopts, &val, &option)) != EOF) {
500 warnx("Suboption 'proto' requires an argument");
503 if (strlen(val) != 3) {
504 warnx("Unknown auth protocol - %s", val);
507 if (strncasecmp("md5", val, strlen("md5")) == 0)
508 snmp_client.user.auth_proto =
510 else if (strncasecmp("sha", val, strlen("sha")) == 0)
511 snmp_client.user.auth_proto =
514 warnx("Unknown auth protocol - %s", val);
520 warnx("Suboption 'key' requires an argument");
523 if (parse_ascii(val, snmp_client.user.auth_key,
524 SNMP_AUTH_KEY_SIZ) < 0) {
525 warnx("Bad authentication key- %s", val);
530 warnx("Unknown suboption - '%s'", suboptarg);
535 return (2/* count */);
539 parse_privacy(struct snmp_toolinfo *snmptoolctx __unused, char *opt_arg)
541 int32_t count, subopt;
543 const char *const subopts[] = {
549 assert(opt_arg != NULL);
551 while ((subopt = getsubopt1(&opt_arg, subopts, &val, &option)) != EOF) {
555 warnx("Suboption 'proto' requires an argument");
558 if (strlen(val) != 3) {
559 warnx("Unknown privacy protocol - %s", val);
562 if (strncasecmp("aes", val, strlen("aes")) == 0)
563 snmp_client.user.priv_proto = SNMP_PRIV_AES;
564 else if (strncasecmp("des", val, strlen("des")) == 0)
565 snmp_client.user.priv_proto = SNMP_PRIV_DES;
567 warnx("Unknown privacy protocol - %s", val);
573 warnx("Suboption 'key' requires an argument");
576 if (parse_ascii(val, snmp_client.user.priv_key,
577 SNMP_PRIV_KEY_SIZ) < 0) {
578 warnx("Bad privacy key- %s", val);
583 warnx("Unknown suboption - '%s'", suboptarg);
588 return (2/* count */);
592 parse_context(struct snmp_toolinfo *snmptoolctx __unused, char *opt_arg)
594 int32_t count, subopt;
596 const char *const subopts[] = {
602 assert(opt_arg != NULL);
604 while ((subopt = getsubopt1(&opt_arg, subopts, &val, &option)) != EOF) {
608 warnx("Suboption 'context' - no argument");
611 strlcpy(snmp_client.cname, val, SNMP_CONTEXT_NAME_SIZ);
615 warnx("Suboption 'context-engine' - no argument");
618 if ((int32_t)(snmp_client.clen = parse_ascii(val,
619 snmp_client.cengine, SNMP_ENGINE_ID_SIZ)) == -1) {
620 warnx("Bad EngineID - %s", val);
625 warnx("Unknown suboption - '%s'", suboptarg);
630 return (2/* count */);
634 parse_user_security(struct snmp_toolinfo *snmptoolctx __unused, char *opt_arg)
636 int32_t count, subopt, saved_errno;
638 const char *const subopts[] = {
646 assert(opt_arg != NULL);
648 while ((subopt = getsubopt1(&opt_arg, subopts, &val, &option)) != EOF) {
652 warnx("Suboption 'engine' - no argument");
655 snmp_client.engine.engine_len = parse_ascii(val,
656 snmp_client.engine.engine_id, SNMP_ENGINE_ID_SIZ);
657 if ((int32_t)snmp_client.engine.engine_len == -1) {
658 warnx("Bad EngineID - %s", val);
664 warnx("Suboption 'engine-boots' - no argument");
669 snmp_client.engine.engine_boots = strtoul(val, NULL, 10);
671 warnx("Bad 'engine-boots' value %s - %s", val,
680 warnx("Suboption 'engine-time' - no argument");
685 snmp_client.engine.engine_time = strtoul(val, NULL, 10);
687 warnx("Bad 'engine-time' value %s - %s", val,
695 strlcpy(snmp_client.user.sec_name, val,
699 warnx("Unknown suboption - '%s'", suboptarg);
704 return (2/* count */);
708 parse_file(struct snmp_toolinfo *snmptoolctx, char *opt_arg)
710 assert(opt_arg != NULL);
712 if (parse_flist(snmptoolctx, opt_arg, NULL, &IsoOrgDod_OID) < 0)
719 parse_include(struct snmp_toolinfo *snmptoolctx, char *opt_arg)
721 char path[MAXPATHLEN + 1];
722 int32_t cut_dflt, len, subopt;
725 const char *const subopts[] = {
736 assert(opt_arg != NULL);
739 free_filelist(snmptoolctx, ); */
741 * This function should be called only after getopt(3) - otherwise if
742 * no previous validation of opt_arg strlen() may not return what is
747 memset(&cut, 0, sizeof(struct asn_oid));
750 while ((subopt = getsubopt1(&opt_arg, subopts, &val, &option)) != EOF) {
754 warnx("Suboption 'cut' requires an argument");
757 if (snmp_parse_numoid(val, &cut) < 0)
764 if ((len = parse_path(val)) < 0)
766 strlcpy(path, val, len + 1);
773 len = parse_flist(snmptoolctx, val, path, &IsoOrgDod_OID);
775 len = parse_flist(snmptoolctx, val, path, &cut);
781 warnx("Unknown suboption - '%s'", suboptarg);
786 /* XXX: Fix me - returning two is wrong here */
791 parse_server(char *opt_arg)
793 assert(opt_arg != NULL);
795 if (snmp_parse_server(&snmp_client, opt_arg) < 0)
798 if (snmp_client.trans > SNMP_TRANS_UDP && snmp_client.chost == NULL) {
799 if ((snmp_client.chost = malloc(strlen(SNMP_DEFAULT_LOCAL) + 1))
801 syslog(LOG_ERR, "malloc() failed: %s", strerror(errno));
804 strcpy(snmp_client.chost, SNMP_DEFAULT_LOCAL);
811 parse_timeout(char *opt_arg)
813 int32_t v, saved_errno;
815 assert(opt_arg != NULL);
820 v = strtol(opt_arg, NULL, 10);
822 warnx( "Error parsing timeout value - %s", strerror(errno));
827 snmp_client.timeout.tv_sec = v;
833 parse_retry(char *opt_arg)
838 assert(opt_arg != NULL);
843 v = strtoul(opt_arg, NULL, 10);
845 warnx("Error parsing retries count - %s", strerror(errno));
850 snmp_client.retries = v;
856 parse_version(char *opt_arg)
861 assert(opt_arg != NULL);
866 v = strtoul(opt_arg, NULL, 10);
868 warnx("Error parsing version - %s", strerror(errno));
875 snmp_client.version = SNMP_V1;
878 snmp_client.version = SNMP_V2c;
881 snmp_client.version = SNMP_V3;
884 warnx("Unsupported SNMP version - %u", v);
894 parse_local_path(char *opt_arg)
896 assert(opt_arg != NULL);
898 if (sizeof(opt_arg) > sizeof(SNMP_LOCAL_PATH)) {
899 warnx("Filename too long - %s", opt_arg);
903 strlcpy(snmp_client.local_path, opt_arg, sizeof(SNMP_LOCAL_PATH));
908 parse_buflen(char *opt_arg)
913 assert(opt_arg != NULL);
918 size = strtoul(opt_arg, NULL, 10);
920 warnx("Error parsing buffer size - %s", strerror(errno));
925 if (size > MAX_BUFF_SIZE) {
926 warnx("Buffer size too big - %d max allowed", MAX_BUFF_SIZE);
931 snmp_client.txbuflen = snmp_client.rxbuflen = size;
939 snmp_client.dump_pdus = 1;
944 parse_discovery(struct snmp_toolinfo *snmptoolctx)
946 SET_EDISCOVER(snmptoolctx);
947 snmp_client.version = SNMP_V3;
952 parse_local_key(struct snmp_toolinfo *snmptoolctx)
954 SET_LOCALKEY(snmptoolctx);
955 snmp_client.version = SNMP_V3;
960 parse_num_oids(struct snmp_toolinfo *snmptoolctx)
962 SET_NUMERIC(snmptoolctx);
967 parse_output(struct snmp_toolinfo *snmptoolctx, char *opt_arg)
969 assert(opt_arg != NULL);
971 if (strlen(opt_arg) > strlen("verbose")) {
972 warnx( "Invalid output option - %s",opt_arg);
976 if (strncasecmp(opt_arg, "short", strlen(opt_arg)) == 0)
977 SET_OUTPUT(snmptoolctx, OUTPUT_SHORT);
978 else if (strncasecmp(opt_arg, "verbose", strlen(opt_arg)) == 0)
979 SET_OUTPUT(snmptoolctx, OUTPUT_VERBOSE);
980 else if (strncasecmp(opt_arg,"tabular", strlen(opt_arg)) == 0)
981 SET_OUTPUT(snmptoolctx, OUTPUT_TABULAR);
982 else if (strncasecmp(opt_arg, "quiet", strlen(opt_arg)) == 0)
983 SET_OUTPUT(snmptoolctx, OUTPUT_QUIET);
985 warnx( "Invalid output option - %s", opt_arg);
993 parse_errors(struct snmp_toolinfo *snmptoolctx)
995 SET_RETRY(snmptoolctx);
1000 parse_skip_access(struct snmp_toolinfo *snmptoolctx)
1002 SET_ERRIGNORE(snmptoolctx);
1007 snmp_parse_suboid(char *str, struct asn_oid *oid)
1015 if (*str < '0' || *str > '9')
1019 suboid = strtoul(str, &endptr, 10);
1020 if ((asn_subid_t) suboid > ASN_MAXID) {
1021 warnx("Suboid %u > ASN_MAXID", suboid);
1024 if (snmp_suboid_append(oid, suboid) < 0)
1027 } while (*endptr == '.');
1033 snmp_int2asn_oid(char *str, struct asn_oid *oid)
1036 int32_t v, saved_errno;
1038 saved_errno = errno;
1041 v = strtol(str, &endptr, 10);
1043 warnx("Integer value %s not supported - %s", str,
1045 errno = saved_errno;
1048 errno = saved_errno;
1050 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
1056 /* It is a bit weird to have a table indexed by OID but still... */
1058 snmp_oid2asn_oid(struct snmp_toolinfo *snmptoolctx, char *str,
1059 struct asn_oid *oid)
1062 char string[MAXSTR + 1], *endptr;
1063 struct snmp_object obj;
1065 for (i = 0; i < MAXSTR; i++)
1066 if (isalpha (*(str + i)) == 0)
1070 memset(&obj, 0, sizeof(struct snmp_object));
1072 if ((endptr = snmp_parse_suboid(str, &(obj.val.var))) == NULL)
1074 if (snmp_suboid_append(oid, (asn_subid_t) obj.val.var.len) < 0)
1077 strlcpy(string, str, i + 1);
1078 if (snmp_lookup_enumoid(snmptoolctx, &obj, string) < 0) {
1079 warnx("Unknown string - %s", string);
1084 asn_append_oid(oid, &(obj.val.var));
1089 snmp_ip2asn_oid(char *str, struct asn_oid *oid)
1096 for (i = 0; i < 4; i++) {
1097 v = strtoul(ptr, &endptr, 10);
1100 if (*endptr != '.' && strchr("],\0", *endptr) == NULL && i != 3)
1102 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
1110 /* 32-bit counter, gauge, timeticks. */
1112 snmp_uint2asn_oid(char *str, struct asn_oid *oid)
1116 int32_t saved_errno;
1118 saved_errno = errno;
1121 v = strtoul(str, &endptr, 10);
1123 warnx("Integer value %s not supported - %s\n", str,
1125 errno = saved_errno;
1128 errno = saved_errno;
1129 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
1136 snmp_cnt64_2asn_oid(char *str, struct asn_oid *oid)
1140 int32_t saved_errno;
1142 saved_errno = errno;
1145 v = strtoull(str, &endptr, 10);
1148 warnx("Integer value %s not supported - %s", str,
1150 errno = saved_errno;
1153 errno = saved_errno;
1154 if (snmp_suboid_append(oid, (asn_subid_t) (v & 0xffffffff)) < 0)
1157 if (snmp_suboid_append(oid, (asn_subid_t) (v >> 32)) < 0)
1164 parse_syntax(char *str)
1168 for (i = 0; i < SNMP_SYNTAX_UNKNOWN; i++) {
1169 if (strncmp(syntax_strings[i].str, str,
1170 strlen(syntax_strings[i].str)) == 0)
1171 return (syntax_strings[i].stx);
1174 return (SNMP_SYNTAX_NULL);
1178 snmp_parse_subindex(struct snmp_toolinfo *snmptoolctx, char *str,
1179 struct index *idx, struct snmp_object *object)
1183 enum snmp_syntax stx;
1184 char syntax[MAX_CMD_SYNTAX_LEN];
1187 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE) {
1188 for (i = 0; i < MAX_CMD_SYNTAX_LEN ; i++) {
1189 if (*(ptr + i) == ':')
1193 if (i >= MAX_CMD_SYNTAX_LEN) {
1194 warnx("Unknown syntax in OID - %s", str);
1197 /* Expect a syntax string here. */
1198 if ((stx = parse_syntax(str)) <= SNMP_SYNTAX_NULL) {
1199 warnx("Invalid syntax - %s",syntax);
1203 if (stx != idx->syntax && !ISSET_ERRIGNORE(snmptoolctx)) {
1204 warnx("Syntax mismatch - %d expected, %d given",
1209 * That is where the suboid started + the syntax length + one
1210 * character for ':'.
1217 case SNMP_SYNTAX_INTEGER:
1218 return (snmp_int2asn_oid(ptr, &(object->val.var)));
1219 case SNMP_SYNTAX_OID:
1220 return (snmp_oid2asn_oid(snmptoolctx, ptr,
1221 &(object->val.var)));
1222 case SNMP_SYNTAX_IPADDRESS:
1223 return (snmp_ip2asn_oid(ptr, &(object->val.var)));
1224 case SNMP_SYNTAX_COUNTER:
1226 case SNMP_SYNTAX_GAUGE:
1228 case SNMP_SYNTAX_TIMETICKS:
1229 return (snmp_uint2asn_oid(ptr, &(object->val.var)));
1230 case SNMP_SYNTAX_COUNTER64:
1231 return (snmp_cnt64_2asn_oid(ptr, &(object->val.var)));
1232 case SNMP_SYNTAX_OCTETSTRING:
1233 return (snmp_tc2oid(idx->tc, ptr, &(object->val.var)));
1243 snmp_parse_index(struct snmp_toolinfo *snmptoolctx, char *str,
1244 struct snmp_object *object)
1249 if (object->info->table_idx == NULL)
1253 STAILQ_FOREACH(temp, &(OBJECT_IDX_LIST(object)), link) {
1254 if ((ptr = snmp_parse_subindex(snmptoolctx, str, temp, object))
1258 if (*ptr != ',' && *ptr != ']')
1263 if (ptr == NULL || *ptr != ']') {
1264 warnx("Mismatching index - %s", str);
1272 * Fill in the struct asn_oid member of snmp_value with suboids from input.
1273 * If an error occurs - print message on stderr and return (-1).
1274 * If all is ok - return the length of the oid.
1277 snmp_parse_numoid(char *argv, struct asn_oid *var)
1288 if (var->len == ASN_MAXOIDLEN) {
1289 warnx("Oid too long - %u", var->len);
1293 suboid = strtoul(str, &endptr, 10);
1294 if (suboid > ASN_MAXID) {
1295 warnx("Oid too long - %u", var->len);
1299 var->subs[var->len++] = suboid;
1301 } while ( *endptr == '.');
1303 if (*endptr != '\0') {
1304 warnx("Invalid oid string - %s", argv);
1311 /* Append a length 1 suboid to an asn_oid structure. */
1313 snmp_suboid_append(struct asn_oid *var, asn_subid_t suboid)
1318 if (var->len >= ASN_MAXOIDLEN) {
1319 warnx("Oid too long - %u", var->len);
1323 var->subs[var->len++] = suboid;
1328 /* Pop the last suboid from an asn_oid structure. */
1330 snmp_suboid_pop(struct asn_oid *var)
1340 suboid = var->subs[--(var->len)];
1341 var->subs[var->len] = 0;
1347 * Parse the command-line provided string into an OID - alocate memory for a new
1348 * snmp object, fill in its fields and insert it in the object list. A
1349 * (snmp_verify_inoid_f) function must be provided to validate the input string.
1352 snmp_object_add(struct snmp_toolinfo *snmptoolctx, snmp_verify_inoid_f func,
1355 struct snmp_object *obj;
1357 if (snmptoolctx == NULL)
1360 /* XXX-BZ does that chack make sense? */
1361 if (snmptoolctx->objects >= SNMP_MAX_BINDINGS) {
1362 warnx("Too many bindings in PDU - %u", snmptoolctx->objects + 1);
1366 if ((obj = calloc(1, sizeof(struct snmp_object))) == NULL) {
1367 syslog(LOG_ERR, "malloc() failed: %s", strerror(errno));
1371 if (func(snmptoolctx, obj, string) < 0) {
1372 warnx("Invalid OID - %s", string);
1377 snmptoolctx->objects++;
1378 SLIST_INSERT_HEAD(&snmptoolctx->snmp_objectlist, obj, link);
1383 /* Given an OID, find it in the object list and remove it. */
1385 snmp_object_remove(struct snmp_toolinfo *snmptoolctx, struct asn_oid *oid)
1387 struct snmp_object *temp;
1389 if (SLIST_EMPTY(&snmptoolctx->snmp_objectlist)) {
1390 warnx("Object list already empty");
1395 SLIST_FOREACH(temp, &snmptoolctx->snmp_objectlist, link)
1396 if (asn_compare_oid(&(temp->val.var), oid) == 0)
1400 warnx("No such object in list");
1404 SLIST_REMOVE(&snmptoolctx->snmp_objectlist, temp, snmp_object, link);
1405 if (temp->val.syntax == SNMP_SYNTAX_OCTETSTRING &&
1406 temp->val.v.octetstring.octets != NULL)
1407 free(temp->val.v.octetstring.octets);
1414 snmp_object_freeall(struct snmp_toolinfo *snmptoolctx)
1416 struct snmp_object *o;
1418 while ((o = SLIST_FIRST(&snmptoolctx->snmp_objectlist)) != NULL) {
1419 SLIST_REMOVE_HEAD(&snmptoolctx->snmp_objectlist, link);
1421 if (o->val.syntax == SNMP_SYNTAX_OCTETSTRING &&
1422 o->val.v.octetstring.octets != NULL)
1423 free(o->val.v.octetstring.octets);
1428 /* Do all possible memory release before exit. */
1430 snmp_tool_freeall(struct snmp_toolinfo *snmptoolctx)
1432 if (snmp_client.chost != NULL) {
1433 free(snmp_client.chost);
1434 snmp_client.chost = NULL;
1437 if (snmp_client.cport != NULL) {
1438 free(snmp_client.cport);
1439 snmp_client.cport = NULL;
1442 snmp_mapping_free(snmptoolctx);
1443 free_filelist(snmptoolctx);
1444 snmp_object_freeall(snmptoolctx);
1446 if (snmptoolctx->passwd != NULL) {
1447 free(snmptoolctx->passwd);
1448 snmptoolctx->passwd = NULL;
1453 * Fill all variables from the object list into a PDU. (snmp_verify_vbind_f)
1454 * function should check whether the variable is consistent in this PDU
1455 * (e.g do not add non-leaf OIDs to a GET PDU, or OIDs with read access only to
1456 * a SET PDU) - might be NULL though. (snmp_add_vbind_f) function is the
1457 * function actually adds the variable to the PDU and must not be NULL.
1460 snmp_pdu_add_bindings(struct snmp_toolinfo *snmptoolctx,
1461 snmp_verify_vbind_f vfunc, snmp_add_vbind_f afunc,
1462 struct snmp_pdu *pdu, int32_t maxcount)
1464 int32_t nbindings, abind;
1465 struct snmp_object *obj;
1467 if (pdu == NULL || afunc == NULL)
1470 /* Return 0 in case of no more work todo. */
1471 if (SLIST_EMPTY(&snmptoolctx->snmp_objectlist))
1474 if (maxcount < 0 || maxcount > SNMP_MAX_BINDINGS) {
1475 warnx("maxcount out of range: <0 || >SNMP_MAX_BINDINGS");
1480 SLIST_FOREACH(obj, &snmptoolctx->snmp_objectlist, link) {
1481 if ((vfunc != NULL) && (vfunc(snmptoolctx, pdu, obj) < 0)) {
1485 if ((abind = afunc(pdu, obj)) < 0) {
1491 /* Do not put more varbindings than requested. */
1492 if (++nbindings >= maxcount)
1501 * Locate an object in the object list and set a corresponding error status.
1504 snmp_object_seterror(struct snmp_toolinfo *snmptoolctx,
1505 struct snmp_value *err_value, int32_t error_status)
1507 struct snmp_object *obj;
1509 if (SLIST_EMPTY(&snmptoolctx->snmp_objectlist) || err_value == NULL)
1512 SLIST_FOREACH(obj, &snmptoolctx->snmp_objectlist, link)
1513 if (asn_compare_oid(&(err_value->var), &(obj->val.var)) == 0) {
1514 obj->error = error_status;
1522 * Check a PDU received in response to a SNMP_PDU_GET/SNMP_PDU_GETBULK request
1523 * but don't compare syntaxes - when sending a request PDU they must be null.
1524 * This is a (almost) complete copy of snmp_pdu_check() - with matching syntaxes
1525 * checks and some other checks skipped.
1528 snmp_parse_get_resp(struct snmp_pdu *resp, struct snmp_pdu *req)
1532 for (i = 0; i < req->nbindings; i++) {
1533 if (asn_compare_oid(&req->bindings[i].var,
1534 &resp->bindings[i].var) != 0) {
1535 warnx("Bad OID in response");
1539 if (snmp_client.version != SNMP_V1 && (resp->bindings[i].syntax
1540 == SNMP_SYNTAX_NOSUCHOBJECT || resp->bindings[i].syntax ==
1541 SNMP_SYNTAX_NOSUCHINSTANCE))
1549 snmp_parse_getbulk_resp(struct snmp_pdu *resp, struct snmp_pdu *req)
1553 if (req->error_status > (int32_t) resp->nbindings) {
1554 warnx("Bad number of bindings in response");
1558 for (N = 0; N < req->error_status; N++) {
1559 if (asn_is_suboid(&req->bindings[N].var,
1560 &resp->bindings[N].var) == 0)
1562 if (resp->bindings[N].syntax == SNMP_SYNTAX_ENDOFMIBVIEW)
1566 for (R = N , r = N; R < (int32_t) req->nbindings; R++) {
1567 for (M = 0; M < req->error_index && (r + M) <
1568 (int32_t) resp->nbindings; M++) {
1569 if (asn_is_suboid(&req->bindings[R].var,
1570 &resp->bindings[r + M].var) == 0)
1573 if (resp->bindings[r + M].syntax ==
1574 SNMP_SYNTAX_ENDOFMIBVIEW) {
1586 snmp_parse_getnext_resp(struct snmp_pdu *resp, struct snmp_pdu *req)
1590 for (i = 0; i < req->nbindings; i++) {
1591 if (asn_is_suboid(&req->bindings[i].var, &resp->bindings[i].var)
1595 if (resp->version != SNMP_V1 && resp->bindings[i].syntax ==
1596 SNMP_SYNTAX_ENDOFMIBVIEW)
1604 * Should be called to check a response to get/getnext/getbulk.
1607 snmp_parse_resp(struct snmp_pdu *resp, struct snmp_pdu *req)
1609 if (resp == NULL || req == NULL)
1612 if (resp->version != req->version) {
1613 warnx("Response has wrong version");
1617 if (resp->error_status == SNMP_ERR_NOSUCHNAME) {
1618 warnx("Error - No Such Name");
1622 if (resp->error_status != SNMP_ERR_NOERROR) {
1623 warnx("Error %d in response", resp->error_status);
1627 if (resp->nbindings != req->nbindings && req->type != SNMP_PDU_GETBULK){
1628 warnx("Bad number of bindings in response");
1632 switch (req->type) {
1634 return (snmp_parse_get_resp(resp,req));
1635 case SNMP_PDU_GETBULK:
1636 return (snmp_parse_getbulk_resp(resp,req));
1637 case SNMP_PDU_GETNEXT:
1638 return (snmp_parse_getnext_resp(resp,req));
1648 snmp_output_octetstring(struct snmp_toolinfo *snmptoolctx, enum snmp_tc tc,
1649 uint32_t len, uint8_t *octets)
1653 if (len == 0 || octets == NULL)
1656 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1657 fprintf(stdout, "%s : ",
1658 syntax_strings[SNMP_SYNTAX_OCTETSTRING].str);
1660 if ((buf = snmp_oct2tc(tc, len, (char *) octets)) != NULL) {
1661 fprintf(stdout, "%s", buf);
1667 snmp_output_octetindex(struct snmp_toolinfo *snmptoolctx, enum snmp_tc tc,
1668 struct asn_oid *oid)
1673 if ((s = malloc(oid->subs[0] + 1)) == NULL)
1674 syslog(LOG_ERR, "malloc failed - %s", strerror(errno));
1676 for (i = 0; i < oid->subs[0]; i++)
1677 s[i] = (u_char) (oid->subs[i + 1]);
1679 snmp_output_octetstring(snmptoolctx, tc, oid->subs[0], s);
1685 * Check and output syntax type and value.
1688 snmp_output_oid_value(struct snmp_toolinfo *snmptoolctx, struct asn_oid *oid)
1690 char oid_string[ASN_OIDSTRLEN];
1691 struct snmp_object obj;
1693 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1694 fprintf(stdout, "%s : ", syntax_strings[SNMP_SYNTAX_OID].str);
1696 if(!ISSET_NUMERIC(snmptoolctx)) {
1697 memset(&obj, 0, sizeof(struct snmp_object));
1698 asn_append_oid(&(obj.val.var), oid);
1700 if (snmp_lookup_enumstring(snmptoolctx, &obj) > 0)
1701 fprintf(stdout, "%s" , obj.info->string);
1702 else if (snmp_lookup_oidstring(snmptoolctx, &obj) > 0)
1703 fprintf(stdout, "%s" , obj.info->string);
1704 else if (snmp_lookup_nodestring(snmptoolctx, &obj) > 0)
1705 fprintf(stdout, "%s" , obj.info->string);
1707 (void) asn_oid2str_r(oid, oid_string);
1708 fprintf(stdout, "%s", oid_string);
1711 (void) asn_oid2str_r(oid, oid_string);
1712 fprintf(stdout, "%s", oid_string);
1717 snmp_output_int(struct snmp_toolinfo *snmptoolctx, struct enum_pairs *enums,
1722 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1723 fprintf(stdout, "%s : ",
1724 syntax_strings[SNMP_SYNTAX_INTEGER].str);
1726 if (enums != NULL && (string = enum_string_lookup(enums, int_val))
1728 fprintf(stdout, "%s", string);
1730 fprintf(stdout, "%d", int_val);
1734 snmp_output_ipaddress(struct snmp_toolinfo *snmptoolctx, uint8_t *ip)
1736 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1737 fprintf(stdout, "%s : ",
1738 syntax_strings[SNMP_SYNTAX_IPADDRESS].str);
1740 fprintf(stdout, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
1744 snmp_output_counter(struct snmp_toolinfo *snmptoolctx, uint32_t counter)
1746 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1747 fprintf(stdout, "%s : ",
1748 syntax_strings[SNMP_SYNTAX_COUNTER].str);
1750 fprintf(stdout, "%u", counter);
1754 snmp_output_gauge(struct snmp_toolinfo *snmptoolctx, uint32_t gauge)
1756 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1757 fprintf(stdout, "%s : ", syntax_strings[SNMP_SYNTAX_GAUGE].str);
1759 fprintf(stdout, "%u", gauge);
1763 snmp_output_ticks(struct snmp_toolinfo *snmptoolctx, uint32_t ticks)
1765 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1766 fprintf(stdout, "%s : ",
1767 syntax_strings[SNMP_SYNTAX_TIMETICKS].str);
1769 fprintf(stdout, "%u", ticks);
1773 snmp_output_counter64(struct snmp_toolinfo *snmptoolctx, uint64_t counter64)
1775 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1776 fprintf(stdout, "%s : ",
1777 syntax_strings[SNMP_SYNTAX_COUNTER64].str);
1779 fprintf(stdout,"%ju", counter64);
1783 snmp_output_numval(struct snmp_toolinfo *snmptoolctx, struct snmp_value *val,
1784 struct snmp_oid2str *entry)
1789 if (GET_OUTPUT(snmptoolctx) != OUTPUT_QUIET)
1790 fprintf(stdout, " = ");
1792 switch (val->syntax) {
1793 case SNMP_SYNTAX_INTEGER:
1795 snmp_output_int(snmptoolctx, entry->snmp_enum,
1798 snmp_output_int(snmptoolctx, NULL, val->v.integer);
1801 case SNMP_SYNTAX_OCTETSTRING:
1803 snmp_output_octetstring(snmptoolctx, entry->tc,
1804 val->v.octetstring.len, val->v.octetstring.octets);
1806 snmp_output_octetstring(snmptoolctx, SNMP_STRING,
1807 val->v.octetstring.len, val->v.octetstring.octets);
1810 case SNMP_SYNTAX_OID:
1811 snmp_output_oid_value(snmptoolctx, &(val->v.oid));
1814 case SNMP_SYNTAX_IPADDRESS:
1815 snmp_output_ipaddress(snmptoolctx, val->v.ipaddress);
1818 case SNMP_SYNTAX_COUNTER:
1819 snmp_output_counter(snmptoolctx, val->v.uint32);
1822 case SNMP_SYNTAX_GAUGE:
1823 snmp_output_gauge(snmptoolctx, val->v.uint32);
1826 case SNMP_SYNTAX_TIMETICKS:
1827 snmp_output_ticks(snmptoolctx, val->v.uint32);
1830 case SNMP_SYNTAX_COUNTER64:
1831 snmp_output_counter64(snmptoolctx, val->v.counter64);
1834 case SNMP_SYNTAX_NOSUCHOBJECT:
1835 fprintf(stdout, "No Such Object\n");
1836 return (val->syntax);
1838 case SNMP_SYNTAX_NOSUCHINSTANCE:
1839 fprintf(stdout, "No Such Instance\n");
1840 return (val->syntax);
1842 case SNMP_SYNTAX_ENDOFMIBVIEW:
1843 fprintf(stdout, "End of Mib View\n");
1844 return (val->syntax);
1846 case SNMP_SYNTAX_NULL:
1848 fprintf(stdout, "agent returned NULL Syntax\n");
1849 return (val->syntax);
1852 /* NOTREACHED - If here - then all went completely wrong. */
1853 fprintf(stdout, "agent returned unknown syntax\n");
1857 fprintf(stdout, "\n");
1863 snmp_fill_object(struct snmp_toolinfo *snmptoolctx, struct snmp_object *obj,
1864 struct snmp_value *val)
1869 if (obj == NULL || val == NULL)
1872 if ((suboid = snmp_suboid_pop(&(val->var))) > ASN_MAXID)
1875 memset(obj, 0, sizeof(struct snmp_object));
1876 asn_append_oid(&(obj->val.var), &(val->var));
1877 obj->val.syntax = val->syntax;
1879 if (obj->val.syntax > 0)
1880 rc = snmp_lookup_leafstring(snmptoolctx, obj);
1882 rc = snmp_lookup_nonleaf_string(snmptoolctx, obj);
1884 (void) snmp_suboid_append(&(val->var), suboid);
1885 (void) snmp_suboid_append(&(obj->val.var), suboid);
1891 snmp_output_index(struct snmp_toolinfo *snmptoolctx, struct index *stx,
1892 struct asn_oid *oid)
1897 struct asn_oid temp, out;
1899 if (oid->len < bytes)
1902 memset(&temp, 0, sizeof(struct asn_oid));
1903 asn_append_oid(&temp, oid);
1905 switch (stx->syntax) {
1906 case SNMP_SYNTAX_INTEGER:
1907 snmp_output_int(snmptoolctx, stx->snmp_enum, temp.subs[0]);
1910 case SNMP_SYNTAX_OCTETSTRING:
1911 if ((temp.subs[0] > temp.len -1 ) || (temp.subs[0] >
1912 ASN_MAXOCTETSTRING))
1914 snmp_output_octetindex(snmptoolctx, stx->tc, &temp);
1915 bytes += temp.subs[0];
1918 case SNMP_SYNTAX_OID:
1919 if ((temp.subs[0] > temp.len -1) || (temp.subs[0] >
1923 bytes += temp.subs[0];
1924 memset(&out, 0, sizeof(struct asn_oid));
1925 asn_slice_oid(&out, &temp, 1, bytes);
1926 snmp_output_oid_value(snmptoolctx, &out);
1929 case SNMP_SYNTAX_IPADDRESS:
1932 for (bytes = 0; bytes < 4; bytes++)
1933 ip[bytes] = temp.subs[bytes];
1935 snmp_output_ipaddress(snmptoolctx, ip);
1939 case SNMP_SYNTAX_COUNTER:
1940 snmp_output_counter(snmptoolctx, temp.subs[0]);
1943 case SNMP_SYNTAX_GAUGE:
1944 snmp_output_gauge(snmptoolctx, temp.subs[0]);
1947 case SNMP_SYNTAX_TIMETICKS:
1948 snmp_output_ticks(snmptoolctx, temp.subs[0]);
1951 case SNMP_SYNTAX_COUNTER64:
1955 memcpy(&cnt64, temp.subs, bytes);
1956 snmp_output_counter64(snmptoolctx, cnt64);
1967 snmp_output_object(struct snmp_toolinfo *snmptoolctx, struct snmp_object *o)
1969 int32_t i, first, len;
1973 if (ISSET_NUMERIC(snmptoolctx))
1976 if (o->info->table_idx == NULL) {
1977 fprintf(stdout,"%s.%d", o->info->string,
1978 o->val.var.subs[o->val.var.len - 1]);
1982 fprintf(stdout,"%s[", o->info->string);
1983 memset(&oid, 0, sizeof(struct asn_oid));
1986 asn_slice_oid(&oid, &(o->val.var), (o->info->table_idx->var.len + len),
1990 STAILQ_FOREACH(temp, &(OBJECT_IDX_LIST(o)), link) {
1994 fprintf(stdout, ", ");
1995 if ((i = snmp_output_index(snmptoolctx, temp, &oid)) < 0)
1998 memset(&oid, 0, sizeof(struct asn_oid));
1999 asn_slice_oid(&oid, &(o->val.var),
2000 (o->info->table_idx->var.len + len), o->val.var.len + 1);
2003 fprintf(stdout,"]");
2008 snmp_output_err_resp(struct snmp_toolinfo *snmptoolctx, struct snmp_pdu *pdu)
2010 char buf[ASN_OIDSTRLEN];
2011 struct snmp_object object;
2013 if (pdu == NULL || (pdu->error_index > (int32_t) pdu->nbindings)) {
2014 fprintf(stdout,"Invalid error index in PDU\n");
2018 fprintf(stdout, "Agent %s:%s returned error \n", snmp_client.chost,
2021 if (!ISSET_NUMERIC(snmptoolctx) && (snmp_fill_object(snmptoolctx, &object,
2022 &(pdu->bindings[pdu->error_index - 1])) > 0))
2023 snmp_output_object(snmptoolctx, &object);
2025 asn_oid2str_r(&(pdu->bindings[pdu->error_index - 1].var), buf);
2026 fprintf(stdout,"%s", buf);
2029 fprintf(stdout," caused error - ");
2030 if ((pdu->error_status > 0) && (pdu->error_status <=
2031 SNMP_ERR_INCONS_NAME))
2032 fprintf(stdout, "%s\n", error_strings[pdu->error_status].str);
2034 fprintf(stdout,"%s\n", error_strings[SNMP_ERR_UNKNOWN].str);
2038 snmp_output_resp(struct snmp_toolinfo *snmptoolctx, struct snmp_pdu *pdu,
2039 struct asn_oid *root)
2042 char p[ASN_OIDSTRLEN];
2044 struct snmp_object object;
2047 while (i < pdu->nbindings) {
2048 if (root != NULL && !(asn_is_suboid(root,
2049 &(pdu->bindings[i].var))))
2052 if (GET_OUTPUT(snmptoolctx) != OUTPUT_QUIET) {
2053 if (!ISSET_NUMERIC(snmptoolctx) &&
2054 (snmp_fill_object(snmptoolctx, &object,
2055 &(pdu->bindings[i])) > 0))
2056 snmp_output_object(snmptoolctx, &object);
2058 asn_oid2str_r(&(pdu->bindings[i].var), p);
2059 fprintf(stdout, "%s", p);
2062 error |= snmp_output_numval(snmptoolctx, &(pdu->bindings[i]), object.info);
2073 snmp_output_engine(void)
2076 char *cptr, engine[2 * SNMP_ENGINE_ID_SIZ + 2];
2079 for (i = 0; i < snmp_client.engine.engine_len; i++)
2080 cptr += sprintf(cptr, "%.2x", snmp_client.engine.engine_id[i]);
2083 fprintf(stdout, "Engine ID 0x%s\n", engine);
2084 fprintf(stdout, "Boots : %u\t\tTime : %d\n",
2085 snmp_client.engine.engine_boots,
2086 snmp_client.engine.engine_time);
2090 snmp_output_keys(void)
2092 uint32_t i, keylen = 0;
2093 char *cptr, extkey[2 * SNMP_AUTH_KEY_SIZ + 2];
2095 fprintf(stdout, "Localized keys for %s\n", snmp_client.user.sec_name);
2096 if (snmp_client.user.auth_proto == SNMP_AUTH_HMAC_MD5) {
2097 fprintf(stdout, "MD5 : 0x");
2098 keylen = SNMP_AUTH_HMACMD5_KEY_SIZ;
2099 } else if (snmp_client.user.auth_proto == SNMP_AUTH_HMAC_SHA) {
2100 fprintf(stdout, "SHA : 0x");
2101 keylen = SNMP_AUTH_HMACSHA_KEY_SIZ;
2103 if (snmp_client.user.auth_proto != SNMP_AUTH_NOAUTH) {
2105 for (i = 0; i < keylen; i++)
2106 cptr += sprintf(cptr, "%.2x",
2107 snmp_client.user.auth_key[i]);
2109 fprintf(stdout, "%s\n", extkey);
2112 if (snmp_client.user.priv_proto == SNMP_PRIV_DES) {
2113 fprintf(stdout, "DES : 0x");
2114 keylen = SNMP_PRIV_DES_KEY_SIZ;
2115 } else if (snmp_client.user.priv_proto == SNMP_PRIV_AES) {
2116 fprintf(stdout, "AES : 0x");
2117 keylen = SNMP_PRIV_AES_KEY_SIZ;
2119 if (snmp_client.user.priv_proto != SNMP_PRIV_NOPRIV) {
2121 for (i = 0; i < keylen; i++)
2122 cptr += sprintf(cptr, "%.2x",
2123 snmp_client.user.priv_key[i]);
2125 fprintf(stdout, "%s\n", extkey);