Blame agent/agent_read_config.c

Packit fcad23
/*
Packit fcad23
 * agent_read_config.c
Packit fcad23
 *
Packit fcad23
 * Portions of this file are copyrighted by:
Packit fcad23
 * Copyright (c) 2016 VMware, Inc. All rights reserved.
Packit fcad23
 * Use is subject to license terms specified in the COPYING file
Packit fcad23
 * distributed with the Net-SNMP package.
Packit fcad23
 */
Packit fcad23
Packit fcad23
#include <net-snmp/net-snmp-config.h>
Packit fcad23
#include <net-snmp/net-snmp-features.h>
Packit fcad23
Packit fcad23
#if HAVE_SYS_PARAM_H
Packit fcad23
#include <sys/param.h>
Packit fcad23
#else
Packit fcad23
#include <sys/types.h>
Packit fcad23
#endif
Packit fcad23
#if HAVE_STDLIB_H
Packit fcad23
#include <stdlib.h>
Packit fcad23
#endif
Packit fcad23
#if HAVE_STRING_H
Packit fcad23
#include <string.h>
Packit fcad23
#else
Packit fcad23
#include <strings.h>
Packit fcad23
#endif
Packit fcad23
#include <stdio.h>
Packit fcad23
#include <ctype.h>
Packit fcad23
#include <errno.h>
Packit fcad23
Packit fcad23
#if TIME_WITH_SYS_TIME
Packit fcad23
# include <sys/time.h>
Packit fcad23
# include <time.h>
Packit fcad23
#else
Packit fcad23
# if HAVE_SYS_TIME_H
Packit fcad23
#  include <sys/time.h>
Packit fcad23
# else
Packit fcad23
#  include <time.h>
Packit fcad23
# endif
Packit fcad23
#endif
Packit fcad23
#if HAVE_NETINET_IN_H
Packit fcad23
#include <netinet/in.h>
Packit fcad23
#endif
Packit fcad23
#if HAVE_NETINET_IN_SYSTM_H
Packit fcad23
#include <netinet/in_systm.h>
Packit fcad23
#endif
Packit fcad23
#if HAVE_NETINET_IP_H
Packit fcad23
#include <netinet/ip.h>
Packit fcad23
#endif
Packit fcad23
#ifdef NETSNMP_ENABLE_IPV6
Packit fcad23
#if HAVE_NETINET_IP6_H
Packit fcad23
#include <netinet/ip6.h>
Packit fcad23
#endif
Packit fcad23
#endif
Packit fcad23
#if HAVE_SYS_QUEUE_H
Packit fcad23
#include <sys/queue.h>
Packit fcad23
#endif
Packit fcad23
#if HAVE_SYS_SOCKET_H
Packit fcad23
#include <sys/socket.h>
Packit fcad23
#if HAVE_SYS_SOCKETVAR_H
Packit fcad23
#ifndef dynix
Packit fcad23
#include <sys/socketvar.h>
Packit fcad23
#else
Packit fcad23
#include <sys/param.h>
Packit fcad23
#endif
Packit fcad23
#endif
Packit fcad23
#endif
Packit fcad23
#if HAVE_SYS_STREAM_H
Packit fcad23
#   ifdef sysv5UnixWare7
Packit fcad23
#      define _KMEMUSER 1   /* <sys/stream.h> needs this for queue_t */
Packit fcad23
#   endif
Packit fcad23
#include <sys/stream.h>
Packit fcad23
#endif
Packit fcad23
#if HAVE_NET_ROUTE_H
Packit fcad23
#include <net/route.h>
Packit fcad23
#endif
Packit fcad23
#if HAVE_NETINET_IP_VAR_H
Packit fcad23
#include <netinet/ip_var.h>
Packit fcad23
#endif
Packit fcad23
#ifdef NETSNMP_ENABLE_IPV6
Packit fcad23
#if HAVE_NETNETSNMP_ENABLE_IPV6_IP6_VAR_H
Packit fcad23
#include <netinet6/ip6_var.h>
Packit fcad23
#endif
Packit fcad23
#endif
Packit fcad23
#if HAVE_NETINET_IN_PCB_H
Packit fcad23
#include <netinet/in_pcb.h>
Packit fcad23
#endif
Packit fcad23
#if HAVE_INET_MIB2_H
Packit fcad23
#include <inet/mib2.h>
Packit fcad23
#endif
Packit fcad23
Packit fcad23
#if HAVE_UNISTD_H
Packit fcad23
#include <unistd.h>
Packit fcad23
#endif
Packit fcad23
#ifdef HAVE_PWD_H
Packit fcad23
#include <pwd.h>
Packit fcad23
#endif
Packit fcad23
#ifdef HAVE_GRP_H
Packit fcad23
#include <grp.h>
Packit fcad23
#endif
Packit fcad23
Packit fcad23
#include <net-snmp/net-snmp-includes.h>
Packit fcad23
#include <net-snmp/agent/net-snmp-agent-includes.h>
Packit fcad23
Packit fcad23
#include "mibgroup/struct.h"
Packit fcad23
#include <net-snmp/agent/agent_trap.h>
Packit fcad23
#include "snmpd.h"
Packit fcad23
#include <net-snmp/agent/agent_callbacks.h>
Packit fcad23
#include <net-snmp/agent/table.h>
Packit fcad23
#include <net-snmp/agent/table_iterator.h>
Packit fcad23
#include <net-snmp/agent/table_data.h>
Packit fcad23
#include <net-snmp/agent/table_dataset.h>
Packit fcad23
#include "agent_module_includes.h"
Packit fcad23
#include "mib_module_includes.h"
Packit fcad23
Packit fcad23
netsnmp_feature_child_of(agent_read_config_all, libnetsnmpagent)
Packit fcad23
Packit fcad23
netsnmp_feature_child_of(snmpd_unregister_config_handler, agent_read_config_all)
Packit fcad23
Packit fcad23
#ifdef HAVE_UNISTD_H
Packit fcad23
void
Packit fcad23
snmpd_set_agent_user(const char *token, char *cptr)
Packit fcad23
{
Packit fcad23
    if (cptr[0] == '#') {
Packit fcad23
        char           *ecp;
Packit fcad23
        int             uid;
Packit fcad23
Packit fcad23
        uid = strtoul(cptr + 1, &ecp, 10);
Packit fcad23
        if (*ecp != 0) {
Packit fcad23
            config_perror("Bad number");
Packit fcad23
	} else {
Packit fcad23
	    netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, 
Packit fcad23
			       NETSNMP_DS_AGENT_USERID, uid);
Packit fcad23
	}
Packit fcad23
#if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
Packit fcad23
    } else {
Packit fcad23
        struct passwd  *info;
Packit fcad23
Packit fcad23
        info = getpwnam(cptr);
Packit fcad23
        if (info)
Packit fcad23
            netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, 
Packit fcad23
                               NETSNMP_DS_AGENT_USERID, info->pw_uid);
Packit fcad23
        else
Packit fcad23
            config_perror("User not found in passwd database");
Packit fcad23
        endpwent();
Packit fcad23
#endif
Packit fcad23
    }
Packit fcad23
}
Packit fcad23
Packit fcad23
void
Packit fcad23
snmpd_set_agent_group(const char *token, char *cptr)
Packit fcad23
{
Packit fcad23
    if (cptr[0] == '#') {
Packit fcad23
        char           *ecp;
Packit fcad23
        int             gid = strtoul(cptr + 1, &ecp, 10);
Packit fcad23
Packit fcad23
        if (*ecp != 0) {
Packit fcad23
            config_perror("Bad number");
Packit fcad23
	} else {
Packit fcad23
            netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, 
Packit fcad23
			       NETSNMP_DS_AGENT_GROUPID, gid);
Packit fcad23
	}
Packit fcad23
#if defined(HAVE_GETGRNAM) && defined(HAVE_GRP_H)
Packit fcad23
    } else {
Packit fcad23
        struct group   *info;
Packit fcad23
Packit fcad23
        info = getgrnam(cptr);
Packit fcad23
        if (info)
Packit fcad23
            netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, 
Packit fcad23
                               NETSNMP_DS_AGENT_GROUPID, info->gr_gid);
Packit fcad23
        else
Packit fcad23
            config_perror("Group not found in group database");
Packit fcad23
        endgrent();
Packit fcad23
#endif
Packit fcad23
    }
Packit fcad23
}
Packit fcad23
#endif
Packit fcad23
Packit fcad23
#ifndef NETSNMP_NO_LISTEN_SUPPORT
Packit fcad23
void
Packit fcad23
snmpd_set_agent_address(const char *token, char *cptr)
Packit fcad23
{
Packit fcad23
    char            buf[SPRINT_MAX_LEN];
Packit fcad23
    char           *ptr;
Packit fcad23
Packit fcad23
    /*
Packit fcad23
     * has something been specified before? 
Packit fcad23
     */
Packit fcad23
    ptr = netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID, 
Packit fcad23
				NETSNMP_DS_AGENT_PORTS);
Packit fcad23
Packit fcad23
    if (ptr) {
Packit fcad23
        /*
Packit fcad23
         * append to the older specification string 
Packit fcad23
         */
Packit fcad23
        snprintf(buf, sizeof(buf), "%s,%s", ptr, cptr);
Packit fcad23
	buf[sizeof(buf) - 1] = '\0';
Packit fcad23
    } else {
Packit fcad23
        strlcpy(buf, cptr, sizeof(buf));
Packit fcad23
    }
Packit fcad23
Packit fcad23
    DEBUGMSGTL(("snmpd_ports", "port spec: %s\n", buf));
Packit fcad23
    netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID, 
Packit fcad23
			  NETSNMP_DS_AGENT_PORTS, buf);
Packit fcad23
}
Packit fcad23
#endif /* NETSNMP_NO_LISTEN_SUPPORT */
Packit fcad23
Packit fcad23
void
Packit fcad23
init_agent_read_config(const char *app)
Packit fcad23
{
Packit fcad23
    if (app != NULL) {
Packit fcad23
        netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, 
Packit fcad23
			      NETSNMP_DS_LIB_APPTYPE, app);
Packit fcad23
    } else {
Packit fcad23
        app = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 
Packit fcad23
				    NETSNMP_DS_LIB_APPTYPE);
Packit fcad23
    }
Packit fcad23
Packit fcad23
    register_app_config_handler("authtrapenable",
Packit fcad23
                                snmpd_parse_config_authtrap, NULL,
Packit fcad23
                                "1 | 2\t\t(1 = enable, 2 = disable)");
Packit fcad23
    register_app_config_handler("pauthtrapenable",
Packit fcad23
                                snmpd_parse_config_authtrap, NULL, NULL);
Packit fcad23
Packit fcad23
Packit fcad23
    if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 
Packit fcad23
			       NETSNMP_DS_AGENT_ROLE) == MASTER_AGENT) {
Packit fcad23
#ifndef NETSNMP_DISABLE_SNMPV1
Packit fcad23
        register_app_config_handler("trapsink",
Packit fcad23
                                    snmpd_parse_config_trapsink,
Packit fcad23
                                    snmpd_free_trapsinks,
Packit fcad23
                                    "[-profile p] [-name n] [-tag t] host [community] [port]");
Packit fcad23
#endif
Packit fcad23
#ifndef NETSNMP_DISABLE_SNMPV2C
Packit fcad23
        register_app_config_handler("trap2sink",
Packit fcad23
                                    snmpd_parse_config_trap2sink, 
Packit fcad23
                                    snmpd_free_trapsinks,
Packit fcad23
                                    "[-profile p] [-name n] [-tag t] host [community] [port]");
Packit fcad23
        register_app_config_handler("informsink",
Packit fcad23
                                    snmpd_parse_config_informsink,
Packit fcad23
                                    snmpd_free_trapsinks,
Packit fcad23
                                    "[-profile p] [-name n] [-tag t] host [community] [port]");
Packit fcad23
#endif
Packit fcad23
        register_app_config_handler("trapsess",
Packit fcad23
                                    snmpd_parse_config_trapsess,
Packit fcad23
                                    snmpd_free_trapsinks,
Packit fcad23
                                    "[-profile p] [-name n] [-tag t] [snmpcmdargs] host");
Packit fcad23
    }
Packit fcad23
#if !defined(NETSNMP_DISABLE_SNMPV1) || !defined(NETSNMP_DISABLE_SNMPV2C)
Packit fcad23
    register_app_config_handler("trapcommunity",
Packit fcad23
                                snmpd_parse_config_trapcommunity,
Packit fcad23
                                snmpd_free_trapcommunity,
Packit fcad23
                                "community-string");
Packit fcad23
#endif /* support for community based SNMP */
Packit fcad23
    netsnmp_ds_register_config(ASN_OCTET_STR, app, "v1trapaddress", 
Packit fcad23
                               NETSNMP_DS_APPLICATION_ID, 
Packit fcad23
                               NETSNMP_DS_AGENT_TRAP_ADDR);
Packit fcad23
#ifdef HAVE_UNISTD_H
Packit fcad23
    register_app_config_handler("agentuser",
Packit fcad23
                                snmpd_set_agent_user, NULL, "userid");
Packit fcad23
    register_app_config_handler("agentgroup",
Packit fcad23
                                snmpd_set_agent_group, NULL, "groupid");
Packit fcad23
#endif
Packit fcad23
#ifndef NETSNMP_NO_LISTEN_SUPPORT
Packit fcad23
    register_app_config_handler("agentaddress",
Packit fcad23
                                snmpd_set_agent_address, NULL,
Packit fcad23
                                "SNMP bind address");
Packit fcad23
#endif /* NETSNMP_NO_LISTEN_SUPPORT */
Packit fcad23
    netsnmp_ds_register_config(ASN_BOOLEAN, app, "quit", 
Packit fcad23
			       NETSNMP_DS_APPLICATION_ID,
Packit fcad23
			       NETSNMP_DS_AGENT_QUIT_IMMEDIATELY);
Packit fcad23
    netsnmp_ds_register_config(ASN_BOOLEAN, app, "leave_pidfile", 
Packit fcad23
			       NETSNMP_DS_APPLICATION_ID,
Packit fcad23
			       NETSNMP_DS_AGENT_LEAVE_PIDFILE);
Packit fcad23
    netsnmp_ds_register_config(ASN_BOOLEAN, app, "dontLogTCPWrappersConnects",
Packit fcad23
                               NETSNMP_DS_APPLICATION_ID,
Packit fcad23
                               NETSNMP_DS_AGENT_DONT_LOG_TCPWRAPPERS_CONNECTS);
Packit fcad23
    netsnmp_ds_register_config(ASN_INTEGER, app, "maxGetbulkRepeats",
Packit fcad23
                               NETSNMP_DS_APPLICATION_ID,
Packit fcad23
                               NETSNMP_DS_AGENT_MAX_GETBULKREPEATS);
Packit fcad23
    netsnmp_ds_register_config(ASN_INTEGER, app, "maxGetbulkResponses",
Packit fcad23
                               NETSNMP_DS_APPLICATION_ID,
Packit fcad23
                               NETSNMP_DS_AGENT_MAX_GETBULKRESPONSES);
Packit fcad23
    netsnmp_ds_register_config(ASN_INTEGER, app, "avgBulkVarbindSize",
Packit fcad23
                               NETSNMP_DS_APPLICATION_ID,
Packit fcad23
                               NETSNMP_DS_AGENT_AVG_BULKVARBINDSIZE);
Packit fcad23
#ifndef NETSNMP_NO_PDU_STATS
Packit fcad23
    netsnmp_ds_register_config(ASN_INTEGER, app, "pduStatsMax",
Packit fcad23
                               NETSNMP_DS_APPLICATION_ID,
Packit fcad23
                               NETSNMP_DS_AGENT_PDU_STATS_MAX);
Packit fcad23
    netsnmp_ds_register_config(ASN_INTEGER, app, "pduStatsThreshold",
Packit fcad23
                               NETSNMP_DS_APPLICATION_ID,
Packit fcad23
                               NETSNMP_DS_AGENT_PDU_STATS_THRESHOLD);
Packit fcad23
#endif /* NETSNMP_NO_PDU_STATS */
Packit fcad23
Packit fcad23
    netsnmp_init_handler_conf();
Packit fcad23
Packit fcad23
#include "agent_module_dot_conf.h"
Packit fcad23
#include "mib_module_dot_conf.h"
Packit fcad23
#ifdef TESTING
Packit fcad23
    print_config_handlers();
Packit fcad23
#endif
Packit fcad23
}
Packit fcad23
Packit fcad23
void
Packit fcad23
update_config(void)
Packit fcad23
{
Packit fcad23
    snmp_call_callbacks(SNMP_CALLBACK_APPLICATION,
Packit fcad23
                        SNMPD_CALLBACK_PRE_UPDATE_CONFIG, NULL);
Packit fcad23
    free_config();
Packit fcad23
    read_configs();
Packit fcad23
}
Packit fcad23
Packit fcad23
Packit fcad23
void
Packit fcad23
snmpd_register_config_handler(const char *token,
Packit fcad23
                              void (*parser) (const char *, char *),
Packit fcad23
                              void (*releaser) (void), const char *help)
Packit fcad23
{
Packit fcad23
    DEBUGMSGTL(("snmpd_register_app_config_handler",
Packit fcad23
                "registering .conf token for \"%s\"\n", token));
Packit fcad23
    register_app_config_handler(token, parser, releaser, help);
Packit fcad23
}
Packit fcad23
Packit fcad23
void
Packit fcad23
snmpd_register_const_config_handler(const char *token,
Packit fcad23
                                    void (*parser) (const char *, const char *),
Packit fcad23
                                    void (*releaser) (void), const char *help)
Packit fcad23
{
Packit fcad23
    DEBUGMSGTL(("snmpd_register_app_config_handler",
Packit fcad23
                "registering .conf token for \"%s\"\n", token));
Packit fcad23
    register_app_config_handler(token, (void(*)(const char *, char *))parser,
Packit fcad23
                                releaser, help);
Packit fcad23
}
Packit fcad23
Packit fcad23
#ifdef NETSNMP_FEATURE_REQUIRE_SNMPD_UNREGISTER_CONFIG_HANDLER
Packit fcad23
netsnmp_feature_require(unregister_app_config_handler)
Packit fcad23
#endif /* NETSNMP_FEATURE_REQUIRE_SNMPD_UNREGISTER_CONFIG_HANDLER */
Packit fcad23
Packit fcad23
#ifndef NETSNMP_FEATURE_REMOVE_SNMPD_UNREGISTER_CONFIG_HANDLER
Packit fcad23
void
Packit fcad23
snmpd_unregister_config_handler(const char *token)
Packit fcad23
{
Packit fcad23
    unregister_app_config_handler(token);
Packit fcad23
}
Packit fcad23
#endif /* NETSNMP_FEATURE_REMOVE_SNMPD_UNREGISTER_CONFIG_HANDLER */
Packit fcad23
Packit fcad23
/*
Packit fcad23
 * this function is intended for use by mib-modules to store permenant
Packit fcad23
 * configuration information generated by sets or persistent counters 
Packit fcad23
 */
Packit fcad23
void
Packit fcad23
snmpd_store_config(const char *line)
Packit fcad23
{
Packit fcad23
    read_app_config_store(line);
Packit fcad23
}