acl_get.c revision 195004
1/*-
2 * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
3 * All rights reserved.
4 *
5 * This software was developed by Robert Watson for the TrustedBSD Project.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 */
28/*
29 * acl_get_fd - syscall wrapper for retrieving access ACL by fd
30 * acl_get_fd_np - syscall wrapper for retrieving ACL by fd (non-POSIX)
31 * acl_get_file - syscall wrapper for retrieving ACL by filename
32 * acl_get_link_np - syscall wrapper for retrieving ACL by filename (NOFOLLOW)
33 *                   (non-POSIX)
34 * acl_get_perm_np() checks if a permission is in the specified
35 *                   permset (non-POSIX)
36 * acl_get_permset() returns the permission set in the ACL entry
37 * acl_get_qualifier() retrieves the qualifier of the tag from the ACL entry
38 * acl_get_tag_type() returns the tag type for the ACL entry entry_d
39 */
40
41#include <sys/cdefs.h>
42__FBSDID("$FreeBSD: head/lib/libc/posix1e/acl_get.c 195004 2009-06-25 20:57:53Z trasz $");
43
44#include <sys/types.h>
45#include "namespace.h"
46#include <sys/acl.h>
47#include "un-namespace.h"
48
49#include <errno.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <unistd.h>
54
55#include "acl_support.h"
56
57acl_t
58acl_get_file(const char *path_p, acl_type_t type)
59{
60	acl_t	aclp;
61	int	error;
62
63	aclp = acl_init(ACL_MAX_ENTRIES);
64	if (aclp == NULL)
65		return (NULL);
66
67	type = _acl_type_unold(type);
68	error = __acl_get_file(path_p, type, &aclp->ats_acl);
69	if (error) {
70		acl_free(aclp);
71		return (NULL);
72	}
73
74	aclp->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;
75	_acl_brand_from_type(aclp, type);
76
77	return (aclp);
78}
79
80acl_t
81acl_get_link_np(const char *path_p, acl_type_t type)
82{
83	acl_t	aclp;
84	int	error;
85
86	aclp = acl_init(ACL_MAX_ENTRIES);
87	if (aclp == NULL)
88		return (NULL);
89
90	type = _acl_type_unold(type);
91	error = __acl_get_link(path_p, type, &aclp->ats_acl);
92	if (error) {
93		acl_free(aclp);
94		return (NULL);
95	}
96
97	aclp->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;
98	_acl_brand_from_type(aclp, type);
99
100	return (aclp);
101}
102
103acl_t
104acl_get_fd(int fd)
105{
106	if (fpathconf(fd, _PC_ACL_NFS4) == 1)
107		return (acl_get_fd_np(fd, ACL_TYPE_NFS4));
108
109	return (acl_get_fd_np(fd, ACL_TYPE_ACCESS));
110}
111
112acl_t
113acl_get_fd_np(int fd, acl_type_t type)
114{
115	acl_t	aclp;
116	int	error;
117
118	aclp = acl_init(ACL_MAX_ENTRIES);
119	if (aclp == NULL)
120		return (NULL);
121
122	type = _acl_type_unold(type);
123	error = ___acl_get_fd(fd, type, &aclp->ats_acl);
124	if (error) {
125		acl_free(aclp);
126		return (NULL);
127	}
128
129	aclp->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;
130	_acl_brand_from_type(aclp, type);
131
132	return (aclp);
133}
134
135int
136acl_get_perm_np(acl_permset_t permset_d, acl_perm_t perm)
137{
138
139	if (permset_d == NULL) {
140		errno = EINVAL;
141		return (-1);
142	}
143
144	switch(perm) {
145	case ACL_READ:
146	case ACL_WRITE:
147	case ACL_EXECUTE:
148		if (*permset_d & perm)
149			return (1);
150		break;
151	default:
152		errno = EINVAL;
153		return (-1);
154	}
155
156	return (0);
157}
158
159/*
160 * acl_get_permset() (23.4.17): return via permset_p a descriptor to
161 * the permission set in the ACL entry entry_d.
162 */
163int
164acl_get_permset(acl_entry_t entry_d, acl_permset_t *permset_p)
165{
166
167	if (entry_d == NULL || permset_p == NULL) {
168		errno = EINVAL;
169		return (-1);
170	}
171
172	*permset_p = &entry_d->ae_perm;
173
174	return (0);
175}
176
177/*
178 * acl_get_qualifier() (23.4.18): retrieve the qualifier of the tag
179 * for the ACL entry entry_d.
180 */
181void *
182acl_get_qualifier(acl_entry_t entry_d)
183{
184	uid_t *retval;
185
186	if (entry_d == NULL) {
187		errno = EINVAL;
188		return (NULL);
189	}
190
191	switch(entry_d->ae_tag) {
192	case ACL_USER:
193	case ACL_GROUP:
194		retval = malloc(sizeof(uid_t));
195		if (retval == NULL)
196			return (NULL);
197		*retval = entry_d->ae_id;
198		return (retval);
199	}
200
201	errno = EINVAL;
202	return (NULL);
203}
204
205/*
206 * acl_get_tag_type() (23.4.19): return the tag type for the ACL
207 * entry entry_p.
208 */
209int
210acl_get_tag_type(acl_entry_t entry_d, acl_tag_t *tag_type_p)
211{
212
213	if (entry_d == NULL || tag_type_p == NULL) {
214		errno = EINVAL;
215		return (-1);
216	}
217
218	*tag_type_p = entry_d->ae_tag;
219
220	return (0);
221}
222
223int
224acl_get_entry_type_np(acl_entry_t entry_d, acl_entry_type_t *entry_type_p)
225{
226
227	if (entry_d == NULL || entry_type_p == NULL) {
228		errno = EINVAL;
229		return (-1);
230	}
231
232	if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) {
233		errno = EINVAL;
234		return (-1);
235	}
236
237	*entry_type_p = entry_d->ae_entry_type;
238
239	return (0);
240}
241