- * @brief Update (or create) an entry in an @b minimal ACL.
- *
- * This function will not work if @c aclp contains extended
- * entries. This is fine for our purposes, since we call @c wipe_acls
- * on each path before applying the default to it.
- *
- * The assumption that there are no extended entries makes things much
- * simpler. For example, we only have to update the @c ACL_USER_OBJ,
- * @c ACL_GROUP_OBJ, and @c ACL_OTHER entries -- all others can simply
- * be created anew. This means we don't have to fool around comparing
- * named-user/group entries.
- *
- * @param aclp
- * A pointer to the acl_t structure whose entry we want to modify.
- *
- * @param entry
- * The new entry. If @c entry contains a user/group/other entry, we
- * update the existing one. Otherwise we create a new entry.
- *
- * @return If there is an unexpected library error, @c ACL_ERROR is
- * returned. Otherwise, @c ACL_SUCCESS.
- *
- */
-int acl_set_entry(acl_t* aclp,
- acl_entry_t entry) {
-
- acl_tag_t entry_tag;
- int gt_result = acl_get_tag_type(entry, &entry_tag);
- if (gt_result == ACL_ERROR) {
- perror("acl_set_entry (acl_get_tag_type)");
- return ACL_ERROR;
- }
-
- acl_permset_t entry_permset;
- int ps_result = acl_get_permset(entry, &entry_permset);
- if (ps_result == ACL_ERROR) {
- perror("acl_set_entry (acl_get_permset)");
- return ACL_ERROR;
- }
-
- 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);
-
- while (result == ACL_SUCCESS) {
- acl_tag_t existing_tag = ACL_UNDEFINED_TAG;
- int tag_result = acl_get_tag_type(existing_entry, &existing_tag);
-
- if (tag_result == ACL_ERROR) {
- perror("set_acl_tag_permset (acl_get_tag_type)");
- return ACL_ERROR;
- }
-
- if (existing_tag == entry_tag) {
- if (entry_tag == ACL_USER_OBJ ||
- entry_tag == ACL_GROUP_OBJ ||
- entry_tag == ACL_OTHER) {
- /* Only update for these three since all other tags will have
- been wiped. These three are guaranteed to exist, so if we
- match one of them, we're allowed to return ACL_SUCCESS
- below and bypass the rest of the function. */
- acl_permset_t existing_permset;
- int gep_result = acl_get_permset(existing_entry, &existing_permset);
- if (gep_result == ACL_ERROR) {
- perror("acl_set_entry (acl_get_permset)");
- return ACL_ERROR;
- }
-
- int s_result = acl_set_permset(existing_entry, entry_permset);
- if (s_result == ACL_ERROR) {
- perror("acl_set_entry (acl_set_permset)");
- return ACL_ERROR;
- }
-
- return ACL_SUCCESS;
- }
-
- }
-
- 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) {
- perror("acl_set_entry (acl_get_entry)");
- return ACL_ERROR;
- }
-
- /* If we've made it this far, we need to add a new entry to the
- ACL. */
- acl_entry_t new_entry;
-
- /* We allocate memory here that we should release! */
- int c_result = acl_create_entry(aclp, &new_entry);
- if (c_result == ACL_ERROR) {
- perror("acl_set_entry (acl_create_entry)");
- return ACL_ERROR;
- }
-
- int st_result = acl_set_tag_type(new_entry, entry_tag);
- if (st_result == ACL_ERROR) {
- perror("acl_set_entry (acl_set_tag_type)");
- return ACL_ERROR;
- }
-
- int s_result = acl_set_permset(new_entry, entry_permset);
- if (s_result == ACL_ERROR) {
- perror("acl_set_entry (acl_set_permset)");
- return ACL_ERROR;
- }
-
- if (entry_tag == ACL_USER || entry_tag == ACL_GROUP) {
- /* We need to set the qualifier too. */
- void* entry_qual = acl_get_qualifier(entry);
- if (entry_qual == (void*)NULL) {
- perror("acl_set_entry (acl_get_qualifier)");
- return ACL_ERROR;
- }
-
- int sq_result = acl_set_qualifier(new_entry, entry_qual);
- if (sq_result == ACL_ERROR) {
- perror("acl_set_entry (acl_set_qualifier)");
- return ACL_ERROR;
- }
- }
-
- return ACL_SUCCESS;
-}
-
-
-
-/**
- * @brief Determine the number of entries in the given ACL.
- *
- * @param acl
- * A pointer to an @c acl_t structure.
- *
- * @return Either the non-negative number of entries in @c acl, or
- * @c ACL_ERROR on error.
- */
-int acl_entry_count(acl_t* acl) {
-
- acl_entry_t entry;
- int entry_count = 0;
- int result = acl_get_entry(*acl, ACL_FIRST_ENTRY, &entry);
-
- while (result == ACL_SUCCESS) {
- entry_count++;
- result = acl_get_entry(*acl, ACL_NEXT_ENTRY, &entry);
- }
-
- if (result == ACL_ERROR) {
- perror("acl_entry_count (acl_get_entry)");
- return ACL_ERROR;
- }
-
- return entry_count;
-}
-
-
-
-/**
- * @brief Determine whether or not the given ACL is minimal.
- *
- * An ACL is minimal if it has fewer than four entries.
- *
- * @param acl
- * A pointer to an acl_t structure.
- *
- * @return
- * - @c ACL_SUCCESS - @c acl is minimal
- * - @c ACL_FAILURE - @c acl is not minimal
- * - @c ACL_ERROR - Unexpected library error
- */
-int acl_is_minimal(acl_t* acl) {
-
- int ec = acl_entry_count(acl);
-
- if (ec == ACL_ERROR) {
- perror("acl_is_minimal (acl_entry_count)");
- return ACL_ERROR;
- }
-
- if (ec < 4) {
- return ACL_SUCCESS;
- }
- else {
- return ACL_FAILURE;
- }
-}
-
-
-
-/**
- * @brief Determine whether the given path has an ACL whose mask
- * denies execute.
- *
- * @param path
- * The path to check.
- *
- * @return
- * - @c ACL_SUCCESS - @c path has a mask which denies execute.
- * - @c ACL_FAILURE - The ACL for @c path does not deny execute,
- * or @c path has no extended ACL at all.
- * - @c ACL_ERROR - Unexpected library error.
- */
-int acl_execute_masked(const char* path) {
-
- acl_t acl = acl_get_file(path, ACL_TYPE_ACCESS);
-
- if (acl == (acl_t)NULL) {
- perror("acl_execute_masked (acl_get_file)");
- return ACL_ERROR;
- }
-
- /* Our return value. */
- int result = ACL_FAILURE;
-
- acl_entry_t entry;
- int ge_result = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
-
- while (ge_result == ACL_SUCCESS) {
- acl_tag_t tag = ACL_UNDEFINED_TAG;
- int tag_result = acl_get_tag_type(entry, &tag);
-
- if (tag_result == ACL_ERROR) {
- perror("acl_execute_masked (acl_get_tag_type)");
- result = ACL_ERROR;
- goto cleanup;
- }
-
- if (tag == ACL_MASK) {
- /* This is the mask entry, get its permissions, and see if
- execute is specified. */
- acl_permset_t permset;
-
- int ps_result = acl_get_permset(entry, &permset);
- if (ps_result == ACL_ERROR) {
- perror("acl_execute_masked (acl_get_permset)");
- result = ACL_ERROR;
- goto cleanup;
- }
-
- int gp_result = acl_get_perm(permset, ACL_EXECUTE);
- if (gp_result == ACL_ERROR) {
- perror("acl_execute_masked (acl_get_perm)");
- result = ACL_ERROR;
- goto cleanup;
- }
-
- if (gp_result == ACL_FAILURE) {
- /* No execute bit set in the mask; execute not allowed. */
- return ACL_SUCCESS;
- }
- }
-
- ge_result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
- }
-
- cleanup:
- acl_free(acl);
- return result;
-}
-
-
-/**
- * @brief Determine whether @c path is executable (by anyone) or a
- * directory.
- *
- * This is used as part of the heuristic to determine whether or not
- * we should mask the execute bit when inheriting an ACL. If @c path
- * is a directory, the answer is a clear-cut yes. This behavior is
- * modeled after the capital 'X' perms of setfacl.
- *
- * If @c path is a file, we check the @a effective permissions,
- * contrary to what setfacl does.