xref: /freebsd/crypto/openssh/auth2.c (revision bb15ca603fa442c72dde3f3cb8b46db6970e3950)
1 /* $OpenBSD: auth2.c,v 1.123 2011/03/10 02:52:57 djm Exp $ */
2 /*
3  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "includes.h"
27 __RCSID("$FreeBSD$");
28 
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/uio.h>
32 
33 #include <fcntl.h>
34 #include <pwd.h>
35 #include <stdarg.h>
36 #include <string.h>
37 #include <unistd.h>
38 
39 #include "atomicio.h"
40 #include "xmalloc.h"
41 #include "ssh2.h"
42 #include "packet.h"
43 #include "log.h"
44 #include "buffer.h"
45 #include "servconf.h"
46 #include "compat.h"
47 #include "key.h"
48 #include "hostfile.h"
49 #include "auth.h"
50 #include "dispatch.h"
51 #include "pathnames.h"
52 #include "buffer.h"
53 #include "canohost.h"
54 
55 #ifdef GSSAPI
56 #include "ssh-gss.h"
57 #endif
58 #include "monitor_wrap.h"
59 
60 /* import */
61 extern ServerOptions options;
62 extern u_char *session_id2;
63 extern u_int session_id2_len;
64 extern Buffer loginmsg;
65 
66 /* methods */
67 
68 extern Authmethod method_none;
69 extern Authmethod method_pubkey;
70 extern Authmethod method_passwd;
71 extern Authmethod method_kbdint;
72 extern Authmethod method_hostbased;
73 #ifdef GSSAPI
74 extern Authmethod method_gssapi;
75 #endif
76 #ifdef JPAKE
77 extern Authmethod method_jpake;
78 #endif
79 
80 Authmethod *authmethods[] = {
81 	&method_none,
82 	&method_pubkey,
83 #ifdef GSSAPI
84 	&method_gssapi,
85 #endif
86 #ifdef JPAKE
87 	&method_jpake,
88 #endif
89 	&method_passwd,
90 	&method_kbdint,
91 	&method_hostbased,
92 	NULL
93 };
94 
95 /* protocol */
96 
97 static void input_service_request(int, u_int32_t, void *);
98 static void input_userauth_request(int, u_int32_t, void *);
99 
100 /* helper */
101 static Authmethod *authmethod_lookup(const char *);
102 static char *authmethods_get(void);
103 
104 char *
105 auth2_read_banner(void)
106 {
107 	struct stat st;
108 	char *banner = NULL;
109 	size_t len, n;
110 	int fd;
111 
112 	if ((fd = open(options.banner, O_RDONLY)) == -1)
113 		return (NULL);
114 	if (fstat(fd, &st) == -1) {
115 		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 
137 void
138 userauth_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 
150 static void
151 userauth_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 
164 done:
165 	if (banner)
166 		xfree(banner);
167 }
168 
169 /*
170  * loop until authctxt->success == TRUE
171  */
172 void
173 do_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*/
181 static void
182 input_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*/
215 static void
216 input_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 
322 void
323 userauth_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 
399 static char *
400 authmethods_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 
424 static Authmethod *
425 authmethod_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