xref: /freebsd/lib/libc/posix1e/acl_get.c (revision aa79fe245de7616cda41b69a296a5ce209c95c45)
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 
53 #include "acl_support.h"
54 
55 acl_t
56 acl_get_file(const char *path_p, acl_type_t type)
57 {
58 	acl_t	aclp;
59 	int	error;
60 
61 	aclp = acl_init(ACL_MAX_ENTRIES);
62 	if (aclp == NULL)
63 		return (NULL);
64 
65 	type = _acl_type_unold(type);
66 	error = __acl_get_file(path_p, type, &aclp->ats_acl);
67 	if (error) {
68 		acl_free(aclp);
69 		return (NULL);
70 	}
71 
72 	return (aclp);
73 }
74 
75 acl_t
76 acl_get_link_np(const char *path_p, acl_type_t type)
77 {
78 	acl_t	aclp;
79 	int	error;
80 
81 	aclp = acl_init(ACL_MAX_ENTRIES);
82 	if (aclp == NULL)
83 		return (NULL);
84 
85 	type = _acl_type_unold(type);
86 	error = __acl_get_link(path_p, type, &aclp->ats_acl);
87 	if (error) {
88 		acl_free(aclp);
89 		return (NULL);
90 	}
91 
92 	return (aclp);
93 }
94 
95 acl_t
96 acl_get_fd(int fd)
97 {
98 	acl_t	aclp;
99 	int	error;
100 
101 	aclp = acl_init(ACL_MAX_ENTRIES);
102 	if (aclp == NULL)
103 		return (NULL);
104 
105 	error = ___acl_get_fd(fd, ACL_TYPE_ACCESS, &aclp->ats_acl);
106 	if (error) {
107 		acl_free(aclp);
108 		return (NULL);
109 	}
110 
111 	return (aclp);
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 	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