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