Mercurial > dnsbl
annotate src/dnsbl.cpp @ 58:7bb8bbf79285
changes to handle 5iantlavalamp.com
author | carl |
---|---|
date | Thu, 28 Oct 2004 22:54:34 -0700 |
parents | 419e00901570 |
children | 510a511ad554 |
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 |
58 | 612 |
613 // don't bother looking up hosts on the ignore list | |
57 | 614 string_set::iterator j = priv.pc->content_host_ignore.find(host); |
58 | 615 if (j != priv.pc->content_host_ignore.end()) continue; |
616 | |
617 // try to only look at lim/cnt fraction of the available cnt host names in random mode | |
44 | 618 if ((cnt > lim) && (lim > 0) && ran) { |
619 int r = rand() % cnt; | |
620 if (r >= lim) { | |
621 char buf[1000]; | |
622 snprintf(buf, sizeof(buf), "host %s skipped", host); | |
623 my_syslog(&priv, buf); | |
624 continue; | |
625 } | |
626 } | |
8 | 627 count++; |
44 | 628 if ((count > lim) && (lim > 0) && (!ran)) { |
34 | 629 discard(nameservers); |
630 return reject_host; | |
631 } | |
44 | 632 ip = protected_dns_interface(host, true, &nameservers); |
16 | 633 if (debug_syslog) { |
44 | 634 char buf[1000]; |
29
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
635 if (ip) { |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
636 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
|
637 adr[0] = '\0'; |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
638 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
|
639 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
|
640 } |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
641 else { |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
642 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
|
643 } |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
644 my_syslog(&priv, buf); |
16 | 645 } |
8 | 646 if (ip) { |
44 | 647 int_set::iterator i = ips.find(ip); |
648 if (i == ips.end()) { | |
649 ips.insert(ip); | |
650 status st = check_single(ip, dc.content_suffix); | |
651 if (st == reject) { | |
652 discard(nameservers); | |
653 return st; | |
654 } | |
34 | 655 } |
8 | 656 } |
657 } | |
34 | 658 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
|
659 for (ns_mapper::iterator i=nameservers.ns_ip.begin(); i!=nameservers.ns_ip.end(); i++) { |
34 | 660 count++; |
661 if ((count > lim) && (lim > 0)) { | |
44 | 662 if (ran) continue; // don't complain |
34 | 663 discard(nameservers); |
664 return reject_host; | |
665 } | |
36 | 666 host = (*i).first; // a transient reference that needs to be replaced before we return it |
34 | 667 ip = (*i).second; |
668 if (!ip) ip = protected_dns_interface(host, false, NULL); | |
669 if (debug_syslog) { | |
670 char buf[200]; | |
671 if (ip) { | |
672 char adr[sizeof "255.255.255.255"]; | |
673 adr[0] = '\0'; | |
674 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr)); | |
675 snprintf(buf, sizeof(buf), "ns %s found at %s", host, adr); | |
676 } | |
677 else { | |
678 snprintf(buf, sizeof(buf), "ns %s not found", host); | |
679 } | |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
680 my_syslog(&priv, buf); |
34 | 681 } |
682 if (ip) { | |
44 | 683 int_set::iterator i = ips.find(ip); |
684 if (i == ips.end()) { | |
685 ips.insert(ip); | |
686 status st = check_single(ip, dc.content_suffix); | |
687 if (st == reject) { | |
53
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
688 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
|
689 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
|
690 char *refer = (*j).second; |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
691 char buf[1000]; |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
692 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
|
693 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
|
694 } |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
695 else { |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
696 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
|
697 } |
44 | 698 discard(nameservers); |
699 return st; | |
700 } | |
34 | 701 } |
702 } | |
703 } | |
704 discard(nameservers); | |
24 | 705 host = NULL; |
26 | 706 int bin = priv.memory->binary_tags; |
24 | 707 int bad = priv.memory->bad_html_tags; |
34 | 708 lim = priv.pc->tag_limit; |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
709 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
|
710 if ((bad > lim) && (lim > 0)) return reject_tag; |
9 | 711 return oksofar; |
8 | 712 } |
713 | |
714 | |
715 //////////////////////////////////////////////// | |
0 | 716 // start of sendmail milter interfaces |
717 // | |
718 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr) | |
719 { | |
720 // allocate some private memory | |
721 mlfiPriv *priv = new mlfiPriv; | |
722 if (hostaddr->sa_family == AF_INET) { | |
723 priv->ip = ((struct sockaddr_in *)hostaddr)->sin_addr.s_addr; | |
724 } | |
725 | |
726 // save the private data | |
727 smfi_setpriv(ctx, (void*)priv); | |
728 | |
729 // continue processing | |
730 return SMFIS_CONTINUE; | |
731 } | |
732 | |
733 sfsistat mlfi_envfrom(SMFICTX *ctx, char **from) | |
734 { | |
735 mlfiPriv &priv = *MLFIPRIV; | |
736 priv.mailaddr = strdup(from[0]); | |
737 priv.authenticated = (smfi_getsymval(ctx, "{auth_authen}") != NULL); | |
738 return SMFIS_CONTINUE; | |
739 } | |
740 | |
741 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **rcpt) | |
742 { | |
743 DNSBLP rejectlist = NULL; // list that caused the reject | |
744 status st = oksofar; | |
745 mlfiPriv &priv = *MLFIPRIV; | |
746 CONFIG &dc = *priv.pc; | |
42 | 747 if (!priv.queueid) priv.queueid = strdup(smfi_getsymval(ctx, "i")); |
0 | 748 char *rcptaddr = rcpt[0]; |
749 char *dnsname = lookup(rcptaddr, dc.env_to_dnsbll); | |
750 char *fromname = lookup(rcptaddr, dc.env_to_chkfrom); | |
751 if ((strcmp(dnsname, BLACK) == 0) || | |
752 (strcmp(fromname, BLACK) == 0)) { | |
753 st = black; // two options to blacklist this recipient | |
754 } | |
755 else if (strcmp(fromname, WHITE) == 0) { | |
756 st = white; | |
757 } | |
758 else { | |
759 // check an env_from map | |
760 string_map *sm = find_from_map(dc, fromname); | |
761 if (sm != NULL) { | |
762 fromname = lookup(priv.mailaddr, *sm); // returns default if name not in map | |
763 if (strcmp(fromname, BLACK) == 0) { | |
764 st = black; // blacklist this envelope from value | |
765 } | |
766 if (strcmp(fromname, WHITE) == 0) { | |
767 st = white; // blacklist this envelope from value | |
768 } | |
769 } | |
770 } | |
771 if ((st == oksofar) && (strcmp(dnsname, WHITE) != 0)) { | |
772 // check dns lists | |
773 st = check_dnsbl(priv, find_dnsbll(dc, dnsname), rejectlist); | |
774 } | |
775 | |
776 if (st == reject) { | |
777 // reject the recipient based on some dnsbl | |
778 char adr[sizeof "255.255.255.255"]; | |
779 adr[0] = '\0'; | |
8 | 780 inet_ntop(AF_INET, (const u_char *)&priv.ip, adr, sizeof(adr)); |
0 | 781 char buf[2000]; |
782 snprintf(buf, sizeof(buf), rejectlist->message, adr, adr); | |
783 smfi_setreply(ctx, "550", "5.7.1", buf); | |
784 return SMFIS_REJECT; | |
785 } | |
786 else if (st == black) { | |
787 // reject the recipient based on blacklisting either from or to | |
788 smfi_setreply(ctx, "550", "5.7.1", "no such user"); | |
789 return SMFIS_REJECT; | |
790 } | |
791 else { | |
792 // accept the recipient | |
8 | 793 if (st == oksofar) { |
794 // but remember the non-whites | |
12 | 795 register_string(priv.non_whites, rcptaddr); |
8 | 796 priv.only_whites = false; |
797 } | |
798 if (st == white) { | |
799 priv.have_whites = true; | |
800 } | |
0 | 801 return SMFIS_CONTINUE; |
802 } | |
803 } | |
804 | |
8 | 805 sfsistat mlfi_body(SMFICTX *ctx, u_char *data, size_t len) |
0 | 806 { |
807 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
|
808 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
|
809 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
|
810 if (!priv.pc->content_suffix) return SMFIS_CONTINUE; |
8 | 811 priv.scanner->scan(data, len); |
11 | 812 return SMFIS_CONTINUE; |
8 | 813 } |
814 | |
815 sfsistat mlfi_eom(SMFICTX *ctx) | |
816 { | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
817 sfsistat rc; |
8 | 818 mlfiPriv &priv = *MLFIPRIV; |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
819 char *host = NULL; |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
820 int ip; |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
821 status st; |
8 | 822 // 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
|
823 if (priv.authenticated || |
44babba1a9b9
updates for 3.6, better documentation on removing content filtering, missing some files in cvs
carl
parents:
53
diff
changeset
|
824 priv.only_whites || |
44babba1a9b9
updates for 3.6, better documentation on removing content filtering, missing some files in cvs
carl
parents:
53
diff
changeset
|
825 (!priv.pc->content_suffix) || |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
826 ((st=check_hosts(priv, host, ip)) == oksofar)) rc = SMFIS_CONTINUE; |
8 | 827 else { |
828 if (!priv.have_whites) { | |
829 // can reject the entire message | |
830 char buf[2000]; | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
831 if (st == reject_tag) { |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
832 // rejected due to excessive bad html tags |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
833 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
|
834 } |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
835 else if (st == reject_host) { |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
836 // rejected due to excessive unique host/urls |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
837 snprintf(buf, sizeof(buf), priv.pc->host_limit_message); |
24 | 838 } |
839 else { | |
840 char adr[sizeof "255.255.255.255"]; | |
841 adr[0] = '\0'; | |
842 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr)); | |
843 snprintf(buf, sizeof(buf), priv.pc->content_message, host, adr); | |
844 } | |
8 | 845 smfi_setreply(ctx, "550", "5.7.1", buf); |
846 rc = SMFIS_REJECT; | |
847 } | |
848 else { | |
849 // need to accept it but remove the recipients that don't want it | |
850 for (string_set::iterator i=priv.non_whites.begin(); i!=priv.non_whites.end(); i++) { | |
851 char *rcpt = *i; | |
852 smfi_delrcpt(ctx, rcpt); | |
853 } | |
854 rc = SMFIS_CONTINUE; | |
855 } | |
0 | 856 } |
8 | 857 // reset for a new message on the same connection |
858 mlfi_abort(ctx); | |
859 return rc; | |
860 } | |
861 | |
862 sfsistat mlfi_abort(SMFICTX *ctx) | |
863 { | |
864 mlfiPriv &priv = *MLFIPRIV; | |
865 priv.reset(); | |
0 | 866 return SMFIS_CONTINUE; |
867 } | |
868 | |
869 sfsistat mlfi_close(SMFICTX *ctx) | |
870 { | |
871 mlfiPriv *priv = MLFIPRIV; | |
872 if (!priv) return SMFIS_CONTINUE; | |
873 delete priv; | |
874 smfi_setpriv(ctx, NULL); | |
875 return SMFIS_CONTINUE; | |
876 } | |
877 | |
878 struct smfiDesc smfilter = | |
879 { | |
880 "DNSBL", // filter name | |
881 SMFI_VERSION, // version code -- do not change | |
882 SMFIF_DELRCPT, // flags | |
883 mlfi_connect, // connection info filter | |
884 NULL, // SMTP HELO command filter | |
885 mlfi_envfrom, // envelope sender filter | |
886 mlfi_envrcpt, // envelope recipient filter | |
887 NULL, // header filter | |
888 NULL, // end of header | |
8 | 889 mlfi_body, // body block filter |
890 mlfi_eom, // end of message | |
891 mlfi_abort, // message aborted | |
0 | 892 mlfi_close, // connection cleanup |
893 }; | |
894 | |
895 | |
896 static void dumpit(char *name, string_map map); | |
897 static void dumpit(char *name, string_map map) { | |
9 | 898 fprintf(stdout, "\n"); |
0 | 899 for (string_map::iterator i=map.begin(); i!=map.end(); i++) { |
9 | 900 fprintf(stdout, "%s %s->%s\n", name, (*i).first, (*i).second); |
0 | 901 } |
902 } | |
903 | |
904 | |
905 static void dumpit(from_map map); | |
906 static void dumpit(from_map map) { | |
907 for (from_map::iterator i=map.begin(); i!=map.end(); i++) { | |
3 | 908 char buf[2000]; |
909 snprintf(buf, sizeof(buf), "envelope from map for %s", (*i).first); | |
0 | 910 string_map *sm = (*i).second; |
3 | 911 dumpit(buf, *sm); |
0 | 912 } |
913 } | |
914 | |
915 | |
3 | 916 static void dumpit(CONFIG &dc); |
917 static void dumpit(CONFIG &dc) { | |
5 | 918 dumpit(dc.env_from); |
919 dumpit("envelope to (dnsbl list)", dc.env_to_dnsbll); | |
920 dumpit("envelope to (from map)", dc.env_to_chkfrom); | |
9 | 921 fprintf(stdout, "\ndnsbls\n"); |
0 | 922 for (dnsblp_map::iterator i=dc.dnsbls.begin(); i!=dc.dnsbls.end(); i++) { |
9 | 923 fprintf(stdout, "%s %s %s\n", (*i).first, (*i).second->suffix, (*i).second->message); |
0 | 924 } |
9 | 925 fprintf(stdout, "\ndnsbl_lists\n"); |
0 | 926 for (dnsbllp_map::iterator i=dc.dnsblls.begin(); i!=dc.dnsblls.end(); i++) { |
927 char *name = (*i).first; | |
928 DNSBLL &dl = *((*i).second); | |
9 | 929 fprintf(stdout, "%s", name); |
0 | 930 for (DNSBLL::iterator j=dl.begin(); j!=dl.end(); j++) { |
931 DNSBL &d = **j; | |
9 | 932 fprintf(stdout, " %s", d.suffix); |
0 | 933 } |
9 | 934 fprintf(stdout, "\n"); |
0 | 935 } |
9 | 936 if (dc.content_suffix) { |
937 fprintf(stdout, "\ncontent filtering enabled with %s %s\n", dc.content_suffix, dc.content_message); | |
938 } | |
57 | 939 for (string_set::iterator i=dc.content_host_ignore.begin(); i!=dc.content_host_ignore.end(); i++) { |
940 fprintf(stdout, "ignore %s\n", (*i)); | |
941 } | |
46 | 942 if (dc.host_limit && !dc.host_random) { |
943 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
|
944 } |
46 | 945 if (dc.host_limit && dc.host_random) { |
946 fprintf(stdout, "\ncontent filtering for host names soft limit %d\n", dc.host_limit); | |
44 | 947 } |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
948 if (dc.tag_limit) { |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
949 fprintf(stdout, "\ncontent filtering for excessive html tags enabled with limit %d %s\n", dc.tag_limit, dc.tag_limit_message); |
24 | 950 } |
9 | 951 fprintf(stdout, "\nfiles\n"); |
3 | 952 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) { |
953 char *f = *i; | |
9 | 954 fprintf(stdout, "config includes %s\n", f); |
3 | 955 } |
956 } | |
957 | |
958 | |
959 //////////////////////////////////////////////// | |
960 // check for redundant or recursive include files | |
961 // | |
962 static bool ok_to_include(CONFIG &dc, char *fn); | |
963 static bool ok_to_include(CONFIG &dc, char *fn) { | |
964 if (!fn) return false; | |
965 bool ok = true; | |
966 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) { | |
967 char *f = *i; | |
968 if (strcmp(f, fn) == 0) { | |
969 my_syslog("redundant or recursive include file detected"); | |
970 ok = false; | |
971 break; | |
972 } | |
973 } | |
974 return ok; | |
0 | 975 } |
976 | |
977 | |
978 //////////////////////////////////////////////// | |
979 // load a single config file | |
980 // | |
3 | 981 static void load_conf_dcc(CONFIG &dc, char *name, char *fn); |
982 static void load_conf_dcc(CONFIG &dc, char *name, char *fn) { | |
983 ifstream is(fn); | |
40 | 984 if (is.fail()) { |
985 char buf[1000]; | |
986 snprintf(buf, sizeof(buf), "include file %s not found", fn); | |
987 my_syslog(buf); | |
988 return; | |
989 } | |
39 | 990 dc.config_files.push_back(fn); |
991 const int LINE_SIZE = 2000; | |
3 | 992 char line[LINE_SIZE]; |
39 | 993 char *list = BLACK; |
3 | 994 char *delim = " \t"; |
995 int curline = 0; | |
996 while (!is.eof()) { | |
997 is.getline(line, LINE_SIZE); | |
998 curline++; | |
999 int n = strlen(line); | |
1000 if (!n) continue; | |
1001 for (int i=0; i<n; i++) line[i] = tolower(line[i]); | |
1002 if (line[0] == '#') continue; | |
1003 char *head = line; | |
1004 if (strspn(line, delim) == 0) { | |
1005 // have a leading ok/many tag to fetch | |
1006 char *cmd = strtok(line, delim); | |
1007 if (strcmp(cmd, MANY) == 0) list = BLACK; | |
1008 else if (strcmp(cmd, OK) == 0) list = WHITE; | |
1009 head = cmd + strlen(cmd) + 1; | |
1010 } | |
1011 char *cmd = strtok(head, delim); | |
1012 if (!cmd) continue; | |
1013 if (strcmp(cmd, "env_from") == 0) { | |
1014 char *from = next_token(delim); | |
1015 if (from) { | |
1016 string_map &fm = really_find_from_map(dc, name); | |
1017 fm[from] = list; | |
1018 } | |
1019 } | |
1020 else if (strcmp(cmd, "env_to") == 0) { | |
1021 char *to = next_token(delim); | |
1022 if (to) { | |
1023 dc.env_to_dnsbll[to] = list; | |
1024 dc.env_to_chkfrom[to] = list; | |
1025 } | |
1026 } | |
1027 else if (strcmp(cmd, "substitute") == 0) { | |
1028 char *tag = next_token(delim); | |
1029 if (tag && (strcmp(tag, "mail_host") == 0)) { | |
1030 char *from = next_token(delim); | |
1031 if (from) { | |
1032 string_map &fm = really_find_from_map(dc, name); | |
1033 fm[from] = list; | |
1034 } | |
1035 } | |
1036 } | |
1037 else if (strcmp(cmd, "include") == 0) { | |
1038 char *fn = next_token(delim); | |
1039 if (ok_to_include(dc, fn)) { | |
1040 load_conf_dcc(dc, name, fn); | |
1041 } | |
1042 } | |
1043 | |
1044 } | |
1045 is.close(); | |
1046 } | |
1047 | |
1048 | |
0 | 1049 static void load_conf(CONFIG &dc, char *fn); |
1050 static void load_conf(CONFIG &dc, char *fn) { | |
39 | 1051 ifstream is(fn); |
40 | 1052 if (is.fail()) { |
1053 char buf[1000]; | |
1054 snprintf(buf, sizeof(buf), "include file %s not found", fn); | |
1055 my_syslog(buf); | |
1056 return; | |
1057 } | |
0 | 1058 dc.config_files.push_back(fn); |
1059 map<char*, int, ltstr> commands; | |
57 | 1060 enum {dummy, tld, content, ignore, hostlimit, hostslimit, htmllimit, htmltag, dnsbl, dnsbll, envfrom, envto, include, includedcc}; |
46 | 1061 commands["tld" ] = tld; |
1062 commands["content" ] = content; | |
57 | 1063 commands["ignore" ] = ignore; |
46 | 1064 commands["host_limit" ] = hostlimit; |
1065 commands["host_soft_limit"] = hostslimit; | |
1066 commands["html_limit" ] = htmllimit; | |
1067 commands["html_tag" ] = htmltag; | |
1068 commands["dnsbl" ] = dnsbl; | |
1069 commands["dnsbl_list" ] = dnsbll; | |
1070 commands["env_from" ] = envfrom; | |
1071 commands["env_to" ] = envto; | |
1072 commands["include" ] = include; | |
1073 commands["include_dcc" ] = includedcc; | |
0 | 1074 const int LINE_SIZE = 2000; |
1075 char line[LINE_SIZE]; | |
1076 char orig[LINE_SIZE]; | |
1077 char *delim = " \t"; | |
1078 int curline = 0; | |
1079 while (!is.eof()) { | |
1080 is.getline(line, LINE_SIZE); | |
1081 snprintf(orig, sizeof(orig), "%s", line); | |
1082 curline++; | |
1083 int n = strlen(line); | |
1084 for (int i=0; i<n; i++) line[i] = tolower(line[i]); | |
1085 char *cmd = strtok(line, delim); | |
1086 if (cmd && (cmd[0] != '#') && (cmd[0] != '\0')) { | |
1087 // have a decent command | |
1088 bool processed = false; | |
1089 switch (commands[cmd]) { | |
28 | 1090 case tld: { |
29
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1091 char *tld = next_token(delim); |
28 | 1092 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
|
1093 dc.tlds.insert(tld); |
28 | 1094 processed = true; |
1095 } break; | |
1096 | |
8 | 1097 case content: { |
1098 char *suff = strtok(NULL, delim); | |
24 | 1099 if (!suff) break; // no dns suffix |
8 | 1100 char *msg = suff + strlen(suff); |
1101 if ((msg - line) >= strlen(orig)) break; // line ended with the dns suffix | |
1102 msg = strchr(msg+1, '\''); | |
1103 if (!msg) break; // no reply message template | |
1104 msg++; // move over the leading ' | |
1105 if ((msg - line) >= strlen(orig)) break; // line ended with the leading quote | |
1106 char *last = strchr(msg, '\''); | |
1107 if (!last) break; // no trailing quote | |
1108 *last = '\0'; // make it a null terminator | |
1109 dc.content_suffix = register_string(suff); | |
1110 dc.content_message = register_string(msg); | |
1111 processed = true; | |
1112 } break; | |
1113 | |
57 | 1114 case ignore: { |
1115 char *host = next_token(delim); | |
1116 if (!host) break; | |
1117 dc.content_host_ignore.insert(host); | |
1118 processed = true; | |
1119 } break; | |
1120 | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1121 case hostlimit: { |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1122 char *limit = strtok(NULL, delim); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1123 if (!limit) break; // no integer limit |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1124 char *msg = limit + strlen(limit); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1125 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
|
1126 msg = strchr(msg+1, '\''); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1127 if (!msg) break; // no reply message template |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1128 msg++; // move over the leading ' |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1129 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
|
1130 char *last = strchr(msg, '\''); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1131 if (!last) break; // no trailing quote |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1132 *last = '\0'; // make it a null terminator |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1133 dc.host_limit = atoi(limit); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1134 dc.host_limit_message = register_string(msg); |
46 | 1135 dc.host_random = false; |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1136 processed = true; |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1137 } break; |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1138 |
46 | 1139 case hostslimit: { |
1140 char *limit = next_token(delim); | |
1141 if (!limit) break; // no integer limit | |
1142 dc.host_limit = atoi(limit); | |
44 | 1143 dc.host_random = true; |
1144 processed = true; | |
1145 } break; | |
1146 | |
24 | 1147 case htmllimit: { |
1148 char *limit = strtok(NULL, delim); | |
1149 if (!limit) break; // no integer limit | |
1150 char *msg = limit + strlen(limit); | |
1151 if ((msg - line) >= strlen(orig)) break; // line ended with the limit | |
1152 msg = strchr(msg+1, '\''); | |
1153 if (!msg) break; // no reply message template | |
1154 msg++; // move over the leading ' | |
1155 if ((msg - line) >= strlen(orig)) break; // line ended with the leading quote | |
1156 char *last = strchr(msg, '\''); | |
1157 if (!last) break; // no trailing quote | |
1158 *last = '\0'; // make it a null terminator | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1159 dc.tag_limit = atoi(limit); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1160 dc.tag_limit_message = register_string(msg); |
24 | 1161 processed = true; |
1162 } break; | |
1163 | |
1164 case htmltag: { | |
1165 char *tag = next_token(delim); | |
1166 if (!tag) break; // no html tag value | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1167 dc.html_tags.insert(tag); // base version |
26 | 1168 char buf[200]; |
1169 snprintf(buf, sizeof(buf), "/%s", tag); | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1170 dc.html_tags.insert(register_string(buf)); // leading / |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1171 snprintf(buf, sizeof(buf), "%s/", tag); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1172 dc.html_tags.insert(register_string(buf)); // trailing / |
24 | 1173 processed = true; |
1174 } break; | |
1175 | |
0 | 1176 case dnsbl: { |
1177 // have a new dnsbl to use | |
1178 char *name = next_token(delim); | |
1179 if (!name) break; // no name name | |
1180 if (find_dnsbl(dc, name)) break; // duplicate entry | |
1181 char *suff = strtok(NULL, delim); | |
1182 if (!suff) break; // no dns suffic | |
1183 char *msg = suff + strlen(suff); | |
1184 if ((msg - line) >= strlen(orig)) break; // line ended with the dns suffix | |
1185 msg = strchr(msg+1, '\''); | |
1186 if (!msg) break; // no reply message template | |
1187 msg++; // move over the leading ' | |
1188 if ((msg - line) >= strlen(orig)) break; // line ended with the leading quote | |
1189 char *last = strchr(msg, '\''); | |
1190 if (!last) break; // no trailing quote | |
1191 *last = '\0'; // make it a null terminator | |
1192 dc.dnsbls[name] = new DNSBL(register_string(suff), register_string(msg)); | |
1193 processed = true; | |
1194 } break; | |
1195 | |
1196 case dnsbll: { | |
1197 // define a new combination of dnsbls | |
1198 char *name = next_token(delim); | |
1199 if (!name) break; | |
1200 if (find_dnsbll(dc, name)) break; // duplicate entry | |
1201 char *list = next_token(delim); | |
1202 if (!list || (*list == '\0') || (*list == '#')) break; | |
1203 DNSBLLP d = new DNSBLL; | |
1204 DNSBLP p = find_dnsbl(dc, list); | |
1205 if (p) d->push_back(p); | |
1206 while (true) { | |
1207 list = next_token(delim); | |
1208 if (!list || (*list == '\0') || (*list == '#')) break; | |
1209 DNSBLP p = find_dnsbl(dc, list); | |
1210 if (p) d->push_back(p); | |
1211 } | |
1212 dc.dnsblls[name] = d; | |
1213 processed = true; | |
1214 } break; | |
1215 | |
1216 case envfrom: { | |
1217 // add an entry into the named string_map | |
1218 char *name = next_token(delim); | |
1219 if (!name) break; | |
1220 char *from = next_token(delim); | |
1221 if (!from) break; | |
1222 char *list = next_token(delim); | |
1223 if (!list) break; | |
1224 if ((strcmp(list, WHITE) == 0) || | |
1225 (strcmp(list, BLACK) == 0)) { | |
1226 string_map &fm = really_find_from_map(dc, name); | |
1227 fm[from] = list; | |
1228 processed = true; | |
1229 } | |
1230 else { | |
1231 // list may be the name of a previously defined from_map | |
1232 string_map *m = find_from_map(dc, list); | |
1233 if (m && (strcmp(list,name) != 0)) { | |
1234 string_map &pm = *m; | |
1235 string_map &fm = really_find_from_map(dc, name); | |
1236 fm.insert(pm.begin(), pm.end()); | |
1237 processed = true; | |
1238 } | |
1239 } | |
1240 } break; | |
1241 | |
1242 case envto: { | |
1243 // define the dnsbl_list and env_from maps to use for this recipient | |
1244 char *to = next_token(delim); | |
1245 if (!to) break; | |
1246 char *list = next_token(delim); | |
1247 if (!list) break; | |
1248 char *from = next_token(delim); | |
1249 if (!from) break; | |
1250 dc.env_to_dnsbll[to] = list; | |
1251 dc.env_to_chkfrom[to] = from; | |
1252 processed = true; | |
1253 } break; | |
1254 | |
1255 case include: { | |
1256 char *fn = next_token(delim); | |
3 | 1257 if (ok_to_include(dc, fn)) { |
1258 load_conf(dc, fn); | |
1259 processed = true; | |
1260 } | |
1261 } break; | |
1262 | |
1263 case includedcc: { | |
1264 char *name = next_token(delim); | |
1265 if (!name) break; | |
1266 char *fn = next_token(delim); | |
1267 if (ok_to_include(dc, fn)) { | |
1268 load_conf_dcc(dc, name, fn); | |
1269 processed = true; | |
0 | 1270 } |
1271 } break; | |
1272 | |
1273 default: { | |
1274 } break; | |
1275 } | |
1276 if (!processed) { | |
1277 pthread_mutex_lock(&syslog_mutex); | |
1278 openlog("dnsbl", LOG_PID, LOG_MAIL); | |
1279 syslog(LOG_ERR, "ignoring file %s line %d : %s\n", fn, curline, orig); | |
1280 closelog(); | |
1281 pthread_mutex_unlock(&syslog_mutex); | |
1282 } | |
1283 } | |
1284 } | |
1285 is.close(); | |
1286 } | |
1287 | |
1288 | |
1289 //////////////////////////////////////////////// | |
1290 // reload the config | |
1291 // | |
1292 static CONFIG* new_conf(); | |
1293 static CONFIG* new_conf() { | |
1294 CONFIG *newc = new CONFIG; | |
29
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1295 pthread_mutex_lock(&config_mutex); |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1296 newc->generation = generation++; |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1297 pthread_mutex_unlock(&config_mutex); |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1298 char buf[200]; |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1299 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
|
1300 my_syslog(buf); |
0 | 1301 load_conf(*newc, "dnsbl.conf"); |
1302 newc->load_time = time(NULL); | |
1303 return newc; | |
1304 } | |
1305 | |
1306 | |
1307 //////////////////////////////////////////////// | |
1308 // thread to watch the old config files for changes | |
1309 // and reload when needed. we also cleanup old | |
1310 // configs whose reference count has gone to zero. | |
1311 // | |
1312 static void* config_loader(void *arg); | |
1313 static void* config_loader(void *arg) { | |
1314 typedef set<CONFIG *> configp_set; | |
1315 configp_set old_configs; | |
18 | 1316 while (loader_run) { |
0 | 1317 sleep(180); // look for modifications every 3 minutes |
18 | 1318 if (!loader_run) break; |
0 | 1319 CONFIG &dc = *config; |
1320 time_t then = dc.load_time; | |
1321 struct stat st; | |
1322 bool reload = false; | |
1323 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) { | |
1324 char *fn = *i; | |
1325 if (stat(fn, &st)) reload = true; // file disappeared | |
1326 else if (st.st_mtime > then) reload = true; // file modified | |
1327 if (reload) break; | |
1328 } | |
1329 if (reload) { | |
1330 CONFIG *newc = new_conf(); | |
1331 // replace the global config pointer | |
1332 pthread_mutex_lock(&config_mutex); | |
1333 CONFIG *old = config; | |
1334 config = newc; | |
1335 pthread_mutex_unlock(&config_mutex); | |
1336 if (old) old_configs.insert(old); | |
1337 } | |
1338 // now look for old configs with zero ref counts | |
1339 for (configp_set::iterator i=old_configs.begin(); i!=old_configs.end(); ) { | |
1340 CONFIG *old = *i; | |
1341 if (!old->reference_count) { | |
29
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1342 char buf[200]; |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1343 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
|
1344 my_syslog(buf); |
0 | 1345 delete old; // destructor does all the work |
1346 old_configs.erase(i++); | |
1347 } | |
1348 else i++; | |
1349 } | |
1350 } | |
18 | 1351 return NULL; |
0 | 1352 } |
1353 | |
1354 | |
1355 static void usage(char *prog); | |
1356 static void usage(char *prog) | |
1357 { | |
16 | 1358 fprintf(stderr, "Usage: %s [-d] [-c] -p socket-addr [-t timeout]\n", prog); |
0 | 1359 fprintf(stderr, "where socket-addr is for the connection to sendmail and should be one of\n"); |
1360 fprintf(stderr, " inet:port@local-ip-address\n"); | |
1361 fprintf(stderr, " local:local-domain-socket-file-name\n"); | |
9 | 1362 fprintf(stderr, "-c will load and dump the config to stdout\n"); |
16 | 1363 fprintf(stderr, "-d will add some syslog debug messages\n"); |
0 | 1364 } |
1365 | |
1366 | |
42 | 1367 |
1368 static void setup_socket(char *sock); | |
1369 static void setup_socket(char *sock) { | |
1370 unlink(sock); | |
43 | 1371 // sockaddr_un addr; |
1372 // memset(&addr, '\0', sizeof addr); | |
1373 // addr.sun_family = AF_UNIX; | |
1374 // strncpy(addr.sun_path, sock, sizeof(addr.sun_path)-1); | |
1375 // int s = socket(AF_UNIX, SOCK_STREAM, 0); | |
1376 // bind(s, (sockaddr*)&addr, sizeof(addr)); | |
1377 // close(s); | |
42 | 1378 } |
1379 | |
1380 | |
0 | 1381 int main(int argc, char**argv) |
1382 { | |
3 | 1383 bool check = false; |
1384 bool setconn = false; | |
0 | 1385 int c; |
16 | 1386 const char *args = "p:t:hcd"; |
0 | 1387 extern char *optarg; |
1388 | |
1389 // Process command line options | |
1390 while ((c = getopt(argc, argv, args)) != -1) { | |
1391 switch (c) { | |
1392 case 'p': | |
1393 if (optarg == NULL || *optarg == '\0') { | |
1394 fprintf(stderr, "Illegal conn: %s\n", optarg); | |
1395 exit(EX_USAGE); | |
1396 } | |
1397 if (smfi_setconn(optarg) == MI_FAILURE) { | |
1398 fprintf(stderr, "smfi_setconn failed\n"); | |
1399 exit(EX_SOFTWARE); | |
1400 } | |
1401 | |
42 | 1402 if (strncasecmp(optarg, "unix:", 5) == 0) setup_socket(optarg + 5); |
1403 else if (strncasecmp(optarg, "local:", 6) == 0) setup_socket(optarg + 6); | |
3 | 1404 setconn = true; |
0 | 1405 break; |
1406 | |
1407 case 't': | |
1408 if (optarg == NULL || *optarg == '\0') { | |
1409 fprintf(stderr, "Illegal timeout: %s\n", optarg); | |
1410 exit(EX_USAGE); | |
1411 } | |
1412 if (smfi_settimeout(atoi(optarg)) == MI_FAILURE) { | |
1413 fprintf(stderr, "smfi_settimeout failed\n"); | |
1414 exit(EX_SOFTWARE); | |
1415 } | |
1416 break; | |
1417 | |
3 | 1418 case 'c': |
1419 check = true; | |
1420 break; | |
1421 | |
16 | 1422 case 'd': |
1423 debug_syslog = true; | |
1424 break; | |
1425 | |
0 | 1426 case 'h': |
1427 default: | |
1428 usage(argv[0]); | |
1429 exit(EX_USAGE); | |
1430 } | |
1431 } | |
5 | 1432 |
1433 if (check) { | |
1434 CONFIG &dc = *new_conf(); | |
1435 dumpit(dc); | |
1436 return 0; | |
1437 } | |
1438 | |
0 | 1439 if (!setconn) { |
1440 fprintf(stderr, "%s: Missing required -p argument\n", argv[0]); | |
1441 usage(argv[0]); | |
1442 exit(EX_USAGE); | |
1443 } | |
5 | 1444 |
0 | 1445 if (smfi_register(smfilter) == MI_FAILURE) { |
1446 fprintf(stderr, "smfi_register failed\n"); | |
1447 exit(EX_UNAVAILABLE); | |
1448 } | |
1449 | |
1450 // switch to background mode | |
1451 if (daemon(1,0) < 0) { | |
1452 fprintf(stderr, "daemon() call failed\n"); | |
1453 exit(EX_UNAVAILABLE); | |
1454 } | |
1455 | |
1456 // write the pid | |
1457 const char *pidpath = "/var/run/dnsbl.pid"; | |
1458 unlink(pidpath); | |
1459 FILE *f = fopen(pidpath, "w"); | |
1460 if (f) { | |
22 | 1461 #ifdef linux |
1462 // from a comment in the DCC source code: | |
1463 // Linux threads are broken. Signals given the | |
1464 // original process are delivered to only the | |
1465 // thread that happens to have that PID. The | |
1466 // sendmail libmilter thread that needs to hear | |
1467 // SIGINT and other signals does not, and that breaks | |
1468 // scripts that need to stop milters. | |
1469 // However, signaling the process group works. | |
0 | 1470 fprintf(f, "-%d\n", (u_int)getpgrp()); |
22 | 1471 #else |
1472 fprintf(f, "%d\n", (u_int)getpid()); | |
1473 #endif | |
0 | 1474 fclose(f); |
1475 } | |
1476 | |
42 | 1477 // drop root privs |
1478 struct passwd *pw = getpwnam("dnsbl"); | |
1479 if (pw) { | |
48 | 1480 if (setgid(pw->pw_gid) == -1) { |
1481 my_syslog("failed to switch to group dnsbl"); | |
1482 } | |
42 | 1483 if (setuid(pw->pw_uid) == -1) { |
1484 my_syslog("failed to switch to user dnsbl"); | |
1485 } | |
1486 } | |
1487 | |
48 | 1488 // initialize the thread sync objects |
1489 pthread_mutex_init(&config_mutex, 0); | |
1490 pthread_mutex_init(&syslog_mutex, 0); | |
1491 pthread_mutex_init(&resolve_mutex, 0); | |
1492 | |
1493 // load the initial config | |
1494 config = new_conf(); | |
1495 | |
1496 // only create threads after the fork() in daemon | |
1497 pthread_t tid; | |
1498 if (pthread_create(&tid, 0, config_loader, 0)) | |
1499 my_syslog("failed to create config loader thread"); | |
1500 if (pthread_detach(tid)) | |
1501 my_syslog("failed to detach config loader thread"); | |
1502 | |
18 | 1503 time_t starting = time(NULL); |
1504 int rc = smfi_main(); | |
22 | 1505 if ((rc != MI_SUCCESS) && (time(NULL) > starting+5*60)) { |
18 | 1506 my_syslog("trying to restart after smfi_main()"); |
1507 loader_run = false; // eventually the config loader thread will terminate | |
1508 execvp(argv[0], argv); | |
1509 } | |
1510 exit((rc == MI_SUCCESS) ? 0 : EX_UNAVAILABLE); | |
0 | 1511 } |
8 | 1512 |