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