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