File: client-authenticate.c

package info (click to toggle)
dovecot 1%3A1.2.15-7
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 30,252 kB
  • ctags: 19,837
  • sloc: ansic: 191,438; sh: 21,091; makefile: 3,330; cpp: 526; perl: 108; xml: 44
file content (425 lines) | stat: -rw-r--r-- 11,174 bytes parent folder | download | duplicates (2)
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
/* Copyright (c) 2002-2010 Dovecot Sieve authors, see the included COPYING file
 */

#include "common.h"
#include "base64.h"
#include "buffer.h"
#include "ioloop.h"
#include "istream.h"
#include "ostream.h"
#include "safe-memset.h"
#include "str.h"
#include "str-sanitize.h"

#include "managesieve-parser.h"
#include "managesieve-quote.h"
#include "auth-client.h"
#include "client.h"
#include "client-authenticate.h"
#include "managesieve-proxy.h"

#include <unistd.h>
#include <stdlib.h>

#define AUTH_FAILURE_DELAY_INCREASE_MSECS 5000

#define MANAGESIEVE_SERVICE_NAME "sieve"

/* FIXME: The use of the ANONYMOUS mechanism is currently denied
 */
static bool _sasl_mechanism_acceptable
	(const struct auth_mech_desc *mech, bool secured) {

	/* a) transport is secured
	   b) auth mechanism isn't plaintext
       c) we allow insecure authentication
	 */

	if ((mech->flags & MECH_SEC_PRIVATE) == 0 &&
		(mech->flags & MECH_SEC_ANONYMOUS) == 0 &&
		(secured || !disable_plaintext_auth ||
		(mech->flags & MECH_SEC_PLAINTEXT) == 0)) {
		return 1;
	}

	return 0;
}

const char *client_authenticate_get_capabilities(bool secured)
{
	const struct auth_mech_desc *mech;
	unsigned int i, count;
	string_t *str;

	str = t_str_new(128);
	mech = auth_client_get_available_mechs(auth_client, &count);

	if ( count > 0 ) {
		if ( _sasl_mechanism_acceptable(&(mech[0]), secured) ) {
			str_append(str, mech[0].name);
		}

		for (i = 1; i < count; i++) {
			if ( _sasl_mechanism_acceptable(&(mech[i]), secured) ) {
				str_append_c(str, ' ');
				str_append(str, mech[i].name);
			}
		}
	}

	return str_c(str);
}

static void client_auth_input(struct managesieve_client *client)
{
	struct managesieve_arg *args;
	const char *msg;
	char *line;
	bool fatal;

	if (!client_read(client))
		return;

	if (client->skip_line) {
		if (i_stream_next_line(client->common.input) == NULL)
			return;

		client->skip_line = FALSE;
	}

	switch (managesieve_parser_read_args(client->parser, 0, 0, &args)) {
	case -1:
		/* error */
		msg = managesieve_parser_get_error(client->parser, &fatal);
		if (fatal) {
			/* FIXME: What to do? */
		}

		sasl_server_auth_failed(&client->common, msg);
		return;
	case -2:
		/* not enough data */
		return;
	}

	client->skip_line = TRUE;

	if (args[0].type != MANAGESIEVE_ARG_STRING ||
		args[1].type != MANAGESIEVE_ARG_EOL) {
		sasl_server_auth_failed(&client->common,
			"Invalid AUTHENTICATE client response.");
		return;
	}

	line = MANAGESIEVE_ARG_STR(&args[0]);

	if (strcmp(line, "*") == 0)
		sasl_server_auth_abort(&client->common);
	else {
		client_set_auth_waiting(client);
		auth_client_request_continue(client->common.auth_request, line);
		io_remove(&client->io);

		/* clear sensitive data */
		safe_memset(line, 0, strlen(line));
	}
}

static void client_authfail_delay_timeout(struct managesieve_client *client)
{
	timeout_remove(&client->to_authfail_delay);

	/* get back to normal client input. */
	i_assert(client->io == NULL);
	client->io = io_add(client->common.fd, IO_READ, client_input, client);
	client_input(client);
}

void client_auth_failed(struct managesieve_client *client, bool nodelay)
{
	unsigned int delay_msecs;

	client->common.auth_command_tag = NULL;

	if ( client->auth_initializing )
		return;

	if ( client->io != NULL )
		io_remove(&client->io);
	if ( nodelay ) {
		client->io = io_add(client->common.fd, IO_READ, client_input, client);
		client_input(client);
		return;
	}

	/* increase the timeout after each unsuccessful attempt, but don't
		increase it so high that the idle timeout would be triggered */
	delay_msecs = client->common.auth_attempts *
		AUTH_FAILURE_DELAY_INCREASE_MSECS;
	if (delay_msecs > CLIENT_LOGIN_IDLE_TIMEOUT_MSECS)
		delay_msecs = CLIENT_LOGIN_IDLE_TIMEOUT_MSECS - 1000;

	i_assert(client->to_authfail_delay == NULL);
	client->to_authfail_delay =
		timeout_add(delay_msecs, client_authfail_delay_timeout, client);
}

static bool client_handle_args(struct managesieve_client *client,
	const char *const *args, bool success, bool *nodelay_r)
{
	const char *reason = NULL, *host = NULL, *destuser = NULL, *pass = NULL;
	const char *master_user = NULL;
	const char *key, *value, *p;
	enum login_proxy_ssl_flags ssl_flags = 0;
	unsigned int port = 2000;
	unsigned int proxy_timeout_msecs = 0;
	bool proxy = FALSE, temp = FALSE, nologin = !success;
	bool authz_failure = FALSE;

	*nodelay_r = FALSE;
	for (; *args != NULL; args++) {
		p = strchr(*args, '=');
		if (p == NULL) {
			key = *args;
			value = "";
		} else {
			key = t_strdup_until(*args, p);
			value = p + 1;
		}
		if (strcmp(key, "nologin") == 0)
			nologin = TRUE;
		else if (strcmp(key, "nodelay") == 0)
			*nodelay_r = TRUE;
		else if (strcmp(key, "proxy") == 0)
			proxy = TRUE;
		else if (strcmp(key, "temp") == 0)
			temp = TRUE;
		else if (strcmp(key, "authz") == 0)
			authz_failure = TRUE;
		else if (strcmp(key, "reason") == 0)
			reason = value + 7;
		else if (strcmp(key, "host") == 0)
			host = value;
		else if (strcmp(key, "port") == 0)
			port = atoi(value);
		else if (strcmp(key, "destuser") == 0)
			destuser = value;
		else if (strcmp(key, "pass") == 0)
			pass = value;
		else if (strcmp(key, "proxy_timeout") == 0)
			proxy_timeout_msecs = 1000*atoi(value);
		else if (strcmp(key, "master") == 0)
			master_user = value;
		else if (strcmp(key, "ssl") == 0) {
			if (strcmp(value, "yes") == 0)
				ssl_flags |= PROXY_SSL_FLAG_YES;
			else if (strcmp(value, "any-cert") == 0) {
				ssl_flags |= PROXY_SSL_FLAG_YES |
					PROXY_SSL_FLAG_ANY_CERT;
			}
		} else if (strcmp(key, "starttls") == 0) {
			ssl_flags |= PROXY_SSL_FLAG_STARTTLS;
		} else if (strcmp(key, "user") == 0) {
			/* already handled in login-common */
		} else if (auth_debug) {
			i_info("Ignoring unknown passdb extra field: %s", key);
		}
	}

	if (destuser == NULL)
		destuser = client->common.virtual_user;

	if (proxy) {
		/* we want to proxy the connection to another server.
		don't do this unless authentication succeeded. with
		master user proxying we can get FAIL with proxy still set.

		proxy host=.. [port=..] [destuser=..] pass=.. */
		if (!success)
			return FALSE;
		if ( managesieve_proxy_new(client, host, port, destuser, master_user,
			pass, ssl_flags, proxy_timeout_msecs) < 0 )
			client_auth_failed(client, TRUE);
		return TRUE;
	}

	if (host != NULL) {
		string_t *resp_code;

		/* MANAGESIEVE referral

		   [nologin] referral host=.. [port=..] [destuser=..]
		   [reason=..]

		   NO (REFERRAL sieve://user;AUTH=mech@host:port/) Can't login.
		   OK (...) Logged in, but you should use this server instead.
		   .. [REFERRAL ..] (Reason from auth server)
		*/
		resp_code = t_str_new(128);
		str_printfa(resp_code, "REFERRAL sieve://%s;AUTH=%s@%s",
			    destuser, client->common.auth_mech_name, host);
		if (port != 2000)
			str_printfa(resp_code, ":%u", port);

		if (reason == NULL) {
			if (nologin)
				reason = "Try this server instead.";
			else
				reason = "Logged in, but you should use "
					"this server instead.";
		}

		if (!nologin) {
			client_send_okresp(client, str_c(resp_code), reason);
			client_destroy_success(client, "Login with referral");
			return TRUE;
		}
		client_send_noresp(client, str_c(resp_code), reason);
	} else if (nologin) {
		/* Authentication went ok, but for some reason user isn't
		   allowed to log in. Shouldn't probably happen. */
		if (reason != NULL)
			client_send_no(client, reason);
		else if (temp)
			client_send_no(client, AUTH_TEMP_FAILED_MSG);
		else if (authz_failure)
			client_send_no(client, "Authorization failed.");
		else
			client_send_no(client, AUTH_FAILED_MSG);
	} else {
		/* normal login/failure */
		return FALSE;
	}

	i_assert(nologin);

	managesieve_parser_reset(client->parser);

	if (!client->destroyed)
		client_auth_failed(client, *nodelay_r);
	return TRUE;
}

static void sasl_callback(struct client *_client, enum sasl_server_reply reply,
			  const char *data, const char *const *args)
{
	struct managesieve_client *client = (struct managesieve_client *)_client;
	string_t *str;
	bool nodelay;

	i_assert(!client->destroyed ||
		reply == SASL_SERVER_REPLY_AUTH_ABORTED ||
		reply == SASL_SERVER_REPLY_MASTER_FAILED);

	switch (reply) {
	case SASL_SERVER_REPLY_SUCCESS:
		if ( client->to_auth_waiting != NULL )
			timeout_remove(&client->to_auth_waiting);
		if (args != NULL) {
			if (client_handle_args(client, args, TRUE, &nodelay))
				break;
		}

		client_destroy_success(client, "Login");
		break;

	case SASL_SERVER_REPLY_AUTH_FAILED:
	case SASL_SERVER_REPLY_AUTH_ABORTED:
		if ( client->to_auth_waiting != NULL )
			timeout_remove(&client->to_auth_waiting);
		if (args != NULL) {
			if (client_handle_args(client, args, FALSE, &nodelay))
				break;
		}


		if ( reply == SASL_SERVER_REPLY_AUTH_ABORTED )
			client_send_no(client, "Authentication aborted by client.");
		else
			client_send_no(client, data != NULL ? data : AUTH_FAILED_MSG);

		managesieve_parser_reset(client->parser);

		if (!client->destroyed)
			client_auth_failed(client, nodelay);
		break;

	case SASL_SERVER_REPLY_MASTER_FAILED:
		if (data == NULL)
			client_destroy_internal_failure(client);
		else {
			client_send_no(client, data);
			client_destroy_success(client, data);
		}
		break;

	case SASL_SERVER_REPLY_CONTINUE:
		T_BEGIN {
			str = t_str_new(256);
			managesieve_quote_append_string(str, data, TRUE);
			str_append(str, "\r\n");

			/* don't check return value here. it gets tricky if we try
			   to call client_destroy() in here. */
			(void)o_stream_send(client->output, str_c(str), str_len(str));
		} T_END;

		if (client->to_auth_waiting != NULL)
			timeout_remove(&client->to_auth_waiting);

		managesieve_parser_reset(client->parser);

		i_assert(client->io == NULL);
		client->io = io_add(client->common.fd, IO_READ, client_auth_input, client);
		client_auth_input(client);
		return;
	}

	client_unref(client);
}

int cmd_authenticate(struct managesieve_client *client, struct managesieve_arg *args)
{
	const char *mech_name, *init_resp = NULL;

	/* one mandatory argument: authentication mechanism name */
	if (args[0].type != MANAGESIEVE_ARG_STRING)
		return -1;
	if (args[1].type != MANAGESIEVE_ARG_EOL) {
		/* optional SASL initial response */
		if (args[1].type != MANAGESIEVE_ARG_STRING ||
		    args[2].type != MANAGESIEVE_ARG_EOL)
			return -1;
		init_resp = MANAGESIEVE_ARG_STR(&args[1]);
	}

	mech_name = MANAGESIEVE_ARG_STR(&args[0]);
	if (*mech_name == '\0')
		return -1;

	/* FIXME: This refuses the ANONYMOUS mechanism.
	 *   This can be removed once anonymous login is implemented according to the
	 *   draft RFC. - Stephan
	 */
	if ( strncasecmp(mech_name, "ANONYMOUS", 9) == 0 ) {
		client_send_no(client, "ANONYMOUS mechanism is not implemented.");
		return 0;
	}

	client_ref(client);
	client->auth_initializing = TRUE;
	sasl_server_auth_begin(&client->common, MANAGESIEVE_SERVICE_NAME, mech_name,
		init_resp, sasl_callback);
	client->auth_initializing = FALSE;
	if (!client->common.authenticating)
		return 1;

	/* don't handle input until we get the initial auth reply */
	if (client->io != NULL)
		io_remove(&client->io);
	client_set_auth_waiting(client);

	managesieve_parser_reset(client->parser);

	return 0;
}