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