annotate src/syslog2iptables.cpp @ 39:a9101672c0e9

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