blob: b8a25587232781b79eba9e024388c3bafe5ac879 [file] [log] [blame]
Damien Miller0b4d48b2008-07-05 09:44:53 +10001/* $OpenBSD: auth1.c,v 1.73 2008/07/04 23:30:16 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 Miller1cdde6f2006-07-24 14:07:35 +100015#include <sys/types.h>
16
Damien Millerded319c2006-09-01 15:38:36 +100017#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100018#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100019#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100020#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100021#include <pwd.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100022
Damien Millerb84886b2008-05-19 15:05:07 +100023#include "openbsd-compat/sys-queue.h"
Damien Miller874d77b2000-10-14 16:23:11 +110024#include "xmalloc.h"
25#include "rsa.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000026#include "ssh1.h"
Damien Miller874d77b2000-10-14 16:23:11 +110027#include "packet.h"
28#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000029#include "log.h"
Damien Miller874d77b2000-10-14 16:23:11 +110030#include "servconf.h"
31#include "compat.h"
Damien Millerd7834352006-08-05 12:39:39 +100032#include "key.h"
33#include "hostfile.h"
Damien Miller874d77b2000-10-14 16:23:11 +110034#include "auth.h"
Damien Millerc7ef63d2002-02-05 12:21:42 +110035#include "channels.h"
Damien Miller874d77b2000-10-14 16:23:11 +110036#include "session.h"
Ben Lindstromec95ed92001-07-04 04:21:14 +000037#include "uidswap.h"
Damien Millerd7834352006-08-05 12:39:39 +100038#ifdef GSSAPI
39#include "ssh-gss.h"
40#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000041#include "monitor_wrap.h"
Darren Tuckerc1386672004-12-03 14:33:47 +110042#include "buffer.h"
Damien Miller874d77b2000-10-14 16:23:11 +110043
Damien Millereba71ba2000-04-29 23:57:08 +100044/* import */
45extern ServerOptions options;
Darren Tuckerc1386672004-12-03 14:33:47 +110046extern Buffer loginmsg;
Damien Miller874d77b2000-10-14 16:23:11 +110047
Damien Miller6abf57c2005-06-19 07:31:37 +100048static int auth1_process_password(Authctxt *, char *, size_t);
49static int auth1_process_rsa(Authctxt *, char *, size_t);
50static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t);
51static int auth1_process_tis_challenge(Authctxt *, char *, size_t);
52static int auth1_process_tis_response(Authctxt *, char *, size_t);
53
54static char *client_user = NULL; /* Used to fill in remote user for PAM */
55
56struct AuthMethod1 {
57 int type;
58 char *name;
59 int *enabled;
60 int (*method)(Authctxt *, char *, size_t);
61};
62
63const struct AuthMethod1 auth1_methods[] = {
64 {
65 SSH_CMSG_AUTH_PASSWORD, "password",
66 &options.password_authentication, auth1_process_password
67 },
68 {
69 SSH_CMSG_AUTH_RSA, "rsa",
70 &options.rsa_authentication, auth1_process_rsa
71 },
72 {
73 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
74 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
75 },
76 {
77 SSH_CMSG_AUTH_TIS, "challenge-response",
78 &options.challenge_response_authentication,
79 auth1_process_tis_challenge
80 },
81 {
82 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
83 &options.challenge_response_authentication,
84 auth1_process_tis_response
85 },
86 { -1, NULL, NULL, NULL}
87};
88
89static const struct AuthMethod1
90*lookup_authmethod1(int type)
91{
92 int i;
93
Damien Millerd62f2ca2006-03-26 13:57:41 +110094 for (i = 0; auth1_methods[i].name != NULL; i++)
Damien Miller6abf57c2005-06-19 07:31:37 +100095 if (auth1_methods[i].type == type)
96 return (&(auth1_methods[i]));
97
98 return (NULL);
99}
100
Ben Lindstrombba81212001-06-25 05:01:22 +0000101static char *
Damien Millereba71ba2000-04-29 23:57:08 +1000102get_authname(int type)
103{
Damien Miller6abf57c2005-06-19 07:31:37 +1000104 const struct AuthMethod1 *a;
105 static char buf[64];
106
107 if ((a = lookup_authmethod1(type)) != NULL)
108 return (a->name);
109 snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
110 return (buf);
111}
112
Damien Miller91d4b122006-03-26 14:05:20 +1100113/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000114static int
115auth1_process_password(Authctxt *authctxt, char *info, size_t infolen)
116{
117 int authenticated = 0;
118 char *password;
119 u_int dlen;
120
121 /*
122 * Read user password. It is in plain text, but was
123 * transmitted over the encrypted channel so it is
124 * not visible to an outside observer.
125 */
126 password = packet_get_string(&dlen);
127 packet_check_eom();
128
129 /* Try authentication with the password. */
130 authenticated = PRIVSEP(auth_password(authctxt, password));
131
132 memset(password, 0, dlen);
133 xfree(password);
134
135 return (authenticated);
136}
137
Damien Miller91d4b122006-03-26 14:05:20 +1100138/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000139static int
140auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen)
141{
142 int authenticated = 0;
143 BIGNUM *n;
144
145 /* RSA authentication requested. */
146 if ((n = BN_new()) == NULL)
147 fatal("do_authloop: BN_new failed");
148 packet_get_bignum(n);
149 packet_check_eom();
150 authenticated = auth_rsa(authctxt, n);
151 BN_clear_free(n);
152
153 return (authenticated);
154}
155
Damien Miller91d4b122006-03-26 14:05:20 +1100156/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000157static int
158auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen)
159{
Damien Miller06221f12005-06-19 07:36:10 +1000160 int keybits, authenticated = 0;
Damien Miller6abf57c2005-06-19 07:31:37 +1000161 u_int bits;
162 Key *client_host_key;
163 u_int ulen;
164
165 /*
166 * Get client user name. Note that we just have to
167 * trust the client; root on the client machine can
168 * claim to be any user.
169 */
170 client_user = packet_get_string(&ulen);
171
172 /* Get the client host key. */
173 client_host_key = key_new(KEY_RSA1);
174 bits = packet_get_int();
175 packet_get_bignum(client_host_key->rsa->e);
176 packet_get_bignum(client_host_key->rsa->n);
177
Damien Miller06221f12005-06-19 07:36:10 +1000178 keybits = BN_num_bits(client_host_key->rsa->n);
179 if (keybits < 0 || bits != (u_int)keybits) {
Damien Miller6abf57c2005-06-19 07:31:37 +1000180 verbose("Warning: keysize mismatch for client_host_key: "
181 "actual %d, announced %d",
182 BN_num_bits(client_host_key->rsa->n), bits);
Damien Millereba71ba2000-04-29 23:57:08 +1000183 }
Damien Miller6abf57c2005-06-19 07:31:37 +1000184 packet_check_eom();
185
186 authenticated = auth_rhosts_rsa(authctxt, client_user,
187 client_host_key);
188 key_free(client_host_key);
189
190 snprintf(info, infolen, " ruser %.100s", client_user);
Damien Miller94cf4c82005-07-17 17:04:47 +1000191
Damien Miller6abf57c2005-06-19 07:31:37 +1000192 return (authenticated);
193}
194
Damien Miller91d4b122006-03-26 14:05:20 +1100195/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000196static int
197auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen)
198{
199 char *challenge;
Damien Miller46d38de2005-07-17 17:02:09 +1000200
Damien Miller6abf57c2005-06-19 07:31:37 +1000201 if ((challenge = get_challenge(authctxt)) == NULL)
202 return (0);
203
204 debug("sending challenge '%s'", challenge);
205 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
206 packet_put_cstring(challenge);
207 xfree(challenge);
208 packet_send();
209 packet_write_wait();
210
211 return (-1);
212}
213
Damien Miller91d4b122006-03-26 14:05:20 +1100214/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000215static int
216auth1_process_tis_response(Authctxt *authctxt, char *info, size_t infolen)
217{
218 int authenticated = 0;
219 char *response;
220 u_int dlen;
221
222 response = packet_get_string(&dlen);
223 packet_check_eom();
224 authenticated = verify_response(authctxt, response);
225 memset(response, 'r', dlen);
226 xfree(response);
227
228 return (authenticated);
Damien Millereba71ba2000-04-29 23:57:08 +1000229}
230
231/*
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000232 * read packets, try to authenticate the user and
233 * return only if authentication is successful
Damien Millereba71ba2000-04-29 23:57:08 +1000234 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000235static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000236do_authloop(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000237{
Damien Miller874d77b2000-10-14 16:23:11 +1100238 int authenticated = 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000239 char info[1024];
Damien Miller6abf57c2005-06-19 07:31:37 +1000240 int prev = 0, type = 0;
241 const struct AuthMethod1 *meth;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000242
243 debug("Attempting authentication for %s%.100s.",
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000244 authctxt->valid ? "" : "invalid user ", authctxt->user);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000245
246 /* If the user has no password, accept authentication immediately. */
247 if (options.password_authentication &&
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000248#ifdef KRB5
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000249 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
250#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000251 PRIVSEP(auth_password(authctxt, ""))) {
Darren Tuckera8c73d32004-06-23 09:17:54 +1000252#ifdef USE_PAM
253 if (options.use_pam && (PRIVSEP(do_pam_account())))
254#endif
255 {
256 auth_log(authctxt, 1, "without authentication", "");
257 return;
258 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000259 }
Damien Millereba71ba2000-04-29 23:57:08 +1000260
261 /* Indicate that authentication is needed. */
262 packet_start(SSH_SMSG_FAILURE);
263 packet_send();
264 packet_write_wait();
265
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000266 for (;;) {
Damien Miller874d77b2000-10-14 16:23:11 +1100267 /* default to fail */
268 authenticated = 0;
269
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000270 info[0] = '\0';
Damien Millereba71ba2000-04-29 23:57:08 +1000271
272 /* Get a packet from the client. */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000273 prev = type;
Damien Millerdff50992002-01-22 23:16:32 +1100274 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000275
Damien Miller4f9f42a2003-05-10 19:28:02 +1000276 /*
277 * If we started challenge-response authentication but the
278 * next packet is not a response to our challenge, release
279 * the resources allocated by get_challenge() (which would
280 * normally have been released by verify_response() had we
281 * received such a response)
282 */
283 if (prev == SSH_CMSG_AUTH_TIS &&
284 type != SSH_CMSG_AUTH_TIS_RESPONSE)
285 abandon_challenge_response(authctxt);
286
Damien Miller0b4d48b2008-07-05 09:44:53 +1000287 if (authctxt->failures >= options.max_authtries)
288 goto skip;
Damien Miller6abf57c2005-06-19 07:31:37 +1000289 if ((meth = lookup_authmethod1(type)) == NULL) {
290 logit("Unknown message during authentication: "
291 "type %d", type);
292 goto skip;
Damien Millereba71ba2000-04-29 23:57:08 +1000293 }
Damien Miller6abf57c2005-06-19 07:31:37 +1000294
295 if (!*(meth->enabled)) {
296 verbose("%s authentication disabled.", meth->name);
297 goto skip;
298 }
299
300 authenticated = meth->method(authctxt, info, sizeof(info));
301 if (authenticated == -1)
302 continue; /* "postponed" */
303
Damien Miller60396b02001-02-18 17:01:00 +1100304#ifdef BSD_AUTH
305 if (authctxt->as) {
306 auth_close(authctxt->as);
307 authctxt->as = NULL;
308 }
309#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000310 if (!authctxt->valid && authenticated)
311 fatal("INTERNAL ERROR: authenticated invalid user %s",
312 authctxt->user);
Damien Millereba71ba2000-04-29 23:57:08 +1000313
Tim Rice81ed5182002-09-25 17:38:46 -0700314#ifdef _UNICOS
Tim Rice81ed5182002-09-25 17:38:46 -0700315 if (authenticated && cray_access_denied(authctxt->user)) {
316 authenticated = 0;
317 fatal("Access denied for user %s.",authctxt->user);
318 }
319#endif /* _UNICOS */
320
Kevin Stevesef4eea92001-02-05 12:42:17 +0000321#ifdef HAVE_CYGWIN
322 if (authenticated &&
Damien Miller94cf4c82005-07-17 17:04:47 +1000323 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,
Ben Lindstrome35bf122004-06-22 03:37:11 +0000324 authctxt->pw)) {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100325 packet_disconnect("Authentication rejected for uid %d.",
Ben Lindstrome35bf122004-06-22 03:37:11 +0000326 authctxt->pw == NULL ? -1 : authctxt->pw->pw_uid);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100327 authenticated = 0;
328 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000329#else
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000330 /* Special handling for root */
Damien Miller556f9312003-02-24 11:59:26 +1100331 if (authenticated && authctxt->pw->pw_uid == 0 &&
Damien Miller6abf57c2005-06-19 07:31:37 +1000332 !auth_root_allowed(meth->name)) {
333 authenticated = 0;
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100334# ifdef SSH_AUDIT_EVENTS
335 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100336# endif
337 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000338#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000339
Damien Miller1f499fd2003-08-25 13:08:49 +1000340#ifdef USE_PAM
Damien Millera8e06ce2003-11-21 23:48:55 +1100341 if (options.use_pam && authenticated &&
Darren Tuckerc1386672004-12-03 14:33:47 +1100342 !PRIVSEP(do_pam_account())) {
343 char *msg;
344 size_t len;
345
346 error("Access denied for user %s by PAM account "
Damien Millerb6f72f52005-07-17 17:26:43 +1000347 "configuration", authctxt->user);
Darren Tuckerc1386672004-12-03 14:33:47 +1100348 len = buffer_len(&loginmsg);
349 buffer_append(&loginmsg, "\0", 1);
350 msg = buffer_ptr(&loginmsg);
351 /* strip trailing newlines */
352 if (len > 0)
353 while (len > 0 && msg[--len] == '\n')
354 msg[len] = '\0';
355 else
356 msg = "Access denied.";
Damien Miller773a7b92008-07-09 20:54:05 +1000357 packet_disconnect("%s", msg);
Darren Tuckerc1386672004-12-03 14:33:47 +1100358 }
Damien Miller1f499fd2003-08-25 13:08:49 +1000359#endif
360
Damien Miller6abf57c2005-06-19 07:31:37 +1000361 skip:
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000362 /* Log before sending the reply */
363 auth_log(authctxt, authenticated, get_authname(type), info);
364
Damien Millereba71ba2000-04-29 23:57:08 +1000365 if (client_user != NULL) {
366 xfree(client_user);
367 client_user = NULL;
368 }
369
Damien Miller874d77b2000-10-14 16:23:11 +1100370 if (authenticated)
371 return;
372
Damien Miller0b4d48b2008-07-05 09:44:53 +1000373 if (++authctxt->failures >= options.max_authtries) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100374#ifdef SSH_AUDIT_EVENTS
375 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100376#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000377 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100378 }
Damien Millereba71ba2000-04-29 23:57:08 +1000379
Damien Millereba71ba2000-04-29 23:57:08 +1000380 packet_start(SSH_SMSG_FAILURE);
381 packet_send();
382 packet_write_wait();
383 }
384}
385
386/*
387 * Performs authentication of an incoming connection. Session key has already
388 * been exchanged and encryption is enabled.
389 */
Darren Tucker3e33cec2003-10-02 16:12:36 +1000390void
391do_authentication(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000392{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000393 u_int ulen;
Ben Lindstromc8226382002-04-10 16:22:09 +0000394 char *user, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000395
396 /* Get the name of the user that we wish to log in as. */
Damien Millerdff50992002-01-22 23:16:32 +1100397 packet_read_expect(SSH_CMSG_USER);
Damien Millereba71ba2000-04-29 23:57:08 +1000398
399 /* Get the user name. */
400 user = packet_get_string(&ulen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100401 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000402
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000403 if ((style = strchr(user, ':')) != NULL)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000404 *style++ = '\0';
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000405
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000406 authctxt->user = user;
407 authctxt->style = style;
408
Damien Millereba71ba2000-04-29 23:57:08 +1000409 /* Verify that the user is a valid user. */
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000410 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000411 authctxt->valid = 1;
Damien Miller856f0be2003-09-03 07:32:45 +1000412 else {
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000413 debug("do_authentication: invalid user %s", user);
Damien Miller856f0be2003-09-03 07:32:45 +1000414 authctxt->pw = fakepw();
415 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000416
Damien Miller30d1f842004-07-21 20:48:53 +1000417 setproctitle("%s%s", authctxt->valid ? user : "unknown",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000418 use_privsep ? " [net]" : "");
Ben Lindstromc1ba31f2001-02-15 03:14:11 +0000419
Damien Millereba71ba2000-04-29 23:57:08 +1000420#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000421 if (options.use_pam)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100422 PRIVSEP(start_pam(authctxt));
Damien Millereba71ba2000-04-29 23:57:08 +1000423#endif
424
425 /*
426 * If we are not running as root, the user must have the same uid as
Damien Miller6abf57c2005-06-19 07:31:37 +1000427 * the server.
Damien Millereba71ba2000-04-29 23:57:08 +1000428 */
Damien Miller874d77b2000-10-14 16:23:11 +1100429#ifndef HAVE_CYGWIN
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000430 if (!use_privsep && getuid() != 0 && authctxt->pw &&
431 authctxt->pw->pw_uid != getuid())
Damien Millereba71ba2000-04-29 23:57:08 +1000432 packet_disconnect("Cannot change user when server not running as root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100433#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000434
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000435 /*
Kevin Steves12aaa042001-01-24 21:23:39 +0000436 * Loop until the user has been authenticated or the connection is
437 * closed, do_authloop() returns only if authentication is successful
438 */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000439 do_authloop(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000440
441 /* The user has been authenticated and accepted. */
Damien Miller874d77b2000-10-14 16:23:11 +1100442 packet_start(SSH_SMSG_SUCCESS);
443 packet_send();
444 packet_write_wait();
Damien Millereba71ba2000-04-29 23:57:08 +1000445}