auth2.c revision 128460
1/*
2 * Copyright (c) 2000 Markus Friedl.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "includes.h"
26RCSID("$OpenBSD: auth2.c,v 1.104 2003/11/04 08:54:09 djm Exp $");
27RCSID("$FreeBSD: head/crypto/openssh/auth2.c 128460 2004-04-20 09:46:41Z des $");
28
29#include "canohost.h"
30#include "ssh2.h"
31#include "xmalloc.h"
32#include "packet.h"
33#include "log.h"
34#include "servconf.h"
35#include "compat.h"
36#include "auth.h"
37#include "dispatch.h"
38#include "pathnames.h"
39#include "monitor_wrap.h"
40
41#ifdef GSSAPI
42#include "ssh-gss.h"
43#endif
44
45/* import */
46extern ServerOptions options;
47extern u_char *session_id2;
48extern u_int session_id2_len;
49
50/* methods */
51
52extern Authmethod method_none;
53extern Authmethod method_pubkey;
54extern Authmethod method_passwd;
55extern Authmethod method_kbdint;
56extern Authmethod method_hostbased;
57#ifdef GSSAPI
58extern Authmethod method_gssapi;
59#endif
60
61Authmethod *authmethods[] = {
62	&method_none,
63	&method_pubkey,
64#ifdef GSSAPI
65	&method_gssapi,
66#endif
67	&method_passwd,
68	&method_kbdint,
69	&method_hostbased,
70	NULL
71};
72
73/* protocol */
74
75static void input_service_request(int, u_int32_t, void *);
76static void input_userauth_request(int, u_int32_t, void *);
77
78/* helper */
79static Authmethod *authmethod_lookup(const char *);
80static char *authmethods_get(void);
81int user_key_allowed(struct passwd *, Key *);
82
83/*
84 * loop until authctxt->success == TRUE
85 */
86
87void
88do_authentication2(Authctxt *authctxt)
89{
90	/* challenge-response is implemented via keyboard interactive */
91	if (options.challenge_response_authentication)
92		options.kbd_interactive_authentication = 1;
93
94	dispatch_init(&dispatch_protocol_error);
95	dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
96	dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
97}
98
99static void
100input_service_request(int type, u_int32_t seq, void *ctxt)
101{
102	Authctxt *authctxt = ctxt;
103	u_int len;
104	int acceptit = 0;
105	char *service = packet_get_string(&len);
106	packet_check_eom();
107
108	if (authctxt == NULL)
109		fatal("input_service_request: no authctxt");
110
111	if (strcmp(service, "ssh-userauth") == 0) {
112		if (!authctxt->success) {
113			acceptit = 1;
114			/* now we can handle user-auth requests */
115			dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
116		}
117	}
118	/* XXX all other service requests are denied */
119
120	if (acceptit) {
121		packet_start(SSH2_MSG_SERVICE_ACCEPT);
122		packet_put_cstring(service);
123		packet_send();
124		packet_write_wait();
125	} else {
126		debug("bad service request %s", service);
127		packet_disconnect("bad service request %s", service);
128	}
129	xfree(service);
130}
131
132static void
133input_userauth_request(int type, u_int32_t seq, void *ctxt)
134{
135	Authctxt *authctxt = ctxt;
136	Authmethod *m = NULL;
137	char *user, *service, *method, *style = NULL;
138	int authenticated = 0;
139#ifdef HAVE_LOGIN_CAP
140	login_cap_t *lc;
141	const char *from_host, *from_ip;
142
143        from_host = get_canonical_hostname(options.use_dns);
144        from_ip = get_remote_ipaddr();
145#endif
146
147	if (authctxt == NULL)
148		fatal("input_userauth_request: no authctxt");
149
150	user = packet_get_string(NULL);
151	service = packet_get_string(NULL);
152	method = packet_get_string(NULL);
153	debug("userauth-request for user %s service %s method %s", user, service, method);
154	debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
155
156	if ((style = strchr(user, ':')) != NULL)
157		*style++ = 0;
158
159	if (authctxt->attempt++ == 0) {
160		/* setup auth context */
161		authctxt->pw = PRIVSEP(getpwnamallow(user));
162		authctxt->user = xstrdup(user);
163		if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
164			authctxt->valid = 1;
165			debug2("input_userauth_request: setting up authctxt for %s", user);
166#ifdef USE_PAM
167			if (options.use_pam)
168				PRIVSEP(start_pam(authctxt));
169#endif
170		} else {
171			logit("input_userauth_request: illegal user %s", user);
172			authctxt->pw = fakepw();
173#ifdef USE_PAM
174			if (options.use_pam)
175				PRIVSEP(start_pam(authctxt));
176#endif
177		}
178		setproctitle("%s%s", authctxt->pw ? user : "unknown",
179		    use_privsep ? " [net]" : "");
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                        logit("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                        logit("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
211	/* reset state */
212	auth2_challenge_stop(authctxt);
213
214#ifdef GSSAPI
215	dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
216	dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
217#endif
218
219	authctxt->postponed = 0;
220
221	/* try to authenticate user */
222	m = authmethod_lookup(method);
223	if (m != NULL) {
224		debug2("input_userauth_request: try method %s", method);
225		authenticated =	m->userauth(authctxt);
226	}
227	userauth_finish(authctxt, authenticated, method);
228
229	xfree(service);
230	xfree(user);
231	xfree(method);
232}
233
234void
235userauth_finish(Authctxt *authctxt, int authenticated, char *method)
236{
237	char *methods;
238
239	if (!authctxt->valid && authenticated)
240		fatal("INTERNAL ERROR: authenticated invalid user %s",
241		    authctxt->user);
242
243	/* Special handling for root */
244	if (authenticated && authctxt->pw->pw_uid == 0 &&
245	    !auth_root_allowed(method))
246		authenticated = 0;
247
248#ifdef USE_PAM
249	if (options.use_pam && authenticated && !PRIVSEP(do_pam_account()))
250		authenticated = 0;
251#endif
252
253#ifdef _UNICOS
254	if (authenticated && cray_access_denied(authctxt->user)) {
255		authenticated = 0;
256		fatal("Access denied for user %s.",authctxt->user);
257	}
258#endif /* _UNICOS */
259
260	/* Log before sending the reply */
261	auth_log(authctxt, authenticated, method, " ssh2");
262
263	if (authctxt->postponed)
264		return;
265
266	/* XXX todo: check if multiple auth methods are needed */
267	if (authenticated == 1) {
268		/* turn off userauth */
269		dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
270		packet_start(SSH2_MSG_USERAUTH_SUCCESS);
271		packet_send();
272		packet_write_wait();
273		/* now we can break out */
274		authctxt->success = 1;
275	} else {
276		if (authctxt->failures++ > AUTH_FAIL_MAX)
277			packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
278		methods = authmethods_get();
279		packet_start(SSH2_MSG_USERAUTH_FAILURE);
280		packet_put_cstring(methods);
281		packet_put_char(0);	/* XXX partial success, unused */
282		packet_send();
283		packet_write_wait();
284		xfree(methods);
285	}
286}
287
288#define	DELIM	","
289
290static char *
291authmethods_get(void)
292{
293	Buffer b;
294	char *list;
295	int i;
296
297	buffer_init(&b);
298	for (i = 0; authmethods[i] != NULL; i++) {
299		if (strcmp(authmethods[i]->name, "none") == 0)
300			continue;
301		if (authmethods[i]->enabled != NULL &&
302		    *(authmethods[i]->enabled) != 0) {
303			if (buffer_len(&b) > 0)
304				buffer_append(&b, ",", 1);
305			buffer_append(&b, authmethods[i]->name,
306			    strlen(authmethods[i]->name));
307		}
308	}
309	buffer_append(&b, "\0", 1);
310	list = xstrdup(buffer_ptr(&b));
311	buffer_free(&b);
312	return list;
313}
314
315static Authmethod *
316authmethod_lookup(const char *name)
317{
318	int i;
319
320	if (name != NULL)
321		for (i = 0; authmethods[i] != NULL; i++)
322			if (authmethods[i]->enabled != NULL &&
323			    *(authmethods[i]->enabled) != 0 &&
324			    strcmp(name, authmethods[i]->name) == 0)
325				return authmethods[i];
326	debug2("Unrecognized authentication method name: %s",
327	    name ? name : "NULL");
328	return NULL;
329}
330