summaryrefslogtreecommitdiffstats
path: root/src/director/login-connection.c
blob: 78de3b9372a87cecd87afa2e18bd6bdae3ed4c2e (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
/* Copyright (c) 2010-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "ioloop.h"
#include "net.h"
#include "str.h"
#include "istream.h"
#include "ostream.h"
#include "llist.h"
#include "strescape.h"
#include "master-service.h"
#include "director.h"
#include "director-request.h"
#include "mail-host.h"
#include "auth-client-interface.h"
#include "auth-connection.h"
#include "login-connection.h"

#include <unistd.h>

#define AUTHREPLY_PROTOCOL_MAJOR_VERSION 1
#define AUTHREPLY_PROTOCOL_MINOR_VERSION 0

struct login_connection {
	struct login_connection *prev, *next;

	int refcount;
	enum login_connection_type type;

	int fd;
	struct io *io;
	struct istream *input;
	struct ostream *output;
	struct auth_connection *auth;
	struct director *dir;

	bool handshaked:1;
	bool destroyed:1;
};

struct login_host_request {
	struct login_connection *conn;
	char *line, *username;

	struct ip_addr local_ip;
	in_port_t local_port;
	in_port_t dest_port;
	bool director_proxy_maybe;
};

static struct login_connection *login_connections;

static void auth_input_line(const char *line, void *context);
static void login_connection_unref(struct login_connection **_conn);

static void login_connection_input(struct login_connection *conn)
{
	struct ostream *output;
	unsigned char buf[4096];
	ssize_t ret;

	ret = read(conn->fd, buf, sizeof(buf));
	if (ret <= 0) {
		if (ret < 0) {
			if (errno == EAGAIN)
				return;
			if (errno != ECONNRESET)
				e_error(conn->dir->event, "read(login connection) failed: %m");
		}
		login_connection_deinit(&conn);
		return;
	}
	output = auth_connection_get_output(conn->auth);
	o_stream_nsend(output, buf, ret);
}

static void login_connection_authreply_input(struct login_connection *conn)
{
	bool bail = FALSE;
	const char *line;

	while (!bail && (line = i_stream_read_next_line(conn->input)) != NULL) T_BEGIN {
		if (!conn->handshaked) {
			if (!version_string_verify(line, "director-authreply-client",
						   AUTHREPLY_PROTOCOL_MAJOR_VERSION)) {
				e_error(conn->dir->event, "authreply client sent invalid handshake: %s", line);
				login_connection_deinit(&conn);
				bail = TRUE; /* don't return from within a T_BEGIN {...} T_END */
			} else {
				conn->handshaked = TRUE;
			}
		} else {
			auth_input_line(line, conn);
		}
	} T_END;

	if (bail)
		return;

	if (conn->input->eof) {
		if (conn->input->stream_errno != 0 &&
		    conn->input->stream_errno != ECONNRESET) {
			e_error(conn->dir->event, "read(authreply connection) failed: %s",
				i_stream_get_error(conn->input));
		}
		login_connection_deinit(&conn);
	}
}

static void
login_connection_send_line(struct login_connection *conn, const char *line)
{
	struct const_iovec iov[2];

	if (conn->destroyed)
		return;

	iov[0].iov_base = line;
	iov[0].iov_len = strlen(line);
	iov[1].iov_base = "\n";
	iov[1].iov_len = 1;
	o_stream_nsendv(conn->output, iov, N_ELEMENTS(iov));
}

static bool login_host_request_is_self(struct login_host_request *request,
				       const struct ip_addr *dest_ip)
{
	if (!net_ip_compare(dest_ip, &request->local_ip))
		return FALSE;
	if (request->dest_port != 0 && request->local_port != 0 &&
	    request->dest_port != request->local_port)
		return FALSE;
	return TRUE;
}

static void
login_host_callback(const struct mail_host *host, const char *hostname,
		    const char *errormsg, void *context)
{
	struct login_host_request *request = context;
	struct director *dir = request->conn->dir;
	const char *line, *line_params;
	unsigned int secs;

	if (host == NULL) {
		if (str_begins(request->line, "OK\t"))
			line_params = request->line + 3;
		else if (str_begins(request->line, "PASS\t"))
			line_params = request->line + 5;
		else
			i_panic("BUG: Unexpected line: %s", request->line);

		e_error(dir->event, "director: User %s host lookup failed: %s",
			request->username, errormsg);
		line = t_strconcat("FAIL\t", t_strcut(line_params, '\t'),
				   "\tcode="AUTH_CLIENT_FAIL_CODE_TEMPFAIL, NULL);
	} else if (request->director_proxy_maybe &&
		   login_host_request_is_self(request, &host->ip)) {
		line = request->line;
	} else {
		string_t *str = t_str_new(64);
		char secs_buf[MAX_INT_STRLEN];

		secs = dir->set->director_user_expire / 2;
		str_append(str, request->line);
		str_append(str, "\tproxy_refresh=");
		str_append(str, dec2str_buf(secs_buf, secs));
		str_append(str, "\thost=");
		if (hostname == NULL || hostname[0] == '\0')
			str_append(str, host->ip_str);
		else {
			str_append(str, hostname);
			str_append(str, "\thostip=");
			str_append(str, host->ip_str);
		}
		line = str_c(str);
	}
	login_connection_send_line(request->conn, line);

	login_connection_unref(&request->conn);
	i_free(request->username);
	i_free(request->line);
	i_free(request);
}

static void auth_input_line(const char *line, void *context)
{
	struct login_connection *conn = context;
	struct login_host_request *request, temp_request;
	const char *const *args, *line_params, *username = NULL, *tag = "";
	bool proxy = FALSE, host = FALSE;

	if (line == NULL) {
		/* auth connection died -> kill also this login connection */
		login_connection_deinit(&conn);
		return;
	}
	if (conn->type != LOGIN_CONNECTION_TYPE_USERDB &&
	    str_begins(line, "OK\t"))
		line_params = line + 3;
	else if (conn->type == LOGIN_CONNECTION_TYPE_USERDB &&
		 str_begins(line, "PASS\t"))
		line_params = line + 5;
	else {
		login_connection_send_line(conn, line);
		return;
	}

	/* OK <id> [<parameters>] */
	args = t_strsplit_tabescaped(line_params);
	if (*args != NULL) {
		/* we should always get here, but in case we don't just
		   forward as-is and let login process handle the error. */
		args++;
	}

	i_zero(&temp_request);
	for (; *args != NULL; args++) {
		if (str_begins(*args, "proxy") &&
		    ((*args)[5] == '=' || (*args)[5] == '\0'))
			proxy = TRUE;
		else if (str_begins(*args, "host="))
			host = TRUE;
		else if (str_begins(*args, "lip=")) {
			if (net_addr2ip((*args) + 4, &temp_request.local_ip) < 0)
				e_error(conn->dir->event, "auth sent invalid lip field: %s", (*args) + 6);
		} else if (str_begins(*args, "lport=")) {
			if (net_str2port((*args) + 6, &temp_request.local_port) < 0)
				e_error(conn->dir->event, "auth sent invalid lport field: %s", (*args) + 6);
		} else if (str_begins(*args, "port=")) {
			if (net_str2port((*args) + 5, &temp_request.dest_port) < 0)
				e_error(conn->dir->event, "auth sent invalid port field: %s", (*args) + 6);
		} else if (str_begins(*args, "destuser="))
			username = *args + 9;
		else if (str_begins(*args, "director_tag="))
			tag = *args + 13;
		else if (str_begins(*args, "director_proxy_maybe") &&
			 ((*args)[20] == '=' || (*args)[20] == '\0'))
			temp_request.director_proxy_maybe = TRUE;
		else if (str_begins(*args, "user=")) {
			if (username == NULL)
				username = *args + 5;
		}
	}
	if ((!proxy && !temp_request.director_proxy_maybe) ||
	    host || username == NULL) {
		login_connection_send_line(conn, line);
		return;
	}
	if (*conn->dir->set->master_user_separator != '\0') {
		/* with master user logins we still want to use only the
		   login username */
		username = t_strcut(username,
				    *conn->dir->set->master_user_separator);
	}

	/* we need to add the host. the lookup might be asynchronous */
	request = i_new(struct login_host_request, 1);
	*request = temp_request;
	request->conn = conn;
	request->line = i_strdup(line);
	request->username = i_strdup(username);

	conn->refcount++;
	director_request(conn->dir, username, tag, login_host_callback, request);
}

struct login_connection *
login_connection_init(struct director *dir, int fd,
		      struct auth_connection *auth,
		      enum login_connection_type type)
{
	struct login_connection *conn;

	conn = i_new(struct login_connection, 1);
	conn->refcount = 1;
	conn->fd = fd;
	conn->dir = dir;
	conn->output = o_stream_create_fd(conn->fd, SIZE_MAX);
	o_stream_set_no_error_handling(conn->output, TRUE);
	if (type != LOGIN_CONNECTION_TYPE_AUTHREPLY) {
		i_assert(auth != NULL);
		conn->auth = auth;
		conn->io = io_add(conn->fd, IO_READ,
				  login_connection_input, conn);
		auth_connection_set_callback(conn->auth, auth_input_line, conn);
	} else {
		i_assert(auth == NULL);
		conn->input = i_stream_create_fd(conn->fd, IO_BLOCK_SIZE);
		conn->io = io_add(conn->fd, IO_READ,
				  login_connection_authreply_input, conn);
		o_stream_nsend_str(conn->output, t_strdup_printf(
			"VERSION\tdirector-authreply-server\t%d\t%d\n",
			AUTHREPLY_PROTOCOL_MAJOR_VERSION,
			AUTHREPLY_PROTOCOL_MINOR_VERSION));
	}
	conn->type = type;

	DLLIST_PREPEND(&login_connections, conn);
	return conn;
}

void login_connection_deinit(struct login_connection **_conn)
{
	struct login_connection *conn = *_conn;

	*_conn = NULL;

	if (conn->destroyed)
		return;
	conn->destroyed = TRUE;

	DLLIST_REMOVE(&login_connections, conn);
	io_remove(&conn->io);
	i_stream_destroy(&conn->input);
	o_stream_destroy(&conn->output);
	if (close(conn->fd) < 0)
		e_error(conn->dir->event, "close(login connection) failed: %m");
	conn->fd = -1;

	if (conn->auth != NULL)
		auth_connection_deinit(&conn->auth);
	login_connection_unref(&conn);

	master_service_client_connection_destroyed(master_service);
}

static void login_connection_unref(struct login_connection **_conn)
{
	struct login_connection *conn = *_conn;

	*_conn = NULL;

	i_assert(conn->refcount > 0);
	if (--conn->refcount == 0)
		i_free(conn);
}

void login_connections_deinit(void)
{
	while (login_connections != NULL) {
		struct login_connection *conn = login_connections;

		login_connection_deinit(&conn);
	}
}