]> arthur.barton.de Git - netatalk.git/blob - libatalk/acl/unix.c
Enhanced machine type
[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_NFSv4_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         return -1;
48     if ( ! (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)))
49         return 0;
50
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 /*
79   Concatenate ACEs
80 */
81 ace_t *concat_aces(ace_t *aces1, int ace1count, ace_t *aces2, int ace2count)
82 {
83     ace_t *new_aces;
84     int i, j;
85
86     /* malloc buffer for new ACL */
87     if ((new_aces = malloc((ace1count + ace2count) * sizeof(ace_t))) == NULL) {
88         LOG(log_error, logtype_afpd, "combine_aces: malloc %s", strerror(errno));
89         return NULL;
90     }
91
92     /* Copy ACEs from buf1 */
93     for (i=0; i < ace1count; ) {
94         memcpy(&new_aces[i], &aces1[i], sizeof(ace_t));
95         i++;
96     }
97
98     j = i;
99
100     /* Copy ACEs from buf2 */
101     for (i=0; i < ace2count; ) {
102         memcpy(&new_aces[j], &aces2[i], sizeof(ace_t));
103         i++;
104         j++;
105     }
106     return new_aces;
107 }
108
109 /*
110   Remove any trivial ACE "in-place". Returns no of non-trivial ACEs
111 */
112 int strip_trivial_aces(ace_t **saces, int sacecount)
113 {
114     int i,j;
115     int nontrivaces = 0;
116     ace_t *aces = *saces;
117     ace_t *new_aces;
118
119     if (aces == NULL || sacecount <= 0)
120         return 0;
121
122     /* Count non-trivial ACEs */
123     for (i=0; i < sacecount; ) {
124         if ( ! (aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
125             nontrivaces++;
126         i++;
127     }
128     /* malloc buffer for new ACL */
129     if ((new_aces = malloc(nontrivaces * sizeof(ace_t))) == NULL) {
130         LOG(log_error, logtype_afpd, "strip_trivial_aces: malloc %s", strerror(errno));
131         return -1;
132     }
133
134     /* Copy non-trivial ACEs */
135     for (i=0, j=0; i < sacecount; ) {
136         if ( ! (aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE))) {
137             memcpy(&new_aces[j], &aces[i], sizeof(ace_t));
138             j++;
139         }
140         i++;
141     }
142
143     free(aces);
144     *saces = new_aces;
145
146     LOG(log_debug7, logtype_afpd, "strip_trivial_aces: non-trivial ACEs: %d", nontrivaces);
147
148     return nontrivaces;
149 }
150
151 /*
152   Remove non-trivial ACEs "in-place". Returns no of trivial ACEs.
153 */
154 int strip_nontrivial_aces(ace_t **saces, int sacecount)
155 {
156     int i,j;
157     int trivaces = 0;
158     ace_t *aces = *saces;
159     ace_t *new_aces;
160
161     /* Count trivial ACEs */
162     for (i=0; i < sacecount; ) {
163         if ((aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE)))
164             trivaces++;
165         i++;
166     }
167     /* malloc buffer for new ACL */
168     if ((new_aces = malloc(trivaces * sizeof(ace_t))) == NULL) {
169         LOG(log_error, logtype_afpd, "strip_nontrivial_aces: malloc %s", strerror(errno));
170         return -1;
171     }
172
173     /* Copy trivial ACEs */
174     for (i=0, j=0; i < sacecount; ) {
175         if ((aces[i].a_flags & (ACE_OWNER | ACE_GROUP | ACE_EVERYONE))) {
176             memcpy(&new_aces[j], &aces[i], sizeof(ace_t));
177             j++;
178         }
179         i++;
180     }
181     /* Free old ACEs */
182     free(aces);
183     *saces = new_aces;
184
185     LOG(log_debug7, logtype_afpd, "strip_nontrivial_aces: trivial ACEs: %d", trivaces);
186
187     return trivaces;
188 }
189
190 /*!
191  * Change mode of file preserving existing explicit ACEs
192  *
193  * nfsv4_chmod
194  * (1) reads objects ACL (acl1)
195  * (2) removes all trivial ACEs from the ACL by calling strip_trivial_aces(), possibly
196  *     leaving 0 ACEs in the ACL if there were only trivial ACEs as mapped from the mode
197  * (3) calls chmod() with mode
198  * (4) reads the changed ACL (acl2) which
199  *     a) might still contain explicit ACEs (up to onnv132)
200  *     b) will have any explicit ACE removed (starting with onnv145/Openindiana)
201  * (5) strip any explicit ACE from acl2 using strip_nontrivial_aces()
202  * (6) merge acl2 and acl2
203  * (7) set the ACL merged ACL on the object
204  */
205 int nfsv4_chmod(char *name, mode_t mode)
206 {
207     int ret = -1;
208     int noaces, nnaces;
209     ace_t *oacl = NULL, *nacl = NULL, *cacl;
210
211     LOG(log_debug, logtype_afpd, "nfsv4_chmod(\"%s/%s\", %04o)",
212         getcwdpath(), name, mode);
213
214     if ((noaces = get_nfsv4_acl(name, &oacl)) == -1) /* (1) */
215         goto exit;
216     if ((noaces = strip_trivial_aces(&oacl, noaces)) == -1) /* (2) */
217         goto exit;
218
219 #ifdef chmod
220 #undef chmod
221 #endif
222     if (chmod(name, mode) != 0) /* (3) */
223         goto exit;
224
225     if ((nnaces = get_nfsv4_acl(name, &nacl)) == -1) /* (4) */
226         goto exit;
227     if ((nnaces = strip_nontrivial_aces(&nacl, nnaces)) == -1) /* (5) */
228         goto exit;
229
230     if ((cacl = concat_aces(oacl, noaces, nacl, nnaces)) == NULL) /* (6) */
231         goto exit;
232
233     if ((ret = acl(name, ACE_SETACL, noaces + nnaces, cacl)) != 0) {
234         LOG(log_error, logtype_afpd, "nfsv4_chmod: error setting acl: %s", strerror(errno));
235         goto exit;
236     }
237
238 exit:
239     if (oacl) free(oacl);
240     if (nacl) free(nacl);
241     if (cacl) free(cacl);
242
243     LOG(log_debug, logtype_afpd, "nfsv4_chmod(\"%s/%s\", %04o): result: %u",
244         ret, getcwdpath(), name, mode);
245
246     return ret;
247 }
248
249
250 #endif /* HAVE_NFSv4_ACLS */