Index: bin/getfacl/getfacl.c =================================================================== --- bin/getfacl/getfacl.c +++ bin/getfacl/getfacl.c @@ -83,97 +83,6 @@ return (gr->gr_name); } -/* - * return an ACL corresponding to the permissions - * contained in mode_t - */ -static acl_t -acl_from_mode(const mode_t mode) -{ - acl_t acl; - acl_entry_t entry; - acl_permset_t perms; - - /* create the ACL */ - acl = acl_init(3); - if (!acl) - return NULL; - - /* First entry: ACL_USER_OBJ */ - if (acl_create_entry(&acl, &entry) == -1) - return NULL; - if (acl_set_tag_type(entry, ACL_USER_OBJ) == -1) - return NULL; - - if (acl_get_permset(entry, &perms) == -1) - return NULL; - if (acl_clear_perms(perms) == -1) - return NULL; - - /* calculate user mode */ - if (mode & S_IRUSR) - if (acl_add_perm(perms, ACL_READ) == -1) - return NULL; - if (mode & S_IWUSR) - if (acl_add_perm(perms, ACL_WRITE) == -1) - return NULL; - if (mode & S_IXUSR) - if (acl_add_perm(perms, ACL_EXECUTE) == -1) - return NULL; - if (acl_set_permset(entry, perms) == -1) - return NULL; - - /* Second entry: ACL_GROUP_OBJ */ - if (acl_create_entry(&acl, &entry) == -1) - return NULL; - if (acl_set_tag_type(entry, ACL_GROUP_OBJ) == -1) - return NULL; - - if (acl_get_permset(entry, &perms) == -1) - return NULL; - if (acl_clear_perms(perms) == -1) - return NULL; - - /* calculate group mode */ - if (mode & S_IRGRP) - if (acl_add_perm(perms, ACL_READ) == -1) - return NULL; - if (mode & S_IWGRP) - if (acl_add_perm(perms, ACL_WRITE) == -1) - return NULL; - if (mode & S_IXGRP) - if (acl_add_perm(perms, ACL_EXECUTE) == -1) - return NULL; - if (acl_set_permset(entry, perms) == -1) - return NULL; - - /* Third entry: ACL_OTHER */ - if (acl_create_entry(&acl, &entry) == -1) - return NULL; - if (acl_set_tag_type(entry, ACL_OTHER) == -1) - return NULL; - - if (acl_get_permset(entry, &perms) == -1) - return NULL; - if (acl_clear_perms(perms) == -1) - return NULL; - - /* calculate other mode */ - if (mode & S_IROTH) - if (acl_add_perm(perms, ACL_READ) == -1) - return NULL; - if (mode & S_IWOTH) - if (acl_add_perm(perms, ACL_WRITE) == -1) - return NULL; - if (mode & S_IXOTH) - if (acl_add_perm(perms, ACL_EXECUTE) == -1) - return NULL; - if (acl_set_permset(entry, perms) == -1) - return NULL; - - return(acl); -} - static int print_acl(char *path, acl_type_t type, int hflag, int iflag, int nflag, int qflag, int vflag) @@ -229,7 +138,7 @@ errno = 0; if (type == ACL_TYPE_DEFAULT) return(0); - acl = acl_from_mode(sb.st_mode); + acl = acl_from_mode_np(sb.st_mode); if (!acl) { warn("%s: acl_from_mode() failed", path); return(-1); Index: lib/libc/posix1e/Makefile.inc =================================================================== --- lib/libc/posix1e/Makefile.inc +++ lib/libc/posix1e/Makefile.inc @@ -11,13 +11,17 @@ CONFS+= posix1e/mac.conf SRCS+= acl_branding.c \ acl_calc_mask.c \ + acl_cmp_np.c \ acl_compat.c \ acl_copy.c \ acl_delete.c \ acl_delete_entry.c \ acl_entry.c \ + acl_equiv_mode_np.c \ + acl_extended_file_np.c \ acl_flag.c \ acl_free.c \ + acl_from_mode_np.c \ acl_from_text.c \ acl_from_text_nfs4.c \ acl_get.c \ @@ -46,6 +50,7 @@ acl_calc_mask.3 \ acl_clear_flags_np.3 \ acl_clear_perms.3 \ + acl_cmp_np.3 \ acl_copy_entry.3 \ acl_create_entry.3 \ acl_delete.3 \ @@ -53,7 +58,10 @@ acl_delete_flag_np.3 \ acl_delete_perm.3 \ acl_dup.3 \ + acl_equiv_mode_np.3 \ + acl_extended_file_np.3 \ acl_free.3 \ + acl_from_mode_np.3 \ acl_from_text.3 \ acl_get.3 \ acl_get_brand_np.3 \ @@ -92,6 +100,8 @@ acl_delete.3 acl_delete_file_np.3 \ acl_delete.3 acl_delete_fd_np.3 \ acl_delete_entry.3 acl_delete_entry_np.3\ + acl_extended_file_np.3 acl_extended_file_nofollow_np.3 \ + acl_extended_file_np.3 acl_extended_link_np.3 \ acl_get.3 acl_get_file.3 \ acl_get.3 acl_get_fd.3 \ acl_get.3 acl_get_fd_np.3 \ Index: lib/libc/posix1e/Symbol.map =================================================================== --- lib/libc/posix1e/Symbol.map +++ lib/libc/posix1e/Symbol.map @@ -84,3 +84,12 @@ acl_strip_np; acl_to_text_np; }; + +FBSD_1.7 { + acl_cmp_np; + acl_equiv_mode_np; + acl_extended_file_np; + acl_extended_file_nofollow_np; + acl_extended_link_np; + acl_from_mode_np; +}; Index: lib/libc/posix1e/acl_cmp_np.3 =================================================================== --- /dev/null +++ lib/libc/posix1e/acl_cmp_np.3 @@ -0,0 +1,83 @@ +.\"- +.\" Copyright (c) 2021 Gleb Popov +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.Dd January 20, 2021 +.Dt ACL_CMP_NP 3 +.Os +.Sh NAME +.Nm acl_cmp +.Nd compare between two ACLs +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/types.h +.In sys/acl.h +.Ft int +.Fn acl_cmp_np "acl_t acl1" "acl_t acl2" +.Sh DESCRIPTION +The +.Fn acl_cmp_np +function is a non-portable call that checks if ACLs pointed to by +.Va acl1 +and +.Va acl2 +are equivalent. +The two ACLs are considered equal when they contain the same +entries with matching tag types, qualifiers and permissions. +.Sh RETURN VALUES +Upon successful completion, this function returns 0 if the given ACLs are +equivalent and 1 if they differ. +Otherwise, the value -1 is returned, and +.Va errno +indicates the error. +.Sh ERRORS +If any of the following conditions occur, the +.Fn acl_cmp_np +function shall return a value of +.Va -1 +and set +.Va errno +to the corresponding value: +.Bl -tag -width Er +.It Bq Er EINVAL +Either first or second argument does not point to a valid ACL. +.Sh SEE ALSO +.Xr acl 3 , +.Xr posix1e 3 +.Sh STANDARDS +POSIX.1e is described in IEEE POSIX.1e draft 17. +Discussion +of the draft continues on the cross-platform POSIX.1e implementation +mailing list. +To join this list, see the +.Fx +POSIX.1e implementation +page for more information. +.Sh HISTORY +POSIX.1e support was introduced in +.Fx 4.0 , +and development continues. +.Sh AUTHORS +.An Gleb Popov Index: lib/libc/posix1e/acl_cmp_np.c =================================================================== --- /dev/null +++ lib/libc/posix1e/acl_cmp_np.c @@ -0,0 +1,55 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2021 Gleb Popov + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * acl_cmp_np: Compare two ACL's. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include "acl_support.h" + +/* + * returns 0 if acl_t's are identical, 1 otherwise + */ +int +acl_cmp_np(acl_t acl1, acl_t acl2) +{ + if (acl1 == NULL || acl2 == NULL) + { + errno = EINVAL; + return (-1); + } + + if (_acl_brand(acl1) != _acl_brand(acl2)) + return (1); + + return _acl_differs(acl1, acl2); +} Index: lib/libc/posix1e/acl_equiv_mode_np.3 =================================================================== --- /dev/null +++ lib/libc/posix1e/acl_equiv_mode_np.3 @@ -0,0 +1,95 @@ +.\"- +.\" Copyright (c) 2021 Gleb Popov +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.Dd January 20, 2021 +.Dt ACL_EQUIV_MODE_NP 3 +.Os +.Sh NAME +.Nm acl_equiv_mode_np +.Nd check if ACL can be represented as UNIX permissions +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/types.h +.In sys/acl.h +.Ft int +.Fn acl_equiv_mode_np "acl_t acl" "mode_t* mode_p" +.Sh DESCRIPTION +The +.Fn acl_equiv_mode_np +function is a non-portable call which checks that entries contained in the +.Va acl +argument use only the ACL_USER_OBJ, ACL_GROUP_OBJ, and ACL_OTHER tag types, and that +permissions contained in these entries consist only of ACL_READ, ACL_WRITE and +ACL_EXECUTE. +If the check succeeds, the ACL can be represented as traditional UNIX +file permissions bits set. +.Pp +If +.Va mode_p +is not NULL and the check succeeds, the function fills the argument with a mode +value corresponding to permissions contained in the ACL. +.Sh RETURN VALUES +Upon successful completion, the function returns 0 if the ACL can be represented +as UNIX permissions and 1 if it is not possible. +Otherwise, a value +of +.Va -1 +is returned, and +.Va errno +is set to indicate the error. +.Sh ERRORS +If any of the following conditions occur, the +.Fn acl_equiv_mode_np +function returns a value of +.Va -1 +and set +.Va errno +to the corresponding value: +.Bl -tag -width Er +.It Bq Er EINVAL +Argument +.Fa acl +does not point to a valid ACL. +.El +.Sh SEE ALSO +.Xr acl 3 , +.Xr acl_from_mode_np 3 , +.Xr posix1e 3 +.Sh STANDARDS +POSIX.1e is described in IEEE POSIX.1e draft 17. +Discussion +of the draft continues on the cross-platform POSIX.1e implementation +mailing list. +To join this list, see the +.Fx +POSIX.1e implementation +page for more information. +.Sh HISTORY +POSIX.1e support was introduced in +.Fx 4.0 , +and development continues. +.Sh AUTHORS +.An Gleb Popov Index: lib/libc/posix1e/acl_equiv_mode_np.c =================================================================== --- /dev/null +++ lib/libc/posix1e/acl_equiv_mode_np.c @@ -0,0 +1,108 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2021 Gleb Popov + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * acl_equiv_mode_np: Check if an ACL can be represented as a mode_t. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include "acl_support.h" + +int +acl_equiv_mode_np(acl_t acl, mode_t *mode_p) +{ + int cur_entry = 0; + mode_t ret_mode = 0; + + if (acl == NULL) + { + errno = EINVAL; + return -1; + } + + /* Linux returns 0 for ACL returned by acl_init() */ + if (_acl_brand(acl) == ACL_BRAND_UNKNOWN && acl->ats_acl.acl_cnt == 0) + { + return 0; + } + + // TODO: Do we want to handle ACL_BRAND_NFS4 in this function? */ + if (_acl_brand(acl) != ACL_BRAND_POSIX) + { + return 1; + } + + while (cur_entry < acl->ats_acl.acl_cnt) + { + acl_entry_t entry = &acl->ats_acl.acl_entry[cur_entry++]; + + if ((entry->ae_perm & ACL_PERM_BITS) != entry->ae_perm) + return 1; + + switch (entry->ae_tag) + { + case ACL_USER_OBJ: + if (entry->ae_perm & ACL_READ) + ret_mode |= S_IRUSR; + if (entry->ae_perm & ACL_WRITE) + ret_mode |= S_IWUSR; + if (entry->ae_perm & ACL_EXECUTE) + ret_mode |= S_IXUSR; + continue; + case ACL_GROUP_OBJ: + if (entry->ae_perm & ACL_READ) + ret_mode |= S_IRGRP; + if (entry->ae_perm & ACL_WRITE) + ret_mode |= S_IWGRP; + if (entry->ae_perm & ACL_EXECUTE) + ret_mode |= S_IXGRP; + continue; + case ACL_OTHER: + if (entry->ae_perm & ACL_READ) + ret_mode |= S_IROTH; + if (entry->ae_perm & ACL_WRITE) + ret_mode |= S_IWOTH; + if (entry->ae_perm & ACL_EXECUTE) + ret_mode |= S_IXOTH; + continue; + default: + return 1; + } + } + + if (mode_p != NULL) + *mode_p = ret_mode; + + return 0; +} Index: lib/libc/posix1e/acl_extended_file_np.3 =================================================================== --- /dev/null +++ lib/libc/posix1e/acl_extended_file_np.3 @@ -0,0 +1,95 @@ +.\"- +.\" Copyright (c) 2021 Gleb Popov +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.Dd February 26, 2021 +.Dt ACL_EXTENDED_FILE_NP 3 +.Os +.Sh NAME +.Nm acl_extended_file_np , +.Nm acl_extended_file_nofollow_np , +.Nm acl_extended_link_np +.Nd checks if the file has extended ACLs set +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/types.h +.In sys/acl.h +.Ft int +.Fn acl_extended_file_np "const char* path_p" +.Ft int +.Fn acl_extended_file_nofollow_np "const char* path_p" +.Ft int +.Fn acl_extended_link_np "const char* path_p" +.Sh DESCRIPTION +The +.Fn acl_extended_file_np +function is a non-portable call that checks if the file or directory referred to +by the argument +.Va path_p +contains extended access ACLs. The +.Fn acl_extended_file_nofollow_np +function works the same way, except it does not follow symlinks. The +.Fn acl_extended_link_np +function is a synonim to +.Fn acl_extended_file_nofollow_np +named in FreeBSD style. +An ACL is considered to be extended access one if it contains entries other +than the three required entries of tag types ACL_USER_OBJ, ACL_GROUP_OBJ and +ACL_OTHER. +.Sh RETURN VALUES +Upon successful completion, this function returns 0 if the file object does not +contain extended access ACLs and 1 in the other case. +Otherwise, the value -1 is returned, and +.Va errno +indicates the error. +.Sh ERRORS +If any of the following conditions occur, the +.Fn acl_extended_file_np +function shall return a value of +.Va -1 +and set +.Va errno +to the corresponding value: +.Bl -tag -width Er +.It Bq Er EACCES +Search permission is denied for a component of the path prefix. +.Sh SEE ALSO +.Xr extattr_get_file 2 , +.Xr posix1e 3 +.Sh STANDARDS +POSIX.1e is described in IEEE POSIX.1e draft 17. +Discussion +of the draft continues on the cross-platform POSIX.1e implementation +mailing list. +To join this list, see the +.Fx +POSIX.1e implementation +page for more information. +.Sh HISTORY +POSIX.1e support was introduced in +.Fx 4.0 , +and development continues. +.Sh AUTHORS +.An Gleb Popov Index: lib/libc/posix1e/acl_extended_file_np.c =================================================================== --- /dev/null +++ lib/libc/posix1e/acl_extended_file_np.c @@ -0,0 +1,89 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2021 Gleb Popov + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * acl_extended_file_np: Check if the file has extended ACLs set. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include + +#include +#include +#include +#include + +typedef acl_t (*acl_get_func)(const char *, acl_type_t); +typedef long (*pathconf_func)(const char *, int); + +static int +_acl_extended_file(acl_get_func f, pathconf_func pathconf_f, const char* path_p); + +int +acl_extended_file_np(const char *path_p) +{ + return _acl_extended_file(acl_get_file, pathconf, path_p); +} + +int +acl_extended_file_nofollow_np(const char *path_p) +{ + return _acl_extended_file(acl_get_link_np, lpathconf, path_p); +} + +int +acl_extended_link_np(const char *path_p) +{ + return _acl_extended_file(acl_get_link_np, lpathconf, path_p); +} + +int +_acl_extended_file(acl_get_func acl_get, pathconf_func pathconf_f, const char* path_p) +{ + acl_t acl; + int retval, istrivial, acltype = ACL_TYPE_ACCESS; + + retval = pathconf_f(path_p, _PC_ACL_NFS4); + if (retval > 0) + acltype = ACL_TYPE_NFS4; + + acl = acl_get(path_p, acltype); + if (acl == NULL) + return -1; + + retval = acl_is_trivial_np(acl, &istrivial); + if (retval == -1) + { + acl_free(acl); + return -1; + } + + acl_free(acl); + return !istrivial; +} Index: lib/libc/posix1e/acl_from_mode_np.3 =================================================================== --- /dev/null +++ lib/libc/posix1e/acl_from_mode_np.3 @@ -0,0 +1,95 @@ +.\"- +.\" Copyright (c) 2021 Gleb Popov +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.Dd January 20, 2021 +.Dt ACL_FROM_MODE_NP 3 +.Os +.Sh NAME +.Nm acl_from_mode_np +.Nd create an ACL from status information +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/types.h +.In sys/acl.h +.Ft acl_t +.Fn acl_from_mode_np "const mode_t mode" +.Sh DESCRIPTION +The +.Fn acl_from_mode_np +function is a non-portable call that converts the permissions set referred to by +.Va mode +into the corresponding minimal ACL structure, appropriate for applying to +files or manipulating. +.Pp +This function causes memory to be allocated. +The caller should free any +free-able memory, when the new ACL is no longer required, by calling +.Xr acl_free 3 +with the +.Va (void *)acl_t +as an argument. +.Sh RETURN VALUES +Upon successful completion, the function returns a pointer to the +internal representation of the ACL in working storage. +Otherwise, a value +of +.Va (acl_t)NULL +is returned, and +.Va errno +is set to indicate the error. +.Sh ERRORS +If any of the following conditions occur, the +.Fn acl_from_mode_np +function returns a value of +.Va (acl_t)NULL +and set +.Va errno +to the corresponding value: +.Bl -tag -width Er +.It Bq Er ENOMEM +The ACL working storage requires more memory than is allowed by the +hardware or system-imposed memory management constraints. +.El +.Sh SEE ALSO +.Xr acl 3 , +.Xr acl_free 3 , +.Xr acl_from_text 3 , +.Xr posix1e 3 +.Sh STANDARDS +POSIX.1e is described in IEEE POSIX.1e draft 17. +Discussion +of the draft continues on the cross-platform POSIX.1e implementation +mailing list. +To join this list, see the +.Fx +POSIX.1e implementation +page for more information. +.Sh HISTORY +POSIX.1e support was introduced in +.Fx 4.0 , +and development continues. +.Sh AUTHORS +.An Gleb Popov Index: lib/libc/posix1e/acl_from_mode_np.c =================================================================== --- /dev/null +++ lib/libc/posix1e/acl_from_mode_np.c @@ -0,0 +1,115 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2021 Robert N M Watson, Gleb Popov + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * acl_from_mode_np: Create an ACL from a mode_t. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include + +/* + * return an ACL corresponding to the permissions + * contained in mode_t + */ +acl_t +acl_from_mode_np(const mode_t mode) +{ + acl_t acl; + acl_entry_t entry; + acl_permset_t perms; + + /* create the ACL */ + acl = acl_init(3); + /* here and below, the only possible reason to fail is ENOMEM, so + * no need to set errno again + */ + if (!acl) + return NULL; + + /* First entry: ACL_USER_OBJ */ + if (acl_create_entry(&acl, &entry) == -1) + return NULL; + /* TODO: need to handle error there and below? */ + acl_set_tag_type(entry, ACL_USER_OBJ); + + acl_get_permset(entry, &perms); + acl_clear_perms(perms); + + /* calculate user mode */ + if (mode & S_IRUSR) + acl_add_perm(perms, ACL_READ); + if (mode & S_IWUSR) + acl_add_perm(perms, ACL_WRITE); + if (mode & S_IXUSR) + acl_add_perm(perms, ACL_EXECUTE); + + acl_set_permset(entry, perms); + + /* Second entry: ACL_GROUP_OBJ */ + if (acl_create_entry(&acl, &entry) == -1) + return NULL; + acl_set_tag_type(entry, ACL_GROUP_OBJ); + + acl_get_permset(entry, &perms); + acl_clear_perms(perms); + + /* calculate group mode */ + if (mode & S_IRGRP) + acl_add_perm(perms, ACL_READ); + if (mode & S_IWGRP) + acl_add_perm(perms, ACL_WRITE); + if (mode & S_IXGRP) + acl_add_perm(perms, ACL_EXECUTE); + + acl_set_permset(entry, perms); + + /* Third entry: ACL_OTHER */ + if (acl_create_entry(&acl, &entry) == -1) + return NULL; + acl_set_tag_type(entry, ACL_OTHER); + + acl_get_permset(entry, &perms); + acl_clear_perms(perms); + + /* calculate other mode */ + if (mode & S_IROTH) + acl_add_perm(perms, ACL_READ); + if (mode & S_IWOTH) + acl_add_perm(perms, ACL_WRITE); + if (mode & S_IXOTH) + acl_add_perm(perms, ACL_EXECUTE); + + acl_set_permset(entry, perms); + + return(acl); +} Index: lib/libc/posix1e/acl_support.c =================================================================== --- lib/libc/posix1e/acl_support.c +++ lib/libc/posix1e/acl_support.c @@ -62,8 +62,6 @@ struct acl_entry *entrya, *entryb; assert(_acl_brand(a) == _acl_brand(b)); - assert(_acl_brand(a) != ACL_BRAND_UNKNOWN); - assert(_acl_brand(b) != ACL_BRAND_UNKNOWN); if (a->ats_acl.acl_cnt != b->ats_acl.acl_cnt) return (1); Index: sys/sys/acl.h =================================================================== --- sys/sys/acl.h +++ sys/sys/acl.h @@ -30,7 +30,7 @@ * * $FreeBSD$ */ -/* +/* * Developed by the TrustedBSD Project. * Support for POSIX.1e and NFSv4 access control lists. */ @@ -363,6 +363,7 @@ int acl_calc_mask(acl_t *_acl_p); int acl_clear_flags_np(acl_flagset_t _flagset_d); int acl_clear_perms(acl_permset_t _permset_d); +int acl_cmp_np(acl_t _acl1, acl_t _acl2); int acl_copy_entry(acl_entry_t _dest_d, acl_entry_t _src_d); ssize_t acl_copy_ext(void *_buf_p, acl_t _acl, ssize_t _size); acl_t acl_copy_int(const void *_buf_p); @@ -378,7 +379,12 @@ int acl_delete_flag_np(acl_flagset_t _flagset_d, acl_flag_t _flag); int acl_delete_perm(acl_permset_t _permset_d, acl_perm_t _perm); acl_t acl_dup(acl_t _acl); +int acl_equiv_mode_np(acl_t _acl, mode_t *_mode_p); +int acl_extended_file_np(const char* _path_p); +int acl_extended_file_nofollow_np(const char* _path_p); +int acl_extended_link_np(const char* _path_p); int acl_free(void *_obj_p); +acl_t acl_from_mode_np(const mode_t _mode); acl_t acl_from_text(const char *_buf_p); int acl_get_brand_np(acl_t _acl, int *_brand_p); int acl_get_entry(acl_t _acl, int _entry_id, acl_entry_t *_entry_p); Index: tests/sys/acl/Makefile =================================================================== --- tests/sys/acl/Makefile +++ tests/sys/acl/Makefile @@ -14,6 +14,8 @@ SCRIPTS+= run +ATF_TESTS_C+= acl-api-test + TAP_TESTS_SH+= 00 TAP_TESTS_SH+= 01 TAP_TESTS_SH+= 02 Index: tests/sys/acl/acl-api-test.c =================================================================== --- /dev/null +++ tests/sys/acl/acl-api-test.c @@ -0,0 +1,194 @@ +/*- + * Copyright (c) 2021 Gleb Popov + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include + +#include +#include + +#include + +/* Compatibility shim to make it possible to run this test on Linux */ +#ifdef __linux__ +#include +#define acl_from_mode_np acl_from_mode +#define acl_equiv_mode_np acl_equiv_mode +#define acl_cmp_np acl_cmp +#endif + +static mode_t all_modes[] = { + S_IRUSR, + S_IWUSR, + S_IXUSR, + S_IRGRP, + S_IWGRP, + S_IXGRP, + S_IROTH, + S_IWOTH, + S_IXOTH +}; + +static mode_t gen_random_mode(void) +{ + mode_t mode = 0; + + for (unsigned i = 0; i < sizeof(all_modes) / sizeof(mode_t); i++) + { + if (rand() % 2) + mode |= all_modes[i]; + } + + return mode; +} + +/* Generate a random mode_t, produce an acl_t from it, + * then use acl_equiv_mode_np to produce a mode_t again. + * The call should succeed and mode_t's should be equal + */ +ATF_TC_WITHOUT_HEAD(acl_mode_roundup); +ATF_TC_BODY(acl_mode_roundup, tc) +{ + int num_tests = 100; + while(num_tests--) + { + mode_t src_mode, equiv_mode; + acl_t acl; + + src_mode = gen_random_mode(); + + acl = acl_from_mode_np(src_mode); + ATF_REQUIRE(acl != NULL); + + ATF_CHECK_EQ(0, acl_equiv_mode_np(acl, &equiv_mode)); + ATF_CHECK_EQ(src_mode, equiv_mode); + + acl_free(acl); + } +} + +/* Successfull acl_equiv_mode_np calls are tested in acl_mode_roundup. + * Here some specific cases are tested. + */ +ATF_TC_WITHOUT_HEAD(acl_equiv_mode_test); +ATF_TC_BODY(acl_equiv_mode_test, tc) +{ + acl_t acl; + acl_entry_t entry; + mode_t mode; + int uid = 0; + + acl = acl_init(1); + ATF_REQUIRE(acl != NULL); + + /* empty acl maps to 0000 UNIX mode */ + ATF_CHECK_EQ(0, acl_equiv_mode_np(acl, &mode)); + ATF_CHECK_EQ(0, mode); + +#ifndef __linux__ + /* NFS-branded acl's can't be converted to UNIX mode */ + ATF_REQUIRE_EQ(0, acl_create_entry(&acl, &entry)); + ATF_REQUIRE_EQ(0, acl_set_tag_type(entry, ACL_EVERYONE)); + ATF_CHECK_EQ(1, acl_equiv_mode_np(acl, &mode)); +#endif + + /* acl's with qualified user entries can't be converted to UNIX mode */ + acl_free(acl); + acl = acl_init(1); + ATF_REQUIRE(acl != NULL); + ATF_REQUIRE_EQ(0, acl_create_entry(&acl, &entry)); + ATF_REQUIRE_EQ(0, acl_set_tag_type(entry, ACL_USER)); + ATF_REQUIRE_EQ(0, acl_set_qualifier(entry, &uid)); + ATF_CHECK_EQ(1, acl_equiv_mode_np(acl, &mode)); + + /* passing NULL causes EINVAL */ + ATF_CHECK_ERRNO(EINVAL, acl_equiv_mode_np(NULL, &mode)); +} + +ATF_TC_WITHOUT_HEAD(acl_cmp_test); +ATF_TC_BODY(acl_cmp_test, tc) +{ + acl_t empty_acl, acl1, acl2; + acl_entry_t entry; + acl_permset_t perms; + + empty_acl = acl_init(1); + ATF_REQUIRE(empty_acl != NULL); + + acl1 = acl_init(3); + ATF_REQUIRE(acl1 != NULL); + + /* first, check that two empty acls are equal */ + ATF_CHECK_EQ(0, acl_cmp_np(acl1, empty_acl)); + + /* now create an entry and compare against empty acl */ + ATF_REQUIRE_EQ(0, acl_create_entry(&acl1, &entry)); + ATF_REQUIRE_EQ(0, acl_set_tag_type(entry, ACL_USER_OBJ)); + ATF_REQUIRE_EQ(0, acl_get_permset(entry, &perms)); + ATF_REQUIRE_EQ(0, acl_clear_perms(perms)); + ATF_REQUIRE_EQ(0, acl_add_perm(perms, ACL_READ)); + ATF_CHECK_EQ(1, acl_cmp_np(empty_acl, acl1)); + + /* make a dup of non-empty acl and check that they are equal */ + acl2 = acl_dup(acl1); + ATF_REQUIRE(acl2 != NULL); + ATF_CHECK_EQ(0, acl_cmp_np(acl1, acl2)); + + /* change the tag type and compare */ + ATF_REQUIRE_EQ(1, acl_get_entry(acl1, ACL_FIRST_ENTRY, &entry)); + ATF_REQUIRE_EQ(0, acl_set_tag_type(entry, ACL_GROUP_OBJ)); + ATF_CHECK_EQ(1, acl_cmp_np(acl1, acl2)); + + /* change the permset and compare */ + acl_free(acl2); + acl2 = acl_dup(acl1); + ATF_REQUIRE(acl2 != NULL); + ATF_REQUIRE_EQ(1, acl_get_entry(acl1, ACL_FIRST_ENTRY, &entry)); + ATF_REQUIRE_EQ(0, acl_get_permset(entry, &perms)); + ATF_REQUIRE_EQ(0, acl_clear_perms(perms)); + ATF_CHECK_EQ(1, acl_cmp_np(acl1, acl2)); + + /* check that passing NULL yields EINVAL */ + ATF_CHECK_ERRNO(EINVAL, acl_cmp_np(NULL, NULL)); + ATF_CHECK_ERRNO(EINVAL, acl_cmp_np(acl1, NULL)); + ATF_CHECK_ERRNO(EINVAL, acl_cmp_np(NULL, acl1)); + + acl_free(empty_acl); + acl_free(acl1); + acl_free(acl2); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, acl_mode_roundup); + ATF_TP_ADD_TC(tp, acl_equiv_mode_test); + ATF_TP_ADD_TC(tp, acl_cmp_test); + + return (atf_no_error()); +}