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