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"
47 #include "blacklist_client.h"
51 extern ServerOptions options;
52 extern Buffer loginmsg;
54 static int auth1_process_password(Authctxt *);
55 static int auth1_process_rsa(Authctxt *);
56 static int auth1_process_rhosts_rsa(Authctxt *);
57 static int auth1_process_tis_challenge(Authctxt *);
58 static int auth1_process_tis_response(Authctxt *);
60 static char *client_user = NULL; /* Used to fill in remote user for PAM */
66 int (*method)(Authctxt *);
69 const struct AuthMethod1 auth1_methods[] = {
71 SSH_CMSG_AUTH_PASSWORD, "password",
72 &options.password_authentication, auth1_process_password
75 SSH_CMSG_AUTH_RSA, "rsa",
76 &options.rsa_authentication, auth1_process_rsa
79 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
80 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
83 SSH_CMSG_AUTH_TIS, "challenge-response",
84 &options.challenge_response_authentication,
85 auth1_process_tis_challenge
88 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
89 &options.challenge_response_authentication,
90 auth1_process_tis_response
92 { -1, NULL, NULL, NULL}
95 static const struct AuthMethod1
96 *lookup_authmethod1(int type)
100 for (i = 0; auth1_methods[i].name != NULL; i++)
101 if (auth1_methods[i].type == type)
102 return (&(auth1_methods[i]));
108 get_authname(int type)
110 const struct AuthMethod1 *a;
113 if ((a = lookup_authmethod1(type)) != NULL)
115 snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
121 auth1_process_password(Authctxt *authctxt)
123 int authenticated = 0;
128 * Read user password. It is in plain text, but was
129 * transmitted over the encrypted channel so it is
130 * not visible to an outside observer.
132 password = packet_get_string(&dlen);
135 /* Try authentication with the password. */
136 authenticated = PRIVSEP(auth_password(authctxt, password));
138 explicit_bzero(password, dlen);
141 return (authenticated);
146 auth1_process_rsa(Authctxt *authctxt)
148 int authenticated = 0;
151 /* RSA authentication requested. */
152 if ((n = BN_new()) == NULL)
153 fatal("do_authloop: BN_new failed");
154 packet_get_bignum(n);
156 authenticated = auth_rsa(authctxt, n);
159 return (authenticated);
164 auth1_process_rhosts_rsa(Authctxt *authctxt)
166 int keybits, authenticated = 0;
168 Key *client_host_key;
172 * Get client user name. Note that we just have to
173 * trust the client; root on the client machine can
174 * claim to be any user.
176 client_user = packet_get_cstring(&ulen);
178 /* Get the client host key. */
179 client_host_key = key_new(KEY_RSA1);
180 bits = packet_get_int();
181 packet_get_bignum(client_host_key->rsa->e);
182 packet_get_bignum(client_host_key->rsa->n);
184 keybits = BN_num_bits(client_host_key->rsa->n);
185 if (keybits < 0 || bits != (u_int)keybits) {
186 verbose("Warning: keysize mismatch for client_host_key: "
187 "actual %d, announced %d",
188 BN_num_bits(client_host_key->rsa->n), bits);
192 authenticated = auth_rhosts_rsa(authctxt, client_user,
194 key_free(client_host_key);
196 auth_info(authctxt, "ruser %.100s", client_user);
198 return (authenticated);
203 auth1_process_tis_challenge(Authctxt *authctxt)
207 if ((challenge = get_challenge(authctxt)) == NULL)
210 debug("sending challenge '%s'", challenge);
211 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
212 packet_put_cstring(challenge);
222 auth1_process_tis_response(Authctxt *authctxt)
224 int authenticated = 0;
228 response = packet_get_string(&dlen);
230 authenticated = verify_response(authctxt, response);
231 explicit_bzero(response, dlen);
234 return (authenticated);
238 * read packets, try to authenticate the user and
239 * return only if authentication is successful
242 do_authloop(Authctxt *authctxt)
244 int authenticated = 0;
245 int prev = 0, type = 0;
246 const struct AuthMethod1 *meth;
248 debug("Attempting authentication for %s%.100s.",
249 authctxt->valid ? "" : "invalid user ", authctxt->user);
251 /* If the user has no password, accept authentication immediately. */
252 if (options.permit_empty_passwd && options.password_authentication &&
254 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
256 PRIVSEP(auth_password(authctxt, ""))) {
258 if (options.use_pam && (PRIVSEP(do_pam_account())))
261 auth_log(authctxt, 1, 0, "without authentication",
267 /* Indicate that authentication is needed. */
268 packet_start(SSH_SMSG_FAILURE);
273 /* default to fail */
277 /* Get a packet from the client. */
279 type = packet_read();
282 * If we started challenge-response authentication but the
283 * next packet is not a response to our challenge, release
284 * the resources allocated by get_challenge() (which would
285 * normally have been released by verify_response() had we
286 * received such a response)
288 if (prev == SSH_CMSG_AUTH_TIS &&
289 type != SSH_CMSG_AUTH_TIS_RESPONSE)
290 abandon_challenge_response(authctxt);
292 if (authctxt->failures >= options.max_authtries)
294 if ((meth = lookup_authmethod1(type)) == NULL) {
295 logit("Unknown message during authentication: "
300 if (!*(meth->enabled)) {
301 verbose("%s authentication disabled.", meth->name);
305 authenticated = meth->method(authctxt);
306 if (authenticated == -1)
307 continue; /* "postponed" */
311 auth_close(authctxt->as);
315 if (!authctxt->valid && authenticated)
316 fatal("INTERNAL ERROR: authenticated invalid user %s",
320 if (authenticated && cray_access_denied(authctxt->user)) {
322 fatal("Access denied for user %s.",authctxt->user);
327 /* Special handling for root */
328 if (authenticated && authctxt->pw->pw_uid == 0 &&
329 !auth_root_allowed(meth->name)) {
331 # ifdef SSH_AUDIT_EVENTS
332 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
338 if (options.use_pam && authenticated &&
339 !PRIVSEP(do_pam_account())) {
346 error("Access denied for user %s by PAM account "
347 "configuration", authctxt->user);
348 len = buffer_len(&loginmsg);
349 buffer_append(&loginmsg, "\0", 1);
350 msg = buffer_ptr(&loginmsg);
351 /* strip trailing newlines */
353 while (len > 0 && msg[--len] == '\n')
356 msg = "Access denied.";
357 packet_disconnect("%s", msg);
362 /* Log before sending the reply */
363 auth_log(authctxt, authenticated, 0, get_authname(type), NULL);
371 if (++authctxt->failures >= options.max_authtries) {
372 #ifdef SSH_AUDIT_EVENTS
373 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
375 auth_maxtries_exceeded(authctxt);
378 packet_start(SSH_SMSG_FAILURE);
385 * Performs authentication of an incoming connection. Session key has already
386 * been exchanged and encryption is enabled.
389 do_authentication(Authctxt *authctxt)
392 char *user, *style = NULL;
394 /* Get the name of the user that we wish to log in as. */
395 packet_read_expect(SSH_CMSG_USER);
397 /* Get the user name. */
398 user = packet_get_cstring(&ulen);
401 if ((style = strchr(user, ':')) != NULL)
404 authctxt->user = user;
405 authctxt->style = style;
407 /* Verify that the user is a valid user. */
408 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
411 debug("do_authentication: invalid user %s", user);
412 authctxt->pw = fakepw();
418 /* Configuration may have changed as a result of Match */
419 if (options.num_auth_methods != 0)
420 fatal("AuthenticationMethods is not supported with SSH "
423 setproctitle("%s%s", authctxt->valid ? user : "unknown",
424 use_privsep ? " [net]" : "");
428 PRIVSEP(start_pam(authctxt));
432 * If we are not running as root, the user must have the same uid as
436 if (!use_privsep && getuid() != 0 && authctxt->pw &&
437 authctxt->pw->pw_uid != getuid())
438 packet_disconnect("Cannot change user when server not running as root.");
442 * Loop until the user has been authenticated or the connection is
443 * closed, do_authloop() returns only if authentication is successful
445 do_authloop(authctxt);
447 /* The user has been authenticated and accepted. */
448 packet_start(SSH_SMSG_SUCCESS);
453 #endif /* WITH_SSH1 */