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