1 /* $OpenBSD: auth2-pubkey.c,v 1.115 2022/05/27 05:02:46 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 const char *remote_ip = ssh_remote_ipaddr(ssh); 99 const char *remote_host = auth_get_canonical_hostname(ssh, 100 options.use_dns); 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) { 152 error_f("type mismatch for decoded key " 153 "(received %d, expected %d)", key->type, pktype); 154 goto done; 155 } 156 if (sshkey_type_plain(key->type) == KEY_RSA && 157 (ssh->compat & SSH_BUG_RSASIGMD5) != 0) { 158 logit("Refusing RSA key because client uses unsafe " 159 "signature scheme"); 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 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 (PRIVSEP(user_key_allowed(pw, key, 1, remote_ip, 224 remote_host, &authopts)) && 225 PRIVSEP(sshkey_verify(key, sig, slen, 226 sshbuf_ptr(b), sshbuf_len(b), 227 (ssh->compat & SSH_BUG_SIGTYPE) == 0 ? pkalg : NULL, 228 ssh->compat, &sig_details)) == 0) { 229 authenticated = 1; 230 } 231 if (authenticated == 1 && sig_details != NULL) { 232 auth2_record_info(authctxt, "signature count = %u", 233 sig_details->sk_counter); 234 debug_f("sk_counter = %u, sk_flags = 0x%02x", 235 sig_details->sk_counter, sig_details->sk_flags); 236 req_presence = (options.pubkey_auth_options & 237 PUBKEYAUTH_TOUCH_REQUIRED) || 238 !authopts->no_require_user_presence; 239 if (req_presence && (sig_details->sk_flags & 240 SSH_SK_USER_PRESENCE_REQD) == 0) { 241 error("public key %s signature for %s%s from " 242 "%.128s port %d rejected: user presence " 243 "(authenticator touch) requirement " 244 "not met ", key_s, 245 authctxt->valid ? "" : "invalid user ", 246 authctxt->user, ssh_remote_ipaddr(ssh), 247 ssh_remote_port(ssh)); 248 authenticated = 0; 249 goto done; 250 } 251 req_verify = (options.pubkey_auth_options & 252 PUBKEYAUTH_VERIFY_REQUIRED) || 253 authopts->require_verify; 254 if (req_verify && (sig_details->sk_flags & 255 SSH_SK_USER_VERIFICATION_REQD) == 0) { 256 error("public key %s signature for %s%s from " 257 "%.128s port %d rejected: user " 258 "verification requirement not met ", key_s, 259 authctxt->valid ? "" : "invalid user ", 260 authctxt->user, ssh_remote_ipaddr(ssh), 261 ssh_remote_port(ssh)); 262 authenticated = 0; 263 goto done; 264 } 265 } 266 auth2_record_key(authctxt, authenticated, key); 267 } else { 268 debug_f("%s test pkalg %s pkblob %s%s%s", method, pkalg, key_s, 269 ca_s == NULL ? "" : " CA ", ca_s == NULL ? "" : ca_s); 270 271 if ((r = sshpkt_get_end(ssh)) != 0) 272 fatal_fr(r, "parse packet"); 273 274 if (!authctxt->valid || authctxt->user == NULL) { 275 debug2_f("disabled because of invalid user"); 276 goto done; 277 } 278 /* XXX fake reply and always send PK_OK ? */ 279 /* 280 * XXX this allows testing whether a user is allowed 281 * to login: if you happen to have a valid pubkey this 282 * message is sent. the message is NEVER sent at all 283 * if a user is not allowed to login. is this an 284 * issue? -markus 285 */ 286 if (PRIVSEP(user_key_allowed(pw, key, 0, remote_ip, 287 remote_host, 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 passwd *pw, struct sshkey *key, 752 int auth_attempt, const char *remote_ip, const char *remote_host, 753 struct sshauthopt **authoptsp) 754 { 755 u_int success = 0, i; 756 char *file; 757 struct sshauthopt *opts = NULL; 758 759 if (authoptsp != NULL) 760 *authoptsp = NULL; 761 762 if (auth_key_is_revoked(key)) 763 return 0; 764 if (sshkey_is_cert(key) && 765 auth_key_is_revoked(key->cert->signature_key)) 766 return 0; 767 768 for (i = 0; !success && i < options.num_authkeys_files; i++) { 769 if (strcasecmp(options.authorized_keys_files[i], "none") == 0) 770 continue; 771 file = expand_authorized_keys( 772 options.authorized_keys_files[i], pw); 773 success = user_key_allowed2(pw, key, file, 774 remote_ip, remote_host, &opts); 775 free(file); 776 if (!success) { 777 sshauthopt_free(opts); 778 opts = NULL; 779 } 780 } 781 if (success) 782 goto out; 783 784 if ((success = user_cert_trusted_ca(pw, key, remote_ip, remote_host, 785 &opts)) != 0) 786 goto out; 787 sshauthopt_free(opts); 788 opts = NULL; 789 790 if ((success = user_key_command_allowed2(pw, key, remote_ip, 791 remote_host, &opts)) != 0) 792 goto out; 793 sshauthopt_free(opts); 794 opts = NULL; 795 796 out: 797 if (success && authoptsp != NULL) { 798 *authoptsp = opts; 799 opts = NULL; 800 } 801 sshauthopt_free(opts); 802 return success; 803 } 804 805 Authmethod method_pubkey = { 806 "publickey", 807 "publickey-hostbound-v00@openssh.com", 808 userauth_pubkey, 809 &options.pubkey_authentication 810 }; 811