Home | History | Annotate | Line # | Download | only in posix1e
      1 /*-
      2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
      3  *
      4  * Copyright (c) 2008, 2009 Edward Tomasz Napieraa <trasz (at) FreeBSD.org>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 #if 0
     31 __FBSDID("$FreeBSD: head/lib/libc/posix1e/acl_support_nfs4.c 326193 2017-11-25 17:12:48Z pfg $");
     32 #else
     33 __RCSID("$NetBSD: acl_support_nfs4.c,v 1.2 2024/01/20 14:52:48 christos Exp $");
     34 #endif
     35 
     36 #include <stdio.h>
     37 #include <stdlib.h>
     38 #include <string.h>
     39 #include <assert.h>
     40 #include <err.h>
     41 #include <sys/acl.h>
     42 #include "acl_support.h"
     43 
     44 struct flagnames_struct {
     45 	uint32_t	flag;
     46 	const char	*name;
     47 	char		letter;
     48 };
     49 
     50 static struct flagnames_struct a_flags[] =
     51     {{ ACL_ENTRY_FILE_INHERIT, "file_inherit", 'f'},
     52      { ACL_ENTRY_DIRECTORY_INHERIT, "dir_inherit", 'd'},
     53      { ACL_ENTRY_INHERIT_ONLY, "inherit_only", 'i'},
     54      { ACL_ENTRY_NO_PROPAGATE_INHERIT, "no_propagate", 'n'},
     55      { ACL_ENTRY_SUCCESSFUL_ACCESS, "successfull_access", 'S'},
     56      { ACL_ENTRY_FAILED_ACCESS, "failed_access", 'F'},
     57      { ACL_ENTRY_INHERITED, "inherited", 'I' },
     58      /*
     59       * There is no ACE_IDENTIFIER_GROUP here - SunOS does not show it
     60       * in the "flags" field.  There is no ACE_OWNER, ACE_GROUP or
     61       * ACE_EVERYONE either, for obvious reasons.
     62       */
     63      { 0, 0, 0}};
     64 
     65 static struct flagnames_struct a_access_masks[] =
     66     {{ ACL_READ_DATA, "read_data", 'r'},
     67      { ACL_WRITE_DATA, "write_data", 'w'},
     68      { ACL_EXECUTE, "execute", 'x'},
     69      { ACL_APPEND_DATA, "append_data", 'p'},
     70      { ACL_DELETE_CHILD, "delete_child", 'D'},
     71      { ACL_DELETE, "delete", 'd'},
     72      { ACL_READ_ATTRIBUTES, "read_attributes", 'a'},
     73      { ACL_WRITE_ATTRIBUTES, "write_attributes", 'A'},
     74      { ACL_READ_NAMED_ATTRS, "read_xattr", 'R'},
     75      { ACL_WRITE_NAMED_ATTRS, "write_xattr", 'W'},
     76      { ACL_READ_ACL, "read_acl", 'c'},
     77      { ACL_WRITE_ACL, "write_acl", 'C'},
     78      { ACL_WRITE_OWNER, "write_owner", 'o'},
     79      { ACL_SYNCHRONIZE, "synchronize", 's'},
     80      { ACL_FULL_SET, "full_set", '\0'},
     81      { ACL_MODIFY_SET, "modify_set", '\0'},
     82      { ACL_READ_SET, "read_set", '\0'},
     83      { ACL_WRITE_SET, "write_set", '\0'},
     84      { 0, 0, 0}};
     85 
     86 static const char *
     87 format_flag(uint32_t *var, const struct flagnames_struct *flags)
     88 {
     89 
     90 	for (; flags->name != NULL; flags++) {
     91 		if ((flags->flag & *var) == 0)
     92 			continue;
     93 
     94 		*var &= ~flags->flag;
     95 		return (flags->name);
     96 	}
     97 
     98 	return (NULL);
     99 }
    100 
    101 static int
    102 format_flags_verbose(char *str, size_t size, uint32_t var,
    103     const struct flagnames_struct *flags)
    104 {
    105 	size_t off = 0;
    106 	const char *tmp;
    107 
    108 	while ((tmp = format_flag(&var, flags)) != NULL) {
    109 		off += snprintf(str + off, size - off, "%s/", tmp);
    110 		assert (off < size);
    111 	}
    112 
    113 	/* If there were any flags added... */
    114 	if (off > 0) {
    115 		off--;
    116 		/* ... then remove the last slash. */
    117 		assert(str[off] == '/');
    118 	}
    119 
    120 	str[off] = '\0';
    121 
    122 	return (0);
    123 }
    124 
    125 static int
    126 format_flags_compact(char *str, size_t size, uint32_t var,
    127     const struct flagnames_struct *flags)
    128 {
    129 	size_t i;
    130 
    131 	for (i = 0; flags[i].letter != '\0'; i++) {
    132 		assert(i < size);
    133 		if ((flags[i].flag & var) == 0)
    134 			str[i] = '-';
    135 		else
    136 			str[i] = flags[i].letter;
    137 	}
    138 
    139 	str[i] = '\0';
    140 
    141 	return (0);
    142 }
    143 
    144 static int
    145 parse_flags_verbose(const char *strp, uint32_t *var,
    146     const struct flagnames_struct *flags, const char *flags_name,
    147     int *try_compact)
    148 {
    149 	int i, found, ever_found = 0;
    150 	char *str, *flag;
    151 
    152 	str = strdup(strp);
    153 	*try_compact = 0;
    154 	*var = 0;
    155 
    156 	while (str != NULL) {
    157 		flag = strsep(&str, "/:");
    158 
    159 		found = 0;
    160 		for (i = 0; flags[i].name != NULL; i++) {
    161 			if (strcmp(flags[i].name, flag) == 0) {
    162 				*var |= flags[i].flag;
    163 				found = 1;
    164 				ever_found = 1;
    165 			}
    166 		}
    167 
    168 		if (!found) {
    169 			if (ever_found)
    170 				warnx("malformed ACL: \"%s\" field contains "
    171 				    "invalid flag \"%s\"", flags_name, flag);
    172 			else
    173 				*try_compact = 1;
    174 			free(str);
    175 			return (-1);
    176 		}
    177 	}
    178 
    179 	free(str);
    180 	return (0);
    181 }
    182 
    183 static int
    184 parse_flags_compact(const char *str, uint32_t *var,
    185     const struct flagnames_struct *flags, const char *flags_name)
    186 {
    187 	int i, j, found;
    188 
    189 	*var = 0;
    190 
    191 	for (i = 0;; i++) {
    192 		if (str[i] == '\0')
    193 			return (0);
    194 
    195 		/* Ignore minus signs. */
    196 		if (str[i] == '-')
    197 			continue;
    198 
    199 		found = 0;
    200 
    201 		for (j = 0; flags[j].name != NULL; j++) {
    202 			if (flags[j].letter == str[i]) {
    203 				*var |= flags[j].flag;
    204 				found = 1;
    205 				break;
    206 			}
    207 		}
    208 
    209 		if (!found) {
    210 			warnx("malformed ACL: \"%s\" field contains "
    211 			    "invalid flag \"%c\"", flags_name, str[i]);
    212 			return (-1);
    213 		}
    214 	}
    215 }
    216 
    217 int
    218 _nfs4_format_flags(char *str, size_t size, acl_flag_t var, int verbose)
    219 {
    220 
    221 	if (verbose)
    222 		return (format_flags_verbose(str, size, var, a_flags));
    223 
    224 	return (format_flags_compact(str, size, var, a_flags));
    225 }
    226 
    227 int
    228 _nfs4_format_access_mask(char *str, size_t size, acl_perm_t var, int verbose)
    229 {
    230 
    231 	if (verbose)
    232 		return (format_flags_verbose(str, size, var, a_access_masks));
    233 
    234 	return (format_flags_compact(str, size, var, a_access_masks));
    235 }
    236 
    237 int
    238 _nfs4_parse_flags(const char *str, acl_flag_t *flags)
    239 {
    240 	int error, try_compact;
    241 	unsigned int tmpflags;
    242 
    243 	error = parse_flags_verbose(str, &tmpflags, a_flags, "flags", &try_compact);
    244 	if (error && try_compact)
    245 		error = parse_flags_compact(str, &tmpflags, a_flags, "flags");
    246 
    247 	*flags = tmpflags;
    248 
    249 	return (error);
    250 }
    251 
    252 int
    253 _nfs4_parse_access_mask(const char *str, acl_perm_t *perms)
    254 {
    255 	int error, try_compact;
    256 	unsigned int tmpperms;
    257 
    258 	error = parse_flags_verbose(str, &tmpperms, a_access_masks,
    259 	    "access permissions", &try_compact);
    260 	if (error && try_compact)
    261 		error = parse_flags_compact(str, &tmpperms,
    262 		    a_access_masks, "access permissions");
    263 
    264 	*perms = tmpperms;
    265 
    266 	return (error);
    267 }
    268