首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >net-snmp api使用用户凭据

net-snmp api使用用户凭据
EN

Stack Overflow用户
提问于 2018-07-26 01:00:25
回答 1查看 270关注 0票数 1

我有一个向其他机器发出SNMP调用的程序。但是,我当前用于建立会话的实现具有硬编码的凭据,例如:

代码语言:javascript
复制
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中有一个配置文件,其中包含以下条目:

代码语言:javascript
复制
defVersion 3
defSecurityName SHAUser
defAuthPassphrase "The Net-SNMP Demo Password"
defAuthType SHA
defSecurityLevel authNoPriv

当我自己运行程序时,我可以让它工作(删除硬编码的凭据,只需不设置securityName或生成KU):

代码语言:javascript
复制
 /*
 * 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,使其接受运行该程序的用户的凭据?

EN

回答 1

Stack Overflow用户

发布于 2018-07-26 01:59:34

该问题是由于守护程序的服务文件未导出该守护程序解析配置文件所需的主目录造成的。

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

代码语言:javascript
复制
[Service]
...
Environment=HOME=/root
...

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51524220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档