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