36
|
1 /*
|
|
2
|
|
3 Copyright (c) 2007 Carl Byington - 510 Software Group, released under
|
|
4 the GPL version 3 or any later version at your choice available at
|
|
5 http://www.gnu.org/licenses/gpl-3.0.txt
|
|
6
|
|
7 */
|
|
8
|
1
|
9
|
5
|
10 // debug levels:
|
|
11 // 4 - show syslog lines that match regex
|
20
|
12 // 3 - show addresses being dropped/released from the filter
|
10
|
13 // 2 - show files open/close
|
5
|
14 // 1 - show config files loading
|
1
|
15
|
9
|
16 #include "includes.h"
|
1
|
17 #include <iostream>
|
|
18 #include <cstdlib>
|
|
19 #include <errno.h>
|
|
20 #include <sysexits.h>
|
|
21 #include <pthread.h>
|
|
22 #include <syslog.h>
|
36
|
23 #include <sys/wait.h> /* header for waitpid() and various macros */
|
|
24 #include <signal.h> /* header for signal functions */
|
1
|
25
|
2
|
26 extern "C" {
|
36
|
27 void sigchld(int sig);
|
|
28 void sigterm(int sig);
|
2
|
29 }
|
1
|
30 int debug_syslog = 0;
|
|
31 bool syslog_opened = false;
|
36
|
32 bool use_syslog = true; // false to printf
|
|
33 bool loader_run = true; // used to stop the config loader thread
|
|
34 CONFIG *config = NULL; // protected by the config_mutex
|
|
35 int generation = 0; // protected by the config_mutex
|
|
36 const int maxlen = 1000; // used for snprintf buffers
|
1
|
37
|
|
38 pthread_mutex_t config_mutex;
|
|
39 pthread_mutex_t syslog_mutex;
|
|
40
|
|
41
|
|
42 ////////////////////////////////////////////////
|
|
43 // syslog a message
|
|
44 //
|
|
45 void my_syslog(char *text) {
|
36
|
46 if (use_syslog) {
|
|
47 pthread_mutex_lock(&syslog_mutex);
|
|
48 if (!syslog_opened) {
|
|
49 openlog("syslog2iptables", LOG_PID, LOG_AUTHPRIV);
|
|
50 syslog_opened = true;
|
|
51 }
|
|
52 syslog(LOG_NOTICE, "%s", text);
|
|
53 pthread_mutex_unlock(&syslog_mutex);
|
|
54 }
|
|
55 else {
|
|
56 printf("%s\n", text);
|
|
57 }
|
1
|
58 }
|
|
59
|
|
60 ////////////////////////////////////////////////
|
36
|
61 // reload the config
|
1
|
62 //
|
|
63 CONFIG* new_conf();
|
|
64 CONFIG* new_conf() {
|
36
|
65 CONFIG *newc = new CONFIG;
|
|
66 pthread_mutex_lock(&config_mutex);
|
|
67 newc->generation = generation++;
|
|
68 pthread_mutex_unlock(&config_mutex);
|
|
69 if (debug_syslog) {
|
|
70 char buf[maxlen];
|
|
71 snprintf(buf, sizeof(buf), "loading configuration generation %d", newc->generation);
|
|
72 my_syslog(buf);
|
|
73 }
|
|
74 if (load_conf(*newc, "syslog2iptables.conf")) {
|
|
75 newc->load_time = time(NULL);
|
|
76 return newc;
|
|
77 }
|
|
78 delete newc;
|
|
79 return NULL;
|
1
|
80 }
|
|
81
|
|
82
|
|
83 ////////////////////////////////////////////////
|
36
|
84 // thread to watch the old config files for changes
|
|
85 // and reload when needed.
|
1
|
86 //
|
|
87 void* config_loader(void *arg);
|
|
88 void* config_loader(void *arg) {
|
36
|
89 typedef set<CONFIG *> configp_set;
|
|
90 while (loader_run) {
|
|
91 sleep(180); // look for modifications every 3 minutes
|
|
92 if (!loader_run) break;
|
|
93 CONFIG &dc = *config;
|
|
94 time_t then = dc.load_time;
|
|
95 struct stat st;
|
|
96 bool reload = false;
|
|
97 for (string_set::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) {
|
|
98 char *fn = *i;
|
|
99 if (stat(fn, &st)) reload = true; // file disappeared
|
|
100 else if (st.st_mtime > then) reload = true; // file modified
|
|
101 if (reload) break;
|
|
102 }
|
|
103 if (reload) {
|
|
104 CONFIG *newc = new_conf();
|
|
105 if (newc) {
|
|
106 // replace the global config pointer
|
|
107 pthread_mutex_lock(&config_mutex);
|
|
108 config = newc;
|
|
109 pthread_mutex_unlock(&config_mutex);
|
|
110 }
|
|
111 else {
|
|
112 // failed to load new config
|
|
113 my_syslog("failed to load new configuration");
|
|
114 system("echo 'failed to load new /etc/syslog2iptables.conf' | mail -s 'error in /etc/syslog2iptables.conf' root");
|
|
115 // update the load time on the current config to prevent complaining every 3 minutes
|
|
116 dc.load_time = time(NULL);
|
|
117 }
|
|
118 }
|
|
119 }
|
|
120 return NULL;
|
1
|
121 }
|
|
122
|
|
123
|
|
124 ////////////////////////////////////////////////
|
36
|
125 // The signal handler function for child process terminations,
|
|
126 // called when a child terminates.
|
1
|
127 //
|
36
|
128 void sigchld(int sig)
|
1
|
129 {
|
36
|
130 int status;
|
|
131 /* Wait for any child without blocking */
|
|
132 while (waitpid(-1, &status, WNOHANG) > 0) {
|
|
133 // ignore child exit status, we only do this to cleanup zombies
|
|
134 }
|
|
135 }
|
|
136
|
|
137
|
|
138 ////////////////////////////////////////////////
|
|
139 // The termination signal handler function, called to
|
|
140 // request termination of this process.
|
|
141 //
|
|
142 void sigterm(int sig)
|
|
143 {
|
|
144 loader_run = false;
|
|
145 signal(sig, SIG_DFL); // quit on repeated signals
|
1
|
146 }
|
|
147
|
|
148
|
|
149 void usage(char *prog);
|
|
150 void usage(char *prog)
|
|
151 {
|
36
|
152 fprintf(stderr, "Usage: %s [-d [level]] [-c]\n", prog);
|
|
153 fprintf(stderr, "-c will load and dump the config to stdout\n");
|
|
154 fprintf(stderr, "-d will set the syslog message level, currently 0 to 3\n");
|
5
|
155 }
|
|
156
|
|
157
|
|
158 void worker();
|
|
159 void worker()
|
|
160 {
|
36
|
161 time_t t = time(NULL);
|
|
162 CONFIG *c;
|
|
163 pthread_mutex_lock(&config_mutex);
|
|
164 c = config;
|
|
165 c->reference_count++;
|
|
166 pthread_mutex_unlock(&config_mutex);
|
|
167 while (loader_run) {
|
|
168 if (c != config) {
|
|
169 pthread_mutex_lock(&config_mutex);
|
|
170 CONFIG *old = c; old->reference_count--;
|
|
171 c = config; c->reference_count++;
|
|
172 pthread_mutex_unlock(&config_mutex);
|
|
173 if (!old->reference_count) {
|
|
174 if (debug_syslog) {
|
|
175 char buf[maxlen];
|
|
176 snprintf(buf, sizeof(buf), "freeing memory for old configuration generation %d", old->generation);
|
|
177 my_syslog(buf);
|
|
178 }
|
|
179 delete old; // destructor does all the work
|
|
180 }
|
|
181 }
|
|
182 c->read();
|
|
183 c->sleep(2, t);
|
|
184 }
|
|
185 // worker shutting down, free all ip addresses
|
|
186 c->free_all();
|
1
|
187 }
|
|
188
|
|
189
|
|
190 int main(int argc, char *argv[])
|
|
191 {
|
36
|
192 token_init();
|
|
193 bool check = false;
|
|
194 int c;
|
|
195 const char *args = "d:ch";
|
|
196 extern char *optarg;
|
1
|
197
|
36
|
198 // Process command line options
|
|
199 while ((c = getopt(argc, argv, args)) != -1) {
|
|
200 switch (c) {
|
|
201 case 'c':
|
|
202 check = true;
|
|
203 break;
|
1
|
204
|
36
|
205 case 'd':
|
|
206 if (optarg == NULL || *optarg == '\0') debug_syslog = 1;
|
|
207 else debug_syslog = atoi(optarg);
|
|
208 break;
|
5
|
209
|
36
|
210 case 'h':
|
|
211 default:
|
|
212 usage(argv[0]);
|
|
213 exit(EX_USAGE);
|
|
214 }
|
|
215 }
|
1
|
216
|
36
|
217 if (check) {
|
|
218 use_syslog = false;
|
|
219 debug_syslog = 10;
|
|
220 config = new_conf();
|
|
221 if (config) {
|
|
222 config->dump();
|
|
223 delete config;
|
38
|
224 clear_strings(); // for valgrind checking
|
36
|
225 return 0;
|
|
226 }
|
|
227 else {
|
|
228 return 1; // config failed to load
|
|
229 }
|
|
230 }
|
1
|
231
|
36
|
232 // switch to background mode
|
|
233 if (daemon(1,0) < 0) {
|
|
234 fprintf(stderr, "daemon() call failed\n");
|
|
235 exit(EX_UNAVAILABLE);
|
|
236 }
|
1
|
237
|
36
|
238 // write the pid
|
|
239 const char *pidpath = "/var/run/syslog2iptables.pid";
|
|
240 unlink(pidpath);
|
|
241 FILE *f = fopen(pidpath, "w");
|
|
242 if (f) {
|
1
|
243 #ifdef linux
|
36
|
244 // from a comment in the DCC source code:
|
|
245 // Linux threads are broken. Signals given the
|
|
246 // original process are delivered to only the
|
|
247 // thread that happens to have that PID. The
|
|
248 // sendmail libmilter thread that needs to hear
|
|
249 // SIGINT and other signals does not, and that breaks
|
|
250 // scripts that need to stop milters.
|
|
251 // However, signaling the process group works.
|
|
252 fprintf(f, "-%d\n", (u_int)getpgrp());
|
1
|
253 #else
|
36
|
254 fprintf(f, "%d\n", (u_int)getpid());
|
1
|
255 #endif
|
36
|
256 fclose(f);
|
|
257 }
|
1
|
258
|
36
|
259 // setup signal handler for termination signals
|
|
260 signal(SIGHUP, sigterm);
|
|
261 signal(SIGTERM, sigterm);
|
|
262 signal(SIGINT, sigterm);
|
1
|
263
|
36
|
264 // initialize the thread sync objects
|
|
265 pthread_mutex_init(&config_mutex, 0);
|
|
266 pthread_mutex_init(&syslog_mutex, 0);
|
|
267
|
|
268 // load the initial config
|
|
269 config = new_conf();
|
|
270 if (!config) {
|
|
271 my_syslog("failed to load initial configuration, quitting");
|
|
272 exit(1);
|
|
273 }
|
1
|
274
|
36
|
275 // setup sigchld handler to prevent zombies
|
|
276 struct sigaction act;
|
|
277 act.sa_handler = sigchld; // Assign sig_chld as our SIGCHLD handler
|
|
278 sigemptyset(&act.sa_mask); // We don't want to block any other signals in this example
|
|
279 act.sa_flags = SA_NOCLDSTOP; // only want children that have terminated
|
|
280 if (sigaction(SIGCHLD, &act, NULL) < 0) {
|
|
281 my_syslog("failed to setup SIGCHLD handler");
|
|
282 exit(1);
|
|
283 }
|
2
|
284
|
36
|
285 // only create threads after the fork() in daemon
|
|
286 pthread_t tid;
|
|
287 if (pthread_create(&tid, 0, config_loader, 0))
|
|
288 my_syslog("failed to create config loader thread");
|
|
289 if (pthread_detach(tid))
|
|
290 my_syslog("failed to detach config loader thread");
|
1
|
291
|
36
|
292 worker();
|
40
|
293 if (config) delete config; // for valgrind checking
|
39
|
294 clear_strings(); // for valgrind checking
|
5
|
295
|
36
|
296 return EXIT_SUCCESS;
|
1
|
297 }
|