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