blob: 2f8e13e054d2b277be1bbe646ed18bb754706e1f [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"
Damien Millereba71ba2000-04-29 23:57:08 +100013
Damien Miller874d77b2000-10-14 16:23:11 +110014#include "xmalloc.h"
15#include "rsa.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000016#include "ssh1.h"
Damien Miller874d77b2000-10-14 16:23:11 +110017#include "packet.h"
18#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000019#include "log.h"
Damien Miller874d77b2000-10-14 16:23:11 +110020#include "servconf.h"
21#include "compat.h"
22#include "auth.h"
Damien Millerc7ef63d2002-02-05 12:21:42 +110023#include "channels.h"
Damien Miller874d77b2000-10-14 16:23:11 +110024#include "session.h"
Ben Lindstromec95ed92001-07-04 04:21:14 +000025#include "uidswap.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000026#include "monitor_wrap.h"
Darren Tuckerc1386672004-12-03 14:33:47 +110027#include "buffer.h"
Damien Miller874d77b2000-10-14 16:23:11 +110028
Damien Millereba71ba2000-04-29 23:57:08 +100029/* import */
30extern ServerOptions options;
Darren Tuckerc1386672004-12-03 14:33:47 +110031extern Buffer loginmsg;
Damien Miller874d77b2000-10-14 16:23:11 +110032
Damien Miller6abf57c2005-06-19 07:31:37 +100033static int auth1_process_password(Authctxt *, char *, size_t);
34static int auth1_process_rsa(Authctxt *, char *, size_t);
35static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t);
36static int auth1_process_tis_challenge(Authctxt *, char *, size_t);
37static int auth1_process_tis_response(Authctxt *, char *, size_t);
38
39static char *client_user = NULL; /* Used to fill in remote user for PAM */
40
41struct AuthMethod1 {
42 int type;
43 char *name;
44 int *enabled;
45 int (*method)(Authctxt *, char *, size_t);
46};
47
48const struct AuthMethod1 auth1_methods[] = {
49 {
50 SSH_CMSG_AUTH_PASSWORD, "password",
51 &options.password_authentication, auth1_process_password
52 },
53 {
54 SSH_CMSG_AUTH_RSA, "rsa",
55 &options.rsa_authentication, auth1_process_rsa
56 },
57 {
58 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
59 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
60 },
61 {
62 SSH_CMSG_AUTH_TIS, "challenge-response",
63 &options.challenge_response_authentication,
64 auth1_process_tis_challenge
65 },
66 {
67 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
68 &options.challenge_response_authentication,
69 auth1_process_tis_response
70 },
71 { -1, NULL, NULL, NULL}
72};
73
74static const struct AuthMethod1
75*lookup_authmethod1(int type)
76{
77 int i;
78
Damien Millerd62f2ca2006-03-26 13:57:41 +110079 for (i = 0; auth1_methods[i].name != NULL; i++)
Damien Miller6abf57c2005-06-19 07:31:37 +100080 if (auth1_methods[i].type == type)
81 return (&(auth1_methods[i]));
82
83 return (NULL);
84}
85
Ben Lindstrombba81212001-06-25 05:01:22 +000086static char *
Damien Millereba71ba2000-04-29 23:57:08 +100087get_authname(int type)
88{
Damien Miller6abf57c2005-06-19 07:31:37 +100089 const struct AuthMethod1 *a;
90 static char buf[64];
91
92 if ((a = lookup_authmethod1(type)) != NULL)
93 return (a->name);
94 snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
95 return (buf);
96}
97
Damien Miller91d4b122006-03-26 14:05:20 +110098/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +100099static int
100auth1_process_password(Authctxt *authctxt, char *info, size_t infolen)
101{
102 int authenticated = 0;
103 char *password;
104 u_int dlen;
105
106 /*
107 * Read user password. It is in plain text, but was
108 * transmitted over the encrypted channel so it is
109 * not visible to an outside observer.
110 */
111 password = packet_get_string(&dlen);
112 packet_check_eom();
113
114 /* Try authentication with the password. */
115 authenticated = PRIVSEP(auth_password(authctxt, password));
116
117 memset(password, 0, dlen);
118 xfree(password);
119
120 return (authenticated);
121}
122
Damien Miller91d4b122006-03-26 14:05:20 +1100123/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000124static int
125auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen)
126{
127 int authenticated = 0;
128 BIGNUM *n;
129
130 /* RSA authentication requested. */
131 if ((n = BN_new()) == NULL)
132 fatal("do_authloop: BN_new failed");
133 packet_get_bignum(n);
134 packet_check_eom();
135 authenticated = auth_rsa(authctxt, n);
136 BN_clear_free(n);
137
138 return (authenticated);
139}
140
Damien Miller91d4b122006-03-26 14:05:20 +1100141/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000142static int
143auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen)
144{
Damien Miller06221f12005-06-19 07:36:10 +1000145 int keybits, authenticated = 0;
Damien Miller6abf57c2005-06-19 07:31:37 +1000146 u_int bits;
147 Key *client_host_key;
148 u_int ulen;
149
150 /*
151 * Get client user name. Note that we just have to
152 * trust the client; root on the client machine can
153 * claim to be any user.
154 */
155 client_user = packet_get_string(&ulen);
156
157 /* Get the client host key. */
158 client_host_key = key_new(KEY_RSA1);
159 bits = packet_get_int();
160 packet_get_bignum(client_host_key->rsa->e);
161 packet_get_bignum(client_host_key->rsa->n);
162
Damien Miller06221f12005-06-19 07:36:10 +1000163 keybits = BN_num_bits(client_host_key->rsa->n);
164 if (keybits < 0 || bits != (u_int)keybits) {
Damien Miller6abf57c2005-06-19 07:31:37 +1000165 verbose("Warning: keysize mismatch for client_host_key: "
166 "actual %d, announced %d",
167 BN_num_bits(client_host_key->rsa->n), bits);
Damien Millereba71ba2000-04-29 23:57:08 +1000168 }
Damien Miller6abf57c2005-06-19 07:31:37 +1000169 packet_check_eom();
170
171 authenticated = auth_rhosts_rsa(authctxt, client_user,
172 client_host_key);
173 key_free(client_host_key);
174
175 snprintf(info, infolen, " ruser %.100s", client_user);
Damien Miller94cf4c82005-07-17 17:04:47 +1000176
Damien Miller6abf57c2005-06-19 07:31:37 +1000177 return (authenticated);
178}
179
Damien Miller91d4b122006-03-26 14:05:20 +1100180/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000181static int
182auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen)
183{
184 char *challenge;
Damien Miller46d38de2005-07-17 17:02:09 +1000185
Damien Miller6abf57c2005-06-19 07:31:37 +1000186 if ((challenge = get_challenge(authctxt)) == NULL)
187 return (0);
188
189 debug("sending challenge '%s'", challenge);
190 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
191 packet_put_cstring(challenge);
192 xfree(challenge);
193 packet_send();
194 packet_write_wait();
195
196 return (-1);
197}
198
Damien Miller91d4b122006-03-26 14:05:20 +1100199/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000200static int
201auth1_process_tis_response(Authctxt *authctxt, char *info, size_t infolen)
202{
203 int authenticated = 0;
204 char *response;
205 u_int dlen;
206
207 response = packet_get_string(&dlen);
208 packet_check_eom();
209 authenticated = verify_response(authctxt, response);
210 memset(response, 'r', dlen);
211 xfree(response);
212
213 return (authenticated);
Damien Millereba71ba2000-04-29 23:57:08 +1000214}
215
216/*
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000217 * read packets, try to authenticate the user and
218 * return only if authentication is successful
Damien Millereba71ba2000-04-29 23:57:08 +1000219 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000220static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000221do_authloop(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000222{
Damien Miller874d77b2000-10-14 16:23:11 +1100223 int authenticated = 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000224 char info[1024];
Damien Miller6abf57c2005-06-19 07:31:37 +1000225 int prev = 0, type = 0;
226 const struct AuthMethod1 *meth;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000227
228 debug("Attempting authentication for %s%.100s.",
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000229 authctxt->valid ? "" : "invalid user ", authctxt->user);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000230
231 /* If the user has no password, accept authentication immediately. */
232 if (options.password_authentication &&
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000233#ifdef KRB5
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000234 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
235#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000236 PRIVSEP(auth_password(authctxt, ""))) {
Darren Tuckera8c73d32004-06-23 09:17:54 +1000237#ifdef USE_PAM
238 if (options.use_pam && (PRIVSEP(do_pam_account())))
239#endif
240 {
241 auth_log(authctxt, 1, "without authentication", "");
242 return;
243 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000244 }
Damien Millereba71ba2000-04-29 23:57:08 +1000245
246 /* Indicate that authentication is needed. */
247 packet_start(SSH_SMSG_FAILURE);
248 packet_send();
249 packet_write_wait();
250
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000251 for (;;) {
Damien Miller874d77b2000-10-14 16:23:11 +1100252 /* default to fail */
253 authenticated = 0;
254
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000255 info[0] = '\0';
Damien Millereba71ba2000-04-29 23:57:08 +1000256
257 /* Get a packet from the client. */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000258 prev = type;
Damien Millerdff50992002-01-22 23:16:32 +1100259 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000260
Damien Miller4f9f42a2003-05-10 19:28:02 +1000261 /*
262 * If we started challenge-response authentication but the
263 * next packet is not a response to our challenge, release
264 * the resources allocated by get_challenge() (which would
265 * normally have been released by verify_response() had we
266 * received such a response)
267 */
268 if (prev == SSH_CMSG_AUTH_TIS &&
269 type != SSH_CMSG_AUTH_TIS_RESPONSE)
270 abandon_challenge_response(authctxt);
271
Damien Miller6abf57c2005-06-19 07:31:37 +1000272 if ((meth = lookup_authmethod1(type)) == NULL) {
273 logit("Unknown message during authentication: "
274 "type %d", type);
275 goto skip;
Damien Millereba71ba2000-04-29 23:57:08 +1000276 }
Damien Miller6abf57c2005-06-19 07:31:37 +1000277
278 if (!*(meth->enabled)) {
279 verbose("%s authentication disabled.", meth->name);
280 goto skip;
281 }
282
283 authenticated = meth->method(authctxt, info, sizeof(info));
284 if (authenticated == -1)
285 continue; /* "postponed" */
286
Damien Miller60396b02001-02-18 17:01:00 +1100287#ifdef BSD_AUTH
288 if (authctxt->as) {
289 auth_close(authctxt->as);
290 authctxt->as = NULL;
291 }
292#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000293 if (!authctxt->valid && authenticated)
294 fatal("INTERNAL ERROR: authenticated invalid user %s",
295 authctxt->user);
Damien Millereba71ba2000-04-29 23:57:08 +1000296
Tim Rice81ed5182002-09-25 17:38:46 -0700297#ifdef _UNICOS
Tim Rice81ed5182002-09-25 17:38:46 -0700298 if (authenticated && cray_access_denied(authctxt->user)) {
299 authenticated = 0;
300 fatal("Access denied for user %s.",authctxt->user);
301 }
302#endif /* _UNICOS */
303
Kevin Stevesef4eea92001-02-05 12:42:17 +0000304#ifdef HAVE_CYGWIN
305 if (authenticated &&
Damien Miller94cf4c82005-07-17 17:04:47 +1000306 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,
Ben Lindstrome35bf122004-06-22 03:37:11 +0000307 authctxt->pw)) {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100308 packet_disconnect("Authentication rejected for uid %d.",
Ben Lindstrome35bf122004-06-22 03:37:11 +0000309 authctxt->pw == NULL ? -1 : authctxt->pw->pw_uid);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100310 authenticated = 0;
311 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000312#else
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000313 /* Special handling for root */
Damien Miller556f9312003-02-24 11:59:26 +1100314 if (authenticated && authctxt->pw->pw_uid == 0 &&
Damien Miller6abf57c2005-06-19 07:31:37 +1000315 !auth_root_allowed(meth->name)) {
316 authenticated = 0;
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100317# ifdef SSH_AUDIT_EVENTS
318 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100319# endif
320 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000321#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000322
Damien Miller1f499fd2003-08-25 13:08:49 +1000323#ifdef USE_PAM
Damien Millera8e06ce2003-11-21 23:48:55 +1100324 if (options.use_pam && authenticated &&
Darren Tuckerc1386672004-12-03 14:33:47 +1100325 !PRIVSEP(do_pam_account())) {
326 char *msg;
327 size_t len;
328
329 error("Access denied for user %s by PAM account "
Damien Millerb6f72f52005-07-17 17:26:43 +1000330 "configuration", authctxt->user);
Darren Tuckerc1386672004-12-03 14:33:47 +1100331 len = buffer_len(&loginmsg);
332 buffer_append(&loginmsg, "\0", 1);
333 msg = buffer_ptr(&loginmsg);
334 /* strip trailing newlines */
335 if (len > 0)
336 while (len > 0 && msg[--len] == '\n')
337 msg[len] = '\0';
338 else
339 msg = "Access denied.";
340 packet_disconnect(msg);
341 }
Damien Miller1f499fd2003-08-25 13:08:49 +1000342#endif
343
Damien Miller6abf57c2005-06-19 07:31:37 +1000344 skip:
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000345 /* Log before sending the reply */
346 auth_log(authctxt, authenticated, get_authname(type), info);
347
Damien Millereba71ba2000-04-29 23:57:08 +1000348 if (client_user != NULL) {
349 xfree(client_user);
350 client_user = NULL;
351 }
352
Damien Miller874d77b2000-10-14 16:23:11 +1100353 if (authenticated)
354 return;
355
Darren Tucker269a1ea2005-02-03 00:20:53 +1100356 if (authctxt->failures++ > options.max_authtries) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100357#ifdef SSH_AUDIT_EVENTS
358 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100359#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000360 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100361 }
Damien Millereba71ba2000-04-29 23:57:08 +1000362
Damien Millereba71ba2000-04-29 23:57:08 +1000363 packet_start(SSH_SMSG_FAILURE);
364 packet_send();
365 packet_write_wait();
366 }
367}
368
369/*
370 * Performs authentication of an incoming connection. Session key has already
371 * been exchanged and encryption is enabled.
372 */
Darren Tucker3e33cec2003-10-02 16:12:36 +1000373void
374do_authentication(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000375{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000376 u_int ulen;
Ben Lindstromc8226382002-04-10 16:22:09 +0000377 char *user, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000378
379 /* Get the name of the user that we wish to log in as. */
Damien Millerdff50992002-01-22 23:16:32 +1100380 packet_read_expect(SSH_CMSG_USER);
Damien Millereba71ba2000-04-29 23:57:08 +1000381
382 /* Get the user name. */
383 user = packet_get_string(&ulen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100384 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000385
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000386 if ((style = strchr(user, ':')) != NULL)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000387 *style++ = '\0';
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000388
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000389 authctxt->user = user;
390 authctxt->style = style;
391
Damien Millereba71ba2000-04-29 23:57:08 +1000392 /* Verify that the user is a valid user. */
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000393 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000394 authctxt->valid = 1;
Damien Miller856f0be2003-09-03 07:32:45 +1000395 else {
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000396 debug("do_authentication: invalid user %s", user);
Damien Miller856f0be2003-09-03 07:32:45 +1000397 authctxt->pw = fakepw();
398 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000399
Damien Miller30d1f842004-07-21 20:48:53 +1000400 setproctitle("%s%s", authctxt->valid ? user : "unknown",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000401 use_privsep ? " [net]" : "");
Ben Lindstromc1ba31f2001-02-15 03:14:11 +0000402
Damien Millereba71ba2000-04-29 23:57:08 +1000403#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000404 if (options.use_pam)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100405 PRIVSEP(start_pam(authctxt));
Damien Millereba71ba2000-04-29 23:57:08 +1000406#endif
407
408 /*
409 * If we are not running as root, the user must have the same uid as
Damien Miller6abf57c2005-06-19 07:31:37 +1000410 * the server.
Damien Millereba71ba2000-04-29 23:57:08 +1000411 */
Damien Miller874d77b2000-10-14 16:23:11 +1100412#ifndef HAVE_CYGWIN
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000413 if (!use_privsep && getuid() != 0 && authctxt->pw &&
414 authctxt->pw->pw_uid != getuid())
Damien Millereba71ba2000-04-29 23:57:08 +1000415 packet_disconnect("Cannot change user when server not running as root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100416#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000417
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000418 /*
Kevin Steves12aaa042001-01-24 21:23:39 +0000419 * Loop until the user has been authenticated or the connection is
420 * closed, do_authloop() returns only if authentication is successful
421 */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000422 do_authloop(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000423
424 /* The user has been authenticated and accepted. */
Damien Miller874d77b2000-10-14 16:23:11 +1100425 packet_start(SSH_SMSG_SUCCESS);
426 packet_send();
427 packet_write_wait();
Damien Millereba71ba2000-04-29 23:57:08 +1000428}