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 * Bsnmpget and bsnmpwalk are simple tools for querying SNMP agents,
30 * bsnmpset can be used to set MIB objects in an agent.
35 #include <sys/queue.h>
36 #include <sys/types.h>
49 #include <bsnmp/asn1.h>
50 #include <bsnmp/snmp.h>
51 #include <bsnmp/snmpclient.h>
53 #include "bsnmptools.h"
55 static const char *program_name = NULL;
56 static enum program_e {
62 /* *****************************************************************************
63 * Common bsnmptools functions.
70 "%s %s [-A options] [-b buffersize] [-C options] [-I options]\n"
71 "\t[-i filelist] [-l filename]%s [-o output] [-P options]\n"
72 "\t%s[-r retries] [-s [trans::][community@][server][:port]]\n"
73 "\t[-t timeout] [-U options] [-v version]%s\n",
75 (program == BSNMPGET) ? "[-aDdehnK]" :
76 (program == BSNMPWALK) ? "[-dhnK]" :
77 (program == BSNMPSET) ? "[-adehnK]" :
79 (program == BSNMPGET) ? " [-M max-repetitions] [-N non-repeaters]" : "",
80 (program == BSNMPGET) ? "[-p pdu] " : "",
81 (program == BSNMPGET) ? " OID [OID ...]" :
82 (program == BSNMPWALK || program == BSNMPSET) ? " [OID ...]" :
88 parse_max_repetitions(struct snmp_toolinfo *snmptoolctx, char *opt_arg)
92 assert(opt_arg != NULL);
94 v = strtoul(opt_arg, (void *) NULL, 10);
96 if (v > SNMP_MAX_BINDINGS) {
97 warnx("Max repetitions value greater than %d maximum allowed.",
102 SET_MAXREP(snmptoolctx, v);
107 parse_non_repeaters(struct snmp_toolinfo *snmptoolctx, char *opt_arg)
111 assert(opt_arg != NULL);
113 v = strtoul(opt_arg, (void *) NULL, 10);
115 if (v > SNMP_MAX_BINDINGS) {
116 warnx("Non repeaters value greater than %d maximum allowed.",
121 SET_NONREP(snmptoolctx, v);
126 parse_pdu_type(struct snmp_toolinfo *snmptoolctx, char *opt_arg)
128 assert(opt_arg != NULL);
130 if (strcasecmp(opt_arg, "getbulk") == 0)
131 SET_PDUTYPE(snmptoolctx, SNMP_PDU_GETBULK);
132 else if (strcasecmp(opt_arg, "getnext") == 0)
133 SET_PDUTYPE(snmptoolctx, SNMP_PDU_GETNEXT);
134 else if (strcasecmp(opt_arg, "get") == 0)
135 SET_PDUTYPE(snmptoolctx, SNMP_PDU_GET);
137 warnx("PDU type '%s' not supported.", opt_arg);
145 snmptool_parse_options(struct snmp_toolinfo *snmptoolctx, int argc, char **argv)
147 int32_t count, optnum = 0;
153 opts = "dhnKA:b:C:I:i:l:o:P:r:s:t:U:v:";
156 opts = "aDdehnKA:b:C:I:i:l:M:N:o:P:p:r:s:t:U:v:";
159 opts = "adehnKA:b:C:I:i:l:o:P:r:s:t:U:v:";
165 while ((ch = getopt(argc, argv, opts)) != EOF) {
168 count = parse_authentication(snmptoolctx, optarg);
171 count = parse_skip_access(snmptoolctx);
174 count = parse_buflen(optarg);
177 count = parse_discovery(snmptoolctx);
180 count = parse_debug();
183 count = parse_errors(snmptoolctx);
189 count = parse_context(snmptoolctx, optarg);
192 count = parse_include(snmptoolctx, optarg);
195 count = parse_file(snmptoolctx, optarg);
198 count = parse_local_key(snmptoolctx);
201 count = parse_local_path(optarg);
204 count = parse_max_repetitions(snmptoolctx, optarg);
207 count = parse_non_repeaters(snmptoolctx, optarg);
210 count = parse_num_oids(snmptoolctx);
213 count = parse_output(snmptoolctx, optarg);
216 count = parse_privacy(snmptoolctx, optarg);
219 count = parse_pdu_type(snmptoolctx, optarg);
222 count = parse_retry(optarg);
225 count = parse_server(optarg);
228 count = parse_timeout(optarg);
231 count = parse_user_security(snmptoolctx, optarg);
234 count = parse_version(optarg);
250 * Read user input OID - one of following formats:
251 * 1) 1.2.1.1.2.1.0 - that is if option numeric was giveni;
252 * 2) string - in such case append .0 to the asn_oid subs;
253 * 3) string.1 - no additional proccessing required in such case.
256 snmptools_parse_stroid(struct snmp_toolinfo *snmptoolctx,
257 struct snmp_object *obj, char *argv)
259 char string[MAXSTR], *str;
261 struct asn_oid in_oid;
268 while (isalpha(*str) || *str == '_' || (i != 0 && isdigit(*str))) {
273 if (i <= 0 || i >= MAXSTR)
276 memset(&in_oid, 0, sizeof(struct asn_oid));
277 if ((str = snmp_parse_suboid((argv + i), &in_oid)) == NULL) {
278 warnx("Invalid OID - %s", argv);
282 strlcpy(string, argv, i + 1);
283 if (snmp_lookup_oidall(snmptoolctx, obj, string) < 0) {
284 warnx("No entry for %s in mapping lists", string);
288 /* If OID given on command line append it. */
290 asn_append_oid(&(obj->val.var), &in_oid);
291 else if (*str == '[') {
292 if ((str = snmp_parse_index(snmptoolctx, str + 1, obj)) == NULL)
294 } else if (obj->val.syntax > 0 && GET_PDUTYPE(snmptoolctx) ==
296 if (snmp_suboid_append(&(obj->val.var), (asn_subid_t) 0) < 0)
304 snmptools_parse_oid(struct snmp_toolinfo *snmptoolctx,
305 struct snmp_object *obj, char *argv)
310 if (ISSET_NUMERIC(snmptoolctx)) {
311 if (snmp_parse_numoid(argv, &(obj->val.var)) < 0)
314 if (snmptools_parse_stroid(snmptoolctx, obj, argv) == NULL &&
315 snmp_parse_numoid(argv, &(obj->val.var)) < 0)
323 snmptool_add_vbind(struct snmp_pdu *pdu, struct snmp_object *obj)
328 asn_append_oid(&(pdu->bindings[pdu->nbindings].var), &(obj->val.var));
331 return (pdu->nbindings);
334 /* *****************************************************************************
335 * bsnmpget private functions.
338 snmpget_verify_vbind(struct snmp_toolinfo *snmptoolctx, struct snmp_pdu *pdu,
339 struct snmp_object *obj)
341 if (pdu->version == SNMP_V1 && obj->val.syntax ==
342 SNMP_SYNTAX_COUNTER64) {
343 warnx("64-bit counters are not supported in SNMPv1 PDU");
347 if (ISSET_NUMERIC(snmptoolctx) || pdu->type == SNMP_PDU_GETNEXT ||
348 pdu->type == SNMP_PDU_GETBULK)
351 if (pdu->type == SNMP_PDU_GET && obj->val.syntax == SNMP_SYNTAX_NULL) {
352 warnx("Only leaf object values can be added to GET PDU");
360 * In case of a getbulk PDU, the error_status and error_index fields are used by
361 * libbsnmp to hold the values of the non-repeaters and max-repetitions fields
362 * that are present only in the getbulk - so before sending the PDU make sure
363 * these have correct values as well.
366 snmpget_fix_getbulk(struct snmp_pdu *pdu, uint32_t max_rep, uint32_t non_rep)
370 if (pdu->nbindings < non_rep)
371 pdu->error_status = pdu->nbindings;
373 pdu->error_status = non_rep;
376 pdu->error_index = max_rep;
378 pdu->error_index = 1;
382 snmptool_get(struct snmp_toolinfo *snmptoolctx)
384 struct snmp_pdu req, resp;
386 snmp_pdu_create(&req, GET_PDUTYPE(snmptoolctx));
388 while ((snmp_pdu_add_bindings(snmptoolctx, snmpget_verify_vbind,
389 snmptool_add_vbind, &req, SNMP_MAX_BINDINGS)) > 0) {
391 if (GET_PDUTYPE(snmptoolctx) == SNMP_PDU_GETBULK)
392 snmpget_fix_getbulk(&req, GET_MAXREP(snmptoolctx),
393 GET_NONREP(snmptoolctx));
395 if (snmp_dialog(&req, &resp) == -1) {
396 warnx("Snmp dialog - %s", strerror(errno));
400 if (snmp_parse_resp(&resp, &req) >= 0) {
401 snmp_output_resp(snmptoolctx, &resp);
405 snmp_output_err_resp(snmptoolctx, &resp);
406 if (GET_PDUTYPE(snmptoolctx) == SNMP_PDU_GETBULK ||
407 !ISSET_RETRY(snmptoolctx))
411 * Loop through the object list and set object->error to the
412 * varbinding that caused the error.
414 if (snmp_object_seterror(snmptoolctx,
415 &(resp.bindings[resp.error_index - 1]),
416 resp.error_status) <= 0)
419 fprintf(stderr, "Retrying...\n");
420 snmp_pdu_free(&resp);
421 snmp_pdu_create(&req, GET_PDUTYPE(snmptoolctx));
424 snmp_pdu_free(&resp);
430 /* *****************************************************************************
431 * bsnmpwalk private functions.
433 /* The default tree to walk. */
434 static const struct asn_oid snmp_mibII_OID = {
435 6 , { 1, 3, 6, 1, 2, 1 }
439 snmpwalk_add_default(struct snmp_toolinfo *snmptoolctx __unused,
440 struct snmp_object *obj, char *string __unused)
442 asn_append_oid(&(obj->val.var), &snmp_mibII_OID);
447 * Prepare the next GetNext/Get PDU to send.
450 snmpwalk_nextpdu_create(uint32_t op, struct asn_oid *var, struct snmp_pdu *pdu)
452 snmp_pdu_create(pdu, op);
453 asn_append_oid(&(pdu->bindings[0].var), var);
458 snmptool_walk(struct snmp_toolinfo *snmptoolctx)
460 struct snmp_pdu req, resp;
461 struct asn_oid root; /* Keep the inital oid. */
464 snmp_pdu_create(&req, SNMP_PDU_GETNEXT);
466 while ((rc = snmp_pdu_add_bindings(snmptoolctx, NULL,
467 snmptool_add_vbind, &req, 1)) > 0) {
469 /* Remember the root where the walk started from. */
470 memset(&root, 0, sizeof(struct asn_oid));
471 asn_append_oid(&root, &(req.bindings[0].var));
474 while (snmp_dialog(&req, &resp) >= 0) {
475 if ((snmp_parse_resp(&resp, &req)) < 0) {
476 snmp_output_err_resp(snmptoolctx, &resp);
477 snmp_pdu_free(&resp);
482 if (!(asn_is_suboid(&root, &(resp.bindings[0].var)))) {
483 snmp_pdu_free(&resp);
487 if (snmp_output_resp(snmptoolctx, &resp)!= 0) {
488 snmp_pdu_free(&resp);
493 snmp_pdu_free(&resp);
495 snmpwalk_nextpdu_create(SNMP_PDU_GETNEXT,
496 &(resp.bindings[0].var), &req);
499 /* Just in case our root was a leaf. */
501 snmpwalk_nextpdu_create(SNMP_PDU_GET, &root, &req);
502 if (snmp_dialog(&req, &resp) == SNMP_CODE_OK) {
503 if (snmp_parse_resp(&resp,&req) < 0)
504 snmp_output_err_resp(snmptoolctx, &resp);
506 snmp_output_resp(snmptoolctx, &(resp));
508 snmp_pdu_free(&resp);
510 warnx("Snmp dialog - %s", strerror(errno));
513 if (snmp_object_remove(snmptoolctx, &root) < 0) {
514 warnx("snmp_object_remove");
518 snmp_pdu_create(&req, SNMP_PDU_GETNEXT);
527 /* *****************************************************************************
528 * bsnmpset private functions.
532 parse_oid_numeric(struct snmp_value *value, char *val)
541 suboid = strtoul(val, &endptr, 10);
543 warnx("Value %s not supported - %s", val,
549 if ((asn_subid_t) suboid > ASN_MAXID) {
550 warnx("Suboid %u > ASN_MAXID", suboid);
553 if (snmp_suboid_append(&(value->v.oid), suboid) < 0)
556 } while (*endptr == '.');
559 warnx("OID value %s not supported", val);
561 value->syntax = SNMP_SYNTAX_OID;
566 * Allow OID leaf in both forms:
567 * 1) 1.3.6.1.2... -> in such case call directly the function reading raw OIDs;
568 * 2) begemotSnmpdAgentFreeBSD -> lookup the ASN OID corresponding to that.
571 parse_oid_string(struct snmp_toolinfo *snmptoolctx,
572 struct snmp_value *value, char *string)
574 struct snmp_object obj;
576 if (isdigit(string[0]))
577 return (parse_oid_numeric(value, string));
579 memset(&obj, 0, sizeof(struct snmp_object));
580 if (snmp_lookup_enumoid(snmptoolctx, &obj, string) < 0) {
581 warnx("Unknown OID enum string - %s", string);
585 asn_append_oid(&(value->v.oid), &(obj.val.var));
590 parse_ip(struct snmp_value * value, char * val)
597 for (i = 0; i < 4; i++) {
598 v = strtoul(str, &endptr, 10);
601 if (*endptr != '.' && *endptr != '\0' && i != 3)
604 value->v.ipaddress[i] = (uint8_t) v;
607 value->syntax = SNMP_SYNTAX_IPADDRESS;
612 parse_int(struct snmp_value *value, char *val)
615 int32_t v, saved_errno;
620 v = strtol(val, &endptr, 10);
623 warnx("Value %s not supported - %s", val, strerror(errno));
628 value->syntax = SNMP_SYNTAX_INTEGER;
629 value->v.integer = v;
636 parse_int_string(struct snmp_object *object, char *val)
641 return ((parse_int(&(object->val), val)));
643 if (object->info == NULL) {
644 warnx("Unknown enumerated integer type - %s", val);
647 if ((v = enum_number_lookup(object->info->snmp_enum, val)) < 0)
648 warnx("Unknown enumerated integer type - %s", val);
650 object->val.v.integer = v;
655 * Here syntax may be one of SNMP_SYNTAX_COUNTER, SNMP_SYNTAX_GAUGE,
656 * SNMP_SYNTAX_TIMETICKS.
659 parse_uint(struct snmp_value *value, char *val)
668 v = strtoul(val, &endptr, 10);
671 warnx("Value %s not supported - %s", val, strerror(errno));
683 parse_ticks(struct snmp_value *value, char *val)
685 if (parse_uint(value, val) < 0)
688 value->syntax = SNMP_SYNTAX_TIMETICKS;
693 parse_gauge(struct snmp_value *value, char *val)
695 if (parse_uint(value, val) < 0)
698 value->syntax = SNMP_SYNTAX_GAUGE;
703 parse_counter(struct snmp_value *value, char *val)
705 if (parse_uint(value, val) < 0)
708 value->syntax = SNMP_SYNTAX_COUNTER;
713 parse_uint64(struct snmp_value *value, char *val)
722 v = strtoull(val, &endptr, 10);
725 warnx("Value %s not supported - %s", val, strerror(errno));
730 value->syntax = SNMP_SYNTAX_COUNTER64;
731 value->v.counter64 = v;
738 parse_syntax_val(struct snmp_value *value, enum snmp_syntax syntax, char *val)
741 case SNMP_SYNTAX_INTEGER:
742 return (parse_int(value, val));
743 case SNMP_SYNTAX_IPADDRESS:
744 return (parse_ip(value, val));
745 case SNMP_SYNTAX_COUNTER:
746 return (parse_counter(value, val));
747 case SNMP_SYNTAX_GAUGE:
748 return (parse_gauge(value, val));
749 case SNMP_SYNTAX_TIMETICKS:
750 return (parse_ticks(value, val));
751 case SNMP_SYNTAX_COUNTER64:
752 return (parse_uint64(value, val));
753 case SNMP_SYNTAX_OCTETSTRING:
754 return (snmp_tc2oct(SNMP_STRING, value, val));
755 case SNMP_SYNTAX_OID:
756 return (parse_oid_numeric(value, val));
766 * Parse a command line argument of type OID=syntax:value and fill in whatever
767 * fields can be derived from the input into snmp_value structure. Reads numeric
771 parse_pair_numoid_val(char *str, struct snmp_value *snmp_val)
775 enum snmp_syntax syntax;
776 char oid_str[ASN_OIDSTRLEN];
779 for (cnt = 0; cnt < ASN_OIDSTRLEN; cnt++)
783 if (cnt >= ASN_OIDSTRLEN) {
784 warnx("OID too long - %s", str);
787 strlcpy(oid_str, ptr, (size_t) (cnt + 1));
790 for (cnt = 0; cnt < MAX_CMD_SYNTAX_LEN; cnt++)
794 if (cnt >= MAX_CMD_SYNTAX_LEN) {
795 warnx("Unknown syntax in OID - %s", str);
799 if ((syntax = parse_syntax(ptr)) <= SNMP_SYNTAX_NULL) {
800 warnx("Unknown syntax in OID - %s", ptr);
805 for (cnt = 0; cnt < MAX_OCTSTRING_LEN; cnt++)
806 if (ptr[cnt] == '\0')
809 if (ptr[cnt] != '\0') {
810 warnx("Value string too long - %s",ptr);
815 * Here try parsing the OIDs and syntaxes and then check values - have
816 * to know syntax to check value boundaries.
818 if (snmp_parse_numoid(oid_str, &(snmp_val->var)) < 0) {
819 warnx("Error parsing OID %s",oid_str);
823 if (parse_syntax_val(snmp_val, syntax, ptr) < 0)
829 /* XXX-BZ aruments should be swapped. */
831 parse_syntax_strval(struct snmp_toolinfo *snmptoolctx, char *str,
832 struct snmp_object *object)
835 enum snmp_syntax syn;
838 * Syntax string here not required - still may be present.
841 if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE) {
842 for (len = 0 ; *(str + len) != ':'; len++) {
843 if (*(str + len) == '\0') {
844 warnx("Syntax missing in value - %s", str);
848 if ((syn = parse_syntax(str)) <= SNMP_SYNTAX_NULL) {
849 warnx("Unknown syntax in - %s", str);
852 if (syn != object->val.syntax) {
853 if (!ISSET_ERRIGNORE(snmptoolctx)) {
854 warnx("Bad syntax in - %s", str);
857 object->val.syntax = syn;
863 switch (object->val.syntax) {
864 case SNMP_SYNTAX_INTEGER:
865 return (parse_int_string(object, str + len));
866 case SNMP_SYNTAX_IPADDRESS:
867 return (parse_ip(&(object->val), str + len));
868 case SNMP_SYNTAX_COUNTER:
869 return (parse_counter(&(object->val), str + len));
870 case SNMP_SYNTAX_GAUGE:
871 return (parse_gauge(&(object->val), str + len));
872 case SNMP_SYNTAX_TIMETICKS:
873 return (parse_ticks(&(object->val), str + len));
874 case SNMP_SYNTAX_COUNTER64:
875 return (parse_uint64(&(object->val), str + len));
876 case SNMP_SYNTAX_OCTETSTRING:
877 return (snmp_tc2oct(object->info->tc, &(object->val),
879 case SNMP_SYNTAX_OID:
880 return (parse_oid_string(snmptoolctx, &(object->val),
891 parse_pair_stroid_val(struct snmp_toolinfo *snmptoolctx,
892 struct snmp_object *obj, char *argv)
896 if ((ptr = snmptools_parse_stroid(snmptoolctx, obj, argv)) == NULL)
900 warnx("Value to set expected after OID");
904 if (parse_syntax_strval(snmptoolctx, ptr + 1, obj) < 0)
912 snmpset_parse_oid(struct snmp_toolinfo *snmptoolctx,
913 struct snmp_object *obj, char *argv)
918 if (ISSET_NUMERIC(snmptoolctx)) {
919 if (parse_pair_numoid_val(argv, &(obj->val)) < 0)
922 if (parse_pair_stroid_val(snmptoolctx, obj, argv) < 0)
930 add_ip_syntax(struct snmp_value *dst, struct snmp_value *src)
934 dst->syntax = SNMP_SYNTAX_IPADDRESS;
935 for (i = 0; i < 4; i++)
936 dst->v.ipaddress[i] = src->v.ipaddress[i];
942 add_octstring_syntax(struct snmp_value *dst, struct snmp_value *src)
944 if (src->v.octetstring.len > ASN_MAXOCTETSTRING) {
945 warnx("OctetString len too big - %u",src->v.octetstring.len);
949 if ((dst->v.octetstring.octets = malloc(src->v.octetstring.len)) ==
951 syslog(LOG_ERR, "malloc() failed - %s", strerror(errno));
955 memcpy(dst->v.octetstring.octets, src->v.octetstring.octets,
956 src->v.octetstring.len);
957 dst->syntax = SNMP_SYNTAX_OCTETSTRING;
958 dst->v.octetstring.len = src->v.octetstring.len;
964 add_oid_syntax(struct snmp_value *dst, struct snmp_value *src)
966 asn_append_oid(&(dst->v.oid), &(src->v.oid));
967 dst->syntax = SNMP_SYNTAX_OID;
972 * Check syntax - if one of SNMP_SYNTAX_NULL, SNMP_SYNTAX_NOSUCHOBJECT,
973 * SNMP_SYNTAX_NOSUCHINSTANCE, SNMP_SYNTAX_ENDOFMIBVIEW or anything not known -
977 snmpset_add_value(struct snmp_value *dst, struct snmp_value *src)
979 if (dst == NULL || src == NULL)
982 switch (src->syntax) {
983 case SNMP_SYNTAX_INTEGER:
984 dst->v.integer = src->v.integer;
985 dst->syntax = SNMP_SYNTAX_INTEGER;
987 case SNMP_SYNTAX_TIMETICKS:
988 dst->v.uint32 = src->v.uint32;
989 dst->syntax = SNMP_SYNTAX_TIMETICKS;
991 case SNMP_SYNTAX_GAUGE:
992 dst->v.uint32 = src->v.uint32;
993 dst->syntax = SNMP_SYNTAX_GAUGE;
995 case SNMP_SYNTAX_COUNTER:
996 dst->v.uint32 = src->v.uint32;
997 dst->syntax = SNMP_SYNTAX_COUNTER;
999 case SNMP_SYNTAX_COUNTER64:
1000 dst->v.counter64 = src->v.counter64;
1001 dst->syntax = SNMP_SYNTAX_COUNTER64;
1003 case SNMP_SYNTAX_IPADDRESS:
1004 add_ip_syntax(dst, src);
1006 case SNMP_SYNTAX_OCTETSTRING:
1007 add_octstring_syntax(dst, src);
1009 case SNMP_SYNTAX_OID:
1010 add_oid_syntax(dst, src);
1013 warnx("Unknown syntax %d", src->syntax);
1021 snmpset_verify_vbind(struct snmp_toolinfo *snmptoolctx, struct snmp_pdu *pdu,
1022 struct snmp_object *obj)
1024 if (pdu->version == SNMP_V1 && obj->val.syntax ==
1025 SNMP_SYNTAX_COUNTER64) {
1026 warnx("64-bit counters are not supported in SNMPv1 PDU");
1030 if (ISSET_NUMERIC(snmptoolctx) || ISSET_ERRIGNORE(snmptoolctx))
1033 if (obj->info->access < SNMP_ACCESS_SET) {
1034 warnx("Object %s not accessible for set - try 'bsnmpset -a'",
1043 snmpset_add_vbind(struct snmp_pdu *pdu, struct snmp_object *obj)
1045 if (pdu->nbindings > SNMP_MAX_BINDINGS) {
1046 warnx("Too many OIDs for one PDU");
1053 if (snmpset_add_value(&(pdu->bindings[pdu->nbindings]), &(obj->val))
1057 asn_append_oid(&(pdu->bindings[pdu->nbindings].var), &(obj->val.var));
1060 return (pdu->nbindings);
1064 snmptool_set(struct snmp_toolinfo *snmptoolctx)
1066 struct snmp_pdu req, resp;
1068 snmp_pdu_create(&req, SNMP_PDU_SET);
1070 while ((snmp_pdu_add_bindings(snmptoolctx, snmpset_verify_vbind,
1071 snmpset_add_vbind, &req, SNMP_MAX_BINDINGS)) > 0) {
1072 if (snmp_dialog(&req, &resp)) {
1073 warnx("Snmp dialog - %s", strerror(errno));
1077 if (snmp_pdu_check(&req, &resp) > 0) {
1078 if (GET_OUTPUT(snmptoolctx) != OUTPUT_QUIET)
1079 snmp_output_resp(snmptoolctx, &resp);
1083 snmp_output_err_resp(snmptoolctx, &resp);
1084 if (!ISSET_RETRY(snmptoolctx))
1087 if (snmp_object_seterror(snmptoolctx,
1088 &(resp.bindings[resp.error_index - 1]),
1089 resp.error_status) <= 0)
1092 fprintf(stderr, "Retrying...\n");
1093 snmp_pdu_free(&req);
1094 snmp_pdu_free(&resp);
1095 snmp_pdu_create(&req, SNMP_PDU_SET);
1098 snmp_pdu_free(&resp);
1103 /* *****************************************************************************
1107 * According to command line options prepare SNMP Get | GetNext | GetBulk PDU.
1108 * Wait for a responce and print it.
1111 * Do a 'snmp walk' - according to command line options request for values
1112 * lexicographically subsequent and subrooted at a common node. Send a GetNext
1113 * PDU requesting the value for each next variable and print the responce. Stop
1114 * when a Responce PDU is received that contains the value of a variable not
1115 * subrooted at the variable the walk started.
1118 main(int argc, char ** argv)
1120 struct snmp_toolinfo snmptoolctx;
1121 int32_t oid_cnt, last_oid, opt_num;
1124 /* Make sure program_name is set and valid. */
1126 program_name = "snmptool";
1128 program_name = strrchr(*argv, '/');
1129 if (program_name != NULL)
1132 program_name = *argv;
1135 if (program_name == NULL) {
1136 fprintf(stderr, "Error: No program name?\n");
1138 } else if (strcmp(program_name, "bsnmpget") == 0)
1140 else if (strcmp(program_name, "bsnmpwalk") == 0)
1141 program = BSNMPWALK;
1142 else if (strcmp(program_name, "bsnmpset") == 0)
1145 fprintf(stderr, "Unknown snmp tool name '%s'.\n", program_name);
1150 if (snmptool_init(&snmptoolctx) < 0)
1153 if ((opt_num = snmptool_parse_options(&snmptoolctx, argc, argv)) < 0) {
1154 snmp_tool_freeall(&snmptoolctx);
1155 /* On -h (help) exit without error. */
1162 oid_cnt = argc - opt_num - 1;
1166 if (!ISSET_EDISCOVER(&snmptoolctx) &&
1167 !ISSET_LOCALKEY(&snmptoolctx)) {
1168 fprintf(stderr, "No OID given.\n");
1170 snmp_tool_freeall(&snmptoolctx);
1176 if (snmp_object_add(&snmptoolctx, snmpwalk_add_default,
1179 "Error setting default subtree.\n");
1180 snmp_tool_freeall(&snmptoolctx);
1186 fprintf(stderr, "No OID given.\n");
1188 snmp_tool_freeall(&snmptoolctx);
1193 if (snmp_import_all(&snmptoolctx) < 0) {
1194 snmp_tool_freeall(&snmptoolctx);
1198 /* A simple sanity check - can not send GETBULK when using SNMPv1. */
1199 if (program == BSNMPGET && snmp_client.version == SNMP_V1 &&
1200 GET_PDUTYPE(&snmptoolctx) == SNMP_PDU_GETBULK) {
1201 fprintf(stderr, "Cannot send GETBULK PDU with SNMPv1.\n");
1202 snmp_tool_freeall(&snmptoolctx);
1206 for (last_oid = argc - 1; oid_cnt > 0; last_oid--, oid_cnt--) {
1207 if ((snmp_object_add(&snmptoolctx, (program == BSNMPSET) ?
1208 snmpset_parse_oid : snmptools_parse_oid,
1209 argv[last_oid])) < 0) {
1210 fprintf(stderr, "Error parsing OID string '%s'.\n",
1212 snmp_tool_freeall(&snmptoolctx);
1217 if (snmp_open(NULL, NULL, NULL, NULL)) {
1218 warnx("Failed to open snmp session: %s.", strerror(errno));
1219 snmp_tool_freeall(&snmptoolctx);
1223 if (snmp_client.version == SNMP_V3 && snmp_client.engine.engine_len == 0)
1224 SET_EDISCOVER(&snmptoolctx);
1226 if (ISSET_EDISCOVER(&snmptoolctx) &&
1227 snmp_discover_engine(snmptoolctx.passwd) < 0) {
1228 warnx("Unknown SNMP Engine ID: %s.", strerror(errno));
1233 if (GET_OUTPUT(&snmptoolctx) == OUTPUT_VERBOSE ||
1234 ISSET_EDISCOVER(&snmptoolctx))
1235 snmp_output_engine();
1237 if (snmp_client.version == SNMP_V3 && ISSET_LOCALKEY(&snmptoolctx) &&
1238 !ISSET_EDISCOVER(&snmptoolctx)) {
1239 if (snmp_passwd_to_keys(&snmp_client.user,
1240 snmptoolctx.passwd) != SNMP_CODE_OK ||
1241 snmp_get_local_keys(&snmp_client.user,
1242 snmp_client.engine.engine_id,
1243 snmp_client.engine.engine_len) != SNMP_CODE_OK) {
1244 warnx("Failed to get keys: %s.", strerror(errno));
1250 if (GET_OUTPUT(&snmptoolctx) == OUTPUT_VERBOSE ||
1251 ISSET_EDISCOVER(&snmptoolctx))
1254 if (ISSET_EDISCOVER(&snmptoolctx) && snmptoolctx.objects == 0)
1259 rc = snmptool_get(&snmptoolctx);
1262 rc = snmptool_walk(&snmptoolctx);
1265 rc = snmptool_set(&snmptoolctx);
1271 snmp_tool_freeall(&snmptoolctx);