Path: blob/main/lib/libc/posix1e/acl_equiv_mode_np.c
39476 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2021 Gleb Popov4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/27/*28* acl_equiv_mode_np: Check if an ACL can be represented as a mode_t.29*/3031#include <sys/types.h>32#include <sys/param.h>33#include <sys/errno.h>34#include <sys/stat.h>35#include <sys/acl.h>3637#include "acl_support.h"3839int40acl_equiv_mode_np(acl_t acl, mode_t *mode_p)41{42mode_t ret_mode = 0;4344if (acl == NULL) {45errno = EINVAL;46return (-1);47}4849/* Linux returns 0 for ACL returned by acl_init() */50if (_acl_brand(acl) == ACL_BRAND_UNKNOWN && acl->ats_acl.acl_cnt == 0)51goto done;5253// TODO: Do we want to handle ACL_BRAND_NFS4 in this function? */54if (_acl_brand(acl) != ACL_BRAND_POSIX)55return (1);5657for (int cur_entry = 0; cur_entry < acl->ats_acl.acl_cnt; cur_entry++) {58acl_entry_t entry = &acl->ats_acl.acl_entry[cur_entry];5960if ((entry->ae_perm & ACL_PERM_BITS) != entry->ae_perm)61return (1);6263switch (entry->ae_tag) {64case ACL_USER_OBJ:65if (entry->ae_perm & ACL_READ)66ret_mode |= S_IRUSR;67if (entry->ae_perm & ACL_WRITE)68ret_mode |= S_IWUSR;69if (entry->ae_perm & ACL_EXECUTE)70ret_mode |= S_IXUSR;71break;72case ACL_GROUP_OBJ:73if (entry->ae_perm & ACL_READ)74ret_mode |= S_IRGRP;75if (entry->ae_perm & ACL_WRITE)76ret_mode |= S_IWGRP;77if (entry->ae_perm & ACL_EXECUTE)78ret_mode |= S_IXGRP;79break;80case ACL_OTHER:81if (entry->ae_perm & ACL_READ)82ret_mode |= S_IROTH;83if (entry->ae_perm & ACL_WRITE)84ret_mode |= S_IWOTH;85if (entry->ae_perm & ACL_EXECUTE)86ret_mode |= S_IXOTH;87break;88default:89return (1);90}91}9293done:94if (mode_p != NULL)95*mode_p = ret_mode;9697return (0);98}99100101