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