| 1 |
/* |
| 2 |
* AgentX Configuration |
| 3 |
*/ |
| 4 |
#include <net-snmp/net-snmp-config.h> |
| 5 |
#include <net-snmp/net-snmp-features.h> |
| 6 |
|
| 7 |
#include <stdio.h> |
| 8 |
#include <sys/types.h> |
| 9 |
#ifdef HAVE_STDLIB_H |
| 10 |
#include <stdlib.h> |
| 11 |
#endif |
| 12 |
#ifdef HAVE_STRING_H |
| 13 |
#include <string.h> |
| 14 |
#else |
| 15 |
#include <strings.h> |
| 16 |
#endif |
| 17 |
|
| 18 |
#include <net-snmp/net-snmp-includes.h> |
| 19 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 20 |
#include "snmpd.h" |
| 21 |
#include "agentx/agentx_config.h" |
| 22 |
#include "agentx/protocol.h" |
| 23 |
|
| 24 |
netsnmp_feature_require(user_information); |
| 25 |
netsnmp_feature_require(string_time_to_secs); |
| 26 |
|
| 27 |
/* --------------------------------------------------------------------- |
| 28 |
* |
| 29 |
* Common master and sub-agent |
| 30 |
*/ |
| 31 |
void |
| 32 |
agentx_parse_agentx_socket(const char *token, char *cptr) |
| 33 |
{ |
| 34 |
DEBUGMSGTL(("agentx/config", "port spec: %s\n", cptr)); |
| 35 |
netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_X_SOCKET, cptr); |
| 36 |
} |
| 37 |
|
| 38 |
/* --------------------------------------------------------------------- |
| 39 |
* |
| 40 |
* Master agent |
| 41 |
*/ |
| 42 |
#ifdef USING_AGENTX_MASTER_MODULE |
| 43 |
void |
| 44 |
agentx_parse_master(const char *token, char *cptr) |
| 45 |
{ |
| 46 |
int i = -1; |
| 47 |
|
| 48 |
if (!strcmp(cptr, "agentx") || |
| 49 |
!strcmp(cptr, "all") || |
| 50 |
!strcmp(cptr, "yes") || !strcmp(cptr, "on")) { |
| 51 |
i = 1; |
| 52 |
snmp_log(LOG_INFO, "Turning on AgentX master support.\n"); |
| 53 |
} else if (!strcmp(cptr, "no") || !strcmp(cptr, "off")) |
| 54 |
i = 0; |
| 55 |
else |
| 56 |
i = atoi(cptr); |
| 57 |
|
| 58 |
if (i < 0 || i > 1) { |
| 59 |
netsnmp_config_error("master '%s' unrecognised", cptr); |
| 60 |
} else |
| 61 |
netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_AGENTX_MASTER, i); |
| 62 |
} |
| 63 |
|
| 64 |
void |
| 65 |
agentx_parse_agentx_perms(const char *token, char *cptr) |
| 66 |
{ |
| 67 |
char *socket_perm, *dir_perm, *socket_user, *socket_group; |
| 68 |
int uid = -1; |
| 69 |
int gid = -1; |
| 70 |
int s_perm = -1; |
| 71 |
int d_perm = -1; |
| 72 |
char *st; |
| 73 |
|
| 74 |
DEBUGMSGTL(("agentx/config", "port permissions: %s\n", cptr)); |
| 75 |
socket_perm = strtok_r(cptr, " \t", &st); |
| 76 |
dir_perm = strtok_r(NULL, " \t", &st); |
| 77 |
socket_user = strtok_r(NULL, " \t", &st); |
| 78 |
socket_group = strtok_r(NULL, " \t", &st); |
| 79 |
|
| 80 |
if (socket_perm) { |
| 81 |
s_perm = strtol(socket_perm, NULL, 8); |
| 82 |
netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, |
| 83 |
NETSNMP_DS_AGENT_X_SOCK_PERM, s_perm); |
| 84 |
DEBUGMSGTL(("agentx/config", "socket permissions: %o (%d)\n", |
| 85 |
s_perm, s_perm)); |
| 86 |
} |
| 87 |
if (dir_perm) { |
| 88 |
d_perm = strtol(dir_perm, NULL, 8); |
| 89 |
netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, |
| 90 |
NETSNMP_DS_AGENT_X_DIR_PERM, d_perm); |
| 91 |
DEBUGMSGTL(("agentx/config", "directory permissions: %o (%d)\n", |
| 92 |
d_perm, d_perm)); |
| 93 |
} |
| 94 |
|
| 95 |
/* |
| 96 |
* Try to handle numeric UIDs or user names for the socket owner |
| 97 |
*/ |
| 98 |
if (socket_user) { |
| 99 |
uid = netsnmp_str_to_uid(socket_user); |
| 100 |
if ( uid != 0 ) |
| 101 |
netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, |
| 102 |
NETSNMP_DS_AGENT_X_SOCK_USER, uid); |
| 103 |
DEBUGMSGTL(("agentx/config", "socket owner: %s (%d)\n", |
| 104 |
socket_user, uid)); |
| 105 |
} |
| 106 |
|
| 107 |
/* |
| 108 |
* and similarly for the socket group ownership |
| 109 |
*/ |
| 110 |
if (socket_group) { |
| 111 |
gid = netsnmp_str_to_gid(socket_group); |
| 112 |
if ( gid != 0 ) |
| 113 |
netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, |
| 114 |
NETSNMP_DS_AGENT_X_SOCK_GROUP, gid); |
| 115 |
DEBUGMSGTL(("agentx/config", "socket group: %s (%d)\n", |
| 116 |
socket_group, gid)); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
void |
| 121 |
agentx_parse_agentx_timeout(const char *token, char *cptr) |
| 122 |
{ |
| 123 |
int x = netsnmp_string_time_to_secs(cptr); |
| 124 |
DEBUGMSGTL(("agentx/config/timeout", "%s\n", cptr)); |
| 125 |
if (x == -1) { |
| 126 |
config_perror("Invalid timeout value"); |
| 127 |
return; |
| 128 |
} |
| 129 |
netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, |
| 130 |
NETSNMP_DS_AGENT_AGENTX_TIMEOUT, x * ONE_SEC); |
| 131 |
} |
| 132 |
|
| 133 |
void |
| 134 |
agentx_parse_agentx_retries(const char *token, char *cptr) |
| 135 |
{ |
| 136 |
int x = atoi(cptr); |
| 137 |
DEBUGMSGTL(("agentx/config/retries", "%s\n", cptr)); |
| 138 |
netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, |
| 139 |
NETSNMP_DS_AGENT_AGENTX_RETRIES, x); |
| 140 |
} |
| 141 |
#endif /* USING_AGENTX_MASTER_MODULE */ |
| 142 |
|
| 143 |
#ifdef USING_AGENTX_SUBAGENT_MODULE |
| 144 |
void |
| 145 |
agentx_parse_agentx_ping_interval(const char *token, char *cptr) |
| 146 |
{ |
| 147 |
int x = atoi(cptr); |
| 148 |
|
| 149 |
DEBUGMSGTL(("agentx/config/ping", "%s\n", cptr)); |
| 150 |
if (x < 1) { |
| 151 |
config_perror("Invalid ping interval value"); |
| 152 |
return; |
| 153 |
} |
| 154 |
netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, |
| 155 |
NETSNMP_DS_AGENT_AGENTX_PING_INTERVAL, x); |
| 156 |
} |
| 157 |
#endif /* USING_AGENTX_SUBAGENT_MODULE */ |
| 158 |
|
| 159 |
/* --------------------------------------------------------------------- |
| 160 |
* |
| 161 |
* Sub-agent |
| 162 |
*/ |
| 163 |
|
| 164 |
|
| 165 |
/* --------------------------------------------------------------------- |
| 166 |
* |
| 167 |
* Utility support routines |
| 168 |
*/ |
| 169 |
void |
| 170 |
agentx_register_config_handler(const char *token, |
| 171 |
void (*parser) (const char *, char *), |
| 172 |
void (*releaser) (void), const char *help) |
| 173 |
{ |
| 174 |
DEBUGMSGTL(("agentx_register_app_config_handler", |
| 175 |
"registering .conf token for \"%s\"\n", token)); |
| 176 |
register_config_handler(":agentx", token, parser, releaser, help); |
| 177 |
} |
| 178 |
|
| 179 |
netsnmp_feature_child_of(agentx_unregister_config_handler, netsnmp_unused); |
| 180 |
#ifndef NETSNMP_FEATURE_REMOVE_AGENTX_UNREGISTER_CONFIG_HANDLER |
| 181 |
void |
| 182 |
agentx_unregister_config_handler(const char *token) |
| 183 |
{ |
| 184 |
unregister_config_handler(":agentx", token); |
| 185 |
} |
| 186 |
#endif /* NETSNMP_FEATURE_REMOVE_AGENTX_UNREGISTER_CONFIG_HANDLER */ |
| 187 |
|
| 188 |
void |
| 189 |
agentx_config_init(void) |
| 190 |
{ |
| 191 |
int agent_role = netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, |
| 192 |
NETSNMP_DS_AGENT_ROLE); |
| 193 |
|
| 194 |
/* |
| 195 |
* Common tokens for master/subagent |
| 196 |
*/ |
| 197 |
netsnmp_register_default_domain("agentx", "unix tcp"); |
| 198 |
netsnmp_register_default_target("agentx", "unix", NETSNMP_AGENTX_SOCKET); |
| 199 |
#define val(x) __STRING(x) |
| 200 |
netsnmp_register_default_target("agentx", "tcp", |
| 201 |
"localhost:" val(AGENTX_PORT)); |
| 202 |
#undef val |
| 203 |
agentx_register_config_handler("agentxsocket", |
| 204 |
agentx_parse_agentx_socket, NULL, |
| 205 |
"AgentX bind address"); |
| 206 |
|
| 207 |
agentx_register_config_handler("agentxRetries", |
| 208 |
agentx_parse_agentx_retries, NULL, |
| 209 |
"AgentX Retries"); |
| 210 |
/* default to 5 retries */ |
| 211 |
netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, |
| 212 |
NETSNMP_DS_AGENT_AGENTX_RETRIES, 5); |
| 213 |
|
| 214 |
agentx_register_config_handler("agentxTimeout", |
| 215 |
agentx_parse_agentx_timeout, NULL, |
| 216 |
"AgentX Timeout (seconds)"); |
| 217 |
/* default to 1 second */ |
| 218 |
netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, |
| 219 |
NETSNMP_DS_AGENT_AGENTX_TIMEOUT, 1 * ONE_SEC); |
| 220 |
|
| 221 |
#ifdef USING_AGENTX_MASTER_MODULE |
| 222 |
/* |
| 223 |
* tokens for master agent |
| 224 |
*/ |
| 225 |
if (MASTER_AGENT == agent_role) { |
| 226 |
snmpd_register_config_handler("master", |
| 227 |
agentx_parse_master, NULL, |
| 228 |
"specify 'agentx' for AgentX support"); |
| 229 |
agentx_register_config_handler("agentxperms", |
| 230 |
agentx_parse_agentx_perms, NULL, |
| 231 |
"AgentX socket permissions: socket_perms [directory_perms [username|userid [groupname|groupid]]]"); |
| 232 |
} |
| 233 |
#endif /* USING_AGENTX_MASTER_MODULE */ |
| 234 |
|
| 235 |
#ifdef USING_AGENTX_SUBAGENT_MODULE |
| 236 |
/* |
| 237 |
* tokens for subagent |
| 238 |
*/ |
| 239 |
if (SUB_AGENT == agent_role) { |
| 240 |
agentx_register_config_handler("agentxPingInterval", |
| 241 |
agentx_parse_agentx_ping_interval, NULL, |
| 242 |
"AgentX ping interval"); |
| 243 |
/* ping and/or reconnect by default every 15 seconds */ |
| 244 |
netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, |
| 245 |
NETSNMP_DS_AGENT_AGENTX_PING_INTERVAL, 15); |
| 246 |
} |
| 247 |
#endif /* USING_AGENTX_SUBAGENT_MODULE */ |
| 248 |
} |