blob: cd6b27685a47c9d7a5dcd07a07544adfb32d6084 [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 Lindstrom5eabda32001-04-12 23:34:34 +000026RCSID("$OpenBSD: auth2.c,v 1.52 2001/04/12 19:15:24 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"
Damien Millereba71ba2000-04-29 23:57:08 +100054
55/* import */
56extern ServerOptions options;
Ben Lindstrom46c16222000-12-22 01:43:59 +000057extern u_char *session_id2;
Damien Millereba71ba2000-04-29 23:57:08 +100058extern int session_id2_len;
59
Damien Miller874d77b2000-10-14 16:23:11 +110060#ifdef WITH_AIXAUTHENTICATE
61extern char *aixloginmsg;
62#endif
Damien Miller874d77b2000-10-14 16:23:11 +110063
64static Authctxt *x_authctxt = NULL;
65static int one = 1;
66
67typedef struct Authmethod Authmethod;
68struct Authmethod {
69 char *name;
70 int (*userauth)(Authctxt *authctxt);
71 int *enabled;
72};
73
Damien Millereba71ba2000-04-29 23:57:08 +100074/* protocol */
75
Damien Miller62cee002000-09-23 17:15:56 +110076void input_service_request(int type, int plen, void *ctxt);
77void input_userauth_request(int type, int plen, void *ctxt);
78void protocol_error(int type, int plen, void *ctxt);
Damien Millereba71ba2000-04-29 23:57:08 +100079
Damien Millereba71ba2000-04-29 23:57:08 +100080/* helper */
Damien Miller874d77b2000-10-14 16:23:11 +110081Authmethod *authmethod_lookup(const char *name);
Damien Miller874d77b2000-10-14 16:23:11 +110082char *authmethods_get(void);
Ben Lindstrom5eabda32001-04-12 23:34:34 +000083int user_key_allowed(struct passwd *pw, Key *key);
84int
85hostbased_key_allowed(struct passwd *pw, const char *cuser, const char *chost,
86 Key *key);
Damien Millereba71ba2000-04-29 23:57:08 +100087
Damien Miller874d77b2000-10-14 16:23:11 +110088/* auth */
Ben Lindstrom48bd7c12001-01-09 00:35:42 +000089void userauth_banner(void);
Damien Miller5d57e502001-03-30 10:48:31 +100090void userauth_reply(Authctxt *authctxt, int authenticated);
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 */
128 if (options.challenge_reponse_authentication)
129 options.kbd_interactive_authentication = 1;
130
Damien Millereba71ba2000-04-29 23:57:08 +1000131 dispatch_init(&protocol_error);
132 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
Damien Miller874d77b2000-10-14 16:23:11 +1100133 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000134 do_authenticated(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000135}
136
137void
Damien Miller62cee002000-09-23 17:15:56 +1100138protocol_error(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000139{
140 log("auth: protocol error: type %d plen %d", type, plen);
141 packet_start(SSH2_MSG_UNIMPLEMENTED);
142 packet_put_int(0);
143 packet_send();
144 packet_write_wait();
145}
146
147void
Damien Miller62cee002000-09-23 17:15:56 +1100148input_service_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000149{
Damien Miller874d77b2000-10-14 16:23:11 +1100150 Authctxt *authctxt = ctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000151 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000152 int accept = 0;
153 char *service = packet_get_string(&len);
154 packet_done();
155
Damien Miller874d77b2000-10-14 16:23:11 +1100156 if (authctxt == NULL)
157 fatal("input_service_request: no authctxt");
158
Damien Millereba71ba2000-04-29 23:57:08 +1000159 if (strcmp(service, "ssh-userauth") == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100160 if (!authctxt->success) {
Damien Millereba71ba2000-04-29 23:57:08 +1000161 accept = 1;
162 /* now we can handle user-auth requests */
163 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
164 }
165 }
166 /* XXX all other service requests are denied */
167
168 if (accept) {
169 packet_start(SSH2_MSG_SERVICE_ACCEPT);
170 packet_put_cstring(service);
171 packet_send();
172 packet_write_wait();
173 } else {
174 debug("bad service request %s", service);
175 packet_disconnect("bad service request %s", service);
176 }
177 xfree(service);
178}
179
180void
Damien Miller62cee002000-09-23 17:15:56 +1100181input_userauth_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000182{
Damien Miller874d77b2000-10-14 16:23:11 +1100183 Authctxt *authctxt = ctxt;
184 Authmethod *m = NULL;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000185 char *user, *service, *method, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000186 int authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000187
Damien Miller874d77b2000-10-14 16:23:11 +1100188 if (authctxt == NULL)
189 fatal("input_userauth_request: no authctxt");
Damien Millereba71ba2000-04-29 23:57:08 +1000190
Damien Miller874d77b2000-10-14 16:23:11 +1100191 user = packet_get_string(NULL);
192 service = packet_get_string(NULL);
193 method = packet_get_string(NULL);
194 debug("userauth-request for user %s service %s method %s", user, service, method);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000195 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
Damien Miller874d77b2000-10-14 16:23:11 +1100196
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000197 if ((style = strchr(user, ':')) != NULL)
198 *style++ = 0;
199
Kevin Stevesef4eea92001-02-05 12:42:17 +0000200 if (authctxt->attempt++ == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100201 /* setup auth context */
202 struct passwd *pw = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100203 pw = getpwnam(user);
204 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
205 authctxt->pw = pwcopy(pw);
206 authctxt->valid = 1;
207 debug2("input_userauth_request: setting up authctxt for %s", user);
208#ifdef USE_PAM
Damien Miller22e22bf2001-01-19 15:46:38 +1100209 start_pam(pw->pw_name);
Damien Miller874d77b2000-10-14 16:23:11 +1100210#endif
211 } else {
212 log("input_userauth_request: illegal user %s", user);
Damien Miller22e22bf2001-01-19 15:46:38 +1100213#ifdef USE_PAM
214 start_pam("NOUSER");
215#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100216 }
Ben Lindstromc1ba31f2001-02-15 03:14:11 +0000217 setproctitle("%s", pw ? user : "unknown");
Damien Miller874d77b2000-10-14 16:23:11 +1100218 authctxt->user = xstrdup(user);
219 authctxt->service = xstrdup(service);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000220 authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */
Damien Miller874d77b2000-10-14 16:23:11 +1100221 } else if (authctxt->valid) {
222 if (strcmp(user, authctxt->user) != 0 ||
223 strcmp(service, authctxt->service) != 0) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000224 log("input_userauth_request: mismatch: (%s,%s)!=(%s,%s)",
Damien Miller874d77b2000-10-14 16:23:11 +1100225 user, service, authctxt->user, authctxt->service);
226 authctxt->valid = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000227 }
228 }
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 Lindstromdb65e8f2001-01-19 04:26:52 +0000255 if (!authctxt->valid && authenticated)
256 fatal("INTERNAL ERROR: authenticated invalid user %s",
257 authctxt->user);
Damien Millerb70b61f2000-09-16 16:25:12 +1100258
Damien Miller874d77b2000-10-14 16:23:11 +1100259 /* Special handling for root */
Ben Lindstromd8a90212001-02-15 03:08:27 +0000260 if (authenticated && authctxt->pw->pw_uid == 0 &&
261 !auth_root_allowed(method))
Damien Millereba71ba2000-04-29 23:57:08 +1000262 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000263
264#ifdef USE_PAM
Damien Millerc83de6d2001-02-16 14:17:59 +1100265 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
266 NULL))
Damien Millerb70b61f2000-09-16 16:25:12 +1100267 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000268#endif /* USE_PAM */
269
Damien Miller874d77b2000-10-14 16:23:11 +1100270 /* Log before sending the reply */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000271 auth_log(authctxt, authenticated, method, " ssh2");
272
273 if (!authctxt->postponed)
274 userauth_reply(authctxt, authenticated);
Damien Miller874d77b2000-10-14 16:23:11 +1100275}
276
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000277void
278userauth_banner(void)
279{
280 struct stat st;
281 char *banner = NULL;
282 off_t len, n;
283 int fd;
284
285 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
286 return;
Ben Lindstrom0cae0402001-04-04 23:47:52 +0000287 if ((fd = open(options.banner, O_RDONLY)) < 0)
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000288 return;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000289 if (fstat(fd, &st) < 0)
290 goto done;
291 len = st.st_size;
292 banner = xmalloc(len + 1);
293 if ((n = read(fd, banner, len)) < 0)
294 goto done;
295 banner[n] = '\0';
296 packet_start(SSH2_MSG_USERAUTH_BANNER);
297 packet_put_cstring(banner);
298 packet_put_cstring(""); /* language, unused */
299 packet_send();
300 debug("userauth_banner: sent");
301done:
302 if (banner)
303 xfree(banner);
304 close(fd);
305 return;
306}
Damien Miller874d77b2000-10-14 16:23:11 +1100307
Kevin Stevesef4eea92001-02-05 12:42:17 +0000308void
Damien Miller874d77b2000-10-14 16:23:11 +1100309userauth_reply(Authctxt *authctxt, int authenticated)
310{
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000311 char *methods;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000312
Damien Millere247cc42000-05-07 12:03:14 +1000313 /* XXX todo: check if multiple auth methods are needed */
Kevin Steves4abe4de2001-02-08 19:16:32 +0000314 if (authenticated == 1) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000315#ifdef WITH_AIXAUTHENTICATE
316 /* We don't have a pty yet, so just label the line as "ssh" */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000317 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
318 get_canonical_hostname(options.reverse_mapping_check),
Damien Miller33804262001-02-04 23:20:18 +1100319 "ssh", &aixloginmsg) < 0)
Damien Millerd2c208a2000-05-17 22:00:02 +1000320 aixloginmsg = NULL;
321#endif /* WITH_AIXAUTHENTICATE */
Damien Millereba71ba2000-04-29 23:57:08 +1000322 /* turn off userauth */
323 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
324 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
325 packet_send();
326 packet_write_wait();
327 /* now we can break out */
Damien Miller874d77b2000-10-14 16:23:11 +1100328 authctxt->success = 1;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000329 } else {
330 if (authctxt->failures++ > AUTH_FAIL_MAX)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000331 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000332 methods = authmethods_get();
Damien Millereba71ba2000-04-29 23:57:08 +1000333 packet_start(SSH2_MSG_USERAUTH_FAILURE);
Damien Miller874d77b2000-10-14 16:23:11 +1100334 packet_put_cstring(methods);
335 packet_put_char(0); /* XXX partial success, unused */
Damien Millereba71ba2000-04-29 23:57:08 +1000336 packet_send();
337 packet_write_wait();
Damien Miller874d77b2000-10-14 16:23:11 +1100338 xfree(methods);
Damien Millereba71ba2000-04-29 23:57:08 +1000339 }
Damien Millereba71ba2000-04-29 23:57:08 +1000340}
341
342int
Damien Miller874d77b2000-10-14 16:23:11 +1100343userauth_none(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000344{
Damien Miller874d77b2000-10-14 16:23:11 +1100345 /* disable method "none", only allowed one time */
346 Authmethod *m = authmethod_lookup("none");
347 if (m != NULL)
348 m->enabled = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000349 packet_done();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000350 userauth_banner();
Damien Millerb8c656e2000-06-28 15:22:41 +1000351
Damien Miller874d77b2000-10-14 16:23:11 +1100352 if (authctxt->valid == 0)
353 return(0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000354
Damien Miller874d77b2000-10-14 16:23:11 +1100355#ifdef HAVE_CYGWIN
356 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
357 return(0);
358#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000359#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100360 return auth_pam_password(authctxt->pw, "");
Damien Millerb8c656e2000-06-28 15:22:41 +1000361#elif defined(HAVE_OSF_SIA)
Damien Miller92ddb7d2001-02-14 01:25:23 +1100362 return 0;
Damien Millerb8c656e2000-06-28 15:22:41 +1000363#else /* !HAVE_OSF_SIA && !USE_PAM */
Damien Miller60396b02001-02-18 17:01:00 +1100364 return auth_password(authctxt, "");
Damien Millereba71ba2000-04-29 23:57:08 +1000365#endif /* USE_PAM */
366}
Damien Miller874d77b2000-10-14 16:23:11 +1100367
Damien Millereba71ba2000-04-29 23:57:08 +1000368int
Damien Miller874d77b2000-10-14 16:23:11 +1100369userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000370{
371 char *password;
372 int authenticated = 0;
373 int change;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000374 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000375 change = packet_get_char();
376 if (change)
377 log("password change not supported");
378 password = packet_get_string(&len);
379 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100380 if (authctxt->valid &&
381#ifdef HAVE_CYGWIN
382 check_nt_auth(1, authctxt->pw->pw_uid) &&
383#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000384#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100385 auth_pam_password(authctxt->pw, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000386#elif defined(HAVE_OSF_SIA)
Damien Miller92ddb7d2001-02-14 01:25:23 +1100387 auth_sia_password(authctxt->user, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000388#else /* !USE_PAM && !HAVE_OSF_SIA */
Damien Miller60396b02001-02-18 17:01:00 +1100389 auth_password(authctxt, password) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000390#endif /* USE_PAM */
391 authenticated = 1;
392 memset(password, 0, len);
393 xfree(password);
394 return authenticated;
395}
Damien Miller874d77b2000-10-14 16:23:11 +1100396
Damien Millereba71ba2000-04-29 23:57:08 +1000397int
Damien Miller874d77b2000-10-14 16:23:11 +1100398userauth_kbdint(Authctxt *authctxt)
399{
400 int authenticated = 0;
401 char *lang = NULL;
402 char *devs = NULL;
403
404 lang = packet_get_string(NULL);
405 devs = packet_get_string(NULL);
406 packet_done();
407
408 debug("keyboard-interactive language %s devs %s", lang, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000409
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000410 if (options.challenge_reponse_authentication)
411 authenticated = auth2_challenge(authctxt, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000412
Damien Millerb8481582000-12-03 11:51:51 +1100413#ifdef USE_PAM
414 if (authenticated == 0)
415 authenticated = auth2_pam(authctxt);
416#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100417 xfree(lang);
418 xfree(devs);
419#ifdef HAVE_CYGWIN
420 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
421 return(0);
422#endif
423 return authenticated;
424}
425
426int
427userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000428{
429 Buffer b;
430 Key *key;
431 char *pkalg, *pkblob, *sig;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000432 u_int alen, blen, slen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100433 int have_sig, pktype;
Damien Millereba71ba2000-04-29 23:57:08 +1000434 int authenticated = 0;
435
Damien Miller874d77b2000-10-14 16:23:11 +1100436 if (!authctxt->valid) {
437 debug2("userauth_pubkey: disabled because of invalid user");
Damien Millereba71ba2000-04-29 23:57:08 +1000438 return 0;
439 }
440 have_sig = packet_get_char();
Ben Lindstromd121f612000-12-03 17:00:47 +0000441 if (datafellows & SSH_BUG_PKAUTH) {
442 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
443 /* no explicit pkalg given */
444 pkblob = packet_get_string(&blen);
445 buffer_init(&b);
446 buffer_append(&b, pkblob, blen);
447 /* so we have to extract the pkalg from the pkblob */
448 pkalg = buffer_get_string(&b, &alen);
449 buffer_free(&b);
450 } else {
451 pkalg = packet_get_string(&alen);
452 pkblob = packet_get_string(&blen);
453 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100454 pktype = key_type_from_name(pkalg);
455 if (pktype == KEY_UNSPEC) {
Ben Lindstromd121f612000-12-03 17:00:47 +0000456 /* this is perfectly legal */
457 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
Damien Miller874d77b2000-10-14 16:23:11 +1100458 xfree(pkalg);
Ben Lindstromd121f612000-12-03 17:00:47 +0000459 xfree(pkblob);
Damien Millereba71ba2000-04-29 23:57:08 +1000460 return 0;
461 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100462 key = key_from_blob(pkblob, blen);
Damien Millereba71ba2000-04-29 23:57:08 +1000463 if (key != NULL) {
464 if (have_sig) {
465 sig = packet_get_string(&slen);
466 packet_done();
467 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100468 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000469 buffer_append(&b, session_id2, session_id2_len);
Damien Miller50a41ed2000-10-16 12:14:42 +1100470 } else {
471 buffer_put_string(&b, session_id2, session_id2_len);
Damien Miller6536c7d2000-06-22 21:32:31 +1000472 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000473 /* reconstruct packet */
Damien Millereba71ba2000-04-29 23:57:08 +1000474 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller874d77b2000-10-14 16:23:11 +1100475 buffer_put_cstring(&b, authctxt->user);
Damien Millerf6d9e222000-06-18 14:50:44 +1000476 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000477 datafellows & SSH_BUG_PKSERVICE ?
Damien Millerf6d9e222000-06-18 14:50:44 +1000478 "ssh-userauth" :
Damien Miller874d77b2000-10-14 16:23:11 +1100479 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000480 if (datafellows & SSH_BUG_PKAUTH) {
481 buffer_put_char(&b, have_sig);
482 } else {
483 buffer_put_cstring(&b, "publickey");
484 buffer_put_char(&b, have_sig);
485 buffer_put_cstring(&b, key_ssh_name(key));
486 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000487 buffer_put_string(&b, pkblob, blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100488#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000489 buffer_dump(&b);
490#endif
491 /* test for correct signature */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100492 if (user_key_allowed(authctxt->pw, key) &&
493 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000494 authenticated = 1;
495 buffer_clear(&b);
496 xfree(sig);
497 } else {
Damien Miller874d77b2000-10-14 16:23:11 +1100498 debug("test whether pkalg/pkblob are acceptable");
Damien Millereba71ba2000-04-29 23:57:08 +1000499 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100500
Damien Millereba71ba2000-04-29 23:57:08 +1000501 /* XXX fake reply and always send PK_OK ? */
Damien Millere247cc42000-05-07 12:03:14 +1000502 /*
503 * XXX this allows testing whether a user is allowed
504 * to login: if you happen to have a valid pubkey this
505 * message is sent. the message is NEVER sent at all
506 * if a user is not allowed to login. is this an
507 * issue? -markus
508 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100509 if (user_key_allowed(authctxt->pw, key)) {
Damien Millereba71ba2000-04-29 23:57:08 +1000510 packet_start(SSH2_MSG_USERAUTH_PK_OK);
511 packet_put_string(pkalg, alen);
512 packet_put_string(pkblob, blen);
513 packet_send();
514 packet_write_wait();
Kevin Steves4abe4de2001-02-08 19:16:32 +0000515 authctxt->postponed = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000516 }
517 }
Damien Miller874d77b2000-10-14 16:23:11 +1100518 if (authenticated != 1)
519 auth_clear_options();
Damien Millereba71ba2000-04-29 23:57:08 +1000520 key_free(key);
521 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100522 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
Damien Millereba71ba2000-04-29 23:57:08 +1000523 xfree(pkalg);
524 xfree(pkblob);
Damien Miller874d77b2000-10-14 16:23:11 +1100525#ifdef HAVE_CYGWIN
526 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
527 return(0);
528#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000529 return authenticated;
530}
531
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000532int
533userauth_hostbased(Authctxt *authctxt)
534{
535 Buffer b;
536 Key *key;
537 char *pkalg, *pkblob, *sig;
538 char *cuser, *chost;
539 u_int alen, blen, slen;
540 int pktype;
541 int authenticated = 0;
542
543 if (!authctxt->valid) {
544 debug2("userauth_hostbased: disabled because of invalid user");
545 return 0;
546 }
547 pkalg = packet_get_string(&alen);
548 pkblob = packet_get_string(&blen);
549 chost = packet_get_string(NULL);
550 cuser = packet_get_string(NULL);
551 sig = packet_get_string(&slen);
552
553 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
554 cuser, chost, pkalg, slen);
555#ifdef DEBUG_PK
556 debug("signature:");
557 buffer_init(&b);
558 buffer_append(&b, sig, slen);
559 buffer_dump(&b);
560 buffer_free(&b);
561#endif
562 pktype = key_type_from_name(pkalg);
563 if (pktype == KEY_UNSPEC) {
564 /* this is perfectly legal */
565 log("userauth_hostbased: unsupported "
566 "public key algorithm: %s", pkalg);
567 goto done;
568 }
569 key = key_from_blob(pkblob, blen);
570 if (key == NULL) {
571 debug("userauth_hostbased: cannot decode key: %s", pkalg);
572 goto done;
573 }
574 buffer_init(&b);
575 if (datafellows & SSH_OLD_SESSIONID) {
576 buffer_append(&b, session_id2, session_id2_len);
577 } else {
578 buffer_put_string(&b, session_id2, session_id2_len);
579 }
580 if (datafellows & SSH_BUG_HBSERVICE)
581 debug("SSH_BUG_HBSERVICE");
582 /* reconstruct packet */
583 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
584 buffer_put_cstring(&b, authctxt->user);
585 buffer_put_cstring(&b,
586 datafellows & SSH_BUG_HBSERVICE ?
587 "ssh-userauth" :
588 authctxt->service);
589 buffer_put_cstring(&b, "hostbased");
590 buffer_put_string(&b, pkalg, alen);
591 buffer_put_string(&b, pkblob, blen);
592 buffer_put_cstring(&b, chost);
593 buffer_put_cstring(&b, cuser);
594#ifdef DEBUG_PK
595 buffer_dump(&b);
596#endif
597 /* test for allowed key and correct signature */
598 if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
599 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
600 authenticated = 1;
601
602 buffer_clear(&b);
603 key_free(key);
604
605done:
606 debug2("userauth_hostbased: authenticated %d", authenticated);
607 xfree(pkalg);
608 xfree(pkblob);
609 xfree(cuser);
610 xfree(chost);
611 xfree(sig);
612 return authenticated;
613}
614
Damien Miller874d77b2000-10-14 16:23:11 +1100615/* get current user */
Damien Millereba71ba2000-04-29 23:57:08 +1000616
617struct passwd*
618auth_get_user(void)
619{
Damien Miller874d77b2000-10-14 16:23:11 +1100620 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000621}
622
Damien Miller874d77b2000-10-14 16:23:11 +1100623#define DELIM ","
Damien Millereba71ba2000-04-29 23:57:08 +1000624
Damien Miller874d77b2000-10-14 16:23:11 +1100625char *
626authmethods_get(void)
627{
628 Authmethod *method = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000629 u_int size = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100630 char *list;
631
632 for (method = authmethods; method->name != NULL; method++) {
633 if (strcmp(method->name, "none") == 0)
634 continue;
635 if (method->enabled != NULL && *(method->enabled) != 0) {
636 if (size != 0)
637 size += strlen(DELIM);
638 size += strlen(method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000639 }
640 }
Damien Miller874d77b2000-10-14 16:23:11 +1100641 size++; /* trailing '\0' */
642 list = xmalloc(size);
643 list[0] = '\0';
644
645 for (method = authmethods; method->name != NULL; method++) {
646 if (strcmp(method->name, "none") == 0)
647 continue;
648 if (method->enabled != NULL && *(method->enabled) != 0) {
649 if (list[0] != '\0')
650 strlcat(list, DELIM, size);
651 strlcat(list, method->name, size);
652 }
653 }
654 return list;
655}
656
657Authmethod *
658authmethod_lookup(const char *name)
659{
660 Authmethod *method = NULL;
661 if (name != NULL)
662 for (method = authmethods; method->name != NULL; method++)
663 if (method->enabled != NULL &&
664 *(method->enabled) != 0 &&
665 strcmp(name, method->name) == 0)
666 return method;
667 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
668 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000669}
670
671/* return 1 if user allows given key */
672int
Damien Miller0bc1bd82000-11-13 22:57:25 +1100673user_key_allowed(struct passwd *pw, Key *key)
Damien Millereba71ba2000-04-29 23:57:08 +1000674{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000675 char line[8192], file[MAXPATHLEN];
Damien Millereba71ba2000-04-29 23:57:08 +1000676 int found_key = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000677 FILE *f;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000678 u_long linenum = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000679 struct stat st;
680 Key *found;
681
Damien Miller874d77b2000-10-14 16:23:11 +1100682 if (pw == NULL)
683 return 0;
684
Damien Millereba71ba2000-04-29 23:57:08 +1000685 /* Temporarily use the user's uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000686 temporarily_use_uid(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000687
688 /* The authorized keys. */
689 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000690 _PATH_SSH_USER_PERMITTED_KEYS2);
Damien Millereba71ba2000-04-29 23:57:08 +1000691
692 /* Fail quietly if file does not exist */
693 if (stat(file, &st) < 0) {
694 /* Restore the privileged uid. */
695 restore_uid();
696 return 0;
697 }
698 /* Open the file containing the authorized keys. */
699 f = fopen(file, "r");
700 if (!f) {
701 /* Restore the privileged uid. */
702 restore_uid();
703 return 0;
704 }
705 if (options.strict_modes) {
706 int fail = 0;
707 char buf[1024];
708 /* Check open file in order to avoid open/stat races */
709 if (fstat(fileno(f), &st) < 0 ||
710 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
711 (st.st_mode & 022) != 0) {
Damien Millerd3444942000-09-30 14:20:03 +1100712 snprintf(buf, sizeof buf,
713 "%s authentication refused for %.100s: "
714 "bad ownership or modes for '%s'.",
715 key_type(key), pw->pw_name, file);
Damien Millereba71ba2000-04-29 23:57:08 +1000716 fail = 1;
717 } else {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000718 /* Check path to _PATH_SSH_USER_PERMITTED_KEYS */
Damien Millereba71ba2000-04-29 23:57:08 +1000719 int i;
720 static const char *check[] = {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000721 "", _PATH_SSH_USER_DIR, NULL
Damien Millereba71ba2000-04-29 23:57:08 +1000722 };
723 for (i = 0; check[i]; i++) {
724 snprintf(line, sizeof line, "%.500s/%.100s",
725 pw->pw_dir, check[i]);
726 if (stat(line, &st) < 0 ||
727 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
728 (st.st_mode & 022) != 0) {
729 snprintf(buf, sizeof buf,
Damien Millerd3444942000-09-30 14:20:03 +1100730 "%s authentication refused for %.100s: "
Damien Millereba71ba2000-04-29 23:57:08 +1000731 "bad ownership or modes for '%s'.",
Damien Millerd3444942000-09-30 14:20:03 +1100732 key_type(key), pw->pw_name, line);
Damien Millereba71ba2000-04-29 23:57:08 +1000733 fail = 1;
734 break;
735 }
736 }
737 }
738 if (fail) {
Damien Millereba71ba2000-04-29 23:57:08 +1000739 fclose(f);
Ben Lindstrom204e4882001-03-05 06:47:00 +0000740 log("%s", buf);
Damien Millereba71ba2000-04-29 23:57:08 +1000741 restore_uid();
742 return 0;
743 }
744 }
745 found_key = 0;
Damien Millerd3444942000-09-30 14:20:03 +1100746 found = key_new(key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000747
748 while (fgets(line, sizeof(line), f)) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000749 char *cp, *options = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000750 linenum++;
751 /* Skip leading whitespace, empty and comment lines. */
752 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
753 ;
754 if (!*cp || *cp == '\n' || *cp == '#')
755 continue;
Damien Millerf6d9e222000-06-18 14:50:44 +1000756
Damien Miller0bc1bd82000-11-13 22:57:25 +1100757 if (key_read(found, &cp) == -1) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000758 /* no key? check if there are options for this key */
759 int quoted = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100760 debug2("user_key_allowed: check options: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000761 options = cp;
762 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
763 if (*cp == '\\' && cp[1] == '"')
764 cp++; /* Skip both */
765 else if (*cp == '"')
766 quoted = !quoted;
767 }
768 /* Skip remaining whitespace. */
769 for (; *cp == ' ' || *cp == '\t'; cp++)
770 ;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100771 if (key_read(found, &cp) == -1) {
772 debug2("user_key_allowed: advance: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000773 /* still no key? advance to next line*/
774 continue;
775 }
776 }
777 if (key_equal(found, key) &&
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000778 auth_parse_options(pw, options, file, linenum) == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000779 found_key = 1;
780 debug("matching key found: file %s, line %ld",
781 file, linenum);
782 break;
783 }
784 }
785 restore_uid();
786 fclose(f);
787 key_free(found);
Ben Lindstromb54873a2001-03-11 20:01:55 +0000788 if (!found_key)
789 debug2("key not found");
Damien Millereba71ba2000-04-29 23:57:08 +1000790 return found_key;
791}
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000792
793/* return 1 if given hostkey is allowed */
794int
795hostbased_key_allowed(struct passwd *pw, const char *cuser, const char *chost,
796 Key *key)
797{
798 Key *found;
799 const char *resolvedname, *ipaddr, *lookup;
800 struct stat st;
801 char *user_hostfile;
802 int host_status;
803
804 resolvedname = get_canonical_hostname(options.reverse_mapping_check);
805 ipaddr = get_remote_ipaddr();
806
807 debug2("userauth_hostbased: resolvedname %s ipaddr %s",
808 resolvedname, ipaddr);
809
810 if (options.hostbased_uses_name_from_packet_only) {
811 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
812 return 0;
813 lookup = chost;
814 } else {
815 if (strcasecmp(resolvedname, chost) != 0)
816 log("userauth_hostbased mismatch: "
817 "client sends %s, but we resolve %s to %s",
818 chost, ipaddr, resolvedname);
819 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
820 return 0;
821 lookup = resolvedname;
822 }
823 debug2("userauth_hostbased: access allowed by auth_rhosts2");
824
825 /* XXX this is copied from auth-rh-rsa.c and should be shared */
826 found = key_new(key->type);
827 host_status = check_host_in_hostfile(_PATH_SSH_SYSTEM_HOSTFILE2, lookup,
828 key, found, NULL);
829
830 if (host_status != HOST_OK && !options.ignore_user_known_hosts) {
831 user_hostfile = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE2,
832 pw->pw_uid);
833 if (options.strict_modes &&
834 (stat(user_hostfile, &st) == 0) &&
835 ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
836 (st.st_mode & 022) != 0)) {
837 log("Hostbased authentication refused for %.100s: "
838 "bad owner or modes for %.200s",
839 pw->pw_name, user_hostfile);
840 } else {
841 temporarily_use_uid(pw);
842 host_status = check_host_in_hostfile(user_hostfile,
843 lookup, key, found, NULL);
844 restore_uid();
845 }
846 xfree(user_hostfile);
847 }
848 key_free(found);
849
850 debug2("userauth_hostbased: key %s for %s", host_status == HOST_OK ?
851 "ok" : "not found", lookup);
852 return (host_status == HOST_OK);
853}