2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * As far as I am concerned, the code I have written for this software
6 * can be used freely for any purpose. Any derived versions of this
7 * software must be clearly marked as such, and if the derived work is
8 * incompatible with the protocol description in the RFC file, it must be
9 * called by a name other than "ssh" or "Secure Shell".
13 RCSID("$OpenBSD: auth1.c,v 1.62 2005/07/16 01:35:24 djm Exp $");
27 #include "monitor_wrap.h"
31 extern ServerOptions options;
32 extern Buffer loginmsg;
34 static int auth1_process_password(Authctxt *, char *, size_t);
35 static int auth1_process_rsa(Authctxt *, char *, size_t);
36 static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t);
37 static int auth1_process_tis_challenge(Authctxt *, char *, size_t);
38 static int auth1_process_tis_response(Authctxt *, char *, size_t);
40 static char *client_user = NULL; /* Used to fill in remote user for PAM */
46 int (*method)(Authctxt *, char *, size_t);
49 const struct AuthMethod1 auth1_methods[] = {
51 SSH_CMSG_AUTH_PASSWORD, "password",
52 &options.password_authentication, auth1_process_password
55 SSH_CMSG_AUTH_RSA, "rsa",
56 &options.rsa_authentication, auth1_process_rsa
59 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
60 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
63 SSH_CMSG_AUTH_TIS, "challenge-response",
64 &options.challenge_response_authentication,
65 auth1_process_tis_challenge
68 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
69 &options.challenge_response_authentication,
70 auth1_process_tis_response
72 { -1, NULL, NULL, NULL}
75 static const struct AuthMethod1
76 *lookup_authmethod1(int type)
80 for(i = 0; auth1_methods[i].name != NULL; i++)
81 if (auth1_methods[i].type == type)
82 return (&(auth1_methods[i]));
88 get_authname(int type)
90 const struct AuthMethod1 *a;
93 if ((a = lookup_authmethod1(type)) != NULL)
95 snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
100 auth1_process_password(Authctxt *authctxt, char *info, size_t infolen)
102 int authenticated = 0;
107 * Read user password. It is in plain text, but was
108 * transmitted over the encrypted channel so it is
109 * not visible to an outside observer.
111 password = packet_get_string(&dlen);
114 /* Try authentication with the password. */
115 authenticated = PRIVSEP(auth_password(authctxt, password));
117 memset(password, 0, dlen);
120 return (authenticated);
124 auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen)
126 int authenticated = 0;
129 /* RSA authentication requested. */
130 if ((n = BN_new()) == NULL)
131 fatal("do_authloop: BN_new failed");
132 packet_get_bignum(n);
134 authenticated = auth_rsa(authctxt, n);
137 return (authenticated);
141 auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen)
143 int keybits, authenticated = 0;
145 Key *client_host_key;
149 * Get client user name. Note that we just have to
150 * trust the client; root on the client machine can
151 * claim to be any user.
153 client_user = packet_get_string(&ulen);
155 /* Get the client host key. */
156 client_host_key = key_new(KEY_RSA1);
157 bits = packet_get_int();
158 packet_get_bignum(client_host_key->rsa->e);
159 packet_get_bignum(client_host_key->rsa->n);
161 keybits = BN_num_bits(client_host_key->rsa->n);
162 if (keybits < 0 || bits != (u_int)keybits) {
163 verbose("Warning: keysize mismatch for client_host_key: "
164 "actual %d, announced %d",
165 BN_num_bits(client_host_key->rsa->n), bits);
169 authenticated = auth_rhosts_rsa(authctxt, client_user,
171 key_free(client_host_key);
173 snprintf(info, infolen, " ruser %.100s", client_user);
175 return (authenticated);
179 auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen)
183 if ((challenge = get_challenge(authctxt)) == NULL)
186 debug("sending challenge '%s'", challenge);
187 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
188 packet_put_cstring(challenge);
197 auth1_process_tis_response(Authctxt *authctxt, char *info, size_t infolen)
199 int authenticated = 0;
203 response = packet_get_string(&dlen);
205 authenticated = verify_response(authctxt, response);
206 memset(response, 'r', dlen);
209 return (authenticated);
213 * read packets, try to authenticate the user and
214 * return only if authentication is successful
217 do_authloop(Authctxt *authctxt)
219 int authenticated = 0;
221 int prev = 0, type = 0;
222 const struct AuthMethod1 *meth;
224 debug("Attempting authentication for %s%.100s.",
225 authctxt->valid ? "" : "invalid user ", authctxt->user);
227 /* If the user has no password, accept authentication immediately. */
228 if (options.password_authentication &&
230 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
232 PRIVSEP(auth_password(authctxt, ""))) {
234 if (options.use_pam && (PRIVSEP(do_pam_account())))
237 auth_log(authctxt, 1, "without authentication", "");
242 /* Indicate that authentication is needed. */
243 packet_start(SSH_SMSG_FAILURE);
248 /* default to fail */
253 /* Get a packet from the client. */
255 type = packet_read();
258 * If we started challenge-response authentication but the
259 * next packet is not a response to our challenge, release
260 * the resources allocated by get_challenge() (which would
261 * normally have been released by verify_response() had we
262 * received such a response)
264 if (prev == SSH_CMSG_AUTH_TIS &&
265 type != SSH_CMSG_AUTH_TIS_RESPONSE)
266 abandon_challenge_response(authctxt);
268 if ((meth = lookup_authmethod1(type)) == NULL) {
269 logit("Unknown message during authentication: "
274 if (!*(meth->enabled)) {
275 verbose("%s authentication disabled.", meth->name);
279 authenticated = meth->method(authctxt, info, sizeof(info));
280 if (authenticated == -1)
281 continue; /* "postponed" */
285 auth_close(authctxt->as);
289 if (!authctxt->valid && authenticated)
290 fatal("INTERNAL ERROR: authenticated invalid user %s",
294 if (authenticated && cray_access_denied(authctxt->user)) {
296 fatal("Access denied for user %s.",authctxt->user);
302 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,
304 packet_disconnect("Authentication rejected for uid %d.",
305 authctxt->pw == NULL ? -1 : authctxt->pw->pw_uid);
309 /* Special handling for root */
310 if (authenticated && authctxt->pw->pw_uid == 0 &&
311 !auth_root_allowed(meth->name)) {
313 # ifdef SSH_AUDIT_EVENTS
314 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
320 if (options.use_pam && authenticated &&
321 !PRIVSEP(do_pam_account())) {
325 error("Access denied for user %s by PAM account "
326 "configuration", authctxt->user);
327 len = buffer_len(&loginmsg);
328 buffer_append(&loginmsg, "\0", 1);
329 msg = buffer_ptr(&loginmsg);
330 /* strip trailing newlines */
332 while (len > 0 && msg[--len] == '\n')
335 msg = "Access denied.";
336 packet_disconnect(msg);
341 /* Log before sending the reply */
342 auth_log(authctxt, authenticated, get_authname(type), info);
344 if (client_user != NULL) {
352 if (authctxt->failures++ > options.max_authtries) {
353 #ifdef SSH_AUDIT_EVENTS
354 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
356 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
359 packet_start(SSH_SMSG_FAILURE);
366 * Performs authentication of an incoming connection. Session key has already
367 * been exchanged and encryption is enabled.
370 do_authentication(Authctxt *authctxt)
373 char *user, *style = NULL;
375 /* Get the name of the user that we wish to log in as. */
376 packet_read_expect(SSH_CMSG_USER);
378 /* Get the user name. */
379 user = packet_get_string(&ulen);
382 if ((style = strchr(user, ':')) != NULL)
385 authctxt->user = user;
386 authctxt->style = style;
388 /* Verify that the user is a valid user. */
389 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
392 debug("do_authentication: invalid user %s", user);
393 authctxt->pw = fakepw();
396 setproctitle("%s%s", authctxt->valid ? user : "unknown",
397 use_privsep ? " [net]" : "");
401 PRIVSEP(start_pam(authctxt));
405 * If we are not running as root, the user must have the same uid as
409 if (!use_privsep && getuid() != 0 && authctxt->pw &&
410 authctxt->pw->pw_uid != getuid())
411 packet_disconnect("Cannot change user when server not running as root.");
415 * Loop until the user has been authenticated or the connection is
416 * closed, do_authloop() returns only if authentication is successful
418 do_authloop(authctxt);
420 /* The user has been authenticated and accepted. */
421 packet_start(SSH_SMSG_SUCCESS);