blob: e541935ce3cc9192ec46444bfe404cdb28124222 [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, ""))) {
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000082 auth_log(authctxt, 1, "without authentication", "");
83 return;
84 }
Damien Millereba71ba2000-04-29 23:57:08 +100085
86 /* Indicate that authentication is needed. */
87 packet_start(SSH_SMSG_FAILURE);
88 packet_send();
89 packet_write_wait();
90
Damien Miller874d77b2000-10-14 16:23:11 +110091 client_user = NULL;
92
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000093 for (;;) {
Damien Miller874d77b2000-10-14 16:23:11 +110094 /* default to fail */
95 authenticated = 0;
96
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000097 info[0] = '\0';
Damien Millereba71ba2000-04-29 23:57:08 +100098
99 /* Get a packet from the client. */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000100 prev = type;
Damien Millerdff50992002-01-22 23:16:32 +1100101 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000102
Damien Miller4f9f42a2003-05-10 19:28:02 +1000103 /*
104 * If we started challenge-response authentication but the
105 * next packet is not a response to our challenge, release
106 * the resources allocated by get_challenge() (which would
107 * normally have been released by verify_response() had we
108 * received such a response)
109 */
110 if (prev == SSH_CMSG_AUTH_TIS &&
111 type != SSH_CMSG_AUTH_TIS_RESPONSE)
112 abandon_challenge_response(authctxt);
113
Damien Millereba71ba2000-04-29 23:57:08 +1000114 /* Process the packet. */
115 switch (type) {
Damien Millereba71ba2000-04-29 23:57:08 +1000116 case SSH_CMSG_AUTH_RHOSTS_RSA:
117 if (!options.rhosts_rsa_authentication) {
118 verbose("Rhosts with RSA authentication disabled.");
119 break;
120 }
121 /*
122 * Get client user name. Note that we just have to
123 * trust the client; root on the client machine can
124 * claim to be any user.
125 */
126 client_user = packet_get_string(&ulen);
127
128 /* Get the client host key. */
Damien Millerda755162002-01-22 23:09:22 +1100129 client_host_key = key_new(KEY_RSA1);
Damien Millereba71ba2000-04-29 23:57:08 +1000130 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100131 packet_get_bignum(client_host_key->rsa->e);
132 packet_get_bignum(client_host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000133
Damien Millerda755162002-01-22 23:09:22 +1100134 if (bits != BN_num_bits(client_host_key->rsa->n))
Damien Miller874d77b2000-10-14 16:23:11 +1100135 verbose("Warning: keysize mismatch for client_host_key: "
Damien Millerda755162002-01-22 23:09:22 +1100136 "actual %d, announced %d",
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000137 BN_num_bits(client_host_key->rsa->n), bits);
Damien Miller48b03fc2002-01-22 23:11:40 +1100138 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000139
Damien Miller3e3b5142003-11-17 21:13:40 +1100140 authenticated = auth_rhosts_rsa(authctxt, client_user,
Damien Millerd221ca62002-01-22 23:11:00 +1100141 client_host_key);
Damien Millerda755162002-01-22 23:09:22 +1100142 key_free(client_host_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000143
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000144 snprintf(info, sizeof info, " ruser %.100s", client_user);
Damien Millereba71ba2000-04-29 23:57:08 +1000145 break;
146
147 case SSH_CMSG_AUTH_RSA:
148 if (!options.rsa_authentication) {
149 verbose("RSA authentication disabled.");
150 break;
151 }
152 /* RSA authentication requested. */
Damien Millerda755162002-01-22 23:09:22 +1100153 if ((n = BN_new()) == NULL)
154 fatal("do_authloop: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100155 packet_get_bignum(n);
Damien Miller48b03fc2002-01-22 23:11:40 +1100156 packet_check_eom();
Damien Miller3e3b5142003-11-17 21:13:40 +1100157 authenticated = auth_rsa(authctxt, n);
Damien Millereba71ba2000-04-29 23:57:08 +1000158 BN_clear_free(n);
159 break;
160
161 case SSH_CMSG_AUTH_PASSWORD:
162 if (!options.password_authentication) {
163 verbose("Password authentication disabled.");
164 break;
165 }
166 /*
167 * Read user password. It is in plain text, but was
168 * transmitted over the encrypted channel so it is
169 * not visible to an outside observer.
170 */
171 password = packet_get_string(&dlen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100172 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000173
Kevin Steves12aaa042001-01-24 21:23:39 +0000174 /* Try authentication with the password. */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000175 authenticated = PRIVSEP(auth_password(authctxt, password));
Damien Millereba71ba2000-04-29 23:57:08 +1000176
177 memset(password, 0, strlen(password));
178 xfree(password);
179 break;
180
Damien Millereba71ba2000-04-29 23:57:08 +1000181 case SSH_CMSG_AUTH_TIS:
182 debug("rcvd SSH_CMSG_AUTH_TIS");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000183 if (options.challenge_response_authentication == 1) {
184 char *challenge = get_challenge(authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000185 if (challenge != NULL) {
186 debug("sending challenge '%s'", challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000187 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000188 packet_put_cstring(challenge);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000189 xfree(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000190 packet_send();
191 packet_write_wait();
192 continue;
193 }
194 }
195 break;
196 case SSH_CMSG_AUTH_TIS_RESPONSE:
197 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
Ben Lindstrom551ea372001-06-05 18:56:16 +0000198 if (options.challenge_response_authentication == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000199 char *response = packet_get_string(&dlen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100200 packet_check_eom();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000201 authenticated = verify_response(authctxt, response);
202 memset(response, 'r', dlen);
Damien Millereba71ba2000-04-29 23:57:08 +1000203 xfree(response);
204 }
205 break;
Damien Millereba71ba2000-04-29 23:57:08 +1000206
207 default:
208 /*
209 * Any unknown messages will be ignored (and failure
210 * returned) during authentication.
211 */
Damien Miller996acd22003-04-09 20:59:48 +1000212 logit("Unknown message during authentication: type %d", type);
Damien Millereba71ba2000-04-29 23:57:08 +1000213 break;
214 }
Damien Miller60396b02001-02-18 17:01:00 +1100215#ifdef BSD_AUTH
216 if (authctxt->as) {
217 auth_close(authctxt->as);
218 authctxt->as = NULL;
219 }
220#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000221 if (!authctxt->valid && authenticated)
222 fatal("INTERNAL ERROR: authenticated invalid user %s",
223 authctxt->user);
Damien Millereba71ba2000-04-29 23:57:08 +1000224
Tim Rice81ed5182002-09-25 17:38:46 -0700225#ifdef _UNICOS
Tim Rice81ed5182002-09-25 17:38:46 -0700226 if (authenticated && cray_access_denied(authctxt->user)) {
227 authenticated = 0;
228 fatal("Access denied for user %s.",authctxt->user);
229 }
230#endif /* _UNICOS */
231
Kevin Stevesef4eea92001-02-05 12:42:17 +0000232#ifdef HAVE_CYGWIN
233 if (authenticated &&
Ben Lindstrome35bf122004-06-22 03:37:11 +0000234 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,
235 authctxt->pw)) {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100236 packet_disconnect("Authentication rejected for uid %d.",
Ben Lindstrome35bf122004-06-22 03:37:11 +0000237 authctxt->pw == NULL ? -1 : authctxt->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}