Path: blob/main/lib/libc/posix1e/acl_from_mode_np.c
39476 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2021 Robert N M Watson, 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_from_mode_np: Create an ACL from a mode_t.29*/3031#include <sys/types.h>32#include <sys/param.h>33#include <sys/acl.h>34#include <sys/stat.h>3536/*37* return an ACL corresponding to the permissions38* contained in mode_t39*/40acl_t41acl_from_mode_np(const mode_t mode)42{43acl_t acl;44acl_entry_t entry;45acl_permset_t perms;4647/* create the ACL */48acl = acl_init(3);49/* here and below, the only possible reason to fail is ENOMEM, so50* no need to set errno again51*/52if (acl == NULL)53return (NULL);5455/* First entry: ACL_USER_OBJ */56if (acl_create_entry(&acl, &entry) == -1)57return (NULL);58/* TODO: need to handle error there and below? */59acl_set_tag_type(entry, ACL_USER_OBJ);6061acl_get_permset(entry, &perms);62acl_clear_perms(perms);6364/* calculate user mode */65if (mode & S_IRUSR)66acl_add_perm(perms, ACL_READ);67if (mode & S_IWUSR)68acl_add_perm(perms, ACL_WRITE);69if (mode & S_IXUSR)70acl_add_perm(perms, ACL_EXECUTE);7172acl_set_permset(entry, perms);7374/* Second entry: ACL_GROUP_OBJ */75if (acl_create_entry(&acl, &entry) == -1)76return (NULL);77acl_set_tag_type(entry, ACL_GROUP_OBJ);7879acl_get_permset(entry, &perms);80acl_clear_perms(perms);8182/* calculate group mode */83if (mode & S_IRGRP)84acl_add_perm(perms, ACL_READ);85if (mode & S_IWGRP)86acl_add_perm(perms, ACL_WRITE);87if (mode & S_IXGRP)88acl_add_perm(perms, ACL_EXECUTE);8990acl_set_permset(entry, perms);9192/* Third entry: ACL_OTHER */93if (acl_create_entry(&acl, &entry) == -1)94return (NULL);95acl_set_tag_type(entry, ACL_OTHER);9697acl_get_permset(entry, &perms);98acl_clear_perms(perms);99100/* calculate other mode */101if (mode & S_IROTH)102acl_add_perm(perms, ACL_READ);103if (mode & S_IWOTH)104acl_add_perm(perms, ACL_WRITE);105if (mode & S_IXOTH)106acl_add_perm(perms, ACL_EXECUTE);107108acl_set_permset(entry, perms);109110return (acl);111}112113114