1 /* $OpenBSD: auth1.c,v 1.80 2014/02/02 03:44:31 djm Exp $ */
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
15 #include <sys/types.h>
23 #include "openbsd-compat/sys-queue.h"
41 #include "monitor_wrap.h"
45 extern ServerOptions options;
46 extern Buffer loginmsg;
48 static int auth1_process_password(Authctxt *);
49 static int auth1_process_rsa(Authctxt *);
50 static int auth1_process_rhosts_rsa(Authctxt *);
51 static int auth1_process_tis_challenge(Authctxt *);
52 static int auth1_process_tis_response(Authctxt *);
54 static char *client_user = NULL; /* Used to fill in remote user for PAM */
60 int (*method)(Authctxt *);
63 const struct AuthMethod1 auth1_methods[] = {
65 SSH_CMSG_AUTH_PASSWORD, "password",
66 &options.password_authentication, auth1_process_password
69 SSH_CMSG_AUTH_RSA, "rsa",
70 &options.rsa_authentication, auth1_process_rsa
73 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
74 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
77 SSH_CMSG_AUTH_TIS, "challenge-response",
78 &options.challenge_response_authentication,
79 auth1_process_tis_challenge
82 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
83 &options.challenge_response_authentication,
84 auth1_process_tis_response
86 { -1, NULL, NULL, NULL}
89 static const struct AuthMethod1
90 *lookup_authmethod1(int type)
94 for (i = 0; auth1_methods[i].name != NULL; i++)
95 if (auth1_methods[i].type == type)
96 return (&(auth1_methods[i]));
102 get_authname(int type)
104 const struct AuthMethod1 *a;
107 if ((a = lookup_authmethod1(type)) != NULL)
109 snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
115 auth1_process_password(Authctxt *authctxt)
117 int authenticated = 0;
122 * Read user password. It is in plain text, but was
123 * transmitted over the encrypted channel so it is
124 * not visible to an outside observer.
126 password = packet_get_string(&dlen);
129 /* Try authentication with the password. */
130 authenticated = PRIVSEP(auth_password(authctxt, password));
132 explicit_bzero(password, dlen);
135 return (authenticated);
140 auth1_process_rsa(Authctxt *authctxt)
142 int authenticated = 0;
145 /* RSA authentication requested. */
146 if ((n = BN_new()) == NULL)
147 fatal("do_authloop: BN_new failed");
148 packet_get_bignum(n);
150 authenticated = auth_rsa(authctxt, n);
153 return (authenticated);
158 auth1_process_rhosts_rsa(Authctxt *authctxt)
160 int keybits, authenticated = 0;
162 Key *client_host_key;
166 * Get client user name. Note that we just have to
167 * trust the client; root on the client machine can
168 * claim to be any user.
170 client_user = packet_get_cstring(&ulen);
172 /* Get the client host key. */
173 client_host_key = key_new(KEY_RSA1);
174 bits = packet_get_int();
175 packet_get_bignum(client_host_key->rsa->e);
176 packet_get_bignum(client_host_key->rsa->n);
178 keybits = BN_num_bits(client_host_key->rsa->n);
179 if (keybits < 0 || bits != (u_int)keybits) {
180 verbose("Warning: keysize mismatch for client_host_key: "
181 "actual %d, announced %d",
182 BN_num_bits(client_host_key->rsa->n), bits);
186 authenticated = auth_rhosts_rsa(authctxt, client_user,
188 key_free(client_host_key);
190 auth_info(authctxt, "ruser %.100s", client_user);
192 return (authenticated);
197 auth1_process_tis_challenge(Authctxt *authctxt)
201 if ((challenge = get_challenge(authctxt)) == NULL)
204 debug("sending challenge '%s'", challenge);
205 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
206 packet_put_cstring(challenge);
216 auth1_process_tis_response(Authctxt *authctxt)
218 int authenticated = 0;
222 response = packet_get_string(&dlen);
224 authenticated = verify_response(authctxt, response);
225 explicit_bzero(response, dlen);
228 return (authenticated);
232 * read packets, try to authenticate the user and
233 * return only if authentication is successful
236 do_authloop(Authctxt *authctxt)
238 int authenticated = 0;
239 int prev = 0, type = 0;
240 const struct AuthMethod1 *meth;
242 debug("Attempting authentication for %s%.100s.",
243 authctxt->valid ? "" : "invalid user ", authctxt->user);
245 /* If the user has no password, accept authentication immediately. */
246 if (options.permit_empty_passwd && options.password_authentication &&
248 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
250 PRIVSEP(auth_password(authctxt, ""))) {
252 if (options.use_pam && (PRIVSEP(do_pam_account())))
255 auth_log(authctxt, 1, 0, "without authentication",
261 /* Indicate that authentication is needed. */
262 packet_start(SSH_SMSG_FAILURE);
267 /* default to fail */
271 /* Get a packet from the client. */
273 type = packet_read();
276 * If we started challenge-response authentication but the
277 * next packet is not a response to our challenge, release
278 * the resources allocated by get_challenge() (which would
279 * normally have been released by verify_response() had we
280 * received such a response)
282 if (prev == SSH_CMSG_AUTH_TIS &&
283 type != SSH_CMSG_AUTH_TIS_RESPONSE)
284 abandon_challenge_response(authctxt);
286 if (authctxt->failures >= options.max_authtries)
288 if ((meth = lookup_authmethod1(type)) == NULL) {
289 logit("Unknown message during authentication: "
294 if (!*(meth->enabled)) {
295 verbose("%s authentication disabled.", meth->name);
299 authenticated = meth->method(authctxt);
300 if (authenticated == -1)
301 continue; /* "postponed" */
305 auth_close(authctxt->as);
309 if (!authctxt->valid && authenticated)
310 fatal("INTERNAL ERROR: authenticated invalid user %s",
314 if (authenticated && cray_access_denied(authctxt->user)) {
316 fatal("Access denied for user %s.",authctxt->user);
321 /* Special handling for root */
322 if (authenticated && authctxt->pw->pw_uid == 0 &&
323 !auth_root_allowed(meth->name)) {
325 # ifdef SSH_AUDIT_EVENTS
326 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
332 if (options.use_pam && authenticated &&
333 !PRIVSEP(do_pam_account())) {
337 error("Access denied for user %s by PAM account "
338 "configuration", authctxt->user);
339 len = buffer_len(&loginmsg);
340 buffer_append(&loginmsg, "\0", 1);
341 msg = buffer_ptr(&loginmsg);
342 /* strip trailing newlines */
344 while (len > 0 && msg[--len] == '\n')
347 msg = "Access denied.";
348 packet_disconnect("%s", msg);
353 /* Log before sending the reply */
354 auth_log(authctxt, authenticated, 0, get_authname(type), NULL);
362 if (++authctxt->failures >= options.max_authtries) {
363 #ifdef SSH_AUDIT_EVENTS
364 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
366 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
369 packet_start(SSH_SMSG_FAILURE);
376 * Performs authentication of an incoming connection. Session key has already
377 * been exchanged and encryption is enabled.
380 do_authentication(Authctxt *authctxt)
383 char *user, *style = NULL;
385 /* Get the name of the user that we wish to log in as. */
386 packet_read_expect(SSH_CMSG_USER);
388 /* Get the user name. */
389 user = packet_get_cstring(&ulen);
392 if ((style = strchr(user, ':')) != NULL)
395 authctxt->user = user;
396 authctxt->style = style;
398 /* Verify that the user is a valid user. */
399 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
402 debug("do_authentication: invalid user %s", user);
403 authctxt->pw = fakepw();
406 /* Configuration may have changed as a result of Match */
407 if (options.num_auth_methods != 0)
408 fatal("AuthenticationMethods is not supported with SSH "
411 setproctitle("%s%s", authctxt->valid ? user : "unknown",
412 use_privsep ? " [net]" : "");
416 PRIVSEP(start_pam(authctxt));
420 * If we are not running as root, the user must have the same uid as
424 if (!use_privsep && getuid() != 0 && authctxt->pw &&
425 authctxt->pw->pw_uid != getuid())
426 packet_disconnect("Cannot change user when server not running as root.");
430 * Loop until the user has been authenticated or the connection is
431 * closed, do_authloop() returns only if authentication is successful
433 do_authloop(authctxt);
435 /* The user has been authenticated and accepted. */
436 packet_start(SSH_SMSG_SUCCESS);