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