blob: 554ca4c10deab5af4a7eddf0fa2c787397813d82 [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 Lindstrom9d0c0662001-06-09 01:40:00 +000026RCSID("$OpenBSD: auth2.c,v 1.62 2001/06/07 19:57:53 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"
Ben Lindstromc7637672001-06-09 00:36:26 +000039#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100040#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 Miller874d77b2000-10-14 16:23:11 +110091int userauth_none(Authctxt *authctxt);
92int userauth_passwd(Authctxt *authctxt);
93int userauth_pubkey(Authctxt *authctxt);
Ben Lindstrom5eabda32001-04-12 23:34:34 +000094int userauth_hostbased(Authctxt *authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +110095int userauth_kbdint(Authctxt *authctxt);
96
97Authmethod authmethods[] = {
98 {"none",
99 userauth_none,
100 &one},
101 {"publickey",
102 userauth_pubkey,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100103 &options.pubkey_authentication},
Damien Miller874d77b2000-10-14 16:23:11 +1100104 {"password",
105 userauth_passwd,
106 &options.password_authentication},
Ben Lindstromd1f20ec2001-02-10 21:31:53 +0000107 {"keyboard-interactive",
108 userauth_kbdint,
109 &options.kbd_interactive_authentication},
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000110 {"hostbased",
111 userauth_hostbased,
112 &options.hostbased_authentication},
Damien Miller874d77b2000-10-14 16:23:11 +1100113 {NULL, NULL, NULL}
Damien Millereba71ba2000-04-29 23:57:08 +1000114};
Damien Millereba71ba2000-04-29 23:57:08 +1000115
116/*
Damien Miller874d77b2000-10-14 16:23:11 +1100117 * loop until authctxt->success == TRUE
Damien Millereba71ba2000-04-29 23:57:08 +1000118 */
119
120void
121do_authentication2()
122{
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000123 Authctxt *authctxt = authctxt_new();
124
Damien Miller874d77b2000-10-14 16:23:11 +1100125 x_authctxt = authctxt; /*XXX*/
126
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000127 /* challenge-reponse is implemented via keyboard interactive */
Ben Lindstrom551ea372001-06-05 18:56:16 +0000128 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000129 options.kbd_interactive_authentication = 1;
Damien Millerf8154422001-04-25 22:44:14 +1000130 if (options.pam_authentication_via_kbd_int)
131 options.kbd_interactive_authentication = 1;
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000132
Damien Millereba71ba2000-04-29 23:57:08 +1000133 dispatch_init(&protocol_error);
134 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
Damien Miller874d77b2000-10-14 16:23:11 +1100135 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000136 do_authenticated(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000137}
138
139void
Damien Miller62cee002000-09-23 17:15:56 +1100140protocol_error(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000141{
142 log("auth: protocol error: type %d plen %d", type, plen);
143 packet_start(SSH2_MSG_UNIMPLEMENTED);
144 packet_put_int(0);
145 packet_send();
146 packet_write_wait();
147}
148
149void
Damien Miller62cee002000-09-23 17:15:56 +1100150input_service_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000151{
Damien Miller874d77b2000-10-14 16:23:11 +1100152 Authctxt *authctxt = ctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000153 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000154 int accept = 0;
155 char *service = packet_get_string(&len);
156 packet_done();
157
Damien Miller874d77b2000-10-14 16:23:11 +1100158 if (authctxt == NULL)
159 fatal("input_service_request: no authctxt");
160
Damien Millereba71ba2000-04-29 23:57:08 +1000161 if (strcmp(service, "ssh-userauth") == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100162 if (!authctxt->success) {
Damien Millereba71ba2000-04-29 23:57:08 +1000163 accept = 1;
164 /* now we can handle user-auth requests */
165 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
166 }
167 }
168 /* XXX all other service requests are denied */
169
170 if (accept) {
171 packet_start(SSH2_MSG_SERVICE_ACCEPT);
172 packet_put_cstring(service);
173 packet_send();
174 packet_write_wait();
175 } else {
176 debug("bad service request %s", service);
177 packet_disconnect("bad service request %s", service);
178 }
179 xfree(service);
180}
181
182void
Damien Miller62cee002000-09-23 17:15:56 +1100183input_userauth_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000184{
Damien Miller874d77b2000-10-14 16:23:11 +1100185 Authctxt *authctxt = ctxt;
186 Authmethod *m = NULL;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000187 char *user, *service, *method, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000188 int authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000189
Damien Miller874d77b2000-10-14 16:23:11 +1100190 if (authctxt == NULL)
191 fatal("input_userauth_request: no authctxt");
Damien Millereba71ba2000-04-29 23:57:08 +1000192
Damien Miller874d77b2000-10-14 16:23:11 +1100193 user = packet_get_string(NULL);
194 service = packet_get_string(NULL);
195 method = packet_get_string(NULL);
196 debug("userauth-request for user %s service %s method %s", user, service, method);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000197 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
Damien Miller874d77b2000-10-14 16:23:11 +1100198
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000199 if ((style = strchr(user, ':')) != NULL)
200 *style++ = 0;
201
Kevin Stevesef4eea92001-02-05 12:42:17 +0000202 if (authctxt->attempt++ == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100203 /* setup auth context */
204 struct passwd *pw = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100205 pw = getpwnam(user);
206 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
207 authctxt->pw = pwcopy(pw);
208 authctxt->valid = 1;
209 debug2("input_userauth_request: setting up authctxt for %s", user);
210#ifdef USE_PAM
Damien Miller22e22bf2001-01-19 15:46:38 +1100211 start_pam(pw->pw_name);
Damien Miller874d77b2000-10-14 16:23:11 +1100212#endif
213 } else {
214 log("input_userauth_request: illegal user %s", user);
Damien Miller22e22bf2001-01-19 15:46:38 +1100215#ifdef USE_PAM
216 start_pam("NOUSER");
217#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100218 }
Ben Lindstromc1ba31f2001-02-15 03:14:11 +0000219 setproctitle("%s", pw ? user : "unknown");
Damien Miller874d77b2000-10-14 16:23:11 +1100220 authctxt->user = xstrdup(user);
221 authctxt->service = xstrdup(service);
Ben Lindstrom9d0c0662001-06-09 01:40:00 +0000222 authctxt->style = style ? xstrdup(style) : NULL;
223 } else if (strcmp(user, authctxt->user) != 0 ||
224 strcmp(service, authctxt->service) != 0) {
225 packet_disconnect("Change of username or service not allowed: "
226 "(%s,%s) -> (%s,%s)",
227 authctxt->user, authctxt->service, user, service);
Damien Millereba71ba2000-04-29 23:57:08 +1000228 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000229 /* reset state */
230 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
231 authctxt->postponed = 0;
Damien Miller60396b02001-02-18 17:01:00 +1100232#ifdef BSD_AUTH
233 if (authctxt->as) {
234 auth_close(authctxt->as);
235 authctxt->as = NULL;
236 }
237#endif
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 }
Damien Miller5d57e502001-03-30 10:48:31 +1000245 userauth_finish(authctxt, authenticated, method);
246
247 xfree(service);
248 xfree(user);
249 xfree(method);
250}
251
252void
253userauth_finish(Authctxt *authctxt, int authenticated, char *method)
254{
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000255 char *methods;
256
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000257 if (!authctxt->valid && authenticated)
258 fatal("INTERNAL ERROR: authenticated invalid user %s",
259 authctxt->user);
Damien Millerb70b61f2000-09-16 16:25:12 +1100260
Damien Miller874d77b2000-10-14 16:23:11 +1100261 /* Special handling for root */
Ben Lindstromd8a90212001-02-15 03:08:27 +0000262 if (authenticated && authctxt->pw->pw_uid == 0 &&
263 !auth_root_allowed(method))
Damien Millereba71ba2000-04-29 23:57:08 +1000264 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000265
266#ifdef USE_PAM
Damien Millerc83de6d2001-02-16 14:17:59 +1100267 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
268 NULL))
Damien Millerb70b61f2000-09-16 16:25:12 +1100269 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000270#endif /* USE_PAM */
271
Damien Miller874d77b2000-10-14 16:23:11 +1100272 /* Log before sending the reply */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000273 auth_log(authctxt, authenticated, method, " ssh2");
274
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000275 if (authctxt->postponed)
276 return;
277
278 /* XXX todo: check if multiple auth methods are needed */
279 if (authenticated == 1) {
280 /* turn off userauth */
281 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
282 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
283 packet_send();
284 packet_write_wait();
285 /* now we can break out */
286 authctxt->success = 1;
287 } else {
288 if (authctxt->failures++ > AUTH_FAIL_MAX)
289 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
290 methods = authmethods_get();
291 packet_start(SSH2_MSG_USERAUTH_FAILURE);
292 packet_put_cstring(methods);
293 packet_put_char(0); /* XXX partial success, unused */
294 packet_send();
295 packet_write_wait();
296 xfree(methods);
297 }
Damien Miller874d77b2000-10-14 16:23:11 +1100298}
299
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000300void
301userauth_banner(void)
302{
303 struct stat st;
304 char *banner = NULL;
305 off_t len, n;
306 int fd;
307
308 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
309 return;
Ben Lindstrom0cae0402001-04-04 23:47:52 +0000310 if ((fd = open(options.banner, O_RDONLY)) < 0)
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000311 return;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000312 if (fstat(fd, &st) < 0)
313 goto done;
314 len = st.st_size;
315 banner = xmalloc(len + 1);
316 if ((n = read(fd, banner, len)) < 0)
317 goto done;
318 banner[n] = '\0';
319 packet_start(SSH2_MSG_USERAUTH_BANNER);
320 packet_put_cstring(banner);
321 packet_put_cstring(""); /* language, unused */
322 packet_send();
323 debug("userauth_banner: sent");
324done:
325 if (banner)
326 xfree(banner);
327 close(fd);
328 return;
329}
Damien Miller874d77b2000-10-14 16:23:11 +1100330
Damien Millereba71ba2000-04-29 23:57:08 +1000331int
Damien Miller874d77b2000-10-14 16:23:11 +1100332userauth_none(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000333{
Damien Miller874d77b2000-10-14 16:23:11 +1100334 /* disable method "none", only allowed one time */
335 Authmethod *m = authmethod_lookup("none");
336 if (m != NULL)
337 m->enabled = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000338 packet_done();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000339 userauth_banner();
Damien Millerb8c656e2000-06-28 15:22:41 +1000340
Damien Miller874d77b2000-10-14 16:23:11 +1100341 if (authctxt->valid == 0)
342 return(0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000343
Damien Miller874d77b2000-10-14 16:23:11 +1100344#ifdef HAVE_CYGWIN
345 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
346 return(0);
347#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000348#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100349 return auth_pam_password(authctxt->pw, "");
Damien Millerb8c656e2000-06-28 15:22:41 +1000350#elif defined(HAVE_OSF_SIA)
Damien Miller92ddb7d2001-02-14 01:25:23 +1100351 return 0;
Damien Millerb8c656e2000-06-28 15:22:41 +1000352#else /* !HAVE_OSF_SIA && !USE_PAM */
Damien Miller60396b02001-02-18 17:01:00 +1100353 return auth_password(authctxt, "");
Damien Millereba71ba2000-04-29 23:57:08 +1000354#endif /* USE_PAM */
355}
Damien Miller874d77b2000-10-14 16:23:11 +1100356
Damien Millereba71ba2000-04-29 23:57:08 +1000357int
Damien Miller874d77b2000-10-14 16:23:11 +1100358userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000359{
360 char *password;
361 int authenticated = 0;
362 int change;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000363 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000364 change = packet_get_char();
365 if (change)
366 log("password change not supported");
367 password = packet_get_string(&len);
368 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100369 if (authctxt->valid &&
370#ifdef HAVE_CYGWIN
371 check_nt_auth(1, authctxt->pw->pw_uid) &&
372#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000373#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100374 auth_pam_password(authctxt->pw, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000375#elif defined(HAVE_OSF_SIA)
Damien Miller92ddb7d2001-02-14 01:25:23 +1100376 auth_sia_password(authctxt->user, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000377#else /* !USE_PAM && !HAVE_OSF_SIA */
Damien Miller60396b02001-02-18 17:01:00 +1100378 auth_password(authctxt, password) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000379#endif /* USE_PAM */
380 authenticated = 1;
381 memset(password, 0, len);
382 xfree(password);
383 return authenticated;
384}
Damien Miller874d77b2000-10-14 16:23:11 +1100385
Damien Millereba71ba2000-04-29 23:57:08 +1000386int
Damien Miller874d77b2000-10-14 16:23:11 +1100387userauth_kbdint(Authctxt *authctxt)
388{
389 int authenticated = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000390 char *lang, *devs;
Damien Miller874d77b2000-10-14 16:23:11 +1100391
392 lang = packet_get_string(NULL);
393 devs = packet_get_string(NULL);
394 packet_done();
395
Ben Lindstrom551ea372001-06-05 18:56:16 +0000396 debug("keyboard-interactive devs %s", devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000397
Ben Lindstrom551ea372001-06-05 18:56:16 +0000398 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000399 authenticated = auth2_challenge(authctxt, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000400
Damien Millerb8481582000-12-03 11:51:51 +1100401#ifdef USE_PAM
Damien Millerf8154422001-04-25 22:44:14 +1000402 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
Damien Millerb8481582000-12-03 11:51:51 +1100403 authenticated = auth2_pam(authctxt);
404#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100405 xfree(devs);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000406 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +1100407#ifdef HAVE_CYGWIN
408 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
409 return(0);
410#endif
411 return authenticated;
412}
413
414int
415userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000416{
417 Buffer b;
418 Key *key;
419 char *pkalg, *pkblob, *sig;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000420 u_int alen, blen, slen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100421 int have_sig, pktype;
Damien Millereba71ba2000-04-29 23:57:08 +1000422 int authenticated = 0;
423
Damien Miller874d77b2000-10-14 16:23:11 +1100424 if (!authctxt->valid) {
425 debug2("userauth_pubkey: disabled because of invalid user");
Damien Millereba71ba2000-04-29 23:57:08 +1000426 return 0;
427 }
428 have_sig = packet_get_char();
Ben Lindstromd121f612000-12-03 17:00:47 +0000429 if (datafellows & SSH_BUG_PKAUTH) {
430 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
431 /* no explicit pkalg given */
432 pkblob = packet_get_string(&blen);
433 buffer_init(&b);
434 buffer_append(&b, pkblob, blen);
435 /* so we have to extract the pkalg from the pkblob */
436 pkalg = buffer_get_string(&b, &alen);
437 buffer_free(&b);
438 } else {
439 pkalg = packet_get_string(&alen);
440 pkblob = packet_get_string(&blen);
441 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100442 pktype = key_type_from_name(pkalg);
443 if (pktype == KEY_UNSPEC) {
Ben Lindstromd121f612000-12-03 17:00:47 +0000444 /* this is perfectly legal */
445 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
Damien Miller874d77b2000-10-14 16:23:11 +1100446 xfree(pkalg);
Ben Lindstromd121f612000-12-03 17:00:47 +0000447 xfree(pkblob);
Damien Millereba71ba2000-04-29 23:57:08 +1000448 return 0;
449 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100450 key = key_from_blob(pkblob, blen);
Damien Millereba71ba2000-04-29 23:57:08 +1000451 if (key != NULL) {
452 if (have_sig) {
453 sig = packet_get_string(&slen);
454 packet_done();
455 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100456 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000457 buffer_append(&b, session_id2, session_id2_len);
Damien Miller50a41ed2000-10-16 12:14:42 +1100458 } else {
459 buffer_put_string(&b, session_id2, session_id2_len);
Damien Miller6536c7d2000-06-22 21:32:31 +1000460 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000461 /* reconstruct packet */
Damien Millereba71ba2000-04-29 23:57:08 +1000462 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller874d77b2000-10-14 16:23:11 +1100463 buffer_put_cstring(&b, authctxt->user);
Damien Millerf6d9e222000-06-18 14:50:44 +1000464 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000465 datafellows & SSH_BUG_PKSERVICE ?
Damien Millerf6d9e222000-06-18 14:50:44 +1000466 "ssh-userauth" :
Damien Miller874d77b2000-10-14 16:23:11 +1100467 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000468 if (datafellows & SSH_BUG_PKAUTH) {
469 buffer_put_char(&b, have_sig);
470 } else {
471 buffer_put_cstring(&b, "publickey");
472 buffer_put_char(&b, have_sig);
Ben Lindstrom3f364962001-04-19 20:50:07 +0000473 buffer_put_cstring(&b, pkalg);
Ben Lindstromd121f612000-12-03 17:00:47 +0000474 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000475 buffer_put_string(&b, pkblob, blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100476#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000477 buffer_dump(&b);
478#endif
479 /* test for correct signature */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100480 if (user_key_allowed(authctxt->pw, key) &&
481 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000482 authenticated = 1;
483 buffer_clear(&b);
484 xfree(sig);
485 } else {
Damien Miller874d77b2000-10-14 16:23:11 +1100486 debug("test whether pkalg/pkblob are acceptable");
Damien Millereba71ba2000-04-29 23:57:08 +1000487 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100488
Damien Millereba71ba2000-04-29 23:57:08 +1000489 /* XXX fake reply and always send PK_OK ? */
Damien Millere247cc42000-05-07 12:03:14 +1000490 /*
491 * XXX this allows testing whether a user is allowed
492 * to login: if you happen to have a valid pubkey this
493 * message is sent. the message is NEVER sent at all
494 * if a user is not allowed to login. is this an
495 * issue? -markus
496 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100497 if (user_key_allowed(authctxt->pw, key)) {
Damien Millereba71ba2000-04-29 23:57:08 +1000498 packet_start(SSH2_MSG_USERAUTH_PK_OK);
499 packet_put_string(pkalg, alen);
500 packet_put_string(pkblob, blen);
501 packet_send();
502 packet_write_wait();
Kevin Steves4abe4de2001-02-08 19:16:32 +0000503 authctxt->postponed = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000504 }
505 }
Damien Miller874d77b2000-10-14 16:23:11 +1100506 if (authenticated != 1)
507 auth_clear_options();
Damien Millereba71ba2000-04-29 23:57:08 +1000508 key_free(key);
509 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100510 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
Damien Millereba71ba2000-04-29 23:57:08 +1000511 xfree(pkalg);
512 xfree(pkblob);
Damien Miller874d77b2000-10-14 16:23:11 +1100513#ifdef HAVE_CYGWIN
514 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
515 return(0);
516#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000517 return authenticated;
518}
519
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000520int
521userauth_hostbased(Authctxt *authctxt)
522{
523 Buffer b;
524 Key *key;
Ben Lindstrom671388f2001-04-19 20:40:45 +0000525 char *pkalg, *pkblob, *sig, *cuser, *chost, *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000526 u_int alen, blen, slen;
527 int pktype;
528 int authenticated = 0;
529
530 if (!authctxt->valid) {
531 debug2("userauth_hostbased: disabled because of invalid user");
532 return 0;
533 }
534 pkalg = packet_get_string(&alen);
535 pkblob = packet_get_string(&blen);
536 chost = packet_get_string(NULL);
537 cuser = packet_get_string(NULL);
538 sig = packet_get_string(&slen);
539
540 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
541 cuser, chost, pkalg, slen);
542#ifdef DEBUG_PK
543 debug("signature:");
544 buffer_init(&b);
545 buffer_append(&b, sig, slen);
546 buffer_dump(&b);
547 buffer_free(&b);
548#endif
549 pktype = key_type_from_name(pkalg);
550 if (pktype == KEY_UNSPEC) {
551 /* this is perfectly legal */
552 log("userauth_hostbased: unsupported "
553 "public key algorithm: %s", pkalg);
554 goto done;
555 }
556 key = key_from_blob(pkblob, blen);
557 if (key == NULL) {
558 debug("userauth_hostbased: cannot decode key: %s", pkalg);
559 goto done;
560 }
Ben Lindstrom671388f2001-04-19 20:40:45 +0000561 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
562 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000563 buffer_init(&b);
Ben Lindstrom671388f2001-04-19 20:40:45 +0000564 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000565 /* reconstruct packet */
566 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
567 buffer_put_cstring(&b, authctxt->user);
Ben Lindstrom671388f2001-04-19 20:40:45 +0000568 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000569 buffer_put_cstring(&b, "hostbased");
570 buffer_put_string(&b, pkalg, alen);
571 buffer_put_string(&b, pkblob, blen);
572 buffer_put_cstring(&b, chost);
573 buffer_put_cstring(&b, cuser);
574#ifdef DEBUG_PK
575 buffer_dump(&b);
576#endif
577 /* test for allowed key and correct signature */
578 if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
579 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
580 authenticated = 1;
581
582 buffer_clear(&b);
583 key_free(key);
584
585done:
586 debug2("userauth_hostbased: authenticated %d", authenticated);
587 xfree(pkalg);
588 xfree(pkblob);
589 xfree(cuser);
590 xfree(chost);
591 xfree(sig);
592 return authenticated;
593}
594
Damien Miller874d77b2000-10-14 16:23:11 +1100595/* get current user */
Damien Millereba71ba2000-04-29 23:57:08 +1000596
597struct passwd*
598auth_get_user(void)
599{
Damien Miller874d77b2000-10-14 16:23:11 +1100600 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000601}
602
Damien Miller874d77b2000-10-14 16:23:11 +1100603#define DELIM ","
Damien Millereba71ba2000-04-29 23:57:08 +1000604
Damien Miller874d77b2000-10-14 16:23:11 +1100605char *
606authmethods_get(void)
607{
608 Authmethod *method = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000609 u_int size = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100610 char *list;
611
612 for (method = authmethods; method->name != NULL; method++) {
613 if (strcmp(method->name, "none") == 0)
614 continue;
615 if (method->enabled != NULL && *(method->enabled) != 0) {
616 if (size != 0)
617 size += strlen(DELIM);
618 size += strlen(method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000619 }
620 }
Damien Miller874d77b2000-10-14 16:23:11 +1100621 size++; /* trailing '\0' */
622 list = xmalloc(size);
623 list[0] = '\0';
624
625 for (method = authmethods; method->name != NULL; method++) {
626 if (strcmp(method->name, "none") == 0)
627 continue;
628 if (method->enabled != NULL && *(method->enabled) != 0) {
629 if (list[0] != '\0')
630 strlcat(list, DELIM, size);
631 strlcat(list, method->name, size);
632 }
633 }
634 return list;
635}
636
637Authmethod *
638authmethod_lookup(const char *name)
639{
640 Authmethod *method = NULL;
641 if (name != NULL)
642 for (method = authmethods; method->name != NULL; method++)
643 if (method->enabled != NULL &&
644 *(method->enabled) != 0 &&
645 strcmp(name, method->name) == 0)
646 return method;
647 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
648 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000649}
650
651/* return 1 if user allows given key */
652int
Damien Miller0bc1bd82000-11-13 22:57:25 +1100653user_key_allowed(struct passwd *pw, Key *key)
Damien Millereba71ba2000-04-29 23:57:08 +1000654{
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000655 char line[8192], *file;
Damien Millereba71ba2000-04-29 23:57:08 +1000656 int found_key = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000657 FILE *f;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000658 u_long linenum = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000659 struct stat st;
660 Key *found;
661
Damien Miller874d77b2000-10-14 16:23:11 +1100662 if (pw == NULL)
663 return 0;
664
Damien Millereba71ba2000-04-29 23:57:08 +1000665 /* Temporarily use the user's uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000666 temporarily_use_uid(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000667
668 /* The authorized keys. */
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000669 file = authorized_keys_file2(pw);
670 debug("trying public key file %s", file);
Damien Millereba71ba2000-04-29 23:57:08 +1000671
672 /* Fail quietly if file does not exist */
673 if (stat(file, &st) < 0) {
674 /* Restore the privileged uid. */
675 restore_uid();
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000676 xfree(file);
Damien Millereba71ba2000-04-29 23:57:08 +1000677 return 0;
678 }
679 /* Open the file containing the authorized keys. */
680 f = fopen(file, "r");
681 if (!f) {
682 /* Restore the privileged uid. */
683 restore_uid();
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000684 xfree(file);
Damien Millereba71ba2000-04-29 23:57:08 +1000685 return 0;
686 }
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000687 if (options.strict_modes &&
688 secure_filename(f, file, pw->pw_uid, line, sizeof(line)) != 0) {
689 xfree(file);
690 fclose(f);
691 log("Authentication refused: %s", line);
692 restore_uid();
693 return 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000694 }
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000695
Damien Millereba71ba2000-04-29 23:57:08 +1000696 found_key = 0;
Damien Millerd3444942000-09-30 14:20:03 +1100697 found = key_new(key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000698
699 while (fgets(line, sizeof(line), f)) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000700 char *cp, *options = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000701 linenum++;
702 /* Skip leading whitespace, empty and comment lines. */
703 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
704 ;
705 if (!*cp || *cp == '\n' || *cp == '#')
706 continue;
Damien Millerf6d9e222000-06-18 14:50:44 +1000707
Damien Miller0bc1bd82000-11-13 22:57:25 +1100708 if (key_read(found, &cp) == -1) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000709 /* no key? check if there are options for this key */
710 int quoted = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100711 debug2("user_key_allowed: check options: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000712 options = cp;
713 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
714 if (*cp == '\\' && cp[1] == '"')
715 cp++; /* Skip both */
716 else if (*cp == '"')
717 quoted = !quoted;
718 }
719 /* Skip remaining whitespace. */
720 for (; *cp == ' ' || *cp == '\t'; cp++)
721 ;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100722 if (key_read(found, &cp) == -1) {
723 debug2("user_key_allowed: advance: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000724 /* still no key? advance to next line*/
725 continue;
726 }
727 }
728 if (key_equal(found, key) &&
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000729 auth_parse_options(pw, options, file, linenum) == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000730 found_key = 1;
731 debug("matching key found: file %s, line %ld",
732 file, linenum);
733 break;
734 }
735 }
736 restore_uid();
737 fclose(f);
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000738 xfree(file);
Damien Millereba71ba2000-04-29 23:57:08 +1000739 key_free(found);
Ben Lindstromb54873a2001-03-11 20:01:55 +0000740 if (!found_key)
741 debug2("key not found");
Damien Millereba71ba2000-04-29 23:57:08 +1000742 return found_key;
743}
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000744
745/* return 1 if given hostkey is allowed */
746int
Ben Lindstrom4aa603c2001-04-19 20:38:06 +0000747hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000748 Key *key)
749{
750 Key *found;
751 const char *resolvedname, *ipaddr, *lookup;
752 struct stat st;
753 char *user_hostfile;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000754 int host_status, len;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000755
756 resolvedname = get_canonical_hostname(options.reverse_mapping_check);
757 ipaddr = get_remote_ipaddr();
758
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000759 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
760 chost, resolvedname, ipaddr);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000761
762 if (options.hostbased_uses_name_from_packet_only) {
763 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
764 return 0;
765 lookup = chost;
766 } else {
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000767 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
768 debug2("stripping trailing dot from chost %s", chost);
769 chost[len - 1] = '\0';
770 }
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000771 if (strcasecmp(resolvedname, chost) != 0)
772 log("userauth_hostbased mismatch: "
773 "client sends %s, but we resolve %s to %s",
774 chost, ipaddr, resolvedname);
775 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
776 return 0;
777 lookup = resolvedname;
778 }
779 debug2("userauth_hostbased: access allowed by auth_rhosts2");
780
781 /* XXX this is copied from auth-rh-rsa.c and should be shared */
782 found = key_new(key->type);
783 host_status = check_host_in_hostfile(_PATH_SSH_SYSTEM_HOSTFILE2, lookup,
784 key, found, NULL);
785
786 if (host_status != HOST_OK && !options.ignore_user_known_hosts) {
787 user_hostfile = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE2,
788 pw->pw_uid);
789 if (options.strict_modes &&
790 (stat(user_hostfile, &st) == 0) &&
791 ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
792 (st.st_mode & 022) != 0)) {
793 log("Hostbased authentication refused for %.100s: "
794 "bad owner or modes for %.200s",
795 pw->pw_name, user_hostfile);
796 } else {
797 temporarily_use_uid(pw);
798 host_status = check_host_in_hostfile(user_hostfile,
799 lookup, key, found, NULL);
800 restore_uid();
801 }
802 xfree(user_hostfile);
803 }
804 key_free(found);
805
806 debug2("userauth_hostbased: key %s for %s", host_status == HOST_OK ?
807 "ok" : "not found", lookup);
808 return (host_status == HOST_OK);
809}