net-snmp没有正确更改auth和priv协议

问题描述 投票:3回答:2

我在Linux下的c ++程序中使用net-snmp库(版本5.7.1)。我有一个Web-Frontend,用户可以选择SNMP版本并进行配置。 SNMPv1和SNMPv2工作得很好,但是我遇到了SNMPv3的一些问题。

这是前端的图片:Screenshot of Webinterface(很抱歉不在这里直接上传,但我需要至少10个声望才能做到这一点)

当我启动c ++后端并正确输入所有需要的SNMPv3凭据时,一切正常,设备可以访问。如果我将Auth协议从MD5更改为SHA,但保留其余凭据相同,我预计设备将无法再访问。实际上它仍然可以访问。重新启动后端后,设备(正如预期)不再可以使用相同的设置访问。

在发现这个问题后,我进行了一些测试。对于测试,我使用了不同的用户和不同的设置。他们使用不同供应商的三种不同设备运行,每次都得到相同的结果。所以它不能是设备相关的问题。结果可以在这里看到:Test results

我在测试后的结论是,net-snmp似乎为一个用户名缓存了所选的auth和priv协议。这在测试2中可以看得很好。我第一次使用具有特定协议的用户名时,我得到了预期的结果。更改协议后,预计会有不同的结果,但我仍然得到与以前相同的结果。

最后,一些信息是如何进行SNMP调用的:

  • 有一个名为SNMPWrapper的类,它涉及整个SNMP通信
  • 在构造函数内部,我将init_snmp()称为init net-snmp
  • 从外面我只能打电话给get()set()walk()。每次调用其中一个方法时,都会创建一个新的SNMP-Session(首先我用snmp_sess_init()创建一个新的Session,而不是设置所需的东西,最后我用snmp_sess_open()打开会话
  • 在我提出请求并收到我的回答后,我与snmp_sess_close()结束会议

问题:在更改协议之前是否必须进行任何其他清理以使其正常工作?

编辑:我添加了一些代码,显示了所描述的行为

int main(int argc, char** argv) {
struct snmp_session session, session1, *ss, *ss1;
struct snmp_pdu *pdu, *pdu1;
struct snmp_pdu *response, *response1;

oid anOID[MAX_OID_LEN];
size_t anOID_len = MAX_OID_LEN;

struct variable_list *vars;
int status, status1;

init_snmp("snmpapp");

const char* user = "md5";
string authpw = "123123123";
string privpw = "";
string ipString = "192.168.15.32";

char ip[16];
memset(&ip, 0, sizeof (ip));
ipString.copy(ip, sizeof (ip) - 1, 0);

/*
 * First request: AuthProto is MD5, no PrivProto is used. The snmp-get
 * request is successful
 */
snmp_sess_init(&session); /* set up defaults */
session.peername = ip;
session.version = SNMP_VERSION_3;

/* set the SNMPv3 user name */
session.securityName = strdup(user);
session.securityNameLen = strlen(session.securityName);

// set the authentication method to MD5     
session.securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;

session.securityAuthProto = usmHMACMD5AuthProtocol;
session.securityAuthProtoLen = USM_AUTH_PROTO_MD5_LEN;
session.securityAuthKeyLen = USM_AUTH_KU_LEN;;

if (generate_Ku(session.securityAuthProto,
        session.securityAuthProtoLen,
        (u_char *) authpw.c_str(), strlen(authpw.c_str()),
        session.securityAuthKey,
        &session.securityAuthKeyLen) != SNMPERR_SUCCESS) {
    //if code reaches here, the creation of the security key was not successful

}

cout << "SecurityAuthProto - session: " << session.securityAuthProto[9] << " / SecurityAuthKey - session: " << session.securityAuthKey << endl;

ss = snmp_open(&session); /* establish the session */

if (!ss) {
    cout << "Couldn't open session1 correctly";
    exit(2);
}

cout << "SecurityAuthProto - ss: " << ss->securityAuthProto[9] << " / SecurityAuthKey - ss: " << ss->securityAuthKey << endl;

//send message
pdu = snmp_pdu_create(SNMP_MSG_GET);
read_objid(".1.3.6.1.2.1.1.1.0", anOID, &anOID_len);
snmp_add_null_var(pdu, anOID, anOID_len);
status = snmp_synch_response(ss, pdu, &response);

/*
 * Process the response.
 */
if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) {
    cout << "SNMP-read success" << endl;
} else {
    cout << "SNMP-read fail" << endl;
}

if (response)
    snmp_free_pdu(response);
if (!snmp_close(ss))
    cout << "Snmp closing failed" << endl;

/*
 * Second request: Only the authProto is changed from MD5 to SHA1. I expect,
 * that the snmp-get fails, but it still succeeds.
 */

snmp_sess_init(&session1);
session1.peername = ip;
session1.version = SNMP_VERSION_3;

/* set the SNMPv3 user name */
session1.securityName = strdup(user);
session1.securityNameLen = strlen(session1.securityName);

// set the authentication method to SHA1 
session1.securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;

session1.securityAuthProto = usmHMACSHA1AuthProtocol;
session1.securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN;
session1.securityAuthKeyLen = USM_AUTH_KU_LEN;

if (generate_Ku(session1.securityAuthProto,
        session1.securityAuthProtoLen,
        (u_char *) authpw.c_str(), strlen(authpw.c_str()),
        session1.securityAuthKey,
        &session1.securityAuthKeyLen) != SNMPERR_SUCCESS) {
    //if code reaches here, the creation of the security key was not successful
}

cout << "SecurityAuthProto - session1: " << session1.securityAuthProto[9] << " / SecurityAuthKey - session1: " << session1.securityAuthKey << endl;

ss1 = snmp_open(&session1); /* establish the session */

if (!ss1) {
    cout << "Couldn't open session1 correctly";
    exit(2);
}

cout << "SecurityAuthProto - ss1: " << ss1->securityAuthProto[9] << " / SecurityAuthKey - ss1: " << ss1->securityAuthKey << endl;

//send message
pdu1 = snmp_pdu_create(SNMP_MSG_GET);
read_objid(".1.3.6.1.2.1.1.1.0", anOID, &anOID_len);
snmp_add_null_var(pdu1, anOID, anOID_len);
status1 = snmp_synch_response(ss1, pdu1, &response1);

/*
 * Process the response.
 */
if (status1 == STAT_SUCCESS && response1->errstat == SNMP_ERR_NOERROR) {
    cout << "SNMP-read success" << endl;
} else {
    cout << "SNMP-read fail" << endl;
}

if (response1)
    snmp_free_pdu(response1);
snmp_close(ss1);

return 0;

}

c++ c net-snmp
2个回答
5
投票

我自己找到了解决方案:

net-snmp为每个EngineId(设备)缓存用户。如果有一个engineID的现有用户,并且您尝试使用此用户打开一个新会话,net-snmp将使用缓存的用户。因此,解决方案是使用缓存用户清除列表。

使用此代码段,我可以解决我的问题:

usmUser* actUser = usm_get_userList();
while (actUser != NULL) {
    usmUser* dummy = actUser;
    usm_remove_user(actUser);
    actUser = dummy->next;
}

我希望我可以帮助其他人。


0
投票

您还可以更新现有用户的密码:

    for (usmUser* actUser = usm_get_userList(); actUser != NULL; actUser = actUser->next) {
        if (strcmp(actUser->secName, user) == 0) {
            //this method calls generate_Ku with previous security data but with specified password
            usm_set_user_password(actUser, "userSetAuthPass", authpw.c_str());
            break;
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.