Home | History | Annotate | Line # | Download | only in posix1e
      1  1.1  christos /*-
      2  1.1  christos  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
      3  1.1  christos  *
      4  1.1  christos  * Copyright (c) 2008, 2009 Edward Tomasz Napieraa <trasz (at) FreeBSD.org>
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that the following conditions
      9  1.1  christos  * are met:
     10  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  christos  *    documentation and/or other materials provided with the distribution.
     15  1.1  christos  *
     16  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  christos  * SUCH DAMAGE.
     27  1.1  christos  */
     28  1.1  christos 
     29  1.1  christos #include <sys/cdefs.h>
     30  1.1  christos #if 0
     31  1.1  christos __FBSDID("$FreeBSD: head/lib/libc/posix1e/acl_branding.c 326193 2017-11-25 17:12:48Z pfg $");
     32  1.1  christos #else
     33  1.2  christos __RCSID("$NetBSD: acl_branding.c,v 1.2 2024/01/20 14:52:48 christos Exp $");
     34  1.1  christos #endif
     35  1.1  christos 
     36  1.1  christos #include <assert.h>
     37  1.1  christos #include <errno.h>
     38  1.1  christos #include <sys/acl.h>
     39  1.1  christos 
     40  1.1  christos #include "acl_support.h"
     41  1.1  christos 
     42  1.1  christos /*
     43  1.1  christos  * An ugly detail of the implementation - fortunately not visible
     44  1.1  christos  * to the API users - is the "branding": libc needs to keep track
     45  1.1  christos  * of what "brand" ACL is: NFSv4, POSIX.1e or unknown.  It happens
     46  1.1  christos  * automatically - for example, during acl_get_file(3) ACL gets
     47  1.1  christos  * branded according to the "type" argument; during acl_set_permset
     48  1.1  christos  * ACL, if its brand is unknown it gets branded as NFSv4 if any of the
     49  1.1  christos  * NFSv4 permissions that are not valid for POSIX.1e ACL are set etc.
     50  1.1  christos  * Branding information is used for printing out the ACL (acl_to_text(3)),
     51  1.1  christos  * veryfying acl_set_whatever arguments (checking against setting
     52  1.1  christos  * bits that are valid only for NFSv4 in ACL branded as POSIX.1e) etc.
     53  1.1  christos  */
     54  1.1  christos 
     55  1.1  christos static acl_t
     56  1.1  christos entry2acl(acl_entry_t entry)
     57  1.1  christos {
     58  1.1  christos 	acl_t aclp;
     59  1.1  christos 
     60  1.2  christos 	aclp = (acl_t)(((unsigned long)entry >> _ACL_T_ALIGNMENT_BITS) << _ACL_T_ALIGNMENT_BITS);
     61  1.1  christos 
     62  1.1  christos 	return (aclp);
     63  1.1  christos }
     64  1.1  christos 
     65  1.1  christos /*
     66  1.1  christos  * Return brand of an ACL.
     67  1.1  christos  */
     68  1.1  christos int
     69  1.1  christos _acl_brand(const acl_t acl)
     70  1.1  christos {
     71  1.1  christos 
     72  1.1  christos 	return (acl->ats_brand);
     73  1.1  christos }
     74  1.1  christos 
     75  1.1  christos int
     76  1.1  christos _entry_brand(const acl_entry_t entry)
     77  1.1  christos {
     78  1.1  christos 
     79  1.1  christos 	return (_acl_brand(entry2acl(entry)));
     80  1.1  christos }
     81  1.1  christos 
     82  1.1  christos /*
     83  1.1  christos  * Return 1, iff branding ACL as "brand" is ok.
     84  1.1  christos  */
     85  1.1  christos int
     86  1.1  christos _acl_brand_may_be(const acl_t acl, int brand)
     87  1.1  christos {
     88  1.1  christos 
     89  1.1  christos 	if (_acl_brand(acl) == ACL_BRAND_UNKNOWN)
     90  1.1  christos 		return (1);
     91  1.1  christos 
     92  1.1  christos 	if (_acl_brand(acl) == brand)
     93  1.1  christos 		return (1);
     94  1.1  christos 
     95  1.1  christos 	return (0);
     96  1.1  christos }
     97  1.1  christos 
     98  1.1  christos int
     99  1.1  christos _entry_brand_may_be(const acl_entry_t entry, int brand)
    100  1.1  christos {
    101  1.1  christos 
    102  1.1  christos 	return (_acl_brand_may_be(entry2acl(entry), brand));
    103  1.1  christos }
    104  1.1  christos 
    105  1.1  christos /*
    106  1.1  christos  * Brand ACL as "brand".
    107  1.1  christos  */
    108  1.1  christos void
    109  1.1  christos _acl_brand_as(acl_t acl, int brand)
    110  1.1  christos {
    111  1.1  christos 
    112  1.1  christos 	assert(_acl_brand_may_be(acl, brand));
    113  1.1  christos 
    114  1.1  christos 	acl->ats_brand = brand;
    115  1.1  christos }
    116  1.1  christos 
    117  1.1  christos void
    118  1.1  christos _entry_brand_as(const acl_entry_t entry, int brand)
    119  1.1  christos {
    120  1.1  christos 
    121  1.1  christos 	_acl_brand_as(entry2acl(entry), brand);
    122  1.1  christos }
    123  1.1  christos 
    124  1.1  christos int
    125  1.1  christos _acl_type_not_valid_for_acl(const acl_t acl, acl_type_t type)
    126  1.1  christos {
    127  1.1  christos 
    128  1.1  christos 	switch (_acl_brand(acl)) {
    129  1.1  christos 	case ACL_BRAND_NFS4:
    130  1.1  christos 		if (type == ACL_TYPE_NFS4)
    131  1.1  christos 			return (0);
    132  1.1  christos 		break;
    133  1.1  christos 
    134  1.1  christos 	case ACL_BRAND_POSIX:
    135  1.1  christos 		if (type == ACL_TYPE_ACCESS || type == ACL_TYPE_DEFAULT)
    136  1.1  christos 			return (0);
    137  1.1  christos 		break;
    138  1.1  christos 
    139  1.1  christos 	case ACL_BRAND_UNKNOWN:
    140  1.1  christos 		return (0);
    141  1.1  christos 	}
    142  1.1  christos 
    143  1.1  christos 	return (-1);
    144  1.1  christos }
    145  1.1  christos 
    146  1.1  christos void
    147  1.1  christos _acl_brand_from_type(acl_t acl, acl_type_t type)
    148  1.1  christos {
    149  1.1  christos 
    150  1.1  christos 	switch (type) {
    151  1.1  christos 	case ACL_TYPE_NFS4:
    152  1.1  christos 		_acl_brand_as(acl, ACL_BRAND_NFS4);
    153  1.1  christos 		break;
    154  1.1  christos 	case ACL_TYPE_ACCESS:
    155  1.1  christos 	case ACL_TYPE_DEFAULT:
    156  1.1  christos 		_acl_brand_as(acl, ACL_BRAND_POSIX);
    157  1.1  christos 		break;
    158  1.1  christos 	default:
    159  1.1  christos 		/* XXX: What to do here? */
    160  1.1  christos 		break;
    161  1.1  christos 	}
    162  1.1  christos }
    163  1.1  christos 
    164  1.1  christos int
    165  1.1  christos acl_get_brand_np(acl_t acl, int *brand_p)
    166  1.1  christos {
    167  1.1  christos 
    168  1.1  christos 	if (acl == NULL || brand_p == NULL) {
    169  1.1  christos 		errno = EINVAL;
    170  1.1  christos 		return (-1);
    171  1.1  christos 	}
    172  1.1  christos 	*brand_p = _acl_brand(acl);
    173  1.1  christos 
    174  1.1  christos 	return (0);
    175  1.1  christos }
    176