summaryrefslogtreecommitdiff
path: root/sys/src/cmd/upas/smtp/mxdial.c
blob: 7a707c509990fa6e124b4d74451695a94230a4af (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
#include "common.h"
#include "smtp.h"
#include <ndb.h>

char	*bustedmxs[Maxbustedmx];

static void
expand(DS *ds)
{
	char *s;
	Ndbtuple *t;

	s = ds->host + 1;
	t = csipinfo(ds->netdir, "sys", sysname(), &s, 1);
	if(t != nil){
		strecpy(ds->expand, ds->expand+sizeof ds->expand, t->val);
		ds->host = ds->expand;
	}
	ndbfree(t);
}

/* break up an address to its component parts */
void
dialstringparse(char *str, DS *ds)
{
	char *p, *p2;

	strecpy(ds->buf, ds->buf + sizeof ds->buf, str);
	p = strchr(ds->buf, '!');
	if(p == 0) {
		ds->netdir = 0;
		ds->proto = "net";
		ds->host = ds->buf;
	} else {
		if(*ds->buf != '/'){
			ds->netdir = 0;
			ds->proto = ds->buf;
		} else {
			for(p2 = p; *p2 != '/'; p2--)
				;
			*p2++ = 0;
			ds->netdir = ds->buf;
			ds->proto = p2;
		}
		*p = 0;
		ds->host = p + 1;
	}
	ds->service = strchr(ds->host, '!');
	if(ds->service)
		*ds->service++ = 0;
	else
		ds->service = "smtp";
	if(*ds->host == '$')
		expand(ds);
}

void
mxtabfree(Mxtab *mx)
{
	free(mx->mx);
	memset(mx, 0, sizeof *mx);
}

static void
mxtabrealloc(Mxtab *mx)
{
	if(mx->nmx < mx->amx)
		return;
	if(mx->amx == 0)
		mx->amx = 1;
	mx->amx <<= 1;
	mx->mx = realloc(mx->mx, sizeof mx->mx[0] * mx->amx);
	if(mx->mx == nil)
		sysfatal("no memory for mx");
}

static void
mxtabadd(Mxtab *mx, char *host, char *ip, char *net, int pref)
{
	int i;
	Mx *x;

	mxtabrealloc(mx);
	x = mx->mx;
	for(i = mx->nmx; i>0 && x[i-1].pref>pref && x[i-1].netdir == net; i--)
		x[i] = x[i-1];
	strecpy(x[i].host, x[i].host + sizeof x[i].host, host);
	if(ip != nil)
		strecpy(x[i].ip, x[i].ip + sizeof x[i].ip, ip);
	else
		x[i].ip[0] = 0;
	x[i].netdir = net;
	x[i].pref = pref;
	x[i].valid = 1;
	mx->nmx++;
}

static int
timeout(void*, char *msg)
{
	if(strstr(msg, "alarm"))
		return 1;
	return 0;
}

static long
timedwrite(int fd, void *buf, long len, long ms)
{
	long n, oalarm;

	atnotify(timeout, 1);
	oalarm = alarm(ms);
	n = pwrite(fd, buf, len, 0);
	alarm(oalarm);
	atnotify(timeout, 0);
	return n;
}

static int
dnslookup(Mxtab *mx, int fd, char *query, char *domain, char *net, int pref0)
{
	int n;
	char buf[1024], *f[4];

	n = timedwrite(fd, query, strlen(query), 60*1000);
	if(n < 0){
		rerrstr(buf, sizeof buf);
		dprint("dns: %s\n", buf);
		if(strstr(buf, "dns failure")){
			/* if dns fails for the mx lookup, we have to stop */
			close(fd);
			return -1;
		}
		return 0;
	}

	seek(fd, 0, 0);
	for(;;){
		if((n = read(fd, buf, sizeof buf - 1)) < 1)
			break;
		buf[n] = 0;
	//	chat("dns: %s\n", buf);
		n = tokenize(buf, f, nelem(f));
		if(n < 2)
			continue;
		if(strcmp(f[1], "mx") == 0 && n == 4){
			if(strchr(domain, '.') == 0)
				strcpy(domain, f[0]);
			mxtabadd(mx, f[3], nil, net, atoi(f[2]));
		}
		else if (strcmp(f[1], "ip") == 0 && n == 3){
			if(strchr(domain, '.') == 0)
				strcpy(domain, f[0]);
			mxtabadd(mx, f[0], f[2], net, pref0);
		}
	}

	return 0;
}

static int
busted(char *mx)
{
	char **bmp;

	for (bmp = bustedmxs; *bmp != nil; bmp++)
		if (strcmp(mx, *bmp) == 0)
			return 1;
	return 0;
}

static void
complain(Mxtab *mx, char *domain)
{
	char buf[1024], *e, *p;
	int i;

	p = buf;
	e = buf + sizeof buf;
	for(i = 0; i < mx->nmx; i++)
		p = seprint(p, e, "%s ", mx->mx[i].ip);
	syslog(0, "smtpd.mx", "loopback for %s %s", domain, buf);
}

static int
okaymx(Mxtab *mx, char *domain)
{
	int i;
	Mx *x;

	/* look for malicious dns entries; TODO use badcidr in ../spf/ to catch more than ip4 */
	for(i = 0; i < mx->nmx; i++){
		x = mx->mx + i;
		if(x->valid && strcmp(x->ip, "127.0.0.1") == 0){
			dprint("illegal: domain %s lists 127.0.0.1 as mail server", domain);
			complain(mx, domain);
			werrstr("illegal: domain %s lists 127.0.0.1 as mail server", domain);
			return -1;
		}
		if(x->valid && busted(x->host)){
			dprint("lookup: skipping busted mx %s\n", x->host);
			x->valid = 0;
		}
	}
	return 0;
}

static int
lookup(Mxtab *mx, char *net, char *host, char *domain, char *type)
{
	char dns[128], buf[1024];
	int fd, i;
	Mx *x;

	snprint(dns, sizeof dns, "%s/dns", net);
	fd = open(dns, ORDWR);
	if(fd == -1)
		return -1;

	snprint(buf, sizeof buf, "%s %s", host, type);
	dprint("sending %s '%s'\n", dns, buf);
	dnslookup(mx, fd, buf, domain, net, 10000);

	for(i = 0; i < mx->nmx; i++){
		x = mx->mx + i;
		if(x->ip[0] != 0)
			continue;
		x->valid = 0;

		snprint(buf, sizeof buf, "%s %s", x->host, "ip");
		dprint("sending %s '%s'\n", dns, buf);
		dnslookup(mx, fd, buf, domain, net, x->pref);
	}

	close(fd);

	if(strcmp(type, "mx") == 0){
		if(okaymx(mx, domain) == -1)
			return -1;
		for(i = 0; i < mx->nmx; i++){
			x = mx->mx + i;
			dprint("mx list: %s	%d	%s\n", x->host, x->pref, x->ip);
		}
		dprint("\n");
	}

	return 0;
}

static int
lookcall(Mxtab *mx, DS *d, char *domain, char *type)
{
	char buf[1024];
	int i;
	Mx *x;

	if(lookup(mx, d->netdir, d->host, domain, type) == -1){
		for(i = 0; i < mx->nmx; i++)
			if(mx->mx[i].netdir == d->netdir)
				mx->mx[i].valid = 0;
		return -1;
	}

	for(i = 0; i < mx->nmx; i++){
		x = mx->mx + i;
		if(x->ip[0] == 0 || x->valid == 0){
			x->valid = 0;
			continue;
		}
		snprint(buf, sizeof buf, "%s/%s!%s!%s", d->netdir, d->proto,
			x->ip /*x->host*/, d->service);
		dprint("mxdial trying %s	[%s]\n", x->host, buf);
		atnotify(timeout, 1);
		alarm(10*1000);
		mx->fd = dial(buf, 0, 0, 0);
		alarm(0);
		atnotify(timeout, 0);
		if(mx->fd >= 0){
			mx->pmx = i;
			return mx->fd;
		}
		dprint("	failed %r\n");
		x->valid = 0;
	}

	return -1;
}

int
mxdial0(char *addr, char *ddomain, char *gdomain, Mxtab *mx)
{
	int nd, i, j;
	DS *d;
	static char *tab[] = {"mx", "ip", };

	dprint("mxdial(%s, %s, %s, mx)\n", addr, ddomain, gdomain);
	memset(mx, 0, sizeof *mx);
	d = mx->ds;
	dialstringparse(addr, d + 0);
	nd = 1;
	if(d[0].netdir == nil){
		d[1] = d[0];
		d[0].netdir = "/net";
		d[1].netdir = "/net.alt";
		nd = 2;
	}

	/* search all networks for mx records; then ip records */
	for(j = 0; j < nelem(tab); j++)
		for(i = 0; i < nd; i++)
			if(lookcall(mx, d + i, ddomain, tab[j]) != -1)
				return mx->fd;

	/* grotty: try gateway machine by ip only (fixme: try cs lookup) */
	if(gdomain != nil){
		dialstringparse(netmkaddr(gdomain, 0, "smtp"), d + 0);
		if(lookcall(mx, d + 0, gdomain, "ip") != -1)
			return mx->fd;
	}

	return -1;
}

int
mxdial(char *addr, char *ddomain, char *gdomain, Mx *x)
{
	int fd;
	Mxtab mx;

	memset(x, 0, sizeof *x);
	fd = mxdial0(addr, ddomain, gdomain, &mx);
	if(fd >= 0 && mx.pmx >= 0)
		*x = mx.mx[mx.pmx];
	mxtabfree(&mx);
	return fd;
}