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