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