-int has_type_tag_acl(const char* path,
- acl_type_t type,
- acl_tag_t desired_tag) {
- /* Returns one if the given path has a default ACL for the supplied
- tag, zero if it doesn't, and -1 on error. */
- acl_t defacl = acl_get_file(path, type);
-
- if (defacl == (acl_t)NULL) {
- return 0;
- }
-
- acl_entry_t entry;
- int result = acl_get_entry(defacl, ACL_FIRST_ENTRY, &entry);
-
- while (result == 1) {
- acl_tag_t tag = ACL_UNDEFINED_TAG;
- int tag_result = acl_get_tag_type(entry, &tag);
-
- if (tag_result == -1) {
- perror("has_default_tag_acl (acl_get_tag_type)");
- return -1;
- }
- else {
- if (tag == desired_tag) {
- return 1;
- }
- }
-
- result = acl_get_entry(defacl, ACL_NEXT_ENTRY, &entry);
- }
-
- if (result == -1) {
- perror("has_default_tag_acl (acl_get_entry)");
- return -1;
- }
-
- return 0;
-}
-
-int has_default_tag_acl(const char* path, acl_tag_t desired_tag) {
- return has_type_tag_acl(path, ACL_TYPE_DEFAULT, desired_tag);
-}
-
-int has_access_tag_acl(const char* path, acl_tag_t desired_tag) {
- return has_type_tag_acl(path, ACL_TYPE_ACCESS, desired_tag);
-}
-
-int has_default_user_obj_acl(const char* path) {
- return has_default_tag_acl(path, ACL_USER_OBJ);
-}
-
-int has_default_group_obj_acl(const char* path) {
- return has_default_tag_acl(path, ACL_GROUP_OBJ);
-}
-
-int has_default_other_acl(const char* path) {
- return has_default_tag_acl(path, ACL_OTHER);
-}
-
-int has_default_mask_acl(const char* path) {
- return has_default_tag_acl(path, ACL_MASK);
-}
-
-
-int get_type_tag_entry(const char* path,
- acl_type_t type,
- acl_tag_t desired_tag,
- acl_entry_t* entry) {
- /* Returns one if successful, zero when the ACL doesn't exist, and
- -1 on unexpected errors. */
- acl_t acl = acl_get_file(path, type);
-
- if (acl == (acl_t)NULL) {
- /* Follow the acl_foo convention of -1 == error. */
- return 0;
- }
-
- int result = acl_get_entry(acl, ACL_FIRST_ENTRY, entry);
-
- while (result == 1) {
- acl_tag_t tag = ACL_UNDEFINED_TAG;
- int tag_result = acl_get_tag_type(*entry, &tag);
-
- if (tag_result == -1) {
- perror("get_type_tag_entry (acl_get_tag_type)");
- return -1;
- }
-
- if (tag == desired_tag) {
- /* We found the right tag, so return successfully. */
- return 1;
- }
-
- result = acl_get_entry(acl, ACL_NEXT_ENTRY, entry);
- }
-
- /* This catches both the initial acl_get_entry and the ones at the
- end of the loop. */
- if (result == -1) {
- perror("get_type_tag_entry (acl_get_entry)");
- return -1;
- }
-
- return 0;
-}
-
-int get_default_tag_entry(const char* path,
- acl_tag_t desired_tag,
- acl_entry_t* entry) {
- return get_type_tag_entry(path, ACL_TYPE_DEFAULT, desired_tag, entry);
-}
-
-int get_access_tag_entry(const char* path,
- acl_tag_t desired_tag,
- acl_entry_t* entry) {
- return get_type_tag_entry(path, ACL_TYPE_ACCESS, desired_tag, entry);
-}
-
-
-
-int get_type_tag_permset(const char* path,
- acl_type_t type,
- acl_tag_t desired_tag,
- acl_permset_t* output_perms) {
- /* Returns one if successful, zero when the ACL doesn't exist, and
- -1 on unexpected errors. */
- acl_t defacl = acl_get_file(path, type);
-
- if (defacl == (acl_t)NULL) {
- /* Follow the acl_foo convention of -1 == error. */
- return 0;
- }
-
- acl_entry_t entry;
- int result = get_type_tag_entry(path, type, desired_tag, &entry);
-
- if (result == 1) {
- /* We found the right tag, now get the permset. */
- int ps_result = acl_get_permset(entry, output_perms);
- if (ps_result == -1) {
- perror("get_type_tag_permset (acl_get_permset)");
- return -1;
- }
-
- if (ps_result == 0) {
- return 1;
- }
- else {
- return 0;
- }
- }
- else {
- return result;
- }
-}
-
-int get_default_tag_permset(const char* path,
- acl_tag_t desired_tag,
- acl_permset_t* output_perms) {
- return get_type_tag_permset(path,
- ACL_TYPE_DEFAULT,
- desired_tag,
- output_perms);
-}
-
-int get_access_tag_permset(const char* path,
- acl_tag_t desired_tag,
- acl_permset_t* output_perms) {
- return get_type_tag_permset(path, ACL_TYPE_ACCESS, desired_tag, output_perms);
-}
-
-int has_default_tag_perm(const char* path,
- acl_tag_t tag,
- acl_perm_t perm) {
- /* Check path to see if tag has the given perm. Returns one if it
- does, zero if it doesn't (or there's no ACL), and -1 on unexpected
- errors. */
-
- if (!has_default_tag_acl(path, tag)) {
- return 0;
- }
-
- acl_permset_t permset;
- bool ps_result = get_default_tag_permset(path, tag, &permset);
-
- if (ps_result != 1) {
- /* Failure or error. */
- return ps_result;
- }
-
- int p_result = acl_get_perm(permset, perm);
- if (p_result == -1) {
- perror("has_default_tag_perm (acl_get_perm)");
- }
-
- return p_result;
-}
-
-int remove_access_tag_perm(const char* path,
- acl_tag_t desired_tag,
- acl_perm_t perm) {
- /* Attempt to remove perm from tag. Returns one if successful, zero
- if there was nothing to do, and -1 on errors. */
- acl_t acl = acl_get_file(path, ACL_TYPE_ACCESS);
- if (acl == (acl_t)NULL) {
- /* Error. */
- return -1;
- }
-
- acl_permset_t permset;
- bool ps_result = get_access_tag_permset(path, desired_tag, &permset);
-
- if (ps_result != 1) {
- /* Failure or error. */
- return ps_result;
- }
-
- int d_result = acl_delete_perm(permset, perm);
- if (d_result == -1) {
- perror("remove_access_tag_perm (acl_delete_perm)");
- return -1;
- }
-
- /* We've only removed perm from the permset; now we have to replace
- the permset. */
- acl_entry_t entry;
- int result = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
-
- while (result == 1) {
- acl_tag_t tag = ACL_UNDEFINED_TAG;
- int tag_result = acl_get_tag_type(entry, &tag);
-
- if (tag_result == -1) {
- perror("remove_access_tag_perm (acl_get_tag_type)");
- return -1;
- }
-
- if (tag == desired_tag) {
- /* We found the right tag. Update the permset. */
- int s_result = acl_set_permset(entry, permset);
- if (s_result == -1) {
- perror("remove_access_tag_perm (acl_set_permset)");
- return -1;
- }
-
- int sf_result = acl_set_file(path, ACL_TYPE_ACCESS, acl);
- if (sf_result == -1) {
- perror("remove_access_tag_perm (acl_set_file)");
- return -1;
- }
-
- return 1;
- }
-
- result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
- }
-
- /* This catches both the initial acl_get_entry and the ones at the
- end of the loop. */
- if (result == -1) {
- perror("remove_access_tag_perm (acl_get_entry)");
- return -1;
- }
-
- return 0;
-}
-
-int remove_access_group_obj_execute(const char* path) {
- return remove_access_tag_perm(path, ACL_GROUP_OBJ, ACL_EXECUTE);
-}
-
-
-int has_default_user_obj_read(const char* path) {
- return has_default_tag_perm(path, ACL_USER_OBJ, ACL_READ);
-}
-
-int has_default_user_obj_write(const char* path) {
- return has_default_tag_perm(path, ACL_USER_OBJ, ACL_WRITE);
-}
-
-int has_default_user_obj_execute(const char* path) {
- return has_default_tag_perm(path, ACL_USER_OBJ, ACL_EXECUTE);
-}
-
-int has_default_group_obj_read(const char* path) {
- return has_default_tag_perm(path, ACL_GROUP_OBJ, ACL_READ);
-}
-
-int has_default_group_obj_write(const char* path) {
- return has_default_tag_perm(path, ACL_GROUP_OBJ, ACL_WRITE);
-}
-
-int has_default_group_obj_execute(const char* path) {
- return has_default_tag_perm(path, ACL_GROUP_OBJ, ACL_EXECUTE);
-}
-
-int has_default_other_read(const char* path) {
- return has_default_tag_perm(path, ACL_OTHER, ACL_READ);
-}
-
-int has_default_other_write(const char* path) {
- return has_default_tag_perm(path, ACL_OTHER, ACL_WRITE);
-}
-
-int has_default_other_execute(const char* path) {
- return has_default_tag_perm(path, ACL_OTHER, ACL_EXECUTE);
-}
-
-int has_default_mask_read(const char* path) {
- return has_default_tag_perm(path, ACL_MASK, ACL_READ);
-}
-
-int has_default_mask_write(const char* path) {
- return has_default_tag_perm(path, ACL_MASK, ACL_WRITE);
-}
-
-int has_default_mask_execute(const char* path) {
- return has_default_tag_perm(path, ACL_MASK, ACL_EXECUTE);
-}
-
-
-int reapply_default_acl(const char* path) {
- /* If this is a normal file or directory (i.e. that has just been
- created), we proceed to find its parent directory which will have
- a default ACL.
-
- Returns one for success, zero for failure (i.e. no ACL), and -1
- on unexpected errors. */
- if (path == NULL) {
- return 0;
- }
-
- if (!is_regular_file(path) && !is_directory(path)) {
- return 0;
- }
-
- /* dirname mangles its argument */
- char path_copy[PATH_MAX];
- strncpy(path_copy, path, PATH_MAX-1);
- path_copy[PATH_MAX-1] = 0;