Blame snmplib/snmp_parse_args.c

Packit Service b38f0b
/*
Packit Service b38f0b
 * snmp_parse_args.c
Packit Service b38f0b
 */
Packit Service b38f0b
/* Portions of this file are subject to the following copyright(s).  See
Packit Service b38f0b
 * the Net-SNMP's COPYING file for more details and other copyrights
Packit Service b38f0b
 * that may apply:
Packit Service b38f0b
 */
Packit Service b38f0b
/*
Packit Service b38f0b
 * Portions of this file are copyrighted by:
Packit Service b38f0b
 * Copyright @ 2003 Sun Microsystems, Inc. All rights reserved.
Packit Service b38f0b
 * Use is subject to license terms specified in the COPYING file
Packit Service b38f0b
 * distributed with the Net-SNMP package.
Packit Service b38f0b
 *
Packit Service b38f0b
 * Portions of this file are copyrighted by:
Packit Service b38f0b
 * Copyright (c) 2016 VMware, Inc. All rights reserved.
Packit Service b38f0b
 * Use is subject to license terms specified in the COPYING file
Packit Service b38f0b
 * distributed with the Net-SNMP package.
Packit Service b38f0b
 */
Packit Service b38f0b
Packit Service b38f0b
#include <net-snmp/net-snmp-config.h>
Packit Service b38f0b
#include <errno.h>
Packit Service b38f0b
Packit Service b38f0b
#if HAVE_STDLIB_H
Packit Service b38f0b
#include <stdlib.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
#if HAVE_UNISTD_H
Packit Service b38f0b
#include <unistd.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
#if HAVE_STRING_H
Packit Service b38f0b
#include <string.h>
Packit Service b38f0b
#else
Packit Service b38f0b
#include <strings.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
#include <sys/types.h>
Packit Service b38f0b
#include <stdio.h>
Packit Service b38f0b
#if HAVE_UNISTD_H
Packit Service b38f0b
#include <unistd.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
#include <ctype.h>
Packit Service b38f0b
#if HAVE_NETINET_IN_H
Packit Service b38f0b
#include <netinet/in.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
#if TIME_WITH_SYS_TIME
Packit Service b38f0b
# include <sys/time.h>
Packit Service b38f0b
# include <time.h>
Packit Service b38f0b
#else
Packit Service b38f0b
# if HAVE_SYS_TIME_H
Packit Service b38f0b
#  include <sys/time.h>
Packit Service b38f0b
# else
Packit Service b38f0b
#  include <time.h>
Packit Service b38f0b
# endif
Packit Service b38f0b
#endif
Packit Service b38f0b
#if HAVE_SYS_SELECT_H
Packit Service b38f0b
#include <sys/select.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
#if HAVE_NETDB_H
Packit Service b38f0b
#include <netdb.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
#if HAVE_ARPA_INET_H
Packit Service b38f0b
#include <arpa/inet.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
Packit Service b38f0b
#include <net-snmp/net-snmp-includes.h>
Packit Service b38f0b
#include <net-snmp/types.h>
Packit Service b38f0b
#include <net-snmp/output_api.h>
Packit Service b38f0b
#include <net-snmp/config_api.h>
Packit Service b38f0b
#include <net-snmp/library/snmpv3.h>
Packit Service b38f0b
#include <net-snmp/library/snmp_parse_args.h>   /* for "internal" definitions */
Packit Service b38f0b
#include <net-snmp/utilities.h>
Packit Service b38f0b
Packit Service b38f0b
#include <net-snmp/library/snmp_api.h>
Packit Service b38f0b
#include <net-snmp/library/snmp_client.h>
Packit Service b38f0b
#include <net-snmp/library/mib.h>
Packit Service b38f0b
#include <net-snmp/library/scapi.h>
Packit Service b38f0b
#include <net-snmp/library/keytools.h>
Packit Service b38f0b
Packit Service b38f0b
#include <net-snmp/version.h>
Packit Service b38f0b
#include <net-snmp/library/parse.h>
Packit Service b38f0b
#include <net-snmp/library/snmpv3.h>
Packit Service b38f0b
#include <net-snmp/library/transform_oids.h>
Packit Service b38f0b
Packit Service b38f0b
void
Packit Service b38f0b
snmp_parse_args_usage(FILE * outf)
Packit Service b38f0b
{
Packit Service b38f0b
    fprintf(outf, "[OPTIONS] AGENT");
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
void
Packit Service b38f0b
snmp_parse_args_descriptions(FILE * outf)
Packit Service b38f0b
{
Packit Service b38f0b
    fprintf(outf, "  Version:  %s\n", netsnmp_get_version());
Packit Service b38f0b
    fprintf(outf, "  Web:      http://www.net-snmp.org/\n");
Packit Service b38f0b
    fprintf(outf,
Packit Service b38f0b
            "  Email:    net-snmp-coders@lists.sourceforge.net\n\nOPTIONS:\n");
Packit Service b38f0b
    fprintf(outf, "  -h, --help\t\tdisplay this help message\n");
Packit Service b38f0b
    fprintf(outf,
Packit Service b38f0b
            "  -H\t\t\tdisplay configuration file directives understood\n");
Packit Service b38f0b
    fprintf(outf, "  -v 1|2c|3\t\tspecifies SNMP version to use\n");
Packit Service b38f0b
    fprintf(outf, "  -V, --version\t\tdisplay package version number\n");
Packit Service b38f0b
#if !defined(NETSNMP_DISABLE_SNMPV1) || !defined(NETSNMP_DISABLE_SNMPV2C)
Packit Service b38f0b
    fprintf(outf, "SNMP Version 1 or 2c specific\n");
Packit Service b38f0b
    fprintf(outf, "  -c COMMUNITY\t\tset the community string\n");
Packit Service b38f0b
#endif /* support for community based SNMP */
Packit Service b38f0b
    fprintf(outf, "SNMP Version 3 specific\n");
Packit Service b38f0b
    fprintf(outf,
Packit Service b38f0b
            "  -a PROTOCOL\t\tset authentication protocol (MD5|SHA|SHA-224|SHA-256|SHA-384|SHA-512)\n");
Packit Service b38f0b
    fprintf(outf,
Packit Service b38f0b
            "  -A PASSPHRASE\t\tset authentication protocol pass phrase\n");
Packit Service b38f0b
    fprintf(outf,
Packit Service b38f0b
            "  -e ENGINE-ID\t\tset security engine ID (e.g. 800000020109840301)\n");
Packit Service b38f0b
    fprintf(outf,
Packit Service b38f0b
            "  -E ENGINE-ID\t\tset context engine ID (e.g. 800000020109840301)\n");
Packit Service b38f0b
    fprintf(outf,
Packit Service b38f0b
            "  -l LEVEL\t\tset security level (noAuthNoPriv|authNoPriv|authPriv)\n");
Packit Service b38f0b
    fprintf(outf, "  -n CONTEXT\t\tset context name (e.g. bridge1)\n");
Packit Service b38f0b
    fprintf(outf, "  -u USER-NAME\t\tset security name (e.g. bert)\n");
Packit Service b38f0b
#ifdef HAVE_AES
Packit Service b38f0b
    fprintf(outf, "  -x PROTOCOL\t\tset privacy protocol (DES|AES"
Packit Service b38f0b
#ifdef NETSNMP_DRAFT_BLUMENTHAL_AES_04
Packit Service b38f0b
            "|AES-192|AES-256"
Packit Service b38f0b
#endif
Packit Service b38f0b
            ")\n");
Packit Service b38f0b
#else
Packit Service b38f0b
    fprintf(outf, "  -x PROTOCOL\t\tset privacy protocol (DES)\n");
Packit Service b38f0b
#endif
Packit Service b38f0b
    fprintf(outf, "  -X PASSPHRASE\t\tset privacy protocol pass phrase\n");
Packit Service b38f0b
    fprintf(outf,
Packit Service b38f0b
            "  -Z BOOTS,TIME\t\tset destination engine boots/time\n");
Packit Service b38f0b
    fprintf(outf, "General communication options\n");
Packit Service b38f0b
    fprintf(outf, "  -r RETRIES\t\tset the number of retries\n");
Packit Service b38f0b
    fprintf(outf,
Packit Service b38f0b
            "  -t TIMEOUT\t\tset the request timeout (in seconds)\n");
Packit Service b38f0b
    fprintf(outf, "Debugging\n");
Packit Service b38f0b
    fprintf(outf, "  -d\t\t\tdump input/output packets in hexadecimal\n");
Packit Service b38f0b
#ifndef NETSNMP_DISABLE_DEBUGGING
Packit Service b38f0b
    fprintf(outf,
Packit Service b38f0b
            "  -D[TOKEN[,...]]\tturn on debugging output for the specified TOKENs\n\t\t\t   (ALL gives extremely verbose debugging output)\n");
Packit Service b38f0b
#endif
Packit Service b38f0b
    fprintf(outf, "General options\n");
Packit Service b38f0b
    fprintf(outf,
Packit Service b38f0b
            "  -m MIB[" ENV_SEPARATOR "...]\t\tload given list of MIBs (ALL loads everything)\n");
Packit Service b38f0b
    fprintf(outf,
Packit Service b38f0b
            "  -M DIR[" ENV_SEPARATOR "...]\t\tlook in given list of directories for MIBs\n");
Packit Service b38f0b
#ifndef NETSNMP_DISABLE_MIB_LOADING
Packit Service b38f0b
    fprintf(outf,
Packit Service b38f0b
            "    (default: %s)\n", netsnmp_get_mib_directory());
Packit Service b38f0b
    fprintf(outf,
Packit Service b38f0b
            "  -P MIBOPTS\t\tToggle various defaults controlling MIB parsing:\n");
Packit Service b38f0b
    snmp_mib_toggle_options_usage("\t\t\t  ", outf);
Packit Service b38f0b
#endif
Packit Service b38f0b
    fprintf(outf,
Packit Service b38f0b
            "  -O OUTOPTS\t\tToggle various defaults controlling output display:\n");
Packit Service b38f0b
    snmp_out_toggle_options_usage("\t\t\t  ", outf);
Packit Service b38f0b
    fprintf(outf,
Packit Service b38f0b
            "  -I INOPTS\t\tToggle various defaults controlling input parsing:\n");
Packit Service b38f0b
    snmp_in_toggle_options_usage("\t\t\t  ", outf);
Packit Service b38f0b
    fprintf(outf,
Packit Service b38f0b
            "  -L LOGOPTS\t\tToggle various defaults controlling logging:\n");
Packit Service b38f0b
    snmp_log_options_usage("\t\t\t  ", outf);
Packit Service b38f0b
    fflush(outf);
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
#define BUF_SIZE 512
Packit Service b38f0b
Packit Service b38f0b
void
Packit Service b38f0b
handle_long_opt(const char *myoptarg)
Packit Service b38f0b
{
Packit Service b38f0b
    char           *cp, *cp2;
Packit Service b38f0b
    /*
Packit Service b38f0b
     * else it's a long option, so process it like name=value 
Packit Service b38f0b
     */
Packit Service b38f0b
    cp = (char *)malloc(strlen(myoptarg) + 3);
Packit Service b38f0b
    if (!cp)
Packit Service b38f0b
        return;
Packit Service b38f0b
    strcpy(cp, myoptarg);
Packit Service b38f0b
    cp2 = strchr(cp, '=');
Packit Service b38f0b
    if (!cp2 && !strchr(cp, ' ')) {
Packit Service b38f0b
        /*
Packit Service b38f0b
         * well, they didn't specify an argument as far as we
Packit Service b38f0b
         * can tell.  Give them a '1' as the argument (which
Packit Service b38f0b
         * works for boolean tokens and a few others) and let
Packit Service b38f0b
         * them suffer from there if it's not what they
Packit Service b38f0b
         * wanted 
Packit Service b38f0b
         */
Packit Service b38f0b
        strcat(cp, " 1");
Packit Service b38f0b
    } else {
Packit Service b38f0b
        /*
Packit Service b38f0b
         * replace the '=' with a ' ' 
Packit Service b38f0b
         */
Packit Service b38f0b
        if (cp2)
Packit Service b38f0b
            *cp2 = ' ';
Packit Service b38f0b
    }
Packit Service b38f0b
    netsnmp_config(cp);
Packit Service b38f0b
    free(cp);
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
int
Packit Service b38f0b
netsnmp_parse_args(int argc,
Packit Service b38f0b
                   char **argv,
Packit Service b38f0b
                   netsnmp_session * session, const char *localOpts,
Packit Service b38f0b
                   void (*proc) (int, char *const *, int),
Packit Service b38f0b
                   int flags)
Packit Service b38f0b
{
Packit Service b38f0b
    int             arg, sp = 0;
Packit Service b38f0b
    char           *cp;
Packit Service b38f0b
    char           *Apsz = NULL;
Packit Service b38f0b
    char           *Xpsz = NULL;
Packit Service b38f0b
    char           *Cpsz = NULL;
Packit Service b38f0b
    char            Opts[BUF_SIZE];
Packit Service b38f0b
    int             zero_sensitive = !( flags & NETSNMP_PARSE_ARGS_NOZERO );
Packit Service b38f0b
    char           *backup_NETSNMP_DS_LIB_OUTPUT_PRECISION = NULL;
Packit Service b38f0b
Packit Service b38f0b
    /*
Packit Service b38f0b
     * initialize session to default values 
Packit Service b38f0b
     */
Packit Service b38f0b
    snmp_sess_init(session);
Packit Service b38f0b
    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:L:s:");
Packit Service b38f0b
    if (localOpts) {
Packit Service b38f0b
        if (strlen(localOpts) + strlen(Opts) >= sizeof(Opts)) {
Packit Service b38f0b
            snmp_log(LOG_ERR, "Too many localOpts in snmp_parse_args()\n");
Packit Service b38f0b
            return -1;
Packit Service b38f0b
        }
Packit Service b38f0b
        strcat(Opts, localOpts);
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    /*
Packit Service b38f0b
     * get the options 
Packit Service b38f0b
     */
Packit Service b38f0b
    DEBUGMSGTL(("snmp_parse_args", "starting: %d/%d\n", optind, argc));
Packit Service b38f0b
    for (arg = 0; arg < argc; arg++) {
Packit Service b38f0b
        DEBUGMSGTL(("snmp_parse_args", " arg %d = %s\n", arg, argv[arg]));
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    optind = 1;
Packit Service b38f0b
    while ((arg = getopt(argc, argv, Opts)) != EOF) {
Packit Service b38f0b
        DEBUGMSGTL(("snmp_parse_args", "handling (#%d): %c (optarg %s) (sp %d)\n",
Packit Service b38f0b
                    optind, arg, optarg, sp));
Packit Service b38f0b
        switch (arg) {
Packit Service b38f0b
        case '-':
Packit Service b38f0b
            if (strcasecmp(optarg, "help") == 0) {
Packit Service b38f0b
                return (NETSNMP_PARSE_ARGS_ERROR_USAGE);
Packit Service b38f0b
            }
Packit Service b38f0b
            if (strcasecmp(optarg, "version") == 0) {
Packit Service b38f0b
                fprintf(stderr,"NET-SNMP version: %s\n",netsnmp_get_version());
Packit Service b38f0b
                return (NETSNMP_PARSE_ARGS_SUCCESS_EXIT);
Packit Service b38f0b
            }
Packit Service b38f0b
Packit Service b38f0b
            handle_long_opt(optarg);
Packit Service b38f0b
            break;
Packit Service b38f0b
Packit Service b38f0b
        case 'V':
Packit Service b38f0b
            fprintf(stderr, "NET-SNMP version: %s\n", netsnmp_get_version());
Packit Service b38f0b
            return (NETSNMP_PARSE_ARGS_SUCCESS_EXIT);
Packit Service b38f0b
Packit Service b38f0b
        case 'h':
Packit Service b38f0b
            return (NETSNMP_PARSE_ARGS_ERROR_USAGE);
Packit Service b38f0b
            break;
Packit Service b38f0b
Packit Service b38f0b
        case 'H':
Packit Service b38f0b
            init_snmp(NETSNMP_APPLICATION_CONFIG_TYPE);
Packit Service b38f0b
            fprintf(stderr, "Configuration directives understood:\n");
Packit Service b38f0b
            read_config_print_usage("  ");
Packit Service b38f0b
            return (NETSNMP_PARSE_ARGS_SUCCESS_EXIT);
Packit Service b38f0b
Packit Service b38f0b
        case 'Y':
Packit Service b38f0b
            netsnmp_config_remember(optarg);
Packit Service b38f0b
            break;
Packit Service b38f0b
Packit Service b38f0b
#ifndef NETSNMP_DISABLE_MIB_LOADING
Packit Service b38f0b
        case 'm':
Packit Service b38f0b
            setenv("MIBS", optarg, 1);
Packit Service b38f0b
            break;
Packit Service b38f0b
Packit Service b38f0b
        case 'M':
Packit Service b38f0b
            netsnmp_get_mib_directory(); /* prepare the default directories */
Packit Service b38f0b
            netsnmp_set_mib_directory(optarg);
Packit Service b38f0b
            break;
Packit Service b38f0b
#endif /* NETSNMP_DISABLE_MIB_LOADING */
Packit Service b38f0b
Packit Service b38f0b
        case 'O':
Packit Service b38f0b
            cp = snmp_out_options(optarg, argc, argv);
Packit Service b38f0b
            if (cp != NULL) {
Packit Service b38f0b
                fprintf(stderr, "Unknown output option passed to -O: %c.\n", 
Packit Service b38f0b
			*cp);
Packit Service b38f0b
                return (NETSNMP_PARSE_ARGS_ERROR_USAGE);
Packit Service b38f0b
            }
Packit Service b38f0b
            break;
Packit Service b38f0b
Packit Service b38f0b
        case 'I':
Packit Service b38f0b
            cp = snmp_in_options(optarg, argc, argv);
Packit Service b38f0b
            if (cp != NULL) {
Packit Service b38f0b
                fprintf(stderr, "Unknown input option passed to -I: %c.\n",
Packit Service b38f0b
			*cp);
Packit Service b38f0b
                return (NETSNMP_PARSE_ARGS_ERROR_USAGE);
Packit Service b38f0b
            }
Packit Service b38f0b
            break;
Packit Service b38f0b
Packit Service b38f0b
#ifndef NETSNMP_DISABLE_MIB_LOADING
Packit Service b38f0b
        case 'P':
Packit Service b38f0b
            cp = snmp_mib_toggle_options(optarg);
Packit Service b38f0b
            if (cp != NULL) {
Packit Service b38f0b
                fprintf(stderr,
Packit Service b38f0b
                        "Unknown parsing option passed to -P: %c.\n", *cp);
Packit Service b38f0b
                return (NETSNMP_PARSE_ARGS_ERROR_USAGE);
Packit Service b38f0b
            }
Packit Service b38f0b
            break;
Packit Service b38f0b
#endif /* NETSNMP_DISABLE_MIB_LOADING */
Packit Service b38f0b
Packit Service b38f0b
        case 'D':
Packit Service b38f0b
#ifdef NETSNMP_NO_DEBUGGING
Packit Service b38f0b
            fprintf(stderr, "Debug not configured in\n");
Packit Service b38f0b
            return (NETSNMP_PARSE_ARGS_ERROR_USAGE);
Packit Service b38f0b
#else
Packit Service b38f0b
            debug_register_tokens(optarg);
Packit Service b38f0b
            snmp_set_do_debugging(1);
Packit Service b38f0b
#endif
Packit Service b38f0b
            break;
Packit Service b38f0b
Packit Service b38f0b
        case 'd':
Packit Service b38f0b
            netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, 
Packit Service b38f0b
				   NETSNMP_DS_LIB_DUMP_PACKET, 1);
Packit Service b38f0b
            break;
Packit Service b38f0b
Packit Service b38f0b
        case 's':
Packit Service b38f0b
            session->localname = strdup(optarg);
Packit Service b38f0b
            break;
Packit Service b38f0b
Packit Service b38f0b
        case 'v':
Packit Service b38f0b
            session->version = -1;
Packit Service b38f0b
#ifndef NETSNMP_DISABLE_SNMPV1
Packit Service b38f0b
            if (!strcmp(optarg, "1")) {
Packit Service b38f0b
                session->version = SNMP_VERSION_1;
Packit Service b38f0b
            }
Packit Service b38f0b
#endif
Packit Service b38f0b
#ifndef NETSNMP_DISABLE_SNMPV2C
Packit Service b38f0b
            if (!strcasecmp(optarg, "2c")) {
Packit Service b38f0b
                session->version = SNMP_VERSION_2c;
Packit Service b38f0b
            }
Packit Service b38f0b
#endif
Packit Service b38f0b
            if (!strcasecmp(optarg, "3")) {
Packit Service b38f0b
                session->version = SNMP_VERSION_3;
Packit Service b38f0b
            }
Packit Service b38f0b
            if (session->version == -1) {
Packit Service b38f0b
                fprintf(stderr,
Packit Service b38f0b
                        "Invalid version specified after -v flag: %s\n",
Packit Service b38f0b
                        optarg);
Packit Service b38f0b
                return (NETSNMP_PARSE_ARGS_ERROR_USAGE);
Packit Service b38f0b
            }
Packit Service b38f0b
            break;
Packit Service b38f0b
Packit Service b38f0b
        case 'p':
Packit Service b38f0b
            fprintf(stderr, "Warning: -p option is no longer used - ");
Packit Service b38f0b
            fprintf(stderr, "specify the remote host as HOST:PORT\n");
Packit Service b38f0b
            return (NETSNMP_PARSE_ARGS_ERROR_USAGE);
Packit Service b38f0b
            break;
Packit Service b38f0b
Packit Service b38f0b
        case 'T':
Packit Service b38f0b
        {
Packit Service b38f0b
            char leftside[SNMP_MAXBUF_MEDIUM], rightside[SNMP_MAXBUF_MEDIUM];
Packit Service b38f0b
            char *tmpcp, *tmpopt;
Packit Service b38f0b
            
Packit Service b38f0b
            /* ensure we have a proper argument */
Packit Service b38f0b
            tmpopt = strdup(optarg);
Packit Service b38f0b
            tmpcp = strchr(tmpopt, '=');
Packit Service b38f0b
            if (!tmpcp) {
Packit Service b38f0b
                fprintf(stderr, "-T expects a NAME=VALUE pair.\n");
Packit Service b38f0b
                free(tmpopt);
Packit Service b38f0b
                return (NETSNMP_PARSE_ARGS_ERROR_USAGE);
Packit Service b38f0b
            }
Packit Service b38f0b
            *tmpcp++ = '\0';
Packit Service b38f0b
Packit Service b38f0b
            /* create the transport config container if this is the first */
Packit Service b38f0b
            if (!session->transport_configuration) {
Packit Service b38f0b
                netsnmp_container_init_list();
Packit Service b38f0b
                session->transport_configuration =
Packit Service b38f0b
                    netsnmp_container_find("transport_configuration:fifo");
Packit Service b38f0b
                if (!session->transport_configuration) {
Packit Service b38f0b
                    fprintf(stderr, "failed to initialize the transport configuration container\n");
Packit Service b38f0b
                    free(tmpopt);
Packit Service b38f0b
                    return (NETSNMP_PARSE_ARGS_ERROR);
Packit Service b38f0b
                }
Packit Service b38f0b
Packit Service b38f0b
                session->transport_configuration->compare =
Packit Service b38f0b
                    (netsnmp_container_compare*)
Packit Service b38f0b
                    netsnmp_transport_config_compare;
Packit Service b38f0b
            }
Packit Service b38f0b
Packit Service b38f0b
            /* set the config */
Packit Service b38f0b
            strlcpy(leftside, tmpopt, sizeof(leftside));
Packit Service b38f0b
            strlcpy(rightside, tmpcp, sizeof(rightside));
Packit Service b38f0b
Packit Service b38f0b
            CONTAINER_INSERT(session->transport_configuration,
Packit Service b38f0b
                             netsnmp_transport_create_config(leftside,
Packit Service b38f0b
                                                             rightside));
Packit Service b38f0b
            free(tmpopt);
Packit Service b38f0b
        }
Packit Service b38f0b
        break;
Packit Service b38f0b
            
Packit Service b38f0b
        case 't':
Packit Service b38f0b
            session->timeout = (long)(atof(optarg) * 1000000L);
Packit Service b38f0b
            if (session->timeout <= 0) {
Packit Service b38f0b
                fprintf(stderr, "Invalid timeout in seconds after -t flag.\n");
Packit Service b38f0b
                return (NETSNMP_PARSE_ARGS_ERROR_USAGE);
Packit Service b38f0b
            }
Packit Service b38f0b
            break;
Packit Service b38f0b
Packit Service b38f0b
        case 'r':
Packit Service b38f0b
            session->retries = atoi(optarg);
Packit Service b38f0b
            if (session->retries < 0 || !isdigit((unsigned char)(optarg[0]))) {
Packit Service b38f0b
                fprintf(stderr, "Invalid number of retries after -r flag.\n");
Packit Service b38f0b
                return (NETSNMP_PARSE_ARGS_ERROR_USAGE);
Packit Service b38f0b
            }
Packit Service b38f0b
            break;
Packit Service b38f0b
Packit Service b38f0b
        case 'c':
Packit Service b38f0b
	    if (zero_sensitive) {
Packit Service b38f0b
                SNMP_FREE(Cpsz); /* free any previous value */
Packit Service b38f0b
		if ((Cpsz = strdup(optarg)) != NULL) {
Packit Service b38f0b
		    memset(optarg, '\0', strlen(optarg));
Packit Service b38f0b
		} else {
Packit Service b38f0b
		    fprintf(stderr, "malloc failure processing -c flag.\n");
Packit Service b38f0b
		    return NETSNMP_PARSE_ARGS_ERROR;
Packit Service b38f0b
		}
Packit Service b38f0b
	    } else {
Packit Service b38f0b
		Cpsz = optarg;
Packit Service b38f0b
	    }
Packit Service b38f0b
            break;
Packit Service b38f0b
Packit Service b38f0b
        case '3':
Packit Service b38f0b
            if (snmpv3_parse_args(optarg, session, &Apsz, &Xpsz, argc, argv,
Packit Service b38f0b
                                  flags) < 0){
Packit Service b38f0b
                return (NETSNMP_PARSE_ARGS_ERROR_USAGE);
Packit Service b38f0b
            }
Packit Service b38f0b
            break;
Packit Service b38f0b
Packit Service b38f0b
        case 'L':
Packit Service b38f0b
            if (snmp_log_options(optarg, argc, argv) < 0) {
Packit Service b38f0b
                return (NETSNMP_PARSE_ARGS_ERROR_USAGE);
Packit Service b38f0b
            }
Packit Service b38f0b
            break;
Packit Service b38f0b
Packit Service b38f0b
#define SNMPV3_CMD_OPTIONS
Packit Service b38f0b
#ifdef  SNMPV3_CMD_OPTIONS
Packit Service b38f0b
        case 'Z':
Packit Service b38f0b
        case 'e':
Packit Service b38f0b
        case 'E':
Packit Service b38f0b
        case 'n':
Packit Service b38f0b
        case 'l':
Packit Service b38f0b
        case 'u':
Packit Service b38f0b
#ifdef NETSNMP_SECMOD_USM
Packit Service b38f0b
        case 'a':
Packit Service b38f0b
        case 'x':
Packit Service b38f0b
        case 'A':
Packit Service b38f0b
        case 'X':
Packit Service b38f0b
#endif             /* NETSNMP_SECMOD_USM */
Packit Service b38f0b
            if (snmpv3_parse_arg(arg, optarg, session, &Apsz, &Xpsz, argc,
Packit Service b38f0b
                                 argv, flags) < 0){
Packit Service b38f0b
                return (NETSNMP_PARSE_ARGS_ERROR_USAGE);
Packit Service b38f0b
            }
Packit Service b38f0b
            break;
Packit Service b38f0b
#endif                          /* SNMPV3_CMD_OPTIONS */
Packit Service b38f0b
Packit Service b38f0b
        case '?':
Packit Service b38f0b
            return (NETSNMP_PARSE_ARGS_ERROR_USAGE);
Packit Service b38f0b
            break;
Packit Service b38f0b
Packit Service b38f0b
        default:
Packit Service b38f0b
            proc(argc, argv, arg);
Packit Service b38f0b
            break;
Packit Service b38f0b
        }
Packit Service b38f0b
    }
Packit Service b38f0b
    DEBUGMSGTL(("snmp_parse_args", "finished: %d/%d\n", optind, argc));
Packit Service b38f0b
    
Packit Service b38f0b
    /*
Packit Service b38f0b
     * save command line parameters which should have precedence above config file settings
Packit Service b38f0b
     *    (There ought to be a more scalable approach than this....)
Packit Service b38f0b
     */
Packit Service b38f0b
    if (netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OUTPUT_PRECISION)) {
Packit Service b38f0b
        backup_NETSNMP_DS_LIB_OUTPUT_PRECISION = 
Packit Service b38f0b
            strdup(netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OUTPUT_PRECISION));
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    /*
Packit Service b38f0b
     * read in MIB database and initialize the snmp library, read the config file
Packit Service b38f0b
     */
Packit Service b38f0b
    init_snmp(NETSNMP_APPLICATION_CONFIG_TYPE);
Packit Service b38f0b
Packit Service b38f0b
    /*
Packit Service b38f0b
     * restore command line parameters which should have precedence above config file settings
Packit Service b38f0b
     */
Packit Service b38f0b
    if(backup_NETSNMP_DS_LIB_OUTPUT_PRECISION) {
Packit Service b38f0b
        netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OUTPUT_PRECISION, backup_NETSNMP_DS_LIB_OUTPUT_PRECISION);
Packit Service b38f0b
        free(backup_NETSNMP_DS_LIB_OUTPUT_PRECISION);
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
    /*
Packit Service b38f0b
     * session default version 
Packit Service b38f0b
     */
Packit Service b38f0b
    if (session->version == SNMP_DEFAULT_VERSION) {
Packit Service b38f0b
        /*
Packit Service b38f0b
         * run time default version 
Packit Service b38f0b
         */
Packit Service b38f0b
        session->version = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, 
Packit Service b38f0b
					      NETSNMP_DS_LIB_SNMPVERSION);
Packit Service b38f0b
Packit Service b38f0b
        /*
Packit Service b38f0b
         * compile time default version 
Packit Service b38f0b
         */
Packit Service b38f0b
        if (!session->version) {
Packit Service b38f0b
            switch (NETSNMP_DEFAULT_SNMP_VERSION) {
Packit Service b38f0b
#ifndef NETSNMP_DISABLE_SNMPV1
Packit Service b38f0b
            case 1:
Packit Service b38f0b
                session->version = SNMP_VERSION_1;
Packit Service b38f0b
                break;
Packit Service b38f0b
#endif
Packit Service b38f0b
Packit Service b38f0b
#ifndef NETSNMP_DISABLE_SNMPV2C
Packit Service b38f0b
            case 2:
Packit Service b38f0b
                session->version = SNMP_VERSION_2c;
Packit Service b38f0b
                break;
Packit Service b38f0b
#endif
Packit Service b38f0b
Packit Service b38f0b
            case 3:
Packit Service b38f0b
                session->version = SNMP_VERSION_3;
Packit Service b38f0b
                break;
Packit Service b38f0b
Packit Service b38f0b
            default:
Packit Service b38f0b
                snmp_log(LOG_ERR, "Can't determine a valid SNMP version for the session\n");
Packit Service b38f0b
                return(NETSNMP_PARSE_ARGS_ERROR);
Packit Service b38f0b
            }
Packit Service b38f0b
        } else {
Packit Service b38f0b
#ifndef NETSNMP_DISABLE_SNMPV1
Packit Service b38f0b
            if (session->version == NETSNMP_DS_SNMP_VERSION_1)  /* bogus value.  version 1 actually = 0 */
Packit Service b38f0b
                session->version = SNMP_VERSION_1;
Packit Service b38f0b
#endif
Packit Service b38f0b
        }
Packit Service b38f0b
    }
Packit Service b38f0b
Packit Service b38f0b
#ifdef NETSNMP_SECMOD_USM
Packit Service b38f0b
    /* XXX: this should ideally be moved to snmpusm.c somehow */
Packit Service b38f0b
Packit Service b38f0b
    /*
Packit Service b38f0b
     * make master key from pass phrases 
Packit Service b38f0b
     */
Packit Service b38f0b
    if (Apsz) {
Packit Service b38f0b
        session->securityAuthKeyLen = USM_AUTH_KU_LEN;
Packit Service b38f0b
        if (session->securityAuthProto == NULL) {
Packit Service b38f0b
            /*
Packit Service b38f0b
             * get .conf set default 
Packit Service b38f0b
             */
Packit Service b38f0b
            const oid      *def =
Packit Service b38f0b
                get_default_authtype(&session->securityAuthProtoLen);
Packit Service b38f0b
            session->securityAuthProto =
Packit Service b38f0b
                snmp_duplicate_objid(def, session->securityAuthProtoLen);
Packit Service b38f0b
        }
Packit Service b38f0b
        if (session->securityAuthProto == NULL) {
Packit Service b38f0b
            session->securityAuthProto =
Packit Service b38f0b
                snmp_duplicate_objid(SNMP_DEFAULT_AUTH_PROTO,
Packit Service b38f0b
                                     SNMP_DEFAULT_AUTH_PROTOLEN);
Packit Service b38f0b
            session->securityAuthProtoLen = SNMP_DEFAULT_AUTH_PROTOLEN;
Packit Service b38f0b
        }
Packit Service b38f0b
        if (generate_Ku(session->securityAuthProto,
Packit Service b38f0b
                        session->securityAuthProtoLen,
Packit Service b38f0b
                        (u_char *) Apsz, strlen(Apsz),
Packit Service b38f0b
                        session->securityAuthKey,
Packit Service b38f0b
                        &session->securityAuthKeyLen) != SNMPERR_SUCCESS) {
Packit Service b38f0b
            snmp_perror(argv[0]);
Packit Service b38f0b
            fprintf(stderr,
Packit Service b38f0b
                    "Error generating a key (Ku) from the supplied authentication pass phrase. \n");
Packit Service b38f0b
            return (NETSNMP_PARSE_ARGS_ERROR);
Packit Service b38f0b
        }
Packit Service b38f0b
        free(Apsz);
Packit Service b38f0b
    }
Packit Service b38f0b
    if (Xpsz) {
Packit Service b38f0b
        session->securityPrivKeyLen = USM_PRIV_KU_LEN;
Packit Service b38f0b
        if (session->securityPrivProto == NULL) {
Packit Service b38f0b
            /*
Packit Service b38f0b
             * get .conf set default 
Packit Service b38f0b
             */
Packit Service b38f0b
            const oid      *def =
Packit Service b38f0b
                get_default_privtype(&session->securityPrivProtoLen);
Packit Service b38f0b
            session->securityPrivProto =
Packit Service b38f0b
                snmp_duplicate_objid(def, session->securityPrivProtoLen);
Packit Service b38f0b
        }
Packit Service b38f0b
        if (session->securityPrivProto == NULL) {
Packit Service b38f0b
            session->securityPrivProto =
Packit Service b38f0b
                snmp_duplicate_objid(SNMP_DEFAULT_PRIV_PROTO,
Packit Service b38f0b
                                     SNMP_DEFAULT_PRIV_PROTOLEN);
Packit Service b38f0b
            session->securityPrivProtoLen = SNMP_DEFAULT_PRIV_PROTOLEN;
Packit Service b38f0b
        }
Packit Service b38f0b
        if (generate_Ku(session->securityAuthProto,
Packit Service b38f0b
                        session->securityAuthProtoLen,
Packit Service b38f0b
                        (u_char *) Xpsz, strlen(Xpsz),
Packit Service b38f0b
                        session->securityPrivKey,
Packit Service b38f0b
                        &session->securityPrivKeyLen) != SNMPERR_SUCCESS) {
Packit Service b38f0b
            snmp_perror(argv[0]);
Packit Service b38f0b
            fprintf(stderr,
Packit Service b38f0b
                    "Error generating a key (Ku) from the supplied privacy pass phrase. \n");
Packit Service b38f0b
            return (NETSNMP_PARSE_ARGS_ERROR);
Packit Service b38f0b
        }
Packit Service b38f0b
        free(Xpsz);
Packit Service b38f0b
    }
Packit Service b38f0b
#endif /* NETSNMP_SECMOD_USM */
Packit Service b38f0b
Packit Service b38f0b
    /*
Packit Service b38f0b
     * get the hostname 
Packit Service b38f0b
     */
Packit Service b38f0b
    if (optind == argc) {
Packit Service b38f0b
        fprintf(stderr, "No hostname specified.\n");
Packit Service b38f0b
        return (NETSNMP_PARSE_ARGS_ERROR_USAGE);
Packit Service b38f0b
    }
Packit Service b38f0b
    session->peername = argv[optind++]; /* hostname */
Packit Service b38f0b
Packit Service b38f0b
#if !defined(NETSNMP_DISABLE_SNMPV1) || !defined(NETSNMP_DISABLE_SNMPV2C)
Packit Service b38f0b
    /*
Packit Service b38f0b
     * If v1 or v2c, check community has been set, either by a -c option above,
Packit Service b38f0b
     * or via a default token somewhere.  
Packit Service b38f0b
     * If neither, it will be taken from the incoming request PDU.
Packit Service b38f0b
     */
Packit Service b38f0b
Packit Service b38f0b
#if defined(NETSNMP_DISABLE_SNMPV1)
Packit Service b38f0b
    if (session->version == SNMP_VERSION_2c)
Packit Service b38f0b
#else 
Packit Service b38f0b
#if defined(NETSNMP_DISABLE_SNMPV2C)
Packit Service b38f0b
    if (session->version == SNMP_VERSION_1)
Packit Service b38f0b
#else
Packit Service b38f0b
    if (session->version == SNMP_VERSION_1 ||
Packit Service b38f0b
	session->version == SNMP_VERSION_2c)
Packit Service b38f0b
#endif
Packit Service b38f0b
#endif
Packit Service b38f0b
    {
Packit Service b38f0b
        if (Cpsz == NULL) {
Packit Service b38f0b
            Cpsz = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 
Packit Service b38f0b
					 NETSNMP_DS_LIB_COMMUNITY);
Packit Service b38f0b
	    if (Cpsz == NULL) {
Packit Service b38f0b
                if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
Packit Service b38f0b
                                           NETSNMP_DS_LIB_IGNORE_NO_COMMUNITY)){
Packit Service b38f0b
                    DEBUGMSGTL(("snmp_parse_args",
Packit Service b38f0b
                                "ignoring that the community string is not present\n"));
Packit Service b38f0b
                    session->community = NULL;
Packit Service b38f0b
                    session->community_len = 0;
Packit Service b38f0b
                } else {
Packit Service b38f0b
                    fprintf(stderr, "No community name specified.\n");
Packit Service b38f0b
                    return (NETSNMP_PARSE_ARGS_ERROR_USAGE);
Packit Service b38f0b
                }
Packit Service b38f0b
	    }
Packit Service b38f0b
	} else {
Packit Service b38f0b
            session->community = (unsigned char *)Cpsz;
Packit Service b38f0b
            session->community_len = strlen(Cpsz);
Packit Service b38f0b
        }
Packit Service b38f0b
    }
Packit Service b38f0b
#endif /* support for community based SNMP */
Packit Service b38f0b
Packit Service b38f0b
    return optind;
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
int
Packit Service b38f0b
snmp_parse_args(int argc,
Packit Service b38f0b
                char **argv,
Packit Service b38f0b
                netsnmp_session * session, const char *localOpts,
Packit Service b38f0b
                void (*proc) (int, char *const *, int))
Packit Service b38f0b
{
Packit Service b38f0b
    return netsnmp_parse_args(argc, argv, session, localOpts, proc, 0);
Packit Service b38f0b
}