xref: /freebsd/lib/libc/posix1e/acl_get.c (revision 39ee7a7a6bdd1557b1c3532abf60d139798ac88b)
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 <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 
55 #include "acl_support.h"
56 
57 acl_t
58 acl_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 
80 acl_t
81 acl_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 
103 acl_t
104 acl_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 
112 acl_t
113 acl_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 
135 /*
136  * acl_get_permset() (23.4.17): return via permset_p a descriptor to
137  * the permission set in the ACL entry entry_d.
138  */
139 int
140 acl_get_permset(acl_entry_t entry_d, acl_permset_t *permset_p)
141 {
142 
143 	if (entry_d == NULL || permset_p == NULL) {
144 		errno = EINVAL;
145 		return (-1);
146 	}
147 
148 	*permset_p = &entry_d->ae_perm;
149 
150 	return (0);
151 }
152 
153 /*
154  * acl_get_qualifier() (23.4.18): retrieve the qualifier of the tag
155  * for the ACL entry entry_d.
156  */
157 void *
158 acl_get_qualifier(acl_entry_t entry_d)
159 {
160 	uid_t *retval;
161 
162 	if (entry_d == NULL) {
163 		errno = EINVAL;
164 		return (NULL);
165 	}
166 
167 	switch(entry_d->ae_tag) {
168 	case ACL_USER:
169 	case ACL_GROUP:
170 		retval = malloc(sizeof(uid_t));
171 		if (retval == NULL)
172 			return (NULL);
173 		*retval = entry_d->ae_id;
174 		return (retval);
175 	}
176 
177 	errno = EINVAL;
178 	return (NULL);
179 }
180 
181 /*
182  * acl_get_tag_type() (23.4.19): return the tag type for the ACL
183  * entry entry_p.
184  */
185 int
186 acl_get_tag_type(acl_entry_t entry_d, acl_tag_t *tag_type_p)
187 {
188 
189 	if (entry_d == NULL || tag_type_p == NULL) {
190 		errno = EINVAL;
191 		return (-1);
192 	}
193 
194 	*tag_type_p = entry_d->ae_tag;
195 
196 	return (0);
197 }
198 
199 int
200 acl_get_entry_type_np(acl_entry_t entry_d, acl_entry_type_t *entry_type_p)
201 {
202 
203 	if (entry_d == NULL || entry_type_p == NULL) {
204 		errno = EINVAL;
205 		return (-1);
206 	}
207 
208 	if (!_entry_brand_may_be(entry_d, ACL_BRAND_NFS4)) {
209 		errno = EINVAL;
210 		return (-1);
211 	}
212 
213 	*entry_type_p = entry_d->ae_entry_type;
214 
215 	return (0);
216 }
217