blob: 348c2f3a4f772ace33f26fd8f52f059f2360c2b6 [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 Lindstromdb65e8f2001-01-19 04:26:52 +000026RCSID("$OpenBSD: auth2.c,v 1.28 2001/01/18 17:00:00 markus Exp $");
Damien Miller874d77b2000-10-14 16:23:11 +110027
28#ifdef HAVE_OSF_SIA
29# include <sia.h>
30# include <siad.h>
31#endif
Damien Millereba71ba2000-04-29 23:57:08 +100032
33#include <openssl/dsa.h>
34#include <openssl/rsa.h>
35#include <openssl/evp.h>
36
37#include "xmalloc.h"
38#include "rsa.h"
39#include "ssh.h"
40#include "pty.h"
41#include "packet.h"
42#include "buffer.h"
Damien Millereba71ba2000-04-29 23:57:08 +100043#include "servconf.h"
44#include "compat.h"
45#include "channels.h"
46#include "bufaux.h"
47#include "ssh2.h"
48#include "auth.h"
49#include "session.h"
50#include "dispatch.h"
51#include "auth.h"
52#include "key.h"
53#include "kex.h"
54
Damien Millereba71ba2000-04-29 23:57:08 +100055#include "uidswap.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100056#include "auth-options.h"
Damien Millereba71ba2000-04-29 23:57:08 +100057
58/* import */
59extern ServerOptions options;
Ben Lindstrom46c16222000-12-22 01:43:59 +000060extern u_char *session_id2;
Damien Millereba71ba2000-04-29 23:57:08 +100061extern int session_id2_len;
62
Damien Miller874d77b2000-10-14 16:23:11 +110063#ifdef WITH_AIXAUTHENTICATE
64extern char *aixloginmsg;
65#endif
66#ifdef HAVE_OSF_SIA
67extern int saved_argc;
68extern char **saved_argv;
69#endif
70
71static Authctxt *x_authctxt = NULL;
72static int one = 1;
73
74typedef struct Authmethod Authmethod;
75struct Authmethod {
76 char *name;
77 int (*userauth)(Authctxt *authctxt);
78 int *enabled;
79};
80
Damien Millereba71ba2000-04-29 23:57:08 +100081/* protocol */
82
Damien Miller62cee002000-09-23 17:15:56 +110083void input_service_request(int type, int plen, void *ctxt);
84void input_userauth_request(int type, int plen, void *ctxt);
85void protocol_error(int type, int plen, void *ctxt);
Damien Millereba71ba2000-04-29 23:57:08 +100086
Damien Millereba71ba2000-04-29 23:57:08 +100087/* helper */
Damien Miller874d77b2000-10-14 16:23:11 +110088Authmethod *authmethod_lookup(const char *name);
89struct passwd *pwcopy(struct passwd *pw);
Damien Miller0bc1bd82000-11-13 22:57:25 +110090int user_key_allowed(struct passwd *pw, Key *key);
Damien Miller874d77b2000-10-14 16:23:11 +110091char *authmethods_get(void);
Damien Millereba71ba2000-04-29 23:57:08 +100092
Damien Miller874d77b2000-10-14 16:23:11 +110093/* auth */
Ben Lindstrom48bd7c12001-01-09 00:35:42 +000094void userauth_banner(void);
Damien Miller874d77b2000-10-14 16:23:11 +110095int userauth_none(Authctxt *authctxt);
96int userauth_passwd(Authctxt *authctxt);
97int userauth_pubkey(Authctxt *authctxt);
98int userauth_kbdint(Authctxt *authctxt);
99
100Authmethod authmethods[] = {
101 {"none",
102 userauth_none,
103 &one},
104 {"publickey",
105 userauth_pubkey,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100106 &options.pubkey_authentication},
Damien Miller874d77b2000-10-14 16:23:11 +1100107 {"keyboard-interactive",
108 userauth_kbdint,
109 &options.kbd_interactive_authentication},
110 {"password",
111 userauth_passwd,
112 &options.password_authentication},
113 {NULL, NULL, NULL}
Damien Millereba71ba2000-04-29 23:57:08 +1000114};
Damien Millereba71ba2000-04-29 23:57:08 +1000115
116/*
Damien Miller874d77b2000-10-14 16:23:11 +1100117 * loop until authctxt->success == TRUE
Damien Millereba71ba2000-04-29 23:57:08 +1000118 */
119
120void
121do_authentication2()
122{
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000123 Authctxt *authctxt = authctxt_new();
124
Damien Miller874d77b2000-10-14 16:23:11 +1100125 x_authctxt = authctxt; /*XXX*/
126
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000127#ifdef AFS
128 /* If machine has AFS, set process authentication group. */
129 if (k_hasafs()) {
130 k_setpag();
131 k_unlog();
132 }
Damien Miller1cead2c2000-05-01 22:55:23 +1000133#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000134 dispatch_init(&protocol_error);
135 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
Damien Miller874d77b2000-10-14 16:23:11 +1100136 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000137 do_authenticated2(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000138}
139
140void
Damien Miller62cee002000-09-23 17:15:56 +1100141protocol_error(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000142{
143 log("auth: protocol error: type %d plen %d", type, plen);
144 packet_start(SSH2_MSG_UNIMPLEMENTED);
145 packet_put_int(0);
146 packet_send();
147 packet_write_wait();
148}
149
150void
Damien Miller62cee002000-09-23 17:15:56 +1100151input_service_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000152{
Damien Miller874d77b2000-10-14 16:23:11 +1100153 Authctxt *authctxt = ctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000154 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000155 int accept = 0;
156 char *service = packet_get_string(&len);
157 packet_done();
158
Damien Miller874d77b2000-10-14 16:23:11 +1100159 if (authctxt == NULL)
160 fatal("input_service_request: no authctxt");
161
Damien Millereba71ba2000-04-29 23:57:08 +1000162 if (strcmp(service, "ssh-userauth") == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100163 if (!authctxt->success) {
Damien Millereba71ba2000-04-29 23:57:08 +1000164 accept = 1;
165 /* now we can handle user-auth requests */
166 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
167 }
168 }
169 /* XXX all other service requests are denied */
170
171 if (accept) {
172 packet_start(SSH2_MSG_SERVICE_ACCEPT);
173 packet_put_cstring(service);
174 packet_send();
175 packet_write_wait();
176 } else {
177 debug("bad service request %s", service);
178 packet_disconnect("bad service request %s", service);
179 }
180 xfree(service);
181}
182
183void
Damien Miller62cee002000-09-23 17:15:56 +1100184input_userauth_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000185{
Damien Miller874d77b2000-10-14 16:23:11 +1100186 Authctxt *authctxt = ctxt;
187 Authmethod *m = NULL;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000188 char *user, *service, *method, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000189 int authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000190
Damien Miller874d77b2000-10-14 16:23:11 +1100191 if (authctxt == NULL)
192 fatal("input_userauth_request: no authctxt");
Damien Millereba71ba2000-04-29 23:57:08 +1000193
Damien Miller874d77b2000-10-14 16:23:11 +1100194 user = packet_get_string(NULL);
195 service = packet_get_string(NULL);
196 method = packet_get_string(NULL);
197 debug("userauth-request for user %s service %s method %s", user, service, method);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000198 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
Damien Miller874d77b2000-10-14 16:23:11 +1100199
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000200 if ((style = strchr(user, ':')) != NULL)
201 *style++ = 0;
202
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000203 if (authctxt->attempt++ == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100204 /* setup auth context */
205 struct passwd *pw = NULL;
206 setproctitle("%s", user);
207 pw = getpwnam(user);
208 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
209 authctxt->pw = pwcopy(pw);
210 authctxt->valid = 1;
211 debug2("input_userauth_request: setting up authctxt for %s", user);
212#ifdef USE_PAM
213 start_pam(pw);
214#endif
215 } else {
216 log("input_userauth_request: illegal user %s", user);
217 }
218 authctxt->user = xstrdup(user);
219 authctxt->service = xstrdup(service);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000220 authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */
Damien Miller874d77b2000-10-14 16:23:11 +1100221 } else if (authctxt->valid) {
222 if (strcmp(user, authctxt->user) != 0 ||
223 strcmp(service, authctxt->service) != 0) {
224 log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)",
225 user, service, authctxt->user, authctxt->service);
226 authctxt->valid = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000227 }
228 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000229 /* reset state */
230 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
231 authctxt->postponed = 0;
Damien Millerb70b61f2000-09-16 16:25:12 +1100232
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000233 /* try to authenticate user */
Damien Miller874d77b2000-10-14 16:23:11 +1100234 m = authmethod_lookup(method);
235 if (m != NULL) {
236 debug2("input_userauth_request: try method %s", method);
237 authenticated = m->userauth(authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100238 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000239 if (!authctxt->valid && authenticated)
240 fatal("INTERNAL ERROR: authenticated invalid user %s",
241 authctxt->user);
Damien Millerb70b61f2000-09-16 16:25:12 +1100242
Damien Miller874d77b2000-10-14 16:23:11 +1100243 /* Special handling for root */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000244 if (authenticated && authctxt->pw->pw_uid == 0 && !auth_root_allowed())
Damien Millereba71ba2000-04-29 23:57:08 +1000245 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000246
247#ifdef USE_PAM
Damien Millerd425d4d2000-10-28 21:05:57 +1100248 if (authenticated && authctxt->user && !do_pam_account(authctxt->user, NULL))
Damien Millerb70b61f2000-09-16 16:25:12 +1100249 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000250#endif /* USE_PAM */
251
Damien Miller874d77b2000-10-14 16:23:11 +1100252 /* Log before sending the reply */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000253 auth_log(authctxt, authenticated, method, " ssh2");
254
255 if (!authctxt->postponed)
256 userauth_reply(authctxt, authenticated);
Damien Miller874d77b2000-10-14 16:23:11 +1100257
258 xfree(service);
259 xfree(user);
260 xfree(method);
261}
262
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000263void
264userauth_banner(void)
265{
266 struct stat st;
267 char *banner = NULL;
268 off_t len, n;
269 int fd;
270
271 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
272 return;
273 if ((fd = open(options.banner, O_RDONLY)) < 0) {
274 error("userauth_banner: open %s failed: %s",
275 options.banner, strerror(errno));
276 return;
277 }
278 if (fstat(fd, &st) < 0)
279 goto done;
280 len = st.st_size;
281 banner = xmalloc(len + 1);
282 if ((n = read(fd, banner, len)) < 0)
283 goto done;
284 banner[n] = '\0';
285 packet_start(SSH2_MSG_USERAUTH_BANNER);
286 packet_put_cstring(banner);
287 packet_put_cstring(""); /* language, unused */
288 packet_send();
289 debug("userauth_banner: sent");
290done:
291 if (banner)
292 xfree(banner);
293 close(fd);
294 return;
295}
Damien Miller874d77b2000-10-14 16:23:11 +1100296
Damien Miller874d77b2000-10-14 16:23:11 +1100297void
298userauth_reply(Authctxt *authctxt, int authenticated)
299{
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000300 char *methods;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000301
Damien Millere247cc42000-05-07 12:03:14 +1000302 /* XXX todo: check if multiple auth methods are needed */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000303 if (authenticated) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000304#ifdef WITH_AIXAUTHENTICATE
305 /* We don't have a pty yet, so just label the line as "ssh" */
Damien Millerd425d4d2000-10-28 21:05:57 +1100306 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
307 get_canonical_hostname(), "ssh", &aixloginmsg) < 0)
Damien Millerd2c208a2000-05-17 22:00:02 +1000308 aixloginmsg = NULL;
309#endif /* WITH_AIXAUTHENTICATE */
Damien Millereba71ba2000-04-29 23:57:08 +1000310 /* turn off userauth */
311 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
312 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
313 packet_send();
314 packet_write_wait();
315 /* now we can break out */
Damien Miller874d77b2000-10-14 16:23:11 +1100316 authctxt->success = 1;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000317 } else {
318 if (authctxt->failures++ > AUTH_FAIL_MAX)
319 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000320 methods = authmethods_get();
Damien Millereba71ba2000-04-29 23:57:08 +1000321 packet_start(SSH2_MSG_USERAUTH_FAILURE);
Damien Miller874d77b2000-10-14 16:23:11 +1100322 packet_put_cstring(methods);
323 packet_put_char(0); /* XXX partial success, unused */
Damien Millereba71ba2000-04-29 23:57:08 +1000324 packet_send();
325 packet_write_wait();
Damien Miller874d77b2000-10-14 16:23:11 +1100326 xfree(methods);
Damien Millereba71ba2000-04-29 23:57:08 +1000327 }
Damien Millereba71ba2000-04-29 23:57:08 +1000328}
329
330int
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();
Ben Lindstrom48bd7c12001-01-09 00:35:42 +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);
342
343#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 Miller874d77b2000-10-14 16:23:11 +1100350 return (sia_validate_user(NULL, saved_argc, saved_argv,
Damien Millerd425d4d2000-10-28 21:05:57 +1100351 get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER",
352 NULL, 0, NULL, "") == SIASUCCESS);
Damien Millerb8c656e2000-06-28 15:22:41 +1000353#else /* !HAVE_OSF_SIA && !USE_PAM */
Damien Miller874d77b2000-10-14 16:23:11 +1100354 return auth_password(authctxt->pw, "");
Damien Millereba71ba2000-04-29 23:57:08 +1000355#endif /* USE_PAM */
356}
Damien Miller874d77b2000-10-14 16:23:11 +1100357
Damien Millereba71ba2000-04-29 23:57:08 +1000358int
Damien Miller874d77b2000-10-14 16:23:11 +1100359userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000360{
361 char *password;
362 int authenticated = 0;
363 int change;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000364 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000365 change = packet_get_char();
366 if (change)
367 log("password change not supported");
368 password = packet_get_string(&len);
369 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100370 if (authctxt->valid &&
371#ifdef HAVE_CYGWIN
372 check_nt_auth(1, authctxt->pw->pw_uid) &&
373#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000374#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100375 auth_pam_password(authctxt->pw, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000376#elif defined(HAVE_OSF_SIA)
377 sia_validate_user(NULL, saved_argc, saved_argv,
Damien Millerd425d4d2000-10-28 21:05:57 +1100378 get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER",
379 NULL, 0, NULL, password) == SIASUCCESS)
Damien Millerb8c656e2000-06-28 15:22:41 +1000380#else /* !USE_PAM && !HAVE_OSF_SIA */
Damien Miller874d77b2000-10-14 16:23:11 +1100381 auth_password(authctxt->pw, password) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000382#endif /* USE_PAM */
383 authenticated = 1;
384 memset(password, 0, len);
385 xfree(password);
386 return authenticated;
387}
Damien Miller874d77b2000-10-14 16:23:11 +1100388
Damien Millereba71ba2000-04-29 23:57:08 +1000389int
Damien Miller874d77b2000-10-14 16:23:11 +1100390userauth_kbdint(Authctxt *authctxt)
391{
392 int authenticated = 0;
393 char *lang = NULL;
394 char *devs = NULL;
395
396 lang = packet_get_string(NULL);
397 devs = packet_get_string(NULL);
398 packet_done();
399
400 debug("keyboard-interactive language %s devs %s", lang, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000401
402 authenticated = auth2_challenge(authctxt, devs);
403
Damien Millerb8481582000-12-03 11:51:51 +1100404#ifdef USE_PAM
405 if (authenticated == 0)
406 authenticated = auth2_pam(authctxt);
407#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100408 xfree(lang);
409 xfree(devs);
410#ifdef HAVE_CYGWIN
411 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
412 return(0);
413#endif
414 return authenticated;
415}
416
417int
418userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000419{
420 Buffer b;
421 Key *key;
422 char *pkalg, *pkblob, *sig;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000423 u_int alen, blen, slen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100424 int have_sig, pktype;
Damien Millereba71ba2000-04-29 23:57:08 +1000425 int authenticated = 0;
426
Damien Miller874d77b2000-10-14 16:23:11 +1100427 if (!authctxt->valid) {
428 debug2("userauth_pubkey: disabled because of invalid user");
Damien Millereba71ba2000-04-29 23:57:08 +1000429 return 0;
430 }
431 have_sig = packet_get_char();
Ben Lindstromd121f612000-12-03 17:00:47 +0000432 if (datafellows & SSH_BUG_PKAUTH) {
433 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
434 /* no explicit pkalg given */
435 pkblob = packet_get_string(&blen);
436 buffer_init(&b);
437 buffer_append(&b, pkblob, blen);
438 /* so we have to extract the pkalg from the pkblob */
439 pkalg = buffer_get_string(&b, &alen);
440 buffer_free(&b);
441 } else {
442 pkalg = packet_get_string(&alen);
443 pkblob = packet_get_string(&blen);
444 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100445 pktype = key_type_from_name(pkalg);
446 if (pktype == KEY_UNSPEC) {
Ben Lindstromd121f612000-12-03 17:00:47 +0000447 /* this is perfectly legal */
448 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
Damien Miller874d77b2000-10-14 16:23:11 +1100449 xfree(pkalg);
Ben Lindstromd121f612000-12-03 17:00:47 +0000450 xfree(pkblob);
Damien Millereba71ba2000-04-29 23:57:08 +1000451 return 0;
452 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100453 key = key_from_blob(pkblob, blen);
Damien Millereba71ba2000-04-29 23:57:08 +1000454 if (key != NULL) {
455 if (have_sig) {
456 sig = packet_get_string(&slen);
457 packet_done();
458 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100459 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000460 buffer_append(&b, session_id2, session_id2_len);
Damien Miller50a41ed2000-10-16 12:14:42 +1100461 } else {
462 buffer_put_string(&b, session_id2, session_id2_len);
Damien Miller6536c7d2000-06-22 21:32:31 +1000463 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000464 /* reconstruct packet */
Damien Millereba71ba2000-04-29 23:57:08 +1000465 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller874d77b2000-10-14 16:23:11 +1100466 buffer_put_cstring(&b, authctxt->user);
Damien Millerf6d9e222000-06-18 14:50:44 +1000467 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000468 datafellows & SSH_BUG_PKSERVICE ?
Damien Millerf6d9e222000-06-18 14:50:44 +1000469 "ssh-userauth" :
Damien Miller874d77b2000-10-14 16:23:11 +1100470 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000471 if (datafellows & SSH_BUG_PKAUTH) {
472 buffer_put_char(&b, have_sig);
473 } else {
474 buffer_put_cstring(&b, "publickey");
475 buffer_put_char(&b, have_sig);
476 buffer_put_cstring(&b, key_ssh_name(key));
477 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000478 buffer_put_string(&b, pkblob, blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100479#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000480 buffer_dump(&b);
481#endif
482 /* test for correct signature */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100483 if (user_key_allowed(authctxt->pw, key) &&
484 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000485 authenticated = 1;
486 buffer_clear(&b);
487 xfree(sig);
488 } else {
Damien Miller874d77b2000-10-14 16:23:11 +1100489 debug("test whether pkalg/pkblob are acceptable");
Damien Millereba71ba2000-04-29 23:57:08 +1000490 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100491
Damien Millereba71ba2000-04-29 23:57:08 +1000492 /* XXX fake reply and always send PK_OK ? */
Damien Millere247cc42000-05-07 12:03:14 +1000493 /*
494 * XXX this allows testing whether a user is allowed
495 * to login: if you happen to have a valid pubkey this
496 * message is sent. the message is NEVER sent at all
497 * if a user is not allowed to login. is this an
498 * issue? -markus
499 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100500 if (user_key_allowed(authctxt->pw, key)) {
Damien Millereba71ba2000-04-29 23:57:08 +1000501 packet_start(SSH2_MSG_USERAUTH_PK_OK);
502 packet_put_string(pkalg, alen);
503 packet_put_string(pkblob, blen);
504 packet_send();
505 packet_write_wait();
506 authenticated = -1;
507 }
508 }
Damien Miller874d77b2000-10-14 16:23:11 +1100509 if (authenticated != 1)
510 auth_clear_options();
Damien Millereba71ba2000-04-29 23:57:08 +1000511 key_free(key);
512 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100513 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
Damien Millereba71ba2000-04-29 23:57:08 +1000514 xfree(pkalg);
515 xfree(pkblob);
Damien Miller874d77b2000-10-14 16:23:11 +1100516#ifdef HAVE_CYGWIN
517 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
518 return(0);
519#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000520 return authenticated;
521}
522
Damien Miller874d77b2000-10-14 16:23:11 +1100523/* get current user */
Damien Millereba71ba2000-04-29 23:57:08 +1000524
525struct passwd*
526auth_get_user(void)
527{
Damien Miller874d77b2000-10-14 16:23:11 +1100528 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000529}
530
Damien Miller874d77b2000-10-14 16:23:11 +1100531#define DELIM ","
Damien Millereba71ba2000-04-29 23:57:08 +1000532
Damien Miller874d77b2000-10-14 16:23:11 +1100533char *
534authmethods_get(void)
535{
536 Authmethod *method = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000537 u_int size = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100538 char *list;
539
540 for (method = authmethods; method->name != NULL; method++) {
541 if (strcmp(method->name, "none") == 0)
542 continue;
543 if (method->enabled != NULL && *(method->enabled) != 0) {
544 if (size != 0)
545 size += strlen(DELIM);
546 size += strlen(method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000547 }
548 }
Damien Miller874d77b2000-10-14 16:23:11 +1100549 size++; /* trailing '\0' */
550 list = xmalloc(size);
551 list[0] = '\0';
552
553 for (method = authmethods; method->name != NULL; method++) {
554 if (strcmp(method->name, "none") == 0)
555 continue;
556 if (method->enabled != NULL && *(method->enabled) != 0) {
557 if (list[0] != '\0')
558 strlcat(list, DELIM, size);
559 strlcat(list, method->name, size);
560 }
561 }
562 return list;
563}
564
565Authmethod *
566authmethod_lookup(const char *name)
567{
568 Authmethod *method = NULL;
569 if (name != NULL)
570 for (method = authmethods; method->name != NULL; method++)
571 if (method->enabled != NULL &&
572 *(method->enabled) != 0 &&
573 strcmp(name, method->name) == 0)
574 return method;
575 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
576 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000577}
578
579/* return 1 if user allows given key */
580int
Damien Miller0bc1bd82000-11-13 22:57:25 +1100581user_key_allowed(struct passwd *pw, Key *key)
Damien Millereba71ba2000-04-29 23:57:08 +1000582{
583 char line[8192], file[1024];
584 int found_key = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000585 FILE *f;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000586 u_long linenum = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000587 struct stat st;
588 Key *found;
589
Damien Miller874d77b2000-10-14 16:23:11 +1100590 if (pw == NULL)
591 return 0;
592
Damien Millereba71ba2000-04-29 23:57:08 +1000593 /* Temporarily use the user's uid. */
594 temporarily_use_uid(pw->pw_uid);
595
596 /* The authorized keys. */
597 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
598 SSH_USER_PERMITTED_KEYS2);
599
600 /* Fail quietly if file does not exist */
601 if (stat(file, &st) < 0) {
602 /* Restore the privileged uid. */
603 restore_uid();
604 return 0;
605 }
606 /* Open the file containing the authorized keys. */
607 f = fopen(file, "r");
608 if (!f) {
609 /* Restore the privileged uid. */
610 restore_uid();
611 return 0;
612 }
613 if (options.strict_modes) {
614 int fail = 0;
615 char buf[1024];
616 /* Check open file in order to avoid open/stat races */
617 if (fstat(fileno(f), &st) < 0 ||
618 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
619 (st.st_mode & 022) != 0) {
Damien Millerd3444942000-09-30 14:20:03 +1100620 snprintf(buf, sizeof buf,
621 "%s authentication refused for %.100s: "
622 "bad ownership or modes for '%s'.",
623 key_type(key), pw->pw_name, file);
Damien Millereba71ba2000-04-29 23:57:08 +1000624 fail = 1;
625 } else {
626 /* Check path to SSH_USER_PERMITTED_KEYS */
627 int i;
628 static const char *check[] = {
629 "", SSH_USER_DIR, NULL
630 };
631 for (i = 0; check[i]; i++) {
632 snprintf(line, sizeof line, "%.500s/%.100s",
633 pw->pw_dir, check[i]);
634 if (stat(line, &st) < 0 ||
635 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
636 (st.st_mode & 022) != 0) {
637 snprintf(buf, sizeof buf,
Damien Millerd3444942000-09-30 14:20:03 +1100638 "%s authentication refused for %.100s: "
Damien Millereba71ba2000-04-29 23:57:08 +1000639 "bad ownership or modes for '%s'.",
Damien Millerd3444942000-09-30 14:20:03 +1100640 key_type(key), pw->pw_name, line);
Damien Millereba71ba2000-04-29 23:57:08 +1000641 fail = 1;
642 break;
643 }
644 }
645 }
646 if (fail) {
Damien Millereba71ba2000-04-29 23:57:08 +1000647 fclose(f);
Damien Miller37023962000-07-11 17:31:38 +1000648 log("%s",buf);
Damien Millereba71ba2000-04-29 23:57:08 +1000649 restore_uid();
650 return 0;
651 }
652 }
653 found_key = 0;
Damien Millerd3444942000-09-30 14:20:03 +1100654 found = key_new(key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000655
656 while (fgets(line, sizeof(line), f)) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000657 char *cp, *options = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000658 linenum++;
659 /* Skip leading whitespace, empty and comment lines. */
660 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
661 ;
662 if (!*cp || *cp == '\n' || *cp == '#')
663 continue;
Damien Millerf6d9e222000-06-18 14:50:44 +1000664
Damien Miller0bc1bd82000-11-13 22:57:25 +1100665 if (key_read(found, &cp) == -1) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000666 /* no key? check if there are options for this key */
667 int quoted = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100668 debug2("user_key_allowed: check options: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000669 options = cp;
670 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
671 if (*cp == '\\' && cp[1] == '"')
672 cp++; /* Skip both */
673 else if (*cp == '"')
674 quoted = !quoted;
675 }
676 /* Skip remaining whitespace. */
677 for (; *cp == ' ' || *cp == '\t'; cp++)
678 ;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100679 if (key_read(found, &cp) == -1) {
680 debug2("user_key_allowed: advance: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000681 /* still no key? advance to next line*/
682 continue;
683 }
684 }
685 if (key_equal(found, key) &&
686 auth_parse_options(pw, options, linenum) == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000687 found_key = 1;
688 debug("matching key found: file %s, line %ld",
689 file, linenum);
690 break;
691 }
692 }
693 restore_uid();
694 fclose(f);
695 key_free(found);
696 return found_key;
697}