]> arthur.barton.de Git - netatalk.git/blob - libatalk/acl/unix.c
chmod wrapper for onnv145+ to preserve ACL
[netatalk.git] / libatalk / acl / unix.c
1 /*
2   Copyright (c) 2010 Frank Lahm <franklahm@gmail.com>
3
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.
8
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.
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif /* HAVE_CONFIG_H */
18
19 #ifdef HAVE_NFSv4_ACLS
20
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/acl.h>
30
31 #include <atalk/logger.h>
32 #include <atalk/afp.h>
33 #include <atalk/util.h>
34 #include <atalk/acl.h>
35
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)
39 {
40     int ace_count = -1;
41     ace_t *aces;
42     struct stat st;
43
44     *retAces = NULL;
45     /* Only call acl() for regular files and directories, otherwise just return 0 */
46     if (lstat(name, &st) != 0)
47         return -1;
48     if ( ! (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)))
49         return 0;
50
51     if ((ace_count = acl(name, ACE_GETACLCNT, 0, NULL)) == 0)
52         return 0;
53
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));
57         return -1;
58     }
59
60     aces = malloc(ace_count * sizeof(ace_t));
61     if (aces == NULL) {
62         LOG(log_error, logtype_afpd, "get_nfsv4_acl: malloc error");
63         return -1;
64     }
65
66     if ( (acl(name, ACE_GETACL, ace_count, aces)) == -1 ) {
67         LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl(ACE_GETACL) error");
68         free(aces);
69         return -1;
70     }
71
72     LOG(log_debug9, logtype_afpd, "get_nfsv4_acl: file: %s -> No. of ACEs: %d", name, ace_count);
73     *retAces = aces;
74
75     return ace_count;
76 }
77
78 /*
79   Concatenate ACEs
80 */
81 ace_t *concat_aces(ace_t *aces1, int ace1count, ace_t *aces2, int ace2count)
82 {
83     ace_t *new_aces;
84     int i, j;
85
86     /* malloc buffer for new ACL */
87     if ((new_aces = malloc((ace1count + ace2count) * sizeof(ace_t))) == NULL) {
88         LOG(log_error, logtype_afpd, "combine_aces: malloc %s", strerror(errno));
89         return NULL;
90     }
91
92     /* Copy ACEs from buf1 */
93     for (i=0; i < ace1count; ) {
94         memcpy(&new_aces[i], &aces1[i], sizeof(ace_t));
95         i++;
96     }
97
98     j = i;
99
100     /* Copy ACEs from buf2 */
101     for (i=0; i < ace2count; ) {
102         memcpy(&new_aces[j], &aces2[i], sizeof(ace_t));
103         i++;
104         j++;
105     }
106     return new_aces;
107 }
108
109 /*
110   Remove any trivial ACE "in-place". Returns no of non-trivial ACEs
111 */
112 int strip_trivial_aces(ace_t **saces, int sacecount)
113 {
114     int i,j;
115     int nontrivaces = 0;
116     ace_t *aces = *saces;
117     ace_t *new_aces;
118
119     if (aces == NULL || sacecount <= 0)
120         return 0;
121
122     /* Count non-trivial ACEs */
123     for (i=0; i < sacecount; ) {
124         if ( ! (aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
125             nontrivaces++;
126         i++;
127     }
128     /* malloc buffer for new ACL */
129     if ((new_aces = malloc(nontrivaces * sizeof(ace_t))) == NULL) {
130         LOG(log_error, logtype_afpd, "strip_trivial_aces: malloc %s", strerror(errno));
131         return -1;
132     }
133
134     /* Copy non-trivial ACEs */
135     for (i=0, j=0; i < sacecount; ) {
136         if ( ! (aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE))) {
137             memcpy(&new_aces[j], &aces[i], sizeof(ace_t));
138             j++;
139         }
140         i++;
141     }
142
143     free(aces);
144     *saces = new_aces;
145
146     LOG(log_debug7, logtype_afpd, "strip_trivial_aces: non-trivial ACEs: %d", nontrivaces);
147
148     return nontrivaces;
149 }
150
151 /*
152   Remove non-trivial ACEs "in-place". Returns no of trivial ACEs.
153 */
154 int strip_nontrivial_aces(ace_t **saces, int sacecount)
155 {
156     int i,j;
157     int trivaces = 0;
158     ace_t *aces = *saces;
159     ace_t *new_aces;
160
161     /* Count trivial ACEs */
162     for (i=0; i < sacecount; ) {
163         if ((aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
164             trivaces++;
165         i++;
166     }
167     /* malloc buffer for new ACL */
168     if ((new_aces = malloc(trivaces * sizeof(ace_t))) == NULL) {
169         LOG(log_error, logtype_afpd, "strip_nontrivial_aces: malloc %s", strerror(errno));
170         return -1;
171     }
172
173     /* Copy trivial ACEs */
174     for (i=0, j=0; i < sacecount; ) {
175         if ((aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE))) {
176             memcpy(&new_aces[j], &aces[i], sizeof(ace_t));
177             j++;
178         }
179         i++;
180     }
181     /* Free old ACEs */
182     free(aces);
183     *saces = new_aces;
184
185     LOG(log_debug7, logtype_afpd, "strip_nontrivial_aces: trivial ACEs: %d", trivaces);
186
187     return trivaces;
188 }
189
190 /*!
191  * Change mode of file preserving existing explicit ACEs
192  *
193  * nfsv4_chmod
194  * (1) reads objects ACL (acl1)
195  * (2) removes all trivial ACEs from the ACL by calling strip_trivial_aces(), possibly
196  *     leaving 0 ACEs in the ACL if there were only trivial ACEs as mapped from the mode
197  * (3) calls chmod() with mode
198  * (4) reads the changed ACL (acl2) which
199  *     a) might still contain explicit ACEs (up to onnv132)
200  *     b) will have any explicit ACE removed (starting with onnv145/Openindiana)
201  * (5) strip any explicit ACE from acl2 using strip_nontrivial_aces()
202  * (6) merge acl2 and acl2
203  * (7) set the ACL merged ACL on the object
204  */
205 int nfsv4_chmod(char *name, mode_t mode)
206 {
207     int ret = -1;
208     int noaces, nnaces;
209     ace_t *oacl = NULL, *nacl = NULL, *cacl;
210
211     if ((noaces = get_nfsv4_acl(name, &oacl)) == -1) /* (1) */
212         goto exit;
213     if ((noaces = strip_trivial_aces(&oacl, noaces)) == -1) /* (2) */
214         goto exit;
215
216     if (chmod(name, mode) != 0) /* (3) */
217         goto exit;
218
219     if ((nnaces = get_nfsv4_acl(name, &nacl)) == -1) /* (4) */
220         goto exit;
221     if ((nnaces = strip_nontrivial_aces(&nacl, nnaces)) == -1) /* (5) */
222         goto exit;
223
224     if ((cacl = concat_aces(oacl, noaces, nacl, nnaces)) == NULL) /* (6) */
225         goto exit;
226
227     if ((ret = acl(name, ACE_SETACL, noaces + nnaces, cacl)) != 0) {
228         LOG(log_error, logtype_afpd, "nfsv4_chmod: error setting acl: %s", strerror(errno));
229         goto exit;
230     }
231
232 exit:
233     if (oacl) free(oacl);
234     if (nacl) free(nacl);
235     if (cacl) free(cacl);
236
237     return ret;
238 }
239
240 #if 0
241 static int set_acl_vfs(const struct vol *vol, char *name, int inherit, char *ibuf)
242 {
243     int ret, i, nfsv4_ace_count, tocopy_aces_count = 0, new_aces_count = 0, trivial_ace_count = 0;
244     ace_t *old_aces, *new_aces = NULL;
245     uint16_t flags;
246     uint32_t ace_count;
247
248     LOG(log_debug9, logtype_afpd, "set_acl: BEGIN");
249
250     /*  Get no of ACEs the client put on the wire */
251     ace_count = htonl(*((uint32_t *)ibuf));
252     ibuf += 8;      /* skip ACL flags (see acls.h) */
253
254     if (inherit)
255         /* inherited + trivial ACEs */
256         flags = ACE_INHERITED_ACE | ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
257     else
258         /* only trivial ACEs */
259         flags = ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
260
261     /* Get existing ACL and count ACEs which have to be copied */
262     if ((nfsv4_ace_count = get_nfsv4_acl(name, &old_aces)) == -1)
263         return AFPERR_MISC;
264     for ( i=0; i < nfsv4_ace_count; i++) {
265         if (old_aces[i].a_flags & flags)
266             tocopy_aces_count++;
267     }
268
269     /* Now malloc buffer exactly sized to fit all new ACEs */
270     new_aces = malloc( (ace_count + tocopy_aces_count) * sizeof(ace_t) );
271     if (new_aces == NULL) {
272         LOG(log_error, logtype_afpd, "set_acl: malloc %s", strerror(errno));
273         ret = AFPERR_MISC;
274         goto cleanup;
275     }
276
277     /* Start building new ACL */
278
279     /* Copy local inherited ACEs. Therefore we have 'Darwin canonical order' (see chmod there):
280        inherited ACEs first. */
281     if (inherit) {
282         for (i=0; i < nfsv4_ace_count; i++) {
283             if (old_aces[i].a_flags & ACE_INHERITED_ACE) {
284                 memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
285                 new_aces_count++;
286             }
287         }
288     }
289     LOG(log_debug7, logtype_afpd, "set_acl: copied %d inherited ACEs", new_aces_count);
290
291     /* Now the ACEs from the client */
292     ret = map_acl(DARWIN_2_SOLARIS, &new_aces[new_aces_count], (darwin_ace_t *)ibuf, ace_count);
293     if (ret == -1) {
294         ret = AFPERR_PARAM;
295         goto cleanup;
296     }
297     new_aces_count += ace_count;
298     LOG(log_debug7, logtype_afpd, "set_acl: mapped %d ACEs from client", ace_count);
299
300     /* Now copy the trivial ACEs */
301     for (i=0; i < nfsv4_ace_count; i++) {
302         if (old_aces[i].a_flags  & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
303             memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
304             new_aces_count++;
305             trivial_ace_count++;
306         }
307     }
308     LOG(log_debug7, logtype_afpd, "set_acl: copied %d trivial ACEs", trivial_ace_count);
309
310     /* Ressourcefork first.
311        Note: for dirs we set the same ACL on the .AppleDouble/.Parent _file_. This
312        might be strange for ACE_DELETE_CHILD and for inheritance flags. */
313     if ( (ret = vol->vfs->vfs_acl(vol, name, ACE_SETACL, new_aces_count, new_aces)) != 0) {
314         LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
315         if (errno == (EACCES | EPERM))
316             ret = AFPERR_ACCESS;
317         else if (errno == ENOENT)
318             ret = AFPERR_NOITEM;
319         else
320             ret = AFPERR_MISC;
321         goto cleanup;
322     }
323     if ( (ret = acl(name, ACE_SETACL, new_aces_count, new_aces)) != 0) {
324         LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
325         if (errno == (EACCES | EPERM))
326             ret = AFPERR_ACCESS;
327         else if (errno == ENOENT)
328             ret = AFPERR_NOITEM;
329         else
330             ret = AFPERR_MISC;
331         goto cleanup;
332     }
333
334     ret = AFP_OK;
335
336 cleanup:
337     free(old_aces);
338     free(new_aces);
339
340     LOG(log_debug9, logtype_afpd, "set_acl: END");
341     return ret;
342 }
343 #endif  /* 0 */
344
345 #endif /* HAVE_NFSv4_ACLS */