2 Copyright (c) 2010 Frank Lahm <franklahm@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
17 #endif /* HAVE_CONFIG_H */
19 #ifdef HAVE_NFSv4_ACLS
22 #include <sys/types.h>
31 #include <atalk/logger.h>
32 #include <atalk/afp.h>
33 #include <atalk/util.h>
34 #include <atalk/acl.h>
36 /* Get ACL. Allocates storage as needed. Caller must free.
37 * Returns no of ACEs or -1 on error. */
38 int get_nfsv4_acl(const char *name, ace_t **retAces)
45 /* Only call acl() for regular files and directories, otherwise just return 0 */
46 if (lstat(name, &st) != 0)
48 if ( ! (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)))
51 if ((ace_count = acl(name, ACE_GETACLCNT, 0, NULL)) == 0)
54 if (ace_count == -1) {
55 LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl('%s/%s', ACE_GETACLCNT): ace_count %i, error: %s",
56 getcwdpath(), name, ace_count, strerror(errno));
60 aces = malloc(ace_count * sizeof(ace_t));
62 LOG(log_error, logtype_afpd, "get_nfsv4_acl: malloc error");
66 if ( (acl(name, ACE_GETACL, ace_count, aces)) == -1 ) {
67 LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl(ACE_GETACL) error");
72 LOG(log_debug9, logtype_afpd, "get_nfsv4_acl: file: %s -> No. of ACEs: %d", name, ace_count);
79 Remove any trivial ACE "in-place". Returns no of non-trivial ACEs
81 int strip_trivial_aces(ace_t **saces, int sacecount)
88 /* Count non-trivial ACEs */
89 for (i=0; i < sacecount; ) {
90 if ( ! (aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
94 /* malloc buffer for new ACL */
95 if ((new_aces = malloc(nontrivaces * sizeof(ace_t))) == NULL) {
96 LOG(log_error, logtype_afpd, "strip_trivial_aces: malloc %s", strerror(errno));
100 /* Copy non-trivial ACEs */
101 for (i=0, j=0; i < sacecount; ) {
102 if ( ! (aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE))) {
103 memcpy(&new_aces[j], &aces[i], sizeof(ace_t));
112 LOG(log_debug7, logtype_afpd, "strip_trivial_aces: non-trivial ACEs: %d", nontrivaces);
120 ace_t *concat_aces(ace_t *aces1, int ace1count, ace_t *aces2, int ace2count)
125 /* malloc buffer for new ACL */
126 if ((new_aces = malloc((ace1count + ace2count) * sizeof(ace_t))) == NULL) {
127 LOG(log_error, logtype_afpd, "combine_aces: malloc %s", strerror(errno));
131 /* Copy ACEs from buf1 */
132 for (i=0; i < ace1count; ) {
133 memcpy(&new_aces[i], &aces1[i], sizeof(ace_t));
139 /* Copy ACEs from buf2 */
140 for (i=0; i < ace2count; ) {
141 memcpy(&new_aces[j], &aces2[i], sizeof(ace_t));
149 Remove non-trivial ACEs "in-place". Returns no of trivial ACEs.
151 int strip_nontrivial_aces(ace_t **saces, int sacecount)
155 ace_t *aces = *saces;
158 /* Count trivial ACEs */
159 for (i=0; i < sacecount; ) {
160 if ((aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
164 /* malloc buffer for new ACL */
165 if ((new_aces = malloc(trivaces * sizeof(ace_t))) == NULL) {
166 LOG(log_error, logtype_afpd, "strip_nontrivial_aces: malloc %s", strerror(errno));
170 /* Copy trivial ACEs */
171 for (i=0, j=0; i < sacecount; ) {
172 if ((aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE))) {
173 memcpy(&new_aces[j], &aces[i], sizeof(ace_t));
182 LOG(log_debug7, logtype_afpd, "strip_nontrivial_aces: trivial ACEs: %d", trivaces);
188 * Change mode of file preserving existing explicit ACEs
191 * (1) reads objects ACL (acl1)
192 * (2) removes all trivial ACEs from the ACL by calling strip_trivial_aces(), possibly
193 * leaving 0 ACEs in the ACL if there were only trivial ACEs as mapped from the mode
194 * (3) calls chmod() with mode
195 * (4) reads the changed ACL (acl2) which
196 * a) might still contain explicit ACEs (up to onnv132)
197 * b) will have any explicit ACE removed (starting with onnv145/Openindiana)
198 * (5) strip any explicit ACE from acl2 using strip_nontrivial_aces()
199 * (6) merge acl2 and acl2
200 * (7) set the ACL merged ACL on the object
202 int nfsv4_chmod(char *name, mode_t mode)
208 static int set_acl_vfs(const struct vol *vol, char *name, int inherit, char *ibuf)
210 int ret, i, nfsv4_ace_count, tocopy_aces_count = 0, new_aces_count = 0, trivial_ace_count = 0;
211 ace_t *old_aces, *new_aces = NULL;
215 LOG(log_debug9, logtype_afpd, "set_acl: BEGIN");
217 /* Get no of ACEs the client put on the wire */
218 ace_count = htonl(*((uint32_t *)ibuf));
219 ibuf += 8; /* skip ACL flags (see acls.h) */
222 /* inherited + trivial ACEs */
223 flags = ACE_INHERITED_ACE | ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
225 /* only trivial ACEs */
226 flags = ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
228 /* Get existing ACL and count ACEs which have to be copied */
229 if ((nfsv4_ace_count = get_nfsv4_acl(name, &old_aces)) == -1)
231 for ( i=0; i < nfsv4_ace_count; i++) {
232 if (old_aces[i].a_flags & flags)
236 /* Now malloc buffer exactly sized to fit all new ACEs */
237 new_aces = malloc( (ace_count + tocopy_aces_count) * sizeof(ace_t) );
238 if (new_aces == NULL) {
239 LOG(log_error, logtype_afpd, "set_acl: malloc %s", strerror(errno));
244 /* Start building new ACL */
246 /* Copy local inherited ACEs. Therefore we have 'Darwin canonical order' (see chmod there):
247 inherited ACEs first. */
249 for (i=0; i < nfsv4_ace_count; i++) {
250 if (old_aces[i].a_flags & ACE_INHERITED_ACE) {
251 memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
256 LOG(log_debug7, logtype_afpd, "set_acl: copied %d inherited ACEs", new_aces_count);
258 /* Now the ACEs from the client */
259 ret = map_acl(DARWIN_2_SOLARIS, &new_aces[new_aces_count], (darwin_ace_t *)ibuf, ace_count);
264 new_aces_count += ace_count;
265 LOG(log_debug7, logtype_afpd, "set_acl: mapped %d ACEs from client", ace_count);
267 /* Now copy the trivial ACEs */
268 for (i=0; i < nfsv4_ace_count; i++) {
269 if (old_aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
270 memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
275 LOG(log_debug7, logtype_afpd, "set_acl: copied %d trivial ACEs", trivial_ace_count);
277 /* Ressourcefork first.
278 Note: for dirs we set the same ACL on the .AppleDouble/.Parent _file_. This
279 might be strange for ACE_DELETE_CHILD and for inheritance flags. */
280 if ( (ret = vol->vfs->vfs_acl(vol, name, ACE_SETACL, new_aces_count, new_aces)) != 0) {
281 LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
282 if (errno == (EACCES | EPERM))
284 else if (errno == ENOENT)
290 if ( (ret = acl(name, ACE_SETACL, new_aces_count, new_aces)) != 0) {
291 LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
292 if (errno == (EACCES | EPERM))
294 else if (errno == ENOENT)
307 LOG(log_debug9, logtype_afpd, "set_acl: END");
312 #endif /* HAVE_NFSv4_ACLS */