blob: e800c0587490538ad45e106c26f8c81d1a046a5a [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 Lindstrom551ea372001-06-05 18:56:16 +000026RCSID("$OpenBSD: auth2.c,v 1.57 2001/05/18 14:13:28 markus Exp $");
Damien Miller874d77b2000-10-14 16:23:11 +110027
Damien Millereba71ba2000-04-29 23:57:08 +100028#include <openssl/evp.h>
29
Ben Lindstrom226cfa02001-01-22 05:34:40 +000030#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100031#include "xmalloc.h"
32#include "rsa.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000033#include "sshpty.h"
Damien Millereba71ba2000-04-29 23:57:08 +100034#include "packet.h"
35#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000036#include "log.h"
Damien Millereba71ba2000-04-29 23:57:08 +100037#include "servconf.h"
38#include "compat.h"
39#include "channels.h"
40#include "bufaux.h"
Damien Millereba71ba2000-04-29 23:57:08 +100041#include "auth.h"
42#include "session.h"
43#include "dispatch.h"
Damien Millereba71ba2000-04-29 23:57:08 +100044#include "key.h"
Ben Lindstrom6d40c0f2001-01-29 09:02:24 +000045#include "cipher.h"
Damien Millereba71ba2000-04-29 23:57:08 +100046#include "kex.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000047#include "pathnames.h"
Damien Millereba71ba2000-04-29 23:57:08 +100048#include "uidswap.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100049#include "auth-options.h"
Ben Lindstrom086cf212001-03-05 05:56:40 +000050#include "misc.h"
Ben Lindstrom5eabda32001-04-12 23:34:34 +000051#include "hostfile.h"
52#include "canohost.h"
53#include "tildexpand.h"
Ben Lindstrom551ea372001-06-05 18:56:16 +000054#include "match.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
Damien Miller874d77b2000-10-14 16:23:11 +110064
65static Authctxt *x_authctxt = NULL;
66static int one = 1;
67
68typedef struct Authmethod Authmethod;
69struct Authmethod {
70 char *name;
71 int (*userauth)(Authctxt *authctxt);
72 int *enabled;
73};
74
Damien Millereba71ba2000-04-29 23:57:08 +100075/* protocol */
76
Damien Miller62cee002000-09-23 17:15:56 +110077void input_service_request(int type, int plen, void *ctxt);
78void input_userauth_request(int type, int plen, void *ctxt);
79void protocol_error(int type, int plen, void *ctxt);
Damien Millereba71ba2000-04-29 23:57:08 +100080
Damien Millereba71ba2000-04-29 23:57:08 +100081/* helper */
Damien Miller874d77b2000-10-14 16:23:11 +110082Authmethod *authmethod_lookup(const char *name);
Damien Miller874d77b2000-10-14 16:23:11 +110083char *authmethods_get(void);
Ben Lindstrom5eabda32001-04-12 23:34:34 +000084int user_key_allowed(struct passwd *pw, Key *key);
85int
Ben Lindstrom4aa603c2001-04-19 20:38:06 +000086hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
Ben Lindstrom5eabda32001-04-12 23:34:34 +000087 Key *key);
Damien Millereba71ba2000-04-29 23:57:08 +100088
Damien Miller874d77b2000-10-14 16:23:11 +110089/* auth */
Ben Lindstrom48bd7c12001-01-09 00:35:42 +000090void userauth_banner(void);
Damien Miller5d57e502001-03-30 10:48:31 +100091void userauth_reply(Authctxt *authctxt, int authenticated);
Damien Miller874d77b2000-10-14 16:23:11 +110092int userauth_none(Authctxt *authctxt);
93int userauth_passwd(Authctxt *authctxt);
94int userauth_pubkey(Authctxt *authctxt);
Ben Lindstrom5eabda32001-04-12 23:34:34 +000095int userauth_hostbased(Authctxt *authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +110096int 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 {"password",
106 userauth_passwd,
107 &options.password_authentication},
Ben Lindstromd1f20ec2001-02-10 21:31:53 +0000108 {"keyboard-interactive",
109 userauth_kbdint,
110 &options.kbd_interactive_authentication},
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000111 {"hostbased",
112 userauth_hostbased,
113 &options.hostbased_authentication},
Damien Miller874d77b2000-10-14 16:23:11 +1100114 {NULL, NULL, NULL}
Damien Millereba71ba2000-04-29 23:57:08 +1000115};
Damien Millereba71ba2000-04-29 23:57:08 +1000116
117/*
Damien Miller874d77b2000-10-14 16:23:11 +1100118 * loop until authctxt->success == TRUE
Damien Millereba71ba2000-04-29 23:57:08 +1000119 */
120
121void
122do_authentication2()
123{
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000124 Authctxt *authctxt = authctxt_new();
125
Damien Miller874d77b2000-10-14 16:23:11 +1100126 x_authctxt = authctxt; /*XXX*/
127
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000128 /* challenge-reponse is implemented via keyboard interactive */
Ben Lindstrom551ea372001-06-05 18:56:16 +0000129 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000130 options.kbd_interactive_authentication = 1;
Damien Millerf8154422001-04-25 22:44:14 +1000131 if (options.pam_authentication_via_kbd_int)
132 options.kbd_interactive_authentication = 1;
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000133
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 Lindstromb31783d2001-03-22 02:02:12 +0000137 do_authenticated(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
Kevin Stevesef4eea92001-02-05 12:42:17 +0000203 if (authctxt->attempt++ == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100204 /* setup auth context */
205 struct passwd *pw = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100206 pw = getpwnam(user);
207 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
208 authctxt->pw = pwcopy(pw);
209 authctxt->valid = 1;
210 debug2("input_userauth_request: setting up authctxt for %s", user);
211#ifdef USE_PAM
Damien Miller22e22bf2001-01-19 15:46:38 +1100212 start_pam(pw->pw_name);
Damien Miller874d77b2000-10-14 16:23:11 +1100213#endif
214 } else {
215 log("input_userauth_request: illegal user %s", user);
Damien Miller22e22bf2001-01-19 15:46:38 +1100216#ifdef USE_PAM
217 start_pam("NOUSER");
218#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100219 }
Ben Lindstromc1ba31f2001-02-15 03:14:11 +0000220 setproctitle("%s", pw ? user : "unknown");
Damien Miller874d77b2000-10-14 16:23:11 +1100221 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) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000227 log("input_userauth_request: mismatch: (%s,%s)!=(%s,%s)",
Damien Miller874d77b2000-10-14 16:23:11 +1100228 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 Miller60396b02001-02-18 17:01:00 +1100235#ifdef BSD_AUTH
236 if (authctxt->as) {
237 auth_close(authctxt->as);
238 authctxt->as = NULL;
239 }
240#endif
Damien Millerb70b61f2000-09-16 16:25:12 +1100241
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000242 /* try to authenticate user */
Damien Miller874d77b2000-10-14 16:23:11 +1100243 m = authmethod_lookup(method);
244 if (m != NULL) {
245 debug2("input_userauth_request: try method %s", method);
246 authenticated = m->userauth(authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100247 }
Damien Miller5d57e502001-03-30 10:48:31 +1000248 userauth_finish(authctxt, authenticated, method);
249
250 xfree(service);
251 xfree(user);
252 xfree(method);
253}
254
255void
256userauth_finish(Authctxt *authctxt, int authenticated, char *method)
257{
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000258 if (!authctxt->valid && authenticated)
259 fatal("INTERNAL ERROR: authenticated invalid user %s",
260 authctxt->user);
Damien Millerb70b61f2000-09-16 16:25:12 +1100261
Damien Miller874d77b2000-10-14 16:23:11 +1100262 /* Special handling for root */
Ben Lindstromd8a90212001-02-15 03:08:27 +0000263 if (authenticated && authctxt->pw->pw_uid == 0 &&
264 !auth_root_allowed(method))
Damien Millereba71ba2000-04-29 23:57:08 +1000265 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000266
267#ifdef USE_PAM
Damien Millerc83de6d2001-02-16 14:17:59 +1100268 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
269 NULL))
Damien Millerb70b61f2000-09-16 16:25:12 +1100270 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000271#endif /* USE_PAM */
272
Damien Miller874d77b2000-10-14 16:23:11 +1100273 /* Log before sending the reply */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000274 auth_log(authctxt, authenticated, method, " ssh2");
275
276 if (!authctxt->postponed)
277 userauth_reply(authctxt, authenticated);
Damien Miller874d77b2000-10-14 16:23:11 +1100278}
279
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000280void
281userauth_banner(void)
282{
283 struct stat st;
284 char *banner = NULL;
285 off_t len, n;
286 int fd;
287
288 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
289 return;
Ben Lindstrom0cae0402001-04-04 23:47:52 +0000290 if ((fd = open(options.banner, O_RDONLY)) < 0)
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000291 return;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000292 if (fstat(fd, &st) < 0)
293 goto done;
294 len = st.st_size;
295 banner = xmalloc(len + 1);
296 if ((n = read(fd, banner, len)) < 0)
297 goto done;
298 banner[n] = '\0';
299 packet_start(SSH2_MSG_USERAUTH_BANNER);
300 packet_put_cstring(banner);
301 packet_put_cstring(""); /* language, unused */
302 packet_send();
303 debug("userauth_banner: sent");
304done:
305 if (banner)
306 xfree(banner);
307 close(fd);
308 return;
309}
Damien Miller874d77b2000-10-14 16:23:11 +1100310
Kevin Stevesef4eea92001-02-05 12:42:17 +0000311void
Damien Miller874d77b2000-10-14 16:23:11 +1100312userauth_reply(Authctxt *authctxt, int authenticated)
313{
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000314 char *methods;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000315
Damien Millere247cc42000-05-07 12:03:14 +1000316 /* XXX todo: check if multiple auth methods are needed */
Kevin Steves4abe4de2001-02-08 19:16:32 +0000317 if (authenticated == 1) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000318#ifdef WITH_AIXAUTHENTICATE
319 /* We don't have a pty yet, so just label the line as "ssh" */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000320 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
321 get_canonical_hostname(options.reverse_mapping_check),
Damien Miller33804262001-02-04 23:20:18 +1100322 "ssh", &aixloginmsg) < 0)
Damien Millerd2c208a2000-05-17 22:00:02 +1000323 aixloginmsg = NULL;
324#endif /* WITH_AIXAUTHENTICATE */
Damien Millereba71ba2000-04-29 23:57:08 +1000325 /* turn off userauth */
326 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
327 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
328 packet_send();
329 packet_write_wait();
330 /* now we can break out */
Damien Miller874d77b2000-10-14 16:23:11 +1100331 authctxt->success = 1;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000332 } else {
333 if (authctxt->failures++ > AUTH_FAIL_MAX)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000334 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000335 methods = authmethods_get();
Damien Millereba71ba2000-04-29 23:57:08 +1000336 packet_start(SSH2_MSG_USERAUTH_FAILURE);
Damien Miller874d77b2000-10-14 16:23:11 +1100337 packet_put_cstring(methods);
338 packet_put_char(0); /* XXX partial success, unused */
Damien Millereba71ba2000-04-29 23:57:08 +1000339 packet_send();
340 packet_write_wait();
Damien Miller874d77b2000-10-14 16:23:11 +1100341 xfree(methods);
Damien Millereba71ba2000-04-29 23:57:08 +1000342 }
Damien Millereba71ba2000-04-29 23:57:08 +1000343}
344
345int
Damien Miller874d77b2000-10-14 16:23:11 +1100346userauth_none(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000347{
Damien Miller874d77b2000-10-14 16:23:11 +1100348 /* disable method "none", only allowed one time */
349 Authmethod *m = authmethod_lookup("none");
350 if (m != NULL)
351 m->enabled = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000352 packet_done();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000353 userauth_banner();
Damien Millerb8c656e2000-06-28 15:22:41 +1000354
Damien Miller874d77b2000-10-14 16:23:11 +1100355 if (authctxt->valid == 0)
356 return(0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000357
Damien Miller874d77b2000-10-14 16:23:11 +1100358#ifdef HAVE_CYGWIN
359 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
360 return(0);
361#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000362#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100363 return auth_pam_password(authctxt->pw, "");
Damien Millerb8c656e2000-06-28 15:22:41 +1000364#elif defined(HAVE_OSF_SIA)
Damien Miller92ddb7d2001-02-14 01:25:23 +1100365 return 0;
Damien Millerb8c656e2000-06-28 15:22:41 +1000366#else /* !HAVE_OSF_SIA && !USE_PAM */
Damien Miller60396b02001-02-18 17:01:00 +1100367 return auth_password(authctxt, "");
Damien Millereba71ba2000-04-29 23:57:08 +1000368#endif /* USE_PAM */
369}
Damien Miller874d77b2000-10-14 16:23:11 +1100370
Damien Millereba71ba2000-04-29 23:57:08 +1000371int
Damien Miller874d77b2000-10-14 16:23:11 +1100372userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000373{
374 char *password;
375 int authenticated = 0;
376 int change;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000377 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000378 change = packet_get_char();
379 if (change)
380 log("password change not supported");
381 password = packet_get_string(&len);
382 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100383 if (authctxt->valid &&
384#ifdef HAVE_CYGWIN
385 check_nt_auth(1, authctxt->pw->pw_uid) &&
386#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000387#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100388 auth_pam_password(authctxt->pw, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000389#elif defined(HAVE_OSF_SIA)
Damien Miller92ddb7d2001-02-14 01:25:23 +1100390 auth_sia_password(authctxt->user, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000391#else /* !USE_PAM && !HAVE_OSF_SIA */
Damien Miller60396b02001-02-18 17:01:00 +1100392 auth_password(authctxt, password) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000393#endif /* USE_PAM */
394 authenticated = 1;
395 memset(password, 0, len);
396 xfree(password);
397 return authenticated;
398}
Damien Miller874d77b2000-10-14 16:23:11 +1100399
Damien Millereba71ba2000-04-29 23:57:08 +1000400int
Damien Miller874d77b2000-10-14 16:23:11 +1100401userauth_kbdint(Authctxt *authctxt)
402{
403 int authenticated = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000404 char *lang, *devs;
Damien Miller874d77b2000-10-14 16:23:11 +1100405
406 lang = packet_get_string(NULL);
407 devs = packet_get_string(NULL);
408 packet_done();
409
Ben Lindstrom551ea372001-06-05 18:56:16 +0000410 debug("keyboard-interactive devs %s", devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000411
Ben Lindstrom551ea372001-06-05 18:56:16 +0000412 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000413 authenticated = auth2_challenge(authctxt, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000414
Damien Millerb8481582000-12-03 11:51:51 +1100415#ifdef USE_PAM
Damien Millerf8154422001-04-25 22:44:14 +1000416 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
Damien Millerb8481582000-12-03 11:51:51 +1100417 authenticated = auth2_pam(authctxt);
418#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100419 xfree(devs);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000420 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +1100421#ifdef HAVE_CYGWIN
422 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
423 return(0);
424#endif
425 return authenticated;
426}
427
428int
429userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000430{
431 Buffer b;
432 Key *key;
433 char *pkalg, *pkblob, *sig;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000434 u_int alen, blen, slen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100435 int have_sig, pktype;
Damien Millereba71ba2000-04-29 23:57:08 +1000436 int authenticated = 0;
437
Damien Miller874d77b2000-10-14 16:23:11 +1100438 if (!authctxt->valid) {
439 debug2("userauth_pubkey: disabled because of invalid user");
Damien Millereba71ba2000-04-29 23:57:08 +1000440 return 0;
441 }
442 have_sig = packet_get_char();
Ben Lindstromd121f612000-12-03 17:00:47 +0000443 if (datafellows & SSH_BUG_PKAUTH) {
444 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
445 /* no explicit pkalg given */
446 pkblob = packet_get_string(&blen);
447 buffer_init(&b);
448 buffer_append(&b, pkblob, blen);
449 /* so we have to extract the pkalg from the pkblob */
450 pkalg = buffer_get_string(&b, &alen);
451 buffer_free(&b);
452 } else {
453 pkalg = packet_get_string(&alen);
454 pkblob = packet_get_string(&blen);
455 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100456 pktype = key_type_from_name(pkalg);
457 if (pktype == KEY_UNSPEC) {
Ben Lindstromd121f612000-12-03 17:00:47 +0000458 /* this is perfectly legal */
459 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
Damien Miller874d77b2000-10-14 16:23:11 +1100460 xfree(pkalg);
Ben Lindstromd121f612000-12-03 17:00:47 +0000461 xfree(pkblob);
Damien Millereba71ba2000-04-29 23:57:08 +1000462 return 0;
463 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100464 key = key_from_blob(pkblob, blen);
Damien Millereba71ba2000-04-29 23:57:08 +1000465 if (key != NULL) {
466 if (have_sig) {
467 sig = packet_get_string(&slen);
468 packet_done();
469 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100470 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000471 buffer_append(&b, session_id2, session_id2_len);
Damien Miller50a41ed2000-10-16 12:14:42 +1100472 } else {
473 buffer_put_string(&b, session_id2, session_id2_len);
Damien Miller6536c7d2000-06-22 21:32:31 +1000474 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000475 /* reconstruct packet */
Damien Millereba71ba2000-04-29 23:57:08 +1000476 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller874d77b2000-10-14 16:23:11 +1100477 buffer_put_cstring(&b, authctxt->user);
Damien Millerf6d9e222000-06-18 14:50:44 +1000478 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000479 datafellows & SSH_BUG_PKSERVICE ?
Damien Millerf6d9e222000-06-18 14:50:44 +1000480 "ssh-userauth" :
Damien Miller874d77b2000-10-14 16:23:11 +1100481 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000482 if (datafellows & SSH_BUG_PKAUTH) {
483 buffer_put_char(&b, have_sig);
484 } else {
485 buffer_put_cstring(&b, "publickey");
486 buffer_put_char(&b, have_sig);
Ben Lindstrom3f364962001-04-19 20:50:07 +0000487 buffer_put_cstring(&b, pkalg);
Ben Lindstromd121f612000-12-03 17:00:47 +0000488 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000489 buffer_put_string(&b, pkblob, blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100490#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000491 buffer_dump(&b);
492#endif
493 /* test for correct signature */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100494 if (user_key_allowed(authctxt->pw, key) &&
495 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000496 authenticated = 1;
497 buffer_clear(&b);
498 xfree(sig);
499 } else {
Damien Miller874d77b2000-10-14 16:23:11 +1100500 debug("test whether pkalg/pkblob are acceptable");
Damien Millereba71ba2000-04-29 23:57:08 +1000501 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100502
Damien Millereba71ba2000-04-29 23:57:08 +1000503 /* XXX fake reply and always send PK_OK ? */
Damien Millere247cc42000-05-07 12:03:14 +1000504 /*
505 * XXX this allows testing whether a user is allowed
506 * to login: if you happen to have a valid pubkey this
507 * message is sent. the message is NEVER sent at all
508 * if a user is not allowed to login. is this an
509 * issue? -markus
510 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100511 if (user_key_allowed(authctxt->pw, key)) {
Damien Millereba71ba2000-04-29 23:57:08 +1000512 packet_start(SSH2_MSG_USERAUTH_PK_OK);
513 packet_put_string(pkalg, alen);
514 packet_put_string(pkblob, blen);
515 packet_send();
516 packet_write_wait();
Kevin Steves4abe4de2001-02-08 19:16:32 +0000517 authctxt->postponed = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000518 }
519 }
Damien Miller874d77b2000-10-14 16:23:11 +1100520 if (authenticated != 1)
521 auth_clear_options();
Damien Millereba71ba2000-04-29 23:57:08 +1000522 key_free(key);
523 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100524 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
Damien Millereba71ba2000-04-29 23:57:08 +1000525 xfree(pkalg);
526 xfree(pkblob);
Damien Miller874d77b2000-10-14 16:23:11 +1100527#ifdef HAVE_CYGWIN
528 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
529 return(0);
530#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000531 return authenticated;
532}
533
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000534int
535userauth_hostbased(Authctxt *authctxt)
536{
537 Buffer b;
538 Key *key;
Ben Lindstrom671388f2001-04-19 20:40:45 +0000539 char *pkalg, *pkblob, *sig, *cuser, *chost, *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000540 u_int alen, blen, slen;
541 int pktype;
542 int authenticated = 0;
543
544 if (!authctxt->valid) {
545 debug2("userauth_hostbased: disabled because of invalid user");
546 return 0;
547 }
548 pkalg = packet_get_string(&alen);
549 pkblob = packet_get_string(&blen);
550 chost = packet_get_string(NULL);
551 cuser = packet_get_string(NULL);
552 sig = packet_get_string(&slen);
553
554 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
555 cuser, chost, pkalg, slen);
556#ifdef DEBUG_PK
557 debug("signature:");
558 buffer_init(&b);
559 buffer_append(&b, sig, slen);
560 buffer_dump(&b);
561 buffer_free(&b);
562#endif
563 pktype = key_type_from_name(pkalg);
564 if (pktype == KEY_UNSPEC) {
565 /* this is perfectly legal */
566 log("userauth_hostbased: unsupported "
567 "public key algorithm: %s", pkalg);
568 goto done;
569 }
570 key = key_from_blob(pkblob, blen);
571 if (key == NULL) {
572 debug("userauth_hostbased: cannot decode key: %s", pkalg);
573 goto done;
574 }
Ben Lindstrom671388f2001-04-19 20:40:45 +0000575 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
576 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000577 buffer_init(&b);
Ben Lindstrom671388f2001-04-19 20:40:45 +0000578 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000579 /* reconstruct packet */
580 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
581 buffer_put_cstring(&b, authctxt->user);
Ben Lindstrom671388f2001-04-19 20:40:45 +0000582 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000583 buffer_put_cstring(&b, "hostbased");
584 buffer_put_string(&b, pkalg, alen);
585 buffer_put_string(&b, pkblob, blen);
586 buffer_put_cstring(&b, chost);
587 buffer_put_cstring(&b, cuser);
588#ifdef DEBUG_PK
589 buffer_dump(&b);
590#endif
591 /* test for allowed key and correct signature */
592 if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
593 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
594 authenticated = 1;
595
596 buffer_clear(&b);
597 key_free(key);
598
599done:
600 debug2("userauth_hostbased: authenticated %d", authenticated);
601 xfree(pkalg);
602 xfree(pkblob);
603 xfree(cuser);
604 xfree(chost);
605 xfree(sig);
606 return authenticated;
607}
608
Damien Miller874d77b2000-10-14 16:23:11 +1100609/* get current user */
Damien Millereba71ba2000-04-29 23:57:08 +1000610
611struct passwd*
612auth_get_user(void)
613{
Damien Miller874d77b2000-10-14 16:23:11 +1100614 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000615}
616
Damien Miller874d77b2000-10-14 16:23:11 +1100617#define DELIM ","
Damien Millereba71ba2000-04-29 23:57:08 +1000618
Damien Miller874d77b2000-10-14 16:23:11 +1100619char *
620authmethods_get(void)
621{
622 Authmethod *method = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000623 u_int size = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100624 char *list;
625
626 for (method = authmethods; method->name != NULL; method++) {
627 if (strcmp(method->name, "none") == 0)
628 continue;
629 if (method->enabled != NULL && *(method->enabled) != 0) {
630 if (size != 0)
631 size += strlen(DELIM);
632 size += strlen(method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000633 }
634 }
Damien Miller874d77b2000-10-14 16:23:11 +1100635 size++; /* trailing '\0' */
636 list = xmalloc(size);
637 list[0] = '\0';
638
639 for (method = authmethods; method->name != NULL; method++) {
640 if (strcmp(method->name, "none") == 0)
641 continue;
642 if (method->enabled != NULL && *(method->enabled) != 0) {
643 if (list[0] != '\0')
644 strlcat(list, DELIM, size);
645 strlcat(list, method->name, size);
646 }
647 }
648 return list;
649}
650
651Authmethod *
652authmethod_lookup(const char *name)
653{
654 Authmethod *method = NULL;
655 if (name != NULL)
656 for (method = authmethods; method->name != NULL; method++)
657 if (method->enabled != NULL &&
658 *(method->enabled) != 0 &&
659 strcmp(name, method->name) == 0)
660 return method;
661 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
662 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000663}
664
665/* return 1 if user allows given key */
666int
Damien Miller0bc1bd82000-11-13 22:57:25 +1100667user_key_allowed(struct passwd *pw, Key *key)
Damien Millereba71ba2000-04-29 23:57:08 +1000668{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000669 char line[8192], file[MAXPATHLEN];
Damien Millereba71ba2000-04-29 23:57:08 +1000670 int found_key = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000671 FILE *f;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000672 u_long linenum = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000673 struct stat st;
674 Key *found;
675
Damien Miller874d77b2000-10-14 16:23:11 +1100676 if (pw == NULL)
677 return 0;
678
Damien Millereba71ba2000-04-29 23:57:08 +1000679 /* Temporarily use the user's uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000680 temporarily_use_uid(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000681
682 /* The authorized keys. */
683 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000684 _PATH_SSH_USER_PERMITTED_KEYS2);
Damien Millereba71ba2000-04-29 23:57:08 +1000685
686 /* Fail quietly if file does not exist */
687 if (stat(file, &st) < 0) {
688 /* Restore the privileged uid. */
689 restore_uid();
690 return 0;
691 }
692 /* Open the file containing the authorized keys. */
693 f = fopen(file, "r");
694 if (!f) {
695 /* Restore the privileged uid. */
696 restore_uid();
697 return 0;
698 }
699 if (options.strict_modes) {
700 int fail = 0;
701 char buf[1024];
702 /* Check open file in order to avoid open/stat races */
703 if (fstat(fileno(f), &st) < 0 ||
704 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
705 (st.st_mode & 022) != 0) {
Damien Millerd3444942000-09-30 14:20:03 +1100706 snprintf(buf, sizeof buf,
707 "%s authentication refused for %.100s: "
708 "bad ownership or modes for '%s'.",
709 key_type(key), pw->pw_name, file);
Damien Millereba71ba2000-04-29 23:57:08 +1000710 fail = 1;
711 } else {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000712 /* Check path to _PATH_SSH_USER_PERMITTED_KEYS */
Damien Millereba71ba2000-04-29 23:57:08 +1000713 int i;
714 static const char *check[] = {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000715 "", _PATH_SSH_USER_DIR, NULL
Damien Millereba71ba2000-04-29 23:57:08 +1000716 };
717 for (i = 0; check[i]; i++) {
718 snprintf(line, sizeof line, "%.500s/%.100s",
719 pw->pw_dir, check[i]);
720 if (stat(line, &st) < 0 ||
721 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
722 (st.st_mode & 022) != 0) {
723 snprintf(buf, sizeof buf,
Damien Millerd3444942000-09-30 14:20:03 +1100724 "%s authentication refused for %.100s: "
Damien Millereba71ba2000-04-29 23:57:08 +1000725 "bad ownership or modes for '%s'.",
Damien Millerd3444942000-09-30 14:20:03 +1100726 key_type(key), pw->pw_name, line);
Damien Millereba71ba2000-04-29 23:57:08 +1000727 fail = 1;
728 break;
729 }
730 }
731 }
732 if (fail) {
Damien Millereba71ba2000-04-29 23:57:08 +1000733 fclose(f);
Ben Lindstrom204e4882001-03-05 06:47:00 +0000734 log("%s", buf);
Damien Millereba71ba2000-04-29 23:57:08 +1000735 restore_uid();
736 return 0;
737 }
738 }
739 found_key = 0;
Damien Millerd3444942000-09-30 14:20:03 +1100740 found = key_new(key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000741
742 while (fgets(line, sizeof(line), f)) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000743 char *cp, *options = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000744 linenum++;
745 /* Skip leading whitespace, empty and comment lines. */
746 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
747 ;
748 if (!*cp || *cp == '\n' || *cp == '#')
749 continue;
Damien Millerf6d9e222000-06-18 14:50:44 +1000750
Damien Miller0bc1bd82000-11-13 22:57:25 +1100751 if (key_read(found, &cp) == -1) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000752 /* no key? check if there are options for this key */
753 int quoted = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100754 debug2("user_key_allowed: check options: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000755 options = cp;
756 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
757 if (*cp == '\\' && cp[1] == '"')
758 cp++; /* Skip both */
759 else if (*cp == '"')
760 quoted = !quoted;
761 }
762 /* Skip remaining whitespace. */
763 for (; *cp == ' ' || *cp == '\t'; cp++)
764 ;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100765 if (key_read(found, &cp) == -1) {
766 debug2("user_key_allowed: advance: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000767 /* still no key? advance to next line*/
768 continue;
769 }
770 }
771 if (key_equal(found, key) &&
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000772 auth_parse_options(pw, options, file, linenum) == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000773 found_key = 1;
774 debug("matching key found: file %s, line %ld",
775 file, linenum);
776 break;
777 }
778 }
779 restore_uid();
780 fclose(f);
781 key_free(found);
Ben Lindstromb54873a2001-03-11 20:01:55 +0000782 if (!found_key)
783 debug2("key not found");
Damien Millereba71ba2000-04-29 23:57:08 +1000784 return found_key;
785}
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000786
787/* return 1 if given hostkey is allowed */
788int
Ben Lindstrom4aa603c2001-04-19 20:38:06 +0000789hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000790 Key *key)
791{
792 Key *found;
793 const char *resolvedname, *ipaddr, *lookup;
794 struct stat st;
795 char *user_hostfile;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000796 int host_status, len;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000797
798 resolvedname = get_canonical_hostname(options.reverse_mapping_check);
799 ipaddr = get_remote_ipaddr();
800
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000801 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
802 chost, resolvedname, ipaddr);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000803
804 if (options.hostbased_uses_name_from_packet_only) {
805 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
806 return 0;
807 lookup = chost;
808 } else {
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000809 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
810 debug2("stripping trailing dot from chost %s", chost);
811 chost[len - 1] = '\0';
812 }
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000813 if (strcasecmp(resolvedname, chost) != 0)
814 log("userauth_hostbased mismatch: "
815 "client sends %s, but we resolve %s to %s",
816 chost, ipaddr, resolvedname);
817 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
818 return 0;
819 lookup = resolvedname;
820 }
821 debug2("userauth_hostbased: access allowed by auth_rhosts2");
822
823 /* XXX this is copied from auth-rh-rsa.c and should be shared */
824 found = key_new(key->type);
825 host_status = check_host_in_hostfile(_PATH_SSH_SYSTEM_HOSTFILE2, lookup,
826 key, found, NULL);
827
828 if (host_status != HOST_OK && !options.ignore_user_known_hosts) {
829 user_hostfile = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE2,
830 pw->pw_uid);
831 if (options.strict_modes &&
832 (stat(user_hostfile, &st) == 0) &&
833 ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
834 (st.st_mode & 022) != 0)) {
835 log("Hostbased authentication refused for %.100s: "
836 "bad owner or modes for %.200s",
837 pw->pw_name, user_hostfile);
838 } else {
839 temporarily_use_uid(pw);
840 host_status = check_host_in_hostfile(user_hostfile,
841 lookup, key, found, NULL);
842 restore_uid();
843 }
844 xfree(user_hostfile);
845 }
846 key_free(found);
847
848 debug2("userauth_hostbased: key %s for %s", host_status == HOST_OK ?
849 "ok" : "not found", lookup);
850 return (host_status == HOST_OK);
851}