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