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