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_to_text_nfs4.c 326193 2017-11-25 17:12:48Z pfg $"); 32 1.1 christos #else 33 1.2 christos __RCSID("$NetBSD: acl_to_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 <sys/syscall.h> 45 1.1 christos #include <sys/types.h> 46 1.1 christos #include <sys/acl.h> 47 1.1 christos 48 1.1 christos #include "acl_support.h" 49 1.1 christos 50 1.1 christos #define MAX_ENTRY_LENGTH 512 51 1.1 christos 52 1.1 christos static int 53 1.1 christos format_who(char *str, size_t size, const acl_entry_t entry, int numeric) 54 1.1 christos { 55 1.1 christos int error; 56 1.1 christos acl_tag_t tag; 57 1.1 christos struct passwd *pwd; 58 1.1 christos struct group *grp; 59 1.1 christos uid_t *id; 60 1.1 christos 61 1.1 christos error = acl_get_tag_type(entry, &tag); 62 1.1 christos if (error) 63 1.1 christos return (error); 64 1.1 christos 65 1.1 christos switch (tag) { 66 1.1 christos case ACL_USER_OBJ: 67 1.1 christos snprintf(str, size, "owner@"); 68 1.1 christos break; 69 1.1 christos 70 1.1 christos case ACL_USER: 71 1.1 christos id = (uid_t *)acl_get_qualifier(entry); 72 1.1 christos if (id == NULL) 73 1.1 christos return (-1); 74 1.1 christos /* XXX: Thread-unsafe. */ 75 1.1 christos if (!numeric) 76 1.1 christos pwd = getpwuid(*id); 77 1.1 christos else 78 1.1 christos pwd = NULL; 79 1.1 christos if (pwd == NULL) 80 1.1 christos snprintf(str, size, "user:%d", (unsigned int)*id); 81 1.1 christos else 82 1.1 christos snprintf(str, size, "user:%s", pwd->pw_name); 83 1.1 christos break; 84 1.1 christos 85 1.1 christos case ACL_GROUP_OBJ: 86 1.1 christos snprintf(str, size, "group@"); 87 1.1 christos break; 88 1.1 christos 89 1.1 christos case ACL_GROUP: 90 1.1 christos id = (uid_t *)acl_get_qualifier(entry); 91 1.1 christos if (id == NULL) 92 1.1 christos return (-1); 93 1.1 christos /* XXX: Thread-unsafe. */ 94 1.1 christos if (!numeric) 95 1.1 christos grp = getgrgid(*id); 96 1.1 christos else 97 1.1 christos grp = NULL; 98 1.1 christos if (grp == NULL) 99 1.1 christos snprintf(str, size, "group:%d", (unsigned int)*id); 100 1.1 christos else 101 1.1 christos snprintf(str, size, "group:%s", grp->gr_name); 102 1.1 christos break; 103 1.1 christos 104 1.1 christos case ACL_EVERYONE: 105 1.1 christos snprintf(str, size, "everyone@"); 106 1.1 christos break; 107 1.1 christos 108 1.1 christos default: 109 1.1 christos return (-1); 110 1.1 christos } 111 1.1 christos 112 1.1 christos return (0); 113 1.1 christos } 114 1.1 christos 115 1.1 christos static int 116 1.1 christos format_entry_type(char *str, size_t size, const acl_entry_t entry) 117 1.1 christos { 118 1.1 christos int error; 119 1.1 christos acl_entry_type_t entry_type; 120 1.1 christos 121 1.1 christos error = acl_get_entry_type_np(entry, &entry_type); 122 1.1 christos if (error) 123 1.1 christos return (error); 124 1.1 christos 125 1.1 christos switch (entry_type) { 126 1.1 christos case ACL_ENTRY_TYPE_ALLOW: 127 1.1 christos snprintf(str, size, "allow"); 128 1.1 christos break; 129 1.1 christos case ACL_ENTRY_TYPE_DENY: 130 1.1 christos snprintf(str, size, "deny"); 131 1.1 christos break; 132 1.1 christos case ACL_ENTRY_TYPE_AUDIT: 133 1.1 christos snprintf(str, size, "audit"); 134 1.1 christos break; 135 1.1 christos case ACL_ENTRY_TYPE_ALARM: 136 1.1 christos snprintf(str, size, "alarm"); 137 1.1 christos break; 138 1.1 christos default: 139 1.1 christos return (-1); 140 1.1 christos } 141 1.1 christos 142 1.1 christos return (0); 143 1.1 christos } 144 1.1 christos 145 1.1 christos static int 146 1.1 christos format_additional_id(char *str, size_t size, const acl_entry_t entry) 147 1.1 christos { 148 1.1 christos int error; 149 1.1 christos acl_tag_t tag; 150 1.1 christos uid_t *id; 151 1.1 christos 152 1.1 christos error = acl_get_tag_type(entry, &tag); 153 1.1 christos if (error) 154 1.1 christos return (error); 155 1.1 christos 156 1.1 christos switch (tag) { 157 1.1 christos case ACL_USER_OBJ: 158 1.1 christos case ACL_GROUP_OBJ: 159 1.1 christos case ACL_EVERYONE: 160 1.1 christos str[0] = '\0'; 161 1.1 christos break; 162 1.1 christos 163 1.1 christos default: 164 1.1 christos id = (uid_t *)acl_get_qualifier(entry); 165 1.1 christos if (id == NULL) 166 1.1 christos return (-1); 167 1.1 christos snprintf(str, size, ":%d", (unsigned int)*id); 168 1.1 christos } 169 1.1 christos 170 1.1 christos return (0); 171 1.1 christos } 172 1.1 christos 173 1.1 christos static int 174 1.1 christos format_entry(char *str, size_t size, const acl_entry_t entry, int flags) 175 1.1 christos { 176 1.1 christos size_t off = 0, min_who_field_length = 18; 177 1.1 christos acl_permset_t permset; 178 1.1 christos acl_flagset_t flagset; 179 1.1 christos int error; 180 1.2 christos size_t len; 181 1.1 christos char buf[MAX_ENTRY_LENGTH + 1]; 182 1.1 christos 183 1.1 christos assert(_entry_brand(entry) == ACL_BRAND_NFS4); 184 1.1 christos 185 1.1 christos error = acl_get_flagset_np(entry, &flagset); 186 1.1 christos if (error) 187 1.1 christos return (error); 188 1.1 christos 189 1.1 christos error = acl_get_permset(entry, &permset); 190 1.1 christos if (error) 191 1.1 christos return (error); 192 1.1 christos 193 1.1 christos error = format_who(buf, sizeof(buf), entry, 194 1.1 christos flags & ACL_TEXT_NUMERIC_IDS); 195 1.1 christos if (error) 196 1.1 christos return (error); 197 1.1 christos len = strlen(buf); 198 1.1 christos if (len < min_who_field_length) 199 1.1 christos len = min_who_field_length; 200 1.2 christos off += snprintf(str + off, size - off, "%*s:", (int)len, buf); 201 1.1 christos 202 1.1 christos error = _nfs4_format_access_mask(buf, sizeof(buf), *permset, 203 1.1 christos flags & ACL_TEXT_VERBOSE); 204 1.1 christos if (error) 205 1.1 christos return (error); 206 1.1 christos off += snprintf(str + off, size - off, "%s:", buf); 207 1.1 christos 208 1.1 christos error = _nfs4_format_flags(buf, sizeof(buf), *flagset, 209 1.1 christos flags & ACL_TEXT_VERBOSE); 210 1.1 christos if (error) 211 1.1 christos return (error); 212 1.1 christos off += snprintf(str + off, size - off, "%s:", buf); 213 1.1 christos 214 1.1 christos error = format_entry_type(buf, sizeof(buf), entry); 215 1.1 christos if (error) 216 1.1 christos return (error); 217 1.1 christos off += snprintf(str + off, size - off, "%s", buf); 218 1.1 christos 219 1.1 christos if (flags & ACL_TEXT_APPEND_ID) { 220 1.1 christos error = format_additional_id(buf, sizeof(buf), entry); 221 1.1 christos if (error) 222 1.1 christos return (error); 223 1.1 christos off += snprintf(str + off, size - off, "%s", buf); 224 1.1 christos } 225 1.1 christos 226 1.1 christos off += snprintf(str + off, size - off, "\n"); 227 1.1 christos 228 1.1 christos /* Make sure we didn't truncate anything. */ 229 1.1 christos assert (off < size); 230 1.1 christos 231 1.1 christos return (0); 232 1.1 christos } 233 1.1 christos 234 1.1 christos char * 235 1.1 christos _nfs4_acl_to_text_np(const acl_t aclp, ssize_t *len_p, int flags) 236 1.1 christos { 237 1.2 christos int error, entry_id = ACL_FIRST_ENTRY; 238 1.2 christos size_t off = 0, size; 239 1.1 christos char *str; 240 1.1 christos acl_entry_t entry; 241 1.1 christos 242 1.1 christos if (aclp->ats_acl.acl_cnt == 0) 243 1.1 christos return strdup(""); 244 1.1 christos 245 1.1 christos size = aclp->ats_acl.acl_cnt * MAX_ENTRY_LENGTH; 246 1.1 christos str = malloc(size); 247 1.1 christos if (str == NULL) 248 1.1 christos return (NULL); 249 1.1 christos 250 1.1 christos while (acl_get_entry(aclp, entry_id, &entry) == 1) { 251 1.1 christos entry_id = ACL_NEXT_ENTRY; 252 1.1 christos 253 1.1 christos assert(off < size); 254 1.1 christos 255 1.1 christos error = format_entry(str + off, size - off, entry, flags); 256 1.1 christos if (error) { 257 1.1 christos free(str); 258 1.1 christos errno = EINVAL; 259 1.1 christos return (NULL); 260 1.1 christos } 261 1.1 christos 262 1.1 christos off = strlen(str); 263 1.1 christos } 264 1.1 christos 265 1.1 christos assert(off < size); 266 1.1 christos str[off] = '\0'; 267 1.1 christos 268 1.1 christos if (len_p != NULL) 269 1.1 christos *len_p = off; 270 1.1 christos 271 1.1 christos return (str); 272 1.1 christos } 273