blob: 6bcc5652787fbac9e67e5205bee4291bb3331d3a [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 Lindstrom58d4daf2002-05-15 16:14:36 +000026RCSID("$OpenBSD: auth2.c,v 1.91 2002/05/13 02:37:39 itojun 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 Lindstrom5eabda32001-04-12 23:34:34 +000050#include "hostfile.h"
51#include "canohost.h"
Ben Lindstrom551ea372001-06-05 18:56:16 +000052#include "match.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000053#include "monitor_wrap.h"
Ben Lindstrom58d4daf2002-05-15 16:14:36 +000054#include "atomicio.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;
Damien Millerffc868f2002-05-09 15:59:13 +1000125 if (use_privsep)
126 options.pam_authentication_via_kbd_int = 0;
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000127
Damien Miller7d053392002-01-22 23:24:13 +1100128 dispatch_init(&dispatch_protocol_error);
Damien Millereba71ba2000-04-29 23:57:08 +1000129 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
Damien Miller874d77b2000-10-14 16:23:11 +1100130 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +0000131
132 return (authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000133}
134
Ben Lindstrombba81212001-06-25 05:01:22 +0000135static void
Damien Miller630d6f42002-01-22 23:17:30 +1100136input_service_request(int type, u_int32_t seq, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000137{
Damien Miller874d77b2000-10-14 16:23:11 +1100138 Authctxt *authctxt = ctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000139 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000140 int accept = 0;
141 char *service = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +1100142 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000143
Damien Miller874d77b2000-10-14 16:23:11 +1100144 if (authctxt == NULL)
145 fatal("input_service_request: no authctxt");
146
Damien Millereba71ba2000-04-29 23:57:08 +1000147 if (strcmp(service, "ssh-userauth") == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100148 if (!authctxt->success) {
Damien Millereba71ba2000-04-29 23:57:08 +1000149 accept = 1;
150 /* now we can handle user-auth requests */
151 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
152 }
153 }
154 /* XXX all other service requests are denied */
155
156 if (accept) {
157 packet_start(SSH2_MSG_SERVICE_ACCEPT);
158 packet_put_cstring(service);
159 packet_send();
160 packet_write_wait();
161 } else {
162 debug("bad service request %s", service);
163 packet_disconnect("bad service request %s", service);
164 }
165 xfree(service);
166}
167
Ben Lindstrombba81212001-06-25 05:01:22 +0000168static void
Damien Miller630d6f42002-01-22 23:17:30 +1100169input_userauth_request(int type, u_int32_t seq, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000170{
Damien Miller874d77b2000-10-14 16:23:11 +1100171 Authctxt *authctxt = ctxt;
172 Authmethod *m = NULL;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000173 char *user, *service, *method, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000174 int authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000175
Damien Miller874d77b2000-10-14 16:23:11 +1100176 if (authctxt == NULL)
177 fatal("input_userauth_request: no authctxt");
Damien Millereba71ba2000-04-29 23:57:08 +1000178
Damien Miller874d77b2000-10-14 16:23:11 +1100179 user = packet_get_string(NULL);
180 service = packet_get_string(NULL);
181 method = packet_get_string(NULL);
182 debug("userauth-request for user %s service %s method %s", user, service, method);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000183 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
Damien Miller874d77b2000-10-14 16:23:11 +1100184
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000185 if ((style = strchr(user, ':')) != NULL)
186 *style++ = 0;
187
Kevin Stevesef4eea92001-02-05 12:42:17 +0000188 if (authctxt->attempt++ == 0) {
Damien Miller3a5b0232002-03-13 13:19:42 +1100189 /* setup auth context */
Kevin Steves205cc1e2002-03-22 20:43:05 +0000190 authctxt->pw = PRIVSEP(getpwnamallow(user));
191 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100192 authctxt->valid = 1;
193 debug2("input_userauth_request: setting up authctxt for %s", user);
194#ifdef USE_PAM
Damien Miller79418552002-04-23 20:28:48 +1000195 PRIVSEP(start_pam(authctxt->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
Damien Miller79418552002-04-23 20:28:48 +1000200 PRIVSEP(start_pam("NOUSER"));
Damien Miller22e22bf2001-01-19 15:46:38 +1100201#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100202 }
Ben Lindstrom7ebb6352002-03-22 03:04:08 +0000203 setproctitle("%s%s", authctxt->pw ? user : "unknown",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000204 use_privsep ? " [net]" : "");
Damien Miller874d77b2000-10-14 16:23:11 +1100205 authctxt->user = xstrdup(user);
206 authctxt->service = xstrdup(service);
Ben Lindstrom9d0c0662001-06-09 01:40:00 +0000207 authctxt->style = style ? xstrdup(style) : NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000208 if (use_privsep)
209 mm_inform_authserv(service, style);
Ben Lindstrom9d0c0662001-06-09 01:40:00 +0000210 } else if (strcmp(user, authctxt->user) != 0 ||
211 strcmp(service, authctxt->service) != 0) {
212 packet_disconnect("Change of username or service not allowed: "
213 "(%s,%s) -> (%s,%s)",
214 authctxt->user, authctxt->service, user, service);
Damien Millereba71ba2000-04-29 23:57:08 +1000215 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000216 /* reset state */
Damien Milleree116252001-12-21 12:42:34 +1100217 auth2_challenge_stop(authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000218 authctxt->postponed = 0;
Damien Millerb70b61f2000-09-16 16:25:12 +1100219
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000220 /* try to authenticate user */
Damien Miller874d77b2000-10-14 16:23:11 +1100221 m = authmethod_lookup(method);
222 if (m != NULL) {
223 debug2("input_userauth_request: try method %s", method);
224 authenticated = m->userauth(authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100225 }
Damien Miller5d57e502001-03-30 10:48:31 +1000226 userauth_finish(authctxt, authenticated, method);
227
228 xfree(service);
229 xfree(user);
230 xfree(method);
231}
232
233void
234userauth_finish(Authctxt *authctxt, int authenticated, char *method)
235{
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000236 char *methods;
237
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000238 if (!authctxt->valid && authenticated)
239 fatal("INTERNAL ERROR: authenticated invalid user %s",
240 authctxt->user);
Damien Millerb70b61f2000-09-16 16:25:12 +1100241
Damien Miller874d77b2000-10-14 16:23:11 +1100242 /* Special handling for root */
Ben Lindstromd8a90212001-02-15 03:08:27 +0000243 if (authenticated && authctxt->pw->pw_uid == 0 &&
244 !auth_root_allowed(method))
Damien Millereba71ba2000-04-29 23:57:08 +1000245 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000246
247#ifdef USE_PAM
Damien Miller79418552002-04-23 20:28:48 +1000248 if (!use_privsep && authenticated && authctxt->user &&
249 !do_pam_account(authctxt->user, NULL))
Damien Millerb70b61f2000-09-16 16:25:12 +1100250 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000251#endif /* USE_PAM */
252
Damien Miller874d77b2000-10-14 16:23:11 +1100253 /* Log before sending the reply */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000254 auth_log(authctxt, authenticated, method, " ssh2");
255
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000256 if (authctxt->postponed)
257 return;
258
259 /* XXX todo: check if multiple auth methods are needed */
260 if (authenticated == 1) {
261 /* turn off userauth */
Damien Miller7d053392002-01-22 23:24:13 +1100262 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000263 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
264 packet_send();
265 packet_write_wait();
266 /* now we can break out */
267 authctxt->success = 1;
268 } else {
Damien Millere49d0962001-11-13 23:46:18 +1100269 if (authctxt->failures++ > AUTH_FAIL_MAX) {
270#ifdef WITH_AIXAUTHENTICATE
271 loginfailed(authctxt->user,
Damien Millerf3451a22002-02-05 12:40:46 +1100272 get_canonical_hostname(options.verify_reverse_mapping),
Damien Millere49d0962001-11-13 23:46:18 +1100273 "ssh");
274#endif /* WITH_AIXAUTHENTICATE */
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000275 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Damien Millere49d0962001-11-13 23:46:18 +1100276 }
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000277 methods = authmethods_get();
278 packet_start(SSH2_MSG_USERAUTH_FAILURE);
279 packet_put_cstring(methods);
280 packet_put_char(0); /* XXX partial success, unused */
281 packet_send();
282 packet_write_wait();
283 xfree(methods);
284 }
Damien Miller874d77b2000-10-14 16:23:11 +1100285}
286
Damien Miller5ad9fd92002-05-13 11:07:41 +1000287char *
288auth2_read_banner(void)
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000289{
290 struct stat st;
291 char *banner = NULL;
292 off_t len, n;
293 int fd;
294
Damien Miller5ad9fd92002-05-13 11:07:41 +1000295 if ((fd = open(options.banner, O_RDONLY)) == -1)
296 return (NULL);
297 if (fstat(fd, &st) == -1) {
298 close(fd);
299 return (NULL);
300 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000301 len = st.st_size;
302 banner = xmalloc(len + 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000303 n = atomicio(read, fd, banner, len);
304 close(fd);
305
306 if (n != len) {
307 free(banner);
308 return (NULL);
309 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000310 banner[n] = '\0';
Damien Miller5ad9fd92002-05-13 11:07:41 +1000311
312 return (banner);
313}
314
315static void
316userauth_banner(void)
317{
318 char *banner = NULL;
319
320 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
321 return;
322
323 if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
324 goto done;
325
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000326 packet_start(SSH2_MSG_USERAUTH_BANNER);
327 packet_put_cstring(banner);
328 packet_put_cstring(""); /* language, unused */
329 packet_send();
330 debug("userauth_banner: sent");
331done:
332 if (banner)
333 xfree(banner);
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000334 return;
335}
Damien Miller874d77b2000-10-14 16:23:11 +1100336
Ben Lindstrombba81212001-06-25 05:01:22 +0000337static int
Damien Miller874d77b2000-10-14 16:23:11 +1100338userauth_none(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000339{
Damien Miller874d77b2000-10-14 16:23:11 +1100340 /* disable method "none", only allowed one time */
341 Authmethod *m = authmethod_lookup("none");
342 if (m != NULL)
343 m->enabled = NULL;
Damien Miller48b03fc2002-01-22 23:11:40 +1100344 packet_check_eom();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000345 userauth_banner();
Damien Millerb8c656e2000-06-28 15:22:41 +1000346
Damien Miller874d77b2000-10-14 16:23:11 +1100347 if (authctxt->valid == 0)
348 return(0);
Damien Miller3a5b0232002-03-13 13:19:42 +1100349
350#ifdef HAVE_CYGWIN
351 if (check_nt_auth(1, authctxt->pw) == 0)
352 return(0);
Damien Miller874d77b2000-10-14 16:23:11 +1100353#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000354 return PRIVSEP(auth_password(authctxt, ""));
Damien Millereba71ba2000-04-29 23:57:08 +1000355}
Damien Miller874d77b2000-10-14 16:23:11 +1100356
Ben Lindstrombba81212001-06-25 05:01:22 +0000357static int
Damien Miller874d77b2000-10-14 16:23:11 +1100358userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000359{
360 char *password;
361 int authenticated = 0;
362 int change;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000363 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000364 change = packet_get_char();
365 if (change)
366 log("password change not supported");
367 password = packet_get_string(&len);
Damien Miller48b03fc2002-01-22 23:11:40 +1100368 packet_check_eom();
Damien Miller3a5b0232002-03-13 13:19:42 +1100369 if (authctxt->valid &&
370#ifdef HAVE_CYGWIN
371 check_nt_auth(1, authctxt->pw) &&
Damien Miller874d77b2000-10-14 16:23:11 +1100372#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000373 PRIVSEP(auth_password(authctxt, password)) == 1)
Damien Miller3a5b0232002-03-13 13:19:42 +1100374 authenticated = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000375 memset(password, 0, len);
376 xfree(password);
377 return authenticated;
378}
Damien Miller874d77b2000-10-14 16:23:11 +1100379
Ben Lindstrombba81212001-06-25 05:01:22 +0000380static int
Damien Miller874d77b2000-10-14 16:23:11 +1100381userauth_kbdint(Authctxt *authctxt)
382{
383 int authenticated = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000384 char *lang, *devs;
Damien Miller874d77b2000-10-14 16:23:11 +1100385
386 lang = packet_get_string(NULL);
387 devs = packet_get_string(NULL);
Damien Miller48b03fc2002-01-22 23:11:40 +1100388 packet_check_eom();
Damien Miller874d77b2000-10-14 16:23:11 +1100389
Ben Lindstrom551ea372001-06-05 18:56:16 +0000390 debug("keyboard-interactive devs %s", devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000391
Ben Lindstrom551ea372001-06-05 18:56:16 +0000392 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000393 authenticated = auth2_challenge(authctxt, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000394
Damien Millerb8481582000-12-03 11:51:51 +1100395#ifdef USE_PAM
Damien Millerf8154422001-04-25 22:44:14 +1000396 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
Damien Millerb8481582000-12-03 11:51:51 +1100397 authenticated = auth2_pam(authctxt);
398#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100399 xfree(devs);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000400 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +1100401#ifdef HAVE_CYGWIN
Damien Miller0dea79d2001-12-29 14:08:28 +1100402 if (check_nt_auth(0, authctxt->pw) == 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100403 return(0);
404#endif
405 return authenticated;
406}
407
Ben Lindstrombba81212001-06-25 05:01:22 +0000408static int
Damien Miller874d77b2000-10-14 16:23:11 +1100409userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000410{
411 Buffer b;
Damien Miller9b74bfc2002-02-05 12:26:03 +1100412 Key *key = NULL;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000413 char *pkalg;
414 u_char *pkblob, *sig;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000415 u_int alen, blen, slen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100416 int have_sig, pktype;
Damien Millereba71ba2000-04-29 23:57:08 +1000417 int authenticated = 0;
418
Damien Miller874d77b2000-10-14 16:23:11 +1100419 if (!authctxt->valid) {
420 debug2("userauth_pubkey: disabled because of invalid user");
Damien Millereba71ba2000-04-29 23:57:08 +1000421 return 0;
422 }
423 have_sig = packet_get_char();
Ben Lindstromd121f612000-12-03 17:00:47 +0000424 if (datafellows & SSH_BUG_PKAUTH) {
425 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
426 /* no explicit pkalg given */
427 pkblob = packet_get_string(&blen);
428 buffer_init(&b);
429 buffer_append(&b, pkblob, blen);
430 /* so we have to extract the pkalg from the pkblob */
431 pkalg = buffer_get_string(&b, &alen);
432 buffer_free(&b);
433 } else {
434 pkalg = packet_get_string(&alen);
435 pkblob = packet_get_string(&blen);
436 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100437 pktype = key_type_from_name(pkalg);
438 if (pktype == KEY_UNSPEC) {
Ben Lindstromd121f612000-12-03 17:00:47 +0000439 /* this is perfectly legal */
Damien Miller9b74bfc2002-02-05 12:26:03 +1100440 log("userauth_pubkey: unsupported public key algorithm: %s",
441 pkalg);
442 goto done;
Damien Millereba71ba2000-04-29 23:57:08 +1000443 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100444 key = key_from_blob(pkblob, blen);
Damien Miller9b74bfc2002-02-05 12:26:03 +1100445 if (key == NULL) {
446 error("userauth_pubkey: cannot decode key: %s", pkalg);
447 goto done;
Damien Millereba71ba2000-04-29 23:57:08 +1000448 }
Damien Miller9b74bfc2002-02-05 12:26:03 +1100449 if (key->type != pktype) {
450 error("userauth_pubkey: type mismatch for decoded key "
451 "(received %d, expected %d)", key->type, pktype);
452 goto done;
453 }
454 if (have_sig) {
455 sig = packet_get_string(&slen);
456 packet_check_eom();
457 buffer_init(&b);
458 if (datafellows & SSH_OLD_SESSIONID) {
459 buffer_append(&b, session_id2, session_id2_len);
460 } else {
461 buffer_put_string(&b, session_id2, session_id2_len);
462 }
463 /* reconstruct packet */
464 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
465 buffer_put_cstring(&b, authctxt->user);
466 buffer_put_cstring(&b,
467 datafellows & SSH_BUG_PKSERVICE ?
468 "ssh-userauth" :
469 authctxt->service);
470 if (datafellows & SSH_BUG_PKAUTH) {
471 buffer_put_char(&b, have_sig);
472 } else {
473 buffer_put_cstring(&b, "publickey");
474 buffer_put_char(&b, have_sig);
475 buffer_put_cstring(&b, pkalg);
476 }
477 buffer_put_string(&b, pkblob, blen);
478#ifdef DEBUG_PK
479 buffer_dump(&b);
480#endif
481 /* test for correct signature */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000482 authenticated = 0;
483 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
484 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
485 buffer_len(&b))) == 1)
Damien Miller3a5b0232002-03-13 13:19:42 +1100486 authenticated = 1;
Damien Miller9b74bfc2002-02-05 12:26:03 +1100487 buffer_clear(&b);
488 xfree(sig);
489 } else {
490 debug("test whether pkalg/pkblob are acceptable");
491 packet_check_eom();
492
493 /* XXX fake reply and always send PK_OK ? */
494 /*
495 * XXX this allows testing whether a user is allowed
496 * to login: if you happen to have a valid pubkey this
497 * message is sent. the message is NEVER sent at all
498 * if a user is not allowed to login. is this an
499 * issue? -markus
500 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000501 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
Damien Miller9b74bfc2002-02-05 12:26:03 +1100502 packet_start(SSH2_MSG_USERAUTH_PK_OK);
503 packet_put_string(pkalg, alen);
504 packet_put_string(pkblob, blen);
505 packet_send();
506 packet_write_wait();
507 authctxt->postponed = 1;
508 }
509 }
510 if (authenticated != 1)
511 auth_clear_options();
512done:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100513 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
Damien Miller9b74bfc2002-02-05 12:26:03 +1100514 if (key != NULL)
515 key_free(key);
Damien Millereba71ba2000-04-29 23:57:08 +1000516 xfree(pkalg);
517 xfree(pkblob);
Damien Miller874d77b2000-10-14 16:23:11 +1100518#ifdef HAVE_CYGWIN
Damien Miller0dea79d2001-12-29 14:08:28 +1100519 if (check_nt_auth(0, authctxt->pw) == 0)
Damien Miller874d77b2000-10-14 16:23:11 +1100520 return(0);
521#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000522 return authenticated;
523}
524
Ben Lindstrombba81212001-06-25 05:01:22 +0000525static int
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000526userauth_hostbased(Authctxt *authctxt)
527{
528 Buffer b;
Damien Miller9b74bfc2002-02-05 12:26:03 +1100529 Key *key = NULL;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000530 char *pkalg, *cuser, *chost, *service;
531 u_char *pkblob, *sig;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000532 u_int alen, blen, slen;
533 int pktype;
534 int authenticated = 0;
535
536 if (!authctxt->valid) {
537 debug2("userauth_hostbased: disabled because of invalid user");
538 return 0;
539 }
540 pkalg = packet_get_string(&alen);
541 pkblob = packet_get_string(&blen);
542 chost = packet_get_string(NULL);
543 cuser = packet_get_string(NULL);
544 sig = packet_get_string(&slen);
545
546 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
547 cuser, chost, pkalg, slen);
548#ifdef DEBUG_PK
549 debug("signature:");
550 buffer_init(&b);
551 buffer_append(&b, sig, slen);
552 buffer_dump(&b);
553 buffer_free(&b);
554#endif
555 pktype = key_type_from_name(pkalg);
556 if (pktype == KEY_UNSPEC) {
557 /* this is perfectly legal */
558 log("userauth_hostbased: unsupported "
559 "public key algorithm: %s", pkalg);
560 goto done;
561 }
562 key = key_from_blob(pkblob, blen);
563 if (key == NULL) {
Damien Miller9b74bfc2002-02-05 12:26:03 +1100564 error("userauth_hostbased: cannot decode key: %s", pkalg);
565 goto done;
566 }
567 if (key->type != pktype) {
568 error("userauth_hostbased: type mismatch for decoded key "
569 "(received %d, expected %d)", key->type, pktype);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000570 goto done;
571 }
Ben Lindstrom671388f2001-04-19 20:40:45 +0000572 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
573 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000574 buffer_init(&b);
Ben Lindstrom671388f2001-04-19 20:40:45 +0000575 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000576 /* reconstruct packet */
577 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
578 buffer_put_cstring(&b, authctxt->user);
Ben Lindstrom671388f2001-04-19 20:40:45 +0000579 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000580 buffer_put_cstring(&b, "hostbased");
581 buffer_put_string(&b, pkalg, alen);
582 buffer_put_string(&b, pkblob, blen);
583 buffer_put_cstring(&b, chost);
584 buffer_put_cstring(&b, cuser);
585#ifdef DEBUG_PK
586 buffer_dump(&b);
587#endif
588 /* test for allowed key and correct signature */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000589 authenticated = 0;
590 if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
591 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
592 buffer_len(&b))) == 1)
Damien Miller3a5b0232002-03-13 13:19:42 +1100593 authenticated = 1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000594
595 buffer_clear(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000596done:
597 debug2("userauth_hostbased: authenticated %d", authenticated);
Damien Miller9b74bfc2002-02-05 12:26:03 +1100598 if (key != NULL)
599 key_free(key);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000600 xfree(pkalg);
601 xfree(pkblob);
602 xfree(cuser);
603 xfree(chost);
604 xfree(sig);
605 return authenticated;
606}
607
Damien Miller874d77b2000-10-14 16:23:11 +1100608/* get current user */
Damien Millereba71ba2000-04-29 23:57:08 +1000609
610struct passwd*
611auth_get_user(void)
612{
Damien Miller874d77b2000-10-14 16:23:11 +1100613 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000614}
615
Damien Miller874d77b2000-10-14 16:23:11 +1100616#define DELIM ","
Damien Millereba71ba2000-04-29 23:57:08 +1000617
Ben Lindstrom79073822001-07-04 03:42:30 +0000618static char *
Damien Miller874d77b2000-10-14 16:23:11 +1100619authmethods_get(void)
620{
621 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +1100622 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100623 char *list;
624
Damien Miller0e3b8722002-01-22 23:26:38 +1100625 buffer_init(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100626 for (method = authmethods; method->name != NULL; method++) {
627 if (strcmp(method->name, "none") == 0)
628 continue;
629 if (method->enabled != NULL && *(method->enabled) != 0) {
Damien Miller0e3b8722002-01-22 23:26:38 +1100630 if (buffer_len(&b) > 0)
631 buffer_append(&b, ",", 1);
632 buffer_append(&b, method->name, strlen(method->name));
Damien Millereba71ba2000-04-29 23:57:08 +1000633 }
634 }
Damien Miller0e3b8722002-01-22 23:26:38 +1100635 buffer_append(&b, "\0", 1);
636 list = xstrdup(buffer_ptr(&b));
637 buffer_free(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100638 return list;
639}
640
Ben Lindstrombba81212001-06-25 05:01:22 +0000641static Authmethod *
Damien Miller874d77b2000-10-14 16:23:11 +1100642authmethod_lookup(const char *name)
643{
644 Authmethod *method = NULL;
645 if (name != NULL)
646 for (method = authmethods; method->name != NULL; method++)
647 if (method->enabled != NULL &&
648 *(method->enabled) != 0 &&
649 strcmp(name, method->name) == 0)
650 return method;
651 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
652 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000653}
654
655/* return 1 if user allows given key */
Ben Lindstrombba81212001-06-25 05:01:22 +0000656static int
Ben Lindstromf96704d2001-06-25 04:17:12 +0000657user_key_allowed2(struct passwd *pw, Key *key, char *file)
Damien Millereba71ba2000-04-29 23:57:08 +1000658{
Ben Lindstromf96704d2001-06-25 04:17:12 +0000659 char line[8192];
Damien Millereba71ba2000-04-29 23:57:08 +1000660 int found_key = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000661 FILE *f;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000662 u_long linenum = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000663 struct stat st;
664 Key *found;
Damien Millerda9edca2001-12-21 12:48:54 +1100665 char *fp;
Damien Millereba71ba2000-04-29 23:57:08 +1000666
Damien Miller874d77b2000-10-14 16:23:11 +1100667 if (pw == NULL)
668 return 0;
669
Damien Millereba71ba2000-04-29 23:57:08 +1000670 /* Temporarily use the user's uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000671 temporarily_use_uid(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000672
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000673 debug("trying public key file %s", file);
Damien Millereba71ba2000-04-29 23:57:08 +1000674
675 /* Fail quietly if file does not exist */
676 if (stat(file, &st) < 0) {
677 /* Restore the privileged uid. */
678 restore_uid();
679 return 0;
680 }
681 /* Open the file containing the authorized keys. */
682 f = fopen(file, "r");
683 if (!f) {
684 /* Restore the privileged uid. */
685 restore_uid();
686 return 0;
687 }
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000688 if (options.strict_modes &&
Ben Lindstrom90279d82001-07-04 03:56:56 +0000689 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000690 fclose(f);
691 log("Authentication refused: %s", line);
692 restore_uid();
693 return 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000694 }
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000695
Damien Millereba71ba2000-04-29 23:57:08 +1000696 found_key = 0;
Damien Millerd3444942000-09-30 14:20:03 +1100697 found = key_new(key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000698
699 while (fgets(line, sizeof(line), f)) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000700 char *cp, *options = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000701 linenum++;
702 /* Skip leading whitespace, empty and comment lines. */
703 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
704 ;
705 if (!*cp || *cp == '\n' || *cp == '#')
706 continue;
Damien Millerf6d9e222000-06-18 14:50:44 +1000707
Ben Lindstrom1bc3bdb2001-09-20 23:11:26 +0000708 if (key_read(found, &cp) != 1) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000709 /* no key? check if there are options for this key */
710 int quoted = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100711 debug2("user_key_allowed: check options: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000712 options = cp;
713 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
714 if (*cp == '\\' && cp[1] == '"')
715 cp++; /* Skip both */
716 else if (*cp == '"')
717 quoted = !quoted;
718 }
719 /* Skip remaining whitespace. */
720 for (; *cp == ' ' || *cp == '\t'; cp++)
721 ;
Ben Lindstrom1bc3bdb2001-09-20 23:11:26 +0000722 if (key_read(found, &cp) != 1) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100723 debug2("user_key_allowed: advance: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000724 /* still no key? advance to next line*/
725 continue;
726 }
727 }
728 if (key_equal(found, key) &&
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000729 auth_parse_options(pw, options, file, linenum) == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000730 found_key = 1;
Ben Lindstrom940fb862001-08-06 21:01:49 +0000731 debug("matching key found: file %s, line %lu",
Damien Millereba71ba2000-04-29 23:57:08 +1000732 file, linenum);
Damien Millerda9edca2001-12-21 12:48:54 +1100733 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
734 verbose("Found matching %s key: %s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100735 key_type(found), fp);
Damien Millerda9edca2001-12-21 12:48:54 +1100736 xfree(fp);
Damien Millereba71ba2000-04-29 23:57:08 +1000737 break;
738 }
739 }
740 restore_uid();
741 fclose(f);
742 key_free(found);
Ben Lindstromb54873a2001-03-11 20:01:55 +0000743 if (!found_key)
744 debug2("key not found");
Damien Millereba71ba2000-04-29 23:57:08 +1000745 return found_key;
746}
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000747
Ben Lindstromf96704d2001-06-25 04:17:12 +0000748/* check whether given key is in .ssh/authorized_keys* */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000749int
Ben Lindstromf96704d2001-06-25 04:17:12 +0000750user_key_allowed(struct passwd *pw, Key *key)
751{
752 int success;
753 char *file;
754
755 file = authorized_keys_file(pw);
756 success = user_key_allowed2(pw, key, file);
757 xfree(file);
758 if (success)
759 return success;
760
761 /* try suffix "2" for backward compat, too */
762 file = authorized_keys_file2(pw);
763 success = user_key_allowed2(pw, key, file);
764 xfree(file);
765 return success;
766}
767
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000768/* return 1 if given hostkey is allowed */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000769int
Ben Lindstrom4aa603c2001-04-19 20:38:06 +0000770hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000771 Key *key)
772{
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000773 const char *resolvedname, *ipaddr, *lookup;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000774 HostStatus host_status;
775 int len;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000776
Damien Millerc5d86352002-02-05 12:13:41 +1100777 resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000778 ipaddr = get_remote_ipaddr();
779
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000780 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
781 chost, resolvedname, ipaddr);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000782
783 if (options.hostbased_uses_name_from_packet_only) {
784 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
785 return 0;
786 lookup = chost;
787 } else {
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000788 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
789 debug2("stripping trailing dot from chost %s", chost);
790 chost[len - 1] = '\0';
791 }
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000792 if (strcasecmp(resolvedname, chost) != 0)
793 log("userauth_hostbased mismatch: "
794 "client sends %s, but we resolve %s to %s",
795 chost, ipaddr, resolvedname);
796 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
797 return 0;
798 lookup = resolvedname;
799 }
800 debug2("userauth_hostbased: access allowed by auth_rhosts2");
801
Ben Lindstrom83647ce2001-06-25 04:30:16 +0000802 host_status = check_key_in_hostfiles(pw, key, lookup,
803 _PATH_SSH_SYSTEM_HOSTFILE,
Ben Lindstroma4789ef2001-06-25 04:40:49 +0000804 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000805
Ben Lindstrom83647ce2001-06-25 04:30:16 +0000806 /* backward compat if no key has been found. */
807 if (host_status == HOST_NEW)
808 host_status = check_key_in_hostfiles(pw, key, lookup,
809 _PATH_SSH_SYSTEM_HOSTFILE2,
Ben Lindstroma4789ef2001-06-25 04:40:49 +0000810 options.ignore_user_known_hosts ? NULL :
811 _PATH_SSH_USER_HOSTFILE2);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000812
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000813 return (host_status == HOST_OK);
814}