s3: VFS: Change SMB_VFS_SYS_ACL_GET_FILE to use const struct smb_filename * instead...
[samba.git] / source3 / modules / vfs_solarisacl.c
1 /*
2    Unix SMB/Netbios implementation.
3    VFS module to get and set Solaris 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
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "modules/vfs_solarisacl.h"
25
26 /* typedef struct acl SOLARIS_ACE_T; */
27 typedef aclent_t SOLARIS_ACE_T;
28 typedef aclent_t *SOLARIS_ACL_T;
29 typedef int SOLARIS_ACL_TAG_T;   /* the type of an ACL entry */
30 typedef o_mode_t SOLARIS_PERM_T;
31
32 /* for convenience: check if solaris acl entry is a default entry?  */
33 #define _IS_DEFAULT(ace) ((ace).a_type & ACL_DEFAULT)
34 #define _IS_OF_TYPE(ace, type) ( \
35         (((type) == SMB_ACL_TYPE_ACCESS) && !_IS_DEFAULT(ace)) \
36         || \
37         (((type) == SMB_ACL_TYPE_DEFAULT) && _IS_DEFAULT(ace)) \
38 )
39
40
41 /* prototypes for private functions */
42
43 static SOLARIS_ACL_T solaris_acl_init(int count);
44 static bool smb_acl_to_solaris_acl(SMB_ACL_T smb_acl, 
45                 SOLARIS_ACL_T *solariacl, int *count, 
46                 SMB_ACL_TYPE_T type);
47 static SMB_ACL_T solaris_acl_to_smb_acl(SOLARIS_ACL_T solarisacl, int count,
48                                         SMB_ACL_TYPE_T type, TALLOC_CTX *mem_ctx);
49 static SOLARIS_ACL_TAG_T smb_tag_to_solaris_tag(SMB_ACL_TAG_T smb_tag);
50 static SMB_ACL_TAG_T solaris_tag_to_smb_tag(SOLARIS_ACL_TAG_T solaris_tag);
51 static bool solaris_add_to_acl(SOLARIS_ACL_T *solaris_acl, int *count,
52                 SOLARIS_ACL_T add_acl, int add_count, SMB_ACL_TYPE_T type);
53 static bool solaris_acl_get_file(const char *name, SOLARIS_ACL_T *solarisacl, 
54                 int *count);
55 static bool solaris_acl_get_fd(int fd, SOLARIS_ACL_T *solarisacl, int *count);
56 static bool solaris_acl_sort(SOLARIS_ACL_T theacl, int count);
57 static SMB_ACL_PERM_T solaris_perm_to_smb_perm(const SOLARIS_PERM_T perm);
58 static SOLARIS_PERM_T smb_perm_to_solaris_perm(const SMB_ACL_PERM_T perm);
59 #if 0
60 static bool solaris_acl_check(SOLARIS_ACL_T solaris_acl, int count);
61 #endif
62
63 /* public functions - the api */
64
65 SMB_ACL_T solarisacl_sys_acl_get_file(vfs_handle_struct *handle,
66                                 const struct smb_filename *smb_fname,
67                                 SMB_ACL_TYPE_T type,
68                                 TALLOC_CTX *mem_ctx)
69 {
70         SMB_ACL_T result = NULL;
71         int count;
72         SOLARIS_ACL_T solaris_acl = NULL;
73         const char *path_p = smb_fname->base_name;
74         
75         DEBUG(10, ("solarisacl_sys_acl_get_file called for file '%s'.\n", 
76                    path_p));
77
78         if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
79                 DEBUG(10, ("invalid SMB_ACL_TYPE given (%d)\n", type));
80                 errno = EINVAL;
81                 goto done;
82         }
83
84         DEBUGADD(10, ("getting %s acl\n", 
85                       ((type == SMB_ACL_TYPE_ACCESS) ? "access" : "default")));
86
87         if (!solaris_acl_get_file(path_p, &solaris_acl, &count)) {
88                 goto done;
89         }
90         result = solaris_acl_to_smb_acl(solaris_acl, count, type, mem_ctx);
91         if (result == NULL) {
92                 DEBUG(10, ("conversion solaris_acl -> smb_acl failed (%s).\n",
93                            strerror(errno)));
94         }
95         
96  done:
97         DEBUG(10, ("solarisacl_sys_acl_get_file %s.\n",
98                    ((result == NULL) ? "failed" : "succeeded" )));
99         SAFE_FREE(solaris_acl);
100         return result;
101 }
102
103
104 /*
105  * get the access ACL of a file referred to by a fd
106  */
107 SMB_ACL_T solarisacl_sys_acl_get_fd(vfs_handle_struct *handle,
108                                     files_struct *fsp, TALLOC_CTX *mem_ctx)
109 {
110         SMB_ACL_T result = NULL;
111         int count;
112         SOLARIS_ACL_T solaris_acl = NULL;
113
114         DEBUG(10, ("entering solarisacl_sys_acl_get_fd.\n"));
115
116         if (!solaris_acl_get_fd(fsp->fh->fd, &solaris_acl, &count)) {
117                 goto done;
118         }
119         /* 
120          * The facl call returns both ACCESS and DEFAULT acls (as present). 
121          * The posix acl_get_fd function returns only the
122          * access acl. So we need to filter this out here.  
123          */
124         result = solaris_acl_to_smb_acl(solaris_acl, count,
125                                         SMB_ACL_TYPE_ACCESS, mem_ctx);
126         if (result == NULL) {
127                 DEBUG(10, ("conversion solaris_acl -> smb_acl failed (%s).\n",
128                            strerror(errno)));
129         }
130         
131  done:
132         DEBUG(10, ("solarisacl_sys_acl_get_fd %s.\n", 
133                    ((result == NULL) ? "failed" : "succeeded")));
134         SAFE_FREE(solaris_acl);
135         return result;
136 }
137
138 int solarisacl_sys_acl_set_file(vfs_handle_struct *handle,
139                                 const char *name,
140                                 SMB_ACL_TYPE_T type,
141                                 SMB_ACL_T theacl)
142 {
143         int ret = -1;
144         SOLARIS_ACL_T solaris_acl = NULL;
145         int count;
146         struct smb_filename smb_fname = {
147                 .base_name = discard_const_p(char, name)
148         };
149
150         DEBUG(10, ("solarisacl_sys_acl_set_file called for file '%s'\n",
151                    name));
152
153         if ((type != SMB_ACL_TYPE_ACCESS) && (type != SMB_ACL_TYPE_DEFAULT)) {
154                 errno = EINVAL;
155                 DEBUG(10, ("invalid smb acl type given (%d).\n", type));
156                 goto done;
157         }
158         DEBUGADD(10, ("setting %s acl\n", 
159                       ((type == SMB_ACL_TYPE_ACCESS) ? "access" : "default")));
160
161         if(!smb_acl_to_solaris_acl(theacl, &solaris_acl, &count, type)) {
162                 DEBUG(10, ("conversion smb_acl -> solaris_acl failed (%s).\n",
163                            strerror(errno)));
164                 goto done;
165         }
166
167         /*
168          * if the file is a directory, there is extra work to do:
169          * since the solaris acl call stores both the access acl and 
170          * the default acl as provided, we have to get the acl part 
171          * that has not been specified in "type" from the file first 
172          * and concatenate it with the acl provided.
173          *
174          * We can directly use SMB_VFS_STAT here, as if this was a
175          * POSIX call on a symlink, we've already refused it.
176          * For a Windows acl mapped call on a symlink, we want to follow
177          * it.
178          */
179         ret = SMB_VFS_STAT(handle->conn, &smb_fname);
180         if (ret != 0) {
181                 DEBUG(10, ("Error in stat call: %s\n", strerror(errno)));
182                 goto done;
183         }
184         if (S_ISDIR(smb_fname.st.st_ex_mode)) {
185                 SOLARIS_ACL_T other_acl = NULL;
186                 int other_count;
187                 SMB_ACL_TYPE_T other_type;
188
189                 other_type = (type == SMB_ACL_TYPE_ACCESS) 
190                         ? SMB_ACL_TYPE_DEFAULT
191                         : SMB_ACL_TYPE_ACCESS;
192                 DEBUGADD(10, ("getting acl from filesystem\n"));
193                 if (!solaris_acl_get_file(name, &other_acl, &other_count)) {
194                         DEBUG(10, ("error getting acl from directory\n"));
195                         goto done;
196                 }
197                 DEBUG(10, ("adding %s part of fs acl to given acl\n",
198                            ((other_type == SMB_ACL_TYPE_ACCESS) 
199                             ? "access"
200                             : "default")));
201                 if (!solaris_add_to_acl(&solaris_acl, &count, other_acl,
202                                         other_count, other_type)) 
203                 {
204                         DEBUG(10, ("error adding other acl.\n"));
205                         SAFE_FREE(other_acl);
206                         goto done;
207                 }
208                 SAFE_FREE(other_acl);
209         }
210         else if (type != SMB_ACL_TYPE_ACCESS) {
211                 errno = EINVAL;
212                 goto done;
213         }
214
215         if (!solaris_acl_sort(solaris_acl, count)) {
216                 DEBUG(10, ("resulting acl is not valid!\n"));
217                 goto done;
218         }
219
220         ret = acl(name, SETACL, count, solaris_acl);
221         
222  done:
223         DEBUG(10, ("solarisacl_sys_acl_set_file %s.\n",
224                    ((ret != 0) ? "failed" : "succeeded")));
225         SAFE_FREE(solaris_acl);
226         return ret;
227 }
228
229 /*
230  * set the access ACL on the file referred to by a fd 
231  */
232 int solarisacl_sys_acl_set_fd(vfs_handle_struct *handle,
233                               files_struct *fsp,
234                               SMB_ACL_T theacl)
235 {
236         SOLARIS_ACL_T solaris_acl = NULL;
237         SOLARIS_ACL_T default_acl = NULL;
238         int count, default_count;
239         int ret = -1;
240
241         DEBUG(10, ("entering solarisacl_sys_acl_set_fd\n"));
242
243         /* 
244          * the posix acl_set_fd call sets the access acl of the
245          * file referred to by fd. the solaris facl-SETACL call
246          * sets the access and default acl as provided, so we
247          * have to retrieve the default acl of the file and 
248          * concatenate it with the access acl provided.
249          */
250         if (!smb_acl_to_solaris_acl(theacl, &solaris_acl, &count, 
251                                     SMB_ACL_TYPE_ACCESS))
252         {
253                 DEBUG(10, ("conversion smb_acl -> solaris_acl failed (%s).\n",
254                            strerror(errno)));
255                 goto done;
256         }
257         if (!solaris_acl_get_fd(fsp->fh->fd, &default_acl, &default_count)) {
258                 DEBUG(10, ("error getting (default) acl from fd\n"));
259                 goto done;
260         }
261         if (!solaris_add_to_acl(&solaris_acl, &count,
262                                 default_acl, default_count,
263                                 SMB_ACL_TYPE_DEFAULT))
264         {
265                 DEBUG(10, ("error adding default acl to solaris acl\n"));
266                 goto done;
267         }
268         if (!solaris_acl_sort(solaris_acl, count)) {
269                 DEBUG(10, ("resulting acl is not valid!\n"));
270                 goto done;
271         }
272
273         ret = facl(fsp->fh->fd, SETACL, count, solaris_acl);
274         if (ret != 0) {
275                 DEBUG(10, ("call of facl failed (%s).\n", strerror(errno)));
276         }
277
278  done:
279         DEBUG(10, ("solarisacl_sys_acl_set_fd %s.\n",
280                    ((ret == 0) ? "succeeded" : "failed" )));
281         SAFE_FREE(solaris_acl);
282         SAFE_FREE(default_acl);
283         return ret;
284 }
285
286 /*
287  * delete the default ACL of a directory
288  *
289  * This is achieved by fetching the access ACL and rewriting it 
290  * directly, via the solaris system call: the SETACL call on 
291  * directories writes both the access and the default ACL as provided.
292  *
293  * XXX: posix acl_delete_def_file returns an error if
294  * the file referred to by path is not a directory.
295  * this function does not complain but the actions 
296  * have no effect on a file other than a directory.
297  * But sys_acl_delete_default_file is only called in
298  * smbd/posixacls.c after having checked that the file
299  * is a directory, anyways. So implementing the extra
300  * check is considered unnecessary. --- Agreed? XXX
301  */
302 int solarisacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
303                                 struct smb_filename *smb_fname)
304 {
305         SMB_ACL_T smb_acl;
306         int ret = -1;
307         SOLARIS_ACL_T solaris_acl = NULL;
308         int count;
309
310         DEBUG(10, ("entering solarisacl_sys_acl_delete_def_file.\n"));
311         
312         smb_acl = solarisacl_sys_acl_get_file(handle, smb_fname->base_name,
313                                               SMB_ACL_TYPE_ACCESS, talloc_tos());
314         if (smb_acl == NULL) {
315                 DEBUG(10, ("getting file acl failed!\n"));
316                 goto done;
317         }
318         if (!smb_acl_to_solaris_acl(smb_acl, &solaris_acl, &count, 
319                                     SMB_ACL_TYPE_ACCESS))
320         {
321                 DEBUG(10, ("conversion smb_acl -> solaris_acl failed.\n"));
322                 goto done;
323         }
324         if (!solaris_acl_sort(solaris_acl, count)) {
325                 DEBUG(10, ("resulting acl is not valid!\n"));
326                 goto done;
327         }
328         ret = acl(smb_fname->base_name, SETACL, count, solaris_acl);
329         if (ret != 0) {
330                 DEBUG(10, ("settinge file acl failed!\n"));
331         }
332         
333  done:
334         DEBUG(10, ("solarisacl_sys_acl_delete_def_file %s.\n",
335                    ((ret != 0) ? "failed" : "succeeded" )));
336         TALLOC_FREE(smb_acl);
337         return ret;
338 }
339
340
341 /* private functions */
342
343 static SOLARIS_ACL_T solaris_acl_init(int count)
344 {
345         SOLARIS_ACL_T solaris_acl = 
346                 (SOLARIS_ACL_T)SMB_MALLOC(sizeof(aclent_t) * count);
347         if (solaris_acl == NULL) {
348                 errno = ENOMEM;
349         }
350         return solaris_acl;
351 }
352
353 /*
354  * Convert the SMB acl to the ACCESS or DEFAULT part of a 
355  * solaris ACL, as desired.
356  */
357 static bool smb_acl_to_solaris_acl(SMB_ACL_T smb_acl, 
358                                    SOLARIS_ACL_T *solaris_acl, int *count, 
359                                    SMB_ACL_TYPE_T type)
360 {
361         bool ret = False;
362         int i;
363
364         DEBUG(10, ("entering smb_acl_to_solaris_acl\n"));
365         
366         *solaris_acl = NULL;
367         *count = 0;
368
369         for (i = 0; i < smb_acl->count; i++) {
370                 const struct smb_acl_entry *smb_entry = &(smb_acl->acl[i]);
371                 SOLARIS_ACE_T solaris_entry;
372
373                 ZERO_STRUCT(solaris_entry);
374
375                 solaris_entry.a_type = smb_tag_to_solaris_tag(smb_entry->a_type);
376                 if (solaris_entry.a_type == 0) {
377                         DEBUG(10, ("smb_tag to solaris_tag failed\n"));
378                         goto fail;
379                 }
380                 switch(solaris_entry.a_type) {
381                 case USER:
382                         DEBUG(10, ("got tag type USER with uid %u\n", 
383                                    (unsigned int)smb_entry->info.user.uid));
384                         solaris_entry.a_id = (uid_t)smb_entry->info.user.uid;
385                         break;
386                 case GROUP:
387                         DEBUG(10, ("got tag type GROUP with gid %u\n", 
388                                    (unsigned int)smb_entry->info.group.gid));
389                         solaris_entry.a_id = (uid_t)smb_entry->info.group.gid;
390                         break;
391                 default:
392                         break;
393                 }
394                 if (type == SMB_ACL_TYPE_DEFAULT) {
395                         DEBUG(10, ("adding default bit to solaris ace\n"));
396                         solaris_entry.a_type |= ACL_DEFAULT;
397                 }
398                 
399                 solaris_entry.a_perm = 
400                         smb_perm_to_solaris_perm(smb_entry->a_perm);
401                 DEBUG(10, ("assembled the following solaris ace:\n"));
402                 DEBUGADD(10, (" - type: 0x%04x\n", solaris_entry.a_type));
403                 DEBUGADD(10, (" - id: %u\n", (unsigned int)solaris_entry.a_id));
404                 DEBUGADD(10, (" - perm: o%o\n", solaris_entry.a_perm));
405                 if (!solaris_add_to_acl(solaris_acl, count, &solaris_entry, 
406                                         1, type))
407                 {
408                         DEBUG(10, ("error adding acl entry\n"));
409                         goto fail;
410                 }
411                 DEBUG(10, ("count after adding: %d (i: %d)\n", *count, i));
412                 DEBUG(10, ("test, if entry has been copied into acl:\n"));
413                 DEBUGADD(10, (" - type: 0x%04x\n",
414                               (*solaris_acl)[(*count)-1].a_type));
415                 DEBUGADD(10, (" - id: %u\n",
416                               (unsigned int)(*solaris_acl)[(*count)-1].a_id));
417                 DEBUGADD(10, (" - perm: o%o\n",
418                               (*solaris_acl)[(*count)-1].a_perm));
419         }
420
421         ret = True;
422         goto done;
423         
424  fail:
425         SAFE_FREE(*solaris_acl);
426  done:
427         DEBUG(10, ("smb_acl_to_solaris_acl %s\n",
428                    ((ret == True) ? "succeeded" : "failed")));
429         return ret;
430 }
431
432 /* 
433  * convert either the access or the default part of a 
434  * soaris acl to the SMB_ACL format.
435  */
436 static SMB_ACL_T solaris_acl_to_smb_acl(SOLARIS_ACL_T solaris_acl, int count, 
437                                         SMB_ACL_TYPE_T type, TALLOC_CTX *mem_ctx)
438 {
439         SMB_ACL_T result;
440         int i;
441
442         if ((result = sys_acl_init(mem_ctx)) == NULL) {
443                 DEBUG(10, ("error allocating memory for SMB_ACL\n"));
444                 goto fail;
445         }
446         for (i = 0; i < count; i++) {
447                 SMB_ACL_ENTRY_T smb_entry;
448                 SMB_ACL_PERM_T smb_perm;
449                 
450                 if (!_IS_OF_TYPE(solaris_acl[i], type)) {
451                         continue;
452                 }
453                 result->acl = talloc_realloc(result, result->acl, struct smb_acl_entry, result->count + 1);
454                 if (result->acl == NULL) {
455                         DEBUG(10, ("error reallocating memory for SMB_ACL\n"));
456                         goto fail;
457                 }
458                 smb_entry = &result->acl[result->count];
459                 if (sys_acl_set_tag_type(smb_entry,
460                                          solaris_tag_to_smb_tag(solaris_acl[i].a_type)) != 0)
461                 {
462                         DEBUG(10, ("invalid tag type given: 0x%04x\n",
463                                    solaris_acl[i].a_type));
464                         goto fail;
465                 }
466                 /* intentionally not checking return code here: */
467                 sys_acl_set_qualifier(smb_entry, (void *)&solaris_acl[i].a_id);
468                 smb_perm = solaris_perm_to_smb_perm(solaris_acl[i].a_perm);
469                 if (sys_acl_set_permset(smb_entry, &smb_perm) != 0) {
470                         DEBUG(10, ("invalid permset given: %d\n", 
471                                    solaris_acl[i].a_perm));
472                         goto fail;
473                 }
474                 result->count += 1;
475         }
476         goto done;
477         
478  fail:
479         TALLOC_FREE(result);
480  done:
481         DEBUG(10, ("solaris_acl_to_smb_acl %s\n",
482                    ((result == NULL) ? "failed" : "succeeded")));
483         return result;
484 }
485
486
487
488 static SOLARIS_ACL_TAG_T smb_tag_to_solaris_tag(SMB_ACL_TAG_T smb_tag)
489 {
490         SOLARIS_ACL_TAG_T solaris_tag = 0;
491
492         DEBUG(10, ("smb_tag_to_solaris_tag\n"));
493         DEBUGADD(10, (" --> got smb tag 0x%04x\n", smb_tag));
494         
495         switch (smb_tag) {
496         case SMB_ACL_USER:
497                 solaris_tag = USER;
498                 break;
499         case SMB_ACL_USER_OBJ:
500                 solaris_tag = USER_OBJ;
501                 break;
502         case SMB_ACL_GROUP:
503                 solaris_tag = GROUP;
504                 break;
505         case SMB_ACL_GROUP_OBJ:
506                 solaris_tag = GROUP_OBJ;
507                 break;
508         case SMB_ACL_OTHER:
509                 solaris_tag = OTHER_OBJ;
510                 break;
511         case SMB_ACL_MASK:
512                 solaris_tag = CLASS_OBJ;
513                 break;
514         default:
515                 DEBUGADD(10, (" !!! unknown smb tag type 0x%04x\n", smb_tag));
516                 break;
517         }
518         
519         DEBUGADD(10, (" --> determined solaris tag 0x%04x\n", solaris_tag));
520
521         return solaris_tag;
522 }
523
524 static SMB_ACL_TAG_T solaris_tag_to_smb_tag(SOLARIS_ACL_TAG_T solaris_tag)
525 {
526         SMB_ACL_TAG_T smb_tag = 0;
527
528         DEBUG(10, ("solaris_tag_to_smb_tag:\n"));
529         DEBUGADD(10, (" --> got solaris tag 0x%04x\n", solaris_tag)); 
530         
531         solaris_tag &= ~ACL_DEFAULT; 
532
533         switch (solaris_tag) {
534         case USER:
535                 smb_tag = SMB_ACL_USER;
536                 break;
537         case USER_OBJ:
538                 smb_tag = SMB_ACL_USER_OBJ;
539                 break;
540         case GROUP:
541                 smb_tag = SMB_ACL_GROUP;
542                 break;
543         case GROUP_OBJ:
544                 smb_tag = SMB_ACL_GROUP_OBJ;
545                 break;
546         case OTHER_OBJ:
547                 smb_tag = SMB_ACL_OTHER;
548                 break;
549         case CLASS_OBJ:
550                 smb_tag = SMB_ACL_MASK;
551                 break;
552         default:
553                 DEBUGADD(10, (" !!! unknown solaris tag type: 0x%04x\n", 
554                                         solaris_tag));
555                 break;
556         }
557
558         DEBUGADD(10, (" --> determined smb tag 0x%04x\n", smb_tag));
559         
560         return smb_tag;
561 }
562
563
564 static SMB_ACL_PERM_T solaris_perm_to_smb_perm(const SOLARIS_PERM_T perm)
565 {
566         SMB_ACL_PERM_T smb_perm = 0;
567         smb_perm |= ((perm & SMB_ACL_READ) ? SMB_ACL_READ : 0);
568         smb_perm |= ((perm & SMB_ACL_WRITE) ? SMB_ACL_WRITE : 0);
569         smb_perm |= ((perm & SMB_ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
570         return smb_perm;
571 }
572
573
574 static SOLARIS_PERM_T smb_perm_to_solaris_perm(const SMB_ACL_PERM_T perm)
575 {
576         SOLARIS_PERM_T solaris_perm = 0;
577         solaris_perm |= ((perm & SMB_ACL_READ) ? SMB_ACL_READ : 0);
578         solaris_perm |= ((perm & SMB_ACL_WRITE) ? SMB_ACL_WRITE : 0);
579         solaris_perm |= ((perm & SMB_ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
580         return solaris_perm;
581 }
582
583
584 static bool solaris_acl_get_file(const char *name, SOLARIS_ACL_T *solaris_acl, 
585                                  int *count)
586 {
587         bool result = False;
588
589         DEBUG(10, ("solaris_acl_get_file called for file '%s'\n", name));
590         
591         /* 
592          * The original code tries some INITIAL_ACL_SIZE
593          * and only did the GETACLCNT call upon failure
594          * (for performance reasons).
595          * For the sake of simplicity, I skip this for now. 
596          */
597         *count = acl(name, GETACLCNT, 0, NULL);
598         if (*count < 0) {
599                 DEBUG(10, ("acl GETACLCNT failed: %s\n", strerror(errno)));
600                 goto done;
601         }
602         *solaris_acl = solaris_acl_init(*count);
603         if (*solaris_acl == NULL) {
604                 DEBUG(10, ("error allocating memory for solaris acl...\n"));
605                 goto done;
606         }
607         *count = acl(name, GETACL, *count, *solaris_acl);
608         if (*count < 0) {
609                 DEBUG(10, ("acl GETACL failed: %s\n", strerror(errno)));
610                 goto done;
611         }
612         result = True;
613
614  done:
615         DEBUG(10, ("solaris_acl_get_file %s.\n",
616                    ((result == True) ? "succeeded" : "failed" )));
617         return result;
618 }
619
620
621 static bool solaris_acl_get_fd(int fd, SOLARIS_ACL_T *solaris_acl, int *count)
622 {
623         bool ret = False;
624
625         DEBUG(10, ("entering solaris_acl_get_fd\n"));
626
627         /* 
628          * see solaris_acl_get_file for comment about omission 
629          * of INITIAL_ACL_SIZE... 
630          */
631         *count = facl(fd, GETACLCNT, 0, NULL);
632         if (*count < 0) {
633                 DEBUG(10, ("facl GETACLCNT failed: %s\n", strerror(errno)));
634                 goto done;
635         }
636         *solaris_acl = solaris_acl_init(*count);
637         if (*solaris_acl == NULL) {
638                 DEBUG(10, ("error allocating memory for solaris acl...\n"));
639                 goto done;
640         }
641         *count = facl(fd, GETACL, *count, *solaris_acl);
642         if (*count < 0) {
643                 DEBUG(10, ("facl GETACL failed: %s\n", strerror(errno)));
644                 goto done;
645         }
646         ret = True;
647
648  done:
649         DEBUG(10, ("solaris_acl_get_fd %s\n",
650                    ((ret == True) ? "succeeded" : "failed")));
651         return ret;
652 }
653
654
655
656 /*
657  * Add entries to a solaris ACL.
658  *
659  * Entries are directly added to the solarisacl parameter.
660  * if memory allocation fails, this may result in solarisacl 
661  * being NULL. if the resulting acl is to be checked and is 
662  * not valid, it is kept in solarisacl but False is returned.
663  *
664  * The type of ACEs (access/default) to be added to the ACL can 
665  * be selected via the type parameter. 
666  * I use the SMB_ACL_TYPE_T type here. Since SMB_ACL_TYPE_ACCESS
667  * is defined as "0", this means that one can only add either
668  * access or default ACEs, not both at the same time. If it 
669  * should become necessary to add all of an ACL, one would have
670  * to replace this parameter by another type.
671  */
672 static bool solaris_add_to_acl(SOLARIS_ACL_T *solaris_acl, int *count,
673                                SOLARIS_ACL_T add_acl, int add_count, 
674                                SMB_ACL_TYPE_T type)
675 {
676         int i;
677         
678         if ((type != SMB_ACL_TYPE_ACCESS) && (type != SMB_ACL_TYPE_DEFAULT)) 
679         {
680                 DEBUG(10, ("invalid acl type given: %d\n", type));
681                 errno = EINVAL;
682                 return False;
683         }
684         for (i = 0; i < add_count; i++) {
685                 if (!_IS_OF_TYPE(add_acl[i], type)) {
686                         continue;
687                 }
688                 ADD_TO_ARRAY(NULL, SOLARIS_ACE_T, add_acl[i], 
689                              solaris_acl, count);
690                 if (solaris_acl == NULL) {
691                         DEBUG(10, ("error enlarging acl.\n"));
692                         errno = ENOMEM;
693                         return False;
694                 }
695         }
696         return True;
697 }
698
699
700 /* 
701  * sort the ACL and check it for validity
702  *
703  * [original comment from lib/sysacls.c:]
704  * 
705  * if it's a minimal ACL with only 4 entries then we
706  * need to recalculate the mask permissions to make
707  * sure that they are the same as the GROUP_OBJ
708  * permissions as required by the UnixWare acl() system call.
709  *
710  * (note: since POSIX allows minimal ACLs which only contain
711  * 3 entries - ie there is no mask entry - we should, in theory,
712  * check for this and add a mask entry if necessary - however
713  * we "know" that the caller of this interface always specifies
714  * a mask, so in practice "this never happens" (tm) - if it *does*
715  * happen aclsort() will fail and return an error and someone will
716  * have to fix it...)
717  */
718 static bool solaris_acl_sort(SOLARIS_ACL_T solaris_acl, int count)
719 {
720         int fixmask = (count <= 4);
721
722         if (aclsort(count, fixmask, solaris_acl) != 0) {
723                 errno = EINVAL;
724                 return False;
725         }
726         return True;
727 }
728
729 #if 0
730 /*
731  * acl check function:
732  *   unused at the moment but could be used to get more
733  *   concrete error messages for debugging...
734  *   (acl sort just says that the acl is invalid...)
735  */
736 static bool solaris_acl_check(SOLARIS_ACL_T solaris_acl, int count)
737 {
738         int check_rc;
739         int check_which;
740         
741         check_rc = aclcheck(solaris_acl, count, &check_which);
742         if (check_rc != 0) {
743                 DEBUG(10, ("acl is not valid:\n"));
744                 DEBUGADD(10, (" - return code: %d\n", check_rc));
745                 DEBUGADD(10, (" - which: %d\n", check_which));
746                 if (check_which != -1) {
747                         DEBUGADD(10, (" - invalid entry:\n"));
748                         DEBUGADD(10, ("   * type: %d:\n", 
749                                       solaris_acl[check_which].a_type));
750                         DEBUGADD(10, ("   * id: %d\n",
751                                       solaris_acl[check_which].a_id));
752                         DEBUGADD(10, ("   * perm: 0o%o\n",
753                                       solaris_acl[check_which].a_perm));
754                 }
755                 return False;
756         }
757         return True;
758 }
759 #endif
760
761 static struct vfs_fn_pointers solarisacl_fns = {
762         .sys_acl_get_file_fn = solarisacl_sys_acl_get_file,
763         .sys_acl_get_fd_fn = solarisacl_sys_acl_get_fd,
764         .sys_acl_blob_get_file_fn = posix_sys_acl_blob_get_file,
765         .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
766         .sys_acl_set_file_fn = solarisacl_sys_acl_set_file,
767         .sys_acl_set_fd_fn = solarisacl_sys_acl_set_fd,
768         .sys_acl_delete_def_file_fn = solarisacl_sys_acl_delete_def_file,
769 };
770
771 NTSTATUS vfs_solarisacl_init(TALLOC_CTX *);
772 NTSTATUS vfs_solarisacl_init(TALLOC_CTX *ctx)
773 {
774         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "solarisacl",
775                                 &solarisacl_fns);
776 }
777
778 /* ENTE */