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