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