blob: 3cd946877333cdbdf2763564fabbe5dc69b6c43a [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 Lindstromd8a90212001-02-15 03:08:27 +000026RCSID("$OpenBSD: auth2.c,v 1.41 2001/02/12 16:16:23 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"
Damien Millereba71ba2000-04-29 23:57:08 +100033#include "pty.h"
34#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"
39#include "channels.h"
40#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"
Damien Millereba71ba2000-04-29 23:57:08 +100050
51/* import */
52extern ServerOptions options;
Ben Lindstrom46c16222000-12-22 01:43:59 +000053extern u_char *session_id2;
Damien Millereba71ba2000-04-29 23:57:08 +100054extern int session_id2_len;
55
Damien Miller874d77b2000-10-14 16:23:11 +110056#ifdef WITH_AIXAUTHENTICATE
57extern char *aixloginmsg;
58#endif
Damien Miller874d77b2000-10-14 16:23:11 +110059
60static Authctxt *x_authctxt = NULL;
61static int one = 1;
62
63typedef struct Authmethod Authmethod;
64struct Authmethod {
65 char *name;
66 int (*userauth)(Authctxt *authctxt);
67 int *enabled;
68};
69
Damien Millereba71ba2000-04-29 23:57:08 +100070/* protocol */
71
Damien Miller62cee002000-09-23 17:15:56 +110072void input_service_request(int type, int plen, void *ctxt);
73void input_userauth_request(int type, int plen, void *ctxt);
74void protocol_error(int type, int plen, void *ctxt);
Damien Millereba71ba2000-04-29 23:57:08 +100075
Damien Millereba71ba2000-04-29 23:57:08 +100076/* helper */
Damien Miller874d77b2000-10-14 16:23:11 +110077Authmethod *authmethod_lookup(const char *name);
78struct passwd *pwcopy(struct passwd *pw);
Damien Miller0bc1bd82000-11-13 22:57:25 +110079int user_key_allowed(struct passwd *pw, Key *key);
Damien Miller874d77b2000-10-14 16:23:11 +110080char *authmethods_get(void);
Damien Millereba71ba2000-04-29 23:57:08 +100081
Damien Miller874d77b2000-10-14 16:23:11 +110082/* auth */
Ben Lindstrom48bd7c12001-01-09 00:35:42 +000083void userauth_banner(void);
Damien Miller874d77b2000-10-14 16:23:11 +110084int userauth_none(Authctxt *authctxt);
85int userauth_passwd(Authctxt *authctxt);
86int userauth_pubkey(Authctxt *authctxt);
87int userauth_kbdint(Authctxt *authctxt);
88
89Authmethod authmethods[] = {
90 {"none",
91 userauth_none,
92 &one},
93 {"publickey",
94 userauth_pubkey,
Damien Miller0bc1bd82000-11-13 22:57:25 +110095 &options.pubkey_authentication},
Damien Miller874d77b2000-10-14 16:23:11 +110096 {"password",
97 userauth_passwd,
98 &options.password_authentication},
Ben Lindstromd1f20ec2001-02-10 21:31:53 +000099 {"keyboard-interactive",
100 userauth_kbdint,
101 &options.kbd_interactive_authentication},
Damien Miller874d77b2000-10-14 16:23:11 +1100102 {NULL, NULL, NULL}
Damien Millereba71ba2000-04-29 23:57:08 +1000103};
Damien Millereba71ba2000-04-29 23:57:08 +1000104
105/*
Damien Miller874d77b2000-10-14 16:23:11 +1100106 * loop until authctxt->success == TRUE
Damien Millereba71ba2000-04-29 23:57:08 +1000107 */
108
109void
110do_authentication2()
111{
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000112 Authctxt *authctxt = authctxt_new();
113
Damien Miller874d77b2000-10-14 16:23:11 +1100114 x_authctxt = authctxt; /*XXX*/
115
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000116 /* challenge-reponse is implemented via keyboard interactive */
117 if (options.challenge_reponse_authentication)
118 options.kbd_interactive_authentication = 1;
119
Damien Millereba71ba2000-04-29 23:57:08 +1000120 dispatch_init(&protocol_error);
121 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
Damien Miller874d77b2000-10-14 16:23:11 +1100122 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000123 do_authenticated2(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000124}
125
126void
Damien Miller62cee002000-09-23 17:15:56 +1100127protocol_error(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000128{
129 log("auth: protocol error: type %d plen %d", type, plen);
130 packet_start(SSH2_MSG_UNIMPLEMENTED);
131 packet_put_int(0);
132 packet_send();
133 packet_write_wait();
134}
135
136void
Damien Miller62cee002000-09-23 17:15:56 +1100137input_service_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000138{
Damien Miller874d77b2000-10-14 16:23:11 +1100139 Authctxt *authctxt = ctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000140 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000141 int accept = 0;
142 char *service = packet_get_string(&len);
143 packet_done();
144
Damien Miller874d77b2000-10-14 16:23:11 +1100145 if (authctxt == NULL)
146 fatal("input_service_request: no authctxt");
147
Damien Millereba71ba2000-04-29 23:57:08 +1000148 if (strcmp(service, "ssh-userauth") == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100149 if (!authctxt->success) {
Damien Millereba71ba2000-04-29 23:57:08 +1000150 accept = 1;
151 /* now we can handle user-auth requests */
152 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
153 }
154 }
155 /* XXX all other service requests are denied */
156
157 if (accept) {
158 packet_start(SSH2_MSG_SERVICE_ACCEPT);
159 packet_put_cstring(service);
160 packet_send();
161 packet_write_wait();
162 } else {
163 debug("bad service request %s", service);
164 packet_disconnect("bad service request %s", service);
165 }
166 xfree(service);
167}
168
169void
Damien Miller62cee002000-09-23 17:15:56 +1100170input_userauth_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000171{
Damien Miller874d77b2000-10-14 16:23:11 +1100172 Authctxt *authctxt = ctxt;
173 Authmethod *m = NULL;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000174 char *user, *service, *method, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000175 int authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000176
Damien Miller874d77b2000-10-14 16:23:11 +1100177 if (authctxt == NULL)
178 fatal("input_userauth_request: no authctxt");
Damien Millereba71ba2000-04-29 23:57:08 +1000179
Damien Miller874d77b2000-10-14 16:23:11 +1100180 user = packet_get_string(NULL);
181 service = packet_get_string(NULL);
182 method = packet_get_string(NULL);
183 debug("userauth-request for user %s service %s method %s", user, service, method);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000184 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
Damien Miller874d77b2000-10-14 16:23:11 +1100185
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000186 if ((style = strchr(user, ':')) != NULL)
187 *style++ = 0;
188
Kevin Stevesef4eea92001-02-05 12:42:17 +0000189 if (authctxt->attempt++ == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100190 /* setup auth context */
191 struct passwd *pw = NULL;
192 setproctitle("%s", user);
193 pw = getpwnam(user);
194 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
195 authctxt->pw = pwcopy(pw);
196 authctxt->valid = 1;
197 debug2("input_userauth_request: setting up authctxt for %s", user);
198#ifdef USE_PAM
Damien Miller22e22bf2001-01-19 15:46:38 +1100199 start_pam(pw->pw_name);
Damien Miller874d77b2000-10-14 16:23:11 +1100200#endif
201 } else {
202 log("input_userauth_request: illegal user %s", user);
Damien Miller22e22bf2001-01-19 15:46:38 +1100203#ifdef USE_PAM
204 start_pam("NOUSER");
205#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100206 }
207 authctxt->user = xstrdup(user);
208 authctxt->service = xstrdup(service);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000209 authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */
Damien Miller874d77b2000-10-14 16:23:11 +1100210 } else if (authctxt->valid) {
211 if (strcmp(user, authctxt->user) != 0 ||
212 strcmp(service, authctxt->service) != 0) {
213 log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)",
214 user, service, authctxt->user, authctxt->service);
215 authctxt->valid = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000216 }
217 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000218 /* reset state */
219 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
220 authctxt->postponed = 0;
Damien Millerb70b61f2000-09-16 16:25:12 +1100221
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000222 /* try to authenticate user */
Damien Miller874d77b2000-10-14 16:23:11 +1100223 m = authmethod_lookup(method);
224 if (m != NULL) {
225 debug2("input_userauth_request: try method %s", method);
226 authenticated = m->userauth(authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100227 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000228 if (!authctxt->valid && authenticated)
229 fatal("INTERNAL ERROR: authenticated invalid user %s",
230 authctxt->user);
Damien Millerb70b61f2000-09-16 16:25:12 +1100231
Damien Miller874d77b2000-10-14 16:23:11 +1100232 /* Special handling for root */
Ben Lindstromd8a90212001-02-15 03:08:27 +0000233 if (authenticated && authctxt->pw->pw_uid == 0 &&
234 !auth_root_allowed(method))
Damien Millereba71ba2000-04-29 23:57:08 +1000235 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000236
237#ifdef USE_PAM
Damien Millerd425d4d2000-10-28 21:05:57 +1100238 if (authenticated && authctxt->user && !do_pam_account(authctxt->user, NULL))
Damien Millerb70b61f2000-09-16 16:25:12 +1100239 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000240#endif /* USE_PAM */
241
Damien Miller874d77b2000-10-14 16:23:11 +1100242 /* Log before sending the reply */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000243 auth_log(authctxt, authenticated, method, " ssh2");
244
245 if (!authctxt->postponed)
246 userauth_reply(authctxt, authenticated);
Damien Miller874d77b2000-10-14 16:23:11 +1100247
248 xfree(service);
249 xfree(user);
250 xfree(method);
251}
252
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000253void
254userauth_banner(void)
255{
256 struct stat st;
257 char *banner = NULL;
258 off_t len, n;
259 int fd;
260
261 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
262 return;
263 if ((fd = open(options.banner, O_RDONLY)) < 0) {
264 error("userauth_banner: open %s failed: %s",
265 options.banner, strerror(errno));
266 return;
267 }
268 if (fstat(fd, &st) < 0)
269 goto done;
270 len = st.st_size;
271 banner = xmalloc(len + 1);
272 if ((n = read(fd, banner, len)) < 0)
273 goto done;
274 banner[n] = '\0';
275 packet_start(SSH2_MSG_USERAUTH_BANNER);
276 packet_put_cstring(banner);
277 packet_put_cstring(""); /* language, unused */
278 packet_send();
279 debug("userauth_banner: sent");
280done:
281 if (banner)
282 xfree(banner);
283 close(fd);
284 return;
285}
Damien Miller874d77b2000-10-14 16:23:11 +1100286
Kevin Stevesef4eea92001-02-05 12:42:17 +0000287void
Damien Miller874d77b2000-10-14 16:23:11 +1100288userauth_reply(Authctxt *authctxt, int authenticated)
289{
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000290 char *methods;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000291
Damien Millere247cc42000-05-07 12:03:14 +1000292 /* XXX todo: check if multiple auth methods are needed */
Kevin Steves4abe4de2001-02-08 19:16:32 +0000293 if (authenticated == 1) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000294#ifdef WITH_AIXAUTHENTICATE
295 /* We don't have a pty yet, so just label the line as "ssh" */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000296 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
297 get_canonical_hostname(options.reverse_mapping_check),
Damien Miller33804262001-02-04 23:20:18 +1100298 "ssh", &aixloginmsg) < 0)
Damien Millerd2c208a2000-05-17 22:00:02 +1000299 aixloginmsg = NULL;
300#endif /* WITH_AIXAUTHENTICATE */
Damien Millereba71ba2000-04-29 23:57:08 +1000301 /* turn off userauth */
302 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
303 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
304 packet_send();
305 packet_write_wait();
306 /* now we can break out */
Damien Miller874d77b2000-10-14 16:23:11 +1100307 authctxt->success = 1;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000308 } else {
309 if (authctxt->failures++ > AUTH_FAIL_MAX)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000310 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000311 methods = authmethods_get();
Damien Millereba71ba2000-04-29 23:57:08 +1000312 packet_start(SSH2_MSG_USERAUTH_FAILURE);
Damien Miller874d77b2000-10-14 16:23:11 +1100313 packet_put_cstring(methods);
314 packet_put_char(0); /* XXX partial success, unused */
Damien Millereba71ba2000-04-29 23:57:08 +1000315 packet_send();
316 packet_write_wait();
Damien Miller874d77b2000-10-14 16:23:11 +1100317 xfree(methods);
Damien Millereba71ba2000-04-29 23:57:08 +1000318 }
Damien Millereba71ba2000-04-29 23:57:08 +1000319}
320
321int
Damien Miller874d77b2000-10-14 16:23:11 +1100322userauth_none(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000323{
Damien Miller874d77b2000-10-14 16:23:11 +1100324 /* disable method "none", only allowed one time */
325 Authmethod *m = authmethod_lookup("none");
326 if (m != NULL)
327 m->enabled = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000328 packet_done();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000329 userauth_banner();
Damien Millerb8c656e2000-06-28 15:22:41 +1000330
Damien Miller874d77b2000-10-14 16:23:11 +1100331 if (authctxt->valid == 0)
332 return(0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000333
Damien Miller874d77b2000-10-14 16:23:11 +1100334#ifdef HAVE_CYGWIN
335 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
336 return(0);
337#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000338#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100339 return auth_pam_password(authctxt->pw, "");
Damien Millerb8c656e2000-06-28 15:22:41 +1000340#elif defined(HAVE_OSF_SIA)
Damien Miller92ddb7d2001-02-14 01:25:23 +1100341 return 0;
Damien Millerb8c656e2000-06-28 15:22:41 +1000342#else /* !HAVE_OSF_SIA && !USE_PAM */
Damien Miller874d77b2000-10-14 16:23:11 +1100343 return auth_password(authctxt->pw, "");
Damien Millereba71ba2000-04-29 23:57:08 +1000344#endif /* USE_PAM */
345}
Damien Miller874d77b2000-10-14 16:23:11 +1100346
Damien Millereba71ba2000-04-29 23:57:08 +1000347int
Damien Miller874d77b2000-10-14 16:23:11 +1100348userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000349{
350 char *password;
351 int authenticated = 0;
352 int change;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000353 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000354 change = packet_get_char();
355 if (change)
356 log("password change not supported");
357 password = packet_get_string(&len);
358 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100359 if (authctxt->valid &&
360#ifdef HAVE_CYGWIN
361 check_nt_auth(1, authctxt->pw->pw_uid) &&
362#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000363#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100364 auth_pam_password(authctxt->pw, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000365#elif defined(HAVE_OSF_SIA)
Damien Miller92ddb7d2001-02-14 01:25:23 +1100366 auth_sia_password(authctxt->user, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000367#else /* !USE_PAM && !HAVE_OSF_SIA */
Damien Miller874d77b2000-10-14 16:23:11 +1100368 auth_password(authctxt->pw, password) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000369#endif /* USE_PAM */
370 authenticated = 1;
371 memset(password, 0, len);
372 xfree(password);
373 return authenticated;
374}
Damien Miller874d77b2000-10-14 16:23:11 +1100375
Damien Millereba71ba2000-04-29 23:57:08 +1000376int
Damien Miller874d77b2000-10-14 16:23:11 +1100377userauth_kbdint(Authctxt *authctxt)
378{
379 int authenticated = 0;
380 char *lang = NULL;
381 char *devs = NULL;
382
383 lang = packet_get_string(NULL);
384 devs = packet_get_string(NULL);
385 packet_done();
386
387 debug("keyboard-interactive language %s devs %s", lang, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000388
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000389 if (options.challenge_reponse_authentication)
390 authenticated = auth2_challenge(authctxt, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000391
Damien Millerb8481582000-12-03 11:51:51 +1100392#ifdef USE_PAM
393 if (authenticated == 0)
394 authenticated = auth2_pam(authctxt);
395#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100396 xfree(lang);
397 xfree(devs);
398#ifdef HAVE_CYGWIN
399 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
400 return(0);
401#endif
402 return authenticated;
403}
404
405int
406userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000407{
408 Buffer b;
409 Key *key;
410 char *pkalg, *pkblob, *sig;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000411 u_int alen, blen, slen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100412 int have_sig, pktype;
Damien Millereba71ba2000-04-29 23:57:08 +1000413 int authenticated = 0;
414
Damien Miller874d77b2000-10-14 16:23:11 +1100415 if (!authctxt->valid) {
416 debug2("userauth_pubkey: disabled because of invalid user");
Damien Millereba71ba2000-04-29 23:57:08 +1000417 return 0;
418 }
419 have_sig = packet_get_char();
Ben Lindstromd121f612000-12-03 17:00:47 +0000420 if (datafellows & SSH_BUG_PKAUTH) {
421 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
422 /* no explicit pkalg given */
423 pkblob = packet_get_string(&blen);
424 buffer_init(&b);
425 buffer_append(&b, pkblob, blen);
426 /* so we have to extract the pkalg from the pkblob */
427 pkalg = buffer_get_string(&b, &alen);
428 buffer_free(&b);
429 } else {
430 pkalg = packet_get_string(&alen);
431 pkblob = packet_get_string(&blen);
432 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100433 pktype = key_type_from_name(pkalg);
434 if (pktype == KEY_UNSPEC) {
Ben Lindstromd121f612000-12-03 17:00:47 +0000435 /* this is perfectly legal */
436 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
Damien Miller874d77b2000-10-14 16:23:11 +1100437 xfree(pkalg);
Ben Lindstromd121f612000-12-03 17:00:47 +0000438 xfree(pkblob);
Damien Millereba71ba2000-04-29 23:57:08 +1000439 return 0;
440 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100441 key = key_from_blob(pkblob, blen);
Damien Millereba71ba2000-04-29 23:57:08 +1000442 if (key != NULL) {
443 if (have_sig) {
444 sig = packet_get_string(&slen);
445 packet_done();
446 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100447 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000448 buffer_append(&b, session_id2, session_id2_len);
Damien Miller50a41ed2000-10-16 12:14:42 +1100449 } else {
450 buffer_put_string(&b, session_id2, session_id2_len);
Damien Miller6536c7d2000-06-22 21:32:31 +1000451 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000452 /* reconstruct packet */
Damien Millereba71ba2000-04-29 23:57:08 +1000453 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller874d77b2000-10-14 16:23:11 +1100454 buffer_put_cstring(&b, authctxt->user);
Damien Millerf6d9e222000-06-18 14:50:44 +1000455 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000456 datafellows & SSH_BUG_PKSERVICE ?
Damien Millerf6d9e222000-06-18 14:50:44 +1000457 "ssh-userauth" :
Damien Miller874d77b2000-10-14 16:23:11 +1100458 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000459 if (datafellows & SSH_BUG_PKAUTH) {
460 buffer_put_char(&b, have_sig);
461 } else {
462 buffer_put_cstring(&b, "publickey");
463 buffer_put_char(&b, have_sig);
464 buffer_put_cstring(&b, key_ssh_name(key));
465 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000466 buffer_put_string(&b, pkblob, blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100467#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000468 buffer_dump(&b);
469#endif
470 /* test for correct signature */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100471 if (user_key_allowed(authctxt->pw, key) &&
472 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000473 authenticated = 1;
474 buffer_clear(&b);
475 xfree(sig);
476 } else {
Damien Miller874d77b2000-10-14 16:23:11 +1100477 debug("test whether pkalg/pkblob are acceptable");
Damien Millereba71ba2000-04-29 23:57:08 +1000478 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100479
Damien Millereba71ba2000-04-29 23:57:08 +1000480 /* XXX fake reply and always send PK_OK ? */
Damien Millere247cc42000-05-07 12:03:14 +1000481 /*
482 * XXX this allows testing whether a user is allowed
483 * to login: if you happen to have a valid pubkey this
484 * message is sent. the message is NEVER sent at all
485 * if a user is not allowed to login. is this an
486 * issue? -markus
487 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100488 if (user_key_allowed(authctxt->pw, key)) {
Damien Millereba71ba2000-04-29 23:57:08 +1000489 packet_start(SSH2_MSG_USERAUTH_PK_OK);
490 packet_put_string(pkalg, alen);
491 packet_put_string(pkblob, blen);
492 packet_send();
493 packet_write_wait();
Kevin Steves4abe4de2001-02-08 19:16:32 +0000494 authctxt->postponed = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000495 }
496 }
Damien Miller874d77b2000-10-14 16:23:11 +1100497 if (authenticated != 1)
498 auth_clear_options();
Damien Millereba71ba2000-04-29 23:57:08 +1000499 key_free(key);
500 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100501 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
Damien Millereba71ba2000-04-29 23:57:08 +1000502 xfree(pkalg);
503 xfree(pkblob);
Damien Miller874d77b2000-10-14 16:23:11 +1100504#ifdef HAVE_CYGWIN
505 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
506 return(0);
507#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000508 return authenticated;
509}
510
Damien Miller874d77b2000-10-14 16:23:11 +1100511/* get current user */
Damien Millereba71ba2000-04-29 23:57:08 +1000512
513struct passwd*
514auth_get_user(void)
515{
Damien Miller874d77b2000-10-14 16:23:11 +1100516 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000517}
518
Damien Miller874d77b2000-10-14 16:23:11 +1100519#define DELIM ","
Damien Millereba71ba2000-04-29 23:57:08 +1000520
Damien Miller874d77b2000-10-14 16:23:11 +1100521char *
522authmethods_get(void)
523{
524 Authmethod *method = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000525 u_int size = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100526 char *list;
527
528 for (method = authmethods; method->name != NULL; method++) {
529 if (strcmp(method->name, "none") == 0)
530 continue;
531 if (method->enabled != NULL && *(method->enabled) != 0) {
532 if (size != 0)
533 size += strlen(DELIM);
534 size += strlen(method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000535 }
536 }
Damien Miller874d77b2000-10-14 16:23:11 +1100537 size++; /* trailing '\0' */
538 list = xmalloc(size);
539 list[0] = '\0';
540
541 for (method = authmethods; method->name != NULL; method++) {
542 if (strcmp(method->name, "none") == 0)
543 continue;
544 if (method->enabled != NULL && *(method->enabled) != 0) {
545 if (list[0] != '\0')
546 strlcat(list, DELIM, size);
547 strlcat(list, method->name, size);
548 }
549 }
550 return list;
551}
552
553Authmethod *
554authmethod_lookup(const char *name)
555{
556 Authmethod *method = NULL;
557 if (name != NULL)
558 for (method = authmethods; method->name != NULL; method++)
559 if (method->enabled != NULL &&
560 *(method->enabled) != 0 &&
561 strcmp(name, method->name) == 0)
562 return method;
563 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
564 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000565}
566
567/* return 1 if user allows given key */
568int
Damien Miller0bc1bd82000-11-13 22:57:25 +1100569user_key_allowed(struct passwd *pw, Key *key)
Damien Millereba71ba2000-04-29 23:57:08 +1000570{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000571 char line[8192], file[MAXPATHLEN];
Damien Millereba71ba2000-04-29 23:57:08 +1000572 int found_key = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000573 FILE *f;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000574 u_long linenum = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000575 struct stat st;
576 Key *found;
577
Damien Miller874d77b2000-10-14 16:23:11 +1100578 if (pw == NULL)
579 return 0;
580
Damien Millereba71ba2000-04-29 23:57:08 +1000581 /* Temporarily use the user's uid. */
582 temporarily_use_uid(pw->pw_uid);
583
584 /* The authorized keys. */
585 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000586 _PATH_SSH_USER_PERMITTED_KEYS2);
Damien Millereba71ba2000-04-29 23:57:08 +1000587
588 /* Fail quietly if file does not exist */
589 if (stat(file, &st) < 0) {
590 /* Restore the privileged uid. */
591 restore_uid();
592 return 0;
593 }
594 /* Open the file containing the authorized keys. */
595 f = fopen(file, "r");
596 if (!f) {
597 /* Restore the privileged uid. */
598 restore_uid();
599 return 0;
600 }
601 if (options.strict_modes) {
602 int fail = 0;
603 char buf[1024];
604 /* Check open file in order to avoid open/stat races */
605 if (fstat(fileno(f), &st) < 0 ||
606 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
607 (st.st_mode & 022) != 0) {
Damien Millerd3444942000-09-30 14:20:03 +1100608 snprintf(buf, sizeof buf,
609 "%s authentication refused for %.100s: "
610 "bad ownership or modes for '%s'.",
611 key_type(key), pw->pw_name, file);
Damien Millereba71ba2000-04-29 23:57:08 +1000612 fail = 1;
613 } else {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000614 /* Check path to _PATH_SSH_USER_PERMITTED_KEYS */
Damien Millereba71ba2000-04-29 23:57:08 +1000615 int i;
616 static const char *check[] = {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000617 "", _PATH_SSH_USER_DIR, NULL
Damien Millereba71ba2000-04-29 23:57:08 +1000618 };
619 for (i = 0; check[i]; i++) {
620 snprintf(line, sizeof line, "%.500s/%.100s",
621 pw->pw_dir, check[i]);
622 if (stat(line, &st) < 0 ||
623 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
624 (st.st_mode & 022) != 0) {
625 snprintf(buf, sizeof buf,
Damien Millerd3444942000-09-30 14:20:03 +1100626 "%s authentication refused for %.100s: "
Damien Millereba71ba2000-04-29 23:57:08 +1000627 "bad ownership or modes for '%s'.",
Damien Millerd3444942000-09-30 14:20:03 +1100628 key_type(key), pw->pw_name, line);
Damien Millereba71ba2000-04-29 23:57:08 +1000629 fail = 1;
630 break;
631 }
632 }
633 }
634 if (fail) {
Damien Millereba71ba2000-04-29 23:57:08 +1000635 fclose(f);
Damien Miller37023962000-07-11 17:31:38 +1000636 log("%s",buf);
Damien Millereba71ba2000-04-29 23:57:08 +1000637 restore_uid();
638 return 0;
639 }
640 }
641 found_key = 0;
Damien Millerd3444942000-09-30 14:20:03 +1100642 found = key_new(key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000643
644 while (fgets(line, sizeof(line), f)) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000645 char *cp, *options = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000646 linenum++;
647 /* Skip leading whitespace, empty and comment lines. */
648 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
649 ;
650 if (!*cp || *cp == '\n' || *cp == '#')
651 continue;
Damien Millerf6d9e222000-06-18 14:50:44 +1000652
Damien Miller0bc1bd82000-11-13 22:57:25 +1100653 if (key_read(found, &cp) == -1) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000654 /* no key? check if there are options for this key */
655 int quoted = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100656 debug2("user_key_allowed: check options: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000657 options = cp;
658 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
659 if (*cp == '\\' && cp[1] == '"')
660 cp++; /* Skip both */
661 else if (*cp == '"')
662 quoted = !quoted;
663 }
664 /* Skip remaining whitespace. */
665 for (; *cp == ' ' || *cp == '\t'; cp++)
666 ;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100667 if (key_read(found, &cp) == -1) {
668 debug2("user_key_allowed: advance: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000669 /* still no key? advance to next line*/
670 continue;
671 }
672 }
673 if (key_equal(found, key) &&
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000674 auth_parse_options(pw, options, file, linenum) == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000675 found_key = 1;
676 debug("matching key found: file %s, line %ld",
677 file, linenum);
678 break;
679 }
680 }
681 restore_uid();
682 fclose(f);
683 key_free(found);
684 return found_key;
685}