Mercurial > dnsbl
annotate src/dnsbl.cpp @ 26:fdae7ab30cfc
allow normal and terminator versions of tags
author | carl |
---|---|
date | Fri, 21 May 2004 21:55:38 -0700 |
parents | 2e23b7184d2b |
children | 43a4f6b3e668 |
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; |
26 | 508 int bin = priv.memory->binary_tags; |
24 | 509 int bad = priv.memory->bad_html_tags; |
510 int lim = priv.pc->bad_tag_limit; | |
26 | 511 if (bin > bad) return oksofar; // probably .zip or .tar.gz with random content |
24 | 512 if ((bad > lim) && (lim > 0)) return reject; |
9 | 513 return oksofar; |
8 | 514 } |
515 | |
516 | |
517 //////////////////////////////////////////////// | |
0 | 518 // start of sendmail milter interfaces |
519 // | |
520 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr) | |
521 { | |
522 // allocate some private memory | |
523 mlfiPriv *priv = new mlfiPriv; | |
524 if (hostaddr->sa_family == AF_INET) { | |
525 priv->ip = ((struct sockaddr_in *)hostaddr)->sin_addr.s_addr; | |
526 } | |
527 | |
528 // save the private data | |
529 smfi_setpriv(ctx, (void*)priv); | |
530 | |
531 // continue processing | |
532 return SMFIS_CONTINUE; | |
533 } | |
534 | |
535 sfsistat mlfi_envfrom(SMFICTX *ctx, char **from) | |
536 { | |
537 mlfiPriv &priv = *MLFIPRIV; | |
538 priv.mailaddr = strdup(from[0]); | |
539 priv.authenticated = (smfi_getsymval(ctx, "{auth_authen}") != NULL); | |
540 return SMFIS_CONTINUE; | |
541 } | |
542 | |
543 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **rcpt) | |
544 { | |
545 DNSBLP rejectlist = NULL; // list that caused the reject | |
546 status st = oksofar; | |
547 mlfiPriv &priv = *MLFIPRIV; | |
548 CONFIG &dc = *priv.pc; | |
549 char *rcptaddr = rcpt[0]; | |
550 char *dnsname = lookup(rcptaddr, dc.env_to_dnsbll); | |
551 char *fromname = lookup(rcptaddr, dc.env_to_chkfrom); | |
552 if ((strcmp(dnsname, BLACK) == 0) || | |
553 (strcmp(fromname, BLACK) == 0)) { | |
554 st = black; // two options to blacklist this recipient | |
555 } | |
556 else if (strcmp(fromname, WHITE) == 0) { | |
557 st = white; | |
558 } | |
559 else { | |
560 // check an env_from map | |
561 string_map *sm = find_from_map(dc, fromname); | |
562 if (sm != NULL) { | |
563 fromname = lookup(priv.mailaddr, *sm); // returns default if name not in map | |
564 if (strcmp(fromname, BLACK) == 0) { | |
565 st = black; // blacklist this envelope from value | |
566 } | |
567 if (strcmp(fromname, WHITE) == 0) { | |
568 st = white; // blacklist this envelope from value | |
569 } | |
570 } | |
571 } | |
572 if ((st == oksofar) && (strcmp(dnsname, WHITE) != 0)) { | |
573 // check dns lists | |
574 st = check_dnsbl(priv, find_dnsbll(dc, dnsname), rejectlist); | |
575 } | |
576 | |
577 if (st == reject) { | |
578 // reject the recipient based on some dnsbl | |
579 char adr[sizeof "255.255.255.255"]; | |
580 adr[0] = '\0'; | |
8 | 581 inet_ntop(AF_INET, (const u_char *)&priv.ip, adr, sizeof(adr)); |
0 | 582 char buf[2000]; |
583 snprintf(buf, sizeof(buf), rejectlist->message, adr, adr); | |
584 smfi_setreply(ctx, "550", "5.7.1", buf); | |
585 return SMFIS_REJECT; | |
586 } | |
587 else if (st == black) { | |
588 // reject the recipient based on blacklisting either from or to | |
589 smfi_setreply(ctx, "550", "5.7.1", "no such user"); | |
590 return SMFIS_REJECT; | |
591 } | |
592 else { | |
593 // accept the recipient | |
8 | 594 if (st == oksofar) { |
595 // but remember the non-whites | |
12 | 596 register_string(priv.non_whites, rcptaddr); |
8 | 597 priv.only_whites = false; |
598 } | |
599 if (st == white) { | |
600 priv.have_whites = true; | |
601 } | |
0 | 602 return SMFIS_CONTINUE; |
603 } | |
604 } | |
605 | |
8 | 606 sfsistat mlfi_body(SMFICTX *ctx, u_char *data, size_t len) |
0 | 607 { |
608 mlfiPriv &priv = *MLFIPRIV; | |
8 | 609 if (priv.authenticated) return SMFIS_CONTINUE; |
610 if (priv.only_whites) return SMFIS_CONTINUE; | |
611 priv.scanner->scan(data, len); | |
11 | 612 return SMFIS_CONTINUE; |
8 | 613 } |
614 | |
615 sfsistat mlfi_eom(SMFICTX *ctx) | |
616 { | |
617 sfsistat rc; | |
618 mlfiPriv &priv = *MLFIPRIV; | |
16 | 619 char *host = NULL; |
8 | 620 int ip; |
621 // process end of message | |
622 if (priv.authenticated || | |
623 priv.only_whites || | |
16 | 624 (check_hosts(priv, host, ip) == oksofar)) rc = SMFIS_CONTINUE; |
8 | 625 else { |
626 if (!priv.have_whites) { | |
627 // can reject the entire message | |
628 char buf[2000]; | |
24 | 629 if (!host) { |
630 // must be rejected due to excessive bad html tags | |
631 snprintf(buf, sizeof(buf), priv.pc->limit_message); | |
632 } | |
633 else { | |
634 char adr[sizeof "255.255.255.255"]; | |
635 adr[0] = '\0'; | |
636 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr)); | |
637 snprintf(buf, sizeof(buf), priv.pc->content_message, host, adr); | |
638 } | |
8 | 639 smfi_setreply(ctx, "550", "5.7.1", buf); |
640 rc = SMFIS_REJECT; | |
641 } | |
642 else { | |
643 // need to accept it but remove the recipients that don't want it | |
644 for (string_set::iterator i=priv.non_whites.begin(); i!=priv.non_whites.end(); i++) { | |
645 char *rcpt = *i; | |
646 smfi_delrcpt(ctx, rcpt); | |
647 } | |
648 rc = SMFIS_CONTINUE; | |
649 } | |
0 | 650 } |
8 | 651 // reset for a new message on the same connection |
652 mlfi_abort(ctx); | |
653 return rc; | |
654 } | |
655 | |
656 sfsistat mlfi_abort(SMFICTX *ctx) | |
657 { | |
658 mlfiPriv &priv = *MLFIPRIV; | |
659 priv.reset(); | |
0 | 660 return SMFIS_CONTINUE; |
661 } | |
662 | |
663 sfsistat mlfi_close(SMFICTX *ctx) | |
664 { | |
665 mlfiPriv *priv = MLFIPRIV; | |
666 if (!priv) return SMFIS_CONTINUE; | |
667 delete priv; | |
668 smfi_setpriv(ctx, NULL); | |
669 return SMFIS_CONTINUE; | |
670 } | |
671 | |
672 struct smfiDesc smfilter = | |
673 { | |
674 "DNSBL", // filter name | |
675 SMFI_VERSION, // version code -- do not change | |
676 SMFIF_DELRCPT, // flags | |
677 mlfi_connect, // connection info filter | |
678 NULL, // SMTP HELO command filter | |
679 mlfi_envfrom, // envelope sender filter | |
680 mlfi_envrcpt, // envelope recipient filter | |
681 NULL, // header filter | |
682 NULL, // end of header | |
8 | 683 mlfi_body, // body block filter |
684 mlfi_eom, // end of message | |
685 mlfi_abort, // message aborted | |
0 | 686 mlfi_close, // connection cleanup |
687 }; | |
688 | |
689 | |
690 static void dumpit(char *name, string_map map); | |
691 static void dumpit(char *name, string_map map) { | |
9 | 692 fprintf(stdout, "\n"); |
0 | 693 for (string_map::iterator i=map.begin(); i!=map.end(); i++) { |
9 | 694 fprintf(stdout, "%s %s->%s\n", name, (*i).first, (*i).second); |
0 | 695 } |
696 } | |
697 | |
698 | |
699 static void dumpit(from_map map); | |
700 static void dumpit(from_map map) { | |
701 for (from_map::iterator i=map.begin(); i!=map.end(); i++) { | |
3 | 702 char buf[2000]; |
703 snprintf(buf, sizeof(buf), "envelope from map for %s", (*i).first); | |
0 | 704 string_map *sm = (*i).second; |
3 | 705 dumpit(buf, *sm); |
0 | 706 } |
707 } | |
708 | |
709 | |
3 | 710 static void dumpit(CONFIG &dc); |
711 static void dumpit(CONFIG &dc) { | |
5 | 712 dumpit(dc.env_from); |
713 dumpit("envelope to (dnsbl list)", dc.env_to_dnsbll); | |
714 dumpit("envelope to (from map)", dc.env_to_chkfrom); | |
9 | 715 fprintf(stdout, "\ndnsbls\n"); |
0 | 716 for (dnsblp_map::iterator i=dc.dnsbls.begin(); i!=dc.dnsbls.end(); i++) { |
9 | 717 fprintf(stdout, "%s %s %s\n", (*i).first, (*i).second->suffix, (*i).second->message); |
0 | 718 } |
9 | 719 fprintf(stdout, "\ndnsbl_lists\n"); |
0 | 720 for (dnsbllp_map::iterator i=dc.dnsblls.begin(); i!=dc.dnsblls.end(); i++) { |
721 char *name = (*i).first; | |
722 DNSBLL &dl = *((*i).second); | |
9 | 723 fprintf(stdout, "%s", name); |
0 | 724 for (DNSBLL::iterator j=dl.begin(); j!=dl.end(); j++) { |
725 DNSBL &d = **j; | |
9 | 726 fprintf(stdout, " %s", d.suffix); |
0 | 727 } |
9 | 728 fprintf(stdout, "\n"); |
0 | 729 } |
9 | 730 if (dc.content_suffix) { |
731 fprintf(stdout, "\ncontent filtering enabled with %s %s\n", dc.content_suffix, dc.content_message); | |
732 } | |
24 | 733 if (dc.bad_tag_limit) { |
734 fprintf(stdout, "\ncontent filtering for excessive html tags enabled with limit %d %s\n", dc.bad_tag_limit, dc.limit_message); | |
735 } | |
9 | 736 fprintf(stdout, "\nfiles\n"); |
3 | 737 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) { |
738 char *f = *i; | |
9 | 739 fprintf(stdout, "config includes %s\n", f); |
3 | 740 } |
741 } | |
742 | |
743 | |
744 //////////////////////////////////////////////// | |
745 // check for redundant or recursive include files | |
746 // | |
747 static bool ok_to_include(CONFIG &dc, char *fn); | |
748 static bool ok_to_include(CONFIG &dc, char *fn) { | |
749 if (!fn) return false; | |
750 bool ok = true; | |
751 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) { | |
752 char *f = *i; | |
753 if (strcmp(f, fn) == 0) { | |
754 my_syslog("redundant or recursive include file detected"); | |
755 ok = false; | |
756 break; | |
757 } | |
758 } | |
759 return ok; | |
0 | 760 } |
761 | |
762 | |
763 //////////////////////////////////////////////// | |
764 // load a single config file | |
765 // | |
3 | 766 static void load_conf_dcc(CONFIG &dc, char *name, char *fn); |
767 static void load_conf_dcc(CONFIG &dc, char *name, char *fn) { | |
768 dc.config_files.push_back(fn); | |
769 char *list = BLACK; | |
770 const int LINE_SIZE = 2000; | |
771 ifstream is(fn); | |
772 if (is.fail()) return; | |
773 char line[LINE_SIZE]; | |
774 char *delim = " \t"; | |
775 int curline = 0; | |
776 while (!is.eof()) { | |
777 is.getline(line, LINE_SIZE); | |
778 curline++; | |
779 int n = strlen(line); | |
780 if (!n) continue; | |
781 for (int i=0; i<n; i++) line[i] = tolower(line[i]); | |
782 if (line[0] == '#') continue; | |
783 char *head = line; | |
784 if (strspn(line, delim) == 0) { | |
785 // have a leading ok/many tag to fetch | |
786 char *cmd = strtok(line, delim); | |
787 if (strcmp(cmd, MANY) == 0) list = BLACK; | |
788 else if (strcmp(cmd, OK) == 0) list = WHITE; | |
789 head = cmd + strlen(cmd) + 1; | |
790 } | |
791 char *cmd = strtok(head, delim); | |
792 if (!cmd) continue; | |
793 if (strcmp(cmd, "env_from") == 0) { | |
794 char *from = next_token(delim); | |
795 if (from) { | |
796 string_map &fm = really_find_from_map(dc, name); | |
797 fm[from] = list; | |
798 } | |
799 } | |
800 else if (strcmp(cmd, "env_to") == 0) { | |
801 char *to = next_token(delim); | |
802 if (to) { | |
803 dc.env_to_dnsbll[to] = list; | |
804 dc.env_to_chkfrom[to] = list; | |
805 } | |
806 } | |
807 else if (strcmp(cmd, "substitute") == 0) { | |
808 char *tag = next_token(delim); | |
809 if (tag && (strcmp(tag, "mail_host") == 0)) { | |
810 char *from = next_token(delim); | |
811 if (from) { | |
812 string_map &fm = really_find_from_map(dc, name); | |
813 fm[from] = list; | |
814 } | |
815 } | |
816 } | |
817 else if (strcmp(cmd, "include") == 0) { | |
818 char *fn = next_token(delim); | |
819 if (ok_to_include(dc, fn)) { | |
820 load_conf_dcc(dc, name, fn); | |
821 } | |
822 } | |
823 | |
824 } | |
825 is.close(); | |
826 } | |
827 | |
828 | |
0 | 829 static void load_conf(CONFIG &dc, char *fn); |
830 static void load_conf(CONFIG &dc, char *fn) { | |
831 dc.config_files.push_back(fn); | |
832 map<char*, int, ltstr> commands; | |
24 | 833 enum {dummy, content, htmllimit, htmltag, dnsbl, dnsbll, envfrom, envto, include, includedcc}; |
8 | 834 commands["content" ] = content; |
24 | 835 commands["html_limit" ] = htmllimit; |
836 commands["html_tag" ] = htmltag; | |
3 | 837 commands["dnsbl" ] = dnsbl; |
838 commands["dnsbl_list" ] = dnsbll; | |
839 commands["env_from" ] = envfrom; | |
840 commands["env_to" ] = envto; | |
841 commands["include" ] = include; | |
842 commands["include_dcc"] = includedcc; | |
0 | 843 const int LINE_SIZE = 2000; |
844 ifstream is(fn); | |
845 if (is.fail()) return; | |
846 char line[LINE_SIZE]; | |
847 char orig[LINE_SIZE]; | |
848 char *delim = " \t"; | |
849 int curline = 0; | |
850 while (!is.eof()) { | |
851 is.getline(line, LINE_SIZE); | |
852 snprintf(orig, sizeof(orig), "%s", line); | |
853 curline++; | |
854 int n = strlen(line); | |
855 for (int i=0; i<n; i++) line[i] = tolower(line[i]); | |
856 char *cmd = strtok(line, delim); | |
857 if (cmd && (cmd[0] != '#') && (cmd[0] != '\0')) { | |
858 // have a decent command | |
859 bool processed = false; | |
860 switch (commands[cmd]) { | |
8 | 861 case content: { |
862 char *suff = strtok(NULL, delim); | |
24 | 863 if (!suff) break; // no dns suffix |
8 | 864 char *msg = suff + strlen(suff); |
865 if ((msg - line) >= strlen(orig)) break; // line ended with the dns suffix | |
866 msg = strchr(msg+1, '\''); | |
867 if (!msg) break; // no reply message template | |
868 msg++; // move over the leading ' | |
869 if ((msg - line) >= strlen(orig)) break; // line ended with the leading quote | |
870 char *last = strchr(msg, '\''); | |
871 if (!last) break; // no trailing quote | |
872 *last = '\0'; // make it a null terminator | |
873 dc.content_suffix = register_string(suff); | |
874 dc.content_message = register_string(msg); | |
875 processed = true; | |
876 } break; | |
877 | |
24 | 878 case htmllimit: { |
879 char *limit = strtok(NULL, delim); | |
880 if (!limit) break; // no integer limit | |
881 char *msg = limit + strlen(limit); | |
882 if ((msg - line) >= strlen(orig)) break; // line ended with the limit | |
883 msg = strchr(msg+1, '\''); | |
884 if (!msg) break; // no reply message template | |
885 msg++; // move over the leading ' | |
886 if ((msg - line) >= strlen(orig)) break; // line ended with the leading quote | |
887 char *last = strchr(msg, '\''); | |
888 if (!last) break; // no trailing quote | |
889 *last = '\0'; // make it a null terminator | |
890 dc.bad_tag_limit = atoi(limit); | |
891 dc.limit_message = register_string(msg); | |
892 processed = true; | |
893 } break; | |
894 | |
895 case htmltag: { | |
896 char *tag = next_token(delim); | |
897 if (!tag) break; // no html tag value | |
26 | 898 char buf[200]; |
899 snprintf(buf, sizeof(buf), "/%s", tag); | |
24 | 900 dc.html_tags.insert(tag); |
26 | 901 dc.html_tags.insert(register_string(buf)); |
24 | 902 processed = true; |
903 } break; | |
904 | |
0 | 905 case dnsbl: { |
906 // have a new dnsbl to use | |
907 char *name = next_token(delim); | |
908 if (!name) break; // no name name | |
909 if (find_dnsbl(dc, name)) break; // duplicate entry | |
910 char *suff = strtok(NULL, delim); | |
911 if (!suff) break; // no dns suffic | |
912 char *msg = suff + strlen(suff); | |
913 if ((msg - line) >= strlen(orig)) break; // line ended with the dns suffix | |
914 msg = strchr(msg+1, '\''); | |
915 if (!msg) break; // no reply message template | |
916 msg++; // move over the leading ' | |
917 if ((msg - line) >= strlen(orig)) break; // line ended with the leading quote | |
918 char *last = strchr(msg, '\''); | |
919 if (!last) break; // no trailing quote | |
920 *last = '\0'; // make it a null terminator | |
921 dc.dnsbls[name] = new DNSBL(register_string(suff), register_string(msg)); | |
922 processed = true; | |
923 } break; | |
924 | |
925 case dnsbll: { | |
926 // define a new combination of dnsbls | |
927 char *name = next_token(delim); | |
928 if (!name) break; | |
929 if (find_dnsbll(dc, name)) break; // duplicate entry | |
930 char *list = next_token(delim); | |
931 if (!list || (*list == '\0') || (*list == '#')) break; | |
932 DNSBLLP d = new DNSBLL; | |
933 DNSBLP p = find_dnsbl(dc, list); | |
934 if (p) d->push_back(p); | |
935 while (true) { | |
936 list = next_token(delim); | |
937 if (!list || (*list == '\0') || (*list == '#')) break; | |
938 DNSBLP p = find_dnsbl(dc, list); | |
939 if (p) d->push_back(p); | |
940 } | |
941 dc.dnsblls[name] = d; | |
942 processed = true; | |
943 } break; | |
944 | |
945 case envfrom: { | |
946 // add an entry into the named string_map | |
947 char *name = next_token(delim); | |
948 if (!name) break; | |
949 char *from = next_token(delim); | |
950 if (!from) break; | |
951 char *list = next_token(delim); | |
952 if (!list) break; | |
953 if ((strcmp(list, WHITE) == 0) || | |
954 (strcmp(list, BLACK) == 0)) { | |
955 string_map &fm = really_find_from_map(dc, name); | |
956 fm[from] = list; | |
957 processed = true; | |
958 } | |
959 else { | |
960 // list may be the name of a previously defined from_map | |
961 string_map *m = find_from_map(dc, list); | |
962 if (m && (strcmp(list,name) != 0)) { | |
963 string_map &pm = *m; | |
964 string_map &fm = really_find_from_map(dc, name); | |
965 fm.insert(pm.begin(), pm.end()); | |
966 processed = true; | |
967 } | |
968 } | |
969 } break; | |
970 | |
971 case envto: { | |
972 // define the dnsbl_list and env_from maps to use for this recipient | |
973 char *to = next_token(delim); | |
974 if (!to) break; | |
975 char *list = next_token(delim); | |
976 if (!list) break; | |
977 char *from = next_token(delim); | |
978 if (!from) break; | |
979 dc.env_to_dnsbll[to] = list; | |
980 dc.env_to_chkfrom[to] = from; | |
981 processed = true; | |
982 } break; | |
983 | |
984 case include: { | |
985 char *fn = next_token(delim); | |
3 | 986 if (ok_to_include(dc, fn)) { |
987 load_conf(dc, fn); | |
988 processed = true; | |
989 } | |
990 } break; | |
991 | |
992 case includedcc: { | |
993 char *name = next_token(delim); | |
994 if (!name) break; | |
995 char *fn = next_token(delim); | |
996 if (ok_to_include(dc, fn)) { | |
997 load_conf_dcc(dc, name, fn); | |
998 processed = true; | |
0 | 999 } |
1000 } break; | |
1001 | |
1002 default: { | |
1003 } break; | |
1004 } | |
1005 if (!processed) { | |
1006 pthread_mutex_lock(&syslog_mutex); | |
1007 openlog("dnsbl", LOG_PID, LOG_MAIL); | |
1008 syslog(LOG_ERR, "ignoring file %s line %d : %s\n", fn, curline, orig); | |
1009 closelog(); | |
1010 pthread_mutex_unlock(&syslog_mutex); | |
1011 } | |
1012 } | |
1013 } | |
1014 is.close(); | |
1015 } | |
1016 | |
1017 | |
1018 //////////////////////////////////////////////// | |
1019 // reload the config | |
1020 // | |
1021 static CONFIG* new_conf(); | |
1022 static CONFIG* new_conf() { | |
1023 my_syslog("loading new configuration"); | |
1024 CONFIG *newc = new CONFIG; | |
1025 load_conf(*newc, "dnsbl.conf"); | |
1026 newc->load_time = time(NULL); | |
1027 return newc; | |
1028 } | |
1029 | |
1030 | |
1031 //////////////////////////////////////////////// | |
1032 // thread to watch the old config files for changes | |
1033 // and reload when needed. we also cleanup old | |
1034 // configs whose reference count has gone to zero. | |
1035 // | |
1036 static void* config_loader(void *arg); | |
1037 static void* config_loader(void *arg) { | |
1038 typedef set<CONFIG *> configp_set; | |
1039 configp_set old_configs; | |
18 | 1040 while (loader_run) { |
0 | 1041 sleep(180); // look for modifications every 3 minutes |
18 | 1042 if (!loader_run) break; |
0 | 1043 CONFIG &dc = *config; |
1044 time_t then = dc.load_time; | |
1045 struct stat st; | |
1046 bool reload = false; | |
1047 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) { | |
1048 char *fn = *i; | |
1049 if (stat(fn, &st)) reload = true; // file disappeared | |
1050 else if (st.st_mtime > then) reload = true; // file modified | |
1051 if (reload) break; | |
1052 } | |
1053 if (reload) { | |
1054 CONFIG *newc = new_conf(); | |
1055 // replace the global config pointer | |
1056 pthread_mutex_lock(&config_mutex); | |
1057 CONFIG *old = config; | |
1058 config = newc; | |
1059 pthread_mutex_unlock(&config_mutex); | |
1060 if (old) old_configs.insert(old); | |
1061 } | |
1062 // now look for old configs with zero ref counts | |
1063 for (configp_set::iterator i=old_configs.begin(); i!=old_configs.end(); ) { | |
1064 CONFIG *old = *i; | |
1065 if (!old->reference_count) { | |
1066 delete old; // destructor does all the work | |
1067 old_configs.erase(i++); | |
1068 } | |
1069 else i++; | |
1070 } | |
1071 } | |
18 | 1072 return NULL; |
0 | 1073 } |
1074 | |
1075 | |
1076 static void usage(char *prog); | |
1077 static void usage(char *prog) | |
1078 { | |
16 | 1079 fprintf(stderr, "Usage: %s [-d] [-c] -p socket-addr [-t timeout]\n", prog); |
0 | 1080 fprintf(stderr, "where socket-addr is for the connection to sendmail and should be one of\n"); |
1081 fprintf(stderr, " inet:port@local-ip-address\n"); | |
1082 fprintf(stderr, " local:local-domain-socket-file-name\n"); | |
9 | 1083 fprintf(stderr, "-c will load and dump the config to stdout\n"); |
16 | 1084 fprintf(stderr, "-d will add some syslog debug messages\n"); |
0 | 1085 } |
1086 | |
1087 | |
1088 int main(int argc, char**argv) | |
1089 { | |
3 | 1090 bool check = false; |
1091 bool setconn = false; | |
0 | 1092 int c; |
16 | 1093 const char *args = "p:t:hcd"; |
0 | 1094 extern char *optarg; |
1095 | |
1096 // Process command line options | |
1097 while ((c = getopt(argc, argv, args)) != -1) { | |
1098 switch (c) { | |
1099 case 'p': | |
1100 if (optarg == NULL || *optarg == '\0') { | |
1101 fprintf(stderr, "Illegal conn: %s\n", optarg); | |
1102 exit(EX_USAGE); | |
1103 } | |
1104 if (smfi_setconn(optarg) == MI_FAILURE) { | |
1105 fprintf(stderr, "smfi_setconn failed\n"); | |
1106 exit(EX_SOFTWARE); | |
1107 } | |
1108 | |
1109 if (strncasecmp(optarg, "unix:", 5) == 0) unlink(optarg + 5); | |
1110 else if (strncasecmp(optarg, "local:", 6) == 0) unlink(optarg + 6); | |
3 | 1111 setconn = true; |
0 | 1112 break; |
1113 | |
1114 case 't': | |
1115 if (optarg == NULL || *optarg == '\0') { | |
1116 fprintf(stderr, "Illegal timeout: %s\n", optarg); | |
1117 exit(EX_USAGE); | |
1118 } | |
1119 if (smfi_settimeout(atoi(optarg)) == MI_FAILURE) { | |
1120 fprintf(stderr, "smfi_settimeout failed\n"); | |
1121 exit(EX_SOFTWARE); | |
1122 } | |
1123 break; | |
1124 | |
3 | 1125 case 'c': |
1126 check = true; | |
1127 break; | |
1128 | |
16 | 1129 case 'd': |
1130 debug_syslog = true; | |
1131 break; | |
1132 | |
0 | 1133 case 'h': |
1134 default: | |
1135 usage(argv[0]); | |
1136 exit(EX_USAGE); | |
1137 } | |
1138 } | |
5 | 1139 |
1140 if (check) { | |
1141 CONFIG &dc = *new_conf(); | |
1142 dumpit(dc); | |
1143 return 0; | |
1144 } | |
1145 | |
0 | 1146 if (!setconn) { |
1147 fprintf(stderr, "%s: Missing required -p argument\n", argv[0]); | |
1148 usage(argv[0]); | |
1149 exit(EX_USAGE); | |
1150 } | |
5 | 1151 |
0 | 1152 if (smfi_register(smfilter) == MI_FAILURE) { |
1153 fprintf(stderr, "smfi_register failed\n"); | |
1154 exit(EX_UNAVAILABLE); | |
1155 } | |
1156 | |
1157 // switch to background mode | |
1158 if (daemon(1,0) < 0) { | |
1159 fprintf(stderr, "daemon() call failed\n"); | |
1160 exit(EX_UNAVAILABLE); | |
1161 } | |
1162 | |
1163 // initialize the thread sync objects | |
1164 pthread_mutex_init(&config_mutex, 0); | |
1165 pthread_mutex_init(&syslog_mutex, 0); | |
1166 pthread_mutex_init(&resolve_mutex, 0); | |
1167 | |
1168 // load the initial config | |
1169 config = new_conf(); | |
1170 | |
1171 // only create threads after the fork() in daemon | |
1172 pthread_t tid; | |
1173 if (pthread_create(&tid, 0, config_loader, 0)) | |
1174 my_syslog("failed to create config loader thread"); | |
1175 if (pthread_detach(tid)) | |
1176 my_syslog("failed to detach config loader thread"); | |
1177 | |
1178 // write the pid | |
1179 const char *pidpath = "/var/run/dnsbl.pid"; | |
1180 unlink(pidpath); | |
1181 FILE *f = fopen(pidpath, "w"); | |
1182 if (f) { | |
22 | 1183 #ifdef linux |
1184 // from a comment in the DCC source code: | |
1185 // Linux threads are broken. Signals given the | |
1186 // original process are delivered to only the | |
1187 // thread that happens to have that PID. The | |
1188 // sendmail libmilter thread that needs to hear | |
1189 // SIGINT and other signals does not, and that breaks | |
1190 // scripts that need to stop milters. | |
1191 // However, signaling the process group works. | |
0 | 1192 fprintf(f, "-%d\n", (u_int)getpgrp()); |
22 | 1193 #else |
1194 fprintf(f, "%d\n", (u_int)getpid()); | |
1195 #endif | |
0 | 1196 fclose(f); |
1197 } | |
1198 | |
18 | 1199 time_t starting = time(NULL); |
1200 int rc = smfi_main(); | |
22 | 1201 if ((rc != MI_SUCCESS) && (time(NULL) > starting+5*60)) { |
18 | 1202 my_syslog("trying to restart after smfi_main()"); |
1203 loader_run = false; // eventually the config loader thread will terminate | |
1204 execvp(argv[0], argv); | |
1205 } | |
1206 exit((rc == MI_SUCCESS) ? 0 : EX_UNAVAILABLE); | |
0 | 1207 } |
8 | 1208 |