HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/tmpfiles/tmpfiles.c
Git blob: c4caa0b857918b2141bedc1762e40c4e54e030a4
Lines 1001–2000 of 5,453
/* Do we need a chown()? */
do_chown = (new_uid != st->st_uid) || (new_gid != st->st_gid);
/* Calculate the mode to apply */
new_mode = i->mode_set && (creation != CREATION_EXISTING || !i->mode_only_create) ?
(i->mask_perms ? process_mask_perms(i->mode, st->st_mode) : i->mode) :
(st->st_mode & 07777);
do_chmod = ((new_mode ^ st->st_mode) & 07777) != 0;
if (do_chmod && do_chown) {
/* Before we issue the chmod() let's reduce the access mode to the common bits of the old and
* the new mode. That way there's no time window where the file exists under the old owner
* with more than the old access modes — and not under the new owner with more than the new
* access modes either. */
if (S_ISLNK(st->st_mode))
log_debug("Skipping temporary mode fix for symlink %s.", path);
else {
mode_t m = new_mode & st->st_mode; /* Mask new mode by old mode */
if (((m ^ st->st_mode) & 07777) == 0)
log_debug("\"%s\" matches temporary mode %o already.", path, m);
else {
log_action("Would temporarily change", "Temporarily changing",
"%s \"%s\" to mode %o", path, m);
if (!arg_dry_run) {
r = fchmod_opath(fd, m);
if (r < 0)
return log_error_errno(r, "fchmod() of %s failed: %m", path);
}
}
}
}
if (do_chown) {
log_action("Would change", "Changing",
"%s \"%s\" to owner "UID_FMT":"GID_FMT, path, new_uid, new_gid);
if (!arg_dry_run &&
fchownat(fd, "",
new_uid != st->st_uid ? new_uid : UID_INVALID,
new_gid != st->st_gid ? new_gid : GID_INVALID,
AT_EMPTY_PATH) < 0)
return log_error_errno(errno, "fchownat() of %s failed: %m", path);
}
/* Now, apply the final mode. We do this in two cases: when the user set a mode explicitly, or after a
* chown(), since chown()'s mangle the access mode in regards to sgid/suid in some conditions. */
if (do_chmod || do_chown) {
if (S_ISLNK(st->st_mode))
log_debug("Skipping mode fix for symlink %s.", path);
else {
log_action("Would change", "Changing", "%s \"%s\" to mode %o", path, new_mode);
if (!arg_dry_run) {
r = fchmod_opath(fd, new_mode);
if (r < 0)
return log_error_errno(r, "fchmod() of %s failed: %m", path);
}
}
}
shortcut:
if (arg_dry_run) {
log_debug("Would relabel \"%s\"", path);
return 0;
}
log_debug("Relabelling \"%s\"", path);
return label_fix_full(fd, /* inode_path= */ NULL, /* label_path= */ path, /* flags= */ 0, arg_label_context);
}
static int path_open_parent_safe(const char *path, bool allow_failure) {
_cleanup_free_ char *dn = NULL;
int r, fd;
if (!path_is_normalized(path))
return log_full_errno(allow_failure ? LOG_INFO : LOG_ERR,
SYNTHETIC_ERRNO(EINVAL),
"Failed to open parent of '%s': path not normalized%s.",
path,
allow_failure ? ", ignoring" : "");
r = path_extract_directory(path, &dn);
if (r < 0)
return log_full_errno(allow_failure ? LOG_INFO : LOG_ERR,
r,
"Unable to determine parent directory of '%s'%s: %m",
path,
allow_failure ? ", ignoring" : "");
r = chase(dn, arg_root, allow_failure ? CHASE_SAFE : CHASE_SAFE|CHASE_WARN, NULL, &fd);
if (r == -ENOLINK) /* Unsafe symlink: already covered by CHASE_WARN */
return r;
if (r < 0)
return log_full_errno(allow_failure ? LOG_INFO : LOG_ERR,
r,
"Failed to open path '%s'%s: %m",
dn,
allow_failure ? ", ignoring" : "");
return fd;
}
static int path_open_safe(const char *path) {
int r, fd;
/* path_open_safe() returns a file descriptor opened with O_PATH after
* verifying that the path doesn't contain unsafe transitions, except
* for its final component as the function does not follow symlink. */
assert(path);
if (!path_is_normalized(path))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to open invalid path '%s'.", path);
r = chase(path, arg_root, CHASE_SAFE|CHASE_WARN|CHASE_NOFOLLOW, NULL, &fd);
if (r == -ENOLINK)
return r; /* Unsafe symlink: already covered by CHASE_WARN */
if (r < 0)
return log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_ERR, r,
"Failed to open path %s%s: %m", path,
r == -ENOENT ? ", ignoring" : "");
return fd;
}
static int path_set_perms(
Context *c,
Item *i,
const char *path,
CreationMode creation) {
_cleanup_close_ int fd = -EBADF;
assert(c);
assert(i);
assert(path);
fd = path_open_safe(path);
if (fd == -ENOENT)
return 0;
if (fd < 0)
return fd;
return fd_set_perms(c, i, fd, path, /* st= */ NULL, creation);
}
static int parse_xattrs_from_arg(Item *i) {
const char *p;
int r;
assert(i);
assert_se(p = i->argument);
for (;;) {
_cleanup_free_ char *name = NULL, *value = NULL, *xattr = NULL;
r = extract_first_word(&p, &xattr, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE);
if (r < 0)
log_warning_errno(r, "Failed to parse extended attribute '%s', ignoring: %m", p);
if (r <= 0)
break;
r = split_pair(xattr, "=", &name, &value);
if (r < 0) {
log_warning_errno(r, "Failed to parse extended attribute, ignoring: %s", xattr);
continue;
}
if (isempty(name) || isempty(value)) {
log_warning("Malformed extended attribute found, ignoring: %s", xattr);
continue;
}
if (strv_push_pair(&i->xattrs, name, value) < 0)
return log_oom();
name = value = NULL;
}
return 0;
}
static int fd_set_xattrs(
Context *c,
Item *i,
int fd,
const char *path,
const struct stat *st,
CreationMode creation) {
int r;
assert(c);
assert(i);
assert(fd >= 0);
assert(path);
STRV_FOREACH_PAIR(name, value, i->xattrs) {
log_action("Would set", "Setting",
"%s extended attribute '%s=%s' on %s", *name, *value, path);
if (!arg_dry_run) {
r = xsetxattr(fd, /* path= */ NULL, AT_EMPTY_PATH, *name, *value);
if (r < 0)
return log_error_errno(r, "Failed to set extended attribute %s=%s on '%s': %m",
*name, *value, path);
}
}
return 0;
}
static int path_set_xattrs(
Context *c,
Item *i,
const char *path,
CreationMode creation) {
_cleanup_close_ int fd = -EBADF;
assert(c);
assert(i);
assert(path);
fd = path_open_safe(path);
if (fd == -ENOENT)
return 0;
if (fd < 0)
return fd;
return fd_set_xattrs(c, i, fd, path, /* st= */ NULL, creation);
}
static int parse_acls_from_arg(Item *item) {
#if HAVE_ACL
int r;
assert(item);
/* If append_or_force (= modify) is set, we will not modify the acl
* afterwards, so the mask can be added now if necessary. */
r = parse_acl(item->argument, &item->acl_access, &item->acl_access_exec,
&item->acl_default, !item->append_or_force);
if (r < 0)
log_full_errno(arg_graceful && IN_SET(r, -EINVAL, -ENOENT, -ESRCH) ? LOG_DEBUG : LOG_WARNING,
r, "Failed to parse ACL \"%s\", ignoring: %m", item->argument);
#else
log_warning("ACLs are not supported, ignoring.");
#endif
return 0;
}
#if HAVE_ACL
static int parse_acl_cond_exec(
const char *path,
const struct stat *st,
acl_t cond_exec,
acl_t access, /* could be empty (NULL) */
bool append,
acl_t *ret) {
acl_entry_t entry;
acl_permset_t permset;
bool has_exec;
int r;
assert(path);
assert(st);
assert(cond_exec);
assert(ret);
r = dlopen_libacl(LOG_DEBUG);
if (r < 0)
return r;
if (!S_ISDIR(st->st_mode)) {
_cleanup_(acl_freep) acl_t old = NULL;
old = sym_acl_get_file(path, ACL_TYPE_ACCESS);
if (!old)
return -errno;
has_exec = false;
for (r = sym_acl_get_entry(old, ACL_FIRST_ENTRY, &entry);
r > 0;
r = sym_acl_get_entry(old, ACL_NEXT_ENTRY, &entry)) {
acl_tag_t tag;
if (sym_acl_get_tag_type(entry, &tag) < 0)
return -errno;
if (tag == ACL_MASK)
continue;
/* If not appending, skip ACL definitions */
if (!append && IN_SET(tag, ACL_USER, ACL_GROUP))
continue;
if (sym_acl_get_permset(entry, &permset) < 0)
return -errno;
r = sym_acl_get_perm(permset, ACL_EXECUTE);
if (r < 0)
return -errno;
if (r > 0) {
has_exec = true;
break;
}
}
if (r < 0)
return -errno;
/* Check if we're about to set the execute bit in acl_access */
if (!has_exec && access) {
for (r = sym_acl_get_entry(access, ACL_FIRST_ENTRY, &entry);
r > 0;
r = sym_acl_get_entry(access, ACL_NEXT_ENTRY, &entry)) {
if (sym_acl_get_permset(entry, &permset) < 0)
return -errno;
r = sym_acl_get_perm(permset, ACL_EXECUTE);
if (r < 0)
return -errno;
if (r > 0) {
has_exec = true;
break;
}
}
if (r < 0)
return -errno;
}
} else
has_exec = true;
_cleanup_(acl_freep) acl_t parsed = access ? sym_acl_dup(access) : sym_acl_init(0);
if (!parsed)
return -errno;
for (r = sym_acl_get_entry(cond_exec, ACL_FIRST_ENTRY, &entry);
r > 0;
r = sym_acl_get_entry(cond_exec, ACL_NEXT_ENTRY, &entry)) {
acl_entry_t parsed_entry;
if (sym_acl_create_entry(&parsed, &parsed_entry) < 0)
return -errno;
if (sym_acl_copy_entry(parsed_entry, entry) < 0)
return -errno;
/* We substituted 'X' with 'x' in parse_acl(), so drop execute bit here if not applicable. */
if (!has_exec) {
if (sym_acl_get_permset(parsed_entry, &permset) < 0)
return -errno;
if (sym_acl_delete_perm(permset, ACL_EXECUTE) < 0)
return -errno;
}
}
if (r < 0)
return -errno;
if (!append) { /* want_mask = true */
r = calc_acl_mask_if_needed(&parsed);
if (r < 0)
return r;
}
*ret = TAKE_PTR(parsed);
return 0;
}
static int path_set_acl(
Context *c,
const char *path,
const char *pretty,
acl_type_t type,
acl_t acl,
bool modify) {
_cleanup_(acl_free_charpp) char *t = NULL;
_cleanup_(acl_freep) acl_t dup = NULL;
int r;
assert(c);
r = dlopen_libacl(LOG_DEBUG);
if (r < 0)
return r;
/* Returns 0 for success, positive error if already warned, negative error otherwise. */
if (modify) {
r = acls_for_file(path, type, acl, &dup);
if (r < 0)
return r;
r = calc_acl_mask_if_needed(&dup);
if (r < 0)
return r;
} else {
dup = sym_acl_dup(acl);
if (!dup)
return -errno;
/* the mask was already added earlier if needed */
}
r = add_base_acls_if_needed(&dup, path);
if (r < 0)
return r;
t = sym_acl_to_any_text(dup, NULL, ',', TEXT_ABBREVIATE);
log_action("Would set", "Setting",
"%s %s ACL %s on %s",
type == ACL_TYPE_ACCESS ? "access" : "default",
strna(t), pretty);
if (!arg_dry_run &&
(r = RET_NERRNO(sym_acl_set_file(path, type, dup))) < 0) {
if (ERRNO_IS_NOT_SUPPORTED(r))
/* No error if filesystem doesn't support ACLs. Return negative. */
return r;
if (r == -EINVAL && running_in_chroot() > 0)
return log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
"Setting %s ACL \"%s\" on %s failed. A chroot environment was detected, ignoring.",
type == ACL_TYPE_ACCESS ? "access" : "default",
strna(t), pretty);
/* Return positive to indicate we already warned */
return -log_error_errno(r,
"Setting %s ACL \"%s\" on %s failed: %m",
type == ACL_TYPE_ACCESS ? "access" : "default",
strna(t), pretty);
}
return 0;
}
#endif
static int fd_set_acls(
Context *c,
Item *item,
int fd,
const char *path,
const struct stat *st,
CreationMode creation) {
int r = 0;
#if HAVE_ACL
_cleanup_(acl_freep) acl_t access_with_exec_parsed = NULL;
struct stat stbuf;
assert(c);
assert(item);
assert(fd >= 0);
assert(path);
if (!st) {
if (fstat(fd, &stbuf) < 0)
return log_error_errno(errno, "fstat(%s) failed: %m", path);
st = &stbuf;
}
if (hardlink_vulnerable(st))
return log_error_errno(SYNTHETIC_ERRNO(EPERM),
"Refusing to set ACLs on hardlinked file %s while the fs.protected_hardlinks sysctl is turned off.",
path);
if (!inode_type_can_acl(st->st_mode)) {
log_debug("Skipping ACL fix for '%s' (inode type does not support ACLs).", path);
return 0;
}
if (item->acl_access_exec) {
r = parse_acl_cond_exec(FORMAT_PROC_FD_PATH(fd), st,
item->acl_access_exec,
item->acl_access,
item->append_or_force,
&access_with_exec_parsed);
if (r < 0)
return log_error_errno(r, "Failed to parse conditionalized execute bit for \"%s\": %m", path);
r = path_set_acl(c, FORMAT_PROC_FD_PATH(fd), path, ACL_TYPE_ACCESS, access_with_exec_parsed, item->append_or_force);
} else if (item->acl_access)
r = path_set_acl(c, FORMAT_PROC_FD_PATH(fd), path, ACL_TYPE_ACCESS, item->acl_access, item->append_or_force);
/* set only default acls to folders */
if (r == 0 && item->acl_default && S_ISDIR(st->st_mode))
r = path_set_acl(c, FORMAT_PROC_FD_PATH(fd), path, ACL_TYPE_DEFAULT, item->acl_default, item->append_or_force);
if (ERRNO_IS_NOT_SUPPORTED(r)) {
log_debug_errno(r, "ACLs not supported by file system at %s", path);
return 0;
}
if (r > 0)
return -r; /* already warned in path_set_acl */
if (r < 0)
return log_error_errno(r, "ACL operation on \"%s\" failed: %m", path);
#endif
return r;
}
static int path_set_acls(
Context *c,
Item *item,
const char *path,
CreationMode creation) {
int r = 0;
#if HAVE_ACL
_cleanup_close_ int fd = -EBADF;
assert(c);
assert(item);
assert(path);
fd = path_open_safe(path);
if (fd == -ENOENT)
return 0;
if (fd < 0)
return fd;
r = fd_set_acls(c, item, fd, path, /* st= */ NULL, creation);
#endif
return r;
}
static int capability_vfs_from_string(const char *s, FCapsUpdate *ret) {
FCapsUpdate set = {
.rootuid = UID_INVALID,
};
assert(s);
assert(ret);
for (const char *p = s;;) {
_cleanup_free_ char *word = NULL, *keys = NULL;
char *value, sep;
int r;
r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX);
if (r < 0)
return log_debug_errno(r, "Failed to split words from '%s': %m", p);
if (r == 0)
break;
value = strpbrk(word, "=+-");
if (!value)
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse key/value '%s': %m", word);
keys = strndup(word, value - word);
if (!keys)
return log_oom();
sep = *value;
value++;
if (sep == '=' && streq(keys, "rootuid")) {
r = parse_uid(value, &set.rootuid);
if (r < 0)
return log_debug_errno(r, "Failed to parse rootuid value '%s': %m", value);
} else {
uint64_t caps = 0;
if (!in_charset(value, "eip"))
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse value '%s': %m", value);
if (STR_IN_SET(keys, "all", ""))
caps = all_capabilities();
else
for (const char *remaining_keys = keys; remaining_keys && *remaining_keys;) {
_cleanup_free_ char *key = NULL;
r = extract_first_word(&remaining_keys, &key, ",", /* flags= */0);
if (r < 0)
return log_debug_errno(r, "Failed to parse capability list '%s': %m", keys);
if (r == 0)
break;
if (streq(key, "all"))
caps = all_capabilities();
else {
r = capability_from_name(key);
if (r < 0)
return log_debug_errno(r, "Failed to parse capability '%s': %m", key);
caps |= UINT64_C(1) << r;
}
}
if (sep == '=') {
set.permitted.mask |= caps;
set.inheritable.mask |= caps;
set.effective.mask |= caps;
}
if (IN_SET(sep, '=', '+')) {
if (strchr(value, 'p'))
set.permitted.set |= caps;
if (strchr(value, 'i'))
set.inheritable.set |= caps;
if (strchr(value, 'e'))
set.effective.set |= caps;
} else {
if (strchr(value, 'p')) {
set.permitted.mask |= caps;
set.permitted.set &= ~caps;
}
if (strchr(value, 'i')) {
set.inheritable.mask |= caps;
set.inheritable.set &= ~caps;
}
if (strchr(value, 'e')) {
set.effective.mask |= caps;
set.effective.set &= ~caps;
}
}
}
}
*ret = set;
return 0;
}
static size_t cap_data_size(uint32_t revision) {
switch (revision) {
case VFS_CAP_REVISION_1:
return XATTR_CAPS_SZ_1;
case VFS_CAP_REVISION_2:
return XATTR_CAPS_SZ_2;
case VFS_CAP_REVISION_3:
return XATTR_CAPS_SZ_3;
default:
return SIZE_MAX;
}
}
static bool inode_type_can_fcaps(mode_t mode) {
return S_ISREG(mode);
}
static int apply_fcaps(int fd, const char *path, bool append, const FCapsUpdate *set) {
struct vfs_ns_cap_data val = {
.magic_etc = htole32(VFS_CAP_REVISION),
};
le32_t effective[VFS_CAP_U32] = {};
struct stat st;
int r;
assert(fd >= 0);
assert(path);
assert(set);
if (fstat(fd, &st) < 0)
return log_error_errno(errno, "Failed to fstat(%s): %m", path);
if (hardlink_vulnerable(&st))
return log_error_errno(SYNTHETIC_ERRNO(EPERM),
"Refusing to set file capabilities on hardlinked file %s while the fs.protected_hardlinks sysctl is turned off.",
path);
if (!inode_type_can_fcaps(st.st_mode)) {
log_debug("Skipping file capabilities for '%s' (inode type does not support file capabilities).", path);
return 0;
}
if (append) {
_cleanup_free_ char *xattr_data = NULL;
size_t xattr_data_len;
r = fgetxattr_malloc(fd, "security.capability", &xattr_data, &xattr_data_len);
if (r == -ENODATA)
log_debug("No capabilities found for '%s'", path);
else if (r < 0)
return log_error_errno(r, "Failed to read capabilities of '%s': %m", path);
else {
_cleanup_free_ struct vfs_ns_cap_data *original = NULL;
if (xattr_data_len < endoffsetof_field(struct vfs_ns_cap_data, magic_etc))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Extended attributes for capabilities are too small");
original = realloc0(xattr_data, sizeof(struct vfs_ns_cap_data));
if (!original)
return log_oom();
xattr_data = NULL;
size_t expected_size = cap_data_size(le32toh(original->magic_etc) & VFS_CAP_REVISION_MASK);
if (expected_size == SIZE_MAX)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown type of file capabilities");
if (xattr_data_len != expected_size)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Size of file capabilities does not match its type");
if (FLAGS_SET(le32toh(original->magic_etc), VFS_CAP_FLAGS_EFFECTIVE))
for (size_t n = 0; n < VFS_CAP_U32; n++)
effective[n] = original->data[n].permitted | original->data[n].inheritable;
for (size_t n = 0; n < VFS_CAP_U32; n++) {
val.data[n].permitted = original->data[n].permitted;
val.data[n].inheritable = original->data[n].inheritable;
}
val.rootid = original->rootid;
}
}
for (size_t n = 0; n < VFS_CAP_U32; n++) {
size_t bit_shift = 32*n;
val.data[n].inheritable &= htole32(~(set->inheritable.mask >> bit_shift) & UINT32_C(0xffffffff));
val.data[n].inheritable |= htole32((set->inheritable.set >> bit_shift) & UINT32_C(0xffffffff));
val.data[n].permitted &= htole32(~(set->permitted.mask >> bit_shift) & UINT32_C(0xffffffff));
val.data[n].permitted |= htole32((set->permitted.set >> bit_shift) & UINT32_C(0xffffffff));
effective[n] &= htole32(~(set->effective.mask >> bit_shift) & UINT32_C(0xffffffff));
effective[n] |= htole32((set->effective.set >> bit_shift) & UINT32_C(0xffffffff));
if (effective[n] != 0)
val.magic_etc |= htole32(VFS_CAP_FLAGS_EFFECTIVE);
}
if (FLAGS_SET(le32toh(val.magic_etc), VFS_CAP_FLAGS_EFFECTIVE))
for (size_t n = 0; n < VFS_CAP_U32; n++)
if ((val.data[n].permitted | val.data[n].inheritable) != effective[n])
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Inconsistent effective bits");
if (set->rootuid != UID_INVALID)
val.rootid = htole32(set->rootuid);
log_action("Would try to set", "Trying to set",
"%s capabilities on %s", path);
if (!arg_dry_run) {
r = xsetxattr_full(fd, /* path= */ NULL, AT_EMPTY_PATH, "security.capability", (void*)&val, sizeof(val), /* xattr_flags= */ 0);
if (r < 0)
return log_error_errno(r, "Failed to setcap '%s': %m", path);
}
return 0;
}
static int parse_caps_from_arg(Item *item) {
FCapsUpdate fcaps;
int r;
assert(item);
r = capability_vfs_from_string(item->argument, &fcaps);
if (r < 0) {
log_full_errno(arg_graceful ? LOG_DEBUG : LOG_WARNING,
r, "Failed to parse capabilities \"%s\", ignoring: %m", item->argument);
return 0;
}
item->fcaps_set = true;
item->fcaps = fcaps;
return 0;
}
static int fd_set_caps(
Context *c,
Item *item,
int fd,
const char *path,
const struct stat *st,
CreationMode creation) {
assert(c);
assert(item);
assert(fd >= 0);
assert(path);
if (!item->fcaps_set)
return 0;
return apply_fcaps(fd, path, item->append_or_force, &item->fcaps);
}
static int path_set_caps(
Context *c,
Item *item,
const char *path,
CreationMode creation) {
_cleanup_close_ int fd = -EBADF;
assert(c);
assert(item);
assert(path);
if (!item->fcaps_set)
return 0;
fd = path_open_safe(path);
if (fd == -ENOENT)
return 0;
if (fd < 0)
return fd;
return apply_fcaps(fd, path, item->append_or_force, &item->fcaps);
}
static int parse_attribute_from_arg(Item *item) {
static const struct {
char character;
unsigned value;
} attributes[] = {
{ 'A', FS_NOATIME_FL }, /* do not update atime */
{ 'S', FS_SYNC_FL }, /* Synchronous updates */
{ 'D', FS_DIRSYNC_FL }, /* dirsync behaviour (directories only) */
{ 'a', FS_APPEND_FL }, /* writes to file may only append */
{ 'c', FS_COMPR_FL }, /* Compress file */
{ 'd', FS_NODUMP_FL }, /* do not dump file */
{ 'e', FS_EXTENT_FL }, /* Extents */
{ 'i', FS_IMMUTABLE_FL }, /* Immutable file */
{ 'j', FS_JOURNAL_DATA_FL }, /* Reserved for ext3 */
{ 's', FS_SECRM_FL }, /* Secure deletion */
{ 'u', FS_UNRM_FL }, /* Undelete */
{ 't', FS_NOTAIL_FL }, /* file tail should not be merged */
{ 'T', FS_TOPDIR_FL }, /* Top of directory hierarchies */
{ 'C', FS_NOCOW_FL }, /* Do not cow file */
{ 'P', FS_PROJINHERIT_FL }, /* Inherit the quota project ID */
};
enum {
MODE_ADD,
MODE_DEL,
MODE_SET
} mode = MODE_ADD;
unsigned value = 0, mask = 0;
const char *p;
assert(item);
p = item->argument;
if (p) {
if (*p == '+') {
mode = MODE_ADD;
p++;
} else if (*p == '-') {
mode = MODE_DEL;
p++;
} else if (*p == '=') {
mode = MODE_SET;
p++;
}
}
if (isempty(p) && mode != MODE_SET)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Setting file attribute on '%s' needs an attribute specification.",
item->path);
for (; p && *p ; p++) {
unsigned i, v;
for (i = 0; i < ELEMENTSOF(attributes); i++)
if (*p == attributes[i].character)
break;
if (i >= ELEMENTSOF(attributes))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Unknown file attribute '%c' on '%s'.", *p, item->path);
v = attributes[i].value;
SET_FLAG(value, v, IN_SET(mode, MODE_ADD, MODE_SET));
mask |= v;
}
if (mode == MODE_SET)
mask |= CHATTR_ALL_FL;
assert(mask != 0);
item->attribute_mask = mask;
item->attribute_value = value;
item->attribute_set = true;
return 0;
}
static int fd_set_attribute(
Context *c,
Item *item,
int fd,
const char *path,
const struct stat *st,
CreationMode creation) {
struct stat stbuf;
unsigned f;
int r;
assert(c);
assert(item);
assert(fd >= 0);
assert(path);
if (!item->attribute_set || item->attribute_mask == 0)
return 0;
if (!st) {
if (fstat(fd, &stbuf) < 0)
return log_error_errno(errno, "fstat(%s) failed: %m", path);
st = &stbuf;
}
/* Issuing the file attribute ioctls on device nodes is not safe, as that will be delivered to the
* drivers, not the file system containing the device node. */
if (!S_ISREG(st->st_mode) && !S_ISDIR(st->st_mode))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Setting file flags is only supported on regular files and directories, cannot set on '%s'.",
path);
f = item->attribute_value & item->attribute_mask;
/* Mask away directory-specific flags */
if (!S_ISDIR(st->st_mode))
f &= ~FS_DIRSYNC_FL;
log_action("Would try to set", "Trying to set",
"%s file attributes 0x%08x on %s",
f & item->attribute_mask,
path);
if (!arg_dry_run) {
_cleanup_close_ int procfs_fd = -EBADF;
procfs_fd = fd_reopen(fd, O_RDONLY|O_CLOEXEC|O_NOATIME);
if (procfs_fd < 0)
return log_error_errno(procfs_fd, "Failed to reopen '%s': %m", path);
unsigned previous, current;
r = chattr_full(procfs_fd, NULL, f, item->attribute_mask, &previous, ¤t, CHATTR_FALLBACK_BITWISE);
if (r == -ENOANO)
log_warning("Cannot set file attributes for '%s', maybe due to incompatibility in specified attributes, "
"previous=0x%08x, current=0x%08x, expected=0x%08x, ignoring.",
path, previous, current, (previous & ~item->attribute_mask) | (f & item->attribute_mask));
else if (r < 0)
log_full_errno(ERRNO_IS_IOCTL_NOT_SUPPORTED(r) ? LOG_DEBUG : LOG_WARNING, r,
"Cannot set file attributes for '%s', value=0x%08x, mask=0x%08x, ignoring: %m",
path, item->attribute_value, item->attribute_mask);
}
return 0;
}
static int path_set_attribute(
Context *c,
Item *item,
const char *path,
CreationMode creation) {
_cleanup_close_ int fd = -EBADF;
assert(c);
assert(item);
if (!item->attribute_set || item->attribute_mask == 0)
return 0;
fd = path_open_safe(path);
if (fd == -ENOENT)
return 0;
if (fd < 0)
return fd;
return fd_set_attribute(c, item, fd, path, /* st= */ NULL, creation);
}
static int write_argument_data(Item *i, int fd, const char *path) {
int r;
assert(i);
assert(fd >= 0);
assert(path);
if (item_binary_argument_size(i) == 0)
return 0;
assert(item_binary_argument(i));
log_action("Would write", "Writing", "%s to \"%s\"", path);
if (!arg_dry_run) {
r = loop_write(fd, item_binary_argument(i), item_binary_argument_size(i));
if (r < 0)
return log_error_errno(r, "Failed to write file \"%s\": %m", path);
}
return 0;
}
static int write_one_file(Context *c, Item *i, const char *path, CreationMode creation) {
_cleanup_close_ int fd = -EBADF, dir_fd = -EBADF;
_cleanup_free_ char *bn = NULL;
int r;
assert(c);
assert(i);