|
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 178-204
Link Here
|
| 178 |
|
178 |
|
| 179 |
/* return 1 if user allows given key */ |
179 |
/* return 1 if user allows given key */ |
| 180 |
static int |
180 |
static int |
| 181 |
user_key_allowed2(struct passwd *pw, Key *key, char *file) |
181 |
user_search_key_in_file(FILE *f, char *file, Key* key, struct passwd *pw) |
| 182 |
{ |
182 |
{ |
| 183 |
char line[SSH_MAX_PUBKEY_BYTES]; |
183 |
char line[SSH_MAX_PUBKEY_BYTES]; |
| 184 |
const char *reason; |
184 |
const char *reason; |
| 185 |
int found_key = 0; |
185 |
int found_key = 0; |
| 186 |
FILE *f; |
|
|
| 187 |
u_long linenum = 0; |
186 |
u_long linenum = 0; |
| 188 |
Key *found; |
187 |
Key *found; |
| 189 |
char *fp; |
188 |
char *fp; |
| 190 |
|
189 |
|
| 191 |
/* Temporarily use the user's uid. */ |
|
|
| 192 |
temporarily_use_uid(pw); |
| 193 |
|
| 194 |
debug("trying public key file %s", file); |
| 195 |
f = auth_openkeyfile(file, pw, options.strict_modes); |
| 196 |
|
| 197 |
if (!f) { |
| 198 |
restore_uid(); |
| 199 |
return 0; |
| 200 |
} |
| 201 |
|
| 202 |
found_key = 0; |
190 |
found_key = 0; |
| 203 |
found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type); |
191 |
found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type); |
| 204 |
|
192 |
|
|
Lines 273-280
Link Here
|
| 273 |
break; |
261 |
break; |
| 274 |
} |
262 |
} |
| 275 |
} |
263 |
} |
| 276 |
restore_uid(); |
|
|
| 277 |
fclose(f); |
| 278 |
key_free(found); |
264 |
key_free(found); |
| 279 |
if (!found_key) |
265 |
if (!found_key) |
| 280 |
debug2("key not found"); |
266 |
debug2("key not found"); |
|
Lines 321-333
Link Here
|
| 321 |
return ret; |
307 |
return ret; |
| 322 |
} |
308 |
} |
| 323 |
|
309 |
|
| 324 |
/* check whether given key is in .ssh/authorized_keys* */ |
310 |
/* return 1 if user allows given key */ |
|
|
311 |
static int |
| 312 |
user_key_allowed2(struct passwd *pw, Key *key, char *file) |
| 313 |
{ |
| 314 |
FILE *f; |
| 315 |
int found_key = 0; |
| 316 |
|
| 317 |
/* Temporarily use the user's uid. */ |
| 318 |
temporarily_use_uid(pw); |
| 319 |
|
| 320 |
debug("trying public key file %s", file); |
| 321 |
f = auth_openkeyfile(file, pw, options.strict_modes); |
| 322 |
|
| 323 |
if (f) { |
| 324 |
found_key = user_search_key_in_file (f, file, key, pw); |
| 325 |
fclose(f); |
| 326 |
} |
| 327 |
|
| 328 |
restore_uid(); |
| 329 |
return found_key; |
| 330 |
} |
| 331 |
|
| 332 |
#ifdef WITH_AUTHORIZED_KEYS_COMMAND |
| 333 |
|
| 334 |
#define WHITESPACE " \t\r\n" |
| 335 |
|
| 336 |
/* return 1 if user allows given key */ |
| 337 |
static int |
| 338 |
user_key_via_command_allowed2(struct passwd *pw, Key *key) |
| 339 |
{ |
| 340 |
FILE *f; |
| 341 |
int found_key = 0; |
| 342 |
char *progname = NULL; |
| 343 |
char *cp; |
| 344 |
struct passwd *runas_pw; |
| 345 |
struct stat st; |
| 346 |
int childdescriptors[2], i; |
| 347 |
pid_t pstat, pid, child; |
| 348 |
|
| 349 |
if (options.authorized_keys_command == NULL || options.authorized_keys_command[0] != '/') |
| 350 |
return -1; |
| 351 |
|
| 352 |
/* get the run as identity from config */ |
| 353 |
runas_pw = (options.authorized_keys_command_runas == NULL)? pw |
| 354 |
: getpwnam (options.authorized_keys_command_runas); |
| 355 |
if (!runas_pw) { |
| 356 |
error("%s: getpwnam(\"%s\"): %s", __func__, |
| 357 |
options.authorized_keys_command_runas, strerror(errno)); |
| 358 |
return 0; |
| 359 |
} |
| 360 |
|
| 361 |
/* Temporarily use the specified uid. */ |
| 362 |
if (runas_pw->pw_uid != 0) |
| 363 |
temporarily_use_uid(runas_pw); |
| 364 |
|
| 365 |
progname = xstrdup(options.authorized_keys_command); |
| 366 |
|
| 367 |
debug3("%s: checking program '%s'", __func__, progname); |
| 368 |
|
| 369 |
if (stat (progname, &st) < 0) { |
| 370 |
error("%s: stat(\"%s\"): %s", __func__, |
| 371 |
progname, strerror(errno)); |
| 372 |
goto go_away; |
| 373 |
} |
| 374 |
|
| 375 |
if (st.st_uid != 0 || (st.st_mode & 022) != 0) { |
| 376 |
error("bad ownership or modes for AuthorizedKeysCommand \"%s\"", |
| 377 |
progname); |
| 378 |
goto go_away; |
| 379 |
} |
| 380 |
|
| 381 |
if (!S_ISREG(st.st_mode)) { |
| 382 |
error("AuthorizedKeysCommand \"%s\" is not a regular file", |
| 383 |
progname); |
| 384 |
goto go_away; |
| 385 |
} |
| 386 |
|
| 387 |
/* |
| 388 |
* Descend the path, checking that each component is a |
| 389 |
* root-owned directory with strict permissions. |
| 390 |
*/ |
| 391 |
do { |
| 392 |
if ((cp = strrchr(progname, '/')) == NULL) |
| 393 |
break; |
| 394 |
else |
| 395 |
*cp = '\0'; |
| 396 |
|
| 397 |
debug3("%s: checking component '%s'", __func__, (*progname == '\0' ? "/" : progname)); |
| 398 |
|
| 399 |
if (stat((*progname == '\0' ? "/" : progname), &st) != 0) { |
| 400 |
error("%s: stat(\"%s\"): %s", __func__, |
| 401 |
progname, strerror(errno)); |
| 402 |
goto go_away; |
| 403 |
} |
| 404 |
if (st.st_uid != 0 || (st.st_mode & 022) != 0) { |
| 405 |
error("bad ownership or modes for AuthorizedKeysCommand path component \"%s\"", |
| 406 |
progname); |
| 407 |
goto go_away; |
| 408 |
} |
| 409 |
if (!S_ISDIR(st.st_mode)) { |
| 410 |
error("AuthorizedKeysCommand path component \"%s\" is not a directory", |
| 411 |
progname); |
| 412 |
goto go_away; |
| 413 |
} |
| 414 |
} while (1); |
| 415 |
|
| 416 |
/* open the pipe and read the keys */ |
| 417 |
if (pipe(childdescriptors)) { |
| 418 |
error("failed to pipe(2) for AuthorizedKeysCommand: %s", |
| 419 |
strerror(errno)); |
| 420 |
goto go_away; |
| 421 |
} |
| 422 |
|
| 423 |
child = fork(); |
| 424 |
if (child == -1) { |
| 425 |
error("failed to fork(2) for AuthorizedKeysCommand: %s", |
| 426 |
strerror(errno)); |
| 427 |
goto go_away; |
| 428 |
} else if (child == 0) { |
| 429 |
/* we're in the child process here -- we should never return from this block. */ |
| 430 |
/* permanently drop privs in child process */ |
| 431 |
if (runas_pw->pw_uid != 0) { |
| 432 |
restore_uid(); |
| 433 |
permanently_set_uid(runas_pw); |
| 434 |
} |
| 435 |
|
| 436 |
close(childdescriptors[0]); |
| 437 |
/* put the write end of the pipe on stdout (FD 1) */ |
| 438 |
if (dup2(childdescriptors[1], 1) == -1) { |
| 439 |
error("failed to dup2(2) from AuthorizedKeysCommand: %s", |
| 440 |
strerror(errno)); |
| 441 |
_exit(127); |
| 442 |
} |
| 443 |
|
| 444 |
debug3("about to execl() AuthorizedKeysCommand: \"%s\" \"%s\"", options.authorized_keys_command, pw->pw_name); |
| 445 |
/* see session.c:child_close_fds() */ |
| 446 |
for (i = 3; i < 64; ++i) { |
| 447 |
close(i); |
| 448 |
} |
| 449 |
|
| 450 |
execl(options.authorized_keys_command, options.authorized_keys_command, pw->pw_name, NULL); |
| 451 |
|
| 452 |
/* if we got here, it didn't work */ |
| 453 |
error("failed to execl AuthorizedKeysCommand: %s", strerror(errno)); /* this won't work because we closed the fds above */ |
| 454 |
_exit(127); |
| 455 |
} |
| 456 |
|
| 457 |
close(childdescriptors[1]); |
| 458 |
f = fdopen(childdescriptors[0], "r"); |
| 459 |
if (!f) { |
| 460 |
error("%s: could not buffer FDs from AuthorizedKeysCommand (\"%s\", \"r\"): %s", __func__, |
| 461 |
options.authorized_keys_command, strerror (errno)); |
| 462 |
goto go_away; |
| 463 |
} |
| 464 |
|
| 465 |
found_key = user_search_key_in_file (f, options.authorized_keys_command, key, pw); |
| 466 |
fclose (f); |
| 467 |
do { |
| 468 |
pid = waitpid(child, &pstat, 0); |
| 469 |
} while (pid == -1 && errno == EINTR); |
| 470 |
|
| 471 |
/* what about the return value from the child process? */ |
| 472 |
go_away: |
| 473 |
if (progname) |
| 474 |
xfree (progname); |
| 475 |
|
| 476 |
if (runas_pw->pw_uid != 0) |
| 477 |
restore_uid(); |
| 478 |
return found_key; |
| 479 |
} |
| 480 |
#endif |
| 481 |
|
| 482 |
/* check whether given key is in <AuthorizedKeysCommand or .ssh/authorized_keys* */ |
| 325 |
int |
483 |
int |
| 326 |
user_key_allowed(struct passwd *pw, Key *key) |
484 |
user_key_allowed(struct passwd *pw, Key *key) |
| 327 |
{ |
485 |
{ |
| 328 |
int success; |
486 |
int success; |
| 329 |
char *file; |
487 |
char *file; |
| 330 |
|
488 |
|
|
|
489 |
#ifdef WITH_AUTHORIZED_KEYS_COMMAND |
| 490 |
success = user_key_via_command_allowed2(pw, key); |
| 491 |
if (success > 0) |
| 492 |
return success; |
| 493 |
#endif |
| 494 |
|
| 331 |
if (auth_key_is_revoked(key)) |
495 |
if (auth_key_is_revoked(key)) |
| 332 |
return 0; |
496 |
return 0; |
| 333 |
if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key)) |
497 |
if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key)) |