xref: /titanic_50/usr/src/cmd/ssh/sshd/auth1.c (revision e07d9cb85217949d497b02d7211de8a197d2f2eb)
1 /*
2  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3  *                    All rights reserved
4  *
5  * As far as I am concerned, the code I have written for this software
6  * can be used freely for any purpose.  Any derived versions of this
7  * software must be clearly marked as such, and if the derived work is
8  * incompatible with the protocol description in the RFC file, it must be
9  * called by a name other than "ssh" or "Secure Shell".
10  */
11 /*
12  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
13  * Use is subject to license terms.
14  */
15 
16 #include "includes.h"
17 RCSID("$OpenBSD: auth1.c,v 1.44 2002/09/26 11:38:43 markus Exp $");
18 
19 #pragma ident	"%Z%%M%	%I%	%E% SMI"
20 
21 #include "xmalloc.h"
22 #include "rsa.h"
23 #include "ssh1.h"
24 #include "packet.h"
25 #include "buffer.h"
26 #include "mpaux.h"
27 #include "log.h"
28 #include "servconf.h"
29 #include "compat.h"
30 #include "auth.h"
31 #include "channels.h"
32 #include "session.h"
33 #include "uidswap.h"
34 #include "monitor_wrap.h"
35 
36 #ifdef HAVE_BSM
37 #include "bsmaudit.h"
38 extern adt_session_data_t *ah;
39 #endif /* HAVE_BSM */
40 
41 /* import */
42 extern ServerOptions options;
43 
44 /*
45  * convert ssh auth msg type into description
46  */
47 static char *
48 get_authname(int type)
49 {
50 	static char buf[1024];
51 	switch (type) {
52 	case SSH_CMSG_AUTH_PASSWORD:
53 		return "password";
54 	case SSH_CMSG_AUTH_RSA:
55 		return "rsa";
56 	case SSH_CMSG_AUTH_RHOSTS_RSA:
57 		return "rhosts-rsa";
58 	case SSH_CMSG_AUTH_RHOSTS:
59 		return "rhosts";
60 	case SSH_CMSG_AUTH_TIS:
61 	case SSH_CMSG_AUTH_TIS_RESPONSE:
62 		return "challenge-response";
63 #if defined(KRB4) || defined(KRB5)
64 	case SSH_CMSG_AUTH_KERBEROS:
65 		return "kerberos";
66 #endif
67 	}
68 	snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
69 	return buf;
70 }
71 
72 /*
73  * read packets, try to authenticate the user and
74  * return only if authentication is successful
75  */
76 static void
77 do_authloop(Authctxt *authctxt)
78 {
79 	int authenticated = 0;
80 	u_int bits;
81 	Key *client_host_key;
82 	BIGNUM *n;
83 	char *client_user, *password;
84 	char info[1024];
85 	u_int dlen;
86 	u_int ulen;
87 	int type = 0;
88 	struct passwd *pw = authctxt->pw;
89 
90 	debug("Attempting authentication for %s%.100s.",
91 	    authctxt->valid ? "" : "illegal user ", authctxt->user);
92 
93 	/* If the user has no password, accept authentication immediately. */
94 	if (options.password_authentication &&
95 #if defined(KRB4) || defined(KRB5)
96 	    (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
97 #endif
98 	    PRIVSEP(auth_password(authctxt, ""))) {
99 		auth_log(authctxt, 1, "without authentication", "");
100 		return;
101 	}
102 
103 	/* Indicate that authentication is needed. */
104 	packet_start(SSH_SMSG_FAILURE);
105 	packet_send();
106 	packet_write_wait();
107 
108 	client_user = NULL;
109 
110 	for ( ;; ) {
111 		/* default to fail */
112 		authenticated = 0;
113 
114 		info[0] = '\0';
115 
116 		/* Get a packet from the client. */
117 		authctxt->v1_auth_type = type = packet_read();
118 		authctxt->v1_auth_name = get_authname(type);
119 
120 		authctxt->attempt++;
121 
122 		/* Process the packet. */
123 		switch (type) {
124 
125 #if defined(KRB4) || defined(KRB5)
126 		case SSH_CMSG_AUTH_KERBEROS:
127 			if (!options.kerberos_authentication) {
128 				verbose("Kerberos authentication disabled.");
129 			} else {
130 				char *kdata = packet_get_string(&dlen);
131 				packet_check_eom();
132 
133 				if (kdata[0] == 4) { /* KRB_PROT_VERSION */
134 #ifdef KRB4
135 					KTEXT_ST tkt, reply;
136 					tkt.length = dlen;
137 					if (tkt.length < MAX_KTXT_LEN)
138 						memcpy(tkt.dat, kdata, tkt.length);
139 
140 					if (PRIVSEP(auth_krb4(authctxt, &tkt,
141 					    &client_user, &reply))) {
142 						authenticated = 1;
143 						snprintf(info, sizeof(info),
144 						    " tktuser %.100s",
145 						    client_user);
146 
147 						packet_start(
148 						    SSH_SMSG_AUTH_KERBEROS_RESPONSE);
149 						packet_put_string((char *)
150 						    reply.dat, reply.length);
151 						packet_send();
152 						packet_write_wait();
153 					}
154 #endif /* KRB4 */
155 				} else {
156 #ifdef KRB5
157 					krb5_data tkt, reply;
158 					tkt.length = dlen;
159 					tkt.data = kdata;
160 
161 					if (PRIVSEP(auth_krb5(authctxt, &tkt,
162 					    &client_user, &reply))) {
163 						authenticated = 1;
164 						snprintf(info, sizeof(info),
165 						    " tktuser %.100s",
166 						    client_user);
167 
168  						/* Send response to client */
169  						packet_start(
170 						    SSH_SMSG_AUTH_KERBEROS_RESPONSE);
171  						packet_put_string((char *)
172 						    reply.data, reply.length);
173  						packet_send();
174  						packet_write_wait();
175 
176  						if (reply.length)
177  							xfree(reply.data);
178 					}
179 #endif /* KRB5 */
180 				}
181 				xfree(kdata);
182 			}
183 			break;
184 #endif /* KRB4 || KRB5 */
185 
186 #if defined(AFS) || defined(KRB5)
187 			/* XXX - punt on backward compatibility here. */
188 		case SSH_CMSG_HAVE_KERBEROS_TGT:
189 			packet_send_debug("Kerberos TGT passing disabled before authentication.");
190 			break;
191 #ifdef AFS
192 		case SSH_CMSG_HAVE_AFS_TOKEN:
193 			packet_send_debug("AFS token passing disabled before authentication.");
194 			break;
195 #endif /* AFS */
196 #endif /* AFS || KRB5 */
197 
198 		case SSH_CMSG_AUTH_RHOSTS:
199 			if (!options.rhosts_authentication) {
200 				verbose("Rhosts authentication disabled.");
201 				break;
202 			}
203 			/*
204 			 * Get client user name.  Note that we just have to
205 			 * trust the client; this is one reason why rhosts
206 			 * authentication is insecure. (Another is
207 			 * IP-spoofing on a local network.)
208 			 */
209 			client_user = packet_get_string(&ulen);
210 			packet_check_eom();
211 
212 			/* Try to authenticate using /etc/hosts.equiv and .rhosts. */
213 			authenticated = auth_rhosts(pw, client_user);
214 
215 			snprintf(info, sizeof info, " ruser %.100s", client_user);
216 			break;
217 
218 		case SSH_CMSG_AUTH_RHOSTS_RSA:
219 			if (!options.rhosts_rsa_authentication) {
220 				verbose("Rhosts with RSA authentication disabled.");
221 				break;
222 			}
223 			/*
224 			 * Get client user name.  Note that we just have to
225 			 * trust the client; root on the client machine can
226 			 * claim to be any user.
227 			 */
228 			client_user = packet_get_string(&ulen);
229 
230 			/* Get the client host key. */
231 			client_host_key = key_new(KEY_RSA1);
232 			bits = packet_get_int();
233 			packet_get_bignum(client_host_key->rsa->e);
234 			packet_get_bignum(client_host_key->rsa->n);
235 
236 			if (bits != BN_num_bits(client_host_key->rsa->n))
237 				verbose("Warning: keysize mismatch for client_host_key: "
238 				    "actual %d, announced %d",
239 				    BN_num_bits(client_host_key->rsa->n), bits);
240 			packet_check_eom();
241 
242 			authenticated = auth_rhosts_rsa(pw, client_user,
243 			    client_host_key);
244 			key_free(client_host_key);
245 
246 			snprintf(info, sizeof info, " ruser %.100s", client_user);
247 			break;
248 
249 		case SSH_CMSG_AUTH_RSA:
250 			if (!options.rsa_authentication) {
251 				verbose("RSA authentication disabled.");
252 				break;
253 			}
254 			/* RSA authentication requested. */
255 			if ((n = BN_new()) == NULL)
256 				fatal("do_authloop: BN_new failed");
257 			packet_get_bignum(n);
258 			packet_check_eom();
259 			authenticated = auth_rsa(pw, n);
260 			BN_clear_free(n);
261 			break;
262 
263 		case SSH_CMSG_AUTH_PASSWORD:
264 			authctxt->init_attempt++;
265 
266 			if (!options.password_authentication) {
267 				verbose("Password authentication disabled.");
268 				break;
269 			}
270 			/*
271 			 * Read user password.  It is in plain text, but was
272 			 * transmitted over the encrypted channel so it is
273 			 * not visible to an outside observer.
274 			 */
275 			password = packet_get_string(&dlen);
276 			packet_check_eom();
277 
278 			/* Try authentication with the password. */
279 			if (authctxt->init_failures <
280 				options.max_init_auth_tries)
281 				authenticated =
282 				    PRIVSEP(auth_password(authctxt,
283 						password));
284 
285 			memset(password, 0, strlen(password));
286 			xfree(password);
287 			break;
288 
289 		case SSH_CMSG_AUTH_TIS:
290 			debug("rcvd SSH_CMSG_AUTH_TIS");
291 			if (options.challenge_response_authentication == 1) {
292 				char *challenge = get_challenge(authctxt);
293 				if (challenge != NULL) {
294 					debug("sending challenge '%s'", challenge);
295 					packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
296 					packet_put_cstring(challenge);
297 					xfree(challenge);
298 					packet_send();
299 					packet_write_wait();
300 					continue;
301 				}
302 			}
303 			break;
304 		case SSH_CMSG_AUTH_TIS_RESPONSE:
305 			debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
306 			if (options.challenge_response_authentication == 1) {
307 				char *response = packet_get_string(&dlen);
308 				debug("got response '%s'", response);
309 				packet_check_eom();
310 				authenticated = verify_response(authctxt, response);
311 				memset(response, 'r', dlen);
312 				xfree(response);
313 			}
314 			break;
315 
316 		default:
317 			/*
318 			 * Any unknown messages will be ignored (and failure
319 			 * returned) during authentication.
320 			 */
321 			log("Unknown message during authentication: type %d", type);
322 			break;
323 		}
324 #ifdef BSD_AUTH
325 		if (authctxt->as) {
326 			auth_close(authctxt->as);
327 			authctxt->as = NULL;
328 		}
329 #endif
330 		if (!authctxt->valid && authenticated) {
331 			authenticated = 0;
332 			log("Ignoring authenticated invalid user %s",
333 			    authctxt->user);
334 		}
335 
336 #ifdef _UNICOS
337 		if (type == SSH_CMSG_AUTH_PASSWORD && !authenticated)
338 			cray_login_failure(authctxt->user, IA_UDBERR);
339 		if (authenticated && cray_access_denied(authctxt->user)) {
340 			authenticated = 0;
341 			fatal("Access denied for user %s.",authctxt->user);
342 		}
343 #endif /* _UNICOS */
344 
345 #ifdef HAVE_CYGWIN
346 		if (authenticated &&
347 		    !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, pw)) {
348 			packet_disconnect("Authentication rejected for uid %d.",
349 			pw == NULL ? -1 : pw->pw_uid);
350 			authenticated = 0;
351 		}
352 #else
353 		/* Special handling for root */
354 		if (!use_privsep &&
355 		    authenticated && authctxt->pw->pw_uid == 0 &&
356 		    !auth_root_allowed(get_authname(type)))
357 			authenticated = 0;
358 #endif
359 #ifdef USE_PAM
360 		/* XXX PAM and PRIVSEP don't mix */
361 		if (use_privsep && authenticated)
362 			fatal("Privsep is not supported");
363 
364 		if (authenticated && type != SSH_CMSG_AUTH_PASSWORD)
365 			authenticated = do_pam_non_initial_userauth(authctxt);
366 		else if (authenticated && !AUTHPAM_DONE(authctxt))
367 			authenticated = 0;
368 
369 		if (!authenticated)
370 			authctxt->pam_retval = AUTHPAM_ERROR(authctxt,
371 				PAM_PERM_DENIED);
372 #endif /* USE_PAM */
373 
374 		/* Log before sending the reply */
375 		auth_log(authctxt, authenticated, get_authname(type), info);
376 
377 		if (client_user != NULL) {
378 			xfree(client_user);
379 			client_user = NULL;
380 		}
381 
382 		if (authenticated)
383 			return;
384 
385 		if (type == SSH_CMSG_AUTH_PASSWORD)
386 			authctxt->init_failures++;
387 
388 		if (authctxt->failures++ > options.max_auth_tries) {
389 #ifdef HAVE_BSM
390 			fatal_remove_cleanup(audit_failed_login_cleanup,
391 				authctxt);
392 			audit_sshd_login_failure(&ah, PAM_MAXTRIES);
393 #endif /* HAVE_BSM */
394 			packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
395 		}
396 
397 		packet_start(SSH_SMSG_FAILURE);
398 		packet_send();
399 		packet_write_wait();
400 	}
401 }
402 
403 /*
404  * Performs authentication of an incoming connection.  Session key has already
405  * been exchanged and encryption is enabled.
406  */
407 Authctxt *
408 do_authentication(void)
409 {
410 	Authctxt *authctxt;
411 	u_int ulen;
412 	char *user, *style = NULL;
413 
414 	/* Get the name of the user that we wish to log in as. */
415 	packet_read_expect(SSH_CMSG_USER);
416 
417 	/* Get the user name. */
418 	user = packet_get_string(&ulen);
419 	packet_check_eom();
420 
421 	if ((style = strchr(user, ':')) != NULL)
422 		*style++ = '\0';
423 
424 #ifdef KRB5
425 	/* XXX - SSH.com Kerberos v5 braindeath. */
426 	if ((datafellows & SSH_BUG_K5USER) &&
427 	    options.kerberos_authentication) {
428 		char *p;
429 		if ((p = strchr(user, '@')) != NULL)
430 			*p = '\0';
431 	}
432 #endif
433 
434 	authctxt = authctxt_new();
435 	authctxt->user = user;
436 	authctxt->style = style;
437 
438 #ifdef HAVE_BSM
439 	fatal_add_cleanup(audit_failed_login_cleanup, authctxt);
440 #endif /* HAVE_BSM */
441 
442 	/* Verify that the user is a valid user. */
443 	if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL) {
444 		authctxt->valid = 1;
445 	} else {
446 		authctxt->valid = 0;
447 		debug("do_authentication: illegal user %s", user);
448 	}
449 
450 	setproctitle("%s%s", authctxt->pw ? user : "unknown",
451 	    use_privsep ? " [net]" : "");
452 
453 #if 0
454 #ifdef USE_PAM
455 	PRIVSEP(start_pam(authctxt->pw == NULL ? "NOUSER" : user));
456 #endif
457 #endif
458 
459 	/*
460 	 * If we are not running as root, the user must have the same uid as
461 	 * the server. (Unless you are running Windows)
462 	 */
463 #ifndef HAVE_CYGWIN
464 	if (!use_privsep && getuid() != 0 && authctxt->pw &&
465 	    authctxt->pw->pw_uid != getuid())
466 		packet_disconnect("Cannot change user when server not running as root.");
467 #endif
468 
469 	/*
470 	 * Loop until the user has been authenticated or the connection is
471 	 * closed, do_authloop() returns only if authentication is successful
472 	 */
473 	do_authloop(authctxt);
474 
475 	/* The user has been authenticated and accepted. */
476 	packet_start(SSH_SMSG_SUCCESS);
477 	packet_send();
478 	packet_write_wait();
479 
480 	return (authctxt);
481 }
482