1/*++
2/* NAME
3/*	postscreen_dnsbl 3
4/* SUMMARY
5/*	postscreen DNSBL support
6/* SYNOPSIS
7/*	#include <postscreen.h>
8/*
9/*	void	psc_dnsbl_init(void)
10/*
11/*	int	psc_dnsbl_request(client_addr, callback, context)
12/*	char	*client_addr;
13/*	void	(*callback)(int, char *);
14/*	char	*context;
15/*
16/*	int	psc_dnsbl_retrieve(client_addr, dnsbl_name, dnsbl_index)
17/*	char	*client_addr;
18/*	const char **dnsbl_name;
19/*	int	dnsbl_index;
20/* DESCRIPTION
21/*	This module implements preliminary support for DNSBL lookups.
22/*	Multiple requests for the same information are handled with
23/*	reference counts.
24/*
25/*	psc_dnsbl_init() initializes this module, and must be called
26/*	once before any of the other functions in this module.
27/*
28/*	psc_dnsbl_request() requests a blocklist score for the
29/*	specified client IP address and increments the reference
30/*	count.  The request completes in the background. The client
31/*	IP address must be in inet_ntop(3) output format.  The
32/*	callback argument specifies a function that is called when
33/*	the requested result is available. The context is passed
34/*	on to the callback function. The callback should ignore its
35/*	first argument (it exists for compatibility with Postfix
36/*	generic event infrastructure).
37/*	The result value is the index for the psc_dnsbl_retrieve()
38/*	call.
39/*
40/*	psc_dnsbl_retrieve() retrieves the result score requested with
41/*	psc_dnsbl_request() and decrements the reference count. It
42/*	is an error to retrieve a score without requesting it first.
43/* LICENSE
44/* .ad
45/* .fi
46/*	The Secure Mailer license must be distributed with this software.
47/* AUTHOR(S)
48/*	Wietse Venema
49/*	IBM T.J. Watson Research
50/*	P.O. Box 704
51/*	Yorktown Heights, NY 10598, USA
52/*--*/
53
54/* System library. */
55
56#include <sys_defs.h>
57#include <sys/socket.h>			/* AF_INET */
58#include <netinet/in.h>			/* inet_pton() */
59#include <arpa/inet.h>			/* inet_pton() */
60#include <stdio.h>			/* sscanf */
61
62/* Utility library. */
63
64#include <msg.h>
65#include <mymalloc.h>
66#include <argv.h>
67#include <htable.h>
68#include <events.h>
69#include <vstream.h>
70#include <connect.h>
71#include <split_at.h>
72#include <valid_hostname.h>
73#include <ip_match.h>
74#include <myaddrinfo.h>
75#include <stringops.h>
76
77/* Global library. */
78
79#include <mail_params.h>
80#include <mail_proto.h>
81
82/* Application-specific. */
83
84#include <postscreen.h>
85
86 /*
87  * Talking to the DNSBLOG service.
88  */
89#define DNSBLOG_TIMEOUT			10
90static char *psc_dnsbl_service;
91
92 /*
93  * Per-DNSBL filters and weights.
94  *
95  * The postscreen_dnsbl_sites parameter specifies zero or more DNSBL domains.
96  * We provide multiple access methods, one for quick iteration when sending
97  * queries to all DNSBL servers, and one for quick location when receiving a
98  * reply from one DNSBL server.
99  *
100  * Each DNSBL domain can be specified more than once, each time with a
101  * different (filter, weight) pair. We group (filter, weight) pairs in a
102  * linked list under their DNSBL domain name. The list head has a reference
103  * to a "safe name" for the DNSBL, in case the name includes a password.
104  */
105static HTABLE *dnsbl_site_cache;	/* indexed by DNSBNL domain */
106static HTABLE_INFO **dnsbl_site_list;	/* flattened cache */
107
108typedef struct {
109    const char *safe_dnsbl;		/* from postscreen_dnsbl_reply_map */
110    struct PSC_DNSBL_SITE *first;	/* list of (filter, weight) tuples */
111} PSC_DNSBL_HEAD;
112
113typedef struct PSC_DNSBL_SITE {
114    char   *filter;			/* printable filter (default: null) */
115    char   *byte_codes;			/* encoded filter (default: null) */
116    int     weight;			/* reply weight (default: 1) */
117    struct PSC_DNSBL_SITE *next;	/* linked list */
118} PSC_DNSBL_SITE;
119
120 /*
121  * Per-client DNSBL scores.
122  *
123  * Some SMTP clients make parallel connections. This can trigger parallel
124  * blocklist score requests when the pre-handshake delays of the connections
125  * overlap.
126  *
127  * We combine requests for the same score under the client IP address in a
128  * single reference-counted entry. The reference count goes up with each
129  * request for a score, and it goes down with each score retrieval. Each
130  * score has one or more requestors that need to be notified when the result
131  * is ready, so that postscreen can terminate a pre-handshake delay when all
132  * pre-handshake tests are completed.
133  */
134static HTABLE *dnsbl_score_cache;	/* indexed by client address */
135
136typedef struct {
137    void    (*callback) (int, char *);	/* generic call-back routine */
138    char   *context;			/* generic call-back argument */
139} PSC_CALL_BACK_ENTRY;
140
141typedef struct {
142    const char *dnsbl_name;		/* DNSBL with largest contribution */
143    int     dnsbl_weight;		/* weight of largest contribution */
144    int     total;			/* combined blocklist score */
145    int     refcount;			/* score reference count */
146    int     pending_lookups;		/* nr of DNS requests in flight */
147    int     request_id;			/* duplicate suppression */
148    /* Call-back table support. */
149    int     index;			/* next table index */
150    int     limit;			/* last valid index */
151    PSC_CALL_BACK_ENTRY table[1];	/* actually a bunch */
152} PSC_DNSBL_SCORE;
153
154#define PSC_CALL_BACK_INIT(sp) do { \
155	(sp)->limit = 0; \
156	(sp)->index = 0; \
157    } while (0)
158
159#define PSC_CALL_BACK_INDEX_OF_LAST(sp) ((sp)->index - 1)
160
161#define PSC_CALL_BACK_CANCEL(sp, idx) do { \
162	PSC_CALL_BACK_ENTRY *_cb_; \
163	if ((idx) < 0 || (idx) >= (sp)->index) \
164	    msg_panic("%s: index %d must be >= 0 and < %d", \
165		      myname, (idx), (sp)->index); \
166	_cb_ = (sp)->table + (idx); \
167	event_cancel_timer(_cb_->callback, _cb_->context); \
168	_cb_->callback = 0; \
169	_cb_->context = 0; \
170    } while (0)
171
172#define PSC_CALL_BACK_EXTEND(hp, sp) do { \
173	if ((sp)->index >= (sp)->limit) { \
174	    int _count_ = ((sp)->limit ? (sp)->limit * 2 : 5); \
175	    (hp)->value = myrealloc((char *) (sp), sizeof(*(sp)) + \
176				    _count_ * sizeof((sp)->table)); \
177	    (sp) = (PSC_DNSBL_SCORE *) (hp)->value; \
178	    (sp)->limit = _count_; \
179	} \
180    } while (0)
181
182#define PSC_CALL_BACK_ENTER(sp, fn, ctx) do { \
183	PSC_CALL_BACK_ENTRY *_cb_ = (sp)->table + (sp)->index++; \
184	_cb_->callback = (fn); \
185	_cb_->context = (ctx); \
186    } while (0)
187
188#define PSC_CALL_BACK_NOTIFY(sp, ev) do { \
189	PSC_CALL_BACK_ENTRY *_cb_; \
190	for (_cb_ = (sp)->table; _cb_ < (sp)->table + (sp)->index; _cb_++) \
191	    if (_cb_->callback != 0) \
192		_cb_->callback((ev), _cb_->context); \
193    } while (0)
194
195#define PSC_NULL_EVENT	(0)
196
197 /*
198  * Per-request state.
199  *
200  * This implementation stores the client IP address and DNSBL domain in the
201  * DNSBLOG query/reply stream. This simplifies code, and allows the DNSBLOG
202  * server to produce more informative logging.
203  */
204static VSTRING *reply_client;		/* client address in DNSBLOG reply */
205static VSTRING *reply_dnsbl;		/* domain in DNSBLOG reply */
206static VSTRING *reply_addr;		/* adress list in DNSBLOG reply */
207
208/* psc_dnsbl_add_site - add DNSBL site information */
209
210static void psc_dnsbl_add_site(const char *site)
211{
212    const char *myname = "psc_dnsbl_add_site";
213    char   *saved_site = mystrdup(site);
214    VSTRING *byte_codes = 0;
215    PSC_DNSBL_HEAD *head;
216    PSC_DNSBL_SITE *new_site;
217    char    junk;
218    const char *weight_text;
219    char   *pattern_text;
220    int     weight;
221    HTABLE_INFO *ht;
222    char   *parse_err;
223
224    /*
225     * Parse the required DNSBL domain name, the optional reply filter and
226     * the optional reply weight factor.
227     */
228#define DO_GRIPE	1
229
230    /* Negative weight means whitelist. */
231    if ((weight_text = split_at(saved_site, '*')) != 0) {
232	if (sscanf(weight_text, "%d%c", &weight, &junk) != 1)
233	    msg_fatal("bad DNSBL weight factor \"%s\" in \"%s\"",
234		      weight_text, site);
235    } else {
236	weight = 1;
237    }
238    /* Reply filter. */
239    if ((pattern_text = split_at(saved_site, '=')) != 0) {
240	byte_codes = vstring_alloc(100);
241	if ((parse_err = ip_match_parse(byte_codes, pattern_text)) != 0)
242	    msg_fatal("bad DNSBL filter syntax: %s", parse_err);
243    }
244    if (valid_hostname(saved_site, DO_GRIPE) == 0)
245	msg_fatal("bad DNSBL domain name \"%s\" in \"%s\"",
246		  saved_site, site);
247
248    if (msg_verbose > 1)
249	msg_info("%s: \"%s\" -> domain=\"%s\" pattern=\"%s\" weight=%d",
250		 myname, site, saved_site, pattern_text ? pattern_text :
251		 "null", weight);
252
253    /*
254     * Look up or create the (filter, weight) list head for this DNSBL domain
255     * name.
256     */
257    if ((head = (PSC_DNSBL_HEAD *)
258	 htable_find(dnsbl_site_cache, saved_site)) == 0) {
259	head = (PSC_DNSBL_HEAD *) mymalloc(sizeof(*head));
260	ht = htable_enter(dnsbl_site_cache, saved_site, (char *) head);
261	/* Translate the DNSBL name into a safe name if available. */
262	if (psc_dnsbl_reply == 0
263	 || (head->safe_dnsbl = dict_get(psc_dnsbl_reply, saved_site)) == 0)
264	    head->safe_dnsbl = ht->key;
265	if (psc_dnsbl_reply && psc_dnsbl_reply->error)
266	    msg_fatal("%s:%s lookup error", psc_dnsbl_reply->type,
267		      psc_dnsbl_reply->name);
268	head->first = 0;
269    }
270
271    /*
272     * Append the new (filter, weight) node to the list for this DNSBL domain
273     * name.
274     */
275    new_site = (PSC_DNSBL_SITE *) mymalloc(sizeof(*new_site));
276    new_site->filter = (pattern_text ? mystrdup(pattern_text) : 0);
277    new_site->byte_codes = (byte_codes ? ip_match_save(byte_codes) : 0);
278    new_site->weight = weight;
279    new_site->next = head->first;
280    head->first = new_site;
281
282    myfree(saved_site);
283    if (byte_codes)
284	vstring_free(byte_codes);
285}
286
287/* psc_dnsbl_match - match DNSBL reply filter */
288
289static int psc_dnsbl_match(const char *filter, ARGV *reply)
290{
291    char    addr_buf[MAI_HOSTADDR_STRSIZE];
292    char  **cpp;
293
294    /*
295     * Run the replies through the pattern-matching engine.
296     */
297    for (cpp = reply->argv; *cpp != 0; cpp++) {
298	if (inet_pton(AF_INET, *cpp, addr_buf) != 1)
299	    msg_warn("address conversion error for %s -- ignoring this reply",
300		     *cpp);
301	if (ip_match_execute(filter, addr_buf))
302	    return (1);
303    }
304    return (0);
305}
306
307/* psc_dnsbl_retrieve - retrieve blocklist score, decrement reference count */
308
309int     psc_dnsbl_retrieve(const char *client_addr, const char **dnsbl_name,
310			           int dnsbl_index)
311{
312    const char *myname = "psc_dnsbl_retrieve";
313    PSC_DNSBL_SCORE *score;
314    int     result_score;
315
316    /*
317     * Sanity check.
318     */
319    if ((score = (PSC_DNSBL_SCORE *)
320	 htable_find(dnsbl_score_cache, client_addr)) == 0)
321	msg_panic("%s: no blocklist score for %s", myname, client_addr);
322
323    /*
324     * Disable callbacks.
325     */
326    PSC_CALL_BACK_CANCEL(score, dnsbl_index);
327
328    /*
329     * Reads are destructive.
330     */
331    result_score = score->total;
332    *dnsbl_name = score->dnsbl_name;
333    score->refcount -= 1;
334    if (score->refcount < 1) {
335	if (msg_verbose > 1)
336	    msg_info("%s: delete blocklist score for %s", myname, client_addr);
337	htable_delete(dnsbl_score_cache, client_addr, myfree);
338    }
339    return (result_score);
340}
341
342/* psc_dnsbl_receive - receive DNSBL reply, update blocklist score */
343
344static void psc_dnsbl_receive(int event, char *context)
345{
346    const char *myname = "psc_dnsbl_receive";
347    VSTREAM *stream = (VSTREAM *) context;
348    PSC_DNSBL_SCORE *score;
349    PSC_DNSBL_HEAD *head;
350    PSC_DNSBL_SITE *site;
351    ARGV   *reply_argv;
352    int     request_id;
353
354    PSC_CLEAR_EVENT_REQUEST(vstream_fileno(stream), psc_dnsbl_receive, context);
355
356    /*
357     * Receive the DNSBL lookup result.
358     *
359     * This is preliminary code to explore the field. Later, DNSBL lookup will
360     * be handled by an UDP-based DNS client that is built directly into some
361     * Postfix daemon.
362     *
363     * Don't bother looking up the blocklist score when the client IP address is
364     * not listed at the DNSBL.
365     *
366     * Don't panic when the blocklist score no longer exists. It may be deleted
367     * when the client triggers a "drop" action after pregreet, when the
368     * client does not pregreet and the DNSBL reply arrives late, or when the
369     * client triggers a "drop" action after hanging up.
370     */
371    if (event == EVENT_READ
372	&& attr_scan(stream,
373		     ATTR_FLAG_STRICT,
374		     ATTR_TYPE_STR, MAIL_ATTR_RBL_DOMAIN, reply_dnsbl,
375		     ATTR_TYPE_STR, MAIL_ATTR_ACT_CLIENT_ADDR, reply_client,
376		     ATTR_TYPE_INT, MAIL_ATTR_LABEL, &request_id,
377		     ATTR_TYPE_STR, MAIL_ATTR_RBL_ADDR, reply_addr,
378		     ATTR_TYPE_END) == 4
379	&& (score = (PSC_DNSBL_SCORE *)
380	    htable_find(dnsbl_score_cache, STR(reply_client))) != 0
381	&& score->request_id == request_id) {
382
383	/*
384	 * Run this response past all applicable DNSBL filters and update the
385	 * blocklist score for this client IP address.
386	 *
387	 * Don't panic when the DNSBL domain name is not found. The DNSBLOG
388	 * server may be messed up.
389	 */
390	if (msg_verbose > 1)
391	    msg_info("%s: client=\"%s\" score=%d domain=\"%s\" reply=\"%s\"",
392		     myname, STR(reply_client), score->total,
393		     STR(reply_dnsbl), STR(reply_addr));
394	if (*STR(reply_addr) != 0) {
395	    head = (PSC_DNSBL_HEAD *)
396		htable_find(dnsbl_site_cache, STR(reply_dnsbl));
397	    site = (head ? head->first : (PSC_DNSBL_SITE *) 0);
398	    for (reply_argv = 0; site != 0; site = site->next) {
399		if (site->byte_codes == 0
400		    || psc_dnsbl_match(site->byte_codes, reply_argv ? reply_argv :
401			 (reply_argv = argv_split(STR(reply_addr), " ")))) {
402		    if (score->dnsbl_name == 0
403			|| score->dnsbl_weight < site->weight) {
404			score->dnsbl_name = head->safe_dnsbl;
405			score->dnsbl_weight = site->weight;
406		    }
407		    score->total += site->weight;
408		    if (msg_verbose > 1)
409			msg_info("%s: filter=\"%s\" weight=%d score=%d",
410			       myname, site->filter ? site->filter : "null",
411				 site->weight, score->total);
412		}
413	    }
414	    if (reply_argv != 0)
415		argv_free(reply_argv);
416	}
417
418	/*
419	 * Notify the requestor(s) that the result is ready to be picked up.
420	 * If this call isn't made, clients have to sit out the entire
421	 * pre-handshake delay.
422	 */
423	score->pending_lookups -= 1;
424	if (score->pending_lookups == 0)
425	    PSC_CALL_BACK_NOTIFY(score, PSC_NULL_EVENT);
426    } else if (event == EVENT_TIME) {
427	msg_warn("dnsblog reply timeout %ds for %s",
428		 DNSBLOG_TIMEOUT, (char *) vstream_context(stream));
429    }
430    /* Here, score may be a null pointer. */
431    vstream_fclose(stream);
432}
433
434/* psc_dnsbl_request  - send dnsbl query, increment reference count */
435
436int     psc_dnsbl_request(const char *client_addr,
437			          void (*callback) (int, char *),
438			          char *context)
439{
440    const char *myname = "psc_dnsbl_request";
441    int     fd;
442    VSTREAM *stream;
443    HTABLE_INFO **ht;
444    PSC_DNSBL_SCORE *score;
445    HTABLE_INFO *hash_node;
446    static int request_count;
447
448    /*
449     * Some spambots make several connections at nearly the same time,
450     * causing their pregreet delays to overlap. Such connections can share
451     * the efforts of DNSBL lookup.
452     *
453     * We store a reference-counted DNSBL score under its client IP address. We
454     * increment the reference count with each score request, and decrement
455     * the reference count with each score retrieval.
456     *
457     * Do not notify the requestor NOW when the DNS replies are already in.
458     * Reason: we must not make a backwards call while we are still in the
459     * middle of executing the corresponding forward call. Instead we create
460     * a zero-delay timer request and call the notification function from
461     * there.
462     *
463     * psc_dnsbl_request() could instead return a result value to indicate that
464     * the DNSBL score is already available, but that would complicate the
465     * caller with two different notification code paths: one asynchronous
466     * code path via the callback invocation, and one synchronous code path
467     * via the psc_dnsbl_request() result value. That would be a source of
468     * future bugs.
469     */
470    if ((hash_node = htable_locate(dnsbl_score_cache, client_addr)) != 0) {
471	score = (PSC_DNSBL_SCORE *) hash_node->value;
472	score->refcount += 1;
473	PSC_CALL_BACK_EXTEND(hash_node, score);
474	PSC_CALL_BACK_ENTER(score, callback, context);
475	if (msg_verbose > 1)
476	    msg_info("%s: reuse blocklist score for %s refcount=%d pending=%d",
477		     myname, client_addr, score->refcount,
478		     score->pending_lookups);
479	if (score->pending_lookups == 0)
480	    event_request_timer(callback, context, EVENT_NULL_DELAY);
481	return (PSC_CALL_BACK_INDEX_OF_LAST(score));
482    }
483    if (msg_verbose > 1)
484	msg_info("%s: create blocklist score for %s", myname, client_addr);
485    score = (PSC_DNSBL_SCORE *) mymalloc(sizeof(*score));
486    score->request_id = request_count++;
487    score->dnsbl_name = 0;
488    score->dnsbl_weight = 0;
489    score->total = 0;
490    score->refcount = 1;
491    score->pending_lookups = 0;
492    PSC_CALL_BACK_INIT(score);
493    PSC_CALL_BACK_ENTER(score, callback, context);
494    (void) htable_enter(dnsbl_score_cache, client_addr, (char *) score);
495
496    /*
497     * Send a query to all DNSBL servers. Later, DNSBL lookup will be done
498     * with an UDP-based DNS client that is built directly into Postfix code.
499     * We therefore do not optimize the maximum out of this temporary
500     * implementation.
501     */
502    for (ht = dnsbl_site_list; *ht; ht++) {
503	if ((fd = LOCAL_CONNECT(psc_dnsbl_service, NON_BLOCKING, 1)) < 0) {
504	    msg_warn("%s: connect to %s service: %m",
505		     myname, psc_dnsbl_service);
506	    continue;
507	}
508	stream = vstream_fdopen(fd, O_RDWR);
509	vstream_control(stream,
510			VSTREAM_CTL_CONTEXT, ht[0]->key,
511			VSTREAM_CTL_END);
512	attr_print(stream, ATTR_FLAG_NONE,
513		   ATTR_TYPE_STR, MAIL_ATTR_RBL_DOMAIN, ht[0]->key,
514		   ATTR_TYPE_STR, MAIL_ATTR_ACT_CLIENT_ADDR, client_addr,
515		   ATTR_TYPE_INT, MAIL_ATTR_LABEL, score->request_id,
516		   ATTR_TYPE_END);
517	if (vstream_fflush(stream) != 0) {
518	    msg_warn("%s: error sending to %s service: %m",
519		     myname, psc_dnsbl_service);
520	    vstream_fclose(stream);
521	    continue;
522	}
523	PSC_READ_EVENT_REQUEST(vstream_fileno(stream), psc_dnsbl_receive,
524			       (char *) stream, DNSBLOG_TIMEOUT);
525	score->pending_lookups += 1;
526    }
527    return (PSC_CALL_BACK_INDEX_OF_LAST(score));
528}
529
530/* psc_dnsbl_init - initialize */
531
532void    psc_dnsbl_init(void)
533{
534    const char *myname = "psc_dnsbl_init";
535    ARGV   *dnsbl_site = argv_split(var_psc_dnsbl_sites, ", \t\r\n");
536    char  **cpp;
537
538    /*
539     * Sanity check.
540     */
541    if (dnsbl_site_cache != 0)
542	msg_panic("%s: called more than once", myname);
543
544    /*
545     * pre-compute the DNSBLOG socket name.
546     */
547    psc_dnsbl_service = concatenate(MAIL_CLASS_PRIVATE, "/",
548				    var_dnsblog_service, (char *) 0);
549
550    /*
551     * Prepare for quick iteration when sending out queries to all DNSBL
552     * servers, and for quick lookup when a reply arrives from a specific
553     * DNSBL server.
554     */
555    dnsbl_site_cache = htable_create(13);
556    for (cpp = dnsbl_site->argv; *cpp; cpp++)
557	psc_dnsbl_add_site(*cpp);
558    argv_free(dnsbl_site);
559    dnsbl_site_list = htable_list(dnsbl_site_cache);
560
561    /*
562     * The per-client blocklist score.
563     */
564    dnsbl_score_cache = htable_create(13);
565
566    /*
567     * Space for ad-hoc DNSBLOG server request/reply parameters.
568     */
569    reply_client = vstring_alloc(100);
570    reply_dnsbl = vstring_alloc(100);
571    reply_addr = vstring_alloc(100);
572}
573