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

📄 snmpv3.c

📁 snmp up 2
💻 C
📖 第 1 页 / 共 3 页
字号:
    /*     * Determine length of the engineID string.      */    len = 5;                    /* always have 5 leading bytes */    switch (localEngineIDType) {    case ENGINEID_TYPE_TEXT:        len += strlen(text);    /* 5 leading bytes+text. No NULL char */        break;#if defined(IFHWADDRLEN) && defined(SIOCGIFHWADDR)    case ENGINEID_TYPE_MACADDR:        /* MAC address */        len += 6;               /* + 6 bytes for MAC address */        break;#endif    case ENGINEID_TYPE_IPV4:   /* IPv4 */        len += 4;               /* + 4 byte IPV4 address */        break;    case ENGINEID_TYPE_IPV6:   /* IPv6 */        len += 16;              /* + 16 byte IPV6 address */        break;    case ENGINEID_TYPE_UCD_RND:        /* UCD specific encoding */        if (engineID)           /* already setup, keep current value */            return engineIDLength;        if (oldEngineID) {            len = oldEngineIDLength;        } else {            len += sizeof(int) + sizeof(time_t);        }        break;    default:        snmp_log(LOG_ERR,                 "Unknown EngineID type requested for setup (%d).  Using IPv4.\n",                 localEngineIDType);        localEngineIDType = ENGINEID_TYPE_IPV4; /* make into IPV4 */        len += 4;               /* + 4 byte IPv4 address */        break;    }                           /* switch */    /*     * Allocate memory and store enterprise ID.     */    if ((bufp = (u_char *) malloc(len)) == NULL) {        snmp_log_perror("setup_engineID malloc");        return -1;    }    if (localEngineIDType == ENGINEID_TYPE_UCD_RND)        /*         * we must use the net-snmp enterprise id here, regardless          */        memcpy(bufp, &ucdavisid, sizeof(ucdavisid));    /* XXX Must be 4 bytes! */    else        memcpy(bufp, &enterpriseid, sizeof(enterpriseid));      /* XXX Must be 4 bytes! */    bufp[0] |= 0x80;    /*     * Store the given text  -OR-   the first found IP address.     */    switch (localEngineIDType) {    case ENGINEID_TYPE_UCD_RND:        if (oldEngineID) {            /*             * keep our previous notion of the engineID              */            memcpy(bufp, oldEngineID, oldEngineIDLength);        } else {            /*             * Here we've desigend our own ENGINEID that is not based on             * an address which may change and may even become conflicting             * in the future like most of the default v3 engineID types             * suffer from.             *              * Ours is built from 2 fairly random elements: a random number and             * the current time in seconds.  This method suffers from boxes             * that may not have a correct clock setting and random number             * seed at startup, but few OSes should have that problem.             */            bufp[4] = ENGINEID_TYPE_UCD_RND;            tmpint = random();            memcpy(bufp + 5, &tmpint, sizeof(tmpint));            tmptime = time(NULL);            memcpy(bufp + 5 + sizeof(tmpint), &tmptime, sizeof(tmptime));        }        break;    case ENGINEID_TYPE_TEXT:        bufp[4] = ENGINEID_TYPE_TEXT;        memcpy((char *) bufp + 5, text, strlen(text));        break;#ifdef HAVE_GETHOSTNAME#ifdef AF_INET6    case ENGINEID_TYPE_IPV6:        bufp[4] = ENGINEID_TYPE_IPV6;        memcpy(bufp + 5, hent->h_addr_list[0], hent->h_length);        break;#endif#endif#if defined(IFHWADDRLEN) && defined(SIOCGIFHWADDR)    case ENGINEID_TYPE_MACADDR:        {            int             x;            bufp[4] = ENGINEID_TYPE_MACADDR;            /*             * use default NIC if none provided              */            if (NULL == engineIDNic) {                x = getHwAddress(DEFAULT_NIC, &bufp[5]);            } else {                x = getHwAddress(engineIDNic, &bufp[5]);            }            if (0 != x)                /*                 * function failed fill MAC address with zeros                  */            {                memset(&bufp[5], 0, 6);            }        }        break;#endif    case ENGINEID_TYPE_IPV4:    default:        bufp[4] = ENGINEID_TYPE_IPV4;#ifdef HAVE_GETHOSTNAME        if (hent && hent->h_addrtype == AF_INET) {            memcpy(bufp + 5, hent->h_addr_list[0], hent->h_length);        } else {                /* Unknown address type.  Default to 127.0.0.1. */            bufp[5] = 127;            bufp[6] = 0;            bufp[7] = 0;            bufp[8] = 1;        }#else                           /* HAVE_GETHOSTNAME */        /*         * Unknown address type.  Default to 127.0.0.1.          */        bufp[5] = 127;        bufp[6] = 0;        bufp[7] = 0;        bufp[8] = 1;#endif                          /* HAVE_GETHOSTNAME */        break;    }    /*     * Pass the string back to the calling environment, or use it for     * our local engineID.     */    if (localsetup) {        SNMP_FREE(engineID);        engineID = bufp;        engineIDLength = len;    } else {        *eidp = bufp;    }    return len;}                               /* end setup_engineID() */voidusm_parse_create_usmUser(const char *token, char *line){    char           *cp;    char            buf[SNMP_MAXBUF_MEDIUM];    struct usmUser *newuser;    u_char          userKey[SNMP_MAXBUF_SMALL];    size_t          userKeyLen = SNMP_MAXBUF_SMALL;    size_t          ret;    newuser = usm_create_user();    /*     * READ: Security Name      */    cp = copy_nword(line, buf, sizeof(buf));    /*     * might be a -e ENGINEID argument      */    if (strcmp(buf, "-e") == 0) {        size_t          ebuf_len = 32, eout_len = 0;        u_char         *ebuf = (u_char *) malloc(ebuf_len);        if (ebuf == NULL) {            config_perror("malloc failure processing -e flag");            usm_free_user(newuser);            return;        }        /*         * Get the specified engineid from the line.           */        cp = copy_nword(cp, buf, sizeof(buf));        if (!snmp_hex_to_binary(&ebuf, &ebuf_len, &eout_len, 1, buf)) {            config_perror("invalid EngineID argument to -e");            usm_free_user(newuser);            free(ebuf);            return;        }        newuser->engineID = ebuf;        newuser->engineIDLen = eout_len;        cp = copy_nword(cp, buf, sizeof(buf));    } else {        newuser->engineID = snmpv3_generate_engineID(&ret);        if (ret == 0) {            usm_free_user(newuser);            return;        }        newuser->engineIDLen = ret;    }    newuser->secName = strdup(buf);    newuser->name = strdup(buf);    if (!cp)        goto add;               /* no authentication or privacy type */    /*     * READ: Authentication Type      */    if (strncmp(cp, "MD5", 3) == 0) {        memcpy(newuser->authProtocol, usmHMACMD5AuthProtocol,               sizeof(usmHMACMD5AuthProtocol));    } else if (strncmp(cp, "SHA", 3) == 0) {        memcpy(newuser->authProtocol, usmHMACSHA1AuthProtocol,               sizeof(usmHMACSHA1AuthProtocol));    } else {        config_perror("Unknown authentication protocol");        usm_free_user(newuser);        return;    }    cp = skip_token(cp);    /*     * READ: Authentication Pass Phrase      */    if (!cp) {        config_perror("no authentication pass phrase");        usm_free_user(newuser);        return;    }    cp = copy_nword(cp, buf, sizeof(buf));    /*     * And turn it into a localized key      */    ret = generate_Ku(newuser->authProtocol, newuser->authProtocolLen,                      (u_char *) buf, strlen(buf), userKey, &userKeyLen);    if (ret != SNMPERR_SUCCESS) {        config_perror("could not generate the authentication key from the "                      "suppiled pass phrase.");        usm_free_user(newuser);        return;    }    newuser->authKeyLen =        sc_get_properlength(newuser->authProtocol,                            newuser->authProtocolLen);    newuser->authKey = (u_char *) malloc(newuser->authKeyLen);    ret = generate_kul(newuser->authProtocol, newuser->authProtocolLen,                       newuser->engineID, newuser->engineIDLen,                       userKey, userKeyLen,                       newuser->authKey, &newuser->authKeyLen);    if (ret != SNMPERR_SUCCESS) {        config_perror("could not generate localized authentication key (Kul) "                      "from the master key (Ku).");        usm_free_user(newuser);        return;    }    if (!cp)        goto add;               /* no privacy type (which is legal) */    /*     * READ: Privacy Type      */    if (strncmp(cp, "DES", 3) == 0) {        memcpy(newuser->privProtocol, usmDESPrivProtocol,               sizeof(usmDESPrivProtocol));#ifdef HAVE_AES    } else if (strncmp(cp, "AES128", 3) == 0) {        memcpy(newuser->privProtocol, usmAES128PrivProtocol,               sizeof(usmAES128PrivProtocol));    } else if (strncmp(cp, "AES192", 3) == 0) {        memcpy(newuser->privProtocol, usmAES192PrivProtocol,               sizeof(usmAES192PrivProtocol));    } else if (strncmp(cp, "AES256", 3) == 0) {        memcpy(newuser->privProtocol, usmAES256PrivProtocol,               sizeof(usmAES256PrivProtocol));#endif    } else {        config_perror("Unknown privacy protocol");        usm_free_user(newuser);        return;    }    cp = skip_token(cp);    /*     * READ: Authentication Pass Phrase      */    if (!cp) {        /*         * assume the same as the authentication key          */        memdup(&newuser->privKey, newuser->authKey, newuser->authKeyLen);        newuser->privKeyLen = newuser->authKeyLen;    } else {        cp = copy_nword(cp, buf, sizeof(buf));        /*         * And turn it into a localized key          */        ret = generate_Ku(newuser->authProtocol, newuser->authProtocolLen,                          (u_char *) buf, strlen(buf),                          userKey, &userKeyLen);        if (ret != SNMPERR_SUCCESS) {            config_perror("could not generate privacy key from the supplied "                          "pass phrase.");            usm_free_user(newuser);            return;        }        ret =            sc_get_properlength(newuser->authProtocol,                                newuser->authProtocolLen);        if (ret < 0) {            config_perror("could not get proper key length to use for the "                          "privacy algorithm.");            usm_free_user(newuser);            return;        }        newuser->privKeyLen = ret;        newuser->privKey = (u_char *) malloc(newuser->privKeyLen);        ret = generate_kul(newuser->authProtocol, newuser->authProtocolLen,                           newuser->engineID, newuser->engineIDLen,                           userKey, userKeyLen,                           newuser->privKey, &newuser->privKeyLen);        if (ret != SNMPERR_SUCCESS) {            config_perror("could not generate the localized privacy key (Kul) "                          "from the master key (Ku).");            usm_free_user(newuser);            return;        }    }  add:    usm_add_user(newuser);    DEBUGMSGTL(("usmUser", "created a new user %s at ", newuser->secName));    DEBUGMSGHEX(("usmUser", newuser->engineID, newuser->engineIDLen));    DEBUGMSG(("usmUser", "\n"));}/*******************************************************************-o-****** * engineBoots_conf * * Parameters: *	*word *	*cptr * * Line syntax: *	engineBoots <num_boots> */voidengineBoots_conf(const char *word, char *cptr){    engineBoots = atoi(cptr) + 1;    DEBUGMSGTL(("snmpv3", "engineBoots: %d\n", engineBoots));}/*******************************************************************-o-****** * engineIDType_conf * * Parameters: *	*word *	*cptr * * Line syntax: *	engineIDType <1 or 3> *		1 is default for IPv4 engine ID type.  Will automatically *		    chose between IPv4 & IPv6 if either 1 or 2 is specified. *		2 is for IPv6. *		3 is hardware (MAC) address, currently supported under Linux */voidengineIDType_conf(const char *word, char *cptr){    engineIDType = atoi(cptr);    /*     * verify valid type selected      */    switch (engineIDType) {    case ENGINEID_TYPE_IPV4:   /* IPv4 */    case ENGINEID_TYPE_IPV6:   /* IPv6 */        /*         * IPV? is always good          */        break;#if defined(IFHWADDRLEN) && defined(SIOCGIFHWADDR)    case ENGINEID_TYPE_MACADDR:        /* MAC address */        break;#endif    default:        /*         * unsupported one chosen          */        config_perror("Unsupported enginedIDType, forcing IPv4");        engineIDType = ENGINEID_TYPE_IPV4;    }    DEBUGMSGTL(("snmpv3", "engineIDType: %d\n", engineIDType));}/*******************************************************************-o-****** * engineIDNic_conf * * Parameters: *	*word *	*cptr * * Line syntax: *	engineIDNic <string> *		eth0 is default */voidengineIDNic_conf(const char *word, char *cptr){    /*     * Make sure they haven't already specified the engineID via the     * * configuration file      */    if (0 == engineIDIsSet)        /*         * engineID has NOT been set via configuration file          */    {        /*         * See if already set if so erase & release it          */        if (NULL != engineIDNic) {            free(engineIDNic);        }        engineIDNic = (u_char *) malloc(strlen(cptr) + 1);        if (NULL != engineIDNic) {            strcpy((char *) engineIDNic, cptr);            DEBUGMSGTL(("snmpv3", "Initializing engineIDNic: %s\n",                        engineIDNic));        } else {            DEBUGMSGTL(("snmpv3",                        "Error allocating memory for engineIDNic!\n"));        }    } else {        DEBUGMSGTL(("snmpv3",

⌨️ 快捷键说明

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