xref: /freebsd/lib/libc/posix1e/acl_calc_mask.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1d915a14eSPedro F. Giffuni /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro 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 
2914721edaSChris D. Faulhaber #include <sys/types.h>
307bd44e92SThomas Moestl #include "namespace.h"
3114721edaSChris D. Faulhaber #include <sys/acl.h>
327bd44e92SThomas Moestl #include "un-namespace.h"
3314721edaSChris D. Faulhaber 
3414721edaSChris D. Faulhaber #include <errno.h>
350f626307SChris D. Faulhaber #include <stdio.h>
3614721edaSChris D. Faulhaber 
37aa015c8eSEdward Tomasz Napierala #include "acl_support.h"
38aa015c8eSEdward Tomasz Napierala 
3914721edaSChris D. Faulhaber /*
400f626307SChris D. Faulhaber  * acl_calc_mask() (23.4.2): calculate and set the permissions
410f626307SChris D. Faulhaber  * associated with the ACL_MASK ACL entry.  If the ACL already
420f626307SChris D. Faulhaber  * contains an ACL_MASK entry, its permissions shall be
430f626307SChris D. Faulhaber  * overwritten; if not, one shall be added.
4414721edaSChris D. Faulhaber  */
4514721edaSChris D. Faulhaber int
acl_calc_mask(acl_t * acl_p)4614721edaSChris D. Faulhaber acl_calc_mask(acl_t *acl_p)
4714721edaSChris D. Faulhaber {
480f626307SChris D. Faulhaber 	struct acl	*acl_int, *acl_int_new;
4914721edaSChris D. Faulhaber 	acl_t		acl_new;
500f626307SChris D. Faulhaber 	int		i, mask_mode, mask_num;
5114721edaSChris D. Faulhaber 
520f626307SChris D. Faulhaber 	/*
530f626307SChris D. Faulhaber 	 * (23.4.2.4) requires acl_p to point to a pointer to a valid ACL.
540f626307SChris D. Faulhaber 	 * Since one of the primary reasons to use this function would be
550f626307SChris D. Faulhaber 	 * to calculate the appropriate mask to obtain a valid ACL, we only
560f626307SChris D. Faulhaber 	 * perform sanity checks here and validate the ACL prior to
570f626307SChris D. Faulhaber 	 * returning.
580f626307SChris D. Faulhaber 	 */
59e76872c1SChris D. Faulhaber 	if (acl_p == NULL || *acl_p == NULL) {
600f626307SChris D. Faulhaber 		errno = EINVAL;
61e76872c1SChris D. Faulhaber 		return (-1);
620f626307SChris D. Faulhaber 	}
6359831d75SEdward Tomasz Napierala 
6459831d75SEdward Tomasz Napierala 	if (!_acl_brand_may_be(*acl_p, ACL_BRAND_POSIX)) {
6559831d75SEdward Tomasz Napierala 		errno = EINVAL;
6659831d75SEdward Tomasz Napierala 		return (-1);
6759831d75SEdward Tomasz Napierala 	}
6859831d75SEdward Tomasz Napierala 	_acl_brand_as(*acl_p, ACL_BRAND_POSIX);
6959831d75SEdward Tomasz Napierala 
700f626307SChris D. Faulhaber 	acl_int = &(*acl_p)->ats_acl;
710f626307SChris D. Faulhaber 	if ((acl_int->acl_cnt < 3) || (acl_int->acl_cnt > ACL_MAX_ENTRIES)) {
7214721edaSChris D. Faulhaber 		errno = EINVAL;
73e76872c1SChris D. Faulhaber 		return (-1);
7414721edaSChris D. Faulhaber 	}
7514721edaSChris D. Faulhaber 
7614721edaSChris D. Faulhaber 	acl_new = acl_dup(*acl_p);
77e76872c1SChris D. Faulhaber 	if (acl_new == NULL)
78e76872c1SChris D. Faulhaber 		return (-1);
790f626307SChris D. Faulhaber 	acl_int_new = &acl_new->ats_acl;
8014721edaSChris D. Faulhaber 
810f626307SChris D. Faulhaber 	mask_mode = 0;
8214721edaSChris D. Faulhaber 	mask_num = -1;
8314721edaSChris D. Faulhaber 
8414721edaSChris D. Faulhaber 	/* gather permissions and find a mask entry */
850f626307SChris D. Faulhaber 	for (i = 0; i < acl_int_new->acl_cnt; i++) {
860f626307SChris D. Faulhaber 		switch(acl_int_new->acl_entry[i].ae_tag) {
8714721edaSChris D. Faulhaber 		case ACL_USER:
880f626307SChris D. Faulhaber 		case ACL_GROUP:
890f626307SChris D. Faulhaber 		case ACL_GROUP_OBJ:
9014721edaSChris D. Faulhaber 			mask_mode |=
910f626307SChris D. Faulhaber 			    acl_int_new->acl_entry[i].ae_perm & ACL_PERM_BITS;
9214721edaSChris D. Faulhaber 			break;
9314721edaSChris D. Faulhaber 		case ACL_MASK:
9414721edaSChris D. Faulhaber 			mask_num = i;
9514721edaSChris D. Faulhaber 			break;
9614721edaSChris D. Faulhaber 		}
9714721edaSChris D. Faulhaber 	}
980f626307SChris D. Faulhaber 
9914721edaSChris D. Faulhaber 	/* if a mask entry already exists, overwrite the perms */
1000f626307SChris D. Faulhaber 	if (mask_num != -1)
1010f626307SChris D. Faulhaber 		acl_int_new->acl_entry[mask_num].ae_perm = mask_mode;
1020f626307SChris D. Faulhaber 	else {
10314721edaSChris D. Faulhaber 		/* if no mask exists, check acl_cnt... */
1040f626307SChris D. Faulhaber 		if (acl_int_new->acl_cnt == ACL_MAX_ENTRIES) {
1050f626307SChris D. Faulhaber 			errno = ENOMEM;
106cf0f0b80SPedro F. Giffuni 			acl_free(acl_new);
107e76872c1SChris D. Faulhaber 			return (-1);
10814721edaSChris D. Faulhaber 		}
10914721edaSChris D. Faulhaber 		/* ...and add the mask entry */
1100f626307SChris D. Faulhaber 		acl_int_new->acl_entry[acl_int_new->acl_cnt].ae_tag = ACL_MASK;
1110f626307SChris D. Faulhaber 		acl_int_new->acl_entry[acl_int_new->acl_cnt].ae_id =
1120f626307SChris D. Faulhaber 		    ACL_UNDEFINED_ID;
1130f626307SChris D. Faulhaber 		acl_int_new->acl_entry[acl_int_new->acl_cnt].ae_perm =
1140f626307SChris D. Faulhaber 		    mask_mode;
1150f626307SChris D. Faulhaber 		acl_int_new->acl_cnt++;
11614721edaSChris D. Faulhaber 	}
11714721edaSChris D. Faulhaber 
11814721edaSChris D. Faulhaber 	if (acl_valid(acl_new) == -1) {
11914721edaSChris D. Faulhaber 		errno = EINVAL;
12014721edaSChris D. Faulhaber 		acl_free(acl_new);
121e76872c1SChris D. Faulhaber 		return (-1);
12214721edaSChris D. Faulhaber 	}
12314721edaSChris D. Faulhaber 
12414721edaSChris D. Faulhaber 	**acl_p = *acl_new;
12514721edaSChris D. Faulhaber 	acl_free(acl_new);
12614721edaSChris D. Faulhaber 
127e76872c1SChris D. Faulhaber 	return (0);
12814721edaSChris D. Faulhaber }
129