1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2008, 2009 Edward Tomasz Napierała <trasz@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 __FBSDID("$FreeBSD$"); 31 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <assert.h> 36 #include <err.h> 37 #include <sys/acl.h> 38 #include "acl_support.h" 39 40 struct flagnames_struct { 41 uint32_t flag; 42 const char *name; 43 char letter; 44 }; 45 46 struct flagnames_struct a_flags[] = 47 {{ ACL_ENTRY_FILE_INHERIT, "file_inherit", 'f'}, 48 { ACL_ENTRY_DIRECTORY_INHERIT, "dir_inherit", 'd'}, 49 { ACL_ENTRY_INHERIT_ONLY, "inherit_only", 'i'}, 50 { ACL_ENTRY_NO_PROPAGATE_INHERIT, "no_propagate", 'n'}, 51 { ACL_ENTRY_SUCCESSFUL_ACCESS, "successfull_access", 'S'}, 52 { ACL_ENTRY_FAILED_ACCESS, "failed_access", 'F'}, 53 { ACL_ENTRY_INHERITED, "inherited", 'I' }, 54 /* 55 * There is no ACE_IDENTIFIER_GROUP here - SunOS does not show it 56 * in the "flags" field. There is no ACE_OWNER, ACE_GROUP or 57 * ACE_EVERYONE either, for obvious reasons. 58 */ 59 { 0, 0, 0}}; 60 61 struct flagnames_struct a_access_masks[] = 62 {{ ACL_READ_DATA, "read_data", 'r'}, 63 { ACL_WRITE_DATA, "write_data", 'w'}, 64 { ACL_EXECUTE, "execute", 'x'}, 65 { ACL_APPEND_DATA, "append_data", 'p'}, 66 { ACL_DELETE_CHILD, "delete_child", 'D'}, 67 { ACL_DELETE, "delete", 'd'}, 68 { ACL_READ_ATTRIBUTES, "read_attributes", 'a'}, 69 { ACL_WRITE_ATTRIBUTES, "write_attributes", 'A'}, 70 { ACL_READ_NAMED_ATTRS, "read_xattr", 'R'}, 71 { ACL_WRITE_NAMED_ATTRS, "write_xattr", 'W'}, 72 { ACL_READ_ACL, "read_acl", 'c'}, 73 { ACL_WRITE_ACL, "write_acl", 'C'}, 74 { ACL_WRITE_OWNER, "write_owner", 'o'}, 75 { ACL_SYNCHRONIZE, "synchronize", 's'}, 76 { ACL_FULL_SET, "full_set", '\0'}, 77 { ACL_MODIFY_SET, "modify_set", '\0'}, 78 { ACL_READ_SET, "read_set", '\0'}, 79 { ACL_WRITE_SET, "write_set", '\0'}, 80 { 0, 0, 0}}; 81 82 static const char * 83 format_flag(uint32_t *var, const struct flagnames_struct *flags) 84 { 85 86 for (; flags->name != NULL; flags++) { 87 if ((flags->flag & *var) == 0) 88 continue; 89 90 *var &= ~flags->flag; 91 return (flags->name); 92 } 93 94 return (NULL); 95 } 96 97 static int 98 format_flags_verbose(char *str, size_t size, uint32_t var, 99 const struct flagnames_struct *flags) 100 { 101 size_t off = 0; 102 const char *tmp; 103 104 while ((tmp = format_flag(&var, flags)) != NULL) { 105 off += snprintf(str + off, size - off, "%s/", tmp); 106 assert (off < size); 107 } 108 109 /* If there were any flags added... */ 110 if (off > 0) { 111 off--; 112 /* ... then remove the last slash. */ 113 assert(str[off] == '/'); 114 } 115 116 str[off] = '\0'; 117 118 return (0); 119 } 120 121 static int 122 format_flags_compact(char *str, size_t size, uint32_t var, 123 const struct flagnames_struct *flags) 124 { 125 size_t i; 126 127 for (i = 0; flags[i].letter != '\0'; i++) { 128 assert(i < size); 129 if ((flags[i].flag & var) == 0) 130 str[i] = '-'; 131 else 132 str[i] = flags[i].letter; 133 } 134 135 str[i] = '\0'; 136 137 return (0); 138 } 139 140 static int 141 parse_flags_verbose(const char *strp, uint32_t *var, 142 const struct flagnames_struct *flags, const char *flags_name, 143 int *try_compact) 144 { 145 int i, found, ever_found = 0; 146 char *str, *flag; 147 148 str = strdup(strp); 149 *try_compact = 0; 150 *var = 0; 151 152 while (str != NULL) { 153 flag = strsep(&str, "/:"); 154 155 found = 0; 156 for (i = 0; flags[i].name != NULL; i++) { 157 if (strcmp(flags[i].name, flag) == 0) { 158 *var |= flags[i].flag; 159 found = 1; 160 ever_found = 1; 161 } 162 } 163 164 if (!found) { 165 if (ever_found) 166 warnx("malformed ACL: \"%s\" field contains " 167 "invalid flag \"%s\"", flags_name, flag); 168 else 169 *try_compact = 1; 170 free(str); 171 return (-1); 172 } 173 } 174 175 free(str); 176 return (0); 177 } 178 179 static int 180 parse_flags_compact(const char *str, uint32_t *var, 181 const struct flagnames_struct *flags, const char *flags_name) 182 { 183 int i, j, found; 184 185 *var = 0; 186 187 for (i = 0;; i++) { 188 if (str[i] == '\0') 189 return (0); 190 191 /* Ignore minus signs. */ 192 if (str[i] == '-') 193 continue; 194 195 found = 0; 196 197 for (j = 0; flags[j].name != NULL; j++) { 198 if (flags[j].letter == str[i]) { 199 *var |= flags[j].flag; 200 found = 1; 201 break; 202 } 203 } 204 205 if (!found) { 206 warnx("malformed ACL: \"%s\" field contains " 207 "invalid flag \"%c\"", flags_name, str[i]); 208 return (-1); 209 } 210 } 211 } 212 213 int 214 _nfs4_format_flags(char *str, size_t size, acl_flag_t var, int verbose) 215 { 216 217 if (verbose) 218 return (format_flags_verbose(str, size, var, a_flags)); 219 220 return (format_flags_compact(str, size, var, a_flags)); 221 } 222 223 int 224 _nfs4_format_access_mask(char *str, size_t size, acl_perm_t var, int verbose) 225 { 226 227 if (verbose) 228 return (format_flags_verbose(str, size, var, a_access_masks)); 229 230 return (format_flags_compact(str, size, var, a_access_masks)); 231 } 232 233 int 234 _nfs4_parse_flags(const char *str, acl_flag_t *flags) 235 { 236 int error, try_compact; 237 int tmpflags; 238 239 error = parse_flags_verbose(str, &tmpflags, a_flags, "flags", &try_compact); 240 if (error && try_compact) 241 error = parse_flags_compact(str, &tmpflags, a_flags, "flags"); 242 243 *flags = tmpflags; 244 245 return (error); 246 } 247 248 int 249 _nfs4_parse_access_mask(const char *str, acl_perm_t *perms) 250 { 251 int error, try_compact; 252 int tmpperms; 253 254 error = parse_flags_verbose(str, &tmpperms, a_access_masks, 255 "access permissions", &try_compact); 256 if (error && try_compact) 257 error = parse_flags_compact(str, &tmpperms, 258 a_access_masks, "access permissions"); 259 260 *perms = tmpperms; 261 262 return (error); 263 } 264