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