1 1.1 christos /*- 2 1.1 christos * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 1.1 christos * 4 1.1 christos * Copyright (c) 2008, 2009 Edward Tomasz Napieraa <trasz (at) FreeBSD.org> 5 1.1 christos * All rights reserved. 6 1.1 christos * 7 1.1 christos * Redistribution and use in source and binary forms, with or without 8 1.1 christos * modification, are permitted provided that the following conditions 9 1.1 christos * are met: 10 1.1 christos * 1. Redistributions of source code must retain the above copyright 11 1.1 christos * notice, this list of conditions and the following disclaimer. 12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 christos * notice, this list of conditions and the following disclaimer in the 14 1.1 christos * documentation and/or other materials provided with the distribution. 15 1.1 christos * 16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 christos * SUCH DAMAGE. 27 1.1 christos */ 28 1.1 christos 29 1.1 christos #include <sys/cdefs.h> 30 1.1 christos #if 0 31 1.1 christos __FBSDID("$FreeBSD: head/lib/libc/posix1e/acl_from_text_nfs4.c 326193 2017-11-25 17:12:48Z pfg $"); 32 1.1 christos #else 33 1.2 christos __RCSID("$NetBSD: acl_from_text_nfs4.c,v 1.2 2024/01/20 14:52:48 christos Exp $"); 34 1.1 christos #endif 35 1.1 christos 36 1.1 christos #include <stdio.h> 37 1.1 christos #include <stdlib.h> 38 1.1 christos #include <unistd.h> 39 1.1 christos #include <errno.h> 40 1.1 christos #include <assert.h> 41 1.1 christos #include <string.h> 42 1.1 christos #include <pwd.h> 43 1.1 christos #include <grp.h> 44 1.1 christos #include <ctype.h> 45 1.1 christos #include <err.h> 46 1.1 christos #include <sys/syscall.h> 47 1.1 christos #include <sys/types.h> 48 1.1 christos #include <sys/acl.h> 49 1.1 christos 50 1.1 christos #include "acl_support.h" 51 1.1 christos 52 1.1 christos #define MAX_ENTRY_LENGTH 512 53 1.1 christos 54 1.1 christos /* 55 1.1 christos * Parse the tag field of ACL entry passed as "str". If qualifier 56 1.1 christos * needs to follow, then the variable referenced by "need_qualifier" 57 1.1 christos * is set to 1, otherwise it's set to 0. 58 1.1 christos */ 59 1.1 christos static int 60 1.1 christos parse_tag(const char *str, acl_entry_t entry, int *need_qualifier) 61 1.1 christos { 62 1.1 christos 63 1.1 christos assert(need_qualifier != NULL); 64 1.1 christos *need_qualifier = 0; 65 1.1 christos 66 1.1 christos if (strcmp(str, "owner@") == 0) 67 1.1 christos return (acl_set_tag_type(entry, ACL_USER_OBJ)); 68 1.1 christos if (strcmp(str, "group@") == 0) 69 1.1 christos return (acl_set_tag_type(entry, ACL_GROUP_OBJ)); 70 1.1 christos if (strcmp(str, "everyone@") == 0) 71 1.1 christos return (acl_set_tag_type(entry, ACL_EVERYONE)); 72 1.1 christos 73 1.1 christos *need_qualifier = 1; 74 1.1 christos 75 1.1 christos if (strcmp(str, "user") == 0 || strcmp(str, "u") == 0) 76 1.1 christos return (acl_set_tag_type(entry, ACL_USER)); 77 1.1 christos if (strcmp(str, "group") == 0 || strcmp(str, "g") == 0) 78 1.1 christos return (acl_set_tag_type(entry, ACL_GROUP)); 79 1.1 christos 80 1.1 christos warnx("malformed ACL: invalid \"tag\" field"); 81 1.1 christos 82 1.1 christos return (-1); 83 1.1 christos } 84 1.1 christos 85 1.1 christos /* 86 1.1 christos * Parse the qualifier field of ACL entry passed as "str". 87 1.1 christos * If user or group name cannot be resolved, then the variable 88 1.1 christos * referenced by "need_qualifier" is set to 1; it will be checked 89 1.1 christos * later to figure out whether the appended_id is required. 90 1.1 christos */ 91 1.1 christos static int 92 1.1 christos parse_qualifier(char *str, acl_entry_t entry, int *need_qualifier) 93 1.1 christos { 94 1.2 christos size_t qualifier_length; 95 1.2 christos int error; 96 1.1 christos uid_t id; 97 1.1 christos acl_tag_t tag; 98 1.1 christos 99 1.1 christos assert(need_qualifier != NULL); 100 1.1 christos *need_qualifier = 0; 101 1.1 christos 102 1.1 christos qualifier_length = strlen(str); 103 1.1 christos 104 1.1 christos if (qualifier_length == 0) { 105 1.1 christos warnx("malformed ACL: empty \"qualifier\" field"); 106 1.1 christos return (-1); 107 1.1 christos } 108 1.1 christos 109 1.1 christos error = acl_get_tag_type(entry, &tag); 110 1.1 christos if (error) 111 1.1 christos return (error); 112 1.1 christos 113 1.1 christos error = _acl_name_to_id(tag, str, &id); 114 1.1 christos if (error) { 115 1.1 christos *need_qualifier = 1; 116 1.1 christos return (0); 117 1.1 christos } 118 1.1 christos 119 1.1 christos return (acl_set_qualifier(entry, &id)); 120 1.1 christos } 121 1.1 christos 122 1.1 christos static int 123 1.1 christos parse_access_mask(char *str, acl_entry_t entry) 124 1.1 christos { 125 1.1 christos int error; 126 1.1 christos acl_perm_t perm; 127 1.1 christos 128 1.1 christos error = _nfs4_parse_access_mask(str, &perm); 129 1.1 christos if (error) 130 1.1 christos return (error); 131 1.1 christos 132 1.1 christos error = acl_set_permset(entry, &perm); 133 1.1 christos 134 1.1 christos return (error); 135 1.1 christos } 136 1.1 christos 137 1.1 christos static int 138 1.1 christos parse_flags(char *str, acl_entry_t entry) 139 1.1 christos { 140 1.1 christos int error; 141 1.1 christos acl_flag_t flags; 142 1.1 christos 143 1.1 christos error = _nfs4_parse_flags(str, &flags); 144 1.1 christos if (error) 145 1.1 christos return (error); 146 1.1 christos 147 1.1 christos error = acl_set_flagset_np(entry, &flags); 148 1.1 christos 149 1.1 christos return (error); 150 1.1 christos } 151 1.1 christos 152 1.1 christos static int 153 1.1 christos parse_entry_type(const char *str, acl_entry_t entry) 154 1.1 christos { 155 1.1 christos 156 1.1 christos if (strcmp(str, "allow") == 0) 157 1.1 christos return (acl_set_entry_type_np(entry, ACL_ENTRY_TYPE_ALLOW)); 158 1.1 christos if (strcmp(str, "deny") == 0) 159 1.1 christos return (acl_set_entry_type_np(entry, ACL_ENTRY_TYPE_DENY)); 160 1.1 christos if (strcmp(str, "audit") == 0) 161 1.1 christos return (acl_set_entry_type_np(entry, ACL_ENTRY_TYPE_AUDIT)); 162 1.1 christos if (strcmp(str, "alarm") == 0) 163 1.1 christos return (acl_set_entry_type_np(entry, ACL_ENTRY_TYPE_ALARM)); 164 1.1 christos 165 1.1 christos warnx("malformed ACL: invalid \"type\" field"); 166 1.1 christos 167 1.1 christos return (-1); 168 1.1 christos } 169 1.1 christos 170 1.1 christos static int 171 1.1 christos parse_appended_id(char *str, acl_entry_t entry) 172 1.1 christos { 173 1.2 christos size_t qualifier_length; 174 1.1 christos char *end; 175 1.1 christos id_t id; 176 1.1 christos 177 1.1 christos qualifier_length = strlen(str); 178 1.1 christos if (qualifier_length == 0) { 179 1.1 christos warnx("malformed ACL: \"appended id\" field present, " 180 1.1 christos "but empty"); 181 1.1 christos return (-1); 182 1.1 christos } 183 1.1 christos 184 1.1 christos id = strtod(str, &end); 185 1.2 christos if ((size_t)(end - str) != qualifier_length) { 186 1.1 christos warnx("malformed ACL: appended id is not a number"); 187 1.1 christos return (-1); 188 1.1 christos } 189 1.1 christos 190 1.1 christos return (acl_set_qualifier(entry, &id)); 191 1.1 christos } 192 1.1 christos 193 1.1 christos static int 194 1.1 christos number_of_colons(const char *str) 195 1.1 christos { 196 1.1 christos int count = 0; 197 1.1 christos 198 1.1 christos while (*str != '\0') { 199 1.1 christos if (*str == ':') 200 1.1 christos count++; 201 1.1 christos 202 1.1 christos str++; 203 1.1 christos } 204 1.1 christos 205 1.1 christos return (count); 206 1.1 christos } 207 1.1 christos 208 1.1 christos int 209 1.1 christos _nfs4_acl_entry_from_text(acl_t aclp, char *str) 210 1.1 christos { 211 1.1 christos int error, need_qualifier; 212 1.1 christos acl_entry_t entry; 213 1.1 christos char *field, *qualifier_field = NULL; 214 1.1 christos 215 1.1 christos error = acl_create_entry(&aclp, &entry); 216 1.1 christos if (error) 217 1.1 christos return (error); 218 1.1 christos 219 1.1 christos assert(_entry_brand(entry) == ACL_BRAND_NFS4); 220 1.1 christos 221 1.1 christos if (str == NULL) 222 1.1 christos goto truncated_entry; 223 1.1 christos field = strsep(&str, ":"); 224 1.1 christos 225 1.1 christos field = string_skip_whitespace(field); 226 1.1 christos if ((*field == '\0') && (!str)) { 227 1.1 christos /* 228 1.1 christos * Is an entirely comment line, skip to next 229 1.1 christos * comma. 230 1.1 christos */ 231 1.1 christos return (0); 232 1.1 christos } 233 1.1 christos 234 1.1 christos error = parse_tag(field, entry, &need_qualifier); 235 1.1 christos if (error) 236 1.1 christos goto malformed_field; 237 1.1 christos 238 1.1 christos if (need_qualifier) { 239 1.1 christos if (str == NULL) 240 1.1 christos goto truncated_entry; 241 1.1 christos qualifier_field = field = strsep(&str, ":"); 242 1.1 christos error = parse_qualifier(field, entry, &need_qualifier); 243 1.1 christos if (error) 244 1.1 christos goto malformed_field; 245 1.1 christos } 246 1.1 christos 247 1.1 christos if (str == NULL) 248 1.1 christos goto truncated_entry; 249 1.1 christos field = strsep(&str, ":"); 250 1.1 christos error = parse_access_mask(field, entry); 251 1.1 christos if (error) 252 1.1 christos goto malformed_field; 253 1.1 christos 254 1.1 christos if (str == NULL) 255 1.1 christos goto truncated_entry; 256 1.1 christos /* Do we have "flags" field? */ 257 1.1 christos if (number_of_colons(str) > 0) { 258 1.1 christos field = strsep(&str, ":"); 259 1.1 christos error = parse_flags(field, entry); 260 1.1 christos if (error) 261 1.1 christos goto malformed_field; 262 1.1 christos } 263 1.1 christos 264 1.1 christos if (str == NULL) 265 1.1 christos goto truncated_entry; 266 1.1 christos field = strsep(&str, ":"); 267 1.1 christos error = parse_entry_type(field, entry); 268 1.1 christos if (error) 269 1.1 christos goto malformed_field; 270 1.1 christos 271 1.1 christos if (need_qualifier) { 272 1.1 christos if (str == NULL) { 273 1.1 christos warnx("malformed ACL: unknown user or group name " 274 1.1 christos "\"%s\"", qualifier_field); 275 1.1 christos goto truncated_entry; 276 1.1 christos } 277 1.1 christos 278 1.1 christos error = parse_appended_id(str, entry); 279 1.1 christos if (error) 280 1.1 christos goto malformed_field; 281 1.1 christos } 282 1.1 christos 283 1.1 christos return (0); 284 1.1 christos 285 1.1 christos truncated_entry: 286 1.1 christos malformed_field: 287 1.1 christos acl_delete_entry(aclp, entry); 288 1.1 christos errno = EINVAL; 289 1.1 christos return (-1); 290 1.1 christos } 291