1 /* $OpenBSD: auth1.c,v 1.68 2006/07/22 20:48:22 stevesk 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>
32 #include "monitor_wrap.h"
36 extern ServerOptions options;
37 extern Buffer loginmsg;
39 static int auth1_process_password(Authctxt *, char *, size_t);
40 static int auth1_process_rsa(Authctxt *, char *, size_t);
41 static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t);
42 static int auth1_process_tis_challenge(Authctxt *, char *, size_t);
43 static int auth1_process_tis_response(Authctxt *, char *, size_t);
45 static char *client_user = NULL; /* Used to fill in remote user for PAM */
51 int (*method)(Authctxt *, char *, size_t);
54 const struct AuthMethod1 auth1_methods[] = {
56 SSH_CMSG_AUTH_PASSWORD, "password",
57 &options.password_authentication, auth1_process_password
60 SSH_CMSG_AUTH_RSA, "rsa",
61 &options.rsa_authentication, auth1_process_rsa
64 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
65 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
68 SSH_CMSG_AUTH_TIS, "challenge-response",
69 &options.challenge_response_authentication,
70 auth1_process_tis_challenge
73 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
74 &options.challenge_response_authentication,
75 auth1_process_tis_response
77 { -1, NULL, NULL, NULL}
80 static const struct AuthMethod1
81 *lookup_authmethod1(int type)
85 for (i = 0; auth1_methods[i].name != NULL; i++)
86 if (auth1_methods[i].type == type)
87 return (&(auth1_methods[i]));
93 get_authname(int type)
95 const struct AuthMethod1 *a;
98 if ((a = lookup_authmethod1(type)) != NULL)
100 snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
106 auth1_process_password(Authctxt *authctxt, char *info, size_t infolen)
108 int authenticated = 0;
113 * Read user password. It is in plain text, but was
114 * transmitted over the encrypted channel so it is
115 * not visible to an outside observer.
117 password = packet_get_string(&dlen);
120 /* Try authentication with the password. */
121 authenticated = PRIVSEP(auth_password(authctxt, password));
123 memset(password, 0, dlen);
126 return (authenticated);
131 auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen)
133 int authenticated = 0;
136 /* RSA authentication requested. */
137 if ((n = BN_new()) == NULL)
138 fatal("do_authloop: BN_new failed");
139 packet_get_bignum(n);
141 authenticated = auth_rsa(authctxt, n);
144 return (authenticated);
149 auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen)
151 int keybits, authenticated = 0;
153 Key *client_host_key;
157 * Get client user name. Note that we just have to
158 * trust the client; root on the client machine can
159 * claim to be any user.
161 client_user = packet_get_string(&ulen);
163 /* Get the client host key. */
164 client_host_key = key_new(KEY_RSA1);
165 bits = packet_get_int();
166 packet_get_bignum(client_host_key->rsa->e);
167 packet_get_bignum(client_host_key->rsa->n);
169 keybits = BN_num_bits(client_host_key->rsa->n);
170 if (keybits < 0 || bits != (u_int)keybits) {
171 verbose("Warning: keysize mismatch for client_host_key: "
172 "actual %d, announced %d",
173 BN_num_bits(client_host_key->rsa->n), bits);
177 authenticated = auth_rhosts_rsa(authctxt, client_user,
179 key_free(client_host_key);
181 snprintf(info, infolen, " ruser %.100s", client_user);
183 return (authenticated);
188 auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen)
192 if ((challenge = get_challenge(authctxt)) == NULL)
195 debug("sending challenge '%s'", challenge);
196 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
197 packet_put_cstring(challenge);
207 auth1_process_tis_response(Authctxt *authctxt, char *info, size_t infolen)
209 int authenticated = 0;
213 response = packet_get_string(&dlen);
215 authenticated = verify_response(authctxt, response);
216 memset(response, 'r', dlen);
219 return (authenticated);
223 * read packets, try to authenticate the user and
224 * return only if authentication is successful
227 do_authloop(Authctxt *authctxt)
229 int authenticated = 0;
231 int prev = 0, type = 0;
232 const struct AuthMethod1 *meth;
234 debug("Attempting authentication for %s%.100s.",
235 authctxt->valid ? "" : "invalid user ", authctxt->user);
237 /* If the user has no password, accept authentication immediately. */
238 if (options.password_authentication &&
240 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
242 PRIVSEP(auth_password(authctxt, ""))) {
244 if (options.use_pam && (PRIVSEP(do_pam_account())))
247 auth_log(authctxt, 1, "without authentication", "");
252 /* Indicate that authentication is needed. */
253 packet_start(SSH_SMSG_FAILURE);
258 /* default to fail */
263 /* Get a packet from the client. */
265 type = packet_read();
268 * If we started challenge-response authentication but the
269 * next packet is not a response to our challenge, release
270 * the resources allocated by get_challenge() (which would
271 * normally have been released by verify_response() had we
272 * received such a response)
274 if (prev == SSH_CMSG_AUTH_TIS &&
275 type != SSH_CMSG_AUTH_TIS_RESPONSE)
276 abandon_challenge_response(authctxt);
278 if ((meth = lookup_authmethod1(type)) == NULL) {
279 logit("Unknown message during authentication: "
284 if (!*(meth->enabled)) {
285 verbose("%s authentication disabled.", meth->name);
289 authenticated = meth->method(authctxt, info, sizeof(info));
290 if (authenticated == -1)
291 continue; /* "postponed" */
295 auth_close(authctxt->as);
299 if (!authctxt->valid && authenticated)
300 fatal("INTERNAL ERROR: authenticated invalid user %s",
304 if (authenticated && cray_access_denied(authctxt->user)) {
306 fatal("Access denied for user %s.",authctxt->user);
312 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,
314 packet_disconnect("Authentication rejected for uid %d.",
315 authctxt->pw == NULL ? -1 : authctxt->pw->pw_uid);
319 /* Special handling for root */
320 if (authenticated && authctxt->pw->pw_uid == 0 &&
321 !auth_root_allowed(meth->name)) {
323 # ifdef SSH_AUDIT_EVENTS
324 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
330 if (options.use_pam && authenticated &&
331 !PRIVSEP(do_pam_account())) {
335 error("Access denied for user %s by PAM account "
336 "configuration", authctxt->user);
337 len = buffer_len(&loginmsg);
338 buffer_append(&loginmsg, "\0", 1);
339 msg = buffer_ptr(&loginmsg);
340 /* strip trailing newlines */
342 while (len > 0 && msg[--len] == '\n')
345 msg = "Access denied.";
346 packet_disconnect(msg);
351 /* Log before sending the reply */
352 auth_log(authctxt, authenticated, get_authname(type), info);
354 if (client_user != 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_string(&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 setproctitle("%s%s", authctxt->valid ? user : "unknown",
407 use_privsep ? " [net]" : "");
411 PRIVSEP(start_pam(authctxt));
415 * If we are not running as root, the user must have the same uid as
419 if (!use_privsep && getuid() != 0 && authctxt->pw &&
420 authctxt->pw->pw_uid != getuid())
421 packet_disconnect("Cannot change user when server not running as root.");
425 * Loop until the user has been authenticated or the connection is
426 * closed, do_authloop() returns only if authentication is successful
428 do_authloop(authctxt);
430 /* The user has been authenticated and accepted. */
431 packet_start(SSH_SMSG_SUCCESS);