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