blob: ab6fd34dd90f536ed645d89ec9d8941725830b04 [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
73 debug("Attempting authentication for %s%.100s.",
Damien Miller9f0f5c62001-12-21 14:45:46 +110074 authctxt->valid ? "" : "illegal user ", authctxt->user);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000075
76 /* If the user has no password, accept authentication immediately. */
77 if (options.password_authentication &&
Darren Tucker6aaa58c2003-08-02 22:24:49 +100078#ifdef KRB5
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000079 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
80#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000081 PRIVSEP(auth_password(authctxt, ""))) {
Darren Tuckera8c73d32004-06-23 09:17:54 +100082#ifdef USE_PAM
83 if (options.use_pam && (PRIVSEP(do_pam_account())))
84#endif
85 {
86 auth_log(authctxt, 1, "without authentication", "");
87 return;
88 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000089 }
Damien Millereba71ba2000-04-29 23:57:08 +100090
91 /* Indicate that authentication is needed. */
92 packet_start(SSH_SMSG_FAILURE);
93 packet_send();
94 packet_write_wait();
95
Damien Miller874d77b2000-10-14 16:23:11 +110096 client_user = NULL;
97
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000098 for (;;) {
Damien Miller874d77b2000-10-14 16:23:11 +110099 /* default to fail */
100 authenticated = 0;
101
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000102 info[0] = '\0';
Damien Millereba71ba2000-04-29 23:57:08 +1000103
104 /* Get a packet from the client. */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000105 prev = type;
Damien Millerdff50992002-01-22 23:16:32 +1100106 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000107
Damien Miller4f9f42a2003-05-10 19:28:02 +1000108 /*
109 * If we started challenge-response authentication but the
110 * next packet is not a response to our challenge, release
111 * the resources allocated by get_challenge() (which would
112 * normally have been released by verify_response() had we
113 * received such a response)
114 */
115 if (prev == SSH_CMSG_AUTH_TIS &&
116 type != SSH_CMSG_AUTH_TIS_RESPONSE)
117 abandon_challenge_response(authctxt);
118
Damien Millereba71ba2000-04-29 23:57:08 +1000119 /* Process the packet. */
120 switch (type) {
Damien Millereba71ba2000-04-29 23:57:08 +1000121 case SSH_CMSG_AUTH_RHOSTS_RSA:
122 if (!options.rhosts_rsa_authentication) {
123 verbose("Rhosts with RSA authentication disabled.");
124 break;
125 }
126 /*
127 * Get client user name. Note that we just have to
128 * trust the client; root on the client machine can
129 * claim to be any user.
130 */
131 client_user = packet_get_string(&ulen);
132
133 /* Get the client host key. */
Damien Millerda755162002-01-22 23:09:22 +1100134 client_host_key = key_new(KEY_RSA1);
Damien Millereba71ba2000-04-29 23:57:08 +1000135 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100136 packet_get_bignum(client_host_key->rsa->e);
137 packet_get_bignum(client_host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000138
Damien Millerda755162002-01-22 23:09:22 +1100139 if (bits != BN_num_bits(client_host_key->rsa->n))
Damien Miller874d77b2000-10-14 16:23:11 +1100140 verbose("Warning: keysize mismatch for client_host_key: "
Damien Millerda755162002-01-22 23:09:22 +1100141 "actual %d, announced %d",
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000142 BN_num_bits(client_host_key->rsa->n), bits);
Damien Miller48b03fc2002-01-22 23:11:40 +1100143 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000144
Damien Miller3e3b5142003-11-17 21:13:40 +1100145 authenticated = auth_rhosts_rsa(authctxt, client_user,
Damien Millerd221ca62002-01-22 23:11:00 +1100146 client_host_key);
Damien Millerda755162002-01-22 23:09:22 +1100147 key_free(client_host_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000148
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000149 snprintf(info, sizeof info, " ruser %.100s", client_user);
Damien Millereba71ba2000-04-29 23:57:08 +1000150 break;
151
152 case SSH_CMSG_AUTH_RSA:
153 if (!options.rsa_authentication) {
154 verbose("RSA authentication disabled.");
155 break;
156 }
157 /* RSA authentication requested. */
Damien Millerda755162002-01-22 23:09:22 +1100158 if ((n = BN_new()) == NULL)
159 fatal("do_authloop: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100160 packet_get_bignum(n);
Damien Miller48b03fc2002-01-22 23:11:40 +1100161 packet_check_eom();
Damien Miller3e3b5142003-11-17 21:13:40 +1100162 authenticated = auth_rsa(authctxt, n);
Damien Millereba71ba2000-04-29 23:57:08 +1000163 BN_clear_free(n);
164 break;
165
166 case SSH_CMSG_AUTH_PASSWORD:
167 if (!options.password_authentication) {
168 verbose("Password authentication disabled.");
169 break;
170 }
171 /*
172 * Read user password. It is in plain text, but was
173 * transmitted over the encrypted channel so it is
174 * not visible to an outside observer.
175 */
176 password = packet_get_string(&dlen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100177 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000178
Kevin Steves12aaa042001-01-24 21:23:39 +0000179 /* Try authentication with the password. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000180 authenticated = PRIVSEP(auth_password(authctxt, password));
Damien Millereba71ba2000-04-29 23:57:08 +1000181
182 memset(password, 0, strlen(password));
183 xfree(password);
184 break;
185
Damien Millereba71ba2000-04-29 23:57:08 +1000186 case SSH_CMSG_AUTH_TIS:
187 debug("rcvd SSH_CMSG_AUTH_TIS");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000188 if (options.challenge_response_authentication == 1) {
189 char *challenge = get_challenge(authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000190 if (challenge != NULL) {
191 debug("sending challenge '%s'", challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000192 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000193 packet_put_cstring(challenge);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000194 xfree(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000195 packet_send();
196 packet_write_wait();
197 continue;
198 }
199 }
200 break;
201 case SSH_CMSG_AUTH_TIS_RESPONSE:
202 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000203 if (options.challenge_response_authentication == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000204 char *response = packet_get_string(&dlen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100205 packet_check_eom();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000206 authenticated = verify_response(authctxt, response);
207 memset(response, 'r', dlen);
Damien Millereba71ba2000-04-29 23:57:08 +1000208 xfree(response);
209 }
210 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000211
212 default:
213 /*
214 * Any unknown messages will be ignored (and failure
215 * returned) during authentication.
216 */
Damien Miller996acd22003-04-09 20:59:48 +1000217 logit("Unknown message during authentication: type %d", type);
Damien Millereba71ba2000-04-29 23:57:08 +1000218 break;
219 }
Damien Miller60396b02001-02-18 17:01:00 +1100220#ifdef BSD_AUTH
221 if (authctxt->as) {
222 auth_close(authctxt->as);
223 authctxt->as = NULL;
224 }
225#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000226 if (!authctxt->valid && authenticated)
227 fatal("INTERNAL ERROR: authenticated invalid user %s",
228 authctxt->user);
Damien Millereba71ba2000-04-29 23:57:08 +1000229
Tim Rice81ed5182002-09-25 17:38:46 -0700230#ifdef _UNICOS
Tim Rice81ed5182002-09-25 17:38:46 -0700231 if (authenticated && cray_access_denied(authctxt->user)) {
232 authenticated = 0;
233 fatal("Access denied for user %s.",authctxt->user);
234 }
235#endif /* _UNICOS */
236
Kevin Stevesef4eea92001-02-05 12:42:17 +0000237#ifdef HAVE_CYGWIN
238 if (authenticated &&
Ben Lindstrome35bf122004-06-22 03:37:11 +0000239 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,
240 authctxt->pw)) {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100241 packet_disconnect("Authentication rejected for uid %d.",
Ben Lindstrome35bf122004-06-22 03:37:11 +0000242 authctxt->pw == NULL ? -1 : authctxt->pw->pw_uid);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100243 authenticated = 0;
244 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000245#else
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000246 /* Special handling for root */
Damien Miller556f9312003-02-24 11:59:26 +1100247 if (authenticated && authctxt->pw->pw_uid == 0 &&
Ben Lindstromd8a90212001-02-15 03:08:27 +0000248 !auth_root_allowed(get_authname(type)))
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000249 authenticated = 0;
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000250#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000251
Damien Miller1f499fd2003-08-25 13:08:49 +1000252#ifdef USE_PAM
Damien Millera8e06ce2003-11-21 23:48:55 +1100253 if (options.use_pam && authenticated &&
Damien Miller1f499fd2003-08-25 13:08:49 +1000254 !PRIVSEP(do_pam_account()))
255 authenticated = 0;
256#endif
257
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000258 /* Log before sending the reply */
259 auth_log(authctxt, authenticated, get_authname(type), info);
260
Damien Millereba71ba2000-04-29 23:57:08 +1000261 if (client_user != NULL) {
262 xfree(client_user);
263 client_user = NULL;
264 }
265
Damien Miller874d77b2000-10-14 16:23:11 +1100266 if (authenticated)
267 return;
268
Darren Tucker89413db2004-05-24 10:36:23 +1000269 if (authctxt->failures++ > options.max_authtries)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000270 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Damien Millereba71ba2000-04-29 23:57:08 +1000271
Damien Millereba71ba2000-04-29 23:57:08 +1000272 packet_start(SSH_SMSG_FAILURE);
273 packet_send();
274 packet_write_wait();
275 }
276}
277
278/*
279 * Performs authentication of an incoming connection. Session key has already
280 * been exchanged and encryption is enabled.
281 */
Darren Tucker3e33cec2003-10-02 16:12:36 +1000282void
283do_authentication(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000284{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000285 u_int ulen;
Ben Lindstromc8226382002-04-10 16:22:09 +0000286 char *user, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000287
288 /* Get the name of the user that we wish to log in as. */
Damien Millerdff50992002-01-22 23:16:32 +1100289 packet_read_expect(SSH_CMSG_USER);
Damien Millereba71ba2000-04-29 23:57:08 +1000290
291 /* Get the user name. */
292 user = packet_get_string(&ulen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100293 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000294
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000295 if ((style = strchr(user, ':')) != NULL)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000296 *style++ = '\0';
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000297
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000298 authctxt->user = user;
299 authctxt->style = style;
300
Damien Millereba71ba2000-04-29 23:57:08 +1000301 /* Verify that the user is a valid user. */
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000302 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000303 authctxt->valid = 1;
Damien Miller856f0be2003-09-03 07:32:45 +1000304 else {
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000305 debug("do_authentication: illegal user %s", user);
Damien Miller856f0be2003-09-03 07:32:45 +1000306 authctxt->pw = fakepw();
307 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000308
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000309 setproctitle("%s%s", authctxt->pw ? user : "unknown",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000310 use_privsep ? " [net]" : "");
Ben Lindstromc1ba31f2001-02-15 03:14:11 +0000311
Damien Millereba71ba2000-04-29 23:57:08 +1000312#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000313 if (options.use_pam)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100314 PRIVSEP(start_pam(authctxt));
Damien Millereba71ba2000-04-29 23:57:08 +1000315#endif
316
317 /*
318 * If we are not running as root, the user must have the same uid as
Damien Miller874d77b2000-10-14 16:23:11 +1100319 * the server. (Unless you are running Windows)
Damien Millereba71ba2000-04-29 23:57:08 +1000320 */
Damien Miller874d77b2000-10-14 16:23:11 +1100321#ifndef HAVE_CYGWIN
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000322 if (!use_privsep && getuid() != 0 && authctxt->pw &&
323 authctxt->pw->pw_uid != getuid())
Damien Millereba71ba2000-04-29 23:57:08 +1000324 packet_disconnect("Cannot change user when server not running as root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100325#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000326
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000327 /*
Kevin Steves12aaa042001-01-24 21:23:39 +0000328 * Loop until the user has been authenticated or the connection is
329 * closed, do_authloop() returns only if authentication is successful
330 */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000331 do_authloop(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000332
333 /* The user has been authenticated and accepted. */
Damien Miller874d77b2000-10-14 16:23:11 +1100334 packet_start(SSH_SMSG_SUCCESS);
335 packet_send();
336 packet_write_wait();
Damien Millereba71ba2000-04-29 23:57:08 +1000337}