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 $");
15 RCSID("$DragonFly: src/crypto/openssh/Attic/auth1.c,v 1.2 2003/06/17 04:24:36 dillon Exp $");
30 #include "monitor_wrap.h"
33 extern ServerOptions options;
36 * convert ssh auth msg type into description
39 get_authname(int type)
41 static char buf[1024];
43 case SSH_CMSG_AUTH_PASSWORD:
45 case SSH_CMSG_AUTH_RSA:
47 case SSH_CMSG_AUTH_RHOSTS_RSA:
49 case SSH_CMSG_AUTH_RHOSTS:
51 case SSH_CMSG_AUTH_TIS:
52 case SSH_CMSG_AUTH_TIS_RESPONSE:
53 return "challenge-response";
54 #if defined(KRB4) || defined(KRB5)
55 case SSH_CMSG_AUTH_KERBEROS:
59 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
64 * read packets, try to authenticate the user and
65 * return only if authentication is successful
68 do_authloop(Authctxt *authctxt)
70 int authenticated = 0;
74 char *client_user, *password;
79 struct passwd *pw = authctxt->pw;
81 debug("Attempting authentication for %s%.100s.",
82 authctxt->valid ? "" : "illegal user ", authctxt->user);
84 /* If the user has no password, accept authentication immediately. */
85 if (options.password_authentication &&
86 #if defined(KRB4) || defined(KRB5)
87 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
89 PRIVSEP(auth_password(authctxt, ""))) {
90 auth_log(authctxt, 1, "without authentication", "");
94 /* Indicate that authentication is needed. */
95 packet_start(SSH_SMSG_FAILURE);
102 /* default to fail */
107 /* Get a packet from the client. */
109 type = packet_read();
112 * If we started challenge-response authentication but the
113 * next packet is not a response to our challenge, release
114 * the resources allocated by get_challenge() (which would
115 * normally have been released by verify_response() had we
116 * received such a response)
118 if (prev == SSH_CMSG_AUTH_TIS &&
119 type != SSH_CMSG_AUTH_TIS_RESPONSE)
120 abandon_challenge_response(authctxt);
122 /* Process the packet. */
125 #if defined(KRB4) || defined(KRB5)
126 case SSH_CMSG_AUTH_KERBEROS:
127 if (!options.kerberos_authentication) {
128 verbose("Kerberos authentication disabled.");
130 char *kdata = packet_get_string(&dlen);
133 if (kdata[0] == 4) { /* KRB_PROT_VERSION */
137 if (tkt.length < MAX_KTXT_LEN)
138 memcpy(tkt.dat, kdata, tkt.length);
140 if (PRIVSEP(auth_krb4(authctxt, &tkt,
141 &client_user, &reply))) {
143 snprintf(info, sizeof(info),
148 SSH_SMSG_AUTH_KERBEROS_RESPONSE);
149 packet_put_string((char *)
150 reply.dat, reply.length);
157 krb5_data tkt, reply;
161 if (PRIVSEP(auth_krb5(authctxt, &tkt,
162 &client_user, &reply))) {
164 snprintf(info, sizeof(info),
168 /* Send response to client */
170 SSH_SMSG_AUTH_KERBEROS_RESPONSE);
171 packet_put_string((char *)
172 reply.data, reply.length);
184 #endif /* KRB4 || KRB5 */
186 #if defined(AFS) || defined(KRB5)
187 /* XXX - punt on backward compatibility here. */
188 case SSH_CMSG_HAVE_KERBEROS_TGT:
189 packet_send_debug("Kerberos TGT passing disabled before authentication.");
192 case SSH_CMSG_HAVE_AFS_TOKEN:
193 packet_send_debug("AFS token passing disabled before authentication.");
196 #endif /* AFS || KRB5 */
198 case SSH_CMSG_AUTH_RHOSTS:
199 if (!options.rhosts_authentication) {
200 verbose("Rhosts authentication disabled.");
204 * Get client user name. Note that we just have to
205 * trust the client; this is one reason why rhosts
206 * authentication is insecure. (Another is
207 * IP-spoofing on a local network.)
209 client_user = packet_get_string(&ulen);
212 /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
213 authenticated = auth_rhosts(pw, client_user);
215 snprintf(info, sizeof info, " ruser %.100s", client_user);
218 case SSH_CMSG_AUTH_RHOSTS_RSA:
219 if (!options.rhosts_rsa_authentication) {
220 verbose("Rhosts with RSA authentication disabled.");
224 * Get client user name. Note that we just have to
225 * trust the client; root on the client machine can
226 * claim to be any user.
228 client_user = packet_get_string(&ulen);
230 /* Get the client host key. */
231 client_host_key = key_new(KEY_RSA1);
232 bits = packet_get_int();
233 packet_get_bignum(client_host_key->rsa->e);
234 packet_get_bignum(client_host_key->rsa->n);
236 if (bits != BN_num_bits(client_host_key->rsa->n))
237 verbose("Warning: keysize mismatch for client_host_key: "
238 "actual %d, announced %d",
239 BN_num_bits(client_host_key->rsa->n), bits);
242 authenticated = auth_rhosts_rsa(pw, client_user,
244 key_free(client_host_key);
246 snprintf(info, sizeof info, " ruser %.100s", client_user);
249 case SSH_CMSG_AUTH_RSA:
250 if (!options.rsa_authentication) {
251 verbose("RSA authentication disabled.");
254 /* RSA authentication requested. */
255 if ((n = BN_new()) == NULL)
256 fatal("do_authloop: BN_new failed");
257 packet_get_bignum(n);
259 authenticated = auth_rsa(pw, n);
263 case SSH_CMSG_AUTH_PASSWORD:
264 if (!options.password_authentication) {
265 verbose("Password authentication disabled.");
269 * Read user password. It is in plain text, but was
270 * transmitted over the encrypted channel so it is
271 * not visible to an outside observer.
273 password = packet_get_string(&dlen);
276 /* Try authentication with the password. */
277 authenticated = PRIVSEP(auth_password(authctxt, password));
279 memset(password, 0, strlen(password));
283 case SSH_CMSG_AUTH_TIS:
284 debug("rcvd SSH_CMSG_AUTH_TIS");
285 if (options.challenge_response_authentication == 1) {
286 char *challenge = get_challenge(authctxt);
287 if (challenge != NULL) {
288 debug("sending challenge '%s'", challenge);
289 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
290 packet_put_cstring(challenge);
298 case SSH_CMSG_AUTH_TIS_RESPONSE:
299 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
300 if (options.challenge_response_authentication == 1) {
301 char *response = packet_get_string(&dlen);
302 debug("got response '%s'", response);
304 authenticated = verify_response(authctxt, response);
305 memset(response, 'r', dlen);
312 * Any unknown messages will be ignored (and failure
313 * returned) during authentication.
315 log("Unknown message during authentication: type %d", type);
320 auth_close(authctxt->as);
324 if (!authctxt->valid && authenticated)
325 fatal("INTERNAL ERROR: authenticated invalid user %s",
329 if (type == SSH_CMSG_AUTH_PASSWORD && !authenticated)
330 cray_login_failure(authctxt->user, IA_UDBERR);
331 if (authenticated && cray_access_denied(authctxt->user)) {
333 fatal("Access denied for user %s.",authctxt->user);
339 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, pw)) {
340 packet_disconnect("Authentication rejected for uid %d.",
341 pw == NULL ? -1 : pw->pw_uid);
345 /* Special handling for root */
347 authenticated && authctxt->pw->pw_uid == 0 &&
348 !auth_root_allowed(get_authname(type)))
352 if (!use_privsep && authenticated &&
353 !do_pam_account(pw->pw_name, client_user))
357 /* Log before sending the reply */
358 auth_log(authctxt, authenticated, get_authname(type), info);
360 if (client_user != NULL) {
368 if (authctxt->failures++ > AUTH_FAIL_MAX) {
369 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
372 packet_start(SSH_SMSG_FAILURE);
379 * Performs authentication of an incoming connection. Session key has already
380 * been exchanged and encryption is enabled.
383 do_authentication(void)
387 char *user, *style = NULL;
389 /* Get the name of the user that we wish to log in as. */
390 packet_read_expect(SSH_CMSG_USER);
392 /* Get the user name. */
393 user = packet_get_string(&ulen);
396 if ((style = strchr(user, ':')) != NULL)
400 /* XXX - SSH.com Kerberos v5 braindeath. */
401 if ((datafellows & SSH_BUG_K5USER) &&
402 options.kerberos_authentication) {
404 if ((p = strchr(user, '@')) != NULL)
409 authctxt = authctxt_new();
410 authctxt->user = user;
411 authctxt->style = style;
413 /* Verify that the user is a valid user. */
414 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
417 debug("do_authentication: illegal user %s", user);
419 setproctitle("%s%s", authctxt->pw ? user : "unknown",
420 use_privsep ? " [net]" : "");
423 PRIVSEP(start_pam(authctxt->pw == NULL ? "NOUSER" : user));
427 * If we are not running as root, the user must have the same uid as
428 * the server. (Unless you are running Windows)
431 if (!use_privsep && getuid() != 0 && authctxt->pw &&
432 authctxt->pw->pw_uid != getuid())
433 packet_disconnect("Cannot change user when server not running as root.");
437 * Loop until the user has been authenticated or the connection is
438 * closed, do_authloop() returns only if authentication is successful
440 do_authloop(authctxt);
442 /* The user has been authenticated and accepted. */
443 packet_start(SSH_SMSG_SUCCESS);