blob: 2d880b57cf41e6b5b5922906d6e7928d0c5d58fe [file] [log] [blame]
Damien Millerd7834352006-08-05 12:39:39 +10001/* $OpenBSD: auth2.c,v 1.113 2006/08/03 03:34:41 deraadt 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>
29
30#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100031#include <stdarg.h>
Damien Millere3476ed2006-07-24 14:13:33 +100032#include <string.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100033
Damien Millereba71ba2000-04-29 23:57:08 +100034#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100035#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100036#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000037#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100038#include "buffer.h"
Damien Millereba71ba2000-04-29 23:57:08 +100039#include "servconf.h"
40#include "compat.h"
Damien Millerd7834352006-08-05 12:39:39 +100041#include "key.h"
42#include "hostfile.h"
Damien Millereba71ba2000-04-29 23:57:08 +100043#include "auth.h"
Damien Millereba71ba2000-04-29 23:57:08 +100044#include "dispatch.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000045#include "pathnames.h"
Darren Tucker77fc29e2004-09-11 23:07:03 +100046#include "buffer.h"
Damien Millereba71ba2000-04-29 23:57:08 +100047
Darren Tucker0efd1552003-08-26 11:49:55 +100048#ifdef GSSAPI
49#include "ssh-gss.h"
50#endif
Damien Millerd7834352006-08-05 12:39:39 +100051#include "monitor_wrap.h"
Darren Tucker0efd1552003-08-26 11:49:55 +100052
Damien Millereba71ba2000-04-29 23:57:08 +100053/* import */
54extern ServerOptions options;
Ben Lindstrom46c16222000-12-22 01:43:59 +000055extern u_char *session_id2;
Darren Tucker502d3842003-06-28 12:38:01 +100056extern u_int session_id2_len;
Darren Tucker77fc29e2004-09-11 23:07:03 +100057extern Buffer loginmsg;
Damien Millereba71ba2000-04-29 23:57:08 +100058
Ben Lindstrom511bb242002-06-06 20:52:37 +000059/* methods */
60
61extern Authmethod method_none;
62extern Authmethod method_pubkey;
63extern Authmethod method_passwd;
64extern Authmethod method_kbdint;
65extern Authmethod method_hostbased;
Darren Tucker0efd1552003-08-26 11:49:55 +100066#ifdef GSSAPI
67extern Authmethod method_gssapi;
68#endif
Ben Lindstrom511bb242002-06-06 20:52:37 +000069
70Authmethod *authmethods[] = {
71 &method_none,
72 &method_pubkey,
Darren Tucker0efd1552003-08-26 11:49:55 +100073#ifdef GSSAPI
74 &method_gssapi,
75#endif
Ben Lindstrom511bb242002-06-06 20:52:37 +000076 &method_passwd,
77 &method_kbdint,
78 &method_hostbased,
79 NULL
Damien Miller874d77b2000-10-14 16:23:11 +110080};
81
Damien Millereba71ba2000-04-29 23:57:08 +100082/* protocol */
83
Damien Miller630d6f42002-01-22 23:17:30 +110084static void input_service_request(int, u_int32_t, void *);
85static void input_userauth_request(int, u_int32_t, void *);
Damien Millereba71ba2000-04-29 23:57:08 +100086
Damien Millereba71ba2000-04-29 23:57:08 +100087/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +000088static Authmethod *authmethod_lookup(const char *);
Ben Lindstrom79073822001-07-04 03:42:30 +000089static char *authmethods_get(void);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000090int user_key_allowed(struct passwd *, Key *);
Damien Millereba71ba2000-04-29 23:57:08 +100091
Damien Millereba71ba2000-04-29 23:57:08 +100092/*
Damien Miller874d77b2000-10-14 16:23:11 +110093 * loop until authctxt->success == TRUE
Damien Millereba71ba2000-04-29 23:57:08 +100094 */
95
Darren Tucker3e33cec2003-10-02 16:12:36 +100096void
97do_authentication2(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +100098{
Ben Lindstrombdfb4df2001-10-03 17:12:43 +000099 /* challenge-response is implemented via keyboard interactive */
Ben Lindstrom551ea372001-06-05 18:56:16 +0000100 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000101 options.kbd_interactive_authentication = 1;
102
Damien Miller7d053392002-01-22 23:24:13 +1100103 dispatch_init(&dispatch_protocol_error);
Damien Millereba71ba2000-04-29 23:57:08 +1000104 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
Damien Miller874d77b2000-10-14 16:23:11 +1100105 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000106}
107
Damien Miller91d4b122006-03-26 14:05:20 +1100108/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000109static void
Damien Miller630d6f42002-01-22 23:17:30 +1100110input_service_request(int type, u_int32_t seq, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000111{
Damien Miller874d77b2000-10-14 16:23:11 +1100112 Authctxt *authctxt = ctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000113 u_int len;
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000114 int acceptit = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000115 char *service = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +1100116 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000117
Damien Miller874d77b2000-10-14 16:23:11 +1100118 if (authctxt == NULL)
119 fatal("input_service_request: no authctxt");
120
Damien Millereba71ba2000-04-29 23:57:08 +1000121 if (strcmp(service, "ssh-userauth") == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100122 if (!authctxt->success) {
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000123 acceptit = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000124 /* now we can handle user-auth requests */
125 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
126 }
127 }
128 /* XXX all other service requests are denied */
129
Ben Lindstrom5a9d0ea2002-07-04 00:12:53 +0000130 if (acceptit) {
Damien Millereba71ba2000-04-29 23:57:08 +1000131 packet_start(SSH2_MSG_SERVICE_ACCEPT);
132 packet_put_cstring(service);
133 packet_send();
134 packet_write_wait();
135 } else {
136 debug("bad service request %s", service);
137 packet_disconnect("bad service request %s", service);
138 }
139 xfree(service);
140}
141
Damien Miller91d4b122006-03-26 14:05:20 +1100142/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000143static void
Damien Miller630d6f42002-01-22 23:17:30 +1100144input_userauth_request(int type, u_int32_t seq, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000145{
Damien Miller874d77b2000-10-14 16:23:11 +1100146 Authctxt *authctxt = ctxt;
147 Authmethod *m = NULL;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000148 char *user, *service, *method, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000149 int authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000150
Damien Miller874d77b2000-10-14 16:23:11 +1100151 if (authctxt == NULL)
152 fatal("input_userauth_request: no authctxt");
Damien Millereba71ba2000-04-29 23:57:08 +1000153
Damien Miller874d77b2000-10-14 16:23:11 +1100154 user = packet_get_string(NULL);
155 service = packet_get_string(NULL);
156 method = packet_get_string(NULL);
157 debug("userauth-request for user %s service %s method %s", user, service, method);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000158 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
Damien Miller874d77b2000-10-14 16:23:11 +1100159
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000160 if ((style = strchr(user, ':')) != NULL)
161 *style++ = 0;
162
Kevin Stevesef4eea92001-02-05 12:42:17 +0000163 if (authctxt->attempt++ == 0) {
Damien Miller3a5b0232002-03-13 13:19:42 +1100164 /* setup auth context */
Kevin Steves205cc1e2002-03-22 20:43:05 +0000165 authctxt->pw = PRIVSEP(getpwnamallow(user));
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100166 authctxt->user = xstrdup(user);
Kevin Steves205cc1e2002-03-22 20:43:05 +0000167 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100168 authctxt->valid = 1;
169 debug2("input_userauth_request: setting up authctxt for %s", user);
Damien Miller874d77b2000-10-14 16:23:11 +1100170 } else {
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000171 logit("input_userauth_request: invalid user %s", user);
Damien Miller856f0be2003-09-03 07:32:45 +1000172 authctxt->pw = fakepw();
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100173#ifdef SSH_AUDIT_EVENTS
174 PRIVSEP(audit_event(SSH_INVALID_USER));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100175#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100176 }
Darren Tuckerd3eff2b2005-09-24 12:43:51 +1000177#ifdef USE_PAM
178 if (options.use_pam)
179 PRIVSEP(start_pam(authctxt));
180#endif
Damien Miller30d1f842004-07-21 20:48:53 +1000181 setproctitle("%s%s", authctxt->valid ? user : "unknown",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000182 use_privsep ? " [net]" : "");
Damien Miller874d77b2000-10-14 16:23:11 +1100183 authctxt->service = xstrdup(service);
Ben Lindstrom9d0c0662001-06-09 01:40:00 +0000184 authctxt->style = style ? xstrdup(style) : NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000185 if (use_privsep)
186 mm_inform_authserv(service, style);
Ben Lindstrom9d0c0662001-06-09 01:40:00 +0000187 } else if (strcmp(user, authctxt->user) != 0 ||
188 strcmp(service, authctxt->service) != 0) {
189 packet_disconnect("Change of username or service not allowed: "
190 "(%s,%s) -> (%s,%s)",
191 authctxt->user, authctxt->service, user, service);
Damien Millereba71ba2000-04-29 23:57:08 +1000192 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000193 /* reset state */
Damien Milleree116252001-12-21 12:42:34 +1100194 auth2_challenge_stop(authctxt);
Darren Tucker0efd1552003-08-26 11:49:55 +1000195
196#ifdef GSSAPI
197 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
198 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
199#endif
200
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000201 authctxt->postponed = 0;
Damien Millerb70b61f2000-09-16 16:25:12 +1100202
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000203 /* try to authenticate user */
Damien Miller874d77b2000-10-14 16:23:11 +1100204 m = authmethod_lookup(method);
205 if (m != NULL) {
206 debug2("input_userauth_request: try method %s", method);
207 authenticated = m->userauth(authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100208 }
Damien Miller5d57e502001-03-30 10:48:31 +1000209 userauth_finish(authctxt, authenticated, method);
210
211 xfree(service);
212 xfree(user);
213 xfree(method);
214}
215
216void
217userauth_finish(Authctxt *authctxt, int authenticated, char *method)
218{
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000219 char *methods;
220
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000221 if (!authctxt->valid && authenticated)
222 fatal("INTERNAL ERROR: authenticated invalid user %s",
223 authctxt->user);
Damien Millerb70b61f2000-09-16 16:25:12 +1100224
Damien Miller874d77b2000-10-14 16:23:11 +1100225 /* Special handling for root */
Damien Miller556f9312003-02-24 11:59:26 +1100226 if (authenticated && authctxt->pw->pw_uid == 0 &&
Darren Tucker269a1ea2005-02-03 00:20:53 +1100227 !auth_root_allowed(method)) {
Damien Millereba71ba2000-04-29 23:57:08 +1000228 authenticated = 0;
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100229#ifdef SSH_AUDIT_EVENTS
230 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100231#endif
232 }
Damien Millereba71ba2000-04-29 23:57:08 +1000233
Damien Miller1f499fd2003-08-25 13:08:49 +1000234#ifdef USE_PAM
Darren Tucker77fc29e2004-09-11 23:07:03 +1000235 if (options.use_pam && authenticated) {
236 if (!PRIVSEP(do_pam_account())) {
Darren Tucker77fc29e2004-09-11 23:07:03 +1000237 /* if PAM returned a message, send it to the user */
238 if (buffer_len(&loginmsg) > 0) {
239 buffer_append(&loginmsg, "\0", 1);
240 userauth_send_banner(buffer_ptr(&loginmsg));
Darren Tuckerc1386672004-12-03 14:33:47 +1100241 packet_write_wait();
Darren Tucker77fc29e2004-09-11 23:07:03 +1000242 }
Darren Tuckerc1386672004-12-03 14:33:47 +1100243 fatal("Access denied for user %s by PAM account "
Damien Millerb6f72f52005-07-17 17:26:43 +1000244 "configuration", authctxt->user);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000245 }
246 }
Damien Miller1f499fd2003-08-25 13:08:49 +1000247#endif
248
Tim Rice81ed5182002-09-25 17:38:46 -0700249#ifdef _UNICOS
250 if (authenticated && cray_access_denied(authctxt->user)) {
251 authenticated = 0;
252 fatal("Access denied for user %s.",authctxt->user);
253 }
254#endif /* _UNICOS */
255
Damien Miller874d77b2000-10-14 16:23:11 +1100256 /* Log before sending the reply */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000257 auth_log(authctxt, authenticated, method, " ssh2");
258
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000259 if (authctxt->postponed)
260 return;
261
262 /* XXX todo: check if multiple auth methods are needed */
263 if (authenticated == 1) {
264 /* turn off userauth */
Damien Miller7d053392002-01-22 23:24:13 +1100265 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000266 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
267 packet_send();
268 packet_write_wait();
269 /* now we can break out */
270 authctxt->success = 1;
271 } else {
Darren Tucker269a1ea2005-02-03 00:20:53 +1100272 if (authctxt->failures++ > options.max_authtries) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100273#ifdef SSH_AUDIT_EVENTS
274 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100275#endif
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000276 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100277 }
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000278 methods = authmethods_get();
279 packet_start(SSH2_MSG_USERAUTH_FAILURE);
280 packet_put_cstring(methods);
281 packet_put_char(0); /* XXX partial success, unused */
282 packet_send();
283 packet_write_wait();
284 xfree(methods);
285 }
Damien Miller874d77b2000-10-14 16:23:11 +1100286}
287
Damien Miller874d77b2000-10-14 16:23:11 +1100288#define DELIM ","
Damien Millereba71ba2000-04-29 23:57:08 +1000289
Ben Lindstrom79073822001-07-04 03:42:30 +0000290static char *
Damien Miller874d77b2000-10-14 16:23:11 +1100291authmethods_get(void)
292{
Damien Miller0e3b8722002-01-22 23:26:38 +1100293 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100294 char *list;
Ben Lindstrom511bb242002-06-06 20:52:37 +0000295 int i;
Damien Miller874d77b2000-10-14 16:23:11 +1100296
Damien Miller0e3b8722002-01-22 23:26:38 +1100297 buffer_init(&b);
Ben Lindstrom511bb242002-06-06 20:52:37 +0000298 for (i = 0; authmethods[i] != NULL; i++) {
299 if (strcmp(authmethods[i]->name, "none") == 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100300 continue;
Ben Lindstrom511bb242002-06-06 20:52:37 +0000301 if (authmethods[i]->enabled != NULL &&
302 *(authmethods[i]->enabled) != 0) {
Damien Miller0e3b8722002-01-22 23:26:38 +1100303 if (buffer_len(&b) > 0)
304 buffer_append(&b, ",", 1);
Ben Lindstrom511bb242002-06-06 20:52:37 +0000305 buffer_append(&b, authmethods[i]->name,
306 strlen(authmethods[i]->name));
Damien Millereba71ba2000-04-29 23:57:08 +1000307 }
308 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100309 buffer_append(&b, "\0", 1);
310 list = xstrdup(buffer_ptr(&b));
311 buffer_free(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100312 return list;
313}
314
Ben Lindstrombba81212001-06-25 05:01:22 +0000315static Authmethod *
Damien Miller874d77b2000-10-14 16:23:11 +1100316authmethod_lookup(const char *name)
317{
Ben Lindstrom511bb242002-06-06 20:52:37 +0000318 int i;
319
Damien Miller874d77b2000-10-14 16:23:11 +1100320 if (name != NULL)
Ben Lindstrom511bb242002-06-06 20:52:37 +0000321 for (i = 0; authmethods[i] != NULL; i++)
322 if (authmethods[i]->enabled != NULL &&
323 *(authmethods[i]->enabled) != 0 &&
324 strcmp(name, authmethods[i]->name) == 0)
325 return authmethods[i];
326 debug2("Unrecognized authentication method name: %s",
327 name ? name : "NULL");
Damien Miller874d77b2000-10-14 16:23:11 +1100328 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000329}