xref: /freebsd/lib/libc/posix1e/acl_set.c (revision b52b9d56d4e96089873a75f9e29062eec19fabba)
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_set_file -- set a file/directory ACL by name
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/types.h>
34 #include "namespace.h"
35 #include <sys/acl.h>
36 #include "un-namespace.h"
37 
38 #include <errno.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
42 #include "acl_support.h"
43 
44 /*
45  * For POSIX.1e-semantic ACLs, do a presort so the kernel doesn't have to
46  * (the POSIX.1e semantic code will reject unsorted ACL submission).  If it's
47  * not a semantic that the library knows about, just submit it flat and
48  * assume the caller knows what they're up to.
49  */
50 int
51 acl_set_file(const char *path_p, acl_type_t type, acl_t acl)
52 {
53 	int	error;
54 
55 	if (acl == NULL || path_p == NULL) {
56 		errno = EINVAL;
57 		return (-1);
58 	}
59 	if (_posix1e_acl(acl, type)) {
60 		error = _posix1e_acl_sort(acl);
61 		if (error) {
62 			errno = error;
63 			return (-1);
64 		}
65 	}
66 
67 	acl->ats_cur_entry = 0;
68 
69 	return (__acl_set_file(path_p, type, &acl->ats_acl));
70 }
71 
72 int
73 acl_set_fd(int fd, acl_t acl)
74 {
75 	int	error;
76 
77 	error = _posix1e_acl_sort(acl);
78 	if (error) {
79 		errno = error;
80 		return(-1);
81 	}
82 
83 	acl->ats_cur_entry = 0;
84 
85 	return (___acl_set_fd(fd, ACL_TYPE_ACCESS, &acl->ats_acl));
86 }
87 
88 int
89 acl_set_fd_np(int fd, acl_t acl, acl_type_t type)
90 {
91 	int	error;
92 
93 	if (_posix1e_acl(acl, type)) {
94 		error = _posix1e_acl_sort(acl);
95 		if (error) {
96 			errno = error;
97 			return (-1);
98 		}
99 	}
100 
101 	acl->ats_cur_entry = 0;
102 
103 	return (___acl_set_fd(fd, type, &acl->ats_acl));
104 }
105 
106 /*
107  * acl_set_permset() (23.4.23): sets the permissions of ACL entry entry_d
108  * with the permissions in permset_d
109  */
110 int
111 acl_set_permset(acl_entry_t entry_d, acl_permset_t permset_d)
112 {
113 
114 	if (!entry_d) {
115 		errno = EINVAL;
116 		return (-1);
117 	}
118 
119 	entry_d->ae_perm = *permset_d;
120 
121 	return (0);
122 }
123 
124 /*
125  * acl_set_qualifier() sets the qualifier (ae_id) of the tag for
126  * ACL entry entry_d to the value referred to by tag_qualifier_p
127  */
128 int
129 acl_set_qualifier(acl_entry_t entry_d, const void *tag_qualifier_p)
130 {
131 	if (!entry_d || !tag_qualifier_p) {
132 		errno = EINVAL;
133 		return (-1);
134 	}
135 
136 	switch(entry_d->ae_tag) {
137 	case ACL_USER:
138 	case ACL_GROUP:
139 		entry_d->ae_id = *(uid_t *)tag_qualifier_p;
140 		break;
141 	default:
142 		errno = EINVAL;
143 		return (-1);
144 	}
145 
146 	return (0);
147 }
148 
149 /*
150  * acl_set_tag_type() sets the tag type for ACL entry entry_d to the
151  * value of tag_type
152  */
153 int
154 acl_set_tag_type(acl_entry_t entry_d, acl_tag_t tag_type)
155 {
156 
157 	if (entry_d == NULL) {
158 		errno = EINVAL;
159 		return (-1);
160 	}
161 
162 	switch(tag_type) {
163 	case ACL_USER_OBJ:
164 	case ACL_USER:
165 	case ACL_GROUP_OBJ:
166 	case ACL_GROUP:
167 	case ACL_MASK:
168 	case ACL_OTHER:
169 		entry_d->ae_tag = tag_type;
170 		return (0);
171 	}
172 
173 	errno = EINVAL;
174 	return (-1);
175 }
176