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