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.40 2002/04/10 08:21:47 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";
52 #if defined(KRB4) || defined(KRB5)
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 &&
84 #if defined(KRB4) || defined(KRB5)
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. */
106 type = packet_read();
108 /* Process the packet. */
111 #if defined(KRB4) || defined(KRB5)
112 case SSH_CMSG_AUTH_KERBEROS:
113 if (!options.kerberos_authentication) {
114 verbose("Kerberos authentication disabled.");
116 char *kdata = packet_get_string(&dlen);
119 if (kdata[0] == 4) { /* KRB_PROT_VERSION */
124 if (tkt.length < MAX_KTXT_LEN)
125 memcpy(tkt.dat, kdata, tkt.length);
127 if (auth_krb4(authctxt, &tkt, &client_user)) {
129 snprintf(info, sizeof(info),
140 if (auth_krb5(authctxt, &tkt, &client_user)) {
142 snprintf(info, sizeof(info),
151 #endif /* KRB4 || KRB5 */
153 #if defined(AFS) || defined(KRB5)
154 /* XXX - punt on backward compatibility here. */
155 case SSH_CMSG_HAVE_KERBEROS_TGT:
156 packet_send_debug("Kerberos TGT passing disabled before authentication.");
159 case SSH_CMSG_HAVE_AFS_TOKEN:
160 packet_send_debug("AFS token passing disabled before authentication.");
163 #endif /* AFS || KRB5 */
165 case SSH_CMSG_AUTH_RHOSTS:
166 if (!options.rhosts_authentication) {
167 verbose("Rhosts authentication disabled.");
171 * Get client user name. Note that we just have to
172 * trust the client; this is one reason why rhosts
173 * authentication is insecure. (Another is
174 * IP-spoofing on a local network.)
176 client_user = packet_get_string(&ulen);
179 /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
180 authenticated = auth_rhosts(pw, client_user);
182 snprintf(info, sizeof info, " ruser %.100s", client_user);
185 case SSH_CMSG_AUTH_RHOSTS_RSA:
186 if (!options.rhosts_rsa_authentication) {
187 verbose("Rhosts with RSA authentication disabled.");
191 * Get client user name. Note that we just have to
192 * trust the client; root on the client machine can
193 * claim to be any user.
195 client_user = packet_get_string(&ulen);
197 /* Get the client host key. */
198 client_host_key = key_new(KEY_RSA1);
199 bits = packet_get_int();
200 packet_get_bignum(client_host_key->rsa->e);
201 packet_get_bignum(client_host_key->rsa->n);
203 if (bits != BN_num_bits(client_host_key->rsa->n))
204 verbose("Warning: keysize mismatch for client_host_key: "
205 "actual %d, announced %d",
206 BN_num_bits(client_host_key->rsa->n), bits);
209 authenticated = auth_rhosts_rsa(pw, client_user,
211 key_free(client_host_key);
213 snprintf(info, sizeof info, " ruser %.100s", client_user);
216 case SSH_CMSG_AUTH_RSA:
217 if (!options.rsa_authentication) {
218 verbose("RSA authentication disabled.");
221 /* RSA authentication requested. */
222 if ((n = BN_new()) == NULL)
223 fatal("do_authloop: BN_new failed");
224 packet_get_bignum(n);
226 authenticated = auth_rsa(pw, n);
230 case SSH_CMSG_AUTH_PASSWORD:
231 if (!options.password_authentication) {
232 verbose("Password authentication disabled.");
236 * Read user password. It is in plain text, but was
237 * transmitted over the encrypted channel so it is
238 * not visible to an outside observer.
240 password = packet_get_string(&dlen);
243 /* Try authentication with the password. */
244 authenticated = PRIVSEP(auth_password(authctxt, password));
246 memset(password, 0, strlen(password));
250 case SSH_CMSG_AUTH_TIS:
251 debug("rcvd SSH_CMSG_AUTH_TIS");
252 if (options.challenge_response_authentication == 1) {
253 char *challenge = get_challenge(authctxt);
254 if (challenge != NULL) {
255 debug("sending challenge '%s'", challenge);
256 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
257 packet_put_cstring(challenge);
265 case SSH_CMSG_AUTH_TIS_RESPONSE:
266 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
267 if (options.challenge_response_authentication == 1) {
268 char *response = packet_get_string(&dlen);
269 debug("got response '%s'", response);
271 authenticated = verify_response(authctxt, response);
272 memset(response, 'r', dlen);
279 * Any unknown messages will be ignored (and failure
280 * returned) during authentication.
282 log("Unknown message during authentication: type %d", type);
287 auth_close(authctxt->as);
291 if (!authctxt->valid && authenticated)
292 fatal("INTERNAL ERROR: authenticated invalid user %s",
297 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, pw)) {
298 packet_disconnect("Authentication rejected for uid %d.",
299 pw == NULL ? -1 : pw->pw_uid);
303 /* Special handling for root */
304 if (authenticated && authctxt->pw->pw_uid == 0 &&
305 !auth_root_allowed(get_authname(type)))
309 if (!use_privsep && authenticated &&
310 !do_pam_account(pw->pw_name, client_user))
314 /* Log before sending the reply */
315 auth_log(authctxt, authenticated, get_authname(type), info);
317 if (client_user != NULL) {
325 if (authctxt->failures++ > AUTH_FAIL_MAX) {
326 #ifdef WITH_AIXAUTHENTICATE
327 loginfailed(authctxt->user,
328 get_canonical_hostname(options.verify_reverse_mapping),
330 #endif /* WITH_AIXAUTHENTICATE */
331 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
334 packet_start(SSH_SMSG_FAILURE);
341 * Performs authentication of an incoming connection. Session key has already
342 * been exchanged and encryption is enabled.
345 do_authentication(void)
349 char *user, *style = NULL;
351 /* Get the name of the user that we wish to log in as. */
352 packet_read_expect(SSH_CMSG_USER);
354 /* Get the user name. */
355 user = packet_get_string(&ulen);
358 if ((style = strchr(user, ':')) != NULL)
362 /* XXX - SSH.com Kerberos v5 braindeath. */
363 if ((datafellows & SSH_BUG_K5USER) &&
364 options.kerberos_authentication) {
366 if ((p = strchr(user, '@')) != NULL)
371 authctxt = authctxt_new();
372 authctxt->user = user;
373 authctxt->style = style;
375 /* Verify that the user is a valid user. */
376 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
379 debug("do_authentication: illegal user %s", user);
381 setproctitle("%s%s", authctxt->pw ? user : "unknown",
382 use_privsep ? " [net]" : "");
385 PRIVSEP(start_pam(authctxt->pw == NULL ? "NOUSER" : user));
389 * If we are not running as root, the user must have the same uid as
390 * the server. (Unless you are running Windows)
393 if (!use_privsep && getuid() != 0 && authctxt->pw &&
394 authctxt->pw->pw_uid != getuid())
395 packet_disconnect("Cannot change user when server not running as root.");
399 * Loop until the user has been authenticated or the connection is
400 * closed, do_authloop() returns only if authentication is successful
402 do_authloop(authctxt);
404 /* The user has been authenticated and accepted. */
405 packet_start(SSH_SMSG_SUCCESS);