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