summaryrefslogtreecommitdiff
path: root/usr.sbin/smtpd/proxy.c
blob: 7bf976d89390b9d23c4294d5df5e044f81d7a3a1 (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
/*
 * Copyright (c) 2017 Antoine Kaufmann <toni@famkaufmann.info>
 *
 * 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/tree.h>
#include <sys/un.h>

#include <err.h>
#include <errno.h>
#include <event.h>
#include <imsg.h>
#include <inttypes.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "log.h"
#include "smtpd.h"


/*
 * The PROXYv2 protocol is described here:
 * http://www.haproxy.org/download/1.8/doc/proxy-protocol.txt
 */

#define PROXY_CLOCAL 0x0
#define PROXY_CPROXY 0x1
#define PROXY_AF_UNSPEC 0x0
#define PROXY_AF_INET 0x1
#define PROXY_AF_INET6 0x2
#define PROXY_AF_UNIX 0x3
#define PROXY_TF_UNSPEC 0x0
#define PROXY_TF_STREAM 0x1
#define PROXY_TF_DGRAM 0x2

#define PROXY_SESSION_TIMEOUT	300

static const uint8_t pv2_signature[] = {
	0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D,
	0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A
};

struct proxy_hdr_v2 {
	uint8_t sig[12];
	uint8_t ver_cmd;
	uint8_t fam;
	uint16_t len;
} __attribute__((packed));


struct proxy_addr_ipv4 {
	uint32_t src_addr;
	uint32_t dst_addr;
	uint16_t src_port;
	uint16_t dst_port;
} __attribute__((packed));

struct proxy_addr_ipv6 {
	uint8_t src_addr[16];
	uint8_t dst_addr[16];
	uint16_t src_port;
	uint16_t dst_port;
} __attribute__((packed));

struct proxy_addr_unix {
	uint8_t src_addr[108];
	uint8_t dst_addr[108];
} __attribute__((packed));

union proxy_addr {
	struct proxy_addr_ipv4 ipv4;
	struct proxy_addr_ipv6 ipv6;
	struct proxy_addr_unix un;
} __attribute__((packed));

struct proxy_session {
	struct listener	*l;
	struct io	*io;

	uint64_t	id;
	int		fd;
	uint16_t	header_len;
	uint16_t	header_total;
	uint16_t	addr_len;
	uint16_t	addr_total;

	struct sockaddr_storage	ss;
	struct proxy_hdr_v2	hdr;
	union proxy_addr	addr;

	void (*cb_accepted)(struct listener *, int,
	    const struct sockaddr_storage *, struct io *);
	void (*cb_dropped)(struct listener *, int,
	    const struct sockaddr_storage *);
};

static void proxy_io(struct io *, int, void *);
static void proxy_error(struct proxy_session *, const char *, const char *);
static int proxy_header_validate(struct proxy_session *);
static int proxy_translate_ss(struct proxy_session *);

int
proxy_session(struct listener *listener, int sock,
    const struct sockaddr_storage *ss,
    void (*accepted)(struct listener *, int,
	const struct sockaddr_storage *, struct io *),
    void (*dropped)(struct listener *, int,
	const struct sockaddr_storage *));

int
proxy_session(struct listener *listener, int sock,
    const struct sockaddr_storage *ss,
    void (*accepted)(struct listener *, int,
	const struct sockaddr_storage *, struct io *),
    void (*dropped)(struct listener *, int,
	const struct sockaddr_storage *))
{
	struct proxy_session *s;

	if ((s = calloc(1, sizeof(*s))) == NULL)
		return (-1);

	if ((s->io = io_new()) == NULL) {
		free(s);
		return (-1);
	}

	s->id = generate_uid();
	s->l = listener;
	s->fd = sock;
	s->header_len = 0;
	s->addr_len = 0;
	s->ss = *ss;
	s->cb_accepted = accepted;
	s->cb_dropped = dropped;

	io_set_callback(s->io, proxy_io, s);
	io_set_fd(s->io, sock);
	io_set_timeout(s->io, PROXY_SESSION_TIMEOUT * 1000);
	io_set_read(s->io);

	log_info("%016"PRIx64" smtp event=proxy address=%s",
	    s->id, ss_to_text(&s->ss));

	return 0;
}

static void
proxy_io(struct io *io, int evt, void *arg)
{
	struct proxy_session	*s = arg;
	struct proxy_hdr_v2	*h = &s->hdr;
	uint8_t *buf;
	size_t len, off;

	switch (evt) {

	case IO_DATAIN:
		buf = io_data(io);
		len = io_datalen(io);

		if (s->header_len < sizeof(s->hdr)) {
			/* header is incomplete */
			off = sizeof(s->hdr) - s->header_len;
			off = (len < off ? len : off);
			memcpy((uint8_t *) &s->hdr + s->header_len, buf, off);

			s->header_len += off;
			buf += off;
			len -= off;
			io_drop(s->io, off);

			if (s->header_len < sizeof(s->hdr)) {
				/* header is still not complete */
				return;
			}

			if (proxy_header_validate(s) != 0)
				return;
		}

		if (s->addr_len < s->addr_total) {
			/* address is incomplete */
			off = s->addr_total - s->addr_len;
			off = (len < off ? len : off);
			memcpy((uint8_t *) &s->addr + s->addr_len, buf, off);

			s->header_len += off;
			s->addr_len += off;
			buf += off;
			len -= off;
			io_drop(s->io, off);

			if (s->addr_len < s->addr_total) {
				/* address is still not complete */
				return;
			}
		}

		if (s->header_len < s->header_total) {
			/* additional parameters not complete */
			/* these are ignored for now, but we still need to drop
			 * the bytes from the buffer */
			off = s->header_total - s->header_len;
			off = (len < off ? len : off);

			s->header_len += off;
			io_drop(s->io, off);

			if (s->header_len < s->header_total)
				/* not complete yet */
				return;
		}

		switch(h->ver_cmd & 0xF) {
		case PROXY_CLOCAL:
			/* local address, no need to modify ss */
			break;

		case PROXY_CPROXY:
			if (proxy_translate_ss(s) != 0)
				return;
			break;

		default:
			proxy_error(s, "protocol error", "unknown command");
			return;
		}

		s->cb_accepted(s->l, s->fd, &s->ss, s->io);
		/* we passed off s->io, so it does not need to be freed here */
		free(s);
		break;

	case IO_TIMEOUT:
		proxy_error(s, "timeout", NULL);
		break;

	case IO_DISCONNECTED:
		proxy_error(s, "disconnected", NULL);
		break;

	case IO_ERROR:
		proxy_error(s, "IO error", io_error(io));
		break;

	default:
		fatalx("proxy_io()");
	}

}

static void
proxy_error(struct proxy_session *s, const char *reason, const char *extra)
{
	if (extra)
		log_info("proxy %p event=closed address=%s reason=\"%s (%s)\"",
		    s, ss_to_text(&s->ss), reason, extra);
	else
		log_info("proxy %p event=closed address=%s reason=\"%s\"",
		    s, ss_to_text(&s->ss), reason);

	s->cb_dropped(s->l, s->fd, &s->ss);
	io_free(s->io);
	free(s);
}

static int
proxy_header_validate(struct proxy_session *s)
{
	struct proxy_hdr_v2 *h = &s->hdr;

	if (memcmp(h->sig, pv2_signature,
		sizeof(pv2_signature)) != 0) {
		proxy_error(s, "protocol error", "invalid signature");
		return (-1);
	}

	if ((h->ver_cmd >> 4) != 2) {
		proxy_error(s, "protocol error", "invalid version");
		return (-1);
	}

	switch (h->fam) {
	case (PROXY_AF_UNSPEC << 4 | PROXY_TF_UNSPEC):
		s->addr_total = 0;
		break;

	case (PROXY_AF_INET << 4 | PROXY_TF_STREAM):
		s->addr_total = sizeof(s->addr.ipv4);
		break;

	case (PROXY_AF_INET6 << 4 | PROXY_TF_STREAM):
		s->addr_total = sizeof(s->addr.ipv6);
		break;

	case (PROXY_AF_UNIX << 4 | PROXY_TF_STREAM):
		s->addr_total = sizeof(s->addr.un);
		break;

	default:
		proxy_error(s, "protocol error", "unsupported address family");
		return (-1);
	}

	s->header_total = ntohs(h->len);
	if (s->header_total > UINT16_MAX - sizeof(struct proxy_hdr_v2)) {
		proxy_error(s, "protocol error", "header too long");
		return (-1);
	}
	s->header_total += sizeof(struct proxy_hdr_v2);

	if (s->header_total < sizeof(struct proxy_hdr_v2) + s->addr_total) {
		proxy_error(s, "protocol error", "address info too short");
		return (-1);
	}

	return 0;
}

static int
proxy_translate_ss(struct proxy_session *s)
{
	struct sockaddr_in *sin = (struct sockaddr_in *) &s->ss;
	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &s->ss;
	struct sockaddr_un *sun = (struct sockaddr_un *) &s->ss;
	size_t sun_len;

	switch (s->hdr.fam) {
	case (PROXY_AF_UNSPEC << 4 | PROXY_TF_UNSPEC):
		/* unspec: only supported for local */
		proxy_error(s, "address translation", "UNSPEC family not "
		    "supported for PROXYing");
		return (-1);

	case (PROXY_AF_INET << 4 | PROXY_TF_STREAM):
		memset(&s->ss, 0, sizeof(s->ss));
		sin->sin_family = AF_INET;
		sin->sin_port = s->addr.ipv4.src_port;
		sin->sin_addr.s_addr = s->addr.ipv4.src_addr;
		break;

	case (PROXY_AF_INET6 << 4 | PROXY_TF_STREAM):
		memset(&s->ss, 0, sizeof(s->ss));
		sin6->sin6_family = AF_INET6;
		sin6->sin6_port = s->addr.ipv6.src_port;
		memcpy(sin6->sin6_addr.s6_addr, s->addr.ipv6.src_addr,
		    sizeof(s->addr.ipv6.src_addr));
		break;

	case (PROXY_AF_UNIX << 4 | PROXY_TF_STREAM):
		memset(&s->ss, 0, sizeof(s->ss));
		sun_len = strnlen(s->addr.un.src_addr,
		    sizeof(s->addr.un.src_addr));
		if (sun_len > sizeof(sun->sun_path)) {
			proxy_error(s, "address translation", "Unix socket path"
			    " longer than supported");
			return (-1);
		}
		sun->sun_family = AF_UNIX;
		memcpy(sun->sun_path, s->addr.un.src_addr, sun_len);
		break;

	default:
		fatalx("proxy_translate_ss()");
	}

	return 0;
}