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