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