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