net-snmp 5.7
agent_read_config.c
00001 /*
00002  * agent_read_config.c
00003  */
00004 
00005 #include <net-snmp/net-snmp-config.h>
00006 #include <net-snmp/net-snmp-features.h>
00007 
00008 #include <sys/types.h>
00009 #if HAVE_STDLIB_H
00010 #include <stdlib.h>
00011 #endif
00012 #if HAVE_STRING_H
00013 #include <string.h>
00014 #else
00015 #include <strings.h>
00016 #endif
00017 #include <stdio.h>
00018 #include <ctype.h>
00019 #include <errno.h>
00020 
00021 #if TIME_WITH_SYS_TIME
00022 # include <sys/time.h>
00023 # include <time.h>
00024 #else
00025 # if HAVE_SYS_TIME_H
00026 #  include <sys/time.h>
00027 # else
00028 #  include <time.h>
00029 # endif
00030 #endif
00031 #if HAVE_NETINET_IN_H
00032 #include <netinet/in.h>
00033 #endif
00034 #if HAVE_NETINET_IN_SYSTM_H
00035 #include <netinet/in_systm.h>
00036 #endif
00037 #if HAVE_NETINET_IP_H
00038 #include <netinet/ip.h>
00039 #endif
00040 #ifdef NETSNMP_ENABLE_IPV6
00041 #if HAVE_NETINET_IP6_H
00042 #include <netinet/ip6.h>
00043 #endif
00044 #endif
00045 #if HAVE_SYS_QUEUE_H
00046 #include <sys/queue.h>
00047 #endif
00048 #if HAVE_SYS_SOCKET_H
00049 #include <sys/socket.h>
00050 #if HAVE_SYS_SOCKETVAR_H
00051 #ifndef dynix
00052 #include <sys/socketvar.h>
00053 #else
00054 #include <sys/param.h>
00055 #endif
00056 #endif
00057 #endif
00058 #if HAVE_SYS_STREAM_H
00059 #   ifdef sysv5UnixWare7
00060 #      define _KMEMUSER 1   /* <sys/stream.h> needs this for queue_t */
00061 #   endif
00062 #include <sys/stream.h>
00063 #endif
00064 #if HAVE_NET_ROUTE_H
00065 #include <net/route.h>
00066 #endif
00067 #if HAVE_NETINET_IP_VAR_H
00068 #include <netinet/ip_var.h>
00069 #endif
00070 #ifdef NETSNMP_ENABLE_IPV6
00071 #if HAVE_NETNETSNMP_ENABLE_IPV6_IP6_VAR_H
00072 #include <netinet6/ip6_var.h>
00073 #endif
00074 #endif
00075 #if HAVE_NETINET_IN_PCB_H
00076 #include <netinet/in_pcb.h>
00077 #endif
00078 #if HAVE_INET_MIB2_H
00079 #include <inet/mib2.h>
00080 #endif
00081 
00082 #if HAVE_UNISTD_H
00083 #include <unistd.h>
00084 #endif
00085 #ifdef HAVE_PWD_H
00086 #include <pwd.h>
00087 #endif
00088 #ifdef HAVE_GRP_H
00089 #include <grp.h>
00090 #endif
00091 
00092 #include <net-snmp/net-snmp-includes.h>
00093 #include <net-snmp/agent/net-snmp-agent-includes.h>
00094 
00095 #include "mibgroup/struct.h"
00096 #include <net-snmp/agent/agent_trap.h>
00097 #include "snmpd.h"
00098 #include <net-snmp/agent/agent_callbacks.h>
00099 #include <net-snmp/agent/table.h>
00100 #include <net-snmp/agent/table_iterator.h>
00101 #include <net-snmp/agent/table_data.h>
00102 #include <net-snmp/agent/table_dataset.h>
00103 #include "agent_module_includes.h"
00104 #include "mib_module_includes.h"
00105 
00106 netsnmp_feature_child_of(agent_read_config_all, libnetsnmpagent)
00107 
00108 netsnmp_feature_child_of(snmpd_unregister_config_handler, agent_read_config_all)
00109 
00110 #ifdef HAVE_UNISTD_H
00111 void
00112 snmpd_set_agent_user(const char *token, char *cptr)
00113 {
00114 #if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
00115     struct passwd  *info;
00116 #endif
00117 
00118     if (cptr[0] == '#') {
00119         char           *ecp;
00120         int             uid;
00121         uid = strtoul(cptr + 1, &ecp, 10);
00122         if (*ecp != 0) {
00123             config_perror("Bad number");
00124         } else {
00125             netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, 
00126                                NETSNMP_DS_AGENT_USERID, uid);
00127         }
00128     }
00129 #if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
00130     else if ((info = getpwnam(cptr)) != NULL) {
00131         netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, 
00132                            NETSNMP_DS_AGENT_USERID, info->pw_uid);
00133     } else {
00134         config_perror("User not found in passwd database");
00135     }
00136     endpwent();
00137 #endif
00138 }
00139 
00140 void
00141 snmpd_set_agent_group(const char *token, char *cptr)
00142 {
00143 #if defined(HAVE_GETGRNAM) && defined(HAVE_GRP_H)
00144     struct group   *info;
00145 #endif
00146 
00147     if (cptr[0] == '#') {
00148         char           *ecp;
00149         int             gid = strtoul(cptr + 1, &ecp, 10);
00150         if (*ecp != 0) {
00151             config_perror("Bad number");
00152         } else {
00153             netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, 
00154                                NETSNMP_DS_AGENT_GROUPID, gid);
00155         }
00156     }
00157 #if defined(HAVE_GETGRNAM) && defined(HAVE_GRP_H)
00158     else if ((info = getgrnam(cptr)) != NULL) {
00159         netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, 
00160                            NETSNMP_DS_AGENT_GROUPID, info->gr_gid);
00161     } else {
00162         config_perror("Group not found in group database");
00163     }
00164     endpwent();
00165 #endif
00166 }
00167 #endif
00168 
00169 #ifndef NETSNMP_NO_LISTEN_SUPPORT
00170 void
00171 snmpd_set_agent_address(const char *token, char *cptr)
00172 {
00173     char            buf[SPRINT_MAX_LEN];
00174     char           *ptr;
00175 
00176     /*
00177      * has something been specified before? 
00178      */
00179     ptr = netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID, 
00180                                 NETSNMP_DS_AGENT_PORTS);
00181 
00182     if (ptr) {
00183         /*
00184          * append to the older specification string 
00185          */
00186         snprintf(buf, SPRINT_MAX_LEN, "%s,%s", ptr, cptr);
00187     } else {
00188         strncpy(buf, cptr, SPRINT_MAX_LEN);
00189     }
00190 
00191     DEBUGMSGTL(("snmpd_ports", "port spec: %s\n", buf));
00192     netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID, 
00193                           NETSNMP_DS_AGENT_PORTS, buf);
00194 }
00195 #endif /* NETSNMP_NO_LISTEN_SUPPORT */
00196 
00197 void
00198 init_agent_read_config(const char *app)
00199 {
00200     if (app != NULL) {
00201         netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, 
00202                               NETSNMP_DS_LIB_APPTYPE, app);
00203     } else {
00204         app = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 
00205                                     NETSNMP_DS_LIB_APPTYPE);
00206     }
00207 
00208     register_app_config_handler("authtrapenable",
00209                                 snmpd_parse_config_authtrap, NULL,
00210                                 "1 | 2\t\t(1 = enable, 2 = disable)");
00211     register_app_config_handler("pauthtrapenable",
00212                                 snmpd_parse_config_authtrap, NULL, NULL);
00213 
00214 
00215     if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 
00216                                NETSNMP_DS_AGENT_ROLE) == MASTER_AGENT) {
00217 #ifndef NETSNMP_DISABLE_SNMPV1
00218         register_app_config_handler("trapsink",
00219                                     snmpd_parse_config_trapsink,
00220                                     snmpd_free_trapsinks,
00221                                     "host [community] [port]");
00222 #endif
00223 #ifndef NETSNMP_DISABLE_SNMPV2C
00224         register_app_config_handler("trap2sink",
00225                                     snmpd_parse_config_trap2sink, 
00226                                     snmpd_free_trapsinks,
00227                                     "host [community] [port]");
00228         register_app_config_handler("informsink",
00229                                     snmpd_parse_config_informsink,
00230                                     snmpd_free_trapsinks,
00231                                     "host [community] [port]");
00232 #endif
00233         register_app_config_handler("trapsess",
00234                                     snmpd_parse_config_trapsess,
00235                                     snmpd_free_trapsinks,
00236                                     "[snmpcmdargs] host");
00237     }
00238 #if !defined(NETSNMP_DISABLE_SNMPV1) || !defined(NETSNMP_DISABLE_SNMPV2C)
00239     register_app_config_handler("trapcommunity",
00240                                 snmpd_parse_config_trapcommunity,
00241                                 snmpd_free_trapcommunity,
00242                                 "community-string");
00243 #endif /* support for community based SNMP */
00244     netsnmp_ds_register_config(ASN_OCTET_STR, app, "v1trapaddress", 
00245                                NETSNMP_DS_APPLICATION_ID, 
00246                                NETSNMP_DS_AGENT_TRAP_ADDR);
00247 #ifdef HAVE_UNISTD_H
00248     register_app_config_handler("agentuser",
00249                                 snmpd_set_agent_user, NULL, "userid");
00250     register_app_config_handler("agentgroup",
00251                                 snmpd_set_agent_group, NULL, "groupid");
00252 #endif
00253 #ifndef NETSNMP_NO_LISTEN_SUPPORT
00254     register_app_config_handler("agentaddress",
00255                                 snmpd_set_agent_address, NULL,
00256                                 "SNMP bind address");
00257 #endif /* NETSNMP_NO_LISTEN_SUPPORT */
00258     netsnmp_ds_register_config(ASN_BOOLEAN, app, "quit", 
00259                                NETSNMP_DS_APPLICATION_ID,
00260                                NETSNMP_DS_AGENT_QUIT_IMMEDIATELY);
00261     netsnmp_ds_register_config(ASN_BOOLEAN, app, "leave_pidfile", 
00262                                NETSNMP_DS_APPLICATION_ID,
00263                                NETSNMP_DS_AGENT_LEAVE_PIDFILE);
00264     netsnmp_ds_register_config(ASN_BOOLEAN, app, "dontLogTCPWrappersConnects",
00265                                NETSNMP_DS_APPLICATION_ID,
00266                                NETSNMP_DS_AGENT_DONT_LOG_TCPWRAPPERS_CONNECTS);
00267     netsnmp_ds_register_config(ASN_INTEGER, app, "maxGetbulkRepeats",
00268                                NETSNMP_DS_APPLICATION_ID,
00269                                NETSNMP_DS_AGENT_MAX_GETBULKREPEATS);
00270     netsnmp_ds_register_config(ASN_INTEGER, app, "maxGetbulkResponses",
00271                                NETSNMP_DS_APPLICATION_ID,
00272                                NETSNMP_DS_AGENT_MAX_GETBULKRESPONSES);
00273     netsnmp_init_handler_conf();
00274 
00275 #include "agent_module_dot_conf.h"
00276 #include "mib_module_dot_conf.h"
00277 #ifdef TESTING
00278     print_config_handlers();
00279 #endif
00280 }
00281 
00282 void
00283 update_config(void)
00284 {
00285     snmp_call_callbacks(SNMP_CALLBACK_APPLICATION,
00286                         SNMPD_CALLBACK_PRE_UPDATE_CONFIG, NULL);
00287     free_config();
00288     read_configs();
00289 }
00290 
00291 
00292 void
00293 snmpd_register_config_handler(const char *token,
00294                               void (*parser) (const char *, char *),
00295                               void (*releaser) (void), const char *help)
00296 {
00297     DEBUGMSGTL(("snmpd_register_app_config_handler",
00298                 "registering .conf token for \"%s\"\n", token));
00299     register_app_config_handler(token, parser, releaser, help);
00300 }
00301 
00302 void
00303 snmpd_register_const_config_handler(const char *token,
00304                                     void (*parser) (const char *, const char *),
00305                                     void (*releaser) (void), const char *help)
00306 {
00307     DEBUGMSGTL(("snmpd_register_app_config_handler",
00308                 "registering .conf token for \"%s\"\n", token));
00309     register_app_config_handler(token, (void(*)(const char *, char *))parser,
00310                                 releaser, help);
00311 }
00312 
00313 #ifdef NETSNMP_FEATURE_REQUIRE_SNMPD_UNREGISTER_CONFIG_HANDLER
00314 netsnmp_feature_require(unregister_app_config_handler)
00315 #endif /* NETSNMP_FEATURE_REQUIRE_SNMPD_UNREGISTER_CONFIG_HANDLER */
00316 
00317 #ifndef NETSNMP_FEATURE_REMOVE_SNMPD_UNREGISTER_CONFIG_HANDLER
00318 void
00319 snmpd_unregister_config_handler(const char *token)
00320 {
00321     unregister_app_config_handler(token);
00322 }
00323 #endif /* NETSNMP_FEATURE_REMOVE_SNMPD_UNREGISTER_CONFIG_HANDLER */
00324 
00325 /*
00326  * this function is intended for use by mib-modules to store permenant
00327  * configuration information generated by sets or persistent counters 
00328  */
00329 void
00330 snmpd_store_config(const char *line)
00331 {
00332     read_app_config_store(line);
00333 }