xref: /freebsd/lib/libc/posix1e/acl_get.c (revision aa015c8e4ad07fbe76ec137fa491c89856b871e0)
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$");
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 <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 
54 #include "acl_support.h"
55 
56 acl_t
57 acl_get_file(const char *path_p, acl_type_t type)
58 {
59 	acl_t	aclp;
60 	int	error;
61 
62 	aclp = acl_init(ACL_MAX_ENTRIES);
63 	if (aclp == NULL)
64 		return (NULL);
65 
66 	type = _acl_type_unold(type);
67 	error = __acl_get_file(path_p, type, &aclp->ats_acl);
68 	if (error) {
69 		acl_free(aclp);
70 		return (NULL);
71 	}
72 
73 	aclp->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;
74 	_acl_brand_from_type(aclp, type);
75 
76 	return (aclp);
77 }
78 
79 acl_t
80 acl_get_link_np(const char *path_p, acl_type_t type)
81 {
82 	acl_t	aclp;
83 	int	error;
84 
85 	aclp = acl_init(ACL_MAX_ENTRIES);
86 	if (aclp == NULL)
87 		return (NULL);
88 
89 	type = _acl_type_unold(type);
90 	error = __acl_get_link(path_p, type, &aclp->ats_acl);
91 	if (error) {
92 		acl_free(aclp);
93 		return (NULL);
94 	}
95 
96 	aclp->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;
97 	_acl_brand_from_type(aclp, type);
98 
99 	return (aclp);
100 }
101 
102 acl_t
103 acl_get_fd(int fd)
104 {
105 	if (fpathconf(fd, _PC_ACL_NFS4))
106 		return (acl_get_fd_np(fd, ACL_TYPE_NFS4));
107 
108 	return (acl_get_fd_np(fd, ACL_TYPE_ACCESS));
109 }
110 
111 acl_t
112 acl_get_fd_np(int fd, acl_type_t type)
113 {
114 	acl_t	aclp;
115 	int	error;
116 
117 	aclp = acl_init(ACL_MAX_ENTRIES);
118 	if (aclp == NULL)
119 		return (NULL);
120 
121 	type = _acl_type_unold(type);
122 	error = ___acl_get_fd(fd, type, &aclp->ats_acl);
123 	if (error) {
124 		acl_free(aclp);
125 		return (NULL);
126 	}
127 
128 	aclp->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;
129 	_acl_brand_from_type(aclp, type);
130 
131 	return (aclp);
132 }
133 
134 int
135 acl_get_perm_np(acl_permset_t permset_d, acl_perm_t perm)
136 {
137 
138 	if (permset_d == NULL) {
139 		errno = EINVAL;
140 		return (-1);
141 	}
142 
143 	switch(perm) {
144 	case ACL_READ:
145 	case ACL_WRITE:
146 	case ACL_EXECUTE:
147 		if (*permset_d & perm)
148 			return (1);
149 		break;
150 	default:
151 		errno = EINVAL;
152 		return (-1);
153 	}
154 
155 	return (0);
156 }
157 
158 /*
159  * acl_get_permset() (23.4.17): return via permset_p a descriptor to
160  * the permission set in the ACL entry entry_d.
161  */
162 int
163 acl_get_permset(acl_entry_t entry_d, acl_permset_t *permset_p)
164 {
165 
166 	if (entry_d == NULL || permset_p == NULL) {
167 		errno = EINVAL;
168 		return (-1);
169 	}
170 
171 	*permset_p = &entry_d->ae_perm;
172 
173 	return (0);
174 }
175 
176 /*
177  * acl_get_qualifier() (23.4.18): retrieve the qualifier of the tag
178  * for the ACL entry entry_d.
179  */
180 void *
181 acl_get_qualifier(acl_entry_t entry_d)
182 {
183 	uid_t *retval;
184 
185 	if (entry_d == NULL) {
186 		errno = EINVAL;
187 		return (NULL);
188 	}
189 
190 	switch(entry_d->ae_tag) {
191 	case ACL_USER:
192 	case ACL_GROUP:
193 		retval = malloc(sizeof(uid_t));
194 		if (retval == NULL)
195 			return (NULL);
196 		*retval = entry_d->ae_id;
197 		return (retval);
198 	}
199 
200 	errno = EINVAL;
201 	return (NULL);
202 }
203 
204 /*
205  * acl_get_tag_type() (23.4.19): return the tag type for the ACL
206  * entry entry_p.
207  */
208 int
209 acl_get_tag_type(acl_entry_t entry_d, acl_tag_t *tag_type_p)
210 {
211 
212 	if (entry_d == NULL || tag_type_p == NULL) {
213 		errno = EINVAL;
214 		return (-1);
215 	}
216 
217 	*tag_type_p = entry_d->ae_tag;
218 
219 	return (0);
220 }
221 
222 int
223 acl_get_entry_type_np(acl_entry_t entry_d, acl_entry_type_t *entry_type_p)
224 {
225 
226 	if (entry_d == NULL || entry_type_p == NULL) {
227 		errno = EINVAL;
228 		return (-1);
229 	}
230 
231 	if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) {
232 		errno = EINVAL;
233 		return (-1);
234 	}
235 
236 	*entry_type_p = entry_d->ae_entry_type;
237 
238 	return (0);
239 }
240