Coverage Report

Created: 2024-09-03 06:48

/src/net-snmp/agent/agent_read_config.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * agent_read_config.c
3
 *
4
 * Portions of this file are copyrighted by:
5
 * Copyright (c) 2016 VMware, Inc. All rights reserved.
6
 * Use is subject to license terms specified in the COPYING file
7
 * distributed with the Net-SNMP package.
8
 */
9
10
#include <net-snmp/net-snmp-config.h>
11
#include <net-snmp/net-snmp-features.h>
12
13
#ifdef HAVE_SYS_PARAM_H
14
#include <sys/param.h>
15
#else
16
#include <sys/types.h>
17
#endif
18
#ifdef HAVE_STDLIB_H
19
#include <stdlib.h>
20
#endif
21
#ifdef HAVE_STRING_H
22
#include <string.h>
23
#else
24
#include <strings.h>
25
#endif
26
#include <stdio.h>
27
#include <ctype.h>
28
#include <errno.h>
29
30
#ifdef TIME_WITH_SYS_TIME
31
# include <sys/time.h>
32
# include <time.h>
33
#else
34
# ifdef HAVE_SYS_TIME_H
35
#  include <sys/time.h>
36
# else
37
#  include <time.h>
38
# endif
39
#endif
40
#ifdef HAVE_NETINET_IN_H
41
#include <netinet/in.h>
42
#endif
43
#ifdef HAVE_NETINET_IN_SYSTM_H
44
#include <netinet/in_systm.h>
45
#endif
46
#ifdef HAVE_NETINET_IP_H
47
#include <netinet/ip.h>
48
#endif
49
#ifdef NETSNMP_ENABLE_IPV6
50
#ifdef HAVE_NETINET_IP6_H
51
#include <netinet/ip6.h>
52
#endif
53
#endif
54
#ifdef HAVE_SYS_QUEUE_H
55
#include <sys/queue.h>
56
#endif
57
#ifdef HAVE_SYS_SOCKET_H
58
#include <sys/socket.h>
59
#ifdef HAVE_SYS_SOCKETVAR_H
60
#ifndef dynix
61
#include <sys/socketvar.h>
62
#else
63
#include <sys/param.h>
64
#endif
65
#endif
66
#endif
67
#ifdef HAVE_SYS_STREAM_H
68
#   ifdef sysv5UnixWare7
69
#      define _KMEMUSER 1   /* <sys/stream.h> needs this for queue_t */
70
#   endif
71
#include <sys/stream.h>
72
#endif
73
#ifdef HAVE_NET_ROUTE_H
74
#include <net/route.h>
75
#endif
76
#ifdef HAVE_NETINET_IP_VAR_H
77
#include <netinet/ip_var.h>
78
#endif
79
#ifdef NETSNMP_ENABLE_IPV6
80
#ifdef HAVE_NETNETSNMP_ENABLE_IPV6_IP6_VAR_H
81
#include <netinet6/ip6_var.h>
82
#endif
83
#endif
84
#ifdef HAVE_NETINET_IN_PCB_H
85
#include <netinet/in_pcb.h>
86
#endif
87
#ifdef HAVE_INET_MIB2_H
88
#include <inet/mib2.h>
89
#endif
90
91
#ifdef HAVE_UNISTD_H
92
#include <unistd.h>
93
#endif
94
#ifdef HAVE_PWD_H
95
#include <pwd.h>
96
#endif
97
#ifdef HAVE_GRP_H
98
#include <grp.h>
99
#endif
100
101
#include <net-snmp/net-snmp-includes.h>
102
#include <net-snmp/agent/net-snmp-agent-includes.h>
103
104
#include "mibgroup/struct.h"
105
#include <net-snmp/agent/agent_trap.h>
106
#include "snmpd.h"
107
#include <net-snmp/agent/agent_callbacks.h>
108
#include <net-snmp/agent/table.h>
109
#include <net-snmp/agent/table_iterator.h>
110
#include <net-snmp/agent/table_data.h>
111
#include <net-snmp/agent/table_dataset.h>
112
#include "agent_module_includes.h"
113
#include "mib_module_includes.h"
114
115
netsnmp_feature_child_of(agent_read_config_all, libnetsnmpagent);
116
117
netsnmp_feature_child_of(snmpd_unregister_config_handler, agent_read_config_all);
118
119
void netsnmp_set_agent_user_id(int uid)
120
0
{
121
0
    netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_USERID, uid);
122
0
}
123
124
void netsnmp_set_agent_group_id(int gid)
125
0
{
126
0
    netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_GROUPID,
127
0
                       gid);
128
0
}
129
130
#ifdef HAVE_UNISTD_H
131
void
132
snmpd_set_agent_user(const char *token, char *cptr)
133
0
{
134
0
    if (cptr[0] == '#') {
135
0
        char           *ecp;
136
0
        int             uid;
137
138
0
        uid = strtoul(cptr + 1, &ecp, 10);
139
0
        if (*ecp != 0) {
140
0
            config_perror("Bad number");
141
0
  } else {
142
0
            netsnmp_set_agent_user_id(uid);
143
0
  }
144
0
#if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
145
0
    } else {
146
0
        struct passwd  *info;
147
148
0
        info = getpwnam(cptr);
149
0
        if (info)
150
0
            netsnmp_set_agent_user_id(info->pw_uid);
151
0
        else
152
0
            config_perror("User not found in password database");
153
0
        endpwent();
154
0
#endif
155
0
    }
156
0
}
157
158
void
159
snmpd_set_agent_group(const char *token, char *cptr)
160
0
{
161
0
    if (cptr[0] == '#') {
162
0
        char           *ecp;
163
0
        int             gid = strtoul(cptr + 1, &ecp, 10);
164
165
0
        if (*ecp != 0) {
166
0
            config_perror("Bad number");
167
0
  } else {
168
0
            netsnmp_set_agent_group_id(gid);
169
0
  }
170
0
#if defined(HAVE_GETGRNAM) && defined(HAVE_GRP_H)
171
0
    } else {
172
0
        struct group   *info;
173
174
0
        info = getgrnam(cptr);
175
0
        if (info)
176
0
            netsnmp_set_agent_group_id(info->gr_gid);
177
0
        else
178
0
            config_perror("Group not found in group database");
179
0
        endgrent();
180
0
#endif
181
0
    }
182
0
}
183
#endif
184
185
#ifndef NETSNMP_NO_LISTEN_SUPPORT
186
void
187
snmpd_set_agent_address(const char *token, char *cptr)
188
0
{
189
0
    char            buf[SPRINT_MAX_LEN];
190
0
    char           *ptr;
191
192
    /*
193
     * has something been specified before? 
194
     */
195
0
    ptr = netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID, 
196
0
        NETSNMP_DS_AGENT_PORTS);
197
198
0
    if (ptr) {
199
        /*
200
         * append to the older specification string 
201
         */
202
0
        snprintf(buf, sizeof(buf), "%s,%s", ptr, cptr);
203
0
    } else {
204
0
        strlcpy(buf, cptr, sizeof(buf));
205
0
    }
206
207
0
    DEBUGMSGTL(("snmpd_ports", "port spec: %s\n", buf));
208
0
    netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID, 
209
0
        NETSNMP_DS_AGENT_PORTS, buf);
210
0
}
211
#endif /* NETSNMP_NO_LISTEN_SUPPORT */
212
213
void
214
init_agent_read_config(const char *app)
215
2.79k
{
216
2.79k
    if (app != NULL) {
217
2.79k
        netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, 
218
2.79k
            NETSNMP_DS_LIB_APPTYPE, app);
219
2.79k
    } else {
220
0
        app = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 
221
0
            NETSNMP_DS_LIB_APPTYPE);
222
0
    }
223
224
2.79k
    register_app_config_handler("authtrapenable",
225
2.79k
                                snmpd_parse_config_authtrap, NULL,
226
2.79k
                                "1 | 2\t\t(1 = enable, 2 = disable)");
227
2.79k
    register_app_config_handler("pauthtrapenable",
228
2.79k
                                snmpd_parse_config_authtrap, NULL, NULL);
229
230
231
2.79k
    if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 
232
2.79k
             NETSNMP_DS_AGENT_ROLE) == MASTER_AGENT) {
233
2.79k
#ifndef NETSNMP_DISABLE_SNMPV1
234
2.79k
        register_app_config_handler("trapsink",
235
2.79k
                                    snmpd_parse_config_trapsink,
236
2.79k
                                    snmpd_free_trapsinks,
237
2.79k
                                    "[-profile p] [-name n] [-tag t] host [community] [port]");
238
2.79k
#endif
239
2.79k
#ifndef NETSNMP_DISABLE_SNMPV2C
240
2.79k
        register_app_config_handler("trap2sink",
241
2.79k
                                    snmpd_parse_config_trap2sink, 
242
2.79k
                                    snmpd_free_trapsinks,
243
2.79k
                                    "[-profile p] [-name n] [-tag t] host [community] [port]");
244
2.79k
        register_app_config_handler("informsink",
245
2.79k
                                    snmpd_parse_config_informsink,
246
2.79k
                                    snmpd_free_trapsinks,
247
2.79k
                                    "[-profile p] [-name n] [-tag t] host [community] [port]");
248
2.79k
#endif
249
2.79k
        register_app_config_handler("trapsess",
250
2.79k
                                    snmpd_parse_config_trapsess,
251
2.79k
                                    snmpd_free_trapsinks,
252
2.79k
                                    "[-profile p] [-name n] [-tag t] [snmpcmdargs] host");
253
2.79k
    }
254
2.79k
#if !defined(NETSNMP_DISABLE_SNMPV1) || !defined(NETSNMP_DISABLE_SNMPV2C)
255
2.79k
    register_app_config_handler("trapcommunity",
256
2.79k
                                snmpd_parse_config_trapcommunity,
257
2.79k
                                snmpd_free_trapcommunity,
258
2.79k
                                "community-string");
259
2.79k
#endif /* support for community based SNMP */
260
2.79k
    netsnmp_ds_register_config(ASN_OCTET_STR, app, "v1trapaddress", 
261
2.79k
                               NETSNMP_DS_APPLICATION_ID, 
262
2.79k
                               NETSNMP_DS_AGENT_TRAP_ADDR);
263
2.79k
#ifdef HAVE_UNISTD_H
264
2.79k
    register_app_config_handler("agentuser",
265
2.79k
                                snmpd_set_agent_user, NULL, "userid");
266
2.79k
    register_app_config_handler("agentgroup",
267
2.79k
                                snmpd_set_agent_group, NULL, "groupid");
268
2.79k
#endif
269
2.79k
#ifndef NETSNMP_NO_LISTEN_SUPPORT
270
2.79k
    register_app_config_handler("agentaddress",
271
2.79k
                                snmpd_set_agent_address, NULL,
272
2.79k
                                "SNMP bind address");
273
2.79k
#endif /* NETSNMP_NO_LISTEN_SUPPORT */
274
2.79k
    netsnmp_ds_register_config(ASN_BOOLEAN, app, "quit", 
275
2.79k
             NETSNMP_DS_APPLICATION_ID,
276
2.79k
             NETSNMP_DS_AGENT_QUIT_IMMEDIATELY);
277
2.79k
    netsnmp_ds_register_config(ASN_BOOLEAN, app, "leave_pidfile", 
278
2.79k
             NETSNMP_DS_APPLICATION_ID,
279
2.79k
             NETSNMP_DS_AGENT_LEAVE_PIDFILE);
280
2.79k
    netsnmp_ds_register_config(ASN_BOOLEAN, app, "dontLogTCPWrappersConnects",
281
2.79k
                               NETSNMP_DS_APPLICATION_ID,
282
2.79k
                               NETSNMP_DS_AGENT_DONT_LOG_TCPWRAPPERS_CONNECTS);
283
2.79k
    netsnmp_ds_register_config(ASN_INTEGER, app, "maxGetbulkRepeats",
284
2.79k
                               NETSNMP_DS_APPLICATION_ID,
285
2.79k
                               NETSNMP_DS_AGENT_MAX_GETBULKREPEATS);
286
2.79k
    netsnmp_ds_register_config(ASN_INTEGER, app, "maxGetbulkResponses",
287
2.79k
                               NETSNMP_DS_APPLICATION_ID,
288
2.79k
                               NETSNMP_DS_AGENT_MAX_GETBULKRESPONSES);
289
2.79k
    netsnmp_ds_register_config(ASN_INTEGER, app, "avgBulkVarbindSize",
290
2.79k
                               NETSNMP_DS_APPLICATION_ID,
291
2.79k
                               NETSNMP_DS_AGENT_AVG_BULKVARBINDSIZE);
292
2.79k
#ifndef NETSNMP_NO_PDU_STATS
293
2.79k
    netsnmp_ds_register_config(ASN_INTEGER, app, "pduStatsMax",
294
2.79k
                               NETSNMP_DS_APPLICATION_ID,
295
2.79k
                               NETSNMP_DS_AGENT_PDU_STATS_MAX);
296
2.79k
    netsnmp_ds_register_config(ASN_INTEGER, app, "pduStatsThreshold",
297
2.79k
                               NETSNMP_DS_APPLICATION_ID,
298
2.79k
                               NETSNMP_DS_AGENT_PDU_STATS_THRESHOLD);
299
2.79k
#endif /* NETSNMP_NO_PDU_STATS */
300
301
2.79k
    netsnmp_init_handler_conf();
302
303
2.79k
#include "agent_module_dot_conf.h"
304
2.79k
#include "mib_module_dot_conf.h"
305
#ifdef TESTING
306
    print_config_handlers();
307
#endif
308
2.79k
}
309
310
void
311
update_config(void)
312
0
{
313
0
    snmp_call_callbacks(SNMP_CALLBACK_APPLICATION,
314
0
                        SNMPD_CALLBACK_PRE_UPDATE_CONFIG, NULL);
315
0
    free_config();
316
0
    read_configs();
317
0
}
318
319
320
void
321
snmpd_register_config_handler(const char *token,
322
                              void (*parser) (const char *, char *),
323
                              void (*releaser) (void), const char *help)
324
50.3k
{
325
50.3k
    DEBUGMSGTL(("snmpd_register_app_config_handler",
326
50.3k
                "registering .conf token for \"%s\"\n", token));
327
50.3k
    register_app_config_handler(token, parser, releaser, help);
328
50.3k
}
329
330
void
331
snmpd_register_const_config_handler(const char *token,
332
                                    void (*parser) (const char *, const char *),
333
                                    void (*releaser) (void), const char *help)
334
11.1k
{
335
11.1k
    DEBUGMSGTL(("snmpd_register_app_config_handler",
336
11.1k
                "registering .conf token for \"%s\"\n", token));
337
11.1k
    register_const_app_config_handler(token, parser, releaser, help);
338
11.1k
}
339
340
#ifdef NETSNMP_FEATURE_REQUIRE_SNMPD_UNREGISTER_CONFIG_HANDLER
341
netsnmp_feature_require(unregister_app_config_handler);
342
#endif /* NETSNMP_FEATURE_REQUIRE_SNMPD_UNREGISTER_CONFIG_HANDLER */
343
344
#ifndef NETSNMP_FEATURE_REMOVE_SNMPD_UNREGISTER_CONFIG_HANDLER
345
void
346
snmpd_unregister_config_handler(const char *token)
347
0
{
348
0
    unregister_app_config_handler(token);
349
0
}
350
#endif /* NETSNMP_FEATURE_REMOVE_SNMPD_UNREGISTER_CONFIG_HANDLER */
351
352
/*
353
 * this function is intended for use by mib-modules to store permanent
354
 * configuration information generated by sets or persistent counters 
355
 */
356
void
357
snmpd_store_config(const char *line)
358
0
{
359
0
    read_app_config_store(line);
360
0
}