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