/*-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 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"3233#include <errno.h>34#include <stdio.h>3536#include "acl_support.h"3738/*39* acl_calc_mask() (23.4.2): calculate and set the permissions40* associated with the ACL_MASK ACL entry. If the ACL already41* contains an ACL_MASK entry, its permissions shall be42* overwritten; if not, one shall be added.43*/44int45acl_calc_mask(acl_t *acl_p)46{47struct acl *acl_int, *acl_int_new;48acl_t acl_new;49int i, mask_mode, mask_num;5051/*52* (23.4.2.4) requires acl_p to point to a pointer to a valid ACL.53* Since one of the primary reasons to use this function would be54* to calculate the appropriate mask to obtain a valid ACL, we only55* perform sanity checks here and validate the ACL prior to56* returning.57*/58if (acl_p == NULL || *acl_p == NULL) {59errno = EINVAL;60return (-1);61}6263if (!_acl_brand_may_be(*acl_p, ACL_BRAND_POSIX)) {64errno = EINVAL;65return (-1);66}67_acl_brand_as(*acl_p, ACL_BRAND_POSIX);6869acl_int = &(*acl_p)->ats_acl;70if ((acl_int->acl_cnt < 3) || (acl_int->acl_cnt > ACL_MAX_ENTRIES)) {71errno = EINVAL;72return (-1);73}7475acl_new = acl_dup(*acl_p);76if (acl_new == NULL)77return (-1);78acl_int_new = &acl_new->ats_acl;7980mask_mode = 0;81mask_num = -1;8283/* gather permissions and find a mask entry */84for (i = 0; i < acl_int_new->acl_cnt; i++) {85switch(acl_int_new->acl_entry[i].ae_tag) {86case ACL_USER:87case ACL_GROUP:88case ACL_GROUP_OBJ:89mask_mode |=90acl_int_new->acl_entry[i].ae_perm & ACL_PERM_BITS;91break;92case ACL_MASK:93mask_num = i;94break;95}96}9798/* if a mask entry already exists, overwrite the perms */99if (mask_num != -1)100acl_int_new->acl_entry[mask_num].ae_perm = mask_mode;101else {102/* if no mask exists, check acl_cnt... */103if (acl_int_new->acl_cnt == ACL_MAX_ENTRIES) {104errno = ENOMEM;105acl_free(acl_new);106return (-1);107}108/* ...and add the mask entry */109acl_int_new->acl_entry[acl_int_new->acl_cnt].ae_tag = ACL_MASK;110acl_int_new->acl_entry[acl_int_new->acl_cnt].ae_id =111ACL_UNDEFINED_ID;112acl_int_new->acl_entry[acl_int_new->acl_cnt].ae_perm =113mask_mode;114acl_int_new->acl_cnt++;115}116117if (acl_valid(acl_new) == -1) {118errno = EINVAL;119acl_free(acl_new);120return (-1);121}122123**acl_p = *acl_new;124acl_free(acl_new);125126return (0);127}128129130