Wes Hardaker | c048523 | 1994-10-13 20:55:30 +0000 | [diff] [blame] | 1 | /* |
Wes Hardaker | 36e94be | 2001-11-22 02:56:52 +0000 | [diff] [blame] | 2 | * snmpd.c |
Wes Hardaker | c048523 | 1994-10-13 20:55:30 +0000 | [diff] [blame] | 3 | */ |
Wes Hardaker | 7bc5c4e | 2003-01-14 13:56:18 +0000 | [diff] [blame] | 4 | /** @defgroup agent The Net-SNMP agent |
Wes Hardaker | 36e94be | 2001-11-22 02:56:52 +0000 | [diff] [blame] | 5 | * The snmp agent responds to SNMP queries from management stations |
| 6 | */ |
Wes Hardaker | 56f8a5f | 2003-08-10 18:38:10 +0000 | [diff] [blame] | 7 | /* Portions of this file are subject to the following copyrights. See |
| 8 | * the Net-SNMP's COPYING file for more details and other copyrights |
| 9 | * that may apply: |
| 10 | */ |
Wes Hardaker | 36e94be | 2001-11-22 02:56:52 +0000 | [diff] [blame] | 11 | /* |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 12 | * Copyright 1988, 1989 by Carnegie Mellon University |
| 13 | * |
| 14 | * All Rights Reserved |
| 15 | * |
| 16 | * Permission to use, copy, modify, and distribute this software and its |
| 17 | * documentation for any purpose and without fee is hereby granted, |
| 18 | * provided that the above copyright notice appear in all copies and that |
| 19 | * both that copyright notice and this permission notice appear in |
| 20 | * supporting documentation, and that the name of CMU not be |
| 21 | * used in advertising or publicity pertaining to distribution of the |
| 22 | * software without specific, written prior permission. |
| 23 | * |
| 24 | * CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING |
| 25 | * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL |
| 26 | * CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR |
| 27 | * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 28 | * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, |
| 29 | * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 30 | * SOFTWARE. |
| 31 | * ***************************************************************** |
| 32 | */ |
Wes Hardaker | a36188e | 2003-07-02 17:58:55 +0000 | [diff] [blame] | 33 | /* |
| 34 | * Copyright © 2003 Sun Microsystems, Inc. All rights reserved. |
| 35 | * Use is subject to license terms specified in the COPYING file |
| 36 | * distributed with the Net-SNMP package. |
| 37 | */ |
Wes Hardaker | eb6d4fc | 2002-01-04 21:00:48 +0000 | [diff] [blame] | 38 | #include <net-snmp/net-snmp-config.h> |
Wes Hardaker | 3b14c59 | 2011-02-23 00:01:18 +0000 | [diff] [blame] | 39 | #include <net-snmp/net-snmp-features.h> |
Bart Van Assche | f13b710 | 2010-01-23 15:07:35 +0000 | [diff] [blame] | 40 | #include <net-snmp/types.h> |
Wes Hardaker | f86050d | 1995-09-29 20:44:13 +0000 | [diff] [blame] | 41 | |
Michael Slifcak | dcad2c6 | 2004-01-27 16:54:25 +0000 | [diff] [blame] | 42 | #if HAVE_IO_H |
| 43 | #include <io.h> |
| 44 | #endif |
Wes Hardaker | 238b7ba | 1997-01-20 14:09:25 +0000 | [diff] [blame] | 45 | #include <stdio.h> |
| 46 | #include <errno.h> |
Wes Hardaker | a1a822e | 1998-03-26 15:22:30 +0000 | [diff] [blame] | 47 | #if HAVE_STRING_H |
Wes Hardaker | 238b7ba | 1997-01-20 14:09:25 +0000 | [diff] [blame] | 48 | #include <string.h> |
Wes Hardaker | a1a822e | 1998-03-26 15:22:30 +0000 | [diff] [blame] | 49 | #else |
| 50 | #include <strings.h> |
Wes Hardaker | 238b7ba | 1997-01-20 14:09:25 +0000 | [diff] [blame] | 51 | #endif |
| 52 | #if HAVE_STDLIB_H |
| 53 | #include <stdlib.h> |
| 54 | #endif |
| 55 | #if HAVE_UNISTD_H |
| 56 | #include <unistd.h> |
| 57 | #endif |
Wes Hardaker | c048523 | 1994-10-13 20:55:30 +0000 | [diff] [blame] | 58 | #include <sys/types.h> |
Wes Hardaker | f86050d | 1995-09-29 20:44:13 +0000 | [diff] [blame] | 59 | #if HAVE_NETINET_IN_H |
Wes Hardaker | c048523 | 1994-10-13 20:55:30 +0000 | [diff] [blame] | 60 | #include <netinet/in.h> |
Wes Hardaker | f86050d | 1995-09-29 20:44:13 +0000 | [diff] [blame] | 61 | #endif |
Wes Hardaker | 238b7ba | 1997-01-20 14:09:25 +0000 | [diff] [blame] | 62 | #if HAVE_ARPA_INET_H |
| 63 | #include <arpa/inet.h> |
| 64 | #endif |
Wes Hardaker | f86050d | 1995-09-29 20:44:13 +0000 | [diff] [blame] | 65 | #if TIME_WITH_SYS_TIME |
Bart Van Assche | 337360e | 2010-01-24 11:41:03 +0000 | [diff] [blame] | 66 | # include <sys/time.h> |
Wes Hardaker | f86050d | 1995-09-29 20:44:13 +0000 | [diff] [blame] | 67 | # include <time.h> |
| 68 | #else |
| 69 | # if HAVE_SYS_TIME_H |
| 70 | # include <sys/time.h> |
| 71 | # else |
| 72 | # include <time.h> |
| 73 | # endif |
| 74 | #endif |
Wes Hardaker | 7a2cb58 | 1997-08-14 14:38:17 +0000 | [diff] [blame] | 75 | #if HAVE_SYS_SELECT_H |
| 76 | #include <sys/select.h> |
| 77 | #endif |
Michael Slifcak | fd869ca | 1999-07-30 11:00:28 +0000 | [diff] [blame] | 78 | #if HAVE_SYS_SOCKET_H |
Wes Hardaker | c048523 | 1994-10-13 20:55:30 +0000 | [diff] [blame] | 79 | #include <sys/socket.h> |
Michael Slifcak | fd869ca | 1999-07-30 11:00:28 +0000 | [diff] [blame] | 80 | #endif |
| 81 | #if HAVE_NET_IF_H |
Wes Hardaker | c048523 | 1994-10-13 20:55:30 +0000 | [diff] [blame] | 82 | #include <net/if.h> |
Michael Slifcak | fd869ca | 1999-07-30 11:00:28 +0000 | [diff] [blame] | 83 | #endif |
Wes Hardaker | 92ba163 | 1999-07-23 17:44:18 +0000 | [diff] [blame] | 84 | #if HAVE_INET_MIB2_H |
| 85 | #include <inet/mib2.h> |
| 86 | #endif |
Wes Hardaker | f86050d | 1995-09-29 20:44:13 +0000 | [diff] [blame] | 87 | #if HAVE_SYS_IOCTL_H |
Wes Hardaker | c048523 | 1994-10-13 20:55:30 +0000 | [diff] [blame] | 88 | #include <sys/ioctl.h> |
Wes Hardaker | f86050d | 1995-09-29 20:44:13 +0000 | [diff] [blame] | 89 | #endif |
| 90 | #if HAVE_SYS_FILE_H |
Wes Hardaker | c048523 | 1994-10-13 20:55:30 +0000 | [diff] [blame] | 91 | #include <sys/file.h> |
Wes Hardaker | f86050d | 1995-09-29 20:44:13 +0000 | [diff] [blame] | 92 | #endif |
Wes Hardaker | 1dfa53c | 1996-05-01 19:37:35 +0000 | [diff] [blame] | 93 | #ifdef HAVE_FCNTL_H |
| 94 | #include <fcntl.h> |
| 95 | #endif |
Michael Slifcak | 1b78b76 | 1999-11-19 01:58:47 +0000 | [diff] [blame] | 96 | #if HAVE_SYS_WAIT_H |
Wes Hardaker | 238b7ba | 1997-01-20 14:09:25 +0000 | [diff] [blame] | 97 | #include <sys/wait.h> |
Michael Slifcak | 1b78b76 | 1999-11-19 01:58:47 +0000 | [diff] [blame] | 98 | #endif |
Wes Hardaker | a5b5955 | 1998-01-16 17:44:56 +0000 | [diff] [blame] | 99 | #include <signal.h> |
Wes Hardaker | 238b7ba | 1997-01-20 14:09:25 +0000 | [diff] [blame] | 100 | #ifdef HAVE_SYS_PARAM_H |
| 101 | #include <sys/param.h> |
| 102 | #endif |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 103 | #if HAVE_PROCESS_H /* Win32-getpid */ |
Wes Hardaker | d7d053b | 2001-03-15 15:46:12 +0000 | [diff] [blame] | 104 | #include <process.h> |
| 105 | #endif |
John Naylon | 04fda2e | 2001-08-06 11:29:49 +0000 | [diff] [blame] | 106 | #if HAVE_LIMITS_H |
| 107 | #include <limits.h> |
| 108 | #endif |
Wes Hardaker | 9743e9f | 2001-10-11 21:01:50 +0000 | [diff] [blame] | 109 | #if HAVE_PWD_H |
| 110 | #include <pwd.h> |
| 111 | #endif |
Wes Hardaker | 15ddba0 | 2001-12-05 16:28:59 +0000 | [diff] [blame] | 112 | #if HAVE_GRP_H |
| 113 | #include <grp.h> |
| 114 | #endif |
Bart Van Assche | 13b46d0 | 2010-10-28 08:44:08 +0000 | [diff] [blame] | 115 | #ifdef HAVE_CRTDBG_H |
| 116 | #include <crtdbg.h> |
| 117 | #endif |
John Naylon | 04fda2e | 2001-08-06 11:29:49 +0000 | [diff] [blame] | 118 | |
| 119 | #ifndef PATH_MAX |
| 120 | # ifdef _POSIX_PATH_MAX |
| 121 | # define PATH_MAX _POSIX_PATH_MAX |
| 122 | # else |
| 123 | # define PATH_MAX 255 |
| 124 | # endif |
| 125 | #endif |
Wes Hardaker | c8c147b | 1999-12-08 23:29:52 +0000 | [diff] [blame] | 126 | |
| 127 | #ifndef FD_SET |
Wes Hardaker | 238b7ba | 1997-01-20 14:09:25 +0000 | [diff] [blame] | 128 | typedef long fd_mask; |
| 129 | #define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */ |
| 130 | #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS))) |
| 131 | #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS))) |
| 132 | #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS))) |
| 133 | #define FD_ZERO(p) memset((p), 0, sizeof(*(p))) |
Wes Hardaker | 268218c | 1995-10-13 22:45:28 +0000 | [diff] [blame] | 134 | #endif |
Wes Hardaker | c048523 | 1994-10-13 20:55:30 +0000 | [diff] [blame] | 135 | |
Dave Shield | 1f17c99 | 2002-02-08 15:45:13 +0000 | [diff] [blame] | 136 | #include <net-snmp/net-snmp-includes.h> |
| 137 | #include <net-snmp/agent/net-snmp-agent-includes.h> |
| 138 | |
Dave Shield | 2decc2f | 2005-04-29 16:12:33 +0000 | [diff] [blame] | 139 | #include <net-snmp/library/fd_event_manager.h> |
Dave Shield | 23d9c65 | 2009-04-21 15:33:42 +0000 | [diff] [blame] | 140 | #include <net-snmp/library/large_fd_set.h> |
Dave Shield | 2decc2f | 2005-04-29 16:12:33 +0000 | [diff] [blame] | 141 | |
Wes Hardaker | c048523 | 1994-10-13 20:55:30 +0000 | [diff] [blame] | 142 | #include "m2m.h" |
Magnus Fromreide | 3f2e788 | 2011-02-02 07:09:27 +0000 | [diff] [blame] | 143 | #include <net-snmp/agent/agent_module_config.h> |
Wes Hardaker | 9103307 | 2002-02-07 21:33:23 +0000 | [diff] [blame] | 144 | #include <net-snmp/agent/mib_module_config.h> |
Wes Hardaker | 8c6ab57 | 1998-06-10 15:53:53 +0000 | [diff] [blame] | 145 | |
Wes Hardaker | 522909f | 1997-03-05 14:06:41 +0000 | [diff] [blame] | 146 | #include "snmpd.h" |
Wes Hardaker | c048523 | 1994-10-13 20:55:30 +0000 | [diff] [blame] | 147 | |
Magnus Fromreide | 1f40adb | 2011-02-01 07:19:58 +0000 | [diff] [blame] | 148 | #include <net-snmp/agent/mib_modules.h> |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 149 | |
Wes Hardaker | 40342b7 | 2002-01-14 16:05:52 +0000 | [diff] [blame] | 150 | #include <net-snmp/agent/agent_trap.h> |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 151 | |
Wes Hardaker | 7a649e8 | 2002-01-11 22:12:21 +0000 | [diff] [blame] | 152 | #include <net-snmp/agent/table.h> |
| 153 | #include <net-snmp/agent/table_iterator.h> |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 154 | |
| 155 | /* |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 156 | * Include winservice.h to support Windows Service |
| 157 | */ |
| 158 | #ifdef WIN32 |
| 159 | #include <windows.h> |
| 160 | #include <tchar.h> |
| 161 | #include <net-snmp/library/winservice.h> |
Michael Slifcak | 78d401c | 2004-02-27 12:50:16 +0000 | [diff] [blame] | 162 | |
Michael Slifcak | 0ef0164 | 2004-02-09 12:06:24 +0000 | [diff] [blame] | 163 | #define WIN32SERVICE |
Michael Slifcak | 78d401c | 2004-02-27 12:50:16 +0000 | [diff] [blame] | 164 | |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 165 | #endif |
| 166 | |
Wes Hardaker | 3b14c59 | 2011-02-23 00:01:18 +0000 | [diff] [blame] | 167 | netsnmp_feature_want(logging_file) |
| 168 | netsnmp_feature_want(logging_stdio) |
| 169 | netsnmp_feature_want(logging_syslog) |
| 170 | |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 171 | /* |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 172 | * Globals. |
| 173 | */ |
Thomas Anders | e7563bf | 2006-09-15 00:48:50 +0000 | [diff] [blame] | 174 | #ifdef NETSNMP_USE_LIBWRAP |
Wes Hardaker | 5f1ce64 | 1998-03-28 00:10:05 +0000 | [diff] [blame] | 175 | #include <tcpd.h> |
Thomas Anders | e7563bf | 2006-09-15 00:48:50 +0000 | [diff] [blame] | 176 | #endif /* NETSNMP_USE_LIBWRAP */ |
Wes Hardaker | 5f1ce64 | 1998-03-28 00:10:05 +0000 | [diff] [blame] | 177 | |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 178 | #define TIMETICK 500000L |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 179 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 180 | int snmp_dump_packet; |
Magnus Fromreide | 5a4072f | 2009-04-04 10:07:39 +0000 | [diff] [blame] | 181 | static int reconfig = 0; |
Dave Shield | f542e4a | 2002-12-12 16:37:26 +0000 | [diff] [blame] | 182 | int Facility = LOG_DAEMON; |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 183 | |
Michael Slifcak | 0ef0164 | 2004-02-09 12:06:24 +0000 | [diff] [blame] | 184 | #ifdef WIN32SERVICE |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 185 | /* |
| 186 | * SNMP Agent Status |
| 187 | */ |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 188 | #define AGENT_RUNNING 1 |
| 189 | #define AGENT_STOPPED 0 |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 190 | int agent_status = AGENT_STOPPED; |
Alex Burger | 327de41 | 2005-11-10 02:33:17 +0000 | [diff] [blame] | 191 | /* app_name_long used for Event Log (syslog), SCM, registry etc */ |
Bart Van Assche | b6d2135 | 2010-03-20 19:36:28 +0000 | [diff] [blame] | 192 | LPCTSTR app_name_long = _T("Net-SNMP Agent"); /* Application Name */ |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 193 | #endif |
Wes Hardaker | c048523 | 1994-10-13 20:55:30 +0000 | [diff] [blame] | 194 | |
Alex Burger | 327de41 | 2005-11-10 02:33:17 +0000 | [diff] [blame] | 195 | const char *app_name = "snmpd"; |
| 196 | |
Robert Story | 7627ec7 | 2005-11-15 16:37:58 +0000 | [diff] [blame] | 197 | extern int netsnmp_running; |
Magnus Fromreide | adff160 | 2010-10-16 22:32:38 +0000 | [diff] [blame] | 198 | #ifdef USING_UTIL_FUNCS_RESTART_MODULE |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 199 | extern char **argvrestartp; |
| 200 | extern char *argvrestart; |
| 201 | extern char *argvrestartname; |
Magnus Fromreide | adff160 | 2010-10-16 22:32:38 +0000 | [diff] [blame] | 202 | #endif /* USING_UTIL_FUNCS_RESTART_MODULE */ |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 203 | |
Dave Shield | be29000 | 1999-12-13 12:43:14 +0000 | [diff] [blame] | 204 | #ifdef USING_SMUX_MODULE |
Niels Baggesen | 08b5839 | 2005-03-15 06:14:06 +0000 | [diff] [blame] | 205 | #include <mibgroup/smux/smux.h> |
Dave Shield | c3fb5c8 | 2007-03-22 19:12:22 +0000 | [diff] [blame] | 206 | #endif /* USING_SMUX_MODULE */ |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 207 | |
| 208 | /* |
| 209 | * Prototypes. |
| 210 | */ |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 211 | int snmp_read_packet(int); |
| 212 | int snmp_input(int, netsnmp_session *, int, netsnmp_pdu *, |
| 213 | void *); |
| 214 | static void usage(char *); |
| 215 | static void SnmpTrapNodeDown(void); |
| 216 | static int receive(void); |
Michael Slifcak | 0ef0164 | 2004-02-09 12:06:24 +0000 | [diff] [blame] | 217 | #ifdef WIN32SERVICE |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 218 | void StopSnmpAgent(void); |
| 219 | int SnmpDaemonMain(int argc, TCHAR * argv[]); |
| 220 | int __cdecl _tmain(int argc, TCHAR * argv[]); |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 221 | #else |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 222 | int main(int, char **); |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 223 | #endif |
Wes Hardaker | 40b2bf1 | 1999-01-18 18:47:36 +0000 | [diff] [blame] | 224 | |
Dave Shield | f542e4a | 2002-12-12 16:37:26 +0000 | [diff] [blame] | 225 | /* |
| 226 | * These definitions handle 4.2 systems without additional syslog facilities. |
| 227 | */ |
| 228 | #ifndef LOG_CONS |
| 229 | #define LOG_CONS 0 /* Don't bother if not defined... */ |
| 230 | #endif |
| 231 | #ifndef LOG_PID |
| 232 | #define LOG_PID 0 /* Don't bother if not defined... */ |
| 233 | #endif |
| 234 | #ifndef LOG_LOCAL0 |
| 235 | #define LOG_LOCAL0 0 |
| 236 | #endif |
| 237 | #ifndef LOG_LOCAL1 |
| 238 | #define LOG_LOCAL1 0 |
| 239 | #endif |
| 240 | #ifndef LOG_LOCAL2 |
| 241 | #define LOG_LOCAL2 0 |
| 242 | #endif |
| 243 | #ifndef LOG_LOCAL3 |
| 244 | #define LOG_LOCAL3 0 |
| 245 | #endif |
| 246 | #ifndef LOG_LOCAL4 |
| 247 | #define LOG_LOCAL4 0 |
| 248 | #endif |
| 249 | #ifndef LOG_LOCAL5 |
| 250 | #define LOG_LOCAL5 0 |
| 251 | #endif |
| 252 | #ifndef LOG_LOCAL6 |
| 253 | #define LOG_LOCAL6 0 |
| 254 | #endif |
| 255 | #ifndef LOG_LOCAL7 |
| 256 | #define LOG_LOCAL7 0 |
| 257 | #endif |
| 258 | #ifndef LOG_DAEMON |
| 259 | #define LOG_DAEMON 0 |
| 260 | #endif |
| 261 | |
| 262 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 263 | static void |
| 264 | usage(char *prog) |
Wes Hardaker | acee521 | 1995-10-06 23:32:04 +0000 | [diff] [blame] | 265 | { |
Michael Slifcak | 0ef0164 | 2004-02-09 12:06:24 +0000 | [diff] [blame] | 266 | #ifdef WIN32SERVICE |
Magnus Fromreide | 05ea7cd | 2009-11-12 08:26:06 +0000 | [diff] [blame] | 267 | printf("\nUsage: %s [-register] [-quiet] [OPTIONS] [LISTENING ADDRESSES]" |
| 268 | "\n %s [-unregister] [-quiet]", prog, prog); |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 269 | #else |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 270 | printf("\nUsage: %s [OPTIONS] [LISTENING ADDRESSES]", prog); |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 271 | #endif |
Magnus Fromreide | 05ea7cd | 2009-11-12 08:26:06 +0000 | [diff] [blame] | 272 | printf("\n" |
Dave Shield | cefe930 | 2010-06-14 15:51:14 +0000 | [diff] [blame] | 273 | "\n\tVersion: %s\n%s" |
| 274 | "\t\t\t (config search path: %s)\n%s%s", |
| 275 | netsnmp_get_version(), |
Magnus Fromreide | 05ea7cd | 2009-11-12 08:26:06 +0000 | [diff] [blame] | 276 | "\tWeb: http://www.net-snmp.org/\n" |
| 277 | "\tEmail: net-snmp-coders@lists.sourceforge.net\n" |
| 278 | "\n -a\t\t\tlog addresses\n" |
| 279 | " -A\t\t\tappend to the logfile rather than truncating it\n" |
| 280 | " -c FILE[,...]\t\tread FILE(s) as configuration file(s)\n" |
Dave Shield | cefe930 | 2010-06-14 15:51:14 +0000 | [diff] [blame] | 281 | " -C\t\t\tdo not read the default configuration files\n", |
| 282 | get_configuration_directory(), |
Magnus Fromreide | 05ea7cd | 2009-11-12 08:26:06 +0000 | [diff] [blame] | 283 | " -d\t\t\tdump sent and received SNMP packets\n" |
Niels Baggesen | 131c93b | 2014-05-06 06:20:28 +0200 | [diff] [blame] | 284 | #ifndef NETSNMP_DISABLE_DEBUGGING |
Jan Safranek | e6fb38b | 2010-07-20 07:32:59 +0000 | [diff] [blame] | 285 | " -D[TOKEN[,...]]\tturn on debugging output for the given TOKEN(s)\n" |
Wes Hardaker | f5b1d0f | 2008-02-15 04:39:29 +0000 | [diff] [blame] | 286 | "\t\t\t (try ALL for extremely verbose output)\n" |
Magnus Fromreide | 05ea7cd | 2009-11-12 08:26:06 +0000 | [diff] [blame] | 287 | "\t\t\t Don't put space(s) between -D and TOKEN(s).\n" |
Niels Baggesen | 131c93b | 2014-05-06 06:20:28 +0200 | [diff] [blame] | 288 | #endif |
Dave Shield | cefe930 | 2010-06-14 15:51:14 +0000 | [diff] [blame] | 289 | " -f\t\t\tdo not fork from the shell\n", |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 290 | #if HAVE_UNISTD_H |
Magnus Fromreide | 05ea7cd | 2009-11-12 08:26:06 +0000 | [diff] [blame] | 291 | " -g GID\t\tchange to this numeric gid after opening\n" |
| 292 | "\t\t\t transport endpoints\n" |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 293 | #endif |
Magnus Fromreide | 05ea7cd | 2009-11-12 08:26:06 +0000 | [diff] [blame] | 294 | " -h, --help\t\tdisplay this usage message\n" |
| 295 | " -H\t\t\tdisplay configuration file directives understood\n" |
| 296 | " -I [-]INITLIST\tlist of mib modules to initialize (or not)\n" |
| 297 | "\t\t\t (run snmpd with -Dmib_init for a list)\n" |
Dave Shield | cefe930 | 2010-06-14 15:51:14 +0000 | [diff] [blame] | 298 | " -L <LOGOPTS>\t\ttoggle options controlling where to log to\n"); |
Dave Shield | e755185 | 2003-10-24 12:49:22 +0000 | [diff] [blame] | 299 | snmp_log_options_usage("\t", stdout); |
Magnus Fromreide | 05ea7cd | 2009-11-12 08:26:06 +0000 | [diff] [blame] | 300 | printf(" -m MIBLIST\t\tuse MIBLIST instead of the default MIB list\n" |
Dave Shield | c2de2f2 | 2010-03-19 22:56:34 +0000 | [diff] [blame] | 301 | " -M DIRLIST\t\tuse DIRLIST as the list of locations to look for MIBs\n" |
Dave Shield | cefe930 | 2010-06-14 15:51:14 +0000 | [diff] [blame] | 302 | "\t\t\t (default %s)\n%s%s", |
| 303 | #ifndef NETSNMP_DISABLE_MIB_LOADING |
| 304 | netsnmp_get_mib_directory(), |
| 305 | #else |
| 306 | "MIBs not loaded", |
| 307 | #endif |
Magnus Fromreide | 05ea7cd | 2009-11-12 08:26:06 +0000 | [diff] [blame] | 308 | " -p FILE\t\tstore process id in FILE\n" |
| 309 | " -q\t\t\tprint information in a more parsable format\n" |
| 310 | " -r\t\t\tdo not exit if files only accessible to root\n" |
| 311 | "\t\t\t cannot be opened\n" |
Michael Slifcak | 0ef0164 | 2004-02-09 12:06:24 +0000 | [diff] [blame] | 312 | #ifdef WIN32SERVICE |
Magnus Fromreide | 05ea7cd | 2009-11-12 08:26:06 +0000 | [diff] [blame] | 313 | " -register\t\tregister as a Windows service\n" |
| 314 | " \t\t\t (followed by -quiet to prevent message popups)\n" |
| 315 | " \t\t\t (followed by the startup parameter list)\n" |
| 316 | " \t\t\t Note that some parameters are not relevant when running as a service\n" |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 317 | #endif |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 318 | #if HAVE_UNISTD_H |
Magnus Fromreide | 05ea7cd | 2009-11-12 08:26:06 +0000 | [diff] [blame] | 319 | " -u UID\t\tchange to this uid (numeric or textual) after\n" |
| 320 | "\t\t\t opening transport endpoints\n" |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 321 | #endif |
Michael Slifcak | 0ef0164 | 2004-02-09 12:06:24 +0000 | [diff] [blame] | 322 | #ifdef WIN32SERVICE |
Magnus Fromreide | 05ea7cd | 2009-11-12 08:26:06 +0000 | [diff] [blame] | 323 | " -unregister\t\tunregister as a Windows service\n" |
| 324 | " \t\t\t (followed -quiet to prevent message popups)\n" |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 325 | #endif |
Magnus Fromreide | 05ea7cd | 2009-11-12 08:26:06 +0000 | [diff] [blame] | 326 | " -v, --version\t\tdisplay version information\n" |
| 327 | " -V\t\t\tverbose display\n" |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 328 | #if defined(USING_AGENTX_SUBAGENT_MODULE)|| defined(USING_AGENTX_MASTER_MODULE) |
Magnus Fromreide | 05ea7cd | 2009-11-12 08:26:06 +0000 | [diff] [blame] | 329 | " -x ADDRESS\t\tuse ADDRESS as AgentX address\n" |
Wes Hardaker | abf3f57 | 2000-10-26 00:44:22 +0000 | [diff] [blame] | 330 | #endif |
| 331 | #ifdef USING_AGENTX_SUBAGENT_MODULE |
Magnus Fromreide | 05ea7cd | 2009-11-12 08:26:06 +0000 | [diff] [blame] | 332 | " -X\t\t\trun as an AgentX subagent rather than as an\n" |
| 333 | "\t\t\t SNMP master agent\n" |
Wes Hardaker | acee521 | 1995-10-06 23:32:04 +0000 | [diff] [blame] | 334 | #endif |
Dave Shield | cefe930 | 2010-06-14 15:51:14 +0000 | [diff] [blame] | 335 | , |
Magnus Fromreide | 05ea7cd | 2009-11-12 08:26:06 +0000 | [diff] [blame] | 336 | "\nDeprecated options:\n" |
| 337 | " -l FILE\t\tuse -Lf <FILE> instead\n" |
| 338 | " -P\t\t\tuse -p instead\n" |
| 339 | " -s\t\t\tuse -Lsd instead\n" |
| 340 | " -S d|i|0-7\t\tuse -Ls <facility> instead\n" |
Dave Shield | cefe930 | 2010-06-14 15:51:14 +0000 | [diff] [blame] | 341 | "\n" |
Dave Shield | 0b34cf4 | 2010-03-22 14:36:59 +0000 | [diff] [blame] | 342 | ); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 343 | exit(1); |
Wes Hardaker | acee521 | 1995-10-06 23:32:04 +0000 | [diff] [blame] | 344 | } |
| 345 | |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 346 | static void |
| 347 | version(void) |
| 348 | { |
Magnus Fromreide | 05ea7cd | 2009-11-12 08:26:06 +0000 | [diff] [blame] | 349 | printf("\nNET-SNMP version: %s\n" |
| 350 | "Web: http://www.net-snmp.org/\n" |
| 351 | "Email: net-snmp-coders@lists.sourceforge.net\n\n", |
| 352 | netsnmp_get_version()); |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 353 | exit(0); |
| 354 | } |
| 355 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 356 | RETSIGTYPE |
Wes Hardaker | 40b2bf1 | 1999-01-18 18:47:36 +0000 | [diff] [blame] | 357 | SnmpdShutDown(int a) |
Wes Hardaker | caf8427 | 1998-09-03 01:50:34 +0000 | [diff] [blame] | 358 | { |
Michael Slifcak | 0ef0164 | 2004-02-09 12:06:24 +0000 | [diff] [blame] | 359 | #ifdef WIN32SERVICE |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 360 | extern netsnmp_session *main_session; |
Robert Story | dde1ade | 2002-04-27 02:14:57 +0000 | [diff] [blame] | 361 | #endif |
Robert Story | 7627ec7 | 2005-11-15 16:37:58 +0000 | [diff] [blame] | 362 | netsnmp_running = 0; |
Michael Slifcak | 0ef0164 | 2004-02-09 12:06:24 +0000 | [diff] [blame] | 363 | #ifdef WIN32SERVICE |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 364 | /* |
| 365 | * In case of windows, select() in receive() function will not return |
| 366 | * on signal. Thats why following function is called, which closes the |
| 367 | * socket descriptors and causes the select() to return |
| 368 | */ |
| 369 | snmp_close(main_session); |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 370 | #endif |
Wes Hardaker | caf8427 | 1998-09-03 01:50:34 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Niels Baggesen | c2be268 | 1999-08-18 09:51:57 +0000 | [diff] [blame] | 373 | #ifdef SIGHUP |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 374 | RETSIGTYPE |
Niels Baggesen | c2be268 | 1999-08-18 09:51:57 +0000 | [diff] [blame] | 375 | SnmpdReconfig(int a) |
| 376 | { |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 377 | reconfig = 1; |
| 378 | signal(SIGHUP, SnmpdReconfig); |
Niels Baggesen | c2be268 | 1999-08-18 09:51:57 +0000 | [diff] [blame] | 379 | } |
| 380 | #endif |
| 381 | |
Dave Shield | 6314222 | 1999-09-21 16:32:27 +0000 | [diff] [blame] | 382 | #ifdef SIGUSR1 |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 383 | extern void dump_registry(void); |
| 384 | RETSIGTYPE |
Dave Shield | 6314222 | 1999-09-21 16:32:27 +0000 | [diff] [blame] | 385 | SnmpdDump(int a) |
| 386 | { |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 387 | dump_registry(); |
| 388 | signal(SIGUSR1, SnmpdDump); |
Dave Shield | 6314222 | 1999-09-21 16:32:27 +0000 | [diff] [blame] | 389 | } |
| 390 | #endif |
| 391 | |
Michael Slifcak | 6790e99 | 2004-03-02 12:18:16 +0000 | [diff] [blame] | 392 | RETSIGTYPE |
| 393 | SnmpdCatchRandomSignal(int a) |
| 394 | { |
| 395 | /* Disable all logs and log the error via syslog */ |
| 396 | snmp_disable_log(); |
Wes Hardaker | 3b14c59 | 2011-02-23 00:01:18 +0000 | [diff] [blame] | 397 | #ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG |
Michael Slifcak | 6790e99 | 2004-03-02 12:18:16 +0000 | [diff] [blame] | 398 | snmp_enable_syslog(); |
Wes Hardaker | 3b14c59 | 2011-02-23 00:01:18 +0000 | [diff] [blame] | 399 | #endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */ |
Michael Slifcak | 6790e99 | 2004-03-02 12:18:16 +0000 | [diff] [blame] | 400 | snmp_log(LOG_ERR, "Exiting on signal %d\n", a); |
Wes Hardaker | 3b14c59 | 2011-02-23 00:01:18 +0000 | [diff] [blame] | 401 | #ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG |
Michael Slifcak | 6790e99 | 2004-03-02 12:18:16 +0000 | [diff] [blame] | 402 | snmp_disable_syslog(); |
Wes Hardaker | 3b14c59 | 2011-02-23 00:01:18 +0000 | [diff] [blame] | 403 | #endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */ |
Michael Slifcak | 6790e99 | 2004-03-02 12:18:16 +0000 | [diff] [blame] | 404 | exit(1); |
| 405 | } |
Dave Shield | 6314222 | 1999-09-21 16:32:27 +0000 | [diff] [blame] | 406 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 407 | static void |
Wes Hardaker | c2eafd1 | 1999-06-10 01:31:42 +0000 | [diff] [blame] | 408 | SnmpTrapNodeDown(void) |
Wes Hardaker | a5b5955 | 1998-01-16 17:44:56 +0000 | [diff] [blame] | 409 | { |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 410 | send_easy_trap(SNMP_TRAP_ENTERPRISESPECIFIC, 2); |
| 411 | /* |
| 412 | * XXX 2 - Node Down #define it as NODE_DOWN_TRAP |
| 413 | */ |
Dave Shield | dd0f237 | 1999-06-18 16:14:58 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 416 | /*******************************************************************-o-****** |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 417 | * main - Non Windows |
Michael Slifcak | 8784cbc | 2004-09-09 10:22:23 +0000 | [diff] [blame] | 418 | * SnmpDaemonMain - Windows to support windows service |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 419 | * |
| 420 | * Parameters: |
| 421 | * argc |
| 422 | * *argv[] |
| 423 | * |
| 424 | * Returns: |
| 425 | * 0 Always succeeds. (?) |
| 426 | * |
| 427 | * |
| 428 | * Setup and start the agent daemon. |
| 429 | * |
| 430 | * Also successfully EXITs with zero for some options. |
| 431 | */ |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 432 | int |
Michael Slifcak | 0ef0164 | 2004-02-09 12:06:24 +0000 | [diff] [blame] | 433 | #ifdef WIN32SERVICE |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 434 | SnmpDaemonMain(int argc, TCHAR * argv[]) |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 435 | #else |
Wes Hardaker | 40b2bf1 | 1999-01-18 18:47:36 +0000 | [diff] [blame] | 436 | main(int argc, char *argv[]) |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 437 | #endif |
Wes Hardaker | c048523 | 1994-10-13 20:55:30 +0000 | [diff] [blame] | 438 | { |
Wes Hardaker | 8b14d73 | 2006-09-01 23:17:28 +0000 | [diff] [blame] | 439 | char options[128] = "aAc:CdD::fhHI:l:L:m:M:n:p:P:qrsS:UvV-:Y:"; |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 440 | int arg, i, ret; |
Robert Story | d004d1b | 2005-09-27 15:34:40 +0000 | [diff] [blame] | 441 | int dont_fork = 0, do_help = 0; |
Robert Story | 69b77f4 | 2005-11-20 19:39:13 +0000 | [diff] [blame] | 442 | int log_set = 0; |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 443 | int agent_mode = -1; |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 444 | char *pid_file = NULL; |
Niels Baggesen | 59d5f7a | 2003-11-02 12:50:39 +0000 | [diff] [blame] | 445 | char option_compatability[] = "-Le"; |
Michael Slifcak | 1b78b76 | 1999-11-19 01:58:47 +0000 | [diff] [blame] | 446 | #if HAVE_GETPID |
Wes Hardaker | a36188e | 2003-07-02 17:58:55 +0000 | [diff] [blame] | 447 | int fd; |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 448 | FILE *PID; |
Michael Slifcak | 1b78b76 | 1999-11-19 01:58:47 +0000 | [diff] [blame] | 449 | #endif |
Wes Hardaker | aebff77 | 1995-07-10 18:27:00 +0000 | [diff] [blame] | 450 | |
Alex Burger | d3dd2a5 | 2005-08-01 11:39:44 +0000 | [diff] [blame] | 451 | #ifndef WIN32 |
Robert Story | 7bf3d20 | 2005-07-07 16:25:39 +0000 | [diff] [blame] | 452 | /* |
| 453 | * close all non-standard file descriptors we may have |
| 454 | * inherited from the shell. |
| 455 | */ |
| 456 | for (i = getdtablesize() - 1; i > 2; --i) { |
| 457 | (void) close(i); |
| 458 | } |
Robert Story | a34441b | 2005-08-18 21:50:41 +0000 | [diff] [blame] | 459 | #endif /* #WIN32 */ |
Robert Story | 7bf3d20 | 2005-07-07 16:25:39 +0000 | [diff] [blame] | 460 | |
| 461 | /* |
| 462 | * register signals ASAP to prevent default action (usually core) |
| 463 | * for signals during startup... |
| 464 | */ |
| 465 | #ifdef SIGTERM |
| 466 | DEBUGMSGTL(("signal", "registering SIGTERM signal handler\n")); |
| 467 | signal(SIGTERM, SnmpdShutDown); |
| 468 | #endif |
| 469 | #ifdef SIGINT |
| 470 | DEBUGMSGTL(("signal", "registering SIGINT signal handler\n")); |
| 471 | signal(SIGINT, SnmpdShutDown); |
| 472 | #endif |
| 473 | #ifdef SIGHUP |
Wes Hardaker | 5e8c26a | 2007-05-17 13:53:50 +0000 | [diff] [blame] | 474 | signal(SIGHUP, SIG_IGN); /* do not terminate on early SIGHUP */ |
Robert Story | 7bf3d20 | 2005-07-07 16:25:39 +0000 | [diff] [blame] | 475 | #endif |
| 476 | #ifdef SIGUSR1 |
| 477 | DEBUGMSGTL(("signal", "registering SIGUSR1 signal handler\n")); |
| 478 | signal(SIGUSR1, SnmpdDump); |
| 479 | #endif |
| 480 | #ifdef SIGPIPE |
| 481 | DEBUGMSGTL(("signal", "registering SIGPIPE signal handler\n")); |
| 482 | signal(SIGPIPE, SIG_IGN); /* 'Inline' failure of wayward readers */ |
| 483 | #endif |
| 484 | #ifdef SIGXFSZ |
| 485 | signal(SIGXFSZ, SnmpdCatchRandomSignal); |
| 486 | #endif |
| 487 | |
Thomas Anders | e7563bf | 2006-09-15 00:48:50 +0000 | [diff] [blame] | 488 | #ifdef NETSNMP_NO_ROOT_ACCESS |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 489 | /* |
| 490 | * Default to no. |
| 491 | */ |
John Naylon | bb401c1 | 2002-05-15 12:53:01 +0000 | [diff] [blame] | 492 | netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, |
| 493 | NETSNMP_DS_AGENT_NO_ROOT_ACCESS, 1); |
Wes Hardaker | 45aeab1 | 1999-11-17 19:55:41 +0000 | [diff] [blame] | 494 | #endif |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 495 | /* |
| 496 | * Default to NOT running an AgentX master. |
| 497 | */ |
John Naylon | bb401c1 | 2002-05-15 12:53:01 +0000 | [diff] [blame] | 498 | netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, |
| 499 | NETSNMP_DS_AGENT_AGENTX_MASTER, 0); |
Robert Story | 87cc9fa | 2002-07-19 21:14:33 +0000 | [diff] [blame] | 500 | netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, |
| 501 | NETSNMP_DS_AGENT_AGENTX_TIMEOUT, -1); |
| 502 | netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, |
| 503 | NETSNMP_DS_AGENT_AGENTX_RETRIES, -1); |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 504 | |
Robert Story | 19617e3 | 2004-03-02 04:52:45 +0000 | [diff] [blame] | 505 | netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, |
| 506 | NETSNMP_DS_AGENT_CACHE_TIMEOUT, 5); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 507 | /* |
| 508 | * Add some options if they are available. |
| 509 | */ |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 510 | #if HAVE_UNISTD_H |
| 511 | strcat(options, "g:u:"); |
| 512 | #endif |
| 513 | #if defined(USING_AGENTX_SUBAGENT_MODULE)|| defined(USING_AGENTX_MASTER_MODULE) |
| 514 | strcat(options, "x:"); |
| 515 | #endif |
| 516 | #ifdef USING_AGENTX_SUBAGENT_MODULE |
| 517 | strcat(options, "X"); |
Wes Hardaker | abf3f57 | 2000-10-26 00:44:22 +0000 | [diff] [blame] | 518 | #endif |
| 519 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 520 | /* |
Dave Shield | ea81023 | 2003-07-09 12:25:18 +0000 | [diff] [blame] | 521 | * This is incredibly ugly, but it's probably the simplest way |
| 522 | * to handle the old '-L' option as well as the new '-Lx' style |
| 523 | */ |
| 524 | for (i=0; i<argc; i++) { |
| 525 | if (!strcmp(argv[i], "-L")) |
| 526 | argv[i] = option_compatability; |
| 527 | } |
| 528 | |
Wes Hardaker | 3b14c59 | 2011-02-23 00:01:18 +0000 | [diff] [blame] | 529 | #ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG |
Alex Burger | 327de41 | 2005-11-10 02:33:17 +0000 | [diff] [blame] | 530 | #ifdef WIN32 |
| 531 | snmp_log_syslogname(app_name_long); |
| 532 | #else |
Alex Burger | 8233c6d | 2004-10-04 22:14:34 +0000 | [diff] [blame] | 533 | snmp_log_syslogname(app_name); |
Alex Burger | 327de41 | 2005-11-10 02:33:17 +0000 | [diff] [blame] | 534 | #endif |
Wes Hardaker | 3b14c59 | 2011-02-23 00:01:18 +0000 | [diff] [blame] | 535 | #endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */ |
Robert Story | d004d1b | 2005-09-27 15:34:40 +0000 | [diff] [blame] | 536 | netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, |
| 537 | NETSNMP_DS_LIB_APPTYPE, app_name); |
Alex Burger | 2f97d7b | 2004-09-29 17:35:44 +0000 | [diff] [blame] | 538 | |
Dave Shield | ea81023 | 2003-07-09 12:25:18 +0000 | [diff] [blame] | 539 | /* |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 540 | * Now process options normally. |
| 541 | */ |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 542 | while ((arg = getopt(argc, argv, options)) != EOF) { |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 543 | switch (arg) { |
Wes Hardaker | 310607d | 2002-02-16 08:17:42 +0000 | [diff] [blame] | 544 | case '-': |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 545 | if (strcasecmp(optarg, "help") == 0) { |
| 546 | usage(argv[0]); |
| 547 | } |
| 548 | if (strcasecmp(optarg, "version") == 0) { |
| 549 | version(); |
| 550 | } |
Wes Hardaker | 310607d | 2002-02-16 08:17:42 +0000 | [diff] [blame] | 551 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 552 | handle_long_opt(optarg); |
| 553 | break; |
Wes Hardaker | 310607d | 2002-02-16 08:17:42 +0000 | [diff] [blame] | 554 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 555 | case 'a': |
| 556 | log_addresses++; |
| 557 | break; |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 558 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 559 | case 'A': |
Robert Story | 69b77f4 | 2005-11-20 19:39:13 +0000 | [diff] [blame] | 560 | netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, |
| 561 | NETSNMP_DS_LIB_APPEND_LOGFILES, 1); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 562 | break; |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 563 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 564 | case 'c': |
| 565 | if (optarg != NULL) { |
John Naylon | bb401c1 | 2002-05-15 12:53:01 +0000 | [diff] [blame] | 566 | netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, |
| 567 | NETSNMP_DS_LIB_OPTIONALCONFIG, optarg); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 568 | } else { |
| 569 | usage(argv[0]); |
| 570 | } |
| 571 | break; |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 572 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 573 | case 'C': |
John Naylon | bb401c1 | 2002-05-15 12:53:01 +0000 | [diff] [blame] | 574 | netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, |
| 575 | NETSNMP_DS_LIB_DONT_READ_CONFIGS, 1); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 576 | break; |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 577 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 578 | case 'd': |
Jan Safranek | 0ef34e6 | 2012-03-08 10:11:30 +0100 | [diff] [blame] | 579 | netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, |
| 580 | NETSNMP_DS_LIB_DUMP_PACKET, |
Wes Hardaker | 3b14c59 | 2011-02-23 00:01:18 +0000 | [diff] [blame] | 581 | ++snmp_dump_packet); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 582 | break; |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 583 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 584 | case 'D': |
Niels Baggesen | 131c93b | 2014-05-06 06:20:28 +0200 | [diff] [blame] | 585 | #ifdef NETSNMP_DISABLE_DEBUGGING |
| 586 | fprintf(stderr, "Debugging not configured\n"); |
| 587 | exit(1); |
| 588 | #else |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 589 | debug_register_tokens(optarg); |
| 590 | snmp_set_do_debugging(1); |
Niels Baggesen | 131c93b | 2014-05-06 06:20:28 +0200 | [diff] [blame] | 591 | #endif |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 592 | break; |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 593 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 594 | case 'f': |
| 595 | dont_fork = 1; |
| 596 | break; |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 597 | |
| 598 | #if HAVE_UNISTD_H |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 599 | case 'g': |
| 600 | if (optarg != NULL) { |
Dave Shield | 968c0dd | 2008-08-17 20:50:55 +0000 | [diff] [blame] | 601 | char *ecp; |
| 602 | int gid; |
| 603 | |
| 604 | gid = strtoul(optarg, &ecp, 10); |
Bart Van Assche | 396e122 | 2011-12-01 09:32:16 +0100 | [diff] [blame] | 605 | #if HAVE_GETGRNAM && HAVE_PWD_H |
Dave Shield | 968c0dd | 2008-08-17 20:50:55 +0000 | [diff] [blame] | 606 | if (*ecp) { |
Dave Shield | 968c0dd | 2008-08-17 20:50:55 +0000 | [diff] [blame] | 607 | struct group *info; |
Bart Van Assche | 396e122 | 2011-12-01 09:32:16 +0100 | [diff] [blame] | 608 | |
Dave Shield | 968c0dd | 2008-08-17 20:50:55 +0000 | [diff] [blame] | 609 | info = getgrnam(optarg); |
Bart Van Assche | 396e122 | 2011-12-01 09:32:16 +0100 | [diff] [blame] | 610 | gid = info ? info->gr_gid : -1; |
| 611 | endgrent(); |
| 612 | } |
Dave Shield | 968c0dd | 2008-08-17 20:50:55 +0000 | [diff] [blame] | 613 | #endif |
Bart Van Assche | 396e122 | 2011-12-01 09:32:16 +0100 | [diff] [blame] | 614 | if (gid < 0) { |
| 615 | fprintf(stderr, "Bad group id: %s\n", optarg); |
| 616 | exit(1); |
Dave Shield | 968c0dd | 2008-08-17 20:50:55 +0000 | [diff] [blame] | 617 | } |
John Naylon | bb401c1 | 2002-05-15 12:53:01 +0000 | [diff] [blame] | 618 | netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, |
Dave Shield | 968c0dd | 2008-08-17 20:50:55 +0000 | [diff] [blame] | 619 | NETSNMP_DS_AGENT_GROUPID, gid); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 620 | } else { |
| 621 | usage(argv[0]); |
| 622 | } |
| 623 | break; |
Wes Hardaker | abf3f57 | 2000-10-26 00:44:22 +0000 | [diff] [blame] | 624 | #endif |
Dave Shield | c76ef93 | 2000-03-21 16:47:40 +0000 | [diff] [blame] | 625 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 626 | case 'h': |
| 627 | usage(argv[0]); |
| 628 | break; |
Wes Hardaker | 45aeab1 | 1999-11-17 19:55:41 +0000 | [diff] [blame] | 629 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 630 | case 'H': |
Robert Story | d004d1b | 2005-09-27 15:34:40 +0000 | [diff] [blame] | 631 | do_help = 1; |
| 632 | break; |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 633 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 634 | case 'I': |
| 635 | if (optarg != NULL) { |
| 636 | add_to_init_list(optarg); |
| 637 | } else { |
| 638 | usage(argv[0]); |
| 639 | } |
| 640 | break; |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 641 | |
Bart Van Assche | 2e9466d | 2011-07-26 13:10:04 +0200 | [diff] [blame] | 642 | #ifndef NETSNMP_FEATURE_REMOVE_LOGGING_FILE |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 643 | case 'l': |
Dave Shield | e755185 | 2003-10-24 12:49:22 +0000 | [diff] [blame] | 644 | printf("Warning: -l option is deprecated, use -Lf <file> instead\n"); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 645 | if (optarg != NULL) { |
| 646 | if (strlen(optarg) > PATH_MAX) { |
| 647 | fprintf(stderr, |
| 648 | "%s: logfile path too long (limit %d chars)\n", |
| 649 | argv[0], PATH_MAX); |
| 650 | exit(1); |
| 651 | } |
Robert Story | 69b77f4 | 2005-11-20 19:39:13 +0000 | [diff] [blame] | 652 | snmp_enable_filelog(optarg, |
| 653 | netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, |
| 654 | NETSNMP_DS_LIB_APPEND_LOGFILES)); |
Robert Story | 60371e8 | 2005-06-20 20:38:41 +0000 | [diff] [blame] | 655 | log_set = 1; |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 656 | } else { |
| 657 | usage(argv[0]); |
| 658 | } |
| 659 | break; |
Bart Van Assche | 2e9466d | 2011-07-26 13:10:04 +0200 | [diff] [blame] | 660 | #endif /* NETSNMP_FEATURE_REMOVE_LOGGING_FILE */ |
Wes Hardaker | 45aeab1 | 1999-11-17 19:55:41 +0000 | [diff] [blame] | 661 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 662 | case 'L': |
Dave Shield | 93d8c23 | 2003-06-17 12:54:27 +0000 | [diff] [blame] | 663 | if (snmp_log_options( optarg, argc, argv ) < 0 ) { |
| 664 | usage(argv[0]); |
| 665 | } |
Robert Story | 60371e8 | 2005-06-20 20:38:41 +0000 | [diff] [blame] | 666 | log_set = 1; |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 667 | break; |
Wes Hardaker | 45aeab1 | 1999-11-17 19:55:41 +0000 | [diff] [blame] | 668 | |
Dave Shield | e755185 | 2003-10-24 12:49:22 +0000 | [diff] [blame] | 669 | case 'm': |
| 670 | if (optarg != NULL) { |
| 671 | setenv("MIBS", optarg, 1); |
| 672 | } else { |
| 673 | usage(argv[0]); |
| 674 | } |
| 675 | break; |
| 676 | |
| 677 | case 'M': |
| 678 | if (optarg != NULL) { |
| 679 | setenv("MIBDIRS", optarg, 1); |
| 680 | } else { |
| 681 | usage(argv[0]); |
| 682 | } |
| 683 | break; |
| 684 | |
Robert Story | d004d1b | 2005-09-27 15:34:40 +0000 | [diff] [blame] | 685 | case 'n': |
| 686 | if (optarg != NULL) { |
| 687 | app_name = optarg; |
| 688 | netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, |
| 689 | NETSNMP_DS_LIB_APPTYPE, app_name); |
| 690 | } else { |
| 691 | usage(argv[0]); |
| 692 | } |
| 693 | break; |
| 694 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 695 | case 'P': |
Dave Shield | e755185 | 2003-10-24 12:49:22 +0000 | [diff] [blame] | 696 | printf("Warning: -P option is deprecated, use -p instead\n"); |
| 697 | case 'p': |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 698 | if (optarg != NULL) { |
| 699 | pid_file = optarg; |
| 700 | } else { |
| 701 | usage(argv[0]); |
| 702 | } |
| 703 | break; |
Wes Hardaker | dc4b3f6 | 2000-05-31 22:53:11 +0000 | [diff] [blame] | 704 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 705 | case 'q': |
Wes Hardaker | 3b14c59 | 2011-02-23 00:01:18 +0000 | [diff] [blame] | 706 | netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, |
| 707 | NETSNMP_DS_LIB_QUICK_PRINT, 1); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 708 | break; |
| 709 | |
| 710 | case 'r': |
John Naylon | bb401c1 | 2002-05-15 12:53:01 +0000 | [diff] [blame] | 711 | netsnmp_ds_toggle_boolean(NETSNMP_DS_APPLICATION_ID, |
| 712 | NETSNMP_DS_AGENT_NO_ROOT_ACCESS); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 713 | break; |
| 714 | |
Wes Hardaker | 3b14c59 | 2011-02-23 00:01:18 +0000 | [diff] [blame] | 715 | #ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 716 | case 's': |
Dave Shield | e755185 | 2003-10-24 12:49:22 +0000 | [diff] [blame] | 717 | printf("Warning: -s option is deprecated, use -Lsd instead\n"); |
Robert Story | 68f9809 | 2005-06-20 21:38:40 +0000 | [diff] [blame] | 718 | snmp_enable_syslog(); |
| 719 | log_set = 1; |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 720 | break; |
Wes Hardaker | dc4b3f6 | 2000-05-31 22:53:11 +0000 | [diff] [blame] | 721 | |
Dave Shield | f542e4a | 2002-12-12 16:37:26 +0000 | [diff] [blame] | 722 | case 'S': |
Dave Shield | e755185 | 2003-10-24 12:49:22 +0000 | [diff] [blame] | 723 | printf("Warning: -S option is deprecated, use -Ls <facility> instead\n"); |
Dave Shield | f542e4a | 2002-12-12 16:37:26 +0000 | [diff] [blame] | 724 | if (optarg != NULL) { |
| 725 | switch (*optarg) { |
| 726 | case 'd': |
| 727 | case 'D': |
| 728 | Facility = LOG_DAEMON; |
| 729 | break; |
| 730 | case 'i': |
| 731 | case 'I': |
| 732 | Facility = LOG_INFO; |
| 733 | break; |
| 734 | case '0': |
| 735 | Facility = LOG_LOCAL0; |
| 736 | break; |
| 737 | case '1': |
| 738 | Facility = LOG_LOCAL1; |
| 739 | break; |
| 740 | case '2': |
| 741 | Facility = LOG_LOCAL2; |
| 742 | break; |
| 743 | case '3': |
| 744 | Facility = LOG_LOCAL3; |
| 745 | break; |
| 746 | case '4': |
| 747 | Facility = LOG_LOCAL4; |
| 748 | break; |
| 749 | case '5': |
| 750 | Facility = LOG_LOCAL5; |
| 751 | break; |
| 752 | case '6': |
| 753 | Facility = LOG_LOCAL6; |
| 754 | break; |
| 755 | case '7': |
| 756 | Facility = LOG_LOCAL7; |
| 757 | break; |
| 758 | default: |
John Naylon | 32ae177 | 2002-12-18 13:19:07 +0000 | [diff] [blame] | 759 | fprintf(stderr, "invalid syslog facility: -S%c\n",*optarg); |
Dave Shield | f542e4a | 2002-12-12 16:37:26 +0000 | [diff] [blame] | 760 | usage(argv[0]); |
| 761 | } |
Robert Story | 68f9809 | 2005-06-20 21:38:40 +0000 | [diff] [blame] | 762 | snmp_enable_syslog_ident(snmp_log_syslogname(NULL), Facility); |
| 763 | log_set = 1; |
Dave Shield | f542e4a | 2002-12-12 16:37:26 +0000 | [diff] [blame] | 764 | } else { |
| 765 | fprintf(stderr, "no syslog facility specified\n"); |
| 766 | usage(argv[0]); |
| 767 | } |
| 768 | break; |
Wes Hardaker | 3b14c59 | 2011-02-23 00:01:18 +0000 | [diff] [blame] | 769 | #endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */ |
Dave Shield | f542e4a | 2002-12-12 16:37:26 +0000 | [diff] [blame] | 770 | |
Dave Shield | 1dff19c | 2002-12-11 09:30:55 +0000 | [diff] [blame] | 771 | case 'U': |
| 772 | netsnmp_ds_toggle_boolean(NETSNMP_DS_APPLICATION_ID, |
| 773 | NETSNMP_DS_AGENT_LEAVE_PIDFILE); |
| 774 | break; |
| 775 | |
Wes Hardaker | 553f1cc | 1999-07-08 23:02:58 +0000 | [diff] [blame] | 776 | #if HAVE_UNISTD_H |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 777 | case 'u': |
| 778 | if (optarg != NULL) { |
| 779 | char *ecp; |
| 780 | int uid; |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 781 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 782 | uid = strtoul(optarg, &ecp, 10); |
Bart Van Assche | 396e122 | 2011-12-01 09:32:16 +0100 | [diff] [blame] | 783 | #if HAVE_GETPWNAM && HAVE_PWD_H |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 784 | if (*ecp) { |
Bart Van Assche | 396e122 | 2011-12-01 09:32:16 +0100 | [diff] [blame] | 785 | struct passwd *info; |
| 786 | |
John Naylon | 0cd236a | 2002-05-21 12:32:04 +0000 | [diff] [blame] | 787 | info = getpwnam(optarg); |
Bart Van Assche | 396e122 | 2011-12-01 09:32:16 +0100 | [diff] [blame] | 788 | uid = info ? info->pw_uid : -1; |
| 789 | endpwent(); |
| 790 | } |
Wes Hardaker | 9743e9f | 2001-10-11 21:01:50 +0000 | [diff] [blame] | 791 | #endif |
Bart Van Assche | 396e122 | 2011-12-01 09:32:16 +0100 | [diff] [blame] | 792 | if (uid < 0) { |
| 793 | fprintf(stderr, "Bad user id: %s\n", optarg); |
| 794 | exit(1); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 795 | } |
John Naylon | bb401c1 | 2002-05-15 12:53:01 +0000 | [diff] [blame] | 796 | netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, |
| 797 | NETSNMP_DS_AGENT_USERID, uid); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 798 | } else { |
| 799 | usage(argv[0]); |
| 800 | } |
| 801 | break; |
Michael Slifcak | fd869ca | 1999-07-30 11:00:28 +0000 | [diff] [blame] | 802 | #endif |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 803 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 804 | case 'v': |
| 805 | version(); |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 806 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 807 | case 'V': |
John Naylon | bb401c1 | 2002-05-15 12:53:01 +0000 | [diff] [blame] | 808 | netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, |
| 809 | NETSNMP_DS_AGENT_VERBOSE, 1); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 810 | break; |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 811 | |
| 812 | #if defined(USING_AGENTX_SUBAGENT_MODULE)|| defined(USING_AGENTX_MASTER_MODULE) |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 813 | case 'x': |
| 814 | if (optarg != NULL) { |
John Naylon | bb401c1 | 2002-05-15 12:53:01 +0000 | [diff] [blame] | 815 | netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID, |
| 816 | NETSNMP_DS_AGENT_X_SOCKET, optarg); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 817 | } else { |
| 818 | usage(argv[0]); |
| 819 | } |
John Naylon | bb401c1 | 2002-05-15 12:53:01 +0000 | [diff] [blame] | 820 | netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, |
| 821 | NETSNMP_DS_AGENT_AGENTX_MASTER, 1); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 822 | break; |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 823 | #endif |
| 824 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 825 | case 'X': |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 826 | #if defined(USING_AGENTX_SUBAGENT_MODULE) |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 827 | agent_mode = SUB_AGENT; |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 828 | #else |
John Naylon | 32ae177 | 2002-12-18 13:19:07 +0000 | [diff] [blame] | 829 | fprintf(stderr, "%s: Illegal argument -X:" |
| 830 | "AgentX support not compiled in.\n", argv[0]); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 831 | usage(argv[0]); |
| 832 | exit(1); |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 833 | #endif |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 834 | break; |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 835 | |
Wes Hardaker | 8b14d73 | 2006-09-01 23:17:28 +0000 | [diff] [blame] | 836 | case 'Y': |
| 837 | netsnmp_config_remember(optarg); |
| 838 | break; |
| 839 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 840 | default: |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 841 | usage(argv[0]); |
| 842 | break; |
| 843 | } |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Robert Story | d004d1b | 2005-09-27 15:34:40 +0000 | [diff] [blame] | 846 | if (do_help) { |
| 847 | netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, |
| 848 | NETSNMP_DS_AGENT_NO_ROOT_ACCESS, 1); |
| 849 | init_agent(app_name); /* register our .conf handlers */ |
| 850 | init_mib_modules(); |
| 851 | init_snmp(app_name); |
| 852 | fprintf(stderr, "Configuration directives understood:\n"); |
| 853 | read_config_print_usage(" "); |
| 854 | exit(0); |
| 855 | } |
| 856 | |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 857 | if (optind < argc) { |
Wes Hardaker | 6c4cdaf | 2011-04-27 21:31:39 +0000 | [diff] [blame] | 858 | #ifndef NETSNMP_NO_LISTEN_SUPPORT |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 859 | /* |
| 860 | * There are optional transport addresses on the command line. |
| 861 | */ |
| 862 | DEBUGMSGTL(("snmpd/main", "optind %d, argc %d\n", optind, argc)); |
| 863 | for (i = optind; i < argc; i++) { |
John Naylon | 32ae177 | 2002-12-18 13:19:07 +0000 | [diff] [blame] | 864 | char *c, *astring; |
John Naylon | bb401c1 | 2002-05-15 12:53:01 +0000 | [diff] [blame] | 865 | if ((c = netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID, |
| 866 | NETSNMP_DS_AGENT_PORTS))) { |
Dave Shield | 679c430 | 2010-06-16 14:22:27 +0000 | [diff] [blame] | 867 | astring = (char*)malloc(strlen(c) + 2 + strlen(argv[i])); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 868 | if (astring == NULL) { |
John Naylon | 32ae177 | 2002-12-18 13:19:07 +0000 | [diff] [blame] | 869 | fprintf(stderr, "malloc failure processing argv[%d]\n", i); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 870 | exit(1); |
| 871 | } |
| 872 | sprintf(astring, "%s,%s", c, argv[i]); |
John Naylon | bb401c1 | 2002-05-15 12:53:01 +0000 | [diff] [blame] | 873 | netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID, |
| 874 | NETSNMP_DS_AGENT_PORTS, astring); |
Wes Hardaker | 91a011e | 2003-07-02 20:29:47 +0000 | [diff] [blame] | 875 | SNMP_FREE(astring); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 876 | } else { |
John Naylon | bb401c1 | 2002-05-15 12:53:01 +0000 | [diff] [blame] | 877 | netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID, |
| 878 | NETSNMP_DS_AGENT_PORTS, argv[i]); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | DEBUGMSGTL(("snmpd/main", "port spec: %s\n", |
John Naylon | bb401c1 | 2002-05-15 12:53:01 +0000 | [diff] [blame] | 882 | netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID, |
| 883 | NETSNMP_DS_AGENT_PORTS))); |
Wes Hardaker | 6c4cdaf | 2011-04-27 21:31:39 +0000 | [diff] [blame] | 884 | #else /* NETSNMP_NO_LISTEN_SUPPORT */ |
| 885 | fprintf(stderr, "You specified ports to open; this agent was built to only send notifications\n"); |
| 886 | exit(1); |
| 887 | #endif /* NETSNMP_NO_LISTEN_SUPPORT */ |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Thomas Anders | e7563bf | 2006-09-15 00:48:50 +0000 | [diff] [blame] | 890 | #ifdef NETSNMP_LOGFILE |
Bart Van Assche | 2e9466d | 2011-07-26 13:10:04 +0200 | [diff] [blame] | 891 | #ifndef NETSNMP_FEATURE_REMOVE_LOGGING_FILE |
Robert Story | 60371e8 | 2005-06-20 20:38:41 +0000 | [diff] [blame] | 892 | if (0 == log_set) |
Thomas Anders | e7563bf | 2006-09-15 00:48:50 +0000 | [diff] [blame] | 893 | snmp_enable_filelog(NETSNMP_LOGFILE, |
Robert Story | 69b77f4 | 2005-11-20 19:39:13 +0000 | [diff] [blame] | 894 | netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, |
| 895 | NETSNMP_DS_LIB_APPEND_LOGFILES)); |
Bart Van Assche | 2e9466d | 2011-07-26 13:10:04 +0200 | [diff] [blame] | 896 | #endif /* NETSNMP_FEATURE_REMOVE_LOGGING_FILE */ |
Robert Story | 60371e8 | 2005-06-20 20:38:41 +0000 | [diff] [blame] | 897 | #endif |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 898 | |
Magnus Fromreide | adff160 | 2010-10-16 22:32:38 +0000 | [diff] [blame] | 899 | #ifdef USING_UTIL_FUNCS_RESTART_MODULE |
| 900 | { |
| 901 | /* |
| 902 | * Initialize a argv set to the current for restarting the agent. |
| 903 | */ |
| 904 | char *cptr, **argvptr; |
| 905 | |
| 906 | argvrestartp = (char **)malloc((argc + 2) * sizeof(char *)); |
| 907 | argvptr = argvrestartp; |
| 908 | for (i = 0, ret = 1; i < argc; i++) { |
| 909 | ret += strlen(argv[i]) + 1; |
| 910 | } |
| 911 | argvrestart = (char *) malloc(ret); |
| 912 | argvrestartname = (char *) malloc(strlen(argv[0]) + 1); |
| 913 | if (!argvrestartp || !argvrestart || !argvrestartname) { |
| 914 | fprintf(stderr, "malloc failure processing argvrestart\n"); |
| 915 | exit(1); |
| 916 | } |
| 917 | strcpy(argvrestartname, argv[0]); |
| 918 | |
| 919 | for (cptr = argvrestart, i = 0; i < argc; i++) { |
| 920 | strcpy(cptr, argv[i]); |
| 921 | *(argvptr++) = cptr; |
| 922 | cptr += strlen(argv[i]) + 1; |
| 923 | } |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 924 | } |
Magnus Fromreide | adff160 | 2010-10-16 22:32:38 +0000 | [diff] [blame] | 925 | #endif /* USING_UTIL_FUNCS_RESTART_MODULE */ |
| 926 | |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 927 | if (agent_mode == -1) { |
Magnus Fromreide | adff160 | 2010-10-16 22:32:38 +0000 | [diff] [blame] | 928 | if (strstr(argv[0], "agentxd") != NULL) { |
| 929 | netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, |
John Naylon | bb401c1 | 2002-05-15 12:53:01 +0000 | [diff] [blame] | 930 | NETSNMP_DS_AGENT_ROLE, SUB_AGENT); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 931 | } else { |
Magnus Fromreide | adff160 | 2010-10-16 22:32:38 +0000 | [diff] [blame] | 932 | netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, |
John Naylon | bb401c1 | 2002-05-15 12:53:01 +0000 | [diff] [blame] | 933 | NETSNMP_DS_AGENT_ROLE, MASTER_AGENT); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 934 | } |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 935 | } else { |
Magnus Fromreide | adff160 | 2010-10-16 22:32:38 +0000 | [diff] [blame] | 936 | netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, |
John Naylon | bb401c1 | 2002-05-15 12:53:01 +0000 | [diff] [blame] | 937 | NETSNMP_DS_AGENT_ROLE, agent_mode); |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 938 | } |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 939 | |
Michael Slifcak | 1b78b76 | 1999-11-19 01:58:47 +0000 | [diff] [blame] | 940 | SOCK_STARTUP; |
Bart Van Assche | e1b910b | 2012-02-23 10:27:54 +0100 | [diff] [blame] | 941 | if (init_agent(app_name) != 0) { |
| 942 | snmp_log(LOG_ERR, "Agent initialization failed\n"); |
| 943 | exit(1); |
| 944 | } |
Wes Hardaker | f2dc63b | 1999-09-28 23:52:04 +0000 | [diff] [blame] | 945 | init_mib_modules(); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 946 | |
| 947 | /* |
| 948 | * start library |
| 949 | */ |
Robert Story | d004d1b | 2005-09-27 15:34:40 +0000 | [diff] [blame] | 950 | init_snmp(app_name); |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 951 | |
Wes Hardaker | b469d77 | 2001-02-09 18:12:51 +0000 | [diff] [blame] | 952 | if ((ret = init_master_agent()) != 0) { |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 953 | /* |
| 954 | * Some error opening one of the specified agent transports. |
| 955 | */ |
Magnus Fromreide | 7cf3cf9 | 2010-10-17 01:41:29 +0000 | [diff] [blame] | 956 | snmp_log(LOG_ERR, "Server Exiting with code 1\n"); |
| 957 | exit(1); |
Wes Hardaker | b469d77 | 2001-02-09 18:12:51 +0000 | [diff] [blame] | 958 | } |
Niels Baggesen | 448c1c3 | 1999-08-19 21:09:25 +0000 | [diff] [blame] | 959 | |
Jan Safranek | db21e01 | 2008-12-04 12:33:33 +0000 | [diff] [blame] | 960 | /* |
| 961 | * Initialize the world. Detach from the shell. Create initial user. |
| 962 | */ |
| 963 | if(!dont_fork) { |
| 964 | int quit = ! netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, |
| 965 | NETSNMP_DS_AGENT_QUIT_IMMEDIATELY); |
Wes Hardaker | 3b14c59 | 2011-02-23 00:01:18 +0000 | [diff] [blame] | 966 | ret = netsnmp_daemonize(quit, |
Bart Van Assche | 2e9466d | 2011-07-26 13:10:04 +0200 | [diff] [blame] | 967 | #ifndef NETSNMP_FEATURE_REMOVE_LOGGING_STDIO |
Wes Hardaker | 3b14c59 | 2011-02-23 00:01:18 +0000 | [diff] [blame] | 968 | snmp_stderrlog_status() |
Bart Van Assche | 2e9466d | 2011-07-26 13:10:04 +0200 | [diff] [blame] | 969 | #else /* NETSNMP_FEATURE_REMOVE_LOGGING_STDIO */ |
Wes Hardaker | 3b14c59 | 2011-02-23 00:01:18 +0000 | [diff] [blame] | 970 | 0 |
Bart Van Assche | 2e9466d | 2011-07-26 13:10:04 +0200 | [diff] [blame] | 971 | #endif /* NETSNMP_FEATURE_REMOVE_LOGGING_STDIO */ |
Wes Hardaker | 3b14c59 | 2011-02-23 00:01:18 +0000 | [diff] [blame] | 972 | ); |
Jan Safranek | db21e01 | 2008-12-04 12:33:33 +0000 | [diff] [blame] | 973 | /* |
| 974 | * xxx-rks: do we care if fork fails? I think we should... |
| 975 | */ |
Magnus Fromreide | 7cf3cf9 | 2010-10-17 01:41:29 +0000 | [diff] [blame] | 976 | if(ret != 0) { |
| 977 | snmp_log(LOG_ERR, "Server Exiting with code 1\n"); |
| 978 | exit(1); |
| 979 | } |
Jan Safranek | db21e01 | 2008-12-04 12:33:33 +0000 | [diff] [blame] | 980 | } |
| 981 | |
John Naylon | 8ec2cfd | 2002-04-30 15:16:06 +0000 | [diff] [blame] | 982 | #if HAVE_GETPID |
| 983 | if (pid_file != NULL) { |
Wes Hardaker | a36188e | 2003-07-02 17:58:55 +0000 | [diff] [blame] | 984 | /* |
| 985 | * unlink the pid_file, if it exists, prior to open. Without |
| 986 | * doing this the open will fail if the user specified pid_file |
| 987 | * already exists. |
| 988 | */ |
| 989 | unlink(pid_file); |
| 990 | fd = open(pid_file, O_CREAT | O_EXCL | O_WRONLY, 0600); |
| 991 | if (fd == -1) { |
| 992 | snmp_log_perror(pid_file); |
John Naylon | bb401c1 | 2002-05-15 12:53:01 +0000 | [diff] [blame] | 993 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, |
Wes Hardaker | a36188e | 2003-07-02 17:58:55 +0000 | [diff] [blame] | 994 | NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) { |
John Naylon | 8ec2cfd | 2002-04-30 15:16:06 +0000 | [diff] [blame] | 995 | exit(1); |
| 996 | } |
| 997 | } else { |
Wes Hardaker | a36188e | 2003-07-02 17:58:55 +0000 | [diff] [blame] | 998 | if ((PID = fdopen(fd, "w")) == NULL) { |
| 999 | snmp_log_perror(pid_file); |
| 1000 | exit(1); |
| 1001 | } else { |
| 1002 | fprintf(PID, "%d\n", (int) getpid()); |
| 1003 | fclose(PID); |
| 1004 | } |
Bart Van Assche | 2f8d4f5 | 2011-08-05 12:02:51 +0200 | [diff] [blame] | 1005 | #ifndef _MSC_VER |
Wes Hardaker | 6ec32d8 | 2011-08-08 15:12:55 -0700 | [diff] [blame] | 1006 | /* The sequence open()/fdopen()/fclose()/close() makes MSVC crash, |
| 1007 | hence skip the close() call when using the MSVC runtime. */ |
Wes Hardaker | a36188e | 2003-07-02 17:58:55 +0000 | [diff] [blame] | 1008 | close(fd); |
Bart Van Assche | 2f8d4f5 | 2011-08-05 12:02:51 +0200 | [diff] [blame] | 1009 | #endif |
John Naylon | 8ec2cfd | 2002-04-30 15:16:06 +0000 | [diff] [blame] | 1010 | } |
| 1011 | } |
| 1012 | #endif |
| 1013 | |
Bart Van Assche | 275d23e | 2011-10-26 09:33:09 +0200 | [diff] [blame] | 1014 | #if defined(HAVE_UNISTD_H) && (defined(HAVE_CHOWN) || defined(HAVE_SETGID) || defined(HAVE_SETUID)) |
| 1015 | { |
| 1016 | const char *persistent_dir; |
| 1017 | int uid, gid; |
| 1018 | |
Niels Baggesen | a2a7316 | 2009-05-06 21:59:20 +0000 | [diff] [blame] | 1019 | persistent_dir = get_persistent_directory(); |
| 1020 | mkdirhier( persistent_dir, NETSNMP_AGENT_DIRECTORY_MODE, 0 ); |
Dave Shield | 1183b67 | 2006-01-30 09:52:54 +0000 | [diff] [blame] | 1021 | |
| 1022 | uid = netsnmp_ds_get_int(NETSNMP_DS_APPLICATION_ID, |
| 1023 | NETSNMP_DS_AGENT_USERID); |
| 1024 | gid = netsnmp_ds_get_int(NETSNMP_DS_APPLICATION_ID, |
| 1025 | NETSNMP_DS_AGENT_GROUPID); |
| 1026 | |
Bart Van Assche | b9637b4 | 2010-01-22 13:32:10 +0000 | [diff] [blame] | 1027 | #ifdef HAVE_CHOWN |
Dave Shield | 1183b67 | 2006-01-30 09:52:54 +0000 | [diff] [blame] | 1028 | if ( uid != 0 || gid != 0 ) |
Niels Baggesen | a2a7316 | 2009-05-06 21:59:20 +0000 | [diff] [blame] | 1029 | chown( persistent_dir, uid, gid ); |
Bart Van Assche | b9637b4 | 2010-01-22 13:32:10 +0000 | [diff] [blame] | 1030 | #endif |
Dave Shield | 1183b67 | 2006-01-30 09:52:54 +0000 | [diff] [blame] | 1031 | |
Wes Hardaker | fc336c6 | 2000-11-07 23:15:37 +0000 | [diff] [blame] | 1032 | #ifdef HAVE_SETGID |
John Naylon | bb401c1 | 2002-05-15 12:53:01 +0000 | [diff] [blame] | 1033 | if ((gid = netsnmp_ds_get_int(NETSNMP_DS_APPLICATION_ID, |
Jan Safranek | 2b6c119 | 2012-01-03 15:51:10 +0100 | [diff] [blame] | 1034 | NETSNMP_DS_AGENT_GROUPID)) > 0) { |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1035 | DEBUGMSGTL(("snmpd/main", "Changing gid to %d.\n", gid)); |
| 1036 | if (setgid(gid) == -1 |
Wes Hardaker | 9743e9f | 2001-10-11 21:01:50 +0000 | [diff] [blame] | 1037 | #ifdef HAVE_SETGROUPS |
John Naylon | c6a2d0c | 2002-07-09 10:14:13 +0000 | [diff] [blame] | 1038 | || setgroups(1, (gid_t *)&gid) == -1 |
Wes Hardaker | 9743e9f | 2001-10-11 21:01:50 +0000 | [diff] [blame] | 1039 | #endif |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1040 | ) { |
| 1041 | snmp_log_perror("setgid failed"); |
John Naylon | bb401c1 | 2002-05-15 12:53:01 +0000 | [diff] [blame] | 1042 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, |
| 1043 | NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) { |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1044 | exit(1); |
| 1045 | } |
| 1046 | } |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 1047 | } |
Wes Hardaker | fc336c6 | 2000-11-07 23:15:37 +0000 | [diff] [blame] | 1048 | #endif |
| 1049 | #ifdef HAVE_SETUID |
John Naylon | bb401c1 | 2002-05-15 12:53:01 +0000 | [diff] [blame] | 1050 | if ((uid = netsnmp_ds_get_int(NETSNMP_DS_APPLICATION_ID, |
Jan Safranek | 2b6c119 | 2012-01-03 15:51:10 +0100 | [diff] [blame] | 1051 | NETSNMP_DS_AGENT_USERID)) > 0) { |
Dave Shield | a1cb2fc | 2007-05-27 19:06:25 +0000 | [diff] [blame] | 1052 | #if HAVE_GETPWNAM && HAVE_PWD_H && HAVE_INITGROUPS |
Bart Van Assche | 396e122 | 2011-12-01 09:32:16 +0100 | [diff] [blame] | 1053 | struct passwd *info; |
| 1054 | |
Dave Shield | f81731d | 2010-04-06 10:06:13 +0000 | [diff] [blame] | 1055 | /* |
| 1056 | * Set supplementary groups before changing UID |
| 1057 | * (which probably involves giving up privileges) |
| 1058 | */ |
Dave Shield | a1cb2fc | 2007-05-27 19:06:25 +0000 | [diff] [blame] | 1059 | info = getpwuid(uid); |
| 1060 | if (info) { |
| 1061 | DEBUGMSGTL(("snmpd/main", "Supplementary groups for %s.\n", info->pw_name)); |
Dave Shield | d979c39 | 2010-06-16 14:37:14 +0000 | [diff] [blame] | 1062 | if (initgroups(info->pw_name, (gid != 0 ? (gid_t)gid : info->pw_gid)) == -1) { |
Dave Shield | a1cb2fc | 2007-05-27 19:06:25 +0000 | [diff] [blame] | 1063 | snmp_log_perror("initgroups failed"); |
| 1064 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, |
| 1065 | NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) { |
| 1066 | exit(1); |
| 1067 | } |
| 1068 | } |
| 1069 | } |
Bart Van Assche | 396e122 | 2011-12-01 09:32:16 +0100 | [diff] [blame] | 1070 | endpwent(); |
Dave Shield | a1cb2fc | 2007-05-27 19:06:25 +0000 | [diff] [blame] | 1071 | #endif |
Dave Shield | f81731d | 2010-04-06 10:06:13 +0000 | [diff] [blame] | 1072 | DEBUGMSGTL(("snmpd/main", "Changing uid to %d.\n", uid)); |
| 1073 | if (setuid(uid) == -1) { |
| 1074 | snmp_log_perror("setuid failed"); |
| 1075 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, |
| 1076 | NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) { |
| 1077 | exit(1); |
| 1078 | } |
| 1079 | } |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 1080 | } |
Wes Hardaker | 553f1cc | 1999-07-08 23:02:58 +0000 | [diff] [blame] | 1081 | #endif |
Bart Van Assche | 275d23e | 2011-10-26 09:33:09 +0200 | [diff] [blame] | 1082 | } |
Wes Hardaker | fc336c6 | 2000-11-07 23:15:37 +0000 | [diff] [blame] | 1083 | #endif |
Wes Hardaker | 553f1cc | 1999-07-08 23:02:58 +0000 | [diff] [blame] | 1084 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1085 | /* |
Dave Shield | 1183b67 | 2006-01-30 09:52:54 +0000 | [diff] [blame] | 1086 | * Store persistent data immediately in case we crash later. |
| 1087 | */ |
| 1088 | snmp_store(app_name); |
| 1089 | |
Wes Hardaker | 5e8c26a | 2007-05-17 13:53:50 +0000 | [diff] [blame] | 1090 | #ifdef SIGHUP |
| 1091 | DEBUGMSGTL(("signal", "registering SIGHUP signal handler\n")); |
| 1092 | signal(SIGHUP, SnmpdReconfig); |
| 1093 | #endif |
| 1094 | |
Dave Shield | 1183b67 | 2006-01-30 09:52:54 +0000 | [diff] [blame] | 1095 | /* |
| 1096 | * Send coldstart trap if possible. |
| 1097 | */ |
| 1098 | send_easy_trap(0, 0); |
| 1099 | |
| 1100 | /* |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1101 | * We're up, log our version number. |
| 1102 | */ |
Wes Hardaker | 11ef61b | 2002-01-30 01:53:05 +0000 | [diff] [blame] | 1103 | snmp_log(LOG_INFO, "NET-SNMP version %s\n", netsnmp_get_version()); |
Michael Slifcak | 0ef0164 | 2004-02-09 12:06:24 +0000 | [diff] [blame] | 1104 | #ifdef WIN32SERVICE |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 1105 | agent_status = AGENT_RUNNING; |
| 1106 | #endif |
Wes Hardaker | 6fb202d | 2002-03-07 21:42:31 +0000 | [diff] [blame] | 1107 | netsnmp_addrcache_initialise(); |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 1108 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1109 | /* |
| 1110 | * Forever monitor the dest_port for incoming PDUs. |
| 1111 | */ |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 1112 | DEBUGMSGTL(("snmpd/main", "We're up. Starting to process data.\n")); |
John Naylon | bb401c1 | 2002-05-15 12:53:01 +0000 | [diff] [blame] | 1113 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, |
| 1114 | NETSNMP_DS_AGENT_QUIT_IMMEDIATELY)) |
Wes Hardaker | 90e79b4 | 2002-02-20 21:42:25 +0000 | [diff] [blame] | 1115 | receive(); |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 1116 | DEBUGMSGTL(("snmpd/main", "sending shutdown trap\n")); |
| 1117 | SnmpTrapNodeDown(); |
| 1118 | DEBUGMSGTL(("snmpd/main", "Bye...\n")); |
Robert Story | d004d1b | 2005-09-27 15:34:40 +0000 | [diff] [blame] | 1119 | snmp_shutdown(app_name); |
Wes Hardaker | 9dfe0f9 | 2003-07-02 20:20:57 +0000 | [diff] [blame] | 1120 | shutdown_master_agent(); |
| 1121 | shutdown_agent(); |
| 1122 | |
John Naylon | 32ae177 | 2002-12-18 13:19:07 +0000 | [diff] [blame] | 1123 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, |
| 1124 | NETSNMP_DS_AGENT_LEAVE_PIDFILE) && |
| 1125 | (pid_file != NULL)) { |
Dave Shield | 594fade | 2002-12-09 14:32:02 +0000 | [diff] [blame] | 1126 | unlink(pid_file); |
| 1127 | } |
Michael Slifcak | 0ef0164 | 2004-02-09 12:06:24 +0000 | [diff] [blame] | 1128 | #ifdef WIN32SERVICE |
Dave Shield | b02d7fd | 2002-02-22 16:55:42 +0000 | [diff] [blame] | 1129 | agent_status = AGENT_STOPPED; |
| 1130 | #endif |
Wes Hardaker | 9dfe0f9 | 2003-07-02 20:20:57 +0000 | [diff] [blame] | 1131 | |
Magnus Fromreide | adff160 | 2010-10-16 22:32:38 +0000 | [diff] [blame] | 1132 | #ifdef USING_UTIL_FUNCS_RESTART_MODULE |
Wes Hardaker | 9dfe0f9 | 2003-07-02 20:20:57 +0000 | [diff] [blame] | 1133 | SNMP_FREE(argvrestartname); |
| 1134 | SNMP_FREE(argvrestart); |
| 1135 | SNMP_FREE(argvrestartp); |
Magnus Fromreide | adff160 | 2010-10-16 22:32:38 +0000 | [diff] [blame] | 1136 | #endif /* USING_UTIL_FUNCS_RESTART_MODULE */ |
| 1137 | |
Robert Story | 23c7b84 | 2005-02-25 22:45:10 +0000 | [diff] [blame] | 1138 | SOCK_CLEANUP; |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 1139 | return 0; |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1140 | } /* End main() -- snmpd */ |
Wes Hardaker | 8c6ab57 | 1998-06-10 15:53:53 +0000 | [diff] [blame] | 1141 | |
Bart Van Assche | 3937cf1 | 2010-10-18 08:52:00 +0000 | [diff] [blame] | 1142 | #if defined(WIN32) |
| 1143 | |
Bart Van Assche | 3937cf1 | 2010-10-18 08:52:00 +0000 | [diff] [blame] | 1144 | #include <process.h> |
Wes Hardaker | bf934d9 | 2010-12-01 01:06:05 +0000 | [diff] [blame] | 1145 | #include <net-snmp/library/snmp_assert.h> |
Bart Van Assche | 3937cf1 | 2010-10-18 08:52:00 +0000 | [diff] [blame] | 1146 | |
| 1147 | static unsigned s_threadid; |
| 1148 | HANDLE s_thread_handle; |
| 1149 | |
| 1150 | static unsigned __stdcall wait_for_stdin(void* arg) |
| 1151 | { |
| 1152 | if (getc(stdin) != EOF) |
| 1153 | netsnmp_running = 0; |
| 1154 | return 0; |
| 1155 | } |
| 1156 | |
Bart Van Assche | 509e978 | 2011-02-14 14:38:00 +0000 | [diff] [blame] | 1157 | static void create_stdin_waiter_thread(void) |
Bart Van Assche | 3937cf1 | 2010-10-18 08:52:00 +0000 | [diff] [blame] | 1158 | { |
Wes Hardaker | bf934d9 | 2010-12-01 01:06:05 +0000 | [diff] [blame] | 1159 | netsnmp_assert(s_thread_handle == 0); |
Bart Van Assche | 3937cf1 | 2010-10-18 08:52:00 +0000 | [diff] [blame] | 1160 | s_thread_handle = (HANDLE)_beginthreadex(0, 0, wait_for_stdin, 0, 0, &s_threadid); |
Wes Hardaker | bf934d9 | 2010-12-01 01:06:05 +0000 | [diff] [blame] | 1161 | netsnmp_assert(s_thread_handle != 0); |
Bart Van Assche | 3937cf1 | 2010-10-18 08:52:00 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Bart Van Assche | 509e978 | 2011-02-14 14:38:00 +0000 | [diff] [blame] | 1164 | static void join_stdin_waiter_thread(void) |
Bart Van Assche | 3937cf1 | 2010-10-18 08:52:00 +0000 | [diff] [blame] | 1165 | { |
| 1166 | int result; |
| 1167 | |
Wes Hardaker | bf934d9 | 2010-12-01 01:06:05 +0000 | [diff] [blame] | 1168 | netsnmp_assert(s_thread_handle != 0); |
Bart Van Assche | 3937cf1 | 2010-10-18 08:52:00 +0000 | [diff] [blame] | 1169 | result = WaitForSingleObject(s_thread_handle, 1000); |
Wes Hardaker | bf934d9 | 2010-12-01 01:06:05 +0000 | [diff] [blame] | 1170 | netsnmp_assert(result != WAIT_TIMEOUT); |
Bart Van Assche | 3937cf1 | 2010-10-18 08:52:00 +0000 | [diff] [blame] | 1171 | CloseHandle(s_thread_handle); |
| 1172 | s_thread_handle = 0; |
| 1173 | } |
| 1174 | #endif |
| 1175 | |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 1176 | /*******************************************************************-o-****** |
| 1177 | * receive |
| 1178 | * |
| 1179 | * Parameters: |
| 1180 | * |
| 1181 | * Returns: |
| 1182 | * 0 On success. |
| 1183 | * -1 System error. |
| 1184 | * |
Bart Van Assche | 675bc13 | 2011-03-10 20:01:33 +0000 | [diff] [blame] | 1185 | * Infinite while-loop which monitors incoming messages for the agent. |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 1186 | * Invoke the established message handlers for incoming messages on a per |
| 1187 | * port basis. Handle timeouts. |
| 1188 | */ |
John Naylon | 1542e43 | 2001-12-19 12:38:20 +0000 | [diff] [blame] | 1189 | static int |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 1190 | receive(void) |
Wes Hardaker | c048523 | 1994-10-13 20:55:30 +0000 | [diff] [blame] | 1191 | { |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1192 | int numfds; |
Dave Shield | 23d9c65 | 2009-04-21 15:33:42 +0000 | [diff] [blame] | 1193 | netsnmp_large_fd_set readfds, writefds, exceptfds; |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1194 | struct timeval timeout, *tvp = &timeout; |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1195 | int count, block, i; |
Dave Shield | be29000 | 1999-12-13 12:43:14 +0000 | [diff] [blame] | 1196 | #ifdef USING_SMUX_MODULE |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1197 | int sd; |
| 1198 | #endif /* USING_SMUX_MODULE */ |
Wes Hardaker | c048523 | 1994-10-13 20:55:30 +0000 | [diff] [blame] | 1199 | |
Dave Shield | 23d9c65 | 2009-04-21 15:33:42 +0000 | [diff] [blame] | 1200 | netsnmp_large_fd_set_init(&readfds, FD_SETSIZE); |
| 1201 | netsnmp_large_fd_set_init(&writefds, FD_SETSIZE); |
| 1202 | netsnmp_large_fd_set_init(&exceptfds, FD_SETSIZE); |
| 1203 | |
Wes Hardaker | a6495ce | 1999-08-30 16:33:41 +0000 | [diff] [blame] | 1204 | /* |
Robert Story | 6fbb105 | 2005-06-22 20:04:36 +0000 | [diff] [blame] | 1205 | * ignore early sighup during startup |
| 1206 | */ |
| 1207 | reconfig = 0; |
| 1208 | |
Bart Van Assche | 3937cf1 | 2010-10-18 08:52:00 +0000 | [diff] [blame] | 1209 | #if defined(WIN32) |
| 1210 | create_stdin_waiter_thread(); |
| 1211 | #endif |
| 1212 | |
Robert Story | 6fbb105 | 2005-06-22 20:04:36 +0000 | [diff] [blame] | 1213 | /* |
Robert Story | 9c1e5f1 | 2004-04-19 14:53:52 +0000 | [diff] [blame] | 1214 | * Loop-forever: execute message handlers for sockets with data |
Wes Hardaker | a6495ce | 1999-08-30 16:33:41 +0000 | [diff] [blame] | 1215 | */ |
Robert Story | 7627ec7 | 2005-11-15 16:37:58 +0000 | [diff] [blame] | 1216 | while (netsnmp_running) { |
Wes Hardaker | a6495ce | 1999-08-30 16:33:41 +0000 | [diff] [blame] | 1217 | if (reconfig) { |
Wes Hardaker | c66bc25 | 2006-10-18 01:46:27 +0000 | [diff] [blame] | 1218 | #if HAVE_SIGHOLD |
| 1219 | sighold(SIGHUP); |
| 1220 | #endif |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1221 | reconfig = 0; |
| 1222 | snmp_log(LOG_INFO, "Reconfiguring daemon\n"); |
John Naylon | 7c5170c | 2002-08-22 11:11:57 +0000 | [diff] [blame] | 1223 | /* Stop and restart logging. This allows logfiles to be |
| 1224 | rotated etc. */ |
Robert Story | 60371e8 | 2005-06-20 20:38:41 +0000 | [diff] [blame] | 1225 | netsnmp_logging_restart(); |
John Naylon | 7c5170c | 2002-08-22 11:11:57 +0000 | [diff] [blame] | 1226 | snmp_log(LOG_INFO, "NET-SNMP version %s restarted\n", |
| 1227 | netsnmp_get_version()); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1228 | update_config(); |
| 1229 | send_easy_trap(SNMP_TRAP_ENTERPRISESPECIFIC, 3); |
Wes Hardaker | c66bc25 | 2006-10-18 01:46:27 +0000 | [diff] [blame] | 1230 | #if HAVE_SIGHOLD |
| 1231 | sigrelse(SIGHUP); |
| 1232 | #endif |
Wes Hardaker | a6495ce | 1999-08-30 16:33:41 +0000 | [diff] [blame] | 1233 | } |
John Naylon | 01e0188 | 2001-04-04 13:46:57 +0000 | [diff] [blame] | 1234 | |
Robert Story | 9c1e5f1 | 2004-04-19 14:53:52 +0000 | [diff] [blame] | 1235 | /* |
Robert Story | 24fd55f | 2005-11-07 18:41:49 +0000 | [diff] [blame] | 1236 | * default to sleeping for a really long time. INT_MAX |
| 1237 | * should be sufficient (eg we don't care if time_t is |
| 1238 | * a long that's bigger than an int). |
Robert Story | 9c1e5f1 | 2004-04-19 14:53:52 +0000 | [diff] [blame] | 1239 | */ |
Robert Story | d0ee22c | 2004-10-06 21:11:23 +0000 | [diff] [blame] | 1240 | tvp = &timeout; |
Robert Story | 24fd55f | 2005-11-07 18:41:49 +0000 | [diff] [blame] | 1241 | tvp->tv_sec = INT_MAX; |
Robert Story | 9c1e5f1 | 2004-04-19 14:53:52 +0000 | [diff] [blame] | 1242 | tvp->tv_usec = 0; |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 1243 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1244 | numfds = 0; |
Dave Shield | 23d9c65 | 2009-04-21 15:33:42 +0000 | [diff] [blame] | 1245 | NETSNMP_LARGE_FD_ZERO(&readfds); |
| 1246 | NETSNMP_LARGE_FD_ZERO(&writefds); |
| 1247 | NETSNMP_LARGE_FD_ZERO(&exceptfds); |
Wes Hardaker | a6495ce | 1999-08-30 16:33:41 +0000 | [diff] [blame] | 1248 | block = 0; |
Dave Shield | 23d9c65 | 2009-04-21 15:33:42 +0000 | [diff] [blame] | 1249 | snmp_select_info2(&numfds, &readfds, tvp, &block); |
John Naylon | 32ae177 | 2002-12-18 13:19:07 +0000 | [diff] [blame] | 1250 | if (block == 1) { |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1251 | tvp = NULL; /* block without timeout */ |
John Naylon | 32ae177 | 2002-12-18 13:19:07 +0000 | [diff] [blame] | 1252 | } |
Dave Shield | be29000 | 1999-12-13 12:43:14 +0000 | [diff] [blame] | 1253 | |
| 1254 | #ifdef USING_SMUX_MODULE |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1255 | if (smux_listen_sd >= 0) { |
Magnus Fromreide | 018c1e9 | 2009-04-22 21:03:21 +0000 | [diff] [blame] | 1256 | NETSNMP_LARGE_FD_SET(smux_listen_sd, &readfds); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1257 | numfds = |
| 1258 | smux_listen_sd >= numfds ? smux_listen_sd + 1 : numfds; |
Dave Shield | c3fb5c8 | 2007-03-22 19:12:22 +0000 | [diff] [blame] | 1259 | |
| 1260 | for (i = 0; i < smux_snmp_select_list_get_length(); i++) { |
| 1261 | sd = smux_snmp_select_list_get_SD_from_List(i); |
| 1262 | if (sd != 0) |
| 1263 | { |
Magnus Fromreide | 018c1e9 | 2009-04-22 21:03:21 +0000 | [diff] [blame] | 1264 | NETSNMP_LARGE_FD_SET(sd, &readfds); |
Dave Shield | c3fb5c8 | 2007-03-22 19:12:22 +0000 | [diff] [blame] | 1265 | numfds = sd >= numfds ? sd + 1 : numfds; |
| 1266 | } |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1267 | } |
| 1268 | } |
| 1269 | #endif /* USING_SMUX_MODULE */ |
Dave Shield | be29000 | 1999-12-13 12:43:14 +0000 | [diff] [blame] | 1270 | |
Wes Hardaker | 3b14c59 | 2011-02-23 00:01:18 +0000 | [diff] [blame] | 1271 | #ifndef NETSNMP_FEATURE_REMOVE_FD_EVENT_MANAGER |
Dave Shield | 23d9c65 | 2009-04-21 15:33:42 +0000 | [diff] [blame] | 1272 | netsnmp_external_event_info2(&numfds, &readfds, &writefds, &exceptfds); |
Wes Hardaker | 3b14c59 | 2011-02-23 00:01:18 +0000 | [diff] [blame] | 1273 | #endif /* NETSNMP_FEATURE_REMOVE_FD_EVENT_MANAGER */ |
Wes Hardaker | b469d77 | 2001-02-09 18:12:51 +0000 | [diff] [blame] | 1274 | |
John Naylon | 9838215 | 2002-10-11 13:40:10 +0000 | [diff] [blame] | 1275 | reselect: |
Magnus Fromreide | d8d6078 | 2013-11-19 08:06:34 +0100 | [diff] [blame] | 1276 | #ifndef NETSNMP_FEATURE_REMOVE_REGISTER_SIGNAL |
Jan Safranek | 11bd6e0 | 2011-07-27 14:45:09 +0200 | [diff] [blame] | 1277 | for (i = 0; i < NUM_EXTERNAL_SIGS; i++) { |
| 1278 | if (external_signal_scheduled[i]) { |
| 1279 | external_signal_scheduled[i]--; |
| 1280 | external_signal_handler[i](i); |
| 1281 | } |
| 1282 | } |
Magnus Fromreide | d8d6078 | 2013-11-19 08:06:34 +0100 | [diff] [blame] | 1283 | #endif /* NETSNMP_FEATURE_REMOVE_REGISTER_SIGNAL */ |
Jan Safranek | 11bd6e0 | 2011-07-27 14:45:09 +0200 | [diff] [blame] | 1284 | |
Robert Story | 6bac8a0 | 2002-10-16 21:00:57 +0000 | [diff] [blame] | 1285 | DEBUGMSGTL(("snmpd/select", "select( numfds=%d, ..., tvp=%p)\n", |
| 1286 | numfds, tvp)); |
Bart Van Assche | b4a1950 | 2012-02-20 16:58:47 +0100 | [diff] [blame] | 1287 | if (tvp) |
Bart Van Assche | 9484d83 | 2012-02-24 14:00:32 +0100 | [diff] [blame] | 1288 | DEBUGMSGTL(("timer", "tvp %ld.%ld\n", (long) tvp->tv_sec, |
Bart Van Assche | b4a1950 | 2012-02-20 16:58:47 +0100 | [diff] [blame] | 1289 | (long) tvp->tv_usec)); |
Dave Shield | fe3d460 | 2011-03-14 09:31:57 +0000 | [diff] [blame] | 1290 | count = netsnmp_large_fd_set_select(numfds, &readfds, &writefds, &exceptfds, |
Bart Van Assche | a6967c9 | 2011-03-10 19:48:43 +0000 | [diff] [blame] | 1291 | tvp); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1292 | DEBUGMSGTL(("snmpd/select", "returned, count = %d\n", count)); |
Wes Hardaker | 5415a52 | 2000-03-10 18:52:37 +0000 | [diff] [blame] | 1293 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1294 | if (count > 0) { |
Wes Hardaker | 5415a52 | 2000-03-10 18:52:37 +0000 | [diff] [blame] | 1295 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1296 | #ifdef USING_SMUX_MODULE |
| 1297 | /* |
| 1298 | * handle the SMUX sd's |
| 1299 | */ |
| 1300 | if (smux_listen_sd >= 0) { |
Dave Shield | c3fb5c8 | 2007-03-22 19:12:22 +0000 | [diff] [blame] | 1301 | for (i = 0; i < smux_snmp_select_list_get_length(); i++) { |
| 1302 | sd = smux_snmp_select_list_get_SD_from_List(i); |
Magnus Fromreide | 018c1e9 | 2009-04-22 21:03:21 +0000 | [diff] [blame] | 1303 | if (NETSNMP_LARGE_FD_ISSET(sd, &readfds)) { |
Dave Shield | c3fb5c8 | 2007-03-22 19:12:22 +0000 | [diff] [blame] | 1304 | if (smux_process(sd) < 0) { |
| 1305 | smux_snmp_select_list_del(sd); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1306 | } |
| 1307 | } |
| 1308 | } |
| 1309 | /* |
| 1310 | * new connection |
| 1311 | */ |
Magnus Fromreide | 018c1e9 | 2009-04-22 21:03:21 +0000 | [diff] [blame] | 1312 | if (NETSNMP_LARGE_FD_ISSET(smux_listen_sd, &readfds)) { |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1313 | if ((sd = smux_accept(smux_listen_sd)) >= 0) { |
Dave Shield | c3fb5c8 | 2007-03-22 19:12:22 +0000 | [diff] [blame] | 1314 | smux_snmp_select_list_add(sd); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1315 | } |
| 1316 | } |
| 1317 | } |
Dave Shield | c3fb5c8 | 2007-03-22 19:12:22 +0000 | [diff] [blame] | 1318 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1319 | #endif /* USING_SMUX_MODULE */ |
Wes Hardaker | 3b14c59 | 2011-02-23 00:01:18 +0000 | [diff] [blame] | 1320 | |
| 1321 | #ifndef NETSNMP_FEATURE_REMOVE_FD_EVENT_MANAGER |
Dave Shield | 23d9c65 | 2009-04-21 15:33:42 +0000 | [diff] [blame] | 1322 | netsnmp_dispatch_external_events2(&count, &readfds, |
| 1323 | &writefds, &exceptfds); |
Wes Hardaker | 3b14c59 | 2011-02-23 00:01:18 +0000 | [diff] [blame] | 1324 | #endif /* NETSNMP_FEATURE_REMOVE_FD_EVENT_MANAGER */ |
| 1325 | |
Dave Shield | 2decc2f | 2005-04-29 16:12:33 +0000 | [diff] [blame] | 1326 | /* If there are still events leftover, process them */ |
| 1327 | if (count > 0) { |
Dave Shield | 23d9c65 | 2009-04-21 15:33:42 +0000 | [diff] [blame] | 1328 | snmp_read2(&readfds); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1329 | } |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1330 | } else |
| 1331 | switch (count) { |
| 1332 | case 0: |
Wes Hardaker | a6495ce | 1999-08-30 16:33:41 +0000 | [diff] [blame] | 1333 | snmp_timeout(); |
| 1334 | break; |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1335 | case -1: |
Robert Story | 19617e3 | 2004-03-02 04:52:45 +0000 | [diff] [blame] | 1336 | DEBUGMSGTL(("snmpd/select", " errno = %d\n", errno)); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1337 | if (errno == EINTR) { |
Robert Story | 6bac8a0 | 2002-10-16 21:00:57 +0000 | [diff] [blame] | 1338 | /* |
| 1339 | * likely that we got a signal. Check our special signal |
| 1340 | * flags before retrying select. |
| 1341 | */ |
Robert Story | 7627ec7 | 2005-11-15 16:37:58 +0000 | [diff] [blame] | 1342 | if (netsnmp_running && !reconfig) { |
Robert Story | 6bac8a0 | 2002-10-16 21:00:57 +0000 | [diff] [blame] | 1343 | goto reselect; |
John Naylon | 32ae177 | 2002-12-18 13:19:07 +0000 | [diff] [blame] | 1344 | } |
Robert Story | ecf9c8e | 2002-10-16 21:19:52 +0000 | [diff] [blame] | 1345 | continue; |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1346 | } else { |
Wes Hardaker | a6495ce | 1999-08-30 16:33:41 +0000 | [diff] [blame] | 1347 | snmp_log_perror("select"); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1348 | } |
| 1349 | return -1; |
| 1350 | default: |
| 1351 | snmp_log(LOG_ERR, "select returned %d\n", count); |
| 1352 | return -1; |
| 1353 | } /* endif -- count>0 */ |
Niels Baggesen | 2ddc7eb | 1999-08-18 09:40:48 +0000 | [diff] [blame] | 1354 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1355 | /* |
Robert Story | acb5952 | 2010-08-17 18:31:31 +0000 | [diff] [blame] | 1356 | * see if persistent store needs to be saved |
| 1357 | */ |
| 1358 | snmp_store_if_needed(); |
| 1359 | |
| 1360 | /* |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1361 | * run requested alarms |
| 1362 | */ |
Wes Hardaker | 6d05cac | 1999-11-19 22:20:25 +0000 | [diff] [blame] | 1363 | run_alarms(); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1364 | |
Wes Hardaker | a7482e5 | 2002-03-27 07:23:12 +0000 | [diff] [blame] | 1365 | netsnmp_check_outstanding_agent_requests(); |
Wes Hardaker | 36e94be | 2001-11-22 02:56:52 +0000 | [diff] [blame] | 1366 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1367 | } /* endwhile */ |
Niels Baggesen | 448c1c3 | 1999-08-19 21:09:25 +0000 | [diff] [blame] | 1368 | |
Dave Shield | 23d9c65 | 2009-04-21 15:33:42 +0000 | [diff] [blame] | 1369 | netsnmp_large_fd_set_cleanup(&readfds); |
| 1370 | netsnmp_large_fd_set_cleanup(&writefds); |
| 1371 | netsnmp_large_fd_set_cleanup(&exceptfds); |
| 1372 | |
Bart Van Assche | 3937cf1 | 2010-10-18 08:52:00 +0000 | [diff] [blame] | 1373 | #if defined(WIN32) |
| 1374 | join_stdin_waiter_thread(); |
| 1375 | #endif |
| 1376 | |
John Naylon | 32ae177 | 2002-12-18 13:19:07 +0000 | [diff] [blame] | 1377 | snmp_log(LOG_INFO, "Received TERM or STOP signal... shutting down...\n"); |
Wes Hardaker | a6495ce | 1999-08-30 16:33:41 +0000 | [diff] [blame] | 1378 | return 0; |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 1379 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1380 | } /* end receive() */ |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 1381 | |
| 1382 | |
| 1383 | |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 1384 | /*******************************************************************-o-****** |
| 1385 | * snmp_input |
| 1386 | * |
| 1387 | * Parameters: |
| 1388 | * op |
| 1389 | * *session |
| 1390 | * requid |
| 1391 | * *pdu |
| 1392 | * *magic |
| 1393 | * |
| 1394 | * Returns: |
| 1395 | * 1 On success -OR- |
| 1396 | * Passes through Return from alarmGetResponse() when |
| 1397 | * USING_V2PARTY_ALARM_MODULE is defined. |
| 1398 | * |
| 1399 | * Call-back function to manage responses to traps (informs) and alarms. |
| 1400 | * Not used by the agent to process other Response PDUs. |
| 1401 | */ |
Wes Hardaker | c048523 | 1994-10-13 20:55:30 +0000 | [diff] [blame] | 1402 | int |
Wes Hardaker | 40b2bf1 | 1999-01-18 18:47:36 +0000 | [diff] [blame] | 1403 | snmp_input(int op, |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1404 | netsnmp_session * session, |
| 1405 | int reqid, netsnmp_pdu *pdu, void *magic) |
Wes Hardaker | c048523 | 1994-10-13 20:55:30 +0000 | [diff] [blame] | 1406 | { |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1407 | struct get_req_state *state = (struct get_req_state *) magic; |
| 1408 | |
Wes Hardaker | 73925b8 | 2002-03-09 00:28:07 +0000 | [diff] [blame] | 1409 | if (op == NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE) { |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1410 | if (pdu->command == SNMP_MSG_GET) { |
| 1411 | if (state->type == EVENT_GET_REQ) { |
| 1412 | /* |
| 1413 | * this is just the ack to our inform pdu |
| 1414 | */ |
| 1415 | return 1; |
| 1416 | } |
| 1417 | } |
| 1418 | } else if (op == NETSNMP_CALLBACK_OP_TIMED_OUT) { |
| 1419 | if (state->type == ALARM_GET_REQ) { |
| 1420 | /* |
| 1421 | * Need a mechanism to replace obsolete SNMPv2p alarm |
| 1422 | */ |
| 1423 | } |
Wes Hardaker | c048523 | 1994-10-13 20:55:30 +0000 | [diff] [blame] | 1424 | } |
| 1425 | return 1; |
Wes Hardaker | 87bed83 | 1999-04-06 22:13:53 +0000 | [diff] [blame] | 1426 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1427 | } /* end snmp_input() */ |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 1428 | |
| 1429 | |
| 1430 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1431 | /* |
| 1432 | * Windows Service Related functions |
| 1433 | */ |
Michael Slifcak | 0ef0164 | 2004-02-09 12:06:24 +0000 | [diff] [blame] | 1434 | #ifdef WIN32SERVICE |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 1435 | /************************************************************ |
| 1436 | * main function for Windows |
| 1437 | * Parse command line arguments for startup options, |
| 1438 | * to start as service or console mode application in windows. |
Michael Slifcak | 8784cbc | 2004-09-09 10:22:23 +0000 | [diff] [blame] | 1439 | * Invokes appropriate startup functions depending on the |
| 1440 | * parameters passed |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 1441 | *************************************************************/ |
| 1442 | int |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1443 | __cdecl |
| 1444 | _tmain(int argc, TCHAR * argv[]) |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 1445 | { |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1446 | /* |
| 1447 | * Define Service Name and Description, which appears in windows SCM |
| 1448 | */ |
Alex Burger | 327de41 | 2005-11-10 02:33:17 +0000 | [diff] [blame] | 1449 | LPCTSTR lpszServiceName = app_name_long; /* Service Registry Name */ |
Michael Slifcak | b9cc54c | 2004-01-29 20:32:27 +0000 | [diff] [blame] | 1450 | LPCTSTR lpszServiceDisplayName = _T("Net-SNMP Agent"); /* Display Name */ |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1451 | LPCTSTR lpszServiceDescription = |
Michael Slifcak | 78d401c | 2004-02-27 12:50:16 +0000 | [diff] [blame] | 1452 | #ifdef IFDESCR |
Michael Slifcak | b9cc54c | 2004-01-29 20:32:27 +0000 | [diff] [blame] | 1453 | _T("SNMPv2c / SNMPv3 command responder from Net-SNMP. Supports MIB objects for IP,ICMP,TCP,UDP, and network interface sub-layers."); |
| 1454 | #else |
| 1455 | _T("SNMPv2c / SNMPv3 command responder from Net-SNMP"); |
| 1456 | #endif |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1457 | InputParams InputOptions; |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 1458 | |
| 1459 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1460 | int nRunType = RUN_AS_CONSOLE; |
Michael Slifcak | 73c0c44 | 2004-09-22 18:20:16 +0000 | [diff] [blame] | 1461 | int quiet = 0; |
| 1462 | |
Bart Van Assche | 13b46d0 | 2010-10-28 08:44:08 +0000 | [diff] [blame] | 1463 | #if 0 |
| 1464 | _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF /*| _CRTDBG_CHECK_ALWAYS_DF*/); |
| 1465 | #endif |
| 1466 | |
Michael Slifcak | 73c0c44 | 2004-09-22 18:20:16 +0000 | [diff] [blame] | 1467 | nRunType = ParseCmdLineForServiceOption(argc, argv, &quiet); |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 1468 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1469 | switch (nRunType) { |
| 1470 | case REGISTER_SERVICE: |
| 1471 | /* |
| 1472 | * Register As service |
| 1473 | */ |
| 1474 | InputOptions.Argc = argc; |
| 1475 | InputOptions.Argv = argv; |
Michael Slifcak | 73c0c44 | 2004-09-22 18:20:16 +0000 | [diff] [blame] | 1476 | exit (RegisterService(lpszServiceName, |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1477 | lpszServiceDisplayName, |
Michael Slifcak | 73c0c44 | 2004-09-22 18:20:16 +0000 | [diff] [blame] | 1478 | lpszServiceDescription, &InputOptions, quiet)); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1479 | break; |
| 1480 | case UN_REGISTER_SERVICE: |
| 1481 | /* |
| 1482 | * Unregister service |
| 1483 | */ |
Michael Slifcak | 73c0c44 | 2004-09-22 18:20:16 +0000 | [diff] [blame] | 1484 | exit (UnregisterService(lpszServiceName, quiet)); |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1485 | break; |
| 1486 | case RUN_AS_SERVICE: |
| 1487 | /* |
| 1488 | * Run as service |
| 1489 | */ |
| 1490 | /* |
| 1491 | * Register Stop Function |
| 1492 | */ |
| 1493 | RegisterStopFunction(StopSnmpAgent); |
| 1494 | return RunAsService(SnmpDaemonMain); |
| 1495 | break; |
| 1496 | default: |
| 1497 | /* |
Michael Slifcak | 8784cbc | 2004-09-09 10:22:23 +0000 | [diff] [blame] | 1498 | * Run in console mode |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1499 | */ |
| 1500 | return SnmpDaemonMain(argc, argv); |
| 1501 | break; |
| 1502 | } |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | /* |
Michael Slifcak | 8784cbc | 2004-09-09 10:22:23 +0000 | [diff] [blame] | 1506 | * To stop Snmp Agent daemon |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 1507 | * This portion is still not working |
| 1508 | */ |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1509 | void |
| 1510 | StopSnmpAgent(void) |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 1511 | { |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1512 | /* |
| 1513 | * Shut Down Agent |
| 1514 | */ |
| 1515 | SnmpdShutDown(1); |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 1516 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1517 | /* |
| 1518 | * Wait till agent is completely stopped |
| 1519 | */ |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 1520 | |
Wes Hardaker | 6d1f602 | 2002-04-20 07:08:20 +0000 | [diff] [blame] | 1521 | while (agent_status != AGENT_STOPPED) { |
| 1522 | Sleep(100); |
| 1523 | } |
Dave Shield | 8b2adde | 2002-02-15 15:28:29 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
Michael Slifcak | 8784cbc | 2004-09-09 10:22:23 +0000 | [diff] [blame] | 1526 | #endif /*WIN32SERVICE*/ |