net-snmp
5.4.1
|
00001 /* 00002 * snmp_parse_args.c 00003 */ 00004 /* Portions of this file are subject to the following copyright(s). See 00005 * the Net-SNMP's COPYING file for more details and other copyrights 00006 * that may apply: 00007 */ 00008 /* 00009 * Portions of this file are copyrighted by: 00010 * Copyright @ 2003 Sun Microsystems, Inc. All rights reserved. 00011 * Use is subject to license terms specified in the COPYING file 00012 * distributed with the Net-SNMP package. 00013 */ 00014 00015 #include <net-snmp/net-snmp-config.h> 00016 #include <errno.h> 00017 00018 #if HAVE_STDLIB_H 00019 #include <stdlib.h> 00020 #endif 00021 #if HAVE_UNISTD_H 00022 #include <unistd.h> 00023 #endif 00024 #if HAVE_STRING_H 00025 #include <string.h> 00026 #else 00027 #include <strings.h> 00028 #endif 00029 #include <sys/types.h> 00030 #include <stdio.h> 00031 #if HAVE_UNISTD_H 00032 #include <unistd.h> 00033 #endif 00034 #include <ctype.h> 00035 #if HAVE_NETINET_IN_H 00036 #include <netinet/in.h> 00037 #endif 00038 #if TIME_WITH_SYS_TIME 00039 # ifdef WIN32 00040 # include <sys/timeb.h> 00041 # else 00042 # include <sys/time.h> 00043 # endif 00044 # include <time.h> 00045 #else 00046 # if HAVE_SYS_TIME_H 00047 # include <sys/time.h> 00048 # else 00049 # include <time.h> 00050 # endif 00051 #endif 00052 #if HAVE_SYS_SELECT_H 00053 #include <sys/select.h> 00054 #endif 00055 #if HAVE_WINSOCK_H 00056 #include <winsock.h> 00057 #endif 00058 #if HAVE_NETDB_H 00059 #include <netdb.h> 00060 #endif 00061 #if HAVE_ARPA_INET_H 00062 #include <arpa/inet.h> 00063 #endif 00064 00065 #include <net-snmp/types.h> 00066 #include <net-snmp/output_api.h> 00067 #include <net-snmp/config_api.h> 00068 #include <net-snmp/library/snmp_parse_args.h> /* for "internal" definitions */ 00069 #include <net-snmp/utilities.h> 00070 00071 #include <net-snmp/library/snmp_api.h> 00072 #include <net-snmp/library/snmp_client.h> 00073 #include <net-snmp/library/mib.h> 00074 #include <net-snmp/library/scapi.h> 00075 #include <net-snmp/library/keytools.h> 00076 00077 #include <net-snmp/version.h> 00078 #include <net-snmp/library/parse.h> 00079 #include <net-snmp/library/snmpv3.h> 00080 #include <net-snmp/library/transform_oids.h> 00081 00082 int random_access = 0; 00083 00084 void 00085 snmp_parse_args_usage(FILE * outf) 00086 { 00087 fprintf(outf, "[OPTIONS] AGENT"); 00088 } 00089 00090 void 00091 snmp_parse_args_descriptions(FILE * outf) 00092 { 00093 fprintf(outf, " Version: %s\n", netsnmp_get_version()); 00094 fprintf(outf, " Web: http://www.net-snmp.org/\n"); 00095 fprintf(outf, 00096 " Email: net-snmp-coders@lists.sourceforge.net\n\nOPTIONS:\n"); 00097 fprintf(outf, " -h, --help\t\tdisplay this help message\n"); 00098 fprintf(outf, 00099 " -H\t\t\tdisplay configuration file directives understood\n"); 00100 fprintf(outf, " -v 1|2c|3\t\tspecifies SNMP version to use\n"); 00101 fprintf(outf, " -V, --version\t\tdisplay package version number\n"); 00102 #if !defined(NETSNMP_DISABLE_SNMPV1) || !defined(NETSNMP_DISABLE_SNMPV2C) 00103 fprintf(outf, "SNMP Version 1 or 2c specific\n"); 00104 fprintf(outf, " -c COMMUNITY\t\tset the community string\n"); 00105 #endif /* support for community based SNMP */ 00106 fprintf(outf, "SNMP Version 3 specific\n"); 00107 fprintf(outf, 00108 " -a PROTOCOL\t\tset authentication protocol (MD5|SHA)\n"); 00109 fprintf(outf, 00110 " -A PASSPHRASE\t\tset authentication protocol pass phrase\n"); 00111 fprintf(outf, 00112 " -e ENGINE-ID\t\tset security engine ID (e.g. 800000020109840301)\n"); 00113 fprintf(outf, 00114 " -E ENGINE-ID\t\tset context engine ID (e.g. 800000020109840301)\n"); 00115 fprintf(outf, 00116 " -l LEVEL\t\tset security level (noAuthNoPriv|authNoPriv|authPriv)\n"); 00117 fprintf(outf, " -n CONTEXT\t\tset context name (e.g. bridge1)\n"); 00118 fprintf(outf, " -u USER-NAME\t\tset security name (e.g. bert)\n"); 00119 #ifdef HAVE_AES 00120 fprintf(outf, " -x PROTOCOL\t\tset privacy protocol (DES|AES)\n"); 00121 #else 00122 fprintf(outf, " -x PROTOCOL\t\tset privacy protocol (DES)\n"); 00123 #endif 00124 fprintf(outf, " -X PASSPHRASE\t\tset privacy protocol pass phrase\n"); 00125 fprintf(outf, 00126 " -Z BOOTS,TIME\t\tset destination engine boots/time\n"); 00127 fprintf(outf, "General communication options\n"); 00128 fprintf(outf, " -r RETRIES\t\tset the number of retries\n"); 00129 fprintf(outf, 00130 " -t TIMEOUT\t\tset the request timeout (in seconds)\n"); 00131 fprintf(outf, "Debugging\n"); 00132 fprintf(outf, " -d\t\t\tdump input/output packets in hexadecimal\n"); 00133 fprintf(outf, 00134 " -D TOKEN[,...]\tturn on debugging output for the specified TOKENs\n\t\t\t (ALL gives extremely verbose debugging output)\n"); 00135 fprintf(outf, "General options\n"); 00136 fprintf(outf, 00137 " -m MIB[:...]\t\tload given list of MIBs (ALL loads everything)\n"); 00138 fprintf(outf, 00139 " -M DIR[:...]\t\tlook in given list of directories for MIBs\n"); 00140 #ifndef NETSNMP_DISABLE_MIB_LOADING 00141 fprintf(outf, 00142 " -P MIBOPTS\t\tToggle various defaults controlling MIB parsing:\n"); 00143 snmp_mib_toggle_options_usage("\t\t\t ", outf); 00144 #endif 00145 fprintf(outf, 00146 " -O OUTOPTS\t\tToggle various defaults controlling output display:\n"); 00147 snmp_out_toggle_options_usage("\t\t\t ", outf); 00148 fprintf(outf, 00149 " -I INOPTS\t\tToggle various defaults controlling input parsing:\n"); 00150 snmp_in_toggle_options_usage("\t\t\t ", outf); 00151 fprintf(outf, 00152 " -L LOGOPTS\t\tToggle various defaults controlling logging:\n"); 00153 snmp_log_options_usage("\t\t\t ", outf); 00154 fflush(outf); 00155 } 00156 00157 #define BUF_SIZE 512 00158 00159 void 00160 handle_long_opt(const char *myoptarg) 00161 { 00162 char *cp, *cp2; 00163 /* 00164 * else it's a long option, so process it like name=value 00165 */ 00166 cp = (char *)malloc(strlen(myoptarg) + 3); 00167 if (!cp) 00168 return; 00169 strcpy(cp, myoptarg); 00170 cp2 = strchr(cp, '='); 00171 if (!cp2 && !strchr(cp, ' ')) { 00172 /* 00173 * well, they didn't specify an argument as far as we 00174 * can tell. Give them a '1' as the argument (which 00175 * works for boolean tokens and a few others) and let 00176 * them suffer from there if it's not what they 00177 * wanted 00178 */ 00179 strcat(cp, " 1"); 00180 } else { 00181 /* 00182 * replace the '=' with a ' ' 00183 */ 00184 if (cp2) 00185 *cp2 = ' '; 00186 } 00187 netsnmp_config(cp); 00188 free(cp); 00189 } 00190 00191 extern int snmpv3_options(char *optarg, netsnmp_session * session, 00192 char **Apsz, char **Xpsz, int argc, 00193 char *const *argv); 00194 00195 /* 00196 * This method does the real work for snmp_parse_args. It takes an 00197 * extra argument, proxy, and uses this to decide how to handle the lack of 00198 * of a community string. 00199 */ 00200 int 00201 snmp_parse_args(int argc, 00202 char **argv, 00203 netsnmp_session * session, const char *localOpts, 00204 void (*proc) (int, char *const *, int)) 00205 { 00206 static char *sensitive[4] = { NULL, NULL, NULL, NULL }; 00207 int arg, sp = 0, zero_sensitive = 1, testcase = 0; 00208 char *cp; 00209 char *Apsz = NULL; 00210 char *Xpsz = NULL; 00211 char *Cpsz = NULL; 00212 char Opts[BUF_SIZE]; 00213 int logopt = 0; 00214 00215 /* 00216 * initialize session to default values 00217 */ 00218 snmp_sess_init(session); 00219 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:s:S:L:"); 00220 if (localOpts) 00221 strcat(Opts, localOpts); 00222 00223 if (strcmp(argv[0], "snmpd-trapsess") == 0 || 00224 strcmp(argv[0], "snmpd-proxy") == 0) { 00225 /* Don't worry about zeroing sensitive parameters as they are not 00226 on the command line anyway (called from internal config-line 00227 handler). */ 00228 zero_sensitive = 0; 00229 } 00230 00231 /* 00232 * get the options 00233 */ 00234 DEBUGMSGTL(("snmp_parse_args", "starting: %d/%d\n", optind, argc)); 00235 for (arg = 0; arg < argc; arg++) { 00236 DEBUGMSGTL(("snmp_parse_args", " arg %d = %s\n", arg, argv[arg])); 00237 } 00238 00239 optind = 1; 00240 while ((arg = getopt(argc, argv, Opts)) != EOF) { 00241 DEBUGMSGTL(("snmp_parse_args", "handling (#%d): %c\n", optind, arg)); 00242 switch (arg) { 00243 case '-': 00244 if (strcasecmp(optarg, "help") == 0) { 00245 return (-1); 00246 } 00247 if (strcasecmp(optarg, "version") == 0) { 00248 fprintf(stderr,"NET-SNMP version: %s\n",netsnmp_get_version()); 00249 return (-2); 00250 } 00251 00252 handle_long_opt(optarg); 00253 break; 00254 00255 case 'V': 00256 fprintf(stderr, "NET-SNMP version: %s\n", netsnmp_get_version()); 00257 return (-2); 00258 00259 case 'h': 00260 return (-1); 00261 break; 00262 00263 case 'H': 00264 init_snmp("snmpapp"); 00265 fprintf(stderr, "Configuration directives understood:\n"); 00266 read_config_print_usage(" "); 00267 return (-2); 00268 00269 case 'Y': 00270 netsnmp_config_remember(optarg); 00271 break; 00272 00273 #ifndef NETSNMP_DISABLE_MIB_LOADING 00274 case 'm': 00275 setenv("MIBS", optarg, 1); 00276 break; 00277 00278 case 'M': 00279 netsnmp_get_mib_directory(); /* prepare the default directories */ 00280 netsnmp_set_mib_directory(optarg); 00281 break; 00282 #endif /* NETSNMP_DISABLE_MIB_LOADING */ 00283 00284 case 'O': 00285 cp = snmp_out_toggle_options(optarg); 00286 if (cp != NULL) { 00287 fprintf(stderr, "Unknown output option passed to -O: %c.\n", 00288 *cp); 00289 return (-1); 00290 } 00291 break; 00292 00293 case 'I': 00294 cp = snmp_in_options(optarg, argc, argv); 00295 if (cp != NULL) { 00296 fprintf(stderr, "Unknown input option passed to -I: %c.\n", 00297 *cp); 00298 return (-1); 00299 } 00300 break; 00301 00302 #ifndef NETSNMP_DISABLE_MIB_LOADING 00303 case 'P': 00304 cp = snmp_mib_toggle_options(optarg); 00305 if (cp != NULL) { 00306 fprintf(stderr, 00307 "Unknown parsing option passed to -P: %c.\n", *cp); 00308 return (-1); 00309 } 00310 break; 00311 #endif /* NETSNMP_DISABLE_MIB_LOADING */ 00312 00313 case 'D': 00314 debug_register_tokens(optarg); 00315 snmp_set_do_debugging(1); 00316 break; 00317 00318 case 'd': 00319 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, 00320 NETSNMP_DS_LIB_DUMP_PACKET, 1); 00321 break; 00322 00323 case 'v': 00324 session->version = -1; 00325 #ifndef NETSNMP_DISABLE_SNMPV1 00326 if (!strcmp(optarg, "1")) { 00327 session->version = SNMP_VERSION_1; 00328 } 00329 #endif 00330 #ifndef NETSNMP_DISABLE_SNMPV2C 00331 if (!strcasecmp(optarg, "2c")) { 00332 session->version = SNMP_VERSION_2c; 00333 } 00334 #endif 00335 if (!strcasecmp(optarg, "3")) { 00336 session->version = SNMP_VERSION_3; 00337 } 00338 if (session->version == -1) { 00339 fprintf(stderr, 00340 "Invalid version specified after -v flag: %s\n", 00341 optarg); 00342 return (-1); 00343 } 00344 break; 00345 00346 case 'p': 00347 fprintf(stderr, "Warning: -p option is no longer used - "); 00348 fprintf(stderr, "specify the remote host as HOST:PORT\n"); 00349 return (-1); 00350 break; 00351 00352 case 'T': 00353 fprintf(stderr, "Warning: -T option is no longer used - "); 00354 fprintf(stderr, "specify the remote host as TRANSPORT:HOST\n"); 00355 return (-1); 00356 break; 00357 00358 case 't': 00359 session->timeout = atoi(optarg) * 1000000L; 00360 if (session->timeout < 0 || !isdigit(optarg[0])) { 00361 fprintf(stderr, "Invalid timeout in seconds after -t flag.\n"); 00362 return (-1); 00363 } 00364 break; 00365 00366 case 'r': 00367 session->retries = atoi(optarg); 00368 if (session->retries < 0 || !isdigit(optarg[0])) { 00369 fprintf(stderr, "Invalid number of retries after -r flag.\n"); 00370 return (-1); 00371 } 00372 break; 00373 00374 case 'c': 00375 if (zero_sensitive) { 00376 if ((sensitive[sp] = strdup(optarg)) != NULL) { 00377 Cpsz = sensitive[sp]; 00378 memset(optarg, '\0', strlen(optarg)); 00379 sp++; 00380 } else { 00381 fprintf(stderr, "malloc failure processing -c flag.\n"); 00382 return -1; 00383 } 00384 } else { 00385 Cpsz = optarg; 00386 } 00387 break; 00388 00389 case '3': 00390 /* TODO: This needs to zero things too. */ 00391 if (snmpv3_options(optarg, session, &Apsz, &Xpsz, argc, argv) < 0){ 00392 return (-1); 00393 } 00394 break; 00395 00396 case 'L': 00397 if (snmp_log_options(optarg, argc, argv) < 0) { 00398 return (-1); 00399 } 00400 logopt = 1; 00401 break; 00402 00403 #define SNMPV3_CMD_OPTIONS 00404 #ifdef SNMPV3_CMD_OPTIONS 00405 case 'Z': 00406 errno = 0; 00407 session->engineBoots = strtoul(optarg, &cp, 10); 00408 if (errno || cp == optarg) { 00409 fprintf(stderr, "Need engine boots value after -Z flag.\n"); 00410 return (-1); 00411 } 00412 if (*cp == ',') { 00413 char *endptr; 00414 cp++; 00415 session->engineTime = strtoul(cp, &endptr, 10); 00416 if (errno || cp == endptr) { 00417 fprintf(stderr, "Need engine time after \"-Z engineBoot,\".\n"); 00418 return (-1); 00419 } 00420 } 00421 /* 00422 * Handle previous '-Z boot time' syntax 00423 */ 00424 else if (optind < argc) { 00425 session->engineTime = strtoul(argv[optind], &cp, 10); 00426 if (errno || cp == argv[optind]) { 00427 fprintf(stderr, "Need engine time after \"-Z engineBoot\".\n"); 00428 return (-1); 00429 } 00430 } else { 00431 fprintf(stderr, "Need engine time after \"-Z engineBoot\".\n"); 00432 return (-1); 00433 } 00434 break; 00435 00436 case 'e':{ 00437 size_t ebuf_len = 32, eout_len = 0; 00438 u_char *ebuf = (u_char *)malloc(ebuf_len); 00439 00440 if (ebuf == NULL) { 00441 fprintf(stderr, "malloc failure processing -e flag.\n"); 00442 return (-1); 00443 } 00444 if (!snmp_hex_to_binary 00445 (&ebuf, &ebuf_len, &eout_len, 1, optarg)) { 00446 fprintf(stderr, "Bad engine ID value after -e flag.\n"); 00447 free(ebuf); 00448 return (-1); 00449 } 00450 if ((eout_len < 5) || (eout_len > 32)) { 00451 fprintf(stderr, "Invalid engine ID value after -e flag.\n"); 00452 free(ebuf); 00453 return (-1); 00454 } 00455 session->securityEngineID = ebuf; 00456 session->securityEngineIDLen = eout_len; 00457 break; 00458 } 00459 00460 case 'E':{ 00461 size_t ebuf_len = 32, eout_len = 0; 00462 u_char *ebuf = (u_char *)malloc(ebuf_len); 00463 00464 if (ebuf == NULL) { 00465 fprintf(stderr, "malloc failure processing -E flag.\n"); 00466 return (-1); 00467 } 00468 if (!snmp_hex_to_binary(&ebuf, &ebuf_len, 00469 &eout_len, 1, optarg)) { 00470 fprintf(stderr, "Bad engine ID value after -E flag.\n"); 00471 free(ebuf); 00472 return (-1); 00473 } 00474 if ((eout_len < 5) || (eout_len > 32)) { 00475 fprintf(stderr, "Invalid engine ID value after -E flag.\n"); 00476 free(ebuf); 00477 return (-1); 00478 } 00479 session->contextEngineID = ebuf; 00480 session->contextEngineIDLen = eout_len; 00481 break; 00482 } 00483 00484 case 'n': 00485 session->contextName = optarg; 00486 session->contextNameLen = strlen(optarg); 00487 break; 00488 00489 case 'u': 00490 if (zero_sensitive) { 00491 if ((sensitive[sp] = strdup(optarg)) != NULL) { 00492 session->securityName = sensitive[sp]; 00493 session->securityNameLen = strlen(sensitive[sp]); 00494 memset(optarg, '\0', strlen(optarg)); 00495 sp++; 00496 } else { 00497 fprintf(stderr, "malloc failure processing -u flag.\n"); 00498 return -1; 00499 } 00500 } else { 00501 session->securityName = optarg; 00502 session->securityNameLen = strlen(optarg); 00503 } 00504 break; 00505 00506 case 'l': 00507 if (!strcasecmp(optarg, "noAuthNoPriv") || !strcmp(optarg, "1") 00508 || !strcasecmp(optarg, "noauth") 00509 || !strcasecmp(optarg, "nanp")) { 00510 session->securityLevel = SNMP_SEC_LEVEL_NOAUTH; 00511 } else if (!strcasecmp(optarg, "authNoPriv") 00512 || !strcmp(optarg, "2") 00513 || !strcasecmp(optarg, "auth") 00514 || !strcasecmp(optarg, "anp")) { 00515 session->securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV; 00516 } else if (!strcasecmp(optarg, "authPriv") 00517 || !strcmp(optarg, "3") 00518 || !strcasecmp(optarg, "priv") 00519 || !strcasecmp(optarg, "ap")) { 00520 session->securityLevel = SNMP_SEC_LEVEL_AUTHPRIV; 00521 } else { 00522 fprintf(stderr, 00523 "Invalid security level specified after -l flag: %s\n", 00524 optarg); 00525 return (-1); 00526 } 00527 00528 break; 00529 00530 case 'a': 00531 #ifndef NETSNMP_DISABLE_MD5 00532 if (!strcasecmp(optarg, "MD5")) { 00533 session->securityAuthProto = usmHMACMD5AuthProtocol; 00534 session->securityAuthProtoLen = USM_AUTH_PROTO_MD5_LEN; 00535 } else 00536 #endif 00537 if (!strcasecmp(optarg, "SHA")) { 00538 session->securityAuthProto = usmHMACSHA1AuthProtocol; 00539 session->securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN; 00540 } else { 00541 fprintf(stderr, 00542 "Invalid authentication protocol specified after -a flag: %s\n", 00543 optarg); 00544 return (-1); 00545 } 00546 break; 00547 00548 case 'x': 00549 testcase = 0; 00550 #ifndef NETSNMP_DISABLE_DES 00551 if (!strcasecmp(optarg, "DES")) { 00552 testcase = 1; 00553 session->securityPrivProto = usmDESPrivProtocol; 00554 session->securityPrivProtoLen = USM_PRIV_PROTO_DES_LEN; 00555 } 00556 #endif 00557 #ifdef HAVE_AES 00558 if (!strcasecmp(optarg, "AES128") || 00559 !strcasecmp(optarg, "AES")) { 00560 testcase = 1; 00561 session->securityPrivProto = usmAESPrivProtocol; 00562 session->securityPrivProtoLen = USM_PRIV_PROTO_AES_LEN; 00563 } 00564 #endif 00565 if (testcase == 0) { 00566 fprintf(stderr, 00567 "Invalid privacy protocol specified after -x flag: %s\n", 00568 optarg); 00569 return (-1); 00570 } 00571 break; 00572 00573 case 'A': 00574 if (zero_sensitive) { 00575 if ((sensitive[sp] = strdup(optarg)) != NULL) { 00576 Apsz = sensitive[sp]; 00577 memset(optarg, '\0', strlen(optarg)); 00578 sp++; 00579 } else { 00580 fprintf(stderr, "malloc failure processing -A flag.\n"); 00581 return -1; 00582 } 00583 } else { 00584 Apsz = optarg; 00585 } 00586 break; 00587 00588 case 'X': 00589 if (zero_sensitive) { 00590 if ((sensitive[sp] = strdup(optarg)) != NULL) { 00591 Xpsz = sensitive[sp]; 00592 memset(optarg, '\0', strlen(optarg)); 00593 sp++; 00594 } else { 00595 fprintf(stderr, "malloc failure processing -X flag.\n"); 00596 return -1; 00597 } 00598 } else { 00599 Xpsz = optarg; 00600 } 00601 break; 00602 #endif /* SNMPV3_CMD_OPTIONS */ 00603 00604 case '?': 00605 return (-1); 00606 break; 00607 00608 default: 00609 proc(argc, argv, arg); 00610 break; 00611 } 00612 } 00613 DEBUGMSGTL(("snmp_parse_args", "finished: %d/%d\n", optind, argc)); 00614 00615 if (!logopt) 00616 snmp_enable_stderrlog(); 00617 00618 /* 00619 * read in MIB database and initialize the snmp library 00620 */ 00621 init_snmp("snmpapp"); 00622 00623 /* 00624 * session default version 00625 */ 00626 if (session->version == SNMP_DEFAULT_VERSION) { 00627 /* 00628 * run time default version 00629 */ 00630 session->version = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, 00631 NETSNMP_DS_LIB_SNMPVERSION); 00632 00633 /* 00634 * compile time default version 00635 */ 00636 if (!session->version) { 00637 switch (NETSNMP_DEFAULT_SNMP_VERSION) { 00638 #ifndef NETSNMP_DISABLE_SNMPV1 00639 case 1: 00640 session->version = SNMP_VERSION_1; 00641 break; 00642 #endif 00643 00644 #ifndef NETSNMP_DISABLE_SNMPV2C 00645 case 2: 00646 session->version = SNMP_VERSION_2c; 00647 break; 00648 #endif 00649 00650 case 3: 00651 session->version = SNMP_VERSION_3; 00652 break; 00653 00654 default: 00655 snmp_log(LOG_ERR, "Can't determine a valid SNMP version for the session\n"); 00656 return(-2); 00657 } 00658 } else { 00659 #ifndef NETSNMP_DISABLE_SNMPV1 00660 if (session->version == NETSNMP_DS_SNMP_VERSION_1) /* bogus value. version 1 actually = 0 */ 00661 session->version = SNMP_VERSION_1; 00662 #endif 00663 } 00664 } 00665 00666 /* 00667 * make master key from pass phrases 00668 */ 00669 if (Apsz) { 00670 session->securityAuthKeyLen = USM_AUTH_KU_LEN; 00671 if (session->securityAuthProto == NULL) { 00672 /* 00673 * get .conf set default 00674 */ 00675 const oid *def = 00676 get_default_authtype(&session->securityAuthProtoLen); 00677 session->securityAuthProto = 00678 snmp_duplicate_objid(def, session->securityAuthProtoLen); 00679 } 00680 if (session->securityAuthProto == NULL) { 00681 #ifndef NETSNMP_DISABLE_MD5 00682 /* 00683 * assume MD5 00684 */ 00685 session->securityAuthProto = 00686 snmp_duplicate_objid(usmHMACMD5AuthProtocol, 00687 USM_AUTH_PROTO_MD5_LEN); 00688 session->securityAuthProtoLen = USM_AUTH_PROTO_MD5_LEN; 00689 #else 00690 session->securityAuthProto = 00691 snmp_duplicate_objid(usmHMACSHA1AuthProtocol, 00692 USM_AUTH_PROTO_SHA_LEN); 00693 session->securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN; 00694 #endif 00695 } 00696 if (generate_Ku(session->securityAuthProto, 00697 session->securityAuthProtoLen, 00698 (u_char *) Apsz, strlen(Apsz), 00699 session->securityAuthKey, 00700 &session->securityAuthKeyLen) != SNMPERR_SUCCESS) { 00701 snmp_perror(argv[0]); 00702 fprintf(stderr, 00703 "Error generating a key (Ku) from the supplied authentication pass phrase. \n"); 00704 return (-2); 00705 } 00706 } 00707 if (Xpsz) { 00708 session->securityPrivKeyLen = USM_PRIV_KU_LEN; 00709 if (session->securityPrivProto == NULL) { 00710 /* 00711 * get .conf set default 00712 */ 00713 const oid *def = 00714 get_default_privtype(&session->securityPrivProtoLen); 00715 session->securityPrivProto = 00716 snmp_duplicate_objid(def, session->securityPrivProtoLen); 00717 } 00718 if (session->securityPrivProto == NULL) { 00719 /* 00720 * assume DES 00721 */ 00722 #ifndef NETSNMP_DISABLE_DES 00723 session->securityPrivProto = 00724 snmp_duplicate_objid(usmDESPrivProtocol, 00725 USM_PRIV_PROTO_DES_LEN); 00726 session->securityPrivProtoLen = USM_PRIV_PROTO_DES_LEN; 00727 #else 00728 session->securityPrivProto = 00729 snmp_duplicate_objid(usmAESPrivProtocol, 00730 USM_PRIV_PROTO_AES_LEN); 00731 session->securityPrivProtoLen = USM_PRIV_PROTO_AES_LEN; 00732 #endif 00733 00734 } 00735 if (generate_Ku(session->securityAuthProto, 00736 session->securityAuthProtoLen, 00737 (u_char *) Xpsz, strlen(Xpsz), 00738 session->securityPrivKey, 00739 &session->securityPrivKeyLen) != SNMPERR_SUCCESS) { 00740 snmp_perror(argv[0]); 00741 fprintf(stderr, 00742 "Error generating a key (Ku) from the supplied privacy pass phrase. \n"); 00743 return (-2); 00744 } 00745 } 00746 /* 00747 * get the hostname 00748 */ 00749 if (optind == argc) { 00750 fprintf(stderr, "No hostname specified.\n"); 00751 return (-1); 00752 } 00753 session->peername = argv[optind++]; /* hostname */ 00754 00755 #if !defined(NETSNMP_DISABLE_SNMPV1) || !defined(NETSNMP_DISABLE_SNMPV2C) 00756 /* 00757 * If v1 or v2c, check community has been set, either by a -c option above, 00758 * or via a default token somewhere. 00759 * If neither, it will be taken from the incoming request PDU. 00760 */ 00761 00762 #if defined(NETSNMP_DISABLE_SNMPV1) 00763 if (session->version == SNMP_VERSION_2c) 00764 #else 00765 #if defined(NETSNMP_DISABLE_SNMPV2C) 00766 if (session->version == SNMP_VERSION_1) 00767 #else 00768 if (session->version == SNMP_VERSION_1 || 00769 session->version == SNMP_VERSION_2c) 00770 #endif 00771 #endif 00772 { 00773 if (Cpsz == NULL) { 00774 Cpsz = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 00775 NETSNMP_DS_LIB_COMMUNITY); 00776 if (Cpsz == NULL) { 00777 if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 00778 NETSNMP_DS_LIB_IGNORE_NO_COMMUNITY)){ 00779 DEBUGMSGTL(("snmp_parse_args", 00780 "ignoring that the community string is not present\n")); 00781 session->community = NULL; 00782 session->community_len = 0; 00783 } else { 00784 fprintf(stderr, "No community name specified.\n"); 00785 return (-1); 00786 } 00787 } 00788 } else { 00789 session->community = (unsigned char *)Cpsz; 00790 session->community_len = strlen(Cpsz); 00791 } 00792 } 00793 #endif /* support for community based SNMP */ 00794 00795 return optind; 00796 }