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.
|
0
|
14
|
|
15 */
|
|
16
|
|
17
|
|
18 // from sendmail sample
|
|
19 #include <sys/types.h>
|
|
20 #include <sys/stat.h>
|
|
21 #include <errno.h>
|
|
22 #include <sysexits.h>
|
|
23 #include <unistd.h>
|
|
24
|
|
25 // needed for socket io
|
|
26 #include <sys/ioctl.h>
|
|
27 #include <net/if.h>
|
|
28 #include <arpa/inet.h>
|
|
29 #include <netinet/in.h>
|
|
30 #include <netinet/tcp.h>
|
|
31 #include <netdb.h>
|
|
32 #include <sys/socket.h>
|
|
33
|
|
34 // needed for thread
|
|
35 #include <pthread.h>
|
|
36
|
|
37 // needed for std c++ collections
|
|
38 #include <set>
|
|
39 #include <map>
|
|
40 #include <list>
|
|
41
|
|
42 // for the dns resolver
|
|
43 #include <netinet/in.h>
|
|
44 #include <arpa/nameser.h>
|
|
45 #include <resolv.h>
|
|
46
|
|
47 // misc stuff needed here
|
|
48 #include <ctype.h>
|
|
49 #include <fstream>
|
|
50 #include <syslog.h>
|
|
51
|
8
|
52 static char* dnsbl_version="$Id$";
|
0
|
53
|
8
|
54 #define DEFAULT "default"
|
|
55 #define WHITE "white"
|
|
56 #define BLACK "black"
|
|
57 #define OK "ok"
|
|
58 #define MANY "many"
|
|
59
|
|
60 enum status {oksofar, // not rejected yet
|
|
61 white, // whitelisted by envelope from
|
|
62 black, // blacklisted by envelope from or to
|
|
63 reject}; // rejected by a dns list
|
1
|
64
|
0
|
65 using namespace std;
|
|
66
|
|
67 extern "C" {
|
|
68 #include "libmilter/mfapi.h"
|
|
69 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr);
|
|
70 sfsistat mlfi_envfrom(SMFICTX *ctx, char **argv);
|
|
71 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **argv);
|
8
|
72 sfsistat mlfi_body(SMFICTX *ctx, u_char *data, size_t len);
|
|
73 sfsistat mlfi_eom(SMFICTX *ctx);
|
|
74 sfsistat mlfi_abort(SMFICTX *ctx);
|
0
|
75 sfsistat mlfi_close(SMFICTX *ctx);
|
|
76 }
|
|
77
|
|
78 struct ltstr {
|
|
79 bool operator()(char* s1, char* s2) const {
|
|
80 return strcmp(s1, s2) < 0;
|
|
81 }
|
|
82 };
|
|
83
|
|
84 struct DNSBL {
|
|
85 char *suffix; // blacklist suffix like blackholes.five-ten-sg.com
|
|
86 char *message; // error message with one or two %s operators for the ip address replacement
|
|
87 DNSBL(char *s, char *m);
|
|
88 };
|
|
89 DNSBL::DNSBL(char *s, char *m) {
|
|
90 suffix = s;
|
|
91 message = m;
|
|
92 }
|
|
93
|
|
94 typedef DNSBL * DNSBLP;
|
|
95 typedef list<DNSBLP> DNSBLL;
|
|
96 typedef DNSBLL * DNSBLLP;
|
|
97 typedef map<char *, char *, ltstr> string_map;
|
|
98 typedef map<char *, string_map *, ltstr> from_map;
|
|
99 typedef map<char *, DNSBLP, ltstr> dnsblp_map;
|
|
100 typedef map<char *, DNSBLLP, ltstr> dnsbllp_map;
|
|
101 typedef set<char *, ltstr> string_set;
|
|
102 typedef list<char *> string_list;
|
|
103
|
|
104 struct CONFIG {
|
|
105 // the only mutable stuff once it has been loaded from the config file
|
|
106 int reference_count; // protected by the global config_mutex
|
|
107 // all the rest is constant after loading from the config file
|
|
108 time_t load_time;
|
|
109 string_list config_files;
|
|
110 dnsblp_map dnsbls;
|
|
111 dnsbllp_map dnsblls;
|
|
112 from_map env_from;
|
|
113 string_map env_to_dnsbll; // map recipient to a named dnsbll
|
|
114 string_map env_to_chkfrom; // map recipient to a named from map
|
8
|
115 char * content_suffix; // for sbl url body filtering
|
9
|
116 char * content_message; // ""
|
0
|
117 CONFIG();
|
|
118 ~CONFIG();
|
|
119 };
|
|
120 CONFIG::CONFIG() {
|
|
121 reference_count = 0;
|
|
122 load_time = 0;
|
8
|
123 content_suffix = NULL;
|
|
124 content_message = NULL;
|
0
|
125 }
|
|
126 CONFIG::~CONFIG() {
|
|
127 for (dnsblp_map::iterator i=dnsbls.begin(); i!=dnsbls.end(); i++) {
|
|
128 DNSBLP d = (*i).second;
|
|
129 delete d;
|
|
130 }
|
|
131 for (dnsbllp_map::iterator i=dnsblls.begin(); i!=dnsblls.end(); i++) {
|
|
132 DNSBLLP d = (*i).second;
|
|
133 delete d;
|
|
134 }
|
|
135 for (from_map::iterator i=env_from.begin(); i!=env_from.end(); i++) {
|
|
136 string_map *d = (*i).second;
|
|
137 delete d;
|
|
138 }
|
|
139 }
|
|
140
|
|
141 static string_set all_strings; // owns all the strings, only modified by the config loader thread
|
|
142 static CONFIG * config = NULL; // protected by the config_mutex
|
|
143
|
|
144 static pthread_mutex_t config_mutex;
|
|
145 static pthread_mutex_t syslog_mutex;
|
|
146 static pthread_mutex_t resolve_mutex;
|
|
147
|
|
148
|
|
149
|
8
|
150 // include the content scanner
|
|
151 #include "scanner.cpp"
|
|
152
|
|
153
|
0
|
154 ////////////////////////////////////////////////
|
8
|
155 // helper to discard the strings held by a string_set
|
0
|
156 //
|
9
|
157 static void discard(string_set &s);
|
|
158 static void discard(string_set &s) {
|
8
|
159 for (string_set::iterator i=s.begin(); i!=s.end(); i++) {
|
|
160 free(*i);
|
|
161 }
|
9
|
162 s.clear();
|
8
|
163 }
|
0
|
164
|
|
165
|
|
166 ////////////////////////////////////////////////
|
|
167 // mail filter private data, held for us by sendmail
|
|
168 //
|
|
169 struct mlfiPriv
|
|
170 {
|
8
|
171 // connection specific data
|
|
172 CONFIG *pc; // global context with our maps
|
|
173 int ip; // ip4 address of the smtp client
|
|
174 map<DNSBLP, status> checked; // status from those lists
|
|
175 // message specific data
|
0
|
176 char *mailaddr; // envelope from value
|
|
177 bool authenticated; // client authenticated? if so, suppress all dnsbl checks
|
8
|
178 bool have_whites; // have at least one whitelisted recipient? need to accept content and remove all non-whitelisted recipients if it fails
|
|
179 bool only_whites; // every recipient is whitelisted?
|
|
180 url_scanner *scanner; // object to handle body scanning
|
|
181 string_set non_whites; // remember the non-whitelisted recipients so we can remove them if need be
|
|
182 string_set urls; // remember the urls that we have checked
|
0
|
183 mlfiPriv();
|
|
184 ~mlfiPriv();
|
8
|
185 void reset(bool final = false); // for a new message
|
0
|
186 };
|
|
187 mlfiPriv::mlfiPriv() {
|
|
188 pthread_mutex_lock(&config_mutex);
|
|
189 pc = config;
|
|
190 pc->reference_count++;
|
|
191 pthread_mutex_unlock(&config_mutex);
|
8
|
192 ip = 0;
|
|
193 mailaddr = NULL;
|
|
194 authenticated = false;
|
|
195 have_whites = false;
|
|
196 only_whites = true;
|
|
197 scanner = new url_scanner(&urls);
|
0
|
198 }
|
|
199 mlfiPriv::~mlfiPriv() {
|
|
200 pthread_mutex_lock(&config_mutex);
|
|
201 pc->reference_count--;
|
|
202 pthread_mutex_unlock(&config_mutex);
|
8
|
203 reset(true);
|
|
204 }
|
|
205 void mlfiPriv::reset(bool final) {
|
0
|
206 if (mailaddr) free(mailaddr);
|
8
|
207 delete scanner;
|
|
208 discard(non_whites);
|
|
209 discard(urls);
|
|
210 if (!final) {
|
|
211 mailaddr = NULL;
|
|
212 authenticated = false;
|
|
213 have_whites = false;
|
|
214 only_whites = true;
|
|
215 scanner = new url_scanner(&urls);
|
|
216 }
|
0
|
217 }
|
|
218
|
|
219 #define MLFIPRIV ((struct mlfiPriv *) smfi_getpriv(ctx))
|
|
220
|
|
221
|
|
222 ////////////////////////////////////////////////
|
|
223 // syslog a message
|
|
224 //
|
|
225 static void my_syslog(char *text);
|
|
226 static void my_syslog(char *text) {
|
|
227 pthread_mutex_lock(&syslog_mutex);
|
|
228 openlog("dnsbl", LOG_PID, LOG_MAIL);
|
|
229 syslog(LOG_NOTICE, "%s", text);
|
|
230 closelog();
|
|
231 pthread_mutex_unlock(&syslog_mutex);
|
|
232 }
|
|
233
|
|
234
|
|
235 ////////////////////////////////////////////////
|
|
236 // register a global string
|
|
237 //
|
|
238 static char* register_string(char *name);
|
|
239 static char* register_string(char *name) {
|
|
240 string_set::iterator i = all_strings.find(name);
|
|
241 if (i != all_strings.end()) return *i;
|
|
242 char *x = strdup(name);
|
|
243 all_strings.insert(x);
|
|
244 return x;
|
|
245 }
|
|
246
|
|
247
|
|
248 static char* next_token(char *delim);
|
|
249 static char* next_token(char *delim) {
|
|
250 char *name = strtok(NULL, delim);
|
|
251 if (!name) return name;
|
|
252 return register_string(name);
|
|
253 }
|
|
254
|
|
255
|
|
256 ////////////////////////////////////////////////
|
|
257 // lookup an email address in the env_from or env_to maps
|
|
258 //
|
|
259 static char* lookup1(char *email, string_map map);
|
|
260 static char* lookup1(char *email, string_map map) {
|
|
261 string_map::iterator i = map.find(email);
|
|
262 if (i != map.end()) return (*i).second;
|
|
263 char *x = strchr(email, '@');
|
|
264 if (!x) return DEFAULT;
|
|
265 x++;
|
|
266 i = map.find(x);
|
|
267 if (i != map.end()) return (*i).second;
|
|
268 return DEFAULT;
|
|
269 }
|
|
270
|
|
271
|
|
272 ////////////////////////////////////////////////
|
|
273 // lookup an email address in the env_from or env_to maps
|
|
274 // this email address is passed in from sendmail, and will
|
|
275 // always be enclosed in <>. It may have mixed case, just
|
|
276 // as the mail client sent it.
|
|
277 //
|
|
278 static char* lookup(char* email, string_map map);
|
|
279 static char* lookup(char* email, string_map map) {
|
|
280 int n = strlen(email)-2;
|
|
281 if (n < 1) return DEFAULT; // malformed
|
|
282 char *key = strdup(email+1);
|
|
283 key[n] = '\0';
|
|
284 for (int i=0; i<n; i++) key[i] = tolower(key[i]);
|
|
285 char *rc = lookup1(key, map);
|
|
286 free(key);
|
|
287 return rc;
|
|
288 }
|
|
289
|
|
290
|
|
291 ////////////////////////////////////////////////
|
|
292 // find the dnsbl with a specific name
|
|
293 //
|
|
294 static DNSBLP find_dnsbl(CONFIG &dc, char *name);
|
|
295 static DNSBLP find_dnsbl(CONFIG &dc, char *name) {
|
|
296 dnsblp_map::iterator i = dc.dnsbls.find(name);
|
|
297 if (i == dc.dnsbls.end()) return NULL;
|
|
298 return (*i).second;
|
|
299 }
|
|
300
|
|
301
|
|
302 ////////////////////////////////////////////////
|
|
303 // find the dnsbll with a specific name
|
|
304 //
|
|
305 static DNSBLLP find_dnsbll(CONFIG &dc, char *name);
|
|
306 static DNSBLLP find_dnsbll(CONFIG &dc, char *name) {
|
|
307 dnsbllp_map::iterator i = dc.dnsblls.find(name);
|
|
308 if (i == dc.dnsblls.end()) return NULL;
|
|
309 return (*i).second;
|
|
310 }
|
|
311
|
|
312
|
|
313 ////////////////////////////////////////////////
|
|
314 // find the envfrom map with a specific name
|
|
315 //
|
|
316 static string_map* find_from_map(CONFIG &dc, char *name);
|
|
317 static string_map* find_from_map(CONFIG &dc, char *name) {
|
|
318 from_map::iterator i = dc.env_from.find(name);
|
|
319 if (i == dc.env_from.end()) return NULL;
|
|
320 return (*i).second;
|
|
321 }
|
|
322
|
|
323
|
|
324 static string_map& really_find_from_map(CONFIG &dc, char *name);
|
|
325 static string_map& really_find_from_map(CONFIG &dc, char *name) {
|
|
326 string_map *sm = find_from_map(dc, name);
|
|
327 if (!sm) {
|
|
328 sm = new string_map;
|
|
329 dc.env_from[name] = sm;
|
|
330 }
|
|
331 return *sm;
|
|
332 }
|
|
333
|
|
334
|
|
335 ////////////////////////////////////////////////
|
8
|
336 //
|
|
337 // ask a dns question and get an A record answer - we don't try
|
|
338 // very hard, just using the default resolver retry settings.
|
|
339 // If we cannot get an answer, we just accept the mail. The
|
|
340 // caller must ensure thread safety.
|
|
341 //
|
0
|
342 //
|
8
|
343 static int dns_interface(char *question);
|
|
344 static int dns_interface(char *question) {
|
|
345 u_char answer[NS_PACKETSZ];
|
|
346 int length = res_search(question, ns_c_in, ns_t_a, answer, sizeof(answer));
|
|
347 if (length < 0) return 0; // error in getting answer
|
|
348 // parse the answer
|
|
349 ns_msg handle;
|
|
350 ns_rr rr;
|
|
351 if (ns_initparse(answer, length, &handle) != 0) return 0;
|
|
352 int rrnum = 0;
|
|
353 while (ns_parserr(&handle, ns_s_an, rrnum++, &rr) == 0) {
|
|
354 if (ns_rr_type(rr) == ns_t_a) {
|
|
355 int address;
|
|
356 memcpy(&address, ns_rr_rdata(rr), sizeof(address));
|
|
357 return address;
|
|
358 }
|
|
359 }
|
|
360 return 0;
|
|
361 }
|
|
362
|
|
363 static int protected_dns_interface(char *question);
|
|
364 static int protected_dns_interface(char *question) {
|
|
365 int ans;
|
|
366 pthread_mutex_lock(&resolve_mutex);
|
|
367 ans = dns_interface(question);
|
|
368 pthread_mutex_unlock(&resolve_mutex);
|
|
369 return ans;
|
|
370
|
|
371 }
|
|
372
|
|
373 ////////////////////////////////////////////////
|
|
374 // check a single dnsbl
|
|
375 //
|
|
376 static status check_single(int ip, char *suffix);
|
|
377 static status check_single(int ip, char *suffix) {
|
0
|
378 // make a dns question
|
|
379 const u_char *src = (const u_char *)&ip;
|
|
380 if (src[0] == 127) return oksofar; // don't do dns lookups on localhost
|
|
381 char question[NS_MAXDNAME];
|
8
|
382 snprintf(question, sizeof(question), "%u.%u.%u.%u.%s.", src[3], src[2], src[1], src[0], suffix);
|
|
383 // ask the question, if we get an A record it implies a blacklisted ip address
|
|
384 return (protected_dns_interface(question)) ? reject : oksofar;
|
|
385 }
|
|
386
|
|
387
|
|
388 ////////////////////////////////////////////////
|
|
389 // check a single dnsbl
|
|
390 //
|
|
391 static status check_single(int ip, DNSBL &bl);
|
|
392 static status check_single(int ip, DNSBL &bl) {
|
|
393 return check_single(ip, bl.suffix);
|
0
|
394 }
|
|
395
|
|
396
|
|
397 ////////////////////////////////////////////////
|
|
398 // check the dnsbls specified for this recipient
|
|
399 //
|
|
400 static status check_dnsbl(mlfiPriv &priv, DNSBLLP dnsbllp, DNSBLP &rejectlist);
|
|
401 static status check_dnsbl(mlfiPriv &priv, DNSBLLP dnsbllp, DNSBLP &rejectlist) {
|
|
402 if (priv.authenticated) return oksofar;
|
|
403 if (!dnsbllp) return oksofar;
|
|
404 DNSBLL &dnsbll = *dnsbllp;
|
|
405 for (DNSBLL::iterator i=dnsbll.begin(); i!=dnsbll.end(); i++) {
|
|
406 DNSBLP dp = *i; // non null by construction
|
|
407 status st;
|
|
408 map<DNSBLP, status>::iterator f = priv.checked.find(dp);
|
|
409 if (f == priv.checked.end()) {
|
|
410 // have not checked this list yet
|
8
|
411 st = check_single(priv.ip, *dp);
|
0
|
412 rejectlist = dp;
|
|
413 priv.checked[dp] = st;
|
|
414 }
|
|
415 else {
|
|
416 st = (*f).second;
|
|
417 rejectlist = (*f).first;
|
|
418 }
|
|
419 if (st == reject) return st;
|
|
420 }
|
|
421 return oksofar;
|
|
422 }
|
|
423
|
|
424
|
|
425 ////////////////////////////////////////////////
|
8
|
426 // check the dnsbls specified for this recipient
|
|
427 //
|
|
428 static status check_urls(mlfiPriv &priv, char *&url, int &ip);
|
|
429 static status check_urls(mlfiPriv &priv, char *&url, int &ip) {
|
|
430 CONFIG &dc = *priv.pc;
|
|
431 if (!dc.content_suffix) return oksofar;
|
|
432 int count = 0;
|
|
433 for (string_set::iterator i=priv.urls.begin(); i!=priv.urls.end(); i++) {
|
|
434 count++;
|
9
|
435 if (count > 20) return oksofar; // silly to check too many urls
|
8
|
436 url = *i;
|
|
437 char buf[200];
|
|
438 snprintf(buf, sizeof(buf), "looking for url %s", url);
|
|
439 my_syslog(buf);
|
|
440 ip = protected_dns_interface(url);
|
|
441 if (ip) {
|
|
442 status st = check_single(ip, dc.content_suffix);
|
|
443 if (st == reject) return st;
|
|
444 }
|
|
445 }
|
9
|
446 return oksofar;
|
8
|
447 }
|
|
448
|
|
449
|
|
450 ////////////////////////////////////////////////
|
0
|
451 // start of sendmail milter interfaces
|
|
452 //
|
|
453 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr)
|
|
454 {
|
|
455 // allocate some private memory
|
|
456 mlfiPriv *priv = new mlfiPriv;
|
|
457 if (hostaddr->sa_family == AF_INET) {
|
|
458 priv->ip = ((struct sockaddr_in *)hostaddr)->sin_addr.s_addr;
|
|
459 }
|
|
460
|
|
461 // save the private data
|
|
462 smfi_setpriv(ctx, (void*)priv);
|
|
463
|
|
464 // continue processing
|
|
465 return SMFIS_CONTINUE;
|
|
466 }
|
|
467
|
|
468 sfsistat mlfi_envfrom(SMFICTX *ctx, char **from)
|
|
469 {
|
|
470 mlfiPriv &priv = *MLFIPRIV;
|
|
471 priv.mailaddr = strdup(from[0]);
|
|
472 priv.authenticated = (smfi_getsymval(ctx, "{auth_authen}") != NULL);
|
|
473 return SMFIS_CONTINUE;
|
|
474 }
|
|
475
|
|
476 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **rcpt)
|
|
477 {
|
|
478 DNSBLP rejectlist = NULL; // list that caused the reject
|
|
479 status st = oksofar;
|
|
480 mlfiPriv &priv = *MLFIPRIV;
|
|
481 CONFIG &dc = *priv.pc;
|
|
482 char *rcptaddr = rcpt[0];
|
|
483 char *dnsname = lookup(rcptaddr, dc.env_to_dnsbll);
|
|
484 char *fromname = lookup(rcptaddr, dc.env_to_chkfrom);
|
|
485 if ((strcmp(dnsname, BLACK) == 0) ||
|
|
486 (strcmp(fromname, BLACK) == 0)) {
|
|
487 st = black; // two options to blacklist this recipient
|
|
488 }
|
|
489 else if (strcmp(fromname, WHITE) == 0) {
|
|
490 st = white;
|
|
491 }
|
|
492 else {
|
|
493 // check an env_from map
|
|
494 string_map *sm = find_from_map(dc, fromname);
|
|
495 if (sm != NULL) {
|
|
496 fromname = lookup(priv.mailaddr, *sm); // returns default if name not in map
|
|
497 if (strcmp(fromname, BLACK) == 0) {
|
|
498 st = black; // blacklist this envelope from value
|
|
499 }
|
|
500 if (strcmp(fromname, WHITE) == 0) {
|
|
501 st = white; // blacklist this envelope from value
|
|
502 }
|
|
503 }
|
|
504 }
|
|
505 if ((st == oksofar) && (strcmp(dnsname, WHITE) != 0)) {
|
|
506 // check dns lists
|
|
507 st = check_dnsbl(priv, find_dnsbll(dc, dnsname), rejectlist);
|
|
508 }
|
|
509
|
|
510 if (st == reject) {
|
|
511 // reject the recipient based on some dnsbl
|
|
512 char adr[sizeof "255.255.255.255"];
|
|
513 adr[0] = '\0';
|
8
|
514 inet_ntop(AF_INET, (const u_char *)&priv.ip, adr, sizeof(adr));
|
0
|
515 char buf[2000];
|
|
516 snprintf(buf, sizeof(buf), rejectlist->message, adr, adr);
|
|
517 smfi_setreply(ctx, "550", "5.7.1", buf);
|
|
518 return SMFIS_REJECT;
|
|
519 }
|
|
520 else if (st == black) {
|
|
521 // reject the recipient based on blacklisting either from or to
|
|
522 smfi_setreply(ctx, "550", "5.7.1", "no such user");
|
|
523 return SMFIS_REJECT;
|
|
524 }
|
|
525 else {
|
|
526 // accept the recipient
|
8
|
527 if (st == oksofar) {
|
|
528 // but remember the non-whites
|
|
529 priv.non_whites.insert(strdup(rcptaddr));
|
|
530 priv.only_whites = false;
|
|
531 }
|
|
532 if (st == white) {
|
|
533 priv.have_whites = true;
|
|
534 }
|
0
|
535 return SMFIS_CONTINUE;
|
|
536 }
|
|
537 }
|
|
538
|
8
|
539 sfsistat mlfi_body(SMFICTX *ctx, u_char *data, size_t len)
|
0
|
540 {
|
|
541 mlfiPriv &priv = *MLFIPRIV;
|
8
|
542 if (priv.authenticated) return SMFIS_CONTINUE;
|
|
543 if (priv.only_whites) return SMFIS_CONTINUE;
|
|
544 priv.scanner->scan(data, len);
|
11
|
545 return SMFIS_CONTINUE;
|
8
|
546 }
|
|
547
|
|
548 sfsistat mlfi_eom(SMFICTX *ctx)
|
|
549 {
|
|
550 sfsistat rc;
|
|
551 mlfiPriv &priv = *MLFIPRIV;
|
|
552 char *url = NULL;
|
|
553 int ip;
|
|
554 // process end of message
|
|
555 if (priv.authenticated ||
|
|
556 priv.only_whites ||
|
|
557 (check_urls(priv, url, ip) == oksofar)) rc = SMFIS_CONTINUE;
|
|
558 else {
|
|
559 if (!priv.have_whites) {
|
|
560 // can reject the entire message
|
|
561 char adr[sizeof "255.255.255.255"];
|
|
562 adr[0] = '\0';
|
|
563 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr));
|
|
564 char buf[2000];
|
|
565 snprintf(buf, sizeof(buf), priv.pc->content_message, url, adr);
|
|
566 smfi_setreply(ctx, "550", "5.7.1", buf);
|
|
567 rc = SMFIS_REJECT;
|
|
568 }
|
|
569 else {
|
|
570 // need to accept it but remove the recipients that don't want it
|
|
571 for (string_set::iterator i=priv.non_whites.begin(); i!=priv.non_whites.end(); i++) {
|
|
572 char *rcpt = *i;
|
|
573 smfi_delrcpt(ctx, rcpt);
|
|
574 }
|
|
575 rc = SMFIS_CONTINUE;
|
|
576 }
|
0
|
577 }
|
8
|
578 // reset for a new message on the same connection
|
|
579 mlfi_abort(ctx);
|
|
580 return rc;
|
|
581 }
|
|
582
|
|
583 sfsistat mlfi_abort(SMFICTX *ctx)
|
|
584 {
|
|
585 mlfiPriv &priv = *MLFIPRIV;
|
|
586 priv.reset();
|
0
|
587 return SMFIS_CONTINUE;
|
|
588 }
|
|
589
|
|
590 sfsistat mlfi_close(SMFICTX *ctx)
|
|
591 {
|
|
592 mlfiPriv *priv = MLFIPRIV;
|
|
593 if (!priv) return SMFIS_CONTINUE;
|
|
594 delete priv;
|
|
595 smfi_setpriv(ctx, NULL);
|
|
596 return SMFIS_CONTINUE;
|
|
597 }
|
|
598
|
|
599 struct smfiDesc smfilter =
|
|
600 {
|
|
601 "DNSBL", // filter name
|
|
602 SMFI_VERSION, // version code -- do not change
|
|
603 SMFIF_DELRCPT, // flags
|
|
604 mlfi_connect, // connection info filter
|
|
605 NULL, // SMTP HELO command filter
|
|
606 mlfi_envfrom, // envelope sender filter
|
|
607 mlfi_envrcpt, // envelope recipient filter
|
|
608 NULL, // header filter
|
|
609 NULL, // end of header
|
8
|
610 mlfi_body, // body block filter
|
|
611 mlfi_eom, // end of message
|
|
612 mlfi_abort, // message aborted
|
0
|
613 mlfi_close, // connection cleanup
|
|
614 };
|
|
615
|
|
616
|
|
617 static void dumpit(char *name, string_map map);
|
|
618 static void dumpit(char *name, string_map map) {
|
9
|
619 fprintf(stdout, "\n");
|
0
|
620 for (string_map::iterator i=map.begin(); i!=map.end(); i++) {
|
9
|
621 fprintf(stdout, "%s %s->%s\n", name, (*i).first, (*i).second);
|
0
|
622 }
|
|
623 }
|
|
624
|
|
625
|
|
626 static void dumpit(from_map map);
|
|
627 static void dumpit(from_map map) {
|
|
628 for (from_map::iterator i=map.begin(); i!=map.end(); i++) {
|
3
|
629 char buf[2000];
|
|
630 snprintf(buf, sizeof(buf), "envelope from map for %s", (*i).first);
|
0
|
631 string_map *sm = (*i).second;
|
3
|
632 dumpit(buf, *sm);
|
0
|
633 }
|
|
634 }
|
|
635
|
|
636
|
3
|
637 static void dumpit(CONFIG &dc);
|
|
638 static void dumpit(CONFIG &dc) {
|
5
|
639 dumpit(dc.env_from);
|
|
640 dumpit("envelope to (dnsbl list)", dc.env_to_dnsbll);
|
|
641 dumpit("envelope to (from map)", dc.env_to_chkfrom);
|
9
|
642 fprintf(stdout, "\ndnsbls\n");
|
0
|
643 for (dnsblp_map::iterator i=dc.dnsbls.begin(); i!=dc.dnsbls.end(); i++) {
|
9
|
644 fprintf(stdout, "%s %s %s\n", (*i).first, (*i).second->suffix, (*i).second->message);
|
0
|
645 }
|
9
|
646 fprintf(stdout, "\ndnsbl_lists\n");
|
0
|
647 for (dnsbllp_map::iterator i=dc.dnsblls.begin(); i!=dc.dnsblls.end(); i++) {
|
|
648 char *name = (*i).first;
|
|
649 DNSBLL &dl = *((*i).second);
|
9
|
650 fprintf(stdout, "%s", name);
|
0
|
651 for (DNSBLL::iterator j=dl.begin(); j!=dl.end(); j++) {
|
|
652 DNSBL &d = **j;
|
9
|
653 fprintf(stdout, " %s", d.suffix);
|
0
|
654 }
|
9
|
655 fprintf(stdout, "\n");
|
0
|
656 }
|
9
|
657 if (dc.content_suffix) {
|
|
658 fprintf(stdout, "\ncontent filtering enabled with %s %s\n", dc.content_suffix, dc.content_message);
|
|
659 }
|
|
660 fprintf(stdout, "\nfiles\n");
|
3
|
661 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) {
|
|
662 char *f = *i;
|
9
|
663 fprintf(stdout, "config includes %s\n", f);
|
3
|
664 }
|
|
665 }
|
|
666
|
|
667
|
|
668 ////////////////////////////////////////////////
|
|
669 // check for redundant or recursive include files
|
|
670 //
|
|
671 static bool ok_to_include(CONFIG &dc, char *fn);
|
|
672 static bool ok_to_include(CONFIG &dc, char *fn) {
|
|
673 if (!fn) return false;
|
|
674 bool ok = true;
|
|
675 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) {
|
|
676 char *f = *i;
|
|
677 if (strcmp(f, fn) == 0) {
|
|
678 my_syslog("redundant or recursive include file detected");
|
|
679 ok = false;
|
|
680 break;
|
|
681 }
|
|
682 }
|
|
683 return ok;
|
0
|
684 }
|
|
685
|
|
686
|
|
687 ////////////////////////////////////////////////
|
|
688 // load a single config file
|
|
689 //
|
3
|
690 static void load_conf_dcc(CONFIG &dc, char *name, char *fn);
|
|
691 static void load_conf_dcc(CONFIG &dc, char *name, char *fn) {
|
|
692 dc.config_files.push_back(fn);
|
|
693 char *list = BLACK;
|
|
694 const int LINE_SIZE = 2000;
|
|
695 ifstream is(fn);
|
|
696 if (is.fail()) return;
|
|
697 char line[LINE_SIZE];
|
|
698 char *delim = " \t";
|
|
699 int curline = 0;
|
|
700 while (!is.eof()) {
|
|
701 is.getline(line, LINE_SIZE);
|
|
702 curline++;
|
|
703 int n = strlen(line);
|
|
704 if (!n) continue;
|
|
705 for (int i=0; i<n; i++) line[i] = tolower(line[i]);
|
|
706 if (line[0] == '#') continue;
|
|
707 char *head = line;
|
|
708 if (strspn(line, delim) == 0) {
|
|
709 // have a leading ok/many tag to fetch
|
|
710 char *cmd = strtok(line, delim);
|
|
711 if (strcmp(cmd, MANY) == 0) list = BLACK;
|
|
712 else if (strcmp(cmd, OK) == 0) list = WHITE;
|
|
713 head = cmd + strlen(cmd) + 1;
|
|
714 }
|
|
715 char *cmd = strtok(head, delim);
|
|
716 if (!cmd) continue;
|
|
717 if (strcmp(cmd, "env_from") == 0) {
|
|
718 char *from = next_token(delim);
|
|
719 if (from) {
|
|
720 string_map &fm = really_find_from_map(dc, name);
|
|
721 fm[from] = list;
|
|
722 }
|
|
723 }
|
|
724 else if (strcmp(cmd, "env_to") == 0) {
|
|
725 char *to = next_token(delim);
|
|
726 if (to) {
|
|
727 dc.env_to_dnsbll[to] = list;
|
|
728 dc.env_to_chkfrom[to] = list;
|
|
729 }
|
|
730 }
|
|
731 else if (strcmp(cmd, "substitute") == 0) {
|
|
732 char *tag = next_token(delim);
|
|
733 if (tag && (strcmp(tag, "mail_host") == 0)) {
|
|
734 char *from = next_token(delim);
|
|
735 if (from) {
|
|
736 string_map &fm = really_find_from_map(dc, name);
|
|
737 fm[from] = list;
|
|
738 }
|
|
739 }
|
|
740 }
|
|
741 else if (strcmp(cmd, "include") == 0) {
|
|
742 char *fn = next_token(delim);
|
|
743 if (ok_to_include(dc, fn)) {
|
|
744 load_conf_dcc(dc, name, fn);
|
|
745 }
|
|
746 }
|
|
747
|
|
748 }
|
|
749 is.close();
|
|
750 }
|
|
751
|
|
752
|
0
|
753 static void load_conf(CONFIG &dc, char *fn);
|
|
754 static void load_conf(CONFIG &dc, char *fn) {
|
|
755 dc.config_files.push_back(fn);
|
|
756 map<char*, int, ltstr> commands;
|
8
|
757 enum {dummy, content, dnsbl, dnsbll, envfrom, envto, include, includedcc};
|
|
758 commands["content" ] = content;
|
3
|
759 commands["dnsbl" ] = dnsbl;
|
|
760 commands["dnsbl_list" ] = dnsbll;
|
|
761 commands["env_from" ] = envfrom;
|
|
762 commands["env_to" ] = envto;
|
|
763 commands["include" ] = include;
|
|
764 commands["include_dcc"] = includedcc;
|
0
|
765 const int LINE_SIZE = 2000;
|
|
766 ifstream is(fn);
|
|
767 if (is.fail()) return;
|
|
768 char line[LINE_SIZE];
|
|
769 char orig[LINE_SIZE];
|
|
770 char *delim = " \t";
|
|
771 int curline = 0;
|
|
772 while (!is.eof()) {
|
|
773 is.getline(line, LINE_SIZE);
|
|
774 snprintf(orig, sizeof(orig), "%s", line);
|
|
775 curline++;
|
|
776 int n = strlen(line);
|
|
777 for (int i=0; i<n; i++) line[i] = tolower(line[i]);
|
|
778 char *cmd = strtok(line, delim);
|
|
779 if (cmd && (cmd[0] != '#') && (cmd[0] != '\0')) {
|
|
780 // have a decent command
|
|
781 bool processed = false;
|
|
782 switch (commands[cmd]) {
|
8
|
783 case content: {
|
|
784 char *suff = strtok(NULL, delim);
|
|
785 if (!suff) break; // no dns suffic
|
|
786 char *msg = suff + strlen(suff);
|
|
787 if ((msg - line) >= strlen(orig)) break; // line ended with the dns suffix
|
|
788 msg = strchr(msg+1, '\'');
|
|
789 if (!msg) break; // no reply message template
|
|
790 msg++; // move over the leading '
|
|
791 if ((msg - line) >= strlen(orig)) break; // line ended with the leading quote
|
|
792 char *last = strchr(msg, '\'');
|
|
793 if (!last) break; // no trailing quote
|
|
794 *last = '\0'; // make it a null terminator
|
|
795 dc.content_suffix = register_string(suff);
|
|
796 dc.content_message = register_string(msg);
|
|
797 processed = true;
|
|
798 } break;
|
|
799
|
0
|
800 case dnsbl: {
|
|
801 // have a new dnsbl to use
|
|
802 char *name = next_token(delim);
|
|
803 if (!name) break; // no name name
|
|
804 if (find_dnsbl(dc, name)) break; // duplicate entry
|
|
805 char *suff = strtok(NULL, delim);
|
|
806 if (!suff) break; // no dns suffic
|
|
807 char *msg = suff + strlen(suff);
|
|
808 if ((msg - line) >= strlen(orig)) break; // line ended with the dns suffix
|
|
809 msg = strchr(msg+1, '\'');
|
|
810 if (!msg) break; // no reply message template
|
|
811 msg++; // move over the leading '
|
|
812 if ((msg - line) >= strlen(orig)) break; // line ended with the leading quote
|
|
813 char *last = strchr(msg, '\'');
|
|
814 if (!last) break; // no trailing quote
|
|
815 *last = '\0'; // make it a null terminator
|
|
816 dc.dnsbls[name] = new DNSBL(register_string(suff), register_string(msg));
|
|
817 processed = true;
|
|
818 } break;
|
|
819
|
|
820 case dnsbll: {
|
|
821 // define a new combination of dnsbls
|
|
822 char *name = next_token(delim);
|
|
823 if (!name) break;
|
|
824 if (find_dnsbll(dc, name)) break; // duplicate entry
|
|
825 char *list = next_token(delim);
|
|
826 if (!list || (*list == '\0') || (*list == '#')) break;
|
|
827 DNSBLLP d = new DNSBLL;
|
|
828 DNSBLP p = find_dnsbl(dc, list);
|
|
829 if (p) d->push_back(p);
|
|
830 while (true) {
|
|
831 list = next_token(delim);
|
|
832 if (!list || (*list == '\0') || (*list == '#')) break;
|
|
833 DNSBLP p = find_dnsbl(dc, list);
|
|
834 if (p) d->push_back(p);
|
|
835 }
|
|
836 dc.dnsblls[name] = d;
|
|
837 processed = true;
|
|
838 } break;
|
|
839
|
|
840 case envfrom: {
|
|
841 // add an entry into the named string_map
|
|
842 char *name = next_token(delim);
|
|
843 if (!name) break;
|
|
844 char *from = next_token(delim);
|
|
845 if (!from) break;
|
|
846 char *list = next_token(delim);
|
|
847 if (!list) break;
|
|
848 if ((strcmp(list, WHITE) == 0) ||
|
|
849 (strcmp(list, BLACK) == 0)) {
|
|
850 string_map &fm = really_find_from_map(dc, name);
|
|
851 fm[from] = list;
|
|
852 processed = true;
|
|
853 }
|
|
854 else {
|
|
855 // list may be the name of a previously defined from_map
|
|
856 string_map *m = find_from_map(dc, list);
|
|
857 if (m && (strcmp(list,name) != 0)) {
|
|
858 string_map &pm = *m;
|
|
859 string_map &fm = really_find_from_map(dc, name);
|
|
860 fm.insert(pm.begin(), pm.end());
|
|
861 processed = true;
|
|
862 }
|
|
863 }
|
|
864 } break;
|
|
865
|
|
866 case envto: {
|
|
867 // define the dnsbl_list and env_from maps to use for this recipient
|
|
868 char *to = next_token(delim);
|
|
869 if (!to) break;
|
|
870 char *list = next_token(delim);
|
|
871 if (!list) break;
|
|
872 char *from = next_token(delim);
|
|
873 if (!from) break;
|
|
874 dc.env_to_dnsbll[to] = list;
|
|
875 dc.env_to_chkfrom[to] = from;
|
|
876 processed = true;
|
|
877 } break;
|
|
878
|
|
879 case include: {
|
|
880 char *fn = next_token(delim);
|
3
|
881 if (ok_to_include(dc, fn)) {
|
|
882 load_conf(dc, fn);
|
|
883 processed = true;
|
|
884 }
|
|
885 } break;
|
|
886
|
|
887 case includedcc: {
|
|
888 char *name = next_token(delim);
|
|
889 if (!name) break;
|
|
890 char *fn = next_token(delim);
|
|
891 if (ok_to_include(dc, fn)) {
|
|
892 load_conf_dcc(dc, name, fn);
|
|
893 processed = true;
|
0
|
894 }
|
|
895 } break;
|
|
896
|
|
897 default: {
|
|
898 } break;
|
|
899 }
|
|
900 if (!processed) {
|
|
901 pthread_mutex_lock(&syslog_mutex);
|
|
902 openlog("dnsbl", LOG_PID, LOG_MAIL);
|
|
903 syslog(LOG_ERR, "ignoring file %s line %d : %s\n", fn, curline, orig);
|
|
904 closelog();
|
|
905 pthread_mutex_unlock(&syslog_mutex);
|
|
906 }
|
|
907 }
|
|
908 }
|
|
909 is.close();
|
|
910 }
|
|
911
|
|
912
|
|
913 ////////////////////////////////////////////////
|
|
914 // reload the config
|
|
915 //
|
|
916 static CONFIG* new_conf();
|
|
917 static CONFIG* new_conf() {
|
|
918 my_syslog("loading new configuration");
|
|
919 CONFIG *newc = new CONFIG;
|
|
920 load_conf(*newc, "dnsbl.conf");
|
|
921 newc->load_time = time(NULL);
|
|
922 return newc;
|
|
923 }
|
|
924
|
|
925
|
|
926 ////////////////////////////////////////////////
|
|
927 // thread to watch the old config files for changes
|
|
928 // and reload when needed. we also cleanup old
|
|
929 // configs whose reference count has gone to zero.
|
|
930 //
|
|
931 static void* config_loader(void *arg);
|
|
932 static void* config_loader(void *arg) {
|
|
933 typedef set<CONFIG *> configp_set;
|
|
934 configp_set old_configs;
|
|
935 while (true) {
|
|
936 sleep(180); // look for modifications every 3 minutes
|
|
937 CONFIG &dc = *config;
|
|
938 time_t then = dc.load_time;
|
|
939 struct stat st;
|
|
940 bool reload = false;
|
|
941 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) {
|
|
942 char *fn = *i;
|
|
943 if (stat(fn, &st)) reload = true; // file disappeared
|
|
944 else if (st.st_mtime > then) reload = true; // file modified
|
|
945 if (reload) break;
|
|
946 }
|
|
947 if (reload) {
|
|
948 CONFIG *newc = new_conf();
|
|
949 // replace the global config pointer
|
|
950 pthread_mutex_lock(&config_mutex);
|
|
951 CONFIG *old = config;
|
|
952 config = newc;
|
|
953 pthread_mutex_unlock(&config_mutex);
|
|
954 if (old) old_configs.insert(old);
|
|
955 }
|
|
956 // now look for old configs with zero ref counts
|
|
957 for (configp_set::iterator i=old_configs.begin(); i!=old_configs.end(); ) {
|
|
958 CONFIG *old = *i;
|
|
959 if (!old->reference_count) {
|
|
960 delete old; // destructor does all the work
|
|
961 old_configs.erase(i++);
|
|
962 }
|
|
963 else i++;
|
|
964 }
|
|
965 }
|
|
966 }
|
|
967
|
|
968
|
|
969 static void usage(char *prog);
|
|
970 static void usage(char *prog)
|
|
971 {
|
9
|
972 fprintf(stderr, "Usage: %s [-c] -p socket-addr [-t timeout]\n", prog);
|
0
|
973 fprintf(stderr, "where socket-addr is for the connection to sendmail and should be one of\n");
|
|
974 fprintf(stderr, " inet:port@local-ip-address\n");
|
|
975 fprintf(stderr, " local:local-domain-socket-file-name\n");
|
9
|
976 fprintf(stderr, "-c will load and dump the config to stdout\n");
|
0
|
977 }
|
|
978
|
|
979
|
|
980 int main(int argc, char**argv)
|
|
981 {
|
3
|
982 bool check = false;
|
|
983 bool setconn = false;
|
0
|
984 int c;
|
3
|
985 const char *args = "p:t:hc";
|
0
|
986 extern char *optarg;
|
|
987
|
|
988 // Process command line options
|
|
989 while ((c = getopt(argc, argv, args)) != -1) {
|
|
990 switch (c) {
|
|
991 case 'p':
|
|
992 if (optarg == NULL || *optarg == '\0') {
|
|
993 fprintf(stderr, "Illegal conn: %s\n", optarg);
|
|
994 exit(EX_USAGE);
|
|
995 }
|
|
996 if (smfi_setconn(optarg) == MI_FAILURE) {
|
|
997 fprintf(stderr, "smfi_setconn failed\n");
|
|
998 exit(EX_SOFTWARE);
|
|
999 }
|
|
1000
|
|
1001 if (strncasecmp(optarg, "unix:", 5) == 0) unlink(optarg + 5);
|
|
1002 else if (strncasecmp(optarg, "local:", 6) == 0) unlink(optarg + 6);
|
3
|
1003 setconn = true;
|
0
|
1004 break;
|
|
1005
|
|
1006 case 't':
|
|
1007 if (optarg == NULL || *optarg == '\0') {
|
|
1008 fprintf(stderr, "Illegal timeout: %s\n", optarg);
|
|
1009 exit(EX_USAGE);
|
|
1010 }
|
|
1011 if (smfi_settimeout(atoi(optarg)) == MI_FAILURE) {
|
|
1012 fprintf(stderr, "smfi_settimeout failed\n");
|
|
1013 exit(EX_SOFTWARE);
|
|
1014 }
|
|
1015 break;
|
|
1016
|
3
|
1017 case 'c':
|
|
1018 check = true;
|
|
1019 break;
|
|
1020
|
0
|
1021 case 'h':
|
|
1022 default:
|
|
1023 usage(argv[0]);
|
|
1024 exit(EX_USAGE);
|
|
1025 }
|
|
1026 }
|
5
|
1027
|
|
1028 if (check) {
|
|
1029 CONFIG &dc = *new_conf();
|
|
1030 dumpit(dc);
|
|
1031 return 0;
|
|
1032 }
|
|
1033
|
0
|
1034 if (!setconn) {
|
|
1035 fprintf(stderr, "%s: Missing required -p argument\n", argv[0]);
|
|
1036 usage(argv[0]);
|
|
1037 exit(EX_USAGE);
|
|
1038 }
|
5
|
1039
|
0
|
1040 if (smfi_register(smfilter) == MI_FAILURE) {
|
|
1041 fprintf(stderr, "smfi_register failed\n");
|
|
1042 exit(EX_UNAVAILABLE);
|
|
1043 }
|
|
1044
|
|
1045 // switch to background mode
|
|
1046 if (daemon(1,0) < 0) {
|
|
1047 fprintf(stderr, "daemon() call failed\n");
|
|
1048 exit(EX_UNAVAILABLE);
|
|
1049 }
|
|
1050
|
|
1051 // initialize the thread sync objects
|
|
1052 pthread_mutex_init(&config_mutex, 0);
|
|
1053 pthread_mutex_init(&syslog_mutex, 0);
|
|
1054 pthread_mutex_init(&resolve_mutex, 0);
|
|
1055
|
|
1056 // load the initial config
|
|
1057 config = new_conf();
|
|
1058
|
|
1059 // only create threads after the fork() in daemon
|
|
1060 pthread_t tid;
|
|
1061 if (pthread_create(&tid, 0, config_loader, 0))
|
|
1062 my_syslog("failed to create config loader thread");
|
|
1063 if (pthread_detach(tid))
|
|
1064 my_syslog("failed to detach config loader thread");
|
|
1065
|
|
1066 // write the pid
|
|
1067 const char *pidpath = "/var/run/dnsbl.pid";
|
|
1068 unlink(pidpath);
|
|
1069 FILE *f = fopen(pidpath, "w");
|
|
1070 if (f) {
|
|
1071 fprintf(f, "-%d\n", (u_int)getpgrp());
|
|
1072 fclose(f);
|
|
1073 }
|
|
1074
|
3
|
1075 return smfi_main();
|
0
|
1076 }
|
8
|
1077
|