annotate src/syslog2iptables.cpp @ 1:551433a01cab

initial coding
author carl
date Wed, 23 Nov 2005 19:29:14 -0800
parents
children 6e88da080f08
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
551433a01cab initial coding
carl
parents:
diff changeset
1 /***************************************************************************
551433a01cab initial coding
carl
parents:
diff changeset
2 * Copyright (C) 2005 by 510 Software Group *
551433a01cab initial coding
carl
parents:
diff changeset
3 * *
551433a01cab initial coding
carl
parents:
diff changeset
4 * *
551433a01cab initial coding
carl
parents:
diff changeset
5 * This program is free software; you can redistribute it and/or modify *
551433a01cab initial coding
carl
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by *
551433a01cab initial coding
carl
parents:
diff changeset
7 * the Free Software Foundation; either version 2 of the License, or *
551433a01cab initial coding
carl
parents:
diff changeset
8 * (at your option) any later version. *
551433a01cab initial coding
carl
parents:
diff changeset
9 * *
551433a01cab initial coding
carl
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful, *
551433a01cab initial coding
carl
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
551433a01cab initial coding
carl
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
551433a01cab initial coding
carl
parents:
diff changeset
13 * GNU General Public License for more details. *
551433a01cab initial coding
carl
parents:
diff changeset
14 * *
551433a01cab initial coding
carl
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License *
551433a01cab initial coding
carl
parents:
diff changeset
16 * along with this program; if not, write to the *
551433a01cab initial coding
carl
parents:
diff changeset
17 * Free Software Foundation, Inc., *
551433a01cab initial coding
carl
parents:
diff changeset
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
551433a01cab initial coding
carl
parents:
diff changeset
19 ***************************************************************************/
551433a01cab initial coding
carl
parents:
diff changeset
20
551433a01cab initial coding
carl
parents:
diff changeset
21
551433a01cab initial coding
carl
parents:
diff changeset
22 #ifdef HAVE_CONFIG_H
551433a01cab initial coding
carl
parents:
diff changeset
23 #include <config.h>
551433a01cab initial coding
carl
parents:
diff changeset
24 #endif
551433a01cab initial coding
carl
parents:
diff changeset
25
551433a01cab initial coding
carl
parents:
diff changeset
26 #include <iostream>
551433a01cab initial coding
carl
parents:
diff changeset
27 #include <cstdlib>
551433a01cab initial coding
carl
parents:
diff changeset
28 #include <errno.h>
551433a01cab initial coding
carl
parents:
diff changeset
29 #include <sysexits.h>
551433a01cab initial coding
carl
parents:
diff changeset
30 #include <unistd.h>
551433a01cab initial coding
carl
parents:
diff changeset
31 #include <pthread.h>
551433a01cab initial coding
carl
parents:
diff changeset
32 #include <syslog.h>
551433a01cab initial coding
carl
parents:
diff changeset
33 #include <sys/types.h>
551433a01cab initial coding
carl
parents:
diff changeset
34 #include <sys/stat.h>
551433a01cab initial coding
carl
parents:
diff changeset
35 #include <sys/wait.h> /* header for waitpid() and various macros */
551433a01cab initial coding
carl
parents:
diff changeset
36 #include <signal.h> /* header for signal functions */
551433a01cab initial coding
carl
parents:
diff changeset
37 #include "includes.h"
551433a01cab initial coding
carl
parents:
diff changeset
38
551433a01cab initial coding
carl
parents:
diff changeset
39 int debug_syslog = 0;
551433a01cab initial coding
carl
parents:
diff changeset
40 bool syslog_opened = false;
551433a01cab initial coding
carl
parents:
diff changeset
41 bool use_syslog = true; // false to printf
551433a01cab initial coding
carl
parents:
diff changeset
42 bool loader_run = true; // used to stop the config loader thread
551433a01cab initial coding
carl
parents:
diff changeset
43 CONFIG *config = NULL; // protected by the config_mutex
551433a01cab initial coding
carl
parents:
diff changeset
44 int generation = 0; // protected by the config_mutex
551433a01cab initial coding
carl
parents:
diff changeset
45 const int maxlen = 1000; // used for snprintf buffers
551433a01cab initial coding
carl
parents:
diff changeset
46
551433a01cab initial coding
carl
parents:
diff changeset
47 pthread_mutex_t config_mutex;
551433a01cab initial coding
carl
parents:
diff changeset
48 pthread_mutex_t syslog_mutex;
551433a01cab initial coding
carl
parents:
diff changeset
49
551433a01cab initial coding
carl
parents:
diff changeset
50
551433a01cab initial coding
carl
parents:
diff changeset
51 ////////////////////////////////////////////////
551433a01cab initial coding
carl
parents:
diff changeset
52 // syslog a message
551433a01cab initial coding
carl
parents:
diff changeset
53 //
551433a01cab initial coding
carl
parents:
diff changeset
54 void my_syslog(char *text) {
551433a01cab initial coding
carl
parents:
diff changeset
55 if (use_syslog) {
551433a01cab initial coding
carl
parents:
diff changeset
56 pthread_mutex_lock(&syslog_mutex);
551433a01cab initial coding
carl
parents:
diff changeset
57 if (!syslog_opened) {
551433a01cab initial coding
carl
parents:
diff changeset
58 openlog("syslog2iptables", LOG_PID, LOG_MAIL);
551433a01cab initial coding
carl
parents:
diff changeset
59 syslog_opened = true;
551433a01cab initial coding
carl
parents:
diff changeset
60 }
551433a01cab initial coding
carl
parents:
diff changeset
61 syslog(LOG_NOTICE, "%s", text);
551433a01cab initial coding
carl
parents:
diff changeset
62 pthread_mutex_unlock(&syslog_mutex);
551433a01cab initial coding
carl
parents:
diff changeset
63 }
551433a01cab initial coding
carl
parents:
diff changeset
64 else {
551433a01cab initial coding
carl
parents:
diff changeset
65 printf("%s \n", text);
551433a01cab initial coding
carl
parents:
diff changeset
66 }
551433a01cab initial coding
carl
parents:
diff changeset
67 }
551433a01cab initial coding
carl
parents:
diff changeset
68
551433a01cab initial coding
carl
parents:
diff changeset
69 ////////////////////////////////////////////////
551433a01cab initial coding
carl
parents:
diff changeset
70 // reload the config
551433a01cab initial coding
carl
parents:
diff changeset
71 //
551433a01cab initial coding
carl
parents:
diff changeset
72 CONFIG* new_conf();
551433a01cab initial coding
carl
parents:
diff changeset
73 CONFIG* new_conf() {
551433a01cab initial coding
carl
parents:
diff changeset
74 CONFIG *newc = new CONFIG;
551433a01cab initial coding
carl
parents:
diff changeset
75 pthread_mutex_lock(&config_mutex);
551433a01cab initial coding
carl
parents:
diff changeset
76 newc->generation = generation++;
551433a01cab initial coding
carl
parents:
diff changeset
77 pthread_mutex_unlock(&config_mutex);
551433a01cab initial coding
carl
parents:
diff changeset
78 if (debug_syslog) {
551433a01cab initial coding
carl
parents:
diff changeset
79 char buf[maxlen];
551433a01cab initial coding
carl
parents:
diff changeset
80 snprintf(buf, sizeof(buf), "loading configuration generation %d", newc->generation);
551433a01cab initial coding
carl
parents:
diff changeset
81 my_syslog(buf);
551433a01cab initial coding
carl
parents:
diff changeset
82 }
551433a01cab initial coding
carl
parents:
diff changeset
83 if (load_conf(*newc, "syslog2iptables.conf")) {
551433a01cab initial coding
carl
parents:
diff changeset
84 newc->load_time = time(NULL);
551433a01cab initial coding
carl
parents:
diff changeset
85 return newc;
551433a01cab initial coding
carl
parents:
diff changeset
86 }
551433a01cab initial coding
carl
parents:
diff changeset
87 delete newc;
551433a01cab initial coding
carl
parents:
diff changeset
88 return NULL;
551433a01cab initial coding
carl
parents:
diff changeset
89 }
551433a01cab initial coding
carl
parents:
diff changeset
90
551433a01cab initial coding
carl
parents:
diff changeset
91
551433a01cab initial coding
carl
parents:
diff changeset
92 ////////////////////////////////////////////////
551433a01cab initial coding
carl
parents:
diff changeset
93 // thread to watch the old config files for changes
551433a01cab initial coding
carl
parents:
diff changeset
94 // and reload when needed. we also cleanup old
551433a01cab initial coding
carl
parents:
diff changeset
95 // configs whose reference count has gone to zero.
551433a01cab initial coding
carl
parents:
diff changeset
96 //
551433a01cab initial coding
carl
parents:
diff changeset
97 void* config_loader(void *arg);
551433a01cab initial coding
carl
parents:
diff changeset
98 void* config_loader(void *arg) {
551433a01cab initial coding
carl
parents:
diff changeset
99 typedef set<CONFIG *> configp_set;
551433a01cab initial coding
carl
parents:
diff changeset
100 configp_set old_configs;
551433a01cab initial coding
carl
parents:
diff changeset
101 while (loader_run) {
551433a01cab initial coding
carl
parents:
diff changeset
102 sleep(180); // look for modifications every 3 minutes
551433a01cab initial coding
carl
parents:
diff changeset
103 if (!loader_run) break;
551433a01cab initial coding
carl
parents:
diff changeset
104 CONFIG &dc = *config;
551433a01cab initial coding
carl
parents:
diff changeset
105 time_t then = dc.load_time;
551433a01cab initial coding
carl
parents:
diff changeset
106 struct stat st;
551433a01cab initial coding
carl
parents:
diff changeset
107 bool reload = false;
551433a01cab initial coding
carl
parents:
diff changeset
108 for (string_set::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) {
551433a01cab initial coding
carl
parents:
diff changeset
109 char *fn = *i;
551433a01cab initial coding
carl
parents:
diff changeset
110 if (stat(fn, &st)) reload = true; // file disappeared
551433a01cab initial coding
carl
parents:
diff changeset
111 else if (st.st_mtime > then) reload = true; // file modified
551433a01cab initial coding
carl
parents:
diff changeset
112 if (reload) break;
551433a01cab initial coding
carl
parents:
diff changeset
113 }
551433a01cab initial coding
carl
parents:
diff changeset
114 if (reload) {
551433a01cab initial coding
carl
parents:
diff changeset
115 CONFIG *newc = new_conf();
551433a01cab initial coding
carl
parents:
diff changeset
116 if (newc) {
551433a01cab initial coding
carl
parents:
diff changeset
117 // replace the global config pointer
551433a01cab initial coding
carl
parents:
diff changeset
118 pthread_mutex_lock(&config_mutex);
551433a01cab initial coding
carl
parents:
diff changeset
119 CONFIG *old = config;
551433a01cab initial coding
carl
parents:
diff changeset
120 config = newc;
551433a01cab initial coding
carl
parents:
diff changeset
121 pthread_mutex_unlock(&config_mutex);
551433a01cab initial coding
carl
parents:
diff changeset
122 if (old) old_configs.insert(old);
551433a01cab initial coding
carl
parents:
diff changeset
123 }
551433a01cab initial coding
carl
parents:
diff changeset
124 else {
551433a01cab initial coding
carl
parents:
diff changeset
125 // failed to load new config
551433a01cab initial coding
carl
parents:
diff changeset
126 my_syslog("failed to load new configuration");
551433a01cab initial coding
carl
parents:
diff changeset
127 system("echo 'failed to load new /etc/syslog2iptables.conf' | mail -s 'error in /etc/syslog2iptables.conf' root");
551433a01cab initial coding
carl
parents:
diff changeset
128 // update the load time on the current config to prevent complaining every 3 minutes
551433a01cab initial coding
carl
parents:
diff changeset
129 dc.load_time = time(NULL);
551433a01cab initial coding
carl
parents:
diff changeset
130 }
551433a01cab initial coding
carl
parents:
diff changeset
131 }
551433a01cab initial coding
carl
parents:
diff changeset
132 // now look for old configs with zero ref counts
551433a01cab initial coding
carl
parents:
diff changeset
133 for (configp_set::iterator i=old_configs.begin(); i!=old_configs.end(); ) {
551433a01cab initial coding
carl
parents:
diff changeset
134 CONFIG *old = *i;
551433a01cab initial coding
carl
parents:
diff changeset
135 if (!old->reference_count) {
551433a01cab initial coding
carl
parents:
diff changeset
136 if (debug_syslog) {
551433a01cab initial coding
carl
parents:
diff changeset
137 char buf[maxlen];
551433a01cab initial coding
carl
parents:
diff changeset
138 snprintf(buf, sizeof(buf), "freeing memory for old configuration generation %d", old->generation);
551433a01cab initial coding
carl
parents:
diff changeset
139 my_syslog(buf);
551433a01cab initial coding
carl
parents:
diff changeset
140 }
551433a01cab initial coding
carl
parents:
diff changeset
141 delete old; // destructor does all the work
551433a01cab initial coding
carl
parents:
diff changeset
142 old_configs.erase(i++);
551433a01cab initial coding
carl
parents:
diff changeset
143 }
551433a01cab initial coding
carl
parents:
diff changeset
144 else i++;
551433a01cab initial coding
carl
parents:
diff changeset
145 }
551433a01cab initial coding
carl
parents:
diff changeset
146 }
551433a01cab initial coding
carl
parents:
diff changeset
147 return NULL;
551433a01cab initial coding
carl
parents:
diff changeset
148 }
551433a01cab initial coding
carl
parents:
diff changeset
149
551433a01cab initial coding
carl
parents:
diff changeset
150
551433a01cab initial coding
carl
parents:
diff changeset
151 ////////////////////////////////////////////////
551433a01cab initial coding
carl
parents:
diff changeset
152 // The signal handler function -- only gets called when a SIGCHLD
551433a01cab initial coding
carl
parents:
diff changeset
153 // is received, ie when a child terminates
551433a01cab initial coding
carl
parents:
diff changeset
154 //
551433a01cab initial coding
carl
parents:
diff changeset
155 void sig_chld(int signo)
551433a01cab initial coding
carl
parents:
diff changeset
156 {
551433a01cab initial coding
carl
parents:
diff changeset
157 int status;
551433a01cab initial coding
carl
parents:
diff changeset
158 /* Wait for any child without blocking */
551433a01cab initial coding
carl
parents:
diff changeset
159 while (waitpid(-1, &status, WNOHANG) > 0) {
551433a01cab initial coding
carl
parents:
diff changeset
160 // ignore child exit status, we only do this to cleanup zombies
551433a01cab initial coding
carl
parents:
diff changeset
161 }
551433a01cab initial coding
carl
parents:
diff changeset
162 }
551433a01cab initial coding
carl
parents:
diff changeset
163
551433a01cab initial coding
carl
parents:
diff changeset
164
551433a01cab initial coding
carl
parents:
diff changeset
165 void usage(char *prog);
551433a01cab initial coding
carl
parents:
diff changeset
166 void usage(char *prog)
551433a01cab initial coding
carl
parents:
diff changeset
167 {
551433a01cab initial coding
carl
parents:
diff changeset
168 }
551433a01cab initial coding
carl
parents:
diff changeset
169
551433a01cab initial coding
carl
parents:
diff changeset
170
551433a01cab initial coding
carl
parents:
diff changeset
171 int main(int argc, char *argv[])
551433a01cab initial coding
carl
parents:
diff changeset
172 {
551433a01cab initial coding
carl
parents:
diff changeset
173 token_init();
551433a01cab initial coding
carl
parents:
diff changeset
174 bool check = true;
551433a01cab initial coding
carl
parents:
diff changeset
175 int c;
551433a01cab initial coding
carl
parents:
diff changeset
176 const char *args = "ch";
551433a01cab initial coding
carl
parents:
diff changeset
177 extern char *optarg;
551433a01cab initial coding
carl
parents:
diff changeset
178
551433a01cab initial coding
carl
parents:
diff changeset
179 // Process command line options
551433a01cab initial coding
carl
parents:
diff changeset
180 while ((c = getopt(argc, argv, args)) != -1) {
551433a01cab initial coding
carl
parents:
diff changeset
181 switch (c) {
551433a01cab initial coding
carl
parents:
diff changeset
182 case 'c':
551433a01cab initial coding
carl
parents:
diff changeset
183 check = true;
551433a01cab initial coding
carl
parents:
diff changeset
184 break;
551433a01cab initial coding
carl
parents:
diff changeset
185
551433a01cab initial coding
carl
parents:
diff changeset
186 case 'h':
551433a01cab initial coding
carl
parents:
diff changeset
187 default:
551433a01cab initial coding
carl
parents:
diff changeset
188 usage(argv[0]);
551433a01cab initial coding
carl
parents:
diff changeset
189 exit(EX_USAGE);
551433a01cab initial coding
carl
parents:
diff changeset
190 }
551433a01cab initial coding
carl
parents:
diff changeset
191 }
551433a01cab initial coding
carl
parents:
diff changeset
192
551433a01cab initial coding
carl
parents:
diff changeset
193 if (check) {
551433a01cab initial coding
carl
parents:
diff changeset
194 use_syslog = false;
551433a01cab initial coding
carl
parents:
diff changeset
195 debug_syslog = 10;
551433a01cab initial coding
carl
parents:
diff changeset
196 CONFIG *conf = new_conf();
551433a01cab initial coding
carl
parents:
diff changeset
197 if (conf) {
551433a01cab initial coding
carl
parents:
diff changeset
198 conf->dump();
551433a01cab initial coding
carl
parents:
diff changeset
199 delete conf;
551433a01cab initial coding
carl
parents:
diff changeset
200 return 0;
551433a01cab initial coding
carl
parents:
diff changeset
201 }
551433a01cab initial coding
carl
parents:
diff changeset
202 else {
551433a01cab initial coding
carl
parents:
diff changeset
203 return 1; // config failed to load
551433a01cab initial coding
carl
parents:
diff changeset
204 }
551433a01cab initial coding
carl
parents:
diff changeset
205 }
551433a01cab initial coding
carl
parents:
diff changeset
206
551433a01cab initial coding
carl
parents:
diff changeset
207 // switch to background mode
551433a01cab initial coding
carl
parents:
diff changeset
208 if (daemon(1,0) < 0) {
551433a01cab initial coding
carl
parents:
diff changeset
209 fprintf(stderr, "daemon() call failed\n");
551433a01cab initial coding
carl
parents:
diff changeset
210 exit(EX_UNAVAILABLE);
551433a01cab initial coding
carl
parents:
diff changeset
211 }
551433a01cab initial coding
carl
parents:
diff changeset
212
551433a01cab initial coding
carl
parents:
diff changeset
213 // write the pid
551433a01cab initial coding
carl
parents:
diff changeset
214 const char *pidpath = "/var/run/syslog2iptables.pid";
551433a01cab initial coding
carl
parents:
diff changeset
215 unlink(pidpath);
551433a01cab initial coding
carl
parents:
diff changeset
216 FILE *f = fopen(pidpath, "w");
551433a01cab initial coding
carl
parents:
diff changeset
217 if (f) {
551433a01cab initial coding
carl
parents:
diff changeset
218 #ifdef linux
551433a01cab initial coding
carl
parents:
diff changeset
219 // from a comment in the DCC source code:
551433a01cab initial coding
carl
parents:
diff changeset
220 // Linux threads are broken. Signals given the
551433a01cab initial coding
carl
parents:
diff changeset
221 // original process are delivered to only the
551433a01cab initial coding
carl
parents:
diff changeset
222 // thread that happens to have that PID. The
551433a01cab initial coding
carl
parents:
diff changeset
223 // sendmail libmilter thread that needs to hear
551433a01cab initial coding
carl
parents:
diff changeset
224 // SIGINT and other signals does not, and that breaks
551433a01cab initial coding
carl
parents:
diff changeset
225 // scripts that need to stop milters.
551433a01cab initial coding
carl
parents:
diff changeset
226 // However, signaling the process group works.
551433a01cab initial coding
carl
parents:
diff changeset
227 fprintf(f, "-%d\n", (u_int)getpgrp());
551433a01cab initial coding
carl
parents:
diff changeset
228 #else
551433a01cab initial coding
carl
parents:
diff changeset
229 fprintf(f, "%d\n", (u_int)getpid());
551433a01cab initial coding
carl
parents:
diff changeset
230 #endif
551433a01cab initial coding
carl
parents:
diff changeset
231 fclose(f);
551433a01cab initial coding
carl
parents:
diff changeset
232 }
551433a01cab initial coding
carl
parents:
diff changeset
233
551433a01cab initial coding
carl
parents:
diff changeset
234 // initialize the thread sync objects
551433a01cab initial coding
carl
parents:
diff changeset
235 pthread_mutex_init(&config_mutex, 0);
551433a01cab initial coding
carl
parents:
diff changeset
236 pthread_mutex_init(&syslog_mutex, 0);
551433a01cab initial coding
carl
parents:
diff changeset
237
551433a01cab initial coding
carl
parents:
diff changeset
238 // load the initial config
551433a01cab initial coding
carl
parents:
diff changeset
239 config = new_conf();
551433a01cab initial coding
carl
parents:
diff changeset
240 if (!config) {
551433a01cab initial coding
carl
parents:
diff changeset
241 my_syslog("failed to load initial configuration, quitting");
551433a01cab initial coding
carl
parents:
diff changeset
242 exit(1);
551433a01cab initial coding
carl
parents:
diff changeset
243 }
551433a01cab initial coding
carl
parents:
diff changeset
244
551433a01cab initial coding
carl
parents:
diff changeset
245 // only create threads after the fork() in daemon
551433a01cab initial coding
carl
parents:
diff changeset
246 pthread_t tid;
551433a01cab initial coding
carl
parents:
diff changeset
247 if (pthread_create(&tid, 0, config_loader, 0))
551433a01cab initial coding
carl
parents:
diff changeset
248 my_syslog("failed to create config loader thread");
551433a01cab initial coding
carl
parents:
diff changeset
249 if (pthread_detach(tid))
551433a01cab initial coding
carl
parents:
diff changeset
250 my_syslog("failed to detach config loader thread");
551433a01cab initial coding
carl
parents:
diff changeset
251
551433a01cab initial coding
carl
parents:
diff changeset
252 loader_run = false; // eventually the config loader thread will terminate
551433a01cab initial coding
carl
parents:
diff changeset
253 return EXIT_SUCCESS;
551433a01cab initial coding
carl
parents:
diff changeset
254 }