r23801: The FSF has moved around a lot. This fixes their Mass Ave address.
[samba.git] / source3 / modules / vfs_hpuxacl.c
1 /*
2  * Unix SMB/Netbios implementation.
3  * VFS module to get and set HP-UX 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  * This module supports JFS (POSIX) ACLs on VxFS (Veritas * Filesystem).
22  * These are available on HP-UX 11.00 if JFS 3.3 is installed. 
23  * On HP-UX 11i (11.11 and above) these ACLs are supported out of
24  * the box.
25  *
26  * There is another form of ACLs on HFS. These ACLs have a
27  * completely different API and their own set of userland tools.
28  * Since HFS seems to be considered deprecated, HFS acls
29  * are not supported. (They could be supported through a separate
30  * vfs-module if there is demand.)
31  */
32
33 /* =================================================================
34  * NOTE:
35  *
36  * The original hpux-acl code in lib/sysacls.c was based upon the
37  * solaris acl code in the same file. Now for the new modularized
38  * acl implementation, I have taken the code from vfs_solarisacls.c
39  * and did similar adaptations as were done before, essentially
40  * reusing the original internal aclsort functions.
41  * The check for the presence of the acl() call has been adopted, and
42  * a check for the presence of the aclsort() call has been added. 
43  * 
44  * Michael Adam <obnox@samba.org>
45  *
46  * ================================================================= */
47
48
49 #include "includes.h"
50
51 /* 
52  * including standard header <sys/aclv.h> 
53  *
54  * included here as a quick hack for the special HP-UX-situation:
55  *
56  * The problem is that, on HP-UX, jfs/posix acls are
57  * defined in <sys/aclv.h>, while the deprecated hfs acls 
58  * are defined inside <sys/acl.h>.
59  *
60  */
61 /* GROUP is defined somewhere else so undef it here... */
62 #undef GROUP
63 #include <sys/aclv.h>
64 /* dl.h: needed to check for acl call via shl_findsym */
65 #include <dl.h>
66
67 typedef struct acl HPUX_ACE_T;
68 typedef struct acl *HPUX_ACL_T;
69 typedef int HPUX_ACL_TAG_T;   /* the type of an ACL entry */
70 typedef ushort HPUX_PERM_T;
71
72 /* Structure to capture the count for each type of ACE. 
73  * (for hpux_internal_aclsort */
74 struct hpux_acl_types {
75         int n_user;
76         int n_def_user;
77         int n_user_obj;
78         int n_def_user_obj;
79
80         int n_group;
81         int n_def_group;
82         int n_group_obj;
83         int n_def_group_obj;
84
85         int n_other;
86         int n_other_obj;
87         int n_def_other_obj;
88
89         int n_class_obj;
90         int n_def_class_obj;
91
92         int n_illegal_obj;
93 };
94
95 /* for convenience: check if hpux acl entry is a default entry?  */
96 #define _IS_DEFAULT(ace) ((ace).a_type & ACL_DEFAULT)
97 #define _IS_OF_TYPE(ace, type) ( \
98         (((type) == SMB_ACL_TYPE_ACCESS) && !_IS_DEFAULT(ace)) \
99         || \
100         (((type) == SMB_ACL_TYPE_DEFAULT) && _IS_DEFAULT(ace)) \
101 )
102
103
104 /* prototypes for private functions */
105
106 static HPUX_ACL_T hpux_acl_init(int count);
107 static BOOL smb_acl_to_hpux_acl(SMB_ACL_T smb_acl, 
108                 HPUX_ACL_T *solariacl, int *count, 
109                 SMB_ACL_TYPE_T type);
110 static SMB_ACL_T hpux_acl_to_smb_acl(HPUX_ACL_T hpuxacl, int count,
111                 SMB_ACL_TYPE_T type);
112 static HPUX_ACL_TAG_T smb_tag_to_hpux_tag(SMB_ACL_TAG_T smb_tag);
113 static SMB_ACL_TAG_T hpux_tag_to_smb_tag(HPUX_ACL_TAG_T hpux_tag);
114 static BOOL hpux_add_to_acl(HPUX_ACL_T *hpux_acl, int *count,
115                 HPUX_ACL_T add_acl, int add_count, SMB_ACL_TYPE_T type);
116 static BOOL hpux_acl_get_file(const char *name, HPUX_ACL_T *hpuxacl, 
117                 int *count);
118 static SMB_ACL_PERM_T hpux_perm_to_smb_perm(const HPUX_PERM_T perm);
119 static HPUX_PERM_T smb_perm_to_hpux_perm(const SMB_ACL_PERM_T perm);
120 #if 0
121 static BOOL hpux_acl_check(HPUX_ACL_T hpux_acl, int count);
122 #endif
123 /* aclsort (internal) and helpers: */
124 static BOOL hpux_acl_sort(HPUX_ACL_T acl, int count);
125 static int hpux_internal_aclsort(int acl_count, int calclass, HPUX_ACL_T aclp);
126 static void hpux_count_obj(int acl_count, HPUX_ACL_T aclp, 
127                 struct hpux_acl_types *acl_type_count);
128 static void hpux_swap_acl_entries(HPUX_ACE_T *aclp0, HPUX_ACE_T *aclp1);
129 static BOOL hpux_prohibited_duplicate_type(int acl_type);
130
131 static BOOL hpux_acl_call_present(void);
132 static BOOL hpux_aclsort_call_present(void);
133
134
135 /* public functions - the api */
136
137 SMB_ACL_T hpuxacl_sys_acl_get_file(vfs_handle_struct *handle,
138                                       const char *path_p,
139                                       SMB_ACL_TYPE_T type)
140 {
141         SMB_ACL_T result = NULL;
142         int count;
143         HPUX_ACL_T hpux_acl;
144         
145         DEBUG(10, ("hpuxacl_sys_acl_get_file called for file '%s'.\n", 
146                    path_p));
147
148         if(hpux_acl_call_present() == False) {
149                 /* Looks like we don't have the acl() system call on HPUX. 
150                  * May be the system doesn't have the latest version of JFS.
151                  */
152                 goto done;
153         }
154
155         if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {
156                 DEBUG(10, ("invalid SMB_ACL_TYPE given (%d)\n", type));
157                 errno = EINVAL;
158                 goto done;
159         }
160
161         DEBUGADD(10, ("getting %s acl\n", 
162                       ((type == SMB_ACL_TYPE_ACCESS) ? "access" : "default")));
163
164         if (!hpux_acl_get_file(path_p, &hpux_acl, &count)) {
165                 goto done;
166         }
167         result = hpux_acl_to_smb_acl(hpux_acl, count, type);
168         if (result == NULL) {
169                 DEBUG(10, ("conversion hpux_acl -> smb_acl failed (%s).\n",
170                            strerror(errno)));
171         }
172         
173  done:
174         DEBUG(10, ("hpuxacl_sys_acl_get_file %s.\n",
175                    ((result == NULL) ? "failed" : "succeeded" )));
176         SAFE_FREE(hpux_acl);
177         return result;
178 }
179
180
181 /*
182  * get the access ACL of a file referred to by a fd
183  */
184 SMB_ACL_T hpuxacl_sys_acl_get_fd(vfs_handle_struct *handle,
185                                  files_struct *fsp,
186                                  int fd)
187 {
188         /* 
189          * HPUX doesn't have the facl call. Fake it using the path.... JRA. 
190          */
191         /* For all I see, the info should already be in the fsp
192          * parameter, but get it again to be safe --- necessary? */
193         files_struct *file_struct_p = file_find_fd(fd);
194         if (file_struct_p == NULL) {
195                 errno = EBADF;
196                 return NULL;
197         }
198         /*
199          * We know we're in the same conn context. So we
200          * can use the relative path.
201          */
202         DEBUG(10, ("redirecting call of hpuxacl_sys_acl_get_fd to "
203                 "hpuxacl_sys_acl_get_file (no facl syscall on HPUX).\n"));
204
205         return hpuxacl_sys_acl_get_file(handle, file_struct_p->fsp_name, 
206                         SMB_ACL_TYPE_ACCESS);
207 }
208
209
210 int hpuxacl_sys_acl_set_file(vfs_handle_struct *handle,
211                              const char *name,
212                              SMB_ACL_TYPE_T type,
213                              SMB_ACL_T theacl)
214 {
215         int ret = -1;
216         SMB_STRUCT_STAT s;
217         HPUX_ACL_T hpux_acl;
218         int count;
219         
220         DEBUG(10, ("hpuxacl_sys_acl_set_file called for file '%s'\n",
221                    name));
222
223
224         if(hpux_acl_call_present() == False) {
225                 /* Looks like we don't have the acl() system call on HPUX. 
226                  * May be the system doesn't have the latest version of JFS.
227                  */
228                 goto done;
229         }
230
231         if ((type != SMB_ACL_TYPE_ACCESS) && (type != SMB_ACL_TYPE_DEFAULT)) {
232                 errno = EINVAL;
233                 DEBUG(10, ("invalid smb acl type given (%d).\n", type));
234                 goto done;
235         }
236         DEBUGADD(10, ("setting %s acl\n", 
237                       ((type == SMB_ACL_TYPE_ACCESS) ? "access" : "default")));
238
239         if(!smb_acl_to_hpux_acl(theacl, &hpux_acl, &count, type)) {
240                 DEBUG(10, ("conversion smb_acl -> hpux_acl failed (%s).\n",
241                            strerror(errno)));
242                 goto done;
243         }
244
245         /*
246          * if the file is a directory, there is extra work to do:
247          * since the hpux acl call stores both the access acl and 
248          * the default acl as provided, we have to get the acl part 
249          * that has _not_ been specified in "type" from the file first 
250          * and concatenate it with the acl provided.
251          */
252         if (SMB_VFS_STAT(handle->conn, name, &s) != 0) {
253                 DEBUG(10, ("Error in stat call: %s\n", strerror(errno)));
254                 goto done;
255         }
256         if (S_ISDIR(s.st_mode)) {
257                 HPUX_ACL_T other_acl; 
258                 int other_count;
259                 SMB_ACL_TYPE_T other_type;
260
261                 other_type = (type == SMB_ACL_TYPE_ACCESS) 
262                         ? SMB_ACL_TYPE_DEFAULT
263                         : SMB_ACL_TYPE_ACCESS;
264                 DEBUGADD(10, ("getting acl from filesystem\n"));
265                 if (!hpux_acl_get_file(name, &other_acl, &other_count)) {
266                         DEBUG(10, ("error getting acl from directory\n"));
267                         goto done;
268                 }
269                 DEBUG(10, ("adding %s part of fs acl to given acl\n",
270                            ((other_type == SMB_ACL_TYPE_ACCESS) 
271                             ? "access"
272                             : "default")));
273                 if (!hpux_add_to_acl(&hpux_acl, &count, other_acl,
274                                         other_count, other_type)) 
275                 {
276                         DEBUG(10, ("error adding other acl.\n"));
277                         SAFE_FREE(other_acl);
278                         goto done;
279                 }
280                 SAFE_FREE(other_acl);
281         }
282         else if (type != SMB_ACL_TYPE_ACCESS) {
283                 errno = EINVAL;
284                 goto done;
285         }
286
287         if (!hpux_acl_sort(hpux_acl, count)) {
288                 DEBUG(10, ("resulting acl is not valid!\n"));
289                 goto done;
290         }
291         DEBUG(10, ("resulting acl is valid.\n"));
292
293         ret = acl(CONST_DISCARD(char *, name), ACL_SET, count, hpux_acl);
294         if (ret != 0) {
295                 DEBUG(0, ("ERROR calling acl: %s\n", strerror(errno)));
296         }
297         
298  done:
299         DEBUG(10, ("hpuxacl_sys_acl_set_file %s.\n",
300                    ((ret != 0) ? "failed" : "succeeded")));
301         SAFE_FREE(hpux_acl);
302         return ret;
303 }
304
305 /*
306  * set the access ACL on the file referred to by a fd 
307  */
308 int hpuxacl_sys_acl_set_fd(vfs_handle_struct *handle,
309                               files_struct *fsp,
310                               int fd, SMB_ACL_T theacl)
311 {
312         /*
313          * HPUX doesn't have the facl call. Fake it using the path.... JRA.
314          */
315         /* For all I see, the info should already be in the fsp
316          * parameter, but get it again to be safe --- necessary? */
317         files_struct *file_struct_p = file_find_fd(fd);
318         if (file_struct_p == NULL) {
319                 errno = EBADF;
320                 return -1;
321         }
322         /*
323          * We know we're in the same conn context. So we
324          * can use the relative path.
325          */
326         DEBUG(10, ("redirecting call of hpuxacl_sys_acl_set_fd to "
327                 "hpuxacl_sys_acl_set_file (no facl syscall on HPUX)\n"));
328
329         return hpuxacl_sys_acl_set_file(handle, file_struct_p->fsp_name,
330                         SMB_ACL_TYPE_ACCESS, theacl);
331 }
332
333
334 /*
335  * delete the default ACL of a directory
336  *
337  * This is achieved by fetching the access ACL and rewriting it 
338  * directly, via the hpux system call: the ACL_SET call on 
339  * directories writes both the access and the default ACL as provided.
340  *
341  * XXX: posix acl_delete_def_file returns an error if
342  * the file referred to by path is not a directory.
343  * this function does not complain but the actions 
344  * have no effect on a file other than a directory.
345  * But sys_acl_delete_default_file is only called in
346  * smbd/posixacls.c after having checked that the file
347  * is a directory, anyways. So implementing the extra
348  * check is considered unnecessary. --- Agreed? XXX
349  */
350 int hpuxacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
351                                     const char *path)
352 {
353         SMB_ACL_T smb_acl;
354         int ret = -1;
355         HPUX_ACL_T hpux_acl;
356         int count;
357
358         DEBUG(10, ("entering hpuxacl_sys_acl_delete_def_file.\n"));
359         
360         smb_acl = hpuxacl_sys_acl_get_file(handle, path, 
361                                            SMB_ACL_TYPE_ACCESS);
362         if (smb_acl == NULL) {
363                 DEBUG(10, ("getting file acl failed!\n"));
364                 goto done;
365         }
366         if (!smb_acl_to_hpux_acl(smb_acl, &hpux_acl, &count, 
367                                  SMB_ACL_TYPE_ACCESS))
368         {
369                 DEBUG(10, ("conversion smb_acl -> hpux_acl failed.\n"));
370                 goto done;
371         }
372         if (!hpux_acl_sort(hpux_acl, count)) {
373                 DEBUG(10, ("resulting acl is not valid!\n"));
374                 goto done;
375         }
376         ret = acl(CONST_DISCARD(char *, path), ACL_SET, count, hpux_acl);
377         if (ret != 0) {
378                 DEBUG(10, ("settinge file acl failed!\n"));
379         }
380         
381  done:
382         DEBUG(10, ("hpuxacl_sys_acl_delete_def_file %s.\n",
383                    ((ret != 0) ? "failed" : "succeeded" )));
384         SAFE_FREE(smb_acl);
385         return ret;
386 }
387
388
389 /* 
390  * private functions 
391  */
392
393 static HPUX_ACL_T hpux_acl_init(int count)
394 {
395         HPUX_ACL_T hpux_acl = 
396                 (HPUX_ACL_T)SMB_MALLOC(sizeof(HPUX_ACE_T) * count);
397         if (hpux_acl == NULL) {
398                 errno = ENOMEM;
399         }
400         return hpux_acl;
401 }
402
403 /*
404  * Convert the SMB acl to the ACCESS or DEFAULT part of a 
405  * hpux ACL, as desired.
406  */
407 static BOOL smb_acl_to_hpux_acl(SMB_ACL_T smb_acl, 
408                                    HPUX_ACL_T *hpux_acl, int *count, 
409                                    SMB_ACL_TYPE_T type)
410 {
411         BOOL ret = False;
412         int i;
413         int check_which, check_rc;
414
415         DEBUG(10, ("entering smb_acl_to_hpux_acl\n"));
416         
417         *hpux_acl = NULL;
418         *count = 0;
419
420         for (i = 0; i < smb_acl->count; i++) {
421                 const struct smb_acl_entry *smb_entry = &(smb_acl->acl[i]);
422                 HPUX_ACE_T hpux_entry;
423
424                 ZERO_STRUCT(hpux_entry);
425
426                 hpux_entry.a_type = smb_tag_to_hpux_tag(smb_entry->a_type);
427                 if (hpux_entry.a_type == 0) {
428                         DEBUG(10, ("smb_tag to hpux_tag failed\n"));
429                         goto fail;
430                 }
431                 switch(hpux_entry.a_type) {
432                 case USER:
433                         DEBUG(10, ("got tag type USER with uid %d\n", 
434                                    smb_entry->uid));
435                         hpux_entry.a_id = (uid_t)smb_entry->uid;
436                         break;
437                 case GROUP:
438                         DEBUG(10, ("got tag type GROUP with gid %d\n", 
439                                    smb_entry->gid));
440                         hpux_entry.a_id = (uid_t)smb_entry->gid;
441                         break;
442                 default:
443                         break;
444                 }
445                 if (type == SMB_ACL_TYPE_DEFAULT) {
446                         DEBUG(10, ("adding default bit to hpux ace\n"));
447                         hpux_entry.a_type |= ACL_DEFAULT;
448                 }
449                 
450                 hpux_entry.a_perm = 
451                         smb_perm_to_hpux_perm(smb_entry->a_perm);
452                 DEBUG(10, ("assembled the following hpux ace:\n"));
453                 DEBUGADD(10, (" - type: 0x%04x\n", hpux_entry.a_type));
454                 DEBUGADD(10, (" - id: %d\n", hpux_entry.a_id));
455                 DEBUGADD(10, (" - perm: o%o\n", hpux_entry.a_perm));
456                 if (!hpux_add_to_acl(hpux_acl, count, &hpux_entry, 
457                                         1, type))
458                 {
459                         DEBUG(10, ("error adding acl entry\n"));
460                         goto fail;
461                 }
462                 DEBUG(10, ("count after adding: %d (i: %d)\n", *count, i));
463                 DEBUG(10, ("test, if entry has been copied into acl:\n"));
464                 DEBUGADD(10, (" - type: 0x%04x\n",
465                               (*hpux_acl)[(*count)-1].a_type));
466                 DEBUGADD(10, (" - id: %d\n",
467                               (*hpux_acl)[(*count)-1].a_id));
468                 DEBUGADD(10, (" - perm: o%o\n",
469                               (*hpux_acl)[(*count)-1].a_perm));
470         }
471
472         ret = True;
473         goto done;
474         
475  fail:
476         SAFE_FREE(*hpux_acl);
477  done:
478         DEBUG(10, ("smb_acl_to_hpux_acl %s\n",
479                    ((ret == True) ? "succeeded" : "failed")));
480         return ret;
481 }
482
483 /* 
484  * convert either the access or the default part of a 
485  * soaris acl to the SMB_ACL format.
486  */
487 static SMB_ACL_T hpux_acl_to_smb_acl(HPUX_ACL_T hpux_acl, int count, 
488                                         SMB_ACL_TYPE_T type)
489 {
490         SMB_ACL_T result;
491         int i;
492
493         if ((result = sys_acl_init(0)) == NULL) {
494                 DEBUG(10, ("error allocating memory for SMB_ACL\n"));
495                 goto fail;
496         }
497         for (i = 0; i < count; i++) {
498                 SMB_ACL_ENTRY_T smb_entry;
499                 SMB_ACL_PERM_T smb_perm;
500                 
501                 if (!_IS_OF_TYPE(hpux_acl[i], type)) {
502                         continue;
503                 }
504                 result = SMB_REALLOC(result, 
505                                      sizeof(struct smb_acl_t) +
506                                      (sizeof(struct smb_acl_entry) *
507                                       (result->count + 1)));
508                 if (result == NULL) {
509                         DEBUG(10, ("error reallocating memory for SMB_ACL\n"));
510                         goto fail;
511                 }
512                 smb_entry = &result->acl[result->count];
513                 if (sys_acl_set_tag_type(smb_entry,
514                                          hpux_tag_to_smb_tag(hpux_acl[i].a_type)) != 0)
515                 {
516                         DEBUG(10, ("invalid tag type given: 0x%04x\n",
517                                    hpux_acl[i].a_type));
518                         goto fail;
519                 }
520                 /* intentionally not checking return code here: */
521                 sys_acl_set_qualifier(smb_entry, (void *)&hpux_acl[i].a_id);
522                 smb_perm = hpux_perm_to_smb_perm(hpux_acl[i].a_perm);
523                 if (sys_acl_set_permset(smb_entry, &smb_perm) != 0) {
524                         DEBUG(10, ("invalid permset given: %d\n", 
525                                    hpux_acl[i].a_perm));
526                         goto fail;
527                 }
528                 result->count += 1;
529         }
530         goto done;
531         
532  fail:
533         SAFE_FREE(result);
534  done:
535         DEBUG(10, ("hpux_acl_to_smb_acl %s\n",
536                    ((result == NULL) ? "failed" : "succeeded")));
537         return result;
538 }
539
540
541
542 static HPUX_ACL_TAG_T smb_tag_to_hpux_tag(SMB_ACL_TAG_T smb_tag)
543 {
544         HPUX_ACL_TAG_T hpux_tag = 0;
545
546         DEBUG(10, ("smb_tag_to_hpux_tag\n"));
547         DEBUGADD(10, (" --> got smb tag 0x%04x\n", smb_tag));
548         
549         switch (smb_tag) {
550         case SMB_ACL_USER:
551                 hpux_tag = USER;
552                 break;
553         case SMB_ACL_USER_OBJ:
554                 hpux_tag = USER_OBJ;
555                 break;
556         case SMB_ACL_GROUP:
557                 hpux_tag = GROUP;
558                 break;
559         case SMB_ACL_GROUP_OBJ:
560                 hpux_tag = GROUP_OBJ;
561                 break;
562         case SMB_ACL_OTHER:
563                 hpux_tag = OTHER_OBJ;
564                 break;
565         case SMB_ACL_MASK:
566                 hpux_tag = CLASS_OBJ;
567                 break;
568         default:
569                 DEBUGADD(10, (" !!! unknown smb tag type 0x%04x\n", smb_tag));
570                 break;
571         }
572         
573         DEBUGADD(10, (" --> determined hpux tag 0x%04x\n", hpux_tag));
574
575         return hpux_tag;
576 }
577
578 static SMB_ACL_TAG_T hpux_tag_to_smb_tag(HPUX_ACL_TAG_T hpux_tag)
579 {
580         SMB_ACL_TAG_T smb_tag = 0;
581
582         DEBUG(10, ("hpux_tag_to_smb_tag:\n"));
583         DEBUGADD(10, (" --> got hpux tag 0x%04x\n", hpux_tag)); 
584         
585         hpux_tag &= ~ACL_DEFAULT; 
586
587         switch (hpux_tag) {
588         case USER:
589                 smb_tag = SMB_ACL_USER;
590                 break;
591         case USER_OBJ:
592                 smb_tag = SMB_ACL_USER_OBJ;
593                 break;
594         case GROUP:
595                 smb_tag = SMB_ACL_GROUP;
596                 break;
597         case GROUP_OBJ:
598                 smb_tag = SMB_ACL_GROUP_OBJ;
599                 break;
600         case OTHER_OBJ:
601                 smb_tag = SMB_ACL_OTHER;
602                 break;
603         case CLASS_OBJ:
604                 smb_tag = SMB_ACL_MASK;
605                 break;
606         default:
607                 DEBUGADD(10, (" !!! unknown hpux tag type: 0x%04x\n", 
608                                         hpux_tag));
609                 break;
610         }
611
612         DEBUGADD(10, (" --> determined smb tag 0x%04x\n", smb_tag));
613         
614         return smb_tag;
615 }
616
617
618 /* 
619  * The permission bits used in the following two permission conversion 
620  * functions are same, but the functions make us independent of the concrete 
621  * permission data types.
622  */
623 static SMB_ACL_PERM_T hpux_perm_to_smb_perm(const HPUX_PERM_T perm)
624 {
625         SMB_ACL_PERM_T smb_perm = 0;
626         smb_perm |= ((perm & SMB_ACL_READ) ? SMB_ACL_READ : 0);
627         smb_perm |= ((perm & SMB_ACL_WRITE) ? SMB_ACL_WRITE : 0);
628         smb_perm |= ((perm & SMB_ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
629         return smb_perm;
630 }
631
632
633 static HPUX_PERM_T smb_perm_to_hpux_perm(const SMB_ACL_PERM_T perm)
634 {
635         HPUX_PERM_T hpux_perm = 0;
636         hpux_perm |= ((perm & SMB_ACL_READ) ? SMB_ACL_READ : 0);
637         hpux_perm |= ((perm & SMB_ACL_WRITE) ? SMB_ACL_WRITE : 0);
638         hpux_perm |= ((perm & SMB_ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
639         return hpux_perm;
640 }
641
642
643 static BOOL hpux_acl_get_file(const char *name, HPUX_ACL_T *hpux_acl, 
644                                  int *count)
645 {
646         BOOL result = False;
647         static HPUX_ACE_T dummy_ace;
648
649         DEBUG(10, ("hpux_acl_get_file called for file '%s'\n", name));
650         
651         /* 
652          * The original code tries some INITIAL_ACL_SIZE
653          * and only did the ACL_CNT call upon failure
654          * (for performance reasons).
655          * For the sake of simplicity, I skip this for now. 
656          *
657          * NOTE: There is a catch here on HP-UX: acl with cmd parameter
658          * ACL_CNT fails with errno EINVAL when called with a NULL
659          * pointer as last argument. So we need to use a dummy acl
660          * struct here (we make it static so it does not need to be
661          * instantiated or malloced each time this function is
662          * called). Btw: the count parameter does not seem to matter...
663          */
664         *count = acl(CONST_DISCARD(char *, name), ACL_CNT, 0, &dummy_ace);
665         if (*count < 0) {
666                 DEBUG(10, ("acl ACL_CNT failed: %s\n", strerror(errno)));
667                 goto done;
668         }
669         *hpux_acl = hpux_acl_init(*count);
670         if (*hpux_acl == NULL) {
671                 DEBUG(10, ("error allocating memory for hpux acl...\n"));
672                 goto done;
673         }
674         *count = acl(CONST_DISCARD(char *, name), ACL_GET, *count, *hpux_acl);
675         if (*count < 0) {
676                 DEBUG(10, ("acl ACL_GET failed: %s\n", strerror(errno)));
677                 goto done;
678         }
679         result = True;
680
681  done:
682         DEBUG(10, ("hpux_acl_get_file %s.\n",
683                    ((result == True) ? "succeeded" : "failed" )));
684         return result;
685 }
686
687
688
689
690 /*
691  * Add entries to a hpux ACL.
692  *
693  * Entries are directly added to the hpuxacl parameter.
694  * if memory allocation fails, this may result in hpuxacl 
695  * being NULL. if the resulting acl is to be checked and is 
696  * not valid, it is kept in hpuxacl but False is returned.
697  *
698  * The type of ACEs (access/default) to be added to the ACL can 
699  * be selected via the type parameter. 
700  * I use the SMB_ACL_TYPE_T type here. Since SMB_ACL_TYPE_ACCESS
701  * is defined as "0", this means that one can only add either
702  * access or default ACEs from the given ACL, not both at the same 
703  * time. If it should become necessary to add all of an ACL, one 
704  * would have to replace this parameter by another type.
705  */
706 static BOOL hpux_add_to_acl(HPUX_ACL_T *hpux_acl, int *count,
707                                HPUX_ACL_T add_acl, int add_count, 
708                                SMB_ACL_TYPE_T type)
709 {
710         int i;
711         
712         if ((type != SMB_ACL_TYPE_ACCESS) && (type != SMB_ACL_TYPE_DEFAULT)) 
713         {
714                 DEBUG(10, ("invalid acl type given: %d\n", type));
715                 errno = EINVAL;
716                 return False;
717         }
718         for (i = 0; i < add_count; i++) {
719                 if (!_IS_OF_TYPE(add_acl[i], type)) {
720                         continue;
721                 }
722                 ADD_TO_ARRAY(NULL, HPUX_ACE_T, add_acl[i], 
723                              hpux_acl, count);
724                 if (hpux_acl == NULL) {
725                         DEBUG(10, ("error enlarging acl.\n"));
726                         errno = ENOMEM;
727                         return False;
728                 }
729         }
730         return True;
731 }
732
733
734 /* 
735  * sort the ACL and check it for validity
736  *
737  * [original comment from lib/sysacls.c:]
738  * 
739  * if it's a minimal ACL with only 4 entries then we
740  * need to recalculate the mask permissions to make
741  * sure that they are the same as the GROUP_OBJ
742  * permissions as required by the UnixWare acl() system call.
743  *
744  * (note: since POSIX allows minimal ACLs which only contain
745  * 3 entries - ie there is no mask entry - we should, in theory,
746  * check for this and add a mask entry if necessary - however
747  * we "know" that the caller of this interface always specifies
748  * a mask, so in practice "this never happens" (tm) - if it *does*
749  * happen aclsort() will fail and return an error and someone will
750  * have to fix it...)
751  */
752 static BOOL hpux_acl_sort(HPUX_ACL_T hpux_acl, int count)
753 {
754         int fixmask = (count <= 4);
755
756         if (hpux_internal_aclsort(count, fixmask, hpux_acl) != 0) {
757                 errno = EINVAL;
758                 return False;
759         }
760         return True;
761 }
762
763
764 /*
765  * Helpers for hpux_internal_aclsort:
766  *   - hpux_count_obj
767  *   - hpux_swap_acl_entries
768  *   - hpux_prohibited_duplicate_type
769  *   - hpux_get_needed_class_perm
770  */
771
772 /* hpux_count_obj:
773  * Counts the different number of objects in a given array of ACL
774  * structures.
775  * Inputs:
776  *
777  * acl_count      - Count of ACLs in the array of ACL strucutres.
778  * aclp           - Array of ACL structures.
779  * acl_type_count - Pointer to acl_types structure. Should already be
780  *                  allocated.
781  * Output: 
782  *
783  * acl_type_count - This structure is filled up with counts of various 
784  *                  acl types.
785  */
786
787 static void hpux_count_obj(int acl_count, HPUX_ACL_T aclp, struct hpux_acl_types *acl_type_count)
788 {
789         int i;
790
791         memset(acl_type_count, 0, sizeof(struct hpux_acl_types));
792
793         for(i=0;i<acl_count;i++) {
794                 switch(aclp[i].a_type) {
795                 case USER: 
796                         acl_type_count->n_user++;
797                         break;
798                 case USER_OBJ: 
799                         acl_type_count->n_user_obj++;
800                         break;
801                 case DEF_USER_OBJ: 
802                         acl_type_count->n_def_user_obj++;
803                         break;
804                 case GROUP: 
805                         acl_type_count->n_group++;
806                         break;
807                 case GROUP_OBJ: 
808                         acl_type_count->n_group_obj++;
809                         break;
810                 case DEF_GROUP_OBJ: 
811                         acl_type_count->n_def_group_obj++;
812                         break;
813                 case OTHER_OBJ: 
814                         acl_type_count->n_other_obj++;
815                         break;
816                 case DEF_OTHER_OBJ: 
817                         acl_type_count->n_def_other_obj++;
818                         break;
819                 case CLASS_OBJ:
820                         acl_type_count->n_class_obj++;
821                         break;
822                 case DEF_CLASS_OBJ:
823                         acl_type_count->n_def_class_obj++;
824                         break;
825                 case DEF_USER:
826                         acl_type_count->n_def_user++;
827                         break;
828                 case DEF_GROUP:
829                         acl_type_count->n_def_group++;
830                         break;
831                 default: 
832                         acl_type_count->n_illegal_obj++;
833                         break;
834                 }
835         }
836 }
837
838 /* hpux_swap_acl_entries:  Swaps two ACL entries. 
839  *
840  * Inputs: aclp0, aclp1 - ACL entries to be swapped.
841  */
842
843 static void hpux_swap_acl_entries(HPUX_ACE_T *aclp0, HPUX_ACE_T *aclp1)
844 {
845         HPUX_ACE_T temp_acl;
846
847         temp_acl.a_type = aclp0->a_type;
848         temp_acl.a_id = aclp0->a_id;
849         temp_acl.a_perm = aclp0->a_perm;
850
851         aclp0->a_type = aclp1->a_type;
852         aclp0->a_id = aclp1->a_id;
853         aclp0->a_perm = aclp1->a_perm;
854
855         aclp1->a_type = temp_acl.a_type;
856         aclp1->a_id = temp_acl.a_id;
857         aclp1->a_perm = temp_acl.a_perm;
858 }
859
860 /* hpux_prohibited_duplicate_type
861  * Identifies if given ACL type can have duplicate entries or 
862  * not.
863  *
864  * Inputs: acl_type - ACL Type.
865  *
866  * Outputs: 
867  *
868  * Return.. 
869  *
870  * True - If the ACL type matches any of the prohibited types.
871  * False - If the ACL type doesn't match any of the prohibited types.
872  */ 
873
874 static BOOL hpux_prohibited_duplicate_type(int acl_type)
875 {
876         switch(acl_type) {
877                 case USER:
878                 case GROUP:
879                 case DEF_USER: 
880                 case DEF_GROUP:
881                         return True;
882                 default:
883                         return False;
884         }
885 }
886
887 /* hpux_get_needed_class_perm
888  * Returns the permissions of a ACL structure only if the ACL
889  * type matches one of the pre-determined types for computing 
890  * CLASS_OBJ permissions.
891  *
892  * Inputs: aclp - Pointer to ACL structure.
893  */
894
895 static int hpux_get_needed_class_perm(struct acl *aclp)
896 {
897         switch(aclp->a_type) {
898                 case USER: 
899                 case GROUP_OBJ: 
900                 case GROUP: 
901                 case DEF_USER_OBJ: 
902                 case DEF_USER:
903                 case DEF_GROUP_OBJ: 
904                 case DEF_GROUP:
905                 case DEF_CLASS_OBJ:
906                 case DEF_OTHER_OBJ: 
907                         return aclp->a_perm;
908                 default: 
909                         return 0;
910         }
911 }
912
913 /* hpux_internal_aclsort: aclsort for HPUX.
914  *
915  * -> The aclsort() system call is availabe on the latest HPUX General
916  * -> Patch Bundles. So for HPUX, we developed our version of aclsort 
917  * -> function. Because, we don't want to update to a new 
918  * -> HPUX GR bundle just for aclsort() call.
919  *
920  * aclsort sorts the array of ACL structures as per the description in
921  * aclsort man page. Refer to aclsort man page for more details
922  *
923  * Inputs:
924  *
925  * acl_count - Count of ACLs in the array of ACL structures.
926  * calclass  - If this is not zero, then we compute the CLASS_OBJ
927  *             permissions.
928  * aclp      - Array of ACL structures.
929  *
930  * Outputs:
931  *
932  * aclp     - Sorted array of ACL structures.
933  *
934  * Outputs:
935  *
936  * Returns 0 for success -1 for failure. Prints a message to the Samba
937  * debug log in case of failure.
938  */
939
940 static int hpux_internal_aclsort(int acl_count, int calclass, HPUX_ACL_T aclp)
941 {
942         struct hpux_acl_types acl_obj_count;
943         int n_class_obj_perm = 0;
944         int i, j;
945  
946         DEBUG(10,("Entering hpux_internal_aclsort. (calclass = %d)\n", calclass));
947
948         if (hpux_aclsort_call_present()) {
949                 DEBUG(10, ("calling hpux aclsort\n"));
950                 return aclsort(acl_count, calclass, aclp);
951         }
952
953         DEBUG(10, ("using internal aclsort\n"));
954
955         if(!acl_count) {
956                 DEBUG(10,("Zero acl count passed. Returning Success\n"));
957                 return 0;
958         }
959
960         if(aclp == NULL) {
961                 DEBUG(0,("Null ACL pointer in hpux_acl_sort. Returning Failure. \n"));
962                 return -1;
963         }
964
965         /* Count different types of ACLs in the ACLs array */
966
967         hpux_count_obj(acl_count, aclp, &acl_obj_count);
968
969         /* There should be only one entry each of type USER_OBJ, GROUP_OBJ, 
970          * CLASS_OBJ and OTHER_OBJ 
971          */
972
973         if ( (acl_obj_count.n_user_obj  != 1) || 
974                 (acl_obj_count.n_group_obj != 1) || 
975                 (acl_obj_count.n_class_obj != 1) ||
976                 (acl_obj_count.n_other_obj != 1) ) 
977         {
978                 DEBUG(0,("hpux_internal_aclsort: More than one entry or no entries for \
979 USER OBJ or GROUP_OBJ or OTHER_OBJ or CLASS_OBJ\n"));
980                 return -1;
981         }
982
983         /* If any of the default objects are present, there should be only
984          * one of them each.
985          */
986
987         if ( (acl_obj_count.n_def_user_obj  > 1) || 
988                 (acl_obj_count.n_def_group_obj > 1) || 
989                 (acl_obj_count.n_def_other_obj > 1) || 
990                 (acl_obj_count.n_def_class_obj > 1) ) 
991         {
992                 DEBUG(0,("hpux_internal_aclsort: More than one entry for DEF_CLASS_OBJ \
993 or DEF_USER_OBJ or DEF_GROUP_OBJ or DEF_OTHER_OBJ\n"));
994                 return -1;
995         }
996
997         /* We now have proper number of OBJ and DEF_OBJ entries. Now sort the acl 
998          * structures.  
999          *
1000          * Sorting crieteria - First sort by ACL type. If there are multiple entries of
1001          * same ACL type, sort by ACL id.
1002          *
1003          * I am using the trival kind of sorting method here because, performance isn't 
1004          * really effected by the ACLs feature. More over there aren't going to be more
1005          * than 17 entries on HPUX. 
1006          */
1007
1008         for(i=0; i<acl_count;i++) {
1009                 for (j=i+1; j<acl_count; j++) {
1010                         if( aclp[i].a_type > aclp[j].a_type ) {
1011                                 /* ACL entries out of order, swap them */
1012                                 hpux_swap_acl_entries((aclp+i), (aclp+j));
1013                         } else if ( aclp[i].a_type == aclp[j].a_type ) {
1014                                 /* ACL entries of same type, sort by id */
1015                                 if(aclp[i].a_id > aclp[j].a_id) {
1016                                         hpux_swap_acl_entries((aclp+i), (aclp+j));
1017                                 } else if (aclp[i].a_id == aclp[j].a_id) {
1018                                         /* We have a duplicate entry. */
1019                                         if(hpux_prohibited_duplicate_type(aclp[i].a_type)) {
1020                                                 DEBUG(0, ("hpux_internal_aclsort: Duplicate entry: Type(hex): %x Id: %d\n",
1021                                                         aclp[i].a_type, aclp[i].a_id));
1022                                                 return -1;
1023                                         }
1024                                 }
1025                         }
1026                 }
1027         }
1028
1029         /* set the class obj permissions to the computed one. */
1030         if(calclass) {
1031                 int n_class_obj_index = -1;
1032
1033                 for(i=0;i<acl_count;i++) {
1034                         n_class_obj_perm |= hpux_get_needed_class_perm((aclp+i));
1035
1036                         if(aclp[i].a_type == CLASS_OBJ)
1037                                 n_class_obj_index = i;
1038                 }
1039                 aclp[n_class_obj_index].a_perm = n_class_obj_perm;
1040         }
1041
1042         return 0;
1043 }
1044
1045
1046 /* 
1047  * hpux_acl_call_present:
1048  *
1049  * This checks if the POSIX ACL system call is defined
1050  * which basically corresponds to whether JFS 3.3 or
1051  * higher is installed. If acl() was called when it
1052  * isn't defined, it causes the process to core dump
1053  * so it is important to check this and avoid acl()
1054  * calls if it isn't there.                            
1055  */
1056
1057 static BOOL hpux_acl_call_present(void)
1058 {
1059
1060         shl_t handle = NULL;
1061         void *value;
1062         int ret_val=0;
1063         static BOOL already_checked = False;
1064
1065         if(already_checked)
1066                 return True;
1067
1068         errno = 0;
1069
1070         ret_val = shl_findsym(&handle, "acl", TYPE_PROCEDURE, &value);
1071
1072         if(ret_val != 0) {
1073                 DEBUG(5, ("hpux_acl_call_present: shl_findsym() returned %d, errno = %d, error %s\n",
1074                         ret_val, errno, strerror(errno)));
1075                 DEBUG(5,("hpux_acl_call_present: acl() system call is not present. Check if you have JFS 3.3 and above?\n"));
1076                 errno = ENOSYS;
1077                 return False;
1078         }
1079
1080         DEBUG(10,("hpux_acl_call_present: acl() system call is present. We have JFS 3.3 or above \n"));
1081
1082         already_checked = True;
1083         return True;
1084 }
1085
1086 /* 
1087  * runtime check for presence of aclsort library call. 
1088  * same code as for acl call. if there are more of these,
1089  * a dispatcher function could be handy...
1090  */
1091
1092 static BOOL hpux_aclsort_call_present(void) 
1093 {
1094         shl_t handle = NULL;
1095         void *value;
1096         int ret_val = 0;
1097         static BOOL already_checked = False;
1098
1099         if (already_checked) {
1100                 return True;
1101         }
1102
1103         errno = 0;
1104         ret_val = shl_findsym(&handle, "aclsort", TYPE_PROCEDURE, &value);
1105         if (ret_val != 0) {
1106                 DEBUG(5, ("hpux_aclsort_call_present: shl_findsym "
1107                         "returned %d, errno = %d, error %s", 
1108                         ret_val, errno, strerror(errno)));
1109                 DEBUG(5, ("hpux_aclsort_call_present: "
1110                         "aclsort() function not available.\n"));
1111                 return False;
1112         }
1113         DEBUG(10,("hpux_aclsort_call_present: aclsort() function present.\n"));
1114         already_checked = True;
1115         return True;
1116 }
1117
1118 #if 0
1119 /*
1120  * acl check function:
1121  *   unused at the moment but could be used to get more
1122  *   concrete error messages for debugging...
1123  *   (acl sort just says that the acl is invalid...)
1124  */
1125 static BOOL hpux_acl_check(HPUX_ACL_T hpux_acl, int count)
1126 {
1127         int check_rc;
1128         int check_which;
1129         
1130         check_rc = aclcheck(hpux_acl, count, &check_which);
1131         if (check_rc != 0) {
1132                 DEBUG(10, ("acl is not valid:\n"));
1133                 DEBUGADD(10, (" - return code: %d\n", check_rc));
1134                 DEBUGADD(10, (" - which: %d\n", check_which));
1135                 if (check_which != -1) {
1136                         DEBUGADD(10, (" - invalid entry:\n"));
1137                         DEBUGADD(10, ("   * type: %d:\n", 
1138                                       hpux_acl[check_which].a_type));
1139                         DEBUGADD(10, ("   * id: %d\n",
1140                                       hpux_acl[check_which].a_id));
1141                         DEBUGADD(10, ("   * perm: 0o%o\n",
1142                                       hpux_acl[check_which].a_perm));
1143                 }
1144                 return False;
1145         }
1146         return True;
1147 }
1148 #endif
1149
1150 /* VFS operations structure */
1151
1152 static vfs_op_tuple hpuxacl_op_tuples[] = {
1153         /* Disk operations */
1154         {SMB_VFS_OP(hpuxacl_sys_acl_get_file),
1155          SMB_VFS_OP_SYS_ACL_GET_FILE,
1156          SMB_VFS_LAYER_TRANSPARENT},
1157
1158         {SMB_VFS_OP(hpuxacl_sys_acl_get_fd),
1159          SMB_VFS_OP_SYS_ACL_GET_FD,
1160          SMB_VFS_LAYER_TRANSPARENT},
1161
1162         {SMB_VFS_OP(hpuxacl_sys_acl_set_file),
1163          SMB_VFS_OP_SYS_ACL_SET_FILE,
1164          SMB_VFS_LAYER_TRANSPARENT},
1165
1166         {SMB_VFS_OP(hpuxacl_sys_acl_set_fd),
1167          SMB_VFS_OP_SYS_ACL_SET_FD,
1168          SMB_VFS_LAYER_TRANSPARENT},
1169
1170         {SMB_VFS_OP(hpuxacl_sys_acl_delete_def_file),
1171          SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE,
1172          SMB_VFS_LAYER_TRANSPARENT},
1173
1174         {SMB_VFS_OP(NULL),
1175          SMB_VFS_OP_NOOP,
1176          SMB_VFS_LAYER_NOOP}
1177 };
1178
1179 NTSTATUS vfs_hpuxacl_init(void)
1180 {
1181         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "hpuxacl",
1182                                 hpuxacl_op_tuples);
1183 }
1184
1185 /* ENTE */