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