c - net-snmp api 使用用户的凭据

标签 c snmp net-snmp

我有一个程序可以向其他机器发出 SNMP 调用。但是,我当前用于建立 session 的实现对凭据进行了硬编码,例如:

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,以便它获取运行该程序的用户的凭据?

最佳答案

该问题可追溯到守护程序的服务文件未导出守护程序解析配置文件所需的主目录。

因此,在服务文件中,我添加了:

[Service]
...
Environment=HOME=/root
...

停止守护进程,执行守护进程重新加载,并重新启动我的守护进程(和相关进程)。行为符合预期。

关于c - net-snmp api 使用用户的凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51524220/

相关文章:

c - 使用网络 snmp :can not regularly read and update data from a file

perl - 我的 Perl 脚本如何从托管系统接收 SNMP 陷阱?

无法在我自己的代码中使用 CHOLMOD 和 CUDA 加速

udp - 为什么 SNMP 通常在 UDP 上运行而不是 TCP/IP 上?

python - 打印 snmpwalk 结果的数量

c - 像 snmpget 这样的 c 应用程序采用什么参数?

snmpget : No such object available on this agent at this OID

C 释放指向指针的指针

c - 如何在打开/写入功能中实现超时

c - 函数未检测到 EOF