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_SOLARIS_ACLS
22 #include <sys/types.h>
31 #include <atalk/logger.h>
32 #include <atalk/afp.h>
33 #include <atalk/util.h>
34 #include <atalk/acl.h>
36 /* Get ACL. Allocates storage as needed. Caller must free.
37 * Returns no of ACEs or -1 on error. */
38 int get_nfsv4_acl(const char *name, ace_t **retAces)
45 /* Only call acl() for regular files and directories, otherwise just return 0 */
46 if (lstat(name, &st) != 0) {
47 LOG(log_warning, logtype_afpd, "get_nfsv4_acl(\"%s/%s\"): %s", getcwdpath(), name, strerror(errno));
50 if ( ! (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))) {
51 LOG(log_warning, logtype_afpd, "get_nfsv4_acl(\"%s/%s\"): special", getcwdpath(), name);
55 if ((ace_count = acl(name, ACE_GETACLCNT, 0, NULL)) == 0) {
56 LOG(log_warning, logtype_afpd, "get_nfsv4_acl(\"%s/%s\"): 0 ACEs", getcwdpath(), name);
60 if (ace_count == -1) {
61 LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl('%s/%s', ACE_GETACLCNT): ace_count %i, error: %s",
62 getcwdpath(), name, ace_count, strerror(errno));
66 aces = malloc(ace_count * sizeof(ace_t));
68 LOG(log_error, logtype_afpd, "get_nfsv4_acl: malloc error");
72 if ( (acl(name, ACE_GETACL, ace_count, aces)) == -1 ) {
73 LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl(ACE_GETACL) error");
78 LOG(log_debug9, logtype_afpd, "get_nfsv4_acl: file: %s -> No. of ACEs: %d", name, ace_count);
87 ace_t *concat_aces(ace_t *aces1, int ace1count, ace_t *aces2, int ace2count)
92 /* malloc buffer for new ACL */
93 if ((new_aces = malloc((ace1count + ace2count) * sizeof(ace_t))) == NULL) {
94 LOG(log_error, logtype_afpd, "combine_aces: malloc %s", strerror(errno));
98 /* Copy ACEs from buf1 */
99 for (i=0; i < ace1count; ) {
100 memcpy(&new_aces[i], &aces1[i], sizeof(ace_t));
106 /* Copy ACEs from buf2 */
107 for (i=0; i < ace2count; ) {
108 memcpy(&new_aces[j], &aces2[i], sizeof(ace_t));
116 Remove any trivial ACE "in-place". Returns no of non-trivial ACEs
118 int strip_trivial_aces(ace_t **saces, int sacecount)
122 ace_t *aces = *saces;
125 if (aces == NULL || sacecount <= 0)
128 /* Count non-trivial ACEs */
129 for (i=0; i < sacecount; ) {
130 if ( ! (aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
134 /* malloc buffer for new ACL */
135 if ((new_aces = malloc(nontrivaces * sizeof(ace_t))) == NULL) {
136 LOG(log_error, logtype_afpd, "strip_trivial_aces: malloc %s", strerror(errno));
140 /* Copy non-trivial ACEs */
141 for (i=0, j=0; i < sacecount; ) {
142 if ( ! (aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE))) {
143 memcpy(&new_aces[j], &aces[i], sizeof(ace_t));
152 LOG(log_debug7, logtype_afpd, "strip_trivial_aces: non-trivial ACEs: %d", nontrivaces);
158 Remove non-trivial ACEs "in-place". Returns no of trivial ACEs.
160 int strip_nontrivial_aces(ace_t **saces, int sacecount)
164 ace_t *aces = *saces;
167 /* Count trivial ACEs */
168 for (i=0; i < sacecount; ) {
169 if ((aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
173 /* malloc buffer for new ACL */
174 if ((new_aces = malloc(trivaces * sizeof(ace_t))) == NULL) {
175 LOG(log_error, logtype_afpd, "strip_nontrivial_aces: malloc %s", strerror(errno));
179 /* Copy trivial ACEs */
180 for (i=0, j=0; i < sacecount; ) {
181 if ((aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE))) {
182 memcpy(&new_aces[j], &aces[i], sizeof(ace_t));
191 LOG(log_debug7, logtype_afpd, "strip_nontrivial_aces: trivial ACEs: %d", trivaces);
197 * Change mode of file preserving existing explicit ACEs
200 * (1) reads objects ACL (acl1)
201 * (2) removes all trivial ACEs from the ACL by calling strip_trivial_aces(), possibly
202 * leaving 0 ACEs in the ACL if there were only trivial ACEs as mapped from the mode
203 * (3) calls chmod() with mode
204 * (4) reads the changed ACL (acl2) which
205 * a) might still contain explicit ACEs (up to onnv132)
206 * b) will have any explicit ACE removed (starting with onnv145/Openindiana)
207 * (5) strip any explicit ACE from acl2 using strip_nontrivial_aces()
208 * (6) merge acl2 and acl2
209 * (7) set the ACL merged ACL on the object
211 int nfsv4_chmod(char *name, mode_t mode)
215 ace_t *oacl = NULL, *nacl = NULL, *cacl;
217 LOG(log_debug, logtype_afpd, "nfsv4_chmod(\"%s/%s\", %04o)",
218 getcwdpath(), name, mode);
220 if ((noaces = get_nfsv4_acl(name, &oacl)) == -1) /* (1) */
222 if ((noaces = strip_trivial_aces(&oacl, noaces)) == -1) /* (2) */
228 if (chmod(name, mode) != 0) /* (3) */
231 if ((nnaces = get_nfsv4_acl(name, &nacl)) == -1) /* (4) */
233 if ((nnaces = strip_nontrivial_aces(&nacl, nnaces)) == -1) /* (5) */
236 if ((cacl = concat_aces(oacl, noaces, nacl, nnaces)) == NULL) /* (6) */
239 if ((ret = acl(name, ACE_SETACL, noaces + nnaces, cacl)) != 0) {
240 LOG(log_error, logtype_afpd, "nfsv4_chmod: error setting acl: %s", strerror(errno));
245 if (oacl) free(oacl);
246 if (nacl) free(nacl);
247 if (cacl) free(cacl);
249 LOG(log_debug, logtype_afpd, "nfsv4_chmod(\"%s/%s\", %04o): result: %u",
250 getcwdpath(), name, mode, ret);
255 #endif /* HAVE_SOLARIS_ACLS */