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