0
|
1 /*
|
|
2
|
|
3 Copyright (c) 2004 Carl Byington - 510 Software Group, released under
|
|
4 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.
|
9
|
12 -c Check the config, and print a copy to stdout. Don't start the
|
4
|
13 milter or do anything with the socket.
|
16
|
14 -d Add debug syslog entries
|
|
15
|
0
|
16
|
13
|
17 TODO:
|
|
18 1) Add config for max_recipients for each mail domain. Recipients in
|
|
19 excess of that limit will be rejected, and the entire data will be
|
|
20 rejected if it is sent.
|
|
21
|
|
22 2) Add config for poison addresses. If any recipient is poison, all
|
|
23 recipients are rejected even if they would be whitelisted, and the
|
|
24 data is rejected if sent.
|
|
25
|
0
|
26 */
|
|
27
|
|
28
|
|
29 // from sendmail sample
|
|
30 #include <sys/types.h>
|
|
31 #include <sys/stat.h>
|
|
32 #include <errno.h>
|
|
33 #include <sysexits.h>
|
|
34 #include <unistd.h>
|
|
35
|
|
36 // needed for socket io
|
|
37 #include <sys/ioctl.h>
|
|
38 #include <net/if.h>
|
|
39 #include <arpa/inet.h>
|
|
40 #include <netinet/in.h>
|
|
41 #include <netinet/tcp.h>
|
|
42 #include <netdb.h>
|
|
43 #include <sys/socket.h>
|
|
44
|
|
45 // needed for thread
|
|
46 #include <pthread.h>
|
|
47
|
|
48 // needed for std c++ collections
|
|
49 #include <set>
|
|
50 #include <map>
|
|
51 #include <list>
|
|
52
|
|
53 // for the dns resolver
|
|
54 #include <netinet/in.h>
|
|
55 #include <arpa/nameser.h>
|
|
56 #include <resolv.h>
|
|
57
|
|
58 // misc stuff needed here
|
|
59 #include <ctype.h>
|
|
60 #include <fstream>
|
|
61 #include <syslog.h>
|
|
62
|
8
|
63 static char* dnsbl_version="$Id$";
|
0
|
64
|
8
|
65 #define DEFAULT "default"
|
|
66 #define WHITE "white"
|
|
67 #define BLACK "black"
|
|
68 #define OK "ok"
|
|
69 #define MANY "many"
|
|
70
|
|
71 enum status {oksofar, // not rejected yet
|
|
72 white, // whitelisted by envelope from
|
|
73 black, // blacklisted by envelope from or to
|
|
74 reject}; // rejected by a dns list
|
1
|
75
|
0
|
76 using namespace std;
|
|
77
|
|
78 extern "C" {
|
|
79 #include "libmilter/mfapi.h"
|
|
80 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr);
|
|
81 sfsistat mlfi_envfrom(SMFICTX *ctx, char **argv);
|
|
82 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **argv);
|
8
|
83 sfsistat mlfi_body(SMFICTX *ctx, u_char *data, size_t len);
|
|
84 sfsistat mlfi_eom(SMFICTX *ctx);
|
|
85 sfsistat mlfi_abort(SMFICTX *ctx);
|
0
|
86 sfsistat mlfi_close(SMFICTX *ctx);
|
|
87 }
|
|
88
|
|
89 struct ltstr {
|
|
90 bool operator()(char* s1, char* s2) const {
|
|
91 return strcmp(s1, s2) < 0;
|
|
92 }
|
|
93 };
|
|
94
|
|
95 struct DNSBL {
|
|
96 char *suffix; // blacklist suffix like blackholes.five-ten-sg.com
|
|
97 char *message; // error message with one or two %s operators for the ip address replacement
|
|
98 DNSBL(char *s, char *m);
|
|
99 };
|
|
100 DNSBL::DNSBL(char *s, char *m) {
|
|
101 suffix = s;
|
|
102 message = m;
|
|
103 }
|
|
104
|
|
105 typedef DNSBL * DNSBLP;
|
|
106 typedef list<DNSBLP> DNSBLL;
|
|
107 typedef DNSBLL * DNSBLLP;
|
|
108 typedef map<char *, char *, ltstr> string_map;
|
|
109 typedef map<char *, string_map *, ltstr> from_map;
|
|
110 typedef map<char *, DNSBLP, ltstr> dnsblp_map;
|
|
111 typedef map<char *, DNSBLLP, ltstr> dnsbllp_map;
|
|
112 typedef set<char *, ltstr> string_set;
|
|
113 typedef list<char *> string_list;
|
|
114
|
|
115 struct CONFIG {
|
|
116 // the only mutable stuff once it has been loaded from the config file
|
|
117 int reference_count; // protected by the global config_mutex
|
|
118 // all the rest is constant after loading from the config file
|
|
119 time_t load_time;
|
|
120 string_list config_files;
|
|
121 dnsblp_map dnsbls;
|
|
122 dnsbllp_map dnsblls;
|
|
123 from_map env_from;
|
|
124 string_map env_to_dnsbll; // map recipient to a named dnsbll
|
|
125 string_map env_to_chkfrom; // map recipient to a named from map
|
8
|
126 char * content_suffix; // for sbl url body filtering
|
9
|
127 char * content_message; // ""
|
0
|
128 CONFIG();
|
|
129 ~CONFIG();
|
|
130 };
|
|
131 CONFIG::CONFIG() {
|
|
132 reference_count = 0;
|
|
133 load_time = 0;
|
8
|
134 content_suffix = NULL;
|
|
135 content_message = NULL;
|
0
|
136 }
|
|
137 CONFIG::~CONFIG() {
|
|
138 for (dnsblp_map::iterator i=dnsbls.begin(); i!=dnsbls.end(); i++) {
|
|
139 DNSBLP d = (*i).second;
|
|
140 delete d;
|
|
141 }
|
|
142 for (dnsbllp_map::iterator i=dnsblls.begin(); i!=dnsblls.end(); i++) {
|
|
143 DNSBLLP d = (*i).second;
|
|
144 delete d;
|
|
145 }
|
|
146 for (from_map::iterator i=env_from.begin(); i!=env_from.end(); i++) {
|
|
147 string_map *d = (*i).second;
|
|
148 delete d;
|
|
149 }
|
|
150 }
|
|
151
|
16
|
152 static bool debug_syslog = false;
|
18
|
153 static bool loader_run = true; // used to stop the config loader thread
|
0
|
154 static string_set all_strings; // owns all the strings, only modified by the config loader thread
|
|
155 static CONFIG * config = NULL; // protected by the config_mutex
|
|
156
|
|
157 static pthread_mutex_t config_mutex;
|
|
158 static pthread_mutex_t syslog_mutex;
|
|
159 static pthread_mutex_t resolve_mutex;
|
|
160
|
|
161
|
|
162 ////////////////////////////////////////////////
|
8
|
163 // helper to discard the strings held by a string_set
|
0
|
164 //
|
9
|
165 static void discard(string_set &s);
|
|
166 static void discard(string_set &s) {
|
8
|
167 for (string_set::iterator i=s.begin(); i!=s.end(); i++) {
|
|
168 free(*i);
|
|
169 }
|
9
|
170 s.clear();
|
8
|
171 }
|
0
|
172
|
12
|
173 ////////////////////////////////////////////////
|
|
174 // helper to register a string in a string set
|
|
175 //
|
|
176 static char* register_string(string_set &s, char *name);
|
|
177 static char* register_string(string_set &s, char *name) {
|
|
178 string_set::iterator i = s.find(name);
|
|
179 if (i != s.end()) return *i;
|
|
180 char *x = strdup(name);
|
|
181 s.insert(x);
|
|
182 return x;
|
|
183 }
|
|
184
|
16
|
185 ////////////////////////////////////////////////
|
|
186 // syslog a message
|
|
187 //
|
|
188 static void my_syslog(char *text);
|
|
189 static void my_syslog(char *text) {
|
|
190 pthread_mutex_lock(&syslog_mutex);
|
|
191 openlog("dnsbl", LOG_PID, LOG_MAIL);
|
|
192 syslog(LOG_NOTICE, "%s", text);
|
|
193 closelog();
|
|
194 pthread_mutex_unlock(&syslog_mutex);
|
|
195 }
|
|
196
|
|
197
|
12
|
198 // include the content scanner
|
|
199 #include "scanner.cpp"
|
|
200
|
|
201
|
0
|
202 ////////////////////////////////////////////////
|
|
203 // mail filter private data, held for us by sendmail
|
|
204 //
|
|
205 struct mlfiPriv
|
|
206 {
|
8
|
207 // connection specific data
|
|
208 CONFIG *pc; // global context with our maps
|
|
209 int ip; // ip4 address of the smtp client
|
|
210 map<DNSBLP, status> checked; // status from those lists
|
|
211 // message specific data
|
0
|
212 char *mailaddr; // envelope from value
|
|
213 bool authenticated; // client authenticated? if so, suppress all dnsbl checks
|
8
|
214 bool have_whites; // have at least one whitelisted recipient? need to accept content and remove all non-whitelisted recipients if it fails
|
|
215 bool only_whites; // every recipient is whitelisted?
|
|
216 url_scanner *scanner; // object to handle body scanning
|
|
217 string_set non_whites; // remember the non-whitelisted recipients so we can remove them if need be
|
12
|
218 string_set hosts; // remember the hosts that we have checked
|
0
|
219 mlfiPriv();
|
|
220 ~mlfiPriv();
|
8
|
221 void reset(bool final = false); // for a new message
|
0
|
222 };
|
|
223 mlfiPriv::mlfiPriv() {
|
|
224 pthread_mutex_lock(&config_mutex);
|
|
225 pc = config;
|
|
226 pc->reference_count++;
|
|
227 pthread_mutex_unlock(&config_mutex);
|
8
|
228 ip = 0;
|
|
229 mailaddr = NULL;
|
|
230 authenticated = false;
|
|
231 have_whites = false;
|
|
232 only_whites = true;
|
12
|
233 scanner = new url_scanner(&hosts);
|
0
|
234 }
|
|
235 mlfiPriv::~mlfiPriv() {
|
|
236 pthread_mutex_lock(&config_mutex);
|
|
237 pc->reference_count--;
|
|
238 pthread_mutex_unlock(&config_mutex);
|
8
|
239 reset(true);
|
|
240 }
|
|
241 void mlfiPriv::reset(bool final) {
|
0
|
242 if (mailaddr) free(mailaddr);
|
8
|
243 delete scanner;
|
|
244 discard(non_whites);
|
12
|
245 discard(hosts);
|
8
|
246 if (!final) {
|
|
247 mailaddr = NULL;
|
|
248 authenticated = false;
|
|
249 have_whites = false;
|
|
250 only_whites = true;
|
12
|
251 scanner = new url_scanner(&hosts);
|
8
|
252 }
|
0
|
253 }
|
|
254
|
|
255 #define MLFIPRIV ((struct mlfiPriv *) smfi_getpriv(ctx))
|
|
256
|
|
257
|
|
258 ////////////////////////////////////////////////
|
|
259 // register a global string
|
|
260 //
|
|
261 static char* register_string(char *name);
|
|
262 static char* register_string(char *name) {
|
12
|
263 return register_string(all_strings, name);
|
0
|
264 }
|
|
265
|
|
266
|
|
267 static char* next_token(char *delim);
|
|
268 static char* next_token(char *delim) {
|
|
269 char *name = strtok(NULL, delim);
|
|
270 if (!name) return name;
|
|
271 return register_string(name);
|
|
272 }
|
|
273
|
|
274
|
|
275 ////////////////////////////////////////////////
|
|
276 // lookup an email address in the env_from or env_to maps
|
|
277 //
|
|
278 static char* lookup1(char *email, string_map map);
|
|
279 static char* lookup1(char *email, string_map map) {
|
|
280 string_map::iterator i = map.find(email);
|
|
281 if (i != map.end()) return (*i).second;
|
|
282 char *x = strchr(email, '@');
|
|
283 if (!x) return DEFAULT;
|
|
284 x++;
|
|
285 i = map.find(x);
|
|
286 if (i != map.end()) return (*i).second;
|
|
287 return DEFAULT;
|
|
288 }
|
|
289
|
|
290
|
|
291 ////////////////////////////////////////////////
|
|
292 // lookup an email address in the env_from or env_to maps
|
|
293 // this email address is passed in from sendmail, and will
|
|
294 // always be enclosed in <>. It may have mixed case, just
|
|
295 // as the mail client sent it.
|
|
296 //
|
|
297 static char* lookup(char* email, string_map map);
|
|
298 static char* lookup(char* email, string_map map) {
|
|
299 int n = strlen(email)-2;
|
|
300 if (n < 1) return DEFAULT; // malformed
|
|
301 char *key = strdup(email+1);
|
|
302 key[n] = '\0';
|
|
303 for (int i=0; i<n; i++) key[i] = tolower(key[i]);
|
|
304 char *rc = lookup1(key, map);
|
|
305 free(key);
|
|
306 return rc;
|
|
307 }
|
|
308
|
|
309
|
|
310 ////////////////////////////////////////////////
|
|
311 // find the dnsbl with a specific name
|
|
312 //
|
|
313 static DNSBLP find_dnsbl(CONFIG &dc, char *name);
|
|
314 static DNSBLP find_dnsbl(CONFIG &dc, char *name) {
|
|
315 dnsblp_map::iterator i = dc.dnsbls.find(name);
|
|
316 if (i == dc.dnsbls.end()) return NULL;
|
|
317 return (*i).second;
|
|
318 }
|
|
319
|
|
320
|
|
321 ////////////////////////////////////////////////
|
|
322 // find the dnsbll with a specific name
|
|
323 //
|
|
324 static DNSBLLP find_dnsbll(CONFIG &dc, char *name);
|
|
325 static DNSBLLP find_dnsbll(CONFIG &dc, char *name) {
|
|
326 dnsbllp_map::iterator i = dc.dnsblls.find(name);
|
|
327 if (i == dc.dnsblls.end()) return NULL;
|
|
328 return (*i).second;
|
|
329 }
|
|
330
|
|
331
|
|
332 ////////////////////////////////////////////////
|
|
333 // find the envfrom map with a specific name
|
|
334 //
|
|
335 static string_map* find_from_map(CONFIG &dc, char *name);
|
|
336 static string_map* find_from_map(CONFIG &dc, char *name) {
|
|
337 from_map::iterator i = dc.env_from.find(name);
|
|
338 if (i == dc.env_from.end()) return NULL;
|
|
339 return (*i).second;
|
|
340 }
|
|
341
|
|
342
|
|
343 static string_map& really_find_from_map(CONFIG &dc, char *name);
|
|
344 static string_map& really_find_from_map(CONFIG &dc, char *name) {
|
|
345 string_map *sm = find_from_map(dc, name);
|
|
346 if (!sm) {
|
|
347 sm = new string_map;
|
|
348 dc.env_from[name] = sm;
|
|
349 }
|
|
350 return *sm;
|
|
351 }
|
|
352
|
|
353
|
|
354 ////////////////////////////////////////////////
|
8
|
355 //
|
|
356 // ask a dns question and get an A record answer - we don't try
|
|
357 // very hard, just using the default resolver retry settings.
|
|
358 // If we cannot get an answer, we just accept the mail. The
|
|
359 // caller must ensure thread safety.
|
|
360 //
|
0
|
361 //
|
8
|
362 static int dns_interface(char *question);
|
|
363 static int dns_interface(char *question) {
|
16
|
364 #ifdef NS_PACKETSZ
|
8
|
365 u_char answer[NS_PACKETSZ];
|
|
366 int length = res_search(question, ns_c_in, ns_t_a, answer, sizeof(answer));
|
|
367 if (length < 0) return 0; // error in getting answer
|
|
368 // parse the answer
|
|
369 ns_msg handle;
|
|
370 ns_rr rr;
|
|
371 if (ns_initparse(answer, length, &handle) != 0) return 0;
|
|
372 int rrnum = 0;
|
|
373 while (ns_parserr(&handle, ns_s_an, rrnum++, &rr) == 0) {
|
|
374 if (ns_rr_type(rr) == ns_t_a) {
|
|
375 int address;
|
|
376 memcpy(&address, ns_rr_rdata(rr), sizeof(address));
|
|
377 return address;
|
|
378 }
|
|
379 }
|
|
380 return 0;
|
16
|
381 #else
|
|
382 struct hostent *host = gethostbyname(question);
|
|
383 if (!host) return 0;
|
|
384 if (host->h_addrtype != AF_INET) return 0;
|
|
385 int address;
|
|
386 memcpy(&address, host->h_addr, sizeof(address));
|
|
387 return address;
|
|
388 #endif
|
8
|
389 }
|
|
390
|
|
391 static int protected_dns_interface(char *question);
|
|
392 static int protected_dns_interface(char *question) {
|
|
393 int ans;
|
|
394 pthread_mutex_lock(&resolve_mutex);
|
|
395 ans = dns_interface(question);
|
|
396 pthread_mutex_unlock(&resolve_mutex);
|
|
397 return ans;
|
|
398
|
|
399 }
|
|
400
|
|
401 ////////////////////////////////////////////////
|
|
402 // check a single dnsbl
|
|
403 //
|
|
404 static status check_single(int ip, char *suffix);
|
|
405 static status check_single(int ip, char *suffix) {
|
0
|
406 // make a dns question
|
|
407 const u_char *src = (const u_char *)&ip;
|
|
408 if (src[0] == 127) return oksofar; // don't do dns lookups on localhost
|
16
|
409 #ifdef NS_MAXDNAME
|
0
|
410 char question[NS_MAXDNAME];
|
16
|
411 #else
|
|
412 char question[1000];
|
|
413 #endif
|
8
|
414 snprintf(question, sizeof(question), "%u.%u.%u.%u.%s.", src[3], src[2], src[1], src[0], suffix);
|
|
415 // ask the question, if we get an A record it implies a blacklisted ip address
|
|
416 return (protected_dns_interface(question)) ? reject : oksofar;
|
|
417 }
|
|
418
|
|
419
|
|
420 ////////////////////////////////////////////////
|
|
421 // check a single dnsbl
|
|
422 //
|
|
423 static status check_single(int ip, DNSBL &bl);
|
|
424 static status check_single(int ip, DNSBL &bl) {
|
|
425 return check_single(ip, bl.suffix);
|
0
|
426 }
|
|
427
|
|
428
|
|
429 ////////////////////////////////////////////////
|
|
430 // check the dnsbls specified for this recipient
|
|
431 //
|
|
432 static status check_dnsbl(mlfiPriv &priv, DNSBLLP dnsbllp, DNSBLP &rejectlist);
|
|
433 static status check_dnsbl(mlfiPriv &priv, DNSBLLP dnsbllp, DNSBLP &rejectlist) {
|
|
434 if (priv.authenticated) return oksofar;
|
|
435 if (!dnsbllp) return oksofar;
|
|
436 DNSBLL &dnsbll = *dnsbllp;
|
|
437 for (DNSBLL::iterator i=dnsbll.begin(); i!=dnsbll.end(); i++) {
|
|
438 DNSBLP dp = *i; // non null by construction
|
|
439 status st;
|
|
440 map<DNSBLP, status>::iterator f = priv.checked.find(dp);
|
|
441 if (f == priv.checked.end()) {
|
|
442 // have not checked this list yet
|
8
|
443 st = check_single(priv.ip, *dp);
|
0
|
444 rejectlist = dp;
|
|
445 priv.checked[dp] = st;
|
|
446 }
|
|
447 else {
|
|
448 st = (*f).second;
|
|
449 rejectlist = (*f).first;
|
|
450 }
|
|
451 if (st == reject) return st;
|
|
452 }
|
|
453 return oksofar;
|
|
454 }
|
|
455
|
|
456
|
|
457 ////////////////////////////////////////////////
|
8
|
458 // check the dnsbls specified for this recipient
|
|
459 //
|
16
|
460 static status check_hosts(mlfiPriv &priv, char *&host, int &ip);
|
|
461 static status check_hosts(mlfiPriv &priv, char *&host, int &ip) {
|
8
|
462 CONFIG &dc = *priv.pc;
|
|
463 if (!dc.content_suffix) return oksofar;
|
|
464 int count = 0;
|
12
|
465 for (string_set::iterator i=priv.hosts.begin(); i!=priv.hosts.end(); i++) {
|
8
|
466 count++;
|
12
|
467 if (count > 20) return oksofar; // silly to check too many hosts
|
16
|
468 host = *i;
|
|
469 if (debug_syslog) {
|
|
470 char buf[200];
|
|
471 snprintf(buf, sizeof(buf), "looking for host %s", host);
|
|
472 my_syslog(buf);
|
|
473 }
|
|
474 ip = protected_dns_interface(host);
|
8
|
475 if (ip) {
|
16
|
476 if (debug_syslog) {
|
|
477 char adr[sizeof "255.255.255.255"];
|
|
478 adr[0] = '\0';
|
|
479 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr));
|
|
480 char buf[200];
|
|
481 snprintf(buf, sizeof(buf), "found host %s at %s", host, adr);
|
|
482 my_syslog(buf);
|
|
483 }
|
8
|
484 status st = check_single(ip, dc.content_suffix);
|
|
485 if (st == reject) return st;
|
|
486 }
|
|
487 }
|
9
|
488 return oksofar;
|
8
|
489 }
|
|
490
|
|
491
|
|
492 ////////////////////////////////////////////////
|
0
|
493 // start of sendmail milter interfaces
|
|
494 //
|
|
495 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr)
|
|
496 {
|
|
497 // allocate some private memory
|
|
498 mlfiPriv *priv = new mlfiPriv;
|
|
499 if (hostaddr->sa_family == AF_INET) {
|
|
500 priv->ip = ((struct sockaddr_in *)hostaddr)->sin_addr.s_addr;
|
|
501 }
|
|
502
|
|
503 // save the private data
|
|
504 smfi_setpriv(ctx, (void*)priv);
|
|
505
|
|
506 // continue processing
|
|
507 return SMFIS_CONTINUE;
|
|
508 }
|
|
509
|
|
510 sfsistat mlfi_envfrom(SMFICTX *ctx, char **from)
|
|
511 {
|
|
512 mlfiPriv &priv = *MLFIPRIV;
|
|
513 priv.mailaddr = strdup(from[0]);
|
|
514 priv.authenticated = (smfi_getsymval(ctx, "{auth_authen}") != NULL);
|
|
515 return SMFIS_CONTINUE;
|
|
516 }
|
|
517
|
|
518 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **rcpt)
|
|
519 {
|
|
520 DNSBLP rejectlist = NULL; // list that caused the reject
|
|
521 status st = oksofar;
|
|
522 mlfiPriv &priv = *MLFIPRIV;
|
|
523 CONFIG &dc = *priv.pc;
|
|
524 char *rcptaddr = rcpt[0];
|
|
525 char *dnsname = lookup(rcptaddr, dc.env_to_dnsbll);
|
|
526 char *fromname = lookup(rcptaddr, dc.env_to_chkfrom);
|
|
527 if ((strcmp(dnsname, BLACK) == 0) ||
|
|
528 (strcmp(fromname, BLACK) == 0)) {
|
|
529 st = black; // two options to blacklist this recipient
|
|
530 }
|
|
531 else if (strcmp(fromname, WHITE) == 0) {
|
|
532 st = white;
|
|
533 }
|
|
534 else {
|
|
535 // check an env_from map
|
|
536 string_map *sm = find_from_map(dc, fromname);
|
|
537 if (sm != NULL) {
|
|
538 fromname = lookup(priv.mailaddr, *sm); // returns default if name not in map
|
|
539 if (strcmp(fromname, BLACK) == 0) {
|
|
540 st = black; // blacklist this envelope from value
|
|
541 }
|
|
542 if (strcmp(fromname, WHITE) == 0) {
|
|
543 st = white; // blacklist this envelope from value
|
|
544 }
|
|
545 }
|
|
546 }
|
|
547 if ((st == oksofar) && (strcmp(dnsname, WHITE) != 0)) {
|
|
548 // check dns lists
|
|
549 st = check_dnsbl(priv, find_dnsbll(dc, dnsname), rejectlist);
|
|
550 }
|
|
551
|
|
552 if (st == reject) {
|
|
553 // reject the recipient based on some dnsbl
|
|
554 char adr[sizeof "255.255.255.255"];
|
|
555 adr[0] = '\0';
|
8
|
556 inet_ntop(AF_INET, (const u_char *)&priv.ip, adr, sizeof(adr));
|
0
|
557 char buf[2000];
|
|
558 snprintf(buf, sizeof(buf), rejectlist->message, adr, adr);
|
|
559 smfi_setreply(ctx, "550", "5.7.1", buf);
|
|
560 return SMFIS_REJECT;
|
|
561 }
|
|
562 else if (st == black) {
|
|
563 // reject the recipient based on blacklisting either from or to
|
|
564 smfi_setreply(ctx, "550", "5.7.1", "no such user");
|
|
565 return SMFIS_REJECT;
|
|
566 }
|
|
567 else {
|
|
568 // accept the recipient
|
8
|
569 if (st == oksofar) {
|
|
570 // but remember the non-whites
|
12
|
571 register_string(priv.non_whites, rcptaddr);
|
8
|
572 priv.only_whites = false;
|
|
573 }
|
|
574 if (st == white) {
|
|
575 priv.have_whites = true;
|
|
576 }
|
0
|
577 return SMFIS_CONTINUE;
|
|
578 }
|
|
579 }
|
|
580
|
8
|
581 sfsistat mlfi_body(SMFICTX *ctx, u_char *data, size_t len)
|
0
|
582 {
|
|
583 mlfiPriv &priv = *MLFIPRIV;
|
8
|
584 if (priv.authenticated) return SMFIS_CONTINUE;
|
|
585 if (priv.only_whites) return SMFIS_CONTINUE;
|
|
586 priv.scanner->scan(data, len);
|
11
|
587 return SMFIS_CONTINUE;
|
8
|
588 }
|
|
589
|
|
590 sfsistat mlfi_eom(SMFICTX *ctx)
|
|
591 {
|
|
592 sfsistat rc;
|
|
593 mlfiPriv &priv = *MLFIPRIV;
|
16
|
594 char *host = NULL;
|
8
|
595 int ip;
|
|
596 // process end of message
|
|
597 if (priv.authenticated ||
|
|
598 priv.only_whites ||
|
16
|
599 (check_hosts(priv, host, ip) == oksofar)) rc = SMFIS_CONTINUE;
|
8
|
600 else {
|
|
601 if (!priv.have_whites) {
|
|
602 // can reject the entire message
|
|
603 char adr[sizeof "255.255.255.255"];
|
|
604 adr[0] = '\0';
|
|
605 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr));
|
|
606 char buf[2000];
|
16
|
607 snprintf(buf, sizeof(buf), priv.pc->content_message, host, adr);
|
8
|
608 smfi_setreply(ctx, "550", "5.7.1", buf);
|
|
609 rc = SMFIS_REJECT;
|
|
610 }
|
|
611 else {
|
|
612 // need to accept it but remove the recipients that don't want it
|
|
613 for (string_set::iterator i=priv.non_whites.begin(); i!=priv.non_whites.end(); i++) {
|
|
614 char *rcpt = *i;
|
|
615 smfi_delrcpt(ctx, rcpt);
|
|
616 }
|
|
617 rc = SMFIS_CONTINUE;
|
|
618 }
|
0
|
619 }
|
8
|
620 // reset for a new message on the same connection
|
|
621 mlfi_abort(ctx);
|
|
622 return rc;
|
|
623 }
|
|
624
|
|
625 sfsistat mlfi_abort(SMFICTX *ctx)
|
|
626 {
|
|
627 mlfiPriv &priv = *MLFIPRIV;
|
|
628 priv.reset();
|
0
|
629 return SMFIS_CONTINUE;
|
|
630 }
|
|
631
|
|
632 sfsistat mlfi_close(SMFICTX *ctx)
|
|
633 {
|
|
634 mlfiPriv *priv = MLFIPRIV;
|
|
635 if (!priv) return SMFIS_CONTINUE;
|
|
636 delete priv;
|
|
637 smfi_setpriv(ctx, NULL);
|
|
638 return SMFIS_CONTINUE;
|
|
639 }
|
|
640
|
|
641 struct smfiDesc smfilter =
|
|
642 {
|
|
643 "DNSBL", // filter name
|
|
644 SMFI_VERSION, // version code -- do not change
|
|
645 SMFIF_DELRCPT, // flags
|
|
646 mlfi_connect, // connection info filter
|
|
647 NULL, // SMTP HELO command filter
|
|
648 mlfi_envfrom, // envelope sender filter
|
|
649 mlfi_envrcpt, // envelope recipient filter
|
|
650 NULL, // header filter
|
|
651 NULL, // end of header
|
8
|
652 mlfi_body, // body block filter
|
|
653 mlfi_eom, // end of message
|
|
654 mlfi_abort, // message aborted
|
0
|
655 mlfi_close, // connection cleanup
|
|
656 };
|
|
657
|
|
658
|
|
659 static void dumpit(char *name, string_map map);
|
|
660 static void dumpit(char *name, string_map map) {
|
9
|
661 fprintf(stdout, "\n");
|
0
|
662 for (string_map::iterator i=map.begin(); i!=map.end(); i++) {
|
9
|
663 fprintf(stdout, "%s %s->%s\n", name, (*i).first, (*i).second);
|
0
|
664 }
|
|
665 }
|
|
666
|
|
667
|
|
668 static void dumpit(from_map map);
|
|
669 static void dumpit(from_map map) {
|
|
670 for (from_map::iterator i=map.begin(); i!=map.end(); i++) {
|
3
|
671 char buf[2000];
|
|
672 snprintf(buf, sizeof(buf), "envelope from map for %s", (*i).first);
|
0
|
673 string_map *sm = (*i).second;
|
3
|
674 dumpit(buf, *sm);
|
0
|
675 }
|
|
676 }
|
|
677
|
|
678
|
3
|
679 static void dumpit(CONFIG &dc);
|
|
680 static void dumpit(CONFIG &dc) {
|
5
|
681 dumpit(dc.env_from);
|
|
682 dumpit("envelope to (dnsbl list)", dc.env_to_dnsbll);
|
|
683 dumpit("envelope to (from map)", dc.env_to_chkfrom);
|
9
|
684 fprintf(stdout, "\ndnsbls\n");
|
0
|
685 for (dnsblp_map::iterator i=dc.dnsbls.begin(); i!=dc.dnsbls.end(); i++) {
|
9
|
686 fprintf(stdout, "%s %s %s\n", (*i).first, (*i).second->suffix, (*i).second->message);
|
0
|
687 }
|
9
|
688 fprintf(stdout, "\ndnsbl_lists\n");
|
0
|
689 for (dnsbllp_map::iterator i=dc.dnsblls.begin(); i!=dc.dnsblls.end(); i++) {
|
|
690 char *name = (*i).first;
|
|
691 DNSBLL &dl = *((*i).second);
|
9
|
692 fprintf(stdout, "%s", name);
|
0
|
693 for (DNSBLL::iterator j=dl.begin(); j!=dl.end(); j++) {
|
|
694 DNSBL &d = **j;
|
9
|
695 fprintf(stdout, " %s", d.suffix);
|
0
|
696 }
|
9
|
697 fprintf(stdout, "\n");
|
0
|
698 }
|
9
|
699 if (dc.content_suffix) {
|
|
700 fprintf(stdout, "\ncontent filtering enabled with %s %s\n", dc.content_suffix, dc.content_message);
|
|
701 }
|
|
702 fprintf(stdout, "\nfiles\n");
|
3
|
703 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) {
|
|
704 char *f = *i;
|
9
|
705 fprintf(stdout, "config includes %s\n", f);
|
3
|
706 }
|
|
707 }
|
|
708
|
|
709
|
|
710 ////////////////////////////////////////////////
|
|
711 // check for redundant or recursive include files
|
|
712 //
|
|
713 static bool ok_to_include(CONFIG &dc, char *fn);
|
|
714 static bool ok_to_include(CONFIG &dc, char *fn) {
|
|
715 if (!fn) return false;
|
|
716 bool ok = true;
|
|
717 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) {
|
|
718 char *f = *i;
|
|
719 if (strcmp(f, fn) == 0) {
|
|
720 my_syslog("redundant or recursive include file detected");
|
|
721 ok = false;
|
|
722 break;
|
|
723 }
|
|
724 }
|
|
725 return ok;
|
0
|
726 }
|
|
727
|
|
728
|
|
729 ////////////////////////////////////////////////
|
|
730 // load a single config file
|
|
731 //
|
3
|
732 static void load_conf_dcc(CONFIG &dc, char *name, char *fn);
|
|
733 static void load_conf_dcc(CONFIG &dc, char *name, char *fn) {
|
|
734 dc.config_files.push_back(fn);
|
|
735 char *list = BLACK;
|
|
736 const int LINE_SIZE = 2000;
|
|
737 ifstream is(fn);
|
|
738 if (is.fail()) return;
|
|
739 char line[LINE_SIZE];
|
|
740 char *delim = " \t";
|
|
741 int curline = 0;
|
|
742 while (!is.eof()) {
|
|
743 is.getline(line, LINE_SIZE);
|
|
744 curline++;
|
|
745 int n = strlen(line);
|
|
746 if (!n) continue;
|
|
747 for (int i=0; i<n; i++) line[i] = tolower(line[i]);
|
|
748 if (line[0] == '#') continue;
|
|
749 char *head = line;
|
|
750 if (strspn(line, delim) == 0) {
|
|
751 // have a leading ok/many tag to fetch
|
|
752 char *cmd = strtok(line, delim);
|
|
753 if (strcmp(cmd, MANY) == 0) list = BLACK;
|
|
754 else if (strcmp(cmd, OK) == 0) list = WHITE;
|
|
755 head = cmd + strlen(cmd) + 1;
|
|
756 }
|
|
757 char *cmd = strtok(head, delim);
|
|
758 if (!cmd) continue;
|
|
759 if (strcmp(cmd, "env_from") == 0) {
|
|
760 char *from = next_token(delim);
|
|
761 if (from) {
|
|
762 string_map &fm = really_find_from_map(dc, name);
|
|
763 fm[from] = list;
|
|
764 }
|
|
765 }
|
|
766 else if (strcmp(cmd, "env_to") == 0) {
|
|
767 char *to = next_token(delim);
|
|
768 if (to) {
|
|
769 dc.env_to_dnsbll[to] = list;
|
|
770 dc.env_to_chkfrom[to] = list;
|
|
771 }
|
|
772 }
|
|
773 else if (strcmp(cmd, "substitute") == 0) {
|
|
774 char *tag = next_token(delim);
|
|
775 if (tag && (strcmp(tag, "mail_host") == 0)) {
|
|
776 char *from = next_token(delim);
|
|
777 if (from) {
|
|
778 string_map &fm = really_find_from_map(dc, name);
|
|
779 fm[from] = list;
|
|
780 }
|
|
781 }
|
|
782 }
|
|
783 else if (strcmp(cmd, "include") == 0) {
|
|
784 char *fn = next_token(delim);
|
|
785 if (ok_to_include(dc, fn)) {
|
|
786 load_conf_dcc(dc, name, fn);
|
|
787 }
|
|
788 }
|
|
789
|
|
790 }
|
|
791 is.close();
|
|
792 }
|
|
793
|
|
794
|
0
|
795 static void load_conf(CONFIG &dc, char *fn);
|
|
796 static void load_conf(CONFIG &dc, char *fn) {
|
|
797 dc.config_files.push_back(fn);
|
|
798 map<char*, int, ltstr> commands;
|
8
|
799 enum {dummy, content, dnsbl, dnsbll, envfrom, envto, include, includedcc};
|
|
800 commands["content" ] = content;
|
3
|
801 commands["dnsbl" ] = dnsbl;
|
|
802 commands["dnsbl_list" ] = dnsbll;
|
|
803 commands["env_from" ] = envfrom;
|
|
804 commands["env_to" ] = envto;
|
|
805 commands["include" ] = include;
|
|
806 commands["include_dcc"] = includedcc;
|
0
|
807 const int LINE_SIZE = 2000;
|
|
808 ifstream is(fn);
|
|
809 if (is.fail()) return;
|
|
810 char line[LINE_SIZE];
|
|
811 char orig[LINE_SIZE];
|
|
812 char *delim = " \t";
|
|
813 int curline = 0;
|
|
814 while (!is.eof()) {
|
|
815 is.getline(line, LINE_SIZE);
|
|
816 snprintf(orig, sizeof(orig), "%s", line);
|
|
817 curline++;
|
|
818 int n = strlen(line);
|
|
819 for (int i=0; i<n; i++) line[i] = tolower(line[i]);
|
|
820 char *cmd = strtok(line, delim);
|
|
821 if (cmd && (cmd[0] != '#') && (cmd[0] != '\0')) {
|
|
822 // have a decent command
|
|
823 bool processed = false;
|
|
824 switch (commands[cmd]) {
|
8
|
825 case content: {
|
|
826 char *suff = strtok(NULL, delim);
|
|
827 if (!suff) break; // no dns suffic
|
|
828 char *msg = suff + strlen(suff);
|
|
829 if ((msg - line) >= strlen(orig)) break; // line ended with the dns suffix
|
|
830 msg = strchr(msg+1, '\'');
|
|
831 if (!msg) break; // no reply message template
|
|
832 msg++; // move over the leading '
|
|
833 if ((msg - line) >= strlen(orig)) break; // line ended with the leading quote
|
|
834 char *last = strchr(msg, '\'');
|
|
835 if (!last) break; // no trailing quote
|
|
836 *last = '\0'; // make it a null terminator
|
|
837 dc.content_suffix = register_string(suff);
|
|
838 dc.content_message = register_string(msg);
|
|
839 processed = true;
|
|
840 } break;
|
|
841
|
0
|
842 case dnsbl: {
|
|
843 // have a new dnsbl to use
|
|
844 char *name = next_token(delim);
|
|
845 if (!name) break; // no name name
|
|
846 if (find_dnsbl(dc, name)) break; // duplicate entry
|
|
847 char *suff = strtok(NULL, delim);
|
|
848 if (!suff) break; // no dns suffic
|
|
849 char *msg = suff + strlen(suff);
|
|
850 if ((msg - line) >= strlen(orig)) break; // line ended with the dns suffix
|
|
851 msg = strchr(msg+1, '\'');
|
|
852 if (!msg) break; // no reply message template
|
|
853 msg++; // move over the leading '
|
|
854 if ((msg - line) >= strlen(orig)) break; // line ended with the leading quote
|
|
855 char *last = strchr(msg, '\'');
|
|
856 if (!last) break; // no trailing quote
|
|
857 *last = '\0'; // make it a null terminator
|
|
858 dc.dnsbls[name] = new DNSBL(register_string(suff), register_string(msg));
|
|
859 processed = true;
|
|
860 } break;
|
|
861
|
|
862 case dnsbll: {
|
|
863 // define a new combination of dnsbls
|
|
864 char *name = next_token(delim);
|
|
865 if (!name) break;
|
|
866 if (find_dnsbll(dc, name)) break; // duplicate entry
|
|
867 char *list = next_token(delim);
|
|
868 if (!list || (*list == '\0') || (*list == '#')) break;
|
|
869 DNSBLLP d = new DNSBLL;
|
|
870 DNSBLP p = find_dnsbl(dc, list);
|
|
871 if (p) d->push_back(p);
|
|
872 while (true) {
|
|
873 list = next_token(delim);
|
|
874 if (!list || (*list == '\0') || (*list == '#')) break;
|
|
875 DNSBLP p = find_dnsbl(dc, list);
|
|
876 if (p) d->push_back(p);
|
|
877 }
|
|
878 dc.dnsblls[name] = d;
|
|
879 processed = true;
|
|
880 } break;
|
|
881
|
|
882 case envfrom: {
|
|
883 // add an entry into the named string_map
|
|
884 char *name = next_token(delim);
|
|
885 if (!name) break;
|
|
886 char *from = next_token(delim);
|
|
887 if (!from) break;
|
|
888 char *list = next_token(delim);
|
|
889 if (!list) break;
|
|
890 if ((strcmp(list, WHITE) == 0) ||
|
|
891 (strcmp(list, BLACK) == 0)) {
|
|
892 string_map &fm = really_find_from_map(dc, name);
|
|
893 fm[from] = list;
|
|
894 processed = true;
|
|
895 }
|
|
896 else {
|
|
897 // list may be the name of a previously defined from_map
|
|
898 string_map *m = find_from_map(dc, list);
|
|
899 if (m && (strcmp(list,name) != 0)) {
|
|
900 string_map &pm = *m;
|
|
901 string_map &fm = really_find_from_map(dc, name);
|
|
902 fm.insert(pm.begin(), pm.end());
|
|
903 processed = true;
|
|
904 }
|
|
905 }
|
|
906 } break;
|
|
907
|
|
908 case envto: {
|
|
909 // define the dnsbl_list and env_from maps to use for this recipient
|
|
910 char *to = next_token(delim);
|
|
911 if (!to) break;
|
|
912 char *list = next_token(delim);
|
|
913 if (!list) break;
|
|
914 char *from = next_token(delim);
|
|
915 if (!from) break;
|
|
916 dc.env_to_dnsbll[to] = list;
|
|
917 dc.env_to_chkfrom[to] = from;
|
|
918 processed = true;
|
|
919 } break;
|
|
920
|
|
921 case include: {
|
|
922 char *fn = next_token(delim);
|
3
|
923 if (ok_to_include(dc, fn)) {
|
|
924 load_conf(dc, fn);
|
|
925 processed = true;
|
|
926 }
|
|
927 } break;
|
|
928
|
|
929 case includedcc: {
|
|
930 char *name = next_token(delim);
|
|
931 if (!name) break;
|
|
932 char *fn = next_token(delim);
|
|
933 if (ok_to_include(dc, fn)) {
|
|
934 load_conf_dcc(dc, name, fn);
|
|
935 processed = true;
|
0
|
936 }
|
|
937 } break;
|
|
938
|
|
939 default: {
|
|
940 } break;
|
|
941 }
|
|
942 if (!processed) {
|
|
943 pthread_mutex_lock(&syslog_mutex);
|
|
944 openlog("dnsbl", LOG_PID, LOG_MAIL);
|
|
945 syslog(LOG_ERR, "ignoring file %s line %d : %s\n", fn, curline, orig);
|
|
946 closelog();
|
|
947 pthread_mutex_unlock(&syslog_mutex);
|
|
948 }
|
|
949 }
|
|
950 }
|
|
951 is.close();
|
|
952 }
|
|
953
|
|
954
|
|
955 ////////////////////////////////////////////////
|
|
956 // reload the config
|
|
957 //
|
|
958 static CONFIG* new_conf();
|
|
959 static CONFIG* new_conf() {
|
|
960 my_syslog("loading new configuration");
|
|
961 CONFIG *newc = new CONFIG;
|
|
962 load_conf(*newc, "dnsbl.conf");
|
|
963 newc->load_time = time(NULL);
|
|
964 return newc;
|
|
965 }
|
|
966
|
|
967
|
|
968 ////////////////////////////////////////////////
|
|
969 // thread to watch the old config files for changes
|
|
970 // and reload when needed. we also cleanup old
|
|
971 // configs whose reference count has gone to zero.
|
|
972 //
|
|
973 static void* config_loader(void *arg);
|
|
974 static void* config_loader(void *arg) {
|
|
975 typedef set<CONFIG *> configp_set;
|
|
976 configp_set old_configs;
|
18
|
977 while (loader_run) {
|
0
|
978 sleep(180); // look for modifications every 3 minutes
|
18
|
979 if (!loader_run) break;
|
0
|
980 CONFIG &dc = *config;
|
|
981 time_t then = dc.load_time;
|
|
982 struct stat st;
|
|
983 bool reload = false;
|
|
984 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) {
|
|
985 char *fn = *i;
|
|
986 if (stat(fn, &st)) reload = true; // file disappeared
|
|
987 else if (st.st_mtime > then) reload = true; // file modified
|
|
988 if (reload) break;
|
|
989 }
|
|
990 if (reload) {
|
|
991 CONFIG *newc = new_conf();
|
|
992 // replace the global config pointer
|
|
993 pthread_mutex_lock(&config_mutex);
|
|
994 CONFIG *old = config;
|
|
995 config = newc;
|
|
996 pthread_mutex_unlock(&config_mutex);
|
|
997 if (old) old_configs.insert(old);
|
|
998 }
|
|
999 // now look for old configs with zero ref counts
|
|
1000 for (configp_set::iterator i=old_configs.begin(); i!=old_configs.end(); ) {
|
|
1001 CONFIG *old = *i;
|
|
1002 if (!old->reference_count) {
|
|
1003 delete old; // destructor does all the work
|
|
1004 old_configs.erase(i++);
|
|
1005 }
|
|
1006 else i++;
|
|
1007 }
|
|
1008 }
|
18
|
1009 return NULL;
|
0
|
1010 }
|
|
1011
|
|
1012
|
|
1013 static void usage(char *prog);
|
|
1014 static void usage(char *prog)
|
|
1015 {
|
16
|
1016 fprintf(stderr, "Usage: %s [-d] [-c] -p socket-addr [-t timeout]\n", prog);
|
0
|
1017 fprintf(stderr, "where socket-addr is for the connection to sendmail and should be one of\n");
|
|
1018 fprintf(stderr, " inet:port@local-ip-address\n");
|
|
1019 fprintf(stderr, " local:local-domain-socket-file-name\n");
|
9
|
1020 fprintf(stderr, "-c will load and dump the config to stdout\n");
|
16
|
1021 fprintf(stderr, "-d will add some syslog debug messages\n");
|
0
|
1022 }
|
|
1023
|
|
1024
|
|
1025 int main(int argc, char**argv)
|
|
1026 {
|
3
|
1027 bool check = false;
|
|
1028 bool setconn = false;
|
0
|
1029 int c;
|
16
|
1030 const char *args = "p:t:hcd";
|
0
|
1031 extern char *optarg;
|
|
1032
|
|
1033 // Process command line options
|
|
1034 while ((c = getopt(argc, argv, args)) != -1) {
|
|
1035 switch (c) {
|
|
1036 case 'p':
|
|
1037 if (optarg == NULL || *optarg == '\0') {
|
|
1038 fprintf(stderr, "Illegal conn: %s\n", optarg);
|
|
1039 exit(EX_USAGE);
|
|
1040 }
|
|
1041 if (smfi_setconn(optarg) == MI_FAILURE) {
|
|
1042 fprintf(stderr, "smfi_setconn failed\n");
|
|
1043 exit(EX_SOFTWARE);
|
|
1044 }
|
|
1045
|
|
1046 if (strncasecmp(optarg, "unix:", 5) == 0) unlink(optarg + 5);
|
|
1047 else if (strncasecmp(optarg, "local:", 6) == 0) unlink(optarg + 6);
|
3
|
1048 setconn = true;
|
0
|
1049 break;
|
|
1050
|
|
1051 case 't':
|
|
1052 if (optarg == NULL || *optarg == '\0') {
|
|
1053 fprintf(stderr, "Illegal timeout: %s\n", optarg);
|
|
1054 exit(EX_USAGE);
|
|
1055 }
|
|
1056 if (smfi_settimeout(atoi(optarg)) == MI_FAILURE) {
|
|
1057 fprintf(stderr, "smfi_settimeout failed\n");
|
|
1058 exit(EX_SOFTWARE);
|
|
1059 }
|
|
1060 break;
|
|
1061
|
3
|
1062 case 'c':
|
|
1063 check = true;
|
|
1064 break;
|
|
1065
|
16
|
1066 case 'd':
|
|
1067 debug_syslog = true;
|
|
1068 break;
|
|
1069
|
0
|
1070 case 'h':
|
|
1071 default:
|
|
1072 usage(argv[0]);
|
|
1073 exit(EX_USAGE);
|
|
1074 }
|
|
1075 }
|
5
|
1076
|
|
1077 if (check) {
|
|
1078 CONFIG &dc = *new_conf();
|
|
1079 dumpit(dc);
|
|
1080 return 0;
|
|
1081 }
|
|
1082
|
0
|
1083 if (!setconn) {
|
|
1084 fprintf(stderr, "%s: Missing required -p argument\n", argv[0]);
|
|
1085 usage(argv[0]);
|
|
1086 exit(EX_USAGE);
|
|
1087 }
|
5
|
1088
|
0
|
1089 if (smfi_register(smfilter) == MI_FAILURE) {
|
|
1090 fprintf(stderr, "smfi_register failed\n");
|
|
1091 exit(EX_UNAVAILABLE);
|
|
1092 }
|
|
1093
|
|
1094 // switch to background mode
|
|
1095 if (daemon(1,0) < 0) {
|
|
1096 fprintf(stderr, "daemon() call failed\n");
|
|
1097 exit(EX_UNAVAILABLE);
|
|
1098 }
|
|
1099
|
|
1100 // initialize the thread sync objects
|
|
1101 pthread_mutex_init(&config_mutex, 0);
|
|
1102 pthread_mutex_init(&syslog_mutex, 0);
|
|
1103 pthread_mutex_init(&resolve_mutex, 0);
|
|
1104
|
|
1105 // load the initial config
|
|
1106 config = new_conf();
|
|
1107
|
|
1108 // only create threads after the fork() in daemon
|
|
1109 pthread_t tid;
|
|
1110 if (pthread_create(&tid, 0, config_loader, 0))
|
|
1111 my_syslog("failed to create config loader thread");
|
|
1112 if (pthread_detach(tid))
|
|
1113 my_syslog("failed to detach config loader thread");
|
|
1114
|
|
1115 // write the pid
|
|
1116 const char *pidpath = "/var/run/dnsbl.pid";
|
|
1117 unlink(pidpath);
|
|
1118 FILE *f = fopen(pidpath, "w");
|
|
1119 if (f) {
|
22
|
1120 #ifdef linux
|
|
1121 // from a comment in the DCC source code:
|
|
1122 // Linux threads are broken. Signals given the
|
|
1123 // original process are delivered to only the
|
|
1124 // thread that happens to have that PID. The
|
|
1125 // sendmail libmilter thread that needs to hear
|
|
1126 // SIGINT and other signals does not, and that breaks
|
|
1127 // scripts that need to stop milters.
|
|
1128 // However, signaling the process group works.
|
0
|
1129 fprintf(f, "-%d\n", (u_int)getpgrp());
|
22
|
1130 #else
|
|
1131 fprintf(f, "%d\n", (u_int)getpid());
|
|
1132 #endif
|
0
|
1133 fclose(f);
|
|
1134 }
|
|
1135
|
18
|
1136 time_t starting = time(NULL);
|
|
1137 int rc = smfi_main();
|
22
|
1138 if ((rc != MI_SUCCESS) && (time(NULL) > starting+5*60)) {
|
18
|
1139 my_syslog("trying to restart after smfi_main()");
|
|
1140 loader_run = false; // eventually the config loader thread will terminate
|
|
1141 execvp(argv[0], argv);
|
|
1142 }
|
|
1143 exit((rc == MI_SUCCESS) ? 0 : EX_UNAVAILABLE);
|
0
|
1144 }
|
8
|
1145
|