and added files
[bcm963xx.git] / userapps / opensource / net-snmp / agent / agent_read_config.c
1 /*
2  * agent_read_config.c
3  */
4
5 #include <net-snmp/net-snmp-config.h>
6
7 #include <sys/types.h>
8 #if HAVE_STDLIB_H
9 #include <stdlib.h>
10 #endif
11 #if HAVE_STRING_H
12 #include <string.h>
13 #else
14 #include <strings.h>
15 #endif
16 #include <stdio.h>
17 #include <ctype.h>
18 #include <errno.h>
19
20 #if TIME_WITH_SYS_TIME
21 # ifdef WIN32
22 #  include <sys/timeb.h>
23 # else
24 #  include <sys/time.h>
25 # endif
26 # include <time.h>
27 #else
28 # if HAVE_SYS_TIME_H
29 #  include <sys/time.h>
30 # else
31 #  include <time.h>
32 # endif
33 #endif
34 #if HAVE_NETINET_IN_H
35 #include <netinet/in.h>
36 #endif
37 #if HAVE_NETINET_IN_SYSTM_H
38 #include <netinet/in_systm.h>
39 #endif
40 #if HAVE_NETINET_IP_H
41 #include <netinet/ip.h>
42 #endif
43 #ifdef INET6
44 #if HAVE_NETINET_IP6_H
45 #include <netinet/ip6.h>
46 #endif
47 #endif
48 #if HAVE_SYS_QUEUE_H
49 #include <sys/queue.h>
50 #endif
51 #if HAVE_SYS_SOCKET_H
52 #include <sys/socket.h>
53 #if HAVE_SYS_SOCKETVAR_H
54 #ifndef dynix
55 #include <sys/socketvar.h>
56 #else
57 #include <sys/param.h>
58 #endif
59 #endif
60 #elif HAVE_WINSOCK_H
61 #include <winsock.h>
62 #endif
63 #if HAVE_SYS_STREAM_H
64 #include <sys/stream.h>
65 #endif
66 #if HAVE_NET_ROUTE_H
67 #include <net/route.h>
68 #endif
69 #if HAVE_NETINET_IP_VAR_H
70 #include <netinet/ip_var.h>
71 #endif
72 #ifdef INET6
73 #if HAVE_NETINET6_IP6_VAR_H
74 #include <netinet6/ip6_var.h>
75 #endif
76 #endif
77 #if HAVE_NETINET_IN_PCB_H
78 #include <netinet/in_pcb.h>
79 #endif
80 #if HAVE_INET_MIB2_H
81 #include <inet/mib2.h>
82 #endif
83
84 #if HAVE_DMALLOC_H
85 #include <dmalloc.h>
86 #endif
87
88 #if HAVE_UNISTD_H
89 #include <unistd.h>
90 #endif
91 #ifdef HAVE_PWD_H
92 #include <pwd.h>
93 #endif
94 #ifdef HAVE_GRP_H
95 #include <grp.h>
96 #endif
97
98 #include <net-snmp/net-snmp-includes.h>
99 #include <net-snmp/agent/net-snmp-agent-includes.h>
100
101 #include "mibgroup/struct.h"
102 #include <net-snmp/agent/agent_trap.h>
103 #include "snmpd.h"
104 #include <net-snmp/agent/agent_callbacks.h>
105 #include <net-snmp/agent/table.h>
106 #include <net-snmp/agent/table_iterator.h>
107 #include <net-snmp/agent/table_data.h>
108 #include <net-snmp/agent/table_dataset.h>
109 #include "mib_module_includes.h"
110
111 char            dontReadConfigFiles;
112 char           *optconfigfile;
113
114 #ifdef BRCM_SNMP_SUPPORT
115 #ifdef HAVE_UNISTD_H
116 void
117 snmpd_set_agent_user(const char *token, char *cptr)
118 {
119 #if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
120     struct passwd  *info;
121 #endif
122
123     if (cptr[0] == '#') {
124         char           *ecp;
125         int             uid;
126         uid = strtoul(cptr + 1, &ecp, 10);
127         if (*ecp != 0) {
128             config_perror("Bad number");
129         } else {
130             netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, 
131                                NETSNMP_DS_AGENT_USERID, uid);
132         }
133     }
134 #if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
135     else if ((info = getpwnam(cptr)) != NULL) {
136         netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, 
137                            NETSNMP_DS_AGENT_USERID, info->pw_uid);
138     } else {
139         config_perror("User not found in passwd database");
140     }
141 #endif
142 }
143
144 void
145 snmpd_set_agent_group(const char *token, char *cptr)
146 {
147 #if defined(HAVE_GETGRNAM) && defined(HAVE_GRP_H)
148     struct group   *info;
149 #endif
150
151     if (cptr[0] == '#') {
152         char           *ecp;
153         int             gid = strtoul(cptr + 1, &ecp, 10);
154         if (*ecp != 0) {
155             config_perror("Bad number");
156         } else {
157             netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, 
158                                NETSNMP_DS_AGENT_GROUPID, gid);
159         }
160     }
161 #if defined(HAVE_GETGRNAM) && defined(HAVE_GRP_H)
162     else if ((info = getgrnam(cptr)) != NULL) {
163         netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, 
164                            NETSNMP_DS_AGENT_GROUPID, info->gr_gid);
165     } else {
166         config_perror("Group not found in group database");
167     }
168 #endif
169 }
170 #endif
171
172 void
173 snmpd_set_agent_address(const char *token, char *cptr)
174 {
175     char            buf[SPRINT_MAX_LEN];
176     char           *ptr;
177
178     /*
179      * has something been specified before? 
180      */
181     ptr = netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID, 
182                                 NETSNMP_DS_AGENT_PORTS);
183
184     if (ptr) {
185         /*
186          * append to the older specification string 
187          */
188         sprintf(buf, "%s,%s", ptr, cptr);
189     } else {
190         strcpy(buf, cptr);
191     }
192
193     DEBUGMSGTL(("snmpd_ports", "port spec: %s\n", buf));
194     netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID, 
195                           NETSNMP_DS_AGENT_PORTS, buf);
196 }
197 #endif /* BRCM_SNMP_SUPPORT */
198
199 void
200 init_agent_read_config(const char *app)
201 {
202     if (app != NULL) {
203         netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, 
204                               NETSNMP_DS_LIB_APPTYPE, app);
205     } else {
206         app = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 
207                                     NETSNMP_DS_LIB_APPTYPE);
208     }
209
210 #ifdef BRCM_SNMP_CONFIG_SUPPORT
211     register_app_config_handler("authtrapenable",
212                                 snmpd_parse_config_authtrap, NULL,
213                                 "1 | 2\t\t(1 = enable, 2 = disable)");
214     register_app_config_handler("pauthtrapenable",
215                                 snmpd_parse_config_authtrap, NULL, NULL);
216 #endif
217
218     if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 
219                                NETSNMP_DS_AGENT_ROLE) == MASTER_AGENT) {
220         register_app_config_handler("trapsink",
221                                     snmpd_parse_config_trapsink,
222                                     snmpd_free_trapsinks,
223                                     "host [community] [port]");
224         register_app_config_handler("trap2sink",
225                                     snmpd_parse_config_trap2sink, NULL,
226                                     "host [community] [port]");
227
228 #ifdef BRCM_SNMP_CONFIG_SUPPORT
229         register_app_config_handler("informsink",
230                                     snmpd_parse_config_informsink, NULL,
231                                     "host [community] [port]");
232         register_app_config_handler("trapsess",
233                                     snmpd_parse_config_trapsess, NULL,
234                                     "[snmpcmdargs] host");
235 #endif
236     }
237     register_app_config_handler("trapcommunity",
238                                 snmpd_parse_config_trapcommunity,
239                                 snmpd_free_trapcommunity,
240                                 "community-string");
241 #ifdef BRCM_SNMP_SUPPORT
242 #ifdef HAVE_UNISTD_H
243     register_app_config_handler("agentuser",
244                                 snmpd_set_agent_user, NULL, "userid");
245     register_app_config_handler("agentgroup",
246                                 snmpd_set_agent_group, NULL, "groupid");
247 #endif
248     register_app_config_handler("agentaddress",
249                                 snmpd_set_agent_address, NULL,
250                                 "SNMP bind address");
251     register_app_config_handler("table",
252                                 netsnmp_config_parse_table_set, NULL,
253                                 "tableoid");
254     register_app_config_handler("add_row", netsnmp_config_parse_add_row,
255                                 NULL, "indexes... values...");
256     netsnmp_ds_register_config(ASN_BOOLEAN, app, "quit", 
257                                NETSNMP_DS_APPLICATION_ID,
258                                NETSNMP_DS_AGENT_QUIT_IMMEDIATELY);
259     netsnmp_ds_register_config(ASN_BOOLEAN, app, "leave_pidfile", 
260                                NETSNMP_DS_APPLICATION_ID,
261                                NETSNMP_DS_AGENT_LEAVE_PIDFILE);
262     netsnmp_init_handler_conf();
263 #endif /* BRCM_SNMP_SUPPORT */
264
265 #include "mib_module_dot_conf.h"
266
267 #ifdef TESTING
268     print_config_handlers();
269 #endif
270 }
271
272 void
273 update_config(void)
274 {
275     snmp_call_callbacks(SNMP_CALLBACK_APPLICATION,
276                         SNMPD_CALLBACK_PRE_UPDATE_CONFIG, NULL);
277     free_config();
278     read_configs();
279 }
280
281
282 void
283 snmpd_register_config_handler(const char *token,
284                               void (*parser) (const char *, char *),
285                               void (*releaser) (void), const char *help)
286 {
287     DEBUGMSGTL(("snmpd_register_app_config_handler",
288                 "registering .conf token for \"%s\"\n", token));
289     register_app_config_handler(token, parser, releaser, help);
290 }
291
292 void
293 snmpd_unregister_config_handler(const char *token)
294 {
295     unregister_app_config_handler(token);
296 }
297
298 /*
299  * this function is intended for use by mib-modules to store permenant
300  * configuration information generated by sets or persistent counters 
301  */
302 void
303 snmpd_store_config(const char *line)
304 {
305     read_app_config_store(line);
306 }