1*d915a14eSPedro F. Giffuni /*- 2*d915a14eSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*d915a14eSPedro F. Giffuni * 4e76872c1SChris D. Faulhaber * Copyright (c) 2001-2002 Chris D. Faulhaber 514721edaSChris D. Faulhaber * All rights reserved. 614721edaSChris D. Faulhaber * 714721edaSChris D. Faulhaber * Redistribution and use in source and binary forms, with or without 814721edaSChris D. Faulhaber * modification, are permitted provided that the following conditions 914721edaSChris D. Faulhaber * are met: 1014721edaSChris D. Faulhaber * 1. Redistributions of source code must retain the above copyright 1114721edaSChris D. Faulhaber * notice, this list of conditions and the following disclaimer. 1214721edaSChris D. Faulhaber * 2. Redistributions in binary form must reproduce the above copyright 1314721edaSChris D. Faulhaber * notice, this list of conditions and the following disclaimer in the 1414721edaSChris D. Faulhaber * documentation and/or other materials provided with the distribution. 1514721edaSChris D. Faulhaber * 1614721edaSChris D. Faulhaber * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1714721edaSChris D. Faulhaber * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1814721edaSChris D. Faulhaber * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1968b23992SWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 2068b23992SWarner Losh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2168b23992SWarner Losh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2268b23992SWarner Losh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2368b23992SWarner Losh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2468b23992SWarner Losh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2568b23992SWarner Losh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2668b23992SWarner Losh * SUCH DAMAGE. 2714721edaSChris D. Faulhaber */ 2814721edaSChris D. Faulhaber 29333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 30333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 31333fc21eSDavid E. O'Brien 3214721edaSChris D. Faulhaber #include <sys/types.h> 337bd44e92SThomas Moestl #include "namespace.h" 3414721edaSChris D. Faulhaber #include <sys/acl.h> 357bd44e92SThomas Moestl #include "un-namespace.h" 3614721edaSChris D. Faulhaber 3714721edaSChris D. Faulhaber #include <errno.h> 380f626307SChris D. Faulhaber #include <stdio.h> 3914721edaSChris D. Faulhaber 40aa015c8eSEdward Tomasz Napierala #include "acl_support.h" 41aa015c8eSEdward Tomasz Napierala 4214721edaSChris D. Faulhaber /* 430f626307SChris D. Faulhaber * acl_calc_mask() (23.4.2): calculate and set the permissions 440f626307SChris D. Faulhaber * associated with the ACL_MASK ACL entry. If the ACL already 450f626307SChris D. Faulhaber * contains an ACL_MASK entry, its permissions shall be 460f626307SChris D. Faulhaber * overwritten; if not, one shall be added. 4714721edaSChris D. Faulhaber */ 4814721edaSChris D. Faulhaber int 4914721edaSChris D. Faulhaber acl_calc_mask(acl_t *acl_p) 5014721edaSChris D. Faulhaber { 510f626307SChris D. Faulhaber struct acl *acl_int, *acl_int_new; 5214721edaSChris D. Faulhaber acl_t acl_new; 530f626307SChris D. Faulhaber int i, mask_mode, mask_num; 5414721edaSChris D. Faulhaber 550f626307SChris D. Faulhaber /* 560f626307SChris D. Faulhaber * (23.4.2.4) requires acl_p to point to a pointer to a valid ACL. 570f626307SChris D. Faulhaber * Since one of the primary reasons to use this function would be 580f626307SChris D. Faulhaber * to calculate the appropriate mask to obtain a valid ACL, we only 590f626307SChris D. Faulhaber * perform sanity checks here and validate the ACL prior to 600f626307SChris D. Faulhaber * returning. 610f626307SChris D. Faulhaber */ 62e76872c1SChris D. Faulhaber if (acl_p == NULL || *acl_p == NULL) { 630f626307SChris D. Faulhaber errno = EINVAL; 64e76872c1SChris D. Faulhaber return (-1); 650f626307SChris D. Faulhaber } 6659831d75SEdward Tomasz Napierala 6759831d75SEdward Tomasz Napierala if (!_acl_brand_may_be(*acl_p, ACL_BRAND_POSIX)) { 6859831d75SEdward Tomasz Napierala errno = EINVAL; 6959831d75SEdward Tomasz Napierala return (-1); 7059831d75SEdward Tomasz Napierala } 7159831d75SEdward Tomasz Napierala _acl_brand_as(*acl_p, ACL_BRAND_POSIX); 7259831d75SEdward Tomasz Napierala 730f626307SChris D. Faulhaber acl_int = &(*acl_p)->ats_acl; 740f626307SChris D. Faulhaber if ((acl_int->acl_cnt < 3) || (acl_int->acl_cnt > ACL_MAX_ENTRIES)) { 7514721edaSChris D. Faulhaber errno = EINVAL; 76e76872c1SChris D. Faulhaber return (-1); 7714721edaSChris D. Faulhaber } 7814721edaSChris D. Faulhaber 7914721edaSChris D. Faulhaber acl_new = acl_dup(*acl_p); 80e76872c1SChris D. Faulhaber if (acl_new == NULL) 81e76872c1SChris D. Faulhaber return (-1); 820f626307SChris D. Faulhaber acl_int_new = &acl_new->ats_acl; 8314721edaSChris D. Faulhaber 840f626307SChris D. Faulhaber mask_mode = 0; 8514721edaSChris D. Faulhaber mask_num = -1; 8614721edaSChris D. Faulhaber 8714721edaSChris D. Faulhaber /* gather permissions and find a mask entry */ 880f626307SChris D. Faulhaber for (i = 0; i < acl_int_new->acl_cnt; i++) { 890f626307SChris D. Faulhaber switch(acl_int_new->acl_entry[i].ae_tag) { 9014721edaSChris D. Faulhaber case ACL_USER: 910f626307SChris D. Faulhaber case ACL_GROUP: 920f626307SChris D. Faulhaber case ACL_GROUP_OBJ: 9314721edaSChris D. Faulhaber mask_mode |= 940f626307SChris D. Faulhaber acl_int_new->acl_entry[i].ae_perm & ACL_PERM_BITS; 9514721edaSChris D. Faulhaber break; 9614721edaSChris D. Faulhaber case ACL_MASK: 9714721edaSChris D. Faulhaber mask_num = i; 9814721edaSChris D. Faulhaber break; 9914721edaSChris D. Faulhaber } 10014721edaSChris D. Faulhaber } 1010f626307SChris D. Faulhaber 10214721edaSChris D. Faulhaber /* if a mask entry already exists, overwrite the perms */ 1030f626307SChris D. Faulhaber if (mask_num != -1) 1040f626307SChris D. Faulhaber acl_int_new->acl_entry[mask_num].ae_perm = mask_mode; 1050f626307SChris D. Faulhaber else { 10614721edaSChris D. Faulhaber /* if no mask exists, check acl_cnt... */ 1070f626307SChris D. Faulhaber if (acl_int_new->acl_cnt == ACL_MAX_ENTRIES) { 1080f626307SChris D. Faulhaber errno = ENOMEM; 109cf0f0b80SPedro F. Giffuni acl_free(acl_new); 110e76872c1SChris D. Faulhaber return (-1); 11114721edaSChris D. Faulhaber } 11214721edaSChris D. Faulhaber /* ...and add the mask entry */ 1130f626307SChris D. Faulhaber acl_int_new->acl_entry[acl_int_new->acl_cnt].ae_tag = ACL_MASK; 1140f626307SChris D. Faulhaber acl_int_new->acl_entry[acl_int_new->acl_cnt].ae_id = 1150f626307SChris D. Faulhaber ACL_UNDEFINED_ID; 1160f626307SChris D. Faulhaber acl_int_new->acl_entry[acl_int_new->acl_cnt].ae_perm = 1170f626307SChris D. Faulhaber mask_mode; 1180f626307SChris D. Faulhaber acl_int_new->acl_cnt++; 11914721edaSChris D. Faulhaber } 12014721edaSChris D. Faulhaber 12114721edaSChris D. Faulhaber if (acl_valid(acl_new) == -1) { 12214721edaSChris D. Faulhaber errno = EINVAL; 12314721edaSChris D. Faulhaber acl_free(acl_new); 124e76872c1SChris D. Faulhaber return (-1); 12514721edaSChris D. Faulhaber } 12614721edaSChris D. Faulhaber 12714721edaSChris D. Faulhaber **acl_p = *acl_new; 12814721edaSChris D. Faulhaber acl_free(acl_new); 12914721edaSChris D. Faulhaber 130e76872c1SChris D. Faulhaber return (0); 13114721edaSChris D. Faulhaber } 132