blob: bab1c2ed83777a33ae092f3359186aaa55209a12 [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
Damien Miller22e22bf2001-01-19 15:46:38 +1100213 start_pam(pw->pw_name);
Damien Miller874d77b2000-10-14 16:23:11 +1100214#endif
215 } else {
216 log("input_userauth_request: illegal user %s", user);
Damien Miller22e22bf2001-01-19 15:46:38 +1100217#ifdef USE_PAM
218 start_pam("NOUSER");
219#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100220 }
221 authctxt->user = xstrdup(user);
222 authctxt->service = xstrdup(service);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000223 authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */
Damien Miller874d77b2000-10-14 16:23:11 +1100224 } else if (authctxt->valid) {
225 if (strcmp(user, authctxt->user) != 0 ||
226 strcmp(service, authctxt->service) != 0) {
227 log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)",
228 user, service, authctxt->user, authctxt->service);
229 authctxt->valid = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000230 }
231 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000232 /* reset state */
233 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
234 authctxt->postponed = 0;
Damien Millerb70b61f2000-09-16 16:25:12 +1100235
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000236 /* try to authenticate user */
Damien Miller874d77b2000-10-14 16:23:11 +1100237 m = authmethod_lookup(method);
238 if (m != NULL) {
239 debug2("input_userauth_request: try method %s", method);
240 authenticated = m->userauth(authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100241 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000242 if (!authctxt->valid && authenticated)
243 fatal("INTERNAL ERROR: authenticated invalid user %s",
244 authctxt->user);
Damien Millerb70b61f2000-09-16 16:25:12 +1100245
Damien Miller874d77b2000-10-14 16:23:11 +1100246 /* Special handling for root */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000247 if (authenticated && authctxt->pw->pw_uid == 0 && !auth_root_allowed())
Damien Millereba71ba2000-04-29 23:57:08 +1000248 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000249
250#ifdef USE_PAM
Damien Millerd425d4d2000-10-28 21:05:57 +1100251 if (authenticated && authctxt->user && !do_pam_account(authctxt->user, NULL))
Damien Millerb70b61f2000-09-16 16:25:12 +1100252 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000253#endif /* USE_PAM */
254
Damien Miller874d77b2000-10-14 16:23:11 +1100255 /* Log before sending the reply */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000256 auth_log(authctxt, authenticated, method, " ssh2");
257
258 if (!authctxt->postponed)
259 userauth_reply(authctxt, authenticated);
Damien Miller874d77b2000-10-14 16:23:11 +1100260
261 xfree(service);
262 xfree(user);
263 xfree(method);
264}
265
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000266void
267userauth_banner(void)
268{
269 struct stat st;
270 char *banner = NULL;
271 off_t len, n;
272 int fd;
273
274 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
275 return;
276 if ((fd = open(options.banner, O_RDONLY)) < 0) {
277 error("userauth_banner: open %s failed: %s",
278 options.banner, strerror(errno));
279 return;
280 }
281 if (fstat(fd, &st) < 0)
282 goto done;
283 len = st.st_size;
284 banner = xmalloc(len + 1);
285 if ((n = read(fd, banner, len)) < 0)
286 goto done;
287 banner[n] = '\0';
288 packet_start(SSH2_MSG_USERAUTH_BANNER);
289 packet_put_cstring(banner);
290 packet_put_cstring(""); /* language, unused */
291 packet_send();
292 debug("userauth_banner: sent");
293done:
294 if (banner)
295 xfree(banner);
296 close(fd);
297 return;
298}
Damien Miller874d77b2000-10-14 16:23:11 +1100299
Damien Miller874d77b2000-10-14 16:23:11 +1100300void
301userauth_reply(Authctxt *authctxt, int authenticated)
302{
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000303 char *methods;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000304
Damien Millere247cc42000-05-07 12:03:14 +1000305 /* XXX todo: check if multiple auth methods are needed */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000306 if (authenticated) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000307#ifdef WITH_AIXAUTHENTICATE
308 /* We don't have a pty yet, so just label the line as "ssh" */
Damien Millerd425d4d2000-10-28 21:05:57 +1100309 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
310 get_canonical_hostname(), "ssh", &aixloginmsg) < 0)
Damien Millerd2c208a2000-05-17 22:00:02 +1000311 aixloginmsg = NULL;
312#endif /* WITH_AIXAUTHENTICATE */
Damien Millereba71ba2000-04-29 23:57:08 +1000313 /* turn off userauth */
314 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
315 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
316 packet_send();
317 packet_write_wait();
318 /* now we can break out */
Damien Miller874d77b2000-10-14 16:23:11 +1100319 authctxt->success = 1;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000320 } else {
321 if (authctxt->failures++ > AUTH_FAIL_MAX)
322 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000323 methods = authmethods_get();
Damien Millereba71ba2000-04-29 23:57:08 +1000324 packet_start(SSH2_MSG_USERAUTH_FAILURE);
Damien Miller874d77b2000-10-14 16:23:11 +1100325 packet_put_cstring(methods);
326 packet_put_char(0); /* XXX partial success, unused */
Damien Millereba71ba2000-04-29 23:57:08 +1000327 packet_send();
328 packet_write_wait();
Damien Miller874d77b2000-10-14 16:23:11 +1100329 xfree(methods);
Damien Millereba71ba2000-04-29 23:57:08 +1000330 }
Damien Millereba71ba2000-04-29 23:57:08 +1000331}
332
333int
Damien Miller874d77b2000-10-14 16:23:11 +1100334userauth_none(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000335{
Damien Miller874d77b2000-10-14 16:23:11 +1100336 /* disable method "none", only allowed one time */
337 Authmethod *m = authmethod_lookup("none");
338 if (m != NULL)
339 m->enabled = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000340 packet_done();
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000341 userauth_banner();
Damien Millerb8c656e2000-06-28 15:22:41 +1000342
Damien Miller874d77b2000-10-14 16:23:11 +1100343 if (authctxt->valid == 0)
344 return(0);
345
346#ifdef HAVE_CYGWIN
347 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
348 return(0);
349#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000350#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100351 return auth_pam_password(authctxt->pw, "");
Damien Millerb8c656e2000-06-28 15:22:41 +1000352#elif defined(HAVE_OSF_SIA)
Damien Miller874d77b2000-10-14 16:23:11 +1100353 return (sia_validate_user(NULL, saved_argc, saved_argv,
Damien Millerd425d4d2000-10-28 21:05:57 +1100354 get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER",
355 NULL, 0, NULL, "") == SIASUCCESS);
Damien Millerb8c656e2000-06-28 15:22:41 +1000356#else /* !HAVE_OSF_SIA && !USE_PAM */
Damien Miller874d77b2000-10-14 16:23:11 +1100357 return auth_password(authctxt->pw, "");
Damien Millereba71ba2000-04-29 23:57:08 +1000358#endif /* USE_PAM */
359}
Damien Miller874d77b2000-10-14 16:23:11 +1100360
Damien Millereba71ba2000-04-29 23:57:08 +1000361int
Damien Miller874d77b2000-10-14 16:23:11 +1100362userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000363{
364 char *password;
365 int authenticated = 0;
366 int change;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000367 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000368 change = packet_get_char();
369 if (change)
370 log("password change not supported");
371 password = packet_get_string(&len);
372 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100373 if (authctxt->valid &&
374#ifdef HAVE_CYGWIN
375 check_nt_auth(1, authctxt->pw->pw_uid) &&
376#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000377#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100378 auth_pam_password(authctxt->pw, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000379#elif defined(HAVE_OSF_SIA)
380 sia_validate_user(NULL, saved_argc, saved_argv,
Damien Millerd425d4d2000-10-28 21:05:57 +1100381 get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER",
382 NULL, 0, NULL, password) == SIASUCCESS)
Damien Millerb8c656e2000-06-28 15:22:41 +1000383#else /* !USE_PAM && !HAVE_OSF_SIA */
Damien Miller874d77b2000-10-14 16:23:11 +1100384 auth_password(authctxt->pw, password) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000385#endif /* USE_PAM */
386 authenticated = 1;
387 memset(password, 0, len);
388 xfree(password);
389 return authenticated;
390}
Damien Miller874d77b2000-10-14 16:23:11 +1100391
Damien Millereba71ba2000-04-29 23:57:08 +1000392int
Damien Miller874d77b2000-10-14 16:23:11 +1100393userauth_kbdint(Authctxt *authctxt)
394{
395 int authenticated = 0;
396 char *lang = NULL;
397 char *devs = NULL;
398
399 lang = packet_get_string(NULL);
400 devs = packet_get_string(NULL);
401 packet_done();
402
403 debug("keyboard-interactive language %s devs %s", lang, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000404
405 authenticated = auth2_challenge(authctxt, devs);
406
Damien Millerb8481582000-12-03 11:51:51 +1100407#ifdef USE_PAM
408 if (authenticated == 0)
409 authenticated = auth2_pam(authctxt);
410#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100411 xfree(lang);
412 xfree(devs);
413#ifdef HAVE_CYGWIN
414 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
415 return(0);
416#endif
417 return authenticated;
418}
419
420int
421userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000422{
423 Buffer b;
424 Key *key;
425 char *pkalg, *pkblob, *sig;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000426 u_int alen, blen, slen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100427 int have_sig, pktype;
Damien Millereba71ba2000-04-29 23:57:08 +1000428 int authenticated = 0;
429
Damien Miller874d77b2000-10-14 16:23:11 +1100430 if (!authctxt->valid) {
431 debug2("userauth_pubkey: disabled because of invalid user");
Damien Millereba71ba2000-04-29 23:57:08 +1000432 return 0;
433 }
434 have_sig = packet_get_char();
Ben Lindstromd121f612000-12-03 17:00:47 +0000435 if (datafellows & SSH_BUG_PKAUTH) {
436 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
437 /* no explicit pkalg given */
438 pkblob = packet_get_string(&blen);
439 buffer_init(&b);
440 buffer_append(&b, pkblob, blen);
441 /* so we have to extract the pkalg from the pkblob */
442 pkalg = buffer_get_string(&b, &alen);
443 buffer_free(&b);
444 } else {
445 pkalg = packet_get_string(&alen);
446 pkblob = packet_get_string(&blen);
447 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100448 pktype = key_type_from_name(pkalg);
449 if (pktype == KEY_UNSPEC) {
Ben Lindstromd121f612000-12-03 17:00:47 +0000450 /* this is perfectly legal */
451 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
Damien Miller874d77b2000-10-14 16:23:11 +1100452 xfree(pkalg);
Ben Lindstromd121f612000-12-03 17:00:47 +0000453 xfree(pkblob);
Damien Millereba71ba2000-04-29 23:57:08 +1000454 return 0;
455 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100456 key = key_from_blob(pkblob, blen);
Damien Millereba71ba2000-04-29 23:57:08 +1000457 if (key != NULL) {
458 if (have_sig) {
459 sig = packet_get_string(&slen);
460 packet_done();
461 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100462 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000463 buffer_append(&b, session_id2, session_id2_len);
Damien Miller50a41ed2000-10-16 12:14:42 +1100464 } else {
465 buffer_put_string(&b, session_id2, session_id2_len);
Damien Miller6536c7d2000-06-22 21:32:31 +1000466 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000467 /* reconstruct packet */
Damien Millereba71ba2000-04-29 23:57:08 +1000468 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller874d77b2000-10-14 16:23:11 +1100469 buffer_put_cstring(&b, authctxt->user);
Damien Millerf6d9e222000-06-18 14:50:44 +1000470 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000471 datafellows & SSH_BUG_PKSERVICE ?
Damien Millerf6d9e222000-06-18 14:50:44 +1000472 "ssh-userauth" :
Damien Miller874d77b2000-10-14 16:23:11 +1100473 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000474 if (datafellows & SSH_BUG_PKAUTH) {
475 buffer_put_char(&b, have_sig);
476 } else {
477 buffer_put_cstring(&b, "publickey");
478 buffer_put_char(&b, have_sig);
479 buffer_put_cstring(&b, key_ssh_name(key));
480 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000481 buffer_put_string(&b, pkblob, blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100482#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000483 buffer_dump(&b);
484#endif
485 /* test for correct signature */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100486 if (user_key_allowed(authctxt->pw, key) &&
487 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000488 authenticated = 1;
489 buffer_clear(&b);
490 xfree(sig);
491 } else {
Damien Miller874d77b2000-10-14 16:23:11 +1100492 debug("test whether pkalg/pkblob are acceptable");
Damien Millereba71ba2000-04-29 23:57:08 +1000493 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100494
Damien Millereba71ba2000-04-29 23:57:08 +1000495 /* XXX fake reply and always send PK_OK ? */
Damien Millere247cc42000-05-07 12:03:14 +1000496 /*
497 * XXX this allows testing whether a user is allowed
498 * to login: if you happen to have a valid pubkey this
499 * message is sent. the message is NEVER sent at all
500 * if a user is not allowed to login. is this an
501 * issue? -markus
502 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100503 if (user_key_allowed(authctxt->pw, key)) {
Damien Millereba71ba2000-04-29 23:57:08 +1000504 packet_start(SSH2_MSG_USERAUTH_PK_OK);
505 packet_put_string(pkalg, alen);
506 packet_put_string(pkblob, blen);
507 packet_send();
508 packet_write_wait();
509 authenticated = -1;
510 }
511 }
Damien Miller874d77b2000-10-14 16:23:11 +1100512 if (authenticated != 1)
513 auth_clear_options();
Damien Millereba71ba2000-04-29 23:57:08 +1000514 key_free(key);
515 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100516 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
Damien Millereba71ba2000-04-29 23:57:08 +1000517 xfree(pkalg);
518 xfree(pkblob);
Damien Miller874d77b2000-10-14 16:23:11 +1100519#ifdef HAVE_CYGWIN
520 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
521 return(0);
522#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000523 return authenticated;
524}
525
Damien Miller874d77b2000-10-14 16:23:11 +1100526/* get current user */
Damien Millereba71ba2000-04-29 23:57:08 +1000527
528struct passwd*
529auth_get_user(void)
530{
Damien Miller874d77b2000-10-14 16:23:11 +1100531 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000532}
533
Damien Miller874d77b2000-10-14 16:23:11 +1100534#define DELIM ","
Damien Millereba71ba2000-04-29 23:57:08 +1000535
Damien Miller874d77b2000-10-14 16:23:11 +1100536char *
537authmethods_get(void)
538{
539 Authmethod *method = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000540 u_int size = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100541 char *list;
542
543 for (method = authmethods; method->name != NULL; method++) {
544 if (strcmp(method->name, "none") == 0)
545 continue;
546 if (method->enabled != NULL && *(method->enabled) != 0) {
547 if (size != 0)
548 size += strlen(DELIM);
549 size += strlen(method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000550 }
551 }
Damien Miller874d77b2000-10-14 16:23:11 +1100552 size++; /* trailing '\0' */
553 list = xmalloc(size);
554 list[0] = '\0';
555
556 for (method = authmethods; method->name != NULL; method++) {
557 if (strcmp(method->name, "none") == 0)
558 continue;
559 if (method->enabled != NULL && *(method->enabled) != 0) {
560 if (list[0] != '\0')
561 strlcat(list, DELIM, size);
562 strlcat(list, method->name, size);
563 }
564 }
565 return list;
566}
567
568Authmethod *
569authmethod_lookup(const char *name)
570{
571 Authmethod *method = NULL;
572 if (name != NULL)
573 for (method = authmethods; method->name != NULL; method++)
574 if (method->enabled != NULL &&
575 *(method->enabled) != 0 &&
576 strcmp(name, method->name) == 0)
577 return method;
578 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
579 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000580}
581
582/* return 1 if user allows given key */
583int
Damien Miller0bc1bd82000-11-13 22:57:25 +1100584user_key_allowed(struct passwd *pw, Key *key)
Damien Millereba71ba2000-04-29 23:57:08 +1000585{
586 char line[8192], file[1024];
587 int found_key = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000588 FILE *f;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000589 u_long linenum = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000590 struct stat st;
591 Key *found;
592
Damien Miller874d77b2000-10-14 16:23:11 +1100593 if (pw == NULL)
594 return 0;
595
Damien Millereba71ba2000-04-29 23:57:08 +1000596 /* Temporarily use the user's uid. */
597 temporarily_use_uid(pw->pw_uid);
598
599 /* The authorized keys. */
600 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
601 SSH_USER_PERMITTED_KEYS2);
602
603 /* Fail quietly if file does not exist */
604 if (stat(file, &st) < 0) {
605 /* Restore the privileged uid. */
606 restore_uid();
607 return 0;
608 }
609 /* Open the file containing the authorized keys. */
610 f = fopen(file, "r");
611 if (!f) {
612 /* Restore the privileged uid. */
613 restore_uid();
614 return 0;
615 }
616 if (options.strict_modes) {
617 int fail = 0;
618 char buf[1024];
619 /* Check open file in order to avoid open/stat races */
620 if (fstat(fileno(f), &st) < 0 ||
621 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
622 (st.st_mode & 022) != 0) {
Damien Millerd3444942000-09-30 14:20:03 +1100623 snprintf(buf, sizeof buf,
624 "%s authentication refused for %.100s: "
625 "bad ownership or modes for '%s'.",
626 key_type(key), pw->pw_name, file);
Damien Millereba71ba2000-04-29 23:57:08 +1000627 fail = 1;
628 } else {
629 /* Check path to SSH_USER_PERMITTED_KEYS */
630 int i;
631 static const char *check[] = {
632 "", SSH_USER_DIR, NULL
633 };
634 for (i = 0; check[i]; i++) {
635 snprintf(line, sizeof line, "%.500s/%.100s",
636 pw->pw_dir, check[i]);
637 if (stat(line, &st) < 0 ||
638 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
639 (st.st_mode & 022) != 0) {
640 snprintf(buf, sizeof buf,
Damien Millerd3444942000-09-30 14:20:03 +1100641 "%s authentication refused for %.100s: "
Damien Millereba71ba2000-04-29 23:57:08 +1000642 "bad ownership or modes for '%s'.",
Damien Millerd3444942000-09-30 14:20:03 +1100643 key_type(key), pw->pw_name, line);
Damien Millereba71ba2000-04-29 23:57:08 +1000644 fail = 1;
645 break;
646 }
647 }
648 }
649 if (fail) {
Damien Millereba71ba2000-04-29 23:57:08 +1000650 fclose(f);
Damien Miller37023962000-07-11 17:31:38 +1000651 log("%s",buf);
Damien Millereba71ba2000-04-29 23:57:08 +1000652 restore_uid();
653 return 0;
654 }
655 }
656 found_key = 0;
Damien Millerd3444942000-09-30 14:20:03 +1100657 found = key_new(key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000658
659 while (fgets(line, sizeof(line), f)) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000660 char *cp, *options = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000661 linenum++;
662 /* Skip leading whitespace, empty and comment lines. */
663 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
664 ;
665 if (!*cp || *cp == '\n' || *cp == '#')
666 continue;
Damien Millerf6d9e222000-06-18 14:50:44 +1000667
Damien Miller0bc1bd82000-11-13 22:57:25 +1100668 if (key_read(found, &cp) == -1) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000669 /* no key? check if there are options for this key */
670 int quoted = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100671 debug2("user_key_allowed: check options: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000672 options = cp;
673 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
674 if (*cp == '\\' && cp[1] == '"')
675 cp++; /* Skip both */
676 else if (*cp == '"')
677 quoted = !quoted;
678 }
679 /* Skip remaining whitespace. */
680 for (; *cp == ' ' || *cp == '\t'; cp++)
681 ;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100682 if (key_read(found, &cp) == -1) {
683 debug2("user_key_allowed: advance: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000684 /* still no key? advance to next line*/
685 continue;
686 }
687 }
688 if (key_equal(found, key) &&
689 auth_parse_options(pw, options, linenum) == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000690 found_key = 1;
691 debug("matching key found: file %s, line %ld",
692 file, linenum);
693 break;
694 }
695 }
696 restore_uid();
697 fclose(f);
698 key_free(found);
699 return found_key;
700}