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