blob: 0892918828c575d13edf08c3c53fa3fc28b05915 [file] [log] [blame]
Damien Miller1cdde6f2006-07-24 14:07:35 +10001/* $OpenBSD: auth1.c,v 1.67 2006/07/20 15:26:14 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
17#include <unistd.h>
18
Damien Miller874d77b2000-10-14 16:23:11 +110019#include "xmalloc.h"
20#include "rsa.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000021#include "ssh1.h"
Damien Miller874d77b2000-10-14 16:23:11 +110022#include "packet.h"
23#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000024#include "log.h"
Damien Miller874d77b2000-10-14 16:23:11 +110025#include "servconf.h"
26#include "compat.h"
27#include "auth.h"
Damien Millerc7ef63d2002-02-05 12:21:42 +110028#include "channels.h"
Damien Miller874d77b2000-10-14 16:23:11 +110029#include "session.h"
Ben Lindstromec95ed92001-07-04 04:21:14 +000030#include "uidswap.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000031#include "monitor_wrap.h"
Darren Tuckerc1386672004-12-03 14:33:47 +110032#include "buffer.h"
Damien Miller874d77b2000-10-14 16:23:11 +110033
Damien Millereba71ba2000-04-29 23:57:08 +100034/* import */
35extern ServerOptions options;
Darren Tuckerc1386672004-12-03 14:33:47 +110036extern Buffer loginmsg;
Damien Miller874d77b2000-10-14 16:23:11 +110037
Damien Miller6abf57c2005-06-19 07:31:37 +100038static int auth1_process_password(Authctxt *, char *, size_t);
39static int auth1_process_rsa(Authctxt *, char *, size_t);
40static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t);
41static int auth1_process_tis_challenge(Authctxt *, char *, size_t);
42static int auth1_process_tis_response(Authctxt *, char *, size_t);
43
44static char *client_user = NULL; /* Used to fill in remote user for PAM */
45
46struct AuthMethod1 {
47 int type;
48 char *name;
49 int *enabled;
50 int (*method)(Authctxt *, char *, size_t);
51};
52
53const struct AuthMethod1 auth1_methods[] = {
54 {
55 SSH_CMSG_AUTH_PASSWORD, "password",
56 &options.password_authentication, auth1_process_password
57 },
58 {
59 SSH_CMSG_AUTH_RSA, "rsa",
60 &options.rsa_authentication, auth1_process_rsa
61 },
62 {
63 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
64 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
65 },
66 {
67 SSH_CMSG_AUTH_TIS, "challenge-response",
68 &options.challenge_response_authentication,
69 auth1_process_tis_challenge
70 },
71 {
72 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
73 &options.challenge_response_authentication,
74 auth1_process_tis_response
75 },
76 { -1, NULL, NULL, NULL}
77};
78
79static const struct AuthMethod1
80*lookup_authmethod1(int type)
81{
82 int i;
83
Damien Millerd62f2ca2006-03-26 13:57:41 +110084 for (i = 0; auth1_methods[i].name != NULL; i++)
Damien Miller6abf57c2005-06-19 07:31:37 +100085 if (auth1_methods[i].type == type)
86 return (&(auth1_methods[i]));
87
88 return (NULL);
89}
90
Ben Lindstrombba81212001-06-25 05:01:22 +000091static char *
Damien Millereba71ba2000-04-29 23:57:08 +100092get_authname(int type)
93{
Damien Miller6abf57c2005-06-19 07:31:37 +100094 const struct AuthMethod1 *a;
95 static char buf[64];
96
97 if ((a = lookup_authmethod1(type)) != NULL)
98 return (a->name);
99 snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
100 return (buf);
101}
102
Damien Miller91d4b122006-03-26 14:05:20 +1100103/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000104static int
105auth1_process_password(Authctxt *authctxt, char *info, size_t infolen)
106{
107 int authenticated = 0;
108 char *password;
109 u_int dlen;
110
111 /*
112 * Read user password. It is in plain text, but was
113 * transmitted over the encrypted channel so it is
114 * not visible to an outside observer.
115 */
116 password = packet_get_string(&dlen);
117 packet_check_eom();
118
119 /* Try authentication with the password. */
120 authenticated = PRIVSEP(auth_password(authctxt, password));
121
122 memset(password, 0, dlen);
123 xfree(password);
124
125 return (authenticated);
126}
127
Damien Miller91d4b122006-03-26 14:05:20 +1100128/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000129static int
130auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen)
131{
132 int authenticated = 0;
133 BIGNUM *n;
134
135 /* RSA authentication requested. */
136 if ((n = BN_new()) == NULL)
137 fatal("do_authloop: BN_new failed");
138 packet_get_bignum(n);
139 packet_check_eom();
140 authenticated = auth_rsa(authctxt, n);
141 BN_clear_free(n);
142
143 return (authenticated);
144}
145
Damien Miller91d4b122006-03-26 14:05:20 +1100146/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000147static int
148auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen)
149{
Damien Miller06221f12005-06-19 07:36:10 +1000150 int keybits, authenticated = 0;
Damien Miller6abf57c2005-06-19 07:31:37 +1000151 u_int bits;
152 Key *client_host_key;
153 u_int ulen;
154
155 /*
156 * Get client user name. Note that we just have to
157 * trust the client; root on the client machine can
158 * claim to be any user.
159 */
160 client_user = packet_get_string(&ulen);
161
162 /* Get the client host key. */
163 client_host_key = key_new(KEY_RSA1);
164 bits = packet_get_int();
165 packet_get_bignum(client_host_key->rsa->e);
166 packet_get_bignum(client_host_key->rsa->n);
167
Damien Miller06221f12005-06-19 07:36:10 +1000168 keybits = BN_num_bits(client_host_key->rsa->n);
169 if (keybits < 0 || bits != (u_int)keybits) {
Damien Miller6abf57c2005-06-19 07:31:37 +1000170 verbose("Warning: keysize mismatch for client_host_key: "
171 "actual %d, announced %d",
172 BN_num_bits(client_host_key->rsa->n), bits);
Damien Millereba71ba2000-04-29 23:57:08 +1000173 }
Damien Miller6abf57c2005-06-19 07:31:37 +1000174 packet_check_eom();
175
176 authenticated = auth_rhosts_rsa(authctxt, client_user,
177 client_host_key);
178 key_free(client_host_key);
179
180 snprintf(info, infolen, " ruser %.100s", client_user);
Damien Miller94cf4c82005-07-17 17:04:47 +1000181
Damien Miller6abf57c2005-06-19 07:31:37 +1000182 return (authenticated);
183}
184
Damien Miller91d4b122006-03-26 14:05:20 +1100185/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000186static int
187auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen)
188{
189 char *challenge;
Damien Miller46d38de2005-07-17 17:02:09 +1000190
Damien Miller6abf57c2005-06-19 07:31:37 +1000191 if ((challenge = get_challenge(authctxt)) == NULL)
192 return (0);
193
194 debug("sending challenge '%s'", challenge);
195 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
196 packet_put_cstring(challenge);
197 xfree(challenge);
198 packet_send();
199 packet_write_wait();
200
201 return (-1);
202}
203
Damien Miller91d4b122006-03-26 14:05:20 +1100204/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000205static int
206auth1_process_tis_response(Authctxt *authctxt, char *info, size_t infolen)
207{
208 int authenticated = 0;
209 char *response;
210 u_int dlen;
211
212 response = packet_get_string(&dlen);
213 packet_check_eom();
214 authenticated = verify_response(authctxt, response);
215 memset(response, 'r', dlen);
216 xfree(response);
217
218 return (authenticated);
Damien Millereba71ba2000-04-29 23:57:08 +1000219}
220
221/*
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000222 * read packets, try to authenticate the user and
223 * return only if authentication is successful
Damien Millereba71ba2000-04-29 23:57:08 +1000224 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000225static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000226do_authloop(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000227{
Damien Miller874d77b2000-10-14 16:23:11 +1100228 int authenticated = 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000229 char info[1024];
Damien Miller6abf57c2005-06-19 07:31:37 +1000230 int prev = 0, type = 0;
231 const struct AuthMethod1 *meth;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000232
233 debug("Attempting authentication for %s%.100s.",
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000234 authctxt->valid ? "" : "invalid user ", authctxt->user);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000235
236 /* If the user has no password, accept authentication immediately. */
237 if (options.password_authentication &&
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000238#ifdef KRB5
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000239 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
240#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000241 PRIVSEP(auth_password(authctxt, ""))) {
Darren Tuckera8c73d32004-06-23 09:17:54 +1000242#ifdef USE_PAM
243 if (options.use_pam && (PRIVSEP(do_pam_account())))
244#endif
245 {
246 auth_log(authctxt, 1, "without authentication", "");
247 return;
248 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000249 }
Damien Millereba71ba2000-04-29 23:57:08 +1000250
251 /* Indicate that authentication is needed. */
252 packet_start(SSH_SMSG_FAILURE);
253 packet_send();
254 packet_write_wait();
255
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000256 for (;;) {
Damien Miller874d77b2000-10-14 16:23:11 +1100257 /* default to fail */
258 authenticated = 0;
259
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000260 info[0] = '\0';
Damien Millereba71ba2000-04-29 23:57:08 +1000261
262 /* Get a packet from the client. */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000263 prev = type;
Damien Millerdff50992002-01-22 23:16:32 +1100264 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000265
Damien Miller4f9f42a2003-05-10 19:28:02 +1000266 /*
267 * If we started challenge-response authentication but the
268 * next packet is not a response to our challenge, release
269 * the resources allocated by get_challenge() (which would
270 * normally have been released by verify_response() had we
271 * received such a response)
272 */
273 if (prev == SSH_CMSG_AUTH_TIS &&
274 type != SSH_CMSG_AUTH_TIS_RESPONSE)
275 abandon_challenge_response(authctxt);
276
Damien Miller6abf57c2005-06-19 07:31:37 +1000277 if ((meth = lookup_authmethod1(type)) == NULL) {
278 logit("Unknown message during authentication: "
279 "type %d", type);
280 goto skip;
Damien Millereba71ba2000-04-29 23:57:08 +1000281 }
Damien Miller6abf57c2005-06-19 07:31:37 +1000282
283 if (!*(meth->enabled)) {
284 verbose("%s authentication disabled.", meth->name);
285 goto skip;
286 }
287
288 authenticated = meth->method(authctxt, info, sizeof(info));
289 if (authenticated == -1)
290 continue; /* "postponed" */
291
Damien Miller60396b02001-02-18 17:01:00 +1100292#ifdef BSD_AUTH
293 if (authctxt->as) {
294 auth_close(authctxt->as);
295 authctxt->as = NULL;
296 }
297#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000298 if (!authctxt->valid && authenticated)
299 fatal("INTERNAL ERROR: authenticated invalid user %s",
300 authctxt->user);
Damien Millereba71ba2000-04-29 23:57:08 +1000301
Tim Rice81ed5182002-09-25 17:38:46 -0700302#ifdef _UNICOS
Tim Rice81ed5182002-09-25 17:38:46 -0700303 if (authenticated && cray_access_denied(authctxt->user)) {
304 authenticated = 0;
305 fatal("Access denied for user %s.",authctxt->user);
306 }
307#endif /* _UNICOS */
308
Kevin Stevesef4eea92001-02-05 12:42:17 +0000309#ifdef HAVE_CYGWIN
310 if (authenticated &&
Damien Miller94cf4c82005-07-17 17:04:47 +1000311 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,
Ben Lindstrome35bf122004-06-22 03:37:11 +0000312 authctxt->pw)) {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100313 packet_disconnect("Authentication rejected for uid %d.",
Ben Lindstrome35bf122004-06-22 03:37:11 +0000314 authctxt->pw == NULL ? -1 : authctxt->pw->pw_uid);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100315 authenticated = 0;
316 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000317#else
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000318 /* Special handling for root */
Damien Miller556f9312003-02-24 11:59:26 +1100319 if (authenticated && authctxt->pw->pw_uid == 0 &&
Damien Miller6abf57c2005-06-19 07:31:37 +1000320 !auth_root_allowed(meth->name)) {
321 authenticated = 0;
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100322# ifdef SSH_AUDIT_EVENTS
323 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100324# endif
325 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000326#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000327
Damien Miller1f499fd2003-08-25 13:08:49 +1000328#ifdef USE_PAM
Damien Millera8e06ce2003-11-21 23:48:55 +1100329 if (options.use_pam && authenticated &&
Darren Tuckerc1386672004-12-03 14:33:47 +1100330 !PRIVSEP(do_pam_account())) {
331 char *msg;
332 size_t len;
333
334 error("Access denied for user %s by PAM account "
Damien Millerb6f72f52005-07-17 17:26:43 +1000335 "configuration", authctxt->user);
Darren Tuckerc1386672004-12-03 14:33:47 +1100336 len = buffer_len(&loginmsg);
337 buffer_append(&loginmsg, "\0", 1);
338 msg = buffer_ptr(&loginmsg);
339 /* strip trailing newlines */
340 if (len > 0)
341 while (len > 0 && msg[--len] == '\n')
342 msg[len] = '\0';
343 else
344 msg = "Access denied.";
345 packet_disconnect(msg);
346 }
Damien Miller1f499fd2003-08-25 13:08:49 +1000347#endif
348
Damien Miller6abf57c2005-06-19 07:31:37 +1000349 skip:
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000350 /* Log before sending the reply */
351 auth_log(authctxt, authenticated, get_authname(type), info);
352
Damien Millereba71ba2000-04-29 23:57:08 +1000353 if (client_user != NULL) {
354 xfree(client_user);
355 client_user = NULL;
356 }
357
Damien Miller874d77b2000-10-14 16:23:11 +1100358 if (authenticated)
359 return;
360
Darren Tucker269a1ea2005-02-03 00:20:53 +1100361 if (authctxt->failures++ > options.max_authtries) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100362#ifdef SSH_AUDIT_EVENTS
363 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100364#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000365 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100366 }
Damien Millereba71ba2000-04-29 23:57:08 +1000367
Damien Millereba71ba2000-04-29 23:57:08 +1000368 packet_start(SSH_SMSG_FAILURE);
369 packet_send();
370 packet_write_wait();
371 }
372}
373
374/*
375 * Performs authentication of an incoming connection. Session key has already
376 * been exchanged and encryption is enabled.
377 */
Darren Tucker3e33cec2003-10-02 16:12:36 +1000378void
379do_authentication(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000380{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000381 u_int ulen;
Ben Lindstromc8226382002-04-10 16:22:09 +0000382 char *user, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000383
384 /* Get the name of the user that we wish to log in as. */
Damien Millerdff50992002-01-22 23:16:32 +1100385 packet_read_expect(SSH_CMSG_USER);
Damien Millereba71ba2000-04-29 23:57:08 +1000386
387 /* Get the user name. */
388 user = packet_get_string(&ulen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100389 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000390
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000391 if ((style = strchr(user, ':')) != NULL)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000392 *style++ = '\0';
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000393
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000394 authctxt->user = user;
395 authctxt->style = style;
396
Damien Millereba71ba2000-04-29 23:57:08 +1000397 /* Verify that the user is a valid user. */
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000398 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000399 authctxt->valid = 1;
Damien Miller856f0be2003-09-03 07:32:45 +1000400 else {
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000401 debug("do_authentication: invalid user %s", user);
Damien Miller856f0be2003-09-03 07:32:45 +1000402 authctxt->pw = fakepw();
403 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000404
Damien Miller30d1f842004-07-21 20:48:53 +1000405 setproctitle("%s%s", authctxt->valid ? user : "unknown",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000406 use_privsep ? " [net]" : "");
Ben Lindstromc1ba31f2001-02-15 03:14:11 +0000407
Damien Millereba71ba2000-04-29 23:57:08 +1000408#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000409 if (options.use_pam)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100410 PRIVSEP(start_pam(authctxt));
Damien Millereba71ba2000-04-29 23:57:08 +1000411#endif
412
413 /*
414 * If we are not running as root, the user must have the same uid as
Damien Miller6abf57c2005-06-19 07:31:37 +1000415 * the server.
Damien Millereba71ba2000-04-29 23:57:08 +1000416 */
Damien Miller874d77b2000-10-14 16:23:11 +1100417#ifndef HAVE_CYGWIN
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000418 if (!use_privsep && getuid() != 0 && authctxt->pw &&
419 authctxt->pw->pw_uid != getuid())
Damien Millereba71ba2000-04-29 23:57:08 +1000420 packet_disconnect("Cannot change user when server not running as root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100421#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000422
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000423 /*
Kevin Steves12aaa042001-01-24 21:23:39 +0000424 * Loop until the user has been authenticated or the connection is
425 * closed, do_authloop() returns only if authentication is successful
426 */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000427 do_authloop(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000428
429 /* The user has been authenticated and accepted. */
Damien Miller874d77b2000-10-14 16:23:11 +1100430 packet_start(SSH_SMSG_SUCCESS);
431 packet_send();
432 packet_write_wait();
Damien Millereba71ba2000-04-29 23:57:08 +1000433}