blob: 0b4df9cdaf50be651388c6b1c5329cd8fe60871c [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 Lindstromc7637672001-06-09 00:36:26 +000026RCSID("$OpenBSD: auth2.c,v 1.61 2001/05/31 10:30:12 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 Lindstromdb65e8f2001-01-19 04:26:52 +0000222 authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */
Damien Miller874d77b2000-10-14 16:23:11 +1100223 } else if (authctxt->valid) {
224 if (strcmp(user, authctxt->user) != 0 ||
225 strcmp(service, authctxt->service) != 0) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000226 log("input_userauth_request: mismatch: (%s,%s)!=(%s,%s)",
Damien Miller874d77b2000-10-14 16:23:11 +1100227 user, service, authctxt->user, authctxt->service);
228 authctxt->valid = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000229 }
230 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000231 /* reset state */
232 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
233 authctxt->postponed = 0;
Damien Miller60396b02001-02-18 17:01:00 +1100234#ifdef BSD_AUTH
235 if (authctxt->as) {
236 auth_close(authctxt->as);
237 authctxt->as = NULL;
238 }
239#endif
Damien Millerb70b61f2000-09-16 16:25:12 +1100240
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000241 /* try to authenticate user */
Damien Miller874d77b2000-10-14 16:23:11 +1100242 m = authmethod_lookup(method);
243 if (m != NULL) {
244 debug2("input_userauth_request: try method %s", method);
245 authenticated = m->userauth(authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100246 }
Damien Miller5d57e502001-03-30 10:48:31 +1000247 userauth_finish(authctxt, authenticated, method);
248
249 xfree(service);
250 xfree(user);
251 xfree(method);
252}
253
254void
255userauth_finish(Authctxt *authctxt, int authenticated, char *method)
256{
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000257 char *methods;
258
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000259 if (!authctxt->valid && authenticated)
260 fatal("INTERNAL ERROR: authenticated invalid user %s",
261 authctxt->user);
Damien Millerb70b61f2000-09-16 16:25:12 +1100262
Damien Miller874d77b2000-10-14 16:23:11 +1100263 /* Special handling for root */
Ben Lindstromd8a90212001-02-15 03:08:27 +0000264 if (authenticated && authctxt->pw->pw_uid == 0 &&
265 !auth_root_allowed(method))
Damien Millereba71ba2000-04-29 23:57:08 +1000266 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000267
268#ifdef USE_PAM
Damien Millerc83de6d2001-02-16 14:17:59 +1100269 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
270 NULL))
Damien Millerb70b61f2000-09-16 16:25:12 +1100271 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000272#endif /* USE_PAM */
273
Damien Miller874d77b2000-10-14 16:23:11 +1100274 /* Log before sending the reply */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000275 auth_log(authctxt, authenticated, method, " ssh2");
276
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000277 if (authctxt->postponed)
278 return;
279
280 /* XXX todo: check if multiple auth methods are needed */
281 if (authenticated == 1) {
282 /* turn off userauth */
283 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
284 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
285 packet_send();
286 packet_write_wait();
287 /* now we can break out */
288 authctxt->success = 1;
289 } else {
290 if (authctxt->failures++ > AUTH_FAIL_MAX)
291 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
292 methods = authmethods_get();
293 packet_start(SSH2_MSG_USERAUTH_FAILURE);
294 packet_put_cstring(methods);
295 packet_put_char(0); /* XXX partial success, unused */
296 packet_send();
297 packet_write_wait();
298 xfree(methods);
299 }
Damien Miller874d77b2000-10-14 16:23:11 +1100300}
301
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000302void
303userauth_banner(void)
304{
305 struct stat st;
306 char *banner = NULL;
307 off_t len, n;
308 int fd;
309
310 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
311 return;
Ben Lindstrom0cae0402001-04-04 23:47:52 +0000312 if ((fd = open(options.banner, O_RDONLY)) < 0)
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000313 return;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000314 if (fstat(fd, &st) < 0)
315 goto done;
316 len = st.st_size;
317 banner = xmalloc(len + 1);
318 if ((n = read(fd, banner, len)) < 0)
319 goto done;
320 banner[n] = '\0';
321 packet_start(SSH2_MSG_USERAUTH_BANNER);
322 packet_put_cstring(banner);
323 packet_put_cstring(""); /* language, unused */
324 packet_send();
325 debug("userauth_banner: sent");
326done:
327 if (banner)
328 xfree(banner);
329 close(fd);
330 return;
331}
Damien Miller874d77b2000-10-14 16:23:11 +1100332
Damien Millereba71ba2000-04-29 23:57:08 +1000333int
Damien Miller874d77b2000-10-14 16:23:11 +1100334userauth_none(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000335{
Damien Miller874d77b2000-10-14 16:23:11 +1100336 /* disable method "none", only allowed one time */
337 Authmethod *m = authmethod_lookup("none");
338 if (m != NULL)
339 m->enabled = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000340 packet_done();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000341 userauth_banner();
Damien Millerb8c656e2000-06-28 15:22:41 +1000342
Damien Miller874d77b2000-10-14 16:23:11 +1100343 if (authctxt->valid == 0)
344 return(0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000345
Damien Miller874d77b2000-10-14 16:23:11 +1100346#ifdef HAVE_CYGWIN
347 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
348 return(0);
349#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000350#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100351 return auth_pam_password(authctxt->pw, "");
Damien Millerb8c656e2000-06-28 15:22:41 +1000352#elif defined(HAVE_OSF_SIA)
Damien Miller92ddb7d2001-02-14 01:25:23 +1100353 return 0;
Damien Millerb8c656e2000-06-28 15:22:41 +1000354#else /* !HAVE_OSF_SIA && !USE_PAM */
Damien Miller60396b02001-02-18 17:01:00 +1100355 return auth_password(authctxt, "");
Damien Millereba71ba2000-04-29 23:57:08 +1000356#endif /* USE_PAM */
357}
Damien Miller874d77b2000-10-14 16:23:11 +1100358
Damien Millereba71ba2000-04-29 23:57:08 +1000359int
Damien Miller874d77b2000-10-14 16:23:11 +1100360userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000361{
362 char *password;
363 int authenticated = 0;
364 int change;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000365 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000366 change = packet_get_char();
367 if (change)
368 log("password change not supported");
369 password = packet_get_string(&len);
370 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100371 if (authctxt->valid &&
372#ifdef HAVE_CYGWIN
373 check_nt_auth(1, authctxt->pw->pw_uid) &&
374#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000375#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100376 auth_pam_password(authctxt->pw, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000377#elif defined(HAVE_OSF_SIA)
Damien Miller92ddb7d2001-02-14 01:25:23 +1100378 auth_sia_password(authctxt->user, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000379#else /* !USE_PAM && !HAVE_OSF_SIA */
Damien Miller60396b02001-02-18 17:01:00 +1100380 auth_password(authctxt, password) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000381#endif /* USE_PAM */
382 authenticated = 1;
383 memset(password, 0, len);
384 xfree(password);
385 return authenticated;
386}
Damien Miller874d77b2000-10-14 16:23:11 +1100387
Damien Millereba71ba2000-04-29 23:57:08 +1000388int
Damien Miller874d77b2000-10-14 16:23:11 +1100389userauth_kbdint(Authctxt *authctxt)
390{
391 int authenticated = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000392 char *lang, *devs;
Damien Miller874d77b2000-10-14 16:23:11 +1100393
394 lang = packet_get_string(NULL);
395 devs = packet_get_string(NULL);
396 packet_done();
397
Ben Lindstrom551ea372001-06-05 18:56:16 +0000398 debug("keyboard-interactive devs %s", devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000399
Ben Lindstrom551ea372001-06-05 18:56:16 +0000400 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000401 authenticated = auth2_challenge(authctxt, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000402
Damien Millerb8481582000-12-03 11:51:51 +1100403#ifdef USE_PAM
Damien Millerf8154422001-04-25 22:44:14 +1000404 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
Damien Millerb8481582000-12-03 11:51:51 +1100405 authenticated = auth2_pam(authctxt);
406#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100407 xfree(devs);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000408 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +1100409#ifdef HAVE_CYGWIN
410 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
411 return(0);
412#endif
413 return authenticated;
414}
415
416int
417userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000418{
419 Buffer b;
420 Key *key;
421 char *pkalg, *pkblob, *sig;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000422 u_int alen, blen, slen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100423 int have_sig, pktype;
Damien Millereba71ba2000-04-29 23:57:08 +1000424 int authenticated = 0;
425
Damien Miller874d77b2000-10-14 16:23:11 +1100426 if (!authctxt->valid) {
427 debug2("userauth_pubkey: disabled because of invalid user");
Damien Millereba71ba2000-04-29 23:57:08 +1000428 return 0;
429 }
430 have_sig = packet_get_char();
Ben Lindstromd121f612000-12-03 17:00:47 +0000431 if (datafellows & SSH_BUG_PKAUTH) {
432 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
433 /* no explicit pkalg given */
434 pkblob = packet_get_string(&blen);
435 buffer_init(&b);
436 buffer_append(&b, pkblob, blen);
437 /* so we have to extract the pkalg from the pkblob */
438 pkalg = buffer_get_string(&b, &alen);
439 buffer_free(&b);
440 } else {
441 pkalg = packet_get_string(&alen);
442 pkblob = packet_get_string(&blen);
443 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100444 pktype = key_type_from_name(pkalg);
445 if (pktype == KEY_UNSPEC) {
Ben Lindstromd121f612000-12-03 17:00:47 +0000446 /* this is perfectly legal */
447 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
Damien Miller874d77b2000-10-14 16:23:11 +1100448 xfree(pkalg);
Ben Lindstromd121f612000-12-03 17:00:47 +0000449 xfree(pkblob);
Damien Millereba71ba2000-04-29 23:57:08 +1000450 return 0;
451 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100452 key = key_from_blob(pkblob, blen);
Damien Millereba71ba2000-04-29 23:57:08 +1000453 if (key != NULL) {
454 if (have_sig) {
455 sig = packet_get_string(&slen);
456 packet_done();
457 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100458 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000459 buffer_append(&b, session_id2, session_id2_len);
Damien Miller50a41ed2000-10-16 12:14:42 +1100460 } else {
461 buffer_put_string(&b, session_id2, session_id2_len);
Damien Miller6536c7d2000-06-22 21:32:31 +1000462 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000463 /* reconstruct packet */
Damien Millereba71ba2000-04-29 23:57:08 +1000464 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller874d77b2000-10-14 16:23:11 +1100465 buffer_put_cstring(&b, authctxt->user);
Damien Millerf6d9e222000-06-18 14:50:44 +1000466 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000467 datafellows & SSH_BUG_PKSERVICE ?
Damien Millerf6d9e222000-06-18 14:50:44 +1000468 "ssh-userauth" :
Damien Miller874d77b2000-10-14 16:23:11 +1100469 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000470 if (datafellows & SSH_BUG_PKAUTH) {
471 buffer_put_char(&b, have_sig);
472 } else {
473 buffer_put_cstring(&b, "publickey");
474 buffer_put_char(&b, have_sig);
Ben Lindstrom3f364962001-04-19 20:50:07 +0000475 buffer_put_cstring(&b, pkalg);
Ben Lindstromd121f612000-12-03 17:00:47 +0000476 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000477 buffer_put_string(&b, pkblob, blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100478#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000479 buffer_dump(&b);
480#endif
481 /* test for correct signature */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100482 if (user_key_allowed(authctxt->pw, key) &&
483 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000484 authenticated = 1;
485 buffer_clear(&b);
486 xfree(sig);
487 } else {
Damien Miller874d77b2000-10-14 16:23:11 +1100488 debug("test whether pkalg/pkblob are acceptable");
Damien Millereba71ba2000-04-29 23:57:08 +1000489 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100490
Damien Millereba71ba2000-04-29 23:57:08 +1000491 /* XXX fake reply and always send PK_OK ? */
Damien Millere247cc42000-05-07 12:03:14 +1000492 /*
493 * XXX this allows testing whether a user is allowed
494 * to login: if you happen to have a valid pubkey this
495 * message is sent. the message is NEVER sent at all
496 * if a user is not allowed to login. is this an
497 * issue? -markus
498 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100499 if (user_key_allowed(authctxt->pw, key)) {
Damien Millereba71ba2000-04-29 23:57:08 +1000500 packet_start(SSH2_MSG_USERAUTH_PK_OK);
501 packet_put_string(pkalg, alen);
502 packet_put_string(pkblob, blen);
503 packet_send();
504 packet_write_wait();
Kevin Steves4abe4de2001-02-08 19:16:32 +0000505 authctxt->postponed = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000506 }
507 }
Damien Miller874d77b2000-10-14 16:23:11 +1100508 if (authenticated != 1)
509 auth_clear_options();
Damien Millereba71ba2000-04-29 23:57:08 +1000510 key_free(key);
511 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100512 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
Damien Millereba71ba2000-04-29 23:57:08 +1000513 xfree(pkalg);
514 xfree(pkblob);
Damien Miller874d77b2000-10-14 16:23:11 +1100515#ifdef HAVE_CYGWIN
516 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
517 return(0);
518#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000519 return authenticated;
520}
521
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000522int
523userauth_hostbased(Authctxt *authctxt)
524{
525 Buffer b;
526 Key *key;
Ben Lindstrom671388f2001-04-19 20:40:45 +0000527 char *pkalg, *pkblob, *sig, *cuser, *chost, *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000528 u_int alen, blen, slen;
529 int pktype;
530 int authenticated = 0;
531
532 if (!authctxt->valid) {
533 debug2("userauth_hostbased: disabled because of invalid user");
534 return 0;
535 }
536 pkalg = packet_get_string(&alen);
537 pkblob = packet_get_string(&blen);
538 chost = packet_get_string(NULL);
539 cuser = packet_get_string(NULL);
540 sig = packet_get_string(&slen);
541
542 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
543 cuser, chost, pkalg, slen);
544#ifdef DEBUG_PK
545 debug("signature:");
546 buffer_init(&b);
547 buffer_append(&b, sig, slen);
548 buffer_dump(&b);
549 buffer_free(&b);
550#endif
551 pktype = key_type_from_name(pkalg);
552 if (pktype == KEY_UNSPEC) {
553 /* this is perfectly legal */
554 log("userauth_hostbased: unsupported "
555 "public key algorithm: %s", pkalg);
556 goto done;
557 }
558 key = key_from_blob(pkblob, blen);
559 if (key == NULL) {
560 debug("userauth_hostbased: cannot decode key: %s", pkalg);
561 goto done;
562 }
Ben Lindstrom671388f2001-04-19 20:40:45 +0000563 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
564 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000565 buffer_init(&b);
Ben Lindstrom671388f2001-04-19 20:40:45 +0000566 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000567 /* reconstruct packet */
568 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
569 buffer_put_cstring(&b, authctxt->user);
Ben Lindstrom671388f2001-04-19 20:40:45 +0000570 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000571 buffer_put_cstring(&b, "hostbased");
572 buffer_put_string(&b, pkalg, alen);
573 buffer_put_string(&b, pkblob, blen);
574 buffer_put_cstring(&b, chost);
575 buffer_put_cstring(&b, cuser);
576#ifdef DEBUG_PK
577 buffer_dump(&b);
578#endif
579 /* test for allowed key and correct signature */
580 if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
581 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
582 authenticated = 1;
583
584 buffer_clear(&b);
585 key_free(key);
586
587done:
588 debug2("userauth_hostbased: authenticated %d", authenticated);
589 xfree(pkalg);
590 xfree(pkblob);
591 xfree(cuser);
592 xfree(chost);
593 xfree(sig);
594 return authenticated;
595}
596
Damien Miller874d77b2000-10-14 16:23:11 +1100597/* get current user */
Damien Millereba71ba2000-04-29 23:57:08 +1000598
599struct passwd*
600auth_get_user(void)
601{
Damien Miller874d77b2000-10-14 16:23:11 +1100602 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000603}
604
Damien Miller874d77b2000-10-14 16:23:11 +1100605#define DELIM ","
Damien Millereba71ba2000-04-29 23:57:08 +1000606
Damien Miller874d77b2000-10-14 16:23:11 +1100607char *
608authmethods_get(void)
609{
610 Authmethod *method = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000611 u_int size = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100612 char *list;
613
614 for (method = authmethods; method->name != NULL; method++) {
615 if (strcmp(method->name, "none") == 0)
616 continue;
617 if (method->enabled != NULL && *(method->enabled) != 0) {
618 if (size != 0)
619 size += strlen(DELIM);
620 size += strlen(method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000621 }
622 }
Damien Miller874d77b2000-10-14 16:23:11 +1100623 size++; /* trailing '\0' */
624 list = xmalloc(size);
625 list[0] = '\0';
626
627 for (method = authmethods; method->name != NULL; method++) {
628 if (strcmp(method->name, "none") == 0)
629 continue;
630 if (method->enabled != NULL && *(method->enabled) != 0) {
631 if (list[0] != '\0')
632 strlcat(list, DELIM, size);
633 strlcat(list, method->name, size);
634 }
635 }
636 return list;
637}
638
639Authmethod *
640authmethod_lookup(const char *name)
641{
642 Authmethod *method = NULL;
643 if (name != NULL)
644 for (method = authmethods; method->name != NULL; method++)
645 if (method->enabled != NULL &&
646 *(method->enabled) != 0 &&
647 strcmp(name, method->name) == 0)
648 return method;
649 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
650 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000651}
652
653/* return 1 if user allows given key */
654int
Damien Miller0bc1bd82000-11-13 22:57:25 +1100655user_key_allowed(struct passwd *pw, Key *key)
Damien Millereba71ba2000-04-29 23:57:08 +1000656{
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000657 char line[8192], *file;
Damien Millereba71ba2000-04-29 23:57:08 +1000658 int found_key = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000659 FILE *f;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000660 u_long linenum = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000661 struct stat st;
662 Key *found;
663
Damien Miller874d77b2000-10-14 16:23:11 +1100664 if (pw == NULL)
665 return 0;
666
Damien Millereba71ba2000-04-29 23:57:08 +1000667 /* Temporarily use the user's uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000668 temporarily_use_uid(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000669
670 /* The authorized keys. */
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000671 file = authorized_keys_file2(pw);
672 debug("trying public key file %s", file);
Damien Millereba71ba2000-04-29 23:57:08 +1000673
674 /* Fail quietly if file does not exist */
675 if (stat(file, &st) < 0) {
676 /* Restore the privileged uid. */
677 restore_uid();
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000678 xfree(file);
Damien Millereba71ba2000-04-29 23:57:08 +1000679 return 0;
680 }
681 /* Open the file containing the authorized keys. */
682 f = fopen(file, "r");
683 if (!f) {
684 /* Restore the privileged uid. */
685 restore_uid();
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000686 xfree(file);
Damien Millereba71ba2000-04-29 23:57:08 +1000687 return 0;
688 }
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000689 if (options.strict_modes &&
690 secure_filename(f, file, pw->pw_uid, line, sizeof(line)) != 0) {
691 xfree(file);
692 fclose(f);
693 log("Authentication refused: %s", line);
694 restore_uid();
695 return 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000696 }
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000697
Damien Millereba71ba2000-04-29 23:57:08 +1000698 found_key = 0;
Damien Millerd3444942000-09-30 14:20:03 +1100699 found = key_new(key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000700
701 while (fgets(line, sizeof(line), f)) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000702 char *cp, *options = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000703 linenum++;
704 /* Skip leading whitespace, empty and comment lines. */
705 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
706 ;
707 if (!*cp || *cp == '\n' || *cp == '#')
708 continue;
Damien Millerf6d9e222000-06-18 14:50:44 +1000709
Damien Miller0bc1bd82000-11-13 22:57:25 +1100710 if (key_read(found, &cp) == -1) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000711 /* no key? check if there are options for this key */
712 int quoted = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100713 debug2("user_key_allowed: check options: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000714 options = cp;
715 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
716 if (*cp == '\\' && cp[1] == '"')
717 cp++; /* Skip both */
718 else if (*cp == '"')
719 quoted = !quoted;
720 }
721 /* Skip remaining whitespace. */
722 for (; *cp == ' ' || *cp == '\t'; cp++)
723 ;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100724 if (key_read(found, &cp) == -1) {
725 debug2("user_key_allowed: advance: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000726 /* still no key? advance to next line*/
727 continue;
728 }
729 }
730 if (key_equal(found, key) &&
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000731 auth_parse_options(pw, options, file, linenum) == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000732 found_key = 1;
733 debug("matching key found: file %s, line %ld",
734 file, linenum);
735 break;
736 }
737 }
738 restore_uid();
739 fclose(f);
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000740 xfree(file);
Damien Millereba71ba2000-04-29 23:57:08 +1000741 key_free(found);
Ben Lindstromb54873a2001-03-11 20:01:55 +0000742 if (!found_key)
743 debug2("key not found");
Damien Millereba71ba2000-04-29 23:57:08 +1000744 return found_key;
745}
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000746
747/* return 1 if given hostkey is allowed */
748int
Ben Lindstrom4aa603c2001-04-19 20:38:06 +0000749hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000750 Key *key)
751{
752 Key *found;
753 const char *resolvedname, *ipaddr, *lookup;
754 struct stat st;
755 char *user_hostfile;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000756 int host_status, len;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000757
758 resolvedname = get_canonical_hostname(options.reverse_mapping_check);
759 ipaddr = get_remote_ipaddr();
760
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000761 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
762 chost, resolvedname, ipaddr);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000763
764 if (options.hostbased_uses_name_from_packet_only) {
765 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
766 return 0;
767 lookup = chost;
768 } else {
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000769 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
770 debug2("stripping trailing dot from chost %s", chost);
771 chost[len - 1] = '\0';
772 }
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000773 if (strcasecmp(resolvedname, chost) != 0)
774 log("userauth_hostbased mismatch: "
775 "client sends %s, but we resolve %s to %s",
776 chost, ipaddr, resolvedname);
777 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
778 return 0;
779 lookup = resolvedname;
780 }
781 debug2("userauth_hostbased: access allowed by auth_rhosts2");
782
783 /* XXX this is copied from auth-rh-rsa.c and should be shared */
784 found = key_new(key->type);
785 host_status = check_host_in_hostfile(_PATH_SSH_SYSTEM_HOSTFILE2, lookup,
786 key, found, NULL);
787
788 if (host_status != HOST_OK && !options.ignore_user_known_hosts) {
789 user_hostfile = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE2,
790 pw->pw_uid);
791 if (options.strict_modes &&
792 (stat(user_hostfile, &st) == 0) &&
793 ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
794 (st.st_mode & 022) != 0)) {
795 log("Hostbased authentication refused for %.100s: "
796 "bad owner or modes for %.200s",
797 pw->pw_name, user_hostfile);
798 } else {
799 temporarily_use_uid(pw);
800 host_status = check_host_in_hostfile(user_hostfile,
801 lookup, key, found, NULL);
802 restore_uid();
803 }
804 xfree(user_hostfile);
805 }
806 key_free(found);
807
808 debug2("userauth_hostbased: key %s for %s", host_status == HOST_OK ?
809 "ok" : "not found", lookup);
810 return (host_status == HOST_OK);
811}