blob: b7dfa987e85ad90ca922364cc0efd771ca4154d0 [file] [log] [blame]
Damien Millereba71ba2000-04-29 23:57:08 +10001/*
2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11004 *
5 * As far as I am concerned, the code I have written for this software
6 * can be used freely for any purpose. Any derived versions of this
7 * software must be clearly marked as such, and if the derived work is
8 * incompatible with the protocol description in the RFC file, it must be
9 * called by a name other than "ssh" or "Secure Shell".
Damien Millereba71ba2000-04-29 23:57:08 +100010 */
11
12#include "includes.h"
Damien Miller6abf57c2005-06-19 07:31:37 +100013RCSID("$OpenBSD: auth1.c,v 1.60 2005/05/20 12:57:01 djm Exp $");
Damien Millereba71ba2000-04-29 23:57:08 +100014
Damien Miller874d77b2000-10-14 16:23:11 +110015#include "xmalloc.h"
16#include "rsa.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000017#include "ssh1.h"
Damien Miller874d77b2000-10-14 16:23:11 +110018#include "packet.h"
19#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000020#include "log.h"
Damien Miller874d77b2000-10-14 16:23:11 +110021#include "servconf.h"
22#include "compat.h"
23#include "auth.h"
Damien Millerc7ef63d2002-02-05 12:21:42 +110024#include "channels.h"
Damien Miller874d77b2000-10-14 16:23:11 +110025#include "session.h"
Ben Lindstromec95ed92001-07-04 04:21:14 +000026#include "uidswap.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000027#include "monitor_wrap.h"
Darren Tuckerc1386672004-12-03 14:33:47 +110028#include "buffer.h"
Damien Miller874d77b2000-10-14 16:23:11 +110029
Damien Millereba71ba2000-04-29 23:57:08 +100030/* import */
31extern ServerOptions options;
Darren Tuckerc1386672004-12-03 14:33:47 +110032extern Buffer loginmsg;
Damien Miller874d77b2000-10-14 16:23:11 +110033
Damien Miller6abf57c2005-06-19 07:31:37 +100034static int auth1_process_password(Authctxt *, char *, size_t);
35static int auth1_process_rsa(Authctxt *, char *, size_t);
36static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t);
37static int auth1_process_tis_challenge(Authctxt *, char *, size_t);
38static int auth1_process_tis_response(Authctxt *, char *, size_t);
39
40static char *client_user = NULL; /* Used to fill in remote user for PAM */
41
42struct AuthMethod1 {
43 int type;
44 char *name;
45 int *enabled;
46 int (*method)(Authctxt *, char *, size_t);
47};
48
49const struct AuthMethod1 auth1_methods[] = {
50 {
51 SSH_CMSG_AUTH_PASSWORD, "password",
52 &options.password_authentication, auth1_process_password
53 },
54 {
55 SSH_CMSG_AUTH_RSA, "rsa",
56 &options.rsa_authentication, auth1_process_rsa
57 },
58 {
59 SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
60 &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
61 },
62 {
63 SSH_CMSG_AUTH_TIS, "challenge-response",
64 &options.challenge_response_authentication,
65 auth1_process_tis_challenge
66 },
67 {
68 SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
69 &options.challenge_response_authentication,
70 auth1_process_tis_response
71 },
72 { -1, NULL, NULL, NULL}
73};
74
75static const struct AuthMethod1
76*lookup_authmethod1(int type)
77{
78 int i;
79
80 for(i = 0; auth1_methods[i].name != NULL; i++)
81 if (auth1_methods[i].type == type)
82 return (&(auth1_methods[i]));
83
84 return (NULL);
85}
86
Ben Lindstrombba81212001-06-25 05:01:22 +000087static char *
Damien Millereba71ba2000-04-29 23:57:08 +100088get_authname(int type)
89{
Damien Miller6abf57c2005-06-19 07:31:37 +100090 const struct AuthMethod1 *a;
91 static char buf[64];
92
93 if ((a = lookup_authmethod1(type)) != NULL)
94 return (a->name);
95 snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
96 return (buf);
97}
98
99static int
100auth1_process_password(Authctxt *authctxt, char *info, size_t infolen)
101{
102 int authenticated = 0;
103 char *password;
104 u_int dlen;
105
106 /*
107 * Read user password. It is in plain text, but was
108 * transmitted over the encrypted channel so it is
109 * not visible to an outside observer.
110 */
111 password = packet_get_string(&dlen);
112 packet_check_eom();
113
114 /* Try authentication with the password. */
115 authenticated = PRIVSEP(auth_password(authctxt, password));
116
117 memset(password, 0, dlen);
118 xfree(password);
119
120 return (authenticated);
121}
122
123static int
124auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen)
125{
126 int authenticated = 0;
127 BIGNUM *n;
128
129 /* RSA authentication requested. */
130 if ((n = BN_new()) == NULL)
131 fatal("do_authloop: BN_new failed");
132 packet_get_bignum(n);
133 packet_check_eom();
134 authenticated = auth_rsa(authctxt, n);
135 BN_clear_free(n);
136
137 return (authenticated);
138}
139
140static int
141auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen)
142{
143 int authenticated = 0;
144 u_int bits;
145 Key *client_host_key;
146 u_int ulen;
147
148 /*
149 * Get client user name. Note that we just have to
150 * trust the client; root on the client machine can
151 * claim to be any user.
152 */
153 client_user = packet_get_string(&ulen);
154
155 /* Get the client host key. */
156 client_host_key = key_new(KEY_RSA1);
157 bits = packet_get_int();
158 packet_get_bignum(client_host_key->rsa->e);
159 packet_get_bignum(client_host_key->rsa->n);
160
161 if (bits != BN_num_bits(client_host_key->rsa->n)) {
162 verbose("Warning: keysize mismatch for client_host_key: "
163 "actual %d, announced %d",
164 BN_num_bits(client_host_key->rsa->n), bits);
Damien Millereba71ba2000-04-29 23:57:08 +1000165 }
Damien Miller6abf57c2005-06-19 07:31:37 +1000166 packet_check_eom();
167
168 authenticated = auth_rhosts_rsa(authctxt, client_user,
169 client_host_key);
170 key_free(client_host_key);
171
172 snprintf(info, infolen, " ruser %.100s", client_user);
173
174 return (authenticated);
175}
176
177static int
178auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen)
179{
180 char *challenge;
181
182 if ((challenge = get_challenge(authctxt)) == NULL)
183 return (0);
184
185 debug("sending challenge '%s'", challenge);
186 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
187 packet_put_cstring(challenge);
188 xfree(challenge);
189 packet_send();
190 packet_write_wait();
191
192 return (-1);
193}
194
195static int
196auth1_process_tis_response(Authctxt *authctxt, char *info, size_t infolen)
197{
198 int authenticated = 0;
199 char *response;
200 u_int dlen;
201
202 response = packet_get_string(&dlen);
203 packet_check_eom();
204 authenticated = verify_response(authctxt, response);
205 memset(response, 'r', dlen);
206 xfree(response);
207
208 return (authenticated);
Damien Millereba71ba2000-04-29 23:57:08 +1000209}
210
211/*
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000212 * read packets, try to authenticate the user and
213 * return only if authentication is successful
Damien Millereba71ba2000-04-29 23:57:08 +1000214 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000215static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000216do_authloop(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000217{
Damien Miller874d77b2000-10-14 16:23:11 +1100218 int authenticated = 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000219 char info[1024];
Damien Miller6abf57c2005-06-19 07:31:37 +1000220 int prev = 0, type = 0;
221 const struct AuthMethod1 *meth;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000222
223 debug("Attempting authentication for %s%.100s.",
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000224 authctxt->valid ? "" : "invalid user ", authctxt->user);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000225
226 /* If the user has no password, accept authentication immediately. */
227 if (options.password_authentication &&
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000228#ifdef KRB5
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000229 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
230#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000231 PRIVSEP(auth_password(authctxt, ""))) {
Darren Tuckera8c73d32004-06-23 09:17:54 +1000232#ifdef USE_PAM
233 if (options.use_pam && (PRIVSEP(do_pam_account())))
234#endif
235 {
236 auth_log(authctxt, 1, "without authentication", "");
237 return;
238 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000239 }
Damien Millereba71ba2000-04-29 23:57:08 +1000240
241 /* Indicate that authentication is needed. */
242 packet_start(SSH_SMSG_FAILURE);
243 packet_send();
244 packet_write_wait();
245
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000246 for (;;) {
Damien Miller874d77b2000-10-14 16:23:11 +1100247 /* default to fail */
248 authenticated = 0;
249
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000250 info[0] = '\0';
Damien Millereba71ba2000-04-29 23:57:08 +1000251
252 /* Get a packet from the client. */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000253 prev = type;
Damien Millerdff50992002-01-22 23:16:32 +1100254 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000255
Damien Miller4f9f42a2003-05-10 19:28:02 +1000256 /*
257 * If we started challenge-response authentication but the
258 * next packet is not a response to our challenge, release
259 * the resources allocated by get_challenge() (which would
260 * normally have been released by verify_response() had we
261 * received such a response)
262 */
263 if (prev == SSH_CMSG_AUTH_TIS &&
264 type != SSH_CMSG_AUTH_TIS_RESPONSE)
265 abandon_challenge_response(authctxt);
266
Damien Miller6abf57c2005-06-19 07:31:37 +1000267 if ((meth = lookup_authmethod1(type)) == NULL) {
268 logit("Unknown message during authentication: "
269 "type %d", type);
270 goto skip;
Damien Millereba71ba2000-04-29 23:57:08 +1000271 }
Damien Miller6abf57c2005-06-19 07:31:37 +1000272
273 if (!*(meth->enabled)) {
274 verbose("%s authentication disabled.", meth->name);
275 goto skip;
276 }
277
278 authenticated = meth->method(authctxt, info, sizeof(info));
279 if (authenticated == -1)
280 continue; /* "postponed" */
281
Damien Miller60396b02001-02-18 17:01:00 +1100282#ifdef BSD_AUTH
283 if (authctxt->as) {
284 auth_close(authctxt->as);
285 authctxt->as = NULL;
286 }
287#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000288 if (!authctxt->valid && authenticated)
289 fatal("INTERNAL ERROR: authenticated invalid user %s",
290 authctxt->user);
Damien Millereba71ba2000-04-29 23:57:08 +1000291
Tim Rice81ed5182002-09-25 17:38:46 -0700292#ifdef _UNICOS
Tim Rice81ed5182002-09-25 17:38:46 -0700293 if (authenticated && cray_access_denied(authctxt->user)) {
294 authenticated = 0;
295 fatal("Access denied for user %s.",authctxt->user);
296 }
297#endif /* _UNICOS */
298
Kevin Stevesef4eea92001-02-05 12:42:17 +0000299#ifdef HAVE_CYGWIN
300 if (authenticated &&
Ben Lindstrome35bf122004-06-22 03:37:11 +0000301 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,
302 authctxt->pw)) {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100303 packet_disconnect("Authentication rejected for uid %d.",
Ben Lindstrome35bf122004-06-22 03:37:11 +0000304 authctxt->pw == NULL ? -1 : authctxt->pw->pw_uid);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100305 authenticated = 0;
306 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000307#else
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000308 /* Special handling for root */
Damien Miller556f9312003-02-24 11:59:26 +1100309 if (authenticated && authctxt->pw->pw_uid == 0 &&
Damien Miller6abf57c2005-06-19 07:31:37 +1000310 !auth_root_allowed(meth->name)) {
311 authenticated = 0;
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100312# ifdef SSH_AUDIT_EVENTS
313 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100314# endif
315 }
Ben Lindstrom5dc81502001-01-19 06:10:29 +0000316#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000317
Damien Miller1f499fd2003-08-25 13:08:49 +1000318#ifdef USE_PAM
Damien Millera8e06ce2003-11-21 23:48:55 +1100319 if (options.use_pam && authenticated &&
Darren Tuckerc1386672004-12-03 14:33:47 +1100320 !PRIVSEP(do_pam_account())) {
321 char *msg;
322 size_t len;
323
324 error("Access denied for user %s by PAM account "
325 "configuration", authctxt->user);
326 len = buffer_len(&loginmsg);
327 buffer_append(&loginmsg, "\0", 1);
328 msg = buffer_ptr(&loginmsg);
329 /* strip trailing newlines */
330 if (len > 0)
331 while (len > 0 && msg[--len] == '\n')
332 msg[len] = '\0';
333 else
334 msg = "Access denied.";
335 packet_disconnect(msg);
336 }
Damien Miller1f499fd2003-08-25 13:08:49 +1000337#endif
338
Damien Miller6abf57c2005-06-19 07:31:37 +1000339 skip:
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000340 /* Log before sending the reply */
341 auth_log(authctxt, authenticated, get_authname(type), info);
342
Damien Millereba71ba2000-04-29 23:57:08 +1000343 if (client_user != NULL) {
344 xfree(client_user);
345 client_user = NULL;
346 }
347
Damien Miller874d77b2000-10-14 16:23:11 +1100348 if (authenticated)
349 return;
350
Darren Tucker269a1ea2005-02-03 00:20:53 +1100351 if (authctxt->failures++ > options.max_authtries) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100352#ifdef SSH_AUDIT_EVENTS
353 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100354#endif
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000355 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100356 }
Damien Millereba71ba2000-04-29 23:57:08 +1000357
Damien Millereba71ba2000-04-29 23:57:08 +1000358 packet_start(SSH_SMSG_FAILURE);
359 packet_send();
360 packet_write_wait();
361 }
362}
363
364/*
365 * Performs authentication of an incoming connection. Session key has already
366 * been exchanged and encryption is enabled.
367 */
Darren Tucker3e33cec2003-10-02 16:12:36 +1000368void
369do_authentication(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000370{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000371 u_int ulen;
Ben Lindstromc8226382002-04-10 16:22:09 +0000372 char *user, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000373
374 /* Get the name of the user that we wish to log in as. */
Damien Millerdff50992002-01-22 23:16:32 +1100375 packet_read_expect(SSH_CMSG_USER);
Damien Millereba71ba2000-04-29 23:57:08 +1000376
377 /* Get the user name. */
378 user = packet_get_string(&ulen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100379 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000380
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000381 if ((style = strchr(user, ':')) != NULL)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000382 *style++ = '\0';
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000383
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000384 authctxt->user = user;
385 authctxt->style = style;
386
Damien Millereba71ba2000-04-29 23:57:08 +1000387 /* Verify that the user is a valid user. */
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000388 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000389 authctxt->valid = 1;
Damien Miller856f0be2003-09-03 07:32:45 +1000390 else {
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000391 debug("do_authentication: invalid user %s", user);
Damien Miller856f0be2003-09-03 07:32:45 +1000392 authctxt->pw = fakepw();
393 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000394
Damien Miller30d1f842004-07-21 20:48:53 +1000395 setproctitle("%s%s", authctxt->valid ? user : "unknown",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000396 use_privsep ? " [net]" : "");
Ben Lindstromc1ba31f2001-02-15 03:14:11 +0000397
Damien Millereba71ba2000-04-29 23:57:08 +1000398#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000399 if (options.use_pam)
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100400 PRIVSEP(start_pam(authctxt));
Damien Millereba71ba2000-04-29 23:57:08 +1000401#endif
402
403 /*
404 * If we are not running as root, the user must have the same uid as
Damien Miller6abf57c2005-06-19 07:31:37 +1000405 * the server.
Damien Millereba71ba2000-04-29 23:57:08 +1000406 */
Damien Miller874d77b2000-10-14 16:23:11 +1100407#ifndef HAVE_CYGWIN
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000408 if (!use_privsep && getuid() != 0 && authctxt->pw &&
409 authctxt->pw->pw_uid != getuid())
Damien Millereba71ba2000-04-29 23:57:08 +1000410 packet_disconnect("Cannot change user when server not running as root.");
Damien Millerbac2d8a2000-09-05 16:13:06 +1100411#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000412
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000413 /*
Kevin Steves12aaa042001-01-24 21:23:39 +0000414 * Loop until the user has been authenticated or the connection is
415 * closed, do_authloop() returns only if authentication is successful
416 */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000417 do_authloop(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000418
419 /* The user has been authenticated and accepted. */
Damien Miller874d77b2000-10-14 16:23:11 +1100420 packet_start(SSH_SMSG_SUCCESS);
421 packet_send();
422 packet_write_wait();
Damien Millereba71ba2000-04-29 23:57:08 +1000423}