⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snmpv3.c

📁 snmp up 2
💻 C
📖 第 1 页 / 共 3 页
字号:
                    "NOT setting engineIDNic, engineID already set\n"));    }}/*******************************************************************-o-****** * engineID_conf * * Parameters: *	*word *	*cptr * * This function reads a string from the configuration file and uses that * string to initialize the engineID.  It's assumed to be human readable. */voidengineID_conf(const char *word, char *cptr){    setup_engineID(NULL, cptr);    DEBUGMSGTL(("snmpv3", "initialized engineID with: %s\n", cptr));}voidversion_conf(const char *word, char *cptr){    if (strcmp(cptr, "1") == 0) {        netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_SNMPVERSION, 			   NETSNMP_DS_SNMP_VERSION_1);       /* bogus value */    } else if (strcasecmp(cptr, "2c") == 0) {        netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_SNMPVERSION, 			   NETSNMP_DS_SNMP_VERSION_2c);    } else if (strcmp(cptr, "3") == 0) {        netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_SNMPVERSION, 			   NETSNMP_DS_SNMP_VERSION_3);    } else {        config_perror("Unknown version specification");        return;    }    DEBUGMSGTL(("snmpv3", "set default version to %d\n",                netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, 				   NETSNMP_DS_LIB_SNMPVERSION)));}/* * engineID_old_conf(const char *, char *): *  * Reads a octet string encoded engineID into the oldEngineID and * oldEngineIDLen pointers. */voidoldengineID_conf(const char *word, char *cptr){    read_config_read_octet_string(cptr, &oldEngineID, &oldEngineIDLength);}/*******************************************************************-o-****** * init_snmpv3 * * Parameters: *	*type	Label for the config file "type" used by calling entity. *       * Set time and engineID. * Set parsing functions for config file tokens. * Initialize SNMP Crypto API (SCAPI). */voidinit_snmpv3(const char *type){    gettimeofday(&snmpv3starttime, NULL);    if (!type)        type = "__snmpapp__";    /*     * we need to be called back later      */    snmp_register_callback(SNMP_CALLBACK_LIBRARY,                           SNMP_CALLBACK_POST_READ_CONFIG,                           init_snmpv3_post_config, NULL);    snmp_register_callback(SNMP_CALLBACK_LIBRARY,                           SNMP_CALLBACK_POST_PREMIB_READ_CONFIG,                           init_snmpv3_post_premib_config, NULL);    /*     * we need to be called back later      */    snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA,                           snmpv3_store, (void *) strdup(type));    /*     * initialize submodules      */    /*     * NOTE: this must be after the callbacks are registered above,     * since they need to be called before the USM callbacks.      */    init_secmod();    /*     * register all our configuration handlers (ack, there's a lot)      */    /*     * handle engineID setup before everything else which may depend on it      */    register_prenetsnmp_mib_handler(type, "engineID", engineID_conf, NULL,                                    "string");    register_prenetsnmp_mib_handler(type, "oldEngineID", oldengineID_conf,                                    NULL, NULL);    register_prenetsnmp_mib_handler(type, "engineIDType",                                    engineIDType_conf, NULL, "num");    register_prenetsnmp_mib_handler(type, "engineIDNic", engineIDNic_conf,                                    NULL, "string");    register_config_handler(type, "engineBoots", engineBoots_conf, NULL,                            NULL);    /*     * default store config entries      */    netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "defSecurityName",			       NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_SECNAME);    netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "defContext", 			       NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_CONTEXT);    netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "defPassphrase",			     NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PASSPHRASE);    netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "defAuthPassphrase",			 NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_AUTHPASSPHRASE);    netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "defPrivPassphrase",			 NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRIVPASSPHRASE);    register_config_handler("snmp", "defVersion", version_conf, NULL,                            "1|2c|3");    register_config_handler("snmp", "defAuthType", snmpv3_authtype_conf,                            NULL, "MD5|SHA");    register_config_handler("snmp", "defPrivType", snmpv3_privtype_conf,                            NULL,#ifdef HAVE_AES                            "DES (AES support not available)");#else                            "DES|AES128|AES192|AES256");#endif    register_config_handler("snmp", "defSecurityLevel",                            snmpv3_secLevel_conf, NULL,                            "noAuthNoPriv|authNoPriv|authPriv");    register_config_handler(type, "userSetAuthPass", usm_set_password,                            NULL, NULL);    register_config_handler(type, "userSetPrivPass", usm_set_password,                            NULL, NULL);    register_config_handler(type, "userSetAuthKey", usm_set_password, NULL,                            NULL);    register_config_handler(type, "userSetPrivKey", usm_set_password, NULL,                            NULL);    register_config_handler(type, "userSetAuthLocalKey", usm_set_password,                            NULL, NULL);    register_config_handler(type, "userSetPrivLocalKey", usm_set_password,                            NULL, NULL);}/* * initializations for SNMPv3 to be called after the configuration files * have been read. */intinit_snmpv3_post_config(int majorid, int minorid, void *serverarg,                        void *clientarg){    size_t          engineIDLen;    u_char         *c_engineID;    c_engineID = snmpv3_generate_engineID(&engineIDLen);    if (engineIDLen == 0) {        /*         * Somethine went wrong - help!          */        return SNMPERR_GENERR;    }    /*     * if our engineID has changed at all, the boots record must be set to 1      */    if (engineIDLen != (int) oldEngineIDLength ||        oldEngineID == NULL || c_engineID == NULL ||        memcmp(oldEngineID, c_engineID, engineIDLen) != 0) {        engineBoots = 1;    }    /*     * set our local engineTime in the LCD timing cache      */    set_enginetime(c_engineID, engineIDLen,                   snmpv3_local_snmpEngineBoots(),                   snmpv3_local_snmpEngineTime(), TRUE);    free(c_engineID);    return SNMPERR_SUCCESS;}intinit_snmpv3_post_premib_config(int majorid, int minorid, void *serverarg,                               void *clientarg){    if (!engineIDIsSet)        setup_engineID(NULL, NULL);    return SNMPERR_SUCCESS;}/*******************************************************************-o-****** * store_snmpv3 * * Parameters: *	*type */intsnmpv3_store(int majorID, int minorID, void *serverarg, void *clientarg){    char            line[SNMP_MAXBUF_SMALL];    u_char          c_engineID[SNMP_MAXBUF_SMALL];    int             engineIDLen;    const char     *type = (const char *) clientarg;    if (type == NULL)           /* should never happen, since the arg is ours */        type = "unknown";    sprintf(line, "engineBoots %ld", engineBoots);    read_config_store(type, line);    engineIDLen = snmpv3_get_engineID(c_engineID, SNMP_MAXBUF_SMALL);    if (engineIDLen) {        /*         * store the engineID used for this run          */        sprintf(line, "oldEngineID ");        read_config_save_octet_string(line + strlen(line), c_engineID,                                      engineIDLen);        read_config_store(type, line);    }    return SNMPERR_SUCCESS;}                               /* snmpv3_store() */u_longsnmpv3_local_snmpEngineBoots(void){    return engineBoots;}/*******************************************************************-o-****** * snmpv3_get_engineID * * Parameters: *	*buf *	 buflen *       * Returns: *	Length of engineID	On Success *	SNMPERR_GENERR		Otherwise. * * * Store engineID in buf; return the length. * */size_tsnmpv3_get_engineID(u_char * buf, size_t buflen){    /*     * Sanity check.     */    if (!buf || (buflen < engineIDLength)) {        return 0;    }    memcpy(buf, engineID, engineIDLength);    return engineIDLength;}                               /* end snmpv3_get_engineID() *//*******************************************************************-o-****** * snmpv3_clone_engineID * * Parameters: *	**dest *       *dest_len *       src *	 srclen *       * Returns: *	Length of engineID	On Success *	0		        Otherwise. * * * Clones engineID, creates memory * */intsnmpv3_clone_engineID(u_char ** dest, size_t * destlen, u_char * src,                      size_t srclen){    if (!dest || !destlen)        return 0;    if (*dest) {        SNMP_FREE(*dest);        *dest = NULL;    }    *destlen = 0;    if (srclen && src) {        *dest = (u_char *) malloc(srclen);        if (*dest == NULL)            return 0;        memmove(*dest, src, srclen);        *destlen = srclen;    }    return *destlen;}                               /* end snmpv3_clone_engineID() *//*******************************************************************-o-****** * snmpv3_generate_engineID * * Parameters: *	*length *       * Returns: *	Pointer to copy of engineID	On Success. *	NULL				If malloc() or snmpv3_get_engineID() *						fail. * * Generates a malloced copy of our engineID. * * 'length' is set to the length of engineID  -OR-  < 0 on failure. */u_char         *snmpv3_generate_engineID(size_t * length){    u_char         *newID;    newID = (u_char *) malloc(engineIDLength);    if (newID) {        *length = snmpv3_get_engineID(newID, engineIDLength);    }    if (*length == 0) {        SNMP_FREE(newID);        newID = NULL;    }    return newID;}                               /* end snmpv3_generate_engineID() *//* * snmpv3_local_snmpEngineTime(): return the number of seconds since the * snmpv3 engine last incremented engine_boots  */u_longsnmpv3_local_snmpEngineTime(void){    struct timeval  now;    gettimeofday(&now, NULL);    return calculate_time_diff(&now, &snmpv3starttime) / 100;}/* * Code only for Linux systems  */#if defined(IFHWADDRLEN) && defined(SIOCGIFHWADDR)static intgetHwAddress(const char *networkDevice, /* e.g. "eth0", "eth1" */             char *addressOut){                               /* return address. Len=IFHWADDRLEN */    /*     * getHwAddress(...)     * *     * *  This function will return a Network Interfaces Card's Hardware     * *  address (aka MAC address).     * *     * *  Input Parameter(s):     * *      networkDevice - a null terminated string with the name of a network     * *                      device.  Examples: eth0, eth1, etc...     * *     * *  Output Parameter(s):     * *      addressOut -    This is the binary value of the hardware address.     * *                      This value is NOT converted into a hexadecimal string.     * *                      The caller must pre-allocate for a return value of     * *                      length IFHWADDRLEN     * *     * *  Return value:   This function will return zero (0) for success.  If     * *                  an error occurred the function will return -1.     * *     * *  Caveats:    This has only been tested on Ethernet networking cards.     */    int             sock;       /* our socket */    struct ifreq    request;    /* struct which will have HW address */    if ((NULL == networkDevice) || (NULL == addressOut)) {        return -1;    }    /*     * In order to find out the hardware (MAC) address of our system under     * * Linux we must do the following:     * * 1.  Create a socket     * * 2.  Do an ioctl(...) call with the SIOCGIFHWADDRLEN operation.     */    sock = socket(AF_INET, SOCK_DGRAM, 0);    if (sock < 0) {        return -1;    }    /*     * erase the request block      */    memset(&request, 0, sizeof(request));    /*     * copy the name of the net device we want to find the HW address for      */    strncpy(request.ifr_name, networkDevice, IFNAMSIZ - 1);    /*     * Get the HW address      */    if (ioctl(sock, SIOCGIFHWADDR, &request)) {        close(sock);        return -1;    }    close(sock);    memcpy(addressOut, request.ifr_hwaddr.sa_data, IFHWADDRLEN);    return 0;}#endif#ifdef SNMP_TESTING_CODE/* * snmpv3_set_engineBootsAndTime(): this function does not exist.  Go away.  *//* * It certainly should never be used, unless in a testing scenero, * which is why it was created  */voidsnmpv3_set_engineBootsAndTime(int boots, int ttime){    engineBoots = boots;    gettimeofday(&snmpv3starttime, NULL);    snmpv3starttime.tv_sec -= ttime;}#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -