blob: 29bbdf4fb24382918703bca11a96625cd194086c [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 Milleree116252001-12-21 12:42:34 +110026RCSID("$OpenBSD: auth2.c,v 1.75 2001/12/09 18:45:56 markus Exp $");
Damien Miller874d77b2000-10-14 16:23:11 +110027
Damien Millereba71ba2000-04-29 23:57:08 +100028#include <openssl/evp.h>
29
Ben Lindstrom226cfa02001-01-22 05:34:40 +000030#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100031#include "xmalloc.h"
32#include "rsa.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000033#include "sshpty.h"
Damien Millereba71ba2000-04-29 23:57:08 +100034#include "packet.h"
35#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000036#include "log.h"
Damien Millereba71ba2000-04-29 23:57:08 +100037#include "servconf.h"
38#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000039#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100040#include "bufaux.h"
Damien Millereba71ba2000-04-29 23:57:08 +100041#include "auth.h"
42#include "session.h"
43#include "dispatch.h"
Damien Millereba71ba2000-04-29 23:57:08 +100044#include "key.h"
Ben Lindstrom6d40c0f2001-01-29 09:02:24 +000045#include "cipher.h"
Damien Millereba71ba2000-04-29 23:57:08 +100046#include "kex.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000047#include "pathnames.h"
Damien Millereba71ba2000-04-29 23:57:08 +100048#include "uidswap.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100049#include "auth-options.h"
Ben Lindstrom086cf212001-03-05 05:56:40 +000050#include "misc.h"
Ben Lindstrom5eabda32001-04-12 23:34:34 +000051#include "hostfile.h"
52#include "canohost.h"
Ben Lindstrom551ea372001-06-05 18:56:16 +000053#include "match.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 +110060static Authctxt *x_authctxt = NULL;
61static int one = 1;
62
63typedef struct Authmethod Authmethod;
64struct Authmethod {
65 char *name;
66 int (*userauth)(Authctxt *authctxt);
67 int *enabled;
68};
69
Damien Millereba71ba2000-04-29 23:57:08 +100070/* protocol */
71
Ben Lindstrombba81212001-06-25 05:01:22 +000072static void input_service_request(int, int, void *);
73static void input_userauth_request(int, int, void *);
74static void protocol_error(int, int, void *);
Damien Millereba71ba2000-04-29 23:57:08 +100075
Damien Millereba71ba2000-04-29 23:57:08 +100076/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +000077static Authmethod *authmethod_lookup(const char *);
Ben Lindstrom79073822001-07-04 03:42:30 +000078static char *authmethods_get(void);
Ben Lindstrombba81212001-06-25 05:01:22 +000079static int user_key_allowed(struct passwd *, Key *);
80static int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
Damien Millereba71ba2000-04-29 23:57:08 +100081
Damien Miller874d77b2000-10-14 16:23:11 +110082/* auth */
Ben Lindstrombba81212001-06-25 05:01:22 +000083static void userauth_banner(void);
84static int userauth_none(Authctxt *);
85static int userauth_passwd(Authctxt *);
86static int userauth_pubkey(Authctxt *);
87static int userauth_hostbased(Authctxt *);
88static int userauth_kbdint(Authctxt *);
Damien Miller874d77b2000-10-14 16:23:11 +110089
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},
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000103 {"hostbased",
104 userauth_hostbased,
105 &options.hostbased_authentication},
Damien Miller874d77b2000-10-14 16:23:11 +1100106 {NULL, NULL, NULL}
Damien Millereba71ba2000-04-29 23:57:08 +1000107};
Damien Millereba71ba2000-04-29 23:57:08 +1000108
109/*
Damien Miller874d77b2000-10-14 16:23:11 +1100110 * loop until authctxt->success == TRUE
Damien Millereba71ba2000-04-29 23:57:08 +1000111 */
112
113void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000114do_authentication2(void)
Damien Millereba71ba2000-04-29 23:57:08 +1000115{
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000116 Authctxt *authctxt = authctxt_new();
117
Damien Miller874d77b2000-10-14 16:23:11 +1100118 x_authctxt = authctxt; /*XXX*/
119
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000120 /* challenge-response is implemented via keyboard interactive */
Ben Lindstrom551ea372001-06-05 18:56:16 +0000121 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000122 options.kbd_interactive_authentication = 1;
Damien Millerf8154422001-04-25 22:44:14 +1000123 if (options.pam_authentication_via_kbd_int)
124 options.kbd_interactive_authentication = 1;
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000125
Damien Millereba71ba2000-04-29 23:57:08 +1000126 dispatch_init(&protocol_error);
127 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
Damien Miller874d77b2000-10-14 16:23:11 +1100128 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000129 do_authenticated(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000130}
131
Ben Lindstrombba81212001-06-25 05:01:22 +0000132static void
Damien Miller62cee002000-09-23 17:15:56 +1100133protocol_error(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000134{
135 log("auth: protocol error: type %d plen %d", type, plen);
136 packet_start(SSH2_MSG_UNIMPLEMENTED);
137 packet_put_int(0);
138 packet_send();
139 packet_write_wait();
140}
141
Ben Lindstrombba81212001-06-25 05:01:22 +0000142static void
Damien Miller62cee002000-09-23 17:15:56 +1100143input_service_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000144{
Damien Miller874d77b2000-10-14 16:23:11 +1100145 Authctxt *authctxt = ctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000146 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000147 int accept = 0;
148 char *service = packet_get_string(&len);
149 packet_done();
150
Damien Miller874d77b2000-10-14 16:23:11 +1100151 if (authctxt == NULL)
152 fatal("input_service_request: no authctxt");
153
Damien Millereba71ba2000-04-29 23:57:08 +1000154 if (strcmp(service, "ssh-userauth") == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100155 if (!authctxt->success) {
Damien Millereba71ba2000-04-29 23:57:08 +1000156 accept = 1;
157 /* now we can handle user-auth requests */
158 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
159 }
160 }
161 /* XXX all other service requests are denied */
162
163 if (accept) {
164 packet_start(SSH2_MSG_SERVICE_ACCEPT);
165 packet_put_cstring(service);
166 packet_send();
167 packet_write_wait();
168 } else {
169 debug("bad service request %s", service);
170 packet_disconnect("bad service request %s", service);
171 }
172 xfree(service);
173}
174
Ben Lindstrombba81212001-06-25 05:01:22 +0000175static void
Damien Miller62cee002000-09-23 17:15:56 +1100176input_userauth_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000177{
Damien Miller874d77b2000-10-14 16:23:11 +1100178 Authctxt *authctxt = ctxt;
179 Authmethod *m = NULL;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000180 char *user, *service, *method, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000181 int authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000182
Damien Miller874d77b2000-10-14 16:23:11 +1100183 if (authctxt == NULL)
184 fatal("input_userauth_request: no authctxt");
Damien Millereba71ba2000-04-29 23:57:08 +1000185
Damien Miller874d77b2000-10-14 16:23:11 +1100186 user = packet_get_string(NULL);
187 service = packet_get_string(NULL);
188 method = packet_get_string(NULL);
189 debug("userauth-request for user %s service %s method %s", user, service, method);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000190 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
Damien Miller874d77b2000-10-14 16:23:11 +1100191
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000192 if ((style = strchr(user, ':')) != NULL)
193 *style++ = 0;
194
Kevin Stevesef4eea92001-02-05 12:42:17 +0000195 if (authctxt->attempt++ == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100196 /* setup auth context */
197 struct passwd *pw = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100198 pw = getpwnam(user);
199 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
200 authctxt->pw = pwcopy(pw);
201 authctxt->valid = 1;
202 debug2("input_userauth_request: setting up authctxt for %s", user);
203#ifdef USE_PAM
Damien Miller22e22bf2001-01-19 15:46:38 +1100204 start_pam(pw->pw_name);
Damien Miller874d77b2000-10-14 16:23:11 +1100205#endif
206 } else {
207 log("input_userauth_request: illegal user %s", user);
Damien Miller22e22bf2001-01-19 15:46:38 +1100208#ifdef USE_PAM
209 start_pam("NOUSER");
210#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100211 }
Ben Lindstromc1ba31f2001-02-15 03:14:11 +0000212 setproctitle("%s", pw ? user : "unknown");
Damien Miller874d77b2000-10-14 16:23:11 +1100213 authctxt->user = xstrdup(user);
214 authctxt->service = xstrdup(service);
Ben Lindstrom9d0c0662001-06-09 01:40:00 +0000215 authctxt->style = style ? xstrdup(style) : NULL;
216 } else if (strcmp(user, authctxt->user) != 0 ||
217 strcmp(service, authctxt->service) != 0) {
218 packet_disconnect("Change of username or service not allowed: "
219 "(%s,%s) -> (%s,%s)",
220 authctxt->user, authctxt->service, user, service);
Damien Millereba71ba2000-04-29 23:57:08 +1000221 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000222 /* reset state */
Damien Milleree116252001-12-21 12:42:34 +1100223 auth2_challenge_stop(authctxt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000224 authctxt->postponed = 0;
Damien Millerb70b61f2000-09-16 16:25:12 +1100225
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000226 /* try to authenticate user */
Damien Miller874d77b2000-10-14 16:23:11 +1100227 m = authmethod_lookup(method);
228 if (m != NULL) {
229 debug2("input_userauth_request: try method %s", method);
230 authenticated = m->userauth(authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100231 }
Damien Miller5d57e502001-03-30 10:48:31 +1000232 userauth_finish(authctxt, authenticated, method);
233
234 xfree(service);
235 xfree(user);
236 xfree(method);
237}
238
239void
240userauth_finish(Authctxt *authctxt, int authenticated, char *method)
241{
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000242 char *methods;
243
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000244 if (!authctxt->valid && authenticated)
245 fatal("INTERNAL ERROR: authenticated invalid user %s",
246 authctxt->user);
Damien Millerb70b61f2000-09-16 16:25:12 +1100247
Damien Miller874d77b2000-10-14 16:23:11 +1100248 /* Special handling for root */
Ben Lindstromd8a90212001-02-15 03:08:27 +0000249 if (authenticated && authctxt->pw->pw_uid == 0 &&
250 !auth_root_allowed(method))
Damien Millereba71ba2000-04-29 23:57:08 +1000251 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000252
253#ifdef USE_PAM
Damien Millerc83de6d2001-02-16 14:17:59 +1100254 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
255 NULL))
Damien Millerb70b61f2000-09-16 16:25:12 +1100256 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000257#endif /* USE_PAM */
258
Damien Miller874d77b2000-10-14 16:23:11 +1100259 /* Log before sending the reply */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000260 auth_log(authctxt, authenticated, method, " ssh2");
261
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000262 if (authctxt->postponed)
263 return;
264
265 /* XXX todo: check if multiple auth methods are needed */
266 if (authenticated == 1) {
267 /* turn off userauth */
268 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
269 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
270 packet_send();
271 packet_write_wait();
272 /* now we can break out */
273 authctxt->success = 1;
274 } else {
Damien Millere49d0962001-11-13 23:46:18 +1100275 if (authctxt->failures++ > AUTH_FAIL_MAX) {
276#ifdef WITH_AIXAUTHENTICATE
277 loginfailed(authctxt->user,
278 get_canonical_hostname(options.reverse_mapping_check),
279 "ssh");
280#endif /* WITH_AIXAUTHENTICATE */
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000281 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
Damien Millere49d0962001-11-13 23:46:18 +1100282 }
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000283 methods = authmethods_get();
284 packet_start(SSH2_MSG_USERAUTH_FAILURE);
285 packet_put_cstring(methods);
286 packet_put_char(0); /* XXX partial success, unused */
287 packet_send();
288 packet_write_wait();
289 xfree(methods);
290 }
Damien Miller874d77b2000-10-14 16:23:11 +1100291}
292
Ben Lindstrombba81212001-06-25 05:01:22 +0000293static void
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000294userauth_banner(void)
295{
296 struct stat st;
297 char *banner = NULL;
298 off_t len, n;
299 int fd;
300
301 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
302 return;
Ben Lindstrom0cae0402001-04-04 23:47:52 +0000303 if ((fd = open(options.banner, O_RDONLY)) < 0)
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000304 return;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000305 if (fstat(fd, &st) < 0)
306 goto done;
307 len = st.st_size;
308 banner = xmalloc(len + 1);
309 if ((n = read(fd, banner, len)) < 0)
310 goto done;
311 banner[n] = '\0';
312 packet_start(SSH2_MSG_USERAUTH_BANNER);
313 packet_put_cstring(banner);
314 packet_put_cstring(""); /* language, unused */
315 packet_send();
316 debug("userauth_banner: sent");
317done:
318 if (banner)
319 xfree(banner);
320 close(fd);
321 return;
322}
Damien Miller874d77b2000-10-14 16:23:11 +1100323
Ben Lindstrombba81212001-06-25 05:01:22 +0000324static int
Damien Miller874d77b2000-10-14 16:23:11 +1100325userauth_none(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000326{
Damien Miller874d77b2000-10-14 16:23:11 +1100327 /* disable method "none", only allowed one time */
328 Authmethod *m = authmethod_lookup("none");
329 if (m != NULL)
330 m->enabled = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000331 packet_done();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000332 userauth_banner();
Damien Millerb8c656e2000-06-28 15:22:41 +1000333
Damien Miller874d77b2000-10-14 16:23:11 +1100334 if (authctxt->valid == 0)
335 return(0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000336
Damien Miller874d77b2000-10-14 16:23:11 +1100337#ifdef HAVE_CYGWIN
338 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
339 return(0);
340#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000341#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100342 return auth_pam_password(authctxt->pw, "");
Damien Millerb8c656e2000-06-28 15:22:41 +1000343#elif defined(HAVE_OSF_SIA)
Damien Miller92ddb7d2001-02-14 01:25:23 +1100344 return 0;
Damien Millerb8c656e2000-06-28 15:22:41 +1000345#else /* !HAVE_OSF_SIA && !USE_PAM */
Damien Miller60396b02001-02-18 17:01:00 +1100346 return auth_password(authctxt, "");
Damien Millereba71ba2000-04-29 23:57:08 +1000347#endif /* USE_PAM */
348}
Damien Miller874d77b2000-10-14 16:23:11 +1100349
Ben Lindstrombba81212001-06-25 05:01:22 +0000350static int
Damien Miller874d77b2000-10-14 16:23:11 +1100351userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000352{
353 char *password;
354 int authenticated = 0;
355 int change;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000356 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000357 change = packet_get_char();
358 if (change)
359 log("password change not supported");
360 password = packet_get_string(&len);
361 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100362 if (authctxt->valid &&
363#ifdef HAVE_CYGWIN
364 check_nt_auth(1, authctxt->pw->pw_uid) &&
365#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000366#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100367 auth_pam_password(authctxt->pw, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000368#elif defined(HAVE_OSF_SIA)
Damien Miller92ddb7d2001-02-14 01:25:23 +1100369 auth_sia_password(authctxt->user, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000370#else /* !USE_PAM && !HAVE_OSF_SIA */
Damien Miller60396b02001-02-18 17:01:00 +1100371 auth_password(authctxt, password) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000372#endif /* USE_PAM */
373 authenticated = 1;
374 memset(password, 0, len);
375 xfree(password);
376 return authenticated;
377}
Damien Miller874d77b2000-10-14 16:23:11 +1100378
Ben Lindstrombba81212001-06-25 05:01:22 +0000379static int
Damien Miller874d77b2000-10-14 16:23:11 +1100380userauth_kbdint(Authctxt *authctxt)
381{
382 int authenticated = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000383 char *lang, *devs;
Damien Miller874d77b2000-10-14 16:23:11 +1100384
385 lang = packet_get_string(NULL);
386 devs = packet_get_string(NULL);
387 packet_done();
388
Ben Lindstrom551ea372001-06-05 18:56:16 +0000389 debug("keyboard-interactive devs %s", devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000390
Ben Lindstrom551ea372001-06-05 18:56:16 +0000391 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000392 authenticated = auth2_challenge(authctxt, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000393
Damien Millerb8481582000-12-03 11:51:51 +1100394#ifdef USE_PAM
Damien Millerf8154422001-04-25 22:44:14 +1000395 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
Damien Millerb8481582000-12-03 11:51:51 +1100396 authenticated = auth2_pam(authctxt);
397#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100398 xfree(devs);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000399 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +1100400#ifdef HAVE_CYGWIN
401 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
402 return(0);
403#endif
404 return authenticated;
405}
406
Ben Lindstrombba81212001-06-25 05:01:22 +0000407static int
Damien Miller874d77b2000-10-14 16:23:11 +1100408userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000409{
410 Buffer b;
411 Key *key;
412 char *pkalg, *pkblob, *sig;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000413 u_int alen, blen, slen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100414 int have_sig, pktype;
Damien Millereba71ba2000-04-29 23:57:08 +1000415 int authenticated = 0;
416
Damien Miller874d77b2000-10-14 16:23:11 +1100417 if (!authctxt->valid) {
418 debug2("userauth_pubkey: disabled because of invalid user");
Damien Millereba71ba2000-04-29 23:57:08 +1000419 return 0;
420 }
421 have_sig = packet_get_char();
Ben Lindstromd121f612000-12-03 17:00:47 +0000422 if (datafellows & SSH_BUG_PKAUTH) {
423 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
424 /* no explicit pkalg given */
425 pkblob = packet_get_string(&blen);
426 buffer_init(&b);
427 buffer_append(&b, pkblob, blen);
428 /* so we have to extract the pkalg from the pkblob */
429 pkalg = buffer_get_string(&b, &alen);
430 buffer_free(&b);
431 } else {
432 pkalg = packet_get_string(&alen);
433 pkblob = packet_get_string(&blen);
434 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100435 pktype = key_type_from_name(pkalg);
436 if (pktype == KEY_UNSPEC) {
Ben Lindstromd121f612000-12-03 17:00:47 +0000437 /* this is perfectly legal */
438 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
Damien Miller874d77b2000-10-14 16:23:11 +1100439 xfree(pkalg);
Ben Lindstromd121f612000-12-03 17:00:47 +0000440 xfree(pkblob);
Damien Millereba71ba2000-04-29 23:57:08 +1000441 return 0;
442 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100443 key = key_from_blob(pkblob, blen);
Damien Millereba71ba2000-04-29 23:57:08 +1000444 if (key != NULL) {
445 if (have_sig) {
446 sig = packet_get_string(&slen);
447 packet_done();
448 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100449 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000450 buffer_append(&b, session_id2, session_id2_len);
Damien Miller50a41ed2000-10-16 12:14:42 +1100451 } else {
452 buffer_put_string(&b, session_id2, session_id2_len);
Damien Miller6536c7d2000-06-22 21:32:31 +1000453 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000454 /* reconstruct packet */
Damien Millereba71ba2000-04-29 23:57:08 +1000455 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller874d77b2000-10-14 16:23:11 +1100456 buffer_put_cstring(&b, authctxt->user);
Damien Millerf6d9e222000-06-18 14:50:44 +1000457 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000458 datafellows & SSH_BUG_PKSERVICE ?
Damien Millerf6d9e222000-06-18 14:50:44 +1000459 "ssh-userauth" :
Damien Miller874d77b2000-10-14 16:23:11 +1100460 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000461 if (datafellows & SSH_BUG_PKAUTH) {
462 buffer_put_char(&b, have_sig);
463 } else {
464 buffer_put_cstring(&b, "publickey");
465 buffer_put_char(&b, have_sig);
Ben Lindstrom3f364962001-04-19 20:50:07 +0000466 buffer_put_cstring(&b, pkalg);
Ben Lindstromd121f612000-12-03 17:00:47 +0000467 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000468 buffer_put_string(&b, pkblob, blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100469#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000470 buffer_dump(&b);
471#endif
472 /* test for correct signature */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100473 if (user_key_allowed(authctxt->pw, key) &&
474 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000475 authenticated = 1;
476 buffer_clear(&b);
477 xfree(sig);
478 } else {
Damien Miller874d77b2000-10-14 16:23:11 +1100479 debug("test whether pkalg/pkblob are acceptable");
Damien Millereba71ba2000-04-29 23:57:08 +1000480 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100481
Damien Millereba71ba2000-04-29 23:57:08 +1000482 /* XXX fake reply and always send PK_OK ? */
Damien Millere247cc42000-05-07 12:03:14 +1000483 /*
484 * XXX this allows testing whether a user is allowed
485 * to login: if you happen to have a valid pubkey this
486 * message is sent. the message is NEVER sent at all
487 * if a user is not allowed to login. is this an
488 * issue? -markus
489 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100490 if (user_key_allowed(authctxt->pw, key)) {
Damien Millereba71ba2000-04-29 23:57:08 +1000491 packet_start(SSH2_MSG_USERAUTH_PK_OK);
492 packet_put_string(pkalg, alen);
493 packet_put_string(pkblob, blen);
494 packet_send();
495 packet_write_wait();
Kevin Steves4abe4de2001-02-08 19:16:32 +0000496 authctxt->postponed = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000497 }
498 }
Damien Miller874d77b2000-10-14 16:23:11 +1100499 if (authenticated != 1)
500 auth_clear_options();
Damien Millereba71ba2000-04-29 23:57:08 +1000501 key_free(key);
502 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100503 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
Damien Millereba71ba2000-04-29 23:57:08 +1000504 xfree(pkalg);
505 xfree(pkblob);
Damien Miller874d77b2000-10-14 16:23:11 +1100506#ifdef HAVE_CYGWIN
507 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
508 return(0);
509#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000510 return authenticated;
511}
512
Ben Lindstrombba81212001-06-25 05:01:22 +0000513static int
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000514userauth_hostbased(Authctxt *authctxt)
515{
516 Buffer b;
517 Key *key;
Ben Lindstrom671388f2001-04-19 20:40:45 +0000518 char *pkalg, *pkblob, *sig, *cuser, *chost, *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000519 u_int alen, blen, slen;
520 int pktype;
521 int authenticated = 0;
522
523 if (!authctxt->valid) {
524 debug2("userauth_hostbased: disabled because of invalid user");
525 return 0;
526 }
527 pkalg = packet_get_string(&alen);
528 pkblob = packet_get_string(&blen);
529 chost = packet_get_string(NULL);
530 cuser = packet_get_string(NULL);
531 sig = packet_get_string(&slen);
532
533 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
534 cuser, chost, pkalg, slen);
535#ifdef DEBUG_PK
536 debug("signature:");
537 buffer_init(&b);
538 buffer_append(&b, sig, slen);
539 buffer_dump(&b);
540 buffer_free(&b);
541#endif
542 pktype = key_type_from_name(pkalg);
543 if (pktype == KEY_UNSPEC) {
544 /* this is perfectly legal */
545 log("userauth_hostbased: unsupported "
546 "public key algorithm: %s", pkalg);
547 goto done;
548 }
549 key = key_from_blob(pkblob, blen);
550 if (key == NULL) {
551 debug("userauth_hostbased: cannot decode key: %s", pkalg);
552 goto done;
553 }
Ben Lindstrom671388f2001-04-19 20:40:45 +0000554 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
555 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000556 buffer_init(&b);
Ben Lindstrom671388f2001-04-19 20:40:45 +0000557 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000558 /* reconstruct packet */
559 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
560 buffer_put_cstring(&b, authctxt->user);
Ben Lindstrom671388f2001-04-19 20:40:45 +0000561 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000562 buffer_put_cstring(&b, "hostbased");
563 buffer_put_string(&b, pkalg, alen);
564 buffer_put_string(&b, pkblob, blen);
565 buffer_put_cstring(&b, chost);
566 buffer_put_cstring(&b, cuser);
567#ifdef DEBUG_PK
568 buffer_dump(&b);
569#endif
570 /* test for allowed key and correct signature */
571 if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
572 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
573 authenticated = 1;
574
575 buffer_clear(&b);
576 key_free(key);
577
578done:
579 debug2("userauth_hostbased: authenticated %d", authenticated);
580 xfree(pkalg);
581 xfree(pkblob);
582 xfree(cuser);
583 xfree(chost);
584 xfree(sig);
585 return authenticated;
586}
587
Damien Miller874d77b2000-10-14 16:23:11 +1100588/* get current user */
Damien Millereba71ba2000-04-29 23:57:08 +1000589
590struct passwd*
591auth_get_user(void)
592{
Damien Miller874d77b2000-10-14 16:23:11 +1100593 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000594}
595
Damien Miller874d77b2000-10-14 16:23:11 +1100596#define DELIM ","
Damien Millereba71ba2000-04-29 23:57:08 +1000597
Ben Lindstrom79073822001-07-04 03:42:30 +0000598static char *
Damien Miller874d77b2000-10-14 16:23:11 +1100599authmethods_get(void)
600{
601 Authmethod *method = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000602 u_int size = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100603 char *list;
604
605 for (method = authmethods; method->name != NULL; method++) {
606 if (strcmp(method->name, "none") == 0)
607 continue;
608 if (method->enabled != NULL && *(method->enabled) != 0) {
609 if (size != 0)
610 size += strlen(DELIM);
611 size += strlen(method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000612 }
613 }
Damien Miller874d77b2000-10-14 16:23:11 +1100614 size++; /* trailing '\0' */
615 list = xmalloc(size);
616 list[0] = '\0';
617
618 for (method = authmethods; method->name != NULL; method++) {
619 if (strcmp(method->name, "none") == 0)
620 continue;
621 if (method->enabled != NULL && *(method->enabled) != 0) {
622 if (list[0] != '\0')
623 strlcat(list, DELIM, size);
624 strlcat(list, method->name, size);
625 }
626 }
627 return list;
628}
629
Ben Lindstrombba81212001-06-25 05:01:22 +0000630static Authmethod *
Damien Miller874d77b2000-10-14 16:23:11 +1100631authmethod_lookup(const char *name)
632{
633 Authmethod *method = NULL;
634 if (name != NULL)
635 for (method = authmethods; method->name != NULL; method++)
636 if (method->enabled != NULL &&
637 *(method->enabled) != 0 &&
638 strcmp(name, method->name) == 0)
639 return method;
640 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
641 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000642}
643
644/* return 1 if user allows given key */
Ben Lindstrombba81212001-06-25 05:01:22 +0000645static int
Ben Lindstromf96704d2001-06-25 04:17:12 +0000646user_key_allowed2(struct passwd *pw, Key *key, char *file)
Damien Millereba71ba2000-04-29 23:57:08 +1000647{
Ben Lindstromf96704d2001-06-25 04:17:12 +0000648 char line[8192];
Damien Millereba71ba2000-04-29 23:57:08 +1000649 int found_key = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000650 FILE *f;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000651 u_long linenum = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000652 struct stat st;
653 Key *found;
654
Damien Miller874d77b2000-10-14 16:23:11 +1100655 if (pw == NULL)
656 return 0;
657
Damien Millereba71ba2000-04-29 23:57:08 +1000658 /* Temporarily use the user's uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000659 temporarily_use_uid(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000660
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000661 debug("trying public key file %s", file);
Damien Millereba71ba2000-04-29 23:57:08 +1000662
663 /* Fail quietly if file does not exist */
664 if (stat(file, &st) < 0) {
665 /* Restore the privileged uid. */
666 restore_uid();
667 return 0;
668 }
669 /* Open the file containing the authorized keys. */
670 f = fopen(file, "r");
671 if (!f) {
672 /* Restore the privileged uid. */
673 restore_uid();
674 return 0;
675 }
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000676 if (options.strict_modes &&
Ben Lindstrom90279d82001-07-04 03:56:56 +0000677 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000678 fclose(f);
679 log("Authentication refused: %s", line);
680 restore_uid();
681 return 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000682 }
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000683
Damien Millereba71ba2000-04-29 23:57:08 +1000684 found_key = 0;
Damien Millerd3444942000-09-30 14:20:03 +1100685 found = key_new(key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000686
687 while (fgets(line, sizeof(line), f)) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000688 char *cp, *options = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000689 linenum++;
690 /* Skip leading whitespace, empty and comment lines. */
691 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
692 ;
693 if (!*cp || *cp == '\n' || *cp == '#')
694 continue;
Damien Millerf6d9e222000-06-18 14:50:44 +1000695
Ben Lindstrom1bc3bdb2001-09-20 23:11:26 +0000696 if (key_read(found, &cp) != 1) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000697 /* no key? check if there are options for this key */
698 int quoted = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100699 debug2("user_key_allowed: check options: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000700 options = cp;
701 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
702 if (*cp == '\\' && cp[1] == '"')
703 cp++; /* Skip both */
704 else if (*cp == '"')
705 quoted = !quoted;
706 }
707 /* Skip remaining whitespace. */
708 for (; *cp == ' ' || *cp == '\t'; cp++)
709 ;
Ben Lindstrom1bc3bdb2001-09-20 23:11:26 +0000710 if (key_read(found, &cp) != 1) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100711 debug2("user_key_allowed: advance: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000712 /* still no key? advance to next line*/
713 continue;
714 }
715 }
716 if (key_equal(found, key) &&
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000717 auth_parse_options(pw, options, file, linenum) == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000718 found_key = 1;
Ben Lindstrom940fb862001-08-06 21:01:49 +0000719 debug("matching key found: file %s, line %lu",
Damien Millereba71ba2000-04-29 23:57:08 +1000720 file, linenum);
721 break;
722 }
723 }
724 restore_uid();
725 fclose(f);
726 key_free(found);
Ben Lindstromb54873a2001-03-11 20:01:55 +0000727 if (!found_key)
728 debug2("key not found");
Damien Millereba71ba2000-04-29 23:57:08 +1000729 return found_key;
730}
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000731
Ben Lindstromf96704d2001-06-25 04:17:12 +0000732/* check whether given key is in .ssh/authorized_keys* */
Ben Lindstrombba81212001-06-25 05:01:22 +0000733static int
Ben Lindstromf96704d2001-06-25 04:17:12 +0000734user_key_allowed(struct passwd *pw, Key *key)
735{
736 int success;
737 char *file;
738
739 file = authorized_keys_file(pw);
740 success = user_key_allowed2(pw, key, file);
741 xfree(file);
742 if (success)
743 return success;
744
745 /* try suffix "2" for backward compat, too */
746 file = authorized_keys_file2(pw);
747 success = user_key_allowed2(pw, key, file);
748 xfree(file);
749 return success;
750}
751
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000752/* return 1 if given hostkey is allowed */
Ben Lindstrombba81212001-06-25 05:01:22 +0000753static int
Ben Lindstrom4aa603c2001-04-19 20:38:06 +0000754hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000755 Key *key)
756{
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000757 const char *resolvedname, *ipaddr, *lookup;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000758 HostStatus host_status;
759 int len;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000760
761 resolvedname = get_canonical_hostname(options.reverse_mapping_check);
762 ipaddr = get_remote_ipaddr();
763
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000764 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
765 chost, resolvedname, ipaddr);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000766
767 if (options.hostbased_uses_name_from_packet_only) {
768 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
769 return 0;
770 lookup = chost;
771 } else {
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000772 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
773 debug2("stripping trailing dot from chost %s", chost);
774 chost[len - 1] = '\0';
775 }
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000776 if (strcasecmp(resolvedname, chost) != 0)
777 log("userauth_hostbased mismatch: "
778 "client sends %s, but we resolve %s to %s",
779 chost, ipaddr, resolvedname);
780 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
781 return 0;
782 lookup = resolvedname;
783 }
784 debug2("userauth_hostbased: access allowed by auth_rhosts2");
785
Ben Lindstrom83647ce2001-06-25 04:30:16 +0000786 host_status = check_key_in_hostfiles(pw, key, lookup,
787 _PATH_SSH_SYSTEM_HOSTFILE,
Ben Lindstroma4789ef2001-06-25 04:40:49 +0000788 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000789
Ben Lindstrom83647ce2001-06-25 04:30:16 +0000790 /* backward compat if no key has been found. */
791 if (host_status == HOST_NEW)
792 host_status = check_key_in_hostfiles(pw, key, lookup,
793 _PATH_SSH_SYSTEM_HOSTFILE2,
Ben Lindstroma4789ef2001-06-25 04:40:49 +0000794 options.ignore_user_known_hosts ? NULL :
795 _PATH_SSH_USER_HOSTFILE2);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000796
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000797 return (host_status == HOST_OK);
798}
Ben Lindstrom83647ce2001-06-25 04:30:16 +0000799