blob: 22b5586967d561dbb5305a13969ca837d3b288d3 [file] [log] [blame]
Damien Millereba71ba2000-04-29 23:57:08 +10001/*
2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
Damien Millereba71ba2000-04-29 23:57:08 +100012 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
Damien Millere4340be2000-09-16 13:29:08 +110024
Damien Millereba71ba2000-04-29 23:57:08 +100025#include "includes.h"
Damien Miller9b74bfc2002-02-05 12:26:03 +110026RCSID("$OpenBSD: auth2.c,v 1.84 2002/02/04 11:58:10 markus Exp $");
Damien Miller874d77b2000-10-14 16:23:11 +110027
Damien Millereba71ba2000-04-29 23:57:08 +100028#include <openssl/evp.h>
29
Ben Lindstrom226cfa02001-01-22 05:34:40 +000030#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100031#include "xmalloc.h"
32#include "rsa.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000033#include "sshpty.h"
Damien Millereba71ba2000-04-29 23:57:08 +100034#include "packet.h"
35#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000036#include "log.h"
Damien Millereba71ba2000-04-29 23:57:08 +100037#include "servconf.h"
38#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000039#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100040#include "bufaux.h"
Damien Millereba71ba2000-04-29 23:57:08 +100041#include "auth.h"
42#include "session.h"
43#include "dispatch.h"
Damien Millereba71ba2000-04-29 23:57:08 +100044#include "key.h"
Ben Lindstrom6d40c0f2001-01-29 09:02:24 +000045#include "cipher.h"
Damien Millereba71ba2000-04-29 23:57:08 +100046#include "kex.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000047#include "pathnames.h"
Damien Millereba71ba2000-04-29 23:57:08 +100048#include "uidswap.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100049#include "auth-options.h"
Ben Lindstrom086cf212001-03-05 05:56:40 +000050#include "misc.h"
Ben Lindstrom5eabda32001-04-12 23:34:34 +000051#include "hostfile.h"
52#include "canohost.h"
Ben Lindstrom551ea372001-06-05 18:56:16 +000053#include "match.h"
Damien Millereba71ba2000-04-29 23:57:08 +100054
55/* import */
56extern ServerOptions options;
Ben Lindstrom46c16222000-12-22 01:43:59 +000057extern u_char *session_id2;
Damien Millereba71ba2000-04-29 23:57:08 +100058extern int session_id2_len;
59
Damien Miller874d77b2000-10-14 16:23:11 +110060static Authctxt *x_authctxt = NULL;
61static int one = 1;
62
63typedef struct Authmethod Authmethod;
64struct Authmethod {
65 char *name;
66 int (*userauth)(Authctxt *authctxt);
67 int *enabled;
68};
69
Damien Millereba71ba2000-04-29 23:57:08 +100070/* protocol */
71
Damien Miller630d6f42002-01-22 23:17:30 +110072static void input_service_request(int, u_int32_t, void *);
73static void input_userauth_request(int, u_int32_t, void *);
Damien Millereba71ba2000-04-29 23:57:08 +100074
Damien Millereba71ba2000-04-29 23:57:08 +100075/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +000076static Authmethod *authmethod_lookup(const char *);
Ben Lindstrom79073822001-07-04 03:42:30 +000077static char *authmethods_get(void);
Ben Lindstrombba81212001-06-25 05:01:22 +000078static int user_key_allowed(struct passwd *, Key *);
79static int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
Damien Millereba71ba2000-04-29 23:57:08 +100080
Damien Miller874d77b2000-10-14 16:23:11 +110081/* auth */
Ben Lindstrombba81212001-06-25 05:01:22 +000082static void userauth_banner(void);
83static int userauth_none(Authctxt *);
84static int userauth_passwd(Authctxt *);
85static int userauth_pubkey(Authctxt *);
86static int userauth_hostbased(Authctxt *);
87static int userauth_kbdint(Authctxt *);
Damien Miller874d77b2000-10-14 16:23:11 +110088
89Authmethod authmethods[] = {
90 {"none",
91 userauth_none,
92 &one},
93 {"publickey",
94 userauth_pubkey,
Damien Miller0bc1bd82000-11-13 22:57:25 +110095 &options.pubkey_authentication},
Damien Miller874d77b2000-10-14 16:23:11 +110096 {"password",
97 userauth_passwd,
98 &options.password_authentication},
Ben Lindstromd1f20ec2001-02-10 21:31:53 +000099 {"keyboard-interactive",
100 userauth_kbdint,
101 &options.kbd_interactive_authentication},
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000102 {"hostbased",
103 userauth_hostbased,
104 &options.hostbased_authentication},
Damien Miller874d77b2000-10-14 16:23:11 +1100105 {NULL, NULL, NULL}
Damien Millereba71ba2000-04-29 23:57:08 +1000106};
Damien Millereba71ba2000-04-29 23:57:08 +1000107
108/*
Damien Miller874d77b2000-10-14 16:23:11 +1100109 * loop until authctxt->success == TRUE
Damien Millereba71ba2000-04-29 23:57:08 +1000110 */
111
112void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000113do_authentication2(void)
Damien Millereba71ba2000-04-29 23:57:08 +1000114{
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000115 Authctxt *authctxt = authctxt_new();
116
Damien Miller874d77b2000-10-14 16:23:11 +1100117 x_authctxt = authctxt; /*XXX*/
118
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000119 /* challenge-response is implemented via keyboard interactive */
Ben Lindstrom551ea372001-06-05 18:56:16 +0000120 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000121 options.kbd_interactive_authentication = 1;
Damien Millerf8154422001-04-25 22:44:14 +1000122 if (options.pam_authentication_via_kbd_int)
123 options.kbd_interactive_authentication = 1;
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000124
Damien Miller7d053392002-01-22 23:24:13 +1100125 dispatch_init(&dispatch_protocol_error);
Damien Millereba71ba2000-04-29 23:57:08 +1000126 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
Damien Miller874d77b2000-10-14 16:23:11 +1100127 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000128 do_authenticated(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000129}
130
Ben Lindstrombba81212001-06-25 05:01:22 +0000131static void
Damien Miller630d6f42002-01-22 23:17:30 +1100132input_service_request(int type, u_int32_t seq, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000133{
Damien Miller874d77b2000-10-14 16:23:11 +1100134 Authctxt *authctxt = ctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000135 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000136 int accept = 0;
137 char *service = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +1100138 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000139
Damien Miller874d77b2000-10-14 16:23:11 +1100140 if (authctxt == NULL)
141 fatal("input_service_request: no authctxt");
142
Damien Millereba71ba2000-04-29 23:57:08 +1000143 if (strcmp(service, "ssh-userauth") == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100144 if (!authctxt->success) {
Damien Millereba71ba2000-04-29 23:57:08 +1000145 accept = 1;
146 /* now we can handle user-auth requests */
147 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
148 }
149 }
150 /* XXX all other service requests are denied */
151
152 if (accept) {
153 packet_start(SSH2_MSG_SERVICE_ACCEPT);
154 packet_put_cstring(service);
155 packet_send();
156 packet_write_wait();
157 } else {
158 debug("bad service request %s", service);
159 packet_disconnect("bad service request %s", service);
160 }
161 xfree(service);
162}
163
Ben Lindstrombba81212001-06-25 05:01:22 +0000164static void
Damien Miller630d6f42002-01-22 23:17:30 +1100165input_userauth_request(int type, u_int32_t seq, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000166{
Damien Miller874d77b2000-10-14 16:23:11 +1100167 Authctxt *authctxt = ctxt;
168 Authmethod *m = NULL;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000169 char *user, *service, *method, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000170 int authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000171
Damien Miller874d77b2000-10-14 16:23:11 +1100172 if (authctxt == NULL)
173 fatal("input_userauth_request: no authctxt");
Damien Millereba71ba2000-04-29 23:57:08 +1000174
Damien Miller874d77b2000-10-14 16:23:11 +1100175 user = packet_get_string(NULL);
176 service = packet_get_string(NULL);
177 method = packet_get_string(NULL);
178 debug("userauth-request for user %s service %s method %s", user, service, method);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000179 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
Damien Miller874d77b2000-10-14 16:23:11 +1100180
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000181 if ((style = strchr(user, ':')) != NULL)
182 *style++ = 0;
183
Kevin Stevesef4eea92001-02-05 12:42:17 +0000184 if (authctxt->attempt++ == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100185 /* setup auth context */
186 struct passwd *pw = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100187 pw = getpwnam(user);
188 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
189 authctxt->pw = pwcopy(pw);
190 authctxt->valid = 1;
191 debug2("input_userauth_request: setting up authctxt for %s", user);
192#ifdef USE_PAM
Damien Miller22e22bf2001-01-19 15:46:38 +1100193 start_pam(pw->pw_name);
Damien Miller874d77b2000-10-14 16:23:11 +1100194#endif
195 } else {
196 log("input_userauth_request: illegal user %s", user);
Damien Miller22e22bf2001-01-19 15:46:38 +1100197#ifdef USE_PAM
198 start_pam("NOUSER");
199#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100200 }
Ben Lindstromc1ba31f2001-02-15 03:14:11 +0000201 setproctitle("%s", pw ? user : "unknown");
Damien Miller874d77b2000-10-14 16:23:11 +1100202 authctxt->user = xstrdup(user);
203 authctxt->service = xstrdup(service);
Ben Lindstrom9d0c0662001-06-09 01:40:00 +0000204 authctxt->style = style ? xstrdup(style) : NULL;
205 } else if (strcmp(user, authctxt->user) != 0 ||
206 strcmp(service, authctxt->service) != 0) {
207 packet_disconnect("Change of username or service not allowed: "
208 "(%s,%s) -> (%s,%s)",
209 authctxt->user, authctxt->service, user, service);
Damien Millereba71ba2000-04-29 23:57:08 +1000210 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000211 /* reset state */
Damien Milleree116252001-12-21 12:42:34 +1100212 auth2_challenge_stop(authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000213 authctxt->postponed = 0;
Damien Millerb70b61f2000-09-16 16:25:12 +1100214
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000215 /* try to authenticate user */
Damien Miller874d77b2000-10-14 16:23:11 +1100216 m = authmethod_lookup(method);
217 if (m != NULL) {
218 debug2("input_userauth_request: try method %s", method);
219 authenticated = m->userauth(authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100220 }
Damien Miller5d57e502001-03-30 10:48:31 +1000221 userauth_finish(authctxt, authenticated, method);
222
223 xfree(service);
224 xfree(user);
225 xfree(method);
226}
227
228void
229userauth_finish(Authctxt *authctxt, int authenticated, char *method)
230{
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000231 char *methods;
232
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000233 if (!authctxt->valid && authenticated)
234 fatal("INTERNAL ERROR: authenticated invalid user %s",
235 authctxt->user);
Damien Millerb70b61f2000-09-16 16:25:12 +1100236
Damien Miller874d77b2000-10-14 16:23:11 +1100237 /* Special handling for root */
Ben Lindstromd8a90212001-02-15 03:08:27 +0000238 if (authenticated && authctxt->pw->pw_uid == 0 &&
239 !auth_root_allowed(method))
Damien Millereba71ba2000-04-29 23:57:08 +1000240 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000241
242#ifdef USE_PAM
Damien Millerc83de6d2001-02-16 14:17:59 +1100243 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
244 NULL))
Damien Millerb70b61f2000-09-16 16:25:12 +1100245 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000246#endif /* USE_PAM */
247
Damien Miller874d77b2000-10-14 16:23:11 +1100248 /* Log before sending the reply */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000249 auth_log(authctxt, authenticated, method, " ssh2");
250
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000251 if (authctxt->postponed)
252 return;
253
254 /* XXX todo: check if multiple auth methods are needed */
255 if (authenticated == 1) {
256 /* turn off userauth */
Damien Miller7d053392002-01-22 23:24:13 +1100257 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000258 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
259 packet_send();
260 packet_write_wait();
261 /* now we can break out */
262 authctxt->success = 1;
263 } else {
Damien Millere49d0962001-11-13 23:46:18 +1100264 if (authctxt->failures++ > AUTH_FAIL_MAX) {
265#ifdef WITH_AIXAUTHENTICATE
266 loginfailed(authctxt->user,
Damien Millerf3451a22002-02-05 12:40:46 +1100267 get_canonical_hostname(options.verify_reverse_mapping),
Damien Millere49d0962001-11-13 23:46:18 +1100268 "ssh");
269#endif /* WITH_AIXAUTHENTICATE */
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000270 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Damien Millere49d0962001-11-13 23:46:18 +1100271 }
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000272 methods = authmethods_get();
273 packet_start(SSH2_MSG_USERAUTH_FAILURE);
274 packet_put_cstring(methods);
275 packet_put_char(0); /* XXX partial success, unused */
276 packet_send();
277 packet_write_wait();
278 xfree(methods);
279 }
Damien Miller874d77b2000-10-14 16:23:11 +1100280}
281
Ben Lindstrombba81212001-06-25 05:01:22 +0000282static void
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000283userauth_banner(void)
284{
285 struct stat st;
286 char *banner = NULL;
287 off_t len, n;
288 int fd;
289
290 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
291 return;
Ben Lindstrom0cae0402001-04-04 23:47:52 +0000292 if ((fd = open(options.banner, O_RDONLY)) < 0)
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000293 return;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000294 if (fstat(fd, &st) < 0)
295 goto done;
296 len = st.st_size;
297 banner = xmalloc(len + 1);
298 if ((n = read(fd, banner, len)) < 0)
299 goto done;
300 banner[n] = '\0';
301 packet_start(SSH2_MSG_USERAUTH_BANNER);
302 packet_put_cstring(banner);
303 packet_put_cstring(""); /* language, unused */
304 packet_send();
305 debug("userauth_banner: sent");
306done:
307 if (banner)
308 xfree(banner);
309 close(fd);
310 return;
311}
Damien Miller874d77b2000-10-14 16:23:11 +1100312
Ben Lindstrombba81212001-06-25 05:01:22 +0000313static int
Damien Miller874d77b2000-10-14 16:23:11 +1100314userauth_none(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000315{
Damien Miller874d77b2000-10-14 16:23:11 +1100316 /* disable method "none", only allowed one time */
317 Authmethod *m = authmethod_lookup("none");
318 if (m != NULL)
319 m->enabled = NULL;
Damien Miller48b03fc2002-01-22 23:11:40 +1100320 packet_check_eom();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000321 userauth_banner();
Damien Millerb8c656e2000-06-28 15:22:41 +1000322
Damien Miller874d77b2000-10-14 16:23:11 +1100323 if (authctxt->valid == 0)
324 return(0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000325
Damien Miller874d77b2000-10-14 16:23:11 +1100326#ifdef HAVE_CYGWIN
Damien Miller0dea79d2001-12-29 14:08:28 +1100327 if (check_nt_auth(1, authctxt->pw) == 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100328 return(0);
329#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000330#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100331 return auth_pam_password(authctxt->pw, "");
Damien Millerb8c656e2000-06-28 15:22:41 +1000332#elif defined(HAVE_OSF_SIA)
Damien Miller92ddb7d2001-02-14 01:25:23 +1100333 return 0;
Damien Millerb8c656e2000-06-28 15:22:41 +1000334#else /* !HAVE_OSF_SIA && !USE_PAM */
Damien Miller60396b02001-02-18 17:01:00 +1100335 return auth_password(authctxt, "");
Damien Millereba71ba2000-04-29 23:57:08 +1000336#endif /* USE_PAM */
337}
Damien Miller874d77b2000-10-14 16:23:11 +1100338
Ben Lindstrombba81212001-06-25 05:01:22 +0000339static int
Damien Miller874d77b2000-10-14 16:23:11 +1100340userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000341{
342 char *password;
343 int authenticated = 0;
344 int change;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000345 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000346 change = packet_get_char();
347 if (change)
348 log("password change not supported");
349 password = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +1100350 packet_check_eom();
Damien Miller874d77b2000-10-14 16:23:11 +1100351 if (authctxt->valid &&
352#ifdef HAVE_CYGWIN
Damien Miller0dea79d2001-12-29 14:08:28 +1100353 check_nt_auth(1, authctxt->pw) &&
Damien Miller874d77b2000-10-14 16:23:11 +1100354#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000355#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100356 auth_pam_password(authctxt->pw, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000357#elif defined(HAVE_OSF_SIA)
Damien Miller92ddb7d2001-02-14 01:25:23 +1100358 auth_sia_password(authctxt->user, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000359#else /* !USE_PAM && !HAVE_OSF_SIA */
Damien Miller60396b02001-02-18 17:01:00 +1100360 auth_password(authctxt, password) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000361#endif /* USE_PAM */
362 authenticated = 1;
363 memset(password, 0, len);
364 xfree(password);
365 return authenticated;
366}
Damien Miller874d77b2000-10-14 16:23:11 +1100367
Ben Lindstrombba81212001-06-25 05:01:22 +0000368static int
Damien Miller874d77b2000-10-14 16:23:11 +1100369userauth_kbdint(Authctxt *authctxt)
370{
371 int authenticated = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000372 char *lang, *devs;
Damien Miller874d77b2000-10-14 16:23:11 +1100373
374 lang = packet_get_string(NULL);
375 devs = packet_get_string(NULL);
Damien Miller48b03fc2002-01-22 23:11:40 +1100376 packet_check_eom();
Damien Miller874d77b2000-10-14 16:23:11 +1100377
Ben Lindstrom551ea372001-06-05 18:56:16 +0000378 debug("keyboard-interactive devs %s", devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000379
Ben Lindstrom551ea372001-06-05 18:56:16 +0000380 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000381 authenticated = auth2_challenge(authctxt, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000382
Damien Millerb8481582000-12-03 11:51:51 +1100383#ifdef USE_PAM
Damien Millerf8154422001-04-25 22:44:14 +1000384 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
Damien Millerb8481582000-12-03 11:51:51 +1100385 authenticated = auth2_pam(authctxt);
386#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100387 xfree(devs);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000388 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +1100389#ifdef HAVE_CYGWIN
Damien Miller0dea79d2001-12-29 14:08:28 +1100390 if (check_nt_auth(0, authctxt->pw) == 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100391 return(0);
392#endif
393 return authenticated;
394}
395
Ben Lindstrombba81212001-06-25 05:01:22 +0000396static int
Damien Miller874d77b2000-10-14 16:23:11 +1100397userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000398{
399 Buffer b;
Damien Miller9b74bfc2002-02-05 12:26:03 +1100400 Key *key = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000401 char *pkalg, *pkblob, *sig;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000402 u_int alen, blen, slen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100403 int have_sig, pktype;
Damien Millereba71ba2000-04-29 23:57:08 +1000404 int authenticated = 0;
405
Damien Miller874d77b2000-10-14 16:23:11 +1100406 if (!authctxt->valid) {
407 debug2("userauth_pubkey: disabled because of invalid user");
Damien Millereba71ba2000-04-29 23:57:08 +1000408 return 0;
409 }
410 have_sig = packet_get_char();
Ben Lindstromd121f612000-12-03 17:00:47 +0000411 if (datafellows & SSH_BUG_PKAUTH) {
412 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
413 /* no explicit pkalg given */
414 pkblob = packet_get_string(&blen);
415 buffer_init(&b);
416 buffer_append(&b, pkblob, blen);
417 /* so we have to extract the pkalg from the pkblob */
418 pkalg = buffer_get_string(&b, &alen);
419 buffer_free(&b);
420 } else {
421 pkalg = packet_get_string(&alen);
422 pkblob = packet_get_string(&blen);
423 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100424 pktype = key_type_from_name(pkalg);
425 if (pktype == KEY_UNSPEC) {
Ben Lindstromd121f612000-12-03 17:00:47 +0000426 /* this is perfectly legal */
Damien Miller9b74bfc2002-02-05 12:26:03 +1100427 log("userauth_pubkey: unsupported public key algorithm: %s",
428 pkalg);
429 goto done;
Damien Millereba71ba2000-04-29 23:57:08 +1000430 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100431 key = key_from_blob(pkblob, blen);
Damien Miller9b74bfc2002-02-05 12:26:03 +1100432 if (key == NULL) {
433 error("userauth_pubkey: cannot decode key: %s", pkalg);
434 goto done;
Damien Millereba71ba2000-04-29 23:57:08 +1000435 }
Damien Miller9b74bfc2002-02-05 12:26:03 +1100436 if (key->type != pktype) {
437 error("userauth_pubkey: type mismatch for decoded key "
438 "(received %d, expected %d)", key->type, pktype);
439 goto done;
440 }
441 if (have_sig) {
442 sig = packet_get_string(&slen);
443 packet_check_eom();
444 buffer_init(&b);
445 if (datafellows & SSH_OLD_SESSIONID) {
446 buffer_append(&b, session_id2, session_id2_len);
447 } else {
448 buffer_put_string(&b, session_id2, session_id2_len);
449 }
450 /* reconstruct packet */
451 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
452 buffer_put_cstring(&b, authctxt->user);
453 buffer_put_cstring(&b,
454 datafellows & SSH_BUG_PKSERVICE ?
455 "ssh-userauth" :
456 authctxt->service);
457 if (datafellows & SSH_BUG_PKAUTH) {
458 buffer_put_char(&b, have_sig);
459 } else {
460 buffer_put_cstring(&b, "publickey");
461 buffer_put_char(&b, have_sig);
462 buffer_put_cstring(&b, pkalg);
463 }
464 buffer_put_string(&b, pkblob, blen);
465#ifdef DEBUG_PK
466 buffer_dump(&b);
467#endif
468 /* test for correct signature */
469 if (user_key_allowed(authctxt->pw, key) &&
470 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
471 authenticated = 1;
472 buffer_clear(&b);
473 xfree(sig);
474 } else {
475 debug("test whether pkalg/pkblob are acceptable");
476 packet_check_eom();
477
478 /* XXX fake reply and always send PK_OK ? */
479 /*
480 * XXX this allows testing whether a user is allowed
481 * to login: if you happen to have a valid pubkey this
482 * message is sent. the message is NEVER sent at all
483 * if a user is not allowed to login. is this an
484 * issue? -markus
485 */
486 if (user_key_allowed(authctxt->pw, key)) {
487 packet_start(SSH2_MSG_USERAUTH_PK_OK);
488 packet_put_string(pkalg, alen);
489 packet_put_string(pkblob, blen);
490 packet_send();
491 packet_write_wait();
492 authctxt->postponed = 1;
493 }
494 }
495 if (authenticated != 1)
496 auth_clear_options();
497done:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100498 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
Damien Miller9b74bfc2002-02-05 12:26:03 +1100499 if (key != NULL)
500 key_free(key);
Damien Millereba71ba2000-04-29 23:57:08 +1000501 xfree(pkalg);
502 xfree(pkblob);
Damien Miller874d77b2000-10-14 16:23:11 +1100503#ifdef HAVE_CYGWIN
Damien Miller0dea79d2001-12-29 14:08:28 +1100504 if (check_nt_auth(0, authctxt->pw) == 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100505 return(0);
506#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000507 return authenticated;
508}
509
Ben Lindstrombba81212001-06-25 05:01:22 +0000510static int
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000511userauth_hostbased(Authctxt *authctxt)
512{
513 Buffer b;
Damien Miller9b74bfc2002-02-05 12:26:03 +1100514 Key *key = NULL;
Ben Lindstrom671388f2001-04-19 20:40:45 +0000515 char *pkalg, *pkblob, *sig, *cuser, *chost, *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000516 u_int alen, blen, slen;
517 int pktype;
518 int authenticated = 0;
519
520 if (!authctxt->valid) {
521 debug2("userauth_hostbased: disabled because of invalid user");
522 return 0;
523 }
524 pkalg = packet_get_string(&alen);
525 pkblob = packet_get_string(&blen);
526 chost = packet_get_string(NULL);
527 cuser = packet_get_string(NULL);
528 sig = packet_get_string(&slen);
529
530 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
531 cuser, chost, pkalg, slen);
532#ifdef DEBUG_PK
533 debug("signature:");
534 buffer_init(&b);
535 buffer_append(&b, sig, slen);
536 buffer_dump(&b);
537 buffer_free(&b);
538#endif
539 pktype = key_type_from_name(pkalg);
540 if (pktype == KEY_UNSPEC) {
541 /* this is perfectly legal */
542 log("userauth_hostbased: unsupported "
543 "public key algorithm: %s", pkalg);
544 goto done;
545 }
546 key = key_from_blob(pkblob, blen);
547 if (key == NULL) {
Damien Miller9b74bfc2002-02-05 12:26:03 +1100548 error("userauth_hostbased: cannot decode key: %s", pkalg);
549 goto done;
550 }
551 if (key->type != pktype) {
552 error("userauth_hostbased: type mismatch for decoded key "
553 "(received %d, expected %d)", key->type, pktype);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000554 goto done;
555 }
Ben Lindstrom671388f2001-04-19 20:40:45 +0000556 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
557 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000558 buffer_init(&b);
Ben Lindstrom671388f2001-04-19 20:40:45 +0000559 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000560 /* reconstruct packet */
561 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
562 buffer_put_cstring(&b, authctxt->user);
Ben Lindstrom671388f2001-04-19 20:40:45 +0000563 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000564 buffer_put_cstring(&b, "hostbased");
565 buffer_put_string(&b, pkalg, alen);
566 buffer_put_string(&b, pkblob, blen);
567 buffer_put_cstring(&b, chost);
568 buffer_put_cstring(&b, cuser);
569#ifdef DEBUG_PK
570 buffer_dump(&b);
571#endif
572 /* test for allowed key and correct signature */
573 if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
574 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
575 authenticated = 1;
576
577 buffer_clear(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000578done:
579 debug2("userauth_hostbased: authenticated %d", authenticated);
Damien Miller9b74bfc2002-02-05 12:26:03 +1100580 if (key != NULL)
581 key_free(key);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000582 xfree(pkalg);
583 xfree(pkblob);
584 xfree(cuser);
585 xfree(chost);
586 xfree(sig);
587 return authenticated;
588}
589
Damien Miller874d77b2000-10-14 16:23:11 +1100590/* get current user */
Damien Millereba71ba2000-04-29 23:57:08 +1000591
592struct passwd*
593auth_get_user(void)
594{
Damien Miller874d77b2000-10-14 16:23:11 +1100595 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000596}
597
Damien Miller874d77b2000-10-14 16:23:11 +1100598#define DELIM ","
Damien Millereba71ba2000-04-29 23:57:08 +1000599
Ben Lindstrom79073822001-07-04 03:42:30 +0000600static char *
Damien Miller874d77b2000-10-14 16:23:11 +1100601authmethods_get(void)
602{
603 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +1100604 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100605 char *list;
606
Damien Miller0e3b8722002-01-22 23:26:38 +1100607 buffer_init(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100608 for (method = authmethods; method->name != NULL; method++) {
609 if (strcmp(method->name, "none") == 0)
610 continue;
611 if (method->enabled != NULL && *(method->enabled) != 0) {
Damien Miller0e3b8722002-01-22 23:26:38 +1100612 if (buffer_len(&b) > 0)
613 buffer_append(&b, ",", 1);
614 buffer_append(&b, method->name, strlen(method->name));
Damien Millereba71ba2000-04-29 23:57:08 +1000615 }
616 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100617 buffer_append(&b, "\0", 1);
618 list = xstrdup(buffer_ptr(&b));
619 buffer_free(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100620 return list;
621}
622
Ben Lindstrombba81212001-06-25 05:01:22 +0000623static Authmethod *
Damien Miller874d77b2000-10-14 16:23:11 +1100624authmethod_lookup(const char *name)
625{
626 Authmethod *method = NULL;
627 if (name != NULL)
628 for (method = authmethods; method->name != NULL; method++)
629 if (method->enabled != NULL &&
630 *(method->enabled) != 0 &&
631 strcmp(name, method->name) == 0)
632 return method;
633 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
634 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000635}
636
637/* return 1 if user allows given key */
Ben Lindstrombba81212001-06-25 05:01:22 +0000638static int
Ben Lindstromf96704d2001-06-25 04:17:12 +0000639user_key_allowed2(struct passwd *pw, Key *key, char *file)
Damien Millereba71ba2000-04-29 23:57:08 +1000640{
Ben Lindstromf96704d2001-06-25 04:17:12 +0000641 char line[8192];
Damien Millereba71ba2000-04-29 23:57:08 +1000642 int found_key = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000643 FILE *f;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000644 u_long linenum = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000645 struct stat st;
646 Key *found;
Damien Millerda9edca2001-12-21 12:48:54 +1100647 char *fp;
Damien Millereba71ba2000-04-29 23:57:08 +1000648
Damien Miller874d77b2000-10-14 16:23:11 +1100649 if (pw == NULL)
650 return 0;
651
Damien Millereba71ba2000-04-29 23:57:08 +1000652 /* Temporarily use the user's uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000653 temporarily_use_uid(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000654
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000655 debug("trying public key file %s", file);
Damien Millereba71ba2000-04-29 23:57:08 +1000656
657 /* Fail quietly if file does not exist */
658 if (stat(file, &st) < 0) {
659 /* Restore the privileged uid. */
660 restore_uid();
661 return 0;
662 }
663 /* Open the file containing the authorized keys. */
664 f = fopen(file, "r");
665 if (!f) {
666 /* Restore the privileged uid. */
667 restore_uid();
668 return 0;
669 }
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000670 if (options.strict_modes &&
Ben Lindstrom90279d82001-07-04 03:56:56 +0000671 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000672 fclose(f);
673 log("Authentication refused: %s", line);
674 restore_uid();
675 return 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000676 }
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000677
Damien Millereba71ba2000-04-29 23:57:08 +1000678 found_key = 0;
Damien Millerd3444942000-09-30 14:20:03 +1100679 found = key_new(key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000680
681 while (fgets(line, sizeof(line), f)) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000682 char *cp, *options = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000683 linenum++;
684 /* Skip leading whitespace, empty and comment lines. */
685 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
686 ;
687 if (!*cp || *cp == '\n' || *cp == '#')
688 continue;
Damien Millerf6d9e222000-06-18 14:50:44 +1000689
Ben Lindstrom1bc3bdb2001-09-20 23:11:26 +0000690 if (key_read(found, &cp) != 1) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000691 /* no key? check if there are options for this key */
692 int quoted = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100693 debug2("user_key_allowed: check options: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000694 options = cp;
695 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
696 if (*cp == '\\' && cp[1] == '"')
697 cp++; /* Skip both */
698 else if (*cp == '"')
699 quoted = !quoted;
700 }
701 /* Skip remaining whitespace. */
702 for (; *cp == ' ' || *cp == '\t'; cp++)
703 ;
Ben Lindstrom1bc3bdb2001-09-20 23:11:26 +0000704 if (key_read(found, &cp) != 1) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100705 debug2("user_key_allowed: advance: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000706 /* still no key? advance to next line*/
707 continue;
708 }
709 }
710 if (key_equal(found, key) &&
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000711 auth_parse_options(pw, options, file, linenum) == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000712 found_key = 1;
Ben Lindstrom940fb862001-08-06 21:01:49 +0000713 debug("matching key found: file %s, line %lu",
Damien Millereba71ba2000-04-29 23:57:08 +1000714 file, linenum);
Damien Millerda9edca2001-12-21 12:48:54 +1100715 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
716 verbose("Found matching %s key: %s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100717 key_type(found), fp);
Damien Millerda9edca2001-12-21 12:48:54 +1100718 xfree(fp);
Damien Millereba71ba2000-04-29 23:57:08 +1000719 break;
720 }
721 }
722 restore_uid();
723 fclose(f);
724 key_free(found);
Ben Lindstromb54873a2001-03-11 20:01:55 +0000725 if (!found_key)
726 debug2("key not found");
Damien Millereba71ba2000-04-29 23:57:08 +1000727 return found_key;
728}
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000729
Ben Lindstromf96704d2001-06-25 04:17:12 +0000730/* check whether given key is in .ssh/authorized_keys* */
Ben Lindstrombba81212001-06-25 05:01:22 +0000731static int
Ben Lindstromf96704d2001-06-25 04:17:12 +0000732user_key_allowed(struct passwd *pw, Key *key)
733{
734 int success;
735 char *file;
736
737 file = authorized_keys_file(pw);
738 success = user_key_allowed2(pw, key, file);
739 xfree(file);
740 if (success)
741 return success;
742
743 /* try suffix "2" for backward compat, too */
744 file = authorized_keys_file2(pw);
745 success = user_key_allowed2(pw, key, file);
746 xfree(file);
747 return success;
748}
749
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000750/* return 1 if given hostkey is allowed */
Ben Lindstrombba81212001-06-25 05:01:22 +0000751static int
Ben Lindstrom4aa603c2001-04-19 20:38:06 +0000752hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000753 Key *key)
754{
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000755 const char *resolvedname, *ipaddr, *lookup;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000756 HostStatus host_status;
757 int len;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000758
Damien Millerc5d86352002-02-05 12:13:41 +1100759 resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000760 ipaddr = get_remote_ipaddr();
761
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000762 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
763 chost, resolvedname, ipaddr);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000764
765 if (options.hostbased_uses_name_from_packet_only) {
766 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
767 return 0;
768 lookup = chost;
769 } else {
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000770 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
771 debug2("stripping trailing dot from chost %s", chost);
772 chost[len - 1] = '\0';
773 }
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000774 if (strcasecmp(resolvedname, chost) != 0)
775 log("userauth_hostbased mismatch: "
776 "client sends %s, but we resolve %s to %s",
777 chost, ipaddr, resolvedname);
778 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
779 return 0;
780 lookup = resolvedname;
781 }
782 debug2("userauth_hostbased: access allowed by auth_rhosts2");
783
Ben Lindstrom83647ce2001-06-25 04:30:16 +0000784 host_status = check_key_in_hostfiles(pw, key, lookup,
785 _PATH_SSH_SYSTEM_HOSTFILE,
Ben Lindstroma4789ef2001-06-25 04:40:49 +0000786 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000787
Ben Lindstrom83647ce2001-06-25 04:30:16 +0000788 /* backward compat if no key has been found. */
789 if (host_status == HOST_NEW)
790 host_status = check_key_in_hostfiles(pw, key, lookup,
791 _PATH_SSH_SYSTEM_HOSTFILE2,
Ben Lindstroma4789ef2001-06-25 04:40:49 +0000792 options.ignore_user_known_hosts ? NULL :
793 _PATH_SSH_USER_HOSTFILE2);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000794
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000795 return (host_status == HOST_OK);
796}
Ben Lindstrom83647ce2001-06-25 04:30:16 +0000797