auth2.c revision 231584
1137019Sdes/* $OpenBSD: auth2.c,v 1.123 2011/03/10 02:52:57 djm Exp $ */
299052Sdes/*
398675Sdes * Copyright (c) 2000 Markus Friedl.  All rights reserved.
498675Sdes *
598675Sdes * Redistribution and use in source and binary forms, with or without
698675Sdes * modification, are permitted provided that the following conditions
798675Sdes * are met:
898675Sdes * 1. Redistributions of source code must retain the above copyright
998675Sdes *    notice, this list of conditions and the following disclaimer.
1098675Sdes * 2. Redistributions in binary form must reproduce the above copyright
1198675Sdes *    notice, this list of conditions and the following disclaimer in the
1298675Sdes *    documentation and/or other materials provided with the distribution.
1398675Sdes *
1498675Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1598675Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1698675Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1798675Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1898675Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1998675Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2098675Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2198675Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2298675Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2398675Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2498675Sdes */
2598675Sdes
2698675Sdes#include "includes.h"
2798675Sdes__RCSID("$FreeBSD: head/crypto/openssh/auth2.c 231584 2012-02-13 11:59:59Z ed $");
2898675Sdes
2998675Sdes#include <sys/types.h>
3098675Sdes#include <sys/stat.h>
3198675Sdes#include <sys/uio.h>
3298675Sdes
3398675Sdes#include <fcntl.h>
3498675Sdes#include <pwd.h>
3598675Sdes#include <stdarg.h>
3698675Sdes#include <string.h>
3798675Sdes#include <unistd.h>
3898675Sdes
3998675Sdes#include "atomicio.h"
4098675Sdes#include "xmalloc.h"
4198675Sdes#include "ssh2.h"
4298675Sdes#include "packet.h"
4398675Sdes#include "log.h"
44126277Sdes#include "buffer.h"
4598675Sdes#include "servconf.h"
4698675Sdes#include "compat.h"
4798675Sdes#include "key.h"
4898675Sdes#include "hostfile.h"
49106130Sdes#include "auth.h"
5098675Sdes#include "dispatch.h"
5198675Sdes#include "pathnames.h"
5298675Sdes#include "buffer.h"
5398675Sdes#include "canohost.h"
5498675Sdes
5598675Sdes#ifdef GSSAPI
5698675Sdes#include "ssh-gss.h"
5798675Sdes#endif
5898675Sdes#include "monitor_wrap.h"
5998675Sdes
60124211Sdes/* import */
61124211Sdesextern ServerOptions options;
62137019Sdesextern u_char *session_id2;
63137019Sdesextern u_int session_id2_len;
64137019Sdesextern Buffer loginmsg;
65124211Sdes
66126277Sdes/* methods */
67124211Sdes
68124211Sdesextern Authmethod method_none;
6998937Sdesextern Authmethod method_pubkey;
70128460Sdesextern Authmethod method_passwd;
71124211Sdesextern Authmethod method_kbdint;
72124211Sdesextern Authmethod method_hostbased;
73124211Sdes#ifdef GSSAPI
74124211Sdesextern Authmethod method_gssapi;
75124211Sdes#endif
7698937Sdes#ifdef JPAKE
7798937Sdesextern Authmethod method_jpake;
78147005Sdes#endif
79147005Sdes
80147005SdesAuthmethod *authmethods[] = {
81147005Sdes	&method_none,
82147005Sdes	&method_pubkey,
83147005Sdes#ifdef GSSAPI
84126277Sdes	&method_gssapi,
8598675Sdes#endif
8698675Sdes#ifdef JPAKE
87126277Sdes	&method_jpake,
8898675Sdes#endif
8998675Sdes	&method_passwd,
9098675Sdes	&method_kbdint,
9198675Sdes	&method_hostbased,
9298675Sdes	NULL
9398675Sdes};
9498675Sdes
9598675Sdes/* protocol */
9698675Sdes
9798675Sdesstatic void input_service_request(int, u_int32_t, void *);
9898675Sdesstatic void input_userauth_request(int, u_int32_t, void *);
9998675Sdes
10098675Sdes/* helper */
10198675Sdesstatic Authmethod *authmethod_lookup(const char *);
10298675Sdesstatic char *authmethods_get(void);
10398675Sdes
10498675Sdeschar *
10598675Sdesauth2_read_banner(void)
10698675Sdes{
10798675Sdes	struct stat st;
10898675Sdes	char *banner = NULL;
10998675Sdes	size_t len, n;
11098675Sdes	int fd;
11198675Sdes
11298675Sdes	if ((fd = open(options.banner, O_RDONLY)) == -1)
11398675Sdes		return (NULL);
11498675Sdes	if (fstat(fd, &st) == -1) {
11598675Sdes		close(fd);
116		return (NULL);
117	}
118	if (st.st_size > 1*1024*1024) {
119		close(fd);
120		return (NULL);
121	}
122
123	len = (size_t)st.st_size;		/* truncate */
124	banner = xmalloc(len + 1);
125	n = atomicio(read, fd, banner, len);
126	close(fd);
127
128	if (n != len) {
129		xfree(banner);
130		return (NULL);
131	}
132	banner[n] = '\0';
133
134	return (banner);
135}
136
137void
138userauth_send_banner(const char *msg)
139{
140	if (datafellows & SSH_BUG_BANNER)
141		return;
142
143	packet_start(SSH2_MSG_USERAUTH_BANNER);
144	packet_put_cstring(msg);
145	packet_put_cstring("");		/* language, unused */
146	packet_send();
147	debug("%s: sent", __func__);
148}
149
150static void
151userauth_banner(void)
152{
153	char *banner = NULL;
154
155	if (options.banner == NULL ||
156	    strcasecmp(options.banner, "none") == 0 ||
157	    (datafellows & SSH_BUG_BANNER) != 0)
158		return;
159
160	if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
161		goto done;
162	userauth_send_banner(banner);
163
164done:
165	if (banner)
166		xfree(banner);
167}
168
169/*
170 * loop until authctxt->success == TRUE
171 */
172void
173do_authentication2(Authctxt *authctxt)
174{
175	dispatch_init(&dispatch_protocol_error);
176	dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
177	dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
178}
179
180/*ARGSUSED*/
181static void
182input_service_request(int type, u_int32_t seq, void *ctxt)
183{
184	Authctxt *authctxt = ctxt;
185	u_int len;
186	int acceptit = 0;
187	char *service = packet_get_cstring(&len);
188	packet_check_eom();
189
190	if (authctxt == NULL)
191		fatal("input_service_request: no authctxt");
192
193	if (strcmp(service, "ssh-userauth") == 0) {
194		if (!authctxt->success) {
195			acceptit = 1;
196			/* now we can handle user-auth requests */
197			dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
198		}
199	}
200	/* XXX all other service requests are denied */
201
202	if (acceptit) {
203		packet_start(SSH2_MSG_SERVICE_ACCEPT);
204		packet_put_cstring(service);
205		packet_send();
206		packet_write_wait();
207	} else {
208		debug("bad service request %s", service);
209		packet_disconnect("bad service request %s", service);
210	}
211	xfree(service);
212}
213
214/*ARGSUSED*/
215static void
216input_userauth_request(int type, u_int32_t seq, void *ctxt)
217{
218	Authctxt *authctxt = ctxt;
219	Authmethod *m = NULL;
220	char *user, *service, *method, *style = NULL;
221	int authenticated = 0;
222#ifdef HAVE_LOGIN_CAP
223	login_cap_t *lc;
224	const char *from_host, *from_ip;
225
226	from_host = get_canonical_hostname(options.use_dns);
227	from_ip = get_remote_ipaddr();
228#endif
229
230	if (authctxt == NULL)
231		fatal("input_userauth_request: no authctxt");
232
233	user = packet_get_cstring(NULL);
234	service = packet_get_cstring(NULL);
235	method = packet_get_cstring(NULL);
236	debug("userauth-request for user %s service %s method %s", user, service, method);
237	debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
238
239	if ((style = strchr(user, ':')) != NULL)
240		*style++ = 0;
241
242	if (authctxt->attempt++ == 0) {
243		/* setup auth context */
244		authctxt->pw = PRIVSEP(getpwnamallow(user));
245		authctxt->user = xstrdup(user);
246		if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
247			authctxt->valid = 1;
248			debug2("input_userauth_request: setting up authctxt for %s", user);
249		} else {
250			logit("input_userauth_request: invalid user %s", user);
251			authctxt->pw = fakepw();
252#ifdef SSH_AUDIT_EVENTS
253			PRIVSEP(audit_event(SSH_INVALID_USER));
254#endif
255		}
256#ifdef USE_PAM
257		if (options.use_pam)
258			PRIVSEP(start_pam(authctxt));
259#endif
260		setproctitle("%s%s", authctxt->valid ? user : "unknown",
261		    use_privsep ? " [net]" : "");
262		authctxt->service = xstrdup(service);
263		authctxt->style = style ? xstrdup(style) : NULL;
264		if (use_privsep)
265			mm_inform_authserv(service, style);
266		userauth_banner();
267	} else if (strcmp(user, authctxt->user) != 0 ||
268	    strcmp(service, authctxt->service) != 0) {
269		packet_disconnect("Change of username or service not allowed: "
270		    "(%s,%s) -> (%s,%s)",
271		    authctxt->user, authctxt->service, user, service);
272	}
273
274#ifdef HAVE_LOGIN_CAP
275	if (authctxt->pw != NULL) {
276		lc = login_getpwclass(authctxt->pw);
277		if (lc == NULL)
278			lc = login_getclassbyname(NULL, authctxt->pw);
279		if (!auth_hostok(lc, from_host, from_ip)) {
280			logit("Denied connection for %.200s from %.200s [%.200s].",
281			    authctxt->pw->pw_name, from_host, from_ip);
282			packet_disconnect("Sorry, you are not allowed to connect.");
283		}
284		if (!auth_timeok(lc, time(NULL))) {
285			logit("LOGIN %.200s REFUSED (TIME) FROM %.200s",
286			    authctxt->pw->pw_name, from_host);
287			packet_disconnect("Logins not available right now.");
288		}
289		login_close(lc);
290		lc = NULL;
291	}
292#endif  /* HAVE_LOGIN_CAP */
293
294	/* reset state */
295	auth2_challenge_stop(authctxt);
296#ifdef JPAKE
297	auth2_jpake_stop(authctxt);
298#endif
299
300#ifdef GSSAPI
301	/* XXX move to auth2_gssapi_stop() */
302	dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
303	dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
304#endif
305
306	authctxt->postponed = 0;
307	authctxt->server_caused_failure = 0;
308
309	/* try to authenticate user */
310	m = authmethod_lookup(method);
311	if (m != NULL && authctxt->failures < options.max_authtries) {
312		debug2("input_userauth_request: try method %s", method);
313		authenticated =	m->userauth(authctxt);
314	}
315	userauth_finish(authctxt, authenticated, method);
316
317	xfree(service);
318	xfree(user);
319	xfree(method);
320}
321
322void
323userauth_finish(Authctxt *authctxt, int authenticated, char *method)
324{
325	char *methods;
326
327	if (!authctxt->valid && authenticated)
328		fatal("INTERNAL ERROR: authenticated invalid user %s",
329		    authctxt->user);
330
331	/* Special handling for root */
332	if (authenticated && authctxt->pw->pw_uid == 0 &&
333	    !auth_root_allowed(method)) {
334		authenticated = 0;
335#ifdef SSH_AUDIT_EVENTS
336		PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
337#endif
338	}
339
340#ifdef USE_PAM
341	if (options.use_pam && authenticated) {
342		if (!PRIVSEP(do_pam_account())) {
343			/* if PAM returned a message, send it to the user */
344			if (buffer_len(&loginmsg) > 0) {
345				buffer_append(&loginmsg, "\0", 1);
346				userauth_send_banner(buffer_ptr(&loginmsg));
347				packet_write_wait();
348			}
349			fatal("Access denied for user %s by PAM account "
350			    "configuration", authctxt->user);
351		}
352	}
353#endif
354
355#ifdef _UNICOS
356	if (authenticated && cray_access_denied(authctxt->user)) {
357		authenticated = 0;
358		fatal("Access denied for user %s.",authctxt->user);
359	}
360#endif /* _UNICOS */
361
362	/* Log before sending the reply */
363	auth_log(authctxt, authenticated, method, " ssh2");
364
365	if (authctxt->postponed)
366		return;
367
368	/* XXX todo: check if multiple auth methods are needed */
369	if (authenticated == 1) {
370		/* turn off userauth */
371		dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
372		packet_start(SSH2_MSG_USERAUTH_SUCCESS);
373		packet_send();
374		packet_write_wait();
375		/* now we can break out */
376		authctxt->success = 1;
377	} else {
378
379		/* Allow initial try of "none" auth without failure penalty */
380		if (!authctxt->server_caused_failure &&
381		    (authctxt->attempt > 1 || strcmp(method, "none") != 0))
382			authctxt->failures++;
383		if (authctxt->failures >= options.max_authtries) {
384#ifdef SSH_AUDIT_EVENTS
385			PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
386#endif
387			packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
388		}
389		methods = authmethods_get();
390		packet_start(SSH2_MSG_USERAUTH_FAILURE);
391		packet_put_cstring(methods);
392		packet_put_char(0);	/* XXX partial success, unused */
393		packet_send();
394		packet_write_wait();
395		xfree(methods);
396	}
397}
398
399static char *
400authmethods_get(void)
401{
402	Buffer b;
403	char *list;
404	int i;
405
406	buffer_init(&b);
407	for (i = 0; authmethods[i] != NULL; i++) {
408		if (strcmp(authmethods[i]->name, "none") == 0)
409			continue;
410		if (authmethods[i]->enabled != NULL &&
411		    *(authmethods[i]->enabled) != 0) {
412			if (buffer_len(&b) > 0)
413				buffer_append(&b, ",", 1);
414			buffer_append(&b, authmethods[i]->name,
415			    strlen(authmethods[i]->name));
416		}
417	}
418	buffer_append(&b, "\0", 1);
419	list = xstrdup(buffer_ptr(&b));
420	buffer_free(&b);
421	return list;
422}
423
424static Authmethod *
425authmethod_lookup(const char *name)
426{
427	int i;
428
429	if (name != NULL)
430		for (i = 0; authmethods[i] != NULL; i++)
431			if (authmethods[i]->enabled != NULL &&
432			    *(authmethods[i]->enabled) != 0 &&
433			    strcmp(name, authmethods[i]->name) == 0)
434				return authmethods[i];
435	debug2("Unrecognized authentication method name: %s",
436	    name ? name : "NULL");
437	return NULL;
438}
439
440