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