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