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