1 /* $OpenBSD: auth1.c,v 1.82 2014/07/15 15:54:14 millert 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".
17 #include <sys/types.h>
25 #include "openbsd-compat/sys-queue.h"
44 #include "monitor_wrap.h"
46 #include "blacklist_client.h"
49 extern ServerOptions options;
50 extern Buffer loginmsg;
52 static int auth1_process_password(Authctxt *);
53 static int auth1_process_rsa(Authctxt *);
54 static int auth1_process_rhosts_rsa(Authctxt *);
55 static int auth1_process_tis_challenge(Authctxt *);
56 static int auth1_process_tis_response(Authctxt *);
58 static char *client_user = NULL; /* Used to fill in remote user for PAM */
64 int (*method)(Authctxt *);
67 const struct AuthMethod1 auth1_methods[] = {
69 SSH_CMSG_AUTH_PASSWORD, "password",
70 &options.password_authentication, auth1_process_password
73 SSH_CMSG_AUTH_RSA, "rsa",
74 &options.rsa_authentication, auth1_process_rsa
77 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
78 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
81 SSH_CMSG_AUTH_TIS, "challenge-response",
82 &options.challenge_response_authentication,
83 auth1_process_tis_challenge
86 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
87 &options.challenge_response_authentication,
88 auth1_process_tis_response
90 { -1, NULL, NULL, NULL}
93 static const struct AuthMethod1
94 *lookup_authmethod1(int type)
98 for (i = 0; auth1_methods[i].name != NULL; i++)
99 if (auth1_methods[i].type == type)
100 return (&(auth1_methods[i]));
106 get_authname(int type)
108 const struct AuthMethod1 *a;
111 if ((a = lookup_authmethod1(type)) != NULL)
113 snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
119 auth1_process_password(Authctxt *authctxt)
121 int authenticated = 0;
126 * Read user password. It is in plain text, but was
127 * transmitted over the encrypted channel so it is
128 * not visible to an outside observer.
130 password = packet_get_string(&dlen);
133 /* Try authentication with the password. */
134 authenticated = PRIVSEP(auth_password(authctxt, password));
136 explicit_bzero(password, dlen);
139 return (authenticated);
144 auth1_process_rsa(Authctxt *authctxt)
146 int authenticated = 0;
149 /* RSA authentication requested. */
150 if ((n = BN_new()) == NULL)
151 fatal("do_authloop: BN_new failed");
152 packet_get_bignum(n);
154 authenticated = auth_rsa(authctxt, n);
157 return (authenticated);
162 auth1_process_rhosts_rsa(Authctxt *authctxt)
164 int keybits, authenticated = 0;
166 Key *client_host_key;
170 * Get client user name. Note that we just have to
171 * trust the client; root on the client machine can
172 * claim to be any user.
174 client_user = packet_get_cstring(&ulen);
176 /* Get the client host key. */
177 client_host_key = key_new(KEY_RSA1);
178 bits = packet_get_int();
179 packet_get_bignum(client_host_key->rsa->e);
180 packet_get_bignum(client_host_key->rsa->n);
182 keybits = BN_num_bits(client_host_key->rsa->n);
183 if (keybits < 0 || bits != (u_int)keybits) {
184 verbose("Warning: keysize mismatch for client_host_key: "
185 "actual %d, announced %d",
186 BN_num_bits(client_host_key->rsa->n), bits);
190 authenticated = auth_rhosts_rsa(authctxt, client_user,
192 key_free(client_host_key);
194 auth_info(authctxt, "ruser %.100s", client_user);
196 return (authenticated);
201 auth1_process_tis_challenge(Authctxt *authctxt)
205 if ((challenge = get_challenge(authctxt)) == NULL)
208 debug("sending challenge '%s'", challenge);
209 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
210 packet_put_cstring(challenge);
220 auth1_process_tis_response(Authctxt *authctxt)
222 int authenticated = 0;
226 response = packet_get_string(&dlen);
228 authenticated = verify_response(authctxt, response);
229 explicit_bzero(response, dlen);
232 return (authenticated);
236 * read packets, try to authenticate the user and
237 * return only if authentication is successful
240 do_authloop(Authctxt *authctxt)
242 int authenticated = 0;
243 int prev = 0, type = 0;
244 const struct AuthMethod1 *meth;
246 debug("Attempting authentication for %s%.100s.",
247 authctxt->valid ? "" : "invalid user ", authctxt->user);
249 /* If the user has no password, accept authentication immediately. */
250 if (options.permit_empty_passwd && options.password_authentication &&
252 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
254 PRIVSEP(auth_password(authctxt, ""))) {
256 if (options.use_pam && (PRIVSEP(do_pam_account())))
259 auth_log(authctxt, 1, 0, "without authentication",
265 /* Indicate that authentication is needed. */
266 packet_start(SSH_SMSG_FAILURE);
271 /* default to fail */
275 /* Get a packet from the client. */
277 type = packet_read();
280 * If we started challenge-response authentication but the
281 * next packet is not a response to our challenge, release
282 * the resources allocated by get_challenge() (which would
283 * normally have been released by verify_response() had we
284 * received such a response)
286 if (prev == SSH_CMSG_AUTH_TIS &&
287 type != SSH_CMSG_AUTH_TIS_RESPONSE)
288 abandon_challenge_response(authctxt);
290 if (authctxt->failures >= options.max_authtries)
292 if ((meth = lookup_authmethod1(type)) == NULL) {
293 logit("Unknown message during authentication: "
298 if (!*(meth->enabled)) {
299 verbose("%s authentication disabled.", meth->name);
303 authenticated = meth->method(authctxt);
304 if (authenticated == -1)
305 continue; /* "postponed" */
309 auth_close(authctxt->as);
313 if (!authctxt->valid && authenticated)
314 fatal("INTERNAL ERROR: authenticated invalid user %s",
318 if (authenticated && cray_access_denied(authctxt->user)) {
320 fatal("Access denied for user %s.",authctxt->user);
325 /* Special handling for root */
326 if (authenticated && authctxt->pw->pw_uid == 0 &&
327 !auth_root_allowed(meth->name)) {
329 # ifdef SSH_AUDIT_EVENTS
330 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
336 if (options.use_pam && authenticated &&
337 !PRIVSEP(do_pam_account())) {
341 BLACKLIST_NOTIFY(BLACKLIST_AUTH_FAIL);
342 error("Access denied for user %s by PAM account "
343 "configuration", authctxt->user);
344 len = buffer_len(&loginmsg);
345 buffer_append(&loginmsg, "\0", 1);
346 msg = buffer_ptr(&loginmsg);
347 /* strip trailing newlines */
349 while (len > 0 && msg[--len] == '\n')
352 msg = "Access denied.";
353 packet_disconnect("%s", msg);
358 /* Log before sending the reply */
359 auth_log(authctxt, authenticated, 0, get_authname(type), NULL);
367 if (++authctxt->failures >= options.max_authtries) {
368 #ifdef SSH_AUDIT_EVENTS
369 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
371 auth_maxtries_exceeded(authctxt);
374 packet_start(SSH_SMSG_FAILURE);
381 * Performs authentication of an incoming connection. Session key has already
382 * been exchanged and encryption is enabled.
385 do_authentication(Authctxt *authctxt)
388 char *user, *style = NULL;
390 /* Get the name of the user that we wish to log in as. */
391 packet_read_expect(SSH_CMSG_USER);
393 /* Get the user name. */
394 user = packet_get_cstring(&ulen);
397 if ((style = strchr(user, ':')) != NULL)
400 authctxt->user = user;
401 authctxt->style = style;
403 /* Verify that the user is a valid user. */
404 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
407 debug("do_authentication: invalid user %s", user);
408 authctxt->pw = fakepw();
409 BLACKLIST_NOTIFY(BLACKLIST_AUTH_FAIL);
412 /* Configuration may have changed as a result of Match */
413 if (options.num_auth_methods != 0)
414 fatal("AuthenticationMethods is not supported with SSH "
417 setproctitle("%s%s", authctxt->valid ? user : "unknown",
418 use_privsep ? " [net]" : "");
422 PRIVSEP(start_pam(authctxt));
426 * If we are not running as root, the user must have the same uid as
430 if (!use_privsep && getuid() != 0 && authctxt->pw &&
431 authctxt->pw->pw_uid != getuid())
432 packet_disconnect("Cannot change user when server not running as root.");
436 * Loop until the user has been authenticated or the connection is
437 * closed, do_authloop() returns only if authentication is successful
439 do_authloop(authctxt);
441 /* The user has been authenticated and accepted. */
442 packet_start(SSH_SMSG_SUCCESS);
447 #endif /* WITH_SSH1 */