blob: b9d6b1115f4f98d778f80186eacc41ee5cc6ce17 [file] [log] [blame]
Damien Millerd7834352006-08-05 12:39:39 +10001/* $OpenBSD: auth1.c,v 1.70 2006/08/03 03:34:41 deraadt 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 Miller874d77b2000-10-14 16:23:11 +110023#include "xmalloc.h"
24#include "rsa.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000025#include "ssh1.h"
Damien Miller874d77b2000-10-14 16:23:11 +110026#include "packet.h"
27#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000028#include "log.h"
Damien Miller874d77b2000-10-14 16:23:11 +110029#include "servconf.h"
30#include "compat.h"
Damien Millerd7834352006-08-05 12:39:39 +100031#include "key.h"
32#include "hostfile.h"
Damien Miller874d77b2000-10-14 16:23:11 +110033#include "auth.h"
Damien Millerc7ef63d2002-02-05 12:21:42 +110034#include "channels.h"
Damien Miller874d77b2000-10-14 16:23:11 +110035#include "session.h"
Ben Lindstromec95ed92001-07-04 04:21:14 +000036#include "uidswap.h"
Damien Millerd7834352006-08-05 12:39:39 +100037#ifdef GSSAPI
38#include "ssh-gss.h"
39#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000040#include "monitor_wrap.h"
Darren Tuckerc1386672004-12-03 14:33:47 +110041#include "buffer.h"
Damien Miller874d77b2000-10-14 16:23:11 +110042
Damien Millereba71ba2000-04-29 23:57:08 +100043/* import */
44extern ServerOptions options;
Darren Tuckerc1386672004-12-03 14:33:47 +110045extern Buffer loginmsg;
Damien Miller874d77b2000-10-14 16:23:11 +110046
Damien Miller6abf57c2005-06-19 07:31:37 +100047static int auth1_process_password(Authctxt *, char *, size_t);
48static int auth1_process_rsa(Authctxt *, char *, size_t);
49static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t);
50static int auth1_process_tis_challenge(Authctxt *, char *, size_t);
51static int auth1_process_tis_response(Authctxt *, char *, size_t);
52
53static char *client_user = NULL; /* Used to fill in remote user for PAM */
54
55struct AuthMethod1 {
56 int type;
57 char *name;
58 int *enabled;
59 int (*method)(Authctxt *, char *, size_t);
60};
61
62const struct AuthMethod1 auth1_methods[] = {
63 {
64 SSH_CMSG_AUTH_PASSWORD, "password",
65 &options.password_authentication, auth1_process_password
66 },
67 {
68 SSH_CMSG_AUTH_RSA, "rsa",
69 &options.rsa_authentication, auth1_process_rsa
70 },
71 {
72 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
73 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
74 },
75 {
76 SSH_CMSG_AUTH_TIS, "challenge-response",
77 &options.challenge_response_authentication,
78 auth1_process_tis_challenge
79 },
80 {
81 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
82 &options.challenge_response_authentication,
83 auth1_process_tis_response
84 },
85 { -1, NULL, NULL, NULL}
86};
87
88static const struct AuthMethod1
89*lookup_authmethod1(int type)
90{
91 int i;
92
Damien Millerd62f2ca2006-03-26 13:57:41 +110093 for (i = 0; auth1_methods[i].name != NULL; i++)
Damien Miller6abf57c2005-06-19 07:31:37 +100094 if (auth1_methods[i].type == type)
95 return (&(auth1_methods[i]));
96
97 return (NULL);
98}
99
Ben Lindstrombba81212001-06-25 05:01:22 +0000100static char *
Damien Millereba71ba2000-04-29 23:57:08 +1000101get_authname(int type)
102{
Damien Miller6abf57c2005-06-19 07:31:37 +1000103 const struct AuthMethod1 *a;
104 static char buf[64];
105
106 if ((a = lookup_authmethod1(type)) != NULL)
107 return (a->name);
108 snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
109 return (buf);
110}
111
Damien Miller91d4b122006-03-26 14:05:20 +1100112/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000113static int
114auth1_process_password(Authctxt *authctxt, char *info, size_t infolen)
115{
116 int authenticated = 0;
117 char *password;
118 u_int dlen;
119
120 /*
121 * Read user password. It is in plain text, but was
122 * transmitted over the encrypted channel so it is
123 * not visible to an outside observer.
124 */
125 password = packet_get_string(&dlen);
126 packet_check_eom();
127
128 /* Try authentication with the password. */
129 authenticated = PRIVSEP(auth_password(authctxt, password));
130
131 memset(password, 0, dlen);
132 xfree(password);
133
134 return (authenticated);
135}
136
Damien Miller91d4b122006-03-26 14:05:20 +1100137/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000138static int
139auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen)
140{
141 int authenticated = 0;
142 BIGNUM *n;
143
144 /* RSA authentication requested. */
145 if ((n = BN_new()) == NULL)
146 fatal("do_authloop: BN_new failed");
147 packet_get_bignum(n);
148 packet_check_eom();
149 authenticated = auth_rsa(authctxt, n);
150 BN_clear_free(n);
151
152 return (authenticated);
153}
154
Damien Miller91d4b122006-03-26 14:05:20 +1100155/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000156static int
157auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen)
158{
Damien Miller06221f12005-06-19 07:36:10 +1000159 int keybits, authenticated = 0;
Damien Miller6abf57c2005-06-19 07:31:37 +1000160 u_int bits;
161 Key *client_host_key;
162 u_int ulen;
163
164 /*
165 * Get client user name. Note that we just have to
166 * trust the client; root on the client machine can
167 * claim to be any user.
168 */
169 client_user = packet_get_string(&ulen);
170
171 /* Get the client host key. */
172 client_host_key = key_new(KEY_RSA1);
173 bits = packet_get_int();
174 packet_get_bignum(client_host_key->rsa->e);
175 packet_get_bignum(client_host_key->rsa->n);
176
Damien Miller06221f12005-06-19 07:36:10 +1000177 keybits = BN_num_bits(client_host_key->rsa->n);
178 if (keybits < 0 || bits != (u_int)keybits) {
Damien Miller6abf57c2005-06-19 07:31:37 +1000179 verbose("Warning: keysize mismatch for client_host_key: "
180 "actual %d, announced %d",
181 BN_num_bits(client_host_key->rsa->n), bits);
Damien Millereba71ba2000-04-29 23:57:08 +1000182 }
Damien Miller6abf57c2005-06-19 07:31:37 +1000183 packet_check_eom();
184
185 authenticated = auth_rhosts_rsa(authctxt, client_user,
186 client_host_key);
187 key_free(client_host_key);
188
189 snprintf(info, infolen, " ruser %.100s", client_user);
Damien Miller94cf4c82005-07-17 17:04:47 +1000190
Damien Miller6abf57c2005-06-19 07:31:37 +1000191 return (authenticated);
192}
193
Damien Miller91d4b122006-03-26 14:05:20 +1100194/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000195static int
196auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen)
197{
198 char *challenge;
Damien Miller46d38de2005-07-17 17:02:09 +1000199
Damien Miller6abf57c2005-06-19 07:31:37 +1000200 if ((challenge = get_challenge(authctxt)) == NULL)
201 return (0);
202
203 debug("sending challenge '%s'", challenge);
204 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
205 packet_put_cstring(challenge);
206 xfree(challenge);
207 packet_send();
208 packet_write_wait();
209
210 return (-1);
211}
212
Damien Miller91d4b122006-03-26 14:05:20 +1100213/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000214static int
215auth1_process_tis_response(Authctxt *authctxt, char *info, size_t infolen)
216{
217 int authenticated = 0;
218 char *response;
219 u_int dlen;
220
221 response = packet_get_string(&dlen);
222 packet_check_eom();
223 authenticated = verify_response(authctxt, response);
224 memset(response, 'r', dlen);
225 xfree(response);
226
227 return (authenticated);
Damien Millereba71ba2000-04-29 23:57:08 +1000228}
229
230/*
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000231 * read packets, try to authenticate the user and
232 * return only if authentication is successful
Damien Millereba71ba2000-04-29 23:57:08 +1000233 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000234static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000235do_authloop(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000236{
Damien Miller874d77b2000-10-14 16:23:11 +1100237 int authenticated = 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000238 char info[1024];
Damien Miller6abf57c2005-06-19 07:31:37 +1000239 int prev = 0, type = 0;
240 const struct AuthMethod1 *meth;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000241
242 debug("Attempting authentication for %s%.100s.",
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000243 authctxt->valid ? "" : "invalid user ", authctxt->user);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000244
245 /* If the user has no password, accept authentication immediately. */
246 if (options.password_authentication &&
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000247#ifdef KRB5
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000248 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
249#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000250 PRIVSEP(auth_password(authctxt, ""))) {
Darren Tuckera8c73d32004-06-23 09:17:54 +1000251#ifdef USE_PAM
252 if (options.use_pam && (PRIVSEP(do_pam_account())))
253#endif
254 {
255 auth_log(authctxt, 1, "without authentication", "");
256 return;
257 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000258 }
Damien Millereba71ba2000-04-29 23:57:08 +1000259
260 /* Indicate that authentication is needed. */
261 packet_start(SSH_SMSG_FAILURE);
262 packet_send();
263 packet_write_wait();
264
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000265 for (;;) {
Damien Miller874d77b2000-10-14 16:23:11 +1100266 /* default to fail */
267 authenticated = 0;
268
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000269 info[0] = '\0';
Damien Millereba71ba2000-04-29 23:57:08 +1000270
271 /* Get a packet from the client. */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000272 prev = type;
Damien Millerdff50992002-01-22 23:16:32 +1100273 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000274
Damien Miller4f9f42a2003-05-10 19:28:02 +1000275 /*
276 * If we started challenge-response authentication but the
277 * next packet is not a response to our challenge, release
278 * the resources allocated by get_challenge() (which would
279 * normally have been released by verify_response() had we
280 * received such a response)
281 */
282 if (prev == SSH_CMSG_AUTH_TIS &&
283 type != SSH_CMSG_AUTH_TIS_RESPONSE)
284 abandon_challenge_response(authctxt);
285
Damien Miller6abf57c2005-06-19 07:31:37 +1000286 if ((meth = lookup_authmethod1(type)) == NULL) {
287 logit("Unknown message during authentication: "
288 "type %d", type);
289 goto skip;
Damien Millereba71ba2000-04-29 23:57:08 +1000290 }
Damien Miller6abf57c2005-06-19 07:31:37 +1000291
292 if (!*(meth->enabled)) {
293 verbose("%s authentication disabled.", meth->name);
294 goto skip;
295 }
296
297 authenticated = meth->method(authctxt, info, sizeof(info));
298 if (authenticated == -1)
299 continue; /* "postponed" */
300
Damien Miller60396b02001-02-18 17:01:00 +1100301#ifdef BSD_AUTH
302 if (authctxt->as) {
303 auth_close(authctxt->as);
304 authctxt->as = NULL;
305 }
306#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000307 if (!authctxt->valid && authenticated)
308 fatal("INTERNAL ERROR: authenticated invalid user %s",
309 authctxt->user);
Damien Millereba71ba2000-04-29 23:57:08 +1000310
Tim Rice81ed5182002-09-25 17:38:46 -0700311#ifdef _UNICOS
Tim Rice81ed5182002-09-25 17:38:46 -0700312 if (authenticated && cray_access_denied(authctxt->user)) {
313 authenticated = 0;
314 fatal("Access denied for user %s.",authctxt->user);
315 }
316#endif /* _UNICOS */
317
Kevin Stevesef4eea92001-02-05 12:42:17 +0000318#ifdef HAVE_CYGWIN
319 if (authenticated &&
Damien Miller94cf4c82005-07-17 17:04:47 +1000320 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,
Ben Lindstrome35bf122004-06-22 03:37:11 +0000321 authctxt->pw)) {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100322 packet_disconnect("Authentication rejected for uid %d.",
Ben Lindstrome35bf122004-06-22 03:37:11 +0000323 authctxt->pw == NULL ? -1 : authctxt->pw->pw_uid);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100324 authenticated = 0;
325 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000326#else
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000327 /* Special handling for root */
Damien Miller556f9312003-02-24 11:59:26 +1100328 if (authenticated && authctxt->pw->pw_uid == 0 &&
Damien Miller6abf57c2005-06-19 07:31:37 +1000329 !auth_root_allowed(meth->name)) {
330 authenticated = 0;
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100331# ifdef SSH_AUDIT_EVENTS
332 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100333# endif
334 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000335#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000336
Damien Miller1f499fd2003-08-25 13:08:49 +1000337#ifdef USE_PAM
Damien Millera8e06ce2003-11-21 23:48:55 +1100338 if (options.use_pam && authenticated &&
Darren Tuckerc1386672004-12-03 14:33:47 +1100339 !PRIVSEP(do_pam_account())) {
340 char *msg;
341 size_t len;
342
343 error("Access denied for user %s by PAM account "
Damien Millerb6f72f52005-07-17 17:26:43 +1000344 "configuration", authctxt->user);
Darren Tuckerc1386672004-12-03 14:33:47 +1100345 len = buffer_len(&loginmsg);
346 buffer_append(&loginmsg, "\0", 1);
347 msg = buffer_ptr(&loginmsg);
348 /* strip trailing newlines */
349 if (len > 0)
350 while (len > 0 && msg[--len] == '\n')
351 msg[len] = '\0';
352 else
353 msg = "Access denied.";
354 packet_disconnect(msg);
355 }
Damien Miller1f499fd2003-08-25 13:08:49 +1000356#endif
357
Damien Miller6abf57c2005-06-19 07:31:37 +1000358 skip:
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000359 /* Log before sending the reply */
360 auth_log(authctxt, authenticated, get_authname(type), info);
361
Damien Millereba71ba2000-04-29 23:57:08 +1000362 if (client_user != NULL) {
363 xfree(client_user);
364 client_user = NULL;
365 }
366
Damien Miller874d77b2000-10-14 16:23:11 +1100367 if (authenticated)
368 return;
369
Darren Tucker269a1ea2005-02-03 00:20:53 +1100370 if (authctxt->failures++ > options.max_authtries) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100371#ifdef SSH_AUDIT_EVENTS
372 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100373#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000374 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100375 }
Damien Millereba71ba2000-04-29 23:57:08 +1000376
Damien Millereba71ba2000-04-29 23:57:08 +1000377 packet_start(SSH_SMSG_FAILURE);
378 packet_send();
379 packet_write_wait();
380 }
381}
382
383/*
384 * Performs authentication of an incoming connection. Session key has already
385 * been exchanged and encryption is enabled.
386 */
Darren Tucker3e33cec2003-10-02 16:12:36 +1000387void
388do_authentication(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000389{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000390 u_int ulen;
Ben Lindstromc8226382002-04-10 16:22:09 +0000391 char *user, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000392
393 /* Get the name of the user that we wish to log in as. */
Damien Millerdff50992002-01-22 23:16:32 +1100394 packet_read_expect(SSH_CMSG_USER);
Damien Millereba71ba2000-04-29 23:57:08 +1000395
396 /* Get the user name. */
397 user = packet_get_string(&ulen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100398 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000399
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000400 if ((style = strchr(user, ':')) != NULL)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000401 *style++ = '\0';
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000402
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000403 authctxt->user = user;
404 authctxt->style = style;
405
Damien Millereba71ba2000-04-29 23:57:08 +1000406 /* Verify that the user is a valid user. */
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000407 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000408 authctxt->valid = 1;
Damien Miller856f0be2003-09-03 07:32:45 +1000409 else {
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000410 debug("do_authentication: invalid user %s", user);
Damien Miller856f0be2003-09-03 07:32:45 +1000411 authctxt->pw = fakepw();
412 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000413
Damien Miller30d1f842004-07-21 20:48:53 +1000414 setproctitle("%s%s", authctxt->valid ? user : "unknown",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000415 use_privsep ? " [net]" : "");
Ben Lindstromc1ba31f2001-02-15 03:14:11 +0000416
Damien Millereba71ba2000-04-29 23:57:08 +1000417#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000418 if (options.use_pam)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100419 PRIVSEP(start_pam(authctxt));
Damien Millereba71ba2000-04-29 23:57:08 +1000420#endif
421
422 /*
423 * If we are not running as root, the user must have the same uid as
Damien Miller6abf57c2005-06-19 07:31:37 +1000424 * the server.
Damien Millereba71ba2000-04-29 23:57:08 +1000425 */
Damien Miller874d77b2000-10-14 16:23:11 +1100426#ifndef HAVE_CYGWIN
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000427 if (!use_privsep && getuid() != 0 && authctxt->pw &&
428 authctxt->pw->pw_uid != getuid())
Damien Millereba71ba2000-04-29 23:57:08 +1000429 packet_disconnect("Cannot change user when server not running as root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100430#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000431
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000432 /*
Kevin Steves12aaa042001-01-24 21:23:39 +0000433 * Loop until the user has been authenticated or the connection is
434 * closed, do_authloop() returns only if authentication is successful
435 */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000436 do_authloop(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000437
438 /* The user has been authenticated and accepted. */
Damien Miller874d77b2000-10-14 16:23:11 +1100439 packet_start(SSH_SMSG_SUCCESS);
440 packet_send();
441 packet_write_wait();
Damien Millereba71ba2000-04-29 23:57:08 +1000442}