Coverage Report

Created: 2024-07-27 06:09

/src/net-snmp/agent/mibgroup/agentx/agentx_config.c
Line
Count
Source (jump to first uncovered line)
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
0
{
34
0
    DEBUGMSGTL(("agentx/config", "port spec: %s\n", cptr));
35
0
    netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_X_SOCKET, cptr);
36
0
}
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
0
{
46
0
    int             i = -1;
47
48
0
    if (!strcmp(cptr, "agentx") ||
49
0
        !strcmp(cptr, "all") ||
50
0
        !strcmp(cptr, "yes") || !strcmp(cptr, "on")) {
51
0
        i = 1;
52
0
        snmp_log(LOG_INFO, "Turning on AgentX master support.\n");
53
0
    } else if (!strcmp(cptr, "no") || !strcmp(cptr, "off"))
54
0
        i = 0;
55
0
    else
56
0
        i = atoi(cptr);
57
58
0
    if (i < 0 || i > 1) {
59
0
  netsnmp_config_error("master '%s' unrecognised", cptr);
60
0
    } else
61
0
        netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_AGENTX_MASTER, i);
62
0
}
63
64
void
65
agentx_parse_agentx_perms(const char *token, char *cptr)
66
0
{
67
0
    char *socket_perm, *dir_perm, *socket_user, *socket_group;
68
0
    int uid = -1;
69
0
    int gid = -1;
70
0
    int s_perm = -1;
71
0
    int d_perm = -1;
72
0
    char *st;
73
74
0
    DEBUGMSGTL(("agentx/config", "port permissions: %s\n", cptr));
75
0
    socket_perm = strtok_r(cptr, " \t", &st);
76
0
    dir_perm    = strtok_r(NULL, " \t", &st);
77
0
    socket_user = strtok_r(NULL, " \t", &st);
78
0
    socket_group = strtok_r(NULL, " \t", &st);
79
80
0
    if (socket_perm) {
81
0
        s_perm = strtol(socket_perm, NULL, 8);
82
0
        netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
83
0
                           NETSNMP_DS_AGENT_X_SOCK_PERM, s_perm);
84
0
        DEBUGMSGTL(("agentx/config", "socket permissions: %o (%d)\n",
85
0
                    s_perm, s_perm));
86
0
    }
87
0
    if (dir_perm) {
88
0
        d_perm = strtol(dir_perm, NULL, 8);
89
0
        netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
90
0
                           NETSNMP_DS_AGENT_X_DIR_PERM, d_perm);
91
0
        DEBUGMSGTL(("agentx/config", "directory permissions: %o (%d)\n",
92
0
                    d_perm, d_perm));
93
0
    }
94
95
    /*
96
     * Try to handle numeric UIDs or user names for the socket owner
97
     */
98
0
    if (socket_user) {
99
0
        uid = netsnmp_str_to_uid(socket_user);
100
0
        if ( uid != 0 )
101
0
            netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
102
0
                               NETSNMP_DS_AGENT_X_SOCK_USER, uid);
103
0
        DEBUGMSGTL(("agentx/config", "socket owner: %s (%d)\n",
104
0
                    socket_user, uid));
105
0
    }
106
107
    /*
108
     * and similarly for the socket group ownership
109
     */
110
0
    if (socket_group) {
111
0
        gid = netsnmp_str_to_gid(socket_group);
112
0
        if ( gid != 0 )
113
0
            netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
114
0
                               NETSNMP_DS_AGENT_X_SOCK_GROUP, gid);
115
0
        DEBUGMSGTL(("agentx/config", "socket group: %s (%d)\n",
116
0
                    socket_group, gid));
117
0
    }
118
0
}
119
120
void
121
agentx_parse_agentx_timeout(const char *token, char *cptr)
122
0
{
123
0
    int x = netsnmp_string_time_to_secs(cptr);
124
0
    DEBUGMSGTL(("agentx/config/timeout", "%s\n", cptr));
125
0
    if (x == -1) {
126
0
        config_perror("Invalid timeout value");
127
0
        return;
128
0
    }
129
0
    netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
130
0
                       NETSNMP_DS_AGENT_AGENTX_TIMEOUT, x * 1000L * 1000L);
131
0
}
132
133
void
134
agentx_parse_agentx_retries(const char *token, char *cptr)
135
0
{
136
0
    int x = atoi(cptr);
137
0
    DEBUGMSGTL(("agentx/config/retries", "%s\n", cptr));
138
0
    netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
139
0
                       NETSNMP_DS_AGENT_AGENTX_RETRIES, x);
140
0
}
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
0
{
147
0
    int x = atoi(cptr);
148
149
0
    DEBUGMSGTL(("agentx/config/ping", "%s\n", cptr));
150
0
    if (x < 1) {
151
0
        config_perror("Invalid ping interval value");
152
0
        return;
153
0
    }
154
0
    netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
155
0
                       NETSNMP_DS_AGENT_AGENTX_PING_INTERVAL, x);
156
0
}
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
11.2k
{
174
11.2k
    DEBUGMSGTL(("agentx_register_app_config_handler",
175
11.2k
                "registering .conf token for \"%s\"\n", token));
176
11.2k
    register_config_handler(":agentx", token, parser, releaser, help);
177
11.2k
}
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
0
{
184
0
    unregister_config_handler(":agentx", token);
185
0
}
186
#endif /* NETSNMP_FEATURE_REMOVE_AGENTX_UNREGISTER_CONFIG_HANDLER */
187
188
void
189
agentx_config_init(void)
190
2.81k
{
191
2.81k
    int agent_role = netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
192
2.81k
                                            NETSNMP_DS_AGENT_ROLE);
193
194
    /*
195
     * Common tokens for master/subagent
196
     */
197
2.81k
    netsnmp_register_default_domain("agentx", "unix tcp");
198
2.81k
    netsnmp_register_default_target("agentx", "unix", NETSNMP_AGENTX_SOCKET);
199
2.81k
#define val(x) __STRING(x)
200
2.81k
    netsnmp_register_default_target("agentx", "tcp",
201
2.81k
                                    "localhost:" val(AGENTX_PORT));
202
2.81k
#undef val
203
2.81k
    agentx_register_config_handler("agentxsocket",
204
2.81k
                                  agentx_parse_agentx_socket, NULL,
205
2.81k
                                  "AgentX bind address");
206
207
2.81k
    agentx_register_config_handler("agentxRetries",
208
2.81k
                                  agentx_parse_agentx_retries, NULL,
209
2.81k
                                  "AgentX Retries");
210
    /* default to 5 retries */
211
2.81k
    netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
212
2.81k
                       NETSNMP_DS_AGENT_AGENTX_RETRIES, 5);
213
214
2.81k
    agentx_register_config_handler("agentxTimeout",
215
2.81k
                                  agentx_parse_agentx_timeout, NULL,
216
2.81k
                                  "AgentX Timeout (seconds)");
217
    /* default to 1 second */
218
2.81k
    netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
219
2.81k
                       NETSNMP_DS_AGENT_AGENTX_TIMEOUT, 1000L * 1000L);
220
221
2.81k
#ifdef USING_AGENTX_MASTER_MODULE
222
    /*
223
     * tokens for master agent
224
     */
225
2.81k
    if (MASTER_AGENT == agent_role) {
226
2.81k
        snmpd_register_config_handler("master",
227
2.81k
                                      agentx_parse_master, NULL,
228
2.81k
                                      "specify 'agentx' for AgentX support");
229
2.81k
    agentx_register_config_handler("agentxperms",
230
2.81k
                                  agentx_parse_agentx_perms, NULL,
231
2.81k
                                  "AgentX socket permissions: socket_perms [directory_perms [username|userid [groupname|groupid]]]");
232
2.81k
    }
233
2.81k
#endif                          /* USING_AGENTX_MASTER_MODULE */
234
235
2.81k
#ifdef USING_AGENTX_SUBAGENT_MODULE
236
    /*
237
     * tokens for subagent
238
     */
239
2.81k
    if (SUB_AGENT == agent_role) {
240
0
      agentx_register_config_handler("agentxPingInterval",
241
0
                                     agentx_parse_agentx_ping_interval, NULL,
242
0
                                     "AgentX ping interval");
243
      /* ping and/or reconnect by default every 15 seconds */
244
0
      netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
245
0
                         NETSNMP_DS_AGENT_AGENTX_PING_INTERVAL, 15);
246
0
    }
247
2.81k
#endif /* USING_AGENTX_SUBAGENT_MODULE */
248
2.81k
}