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 = malloc(strlen(filename) + 1)) == NULL) {
255 warnx("malloc() failed - %s", strerror(errno));
259 if ((entry = malloc(sizeof(struct fname))) == NULL) {
260 warnx("malloc() failed - %s", strerror(errno));
265 memset(entry, 0, sizeof(struct fname));
268 asn_append_oid(&(entry->cut), cut);
269 strlcpy(fstring, filename, strlen(filename) + 1);
270 entry->name = fstring;
272 SLIST_INSERT_HEAD(&snmptoolctx->filelist, entry, link);
278 free_filelist(struct snmp_toolinfo *snmptoolctx)
282 if (snmptoolctx == NULL)
283 return; /* XXX error handling */
285 while ((f = SLIST_FIRST(&snmptoolctx->filelist)) != NULL) {
286 SLIST_REMOVE_HEAD(&snmptoolctx->filelist, link);
294 isvalid_fchar(char c, int pos)
296 if (isalpha(c)|| c == '/'|| c == '_' || c == '.' || c == '~' ||
297 (pos != 0 && isdigit(c))){
304 if (!isascii(c) || !isprint(c))
305 warnx("Unexpected character %#2x", (u_int) c);
307 warnx("Illegal character '%c'", c);
313 * Re-implement getsubopt from scratch, because the second argument is broken
314 * and will not compile with WARNS=5.
315 * Copied from src/contrib/bsnmp/snmpd/main.c.
318 getsubopt1(char **arg, const char *const *options, char **valp, char **optp)
320 static const char *const delim = ",\t ";
326 /* Skip leading junk. */
327 for (ptr = *arg; *ptr != '\0'; ptr++)
328 if (strchr(delim, *ptr) == NULL)
336 /* Find the end of the option. */
337 while (*++ptr != '\0')
338 if (strchr(delim, *ptr) != NULL || *ptr == '=')
345 while (*ptr != '\0' && strchr(delim, *ptr) == NULL)
355 for (i = 0; *options != NULL; options++, i++)
356 if (strcmp(*optp, *options) == 0)
362 parse_path(char *value)
369 for (len = 0; len < MAXPATHLEN; len++) {
370 i = isvalid_fchar(*(value + len), len) ;
378 if (len >= MAXPATHLEN || value[len] != '\0') {
379 warnx("Bad pathname - '%s'", value);
387 parse_flist(struct snmp_toolinfo *snmptoolctx, char *value, char *path,
388 const struct asn_oid *cut)
391 char filename[MAXPATHLEN + 1];
397 memset(filename, 0, MAXPATHLEN + 1);
399 if (isalpha(*value) && (path == NULL || path[0] == '\0')) {
400 strlcpy(filename, SNMP_DEFS_DIR, MAXPATHLEN + 1);
401 namelen = strlen(SNMP_DEFS_DIR);
402 } else if (path != NULL){
403 strlcpy(filename, path, MAXPATHLEN + 1);
404 namelen = strlen(path);
408 for ( ; namelen < MAXPATHLEN; value++) {
409 if (isvalid_fchar(*value, namelen) > 0) {
410 filename[namelen++] = *value;
416 else if (*value == '\0')
419 if (!isascii(*value) || !isprint(*value))
420 warnx("Unexpected character %#2x in"
421 " filename", (u_int) *value);
423 warnx("Illegal character '%c' in"
424 " filename", *value);
428 filename[namelen]='\0';
432 if ((namelen == MAXPATHLEN) && (filename[MAXPATHLEN] != '\0')) {
433 warnx("Filename %s too long", filename);
437 if (add_filename(snmptoolctx, filename, cut, 0) < 0) {
438 warnx("Error adding file %s to list", filename);
441 } while (*value != '\0');
447 parse_ascii(char *ascii, uint8_t *binstr, size_t binlen)
449 int32_t alen, count, saved_errno, i;
453 /* Filter 0x at the beginning */
454 if ((alen = strlen(ascii)) > 2 && ascii[0] == '0' && ascii[1] == 'x')
461 for (count = 0; i < alen; i += 2) {
462 /* XXX: consider strlen(ascii) % 2 != 0 */
464 dptr[1] = ascii[i + 1];
466 if ((val = strtoul(dptr, NULL, 16)) > 0xFF || errno != 0) {
470 binstr[count] = (uint8_t) val;
471 if (++count >= binlen) {
472 warnx("Key %s too long - truncating to %zu octets",
482 * Functions to parse common input options for client tools and fill in the
483 * snmp_client structure.
486 parse_authentication(struct snmp_toolinfo *snmptoolctx, char *opt_arg)
488 int32_t count, subopt;
490 const char *const subopts[] = {
496 assert(opt_arg != NULL);
498 while ((subopt = getsubopt1(&opt_arg, subopts, &val, &option)) != EOF) {
502 warnx("Suboption 'proto' requires an argument");
505 if (strlen(val) != 3) {
506 warnx("Unknown auth protocol - %s", val);
509 if (strncasecmp("md5", val, strlen("md5")) == 0)
510 snmp_client.user.auth_proto =
512 else if (strncasecmp("sha", val, strlen("sha")) == 0)
513 snmp_client.user.auth_proto =
516 warnx("Unknown auth protocol - %s", val);
522 warnx("Suboption 'key' requires an argument");
525 if (parse_ascii(val, snmp_client.user.auth_key,
526 SNMP_AUTH_KEY_SIZ) < 0) {
527 warnx("Bad authentication key- %s", val);
532 warnx("Unknown suboption - '%s'", suboptarg);
537 return (2/* count */);
541 parse_privacy(struct snmp_toolinfo *snmptoolctx, char *opt_arg)
543 int32_t count, subopt;
545 const char *const subopts[] = {
551 assert(opt_arg != NULL);
553 while ((subopt = getsubopt1(&opt_arg, subopts, &val, &option)) != EOF) {
557 warnx("Suboption 'proto' requires an argument");
560 if (strlen(val) != 3) {
561 warnx("Unknown privacy protocol - %s", val);
564 if (strncasecmp("aes", val, strlen("aes")) == 0)
565 snmp_client.user.priv_proto = SNMP_PRIV_AES;
566 else if (strncasecmp("des", val, strlen("des")) == 0)
567 snmp_client.user.priv_proto = SNMP_PRIV_DES;
569 warnx("Unknown privacy protocol - %s", val);
575 warnx("Suboption 'key' requires an argument");
578 if (parse_ascii(val, snmp_client.user.priv_key,
579 SNMP_PRIV_KEY_SIZ) < 0) {
580 warnx("Bad privacy key- %s", val);
585 warnx("Unknown suboption - '%s'", suboptarg);
590 return (2/* count */);
594 parse_context(struct snmp_toolinfo *snmptoolctx, char *opt_arg)
596 int32_t count, subopt;
598 const char *const subopts[] = {
604 assert(opt_arg != NULL);
606 while ((subopt = getsubopt1(&opt_arg, subopts, &val, &option)) != EOF) {
610 warnx("Suboption 'context' - no argument");
613 strlcpy(snmp_client.cname, val, SNMP_CONTEXT_NAME_SIZ);
617 warnx("Suboption 'context-engine' - no argument");
620 if ((snmp_client.clen = parse_ascii(val,
621 snmp_client.cengine, SNMP_ENGINE_ID_SIZ)) < 0) {
622 warnx("Bad EngineID - %s", val);
627 warnx("Unknown suboption - '%s'", suboptarg);
632 return (2/* count */);
636 parse_user_security(struct snmp_toolinfo *snmptoolctx, char *opt_arg)
638 int32_t count, subopt, saved_errno;
640 const char *const subopts[] = {
648 assert(opt_arg != NULL);
650 while ((subopt = getsubopt1(&opt_arg, subopts, &val, &option)) != EOF) {
654 warnx("Suboption 'engine' - no argument");
657 snmp_client.engine.engine_len = parse_ascii(val,
658 snmp_client.engine.engine_id, SNMP_ENGINE_ID_SIZ);
659 if (snmp_client.engine.engine_len < 0) {
660 warnx("Bad EngineID - %s", val);
666 warnx("Suboption 'engine-boots' - no argument");
671 snmp_client.engine.engine_boots = strtoul(val, NULL, 10);
673 warnx("Bad 'engine-boots' value %s - %s", val,
682 warnx("Suboption 'engine-time' - no argument");
687 snmp_client.engine.engine_time = strtoul(val, NULL, 10);
689 warnx("Bad 'engine-time' value %s - %s", val,
697 strlcpy(snmp_client.user.sec_name, val,
701 warnx("Unknown suboption - '%s'", suboptarg);
706 return (2/* count */);
710 parse_file(struct snmp_toolinfo *snmptoolctx, char *opt_arg)
712 assert(opt_arg != NULL);
714 if (parse_flist(snmptoolctx, opt_arg, NULL, &IsoOrgDod_OID) < 0)
721 parse_include(struct snmp_toolinfo *snmptoolctx, char *opt_arg)
723 char path[MAXPATHLEN + 1];
724 int32_t cut_dflt, len, subopt;
727 const char *const subopts[] = {
738 assert(opt_arg != NULL);
741 free_filelist(snmptoolctx, ); */
743 * This function should be called only after getopt(3) - otherwise if
744 * no previous validation of opt_arg strlen() may not return what is
749 memset(&cut, 0, sizeof(struct asn_oid));
752 while ((subopt = getsubopt1(&opt_arg, subopts, &val, &option)) != EOF) {
756 warnx("Suboption 'cut' requires an argument");
759 if (snmp_parse_numoid(val, &cut) < 0)
766 if ((len = parse_path(val)) < 0)
768 strlcpy(path, val, len + 1);
775 len = parse_flist(snmptoolctx, val, path, &IsoOrgDod_OID);
777 len = parse_flist(snmptoolctx, val, path, &cut);
783 warnx("Unknown suboption - '%s'", suboptarg);
788 /* XXX: Fix me - returning two is wrong here */
793 parse_server(char *opt_arg)
795 assert(opt_arg != NULL);
797 if (snmp_parse_server(&snmp_client, opt_arg) < 0)
800 if (snmp_client.trans > SNMP_TRANS_UDP && snmp_client.chost == NULL) {
801 if ((snmp_client.chost = malloc(strlen(SNMP_DEFAULT_LOCAL + 1)))
803 syslog(LOG_ERR, "malloc() failed: %s", strerror(errno));
806 strcpy(snmp_client.chost, SNMP_DEFAULT_LOCAL);
813 parse_timeout(char *opt_arg)
815 int32_t v, saved_errno;
817 assert(opt_arg != NULL);
822 v = strtol(opt_arg, NULL, 10);
824 warnx( "Error parsing timeout value - %s", strerror(errno));
829 snmp_client.timeout.tv_sec = v;
835 parse_retry(char *opt_arg)
840 assert(opt_arg != NULL);
845 v = strtoul(opt_arg, NULL, 10);
847 warnx("Error parsing retries count - %s", strerror(errno));
852 snmp_client.retries = v;
858 parse_version(char *opt_arg)
863 assert(opt_arg != NULL);
868 v = strtoul(opt_arg, NULL, 10);
870 warnx("Error parsing version - %s", strerror(errno));
877 snmp_client.version = SNMP_V1;
880 snmp_client.version = SNMP_V2c;
883 snmp_client.version = SNMP_V3;
886 warnx("Unsupported SNMP version - %u", v);
896 parse_local_path(char *opt_arg)
898 assert(opt_arg != NULL);
900 if (sizeof(opt_arg) > sizeof(SNMP_LOCAL_PATH)) {
901 warnx("Filename too long - %s", opt_arg);
905 strlcpy(snmp_client.local_path, opt_arg, sizeof(SNMP_LOCAL_PATH));
910 parse_buflen(char *opt_arg)
915 assert(opt_arg != NULL);
920 size = strtoul(opt_arg, NULL, 10);
922 warnx("Error parsing buffer size - %s", strerror(errno));
927 if (size > MAX_BUFF_SIZE) {
928 warnx("Buffer size too big - %d max allowed", MAX_BUFF_SIZE);
933 snmp_client.txbuflen = snmp_client.rxbuflen = size;
941 snmp_client.dump_pdus = 1;
946 parse_discovery(struct snmp_toolinfo *snmptoolctx)
948 SET_EDISCOVER(snmptoolctx);
949 snmp_client.version = SNMP_V3;
954 parse_local_key(struct snmp_toolinfo *snmptoolctx)
956 SET_LOCALKEY(snmptoolctx);
957 snmp_client.version = SNMP_V3;
962 parse_num_oids(struct snmp_toolinfo *snmptoolctx)
964 SET_NUMERIC(snmptoolctx);
969 parse_output(struct snmp_toolinfo *snmptoolctx, char *opt_arg)
971 assert(opt_arg != NULL);
973 if (strlen(opt_arg) > strlen("verbose")) {
974 warnx( "Invalid output option - %s",opt_arg);
978 if (strncasecmp(opt_arg, "short", strlen(opt_arg)) == 0)
979 SET_OUTPUT(snmptoolctx, OUTPUT_SHORT);
980 else if (strncasecmp(opt_arg, "verbose", strlen(opt_arg)) == 0)
981 SET_OUTPUT(snmptoolctx, OUTPUT_VERBOSE);
982 else if (strncasecmp(opt_arg,"tabular", strlen(opt_arg)) == 0)
983 SET_OUTPUT(snmptoolctx, OUTPUT_TABULAR);
984 else if (strncasecmp(opt_arg, "quiet", strlen(opt_arg)) == 0)
985 SET_OUTPUT(snmptoolctx, OUTPUT_QUIET);
987 warnx( "Invalid output option - %s", opt_arg);
995 parse_errors(struct snmp_toolinfo *snmptoolctx)
997 SET_RETRY(snmptoolctx);
1002 parse_skip_access(struct snmp_toolinfo *snmptoolctx)
1004 SET_ERRIGNORE(snmptoolctx);
1009 snmp_parse_suboid(char *str, struct asn_oid *oid)
1017 if (*str < '0' || *str > '9')
1021 suboid = strtoul(str, &endptr, 10);
1022 if ((asn_subid_t) suboid > ASN_MAXID) {
1023 warnx("Suboid %u > ASN_MAXID", suboid);
1026 if (snmp_suboid_append(oid, suboid) < 0)
1029 } while (*endptr == '.');
1035 snmp_int2asn_oid(char *str, struct asn_oid *oid)
1038 int32_t v, saved_errno;
1040 saved_errno = errno;
1043 v = strtol(str, &endptr, 10);
1045 warnx("Integer value %s not supported - %s", str,
1047 errno = saved_errno;
1050 errno = saved_errno;
1052 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
1058 /* It is a bit weird to have a table indexed by OID but still... */
1060 snmp_oid2asn_oid(struct snmp_toolinfo *snmptoolctx, char *str,
1061 struct asn_oid *oid)
1064 char string[MAXSTR], *endptr;
1065 struct snmp_object obj;
1067 for (i = 0; i < MAXSTR; i++)
1068 if (isalpha (*(str + i)) == 0)
1072 memset(&obj, 0, sizeof(struct snmp_object));
1074 if ((endptr = snmp_parse_suboid(str, &(obj.val.var))) == NULL)
1076 if (snmp_suboid_append(oid, (asn_subid_t) obj.val.var.len) < 0)
1079 strlcpy(string, str, i + 1);
1081 if (snmp_lookup_enumoid(snmptoolctx, &obj, string) < 0) {
1082 warnx("Unknown string - %s",string);
1088 asn_append_oid(oid, &(obj.val.var));
1093 snmp_ip2asn_oid(char *str, struct asn_oid *oid)
1100 for (i = 0; i < 4; i++) {
1101 v = strtoul(ptr, &endptr, 10);
1104 if (*endptr != '.' && strchr("],\0", *endptr) == NULL && i != 3)
1106 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
1114 /* 32-bit counter, gauge, timeticks. */
1116 snmp_uint2asn_oid(char *str, struct asn_oid *oid)
1120 int32_t saved_errno;
1122 saved_errno = errno;
1125 v = strtoul(str, &endptr, 10);
1127 warnx("Integer value %s not supported - %s\n", str,
1129 errno = saved_errno;
1132 errno = saved_errno;
1133 if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
1140 snmp_cnt64_2asn_oid(char *str, struct asn_oid *oid)
1144 int32_t saved_errno;
1146 saved_errno = errno;
1149 v = strtoull(str, &endptr, 10);
1152 warnx("Integer value %s not supported - %s", str,
1154 errno = saved_errno;
1157 errno = saved_errno;
1158 if (snmp_suboid_append(oid, (asn_subid_t) (v & 0xffffffff)) < 0)
1161 if (snmp_suboid_append(oid, (asn_subid_t) (v >> 32)) < 0)
1168 parse_syntax(char *str)
1172 for (i = 0; i < SNMP_SYNTAX_UNKNOWN; i++) {
1173 if (strncmp(syntax_strings[i].str, str,
1174 strlen(syntax_strings[i].str)) == 0)
1175 return (syntax_strings[i].stx);
1178 return (SNMP_SYNTAX_NULL);
1182 snmp_parse_subindex(struct snmp_toolinfo *snmptoolctx, char *str,
1183 struct index *idx, struct snmp_object *object)
1187 enum snmp_syntax stx;
1188 char syntax[MAX_CMD_SYNTAX_LEN];
1191 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE) {
1192 for (i = 0; i < MAX_CMD_SYNTAX_LEN ; i++) {
1193 if (*(ptr + i) == ':')
1197 if (i >= MAX_CMD_SYNTAX_LEN) {
1198 warnx("Unknown syntax in OID - %s", str);
1201 /* Expect a syntax string here. */
1202 if ((stx = parse_syntax(str)) <= SNMP_SYNTAX_NULL) {
1203 warnx("Invalid syntax - %s",syntax);
1207 if (stx != idx->syntax && !ISSET_ERRIGNORE(snmptoolctx)) {
1208 warnx("Syntax mismatch - %d expected, %d given",
1213 * That is where the suboid started + the syntax length + one
1214 * character for ':'.
1221 case SNMP_SYNTAX_INTEGER:
1222 return (snmp_int2asn_oid(ptr, &(object->val.var)));
1223 case SNMP_SYNTAX_OID:
1224 return (snmp_oid2asn_oid(snmptoolctx, ptr,
1225 &(object->val.var)));
1226 case SNMP_SYNTAX_IPADDRESS:
1227 return (snmp_ip2asn_oid(ptr, &(object->val.var)));
1228 case SNMP_SYNTAX_COUNTER:
1230 case SNMP_SYNTAX_GAUGE:
1232 case SNMP_SYNTAX_TIMETICKS:
1233 return (snmp_uint2asn_oid(ptr, &(object->val.var)));
1234 case SNMP_SYNTAX_COUNTER64:
1235 return (snmp_cnt64_2asn_oid(ptr, &(object->val.var)));
1236 case SNMP_SYNTAX_OCTETSTRING:
1237 return (snmp_tc2oid(idx->tc, ptr, &(object->val.var)));
1247 snmp_parse_index(struct snmp_toolinfo *snmptoolctx, char *str,
1248 struct snmp_object *object)
1253 if (object->info->table_idx == NULL)
1257 STAILQ_FOREACH(temp, &(OBJECT_IDX_LIST(object)), link) {
1258 if ((ptr = snmp_parse_subindex(snmptoolctx, str, temp, object))
1262 if (*ptr != ',' && *ptr != ']')
1267 if (ptr == NULL || *ptr != ']') {
1268 warnx("Mismatching index - %s", str);
1276 * Fill in the struct asn_oid member of snmp_value with suboids from input.
1277 * If an error occurs - print message on stderr and return (-1).
1278 * If all is ok - return the length of the oid.
1281 snmp_parse_numoid(char *argv, struct asn_oid *var)
1292 if (var->len == ASN_MAXOIDLEN) {
1293 warnx("Oid too long - %u", var->len);
1297 suboid = strtoul(str, &endptr, 10);
1298 if (suboid > ASN_MAXID) {
1299 warnx("Oid too long - %u", var->len);
1303 var->subs[var->len++] = suboid;
1305 } while ( *endptr == '.');
1307 if (*endptr != '\0') {
1308 warnx("Invalid oid string - %s", argv);
1315 /* Append a length 1 suboid to an asn_oid structure. */
1317 snmp_suboid_append(struct asn_oid *var, asn_subid_t suboid)
1322 if (var->len >= ASN_MAXOIDLEN) {
1323 warnx("Oid too long - %u", var->len);
1327 var->subs[var->len++] = suboid;
1332 /* Pop the last suboid from an asn_oid structure. */
1334 snmp_suboid_pop(struct asn_oid *var)
1344 suboid = var->subs[--(var->len)];
1345 var->subs[var->len] = 0;
1351 * Parse the command-line provided string into an OID - alocate memory for a new
1352 * snmp object, fill in its fields and insert it in the object list. A
1353 * (snmp_verify_inoid_f) function must be provided to validate the input string.
1356 snmp_object_add(struct snmp_toolinfo *snmptoolctx, snmp_verify_inoid_f func,
1359 struct snmp_object *obj;
1361 if (snmptoolctx == NULL)
1364 /* XXX-BZ does that chack make sense? */
1365 if (snmptoolctx->objects >= SNMP_MAX_BINDINGS) {
1366 warnx("Too many bindings in PDU - %u", snmptoolctx->objects + 1);
1370 if ((obj = malloc(sizeof(struct snmp_object))) == NULL) {
1371 syslog(LOG_ERR, "malloc() failed: %s", strerror(errno));
1375 memset(obj, 0, sizeof(struct snmp_object));
1376 if (func(snmptoolctx, obj, string) < 0) {
1377 warnx("Invalid OID - %s", string);
1382 snmptoolctx->objects++;
1383 SLIST_INSERT_HEAD(&snmptoolctx->snmp_objectlist, obj, link);
1388 /* Given an OID, find it in the object list and remove it. */
1390 snmp_object_remove(struct snmp_toolinfo *snmptoolctx, struct asn_oid *oid)
1392 struct snmp_object *temp;
1394 if (SLIST_EMPTY(&snmptoolctx->snmp_objectlist)) {
1395 warnx("Object list already empty");
1400 SLIST_FOREACH(temp, &snmptoolctx->snmp_objectlist, link)
1401 if (asn_compare_oid(&(temp->val.var), oid) == 0)
1405 warnx("No such object in list");
1409 SLIST_REMOVE(&snmptoolctx->snmp_objectlist, temp, snmp_object, link);
1410 if (temp->val.syntax == SNMP_SYNTAX_OCTETSTRING &&
1411 temp->val.v.octetstring.octets != NULL)
1412 free(temp->val.v.octetstring.octets);
1419 snmp_object_freeall(struct snmp_toolinfo *snmptoolctx)
1421 struct snmp_object *o;
1423 while ((o = SLIST_FIRST(&snmptoolctx->snmp_objectlist)) != NULL) {
1424 SLIST_REMOVE_HEAD(&snmptoolctx->snmp_objectlist, link);
1426 if (o->val.syntax == SNMP_SYNTAX_OCTETSTRING &&
1427 o->val.v.octetstring.octets != NULL)
1428 free(o->val.v.octetstring.octets);
1433 /* Do all possible memory release before exit. */
1435 snmp_tool_freeall(struct snmp_toolinfo *snmptoolctx)
1437 if (snmp_client.chost != NULL) {
1438 free(snmp_client.chost);
1439 snmp_client.chost = NULL;
1442 if (snmp_client.cport != NULL) {
1443 free(snmp_client.cport);
1444 snmp_client.cport = NULL;
1447 snmp_mapping_free(snmptoolctx);
1448 free_filelist(snmptoolctx);
1449 snmp_object_freeall(snmptoolctx);
1451 if (snmptoolctx->passwd != NULL) {
1452 free(snmptoolctx->passwd);
1453 snmptoolctx->passwd = NULL;
1458 * Fill all variables from the object list into a PDU. (snmp_verify_vbind_f)
1459 * function should check whether the variable is consistent in this PDU
1460 * (e.g do not add non-leaf OIDs to a GET PDU, or OIDs with read access only to
1461 * a SET PDU) - might be NULL though. (snmp_add_vbind_f) function is the
1462 * function actually adds the variable to the PDU and must not be NULL.
1465 snmp_pdu_add_bindings(struct snmp_toolinfo *snmptoolctx,
1466 snmp_verify_vbind_f vfunc, snmp_add_vbind_f afunc,
1467 struct snmp_pdu *pdu, int32_t maxcount)
1469 int32_t nbindings, abind;
1470 struct snmp_object *obj;
1472 if (pdu == NULL || afunc == NULL)
1475 /* Return 0 in case of no more work todo. */
1476 if (SLIST_EMPTY(&snmptoolctx->snmp_objectlist))
1479 if (maxcount < 0 || maxcount > SNMP_MAX_BINDINGS) {
1480 warnx("maxcount out of range: <0 || >SNMP_MAX_BINDINGS");
1485 SLIST_FOREACH(obj, &snmptoolctx->snmp_objectlist, link) {
1486 if ((vfunc != NULL) && (vfunc(snmptoolctx, pdu, obj) < 0)) {
1490 if ((abind = afunc(pdu, obj)) < 0) {
1496 /* Do not put more varbindings than requested. */
1497 if (++nbindings >= maxcount)
1506 * Locate an object in the object list and set a corresponding error status.
1509 snmp_object_seterror(struct snmp_toolinfo *snmptoolctx,
1510 struct snmp_value *err_value, int32_t error_status)
1512 struct snmp_object *obj;
1514 if (SLIST_EMPTY(&snmptoolctx->snmp_objectlist) || err_value == NULL)
1517 SLIST_FOREACH(obj, &snmptoolctx->snmp_objectlist, link)
1518 if (asn_compare_oid(&(err_value->var), &(obj->val.var)) == 0) {
1519 obj->error = error_status;
1527 * Check a PDU received in response to a SNMP_PDU_GET/SNMP_PDU_GETBULK request
1528 * but don't compare syntaxes - when sending a request PDU they must be null.
1529 * This is a (almost) complete copy of snmp_pdu_check() - with matching syntaxes
1530 * checks and some other checks skipped.
1533 snmp_parse_get_resp(struct snmp_pdu *resp, struct snmp_pdu *req)
1537 for (i = 0; i < req->nbindings; i++) {
1538 if (asn_compare_oid(&req->bindings[i].var,
1539 &resp->bindings[i].var) != 0) {
1540 warnx("Bad OID in response");
1544 if (snmp_client.version != SNMP_V1 && (resp->bindings[i].syntax
1545 == SNMP_SYNTAX_NOSUCHOBJECT || resp->bindings[i].syntax ==
1546 SNMP_SYNTAX_NOSUCHINSTANCE))
1554 snmp_parse_getbulk_resp(struct snmp_pdu *resp, struct snmp_pdu *req)
1558 if (req->error_status > (int32_t) resp->nbindings) {
1559 warnx("Bad number of bindings in response");
1563 for (N = 0; N < req->error_status; N++) {
1564 if (asn_is_suboid(&req->bindings[N].var,
1565 &resp->bindings[N].var) == 0)
1567 if (resp->bindings[N].syntax == SNMP_SYNTAX_ENDOFMIBVIEW)
1571 for (R = N , r = N; R < (int32_t) req->nbindings; R++) {
1572 for (M = 0; M < req->error_index && (r + M) <
1573 (int32_t) resp->nbindings; M++) {
1574 if (asn_is_suboid(&req->bindings[R].var,
1575 &resp->bindings[r + M].var) == 0)
1578 if (resp->bindings[r + M].syntax ==
1579 SNMP_SYNTAX_ENDOFMIBVIEW) {
1591 snmp_parse_getnext_resp(struct snmp_pdu *resp, struct snmp_pdu *req)
1595 for (i = 0; i < req->nbindings; i++) {
1596 if (asn_is_suboid(&req->bindings[i].var, &resp->bindings[i].var)
1600 if (resp->version != SNMP_V1 && resp->bindings[i].syntax ==
1601 SNMP_SYNTAX_ENDOFMIBVIEW)
1609 * Should be called to check a response to get/getnext/getbulk.
1612 snmp_parse_resp(struct snmp_pdu *resp, struct snmp_pdu *req)
1614 if (resp == NULL || req == NULL)
1617 if (resp->version != req->version) {
1618 warnx("Response has wrong version");
1622 if (resp->error_status == SNMP_ERR_NOSUCHNAME) {
1623 warnx("Error - No Such Name");
1627 if (resp->error_status != SNMP_ERR_NOERROR) {
1628 warnx("Error %d in response", resp->error_status);
1632 if (resp->nbindings != req->nbindings && req->type != SNMP_PDU_GETBULK){
1633 warnx("Bad number of bindings in response");
1637 switch (req->type) {
1639 return (snmp_parse_get_resp(resp,req));
1640 case SNMP_PDU_GETBULK:
1641 return (snmp_parse_getbulk_resp(resp,req));
1642 case SNMP_PDU_GETNEXT:
1643 return (snmp_parse_getnext_resp(resp,req));
1653 snmp_output_octetstring(struct snmp_toolinfo *snmptoolctx, enum snmp_tc tc,
1654 uint32_t len, uint8_t *octets)
1658 if (len == 0 || octets == NULL)
1661 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1662 fprintf(stdout, "%s : ",
1663 syntax_strings[SNMP_SYNTAX_OCTETSTRING].str);
1665 if ((buf = snmp_oct2tc(tc, len, (char *) octets)) != NULL) {
1666 fprintf(stdout, "%s", buf);
1672 snmp_output_octetindex(struct snmp_toolinfo *snmptoolctx, enum snmp_tc tc,
1673 struct asn_oid *oid)
1678 if ((s = malloc(oid->subs[0] + 1)) == NULL)
1679 syslog(LOG_ERR, "malloc failed - %s", strerror(errno));
1681 for (i = 0; i < oid->subs[0]; i++)
1682 s[i] = (u_char) (oid->subs[i + 1]);
1684 snmp_output_octetstring(snmptoolctx, tc, oid->subs[0], s);
1690 * Check and output syntax type and value.
1693 snmp_output_oid_value(struct snmp_toolinfo *snmptoolctx, struct asn_oid *oid)
1695 char oid_string[ASN_OIDSTRLEN];
1696 struct snmp_object obj;
1698 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1699 fprintf(stdout, "%s : ", syntax_strings[SNMP_SYNTAX_OID].str);
1701 if(!ISSET_NUMERIC(snmptoolctx)) {
1702 memset(&obj, 0, sizeof(struct snmp_object));
1703 asn_append_oid(&(obj.val.var), oid);
1705 if (snmp_lookup_enumstring(snmptoolctx, &obj) > 0)
1706 fprintf(stdout, "%s" , obj.info->string);
1707 else if (snmp_lookup_oidstring(snmptoolctx, &obj) > 0)
1708 fprintf(stdout, "%s" , obj.info->string);
1709 else if (snmp_lookup_nodestring(snmptoolctx, &obj) > 0)
1710 fprintf(stdout, "%s" , obj.info->string);
1712 (void) asn_oid2str_r(oid, oid_string);
1713 fprintf(stdout, "%s", oid_string);
1716 (void) asn_oid2str_r(oid, oid_string);
1717 fprintf(stdout, "%s", oid_string);
1722 snmp_output_int(struct snmp_toolinfo *snmptoolctx, struct enum_pairs *enums,
1727 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1728 fprintf(stdout, "%s : ",
1729 syntax_strings[SNMP_SYNTAX_INTEGER].str);
1731 if (enums != NULL && (string = enum_string_lookup(enums, int_val))
1733 fprintf(stdout, "%s", string);
1735 fprintf(stdout, "%d", int_val);
1739 snmp_output_ipaddress(struct snmp_toolinfo *snmptoolctx, uint8_t *ip)
1741 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1742 fprintf(stdout, "%s : ",
1743 syntax_strings[SNMP_SYNTAX_IPADDRESS].str);
1745 fprintf(stdout, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
1749 snmp_output_counter(struct snmp_toolinfo *snmptoolctx, uint32_t counter)
1751 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1752 fprintf(stdout, "%s : ",
1753 syntax_strings[SNMP_SYNTAX_COUNTER].str);
1755 fprintf(stdout, "%u", counter);
1759 snmp_output_gauge(struct snmp_toolinfo *snmptoolctx, uint32_t gauge)
1761 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1762 fprintf(stdout, "%s : ", syntax_strings[SNMP_SYNTAX_GAUGE].str);
1764 fprintf(stdout, "%u", gauge);
1768 snmp_output_ticks(struct snmp_toolinfo *snmptoolctx, uint32_t ticks)
1770 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1771 fprintf(stdout, "%s : ",
1772 syntax_strings[SNMP_SYNTAX_TIMETICKS].str);
1774 fprintf(stdout, "%u", ticks);
1778 snmp_output_counter64(struct snmp_toolinfo *snmptoolctx, uint64_t counter64)
1780 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
1781 fprintf(stdout, "%s : ",
1782 syntax_strings[SNMP_SYNTAX_COUNTER64].str);
1784 fprintf(stdout,"%ju", counter64);
1788 snmp_output_numval(struct snmp_toolinfo *snmptoolctx, struct snmp_value *val,
1789 struct snmp_oid2str *entry)
1794 if (GET_OUTPUT(snmptoolctx) != OUTPUT_QUIET)
1795 fprintf(stdout, " = ");
1797 switch (val->syntax) {
1798 case SNMP_SYNTAX_INTEGER:
1800 snmp_output_int(snmptoolctx, entry->snmp_enum,
1803 snmp_output_int(snmptoolctx, NULL, val->v.integer);
1806 case SNMP_SYNTAX_OCTETSTRING:
1808 snmp_output_octetstring(snmptoolctx, entry->tc,
1809 val->v.octetstring.len, val->v.octetstring.octets);
1811 snmp_output_octetstring(snmptoolctx, SNMP_STRING,
1812 val->v.octetstring.len, val->v.octetstring.octets);
1815 case SNMP_SYNTAX_OID:
1816 snmp_output_oid_value(snmptoolctx, &(val->v.oid));
1819 case SNMP_SYNTAX_IPADDRESS:
1820 snmp_output_ipaddress(snmptoolctx, val->v.ipaddress);
1823 case SNMP_SYNTAX_COUNTER:
1824 snmp_output_counter(snmptoolctx, val->v.uint32);
1827 case SNMP_SYNTAX_GAUGE:
1828 snmp_output_gauge(snmptoolctx, val->v.uint32);
1831 case SNMP_SYNTAX_TIMETICKS:
1832 snmp_output_ticks(snmptoolctx, val->v.uint32);
1835 case SNMP_SYNTAX_COUNTER64:
1836 snmp_output_counter64(snmptoolctx, val->v.counter64);
1839 case SNMP_SYNTAX_NOSUCHOBJECT:
1840 fprintf(stdout, "No Such Object\n");
1841 return (val->syntax);
1843 case SNMP_SYNTAX_NOSUCHINSTANCE:
1844 fprintf(stdout, "No Such Instance\n");
1845 return (val->syntax);
1847 case SNMP_SYNTAX_ENDOFMIBVIEW:
1848 fprintf(stdout, "End of Mib View\n");
1849 return (val->syntax);
1851 case SNMP_SYNTAX_NULL:
1853 fprintf(stdout, "agent returned NULL Syntax\n");
1854 return (val->syntax);
1857 /* NOTREACHED - If here - then all went completely wrong. */
1858 fprintf(stdout, "agent returned unknown syntax\n");
1862 fprintf(stdout, "\n");
1868 snmp_fill_object(struct snmp_toolinfo *snmptoolctx, struct snmp_object *obj,
1869 struct snmp_value *val)
1874 if (obj == NULL || val == NULL)
1877 if ((suboid = snmp_suboid_pop(&(val->var))) > ASN_MAXID)
1880 memset(obj, 0, sizeof(struct snmp_object));
1881 asn_append_oid(&(obj->val.var), &(val->var));
1882 obj->val.syntax = val->syntax;
1884 if (obj->val.syntax > 0)
1885 rc = snmp_lookup_leafstring(snmptoolctx, obj);
1887 rc = snmp_lookup_nonleaf_string(snmptoolctx, obj);
1889 (void) snmp_suboid_append(&(val->var), suboid);
1890 (void) snmp_suboid_append(&(obj->val.var), suboid);
1896 snmp_output_index(struct snmp_toolinfo *snmptoolctx, struct index *stx,
1897 struct asn_oid *oid)
1902 struct asn_oid temp, out;
1904 if (oid->len < bytes)
1907 memset(&temp, 0, sizeof(struct asn_oid));
1908 asn_append_oid(&temp, oid);
1910 switch (stx->syntax) {
1911 case SNMP_SYNTAX_INTEGER:
1912 snmp_output_int(snmptoolctx, stx->snmp_enum, temp.subs[0]);
1915 case SNMP_SYNTAX_OCTETSTRING:
1916 if ((temp.subs[0] > temp.len -1 ) || (temp.subs[0] >
1917 ASN_MAXOCTETSTRING))
1919 snmp_output_octetindex(snmptoolctx, stx->tc, &temp);
1920 bytes += temp.subs[0];
1923 case SNMP_SYNTAX_OID:
1924 if ((temp.subs[0] > temp.len -1) || (temp.subs[0] >
1928 bytes += temp.subs[0];
1929 memset(&out, 0, sizeof(struct asn_oid));
1930 asn_slice_oid(&out, &temp, 1, bytes);
1931 snmp_output_oid_value(snmptoolctx, &out);
1934 case SNMP_SYNTAX_IPADDRESS:
1937 for (bytes = 0; bytes < 4; bytes++)
1938 ip[bytes] = temp.subs[bytes];
1940 snmp_output_ipaddress(snmptoolctx, ip);
1944 case SNMP_SYNTAX_COUNTER:
1945 snmp_output_counter(snmptoolctx, temp.subs[0]);
1948 case SNMP_SYNTAX_GAUGE:
1949 snmp_output_gauge(snmptoolctx, temp.subs[0]);
1952 case SNMP_SYNTAX_TIMETICKS:
1953 snmp_output_ticks(snmptoolctx, temp.subs[0]);
1956 case SNMP_SYNTAX_COUNTER64:
1960 memcpy(&cnt64, temp.subs, bytes);
1961 snmp_output_counter64(snmptoolctx, cnt64);
1972 snmp_output_object(struct snmp_toolinfo *snmptoolctx, struct snmp_object *o)
1974 int32_t i, first, len;
1978 if (ISSET_NUMERIC(snmptoolctx))
1981 if (o->info->table_idx == NULL) {
1982 fprintf(stdout,"%s.%d", o->info->string,
1983 o->val.var.subs[o->val.var.len - 1]);
1987 fprintf(stdout,"%s[", o->info->string);
1988 memset(&oid, 0, sizeof(struct asn_oid));
1991 asn_slice_oid(&oid, &(o->val.var), (o->info->table_idx->var.len + len),
1995 STAILQ_FOREACH(temp, &(OBJECT_IDX_LIST(o)), link) {
1999 fprintf(stdout, ", ");
2000 if ((i = snmp_output_index(snmptoolctx, temp, &oid)) < 0)
2003 memset(&oid, 0, sizeof(struct asn_oid));
2004 asn_slice_oid(&oid, &(o->val.var),
2005 (o->info->table_idx->var.len + len), o->val.var.len + 1);
2008 fprintf(stdout,"]");
2013 snmp_output_err_resp(struct snmp_toolinfo *snmptoolctx, struct snmp_pdu *pdu)
2015 char buf[ASN_OIDSTRLEN];
2016 struct snmp_object object;
2018 if (pdu == NULL || (pdu->error_index > (int32_t) pdu->nbindings)) {
2019 fprintf(stdout,"Invalid error index in PDU\n");
2023 fprintf(stdout, "Agent %s:%s returned error \n", snmp_client.chost,
2026 if (!ISSET_NUMERIC(snmptoolctx) && (snmp_fill_object(snmptoolctx, &object,
2027 &(pdu->bindings[pdu->error_index - 1])) > 0))
2028 snmp_output_object(snmptoolctx, &object);
2030 asn_oid2str_r(&(pdu->bindings[pdu->error_index - 1].var), buf);
2031 fprintf(stdout,"%s", buf);
2034 fprintf(stdout," caused error - ");
2035 if ((pdu->error_status > 0) && (pdu->error_status <=
2036 SNMP_ERR_INCONS_NAME))
2037 fprintf(stdout, "%s\n", error_strings[pdu->error_status].str);
2039 fprintf(stdout,"%s\n", error_strings[SNMP_ERR_UNKNOWN].str);
2043 snmp_output_resp(struct snmp_toolinfo *snmptoolctx, struct snmp_pdu *pdu,
2044 struct asn_oid *root)
2047 char p[ASN_OIDSTRLEN];
2049 struct snmp_object object;
2052 while (i < pdu->nbindings) {
2053 if (root != NULL && !(asn_is_suboid(root,
2054 &(pdu->bindings[i].var))))
2057 if (GET_OUTPUT(snmptoolctx) != OUTPUT_QUIET) {
2058 if (!ISSET_NUMERIC(snmptoolctx) &&
2059 (snmp_fill_object(snmptoolctx, &object,
2060 &(pdu->bindings[i])) > 0))
2061 snmp_output_object(snmptoolctx, &object);
2063 asn_oid2str_r(&(pdu->bindings[i].var), p);
2064 fprintf(stdout, "%s", p);
2067 error |= snmp_output_numval(snmptoolctx, &(pdu->bindings[i]), object.info);
2078 snmp_output_engine(void)
2081 char *cptr, engine[2 * SNMP_ENGINE_ID_SIZ + 2];
2084 for (i = 0; i < snmp_client.engine.engine_len; i++)
2085 cptr += sprintf(cptr, "%.2x", snmp_client.engine.engine_id[i]);
2088 fprintf(stdout, "Engine ID 0x%s\n", engine);
2089 fprintf(stdout, "Boots : %u\t\tTime : %d\n",
2090 snmp_client.engine.engine_boots,
2091 snmp_client.engine.engine_time);
2095 snmp_output_keys(void)
2097 uint32_t i, keylen = 0;
2098 char *cptr, extkey[2 * SNMP_AUTH_KEY_SIZ + 2];
2100 fprintf(stdout, "Localized keys for %s\n", snmp_client.user.sec_name);
2101 if (snmp_client.user.auth_proto == SNMP_AUTH_HMAC_MD5) {
2102 fprintf(stdout, "MD5 : 0x");
2103 keylen = SNMP_AUTH_HMACMD5_KEY_SIZ;
2104 } else if (snmp_client.user.auth_proto == SNMP_AUTH_HMAC_SHA) {
2105 fprintf(stdout, "SHA : 0x");
2106 keylen = SNMP_AUTH_HMACSHA_KEY_SIZ;
2108 if (snmp_client.user.auth_proto != SNMP_AUTH_NOAUTH) {
2110 for (i = 0; i < keylen; i++)
2111 cptr += sprintf(cptr, "%.2x",
2112 snmp_client.user.auth_key[i]);
2114 fprintf(stdout, "%s\n", extkey);
2117 if (snmp_client.user.priv_proto == SNMP_PRIV_DES) {
2118 fprintf(stdout, "DES : 0x");
2119 keylen = SNMP_PRIV_DES_KEY_SIZ;
2120 } else if (snmp_client.user.priv_proto == SNMP_PRIV_AES) {
2121 fprintf(stdout, "AES : 0x");
2122 keylen = SNMP_PRIV_AES_KEY_SIZ;
2124 if (snmp_client.user.priv_proto != SNMP_PRIV_NOPRIV) {
2126 for (i = 0; i < keylen; i++)
2127 cptr += sprintf(cptr, "%.2x",
2128 snmp_client.user.priv_key[i]);
2130 fprintf(stdout, "%s\n", extkey);