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