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 lenght", 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);
136 if (add_filename(snmptoolctx, bsnmpd_defs, &IsoOrgDod_OID, 0) < 0)
137 warnx("Error adding file %s to list", bsnmpd_defs);
139 if (add_filename(snmptoolctx, mibII_defs, &IsoOrgDod_OID, 0) < 0)
140 warnx("Error adding file %s to list", mibII_defs);
142 /* Read the environment */
143 if ((str = getenv("SNMPAUTH")) != NULL) {
145 if (slen == strlen("md5") && strcasecmp(str, "md5") == 0)
146 snmp_client.user.auth_proto = SNMP_AUTH_HMAC_MD5;
147 else if (slen == strlen("sha")&& strcasecmp(str, "sha") == 0)
148 snmp_client.user.auth_proto = SNMP_AUTH_HMAC_SHA;
150 warnx("Bad authentication type - %s in SNMPAUTH", str);
153 if ((str = getenv("SNMPPRIV")) != NULL) {
155 if (slen == strlen("des") && strcasecmp(str, "des") == 0)
156 snmp_client.user.priv_proto = SNMP_PRIV_DES;
157 else if (slen == strlen("aes")&& strcasecmp(str, "aes") == 0)
158 snmp_client.user.priv_proto = SNMP_PRIV_AES;
160 warnx("Bad privacy type - %s in SNMPPRIV", str);
163 if ((str = getenv("SNMPUSER")) != NULL) {
164 if ((slen = strlen(str)) > sizeof(snmp_client.user.sec_name)) {
165 warnx("Username too long - %s in SNMPUSER", str);
169 strlcpy(snmp_client.user.sec_name, str,
170 sizeof(snmp_client.user.sec_name));
171 snmp_client.version = SNMP_V3;
175 if ((str = getenv("SNMPPASSWD")) != NULL) {
176 if ((slen = strlen(str)) > MAXSTR)
178 if ((snmptoolctx->passwd = malloc(slen + 1)) == NULL) {
179 warnx("malloc() failed - %s", strerror(errno));
183 strlcpy(snmptoolctx->passwd, str, slen + 1);
189 #define OBJECT_IDX_LIST(o) o->info->table_idx->index_list
192 * Walk through the file list and import string<->oid mappings from each file.
195 snmp_import_all(struct snmp_toolinfo *snmptoolctx)
200 if (snmptoolctx == NULL)
203 if (ISSET_NUMERIC(snmptoolctx))
206 if ((snmptoolctx->mappings = snmp_mapping_init()) == NULL)
210 if (SLIST_EMPTY(&snmptoolctx->filelist)) {
211 warnx("No files to read OID <-> string conversions from");
214 SLIST_FOREACH(tmp, &snmptoolctx->filelist, link) {
217 if (snmp_import_file(snmptoolctx, tmp) < 0) {
225 snmp_mapping_dump(snmptoolctx);
230 * Add a filename to the file list - the initail idea of keeping a list with all
231 * files to read OIDs from was that an application might want to have loaded in
232 * memory the OIDs from a single file only and when done with them read the OIDs
233 * from another file. This is not used yet but might be a good idea at some
234 * point. Size argument is number of bytes in string including trailing '\0',
238 add_filename(struct snmp_toolinfo *snmptoolctx, const char *filename,
239 const struct asn_oid *cut, int32_t done)
244 if (snmptoolctx == NULL)
247 /* Make sure file was not in list. */
248 SLIST_FOREACH(entry, &snmptoolctx->filelist, link) {
249 if (strncmp(entry->name, filename, strlen(entry->name)) == 0)
253 if ((fstring = strdup(filename)) == NULL) {
254 warnx("strdup() failed - %s", strerror(errno));
258 if ((entry = calloc(1, sizeof(struct fname))) == NULL) {
259 warnx("calloc() failed - %s", strerror(errno));
265 asn_append_oid(&(entry->cut), cut);
266 entry->name = fstring;
268 SLIST_INSERT_HEAD(&snmptoolctx->filelist, entry, link);
274 free_filelist(struct snmp_toolinfo *snmptoolctx)
278 if (snmptoolctx == NULL)
279 return; /* XXX error handling */
281 while ((f = SLIST_FIRST(&snmptoolctx->filelist)) != NULL) {
282 SLIST_REMOVE_HEAD(&snmptoolctx->filelist, link);
290 isvalid_fchar(char c, int pos)
292 if (isalpha(c)|| c == '/'|| c == '_' || c == '.' || c == '~' ||
293 (pos != 0 && isdigit(c))){
300 if (!isascii(c) || !isprint(c))
301 warnx("Unexpected character %#2x", (u_int) c);
303 warnx("Illegal character '%c'", c);
309 * Re-implement getsubopt from scratch, because the second argument is broken
310 * and will not compile with WARNS=5.
311 * Copied from src/contrib/bsnmp/snmpd/main.c.
314 getsubopt1(char **arg, const char *const *options, char **valp, char **optp)
316 static const char *const delim = ",\t ";
322 /* Skip leading junk. */
323 for (ptr = *arg; *ptr != '\0'; ptr++)
324 if (strchr(delim, *ptr) == NULL)
332 /* Find the end of the option. */
333 while (*++ptr != '\0')
334 if (strchr(delim, *ptr) != NULL || *ptr == '=')
341 while (*ptr != '\0' && strchr(delim, *ptr) == NULL)
351 for (i = 0; *options != NULL; options++, i++)
352 if (strcmp(*optp, *options) == 0)
358 parse_path(char *value)
365 for (len = 0; len < MAXPATHLEN; len++) {
366 i = isvalid_fchar(*(value + len), len) ;
374 if (len >= MAXPATHLEN || value[len] != '\0') {
375 warnx("Bad pathname - '%s'", value);
383 parse_flist(struct snmp_toolinfo *snmptoolctx, char *value, char *path,
384 const struct asn_oid *cut)
387 char filename[MAXPATHLEN + 1];
393 memset(filename, 0, MAXPATHLEN + 1);
395 if (isalpha(*value) && (path == NULL || path[0] == '\0')) {
396 strlcpy(filename, SNMP_DEFS_DIR, MAXPATHLEN + 1);
397 namelen = strlen(SNMP_DEFS_DIR);
398 } else if (path != NULL){
399 strlcpy(filename, path, MAXPATHLEN + 1);
400 namelen = strlen(path);
404 for ( ; namelen < MAXPATHLEN; value++) {
405 if (isvalid_fchar(*value, namelen) > 0) {
406 filename[namelen++] = *value;
412 else if (*value == '\0')
415 if (!isascii(*value) || !isprint(*value))
416 warnx("Unexpected character %#2x in"
417 " filename", (u_int) *value);
419 warnx("Illegal character '%c' in"
420 " filename", *value);
424 filename[namelen]='\0';
428 if ((namelen == MAXPATHLEN) && (filename[MAXPATHLEN] != '\0')) {
429 warnx("Filename %s too long", filename);
433 if (add_filename(snmptoolctx, filename, cut, 0) < 0) {
434 warnx("Error adding file %s to list", filename);
437 } while (*value != '\0');
443 parse_ascii(char *ascii, uint8_t *binstr, size_t binlen)
447 int32_t alen, i, saved_errno;
450 /* Filter 0x at the beggining */
451 if ((alen = strlen(ascii)) > 2 && ascii[0] == '0' && ascii[1] == 'x')
458 for (count = 0; i < alen; i += 2) {
459 /* XXX: consider strlen(ascii) % 2 != 0 */
461 dptr[1] = ascii[i + 1];
463 if ((val = strtoul(dptr, NULL, 16)) > 0xFF || errno != 0) {
467 binstr[count] = (uint8_t) val;
468 if (++count >= binlen) {
469 warnx("Key %s too long - truncating to %zu octest",
479 * Functions to parse common input options for client tools and fill in the
480 * snmp_client structure.
483 parse_authentication(struct snmp_toolinfo *snmptoolctx __unused, char *opt_arg)
485 int32_t count, subopt;
487 const char *const subopts[] = {
493 assert(opt_arg != NULL);
495 while ((subopt = getsubopt1(&opt_arg, subopts, &val, &option)) != EOF) {
499 warnx("Suboption 'proto' requires an argument");
502 if (strlen(val) != 3) {
503 warnx("Unknown auth protocol - %s", val);
506 if (strncasecmp("md5", val, strlen("md5")) == 0)
507 snmp_client.user.auth_proto =
509 else if (strncasecmp("sha", val, strlen("sha")) == 0)
510 snmp_client.user.auth_proto =
513 warnx("Unknown auth protocol - %s", val);
519 warnx("Suboption 'key' requires an argument");
522 if (parse_ascii(val, snmp_client.user.auth_key,
523 SNMP_AUTH_KEY_SIZ) < 0) {
524 warnx("Bad authentication key- %s", val);
529 warnx("Unknown suboption - '%s'", suboptarg);
534 return (2/* count */);
538 parse_privacy(struct snmp_toolinfo *snmptoolctx __unused, char *opt_arg)
540 int32_t count, subopt;
542 const char *const subopts[] = {
548 assert(opt_arg != NULL);
550 while ((subopt = getsubopt1(&opt_arg, subopts, &val, &option)) != EOF) {
554 warnx("Suboption 'proto' requires an argument");
557 if (strlen(val) != 3) {
558 warnx("Unknown privacy protocol - %s", val);
561 if (strncasecmp("aes", val, strlen("aes")) == 0)
562 snmp_client.user.priv_proto = SNMP_PRIV_AES;
563 else if (strncasecmp("des", val, strlen("des")) == 0)
564 snmp_client.user.priv_proto = SNMP_PRIV_DES;
566 warnx("Unknown privacy protocol - %s", val);
572 warnx("Suboption 'key' requires an argument");
575 if (parse_ascii(val, snmp_client.user.priv_key,
576 SNMP_PRIV_KEY_SIZ) < 0) {
577 warnx("Bad privacy key- %s", val);
582 warnx("Unknown suboption - '%s'", suboptarg);
587 return (2/* count */);
591 parse_context(struct snmp_toolinfo *snmptoolctx __unused, char *opt_arg)
593 int32_t count, subopt;
595 const char *const subopts[] = {
601 assert(opt_arg != NULL);
603 while ((subopt = getsubopt1(&opt_arg, subopts, &val, &option)) != EOF) {
607 warnx("Suboption 'context' - no argument");
610 strlcpy(snmp_client.cname, val, SNMP_CONTEXT_NAME_SIZ);
614 warnx("Suboption 'context-engine' - no argument");
617 if ((snmp_client.clen = parse_ascii(val,
618 snmp_client.cengine, SNMP_ENGINE_ID_SIZ)) < 0) {
619 warnx("Bad EngineID - %s", val);
624 warnx("Unknown suboption - '%s'", suboptarg);
629 return (2/* count */);
633 parse_user_security(struct snmp_toolinfo *snmptoolctx __unused, char *opt_arg)
635 int32_t count, subopt, saved_errno;
637 const char *const subopts[] = {
645 assert(opt_arg != NULL);
647 while ((subopt = getsubopt1(&opt_arg, subopts, &val, &option)) != EOF) {
651 warnx("Suboption 'engine' - no argument");
654 snmp_client.engine.engine_len = parse_ascii(val,
655 snmp_client.engine.engine_id, SNMP_ENGINE_ID_SIZ);
656 if (snmp_client.engine.engine_len < 0) {
657 warnx("Bad EngineID - %s", val);
663 warnx("Suboption 'engine-boots' - no argument");
668 snmp_client.engine.engine_boots = strtoul(val, NULL, 10);
670 warnx("Bad 'engine-boots' value %s - %s", val,
679 warnx("Suboption 'engine-time' - no argument");
684 snmp_client.engine.engine_time = strtoul(val, NULL, 10);
686 warnx("Bad 'engine-time' value %s - %s", val,
694 strlcpy(snmp_client.user.sec_name, val,
698 warnx("Unknown suboption - '%s'", suboptarg);
703 return (2/* count */);
707 parse_file(struct snmp_toolinfo *snmptoolctx, char *opt_arg)
709 assert(opt_arg != NULL);
711 if (parse_flist(snmptoolctx, opt_arg, NULL, &IsoOrgDod_OID) < 0)
718 parse_include(struct snmp_toolinfo *snmptoolctx, char *opt_arg)
720 char path[MAXPATHLEN + 1];
721 int32_t cut_dflt, len, subopt;
724 const char *const subopts[] = {
735 assert(opt_arg != NULL);
738 free_filelist(snmptoolctx, ); */
740 * This function should be called only after getopt(3) - otherwise if
741 * no previous validation of opt_arg strlen() may not return what is
746 memset(&cut, 0, sizeof(struct asn_oid));
749 while ((subopt = getsubopt1(&opt_arg, subopts, &val, &option)) != EOF) {
753 warnx("Suboption 'cut' requires an argument");
756 if (snmp_parse_numoid(val, &cut) < 0)
763 if ((len = parse_path(val)) < 0)
765 strlcpy(path, val, len + 1);
772 len = parse_flist(snmptoolctx, val, path, &IsoOrgDod_OID);
774 len = parse_flist(snmptoolctx, val, path, &cut);
780 warnx("Unknown suboption - '%s'", suboptarg);
785 /* XXX: Fix me - returning two is wrong here */
790 parse_server(char *opt_arg)
792 assert(opt_arg != NULL);
794 if (snmp_parse_server(&snmp_client, opt_arg) < 0)
797 if (snmp_client.trans > SNMP_TRANS_UDP && snmp_client.chost == NULL) {
798 if ((snmp_client.chost = malloc(strlen(SNMP_DEFAULT_LOCAL + 1)))
800 syslog(LOG_ERR, "malloc() failed: %s", strerror(errno));
803 strcpy(snmp_client.chost, SNMP_DEFAULT_LOCAL);
810 parse_timeout(char *opt_arg)
812 int32_t v, saved_errno;
814 assert(opt_arg != NULL);
819 v = strtol(opt_arg, NULL, 10);
821 warnx( "Error parsing timeout value - %s", strerror(errno));
826 snmp_client.timeout.tv_sec = v;
832 parse_retry(char *opt_arg)
837 assert(opt_arg != NULL);
842 v = strtoul(opt_arg, NULL, 10);
844 warnx("Error parsing retries count - %s", strerror(errno));
849 snmp_client.retries = v;
855 parse_version(char *opt_arg)
860 assert(opt_arg != NULL);
865 v = strtoul(opt_arg, NULL, 10);
867 warnx("Error parsing version - %s", strerror(errno));
874 snmp_client.version = SNMP_V1;
877 snmp_client.version = SNMP_V2c;
880 snmp_client.version = SNMP_V3;
883 warnx("Unsupported SNMP version - %u", v);
893 parse_local_path(char *opt_arg)
895 assert(opt_arg != NULL);
897 if (sizeof(opt_arg) > sizeof(SNMP_LOCAL_PATH)) {
898 warnx("Filename too long - %s", opt_arg);
902 strlcpy(snmp_client.local_path, opt_arg, sizeof(SNMP_LOCAL_PATH));
907 parse_buflen(char *opt_arg)
912 assert(opt_arg != NULL);
917 size = strtoul(opt_arg, NULL, 10);
919 warnx("Error parsing buffer size - %s", strerror(errno));
924 if (size > MAX_BUFF_SIZE) {
925 warnx("Buffer size too big - %d max allowed", MAX_BUFF_SIZE);
930 snmp_client.txbuflen = snmp_client.rxbuflen = size;
938 snmp_client.dump_pdus = 1;
943 parse_discovery(struct snmp_toolinfo *snmptoolctx)
945 SET_EDISCOVER(snmptoolctx);
946 snmp_client.version = SNMP_V3;
951 parse_local_key(struct snmp_toolinfo *snmptoolctx)
953 SET_LOCALKEY(snmptoolctx);
954 snmp_client.version = SNMP_V3;
959 parse_num_oids(struct snmp_toolinfo *snmptoolctx)
961 SET_NUMERIC(snmptoolctx);
966 parse_output(struct snmp_toolinfo *snmptoolctx, char *opt_arg)
968 assert(opt_arg != NULL);
970 if (strlen(opt_arg) > strlen("verbose")) {
971 warnx( "Invalid output option - %s",opt_arg);
975 if (strncasecmp(opt_arg, "short", strlen(opt_arg)) == 0)
976 SET_OUTPUT(snmptoolctx, OUTPUT_SHORT);
977 else if (strncasecmp(opt_arg, "verbose", strlen(opt_arg)) == 0)
978 SET_OUTPUT(snmptoolctx, OUTPUT_VERBOSE);
979 else if (strncasecmp(opt_arg,"tabular", strlen(opt_arg)) == 0)
980 SET_OUTPUT(snmptoolctx, OUTPUT_TABULAR);
981 else if (strncasecmp(opt_arg, "quiet", strlen(opt_arg)) == 0)
982 SET_OUTPUT(snmptoolctx, OUTPUT_QUIET);
984 warnx( "Invalid output option - %s", opt_arg);
992 parse_errors(struct snmp_toolinfo *snmptoolctx)
994 SET_RETRY(snmptoolctx);
999 parse_skip_access(struct snmp_toolinfo *snmptoolctx)
1001 SET_ERRIGNORE(snmptoolctx);
1006 snmp_parse_suboid(char *str, struct asn_oid *oid)
1014 if (*str < '0' || *str > '9')
1018 suboid = strtoul(str, &endptr, 10);
1019 if ((asn_subid_t) suboid > ASN_MAXID) {
1020 warnx("Suboid %u > ASN_MAXID", suboid);
1023 if (snmp_suboid_append(oid, suboid) < 0)
1026 } while (*endptr == '.');
1032 snmp_int2asn_oid(char *str, struct asn_oid *oid)
1035 int32_t v, saved_errno;
1037 saved_errno = errno;
1040 v = strtol(str, &endptr, 10);
1042 warnx("Integer value %s not supported - %s", str,
1044 errno = saved_errno;
1047 errno = saved_errno;
1049 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
1055 /* It is a bit weird to have a table indexed by OID but still... */
1057 snmp_oid2asn_oid(struct snmp_toolinfo *snmptoolctx, char *str,
1058 struct asn_oid *oid)
1061 char string[MAXSTR + 1], *endptr;
1062 struct snmp_object obj;
1064 for (i = 0; i < MAXSTR; i++)
1065 if (isalpha (*(str + i)) == 0)
1069 memset(&obj, 0, sizeof(struct snmp_object));
1071 if ((endptr = snmp_parse_suboid(str, &(obj.val.var))) == NULL)
1073 if (snmp_suboid_append(oid, (asn_subid_t) obj.val.var.len) < 0)
1076 strlcpy(string, str, i + 1);
1077 if (snmp_lookup_enumoid(snmptoolctx, &obj, string) < 0) {
1078 warnx("Unknown string - %s", string);
1083 asn_append_oid(oid, &(obj.val.var));
1088 snmp_ip2asn_oid(char *str, struct asn_oid *oid)
1095 for (i = 0; i < 4; i++) {
1096 v = strtoul(ptr, &endptr, 10);
1099 if (*endptr != '.' && strchr("],\0", *endptr) == NULL && i != 3)
1101 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
1109 /* 32-bit counter, gauge, timeticks. */
1111 snmp_uint2asn_oid(char *str, struct asn_oid *oid)
1115 int32_t saved_errno;
1117 saved_errno = errno;
1120 v = strtoul(str, &endptr, 10);
1122 warnx("Integer value %s not supported - %s\n", str,
1124 errno = saved_errno;
1127 errno = saved_errno;
1128 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
1135 snmp_cnt64_2asn_oid(char *str, struct asn_oid *oid)
1139 int32_t saved_errno;
1141 saved_errno = errno;
1144 v = strtoull(str, &endptr, 10);
1147 warnx("Integer value %s not supported - %s", str,
1149 errno = saved_errno;
1152 errno = saved_errno;
1153 if (snmp_suboid_append(oid, (asn_subid_t) (v & 0xffffffff)) < 0)
1156 if (snmp_suboid_append(oid, (asn_subid_t) (v >> 32)) < 0)
1163 parse_syntax(char *str)
1167 for (i = 0; i < SNMP_SYNTAX_UNKNOWN; i++) {
1168 if (strncmp(syntax_strings[i].str, str,
1169 strlen(syntax_strings[i].str)) == 0)
1170 return (syntax_strings[i].stx);
1173 return (SNMP_SYNTAX_NULL);
1177 snmp_parse_subindex(struct snmp_toolinfo *snmptoolctx, char *str,
1178 struct index *idx, struct snmp_object *object)
1182 enum snmp_syntax stx;
1183 char syntax[MAX_CMD_SYNTAX_LEN];
1186 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE) {
1187 for (i = 0; i < MAX_CMD_SYNTAX_LEN ; i++) {
1188 if (*(ptr + i) == ':')
1192 if (i >= MAX_CMD_SYNTAX_LEN) {
1193 warnx("Unknown syntax in OID - %s", str);
1196 /* Expect a syntax string here. */
1197 if ((stx = parse_syntax(str)) <= SNMP_SYNTAX_NULL) {
1198 warnx("Invalid syntax - %s",syntax);
1202 if (stx != idx->syntax && !ISSET_ERRIGNORE(snmptoolctx)) {
1203 warnx("Syntax mismatch - %d expected, %d given",
1208 * That is where the suboid started + the syntax length + one
1209 * character for ':'.
1216 case SNMP_SYNTAX_INTEGER:
1217 return (snmp_int2asn_oid(ptr, &(object->val.var)));
1218 case SNMP_SYNTAX_OID:
1219 return (snmp_oid2asn_oid(snmptoolctx, ptr,
1220 &(object->val.var)));
1221 case SNMP_SYNTAX_IPADDRESS:
1222 return (snmp_ip2asn_oid(ptr, &(object->val.var)));
1223 case SNMP_SYNTAX_COUNTER:
1225 case SNMP_SYNTAX_GAUGE:
1227 case SNMP_SYNTAX_TIMETICKS:
1228 return (snmp_uint2asn_oid(ptr, &(object->val.var)));
1229 case SNMP_SYNTAX_COUNTER64:
1230 return (snmp_cnt64_2asn_oid(ptr, &(object->val.var)));
1231 case SNMP_SYNTAX_OCTETSTRING:
1232 return (snmp_tc2oid(idx->tc, ptr, &(object->val.var)));
1242 snmp_parse_index(struct snmp_toolinfo *snmptoolctx, char *str,
1243 struct snmp_object *object)
1248 if (object->info->table_idx == NULL)
1252 STAILQ_FOREACH(temp, &(OBJECT_IDX_LIST(object)), link) {
1253 if ((ptr = snmp_parse_subindex(snmptoolctx, str, temp, object))
1257 if (*ptr != ',' && *ptr != ']')
1262 if (ptr == NULL || *ptr != ']') {
1263 warnx("Mismatching index - %s", str);
1271 * Fill in the struct asn_oid member of snmp_value with suboids from input.
1272 * If an error occurs - print message on stderr and return (-1).
1273 * If all is ok - return the length of the oid.
1276 snmp_parse_numoid(char *argv, struct asn_oid *var)
1287 if (var->len == ASN_MAXOIDLEN) {
1288 warnx("Oid too long - %u", var->len);
1292 suboid = strtoul(str, &endptr, 10);
1293 if (suboid > ASN_MAXID) {
1294 warnx("Oid too long - %u", var->len);
1298 var->subs[var->len++] = suboid;
1300 } while ( *endptr == '.');
1302 if (*endptr != '\0') {
1303 warnx("Invalid oid string - %s", argv);
1310 /* Append a length 1 suboid to an asn_oid structure. */
1312 snmp_suboid_append(struct asn_oid *var, asn_subid_t suboid)
1317 if (var->len >= ASN_MAXOIDLEN) {
1318 warnx("Oid too long - %u", var->len);
1322 var->subs[var->len++] = suboid;
1327 /* Pop the last suboid from an asn_oid structure. */
1329 snmp_suboid_pop(struct asn_oid *var)
1339 suboid = var->subs[--(var->len)];
1340 var->subs[var->len] = 0;
1346 * Parse the command-line provided string into an OID - alocate memory for a new
1347 * snmp object, fill in its fields and insert it in the object list. A
1348 * (snmp_verify_inoid_f) function must be provided to validate the input string.
1351 snmp_object_add(struct snmp_toolinfo *snmptoolctx, snmp_verify_inoid_f func,
1354 struct snmp_object *obj;
1356 if (snmptoolctx == NULL)
1359 /* XXX-BZ does that chack make sense? */
1360 if (snmptoolctx->objects >= SNMP_MAX_BINDINGS) {
1361 warnx("Too many bindings in PDU - %u", snmptoolctx->objects + 1);
1365 if ((obj = calloc(1, sizeof(struct snmp_object))) == NULL) {
1366 syslog(LOG_ERR, "malloc() failed: %s", strerror(errno));
1370 if (func(snmptoolctx, obj, string) < 0) {
1371 warnx("Invalid OID - %s", string);
1376 snmptoolctx->objects++;
1377 SLIST_INSERT_HEAD(&snmptoolctx->snmp_objectlist, obj, link);
1382 /* Given an OID, find it in the object list and remove it. */
1384 snmp_object_remove(struct snmp_toolinfo *snmptoolctx, struct asn_oid *oid)
1386 struct snmp_object *temp;
1388 if (SLIST_EMPTY(&snmptoolctx->snmp_objectlist)) {
1389 warnx("Object list already empty");
1394 SLIST_FOREACH(temp, &snmptoolctx->snmp_objectlist, link)
1395 if (asn_compare_oid(&(temp->val.var), oid) == 0)
1399 warnx("No such object in list");
1403 SLIST_REMOVE(&snmptoolctx->snmp_objectlist, temp, snmp_object, link);
1404 if (temp->val.syntax == SNMP_SYNTAX_OCTETSTRING &&
1405 temp->val.v.octetstring.octets != NULL)
1406 free(temp->val.v.octetstring.octets);
1413 snmp_object_freeall(struct snmp_toolinfo *snmptoolctx)
1415 struct snmp_object *o;
1417 while ((o = SLIST_FIRST(&snmptoolctx->snmp_objectlist)) != NULL) {
1418 SLIST_REMOVE_HEAD(&snmptoolctx->snmp_objectlist, link);
1420 if (o->val.syntax == SNMP_SYNTAX_OCTETSTRING &&
1421 o->val.v.octetstring.octets != NULL)
1422 free(o->val.v.octetstring.octets);
1427 /* Do all possible memory release before exit. */
1429 snmp_tool_freeall(struct snmp_toolinfo *snmptoolctx)
1431 if (snmp_client.chost != NULL) {
1432 free(snmp_client.chost);
1433 snmp_client.chost = NULL;
1436 if (snmp_client.cport != NULL) {
1437 free(snmp_client.cport);
1438 snmp_client.cport = NULL;
1441 snmp_mapping_free(snmptoolctx);
1442 free_filelist(snmptoolctx);
1443 snmp_object_freeall(snmptoolctx);
1445 if (snmptoolctx->passwd != NULL) {
1446 free(snmptoolctx->passwd);
1447 snmptoolctx->passwd = NULL;
1452 * Fill all variables from the object list into a PDU. (snmp_verify_vbind_f)
1453 * function should check whether the variable is consistent in this PDU
1454 * (e.g do not add non-leaf OIDs to a GET PDU, or OIDs with read access only to
1455 * a SET PDU) - might be NULL though. (snmp_add_vbind_f) function is the
1456 * function actually adds the variable to the PDU and must not be NULL.
1459 snmp_pdu_add_bindings(struct snmp_toolinfo *snmptoolctx,
1460 snmp_verify_vbind_f vfunc, snmp_add_vbind_f afunc,
1461 struct snmp_pdu *pdu, int32_t maxcount)
1463 int32_t nbindings, abind;
1464 struct snmp_object *obj;
1466 if (pdu == NULL || afunc == NULL)
1469 /* Return 0 in case of no more work todo. */
1470 if (SLIST_EMPTY(&snmptoolctx->snmp_objectlist))
1473 if (maxcount < 0 || maxcount > SNMP_MAX_BINDINGS) {
1474 warnx("maxcount out of range: <0 || >SNMP_MAX_BINDINGS");
1479 SLIST_FOREACH(obj, &snmptoolctx->snmp_objectlist, link) {
1480 if ((vfunc != NULL) && (vfunc(snmptoolctx, pdu, obj) < 0)) {
1484 if ((abind = afunc(pdu, obj)) < 0) {
1490 /* Do not put more varbindings than requested. */
1491 if (++nbindings >= maxcount)
1500 * Locate an object in the object list and set a corresponding error status.
1503 snmp_object_seterror(struct snmp_toolinfo *snmptoolctx,
1504 struct snmp_value *err_value, int32_t error_status)
1506 struct snmp_object *obj;
1508 if (SLIST_EMPTY(&snmptoolctx->snmp_objectlist) || err_value == NULL)
1511 SLIST_FOREACH(obj, &snmptoolctx->snmp_objectlist, link)
1512 if (asn_compare_oid(&(err_value->var), &(obj->val.var)) == 0) {
1513 obj->error = error_status;
1521 * Check a PDU received in responce to a SNMP_PDU_GET/SNMP_PDU_GETBULK request
1522 * but don't compare syntaxes - when sending a request PDU they must be null.
1523 * This is a (almost) complete copy of snmp_pdu_check() - with matching syntaxes
1524 * checks and some other checks skiped.
1527 snmp_parse_get_resp(struct snmp_pdu *resp, struct snmp_pdu *req)
1531 for (i = 0; i < req->nbindings; i++) {
1532 if (asn_compare_oid(&req->bindings[i].var,
1533 &resp->bindings[i].var) != 0) {
1534 warnx("Bad OID in response");
1538 if (snmp_client.version != SNMP_V1 && (resp->bindings[i].syntax
1539 == SNMP_SYNTAX_NOSUCHOBJECT || resp->bindings[i].syntax ==
1540 SNMP_SYNTAX_NOSUCHINSTANCE))
1548 snmp_parse_getbulk_resp(struct snmp_pdu *resp, struct snmp_pdu *req)
1552 if (req->error_status > (int32_t) resp->nbindings) {
1553 warnx("Bad number of bindings in response");
1557 for (N = 0; N < req->error_status; N++) {
1558 if (asn_is_suboid(&req->bindings[N].var,
1559 &resp->bindings[N].var) == 0)
1561 if (resp->bindings[N].syntax == SNMP_SYNTAX_ENDOFMIBVIEW)
1565 for (R = N , r = N; R < (int32_t) req->nbindings; R++) {
1566 for (M = 0; M < req->error_index && (r + M) <
1567 (int32_t) resp->nbindings; M++) {
1568 if (asn_is_suboid(&req->bindings[R].var,
1569 &resp->bindings[r + M].var) == 0)
1572 if (resp->bindings[r + M].syntax ==
1573 SNMP_SYNTAX_ENDOFMIBVIEW) {
1585 snmp_parse_getnext_resp(struct snmp_pdu *resp, struct snmp_pdu *req)
1589 for (i = 0; i < req->nbindings; i++) {
1590 if (asn_is_suboid(&req->bindings[i].var, &resp->bindings[i].var)
1594 if (resp->version != SNMP_V1 && resp->bindings[i].syntax ==
1595 SNMP_SYNTAX_ENDOFMIBVIEW)
1603 * Should be called to check a responce to get/getnext/getbulk.
1606 snmp_parse_resp(struct snmp_pdu *resp, struct snmp_pdu *req)
1608 if (resp == NULL || req == NULL)
1611 if (resp->version != req->version) {
1612 warnx("Response has wrong version");
1616 if (resp->error_status == SNMP_ERR_NOSUCHNAME) {
1617 warnx("Error - No Such Name");
1621 if (resp->error_status != SNMP_ERR_NOERROR) {
1622 warnx("Error %d in responce", resp->error_status);
1626 if (resp->nbindings != req->nbindings && req->type != SNMP_PDU_GETBULK){
1627 warnx("Bad number of bindings in response");
1631 switch (req->type) {
1633 return (snmp_parse_get_resp(resp,req));
1634 case SNMP_PDU_GETBULK:
1635 return (snmp_parse_getbulk_resp(resp,req));
1636 case SNMP_PDU_GETNEXT:
1637 return (snmp_parse_getnext_resp(resp,req));
1647 snmp_output_octetstring(struct snmp_toolinfo *snmptoolctx, enum snmp_tc tc,
1648 uint32_t len, uint8_t *octets)
1652 if (len == 0 || octets == NULL)
1655 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1656 fprintf(stdout, "%s : ",
1657 syntax_strings[SNMP_SYNTAX_OCTETSTRING].str);
1659 if ((buf = snmp_oct2tc(tc, len, (char *) octets)) != NULL) {
1660 fprintf(stdout, "%s", buf);
1666 snmp_output_octetindex(struct snmp_toolinfo *snmptoolctx, enum snmp_tc tc,
1667 struct asn_oid *oid)
1672 if ((s = malloc(oid->subs[0] + 1)) == NULL)
1673 syslog(LOG_ERR, "malloc failed - %s", strerror(errno));
1675 for (i = 0; i < oid->subs[0]; i++)
1676 s[i] = (u_char) (oid->subs[i + 1]);
1678 snmp_output_octetstring(snmptoolctx, tc, oid->subs[0], s);
1684 * Check and output syntax type and value.
1687 snmp_output_oid_value(struct snmp_toolinfo *snmptoolctx, struct asn_oid *oid)
1689 char oid_string[ASN_OIDSTRLEN];
1690 struct snmp_object obj;
1692 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1693 fprintf(stdout, "%s : ", syntax_strings[SNMP_SYNTAX_OID].str);
1695 if(!ISSET_NUMERIC(snmptoolctx)) {
1696 memset(&obj, 0, sizeof(struct snmp_object));
1697 asn_append_oid(&(obj.val.var), oid);
1699 if (snmp_lookup_enumstring(snmptoolctx, &obj) > 0)
1700 fprintf(stdout, "%s" , obj.info->string);
1701 else if (snmp_lookup_oidstring(snmptoolctx, &obj) > 0)
1702 fprintf(stdout, "%s" , obj.info->string);
1703 else if (snmp_lookup_nodestring(snmptoolctx, &obj) > 0)
1704 fprintf(stdout, "%s" , obj.info->string);
1706 (void) asn_oid2str_r(oid, oid_string);
1707 fprintf(stdout, "%s", oid_string);
1710 (void) asn_oid2str_r(oid, oid_string);
1711 fprintf(stdout, "%s", oid_string);
1716 snmp_output_int(struct snmp_toolinfo *snmptoolctx, struct enum_pairs *enums,
1721 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1722 fprintf(stdout, "%s : ",
1723 syntax_strings[SNMP_SYNTAX_INTEGER].str);
1725 if (enums != NULL && (string = enum_string_lookup(enums, int_val))
1727 fprintf(stdout, "%s", string);
1729 fprintf(stdout, "%d", int_val);
1733 snmp_output_ipaddress(struct snmp_toolinfo *snmptoolctx, uint8_t *ip)
1735 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1736 fprintf(stdout, "%s : ",
1737 syntax_strings[SNMP_SYNTAX_IPADDRESS].str);
1739 fprintf(stdout, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
1743 snmp_output_counter(struct snmp_toolinfo *snmptoolctx, uint32_t counter)
1745 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1746 fprintf(stdout, "%s : ",
1747 syntax_strings[SNMP_SYNTAX_COUNTER].str);
1749 fprintf(stdout, "%u", counter);
1753 snmp_output_gauge(struct snmp_toolinfo *snmptoolctx, uint32_t gauge)
1755 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1756 fprintf(stdout, "%s : ", syntax_strings[SNMP_SYNTAX_GAUGE].str);
1758 fprintf(stdout, "%u", gauge);
1762 snmp_output_ticks(struct snmp_toolinfo *snmptoolctx, uint32_t ticks)
1764 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1765 fprintf(stdout, "%s : ",
1766 syntax_strings[SNMP_SYNTAX_TIMETICKS].str);
1768 fprintf(stdout, "%u", ticks);
1772 snmp_output_counter64(struct snmp_toolinfo *snmptoolctx, uint64_t counter64)
1774 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1775 fprintf(stdout, "%s : ",
1776 syntax_strings[SNMP_SYNTAX_COUNTER64].str);
1778 fprintf(stdout,"%ju", counter64);
1782 snmp_output_numval(struct snmp_toolinfo *snmptoolctx, struct snmp_value *val,
1783 struct snmp_oid2str *entry)
1788 if (GET_OUTPUT(snmptoolctx) != OUTPUT_QUIET)
1789 fprintf(stdout, " = ");
1791 switch (val->syntax) {
1792 case SNMP_SYNTAX_INTEGER:
1794 snmp_output_int(snmptoolctx, entry->snmp_enum,
1797 snmp_output_int(snmptoolctx, NULL, val->v.integer);
1800 case SNMP_SYNTAX_OCTETSTRING:
1802 snmp_output_octetstring(snmptoolctx, entry->tc,
1803 val->v.octetstring.len, val->v.octetstring.octets);
1805 snmp_output_octetstring(snmptoolctx, SNMP_STRING,
1806 val->v.octetstring.len, val->v.octetstring.octets);
1809 case SNMP_SYNTAX_OID:
1810 snmp_output_oid_value(snmptoolctx, &(val->v.oid));
1813 case SNMP_SYNTAX_IPADDRESS:
1814 snmp_output_ipaddress(snmptoolctx, val->v.ipaddress);
1817 case SNMP_SYNTAX_COUNTER:
1818 snmp_output_counter(snmptoolctx, val->v.uint32);
1821 case SNMP_SYNTAX_GAUGE:
1822 snmp_output_gauge(snmptoolctx, val->v.uint32);
1825 case SNMP_SYNTAX_TIMETICKS:
1826 snmp_output_ticks(snmptoolctx, val->v.uint32);
1829 case SNMP_SYNTAX_COUNTER64:
1830 snmp_output_counter64(snmptoolctx, val->v.counter64);
1833 case SNMP_SYNTAX_NOSUCHOBJECT:
1834 fprintf(stdout, "No Such Object\n");
1835 return (val->syntax);
1837 case SNMP_SYNTAX_NOSUCHINSTANCE:
1838 fprintf(stdout, "No Such Instance\n");
1839 return (val->syntax);
1841 case SNMP_SYNTAX_ENDOFMIBVIEW:
1842 fprintf(stdout, "End of Mib View\n");
1843 return (val->syntax);
1845 case SNMP_SYNTAX_NULL:
1847 fprintf(stdout, "agent returned NULL Syntax\n");
1848 return (val->syntax);
1851 /* NOTREACHED - If here - then all went completely wrong. */
1852 fprintf(stdout, "agent returned unknown syntax\n");
1856 fprintf(stdout, "\n");
1862 snmp_fill_object(struct snmp_toolinfo *snmptoolctx, struct snmp_object *obj,
1863 struct snmp_value *val)
1868 if (obj == NULL || val == NULL)
1871 if ((suboid = snmp_suboid_pop(&(val->var))) > ASN_MAXID)
1874 memset(obj, 0, sizeof(struct snmp_object));
1875 asn_append_oid(&(obj->val.var), &(val->var));
1876 obj->val.syntax = val->syntax;
1878 if (obj->val.syntax > 0)
1879 rc = snmp_lookup_leafstring(snmptoolctx, obj);
1881 rc = snmp_lookup_nonleaf_string(snmptoolctx, obj);
1883 (void) snmp_suboid_append(&(val->var), suboid);
1884 (void) snmp_suboid_append(&(obj->val.var), suboid);
1890 snmp_output_index(struct snmp_toolinfo *snmptoolctx, struct index *stx,
1891 struct asn_oid *oid)
1896 struct asn_oid temp, out;
1898 if (oid->len < bytes)
1901 memset(&temp, 0, sizeof(struct asn_oid));
1902 asn_append_oid(&temp, oid);
1904 switch (stx->syntax) {
1905 case SNMP_SYNTAX_INTEGER:
1906 snmp_output_int(snmptoolctx, stx->snmp_enum, temp.subs[0]);
1909 case SNMP_SYNTAX_OCTETSTRING:
1910 if ((temp.subs[0] > temp.len -1 ) || (temp.subs[0] >
1911 ASN_MAXOCTETSTRING))
1913 snmp_output_octetindex(snmptoolctx, stx->tc, &temp);
1914 bytes += temp.subs[0];
1917 case SNMP_SYNTAX_OID:
1918 if ((temp.subs[0] > temp.len -1) || (temp.subs[0] >
1922 bytes += temp.subs[0];
1923 memset(&out, 0, sizeof(struct asn_oid));
1924 asn_slice_oid(&out, &temp, 1, bytes);
1925 snmp_output_oid_value(snmptoolctx, &out);
1928 case SNMP_SYNTAX_IPADDRESS:
1931 for (bytes = 0; bytes < 4; bytes++)
1932 ip[bytes] = temp.subs[bytes];
1934 snmp_output_ipaddress(snmptoolctx, ip);
1938 case SNMP_SYNTAX_COUNTER:
1939 snmp_output_counter(snmptoolctx, temp.subs[0]);
1942 case SNMP_SYNTAX_GAUGE:
1943 snmp_output_gauge(snmptoolctx, temp.subs[0]);
1946 case SNMP_SYNTAX_TIMETICKS:
1947 snmp_output_ticks(snmptoolctx, temp.subs[0]);
1950 case SNMP_SYNTAX_COUNTER64:
1954 memcpy(&cnt64, temp.subs, bytes);
1955 snmp_output_counter64(snmptoolctx, cnt64);
1966 snmp_output_object(struct snmp_toolinfo *snmptoolctx, struct snmp_object *o)
1968 int32_t i, first, len;
1972 if (ISSET_NUMERIC(snmptoolctx))
1975 if (o->info->table_idx == NULL) {
1976 fprintf(stdout,"%s.%d", o->info->string,
1977 o->val.var.subs[o->val.var.len - 1]);
1981 fprintf(stdout,"%s[", o->info->string);
1982 memset(&oid, 0, sizeof(struct asn_oid));
1985 asn_slice_oid(&oid, &(o->val.var), (o->info->table_idx->var.len + len),
1989 STAILQ_FOREACH(temp, &(OBJECT_IDX_LIST(o)), link) {
1993 fprintf(stdout, ", ");
1994 if ((i = snmp_output_index(snmptoolctx, temp, &oid)) < 0)
1997 memset(&oid, 0, sizeof(struct asn_oid));
1998 asn_slice_oid(&oid, &(o->val.var),
1999 (o->info->table_idx->var.len + len), o->val.var.len + 1);
2002 fprintf(stdout,"]");
2007 snmp_output_err_resp(struct snmp_toolinfo *snmptoolctx, struct snmp_pdu *pdu)
2009 char buf[ASN_OIDSTRLEN];
2010 struct snmp_object object;
2012 if (pdu == NULL || (pdu->error_index > (int32_t) pdu->nbindings)) {
2013 fprintf(stdout,"Invalid error index in PDU\n");
2017 fprintf(stdout, "Agent %s:%s returned error \n", snmp_client.chost,
2020 if (!ISSET_NUMERIC(snmptoolctx) && (snmp_fill_object(snmptoolctx, &object,
2021 &(pdu->bindings[pdu->error_index - 1])) > 0))
2022 snmp_output_object(snmptoolctx, &object);
2024 asn_oid2str_r(&(pdu->bindings[pdu->error_index - 1].var), buf);
2025 fprintf(stdout,"%s", buf);
2028 fprintf(stdout," caused error - ");
2029 if ((pdu->error_status > 0) && (pdu->error_status <=
2030 SNMP_ERR_INCONS_NAME))
2031 fprintf(stdout, "%s\n", error_strings[pdu->error_status].str);
2033 fprintf(stdout,"%s\n", error_strings[SNMP_ERR_UNKNOWN].str);
2037 snmp_output_resp(struct snmp_toolinfo *snmptoolctx, struct snmp_pdu *pdu)
2040 char p[ASN_OIDSTRLEN];
2042 struct snmp_object object;
2044 for (i = 0, error = 0; i < pdu->nbindings; i++) {
2045 if (GET_OUTPUT(snmptoolctx) != OUTPUT_QUIET) {
2046 if (!ISSET_NUMERIC(snmptoolctx) &&
2047 (snmp_fill_object(snmptoolctx, &object,
2048 &(pdu->bindings[i])) > 0))
2049 snmp_output_object(snmptoolctx, &object);
2051 asn_oid2str_r(&(pdu->bindings[i].var), p);
2052 fprintf(stdout, "%s", p);
2055 error |= snmp_output_numval(snmptoolctx, &(pdu->bindings[i]), object.info);
2062 snmp_output_engine(void)
2065 char *cptr, engine[2 * SNMP_ENGINE_ID_SIZ + 2];
2068 for (i = 0; i < snmp_client.engine.engine_len; i++)
2069 cptr += sprintf(cptr, "%.2x", snmp_client.engine.engine_id[i]);
2072 fprintf(stdout, "Engine ID 0x%s\n", engine);
2073 fprintf(stdout, "Boots : %u\t\tTime : %d\n",
2074 snmp_client.engine.engine_boots,
2075 snmp_client.engine.engine_time);
2079 snmp_output_keys(void)
2081 uint32_t i, keylen = 0;
2082 char *cptr, extkey[2 * SNMP_AUTH_KEY_SIZ + 2];
2084 fprintf(stdout, "Localized keys for %s\n", snmp_client.user.sec_name);
2085 if (snmp_client.user.auth_proto == SNMP_AUTH_HMAC_MD5) {
2086 fprintf(stdout, "MD5 : 0x");
2087 keylen = SNMP_AUTH_HMACMD5_KEY_SIZ;
2088 } else if (snmp_client.user.auth_proto == SNMP_AUTH_HMAC_SHA) {
2089 fprintf(stdout, "SHA : 0x");
2090 keylen = SNMP_AUTH_HMACSHA_KEY_SIZ;
2092 if (snmp_client.user.auth_proto != SNMP_AUTH_NOAUTH) {
2094 for (i = 0; i < keylen; i++)
2095 cptr += sprintf(cptr, "%.2x",
2096 snmp_client.user.auth_key[i]);
2098 fprintf(stdout, "%s\n", extkey);
2101 if (snmp_client.user.priv_proto == SNMP_PRIV_DES) {
2102 fprintf(stdout, "DES : 0x");
2103 keylen = SNMP_PRIV_DES_KEY_SIZ;
2104 } else if (snmp_client.user.priv_proto == SNMP_PRIV_AES) {
2105 fprintf(stdout, "AES : 0x");
2106 keylen = SNMP_PRIV_AES_KEY_SIZ;
2108 if (snmp_client.user.priv_proto != SNMP_PRIV_NOPRIV) {
2110 for (i = 0; i < keylen; i++)
2111 cptr += sprintf(cptr, "%.2x",
2112 snmp_client.user.priv_key[i]);
2114 fprintf(stdout, "%s\n", extkey);