2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008, 2009 Edward Tomasz NapieraĆa <trasz@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
40 #include <sys/syscall.h>
41 #include <sys/types.h>
44 #include "acl_support.h"
46 #define MAX_ENTRY_LENGTH 512
49 format_who(char *str, size_t size, const acl_entry_t entry, int numeric)
57 error = acl_get_tag_type(entry, &tag);
63 snprintf(str, size, "owner@");
67 id = (uid_t *)acl_get_qualifier(entry);
70 /* XXX: Thread-unsafe. */
76 snprintf(str, size, "user:%d", (unsigned int)*id);
78 snprintf(str, size, "user:%s", pwd->pw_name);
82 snprintf(str, size, "group@");
86 id = (uid_t *)acl_get_qualifier(entry);
89 /* XXX: Thread-unsafe. */
95 snprintf(str, size, "group:%d", (unsigned int)*id);
97 snprintf(str, size, "group:%s", grp->gr_name);
101 snprintf(str, size, "everyone@");
112 format_entry_type(char *str, size_t size, const acl_entry_t entry)
115 acl_entry_type_t entry_type;
117 error = acl_get_entry_type_np(entry, &entry_type);
121 switch (entry_type) {
122 case ACL_ENTRY_TYPE_ALLOW:
123 snprintf(str, size, "allow");
125 case ACL_ENTRY_TYPE_DENY:
126 snprintf(str, size, "deny");
128 case ACL_ENTRY_TYPE_AUDIT:
129 snprintf(str, size, "audit");
131 case ACL_ENTRY_TYPE_ALARM:
132 snprintf(str, size, "alarm");
142 format_additional_id(char *str, size_t size, const acl_entry_t entry)
148 error = acl_get_tag_type(entry, &tag);
160 id = (uid_t *)acl_get_qualifier(entry);
163 snprintf(str, size, ":%d", (unsigned int)*id);
170 format_entry(char *str, size_t size, const acl_entry_t entry, int flags)
172 size_t off = 0, min_who_field_length = 18;
173 acl_permset_t permset;
174 acl_flagset_t flagset;
176 char buf[MAX_ENTRY_LENGTH + 1];
178 assert(_entry_brand(entry) == ACL_BRAND_NFS4);
180 error = acl_get_flagset_np(entry, &flagset);
184 error = acl_get_permset(entry, &permset);
188 error = format_who(buf, sizeof(buf), entry,
189 flags & ACL_TEXT_NUMERIC_IDS);
193 if (len < min_who_field_length)
194 len = min_who_field_length;
195 off += snprintf(str + off, size - off, "%*s:", len, buf);
197 error = _nfs4_format_access_mask(buf, sizeof(buf), *permset,
198 flags & ACL_TEXT_VERBOSE);
201 off += snprintf(str + off, size - off, "%s:", buf);
203 error = _nfs4_format_flags(buf, sizeof(buf), *flagset,
204 flags & ACL_TEXT_VERBOSE);
207 off += snprintf(str + off, size - off, "%s:", buf);
209 error = format_entry_type(buf, sizeof(buf), entry);
212 off += snprintf(str + off, size - off, "%s", buf);
214 if (flags & ACL_TEXT_APPEND_ID) {
215 error = format_additional_id(buf, sizeof(buf), entry);
218 off += snprintf(str + off, size - off, "%s", buf);
221 off += snprintf(str + off, size - off, "\n");
223 /* Make sure we didn't truncate anything. */
230 _nfs4_acl_to_text_np(const acl_t aclp, ssize_t *len_p, int flags)
232 int error, off = 0, size, entry_id = ACL_FIRST_ENTRY;
236 if (aclp->ats_acl.acl_cnt == 0)
239 size = aclp->ats_acl.acl_cnt * MAX_ENTRY_LENGTH;
244 while (acl_get_entry(aclp, entry_id, &entry) == 1) {
245 entry_id = ACL_NEXT_ENTRY;
249 error = format_entry(str + off, size - off, entry, flags);