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