annotate src/syslog2iptables.cpp @ 81:cc01f2caff37 stable-1-0-19

add error_log to the httpd files
author Carl Byington <carl@five-ten-sg.com>
date Sun, 11 Feb 2024 12:04:13 -0800
parents f133196b8591
children
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 while (loader_run) {
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
95 sleep(180); // look for modifications every 3 minutes
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
96 if (!loader_run) break;
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
97 CONFIG &dc = *config;
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
98 time_t then = dc.load_time;
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
99 struct stat st;
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
100 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
101 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
102 const char *fn = *i;
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
103 if (stat(fn, &st)) reload = true; // file disappeared
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
104 else if (st.st_mtime > then) reload = true; // file modified
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
105 if (reload) break;
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
106 }
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
107 if (reload) {
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
108 CONFIG *newc = new_conf();
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
109 if (newc) {
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
110 // replace the global config pointer
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
111 pthread_mutex_lock(&config_mutex);
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
112 config = newc;
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
113 pthread_mutex_unlock(&config_mutex);
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
114 }
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
115 else {
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
116 // failed to load new config
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
117 my_syslog("failed to load new configuration");
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
118 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
119 // 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
120 dc.load_time = time(NULL);
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 }
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 return NULL;
1
551433a01cab initial coding
carl
parents:
diff changeset
125 }
551433a01cab initial coding
carl
parents:
diff changeset
126
551433a01cab initial coding
carl
parents:
diff changeset
127
551433a01cab initial coding
carl
parents:
diff changeset
128 ////////////////////////////////////////////////
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
129 // The signal handler function for child process terminations,
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
130 // called when a child terminates.
1
551433a01cab initial coding
carl
parents:
diff changeset
131 //
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
132 void sigchld(int sig)
1
551433a01cab initial coding
carl
parents:
diff changeset
133 {
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
134 int status;
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
135 /* Wait for any child without blocking */
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
136 while (waitpid(-1, &status, WNOHANG) > 0) {
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
137 // ignore child exit status, we only do this to cleanup zombies
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
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 // The termination signal handler function, called to
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
144 // request termination of this process.
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 void sigterm(int sig)
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
147 {
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
148 loader_run = false;
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
149 signal(sig, SIG_DFL); // quit on repeated signals
1
551433a01cab initial coding
carl
parents:
diff changeset
150 }
551433a01cab initial coding
carl
parents:
diff changeset
151
551433a01cab initial coding
carl
parents:
diff changeset
152
551433a01cab initial coding
carl
parents:
diff changeset
153 void usage(char *prog);
551433a01cab initial coding
carl
parents:
diff changeset
154 void usage(char *prog)
551433a01cab initial coding
carl
parents:
diff changeset
155 {
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
156 fprintf(stderr, "Usage: %s [-d [level]] [-c]\n", prog);
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
157 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
158 fprintf(stderr, "-d will set the syslog message level, currently 0 to 3\n");
5
276c4edc8521 initial coding
carl
parents: 4
diff changeset
159 }
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 void worker();
276c4edc8521 initial coding
carl
parents: 4
diff changeset
163 void worker()
276c4edc8521 initial coding
carl
parents: 4
diff changeset
164 {
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
165 time_t t = time(NULL);
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
166 CONFIG *c;
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
167 pthread_mutex_lock(&config_mutex);
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
168 c = config;
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
169 c->reference_count++;
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
170 pthread_mutex_unlock(&config_mutex);
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
171 while (loader_run) {
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
172 if (c != config) {
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
173 pthread_mutex_lock(&config_mutex);
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
174 CONFIG *old = c; old->reference_count--;
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
175 c = config; c->reference_count++;
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
176 pthread_mutex_unlock(&config_mutex);
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
177 if (!old->reference_count) {
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
178 if (debug_syslog) {
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
179 char buf[maxlen];
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
180 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
181 my_syslog(buf);
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 delete old; // destructor does all the work
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 }
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
186 c->read();
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
187 c->sleep(2, t);
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
188 }
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
189 // worker shutting down, free all ip addresses
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
190 c->free_all();
1
551433a01cab initial coding
carl
parents:
diff changeset
191 }
551433a01cab initial coding
carl
parents:
diff changeset
192
551433a01cab initial coding
carl
parents:
diff changeset
193
551433a01cab initial coding
carl
parents:
diff changeset
194 int main(int argc, char *argv[])
551433a01cab initial coding
carl
parents:
diff changeset
195 {
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
196 token_init();
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
197 bool check = false;
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
198 int c;
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
199 const char *args = "d:ch";
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
200 extern char *optarg;
1
551433a01cab initial coding
carl
parents:
diff changeset
201
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
202 // Process command line options
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
203 while ((c = getopt(argc, argv, args)) != -1) {
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
204 switch (c) {
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
205 case 'c':
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
206 check = true;
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
207 break;
1
551433a01cab initial coding
carl
parents:
diff changeset
208
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
209 case 'd':
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
210 if (optarg == NULL || *optarg == '\0') debug_syslog = 1;
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
211 else debug_syslog = atoi(optarg);
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
212 break;
5
276c4edc8521 initial coding
carl
parents: 4
diff changeset
213
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
214 case 'h':
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
215 default:
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
216 usage(argv[0]);
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
217 exit(EX_USAGE);
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
218 }
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
219 }
1
551433a01cab initial coding
carl
parents:
diff changeset
220
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
221 if (check) {
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
222 use_syslog = false;
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
223 debug_syslog = 10;
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
224 config = new_conf();
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
225 if (config) {
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
226 config->dump();
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
227 delete config;
38
26c29da3fbdf shutdown removes iptables entries that we added
carl
parents: 36
diff changeset
228 clear_strings(); // for valgrind checking
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
229 return 0;
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
230 }
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
231 else {
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
232 return 1; // config failed to load
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
233 }
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
234 }
1
551433a01cab initial coding
carl
parents:
diff changeset
235
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
236 // switch to background mode
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
237 if (daemon(1,0) < 0) {
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
238 fprintf(stderr, "daemon() call failed\n");
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
239 exit(EX_UNAVAILABLE);
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
240 }
1
551433a01cab initial coding
carl
parents:
diff changeset
241
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
242 // write the pid
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
243 const char *pidpath = "/var/run/syslog2iptables.pid";
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
244 unlink(pidpath);
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
245 FILE *f = fopen(pidpath, "w");
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
246 if (f) {
1
551433a01cab initial coding
carl
parents:
diff changeset
247 #ifdef linux
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
248 // from a comment in the DCC source code:
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
249 // Linux threads are broken. Signals given the
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
250 // original process are delivered to only the
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
251 // thread that happens to have that PID. The
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
252 // sendmail libmilter thread that needs to hear
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
253 // SIGINT and other signals does not, and that breaks
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
254 // scripts that need to stop milters.
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
255 // However, signaling the process group works.
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
256 fprintf(f, "-%d\n", (u_int)getpgrp());
1
551433a01cab initial coding
carl
parents:
diff changeset
257 #else
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
258 fprintf(f, "%d\n", (u_int)getpid());
1
551433a01cab initial coding
carl
parents:
diff changeset
259 #endif
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
260 fclose(f);
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
261 }
1
551433a01cab initial coding
carl
parents:
diff changeset
262
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
263 // setup signal handler for termination signals
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
264 signal(SIGHUP, sigterm);
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
265 signal(SIGTERM, sigterm);
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
266 signal(SIGINT, sigterm);
1
551433a01cab initial coding
carl
parents:
diff changeset
267
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
268 // initialize the thread sync objects
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
269 pthread_mutex_init(&config_mutex, 0);
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
270 pthread_mutex_init(&syslog_mutex, 0);
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
271
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
272 // load the initial config
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
273 config = new_conf();
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
274 if (!config) {
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
275 my_syslog("failed to load initial configuration, quitting");
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
276 exit(1);
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
277 }
1
551433a01cab initial coding
carl
parents:
diff changeset
278
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
279 // setup sigchld handler to prevent zombies
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
280 struct sigaction act;
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
281 act.sa_handler = sigchld; // Assign sig_chld as our SIGCHLD handler
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
282 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
283 act.sa_flags = SA_NOCLDSTOP; // only want children that have terminated
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
284 if (sigaction(SIGCHLD, &act, NULL) < 0) {
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
285 my_syslog("failed to setup SIGCHLD handler");
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
286 exit(1);
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
287 }
2
6e88da080f08 initial coding
carl
parents: 1
diff changeset
288
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
289 // only create threads after the fork() in daemon
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
290 pthread_t tid;
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
291 if (pthread_create(&tid, 0, config_loader, 0))
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
292 my_syslog("failed to create config loader thread");
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
293 if (pthread_detach(tid))
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
294 my_syslog("failed to detach config loader thread");
1
551433a01cab initial coding
carl
parents:
diff changeset
295
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
296 worker();
40
cdd6dde8d4ec shutdown removes iptables entries that we added
carl
parents: 39
diff changeset
297 if (config) delete config; // for valgrind checking
39
a9101672c0e9 shutdown removes iptables entries that we added
carl
parents: 38
diff changeset
298 clear_strings(); // for valgrind checking
5
276c4edc8521 initial coding
carl
parents: 4
diff changeset
299
36
6a2f26976898 shutdown removes iptables entries that we added
carl
parents: 26
diff changeset
300 return EXIT_SUCCESS;
1
551433a01cab initial coding
carl
parents:
diff changeset
301 }