smbd-posix_acls: Use a IDL union to store the ACL entry
[kai/samba.git] / source3 / modules / vfs_posixacl.c
1 /*
2    Unix SMB/Netbios implementation.
3    VFS module to get and set posix acls
4    Copyright (C) Volker Lendecke 2006
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "smbd/smbd.h"
23 #include "modules/vfs_posixacl.h"
24
25 /* prototypes for static functions first - for clarity */
26
27 static bool smb_ace_to_internal(acl_entry_t posix_ace,
28                                 struct smb_acl_entry *ace);
29 static struct smb_acl_t *smb_acl_to_internal(acl_t acl);
30 static int smb_acl_set_mode(acl_entry_t entry, SMB_ACL_PERM_T perm);
31 static acl_t smb_acl_to_posix(const struct smb_acl_t *acl);
32
33
34 /* public functions - the api */
35
36 SMB_ACL_T posixacl_sys_acl_get_file(vfs_handle_struct *handle,
37                                     const char *path_p,
38                                     SMB_ACL_TYPE_T type)
39 {
40         struct smb_acl_t *result;
41         acl_type_t acl_type;
42         acl_t acl;
43
44         switch(type) {
45         case SMB_ACL_TYPE_ACCESS:
46                 acl_type = ACL_TYPE_ACCESS;
47                 break;
48         case SMB_ACL_TYPE_DEFAULT:
49                 acl_type = ACL_TYPE_DEFAULT;
50                 break;
51         default:
52                 errno = EINVAL;
53                 return NULL;
54         }
55
56         acl = acl_get_file(path_p, acl_type);
57
58         if (acl == NULL) {
59                 return NULL;
60         }
61
62         result = smb_acl_to_internal(acl);
63         acl_free(acl);
64         return result;
65 }
66
67 SMB_ACL_T posixacl_sys_acl_get_fd(vfs_handle_struct *handle,
68                                   files_struct *fsp)
69 {
70         struct smb_acl_t *result;
71         acl_t acl = acl_get_fd(fsp->fh->fd);
72
73         if (acl == NULL) {
74                 return NULL;
75         }
76
77         result = smb_acl_to_internal(acl);
78         acl_free(acl);
79         return result;
80 }
81
82 int posixacl_sys_acl_set_file(vfs_handle_struct *handle,
83                               const char *name,
84                               SMB_ACL_TYPE_T type,
85                               SMB_ACL_T theacl)
86 {
87         int res;
88         acl_type_t acl_type;
89         acl_t acl;
90
91         DEBUG(10, ("Calling acl_set_file: %s, %d\n", name, type));
92
93         switch(type) {
94         case SMB_ACL_TYPE_ACCESS:
95                 acl_type = ACL_TYPE_ACCESS;
96                 break;
97         case SMB_ACL_TYPE_DEFAULT:
98                 acl_type = ACL_TYPE_DEFAULT;
99                 break;
100         default:
101                 errno = EINVAL;
102                 return -1;
103         }
104
105         if ((acl = smb_acl_to_posix(theacl)) == NULL) {
106                 return -1;
107         }
108         res = acl_set_file(name, acl_type, acl);
109         if (res != 0) {
110                 DEBUG(10, ("acl_set_file failed: %s\n", strerror(errno)));
111         }
112         acl_free(acl);
113         return res;
114 }
115
116 int posixacl_sys_acl_set_fd(vfs_handle_struct *handle,
117                             files_struct *fsp,
118                             SMB_ACL_T theacl)
119 {
120         int res;
121         acl_t acl = smb_acl_to_posix(theacl);
122         if (acl == NULL) {
123                 return -1;
124         }
125         res =  acl_set_fd(fsp->fh->fd, acl);
126         acl_free(acl);
127         return res;
128 }
129
130 int posixacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
131                                      const char *path)
132 {
133         return acl_delete_def_file(path);
134 }
135
136
137 /* private functions */
138
139 static bool smb_ace_to_internal(acl_entry_t posix_ace,
140                                 struct smb_acl_entry *ace)
141 {
142         acl_tag_t tag;
143         acl_permset_t permset;
144
145         if (acl_get_tag_type(posix_ace, &tag) != 0) {
146                 DEBUG(0, ("smb_acl_get_tag_type failed\n"));
147                 return False;
148         }
149
150         switch(tag) {
151         case ACL_USER:
152                 ace->a_type = SMB_ACL_USER;
153                 break;
154         case ACL_USER_OBJ:
155                 ace->a_type = SMB_ACL_USER_OBJ;
156                 break;
157         case ACL_GROUP:
158                 ace->a_type = SMB_ACL_GROUP;
159                 break;
160         case ACL_GROUP_OBJ:
161                 ace->a_type = SMB_ACL_GROUP_OBJ;
162                 break;
163         case ACL_OTHER:
164                 ace->a_type = SMB_ACL_OTHER;
165                 break;
166         case ACL_MASK:
167                 ace->a_type = SMB_ACL_MASK;
168                 break;
169         default:
170                 DEBUG(0, ("unknown tag type %d\n", (unsigned int)tag));
171                 return False;
172         }
173         switch(ace->a_type) {
174         case SMB_ACL_USER: {
175                 uid_t *puid = (uid_t *)acl_get_qualifier(posix_ace);
176                 if (puid == NULL) {
177                         DEBUG(0, ("smb_acl_get_qualifier failed\n"));
178                         return False;
179                 }
180                 ace->info.user.uid = *puid;
181                 acl_free(puid);
182                 break;
183         }
184                 
185         case SMB_ACL_GROUP: {
186                 gid_t *pgid = (uid_t *)acl_get_qualifier(posix_ace);
187                 if (pgid == NULL) {
188                         DEBUG(0, ("smb_acl_get_qualifier failed\n"));
189                         return False;
190                 }
191                 ace->info.group.gid = *pgid;
192                 acl_free(pgid);
193                 break;
194         }
195         default:
196                 break;
197         }
198         if (acl_get_permset(posix_ace, &permset) != 0) {
199                 DEBUG(0, ("smb_acl_get_mode failed\n"));
200                 return False;
201         }
202         ace->a_perm = 0;
203 #ifdef HAVE_ACL_GET_PERM_NP
204         ace->a_perm |= (acl_get_perm_np(permset, ACL_READ) ? SMB_ACL_READ : 0);
205         ace->a_perm |= (acl_get_perm_np(permset, ACL_WRITE) ? SMB_ACL_WRITE : 0);
206         ace->a_perm |= (acl_get_perm_np(permset, ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
207 #else
208         ace->a_perm |= (acl_get_perm(permset, ACL_READ) ? SMB_ACL_READ : 0);
209         ace->a_perm |= (acl_get_perm(permset, ACL_WRITE) ? SMB_ACL_WRITE : 0);
210         ace->a_perm |= (acl_get_perm(permset, ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
211 #endif
212         return True;
213 }
214
215 static struct smb_acl_t *smb_acl_to_internal(acl_t acl)
216 {
217         struct smb_acl_t *result = sys_acl_init(0);
218         int entry_id = ACL_FIRST_ENTRY;
219         acl_entry_t e;
220         if (result == NULL) {
221                 return NULL;
222         }
223         while (acl_get_entry(acl, entry_id, &e) == 1) {
224
225                 entry_id = ACL_NEXT_ENTRY;
226
227                 result->acl = talloc_realloc(result, result->acl, 
228                                              struct smb_acl_entry, result->count+1);
229                 if (result->acl == NULL) {
230                         TALLOC_FREE(result);
231                         DEBUG(0, ("talloc_realloc failed\n"));
232                         errno = ENOMEM;
233                         return NULL;
234                 }
235
236                 if (!smb_ace_to_internal(e, &result->acl[result->count])) {
237                         TALLOC_FREE(result);
238                         return NULL;
239                 }
240
241                 result->count += 1;
242         }
243         return result;
244 }
245
246 static int smb_acl_set_mode(acl_entry_t entry, SMB_ACL_PERM_T perm)
247 {
248         int ret;
249         acl_permset_t permset;
250
251         if ((ret = acl_get_permset(entry, &permset)) != 0) {
252                 return ret;
253         }
254         if ((ret = acl_clear_perms(permset)) != 0) {
255                 return ret;
256         }
257         if ((perm & SMB_ACL_READ) &&
258             ((ret = acl_add_perm(permset, ACL_READ)) != 0)) {
259                 return ret;
260         }
261         if ((perm & SMB_ACL_WRITE) &&
262             ((ret = acl_add_perm(permset, ACL_WRITE)) != 0)) {
263                 return ret;
264         }
265         if ((perm & SMB_ACL_EXECUTE) &&
266             ((ret = acl_add_perm(permset, ACL_EXECUTE)) != 0)) {
267                 return ret;
268         }
269         return acl_set_permset(entry, permset);
270 }
271
272 static acl_t smb_acl_to_posix(const struct smb_acl_t *acl)
273 {
274         acl_t result;
275         int i;
276
277         result = acl_init(acl->count);
278         if (result == NULL) {
279                 DEBUG(10, ("acl_init failed\n"));
280                 return NULL;
281         }
282
283         for (i=0; i<acl->count; i++) {
284                 const struct smb_acl_entry *entry = &acl->acl[i];
285                 acl_entry_t e;
286                 acl_tag_t tag;
287
288                 if (acl_create_entry(&result, &e) != 0) {
289                         DEBUG(1, ("acl_create_entry failed: %s\n",
290                                   strerror(errno)));
291                         goto fail;
292                 }
293
294                 switch (entry->a_type) {
295                 case SMB_ACL_USER:
296                         tag = ACL_USER;
297                         break;
298                 case SMB_ACL_USER_OBJ:
299                         tag = ACL_USER_OBJ;
300                         break;
301                 case SMB_ACL_GROUP:
302                         tag = ACL_GROUP;
303                         break;
304                 case SMB_ACL_GROUP_OBJ:
305                         tag = ACL_GROUP_OBJ;
306                         break;
307                 case SMB_ACL_OTHER:
308                         tag = ACL_OTHER;
309                         break;
310                 case SMB_ACL_MASK:
311                         tag = ACL_MASK;
312                         break;
313                 default:
314                         DEBUG(1, ("Unknown tag value %d\n", entry->a_type));
315                         goto fail;
316                 }
317
318                 if (acl_set_tag_type(e, tag) != 0) {
319                         DEBUG(10, ("acl_set_tag_type(%d) failed: %s\n",
320                                    tag, strerror(errno)));
321                         goto fail;
322                 }
323
324                 switch (entry->a_type) {
325                 case SMB_ACL_USER:
326                         if (acl_set_qualifier(e, &entry->info.user.uid) != 0) {
327                                 DEBUG(1, ("acl_set_qualifiier failed: %s\n",
328                                           strerror(errno)));
329                                 goto fail;
330                         }
331                         break;
332                 case SMB_ACL_GROUP:
333                         if (acl_set_qualifier(e, &entry->info.group.gid) != 0) {
334                                 DEBUG(1, ("acl_set_qualifiier failed: %s\n",
335                                           strerror(errno)));
336                                 goto fail;
337                         }
338                         break;
339                 default:        /* Shut up, compiler! :-) */
340                         break;
341                 }
342
343                 if (smb_acl_set_mode(e, entry->a_perm) != 0) {
344                         goto fail;
345                 }
346         }
347
348         if (acl_valid(result) != 0) {
349                 char *acl_string = sys_acl_to_text(acl, NULL);
350                 DEBUG(0, ("smb_acl_to_posix: ACL %s is invalid for set (%s)\n",
351                           acl_string, strerror(errno)));
352                 SAFE_FREE(acl_string);
353                 goto fail;
354         }
355
356         return result;
357
358  fail:
359         if (result != NULL) {
360                 acl_free(result);
361         }
362         return NULL;
363 }
364
365 /* VFS operations structure */
366
367 static struct vfs_fn_pointers posixacl_fns = {
368         .sys_acl_get_file_fn = posixacl_sys_acl_get_file,
369         .sys_acl_get_fd_fn = posixacl_sys_acl_get_fd,
370         .sys_acl_set_file_fn = posixacl_sys_acl_set_file,
371         .sys_acl_set_fd_fn = posixacl_sys_acl_set_fd,
372         .sys_acl_delete_def_file_fn = posixacl_sys_acl_delete_def_file,
373 };
374
375 NTSTATUS vfs_posixacl_init(void);
376 NTSTATUS vfs_posixacl_init(void)
377 {
378         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "posixacl",
379                                 &posixacl_fns);
380 }