blob: 1920eb32e0ad9e14e842201348681ec6d8efaabb [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 Miller6fd5b392001-11-12 11:04:28 +110026RCSID("$OpenBSD: auth2.c,v 1.72 2001/11/07 22:41:51 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
Ben Lindstrombba81212001-06-25 05:01:22 +000072static void input_service_request(int, int, void *);
73static void input_userauth_request(int, int, void *);
74static void protocol_error(int, int, 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 Lindstrombba81212001-06-25 05:01:22 +000079static int user_key_allowed(struct passwd *, Key *);
80static int 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
113void
114do_authentication2()
115{
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 Millereba71ba2000-04-29 23:57:08 +1000126 dispatch_init(&protocol_error);
127 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 Lindstromb31783d2001-03-22 02:02:12 +0000129 do_authenticated(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000130}
131
Ben Lindstrombba81212001-06-25 05:01:22 +0000132static void
Damien Miller62cee002000-09-23 17:15:56 +1100133protocol_error(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000134{
135 log("auth: protocol error: type %d plen %d", type, plen);
136 packet_start(SSH2_MSG_UNIMPLEMENTED);
137 packet_put_int(0);
138 packet_send();
139 packet_write_wait();
140}
141
Ben Lindstrombba81212001-06-25 05:01:22 +0000142static void
Damien Miller62cee002000-09-23 17:15:56 +1100143input_service_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000144{
Damien Miller874d77b2000-10-14 16:23:11 +1100145 Authctxt *authctxt = ctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000146 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000147 int accept = 0;
148 char *service = packet_get_string(&len);
149 packet_done();
150
Damien Miller874d77b2000-10-14 16:23:11 +1100151 if (authctxt == NULL)
152 fatal("input_service_request: no authctxt");
153
Damien Millereba71ba2000-04-29 23:57:08 +1000154 if (strcmp(service, "ssh-userauth") == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100155 if (!authctxt->success) {
Damien Millereba71ba2000-04-29 23:57:08 +1000156 accept = 1;
157 /* now we can handle user-auth requests */
158 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
159 }
160 }
161 /* XXX all other service requests are denied */
162
163 if (accept) {
164 packet_start(SSH2_MSG_SERVICE_ACCEPT);
165 packet_put_cstring(service);
166 packet_send();
167 packet_write_wait();
168 } else {
169 debug("bad service request %s", service);
170 packet_disconnect("bad service request %s", service);
171 }
172 xfree(service);
173}
174
Ben Lindstrombba81212001-06-25 05:01:22 +0000175static void
Damien Miller62cee002000-09-23 17:15:56 +1100176input_userauth_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000177{
Damien Miller874d77b2000-10-14 16:23:11 +1100178 Authctxt *authctxt = ctxt;
179 Authmethod *m = NULL;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000180 char *user, *service, *method, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000181 int authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000182
Damien Miller874d77b2000-10-14 16:23:11 +1100183 if (authctxt == NULL)
184 fatal("input_userauth_request: no authctxt");
Damien Millereba71ba2000-04-29 23:57:08 +1000185
Damien Miller874d77b2000-10-14 16:23:11 +1100186 user = packet_get_string(NULL);
187 service = packet_get_string(NULL);
188 method = packet_get_string(NULL);
189 debug("userauth-request for user %s service %s method %s", user, service, method);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000190 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
Damien Miller874d77b2000-10-14 16:23:11 +1100191
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000192 if ((style = strchr(user, ':')) != NULL)
193 *style++ = 0;
194
Kevin Stevesef4eea92001-02-05 12:42:17 +0000195 if (authctxt->attempt++ == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100196 /* setup auth context */
197 struct passwd *pw = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100198 pw = getpwnam(user);
199 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
200 authctxt->pw = pwcopy(pw);
201 authctxt->valid = 1;
202 debug2("input_userauth_request: setting up authctxt for %s", user);
203#ifdef USE_PAM
Damien Miller22e22bf2001-01-19 15:46:38 +1100204 start_pam(pw->pw_name);
Damien Miller874d77b2000-10-14 16:23:11 +1100205#endif
206 } else {
207 log("input_userauth_request: illegal user %s", user);
Damien Miller22e22bf2001-01-19 15:46:38 +1100208#ifdef USE_PAM
209 start_pam("NOUSER");
210#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100211 }
Ben Lindstromc1ba31f2001-02-15 03:14:11 +0000212 setproctitle("%s", pw ? user : "unknown");
Damien Miller874d77b2000-10-14 16:23:11 +1100213 authctxt->user = xstrdup(user);
214 authctxt->service = xstrdup(service);
Ben Lindstrom9d0c0662001-06-09 01:40:00 +0000215 authctxt->style = style ? xstrdup(style) : NULL;
216 } else if (strcmp(user, authctxt->user) != 0 ||
217 strcmp(service, authctxt->service) != 0) {
218 packet_disconnect("Change of username or service not allowed: "
219 "(%s,%s) -> (%s,%s)",
220 authctxt->user, authctxt->service, user, service);
Damien Millereba71ba2000-04-29 23:57:08 +1000221 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000222 /* reset state */
223 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
224 authctxt->postponed = 0;
Damien Miller60396b02001-02-18 17:01:00 +1100225#ifdef BSD_AUTH
226 if (authctxt->as) {
227 auth_close(authctxt->as);
228 authctxt->as = NULL;
229 }
230#endif
Damien Millerb70b61f2000-09-16 16:25:12 +1100231
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000232 /* try to authenticate user */
Damien Miller874d77b2000-10-14 16:23:11 +1100233 m = authmethod_lookup(method);
234 if (m != NULL) {
235 debug2("input_userauth_request: try method %s", method);
236 authenticated = m->userauth(authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100237 }
Damien Miller5d57e502001-03-30 10:48:31 +1000238 userauth_finish(authctxt, authenticated, method);
239
240 xfree(service);
241 xfree(user);
242 xfree(method);
243}
244
245void
246userauth_finish(Authctxt *authctxt, int authenticated, char *method)
247{
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000248 char *methods;
249
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000250 if (!authctxt->valid && authenticated)
251 fatal("INTERNAL ERROR: authenticated invalid user %s",
252 authctxt->user);
Damien Millerb70b61f2000-09-16 16:25:12 +1100253
Damien Miller874d77b2000-10-14 16:23:11 +1100254 /* Special handling for root */
Ben Lindstromd8a90212001-02-15 03:08:27 +0000255 if (authenticated && authctxt->pw->pw_uid == 0 &&
256 !auth_root_allowed(method))
Damien Millereba71ba2000-04-29 23:57:08 +1000257 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000258
259#ifdef USE_PAM
Damien Millerc83de6d2001-02-16 14:17:59 +1100260 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
261 NULL))
Damien Millerb70b61f2000-09-16 16:25:12 +1100262 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000263#endif /* USE_PAM */
264
Damien Miller874d77b2000-10-14 16:23:11 +1100265 /* Log before sending the reply */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000266 auth_log(authctxt, authenticated, method, " ssh2");
267
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000268 if (authctxt->postponed)
269 return;
270
271 /* XXX todo: check if multiple auth methods are needed */
272 if (authenticated == 1) {
273 /* turn off userauth */
274 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
275 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
276 packet_send();
277 packet_write_wait();
278 /* now we can break out */
279 authctxt->success = 1;
280 } else {
Damien Millere49d0962001-11-13 23:46:18 +1100281 if (authctxt->failures++ > AUTH_FAIL_MAX) {
282#ifdef WITH_AIXAUTHENTICATE
283 loginfailed(authctxt->user,
284 get_canonical_hostname(options.reverse_mapping_check),
285 "ssh");
286#endif /* WITH_AIXAUTHENTICATE */
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000287 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Damien Millere49d0962001-11-13 23:46:18 +1100288 }
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000289 methods = authmethods_get();
290 packet_start(SSH2_MSG_USERAUTH_FAILURE);
291 packet_put_cstring(methods);
292 packet_put_char(0); /* XXX partial success, unused */
293 packet_send();
294 packet_write_wait();
295 xfree(methods);
296 }
Damien Miller874d77b2000-10-14 16:23:11 +1100297}
298
Ben Lindstrombba81212001-06-25 05:01:22 +0000299static void
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000300userauth_banner(void)
301{
302 struct stat st;
303 char *banner = NULL;
304 off_t len, n;
305 int fd;
306
307 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
308 return;
Ben Lindstrom0cae0402001-04-04 23:47:52 +0000309 if ((fd = open(options.banner, O_RDONLY)) < 0)
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000310 return;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000311 if (fstat(fd, &st) < 0)
312 goto done;
313 len = st.st_size;
314 banner = xmalloc(len + 1);
315 if ((n = read(fd, banner, len)) < 0)
316 goto done;
317 banner[n] = '\0';
318 packet_start(SSH2_MSG_USERAUTH_BANNER);
319 packet_put_cstring(banner);
320 packet_put_cstring(""); /* language, unused */
321 packet_send();
322 debug("userauth_banner: sent");
323done:
324 if (banner)
325 xfree(banner);
326 close(fd);
327 return;
328}
Damien Miller874d77b2000-10-14 16:23:11 +1100329
Ben Lindstrombba81212001-06-25 05:01:22 +0000330static int
Damien Miller874d77b2000-10-14 16:23:11 +1100331userauth_none(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000332{
Damien Miller874d77b2000-10-14 16:23:11 +1100333 /* disable method "none", only allowed one time */
334 Authmethod *m = authmethod_lookup("none");
335 if (m != NULL)
336 m->enabled = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000337 packet_done();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000338 userauth_banner();
Damien Millerb8c656e2000-06-28 15:22:41 +1000339
Damien Miller874d77b2000-10-14 16:23:11 +1100340 if (authctxt->valid == 0)
341 return(0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000342
Damien Miller874d77b2000-10-14 16:23:11 +1100343#ifdef HAVE_CYGWIN
344 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
345 return(0);
346#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000347#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100348 return auth_pam_password(authctxt->pw, "");
Damien Millerb8c656e2000-06-28 15:22:41 +1000349#elif defined(HAVE_OSF_SIA)
Damien Miller92ddb7d2001-02-14 01:25:23 +1100350 return 0;
Damien Millerb8c656e2000-06-28 15:22:41 +1000351#else /* !HAVE_OSF_SIA && !USE_PAM */
Damien Miller60396b02001-02-18 17:01:00 +1100352 return auth_password(authctxt, "");
Damien Millereba71ba2000-04-29 23:57:08 +1000353#endif /* USE_PAM */
354}
Damien Miller874d77b2000-10-14 16:23:11 +1100355
Ben Lindstrombba81212001-06-25 05:01:22 +0000356static int
Damien Miller874d77b2000-10-14 16:23:11 +1100357userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000358{
359 char *password;
360 int authenticated = 0;
361 int change;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000362 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000363 change = packet_get_char();
364 if (change)
365 log("password change not supported");
366 password = packet_get_string(&len);
367 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100368 if (authctxt->valid &&
369#ifdef HAVE_CYGWIN
370 check_nt_auth(1, authctxt->pw->pw_uid) &&
371#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000372#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100373 auth_pam_password(authctxt->pw, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000374#elif defined(HAVE_OSF_SIA)
Damien Miller92ddb7d2001-02-14 01:25:23 +1100375 auth_sia_password(authctxt->user, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000376#else /* !USE_PAM && !HAVE_OSF_SIA */
Damien Miller60396b02001-02-18 17:01:00 +1100377 auth_password(authctxt, password) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000378#endif /* USE_PAM */
379 authenticated = 1;
380 memset(password, 0, len);
381 xfree(password);
382 return authenticated;
383}
Damien Miller874d77b2000-10-14 16:23:11 +1100384
Ben Lindstrombba81212001-06-25 05:01:22 +0000385static int
Damien Miller874d77b2000-10-14 16:23:11 +1100386userauth_kbdint(Authctxt *authctxt)
387{
388 int authenticated = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000389 char *lang, *devs;
Damien Miller874d77b2000-10-14 16:23:11 +1100390
391 lang = packet_get_string(NULL);
392 devs = packet_get_string(NULL);
393 packet_done();
394
Ben Lindstrom551ea372001-06-05 18:56:16 +0000395 debug("keyboard-interactive devs %s", devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000396
Ben Lindstrom551ea372001-06-05 18:56:16 +0000397 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000398 authenticated = auth2_challenge(authctxt, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000399
Damien Millerb8481582000-12-03 11:51:51 +1100400#ifdef USE_PAM
Damien Millerf8154422001-04-25 22:44:14 +1000401 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
Damien Millerb8481582000-12-03 11:51:51 +1100402 authenticated = auth2_pam(authctxt);
403#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100404 xfree(devs);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000405 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +1100406#ifdef HAVE_CYGWIN
407 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
408 return(0);
409#endif
410 return authenticated;
411}
412
Ben Lindstrombba81212001-06-25 05:01:22 +0000413static int
Damien Miller874d77b2000-10-14 16:23:11 +1100414userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000415{
416 Buffer b;
417 Key *key;
418 char *pkalg, *pkblob, *sig;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000419 u_int alen, blen, slen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100420 int have_sig, pktype;
Damien Millereba71ba2000-04-29 23:57:08 +1000421 int authenticated = 0;
422
Damien Miller874d77b2000-10-14 16:23:11 +1100423 if (!authctxt->valid) {
424 debug2("userauth_pubkey: disabled because of invalid user");
Damien Millereba71ba2000-04-29 23:57:08 +1000425 return 0;
426 }
427 have_sig = packet_get_char();
Ben Lindstromd121f612000-12-03 17:00:47 +0000428 if (datafellows & SSH_BUG_PKAUTH) {
429 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
430 /* no explicit pkalg given */
431 pkblob = packet_get_string(&blen);
432 buffer_init(&b);
433 buffer_append(&b, pkblob, blen);
434 /* so we have to extract the pkalg from the pkblob */
435 pkalg = buffer_get_string(&b, &alen);
436 buffer_free(&b);
437 } else {
438 pkalg = packet_get_string(&alen);
439 pkblob = packet_get_string(&blen);
440 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100441 pktype = key_type_from_name(pkalg);
442 if (pktype == KEY_UNSPEC) {
Ben Lindstromd121f612000-12-03 17:00:47 +0000443 /* this is perfectly legal */
444 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
Damien Miller874d77b2000-10-14 16:23:11 +1100445 xfree(pkalg);
Ben Lindstromd121f612000-12-03 17:00:47 +0000446 xfree(pkblob);
Damien Millereba71ba2000-04-29 23:57:08 +1000447 return 0;
448 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100449 key = key_from_blob(pkblob, blen);
Damien Millereba71ba2000-04-29 23:57:08 +1000450 if (key != NULL) {
451 if (have_sig) {
452 sig = packet_get_string(&slen);
453 packet_done();
454 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100455 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000456 buffer_append(&b, session_id2, session_id2_len);
Damien Miller50a41ed2000-10-16 12:14:42 +1100457 } else {
458 buffer_put_string(&b, session_id2, session_id2_len);
Damien Miller6536c7d2000-06-22 21:32:31 +1000459 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000460 /* reconstruct packet */
Damien Millereba71ba2000-04-29 23:57:08 +1000461 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller874d77b2000-10-14 16:23:11 +1100462 buffer_put_cstring(&b, authctxt->user);
Damien Millerf6d9e222000-06-18 14:50:44 +1000463 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000464 datafellows & SSH_BUG_PKSERVICE ?
Damien Millerf6d9e222000-06-18 14:50:44 +1000465 "ssh-userauth" :
Damien Miller874d77b2000-10-14 16:23:11 +1100466 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000467 if (datafellows & SSH_BUG_PKAUTH) {
468 buffer_put_char(&b, have_sig);
469 } else {
470 buffer_put_cstring(&b, "publickey");
471 buffer_put_char(&b, have_sig);
Ben Lindstrom3f364962001-04-19 20:50:07 +0000472 buffer_put_cstring(&b, pkalg);
Ben Lindstromd121f612000-12-03 17:00:47 +0000473 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000474 buffer_put_string(&b, pkblob, blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100475#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000476 buffer_dump(&b);
477#endif
478 /* test for correct signature */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100479 if (user_key_allowed(authctxt->pw, key) &&
480 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000481 authenticated = 1;
482 buffer_clear(&b);
483 xfree(sig);
484 } else {
Damien Miller874d77b2000-10-14 16:23:11 +1100485 debug("test whether pkalg/pkblob are acceptable");
Damien Millereba71ba2000-04-29 23:57:08 +1000486 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100487
Damien Millereba71ba2000-04-29 23:57:08 +1000488 /* XXX fake reply and always send PK_OK ? */
Damien Millere247cc42000-05-07 12:03:14 +1000489 /*
490 * XXX this allows testing whether a user is allowed
491 * to login: if you happen to have a valid pubkey this
492 * message is sent. the message is NEVER sent at all
493 * if a user is not allowed to login. is this an
494 * issue? -markus
495 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100496 if (user_key_allowed(authctxt->pw, key)) {
Damien Millereba71ba2000-04-29 23:57:08 +1000497 packet_start(SSH2_MSG_USERAUTH_PK_OK);
498 packet_put_string(pkalg, alen);
499 packet_put_string(pkblob, blen);
500 packet_send();
501 packet_write_wait();
Kevin Steves4abe4de2001-02-08 19:16:32 +0000502 authctxt->postponed = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000503 }
504 }
Damien Miller874d77b2000-10-14 16:23:11 +1100505 if (authenticated != 1)
506 auth_clear_options();
Damien Millereba71ba2000-04-29 23:57:08 +1000507 key_free(key);
508 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100509 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
Damien Millereba71ba2000-04-29 23:57:08 +1000510 xfree(pkalg);
511 xfree(pkblob);
Damien Miller874d77b2000-10-14 16:23:11 +1100512#ifdef HAVE_CYGWIN
513 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
514 return(0);
515#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000516 return authenticated;
517}
518
Ben Lindstrombba81212001-06-25 05:01:22 +0000519static int
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000520userauth_hostbased(Authctxt *authctxt)
521{
522 Buffer b;
523 Key *key;
Ben Lindstrom671388f2001-04-19 20:40:45 +0000524 char *pkalg, *pkblob, *sig, *cuser, *chost, *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000525 u_int alen, blen, slen;
526 int pktype;
527 int authenticated = 0;
528
529 if (!authctxt->valid) {
530 debug2("userauth_hostbased: disabled because of invalid user");
531 return 0;
532 }
533 pkalg = packet_get_string(&alen);
534 pkblob = packet_get_string(&blen);
535 chost = packet_get_string(NULL);
536 cuser = packet_get_string(NULL);
537 sig = packet_get_string(&slen);
538
539 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
540 cuser, chost, pkalg, slen);
541#ifdef DEBUG_PK
542 debug("signature:");
543 buffer_init(&b);
544 buffer_append(&b, sig, slen);
545 buffer_dump(&b);
546 buffer_free(&b);
547#endif
548 pktype = key_type_from_name(pkalg);
549 if (pktype == KEY_UNSPEC) {
550 /* this is perfectly legal */
551 log("userauth_hostbased: unsupported "
552 "public key algorithm: %s", pkalg);
553 goto done;
554 }
555 key = key_from_blob(pkblob, blen);
556 if (key == NULL) {
557 debug("userauth_hostbased: cannot decode key: %s", pkalg);
558 goto done;
559 }
Ben Lindstrom671388f2001-04-19 20:40:45 +0000560 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
561 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000562 buffer_init(&b);
Ben Lindstrom671388f2001-04-19 20:40:45 +0000563 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000564 /* reconstruct packet */
565 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
566 buffer_put_cstring(&b, authctxt->user);
Ben Lindstrom671388f2001-04-19 20:40:45 +0000567 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000568 buffer_put_cstring(&b, "hostbased");
569 buffer_put_string(&b, pkalg, alen);
570 buffer_put_string(&b, pkblob, blen);
571 buffer_put_cstring(&b, chost);
572 buffer_put_cstring(&b, cuser);
573#ifdef DEBUG_PK
574 buffer_dump(&b);
575#endif
576 /* test for allowed key and correct signature */
577 if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
578 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
579 authenticated = 1;
580
581 buffer_clear(&b);
582 key_free(key);
583
584done:
585 debug2("userauth_hostbased: authenticated %d", authenticated);
586 xfree(pkalg);
587 xfree(pkblob);
588 xfree(cuser);
589 xfree(chost);
590 xfree(sig);
591 return authenticated;
592}
593
Damien Miller874d77b2000-10-14 16:23:11 +1100594/* get current user */
Damien Millereba71ba2000-04-29 23:57:08 +1000595
596struct passwd*
597auth_get_user(void)
598{
Damien Miller874d77b2000-10-14 16:23:11 +1100599 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000600}
601
Damien Miller874d77b2000-10-14 16:23:11 +1100602#define DELIM ","
Damien Millereba71ba2000-04-29 23:57:08 +1000603
Ben Lindstrom79073822001-07-04 03:42:30 +0000604static char *
Damien Miller874d77b2000-10-14 16:23:11 +1100605authmethods_get(void)
606{
607 Authmethod *method = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000608 u_int size = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100609 char *list;
610
611 for (method = authmethods; method->name != NULL; method++) {
612 if (strcmp(method->name, "none") == 0)
613 continue;
614 if (method->enabled != NULL && *(method->enabled) != 0) {
615 if (size != 0)
616 size += strlen(DELIM);
617 size += strlen(method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000618 }
619 }
Damien Miller874d77b2000-10-14 16:23:11 +1100620 size++; /* trailing '\0' */
621 list = xmalloc(size);
622 list[0] = '\0';
623
624 for (method = authmethods; method->name != NULL; method++) {
625 if (strcmp(method->name, "none") == 0)
626 continue;
627 if (method->enabled != NULL && *(method->enabled) != 0) {
628 if (list[0] != '\0')
629 strlcat(list, DELIM, size);
630 strlcat(list, method->name, size);
631 }
632 }
633 return list;
634}
635
Ben Lindstrombba81212001-06-25 05:01:22 +0000636static Authmethod *
Damien Miller874d77b2000-10-14 16:23:11 +1100637authmethod_lookup(const char *name)
638{
639 Authmethod *method = NULL;
640 if (name != NULL)
641 for (method = authmethods; method->name != NULL; method++)
642 if (method->enabled != NULL &&
643 *(method->enabled) != 0 &&
644 strcmp(name, method->name) == 0)
645 return method;
646 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
647 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000648}
649
650/* return 1 if user allows given key */
Ben Lindstrombba81212001-06-25 05:01:22 +0000651static int
Ben Lindstromf96704d2001-06-25 04:17:12 +0000652user_key_allowed2(struct passwd *pw, Key *key, char *file)
Damien Millereba71ba2000-04-29 23:57:08 +1000653{
Ben Lindstromf96704d2001-06-25 04:17:12 +0000654 char line[8192];
Damien Millereba71ba2000-04-29 23:57:08 +1000655 int found_key = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000656 FILE *f;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000657 u_long linenum = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000658 struct stat st;
659 Key *found;
660
Damien Miller874d77b2000-10-14 16:23:11 +1100661 if (pw == NULL)
662 return 0;
663
Damien Millereba71ba2000-04-29 23:57:08 +1000664 /* Temporarily use the user's uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000665 temporarily_use_uid(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000666
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000667 debug("trying public key file %s", file);
Damien Millereba71ba2000-04-29 23:57:08 +1000668
669 /* Fail quietly if file does not exist */
670 if (stat(file, &st) < 0) {
671 /* Restore the privileged uid. */
672 restore_uid();
673 return 0;
674 }
675 /* Open the file containing the authorized keys. */
676 f = fopen(file, "r");
677 if (!f) {
678 /* Restore the privileged uid. */
679 restore_uid();
680 return 0;
681 }
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000682 if (options.strict_modes &&
Ben Lindstrom90279d82001-07-04 03:56:56 +0000683 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000684 fclose(f);
685 log("Authentication refused: %s", line);
686 restore_uid();
687 return 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000688 }
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000689
Damien Millereba71ba2000-04-29 23:57:08 +1000690 found_key = 0;
Damien Millerd3444942000-09-30 14:20:03 +1100691 found = key_new(key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000692
693 while (fgets(line, sizeof(line), f)) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000694 char *cp, *options = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000695 linenum++;
696 /* Skip leading whitespace, empty and comment lines. */
697 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
698 ;
699 if (!*cp || *cp == '\n' || *cp == '#')
700 continue;
Damien Millerf6d9e222000-06-18 14:50:44 +1000701
Ben Lindstrom1bc3bdb2001-09-20 23:11:26 +0000702 if (key_read(found, &cp) != 1) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000703 /* no key? check if there are options for this key */
704 int quoted = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100705 debug2("user_key_allowed: check options: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000706 options = cp;
707 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
708 if (*cp == '\\' && cp[1] == '"')
709 cp++; /* Skip both */
710 else if (*cp == '"')
711 quoted = !quoted;
712 }
713 /* Skip remaining whitespace. */
714 for (; *cp == ' ' || *cp == '\t'; cp++)
715 ;
Ben Lindstrom1bc3bdb2001-09-20 23:11:26 +0000716 if (key_read(found, &cp) != 1) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100717 debug2("user_key_allowed: advance: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000718 /* still no key? advance to next line*/
719 continue;
720 }
721 }
722 if (key_equal(found, key) &&
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000723 auth_parse_options(pw, options, file, linenum) == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000724 found_key = 1;
Ben Lindstrom940fb862001-08-06 21:01:49 +0000725 debug("matching key found: file %s, line %lu",
Damien Millereba71ba2000-04-29 23:57:08 +1000726 file, linenum);
727 break;
728 }
729 }
730 restore_uid();
731 fclose(f);
732 key_free(found);
Ben Lindstromb54873a2001-03-11 20:01:55 +0000733 if (!found_key)
734 debug2("key not found");
Damien Millereba71ba2000-04-29 23:57:08 +1000735 return found_key;
736}
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000737
Ben Lindstromf96704d2001-06-25 04:17:12 +0000738/* check whether given key is in .ssh/authorized_keys* */
Ben Lindstrombba81212001-06-25 05:01:22 +0000739static int
Ben Lindstromf96704d2001-06-25 04:17:12 +0000740user_key_allowed(struct passwd *pw, Key *key)
741{
742 int success;
743 char *file;
744
745 file = authorized_keys_file(pw);
746 success = user_key_allowed2(pw, key, file);
747 xfree(file);
748 if (success)
749 return success;
750
751 /* try suffix "2" for backward compat, too */
752 file = authorized_keys_file2(pw);
753 success = user_key_allowed2(pw, key, file);
754 xfree(file);
755 return success;
756}
757
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000758/* return 1 if given hostkey is allowed */
Ben Lindstrombba81212001-06-25 05:01:22 +0000759static int
Ben Lindstrom4aa603c2001-04-19 20:38:06 +0000760hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000761 Key *key)
762{
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000763 const char *resolvedname, *ipaddr, *lookup;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000764 int host_status, len;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000765
766 resolvedname = get_canonical_hostname(options.reverse_mapping_check);
767 ipaddr = get_remote_ipaddr();
768
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000769 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
770 chost, resolvedname, ipaddr);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000771
772 if (options.hostbased_uses_name_from_packet_only) {
773 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
774 return 0;
775 lookup = chost;
776 } else {
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000777 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
778 debug2("stripping trailing dot from chost %s", chost);
779 chost[len - 1] = '\0';
780 }
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000781 if (strcasecmp(resolvedname, chost) != 0)
782 log("userauth_hostbased mismatch: "
783 "client sends %s, but we resolve %s to %s",
784 chost, ipaddr, resolvedname);
785 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
786 return 0;
787 lookup = resolvedname;
788 }
789 debug2("userauth_hostbased: access allowed by auth_rhosts2");
790
Ben Lindstrom83647ce2001-06-25 04:30:16 +0000791 host_status = check_key_in_hostfiles(pw, key, lookup,
792 _PATH_SSH_SYSTEM_HOSTFILE,
Ben Lindstroma4789ef2001-06-25 04:40:49 +0000793 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000794
Ben Lindstrom83647ce2001-06-25 04:30:16 +0000795 /* backward compat if no key has been found. */
796 if (host_status == HOST_NEW)
797 host_status = check_key_in_hostfiles(pw, key, lookup,
798 _PATH_SSH_SYSTEM_HOSTFILE2,
Ben Lindstroma4789ef2001-06-25 04:40:49 +0000799 options.ignore_user_known_hosts ? NULL :
800 _PATH_SSH_USER_HOSTFILE2);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000801
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000802 return (host_status == HOST_OK);
803}
Ben Lindstrom83647ce2001-06-25 04:30:16 +0000804