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