blob: f2f4812e7b86b0d8c985548d575b70cb86fb7ab3 [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"
Kevin Steves0afcc9f2001-02-05 13:57:36 +000026RCSID("$OpenBSD: auth2.c,v 1.36 2001/02/04 15:32:22 stevesk 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"
Damien Millereba71ba2000-04-29 23:57:08 +100049#include "key.h"
Ben Lindstrom6d40c0f2001-01-29 09:02:24 +000050#include "cipher.h"
Damien Millereba71ba2000-04-29 23:57:08 +100051#include "kex.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "pathnames.h"
Damien Millereba71ba2000-04-29 23:57:08 +100053#include "uidswap.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100054#include "auth-options.h"
Damien Millereba71ba2000-04-29 23:57:08 +100055
56/* import */
57extern ServerOptions options;
Ben Lindstrom46c16222000-12-22 01:43:59 +000058extern u_char *session_id2;
Damien Millereba71ba2000-04-29 23:57:08 +100059extern int session_id2_len;
60
Damien Miller874d77b2000-10-14 16:23:11 +110061#ifdef WITH_AIXAUTHENTICATE
62extern char *aixloginmsg;
63#endif
64#ifdef HAVE_OSF_SIA
65extern int saved_argc;
66extern char **saved_argv;
67#endif
68
69static Authctxt *x_authctxt = NULL;
70static int one = 1;
71
72typedef struct Authmethod Authmethod;
73struct Authmethod {
74 char *name;
75 int (*userauth)(Authctxt *authctxt);
76 int *enabled;
77};
78
Damien Millereba71ba2000-04-29 23:57:08 +100079/* protocol */
80
Damien Miller62cee002000-09-23 17:15:56 +110081void input_service_request(int type, int plen, void *ctxt);
82void input_userauth_request(int type, int plen, void *ctxt);
83void protocol_error(int type, int plen, void *ctxt);
Damien Millereba71ba2000-04-29 23:57:08 +100084
Damien Millereba71ba2000-04-29 23:57:08 +100085/* helper */
Damien Miller874d77b2000-10-14 16:23:11 +110086Authmethod *authmethod_lookup(const char *name);
87struct passwd *pwcopy(struct passwd *pw);
Damien Miller0bc1bd82000-11-13 22:57:25 +110088int user_key_allowed(struct passwd *pw, Key *key);
Damien Miller874d77b2000-10-14 16:23:11 +110089char *authmethods_get(void);
Damien Millereba71ba2000-04-29 23:57:08 +100090
Damien Miller874d77b2000-10-14 16:23:11 +110091/* auth */
Ben Lindstrom48bd7c12001-01-09 00:35:42 +000092void userauth_banner(void);
Damien Miller874d77b2000-10-14 16:23:11 +110093int userauth_none(Authctxt *authctxt);
94int userauth_passwd(Authctxt *authctxt);
95int userauth_pubkey(Authctxt *authctxt);
96int userauth_kbdint(Authctxt *authctxt);
97
98Authmethod authmethods[] = {
99 {"none",
100 userauth_none,
101 &one},
102 {"publickey",
103 userauth_pubkey,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100104 &options.pubkey_authentication},
Damien Miller874d77b2000-10-14 16:23:11 +1100105 {"keyboard-interactive",
106 userauth_kbdint,
107 &options.kbd_interactive_authentication},
108 {"password",
109 userauth_passwd,
110 &options.password_authentication},
111 {NULL, NULL, NULL}
Damien Millereba71ba2000-04-29 23:57:08 +1000112};
Damien Millereba71ba2000-04-29 23:57:08 +1000113
114/*
Damien Miller874d77b2000-10-14 16:23:11 +1100115 * loop until authctxt->success == TRUE
Damien Millereba71ba2000-04-29 23:57:08 +1000116 */
117
118void
119do_authentication2()
120{
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000121 Authctxt *authctxt = authctxt_new();
122
Damien Miller874d77b2000-10-14 16:23:11 +1100123 x_authctxt = authctxt; /*XXX*/
124
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000125 /* challenge-reponse is implemented via keyboard interactive */
126 if (options.challenge_reponse_authentication)
127 options.kbd_interactive_authentication = 1;
128
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000129#ifdef AFS
130 /* If machine has AFS, set process authentication group. */
131 if (k_hasafs()) {
132 k_setpag();
133 k_unlog();
134 }
Damien Miller1cead2c2000-05-01 22:55:23 +1000135#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000136 dispatch_init(&protocol_error);
137 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
Damien Miller874d77b2000-10-14 16:23:11 +1100138 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000139 do_authenticated2(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000140}
141
142void
Damien Miller62cee002000-09-23 17:15:56 +1100143protocol_error(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000144{
145 log("auth: protocol error: type %d plen %d", type, plen);
146 packet_start(SSH2_MSG_UNIMPLEMENTED);
147 packet_put_int(0);
148 packet_send();
149 packet_write_wait();
150}
151
152void
Damien Miller62cee002000-09-23 17:15:56 +1100153input_service_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000154{
Damien Miller874d77b2000-10-14 16:23:11 +1100155 Authctxt *authctxt = ctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000156 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000157 int accept = 0;
158 char *service = packet_get_string(&len);
159 packet_done();
160
Damien Miller874d77b2000-10-14 16:23:11 +1100161 if (authctxt == NULL)
162 fatal("input_service_request: no authctxt");
163
Damien Millereba71ba2000-04-29 23:57:08 +1000164 if (strcmp(service, "ssh-userauth") == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100165 if (!authctxt->success) {
Damien Millereba71ba2000-04-29 23:57:08 +1000166 accept = 1;
167 /* now we can handle user-auth requests */
168 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
169 }
170 }
171 /* XXX all other service requests are denied */
172
173 if (accept) {
174 packet_start(SSH2_MSG_SERVICE_ACCEPT);
175 packet_put_cstring(service);
176 packet_send();
177 packet_write_wait();
178 } else {
179 debug("bad service request %s", service);
180 packet_disconnect("bad service request %s", service);
181 }
182 xfree(service);
183}
184
185void
Damien Miller62cee002000-09-23 17:15:56 +1100186input_userauth_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000187{
Damien Miller874d77b2000-10-14 16:23:11 +1100188 Authctxt *authctxt = ctxt;
189 Authmethod *m = NULL;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000190 char *user, *service, *method, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000191 int authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000192
Damien Miller874d77b2000-10-14 16:23:11 +1100193 if (authctxt == NULL)
194 fatal("input_userauth_request: no authctxt");
Damien Millereba71ba2000-04-29 23:57:08 +1000195
Damien Miller874d77b2000-10-14 16:23:11 +1100196 user = packet_get_string(NULL);
197 service = packet_get_string(NULL);
198 method = packet_get_string(NULL);
199 debug("userauth-request for user %s service %s method %s", user, service, method);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000200 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
Damien Miller874d77b2000-10-14 16:23:11 +1100201
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000202 if ((style = strchr(user, ':')) != NULL)
203 *style++ = 0;
204
Kevin Stevesef4eea92001-02-05 12:42:17 +0000205 if (authctxt->attempt++ == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100206 /* setup auth context */
207 struct passwd *pw = NULL;
208 setproctitle("%s", user);
209 pw = getpwnam(user);
210 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
211 authctxt->pw = pwcopy(pw);
212 authctxt->valid = 1;
213 debug2("input_userauth_request: setting up authctxt for %s", user);
214#ifdef USE_PAM
Damien Miller22e22bf2001-01-19 15:46:38 +1100215 start_pam(pw->pw_name);
Damien Miller874d77b2000-10-14 16:23:11 +1100216#endif
217 } else {
218 log("input_userauth_request: illegal user %s", user);
Damien Miller22e22bf2001-01-19 15:46:38 +1100219#ifdef USE_PAM
220 start_pam("NOUSER");
221#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100222 }
223 authctxt->user = xstrdup(user);
224 authctxt->service = xstrdup(service);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000225 authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */
Damien Miller874d77b2000-10-14 16:23:11 +1100226 } else if (authctxt->valid) {
227 if (strcmp(user, authctxt->user) != 0 ||
228 strcmp(service, authctxt->service) != 0) {
229 log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)",
230 user, service, authctxt->user, authctxt->service);
231 authctxt->valid = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000232 }
233 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000234 /* reset state */
235 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
236 authctxt->postponed = 0;
Damien Millerb70b61f2000-09-16 16:25:12 +1100237
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000238 /* try to authenticate user */
Damien Miller874d77b2000-10-14 16:23:11 +1100239 m = authmethod_lookup(method);
240 if (m != NULL) {
241 debug2("input_userauth_request: try method %s", method);
242 authenticated = m->userauth(authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100243 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000244 if (!authctxt->valid && authenticated)
245 fatal("INTERNAL ERROR: authenticated invalid user %s",
246 authctxt->user);
Damien Millerb70b61f2000-09-16 16:25:12 +1100247
Damien Miller874d77b2000-10-14 16:23:11 +1100248 /* Special handling for root */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000249 if (authenticated && authctxt->pw->pw_uid == 0 && !auth_root_allowed())
Damien Millereba71ba2000-04-29 23:57:08 +1000250 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000251
252#ifdef USE_PAM
Damien Millerd425d4d2000-10-28 21:05:57 +1100253 if (authenticated && authctxt->user && !do_pam_account(authctxt->user, NULL))
Damien Millerb70b61f2000-09-16 16:25:12 +1100254 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000255#endif /* USE_PAM */
256
Damien Miller874d77b2000-10-14 16:23:11 +1100257 /* Log before sending the reply */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000258 auth_log(authctxt, authenticated, method, " ssh2");
259
260 if (!authctxt->postponed)
261 userauth_reply(authctxt, authenticated);
Damien Miller874d77b2000-10-14 16:23:11 +1100262
263 xfree(service);
264 xfree(user);
265 xfree(method);
266}
267
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000268void
269userauth_banner(void)
270{
271 struct stat st;
272 char *banner = NULL;
273 off_t len, n;
274 int fd;
275
276 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
277 return;
278 if ((fd = open(options.banner, O_RDONLY)) < 0) {
279 error("userauth_banner: open %s failed: %s",
280 options.banner, strerror(errno));
281 return;
282 }
283 if (fstat(fd, &st) < 0)
284 goto done;
285 len = st.st_size;
286 banner = xmalloc(len + 1);
287 if ((n = read(fd, banner, len)) < 0)
288 goto done;
289 banner[n] = '\0';
290 packet_start(SSH2_MSG_USERAUTH_BANNER);
291 packet_put_cstring(banner);
292 packet_put_cstring(""); /* language, unused */
293 packet_send();
294 debug("userauth_banner: sent");
295done:
296 if (banner)
297 xfree(banner);
298 close(fd);
299 return;
300}
Damien Miller874d77b2000-10-14 16:23:11 +1100301
Kevin Stevesef4eea92001-02-05 12:42:17 +0000302void
Damien Miller874d77b2000-10-14 16:23:11 +1100303userauth_reply(Authctxt *authctxt, int authenticated)
304{
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000305 char *methods;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000306
Damien Millere247cc42000-05-07 12:03:14 +1000307 /* XXX todo: check if multiple auth methods are needed */
Kevin Steves4abe4de2001-02-08 19:16:32 +0000308 if (authenticated == 1) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000309#ifdef WITH_AIXAUTHENTICATE
310 /* We don't have a pty yet, so just label the line as "ssh" */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000311 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
312 get_canonical_hostname(options.reverse_mapping_check),
Damien Miller33804262001-02-04 23:20:18 +1100313 "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)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000325 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();
Kevin Stevesef4eea92001-02-05 12:42:17 +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);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000348
Damien Miller874d77b2000-10-14 16:23:11 +1100349#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)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000356 return (sia_validate_user(NULL, saved_argc, saved_argv,
357 get_canonical_hostname(options.reverse_mapping_check),
358 authctxt->user?authctxt->user:"NOUSER", NULL, 0,
Damien Miller33804262001-02-04 23:20:18 +1100359 NULL, "") == SIASUCCESS);
Damien Millerb8c656e2000-06-28 15:22:41 +1000360#else /* !HAVE_OSF_SIA && !USE_PAM */
Damien Miller874d77b2000-10-14 16:23:11 +1100361 return auth_password(authctxt->pw, "");
Damien Millereba71ba2000-04-29 23:57:08 +1000362#endif /* USE_PAM */
363}
Damien Miller874d77b2000-10-14 16:23:11 +1100364
Damien Millereba71ba2000-04-29 23:57:08 +1000365int
Damien Miller874d77b2000-10-14 16:23:11 +1100366userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000367{
368 char *password;
369 int authenticated = 0;
370 int change;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000371 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000372 change = packet_get_char();
373 if (change)
374 log("password change not supported");
375 password = packet_get_string(&len);
376 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100377 if (authctxt->valid &&
378#ifdef HAVE_CYGWIN
379 check_nt_auth(1, authctxt->pw->pw_uid) &&
380#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000381#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100382 auth_pam_password(authctxt->pw, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000383#elif defined(HAVE_OSF_SIA)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000384 sia_validate_user(NULL, saved_argc, saved_argv,
385 get_canonical_hostname(options.reverse_mapping_check),
386 authctxt->user?authctxt->user:"NOUSER", NULL, 0, NULL,
Damien Miller33804262001-02-04 23:20:18 +1100387 password) == SIASUCCESS)
Damien Millerb8c656e2000-06-28 15:22:41 +1000388#else /* !USE_PAM && !HAVE_OSF_SIA */
Damien Miller874d77b2000-10-14 16:23:11 +1100389 auth_password(authctxt->pw, password) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000390#endif /* USE_PAM */
391 authenticated = 1;
392 memset(password, 0, len);
393 xfree(password);
394 return authenticated;
395}
Damien Miller874d77b2000-10-14 16:23:11 +1100396
Damien Millereba71ba2000-04-29 23:57:08 +1000397int
Damien Miller874d77b2000-10-14 16:23:11 +1100398userauth_kbdint(Authctxt *authctxt)
399{
400 int authenticated = 0;
401 char *lang = NULL;
402 char *devs = NULL;
403
404 lang = packet_get_string(NULL);
405 devs = packet_get_string(NULL);
406 packet_done();
407
408 debug("keyboard-interactive language %s devs %s", lang, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000409
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000410 if (options.challenge_reponse_authentication)
411 authenticated = auth2_challenge(authctxt, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000412
Damien Millerb8481582000-12-03 11:51:51 +1100413#ifdef USE_PAM
414 if (authenticated == 0)
415 authenticated = auth2_pam(authctxt);
416#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100417 xfree(lang);
418 xfree(devs);
419#ifdef HAVE_CYGWIN
420 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
421 return(0);
422#endif
423 return authenticated;
424}
425
426int
427userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000428{
429 Buffer b;
430 Key *key;
431 char *pkalg, *pkblob, *sig;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000432 u_int alen, blen, slen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100433 int have_sig, pktype;
Damien Millereba71ba2000-04-29 23:57:08 +1000434 int authenticated = 0;
435
Damien Miller874d77b2000-10-14 16:23:11 +1100436 if (!authctxt->valid) {
437 debug2("userauth_pubkey: disabled because of invalid user");
Damien Millereba71ba2000-04-29 23:57:08 +1000438 return 0;
439 }
440 have_sig = packet_get_char();
Ben Lindstromd121f612000-12-03 17:00:47 +0000441 if (datafellows & SSH_BUG_PKAUTH) {
442 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
443 /* no explicit pkalg given */
444 pkblob = packet_get_string(&blen);
445 buffer_init(&b);
446 buffer_append(&b, pkblob, blen);
447 /* so we have to extract the pkalg from the pkblob */
448 pkalg = buffer_get_string(&b, &alen);
449 buffer_free(&b);
450 } else {
451 pkalg = packet_get_string(&alen);
452 pkblob = packet_get_string(&blen);
453 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100454 pktype = key_type_from_name(pkalg);
455 if (pktype == KEY_UNSPEC) {
Ben Lindstromd121f612000-12-03 17:00:47 +0000456 /* this is perfectly legal */
457 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
Damien Miller874d77b2000-10-14 16:23:11 +1100458 xfree(pkalg);
Ben Lindstromd121f612000-12-03 17:00:47 +0000459 xfree(pkblob);
Damien Millereba71ba2000-04-29 23:57:08 +1000460 return 0;
461 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100462 key = key_from_blob(pkblob, blen);
Damien Millereba71ba2000-04-29 23:57:08 +1000463 if (key != NULL) {
464 if (have_sig) {
465 sig = packet_get_string(&slen);
466 packet_done();
467 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100468 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000469 buffer_append(&b, session_id2, session_id2_len);
Damien Miller50a41ed2000-10-16 12:14:42 +1100470 } else {
471 buffer_put_string(&b, session_id2, session_id2_len);
Damien Miller6536c7d2000-06-22 21:32:31 +1000472 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000473 /* reconstruct packet */
Damien Millereba71ba2000-04-29 23:57:08 +1000474 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller874d77b2000-10-14 16:23:11 +1100475 buffer_put_cstring(&b, authctxt->user);
Damien Millerf6d9e222000-06-18 14:50:44 +1000476 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000477 datafellows & SSH_BUG_PKSERVICE ?
Damien Millerf6d9e222000-06-18 14:50:44 +1000478 "ssh-userauth" :
Damien Miller874d77b2000-10-14 16:23:11 +1100479 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000480 if (datafellows & SSH_BUG_PKAUTH) {
481 buffer_put_char(&b, have_sig);
482 } else {
483 buffer_put_cstring(&b, "publickey");
484 buffer_put_char(&b, have_sig);
485 buffer_put_cstring(&b, key_ssh_name(key));
486 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000487 buffer_put_string(&b, pkblob, blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100488#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000489 buffer_dump(&b);
490#endif
491 /* test for correct signature */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100492 if (user_key_allowed(authctxt->pw, key) &&
493 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000494 authenticated = 1;
495 buffer_clear(&b);
496 xfree(sig);
497 } else {
Damien Miller874d77b2000-10-14 16:23:11 +1100498 debug("test whether pkalg/pkblob are acceptable");
Damien Millereba71ba2000-04-29 23:57:08 +1000499 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100500
Damien Millereba71ba2000-04-29 23:57:08 +1000501 /* XXX fake reply and always send PK_OK ? */
Damien Millere247cc42000-05-07 12:03:14 +1000502 /*
503 * XXX this allows testing whether a user is allowed
504 * to login: if you happen to have a valid pubkey this
505 * message is sent. the message is NEVER sent at all
506 * if a user is not allowed to login. is this an
507 * issue? -markus
508 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100509 if (user_key_allowed(authctxt->pw, key)) {
Damien Millereba71ba2000-04-29 23:57:08 +1000510 packet_start(SSH2_MSG_USERAUTH_PK_OK);
511 packet_put_string(pkalg, alen);
512 packet_put_string(pkblob, blen);
513 packet_send();
514 packet_write_wait();
Kevin Steves4abe4de2001-02-08 19:16:32 +0000515 authctxt->postponed = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000516 }
517 }
Damien Miller874d77b2000-10-14 16:23:11 +1100518 if (authenticated != 1)
519 auth_clear_options();
Damien Millereba71ba2000-04-29 23:57:08 +1000520 key_free(key);
521 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100522 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
Damien Millereba71ba2000-04-29 23:57:08 +1000523 xfree(pkalg);
524 xfree(pkblob);
Damien Miller874d77b2000-10-14 16:23:11 +1100525#ifdef HAVE_CYGWIN
526 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
527 return(0);
528#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000529 return authenticated;
530}
531
Damien Miller874d77b2000-10-14 16:23:11 +1100532/* get current user */
Damien Millereba71ba2000-04-29 23:57:08 +1000533
534struct passwd*
535auth_get_user(void)
536{
Damien Miller874d77b2000-10-14 16:23:11 +1100537 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000538}
539
Damien Miller874d77b2000-10-14 16:23:11 +1100540#define DELIM ","
Damien Millereba71ba2000-04-29 23:57:08 +1000541
Damien Miller874d77b2000-10-14 16:23:11 +1100542char *
543authmethods_get(void)
544{
545 Authmethod *method = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000546 u_int size = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100547 char *list;
548
549 for (method = authmethods; method->name != NULL; method++) {
550 if (strcmp(method->name, "none") == 0)
551 continue;
552 if (method->enabled != NULL && *(method->enabled) != 0) {
553 if (size != 0)
554 size += strlen(DELIM);
555 size += strlen(method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000556 }
557 }
Damien Miller874d77b2000-10-14 16:23:11 +1100558 size++; /* trailing '\0' */
559 list = xmalloc(size);
560 list[0] = '\0';
561
562 for (method = authmethods; method->name != NULL; method++) {
563 if (strcmp(method->name, "none") == 0)
564 continue;
565 if (method->enabled != NULL && *(method->enabled) != 0) {
566 if (list[0] != '\0')
567 strlcat(list, DELIM, size);
568 strlcat(list, method->name, size);
569 }
570 }
571 return list;
572}
573
574Authmethod *
575authmethod_lookup(const char *name)
576{
577 Authmethod *method = NULL;
578 if (name != NULL)
579 for (method = authmethods; method->name != NULL; method++)
580 if (method->enabled != NULL &&
581 *(method->enabled) != 0 &&
582 strcmp(name, method->name) == 0)
583 return method;
584 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
585 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000586}
587
588/* return 1 if user allows given key */
589int
Damien Miller0bc1bd82000-11-13 22:57:25 +1100590user_key_allowed(struct passwd *pw, Key *key)
Damien Millereba71ba2000-04-29 23:57:08 +1000591{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000592 char line[8192], file[MAXPATHLEN];
Damien Millereba71ba2000-04-29 23:57:08 +1000593 int found_key = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000594 FILE *f;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000595 u_long linenum = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000596 struct stat st;
597 Key *found;
598
Damien Miller874d77b2000-10-14 16:23:11 +1100599 if (pw == NULL)
600 return 0;
601
Damien Millereba71ba2000-04-29 23:57:08 +1000602 /* Temporarily use the user's uid. */
603 temporarily_use_uid(pw->pw_uid);
604
605 /* The authorized keys. */
606 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000607 _PATH_SSH_USER_PERMITTED_KEYS2);
Damien Millereba71ba2000-04-29 23:57:08 +1000608
609 /* Fail quietly if file does not exist */
610 if (stat(file, &st) < 0) {
611 /* Restore the privileged uid. */
612 restore_uid();
613 return 0;
614 }
615 /* Open the file containing the authorized keys. */
616 f = fopen(file, "r");
617 if (!f) {
618 /* Restore the privileged uid. */
619 restore_uid();
620 return 0;
621 }
622 if (options.strict_modes) {
623 int fail = 0;
624 char buf[1024];
625 /* Check open file in order to avoid open/stat races */
626 if (fstat(fileno(f), &st) < 0 ||
627 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
628 (st.st_mode & 022) != 0) {
Damien Millerd3444942000-09-30 14:20:03 +1100629 snprintf(buf, sizeof buf,
630 "%s authentication refused for %.100s: "
631 "bad ownership or modes for '%s'.",
632 key_type(key), pw->pw_name, file);
Damien Millereba71ba2000-04-29 23:57:08 +1000633 fail = 1;
634 } else {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000635 /* Check path to _PATH_SSH_USER_PERMITTED_KEYS */
Damien Millereba71ba2000-04-29 23:57:08 +1000636 int i;
637 static const char *check[] = {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000638 "", _PATH_SSH_USER_DIR, NULL
Damien Millereba71ba2000-04-29 23:57:08 +1000639 };
640 for (i = 0; check[i]; i++) {
641 snprintf(line, sizeof line, "%.500s/%.100s",
642 pw->pw_dir, check[i]);
643 if (stat(line, &st) < 0 ||
644 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
645 (st.st_mode & 022) != 0) {
646 snprintf(buf, sizeof buf,
Damien Millerd3444942000-09-30 14:20:03 +1100647 "%s authentication refused for %.100s: "
Damien Millereba71ba2000-04-29 23:57:08 +1000648 "bad ownership or modes for '%s'.",
Damien Millerd3444942000-09-30 14:20:03 +1100649 key_type(key), pw->pw_name, line);
Damien Millereba71ba2000-04-29 23:57:08 +1000650 fail = 1;
651 break;
652 }
653 }
654 }
655 if (fail) {
Damien Millereba71ba2000-04-29 23:57:08 +1000656 fclose(f);
Damien Miller37023962000-07-11 17:31:38 +1000657 log("%s",buf);
Damien Millereba71ba2000-04-29 23:57:08 +1000658 restore_uid();
659 return 0;
660 }
661 }
662 found_key = 0;
Damien Millerd3444942000-09-30 14:20:03 +1100663 found = key_new(key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000664
665 while (fgets(line, sizeof(line), f)) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000666 char *cp, *options = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000667 linenum++;
668 /* Skip leading whitespace, empty and comment lines. */
669 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
670 ;
671 if (!*cp || *cp == '\n' || *cp == '#')
672 continue;
Damien Millerf6d9e222000-06-18 14:50:44 +1000673
Damien Miller0bc1bd82000-11-13 22:57:25 +1100674 if (key_read(found, &cp) == -1) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000675 /* no key? check if there are options for this key */
676 int quoted = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100677 debug2("user_key_allowed: check options: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000678 options = cp;
679 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
680 if (*cp == '\\' && cp[1] == '"')
681 cp++; /* Skip both */
682 else if (*cp == '"')
683 quoted = !quoted;
684 }
685 /* Skip remaining whitespace. */
686 for (; *cp == ' ' || *cp == '\t'; cp++)
687 ;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100688 if (key_read(found, &cp) == -1) {
689 debug2("user_key_allowed: advance: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000690 /* still no key? advance to next line*/
691 continue;
692 }
693 }
694 if (key_equal(found, key) &&
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000695 auth_parse_options(pw, options, file, linenum) == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000696 found_key = 1;
697 debug("matching key found: file %s, line %ld",
698 file, linenum);
699 break;
700 }
701 }
702 restore_uid();
703 fclose(f);
704 key_free(found);
705 return found_key;
706}