blob: 471f271726b124913f0a4e3f65c216e2cb0d9aa1 [file] [log] [blame]
Damien Millereba71ba2000-04-29 23:57:08 +10001/*
2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11004 *
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".
Damien Millereba71ba2000-04-29 23:57:08 +100010 */
11
12#include "includes.h"
Darren Tucker89413db2004-05-24 10:36:23 +100013RCSID("$OpenBSD: auth1.c,v 1.57 2004/05/23 23:59:53 dtucker Exp $");
Damien Millereba71ba2000-04-29 23:57:08 +100014
Damien Miller874d77b2000-10-14 16:23:11 +110015#include "xmalloc.h"
16#include "rsa.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000017#include "ssh1.h"
Damien Miller874d77b2000-10-14 16:23:11 +110018#include "packet.h"
19#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000020#include "log.h"
Damien Miller874d77b2000-10-14 16:23:11 +110021#include "servconf.h"
22#include "compat.h"
23#include "auth.h"
Damien Millerc7ef63d2002-02-05 12:21:42 +110024#include "channels.h"
Damien Miller874d77b2000-10-14 16:23:11 +110025#include "session.h"
Ben Lindstromec95ed92001-07-04 04:21:14 +000026#include "uidswap.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000027#include "monitor_wrap.h"
Damien Miller874d77b2000-10-14 16:23:11 +110028
Damien Millereba71ba2000-04-29 23:57:08 +100029/* import */
30extern ServerOptions options;
Damien Miller874d77b2000-10-14 16:23:11 +110031
Damien Millereba71ba2000-04-29 23:57:08 +100032/*
33 * convert ssh auth msg type into description
34 */
Ben Lindstrombba81212001-06-25 05:01:22 +000035static char *
Damien Millereba71ba2000-04-29 23:57:08 +100036get_authname(int type)
37{
38 static char buf[1024];
39 switch (type) {
40 case SSH_CMSG_AUTH_PASSWORD:
41 return "password";
42 case SSH_CMSG_AUTH_RSA:
43 return "rsa";
44 case SSH_CMSG_AUTH_RHOSTS_RSA:
45 return "rhosts-rsa";
46 case SSH_CMSG_AUTH_RHOSTS:
47 return "rhosts";
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000048 case SSH_CMSG_AUTH_TIS:
49 case SSH_CMSG_AUTH_TIS_RESPONSE:
50 return "challenge-response";
Damien Millereba71ba2000-04-29 23:57:08 +100051 }
52 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
53 return buf;
54}
55
56/*
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000057 * read packets, try to authenticate the user and
58 * return only if authentication is successful
Damien Millereba71ba2000-04-29 23:57:08 +100059 */
Ben Lindstrombba81212001-06-25 05:01:22 +000060static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000061do_authloop(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +100062{
Damien Miller874d77b2000-10-14 16:23:11 +110063 int authenticated = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +000064 u_int bits;
Damien Millerda755162002-01-22 23:09:22 +110065 Key *client_host_key;
Damien Millereba71ba2000-04-29 23:57:08 +100066 BIGNUM *n;
Damien Miller874d77b2000-10-14 16:23:11 +110067 char *client_user, *password;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000068 char info[1024];
Ben Lindstrom46c16222000-12-22 01:43:59 +000069 u_int dlen;
Ben Lindstrom46c16222000-12-22 01:43:59 +000070 u_int ulen;
Damien Miller4f9f42a2003-05-10 19:28:02 +100071 int prev, type = 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000072 struct passwd *pw = authctxt->pw;
73
74 debug("Attempting authentication for %s%.100s.",
Damien Miller9f0f5c62001-12-21 14:45:46 +110075 authctxt->valid ? "" : "illegal user ", authctxt->user);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000076
77 /* If the user has no password, accept authentication immediately. */
78 if (options.password_authentication &&
Darren Tucker6aaa58c2003-08-02 22:24:49 +100079#ifdef KRB5
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000080 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
81#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000082 PRIVSEP(auth_password(authctxt, ""))) {
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000083 auth_log(authctxt, 1, "without authentication", "");
84 return;
85 }
Damien Millereba71ba2000-04-29 23:57:08 +100086
87 /* Indicate that authentication is needed. */
88 packet_start(SSH_SMSG_FAILURE);
89 packet_send();
90 packet_write_wait();
91
Damien Miller874d77b2000-10-14 16:23:11 +110092 client_user = NULL;
93
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000094 for (;;) {
Damien Miller874d77b2000-10-14 16:23:11 +110095 /* default to fail */
96 authenticated = 0;
97
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000098 info[0] = '\0';
Damien Millereba71ba2000-04-29 23:57:08 +100099
100 /* Get a packet from the client. */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000101 prev = type;
Damien Millerdff50992002-01-22 23:16:32 +1100102 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000103
Damien Miller4f9f42a2003-05-10 19:28:02 +1000104 /*
105 * If we started challenge-response authentication but the
106 * next packet is not a response to our challenge, release
107 * the resources allocated by get_challenge() (which would
108 * normally have been released by verify_response() had we
109 * received such a response)
110 */
111 if (prev == SSH_CMSG_AUTH_TIS &&
112 type != SSH_CMSG_AUTH_TIS_RESPONSE)
113 abandon_challenge_response(authctxt);
114
Damien Millereba71ba2000-04-29 23:57:08 +1000115 /* Process the packet. */
116 switch (type) {
Damien Millereba71ba2000-04-29 23:57:08 +1000117 case SSH_CMSG_AUTH_RHOSTS_RSA:
118 if (!options.rhosts_rsa_authentication) {
119 verbose("Rhosts with RSA authentication disabled.");
120 break;
121 }
122 /*
123 * Get client user name. Note that we just have to
124 * trust the client; root on the client machine can
125 * claim to be any user.
126 */
127 client_user = packet_get_string(&ulen);
128
129 /* Get the client host key. */
Damien Millerda755162002-01-22 23:09:22 +1100130 client_host_key = key_new(KEY_RSA1);
Damien Millereba71ba2000-04-29 23:57:08 +1000131 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100132 packet_get_bignum(client_host_key->rsa->e);
133 packet_get_bignum(client_host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000134
Damien Millerda755162002-01-22 23:09:22 +1100135 if (bits != BN_num_bits(client_host_key->rsa->n))
Damien Miller874d77b2000-10-14 16:23:11 +1100136 verbose("Warning: keysize mismatch for client_host_key: "
Damien Millerda755162002-01-22 23:09:22 +1100137 "actual %d, announced %d",
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000138 BN_num_bits(client_host_key->rsa->n), bits);
Damien Miller48b03fc2002-01-22 23:11:40 +1100139 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000140
Damien Miller3e3b5142003-11-17 21:13:40 +1100141 authenticated = auth_rhosts_rsa(authctxt, client_user,
Damien Millerd221ca62002-01-22 23:11:00 +1100142 client_host_key);
Damien Millerda755162002-01-22 23:09:22 +1100143 key_free(client_host_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000144
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000145 snprintf(info, sizeof info, " ruser %.100s", client_user);
Damien Millereba71ba2000-04-29 23:57:08 +1000146 break;
147
148 case SSH_CMSG_AUTH_RSA:
149 if (!options.rsa_authentication) {
150 verbose("RSA authentication disabled.");
151 break;
152 }
153 /* RSA authentication requested. */
Damien Millerda755162002-01-22 23:09:22 +1100154 if ((n = BN_new()) == NULL)
155 fatal("do_authloop: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100156 packet_get_bignum(n);
Damien Miller48b03fc2002-01-22 23:11:40 +1100157 packet_check_eom();
Damien Miller3e3b5142003-11-17 21:13:40 +1100158 authenticated = auth_rsa(authctxt, n);
Damien Millereba71ba2000-04-29 23:57:08 +1000159 BN_clear_free(n);
160 break;
161
162 case SSH_CMSG_AUTH_PASSWORD:
163 if (!options.password_authentication) {
164 verbose("Password authentication disabled.");
165 break;
166 }
167 /*
168 * Read user password. It is in plain text, but was
169 * transmitted over the encrypted channel so it is
170 * not visible to an outside observer.
171 */
172 password = packet_get_string(&dlen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100173 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000174
Kevin Steves12aaa042001-01-24 21:23:39 +0000175 /* Try authentication with the password. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000176 authenticated = PRIVSEP(auth_password(authctxt, password));
Damien Millereba71ba2000-04-29 23:57:08 +1000177
178 memset(password, 0, strlen(password));
179 xfree(password);
180 break;
181
Damien Millereba71ba2000-04-29 23:57:08 +1000182 case SSH_CMSG_AUTH_TIS:
183 debug("rcvd SSH_CMSG_AUTH_TIS");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000184 if (options.challenge_response_authentication == 1) {
185 char *challenge = get_challenge(authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000186 if (challenge != NULL) {
187 debug("sending challenge '%s'", challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000188 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000189 packet_put_cstring(challenge);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000190 xfree(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000191 packet_send();
192 packet_write_wait();
193 continue;
194 }
195 }
196 break;
197 case SSH_CMSG_AUTH_TIS_RESPONSE:
198 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000199 if (options.challenge_response_authentication == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000200 char *response = packet_get_string(&dlen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100201 packet_check_eom();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000202 authenticated = verify_response(authctxt, response);
203 memset(response, 'r', dlen);
Damien Millereba71ba2000-04-29 23:57:08 +1000204 xfree(response);
205 }
206 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000207
208 default:
209 /*
210 * Any unknown messages will be ignored (and failure
211 * returned) during authentication.
212 */
Damien Miller996acd22003-04-09 20:59:48 +1000213 logit("Unknown message during authentication: type %d", type);
Damien Millereba71ba2000-04-29 23:57:08 +1000214 break;
215 }
Damien Miller60396b02001-02-18 17:01:00 +1100216#ifdef BSD_AUTH
217 if (authctxt->as) {
218 auth_close(authctxt->as);
219 authctxt->as = NULL;
220 }
221#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000222 if (!authctxt->valid && authenticated)
223 fatal("INTERNAL ERROR: authenticated invalid user %s",
224 authctxt->user);
Damien Millereba71ba2000-04-29 23:57:08 +1000225
Tim Rice81ed5182002-09-25 17:38:46 -0700226#ifdef _UNICOS
Tim Rice81ed5182002-09-25 17:38:46 -0700227 if (authenticated && cray_access_denied(authctxt->user)) {
228 authenticated = 0;
229 fatal("Access denied for user %s.",authctxt->user);
230 }
231#endif /* _UNICOS */
232
Kevin Stevesef4eea92001-02-05 12:42:17 +0000233#ifdef HAVE_CYGWIN
234 if (authenticated &&
Damien Miller0dea79d2001-12-29 14:08:28 +1100235 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, pw)) {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100236 packet_disconnect("Authentication rejected for uid %d.",
Damien Miller0eae4422003-11-22 14:15:30 +1100237 pw == NULL ? -1 : pw->pw_uid);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100238 authenticated = 0;
239 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000240#else
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000241 /* Special handling for root */
Damien Miller556f9312003-02-24 11:59:26 +1100242 if (authenticated && authctxt->pw->pw_uid == 0 &&
Ben Lindstromd8a90212001-02-15 03:08:27 +0000243 !auth_root_allowed(get_authname(type)))
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000244 authenticated = 0;
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000245#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000246
Damien Miller1f499fd2003-08-25 13:08:49 +1000247#ifdef USE_PAM
Damien Millera8e06ce2003-11-21 23:48:55 +1100248 if (options.use_pam && authenticated &&
Damien Miller1f499fd2003-08-25 13:08:49 +1000249 !PRIVSEP(do_pam_account()))
250 authenticated = 0;
251#endif
252
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000253 /* Log before sending the reply */
254 auth_log(authctxt, authenticated, get_authname(type), info);
255
Damien Millereba71ba2000-04-29 23:57:08 +1000256 if (client_user != NULL) {
257 xfree(client_user);
258 client_user = NULL;
259 }
260
Damien Miller874d77b2000-10-14 16:23:11 +1100261 if (authenticated)
262 return;
263
Darren Tucker89413db2004-05-24 10:36:23 +1000264 if (authctxt->failures++ > options.max_authtries)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000265 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Damien Millereba71ba2000-04-29 23:57:08 +1000266
Damien Millereba71ba2000-04-29 23:57:08 +1000267 packet_start(SSH_SMSG_FAILURE);
268 packet_send();
269 packet_write_wait();
270 }
271}
272
273/*
274 * Performs authentication of an incoming connection. Session key has already
275 * been exchanged and encryption is enabled.
276 */
Darren Tucker3e33cec2003-10-02 16:12:36 +1000277void
278do_authentication(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000279{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000280 u_int ulen;
Ben Lindstromc8226382002-04-10 16:22:09 +0000281 char *user, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000282
283 /* Get the name of the user that we wish to log in as. */
Damien Millerdff50992002-01-22 23:16:32 +1100284 packet_read_expect(SSH_CMSG_USER);
Damien Millereba71ba2000-04-29 23:57:08 +1000285
286 /* Get the user name. */
287 user = packet_get_string(&ulen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100288 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000289
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000290 if ((style = strchr(user, ':')) != NULL)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000291 *style++ = '\0';
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000292
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000293 authctxt->user = user;
294 authctxt->style = style;
295
Damien Millereba71ba2000-04-29 23:57:08 +1000296 /* Verify that the user is a valid user. */
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000297 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000298 authctxt->valid = 1;
Damien Miller856f0be2003-09-03 07:32:45 +1000299 else {
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000300 debug("do_authentication: illegal user %s", user);
Damien Miller856f0be2003-09-03 07:32:45 +1000301 authctxt->pw = fakepw();
302 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000303
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000304 setproctitle("%s%s", authctxt->pw ? user : "unknown",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000305 use_privsep ? " [net]" : "");
Ben Lindstromc1ba31f2001-02-15 03:14:11 +0000306
Damien Millereba71ba2000-04-29 23:57:08 +1000307#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000308 if (options.use_pam)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100309 PRIVSEP(start_pam(authctxt));
Damien Millereba71ba2000-04-29 23:57:08 +1000310#endif
311
312 /*
313 * If we are not running as root, the user must have the same uid as
Damien Miller874d77b2000-10-14 16:23:11 +1100314 * the server. (Unless you are running Windows)
Damien Millereba71ba2000-04-29 23:57:08 +1000315 */
Damien Miller874d77b2000-10-14 16:23:11 +1100316#ifndef HAVE_CYGWIN
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000317 if (!use_privsep && getuid() != 0 && authctxt->pw &&
318 authctxt->pw->pw_uid != getuid())
Damien Millereba71ba2000-04-29 23:57:08 +1000319 packet_disconnect("Cannot change user when server not running as root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100320#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000321
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000322 /*
Kevin Steves12aaa042001-01-24 21:23:39 +0000323 * Loop until the user has been authenticated or the connection is
324 * closed, do_authloop() returns only if authentication is successful
325 */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000326 do_authloop(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000327
328 /* The user has been authenticated and accepted. */
Damien Miller874d77b2000-10-14 16:23:11 +1100329 packet_start(SSH_SMSG_SUCCESS);
330 packet_send();
331 packet_write_wait();
Damien Millereba71ba2000-04-29 23:57:08 +1000332}