/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson4* All rights reserved.5*6* This software was developed by Robert Watson for the TrustedBSD Project.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*/29/*30* acl_get_fd - syscall wrapper for retrieving access ACL by fd31* acl_get_fd_np - syscall wrapper for retrieving ACL by fd (non-POSIX)32* acl_get_file - syscall wrapper for retrieving ACL by filename33* acl_get_link_np - syscall wrapper for retrieving ACL by filename (NOFOLLOW)34* (non-POSIX)35* acl_get_perm_np() checks if a permission is in the specified36* permset (non-POSIX)37* acl_get_permset() returns the permission set in the ACL entry38* acl_get_qualifier() retrieves the qualifier of the tag from the ACL entry39* acl_get_tag_type() returns the tag type for the ACL entry entry_d40*/4142#include <sys/types.h>43#include "namespace.h"44#include <sys/acl.h>45#include "un-namespace.h"4647#include <errno.h>48#include <stdio.h>49#include <stdlib.h>50#include <string.h>51#include <unistd.h>5253#include "acl_support.h"5455acl_t56acl_get_file(const char *path_p, acl_type_t type)57{58acl_t aclp;59int error;6061aclp = acl_init(ACL_MAX_ENTRIES);62if (aclp == NULL)63return (NULL);6465type = _acl_type_unold(type);66error = __acl_get_file(path_p, type, &aclp->ats_acl);67if (error) {68acl_free(aclp);69return (NULL);70}7172aclp->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;73_acl_brand_from_type(aclp, type);7475return (aclp);76}7778acl_t79acl_get_link_np(const char *path_p, acl_type_t type)80{81acl_t aclp;82int error;8384aclp = acl_init(ACL_MAX_ENTRIES);85if (aclp == NULL)86return (NULL);8788type = _acl_type_unold(type);89error = __acl_get_link(path_p, type, &aclp->ats_acl);90if (error) {91acl_free(aclp);92return (NULL);93}9495aclp->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;96_acl_brand_from_type(aclp, type);9798return (aclp);99}100101acl_t102acl_get_fd(int fd)103{104if (fpathconf(fd, _PC_ACL_NFS4) == 1)105return (acl_get_fd_np(fd, ACL_TYPE_NFS4));106107return (acl_get_fd_np(fd, ACL_TYPE_ACCESS));108}109110acl_t111acl_get_fd_np(int fd, acl_type_t type)112{113acl_t aclp;114int error;115116aclp = acl_init(ACL_MAX_ENTRIES);117if (aclp == NULL)118return (NULL);119120type = _acl_type_unold(type);121error = ___acl_get_fd(fd, type, &aclp->ats_acl);122if (error) {123acl_free(aclp);124return (NULL);125}126127aclp->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;128_acl_brand_from_type(aclp, type);129130return (aclp);131}132133/*134* acl_get_permset() (23.4.17): return via permset_p a descriptor to135* the permission set in the ACL entry entry_d.136*/137int138acl_get_permset(acl_entry_t entry_d, acl_permset_t *permset_p)139{140141if (entry_d == NULL || permset_p == NULL) {142errno = EINVAL;143return (-1);144}145146*permset_p = &entry_d->ae_perm;147148return (0);149}150151/*152* acl_get_qualifier() (23.4.18): retrieve the qualifier of the tag153* for the ACL entry entry_d.154*/155void *156acl_get_qualifier(acl_entry_t entry_d)157{158uid_t *retval;159160if (entry_d == NULL) {161errno = EINVAL;162return (NULL);163}164165switch(entry_d->ae_tag) {166case ACL_USER:167case ACL_GROUP:168retval = malloc(sizeof(uid_t));169if (retval == NULL)170return (NULL);171*retval = entry_d->ae_id;172return (retval);173}174175errno = EINVAL;176return (NULL);177}178179/*180* acl_get_tag_type() (23.4.19): return the tag type for the ACL181* entry entry_p.182*/183int184acl_get_tag_type(acl_entry_t entry_d, acl_tag_t *tag_type_p)185{186187if (entry_d == NULL || tag_type_p == NULL) {188errno = EINVAL;189return (-1);190}191192*tag_type_p = entry_d->ae_tag;193194return (0);195}196197int198acl_get_entry_type_np(acl_entry_t entry_d, acl_entry_type_t *entry_type_p)199{200201if (entry_d == NULL || entry_type_p == NULL) {202errno = EINVAL;203return (-1);204}205206if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) {207errno = EINVAL;208return (-1);209}210211*entry_type_p = entry_d->ae_entry_type;212213return (0);214}215216217