blob: 034010fdaa49cd6fe2192e7ea02b2cee3c62dc66 [file] [log] [blame]
Damien Millere3476ed2006-07-24 14:13:33 +10001/* $OpenBSD: auth1.c,v 1.68 2006/07/22 20:48:22 stevesk Exp $ */
Damien Millereba71ba2000-04-29 23:57:08 +10002/*
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11005 *
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
Damien Millereba71ba2000-04-29 23:57:08 +100011 */
12
13#include "includes.h"
Damien Millereba71ba2000-04-29 23:57:08 +100014
Damien Miller1cdde6f2006-07-24 14:07:35 +100015#include <sys/types.h>
16
Damien Millere3476ed2006-07-24 14:13:33 +100017#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100018#include <unistd.h>
19
Damien Miller874d77b2000-10-14 16:23:11 +110020#include "xmalloc.h"
21#include "rsa.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000022#include "ssh1.h"
Damien Miller874d77b2000-10-14 16:23:11 +110023#include "packet.h"
24#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000025#include "log.h"
Damien Miller874d77b2000-10-14 16:23:11 +110026#include "servconf.h"
27#include "compat.h"
28#include "auth.h"
Damien Millerc7ef63d2002-02-05 12:21:42 +110029#include "channels.h"
Damien Miller874d77b2000-10-14 16:23:11 +110030#include "session.h"
Ben Lindstromec95ed92001-07-04 04:21:14 +000031#include "uidswap.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000032#include "monitor_wrap.h"
Darren Tuckerc1386672004-12-03 14:33:47 +110033#include "buffer.h"
Damien Miller874d77b2000-10-14 16:23:11 +110034
Damien Millereba71ba2000-04-29 23:57:08 +100035/* import */
36extern ServerOptions options;
Darren Tuckerc1386672004-12-03 14:33:47 +110037extern Buffer loginmsg;
Damien Miller874d77b2000-10-14 16:23:11 +110038
Damien Miller6abf57c2005-06-19 07:31:37 +100039static int auth1_process_password(Authctxt *, char *, size_t);
40static int auth1_process_rsa(Authctxt *, char *, size_t);
41static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t);
42static int auth1_process_tis_challenge(Authctxt *, char *, size_t);
43static int auth1_process_tis_response(Authctxt *, char *, size_t);
44
45static char *client_user = NULL; /* Used to fill in remote user for PAM */
46
47struct AuthMethod1 {
48 int type;
49 char *name;
50 int *enabled;
51 int (*method)(Authctxt *, char *, size_t);
52};
53
54const struct AuthMethod1 auth1_methods[] = {
55 {
56 SSH_CMSG_AUTH_PASSWORD, "password",
57 &options.password_authentication, auth1_process_password
58 },
59 {
60 SSH_CMSG_AUTH_RSA, "rsa",
61 &options.rsa_authentication, auth1_process_rsa
62 },
63 {
64 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
65 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
66 },
67 {
68 SSH_CMSG_AUTH_TIS, "challenge-response",
69 &options.challenge_response_authentication,
70 auth1_process_tis_challenge
71 },
72 {
73 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
74 &options.challenge_response_authentication,
75 auth1_process_tis_response
76 },
77 { -1, NULL, NULL, NULL}
78};
79
80static const struct AuthMethod1
81*lookup_authmethod1(int type)
82{
83 int i;
84
Damien Millerd62f2ca2006-03-26 13:57:41 +110085 for (i = 0; auth1_methods[i].name != NULL; i++)
Damien Miller6abf57c2005-06-19 07:31:37 +100086 if (auth1_methods[i].type == type)
87 return (&(auth1_methods[i]));
88
89 return (NULL);
90}
91
Ben Lindstrombba81212001-06-25 05:01:22 +000092static char *
Damien Millereba71ba2000-04-29 23:57:08 +100093get_authname(int type)
94{
Damien Miller6abf57c2005-06-19 07:31:37 +100095 const struct AuthMethod1 *a;
96 static char buf[64];
97
98 if ((a = lookup_authmethod1(type)) != NULL)
99 return (a->name);
100 snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
101 return (buf);
102}
103
Damien Miller91d4b122006-03-26 14:05:20 +1100104/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000105static int
106auth1_process_password(Authctxt *authctxt, char *info, size_t infolen)
107{
108 int authenticated = 0;
109 char *password;
110 u_int dlen;
111
112 /*
113 * Read user password. It is in plain text, but was
114 * transmitted over the encrypted channel so it is
115 * not visible to an outside observer.
116 */
117 password = packet_get_string(&dlen);
118 packet_check_eom();
119
120 /* Try authentication with the password. */
121 authenticated = PRIVSEP(auth_password(authctxt, password));
122
123 memset(password, 0, dlen);
124 xfree(password);
125
126 return (authenticated);
127}
128
Damien Miller91d4b122006-03-26 14:05:20 +1100129/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000130static int
131auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen)
132{
133 int authenticated = 0;
134 BIGNUM *n;
135
136 /* RSA authentication requested. */
137 if ((n = BN_new()) == NULL)
138 fatal("do_authloop: BN_new failed");
139 packet_get_bignum(n);
140 packet_check_eom();
141 authenticated = auth_rsa(authctxt, n);
142 BN_clear_free(n);
143
144 return (authenticated);
145}
146
Damien Miller91d4b122006-03-26 14:05:20 +1100147/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000148static int
149auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen)
150{
Damien Miller06221f12005-06-19 07:36:10 +1000151 int keybits, authenticated = 0;
Damien Miller6abf57c2005-06-19 07:31:37 +1000152 u_int bits;
153 Key *client_host_key;
154 u_int ulen;
155
156 /*
157 * Get client user name. Note that we just have to
158 * trust the client; root on the client machine can
159 * claim to be any user.
160 */
161 client_user = packet_get_string(&ulen);
162
163 /* Get the client host key. */
164 client_host_key = key_new(KEY_RSA1);
165 bits = packet_get_int();
166 packet_get_bignum(client_host_key->rsa->e);
167 packet_get_bignum(client_host_key->rsa->n);
168
Damien Miller06221f12005-06-19 07:36:10 +1000169 keybits = BN_num_bits(client_host_key->rsa->n);
170 if (keybits < 0 || bits != (u_int)keybits) {
Damien Miller6abf57c2005-06-19 07:31:37 +1000171 verbose("Warning: keysize mismatch for client_host_key: "
172 "actual %d, announced %d",
173 BN_num_bits(client_host_key->rsa->n), bits);
Damien Millereba71ba2000-04-29 23:57:08 +1000174 }
Damien Miller6abf57c2005-06-19 07:31:37 +1000175 packet_check_eom();
176
177 authenticated = auth_rhosts_rsa(authctxt, client_user,
178 client_host_key);
179 key_free(client_host_key);
180
181 snprintf(info, infolen, " ruser %.100s", client_user);
Damien Miller94cf4c82005-07-17 17:04:47 +1000182
Damien Miller6abf57c2005-06-19 07:31:37 +1000183 return (authenticated);
184}
185
Damien Miller91d4b122006-03-26 14:05:20 +1100186/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000187static int
188auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen)
189{
190 char *challenge;
Damien Miller46d38de2005-07-17 17:02:09 +1000191
Damien Miller6abf57c2005-06-19 07:31:37 +1000192 if ((challenge = get_challenge(authctxt)) == NULL)
193 return (0);
194
195 debug("sending challenge '%s'", challenge);
196 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
197 packet_put_cstring(challenge);
198 xfree(challenge);
199 packet_send();
200 packet_write_wait();
201
202 return (-1);
203}
204
Damien Miller91d4b122006-03-26 14:05:20 +1100205/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000206static int
207auth1_process_tis_response(Authctxt *authctxt, char *info, size_t infolen)
208{
209 int authenticated = 0;
210 char *response;
211 u_int dlen;
212
213 response = packet_get_string(&dlen);
214 packet_check_eom();
215 authenticated = verify_response(authctxt, response);
216 memset(response, 'r', dlen);
217 xfree(response);
218
219 return (authenticated);
Damien Millereba71ba2000-04-29 23:57:08 +1000220}
221
222/*
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000223 * read packets, try to authenticate the user and
224 * return only if authentication is successful
Damien Millereba71ba2000-04-29 23:57:08 +1000225 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000226static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000227do_authloop(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000228{
Damien Miller874d77b2000-10-14 16:23:11 +1100229 int authenticated = 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000230 char info[1024];
Damien Miller6abf57c2005-06-19 07:31:37 +1000231 int prev = 0, type = 0;
232 const struct AuthMethod1 *meth;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000233
234 debug("Attempting authentication for %s%.100s.",
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000235 authctxt->valid ? "" : "invalid user ", authctxt->user);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000236
237 /* If the user has no password, accept authentication immediately. */
238 if (options.password_authentication &&
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000239#ifdef KRB5
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000240 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
241#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000242 PRIVSEP(auth_password(authctxt, ""))) {
Darren Tuckera8c73d32004-06-23 09:17:54 +1000243#ifdef USE_PAM
244 if (options.use_pam && (PRIVSEP(do_pam_account())))
245#endif
246 {
247 auth_log(authctxt, 1, "without authentication", "");
248 return;
249 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000250 }
Damien Millereba71ba2000-04-29 23:57:08 +1000251
252 /* Indicate that authentication is needed. */
253 packet_start(SSH_SMSG_FAILURE);
254 packet_send();
255 packet_write_wait();
256
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000257 for (;;) {
Damien Miller874d77b2000-10-14 16:23:11 +1100258 /* default to fail */
259 authenticated = 0;
260
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000261 info[0] = '\0';
Damien Millereba71ba2000-04-29 23:57:08 +1000262
263 /* Get a packet from the client. */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000264 prev = type;
Damien Millerdff50992002-01-22 23:16:32 +1100265 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000266
Damien Miller4f9f42a2003-05-10 19:28:02 +1000267 /*
268 * If we started challenge-response authentication but the
269 * next packet is not a response to our challenge, release
270 * the resources allocated by get_challenge() (which would
271 * normally have been released by verify_response() had we
272 * received such a response)
273 */
274 if (prev == SSH_CMSG_AUTH_TIS &&
275 type != SSH_CMSG_AUTH_TIS_RESPONSE)
276 abandon_challenge_response(authctxt);
277
Damien Miller6abf57c2005-06-19 07:31:37 +1000278 if ((meth = lookup_authmethod1(type)) == NULL) {
279 logit("Unknown message during authentication: "
280 "type %d", type);
281 goto skip;
Damien Millereba71ba2000-04-29 23:57:08 +1000282 }
Damien Miller6abf57c2005-06-19 07:31:37 +1000283
284 if (!*(meth->enabled)) {
285 verbose("%s authentication disabled.", meth->name);
286 goto skip;
287 }
288
289 authenticated = meth->method(authctxt, info, sizeof(info));
290 if (authenticated == -1)
291 continue; /* "postponed" */
292
Damien Miller60396b02001-02-18 17:01:00 +1100293#ifdef BSD_AUTH
294 if (authctxt->as) {
295 auth_close(authctxt->as);
296 authctxt->as = NULL;
297 }
298#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000299 if (!authctxt->valid && authenticated)
300 fatal("INTERNAL ERROR: authenticated invalid user %s",
301 authctxt->user);
Damien Millereba71ba2000-04-29 23:57:08 +1000302
Tim Rice81ed5182002-09-25 17:38:46 -0700303#ifdef _UNICOS
Tim Rice81ed5182002-09-25 17:38:46 -0700304 if (authenticated && cray_access_denied(authctxt->user)) {
305 authenticated = 0;
306 fatal("Access denied for user %s.",authctxt->user);
307 }
308#endif /* _UNICOS */
309
Kevin Stevesef4eea92001-02-05 12:42:17 +0000310#ifdef HAVE_CYGWIN
311 if (authenticated &&
Damien Miller94cf4c82005-07-17 17:04:47 +1000312 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,
Ben Lindstrome35bf122004-06-22 03:37:11 +0000313 authctxt->pw)) {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100314 packet_disconnect("Authentication rejected for uid %d.",
Ben Lindstrome35bf122004-06-22 03:37:11 +0000315 authctxt->pw == NULL ? -1 : authctxt->pw->pw_uid);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100316 authenticated = 0;
317 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000318#else
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000319 /* Special handling for root */
Damien Miller556f9312003-02-24 11:59:26 +1100320 if (authenticated && authctxt->pw->pw_uid == 0 &&
Damien Miller6abf57c2005-06-19 07:31:37 +1000321 !auth_root_allowed(meth->name)) {
322 authenticated = 0;
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100323# ifdef SSH_AUDIT_EVENTS
324 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100325# endif
326 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000327#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000328
Damien Miller1f499fd2003-08-25 13:08:49 +1000329#ifdef USE_PAM
Damien Millera8e06ce2003-11-21 23:48:55 +1100330 if (options.use_pam && authenticated &&
Darren Tuckerc1386672004-12-03 14:33:47 +1100331 !PRIVSEP(do_pam_account())) {
332 char *msg;
333 size_t len;
334
335 error("Access denied for user %s by PAM account "
Damien Millerb6f72f52005-07-17 17:26:43 +1000336 "configuration", authctxt->user);
Darren Tuckerc1386672004-12-03 14:33:47 +1100337 len = buffer_len(&loginmsg);
338 buffer_append(&loginmsg, "\0", 1);
339 msg = buffer_ptr(&loginmsg);
340 /* strip trailing newlines */
341 if (len > 0)
342 while (len > 0 && msg[--len] == '\n')
343 msg[len] = '\0';
344 else
345 msg = "Access denied.";
346 packet_disconnect(msg);
347 }
Damien Miller1f499fd2003-08-25 13:08:49 +1000348#endif
349
Damien Miller6abf57c2005-06-19 07:31:37 +1000350 skip:
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000351 /* Log before sending the reply */
352 auth_log(authctxt, authenticated, get_authname(type), info);
353
Damien Millereba71ba2000-04-29 23:57:08 +1000354 if (client_user != NULL) {
355 xfree(client_user);
356 client_user = NULL;
357 }
358
Damien Miller874d77b2000-10-14 16:23:11 +1100359 if (authenticated)
360 return;
361
Darren Tucker269a1ea2005-02-03 00:20:53 +1100362 if (authctxt->failures++ > options.max_authtries) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100363#ifdef SSH_AUDIT_EVENTS
364 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100365#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000366 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100367 }
Damien Millereba71ba2000-04-29 23:57:08 +1000368
Damien Millereba71ba2000-04-29 23:57:08 +1000369 packet_start(SSH_SMSG_FAILURE);
370 packet_send();
371 packet_write_wait();
372 }
373}
374
375/*
376 * Performs authentication of an incoming connection. Session key has already
377 * been exchanged and encryption is enabled.
378 */
Darren Tucker3e33cec2003-10-02 16:12:36 +1000379void
380do_authentication(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000381{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000382 u_int ulen;
Ben Lindstromc8226382002-04-10 16:22:09 +0000383 char *user, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000384
385 /* Get the name of the user that we wish to log in as. */
Damien Millerdff50992002-01-22 23:16:32 +1100386 packet_read_expect(SSH_CMSG_USER);
Damien Millereba71ba2000-04-29 23:57:08 +1000387
388 /* Get the user name. */
389 user = packet_get_string(&ulen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100390 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000391
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000392 if ((style = strchr(user, ':')) != NULL)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000393 *style++ = '\0';
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000394
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000395 authctxt->user = user;
396 authctxt->style = style;
397
Damien Millereba71ba2000-04-29 23:57:08 +1000398 /* Verify that the user is a valid user. */
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000399 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000400 authctxt->valid = 1;
Damien Miller856f0be2003-09-03 07:32:45 +1000401 else {
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000402 debug("do_authentication: invalid user %s", user);
Damien Miller856f0be2003-09-03 07:32:45 +1000403 authctxt->pw = fakepw();
404 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000405
Damien Miller30d1f842004-07-21 20:48:53 +1000406 setproctitle("%s%s", authctxt->valid ? user : "unknown",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000407 use_privsep ? " [net]" : "");
Ben Lindstromc1ba31f2001-02-15 03:14:11 +0000408
Damien Millereba71ba2000-04-29 23:57:08 +1000409#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000410 if (options.use_pam)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100411 PRIVSEP(start_pam(authctxt));
Damien Millereba71ba2000-04-29 23:57:08 +1000412#endif
413
414 /*
415 * If we are not running as root, the user must have the same uid as
Damien Miller6abf57c2005-06-19 07:31:37 +1000416 * the server.
Damien Millereba71ba2000-04-29 23:57:08 +1000417 */
Damien Miller874d77b2000-10-14 16:23:11 +1100418#ifndef HAVE_CYGWIN
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000419 if (!use_privsep && getuid() != 0 && authctxt->pw &&
420 authctxt->pw->pw_uid != getuid())
Damien Millereba71ba2000-04-29 23:57:08 +1000421 packet_disconnect("Cannot change user when server not running as root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100422#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000423
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000424 /*
Kevin Steves12aaa042001-01-24 21:23:39 +0000425 * Loop until the user has been authenticated or the connection is
426 * closed, do_authloop() returns only if authentication is successful
427 */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000428 do_authloop(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000429
430 /* The user has been authenticated and accepted. */
Damien Miller874d77b2000-10-14 16:23:11 +1100431 packet_start(SSH_SMSG_SUCCESS);
432 packet_send();
433 packet_write_wait();
Damien Millereba71ba2000-04-29 23:57:08 +1000434}