summaryrefslogtreecommitdiff
path: root/usr.sbin/smtpd/dns.c
blob: 36bbf407c5d23f722e2049259d4bf61908269b31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*	$OpenBSD: dns.c,v 1.31 2011/03/23 20:38:56 eric Exp $	*/

/*
 * Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org>
 * Copyright (c) 2009 Jacek Masiulaniec <jacekm@dobremiasto.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/queue.h>
#include <sys/tree.h>
#include <sys/stat.h>

#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>

#include <event.h>
#include <imsg.h>
#include <netdb.h>
#include <resolv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "asr.h"
#include "dnsdefs.h"
#include "dnsutil.h"
#include "smtpd.h"
#include "log.h"

struct dnssession *dnssession_init(struct smtpd *, struct dns *);
void	dnssession_destroy(struct smtpd *, struct dnssession *);
void	dnssession_mx_insert(struct dnssession *, struct mx *);
void	dns_asr_handler(int, short, void *);
void	dns_asr_mx_handler(int, short, void *);
void	lookup_host(struct imsgev *, struct dns *, int, int);
void	lookup_mx(struct imsgev *, struct dns *);
void	lookup_ptr(struct imsgev *, struct dns *);

struct asr *asr = NULL;

/*
 * User interface.
 */

void
dns_query_host(struct smtpd *env, char *host, int port, u_int64_t id)
{
	struct dns	 query;

	bzero(&query, sizeof(query));
	strlcpy(query.host, host, sizeof(query.host));
	query.port = port;
	query.id = id;

	imsg_compose_event(env->sc_ievs[PROC_LKA], IMSG_DNS_HOST, 0, 0, -1,
	    &query, sizeof(query));
}

void
dns_query_mx(struct smtpd *env, char *host, int port, u_int64_t id)
{
	struct dns	 query;

	bzero(&query, sizeof(query));
	strlcpy(query.host, host, sizeof(query.host));
	query.port = port;
	query.id = id;

	imsg_compose_event(env->sc_ievs[PROC_LKA], IMSG_DNS_MX, 0, 0, -1, &query,
	    sizeof(query));
}

void
dns_query_ptr(struct smtpd *env, struct sockaddr_storage *ss, u_int64_t id)
{
	struct dns	 query;

	bzero(&query, sizeof(query));
	query.ss = *ss;
	query.id = id;

	if (strlcpy(query.host, ss_to_ptr(ss), sizeof (query.host))
	    >= sizeof (query.host))
		fatalx("dns_query_ptr");

	imsg_compose_event(env->sc_ievs[PROC_LKA], IMSG_DNS_PTR, 0, 0, -1, &query,
	    sizeof(query));
}

/* LKA interface */
void
dns_async(struct smtpd *env, struct imsgev *asker, int type, struct dns *query)
{
	struct dnssession *dnssession;

	if (asr == NULL && (asr = asr_resolver(NULL)) == NULL) {
		log_warnx("dns_async: cannot create resolver");
		goto noasr;
	}

	query->env   = env;
	query->type  = type;
	query->asker = asker;
	dnssession = dnssession_init(env, query);
	
	switch (type) {
	case IMSG_DNS_HOST:
		dnssession->aq = asr_query_host(asr, query->host, AF_UNSPEC);
		break;
	case IMSG_DNS_PTR:
		dnssession->aq = asr_query_dns(asr, T_PTR, C_IN, query->host, 0);
		break;
	case IMSG_DNS_MX:
		dnssession->aq = asr_query_dns(asr, T_MX, C_IN, query->host, 0);
		break;
	default:
		goto err;
	}

	/* query and set up event to handle answer */
	if (dnssession->aq == NULL)
		goto err;
	dns_asr_handler(-1, -1, dnssession);
	return;

err:
	log_debug("dns_async: ASR error while attempting to resolve `%s'",
	    query->host);
	dnssession_destroy(env, dnssession);

noasr:
	query->error = EAI_AGAIN;
	if (type != IMSG_DNS_PTR)
		type = IMSG_DNS_HOST_END;
	imsg_compose_event(asker, type, 0, 0, -1, query, sizeof(*query));
}

void
dns_asr_handler(int fd, short event, void *arg)
{
	struct dnssession *dnssession = arg;
	struct dns *query = &dnssession->query;
	struct smtpd *env = query->env;
	struct packed pack;
	struct header	h;
	struct query	q;
	struct rr rr;
	struct asr_result ar;
	struct timeval tv = { 0, 0 };
	char *p;
	int cnt;
	int ret;

	bzero(&ar, sizeof (ar));

	switch ((ret = asr_run(dnssession->aq, &ar))) {
	case ASR_NEED_READ:
		tv.tv_usec = ar.ar_timeout * 1000;
		event_set(&dnssession->ev, ar.ar_fd, EV_READ,
		    dns_asr_handler, dnssession);
		event_add(&dnssession->ev, &tv);
		return;

	case ASR_NEED_WRITE:
		tv.tv_usec = ar.ar_timeout * 1000;
		event_set(&dnssession->ev, ar.ar_fd, EV_WRITE,
		    dns_asr_handler, dnssession);
		event_add(&dnssession->ev, &tv);
		return;

	case ASR_YIELD:
	case ASR_DONE:
		break;
	}

	query->error = EAI_AGAIN;

	if (ret == ASR_YIELD) {
		free(ar.ar_cname);
		memcpy(&query->ss, &ar.ar_sa.sa, ar.ar_sa.sa.sa_len);
		query->error = 0;
		imsg_compose_event(query->asker, IMSG_DNS_HOST, 0, 0, -1, query,
		    sizeof(*query));
		dns_asr_handler(-1, -1, dnssession);
		return;
	}

	/* ASR_DONE */
	if (ar.ar_err) {
		query->error = ar.ar_err;
		goto err;
	}

	if (query->type == IMSG_DNS_HOST) {
		query->error = 0;
		imsg_compose_event(query->asker, IMSG_DNS_HOST_END, 0, 0, -1,
		    query, sizeof(*query));
		dnssession_destroy(env, dnssession);
		return;
	}

	packed_init(&pack, ar.ar_data, ar.ar_datalen);
	if (unpack_header(&pack, &h) < 0 || unpack_query(&pack, &q) < 0)
		goto err;

	if (h.ancount == 0) {
		if (query->type == IMSG_DNS_MX) {
			/* we were looking for MX and got no answer,
			 * fallback to host.
			 */
			query->type = IMSG_DNS_HOST;
			dnssession->aq = asr_query_host(asr, query->host,
			    AF_UNSPEC);
			if (dnssession->aq == NULL)
				goto err;
			dns_asr_handler(-1, -1, dnssession);
			return;
		}
		query->error = EAI_NONAME;
		goto err;
	}

	if (query->type == IMSG_DNS_PTR) {
		if (h.ancount > 1) {
			log_debug("dns_asr_handler: PTR query returned several answers.");
			log_debug("dns_asr_handler: keeping only first result.");
		}
		if (unpack_rr(&pack, &rr) < 0)
			goto err;

		print_dname(rr.rr.ptr.ptrname, query->host, sizeof (query->host));
		if ((p = strrchr(query->host, '.')) != NULL)
			*p = '\0';
		free(ar.ar_data);
		
		query->error = 0;
		imsg_compose_event(query->asker, IMSG_DNS_PTR, 0, 0, -1, query,
		    sizeof(*query));
		dnssession_destroy(env, dnssession);
		return;
	}

	if (query->type == IMSG_DNS_MX) {
		struct mx mx;
		
		cnt = h.ancount;
		for (; cnt; cnt--) {
			if (unpack_rr(&pack, &rr) < 0)
				goto err;

			print_dname(rr.rr.mx.exchange, mx.host, sizeof (mx.host));
			if ((p = strrchr(mx.host, '.')) != NULL)
				*p = '\0';
			mx.prio =  rr.rr.mx.preference;

			/* sorted insert that will not overflow MAX_MX_COUNT */
			dnssession_mx_insert(dnssession, &mx);
		}
		free(ar.ar_data);
		ar.ar_data = NULL;

		/* The T_MX scenario is a bit trickier than T_PTR and T_A lookups.
		 * Rather than forwarding the answers to the process that queried,
		 * we retrieve a set of MX hosts ... that need to be resolved. The
		 * loop above sorts them by priority, all we have left to do is to
		 * perform T_A lookups on all of them sequentially and provide the
		 * process that queried with the answers.
		 *
		 * To make it easier, we do this in another handler.
		 *
		 * -- gilles@
		 */
		dnssession->mxcurrent = &dnssession->mxarray[0];
		dnssession->aq = asr_query_host(asr,
		    dnssession->mxcurrent->host, AF_UNSPEC);
		if (dnssession->aq == NULL)
			goto err;

		dns_asr_mx_handler(-1, -1, dnssession);
		return;
	}
	return;

err:
	free(ar.ar_data);
	if (query->type != IMSG_DNS_PTR)
		query->type = IMSG_DNS_HOST_END;
	imsg_compose_event(query->asker, query->type, 0, 0, -1, query,
	    sizeof(*query));
	dnssession_destroy(env, dnssession);
}


/* only handle MX requests */
void
dns_asr_mx_handler(int fd, short event, void *arg)
{
	struct dnssession *dnssession = arg;
	struct dns *query = &dnssession->query;
	struct smtpd *env = query->env;
	struct asr_result ar;
	struct timeval tv = { 0, 0 };
	struct mx *lastmx;
	int ret;

	switch ((ret = asr_run(dnssession->aq, &ar))) {
	case ASR_NEED_READ:
		tv.tv_usec = ar.ar_timeout * 1000;
		event_set(&dnssession->ev, ar.ar_fd, EV_READ,
		    dns_asr_mx_handler, dnssession);
		event_add(&dnssession->ev, &tv);
		return;

	case ASR_NEED_WRITE:
		tv.tv_usec = ar.ar_timeout * 1000;
		event_set(&dnssession->ev, ar.ar_fd, EV_WRITE,
		    dns_asr_mx_handler, dnssession);
		event_add(&dnssession->ev, &tv);
		return;

	case ASR_YIELD:
	case ASR_DONE:
		break;
	}

	query->error = EAI_AGAIN;

	if (ret == ASR_YIELD) {
		free(ar.ar_cname);
		memcpy(&query->ss, &ar.ar_sa.sa, ar.ar_sa.sa.sa_len);
		query->error = 0;
		imsg_compose_event(query->asker, IMSG_DNS_HOST, 0, 0, -1, query,
		    sizeof(*query));
		dns_asr_mx_handler(-1, -1, dnssession);
		return;
	}

	/* ASR_DONE */
	if (ar.ar_err) {
		query->error = ar.ar_err;
		goto end;
	}

	lastmx = &dnssession->mxarray[dnssession->mxarraysz - 1];
	if (dnssession->mxcurrent == lastmx) {
		query->error = 0;
		goto end;
	}

	dnssession->mxcurrent++;
	dnssession->aq = asr_query_host(asr, dnssession->mxcurrent->host,
	    AF_UNSPEC);
	if (dnssession->aq == NULL)
		goto end;
	dns_asr_mx_handler(-1, -1, dnssession);
	return;

end:
	imsg_compose_event(query->asker, IMSG_DNS_HOST_END, 0, 0, -1, query,
	    sizeof(*query));
	dnssession_destroy(env, dnssession);
	return;
}

struct dnssession *
dnssession_init(struct smtpd *env, struct dns *query)
{
	struct dnssession *dnssession;

	dnssession = calloc(1, sizeof(struct dnssession));
	if (dnssession == NULL)
		fatal("dnssession_init: calloc");

	dnssession->id = query->id;
	dnssession->query = *query;
	SPLAY_INSERT(dnstree, &env->dns_sessions, dnssession);
	return dnssession;
}

void
dnssession_destroy(struct smtpd *env, struct dnssession *dnssession)
{
	SPLAY_REMOVE(dnstree, &env->dns_sessions, dnssession);
	event_del(&dnssession->ev);
	free(dnssession);
}

void
dnssession_mx_insert(struct dnssession *dnssession, struct mx *mx)
{
        size_t i;
        size_t j;

	if (dnssession->mxarraysz > MAX_MX_COUNT)
		dnssession->mxarraysz = MAX_MX_COUNT;

        if (dnssession->mxarraysz == 0) {
                dnssession->mxarray[0] = *mx;
		dnssession->mxarraysz++;
                return;
        }

        for (i = 0; i < dnssession->mxarraysz; ++i)
                if (mx->prio < dnssession->mxarray[i].prio)
                        goto insert;

        if (i < MAX_MX_COUNT)
                dnssession->mxarray[i] = *mx;
	dnssession->mxarraysz++;
        return;

insert:
        for (j = dnssession->mxarraysz; j > i; --j)
                dnssession->mxarray[j] = dnssession->mxarray[j - 1];
        dnssession->mxarray[i] = *mx;
}

int
dnssession_cmp(struct dnssession *s1, struct dnssession *s2)
{
	/*
	 * do not return u_int64_t's
	 */
	if (s1->id < s2->id)
		return (-1);

	if (s1->id > s2->id)
		return (1);

	return (0);
}

SPLAY_GENERATE(dnstree, dnssession, nodes, dnssession_cmp);