📄 v3_user.c
字号:
* SEE ALSO: SNMP_User_Deinstall(), SNMP_User_Destroy(), SNMP_User_Install(), * SNMP_User_Lookup(), SNMP_User_Name(), SNMP_User_Next_User(), SNMPv3 User * Table Field Routines, SNMPv3 User Table Key Routines*/SNMP_USER_T * SNMP_User_Create(SNMP_AUTH_T *auth, SNMP_PRIV_T *priv){SNMP_USER_T *user;bits8_t *buffp;ALENGTH_T need;if (priv && (auth == 0)) return(0);need = sizeof(SNMP_USER_T);if (auth) need += SNMP_Auth_Get_KeySize(auth);if (priv) need += SNMP_Priv_Get_KeySize(priv);buffp = (bits8_t *)SNMP_memory_alloc_lt(need);if (buffp == 0) return(0);MEMSET(buffp, 0, need);user = (SNMP_USER_T *)buffp;SNMP_User_Set_Defaults(user);if (auth) { user->auth = auth; user->authkey = buffp + sizeof(SNMP_USER_T); user->maxsec |= ETC_V3_AUTH; }if (priv) { user->priv = priv; user->privkey = user->authkey + SNMP_Auth_Get_KeySize(auth); user->maxsec |= ETC_V3_PRIV; }#if INSTALL_SNMP_V3_DIFFIE_HELLMAN/* Initialize the Diffie-Hellman structures and variables */if ((user -> DH_keys.DHauthValues = DH_new()) == NULL) return(0);if ((user -> DH_keys.DHprivValues = DH_new()) == NULL) return(0);if (((user -> DH_keys.DHauthValues -> p = BN_new()) == 0) || ((user -> DH_keys.DHauthValues -> g = BN_new()) == 0) || ((user -> DH_keys.DHauthValues -> pub_key = BN_new()) == 0) || ((user -> DH_keys.DHauthValues -> priv_key = BN_new()) == 0) || ((user -> DH_keys.DHprivValues -> p = BN_new()) == 0) || ((user -> DH_keys.DHprivValues -> g = BN_new()) == 0) || ((user -> DH_keys.DHprivValues -> pub_key = BN_new()) == 0) || ((user -> DH_keys.DHprivValues -> priv_key = BN_new()) == 0)) { return (0);}#endif /* INSTALL_SNMP_V3_DIFFIE_HELLMAN */return(user);}/********************************************************************************* SNMP_User_Install - install the specified user entry in the user table* SYNOPSIS** \cs* int SNMP_User_Install * (* SNMP_USER_T * user, * bits8_t * id, * ALENGTH_T id_length, * bits8_t * name, * ALENGTH_T name_length * )* \ce** DESCRIPTION** This routine installs the specified user entry in the user table using <id> * and <name> as indices.** \&NOTE: Once a user entry has been installed, you must call * SNMP_User_Deinstall() before calling SNMP_User_Destroy() to remove it.** Parameters:* \is* \i <*user>* Specify the user entry to install in the user table.* \i <*id>* Point to the user <id>.* \i <id_length>* Specify the length in bytes of the <id>.* \i <*name>* Specify the user <name>.* \i <name_length>* Specify the length in bytes of the user <name>.* \ie** RETURNS: If successful, this routine returns a value of 0. If an entry * already exists with the same indices or another error occurs, it returns -1.** ERRNO: N/A** SEE ALSO: SNMP_User_Create(), SNMP_User_Deinstall(), SNMP_User_Destroy(), * SNMP_User_Install(), SNMP_User_Name(), SNMP_User_Next_User(), SNMPv3 User * Table Field Routines, SNMPv3 User Table Key Routines*/int SNMP_User_Install(SNMP_USER_T *in_user, bits8_t *id, ALENGTH_T id_len, bits8_t *uname, ALENGTH_T uname_len){SNMP_USER_T **user;SNMP_USER_ENG_T **engine, *temp_eng;bits8_t *buffp;/* sanity check the name information */if ((id_len == 0) || (id_len > ETC_USER_ENGINE_MAX) || (uname_len == 0) || (uname_len > ETC_USER_USER_MAX)) return(-1);/* allocate space for the user name and copy it over */buffp = (bits8_t *)SNMP_memory_alloc_lt(uname_len);if (buffp == 0) return(-1);/* walk through the engine list trying to find our engine id */for(engine = &root_user; *engine; engine = &(*engine)->next) { if ((*engine)->id_len >= id_len) break; }for(; *engine; engine = &(*engine)->next) { if ((*engine)->id_len != id_len) break; if (MEMCMP((*engine)->id, id, id_len) >= 0) break; }if ((*engine == 0) || ((*engine)->id_len != id_len) || MEMCMP((*engine)->id, id, id_len)) { /* if we didn't find our engine name create it and attache the user struct to it. */ temp_eng = (SNMP_USER_ENG_T *)SNMP_memory_alloc_lt(sizeof(SNMP_USER_ENG_T) + id_len); if (temp_eng == 0) { SNMP_memory_free_lt(buffp); return(1); } temp_eng->id = ((bits8_t *)temp_eng) + sizeof(SNMP_USER_ENG_T); MEMCPY(temp_eng->id, id, id_len); temp_eng->id_len = id_len; temp_eng->user = in_user; temp_eng->next = *engine; *engine = temp_eng; }else { /* search through the list of users for this engine and attach us properly */ for(user = &(*engine)->user; *user; user = &(*user)->next) { if ((*user)->uname_len >= uname_len) break; } for(; *user; user = &(*user)->next) { if (((*user)->uname_len != uname_len) || (MEMCMP((*user)->uname, uname, uname_len) >= 0)) break; } if ((*user) && ((*user)->uname_len == uname_len) && (MEMCMP((*user)->uname, uname, uname_len) == 0)) { /* a user is already installed in the requested slot cleanup and error out */ SNMP_memory_free_lt(buffp); return(1); } /* link the user with its peers */ in_user->next = *user; *user = in_user; }/* and connect the user to its parent and its name*/in_user->parent = *engine;in_user->uname = buffp;in_user->uname_len = uname_len;MEMCPY(in_user->uname, uname, uname_len);return(0);}/********************************************************************************* SNMP_User_Deinstall - remove the specified user entry from the user table* SYNOPSIS** \cs* void SNMP_User_Deinstall* (* SNMP_USER_T * user * )* \ce** DESCRIPTION** This routine removes the specified user entry from the user table and cleans * up any resources that might have been used for indexing purposes.** Parameters:* \is* \i <*user>* Specify the user entry to remove from the user table.* \ie** RETURNS: None.** ERRNO: N/A** SEE ALSO: SNMP_User_Create(), SNMP_User_Destroy(), SNMP_User_Install(), * SNMP_User_Lookup(), SNMP_User_Name(), SNMP_User_Next_User(), SNMPv3 User * Table Field Routines, SNMPv3 User Table Key Routines*/void SNMP_User_Deinstall(SNMP_USER_T *in_user){SNMP_USER_ENG_T **engine, *our_engine;SNMP_USER_T **user;our_engine = in_user->parent;if (our_engine == 0) return;/* if our structure is in the list clean it up */for(user = &(our_engine->user); *user; user = &(*user)->next) { if (*user == in_user) { *user = in_user->next; if (in_user->uname) SNMP_memory_free_lt(in_user->uname); in_user->parent = 0; in_user->next = 0; in_user->uname = 0; in_user->uname_len = 0; break; } }/* see if we need to remove the engine as well */if (our_engine->user == 0) { for(engine = &root_user; *engine; engine = &(*engine)->next) { if (*engine == our_engine) { *engine = our_engine->next; SNMP_memory_free_lt(our_engine); break; } } }return;}/****************************************************************************** SNMP_User_Name - extract the indexing information for the specified user entry* SYNOPSIS** \cs* void SNMP_User_Name * ( * SNMP_USER_T * user, * bits8_t * id, * ALENGTH_T * id_length, * bits8_t * name, * ALENGTH_T * name_length * )* \ce** DESCRIPTION** This routine extracts the indexing information for the specified user entry. * After this routine completes, check the values in the <name>, * <name_length,id,> and< id_length> fields.** Parameters:* \is* \i <*user>* Specify the user entry to install in the user table.* \i <*id>* Point to the user <id>.* \i <*id_length>* Specify the length in bytes of the <id>. On input, specifies the amount of * space available in the <id_length>. On output, it is the amount of space * required to hold the name. If the name argument is long enough to hold the * name, the name is copied.* \i <*name>* Specify the user <name>.* \i <*name_length>* Specify the length in bytes of the user name. On input, specifies the amount * of space available in the <name_length>. On output, it is the amount of space * required to hold the name. If the name argument is long enough to hold the * name, the name is copied.* \ie** RETURNS: None.** ERRNO: N/A** SEE ALSO: SNMP_User_Create(), SNMP_User_Deinstall(), SNMP_User_Destroy(), * SNMP_User_Install(), SNMP_User_Lookup(), SNMP_User_Next_User(), SNMPv3 User * Table Field Routines, SNMPv3 User Table Key Routines****************************************************************************/void SNMP_User_Name(SNMP_USER_T *user, bits8_t *id, ALENGTH_T *id_len, bits8_t *uname, ALENGTH_T *uname_len){if (user->parent) { if (*id_len >= user->parent->id_len) { MEMCPY(id, user->parent->id, user->parent->id_len); } *id_len = user->parent->id_len; }else *id_len = 0;if (*uname_len >= user->uname_len) { MEMCPY(uname, user->uname, user->uname_len); }*uname_len = user->uname_len;return;}/****************************************************************************\NOMANUALNAME: SNMP_User_Get_Sec_NamePURPOSE: Get the security name for this entryPARAMETERS: SNMP_USER_T * entry to get the security name for bits8_t * space for the security name ALENGTH_T * input: length of space for the security name output: length of space needed for the security nameRETURNS: nothing****************************************************************************/void SNMP_User_Get_Sec_Name(SNMP_USER_T *user, bits8_t *uname, ALENGTH_T *uname_len){if (*uname_len >= user->uname_len) { MEMCPY(uname, user->uname, user->uname_len); }*uname_len = user->uname_len;return;}/****************************************************************************\NOMANUALNAME: SNMP_User_Set_AuthKeyPURPOSE: copy the new key into the key field for the entry Note: as part of building the user entry we allocate space for the key so we don't have to deal with buffer issues.PARAMETERS: SNMP_USER_T * entry to insert the key into bits8_t * key to insert ALENGTH_T length of keyRETURNS: int 0 on success****************************************************************************/int SNMP_User_Set_AuthKey(SNMP_USER_T *user, bits8_t *key, ALENGTH_T keylen){if ((user->auth == 0) || (SNMP_Auth_Get_KeySize(user->auth) != keylen)) return(1);MEMCPY(user->authkey, key, keylen);return(0);}/****************************************************************************\NOMANUALNAME: SNMP_User_Set_PrivKeyPURPOSE: copy the new key into the key field for the entry Note: as part of building the user entry we allocate space for the key so we don't have to deal with buffer issues.PARAMETERS: SNMP_USER_T * entry to insert the key into bits8_t * key to insert ALENGTH_T length of keyRETURNS: int 0 on success****************************************************************************/int SNMP_User_Set_PrivKey(SNMP_USER_T *user, bits8_t *key, ALENGTH_T keylen){if ((user->priv == 0) || (SNMP_Priv_Get_KeySize(user->priv) != keylen)) return(1);MEMCPY(user->privkey, key, keylen);return(0);}/****************************************************************************\NOMANUALNAME: SNMP_User_Swap_UsersPURPOSE: Puts a new user into the user table in place of an old one. Should be equivalent to deinstalling the old user and installing the new one. This is an internal function and should not be called by customers.PARAMETERS: SNMP_USER_T * new (not installed) entry SNMP_USER_T * old (installed) entryRETURNS: int 0 on success****************************************************************************/int SNMP_User_Swap_Users(SNMP_USER_T *new_user, SNMP_USER_T *old_user){SNMP_USER_ENG_T *temp_engine;SNMP_USER_T *temp_user;bits8_t *temp_uname;ALENGTH_T temp_uname_len;if (old_user == SNMP_User_Next_User(0)) root_user->user = new_user;else for (temp_user = SNMP_User_Next_User(0); temp_user; temp_user = SNMP_User_Next_User(temp_user)) { if (temp_user->next) { if (temp_user->next == old_user) { temp_user->next = new_user; break; } } else if (temp_user->parent->next) { if (temp_user->parent->next->user == old_user) { temp_user->parent->next->user = new_user; break; } } else return 1; }temp_engine = old_user->parent;old_user->parent = new_user->parent;new_user->parent = temp_engine;new_user->next = old_user->next;old_user->next = new_user;temp_uname = SNMP_User_Get_SName(old_user);SNMP_User_Get_SName(old_user) = SNMP_User_Get_SName(new_user);SNMP_User_Get_SName(new_user) = temp_uname;temp_uname_len = old_user->uname_len;old_user->uname_len = new_user->uname_len;new_user->uname_len = temp_uname_len;return 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -