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