|
Lines 518-526
user_key_command_allowed2(struct passwd *user_pw, Key *key)
Link Here
|
| 518 |
int ok, found_key = 0; |
518 |
int ok, found_key = 0; |
| 519 |
struct passwd *pw; |
519 |
struct passwd *pw; |
| 520 |
struct stat st; |
520 |
struct stat st; |
| 521 |
int status, devnull, p[2], i; |
521 |
int status, devnull, p[2], i, argc; |
| 522 |
pid_t pid; |
522 |
pid_t pid; |
| 523 |
char *username, errmsg[512]; |
523 |
char *username, errmsg[512]; |
|
|
524 |
char *cp, *command = NULL, **argv = NULL; |
| 525 |
char *authorized_keys_command_path; |
| 526 |
char *key_fp = NULL, *keytext = NULL; |
| 527 |
size_t keytextlen; |
| 524 |
|
528 |
|
| 525 |
if (options.authorized_keys_command == NULL || |
529 |
if (options.authorized_keys_command == NULL || |
| 526 |
options.authorized_keys_command[0] != '/') |
530 |
options.authorized_keys_command[0] != '/') |
|
Lines 544-555
user_key_command_allowed2(struct passwd *user_pw, Key *key)
Link Here
|
| 544 |
|
548 |
|
| 545 |
temporarily_use_uid(pw); |
549 |
temporarily_use_uid(pw); |
| 546 |
|
550 |
|
| 547 |
if (stat(options.authorized_keys_command, &st) < 0) { |
551 |
if (strstr(options.authorized_keys_command, "%f") != NULL) { |
|
|
552 |
key_fp = key_fingerprint(key, options.fingerprint_hash, |
| 553 |
SSH_FP_DEFAULT); |
| 554 |
if (key_fp == NULL) { |
| 555 |
error("AuthorizedKeysCommand %%f parameter expansion failed"); |
| 556 |
goto out; |
| 557 |
} |
| 558 |
} |
| 559 |
if (strstr(options.authorized_keys_command, "%k") != NULL) { |
| 560 |
if (key_to_base64(key, &keytext, &keytextlen) != 0) { |
| 561 |
error("AuthorizedKeysCommand %%k parameter expansion failed"); |
| 562 |
goto out; |
| 563 |
} |
| 564 |
} |
| 565 |
command = cp = percent_expand(options.authorized_keys_command, |
| 566 |
"u", user_pw->pw_name, "h", user_pw->pw_dir, "t", key_ssh_name(key), |
| 567 |
"f", key_fp == NULL ? "" : key_fp, |
| 568 |
"k", keytext == NULL ? "" : keytext, (char *)NULL); |
| 569 |
i = 0; |
| 570 |
argc = 10; |
| 571 |
while (cp != NULL) { |
| 572 |
if (argv == NULL || i == argc - 1) { |
| 573 |
argc *= 2; |
| 574 |
argv = xrealloc(argv, argc, sizeof(argv[0])); |
| 575 |
} |
| 576 |
argv[i++] = strdelim(&cp); |
| 577 |
argv[i] = NULL; |
| 578 |
debug3("AuthorizedKeysCommand argv[%d]: %s", |
| 579 |
i - 1, argv[i - 1]); |
| 580 |
} |
| 581 |
if (argv[1] == NULL) { |
| 582 |
argv[1] = user_pw->pw_name; |
| 583 |
argv[2] = NULL; |
| 584 |
debug3("AuthorizedKeysCommand argv[1]: %s", argv[1]); |
| 585 |
} |
| 586 |
authorized_keys_command_path = argv[0]; |
| 587 |
|
| 588 |
if (stat(authorized_keys_command_path, &st) < 0) { |
| 548 |
error("Could not stat AuthorizedKeysCommand \"%s\": %s", |
589 |
error("Could not stat AuthorizedKeysCommand \"%s\": %s", |
| 549 |
options.authorized_keys_command, strerror(errno)); |
590 |
authorized_keys_command_path, strerror(errno)); |
| 550 |
goto out; |
591 |
goto out; |
| 551 |
} |
592 |
} |
| 552 |
if (auth_secure_path(options.authorized_keys_command, &st, NULL, 0, |
593 |
if (auth_secure_path(authorized_keys_command_path, &st, NULL, 0, |
| 553 |
errmsg, sizeof(errmsg)) != 0) { |
594 |
errmsg, sizeof(errmsg)) != 0) { |
| 554 |
error("Unsafe AuthorizedKeysCommand: %s", errmsg); |
595 |
error("Unsafe AuthorizedKeysCommand: %s", errmsg); |
| 555 |
goto out; |
596 |
goto out; |
|
Lines 560-567
user_key_command_allowed2(struct passwd *user_pw, Key *key)
Link Here
|
| 560 |
goto out; |
601 |
goto out; |
| 561 |
} |
602 |
} |
| 562 |
|
603 |
|
| 563 |
debug3("Running AuthorizedKeysCommand: \"%s %s\" as \"%s\"", |
604 |
debug3("Running AuthorizedKeysCommand: \"%s ...\" as \"%s\"", |
| 564 |
options.authorized_keys_command, user_pw->pw_name, pw->pw_name); |
605 |
authorized_keys_command_path, pw->pw_name); |
| 565 |
|
606 |
|
| 566 |
/* |
607 |
/* |
| 567 |
* Don't want to call this in the child, where it can fatal() and |
608 |
* Don't want to call this in the child, where it can fatal() and |
|
Lines 574-579
user_key_command_allowed2(struct passwd *user_pw, Key *key)
Link Here
|
| 574 |
error("%s: fork: %s", __func__, strerror(errno)); |
615 |
error("%s: fork: %s", __func__, strerror(errno)); |
| 575 |
close(p[0]); |
616 |
close(p[0]); |
| 576 |
close(p[1]); |
617 |
close(p[1]); |
|
|
618 |
free(argv); |
| 619 |
free(command); |
| 620 |
if (keytext != NULL) |
| 621 |
free(keytext); |
| 622 |
if (key_fp != NULL) |
| 623 |
free(key_fp); |
| 577 |
return 0; |
624 |
return 0; |
| 578 |
case 0: /* child */ |
625 |
case 0: /* child */ |
| 579 |
for (i = 0; i < NSIG; i++) |
626 |
for (i = 0; i < NSIG; i++) |
|
Lines 609-619
user_key_command_allowed2(struct passwd *user_pw, Key *key)
Link Here
|
| 609 |
_exit(1); |
656 |
_exit(1); |
| 610 |
} |
657 |
} |
| 611 |
|
658 |
|
| 612 |
execl(options.authorized_keys_command, |
659 |
execv(authorized_keys_command_path, argv); |
| 613 |
options.authorized_keys_command, user_pw->pw_name, NULL); |
|
|
| 614 |
|
660 |
|
| 615 |
error("AuthorizedKeysCommand %s exec failed: %s", |
661 |
error("AuthorizedKeysCommand %s exec failed: %s", |
| 616 |
options.authorized_keys_command, strerror(errno)); |
662 |
authorized_keys_command_path, strerror(errno)); |
| 617 |
_exit(127); |
663 |
_exit(127); |
| 618 |
default: /* parent */ |
664 |
default: /* parent */ |
| 619 |
break; |
665 |
break; |
|
Lines 631-637
user_key_command_allowed2(struct passwd *user_pw, Key *key)
Link Here
|
| 631 |
; |
677 |
; |
| 632 |
goto out; |
678 |
goto out; |
| 633 |
} |
679 |
} |
| 634 |
ok = check_authkeys_file(f, options.authorized_keys_command, key, pw); |
680 |
ok = check_authkeys_file(f, authorized_keys_command_path, key, pw); |
| 635 |
fclose(f); |
681 |
fclose(f); |
| 636 |
|
682 |
|
| 637 |
while (waitpid(pid, &status, 0) == -1) { |
683 |
while (waitpid(pid, &status, 0) == -1) { |
|
Lines 642-656
user_key_command_allowed2(struct passwd *user_pw, Key *key)
Link Here
|
| 642 |
} |
688 |
} |
| 643 |
if (WIFSIGNALED(status)) { |
689 |
if (WIFSIGNALED(status)) { |
| 644 |
error("AuthorizedKeysCommand %s exited on signal %d", |
690 |
error("AuthorizedKeysCommand %s exited on signal %d", |
| 645 |
options.authorized_keys_command, WTERMSIG(status)); |
691 |
authorized_keys_command_path, WTERMSIG(status)); |
| 646 |
goto out; |
692 |
goto out; |
| 647 |
} else if (WEXITSTATUS(status) != 0) { |
693 |
} else if (WEXITSTATUS(status) != 0) { |
| 648 |
error("AuthorizedKeysCommand %s returned status %d", |
694 |
error("AuthorizedKeysCommand %s returned status %d", |
| 649 |
options.authorized_keys_command, WEXITSTATUS(status)); |
695 |
authorized_keys_command_path, WEXITSTATUS(status)); |
| 650 |
goto out; |
696 |
goto out; |
| 651 |
} |
697 |
} |
| 652 |
found_key = ok; |
698 |
found_key = ok; |
| 653 |
out: |
699 |
out: |
|
|
700 |
if (argv != NULL) |
| 701 |
free(argv); |
| 702 |
if (command != NULL) |
| 703 |
free(command); |
| 704 |
if (keytext != NULL) |
| 705 |
free(keytext); |
| 706 |
if (key_fp != NULL) |
| 707 |
free(key_fp); |
| 654 |
restore_uid(); |
708 |
restore_uid(); |
| 655 |
return found_key; |
709 |
return found_key; |
| 656 |
} |
710 |
} |