114721edaSChris D. Faulhaber /* 2e76872c1SChris D. Faulhaber * Copyright (c) 2001-2002 Chris D. Faulhaber 314721edaSChris D. Faulhaber * All rights reserved. 414721edaSChris D. Faulhaber * 514721edaSChris D. Faulhaber * Redistribution and use in source and binary forms, with or without 614721edaSChris D. Faulhaber * modification, are permitted provided that the following conditions 714721edaSChris D. Faulhaber * are met: 814721edaSChris D. Faulhaber * 1. Redistributions of source code must retain the above copyright 914721edaSChris D. Faulhaber * notice, this list of conditions and the following disclaimer. 1014721edaSChris D. Faulhaber * 2. Redistributions in binary form must reproduce the above copyright 1114721edaSChris D. Faulhaber * notice, this list of conditions and the following disclaimer in the 1214721edaSChris D. Faulhaber * documentation and/or other materials provided with the distribution. 1314721edaSChris D. Faulhaber * 1414721edaSChris D. Faulhaber * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1514721edaSChris D. Faulhaber * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1614721edaSChris D. Faulhaber * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1768b23992SWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 1868b23992SWarner Losh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1968b23992SWarner Losh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2068b23992SWarner Losh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2168b23992SWarner Losh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2268b23992SWarner Losh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2368b23992SWarner Losh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2468b23992SWarner Losh * SUCH DAMAGE. 2514721edaSChris D. Faulhaber */ 2614721edaSChris D. Faulhaber 27333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 28333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 29333fc21eSDavid E. O'Brien 3014721edaSChris D. Faulhaber #include <sys/types.h> 317bd44e92SThomas Moestl #include "namespace.h" 3214721edaSChris D. Faulhaber #include <sys/acl.h> 337bd44e92SThomas Moestl #include "un-namespace.h" 3414721edaSChris D. Faulhaber 3514721edaSChris D. Faulhaber #include <errno.h> 360f626307SChris D. Faulhaber #include <stdio.h> 3714721edaSChris D. Faulhaber 38aa015c8eSEdward Tomasz Napierala #include "acl_support.h" 39aa015c8eSEdward Tomasz Napierala 4014721edaSChris D. Faulhaber /* 410f626307SChris D. Faulhaber * acl_calc_mask() (23.4.2): calculate and set the permissions 420f626307SChris D. Faulhaber * associated with the ACL_MASK ACL entry. If the ACL already 430f626307SChris D. Faulhaber * contains an ACL_MASK entry, its permissions shall be 440f626307SChris D. Faulhaber * overwritten; if not, one shall be added. 4514721edaSChris D. Faulhaber */ 4614721edaSChris D. Faulhaber int 4714721edaSChris D. Faulhaber acl_calc_mask(acl_t *acl_p) 4814721edaSChris D. Faulhaber { 490f626307SChris D. Faulhaber struct acl *acl_int, *acl_int_new; 5014721edaSChris D. Faulhaber acl_t acl_new; 510f626307SChris D. Faulhaber int i, mask_mode, mask_num; 5214721edaSChris D. Faulhaber 530f626307SChris D. Faulhaber /* 540f626307SChris D. Faulhaber * (23.4.2.4) requires acl_p to point to a pointer to a valid ACL. 550f626307SChris D. Faulhaber * Since one of the primary reasons to use this function would be 560f626307SChris D. Faulhaber * to calculate the appropriate mask to obtain a valid ACL, we only 570f626307SChris D. Faulhaber * perform sanity checks here and validate the ACL prior to 580f626307SChris D. Faulhaber * returning. 590f626307SChris D. Faulhaber */ 60e76872c1SChris D. Faulhaber if (acl_p == NULL || *acl_p == NULL) { 610f626307SChris D. Faulhaber errno = EINVAL; 62e76872c1SChris D. Faulhaber return (-1); 630f626307SChris D. Faulhaber } 6459831d75SEdward Tomasz Napierala 6559831d75SEdward Tomasz Napierala if (!_acl_brand_may_be(*acl_p, ACL_BRAND_POSIX)) { 6659831d75SEdward Tomasz Napierala errno = EINVAL; 6759831d75SEdward Tomasz Napierala return (-1); 6859831d75SEdward Tomasz Napierala } 6959831d75SEdward Tomasz Napierala _acl_brand_as(*acl_p, ACL_BRAND_POSIX); 7059831d75SEdward Tomasz Napierala 710f626307SChris D. Faulhaber acl_int = &(*acl_p)->ats_acl; 720f626307SChris D. Faulhaber if ((acl_int->acl_cnt < 3) || (acl_int->acl_cnt > ACL_MAX_ENTRIES)) { 7314721edaSChris D. Faulhaber errno = EINVAL; 74e76872c1SChris D. Faulhaber return (-1); 7514721edaSChris D. Faulhaber } 7614721edaSChris D. Faulhaber 7714721edaSChris D. Faulhaber acl_new = acl_dup(*acl_p); 78e76872c1SChris D. Faulhaber if (acl_new == NULL) 79e76872c1SChris D. Faulhaber return (-1); 800f626307SChris D. Faulhaber acl_int_new = &acl_new->ats_acl; 8114721edaSChris D. Faulhaber 820f626307SChris D. Faulhaber mask_mode = 0; 8314721edaSChris D. Faulhaber mask_num = -1; 8414721edaSChris D. Faulhaber 8514721edaSChris D. Faulhaber /* gather permissions and find a mask entry */ 860f626307SChris D. Faulhaber for (i = 0; i < acl_int_new->acl_cnt; i++) { 870f626307SChris D. Faulhaber switch(acl_int_new->acl_entry[i].ae_tag) { 8814721edaSChris D. Faulhaber case ACL_USER: 890f626307SChris D. Faulhaber case ACL_GROUP: 900f626307SChris D. Faulhaber case ACL_GROUP_OBJ: 9114721edaSChris D. Faulhaber mask_mode |= 920f626307SChris D. Faulhaber acl_int_new->acl_entry[i].ae_perm & ACL_PERM_BITS; 9314721edaSChris D. Faulhaber break; 9414721edaSChris D. Faulhaber case ACL_MASK: 9514721edaSChris D. Faulhaber mask_num = i; 9614721edaSChris D. Faulhaber break; 9714721edaSChris D. Faulhaber } 9814721edaSChris D. Faulhaber } 990f626307SChris D. Faulhaber 10014721edaSChris D. Faulhaber /* if a mask entry already exists, overwrite the perms */ 1010f626307SChris D. Faulhaber if (mask_num != -1) 1020f626307SChris D. Faulhaber acl_int_new->acl_entry[mask_num].ae_perm = mask_mode; 1030f626307SChris D. Faulhaber else { 10414721edaSChris D. Faulhaber /* if no mask exists, check acl_cnt... */ 1050f626307SChris D. Faulhaber if (acl_int_new->acl_cnt == ACL_MAX_ENTRIES) { 1060f626307SChris D. Faulhaber errno = ENOMEM; 107*cf0f0b80SPedro F. Giffuni acl_free(acl_new); 108e76872c1SChris D. Faulhaber return (-1); 10914721edaSChris D. Faulhaber } 11014721edaSChris D. Faulhaber /* ...and add the mask entry */ 1110f626307SChris D. Faulhaber acl_int_new->acl_entry[acl_int_new->acl_cnt].ae_tag = ACL_MASK; 1120f626307SChris D. Faulhaber acl_int_new->acl_entry[acl_int_new->acl_cnt].ae_id = 1130f626307SChris D. Faulhaber ACL_UNDEFINED_ID; 1140f626307SChris D. Faulhaber acl_int_new->acl_entry[acl_int_new->acl_cnt].ae_perm = 1150f626307SChris D. Faulhaber mask_mode; 1160f626307SChris D. Faulhaber acl_int_new->acl_cnt++; 11714721edaSChris D. Faulhaber } 11814721edaSChris D. Faulhaber 11914721edaSChris D. Faulhaber if (acl_valid(acl_new) == -1) { 12014721edaSChris D. Faulhaber errno = EINVAL; 12114721edaSChris D. Faulhaber acl_free(acl_new); 122e76872c1SChris D. Faulhaber return (-1); 12314721edaSChris D. Faulhaber } 12414721edaSChris D. Faulhaber 12514721edaSChris D. Faulhaber **acl_p = *acl_new; 12614721edaSChris D. Faulhaber acl_free(acl_new); 12714721edaSChris D. Faulhaber 128e76872c1SChris D. Faulhaber return (0); 12914721edaSChris D. Faulhaber } 130