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