]> 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 <sys/types.h>
22 #include <sys/stat.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #ifdef HAVE_SOLARIS_ACLS
28 #include <sys/acl.h>
29 #endif
30
31 #include <atalk/afp.h>
32 #include <atalk/util.h>
33 #include <atalk/logger.h>
34
35 #ifdef HAVE_SOLARIS_ACLS
36
37 /* Get ACL. Allocates storage as needed. Caller must free.
38  * Returns no of ACEs or -1 on error.  */
39 int get_nfsv4_acl(const char *name, ace_t **retAces)
40 {
41     int ace_count = -1;
42     ace_t *aces;
43     struct stat st;
44
45     *retAces = NULL;
46     /* Only call acl() for regular files and directories, otherwise just return 0 */
47     if (lstat(name, &st) != 0)
48         return -1;
49     if ( ! (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)))
50         return 0;
51     if ((ace_count = acl(name, ACE_GETACLCNT, 0, NULL)) == 0)
52         return 0;
53
54     if (ace_count == -1) {
55         LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl('%s/%s', ACE_GETACLCNT): ace_count %i, error: %s",
56             getcwdpath(), name, ace_count, strerror(errno));
57         return -1;
58     }
59
60     aces = malloc(ace_count * sizeof(ace_t));
61     if (aces == NULL) {
62         LOG(log_error, logtype_afpd, "get_nfsv4_acl: malloc error");
63         return -1;
64     }
65
66     if ( (acl(name, ACE_GETACL, ace_count, aces)) == -1 ) {
67         LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl(ACE_GETACL) error");
68         free(aces);
69         return -1;
70     }
71
72     LOG(log_debug9, logtype_afpd, "get_nfsv4_acl: file: %s -> No. of ACEs: %d", name, ace_count);
73     *retAces = aces;
74
75     return ace_count;
76 }
77
78 /* Removes all non-trivial ACLs from object. Returns full AFPERR code. */
79 int remove_acl_vfs(const char *name)
80 {
81     int ret,i, ace_count, trivial_aces, new_aces_count;
82     ace_t *old_aces = NULL;
83     ace_t *new_aces = NULL;
84
85     LOG(log_debug9, logtype_afpd, "remove_acl: BEGIN");
86
87     /* Get existing ACL and count trivial ACEs */
88     if ((ace_count = get_nfsv4_acl(name, &old_aces)) == -1)
89         return AFPERR_MISC;
90     trivial_aces = 0;
91     for ( i=0; i < ace_count; i++) {
92         if (old_aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE))
93             trivial_aces++;
94     }
95
96     /* malloc buffer for new ACL */
97     if ((new_aces = malloc(trivial_aces * sizeof(ace_t))) == NULL) {
98         LOG(log_error, logtype_afpd, "remove_acl: malloc %s", strerror(errno));
99         ret = AFPERR_MISC;
100         goto exit;
101     }
102
103     /* Now copy the trivial ACEs */
104     new_aces_count = 0;
105     for (i=0; i < ace_count; i++) {
106         if (old_aces[i].a_flags  & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)) {
107             memcpy(&new_aces[new_aces_count], &old_aces[i], sizeof(ace_t));
108             new_aces_count++;
109         }
110     }
111
112     if ( (acl(name, ACE_SETACL, trivial_aces, new_aces)) == 0)
113         ret = AFP_OK;
114     else {
115         LOG(log_error, logtype_afpd, "set_acl: error setting acl: %s", strerror(errno));
116         if (errno == (EACCES | EPERM))
117             ret = AFPERR_ACCESS;
118         else if (errno == ENOENT)
119             ret = AFPERR_NOITEM;
120         else
121             ret = AFPERR_MISC;
122     }
123
124 exit:
125     free(old_aces);
126     free(new_aces);
127
128     LOG(log_debug9, logtype_afpd, "remove_acl: END");
129     return ret;
130 }
131
132 #endif  /* HAVE_SOLARIS_ACLS */
133
134 #ifdef HAVE_POSIX_ACLS
135 int remove_acl_vfs(const char *name)
136 {
137     return AFP_OK;
138 }
139 #endif /* HAVE_POSIX_ACLS */