|
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 245-250
pubkey_auth_info(Authctxt *authctxt, const Key *key, const char *fmt, ...)
Link Here
|
| 245 |
free(extra); |
248 |
free(extra); |
| 246 |
} |
249 |
} |
| 247 |
|
250 |
|
|
|
251 |
/* |
| 252 |
* Splits 's' into an argument vector. Handles quoted string and basic |
| 253 |
* escape characters (\\, \", \'). Caller must free the argument vector |
| 254 |
* and its members. |
| 255 |
*/ |
| 256 |
static int |
| 257 |
split_argv(const char *s, int *argcp, char ***argvp) |
| 258 |
{ |
| 259 |
int r = SSH_ERR_INTERNAL_ERROR; |
| 260 |
int argc = 0, quote, i, j; |
| 261 |
char *arg, **argv = xcalloc(1, sizeof(*argv)); |
| 262 |
|
| 263 |
*argvp = NULL; |
| 264 |
*argcp = 0; |
| 265 |
|
| 266 |
for (i = 0; s[i] != '\0'; i++) { |
| 267 |
/* Skip leading whitespace */ |
| 268 |
if (s[i] == ' ' || s[i] == '\t') |
| 269 |
continue; |
| 270 |
|
| 271 |
/* Start of a token */ |
| 272 |
quote = 0; |
| 273 |
if (s[i] == '\\' && |
| 274 |
(s[i + 1] == '\'' || s[i + 1] == '\"' || s[i + 1] == '\\')) |
| 275 |
i++; |
| 276 |
else if (s[i] == '\'' || s[i] == '"') |
| 277 |
quote = s[i++]; |
| 278 |
|
| 279 |
argv = xrealloc(argv, (argc + 2), sizeof(*argv)); |
| 280 |
arg = argv[argc++] = xcalloc(1, strlen(s + i) + 1); |
| 281 |
argv[argc] = NULL; |
| 282 |
|
| 283 |
/* Copy the token in, removing escapes */ |
| 284 |
for (j = 0; s[i] != '\0'; i++) { |
| 285 |
if (s[i] == '\\') { |
| 286 |
if (s[i + 1] == '\'' || |
| 287 |
s[i + 1] == '\"' || |
| 288 |
s[i + 1] == '\\') { |
| 289 |
i++; /* Skip '\' */ |
| 290 |
arg[j++] = s[i]; |
| 291 |
} else { |
| 292 |
/* Unrecognised escape */ |
| 293 |
arg[j++] = s[i]; |
| 294 |
} |
| 295 |
} else if (quote == 0 && (s[i] == ' ' || s[i] == '\t')) |
| 296 |
break; /* done */ |
| 297 |
else if (quote != 0 && s[i] == quote) |
| 298 |
break; /* done */ |
| 299 |
else |
| 300 |
arg[j++] = s[i]; |
| 301 |
} |
| 302 |
if (s[i] == '\0') { |
| 303 |
if (quote != 0) { |
| 304 |
/* Ran out of string looking for close quote */ |
| 305 |
r = SSH_ERR_INVALID_FORMAT; |
| 306 |
goto out; |
| 307 |
} |
| 308 |
break; |
| 309 |
} |
| 310 |
} |
| 311 |
/* Success */ |
| 312 |
*argcp = argc; |
| 313 |
*argvp = argv; |
| 314 |
argc = 0; |
| 315 |
argv = NULL; |
| 316 |
r = 0; |
| 317 |
out: |
| 318 |
if (argc != 0 && argv != NULL) { |
| 319 |
for (i = 0; i < argc; i++) |
| 320 |
free(argv[i]); |
| 321 |
free(argv); |
| 322 |
} |
| 323 |
return r; |
| 324 |
} |
| 325 |
|
| 326 |
/* |
| 327 |
* Runs command in a subprocess. Returns pid on success and a FILE* to the |
| 328 |
* subprocess' stdout or 0 on failure. |
| 329 |
* NB. "command" is only used for logging. |
| 330 |
*/ |
| 331 |
static pid_t |
| 332 |
subprocess(const char *tag, struct passwd *pw, const char *command, |
| 333 |
int ac, char **av, FILE **child) |
| 334 |
{ |
| 335 |
FILE *f; |
| 336 |
struct stat st; |
| 337 |
int devnull, p[2], i; |
| 338 |
pid_t pid; |
| 339 |
char *cp, errmsg[512]; |
| 340 |
u_int envsize; |
| 341 |
char **child_env; |
| 342 |
|
| 343 |
*child = NULL; |
| 344 |
|
| 345 |
debug3("%s: %s command \"%s\" running as %s", __func__, |
| 346 |
tag, command, pw->pw_name); |
| 347 |
|
| 348 |
/* Verify the path exists and is safe-ish to execute */ |
| 349 |
if (*av[0] != '/') { |
| 350 |
error("%s path is not absolute", tag); |
| 351 |
return 0; |
| 352 |
} |
| 353 |
temporarily_use_uid(pw); |
| 354 |
if (stat(av[0], &st) < 0) { |
| 355 |
error("Could not stat %s \"%s\": %s", tag, |
| 356 |
av[0], strerror(errno)); |
| 357 |
restore_uid(); |
| 358 |
return 0; |
| 359 |
} |
| 360 |
if (auth_secure_path(av[0], &st, NULL, 0, |
| 361 |
errmsg, sizeof(errmsg)) != 0) { |
| 362 |
error("Unsafe %s \"%s\": %s", tag, av[0], errmsg); |
| 363 |
restore_uid(); |
| 364 |
return 0; |
| 365 |
} |
| 366 |
|
| 367 |
/* |
| 368 |
* Run the command; stderr is left in place, stdout is the |
| 369 |
* authorized_keys output. |
| 370 |
*/ |
| 371 |
if (pipe(p) != 0) { |
| 372 |
error("%s: pipe: %s", tag, strerror(errno)); |
| 373 |
restore_uid(); |
| 374 |
return 0; |
| 375 |
} |
| 376 |
|
| 377 |
/* |
| 378 |
* Don't want to call this in the child, where it can fatal() and |
| 379 |
* run cleanup_exit() code. |
| 380 |
*/ |
| 381 |
restore_uid(); |
| 382 |
|
| 383 |
switch ((pid = fork())) { |
| 384 |
case -1: /* error */ |
| 385 |
error("%s: fork: %s", tag, strerror(errno)); |
| 386 |
close(p[0]); |
| 387 |
close(p[1]); |
| 388 |
return 0; |
| 389 |
case 0: /* child */ |
| 390 |
/* Prepare a minimal environment for the child. */ |
| 391 |
envsize = 5; |
| 392 |
child_env = xcalloc(sizeof(*child_env), envsize); |
| 393 |
child_set_env(&child_env, &envsize, "PATH", _PATH_STDPATH); |
| 394 |
child_set_env(&child_env, &envsize, "USER", pw->pw_name); |
| 395 |
child_set_env(&child_env, &envsize, "LOGNAME", pw->pw_name); |
| 396 |
child_set_env(&child_env, &envsize, "HOME", pw->pw_dir); |
| 397 |
if ((cp = getenv("LANG")) != NULL) |
| 398 |
child_set_env(&child_env, &envsize, "LANG", cp); |
| 399 |
|
| 400 |
for (i = 0; i < NSIG; i++) |
| 401 |
signal(i, SIG_DFL); |
| 402 |
|
| 403 |
if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) { |
| 404 |
error("%s: open %s: %s", tag, _PATH_DEVNULL, |
| 405 |
strerror(errno)); |
| 406 |
_exit(1); |
| 407 |
} |
| 408 |
/* Keep stderr around a while longer to catch errors */ |
| 409 |
if (dup2(devnull, STDIN_FILENO) == -1 || |
| 410 |
dup2(p[1], STDOUT_FILENO) == -1) { |
| 411 |
error("%s: dup2: %s", tag, strerror(errno)); |
| 412 |
_exit(1); |
| 413 |
} |
| 414 |
closefrom(STDERR_FILENO + 1); |
| 415 |
|
| 416 |
/* Don't use permanently_set_uid() here to avoid fatal() */ |
| 417 |
if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) { |
| 418 |
error("%s: setresgid %u: %s", tag, (u_int)pw->pw_gid, |
| 419 |
strerror(errno)); |
| 420 |
_exit(1); |
| 421 |
} |
| 422 |
if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) { |
| 423 |
error("%s: setresuid %u: %s", tag, (u_int)pw->pw_uid, |
| 424 |
strerror(errno)); |
| 425 |
_exit(1); |
| 426 |
} |
| 427 |
/* stdin is pointed to /dev/null at this point */ |
| 428 |
if (dup2(STDIN_FILENO, STDERR_FILENO) == -1) { |
| 429 |
error("%s: dup2: %s", tag, strerror(errno)); |
| 430 |
_exit(1); |
| 431 |
} |
| 432 |
|
| 433 |
execve(av[0], av, child_env); |
| 434 |
error("%s exec \"%s\": %s", tag, command, strerror(errno)); |
| 435 |
_exit(127); |
| 436 |
default: /* parent */ |
| 437 |
break; |
| 438 |
} |
| 439 |
|
| 440 |
close(p[1]); |
| 441 |
if ((f = fdopen(p[0], "r")) == NULL) { |
| 442 |
error("%s: fdopen: %s", tag, strerror(errno)); |
| 443 |
close(p[0]); |
| 444 |
/* Don't leave zombie child */ |
| 445 |
kill(pid, SIGTERM); |
| 446 |
while (waitpid(pid, NULL, 0) == -1 && errno == EINTR) |
| 447 |
; |
| 448 |
return 0; |
| 449 |
} |
| 450 |
/* Success */ |
| 451 |
debug3("%s: %s pid %ld", __func__, tag, (long)pid); |
| 452 |
*child = f; |
| 453 |
return pid; |
| 454 |
} |
| 455 |
|
| 456 |
/* Returns 0 if pid exited cleanly, non-zero otherwise */ |
| 457 |
static int |
| 458 |
exited_cleanly(pid_t pid, const char *tag, const char *cmd) |
| 459 |
{ |
| 460 |
int status; |
| 461 |
|
| 462 |
while (waitpid(pid, &status, 0) == -1) { |
| 463 |
if (errno != EINTR) { |
| 464 |
error("%s: waitpid: %s", tag, strerror(errno)); |
| 465 |
return -1; |
| 466 |
} |
| 467 |
} |
| 468 |
if (WIFSIGNALED(status)) { |
| 469 |
error("%s %s exited on signal %d", tag, cmd, WTERMSIG(status)); |
| 470 |
return -1; |
| 471 |
} else if (WEXITSTATUS(status) != 0) { |
| 472 |
error("%s %s failed, status %d", tag, cmd, WEXITSTATUS(status)); |
| 473 |
return -1; |
| 474 |
} |
| 475 |
return 0; |
| 476 |
} |
| 477 |
|
| 248 |
static int |
478 |
static int |
| 249 |
match_principals_option(const char *principal_list, struct sshkey_cert *cert) |
479 |
match_principals_option(const char *principal_list, struct sshkey_cert *cert) |
| 250 |
{ |
480 |
{ |
|
Lines 266-284
match_principals_option(const char *principal_list, struct sshkey_cert *cert)
Link Here
|
| 266 |
} |
496 |
} |
| 267 |
|
497 |
|
| 268 |
static int |
498 |
static int |
| 269 |
match_principals_file(char *file, struct passwd *pw, struct sshkey_cert *cert) |
499 |
process_principals(FILE *f, char *file, struct passwd *pw, |
|
|
500 |
struct sshkey_cert *cert) |
| 270 |
{ |
501 |
{ |
| 271 |
FILE *f; |
|
|
| 272 |
char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts; |
502 |
char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts; |
| 273 |
u_long linenum = 0; |
503 |
u_long linenum = 0; |
| 274 |
u_int i; |
504 |
u_int i; |
| 275 |
|
505 |
|
| 276 |
temporarily_use_uid(pw); |
|
|
| 277 |
debug("trying authorized principals file %s", file); |
| 278 |
if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) { |
| 279 |
restore_uid(); |
| 280 |
return 0; |
| 281 |
} |
| 282 |
while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { |
506 |
while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { |
| 283 |
/* Skip leading whitespace. */ |
507 |
/* Skip leading whitespace. */ |
| 284 |
for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
508 |
for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
|
Lines 306-329
match_principals_file(char *file, struct passwd *pw, struct sshkey_cert *cert)
Link Here
|
| 306 |
} |
530 |
} |
| 307 |
for (i = 0; i < cert->nprincipals; i++) { |
531 |
for (i = 0; i < cert->nprincipals; i++) { |
| 308 |
if (strcmp(cp, cert->principals[i]) == 0) { |
532 |
if (strcmp(cp, cert->principals[i]) == 0) { |
| 309 |
debug3("matched principal \"%.100s\" " |
533 |
debug3("%s:%lu: matched principal \"%.100s\"", |
| 310 |
"from file \"%s\" on line %lu", |
534 |
file == NULL ? "(command)" : file, |
| 311 |
cert->principals[i], file, linenum); |
535 |
linenum, cert->principals[i]); |
| 312 |
if (auth_parse_options(pw, line_opts, |
536 |
if (auth_parse_options(pw, line_opts, |
| 313 |
file, linenum) != 1) |
537 |
file, linenum) != 1) |
| 314 |
continue; |
538 |
continue; |
| 315 |
fclose(f); |
|
|
| 316 |
restore_uid(); |
| 317 |
return 1; |
539 |
return 1; |
| 318 |
} |
540 |
} |
| 319 |
} |
541 |
} |
| 320 |
} |
542 |
} |
|
|
543 |
return 0; |
| 544 |
} |
| 545 |
|
| 546 |
static int |
| 547 |
match_principals_file(char *file, struct passwd *pw, struct sshkey_cert *cert) |
| 548 |
{ |
| 549 |
FILE *f; |
| 550 |
int success; |
| 551 |
|
| 552 |
temporarily_use_uid(pw); |
| 553 |
debug("trying authorized principals file %s", file); |
| 554 |
if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) { |
| 555 |
restore_uid(); |
| 556 |
return 0; |
| 557 |
} |
| 558 |
success = process_principals(f, file, pw, cert); |
| 321 |
fclose(f); |
559 |
fclose(f); |
| 322 |
restore_uid(); |
560 |
restore_uid(); |
| 323 |
return 0; |
561 |
return success; |
| 324 |
} |
562 |
} |
| 325 |
|
563 |
|
| 326 |
/* |
564 |
/* |
|
|
565 |
* Checks whether principal is allowed in output of command. |
| 566 |
* returns 1 if the principal is allowed or 0 otherwise. |
| 567 |
*/ |
| 568 |
static int |
| 569 |
match_principals_command(struct passwd *user_pw, struct sshkey *key) |
| 570 |
{ |
| 571 |
FILE *f = NULL; |
| 572 |
int ok, found_principal = 0; |
| 573 |
struct passwd *pw; |
| 574 |
int i, ac = 0, uid_swapped = 0; |
| 575 |
pid_t pid; |
| 576 |
char *username = NULL, *command = NULL, **av = NULL; |
| 577 |
void (*osigchld)(int); |
| 578 |
|
| 579 |
if (options.authorized_principals_command == NULL) |
| 580 |
return 0; |
| 581 |
if (options.authorized_principals_command_user == NULL) { |
| 582 |
error("No user for AuthorizedPrincipalsCommand specified, " |
| 583 |
"skipping"); |
| 584 |
return 0; |
| 585 |
} |
| 586 |
|
| 587 |
/* |
| 588 |
* NB. all returns later this function should go via "out" to |
| 589 |
* ensure the original SIGCHLD handler is restored properly. |
| 590 |
*/ |
| 591 |
osigchld = signal(SIGCHLD, SIG_DFL); |
| 592 |
|
| 593 |
/* Prepare and verify the user for the command */ |
| 594 |
username = percent_expand(options.authorized_principals_command_user, |
| 595 |
"u", user_pw->pw_name, (char *)NULL); |
| 596 |
pw = getpwnam(username); |
| 597 |
if (pw == NULL) { |
| 598 |
error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s", |
| 599 |
username, strerror(errno)); |
| 600 |
goto out; |
| 601 |
} |
| 602 |
|
| 603 |
command = percent_expand(options.authorized_principals_command, |
| 604 |
"u", user_pw->pw_name, "h", user_pw->pw_dir, (char *)NULL); |
| 605 |
|
| 606 |
/* Turn the command into an argument vector */ |
| 607 |
if (split_argv(command, &ac, &av) != 0) { |
| 608 |
error("AuthorizedPrincipalsCommand \"%s\" contains " |
| 609 |
"invalid quotes", command); |
| 610 |
goto out; |
| 611 |
} |
| 612 |
if (ac == 0) { |
| 613 |
error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments", |
| 614 |
command); |
| 615 |
goto out; |
| 616 |
} |
| 617 |
|
| 618 |
if ((pid = subprocess("AuthorizedPrincipalsCommand", pw, command, |
| 619 |
ac, av, &f)) == 0) |
| 620 |
goto out; |
| 621 |
|
| 622 |
uid_swapped = 1; |
| 623 |
temporarily_use_uid(pw); |
| 624 |
|
| 625 |
ok = process_principals(f, NULL, pw, key->cert); |
| 626 |
|
| 627 |
if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command)) |
| 628 |
goto out; |
| 629 |
|
| 630 |
/* Read completed successfully */ |
| 631 |
found_principal = ok; |
| 632 |
out: |
| 633 |
if (f != NULL) |
| 634 |
fclose(f); |
| 635 |
signal(SIGCHLD, osigchld); |
| 636 |
for (i = 0; i < ac; i++) |
| 637 |
free(av[i]); |
| 638 |
free(av); |
| 639 |
if (uid_swapped) |
| 640 |
restore_uid(); |
| 641 |
free(command); |
| 642 |
free(username); |
| 643 |
return found_principal; |
| 644 |
} |
| 645 |
/* |
| 327 |
* Checks whether key is allowed in authorized_keys-format file, |
646 |
* Checks whether key is allowed in authorized_keys-format file, |
| 328 |
* returns 1 if the key is allowed or 0 otherwise. |
647 |
* returns 1 if the key is allowed or 0 otherwise. |
| 329 |
*/ |
648 |
*/ |
|
Lines 445-451
user_cert_trusted_ca(struct passwd *pw, Key *key)
Link Here
|
| 445 |
{ |
764 |
{ |
| 446 |
char *ca_fp, *principals_file = NULL; |
765 |
char *ca_fp, *principals_file = NULL; |
| 447 |
const char *reason; |
766 |
const char *reason; |
| 448 |
int ret = 0; |
767 |
int ret = 0, found_principal = 0; |
| 449 |
|
768 |
|
| 450 |
if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL) |
769 |
if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL) |
| 451 |
return 0; |
770 |
return 0; |
|
Lines 467-480
user_cert_trusted_ca(struct passwd *pw, Key *key)
Link Here
|
| 467 |
* against the username. |
786 |
* against the username. |
| 468 |
*/ |
787 |
*/ |
| 469 |
if ((principals_file = authorized_principals_file(pw)) != NULL) { |
788 |
if ((principals_file = authorized_principals_file(pw)) != NULL) { |
| 470 |
if (!match_principals_file(principals_file, pw, key->cert)) { |
789 |
if (match_principals_file(principals_file, pw, key->cert)) |
| 471 |
reason = "Certificate does not contain an " |
790 |
found_principal = 1; |
| 472 |
"authorized principal"; |
791 |
} |
|
|
792 |
/* Try querying command if specified */ |
| 793 |
if (!found_principal && match_principals_command(pw, key)) |
| 794 |
found_principal = 1; |
| 795 |
/* If principals file or command specify, then require a match here */ |
| 796 |
if (!found_principal && (principals_file != NULL || |
| 797 |
options.authorized_principals_command != NULL)) { |
| 798 |
reason = "Certificate does not contain an authorized principal"; |
| 473 |
fail_reason: |
799 |
fail_reason: |
| 474 |
error("%s", reason); |
800 |
error("%s", reason); |
| 475 |
auth_debug_add("%s", reason); |
801 |
auth_debug_add("%s", reason); |
| 476 |
goto out; |
802 |
goto out; |
| 477 |
} |
|
|
| 478 |
} |
803 |
} |
| 479 |
if (key_cert_check_authority(key, 0, 1, |
804 |
if (key_cert_check_authority(key, 0, 1, |
| 480 |
principals_file == NULL ? pw->pw_name : NULL, &reason) != 0) |
805 |
principals_file == NULL ? pw->pw_name : NULL, &reason) != 0) |
|
Lines 523-666
user_key_allowed2(struct passwd *pw, Key *key, char *file)
Link Here
|
| 523 |
static int |
848 |
static int |
| 524 |
user_key_command_allowed2(struct passwd *user_pw, Key *key) |
849 |
user_key_command_allowed2(struct passwd *user_pw, Key *key) |
| 525 |
{ |
850 |
{ |
| 526 |
FILE *f; |
851 |
FILE *f = NULL; |
| 527 |
int ok, found_key = 0; |
852 |
int r, ok, found_key = 0; |
| 528 |
struct passwd *pw; |
853 |
struct passwd *pw; |
| 529 |
struct stat st; |
854 |
int i, uid_swapped = 0, ac = 0; |
| 530 |
int status, devnull, p[2], i; |
|
|
| 531 |
pid_t pid; |
855 |
pid_t pid; |
| 532 |
char *username, errmsg[512]; |
856 |
char *username = NULL, *key_fp = NULL, *keytext = NULL; |
|
|
857 |
char *command = NULL, **av = NULL; |
| 858 |
void (*osigchld)(int); |
| 533 |
|
859 |
|
| 534 |
if (options.authorized_keys_command == NULL || |
860 |
if (options.authorized_keys_command == NULL) |
| 535 |
options.authorized_keys_command[0] != '/') |
|
|
| 536 |
return 0; |
861 |
return 0; |
| 537 |
|
|
|
| 538 |
if (options.authorized_keys_command_user == NULL) { |
862 |
if (options.authorized_keys_command_user == NULL) { |
| 539 |
error("No user for AuthorizedKeysCommand specified, skipping"); |
863 |
error("No user for AuthorizedKeysCommand specified, skipping"); |
| 540 |
return 0; |
864 |
return 0; |
| 541 |
} |
865 |
} |
| 542 |
|
866 |
|
|
|
867 |
/* |
| 868 |
* NB. all returns later this function should go via "out" to |
| 869 |
* ensure the original SIGCHLD handler is restored properly. |
| 870 |
*/ |
| 871 |
osigchld = signal(SIGCHLD, SIG_DFL); |
| 872 |
|
| 873 |
/* Prepare and verify the user for the command */ |
| 543 |
username = percent_expand(options.authorized_keys_command_user, |
874 |
username = percent_expand(options.authorized_keys_command_user, |
| 544 |
"u", user_pw->pw_name, (char *)NULL); |
875 |
"u", user_pw->pw_name, (char *)NULL); |
| 545 |
pw = getpwnam(username); |
876 |
pw = getpwnam(username); |
| 546 |
if (pw == NULL) { |
877 |
if (pw == NULL) { |
| 547 |
error("AuthorizedKeysCommandUser \"%s\" not found: %s", |
878 |
error("AuthorizedKeysCommandUser \"%s\" not found: %s", |
| 548 |
username, strerror(errno)); |
879 |
username, strerror(errno)); |
| 549 |
free(username); |
880 |
goto out; |
| 550 |
return 0; |
|
|
| 551 |
} |
881 |
} |
| 552 |
free(username); |
|
|
| 553 |
|
| 554 |
temporarily_use_uid(pw); |
| 555 |
|
882 |
|
| 556 |
if (stat(options.authorized_keys_command, &st) < 0) { |
883 |
/* Prepare AuthorizedKeysCommand */ |
| 557 |
error("Could not stat AuthorizedKeysCommand \"%s\": %s", |
884 |
if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash, |
| 558 |
options.authorized_keys_command, strerror(errno)); |
885 |
SSH_FP_DEFAULT)) == NULL) { |
|
|
886 |
error("%s: sshkey_fingerprint failed", __func__); |
| 559 |
goto out; |
887 |
goto out; |
| 560 |
} |
888 |
} |
| 561 |
if (auth_secure_path(options.authorized_keys_command, &st, NULL, 0, |
889 |
if ((r = sshkey_to_base64(key, &keytext)) != 0) { |
| 562 |
errmsg, sizeof(errmsg)) != 0) { |
890 |
error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r)); |
| 563 |
error("Unsafe AuthorizedKeysCommand: %s", errmsg); |
|
|
| 564 |
goto out; |
891 |
goto out; |
| 565 |
} |
892 |
} |
|
|
893 |
command = percent_expand(options.authorized_keys_command, |
| 894 |
"u", user_pw->pw_name, "h", user_pw->pw_dir, |
| 895 |
"t", sshkey_ssh_name(key), "f", key_fp, "k", keytext, (char *)NULL); |
| 566 |
|
896 |
|
| 567 |
if (pipe(p) != 0) { |
897 |
/* Turn the command into an argument vector */ |
| 568 |
error("%s: pipe: %s", __func__, strerror(errno)); |
898 |
if (split_argv(command, &ac, &av) != 0) { |
|
|
899 |
error("AuthorizedKeysCommand \"%s\" contains invalid quotes", |
| 900 |
command); |
| 901 |
goto out; |
| 902 |
} |
| 903 |
if (ac == 0) { |
| 904 |
error("AuthorizedKeysCommand \"%s\" yielded no arguments", |
| 905 |
command); |
| 569 |
goto out; |
906 |
goto out; |
| 570 |
} |
907 |
} |
| 571 |
|
|
|
| 572 |
debug3("Running AuthorizedKeysCommand: \"%s %s\" as \"%s\"", |
| 573 |
options.authorized_keys_command, user_pw->pw_name, pw->pw_name); |
| 574 |
|
908 |
|
| 575 |
/* |
909 |
/* |
| 576 |
* Don't want to call this in the child, where it can fatal() and |
910 |
* If AuthorizedKeysCommand was run without arguments |
| 577 |
* run cleanup_exit() code. |
911 |
* then fall back to the old behaviour of passing the |
|
|
912 |
* target username as a single argument. |
| 578 |
*/ |
913 |
*/ |
| 579 |
restore_uid(); |
914 |
if (ac == 1) { |
| 580 |
|
915 |
av = xrealloc(av, ac + 2, sizeof(*av)); |
| 581 |
switch ((pid = fork())) { |
916 |
av[1] = xstrdup(user_pw->pw_name); |
| 582 |
case -1: /* error */ |
917 |
av[2] = NULL; |
| 583 |
error("%s: fork: %s", __func__, strerror(errno)); |
918 |
/* Fix up command too, since it is used in log messages */ |
| 584 |
close(p[0]); |
919 |
free(command); |
| 585 |
close(p[1]); |
920 |
xasprintf(&command, "%s %s", av[0], av[1]); |
| 586 |
return 0; |
|
|
| 587 |
case 0: /* child */ |
| 588 |
for (i = 0; i < NSIG; i++) |
| 589 |
signal(i, SIG_DFL); |
| 590 |
|
| 591 |
if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) { |
| 592 |
error("%s: open %s: %s", __func__, _PATH_DEVNULL, |
| 593 |
strerror(errno)); |
| 594 |
_exit(1); |
| 595 |
} |
| 596 |
/* Keep stderr around a while longer to catch errors */ |
| 597 |
if (dup2(devnull, STDIN_FILENO) == -1 || |
| 598 |
dup2(p[1], STDOUT_FILENO) == -1) { |
| 599 |
error("%s: dup2: %s", __func__, strerror(errno)); |
| 600 |
_exit(1); |
| 601 |
} |
| 602 |
closefrom(STDERR_FILENO + 1); |
| 603 |
|
| 604 |
/* Don't use permanently_set_uid() here to avoid fatal() */ |
| 605 |
if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) { |
| 606 |
error("setresgid %u: %s", (u_int)pw->pw_gid, |
| 607 |
strerror(errno)); |
| 608 |
_exit(1); |
| 609 |
} |
| 610 |
if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) { |
| 611 |
error("setresuid %u: %s", (u_int)pw->pw_uid, |
| 612 |
strerror(errno)); |
| 613 |
_exit(1); |
| 614 |
} |
| 615 |
/* stdin is pointed to /dev/null at this point */ |
| 616 |
if (dup2(STDIN_FILENO, STDERR_FILENO) == -1) { |
| 617 |
error("%s: dup2: %s", __func__, strerror(errno)); |
| 618 |
_exit(1); |
| 619 |
} |
| 620 |
|
| 621 |
execl(options.authorized_keys_command, |
| 622 |
options.authorized_keys_command, user_pw->pw_name, NULL); |
| 623 |
|
| 624 |
error("AuthorizedKeysCommand %s exec failed: %s", |
| 625 |
options.authorized_keys_command, strerror(errno)); |
| 626 |
_exit(127); |
| 627 |
default: /* parent */ |
| 628 |
break; |
| 629 |
} |
921 |
} |
| 630 |
|
922 |
|
| 631 |
temporarily_use_uid(pw); |
923 |
if ((pid = subprocess("AuthorizedKeysCommand", pw, command, |
| 632 |
|
924 |
ac, av, &f)) == 0) |
| 633 |
close(p[1]); |
|
|
| 634 |
if ((f = fdopen(p[0], "r")) == NULL) { |
| 635 |
error("%s: fdopen: %s", __func__, strerror(errno)); |
| 636 |
close(p[0]); |
| 637 |
/* Don't leave zombie child */ |
| 638 |
kill(pid, SIGTERM); |
| 639 |
while (waitpid(pid, NULL, 0) == -1 && errno == EINTR) |
| 640 |
; |
| 641 |
goto out; |
925 |
goto out; |
| 642 |
} |
926 |
|
|
|
927 |
uid_swapped = 1; |
| 928 |
temporarily_use_uid(pw); |
| 929 |
|
| 643 |
ok = check_authkeys_file(f, options.authorized_keys_command, key, pw); |
930 |
ok = check_authkeys_file(f, options.authorized_keys_command, key, pw); |
| 644 |
fclose(f); |
|
|
| 645 |
|
931 |
|
| 646 |
while (waitpid(pid, &status, 0) == -1) { |
932 |
if (exited_cleanly(pid, "AuthorizedKeysCommand", command)) |
| 647 |
if (errno != EINTR) { |
|
|
| 648 |
error("%s: waitpid: %s", __func__, strerror(errno)); |
| 649 |
goto out; |
| 650 |
} |
| 651 |
} |
| 652 |
if (WIFSIGNALED(status)) { |
| 653 |
error("AuthorizedKeysCommand %s exited on signal %d", |
| 654 |
options.authorized_keys_command, WTERMSIG(status)); |
| 655 |
goto out; |
933 |
goto out; |
| 656 |
} else if (WEXITSTATUS(status) != 0) { |
934 |
|
| 657 |
error("AuthorizedKeysCommand %s returned status %d", |
935 |
/* Read completed successfully */ |
| 658 |
options.authorized_keys_command, WEXITSTATUS(status)); |
|
|
| 659 |
goto out; |
| 660 |
} |
| 661 |
found_key = ok; |
936 |
found_key = ok; |
| 662 |
out: |
937 |
out: |
| 663 |
restore_uid(); |
938 |
if (f != NULL) |
|
|
939 |
fclose(f); |
| 940 |
signal(SIGCHLD, osigchld); |
| 941 |
for (i = 0; i < ac; i++) |
| 942 |
free(av[i]); |
| 943 |
free(av); |
| 944 |
if (uid_swapped) |
| 945 |
restore_uid(); |
| 946 |
free(command); |
| 947 |
free(username); |
| 948 |
free(key_fp); |
| 949 |
free(keytext); |
| 664 |
return found_key; |
950 |
return found_key; |
| 665 |
} |
951 |
} |
| 666 |
|
952 |
|