1 /* $NetBSD: auth2-pubkey.c,v 1.18 2017/10/07 19:39:19 christos Exp $ */ 2 /* $OpenBSD: auth2-pubkey.c,v 1.71 2017/09/07 23:48:09 djm Exp $ */ 3 /* 4 * Copyright (c) 2000 Markus Friedl. 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 __RCSID("$NetBSD: auth2-pubkey.c,v 1.18 2017/10/07 19:39:19 christos Exp $"); 29 #include <sys/types.h> 30 #include <sys/stat.h> 31 32 #include <errno.h> 33 #include <fcntl.h> 34 #include <paths.h> 35 #include <pwd.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 "buffer.h" 49 #include "log.h" 50 #include "misc.h" 51 #include "servconf.h" 52 #include "compat.h" 53 #include "sshkey.h" 54 #include "hostfile.h" 55 #include "auth.h" 56 #include "pathnames.h" 57 #include "uidswap.h" 58 #include "auth-options.h" 59 #include "canohost.h" 60 #ifdef GSSAPI 61 #include "ssh-gss.h" 62 #endif 63 #include "monitor_wrap.h" 64 #include "authfile.h" 65 #include "match.h" 66 #include "digest.h" 67 68 #ifdef WITH_LDAP_PUBKEY 69 #include "ldapauth.h" 70 #endif 71 72 #include "ssherr.h" 73 #include "channels.h" /* XXX for session.h */ 74 #include "session.h" /* XXX for child_set_env(); refactor? */ 75 76 /* import */ 77 extern ServerOptions options; 78 extern u_char *session_id2; 79 extern u_int session_id2_len; 80 81 static int 82 userauth_pubkey(struct ssh *ssh) 83 { 84 Authctxt *authctxt = ssh->authctxt; 85 struct sshbuf *b; 86 struct sshkey *key = NULL; 87 char *pkalg, *userstyle = NULL, *fp = NULL; 88 u_char *pkblob, *sig, have_sig; 89 size_t blen, slen; 90 int r, pktype; 91 int authenticated = 0; 92 93 if (!authctxt->valid) { 94 debug2("%s: disabled because of invalid user", __func__); 95 return 0; 96 } 97 if ((r = sshpkt_get_u8(ssh, &have_sig)) != 0) 98 fatal("%s: sshpkt_get_u8 failed: %s", __func__, ssh_err(r)); 99 if (ssh->compat & SSH_BUG_PKAUTH) { 100 debug2("%s: SSH_BUG_PKAUTH", __func__); 101 if ((b = sshbuf_new()) == NULL) 102 fatal("%s: sshbuf_new failed", __func__); 103 /* no explicit pkalg given */ 104 /* so we have to extract the pkalg from the pkblob */ 105 /* XXX use sshbuf_from() */ 106 if ((r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 || 107 (r = sshbuf_put(b, pkblob, blen)) != 0 || 108 (r = sshbuf_get_cstring(b, &pkalg, NULL)) != 0) 109 fatal("%s: failed: %s", __func__, ssh_err(r)); 110 sshbuf_free(b); 111 } else { 112 if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 || 113 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0) 114 fatal("%s: sshpkt_get_cstring failed: %s", 115 __func__, ssh_err(r)); 116 } 117 pktype = sshkey_type_from_name(pkalg); 118 if (pktype == KEY_UNSPEC) { 119 /* this is perfectly legal */ 120 logit("%s: unsupported public key algorithm: %s", 121 __func__, pkalg); 122 goto done; 123 } 124 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) { 125 error("%s: could not parse key: %s", __func__, ssh_err(r)); 126 goto done; 127 } 128 if (key == NULL) { 129 error("%s: cannot decode key: %s", __func__, pkalg); 130 goto done; 131 } 132 if (key->type != pktype) { 133 error("%s: type mismatch for decoded key " 134 "(received %d, expected %d)", __func__, key->type, pktype); 135 goto done; 136 } 137 if (sshkey_type_plain(key->type) == KEY_RSA && 138 (ssh->compat & SSH_BUG_RSASIGMD5) != 0) { 139 logit("Refusing RSA key because client uses unsafe " 140 "signature scheme"); 141 goto done; 142 } 143 fp = sshkey_fingerprint(key, options.fingerprint_hash, SSH_FP_DEFAULT); 144 if (auth2_key_already_used(authctxt, key)) { 145 logit("refusing previously-used %s key", sshkey_type(key)); 146 goto done; 147 } 148 if (match_pattern_list(sshkey_ssh_name(key), 149 options.pubkey_key_types, 0) != 1) { 150 logit("%s: key type %s not in PubkeyAcceptedKeyTypes", 151 __func__, sshkey_ssh_name(key)); 152 goto done; 153 } 154 155 if (have_sig) { 156 debug3("%s: have signature for %s %s", 157 __func__, sshkey_type(key), fp); 158 if ((r = sshpkt_get_string(ssh, &sig, &slen)) != 0 || 159 (r = sshpkt_get_end(ssh)) != 0) 160 fatal("%s: %s", __func__, ssh_err(r)); 161 if ((b = sshbuf_new()) == NULL) 162 fatal("%s: sshbuf_new failed", __func__); 163 if (ssh->compat & SSH_OLD_SESSIONID) { 164 if ((r = sshbuf_put(b, session_id2, 165 session_id2_len)) != 0) 166 fatal("%s: sshbuf_put session id: %s", 167 __func__, ssh_err(r)); 168 } else { 169 if ((r = sshbuf_put_string(b, session_id2, 170 session_id2_len)) != 0) 171 fatal("%s: sshbuf_put_string session id: %s", 172 __func__, ssh_err(r)); 173 } 174 /* reconstruct packet */ 175 xasprintf(&userstyle, "%s%s%s", authctxt->user, 176 authctxt->style ? ":" : "", 177 authctxt->style ? authctxt->style : ""); 178 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 179 (r = sshbuf_put_cstring(b, userstyle)) != 0 || 180 (r = sshbuf_put_cstring(b, ssh->compat & SSH_BUG_PKSERVICE ? 181 "ssh-userauth" : authctxt->service)) != 0) 182 fatal("%s: build packet failed: %s", 183 __func__, ssh_err(r)); 184 if (ssh->compat & SSH_BUG_PKAUTH) { 185 if ((r = sshbuf_put_u8(b, have_sig)) != 0) 186 fatal("%s: build packet failed: %s", 187 __func__, ssh_err(r)); 188 } else { 189 if ((r = sshbuf_put_cstring(b, "publickey")) != 0 || 190 (r = sshbuf_put_u8(b, have_sig)) != 0 || 191 (r = sshbuf_put_cstring(b, pkalg) != 0)) 192 fatal("%s: build packet failed: %s", 193 __func__, ssh_err(r)); 194 } 195 if ((r = sshbuf_put_string(b, pkblob, blen)) != 0) 196 fatal("%s: build packet failed: %s", 197 __func__, ssh_err(r)); 198 #ifdef DEBUG_PK 199 sshbuf_dump(b, stderr); 200 #endif 201 202 /* test for correct signature */ 203 authenticated = 0; 204 if (PRIVSEP(user_key_allowed(authctxt->pw, key, 1)) && 205 PRIVSEP(sshkey_verify(key, sig, slen, sshbuf_ptr(b), 206 sshbuf_len(b), ssh->compat)) == 0) { 207 authenticated = 1; 208 } 209 sshbuf_free(b); 210 free(sig); 211 auth2_record_key(authctxt, authenticated, key); 212 } else { 213 debug("%s: test whether pkalg/pkblob are acceptable for %s %s", 214 __func__, sshkey_type(key), fp); 215 if ((r = sshpkt_get_end(ssh)) != 0) 216 fatal("%s: %s", __func__, ssh_err(r)); 217 218 /* XXX fake reply and always send PK_OK ? */ 219 /* 220 * XXX this allows testing whether a user is allowed 221 * to login: if you happen to have a valid pubkey this 222 * message is sent. the message is NEVER sent at all 223 * if a user is not allowed to login. is this an 224 * issue? -markus 225 */ 226 if (PRIVSEP(user_key_allowed(authctxt->pw, key, 0))) { 227 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_PK_OK)) 228 != 0 || 229 (r = sshpkt_put_cstring(ssh, pkalg)) != 0 || 230 (r = sshpkt_put_string(ssh, pkblob, blen)) != 0 || 231 (r = sshpkt_send(ssh)) != 0) 232 fatal("%s: %s", __func__, ssh_err(r)); 233 ssh_packet_write_wait(ssh); 234 authctxt->postponed = 1; 235 } 236 } 237 if (authenticated != 1) 238 auth_clear_options(); 239 done: 240 debug2("%s: authenticated %d pkalg %s", __func__, authenticated, pkalg); 241 sshkey_free(key); 242 free(userstyle); 243 free(pkalg); 244 free(pkblob); 245 free(fp); 246 return authenticated; 247 } 248 249 static int 250 match_principals_option(const char *principal_list, struct sshkey_cert *cert) 251 { 252 char *result; 253 u_int i; 254 255 /* XXX percent_expand() sequences for authorized_principals? */ 256 257 for (i = 0; i < cert->nprincipals; i++) { 258 if ((result = match_list(cert->principals[i], 259 principal_list, NULL)) != NULL) { 260 debug3("matched principal from key options \"%.100s\"", 261 result); 262 free(result); 263 return 1; 264 } 265 } 266 return 0; 267 } 268 269 static int 270 process_principals(FILE *f, const char *file, struct passwd *pw, 271 const struct sshkey_cert *cert) 272 { 273 char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts; 274 u_long linenum = 0; 275 u_int i, found_principal = 0; 276 277 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { 278 /* Always consume entire input */ 279 if (found_principal) 280 continue; 281 /* Skip leading whitespace. */ 282 for (cp = line; *cp == ' ' || *cp == '\t'; cp++) 283 ; 284 /* Skip blank and comment lines. */ 285 if ((ep = strchr(cp, '#')) != NULL) 286 *ep = '\0'; 287 if (!*cp || *cp == '\n') 288 continue; 289 /* Trim trailing whitespace. */ 290 ep = cp + strlen(cp) - 1; 291 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t')) 292 *ep-- = '\0'; 293 /* 294 * If the line has internal whitespace then assume it has 295 * key options. 296 */ 297 line_opts = NULL; 298 if ((ep = strrchr(cp, ' ')) != NULL || 299 (ep = strrchr(cp, '\t')) != NULL) { 300 for (; *ep == ' ' || *ep == '\t'; ep++) 301 ; 302 line_opts = cp; 303 cp = ep; 304 } 305 for (i = 0; i < cert->nprincipals; i++) { 306 if (strcmp(cp, cert->principals[i]) == 0) { 307 debug3("%s:%lu: matched principal \"%.100s\"", 308 file, linenum, cert->principals[i]); 309 if (auth_parse_options(pw, line_opts, 310 file, linenum) != 1) 311 continue; 312 found_principal = 1; 313 continue; 314 } 315 } 316 } 317 return found_principal; 318 } 319 320 static int 321 match_principals_file(char *file, struct passwd *pw, struct sshkey_cert *cert) 322 { 323 FILE *f; 324 int success; 325 326 temporarily_use_uid(pw); 327 debug("trying authorized principals file %s", file); 328 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) { 329 restore_uid(); 330 return 0; 331 } 332 success = process_principals(f, file, pw, cert); 333 fclose(f); 334 restore_uid(); 335 return success; 336 } 337 338 /* 339 * Checks whether principal is allowed in output of command. 340 * returns 1 if the principal is allowed or 0 otherwise. 341 */ 342 static int 343 match_principals_command(struct passwd *user_pw, const struct sshkey *key) 344 { 345 const struct sshkey_cert *cert = key->cert; 346 FILE *f = NULL; 347 int r, ok, found_principal = 0; 348 struct passwd *pw; 349 int i, ac = 0, uid_swapped = 0; 350 pid_t pid; 351 char *tmp, *username = NULL, *command = NULL, **av = NULL; 352 char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL; 353 char serial_s[16]; 354 void (*osigchld)(int); 355 356 if (options.authorized_principals_command == NULL) 357 return 0; 358 if (options.authorized_principals_command_user == NULL) { 359 error("No user for AuthorizedPrincipalsCommand specified, " 360 "skipping"); 361 return 0; 362 } 363 364 /* 365 * NB. all returns later this function should go via "out" to 366 * ensure the original SIGCHLD handler is restored properly. 367 */ 368 osigchld = signal(SIGCHLD, SIG_DFL); 369 370 /* Prepare and verify the user for the command */ 371 username = percent_expand(options.authorized_principals_command_user, 372 "u", user_pw->pw_name, (char *)NULL); 373 pw = getpwnam(username); 374 if (pw == NULL) { 375 error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s", 376 username, strerror(errno)); 377 goto out; 378 } 379 380 /* Turn the command into an argument vector */ 381 if (argv_split(options.authorized_principals_command, &ac, &av) != 0) { 382 error("AuthorizedPrincipalsCommand \"%s\" contains " 383 "invalid quotes", command); 384 goto out; 385 } 386 if (ac == 0) { 387 error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments", 388 command); 389 goto out; 390 } 391 if ((ca_fp = sshkey_fingerprint(cert->signature_key, 392 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) { 393 error("%s: sshkey_fingerprint failed", __func__); 394 goto out; 395 } 396 if ((key_fp = sshkey_fingerprint(key, 397 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) { 398 error("%s: sshkey_fingerprint failed", __func__); 399 goto out; 400 } 401 if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) { 402 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r)); 403 goto out; 404 } 405 if ((r = sshkey_to_base64(key, &keytext)) != 0) { 406 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r)); 407 goto out; 408 } 409 snprintf(serial_s, sizeof(serial_s), "%llu", 410 (unsigned long long)cert->serial); 411 for (i = 1; i < ac; i++) { 412 tmp = percent_expand(av[i], 413 "u", user_pw->pw_name, 414 "h", user_pw->pw_dir, 415 "t", sshkey_ssh_name(key), 416 "T", sshkey_ssh_name(cert->signature_key), 417 "f", key_fp, 418 "F", ca_fp, 419 "k", keytext, 420 "K", catext, 421 "i", cert->key_id, 422 "s", serial_s, 423 (char *)NULL); 424 if (tmp == NULL) 425 fatal("%s: percent_expand failed", __func__); 426 free(av[i]); 427 av[i] = tmp; 428 } 429 /* Prepare a printable command for logs, etc. */ 430 command = argv_assemble(ac, av); 431 432 if ((pid = subprocess("AuthorizedPrincipalsCommand", pw, command, 433 ac, av, &f, 434 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0) 435 goto out; 436 437 uid_swapped = 1; 438 temporarily_use_uid(pw); 439 440 ok = process_principals(f, "(command)", pw, cert); 441 442 fclose(f); 443 f = NULL; 444 445 if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command, 0) != 0) 446 goto out; 447 448 /* Read completed successfully */ 449 found_principal = ok; 450 out: 451 if (f != NULL) 452 fclose(f); 453 signal(SIGCHLD, osigchld); 454 for (i = 0; i < ac; i++) 455 free(av[i]); 456 free(av); 457 if (uid_swapped) 458 restore_uid(); 459 free(command); 460 free(username); 461 free(ca_fp); 462 free(key_fp); 463 free(catext); 464 free(keytext); 465 return found_principal; 466 } 467 /* 468 * Checks whether key is allowed in authorized_keys-format file, 469 * returns 1 if the key is allowed or 0 otherwise. 470 */ 471 static int 472 check_authkeys_file(FILE *f, char *file, struct sshkey *key, struct passwd *pw) 473 { 474 char line[SSH_MAX_PUBKEY_BYTES]; 475 int found_key = 0; 476 u_long linenum = 0; 477 struct sshkey *found = NULL; 478 #ifdef WITH_LDAP_PUBKEY 479 ldap_key_t * k; 480 unsigned int i = 0; 481 #endif 482 483 #ifdef WITH_LDAP_PUBKEY 484 found_key = 0; 485 /* allocate a new key type */ 486 found = sshkey_new(key->type); 487 488 /* first check if the options is enabled, then try.. */ 489 if (options.lpk.on) { 490 debug("[LDAP] trying LDAP first uid=%s",pw->pw_name); 491 if (ldap_ismember(&options.lpk, pw->pw_name) > 0) { 492 if ((k = ldap_getuserkey(&options.lpk, pw->pw_name)) != NULL) { 493 /* Skip leading whitespace, empty and comment lines. */ 494 for (i = 0 ; i < k->num ; i++) { 495 /* dont forget if multiple keys to reset options */ 496 char *cp, *xoptions = NULL; 497 498 for (cp = (char *)k->keys[i]->bv_val; *cp == ' ' || *cp == '\t'; cp++) 499 ; 500 if (!*cp || *cp == '\n' || *cp == '#') 501 continue; 502 503 if (sshkey_read(found, &cp) != 0) { 504 /* no key? check if there are options for this key */ 505 int quoted = 0; 506 debug2("[LDAP] user_key_allowed: check options: '%s'", cp); 507 xoptions = cp; 508 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { 509 if (*cp == '\\' && cp[1] == '"') 510 cp++; /* Skip both */ 511 else if (*cp == '"') 512 quoted = !quoted; 513 } 514 /* Skip remaining whitespace. */ 515 for (; *cp == ' ' || *cp == '\t'; cp++) 516 ; 517 if (sshkey_read(found, &cp) != 0) { 518 debug2("[LDAP] user_key_allowed: advance: '%s'", cp); 519 /* still no key? advance to next line*/ 520 continue; 521 } 522 } 523 524 if (sshkey_equal(found, key) && 525 auth_parse_options(pw, xoptions, file, linenum) == 1) { 526 found_key = 1; 527 debug("[LDAP] matching key found"); 528 char *fp = sshkey_fingerprint(found, SSH_FP_HASH_DEFAULT, SSH_FP_HEX); 529 verbose("[LDAP] Found matching %s key: %s", sshkey_type(found), fp); 530 531 /* restoring memory */ 532 ldap_keys_free(k); 533 free(fp); 534 restore_uid(); 535 sshkey_free(found); 536 return found_key; 537 break; 538 } 539 }/* end of LDAP for() */ 540 } else { 541 logit("[LDAP] no keys found for '%s'!", pw->pw_name); 542 } 543 } else { 544 logit("[LDAP] '%s' is not in '%s'", pw->pw_name, options.lpk.sgroup); 545 } 546 } 547 #endif 548 549 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { 550 char *cp, *key_options = NULL, *fp = NULL; 551 const char *reason = NULL; 552 553 /* Always consume entire file */ 554 if (found_key) 555 continue; 556 if (found != NULL) 557 sshkey_free(found); 558 found = sshkey_new(sshkey_is_cert(key) ? KEY_UNSPEC : key->type); 559 if (found == NULL) 560 goto done; 561 auth_clear_options(); 562 563 /* Skip leading whitespace, empty and comment lines. */ 564 for (cp = line; *cp == ' ' || *cp == '\t'; cp++) 565 ; 566 if (!*cp || *cp == '\n' || *cp == '#') 567 continue; 568 569 if (sshkey_read(found, &cp) != 0) { 570 /* no key? check if there are options for this key */ 571 int quoted = 0; 572 debug2("user_key_allowed: check options: '%s'", cp); 573 key_options = cp; 574 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { 575 if (*cp == '\\' && cp[1] == '"') 576 cp++; /* Skip both */ 577 else if (*cp == '"') 578 quoted = !quoted; 579 } 580 /* Skip remaining whitespace. */ 581 for (; *cp == ' ' || *cp == '\t'; cp++) 582 ; 583 if (sshkey_read(found, &cp) != 0) { 584 debug2("user_key_allowed: advance: '%s'", cp); 585 /* still no key? advance to next line*/ 586 continue; 587 } 588 } 589 if (sshkey_is_cert(key)) { 590 if (!sshkey_equal(found, key->cert->signature_key)) 591 continue; 592 if (auth_parse_options(pw, key_options, file, 593 linenum) != 1) 594 continue; 595 if (!key_is_cert_authority) 596 continue; 597 if ((fp = sshkey_fingerprint(found, 598 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) 599 continue; 600 debug("matching CA found: file %s, line %lu, %s %s", 601 file, linenum, sshkey_type(found), fp); 602 /* 603 * If the user has specified a list of principals as 604 * a key option, then prefer that list to matching 605 * their username in the certificate principals list. 606 */ 607 if (authorized_principals != NULL && 608 !match_principals_option(authorized_principals, 609 key->cert)) { 610 reason = "Certificate does not contain an " 611 "authorized principal"; 612 fail_reason: 613 free(fp); 614 error("%s", reason); 615 auth_debug_add("%s", reason); 616 continue; 617 } 618 if (sshkey_cert_check_authority(key, 0, 0, 619 authorized_principals == NULL ? pw->pw_name : NULL, 620 &reason) != 0) 621 goto fail_reason; 622 if (auth_cert_options(key, pw, &reason) != 0) 623 goto fail_reason; 624 verbose("Accepted certificate ID \"%s\" (serial %llu) " 625 "signed by %s CA %s via %s", key->cert->key_id, 626 (unsigned long long)key->cert->serial, 627 sshkey_type(found), fp, file); 628 free(fp); 629 found_key = 1; 630 break; 631 } else if (sshkey_equal(found, key)) { 632 if (auth_parse_options(pw, key_options, file, 633 linenum) != 1) 634 continue; 635 if (key_is_cert_authority) 636 continue; 637 if ((fp = sshkey_fingerprint(found, 638 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) 639 continue; 640 debug("matching key found: file %s, line %lu %s %s", 641 file, linenum, sshkey_type(found), fp); 642 free(fp); 643 found_key = 1; 644 continue; 645 } 646 } 647 done: 648 if (found != NULL) 649 sshkey_free(found); 650 if (!found_key) 651 debug2("key not found"); 652 return found_key; 653 } 654 655 /* Authenticate a certificate key against TrustedUserCAKeys */ 656 static int 657 user_cert_trusted_ca(struct passwd *pw, struct sshkey *key) 658 { 659 char *ca_fp, *principals_file = NULL; 660 const char *reason; 661 int r, ret = 0, found_principal = 0, use_authorized_principals; 662 663 if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL) 664 return 0; 665 666 if ((ca_fp = sshkey_fingerprint(key->cert->signature_key, 667 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) 668 return 0; 669 670 if ((r = sshkey_in_file(key->cert->signature_key, 671 options.trusted_user_ca_keys, 1, 0)) != 0) { 672 debug2("%s: CA %s %s is not listed in %s: %s", __func__, 673 sshkey_type(key->cert->signature_key), ca_fp, 674 options.trusted_user_ca_keys, ssh_err(r)); 675 goto out; 676 } 677 /* 678 * If AuthorizedPrincipals is in use, then compare the certificate 679 * principals against the names in that file rather than matching 680 * against the username. 681 */ 682 if ((principals_file = authorized_principals_file(pw)) != NULL) { 683 if (match_principals_file(principals_file, pw, key->cert)) 684 found_principal = 1; 685 } 686 /* Try querying command if specified */ 687 if (!found_principal && match_principals_command(pw, key)) 688 found_principal = 1; 689 /* If principals file or command is specified, then require a match */ 690 use_authorized_principals = principals_file != NULL || 691 options.authorized_principals_command != NULL; 692 if (!found_principal && use_authorized_principals) { 693 reason = "Certificate does not contain an authorized principal"; 694 fail_reason: 695 error("%s", reason); 696 auth_debug_add("%s", reason); 697 goto out; 698 } 699 if (sshkey_cert_check_authority(key, 0, 1, 700 use_authorized_principals ? NULL : pw->pw_name, &reason) != 0) 701 goto fail_reason; 702 if (auth_cert_options(key, pw, &reason) != 0) 703 goto fail_reason; 704 705 verbose("Accepted certificate ID \"%s\" (serial %llu) signed by " 706 "%s CA %s via %s", key->cert->key_id, 707 (unsigned long long)key->cert->serial, 708 sshkey_type(key->cert->signature_key), ca_fp, 709 options.trusted_user_ca_keys); 710 ret = 1; 711 712 out: 713 free(principals_file); 714 free(ca_fp); 715 return ret; 716 } 717 718 /* 719 * Checks whether key is allowed in file. 720 * returns 1 if the key is allowed or 0 otherwise. 721 */ 722 static int 723 user_key_allowed2(struct passwd *pw, struct sshkey *key, char *file) 724 { 725 FILE *f; 726 int found_key = 0; 727 728 /* Temporarily use the user's uid. */ 729 temporarily_use_uid(pw); 730 731 debug("trying public key file %s", file); 732 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) { 733 found_key = check_authkeys_file(f, file, key, pw); 734 fclose(f); 735 } 736 737 restore_uid(); 738 return found_key; 739 } 740 741 /* 742 * Checks whether key is allowed in output of command. 743 * returns 1 if the key is allowed or 0 otherwise. 744 */ 745 static int 746 user_key_command_allowed2(struct passwd *user_pw, struct sshkey *key) 747 { 748 FILE *f = NULL; 749 int r, ok, found_key = 0; 750 struct passwd *pw; 751 int i, uid_swapped = 0, ac = 0; 752 pid_t pid; 753 char *username = NULL, *key_fp = NULL, *keytext = NULL; 754 char *tmp, *command = NULL, **av = NULL; 755 void (*osigchld)(int); 756 757 if (options.authorized_keys_command == NULL) 758 return 0; 759 if (options.authorized_keys_command_user == NULL) { 760 error("No user for AuthorizedKeysCommand specified, skipping"); 761 return 0; 762 } 763 764 /* 765 * NB. all returns later this function should go via "out" to 766 * ensure the original SIGCHLD handler is restored properly. 767 */ 768 osigchld = signal(SIGCHLD, SIG_DFL); 769 770 /* Prepare and verify the user for the command */ 771 username = percent_expand(options.authorized_keys_command_user, 772 "u", user_pw->pw_name, (char *)NULL); 773 pw = getpwnam(username); 774 if (pw == NULL) { 775 error("AuthorizedKeysCommandUser \"%s\" not found: %s", 776 username, strerror(errno)); 777 goto out; 778 } 779 780 /* Prepare AuthorizedKeysCommand */ 781 if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash, 782 SSH_FP_DEFAULT)) == NULL) { 783 error("%s: sshkey_fingerprint failed", __func__); 784 goto out; 785 } 786 if ((r = sshkey_to_base64(key, &keytext)) != 0) { 787 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r)); 788 goto out; 789 } 790 791 /* Turn the command into an argument vector */ 792 if (argv_split(options.authorized_keys_command, &ac, &av) != 0) { 793 error("AuthorizedKeysCommand \"%s\" contains invalid quotes", 794 command); 795 goto out; 796 } 797 if (ac == 0) { 798 error("AuthorizedKeysCommand \"%s\" yielded no arguments", 799 command); 800 goto out; 801 } 802 for (i = 1; i < ac; i++) { 803 tmp = percent_expand(av[i], 804 "u", user_pw->pw_name, 805 "h", user_pw->pw_dir, 806 "t", sshkey_ssh_name(key), 807 "f", key_fp, 808 "k", keytext, 809 (char *)NULL); 810 if (tmp == NULL) 811 fatal("%s: percent_expand failed", __func__); 812 free(av[i]); 813 av[i] = tmp; 814 } 815 /* Prepare a printable command for logs, etc. */ 816 command = argv_assemble(ac, av); 817 818 /* 819 * If AuthorizedKeysCommand was run without arguments 820 * then fall back to the old behaviour of passing the 821 * target username as a single argument. 822 */ 823 if (ac == 1) { 824 av = xreallocarray(av, ac + 2, sizeof(*av)); 825 av[1] = xstrdup(user_pw->pw_name); 826 av[2] = NULL; 827 /* Fix up command too, since it is used in log messages */ 828 free(command); 829 xasprintf(&command, "%s %s", av[0], av[1]); 830 } 831 832 if ((pid = subprocess("AuthorizedKeysCommand", pw, command, 833 ac, av, &f, 834 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0) 835 goto out; 836 837 uid_swapped = 1; 838 temporarily_use_uid(pw); 839 840 ok = check_authkeys_file(f, options.authorized_keys_command, key, pw); 841 842 fclose(f); 843 f = NULL; 844 845 if (exited_cleanly(pid, "AuthorizedKeysCommand", command, 0) != 0) 846 goto out; 847 848 /* Read completed successfully */ 849 found_key = ok; 850 out: 851 if (f != NULL) 852 fclose(f); 853 signal(SIGCHLD, osigchld); 854 for (i = 0; i < ac; i++) 855 free(av[i]); 856 free(av); 857 if (uid_swapped) 858 restore_uid(); 859 free(command); 860 free(username); 861 free(key_fp); 862 free(keytext); 863 return found_key; 864 } 865 866 /* 867 * Check whether key authenticates and authorises the user. 868 */ 869 int 870 user_key_allowed(struct passwd *pw, struct sshkey *key, int auth_attempt) 871 { 872 u_int success, i; 873 char *file; 874 875 if (auth_key_is_revoked(key)) 876 return 0; 877 if (sshkey_is_cert(key) && 878 auth_key_is_revoked(key->cert->signature_key)) 879 return 0; 880 881 success = user_cert_trusted_ca(pw, key); 882 if (success) 883 return success; 884 885 success = user_key_command_allowed2(pw, key); 886 if (success > 0) 887 return success; 888 889 for (i = 0; !success && i < options.num_authkeys_files; i++) { 890 891 if (strcasecmp(options.authorized_keys_files[i], "none") == 0) 892 continue; 893 file = expand_authorized_keys( 894 options.authorized_keys_files[i], pw); 895 896 success = user_key_allowed2(pw, key, file); 897 free(file); 898 } 899 900 return success; 901 } 902 903 Authmethod method_pubkey = { 904 "publickey", 905 userauth_pubkey, 906 &options.pubkey_authentication 907 }; 908