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