blob: 4880b736ef1e80ff8bda088062de40cb0d1aca25 [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 Lindstrom4dccfa52000-12-28 16:40:05 +000026RCSID("$OpenBSD: auth2.c,v 1.24 2000/12/28 14:25:51 markus Exp $");
Damien Miller874d77b2000-10-14 16:23:11 +110027
28#ifdef HAVE_OSF_SIA
29# include <sia.h>
30# include <siad.h>
31#endif
Damien Millereba71ba2000-04-29 23:57:08 +100032
33#include <openssl/dsa.h>
34#include <openssl/rsa.h>
35#include <openssl/evp.h>
36
37#include "xmalloc.h"
38#include "rsa.h"
39#include "ssh.h"
40#include "pty.h"
41#include "packet.h"
42#include "buffer.h"
Damien Millereba71ba2000-04-29 23:57:08 +100043#include "servconf.h"
44#include "compat.h"
45#include "channels.h"
46#include "bufaux.h"
47#include "ssh2.h"
48#include "auth.h"
49#include "session.h"
50#include "dispatch.h"
51#include "auth.h"
52#include "key.h"
53#include "kex.h"
54
Damien Millereba71ba2000-04-29 23:57:08 +100055#include "uidswap.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100056#include "auth-options.h"
Damien Millereba71ba2000-04-29 23:57:08 +100057
58/* import */
59extern ServerOptions options;
Ben Lindstrom46c16222000-12-22 01:43:59 +000060extern u_char *session_id2;
Damien Millereba71ba2000-04-29 23:57:08 +100061extern int session_id2_len;
62
Damien Miller874d77b2000-10-14 16:23:11 +110063#ifdef WITH_AIXAUTHENTICATE
64extern char *aixloginmsg;
65#endif
66#ifdef HAVE_OSF_SIA
67extern int saved_argc;
68extern char **saved_argv;
69#endif
70
71static Authctxt *x_authctxt = NULL;
72static int one = 1;
73
74typedef struct Authmethod Authmethod;
75struct Authmethod {
76 char *name;
77 int (*userauth)(Authctxt *authctxt);
78 int *enabled;
79};
80
Damien Millereba71ba2000-04-29 23:57:08 +100081/* protocol */
82
Damien Miller62cee002000-09-23 17:15:56 +110083void input_service_request(int type, int plen, void *ctxt);
84void input_userauth_request(int type, int plen, void *ctxt);
85void protocol_error(int type, int plen, void *ctxt);
Damien Millereba71ba2000-04-29 23:57:08 +100086
Damien Millereba71ba2000-04-29 23:57:08 +100087
88/* helper */
Damien Miller874d77b2000-10-14 16:23:11 +110089Authmethod *authmethod_lookup(const char *name);
90struct passwd *pwcopy(struct passwd *pw);
Damien Miller0bc1bd82000-11-13 22:57:25 +110091int user_key_allowed(struct passwd *pw, Key *key);
Damien Miller874d77b2000-10-14 16:23:11 +110092char *authmethods_get(void);
Damien Millereba71ba2000-04-29 23:57:08 +100093
Damien Miller874d77b2000-10-14 16:23:11 +110094/* auth */
95int userauth_none(Authctxt *authctxt);
96int userauth_passwd(Authctxt *authctxt);
97int userauth_pubkey(Authctxt *authctxt);
98int userauth_kbdint(Authctxt *authctxt);
99
100Authmethod authmethods[] = {
101 {"none",
102 userauth_none,
103 &one},
104 {"publickey",
105 userauth_pubkey,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100106 &options.pubkey_authentication},
Damien Miller874d77b2000-10-14 16:23:11 +1100107 {"keyboard-interactive",
108 userauth_kbdint,
109 &options.kbd_interactive_authentication},
110 {"password",
111 userauth_passwd,
112 &options.password_authentication},
113 {NULL, NULL, NULL}
Damien Millereba71ba2000-04-29 23:57:08 +1000114};
Damien Millereba71ba2000-04-29 23:57:08 +1000115
116/*
Damien Miller874d77b2000-10-14 16:23:11 +1100117 * loop until authctxt->success == TRUE
Damien Millereba71ba2000-04-29 23:57:08 +1000118 */
119
120void
121do_authentication2()
122{
Damien Miller874d77b2000-10-14 16:23:11 +1100123 Authctxt *authctxt = xmalloc(sizeof(*authctxt));
124 memset(authctxt, 'a', sizeof(*authctxt));
125 authctxt->valid = 0;
126 authctxt->attempt = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000127 authctxt->failures = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100128 authctxt->success = 0;
129 x_authctxt = authctxt; /*XXX*/
130
Damien Miller1cead2c2000-05-01 22:55:23 +1000131#ifdef KRB4
Damien Miller874d77b2000-10-14 16:23:11 +1100132 /* turn off kerberos, not supported by SSH2 */
Damien Miller35dabd02000-05-01 21:10:33 +1000133 options.kerberos_authentication = 0;
Damien Miller1cead2c2000-05-01 22:55:23 +1000134#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000135 dispatch_init(&protocol_error);
136 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
Damien Miller874d77b2000-10-14 16:23:11 +1100137 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000138 do_authenticated2();
139}
140
141void
Damien Miller62cee002000-09-23 17:15:56 +1100142protocol_error(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000143{
144 log("auth: protocol error: type %d plen %d", type, plen);
145 packet_start(SSH2_MSG_UNIMPLEMENTED);
146 packet_put_int(0);
147 packet_send();
148 packet_write_wait();
149}
150
151void
Damien Miller62cee002000-09-23 17:15:56 +1100152input_service_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000153{
Damien Miller874d77b2000-10-14 16:23:11 +1100154 Authctxt *authctxt = ctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000155 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000156 int accept = 0;
157 char *service = packet_get_string(&len);
158 packet_done();
159
Damien Miller874d77b2000-10-14 16:23:11 +1100160 if (authctxt == NULL)
161 fatal("input_service_request: no authctxt");
162
Damien Millereba71ba2000-04-29 23:57:08 +1000163 if (strcmp(service, "ssh-userauth") == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100164 if (!authctxt->success) {
Damien Millereba71ba2000-04-29 23:57:08 +1000165 accept = 1;
166 /* now we can handle user-auth requests */
167 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
168 }
169 }
170 /* XXX all other service requests are denied */
171
172 if (accept) {
173 packet_start(SSH2_MSG_SERVICE_ACCEPT);
174 packet_put_cstring(service);
175 packet_send();
176 packet_write_wait();
177 } else {
178 debug("bad service request %s", service);
179 packet_disconnect("bad service request %s", service);
180 }
181 xfree(service);
182}
183
184void
Damien Miller62cee002000-09-23 17:15:56 +1100185input_userauth_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000186{
Damien Miller874d77b2000-10-14 16:23:11 +1100187 Authctxt *authctxt = ctxt;
188 Authmethod *m = NULL;
189 char *user, *service, *method;
Damien Millereba71ba2000-04-29 23:57:08 +1000190 int authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000191
Damien Miller874d77b2000-10-14 16:23:11 +1100192 if (authctxt == NULL)
193 fatal("input_userauth_request: no authctxt");
Damien Millereba71ba2000-04-29 23:57:08 +1000194
Damien Miller874d77b2000-10-14 16:23:11 +1100195 user = packet_get_string(NULL);
196 service = packet_get_string(NULL);
197 method = packet_get_string(NULL);
198 debug("userauth-request for user %s service %s method %s", user, service, method);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000199 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
Damien Miller874d77b2000-10-14 16:23:11 +1100200
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000201 if (authctxt->attempt++ == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100202 /* setup auth context */
203 struct passwd *pw = NULL;
204 setproctitle("%s", user);
205 pw = getpwnam(user);
206 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
207 authctxt->pw = pwcopy(pw);
208 authctxt->valid = 1;
209 debug2("input_userauth_request: setting up authctxt for %s", user);
210#ifdef USE_PAM
211 start_pam(pw);
212#endif
213 } else {
214 log("input_userauth_request: illegal user %s", user);
215 }
216 authctxt->user = xstrdup(user);
217 authctxt->service = xstrdup(service);
218 } else if (authctxt->valid) {
219 if (strcmp(user, authctxt->user) != 0 ||
220 strcmp(service, authctxt->service) != 0) {
221 log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)",
222 user, service, authctxt->user, authctxt->service);
223 authctxt->valid = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000224 }
225 }
Damien Millerb70b61f2000-09-16 16:25:12 +1100226
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);
231 } else {
232 debug2("input_userauth_request: unsupported method %s", method);
233 }
234 if (!authctxt->valid && authenticated == 1) {
235 log("input_userauth_request: INTERNAL ERROR: authenticated invalid user %s service %s", user, method);
Damien Millerb70b61f2000-09-16 16:25:12 +1100236 authenticated = 0;
237 }
Damien Millerb70b61f2000-09-16 16:25:12 +1100238
Damien Miller874d77b2000-10-14 16:23:11 +1100239 /* Special handling for root */
240 if (authenticated == 1 &&
241 authctxt->valid && authctxt->pw->pw_uid == 0 && !options.permit_root_login) {
Damien Millereba71ba2000-04-29 23:57:08 +1000242 authenticated = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100243 log("ROOT LOGIN REFUSED FROM %.200s", get_canonical_hostname());
Damien Millereba71ba2000-04-29 23:57:08 +1000244 }
245
246#ifdef USE_PAM
Damien Millerd425d4d2000-10-28 21:05:57 +1100247 if (authenticated && authctxt->user && !do_pam_account(authctxt->user, NULL))
Damien Millerb70b61f2000-09-16 16:25:12 +1100248 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000249#endif /* USE_PAM */
250
Damien Miller874d77b2000-10-14 16:23:11 +1100251 /* Log before sending the reply */
252 userauth_log(authctxt, authenticated, method);
253 userauth_reply(authctxt, authenticated);
254
255 xfree(service);
256 xfree(user);
257 xfree(method);
258}
259
260
261void
262userauth_log(Authctxt *authctxt, int authenticated, char *method)
263{
264 void (*authlog) (const char *fmt,...) = verbose;
265 char *user = NULL, *authmsg = NULL;
266
Damien Millere247cc42000-05-07 12:03:14 +1000267 /* Raise logging level */
268 if (authenticated == 1 ||
Damien Miller874d77b2000-10-14 16:23:11 +1100269 !authctxt->valid ||
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000270 authctxt->failures >= AUTH_FAIL_LOG ||
Damien Millere247cc42000-05-07 12:03:14 +1000271 strcmp(method, "password") == 0)
272 authlog = log;
273
Damien Millereba71ba2000-04-29 23:57:08 +1000274 if (authenticated == 1) {
275 authmsg = "Accepted";
Damien Millere247cc42000-05-07 12:03:14 +1000276 } else if (authenticated == 0) {
277 authmsg = "Failed";
278 } else {
279 authmsg = "Postponed";
280 }
Damien Millere247cc42000-05-07 12:03:14 +1000281
Damien Miller874d77b2000-10-14 16:23:11 +1100282 if (authctxt->valid) {
283 user = authctxt->pw->pw_uid == 0 ? "ROOT" : authctxt->user;
284 } else {
285 user = "NOUSER";
286 }
287
288 authlog("%s %s for %.200s from %.200s port %d ssh2",
289 authmsg,
290 method,
291 user,
292 get_remote_ipaddr(),
293 get_remote_port());
294}
295
296void
297userauth_reply(Authctxt *authctxt, int authenticated)
298{
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000299 char *methods;
Damien Millere247cc42000-05-07 12:03:14 +1000300 /* XXX todo: check if multiple auth methods are needed */
301 if (authenticated == 1) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000302#ifdef WITH_AIXAUTHENTICATE
303 /* We don't have a pty yet, so just label the line as "ssh" */
Damien Millerd425d4d2000-10-28 21:05:57 +1100304 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
305 get_canonical_hostname(), "ssh", &aixloginmsg) < 0)
Damien Millerd2c208a2000-05-17 22:00:02 +1000306 aixloginmsg = NULL;
307#endif /* WITH_AIXAUTHENTICATE */
Damien Millereba71ba2000-04-29 23:57:08 +1000308 /* turn off userauth */
309 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
310 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
311 packet_send();
312 packet_write_wait();
313 /* now we can break out */
Damien Miller874d77b2000-10-14 16:23:11 +1100314 authctxt->success = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000315 } else if (authenticated == 0) {
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000316 if (authctxt->failures++ >= AUTH_FAIL_MAX)
317 packet_disconnect("too many failed userauth_requests");
318 methods = authmethods_get();
Damien Millereba71ba2000-04-29 23:57:08 +1000319 packet_start(SSH2_MSG_USERAUTH_FAILURE);
Damien Miller874d77b2000-10-14 16:23:11 +1100320 packet_put_cstring(methods);
321 packet_put_char(0); /* XXX partial success, unused */
Damien Millereba71ba2000-04-29 23:57:08 +1000322 packet_send();
323 packet_write_wait();
Damien Miller874d77b2000-10-14 16:23:11 +1100324 xfree(methods);
325 } else {
326 /* do nothing, we did already send a reply */
Damien Millereba71ba2000-04-29 23:57:08 +1000327 }
Damien Millereba71ba2000-04-29 23:57:08 +1000328}
329
330int
Damien Miller874d77b2000-10-14 16:23:11 +1100331userauth_none(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000332{
Damien Miller874d77b2000-10-14 16:23:11 +1100333 /* disable method "none", only allowed one time */
334 Authmethod *m = authmethod_lookup("none");
335 if (m != NULL)
336 m->enabled = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000337 packet_done();
Damien Millerb8c656e2000-06-28 15:22:41 +1000338
Damien Miller874d77b2000-10-14 16:23:11 +1100339 if (authctxt->valid == 0)
340 return(0);
341
342#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 Miller874d77b2000-10-14 16:23:11 +1100349 return (sia_validate_user(NULL, saved_argc, saved_argv,
Damien Millerd425d4d2000-10-28 21:05:57 +1100350 get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER",
351 NULL, 0, NULL, "") == SIASUCCESS);
Damien Millerb8c656e2000-06-28 15:22:41 +1000352#else /* !HAVE_OSF_SIA && !USE_PAM */
Damien Miller874d77b2000-10-14 16:23:11 +1100353 return auth_password(authctxt->pw, "");
Damien Millereba71ba2000-04-29 23:57:08 +1000354#endif /* USE_PAM */
355}
Damien Miller874d77b2000-10-14 16:23:11 +1100356
Damien Millereba71ba2000-04-29 23:57:08 +1000357int
Damien Miller874d77b2000-10-14 16:23:11 +1100358userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000359{
360 char *password;
361 int authenticated = 0;
362 int change;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000363 u_int len;
Damien Millereba71ba2000-04-29 23:57:08 +1000364 change = packet_get_char();
365 if (change)
366 log("password change not supported");
367 password = packet_get_string(&len);
368 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100369 if (authctxt->valid &&
370#ifdef HAVE_CYGWIN
371 check_nt_auth(1, authctxt->pw->pw_uid) &&
372#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000373#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100374 auth_pam_password(authctxt->pw, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000375#elif defined(HAVE_OSF_SIA)
376 sia_validate_user(NULL, saved_argc, saved_argv,
Damien Millerd425d4d2000-10-28 21:05:57 +1100377 get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER",
378 NULL, 0, NULL, password) == SIASUCCESS)
Damien Millerb8c656e2000-06-28 15:22:41 +1000379#else /* !USE_PAM && !HAVE_OSF_SIA */
Damien Miller874d77b2000-10-14 16:23:11 +1100380 auth_password(authctxt->pw, password) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000381#endif /* USE_PAM */
382 authenticated = 1;
383 memset(password, 0, len);
384 xfree(password);
385 return authenticated;
386}
Damien Miller874d77b2000-10-14 16:23:11 +1100387
Damien Millereba71ba2000-04-29 23:57:08 +1000388int
Damien Miller874d77b2000-10-14 16:23:11 +1100389userauth_kbdint(Authctxt *authctxt)
390{
391 int authenticated = 0;
392 char *lang = NULL;
393 char *devs = NULL;
394
395 lang = packet_get_string(NULL);
396 devs = packet_get_string(NULL);
397 packet_done();
398
399 debug("keyboard-interactive language %s devs %s", lang, devs);
Damien Millerb8481582000-12-03 11:51:51 +1100400#ifdef USE_PAM
401 if (authenticated == 0)
402 authenticated = auth2_pam(authctxt);
403#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100404#ifdef SKEY
405 /* XXX hardcoded, we should look at devs */
Damien Millerb8481582000-12-03 11:51:51 +1100406 if (authenticated == 0)
407 if (options.skey_authentication != 0)
408 authenticated = auth2_skey(authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100409#endif
410 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();
508 authenticated = -1;
509 }
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{
585 char line[8192], file[1024];
586 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,
600 SSH_USER_PERMITTED_KEYS2);
601
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 {
628 /* Check path to SSH_USER_PERMITTED_KEYS */
629 int i;
630 static const char *check[] = {
631 "", SSH_USER_DIR, NULL
632 };
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);
Damien Miller37023962000-07-11 17:31:38 +1000650 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) &&
688 auth_parse_options(pw, options, 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);
698 return found_key;
699}
Damien Miller874d77b2000-10-14 16:23:11 +1100700
701struct passwd *
702pwcopy(struct passwd *pw)
703{
704 struct passwd *copy = xmalloc(sizeof(*copy));
705 memset(copy, 0, sizeof(*copy));
706 copy->pw_name = xstrdup(pw->pw_name);
707 copy->pw_passwd = xstrdup(pw->pw_passwd);
708 copy->pw_uid = pw->pw_uid;
709 copy->pw_gid = pw->pw_gid;
710#ifdef HAVE_PW_CLASS_IN_PASSWD
711 copy->pw_class = xstrdup(pw->pw_class);
712#endif
713 copy->pw_dir = xstrdup(pw->pw_dir);
714 copy->pw_shell = xstrdup(pw->pw_shell);
715 return copy;
716}