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.50 2003/08/13 08:46:30 markus Exp $");
28 #include "monitor_wrap.h"
31 extern ServerOptions options;
34 * convert ssh auth msg type into description
37 get_authname(int type)
39 static char buf[1024];
41 case SSH_CMSG_AUTH_PASSWORD:
43 case SSH_CMSG_AUTH_RSA:
45 case SSH_CMSG_AUTH_RHOSTS_RSA:
47 case SSH_CMSG_AUTH_RHOSTS:
49 case SSH_CMSG_AUTH_TIS:
50 case SSH_CMSG_AUTH_TIS_RESPONSE:
51 return "challenge-response";
53 case SSH_CMSG_AUTH_KERBEROS:
57 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
62 * read packets, try to authenticate the user and
63 * return only if authentication is successful
66 do_authloop(Authctxt *authctxt)
68 int authenticated = 0;
72 char *client_user, *password;
77 struct passwd *pw = authctxt->pw;
79 debug("Attempting authentication for %s%.100s.",
80 authctxt->valid ? "" : "illegal user ", authctxt->user);
82 /* If the user has no password, accept authentication immediately. */
83 if (options.password_authentication &&
85 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
87 PRIVSEP(auth_password(authctxt, ""))) {
88 auth_log(authctxt, 1, "without authentication", "");
92 /* Indicate that authentication is needed. */
93 packet_start(SSH_SMSG_FAILURE);
100 /* default to fail */
105 /* Get a packet from the client. */
107 type = packet_read();
110 * If we started challenge-response authentication but the
111 * next packet is not a response to our challenge, release
112 * the resources allocated by get_challenge() (which would
113 * normally have been released by verify_response() had we
114 * received such a response)
116 if (prev == SSH_CMSG_AUTH_TIS &&
117 type != SSH_CMSG_AUTH_TIS_RESPONSE)
118 abandon_challenge_response(authctxt);
120 /* Process the packet. */
124 case SSH_CMSG_AUTH_KERBEROS:
125 if (!options.kerberos_authentication) {
126 verbose("Kerberos authentication disabled.");
128 char *kdata = packet_get_string(&dlen);
131 if (kdata[0] != 4) { /* KRB_PROT_VERSION */
132 krb5_data tkt, reply;
136 if (PRIVSEP(auth_krb5(authctxt, &tkt,
137 &client_user, &reply))) {
139 snprintf(info, sizeof(info),
143 /* Send response to client */
145 SSH_SMSG_AUTH_KERBEROS_RESPONSE);
146 packet_put_string((char *)
147 reply.data, reply.length);
158 case SSH_CMSG_HAVE_KERBEROS_TGT:
159 packet_send_debug("Kerberos TGT passing disabled before authentication.");
163 case SSH_CMSG_AUTH_RHOSTS_RSA:
164 if (!options.rhosts_rsa_authentication) {
165 verbose("Rhosts with RSA authentication disabled.");
169 * Get client user name. Note that we just have to
170 * trust the client; root on the client machine can
171 * claim to be any user.
173 client_user = packet_get_string(&ulen);
175 /* Get the client host key. */
176 client_host_key = key_new(KEY_RSA1);
177 bits = packet_get_int();
178 packet_get_bignum(client_host_key->rsa->e);
179 packet_get_bignum(client_host_key->rsa->n);
181 if (bits != BN_num_bits(client_host_key->rsa->n))
182 verbose("Warning: keysize mismatch for client_host_key: "
183 "actual %d, announced %d",
184 BN_num_bits(client_host_key->rsa->n), bits);
187 authenticated = auth_rhosts_rsa(pw, client_user,
189 key_free(client_host_key);
191 snprintf(info, sizeof info, " ruser %.100s", client_user);
194 case SSH_CMSG_AUTH_RSA:
195 if (!options.rsa_authentication) {
196 verbose("RSA authentication disabled.");
199 /* RSA authentication requested. */
200 if ((n = BN_new()) == NULL)
201 fatal("do_authloop: BN_new failed");
202 packet_get_bignum(n);
204 authenticated = auth_rsa(pw, n);
208 case SSH_CMSG_AUTH_PASSWORD:
209 if (!options.password_authentication) {
210 verbose("Password authentication disabled.");
214 * Read user password. It is in plain text, but was
215 * transmitted over the encrypted channel so it is
216 * not visible to an outside observer.
218 password = packet_get_string(&dlen);
221 /* Try authentication with the password. */
222 authenticated = PRIVSEP(auth_password(authctxt, password));
224 memset(password, 0, strlen(password));
228 case SSH_CMSG_AUTH_TIS:
229 debug("rcvd SSH_CMSG_AUTH_TIS");
230 if (options.challenge_response_authentication == 1) {
231 char *challenge = get_challenge(authctxt);
232 if (challenge != NULL) {
233 debug("sending challenge '%s'", challenge);
234 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
235 packet_put_cstring(challenge);
243 case SSH_CMSG_AUTH_TIS_RESPONSE:
244 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
245 if (options.challenge_response_authentication == 1) {
246 char *response = packet_get_string(&dlen);
248 authenticated = verify_response(authctxt, response);
249 memset(response, 'r', dlen);
256 * Any unknown messages will be ignored (and failure
257 * returned) during authentication.
259 logit("Unknown message during authentication: type %d", type);
264 auth_close(authctxt->as);
268 if (!authctxt->valid && authenticated)
269 fatal("INTERNAL ERROR: authenticated invalid user %s",
273 if (authenticated && cray_access_denied(authctxt->user)) {
275 fatal("Access denied for user %s.",authctxt->user);
281 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, pw)) {
282 packet_disconnect("Authentication rejected for uid %d.",
283 pw == NULL ? -1 : pw->pw_uid);
287 /* Special handling for root */
288 if (authenticated && authctxt->pw->pw_uid == 0 &&
289 !auth_root_allowed(get_authname(type)))
293 /* Log before sending the reply */
294 auth_log(authctxt, authenticated, get_authname(type), info);
296 if (client_user != NULL) {
304 if (authctxt->failures++ > AUTH_FAIL_MAX)
305 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
307 packet_start(SSH_SMSG_FAILURE);
314 * Performs authentication of an incoming connection. Session key has already
315 * been exchanged and encryption is enabled.
318 do_authentication(void)
322 char *user, *style = NULL;
324 /* Get the name of the user that we wish to log in as. */
325 packet_read_expect(SSH_CMSG_USER);
327 /* Get the user name. */
328 user = packet_get_string(&ulen);
331 if ((style = strchr(user, ':')) != NULL)
335 /* XXX - SSH.com Kerberos v5 braindeath. */
336 if ((datafellows & SSH_BUG_K5USER) &&
337 options.kerberos_authentication) {
339 if ((p = strchr(user, '@')) != NULL)
344 authctxt = authctxt_new();
345 authctxt->user = user;
346 authctxt->style = style;
348 /* Verify that the user is a valid user. */
349 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
352 debug("do_authentication: illegal user %s", user);
354 setproctitle("%s%s", authctxt->pw ? user : "unknown",
355 use_privsep ? " [net]" : "");
359 PRIVSEP(start_pam(user));
363 * If we are not running as root, the user must have the same uid as
364 * the server. (Unless you are running Windows)
367 if (!use_privsep && getuid() != 0 && authctxt->pw &&
368 authctxt->pw->pw_uid != getuid())
369 packet_disconnect("Cannot change user when server not running as root.");
373 * Loop until the user has been authenticated or the connection is
374 * closed, do_authloop() returns only if authentication is successful
376 do_authloop(authctxt);
378 /* The user has been authenticated and accepted. */
379 packet_start(SSH_SMSG_SUCCESS);