annotate src/syslog2iptables.cpp @ 42:d9ae11033b4b stable-1-9

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