Mercurial > sm-archive
annotate src/sm-archive.cpp @ 26:c6d5b1658f61
Added tag stable-1-0-9 for changeset a2d60f43590b
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Mon, 06 Feb 2017 12:08:35 -0800 |
parents | 09564d4acd9e |
children | 9298f8b00db2 |
rev | line source |
---|---|
0 | 1 /* |
2 | |
13 | 3 Copyright (c) 2007 Carl Byington - 510 Software Group, released under |
4 the GPL version 3 or any later version at your choice available at | |
5 http://www.gnu.org/licenses/gpl-3.0.txt | |
0 | 6 |
7 Based on a sample milter Copyright (c) 2000-2003 Sendmail, Inc. and its | |
13 | 8 suppliers. |
0 | 9 |
10 -p port The port through which the MTA will connect to this milter. | |
11 -t sec The timeout value. | |
12 -c Check the config, and print a copy to stdout. Don't start the | |
13 milter or do anything with the socket. | |
14 -d increase debug level | |
15 | |
16 */ | |
17 | |
18 | |
19 // from sendmail sample | |
20 #include <sys/types.h> | |
21 #include <sys/stat.h> | |
22 #include <errno.h> | |
23 #include <sysexits.h> | |
24 #include <unistd.h> | |
25 | |
26 // needed for thread | |
27 #include <pthread.h> | |
28 | |
29 // needed for std c++ collections | |
30 #include <set> | |
31 #include <map> | |
32 #include <list> | |
33 | |
34 // misc stuff needed here | |
35 #include <ctype.h> | |
36 #include <syslog.h> | |
37 #include <pwd.h> | |
38 #include <sys/wait.h> /* header for waitpid() and various macros */ | |
39 #include <signal.h> /* header for signal functions */ | |
40 | |
41 #include "includes.h" | |
42 | |
9 | 43 #ifndef HAVE_DAEMON |
44 #include "daemon.h" | |
45 #include "daemon.c" | |
46 #endif | |
47 | |
0 | 48 extern "C" { |
9 | 49 #include <libmilter/mfapi.h> |
0 | 50 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr); |
51 sfsistat mlfi_envfrom(SMFICTX *ctx, char **argv); | |
52 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **argv); | |
53 sfsistat mlfi_eom(SMFICTX *ctx); | |
54 sfsistat mlfi_abort(SMFICTX *ctx); | |
55 sfsistat mlfi_close(SMFICTX *ctx); | |
56 void sig_chld(int signo); | |
57 } | |
58 | |
59 int debug_syslog = 0; | |
60 bool syslog_opened = false; | |
61 bool use_syslog = true; // false to printf | |
62 bool loader_run = true; // used to stop the config loader thread | |
63 CONFIG *config = NULL; // protected by the config_mutex | |
64 int generation = 0; // protected by the config_mutex | |
65 const int maxlen = 1000; // used for snprintf buffers | |
66 | |
67 pthread_mutex_t config_mutex; | |
68 pthread_mutex_t syslog_mutex; | |
69 | |
70 | |
71 mlfiPriv::mlfiPriv() { | |
72 pthread_mutex_lock(&config_mutex); | |
73 pc = config; | |
74 pc->reference_count++; | |
75 pthread_mutex_unlock(&config_mutex); | |
8 | 76 mailaddr = NULL; |
77 queueid = NULL; | |
0 | 78 } |
79 | |
80 mlfiPriv::~mlfiPriv() { | |
81 pthread_mutex_lock(&config_mutex); | |
82 pc->reference_count--; | |
83 pthread_mutex_unlock(&config_mutex); | |
84 reset(true); | |
85 } | |
86 | |
87 void mlfiPriv::reset(bool final) { | |
8 | 88 targets.clear(); |
13 | 89 for (string_set::iterator i=removal.begin(); i!=removal.end(); i++) { |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
90 const char *remove = (*i); |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
91 free((void*)remove); |
13 | 92 } |
93 removal.clear(); | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
94 if (mailaddr) free((void*)mailaddr); |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
95 if (queueid) free((void*)queueid); |
0 | 96 if (!final) { |
97 mailaddr = NULL; | |
98 queueid = NULL; | |
99 } | |
100 } | |
101 | |
102 #define MLFIPRIV ((struct mlfiPriv *) smfi_getpriv(ctx)) | |
103 | |
104 | |
105 //////////////////////////////////////////////// | |
106 // syslog a message | |
107 // | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
108 void my_syslog(mlfiPriv *priv, const char *text) { |
0 | 109 char buf[maxlen]; |
110 if (priv) { | |
21
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
111 snprintf(buf, sizeof(buf), "%s: %s", |
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
112 priv->queueid ? priv->queueid : "NOQUEUE", text); |
0 | 113 text = buf; |
114 } | |
115 if (use_syslog) { | |
116 pthread_mutex_lock(&syslog_mutex); | |
117 if (!syslog_opened) { | |
2 | 118 openlog("sm-archive", LOG_PID, LOG_MAIL); |
0 | 119 syslog_opened = true; |
120 } | |
121 syslog(LOG_NOTICE, "%s", text); | |
122 pthread_mutex_unlock(&syslog_mutex); | |
123 } | |
124 else { | |
125 printf("%s \n", text); | |
126 } | |
127 } | |
128 | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
129 void my_syslog(const char *text) { |
0 | 130 my_syslog(NULL, text); |
131 } | |
132 | |
133 | |
134 //////////////////////////////////////////////// | |
135 // this email address is passed in from sendmail, and will | |
136 // always be enclosed in <>. It may have mixed case, just | |
137 // as the mail client sent it. We dup the string and convert | |
138 // the duplicate to lower case. | |
21
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
139 // Postfix will return addresses without <> if they have been provided |
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
140 // this way in the SMTP dialog. |
0 | 141 // |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
142 const char *to_lower_string(const char *email); |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
143 const char *to_lower_string(const char *email) { |
21
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
144 char *key, *p; |
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
145 int i; |
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
146 |
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
147 if (strcmp(email, "<>") == 0) return strdup(email); |
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
148 if (email[0] == '<') p = (char *) email + 1; |
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
149 else p = (char *) email; |
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
150 key = (char *) malloc(strlen(p) + 1); |
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
151 for (i = 0; p[i] != '\0'; i++) key[i] = tolower(p[i]); |
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
152 if (p[i - 1] == '>') i--; |
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
153 key[i] = '\0'; |
0 | 154 return key; |
155 } | |
156 | |
157 | |
158 //////////////////////////////////////////////// | |
159 // start of sendmail milter interfaces | |
160 // | |
161 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr) | |
162 { | |
163 // allocate some private memory | |
164 mlfiPriv *priv = new mlfiPriv; | |
165 // save the private data | |
166 smfi_setpriv(ctx, (void*)priv); | |
167 // continue processing | |
168 return SMFIS_CONTINUE; | |
169 } | |
170 | |
171 sfsistat mlfi_envfrom(SMFICTX *ctx, char **from) | |
172 { | |
173 mlfiPriv &priv = *MLFIPRIV; | |
174 priv.mailaddr = to_lower_string(from[0]); | |
175 return SMFIS_CONTINUE; | |
176 } | |
177 | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
178 void add_target(mlfiPriv &priv, const char *target); |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
179 void add_target(mlfiPriv &priv, const char *target) |
7 | 180 { |
181 if (target) { | |
8 | 182 string_set::iterator i = priv.targets.find(target); |
183 if (i == priv.targets.end()) priv.targets.insert(target); | |
7 | 184 } |
185 } | |
186 | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
187 void add_remove(mlfiPriv &priv, const char *remove); |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
188 void add_remove(mlfiPriv &priv, const char *remove) |
13 | 189 { |
190 if (remove) { | |
191 string_set::iterator i = priv.removal.find(remove); | |
192 if (i == priv.removal.end()) priv.removal.insert(remove); | |
193 } | |
194 } | |
195 | |
0 | 196 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **rcpt) |
197 { | |
198 mlfiPriv &priv = *MLFIPRIV; | |
199 CONFIG &dc = *priv.pc; | |
21
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
200 const char *i_macro = smfi_getsymval(ctx, (char*)"i"); |
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
201 if (!priv.queueid && i_macro) priv.queueid = strdup(i_macro); |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
202 const char *rcptaddr = to_lower_string(rcpt[0]); |
0 | 203 if (debug_syslog > 1) { |
204 char msg[maxlen]; | |
205 snprintf(msg, sizeof(msg), "from <%s> to <%s>", priv.mailaddr, rcptaddr); | |
206 my_syslog(&priv, msg); | |
207 } | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
208 const char *target = dc.find_to(rcptaddr); |
13 | 209 add_target(priv, target); |
210 bool remove = dc.find_remove(rcptaddr); | |
211 if (remove) add_remove(priv, strdup(rcptaddr)); | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
212 free((void*)rcptaddr); |
0 | 213 return SMFIS_CONTINUE; |
214 } | |
215 | |
216 sfsistat mlfi_eom(SMFICTX *ctx) | |
217 { | |
8 | 218 mlfiPriv &priv = *MLFIPRIV; |
219 CONFIG &dc = *priv.pc; | |
21
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
220 const char *i_macro = smfi_getsymval(ctx, (char*)"i"); |
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
221 if (!priv.queueid && i_macro) priv.queueid = strdup(i_macro); |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
222 const char *target = dc.find_from(priv.mailaddr); |
8 | 223 add_target(priv, target); |
224 for (string_set::iterator i=priv.targets.begin(); i!=priv.targets.end(); i++) { | |
225 target = (*i); | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
226 smfi_addrcpt(ctx, (char*)target); |
8 | 227 if (debug_syslog > 1) { |
228 char msg[maxlen]; | |
229 snprintf(msg, sizeof(msg), "adding recipient <%s>", target); | |
230 my_syslog(&priv, msg); | |
231 } | |
232 } | |
13 | 233 for (string_set::iterator i=priv.removal.begin(); i!=priv.removal.end(); i++) { |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
234 const char *remove = (*i); |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
235 smfi_delrcpt(ctx, (char*)remove); |
13 | 236 if (debug_syslog > 1) { |
237 char msg[maxlen]; | |
238 snprintf(msg, sizeof(msg), "removing recipient <%s>", remove); | |
239 my_syslog(&priv, msg); | |
240 } | |
241 } | |
0 | 242 // reset for a new message on the same connection |
243 mlfi_abort(ctx); | |
244 return SMFIS_CONTINUE; | |
245 } | |
246 | |
247 sfsistat mlfi_abort(SMFICTX *ctx) | |
248 { | |
249 mlfiPriv &priv = *MLFIPRIV; | |
250 priv.reset(); | |
251 return SMFIS_CONTINUE; | |
252 } | |
253 | |
254 sfsistat mlfi_close(SMFICTX *ctx) | |
255 { | |
256 mlfiPriv *priv = MLFIPRIV; | |
257 if (!priv) return SMFIS_CONTINUE; | |
258 delete priv; | |
259 smfi_setpriv(ctx, NULL); | |
260 return SMFIS_CONTINUE; | |
261 } | |
262 | |
263 struct smfiDesc smfilter = | |
264 { | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
265 (char*)"SM-ARCHIVE",// filter name |
0 | 266 SMFI_VERSION, // version code -- do not change |
13 | 267 SMFIF_ADDRCPT | \ |
268 SMFIF_DELRCPT, // flags | |
0 | 269 mlfi_connect, // connection info filter |
270 NULL, // SMTP HELO command filter | |
271 mlfi_envfrom, // envelope sender filter | |
272 mlfi_envrcpt, // envelope recipient filter | |
273 NULL, // header filter | |
274 NULL, // end of header | |
275 NULL, // body block filter | |
276 mlfi_eom, // end of message | |
277 mlfi_abort, // message aborted | |
278 mlfi_close, // connection cleanup | |
279 }; | |
280 | |
281 | |
282 //////////////////////////////////////////////// | |
283 // reload the config | |
284 // | |
285 CONFIG* new_conf(); | |
286 CONFIG* new_conf() { | |
287 CONFIG *newc = new CONFIG; | |
288 pthread_mutex_lock(&config_mutex); | |
289 newc->generation = generation++; | |
290 pthread_mutex_unlock(&config_mutex); | |
291 if (debug_syslog) { | |
292 char buf[maxlen]; | |
293 snprintf(buf, sizeof(buf), "loading configuration generation %d", newc->generation); | |
294 my_syslog(buf); | |
295 } | |
2 | 296 if (load_conf(*newc, "sm-archive.conf")) { |
0 | 297 newc->load_time = time(NULL); |
298 return newc; | |
299 } | |
300 delete newc; | |
301 return NULL; | |
302 } | |
303 | |
304 | |
305 //////////////////////////////////////////////// | |
306 // thread to watch the old config files for changes | |
307 // and reload when needed. we also cleanup old | |
308 // configs whose reference count has gone to zero. | |
309 // | |
10 | 310 extern "C" {void* config_loader(void *arg);} |
0 | 311 void* config_loader(void *arg) { |
312 while (loader_run) { | |
313 sleep(180); // look for modifications every 3 minutes | |
314 if (!loader_run) break; | |
315 CONFIG &dc = *config; | |
316 time_t then = dc.load_time; | |
317 struct stat st; | |
318 bool reload = false; | |
319 for (string_set::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) { | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
320 const char *fn = *i; |
0 | 321 if (stat(fn, &st)) reload = true; // file disappeared |
322 else if (st.st_mtime > then) reload = true; // file modified | |
323 if (reload) break; | |
324 } | |
325 if (reload) { | |
326 CONFIG *newc = new_conf(); | |
327 if (newc) { | |
328 // replace the global config pointer | |
329 pthread_mutex_lock(&config_mutex); | |
330 CONFIG *old = config; | |
13 | 331 bool last = old && (!old->reference_count); |
0 | 332 config = newc; |
333 pthread_mutex_unlock(&config_mutex); | |
13 | 334 if (last) delete old; // there were no references to this config |
0 | 335 } |
336 else { | |
337 // failed to load new config | |
338 my_syslog("failed to load new configuration"); | |
2 | 339 system("echo 'failed to load new sm-archive configuration from /etc/sm-archive' | mail -s 'error in /etc/sm-archive configuration' root"); |
0 | 340 // update the load time on the current config to prevent complaining every 3 minutes |
341 dc.load_time = time(NULL); | |
342 } | |
343 } | |
344 } | |
345 return NULL; | |
346 } | |
347 | |
348 | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
349 void usage(const char *prog); |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
350 void usage(const char *prog) |
0 | 351 { |
352 fprintf(stderr, "Usage: %s [-d [level]] [-c] -p sm-sock-addr [-t timeout]\n", prog); | |
353 fprintf(stderr, "where sm-sock-addr is for the connection to sendmail\n"); | |
354 fprintf(stderr, " and should be one of\n"); | |
355 fprintf(stderr, " inet:port@ip-address\n"); | |
356 fprintf(stderr, " local:local-domain-socket-file-name\n"); | |
357 fprintf(stderr, "-c will load and dump the config to stdout\n"); | |
358 fprintf(stderr, "-d will set the syslog message level, currently 0 to 3\n"); | |
359 } | |
360 | |
361 | |
362 | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
363 void setup_socket(const char *sock); |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
364 void setup_socket(const char *sock) { |
0 | 365 unlink(sock); |
366 // sockaddr_un addr; | |
367 // memset(&addr, '\0', sizeof addr); | |
368 // addr.sun_family = AF_UNIX; | |
369 // strncpy(addr.sun_path, sock, sizeof(addr.sun_path)-1); | |
370 // int s = socket(AF_UNIX, SOCK_STREAM, 0); | |
371 // bind(s, (sockaddr*)&addr, sizeof(addr)); | |
372 // close(s); | |
373 } | |
374 | |
375 | |
376 /* | |
377 * The signal handler function -- only gets called when a SIGCHLD | |
378 * is received, ie when a child terminates | |
379 */ | |
380 void sig_chld(int signo) | |
381 { | |
382 int status; | |
383 /* Wait for any child without blocking */ | |
384 while (waitpid(-1, &status, WNOHANG) > 0) { | |
385 // ignore child exit status, we only do this to cleanup zombies | |
386 } | |
387 } | |
388 | |
389 | |
390 int main(int argc, char**argv) | |
391 { | |
392 token_init(); | |
393 bool check = false; | |
394 bool setconn = false; | |
395 int c; | |
396 const char *args = "p:t:d:ch"; | |
397 extern char *optarg; | |
398 | |
399 // Process command line options | |
400 while ((c = getopt(argc, argv, args)) != -1) { | |
401 switch (c) { | |
402 case 'p': | |
403 if (optarg == NULL || *optarg == '\0') { | |
404 fprintf(stderr, "Illegal sendmail socket: %s\n", optarg); | |
405 exit(EX_USAGE); | |
406 } | |
407 if (smfi_setconn(optarg) == MI_FAILURE) { | |
408 fprintf(stderr, "smfi_setconn failed\n"); | |
409 exit(EX_SOFTWARE); | |
410 } | |
411 if (strncasecmp(optarg, "unix:", 5) == 0) setup_socket(optarg + 5); | |
412 else if (strncasecmp(optarg, "local:", 6) == 0) setup_socket(optarg + 6); | |
413 setconn = true; | |
414 break; | |
415 | |
416 case 't': | |
417 if (optarg == NULL || *optarg == '\0') { | |
418 fprintf(stderr, "Illegal timeout: %s\n", optarg); | |
419 exit(EX_USAGE); | |
420 } | |
421 if (smfi_settimeout(atoi(optarg)) == MI_FAILURE) { | |
422 fprintf(stderr, "smfi_settimeout failed\n"); | |
423 exit(EX_SOFTWARE); | |
424 } | |
425 break; | |
426 | |
427 case 'c': | |
428 check = true; | |
429 break; | |
430 | |
431 case 'd': | |
432 if (optarg == NULL || *optarg == '\0') debug_syslog = 1; | |
433 else debug_syslog = atoi(optarg); | |
434 break; | |
435 | |
436 case 'h': | |
437 default: | |
438 usage(argv[0]); | |
439 exit(EX_USAGE); | |
440 } | |
441 } | |
442 | |
443 if (check) { | |
444 use_syslog = false; | |
445 debug_syslog = 10; | |
446 CONFIG *conf = new_conf(); | |
447 if (conf) { | |
448 conf->dump(); | |
449 delete conf; | |
13 | 450 clear_strings(); // for valgrind checking |
0 | 451 return 0; |
452 } | |
453 else { | |
454 return 1; // config failed to load | |
455 } | |
456 } | |
457 | |
458 if (!setconn) { | |
459 fprintf(stderr, "%s: Missing required -p argument\n", argv[0]); | |
460 usage(argv[0]); | |
461 exit(EX_USAGE); | |
462 } | |
463 | |
464 if (smfi_register(smfilter) == MI_FAILURE) { | |
465 fprintf(stderr, "smfi_register failed\n"); | |
466 exit(EX_UNAVAILABLE); | |
467 } | |
468 | |
469 // switch to background mode | |
470 if (daemon(1,0) < 0) { | |
471 fprintf(stderr, "daemon() call failed\n"); | |
472 exit(EX_UNAVAILABLE); | |
473 } | |
474 | |
475 // write the pid | |
2 | 476 const char *pidpath = "/var/run/sm-archive.pid"; |
0 | 477 unlink(pidpath); |
478 FILE *f = fopen(pidpath, "w"); | |
479 if (f) { | |
480 #ifdef linux | |
481 // from a comment in the DCC source code: | |
482 // Linux threads are broken. Signals given the | |
483 // original process are delivered to only the | |
484 // thread that happens to have that PID. The | |
485 // sendmail libmilter thread that needs to hear | |
486 // SIGINT and other signals does not, and that breaks | |
487 // scripts that need to stop milters. | |
488 // However, signaling the process group works. | |
489 fprintf(f, "-%d\n", (u_int)getpgrp()); | |
490 #else | |
491 fprintf(f, "%d\n", (u_int)getpid()); | |
492 #endif | |
493 fclose(f); | |
494 } | |
495 | |
496 // initialize the thread sync objects | |
497 pthread_mutex_init(&config_mutex, 0); | |
498 pthread_mutex_init(&syslog_mutex, 0); | |
499 | |
500 // drop root privs | |
501 struct passwd *pw = getpwnam("sm-archive"); | |
502 if (pw) { | |
503 if (setgid(pw->pw_gid) == -1) { | |
2 | 504 my_syslog("failed to switch to group sm-archive"); |
0 | 505 } |
506 if (setuid(pw->pw_uid) == -1) { | |
2 | 507 my_syslog("failed to switch to user sm-archive"); |
0 | 508 } |
509 } | |
510 | |
511 // load the initial config | |
512 config = new_conf(); | |
513 if (!config) { | |
514 my_syslog("failed to load initial configuration, quitting"); | |
515 exit(1); | |
516 } | |
517 | |
518 // only create threads after the fork() in daemon | |
519 pthread_t tid; | |
520 if (pthread_create(&tid, 0, config_loader, 0)) | |
521 my_syslog("failed to create config loader thread"); | |
522 if (pthread_detach(tid)) | |
523 my_syslog("failed to detach config loader thread"); | |
524 | |
525 time_t starting = time(NULL); | |
526 int rc = smfi_main(); | |
527 if ((rc != MI_SUCCESS) && (time(NULL) > starting+5*60)) { | |
528 my_syslog("trying to restart after smfi_main()"); | |
529 loader_run = false; // eventually the config loader thread will terminate | |
530 execvp(argv[0], argv); | |
531 } | |
532 exit((rc == MI_SUCCESS) ? 0 : EX_UNAVAILABLE); | |
533 } | |
534 |