1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright (c) 2000 Markus Friedl. All rights reserved. 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 5*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 6*0Sstevel@tonic-gate * are met: 7*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 8*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 9*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 10*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 11*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 12*0Sstevel@tonic-gate * 13*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14*0Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15*0Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16*0Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17*0Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 18*0Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 19*0Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20*0Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21*0Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22*0Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23*0Sstevel@tonic-gate */ 24*0Sstevel@tonic-gate /* 25*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 26*0Sstevel@tonic-gate * Use is subject to license terms. 27*0Sstevel@tonic-gate */ 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include "includes.h" 30*0Sstevel@tonic-gate RCSID("$OpenBSD: auth2-pubkey.c,v 1.2 2002/05/31 11:35:15 markus Exp $"); 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 33*0Sstevel@tonic-gate 34*0Sstevel@tonic-gate #include "ssh2.h" 35*0Sstevel@tonic-gate #include "xmalloc.h" 36*0Sstevel@tonic-gate #include "packet.h" 37*0Sstevel@tonic-gate #include "buffer.h" 38*0Sstevel@tonic-gate #include "log.h" 39*0Sstevel@tonic-gate #include "servconf.h" 40*0Sstevel@tonic-gate #include "compat.h" 41*0Sstevel@tonic-gate #include "bufaux.h" 42*0Sstevel@tonic-gate #include "auth.h" 43*0Sstevel@tonic-gate #include "key.h" 44*0Sstevel@tonic-gate #include "pathnames.h" 45*0Sstevel@tonic-gate #include "uidswap.h" 46*0Sstevel@tonic-gate #include "auth-options.h" 47*0Sstevel@tonic-gate #include "canohost.h" 48*0Sstevel@tonic-gate #include "monitor_wrap.h" 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate #ifdef USE_PAM 51*0Sstevel@tonic-gate #include <security/pam_appl.h> 52*0Sstevel@tonic-gate #include "auth-pam.h" 53*0Sstevel@tonic-gate #endif /* USE_PAM */ 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate /* import */ 56*0Sstevel@tonic-gate extern ServerOptions options; 57*0Sstevel@tonic-gate extern u_char *session_id2; 58*0Sstevel@tonic-gate extern int session_id2_len; 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate static void 61*0Sstevel@tonic-gate userauth_pubkey(Authctxt *authctxt) 62*0Sstevel@tonic-gate { 63*0Sstevel@tonic-gate Buffer b; 64*0Sstevel@tonic-gate Key *key = NULL; 65*0Sstevel@tonic-gate char *pkalg; 66*0Sstevel@tonic-gate u_char *pkblob, *sig; 67*0Sstevel@tonic-gate u_int alen, blen, slen; 68*0Sstevel@tonic-gate int have_sig, pktype; 69*0Sstevel@tonic-gate int authenticated = 0; 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate if (!authctxt || !authctxt->method) 72*0Sstevel@tonic-gate fatal("%s: missing context", __func__); 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate have_sig = packet_get_char(); 75*0Sstevel@tonic-gate if (datafellows & SSH_BUG_PKAUTH) { 76*0Sstevel@tonic-gate debug2("userauth_pubkey: SSH_BUG_PKAUTH"); 77*0Sstevel@tonic-gate /* no explicit pkalg given */ 78*0Sstevel@tonic-gate pkblob = packet_get_string(&blen); 79*0Sstevel@tonic-gate buffer_init(&b); 80*0Sstevel@tonic-gate buffer_append(&b, pkblob, blen); 81*0Sstevel@tonic-gate /* so we have to extract the pkalg from the pkblob */ 82*0Sstevel@tonic-gate pkalg = buffer_get_string(&b, &alen); 83*0Sstevel@tonic-gate buffer_free(&b); 84*0Sstevel@tonic-gate } else { 85*0Sstevel@tonic-gate pkalg = packet_get_string(&alen); 86*0Sstevel@tonic-gate pkblob = packet_get_string(&blen); 87*0Sstevel@tonic-gate } 88*0Sstevel@tonic-gate pktype = key_type_from_name(pkalg); 89*0Sstevel@tonic-gate if (pktype == KEY_UNSPEC) { 90*0Sstevel@tonic-gate /* this is perfectly legal */ 91*0Sstevel@tonic-gate log("userauth_pubkey: unsupported public key algorithm: %s", 92*0Sstevel@tonic-gate pkalg); 93*0Sstevel@tonic-gate goto done; 94*0Sstevel@tonic-gate } 95*0Sstevel@tonic-gate key = key_from_blob(pkblob, blen); 96*0Sstevel@tonic-gate if (key == NULL) { 97*0Sstevel@tonic-gate error("userauth_pubkey: cannot decode key: %s", pkalg); 98*0Sstevel@tonic-gate goto done; 99*0Sstevel@tonic-gate } 100*0Sstevel@tonic-gate if (key->type != pktype) { 101*0Sstevel@tonic-gate error("userauth_pubkey: type mismatch for decoded key " 102*0Sstevel@tonic-gate "(received %d, expected %d)", key->type, pktype); 103*0Sstevel@tonic-gate goto done; 104*0Sstevel@tonic-gate } 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate /* Detect and count abandonment */ 107*0Sstevel@tonic-gate if (authctxt->method->method_data) { 108*0Sstevel@tonic-gate Key *prev_key; 109*0Sstevel@tonic-gate unsigned char *prev_pkblob; 110*0Sstevel@tonic-gate int prev_blen; 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate /* 113*0Sstevel@tonic-gate * Check for earlier test of a key that was allowed but 114*0Sstevel@tonic-gate * not followed up with a pubkey req for the same pubkey 115*0Sstevel@tonic-gate * and with a signature. 116*0Sstevel@tonic-gate */ 117*0Sstevel@tonic-gate prev_key = authctxt->method->method_data; 118*0Sstevel@tonic-gate if ((prev_blen = key_to_blob(prev_key, 119*0Sstevel@tonic-gate &prev_pkblob, NULL))) { 120*0Sstevel@tonic-gate if (prev_blen != blen || 121*0Sstevel@tonic-gate memcmp(prev_pkblob, pkblob, blen) != 0) { 122*0Sstevel@tonic-gate authctxt->method->abandons++; 123*0Sstevel@tonic-gate authctxt->method->attempts++; 124*0Sstevel@tonic-gate } 125*0Sstevel@tonic-gate } 126*0Sstevel@tonic-gate key_free(prev_key); 127*0Sstevel@tonic-gate authctxt->method->method_data = NULL; 128*0Sstevel@tonic-gate } 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gate if (have_sig) { 131*0Sstevel@tonic-gate sig = packet_get_string(&slen); 132*0Sstevel@tonic-gate packet_check_eom(); 133*0Sstevel@tonic-gate buffer_init(&b); 134*0Sstevel@tonic-gate if (datafellows & SSH_OLD_SESSIONID) { 135*0Sstevel@tonic-gate buffer_append(&b, session_id2, session_id2_len); 136*0Sstevel@tonic-gate } else { 137*0Sstevel@tonic-gate buffer_put_string(&b, session_id2, session_id2_len); 138*0Sstevel@tonic-gate } 139*0Sstevel@tonic-gate /* reconstruct packet */ 140*0Sstevel@tonic-gate buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); 141*0Sstevel@tonic-gate buffer_put_cstring(&b, authctxt->user); 142*0Sstevel@tonic-gate buffer_put_cstring(&b, 143*0Sstevel@tonic-gate datafellows & SSH_BUG_PKSERVICE ? 144*0Sstevel@tonic-gate "ssh-userauth" : 145*0Sstevel@tonic-gate authctxt->service); 146*0Sstevel@tonic-gate if (datafellows & SSH_BUG_PKAUTH) { 147*0Sstevel@tonic-gate buffer_put_char(&b, have_sig); 148*0Sstevel@tonic-gate } else { 149*0Sstevel@tonic-gate buffer_put_cstring(&b, "publickey"); 150*0Sstevel@tonic-gate buffer_put_char(&b, have_sig); 151*0Sstevel@tonic-gate buffer_put_cstring(&b, pkalg); 152*0Sstevel@tonic-gate } 153*0Sstevel@tonic-gate buffer_put_string(&b, pkblob, blen); 154*0Sstevel@tonic-gate #ifdef DEBUG_PK 155*0Sstevel@tonic-gate buffer_dump(&b); 156*0Sstevel@tonic-gate #endif 157*0Sstevel@tonic-gate /* test for correct signature */ 158*0Sstevel@tonic-gate if (PRIVSEP(user_key_allowed(authctxt->pw, key)) && 159*0Sstevel@tonic-gate PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b), 160*0Sstevel@tonic-gate buffer_len(&b))) == 1) 161*0Sstevel@tonic-gate authenticated = 1; 162*0Sstevel@tonic-gate authctxt->method->postponed = 0; 163*0Sstevel@tonic-gate buffer_clear(&b); 164*0Sstevel@tonic-gate xfree(sig); 165*0Sstevel@tonic-gate } else { 166*0Sstevel@tonic-gate debug("test whether pkalg/pkblob are acceptable"); 167*0Sstevel@tonic-gate packet_check_eom(); 168*0Sstevel@tonic-gate 169*0Sstevel@tonic-gate /* XXX fake reply and always send PK_OK ? */ 170*0Sstevel@tonic-gate /* 171*0Sstevel@tonic-gate * XXX this allows testing whether a user is allowed 172*0Sstevel@tonic-gate * to login: if you happen to have a valid pubkey this 173*0Sstevel@tonic-gate * message is sent. the message is NEVER sent at all 174*0Sstevel@tonic-gate * if a user is not allowed to login. is this an 175*0Sstevel@tonic-gate * issue? -markus 176*0Sstevel@tonic-gate */ 177*0Sstevel@tonic-gate if (PRIVSEP(user_key_allowed(authctxt->pw, key))) { 178*0Sstevel@tonic-gate packet_start(SSH2_MSG_USERAUTH_PK_OK); 179*0Sstevel@tonic-gate packet_put_string(pkalg, alen); 180*0Sstevel@tonic-gate packet_put_string(pkblob, blen); 181*0Sstevel@tonic-gate packet_send(); 182*0Sstevel@tonic-gate packet_write_wait(); 183*0Sstevel@tonic-gate authctxt->method->postponed = 1; 184*0Sstevel@tonic-gate /* 185*0Sstevel@tonic-gate * Remember key that was tried so we can 186*0Sstevel@tonic-gate * correctly detect abandonment. See above. 187*0Sstevel@tonic-gate */ 188*0Sstevel@tonic-gate authctxt->method->method_data = (void *) key; 189*0Sstevel@tonic-gate key = NULL; 190*0Sstevel@tonic-gate } 191*0Sstevel@tonic-gate } 192*0Sstevel@tonic-gate if (authenticated != 1) 193*0Sstevel@tonic-gate auth_clear_options(); 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate done: 196*0Sstevel@tonic-gate /* 197*0Sstevel@tonic-gate * XXX TODO: add config options for specifying users for whom 198*0Sstevel@tonic-gate * this userauth is insufficient and what userauths may 199*0Sstevel@tonic-gate * continue. 200*0Sstevel@tonic-gate */ 201*0Sstevel@tonic-gate #ifdef USE_PAM 202*0Sstevel@tonic-gate if (authenticated) { 203*0Sstevel@tonic-gate if (!do_pam_non_initial_userauth(authctxt)) 204*0Sstevel@tonic-gate authenticated = 0; 205*0Sstevel@tonic-gate } 206*0Sstevel@tonic-gate #endif /* USE_PAM */ 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg); 209*0Sstevel@tonic-gate if (key != NULL) 210*0Sstevel@tonic-gate key_free(key); 211*0Sstevel@tonic-gate xfree(pkalg); 212*0Sstevel@tonic-gate xfree(pkblob); 213*0Sstevel@tonic-gate #ifdef HAVE_CYGWIN 214*0Sstevel@tonic-gate if (check_nt_auth(0, authctxt->pw) == 0) 215*0Sstevel@tonic-gate return; 216*0Sstevel@tonic-gate #endif 217*0Sstevel@tonic-gate if (authenticated) 218*0Sstevel@tonic-gate authctxt->method->authenticated = 1; 219*0Sstevel@tonic-gate } 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate /* return 1 if user allows given key */ 222*0Sstevel@tonic-gate static int 223*0Sstevel@tonic-gate user_key_allowed2(struct passwd *pw, Key *key, char *file) 224*0Sstevel@tonic-gate { 225*0Sstevel@tonic-gate char line[8192]; 226*0Sstevel@tonic-gate int found_key = 0; 227*0Sstevel@tonic-gate FILE *f; 228*0Sstevel@tonic-gate u_long linenum = 0; 229*0Sstevel@tonic-gate struct stat st; 230*0Sstevel@tonic-gate Key *found; 231*0Sstevel@tonic-gate char *fp; 232*0Sstevel@tonic-gate 233*0Sstevel@tonic-gate if (pw == NULL) 234*0Sstevel@tonic-gate return 0; 235*0Sstevel@tonic-gate 236*0Sstevel@tonic-gate /* Temporarily use the user's uid. */ 237*0Sstevel@tonic-gate temporarily_use_uid(pw); 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gate debug("trying public key file %s", file); 240*0Sstevel@tonic-gate 241*0Sstevel@tonic-gate /* Fail quietly if file does not exist */ 242*0Sstevel@tonic-gate if (stat(file, &st) < 0) { 243*0Sstevel@tonic-gate /* Restore the privileged uid. */ 244*0Sstevel@tonic-gate restore_uid(); 245*0Sstevel@tonic-gate return 0; 246*0Sstevel@tonic-gate } 247*0Sstevel@tonic-gate /* Open the file containing the authorized keys. */ 248*0Sstevel@tonic-gate f = fopen(file, "r"); 249*0Sstevel@tonic-gate if (!f) { 250*0Sstevel@tonic-gate /* Restore the privileged uid. */ 251*0Sstevel@tonic-gate restore_uid(); 252*0Sstevel@tonic-gate return 0; 253*0Sstevel@tonic-gate } 254*0Sstevel@tonic-gate if (options.strict_modes && 255*0Sstevel@tonic-gate secure_filename(f, file, pw, line, sizeof(line)) != 0) { 256*0Sstevel@tonic-gate (void) fclose(f); 257*0Sstevel@tonic-gate log("Authentication refused: %s", line); 258*0Sstevel@tonic-gate restore_uid(); 259*0Sstevel@tonic-gate return 0; 260*0Sstevel@tonic-gate } 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gate found_key = 0; 263*0Sstevel@tonic-gate found = key_new(key->type); 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate while (fgets(line, sizeof(line), f)) { 266*0Sstevel@tonic-gate char *cp, *options = NULL; 267*0Sstevel@tonic-gate linenum++; 268*0Sstevel@tonic-gate /* Skip leading whitespace, empty and comment lines. */ 269*0Sstevel@tonic-gate for (cp = line; *cp == ' ' || *cp == '\t'; cp++) 270*0Sstevel@tonic-gate ; 271*0Sstevel@tonic-gate if (!*cp || *cp == '\n' || *cp == '#') 272*0Sstevel@tonic-gate continue; 273*0Sstevel@tonic-gate 274*0Sstevel@tonic-gate if (key_read(found, &cp) != 1) { 275*0Sstevel@tonic-gate /* no key? check if there are options for this key */ 276*0Sstevel@tonic-gate int quoted = 0; 277*0Sstevel@tonic-gate debug2("user_key_allowed: check options: '%s'", cp); 278*0Sstevel@tonic-gate options = cp; 279*0Sstevel@tonic-gate for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { 280*0Sstevel@tonic-gate if (*cp == '\\' && cp[1] == '"') 281*0Sstevel@tonic-gate cp++; /* Skip both */ 282*0Sstevel@tonic-gate else if (*cp == '"') 283*0Sstevel@tonic-gate quoted = !quoted; 284*0Sstevel@tonic-gate } 285*0Sstevel@tonic-gate /* Skip remaining whitespace. */ 286*0Sstevel@tonic-gate for (; *cp == ' ' || *cp == '\t'; cp++) 287*0Sstevel@tonic-gate ; 288*0Sstevel@tonic-gate if (key_read(found, &cp) != 1) { 289*0Sstevel@tonic-gate debug2("user_key_allowed: advance: '%s'", cp); 290*0Sstevel@tonic-gate /* still no key? advance to next line*/ 291*0Sstevel@tonic-gate continue; 292*0Sstevel@tonic-gate } 293*0Sstevel@tonic-gate } 294*0Sstevel@tonic-gate if (key_equal(found, key) && 295*0Sstevel@tonic-gate auth_parse_options(pw, options, file, linenum) == 1) { 296*0Sstevel@tonic-gate found_key = 1; 297*0Sstevel@tonic-gate debug("matching key found: file %s, line %lu", 298*0Sstevel@tonic-gate file, linenum); 299*0Sstevel@tonic-gate fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX); 300*0Sstevel@tonic-gate verbose("Found matching %s key: %s", 301*0Sstevel@tonic-gate key_type(found), fp); 302*0Sstevel@tonic-gate xfree(fp); 303*0Sstevel@tonic-gate break; 304*0Sstevel@tonic-gate } 305*0Sstevel@tonic-gate } 306*0Sstevel@tonic-gate restore_uid(); 307*0Sstevel@tonic-gate (void) fclose(f); 308*0Sstevel@tonic-gate key_free(found); 309*0Sstevel@tonic-gate if (!found_key) 310*0Sstevel@tonic-gate debug2("key not found"); 311*0Sstevel@tonic-gate return found_key; 312*0Sstevel@tonic-gate } 313*0Sstevel@tonic-gate 314*0Sstevel@tonic-gate /* check whether given key is in .ssh/authorized_keys* */ 315*0Sstevel@tonic-gate int 316*0Sstevel@tonic-gate user_key_allowed(struct passwd *pw, Key *key) 317*0Sstevel@tonic-gate { 318*0Sstevel@tonic-gate int success; 319*0Sstevel@tonic-gate char *file; 320*0Sstevel@tonic-gate 321*0Sstevel@tonic-gate if (pw == NULL) 322*0Sstevel@tonic-gate return 0; 323*0Sstevel@tonic-gate 324*0Sstevel@tonic-gate file = authorized_keys_file(pw); 325*0Sstevel@tonic-gate success = user_key_allowed2(pw, key, file); 326*0Sstevel@tonic-gate xfree(file); 327*0Sstevel@tonic-gate if (success) 328*0Sstevel@tonic-gate return success; 329*0Sstevel@tonic-gate 330*0Sstevel@tonic-gate /* try suffix "2" for backward compat, too */ 331*0Sstevel@tonic-gate file = authorized_keys_file2(pw); 332*0Sstevel@tonic-gate success = user_key_allowed2(pw, key, file); 333*0Sstevel@tonic-gate xfree(file); 334*0Sstevel@tonic-gate return success; 335*0Sstevel@tonic-gate } 336*0Sstevel@tonic-gate 337*0Sstevel@tonic-gate static 338*0Sstevel@tonic-gate void 339*0Sstevel@tonic-gate userauth_pubkey_abandon(Authctxt *authctxt, Authmethod *method) 340*0Sstevel@tonic-gate { 341*0Sstevel@tonic-gate if (!authctxt || !method) 342*0Sstevel@tonic-gate return; 343*0Sstevel@tonic-gate 344*0Sstevel@tonic-gate if (method->method_data) { 345*0Sstevel@tonic-gate method->abandons++; 346*0Sstevel@tonic-gate method->attempts++; 347*0Sstevel@tonic-gate key_free((Key *) method->method_data); 348*0Sstevel@tonic-gate method->method_data = NULL; 349*0Sstevel@tonic-gate } 350*0Sstevel@tonic-gate } 351*0Sstevel@tonic-gate 352*0Sstevel@tonic-gate Authmethod method_pubkey = { 353*0Sstevel@tonic-gate "publickey", 354*0Sstevel@tonic-gate &options.pubkey_authentication, 355*0Sstevel@tonic-gate userauth_pubkey, 356*0Sstevel@tonic-gate userauth_pubkey_abandon, 357*0Sstevel@tonic-gate NULL, NULL, /* method data and hist data */ 358*0Sstevel@tonic-gate 0, /* not initial userauth */ 359*0Sstevel@tonic-gate 0, 0, 0, /* counters */ 360*0Sstevel@tonic-gate 0, 0, 0, 0, 0, 0 /* state */ 361*0Sstevel@tonic-gate }; 362