auth2.c revision 98684
1326941Sdim/*
2326941Sdim * Copyright (c) 2000 Markus Friedl.  All rights reserved.
3353358Sdim *
4353358Sdim * Redistribution and use in source and binary forms, with or without
5353358Sdim * modification, are permitted provided that the following conditions
6326941Sdim * are met:
7326941Sdim * 1. Redistributions of source code must retain the above copyright
8326941Sdim *    notice, this list of conditions and the following disclaimer.
9326941Sdim * 2. Redistributions in binary form must reproduce the above copyright
10326941Sdim *    notice, this list of conditions and the following disclaimer in the
11326941Sdim *    documentation and/or other materials provided with the distribution.
12326941Sdim *
13326941Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14326941Sdim * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15326941Sdim * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16326941Sdim * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17326941Sdim * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18326941Sdim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19326941Sdim * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20326941Sdim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21326941Sdim * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22326941Sdim * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23326941Sdim */
24326941Sdim
25326941Sdim#include "includes.h"
26326941SdimRCSID("$OpenBSD: auth2.c,v 1.93 2002/05/31 11:35:15 markus Exp $");
27326941SdimRCSID("$FreeBSD: head/crypto/openssh/auth2.c 98684 2002-06-23 16:09:08Z des $");
28326941Sdim
29326941Sdim#include "ssh2.h"
30326941Sdim#include "xmalloc.h"
31326941Sdim#include "packet.h"
32326941Sdim#include "log.h"
33326941Sdim#include "servconf.h"
34#include "compat.h"
35#include "auth.h"
36#include "dispatch.h"
37#include "pathnames.h"
38#include "canohost.h"
39#include "monitor_wrap.h"
40
41#ifdef HAVE_LOGIN_CAP
42#include <login_cap.h>
43#endif /* HAVE_LOGIN_CAP */
44
45/* import */
46extern ServerOptions options;
47extern u_char *session_id2;
48extern int session_id2_len;
49
50Authctxt *x_authctxt = NULL;
51
52/* methods */
53
54extern Authmethod method_none;
55extern Authmethod method_pubkey;
56extern Authmethod method_passwd;
57extern Authmethod method_kbdint;
58extern Authmethod method_hostbased;
59
60Authmethod *authmethods[] = {
61	&method_none,
62	&method_pubkey,
63	&method_passwd,
64	&method_kbdint,
65	&method_hostbased,
66	NULL
67};
68
69/* protocol */
70
71static void input_service_request(int, u_int32_t, void *);
72static void input_userauth_request(int, u_int32_t, void *);
73
74/* helper */
75static Authmethod *authmethod_lookup(const char *);
76static char *authmethods_get(void);
77int user_key_allowed(struct passwd *, Key *);
78int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
79
80/*
81 * loop until authctxt->success == TRUE
82 */
83
84Authctxt *
85do_authentication2(void)
86{
87	Authctxt *authctxt = authctxt_new();
88
89	x_authctxt = authctxt;		/*XXX*/
90
91	/* challenge-response is implemented via keyboard interactive */
92	if (options.challenge_response_authentication)
93		options.kbd_interactive_authentication = 1;
94
95	dispatch_init(&dispatch_protocol_error);
96	dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
97	dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
98
99	return (authctxt);
100}
101
102static void
103input_service_request(int type, u_int32_t seq, void *ctxt)
104{
105	Authctxt *authctxt = ctxt;
106	u_int len;
107	int accept = 0;
108	char *service = packet_get_string(&len);
109	packet_check_eom();
110
111	if (authctxt == NULL)
112		fatal("input_service_request: no authctxt");
113
114	if (strcmp(service, "ssh-userauth") == 0) {
115		if (!authctxt->success) {
116			accept = 1;
117			/* now we can handle user-auth requests */
118			dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
119		}
120	}
121	/* XXX all other service requests are denied */
122
123	if (accept) {
124		packet_start(SSH2_MSG_SERVICE_ACCEPT);
125		packet_put_cstring(service);
126		packet_send();
127		packet_write_wait();
128	} else {
129		debug("bad service request %s", service);
130		packet_disconnect("bad service request %s", service);
131	}
132	xfree(service);
133}
134
135static void
136input_userauth_request(int type, u_int32_t seq, void *ctxt)
137{
138	Authctxt *authctxt = ctxt;
139	Authmethod *m = NULL;
140	char *user, *service, *method, *style = NULL;
141	int authenticated = 0;
142#ifdef HAVE_LOGIN_CAP
143	login_cap_t *lc;
144#endif /* HAVE_LOGIN_CAP */
145#if defined(HAVE_LOGIN_CAP)
146	const char *from_host, *from_ip;
147
148	from_host = get_canonical_hostname(options.verify_reverse_mapping);
149	from_ip = get_remote_ipaddr();
150#endif /* HAVE_LOGIN_CAP */
151
152	if (authctxt == NULL)
153		fatal("input_userauth_request: no authctxt");
154
155	user = packet_get_string(NULL);
156	service = packet_get_string(NULL);
157	method = packet_get_string(NULL);
158	debug("userauth-request for user %s service %s method %s", user, service, method);
159	debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
160
161	if ((style = strchr(user, ':')) != NULL)
162		*style++ = 0;
163
164	if (authctxt->attempt++ == 0) {
165		/* setup auth context */
166		authctxt->pw = PRIVSEP(getpwnamallow(user));
167		if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
168			authctxt->valid = 1;
169			debug2("input_userauth_request: setting up authctxt for %s", user);
170#ifdef USE_PAM
171			start_pam(authctxt->pw);
172#endif
173		} else {
174			log("input_userauth_request: illegal user %s", user);
175			authctxt->pw = NULL;
176		}
177		setproctitle("%s%s", authctxt->pw ? user : "unknown",
178		    use_privsep ? " [net]" : "");
179		authctxt->user = xstrdup(user);
180		authctxt->service = xstrdup(service);
181		authctxt->style = style ? xstrdup(style) : NULL;
182		if (use_privsep)
183			mm_inform_authserv(service, style);
184	} else if (strcmp(user, authctxt->user) != 0 ||
185	    strcmp(service, authctxt->service) != 0) {
186		packet_disconnect("Change of username or service not allowed: "
187		    "(%s,%s) -> (%s,%s)",
188		    authctxt->user, authctxt->service, user, service);
189	}
190
191#ifdef HAVE_LOGIN_CAP
192	if (authctxt->pw != NULL) {
193		lc = login_getpwclass(authctxt->pw);
194		if (lc == NULL)
195			lc = login_getclassbyname(NULL, authctxt->pw);
196		if (!auth_hostok(lc, from_host, from_ip)) {
197			log("Denied connection for %.200s from %.200s [%.200s].",
198			    authctxt->pw->pw_name, from_host, from_ip);
199			packet_disconnect("Sorry, you are not allowed to connect.");
200		}
201		if (!auth_timeok(lc, time(NULL))) {
202			log("LOGIN %.200s REFUSED (TIME) FROM %.200s",
203			    authctxt->pw->pw_name, from_host);
204			packet_disconnect("Logins not available right now.");
205		}
206		login_close(lc);
207		lc = NULL;
208	}
209#endif  /* HAVE_LOGIN_CAP */
210	/* reset state */
211	auth2_challenge_stop(authctxt);
212	authctxt->postponed = 0;
213
214	/* try to authenticate user */
215	m = authmethod_lookup(method);
216	if (m != NULL) {
217		debug2("input_userauth_request: try method %s", method);
218		authenticated =	m->userauth(authctxt);
219	}
220#ifdef USE_PAM
221	if (authenticated && authctxt->user && !do_pam_account(authctxt->user, NULL))
222		authenticated = 0;
223#endif /* USE_PAM */
224	userauth_finish(authctxt, authenticated, method);
225
226	xfree(service);
227	xfree(user);
228	xfree(method);
229}
230
231void
232userauth_finish(Authctxt *authctxt, int authenticated, char *method)
233{
234	char *methods;
235
236	if (!authctxt->valid && authenticated)
237		fatal("INTERNAL ERROR: authenticated invalid user %s",
238		    authctxt->user);
239
240	/* Special handling for root */
241	if (authenticated && authctxt->pw->pw_uid == 0 &&
242	    !auth_root_allowed(method))
243		authenticated = 0;
244
245	/* Log before sending the reply */
246	auth_log(authctxt, authenticated, method, " ssh2");
247
248	if (authctxt->postponed)
249		return;
250
251	/* XXX todo: check if multiple auth methods are needed */
252	if (authenticated == 1) {
253		/* turn off userauth */
254		dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
255		packet_start(SSH2_MSG_USERAUTH_SUCCESS);
256		packet_send();
257		packet_write_wait();
258		/* now we can break out */
259		authctxt->success = 1;
260	} else {
261		if (authctxt->failures++ > AUTH_FAIL_MAX)
262			packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
263		methods = authmethods_get();
264		packet_start(SSH2_MSG_USERAUTH_FAILURE);
265		packet_put_cstring(methods);
266		packet_put_char(0);	/* XXX partial success, unused */
267		packet_send();
268		packet_write_wait();
269		xfree(methods);
270	}
271}
272
273/* get current user */
274
275struct passwd*
276auth_get_user(void)
277{
278	return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
279}
280
281#define	DELIM	","
282
283static char *
284authmethods_get(void)
285{
286	Buffer b;
287	char *list;
288	int i;
289
290	buffer_init(&b);
291	for (i = 0; authmethods[i] != NULL; i++) {
292		if (strcmp(authmethods[i]->name, "none") == 0)
293			continue;
294		if (authmethods[i]->enabled != NULL &&
295		    *(authmethods[i]->enabled) != 0) {
296			if (buffer_len(&b) > 0)
297				buffer_append(&b, ",", 1);
298			buffer_append(&b, authmethods[i]->name,
299			    strlen(authmethods[i]->name));
300		}
301	}
302	buffer_append(&b, "\0", 1);
303	list = xstrdup(buffer_ptr(&b));
304	buffer_free(&b);
305	return list;
306}
307
308static Authmethod *
309authmethod_lookup(const char *name)
310{
311	int i;
312
313	if (name != NULL)
314		for (i = 0; authmethods[i] != NULL; i++)
315			if (authmethods[i]->enabled != NULL &&
316			    *(authmethods[i]->enabled) != 0 &&
317			    strcmp(name, authmethods[i]->name) == 0)
318				return authmethods[i];
319	debug2("Unrecognized authentication method name: %s",
320	    name ? name : "NULL");
321	return NULL;
322}
323