blob: 9bfcde5c1de7304de5db396c09a4c4d9eb6f8ac0 [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"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000026RCSID("$OpenBSD: auth2.c,v 1.88 2002/03/18 17:50:31 provos 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"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000054#include "monitor_wrap.h"
Damien Millereba71ba2000-04-29 23:57:08 +100055
56/* import */
57extern ServerOptions options;
Ben Lindstrom46c16222000-12-22 01:43:59 +000058extern u_char *session_id2;
Damien Millereba71ba2000-04-29 23:57:08 +100059extern int session_id2_len;
60
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000061Authctxt *x_authctxt = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +110062static int one = 1;
63
64typedef struct Authmethod Authmethod;
65struct Authmethod {
66 char *name;
67 int (*userauth)(Authctxt *authctxt);
68 int *enabled;
69};
70
Damien Millereba71ba2000-04-29 23:57:08 +100071/* protocol */
72
Damien Miller630d6f42002-01-22 23:17:30 +110073static void input_service_request(int, u_int32_t, void *);
74static void input_userauth_request(int, u_int32_t, void *);
Damien Millereba71ba2000-04-29 23:57:08 +100075
Damien Millereba71ba2000-04-29 23:57:08 +100076/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +000077static Authmethod *authmethod_lookup(const char *);
Ben Lindstrom79073822001-07-04 03:42:30 +000078static char *authmethods_get(void);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000079int user_key_allowed(struct passwd *, Key *);
80int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
Damien Millereba71ba2000-04-29 23:57:08 +100081
Damien Miller874d77b2000-10-14 16:23:11 +110082/* auth */
Ben Lindstrombba81212001-06-25 05:01:22 +000083static void userauth_banner(void);
84static int userauth_none(Authctxt *);
85static int userauth_passwd(Authctxt *);
86static int userauth_pubkey(Authctxt *);
87static int userauth_hostbased(Authctxt *);
88static int userauth_kbdint(Authctxt *);
Damien Miller874d77b2000-10-14 16:23:11 +110089
90Authmethod authmethods[] = {
91 {"none",
92 userauth_none,
93 &one},
94 {"publickey",
95 userauth_pubkey,
Damien Miller0bc1bd82000-11-13 22:57:25 +110096 &options.pubkey_authentication},
Damien Miller874d77b2000-10-14 16:23:11 +110097 {"password",
98 userauth_passwd,
99 &options.password_authentication},
Ben Lindstromd1f20ec2001-02-10 21:31:53 +0000100 {"keyboard-interactive",
101 userauth_kbdint,
102 &options.kbd_interactive_authentication},
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000103 {"hostbased",
104 userauth_hostbased,
105 &options.hostbased_authentication},
Damien Miller874d77b2000-10-14 16:23:11 +1100106 {NULL, NULL, NULL}
Damien Millereba71ba2000-04-29 23:57:08 +1000107};
Damien Millereba71ba2000-04-29 23:57:08 +1000108
109/*
Damien Miller874d77b2000-10-14 16:23:11 +1100110 * loop until authctxt->success == TRUE
Damien Millereba71ba2000-04-29 23:57:08 +1000111 */
112
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +0000113Authctxt *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000114do_authentication2(void)
Damien Millereba71ba2000-04-29 23:57:08 +1000115{
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000116 Authctxt *authctxt = authctxt_new();
117
Damien Miller874d77b2000-10-14 16:23:11 +1100118 x_authctxt = authctxt; /*XXX*/
119
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000120 /* challenge-response is implemented via keyboard interactive */
Ben Lindstrom551ea372001-06-05 18:56:16 +0000121 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000122 options.kbd_interactive_authentication = 1;
Damien Millerf8154422001-04-25 22:44:14 +1000123 if (options.pam_authentication_via_kbd_int)
124 options.kbd_interactive_authentication = 1;
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000125
Damien Miller7d053392002-01-22 23:24:13 +1100126 dispatch_init(&dispatch_protocol_error);
Damien Millereba71ba2000-04-29 23:57:08 +1000127 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
Damien Miller874d77b2000-10-14 16:23:11 +1100128 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +0000129
130 return (authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000131}
132
Ben Lindstrombba81212001-06-25 05:01:22 +0000133static void
Damien Miller630d6f42002-01-22 23:17:30 +1100134input_service_request(int type, u_int32_t seq, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000135{
Damien Miller874d77b2000-10-14 16:23:11 +1100136 Authctxt *authctxt = ctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000137 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000138 int accept = 0;
139 char *service = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +1100140 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000141
Damien Miller874d77b2000-10-14 16:23:11 +1100142 if (authctxt == NULL)
143 fatal("input_service_request: no authctxt");
144
Damien Millereba71ba2000-04-29 23:57:08 +1000145 if (strcmp(service, "ssh-userauth") == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100146 if (!authctxt->success) {
Damien Millereba71ba2000-04-29 23:57:08 +1000147 accept = 1;
148 /* now we can handle user-auth requests */
149 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
150 }
151 }
152 /* XXX all other service requests are denied */
153
154 if (accept) {
155 packet_start(SSH2_MSG_SERVICE_ACCEPT);
156 packet_put_cstring(service);
157 packet_send();
158 packet_write_wait();
159 } else {
160 debug("bad service request %s", service);
161 packet_disconnect("bad service request %s", service);
162 }
163 xfree(service);
164}
165
Ben Lindstrombba81212001-06-25 05:01:22 +0000166static void
Damien Miller630d6f42002-01-22 23:17:30 +1100167input_userauth_request(int type, u_int32_t seq, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000168{
Damien Miller874d77b2000-10-14 16:23:11 +1100169 Authctxt *authctxt = ctxt;
170 Authmethod *m = NULL;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000171 char *user, *service, *method, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000172 int authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000173
Damien Miller874d77b2000-10-14 16:23:11 +1100174 if (authctxt == NULL)
175 fatal("input_userauth_request: no authctxt");
Damien Millereba71ba2000-04-29 23:57:08 +1000176
Damien Miller874d77b2000-10-14 16:23:11 +1100177 user = packet_get_string(NULL);
178 service = packet_get_string(NULL);
179 method = packet_get_string(NULL);
180 debug("userauth-request for user %s service %s method %s", user, service, method);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000181 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
Damien Miller874d77b2000-10-14 16:23:11 +1100182
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000183 if ((style = strchr(user, ':')) != NULL)
184 *style++ = 0;
185
Kevin Stevesef4eea92001-02-05 12:42:17 +0000186 if (authctxt->attempt++ == 0) {
Damien Miller3a5b0232002-03-13 13:19:42 +1100187 /* setup auth context */
Damien Miller874d77b2000-10-14 16:23:11 +1100188 struct passwd *pw = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000189 pw = PRIVSEP(getpwnamallow(user));
Ben Lindstrom2ae18f42002-03-22 01:24:38 +0000190 if (pw && strcmp(service, "ssh-connection")==0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100191 authctxt->pw = pwcopy(pw);
192 authctxt->valid = 1;
193 debug2("input_userauth_request: setting up authctxt for %s", user);
194#ifdef USE_PAM
Damien Miller22e22bf2001-01-19 15:46:38 +1100195 start_pam(pw->pw_name);
Damien Miller874d77b2000-10-14 16:23:11 +1100196#endif
197 } else {
198 log("input_userauth_request: illegal user %s", user);
Damien Miller22e22bf2001-01-19 15:46:38 +1100199#ifdef USE_PAM
200 start_pam("NOUSER");
201#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100202 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000203 /* Free memory */
204 if (use_privsep && pw != NULL)
205 pwfree(pw);
206
207 setproctitle("%s%s", pw ? user : "unknown",
208 use_privsep ? " [net]" : "");
Damien Miller874d77b2000-10-14 16:23:11 +1100209 authctxt->user = xstrdup(user);
210 authctxt->service = xstrdup(service);
Ben Lindstrom9d0c0662001-06-09 01:40:00 +0000211 authctxt->style = style ? xstrdup(style) : NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000212
213 if (use_privsep)
214 mm_inform_authserv(service, style);
Ben Lindstrom9d0c0662001-06-09 01:40:00 +0000215 } else if (strcmp(user, authctxt->user) != 0 ||
216 strcmp(service, authctxt->service) != 0) {
217 packet_disconnect("Change of username or service not allowed: "
218 "(%s,%s) -> (%s,%s)",
219 authctxt->user, authctxt->service, user, service);
Damien Millereba71ba2000-04-29 23:57:08 +1000220 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000221 /* reset state */
Damien Milleree116252001-12-21 12:42:34 +1100222 auth2_challenge_stop(authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000223 authctxt->postponed = 0;
Damien Millerb70b61f2000-09-16 16:25:12 +1100224
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000225 /* try to authenticate user */
Damien Miller874d77b2000-10-14 16:23:11 +1100226 m = authmethod_lookup(method);
227 if (m != NULL) {
228 debug2("input_userauth_request: try method %s", method);
229 authenticated = m->userauth(authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100230 }
Damien Miller5d57e502001-03-30 10:48:31 +1000231 userauth_finish(authctxt, authenticated, method);
232
233 xfree(service);
234 xfree(user);
235 xfree(method);
236}
237
238void
239userauth_finish(Authctxt *authctxt, int authenticated, char *method)
240{
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000241 char *methods;
242
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000243 if (!authctxt->valid && authenticated)
244 fatal("INTERNAL ERROR: authenticated invalid user %s",
245 authctxt->user);
Damien Millerb70b61f2000-09-16 16:25:12 +1100246
Damien Miller874d77b2000-10-14 16:23:11 +1100247 /* Special handling for root */
Ben Lindstromd8a90212001-02-15 03:08:27 +0000248 if (authenticated && authctxt->pw->pw_uid == 0 &&
249 !auth_root_allowed(method))
Damien Millereba71ba2000-04-29 23:57:08 +1000250 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000251
252#ifdef USE_PAM
Damien Millerc83de6d2001-02-16 14:17:59 +1100253 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
254 NULL))
Damien Millerb70b61f2000-09-16 16:25:12 +1100255 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000256#endif /* USE_PAM */
257
Damien Miller874d77b2000-10-14 16:23:11 +1100258 /* Log before sending the reply */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000259 auth_log(authctxt, authenticated, method, " ssh2");
260
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000261 if (authctxt->postponed)
262 return;
263
264 /* XXX todo: check if multiple auth methods are needed */
265 if (authenticated == 1) {
266 /* turn off userauth */
Damien Miller7d053392002-01-22 23:24:13 +1100267 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000268 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
269 packet_send();
270 packet_write_wait();
271 /* now we can break out */
272 authctxt->success = 1;
273 } else {
Damien Millere49d0962001-11-13 23:46:18 +1100274 if (authctxt->failures++ > AUTH_FAIL_MAX) {
275#ifdef WITH_AIXAUTHENTICATE
276 loginfailed(authctxt->user,
Damien Millerf3451a22002-02-05 12:40:46 +1100277 get_canonical_hostname(options.verify_reverse_mapping),
Damien Millere49d0962001-11-13 23:46:18 +1100278 "ssh");
279#endif /* WITH_AIXAUTHENTICATE */
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000280 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Damien Millere49d0962001-11-13 23:46:18 +1100281 }
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000282 methods = authmethods_get();
283 packet_start(SSH2_MSG_USERAUTH_FAILURE);
284 packet_put_cstring(methods);
285 packet_put_char(0); /* XXX partial success, unused */
286 packet_send();
287 packet_write_wait();
288 xfree(methods);
289 }
Damien Miller874d77b2000-10-14 16:23:11 +1100290}
291
Ben Lindstrombba81212001-06-25 05:01:22 +0000292static void
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000293userauth_banner(void)
294{
295 struct stat st;
296 char *banner = NULL;
297 off_t len, n;
298 int fd;
299
300 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
301 return;
Ben Lindstrom0cae0402001-04-04 23:47:52 +0000302 if ((fd = open(options.banner, O_RDONLY)) < 0)
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000303 return;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000304 if (fstat(fd, &st) < 0)
305 goto done;
306 len = st.st_size;
307 banner = xmalloc(len + 1);
308 if ((n = read(fd, banner, len)) < 0)
309 goto done;
310 banner[n] = '\0';
311 packet_start(SSH2_MSG_USERAUTH_BANNER);
312 packet_put_cstring(banner);
313 packet_put_cstring(""); /* language, unused */
314 packet_send();
315 debug("userauth_banner: sent");
316done:
317 if (banner)
318 xfree(banner);
319 close(fd);
320 return;
321}
Damien Miller874d77b2000-10-14 16:23:11 +1100322
Ben Lindstrombba81212001-06-25 05:01:22 +0000323static int
Damien Miller874d77b2000-10-14 16:23:11 +1100324userauth_none(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000325{
Damien Miller874d77b2000-10-14 16:23:11 +1100326 /* disable method "none", only allowed one time */
327 Authmethod *m = authmethod_lookup("none");
328 if (m != NULL)
329 m->enabled = NULL;
Damien Miller48b03fc2002-01-22 23:11:40 +1100330 packet_check_eom();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000331 userauth_banner();
Damien Millerb8c656e2000-06-28 15:22:41 +1000332
Damien Miller874d77b2000-10-14 16:23:11 +1100333 if (authctxt->valid == 0)
334 return(0);
Damien Miller3a5b0232002-03-13 13:19:42 +1100335
336#ifdef HAVE_CYGWIN
337 if (check_nt_auth(1, authctxt->pw) == 0)
338 return(0);
Damien Miller874d77b2000-10-14 16:23:11 +1100339#endif
Damien Miller3a5b0232002-03-13 13:19:42 +1100340#ifdef USE_PAM
341 return auth_pam_password(authctxt->pw, "");
342#elif defined(HAVE_OSF_SIA)
343 return 0;
344#else /* !HAVE_OSF_SIA && !USE_PAM */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000345 return PRIVSEP(auth_password(authctxt, ""));
Damien Miller3a5b0232002-03-13 13:19:42 +1100346#endif /* USE_PAM */
Damien Millereba71ba2000-04-29 23:57:08 +1000347}
Damien Miller874d77b2000-10-14 16:23:11 +1100348
Ben Lindstrombba81212001-06-25 05:01:22 +0000349static int
Damien Miller874d77b2000-10-14 16:23:11 +1100350userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000351{
352 char *password;
353 int authenticated = 0;
354 int change;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000355 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000356 change = packet_get_char();
357 if (change)
358 log("password change not supported");
359 password = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +1100360 packet_check_eom();
Damien Miller3a5b0232002-03-13 13:19:42 +1100361 if (authctxt->valid &&
362#ifdef HAVE_CYGWIN
363 check_nt_auth(1, authctxt->pw) &&
Damien Miller874d77b2000-10-14 16:23:11 +1100364#endif
Damien Miller3a5b0232002-03-13 13:19:42 +1100365#ifdef USE_PAM
366 auth_pam_password(authctxt->pw, password) == 1)
367#elif defined(HAVE_OSF_SIA)
368 auth_sia_password(authctxt->user, password) == 1)
369#else /* !USE_PAM && !HAVE_OSF_SIA */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000370 PRIVSEP(auth_password(authctxt, password)) == 1)
Damien Miller3a5b0232002-03-13 13:19:42 +1100371#endif /* USE_PAM */
372 authenticated = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000373 memset(password, 0, len);
374 xfree(password);
375 return authenticated;
376}
Damien Miller874d77b2000-10-14 16:23:11 +1100377
Ben Lindstrombba81212001-06-25 05:01:22 +0000378static int
Damien Miller874d77b2000-10-14 16:23:11 +1100379userauth_kbdint(Authctxt *authctxt)
380{
381 int authenticated = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000382 char *lang, *devs;
Damien Miller874d77b2000-10-14 16:23:11 +1100383
384 lang = packet_get_string(NULL);
385 devs = packet_get_string(NULL);
Damien Miller48b03fc2002-01-22 23:11:40 +1100386 packet_check_eom();
Damien Miller874d77b2000-10-14 16:23:11 +1100387
Ben Lindstrom551ea372001-06-05 18:56:16 +0000388 debug("keyboard-interactive devs %s", devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000389
Ben Lindstrom551ea372001-06-05 18:56:16 +0000390 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000391 authenticated = auth2_challenge(authctxt, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000392
Damien Millerb8481582000-12-03 11:51:51 +1100393#ifdef USE_PAM
Damien Millerf8154422001-04-25 22:44:14 +1000394 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
Damien Millerb8481582000-12-03 11:51:51 +1100395 authenticated = auth2_pam(authctxt);
396#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100397 xfree(devs);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000398 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +1100399#ifdef HAVE_CYGWIN
Damien Miller0dea79d2001-12-29 14:08:28 +1100400 if (check_nt_auth(0, authctxt->pw) == 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100401 return(0);
402#endif
403 return authenticated;
404}
405
Ben Lindstrombba81212001-06-25 05:01:22 +0000406static int
Damien Miller874d77b2000-10-14 16:23:11 +1100407userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000408{
409 Buffer b;
Damien Miller9b74bfc2002-02-05 12:26:03 +1100410 Key *key = NULL;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000411 char *pkalg;
412 u_char *pkblob, *sig;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000413 u_int alen, blen, slen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100414 int have_sig, pktype;
Damien Millereba71ba2000-04-29 23:57:08 +1000415 int authenticated = 0;
416
Damien Miller874d77b2000-10-14 16:23:11 +1100417 if (!authctxt->valid) {
418 debug2("userauth_pubkey: disabled because of invalid user");
Damien Millereba71ba2000-04-29 23:57:08 +1000419 return 0;
420 }
421 have_sig = packet_get_char();
Ben Lindstromd121f612000-12-03 17:00:47 +0000422 if (datafellows & SSH_BUG_PKAUTH) {
423 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
424 /* no explicit pkalg given */
425 pkblob = packet_get_string(&blen);
426 buffer_init(&b);
427 buffer_append(&b, pkblob, blen);
428 /* so we have to extract the pkalg from the pkblob */
429 pkalg = buffer_get_string(&b, &alen);
430 buffer_free(&b);
431 } else {
432 pkalg = packet_get_string(&alen);
433 pkblob = packet_get_string(&blen);
434 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100435 pktype = key_type_from_name(pkalg);
436 if (pktype == KEY_UNSPEC) {
Ben Lindstromd121f612000-12-03 17:00:47 +0000437 /* this is perfectly legal */
Damien Miller9b74bfc2002-02-05 12:26:03 +1100438 log("userauth_pubkey: unsupported public key algorithm: %s",
439 pkalg);
440 goto done;
Damien Millereba71ba2000-04-29 23:57:08 +1000441 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100442 key = key_from_blob(pkblob, blen);
Damien Miller9b74bfc2002-02-05 12:26:03 +1100443 if (key == NULL) {
444 error("userauth_pubkey: cannot decode key: %s", pkalg);
445 goto done;
Damien Millereba71ba2000-04-29 23:57:08 +1000446 }
Damien Miller9b74bfc2002-02-05 12:26:03 +1100447 if (key->type != pktype) {
448 error("userauth_pubkey: type mismatch for decoded key "
449 "(received %d, expected %d)", key->type, pktype);
450 goto done;
451 }
452 if (have_sig) {
453 sig = packet_get_string(&slen);
454 packet_check_eom();
455 buffer_init(&b);
456 if (datafellows & SSH_OLD_SESSIONID) {
457 buffer_append(&b, session_id2, session_id2_len);
458 } else {
459 buffer_put_string(&b, session_id2, session_id2_len);
460 }
461 /* reconstruct packet */
462 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
463 buffer_put_cstring(&b, authctxt->user);
464 buffer_put_cstring(&b,
465 datafellows & SSH_BUG_PKSERVICE ?
466 "ssh-userauth" :
467 authctxt->service);
468 if (datafellows & SSH_BUG_PKAUTH) {
469 buffer_put_char(&b, have_sig);
470 } else {
471 buffer_put_cstring(&b, "publickey");
472 buffer_put_char(&b, have_sig);
473 buffer_put_cstring(&b, pkalg);
474 }
475 buffer_put_string(&b, pkblob, blen);
476#ifdef DEBUG_PK
477 buffer_dump(&b);
478#endif
479 /* test for correct signature */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000480 authenticated = 0;
481 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
482 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
483 buffer_len(&b))) == 1)
Damien Miller3a5b0232002-03-13 13:19:42 +1100484 authenticated = 1;
Damien Miller9b74bfc2002-02-05 12:26:03 +1100485 buffer_clear(&b);
486 xfree(sig);
487 } else {
488 debug("test whether pkalg/pkblob are acceptable");
489 packet_check_eom();
490
491 /* XXX fake reply and always send PK_OK ? */
492 /*
493 * XXX this allows testing whether a user is allowed
494 * to login: if you happen to have a valid pubkey this
495 * message is sent. the message is NEVER sent at all
496 * if a user is not allowed to login. is this an
497 * issue? -markus
498 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000499 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
Damien Miller9b74bfc2002-02-05 12:26:03 +1100500 packet_start(SSH2_MSG_USERAUTH_PK_OK);
501 packet_put_string(pkalg, alen);
502 packet_put_string(pkblob, blen);
503 packet_send();
504 packet_write_wait();
505 authctxt->postponed = 1;
506 }
507 }
508 if (authenticated != 1)
509 auth_clear_options();
510done:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100511 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
Damien Miller9b74bfc2002-02-05 12:26:03 +1100512 if (key != NULL)
513 key_free(key);
Damien Millereba71ba2000-04-29 23:57:08 +1000514 xfree(pkalg);
515 xfree(pkblob);
Damien Miller874d77b2000-10-14 16:23:11 +1100516#ifdef HAVE_CYGWIN
Damien Miller0dea79d2001-12-29 14:08:28 +1100517 if (check_nt_auth(0, authctxt->pw) == 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100518 return(0);
519#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000520 return authenticated;
521}
522
Ben Lindstrombba81212001-06-25 05:01:22 +0000523static int
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000524userauth_hostbased(Authctxt *authctxt)
525{
526 Buffer b;
Damien Miller9b74bfc2002-02-05 12:26:03 +1100527 Key *key = NULL;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000528 char *pkalg, *cuser, *chost, *service;
529 u_char *pkblob, *sig;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000530 u_int alen, blen, slen;
531 int pktype;
532 int authenticated = 0;
533
534 if (!authctxt->valid) {
535 debug2("userauth_hostbased: disabled because of invalid user");
536 return 0;
537 }
538 pkalg = packet_get_string(&alen);
539 pkblob = packet_get_string(&blen);
540 chost = packet_get_string(NULL);
541 cuser = packet_get_string(NULL);
542 sig = packet_get_string(&slen);
543
544 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
545 cuser, chost, pkalg, slen);
546#ifdef DEBUG_PK
547 debug("signature:");
548 buffer_init(&b);
549 buffer_append(&b, sig, slen);
550 buffer_dump(&b);
551 buffer_free(&b);
552#endif
553 pktype = key_type_from_name(pkalg);
554 if (pktype == KEY_UNSPEC) {
555 /* this is perfectly legal */
556 log("userauth_hostbased: unsupported "
557 "public key algorithm: %s", pkalg);
558 goto done;
559 }
560 key = key_from_blob(pkblob, blen);
561 if (key == NULL) {
Damien Miller9b74bfc2002-02-05 12:26:03 +1100562 error("userauth_hostbased: cannot decode key: %s", pkalg);
563 goto done;
564 }
565 if (key->type != pktype) {
566 error("userauth_hostbased: type mismatch for decoded key "
567 "(received %d, expected %d)", key->type, pktype);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000568 goto done;
569 }
Ben Lindstrom671388f2001-04-19 20:40:45 +0000570 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
571 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000572 buffer_init(&b);
Ben Lindstrom671388f2001-04-19 20:40:45 +0000573 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000574 /* reconstruct packet */
575 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
576 buffer_put_cstring(&b, authctxt->user);
Ben Lindstrom671388f2001-04-19 20:40:45 +0000577 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000578 buffer_put_cstring(&b, "hostbased");
579 buffer_put_string(&b, pkalg, alen);
580 buffer_put_string(&b, pkblob, blen);
581 buffer_put_cstring(&b, chost);
582 buffer_put_cstring(&b, cuser);
583#ifdef DEBUG_PK
584 buffer_dump(&b);
585#endif
586 /* test for allowed key and correct signature */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000587 authenticated = 0;
588 if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
589 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
590 buffer_len(&b))) == 1)
Damien Miller3a5b0232002-03-13 13:19:42 +1100591 authenticated = 1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000592
593 buffer_clear(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000594done:
595 debug2("userauth_hostbased: authenticated %d", authenticated);
Damien Miller9b74bfc2002-02-05 12:26:03 +1100596 if (key != NULL)
597 key_free(key);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000598 xfree(pkalg);
599 xfree(pkblob);
600 xfree(cuser);
601 xfree(chost);
602 xfree(sig);
603 return authenticated;
604}
605
Damien Miller874d77b2000-10-14 16:23:11 +1100606/* get current user */
Damien Millereba71ba2000-04-29 23:57:08 +1000607
608struct passwd*
609auth_get_user(void)
610{
Damien Miller874d77b2000-10-14 16:23:11 +1100611 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000612}
613
Damien Miller874d77b2000-10-14 16:23:11 +1100614#define DELIM ","
Damien Millereba71ba2000-04-29 23:57:08 +1000615
Ben Lindstrom79073822001-07-04 03:42:30 +0000616static char *
Damien Miller874d77b2000-10-14 16:23:11 +1100617authmethods_get(void)
618{
619 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +1100620 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100621 char *list;
622
Damien Miller0e3b8722002-01-22 23:26:38 +1100623 buffer_init(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100624 for (method = authmethods; method->name != NULL; method++) {
625 if (strcmp(method->name, "none") == 0)
626 continue;
627 if (method->enabled != NULL && *(method->enabled) != 0) {
Damien Miller0e3b8722002-01-22 23:26:38 +1100628 if (buffer_len(&b) > 0)
629 buffer_append(&b, ",", 1);
630 buffer_append(&b, method->name, strlen(method->name));
Damien Millereba71ba2000-04-29 23:57:08 +1000631 }
632 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100633 buffer_append(&b, "\0", 1);
634 list = xstrdup(buffer_ptr(&b));
635 buffer_free(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100636 return list;
637}
638
Ben Lindstrombba81212001-06-25 05:01:22 +0000639static Authmethod *
Damien Miller874d77b2000-10-14 16:23:11 +1100640authmethod_lookup(const char *name)
641{
642 Authmethod *method = NULL;
643 if (name != NULL)
644 for (method = authmethods; method->name != NULL; method++)
645 if (method->enabled != NULL &&
646 *(method->enabled) != 0 &&
647 strcmp(name, method->name) == 0)
648 return method;
649 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
650 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000651}
652
653/* return 1 if user allows given key */
Ben Lindstrombba81212001-06-25 05:01:22 +0000654static int
Ben Lindstromf96704d2001-06-25 04:17:12 +0000655user_key_allowed2(struct passwd *pw, Key *key, char *file)
Damien Millereba71ba2000-04-29 23:57:08 +1000656{
Ben Lindstromf96704d2001-06-25 04:17:12 +0000657 char line[8192];
Damien Millereba71ba2000-04-29 23:57:08 +1000658 int found_key = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000659 FILE *f;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000660 u_long linenum = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000661 struct stat st;
662 Key *found;
Damien Millerda9edca2001-12-21 12:48:54 +1100663 char *fp;
Damien Millereba71ba2000-04-29 23:57:08 +1000664
Damien Miller874d77b2000-10-14 16:23:11 +1100665 if (pw == NULL)
666 return 0;
667
Damien Millereba71ba2000-04-29 23:57:08 +1000668 /* Temporarily use the user's uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000669 temporarily_use_uid(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000670
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000671 debug("trying public key file %s", file);
Damien Millereba71ba2000-04-29 23:57:08 +1000672
673 /* Fail quietly if file does not exist */
674 if (stat(file, &st) < 0) {
675 /* Restore the privileged uid. */
676 restore_uid();
677 return 0;
678 }
679 /* Open the file containing the authorized keys. */
680 f = fopen(file, "r");
681 if (!f) {
682 /* Restore the privileged uid. */
683 restore_uid();
684 return 0;
685 }
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000686 if (options.strict_modes &&
Ben Lindstrom90279d82001-07-04 03:56:56 +0000687 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000688 fclose(f);
689 log("Authentication refused: %s", line);
690 restore_uid();
691 return 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000692 }
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000693
Damien Millereba71ba2000-04-29 23:57:08 +1000694 found_key = 0;
Damien Millerd3444942000-09-30 14:20:03 +1100695 found = key_new(key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000696
697 while (fgets(line, sizeof(line), f)) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000698 char *cp, *options = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000699 linenum++;
700 /* Skip leading whitespace, empty and comment lines. */
701 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
702 ;
703 if (!*cp || *cp == '\n' || *cp == '#')
704 continue;
Damien Millerf6d9e222000-06-18 14:50:44 +1000705
Ben Lindstrom1bc3bdb2001-09-20 23:11:26 +0000706 if (key_read(found, &cp) != 1) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000707 /* no key? check if there are options for this key */
708 int quoted = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100709 debug2("user_key_allowed: check options: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000710 options = cp;
711 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
712 if (*cp == '\\' && cp[1] == '"')
713 cp++; /* Skip both */
714 else if (*cp == '"')
715 quoted = !quoted;
716 }
717 /* Skip remaining whitespace. */
718 for (; *cp == ' ' || *cp == '\t'; cp++)
719 ;
Ben Lindstrom1bc3bdb2001-09-20 23:11:26 +0000720 if (key_read(found, &cp) != 1) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100721 debug2("user_key_allowed: advance: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000722 /* still no key? advance to next line*/
723 continue;
724 }
725 }
726 if (key_equal(found, key) &&
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000727 auth_parse_options(pw, options, file, linenum) == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000728 found_key = 1;
Ben Lindstrom940fb862001-08-06 21:01:49 +0000729 debug("matching key found: file %s, line %lu",
Damien Millereba71ba2000-04-29 23:57:08 +1000730 file, linenum);
Damien Millerda9edca2001-12-21 12:48:54 +1100731 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
732 verbose("Found matching %s key: %s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100733 key_type(found), fp);
Damien Millerda9edca2001-12-21 12:48:54 +1100734 xfree(fp);
Damien Millereba71ba2000-04-29 23:57:08 +1000735 break;
736 }
737 }
738 restore_uid();
739 fclose(f);
740 key_free(found);
Ben Lindstromb54873a2001-03-11 20:01:55 +0000741 if (!found_key)
742 debug2("key not found");
Damien Millereba71ba2000-04-29 23:57:08 +1000743 return found_key;
744}
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000745
Ben Lindstromf96704d2001-06-25 04:17:12 +0000746/* check whether given key is in .ssh/authorized_keys* */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000747int
Ben Lindstromf96704d2001-06-25 04:17:12 +0000748user_key_allowed(struct passwd *pw, Key *key)
749{
750 int success;
751 char *file;
752
753 file = authorized_keys_file(pw);
754 success = user_key_allowed2(pw, key, file);
755 xfree(file);
756 if (success)
757 return success;
758
759 /* try suffix "2" for backward compat, too */
760 file = authorized_keys_file2(pw);
761 success = user_key_allowed2(pw, key, file);
762 xfree(file);
763 return success;
764}
765
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000766/* return 1 if given hostkey is allowed */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000767int
Ben Lindstrom4aa603c2001-04-19 20:38:06 +0000768hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000769 Key *key)
770{
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000771 const char *resolvedname, *ipaddr, *lookup;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000772 HostStatus host_status;
773 int len;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000774
Damien Millerc5d86352002-02-05 12:13:41 +1100775 resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000776 ipaddr = get_remote_ipaddr();
777
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000778 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
779 chost, resolvedname, ipaddr);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000780
781 if (options.hostbased_uses_name_from_packet_only) {
782 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
783 return 0;
784 lookup = chost;
785 } else {
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000786 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
787 debug2("stripping trailing dot from chost %s", chost);
788 chost[len - 1] = '\0';
789 }
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000790 if (strcasecmp(resolvedname, chost) != 0)
791 log("userauth_hostbased mismatch: "
792 "client sends %s, but we resolve %s to %s",
793 chost, ipaddr, resolvedname);
794 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
795 return 0;
796 lookup = resolvedname;
797 }
798 debug2("userauth_hostbased: access allowed by auth_rhosts2");
799
Ben Lindstrom83647ce2001-06-25 04:30:16 +0000800 host_status = check_key_in_hostfiles(pw, key, lookup,
801 _PATH_SSH_SYSTEM_HOSTFILE,
Ben Lindstroma4789ef2001-06-25 04:40:49 +0000802 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000803
Ben Lindstrom83647ce2001-06-25 04:30:16 +0000804 /* backward compat if no key has been found. */
805 if (host_status == HOST_NEW)
806 host_status = check_key_in_hostfiles(pw, key, lookup,
807 _PATH_SSH_SYSTEM_HOSTFILE2,
Ben Lindstroma4789ef2001-06-25 04:40:49 +0000808 options.ignore_user_known_hosts ? NULL :
809 _PATH_SSH_USER_HOSTFILE2);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000810
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000811 return (host_status == HOST_OK);
812}