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.44 2002/09/26 11:38:43 markus Exp $");
14 RCSID("$FreeBSD: src/crypto/openssh/auth1.c,v 1.3.2.10 2003/04/07 09:56:46 des Exp $");
29 #include "monitor_wrap.h"
32 extern ServerOptions options;
35 * convert ssh auth msg type into description
38 get_authname(int type)
40 static char buf[1024];
42 case SSH_CMSG_AUTH_PASSWORD:
44 case SSH_CMSG_AUTH_RSA:
46 case SSH_CMSG_AUTH_RHOSTS_RSA:
48 case SSH_CMSG_AUTH_RHOSTS:
50 case SSH_CMSG_AUTH_TIS:
51 case SSH_CMSG_AUTH_TIS_RESPONSE:
52 return "challenge-response";
53 #if defined(KRB4) || defined(KRB5)
54 case SSH_CMSG_AUTH_KERBEROS:
58 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
63 * read packets, try to authenticate the user and
64 * return only if authentication is successful
67 do_authloop(Authctxt *authctxt)
69 int authenticated = 0;
73 char *client_user, *password;
78 struct passwd *pw = authctxt->pw;
80 debug("Attempting authentication for %s%.100s.",
81 authctxt->valid ? "" : "illegal user ", authctxt->user);
83 /* If the user has no password, accept authentication immediately. */
84 if (options.password_authentication &&
85 #if defined(KRB4) || defined(KRB5)
86 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
88 PRIVSEP(auth_password(authctxt, ""))) {
89 auth_log(authctxt, 1, "without authentication", "");
93 /* Indicate that authentication is needed. */
94 packet_start(SSH_SMSG_FAILURE);
101 /* default to fail */
106 /* Get a packet from the client. */
108 type = packet_read();
111 * If we started challenge-response authentication but the
112 * next packet is not a response to our challenge, release
113 * the resources allocated by get_challenge() (which would
114 * normally have been released by verify_response() had we
115 * received such a response)
117 if (prev == SSH_CMSG_AUTH_TIS &&
118 type != SSH_CMSG_AUTH_TIS_RESPONSE)
119 abandon_challenge_response(authctxt);
121 /* Process the packet. */
124 #if defined(KRB4) || defined(KRB5)
125 case SSH_CMSG_AUTH_KERBEROS:
126 if (!options.kerberos_authentication) {
127 verbose("Kerberos authentication disabled.");
129 char *kdata = packet_get_string(&dlen);
132 if (kdata[0] == 4) { /* KRB_PROT_VERSION */
136 if (tkt.length < MAX_KTXT_LEN)
137 memcpy(tkt.dat, kdata, tkt.length);
139 if (PRIVSEP(auth_krb4(authctxt, &tkt,
140 &client_user, &reply))) {
142 snprintf(info, sizeof(info),
147 SSH_SMSG_AUTH_KERBEROS_RESPONSE);
148 packet_put_string((char *)
149 reply.dat, reply.length);
156 krb5_data tkt, reply;
160 if (PRIVSEP(auth_krb5(authctxt, &tkt,
161 &client_user, &reply))) {
163 snprintf(info, sizeof(info),
167 /* Send response to client */
169 SSH_SMSG_AUTH_KERBEROS_RESPONSE);
170 packet_put_string((char *)
171 reply.data, reply.length);
183 #endif /* KRB4 || KRB5 */
185 #if defined(AFS) || defined(KRB5)
186 /* XXX - punt on backward compatibility here. */
187 case SSH_CMSG_HAVE_KERBEROS_TGT:
188 packet_send_debug("Kerberos TGT passing disabled before authentication.");
191 case SSH_CMSG_HAVE_AFS_TOKEN:
192 packet_send_debug("AFS token passing disabled before authentication.");
195 #endif /* AFS || KRB5 */
197 case SSH_CMSG_AUTH_RHOSTS:
198 if (!options.rhosts_authentication) {
199 verbose("Rhosts authentication disabled.");
203 * Get client user name. Note that we just have to
204 * trust the client; this is one reason why rhosts
205 * authentication is insecure. (Another is
206 * IP-spoofing on a local network.)
208 client_user = packet_get_string(&ulen);
211 /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
212 authenticated = auth_rhosts(pw, client_user);
214 snprintf(info, sizeof info, " ruser %.100s", client_user);
217 case SSH_CMSG_AUTH_RHOSTS_RSA:
218 if (!options.rhosts_rsa_authentication) {
219 verbose("Rhosts with RSA authentication disabled.");
223 * Get client user name. Note that we just have to
224 * trust the client; root on the client machine can
225 * claim to be any user.
227 client_user = packet_get_string(&ulen);
229 /* Get the client host key. */
230 client_host_key = key_new(KEY_RSA1);
231 bits = packet_get_int();
232 packet_get_bignum(client_host_key->rsa->e);
233 packet_get_bignum(client_host_key->rsa->n);
235 if (bits != BN_num_bits(client_host_key->rsa->n))
236 verbose("Warning: keysize mismatch for client_host_key: "
237 "actual %d, announced %d",
238 BN_num_bits(client_host_key->rsa->n), bits);
241 authenticated = auth_rhosts_rsa(pw, client_user,
243 key_free(client_host_key);
245 snprintf(info, sizeof info, " ruser %.100s", client_user);
248 case SSH_CMSG_AUTH_RSA:
249 if (!options.rsa_authentication) {
250 verbose("RSA authentication disabled.");
253 /* RSA authentication requested. */
254 if ((n = BN_new()) == NULL)
255 fatal("do_authloop: BN_new failed");
256 packet_get_bignum(n);
258 authenticated = auth_rsa(pw, n);
262 case SSH_CMSG_AUTH_PASSWORD:
263 if (!options.password_authentication) {
264 verbose("Password authentication disabled.");
268 * Read user password. It is in plain text, but was
269 * transmitted over the encrypted channel so it is
270 * not visible to an outside observer.
272 password = packet_get_string(&dlen);
275 /* Try authentication with the password. */
276 authenticated = PRIVSEP(auth_password(authctxt, password));
278 memset(password, 0, strlen(password));
282 case SSH_CMSG_AUTH_TIS:
283 debug("rcvd SSH_CMSG_AUTH_TIS");
284 if (options.challenge_response_authentication == 1) {
285 char *challenge = get_challenge(authctxt);
286 if (challenge != NULL) {
287 debug("sending challenge '%s'", challenge);
288 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
289 packet_put_cstring(challenge);
297 case SSH_CMSG_AUTH_TIS_RESPONSE:
298 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
299 if (options.challenge_response_authentication == 1) {
300 char *response = packet_get_string(&dlen);
301 debug("got response '%s'", response);
303 authenticated = verify_response(authctxt, response);
304 memset(response, 'r', dlen);
311 * Any unknown messages will be ignored (and failure
312 * returned) during authentication.
314 log("Unknown message during authentication: type %d", type);
319 auth_close(authctxt->as);
323 if (!authctxt->valid && authenticated)
324 fatal("INTERNAL ERROR: authenticated invalid user %s",
328 if (type == SSH_CMSG_AUTH_PASSWORD && !authenticated)
329 cray_login_failure(authctxt->user, IA_UDBERR);
330 if (authenticated && cray_access_denied(authctxt->user)) {
332 fatal("Access denied for user %s.",authctxt->user);
338 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, pw)) {
339 packet_disconnect("Authentication rejected for uid %d.",
340 pw == NULL ? -1 : pw->pw_uid);
344 /* Special handling for root */
346 authenticated && authctxt->pw->pw_uid == 0 &&
347 !auth_root_allowed(get_authname(type)))
351 if (!use_privsep && authenticated &&
352 !do_pam_account(pw->pw_name, client_user))
356 /* Log before sending the reply */
357 auth_log(authctxt, authenticated, get_authname(type), info);
359 if (client_user != NULL) {
367 if (authctxt->failures++ > AUTH_FAIL_MAX) {
368 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
371 packet_start(SSH_SMSG_FAILURE);
378 * Performs authentication of an incoming connection. Session key has already
379 * been exchanged and encryption is enabled.
382 do_authentication(void)
386 char *user, *style = NULL;
388 /* Get the name of the user that we wish to log in as. */
389 packet_read_expect(SSH_CMSG_USER);
391 /* Get the user name. */
392 user = packet_get_string(&ulen);
395 if ((style = strchr(user, ':')) != NULL)
399 /* XXX - SSH.com Kerberos v5 braindeath. */
400 if ((datafellows & SSH_BUG_K5USER) &&
401 options.kerberos_authentication) {
403 if ((p = strchr(user, '@')) != NULL)
408 authctxt = authctxt_new();
409 authctxt->user = user;
410 authctxt->style = style;
412 /* Verify that the user is a valid user. */
413 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
416 debug("do_authentication: illegal user %s", user);
418 setproctitle("%s%s", authctxt->pw ? user : "unknown",
419 use_privsep ? " [net]" : "");
422 PRIVSEP(start_pam(authctxt->pw == NULL ? "NOUSER" : user));
426 * If we are not running as root, the user must have the same uid as
427 * the server. (Unless you are running Windows)
430 if (!use_privsep && getuid() != 0 && authctxt->pw &&
431 authctxt->pw->pw_uid != getuid())
432 packet_disconnect("Cannot change user when server not running as root.");
436 * Loop until the user has been authenticated or the connection is
437 * closed, do_authloop() returns only if authentication is successful
439 do_authloop(authctxt);
441 /* The user has been authenticated and accepted. */
442 packet_start(SSH_SMSG_SUCCESS);