]> arthur.barton.de Git - netatalk.git/blob - libatalk/vfs/acl.c
Fix NFSv4 ACL VFS integration
[netatalk.git] / libatalk / vfs / acl.c
1 /*
2   $Id: acl.c,v 1.2 2009-11-26 18:17:12 franklahm Exp $
3   Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
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 #include <unistd.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <sys/acl.h>
25
26 #include <atalk/afp.h>
27 #include <atalk/util.h>
28 #include <atalk/logger.h>
29
30 /* Get ACL. Allocates storage as needed. Caller must free.
31  * Returns no of ACEs or -1 on error.  */
32 int get_nfsv4_acl(const char *name, ace_t **retAces)
33 {
34     int ace_count = -1;
35     ace_t *aces;
36
37     *retAces = NULL;
38     ace_count = acl(name, ACE_GETACLCNT, 0, NULL);
39     if (ace_count <= 0) {
40         LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl(ACE_GETACLCNT) error");
41         return -1;
42     }
43
44     aces = malloc(ace_count * sizeof(ace_t));
45     if (aces == NULL) {
46         LOG(log_error, logtype_afpd, "get_nfsv4_acl: malloc error");
47         return -1;
48     }
49
50     if ( (acl(name, ACE_GETACL, ace_count, aces)) == -1 ) {
51         LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl(ACE_GETACL) error");
52         free(aces);
53         return -1;
54     }
55
56     LOG(log_debug9, logtype_afpd, "get_nfsv4_acl: file: %s -> No. of ACEs: %d", name, ace_count);
57     *retAces = aces;
58
59     return ace_count;
60 }
61
62 /* Removes all non-trivial ACLs from object. Returns full AFPERR code. */
63 int remove_acl(const char *name)
64 {
65     int ret,i, ace_count, trivial_aces, new_aces_count;
66     ace_t *old_aces = NULL;
67     ace_t *new_aces = NULL;
68
69     LOG(log_debug9, logtype_afpd, "remove_acl: BEGIN");
70
71     /* Get existing ACL and count trivial ACEs */
72     if ((ace_count = get_nfsv4_acl(name, &old_aces)) == -1)
73         return AFPERR_MISC;
74     trivial_aces = 0;
75     for ( i=0; i < ace_count; i++) {
76         if (old_aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE))
77             trivial_aces++;
78     }
79
80     /* malloc buffer for new ACL */
81     if ((new_aces = malloc(trivial_aces * sizeof(ace_t))) == NULL) {
82         LOG(log_error, logtype_afpd, "remove_acl: malloc %s", strerror(errno));
83         ret = AFPERR_MISC;
84         goto exit;
85     }
86
87     /* Now copy the trivial ACEs */
88     new_aces_count = 0;
89     for (i=0; i < ace_count; i++) {
90         if (old_aces[i].a_flags  & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
91             memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
92             new_aces_count++;
93         }
94     }
95
96     if ( (acl(name, ACE_SETACL, trivial_aces, new_aces)) == 0)
97         ret = AFP_OK;
98     else {
99         LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
100         if (errno == (EACCES | EPERM))
101             ret = AFPERR_ACCESS;
102         else if (errno == ENOENT)
103             ret = AFPERR_NOITEM;
104         else
105             ret = AFPERR_MISC;
106     }
107
108 exit:
109     free(old_aces);
110     free(new_aces);
111
112     LOG(log_debug9, logtype_afpd, "remove_acl: END");
113     return ret;
114 }