1 #ifdef BRCM_SNMP_CONFIG_SUPPORT
7 #include <net-snmp/net-snmp-config.h>
20 #include <sys/types.h>
27 #include <netinet/in.h>
29 #if TIME_WITH_SYS_TIME
31 # include <sys/timeb.h>
33 # include <sys/time.h>
38 # include <sys/time.h>
44 #include <sys/select.h>
53 #include <arpa/inet.h>
56 #include <net-snmp/types.h>
57 #include <net-snmp/output_api.h>
58 #include <net-snmp/config_api.h>
59 #include <net-snmp/library/snmp_parse_args.h> /* for "internal" definitions */
60 #include <net-snmp/utilities.h>
62 #include <net-snmp/library/snmp_api.h>
63 #include <net-snmp/library/snmp_client.h>
64 #include <net-snmp/library/mib.h>
65 #include <net-snmp/library/scapi.h>
66 #include <net-snmp/library/keytools.h>
68 #include <net-snmp/version.h>
69 #include <net-snmp/library/parse.h>
70 #include <net-snmp/library/snmpv3.h>
71 #include <net-snmp/library/transform_oids.h>
73 int random_access = 0;
75 snmp_parse_args_usage(FILE * outf)
77 fprintf(outf, "[OPTIONS] AGENT");
81 snmp_parse_args_descriptions(FILE * outf)
83 fprintf(outf, " Version: %s\n", netsnmp_get_version());
84 fprintf(outf, " Web: http://www.net-snmp.org/\n");
86 " Email: net-snmp-coders@lists.sourceforge.net\n\nOPTIONS:\n");
87 fprintf(outf, " -h, --help\t\tdisplay this help message\n");
89 " -H\t\t\tdisplay configuration file directives understood\n");
90 fprintf(outf, " -v 1|2c|3\t\tspecifies SNMP version to use\n");
91 fprintf(outf, " -V, --version\t\tdisplay package version number\n");
92 fprintf(outf, "SNMP Version 1 or 2c specific\n");
93 fprintf(outf, " -c COMMUNITY\t\tset the community string\n");
94 fprintf(outf, "SNMP Version 3 specific\n");
96 " -a PROTOCOL\t\tset authentication protocol (MD5|SHA)\n");
98 " -A PASSPHRASE\t\tset authentication protocol pass phrase\n");
100 " -e ENGINE-ID\t\tset security engine ID (e.g. 800000020109840301)\n");
102 " -E ENGINE-ID\t\tset context engine ID (e.g. 800000020109840301)\n");
104 " -l LEVEL\t\tset security level (noAuthNoPriv|authNoPriv|authPriv)\n");
105 fprintf(outf, " -n CONTEXT\t\tset context name (e.g. bridge1)\n");
106 fprintf(outf, " -u USER-NAME\t\tset security name (e.g. bert)\n");
108 fprintf(outf, " -x PROTOCOL\t\tset privacy protocol (DES|AES)\n");
110 fprintf(outf, " -x PROTOCOL\t\tset privacy protocol (DES)\n");
112 fprintf(outf, " -X PASSPHRASE\t\tset privacy protocol pass phrase\n");
114 " -Z BOOTS,TIME\t\tset destination engine boots/time\n");
115 fprintf(outf, "General communication options\n");
116 fprintf(outf, " -r RETRIES\t\tset the number of retries\n");
118 " -t TIMEOUT\t\tset the request timeout (in seconds)\n");
119 fprintf(outf, "Debugging\n");
120 fprintf(outf, " -d\t\t\tdump input/output packets in hexadecimal\n");
122 " -D TOKEN[,...]\tturn on debugging output for the specified TOKENs\n\t\t\t (ALL gives extremely verbose debugging output)\n");
123 fprintf(outf, "General options\n");
125 " -m MIB[:...]\t\tload given list of MIBs (ALL loads everything)\n");
127 " -M DIR[:...]\t\tlook in given list of directories for MIBs\n");
129 " -P MIBOPTS\t\tToggle various defaults controlling MIB parsing:\n");
130 snmp_mib_toggle_options_usage("\t\t\t ", outf);
132 " -O OUTOPTS\t\tToggle various defaults controlling output display:\n");
133 snmp_out_toggle_options_usage("\t\t\t ", outf);
135 " -I INOPTS\t\tToggle various defaults controlling input parsing:\n");
136 snmp_in_toggle_options_usage("\t\t\t ", outf);
141 #endif /* BRCM_SNMP_CONFIG_SUPPORT */
143 handle_long_opt(const char *myoptarg)
147 * else it's a long option, so process it like name=value
149 cp = malloc(strlen(myoptarg) + 3);
150 strcpy(cp, myoptarg);
151 cp2 = strchr(cp, '=');
152 if (!cp2 && !strchr(cp, ' ')) {
154 * well, they didn't specify an argument as far as we
155 * can tell. Give them a '1' as the argument (which
156 * works for boolean tokens and a few others) and let
157 * them suffer from there if it's not what they
163 * replace the '=' with a ' '
171 #ifdef BRCM_SNMP_CONFIG_SUPPORT
172 extern int snmpv3_options(char *optarg, netsnmp_session * session,
173 char **Apsz, char **Xpsz, int argc,
176 snmp_parse_args(int argc,
178 netsnmp_session * session, const char *localOpts,
179 void (*proc) (int, char *const *, int))
189 * initialize session to default values
191 snmp_sess_init(session);
192 strcpy(Opts, "Y:VhHm:M:O:I:P:D:dv:r:t:c:Z:e:E:n:u:l:x:X:a:A:p:T:-:3:");
194 strcat(Opts, localOpts);
199 DEBUGMSGTL(("snmp_parse_args", "starting: %d/%d\n", optind, argc));
200 for (arg = 0; arg < argc; arg++) {
201 DEBUGMSGTL(("snmp_parse_args", " arg %d = %s\n", arg, argv[arg]));
205 while ((arg = getopt(argc, argv, Opts)) != EOF) {
206 DEBUGMSGTL(("snmp_parse_args", "handling (#%d): %c\n", optind,
210 if (strcasecmp(optarg, "help") == 0) {
213 if (strcasecmp(optarg, "version") == 0) {
214 fprintf(stderr, "NET-SNMP version: %s\n",
215 netsnmp_get_version());
219 handle_long_opt(optarg);
223 fprintf(stderr, "NET-SNMP version: %s\n",
224 netsnmp_get_version());
232 init_snmp("snmpapp");
233 fprintf(stderr, "Configuration directives understood:\n");
234 read_config_print_usage(" ");
238 netsnmp_config_remember(optarg);
242 setenv("MIBS", optarg, 1);
246 netsnmp_set_mib_directory(optarg);
250 cp = snmp_out_toggle_options(optarg);
253 "Unknown output option passed to -O: %c.\n", *cp);
259 cp = snmp_in_toggle_options(optarg);
261 fprintf(stderr, "Unknown input option passed to -I: %c.\n",
268 cp = snmp_mib_toggle_options(optarg);
271 "Unknown parsing option passed to -P: %c.\n", *cp);
277 debug_register_tokens(optarg);
278 snmp_set_do_debugging(1);
282 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
283 NETSNMP_DS_LIB_DUMP_PACKET, 1);
287 if (!strcmp(optarg, "1")) {
288 session->version = SNMP_VERSION_1;
289 } else if (!strcasecmp(optarg, "2c")) {
290 session->version = SNMP_VERSION_2c;
291 } else if (!strcasecmp(optarg, "3")) {
292 session->version = SNMP_VERSION_3;
295 "Invalid version specified after -v flag: %s\n",
302 fprintf(stderr, "Warning: -p option is no longer used - ");
303 fprintf(stderr, "specify the remote host as HOST:PORT\n");
308 fprintf(stderr, "Warning: -T option is no longer used - ");
309 fprintf(stderr, "specify the remote host as TRANSPORT:HOST\n");
314 session->timeout = atoi(optarg) * 1000000L;
315 if (session->timeout < 0 || !isdigit(optarg[0])) {
317 "Invalid timeout in seconds after -t flag.\n");
323 session->retries = atoi(optarg);
324 if (session->retries < 0 || !isdigit(optarg[0])) {
326 "Invalid number of retries after -r flag.\n");
336 if (snmpv3_options(optarg, session, &Apsz, &Xpsz, argc, argv) <
342 #define SNMPV3_CMD_OPTIONS
343 #ifdef SNMPV3_CMD_OPTIONS
345 session->engineBoots = strtoul(optarg, NULL, 10);
346 if (session->engineBoots == 0 || !isdigit(optarg[0])) {
348 "Need engine boots value after -Z flag.\n");
351 cp = strchr(optarg, ',');
352 if (cp && *(++cp) && isdigit(*cp))
353 session->engineTime = strtoul(cp, NULL, 10);
355 * Handle previous '-Z boot time' syntax
357 else if ((optind < argc) && isdigit(argv[optind][0]))
358 session->engineTime = strtoul(argv[optind], NULL, 10);
360 fprintf(stderr, "Need engine time value after -Z flag.\n");
366 size_t ebuf_len = 32, eout_len = 0;
367 u_char *ebuf = (u_char *) malloc(ebuf_len);
371 "malloc failure processing -e flag.\n");
374 if (!snmp_hex_to_binary
375 (&ebuf, &ebuf_len, &eout_len, 1, optarg)) {
377 "Bad engine ID value after -e flag.\n");
381 session->securityEngineID = ebuf;
382 session->securityEngineIDLen = eout_len;
387 size_t ebuf_len = 32, eout_len = 0;
388 u_char *ebuf = (u_char *) malloc(ebuf_len);
392 "malloc failure processing -E flag.\n");
395 if (!snmp_hex_to_binary
396 (&ebuf, &ebuf_len, &eout_len, 1, optarg)) {
398 "Bad engine ID value after -E flag.\n");
402 session->contextEngineID = ebuf;
403 session->contextEngineIDLen = eout_len;
408 session->contextName = optarg;
409 session->contextNameLen = strlen(optarg);
413 session->securityName = optarg;
414 session->securityNameLen = strlen(optarg);
418 if (!strcasecmp(optarg, "noAuthNoPriv") || !strcmp(optarg, "1")
419 || !strcasecmp(optarg, "nanp")) {
420 session->securityLevel = SNMP_SEC_LEVEL_NOAUTH;
421 } else if (!strcasecmp(optarg, "authNoPriv")
422 || !strcmp(optarg, "2")
423 || !strcasecmp(optarg, "anp")) {
424 session->securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;
425 } else if (!strcasecmp(optarg, "authPriv")
426 || !strcmp(optarg, "3")
427 || !strcasecmp(optarg, "ap")) {
428 session->securityLevel = SNMP_SEC_LEVEL_AUTHPRIV;
431 "Invalid security level specified after -l flag: %s\n",
439 if (!strcasecmp(optarg, "MD5")) {
440 session->securityAuthProto = usmHMACMD5AuthProtocol;
441 session->securityAuthProtoLen = USM_AUTH_PROTO_MD5_LEN;
442 } else if (!strcasecmp(optarg, "SHA")) {
443 session->securityAuthProto = usmHMACSHA1AuthProtocol;
444 session->securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN;
447 "Invalid authentication protocol specified after -a flag: %s\n",
454 if (!strcasecmp(optarg, "DES")) {
455 session->securityPrivProto = usmDESPrivProtocol;
456 session->securityPrivProtoLen = USM_PRIV_PROTO_DES_LEN;
458 } else if (!strcasecmp(optarg, "AES128")) {
459 session->securityPrivProto = usmAES128PrivProtocol;
460 session->securityPrivProtoLen = USM_PRIV_PROTO_AES128_LEN;
461 } else if (!strcasecmp(optarg, "AES192")) {
462 session->securityPrivProto = usmAES192PrivProtocol;
463 session->securityPrivProtoLen = USM_PRIV_PROTO_AES192_LEN;
464 } else if (!strcasecmp(optarg, "AES256")) {
465 session->securityPrivProto = usmAES256PrivProtocol;
466 session->securityPrivProtoLen = USM_PRIV_PROTO_AES256_LEN;
470 "Invalid privacy protocol specified after -x flag: %s\n",
483 #endif /* SNMPV3_CMD_OPTIONS */
490 proc(argc, argv, arg);
494 DEBUGMSGTL(("snmp_parse_args", "finished: %d/%d\n", optind, argc));
497 * read in MIB database and initialize the snmp library
499 init_snmp("snmpapp");
502 * session default version
504 if (session->version == SNMP_DEFAULT_VERSION) {
506 * run time default version
508 session->version = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID,
509 NETSNMP_DS_LIB_SNMPVERSION);
512 * compile time default version
514 if (!session->version) {
515 switch (DEFAULT_SNMP_VERSION) {
517 session->version = SNMP_VERSION_1;
521 session->version = SNMP_VERSION_2c;
525 session->version = SNMP_VERSION_3;
529 if (session->version == NETSNMP_DS_SNMP_VERSION_1) /* bogus value. version 1 actually = 0 */
530 session->version = SNMP_VERSION_1;
535 * make master key from pass phrases
538 session->securityAuthKeyLen = USM_AUTH_KU_LEN;
539 if (session->securityAuthProto == NULL) {
541 * get .conf set default
544 get_default_authtype(&session->securityAuthProtoLen);
545 session->securityAuthProto =
546 snmp_duplicate_objid(def, session->securityAuthProtoLen);
548 if (session->securityAuthProto == NULL) {
552 session->securityAuthProto =
553 snmp_duplicate_objid(usmHMACMD5AuthProtocol,
554 USM_AUTH_PROTO_MD5_LEN);
555 session->securityAuthProtoLen = USM_AUTH_PROTO_MD5_LEN;
557 if (generate_Ku(session->securityAuthProto,
558 session->securityAuthProtoLen,
559 (u_char *) Apsz, strlen(Apsz),
560 session->securityAuthKey,
561 &session->securityAuthKeyLen) != SNMPERR_SUCCESS) {
562 snmp_perror(argv[0]);
564 "Error generating a key (Ku) from the supplied authentication pass phrase. \n");
569 session->securityPrivKeyLen = USM_PRIV_KU_LEN;
570 if (session->securityPrivProto == NULL) {
572 * get .conf set default
575 get_default_privtype(&session->securityPrivProtoLen);
576 session->securityPrivProto =
577 snmp_duplicate_objid(def, session->securityPrivProtoLen);
579 if (session->securityPrivProto == NULL) {
583 session->securityPrivProto =
584 snmp_duplicate_objid(usmDESPrivProtocol,
585 USM_PRIV_PROTO_DES_LEN);
586 session->securityPrivProtoLen = USM_PRIV_PROTO_DES_LEN;
588 if (generate_Ku(session->securityAuthProto,
589 session->securityAuthProtoLen,
590 (u_char *) Xpsz, strlen(Xpsz),
591 session->securityPrivKey,
592 &session->securityPrivKeyLen) != SNMPERR_SUCCESS) {
593 snmp_perror(argv[0]);
595 "Error generating a key (Ku) from the supplied privacy pass phrase. \n");
602 if (optind == argc) {
603 fprintf(stderr, "No hostname specified.\n");
606 session->peername = argv[optind++]; /* hostname */
609 * If v1 or v2c, check community has been set, either by a -c option above,
610 * or via a default token somewhere.
613 if (session->version == SNMP_VERSION_1 ||
614 session->version == SNMP_VERSION_2c) {
616 Cpsz = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
617 NETSNMP_DS_LIB_COMMUNITY);
620 fprintf(stderr, "No community name specified.\n");
623 session->community = (unsigned char *) Cpsz;
624 session->community_len = strlen(Cpsz);
629 #endif /* BRCM_SNMP_CONFIG_SUPPORT */