xref: /freebsd/lib/libc/posix1e/acl_set.c (revision b5a8f767a62e0253ce02878cd6d69ea7f9574d1a)
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 (acl == NULL || path_p == NULL) {
55 		errno = EINVAL;
56 		return (-1);
57 	}
58 	if (_posix1e_acl(acl, type)) {
59 		error = _posix1e_acl_sort(acl);
60 		if (error) {
61 			errno = error;
62 			return (-1);
63 		}
64 	}
65 
66 	acl->ats_cur_entry = 0;
67 
68 	return (__acl_set_file(path_p, type, &acl->ats_acl));
69 }
70 
71 int
72 acl_set_fd(int fd, acl_t acl)
73 {
74 	int	error;
75 
76 	error = _posix1e_acl_sort(acl);
77 	if (error) {
78 		errno = error;
79 		return(-1);
80 	}
81 
82 	acl->ats_cur_entry = 0;
83 
84 	return (___acl_set_fd(fd, ACL_TYPE_ACCESS, &acl->ats_acl));
85 }
86 
87 int
88 acl_set_fd_np(int fd, acl_t acl, acl_type_t type)
89 {
90 	int	error;
91 
92 	if (_posix1e_acl(acl, type)) {
93 		error = _posix1e_acl_sort(acl);
94 		if (error) {
95 			errno = error;
96 			return (-1);
97 		}
98 	}
99 
100 	acl->ats_cur_entry = 0;
101 
102 	return (___acl_set_fd(fd, type, &acl->ats_acl));
103 }
104 
105 /*
106  * acl_set_permset() (23.4.23): sets the permissions of ACL entry entry_d
107  * with the permissions in permset_d
108  */
109 int
110 acl_set_permset(acl_entry_t entry_d, acl_permset_t permset_d)
111 {
112 
113 	if (!entry_d) {
114 		errno = EINVAL;
115 		return (-1);
116 	}
117 
118 	entry_d->ae_perm = *permset_d;
119 
120 	return (0);
121 }
122 
123 /*
124  * acl_set_qualifier() sets the qualifier (ae_id) of the tag for
125  * ACL entry entry_d to the value referred to by tag_qualifier_p
126  */
127 int
128 acl_set_qualifier(acl_entry_t entry_d, const void *tag_qualifier_p)
129 {
130 	if (!entry_d || !tag_qualifier_p) {
131 		errno = EINVAL;
132 		return (-1);
133 	}
134 
135 	switch(entry_d->ae_tag) {
136 	case ACL_USER:
137 	case ACL_GROUP:
138 		entry_d->ae_id = *(uid_t *)tag_qualifier_p;
139 		break;
140 	default:
141 		errno = EINVAL;
142 		return (-1);
143 	}
144 
145 	return (0);
146 }
147 
148 /*
149  * acl_set_tag_type() sets the tag type for ACL entry entry_d to the
150  * value of tag_type
151  */
152 int
153 acl_set_tag_type(acl_entry_t entry_d, acl_tag_t tag_type)
154 {
155 
156 	if (entry_d == NULL) {
157 		errno = EINVAL;
158 		return (-1);
159 	}
160 
161 	switch(tag_type) {
162 	case ACL_USER_OBJ:
163 	case ACL_USER:
164 	case ACL_GROUP_OBJ:
165 	case ACL_GROUP:
166 	case ACL_MASK:
167 	case ACL_OTHER:
168 		entry_d->ae_tag = tag_type;
169 		return (0);
170 	}
171 
172 	errno = EINVAL;
173 	return (-1);
174 }
175