Home | History | Annotate | Line # | Download | only in posix1e
      1 /*-
      2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
      3  *
      4  * Copyright (c) 2001-2002 Chris D. Faulhaber
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 #if 0
     31 __FBSDID("$FreeBSD: head/lib/libc/posix1e/acl_calc_mask.c 326193 2017-11-25 17:12:48Z pfg $");
     32 #else
     33 __RCSID("$NetBSD: acl_calc_mask.c,v 1.2 2024/01/20 14:52:48 christos Exp $");
     34 #endif
     35 
     36 #include "namespace.h"
     37 
     38 #include <sys/types.h>
     39 #include <sys/acl.h>
     40 
     41 #include <errno.h>
     42 #include <stdio.h>
     43 
     44 #include "acl_support.h"
     45 
     46 /*
     47  * acl_calc_mask() (23.4.2): calculate and set the permissions
     48  * associated with the ACL_MASK ACL entry.  If the ACL already
     49  * contains an ACL_MASK entry, its permissions shall be
     50  * overwritten; if not, one shall be added.
     51  */
     52 int
     53 acl_calc_mask(acl_t *acl_p)
     54 {
     55 	struct acl	*acl_int, *acl_int_new;
     56 	acl_t		acl_new;
     57 	size_t		i;
     58 	int		mask_mode, mask_num;
     59 
     60 	/*
     61 	 * (23.4.2.4) requires acl_p to point to a pointer to a valid ACL.
     62 	 * Since one of the primary reasons to use this function would be
     63 	 * to calculate the appropriate mask to obtain a valid ACL, we only
     64 	 * perform sanity checks here and validate the ACL prior to
     65 	 * returning.
     66 	 */
     67 	if (acl_p == NULL || *acl_p == NULL) {
     68 		errno = EINVAL;
     69 		return (-1);
     70 	}
     71 
     72 	if (!_acl_brand_may_be(*acl_p, ACL_BRAND_POSIX)) {
     73 		errno = EINVAL;
     74 		return (-1);
     75 	}
     76 	_acl_brand_as(*acl_p, ACL_BRAND_POSIX);
     77 
     78 	acl_int = &(*acl_p)->ats_acl;
     79 	if ((acl_int->acl_cnt < 3) || (acl_int->acl_cnt > ACL_MAX_ENTRIES)) {
     80 		errno = EINVAL;
     81 		return (-1);
     82 	}
     83 
     84 	acl_new = acl_dup(*acl_p);
     85 	if (acl_new == NULL)
     86 		return (-1);
     87 	acl_int_new = &acl_new->ats_acl;
     88 
     89 	mask_mode = 0;
     90 	mask_num = -1;
     91 
     92 	/* gather permissions and find a mask entry */
     93 	for (i = 0; i < acl_int_new->acl_cnt; i++) {
     94 		switch(acl_int_new->acl_entry[i].ae_tag) {
     95 		case ACL_USER:
     96 		case ACL_GROUP:
     97 		case ACL_GROUP_OBJ:
     98 			mask_mode |=
     99 			    acl_int_new->acl_entry[i].ae_perm & ACL_PERM_BITS;
    100 			break;
    101 		case ACL_MASK:
    102 			mask_num = (int)i;
    103 			break;
    104 		}
    105 	}
    106 
    107 	/* if a mask entry already exists, overwrite the perms */
    108 	if (mask_num != -1)
    109 		acl_int_new->acl_entry[mask_num].ae_perm = mask_mode;
    110 	else {
    111 		/* if no mask exists, check acl_cnt... */
    112 		if (acl_int_new->acl_cnt == ACL_MAX_ENTRIES) {
    113 			errno = ENOMEM;
    114 			acl_free(acl_new);
    115 			return (-1);
    116 		}
    117 		/* ...and add the mask entry */
    118 		acl_int_new->acl_entry[acl_int_new->acl_cnt].ae_tag = ACL_MASK;
    119 		acl_int_new->acl_entry[acl_int_new->acl_cnt].ae_id =
    120 		    ACL_UNDEFINED_ID;
    121 		acl_int_new->acl_entry[acl_int_new->acl_cnt].ae_perm =
    122 		    mask_mode;
    123 		acl_int_new->acl_cnt++;
    124 	}
    125 
    126 	if (acl_valid(acl_new) == -1) {
    127 		errno = EINVAL;
    128 		acl_free(acl_new);
    129 		return (-1);
    130 	}
    131 
    132 	**acl_p = *acl_new;
    133 	acl_free(acl_new);
    134 
    135 	return (0);
    136 }
    137