|
Lines 62-67
Link Here
|
| 62 |
#include "monitor_wrap.h" |
62 |
#include "monitor_wrap.h" |
| 63 |
#include "authfile.h" |
63 |
#include "authfile.h" |
| 64 |
#include "match.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? */ |
| 65 |
|
68 |
|
| 66 |
/* import */ |
69 |
/* import */ |
| 67 |
extern ServerOptions options; |
70 |
extern ServerOptions options; |
|
Lines 517-522
user_key_allowed2(struct passwd *pw, Key *key, char *file)
Link Here
|
| 517 |
} |
520 |
} |
| 518 |
|
521 |
|
| 519 |
/* |
522 |
/* |
|
|
523 |
* Splits 's' into an argument vector. Handles quoted string, basic escape |
| 524 |
* characters (\\, \", \'), but not nested quotes. Caller must free the |
| 525 |
* argument vector and its members. |
| 526 |
*/ |
| 527 |
static int |
| 528 |
split_argv(const char *s, int *argcp, char ***argvp) |
| 529 |
{ |
| 530 |
int r = SSH_ERR_INTERNAL_ERROR; |
| 531 |
int argc = 0, quote, i, j; |
| 532 |
char *arg, **argv = xcalloc(1, sizeof(*argv)); |
| 533 |
|
| 534 |
*argvp = NULL; |
| 535 |
*argcp = 0; |
| 536 |
|
| 537 |
for (i = 0; s[i] != '\0'; i++) { |
| 538 |
/* Skip leading whitespace */ |
| 539 |
if (s[i] == ' ' || s[i] == '\t') |
| 540 |
continue; |
| 541 |
|
| 542 |
/* Start of a token */ |
| 543 |
quote = 0; |
| 544 |
if (s[i] == '\\' && |
| 545 |
(s[i + 1] == '\'' || s[i + 1] == '\"' || s[i + 1] == '\\')) |
| 546 |
i++; |
| 547 |
else if (s[i] == '\'' || s[i] == '"') |
| 548 |
quote = s[i++]; |
| 549 |
|
| 550 |
argv = xrealloc(argv, (argc + 2), sizeof(*argv)); |
| 551 |
arg = argv[argc++] = xcalloc(1, strlen(s + i) + 1); |
| 552 |
argv[argc] = NULL; |
| 553 |
|
| 554 |
/* Copy the token in, removing escapes */ |
| 555 |
for (j = 0; s[i] != '\0'; i++) { |
| 556 |
if (s[i] == '\\') { |
| 557 |
if (s[i + 1] == '\'' || |
| 558 |
s[i + 1] == '\"' || |
| 559 |
s[i + 1] == '\\') { |
| 560 |
i++; /* Skip '\' */ |
| 561 |
arg[j++] = s[i]; |
| 562 |
} else { |
| 563 |
/* Unrecognised escape */ |
| 564 |
arg[j++] = s[i]; |
| 565 |
} |
| 566 |
} else if (quote == 0 && (s[i] == ' ' || s[i] == '\t')) |
| 567 |
break; /* done */ |
| 568 |
else if (quote != 0 && s[i] == quote) |
| 569 |
break; /* done */ |
| 570 |
else |
| 571 |
arg[j++] = s[i]; |
| 572 |
} |
| 573 |
if (s[i] == '\0') { |
| 574 |
if (quote != 0) { |
| 575 |
/* Ran out of string looking for close quote */ |
| 576 |
r = SSH_ERR_INVALID_FORMAT; |
| 577 |
goto out; |
| 578 |
} |
| 579 |
break; |
| 580 |
} |
| 581 |
} |
| 582 |
/* Success */ |
| 583 |
*argcp = argc; |
| 584 |
*argvp = argv; |
| 585 |
argc = 0; |
| 586 |
argv = NULL; |
| 587 |
r = 0; |
| 588 |
out: |
| 589 |
if (argc != 0 && argv != NULL) { |
| 590 |
for (i = 0; i < argc; i++) |
| 591 |
free(argv[i]); |
| 592 |
free(argv); |
| 593 |
} |
| 594 |
return r; |
| 595 |
} |
| 596 |
/* |
| 520 |
* Checks whether key is allowed in output of command. |
597 |
* Checks whether key is allowed in output of command. |
| 521 |
* returns 1 if the key is allowed or 0 otherwise. |
598 |
* returns 1 if the key is allowed or 0 otherwise. |
| 522 |
*/ |
599 |
*/ |
|
Lines 524-576
static int
Link Here
|
| 524 |
user_key_command_allowed2(struct passwd *user_pw, Key *key) |
601 |
user_key_command_allowed2(struct passwd *user_pw, Key *key) |
| 525 |
{ |
602 |
{ |
| 526 |
FILE *f; |
603 |
FILE *f; |
| 527 |
int ok, found_key = 0; |
604 |
int r, ok, found_key = 0; |
| 528 |
struct passwd *pw; |
605 |
struct passwd *pw; |
| 529 |
struct stat st; |
606 |
struct stat st; |
| 530 |
int status, devnull, p[2], i; |
607 |
int status, devnull, p[3], i, ac = 0; |
| 531 |
pid_t pid; |
608 |
pid_t pid; |
| 532 |
char *username, errmsg[512]; |
609 |
char *username = NULL, *key_fp = NULL, *keytext = NULL; |
|
|
610 |
char *command = NULL, **av = NULL, *cp, errmsg[512]; |
| 611 |
u_int envsize; |
| 612 |
char **child_env; |
| 613 |
void (*osigchld)(int); |
| 533 |
|
614 |
|
| 534 |
if (options.authorized_keys_command == NULL || |
615 |
if (options.authorized_keys_command == NULL) |
| 535 |
options.authorized_keys_command[0] != '/') |
|
|
| 536 |
return 0; |
616 |
return 0; |
| 537 |
|
|
|
| 538 |
if (options.authorized_keys_command_user == NULL) { |
617 |
if (options.authorized_keys_command_user == NULL) { |
| 539 |
error("No user for AuthorizedKeysCommand specified, skipping"); |
618 |
error("No user for AuthorizedKeysCommand specified, skipping"); |
| 540 |
return 0; |
619 |
return 0; |
| 541 |
} |
620 |
} |
| 542 |
|
621 |
|
|
|
622 |
/* |
| 623 |
* NB. all returns later this function should go via "out" to |
| 624 |
* ensure the original SIGCHLD handler is restored properly. |
| 625 |
*/ |
| 626 |
osigchld = signal(SIGCHLD, SIG_DFL); |
| 627 |
|
| 628 |
/* Prepare and verify the user for the command */ |
| 543 |
username = percent_expand(options.authorized_keys_command_user, |
629 |
username = percent_expand(options.authorized_keys_command_user, |
| 544 |
"u", user_pw->pw_name, (char *)NULL); |
630 |
"u", user_pw->pw_name, (char *)NULL); |
| 545 |
pw = getpwnam(username); |
631 |
pw = getpwnam(username); |
| 546 |
if (pw == NULL) { |
632 |
if (pw == NULL) { |
| 547 |
error("AuthorizedKeysCommandUser \"%s\" not found: %s", |
633 |
error("AuthorizedKeysCommandUser \"%s\" not found: %s", |
| 548 |
username, strerror(errno)); |
634 |
username, strerror(errno)); |
| 549 |
free(username); |
635 |
goto out; |
| 550 |
return 0; |
|
|
| 551 |
} |
636 |
} |
| 552 |
free(username); |
|
|
| 553 |
|
637 |
|
|
|
638 |
/* Prepare AuthorizedKeysCommand */ |
| 639 |
if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash, |
| 640 |
SSH_FP_DEFAULT)) == NULL) { |
| 641 |
error("%s: sshkey_fingerprint failed", __func__); |
| 642 |
goto out; |
| 643 |
} |
| 644 |
if ((r = sshkey_to_base64(key, &keytext)) != 0) { |
| 645 |
error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r)); |
| 646 |
goto out; |
| 647 |
} |
| 648 |
command = percent_expand(options.authorized_keys_command, |
| 649 |
"u", user_pw->pw_name, "h", user_pw->pw_dir, |
| 650 |
"t", sshkey_ssh_name(key), "f", key_fp, "k", keytext, (char *)NULL); |
| 651 |
|
| 652 |
/* Turn the command into an argument vector */ |
| 653 |
if (split_argv(command, &ac, &av) != 0) { |
| 654 |
error("AuthorizedKeysCommand \"%s\" contains invalid quotes", |
| 655 |
command); |
| 656 |
goto out; |
| 657 |
} |
| 658 |
if (ac == 0) { |
| 659 |
error("AuthorizedKeysCommand \"%s\" yielded no arguments", |
| 660 |
command); |
| 661 |
goto out; |
| 662 |
} |
| 663 |
|
| 664 |
/* |
| 665 |
* If AuthorizedKeysCommand was run without arguments |
| 666 |
* then fall back to the old behaviour of passing the |
| 667 |
* target username as a single argument. |
| 668 |
*/ |
| 669 |
if (ac == 1) { |
| 670 |
av = xrealloc(av, ac + 2, sizeof(*av)); |
| 671 |
av[1] = xstrdup(user_pw->pw_name); |
| 672 |
av[2] = NULL; |
| 673 |
/* Fix up command too, since it is used in log messages */ |
| 674 |
free(command); |
| 675 |
xasprintf(&command, "%s %s", |
| 676 |
options.authorized_keys_command, user_pw->pw_name); |
| 677 |
} |
| 678 |
|
| 679 |
/* Verify the path exists and is safe-ish to execute */ |
| 680 |
if (*av[0] != '/') { |
| 681 |
error("AuthorizedKeysCommand path is not absolute"); |
| 682 |
goto out; |
| 683 |
} |
| 554 |
temporarily_use_uid(pw); |
684 |
temporarily_use_uid(pw); |
| 555 |
|
685 |
if (stat(av[0], &st) < 0) { |
| 556 |
if (stat(options.authorized_keys_command, &st) < 0) { |
|
|
| 557 |
error("Could not stat AuthorizedKeysCommand \"%s\": %s", |
686 |
error("Could not stat AuthorizedKeysCommand \"%s\": %s", |
| 558 |
options.authorized_keys_command, strerror(errno)); |
687 |
av[0], strerror(errno)); |
| 559 |
goto out; |
688 |
goto out; |
| 560 |
} |
689 |
} |
| 561 |
if (auth_secure_path(options.authorized_keys_command, &st, NULL, 0, |
690 |
if (auth_secure_path(av[0], &st, NULL, 0, |
| 562 |
errmsg, sizeof(errmsg)) != 0) { |
691 |
errmsg, sizeof(errmsg)) != 0) { |
| 563 |
error("Unsafe AuthorizedKeysCommand: %s", errmsg); |
692 |
error("Unsafe AuthorizedKeysCommand \"%s\": %s", av[0], errmsg); |
| 564 |
goto out; |
693 |
goto out; |
| 565 |
} |
694 |
} |
| 566 |
|
695 |
|
|
|
696 |
/* |
| 697 |
* Run the command; stderr is left in place, stdout is the |
| 698 |
* authorized_keys output. |
| 699 |
*/ |
| 567 |
if (pipe(p) != 0) { |
700 |
if (pipe(p) != 0) { |
| 568 |
error("%s: pipe: %s", __func__, strerror(errno)); |
701 |
error("%s: pipe: %s", __func__, strerror(errno)); |
| 569 |
goto out; |
702 |
goto out; |
| 570 |
} |
703 |
} |
| 571 |
|
704 |
|
| 572 |
debug3("Running AuthorizedKeysCommand: \"%s %s\" as \"%s\"", |
705 |
debug3("Running AuthorizedKeysCommand: \"%s\" as \"%s\"", |
| 573 |
options.authorized_keys_command, user_pw->pw_name, pw->pw_name); |
706 |
command, pw->pw_name); |
| 574 |
|
707 |
|
| 575 |
/* |
708 |
/* |
| 576 |
* Don't want to call this in the child, where it can fatal() and |
709 |
* Don't want to call this in the child, where it can fatal() and |
|
Lines 585-590
user_key_command_allowed2(struct passwd *user_pw, Key *key)
Link Here
|
| 585 |
close(p[1]); |
718 |
close(p[1]); |
| 586 |
return 0; |
719 |
return 0; |
| 587 |
case 0: /* child */ |
720 |
case 0: /* child */ |
|
|
721 |
/* Prepare a minimal environment for the child. */ |
| 722 |
envsize = 5; |
| 723 |
child_env = xcalloc(sizeof(*child_env), envsize); |
| 724 |
child_set_env(&child_env, &envsize, "PATH", _PATH_STDPATH); |
| 725 |
child_set_env(&child_env, &envsize, "USER", pw->pw_name); |
| 726 |
child_set_env(&child_env, &envsize, "LOGNAME", pw->pw_name); |
| 727 |
child_set_env(&child_env, &envsize, "HOME", pw->pw_dir); |
| 728 |
if ((cp = getenv("LANG")) != NULL) |
| 729 |
child_set_env(&child_env, &envsize, "LANG", cp); |
| 730 |
|
| 588 |
for (i = 0; i < NSIG; i++) |
731 |
for (i = 0; i < NSIG; i++) |
| 589 |
signal(i, SIG_DFL); |
732 |
signal(i, SIG_DFL); |
| 590 |
|
733 |
|
|
Lines 618-628
user_key_command_allowed2(struct passwd *user_pw, Key *key)
Link Here
|
| 618 |
_exit(1); |
761 |
_exit(1); |
| 619 |
} |
762 |
} |
| 620 |
|
763 |
|
| 621 |
execl(options.authorized_keys_command, |
764 |
execve(av[0], av, child_env); |
| 622 |
options.authorized_keys_command, user_pw->pw_name, NULL); |
765 |
error("AuthorizedKeysCommand exec \"%s\" failed: %s", |
| 623 |
|
766 |
command, strerror(errno)); |
| 624 |
error("AuthorizedKeysCommand %s exec failed: %s", |
|
|
| 625 |
options.authorized_keys_command, strerror(errno)); |
| 626 |
_exit(127); |
767 |
_exit(127); |
| 627 |
default: /* parent */ |
768 |
default: /* parent */ |
| 628 |
break; |
769 |
break; |
|
Lines 658-666
user_key_command_allowed2(struct passwd *user_pw, Key *key)
Link Here
|
| 658 |
options.authorized_keys_command, WEXITSTATUS(status)); |
799 |
options.authorized_keys_command, WEXITSTATUS(status)); |
| 659 |
goto out; |
800 |
goto out; |
| 660 |
} |
801 |
} |
|
|
802 |
/* Read completed successfully */ |
| 661 |
found_key = ok; |
803 |
found_key = ok; |
| 662 |
out: |
804 |
out: |
|
|
805 |
signal(SIGCHLD, osigchld); |
| 806 |
for (i = 0; i < ac; i++) |
| 807 |
free(av[i]); |
| 808 |
free(av); |
| 663 |
restore_uid(); |
809 |
restore_uid(); |
|
|
810 |
free(command); |
| 811 |
free(username); |
| 812 |
free(key_fp); |
| 813 |
free(keytext); |
| 664 |
return found_key; |
814 |
return found_key; |
| 665 |
} |
815 |
} |
| 666 |
|
816 |
|