summaryrefslogtreecommitdiffstats
path: root/rfc1035/rfc1035mxlist.c
blob: cd08a111917e8bc852be5d95b6a20f72c0259ca8 (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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
** Copyright 1998 - 2011 Double Precision, Inc.
** See COPYING for distribution information.
*/

#include	"config.h"
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	"rfc1035.h"
#include	"rfc1035mxlist.h"

void rfc1035_mxlist_free(struct rfc1035_mxlist *p)
{
struct rfc1035_mxlist *q;

	while (p)
	{
		q=p->next;
		if (p->hostname)	free(p->hostname);
		free(p);
		p=q;
	}
}

static int addrecord(struct rfc1035_mxlist **list, const char *mxname,
		     int mxpreference,

#if	RFC1035_IPV6
		     struct in6_addr *in,
#else
		     struct in_addr *in,
#endif
		     int ad,
		     int port)
{
#if	RFC1035_IPV6
struct sockaddr_in6 sin;
#else
struct sockaddr_in sin;
#endif
struct rfc1035_mxlist *p;

	if ((p=(struct rfc1035_mxlist *)malloc(sizeof(struct rfc1035_mxlist)))
		== 0 || (p->hostname=malloc(strlen(mxname)+1)) == 0)
	{
		if (p)	free ( (char *)p);
		return (-1);
	}

	memset(&sin, 0, sizeof(sin));

#if	RFC1035_IPV6
	sin.sin6_family=AF_INET6;
	sin.sin6_addr= *in;
	sin.sin6_port=htons(port);
	p->protocol=PF_INET6;
#else
	sin.sin_family=AF_INET;
	sin.sin_addr= *in;
	sin.sin_port=htons(port);
	p->protocol=PF_INET;
#endif

	while ( *list && (*list)->priority < mxpreference )
		list= &(*list)->next;

	p->next=*list;
	*list=p;
	p->ad=ad;
	p->priority=mxpreference;
	strcpy(p->hostname, mxname);
	memcpy(&p->address, &sin, sizeof(sin));
	return (0);
}

static int harvest_records(struct rfc1035_res *res,
	struct rfc1035_mxlist **list,
	struct rfc1035_reply *mxreply,
	int mxpreference,
	char *mxname,
	int q_type, int *found, int autoquery, int port);

#define	HARVEST_AUTOQUERY	1
#define	HARVEST_NODUPE		2

static int add_arecords(struct rfc1035_res *res, struct rfc1035_mxlist **list,
			struct rfc1035_reply *mxreply,
			int mxpreference,
			char *mxname, int port, int opts)
{
#if	RFC1035_IPV6
	struct in6_addr	in;
	int rc2;
	int first_a=RFC1035_TYPE_A;
	int second_a=RFC1035_TYPE_AAAA;
	const char *prefer_ipv6=getenv("ESMTP_PREFER_IPV6_MX");
#else
	struct in_addr	in;
#endif
	int	found=0;
	int	rc;

	if (rfc1035_aton(mxname, &in) == 0)
	{	/* Broken MX record */
	char	buf[RFC1035_NTOABUFSIZE];

		rfc1035_ntoa(&in, buf);

		if (addrecord(list, buf, mxpreference, &in, 0, port))
			return (RFC1035_MX_INTERNAL);

		return (RFC1035_MX_OK);
	}

#if	RFC1035_IPV6

/*
	Here's the IPv6 strategy:

If we have an existing MX record to work with, try to harvest
both A and AAAA addresses from it.  If we find either an A or an AAAA
record, stop.

If we don't have an existing MX record, or we didn't find A or AAAA
records, then query for A records.  Query for AAAA records only if A
records weren't found.

*/
	if (prefer_ipv6 && *prefer_ipv6 && *prefer_ipv6 != '0')
	{
		first_a=RFC1035_TYPE_AAAA;
		second_a=RFC1035_TYPE_A;
	}

	if (mxreply && !(opts & RFC1035_MX_QUERYALL))
	{
		rc2=harvest_records(res, list, mxreply, mxpreference,
				    mxname, second_a, &found, HARVEST_NODUPE,
				    port);

		if (rc2 != RFC1035_MX_OK && rc2 != RFC1035_MX_SOFTERR)
			return rc2;

		rc=harvest_records(res, list, mxreply, mxpreference,
				   mxname, first_a, &found, 0, port);

		if (rc != RFC1035_MX_OK && rc != RFC1035_MX_SOFTERR)
			return (rc);

		if (rc == RFC1035_MX_SOFTERR && rc2 == RFC1035_MX_SOFTERR)
			return rc;

		if (found)	return (RFC1035_MX_OK);
	}

	rc2=harvest_records(res, list, mxreply, mxpreference, mxname,
			    second_a, &found, HARVEST_AUTOQUERY|HARVEST_NODUPE,
			    port);

	if (rc2 != RFC1035_MX_OK && rc2 != RFC1035_MX_SOFTERR)
		return rc2;

	rc=harvest_records(res, list, mxreply, mxpreference, mxname,
			   first_a, &found, HARVEST_AUTOQUERY, port);

	if (rc != RFC1035_MX_OK && rc != RFC1035_MX_SOFTERR)
		return (rc);

	if (rc == RFC1035_MX_SOFTERR && rc2 == RFC1035_MX_SOFTERR)
		return rc;

	if (found)	return (RFC1035_MX_OK);

#else
	if ((rc=harvest_records(res, list, mxreply, mxpreference, mxname,
		RFC1035_TYPE_A, &found, HARVEST_AUTOQUERY, port))
			!= RFC1035_MX_OK)
		return (rc);
#endif

	if (!found)	return (RFC1035_MX_HARDERR);
	return (RFC1035_MX_OK);
}

static int harvest_records(struct rfc1035_res *res,
	struct rfc1035_mxlist **list,
	struct rfc1035_reply *mxreply,
	int mxpreference,
	char *mxname,
	int q_type, int *found,
	int flags, int port)
{
	char	lookup_name[RFC1035_MAXNAMESIZE+1];

	struct rfc1035_reply *areply=0;
	int index;
#if	RFC1035_IPV6
	struct in6_addr in;
#else
	struct in_addr in;
#endif

	/*
	** Make a copy of mxname, because resolve_cname modifies it.
	** That is rather rude, since harvest_records gets called multiple
	** times.
	**
	** We still need to know what resolve_cname() did, since
	** after resolve_cname() we call replysearch_all(), which needs to
	** have the same hostname.
	**
	** mxname always points to a char[RFC1035_MAXNAMESIZE_1], so what's
	** good for the goose is good for the gander.
	*/

	strcpy(lookup_name, mxname);

	index= -1;

	if (!mxreply || (
		((index=rfc1035_replysearch_all( res, mxreply, lookup_name,
					q_type,
					RFC1035_CLASS_IN,
					0)) < 0 ||
			mxreply->allrrs[index]->rrtype != q_type)
		&& (flags & HARVEST_AUTOQUERY))
		)
	{
		index=rfc1035_resolve_cname(res, lookup_name,
			q_type,
			RFC1035_CLASS_IN, &areply, RFC1035_X_RANDOMIZE);
		if (index < 0)
		{
			if (!areply)
			{
				if (index == RFC1035_ERR_CNAME_RECURSIVE)
					return (RFC1035_MX_BADDNS);
				return (RFC1035_MX_INTERNAL);
			}

			if (areply->rcode == RFC1035_RCODE_NXDOMAIN ||
				areply->rcode == RFC1035_RCODE_NOERROR)
			{
				rfc1035_replyfree(areply);
				return (RFC1035_MX_OK);
			}
			rfc1035_replyfree(areply);
			return (RFC1035_MX_SOFTERR);
		}
		mxreply=areply;
	}

	for ( ; index >= 0 ;
			index=rfc1035_replysearch_all( res, mxreply,
						       lookup_name,
						       q_type,
						       RFC1035_CLASS_IN,
						       index+1))
	{
		if (mxreply->allrrs[index]->rrtype != q_type)
			continue;

#if RFC1035_IPV6
		if (q_type == RFC1035_TYPE_A)
		{
		struct rfc1035_mxlist *q;

			/* Map it to an IPv4 address */

			rfc1035_ipv4to6(&in,
				&mxreply->allrrs[index]->rr.inaddr);

			/* See if it's already here */

			for (q= *list; q; q=q->next)
			{
			struct sockaddr_in6 sin6;

				if (q->protocol != PF_INET6)
					continue;
				memcpy(&sin6, &q->address, sizeof(sin6));

				if (memcmp(&sin6.sin6_addr, &in, sizeof(in))
					== 0 && q->priority == mxpreference)
					break;
			}
			if ((flags & HARVEST_NODUPE) && q)	continue;
		}
		else
			in=mxreply->allrrs[index]->rr.in6addr;
#else
		in.s_addr=mxreply->allrrs[index]->rr.inaddr.s_addr;
#endif
		*found=1;
		if (addrecord(list, mxname, mxpreference, &in, mxreply->ad,
			      port))
		{
			if (areply)
				rfc1035_replyfree(areply);
			return (RFC1035_MX_INTERNAL);
		}
	}
	if (areply)
		rfc1035_replyfree(areply);

	return (RFC1035_MX_OK);
}

static int domxlistcreate(struct rfc1035_res *res,
			  const char *q_name, int opts,
			  struct rfc1035_mxlist **list, int port)
{
char	namebuf[RFC1035_MAXNAMESIZE+1];
struct	rfc1035_reply *replyp;
int	index;
RFC1035_ADDR	in;
int seen_softerr=0;
int seen_good=0;

	*list=0;
	if (rfc1035_aton(q_name, &in) == 0)
		return (RFC1035_MX_HARDERR);	/* Don't gimme an IP address */

	namebuf[0]=0;
	strncat(namebuf, q_name, RFC1035_MAXNAMESIZE);
	if (namebuf[0] == '[')
	{
	char	*q=strchr(namebuf, ']');

		if (!q || q[1])	return (RFC1035_MX_HARDERR);	/* Bad addr */
		*q=0;
		if (rfc1035_aton(namebuf+1, &in))
			return (RFC1035_MX_HARDERR);

		if (addrecord(list, q_name, -1, &in, 0, port))
			return (RFC1035_MX_INTERNAL);
		return (RFC1035_MX_OK);
	}

	index=rfc1035_resolve_cname(res, namebuf,
		RFC1035_TYPE_MX,
		RFC1035_CLASS_IN, &replyp, RFC1035_X_RANDOMIZE);

	if (index < 0)
	{
		if (!replyp)
		{
			if (index == RFC1035_ERR_CNAME_RECURSIVE)
				return (RFC1035_MX_BADDNS);
			return (RFC1035_MX_INTERNAL);
		}

		if (replyp->rcode == RFC1035_RCODE_NXDOMAIN ||
			replyp->rcode == RFC1035_RCODE_NOERROR)
		{
			rfc1035_replyfree(replyp);
			strcpy(namebuf, q_name);

			if (opts & RFC1035_MX_AFALLBACK)
				return (add_arecords(res, list, 0, -1,
						     namebuf, port, opts));
			return RFC1035_MX_HARDERR;
		}

		rfc1035_replyfree(replyp);
		return (RFC1035_MX_SOFTERR);
	}

	for ( ; index >= 0;
			index=rfc1035_replysearch_all( res, replyp, namebuf,
					RFC1035_TYPE_MX,
					RFC1035_CLASS_IN,
					index+1))
	{
	char	mxname[RFC1035_MAXNAMESIZE+1];

		if (replyp->allrrs[index]->rrtype != RFC1035_TYPE_MX)
			continue;

		if (rfc1035_replyhostname(replyp,
			replyp->allrrs[index]->rr.mx.mx_label, mxname) == 0)
			continue;

		if (*mxname == 0) /* MX . */
		{
			rfc1035_replyfree(replyp);
			return (RFC1035_MX_NONE);
		}

		switch (add_arecords(res, list, replyp,
				     replyp->allrrs[index]->rr.mx.preference,
				     mxname,
				     port, opts)) {
		case	RFC1035_MX_SOFTERR:
			seen_softerr=1;
			continue;
		case	RFC1035_MX_INTERNAL:
			rfc1035_replyfree(replyp);
			return (RFC1035_MX_INTERNAL);
		case	RFC1035_MX_BADDNS:
			rfc1035_replyfree(replyp);
			return (RFC1035_MX_BADDNS);
		default:
			seen_good=1;
			continue;
		}
	}

	rfc1035_replyfree(replyp);

	if (seen_good && (opts & RFC1035_MX_IGNORESOFTERR))
		seen_softerr=0;
		/* At least some A records were probably fetched */

	if (seen_softerr)
		return (RFC1035_MX_SOFTERR);

	if (*list)	return (RFC1035_MX_OK);
	return (RFC1035_MX_HARDERR);
}

static int domxlistcreate2(struct rfc1035_res *res,
			   const char *q_name,
			   int opts,
			   struct rfc1035_mxlist **list,
			   int port)
{
char	*buf;
int	rc;

	if (strchr(q_name, '.') || strchr(q_name, ':') ||
		!res->rfc1035_defaultdomain)
		return (domxlistcreate(res, q_name, opts, list, port));

	if ((buf=malloc(strlen(q_name)+
		strlen(res->rfc1035_defaultdomain)+2)) == 0)
		return (-1);

	strcat(strcat(strcpy(buf, q_name), "."), res->rfc1035_defaultdomain);

	rc=domxlistcreate(res, buf, opts, list, port);

	free(buf);
	return (rc);
}

static int domxlistcreate3(struct rfc1035_res *res,
			   const char *q_name,
			   int opts,
			   struct rfc1035_mxlist **list)
{
char	*buf;
int	rc;
const char *p;

	p=strchr(q_name, ',');

	if (p == 0)	return (domxlistcreate2(res, q_name, opts, list, 25));

	if ((buf=malloc(p-q_name+1)) == 0)
		return (-1);

	memcpy(buf, q_name, p-q_name);
	buf[p-q_name]=0;

	rc=domxlistcreate2(res, buf, opts, list, atoi(p+1));

	free(buf);
	return (rc);
}

int rfc1035_mxlist_create_x(struct rfc1035_res *res,
			    const char *q_name,
			    int opts,
			    struct rfc1035_mxlist **list)
{
int	rc=domxlistcreate3(res, q_name, opts, list);

	if (rc != RFC1035_MX_OK)
	{
		rfc1035_mxlist_free(*list);
		*list=0;
	}
	return (rc);
}

int rfc1035_mxlist_create(struct rfc1035_res *res,
			  const char *q_name,
			  struct rfc1035_mxlist **list)
{
	return rfc1035_mxlist_create_x(res, q_name,
				       RFC1035_MX_AFALLBACK |
				       RFC1035_MX_IGNORESOFTERR,
				       list);
}