| 1 | /* |
| 2 | * POSIX ACL get/set/delete via the generic xattr syscalls, addressing the |
| 3 | * kernel "system.posix_acl_{access,default}" attributes directly so that the |
| 4 | * operation can be confined to a held O_NOFOLLOW fd (fsetxattr) or a |
| 5 | * dirfd+leaf with AT_SYMLINK_NOFOLLOW (setxattrat). This replaces the path- |
| 6 | * based libacl acl_*_file() calls on Linux, where those would re-resolve the |
| 7 | * path and could be redirected by a parent-component symlink race. |
| 8 | * |
| 9 | * Copyright (C) 2026 Wayne Davison & the rsync project |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 3 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, visit the http://fsf.org website. |
| 23 | */ |
| 24 | |
| 25 | #ifdef SUPPORT_ACL_FD |
| 26 | |
| 27 | #include <stdint.h> |
| 28 | |
| 29 | /* A single logical POSIX ACL entry in host-native form. The tag values are |
| 30 | * the stable kernel ABI numbers (== the libacl ACL_* constants), so they map |
| 31 | * straight onto the on-disk e_tag without translation. */ |
| 32 | typedef struct { |
| 33 | uint16_t tag; /* RACL_USER_OBJ / USER / GROUP_OBJ / GROUP / MASK / OTHER */ |
| 34 | uint16_t perm; /* permission bits: read=4, write=2, execute=1 */ |
| 35 | uint32_t id; /* uid/gid for USER/GROUP entries; RACL_UNDEFINED_ID otherwise */ |
| 36 | } rsync_acl_ent; |
| 37 | |
| 38 | #define RACL_USER_OBJ 0x01 |
| 39 | #define RACL_USER 0x02 |
| 40 | #define RACL_GROUP_OBJ 0x04 |
| 41 | #define RACL_GROUP 0x08 |
| 42 | #define RACL_MASK 0x10 |
| 43 | #define RACL_OTHER 0x20 |
| 44 | |
| 45 | #define RACL_UNDEFINED_ID ((uint32_t)-1) |
| 46 | |
| 47 | /* Read the access (want_default==0) or default (want_default!=0) ACL. |
| 48 | * |
| 49 | * On success returns 0 and sets *entries to a malloc()ed array of *count |
| 50 | * entries (the caller frees it with free(); *entries may be NULL when |
| 51 | * *count==0, which means "no explicit ACL present" -- e.g. ENODATA). |
| 52 | * |
| 53 | * On failure returns -1 with errno set. Callers distinguish: |
| 54 | * ENOTSUP/EOPNOTSUPP - this filesystem has no ACL support (may differ per fs) |
| 55 | * ENOSYS - the at-variant syscalls are unavailable on this kernel |
| 56 | * The fd-variant operates on a held, already-NOFOLLOW-opened descriptor. The |
| 57 | * at-variant resolves leaf relative to dirfd and never follows a leaf symlink. */ |
| 58 | int xacl_get_fd(int fd, int want_default, rsync_acl_ent **entries, int *count); |
| 59 | int xacl_get_at(int dirfd, const char *leaf, int want_default, rsync_acl_ent **entries, int *count); |
| 60 | |
| 61 | /* Write the given entries as the access/default ACL. The entries are emitted |
| 62 | * in canonical order; the kernel validates them (a malformed set -> EINVAL). */ |
| 63 | int xacl_set_fd(int fd, int want_default, const rsync_acl_ent *entries, int count); |
| 64 | int xacl_set_at(int dirfd, const char *leaf, int want_default, const rsync_acl_ent *entries, int count); |
| 65 | |
| 66 | /* Delete a directory's default ACL. A missing default ACL is success. */ |
| 67 | int xacl_del_default_fd(int fd); |
| 68 | int xacl_del_default_at(int dirfd, const char *leaf); |
| 69 | |
| 70 | /* Cached runtime probe: are the *xattrat syscalls usable on this kernel? |
| 71 | * Returns 0 when they are absent (so callers can fall back) or unbuilt. */ |
| 72 | int xacl_at_available(void); |
| 73 | |
| 74 | #endif /* SUPPORT_ACL_FD */ |