1 /*- 2 * Copyright (c) 2008, 2009 Edward Tomasz Napierała <trasz@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <stdio.h> 31 #include <stdlib.h> 32 #include <string.h> 33 #include <assert.h> 34 #include <err.h> 35 #include <sys/acl.h> 36 #include "acl_support.h" 37 38 struct flagnames_struct { 39 uint32_t flag; 40 const char *name; 41 char letter; 42 }; 43 44 struct flagnames_struct a_flags[] = 45 {{ ACL_ENTRY_FILE_INHERIT, "file_inherit", 'f'}, 46 { ACL_ENTRY_DIRECTORY_INHERIT, "dir_inherit", 'd'}, 47 { ACL_ENTRY_INHERIT_ONLY, "inherit_only", 'i'}, 48 { ACL_ENTRY_NO_PROPAGATE_INHERIT, "no_propagate", 'n'}, 49 { ACL_ENTRY_SUCCESSFUL_ACCESS, "successfull_access", 'S'}, 50 { ACL_ENTRY_FAILED_ACCESS, "failed_access", 'F'}, 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 != 0; 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; 144 145 str = strdup(strp); 146 *try_compact = 0; 147 *var = 0; 148 149 while (str != NULL) { 150 flag = strsep(&str, "/:"); 151 152 found = 0; 153 for (i = 0; flags[i].name != NULL; i++) { 154 if (strcmp(flags[i].name, flag) == 0) { 155 *var |= flags[i].flag; 156 found = 1; 157 ever_found = 1; 158 } 159 } 160 161 if (!found) { 162 if (ever_found) 163 warnx("malformed ACL: \"%s\" field contains " 164 "invalid flag \"%s\"", flags_name, flag); 165 else 166 *try_compact = 1; 167 free(str); 168 return (-1); 169 } 170 } 171 172 free(str); 173 return (0); 174 } 175 176 static int 177 parse_flags_compact(const char *str, uint32_t *var, 178 const struct flagnames_struct *flags, const char *flags_name) 179 { 180 int i, j, found; 181 182 *var = 0; 183 184 for (i = 0;; i++) { 185 if (str[i] == '\0') 186 return (0); 187 188 /* Ignore minus signs. */ 189 if (str[i] == '-') 190 continue; 191 192 found = 0; 193 194 for (j = 0; flags[j].name != NULL; j++) { 195 if (flags[j].letter == str[i]) { 196 *var |= flags[j].flag; 197 found = 1; 198 break; 199 } 200 } 201 202 if (!found) { 203 warnx("malformed ACL: \"%s\" field contains " 204 "invalid flag \"%c\"", flags_name, str[i]); 205 return (-1); 206 } 207 } 208 } 209 210 int 211 _nfs4_format_flags(char *str, size_t size, acl_flag_t var, int verbose) 212 { 213 214 if (verbose) 215 return (format_flags_verbose(str, size, var, a_flags)); 216 217 return (format_flags_compact(str, size, var, a_flags)); 218 } 219 220 int 221 _nfs4_format_access_mask(char *str, size_t size, acl_perm_t var, int verbose) 222 { 223 224 if (verbose) 225 return (format_flags_verbose(str, size, var, a_access_masks)); 226 227 return (format_flags_compact(str, size, var, a_access_masks)); 228 } 229 230 int 231 _nfs4_parse_flags(const char *str, acl_flag_t *flags) 232 { 233 int error, try_compact; 234 int tmpflags; 235 236 error = parse_flags_verbose(str, &tmpflags, a_flags, "flags", &try_compact); 237 if (error && try_compact) 238 error = parse_flags_compact(str, &tmpflags, a_flags, "flags"); 239 240 *flags = tmpflags; 241 242 return (error); 243 } 244 245 int 246 _nfs4_parse_access_mask(const char *str, acl_perm_t *perms) 247 { 248 int error, try_compact; 249 int tmpperms; 250 251 error = parse_flags_verbose(str, &tmpperms, a_access_masks, 252 "access permissions", &try_compact); 253 if (error && try_compact) 254 error = parse_flags_compact(str, &tmpperms, 255 a_access_masks, "access permissions"); 256 257 *perms = tmpperms; 258 259 return (error); 260 } 261