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