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