vfs: add acl type arg to SMB_VFS_SYS_ACL_SET_FD()
[samba.git] / source3 / modules / vfs_tru64acl.c
1 /*
2    Unix SMB/Netbios implementation.
3    VFS module to get and set Tru64 acls
4    Copyright (C) Michael Adam 2006,2008
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_tru64acl.h"
24
25 /* prototypes for private functions first - for clarity */
26
27 static struct smb_acl_t *tru64_acl_to_smb_acl(const struct acl *tru64_acl,
28                                               TALLOC_CTX *mem_ctx);
29 static bool tru64_ace_to_smb_ace(acl_entry_t tru64_ace, 
30                                 struct smb_acl_entry *smb_ace);
31 static acl_t smb_acl_to_tru64_acl(const SMB_ACL_T smb_acl);
32 static acl_tag_t smb_tag_to_tru64(SMB_ACL_TAG_T smb_tag);
33 static SMB_ACL_TAG_T tru64_tag_to_smb(acl_tag_t tru64_tag);
34 static acl_perm_t smb_permset_to_tru64(SMB_ACL_PERM_T smb_permset);
35 static SMB_ACL_PERM_T tru64_permset_to_smb(const acl_perm_t tru64_permset);
36
37
38 /* public functions - the api */
39
40 SMB_ACL_T tru64acl_sys_acl_get_file(vfs_handle_struct *handle,
41                                 const struct smb_filename *smb_fname,
42                                 SMB_ACL_TYPE_T type,
43                                 TALLOC_CTX *mem_ctx)
44 {
45         struct smb_acl_t *result;
46         acl_type_t the_acl_type;
47         acl_t tru64_acl;
48
49         DEBUG(10, ("Hi! This is tru64acl_sys_acl_get_file.\n"));
50
51         switch(type) {
52         case SMB_ACL_TYPE_ACCESS:
53                 the_acl_type = ACL_TYPE_ACCESS;
54                 break;
55         case SMB_ACL_TYPE_DEFAULT:
56                 the_acl_type = ACL_TYPE_DEFAULT;
57                 break;
58         default:
59                 errno = EINVAL;
60                 return NULL;
61         }
62
63         tru64_acl = acl_get_file((char *)smb_fname->base_name, the_acl_type);
64
65         if (tru64_acl == NULL) {
66                 return NULL;
67         }
68
69         result = tru64_acl_to_smb_acl(tru64_acl, mem_ctx);
70         acl_free(tru64_acl);
71         return result;
72 }
73
74 SMB_ACL_T tru64acl_sys_acl_get_fd(vfs_handle_struct *handle,
75                                   files_struct *fsp,
76                                   TALLOC_CTX *mem_ctx)
77 {
78         struct smb_acl_t *result;
79         acl_t tru64_acl = acl_get_fd(fsp_get_io_fd(fsp), ACL_TYPE_ACCESS);
80
81         if (tru64_acl == NULL) {
82                 return NULL;
83         }
84         
85         result = tru64_acl_to_smb_acl(tru64_acl, mem_ctx);
86         acl_free(tru64_acl);
87         return result;
88 }
89
90 int tru64acl_sys_acl_set_file(vfs_handle_struct *handle,
91                               const struct smb_filename *smb_fname,
92                               SMB_ACL_TYPE_T type,
93                               SMB_ACL_T theacl)
94 {
95         int res;
96         acl_type_t the_acl_type;
97         acl_t tru64_acl;
98
99         DEBUG(10, ("tru64acl_sys_acl_set_file called with name %s, type %d\n", 
100                         smb_fname->base_name, type));
101
102         switch(type) {
103         case SMB_ACL_TYPE_ACCESS:
104                 DEBUGADD(10, ("got acl type ACL_TYPE_ACCESS\n"));
105                 the_acl_type = ACL_TYPE_ACCESS;
106                 break;
107         case SMB_ACL_TYPE_DEFAULT:
108                 DEBUGADD(10, ("got acl type ACL_TYPE_DEFAULT\n"));
109                 the_acl_type = ACL_TYPE_DEFAULT;
110                 break;
111         default:
112                 DEBUGADD(10, ("invalid acl type\n"));
113                 errno = EINVAL;
114                 goto fail;
115         }
116
117         tru64_acl = smb_acl_to_tru64_acl(theacl);
118         if (tru64_acl == NULL) {
119                 DEBUG(10, ("smb_acl_to_tru64_acl failed!\n"));
120                 goto fail;
121         }
122         DEBUG(10, ("got tru64 acl...\n"));
123         res = acl_set_file((char *)smb_fname->base_name,
124                                 the_acl_type, tru64_acl);
125         acl_free(tru64_acl);
126         if (res != 0) {
127                 DEBUG(10, ("acl_set_file failed: %s\n", strerror(errno)));
128                 goto fail;
129         }
130         return res;
131 fail:
132         DEBUG(1, ("tru64acl_sys_acl_set_file failed!\n"));
133         return -1;
134 }
135
136 int tru64acl_sys_acl_set_fd(vfs_handle_struct *handle,
137                             files_struct *fsp,
138                             SMB_ACL_TYPE_T type,
139                             SMB_ACL_T theacl)
140 {
141         int res;
142         acl_t tru64_acl = smb_acl_to_tru64_acl(theacl);
143         if (tru64_acl == NULL) {
144                 return -1;
145         }
146         res =  acl_set_fd(fsp_get_io_fd(fsp), ACL_TYPE_ACCESS, tru64_acl);
147         acl_free(tru64_acl);
148         return res;
149
150 }
151
152 int tru64acl_sys_acl_delete_def_file(vfs_handle_struct *handle,
153                                 const struct smb_filename *smb_fname)
154 {
155         return acl_delete_def_file((char *)smb_fname->base_name);
156 }
157
158
159 /* private functions */
160
161 static struct smb_acl_t *tru64_acl_to_smb_acl(const struct acl *tru64_acl,
162                                               TALLOC_CTX *mem_ctx)
163 {
164         struct smb_acl_t *result;
165         acl_entry_t entry;
166
167         DEBUG(10, ("Hi! This is tru64_acl_to_smb_acl.\n"));
168         
169         if ((result = sys_acl_init(mem_ctx)) == NULL) {
170                 DEBUG(0, ("sys_acl_init() failed in tru64_acl_to_smb_acl\n"));
171                 errno = ENOMEM;
172                 goto fail;
173         }
174         if (acl_first_entry((struct acl *)tru64_acl) != 0) {
175                 DEBUG(10, ("acl_first_entry failed: %s\n", strerror(errno)));
176                 goto fail;
177         }
178         while ((entry = acl_get_entry((struct acl *)tru64_acl)) != NULL) {
179                 result->acl = talloc_realloc(result, result->acl, struct smb_acl_entry, 
180                                              result->count + 1);
181                 if (result->acl == NULL) {
182                         TALLOC_FREE(result);
183                         DEBUG(0, ("talloc_realloc failed in tru64_acl_to_smb_acl\n"));
184                         errno = ENOMEM;
185                         goto fail;
186                 }
187                 /* XYZ */
188                 if (!tru64_ace_to_smb_ace(entry, &result->acl[result->count])) {
189                         TALLOC_FREE(result);
190                         goto fail;
191                 }
192                 result->count += 1;
193         }
194         return result;
195
196 fail:
197         TALLOC_FREE(result);
198         DEBUG(1, ("tru64_acl_to_smb_acl failed!\n"));
199         return NULL;
200 }
201
202 static bool tru64_ace_to_smb_ace(acl_entry_t tru64_ace, 
203                                 struct smb_acl_entry *smb_ace) 
204 {
205         acl_tag_t tru64_tag;
206         acl_permset_t permset;
207         SMB_ACL_TAG_T smb_tag_type;
208         SMB_ACL_PERM_T smb_permset;
209         void *qualifier;
210
211         if (acl_get_tag_type(tru64_ace, &tru64_tag) != 0) {
212                 DEBUG(0, ("acl_get_tag_type failed: %s\n", strerror(errno)));
213                 return False;
214         }
215         
216         /* On could set the tag type directly to save a function call, 
217          * but I like this better... */
218         smb_tag_type = tru64_tag_to_smb(tru64_tag);
219         if (smb_tag_type == 0) {
220                 DEBUG(3, ("invalid tag type given: %d\n", tru64_tag));
221                 return False;
222         }
223         if (sys_acl_set_tag_type(smb_ace, smb_tag_type) != 0) {
224                 DEBUG(3, ("sys_acl_set_tag_type failed: %s\n", 
225                                 strerror(errno)));
226                 return False;
227         }
228         qualifier = acl_get_qualifier(tru64_ace);
229         if (qualifier != NULL) {
230                 if (sys_acl_set_qualifier(smb_ace, qualifier) != 0) {
231                         DEBUG(3, ("sys_acl_set_qualifier failed\n"));
232                         return False;
233                 }
234         }
235         if (acl_get_permset(tru64_ace, &permset) != 0) {
236                 DEBUG(3, ("acl_get_permset failed: %s\n", strerror(errno)));
237                 return False;
238         }
239         smb_permset = tru64_permset_to_smb(*permset);
240         if (sys_acl_set_permset(smb_ace, &smb_permset) != 0) {
241                 DEBUG(3, ("sys_acl_set_permset failed: %s\n", strerror(errno)));
242                 return False;
243         }
244         return True;
245 }
246
247 static acl_t smb_acl_to_tru64_acl(const SMB_ACL_T smb_acl) 
248 {
249         acl_t result;
250         acl_entry_t tru64_entry;
251         int i;
252         char *acl_text;
253         ssize_t acl_text_len;
254         
255         /* The tru64 acl_init function takes a size_t value
256          * instead of a count of entries (as with posix). 
257          * the size parameter "Specifies the size of the working
258          * storage in bytes" (according to the man page). 
259          * But it is unclear to me, how this size is to be 
260          * calculated. 
261          *
262          * It should not matter, since acl_create_entry enlarges 
263          * the working storage at need. ... */
264
265         DEBUG(10, ("Hi! This is smb_acl_to_tru64_acl.\n"));
266
267         result = acl_init(1);
268
269         if (result == NULL) {
270                 DEBUG(3, ("acl_init failed!\n"));
271                 goto fail;
272         }
273         
274         DEBUGADD(10, ("parsing acl entries...\n"));
275         for (i = 0; i < smb_acl->count; i++) {
276                 /* XYZ - maybe eliminate this direct access? */
277                 const struct smb_acl_entry *smb_entry = &smb_acl->acl[i];
278                 acl_tag_t tru64_tag;
279                 acl_perm_t tru64_permset;
280
281                 tru64_tag = smb_tag_to_tru64(smb_entry->a_type);
282                 if (tru64_tag == -1) {
283                         DEBUG(3, ("smb_tag_to_tru64 failed!\n"));
284                         goto fail;
285                 }
286
287                 if (tru64_tag == ACL_MASK) {
288                         DEBUGADD(10, (" - acl type ACL_MASK: not implemented on Tru64 ==> skipping\n"));
289                         continue;
290                 }
291                 
292                 tru64_entry = acl_create_entry(&result);
293                 if (tru64_entry == NULL) {
294                         DEBUG(3, ("acl_create_entry failed: %s\n", 
295                                         strerror(errno)));
296                         goto fail;
297                 }
298
299                 if (acl_set_tag_type(tru64_entry, tru64_tag) != 0) {
300                         DEBUG(3, ("acl_set_tag_type(%d) failed: %s\n",
301                                         strerror(errno)));
302                         goto fail;
303                 }
304                 
305                 switch (smb_entry->a_type) {
306                 case SMB_ACL_USER:
307                         if (acl_set_qualifier(tru64_entry, 
308                                                 (int *)&smb_entry->info.user.uid) != 0) 
309                         {
310                                 DEBUG(3, ("acl_set_qualifier failed: %s\n",
311                                         strerror(errno)));
312                                 goto fail;
313                         }
314                         DEBUGADD(10, (" - setting uid to %d\n", smb_entry->info.user.uid));
315                         break;
316                 case SMB_ACL_GROUP:
317                         if (acl_set_qualifier(tru64_entry, 
318                                                 (int *)&smb_entry->info.group.gid) != 0)
319                         {
320                                 DEBUG(3, ("acl_set_qualifier failed: %s\n",
321                                         strerror(errno)));
322                                 goto fail;
323                         }
324                         DEBUGADD(10, (" - setting gid to %d\n", smb_entry->info.group.gid));
325                         break;
326                 default:
327                         break;
328                 }
329
330                 tru64_permset = smb_permset_to_tru64(smb_entry->a_perm);
331                 if (tru64_permset == -1) {
332                         DEBUG(3, ("smb_permset_to_tru64 failed!\n"));
333                         goto fail;
334                 }
335                 DEBUGADD(10, (" - setting perms to %0d\n", tru64_permset));
336                 if (acl_set_permset(tru64_entry, &tru64_permset) != 0)
337                 {
338                         DEBUG(3, ("acl_set_permset failed: %s\n", strerror(errno)));
339                         goto fail;
340                 }
341         } /* for */
342         DEBUGADD(10, ("done parsing acl entries\n"));
343
344         tru64_entry = NULL;
345         if (acl_valid(result, &tru64_entry) != 0) {
346                 DEBUG(1, ("smb_acl_to_tru64_acl: ACL is invalid (%s)\n",
347                                 strerror(errno)));
348                 if (tru64_entry != NULL) {
349                         DEBUGADD(1, ("the acl contains duplicate entries\n"));
350                 }
351                 goto fail;
352         }
353         DEBUGADD(10, ("acl is valid\n"));
354
355         acl_text = acl_to_text(result, &acl_text_len);
356         if (acl_text == NULL) {
357                 DEBUG(3, ("acl_to_text failed: %s\n", strerror(errno)));
358                 goto fail;
359         }
360         DEBUG(1, ("acl_text: %s\n", acl_text));
361         free(acl_text);
362
363         return result;
364
365 fail:
366         if (result != NULL) {
367                 acl_free(result);
368         }
369         DEBUG(1, ("smb_acl_to_tru64_acl failed!\n"));
370         return NULL;
371 }
372
373 static acl_tag_t smb_tag_to_tru64(SMB_ACL_TAG_T smb_tag)
374 {
375         acl_tag_t result;
376         switch (smb_tag) {
377         case SMB_ACL_USER:
378                 result = ACL_USER;
379                 DEBUGADD(10, ("got acl type ACL_USER\n"));
380                 break;
381         case SMB_ACL_USER_OBJ:
382                 result = ACL_USER_OBJ;
383                 DEBUGADD(10, ("got acl type ACL_USER_OBJ\n"));
384                 break;
385         case SMB_ACL_GROUP:
386                 result = ACL_GROUP;
387                 DEBUGADD(10, ("got acl type ACL_GROUP\n"));
388                 break;
389         case SMB_ACL_GROUP_OBJ:
390                 result = ACL_GROUP_OBJ;
391                 DEBUGADD(10, ("got acl type ACL_GROUP_OBJ\n"));
392                 break;
393         case SMB_ACL_OTHER:
394                 result = ACL_OTHER;
395                 DEBUGADD(10, ("got acl type ACL_OTHER\n"));
396                 break;
397         case SMB_ACL_MASK:
398                 result = ACL_MASK;
399                 DEBUGADD(10, ("got acl type ACL_MASK\n"));
400                 break;
401         default:
402                 DEBUG(1, ("Unknown tag type %d\n", smb_tag));
403                 result = -1;
404         }
405         return result;
406 }
407
408
409 static SMB_ACL_TAG_T tru64_tag_to_smb(acl_tag_t tru64_tag)
410 {
411         SMB_ACL_TAG_T smb_tag_type;
412         switch(tru64_tag) {
413         case ACL_USER:
414                 smb_tag_type = SMB_ACL_USER;
415                 DEBUGADD(10, ("got smb acl tag type SMB_ACL_USER\n"));
416                 break;
417         case ACL_USER_OBJ:
418                 smb_tag_type = SMB_ACL_USER_OBJ;
419                 DEBUGADD(10, ("got smb acl tag type SMB_ACL_USER_OBJ\n"));
420                 break;
421         case ACL_GROUP:
422                 smb_tag_type = SMB_ACL_GROUP;
423                 DEBUGADD(10, ("got smb acl tag type SMB_ACL_GROUP\n"));
424                 break;
425         case ACL_GROUP_OBJ:
426                 smb_tag_type = SMB_ACL_GROUP_OBJ;
427                 DEBUGADD(10, ("got smb acl tag type SMB_ACL_GROUP_OBJ\n"));
428                 break;
429         case ACL_OTHER:
430                 smb_tag_type = SMB_ACL_OTHER;
431                 DEBUGADD(10, ("got smb acl tag type SMB_ACL_OTHER\n"));
432                 break;
433         case ACL_MASK:
434                 smb_tag_type = SMB_ACL_MASK;
435                 DEBUGADD(10, ("got smb acl tag type SMB_ACL_MASK\n"));
436                 break;
437         default:
438                 DEBUG(0, ("Unknown tag type %d\n", (unsigned int)tru64_tag));
439                 smb_tag_type = 0;
440         }
441         return smb_tag_type;
442 }
443
444 static acl_perm_t smb_permset_to_tru64(SMB_ACL_PERM_T smb_permset)
445 {
446         /* originally, I thought that acl_clear_perm was the
447          * proper way to reset the permset to 0. but without 
448          * initializing it to 0, acl_clear_perm fails.
449          * so probably, acl_clear_perm is not necessary here... ?! */
450         acl_perm_t tru64_permset = 0;
451         if (acl_clear_perm(&tru64_permset) != 0) {
452                 DEBUG(5, ("acl_clear_perm failed: %s\n", strerror(errno)));
453                 return -1;
454         }
455         /* according to original lib/sysacls.c, acl_add_perm is
456          * broken on tru64 ... */
457         tru64_permset |= ((smb_permset & SMB_ACL_READ) ? ACL_READ : 0);
458         tru64_permset |= ((smb_permset & SMB_ACL_WRITE) ? ACL_WRITE : 0);
459         tru64_permset |= ((smb_permset & SMB_ACL_EXECUTE) ? ACL_EXECUTE : 0);
460         return tru64_permset;
461 }
462
463 static SMB_ACL_PERM_T tru64_permset_to_smb(const acl_perm_t tru64_permset)
464 {
465         SMB_ACL_PERM_T smb_permset  = 0;
466         smb_permset |= ((tru64_permset & ACL_READ) ? SMB_ACL_READ : 0);
467         smb_permset |= ((tru64_permset & ACL_WRITE) ? SMB_ACL_WRITE : 0);
468         smb_permset |= ((tru64_permset & ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
469         return smb_permset;
470 }
471
472
473 /* VFS operations structure */
474
475 static struct vfs_fn_pointers tru64acl_fns = {
476         .sys_acl_get_file_fn = tru64acl_sys_acl_get_file,
477         .sys_acl_get_fd_fn = tru64acl_sys_acl_get_fd,
478         .sys_acl_blob_get_file_fn = posix_sys_acl_blob_get_file,
479         .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
480         .sys_acl_set_file_fn = tru64acl_sys_acl_set_file,
481         .sys_acl_set_fd_fn = tru64acl_sys_acl_set_fd,
482         .sys_acl_delete_def_file_fn = tru64acl_sys_acl_delete_def_file,
483 };
484
485 static_decl_vfs;
486 NTSTATUS vfs_tru64acl_init(TALLOC_CTX *ctx)
487 {
488         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "tru64acl",
489                                 &tru64acl_fns);
490 }
491
492 /* ENTE */