agent_read_config.c
上传用户:wxp200602
上传日期:2007-10-30
资源大小:4028k
文件大小:8k
源码类别:

SNMP编程

开发平台:

Unix_Linux

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