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