2 Copyright (c) 2010 Frank Lahm <franklahm@gmail.com>
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.
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.
17 #endif /* HAVE_CONFIG_H */
19 #ifdef HAVE_NFSv4_ACLS
22 #include <sys/types.h>
31 #include <atalk/logger.h>
32 #include <atalk/afp.h>
33 #include <atalk/acl.h>
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)
44 /* Only call acl() for regular files and directories, otherwise just return 0 */
45 if (lstat(name, &st) != 0)
47 if ( ! (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)))
50 if ((ace_count = acl(name, ACE_GETACLCNT, 0, NULL)) == 0)
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));
59 aces = malloc(ace_count * sizeof(ace_t));
61 LOG(log_error, logtype_afpd, "get_nfsv4_acl: malloc error");
65 if ( (acl(name, ACE_GETACL, ace_count, aces)) == -1 ) {
66 LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl(ACE_GETACL) error");
71 LOG(log_debug9, logtype_afpd, "get_nfsv4_acl: file: %s -> No. of ACEs: %d", name, ace_count);
78 Remove any trivial ACE "in-place". Returns no of non-trivial ACEs
80 int strip_trivial_aces(ace_t **saces, int sacecount)
87 /* Count non-trivial ACEs */
88 for (i=0; i < sacecount; ) {
89 if ( ! (aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
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));
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));
111 LOG(log_debug7, logtype_afpd, "strip_trivial_aces: non-trivial ACEs: %d", nontrivaces);
119 ace_t *concat_aces(ace_t *aces1, int ace1count, ace_t *aces2, int ace2count)
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));
130 /* Copy ACEs from buf1 */
131 for (i=0; i < ace1count; ) {
132 memcpy(&new_aces[i], &aces1[i], sizeof(ace_t));
138 /* Copy ACEs from buf2 */
139 for (i=0; i < ace2count; ) {
140 memcpy(&new_aces[j], &aces2[i], sizeof(ace_t));
148 Remove non-trivial ACEs "in-place". Returns no of trivial ACEs.
150 int strip_nontrivial_aces(ace_t **saces, int sacecount)
154 ace_t *aces = *saces;
157 /* Count trivial ACEs */
158 for (i=0; i < sacecount; ) {
159 if ((aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
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));
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));
181 LOG(log_debug7, logtype_afpd, "strip_nontrivial_aces: trivial ACEs: %d", trivaces);
187 * Change mode of file preserving existing explicit ACEs
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
201 int nfsv4_chmod(char *name, mode_t mode)
207 static int set_acl_vfs(const struct vol *vol, char *name, int inherit, char *ibuf)
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;
214 LOG(log_debug9, logtype_afpd, "set_acl: BEGIN");
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) */
221 /* inherited + trivial ACEs */
222 flags = ACE_INHERITED_ACE | ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
224 /* only trivial ACEs */
225 flags = ACE_OWNER | ACE_GROUP | ACE_EVERYONE;
227 /* Get existing ACL and count ACEs which have to be copied */
228 if ((nfsv4_ace_count = get_nfsv4_acl(name, &old_aces)) == -1)
230 for ( i=0; i < nfsv4_ace_count; i++) {
231 if (old_aces[i].a_flags & flags)
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));
243 /* Start building new ACL */
245 /* Copy local inherited ACEs. Therefore we have 'Darwin canonical order' (see chmod there):
246 inherited ACEs first. */
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));
255 LOG(log_debug7, logtype_afpd, "set_acl: copied %d inherited ACEs", new_aces_count);
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);
263 new_aces_count += ace_count;
264 LOG(log_debug7, logtype_afpd, "set_acl: mapped %d ACEs from client", ace_count);
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));
274 LOG(log_debug7, logtype_afpd, "set_acl: copied %d trivial ACEs", trivial_ace_count);
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))
283 else if (errno == ENOENT)
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))
293 else if (errno == ENOENT)
306 LOG(log_debug9, logtype_afpd, "set_acl: END");
311 #endif /* HAVE_NFSv4_ACLS */