blob: 31f01f9fbecd38b93d3b0732456e2e42ca19288b [file] [log] [blame]
Darren Tucker4230a5d2008-07-02 22:56:09 +10001/* $OpenBSD: auth2.c,v 1.117 2008/07/02 12:36:39 djm Exp $ */
Damien Millereba71ba2000-04-29 23:57:08 +10002/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
Damien Millereba71ba2000-04-29 23:57:08 +100013 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
Damien Millere4340be2000-09-16 13:29:08 +110025
Damien Millereba71ba2000-04-29 23:57:08 +100026#include "includes.h"
Damien Millereba71ba2000-04-29 23:57:08 +100027
Damien Miller9f2abc42006-07-10 20:53:08 +100028#include <sys/types.h>
Darren Tucker4230a5d2008-07-02 22:56:09 +100029#include <sys/stat.h>
30#include <sys/uio.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100031
Darren Tucker4230a5d2008-07-02 22:56:09 +100032#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100033#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100034#include <stdarg.h>
Damien Millere3476ed2006-07-24 14:13:33 +100035#include <string.h>
Darren Tucker4230a5d2008-07-02 22:56:09 +100036#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100037
Damien Millereba71ba2000-04-29 23:57:08 +100038#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100039#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100040#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000041#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100042#include "buffer.h"
Damien Millereba71ba2000-04-29 23:57:08 +100043#include "servconf.h"
44#include "compat.h"
Damien Millerd7834352006-08-05 12:39:39 +100045#include "key.h"
46#include "hostfile.h"
Damien Millereba71ba2000-04-29 23:57:08 +100047#include "auth.h"
Damien Millereba71ba2000-04-29 23:57:08 +100048#include "dispatch.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000049#include "pathnames.h"
Darren Tucker77fc29e2004-09-11 23:07:03 +100050#include "buffer.h"
Damien Millereba71ba2000-04-29 23:57:08 +100051
Darren Tucker0efd1552003-08-26 11:49:55 +100052#ifdef GSSAPI
53#include "ssh-gss.h"
54#endif
Damien Millerd7834352006-08-05 12:39:39 +100055#include "monitor_wrap.h"
Darren Tucker0efd1552003-08-26 11:49:55 +100056
Damien Millereba71ba2000-04-29 23:57:08 +100057/* import */
58extern ServerOptions options;
Ben Lindstrom46c16222000-12-22 01:43:59 +000059extern u_char *session_id2;
Darren Tucker502d3842003-06-28 12:38:01 +100060extern u_int session_id2_len;
Darren Tucker77fc29e2004-09-11 23:07:03 +100061extern Buffer loginmsg;
Damien Millereba71ba2000-04-29 23:57:08 +100062
Ben Lindstrom511bb242002-06-06 20:52:37 +000063/* methods */
64
65extern Authmethod method_none;
66extern Authmethod method_pubkey;
67extern Authmethod method_passwd;
68extern Authmethod method_kbdint;
69extern Authmethod method_hostbased;
Darren Tucker0efd1552003-08-26 11:49:55 +100070#ifdef GSSAPI
71extern Authmethod method_gssapi;
72#endif
Ben Lindstrom511bb242002-06-06 20:52:37 +000073
74Authmethod *authmethods[] = {
75 &method_none,
76 &method_pubkey,
Darren Tucker0efd1552003-08-26 11:49:55 +100077#ifdef GSSAPI
78 &method_gssapi,
79#endif
Ben Lindstrom511bb242002-06-06 20:52:37 +000080 &method_passwd,
81 &method_kbdint,
82 &method_hostbased,
83 NULL
Damien Miller874d77b2000-10-14 16:23:11 +110084};
85
Damien Millereba71ba2000-04-29 23:57:08 +100086/* protocol */
87
Damien Miller630d6f42002-01-22 23:17:30 +110088static void input_service_request(int, u_int32_t, void *);
89static void input_userauth_request(int, u_int32_t, void *);
Damien Millereba71ba2000-04-29 23:57:08 +100090
Damien Millereba71ba2000-04-29 23:57:08 +100091/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +000092static Authmethod *authmethod_lookup(const char *);
Ben Lindstrom79073822001-07-04 03:42:30 +000093static char *authmethods_get(void);
Damien Millereba71ba2000-04-29 23:57:08 +100094
Darren Tucker4230a5d2008-07-02 22:56:09 +100095char *
96auth2_read_banner(void)
97{
98 struct stat st;
99 char *banner = NULL;
100 size_t len, n;
101 int fd;
102
103 if ((fd = open(options.banner, O_RDONLY)) == -1)
104 return (NULL);
105 if (fstat(fd, &st) == -1) {
106 close(fd);
107 return (NULL);
108 }
109 if (st.st_size > 1*1024*1024) {
110 close(fd);
111 return (NULL);
112 }
113
114 len = (size_t)st.st_size; /* truncate */
115 banner = xmalloc(len + 1);
116 n = atomicio(read, fd, banner, len);
117 close(fd);
118
119 if (n != len) {
120 xfree(banner);
121 return (NULL);
122 }
123 banner[n] = '\0';
124
125 return (banner);
126}
127
128void
129userauth_send_banner(const char *msg)
130{
131 if (datafellows & SSH_BUG_BANNER)
132 return;
133
134 packet_start(SSH2_MSG_USERAUTH_BANNER);
135 packet_put_cstring(msg);
136 packet_put_cstring(""); /* language, unused */
137 packet_send();
138 debug("%s: sent", __func__);
139}
140
141static void
142userauth_banner(void)
143{
144 char *banner = NULL;
145
146 if (options.banner == NULL ||
147 strcasecmp(options.banner, "none") == 0 ||
148 (datafellows & SSH_BUG_BANNER) != 0)
149 return;
150
151 if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
152 goto done;
153 userauth_send_banner(banner);
154
155done:
156 if (banner)
157 xfree(banner);
158}
159
Damien Millereba71ba2000-04-29 23:57:08 +1000160/*
Damien Miller874d77b2000-10-14 16:23:11 +1100161 * loop until authctxt->success == TRUE
Damien Millereba71ba2000-04-29 23:57:08 +1000162 */
Darren Tucker3e33cec2003-10-02 16:12:36 +1000163void
164do_authentication2(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000165{
Damien Miller7d053392002-01-22 23:24:13 +1100166 dispatch_init(&dispatch_protocol_error);
Damien Millereba71ba2000-04-29 23:57:08 +1000167 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
Damien Miller874d77b2000-10-14 16:23:11 +1100168 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000169}
170
Damien Miller91d4b122006-03-26 14:05:20 +1100171/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000172static void
Damien Miller630d6f42002-01-22 23:17:30 +1100173input_service_request(int type, u_int32_t seq, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000174{
Damien Miller874d77b2000-10-14 16:23:11 +1100175 Authctxt *authctxt = ctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000176 u_int len;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000177 int acceptit = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000178 char *service = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +1100179 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000180
Damien Miller874d77b2000-10-14 16:23:11 +1100181 if (authctxt == NULL)
182 fatal("input_service_request: no authctxt");
183
Damien Millereba71ba2000-04-29 23:57:08 +1000184 if (strcmp(service, "ssh-userauth") == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100185 if (!authctxt->success) {
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000186 acceptit = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000187 /* now we can handle user-auth requests */
188 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
189 }
190 }
191 /* XXX all other service requests are denied */
192
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000193 if (acceptit) {
Damien Millereba71ba2000-04-29 23:57:08 +1000194 packet_start(SSH2_MSG_SERVICE_ACCEPT);
195 packet_put_cstring(service);
196 packet_send();
197 packet_write_wait();
198 } else {
199 debug("bad service request %s", service);
200 packet_disconnect("bad service request %s", service);
201 }
202 xfree(service);
203}
204
Damien Miller91d4b122006-03-26 14:05:20 +1100205/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000206static void
Damien Miller630d6f42002-01-22 23:17:30 +1100207input_userauth_request(int type, u_int32_t seq, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000208{
Damien Miller874d77b2000-10-14 16:23:11 +1100209 Authctxt *authctxt = ctxt;
210 Authmethod *m = NULL;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000211 char *user, *service, *method, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000212 int authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000213
Damien Miller874d77b2000-10-14 16:23:11 +1100214 if (authctxt == NULL)
215 fatal("input_userauth_request: no authctxt");
Damien Millereba71ba2000-04-29 23:57:08 +1000216
Damien Miller874d77b2000-10-14 16:23:11 +1100217 user = packet_get_string(NULL);
218 service = packet_get_string(NULL);
219 method = packet_get_string(NULL);
220 debug("userauth-request for user %s service %s method %s", user, service, method);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000221 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
Damien Miller874d77b2000-10-14 16:23:11 +1100222
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000223 if ((style = strchr(user, ':')) != NULL)
224 *style++ = 0;
225
Kevin Stevesef4eea92001-02-05 12:42:17 +0000226 if (authctxt->attempt++ == 0) {
Damien Miller3a5b0232002-03-13 13:19:42 +1100227 /* setup auth context */
Kevin Steves205cc1e2002-03-22 20:43:05 +0000228 authctxt->pw = PRIVSEP(getpwnamallow(user));
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100229 authctxt->user = xstrdup(user);
Kevin Steves205cc1e2002-03-22 20:43:05 +0000230 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100231 authctxt->valid = 1;
232 debug2("input_userauth_request: setting up authctxt for %s", user);
Damien Miller874d77b2000-10-14 16:23:11 +1100233 } else {
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000234 logit("input_userauth_request: invalid user %s", user);
Damien Miller856f0be2003-09-03 07:32:45 +1000235 authctxt->pw = fakepw();
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100236#ifdef SSH_AUDIT_EVENTS
237 PRIVSEP(audit_event(SSH_INVALID_USER));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100238#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100239 }
Darren Tuckerd3eff2b2005-09-24 12:43:51 +1000240#ifdef USE_PAM
241 if (options.use_pam)
242 PRIVSEP(start_pam(authctxt));
243#endif
Damien Miller30d1f842004-07-21 20:48:53 +1000244 setproctitle("%s%s", authctxt->valid ? user : "unknown",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000245 use_privsep ? " [net]" : "");
Damien Miller874d77b2000-10-14 16:23:11 +1100246 authctxt->service = xstrdup(service);
Ben Lindstrom9d0c0662001-06-09 01:40:00 +0000247 authctxt->style = style ? xstrdup(style) : NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000248 if (use_privsep)
249 mm_inform_authserv(service, style);
Darren Tucker4230a5d2008-07-02 22:56:09 +1000250 userauth_banner();
Ben Lindstrom9d0c0662001-06-09 01:40:00 +0000251 } else if (strcmp(user, authctxt->user) != 0 ||
252 strcmp(service, authctxt->service) != 0) {
253 packet_disconnect("Change of username or service not allowed: "
254 "(%s,%s) -> (%s,%s)",
255 authctxt->user, authctxt->service, user, service);
Damien Millereba71ba2000-04-29 23:57:08 +1000256 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000257 /* reset state */
Damien Milleree116252001-12-21 12:42:34 +1100258 auth2_challenge_stop(authctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +1000259
260#ifdef GSSAPI
261 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
262 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
263#endif
264
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000265 authctxt->postponed = 0;
Damien Millerb70b61f2000-09-16 16:25:12 +1100266
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000267 /* try to authenticate user */
Damien Miller874d77b2000-10-14 16:23:11 +1100268 m = authmethod_lookup(method);
Darren Tucker4230a5d2008-07-02 22:56:09 +1000269 if (m != NULL && authctxt->failures < options.max_authtries) {
Damien Miller874d77b2000-10-14 16:23:11 +1100270 debug2("input_userauth_request: try method %s", method);
271 authenticated = m->userauth(authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100272 }
Damien Miller5d57e502001-03-30 10:48:31 +1000273 userauth_finish(authctxt, authenticated, method);
274
275 xfree(service);
276 xfree(user);
277 xfree(method);
278}
279
280void
281userauth_finish(Authctxt *authctxt, int authenticated, char *method)
282{
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000283 char *methods;
284
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000285 if (!authctxt->valid && authenticated)
286 fatal("INTERNAL ERROR: authenticated invalid user %s",
287 authctxt->user);
Damien Millerb70b61f2000-09-16 16:25:12 +1100288
Damien Miller874d77b2000-10-14 16:23:11 +1100289 /* Special handling for root */
Damien Miller556f9312003-02-24 11:59:26 +1100290 if (authenticated && authctxt->pw->pw_uid == 0 &&
Darren Tucker269a1ea2005-02-03 00:20:53 +1100291 !auth_root_allowed(method)) {
Damien Millereba71ba2000-04-29 23:57:08 +1000292 authenticated = 0;
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100293#ifdef SSH_AUDIT_EVENTS
294 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100295#endif
296 }
Damien Millereba71ba2000-04-29 23:57:08 +1000297
Damien Miller1f499fd2003-08-25 13:08:49 +1000298#ifdef USE_PAM
Darren Tucker77fc29e2004-09-11 23:07:03 +1000299 if (options.use_pam && authenticated) {
300 if (!PRIVSEP(do_pam_account())) {
Darren Tucker77fc29e2004-09-11 23:07:03 +1000301 /* if PAM returned a message, send it to the user */
302 if (buffer_len(&loginmsg) > 0) {
303 buffer_append(&loginmsg, "\0", 1);
304 userauth_send_banner(buffer_ptr(&loginmsg));
Darren Tuckerc1386672004-12-03 14:33:47 +1100305 packet_write_wait();
Darren Tucker77fc29e2004-09-11 23:07:03 +1000306 }
Darren Tuckerc1386672004-12-03 14:33:47 +1100307 fatal("Access denied for user %s by PAM account "
Damien Millerb6f72f52005-07-17 17:26:43 +1000308 "configuration", authctxt->user);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000309 }
310 }
Damien Miller1f499fd2003-08-25 13:08:49 +1000311#endif
312
Tim Rice81ed5182002-09-25 17:38:46 -0700313#ifdef _UNICOS
314 if (authenticated && cray_access_denied(authctxt->user)) {
315 authenticated = 0;
316 fatal("Access denied for user %s.",authctxt->user);
317 }
318#endif /* _UNICOS */
319
Damien Miller874d77b2000-10-14 16:23:11 +1100320 /* Log before sending the reply */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000321 auth_log(authctxt, authenticated, method, " ssh2");
322
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000323 if (authctxt->postponed)
324 return;
325
326 /* XXX todo: check if multiple auth methods are needed */
327 if (authenticated == 1) {
328 /* turn off userauth */
Damien Miller7d053392002-01-22 23:24:13 +1100329 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000330 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
331 packet_send();
332 packet_write_wait();
333 /* now we can break out */
334 authctxt->success = 1;
335 } else {
Darren Tucker4230a5d2008-07-02 22:56:09 +1000336 if (++authctxt->failures > options.max_authtries) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100337#ifdef SSH_AUDIT_EVENTS
338 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100339#endif
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000340 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100341 }
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000342 methods = authmethods_get();
343 packet_start(SSH2_MSG_USERAUTH_FAILURE);
344 packet_put_cstring(methods);
345 packet_put_char(0); /* XXX partial success, unused */
346 packet_send();
347 packet_write_wait();
348 xfree(methods);
349 }
Damien Miller874d77b2000-10-14 16:23:11 +1100350}
351
Ben Lindstrom79073822001-07-04 03:42:30 +0000352static char *
Damien Miller874d77b2000-10-14 16:23:11 +1100353authmethods_get(void)
354{
Damien Miller0e3b8722002-01-22 23:26:38 +1100355 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100356 char *list;
Ben Lindstrom511bb242002-06-06 20:52:37 +0000357 int i;
Damien Miller874d77b2000-10-14 16:23:11 +1100358
Damien Miller0e3b8722002-01-22 23:26:38 +1100359 buffer_init(&b);
Ben Lindstrom511bb242002-06-06 20:52:37 +0000360 for (i = 0; authmethods[i] != NULL; i++) {
361 if (strcmp(authmethods[i]->name, "none") == 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100362 continue;
Ben Lindstrom511bb242002-06-06 20:52:37 +0000363 if (authmethods[i]->enabled != NULL &&
364 *(authmethods[i]->enabled) != 0) {
Damien Miller0e3b8722002-01-22 23:26:38 +1100365 if (buffer_len(&b) > 0)
366 buffer_append(&b, ",", 1);
Ben Lindstrom511bb242002-06-06 20:52:37 +0000367 buffer_append(&b, authmethods[i]->name,
368 strlen(authmethods[i]->name));
Damien Millereba71ba2000-04-29 23:57:08 +1000369 }
370 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100371 buffer_append(&b, "\0", 1);
372 list = xstrdup(buffer_ptr(&b));
373 buffer_free(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100374 return list;
375}
376
Ben Lindstrombba81212001-06-25 05:01:22 +0000377static Authmethod *
Damien Miller874d77b2000-10-14 16:23:11 +1100378authmethod_lookup(const char *name)
379{
Ben Lindstrom511bb242002-06-06 20:52:37 +0000380 int i;
381
Damien Miller874d77b2000-10-14 16:23:11 +1100382 if (name != NULL)
Ben Lindstrom511bb242002-06-06 20:52:37 +0000383 for (i = 0; authmethods[i] != NULL; i++)
384 if (authmethods[i]->enabled != NULL &&
385 *(authmethods[i]->enabled) != 0 &&
386 strcmp(name, authmethods[i]->name) == 0)
387 return authmethods[i];
388 debug2("Unrecognized authentication method name: %s",
389 name ? name : "NULL");
Damien Miller874d77b2000-10-14 16:23:11 +1100390 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000391}
Darren Tucker4230a5d2008-07-02 22:56:09 +1000392