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