X-Git-Url: http://gitweb.michael.orlitzky.com/?p=apply-default-acl.git;a=blobdiff_plain;f=src%2Flibadacl.c;h=a21aa709ba842bc7bd21aa672b99b4e3cb750cf0;hp=36812e8af3dd18a4dbfd96f7c7b769865d90298d;hb=HEAD;hpb=ee4b602d26225bb5a548eaeb7bd570adaa48721a diff --git a/src/libadacl.c b/src/libadacl.c index 36812e8..a21aa70 100644 --- a/src/libadacl.c +++ b/src/libadacl.c @@ -5,15 +5,18 @@ * */ -/* Enables get_current_dir_name() in unistd.h and the O_PATH flag. */ +/* Enables get_current_dir_name() in unistd.h, the O_PATH flag, and + * the asprintf() function. +*/ #define _GNU_SOURCE +#include /* readdir(), etc. */ #include /* EINVAL, ELOOP, ENOTDIR, etc. */ #include /* openat() */ #include /* basename(), dirname() */ #include /* PATH_MAX */ #include /* the "bool" type */ -#include /* perror(), snprintf() */ +#include /* perror(), asprintf() */ #include /* free() */ #include /* strdup() */ #include /* fstat() */ @@ -35,11 +38,29 @@ */ #define CLOSE_ERROR -1 #define OPEN_ERROR -1 -#define SNPRINTF_ERROR -1 +#define ASPRINTF_ERROR -1 #define STAT_ERROR -1 #define XATTR_ERROR -1 +/* Prototypes */ +int safe_open_ex(int at_fd, char* pathname, int flags); +int safe_open(const char* pathname, int flags); +int acl_update_entry(acl_t aclp, acl_entry_t updated_entry); +int acl_entry_count(acl_t acl); +int acl_is_minimal(acl_t acl); +int acl_execute_masked(acl_t acl); +int any_can_execute(int fd, const struct stat* sp); +int acl_copy_xattr(int src_fd, + acl_type_t src_type, + int dst_fd, + acl_type_t dst_type); +int has_default_acl_fd(int fd); +int apply_default_acl_fds(int parent_fd, int fd, bool recursive); +int apply_default_acl(const char* path, bool recursive); + + + /** * @brief The recursive portion of the @c safe_open function, used to * open a file descriptor in a symlink-safe way when combined with @@ -133,11 +154,11 @@ int safe_open(const char* pathname, int flags) { return OPEN_ERROR; } - char abspath[PATH_MAX]; - int snprintf_result = 0; + char* abspath = NULL; + int asprintf_result = 0; if (strchr(pathname, '/') == pathname) { /* pathname is already absolute; just copy it. */ - snprintf_result = snprintf(abspath, PATH_MAX, "%s", pathname); + asprintf_result = asprintf(&abspath, "%s", pathname); } else { /* Concatenate the current working directory and pathname into an @@ -162,14 +183,17 @@ int safe_open(const char* pathname, int flags) { free(cwd); return OPEN_ERROR; } - snprintf_result = snprintf(abspath, PATH_MAX, "%s/%s", abs_cwd, pathname); + asprintf_result = asprintf(&abspath, "%s/%s", abs_cwd, pathname); free(cwd); } - if (snprintf_result == SNPRINTF_ERROR || snprintf_result > PATH_MAX) { - perror("safe_open (snprintf)"); + if (asprintf_result == ASPRINTF_ERROR) { + perror("safe_open (asprintf)"); return OPEN_ERROR; } + /* Beyond here, asprintf() worked, and we need to free abspath. */ + int result = OPEN_ERROR; + bool abspath_is_root = (strcmp(abspath, "/") == 0); int rootflags = flags | O_DIRECTORY; if (!abspath_is_root) { @@ -179,18 +203,24 @@ int safe_open(const char* pathname, int flags) { int rootfd = open("/", rootflags); if (rootfd == OPEN_ERROR) { perror("safe_open (open)"); - return OPEN_ERROR; + result = OPEN_ERROR; + goto cleanup; } if (abspath_is_root) { - return rootfd; + result = rootfd; + goto cleanup; } - int result = safe_open_ex(rootfd, abspath+1, flags); + result = safe_open_ex(rootfd, abspath+1, flags); if (close(rootfd) == CLOSE_ERROR) { perror("safe_open (close)"); - return OPEN_ERROR; + result = OPEN_ERROR; + goto cleanup; } + + cleanup: + free(abspath); return result; } @@ -203,66 +233,131 @@ int safe_open(const char* pathname, int flags) { * @param aclp * A pointer to the acl_t structure whose entry we want to update. * - * @param entry - * The new entry. + * @param updated_entry + * An updated copy of an existing entry in @c aclp. * * @return * - @c ACL_SUCCESS - If we update an existing entry. * - @c ACL_FAILURE - If we don't find an entry to update. * - @c ACL_ERROR - Unexpected library error. */ -int acl_update_entry(acl_t aclp, acl_entry_t entry) { - if (aclp == NULL || entry == NULL) { +int acl_update_entry(acl_t aclp, acl_entry_t updated_entry) { + if (aclp == NULL || updated_entry == NULL) { errno = EINVAL; perror("acl_update_entry (args)"); return ACL_ERROR; } - acl_tag_t entry_tag; - if (acl_get_tag_type(entry, &entry_tag) == ACL_ERROR) { + acl_tag_t updated_tag; + if (acl_get_tag_type(updated_entry, &updated_tag) == ACL_ERROR) { perror("acl_update_entry (acl_get_tag_type)"); return ACL_ERROR; } - acl_permset_t entry_permset; - if (acl_get_permset(entry, &entry_permset) == ACL_ERROR) { + acl_permset_t updated_permset; + if (acl_get_permset(updated_entry, &updated_permset) == ACL_ERROR) { perror("acl_update_entry (acl_get_permset)"); return ACL_ERROR; } + /* This can allocate memory, so from here on out we have to jump to + the "cleanup" label to exit. */ + void* updated_qualifier = acl_get_qualifier(updated_entry); + if (updated_qualifier == NULL && + (updated_tag == ACL_USER || updated_tag == ACL_GROUP)) { + /* acl_get_qualifier() can return NULL, but it shouldn't for + ACL_USER or ACL_GROUP entries. */ + perror("acl_update_entry (acl_get_qualifier)"); + return ACL_ERROR; + } + + /* Our return value. Default to failure, and change to success if we + actually update something. */ + int result = ACL_FAILURE; + acl_entry_t existing_entry; /* Loop through the given ACL looking for matching entries. */ - int result = acl_get_entry(aclp, ACL_FIRST_ENTRY, &existing_entry); + int get_entry_result = acl_get_entry(aclp, ACL_FIRST_ENTRY, &existing_entry); - while (result == ACL_SUCCESS) { + while (get_entry_result == ACL_SUCCESS) { acl_tag_t existing_tag = ACL_UNDEFINED_TAG; if (acl_get_tag_type(existing_entry, &existing_tag) == ACL_ERROR) { perror("set_acl_tag_permset (acl_get_tag_type)"); - return ACL_ERROR; + result = ACL_ERROR; + goto cleanup; } - if (existing_tag == entry_tag) { - /* If we update something, we're done and return ACL_SUCCESS */ - if (acl_set_permset(existing_entry, entry_permset) == ACL_ERROR) { - perror("acl_update_entry (acl_set_permset)"); - return ACL_ERROR; + if (existing_tag == updated_tag) { + /* Our tag types match, but if we have a named user or group + entry, then we need to check that the user/group (that is, + the qualifier) matches too. */ + bool qualifiers_match = false; + + /* There are three ways the qualifiers can match... */ + void* existing_qualifier = acl_get_qualifier(existing_entry); + if (existing_qualifier == NULL) { + if (existing_tag == ACL_USER || existing_tag == ACL_GROUP) { + perror("acl_update_entry (acl_get_qualifier)"); + result = ACL_ERROR; + goto cleanup; + } + else { + /* First, we could be dealing with an entry that isn't a + named user or group, in which case they "match + vacuously." */ + qualifiers_match = true; + } + } + + /* Second, they could have matching UIDs. We don't really need to + check both tags here, since we know that they're equal. However, + clang-tidy can't figure that out, and the redundant equality + check prevents it from complaining about a potential null pointer + dereference. */ + if (updated_tag == ACL_USER && existing_tag == ACL_USER) { + qualifiers_match = ( *((uid_t*)existing_qualifier) + == + *((uid_t*)updated_qualifier) ); + } + + /* Third, they could have matching GIDs. See above for why + we check the redundant condition existing_tag == ACL_GROUP. */ + if (updated_tag == ACL_GROUP && existing_tag == ACL_GROUP) { + qualifiers_match = ( *((gid_t*)existing_qualifier) + == + *((gid_t*)updated_qualifier) ); } - return ACL_SUCCESS; + /* Be sure to free this inside the loop, where memory is allocated. */ + acl_free(existing_qualifier); + + if (qualifiers_match) { + /* If we update something, we're done and return ACL_SUCCESS */ + if (acl_set_permset(existing_entry, updated_permset) == ACL_ERROR) { + perror("acl_update_entry (acl_set_permset)"); + result = ACL_ERROR; + goto cleanup; + } + + result = ACL_SUCCESS; + goto cleanup; + } } - result = acl_get_entry(aclp, ACL_NEXT_ENTRY, &existing_entry); + get_entry_result = acl_get_entry(aclp, ACL_NEXT_ENTRY, &existing_entry); } /* This catches both the initial acl_get_entry and the ones at the end of the loop. */ - if (result == ACL_ERROR) { + if (get_entry_result == ACL_ERROR) { perror("acl_update_entry (acl_get_entry)"); - return ACL_ERROR; + result = ACL_ERROR; } - return ACL_FAILURE; + cleanup: + acl_free(updated_qualifier); + return result; } @@ -578,8 +673,14 @@ int acl_copy_xattr(int src_fd, return ACL_ERROR; } char* src_acl_p = alloca(src_size_guess); - /* The actual size may be smaller than our guess? I don't know. */ - ssize_t src_size = fgetxattr(src_fd, src_name, src_acl_p, src_size_guess); + /* The actual size may be smaller than our guess? I don't know. The + return value from fgetxattr() will either be nonnegative, or + XATTR_ERROR (which we've already ruled out), so it's safe to cast + it to an unsigned size_t here to avoid a compiler warning. */ + ssize_t src_size = fgetxattr(src_fd, + src_name, + src_acl_p, + (size_t)src_size_guess); if (src_size == XATTR_ERROR) { if (errno == ENODATA) { /* A missing ACL isn't an error. */ @@ -589,7 +690,14 @@ int acl_copy_xattr(int src_fd, return ACL_ERROR; } - if (fsetxattr(dst_fd, dst_name, src_acl_p, src_size, 0) == XATTR_ERROR) { + /* See above: src_size must be nonnegative at this point,so we cast + it to size_t to avoid a compiler warning. */ + if (fsetxattr(dst_fd, + dst_name, + src_acl_p, + (size_t)src_size, + 0) + == XATTR_ERROR) { perror("acl_copy_xattr (fsetxattr)"); return ACL_ERROR; } @@ -622,41 +730,36 @@ int has_default_acl_fd(int fd) { } + /** - * @brief Apply parent default ACL to a path. + * @brief The recursive portion of @c apply_default_acl. * - * This overwrites any existing ACLs on @c path. + * The @c apply_default_acl function takes a path, but then opens file + * descriptors for the path and its parent. Afterwards, everything is + * done using file descriptors, including the recursive application on + * the path's children. This function encapsulates the portion of @c + * apply_default_acl that uses only file descriptors; for the + * recursion, this function ultimately calls itself. * - * @param path - * The path whose ACL we would like to reset to its default. + * This overwrites any existing ACLs on @c fd and, if @c recursive is + * @c true, its children. When @c recursive is @c true, the "worst" + * result encountered is returned as the overall result. * - * @param sp - * A pointer to a stat structure for @c path, or @c NULL if you don't - * have one handy. + * @param parent_fd + * A file descriptor for the parent directory of @c fd. + * + * @param fd + * The file descriptor that should inherit its parent's default ACL. * - * @param no_exec_mask - * The value (either true or false) of the --no-exec-mask flag. + * @param recursive + * Should we recurse into subdirectories? * * @return - * - @c ACL_SUCCESS - The parent default ACL was inherited successfully. + * - @c ACL_SUCCESS - The parent default ACLs were inherited successfully. * - @c ACL_FAILURE - If symlinks or hard links are encountered. * - @c ACL_ERROR - Unexpected library error. */ -int apply_default_acl_ex(const char* path, - const struct stat* sp, - bool no_exec_mask) { - - if (path == NULL) { - errno = EINVAL; - perror("apply_default_acl_ex (args)"); - return ACL_ERROR; - } - - /* Define these next three variables here because we may have to - * jump to the cleanup routine which expects them to exist. - */ - - /* Our return value. */ +int apply_default_acl_fds(int parent_fd, int fd, bool recursive) { int result = ACL_SUCCESS; /* The new ACL for this path */ @@ -666,113 +769,68 @@ int apply_default_acl_ex(const char* path, order to mask the execute bit. */ acl_t new_acl_unmasked = (acl_t)NULL; - /* The file descriptor corresponding to "path" */ - int fd = 0; - - /* The file descriptor for the directory containing "path" */ - int parent_fd = 0; - - /* Get the parent directory of "path" with dirname(), which happens - * to murder its argument and necessitates a path_copy. */ - char* path_copy = strdup(path); - if (path_copy == NULL) { - perror("apply_default_acl_ex (strdup)"); - return ACL_ERROR; - } - char* parent = dirname(path_copy); - parent_fd = safe_open(parent, O_DIRECTORY | O_NOFOLLOW); - if (parent_fd == OPEN_ERROR) { - if (errno == ELOOP || errno == ENOTDIR) { - /* We hit a symlink, either in the last path component (ELOOP) - or higher up (ENOTDIR). */ - result = ACL_FAILURE; - goto cleanup; - } - else { - perror("apply_default_acl_ex (open parent fd)"); - result = ACL_ERROR; - goto cleanup; - } - } - - /* Check to make sure the parent descriptor actually has a default - ACL. If it doesn't, then we can "succeed" immediately. */ - if (has_default_acl_fd(parent_fd) == ACL_FAILURE) { - result = ACL_SUCCESS; - goto cleanup; - } - - fd = safe_open(path, O_NOFOLLOW); - if (fd == OPEN_ERROR) { - if (errno == ELOOP || errno == ENOTDIR) { - /* We hit a symlink, either in the last path component (ELOOP) - or higher up (ENOTDIR). */ - result = ACL_FAILURE; - goto cleanup; - } - else { - perror("apply_default_acl_ex (open fd)"); - result = ACL_ERROR; - goto cleanup; - } - } - /* Refuse to operate on hard links, which can be abused by an * attacker to trick us into changing the ACL on a file we didn't * intend to; namely the "target" of the hard link. There is TOCTOU * race condition here, but the window is as small as possible * between when we open the file descriptor (look above) and when we * fstat it. - * - * Note: we only need to call fstat ourselves if we weren't passed a - * valid pointer to a stat structure (nftw does that). */ - if (sp == NULL) { - struct stat s; - if (fstat(fd, &s) == STAT_ERROR) { - perror("apply_default_acl_ex (fstat)"); - goto cleanup; - } + struct stat s; + if (fstat(fd, &s) == STAT_ERROR) { + perror("apply_default_acl_fds (fstat)"); + /* We can't recurse without the stat struct for fd */ + goto cleanup; + } - sp = &s; + + /* Check to make sure the parent descriptor actually has a default + ACL. If it doesn't, then we can "succeed" immediately, saving a + little work, particularly in any_can_execute(). Note that we + can't skip the fstat() above, because we need it in case we + recurse. */ + if (has_default_acl_fd(parent_fd) == ACL_FAILURE) { + result = ACL_SUCCESS; + /* Just because this target can't inherit anything doesn't mean + that one of it's children can't. For example, if there's a + default on "c" in "a/b/c/d", then we don't want to skip all + children of "a"! */ + goto recurse; } - if (!S_ISDIR(sp->st_mode)) { + + if (!S_ISDIR(s.st_mode)) { /* If it's not a directory, make sure it's a regular, non-hard-linked file. */ - if (!S_ISREG(sp->st_mode) || sp->st_nlink != 1) { + if (!S_ISREG(s.st_mode) || s.st_nlink != 1) { result = ACL_FAILURE; - goto cleanup; + goto cleanup; /* It's not a directory, so we can skip the recursion. */ } } - /* Default to not masking the exec bit; i.e. applying the default - ACL literally. If --no-exec-mask was not specified, then we try - to "guess" whether or not to mask the exec bit. This behavior - is modeled after the capital 'X' perms of setfacl. */ - bool allow_exec = true; + /* Next We try to guess whether or not to strip the execute bits. + * This behavior is modeled after the capital 'X' perms of setfacl. + */ + int ace_result = any_can_execute(fd, &s); - if (!no_exec_mask) { - /* Never mask the execute bit on directories. */ - int ace_result = any_can_execute(fd,sp) || S_ISDIR(sp->st_mode); + if (ace_result == ACL_ERROR) { + perror("apply_default_acl_fds (any_can_execute)"); + result = ACL_ERROR; + goto cleanup; + } - if (ace_result == ACL_ERROR) { - perror("apply_default_acl_ex (any_can_execute)"); - result = ACL_ERROR; - goto cleanup; - } + /* Never mask the execute bit on directories. */ + bool allow_exec = (bool)ace_result || S_ISDIR(s.st_mode); - allow_exec = (bool)ace_result; - } /* If it's a directory, inherit the parent's default. */ - if (S_ISDIR(sp->st_mode)) { + if (S_ISDIR(s.st_mode)) { if (acl_copy_xattr(parent_fd, ACL_TYPE_DEFAULT, fd, ACL_TYPE_DEFAULT) == ACL_ERROR) { - perror("apply_default_acl_ex (acl_copy_xattr default)"); + perror("apply_default_acl_fds (acl_copy_xattr default)"); result = ACL_ERROR; goto cleanup; } @@ -783,13 +841,13 @@ int apply_default_acl_ex(const char* path, ACL_TYPE_DEFAULT, fd, ACL_TYPE_ACCESS) == ACL_ERROR) { - perror("apply_default_acl_ex (acl_copy_xattr access)"); + perror("apply_default_acl_fds (acl_copy_xattr access)"); result = ACL_ERROR; goto cleanup; } /* There's a good reason why we saved the ACL above, even though - * we're about tto read it back into memory and mess with it on the + * we're about to read it back into memory and mess with it on the * next line. The acl_copy_xattr() function is already a hack to let * us copy default ACLs without resorting to path names; we simply * have no way to read the parent's default ACL into memory using @@ -800,16 +858,17 @@ int apply_default_acl_ex(const char* path, */ /* Now we potentially need to mask the execute permissions in the - ACL on fd; or maybe now. */ + ACL on fd; or maybe not. */ if (allow_exec) { - goto cleanup; + /* Skip the mask code for this target, but don't skip its children! */ + goto recurse; } /* OK, we need to mask some execute permissions. First obtain the current ACL... */ new_acl = acl_get_fd(fd); if (new_acl == (acl_t)NULL) { - perror("apply_default_acl_ex (acl_get_fd)"); + perror("apply_default_acl_fds (acl_get_fd)"); result = ACL_ERROR; goto cleanup; } @@ -819,7 +878,7 @@ int apply_default_acl_ex(const char* path, looping over it no worky). */ new_acl_unmasked = acl_dup(new_acl); if (new_acl_unmasked == (acl_t)NULL) { - perror("apply_default_acl_ex (acl_dup)"); + perror("apply_default_acl_fds (acl_dup)"); result = ACL_ERROR; goto cleanup; } @@ -831,7 +890,7 @@ int apply_default_acl_ex(const char* path, acl_tag_t tag = ACL_UNDEFINED_TAG; if (acl_get_tag_type(entry, &tag) == ACL_ERROR) { - perror("apply_default_acl_ex (acl_get_tag_type)"); + perror("apply_default_acl_fds (acl_get_tag_type)"); result = ACL_ERROR; goto cleanup; } @@ -840,11 +899,13 @@ int apply_default_acl_ex(const char* path, /* We've got an entry/tag from the default ACL. Get its permset. */ acl_permset_t permset; if (acl_get_permset(entry, &permset) == ACL_ERROR) { - perror("apply_default_acl_ex (acl_get_permset)"); + perror("apply_default_acl_fds (acl_get_permset)"); result = ACL_ERROR; goto cleanup; } + /* To mimic what the kernel does, I think we could drop + ACL_GROUP_OBJ from the list below? */ if (tag == ACL_MASK || tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ || @@ -854,20 +915,20 @@ int apply_default_acl_ex(const char* path, minimal ACLs) or acl_other entries, so if execute should be masked, we have to do it manually. */ if (acl_delete_perm(permset, ACL_EXECUTE) == ACL_ERROR) { - perror("apply_default_acl_ex (acl_delete_perm)"); + perror("apply_default_acl_fds (acl_delete_perm)"); result = ACL_ERROR; goto cleanup; } if (acl_set_permset(entry, permset) == ACL_ERROR) { - perror("apply_default_acl_ex (acl_set_permset)"); + perror("apply_default_acl_fds (acl_set_permset)"); result = ACL_ERROR; goto cleanup; } } if (acl_update_entry(new_acl, entry) == ACL_ERROR) { - perror("apply_default_acl_ex (acl_update_entry)"); + perror("apply_default_acl_fds (acl_update_entry)"); result = ACL_ERROR; goto cleanup; } @@ -878,60 +939,216 @@ int apply_default_acl_ex(const char* path, /* Catches the first acl_get_entry as well as the ones at the end of the loop. */ if (ge_result == ACL_ERROR) { - perror("apply_default_acl_ex (acl_get_entry)"); + perror("apply_default_acl_fds (acl_get_entry)"); result = ACL_ERROR; goto cleanup; } if (acl_set_fd(fd, new_acl) == ACL_ERROR) { - perror("apply_default_acl_ex (acl_set_fd)"); + perror("apply_default_acl_fds (acl_set_fd)"); result = ACL_ERROR; goto cleanup; } - cleanup: - free(path_copy); - if (new_acl != (acl_t)NULL) { - acl_free(new_acl); - } - if (new_acl_unmasked != (acl_t)NULL) { - acl_free(new_acl_unmasked); - } - if (fd > 0 && close(fd) == CLOSE_ERROR) { - perror("apply_default_acl_ex (close fd)"); - result = ACL_ERROR; - } - if (parent_fd > 0 && close(parent_fd) == CLOSE_ERROR) { - perror("apply_default_acl_ex (close parent_fd)"); - result = ACL_ERROR; + recurse: + if (recursive && S_ISDIR(s.st_mode)) { + /* Recurse into subdirectories. Don't call closedir() on d! It + closes the open file descriptor as well, and subsequent calls + to close() then throw errors. */ + DIR* d = fdopendir(fd); + if (d == NULL) { + perror("apply_default_acl_fds (fdopendir)"); + result = ACL_ERROR; + goto cleanup; + } + + struct dirent* de; + int new_fd = 0; + while ((de = readdir(d)) != NULL) { + if (de->d_type != DT_DIR && de->d_type != DT_REG) { + /* Hit a symlink or whatever. */ + result = ACL_FAILURE; + continue; + } + if (strcmp(de->d_name, ".") == 0) { continue; } + if (strcmp(de->d_name, "..") == 0) { continue; } + + /* Be careful not to "return" out of this loop and leave the + new_fd open! */ + new_fd = openat(fd, de->d_name, O_NOFOLLOW); + if (new_fd == OPEN_ERROR) { + if (errno == ELOOP || errno == ENOTDIR) { + /* We hit a symlink, either in the last path component (ELOOP) + or higher up (ENOTDIR). */ + if (result == ACL_SUCCESS) { + /* Don't overwrite an error result with success/failure. */ + result = ACL_FAILURE; + } + continue; + } + else { + perror("apply_default_acl_fds (openat)"); + result = ACL_ERROR; + continue; + } + } + switch (apply_default_acl_fds(fd, new_fd, recursive)) { + /* Don't overwrite an error result with success/failure. */ + case ACL_FAILURE: + if (result == ACL_SUCCESS) { + result = ACL_FAILURE; + } + break; + case ACL_ERROR: + result = ACL_ERROR; + default: + if (close(new_fd) == CLOSE_ERROR) { + perror("apply_default_acl_fds (close)"); + result = ACL_ERROR; + } + } + } } + + cleanup: + acl_free(new_acl); + acl_free(new_acl_unmasked); return result; } - /** - * @brief The friendly interface to @c apply_default_acl_ex. - * - * The @c apply_default_acl_ex function holds the real implementation - * of this function, but it takes a weird second argument that most - * people won't care about (a stat structure). But, we use that - * argument for the recursive mode of the CLI, so it's there. + * @brief Apply parent default ACL to a path and optionally its children. * - * If you don't have a stat structure for your @c path, use this instead. + * This overwrites any existing ACLs on the target, and, if @c + * recursive is @c true, its children. When @c recursive is @c true, + * the "worst" result encountered is returned as the overall result. * * @param path * The path whose ACL we would like to reset to its default. * - * @param no_exec_mask - * The value (either true or false) of the --no-exec-mask flag. + * @param recursive + * Should we recurse into subdirectories? * * @return - * - @c ACL_SUCCESS - The parent default ACL was inherited successfully. + * - @c ACL_SUCCESS - The parent default ACLs were inherited successfully. * - @c ACL_FAILURE - If symlinks or hard links are encountered. - * or the parent of @c path is not a directory. * - @c ACL_ERROR - Unexpected library error. */ -int apply_default_acl(const char* path, bool no_exec_mask) { - return apply_default_acl_ex(path, NULL, no_exec_mask); +int apply_default_acl(const char* path, bool recursive) { + + if (path == NULL) { + errno = EINVAL; + perror("apply_default_acl (args)"); + return ACL_ERROR; + } + + /* Define these next three variables here because we may have to + * jump to the cleanup routine which expects them to exist. + */ + + /* Our return value. */ + int result = ACL_SUCCESS; + + /* The file descriptor corresponding to "path" */ + int fd = 0; + + /* The file descriptor for the directory containing "path" */ + int parent_fd = 0; + + /* dirname() and basename() mangle their arguments, so we need + to make copies of "path" before using them. */ + char* dirname_path_copy = NULL; + char* basename_path_copy = NULL; + + /* Get the parent directory of "path" with dirname(), which happens + * to murder its argument and necessitates a path_copy. */ + dirname_path_copy = strdup(path); + if (dirname_path_copy == NULL) { + perror("apply_default_acl (strdup)"); + return ACL_ERROR; + } + char* parent = dirname(dirname_path_copy); + + basename_path_copy = strdup(path); + if (basename_path_copy == NULL) { + perror("apply_default_acl (strdup)"); + result = ACL_ERROR; + goto cleanup; + } + char* child = basename(basename_path_copy); + + /* Just kidding, if the path is "." or "..", then dirname will do + * the wrong thing and give us "." as its parent, too. So, we handle + * those as special cases. We use "child" instead of "path" here to + * catch things like "./" and "../" + */ + bool path_is_dots = strcmp(child, ".") == 0 || strcmp(child, "..") == 0; + char dots_parent[6] = "../"; + if (path_is_dots) { + /* We know that "child" contains no more than two characters here, and + using strncat to enforce that belief keeps clang-tidy happy. */ + parent = strncat(dots_parent, child, 2); + } + + parent_fd = safe_open(parent, O_DIRECTORY | O_NOFOLLOW); + + if (parent_fd == OPEN_ERROR) { + if (errno == ELOOP || errno == ENOTDIR) { + /* We hit a symlink, either in the last path component (ELOOP) + or higher up (ENOTDIR). */ + result = ACL_FAILURE; + goto cleanup; + } + else { + perror("apply_default_acl (open parent fd)"); + result = ACL_ERROR; + goto cleanup; + } + } + + /* We already obtained the parent fd safely, so if we use the + * basename of path here instead of the full thing, then we can get + * away with using openat() and spare ourselves the slowness of + * another safe_open(). + * + * Note that if the basename is "." or "..", then we don't want to + * open it relative to the parent_fd, so we need another special + * case for those paths here. + */ + if (path_is_dots) { + fd = open(child, O_NOFOLLOW); + } + else { + fd = openat(parent_fd, child, O_NOFOLLOW); + } + if (fd == OPEN_ERROR) { + if (errno == ELOOP || errno == ENOTDIR) { + /* We hit a symlink, either in the last path component (ELOOP) + or higher up (ENOTDIR). */ + result = ACL_FAILURE; + goto cleanup; + } + else { + perror("apply_default_acl (open fd)"); + result = ACL_ERROR; + goto cleanup; + } + } + + result = apply_default_acl_fds(parent_fd, fd, recursive); + + cleanup: + free(dirname_path_copy); + free(basename_path_copy); + + if (parent_fd > 0 && close(parent_fd) == CLOSE_ERROR) { + perror("apply_default_acl (close parent_fd)"); + result = ACL_ERROR; + } + if (fd > 0 && close(fd) == CLOSE_ERROR) { + perror("apply_default_acl (close fd)"); + result = ACL_ERROR; + } + return result; }