xref: /freebsd/lib/libc/posix1e/acl_get.c (revision 43a5ec4eb41567cc92586503212743d89686d78f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
5  * All rights reserved.
6  *
7  * This software was developed by Robert Watson for the TrustedBSD Project.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 /*
31  * acl_get_fd - syscall wrapper for retrieving access ACL by fd
32  * acl_get_fd_np - syscall wrapper for retrieving ACL by fd (non-POSIX)
33  * acl_get_file - syscall wrapper for retrieving ACL by filename
34  * acl_get_link_np - syscall wrapper for retrieving ACL by filename (NOFOLLOW)
35  *                   (non-POSIX)
36  * acl_get_perm_np() checks if a permission is in the specified
37  *                   permset (non-POSIX)
38  * acl_get_permset() returns the permission set in the ACL entry
39  * acl_get_qualifier() retrieves the qualifier of the tag from the ACL entry
40  * acl_get_tag_type() returns the tag type for the ACL entry entry_d
41  */
42 
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45 
46 #include <sys/types.h>
47 #include "namespace.h"
48 #include <sys/acl.h>
49 #include "un-namespace.h"
50 
51 #include <errno.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 
57 #include "acl_support.h"
58 
59 acl_t
60 acl_get_file(const char *path_p, acl_type_t type)
61 {
62 	acl_t	aclp;
63 	int	error;
64 
65 	aclp = acl_init(ACL_MAX_ENTRIES);
66 	if (aclp == NULL)
67 		return (NULL);
68 
69 	type = _acl_type_unold(type);
70 	error = __acl_get_file(path_p, type, &aclp->ats_acl);
71 	if (error) {
72 		acl_free(aclp);
73 		return (NULL);
74 	}
75 
76 	aclp->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;
77 	_acl_brand_from_type(aclp, type);
78 
79 	return (aclp);
80 }
81 
82 acl_t
83 acl_get_link_np(const char *path_p, acl_type_t type)
84 {
85 	acl_t	aclp;
86 	int	error;
87 
88 	aclp = acl_init(ACL_MAX_ENTRIES);
89 	if (aclp == NULL)
90 		return (NULL);
91 
92 	type = _acl_type_unold(type);
93 	error = __acl_get_link(path_p, type, &aclp->ats_acl);
94 	if (error) {
95 		acl_free(aclp);
96 		return (NULL);
97 	}
98 
99 	aclp->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;
100 	_acl_brand_from_type(aclp, type);
101 
102 	return (aclp);
103 }
104 
105 acl_t
106 acl_get_fd(int fd)
107 {
108 	if (fpathconf(fd, _PC_ACL_NFS4) == 1)
109 		return (acl_get_fd_np(fd, ACL_TYPE_NFS4));
110 
111 	return (acl_get_fd_np(fd, ACL_TYPE_ACCESS));
112 }
113 
114 acl_t
115 acl_get_fd_np(int fd, acl_type_t type)
116 {
117 	acl_t	aclp;
118 	int	error;
119 
120 	aclp = acl_init(ACL_MAX_ENTRIES);
121 	if (aclp == NULL)
122 		return (NULL);
123 
124 	type = _acl_type_unold(type);
125 	error = ___acl_get_fd(fd, type, &aclp->ats_acl);
126 	if (error) {
127 		acl_free(aclp);
128 		return (NULL);
129 	}
130 
131 	aclp->ats_acl.acl_maxcnt = ACL_MAX_ENTRIES;
132 	_acl_brand_from_type(aclp, type);
133 
134 	return (aclp);
135 }
136 
137 /*
138  * acl_get_permset() (23.4.17): return via permset_p a descriptor to
139  * the permission set in the ACL entry entry_d.
140  */
141 int
142 acl_get_permset(acl_entry_t entry_d, acl_permset_t *permset_p)
143 {
144 
145 	if (entry_d == NULL || permset_p == NULL) {
146 		errno = EINVAL;
147 		return (-1);
148 	}
149 
150 	*permset_p = &entry_d->ae_perm;
151 
152 	return (0);
153 }
154 
155 /*
156  * acl_get_qualifier() (23.4.18): retrieve the qualifier of the tag
157  * for the ACL entry entry_d.
158  */
159 void *
160 acl_get_qualifier(acl_entry_t entry_d)
161 {
162 	uid_t *retval;
163 
164 	if (entry_d == NULL) {
165 		errno = EINVAL;
166 		return (NULL);
167 	}
168 
169 	switch(entry_d->ae_tag) {
170 	case ACL_USER:
171 	case ACL_GROUP:
172 		retval = malloc(sizeof(uid_t));
173 		if (retval == NULL)
174 			return (NULL);
175 		*retval = entry_d->ae_id;
176 		return (retval);
177 	}
178 
179 	errno = EINVAL;
180 	return (NULL);
181 }
182 
183 /*
184  * acl_get_tag_type() (23.4.19): return the tag type for the ACL
185  * entry entry_p.
186  */
187 int
188 acl_get_tag_type(acl_entry_t entry_d, acl_tag_t *tag_type_p)
189 {
190 
191 	if (entry_d == NULL || tag_type_p == NULL) {
192 		errno = EINVAL;
193 		return (-1);
194 	}
195 
196 	*tag_type_p = entry_d->ae_tag;
197 
198 	return (0);
199 }
200 
201 int
202 acl_get_entry_type_np(acl_entry_t entry_d, acl_entry_type_t *entry_type_p)
203 {
204 
205 	if (entry_d == NULL || entry_type_p == NULL) {
206 		errno = EINVAL;
207 		return (-1);
208 	}
209 
210 	if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) {
211 		errno = EINVAL;
212 		return (-1);
213 	}
214 
215 	*entry_type_p = entry_d->ae_entry_type;
216 
217 	return (0);
218 }
219