blob: cff34c602a87435b92244db3e6a4479e0c4ca643 [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 Lindstrom95fb2dd2001-01-23 03:12:10 +000026RCSID("$OpenBSD: auth2.c,v 1.34 2001/01/22 23:06:39 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
Damien Millereba71ba2000-04-29 23:57:08 +100033#include <openssl/evp.h>
34
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100036#include "xmalloc.h"
37#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100038#include "pty.h"
39#include "packet.h"
40#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000041#include "log.h"
Damien Millereba71ba2000-04-29 23:57:08 +100042#include "servconf.h"
43#include "compat.h"
44#include "channels.h"
45#include "bufaux.h"
Damien Millereba71ba2000-04-29 23:57:08 +100046#include "auth.h"
47#include "session.h"
48#include "dispatch.h"
49#include "auth.h"
50#include "key.h"
Ben Lindstrom6d40c0f2001-01-29 09:02:24 +000051#include "cipher.h"
Damien Millereba71ba2000-04-29 23:57:08 +100052#include "kex.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000053#include "pathnames.h"
Damien Millereba71ba2000-04-29 23:57:08 +100054#include "uidswap.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100055#include "auth-options.h"
Damien Millereba71ba2000-04-29 23:57:08 +100056
57/* import */
58extern ServerOptions options;
Ben Lindstrom46c16222000-12-22 01:43:59 +000059extern u_char *session_id2;
Damien Millereba71ba2000-04-29 23:57:08 +100060extern int session_id2_len;
61
Damien Miller874d77b2000-10-14 16:23:11 +110062#ifdef WITH_AIXAUTHENTICATE
63extern char *aixloginmsg;
64#endif
65#ifdef HAVE_OSF_SIA
66extern int saved_argc;
67extern char **saved_argv;
68#endif
69
70static Authctxt *x_authctxt = NULL;
71static int one = 1;
72
73typedef struct Authmethod Authmethod;
74struct Authmethod {
75 char *name;
76 int (*userauth)(Authctxt *authctxt);
77 int *enabled;
78};
79
Damien Millereba71ba2000-04-29 23:57:08 +100080/* protocol */
81
Damien Miller62cee002000-09-23 17:15:56 +110082void input_service_request(int type, int plen, void *ctxt);
83void input_userauth_request(int type, int plen, void *ctxt);
84void protocol_error(int type, int plen, void *ctxt);
Damien Millereba71ba2000-04-29 23:57:08 +100085
Damien Millereba71ba2000-04-29 23:57:08 +100086/* helper */
Damien Miller874d77b2000-10-14 16:23:11 +110087Authmethod *authmethod_lookup(const char *name);
88struct passwd *pwcopy(struct passwd *pw);
Damien Miller0bc1bd82000-11-13 22:57:25 +110089int user_key_allowed(struct passwd *pw, Key *key);
Damien Miller874d77b2000-10-14 16:23:11 +110090char *authmethods_get(void);
Damien Millereba71ba2000-04-29 23:57:08 +100091
Damien Miller874d77b2000-10-14 16:23:11 +110092/* auth */
Ben Lindstrom48bd7c12001-01-09 00:35:42 +000093void userauth_banner(void);
Damien Miller874d77b2000-10-14 16:23:11 +110094int userauth_none(Authctxt *authctxt);
95int userauth_passwd(Authctxt *authctxt);
96int userauth_pubkey(Authctxt *authctxt);
97int userauth_kbdint(Authctxt *authctxt);
98
99Authmethod authmethods[] = {
100 {"none",
101 userauth_none,
102 &one},
103 {"publickey",
104 userauth_pubkey,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100105 &options.pubkey_authentication},
Damien Miller874d77b2000-10-14 16:23:11 +1100106 {"keyboard-interactive",
107 userauth_kbdint,
108 &options.kbd_interactive_authentication},
109 {"password",
110 userauth_passwd,
111 &options.password_authentication},
112 {NULL, NULL, NULL}
Damien Millereba71ba2000-04-29 23:57:08 +1000113};
Damien Millereba71ba2000-04-29 23:57:08 +1000114
115/*
Damien Miller874d77b2000-10-14 16:23:11 +1100116 * loop until authctxt->success == TRUE
Damien Millereba71ba2000-04-29 23:57:08 +1000117 */
118
119void
120do_authentication2()
121{
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000122 Authctxt *authctxt = authctxt_new();
123
Damien Miller874d77b2000-10-14 16:23:11 +1100124 x_authctxt = authctxt; /*XXX*/
125
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000126 /* challenge-reponse is implemented via keyboard interactive */
127 if (options.challenge_reponse_authentication)
128 options.kbd_interactive_authentication = 1;
129
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000130#ifdef AFS
131 /* If machine has AFS, set process authentication group. */
132 if (k_hasafs()) {
133 k_setpag();
134 k_unlog();
135 }
Damien Miller1cead2c2000-05-01 22:55:23 +1000136#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000137 dispatch_init(&protocol_error);
138 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
Damien Miller874d77b2000-10-14 16:23:11 +1100139 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000140 do_authenticated2(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000141}
142
143void
Damien Miller62cee002000-09-23 17:15:56 +1100144protocol_error(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000145{
146 log("auth: protocol error: type %d plen %d", type, plen);
147 packet_start(SSH2_MSG_UNIMPLEMENTED);
148 packet_put_int(0);
149 packet_send();
150 packet_write_wait();
151}
152
153void
Damien Miller62cee002000-09-23 17:15:56 +1100154input_service_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000155{
Damien Miller874d77b2000-10-14 16:23:11 +1100156 Authctxt *authctxt = ctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000157 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000158 int accept = 0;
159 char *service = packet_get_string(&len);
160 packet_done();
161
Damien Miller874d77b2000-10-14 16:23:11 +1100162 if (authctxt == NULL)
163 fatal("input_service_request: no authctxt");
164
Damien Millereba71ba2000-04-29 23:57:08 +1000165 if (strcmp(service, "ssh-userauth") == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100166 if (!authctxt->success) {
Damien Millereba71ba2000-04-29 23:57:08 +1000167 accept = 1;
168 /* now we can handle user-auth requests */
169 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
170 }
171 }
172 /* XXX all other service requests are denied */
173
174 if (accept) {
175 packet_start(SSH2_MSG_SERVICE_ACCEPT);
176 packet_put_cstring(service);
177 packet_send();
178 packet_write_wait();
179 } else {
180 debug("bad service request %s", service);
181 packet_disconnect("bad service request %s", service);
182 }
183 xfree(service);
184}
185
186void
Damien Miller62cee002000-09-23 17:15:56 +1100187input_userauth_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000188{
Damien Miller874d77b2000-10-14 16:23:11 +1100189 Authctxt *authctxt = ctxt;
190 Authmethod *m = NULL;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000191 char *user, *service, *method, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000192 int authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000193
Damien Miller874d77b2000-10-14 16:23:11 +1100194 if (authctxt == NULL)
195 fatal("input_userauth_request: no authctxt");
Damien Millereba71ba2000-04-29 23:57:08 +1000196
Damien Miller874d77b2000-10-14 16:23:11 +1100197 user = packet_get_string(NULL);
198 service = packet_get_string(NULL);
199 method = packet_get_string(NULL);
200 debug("userauth-request for user %s service %s method %s", user, service, method);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000201 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
Damien Miller874d77b2000-10-14 16:23:11 +1100202
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000203 if ((style = strchr(user, ':')) != NULL)
204 *style++ = 0;
205
Ben Lindstrom6d40c0f2001-01-29 09:02:24 +0000206 if (authctxt->attempt++ == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100207 /* setup auth context */
208 struct passwd *pw = NULL;
209 setproctitle("%s", user);
210 pw = getpwnam(user);
211 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
212 authctxt->pw = pwcopy(pw);
213 authctxt->valid = 1;
214 debug2("input_userauth_request: setting up authctxt for %s", user);
215#ifdef USE_PAM
Damien Miller22e22bf2001-01-19 15:46:38 +1100216 start_pam(pw->pw_name);
Damien Miller874d77b2000-10-14 16:23:11 +1100217#endif
218 } else {
219 log("input_userauth_request: illegal user %s", user);
Damien Miller22e22bf2001-01-19 15:46:38 +1100220#ifdef USE_PAM
221 start_pam("NOUSER");
222#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100223 }
224 authctxt->user = xstrdup(user);
225 authctxt->service = xstrdup(service);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000226 authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */
Damien Miller874d77b2000-10-14 16:23:11 +1100227 } else if (authctxt->valid) {
228 if (strcmp(user, authctxt->user) != 0 ||
229 strcmp(service, authctxt->service) != 0) {
230 log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)",
231 user, service, authctxt->user, authctxt->service);
232 authctxt->valid = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000233 }
234 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000235 /* reset state */
236 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
237 authctxt->postponed = 0;
Damien Millerb70b61f2000-09-16 16:25:12 +1100238
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000239 /* try to authenticate user */
Damien Miller874d77b2000-10-14 16:23:11 +1100240 m = authmethod_lookup(method);
241 if (m != NULL) {
242 debug2("input_userauth_request: try method %s", method);
243 authenticated = m->userauth(authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100244 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000245 if (!authctxt->valid && authenticated)
246 fatal("INTERNAL ERROR: authenticated invalid user %s",
247 authctxt->user);
Damien Millerb70b61f2000-09-16 16:25:12 +1100248
Damien Miller874d77b2000-10-14 16:23:11 +1100249 /* Special handling for root */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000250 if (authenticated && authctxt->pw->pw_uid == 0 && !auth_root_allowed())
Damien Millereba71ba2000-04-29 23:57:08 +1000251 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000252
253#ifdef USE_PAM
Damien Millerd425d4d2000-10-28 21:05:57 +1100254 if (authenticated && authctxt->user && !do_pam_account(authctxt->user, NULL))
Damien Millerb70b61f2000-09-16 16:25:12 +1100255 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000256#endif /* USE_PAM */
257
Damien Miller874d77b2000-10-14 16:23:11 +1100258 /* Log before sending the reply */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000259 auth_log(authctxt, authenticated, method, " ssh2");
260
261 if (!authctxt->postponed)
262 userauth_reply(authctxt, authenticated);
Damien Miller874d77b2000-10-14 16:23:11 +1100263
264 xfree(service);
265 xfree(user);
266 xfree(method);
267}
268
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000269void
270userauth_banner(void)
271{
272 struct stat st;
273 char *banner = NULL;
274 off_t len, n;
275 int fd;
276
277 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
278 return;
279 if ((fd = open(options.banner, O_RDONLY)) < 0) {
280 error("userauth_banner: open %s failed: %s",
281 options.banner, strerror(errno));
282 return;
283 }
284 if (fstat(fd, &st) < 0)
285 goto done;
286 len = st.st_size;
287 banner = xmalloc(len + 1);
288 if ((n = read(fd, banner, len)) < 0)
289 goto done;
290 banner[n] = '\0';
291 packet_start(SSH2_MSG_USERAUTH_BANNER);
292 packet_put_cstring(banner);
293 packet_put_cstring(""); /* language, unused */
294 packet_send();
295 debug("userauth_banner: sent");
296done:
297 if (banner)
298 xfree(banner);
299 close(fd);
300 return;
301}
Damien Miller874d77b2000-10-14 16:23:11 +1100302
Damien Miller874d77b2000-10-14 16:23:11 +1100303void
304userauth_reply(Authctxt *authctxt, int authenticated)
305{
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000306 char *methods;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000307
Damien Millere247cc42000-05-07 12:03:14 +1000308 /* XXX todo: check if multiple auth methods are needed */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000309 if (authenticated) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000310#ifdef WITH_AIXAUTHENTICATE
311 /* We don't have a pty yet, so just label the line as "ssh" */
Damien Millerd425d4d2000-10-28 21:05:57 +1100312 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
313 get_canonical_hostname(), "ssh", &aixloginmsg) < 0)
Damien Millerd2c208a2000-05-17 22:00:02 +1000314 aixloginmsg = NULL;
315#endif /* WITH_AIXAUTHENTICATE */
Damien Millereba71ba2000-04-29 23:57:08 +1000316 /* turn off userauth */
317 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
318 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
319 packet_send();
320 packet_write_wait();
321 /* now we can break out */
Damien Miller874d77b2000-10-14 16:23:11 +1100322 authctxt->success = 1;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000323 } else {
324 if (authctxt->failures++ > AUTH_FAIL_MAX)
325 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000326 methods = authmethods_get();
Damien Millereba71ba2000-04-29 23:57:08 +1000327 packet_start(SSH2_MSG_USERAUTH_FAILURE);
Damien Miller874d77b2000-10-14 16:23:11 +1100328 packet_put_cstring(methods);
329 packet_put_char(0); /* XXX partial success, unused */
Damien Millereba71ba2000-04-29 23:57:08 +1000330 packet_send();
331 packet_write_wait();
Damien Miller874d77b2000-10-14 16:23:11 +1100332 xfree(methods);
Damien Millereba71ba2000-04-29 23:57:08 +1000333 }
Damien Millereba71ba2000-04-29 23:57:08 +1000334}
335
336int
Damien Miller874d77b2000-10-14 16:23:11 +1100337userauth_none(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000338{
Damien Miller874d77b2000-10-14 16:23:11 +1100339 /* disable method "none", only allowed one time */
340 Authmethod *m = authmethod_lookup("none");
341 if (m != NULL)
342 m->enabled = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000343 packet_done();
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000344 userauth_banner();
Damien Millerb8c656e2000-06-28 15:22:41 +1000345
Damien Miller874d77b2000-10-14 16:23:11 +1100346 if (authctxt->valid == 0)
347 return(0);
348
349#ifdef HAVE_CYGWIN
350 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
351 return(0);
352#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000353#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100354 return auth_pam_password(authctxt->pw, "");
Damien Millerb8c656e2000-06-28 15:22:41 +1000355#elif defined(HAVE_OSF_SIA)
Damien Miller874d77b2000-10-14 16:23:11 +1100356 return (sia_validate_user(NULL, saved_argc, saved_argv,
Damien Millerd425d4d2000-10-28 21:05:57 +1100357 get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER",
358 NULL, 0, NULL, "") == SIASUCCESS);
Damien Millerb8c656e2000-06-28 15:22:41 +1000359#else /* !HAVE_OSF_SIA && !USE_PAM */
Damien Miller874d77b2000-10-14 16:23:11 +1100360 return auth_password(authctxt->pw, "");
Damien Millereba71ba2000-04-29 23:57:08 +1000361#endif /* USE_PAM */
362}
Damien Miller874d77b2000-10-14 16:23:11 +1100363
Damien Millereba71ba2000-04-29 23:57:08 +1000364int
Damien Miller874d77b2000-10-14 16:23:11 +1100365userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000366{
367 char *password;
368 int authenticated = 0;
369 int change;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000370 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000371 change = packet_get_char();
372 if (change)
373 log("password change not supported");
374 password = packet_get_string(&len);
375 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100376 if (authctxt->valid &&
377#ifdef HAVE_CYGWIN
378 check_nt_auth(1, authctxt->pw->pw_uid) &&
379#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000380#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100381 auth_pam_password(authctxt->pw, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000382#elif defined(HAVE_OSF_SIA)
383 sia_validate_user(NULL, saved_argc, saved_argv,
Damien Millerd425d4d2000-10-28 21:05:57 +1100384 get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER",
385 NULL, 0, NULL, password) == SIASUCCESS)
Damien Millerb8c656e2000-06-28 15:22:41 +1000386#else /* !USE_PAM && !HAVE_OSF_SIA */
Damien Miller874d77b2000-10-14 16:23:11 +1100387 auth_password(authctxt->pw, password) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000388#endif /* USE_PAM */
389 authenticated = 1;
390 memset(password, 0, len);
391 xfree(password);
392 return authenticated;
393}
Damien Miller874d77b2000-10-14 16:23:11 +1100394
Damien Millereba71ba2000-04-29 23:57:08 +1000395int
Damien Miller874d77b2000-10-14 16:23:11 +1100396userauth_kbdint(Authctxt *authctxt)
397{
398 int authenticated = 0;
399 char *lang = NULL;
400 char *devs = NULL;
401
402 lang = packet_get_string(NULL);
403 devs = packet_get_string(NULL);
404 packet_done();
405
406 debug("keyboard-interactive language %s devs %s", lang, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000407
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000408 if (options.challenge_reponse_authentication)
409 authenticated = auth2_challenge(authctxt, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000410
Damien Millerb8481582000-12-03 11:51:51 +1100411#ifdef USE_PAM
412 if (authenticated == 0)
413 authenticated = auth2_pam(authctxt);
414#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100415 xfree(lang);
416 xfree(devs);
417#ifdef HAVE_CYGWIN
418 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
419 return(0);
420#endif
421 return authenticated;
422}
423
424int
425userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000426{
427 Buffer b;
428 Key *key;
429 char *pkalg, *pkblob, *sig;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000430 u_int alen, blen, slen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100431 int have_sig, pktype;
Damien Millereba71ba2000-04-29 23:57:08 +1000432 int authenticated = 0;
433
Damien Miller874d77b2000-10-14 16:23:11 +1100434 if (!authctxt->valid) {
435 debug2("userauth_pubkey: disabled because of invalid user");
Damien Millereba71ba2000-04-29 23:57:08 +1000436 return 0;
437 }
438 have_sig = packet_get_char();
Ben Lindstromd121f612000-12-03 17:00:47 +0000439 if (datafellows & SSH_BUG_PKAUTH) {
440 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
441 /* no explicit pkalg given */
442 pkblob = packet_get_string(&blen);
443 buffer_init(&b);
444 buffer_append(&b, pkblob, blen);
445 /* so we have to extract the pkalg from the pkblob */
446 pkalg = buffer_get_string(&b, &alen);
447 buffer_free(&b);
448 } else {
449 pkalg = packet_get_string(&alen);
450 pkblob = packet_get_string(&blen);
451 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100452 pktype = key_type_from_name(pkalg);
453 if (pktype == KEY_UNSPEC) {
Ben Lindstromd121f612000-12-03 17:00:47 +0000454 /* this is perfectly legal */
455 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
Damien Miller874d77b2000-10-14 16:23:11 +1100456 xfree(pkalg);
Ben Lindstromd121f612000-12-03 17:00:47 +0000457 xfree(pkblob);
Damien Millereba71ba2000-04-29 23:57:08 +1000458 return 0;
459 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100460 key = key_from_blob(pkblob, blen);
Damien Millereba71ba2000-04-29 23:57:08 +1000461 if (key != NULL) {
462 if (have_sig) {
463 sig = packet_get_string(&slen);
464 packet_done();
465 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100466 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000467 buffer_append(&b, session_id2, session_id2_len);
Damien Miller50a41ed2000-10-16 12:14:42 +1100468 } else {
469 buffer_put_string(&b, session_id2, session_id2_len);
Damien Miller6536c7d2000-06-22 21:32:31 +1000470 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000471 /* reconstruct packet */
Damien Millereba71ba2000-04-29 23:57:08 +1000472 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller874d77b2000-10-14 16:23:11 +1100473 buffer_put_cstring(&b, authctxt->user);
Damien Millerf6d9e222000-06-18 14:50:44 +1000474 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000475 datafellows & SSH_BUG_PKSERVICE ?
Damien Millerf6d9e222000-06-18 14:50:44 +1000476 "ssh-userauth" :
Damien Miller874d77b2000-10-14 16:23:11 +1100477 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000478 if (datafellows & SSH_BUG_PKAUTH) {
479 buffer_put_char(&b, have_sig);
480 } else {
481 buffer_put_cstring(&b, "publickey");
482 buffer_put_char(&b, have_sig);
483 buffer_put_cstring(&b, key_ssh_name(key));
484 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000485 buffer_put_string(&b, pkblob, blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100486#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000487 buffer_dump(&b);
488#endif
489 /* test for correct signature */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100490 if (user_key_allowed(authctxt->pw, key) &&
491 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000492 authenticated = 1;
493 buffer_clear(&b);
494 xfree(sig);
495 } else {
Damien Miller874d77b2000-10-14 16:23:11 +1100496 debug("test whether pkalg/pkblob are acceptable");
Damien Millereba71ba2000-04-29 23:57:08 +1000497 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100498
Damien Millereba71ba2000-04-29 23:57:08 +1000499 /* XXX fake reply and always send PK_OK ? */
Damien Millere247cc42000-05-07 12:03:14 +1000500 /*
501 * XXX this allows testing whether a user is allowed
502 * to login: if you happen to have a valid pubkey this
503 * message is sent. the message is NEVER sent at all
504 * if a user is not allowed to login. is this an
505 * issue? -markus
506 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100507 if (user_key_allowed(authctxt->pw, key)) {
Damien Millereba71ba2000-04-29 23:57:08 +1000508 packet_start(SSH2_MSG_USERAUTH_PK_OK);
509 packet_put_string(pkalg, alen);
510 packet_put_string(pkblob, blen);
511 packet_send();
512 packet_write_wait();
513 authenticated = -1;
514 }
515 }
Damien Miller874d77b2000-10-14 16:23:11 +1100516 if (authenticated != 1)
517 auth_clear_options();
Damien Millereba71ba2000-04-29 23:57:08 +1000518 key_free(key);
519 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100520 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
Damien Millereba71ba2000-04-29 23:57:08 +1000521 xfree(pkalg);
522 xfree(pkblob);
Damien Miller874d77b2000-10-14 16:23:11 +1100523#ifdef HAVE_CYGWIN
524 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
525 return(0);
526#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000527 return authenticated;
528}
529
Damien Miller874d77b2000-10-14 16:23:11 +1100530/* get current user */
Damien Millereba71ba2000-04-29 23:57:08 +1000531
532struct passwd*
533auth_get_user(void)
534{
Damien Miller874d77b2000-10-14 16:23:11 +1100535 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000536}
537
Damien Miller874d77b2000-10-14 16:23:11 +1100538#define DELIM ","
Damien Millereba71ba2000-04-29 23:57:08 +1000539
Damien Miller874d77b2000-10-14 16:23:11 +1100540char *
541authmethods_get(void)
542{
543 Authmethod *method = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000544 u_int size = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100545 char *list;
546
547 for (method = authmethods; method->name != NULL; method++) {
548 if (strcmp(method->name, "none") == 0)
549 continue;
550 if (method->enabled != NULL && *(method->enabled) != 0) {
551 if (size != 0)
552 size += strlen(DELIM);
553 size += strlen(method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000554 }
555 }
Damien Miller874d77b2000-10-14 16:23:11 +1100556 size++; /* trailing '\0' */
557 list = xmalloc(size);
558 list[0] = '\0';
559
560 for (method = authmethods; method->name != NULL; method++) {
561 if (strcmp(method->name, "none") == 0)
562 continue;
563 if (method->enabled != NULL && *(method->enabled) != 0) {
564 if (list[0] != '\0')
565 strlcat(list, DELIM, size);
566 strlcat(list, method->name, size);
567 }
568 }
569 return list;
570}
571
572Authmethod *
573authmethod_lookup(const char *name)
574{
575 Authmethod *method = NULL;
576 if (name != NULL)
577 for (method = authmethods; method->name != NULL; method++)
578 if (method->enabled != NULL &&
579 *(method->enabled) != 0 &&
580 strcmp(name, method->name) == 0)
581 return method;
582 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
583 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000584}
585
586/* return 1 if user allows given key */
587int
Damien Miller0bc1bd82000-11-13 22:57:25 +1100588user_key_allowed(struct passwd *pw, Key *key)
Damien Millereba71ba2000-04-29 23:57:08 +1000589{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000590 char line[8192], file[MAXPATHLEN];
Damien Millereba71ba2000-04-29 23:57:08 +1000591 int found_key = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000592 FILE *f;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000593 u_long linenum = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000594 struct stat st;
595 Key *found;
596
Damien Miller874d77b2000-10-14 16:23:11 +1100597 if (pw == NULL)
598 return 0;
599
Damien Millereba71ba2000-04-29 23:57:08 +1000600 /* Temporarily use the user's uid. */
601 temporarily_use_uid(pw->pw_uid);
602
603 /* The authorized keys. */
604 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000605 _PATH_SSH_USER_PERMITTED_KEYS2);
Damien Millereba71ba2000-04-29 23:57:08 +1000606
607 /* Fail quietly if file does not exist */
608 if (stat(file, &st) < 0) {
609 /* Restore the privileged uid. */
610 restore_uid();
611 return 0;
612 }
613 /* Open the file containing the authorized keys. */
614 f = fopen(file, "r");
615 if (!f) {
616 /* Restore the privileged uid. */
617 restore_uid();
618 return 0;
619 }
620 if (options.strict_modes) {
621 int fail = 0;
622 char buf[1024];
623 /* Check open file in order to avoid open/stat races */
624 if (fstat(fileno(f), &st) < 0 ||
625 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
626 (st.st_mode & 022) != 0) {
Damien Millerd3444942000-09-30 14:20:03 +1100627 snprintf(buf, sizeof buf,
628 "%s authentication refused for %.100s: "
629 "bad ownership or modes for '%s'.",
630 key_type(key), pw->pw_name, file);
Damien Millereba71ba2000-04-29 23:57:08 +1000631 fail = 1;
632 } else {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000633 /* Check path to _PATH_SSH_USER_PERMITTED_KEYS */
Damien Millereba71ba2000-04-29 23:57:08 +1000634 int i;
635 static const char *check[] = {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000636 "", _PATH_SSH_USER_DIR, NULL
Damien Millereba71ba2000-04-29 23:57:08 +1000637 };
638 for (i = 0; check[i]; i++) {
639 snprintf(line, sizeof line, "%.500s/%.100s",
640 pw->pw_dir, check[i]);
641 if (stat(line, &st) < 0 ||
642 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
643 (st.st_mode & 022) != 0) {
644 snprintf(buf, sizeof buf,
Damien Millerd3444942000-09-30 14:20:03 +1100645 "%s authentication refused for %.100s: "
Damien Millereba71ba2000-04-29 23:57:08 +1000646 "bad ownership or modes for '%s'.",
Damien Millerd3444942000-09-30 14:20:03 +1100647 key_type(key), pw->pw_name, line);
Damien Millereba71ba2000-04-29 23:57:08 +1000648 fail = 1;
649 break;
650 }
651 }
652 }
653 if (fail) {
Damien Millereba71ba2000-04-29 23:57:08 +1000654 fclose(f);
Damien Miller37023962000-07-11 17:31:38 +1000655 log("%s",buf);
Damien Millereba71ba2000-04-29 23:57:08 +1000656 restore_uid();
657 return 0;
658 }
659 }
660 found_key = 0;
Damien Millerd3444942000-09-30 14:20:03 +1100661 found = key_new(key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000662
663 while (fgets(line, sizeof(line), f)) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000664 char *cp, *options = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000665 linenum++;
666 /* Skip leading whitespace, empty and comment lines. */
667 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
668 ;
669 if (!*cp || *cp == '\n' || *cp == '#')
670 continue;
Damien Millerf6d9e222000-06-18 14:50:44 +1000671
Damien Miller0bc1bd82000-11-13 22:57:25 +1100672 if (key_read(found, &cp) == -1) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000673 /* no key? check if there are options for this key */
674 int quoted = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100675 debug2("user_key_allowed: check options: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000676 options = cp;
677 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
678 if (*cp == '\\' && cp[1] == '"')
679 cp++; /* Skip both */
680 else if (*cp == '"')
681 quoted = !quoted;
682 }
683 /* Skip remaining whitespace. */
684 for (; *cp == ' ' || *cp == '\t'; cp++)
685 ;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100686 if (key_read(found, &cp) == -1) {
687 debug2("user_key_allowed: advance: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000688 /* still no key? advance to next line*/
689 continue;
690 }
691 }
692 if (key_equal(found, key) &&
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000693 auth_parse_options(pw, options, file, linenum) == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000694 found_key = 1;
695 debug("matching key found: file %s, line %ld",
696 file, linenum);
697 break;
698 }
699 }
700 restore_uid();
701 fclose(f);
702 key_free(found);
703 return found_key;
704}