blob: 6a6cff862aa6dcd21ee933668021297984ca24f2 [file] [log] [blame]
Damien Miller57c30112006-03-26 14:24:48 +11001/* $OpenBSD: auth1.c,v 1.66 2006/03/25 13:17:01 djm 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 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 Miller6abf57c2005-06-19 07:31:37 +100034static int auth1_process_password(Authctxt *, char *, size_t);
35static int auth1_process_rsa(Authctxt *, char *, size_t);
36static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t);
37static int auth1_process_tis_challenge(Authctxt *, char *, size_t);
38static int auth1_process_tis_response(Authctxt *, char *, size_t);
39
40static char *client_user = NULL; /* Used to fill in remote user for PAM */
41
42struct AuthMethod1 {
43 int type;
44 char *name;
45 int *enabled;
46 int (*method)(Authctxt *, char *, size_t);
47};
48
49const struct AuthMethod1 auth1_methods[] = {
50 {
51 SSH_CMSG_AUTH_PASSWORD, "password",
52 &options.password_authentication, auth1_process_password
53 },
54 {
55 SSH_CMSG_AUTH_RSA, "rsa",
56 &options.rsa_authentication, auth1_process_rsa
57 },
58 {
59 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
60 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
61 },
62 {
63 SSH_CMSG_AUTH_TIS, "challenge-response",
64 &options.challenge_response_authentication,
65 auth1_process_tis_challenge
66 },
67 {
68 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
69 &options.challenge_response_authentication,
70 auth1_process_tis_response
71 },
72 { -1, NULL, NULL, NULL}
73};
74
75static const struct AuthMethod1
76*lookup_authmethod1(int type)
77{
78 int i;
79
Damien Millerd62f2ca2006-03-26 13:57:41 +110080 for (i = 0; auth1_methods[i].name != NULL; i++)
Damien Miller6abf57c2005-06-19 07:31:37 +100081 if (auth1_methods[i].type == type)
82 return (&(auth1_methods[i]));
83
84 return (NULL);
85}
86
Ben Lindstrombba81212001-06-25 05:01:22 +000087static char *
Damien Millereba71ba2000-04-29 23:57:08 +100088get_authname(int type)
89{
Damien Miller6abf57c2005-06-19 07:31:37 +100090 const struct AuthMethod1 *a;
91 static char buf[64];
92
93 if ((a = lookup_authmethod1(type)) != NULL)
94 return (a->name);
95 snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
96 return (buf);
97}
98
Damien Miller91d4b122006-03-26 14:05:20 +110099/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000100static int
101auth1_process_password(Authctxt *authctxt, char *info, size_t infolen)
102{
103 int authenticated = 0;
104 char *password;
105 u_int dlen;
106
107 /*
108 * Read user password. It is in plain text, but was
109 * transmitted over the encrypted channel so it is
110 * not visible to an outside observer.
111 */
112 password = packet_get_string(&dlen);
113 packet_check_eom();
114
115 /* Try authentication with the password. */
116 authenticated = PRIVSEP(auth_password(authctxt, password));
117
118 memset(password, 0, dlen);
119 xfree(password);
120
121 return (authenticated);
122}
123
Damien Miller91d4b122006-03-26 14:05:20 +1100124/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000125static int
126auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen)
127{
128 int authenticated = 0;
129 BIGNUM *n;
130
131 /* RSA authentication requested. */
132 if ((n = BN_new()) == NULL)
133 fatal("do_authloop: BN_new failed");
134 packet_get_bignum(n);
135 packet_check_eom();
136 authenticated = auth_rsa(authctxt, n);
137 BN_clear_free(n);
138
139 return (authenticated);
140}
141
Damien Miller91d4b122006-03-26 14:05:20 +1100142/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000143static int
144auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen)
145{
Damien Miller06221f12005-06-19 07:36:10 +1000146 int keybits, authenticated = 0;
Damien Miller6abf57c2005-06-19 07:31:37 +1000147 u_int bits;
148 Key *client_host_key;
149 u_int ulen;
150
151 /*
152 * Get client user name. Note that we just have to
153 * trust the client; root on the client machine can
154 * claim to be any user.
155 */
156 client_user = packet_get_string(&ulen);
157
158 /* Get the client host key. */
159 client_host_key = key_new(KEY_RSA1);
160 bits = packet_get_int();
161 packet_get_bignum(client_host_key->rsa->e);
162 packet_get_bignum(client_host_key->rsa->n);
163
Damien Miller06221f12005-06-19 07:36:10 +1000164 keybits = BN_num_bits(client_host_key->rsa->n);
165 if (keybits < 0 || bits != (u_int)keybits) {
Damien Miller6abf57c2005-06-19 07:31:37 +1000166 verbose("Warning: keysize mismatch for client_host_key: "
167 "actual %d, announced %d",
168 BN_num_bits(client_host_key->rsa->n), bits);
Damien Millereba71ba2000-04-29 23:57:08 +1000169 }
Damien Miller6abf57c2005-06-19 07:31:37 +1000170 packet_check_eom();
171
172 authenticated = auth_rhosts_rsa(authctxt, client_user,
173 client_host_key);
174 key_free(client_host_key);
175
176 snprintf(info, infolen, " ruser %.100s", client_user);
Damien Miller94cf4c82005-07-17 17:04:47 +1000177
Damien Miller6abf57c2005-06-19 07:31:37 +1000178 return (authenticated);
179}
180
Damien Miller91d4b122006-03-26 14:05:20 +1100181/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000182static int
183auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen)
184{
185 char *challenge;
Damien Miller46d38de2005-07-17 17:02:09 +1000186
Damien Miller6abf57c2005-06-19 07:31:37 +1000187 if ((challenge = get_challenge(authctxt)) == NULL)
188 return (0);
189
190 debug("sending challenge '%s'", challenge);
191 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
192 packet_put_cstring(challenge);
193 xfree(challenge);
194 packet_send();
195 packet_write_wait();
196
197 return (-1);
198}
199
Damien Miller91d4b122006-03-26 14:05:20 +1100200/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000201static int
202auth1_process_tis_response(Authctxt *authctxt, char *info, size_t infolen)
203{
204 int authenticated = 0;
205 char *response;
206 u_int dlen;
207
208 response = packet_get_string(&dlen);
209 packet_check_eom();
210 authenticated = verify_response(authctxt, response);
211 memset(response, 'r', dlen);
212 xfree(response);
213
214 return (authenticated);
Damien Millereba71ba2000-04-29 23:57:08 +1000215}
216
217/*
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000218 * read packets, try to authenticate the user and
219 * return only if authentication is successful
Damien Millereba71ba2000-04-29 23:57:08 +1000220 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000221static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000222do_authloop(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000223{
Damien Miller874d77b2000-10-14 16:23:11 +1100224 int authenticated = 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000225 char info[1024];
Damien Miller6abf57c2005-06-19 07:31:37 +1000226 int prev = 0, type = 0;
227 const struct AuthMethod1 *meth;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000228
229 debug("Attempting authentication for %s%.100s.",
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000230 authctxt->valid ? "" : "invalid user ", authctxt->user);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000231
232 /* If the user has no password, accept authentication immediately. */
233 if (options.password_authentication &&
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000234#ifdef KRB5
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000235 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
236#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000237 PRIVSEP(auth_password(authctxt, ""))) {
Darren Tuckera8c73d32004-06-23 09:17:54 +1000238#ifdef USE_PAM
239 if (options.use_pam && (PRIVSEP(do_pam_account())))
240#endif
241 {
242 auth_log(authctxt, 1, "without authentication", "");
243 return;
244 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000245 }
Damien Millereba71ba2000-04-29 23:57:08 +1000246
247 /* Indicate that authentication is needed. */
248 packet_start(SSH_SMSG_FAILURE);
249 packet_send();
250 packet_write_wait();
251
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000252 for (;;) {
Damien Miller874d77b2000-10-14 16:23:11 +1100253 /* default to fail */
254 authenticated = 0;
255
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000256 info[0] = '\0';
Damien Millereba71ba2000-04-29 23:57:08 +1000257
258 /* Get a packet from the client. */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000259 prev = type;
Damien Millerdff50992002-01-22 23:16:32 +1100260 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000261
Damien Miller4f9f42a2003-05-10 19:28:02 +1000262 /*
263 * If we started challenge-response authentication but the
264 * next packet is not a response to our challenge, release
265 * the resources allocated by get_challenge() (which would
266 * normally have been released by verify_response() had we
267 * received such a response)
268 */
269 if (prev == SSH_CMSG_AUTH_TIS &&
270 type != SSH_CMSG_AUTH_TIS_RESPONSE)
271 abandon_challenge_response(authctxt);
272
Damien Miller6abf57c2005-06-19 07:31:37 +1000273 if ((meth = lookup_authmethod1(type)) == NULL) {
274 logit("Unknown message during authentication: "
275 "type %d", type);
276 goto skip;
Damien Millereba71ba2000-04-29 23:57:08 +1000277 }
Damien Miller6abf57c2005-06-19 07:31:37 +1000278
279 if (!*(meth->enabled)) {
280 verbose("%s authentication disabled.", meth->name);
281 goto skip;
282 }
283
284 authenticated = meth->method(authctxt, info, sizeof(info));
285 if (authenticated == -1)
286 continue; /* "postponed" */
287
Damien Miller60396b02001-02-18 17:01:00 +1100288#ifdef BSD_AUTH
289 if (authctxt->as) {
290 auth_close(authctxt->as);
291 authctxt->as = NULL;
292 }
293#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000294 if (!authctxt->valid && authenticated)
295 fatal("INTERNAL ERROR: authenticated invalid user %s",
296 authctxt->user);
Damien Millereba71ba2000-04-29 23:57:08 +1000297
Tim Rice81ed5182002-09-25 17:38:46 -0700298#ifdef _UNICOS
Tim Rice81ed5182002-09-25 17:38:46 -0700299 if (authenticated && cray_access_denied(authctxt->user)) {
300 authenticated = 0;
301 fatal("Access denied for user %s.",authctxt->user);
302 }
303#endif /* _UNICOS */
304
Kevin Stevesef4eea92001-02-05 12:42:17 +0000305#ifdef HAVE_CYGWIN
306 if (authenticated &&
Damien Miller94cf4c82005-07-17 17:04:47 +1000307 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,
Ben Lindstrome35bf122004-06-22 03:37:11 +0000308 authctxt->pw)) {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100309 packet_disconnect("Authentication rejected for uid %d.",
Ben Lindstrome35bf122004-06-22 03:37:11 +0000310 authctxt->pw == NULL ? -1 : authctxt->pw->pw_uid);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100311 authenticated = 0;
312 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000313#else
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000314 /* Special handling for root */
Damien Miller556f9312003-02-24 11:59:26 +1100315 if (authenticated && authctxt->pw->pw_uid == 0 &&
Damien Miller6abf57c2005-06-19 07:31:37 +1000316 !auth_root_allowed(meth->name)) {
317 authenticated = 0;
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100318# ifdef SSH_AUDIT_EVENTS
319 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100320# endif
321 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000322#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000323
Damien Miller1f499fd2003-08-25 13:08:49 +1000324#ifdef USE_PAM
Damien Millera8e06ce2003-11-21 23:48:55 +1100325 if (options.use_pam && authenticated &&
Darren Tuckerc1386672004-12-03 14:33:47 +1100326 !PRIVSEP(do_pam_account())) {
327 char *msg;
328 size_t len;
329
330 error("Access denied for user %s by PAM account "
Damien Millerb6f72f52005-07-17 17:26:43 +1000331 "configuration", authctxt->user);
Darren Tuckerc1386672004-12-03 14:33:47 +1100332 len = buffer_len(&loginmsg);
333 buffer_append(&loginmsg, "\0", 1);
334 msg = buffer_ptr(&loginmsg);
335 /* strip trailing newlines */
336 if (len > 0)
337 while (len > 0 && msg[--len] == '\n')
338 msg[len] = '\0';
339 else
340 msg = "Access denied.";
341 packet_disconnect(msg);
342 }
Damien Miller1f499fd2003-08-25 13:08:49 +1000343#endif
344
Damien Miller6abf57c2005-06-19 07:31:37 +1000345 skip:
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000346 /* Log before sending the reply */
347 auth_log(authctxt, authenticated, get_authname(type), info);
348
Damien Millereba71ba2000-04-29 23:57:08 +1000349 if (client_user != NULL) {
350 xfree(client_user);
351 client_user = NULL;
352 }
353
Damien Miller874d77b2000-10-14 16:23:11 +1100354 if (authenticated)
355 return;
356
Darren Tucker269a1ea2005-02-03 00:20:53 +1100357 if (authctxt->failures++ > options.max_authtries) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100358#ifdef SSH_AUDIT_EVENTS
359 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100360#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000361 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100362 }
Damien Millereba71ba2000-04-29 23:57:08 +1000363
Damien Millereba71ba2000-04-29 23:57:08 +1000364 packet_start(SSH_SMSG_FAILURE);
365 packet_send();
366 packet_write_wait();
367 }
368}
369
370/*
371 * Performs authentication of an incoming connection. Session key has already
372 * been exchanged and encryption is enabled.
373 */
Darren Tucker3e33cec2003-10-02 16:12:36 +1000374void
375do_authentication(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000376{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000377 u_int ulen;
Ben Lindstromc8226382002-04-10 16:22:09 +0000378 char *user, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000379
380 /* Get the name of the user that we wish to log in as. */
Damien Millerdff50992002-01-22 23:16:32 +1100381 packet_read_expect(SSH_CMSG_USER);
Damien Millereba71ba2000-04-29 23:57:08 +1000382
383 /* Get the user name. */
384 user = packet_get_string(&ulen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100385 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000386
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000387 if ((style = strchr(user, ':')) != NULL)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000388 *style++ = '\0';
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000389
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000390 authctxt->user = user;
391 authctxt->style = style;
392
Damien Millereba71ba2000-04-29 23:57:08 +1000393 /* Verify that the user is a valid user. */
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000394 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000395 authctxt->valid = 1;
Damien Miller856f0be2003-09-03 07:32:45 +1000396 else {
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000397 debug("do_authentication: invalid user %s", user);
Damien Miller856f0be2003-09-03 07:32:45 +1000398 authctxt->pw = fakepw();
399 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000400
Damien Miller30d1f842004-07-21 20:48:53 +1000401 setproctitle("%s%s", authctxt->valid ? user : "unknown",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000402 use_privsep ? " [net]" : "");
Ben Lindstromc1ba31f2001-02-15 03:14:11 +0000403
Damien Millereba71ba2000-04-29 23:57:08 +1000404#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000405 if (options.use_pam)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100406 PRIVSEP(start_pam(authctxt));
Damien Millereba71ba2000-04-29 23:57:08 +1000407#endif
408
409 /*
410 * If we are not running as root, the user must have the same uid as
Damien Miller6abf57c2005-06-19 07:31:37 +1000411 * the server.
Damien Millereba71ba2000-04-29 23:57:08 +1000412 */
Damien Miller874d77b2000-10-14 16:23:11 +1100413#ifndef HAVE_CYGWIN
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000414 if (!use_privsep && getuid() != 0 && authctxt->pw &&
415 authctxt->pw->pw_uid != getuid())
Damien Millereba71ba2000-04-29 23:57:08 +1000416 packet_disconnect("Cannot change user when server not running as root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100417#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000418
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000419 /*
Kevin Steves12aaa042001-01-24 21:23:39 +0000420 * Loop until the user has been authenticated or the connection is
421 * closed, do_authloop() returns only if authentication is successful
422 */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000423 do_authloop(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000424
425 /* The user has been authenticated and accepted. */
Damien Miller874d77b2000-10-14 16:23:11 +1100426 packet_start(SSH_SMSG_SUCCESS);
427 packet_send();
428 packet_write_wait();
Damien Millereba71ba2000-04-29 23:57:08 +1000429}