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