1 /* $OpenBSD: auth1.c,v 1.70 2006/08/03 03:34:41 deraadt 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>
39 #include "monitor_wrap.h"
43 extern ServerOptions options;
44 extern Buffer loginmsg;
46 static int auth1_process_password(Authctxt *, char *, size_t);
47 static int auth1_process_rsa(Authctxt *, char *, size_t);
48 static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t);
49 static int auth1_process_tis_challenge(Authctxt *, char *, size_t);
50 static int auth1_process_tis_response(Authctxt *, char *, size_t);
52 static char *client_user = NULL; /* Used to fill in remote user for PAM */
58 int (*method)(Authctxt *, char *, size_t);
61 const struct AuthMethod1 auth1_methods[] = {
63 SSH_CMSG_AUTH_PASSWORD, "password",
64 &options.password_authentication, auth1_process_password
67 SSH_CMSG_AUTH_RSA, "rsa",
68 &options.rsa_authentication, auth1_process_rsa
71 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
72 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
75 SSH_CMSG_AUTH_TIS, "challenge-response",
76 &options.challenge_response_authentication,
77 auth1_process_tis_challenge
80 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
81 &options.challenge_response_authentication,
82 auth1_process_tis_response
84 { -1, NULL, NULL, NULL}
87 static const struct AuthMethod1
88 *lookup_authmethod1(int type)
92 for (i = 0; auth1_methods[i].name != NULL; i++)
93 if (auth1_methods[i].type == type)
94 return (&(auth1_methods[i]));
100 get_authname(int type)
102 const struct AuthMethod1 *a;
105 if ((a = lookup_authmethod1(type)) != NULL)
107 snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
113 auth1_process_password(Authctxt *authctxt, char *info, size_t infolen)
115 int authenticated = 0;
120 * Read user password. It is in plain text, but was
121 * transmitted over the encrypted channel so it is
122 * not visible to an outside observer.
124 password = packet_get_string(&dlen);
127 /* Try authentication with the password. */
128 authenticated = PRIVSEP(auth_password(authctxt, password));
130 memset(password, 0, dlen);
133 return (authenticated);
138 auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen)
140 int authenticated = 0;
143 /* RSA authentication requested. */
144 if ((n = BN_new()) == NULL)
145 fatal("do_authloop: BN_new failed");
146 packet_get_bignum(n);
148 authenticated = auth_rsa(authctxt, n);
151 return (authenticated);
156 auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen)
158 int keybits, authenticated = 0;
160 Key *client_host_key;
164 * Get client user name. Note that we just have to
165 * trust the client; root on the client machine can
166 * claim to be any user.
168 client_user = packet_get_string(&ulen);
170 /* Get the client host key. */
171 client_host_key = key_new(KEY_RSA1);
172 bits = packet_get_int();
173 packet_get_bignum(client_host_key->rsa->e);
174 packet_get_bignum(client_host_key->rsa->n);
176 keybits = BN_num_bits(client_host_key->rsa->n);
177 if (keybits < 0 || bits != (u_int)keybits) {
178 verbose("Warning: keysize mismatch for client_host_key: "
179 "actual %d, announced %d",
180 BN_num_bits(client_host_key->rsa->n), bits);
184 authenticated = auth_rhosts_rsa(authctxt, client_user,
186 key_free(client_host_key);
188 snprintf(info, infolen, " ruser %.100s", client_user);
190 return (authenticated);
195 auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen)
199 if ((challenge = get_challenge(authctxt)) == NULL)
202 debug("sending challenge '%s'", challenge);
203 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
204 packet_put_cstring(challenge);
214 auth1_process_tis_response(Authctxt *authctxt, char *info, size_t infolen)
216 int authenticated = 0;
220 response = packet_get_string(&dlen);
222 authenticated = verify_response(authctxt, response);
223 memset(response, 'r', dlen);
226 return (authenticated);
230 * read packets, try to authenticate the user and
231 * return only if authentication is successful
234 do_authloop(Authctxt *authctxt)
236 int authenticated = 0;
238 int prev = 0, type = 0;
239 const struct AuthMethod1 *meth;
241 debug("Attempting authentication for %s%.100s.",
242 authctxt->valid ? "" : "invalid user ", authctxt->user);
244 /* If the user has no password, accept authentication immediately. */
245 if (options.password_authentication &&
247 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
249 PRIVSEP(auth_password(authctxt, ""))) {
251 if (options.use_pam && (PRIVSEP(do_pam_account())))
254 auth_log(authctxt, 1, "without authentication", "");
259 /* Indicate that authentication is needed. */
260 packet_start(SSH_SMSG_FAILURE);
265 /* default to fail */
270 /* Get a packet from the client. */
272 type = packet_read();
275 * If we started challenge-response authentication but the
276 * next packet is not a response to our challenge, release
277 * the resources allocated by get_challenge() (which would
278 * normally have been released by verify_response() had we
279 * received such a response)
281 if (prev == SSH_CMSG_AUTH_TIS &&
282 type != SSH_CMSG_AUTH_TIS_RESPONSE)
283 abandon_challenge_response(authctxt);
285 if ((meth = lookup_authmethod1(type)) == NULL) {
286 logit("Unknown message during authentication: "
291 if (!*(meth->enabled)) {
292 verbose("%s authentication disabled.", meth->name);
296 authenticated = meth->method(authctxt, info, sizeof(info));
297 if (authenticated == -1)
298 continue; /* "postponed" */
302 auth_close(authctxt->as);
306 if (!authctxt->valid && authenticated)
307 fatal("INTERNAL ERROR: authenticated invalid user %s",
311 if (authenticated && cray_access_denied(authctxt->user)) {
313 fatal("Access denied for user %s.",authctxt->user);
319 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,
321 packet_disconnect("Authentication rejected for uid %d.",
322 authctxt->pw == NULL ? -1 : authctxt->pw->pw_uid);
326 /* Special handling for root */
327 if (authenticated && authctxt->pw->pw_uid == 0 &&
328 !auth_root_allowed(meth->name)) {
330 # ifdef SSH_AUDIT_EVENTS
331 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
337 if (options.use_pam && authenticated &&
338 !PRIVSEP(do_pam_account())) {
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(msg);
358 /* Log before sending the reply */
359 auth_log(authctxt, authenticated, get_authname(type), info);
361 if (client_user != NULL) {
369 if (authctxt->failures++ > options.max_authtries) {
370 #ifdef SSH_AUDIT_EVENTS
371 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
373 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
376 packet_start(SSH_SMSG_FAILURE);
383 * Performs authentication of an incoming connection. Session key has already
384 * been exchanged and encryption is enabled.
387 do_authentication(Authctxt *authctxt)
390 char *user, *style = NULL;
392 /* Get the name of the user that we wish to log in as. */
393 packet_read_expect(SSH_CMSG_USER);
395 /* Get the user name. */
396 user = packet_get_string(&ulen);
399 if ((style = strchr(user, ':')) != NULL)
402 authctxt->user = user;
403 authctxt->style = style;
405 /* Verify that the user is a valid user. */
406 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
409 debug("do_authentication: invalid user %s", user);
410 authctxt->pw = fakepw();
413 setproctitle("%s%s", authctxt->valid ? user : "unknown",
414 use_privsep ? " [net]" : "");
418 PRIVSEP(start_pam(authctxt));
422 * If we are not running as root, the user must have the same uid as
426 if (!use_privsep && getuid() != 0 && authctxt->pw &&
427 authctxt->pw->pw_uid != getuid())
428 packet_disconnect("Cannot change user when server not running as root.");
432 * Loop until the user has been authenticated or the connection is
433 * closed, do_authloop() returns only if authentication is successful
435 do_authloop(authctxt);
437 /* The user has been authenticated and accepted. */
438 packet_start(SSH_SMSG_SUCCESS);