]> arthur.barton.de Git - netatalk.git/blob - libatalk/acl/unix.c
31027abe581da133c0de503b479c2ccb301142ff
[netatalk.git] / libatalk / acl / unix.c
1 /*
2   Copyright (c) 2010 Frank Lahm <franklahm@gmail.com>
3   Copyright (c) 2011 Laura Mueller <laura-mueller@uni-duesseldorf.de>
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 */
15
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif /* HAVE_CONFIG_H */
19
20 #ifdef HAVE_ACLS
21
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29 #include <errno.h>
30 #include <sys/acl.h>
31
32 #include <atalk/logger.h>
33 #include <atalk/afp.h>
34 #include <atalk/util.h>
35 #include <atalk/acl.h>
36
37 #ifdef HAVE_SOLARIS_ACLS
38
39 /* Get ACL. Allocates storage as needed. Caller must free.
40  * Returns no of ACEs or -1 on error.  */
41 int get_nfsv4_acl(const char *name, ace_t **retAces)
42 {
43     int ace_count = -1;
44     ace_t *aces;
45     struct stat st;
46
47     *retAces = NULL;
48     /* Only call acl() for regular files and directories, otherwise just return 0 */
49     if (lstat(name, &st) != 0) {
50         LOG(log_warning, logtype_afpd, "get_nfsv4_acl(\"%s/%s\"): %s", getcwdpath(), name, strerror(errno));
51         return -1;
52     }
53
54     if (S_ISLNK(st.st_mode))
55         /* sorry, no ACLs for symlinks */
56         return 0;
57
58     if ( ! (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))) {
59         LOG(log_warning, logtype_afpd, "get_nfsv4_acl(\"%s/%s\"): special", getcwdpath(), name);
60         return 0;
61     }
62
63     if ((ace_count = acl(name, ACE_GETACLCNT, 0, NULL)) == 0) {
64         LOG(log_warning, logtype_afpd, "get_nfsv4_acl(\"%s/%s\"): 0 ACEs", getcwdpath(), name);
65         return 0;
66     }
67
68     if (ace_count == -1) {
69         LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl('%s/%s', ACE_GETACLCNT): ace_count %i, error: %s",
70             getcwdpath(), name, ace_count, strerror(errno));
71         return -1;
72     }
73
74     aces = malloc(ace_count * sizeof(ace_t));
75     if (aces == NULL) {
76         LOG(log_error, logtype_afpd, "get_nfsv4_acl: malloc error");
77         return -1;
78     }
79
80     if ( (acl(name, ACE_GETACL, ace_count, aces)) == -1 ) {
81         LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl(ACE_GETACL) error");
82         free(aces);
83         return -1;
84     }
85
86     LOG(log_debug9, logtype_afpd, "get_nfsv4_acl: file: %s -> No. of ACEs: %d", name, ace_count);
87     *retAces = aces;
88
89     return ace_count;
90 }
91
92 /*
93   Concatenate ACEs
94 */
95 ace_t *concat_aces(ace_t *aces1, int ace1count, ace_t *aces2, int ace2count)
96 {
97     ace_t *new_aces;
98     int i, j;
99
100     /* malloc buffer for new ACL */
101     if ((new_aces = malloc((ace1count + ace2count) * sizeof(ace_t))) == NULL) {
102         LOG(log_error, logtype_afpd, "combine_aces: malloc %s", strerror(errno));
103         return NULL;
104     }
105
106     /* Copy ACEs from buf1 */
107     for (i=0; i < ace1count; ) {
108         memcpy(&new_aces[i], &aces1[i], sizeof(ace_t));
109         i++;
110     }
111
112     j = i;
113
114     /* Copy ACEs from buf2 */
115     for (i=0; i < ace2count; ) {
116         memcpy(&new_aces[j], &aces2[i], sizeof(ace_t));
117         i++;
118         j++;
119     }
120     return new_aces;
121 }
122
123 /*
124   Remove any trivial ACE "in-place". Returns no of non-trivial ACEs
125 */
126 int strip_trivial_aces(ace_t **saces, int sacecount)
127 {
128     int i,j;
129     int nontrivaces = 0;
130     ace_t *aces = *saces;
131     ace_t *new_aces;
132
133     if (aces == NULL || sacecount <= 0)
134         return 0;
135
136     /* Count non-trivial ACEs */
137     for (i=0; i < sacecount; ) {
138         if ( ! (aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
139             nontrivaces++;
140         i++;
141     }
142     /* malloc buffer for new ACL */
143     if ((new_aces = malloc(nontrivaces * sizeof(ace_t))) == NULL) {
144         LOG(log_error, logtype_afpd, "strip_trivial_aces: malloc %s", strerror(errno));
145         return -1;
146     }
147
148     /* Copy non-trivial ACEs */
149     for (i=0, j=0; i < sacecount; ) {
150         if ( ! (aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE))) {
151             memcpy(&new_aces[j], &aces[i], sizeof(ace_t));
152             j++;
153         }
154         i++;
155     }
156
157     free(aces);
158     *saces = new_aces;
159
160     LOG(log_debug7, logtype_afpd, "strip_trivial_aces: non-trivial ACEs: %d", nontrivaces);
161
162     return nontrivaces;
163 }
164
165 /*
166   Remove non-trivial ACEs "in-place". Returns no of trivial ACEs.
167 */
168 int strip_nontrivial_aces(ace_t **saces, int sacecount)
169 {
170     int i,j;
171     int trivaces = 0;
172     ace_t *aces = *saces;
173     ace_t *new_aces;
174
175     /* Count trivial ACEs */
176     for (i=0; i < sacecount; ) {
177         if ((aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
178             trivaces++;
179         i++;
180     }
181     /* malloc buffer for new ACL */
182     if ((new_aces = malloc(trivaces * sizeof(ace_t))) == NULL) {
183         LOG(log_error, logtype_afpd, "strip_nontrivial_aces: malloc %s", strerror(errno));
184         return -1;
185     }
186
187     /* Copy trivial ACEs */
188     for (i=0, j=0; i < sacecount; ) {
189         if ((aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE))) {
190             memcpy(&new_aces[j], &aces[i], sizeof(ace_t));
191             j++;
192         }
193         i++;
194     }
195     /* Free old ACEs */
196     free(aces);
197     *saces = new_aces;
198
199     LOG(log_debug7, logtype_afpd, "strip_nontrivial_aces: trivial ACEs: %d", trivaces);
200
201     return trivaces;
202 }
203
204 /*!
205  * Change mode of file preserving existing explicit ACEs
206  *
207  * nfsv4_chmod
208  * (1) reads objects ACL (acl1)
209  * (2) removes all trivial ACEs from the ACL by calling strip_trivial_aces(), possibly
210  *     leaving 0 ACEs in the ACL if there were only trivial ACEs as mapped from the mode
211  * (3) calls chmod() with mode
212  * (4) reads the changed ACL (acl2) which
213  *     a) might still contain explicit ACEs (up to onnv132)
214  *     b) will have any explicit ACE removed (starting with onnv145/Openindiana)
215  * (5) strip any explicit ACE from acl2 using strip_nontrivial_aces()
216  * (6) merge acl2 and acl2
217  * (7) set the ACL merged ACL on the object
218  */
219 int nfsv4_chmod(char *name, mode_t mode)
220 {
221     int ret = -1;
222     int noaces, nnaces;
223     ace_t *oacl = NULL, *nacl = NULL, *cacl = NULL;
224
225     LOG(log_debug, logtype_afpd, "nfsv4_chmod(\"%s/%s\", %04o)",
226         getcwdpath(), name, mode);
227
228     if ((noaces = get_nfsv4_acl(name, &oacl)) == -1) /* (1) */
229         goto exit;
230     if ((noaces = strip_trivial_aces(&oacl, noaces)) == -1) /* (2) */
231         goto exit;
232
233     if (chmod(name, mode) != 0) /* (3) */
234         goto exit;
235
236     if ((nnaces = get_nfsv4_acl(name, &nacl)) == -1) /* (4) */
237         goto exit;
238     if ((nnaces = strip_nontrivial_aces(&nacl, nnaces)) == -1) /* (5) */
239         goto exit;
240
241     if ((cacl = concat_aces(oacl, noaces, nacl, nnaces)) == NULL) /* (6) */
242         goto exit;
243
244     if ((ret = acl(name, ACE_SETACL, noaces + nnaces, cacl)) != 0) {
245         LOG(log_error, logtype_afpd, "nfsv4_chmod: error setting acl: %s", strerror(errno));
246         goto exit;
247     }
248
249 exit:
250     if (oacl) free(oacl);
251     if (nacl) free(nacl);
252     if (cacl) free(cacl);
253
254     LOG(log_debug, logtype_afpd, "nfsv4_chmod(\"%s/%s\", %04o): result: %u",
255         getcwdpath(), name, mode, ret);
256
257     return ret;
258 }
259
260 #endif /* HAVE_SOLARIS_ACLS */
261
262 #ifdef HAVE_POSIX_ACLS
263
264 /* This is a workaround for chmod() on filestystems supporting Posix 1003.1e draft 17
265  * compliant ACLs. For objects with extented ACLs, eg objects with an ACL_MASK entry,
266  * chmod() manipulates ACL_MASK instead of ACL_GROUP_OBJ. As OS X isn't aware of
267  * this behavior calling FPSetFileDirParms may lead to unpredictable results. For
268  * more information see section 23.1.2 of Posix 1003.1e draft 17.
269  *
270  * posix_chmod() accepts the same arguments as chmod() and returns 0 in case of
271  * success or -1 in case something went wrong.
272  */
273
274 #define SEARCH_GROUP_OBJ 0x01
275 #define SEARCH_MASK 0x02
276
277 int posix_chmod(const char *name, mode_t mode) {
278     int ret = 0;
279     int entry_id = ACL_FIRST_ENTRY;
280     acl_entry_t entry;
281     acl_entry_t group_entry;
282     acl_tag_t tag;
283     acl_t acl;
284     u_char not_found = (SEARCH_GROUP_OBJ|SEARCH_MASK); /* used as flags */
285
286     LOG(log_maxdebug, logtype_afpd, "posix_chmod: %s mode: 0x%08x", name, mode);
287
288     /* Call chmod() first because there might be some special bits to be set which
289      * aren't related to access control.
290      */
291     ret = chmod(name, mode);
292
293     if (ret)
294         goto done;
295
296     /* Check if the underlying filesystem supports ACLs. */
297     acl = acl_get_file(name, ACL_TYPE_ACCESS);
298
299     if (acl) {
300         /* There is no need to keep iterating once we have found ACL_GROUP_OBJ and ACL_MASK. */
301         while ((acl_get_entry(acl, entry_id, &entry) == 1) && not_found) {
302             entry_id = ACL_NEXT_ENTRY;
303
304             ret = acl_get_tag_type(entry, &tag);
305
306             if (ret) {
307                 LOG(log_error, logtype_afpd, "posix_chmod: Failed to get tag type.");
308                 goto cleanup;
309             }
310
311             switch (tag) {
312                 case ACL_GROUP_OBJ:
313                     group_entry = entry;
314                     not_found &= ~SEARCH_GROUP_OBJ;
315                     break;
316
317                 case ACL_MASK:
318                     not_found &= ~SEARCH_MASK;
319                     break;
320
321                 default:
322                     break;
323             }
324         }
325         if (!not_found) {
326             /* The filesystem object has extented ACLs. We have to update ACL_GROUP_OBJ
327              * with the group permissions.
328              */
329             acl_permset_t permset;
330             acl_perm_t perm = 0;
331
332             ret = acl_get_permset(group_entry, &permset);
333
334             if (ret) {
335                 LOG(log_error, logtype_afpd, "posix_chmod: Can't get permset.");
336                 goto cleanup;
337             }
338             ret = acl_clear_perms(permset);
339
340             if (ret)
341                 goto cleanup;
342
343             if (mode & S_IXGRP)
344                 perm |= ACL_EXECUTE;
345
346             if (mode & S_IWGRP)
347                 perm |= ACL_WRITE;
348
349             if (mode & S_IRGRP)
350                 perm |= ACL_READ;
351
352             ret = acl_add_perm(permset, perm);
353
354             if (ret)
355                 goto cleanup;
356
357             ret = acl_set_permset(group_entry, permset);
358
359             if (ret) {
360                 LOG(log_error, logtype_afpd, "posix_chmod: Can't set permset.");
361                 goto cleanup;
362             }
363             /* also update ACL_MASK */
364             ret = acl_calc_mask(&acl);
365
366             if (ret) {
367                 LOG(log_error, logtype_afpd, "posix_chmod: acl_calc_mask failed.");
368                 goto cleanup;
369             }
370             ret = acl_set_file(name, ACL_TYPE_ACCESS, acl);
371         }
372 cleanup:
373         acl_free(acl);
374     }
375 done:
376     LOG(log_maxdebug, logtype_afpd, "posix_chmod: %d", ret);
377     return ret;
378 }
379
380 /*
381  * posix_fchmod() accepts the same arguments as fchmod() and returns 0 in case of
382  * success or -1 in case something went wrong.
383  */
384 int posix_fchmod(int fd, mode_t mode) {
385     int ret = 0;
386     int entry_id = ACL_FIRST_ENTRY;
387     acl_entry_t entry;
388     acl_entry_t group_entry;
389     acl_tag_t tag;
390     acl_t acl;
391     u_char not_found = (SEARCH_GROUP_OBJ|SEARCH_MASK); /* used as flags */
392
393     /* Call chmod() first because there might be some special bits to be set which
394      * aren't related to access control.
395      */
396     ret = fchmod(fd, mode);
397
398     if (ret)
399         goto done;
400
401     /* Check if the underlying filesystem supports ACLs. */
402     acl = acl_get_fd(fd);
403
404     if (acl) {
405         /* There is no need to keep iterating once we have found ACL_GROUP_OBJ and ACL_MASK. */
406         while ((acl_get_entry(acl, entry_id, &entry) == 1) && not_found) {
407             entry_id = ACL_NEXT_ENTRY;
408
409             ret = acl_get_tag_type(entry, &tag);
410
411             if (ret) {
412                 LOG(log_error, logtype_afpd, "posix_fchmod: Failed to get tag type.");
413                 goto cleanup;
414             }
415
416             switch (tag) {
417                 case ACL_GROUP_OBJ:
418                     group_entry = entry;
419                     not_found &= ~SEARCH_GROUP_OBJ;
420                     break;
421
422                 case ACL_MASK:
423                     not_found &= ~SEARCH_MASK;
424                     break;
425
426                 default:
427                     break;
428             }
429         }
430         if (!not_found) {
431             /* The filesystem object has extented ACLs. We have to update ACL_GROUP_OBJ
432              * with the group permissions.
433              */
434             acl_permset_t permset;
435             acl_perm_t perm = 0;
436
437             ret = acl_get_permset(group_entry, &permset);
438
439             if (ret) {
440                 LOG(log_error, logtype_afpd, "posix_fchmod: Can't get permset.");
441                 goto cleanup;
442             }
443             ret = acl_clear_perms(permset);
444
445             if (ret)
446                 goto cleanup;
447
448             if (mode & S_IXGRP)
449                 perm |= ACL_EXECUTE;
450
451             if (mode & S_IWGRP)
452                 perm |= ACL_WRITE;
453
454             if (mode & S_IRGRP)
455                 perm |= ACL_READ;
456
457             ret = acl_add_perm(permset, perm);
458
459             if (ret)
460                 goto cleanup;
461
462             ret = acl_set_permset(group_entry, permset);
463
464             if (ret) {
465                 LOG(log_error, logtype_afpd, "posix_fchmod: Can't set permset.");
466                 goto cleanup;
467             }
468             /* also update ACL_MASK */
469             ret = acl_calc_mask(&acl);
470
471             if (ret) {
472                 LOG(log_error, logtype_afpd, "posix_fchmod: acl_calc_mask failed.");
473                 goto cleanup;
474             }
475             ret = acl_set_fd(fd, acl);
476         }
477 cleanup:
478         acl_free(acl);
479     }
480 done:
481     return ret;
482 }
483
484 #endif /* HAVE_POSIX_ACLS */
485
486 #endif /* HAVE_ACLS */