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