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