blob: 2c5585768884852f8987dc35d82700353125ae7f [file] [log] [blame]
Damien Millera7a73ee2006-08-05 11:37:59 +10001/* $OpenBSD: auth1.c,v 1.69 2006/08/01 23:22:47 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
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>
20
Damien Miller874d77b2000-10-14 16:23:11 +110021#include "xmalloc.h"
22#include "rsa.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000023#include "ssh1.h"
Damien Miller874d77b2000-10-14 16:23:11 +110024#include "packet.h"
25#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000026#include "log.h"
Damien Miller874d77b2000-10-14 16:23:11 +110027#include "servconf.h"
28#include "compat.h"
29#include "auth.h"
Damien Millerc7ef63d2002-02-05 12:21:42 +110030#include "channels.h"
Damien Miller874d77b2000-10-14 16:23:11 +110031#include "session.h"
Ben Lindstromec95ed92001-07-04 04:21:14 +000032#include "uidswap.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000033#include "monitor_wrap.h"
Darren Tuckerc1386672004-12-03 14:33:47 +110034#include "buffer.h"
Damien Miller874d77b2000-10-14 16:23:11 +110035
Damien Millereba71ba2000-04-29 23:57:08 +100036/* import */
37extern ServerOptions options;
Darren Tuckerc1386672004-12-03 14:33:47 +110038extern Buffer loginmsg;
Damien Miller874d77b2000-10-14 16:23:11 +110039
Damien Miller6abf57c2005-06-19 07:31:37 +100040static int auth1_process_password(Authctxt *, char *, size_t);
41static int auth1_process_rsa(Authctxt *, char *, size_t);
42static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t);
43static int auth1_process_tis_challenge(Authctxt *, char *, size_t);
44static int auth1_process_tis_response(Authctxt *, char *, size_t);
45
46static char *client_user = NULL; /* Used to fill in remote user for PAM */
47
48struct AuthMethod1 {
49 int type;
50 char *name;
51 int *enabled;
52 int (*method)(Authctxt *, char *, size_t);
53};
54
55const struct AuthMethod1 auth1_methods[] = {
56 {
57 SSH_CMSG_AUTH_PASSWORD, "password",
58 &options.password_authentication, auth1_process_password
59 },
60 {
61 SSH_CMSG_AUTH_RSA, "rsa",
62 &options.rsa_authentication, auth1_process_rsa
63 },
64 {
65 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
66 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
67 },
68 {
69 SSH_CMSG_AUTH_TIS, "challenge-response",
70 &options.challenge_response_authentication,
71 auth1_process_tis_challenge
72 },
73 {
74 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
75 &options.challenge_response_authentication,
76 auth1_process_tis_response
77 },
78 { -1, NULL, NULL, NULL}
79};
80
81static const struct AuthMethod1
82*lookup_authmethod1(int type)
83{
84 int i;
85
Damien Millerd62f2ca2006-03-26 13:57:41 +110086 for (i = 0; auth1_methods[i].name != NULL; i++)
Damien Miller6abf57c2005-06-19 07:31:37 +100087 if (auth1_methods[i].type == type)
88 return (&(auth1_methods[i]));
89
90 return (NULL);
91}
92
Ben Lindstrombba81212001-06-25 05:01:22 +000093static char *
Damien Millereba71ba2000-04-29 23:57:08 +100094get_authname(int type)
95{
Damien Miller6abf57c2005-06-19 07:31:37 +100096 const struct AuthMethod1 *a;
97 static char buf[64];
98
99 if ((a = lookup_authmethod1(type)) != NULL)
100 return (a->name);
101 snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
102 return (buf);
103}
104
Damien Miller91d4b122006-03-26 14:05:20 +1100105/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000106static int
107auth1_process_password(Authctxt *authctxt, char *info, size_t infolen)
108{
109 int authenticated = 0;
110 char *password;
111 u_int dlen;
112
113 /*
114 * Read user password. It is in plain text, but was
115 * transmitted over the encrypted channel so it is
116 * not visible to an outside observer.
117 */
118 password = packet_get_string(&dlen);
119 packet_check_eom();
120
121 /* Try authentication with the password. */
122 authenticated = PRIVSEP(auth_password(authctxt, password));
123
124 memset(password, 0, dlen);
125 xfree(password);
126
127 return (authenticated);
128}
129
Damien Miller91d4b122006-03-26 14:05:20 +1100130/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000131static int
132auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen)
133{
134 int authenticated = 0;
135 BIGNUM *n;
136
137 /* RSA authentication requested. */
138 if ((n = BN_new()) == NULL)
139 fatal("do_authloop: BN_new failed");
140 packet_get_bignum(n);
141 packet_check_eom();
142 authenticated = auth_rsa(authctxt, n);
143 BN_clear_free(n);
144
145 return (authenticated);
146}
147
Damien Miller91d4b122006-03-26 14:05:20 +1100148/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000149static int
150auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen)
151{
Damien Miller06221f12005-06-19 07:36:10 +1000152 int keybits, authenticated = 0;
Damien Miller6abf57c2005-06-19 07:31:37 +1000153 u_int bits;
154 Key *client_host_key;
155 u_int ulen;
156
157 /*
158 * Get client user name. Note that we just have to
159 * trust the client; root on the client machine can
160 * claim to be any user.
161 */
162 client_user = packet_get_string(&ulen);
163
164 /* Get the client host key. */
165 client_host_key = key_new(KEY_RSA1);
166 bits = packet_get_int();
167 packet_get_bignum(client_host_key->rsa->e);
168 packet_get_bignum(client_host_key->rsa->n);
169
Damien Miller06221f12005-06-19 07:36:10 +1000170 keybits = BN_num_bits(client_host_key->rsa->n);
171 if (keybits < 0 || bits != (u_int)keybits) {
Damien Miller6abf57c2005-06-19 07:31:37 +1000172 verbose("Warning: keysize mismatch for client_host_key: "
173 "actual %d, announced %d",
174 BN_num_bits(client_host_key->rsa->n), bits);
Damien Millereba71ba2000-04-29 23:57:08 +1000175 }
Damien Miller6abf57c2005-06-19 07:31:37 +1000176 packet_check_eom();
177
178 authenticated = auth_rhosts_rsa(authctxt, client_user,
179 client_host_key);
180 key_free(client_host_key);
181
182 snprintf(info, infolen, " ruser %.100s", client_user);
Damien Miller94cf4c82005-07-17 17:04:47 +1000183
Damien Miller6abf57c2005-06-19 07:31:37 +1000184 return (authenticated);
185}
186
Damien Miller91d4b122006-03-26 14:05:20 +1100187/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000188static int
189auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen)
190{
191 char *challenge;
Damien Miller46d38de2005-07-17 17:02:09 +1000192
Damien Miller6abf57c2005-06-19 07:31:37 +1000193 if ((challenge = get_challenge(authctxt)) == NULL)
194 return (0);
195
196 debug("sending challenge '%s'", challenge);
197 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
198 packet_put_cstring(challenge);
199 xfree(challenge);
200 packet_send();
201 packet_write_wait();
202
203 return (-1);
204}
205
Damien Miller91d4b122006-03-26 14:05:20 +1100206/*ARGSUSED*/
Damien Miller6abf57c2005-06-19 07:31:37 +1000207static int
208auth1_process_tis_response(Authctxt *authctxt, char *info, size_t infolen)
209{
210 int authenticated = 0;
211 char *response;
212 u_int dlen;
213
214 response = packet_get_string(&dlen);
215 packet_check_eom();
216 authenticated = verify_response(authctxt, response);
217 memset(response, 'r', dlen);
218 xfree(response);
219
220 return (authenticated);
Damien Millereba71ba2000-04-29 23:57:08 +1000221}
222
223/*
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000224 * read packets, try to authenticate the user and
225 * return only if authentication is successful
Damien Millereba71ba2000-04-29 23:57:08 +1000226 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000227static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000228do_authloop(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000229{
Damien Miller874d77b2000-10-14 16:23:11 +1100230 int authenticated = 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000231 char info[1024];
Damien Miller6abf57c2005-06-19 07:31:37 +1000232 int prev = 0, type = 0;
233 const struct AuthMethod1 *meth;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000234
235 debug("Attempting authentication for %s%.100s.",
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000236 authctxt->valid ? "" : "invalid user ", authctxt->user);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000237
238 /* If the user has no password, accept authentication immediately. */
239 if (options.password_authentication &&
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000240#ifdef KRB5
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000241 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
242#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000243 PRIVSEP(auth_password(authctxt, ""))) {
Darren Tuckera8c73d32004-06-23 09:17:54 +1000244#ifdef USE_PAM
245 if (options.use_pam && (PRIVSEP(do_pam_account())))
246#endif
247 {
248 auth_log(authctxt, 1, "without authentication", "");
249 return;
250 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000251 }
Damien Millereba71ba2000-04-29 23:57:08 +1000252
253 /* Indicate that authentication is needed. */
254 packet_start(SSH_SMSG_FAILURE);
255 packet_send();
256 packet_write_wait();
257
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000258 for (;;) {
Damien Miller874d77b2000-10-14 16:23:11 +1100259 /* default to fail */
260 authenticated = 0;
261
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000262 info[0] = '\0';
Damien Millereba71ba2000-04-29 23:57:08 +1000263
264 /* Get a packet from the client. */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000265 prev = type;
Damien Millerdff50992002-01-22 23:16:32 +1100266 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000267
Damien Miller4f9f42a2003-05-10 19:28:02 +1000268 /*
269 * If we started challenge-response authentication but the
270 * next packet is not a response to our challenge, release
271 * the resources allocated by get_challenge() (which would
272 * normally have been released by verify_response() had we
273 * received such a response)
274 */
275 if (prev == SSH_CMSG_AUTH_TIS &&
276 type != SSH_CMSG_AUTH_TIS_RESPONSE)
277 abandon_challenge_response(authctxt);
278
Damien Miller6abf57c2005-06-19 07:31:37 +1000279 if ((meth = lookup_authmethod1(type)) == NULL) {
280 logit("Unknown message during authentication: "
281 "type %d", type);
282 goto skip;
Damien Millereba71ba2000-04-29 23:57:08 +1000283 }
Damien Miller6abf57c2005-06-19 07:31:37 +1000284
285 if (!*(meth->enabled)) {
286 verbose("%s authentication disabled.", meth->name);
287 goto skip;
288 }
289
290 authenticated = meth->method(authctxt, info, sizeof(info));
291 if (authenticated == -1)
292 continue; /* "postponed" */
293
Damien Miller60396b02001-02-18 17:01:00 +1100294#ifdef BSD_AUTH
295 if (authctxt->as) {
296 auth_close(authctxt->as);
297 authctxt->as = NULL;
298 }
299#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000300 if (!authctxt->valid && authenticated)
301 fatal("INTERNAL ERROR: authenticated invalid user %s",
302 authctxt->user);
Damien Millereba71ba2000-04-29 23:57:08 +1000303
Tim Rice81ed5182002-09-25 17:38:46 -0700304#ifdef _UNICOS
Tim Rice81ed5182002-09-25 17:38:46 -0700305 if (authenticated && cray_access_denied(authctxt->user)) {
306 authenticated = 0;
307 fatal("Access denied for user %s.",authctxt->user);
308 }
309#endif /* _UNICOS */
310
Kevin Stevesef4eea92001-02-05 12:42:17 +0000311#ifdef HAVE_CYGWIN
312 if (authenticated &&
Damien Miller94cf4c82005-07-17 17:04:47 +1000313 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,
Ben Lindstrome35bf122004-06-22 03:37:11 +0000314 authctxt->pw)) {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100315 packet_disconnect("Authentication rejected for uid %d.",
Ben Lindstrome35bf122004-06-22 03:37:11 +0000316 authctxt->pw == NULL ? -1 : authctxt->pw->pw_uid);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100317 authenticated = 0;
318 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000319#else
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000320 /* Special handling for root */
Damien Miller556f9312003-02-24 11:59:26 +1100321 if (authenticated && authctxt->pw->pw_uid == 0 &&
Damien Miller6abf57c2005-06-19 07:31:37 +1000322 !auth_root_allowed(meth->name)) {
323 authenticated = 0;
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100324# ifdef SSH_AUDIT_EVENTS
325 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100326# endif
327 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000328#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000329
Damien Miller1f499fd2003-08-25 13:08:49 +1000330#ifdef USE_PAM
Damien Millera8e06ce2003-11-21 23:48:55 +1100331 if (options.use_pam && authenticated &&
Darren Tuckerc1386672004-12-03 14:33:47 +1100332 !PRIVSEP(do_pam_account())) {
333 char *msg;
334 size_t len;
335
336 error("Access denied for user %s by PAM account "
Damien Millerb6f72f52005-07-17 17:26:43 +1000337 "configuration", authctxt->user);
Darren Tuckerc1386672004-12-03 14:33:47 +1100338 len = buffer_len(&loginmsg);
339 buffer_append(&loginmsg, "\0", 1);
340 msg = buffer_ptr(&loginmsg);
341 /* strip trailing newlines */
342 if (len > 0)
343 while (len > 0 && msg[--len] == '\n')
344 msg[len] = '\0';
345 else
346 msg = "Access denied.";
347 packet_disconnect(msg);
348 }
Damien Miller1f499fd2003-08-25 13:08:49 +1000349#endif
350
Damien Miller6abf57c2005-06-19 07:31:37 +1000351 skip:
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000352 /* Log before sending the reply */
353 auth_log(authctxt, authenticated, get_authname(type), info);
354
Damien Millereba71ba2000-04-29 23:57:08 +1000355 if (client_user != NULL) {
356 xfree(client_user);
357 client_user = NULL;
358 }
359
Damien Miller874d77b2000-10-14 16:23:11 +1100360 if (authenticated)
361 return;
362
Darren Tucker269a1ea2005-02-03 00:20:53 +1100363 if (authctxt->failures++ > options.max_authtries) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100364#ifdef SSH_AUDIT_EVENTS
365 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100366#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000367 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100368 }
Damien Millereba71ba2000-04-29 23:57:08 +1000369
Damien Millereba71ba2000-04-29 23:57:08 +1000370 packet_start(SSH_SMSG_FAILURE);
371 packet_send();
372 packet_write_wait();
373 }
374}
375
376/*
377 * Performs authentication of an incoming connection. Session key has already
378 * been exchanged and encryption is enabled.
379 */
Darren Tucker3e33cec2003-10-02 16:12:36 +1000380void
381do_authentication(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000382{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000383 u_int ulen;
Ben Lindstromc8226382002-04-10 16:22:09 +0000384 char *user, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000385
386 /* Get the name of the user that we wish to log in as. */
Damien Millerdff50992002-01-22 23:16:32 +1100387 packet_read_expect(SSH_CMSG_USER);
Damien Millereba71ba2000-04-29 23:57:08 +1000388
389 /* Get the user name. */
390 user = packet_get_string(&ulen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100391 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000392
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000393 if ((style = strchr(user, ':')) != NULL)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000394 *style++ = '\0';
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000395
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000396 authctxt->user = user;
397 authctxt->style = style;
398
Damien Millereba71ba2000-04-29 23:57:08 +1000399 /* Verify that the user is a valid user. */
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000400 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000401 authctxt->valid = 1;
Damien Miller856f0be2003-09-03 07:32:45 +1000402 else {
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000403 debug("do_authentication: invalid user %s", user);
Damien Miller856f0be2003-09-03 07:32:45 +1000404 authctxt->pw = fakepw();
405 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000406
Damien Miller30d1f842004-07-21 20:48:53 +1000407 setproctitle("%s%s", authctxt->valid ? user : "unknown",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000408 use_privsep ? " [net]" : "");
Ben Lindstromc1ba31f2001-02-15 03:14:11 +0000409
Damien Millereba71ba2000-04-29 23:57:08 +1000410#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000411 if (options.use_pam)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100412 PRIVSEP(start_pam(authctxt));
Damien Millereba71ba2000-04-29 23:57:08 +1000413#endif
414
415 /*
416 * If we are not running as root, the user must have the same uid as
Damien Miller6abf57c2005-06-19 07:31:37 +1000417 * the server.
Damien Millereba71ba2000-04-29 23:57:08 +1000418 */
Damien Miller874d77b2000-10-14 16:23:11 +1100419#ifndef HAVE_CYGWIN
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000420 if (!use_privsep && getuid() != 0 && authctxt->pw &&
421 authctxt->pw->pw_uid != getuid())
Damien Millereba71ba2000-04-29 23:57:08 +1000422 packet_disconnect("Cannot change user when server not running as root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100423#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000424
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000425 /*
Kevin Steves12aaa042001-01-24 21:23:39 +0000426 * Loop until the user has been authenticated or the connection is
427 * closed, do_authloop() returns only if authentication is successful
428 */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000429 do_authloop(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000430
431 /* The user has been authenticated and accepted. */
Damien Miller874d77b2000-10-14 16:23:11 +1100432 packet_start(SSH_SMSG_SUCCESS);
433 packet_send();
434 packet_write_wait();
Damien Millereba71ba2000-04-29 23:57:08 +1000435}