|
Lines 27-32
Link Here
|
| 27 |
|
27 |
|
| 28 |
#include <sys/types.h> |
28 |
#include <sys/types.h> |
| 29 |
#include <sys/stat.h> |
29 |
#include <sys/stat.h> |
|
|
30 |
#include <sys/wait.h> |
| 30 |
|
31 |
|
| 31 |
#include <fcntl.h> |
32 |
#include <fcntl.h> |
| 32 |
#include <pwd.h> |
33 |
#include <pwd.h> |
|
Lines 264-290
match_principals_file(char *file, struct
Link Here
|
| 264 |
|
265 |
|
| 265 |
/* return 1 if user allows given key */ |
266 |
/* return 1 if user allows given key */ |
| 266 |
static int |
267 |
static int |
| 267 |
user_key_allowed2(struct passwd *pw, Key *key, char *file) |
268 |
user_search_key_in_file(FILE *f, char *file, Key* key, struct passwd *pw) |
| 268 |
{ |
269 |
{ |
| 269 |
char line[SSH_MAX_PUBKEY_BYTES]; |
270 |
char line[SSH_MAX_PUBKEY_BYTES]; |
| 270 |
const char *reason; |
271 |
const char *reason; |
| 271 |
int found_key = 0; |
272 |
int found_key = 0; |
| 272 |
FILE *f; |
|
|
| 273 |
u_long linenum = 0; |
273 |
u_long linenum = 0; |
| 274 |
Key *found; |
274 |
Key *found; |
| 275 |
char *fp; |
275 |
char *fp; |
| 276 |
|
276 |
|
| 277 |
/* Temporarily use the user's uid. */ |
|
|
| 278 |
temporarily_use_uid(pw); |
| 279 |
|
| 280 |
debug("trying public key file %s", file); |
| 281 |
f = auth_openkeyfile(file, pw, options.strict_modes); |
| 282 |
|
| 283 |
if (!f) { |
| 284 |
restore_uid(); |
| 285 |
return 0; |
| 286 |
} |
| 287 |
|
| 288 |
found_key = 0; |
277 |
found_key = 0; |
| 289 |
found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type); |
278 |
found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type); |
| 290 |
|
279 |
|
|
Lines 377-384
user_key_allowed2(struct passwd *pw, Key
Link Here
|
| 377 |
break; |
366 |
break; |
| 378 |
} |
367 |
} |
| 379 |
} |
368 |
} |
| 380 |
restore_uid(); |
|
|
| 381 |
fclose(f); |
| 382 |
key_free(found); |
369 |
key_free(found); |
| 383 |
if (!found_key) |
370 |
if (!found_key) |
| 384 |
debug2("key not found"); |
371 |
debug2("key not found"); |
|
Lines 440-452
user_cert_trusted_ca(struct passwd *pw,
Link Here
|
| 440 |
return ret; |
427 |
return ret; |
| 441 |
} |
428 |
} |
| 442 |
|
429 |
|
| 443 |
/* check whether given key is in .ssh/authorized_keys* */ |
430 |
/* return 1 if user allows given key */ |
|
|
431 |
static int |
| 432 |
user_key_allowed2(struct passwd *pw, Key *key, char *file) |
| 433 |
{ |
| 434 |
FILE *f; |
| 435 |
int found_key = 0; |
| 436 |
|
| 437 |
/* Temporarily use the user's uid. */ |
| 438 |
temporarily_use_uid(pw); |
| 439 |
|
| 440 |
debug("trying public key file %s", file); |
| 441 |
f = auth_openkeyfile(file, pw, options.strict_modes); |
| 442 |
|
| 443 |
if (f) { |
| 444 |
found_key = user_search_key_in_file (f, file, key, pw); |
| 445 |
fclose(f); |
| 446 |
} |
| 447 |
|
| 448 |
restore_uid(); |
| 449 |
return found_key; |
| 450 |
} |
| 451 |
|
| 452 |
#ifdef WITH_AUTHORIZED_KEYS_COMMAND |
| 453 |
|
| 454 |
#define WHITESPACE " \t\r\n" |
| 455 |
|
| 456 |
/* return 1 if user allows given key */ |
| 457 |
static int |
| 458 |
user_key_via_command_allowed2(struct passwd *pw, Key *key) |
| 459 |
{ |
| 460 |
FILE *f; |
| 461 |
int found_key = 0; |
| 462 |
char *progname = NULL; |
| 463 |
char *cp; |
| 464 |
struct passwd *runas_pw; |
| 465 |
struct stat st; |
| 466 |
int childdescriptors[2], i; |
| 467 |
pid_t pstat, pid, child; |
| 468 |
|
| 469 |
if (options.authorized_keys_command == NULL || options.authorized_keys_command[0] != '/') |
| 470 |
return -1; |
| 471 |
|
| 472 |
/* get the run as identity from config */ |
| 473 |
runas_pw = (options.authorized_keys_command_runas == NULL)? pw |
| 474 |
: getpwnam (options.authorized_keys_command_runas); |
| 475 |
if (!runas_pw) { |
| 476 |
error("%s: getpwnam(\"%s\"): %s", __func__, |
| 477 |
options.authorized_keys_command_runas, strerror(errno)); |
| 478 |
return 0; |
| 479 |
} |
| 480 |
|
| 481 |
/* Temporarily use the specified uid. */ |
| 482 |
if (runas_pw->pw_uid != 0) |
| 483 |
temporarily_use_uid(runas_pw); |
| 484 |
|
| 485 |
progname = xstrdup(options.authorized_keys_command); |
| 486 |
|
| 487 |
debug3("%s: checking program '%s'", __func__, progname); |
| 488 |
|
| 489 |
if (stat (progname, &st) < 0) { |
| 490 |
error("%s: stat(\"%s\"): %s", __func__, |
| 491 |
progname, strerror(errno)); |
| 492 |
goto go_away; |
| 493 |
} |
| 494 |
|
| 495 |
if (st.st_uid != 0 || (st.st_mode & 022) != 0) { |
| 496 |
error("bad ownership or modes for AuthorizedKeysCommand \"%s\"", |
| 497 |
progname); |
| 498 |
goto go_away; |
| 499 |
} |
| 500 |
|
| 501 |
if (!S_ISREG(st.st_mode)) { |
| 502 |
error("AuthorizedKeysCommand \"%s\" is not a regular file", |
| 503 |
progname); |
| 504 |
goto go_away; |
| 505 |
} |
| 506 |
|
| 507 |
/* |
| 508 |
* Descend the path, checking that each component is a |
| 509 |
* root-owned directory with strict permissions. |
| 510 |
*/ |
| 511 |
do { |
| 512 |
if ((cp = strrchr(progname, '/')) == NULL) |
| 513 |
break; |
| 514 |
else |
| 515 |
*cp = '\0'; |
| 516 |
|
| 517 |
debug3("%s: checking component '%s'", __func__, (*progname == '\0' ? "/" : progname)); |
| 518 |
|
| 519 |
if (stat((*progname == '\0' ? "/" : progname), &st) != 0) { |
| 520 |
error("%s: stat(\"%s\"): %s", __func__, |
| 521 |
progname, strerror(errno)); |
| 522 |
goto go_away; |
| 523 |
} |
| 524 |
if (st.st_uid != 0 || (st.st_mode & 022) != 0) { |
| 525 |
error("bad ownership or modes for AuthorizedKeysCommand path component \"%s\"", |
| 526 |
progname); |
| 527 |
goto go_away; |
| 528 |
} |
| 529 |
if (!S_ISDIR(st.st_mode)) { |
| 530 |
error("AuthorizedKeysCommand path component \"%s\" is not a directory", |
| 531 |
progname); |
| 532 |
goto go_away; |
| 533 |
} |
| 534 |
} while (1); |
| 535 |
|
| 536 |
/* open the pipe and read the keys */ |
| 537 |
if (pipe(childdescriptors)) { |
| 538 |
error("failed to pipe(2) for AuthorizedKeysCommand: %s", |
| 539 |
strerror(errno)); |
| 540 |
goto go_away; |
| 541 |
} |
| 542 |
|
| 543 |
child = fork(); |
| 544 |
if (child == -1) { |
| 545 |
error("failed to fork(2) for AuthorizedKeysCommand: %s", |
| 546 |
strerror(errno)); |
| 547 |
goto go_away; |
| 548 |
} else if (child == 0) { |
| 549 |
/* we're in the child process here -- we should never return from this block. */ |
| 550 |
/* permanently drop privs in child process */ |
| 551 |
if (runas_pw->pw_uid != 0) { |
| 552 |
restore_uid(); |
| 553 |
permanently_set_uid(runas_pw); |
| 554 |
} |
| 555 |
|
| 556 |
close(childdescriptors[0]); |
| 557 |
/* put the write end of the pipe on stdout (FD 1) */ |
| 558 |
if (dup2(childdescriptors[1], 1) == -1) { |
| 559 |
error("failed to dup2(2) from AuthorizedKeysCommand: %s", |
| 560 |
strerror(errno)); |
| 561 |
_exit(127); |
| 562 |
} |
| 563 |
|
| 564 |
debug3("about to execl() AuthorizedKeysCommand: \"%s\" \"%s\"", options.authorized_keys_command, pw->pw_name); |
| 565 |
/* see session.c:child_close_fds() */ |
| 566 |
for (i = 3; i < 64; ++i) { |
| 567 |
close(i); |
| 568 |
} |
| 569 |
|
| 570 |
execl(options.authorized_keys_command, options.authorized_keys_command, pw->pw_name, NULL); |
| 571 |
|
| 572 |
/* if we got here, it didn't work */ |
| 573 |
error("failed to execl AuthorizedKeysCommand: %s", strerror(errno)); /* this won't work because we closed the fds above */ |
| 574 |
_exit(127); |
| 575 |
} |
| 576 |
|
| 577 |
close(childdescriptors[1]); |
| 578 |
f = fdopen(childdescriptors[0], "r"); |
| 579 |
if (!f) { |
| 580 |
error("%s: could not buffer FDs from AuthorizedKeysCommand (\"%s\", \"r\"): %s", __func__, |
| 581 |
options.authorized_keys_command, strerror (errno)); |
| 582 |
goto go_away; |
| 583 |
} |
| 584 |
|
| 585 |
found_key = user_search_key_in_file (f, options.authorized_keys_command, key, pw); |
| 586 |
fclose (f); |
| 587 |
do { |
| 588 |
pid = waitpid(child, &pstat, 0); |
| 589 |
} while (pid == -1 && errno == EINTR); |
| 590 |
|
| 591 |
/* what about the return value from the child process? */ |
| 592 |
go_away: |
| 593 |
if (progname) |
| 594 |
xfree (progname); |
| 595 |
|
| 596 |
if (runas_pw->pw_uid != 0) |
| 597 |
restore_uid(); |
| 598 |
return found_key; |
| 599 |
} |
| 600 |
#endif |
| 601 |
|
| 602 |
/* check whether given key is in <AuthorizedKeysCommand or .ssh/authorized_keys* */ |
| 444 |
int |
603 |
int |
| 445 |
user_key_allowed(struct passwd *pw, Key *key) |
604 |
user_key_allowed(struct passwd *pw, Key *key) |
| 446 |
{ |
605 |
{ |
| 447 |
int success; |
606 |
int success; |
| 448 |
char *file; |
607 |
char *file; |
| 449 |
|
608 |
|
|
|
609 |
#ifdef WITH_AUTHORIZED_KEYS_COMMAND |
| 610 |
success = user_key_via_command_allowed2(pw, key); |
| 611 |
if (success > 0) |
| 612 |
return success; |
| 613 |
#endif |
| 614 |
|
| 450 |
if (auth_key_is_revoked(key)) |
615 |
if (auth_key_is_revoked(key)) |
| 451 |
return 0; |
616 |
return 0; |
| 452 |
if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key)) |
617 |
if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key)) |