]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/posix1e/acl_strip.c
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r305575, and update
[FreeBSD/FreeBSD.git] / lib / libc / posix1e / acl_strip.c
1 /*-
2  * Copyright (c) 2001 Chris D. Faulhaber
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 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <errno.h>
31 #include <stdio.h>
32 #include <assert.h>
33 #include <sys/acl.h>
34 #include <sys/stat.h>
35
36 #include "acl_support.h"
37
38 /*
39  * These routines from sys/kern/subr_acl_nfs4.c are used by both kernel
40  * and libc.
41  */
42 void    acl_nfs4_sync_mode_from_acl(mode_t *_mode, const struct acl *aclp);
43 void    acl_nfs4_trivial_from_mode_libc(struct acl *aclp, int file_owner_id,
44             int canonical_six);
45
46 static acl_t
47 _nfs4_acl_strip_np(const acl_t aclp, int canonical_six)
48 {
49         acl_t newacl;
50         mode_t mode = 0;
51
52         newacl = acl_init(ACL_MAX_ENTRIES);
53         if (newacl == NULL) {
54                 errno = ENOMEM;
55                 return (NULL);
56         }
57
58         _acl_brand_as(newacl, ACL_BRAND_NFS4);
59
60         acl_nfs4_sync_mode_from_acl(&mode, &(aclp->ats_acl));
61         acl_nfs4_trivial_from_mode_libc(&(newacl->ats_acl), mode, canonical_six);
62
63         return (newacl);
64 }
65
66 static acl_t
67 _posix1e_acl_strip_np(const acl_t aclp, int recalculate_mask)
68 {
69         acl_t acl_new, acl_old;
70         acl_entry_t entry, entry_new;
71         acl_permset_t perm;
72         acl_tag_t tag;
73         int entry_id, have_mask_entry;
74
75         assert(_acl_brand(aclp) == ACL_BRAND_POSIX);
76
77         acl_old = acl_dup(aclp);
78         if (acl_old == NULL)
79                 return (NULL);
80
81         assert(_acl_brand(acl_old) == ACL_BRAND_POSIX);
82
83         have_mask_entry = 0;
84         acl_new = acl_init(ACL_MAX_ENTRIES);
85         if (acl_new == NULL) {
86                 acl_free(acl_old);
87                 return (NULL);
88         }
89         tag = ACL_UNDEFINED_TAG;
90
91         /* only save the default user/group/other entries */
92         entry_id = ACL_FIRST_ENTRY;
93         while (acl_get_entry(acl_old, entry_id, &entry) == 1) {
94                 entry_id = ACL_NEXT_ENTRY;
95
96                 assert(_entry_brand(entry) == ACL_BRAND_POSIX);
97
98                 if (acl_get_tag_type(entry, &tag) == -1)
99                         goto fail;
100
101                 switch(tag) {
102                 case ACL_USER_OBJ:
103                 case ACL_GROUP_OBJ:
104                 case ACL_OTHER:
105                         if (acl_get_tag_type(entry, &tag) == -1)
106                                 goto fail;
107                         if (acl_get_permset(entry, &perm) == -1)
108                                 goto fail;
109                         if (acl_create_entry(&acl_new, &entry_new) == -1)
110                                 goto fail;
111                         if (acl_set_tag_type(entry_new, tag) == -1)
112                                 goto fail;
113                         if (acl_set_permset(entry_new, perm) == -1)
114                                 goto fail;
115                         if (acl_copy_entry(entry_new, entry) == -1)
116                                 goto fail;
117                         assert(_entry_brand(entry_new) == ACL_BRAND_POSIX);
118                         break;
119                 case ACL_MASK:
120                         have_mask_entry = 1;
121                         break;
122                 default:
123                         break;
124                 }
125         }
126
127         assert(_acl_brand(acl_new) == ACL_BRAND_POSIX);
128
129         if (have_mask_entry && recalculate_mask) {
130                 if (acl_calc_mask(&acl_new) == -1)
131                         goto fail;
132         }
133
134         return (acl_new);
135
136 fail:
137         acl_free(acl_new);
138         acl_free(acl_old);
139
140         return (NULL);
141 }
142
143 acl_t
144 acl_strip_np(const acl_t aclp, int recalculate_mask)
145 {
146         switch (_acl_brand(aclp)) {
147         case ACL_BRAND_NFS4:
148                 return (_nfs4_acl_strip_np(aclp, 0));
149
150         case ACL_BRAND_POSIX:
151                 return (_posix1e_acl_strip_np(aclp, recalculate_mask));
152
153         default:
154                 errno = EINVAL;
155                 return (NULL);
156         }
157 }
158
159 /*
160  * Return 1, if ACL is trivial, 0 otherwise.
161  *
162  * ACL is trivial, iff its meaning could be fully expressed using just file
163  * mode.  In other words, ACL is trivial iff it doesn't have "+" to the right
164  * of the mode bits in "ls -l" output ;-)
165  */
166 int
167 acl_is_trivial_np(const acl_t aclp, int *trivialp)
168 {
169         acl_t tmpacl;
170         int differs;
171
172         if (aclp == NULL || trivialp == NULL) {
173                 errno = EINVAL;
174                 return (-1);
175         }
176
177         switch (_acl_brand(aclp)) {
178         case ACL_BRAND_POSIX:
179                 if (aclp->ats_acl.acl_cnt == 3)
180                         *trivialp = 1;
181                 else
182                         *trivialp = 0;
183
184                 return (0);
185
186         case ACL_BRAND_NFS4:
187                 /*
188                  * If the ACL has more than canonical six entries,
189                  * it's non trivial by definition.
190                  */
191                 if (aclp->ats_acl.acl_cnt > 6) {
192                         *trivialp = 0;
193                         return (0);
194                 }
195                         
196                 /*
197                  * Calculate trivial ACL - using acl_strip_np(3) - and compare
198                  * with the original.
199                  */
200                 tmpacl = _nfs4_acl_strip_np(aclp, 0);
201                 if (tmpacl == NULL)
202                         return (-1);
203
204                 differs = _acl_differs(aclp, tmpacl);
205                 acl_free(tmpacl);
206
207                 if (differs == 0) {
208                         *trivialp = 1;
209                         return (0);
210                 }
211
212                 /*
213                  * Try again with an old-style, "canonical six" trivial ACL.
214                  */
215                 tmpacl = _nfs4_acl_strip_np(aclp, 1);
216                 if (tmpacl == NULL)
217                         return (-1);
218
219                 differs = _acl_differs(aclp, tmpacl);
220                 acl_free(tmpacl);
221
222                 if (differs)
223                         *trivialp = 0;
224                 else
225                         *trivialp = 1;
226
227                 return (0);
228
229         default:
230                 errno = EINVAL;
231                 return (-1);
232         }
233 }