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.49 2003/07/22 13:35:22 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:
164 if (!options.rhosts_authentication) {
165 verbose("Rhosts authentication disabled.");
169 * Get client user name. Note that we just have to
170 * trust the client; this is one reason why rhosts
171 * authentication is insecure. (Another is
172 * IP-spoofing on a local network.)
174 client_user = packet_get_string(&ulen);
177 /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
178 authenticated = auth_rhosts(pw, client_user);
180 snprintf(info, sizeof info, " ruser %.100s", client_user);
183 case SSH_CMSG_AUTH_RHOSTS_RSA:
184 if (!options.rhosts_rsa_authentication) {
185 verbose("Rhosts with RSA authentication disabled.");
189 * Get client user name. Note that we just have to
190 * trust the client; root on the client machine can
191 * claim to be any user.
193 client_user = packet_get_string(&ulen);
195 /* Get the client host key. */
196 client_host_key = key_new(KEY_RSA1);
197 bits = packet_get_int();
198 packet_get_bignum(client_host_key->rsa->e);
199 packet_get_bignum(client_host_key->rsa->n);
201 if (bits != BN_num_bits(client_host_key->rsa->n))
202 verbose("Warning: keysize mismatch for client_host_key: "
203 "actual %d, announced %d",
204 BN_num_bits(client_host_key->rsa->n), bits);
207 authenticated = auth_rhosts_rsa(pw, client_user,
209 key_free(client_host_key);
211 snprintf(info, sizeof info, " ruser %.100s", client_user);
214 case SSH_CMSG_AUTH_RSA:
215 if (!options.rsa_authentication) {
216 verbose("RSA authentication disabled.");
219 /* RSA authentication requested. */
220 if ((n = BN_new()) == NULL)
221 fatal("do_authloop: BN_new failed");
222 packet_get_bignum(n);
224 authenticated = auth_rsa(pw, n);
228 case SSH_CMSG_AUTH_PASSWORD:
229 if (!options.password_authentication) {
230 verbose("Password authentication disabled.");
234 * Read user password. It is in plain text, but was
235 * transmitted over the encrypted channel so it is
236 * not visible to an outside observer.
238 password = packet_get_string(&dlen);
241 /* Try authentication with the password. */
242 authenticated = PRIVSEP(auth_password(authctxt, password));
244 memset(password, 0, strlen(password));
248 case SSH_CMSG_AUTH_TIS:
249 debug("rcvd SSH_CMSG_AUTH_TIS");
250 if (options.challenge_response_authentication == 1) {
251 char *challenge = get_challenge(authctxt);
252 if (challenge != NULL) {
253 debug("sending challenge '%s'", challenge);
254 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
255 packet_put_cstring(challenge);
263 case SSH_CMSG_AUTH_TIS_RESPONSE:
264 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
265 if (options.challenge_response_authentication == 1) {
266 char *response = packet_get_string(&dlen);
268 authenticated = verify_response(authctxt, response);
269 memset(response, 'r', dlen);
276 * Any unknown messages will be ignored (and failure
277 * returned) during authentication.
279 logit("Unknown message during authentication: type %d", type);
284 auth_close(authctxt->as);
288 if (!authctxt->valid && authenticated)
289 fatal("INTERNAL ERROR: authenticated invalid user %s",
293 if (authenticated && cray_access_denied(authctxt->user)) {
295 fatal("Access denied for user %s.",authctxt->user);
301 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, pw)) {
302 packet_disconnect("Authentication rejected for uid %d.",
303 pw == NULL ? -1 : pw->pw_uid);
307 /* Special handling for root */
308 if (authenticated && authctxt->pw->pw_uid == 0 &&
309 !auth_root_allowed(get_authname(type)))
313 /* Log before sending the reply */
314 auth_log(authctxt, authenticated, get_authname(type), info);
316 if (client_user != NULL) {
324 if (authctxt->failures++ > AUTH_FAIL_MAX)
325 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
327 packet_start(SSH_SMSG_FAILURE);
334 * Performs authentication of an incoming connection. Session key has already
335 * been exchanged and encryption is enabled.
338 do_authentication(void)
342 char *user, *style = NULL;
344 /* Get the name of the user that we wish to log in as. */
345 packet_read_expect(SSH_CMSG_USER);
347 /* Get the user name. */
348 user = packet_get_string(&ulen);
351 if ((style = strchr(user, ':')) != NULL)
355 /* XXX - SSH.com Kerberos v5 braindeath. */
356 if ((datafellows & SSH_BUG_K5USER) &&
357 options.kerberos_authentication) {
359 if ((p = strchr(user, '@')) != NULL)
364 authctxt = authctxt_new();
365 authctxt->user = user;
366 authctxt->style = style;
368 /* Verify that the user is a valid user. */
369 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
372 debug("do_authentication: illegal user %s", user);
374 setproctitle("%s%s", authctxt->pw ? user : "unknown",
375 use_privsep ? " [net]" : "");
379 PRIVSEP(start_pam(user));
383 * If we are not running as root, the user must have the same uid as
384 * the server. (Unless you are running Windows)
387 if (!use_privsep && getuid() != 0 && authctxt->pw &&
388 authctxt->pw->pw_uid != getuid())
389 packet_disconnect("Cannot change user when server not running as root.");
393 * Loop until the user has been authenticated or the connection is
394 * closed, do_authloop() returns only if authentication is successful
396 do_authloop(authctxt);
398 /* The user has been authenticated and accepted. */
399 packet_start(SSH_SMSG_SUCCESS);