1/* $NetBSD: auth2-pubkey.c,v 1.34 2023/10/25 20:19:57 christos Exp $ */ 2/* $OpenBSD: auth2-pubkey.c,v 1.119 2023/07/27 22:25:17 djm Exp $ */ 3 4/* 5 * Copyright (c) 2000 Markus Friedl. All rights reserved. 6 * Copyright (c) 2010 Damien Miller. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#include "includes.h" 30__RCSID("$NetBSD: auth2-pubkey.c,v 1.34 2023/10/25 20:19:57 christos Exp $"); 31#include <sys/types.h> 32 33#include <stdlib.h> 34#include <errno.h> 35#include <paths.h> 36#include <pwd.h> 37#include <signal.h> 38#include <stdio.h> 39#include <stdarg.h> 40#include <string.h> 41#include <time.h> 42#include <unistd.h> 43#include <limits.h> 44 45#include "xmalloc.h" 46#include "ssh.h" 47#include "ssh2.h" 48#include "packet.h" 49#include "kex.h" 50#include "sshbuf.h" 51#include "log.h" 52#include "misc.h" 53#include "servconf.h" 54#include "compat.h" 55#include "sshkey.h" 56#include "hostfile.h" 57#include "auth.h" 58#include "pathnames.h" 59#include "uidswap.h" 60#include "auth-options.h" 61#include "canohost.h" 62#ifdef GSSAPI 63#include "ssh-gss.h" 64#endif 65#include "monitor_wrap.h" 66#include "authfile.h" 67#include "match.h" 68#include "digest.h" 69 70#ifdef WITH_LDAP_PUBKEY 71#include "ldapauth.h" 72#endif 73 74#include "ssherr.h" 75#include "channels.h" /* XXX for session.h */ 76#include "session.h" /* XXX for child_set_env(); refactor? */ 77#include "sk-api.h" 78 79/* import */ 80extern ServerOptions options; 81 82static char * 83format_key(const struct sshkey *key) 84{ 85 char *ret, *fp = sshkey_fingerprint(key, 86 options.fingerprint_hash, SSH_FP_DEFAULT); 87 88 xasprintf(&ret, "%s %s", sshkey_type(key), fp); 89 free(fp); 90 return ret; 91} 92 93static int 94userauth_pubkey(struct ssh *ssh, const char *method) 95{ 96 Authctxt *authctxt = ssh->authctxt; 97 struct passwd *pw = authctxt->pw; 98 struct sshbuf *b = NULL; 99 struct sshkey *key = NULL, *hostkey = NULL; 100 char *pkalg = NULL, *userstyle = NULL, *key_s = NULL, *ca_s = NULL; 101 u_char *pkblob = NULL, *sig = NULL, have_sig; 102 size_t blen, slen; 103 int hostbound, r, pktype; 104 int req_presence = 0, req_verify = 0, authenticated = 0; 105 struct sshauthopt *authopts = NULL; 106 struct sshkey_sig_details *sig_details = NULL; 107 108 hostbound = strcmp(method, "publickey-hostbound-v00@openssh.com") == 0; 109 110 if ((r = sshpkt_get_u8(ssh, &have_sig)) != 0 || 111 (r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 || 112 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0) 113 fatal_fr(r, "parse %s packet", method); 114 115 /* hostbound auth includes the hostkey offered at initial KEX */ 116 if (hostbound) { 117 if ((r = sshpkt_getb_froms(ssh, &b)) != 0 || 118 (r = sshkey_fromb(b, &hostkey)) != 0) 119 fatal_fr(r, "parse %s hostkey", method); 120 if (ssh->kex->initial_hostkey == NULL) 121 fatal_f("internal error: initial hostkey not recorded"); 122 if (!sshkey_equal(hostkey, ssh->kex->initial_hostkey)) 123 fatal_f("%s packet contained wrong host key", method); 124 sshbuf_free(b); 125 b = NULL; 126 } 127 128 if (log_level_get() >= SYSLOG_LEVEL_DEBUG2) { 129 char *keystring; 130 struct sshbuf *pkbuf; 131 132 if ((pkbuf = sshbuf_from(pkblob, blen)) == NULL) 133 fatal_f("sshbuf_from failed"); 134 if ((keystring = sshbuf_dtob64_string(pkbuf, 0)) == NULL) 135 fatal_f("sshbuf_dtob64 failed"); 136 debug2_f("%s user %s %s public key %s %s", 137 authctxt->valid ? "valid" : "invalid", authctxt->user, 138 have_sig ? "attempting" : "querying", pkalg, keystring); 139 sshbuf_free(pkbuf); 140 free(keystring); 141 } 142 143 pktype = sshkey_type_from_name(pkalg); 144 if (pktype == KEY_UNSPEC) { 145 /* this is perfectly legal */ 146 verbose_f("unsupported public key algorithm: %s", pkalg); 147 goto done; 148 } 149 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) { 150 error_fr(r, "parse key"); 151 goto done; 152 } 153 if (key == NULL) { 154 error_f("cannot decode key: %s", pkalg); 155 goto done; 156 } 157 if (key->type != pktype) { 158 error_f("type mismatch for decoded key " 159 "(received %d, expected %d)", key->type, pktype); 160 goto done; 161 } 162 if (auth2_key_already_used(authctxt, key)) { 163 logit("refusing previously-used %s key", sshkey_type(key)); 164 goto done; 165 } 166 if (match_pattern_list(pkalg, options.pubkey_accepted_algos, 0) != 1) { 167 logit_f("signature algorithm %s not in " 168 "PubkeyAcceptedAlgorithms", pkalg); 169 goto done; 170 } 171 if ((r = sshkey_check_cert_sigtype(key, 172 options.ca_sign_algorithms)) != 0) { 173 logit_fr(r, "certificate signature algorithm %s", 174 (key->cert == NULL || key->cert->signature_type == NULL) ? 175 "(null)" : key->cert->signature_type); 176 goto done; 177 } 178 if ((r = sshkey_check_rsa_length(key, 179 options.required_rsa_size)) != 0) { 180 logit_r(r, "refusing %s key", sshkey_type(key)); 181 goto done; 182 } 183 key_s = format_key(key); 184 if (sshkey_is_cert(key)) 185 ca_s = format_key(key->cert->signature_key); 186 187 if (have_sig) { 188 debug3_f("%s have %s signature for %s%s%s", 189 method, pkalg, key_s, 190 ca_s == NULL ? "" : " CA ", ca_s == NULL ? "" : ca_s); 191 if ((r = sshpkt_get_string(ssh, &sig, &slen)) != 0 || 192 (r = sshpkt_get_end(ssh)) != 0) 193 fatal_fr(r, "parse signature packet"); 194 if ((b = sshbuf_new()) == NULL) 195 fatal_f("sshbuf_new failed"); 196 if (ssh->compat & SSH_OLD_SESSIONID) { 197 if ((r = sshbuf_putb(b, ssh->kex->session_id)) != 0) 198 fatal_fr(r, "put old session id"); 199 } else { 200 if ((r = sshbuf_put_stringb(b, 201 ssh->kex->session_id)) != 0) 202 fatal_fr(r, "put session id"); 203 } 204 if (!authctxt->valid || authctxt->user == NULL) { 205 debug2_f("disabled because of invalid user"); 206 goto done; 207 } 208 /* reconstruct packet */ 209 xasprintf(&userstyle, "%s%s%s", authctxt->user, 210 authctxt->style ? ":" : "", 211 authctxt->style ? authctxt->style : ""); 212 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 213 (r = sshbuf_put_cstring(b, userstyle)) != 0 || 214 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 || 215 (r = sshbuf_put_cstring(b, method)) != 0 || 216 (r = sshbuf_put_u8(b, have_sig)) != 0 || 217 (r = sshbuf_put_cstring(b, pkalg)) != 0 || 218 (r = sshbuf_put_string(b, pkblob, blen)) != 0) 219 fatal_fr(r, "reconstruct %s packet", method); 220 if (hostbound && 221 (r = sshkey_puts(ssh->kex->initial_hostkey, b)) != 0) 222 fatal_fr(r, "reconstruct %s packet", method); 223#ifdef DEBUG_PK 224 sshbuf_dump(b, stderr); 225#endif 226 /* test for correct signature */ 227 authenticated = 0; 228 if (PRIVSEP(user_key_allowed(ssh, pw, key, 1, &authopts)) && 229 PRIVSEP(sshkey_verify(key, sig, slen, 230 sshbuf_ptr(b), sshbuf_len(b), 231 (ssh->compat & SSH_BUG_SIGTYPE) == 0 ? pkalg : NULL, 232 ssh->compat, &sig_details)) == 0) { 233 authenticated = 1; 234 } 235 if (authenticated == 1 && sig_details != NULL) { 236 auth2_record_info(authctxt, "signature count = %u", 237 sig_details->sk_counter); 238 debug_f("sk_counter = %u, sk_flags = 0x%02x", 239 sig_details->sk_counter, sig_details->sk_flags); 240 req_presence = (options.pubkey_auth_options & 241 PUBKEYAUTH_TOUCH_REQUIRED) || 242 !authopts->no_require_user_presence; 243 if (req_presence && (sig_details->sk_flags & 244 SSH_SK_USER_PRESENCE_REQD) == 0) { 245 error("public key %s signature for %s%s from " 246 "%.128s port %d rejected: user presence " 247 "(authenticator touch) requirement " 248 "not met ", key_s, 249 authctxt->valid ? "" : "invalid user ", 250 authctxt->user, ssh_remote_ipaddr(ssh), 251 ssh_remote_port(ssh)); 252 authenticated = 0; 253 goto done; 254 } 255 req_verify = (options.pubkey_auth_options & 256 PUBKEYAUTH_VERIFY_REQUIRED) || 257 authopts->require_verify; 258 if (req_verify && (sig_details->sk_flags & 259 SSH_SK_USER_VERIFICATION_REQD) == 0) { 260 error("public key %s signature for %s%s from " 261 "%.128s port %d rejected: user " 262 "verification requirement not met ", key_s, 263 authctxt->valid ? "" : "invalid user ", 264 authctxt->user, ssh_remote_ipaddr(ssh), 265 ssh_remote_port(ssh)); 266 authenticated = 0; 267 goto done; 268 } 269 } 270 auth2_record_key(authctxt, authenticated, key); 271 } else { 272 debug_f("%s test pkalg %s pkblob %s%s%s", method, pkalg, key_s, 273 ca_s == NULL ? "" : " CA ", ca_s == NULL ? "" : ca_s); 274 275 if ((r = sshpkt_get_end(ssh)) != 0) 276 fatal_fr(r, "parse packet"); 277 278 if (!authctxt->valid || authctxt->user == NULL) { 279 debug2_f("disabled because of invalid user"); 280 goto done; 281 } 282 /* XXX fake reply and always send PK_OK ? */ 283 /* 284 * XXX this allows testing whether a user is allowed 285 * to login: if you happen to have a valid pubkey this 286 * message is sent. the message is NEVER sent at all 287 * if a user is not allowed to login. is this an 288 * issue? -markus 289 */ 290 if (PRIVSEP(user_key_allowed(ssh, pw, key, 0, NULL))) { 291 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_PK_OK)) 292 != 0 || 293 (r = sshpkt_put_cstring(ssh, pkalg)) != 0 || 294 (r = sshpkt_put_string(ssh, pkblob, blen)) != 0 || 295 (r = sshpkt_send(ssh)) != 0 || 296 (r = ssh_packet_write_wait(ssh)) < 0) 297 fatal_fr(r, "send packet"); 298 authctxt->postponed = 1; 299 } 300 } 301done: 302 if (authenticated == 1 && auth_activate_options(ssh, authopts) != 0) { 303 debug_f("key options inconsistent with existing"); 304 authenticated = 0; 305 } 306 debug2_f("authenticated %d pkalg %s", authenticated, pkalg); 307 308 sshbuf_free(b); 309 sshauthopt_free(authopts); 310 sshkey_free(key); 311 sshkey_free(hostkey); 312 free(userstyle); 313 free(pkalg); 314 free(pkblob); 315 free(key_s); 316 free(ca_s); 317 free(sig); 318 sshkey_sig_details_free(sig_details); 319 return authenticated; 320} 321 322static int 323match_principals_file(struct passwd *pw, char *file, 324 struct sshkey_cert *cert, struct sshauthopt **authoptsp) 325{ 326 FILE *f; 327 int success; 328 329 if (authoptsp != NULL) 330 *authoptsp = NULL; 331 332 temporarily_use_uid(pw); 333 debug("trying authorized principals file %s", file); 334 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) { 335 restore_uid(); 336 return 0; 337 } 338 success = auth_process_principals(f, file, cert, authoptsp); 339 fclose(f); 340 restore_uid(); 341 return success; 342} 343 344/* 345 * Checks whether principal is allowed in output of command. 346 * returns 1 if the principal is allowed or 0 otherwise. 347 */ 348static int 349match_principals_command(struct passwd *user_pw, const struct sshkey *key, 350 const char *conn_id, const char *rdomain, struct sshauthopt **authoptsp) 351{ 352 struct passwd *runas_pw = NULL; 353 const struct sshkey_cert *cert = key->cert; 354 FILE *f = NULL; 355 int r, ok, found_principal = 0; 356 int i, ac = 0, uid_swapped = 0; 357 pid_t pid; 358 char *tmp, *username = NULL, *command = NULL, **av = NULL; 359 char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL; 360 char serial_s[32], uidstr[32]; 361 void (*osigchld)(int); 362 363 if (authoptsp != NULL) 364 *authoptsp = NULL; 365 if (options.authorized_principals_command == NULL) 366 return 0; 367 if (options.authorized_principals_command_user == NULL) { 368 error("No user for AuthorizedPrincipalsCommand specified, " 369 "skipping"); 370 return 0; 371 } 372 373 /* 374 * NB. all returns later this function should go via "out" to 375 * ensure the original SIGCHLD handler is restored properly. 376 */ 377 osigchld = ssh_signal(SIGCHLD, SIG_DFL); 378 379 /* Prepare and verify the user for the command */ 380 username = percent_expand(options.authorized_principals_command_user, 381 "u", user_pw->pw_name, (char *)NULL); 382 runas_pw = getpwnam(username); 383 if (runas_pw == NULL) { 384 error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s", 385 username, strerror(errno)); 386 goto out; 387 } 388 389 /* Turn the command into an argument vector */ 390 if (argv_split(options.authorized_principals_command, 391 &ac, &av, 0) != 0) { 392 error("AuthorizedPrincipalsCommand \"%s\" contains " 393 "invalid quotes", options.authorized_principals_command); 394 goto out; 395 } 396 if (ac == 0) { 397 error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments", 398 options.authorized_principals_command); 399 goto out; 400 } 401 if ((ca_fp = sshkey_fingerprint(cert->signature_key, 402 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) { 403 error_f("sshkey_fingerprint failed"); 404 goto out; 405 } 406 if ((key_fp = sshkey_fingerprint(key, 407 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) { 408 error_f("sshkey_fingerprint failed"); 409 goto out; 410 } 411 if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) { 412 error_fr(r, "sshkey_to_base64 failed"); 413 goto out; 414 } 415 if ((r = sshkey_to_base64(key, &keytext)) != 0) { 416 error_fr(r, "sshkey_to_base64 failed"); 417 goto out; 418 } 419 snprintf(serial_s, sizeof(serial_s), "%llu", 420 (unsigned long long)cert->serial); 421 snprintf(uidstr, sizeof(uidstr), "%llu", 422 (unsigned long long)user_pw->pw_uid); 423 for (i = 1; i < ac; i++) { 424 tmp = percent_expand(av[i], 425 "C", conn_id, 426 "D", rdomain, 427 "U", uidstr, 428 "u", user_pw->pw_name, 429 "h", user_pw->pw_dir, 430 "t", sshkey_ssh_name(key), 431 "T", sshkey_ssh_name(cert->signature_key), 432 "f", key_fp, 433 "F", ca_fp, 434 "k", keytext, 435 "K", catext, 436 "i", cert->key_id, 437 "s", serial_s, 438 (char *)NULL); 439 if (tmp == NULL) 440 fatal_f("percent_expand failed"); 441 free(av[i]); 442 av[i] = tmp; 443 } 444 /* Prepare a printable command for logs, etc. */ 445 command = argv_assemble(ac, av); 446 447 if ((pid = subprocess("AuthorizedPrincipalsCommand", command, 448 ac, av, &f, 449 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD, 450 runas_pw, temporarily_use_uid, restore_uid)) == 0) 451 goto out; 452 453 uid_swapped = 1; 454 temporarily_use_uid(runas_pw); 455 456 ok = auth_process_principals(f, "(command)", cert, authoptsp); 457 458 fclose(f); 459 f = NULL; 460 461 if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command, 0) != 0) 462 goto out; 463 464 /* Read completed successfully */ 465 found_principal = ok; 466 out: 467 if (f != NULL) 468 fclose(f); 469 ssh_signal(SIGCHLD, osigchld); 470 for (i = 0; i < ac; i++) 471 free(av[i]); 472 free(av); 473 if (uid_swapped) 474 restore_uid(); 475 free(command); 476 free(username); 477 free(ca_fp); 478 free(key_fp); 479 free(catext); 480 free(keytext); 481 return found_principal; 482} 483 484/* Authenticate a certificate key against TrustedUserCAKeys */ 485static int 486user_cert_trusted_ca(struct passwd *pw, struct sshkey *key, 487 const char *remote_ip, const char *remote_host, 488 const char *conn_id, const char *rdomain, struct sshauthopt **authoptsp) 489{ 490 char *ca_fp, *principals_file = NULL; 491 const char *reason; 492 struct sshauthopt *principals_opts = NULL, *cert_opts = NULL; 493 struct sshauthopt *final_opts = NULL; 494 int r, ret = 0, found_principal = 0, use_authorized_principals; 495 496 if (authoptsp != NULL) 497 *authoptsp = NULL; 498 499 if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL) 500 return 0; 501 502 if ((ca_fp = sshkey_fingerprint(key->cert->signature_key, 503 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) 504 return 0; 505 506 if ((r = sshkey_in_file(key->cert->signature_key, 507 options.trusted_user_ca_keys, 1, 0)) != 0) { 508 debug2_fr(r, "CA %s %s is not listed in %s", 509 sshkey_type(key->cert->signature_key), ca_fp, 510 options.trusted_user_ca_keys); 511 goto out; 512 } 513 /* 514 * If AuthorizedPrincipals is in use, then compare the certificate 515 * principals against the names in that file rather than matching 516 * against the username. 517 */ 518 if ((principals_file = authorized_principals_file(pw)) != NULL) { 519 if (match_principals_file(pw, principals_file, 520 key->cert, &principals_opts)) 521 found_principal = 1; 522 } 523 /* Try querying command if specified */ 524 if (!found_principal && match_principals_command(pw, key, 525 conn_id, rdomain, &principals_opts)) 526 found_principal = 1; 527 /* If principals file or command is specified, then require a match */ 528 use_authorized_principals = principals_file != NULL || 529 options.authorized_principals_command != NULL; 530 if (!found_principal && use_authorized_principals) { 531 reason = "Certificate does not contain an authorized principal"; 532 goto fail_reason; 533 } 534 if (use_authorized_principals && principals_opts == NULL) 535 fatal_f("internal error: missing principals_opts"); 536 if (sshkey_cert_check_authority_now(key, 0, 1, 0, 537 use_authorized_principals ? NULL : pw->pw_name, &reason) != 0) 538 goto fail_reason; 539 540 /* Check authority from options in key and from principals file/cmd */ 541 if ((cert_opts = sshauthopt_from_cert(key)) == NULL) { 542 reason = "Invalid certificate options"; 543 goto fail_reason; 544 } 545 if (auth_authorise_keyopts(pw, cert_opts, 0, 546 remote_ip, remote_host, "cert") != 0) { 547 reason = "Refused by certificate options"; 548 goto fail_reason; 549 } 550 if (principals_opts == NULL) { 551 final_opts = cert_opts; 552 cert_opts = NULL; 553 } else { 554 if (auth_authorise_keyopts(pw, principals_opts, 0, 555 remote_ip, remote_host, "principals") != 0) { 556 reason = "Refused by certificate principals options"; 557 goto fail_reason; 558 } 559 if ((final_opts = sshauthopt_merge(principals_opts, 560 cert_opts, &reason)) == NULL) { 561 fail_reason: 562 error("%s", reason); 563 auth_debug_add("%s", reason); 564 goto out; 565 } 566 } 567 568 /* Success */ 569 verbose("Accepted certificate ID \"%s\" (serial %llu) signed by " 570 "%s CA %s via %s", key->cert->key_id, 571 (unsigned long long)key->cert->serial, 572 sshkey_type(key->cert->signature_key), ca_fp, 573 options.trusted_user_ca_keys); 574 if (authoptsp != NULL) { 575 *authoptsp = final_opts; 576 final_opts = NULL; 577 } 578 ret = 1; 579 out: 580 sshauthopt_free(principals_opts); 581 sshauthopt_free(cert_opts); 582 sshauthopt_free(final_opts); 583 free(principals_file); 584 free(ca_fp); 585 return ret; 586} 587 588/* 589 * Checks whether key is allowed in file. 590 * returns 1 if the key is allowed or 0 otherwise. 591 */ 592static int 593user_key_allowed2(struct passwd *pw, struct sshkey *key, 594 char *file, const char *remote_ip, const char *remote_host, 595 struct sshauthopt **authoptsp) 596{ 597 FILE *f; 598 int found_key = 0; 599 600 if (authoptsp != NULL) 601 *authoptsp = NULL; 602 603 /* Temporarily use the user's uid. */ 604 temporarily_use_uid(pw); 605 606 debug("trying public key file %s", file); 607 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) { 608 found_key = auth_check_authkeys_file(pw, f, file, 609 key, remote_ip, remote_host, authoptsp); 610 fclose(f); 611 } 612 613 restore_uid(); 614 return found_key; 615} 616 617/* 618 * Checks whether key is allowed in output of command. 619 * returns 1 if the key is allowed or 0 otherwise. 620 */ 621static int 622user_key_command_allowed2(struct passwd *user_pw, struct sshkey *key, 623 const char *remote_ip, const char *remote_host, 624 const char *conn_id, const char *rdomain, struct sshauthopt **authoptsp) 625{ 626 struct passwd *runas_pw = NULL; 627 FILE *f = NULL; 628 int r, ok, found_key = 0; 629 int i, uid_swapped = 0, ac = 0; 630 pid_t pid; 631 char *username = NULL, *key_fp = NULL, *keytext = NULL; 632 char uidstr[32], *tmp, *command = NULL, **av = NULL; 633 void (*osigchld)(int); 634 635 if (authoptsp != NULL) 636 *authoptsp = NULL; 637 if (options.authorized_keys_command == NULL) 638 return 0; 639 if (options.authorized_keys_command_user == NULL) { 640 error("No user for AuthorizedKeysCommand specified, skipping"); 641 return 0; 642 } 643 644 /* 645 * NB. all returns later this function should go via "out" to 646 * ensure the original SIGCHLD handler is restored properly. 647 */ 648 osigchld = ssh_signal(SIGCHLD, SIG_DFL); 649 650 /* Prepare and verify the user for the command */ 651 username = percent_expand(options.authorized_keys_command_user, 652 "u", user_pw->pw_name, (char *)NULL); 653 runas_pw = getpwnam(username); 654 if (runas_pw == NULL) { 655 error("AuthorizedKeysCommandUser \"%s\" not found: %s", 656 username, strerror(errno)); 657 goto out; 658 } 659 660 /* Prepare AuthorizedKeysCommand */ 661 if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash, 662 SSH_FP_DEFAULT)) == NULL) { 663 error_f("sshkey_fingerprint failed"); 664 goto out; 665 } 666 if ((r = sshkey_to_base64(key, &keytext)) != 0) { 667 error_fr(r, "sshkey_to_base64 failed"); 668 goto out; 669 } 670 671 /* Turn the command into an argument vector */ 672 if (argv_split(options.authorized_keys_command, &ac, &av, 0) != 0) { 673 error("AuthorizedKeysCommand \"%s\" contains invalid quotes", 674 options.authorized_keys_command); 675 goto out; 676 } 677 if (ac == 0) { 678 error("AuthorizedKeysCommand \"%s\" yielded no arguments", 679 options.authorized_keys_command); 680 goto out; 681 } 682 snprintf(uidstr, sizeof(uidstr), "%llu", 683 (unsigned long long)user_pw->pw_uid); 684 for (i = 1; i < ac; i++) { 685 tmp = percent_expand(av[i], 686 "C", conn_id, 687 "D", rdomain, 688 "U", uidstr, 689 "u", user_pw->pw_name, 690 "h", user_pw->pw_dir, 691 "t", sshkey_ssh_name(key), 692 "f", key_fp, 693 "k", keytext, 694 (char *)NULL); 695 if (tmp == NULL) 696 fatal_f("percent_expand failed"); 697 free(av[i]); 698 av[i] = tmp; 699 } 700 /* Prepare a printable command for logs, etc. */ 701 command = argv_assemble(ac, av); 702 703 /* 704 * If AuthorizedKeysCommand was run without arguments 705 * then fall back to the old behaviour of passing the 706 * target username as a single argument. 707 */ 708 if (ac == 1) { 709 av = xreallocarray(av, ac + 2, sizeof(*av)); 710 av[1] = xstrdup(user_pw->pw_name); 711 av[2] = NULL; 712 /* Fix up command too, since it is used in log messages */ 713 free(command); 714 xasprintf(&command, "%s %s", av[0], av[1]); 715 } 716 717 if ((pid = subprocess("AuthorizedKeysCommand", command, 718 ac, av, &f, 719 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD, 720 runas_pw, temporarily_use_uid, restore_uid)) == 0) 721 goto out; 722 723 uid_swapped = 1; 724 temporarily_use_uid(runas_pw); 725 726 ok = auth_check_authkeys_file(user_pw, f, 727 options.authorized_keys_command, key, remote_ip, 728 remote_host, authoptsp); 729 730 fclose(f); 731 f = NULL; 732 733 if (exited_cleanly(pid, "AuthorizedKeysCommand", command, 0) != 0) 734 goto out; 735 736 /* Read completed successfully */ 737 found_key = ok; 738 out: 739 if (f != NULL) 740 fclose(f); 741 ssh_signal(SIGCHLD, osigchld); 742 for (i = 0; i < ac; i++) 743 free(av[i]); 744 free(av); 745 if (uid_swapped) 746 restore_uid(); 747 free(command); 748 free(username); 749 free(key_fp); 750 free(keytext); 751 return found_key; 752} 753 754/* 755 * Check whether key authenticates and authorises the user. 756 */ 757int 758user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key, 759 int auth_attempt, struct sshauthopt **authoptsp) 760{ 761 u_int success = 0, i; 762 char *file, *conn_id; 763 struct sshauthopt *opts = NULL; 764 const char *rdomain, *remote_ip, *remote_host; 765 766 if (authoptsp != NULL) 767 *authoptsp = NULL; 768 769 if (auth_key_is_revoked(key)) 770 return 0; 771 if (sshkey_is_cert(key) && 772 auth_key_is_revoked(key->cert->signature_key)) 773 return 0; 774 775 if ((rdomain = ssh_packet_rdomain_in(ssh)) == NULL) 776 rdomain = ""; 777 remote_ip = ssh_remote_ipaddr(ssh); 778 remote_host = auth_get_canonical_hostname(ssh, options.use_dns); 779 xasprintf(&conn_id, "%s %d %s %d", 780 ssh_local_ipaddr(ssh), ssh_local_port(ssh), 781 remote_ip, ssh_remote_port(ssh)); 782 783 for (i = 0; !success && i < options.num_authkeys_files; i++) { 784 if (strcasecmp(options.authorized_keys_files[i], "none") == 0) 785 continue; 786 file = expand_authorized_keys( 787 options.authorized_keys_files[i], pw); 788 success = user_key_allowed2(pw, key, file, 789 remote_ip, remote_host, &opts); 790 free(file); 791 if (!success) { 792 sshauthopt_free(opts); 793 opts = NULL; 794 } 795 } 796 if (success) 797 goto out; 798 799 if ((success = user_cert_trusted_ca(pw, key, remote_ip, remote_host, 800 conn_id, rdomain, &opts)) != 0) 801 goto out; 802 sshauthopt_free(opts); 803 opts = NULL; 804 805 if ((success = user_key_command_allowed2(pw, key, remote_ip, 806 remote_host, conn_id, rdomain, &opts)) != 0) 807 goto out; 808 sshauthopt_free(opts); 809 opts = NULL; 810 811 out: 812 free(conn_id); 813 if (success && authoptsp != NULL) { 814 *authoptsp = opts; 815 opts = NULL; 816 } 817 sshauthopt_free(opts); 818 return success; 819} 820 821Authmethod method_pubkey = { 822 "publickey", 823 "publickey-hostbound-v00@openssh.com", 824 userauth_pubkey, 825 &options.pubkey_authentication 826}; 827