xref: /freebsd/lib/libc/posix1e/acl_get.c (revision 1a2cdef4962b47be5057809ce730a733b7f3c27c)
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 	struct acl	*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);
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 	struct acl	*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);
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 	struct acl	*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);
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 	switch(perm) {
113 	case ACL_READ:
114 	case ACL_WRITE:
115 	case ACL_EXECUTE:
116 		if (*permset_d & perm)
117 			return 1;
118 		break;
119 	default:
120 		errno = EINVAL;
121 		return -1;
122 	}
123 
124 	return 0;
125 }
126 
127 int
128 acl_get_permset(acl_entry_t entry_d, acl_permset_t *permset_p)
129 {
130 
131 	if (!entry_d || !permset_p) {
132 		errno = EINVAL;
133 		return -1;
134 	}
135 
136 	*permset_p = &entry_d->ae_perm;
137 
138 	return 0;
139 }
140 
141 void *
142 acl_get_qualifier(acl_entry_t entry_d)
143 {
144 	uid_t *retval;
145 
146 	if (!entry_d) {
147 		errno = EINVAL;
148 		return NULL;
149 	}
150 
151 	switch(entry_d->ae_tag) {
152 	case ACL_USER:
153 	case ACL_GROUP:
154 		retval = malloc(sizeof(uid_t));
155 		if (retval) {
156 			*retval = entry_d->ae_id;
157 			return retval;
158 		}
159 	}
160 
161 	errno = EINVAL;
162 	return NULL;
163 }
164 
165 int
166 acl_get_tag_type(acl_entry_t entry_d, acl_tag_t *tag_type_p)
167 {
168 
169 	if (!entry_d || !tag_type_p) {
170 		errno = EINVAL;
171 		return -1;
172 	}
173 
174 	*tag_type_p = entry_d->ae_tag;
175 
176 	return 0;
177 }
178