Mercurial > dnsbl
annotate src/dnsbl.cpp @ 36:95607fbef608
no message
author | carl |
---|---|
date | Sun, 30 May 2004 18:38:36 -0700 |
parents | d718dca81bc9 |
children | 6e9d0b2d0720 |
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 } | |
36 | 445 if (n-nam) n--; // remove trailing . |
446 *n = '\0'; // null terminate it | |
34 | 447 register_string(ns, nam); // ns host to lookup later |
448 } | |
449 } | |
450 rrnum = 0; | |
451 while (ns_parserr(&handle, ns_s_ar, rrnum++, &rr) == 0) { | |
452 if (ns_rr_type(rr) == ns_t_a) { | |
453 char* nam = (char*)ns_rr_name(rr); | |
454 ns_map::iterator i = ns.find(nam); | |
455 if (i != ns.end()) { | |
456 // we want this ip address | |
457 int address; | |
458 memcpy(&address, ns_rr_rdata(rr), sizeof(address)); | |
459 ns[nam] = address; | |
460 } | |
461 } | |
462 } | |
463 } | |
23
06de5ab6a232
add url decoding stage, allow http:/ single / in yahoo redirector, allow ip address hostnames
carl
parents:
22
diff
changeset
|
464 int rrnum = 0; |
06de5ab6a232
add url decoding stage, allow http:/ single / in yahoo redirector, allow ip address hostnames
carl
parents:
22
diff
changeset
|
465 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
|
466 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
|
467 int address; |
06de5ab6a232
add url decoding stage, allow http:/ single / in yahoo redirector, allow ip address hostnames
carl
parents:
22
diff
changeset
|
468 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
|
469 return address; |
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 } |
06de5ab6a232
add url decoding stage, allow http:/ single / in yahoo redirector, allow ip address hostnames
carl
parents:
22
diff
changeset
|
474 if (maybe_ip) { |
06de5ab6a232
add url decoding stage, allow http:/ single / in yahoo redirector, allow ip address hostnames
carl
parents:
22
diff
changeset
|
475 // 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
|
476 in_addr ip; |
06de5ab6a232
add url decoding stage, allow http:/ single / in yahoo redirector, allow ip address hostnames
carl
parents:
22
diff
changeset
|
477 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
|
478 return ip.s_addr; |
8 | 479 } |
480 } | |
481 return 0; | |
16 | 482 #else |
483 struct hostent *host = gethostbyname(question); | |
484 if (!host) return 0; | |
485 if (host->h_addrtype != AF_INET) return 0; | |
486 int address; | |
487 memcpy(&address, host->h_addr, sizeof(address)); | |
488 return address; | |
489 #endif | |
8 | 490 } |
491 | |
34 | 492 static int protected_dns_interface(char *question, bool maybe_ip, ns_map *nameservers); |
493 static int protected_dns_interface(char *question, bool maybe_ip, ns_map *nameservers) { | |
8 | 494 int ans; |
495 pthread_mutex_lock(&resolve_mutex); | |
34 | 496 ans = dns_interface(question, maybe_ip, nameservers); |
8 | 497 pthread_mutex_unlock(&resolve_mutex); |
498 return ans; | |
499 | |
500 } | |
501 | |
502 //////////////////////////////////////////////// | |
503 // check a single dnsbl | |
504 // | |
505 static status check_single(int ip, char *suffix); | |
506 static status check_single(int ip, char *suffix) { | |
0 | 507 // make a dns question |
508 const u_char *src = (const u_char *)&ip; | |
509 if (src[0] == 127) return oksofar; // don't do dns lookups on localhost | |
16 | 510 #ifdef NS_MAXDNAME |
0 | 511 char question[NS_MAXDNAME]; |
16 | 512 #else |
513 char question[1000]; | |
514 #endif | |
8 | 515 snprintf(question, sizeof(question), "%u.%u.%u.%u.%s.", src[3], src[2], src[1], src[0], suffix); |
516 // ask the question, if we get an A record it implies a blacklisted ip address | |
34 | 517 return (protected_dns_interface(question, false, NULL)) ? reject : oksofar; |
8 | 518 } |
519 | |
520 | |
521 //////////////////////////////////////////////// | |
522 // check a single dnsbl | |
523 // | |
524 static status check_single(int ip, DNSBL &bl); | |
525 static status check_single(int ip, DNSBL &bl) { | |
526 return check_single(ip, bl.suffix); | |
0 | 527 } |
528 | |
529 | |
530 //////////////////////////////////////////////// | |
531 // check the dnsbls specified for this recipient | |
532 // | |
533 static status check_dnsbl(mlfiPriv &priv, DNSBLLP dnsbllp, DNSBLP &rejectlist); | |
534 static status check_dnsbl(mlfiPriv &priv, DNSBLLP dnsbllp, DNSBLP &rejectlist) { | |
535 if (priv.authenticated) return oksofar; | |
536 if (!dnsbllp) return oksofar; | |
537 DNSBLL &dnsbll = *dnsbllp; | |
538 for (DNSBLL::iterator i=dnsbll.begin(); i!=dnsbll.end(); i++) { | |
539 DNSBLP dp = *i; // non null by construction | |
540 status st; | |
541 map<DNSBLP, status>::iterator f = priv.checked.find(dp); | |
542 if (f == priv.checked.end()) { | |
543 // have not checked this list yet | |
8 | 544 st = check_single(priv.ip, *dp); |
0 | 545 rejectlist = dp; |
546 priv.checked[dp] = st; | |
547 } | |
548 else { | |
549 st = (*f).second; | |
550 rejectlist = (*f).first; | |
551 } | |
552 if (st == reject) return st; | |
553 } | |
554 return oksofar; | |
555 } | |
556 | |
557 | |
558 //////////////////////////////////////////////// | |
8 | 559 // check the dnsbls specified for this recipient |
560 // | |
16 | 561 static status check_hosts(mlfiPriv &priv, char *&host, int &ip); |
562 static status check_hosts(mlfiPriv &priv, char *&host, int &ip) { | |
8 | 563 CONFIG &dc = *priv.pc; |
564 if (!dc.content_suffix) return oksofar; | |
565 int count = 0; | |
34 | 566 ns_map nameservers; |
567 int lim = priv.pc->host_limit; | |
24 | 568 for (string_set::iterator i=priv.memory->hosts.begin(); i!=priv.memory->hosts.end(); i++) { |
8 | 569 count++; |
34 | 570 if ((count > lim) && (lim > 0)) { |
571 discard(nameservers); | |
572 return reject_host; | |
573 } | |
35 | 574 host = *i; // a reference into priv.memory->hosts, which will live until this smtp transaction is closed |
34 | 575 ip = protected_dns_interface(host, true, &nameservers); |
16 | 576 if (debug_syslog) { |
577 char buf[200]; | |
29
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
578 if (ip) { |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
579 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
|
580 adr[0] = '\0'; |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
581 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
|
582 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
|
583 } |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
584 else { |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
585 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
|
586 } |
16 | 587 my_syslog(buf); |
588 } | |
8 | 589 if (ip) { |
590 status st = check_single(ip, dc.content_suffix); | |
34 | 591 if (st == reject) { |
592 discard(nameservers); | |
593 return st; | |
594 } | |
8 | 595 } |
596 } | |
34 | 597 lim *= 4; // allow average of 3 ns per host name |
598 for (ns_map::iterator i=nameservers.begin(); i!=nameservers.end(); i++) { | |
599 count++; | |
600 if ((count > lim) && (lim > 0)) { | |
601 discard(nameservers); | |
602 return reject_host; | |
603 } | |
36 | 604 host = (*i).first; // a transient reference that needs to be replaced before we return it |
34 | 605 ip = (*i).second; |
606 if (!ip) ip = protected_dns_interface(host, false, NULL); | |
607 if (debug_syslog) { | |
608 char buf[200]; | |
609 if (ip) { | |
610 char adr[sizeof "255.255.255.255"]; | |
611 adr[0] = '\0'; | |
612 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr)); | |
613 snprintf(buf, sizeof(buf), "ns %s found at %s", host, adr); | |
614 } | |
615 else { | |
616 snprintf(buf, sizeof(buf), "ns %s not found", host); | |
617 } | |
618 my_syslog(buf); | |
619 } | |
620 if (ip) { | |
621 status st = check_single(ip, dc.content_suffix); | |
622 if (st == reject) { | |
36 | 623 host = register_string(priv.memory->hosts, host); // put a copy into priv.memory->hosts, and return that reference |
34 | 624 discard(nameservers); |
625 return st; | |
626 } | |
627 } | |
628 } | |
629 discard(nameservers); | |
24 | 630 host = NULL; |
26 | 631 int bin = priv.memory->binary_tags; |
24 | 632 int bad = priv.memory->bad_html_tags; |
34 | 633 lim = priv.pc->tag_limit; |
26 | 634 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
|
635 if ((bad > lim) && (lim > 0)) return reject_tag; |
9 | 636 return oksofar; |
8 | 637 } |
638 | |
639 | |
640 //////////////////////////////////////////////// | |
0 | 641 // start of sendmail milter interfaces |
642 // | |
643 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr) | |
644 { | |
645 // allocate some private memory | |
646 mlfiPriv *priv = new mlfiPriv; | |
647 if (hostaddr->sa_family == AF_INET) { | |
648 priv->ip = ((struct sockaddr_in *)hostaddr)->sin_addr.s_addr; | |
649 } | |
650 | |
651 // save the private data | |
652 smfi_setpriv(ctx, (void*)priv); | |
653 | |
654 // continue processing | |
655 return SMFIS_CONTINUE; | |
656 } | |
657 | |
658 sfsistat mlfi_envfrom(SMFICTX *ctx, char **from) | |
659 { | |
660 mlfiPriv &priv = *MLFIPRIV; | |
661 priv.mailaddr = strdup(from[0]); | |
662 priv.authenticated = (smfi_getsymval(ctx, "{auth_authen}") != NULL); | |
663 return SMFIS_CONTINUE; | |
664 } | |
665 | |
666 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **rcpt) | |
667 { | |
668 DNSBLP rejectlist = NULL; // list that caused the reject | |
669 status st = oksofar; | |
670 mlfiPriv &priv = *MLFIPRIV; | |
671 CONFIG &dc = *priv.pc; | |
672 char *rcptaddr = rcpt[0]; | |
673 char *dnsname = lookup(rcptaddr, dc.env_to_dnsbll); | |
674 char *fromname = lookup(rcptaddr, dc.env_to_chkfrom); | |
675 if ((strcmp(dnsname, BLACK) == 0) || | |
676 (strcmp(fromname, BLACK) == 0)) { | |
677 st = black; // two options to blacklist this recipient | |
678 } | |
679 else if (strcmp(fromname, WHITE) == 0) { | |
680 st = white; | |
681 } | |
682 else { | |
683 // check an env_from map | |
684 string_map *sm = find_from_map(dc, fromname); | |
685 if (sm != NULL) { | |
686 fromname = lookup(priv.mailaddr, *sm); // returns default if name not in map | |
687 if (strcmp(fromname, BLACK) == 0) { | |
688 st = black; // blacklist this envelope from value | |
689 } | |
690 if (strcmp(fromname, WHITE) == 0) { | |
691 st = white; // blacklist this envelope from value | |
692 } | |
693 } | |
694 } | |
695 if ((st == oksofar) && (strcmp(dnsname, WHITE) != 0)) { | |
696 // check dns lists | |
697 st = check_dnsbl(priv, find_dnsbll(dc, dnsname), rejectlist); | |
698 } | |
699 | |
700 if (st == reject) { | |
701 // reject the recipient based on some dnsbl | |
702 char adr[sizeof "255.255.255.255"]; | |
703 adr[0] = '\0'; | |
8 | 704 inet_ntop(AF_INET, (const u_char *)&priv.ip, adr, sizeof(adr)); |
0 | 705 char buf[2000]; |
706 snprintf(buf, sizeof(buf), rejectlist->message, adr, adr); | |
707 smfi_setreply(ctx, "550", "5.7.1", buf); | |
708 return SMFIS_REJECT; | |
709 } | |
710 else if (st == black) { | |
711 // reject the recipient based on blacklisting either from or to | |
712 smfi_setreply(ctx, "550", "5.7.1", "no such user"); | |
713 return SMFIS_REJECT; | |
714 } | |
715 else { | |
716 // accept the recipient | |
8 | 717 if (st == oksofar) { |
718 // but remember the non-whites | |
12 | 719 register_string(priv.non_whites, rcptaddr); |
8 | 720 priv.only_whites = false; |
721 } | |
722 if (st == white) { | |
723 priv.have_whites = true; | |
724 } | |
0 | 725 return SMFIS_CONTINUE; |
726 } | |
727 } | |
728 | |
8 | 729 sfsistat mlfi_body(SMFICTX *ctx, u_char *data, size_t len) |
0 | 730 { |
731 mlfiPriv &priv = *MLFIPRIV; | |
8 | 732 if (priv.authenticated) return SMFIS_CONTINUE; |
733 if (priv.only_whites) return SMFIS_CONTINUE; | |
734 priv.scanner->scan(data, len); | |
11 | 735 return SMFIS_CONTINUE; |
8 | 736 } |
737 | |
738 sfsistat mlfi_eom(SMFICTX *ctx) | |
739 { | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
740 sfsistat rc; |
8 | 741 mlfiPriv &priv = *MLFIPRIV; |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
742 char *host = NULL; |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
743 int ip; |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
744 status st; |
8 | 745 // process end of message |
746 if (priv.authenticated || | |
747 priv.only_whites || | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
748 ((st=check_hosts(priv, host, ip)) == oksofar)) rc = SMFIS_CONTINUE; |
8 | 749 else { |
750 if (!priv.have_whites) { | |
751 // can reject the entire message | |
752 char buf[2000]; | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
753 if (st == reject_tag) { |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
754 // rejected due to excessive bad html tags |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
755 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
|
756 } |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
757 else if (st == reject_host) { |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
758 // rejected due to excessive unique host/urls |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
759 snprintf(buf, sizeof(buf), priv.pc->host_limit_message); |
24 | 760 } |
761 else { | |
762 char adr[sizeof "255.255.255.255"]; | |
763 adr[0] = '\0'; | |
764 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr)); | |
765 snprintf(buf, sizeof(buf), priv.pc->content_message, host, adr); | |
766 } | |
8 | 767 smfi_setreply(ctx, "550", "5.7.1", buf); |
768 rc = SMFIS_REJECT; | |
769 } | |
770 else { | |
771 // need to accept it but remove the recipients that don't want it | |
772 for (string_set::iterator i=priv.non_whites.begin(); i!=priv.non_whites.end(); i++) { | |
773 char *rcpt = *i; | |
774 smfi_delrcpt(ctx, rcpt); | |
775 } | |
776 rc = SMFIS_CONTINUE; | |
777 } | |
0 | 778 } |
8 | 779 // reset for a new message on the same connection |
780 mlfi_abort(ctx); | |
781 return rc; | |
782 } | |
783 | |
784 sfsistat mlfi_abort(SMFICTX *ctx) | |
785 { | |
786 mlfiPriv &priv = *MLFIPRIV; | |
787 priv.reset(); | |
0 | 788 return SMFIS_CONTINUE; |
789 } | |
790 | |
791 sfsistat mlfi_close(SMFICTX *ctx) | |
792 { | |
793 mlfiPriv *priv = MLFIPRIV; | |
794 if (!priv) return SMFIS_CONTINUE; | |
795 delete priv; | |
796 smfi_setpriv(ctx, NULL); | |
797 return SMFIS_CONTINUE; | |
798 } | |
799 | |
800 struct smfiDesc smfilter = | |
801 { | |
802 "DNSBL", // filter name | |
803 SMFI_VERSION, // version code -- do not change | |
804 SMFIF_DELRCPT, // flags | |
805 mlfi_connect, // connection info filter | |
806 NULL, // SMTP HELO command filter | |
807 mlfi_envfrom, // envelope sender filter | |
808 mlfi_envrcpt, // envelope recipient filter | |
809 NULL, // header filter | |
810 NULL, // end of header | |
8 | 811 mlfi_body, // body block filter |
812 mlfi_eom, // end of message | |
813 mlfi_abort, // message aborted | |
0 | 814 mlfi_close, // connection cleanup |
815 }; | |
816 | |
817 | |
818 static void dumpit(char *name, string_map map); | |
819 static void dumpit(char *name, string_map map) { | |
9 | 820 fprintf(stdout, "\n"); |
0 | 821 for (string_map::iterator i=map.begin(); i!=map.end(); i++) { |
9 | 822 fprintf(stdout, "%s %s->%s\n", name, (*i).first, (*i).second); |
0 | 823 } |
824 } | |
825 | |
826 | |
827 static void dumpit(from_map map); | |
828 static void dumpit(from_map map) { | |
829 for (from_map::iterator i=map.begin(); i!=map.end(); i++) { | |
3 | 830 char buf[2000]; |
831 snprintf(buf, sizeof(buf), "envelope from map for %s", (*i).first); | |
0 | 832 string_map *sm = (*i).second; |
3 | 833 dumpit(buf, *sm); |
0 | 834 } |
835 } | |
836 | |
837 | |
3 | 838 static void dumpit(CONFIG &dc); |
839 static void dumpit(CONFIG &dc) { | |
5 | 840 dumpit(dc.env_from); |
841 dumpit("envelope to (dnsbl list)", dc.env_to_dnsbll); | |
842 dumpit("envelope to (from map)", dc.env_to_chkfrom); | |
9 | 843 fprintf(stdout, "\ndnsbls\n"); |
0 | 844 for (dnsblp_map::iterator i=dc.dnsbls.begin(); i!=dc.dnsbls.end(); i++) { |
9 | 845 fprintf(stdout, "%s %s %s\n", (*i).first, (*i).second->suffix, (*i).second->message); |
0 | 846 } |
9 | 847 fprintf(stdout, "\ndnsbl_lists\n"); |
0 | 848 for (dnsbllp_map::iterator i=dc.dnsblls.begin(); i!=dc.dnsblls.end(); i++) { |
849 char *name = (*i).first; | |
850 DNSBLL &dl = *((*i).second); | |
9 | 851 fprintf(stdout, "%s", name); |
0 | 852 for (DNSBLL::iterator j=dl.begin(); j!=dl.end(); j++) { |
853 DNSBL &d = **j; | |
9 | 854 fprintf(stdout, " %s", d.suffix); |
0 | 855 } |
9 | 856 fprintf(stdout, "\n"); |
0 | 857 } |
9 | 858 if (dc.content_suffix) { |
859 fprintf(stdout, "\ncontent filtering enabled with %s %s\n", dc.content_suffix, dc.content_message); | |
860 } | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
861 if (dc.host_limit) { |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
862 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
|
863 } |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
864 if (dc.tag_limit) { |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
865 fprintf(stdout, "\ncontent filtering for excessive html tags enabled with limit %d %s\n", dc.tag_limit, dc.tag_limit_message); |
24 | 866 } |
9 | 867 fprintf(stdout, "\nfiles\n"); |
3 | 868 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) { |
869 char *f = *i; | |
9 | 870 fprintf(stdout, "config includes %s\n", f); |
3 | 871 } |
872 } | |
873 | |
874 | |
875 //////////////////////////////////////////////// | |
876 // check for redundant or recursive include files | |
877 // | |
878 static bool ok_to_include(CONFIG &dc, char *fn); | |
879 static bool ok_to_include(CONFIG &dc, char *fn) { | |
880 if (!fn) return false; | |
881 bool ok = true; | |
882 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) { | |
883 char *f = *i; | |
884 if (strcmp(f, fn) == 0) { | |
885 my_syslog("redundant or recursive include file detected"); | |
886 ok = false; | |
887 break; | |
888 } | |
889 } | |
890 return ok; | |
0 | 891 } |
892 | |
893 | |
894 //////////////////////////////////////////////// | |
895 // load a single config file | |
896 // | |
3 | 897 static void load_conf_dcc(CONFIG &dc, char *name, char *fn); |
898 static void load_conf_dcc(CONFIG &dc, char *name, char *fn) { | |
899 dc.config_files.push_back(fn); | |
900 char *list = BLACK; | |
901 const int LINE_SIZE = 2000; | |
902 ifstream is(fn); | |
903 if (is.fail()) return; | |
904 char line[LINE_SIZE]; | |
905 char *delim = " \t"; | |
906 int curline = 0; | |
907 while (!is.eof()) { | |
908 is.getline(line, LINE_SIZE); | |
909 curline++; | |
910 int n = strlen(line); | |
911 if (!n) continue; | |
912 for (int i=0; i<n; i++) line[i] = tolower(line[i]); | |
913 if (line[0] == '#') continue; | |
914 char *head = line; | |
915 if (strspn(line, delim) == 0) { | |
916 // have a leading ok/many tag to fetch | |
917 char *cmd = strtok(line, delim); | |
918 if (strcmp(cmd, MANY) == 0) list = BLACK; | |
919 else if (strcmp(cmd, OK) == 0) list = WHITE; | |
920 head = cmd + strlen(cmd) + 1; | |
921 } | |
922 char *cmd = strtok(head, delim); | |
923 if (!cmd) continue; | |
924 if (strcmp(cmd, "env_from") == 0) { | |
925 char *from = next_token(delim); | |
926 if (from) { | |
927 string_map &fm = really_find_from_map(dc, name); | |
928 fm[from] = list; | |
929 } | |
930 } | |
931 else if (strcmp(cmd, "env_to") == 0) { | |
932 char *to = next_token(delim); | |
933 if (to) { | |
934 dc.env_to_dnsbll[to] = list; | |
935 dc.env_to_chkfrom[to] = list; | |
936 } | |
937 } | |
938 else if (strcmp(cmd, "substitute") == 0) { | |
939 char *tag = next_token(delim); | |
940 if (tag && (strcmp(tag, "mail_host") == 0)) { | |
941 char *from = next_token(delim); | |
942 if (from) { | |
943 string_map &fm = really_find_from_map(dc, name); | |
944 fm[from] = list; | |
945 } | |
946 } | |
947 } | |
948 else if (strcmp(cmd, "include") == 0) { | |
949 char *fn = next_token(delim); | |
950 if (ok_to_include(dc, fn)) { | |
951 load_conf_dcc(dc, name, fn); | |
952 } | |
953 } | |
954 | |
955 } | |
956 is.close(); | |
957 } | |
958 | |
959 | |
0 | 960 static void load_conf(CONFIG &dc, char *fn); |
961 static void load_conf(CONFIG &dc, char *fn) { | |
962 dc.config_files.push_back(fn); | |
963 map<char*, int, ltstr> commands; | |
28 | 964 enum {dummy, tld, content, hostlimit, htmllimit, htmltag, dnsbl, dnsbll, envfrom, envto, include, includedcc}; |
965 commands["tld" ] = tld; | |
8 | 966 commands["content" ] = content; |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
967 commands["host_limit" ] = hostlimit; |
24 | 968 commands["html_limit" ] = htmllimit; |
969 commands["html_tag" ] = htmltag; | |
3 | 970 commands["dnsbl" ] = dnsbl; |
971 commands["dnsbl_list" ] = dnsbll; | |
972 commands["env_from" ] = envfrom; | |
973 commands["env_to" ] = envto; | |
974 commands["include" ] = include; | |
975 commands["include_dcc"] = includedcc; | |
0 | 976 const int LINE_SIZE = 2000; |
977 ifstream is(fn); | |
978 if (is.fail()) return; | |
979 char line[LINE_SIZE]; | |
980 char orig[LINE_SIZE]; | |
981 char *delim = " \t"; | |
982 int curline = 0; | |
983 while (!is.eof()) { | |
984 is.getline(line, LINE_SIZE); | |
985 snprintf(orig, sizeof(orig), "%s", line); | |
986 curline++; | |
987 int n = strlen(line); | |
988 for (int i=0; i<n; i++) line[i] = tolower(line[i]); | |
989 char *cmd = strtok(line, delim); | |
990 if (cmd && (cmd[0] != '#') && (cmd[0] != '\0')) { | |
991 // have a decent command | |
992 bool processed = false; | |
993 switch (commands[cmd]) { | |
28 | 994 case tld: { |
29
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
995 char *tld = next_token(delim); |
28 | 996 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
|
997 dc.tlds.insert(tld); |
28 | 998 processed = true; |
999 } break; | |
1000 | |
8 | 1001 case content: { |
1002 char *suff = strtok(NULL, delim); | |
24 | 1003 if (!suff) break; // no dns suffix |
8 | 1004 char *msg = suff + strlen(suff); |
1005 if ((msg - line) >= strlen(orig)) break; // line ended with the dns suffix | |
1006 msg = strchr(msg+1, '\''); | |
1007 if (!msg) break; // no reply message template | |
1008 msg++; // move over the leading ' | |
1009 if ((msg - line) >= strlen(orig)) break; // line ended with the leading quote | |
1010 char *last = strchr(msg, '\''); | |
1011 if (!last) break; // no trailing quote | |
1012 *last = '\0'; // make it a null terminator | |
1013 dc.content_suffix = register_string(suff); | |
1014 dc.content_message = register_string(msg); | |
1015 processed = true; | |
1016 } break; | |
1017 | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1018 case hostlimit: { |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1019 char *limit = strtok(NULL, delim); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1020 if (!limit) break; // no integer limit |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1021 char *msg = limit + strlen(limit); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1022 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
|
1023 msg = strchr(msg+1, '\''); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1024 if (!msg) break; // no reply message template |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1025 msg++; // move over the leading ' |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1026 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
|
1027 char *last = strchr(msg, '\''); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1028 if (!last) break; // no trailing quote |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1029 *last = '\0'; // make it a null terminator |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1030 dc.host_limit = atoi(limit); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1031 dc.host_limit_message = register_string(msg); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1032 processed = true; |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1033 } break; |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1034 |
24 | 1035 case htmllimit: { |
1036 char *limit = strtok(NULL, delim); | |
1037 if (!limit) break; // no integer limit | |
1038 char *msg = limit + strlen(limit); | |
1039 if ((msg - line) >= strlen(orig)) break; // line ended with the limit | |
1040 msg = strchr(msg+1, '\''); | |
1041 if (!msg) break; // no reply message template | |
1042 msg++; // move over the leading ' | |
1043 if ((msg - line) >= strlen(orig)) break; // line ended with the leading quote | |
1044 char *last = strchr(msg, '\''); | |
1045 if (!last) break; // no trailing quote | |
1046 *last = '\0'; // make it a null terminator | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1047 dc.tag_limit = atoi(limit); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1048 dc.tag_limit_message = register_string(msg); |
24 | 1049 processed = true; |
1050 } break; | |
1051 | |
1052 case htmltag: { | |
1053 char *tag = next_token(delim); | |
1054 if (!tag) break; // no html tag value | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1055 dc.html_tags.insert(tag); // base version |
26 | 1056 char buf[200]; |
1057 snprintf(buf, sizeof(buf), "/%s", tag); | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1058 dc.html_tags.insert(register_string(buf)); // leading / |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1059 snprintf(buf, sizeof(buf), "%s/", tag); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1060 dc.html_tags.insert(register_string(buf)); // trailing / |
24 | 1061 processed = true; |
1062 } break; | |
1063 | |
0 | 1064 case dnsbl: { |
1065 // have a new dnsbl to use | |
1066 char *name = next_token(delim); | |
1067 if (!name) break; // no name name | |
1068 if (find_dnsbl(dc, name)) break; // duplicate entry | |
1069 char *suff = strtok(NULL, delim); | |
1070 if (!suff) break; // no dns suffic | |
1071 char *msg = suff + strlen(suff); | |
1072 if ((msg - line) >= strlen(orig)) break; // line ended with the dns suffix | |
1073 msg = strchr(msg+1, '\''); | |
1074 if (!msg) break; // no reply message template | |
1075 msg++; // move over the leading ' | |
1076 if ((msg - line) >= strlen(orig)) break; // line ended with the leading quote | |
1077 char *last = strchr(msg, '\''); | |
1078 if (!last) break; // no trailing quote | |
1079 *last = '\0'; // make it a null terminator | |
1080 dc.dnsbls[name] = new DNSBL(register_string(suff), register_string(msg)); | |
1081 processed = true; | |
1082 } break; | |
1083 | |
1084 case dnsbll: { | |
1085 // define a new combination of dnsbls | |
1086 char *name = next_token(delim); | |
1087 if (!name) break; | |
1088 if (find_dnsbll(dc, name)) break; // duplicate entry | |
1089 char *list = next_token(delim); | |
1090 if (!list || (*list == '\0') || (*list == '#')) break; | |
1091 DNSBLLP d = new DNSBLL; | |
1092 DNSBLP p = find_dnsbl(dc, list); | |
1093 if (p) d->push_back(p); | |
1094 while (true) { | |
1095 list = next_token(delim); | |
1096 if (!list || (*list == '\0') || (*list == '#')) break; | |
1097 DNSBLP p = find_dnsbl(dc, list); | |
1098 if (p) d->push_back(p); | |
1099 } | |
1100 dc.dnsblls[name] = d; | |
1101 processed = true; | |
1102 } break; | |
1103 | |
1104 case envfrom: { | |
1105 // add an entry into the named string_map | |
1106 char *name = next_token(delim); | |
1107 if (!name) break; | |
1108 char *from = next_token(delim); | |
1109 if (!from) break; | |
1110 char *list = next_token(delim); | |
1111 if (!list) break; | |
1112 if ((strcmp(list, WHITE) == 0) || | |
1113 (strcmp(list, BLACK) == 0)) { | |
1114 string_map &fm = really_find_from_map(dc, name); | |
1115 fm[from] = list; | |
1116 processed = true; | |
1117 } | |
1118 else { | |
1119 // list may be the name of a previously defined from_map | |
1120 string_map *m = find_from_map(dc, list); | |
1121 if (m && (strcmp(list,name) != 0)) { | |
1122 string_map &pm = *m; | |
1123 string_map &fm = really_find_from_map(dc, name); | |
1124 fm.insert(pm.begin(), pm.end()); | |
1125 processed = true; | |
1126 } | |
1127 } | |
1128 } break; | |
1129 | |
1130 case envto: { | |
1131 // define the dnsbl_list and env_from maps to use for this recipient | |
1132 char *to = next_token(delim); | |
1133 if (!to) break; | |
1134 char *list = next_token(delim); | |
1135 if (!list) break; | |
1136 char *from = next_token(delim); | |
1137 if (!from) break; | |
1138 dc.env_to_dnsbll[to] = list; | |
1139 dc.env_to_chkfrom[to] = from; | |
1140 processed = true; | |
1141 } break; | |
1142 | |
1143 case include: { | |
1144 char *fn = next_token(delim); | |
3 | 1145 if (ok_to_include(dc, fn)) { |
1146 load_conf(dc, fn); | |
1147 processed = true; | |
1148 } | |
1149 } break; | |
1150 | |
1151 case includedcc: { | |
1152 char *name = next_token(delim); | |
1153 if (!name) break; | |
1154 char *fn = next_token(delim); | |
1155 if (ok_to_include(dc, fn)) { | |
1156 load_conf_dcc(dc, name, fn); | |
1157 processed = true; | |
0 | 1158 } |
1159 } break; | |
1160 | |
1161 default: { | |
1162 } break; | |
1163 } | |
1164 if (!processed) { | |
1165 pthread_mutex_lock(&syslog_mutex); | |
1166 openlog("dnsbl", LOG_PID, LOG_MAIL); | |
1167 syslog(LOG_ERR, "ignoring file %s line %d : %s\n", fn, curline, orig); | |
1168 closelog(); | |
1169 pthread_mutex_unlock(&syslog_mutex); | |
1170 } | |
1171 } | |
1172 } | |
1173 is.close(); | |
1174 } | |
1175 | |
1176 | |
1177 //////////////////////////////////////////////// | |
1178 // reload the config | |
1179 // | |
1180 static CONFIG* new_conf(); | |
1181 static CONFIG* new_conf() { | |
1182 CONFIG *newc = new CONFIG; | |
29
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1183 pthread_mutex_lock(&config_mutex); |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1184 newc->generation = generation++; |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1185 pthread_mutex_unlock(&config_mutex); |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1186 char buf[200]; |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1187 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
|
1188 my_syslog(buf); |
0 | 1189 load_conf(*newc, "dnsbl.conf"); |
1190 newc->load_time = time(NULL); | |
1191 return newc; | |
1192 } | |
1193 | |
1194 | |
1195 //////////////////////////////////////////////// | |
1196 // thread to watch the old config files for changes | |
1197 // and reload when needed. we also cleanup old | |
1198 // configs whose reference count has gone to zero. | |
1199 // | |
1200 static void* config_loader(void *arg); | |
1201 static void* config_loader(void *arg) { | |
1202 typedef set<CONFIG *> configp_set; | |
1203 configp_set old_configs; | |
18 | 1204 while (loader_run) { |
0 | 1205 sleep(180); // look for modifications every 3 minutes |
18 | 1206 if (!loader_run) break; |
0 | 1207 CONFIG &dc = *config; |
1208 time_t then = dc.load_time; | |
1209 struct stat st; | |
1210 bool reload = false; | |
1211 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) { | |
1212 char *fn = *i; | |
1213 if (stat(fn, &st)) reload = true; // file disappeared | |
1214 else if (st.st_mtime > then) reload = true; // file modified | |
1215 if (reload) break; | |
1216 } | |
1217 if (reload) { | |
1218 CONFIG *newc = new_conf(); | |
1219 // replace the global config pointer | |
1220 pthread_mutex_lock(&config_mutex); | |
1221 CONFIG *old = config; | |
1222 config = newc; | |
1223 pthread_mutex_unlock(&config_mutex); | |
1224 if (old) old_configs.insert(old); | |
1225 } | |
1226 // now look for old configs with zero ref counts | |
1227 for (configp_set::iterator i=old_configs.begin(); i!=old_configs.end(); ) { | |
1228 CONFIG *old = *i; | |
1229 if (!old->reference_count) { | |
29
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1230 char buf[200]; |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1231 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
|
1232 my_syslog(buf); |
0 | 1233 delete old; // destructor does all the work |
1234 old_configs.erase(i++); | |
1235 } | |
1236 else i++; | |
1237 } | |
1238 } | |
18 | 1239 return NULL; |
0 | 1240 } |
1241 | |
1242 | |
1243 static void usage(char *prog); | |
1244 static void usage(char *prog) | |
1245 { | |
16 | 1246 fprintf(stderr, "Usage: %s [-d] [-c] -p socket-addr [-t timeout]\n", prog); |
0 | 1247 fprintf(stderr, "where socket-addr is for the connection to sendmail and should be one of\n"); |
1248 fprintf(stderr, " inet:port@local-ip-address\n"); | |
1249 fprintf(stderr, " local:local-domain-socket-file-name\n"); | |
9 | 1250 fprintf(stderr, "-c will load and dump the config to stdout\n"); |
16 | 1251 fprintf(stderr, "-d will add some syslog debug messages\n"); |
0 | 1252 } |
1253 | |
1254 | |
1255 int main(int argc, char**argv) | |
1256 { | |
3 | 1257 bool check = false; |
1258 bool setconn = false; | |
0 | 1259 int c; |
16 | 1260 const char *args = "p:t:hcd"; |
0 | 1261 extern char *optarg; |
1262 | |
1263 // Process command line options | |
1264 while ((c = getopt(argc, argv, args)) != -1) { | |
1265 switch (c) { | |
1266 case 'p': | |
1267 if (optarg == NULL || *optarg == '\0') { | |
1268 fprintf(stderr, "Illegal conn: %s\n", optarg); | |
1269 exit(EX_USAGE); | |
1270 } | |
1271 if (smfi_setconn(optarg) == MI_FAILURE) { | |
1272 fprintf(stderr, "smfi_setconn failed\n"); | |
1273 exit(EX_SOFTWARE); | |
1274 } | |
1275 | |
1276 if (strncasecmp(optarg, "unix:", 5) == 0) unlink(optarg + 5); | |
1277 else if (strncasecmp(optarg, "local:", 6) == 0) unlink(optarg + 6); | |
3 | 1278 setconn = true; |
0 | 1279 break; |
1280 | |
1281 case 't': | |
1282 if (optarg == NULL || *optarg == '\0') { | |
1283 fprintf(stderr, "Illegal timeout: %s\n", optarg); | |
1284 exit(EX_USAGE); | |
1285 } | |
1286 if (smfi_settimeout(atoi(optarg)) == MI_FAILURE) { | |
1287 fprintf(stderr, "smfi_settimeout failed\n"); | |
1288 exit(EX_SOFTWARE); | |
1289 } | |
1290 break; | |
1291 | |
3 | 1292 case 'c': |
1293 check = true; | |
1294 break; | |
1295 | |
16 | 1296 case 'd': |
1297 debug_syslog = true; | |
1298 break; | |
1299 | |
0 | 1300 case 'h': |
1301 default: | |
1302 usage(argv[0]); | |
1303 exit(EX_USAGE); | |
1304 } | |
1305 } | |
5 | 1306 |
1307 if (check) { | |
1308 CONFIG &dc = *new_conf(); | |
1309 dumpit(dc); | |
1310 return 0; | |
1311 } | |
1312 | |
0 | 1313 if (!setconn) { |
1314 fprintf(stderr, "%s: Missing required -p argument\n", argv[0]); | |
1315 usage(argv[0]); | |
1316 exit(EX_USAGE); | |
1317 } | |
5 | 1318 |
0 | 1319 if (smfi_register(smfilter) == MI_FAILURE) { |
1320 fprintf(stderr, "smfi_register failed\n"); | |
1321 exit(EX_UNAVAILABLE); | |
1322 } | |
1323 | |
1324 // switch to background mode | |
1325 if (daemon(1,0) < 0) { | |
1326 fprintf(stderr, "daemon() call failed\n"); | |
1327 exit(EX_UNAVAILABLE); | |
1328 } | |
1329 | |
1330 // initialize the thread sync objects | |
1331 pthread_mutex_init(&config_mutex, 0); | |
1332 pthread_mutex_init(&syslog_mutex, 0); | |
1333 pthread_mutex_init(&resolve_mutex, 0); | |
1334 | |
1335 // load the initial config | |
1336 config = new_conf(); | |
1337 | |
1338 // only create threads after the fork() in daemon | |
1339 pthread_t tid; | |
1340 if (pthread_create(&tid, 0, config_loader, 0)) | |
1341 my_syslog("failed to create config loader thread"); | |
1342 if (pthread_detach(tid)) | |
1343 my_syslog("failed to detach config loader thread"); | |
1344 | |
1345 // write the pid | |
1346 const char *pidpath = "/var/run/dnsbl.pid"; | |
1347 unlink(pidpath); | |
1348 FILE *f = fopen(pidpath, "w"); | |
1349 if (f) { | |
22 | 1350 #ifdef linux |
1351 // from a comment in the DCC source code: | |
1352 // Linux threads are broken. Signals given the | |
1353 // original process are delivered to only the | |
1354 // thread that happens to have that PID. The | |
1355 // sendmail libmilter thread that needs to hear | |
1356 // SIGINT and other signals does not, and that breaks | |
1357 // scripts that need to stop milters. | |
1358 // However, signaling the process group works. | |
0 | 1359 fprintf(f, "-%d\n", (u_int)getpgrp()); |
22 | 1360 #else |
1361 fprintf(f, "%d\n", (u_int)getpid()); | |
1362 #endif | |
0 | 1363 fclose(f); |
1364 } | |
1365 | |
18 | 1366 time_t starting = time(NULL); |
1367 int rc = smfi_main(); | |
22 | 1368 if ((rc != MI_SUCCESS) && (time(NULL) > starting+5*60)) { |
18 | 1369 my_syslog("trying to restart after smfi_main()"); |
1370 loader_run = false; // eventually the config loader thread will terminate | |
1371 execvp(argv[0], argv); | |
1372 } | |
1373 exit((rc == MI_SUCCESS) ? 0 : EX_UNAVAILABLE); | |
0 | 1374 } |
8 | 1375 |