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