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