blob: c0ed362139df5e831c8d820ec79c9870af4557ac [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 Lindstrombdfb4df2001-10-03 17:12:43 +000026RCSID("$OpenBSD: auth2.c,v 1.71 2001/09/27 15:31:17 markus Exp $");
Damien Miller874d77b2000-10-14 16:23:11 +110027
Damien Millereba71ba2000-04-29 23:57:08 +100028#include <openssl/evp.h>
29
Ben Lindstrom226cfa02001-01-22 05:34:40 +000030#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100031#include "xmalloc.h"
32#include "rsa.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000033#include "sshpty.h"
Damien Millereba71ba2000-04-29 23:57:08 +100034#include "packet.h"
35#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000036#include "log.h"
Damien Millereba71ba2000-04-29 23:57:08 +100037#include "servconf.h"
38#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000039#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100040#include "bufaux.h"
Damien Millereba71ba2000-04-29 23:57:08 +100041#include "auth.h"
42#include "session.h"
43#include "dispatch.h"
Damien Millereba71ba2000-04-29 23:57:08 +100044#include "key.h"
Ben Lindstrom6d40c0f2001-01-29 09:02:24 +000045#include "cipher.h"
Damien Millereba71ba2000-04-29 23:57:08 +100046#include "kex.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000047#include "pathnames.h"
Damien Millereba71ba2000-04-29 23:57:08 +100048#include "uidswap.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100049#include "auth-options.h"
Ben Lindstrom086cf212001-03-05 05:56:40 +000050#include "misc.h"
Ben Lindstrom5eabda32001-04-12 23:34:34 +000051#include "hostfile.h"
52#include "canohost.h"
53#include "tildexpand.h"
Ben Lindstrom551ea372001-06-05 18:56:16 +000054#include "match.h"
Damien Millereba71ba2000-04-29 23:57:08 +100055
56/* import */
57extern ServerOptions options;
Ben Lindstrom46c16222000-12-22 01:43:59 +000058extern u_char *session_id2;
Damien Millereba71ba2000-04-29 23:57:08 +100059extern int session_id2_len;
60
Damien Miller874d77b2000-10-14 16:23:11 +110061#ifdef WITH_AIXAUTHENTICATE
62extern char *aixloginmsg;
63#endif
Damien Miller874d77b2000-10-14 16:23:11 +110064
65static Authctxt *x_authctxt = NULL;
66static int one = 1;
67
68typedef struct Authmethod Authmethod;
69struct Authmethod {
70 char *name;
71 int (*userauth)(Authctxt *authctxt);
72 int *enabled;
73};
74
Damien Millereba71ba2000-04-29 23:57:08 +100075/* protocol */
76
Ben Lindstrombba81212001-06-25 05:01:22 +000077static void input_service_request(int, int, void *);
78static void input_userauth_request(int, int, void *);
79static void protocol_error(int, int, void *);
Damien Millereba71ba2000-04-29 23:57:08 +100080
Damien Millereba71ba2000-04-29 23:57:08 +100081/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +000082static Authmethod *authmethod_lookup(const char *);
Ben Lindstrom79073822001-07-04 03:42:30 +000083static char *authmethods_get(void);
Ben Lindstrombba81212001-06-25 05:01:22 +000084static int user_key_allowed(struct passwd *, Key *);
85static int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
Damien Millereba71ba2000-04-29 23:57:08 +100086
Damien Miller874d77b2000-10-14 16:23:11 +110087/* auth */
Ben Lindstrombba81212001-06-25 05:01:22 +000088static void userauth_banner(void);
89static int userauth_none(Authctxt *);
90static int userauth_passwd(Authctxt *);
91static int userauth_pubkey(Authctxt *);
92static int userauth_hostbased(Authctxt *);
93static int userauth_kbdint(Authctxt *);
Damien Miller874d77b2000-10-14 16:23:11 +110094
95Authmethod authmethods[] = {
96 {"none",
97 userauth_none,
98 &one},
99 {"publickey",
100 userauth_pubkey,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100101 &options.pubkey_authentication},
Damien Miller874d77b2000-10-14 16:23:11 +1100102 {"password",
103 userauth_passwd,
104 &options.password_authentication},
Ben Lindstromd1f20ec2001-02-10 21:31:53 +0000105 {"keyboard-interactive",
106 userauth_kbdint,
107 &options.kbd_interactive_authentication},
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000108 {"hostbased",
109 userauth_hostbased,
110 &options.hostbased_authentication},
Damien Miller874d77b2000-10-14 16:23:11 +1100111 {NULL, NULL, NULL}
Damien Millereba71ba2000-04-29 23:57:08 +1000112};
Damien Millereba71ba2000-04-29 23:57:08 +1000113
114/*
Damien Miller874d77b2000-10-14 16:23:11 +1100115 * loop until authctxt->success == TRUE
Damien Millereba71ba2000-04-29 23:57:08 +1000116 */
117
118void
119do_authentication2()
120{
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000121 Authctxt *authctxt = authctxt_new();
122
Damien Miller874d77b2000-10-14 16:23:11 +1100123 x_authctxt = authctxt; /*XXX*/
124
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000125 /* challenge-response is implemented via keyboard interactive */
Ben Lindstrom551ea372001-06-05 18:56:16 +0000126 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000127 options.kbd_interactive_authentication = 1;
Damien Millerf8154422001-04-25 22:44:14 +1000128 if (options.pam_authentication_via_kbd_int)
129 options.kbd_interactive_authentication = 1;
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000130
Damien Millereba71ba2000-04-29 23:57:08 +1000131 dispatch_init(&protocol_error);
132 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
Damien Miller874d77b2000-10-14 16:23:11 +1100133 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000134 do_authenticated(authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000135}
136
Ben Lindstrombba81212001-06-25 05:01:22 +0000137static void
Damien Miller62cee002000-09-23 17:15:56 +1100138protocol_error(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000139{
140 log("auth: protocol error: type %d plen %d", type, plen);
141 packet_start(SSH2_MSG_UNIMPLEMENTED);
142 packet_put_int(0);
143 packet_send();
144 packet_write_wait();
145}
146
Ben Lindstrombba81212001-06-25 05:01:22 +0000147static void
Damien Miller62cee002000-09-23 17:15:56 +1100148input_service_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000149{
Damien Miller874d77b2000-10-14 16:23:11 +1100150 Authctxt *authctxt = ctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000151 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000152 int accept = 0;
153 char *service = packet_get_string(&len);
154 packet_done();
155
Damien Miller874d77b2000-10-14 16:23:11 +1100156 if (authctxt == NULL)
157 fatal("input_service_request: no authctxt");
158
Damien Millereba71ba2000-04-29 23:57:08 +1000159 if (strcmp(service, "ssh-userauth") == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100160 if (!authctxt->success) {
Damien Millereba71ba2000-04-29 23:57:08 +1000161 accept = 1;
162 /* now we can handle user-auth requests */
163 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
164 }
165 }
166 /* XXX all other service requests are denied */
167
168 if (accept) {
169 packet_start(SSH2_MSG_SERVICE_ACCEPT);
170 packet_put_cstring(service);
171 packet_send();
172 packet_write_wait();
173 } else {
174 debug("bad service request %s", service);
175 packet_disconnect("bad service request %s", service);
176 }
177 xfree(service);
178}
179
Ben Lindstrombba81212001-06-25 05:01:22 +0000180static void
Damien Miller62cee002000-09-23 17:15:56 +1100181input_userauth_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000182{
Damien Miller874d77b2000-10-14 16:23:11 +1100183 Authctxt *authctxt = ctxt;
184 Authmethod *m = NULL;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000185 char *user, *service, *method, *style = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000186 int authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000187
Damien Miller874d77b2000-10-14 16:23:11 +1100188 if (authctxt == NULL)
189 fatal("input_userauth_request: no authctxt");
Damien Millereba71ba2000-04-29 23:57:08 +1000190
Damien Miller874d77b2000-10-14 16:23:11 +1100191 user = packet_get_string(NULL);
192 service = packet_get_string(NULL);
193 method = packet_get_string(NULL);
194 debug("userauth-request for user %s service %s method %s", user, service, method);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000195 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
Damien Miller874d77b2000-10-14 16:23:11 +1100196
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000197 if ((style = strchr(user, ':')) != NULL)
198 *style++ = 0;
199
Kevin Stevesef4eea92001-02-05 12:42:17 +0000200 if (authctxt->attempt++ == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100201 /* setup auth context */
202 struct passwd *pw = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100203 pw = getpwnam(user);
204 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
205 authctxt->pw = pwcopy(pw);
206 authctxt->valid = 1;
207 debug2("input_userauth_request: setting up authctxt for %s", user);
208#ifdef USE_PAM
Damien Miller22e22bf2001-01-19 15:46:38 +1100209 start_pam(pw->pw_name);
Damien Miller874d77b2000-10-14 16:23:11 +1100210#endif
211 } else {
212 log("input_userauth_request: illegal user %s", user);
Damien Miller22e22bf2001-01-19 15:46:38 +1100213#ifdef USE_PAM
214 start_pam("NOUSER");
215#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100216 }
Ben Lindstromc1ba31f2001-02-15 03:14:11 +0000217 setproctitle("%s", pw ? user : "unknown");
Damien Miller874d77b2000-10-14 16:23:11 +1100218 authctxt->user = xstrdup(user);
219 authctxt->service = xstrdup(service);
Ben Lindstrom9d0c0662001-06-09 01:40:00 +0000220 authctxt->style = style ? xstrdup(style) : NULL;
221 } else if (strcmp(user, authctxt->user) != 0 ||
222 strcmp(service, authctxt->service) != 0) {
223 packet_disconnect("Change of username or service not allowed: "
224 "(%s,%s) -> (%s,%s)",
225 authctxt->user, authctxt->service, user, service);
Damien Millereba71ba2000-04-29 23:57:08 +1000226 }
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000227 /* reset state */
228 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
229 authctxt->postponed = 0;
Damien Miller60396b02001-02-18 17:01:00 +1100230#ifdef BSD_AUTH
231 if (authctxt->as) {
232 auth_close(authctxt->as);
233 authctxt->as = NULL;
234 }
235#endif
Damien Millerb70b61f2000-09-16 16:25:12 +1100236
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000237 /* try to authenticate user */
Damien Miller874d77b2000-10-14 16:23:11 +1100238 m = authmethod_lookup(method);
239 if (m != NULL) {
240 debug2("input_userauth_request: try method %s", method);
241 authenticated = m->userauth(authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100242 }
Damien Miller5d57e502001-03-30 10:48:31 +1000243 userauth_finish(authctxt, authenticated, method);
244
245 xfree(service);
246 xfree(user);
247 xfree(method);
248}
249
250void
251userauth_finish(Authctxt *authctxt, int authenticated, char *method)
252{
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000253 char *methods;
254
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000255 if (!authctxt->valid && authenticated)
256 fatal("INTERNAL ERROR: authenticated invalid user %s",
257 authctxt->user);
Damien Millerb70b61f2000-09-16 16:25:12 +1100258
Damien Miller874d77b2000-10-14 16:23:11 +1100259 /* Special handling for root */
Ben Lindstromd8a90212001-02-15 03:08:27 +0000260 if (authenticated && authctxt->pw->pw_uid == 0 &&
261 !auth_root_allowed(method))
Damien Millereba71ba2000-04-29 23:57:08 +1000262 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000263
264#ifdef USE_PAM
Damien Millerc83de6d2001-02-16 14:17:59 +1100265 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
266 NULL))
Damien Millerb70b61f2000-09-16 16:25:12 +1100267 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000268#endif /* USE_PAM */
269
Damien Miller874d77b2000-10-14 16:23:11 +1100270 /* Log before sending the reply */
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000271 auth_log(authctxt, authenticated, method, " ssh2");
272
Ben Lindstromcd4349f2001-06-09 00:23:17 +0000273 if (authctxt->postponed)
274 return;
275
276 /* XXX todo: check if multiple auth methods are needed */
277 if (authenticated == 1) {
278 /* turn off userauth */
279 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
280 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
281 packet_send();
282 packet_write_wait();
283 /* now we can break out */
284 authctxt->success = 1;
285 } else {
286 if (authctxt->failures++ > AUTH_FAIL_MAX)
287 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
288 methods = authmethods_get();
289 packet_start(SSH2_MSG_USERAUTH_FAILURE);
290 packet_put_cstring(methods);
291 packet_put_char(0); /* XXX partial success, unused */
292 packet_send();
293 packet_write_wait();
294 xfree(methods);
295 }
Damien Miller874d77b2000-10-14 16:23:11 +1100296}
297
Ben Lindstrombba81212001-06-25 05:01:22 +0000298static void
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000299userauth_banner(void)
300{
301 struct stat st;
302 char *banner = NULL;
303 off_t len, n;
304 int fd;
305
306 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
307 return;
Ben Lindstrom0cae0402001-04-04 23:47:52 +0000308 if ((fd = open(options.banner, O_RDONLY)) < 0)
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000309 return;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000310 if (fstat(fd, &st) < 0)
311 goto done;
312 len = st.st_size;
313 banner = xmalloc(len + 1);
314 if ((n = read(fd, banner, len)) < 0)
315 goto done;
316 banner[n] = '\0';
317 packet_start(SSH2_MSG_USERAUTH_BANNER);
318 packet_put_cstring(banner);
319 packet_put_cstring(""); /* language, unused */
320 packet_send();
321 debug("userauth_banner: sent");
322done:
323 if (banner)
324 xfree(banner);
325 close(fd);
326 return;
327}
Damien Miller874d77b2000-10-14 16:23:11 +1100328
Ben Lindstrombba81212001-06-25 05:01:22 +0000329static int
Damien Miller874d77b2000-10-14 16:23:11 +1100330userauth_none(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000331{
Damien Miller874d77b2000-10-14 16:23:11 +1100332 /* disable method "none", only allowed one time */
333 Authmethod *m = authmethod_lookup("none");
334 if (m != NULL)
335 m->enabled = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000336 packet_done();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000337 userauth_banner();
Damien Millerb8c656e2000-06-28 15:22:41 +1000338
Damien Miller874d77b2000-10-14 16:23:11 +1100339 if (authctxt->valid == 0)
340 return(0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000341
Damien Miller874d77b2000-10-14 16:23:11 +1100342#ifdef HAVE_CYGWIN
343 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
344 return(0);
345#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000346#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100347 return auth_pam_password(authctxt->pw, "");
Damien Millerb8c656e2000-06-28 15:22:41 +1000348#elif defined(HAVE_OSF_SIA)
Damien Miller92ddb7d2001-02-14 01:25:23 +1100349 return 0;
Damien Millerb8c656e2000-06-28 15:22:41 +1000350#else /* !HAVE_OSF_SIA && !USE_PAM */
Damien Miller60396b02001-02-18 17:01:00 +1100351 return auth_password(authctxt, "");
Damien Millereba71ba2000-04-29 23:57:08 +1000352#endif /* USE_PAM */
353}
Damien Miller874d77b2000-10-14 16:23:11 +1100354
Ben Lindstrombba81212001-06-25 05:01:22 +0000355static int
Damien Miller874d77b2000-10-14 16:23:11 +1100356userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000357{
358 char *password;
359 int authenticated = 0;
360 int change;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000361 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000362 change = packet_get_char();
363 if (change)
364 log("password change not supported");
365 password = packet_get_string(&len);
366 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100367 if (authctxt->valid &&
368#ifdef HAVE_CYGWIN
369 check_nt_auth(1, authctxt->pw->pw_uid) &&
370#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000371#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100372 auth_pam_password(authctxt->pw, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000373#elif defined(HAVE_OSF_SIA)
Damien Miller92ddb7d2001-02-14 01:25:23 +1100374 auth_sia_password(authctxt->user, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000375#else /* !USE_PAM && !HAVE_OSF_SIA */
Damien Miller60396b02001-02-18 17:01:00 +1100376 auth_password(authctxt, password) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000377#endif /* USE_PAM */
378 authenticated = 1;
379 memset(password, 0, len);
380 xfree(password);
381 return authenticated;
382}
Damien Miller874d77b2000-10-14 16:23:11 +1100383
Ben Lindstrombba81212001-06-25 05:01:22 +0000384static int
Damien Miller874d77b2000-10-14 16:23:11 +1100385userauth_kbdint(Authctxt *authctxt)
386{
387 int authenticated = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000388 char *lang, *devs;
Damien Miller874d77b2000-10-14 16:23:11 +1100389
390 lang = packet_get_string(NULL);
391 devs = packet_get_string(NULL);
392 packet_done();
393
Ben Lindstrom551ea372001-06-05 18:56:16 +0000394 debug("keyboard-interactive devs %s", devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000395
Ben Lindstrom551ea372001-06-05 18:56:16 +0000396 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000397 authenticated = auth2_challenge(authctxt, devs);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000398
Damien Millerb8481582000-12-03 11:51:51 +1100399#ifdef USE_PAM
Damien Millerf8154422001-04-25 22:44:14 +1000400 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
Damien Millerb8481582000-12-03 11:51:51 +1100401 authenticated = auth2_pam(authctxt);
402#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100403 xfree(devs);
Ben Lindstrom551ea372001-06-05 18:56:16 +0000404 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +1100405#ifdef HAVE_CYGWIN
406 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
407 return(0);
408#endif
409 return authenticated;
410}
411
Ben Lindstrombba81212001-06-25 05:01:22 +0000412static int
Damien Miller874d77b2000-10-14 16:23:11 +1100413userauth_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);
Ben Lindstrom3f364962001-04-19 20:50:07 +0000471 buffer_put_cstring(&b, pkalg);
Ben Lindstromd121f612000-12-03 17:00:47 +0000472 }
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
Ben Lindstrombba81212001-06-25 05:01:22 +0000518static int
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000519userauth_hostbased(Authctxt *authctxt)
520{
521 Buffer b;
522 Key *key;
Ben Lindstrom671388f2001-04-19 20:40:45 +0000523 char *pkalg, *pkblob, *sig, *cuser, *chost, *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000524 u_int alen, blen, slen;
525 int pktype;
526 int authenticated = 0;
527
528 if (!authctxt->valid) {
529 debug2("userauth_hostbased: disabled because of invalid user");
530 return 0;
531 }
532 pkalg = packet_get_string(&alen);
533 pkblob = packet_get_string(&blen);
534 chost = packet_get_string(NULL);
535 cuser = packet_get_string(NULL);
536 sig = packet_get_string(&slen);
537
538 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
539 cuser, chost, pkalg, slen);
540#ifdef DEBUG_PK
541 debug("signature:");
542 buffer_init(&b);
543 buffer_append(&b, sig, slen);
544 buffer_dump(&b);
545 buffer_free(&b);
546#endif
547 pktype = key_type_from_name(pkalg);
548 if (pktype == KEY_UNSPEC) {
549 /* this is perfectly legal */
550 log("userauth_hostbased: unsupported "
551 "public key algorithm: %s", pkalg);
552 goto done;
553 }
554 key = key_from_blob(pkblob, blen);
555 if (key == NULL) {
556 debug("userauth_hostbased: cannot decode key: %s", pkalg);
557 goto done;
558 }
Ben Lindstrom671388f2001-04-19 20:40:45 +0000559 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
560 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000561 buffer_init(&b);
Ben Lindstrom671388f2001-04-19 20:40:45 +0000562 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000563 /* reconstruct packet */
564 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
565 buffer_put_cstring(&b, authctxt->user);
Ben Lindstrom671388f2001-04-19 20:40:45 +0000566 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000567 buffer_put_cstring(&b, "hostbased");
568 buffer_put_string(&b, pkalg, alen);
569 buffer_put_string(&b, pkblob, blen);
570 buffer_put_cstring(&b, chost);
571 buffer_put_cstring(&b, cuser);
572#ifdef DEBUG_PK
573 buffer_dump(&b);
574#endif
575 /* test for allowed key and correct signature */
576 if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
577 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
578 authenticated = 1;
579
580 buffer_clear(&b);
581 key_free(key);
582
583done:
584 debug2("userauth_hostbased: authenticated %d", authenticated);
585 xfree(pkalg);
586 xfree(pkblob);
587 xfree(cuser);
588 xfree(chost);
589 xfree(sig);
590 return authenticated;
591}
592
Damien Miller874d77b2000-10-14 16:23:11 +1100593/* get current user */
Damien Millereba71ba2000-04-29 23:57:08 +1000594
595struct passwd*
596auth_get_user(void)
597{
Damien Miller874d77b2000-10-14 16:23:11 +1100598 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000599}
600
Damien Miller874d77b2000-10-14 16:23:11 +1100601#define DELIM ","
Damien Millereba71ba2000-04-29 23:57:08 +1000602
Ben Lindstrom79073822001-07-04 03:42:30 +0000603static char *
Damien Miller874d77b2000-10-14 16:23:11 +1100604authmethods_get(void)
605{
606 Authmethod *method = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000607 u_int size = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100608 char *list;
609
610 for (method = authmethods; method->name != NULL; method++) {
611 if (strcmp(method->name, "none") == 0)
612 continue;
613 if (method->enabled != NULL && *(method->enabled) != 0) {
614 if (size != 0)
615 size += strlen(DELIM);
616 size += strlen(method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000617 }
618 }
Damien Miller874d77b2000-10-14 16:23:11 +1100619 size++; /* trailing '\0' */
620 list = xmalloc(size);
621 list[0] = '\0';
622
623 for (method = authmethods; method->name != NULL; method++) {
624 if (strcmp(method->name, "none") == 0)
625 continue;
626 if (method->enabled != NULL && *(method->enabled) != 0) {
627 if (list[0] != '\0')
628 strlcat(list, DELIM, size);
629 strlcat(list, method->name, size);
630 }
631 }
632 return list;
633}
634
Ben Lindstrombba81212001-06-25 05:01:22 +0000635static Authmethod *
Damien Miller874d77b2000-10-14 16:23:11 +1100636authmethod_lookup(const char *name)
637{
638 Authmethod *method = NULL;
639 if (name != NULL)
640 for (method = authmethods; method->name != NULL; method++)
641 if (method->enabled != NULL &&
642 *(method->enabled) != 0 &&
643 strcmp(name, method->name) == 0)
644 return method;
645 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
646 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000647}
648
649/* return 1 if user allows given key */
Ben Lindstrombba81212001-06-25 05:01:22 +0000650static int
Ben Lindstromf96704d2001-06-25 04:17:12 +0000651user_key_allowed2(struct passwd *pw, Key *key, char *file)
Damien Millereba71ba2000-04-29 23:57:08 +1000652{
Ben Lindstromf96704d2001-06-25 04:17:12 +0000653 char line[8192];
Damien Millereba71ba2000-04-29 23:57:08 +1000654 int found_key = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000655 FILE *f;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000656 u_long linenum = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000657 struct stat st;
658 Key *found;
659
Damien Miller874d77b2000-10-14 16:23:11 +1100660 if (pw == NULL)
661 return 0;
662
Damien Millereba71ba2000-04-29 23:57:08 +1000663 /* Temporarily use the user's uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000664 temporarily_use_uid(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000665
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000666 debug("trying public key file %s", file);
Damien Millereba71ba2000-04-29 23:57:08 +1000667
668 /* Fail quietly if file does not exist */
669 if (stat(file, &st) < 0) {
670 /* Restore the privileged uid. */
671 restore_uid();
672 return 0;
673 }
674 /* Open the file containing the authorized keys. */
675 f = fopen(file, "r");
676 if (!f) {
677 /* Restore the privileged uid. */
678 restore_uid();
679 return 0;
680 }
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000681 if (options.strict_modes &&
Ben Lindstrom90279d82001-07-04 03:56:56 +0000682 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000683 fclose(f);
684 log("Authentication refused: %s", line);
685 restore_uid();
686 return 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000687 }
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000688
Damien Millereba71ba2000-04-29 23:57:08 +1000689 found_key = 0;
Damien Millerd3444942000-09-30 14:20:03 +1100690 found = key_new(key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000691
692 while (fgets(line, sizeof(line), f)) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000693 char *cp, *options = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000694 linenum++;
695 /* Skip leading whitespace, empty and comment lines. */
696 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
697 ;
698 if (!*cp || *cp == '\n' || *cp == '#')
699 continue;
Damien Millerf6d9e222000-06-18 14:50:44 +1000700
Ben Lindstrom1bc3bdb2001-09-20 23:11:26 +0000701 if (key_read(found, &cp) != 1) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000702 /* no key? check if there are options for this key */
703 int quoted = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100704 debug2("user_key_allowed: check options: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000705 options = cp;
706 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
707 if (*cp == '\\' && cp[1] == '"')
708 cp++; /* Skip both */
709 else if (*cp == '"')
710 quoted = !quoted;
711 }
712 /* Skip remaining whitespace. */
713 for (; *cp == ' ' || *cp == '\t'; cp++)
714 ;
Ben Lindstrom1bc3bdb2001-09-20 23:11:26 +0000715 if (key_read(found, &cp) != 1) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100716 debug2("user_key_allowed: advance: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000717 /* still no key? advance to next line*/
718 continue;
719 }
720 }
721 if (key_equal(found, key) &&
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000722 auth_parse_options(pw, options, file, linenum) == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000723 found_key = 1;
Ben Lindstrom940fb862001-08-06 21:01:49 +0000724 debug("matching key found: file %s, line %lu",
Damien Millereba71ba2000-04-29 23:57:08 +1000725 file, linenum);
726 break;
727 }
728 }
729 restore_uid();
730 fclose(f);
731 key_free(found);
Ben Lindstromb54873a2001-03-11 20:01:55 +0000732 if (!found_key)
733 debug2("key not found");
Damien Millereba71ba2000-04-29 23:57:08 +1000734 return found_key;
735}
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000736
Ben Lindstromf96704d2001-06-25 04:17:12 +0000737/* check whether given key is in .ssh/authorized_keys* */
Ben Lindstrombba81212001-06-25 05:01:22 +0000738static int
Ben Lindstromf96704d2001-06-25 04:17:12 +0000739user_key_allowed(struct passwd *pw, Key *key)
740{
741 int success;
742 char *file;
743
744 file = authorized_keys_file(pw);
745 success = user_key_allowed2(pw, key, file);
746 xfree(file);
747 if (success)
748 return success;
749
750 /* try suffix "2" for backward compat, too */
751 file = authorized_keys_file2(pw);
752 success = user_key_allowed2(pw, key, file);
753 xfree(file);
754 return success;
755}
756
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000757/* return 1 if given hostkey is allowed */
Ben Lindstrombba81212001-06-25 05:01:22 +0000758static int
Ben Lindstrom4aa603c2001-04-19 20:38:06 +0000759hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000760 Key *key)
761{
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000762 const char *resolvedname, *ipaddr, *lookup;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000763 int host_status, len;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000764
765 resolvedname = get_canonical_hostname(options.reverse_mapping_check);
766 ipaddr = get_remote_ipaddr();
767
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000768 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
769 chost, resolvedname, ipaddr);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000770
771 if (options.hostbased_uses_name_from_packet_only) {
772 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
773 return 0;
774 lookup = chost;
775 } else {
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000776 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
777 debug2("stripping trailing dot from chost %s", chost);
778 chost[len - 1] = '\0';
779 }
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000780 if (strcasecmp(resolvedname, chost) != 0)
781 log("userauth_hostbased mismatch: "
782 "client sends %s, but we resolve %s to %s",
783 chost, ipaddr, resolvedname);
784 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
785 return 0;
786 lookup = resolvedname;
787 }
788 debug2("userauth_hostbased: access allowed by auth_rhosts2");
789
Ben Lindstrom83647ce2001-06-25 04:30:16 +0000790 host_status = check_key_in_hostfiles(pw, key, lookup,
791 _PATH_SSH_SYSTEM_HOSTFILE,
Ben Lindstroma4789ef2001-06-25 04:40:49 +0000792 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000793
Ben Lindstrom83647ce2001-06-25 04:30:16 +0000794 /* backward compat if no key has been found. */
795 if (host_status == HOST_NEW)
796 host_status = check_key_in_hostfiles(pw, key, lookup,
797 _PATH_SSH_SYSTEM_HOSTFILE2,
Ben Lindstroma4789ef2001-06-25 04:40:49 +0000798 options.ignore_user_known_hosts ? NULL :
799 _PATH_SSH_USER_HOSTFILE2);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000800
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000801 return (host_status == HOST_OK);
802}
Ben Lindstrom83647ce2001-06-25 04:30:16 +0000803