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