|
7 | 7 |
|
8 | 8 | #include "acl-util.h" |
9 | 9 | #include "alloc-util.h" |
| 10 | +#include "errno-util.h" |
10 | 11 | #include "string-util.h" |
11 | 12 | #include "strv.h" |
12 | 13 | #include "user-util.h" |
13 | 14 | #include "util.h" |
14 | 15 |
|
| 16 | +#if HAVE_ACL |
| 17 | + |
15 | 18 | int acl_find_uid(acl_t acl, uid_t uid, acl_entry_t *ret_entry) { |
16 | 19 | acl_entry_t i; |
17 | 20 | int r; |
@@ -434,3 +437,161 @@ int fd_add_uid_acl_permission( |
434 | 437 |
|
435 | 438 | return 0; |
436 | 439 | } |
| 440 | + |
| 441 | +int fd_acl_make_read_only(int fd) { |
| 442 | + _cleanup_(acl_freep) acl_t acl = NULL; |
| 443 | + bool changed = false; |
| 444 | + acl_entry_t i; |
| 445 | + int r; |
| 446 | + |
| 447 | + assert(fd >= 0); |
| 448 | + |
| 449 | + /* Safely drops all W bits from all relevant ACL entries of the file, without changing entries which |
| 450 | + * are masked by the ACL mask */ |
| 451 | + |
| 452 | + acl = acl_get_fd(fd); |
| 453 | + if (!acl) { |
| 454 | + |
| 455 | + if (!ERRNO_IS_NOT_SUPPORTED(errno)) |
| 456 | + return -errno; |
| 457 | + |
| 458 | + /* No ACLs? Then just update the regular mode_t */ |
| 459 | + return fd_acl_make_read_only_fallback(fd); |
| 460 | + } |
| 461 | + |
| 462 | + for (r = acl_get_entry(acl, ACL_FIRST_ENTRY, &i); |
| 463 | + r > 0; |
| 464 | + r = acl_get_entry(acl, ACL_NEXT_ENTRY, &i)) { |
| 465 | + acl_permset_t permset; |
| 466 | + acl_tag_t tag; |
| 467 | + int b; |
| 468 | + |
| 469 | + if (acl_get_tag_type(i, &tag) < 0) |
| 470 | + return -errno; |
| 471 | + |
| 472 | + /* These three control the x bits overall (as ACL_MASK affects all remaining tags) */ |
| 473 | + if (!IN_SET(tag, ACL_USER_OBJ, ACL_MASK, ACL_OTHER)) |
| 474 | + continue; |
| 475 | + |
| 476 | + if (acl_get_permset(i, &permset) < 0) |
| 477 | + return -errno; |
| 478 | + |
| 479 | + b = acl_get_perm(permset, ACL_WRITE); |
| 480 | + if (b < 0) |
| 481 | + return -errno; |
| 482 | + |
| 483 | + if (b) { |
| 484 | + if (acl_delete_perm(permset, ACL_WRITE) < 0) |
| 485 | + return -errno; |
| 486 | + |
| 487 | + changed = true; |
| 488 | + } |
| 489 | + } |
| 490 | + if (r < 0) |
| 491 | + return -errno; |
| 492 | + |
| 493 | + if (!changed) |
| 494 | + return 0; |
| 495 | + |
| 496 | + if (acl_set_fd(fd, acl) < 0) { |
| 497 | + if (!ERRNO_IS_NOT_SUPPORTED(errno)) |
| 498 | + return -errno; |
| 499 | + |
| 500 | + return fd_acl_make_read_only_fallback(fd); |
| 501 | + } |
| 502 | + |
| 503 | + return 1; |
| 504 | +} |
| 505 | + |
| 506 | +int fd_acl_make_writable(int fd) { |
| 507 | + _cleanup_(acl_freep) acl_t acl = NULL; |
| 508 | + acl_entry_t i; |
| 509 | + int r; |
| 510 | + |
| 511 | + /* Safely adds the writable bit to the owner's ACL entry of this inode. (And only the owner's! – This |
| 512 | + * not the obvious inverse of fd_acl_make_read_only() hence!) */ |
| 513 | + |
| 514 | + acl = acl_get_fd(fd); |
| 515 | + if (!acl) { |
| 516 | + if (!ERRNO_IS_NOT_SUPPORTED(errno)) |
| 517 | + return -errno; |
| 518 | + |
| 519 | + /* No ACLs? Then just update the regular mode_t */ |
| 520 | + return fd_acl_make_writable_fallback(fd); |
| 521 | + } |
| 522 | + |
| 523 | + for (r = acl_get_entry(acl, ACL_FIRST_ENTRY, &i); |
| 524 | + r > 0; |
| 525 | + r = acl_get_entry(acl, ACL_NEXT_ENTRY, &i)) { |
| 526 | + acl_permset_t permset; |
| 527 | + acl_tag_t tag; |
| 528 | + int b; |
| 529 | + |
| 530 | + if (acl_get_tag_type(i, &tag) < 0) |
| 531 | + return -errno; |
| 532 | + |
| 533 | + if (tag != ACL_USER_OBJ) |
| 534 | + continue; |
| 535 | + |
| 536 | + if (acl_get_permset(i, &permset) < 0) |
| 537 | + return -errno; |
| 538 | + |
| 539 | + b = acl_get_perm(permset, ACL_WRITE); |
| 540 | + if (b < 0) |
| 541 | + return -errno; |
| 542 | + |
| 543 | + if (b) |
| 544 | + return 0; /* Already set? Then there's nothing to do. */ |
| 545 | + |
| 546 | + if (acl_add_perm(permset, ACL_WRITE) < 0) |
| 547 | + return -errno; |
| 548 | + |
| 549 | + break; |
| 550 | + } |
| 551 | + if (r < 0) |
| 552 | + return -errno; |
| 553 | + |
| 554 | + if (acl_set_fd(fd, acl) < 0) { |
| 555 | + if (!ERRNO_IS_NOT_SUPPORTED(errno)) |
| 556 | + return -errno; |
| 557 | + |
| 558 | + return fd_acl_make_writable_fallback(fd); |
| 559 | + } |
| 560 | + |
| 561 | + return 1; |
| 562 | +} |
| 563 | +#endif |
| 564 | + |
| 565 | +int fd_acl_make_read_only_fallback(int fd) { |
| 566 | + struct stat st; |
| 567 | + |
| 568 | + assert(fd >= 0); |
| 569 | + |
| 570 | + if (fstat(fd, &st) < 0) |
| 571 | + return -errno; |
| 572 | + |
| 573 | + if ((st.st_mode & 0222) == 0) |
| 574 | + return 0; |
| 575 | + |
| 576 | + if (fchmod(fd, st.st_mode & 0555) < 0) |
| 577 | + return -errno; |
| 578 | + |
| 579 | + return 1; |
| 580 | +} |
| 581 | + |
| 582 | +int fd_acl_make_writable_fallback(int fd) { |
| 583 | + struct stat st; |
| 584 | + |
| 585 | + assert(fd >= 0); |
| 586 | + |
| 587 | + if (fstat(fd, &st) < 0) |
| 588 | + return -errno; |
| 589 | + |
| 590 | + if ((st.st_mode & 0200) != 0) /* already set */ |
| 591 | + return 0; |
| 592 | + |
| 593 | + if (fchmod(fd, (st.st_mode & 07777) | 0200) < 0) |
| 594 | + return -errno; |
| 595 | + |
| 596 | + return 1; |
| 597 | +} |
0 commit comments