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) 1999, 2000, 2001, 2002 Robert N. M. Watson
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This software was developed by Robert Watson for the TrustedBSD Project.
      8  1.1  christos  *
      9  1.1  christos  * Redistribution and use in source and binary forms, with or without
     10  1.1  christos  * modification, are permitted provided that the following conditions
     11  1.1  christos  * are met:
     12  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     14  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  christos  *    documentation and/or other materials provided with the distribution.
     17  1.1  christos  *
     18  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     19  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.1  christos  * SUCH DAMAGE.
     29  1.1  christos  */
     30  1.1  christos /*
     31  1.1  christos  * acl_get_fd - syscall wrapper for retrieving access ACL by fd
     32  1.1  christos  * acl_get_fd_np - syscall wrapper for retrieving ACL by fd (non-POSIX)
     33  1.1  christos  * acl_get_file - syscall wrapper for retrieving ACL by filename
     34  1.1  christos  * acl_get_link_np - syscall wrapper for retrieving ACL by filename (NOFOLLOW)
     35  1.1  christos  *                   (non-POSIX)
     36  1.1  christos  * acl_get_perm_np() checks if a permission is in the specified
     37  1.1  christos  *                   permset (non-POSIX)
     38  1.1  christos  * acl_get_permset() returns the permission set in the ACL entry
     39  1.1  christos  * acl_get_qualifier() retrieves the qualifier of the tag from the ACL entry
     40  1.1  christos  * acl_get_tag_type() returns the tag type for the ACL entry entry_d
     41  1.1  christos  */
     42  1.1  christos 
     43  1.1  christos #include <sys/cdefs.h>
     44  1.1  christos #if 0
     45  1.1  christos __FBSDID("$FreeBSD: head/lib/libc/posix1e/acl_get.c 326193 2017-11-25 17:12:48Z pfg $");
     46  1.1  christos #else
     47  1.1  christos __RCSID("$NetBSD: acl_get.c,v 1.1 2020/05/16 18:31:47 christos Exp $");
     48  1.1  christos #endif
     49  1.1  christos 
     50  1.1  christos #include "namespace.h"
     51  1.1  christos #include <sys/types.h>
     52  1.1  christos #include <sys/acl.h>
     53  1.1  christos 
     54  1.1  christos #include <errno.h>
     55  1.1  christos #include <stdio.h>
     56  1.1  christos #include <stdlib.h>
     57  1.1  christos #include <string.h>
     58  1.1  christos #include <unistd.h>
     59  1.1  christos 
     60  1.1  christos #include "acl_support.h"
     61  1.1  christos 
     62  1.1  christos acl_t
     63  1.1  christos acl_get_file(const char *path_p, acl_type_t type)
     64  1.1  christos {
     65  1.1  christos 	acl_t	aclp;
     66  1.1  christos 	int	error;
     67  1.1  christos 
     68  1.1  christos 	aclp = acl_init(ACL_MAX_ENTRIES);
     69  1.1  christos 	if (aclp == NULL)
     70  1.1  christos 		return (NULL);
     71  1.1  christos 
     72  1.1  christos 	type = _acl_type_unold(type);
     73  1.1  christos 	error = __acl_get_file(path_p, type, &aclp->ats_acl);
     74  1.1  christos 	if (error) {
     75  1.1  christos 		acl_free(aclp);
     76  1.1  christos 		return (NULL);
     77  1.1  christos 	}
     78  1.1  christos 
     79  1.1  christos 	aclp->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;
     80  1.1  christos 	_acl_brand_from_type(aclp, type);
     81  1.1  christos 
     82  1.1  christos 	return (aclp);
     83  1.1  christos }
     84  1.1  christos 
     85  1.1  christos acl_t
     86  1.1  christos acl_get_link_np(const char *path_p, acl_type_t type)
     87  1.1  christos {
     88  1.1  christos 	acl_t	aclp;
     89  1.1  christos 	int	error;
     90  1.1  christos 
     91  1.1  christos 	aclp = acl_init(ACL_MAX_ENTRIES);
     92  1.1  christos 	if (aclp == NULL)
     93  1.1  christos 		return (NULL);
     94  1.1  christos 
     95  1.1  christos 	type = _acl_type_unold(type);
     96  1.1  christos 	error = __acl_get_link(path_p, type, &aclp->ats_acl);
     97  1.1  christos 	if (error) {
     98  1.1  christos 		acl_free(aclp);
     99  1.1  christos 		return (NULL);
    100  1.1  christos 	}
    101  1.1  christos 
    102  1.1  christos 	aclp->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;
    103  1.1  christos 	_acl_brand_from_type(aclp, type);
    104  1.1  christos 
    105  1.1  christos 	return (aclp);
    106  1.1  christos }
    107  1.1  christos 
    108  1.1  christos acl_t
    109  1.1  christos acl_get_fd(int fd)
    110  1.1  christos {
    111  1.1  christos 	if (fpathconf(fd, _PC_ACL_NFS4) == 1)
    112  1.1  christos 		return (acl_get_fd_np(fd, ACL_TYPE_NFS4));
    113  1.1  christos 
    114  1.1  christos 	return (acl_get_fd_np(fd, ACL_TYPE_ACCESS));
    115  1.1  christos }
    116  1.1  christos 
    117  1.1  christos acl_t
    118  1.1  christos acl_get_fd_np(int fd, acl_type_t type)
    119  1.1  christos {
    120  1.1  christos 	acl_t	aclp;
    121  1.1  christos 	int	error;
    122  1.1  christos 
    123  1.1  christos 	aclp = acl_init(ACL_MAX_ENTRIES);
    124  1.1  christos 	if (aclp == NULL)
    125  1.1  christos 		return (NULL);
    126  1.1  christos 
    127  1.1  christos 	type = _acl_type_unold(type);
    128  1.1  christos 	error = __acl_get_fd(fd, type, &aclp->ats_acl);
    129  1.1  christos 	if (error) {
    130  1.1  christos 		acl_free(aclp);
    131  1.1  christos 		return (NULL);
    132  1.1  christos 	}
    133  1.1  christos 
    134  1.1  christos 	aclp->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;
    135  1.1  christos 	_acl_brand_from_type(aclp, type);
    136  1.1  christos 
    137  1.1  christos 	return (aclp);
    138  1.1  christos }
    139  1.1  christos 
    140  1.1  christos /*
    141  1.1  christos  * acl_get_permset() (23.4.17): return via permset_p a descriptor to
    142  1.1  christos  * the permission set in the ACL entry entry_d.
    143  1.1  christos  */
    144  1.1  christos int
    145  1.1  christos acl_get_permset(acl_entry_t entry_d, acl_permset_t *permset_p)
    146  1.1  christos {
    147  1.1  christos 
    148  1.1  christos 	if (entry_d == NULL || permset_p == NULL) {
    149  1.1  christos 		errno = EINVAL;
    150  1.1  christos 		return (-1);
    151  1.1  christos 	}
    152  1.1  christos 
    153  1.1  christos 	*permset_p = &entry_d->ae_perm;
    154  1.1  christos 
    155  1.1  christos 	return (0);
    156  1.1  christos }
    157  1.1  christos 
    158  1.1  christos /*
    159  1.1  christos  * acl_get_qualifier() (23.4.18): retrieve the qualifier of the tag
    160  1.1  christos  * for the ACL entry entry_d.
    161  1.1  christos  */
    162  1.1  christos void *
    163  1.1  christos acl_get_qualifier(acl_entry_t entry_d)
    164  1.1  christos {
    165  1.1  christos 	uid_t *retval;
    166  1.1  christos 
    167  1.1  christos 	if (entry_d == NULL) {
    168  1.1  christos 		errno = EINVAL;
    169  1.1  christos 		return (NULL);
    170  1.1  christos 	}
    171  1.1  christos 
    172  1.1  christos 	switch(entry_d->ae_tag) {
    173  1.1  christos 	case ACL_USER:
    174  1.1  christos 	case ACL_GROUP:
    175  1.1  christos 		retval = malloc(sizeof(uid_t));
    176  1.1  christos 		if (retval == NULL)
    177  1.1  christos 			return (NULL);
    178  1.1  christos 		*retval = entry_d->ae_id;
    179  1.1  christos 		return (retval);
    180  1.1  christos 	}
    181  1.1  christos 
    182  1.1  christos 	errno = EINVAL;
    183  1.1  christos 	return (NULL);
    184  1.1  christos }
    185  1.1  christos 
    186  1.1  christos /*
    187  1.1  christos  * acl_get_tag_type() (23.4.19): return the tag type for the ACL
    188  1.1  christos  * entry entry_p.
    189  1.1  christos  */
    190  1.1  christos int
    191  1.1  christos acl_get_tag_type(acl_entry_t entry_d, acl_tag_t *tag_type_p)
    192  1.1  christos {
    193  1.1  christos 
    194  1.1  christos 	if (entry_d == NULL || tag_type_p == NULL) {
    195  1.1  christos 		errno = EINVAL;
    196  1.1  christos 		return (-1);
    197  1.1  christos 	}
    198  1.1  christos 
    199  1.1  christos 	*tag_type_p = entry_d->ae_tag;
    200  1.1  christos 
    201  1.1  christos 	return (0);
    202  1.1  christos }
    203  1.1  christos 
    204  1.1  christos int
    205  1.1  christos acl_get_entry_type_np(acl_entry_t entry_d, acl_entry_type_t *entry_type_p)
    206  1.1  christos {
    207  1.1  christos 
    208  1.1  christos 	if (entry_d == NULL || entry_type_p == NULL) {
    209  1.1  christos 		errno = EINVAL;
    210  1.1  christos 		return (-1);
    211  1.1  christos 	}
    212  1.1  christos 
    213  1.1  christos 	if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) {
    214  1.1  christos 		errno = EINVAL;
    215  1.1  christos 		return (-1);
    216  1.1  christos 	}
    217  1.1  christos 
    218  1.1  christos 	*entry_type_p = entry_d->ae_entry_type;
    219  1.1  christos 
    220  1.1  christos 	return (0);
    221  1.1  christos }
    222