2 Copyright (c) 2010 Frank Lahm <franklahm@gmail.com>
3 Copyright (c) 2011 Laura Mueller <laura-mueller@uni-duesseldorf.de>
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.
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.
18 #endif /* HAVE_CONFIG_H */
23 #include <sys/types.h>
32 #include <atalk/logger.h>
33 #include <atalk/afp.h>
34 #include <atalk/util.h>
35 #include <atalk/acl.h>
37 #ifdef HAVE_SOLARIS_ACLS
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)
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));
54 if (S_ISLNK(st.st_mode))
55 /* sorry, no ACLs for symlinks */
58 if ( ! (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))) {
59 LOG(log_debug, logtype_afpd, "get_nfsv4_acl(\"%s/%s\"): special", getcwdpath(), name);
63 if ((ace_count = acl(name, ACE_GETACLCNT, 0, NULL)) == 0) {
64 LOG(log_debug, logtype_afpd, "get_nfsv4_acl(\"%s/%s\"): 0 ACEs", getcwdpath(), name);
68 if (ace_count == -1) {
69 LOG(log_debug, logtype_afpd, "get_nfsv4_acl: acl('%s/%s', ACE_GETACLCNT): ace_count %i, error: %s",
70 getcwdpath(), name, ace_count, strerror(errno));
74 aces = malloc(ace_count * sizeof(ace_t));
76 LOG(log_error, logtype_afpd, "get_nfsv4_acl: malloc error");
80 if ( (acl(name, ACE_GETACL, ace_count, aces)) == -1 ) {
81 LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl(ACE_GETACL) error");
86 LOG(log_debug9, logtype_afpd, "get_nfsv4_acl: file: %s -> No. of ACEs: %d", name, ace_count);
95 ace_t *concat_aces(ace_t *aces1, int ace1count, ace_t *aces2, int ace2count)
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));
106 /* Copy ACEs from buf1 */
107 for (i=0; i < ace1count; ) {
108 memcpy(&new_aces[i], &aces1[i], sizeof(ace_t));
114 /* Copy ACEs from buf2 */
115 for (i=0; i < ace2count; ) {
116 memcpy(&new_aces[j], &aces2[i], sizeof(ace_t));
124 Remove any trivial ACE "in-place". Returns no of non-trivial ACEs
126 int strip_trivial_aces(ace_t **saces, int sacecount)
130 ace_t *aces = *saces;
133 if (aces == NULL || sacecount <= 0)
136 /* Count non-trivial ACEs */
137 for (i=0; i < sacecount; ) {
138 if ( ! (aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
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));
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));
160 LOG(log_debug7, logtype_afpd, "strip_trivial_aces: non-trivial ACEs: %d", nontrivaces);
166 Remove non-trivial ACEs "in-place". Returns no of trivial ACEs.
168 int strip_nontrivial_aces(ace_t **saces, int sacecount)
172 ace_t *aces = *saces;
175 /* Count trivial ACEs */
176 for (i=0; i < sacecount; ) {
177 if ((aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
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));
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));
199 LOG(log_debug7, logtype_afpd, "strip_nontrivial_aces: trivial ACEs: %d", trivaces);
205 * Change mode of file preserving existing explicit ACEs
208 * (1) reads objects ACL (acl1), may return 0 or -1 NFSv4 ACEs on eg UFS fs
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, we're done if step (1) returned 0 for noaces
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
219 int nfsv4_chmod(char *name, mode_t mode)
223 ace_t *oacl = NULL, *nacl = NULL, *cacl = NULL;
225 LOG(log_debug, logtype_afpd, "nfsv4_chmod(\"%s/%s\", %04o)",
226 getcwdpath(), name, mode);
228 if ((noaces = get_nfsv4_acl(name, &oacl)) < 1) /* (1) */
229 return chmod(name, mode);
231 if ((noaces = strip_trivial_aces(&oacl, noaces)) == -1) /* (2) */
234 if (chmod(name, mode) != 0) /* (3) */
237 if ((nnaces = get_nfsv4_acl(name, &nacl)) == -1) /* (4) */
239 if ((nnaces = strip_nontrivial_aces(&nacl, nnaces)) == -1) /* (5) */
242 if ((cacl = concat_aces(oacl, noaces, nacl, nnaces)) == NULL) /* (6) */
245 if ((ret = acl(name, ACE_SETACL, noaces + nnaces, cacl)) != 0) {
246 LOG(log_error, logtype_afpd, "nfsv4_chmod: error setting acl: %s", strerror(errno));
251 if (oacl) free(oacl);
252 if (nacl) free(nacl);
253 if (cacl) free(cacl);
255 LOG(log_debug, logtype_afpd, "nfsv4_chmod(\"%s/%s\", %04o): result: %u",
256 getcwdpath(), name, mode, ret);
261 #endif /* HAVE_SOLARIS_ACLS */
263 #ifdef HAVE_POSIX_ACLS
265 /* This is a workaround for chmod() on filestystems supporting Posix 1003.1e draft 17
266 * compliant ACLs. For objects with extented ACLs, eg objects with an ACL_MASK entry,
267 * chmod() manipulates ACL_MASK instead of ACL_GROUP_OBJ. As OS X isn't aware of
268 * this behavior calling FPSetFileDirParms may lead to unpredictable results. For
269 * more information see section 23.1.2 of Posix 1003.1e draft 17.
271 * posix_chmod() accepts the same arguments as chmod() and returns 0 in case of
272 * success or -1 in case something went wrong.
275 #define SEARCH_GROUP_OBJ 0x01
276 #define SEARCH_MASK 0x02
278 int posix_chmod(const char *name, mode_t mode) {
280 int entry_id = ACL_FIRST_ENTRY;
282 acl_entry_t group_entry;
285 u_char not_found = (SEARCH_GROUP_OBJ|SEARCH_MASK); /* used as flags */
287 LOG(log_maxdebug, logtype_afpd, "posix_chmod: %s mode: 0x%08x", name, mode);
289 /* Call chmod() first because there might be some special bits to be set which
290 * aren't related to access control.
292 ret = chmod(name, mode);
297 /* Check if the underlying filesystem supports ACLs. */
298 acl = acl_get_file(name, ACL_TYPE_ACCESS);
301 /* There is no need to keep iterating once we have found ACL_GROUP_OBJ and ACL_MASK. */
302 while ((acl_get_entry(acl, entry_id, &entry) == 1) && not_found) {
303 entry_id = ACL_NEXT_ENTRY;
305 ret = acl_get_tag_type(entry, &tag);
308 LOG(log_error, logtype_afpd, "posix_chmod: Failed to get tag type.");
315 not_found &= ~SEARCH_GROUP_OBJ;
319 not_found &= ~SEARCH_MASK;
327 /* The filesystem object has extented ACLs. We have to update ACL_GROUP_OBJ
328 * with the group permissions.
330 acl_permset_t permset;
333 ret = acl_get_permset(group_entry, &permset);
336 LOG(log_error, logtype_afpd, "posix_chmod: Can't get permset.");
339 ret = acl_clear_perms(permset);
353 ret = acl_add_perm(permset, perm);
358 ret = acl_set_permset(group_entry, permset);
361 LOG(log_error, logtype_afpd, "posix_chmod: Can't set permset.");
364 /* also update ACL_MASK */
365 ret = acl_calc_mask(&acl);
368 LOG(log_error, logtype_afpd, "posix_chmod: acl_calc_mask failed.");
371 ret = acl_set_file(name, ACL_TYPE_ACCESS, acl);
377 LOG(log_maxdebug, logtype_afpd, "posix_chmod: %d", ret);
382 * posix_fchmod() accepts the same arguments as fchmod() and returns 0 in case of
383 * success or -1 in case something went wrong.
385 int posix_fchmod(int fd, mode_t mode) {
387 int entry_id = ACL_FIRST_ENTRY;
389 acl_entry_t group_entry;
392 u_char not_found = (SEARCH_GROUP_OBJ|SEARCH_MASK); /* used as flags */
394 /* Call chmod() first because there might be some special bits to be set which
395 * aren't related to access control.
397 ret = fchmod(fd, mode);
402 /* Check if the underlying filesystem supports ACLs. */
403 acl = acl_get_fd(fd);
406 /* There is no need to keep iterating once we have found ACL_GROUP_OBJ and ACL_MASK. */
407 while ((acl_get_entry(acl, entry_id, &entry) == 1) && not_found) {
408 entry_id = ACL_NEXT_ENTRY;
410 ret = acl_get_tag_type(entry, &tag);
413 LOG(log_error, logtype_afpd, "posix_fchmod: Failed to get tag type.");
420 not_found &= ~SEARCH_GROUP_OBJ;
424 not_found &= ~SEARCH_MASK;
432 /* The filesystem object has extented ACLs. We have to update ACL_GROUP_OBJ
433 * with the group permissions.
435 acl_permset_t permset;
438 ret = acl_get_permset(group_entry, &permset);
441 LOG(log_error, logtype_afpd, "posix_fchmod: Can't get permset.");
444 ret = acl_clear_perms(permset);
458 ret = acl_add_perm(permset, perm);
463 ret = acl_set_permset(group_entry, permset);
466 LOG(log_error, logtype_afpd, "posix_fchmod: Can't set permset.");
469 /* also update ACL_MASK */
470 ret = acl_calc_mask(&acl);
473 LOG(log_error, logtype_afpd, "posix_fchmod: acl_calc_mask failed.");
476 ret = acl_set_fd(fd, acl);
485 #endif /* HAVE_POSIX_ACLS */
487 #endif /* HAVE_ACLS */