blob: 8d229955cb6a76389827dced58bb5a46e3d8261d [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 Lindstromb54873a2001-03-11 20:01:55 +000026RCSID("$OpenBSD: auth2.c,v 1.46 2001/03/11 13:25:36 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"
Damien Millereba71ba2000-04-29 23:57:08 +100051
52/* import */
53extern ServerOptions options;
Ben Lindstrom46c16222000-12-22 01:43:59 +000054extern u_char *session_id2;
Damien Millereba71ba2000-04-29 23:57:08 +100055extern int session_id2_len;
56
Damien Miller874d77b2000-10-14 16:23:11 +110057#ifdef WITH_AIXAUTHENTICATE
58extern char *aixloginmsg;
59#endif
Damien Miller874d77b2000-10-14 16:23:11 +110060
61static Authctxt *x_authctxt = NULL;
62static int one = 1;
63
64typedef struct Authmethod Authmethod;
65struct Authmethod {
66 char *name;
67 int (*userauth)(Authctxt *authctxt);
68 int *enabled;
69};
70
Damien Millereba71ba2000-04-29 23:57:08 +100071/* protocol */
72
Damien Miller62cee002000-09-23 17:15:56 +110073void input_service_request(int type, int plen, void *ctxt);
74void input_userauth_request(int type, int plen, void *ctxt);
75void protocol_error(int type, int plen, void *ctxt);
Damien Millereba71ba2000-04-29 23:57:08 +100076
Damien Millereba71ba2000-04-29 23:57:08 +100077/* helper */
Damien Miller874d77b2000-10-14 16:23:11 +110078Authmethod *authmethod_lookup(const char *name);
Damien Miller0bc1bd82000-11-13 22:57:25 +110079int user_key_allowed(struct passwd *pw, Key *key);
Damien Miller874d77b2000-10-14 16:23:11 +110080char *authmethods_get(void);
Damien Millereba71ba2000-04-29 23:57:08 +100081
Damien Miller874d77b2000-10-14 16:23:11 +110082/* auth */
Ben Lindstrom48bd7c12001-01-09 00:35:42 +000083void userauth_banner(void);
Damien Miller874d77b2000-10-14 16:23:11 +110084int userauth_none(Authctxt *authctxt);
85int userauth_passwd(Authctxt *authctxt);
86int userauth_pubkey(Authctxt *authctxt);
87int userauth_kbdint(Authctxt *authctxt);
88
89Authmethod authmethods[] = {
90 {"none",
91 userauth_none,
92 &one},
93 {"publickey",
94 userauth_pubkey,
Damien Miller0bc1bd82000-11-13 22:57:25 +110095 &options.pubkey_authentication},
Damien Miller874d77b2000-10-14 16:23:11 +110096 {"password",
97 userauth_passwd,
98 &options.password_authentication},
Ben Lindstromd1f20ec2001-02-10 21:31:53 +000099 {"keyboard-interactive",
100 userauth_kbdint,
101 &options.kbd_interactive_authentication},
Damien Miller874d77b2000-10-14 16:23:11 +1100102 {NULL, NULL, NULL}
Damien Millereba71ba2000-04-29 23:57:08 +1000103};
Damien Millereba71ba2000-04-29 23:57:08 +1000104
105/*
Damien Miller874d77b2000-10-14 16:23:11 +1100106 * loop until authctxt->success == TRUE
Damien Millereba71ba2000-04-29 23:57:08 +1000107 */
108
109void
110do_authentication2()
111{
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000112 Authctxt *authctxt = authctxt_new();
113
Damien Miller874d77b2000-10-14 16:23:11 +1100114 x_authctxt = authctxt; /*XXX*/
115
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000116 /* challenge-reponse is implemented via keyboard interactive */
117 if (options.challenge_reponse_authentication)
118 options.kbd_interactive_authentication = 1;
119
Damien Millereba71ba2000-04-29 23:57:08 +1000120 dispatch_init(&protocol_error);
121 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
Damien Miller874d77b2000-10-14 16:23:11 +1100122 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000123 do_authenticated2(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000124}
125
126void
Damien Miller62cee002000-09-23 17:15:56 +1100127protocol_error(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000128{
129 log("auth: protocol error: type %d plen %d", type, plen);
130 packet_start(SSH2_MSG_UNIMPLEMENTED);
131 packet_put_int(0);
132 packet_send();
133 packet_write_wait();
134}
135
136void
Damien Miller62cee002000-09-23 17:15:56 +1100137input_service_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000138{
Damien Miller874d77b2000-10-14 16:23:11 +1100139 Authctxt *authctxt = ctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000140 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000141 int accept = 0;
142 char *service = packet_get_string(&len);
143 packet_done();
144
Damien Miller874d77b2000-10-14 16:23:11 +1100145 if (authctxt == NULL)
146 fatal("input_service_request: no authctxt");
147
Damien Millereba71ba2000-04-29 23:57:08 +1000148 if (strcmp(service, "ssh-userauth") == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100149 if (!authctxt->success) {
Damien Millereba71ba2000-04-29 23:57:08 +1000150 accept = 1;
151 /* now we can handle user-auth requests */
152 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
153 }
154 }
155 /* XXX all other service requests are denied */
156
157 if (accept) {
158 packet_start(SSH2_MSG_SERVICE_ACCEPT);
159 packet_put_cstring(service);
160 packet_send();
161 packet_write_wait();
162 } else {
163 debug("bad service request %s", service);
164 packet_disconnect("bad service request %s", service);
165 }
166 xfree(service);
167}
168
169void
Damien Miller62cee002000-09-23 17:15:56 +1100170input_userauth_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000171{
Damien Miller874d77b2000-10-14 16:23:11 +1100172 Authctxt *authctxt = ctxt;
173 Authmethod *m = NULL;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000174 char *user, *service, *method, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000175 int authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000176
Damien Miller874d77b2000-10-14 16:23:11 +1100177 if (authctxt == NULL)
178 fatal("input_userauth_request: no authctxt");
Damien Millereba71ba2000-04-29 23:57:08 +1000179
Damien Miller874d77b2000-10-14 16:23:11 +1100180 user = packet_get_string(NULL);
181 service = packet_get_string(NULL);
182 method = packet_get_string(NULL);
183 debug("userauth-request for user %s service %s method %s", user, service, method);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000184 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
Damien Miller874d77b2000-10-14 16:23:11 +1100185
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000186 if ((style = strchr(user, ':')) != NULL)
187 *style++ = 0;
188
Kevin Stevesef4eea92001-02-05 12:42:17 +0000189 if (authctxt->attempt++ == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100190 /* setup auth context */
191 struct passwd *pw = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100192 pw = getpwnam(user);
193 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
194 authctxt->pw = pwcopy(pw);
195 authctxt->valid = 1;
196 debug2("input_userauth_request: setting up authctxt for %s", user);
197#ifdef USE_PAM
Damien Miller22e22bf2001-01-19 15:46:38 +1100198 start_pam(pw->pw_name);
Damien Miller874d77b2000-10-14 16:23:11 +1100199#endif
200 } else {
201 log("input_userauth_request: illegal user %s", user);
Damien Miller22e22bf2001-01-19 15:46:38 +1100202#ifdef USE_PAM
203 start_pam("NOUSER");
204#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100205 }
Ben Lindstromc1ba31f2001-02-15 03:14:11 +0000206 setproctitle("%s", pw ? user : "unknown");
Damien Miller874d77b2000-10-14 16:23:11 +1100207 authctxt->user = xstrdup(user);
208 authctxt->service = xstrdup(service);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000209 authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */
Damien Miller874d77b2000-10-14 16:23:11 +1100210 } else if (authctxt->valid) {
211 if (strcmp(user, authctxt->user) != 0 ||
212 strcmp(service, authctxt->service) != 0) {
213 log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)",
214 user, service, authctxt->user, authctxt->service);
215 authctxt->valid = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000216 }
217 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000218 /* reset state */
219 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
220 authctxt->postponed = 0;
Damien Miller60396b02001-02-18 17:01:00 +1100221#ifdef BSD_AUTH
222 if (authctxt->as) {
223 auth_close(authctxt->as);
224 authctxt->as = NULL;
225 }
226#endif
Damien Millerb70b61f2000-09-16 16:25:12 +1100227
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000228 /* try to authenticate user */
Damien Miller874d77b2000-10-14 16:23:11 +1100229 m = authmethod_lookup(method);
230 if (m != NULL) {
231 debug2("input_userauth_request: try method %s", method);
232 authenticated = m->userauth(authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100233 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000234 if (!authctxt->valid && authenticated)
235 fatal("INTERNAL ERROR: authenticated invalid user %s",
236 authctxt->user);
Damien Millerb70b61f2000-09-16 16:25:12 +1100237
Damien Miller874d77b2000-10-14 16:23:11 +1100238 /* Special handling for root */
Ben Lindstromd8a90212001-02-15 03:08:27 +0000239 if (authenticated && authctxt->pw->pw_uid == 0 &&
240 !auth_root_allowed(method))
Damien Millereba71ba2000-04-29 23:57:08 +1000241 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000242
243#ifdef USE_PAM
Damien Millerc83de6d2001-02-16 14:17:59 +1100244 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
245 NULL))
Damien Millerb70b61f2000-09-16 16:25:12 +1100246 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000247#endif /* USE_PAM */
248
Damien Miller874d77b2000-10-14 16:23:11 +1100249 /* Log before sending the reply */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000250 auth_log(authctxt, authenticated, method, " ssh2");
251
252 if (!authctxt->postponed)
253 userauth_reply(authctxt, authenticated);
Damien Miller874d77b2000-10-14 16:23:11 +1100254
255 xfree(service);
256 xfree(user);
257 xfree(method);
258}
259
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000260void
261userauth_banner(void)
262{
263 struct stat st;
264 char *banner = NULL;
265 off_t len, n;
266 int fd;
267
268 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
269 return;
270 if ((fd = open(options.banner, O_RDONLY)) < 0) {
271 error("userauth_banner: open %s failed: %s",
272 options.banner, strerror(errno));
273 return;
274 }
275 if (fstat(fd, &st) < 0)
276 goto done;
277 len = st.st_size;
278 banner = xmalloc(len + 1);
279 if ((n = read(fd, banner, len)) < 0)
280 goto done;
281 banner[n] = '\0';
282 packet_start(SSH2_MSG_USERAUTH_BANNER);
283 packet_put_cstring(banner);
284 packet_put_cstring(""); /* language, unused */
285 packet_send();
286 debug("userauth_banner: sent");
287done:
288 if (banner)
289 xfree(banner);
290 close(fd);
291 return;
292}
Damien Miller874d77b2000-10-14 16:23:11 +1100293
Kevin Stevesef4eea92001-02-05 12:42:17 +0000294void
Damien Miller874d77b2000-10-14 16:23:11 +1100295userauth_reply(Authctxt *authctxt, int authenticated)
296{
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000297 char *methods;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000298
Damien Millere247cc42000-05-07 12:03:14 +1000299 /* XXX todo: check if multiple auth methods are needed */
Kevin Steves4abe4de2001-02-08 19:16:32 +0000300 if (authenticated == 1) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000301#ifdef WITH_AIXAUTHENTICATE
302 /* We don't have a pty yet, so just label the line as "ssh" */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000303 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
304 get_canonical_hostname(options.reverse_mapping_check),
Damien Miller33804262001-02-04 23:20:18 +1100305 "ssh", &aixloginmsg) < 0)
Damien Millerd2c208a2000-05-17 22:00:02 +1000306 aixloginmsg = NULL;
307#endif /* WITH_AIXAUTHENTICATE */
Damien Millereba71ba2000-04-29 23:57:08 +1000308 /* turn off userauth */
309 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
310 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
311 packet_send();
312 packet_write_wait();
313 /* now we can break out */
Damien Miller874d77b2000-10-14 16:23:11 +1100314 authctxt->success = 1;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000315 } else {
316 if (authctxt->failures++ > AUTH_FAIL_MAX)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000317 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000318 methods = authmethods_get();
Damien Millereba71ba2000-04-29 23:57:08 +1000319 packet_start(SSH2_MSG_USERAUTH_FAILURE);
Damien Miller874d77b2000-10-14 16:23:11 +1100320 packet_put_cstring(methods);
321 packet_put_char(0); /* XXX partial success, unused */
Damien Millereba71ba2000-04-29 23:57:08 +1000322 packet_send();
323 packet_write_wait();
Damien Miller874d77b2000-10-14 16:23:11 +1100324 xfree(methods);
Damien Millereba71ba2000-04-29 23:57:08 +1000325 }
Damien Millereba71ba2000-04-29 23:57:08 +1000326}
327
328int
Damien Miller874d77b2000-10-14 16:23:11 +1100329userauth_none(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000330{
Damien Miller874d77b2000-10-14 16:23:11 +1100331 /* disable method "none", only allowed one time */
332 Authmethod *m = authmethod_lookup("none");
333 if (m != NULL)
334 m->enabled = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000335 packet_done();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000336 userauth_banner();
Damien Millerb8c656e2000-06-28 15:22:41 +1000337
Damien Miller874d77b2000-10-14 16:23:11 +1100338 if (authctxt->valid == 0)
339 return(0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000340
Damien Miller874d77b2000-10-14 16:23:11 +1100341#ifdef HAVE_CYGWIN
342 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
343 return(0);
344#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000345#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100346 return auth_pam_password(authctxt->pw, "");
Damien Millerb8c656e2000-06-28 15:22:41 +1000347#elif defined(HAVE_OSF_SIA)
Damien Miller92ddb7d2001-02-14 01:25:23 +1100348 return 0;
Damien Millerb8c656e2000-06-28 15:22:41 +1000349#else /* !HAVE_OSF_SIA && !USE_PAM */
Damien Miller60396b02001-02-18 17:01:00 +1100350 return auth_password(authctxt, "");
Damien Millereba71ba2000-04-29 23:57:08 +1000351#endif /* USE_PAM */
352}
Damien Miller874d77b2000-10-14 16:23:11 +1100353
Damien Millereba71ba2000-04-29 23:57:08 +1000354int
Damien Miller874d77b2000-10-14 16:23:11 +1100355userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000356{
357 char *password;
358 int authenticated = 0;
359 int change;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000360 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000361 change = packet_get_char();
362 if (change)
363 log("password change not supported");
364 password = packet_get_string(&len);
365 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100366 if (authctxt->valid &&
367#ifdef HAVE_CYGWIN
368 check_nt_auth(1, authctxt->pw->pw_uid) &&
369#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000370#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100371 auth_pam_password(authctxt->pw, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000372#elif defined(HAVE_OSF_SIA)
Damien Miller92ddb7d2001-02-14 01:25:23 +1100373 auth_sia_password(authctxt->user, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000374#else /* !USE_PAM && !HAVE_OSF_SIA */
Damien Miller60396b02001-02-18 17:01:00 +1100375 auth_password(authctxt, password) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000376#endif /* USE_PAM */
377 authenticated = 1;
378 memset(password, 0, len);
379 xfree(password);
380 return authenticated;
381}
Damien Miller874d77b2000-10-14 16:23:11 +1100382
Damien Millereba71ba2000-04-29 23:57:08 +1000383int
Damien Miller874d77b2000-10-14 16:23:11 +1100384userauth_kbdint(Authctxt *authctxt)
385{
386 int authenticated = 0;
387 char *lang = NULL;
388 char *devs = NULL;
389
390 lang = packet_get_string(NULL);
391 devs = packet_get_string(NULL);
392 packet_done();
393
394 debug("keyboard-interactive language %s devs %s", lang, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000395
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000396 if (options.challenge_reponse_authentication)
397 authenticated = auth2_challenge(authctxt, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000398
Damien Millerb8481582000-12-03 11:51:51 +1100399#ifdef USE_PAM
400 if (authenticated == 0)
401 authenticated = auth2_pam(authctxt);
402#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100403 xfree(lang);
404 xfree(devs);
405#ifdef HAVE_CYGWIN
406 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
407 return(0);
408#endif
409 return authenticated;
410}
411
412int
413userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000414{
415 Buffer b;
416 Key *key;
417 char *pkalg, *pkblob, *sig;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000418 u_int alen, blen, slen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100419 int have_sig, pktype;
Damien Millereba71ba2000-04-29 23:57:08 +1000420 int authenticated = 0;
421
Damien Miller874d77b2000-10-14 16:23:11 +1100422 if (!authctxt->valid) {
423 debug2("userauth_pubkey: disabled because of invalid user");
Damien Millereba71ba2000-04-29 23:57:08 +1000424 return 0;
425 }
426 have_sig = packet_get_char();
Ben Lindstromd121f612000-12-03 17:00:47 +0000427 if (datafellows & SSH_BUG_PKAUTH) {
428 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
429 /* no explicit pkalg given */
430 pkblob = packet_get_string(&blen);
431 buffer_init(&b);
432 buffer_append(&b, pkblob, blen);
433 /* so we have to extract the pkalg from the pkblob */
434 pkalg = buffer_get_string(&b, &alen);
435 buffer_free(&b);
436 } else {
437 pkalg = packet_get_string(&alen);
438 pkblob = packet_get_string(&blen);
439 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100440 pktype = key_type_from_name(pkalg);
441 if (pktype == KEY_UNSPEC) {
Ben Lindstromd121f612000-12-03 17:00:47 +0000442 /* this is perfectly legal */
443 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
Damien Miller874d77b2000-10-14 16:23:11 +1100444 xfree(pkalg);
Ben Lindstromd121f612000-12-03 17:00:47 +0000445 xfree(pkblob);
Damien Millereba71ba2000-04-29 23:57:08 +1000446 return 0;
447 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100448 key = key_from_blob(pkblob, blen);
Damien Millereba71ba2000-04-29 23:57:08 +1000449 if (key != NULL) {
450 if (have_sig) {
451 sig = packet_get_string(&slen);
452 packet_done();
453 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100454 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000455 buffer_append(&b, session_id2, session_id2_len);
Damien Miller50a41ed2000-10-16 12:14:42 +1100456 } else {
457 buffer_put_string(&b, session_id2, session_id2_len);
Damien Miller6536c7d2000-06-22 21:32:31 +1000458 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000459 /* reconstruct packet */
Damien Millereba71ba2000-04-29 23:57:08 +1000460 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller874d77b2000-10-14 16:23:11 +1100461 buffer_put_cstring(&b, authctxt->user);
Damien Millerf6d9e222000-06-18 14:50:44 +1000462 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000463 datafellows & SSH_BUG_PKSERVICE ?
Damien Millerf6d9e222000-06-18 14:50:44 +1000464 "ssh-userauth" :
Damien Miller874d77b2000-10-14 16:23:11 +1100465 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000466 if (datafellows & SSH_BUG_PKAUTH) {
467 buffer_put_char(&b, have_sig);
468 } else {
469 buffer_put_cstring(&b, "publickey");
470 buffer_put_char(&b, have_sig);
471 buffer_put_cstring(&b, key_ssh_name(key));
472 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000473 buffer_put_string(&b, pkblob, blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100474#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000475 buffer_dump(&b);
476#endif
477 /* test for correct signature */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100478 if (user_key_allowed(authctxt->pw, key) &&
479 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000480 authenticated = 1;
481 buffer_clear(&b);
482 xfree(sig);
483 } else {
Damien Miller874d77b2000-10-14 16:23:11 +1100484 debug("test whether pkalg/pkblob are acceptable");
Damien Millereba71ba2000-04-29 23:57:08 +1000485 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100486
Damien Millereba71ba2000-04-29 23:57:08 +1000487 /* XXX fake reply and always send PK_OK ? */
Damien Millere247cc42000-05-07 12:03:14 +1000488 /*
489 * XXX this allows testing whether a user is allowed
490 * to login: if you happen to have a valid pubkey this
491 * message is sent. the message is NEVER sent at all
492 * if a user is not allowed to login. is this an
493 * issue? -markus
494 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100495 if (user_key_allowed(authctxt->pw, key)) {
Damien Millereba71ba2000-04-29 23:57:08 +1000496 packet_start(SSH2_MSG_USERAUTH_PK_OK);
497 packet_put_string(pkalg, alen);
498 packet_put_string(pkblob, blen);
499 packet_send();
500 packet_write_wait();
Kevin Steves4abe4de2001-02-08 19:16:32 +0000501 authctxt->postponed = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000502 }
503 }
Damien Miller874d77b2000-10-14 16:23:11 +1100504 if (authenticated != 1)
505 auth_clear_options();
Damien Millereba71ba2000-04-29 23:57:08 +1000506 key_free(key);
507 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100508 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
Damien Millereba71ba2000-04-29 23:57:08 +1000509 xfree(pkalg);
510 xfree(pkblob);
Damien Miller874d77b2000-10-14 16:23:11 +1100511#ifdef HAVE_CYGWIN
512 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
513 return(0);
514#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000515 return authenticated;
516}
517
Damien Miller874d77b2000-10-14 16:23:11 +1100518/* get current user */
Damien Millereba71ba2000-04-29 23:57:08 +1000519
520struct passwd*
521auth_get_user(void)
522{
Damien Miller874d77b2000-10-14 16:23:11 +1100523 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000524}
525
Damien Miller874d77b2000-10-14 16:23:11 +1100526#define DELIM ","
Damien Millereba71ba2000-04-29 23:57:08 +1000527
Damien Miller874d77b2000-10-14 16:23:11 +1100528char *
529authmethods_get(void)
530{
531 Authmethod *method = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000532 u_int size = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100533 char *list;
534
535 for (method = authmethods; method->name != NULL; method++) {
536 if (strcmp(method->name, "none") == 0)
537 continue;
538 if (method->enabled != NULL && *(method->enabled) != 0) {
539 if (size != 0)
540 size += strlen(DELIM);
541 size += strlen(method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000542 }
543 }
Damien Miller874d77b2000-10-14 16:23:11 +1100544 size++; /* trailing '\0' */
545 list = xmalloc(size);
546 list[0] = '\0';
547
548 for (method = authmethods; method->name != NULL; method++) {
549 if (strcmp(method->name, "none") == 0)
550 continue;
551 if (method->enabled != NULL && *(method->enabled) != 0) {
552 if (list[0] != '\0')
553 strlcat(list, DELIM, size);
554 strlcat(list, method->name, size);
555 }
556 }
557 return list;
558}
559
560Authmethod *
561authmethod_lookup(const char *name)
562{
563 Authmethod *method = NULL;
564 if (name != NULL)
565 for (method = authmethods; method->name != NULL; method++)
566 if (method->enabled != NULL &&
567 *(method->enabled) != 0 &&
568 strcmp(name, method->name) == 0)
569 return method;
570 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
571 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000572}
573
574/* return 1 if user allows given key */
575int
Damien Miller0bc1bd82000-11-13 22:57:25 +1100576user_key_allowed(struct passwd *pw, Key *key)
Damien Millereba71ba2000-04-29 23:57:08 +1000577{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000578 char line[8192], file[MAXPATHLEN];
Damien Millereba71ba2000-04-29 23:57:08 +1000579 int found_key = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000580 FILE *f;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000581 u_long linenum = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000582 struct stat st;
583 Key *found;
584
Damien Miller874d77b2000-10-14 16:23:11 +1100585 if (pw == NULL)
586 return 0;
587
Damien Millereba71ba2000-04-29 23:57:08 +1000588 /* Temporarily use the user's uid. */
589 temporarily_use_uid(pw->pw_uid);
590
591 /* The authorized keys. */
592 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000593 _PATH_SSH_USER_PERMITTED_KEYS2);
Damien Millereba71ba2000-04-29 23:57:08 +1000594
595 /* Fail quietly if file does not exist */
596 if (stat(file, &st) < 0) {
597 /* Restore the privileged uid. */
598 restore_uid();
599 return 0;
600 }
601 /* Open the file containing the authorized keys. */
602 f = fopen(file, "r");
603 if (!f) {
604 /* Restore the privileged uid. */
605 restore_uid();
606 return 0;
607 }
608 if (options.strict_modes) {
609 int fail = 0;
610 char buf[1024];
611 /* Check open file in order to avoid open/stat races */
612 if (fstat(fileno(f), &st) < 0 ||
613 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
614 (st.st_mode & 022) != 0) {
Damien Millerd3444942000-09-30 14:20:03 +1100615 snprintf(buf, sizeof buf,
616 "%s authentication refused for %.100s: "
617 "bad ownership or modes for '%s'.",
618 key_type(key), pw->pw_name, file);
Damien Millereba71ba2000-04-29 23:57:08 +1000619 fail = 1;
620 } else {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000621 /* Check path to _PATH_SSH_USER_PERMITTED_KEYS */
Damien Millereba71ba2000-04-29 23:57:08 +1000622 int i;
623 static const char *check[] = {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000624 "", _PATH_SSH_USER_DIR, NULL
Damien Millereba71ba2000-04-29 23:57:08 +1000625 };
626 for (i = 0; check[i]; i++) {
627 snprintf(line, sizeof line, "%.500s/%.100s",
628 pw->pw_dir, check[i]);
629 if (stat(line, &st) < 0 ||
630 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
631 (st.st_mode & 022) != 0) {
632 snprintf(buf, sizeof buf,
Damien Millerd3444942000-09-30 14:20:03 +1100633 "%s authentication refused for %.100s: "
Damien Millereba71ba2000-04-29 23:57:08 +1000634 "bad ownership or modes for '%s'.",
Damien Millerd3444942000-09-30 14:20:03 +1100635 key_type(key), pw->pw_name, line);
Damien Millereba71ba2000-04-29 23:57:08 +1000636 fail = 1;
637 break;
638 }
639 }
640 }
641 if (fail) {
Damien Millereba71ba2000-04-29 23:57:08 +1000642 fclose(f);
Ben Lindstrom204e4882001-03-05 06:47:00 +0000643 log("%s", buf);
Damien Millereba71ba2000-04-29 23:57:08 +1000644 restore_uid();
645 return 0;
646 }
647 }
648 found_key = 0;
Damien Millerd3444942000-09-30 14:20:03 +1100649 found = key_new(key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000650
651 while (fgets(line, sizeof(line), f)) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000652 char *cp, *options = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000653 linenum++;
654 /* Skip leading whitespace, empty and comment lines. */
655 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
656 ;
657 if (!*cp || *cp == '\n' || *cp == '#')
658 continue;
Damien Millerf6d9e222000-06-18 14:50:44 +1000659
Damien Miller0bc1bd82000-11-13 22:57:25 +1100660 if (key_read(found, &cp) == -1) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000661 /* no key? check if there are options for this key */
662 int quoted = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100663 debug2("user_key_allowed: check options: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000664 options = cp;
665 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
666 if (*cp == '\\' && cp[1] == '"')
667 cp++; /* Skip both */
668 else if (*cp == '"')
669 quoted = !quoted;
670 }
671 /* Skip remaining whitespace. */
672 for (; *cp == ' ' || *cp == '\t'; cp++)
673 ;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100674 if (key_read(found, &cp) == -1) {
675 debug2("user_key_allowed: advance: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000676 /* still no key? advance to next line*/
677 continue;
678 }
679 }
680 if (key_equal(found, key) &&
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000681 auth_parse_options(pw, options, file, linenum) == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000682 found_key = 1;
683 debug("matching key found: file %s, line %ld",
684 file, linenum);
685 break;
686 }
687 }
688 restore_uid();
689 fclose(f);
690 key_free(found);
Ben Lindstromb54873a2001-03-11 20:01:55 +0000691 if (!found_key)
692 debug2("key not found");
Damien Millereba71ba2000-04-29 23:57:08 +1000693 return found_key;
694}