我有一个向其他机器发出SNMP调用的程序。但是,我当前用于建立会话的实现具有硬编码的凭据,例如:
const char *our_v3_passphrase = "The Net-SNMP Demo Password";
/*
* Initialize the SNMP library
*/
init_snmp("snmpdemoapp");
/*
* Initialize a "session" that defines who we're going to talk to
*/
snmp_sess_init( &session ); /* set up defaults */
session.peername = strdup("test.net-snmp.org");
/* set the SNMPv3 user name */
session.securityName = strdup("SHAUser");
session.securityNameLen = strlen(session.securityName);
/* set the security level to authenticated, but not encrypted */
session.securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;
/* set the authentication method to SHA */
session.securityAuthProto = usmHMACSHA1AuthProtocol;
session.securityAuthProtoLen = sizeof(usmHMACSHA1AuthProtocol)/sizeof(oid);
session.securityAuthKeyLen = USM_AUTH_KU_LEN;
/* set the authentication key to a SHA hashed version of our
passphrase "The Net-SNMP Demo Password" (which must be at least 8
characters long) */
if (generate_Ku(session.securityAuthProto,
session.securityAuthProtoLen,
(u_char *) our_v3_passphrase, strlen(our_v3_passphrase),
session.securityAuthKey,
&session.securityAuthKeyLen) != SNMPERR_SUCCESS) {
snmp_perror(argv[0]);
snmp_log(LOG_ERR,
"Error generating Ku from authentication pass phrase. \n");
exit(1);
}
/*
* Open the session
*/
ss = snmp_open(&session);
if (!ss) {
snmp_sess_perror("ack", &session);
SOCK_CLEANUP;
exit(1);
}我想要从机器派生SNMP凭据,而不是使用net-snmp的路径来解析配置文件的存储位置,而不是对它们进行硬编码。
我在~/.snmp/snmp.conf中有一个配置文件,其中包含以下条目:
defVersion 3
defSecurityName SHAUser
defAuthPassphrase "The Net-SNMP Demo Password"
defAuthType SHA
defSecurityLevel authNoPriv当我自己运行程序时,我可以让它工作(删除硬编码的凭据,只需不设置securityName或生成KU):
/*
* Initialize the SNMP library
*/
init_snmp("snmpdemoapp");
/*
* Initialize a "session" that defines who we're going to talk to
*/
snmp_sess_init( &session ); /* set up defaults */
session.peername = strdup("test.net-snmp.org");
/*
* Open the session
*/
ss = snmp_open(&session);
if (!ss) {
snmp_sess_perror("ack", &session);
SOCK_CLEANUP;
exit(1);
}但是,如果我将其用作守护进程的一部分,则找不到凭据。我通过侦听tcp端口161并观察在未设置securityName (仅为空白)的情况下发送的传出SNMP通信,验证了凭据不是派生出来的。
我使用的服务管理器是systemd。
我的问题是:当程序作为守护进程运行时,我如何配置net-snmp API,使其接受运行该程序的用户的凭据?
发布于 2018-07-26 01:59:34
该问题是由于守护程序的服务文件未导出该守护程序解析配置文件所需的主目录造成的。
因此,在服务文件中,我添加了:
[Service]
...
Environment=HOME=/root
...停止守护进程,重新加载守护进程,并重新启动我的守护进程(以及相关进程)。行为符合预期。
https://stackoverflow.com/questions/51524220
复制相似问题