2 * Copyright (c) 2008, 2009 Edward Tomasz NapieraĆa <trasz@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
38 #include <sys/syscall.h>
39 #include <sys/types.h>
42 #include "acl_support.h"
44 #define MAX_ENTRY_LENGTH 512
47 format_who(char *str, size_t size, const acl_entry_t entry, int numeric)
55 error = acl_get_tag_type(entry, &tag);
61 snprintf(str, size, "owner@");
65 id = (id_t *)acl_get_qualifier(entry);
68 /* XXX: Thread-unsafe. */
74 snprintf(str, size, "user:%d", (unsigned int)*id);
76 snprintf(str, size, "user:%s", pwd->pw_name);
80 snprintf(str, size, "group@");
84 id = (id_t *)acl_get_qualifier(entry);
87 /* XXX: Thread-unsafe. */
93 snprintf(str, size, "group:%d", (unsigned int)*id);
95 snprintf(str, size, "group:%s", grp->gr_name);
99 snprintf(str, size, "everyone@");
110 format_entry_type(char *str, size_t size, const acl_entry_t entry)
113 acl_entry_type_t entry_type;
115 error = acl_get_entry_type_np(entry, &entry_type);
119 switch (entry_type) {
120 case ACL_ENTRY_TYPE_ALLOW:
121 snprintf(str, size, "allow");
123 case ACL_ENTRY_TYPE_DENY:
124 snprintf(str, size, "deny");
126 case ACL_ENTRY_TYPE_AUDIT:
127 snprintf(str, size, "audit");
129 case ACL_ENTRY_TYPE_ALARM:
130 snprintf(str, size, "alarm");
140 format_additional_id(char *str, size_t size, const acl_entry_t entry)
146 error = acl_get_tag_type(entry, &tag);
158 id = (id_t *)acl_get_qualifier(entry);
161 snprintf(str, size, ":%d", (unsigned int)*id);
168 format_entry(char *str, size_t size, const acl_entry_t entry, int flags)
170 size_t off = 0, min_who_field_length = 18;
171 acl_permset_t permset;
172 acl_flagset_t flagset;
174 char buf[MAX_ENTRY_LENGTH + 1];
176 assert(_entry_brand(entry) == ACL_BRAND_NFS4);
178 error = acl_get_flagset_np(entry, &flagset);
182 error = acl_get_permset(entry, &permset);
186 error = format_who(buf, sizeof(buf), entry,
187 flags & ACL_TEXT_NUMERIC_IDS);
191 if (len < min_who_field_length)
192 len = min_who_field_length;
193 off += snprintf(str + off, size - off, "%*s:", len, buf);
195 error = _nfs4_format_access_mask(buf, sizeof(buf), *permset,
196 flags & ACL_TEXT_VERBOSE);
199 off += snprintf(str + off, size - off, "%s:", buf);
201 error = _nfs4_format_flags(buf, sizeof(buf), *flagset,
202 flags & ACL_TEXT_VERBOSE);
205 off += snprintf(str + off, size - off, "%s:", buf);
207 error = format_entry_type(buf, sizeof(buf), entry);
210 off += snprintf(str + off, size - off, "%s", buf);
212 if (flags & ACL_TEXT_APPEND_ID) {
213 error = format_additional_id(buf, sizeof(buf), entry);
216 off += snprintf(str + off, size - off, "%s", buf);
219 off += snprintf(str + off, size - off, "\n");
221 /* Make sure we didn't truncate anything. */
228 _nfs4_acl_to_text_np(const acl_t aclp, ssize_t *len_p, int flags)
230 int error, off = 0, size, entry_id = ACL_FIRST_ENTRY;
234 if (aclp->ats_acl.acl_cnt == 0)
237 size = aclp->ats_acl.acl_cnt * MAX_ENTRY_LENGTH;
242 while (acl_get_entry(aclp, entry_id, &entry) == 1) {
243 entry_id = ACL_NEXT_ENTRY;
247 error = format_entry(str + off, size - off, entry, flags);