]> arthur.barton.de Git - netatalk.git/blob - libatalk/acl/unix.c
Handle symlinks
[netatalk.git] / libatalk / acl / unix.c
1 /*
2   Copyright (c) 2010 Frank Lahm <franklahm@gmail.com>
3
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.
8
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.
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif /* HAVE_CONFIG_H */
18
19 #ifdef HAVE_SOLARIS_ACLS
20
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/acl.h>
30
31 #include <atalk/logger.h>
32 #include <atalk/afp.h>
33 #include <atalk/util.h>
34 #include <atalk/acl.h>
35
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)
39 {
40     int ace_count = -1;
41     ace_t *aces;
42     struct stat st;
43
44     *retAces = NULL;
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));
48         return -1;
49     }
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);
52         return 0;
53     }
54
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);
57         return 0;
58     }
59
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));
63         return -1;
64     }
65
66     aces = malloc(ace_count * sizeof(ace_t));
67     if (aces == NULL) {
68         LOG(log_error, logtype_afpd, "get_nfsv4_acl: malloc error");
69         return -1;
70     }
71
72     if ( (acl(name, ACE_GETACL, ace_count, aces)) == -1 ) {
73         LOG(log_error, logtype_afpd, "get_nfsv4_acl: acl(ACE_GETACL) error");
74         free(aces);
75         return -1;
76     }
77
78     LOG(log_debug9, logtype_afpd, "get_nfsv4_acl: file: %s -> No. of ACEs: %d", name, ace_count);
79     *retAces = aces;
80
81     return ace_count;
82 }
83
84 /*
85   Concatenate ACEs
86 */
87 ace_t *concat_aces(ace_t *aces1, int ace1count, ace_t *aces2, int ace2count)
88 {
89     ace_t *new_aces;
90     int i, j;
91
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));
95         return NULL;
96     }
97
98     /* Copy ACEs from buf1 */
99     for (i=0; i < ace1count; ) {
100         memcpy(&new_aces[i], &aces1[i], sizeof(ace_t));
101         i++;
102     }
103
104     j = i;
105
106     /* Copy ACEs from buf2 */
107     for (i=0; i < ace2count; ) {
108         memcpy(&new_aces[j], &aces2[i], sizeof(ace_t));
109         i++;
110         j++;
111     }
112     return new_aces;
113 }
114
115 /*
116   Remove any trivial ACE "in-place". Returns no of non-trivial ACEs
117 */
118 int strip_trivial_aces(ace_t **saces, int sacecount)
119 {
120     int i,j;
121     int nontrivaces = 0;
122     ace_t *aces = *saces;
123     ace_t *new_aces;
124
125     if (aces == NULL || sacecount <= 0)
126         return 0;
127
128     /* Count non-trivial ACEs */
129     for (i=0; i < sacecount; ) {
130         if ( ! (aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
131             nontrivaces++;
132         i++;
133     }
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));
137         return -1;
138     }
139
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));
144             j++;
145         }
146         i++;
147     }
148
149     free(aces);
150     *saces = new_aces;
151
152     LOG(log_debug7, logtype_afpd, "strip_trivial_aces: non-trivial ACEs: %d", nontrivaces);
153
154     return nontrivaces;
155 }
156
157 /*
158   Remove non-trivial ACEs "in-place". Returns no of trivial ACEs.
159 */
160 int strip_nontrivial_aces(ace_t **saces, int sacecount)
161 {
162     int i,j;
163     int trivaces = 0;
164     ace_t *aces = *saces;
165     ace_t *new_aces;
166
167     /* Count trivial ACEs */
168     for (i=0; i < sacecount; ) {
169         if ((aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
170             trivaces++;
171         i++;
172     }
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));
176         return -1;
177     }
178
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));
183             j++;
184         }
185         i++;
186     }
187     /* Free old ACEs */
188     free(aces);
189     *saces = new_aces;
190
191     LOG(log_debug7, logtype_afpd, "strip_nontrivial_aces: trivial ACEs: %d", trivaces);
192
193     return trivaces;
194 }
195
196 /*!
197  * Change mode of file preserving existing explicit ACEs
198  *
199  * nfsv4_chmod
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
210  */
211 int nfsv4_chmod(char *name, mode_t mode)
212 {
213     int ret = -1;
214     int noaces, nnaces;
215     ace_t *oacl = NULL, *nacl = NULL, *cacl;
216
217     LOG(log_debug, logtype_afpd, "nfsv4_chmod(\"%s/%s\", %04o)",
218         getcwdpath(), name, mode);
219
220     if ((noaces = get_nfsv4_acl(name, &oacl)) == -1) /* (1) */
221         goto exit;
222     if ((noaces = strip_trivial_aces(&oacl, noaces)) == -1) /* (2) */
223         goto exit;
224
225 #ifdef chmod
226 #undef chmod
227 #endif
228     if (chmod(name, mode) != 0) /* (3) */
229         goto exit;
230
231     if ((nnaces = get_nfsv4_acl(name, &nacl)) == -1) /* (4) */
232         goto exit;
233     if ((nnaces = strip_nontrivial_aces(&nacl, nnaces)) == -1) /* (5) */
234         goto exit;
235
236     if ((cacl = concat_aces(oacl, noaces, nacl, nnaces)) == NULL) /* (6) */
237         goto exit;
238
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));
241         goto exit;
242     }
243
244 exit:
245     if (oacl) free(oacl);
246     if (nacl) free(nacl);
247     if (cacl) free(cacl);
248
249     LOG(log_debug, logtype_afpd, "nfsv4_chmod(\"%s/%s\", %04o): result: %u",
250         getcwdpath(), name, mode, ret);
251
252     return ret;
253 }
254
255 #endif /* HAVE_SOLARIS_ACLS */