10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * Copyright (c) 2000 Markus Friedl. All rights reserved.
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
50Sstevel@tonic-gate * modification, are permitted provided that the following conditions
60Sstevel@tonic-gate * are met:
70Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
80Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
90Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
100Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
110Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
140Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
150Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
160Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
170Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
180Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
190Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
200Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
210Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
220Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate /*
25*7574SJan.Pechanec@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
260Sstevel@tonic-gate * Use is subject to license terms.
270Sstevel@tonic-gate */
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include "includes.h"
300Sstevel@tonic-gate RCSID("$OpenBSD: auth2-pubkey.c,v 1.2 2002/05/31 11:35:15 markus Exp $");
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include "ssh2.h"
330Sstevel@tonic-gate #include "xmalloc.h"
340Sstevel@tonic-gate #include "packet.h"
350Sstevel@tonic-gate #include "buffer.h"
360Sstevel@tonic-gate #include "log.h"
370Sstevel@tonic-gate #include "servconf.h"
380Sstevel@tonic-gate #include "compat.h"
390Sstevel@tonic-gate #include "bufaux.h"
400Sstevel@tonic-gate #include "auth.h"
410Sstevel@tonic-gate #include "key.h"
420Sstevel@tonic-gate #include "pathnames.h"
430Sstevel@tonic-gate #include "uidswap.h"
440Sstevel@tonic-gate #include "auth-options.h"
450Sstevel@tonic-gate #include "canohost.h"
460Sstevel@tonic-gate
470Sstevel@tonic-gate #ifdef USE_PAM
480Sstevel@tonic-gate #include <security/pam_appl.h>
490Sstevel@tonic-gate #include "auth-pam.h"
500Sstevel@tonic-gate #endif /* USE_PAM */
510Sstevel@tonic-gate
520Sstevel@tonic-gate /* import */
530Sstevel@tonic-gate extern ServerOptions options;
540Sstevel@tonic-gate extern u_char *session_id2;
550Sstevel@tonic-gate extern int session_id2_len;
560Sstevel@tonic-gate
570Sstevel@tonic-gate static void
userauth_pubkey(Authctxt * authctxt)580Sstevel@tonic-gate userauth_pubkey(Authctxt *authctxt)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate Buffer b;
610Sstevel@tonic-gate Key *key = NULL;
620Sstevel@tonic-gate char *pkalg;
630Sstevel@tonic-gate u_char *pkblob, *sig;
640Sstevel@tonic-gate u_int alen, blen, slen;
650Sstevel@tonic-gate int have_sig, pktype;
660Sstevel@tonic-gate int authenticated = 0;
670Sstevel@tonic-gate
680Sstevel@tonic-gate if (!authctxt || !authctxt->method)
690Sstevel@tonic-gate fatal("%s: missing context", __func__);
700Sstevel@tonic-gate
710Sstevel@tonic-gate have_sig = packet_get_char();
720Sstevel@tonic-gate if (datafellows & SSH_BUG_PKAUTH) {
730Sstevel@tonic-gate debug2("userauth_pubkey: SSH_BUG_PKAUTH");
740Sstevel@tonic-gate /* no explicit pkalg given */
750Sstevel@tonic-gate pkblob = packet_get_string(&blen);
760Sstevel@tonic-gate buffer_init(&b);
770Sstevel@tonic-gate buffer_append(&b, pkblob, blen);
780Sstevel@tonic-gate /* so we have to extract the pkalg from the pkblob */
790Sstevel@tonic-gate pkalg = buffer_get_string(&b, &alen);
800Sstevel@tonic-gate buffer_free(&b);
810Sstevel@tonic-gate } else {
820Sstevel@tonic-gate pkalg = packet_get_string(&alen);
830Sstevel@tonic-gate pkblob = packet_get_string(&blen);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate pktype = key_type_from_name(pkalg);
860Sstevel@tonic-gate if (pktype == KEY_UNSPEC) {
870Sstevel@tonic-gate /* this is perfectly legal */
880Sstevel@tonic-gate log("userauth_pubkey: unsupported public key algorithm: %s",
890Sstevel@tonic-gate pkalg);
900Sstevel@tonic-gate goto done;
910Sstevel@tonic-gate }
920Sstevel@tonic-gate key = key_from_blob(pkblob, blen);
930Sstevel@tonic-gate if (key == NULL) {
940Sstevel@tonic-gate error("userauth_pubkey: cannot decode key: %s", pkalg);
950Sstevel@tonic-gate goto done;
960Sstevel@tonic-gate }
970Sstevel@tonic-gate if (key->type != pktype) {
980Sstevel@tonic-gate error("userauth_pubkey: type mismatch for decoded key "
990Sstevel@tonic-gate "(received %d, expected %d)", key->type, pktype);
1000Sstevel@tonic-gate goto done;
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate /* Detect and count abandonment */
1040Sstevel@tonic-gate if (authctxt->method->method_data) {
1050Sstevel@tonic-gate Key *prev_key;
1060Sstevel@tonic-gate unsigned char *prev_pkblob;
1070Sstevel@tonic-gate int prev_blen;
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate * Check for earlier test of a key that was allowed but
1110Sstevel@tonic-gate * not followed up with a pubkey req for the same pubkey
1120Sstevel@tonic-gate * and with a signature.
1130Sstevel@tonic-gate */
1140Sstevel@tonic-gate prev_key = authctxt->method->method_data;
1150Sstevel@tonic-gate if ((prev_blen = key_to_blob(prev_key,
1160Sstevel@tonic-gate &prev_pkblob, NULL))) {
1170Sstevel@tonic-gate if (prev_blen != blen ||
1180Sstevel@tonic-gate memcmp(prev_pkblob, pkblob, blen) != 0) {
1190Sstevel@tonic-gate authctxt->method->abandons++;
1200Sstevel@tonic-gate authctxt->method->attempts++;
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate key_free(prev_key);
1240Sstevel@tonic-gate authctxt->method->method_data = NULL;
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate if (have_sig) {
1280Sstevel@tonic-gate sig = packet_get_string(&slen);
1290Sstevel@tonic-gate packet_check_eom();
1300Sstevel@tonic-gate buffer_init(&b);
1310Sstevel@tonic-gate if (datafellows & SSH_OLD_SESSIONID) {
1320Sstevel@tonic-gate buffer_append(&b, session_id2, session_id2_len);
1330Sstevel@tonic-gate } else {
1340Sstevel@tonic-gate buffer_put_string(&b, session_id2, session_id2_len);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate /* reconstruct packet */
1370Sstevel@tonic-gate buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1380Sstevel@tonic-gate buffer_put_cstring(&b, authctxt->user);
1390Sstevel@tonic-gate buffer_put_cstring(&b,
1400Sstevel@tonic-gate datafellows & SSH_BUG_PKSERVICE ?
1410Sstevel@tonic-gate "ssh-userauth" :
1420Sstevel@tonic-gate authctxt->service);
1430Sstevel@tonic-gate if (datafellows & SSH_BUG_PKAUTH) {
1440Sstevel@tonic-gate buffer_put_char(&b, have_sig);
1450Sstevel@tonic-gate } else {
1460Sstevel@tonic-gate buffer_put_cstring(&b, "publickey");
1470Sstevel@tonic-gate buffer_put_char(&b, have_sig);
1480Sstevel@tonic-gate buffer_put_cstring(&b, pkalg);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate buffer_put_string(&b, pkblob, blen);
1510Sstevel@tonic-gate #ifdef DEBUG_PK
1520Sstevel@tonic-gate buffer_dump(&b);
1530Sstevel@tonic-gate #endif
1540Sstevel@tonic-gate /* test for correct signature */
1555562Sjp161948 if (user_key_allowed(authctxt->pw, key) &&
1565562Sjp161948 key_verify(key, sig, slen, buffer_ptr(&b),
1575562Sjp161948 buffer_len(&b)) == 1) {
1580Sstevel@tonic-gate authenticated = 1;
1595562Sjp161948 }
1600Sstevel@tonic-gate authctxt->method->postponed = 0;
161*7574SJan.Pechanec@Sun.COM buffer_free(&b);
1620Sstevel@tonic-gate xfree(sig);
1630Sstevel@tonic-gate } else {
1640Sstevel@tonic-gate debug("test whether pkalg/pkblob are acceptable");
1650Sstevel@tonic-gate packet_check_eom();
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate /* XXX fake reply and always send PK_OK ? */
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate * XXX this allows testing whether a user is allowed
1700Sstevel@tonic-gate * to login: if you happen to have a valid pubkey this
1710Sstevel@tonic-gate * message is sent. the message is NEVER sent at all
1720Sstevel@tonic-gate * if a user is not allowed to login. is this an
1730Sstevel@tonic-gate * issue? -markus
1740Sstevel@tonic-gate */
1755562Sjp161948 if (user_key_allowed(authctxt->pw, key)) {
1760Sstevel@tonic-gate packet_start(SSH2_MSG_USERAUTH_PK_OK);
1770Sstevel@tonic-gate packet_put_string(pkalg, alen);
1780Sstevel@tonic-gate packet_put_string(pkblob, blen);
1790Sstevel@tonic-gate packet_send();
1800Sstevel@tonic-gate packet_write_wait();
1810Sstevel@tonic-gate authctxt->method->postponed = 1;
1820Sstevel@tonic-gate /*
1830Sstevel@tonic-gate * Remember key that was tried so we can
1840Sstevel@tonic-gate * correctly detect abandonment. See above.
1850Sstevel@tonic-gate */
1860Sstevel@tonic-gate authctxt->method->method_data = (void *) key;
1870Sstevel@tonic-gate key = NULL;
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate if (authenticated != 1)
1910Sstevel@tonic-gate auth_clear_options();
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate done:
1940Sstevel@tonic-gate /*
1950Sstevel@tonic-gate * XXX TODO: add config options for specifying users for whom
1960Sstevel@tonic-gate * this userauth is insufficient and what userauths may
1970Sstevel@tonic-gate * continue.
1980Sstevel@tonic-gate */
1990Sstevel@tonic-gate #ifdef USE_PAM
2000Sstevel@tonic-gate if (authenticated) {
2010Sstevel@tonic-gate if (!do_pam_non_initial_userauth(authctxt))
2020Sstevel@tonic-gate authenticated = 0;
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate #endif /* USE_PAM */
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
2070Sstevel@tonic-gate if (key != NULL)
2080Sstevel@tonic-gate key_free(key);
2090Sstevel@tonic-gate xfree(pkalg);
2100Sstevel@tonic-gate xfree(pkblob);
2110Sstevel@tonic-gate #ifdef HAVE_CYGWIN
2120Sstevel@tonic-gate if (check_nt_auth(0, authctxt->pw) == 0)
2130Sstevel@tonic-gate return;
2140Sstevel@tonic-gate #endif
2150Sstevel@tonic-gate if (authenticated)
2160Sstevel@tonic-gate authctxt->method->authenticated = 1;
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate /* return 1 if user allows given key */
2200Sstevel@tonic-gate static int
user_key_allowed2(struct passwd * pw,Key * key,char * file)2210Sstevel@tonic-gate user_key_allowed2(struct passwd *pw, Key *key, char *file)
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate char line[8192];
2240Sstevel@tonic-gate int found_key = 0;
2250Sstevel@tonic-gate FILE *f;
2260Sstevel@tonic-gate u_long linenum = 0;
2270Sstevel@tonic-gate struct stat st;
2280Sstevel@tonic-gate Key *found;
2290Sstevel@tonic-gate char *fp;
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate if (pw == NULL)
2320Sstevel@tonic-gate return 0;
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate /* Temporarily use the user's uid. */
2350Sstevel@tonic-gate temporarily_use_uid(pw);
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate debug("trying public key file %s", file);
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate /* Fail quietly if file does not exist */
2400Sstevel@tonic-gate if (stat(file, &st) < 0) {
2410Sstevel@tonic-gate /* Restore the privileged uid. */
2420Sstevel@tonic-gate restore_uid();
2430Sstevel@tonic-gate return 0;
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate /* Open the file containing the authorized keys. */
2460Sstevel@tonic-gate f = fopen(file, "r");
2470Sstevel@tonic-gate if (!f) {
2480Sstevel@tonic-gate /* Restore the privileged uid. */
2490Sstevel@tonic-gate restore_uid();
2500Sstevel@tonic-gate return 0;
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate if (options.strict_modes &&
2530Sstevel@tonic-gate secure_filename(f, file, pw, line, sizeof(line)) != 0) {
2540Sstevel@tonic-gate (void) fclose(f);
2550Sstevel@tonic-gate log("Authentication refused: %s", line);
2560Sstevel@tonic-gate restore_uid();
2570Sstevel@tonic-gate return 0;
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate
2600Sstevel@tonic-gate found_key = 0;
2610Sstevel@tonic-gate found = key_new(key->type);
2620Sstevel@tonic-gate
2630Sstevel@tonic-gate while (fgets(line, sizeof(line), f)) {
2640Sstevel@tonic-gate char *cp, *options = NULL;
2650Sstevel@tonic-gate linenum++;
2660Sstevel@tonic-gate /* Skip leading whitespace, empty and comment lines. */
2670Sstevel@tonic-gate for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
2680Sstevel@tonic-gate ;
2690Sstevel@tonic-gate if (!*cp || *cp == '\n' || *cp == '#')
2700Sstevel@tonic-gate continue;
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate if (key_read(found, &cp) != 1) {
2730Sstevel@tonic-gate /* no key? check if there are options for this key */
2740Sstevel@tonic-gate int quoted = 0;
2750Sstevel@tonic-gate debug2("user_key_allowed: check options: '%s'", cp);
2760Sstevel@tonic-gate options = cp;
2770Sstevel@tonic-gate for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
2780Sstevel@tonic-gate if (*cp == '\\' && cp[1] == '"')
2790Sstevel@tonic-gate cp++; /* Skip both */
2800Sstevel@tonic-gate else if (*cp == '"')
2810Sstevel@tonic-gate quoted = !quoted;
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate /* Skip remaining whitespace. */
2840Sstevel@tonic-gate for (; *cp == ' ' || *cp == '\t'; cp++)
2850Sstevel@tonic-gate ;
2860Sstevel@tonic-gate if (key_read(found, &cp) != 1) {
2870Sstevel@tonic-gate debug2("user_key_allowed: advance: '%s'", cp);
2880Sstevel@tonic-gate /* still no key? advance to next line*/
2890Sstevel@tonic-gate continue;
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate if (key_equal(found, key) &&
2930Sstevel@tonic-gate auth_parse_options(pw, options, file, linenum) == 1) {
2940Sstevel@tonic-gate found_key = 1;
2950Sstevel@tonic-gate debug("matching key found: file %s, line %lu",
2960Sstevel@tonic-gate file, linenum);
2970Sstevel@tonic-gate fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
2980Sstevel@tonic-gate verbose("Found matching %s key: %s",
2990Sstevel@tonic-gate key_type(found), fp);
3000Sstevel@tonic-gate xfree(fp);
3010Sstevel@tonic-gate break;
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate restore_uid();
3050Sstevel@tonic-gate (void) fclose(f);
3060Sstevel@tonic-gate key_free(found);
3070Sstevel@tonic-gate if (!found_key)
3080Sstevel@tonic-gate debug2("key not found");
3090Sstevel@tonic-gate return found_key;
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate /* check whether given key is in .ssh/authorized_keys* */
3130Sstevel@tonic-gate int
user_key_allowed(struct passwd * pw,Key * key)3140Sstevel@tonic-gate user_key_allowed(struct passwd *pw, Key *key)
3150Sstevel@tonic-gate {
3160Sstevel@tonic-gate int success;
3170Sstevel@tonic-gate char *file;
3180Sstevel@tonic-gate
3190Sstevel@tonic-gate if (pw == NULL)
3200Sstevel@tonic-gate return 0;
3210Sstevel@tonic-gate
3220Sstevel@tonic-gate file = authorized_keys_file(pw);
3230Sstevel@tonic-gate success = user_key_allowed2(pw, key, file);
3240Sstevel@tonic-gate xfree(file);
3250Sstevel@tonic-gate if (success)
3260Sstevel@tonic-gate return success;
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate /* try suffix "2" for backward compat, too */
3290Sstevel@tonic-gate file = authorized_keys_file2(pw);
3300Sstevel@tonic-gate success = user_key_allowed2(pw, key, file);
3310Sstevel@tonic-gate xfree(file);
3320Sstevel@tonic-gate return success;
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate static
3360Sstevel@tonic-gate void
userauth_pubkey_abandon(Authctxt * authctxt,Authmethod * method)3370Sstevel@tonic-gate userauth_pubkey_abandon(Authctxt *authctxt, Authmethod *method)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate if (!authctxt || !method)
3400Sstevel@tonic-gate return;
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate if (method->method_data) {
3430Sstevel@tonic-gate method->abandons++;
3440Sstevel@tonic-gate method->attempts++;
3450Sstevel@tonic-gate key_free((Key *) method->method_data);
3460Sstevel@tonic-gate method->method_data = NULL;
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate Authmethod method_pubkey = {
3510Sstevel@tonic-gate "publickey",
3520Sstevel@tonic-gate &options.pubkey_authentication,
3530Sstevel@tonic-gate userauth_pubkey,
3540Sstevel@tonic-gate userauth_pubkey_abandon,
3550Sstevel@tonic-gate NULL, NULL, /* method data and hist data */
3560Sstevel@tonic-gate 0, /* not initial userauth */
3570Sstevel@tonic-gate 0, 0, 0, /* counters */
3580Sstevel@tonic-gate 0, 0, 0, 0, 0, 0 /* state */
3590Sstevel@tonic-gate };
360