blob: 34dcf62669e2290ac82c6df7027c4802cdbcf30c [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 Millera7a73ee2006-08-05 11:37:59 +100017#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100018#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100019#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100020#include <pwd.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100021
Damien Miller874d77b2000-10-14 16:23:11 +110022#include "xmalloc.h"
23#include "rsa.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000024#include "ssh1.h"
Damien Miller874d77b2000-10-14 16:23:11 +110025#include "packet.h"
26#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000027#include "log.h"
Damien Miller874d77b2000-10-14 16:23:11 +110028#include "servconf.h"
29#include "compat.h"
Damien Millerd7834352006-08-05 12:39:39 +100030#include "key.h"
31#include "hostfile.h"
Damien Miller874d77b2000-10-14 16:23:11 +110032#include "auth.h"
Damien Millerc7ef63d2002-02-05 12:21:42 +110033#include "channels.h"
Damien Miller874d77b2000-10-14 16:23:11 +110034#include "session.h"
Ben Lindstromec95ed92001-07-04 04:21:14 +000035#include "uidswap.h"
Damien Millerd7834352006-08-05 12:39:39 +100036#ifdef GSSAPI
37#include "ssh-gss.h"
38#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000039#include "monitor_wrap.h"
Darren Tuckerc1386672004-12-03 14:33:47 +110040#include "buffer.h"
Damien Miller874d77b2000-10-14 16:23:11 +110041
Damien Millereba71ba2000-04-29 23:57:08 +100042/* import */
43extern ServerOptions options;
Darren Tuckerc1386672004-12-03 14:33:47 +110044extern Buffer loginmsg;
Damien Miller874d77b2000-10-14 16:23:11 +110045
Damien Miller6abf57c2005-06-19 07:31:37 +100046static int auth1_process_password(Authctxt *, char *, size_t);
47static int auth1_process_rsa(Authctxt *, char *, size_t);
48static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t);
49static int auth1_process_tis_challenge(Authctxt *, char *, size_t);
50static int auth1_process_tis_response(Authctxt *, char *, size_t);
51
52static char *client_user = NULL; /* Used to fill in remote user for PAM */
53
54struct AuthMethod1 {
55 int type;
56 char *name;
57 int *enabled;
58 int (*method)(Authctxt *, char *, size_t);
59};
60
61const struct AuthMethod1 auth1_methods[] = {
62 {
63 SSH_CMSG_AUTH_PASSWORD, "password",
64 &options.password_authentication, auth1_process_password
65 },
66 {
67 SSH_CMSG_AUTH_RSA, "rsa",
68 &options.rsa_authentication, auth1_process_rsa
69 },
70 {
71 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
72 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
73 },
74 {
75 SSH_CMSG_AUTH_TIS, "challenge-response",
76 &options.challenge_response_authentication,
77 auth1_process_tis_challenge
78 },
79 {
80 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
81 &options.challenge_response_authentication,
82 auth1_process_tis_response
83 },
84 { -1, NULL, NULL, NULL}
85};
86
87static const struct AuthMethod1
88*lookup_authmethod1(int type)
89{
90 int i;
91
Damien Millerd62f2ca2006-03-26 13:57:41 +110092 for (i = 0; auth1_methods[i].name != NULL; i++)
Damien Miller6abf57c2005-06-19 07:31:37 +100093 if (auth1_methods[i].type == type)
94 return (&(auth1_methods[i]));
95
96 return (NULL);
97}
98
Ben Lindstrombba81212001-06-25 05:01:22 +000099static char *
Damien Millereba71ba2000-04-29 23:57:08 +1000100get_authname(int type)
101{
Damien Miller6abf57c2005-06-19 07:31:37 +1000102 const struct AuthMethod1 *a;
103 static char buf[64];
104
105 if ((a = lookup_authmethod1(type)) != NULL)
106 return (a->name);
107 snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
108 return (buf);
109}
110
Damien Miller91d4b122006-03-26 14:05:20 +1100111/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000112static int
113auth1_process_password(Authctxt *authctxt, char *info, size_t infolen)
114{
115 int authenticated = 0;
116 char *password;
117 u_int dlen;
118
119 /*
120 * Read user password. It is in plain text, but was
121 * transmitted over the encrypted channel so it is
122 * not visible to an outside observer.
123 */
124 password = packet_get_string(&dlen);
125 packet_check_eom();
126
127 /* Try authentication with the password. */
128 authenticated = PRIVSEP(auth_password(authctxt, password));
129
130 memset(password, 0, dlen);
131 xfree(password);
132
133 return (authenticated);
134}
135
Damien Miller91d4b122006-03-26 14:05:20 +1100136/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000137static int
138auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen)
139{
140 int authenticated = 0;
141 BIGNUM *n;
142
143 /* RSA authentication requested. */
144 if ((n = BN_new()) == NULL)
145 fatal("do_authloop: BN_new failed");
146 packet_get_bignum(n);
147 packet_check_eom();
148 authenticated = auth_rsa(authctxt, n);
149 BN_clear_free(n);
150
151 return (authenticated);
152}
153
Damien Miller91d4b122006-03-26 14:05:20 +1100154/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000155static int
156auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen)
157{
Damien Miller06221f12005-06-19 07:36:10 +1000158 int keybits, authenticated = 0;
Damien Miller6abf57c2005-06-19 07:31:37 +1000159 u_int bits;
160 Key *client_host_key;
161 u_int ulen;
162
163 /*
164 * Get client user name. Note that we just have to
165 * trust the client; root on the client machine can
166 * claim to be any user.
167 */
168 client_user = packet_get_string(&ulen);
169
170 /* Get the client host key. */
171 client_host_key = key_new(KEY_RSA1);
172 bits = packet_get_int();
173 packet_get_bignum(client_host_key->rsa->e);
174 packet_get_bignum(client_host_key->rsa->n);
175
Damien Miller06221f12005-06-19 07:36:10 +1000176 keybits = BN_num_bits(client_host_key->rsa->n);
177 if (keybits < 0 || bits != (u_int)keybits) {
Damien Miller6abf57c2005-06-19 07:31:37 +1000178 verbose("Warning: keysize mismatch for client_host_key: "
179 "actual %d, announced %d",
180 BN_num_bits(client_host_key->rsa->n), bits);
Damien Millereba71ba2000-04-29 23:57:08 +1000181 }
Damien Miller6abf57c2005-06-19 07:31:37 +1000182 packet_check_eom();
183
184 authenticated = auth_rhosts_rsa(authctxt, client_user,
185 client_host_key);
186 key_free(client_host_key);
187
188 snprintf(info, infolen, " ruser %.100s", client_user);
Damien Miller94cf4c82005-07-17 17:04:47 +1000189
Damien Miller6abf57c2005-06-19 07:31:37 +1000190 return (authenticated);
191}
192
Damien Miller91d4b122006-03-26 14:05:20 +1100193/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000194static int
195auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen)
196{
197 char *challenge;
Damien Miller46d38de2005-07-17 17:02:09 +1000198
Damien Miller6abf57c2005-06-19 07:31:37 +1000199 if ((challenge = get_challenge(authctxt)) == NULL)
200 return (0);
201
202 debug("sending challenge '%s'", challenge);
203 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
204 packet_put_cstring(challenge);
205 xfree(challenge);
206 packet_send();
207 packet_write_wait();
208
209 return (-1);
210}
211
Damien Miller91d4b122006-03-26 14:05:20 +1100212/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000213static int
214auth1_process_tis_response(Authctxt *authctxt, char *info, size_t infolen)
215{
216 int authenticated = 0;
217 char *response;
218 u_int dlen;
219
220 response = packet_get_string(&dlen);
221 packet_check_eom();
222 authenticated = verify_response(authctxt, response);
223 memset(response, 'r', dlen);
224 xfree(response);
225
226 return (authenticated);
Damien Millereba71ba2000-04-29 23:57:08 +1000227}
228
229/*
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000230 * read packets, try to authenticate the user and
231 * return only if authentication is successful
Damien Millereba71ba2000-04-29 23:57:08 +1000232 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000233static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000234do_authloop(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000235{
Damien Miller874d77b2000-10-14 16:23:11 +1100236 int authenticated = 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000237 char info[1024];
Damien Miller6abf57c2005-06-19 07:31:37 +1000238 int prev = 0, type = 0;
239 const struct AuthMethod1 *meth;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000240
241 debug("Attempting authentication for %s%.100s.",
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000242 authctxt->valid ? "" : "invalid user ", authctxt->user);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000243
244 /* If the user has no password, accept authentication immediately. */
245 if (options.password_authentication &&
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000246#ifdef KRB5
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000247 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
248#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000249 PRIVSEP(auth_password(authctxt, ""))) {
Darren Tuckera8c73d32004-06-23 09:17:54 +1000250#ifdef USE_PAM
251 if (options.use_pam && (PRIVSEP(do_pam_account())))
252#endif
253 {
254 auth_log(authctxt, 1, "without authentication", "");
255 return;
256 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000257 }
Damien Millereba71ba2000-04-29 23:57:08 +1000258
259 /* Indicate that authentication is needed. */
260 packet_start(SSH_SMSG_FAILURE);
261 packet_send();
262 packet_write_wait();
263
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000264 for (;;) {
Damien Miller874d77b2000-10-14 16:23:11 +1100265 /* default to fail */
266 authenticated = 0;
267
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000268 info[0] = '\0';
Damien Millereba71ba2000-04-29 23:57:08 +1000269
270 /* Get a packet from the client. */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000271 prev = type;
Damien Millerdff50992002-01-22 23:16:32 +1100272 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000273
Damien Miller4f9f42a2003-05-10 19:28:02 +1000274 /*
275 * If we started challenge-response authentication but the
276 * next packet is not a response to our challenge, release
277 * the resources allocated by get_challenge() (which would
278 * normally have been released by verify_response() had we
279 * received such a response)
280 */
281 if (prev == SSH_CMSG_AUTH_TIS &&
282 type != SSH_CMSG_AUTH_TIS_RESPONSE)
283 abandon_challenge_response(authctxt);
284
Damien Miller6abf57c2005-06-19 07:31:37 +1000285 if ((meth = lookup_authmethod1(type)) == NULL) {
286 logit("Unknown message during authentication: "
287 "type %d", type);
288 goto skip;
Damien Millereba71ba2000-04-29 23:57:08 +1000289 }
Damien Miller6abf57c2005-06-19 07:31:37 +1000290
291 if (!*(meth->enabled)) {
292 verbose("%s authentication disabled.", meth->name);
293 goto skip;
294 }
295
296 authenticated = meth->method(authctxt, info, sizeof(info));
297 if (authenticated == -1)
298 continue; /* "postponed" */
299
Damien Miller60396b02001-02-18 17:01:00 +1100300#ifdef BSD_AUTH
301 if (authctxt->as) {
302 auth_close(authctxt->as);
303 authctxt->as = NULL;
304 }
305#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000306 if (!authctxt->valid && authenticated)
307 fatal("INTERNAL ERROR: authenticated invalid user %s",
308 authctxt->user);
Damien Millereba71ba2000-04-29 23:57:08 +1000309
Tim Rice81ed5182002-09-25 17:38:46 -0700310#ifdef _UNICOS
Tim Rice81ed5182002-09-25 17:38:46 -0700311 if (authenticated && cray_access_denied(authctxt->user)) {
312 authenticated = 0;
313 fatal("Access denied for user %s.",authctxt->user);
314 }
315#endif /* _UNICOS */
316
Kevin Stevesef4eea92001-02-05 12:42:17 +0000317#ifdef HAVE_CYGWIN
318 if (authenticated &&
Damien Miller94cf4c82005-07-17 17:04:47 +1000319 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,
Ben Lindstrome35bf122004-06-22 03:37:11 +0000320 authctxt->pw)) {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100321 packet_disconnect("Authentication rejected for uid %d.",
Ben Lindstrome35bf122004-06-22 03:37:11 +0000322 authctxt->pw == NULL ? -1 : authctxt->pw->pw_uid);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100323 authenticated = 0;
324 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000325#else
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000326 /* Special handling for root */
Damien Miller556f9312003-02-24 11:59:26 +1100327 if (authenticated && authctxt->pw->pw_uid == 0 &&
Damien Miller6abf57c2005-06-19 07:31:37 +1000328 !auth_root_allowed(meth->name)) {
329 authenticated = 0;
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100330# ifdef SSH_AUDIT_EVENTS
331 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100332# endif
333 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000334#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000335
Damien Miller1f499fd2003-08-25 13:08:49 +1000336#ifdef USE_PAM
Damien Millera8e06ce2003-11-21 23:48:55 +1100337 if (options.use_pam && authenticated &&
Darren Tuckerc1386672004-12-03 14:33:47 +1100338 !PRIVSEP(do_pam_account())) {
339 char *msg;
340 size_t len;
341
342 error("Access denied for user %s by PAM account "
Damien Millerb6f72f52005-07-17 17:26:43 +1000343 "configuration", authctxt->user);
Darren Tuckerc1386672004-12-03 14:33:47 +1100344 len = buffer_len(&loginmsg);
345 buffer_append(&loginmsg, "\0", 1);
346 msg = buffer_ptr(&loginmsg);
347 /* strip trailing newlines */
348 if (len > 0)
349 while (len > 0 && msg[--len] == '\n')
350 msg[len] = '\0';
351 else
352 msg = "Access denied.";
353 packet_disconnect(msg);
354 }
Damien Miller1f499fd2003-08-25 13:08:49 +1000355#endif
356
Damien Miller6abf57c2005-06-19 07:31:37 +1000357 skip:
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000358 /* Log before sending the reply */
359 auth_log(authctxt, authenticated, get_authname(type), info);
360
Damien Millereba71ba2000-04-29 23:57:08 +1000361 if (client_user != NULL) {
362 xfree(client_user);
363 client_user = NULL;
364 }
365
Damien Miller874d77b2000-10-14 16:23:11 +1100366 if (authenticated)
367 return;
368
Darren Tucker269a1ea2005-02-03 00:20:53 +1100369 if (authctxt->failures++ > options.max_authtries) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100370#ifdef SSH_AUDIT_EVENTS
371 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100372#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000373 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100374 }
Damien Millereba71ba2000-04-29 23:57:08 +1000375
Damien Millereba71ba2000-04-29 23:57:08 +1000376 packet_start(SSH_SMSG_FAILURE);
377 packet_send();
378 packet_write_wait();
379 }
380}
381
382/*
383 * Performs authentication of an incoming connection. Session key has already
384 * been exchanged and encryption is enabled.
385 */
Darren Tucker3e33cec2003-10-02 16:12:36 +1000386void
387do_authentication(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000388{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000389 u_int ulen;
Ben Lindstromc8226382002-04-10 16:22:09 +0000390 char *user, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000391
392 /* Get the name of the user that we wish to log in as. */
Damien Millerdff50992002-01-22 23:16:32 +1100393 packet_read_expect(SSH_CMSG_USER);
Damien Millereba71ba2000-04-29 23:57:08 +1000394
395 /* Get the user name. */
396 user = packet_get_string(&ulen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100397 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000398
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000399 if ((style = strchr(user, ':')) != NULL)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000400 *style++ = '\0';
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000401
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000402 authctxt->user = user;
403 authctxt->style = style;
404
Damien Millereba71ba2000-04-29 23:57:08 +1000405 /* Verify that the user is a valid user. */
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000406 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000407 authctxt->valid = 1;
Damien Miller856f0be2003-09-03 07:32:45 +1000408 else {
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000409 debug("do_authentication: invalid user %s", user);
Damien Miller856f0be2003-09-03 07:32:45 +1000410 authctxt->pw = fakepw();
411 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000412
Damien Miller30d1f842004-07-21 20:48:53 +1000413 setproctitle("%s%s", authctxt->valid ? user : "unknown",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000414 use_privsep ? " [net]" : "");
Ben Lindstromc1ba31f2001-02-15 03:14:11 +0000415
Damien Millereba71ba2000-04-29 23:57:08 +1000416#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000417 if (options.use_pam)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100418 PRIVSEP(start_pam(authctxt));
Damien Millereba71ba2000-04-29 23:57:08 +1000419#endif
420
421 /*
422 * If we are not running as root, the user must have the same uid as
Damien Miller6abf57c2005-06-19 07:31:37 +1000423 * the server.
Damien Millereba71ba2000-04-29 23:57:08 +1000424 */
Damien Miller874d77b2000-10-14 16:23:11 +1100425#ifndef HAVE_CYGWIN
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000426 if (!use_privsep && getuid() != 0 && authctxt->pw &&
427 authctxt->pw->pw_uid != getuid())
Damien Millereba71ba2000-04-29 23:57:08 +1000428 packet_disconnect("Cannot change user when server not running as root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100429#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000430
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000431 /*
Kevin Steves12aaa042001-01-24 21:23:39 +0000432 * Loop until the user has been authenticated or the connection is
433 * closed, do_authloop() returns only if authentication is successful
434 */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000435 do_authloop(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000436
437 /* The user has been authenticated and accepted. */
Damien Miller874d77b2000-10-14 16:23:11 +1100438 packet_start(SSH_SMSG_SUCCESS);
439 packet_send();
440 packet_write_wait();
Damien Millereba71ba2000-04-29 23:57:08 +1000441}