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