Path: blob/main/lib/libc/posix1e/acl_delete_entry.c
39476 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2001-2002 Chris D. Faulhaber4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <sys/types.h>29#include "namespace.h"30#include <sys/acl.h>31#include "un-namespace.h"32#include <errno.h>33#include <string.h>34#include <stdio.h>3536#include "acl_support.h"3738static int39_entry_matches(const acl_entry_t a, const acl_entry_t b)40{41/*42* There is a semantical difference here between NFSv4 and POSIX43* draft ACLs. In POSIX, there may be only one entry for the particular44* user or group. In NFSv4 ACL, there may be any number of them. We're45* trying to be more specific here in that case.46*/47switch (_entry_brand(a)) {48case ACL_BRAND_NFS4:49if (a->ae_tag != b->ae_tag || a->ae_entry_type != b->ae_entry_type)50return (0);5152/* If ae_ids matter, compare them as well. */53if (a->ae_tag == ACL_USER || a->ae_tag == ACL_GROUP) {54if (a->ae_id != b->ae_id)55return (0);56}5758return (1);5960default:61if ((a->ae_tag == b->ae_tag) && (a->ae_id == b->ae_id))62return (1);63}6465return (0);66}6768/*69* acl_delete_entry() (23.4.9): remove the ACL entry indicated by entry_d70* from acl.71*/72int73acl_delete_entry(acl_t acl, acl_entry_t entry_d)74{75struct acl_entry entry_int;76int i, j, found = 0;7778if (acl == NULL || entry_d == NULL) {79errno = EINVAL;80return (-1);81}8283if (_entry_brand(entry_d) != _acl_brand(acl)) {84errno = EINVAL;85return (-1);86}8788if ((acl->ats_acl.acl_cnt < 1) ||89(acl->ats_acl.acl_cnt > ACL_MAX_ENTRIES)) {90errno = EINVAL;91return (-1);92}9394/* Use a local copy to prevent deletion of more than this entry */95entry_int = *entry_d;9697for (i = 0; i < acl->ats_acl.acl_cnt;) {98if (_entry_matches(&(acl->ats_acl.acl_entry[i]), &entry_int)) {99/* ...shift the remaining entries... */100for (j = i; j < acl->ats_acl.acl_cnt - 1; ++j)101acl->ats_acl.acl_entry[j] =102acl->ats_acl.acl_entry[j+1];103/* ...drop the count and zero the unused entry... */104acl->ats_acl.acl_cnt--;105bzero(&acl->ats_acl.acl_entry[j],106sizeof(struct acl_entry));107acl->ats_cur_entry = 0;108109/* Continue with the loop to remove all matching entries. */110found = 1;111} else112i++;113}114115if (found)116return (0);117118errno = EINVAL;119return (-1);120}121122int123acl_delete_entry_np(acl_t acl, int offset)124{125struct acl *acl_int;126int i;127128if (acl == NULL) {129errno = EINVAL;130return (-1);131}132133acl_int = &acl->ats_acl;134135if (offset < 0 || offset >= acl_int->acl_cnt) {136errno = EINVAL;137return (-1);138}139140if ((acl->ats_acl.acl_cnt < 1) ||141(acl->ats_acl.acl_cnt > ACL_MAX_ENTRIES)) {142errno = EINVAL;143return (-1);144}145146/* ...shift the remaining entries... */147for (i = offset; i < acl->ats_acl.acl_cnt - 1; ++i)148acl->ats_acl.acl_entry[i] =149acl->ats_acl.acl_entry[i+1];150/* ...drop the count and zero the unused entry... */151acl->ats_acl.acl_cnt--;152bzero(&acl->ats_acl.acl_entry[i],153sizeof(struct acl_entry));154acl->ats_cur_entry = 0;155156return (0);157}158159160