blob: d08928455c52320665bcf4d7d50ae0e0342ccb7a [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 Tucker5cb30ad2004-08-12 22:40:24 +100013RCSID("$OpenBSD: auth1.c,v 1.59 2004/07/28 09:40:29 markus 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"
Darren Tuckerc1386672004-12-03 14:33:47 +110028#include "buffer.h"
Damien Miller874d77b2000-10-14 16:23:11 +110029
Damien Millereba71ba2000-04-29 23:57:08 +100030/* import */
31extern ServerOptions options;
Darren Tuckerc1386672004-12-03 14:33:47 +110032extern Buffer loginmsg;
Damien Miller874d77b2000-10-14 16:23:11 +110033
Damien Millereba71ba2000-04-29 23:57:08 +100034/*
35 * convert ssh auth msg type into description
36 */
Ben Lindstrombba81212001-06-25 05:01:22 +000037static char *
Damien Millereba71ba2000-04-29 23:57:08 +100038get_authname(int type)
39{
40 static char buf[1024];
41 switch (type) {
42 case SSH_CMSG_AUTH_PASSWORD:
43 return "password";
44 case SSH_CMSG_AUTH_RSA:
45 return "rsa";
46 case SSH_CMSG_AUTH_RHOSTS_RSA:
47 return "rhosts-rsa";
48 case SSH_CMSG_AUTH_RHOSTS:
49 return "rhosts";
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000050 case SSH_CMSG_AUTH_TIS:
51 case SSH_CMSG_AUTH_TIS_RESPONSE:
52 return "challenge-response";
Damien Millereba71ba2000-04-29 23:57:08 +100053 }
54 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
55 return buf;
56}
57
58/*
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000059 * read packets, try to authenticate the user and
60 * return only if authentication is successful
Damien Millereba71ba2000-04-29 23:57:08 +100061 */
Ben Lindstrombba81212001-06-25 05:01:22 +000062static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000063do_authloop(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +100064{
Damien Miller874d77b2000-10-14 16:23:11 +110065 int authenticated = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +000066 u_int bits;
Damien Millerda755162002-01-22 23:09:22 +110067 Key *client_host_key;
Damien Millereba71ba2000-04-29 23:57:08 +100068 BIGNUM *n;
Damien Miller874d77b2000-10-14 16:23:11 +110069 char *client_user, *password;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000070 char info[1024];
Ben Lindstrom46c16222000-12-22 01:43:59 +000071 u_int dlen;
Ben Lindstrom46c16222000-12-22 01:43:59 +000072 u_int ulen;
Damien Miller4f9f42a2003-05-10 19:28:02 +100073 int prev, type = 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000074
75 debug("Attempting authentication for %s%.100s.",
Darren Tucker5cb30ad2004-08-12 22:40:24 +100076 authctxt->valid ? "" : "invalid user ", authctxt->user);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000077
78 /* If the user has no password, accept authentication immediately. */
79 if (options.password_authentication &&
Darren Tucker6aaa58c2003-08-02 22:24:49 +100080#ifdef KRB5
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000081 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
82#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000083 PRIVSEP(auth_password(authctxt, ""))) {
Darren Tuckera8c73d32004-06-23 09:17:54 +100084#ifdef USE_PAM
85 if (options.use_pam && (PRIVSEP(do_pam_account())))
86#endif
87 {
88 auth_log(authctxt, 1, "without authentication", "");
89 return;
90 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000091 }
Damien Millereba71ba2000-04-29 23:57:08 +100092
93 /* Indicate that authentication is needed. */
94 packet_start(SSH_SMSG_FAILURE);
95 packet_send();
96 packet_write_wait();
97
Damien Miller874d77b2000-10-14 16:23:11 +110098 client_user = NULL;
99
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000100 for (;;) {
Damien Miller874d77b2000-10-14 16:23:11 +1100101 /* default to fail */
102 authenticated = 0;
103
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000104 info[0] = '\0';
Damien Millereba71ba2000-04-29 23:57:08 +1000105
106 /* Get a packet from the client. */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000107 prev = type;
Damien Millerdff50992002-01-22 23:16:32 +1100108 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000109
Damien Miller4f9f42a2003-05-10 19:28:02 +1000110 /*
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)
116 */
117 if (prev == SSH_CMSG_AUTH_TIS &&
118 type != SSH_CMSG_AUTH_TIS_RESPONSE)
119 abandon_challenge_response(authctxt);
120
Damien Millereba71ba2000-04-29 23:57:08 +1000121 /* Process the packet. */
122 switch (type) {
Damien Millereba71ba2000-04-29 23:57:08 +1000123 case SSH_CMSG_AUTH_RHOSTS_RSA:
124 if (!options.rhosts_rsa_authentication) {
125 verbose("Rhosts with RSA authentication disabled.");
126 break;
127 }
128 /*
129 * Get client user name. Note that we just have to
130 * trust the client; root on the client machine can
131 * claim to be any user.
132 */
133 client_user = packet_get_string(&ulen);
134
135 /* Get the client host key. */
Damien Millerda755162002-01-22 23:09:22 +1100136 client_host_key = key_new(KEY_RSA1);
Damien Millereba71ba2000-04-29 23:57:08 +1000137 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100138 packet_get_bignum(client_host_key->rsa->e);
139 packet_get_bignum(client_host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000140
Damien Millerda755162002-01-22 23:09:22 +1100141 if (bits != BN_num_bits(client_host_key->rsa->n))
Damien Miller874d77b2000-10-14 16:23:11 +1100142 verbose("Warning: keysize mismatch for client_host_key: "
Damien Millerda755162002-01-22 23:09:22 +1100143 "actual %d, announced %d",
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000144 BN_num_bits(client_host_key->rsa->n), bits);
Damien Miller48b03fc2002-01-22 23:11:40 +1100145 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000146
Damien Miller3e3b5142003-11-17 21:13:40 +1100147 authenticated = auth_rhosts_rsa(authctxt, client_user,
Damien Millerd221ca62002-01-22 23:11:00 +1100148 client_host_key);
Damien Millerda755162002-01-22 23:09:22 +1100149 key_free(client_host_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000150
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000151 snprintf(info, sizeof info, " ruser %.100s", client_user);
Damien Millereba71ba2000-04-29 23:57:08 +1000152 break;
153
154 case SSH_CMSG_AUTH_RSA:
155 if (!options.rsa_authentication) {
156 verbose("RSA authentication disabled.");
157 break;
158 }
159 /* RSA authentication requested. */
Damien Millerda755162002-01-22 23:09:22 +1100160 if ((n = BN_new()) == NULL)
161 fatal("do_authloop: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100162 packet_get_bignum(n);
Damien Miller48b03fc2002-01-22 23:11:40 +1100163 packet_check_eom();
Damien Miller3e3b5142003-11-17 21:13:40 +1100164 authenticated = auth_rsa(authctxt, n);
Damien Millereba71ba2000-04-29 23:57:08 +1000165 BN_clear_free(n);
166 break;
167
168 case SSH_CMSG_AUTH_PASSWORD:
169 if (!options.password_authentication) {
170 verbose("Password authentication disabled.");
171 break;
172 }
173 /*
174 * Read user password. It is in plain text, but was
175 * transmitted over the encrypted channel so it is
176 * not visible to an outside observer.
177 */
178 password = packet_get_string(&dlen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100179 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000180
Kevin Steves12aaa042001-01-24 21:23:39 +0000181 /* Try authentication with the password. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000182 authenticated = PRIVSEP(auth_password(authctxt, password));
Damien Millereba71ba2000-04-29 23:57:08 +1000183
184 memset(password, 0, strlen(password));
185 xfree(password);
186 break;
187
Damien Millereba71ba2000-04-29 23:57:08 +1000188 case SSH_CMSG_AUTH_TIS:
189 debug("rcvd SSH_CMSG_AUTH_TIS");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000190 if (options.challenge_response_authentication == 1) {
191 char *challenge = get_challenge(authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000192 if (challenge != NULL) {
193 debug("sending challenge '%s'", challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000194 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000195 packet_put_cstring(challenge);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000196 xfree(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000197 packet_send();
198 packet_write_wait();
199 continue;
200 }
201 }
202 break;
203 case SSH_CMSG_AUTH_TIS_RESPONSE:
204 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000205 if (options.challenge_response_authentication == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000206 char *response = packet_get_string(&dlen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100207 packet_check_eom();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000208 authenticated = verify_response(authctxt, response);
209 memset(response, 'r', dlen);
Damien Millereba71ba2000-04-29 23:57:08 +1000210 xfree(response);
211 }
212 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000213
214 default:
215 /*
216 * Any unknown messages will be ignored (and failure
217 * returned) during authentication.
218 */
Damien Miller996acd22003-04-09 20:59:48 +1000219 logit("Unknown message during authentication: type %d", type);
Damien Millereba71ba2000-04-29 23:57:08 +1000220 break;
221 }
Damien Miller60396b02001-02-18 17:01:00 +1100222#ifdef BSD_AUTH
223 if (authctxt->as) {
224 auth_close(authctxt->as);
225 authctxt->as = NULL;
226 }
227#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000228 if (!authctxt->valid && authenticated)
229 fatal("INTERNAL ERROR: authenticated invalid user %s",
230 authctxt->user);
Damien Millereba71ba2000-04-29 23:57:08 +1000231
Tim Rice81ed5182002-09-25 17:38:46 -0700232#ifdef _UNICOS
Tim Rice81ed5182002-09-25 17:38:46 -0700233 if (authenticated && cray_access_denied(authctxt->user)) {
234 authenticated = 0;
235 fatal("Access denied for user %s.",authctxt->user);
236 }
237#endif /* _UNICOS */
238
Kevin Stevesef4eea92001-02-05 12:42:17 +0000239#ifdef HAVE_CYGWIN
240 if (authenticated &&
Ben Lindstrome35bf122004-06-22 03:37:11 +0000241 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,
242 authctxt->pw)) {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100243 packet_disconnect("Authentication rejected for uid %d.",
Ben Lindstrome35bf122004-06-22 03:37:11 +0000244 authctxt->pw == NULL ? -1 : authctxt->pw->pw_uid);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100245 authenticated = 0;
246 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000247#else
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000248 /* Special handling for root */
Damien Miller556f9312003-02-24 11:59:26 +1100249 if (authenticated && authctxt->pw->pw_uid == 0 &&
Darren Tucker269a1ea2005-02-03 00:20:53 +1100250 !auth_root_allowed(get_authname(type))) {
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000251 authenticated = 0;
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100252# ifdef SSH_AUDIT_EVENTS
253 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100254# endif
255 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000256#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000257
Damien Miller1f499fd2003-08-25 13:08:49 +1000258#ifdef USE_PAM
Damien Millera8e06ce2003-11-21 23:48:55 +1100259 if (options.use_pam && authenticated &&
Darren Tuckerc1386672004-12-03 14:33:47 +1100260 !PRIVSEP(do_pam_account())) {
261 char *msg;
262 size_t len;
263
264 error("Access denied for user %s by PAM account "
265 "configuration", authctxt->user);
266 len = buffer_len(&loginmsg);
267 buffer_append(&loginmsg, "\0", 1);
268 msg = buffer_ptr(&loginmsg);
269 /* strip trailing newlines */
270 if (len > 0)
271 while (len > 0 && msg[--len] == '\n')
272 msg[len] = '\0';
273 else
274 msg = "Access denied.";
275 packet_disconnect(msg);
276 }
Damien Miller1f499fd2003-08-25 13:08:49 +1000277#endif
278
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000279 /* Log before sending the reply */
280 auth_log(authctxt, authenticated, get_authname(type), info);
281
Damien Millereba71ba2000-04-29 23:57:08 +1000282 if (client_user != NULL) {
283 xfree(client_user);
284 client_user = NULL;
285 }
286
Damien Miller874d77b2000-10-14 16:23:11 +1100287 if (authenticated)
288 return;
289
Darren Tucker269a1ea2005-02-03 00:20:53 +1100290 if (authctxt->failures++ > options.max_authtries) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100291#ifdef SSH_AUDIT_EVENTS
292 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100293#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000294 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100295 }
Damien Millereba71ba2000-04-29 23:57:08 +1000296
Damien Millereba71ba2000-04-29 23:57:08 +1000297 packet_start(SSH_SMSG_FAILURE);
298 packet_send();
299 packet_write_wait();
300 }
301}
302
303/*
304 * Performs authentication of an incoming connection. Session key has already
305 * been exchanged and encryption is enabled.
306 */
Darren Tucker3e33cec2003-10-02 16:12:36 +1000307void
308do_authentication(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000309{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000310 u_int ulen;
Ben Lindstromc8226382002-04-10 16:22:09 +0000311 char *user, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000312
313 /* Get the name of the user that we wish to log in as. */
Damien Millerdff50992002-01-22 23:16:32 +1100314 packet_read_expect(SSH_CMSG_USER);
Damien Millereba71ba2000-04-29 23:57:08 +1000315
316 /* Get the user name. */
317 user = packet_get_string(&ulen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100318 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000319
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000320 if ((style = strchr(user, ':')) != NULL)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000321 *style++ = '\0';
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000322
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000323 authctxt->user = user;
324 authctxt->style = style;
325
Damien Millereba71ba2000-04-29 23:57:08 +1000326 /* Verify that the user is a valid user. */
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000327 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000328 authctxt->valid = 1;
Damien Miller856f0be2003-09-03 07:32:45 +1000329 else {
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000330 debug("do_authentication: invalid user %s", user);
Damien Miller856f0be2003-09-03 07:32:45 +1000331 authctxt->pw = fakepw();
332 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000333
Damien Miller30d1f842004-07-21 20:48:53 +1000334 setproctitle("%s%s", authctxt->valid ? user : "unknown",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000335 use_privsep ? " [net]" : "");
Ben Lindstromc1ba31f2001-02-15 03:14:11 +0000336
Damien Millereba71ba2000-04-29 23:57:08 +1000337#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000338 if (options.use_pam)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100339 PRIVSEP(start_pam(authctxt));
Damien Millereba71ba2000-04-29 23:57:08 +1000340#endif
341
342 /*
343 * If we are not running as root, the user must have the same uid as
Damien Miller874d77b2000-10-14 16:23:11 +1100344 * the server. (Unless you are running Windows)
Damien Millereba71ba2000-04-29 23:57:08 +1000345 */
Damien Miller874d77b2000-10-14 16:23:11 +1100346#ifndef HAVE_CYGWIN
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000347 if (!use_privsep && getuid() != 0 && authctxt->pw &&
348 authctxt->pw->pw_uid != getuid())
Damien Millereba71ba2000-04-29 23:57:08 +1000349 packet_disconnect("Cannot change user when server not running as root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100350#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000351
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000352 /*
Kevin Steves12aaa042001-01-24 21:23:39 +0000353 * Loop until the user has been authenticated or the connection is
354 * closed, do_authloop() returns only if authentication is successful
355 */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000356 do_authloop(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000357
358 /* The user has been authenticated and accepted. */
Damien Miller874d77b2000-10-14 16:23:11 +1100359 packet_start(SSH_SMSG_SUCCESS);
360 packet_send();
361 packet_write_wait();
Damien Millereba71ba2000-04-29 23:57:08 +1000362}