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