blob: 8e8edf920db6d83f862e38939a875ebd34a2ff9e [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 Miller0bc1bd82000-11-13 22:57:25 +110026RCSID("$OpenBSD: auth2.c,v 1.21 2000/11/12 19:50:37 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;
60extern unsigned char *session_id2;
61extern 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;
127 authctxt->success = 0;
128 x_authctxt = authctxt; /*XXX*/
129
Damien Miller1cead2c2000-05-01 22:55:23 +1000130#ifdef KRB4
Damien Miller874d77b2000-10-14 16:23:11 +1100131 /* turn off kerberos, not supported by SSH2 */
Damien Miller35dabd02000-05-01 21:10:33 +1000132 options.kerberos_authentication = 0;
Damien Miller1cead2c2000-05-01 22:55:23 +1000133#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000134 dispatch_init(&protocol_error);
135 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
Damien Miller874d77b2000-10-14 16:23:11 +1100136 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
Damien Millereba71ba2000-04-29 23:57:08 +1000137 do_authenticated2();
138}
139
140void
Damien Miller62cee002000-09-23 17:15:56 +1100141protocol_error(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000142{
143 log("auth: protocol error: type %d plen %d", type, plen);
144 packet_start(SSH2_MSG_UNIMPLEMENTED);
145 packet_put_int(0);
146 packet_send();
147 packet_write_wait();
148}
149
150void
Damien Miller62cee002000-09-23 17:15:56 +1100151input_service_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000152{
Damien Miller874d77b2000-10-14 16:23:11 +1100153 Authctxt *authctxt = ctxt;
Damien Millereba71ba2000-04-29 23:57:08 +1000154 unsigned int len;
155 int accept = 0;
156 char *service = packet_get_string(&len);
157 packet_done();
158
Damien Miller874d77b2000-10-14 16:23:11 +1100159 if (authctxt == NULL)
160 fatal("input_service_request: no authctxt");
161
Damien Millereba71ba2000-04-29 23:57:08 +1000162 if (strcmp(service, "ssh-userauth") == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100163 if (!authctxt->success) {
Damien Millereba71ba2000-04-29 23:57:08 +1000164 accept = 1;
165 /* now we can handle user-auth requests */
166 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
167 }
168 }
169 /* XXX all other service requests are denied */
170
171 if (accept) {
172 packet_start(SSH2_MSG_SERVICE_ACCEPT);
173 packet_put_cstring(service);
174 packet_send();
175 packet_write_wait();
176 } else {
177 debug("bad service request %s", service);
178 packet_disconnect("bad service request %s", service);
179 }
180 xfree(service);
181}
182
183void
Damien Miller62cee002000-09-23 17:15:56 +1100184input_userauth_request(int type, int plen, void *ctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000185{
Damien Miller874d77b2000-10-14 16:23:11 +1100186 Authctxt *authctxt = ctxt;
187 Authmethod *m = NULL;
188 char *user, *service, *method;
Damien Millereba71ba2000-04-29 23:57:08 +1000189 int authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000190
Damien Miller874d77b2000-10-14 16:23:11 +1100191 if (authctxt == NULL)
192 fatal("input_userauth_request: no authctxt");
193 if (authctxt->attempt++ >= AUTH_FAIL_MAX) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000194#ifdef WITH_AIXAUTHENTICATE
Damien Millerd425d4d2000-10-28 21:05:57 +1100195 loginfailed(authctxt->user?authctxt->user:"NOUSER",
196 get_canonical_hostname(), "ssh");
Damien Millerd2c208a2000-05-17 22:00:02 +1000197#endif /* WITH_AIXAUTHENTICATE */
198 packet_disconnect("too many failed userauth_requests");
199 }
Damien Millereba71ba2000-04-29 23:57:08 +1000200
Damien Miller874d77b2000-10-14 16:23:11 +1100201 user = packet_get_string(NULL);
202 service = packet_get_string(NULL);
203 method = packet_get_string(NULL);
204 debug("userauth-request for user %s service %s method %s", user, service, method);
205 debug("attempt #%d", authctxt->attempt);
206
207 if (authctxt->attempt == 1) {
208 /* setup auth context */
209 struct passwd *pw = NULL;
210 setproctitle("%s", user);
211 pw = getpwnam(user);
212 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
213 authctxt->pw = pwcopy(pw);
214 authctxt->valid = 1;
215 debug2("input_userauth_request: setting up authctxt for %s", user);
216#ifdef USE_PAM
217 start_pam(pw);
218#endif
219 } else {
220 log("input_userauth_request: illegal user %s", user);
221 }
222 authctxt->user = xstrdup(user);
223 authctxt->service = xstrdup(service);
224 } else if (authctxt->valid) {
225 if (strcmp(user, authctxt->user) != 0 ||
226 strcmp(service, authctxt->service) != 0) {
227 log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)",
228 user, service, authctxt->user, authctxt->service);
229 authctxt->valid = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000230 }
231 }
Damien Millerb70b61f2000-09-16 16:25:12 +1100232
Damien Miller874d77b2000-10-14 16:23:11 +1100233 m = authmethod_lookup(method);
234 if (m != NULL) {
235 debug2("input_userauth_request: try method %s", method);
236 authenticated = m->userauth(authctxt);
237 } else {
238 debug2("input_userauth_request: unsupported method %s", method);
239 }
240 if (!authctxt->valid && authenticated == 1) {
241 log("input_userauth_request: INTERNAL ERROR: authenticated invalid user %s service %s", user, method);
Damien Millerb70b61f2000-09-16 16:25:12 +1100242 authenticated = 0;
243 }
Damien Millerb70b61f2000-09-16 16:25:12 +1100244
Damien Miller874d77b2000-10-14 16:23:11 +1100245 /* Special handling for root */
246 if (authenticated == 1 &&
247 authctxt->valid && authctxt->pw->pw_uid == 0 && !options.permit_root_login) {
Damien Millereba71ba2000-04-29 23:57:08 +1000248 authenticated = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100249 log("ROOT LOGIN REFUSED FROM %.200s", get_canonical_hostname());
Damien Millereba71ba2000-04-29 23:57:08 +1000250 }
251
252#ifdef USE_PAM
Damien Millerd425d4d2000-10-28 21:05:57 +1100253 if (authenticated && authctxt->user && !do_pam_account(authctxt->user, NULL))
Damien Millerb70b61f2000-09-16 16:25:12 +1100254 authenticated = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000255#endif /* USE_PAM */
256
Damien Miller874d77b2000-10-14 16:23:11 +1100257 /* Log before sending the reply */
258 userauth_log(authctxt, authenticated, method);
259 userauth_reply(authctxt, authenticated);
260
261 xfree(service);
262 xfree(user);
263 xfree(method);
264}
265
266
267void
268userauth_log(Authctxt *authctxt, int authenticated, char *method)
269{
270 void (*authlog) (const char *fmt,...) = verbose;
271 char *user = NULL, *authmsg = NULL;
272
Damien Millere247cc42000-05-07 12:03:14 +1000273 /* Raise logging level */
274 if (authenticated == 1 ||
Damien Miller874d77b2000-10-14 16:23:11 +1100275 !authctxt->valid ||
276 authctxt->attempt >= AUTH_FAIL_LOG ||
Damien Millere247cc42000-05-07 12:03:14 +1000277 strcmp(method, "password") == 0)
278 authlog = log;
279
Damien Millereba71ba2000-04-29 23:57:08 +1000280 if (authenticated == 1) {
281 authmsg = "Accepted";
Damien Millere247cc42000-05-07 12:03:14 +1000282 } else if (authenticated == 0) {
283 authmsg = "Failed";
284 } else {
285 authmsg = "Postponed";
286 }
Damien Millere247cc42000-05-07 12:03:14 +1000287
Damien Miller874d77b2000-10-14 16:23:11 +1100288 if (authctxt->valid) {
289 user = authctxt->pw->pw_uid == 0 ? "ROOT" : authctxt->user;
290 } else {
291 user = "NOUSER";
292 }
293
294 authlog("%s %s for %.200s from %.200s port %d ssh2",
295 authmsg,
296 method,
297 user,
298 get_remote_ipaddr(),
299 get_remote_port());
300}
301
302void
303userauth_reply(Authctxt *authctxt, int authenticated)
304{
Damien Millere247cc42000-05-07 12:03:14 +1000305 /* XXX todo: check if multiple auth methods are needed */
306 if (authenticated == 1) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000307#ifdef WITH_AIXAUTHENTICATE
308 /* We don't have a pty yet, so just label the line as "ssh" */
Damien Millerd425d4d2000-10-28 21:05:57 +1100309 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
310 get_canonical_hostname(), "ssh", &aixloginmsg) < 0)
Damien Millerd2c208a2000-05-17 22:00:02 +1000311 aixloginmsg = NULL;
312#endif /* WITH_AIXAUTHENTICATE */
Damien Millereba71ba2000-04-29 23:57:08 +1000313 /* turn off userauth */
314 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
315 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
316 packet_send();
317 packet_write_wait();
318 /* now we can break out */
Damien Miller874d77b2000-10-14 16:23:11 +1100319 authctxt->success = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000320 } else if (authenticated == 0) {
Damien Miller874d77b2000-10-14 16:23:11 +1100321 char *methods = authmethods_get();
Damien Millereba71ba2000-04-29 23:57:08 +1000322 packet_start(SSH2_MSG_USERAUTH_FAILURE);
Damien Miller874d77b2000-10-14 16:23:11 +1100323 packet_put_cstring(methods);
324 packet_put_char(0); /* XXX partial success, unused */
Damien Millereba71ba2000-04-29 23:57:08 +1000325 packet_send();
326 packet_write_wait();
Damien Miller874d77b2000-10-14 16:23:11 +1100327 xfree(methods);
328 } else {
329 /* do nothing, we did already send a reply */
Damien Millereba71ba2000-04-29 23:57:08 +1000330 }
Damien Millereba71ba2000-04-29 23:57:08 +1000331}
332
333int
Damien Miller874d77b2000-10-14 16:23:11 +1100334userauth_none(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000335{
Damien Miller874d77b2000-10-14 16:23:11 +1100336 /* disable method "none", only allowed one time */
337 Authmethod *m = authmethod_lookup("none");
338 if (m != NULL)
339 m->enabled = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000340 packet_done();
Damien Millerb8c656e2000-06-28 15:22:41 +1000341
Damien Miller874d77b2000-10-14 16:23:11 +1100342 if (authctxt->valid == 0)
343 return(0);
344
345#ifdef HAVE_CYGWIN
346 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
347 return(0);
348#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000349#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100350 return auth_pam_password(authctxt->pw, "");
Damien Millerb8c656e2000-06-28 15:22:41 +1000351#elif defined(HAVE_OSF_SIA)
Damien Miller874d77b2000-10-14 16:23:11 +1100352 return (sia_validate_user(NULL, saved_argc, saved_argv,
Damien Millerd425d4d2000-10-28 21:05:57 +1100353 get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER",
354 NULL, 0, NULL, "") == SIASUCCESS);
Damien Millerb8c656e2000-06-28 15:22:41 +1000355#else /* !HAVE_OSF_SIA && !USE_PAM */
Damien Miller874d77b2000-10-14 16:23:11 +1100356 return auth_password(authctxt->pw, "");
Damien Millereba71ba2000-04-29 23:57:08 +1000357#endif /* USE_PAM */
358}
Damien Miller874d77b2000-10-14 16:23:11 +1100359
Damien Millereba71ba2000-04-29 23:57:08 +1000360int
Damien Miller874d77b2000-10-14 16:23:11 +1100361userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000362{
363 char *password;
364 int authenticated = 0;
365 int change;
366 unsigned int len;
367 change = packet_get_char();
368 if (change)
369 log("password change not supported");
370 password = packet_get_string(&len);
371 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100372 if (authctxt->valid &&
373#ifdef HAVE_CYGWIN
374 check_nt_auth(1, authctxt->pw->pw_uid) &&
375#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000376#ifdef USE_PAM
Damien Miller874d77b2000-10-14 16:23:11 +1100377 auth_pam_password(authctxt->pw, password) == 1)
Damien Millerb8c656e2000-06-28 15:22:41 +1000378#elif defined(HAVE_OSF_SIA)
379 sia_validate_user(NULL, saved_argc, saved_argv,
Damien Millerd425d4d2000-10-28 21:05:57 +1100380 get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER",
381 NULL, 0, NULL, password) == SIASUCCESS)
Damien Millerb8c656e2000-06-28 15:22:41 +1000382#else /* !USE_PAM && !HAVE_OSF_SIA */
Damien Miller874d77b2000-10-14 16:23:11 +1100383 auth_password(authctxt->pw, password) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000384#endif /* USE_PAM */
385 authenticated = 1;
386 memset(password, 0, len);
387 xfree(password);
388 return authenticated;
389}
Damien Miller874d77b2000-10-14 16:23:11 +1100390
Damien Millereba71ba2000-04-29 23:57:08 +1000391int
Damien Miller874d77b2000-10-14 16:23:11 +1100392userauth_kbdint(Authctxt *authctxt)
393{
394 int authenticated = 0;
395 char *lang = NULL;
396 char *devs = NULL;
397
398 lang = packet_get_string(NULL);
399 devs = packet_get_string(NULL);
400 packet_done();
401
402 debug("keyboard-interactive language %s devs %s", lang, devs);
Damien Millerb8481582000-12-03 11:51:51 +1100403#ifdef USE_PAM
404 if (authenticated == 0)
405 authenticated = auth2_pam(authctxt);
406#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100407#ifdef SKEY
408 /* XXX hardcoded, we should look at devs */
Damien Millerb8481582000-12-03 11:51:51 +1100409 if (authenticated == 0)
410 if (options.skey_authentication != 0)
411 authenticated = auth2_skey(authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100412#endif
413 xfree(lang);
414 xfree(devs);
415#ifdef HAVE_CYGWIN
416 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
417 return(0);
418#endif
419 return authenticated;
420}
421
422int
423userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000424{
425 Buffer b;
426 Key *key;
427 char *pkalg, *pkblob, *sig;
428 unsigned int alen, blen, slen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100429 int have_sig, pktype;
Damien Millereba71ba2000-04-29 23:57:08 +1000430 int authenticated = 0;
431
Damien Miller874d77b2000-10-14 16:23:11 +1100432 if (!authctxt->valid) {
433 debug2("userauth_pubkey: disabled because of invalid user");
Damien Millereba71ba2000-04-29 23:57:08 +1000434 return 0;
435 }
436 have_sig = packet_get_char();
437 pkalg = packet_get_string(&alen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100438 pktype = key_type_from_name(pkalg);
439 if (pktype == KEY_UNSPEC) {
440 log("bad pkalg %s", pkalg);
Damien Miller874d77b2000-10-14 16:23:11 +1100441 xfree(pkalg);
Damien Millereba71ba2000-04-29 23:57:08 +1000442 return 0;
443 }
444 pkblob = packet_get_string(&blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100445 key = key_from_blob(pkblob, blen);
Damien Millereba71ba2000-04-29 23:57:08 +1000446 if (key != NULL) {
447 if (have_sig) {
448 sig = packet_get_string(&slen);
449 packet_done();
450 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100451 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000452 buffer_append(&b, session_id2, session_id2_len);
Damien Miller50a41ed2000-10-16 12:14:42 +1100453 } else {
454 buffer_put_string(&b, session_id2, session_id2_len);
Damien Miller6536c7d2000-06-22 21:32:31 +1000455 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000456 /* reconstruct packet */
Damien Millereba71ba2000-04-29 23:57:08 +1000457 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller874d77b2000-10-14 16:23:11 +1100458 buffer_put_cstring(&b, authctxt->user);
Damien Millerf6d9e222000-06-18 14:50:44 +1000459 buffer_put_cstring(&b,
460 datafellows & SSH_BUG_PUBKEYAUTH ?
461 "ssh-userauth" :
Damien Miller874d77b2000-10-14 16:23:11 +1100462 authctxt->service);
Damien Millerf6d9e222000-06-18 14:50:44 +1000463 buffer_put_cstring(&b, "publickey");
464 buffer_put_char(&b, have_sig);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100465 buffer_put_cstring(&b, key_ssh_name(key));
Damien Millerf6d9e222000-06-18 14:50:44 +1000466 buffer_put_string(&b, pkblob, blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100467#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000468 buffer_dump(&b);
469#endif
470 /* test for correct signature */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100471 if (user_key_allowed(authctxt->pw, key) &&
472 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
Damien Millereba71ba2000-04-29 23:57:08 +1000473 authenticated = 1;
474 buffer_clear(&b);
475 xfree(sig);
476 } else {
Damien Miller874d77b2000-10-14 16:23:11 +1100477 debug("test whether pkalg/pkblob are acceptable");
Damien Millereba71ba2000-04-29 23:57:08 +1000478 packet_done();
Damien Miller874d77b2000-10-14 16:23:11 +1100479
Damien Millereba71ba2000-04-29 23:57:08 +1000480 /* XXX fake reply and always send PK_OK ? */
Damien Millere247cc42000-05-07 12:03:14 +1000481 /*
482 * XXX this allows testing whether a user is allowed
483 * to login: if you happen to have a valid pubkey this
484 * message is sent. the message is NEVER sent at all
485 * if a user is not allowed to login. is this an
486 * issue? -markus
487 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100488 if (user_key_allowed(authctxt->pw, key)) {
Damien Millereba71ba2000-04-29 23:57:08 +1000489 packet_start(SSH2_MSG_USERAUTH_PK_OK);
490 packet_put_string(pkalg, alen);
491 packet_put_string(pkblob, blen);
492 packet_send();
493 packet_write_wait();
494 authenticated = -1;
495 }
496 }
Damien Miller874d77b2000-10-14 16:23:11 +1100497 if (authenticated != 1)
498 auth_clear_options();
Damien Millereba71ba2000-04-29 23:57:08 +1000499 key_free(key);
500 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100501 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
Damien Millereba71ba2000-04-29 23:57:08 +1000502 xfree(pkalg);
503 xfree(pkblob);
Damien Miller874d77b2000-10-14 16:23:11 +1100504#ifdef HAVE_CYGWIN
505 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
506 return(0);
507#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000508 return authenticated;
509}
510
Damien Miller874d77b2000-10-14 16:23:11 +1100511/* get current user */
Damien Millereba71ba2000-04-29 23:57:08 +1000512
513struct passwd*
514auth_get_user(void)
515{
Damien Miller874d77b2000-10-14 16:23:11 +1100516 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000517}
518
Damien Miller874d77b2000-10-14 16:23:11 +1100519#define DELIM ","
Damien Millereba71ba2000-04-29 23:57:08 +1000520
Damien Miller874d77b2000-10-14 16:23:11 +1100521char *
522authmethods_get(void)
523{
524 Authmethod *method = NULL;
525 unsigned int size = 0;
526 char *list;
527
528 for (method = authmethods; method->name != NULL; method++) {
529 if (strcmp(method->name, "none") == 0)
530 continue;
531 if (method->enabled != NULL && *(method->enabled) != 0) {
532 if (size != 0)
533 size += strlen(DELIM);
534 size += strlen(method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000535 }
536 }
Damien Miller874d77b2000-10-14 16:23:11 +1100537 size++; /* trailing '\0' */
538 list = xmalloc(size);
539 list[0] = '\0';
540
541 for (method = authmethods; method->name != NULL; method++) {
542 if (strcmp(method->name, "none") == 0)
543 continue;
544 if (method->enabled != NULL && *(method->enabled) != 0) {
545 if (list[0] != '\0')
546 strlcat(list, DELIM, size);
547 strlcat(list, method->name, size);
548 }
549 }
550 return list;
551}
552
553Authmethod *
554authmethod_lookup(const char *name)
555{
556 Authmethod *method = NULL;
557 if (name != NULL)
558 for (method = authmethods; method->name != NULL; method++)
559 if (method->enabled != NULL &&
560 *(method->enabled) != 0 &&
561 strcmp(name, method->name) == 0)
562 return method;
563 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
564 return NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000565}
566
567/* return 1 if user allows given key */
568int
Damien Miller0bc1bd82000-11-13 22:57:25 +1100569user_key_allowed(struct passwd *pw, Key *key)
Damien Millereba71ba2000-04-29 23:57:08 +1000570{
571 char line[8192], file[1024];
572 int found_key = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000573 FILE *f;
574 unsigned long linenum = 0;
575 struct stat st;
576 Key *found;
577
Damien Miller874d77b2000-10-14 16:23:11 +1100578 if (pw == NULL)
579 return 0;
580
Damien Millereba71ba2000-04-29 23:57:08 +1000581 /* Temporarily use the user's uid. */
582 temporarily_use_uid(pw->pw_uid);
583
584 /* The authorized keys. */
585 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
586 SSH_USER_PERMITTED_KEYS2);
587
588 /* Fail quietly if file does not exist */
589 if (stat(file, &st) < 0) {
590 /* Restore the privileged uid. */
591 restore_uid();
592 return 0;
593 }
594 /* Open the file containing the authorized keys. */
595 f = fopen(file, "r");
596 if (!f) {
597 /* Restore the privileged uid. */
598 restore_uid();
599 return 0;
600 }
601 if (options.strict_modes) {
602 int fail = 0;
603 char buf[1024];
604 /* Check open file in order to avoid open/stat races */
605 if (fstat(fileno(f), &st) < 0 ||
606 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
607 (st.st_mode & 022) != 0) {
Damien Millerd3444942000-09-30 14:20:03 +1100608 snprintf(buf, sizeof buf,
609 "%s authentication refused for %.100s: "
610 "bad ownership or modes for '%s'.",
611 key_type(key), pw->pw_name, file);
Damien Millereba71ba2000-04-29 23:57:08 +1000612 fail = 1;
613 } else {
614 /* Check path to SSH_USER_PERMITTED_KEYS */
615 int i;
616 static const char *check[] = {
617 "", SSH_USER_DIR, NULL
618 };
619 for (i = 0; check[i]; i++) {
620 snprintf(line, sizeof line, "%.500s/%.100s",
621 pw->pw_dir, check[i]);
622 if (stat(line, &st) < 0 ||
623 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
624 (st.st_mode & 022) != 0) {
625 snprintf(buf, sizeof buf,
Damien Millerd3444942000-09-30 14:20:03 +1100626 "%s authentication refused for %.100s: "
Damien Millereba71ba2000-04-29 23:57:08 +1000627 "bad ownership or modes for '%s'.",
Damien Millerd3444942000-09-30 14:20:03 +1100628 key_type(key), pw->pw_name, line);
Damien Millereba71ba2000-04-29 23:57:08 +1000629 fail = 1;
630 break;
631 }
632 }
633 }
634 if (fail) {
Damien Millereba71ba2000-04-29 23:57:08 +1000635 fclose(f);
Damien Miller37023962000-07-11 17:31:38 +1000636 log("%s",buf);
Damien Millereba71ba2000-04-29 23:57:08 +1000637 restore_uid();
638 return 0;
639 }
640 }
641 found_key = 0;
Damien Millerd3444942000-09-30 14:20:03 +1100642 found = key_new(key->type);
Damien Millereba71ba2000-04-29 23:57:08 +1000643
644 while (fgets(line, sizeof(line), f)) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000645 char *cp, *options = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000646 linenum++;
647 /* Skip leading whitespace, empty and comment lines. */
648 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
649 ;
650 if (!*cp || *cp == '\n' || *cp == '#')
651 continue;
Damien Millerf6d9e222000-06-18 14:50:44 +1000652
Damien Miller0bc1bd82000-11-13 22:57:25 +1100653 if (key_read(found, &cp) == -1) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000654 /* no key? check if there are options for this key */
655 int quoted = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100656 debug2("user_key_allowed: check options: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000657 options = cp;
658 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
659 if (*cp == '\\' && cp[1] == '"')
660 cp++; /* Skip both */
661 else if (*cp == '"')
662 quoted = !quoted;
663 }
664 /* Skip remaining whitespace. */
665 for (; *cp == ' ' || *cp == '\t'; cp++)
666 ;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100667 if (key_read(found, &cp) == -1) {
668 debug2("user_key_allowed: advance: '%s'", cp);
Damien Millerf6d9e222000-06-18 14:50:44 +1000669 /* still no key? advance to next line*/
670 continue;
671 }
672 }
673 if (key_equal(found, key) &&
674 auth_parse_options(pw, options, linenum) == 1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000675 found_key = 1;
676 debug("matching key found: file %s, line %ld",
677 file, linenum);
678 break;
679 }
680 }
681 restore_uid();
682 fclose(f);
683 key_free(found);
684 return found_key;
685}
Damien Miller874d77b2000-10-14 16:23:11 +1100686
687struct passwd *
688pwcopy(struct passwd *pw)
689{
690 struct passwd *copy = xmalloc(sizeof(*copy));
691 memset(copy, 0, sizeof(*copy));
692 copy->pw_name = xstrdup(pw->pw_name);
693 copy->pw_passwd = xstrdup(pw->pw_passwd);
694 copy->pw_uid = pw->pw_uid;
695 copy->pw_gid = pw->pw_gid;
696#ifdef HAVE_PW_CLASS_IN_PASSWD
697 copy->pw_class = xstrdup(pw->pw_class);
698#endif
699 copy->pw_dir = xstrdup(pw->pw_dir);
700 copy->pw_shell = xstrdup(pw->pw_shell);
701 return copy;
702}