xref: /freebsd/lib/libc/posix1e/acl_get.c (revision 10ddeb64d4e4480e69f2c3e9add26e65a90ec951)
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  * $FreeBSD$
27  */
28 /*
29  * acl_get_file - syscall wrapper for retrieving ACL by filename
30  * acl_get_fd - syscall wrapper for retrieving access ACL by fd
31  * acl_get_fd_np - syscall wrapper for retrieving ACL by fd (non-POSIX)
32  * acl_get_perm_np() checks if a permission is in the specified
33  *                   permset (non-POSIX)
34  * acl_get_permset() returns the permission set in the ACL entry
35  * acl_get_qualifier() retrieves the qualifier of the tag from the ACL entry
36  * acl_get_tag_type() returns the tag type for the ACL entry entry_d
37  */
38 
39 #include <sys/types.h>
40 #include "namespace.h"
41 #include <sys/acl.h>
42 #include "un-namespace.h"
43 
44 #include <errno.h>
45 #include <stdlib.h>
46 #include <string.h>
47 
48 acl_t
49 acl_get_file(const char *path_p, acl_type_t type)
50 {
51 	acl_t	aclp;
52 	int	error;
53 
54 	aclp = acl_init(ACL_MAX_ENTRIES);
55 	if (aclp == NULL)
56 		return (NULL);
57 
58 	error = __acl_get_file(path_p, type, &aclp->ats_acl);
59 	if (error) {
60 		acl_free(aclp);
61 		return (NULL);
62 	}
63 
64 	return (aclp);
65 }
66 
67 acl_t
68 acl_get_fd(int fd)
69 {
70 	acl_t	aclp;
71 	int	error;
72 
73 	aclp = acl_init(ACL_MAX_ENTRIES);
74 	if (aclp == NULL)
75 		return (NULL);
76 
77 	error = ___acl_get_fd(fd, ACL_TYPE_ACCESS, &aclp->ats_acl);
78 	if (error) {
79 		acl_free(aclp);
80 		return (NULL);
81 	}
82 
83 	return (aclp);
84 }
85 
86 acl_t
87 acl_get_fd_np(int fd, acl_type_t type)
88 {
89 	acl_t	aclp;
90 	int	error;
91 
92 	aclp = acl_init(ACL_MAX_ENTRIES);
93 	if (aclp == NULL)
94 		return (NULL);
95 
96 	error = ___acl_get_fd(fd, type, &aclp->ats_acl);
97 	if (error) {
98 		acl_free(aclp);
99 		return (NULL);
100 	}
101 
102 	return (aclp);
103 }
104 
105 int
106 acl_get_perm_np(acl_permset_t permset_d, acl_perm_t perm)
107 {
108 
109 	if (permset_d == NULL) {
110 		errno = EINVAL;
111 		return (-1);
112 	}
113 
114 	switch(perm) {
115 	case ACL_READ:
116 	case ACL_WRITE:
117 	case ACL_EXECUTE:
118 		if (*permset_d & perm)
119 			return (1);
120 		break;
121 	default:
122 		errno = EINVAL;
123 		return (-1);
124 	}
125 
126 	return (0);
127 }
128 
129 /*
130  * acl_get_permset() (23.4.17): return via permset_p a descriptor to
131  * the permission set in the ACL entry entry_d.
132  */
133 int
134 acl_get_permset(acl_entry_t entry_d, acl_permset_t *permset_p)
135 {
136 
137 	if (entry_d == NULL || permset_p == NULL) {
138 		errno = EINVAL;
139 		return (-1);
140 	}
141 
142 	*permset_p = &entry_d->ae_perm;
143 
144 	return (0);
145 }
146 
147 /*
148  * acl_get_qualifier() (23.4.18): retrieve the qualifier of the tag
149  * for the ACL entry entry_d.
150  */
151 void *
152 acl_get_qualifier(acl_entry_t entry_d)
153 {
154 	uid_t *retval;
155 
156 	if (entry_d == NULL) {
157 		errno = EINVAL;
158 		return (NULL);
159 	}
160 
161 	switch(entry_d->ae_tag) {
162 	case ACL_USER:
163 	case ACL_GROUP:
164 		retval = malloc(sizeof(uid_t));
165 		if (retval == NULL)
166 			return (NULL);
167 		*retval = entry_d->ae_id;
168 		return (retval);
169 	}
170 
171 	errno = EINVAL;
172 	return (NULL);
173 }
174 
175 /*
176  * acl_get_tag_type() (23.4.19): return the tag type for the ACL
177  * entry entry_p.
178  */
179 int
180 acl_get_tag_type(acl_entry_t entry_d, acl_tag_t *tag_type_p)
181 {
182 
183 	if (entry_d == NULL || tag_type_p == NULL) {
184 		errno = EINVAL;
185 		return (-1);
186 	}
187 
188 	*tag_type_p = entry_d->ae_tag;
189 
190 	return (0);
191 }
192