1 /* $OpenBSD: auth2-pubkey.c,v 1.77 2018/03/03 03:15:51 djm Exp $ */ 2 /* 3 * Copyright (c) 2000 Markus Friedl. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 27 #include <sys/types.h> 28 #include <sys/stat.h> 29 30 #include <errno.h> 31 #include <fcntl.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 "buffer.h" 47 #include "log.h" 48 #include "misc.h" 49 #include "servconf.h" 50 #include "compat.h" 51 #include "sshkey.h" 52 #include "hostfile.h" 53 #include "auth.h" 54 #include "pathnames.h" 55 #include "uidswap.h" 56 #include "auth-options.h" 57 #include "canohost.h" 58 #ifdef GSSAPI 59 #include "ssh-gss.h" 60 #endif 61 #include "monitor_wrap.h" 62 #include "authfile.h" 63 #include "match.h" 64 #include "ssherr.h" 65 #include "channels.h" /* XXX for session.h */ 66 #include "session.h" /* XXX for child_set_env(); refactor? */ 67 68 /* import */ 69 extern ServerOptions options; 70 extern u_char *session_id2; 71 extern u_int session_id2_len; 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) 86 { 87 Authctxt *authctxt = ssh->authctxt; 88 struct passwd *pw = authctxt->pw; 89 struct sshbuf *b; 90 struct sshkey *key = NULL; 91 char *pkalg, *userstyle = NULL, *key_s = NULL, *ca_s = NULL; 92 u_char *pkblob, *sig, have_sig; 93 size_t blen, slen; 94 int r, pktype; 95 int authenticated = 0; 96 struct sshauthopt *authopts = NULL; 97 98 if (!authctxt->valid) { 99 debug2("%s: disabled because of invalid user", __func__); 100 return 0; 101 } 102 if ((r = sshpkt_get_u8(ssh, &have_sig)) != 0 || 103 (r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 || 104 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0) 105 fatal("%s: parse request failed: %s", __func__, ssh_err(r)); 106 pktype = sshkey_type_from_name(pkalg); 107 if (pktype == KEY_UNSPEC) { 108 /* this is perfectly legal */ 109 logit("%s: unsupported public key algorithm: %s", 110 __func__, pkalg); 111 goto done; 112 } 113 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) { 114 error("%s: could not parse key: %s", __func__, ssh_err(r)); 115 goto done; 116 } 117 if (key == NULL) { 118 error("%s: cannot decode key: %s", __func__, pkalg); 119 goto done; 120 } 121 if (key->type != pktype) { 122 error("%s: type mismatch for decoded key " 123 "(received %d, expected %d)", __func__, key->type, pktype); 124 goto done; 125 } 126 if (sshkey_type_plain(key->type) == KEY_RSA && 127 (ssh->compat & SSH_BUG_RSASIGMD5) != 0) { 128 logit("Refusing RSA key because client uses unsafe " 129 "signature scheme"); 130 goto done; 131 } 132 if (auth2_key_already_used(authctxt, key)) { 133 logit("refusing previously-used %s key", sshkey_type(key)); 134 goto done; 135 } 136 if (match_pattern_list(sshkey_ssh_name(key), 137 options.pubkey_key_types, 0) != 1) { 138 logit("%s: key type %s not in PubkeyAcceptedKeyTypes", 139 __func__, sshkey_ssh_name(key)); 140 goto done; 141 } 142 143 key_s = format_key(key); 144 if (sshkey_is_cert(key)) 145 ca_s = format_key(key->cert->signature_key); 146 147 if (have_sig) { 148 debug3("%s: have %s signature for %s%s%s", 149 __func__, pkalg, key_s, 150 ca_s == NULL ? "" : " CA ", 151 ca_s == NULL ? "" : ca_s); 152 if ((r = sshpkt_get_string(ssh, &sig, &slen)) != 0 || 153 (r = sshpkt_get_end(ssh)) != 0) 154 fatal("%s: %s", __func__, ssh_err(r)); 155 if ((b = sshbuf_new()) == NULL) 156 fatal("%s: sshbuf_new failed", __func__); 157 if (ssh->compat & SSH_OLD_SESSIONID) { 158 if ((r = sshbuf_put(b, session_id2, 159 session_id2_len)) != 0) 160 fatal("%s: sshbuf_put session id: %s", 161 __func__, ssh_err(r)); 162 } else { 163 if ((r = sshbuf_put_string(b, session_id2, 164 session_id2_len)) != 0) 165 fatal("%s: sshbuf_put_string session id: %s", 166 __func__, ssh_err(r)); 167 } 168 /* reconstruct packet */ 169 xasprintf(&userstyle, "%s%s%s", authctxt->user, 170 authctxt->style ? ":" : "", 171 authctxt->style ? authctxt->style : ""); 172 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 173 (r = sshbuf_put_cstring(b, userstyle)) != 0 || 174 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 || 175 (r = sshbuf_put_cstring(b, "publickey")) != 0 || 176 (r = sshbuf_put_u8(b, have_sig)) != 0 || 177 (r = sshbuf_put_cstring(b, pkalg) != 0) || 178 (r = sshbuf_put_string(b, pkblob, blen)) != 0) 179 fatal("%s: build packet failed: %s", 180 __func__, ssh_err(r)); 181 #ifdef DEBUG_PK 182 sshbuf_dump(b, stderr); 183 #endif 184 185 /* test for correct signature */ 186 authenticated = 0; 187 if (PRIVSEP(user_key_allowed(ssh, pw, key, 1, &authopts)) && 188 PRIVSEP(sshkey_verify(key, sig, slen, sshbuf_ptr(b), 189 sshbuf_len(b), NULL, ssh->compat)) == 0) { 190 authenticated = 1; 191 } 192 sshbuf_free(b); 193 free(sig); 194 auth2_record_key(authctxt, authenticated, key); 195 } else { 196 debug("%s: test pkalg %s pkblob %s%s%s", 197 __func__, pkalg, key_s, 198 ca_s == NULL ? "" : " CA ", 199 ca_s == NULL ? "" : ca_s); 200 201 if ((r = sshpkt_get_end(ssh)) != 0) 202 fatal("%s: %s", __func__, ssh_err(r)); 203 204 /* XXX fake reply and always send PK_OK ? */ 205 /* 206 * XXX this allows testing whether a user is allowed 207 * to login: if you happen to have a valid pubkey this 208 * message is sent. the message is NEVER sent at all 209 * if a user is not allowed to login. is this an 210 * issue? -markus 211 */ 212 if (PRIVSEP(user_key_allowed(ssh, pw, key, 0, NULL))) { 213 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_PK_OK)) 214 != 0 || 215 (r = sshpkt_put_cstring(ssh, pkalg)) != 0 || 216 (r = sshpkt_put_string(ssh, pkblob, blen)) != 0 || 217 (r = sshpkt_send(ssh)) != 0) 218 fatal("%s: %s", __func__, ssh_err(r)); 219 ssh_packet_write_wait(ssh); 220 authctxt->postponed = 1; 221 } 222 } 223 done: 224 if (authenticated == 1 && auth_activate_options(ssh, authopts) != 0) { 225 debug("%s: key options inconsistent with existing", __func__); 226 authenticated = 0; 227 } 228 debug2("%s: authenticated %d pkalg %s", __func__, authenticated, pkalg); 229 230 sshauthopt_free(authopts); 231 sshkey_free(key); 232 free(userstyle); 233 free(pkalg); 234 free(pkblob); 235 free(key_s); 236 free(ca_s); 237 return authenticated; 238 } 239 240 static int 241 match_principals_option(const char *principal_list, struct sshkey_cert *cert) 242 { 243 char *result; 244 u_int i; 245 246 /* XXX percent_expand() sequences for authorized_principals? */ 247 248 for (i = 0; i < cert->nprincipals; i++) { 249 if ((result = match_list(cert->principals[i], 250 principal_list, NULL)) != NULL) { 251 debug3("matched principal from key options \"%.100s\"", 252 result); 253 free(result); 254 return 1; 255 } 256 } 257 return 0; 258 } 259 260 /* 261 * Process a single authorized_principals format line. Returns 0 and sets 262 * authoptsp is principal is authorised, -1 otherwise. "loc" is used as a 263 * log preamble for file/line information. 264 */ 265 static int 266 check_principals_line(struct ssh *ssh, char *cp, const struct sshkey_cert *cert, 267 const char *loc, struct sshauthopt **authoptsp) 268 { 269 u_int i, found = 0; 270 char *ep, *line_opts; 271 const char *reason = NULL; 272 struct sshauthopt *opts = NULL; 273 274 if (authoptsp != NULL) 275 *authoptsp = NULL; 276 277 /* Trim trailing whitespace. */ 278 ep = cp + strlen(cp) - 1; 279 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t')) 280 *ep-- = '\0'; 281 282 /* 283 * If the line has internal whitespace then assume it has 284 * key options. 285 */ 286 line_opts = NULL; 287 if ((ep = strrchr(cp, ' ')) != NULL || 288 (ep = strrchr(cp, '\t')) != NULL) { 289 for (; *ep == ' ' || *ep == '\t'; ep++) 290 ; 291 line_opts = cp; 292 cp = ep; 293 } 294 if ((opts = sshauthopt_parse(line_opts, &reason)) == NULL) { 295 debug("%s: bad principals options: %s", loc, reason); 296 auth_debug_add("%s: bad principals options: %s", loc, reason); 297 return -1; 298 } 299 /* Check principals in cert against those on line */ 300 for (i = 0; i < cert->nprincipals; i++) { 301 if (strcmp(cp, cert->principals[i]) != 0) 302 continue; 303 debug3("%s: matched principal \"%.100s\"", 304 loc, cert->principals[i]); 305 found = 1; 306 } 307 if (found && authoptsp != NULL) { 308 *authoptsp = opts; 309 opts = NULL; 310 } 311 sshauthopt_free(opts); 312 return found ? 0 : -1; 313 } 314 315 static int 316 process_principals(struct ssh *ssh, FILE *f, const char *file, 317 const struct sshkey_cert *cert, struct sshauthopt **authoptsp) 318 { 319 char loc[256], line[SSH_MAX_PUBKEY_BYTES], *cp, *ep; 320 u_long linenum = 0; 321 u_int found_principal = 0; 322 323 if (authoptsp != NULL) 324 *authoptsp = NULL; 325 326 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { 327 /* Always consume entire input */ 328 if (found_principal) 329 continue; 330 331 /* Skip leading whitespace. */ 332 for (cp = line; *cp == ' ' || *cp == '\t'; cp++) 333 ; 334 /* Skip blank and comment lines. */ 335 if ((ep = strchr(cp, '#')) != NULL) 336 *ep = '\0'; 337 if (!*cp || *cp == '\n') 338 continue; 339 340 snprintf(loc, sizeof(loc), "%.200s:%lu", file, linenum); 341 if (check_principals_line(ssh, cp, cert, loc, authoptsp) == 0) 342 found_principal = 1; 343 } 344 return found_principal; 345 } 346 347 /* XXX remove pw args here and elsewhere once ssh->authctxt is guaranteed */ 348 349 static int 350 match_principals_file(struct ssh *ssh, struct passwd *pw, char *file, 351 struct sshkey_cert *cert, struct sshauthopt **authoptsp) 352 { 353 FILE *f; 354 int success; 355 356 if (authoptsp != NULL) 357 *authoptsp = NULL; 358 359 temporarily_use_uid(pw); 360 debug("trying authorized principals file %s", file); 361 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) { 362 restore_uid(); 363 return 0; 364 } 365 success = process_principals(ssh, f, file, cert, authoptsp); 366 fclose(f); 367 restore_uid(); 368 return success; 369 } 370 371 /* 372 * Checks whether principal is allowed in output of command. 373 * returns 1 if the principal is allowed or 0 otherwise. 374 */ 375 static int 376 match_principals_command(struct ssh *ssh, struct passwd *user_pw, 377 const struct sshkey *key, struct sshauthopt **authoptsp) 378 { 379 struct passwd *runas_pw = NULL; 380 const struct sshkey_cert *cert = key->cert; 381 FILE *f = NULL; 382 int r, ok, found_principal = 0; 383 int i, ac = 0, uid_swapped = 0; 384 pid_t pid; 385 char *tmp, *username = NULL, *command = NULL, **av = NULL; 386 char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL; 387 char serial_s[16]; 388 void (*osigchld)(int); 389 390 if (authoptsp != NULL) 391 *authoptsp = NULL; 392 if (options.authorized_principals_command == NULL) 393 return 0; 394 if (options.authorized_principals_command_user == NULL) { 395 error("No user for AuthorizedPrincipalsCommand specified, " 396 "skipping"); 397 return 0; 398 } 399 400 /* 401 * NB. all returns later this function should go via "out" to 402 * ensure the original SIGCHLD handler is restored properly. 403 */ 404 osigchld = signal(SIGCHLD, SIG_DFL); 405 406 /* Prepare and verify the user for the command */ 407 username = percent_expand(options.authorized_principals_command_user, 408 "u", user_pw->pw_name, (char *)NULL); 409 runas_pw = getpwnam(username); 410 if (runas_pw == NULL) { 411 error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s", 412 username, strerror(errno)); 413 goto out; 414 } 415 416 /* Turn the command into an argument vector */ 417 if (argv_split(options.authorized_principals_command, &ac, &av) != 0) { 418 error("AuthorizedPrincipalsCommand \"%s\" contains " 419 "invalid quotes", command); 420 goto out; 421 } 422 if (ac == 0) { 423 error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments", 424 command); 425 goto out; 426 } 427 if ((ca_fp = sshkey_fingerprint(cert->signature_key, 428 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) { 429 error("%s: sshkey_fingerprint failed", __func__); 430 goto out; 431 } 432 if ((key_fp = sshkey_fingerprint(key, 433 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) { 434 error("%s: sshkey_fingerprint failed", __func__); 435 goto out; 436 } 437 if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) { 438 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r)); 439 goto out; 440 } 441 if ((r = sshkey_to_base64(key, &keytext)) != 0) { 442 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r)); 443 goto out; 444 } 445 snprintf(serial_s, sizeof(serial_s), "%llu", 446 (unsigned long long)cert->serial); 447 for (i = 1; i < ac; i++) { 448 tmp = percent_expand(av[i], 449 "u", user_pw->pw_name, 450 "h", user_pw->pw_dir, 451 "t", sshkey_ssh_name(key), 452 "T", sshkey_ssh_name(cert->signature_key), 453 "f", key_fp, 454 "F", ca_fp, 455 "k", keytext, 456 "K", catext, 457 "i", cert->key_id, 458 "s", serial_s, 459 (char *)NULL); 460 if (tmp == NULL) 461 fatal("%s: percent_expand failed", __func__); 462 free(av[i]); 463 av[i] = tmp; 464 } 465 /* Prepare a printable command for logs, etc. */ 466 command = argv_assemble(ac, av); 467 468 if ((pid = subprocess("AuthorizedPrincipalsCommand", runas_pw, command, 469 ac, av, &f, 470 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0) 471 goto out; 472 473 uid_swapped = 1; 474 temporarily_use_uid(runas_pw); 475 476 ok = process_principals(ssh, f, "(command)", cert, authoptsp); 477 478 fclose(f); 479 f = NULL; 480 481 if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command, 0) != 0) 482 goto out; 483 484 /* Read completed successfully */ 485 found_principal = ok; 486 out: 487 if (f != NULL) 488 fclose(f); 489 signal(SIGCHLD, osigchld); 490 for (i = 0; i < ac; i++) 491 free(av[i]); 492 free(av); 493 if (uid_swapped) 494 restore_uid(); 495 free(command); 496 free(username); 497 free(ca_fp); 498 free(key_fp); 499 free(catext); 500 free(keytext); 501 return found_principal; 502 } 503 504 static void 505 skip_space(char **cpp) 506 { 507 char *cp; 508 509 for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++) 510 ; 511 *cpp = cp; 512 } 513 514 /* 515 * Advanced *cpp past the end of key options, defined as the first unquoted 516 * whitespace character. Returns 0 on success or -1 on failure (e.g. 517 * unterminated quotes). 518 */ 519 static int 520 advance_past_options(char **cpp) 521 { 522 char *cp = *cpp; 523 int quoted = 0; 524 525 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { 526 if (*cp == '\\' && cp[1] == '"') 527 cp++; /* Skip both */ 528 else if (*cp == '"') 529 quoted = !quoted; 530 } 531 *cpp = cp; 532 /* return failure for unterminated quotes */ 533 return (*cp == '\0' && quoted) ? -1 : 0; 534 } 535 536 /* 537 * Check a single line of an authorized_keys-format file. Returns 0 if key 538 * matches, -1 otherwise. Will return key/cert options via *authoptsp 539 * on success. "loc" is used as file/line location in log messages. 540 */ 541 static int 542 check_authkey_line(struct ssh *ssh, struct passwd *pw, struct sshkey *key, 543 char *cp, const char *loc, struct sshauthopt **authoptsp) 544 { 545 int want_keytype = sshkey_is_cert(key) ? KEY_UNSPEC : key->type; 546 struct sshkey *found = NULL; 547 struct sshauthopt *keyopts = NULL, *certopts = NULL, *finalopts = NULL; 548 char *key_options = NULL, *fp = NULL; 549 const char *reason = NULL; 550 int ret = -1; 551 552 if (authoptsp != NULL) 553 *authoptsp = NULL; 554 555 if ((found = sshkey_new(want_keytype)) == NULL) { 556 debug3("%s: keytype %d failed", __func__, want_keytype); 557 goto out; 558 } 559 560 /* XXX djm: peek at key type in line and skip if unwanted */ 561 562 if (sshkey_read(found, &cp) != 0) { 563 /* no key? check for options */ 564 debug2("%s: check options: '%s'", loc, cp); 565 key_options = cp; 566 if (advance_past_options(&cp) != 0) { 567 reason = "invalid key option string"; 568 goto fail_reason; 569 } 570 skip_space(&cp); 571 if (sshkey_read(found, &cp) != 0) { 572 /* still no key? advance to next line*/ 573 debug2("%s: advance: '%s'", loc, cp); 574 goto out; 575 } 576 } 577 /* Parse key options now; we need to know if this is a CA key */ 578 if ((keyopts = sshauthopt_parse(key_options, &reason)) == NULL) { 579 debug("%s: bad key options: %s", loc, reason); 580 auth_debug_add("%s: bad key options: %s", loc, reason); 581 goto out; 582 } 583 /* Ignore keys that don't match or incorrectly marked as CAs */ 584 if (sshkey_is_cert(key)) { 585 /* Certificate; check signature key against CA */ 586 if (!sshkey_equal(found, key->cert->signature_key) || 587 !keyopts->cert_authority) 588 goto out; 589 } else { 590 /* Plain key: check it against key found in file */ 591 if (!sshkey_equal(found, key) || keyopts->cert_authority) 592 goto out; 593 } 594 595 /* We have a candidate key, perform authorisation checks */ 596 if ((fp = sshkey_fingerprint(found, 597 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) 598 fatal("%s: fingerprint failed", __func__); 599 600 debug("%s: matching %s found: %s %s", loc, 601 sshkey_is_cert(key) ? "CA" : "key", sshkey_type(found), fp); 602 603 if (auth_authorise_keyopts(ssh, pw, keyopts, 604 sshkey_is_cert(key), loc) != 0) { 605 reason = "Refused by key options"; 606 goto fail_reason; 607 } 608 /* That's all we need for plain keys. */ 609 if (!sshkey_is_cert(key)) { 610 verbose("Accepted key %s %s found at %s", 611 sshkey_type(found), fp, loc); 612 finalopts = keyopts; 613 keyopts = NULL; 614 goto success; 615 } 616 617 /* 618 * Additional authorisation for certificates. 619 */ 620 621 /* Parse and check options present in certificate */ 622 if ((certopts = sshauthopt_from_cert(key)) == NULL) { 623 reason = "Invalid certificate options"; 624 goto fail_reason; 625 } 626 if (auth_authorise_keyopts(ssh, pw, certopts, 0, loc) != 0) { 627 reason = "Refused by certificate options"; 628 goto fail_reason; 629 } 630 if ((finalopts = sshauthopt_merge(keyopts, certopts, &reason)) == NULL) 631 goto fail_reason; 632 633 /* 634 * If the user has specified a list of principals as 635 * a key option, then prefer that list to matching 636 * their username in the certificate principals list. 637 */ 638 if (keyopts->cert_principals != NULL && 639 !match_principals_option(keyopts->cert_principals, key->cert)) { 640 reason = "Certificate does not contain an authorized principal"; 641 goto fail_reason; 642 } 643 if (sshkey_cert_check_authority(key, 0, 0, 644 keyopts->cert_principals == NULL ? pw->pw_name : NULL, &reason) != 0) 645 goto fail_reason; 646 647 verbose("Accepted certificate ID \"%s\" (serial %llu) " 648 "signed by CA %s %s found at %s", 649 key->cert->key_id, 650 (unsigned long long)key->cert->serial, 651 sshkey_type(found), fp, loc); 652 653 success: 654 if (finalopts == NULL) 655 fatal("%s: internal error: missing options", __func__); 656 if (authoptsp != NULL) { 657 *authoptsp = finalopts; 658 finalopts = NULL; 659 } 660 /* success */ 661 ret = 0; 662 goto out; 663 664 fail_reason: 665 error("%s", reason); 666 auth_debug_add("%s", reason); 667 out: 668 free(fp); 669 sshauthopt_free(keyopts); 670 sshauthopt_free(certopts); 671 sshauthopt_free(finalopts); 672 sshkey_free(found); 673 return ret; 674 } 675 676 /* 677 * Checks whether key is allowed in authorized_keys-format file, 678 * returns 1 if the key is allowed or 0 otherwise. 679 */ 680 static int 681 check_authkeys_file(struct ssh *ssh, struct passwd *pw, FILE *f, 682 char *file, struct sshkey *key, struct sshauthopt **authoptsp) 683 { 684 char *cp, line[SSH_MAX_PUBKEY_BYTES], loc[256]; 685 int found_key = 0; 686 u_long linenum = 0; 687 688 if (authoptsp != NULL) 689 *authoptsp = NULL; 690 691 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { 692 /* Always consume entire file */ 693 if (found_key) 694 continue; 695 696 /* Skip leading whitespace, empty and comment lines. */ 697 cp = line; 698 skip_space(&cp); 699 if (!*cp || *cp == '\n' || *cp == '#') 700 continue; 701 snprintf(loc, sizeof(loc), "%.200s:%lu", file, linenum); 702 if (check_authkey_line(ssh, pw, key, cp, loc, authoptsp) == 0) 703 found_key = 1; 704 } 705 return found_key; 706 } 707 708 /* Authenticate a certificate key against TrustedUserCAKeys */ 709 static int 710 user_cert_trusted_ca(struct ssh *ssh, struct passwd *pw, struct sshkey *key, 711 struct sshauthopt **authoptsp) 712 { 713 char *ca_fp, *principals_file = NULL; 714 const char *reason; 715 struct sshauthopt *principals_opts = NULL, *cert_opts = NULL; 716 struct sshauthopt *final_opts = NULL; 717 int r, ret = 0, found_principal = 0, use_authorized_principals; 718 719 if (authoptsp != NULL) 720 *authoptsp = NULL; 721 722 if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL) 723 return 0; 724 725 if ((ca_fp = sshkey_fingerprint(key->cert->signature_key, 726 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) 727 return 0; 728 729 if ((r = sshkey_in_file(key->cert->signature_key, 730 options.trusted_user_ca_keys, 1, 0)) != 0) { 731 debug2("%s: CA %s %s is not listed in %s: %s", __func__, 732 sshkey_type(key->cert->signature_key), ca_fp, 733 options.trusted_user_ca_keys, ssh_err(r)); 734 goto out; 735 } 736 /* 737 * If AuthorizedPrincipals is in use, then compare the certificate 738 * principals against the names in that file rather than matching 739 * against the username. 740 */ 741 if ((principals_file = authorized_principals_file(pw)) != NULL) { 742 if (match_principals_file(ssh, pw, principals_file, 743 key->cert, &principals_opts)) 744 found_principal = 1; 745 } 746 /* Try querying command if specified */ 747 if (!found_principal && match_principals_command(ssh, pw, key, 748 &principals_opts)) 749 found_principal = 1; 750 /* If principals file or command is specified, then require a match */ 751 use_authorized_principals = principals_file != NULL || 752 options.authorized_principals_command != NULL; 753 if (!found_principal && use_authorized_principals) { 754 reason = "Certificate does not contain an authorized principal"; 755 goto fail_reason; 756 } 757 if (use_authorized_principals && principals_opts == NULL) 758 fatal("%s: internal error: missing principals_opts", __func__); 759 if (sshkey_cert_check_authority(key, 0, 1, 760 use_authorized_principals ? NULL : pw->pw_name, &reason) != 0) 761 goto fail_reason; 762 763 /* Check authority from options in key and from principals file/cmd */ 764 if ((cert_opts = sshauthopt_from_cert(key)) == NULL) { 765 reason = "Invalid certificate options"; 766 goto fail_reason; 767 } 768 if (auth_authorise_keyopts(ssh, pw, cert_opts, 0, "cert") != 0) { 769 reason = "Refused by certificate options"; 770 goto fail_reason; 771 } 772 if (principals_opts == NULL) { 773 final_opts = cert_opts; 774 cert_opts = NULL; 775 } else { 776 if (auth_authorise_keyopts(ssh, pw, principals_opts, 0, 777 "principals") != 0) { 778 reason = "Refused by certificate principals options"; 779 goto fail_reason; 780 } 781 if ((final_opts = sshauthopt_merge(principals_opts, 782 cert_opts, &reason)) == NULL) { 783 fail_reason: 784 error("%s", reason); 785 auth_debug_add("%s", reason); 786 goto out; 787 } 788 } 789 790 /* Success */ 791 verbose("Accepted certificate ID \"%s\" (serial %llu) signed by " 792 "%s CA %s via %s", key->cert->key_id, 793 (unsigned long long)key->cert->serial, 794 sshkey_type(key->cert->signature_key), ca_fp, 795 options.trusted_user_ca_keys); 796 if (authoptsp != NULL) { 797 *authoptsp = final_opts; 798 final_opts = NULL; 799 } 800 ret = 1; 801 out: 802 sshauthopt_free(principals_opts); 803 sshauthopt_free(cert_opts); 804 sshauthopt_free(final_opts); 805 free(principals_file); 806 free(ca_fp); 807 return ret; 808 } 809 810 /* 811 * Checks whether key is allowed in file. 812 * returns 1 if the key is allowed or 0 otherwise. 813 */ 814 static int 815 user_key_allowed2(struct ssh *ssh, struct passwd *pw, struct sshkey *key, 816 char *file, struct sshauthopt **authoptsp) 817 { 818 FILE *f; 819 int found_key = 0; 820 821 if (authoptsp != NULL) 822 *authoptsp = NULL; 823 824 /* Temporarily use the user's uid. */ 825 temporarily_use_uid(pw); 826 827 debug("trying public key file %s", file); 828 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) { 829 found_key = check_authkeys_file(ssh, pw, f, file, 830 key, authoptsp); 831 fclose(f); 832 } 833 834 restore_uid(); 835 return found_key; 836 } 837 838 /* 839 * Checks whether key is allowed in output of command. 840 * returns 1 if the key is allowed or 0 otherwise. 841 */ 842 static int 843 user_key_command_allowed2(struct ssh *ssh, struct passwd *user_pw, 844 struct sshkey *key, struct sshauthopt **authoptsp) 845 { 846 struct passwd *runas_pw = NULL; 847 FILE *f = NULL; 848 int r, ok, found_key = 0; 849 int i, uid_swapped = 0, ac = 0; 850 pid_t pid; 851 char *username = NULL, *key_fp = NULL, *keytext = NULL; 852 char *tmp, *command = NULL, **av = NULL; 853 void (*osigchld)(int); 854 855 if (authoptsp != NULL) 856 *authoptsp = NULL; 857 if (options.authorized_keys_command == NULL) 858 return 0; 859 if (options.authorized_keys_command_user == NULL) { 860 error("No user for AuthorizedKeysCommand specified, skipping"); 861 return 0; 862 } 863 864 /* 865 * NB. all returns later this function should go via "out" to 866 * ensure the original SIGCHLD handler is restored properly. 867 */ 868 osigchld = signal(SIGCHLD, SIG_DFL); 869 870 /* Prepare and verify the user for the command */ 871 username = percent_expand(options.authorized_keys_command_user, 872 "u", user_pw->pw_name, (char *)NULL); 873 runas_pw = getpwnam(username); 874 if (runas_pw == NULL) { 875 error("AuthorizedKeysCommandUser \"%s\" not found: %s", 876 username, strerror(errno)); 877 goto out; 878 } 879 880 /* Prepare AuthorizedKeysCommand */ 881 if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash, 882 SSH_FP_DEFAULT)) == NULL) { 883 error("%s: sshkey_fingerprint failed", __func__); 884 goto out; 885 } 886 if ((r = sshkey_to_base64(key, &keytext)) != 0) { 887 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r)); 888 goto out; 889 } 890 891 /* Turn the command into an argument vector */ 892 if (argv_split(options.authorized_keys_command, &ac, &av) != 0) { 893 error("AuthorizedKeysCommand \"%s\" contains invalid quotes", 894 command); 895 goto out; 896 } 897 if (ac == 0) { 898 error("AuthorizedKeysCommand \"%s\" yielded no arguments", 899 command); 900 goto out; 901 } 902 for (i = 1; i < ac; i++) { 903 tmp = percent_expand(av[i], 904 "u", user_pw->pw_name, 905 "h", user_pw->pw_dir, 906 "t", sshkey_ssh_name(key), 907 "f", key_fp, 908 "k", keytext, 909 (char *)NULL); 910 if (tmp == NULL) 911 fatal("%s: percent_expand failed", __func__); 912 free(av[i]); 913 av[i] = tmp; 914 } 915 /* Prepare a printable command for logs, etc. */ 916 command = argv_assemble(ac, av); 917 918 /* 919 * If AuthorizedKeysCommand was run without arguments 920 * then fall back to the old behaviour of passing the 921 * target username as a single argument. 922 */ 923 if (ac == 1) { 924 av = xreallocarray(av, ac + 2, sizeof(*av)); 925 av[1] = xstrdup(user_pw->pw_name); 926 av[2] = NULL; 927 /* Fix up command too, since it is used in log messages */ 928 free(command); 929 xasprintf(&command, "%s %s", av[0], av[1]); 930 } 931 932 if ((pid = subprocess("AuthorizedKeysCommand", runas_pw, command, 933 ac, av, &f, 934 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0) 935 goto out; 936 937 uid_swapped = 1; 938 temporarily_use_uid(runas_pw); 939 940 ok = check_authkeys_file(ssh, user_pw, f, 941 options.authorized_keys_command, key, authoptsp); 942 943 fclose(f); 944 f = NULL; 945 946 if (exited_cleanly(pid, "AuthorizedKeysCommand", command, 0) != 0) 947 goto out; 948 949 /* Read completed successfully */ 950 found_key = ok; 951 out: 952 if (f != NULL) 953 fclose(f); 954 signal(SIGCHLD, osigchld); 955 for (i = 0; i < ac; i++) 956 free(av[i]); 957 free(av); 958 if (uid_swapped) 959 restore_uid(); 960 free(command); 961 free(username); 962 free(key_fp); 963 free(keytext); 964 return found_key; 965 } 966 967 /* 968 * Check whether key authenticates and authorises the user. 969 */ 970 int 971 user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key, 972 int auth_attempt, struct sshauthopt **authoptsp) 973 { 974 u_int success, i; 975 char *file; 976 struct sshauthopt *opts = NULL; 977 if (authoptsp != NULL) 978 *authoptsp = NULL; 979 980 if (auth_key_is_revoked(key)) 981 return 0; 982 if (sshkey_is_cert(key) && 983 auth_key_is_revoked(key->cert->signature_key)) 984 return 0; 985 986 if ((success = user_cert_trusted_ca(ssh, pw, key, &opts)) != 0) 987 goto out; 988 sshauthopt_free(opts); 989 opts = NULL; 990 991 if ((success = user_key_command_allowed2(ssh, pw, key, &opts)) != 0) 992 goto out; 993 sshauthopt_free(opts); 994 opts = NULL; 995 996 for (i = 0; !success && i < options.num_authkeys_files; i++) { 997 if (strcasecmp(options.authorized_keys_files[i], "none") == 0) 998 continue; 999 file = expand_authorized_keys( 1000 options.authorized_keys_files[i], pw); 1001 success = user_key_allowed2(ssh, pw, key, file, &opts); 1002 free(file); 1003 } 1004 1005 out: 1006 if (success && authoptsp != NULL) { 1007 *authoptsp = opts; 1008 opts = NULL; 1009 } 1010 sshauthopt_free(opts); 1011 return success; 1012 } 1013 1014 Authmethod method_pubkey = { 1015 "publickey", 1016 userauth_pubkey, 1017 &options.pubkey_authentication 1018 }; 1019