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