]> arthur.barton.de Git - netatalk.git/blob - libatalk/acl/unix.c
include header
[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   Remove any trivial ACE "in-place". Returns no of non-trivial ACEs
80 */
81 int strip_trivial_aces(ace_t **saces, int sacecount)
82 {
83     int i,j;
84     int nontrivaces = 0;
85     ace_t *aces = *saces;
86     ace_t *new_aces;
87
88     /* Count non-trivial ACEs */
89     for (i=0; i < sacecount; ) {
90         if ( ! (aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
91             nontrivaces++;
92         i++;
93     }
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));
97         return -1;
98     }
99
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));
104             j++;
105         }
106         i++;
107     }
108
109     free(aces);
110     *saces = new_aces;
111
112     LOG(log_debug7, logtype_afpd, "strip_trivial_aces: non-trivial ACEs: %d", nontrivaces);
113
114     return nontrivaces;
115 }
116
117 /*
118   Concatenate ACEs
119 */
120 ace_t *concat_aces(ace_t *aces1, int ace1count, ace_t *aces2, int ace2count)
121 {
122     ace_t *new_aces;
123     int i, j;
124
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));
128         return NULL;
129     }
130
131     /* Copy ACEs from buf1 */
132     for (i=0; i < ace1count; ) {
133         memcpy(&new_aces[i], &aces1[i], sizeof(ace_t));
134         i++;
135     }
136
137     j = i;
138
139     /* Copy ACEs from buf2 */
140     for (i=0; i < ace2count; ) {
141         memcpy(&new_aces[j], &aces2[i], sizeof(ace_t));
142         i++;
143         j++;
144     }
145     return new_aces;
146 }
147
148 /*
149   Remove non-trivial ACEs "in-place". Returns no of trivial ACEs.
150 */
151 int strip_nontrivial_aces(ace_t **saces, int sacecount)
152 {
153     int i,j;
154     int trivaces = 0;
155     ace_t *aces = *saces;
156     ace_t *new_aces;
157
158     /* Count trivial ACEs */
159     for (i=0; i < sacecount; ) {
160         if ((aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
161             trivaces++;
162         i++;
163     }
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));
167         return -1;
168     }
169
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));
174             j++;
175         }
176         i++;
177     }
178     /* Free old ACEs */
179     free(aces);
180     *saces = new_aces;
181
182     LOG(log_debug7, logtype_afpd, "strip_nontrivial_aces: trivial ACEs: %d", trivaces);
183
184     return trivaces;
185 }
186
187 /*!
188  * Change mode of file preserving existing explicit ACEs
189  *
190  * nfsv4_chmod
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
201  */
202 int nfsv4_chmod(char *name, mode_t mode)
203 {
204
205 }
206
207 #if 0
208 static int set_acl_vfs(const struct vol *vol, char *name, int inherit, char *ibuf)
209 {
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;
212     uint16_t flags;
213     uint32_t ace_count;
214
215     LOG(log_debug9, logtype_afpd, "set_acl: BEGIN");
216
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) */
220
221     if (inherit)
222         /* inherited + trivial ACEs */
223         flags = ACE_INHERITED_ACE | ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
224     else
225         /* only trivial ACEs */
226         flags = ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
227
228     /* Get existing ACL and count ACEs which have to be copied */
229     if ((nfsv4_ace_count = get_nfsv4_acl(name, &old_aces)) == -1)
230         return AFPERR_MISC;
231     for ( i=0; i < nfsv4_ace_count; i++) {
232         if (old_aces[i].a_flags & flags)
233             tocopy_aces_count++;
234     }
235
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));
240         ret = AFPERR_MISC;
241         goto cleanup;
242     }
243
244     /* Start building new ACL */
245
246     /* Copy local inherited ACEs. Therefore we have 'Darwin canonical order' (see chmod there):
247        inherited ACEs first. */
248     if (inherit) {
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));
252                 new_aces_count++;
253             }
254         }
255     }
256     LOG(log_debug7, logtype_afpd, "set_acl: copied %d inherited ACEs", new_aces_count);
257
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);
260     if (ret == -1) {
261         ret = AFPERR_PARAM;
262         goto cleanup;
263     }
264     new_aces_count += ace_count;
265     LOG(log_debug7, logtype_afpd, "set_acl: mapped %d ACEs from client", ace_count);
266
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));
271             new_aces_count++;
272             trivial_ace_count++;
273         }
274     }
275     LOG(log_debug7, logtype_afpd, "set_acl: copied %d trivial ACEs", trivial_ace_count);
276
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))
283             ret = AFPERR_ACCESS;
284         else if (errno == ENOENT)
285             ret = AFPERR_NOITEM;
286         else
287             ret = AFPERR_MISC;
288         goto cleanup;
289     }
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))
293             ret = AFPERR_ACCESS;
294         else if (errno == ENOENT)
295             ret = AFPERR_NOITEM;
296         else
297             ret = AFPERR_MISC;
298         goto cleanup;
299     }
300
301     ret = AFP_OK;
302
303 cleanup:
304     free(old_aces);
305     free(new_aces);
306
307     LOG(log_debug9, logtype_afpd, "set_acl: END");
308     return ret;
309 }
310 #endif  /* 0 */
311
312 #endif /* HAVE_NFSv4_ACLS */