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