blob: 3e99c5ba25f7820a2e1f7a02b7cf7fe469a57e2b [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"
Damien Miller5d57e502001-03-30 10:48:31 +100026RCSID("$OpenBSD: auth2.c,v 1.49 2001/03/28 22:43:31 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 Miller5d57e502001-03-30 10:48:31 +100084void userauth_reply(Authctxt *authctxt, int authenticated);
Damien Miller874d77b2000-10-14 16:23:11 +110085int userauth_none(Authctxt *authctxt);
86int userauth_passwd(Authctxt *authctxt);
87int userauth_pubkey(Authctxt *authctxt);
88int userauth_kbdint(Authctxt *authctxt);
89
90Authmethod authmethods[] = {
91 {"none",
92 userauth_none,
93 &one},
94 {"publickey",
95 userauth_pubkey,
Damien Miller0bc1bd82000-11-13 22:57:25 +110096 &options.pubkey_authentication},
Damien Miller874d77b2000-10-14 16:23:11 +110097 {"password",
98 userauth_passwd,
99 &options.password_authentication},
Ben Lindstromd1f20ec2001-02-10 21:31:53 +0000100 {"keyboard-interactive",
101 userauth_kbdint,
102 &options.kbd_interactive_authentication},
Damien Miller874d77b2000-10-14 16:23:11 +1100103 {NULL, NULL, NULL}
Damien Millereba71ba2000-04-29 23:57:08 +1000104};
Damien Millereba71ba2000-04-29 23:57:08 +1000105
106/*
Damien Miller874d77b2000-10-14 16:23:11 +1100107 * loop until authctxt->success == TRUE
Damien Millereba71ba2000-04-29 23:57:08 +1000108 */
109
110void
111do_authentication2()
112{
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000113 Authctxt *authctxt = authctxt_new();
114
Damien Miller874d77b2000-10-14 16:23:11 +1100115 x_authctxt = authctxt; /*XXX*/
116
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000117 /* challenge-reponse is implemented via keyboard interactive */
118 if (options.challenge_reponse_authentication)
119 options.kbd_interactive_authentication = 1;
120
Damien Millereba71ba2000-04-29 23:57:08 +1000121 dispatch_init(&protocol_error);
122 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
Damien Miller874d77b2000-10-14 16:23:11 +1100123 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000124 do_authenticated(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000125}
126
127void
Damien Miller62cee002000-09-23 17:15:56 +1100128protocol_error(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000129{
130 log("auth: protocol error: type %d plen %d", type, plen);
131 packet_start(SSH2_MSG_UNIMPLEMENTED);
132 packet_put_int(0);
133 packet_send();
134 packet_write_wait();
135}
136
137void
Damien Miller62cee002000-09-23 17:15:56 +1100138input_service_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000139{
Damien Miller874d77b2000-10-14 16:23:11 +1100140 Authctxt *authctxt = ctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000141 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000142 int accept = 0;
143 char *service = packet_get_string(&len);
144 packet_done();
145
Damien Miller874d77b2000-10-14 16:23:11 +1100146 if (authctxt == NULL)
147 fatal("input_service_request: no authctxt");
148
Damien Millereba71ba2000-04-29 23:57:08 +1000149 if (strcmp(service, "ssh-userauth") == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100150 if (!authctxt->success) {
Damien Millereba71ba2000-04-29 23:57:08 +1000151 accept = 1;
152 /* now we can handle user-auth requests */
153 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
154 }
155 }
156 /* XXX all other service requests are denied */
157
158 if (accept) {
159 packet_start(SSH2_MSG_SERVICE_ACCEPT);
160 packet_put_cstring(service);
161 packet_send();
162 packet_write_wait();
163 } else {
164 debug("bad service request %s", service);
165 packet_disconnect("bad service request %s", service);
166 }
167 xfree(service);
168}
169
170void
Damien Miller62cee002000-09-23 17:15:56 +1100171input_userauth_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000172{
Damien Miller874d77b2000-10-14 16:23:11 +1100173 Authctxt *authctxt = ctxt;
174 Authmethod *m = NULL;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000175 char *user, *service, *method, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000176 int authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000177
Damien Miller874d77b2000-10-14 16:23:11 +1100178 if (authctxt == NULL)
179 fatal("input_userauth_request: no authctxt");
Damien Millereba71ba2000-04-29 23:57:08 +1000180
Damien Miller874d77b2000-10-14 16:23:11 +1100181 user = packet_get_string(NULL);
182 service = packet_get_string(NULL);
183 method = packet_get_string(NULL);
184 debug("userauth-request for user %s service %s method %s", user, service, method);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000185 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
Damien Miller874d77b2000-10-14 16:23:11 +1100186
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000187 if ((style = strchr(user, ':')) != NULL)
188 *style++ = 0;
189
Kevin Stevesef4eea92001-02-05 12:42:17 +0000190 if (authctxt->attempt++ == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100191 /* setup auth context */
192 struct passwd *pw = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100193 pw = getpwnam(user);
194 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
195 authctxt->pw = pwcopy(pw);
196 authctxt->valid = 1;
197 debug2("input_userauth_request: setting up authctxt for %s", user);
198#ifdef USE_PAM
Damien Miller22e22bf2001-01-19 15:46:38 +1100199 start_pam(pw->pw_name);
Damien Miller874d77b2000-10-14 16:23:11 +1100200#endif
201 } else {
202 log("input_userauth_request: illegal user %s", user);
Damien Miller22e22bf2001-01-19 15:46:38 +1100203#ifdef USE_PAM
204 start_pam("NOUSER");
205#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100206 }
Ben Lindstromc1ba31f2001-02-15 03:14:11 +0000207 setproctitle("%s", pw ? user : "unknown");
Damien Miller874d77b2000-10-14 16:23:11 +1100208 authctxt->user = xstrdup(user);
209 authctxt->service = xstrdup(service);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000210 authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */
Damien Miller874d77b2000-10-14 16:23:11 +1100211 } else if (authctxt->valid) {
212 if (strcmp(user, authctxt->user) != 0 ||
213 strcmp(service, authctxt->service) != 0) {
214 log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)",
215 user, service, authctxt->user, authctxt->service);
216 authctxt->valid = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000217 }
218 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000219 /* reset state */
220 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
221 authctxt->postponed = 0;
Damien Miller60396b02001-02-18 17:01:00 +1100222#ifdef BSD_AUTH
223 if (authctxt->as) {
224 auth_close(authctxt->as);
225 authctxt->as = NULL;
226 }
227#endif
Damien Millerb70b61f2000-09-16 16:25:12 +1100228
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000229 /* try to authenticate user */
Damien Miller874d77b2000-10-14 16:23:11 +1100230 m = authmethod_lookup(method);
231 if (m != NULL) {
232 debug2("input_userauth_request: try method %s", method);
233 authenticated = m->userauth(authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100234 }
Damien Miller5d57e502001-03-30 10:48:31 +1000235 userauth_finish(authctxt, authenticated, method);
236
237 xfree(service);
238 xfree(user);
239 xfree(method);
240}
241
242void
243userauth_finish(Authctxt *authctxt, int authenticated, char *method)
244{
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000245 if (!authctxt->valid && authenticated)
246 fatal("INTERNAL ERROR: authenticated invalid user %s",
247 authctxt->user);
Damien Millerb70b61f2000-09-16 16:25:12 +1100248
Damien Miller874d77b2000-10-14 16:23:11 +1100249 /* Special handling for root */
Ben Lindstromd8a90212001-02-15 03:08:27 +0000250 if (authenticated && authctxt->pw->pw_uid == 0 &&
251 !auth_root_allowed(method))
Damien Millereba71ba2000-04-29 23:57:08 +1000252 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000253
254#ifdef USE_PAM
Damien Millerc83de6d2001-02-16 14:17:59 +1100255 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
256 NULL))
Damien Millerb70b61f2000-09-16 16:25:12 +1100257 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000258#endif /* USE_PAM */
259
Damien Miller874d77b2000-10-14 16:23:11 +1100260 /* Log before sending the reply */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000261 auth_log(authctxt, authenticated, method, " ssh2");
262
263 if (!authctxt->postponed)
264 userauth_reply(authctxt, authenticated);
Damien Miller874d77b2000-10-14 16:23:11 +1100265}
266
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000267void
268userauth_banner(void)
269{
270 struct stat st;
271 char *banner = NULL;
272 off_t len, n;
273 int fd;
274
275 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
276 return;
277 if ((fd = open(options.banner, O_RDONLY)) < 0) {
278 error("userauth_banner: open %s failed: %s",
279 options.banner, strerror(errno));
280 return;
281 }
282 if (fstat(fd, &st) < 0)
283 goto done;
284 len = st.st_size;
285 banner = xmalloc(len + 1);
286 if ((n = read(fd, banner, len)) < 0)
287 goto done;
288 banner[n] = '\0';
289 packet_start(SSH2_MSG_USERAUTH_BANNER);
290 packet_put_cstring(banner);
291 packet_put_cstring(""); /* language, unused */
292 packet_send();
293 debug("userauth_banner: sent");
294done:
295 if (banner)
296 xfree(banner);
297 close(fd);
298 return;
299}
Damien Miller874d77b2000-10-14 16:23:11 +1100300
Kevin Stevesef4eea92001-02-05 12:42:17 +0000301void
Damien Miller874d77b2000-10-14 16:23:11 +1100302userauth_reply(Authctxt *authctxt, int authenticated)
303{
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000304 char *methods;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000305
Damien Millere247cc42000-05-07 12:03:14 +1000306 /* XXX todo: check if multiple auth methods are needed */
Kevin Steves4abe4de2001-02-08 19:16:32 +0000307 if (authenticated == 1) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000308#ifdef WITH_AIXAUTHENTICATE
309 /* We don't have a pty yet, so just label the line as "ssh" */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000310 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
311 get_canonical_hostname(options.reverse_mapping_check),
Damien Miller33804262001-02-04 23:20:18 +1100312 "ssh", &aixloginmsg) < 0)
Damien Millerd2c208a2000-05-17 22:00:02 +1000313 aixloginmsg = NULL;
314#endif /* WITH_AIXAUTHENTICATE */
Damien Millereba71ba2000-04-29 23:57:08 +1000315 /* turn off userauth */
316 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
317 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
318 packet_send();
319 packet_write_wait();
320 /* now we can break out */
Damien Miller874d77b2000-10-14 16:23:11 +1100321 authctxt->success = 1;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000322 } else {
323 if (authctxt->failures++ > AUTH_FAIL_MAX)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000324 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000325 methods = authmethods_get();
Damien Millereba71ba2000-04-29 23:57:08 +1000326 packet_start(SSH2_MSG_USERAUTH_FAILURE);
Damien Miller874d77b2000-10-14 16:23:11 +1100327 packet_put_cstring(methods);
328 packet_put_char(0); /* XXX partial success, unused */
Damien Millereba71ba2000-04-29 23:57:08 +1000329 packet_send();
330 packet_write_wait();
Damien Miller874d77b2000-10-14 16:23:11 +1100331 xfree(methods);
Damien Millereba71ba2000-04-29 23:57:08 +1000332 }
Damien Millereba71ba2000-04-29 23:57:08 +1000333}
334
335int
Damien Miller874d77b2000-10-14 16:23:11 +1100336userauth_none(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000337{
Damien Miller874d77b2000-10-14 16:23:11 +1100338 /* disable method "none", only allowed one time */
339 Authmethod *m = authmethod_lookup("none");
340 if (m != NULL)
341 m->enabled = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000342 packet_done();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000343 userauth_banner();
Damien Millerb8c656e2000-06-28 15:22:41 +1000344
Damien Miller874d77b2000-10-14 16:23:11 +1100345 if (authctxt->valid == 0)
346 return(0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000347
Damien Miller874d77b2000-10-14 16:23:11 +1100348#ifdef HAVE_CYGWIN
349 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
350 return(0);
351#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000352#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100353 return auth_pam_password(authctxt->pw, "");
Damien Millerb8c656e2000-06-28 15:22:41 +1000354#elif defined(HAVE_OSF_SIA)
Damien Miller92ddb7d2001-02-14 01:25:23 +1100355 return 0;
Damien Millerb8c656e2000-06-28 15:22:41 +1000356#else /* !HAVE_OSF_SIA && !USE_PAM */
Damien Miller60396b02001-02-18 17:01:00 +1100357 return auth_password(authctxt, "");
Damien Millereba71ba2000-04-29 23:57:08 +1000358#endif /* USE_PAM */
359}
Damien Miller874d77b2000-10-14 16:23:11 +1100360
Damien Millereba71ba2000-04-29 23:57:08 +1000361int
Damien Miller874d77b2000-10-14 16:23:11 +1100362userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000363{
364 char *password;
365 int authenticated = 0;
366 int change;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000367 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000368 change = packet_get_char();
369 if (change)
370 log("password change not supported");
371 password = packet_get_string(&len);
372 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100373 if (authctxt->valid &&
374#ifdef HAVE_CYGWIN
375 check_nt_auth(1, authctxt->pw->pw_uid) &&
376#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000377#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100378 auth_pam_password(authctxt->pw, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000379#elif defined(HAVE_OSF_SIA)
Damien Miller92ddb7d2001-02-14 01:25:23 +1100380 auth_sia_password(authctxt->user, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000381#else /* !USE_PAM && !HAVE_OSF_SIA */
Damien Miller60396b02001-02-18 17:01:00 +1100382 auth_password(authctxt, password) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000383#endif /* USE_PAM */
384 authenticated = 1;
385 memset(password, 0, len);
386 xfree(password);
387 return authenticated;
388}
Damien Miller874d77b2000-10-14 16:23:11 +1100389
Damien Millereba71ba2000-04-29 23:57:08 +1000390int
Damien Miller874d77b2000-10-14 16:23:11 +1100391userauth_kbdint(Authctxt *authctxt)
392{
393 int authenticated = 0;
394 char *lang = NULL;
395 char *devs = NULL;
396
397 lang = packet_get_string(NULL);
398 devs = packet_get_string(NULL);
399 packet_done();
400
401 debug("keyboard-interactive language %s devs %s", lang, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000402
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000403 if (options.challenge_reponse_authentication)
404 authenticated = auth2_challenge(authctxt, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000405
Damien Millerb8481582000-12-03 11:51:51 +1100406#ifdef USE_PAM
407 if (authenticated == 0)
408 authenticated = auth2_pam(authctxt);
409#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100410 xfree(lang);
411 xfree(devs);
412#ifdef HAVE_CYGWIN
413 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
414 return(0);
415#endif
416 return authenticated;
417}
418
419int
420userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000421{
422 Buffer b;
423 Key *key;
424 char *pkalg, *pkblob, *sig;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000425 u_int alen, blen, slen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100426 int have_sig, pktype;
Damien Millereba71ba2000-04-29 23:57:08 +1000427 int authenticated = 0;
428
Damien Miller874d77b2000-10-14 16:23:11 +1100429 if (!authctxt->valid) {
430 debug2("userauth_pubkey: disabled because of invalid user");
Damien Millereba71ba2000-04-29 23:57:08 +1000431 return 0;
432 }
433 have_sig = packet_get_char();
Ben Lindstromd121f612000-12-03 17:00:47 +0000434 if (datafellows & SSH_BUG_PKAUTH) {
435 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
436 /* no explicit pkalg given */
437 pkblob = packet_get_string(&blen);
438 buffer_init(&b);
439 buffer_append(&b, pkblob, blen);
440 /* so we have to extract the pkalg from the pkblob */
441 pkalg = buffer_get_string(&b, &alen);
442 buffer_free(&b);
443 } else {
444 pkalg = packet_get_string(&alen);
445 pkblob = packet_get_string(&blen);
446 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100447 pktype = key_type_from_name(pkalg);
448 if (pktype == KEY_UNSPEC) {
Ben Lindstromd121f612000-12-03 17:00:47 +0000449 /* this is perfectly legal */
450 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
Damien Miller874d77b2000-10-14 16:23:11 +1100451 xfree(pkalg);
Ben Lindstromd121f612000-12-03 17:00:47 +0000452 xfree(pkblob);
Damien Millereba71ba2000-04-29 23:57:08 +1000453 return 0;
454 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100455 key = key_from_blob(pkblob, blen);
Damien Millereba71ba2000-04-29 23:57:08 +1000456 if (key != NULL) {
457 if (have_sig) {
458 sig = packet_get_string(&slen);
459 packet_done();
460 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100461 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000462 buffer_append(&b, session_id2, session_id2_len);
Damien Miller50a41ed2000-10-16 12:14:42 +1100463 } else {
464 buffer_put_string(&b, session_id2, session_id2_len);
Damien Miller6536c7d2000-06-22 21:32:31 +1000465 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000466 /* reconstruct packet */
Damien Millereba71ba2000-04-29 23:57:08 +1000467 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller874d77b2000-10-14 16:23:11 +1100468 buffer_put_cstring(&b, authctxt->user);
Damien Millerf6d9e222000-06-18 14:50:44 +1000469 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000470 datafellows & SSH_BUG_PKSERVICE ?
Damien Millerf6d9e222000-06-18 14:50:44 +1000471 "ssh-userauth" :
Damien Miller874d77b2000-10-14 16:23:11 +1100472 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000473 if (datafellows & SSH_BUG_PKAUTH) {
474 buffer_put_char(&b, have_sig);
475 } else {
476 buffer_put_cstring(&b, "publickey");
477 buffer_put_char(&b, have_sig);
478 buffer_put_cstring(&b, key_ssh_name(key));
479 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000480 buffer_put_string(&b, pkblob, blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100481#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000482 buffer_dump(&b);
483#endif
484 /* test for correct signature */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100485 if (user_key_allowed(authctxt->pw, key) &&
486 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000487 authenticated = 1;
488 buffer_clear(&b);
489 xfree(sig);
490 } else {
Damien Miller874d77b2000-10-14 16:23:11 +1100491 debug("test whether pkalg/pkblob are acceptable");
Damien Millereba71ba2000-04-29 23:57:08 +1000492 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100493
Damien Millereba71ba2000-04-29 23:57:08 +1000494 /* XXX fake reply and always send PK_OK ? */
Damien Millere247cc42000-05-07 12:03:14 +1000495 /*
496 * XXX this allows testing whether a user is allowed
497 * to login: if you happen to have a valid pubkey this
498 * message is sent. the message is NEVER sent at all
499 * if a user is not allowed to login. is this an
500 * issue? -markus
501 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100502 if (user_key_allowed(authctxt->pw, key)) {
Damien Millereba71ba2000-04-29 23:57:08 +1000503 packet_start(SSH2_MSG_USERAUTH_PK_OK);
504 packet_put_string(pkalg, alen);
505 packet_put_string(pkblob, blen);
506 packet_send();
507 packet_write_wait();
Kevin Steves4abe4de2001-02-08 19:16:32 +0000508 authctxt->postponed = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000509 }
510 }
Damien Miller874d77b2000-10-14 16:23:11 +1100511 if (authenticated != 1)
512 auth_clear_options();
Damien Millereba71ba2000-04-29 23:57:08 +1000513 key_free(key);
514 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100515 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
Damien Millereba71ba2000-04-29 23:57:08 +1000516 xfree(pkalg);
517 xfree(pkblob);
Damien Miller874d77b2000-10-14 16:23:11 +1100518#ifdef HAVE_CYGWIN
519 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
520 return(0);
521#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000522 return authenticated;
523}
524
Damien Miller874d77b2000-10-14 16:23:11 +1100525/* get current user */
Damien Millereba71ba2000-04-29 23:57:08 +1000526
527struct passwd*
528auth_get_user(void)
529{
Damien Miller874d77b2000-10-14 16:23:11 +1100530 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000531}
532
Damien Miller874d77b2000-10-14 16:23:11 +1100533#define DELIM ","
Damien Millereba71ba2000-04-29 23:57:08 +1000534
Damien Miller874d77b2000-10-14 16:23:11 +1100535char *
536authmethods_get(void)
537{
538 Authmethod *method = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000539 u_int size = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100540 char *list;
541
542 for (method = authmethods; method->name != NULL; method++) {
543 if (strcmp(method->name, "none") == 0)
544 continue;
545 if (method->enabled != NULL && *(method->enabled) != 0) {
546 if (size != 0)
547 size += strlen(DELIM);
548 size += strlen(method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000549 }
550 }
Damien Miller874d77b2000-10-14 16:23:11 +1100551 size++; /* trailing '\0' */
552 list = xmalloc(size);
553 list[0] = '\0';
554
555 for (method = authmethods; method->name != NULL; method++) {
556 if (strcmp(method->name, "none") == 0)
557 continue;
558 if (method->enabled != NULL && *(method->enabled) != 0) {
559 if (list[0] != '\0')
560 strlcat(list, DELIM, size);
561 strlcat(list, method->name, size);
562 }
563 }
564 return list;
565}
566
567Authmethod *
568authmethod_lookup(const char *name)
569{
570 Authmethod *method = NULL;
571 if (name != NULL)
572 for (method = authmethods; method->name != NULL; method++)
573 if (method->enabled != NULL &&
574 *(method->enabled) != 0 &&
575 strcmp(name, method->name) == 0)
576 return method;
577 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
578 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000579}
580
581/* return 1 if user allows given key */
582int
Damien Miller0bc1bd82000-11-13 22:57:25 +1100583user_key_allowed(struct passwd *pw, Key *key)
Damien Millereba71ba2000-04-29 23:57:08 +1000584{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000585 char line[8192], file[MAXPATHLEN];
Damien Millereba71ba2000-04-29 23:57:08 +1000586 int found_key = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000587 FILE *f;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000588 u_long linenum = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000589 struct stat st;
590 Key *found;
591
Damien Miller874d77b2000-10-14 16:23:11 +1100592 if (pw == NULL)
593 return 0;
594
Damien Millereba71ba2000-04-29 23:57:08 +1000595 /* Temporarily use the user's uid. */
596 temporarily_use_uid(pw->pw_uid);
597
598 /* The authorized keys. */
599 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000600 _PATH_SSH_USER_PERMITTED_KEYS2);
Damien Millereba71ba2000-04-29 23:57:08 +1000601
602 /* Fail quietly if file does not exist */
603 if (stat(file, &st) < 0) {
604 /* Restore the privileged uid. */
605 restore_uid();
606 return 0;
607 }
608 /* Open the file containing the authorized keys. */
609 f = fopen(file, "r");
610 if (!f) {
611 /* Restore the privileged uid. */
612 restore_uid();
613 return 0;
614 }
615 if (options.strict_modes) {
616 int fail = 0;
617 char buf[1024];
618 /* Check open file in order to avoid open/stat races */
619 if (fstat(fileno(f), &st) < 0 ||
620 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
621 (st.st_mode & 022) != 0) {
Damien Millerd3444942000-09-30 14:20:03 +1100622 snprintf(buf, sizeof buf,
623 "%s authentication refused for %.100s: "
624 "bad ownership or modes for '%s'.",
625 key_type(key), pw->pw_name, file);
Damien Millereba71ba2000-04-29 23:57:08 +1000626 fail = 1;
627 } else {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000628 /* Check path to _PATH_SSH_USER_PERMITTED_KEYS */
Damien Millereba71ba2000-04-29 23:57:08 +1000629 int i;
630 static const char *check[] = {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000631 "", _PATH_SSH_USER_DIR, NULL
Damien Millereba71ba2000-04-29 23:57:08 +1000632 };
633 for (i = 0; check[i]; i++) {
634 snprintf(line, sizeof line, "%.500s/%.100s",
635 pw->pw_dir, check[i]);
636 if (stat(line, &st) < 0 ||
637 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
638 (st.st_mode & 022) != 0) {
639 snprintf(buf, sizeof buf,
Damien Millerd3444942000-09-30 14:20:03 +1100640 "%s authentication refused for %.100s: "
Damien Millereba71ba2000-04-29 23:57:08 +1000641 "bad ownership or modes for '%s'.",
Damien Millerd3444942000-09-30 14:20:03 +1100642 key_type(key), pw->pw_name, line);
Damien Millereba71ba2000-04-29 23:57:08 +1000643 fail = 1;
644 break;
645 }
646 }
647 }
648 if (fail) {
Damien Millereba71ba2000-04-29 23:57:08 +1000649 fclose(f);
Ben Lindstrom204e4882001-03-05 06:47:00 +0000650 log("%s", buf);
Damien Millereba71ba2000-04-29 23:57:08 +1000651 restore_uid();
652 return 0;
653 }
654 }
655 found_key = 0;
Damien Millerd3444942000-09-30 14:20:03 +1100656 found = key_new(key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000657
658 while (fgets(line, sizeof(line), f)) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000659 char *cp, *options = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000660 linenum++;
661 /* Skip leading whitespace, empty and comment lines. */
662 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
663 ;
664 if (!*cp || *cp == '\n' || *cp == '#')
665 continue;
Damien Millerf6d9e222000-06-18 14:50:44 +1000666
Damien Miller0bc1bd82000-11-13 22:57:25 +1100667 if (key_read(found, &cp) == -1) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000668 /* no key? check if there are options for this key */
669 int quoted = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100670 debug2("user_key_allowed: check options: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000671 options = cp;
672 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
673 if (*cp == '\\' && cp[1] == '"')
674 cp++; /* Skip both */
675 else if (*cp == '"')
676 quoted = !quoted;
677 }
678 /* Skip remaining whitespace. */
679 for (; *cp == ' ' || *cp == '\t'; cp++)
680 ;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100681 if (key_read(found, &cp) == -1) {
682 debug2("user_key_allowed: advance: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000683 /* still no key? advance to next line*/
684 continue;
685 }
686 }
687 if (key_equal(found, key) &&
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000688 auth_parse_options(pw, options, file, linenum) == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000689 found_key = 1;
690 debug("matching key found: file %s, line %ld",
691 file, linenum);
692 break;
693 }
694 }
695 restore_uid();
696 fclose(f);
697 key_free(found);
Ben Lindstromb54873a2001-03-11 20:01:55 +0000698 if (!found_key)
699 debug2("key not found");
Damien Millereba71ba2000-04-29 23:57:08 +1000700 return found_key;
701}