2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008, 2009 Edward Tomasz NapieraĆa <trasz@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
39 #include <sys/syscall.h>
40 #include <sys/types.h>
43 #include "acl_support.h"
45 #define MAX_ENTRY_LENGTH 512
48 format_who(char *str, size_t size, const acl_entry_t entry, int numeric)
56 error = acl_get_tag_type(entry, &tag);
62 snprintf(str, size, "owner@");
66 id = (uid_t *)acl_get_qualifier(entry);
69 /* XXX: Thread-unsafe. */
75 snprintf(str, size, "user:%d", (unsigned int)*id);
77 snprintf(str, size, "user:%s", pwd->pw_name);
81 snprintf(str, size, "group@");
85 id = (uid_t *)acl_get_qualifier(entry);
88 /* XXX: Thread-unsafe. */
94 snprintf(str, size, "group:%d", (unsigned int)*id);
96 snprintf(str, size, "group:%s", grp->gr_name);
100 snprintf(str, size, "everyone@");
111 format_entry_type(char *str, size_t size, const acl_entry_t entry)
114 acl_entry_type_t entry_type;
116 error = acl_get_entry_type_np(entry, &entry_type);
120 switch (entry_type) {
121 case ACL_ENTRY_TYPE_ALLOW:
122 snprintf(str, size, "allow");
124 case ACL_ENTRY_TYPE_DENY:
125 snprintf(str, size, "deny");
127 case ACL_ENTRY_TYPE_AUDIT:
128 snprintf(str, size, "audit");
130 case ACL_ENTRY_TYPE_ALARM:
131 snprintf(str, size, "alarm");
141 format_additional_id(char *str, size_t size, const acl_entry_t entry)
147 error = acl_get_tag_type(entry, &tag);
159 id = (uid_t *)acl_get_qualifier(entry);
162 snprintf(str, size, ":%d", (unsigned int)*id);
169 format_entry(char *str, size_t size, const acl_entry_t entry, int flags)
171 size_t off = 0, min_who_field_length = 18;
172 acl_permset_t permset;
173 acl_flagset_t flagset;
175 char buf[MAX_ENTRY_LENGTH + 1];
177 assert(_entry_brand(entry) == ACL_BRAND_NFS4);
179 error = acl_get_flagset_np(entry, &flagset);
183 error = acl_get_permset(entry, &permset);
187 error = format_who(buf, sizeof(buf), entry,
188 flags & ACL_TEXT_NUMERIC_IDS);
192 if (len < min_who_field_length)
193 len = min_who_field_length;
194 off += snprintf(str + off, size - off, "%*s:", len, buf);
196 error = _nfs4_format_access_mask(buf, sizeof(buf), *permset,
197 flags & ACL_TEXT_VERBOSE);
200 off += snprintf(str + off, size - off, "%s:", buf);
202 error = _nfs4_format_flags(buf, sizeof(buf), *flagset,
203 flags & ACL_TEXT_VERBOSE);
206 off += snprintf(str + off, size - off, "%s:", buf);
208 error = format_entry_type(buf, sizeof(buf), entry);
211 off += snprintf(str + off, size - off, "%s", buf);
213 if (flags & ACL_TEXT_APPEND_ID) {
214 error = format_additional_id(buf, sizeof(buf), entry);
217 off += snprintf(str + off, size - off, "%s", buf);
220 off += snprintf(str + off, size - off, "\n");
222 /* Make sure we didn't truncate anything. */
229 _nfs4_acl_to_text_np(const acl_t aclp, ssize_t *len_p, int flags)
231 int error, off = 0, size, entry_id = ACL_FIRST_ENTRY;
235 if (aclp->ats_acl.acl_cnt == 0)
238 size = aclp->ats_acl.acl_cnt * MAX_ENTRY_LENGTH;
243 while (acl_get_entry(aclp, entry_id, &entry) == 1) {
244 entry_id = ACL_NEXT_ENTRY;
248 error = format_entry(str + off, size - off, entry, flags);