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);
|
|
545 }
|
|
546
|
|
547 sfsistat mlfi_eom(SMFICTX *ctx)
|
|
548 {
|
|
549 sfsistat rc;
|
|
550 mlfiPriv &priv = *MLFIPRIV;
|
|
551 char *url = NULL;
|
|
552 int ip;
|
|
553 // process end of message
|
|
554 if (priv.authenticated ||
|
|
555 priv.only_whites ||
|
|
556 (check_urls(priv, url, ip) == oksofar)) rc = SMFIS_CONTINUE;
|
|
557 else {
|
|
558 if (!priv.have_whites) {
|
|
559 // can reject the entire message
|
|
560 char adr[sizeof "255.255.255.255"];
|
|
561 adr[0] = '\0';
|
|
562 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr));
|
|
563 char buf[2000];
|
|
564 snprintf(buf, sizeof(buf), priv.pc->content_message, url, adr);
|
|
565 smfi_setreply(ctx, "550", "5.7.1", buf);
|
|
566 rc = SMFIS_REJECT;
|
|
567 }
|
|
568 else {
|
|
569 // need to accept it but remove the recipients that don't want it
|
|
570 for (string_set::iterator i=priv.non_whites.begin(); i!=priv.non_whites.end(); i++) {
|
|
571 char *rcpt = *i;
|
|
572 smfi_delrcpt(ctx, rcpt);
|
|
573 }
|
|
574 rc = SMFIS_CONTINUE;
|
|
575 }
|
0
|
576 }
|
8
|
577 // reset for a new message on the same connection
|
|
578 mlfi_abort(ctx);
|
|
579 return rc;
|
|
580 }
|
|
581
|
|
582 sfsistat mlfi_abort(SMFICTX *ctx)
|
|
583 {
|
|
584 mlfiPriv &priv = *MLFIPRIV;
|
|
585 priv.reset();
|
0
|
586 return SMFIS_CONTINUE;
|
|
587 }
|
|
588
|
|
589 sfsistat mlfi_close(SMFICTX *ctx)
|
|
590 {
|
|
591 mlfiPriv *priv = MLFIPRIV;
|
|
592 if (!priv) return SMFIS_CONTINUE;
|
|
593 delete priv;
|
|
594 smfi_setpriv(ctx, NULL);
|
|
595 return SMFIS_CONTINUE;
|
|
596 }
|
|
597
|
|
598 struct smfiDesc smfilter =
|
|
599 {
|
|
600 "DNSBL", // filter name
|
|
601 SMFI_VERSION, // version code -- do not change
|
|
602 SMFIF_DELRCPT, // flags
|
|
603 mlfi_connect, // connection info filter
|
|
604 NULL, // SMTP HELO command filter
|
|
605 mlfi_envfrom, // envelope sender filter
|
|
606 mlfi_envrcpt, // envelope recipient filter
|
|
607 NULL, // header filter
|
|
608 NULL, // end of header
|
8
|
609 mlfi_body, // body block filter
|
|
610 mlfi_eom, // end of message
|
|
611 mlfi_abort, // message aborted
|
0
|
612 mlfi_close, // connection cleanup
|
|
613 };
|
|
614
|
|
615
|
|
616 static void dumpit(char *name, string_map map);
|
|
617 static void dumpit(char *name, string_map map) {
|
9
|
618 fprintf(stdout, "\n");
|
0
|
619 for (string_map::iterator i=map.begin(); i!=map.end(); i++) {
|
9
|
620 fprintf(stdout, "%s %s->%s\n", name, (*i).first, (*i).second);
|
0
|
621 }
|
|
622 }
|
|
623
|
|
624
|
|
625 static void dumpit(from_map map);
|
|
626 static void dumpit(from_map map) {
|
|
627 for (from_map::iterator i=map.begin(); i!=map.end(); i++) {
|
3
|
628 char buf[2000];
|
|
629 snprintf(buf, sizeof(buf), "envelope from map for %s", (*i).first);
|
0
|
630 string_map *sm = (*i).second;
|
3
|
631 dumpit(buf, *sm);
|
0
|
632 }
|
|
633 }
|
|
634
|
|
635
|
3
|
636 static void dumpit(CONFIG &dc);
|
|
637 static void dumpit(CONFIG &dc) {
|
5
|
638 dumpit(dc.env_from);
|
|
639 dumpit("envelope to (dnsbl list)", dc.env_to_dnsbll);
|
|
640 dumpit("envelope to (from map)", dc.env_to_chkfrom);
|
9
|
641 fprintf(stdout, "\ndnsbls\n");
|
0
|
642 for (dnsblp_map::iterator i=dc.dnsbls.begin(); i!=dc.dnsbls.end(); i++) {
|
9
|
643 fprintf(stdout, "%s %s %s\n", (*i).first, (*i).second->suffix, (*i).second->message);
|
0
|
644 }
|
9
|
645 fprintf(stdout, "\ndnsbl_lists\n");
|
0
|
646 for (dnsbllp_map::iterator i=dc.dnsblls.begin(); i!=dc.dnsblls.end(); i++) {
|
|
647 char *name = (*i).first;
|
|
648 DNSBLL &dl = *((*i).second);
|
9
|
649 fprintf(stdout, "%s", name);
|
0
|
650 for (DNSBLL::iterator j=dl.begin(); j!=dl.end(); j++) {
|
|
651 DNSBL &d = **j;
|
9
|
652 fprintf(stdout, " %s", d.suffix);
|
0
|
653 }
|
9
|
654 fprintf(stdout, "\n");
|
0
|
655 }
|
9
|
656 if (dc.content_suffix) {
|
|
657 fprintf(stdout, "\ncontent filtering enabled with %s %s\n", dc.content_suffix, dc.content_message);
|
|
658 }
|
|
659 fprintf(stdout, "\nfiles\n");
|
3
|
660 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) {
|
|
661 char *f = *i;
|
9
|
662 fprintf(stdout, "config includes %s\n", f);
|
3
|
663 }
|
|
664 }
|
|
665
|
|
666
|
|
667 ////////////////////////////////////////////////
|
|
668 // check for redundant or recursive include files
|
|
669 //
|
|
670 static bool ok_to_include(CONFIG &dc, char *fn);
|
|
671 static bool ok_to_include(CONFIG &dc, char *fn) {
|
|
672 if (!fn) return false;
|
|
673 bool ok = true;
|
|
674 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) {
|
|
675 char *f = *i;
|
|
676 if (strcmp(f, fn) == 0) {
|
|
677 my_syslog("redundant or recursive include file detected");
|
|
678 ok = false;
|
|
679 break;
|
|
680 }
|
|
681 }
|
|
682 return ok;
|
0
|
683 }
|
|
684
|
|
685
|
|
686 ////////////////////////////////////////////////
|
|
687 // load a single config file
|
|
688 //
|
3
|
689 static void load_conf_dcc(CONFIG &dc, char *name, char *fn);
|
|
690 static void load_conf_dcc(CONFIG &dc, char *name, char *fn) {
|
|
691 dc.config_files.push_back(fn);
|
|
692 char *list = BLACK;
|
|
693 const int LINE_SIZE = 2000;
|
|
694 ifstream is(fn);
|
|
695 if (is.fail()) return;
|
|
696 char line[LINE_SIZE];
|
|
697 char *delim = " \t";
|
|
698 int curline = 0;
|
|
699 while (!is.eof()) {
|
|
700 is.getline(line, LINE_SIZE);
|
|
701 curline++;
|
|
702 int n = strlen(line);
|
|
703 if (!n) continue;
|
|
704 for (int i=0; i<n; i++) line[i] = tolower(line[i]);
|
|
705 if (line[0] == '#') continue;
|
|
706 char *head = line;
|
|
707 if (strspn(line, delim) == 0) {
|
|
708 // have a leading ok/many tag to fetch
|
|
709 char *cmd = strtok(line, delim);
|
|
710 if (strcmp(cmd, MANY) == 0) list = BLACK;
|
|
711 else if (strcmp(cmd, OK) == 0) list = WHITE;
|
|
712 head = cmd + strlen(cmd) + 1;
|
|
713 }
|
|
714 char *cmd = strtok(head, delim);
|
|
715 if (!cmd) continue;
|
|
716 if (strcmp(cmd, "env_from") == 0) {
|
|
717 char *from = next_token(delim);
|
|
718 if (from) {
|
|
719 string_map &fm = really_find_from_map(dc, name);
|
|
720 fm[from] = list;
|
|
721 }
|
|
722 }
|
|
723 else if (strcmp(cmd, "env_to") == 0) {
|
|
724 char *to = next_token(delim);
|
|
725 if (to) {
|
|
726 dc.env_to_dnsbll[to] = list;
|
|
727 dc.env_to_chkfrom[to] = list;
|
|
728 }
|
|
729 }
|
|
730 else if (strcmp(cmd, "substitute") == 0) {
|
|
731 char *tag = next_token(delim);
|
|
732 if (tag && (strcmp(tag, "mail_host") == 0)) {
|
|
733 char *from = next_token(delim);
|
|
734 if (from) {
|
|
735 string_map &fm = really_find_from_map(dc, name);
|
|
736 fm[from] = list;
|
|
737 }
|
|
738 }
|
|
739 }
|
|
740 else if (strcmp(cmd, "include") == 0) {
|
|
741 char *fn = next_token(delim);
|
|
742 if (ok_to_include(dc, fn)) {
|
|
743 load_conf_dcc(dc, name, fn);
|
|
744 }
|
|
745 }
|
|
746
|
|
747 }
|
|
748 is.close();
|
|
749 }
|
|
750
|
|
751
|
0
|
752 static void load_conf(CONFIG &dc, char *fn);
|
|
753 static void load_conf(CONFIG &dc, char *fn) {
|
|
754 dc.config_files.push_back(fn);
|
|
755 map<char*, int, ltstr> commands;
|
8
|
756 enum {dummy, content, dnsbl, dnsbll, envfrom, envto, include, includedcc};
|
|
757 commands["content" ] = content;
|
3
|
758 commands["dnsbl" ] = dnsbl;
|
|
759 commands["dnsbl_list" ] = dnsbll;
|
|
760 commands["env_from" ] = envfrom;
|
|
761 commands["env_to" ] = envto;
|
|
762 commands["include" ] = include;
|
|
763 commands["include_dcc"] = includedcc;
|
0
|
764 const int LINE_SIZE = 2000;
|
|
765 ifstream is(fn);
|
|
766 if (is.fail()) return;
|
|
767 char line[LINE_SIZE];
|
|
768 char orig[LINE_SIZE];
|
|
769 char *delim = " \t";
|
|
770 int curline = 0;
|
|
771 while (!is.eof()) {
|
|
772 is.getline(line, LINE_SIZE);
|
|
773 snprintf(orig, sizeof(orig), "%s", line);
|
|
774 curline++;
|
|
775 int n = strlen(line);
|
|
776 for (int i=0; i<n; i++) line[i] = tolower(line[i]);
|
|
777 char *cmd = strtok(line, delim);
|
|
778 if (cmd && (cmd[0] != '#') && (cmd[0] != '\0')) {
|
|
779 // have a decent command
|
|
780 bool processed = false;
|
|
781 switch (commands[cmd]) {
|
8
|
782 case content: {
|
|
783 char *suff = strtok(NULL, delim);
|
|
784 if (!suff) break; // no dns suffic
|
|
785 char *msg = suff + strlen(suff);
|
|
786 if ((msg - line) >= strlen(orig)) break; // line ended with the dns suffix
|
|
787 msg = strchr(msg+1, '\'');
|
|
788 if (!msg) break; // no reply message template
|
|
789 msg++; // move over the leading '
|
|
790 if ((msg - line) >= strlen(orig)) break; // line ended with the leading quote
|
|
791 char *last = strchr(msg, '\'');
|
|
792 if (!last) break; // no trailing quote
|
|
793 *last = '\0'; // make it a null terminator
|
|
794 dc.content_suffix = register_string(suff);
|
|
795 dc.content_message = register_string(msg);
|
|
796 processed = true;
|
|
797 } break;
|
|
798
|
0
|
799 case dnsbl: {
|
|
800 // have a new dnsbl to use
|
|
801 char *name = next_token(delim);
|
|
802 if (!name) break; // no name name
|
|
803 if (find_dnsbl(dc, name)) break; // duplicate entry
|
|
804 char *suff = strtok(NULL, delim);
|
|
805 if (!suff) break; // no dns suffic
|
|
806 char *msg = suff + strlen(suff);
|
|
807 if ((msg - line) >= strlen(orig)) break; // line ended with the dns suffix
|
|
808 msg = strchr(msg+1, '\'');
|
|
809 if (!msg) break; // no reply message template
|
|
810 msg++; // move over the leading '
|
|
811 if ((msg - line) >= strlen(orig)) break; // line ended with the leading quote
|
|
812 char *last = strchr(msg, '\'');
|
|
813 if (!last) break; // no trailing quote
|
|
814 *last = '\0'; // make it a null terminator
|
|
815 dc.dnsbls[name] = new DNSBL(register_string(suff), register_string(msg));
|
|
816 processed = true;
|
|
817 } break;
|
|
818
|
|
819 case dnsbll: {
|
|
820 // define a new combination of dnsbls
|
|
821 char *name = next_token(delim);
|
|
822 if (!name) break;
|
|
823 if (find_dnsbll(dc, name)) break; // duplicate entry
|
|
824 char *list = next_token(delim);
|
|
825 if (!list || (*list == '\0') || (*list == '#')) break;
|
|
826 DNSBLLP d = new DNSBLL;
|
|
827 DNSBLP p = find_dnsbl(dc, list);
|
|
828 if (p) d->push_back(p);
|
|
829 while (true) {
|
|
830 list = next_token(delim);
|
|
831 if (!list || (*list == '\0') || (*list == '#')) break;
|
|
832 DNSBLP p = find_dnsbl(dc, list);
|
|
833 if (p) d->push_back(p);
|
|
834 }
|
|
835 dc.dnsblls[name] = d;
|
|
836 processed = true;
|
|
837 } break;
|
|
838
|
|
839 case envfrom: {
|
|
840 // add an entry into the named string_map
|
|
841 char *name = next_token(delim);
|
|
842 if (!name) break;
|
|
843 char *from = next_token(delim);
|
|
844 if (!from) break;
|
|
845 char *list = next_token(delim);
|
|
846 if (!list) break;
|
|
847 if ((strcmp(list, WHITE) == 0) ||
|
|
848 (strcmp(list, BLACK) == 0)) {
|
|
849 string_map &fm = really_find_from_map(dc, name);
|
|
850 fm[from] = list;
|
|
851 processed = true;
|
|
852 }
|
|
853 else {
|
|
854 // list may be the name of a previously defined from_map
|
|
855 string_map *m = find_from_map(dc, list);
|
|
856 if (m && (strcmp(list,name) != 0)) {
|
|
857 string_map &pm = *m;
|
|
858 string_map &fm = really_find_from_map(dc, name);
|
|
859 fm.insert(pm.begin(), pm.end());
|
|
860 processed = true;
|
|
861 }
|
|
862 }
|
|
863 } break;
|
|
864
|
|
865 case envto: {
|
|
866 // define the dnsbl_list and env_from maps to use for this recipient
|
|
867 char *to = next_token(delim);
|
|
868 if (!to) break;
|
|
869 char *list = next_token(delim);
|
|
870 if (!list) break;
|
|
871 char *from = next_token(delim);
|
|
872 if (!from) break;
|
|
873 dc.env_to_dnsbll[to] = list;
|
|
874 dc.env_to_chkfrom[to] = from;
|
|
875 processed = true;
|
|
876 } break;
|
|
877
|
|
878 case include: {
|
|
879 char *fn = next_token(delim);
|
3
|
880 if (ok_to_include(dc, fn)) {
|
|
881 load_conf(dc, fn);
|
|
882 processed = true;
|
|
883 }
|
|
884 } break;
|
|
885
|
|
886 case includedcc: {
|
|
887 char *name = next_token(delim);
|
|
888 if (!name) break;
|
|
889 char *fn = next_token(delim);
|
|
890 if (ok_to_include(dc, fn)) {
|
|
891 load_conf_dcc(dc, name, fn);
|
|
892 processed = true;
|
0
|
893 }
|
|
894 } break;
|
|
895
|
|
896 default: {
|
|
897 } break;
|
|
898 }
|
|
899 if (!processed) {
|
|
900 pthread_mutex_lock(&syslog_mutex);
|
|
901 openlog("dnsbl", LOG_PID, LOG_MAIL);
|
|
902 syslog(LOG_ERR, "ignoring file %s line %d : %s\n", fn, curline, orig);
|
|
903 closelog();
|
|
904 pthread_mutex_unlock(&syslog_mutex);
|
|
905 }
|
|
906 }
|
|
907 }
|
|
908 is.close();
|
|
909 }
|
|
910
|
|
911
|
|
912 ////////////////////////////////////////////////
|
|
913 // reload the config
|
|
914 //
|
|
915 static CONFIG* new_conf();
|
|
916 static CONFIG* new_conf() {
|
|
917 my_syslog("loading new configuration");
|
|
918 CONFIG *newc = new CONFIG;
|
|
919 load_conf(*newc, "dnsbl.conf");
|
|
920 newc->load_time = time(NULL);
|
|
921 return newc;
|
|
922 }
|
|
923
|
|
924
|
|
925 ////////////////////////////////////////////////
|
|
926 // thread to watch the old config files for changes
|
|
927 // and reload when needed. we also cleanup old
|
|
928 // configs whose reference count has gone to zero.
|
|
929 //
|
|
930 static void* config_loader(void *arg);
|
|
931 static void* config_loader(void *arg) {
|
|
932 typedef set<CONFIG *> configp_set;
|
|
933 configp_set old_configs;
|
|
934 while (true) {
|
|
935 sleep(180); // look for modifications every 3 minutes
|
|
936 CONFIG &dc = *config;
|
|
937 time_t then = dc.load_time;
|
|
938 struct stat st;
|
|
939 bool reload = false;
|
|
940 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) {
|
|
941 char *fn = *i;
|
|
942 if (stat(fn, &st)) reload = true; // file disappeared
|
|
943 else if (st.st_mtime > then) reload = true; // file modified
|
|
944 if (reload) break;
|
|
945 }
|
|
946 if (reload) {
|
|
947 CONFIG *newc = new_conf();
|
|
948 // replace the global config pointer
|
|
949 pthread_mutex_lock(&config_mutex);
|
|
950 CONFIG *old = config;
|
|
951 config = newc;
|
|
952 pthread_mutex_unlock(&config_mutex);
|
|
953 if (old) old_configs.insert(old);
|
|
954 }
|
|
955 // now look for old configs with zero ref counts
|
|
956 for (configp_set::iterator i=old_configs.begin(); i!=old_configs.end(); ) {
|
|
957 CONFIG *old = *i;
|
|
958 if (!old->reference_count) {
|
|
959 delete old; // destructor does all the work
|
|
960 old_configs.erase(i++);
|
|
961 }
|
|
962 else i++;
|
|
963 }
|
|
964 }
|
|
965 }
|
|
966
|
|
967
|
|
968 static void usage(char *prog);
|
|
969 static void usage(char *prog)
|
|
970 {
|
9
|
971 fprintf(stderr, "Usage: %s [-c] -p socket-addr [-t timeout]\n", prog);
|
0
|
972 fprintf(stderr, "where socket-addr is for the connection to sendmail and should be one of\n");
|
|
973 fprintf(stderr, " inet:port@local-ip-address\n");
|
|
974 fprintf(stderr, " local:local-domain-socket-file-name\n");
|
9
|
975 fprintf(stderr, "-c will load and dump the config to stdout\n");
|
0
|
976 }
|
|
977
|
|
978
|
|
979 int main(int argc, char**argv)
|
|
980 {
|
3
|
981 bool check = false;
|
|
982 bool setconn = false;
|
0
|
983 int c;
|
3
|
984 const char *args = "p:t:hc";
|
0
|
985 extern char *optarg;
|
|
986
|
|
987 // Process command line options
|
|
988 while ((c = getopt(argc, argv, args)) != -1) {
|
|
989 switch (c) {
|
|
990 case 'p':
|
|
991 if (optarg == NULL || *optarg == '\0') {
|
|
992 fprintf(stderr, "Illegal conn: %s\n", optarg);
|
|
993 exit(EX_USAGE);
|
|
994 }
|
|
995 if (smfi_setconn(optarg) == MI_FAILURE) {
|
|
996 fprintf(stderr, "smfi_setconn failed\n");
|
|
997 exit(EX_SOFTWARE);
|
|
998 }
|
|
999
|
|
1000 if (strncasecmp(optarg, "unix:", 5) == 0) unlink(optarg + 5);
|
|
1001 else if (strncasecmp(optarg, "local:", 6) == 0) unlink(optarg + 6);
|
3
|
1002 setconn = true;
|
0
|
1003 break;
|
|
1004
|
|
1005 case 't':
|
|
1006 if (optarg == NULL || *optarg == '\0') {
|
|
1007 fprintf(stderr, "Illegal timeout: %s\n", optarg);
|
|
1008 exit(EX_USAGE);
|
|
1009 }
|
|
1010 if (smfi_settimeout(atoi(optarg)) == MI_FAILURE) {
|
|
1011 fprintf(stderr, "smfi_settimeout failed\n");
|
|
1012 exit(EX_SOFTWARE);
|
|
1013 }
|
|
1014 break;
|
|
1015
|
3
|
1016 case 'c':
|
|
1017 check = true;
|
|
1018 break;
|
|
1019
|
0
|
1020 case 'h':
|
|
1021 default:
|
|
1022 usage(argv[0]);
|
|
1023 exit(EX_USAGE);
|
|
1024 }
|
|
1025 }
|
5
|
1026
|
|
1027 if (check) {
|
|
1028 CONFIG &dc = *new_conf();
|
|
1029 dumpit(dc);
|
|
1030 return 0;
|
|
1031 }
|
|
1032
|
0
|
1033 if (!setconn) {
|
|
1034 fprintf(stderr, "%s: Missing required -p argument\n", argv[0]);
|
|
1035 usage(argv[0]);
|
|
1036 exit(EX_USAGE);
|
|
1037 }
|
5
|
1038
|
0
|
1039 if (smfi_register(smfilter) == MI_FAILURE) {
|
|
1040 fprintf(stderr, "smfi_register failed\n");
|
|
1041 exit(EX_UNAVAILABLE);
|
|
1042 }
|
|
1043
|
|
1044 // switch to background mode
|
|
1045 if (daemon(1,0) < 0) {
|
|
1046 fprintf(stderr, "daemon() call failed\n");
|
|
1047 exit(EX_UNAVAILABLE);
|
|
1048 }
|
|
1049
|
|
1050 // initialize the thread sync objects
|
|
1051 pthread_mutex_init(&config_mutex, 0);
|
|
1052 pthread_mutex_init(&syslog_mutex, 0);
|
|
1053 pthread_mutex_init(&resolve_mutex, 0);
|
|
1054
|
|
1055 // load the initial config
|
|
1056 config = new_conf();
|
|
1057
|
|
1058 // only create threads after the fork() in daemon
|
|
1059 pthread_t tid;
|
|
1060 if (pthread_create(&tid, 0, config_loader, 0))
|
|
1061 my_syslog("failed to create config loader thread");
|
|
1062 if (pthread_detach(tid))
|
|
1063 my_syslog("failed to detach config loader thread");
|
|
1064
|
|
1065 // write the pid
|
|
1066 const char *pidpath = "/var/run/dnsbl.pid";
|
|
1067 unlink(pidpath);
|
|
1068 FILE *f = fopen(pidpath, "w");
|
|
1069 if (f) {
|
|
1070 fprintf(f, "-%d\n", (u_int)getpgrp());
|
|
1071 fclose(f);
|
|
1072 }
|
|
1073
|
3
|
1074 return smfi_main();
|
0
|
1075 }
|
8
|
1076
|