【问题标题】:net-snmp api use user's credentialsnet-snmp api 使用用户的凭据
【发布时间】:2018-07-25 17:00:24
【问题描述】:

我有一个程序可以对其他机器进行 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 以获取运行程序的用户的凭据?

【问题讨论】:

    标签: c snmp net-snmp


    【解决方案1】:

    问题被追溯到守护进程的服务文件没有导出守护进程解析配置文件所需的主目录。

    所以,在服务文件中,我添加了:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-30
      • 2015-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多