s3:vfs_catia: Use talloc_zero for simplification
[Samba/aatanasov.git] / source3 / modules / vfs_hpuxacl.c
blobabc80dcd9c2cddf728f03c0b6250300e42526beb
1 /*
2 * Unix SMB/Netbios implementation.
3 * VFS module to get and set HP-UX ACLs
4 * Copyright (C) Michael Adam 2006,2008
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.
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.
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/>.
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.
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.)
33 /* =================================================================
34 * NOTE:
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.
44 * Michael Adam <obnox@samba.org>
46 * ================================================================= */
49 #include "includes.h"
51 /*
52 * including standard header <sys/aclv.h>
54 * included here as a quick hack for the special HP-UX-situation:
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>.
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>
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;
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;
80 int n_group;
81 int n_def_group;
82 int n_group_obj;
83 int n_def_group_obj;
85 int n_other;
86 int n_other_obj;
87 int n_def_other_obj;
89 int n_class_obj;
90 int n_def_class_obj;
92 int n_illegal_obj;
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)) \
104 /* prototypes for private functions */
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);
131 static bool hpux_acl_call_present(void);
132 static bool hpux_aclsort_call_present(void);
135 /* public functions - the api */
137 SMB_ACL_T hpuxacl_sys_acl_get_file(vfs_handle_struct *handle,
138 const char *path_p,
139 SMB_ACL_TYPE_T type)
141 SMB_ACL_T result = NULL;
142 int count;
143 HPUX_ACL_T hpux_acl = NULL;
145 DEBUG(10, ("hpuxacl_sys_acl_get_file called for file '%s'.\n",
146 path_p));
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.
152 goto done;
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;
161 DEBUGADD(10, ("getting %s acl\n",
162 ((type == SMB_ACL_TYPE_ACCESS) ? "access" : "default")));
164 if (!hpux_acl_get_file(path_p, &hpux_acl, &count)) {
165 goto done;
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)));
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;
182 * get the access ACL of a file referred to by a fd
184 SMB_ACL_T hpuxacl_sys_acl_get_fd(vfs_handle_struct *handle,
185 files_struct *fsp)
188 * HPUX doesn't have the facl call. Fake it using the path.... JRA.
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;
198 * We know we're in the same conn context. So we
199 * can use the relative path.
201 DEBUG(10, ("redirecting call of hpuxacl_sys_acl_get_fd to "
202 "hpuxacl_sys_acl_get_file (no facl syscall on HPUX).\n"));
204 return hpuxacl_sys_acl_get_file(handle,
205 file_struct_p->fsp_name->base_name,
206 SMB_ACL_TYPE_ACCESS);
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)
215 int ret = -1;
216 HPUX_ACL_T hpux_acl = NULL;
217 int count;
218 struct smb_filename *smb_fname = NULL;
220 DEBUG(10, ("hpuxacl_sys_acl_set_file called for file '%s'\n",
221 name));
223 status = create_synthetic_smb_fname(talloc_tos(), name, NULL, NULL,
224 &smb_fname);
225 if (!NT_STATUS_IS_OK(status)) {
226 goto done;
229 if(hpux_acl_call_present() == False) {
230 /* Looks like we don't have the acl() system call on HPUX.
231 * May be the system doesn't have the latest version of JFS.
233 goto done;
236 if ((type != SMB_ACL_TYPE_ACCESS) && (type != SMB_ACL_TYPE_DEFAULT)) {
237 errno = EINVAL;
238 DEBUG(10, ("invalid smb acl type given (%d).\n", type));
239 goto done;
241 DEBUGADD(10, ("setting %s acl\n",
242 ((type == SMB_ACL_TYPE_ACCESS) ? "access" : "default")));
244 if(!smb_acl_to_hpux_acl(theacl, &hpux_acl, &count, type)) {
245 DEBUG(10, ("conversion smb_acl -> hpux_acl failed (%s).\n",
246 strerror(errno)));
247 goto done;
251 * if the file is a directory, there is extra work to do:
252 * since the hpux acl call stores both the access acl and
253 * the default acl as provided, we have to get the acl part
254 * that has _not_ been specified in "type" from the file first
255 * and concatenate it with the acl provided.
257 if (SMB_VFS_STAT(handle->conn, smb_fname) != 0) {
258 DEBUG(10, ("Error in stat call: %s\n", strerror(errno)));
259 goto done;
261 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
262 HPUX_ACL_T other_acl;
263 int other_count;
264 SMB_ACL_TYPE_T other_type;
266 other_type = (type == SMB_ACL_TYPE_ACCESS)
267 ? SMB_ACL_TYPE_DEFAULT
268 : SMB_ACL_TYPE_ACCESS;
269 DEBUGADD(10, ("getting acl from filesystem\n"));
270 if (!hpux_acl_get_file(smb_fname->base_name, &other_acl,
271 &other_count)) {
272 DEBUG(10, ("error getting acl from directory\n"));
273 goto done;
275 DEBUG(10, ("adding %s part of fs acl to given acl\n",
276 ((other_type == SMB_ACL_TYPE_ACCESS)
277 ? "access"
278 : "default")));
279 if (!hpux_add_to_acl(&hpux_acl, &count, other_acl,
280 other_count, other_type))
282 DEBUG(10, ("error adding other acl.\n"));
283 SAFE_FREE(other_acl);
284 goto done;
286 SAFE_FREE(other_acl);
288 else if (type != SMB_ACL_TYPE_ACCESS) {
289 errno = EINVAL;
290 goto done;
293 if (!hpux_acl_sort(hpux_acl, count)) {
294 DEBUG(10, ("resulting acl is not valid!\n"));
295 goto done;
297 DEBUG(10, ("resulting acl is valid.\n"));
299 ret = acl(CONST_DISCARD(char *, smb_fname->base_name), ACL_SET, count,
300 hpux_acl);
301 if (ret != 0) {
302 DEBUG(0, ("ERROR calling acl: %s\n", strerror(errno)));
305 done:
306 DEBUG(10, ("hpuxacl_sys_acl_set_file %s.\n",
307 ((ret != 0) ? "failed" : "succeeded")));
308 TALLOC_FREE(smb_fname);
309 SAFE_FREE(hpux_acl);
310 return ret;
314 * set the access ACL on the file referred to by a fd
316 int hpuxacl_sys_acl_set_fd(vfs_handle_struct *handle,
317 files_struct *fsp,
318 SMB_ACL_T theacl)
321 * HPUX doesn't have the facl call. Fake it using the path.... JRA.
323 /* For all I see, the info should already be in the fsp
324 * parameter, but get it again to be safe --- necessary? */
325 files_struct *file_struct_p = file_find_fd(fsp->fh->fd);
326 if (file_struct_p == NULL) {
327 errno = EBADF;
328 return -1;
331 * We know we're in the same conn context. So we
332 * can use the relative path.
334 DEBUG(10, ("redirecting call of hpuxacl_sys_acl_set_fd to "
335 "hpuxacl_sys_acl_set_file (no facl syscall on HPUX)\n"));
337 return hpuxacl_sys_acl_set_file(handle,
338 file_struct_p->fsp_name->base_name,
339 SMB_ACL_TYPE_ACCESS, theacl);
344 * delete the default ACL of a directory
346 * This is achieved by fetching the access ACL and rewriting it
347 * directly, via the hpux system call: the ACL_SET call on
348 * directories writes both the access and the default ACL as provided.
350 * XXX: posix acl_delete_def_file returns an error if
351 * the file referred to by path is not a directory.
352 * this function does not complain but the actions
353 * have no effect on a file other than a directory.
354 * But sys_acl_delete_default_file is only called in
355 * smbd/posixacls.c after having checked that the file
356 * is a directory, anyways. So implementing the extra
357 * check is considered unnecessary. --- Agreed? XXX
359 int hpuxacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
360 const char *path)
362 SMB_ACL_T smb_acl;
363 int ret = -1;
364 HPUX_ACL_T hpux_acl;
365 int count;
367 DEBUG(10, ("entering hpuxacl_sys_acl_delete_def_file.\n"));
369 smb_acl = hpuxacl_sys_acl_get_file(handle, path,
370 SMB_ACL_TYPE_ACCESS);
371 if (smb_acl == NULL) {
372 DEBUG(10, ("getting file acl failed!\n"));
373 goto done;
375 if (!smb_acl_to_hpux_acl(smb_acl, &hpux_acl, &count,
376 SMB_ACL_TYPE_ACCESS))
378 DEBUG(10, ("conversion smb_acl -> hpux_acl failed.\n"));
379 goto done;
381 if (!hpux_acl_sort(hpux_acl, count)) {
382 DEBUG(10, ("resulting acl is not valid!\n"));
383 goto done;
385 ret = acl(CONST_DISCARD(char *, path), ACL_SET, count, hpux_acl);
386 if (ret != 0) {
387 DEBUG(10, ("settinge file acl failed!\n"));
390 done:
391 DEBUG(10, ("hpuxacl_sys_acl_delete_def_file %s.\n",
392 ((ret != 0) ? "failed" : "succeeded" )));
393 SAFE_FREE(smb_acl);
394 return ret;
399 * private functions
402 static HPUX_ACL_T hpux_acl_init(int count)
404 HPUX_ACL_T hpux_acl =
405 (HPUX_ACL_T)SMB_MALLOC(sizeof(HPUX_ACE_T) * count);
406 if (hpux_acl == NULL) {
407 errno = ENOMEM;
409 return hpux_acl;
413 * Convert the SMB acl to the ACCESS or DEFAULT part of a
414 * hpux ACL, as desired.
416 static bool smb_acl_to_hpux_acl(SMB_ACL_T smb_acl,
417 HPUX_ACL_T *hpux_acl, int *count,
418 SMB_ACL_TYPE_T type)
420 bool ret = False;
421 int i;
422 int check_which, check_rc;
424 DEBUG(10, ("entering smb_acl_to_hpux_acl\n"));
426 *hpux_acl = NULL;
427 *count = 0;
429 for (i = 0; i < smb_acl->count; i++) {
430 const struct smb_acl_entry *smb_entry = &(smb_acl->acl[i]);
431 HPUX_ACE_T hpux_entry;
433 ZERO_STRUCT(hpux_entry);
435 hpux_entry.a_type = smb_tag_to_hpux_tag(smb_entry->a_type);
436 if (hpux_entry.a_type == 0) {
437 DEBUG(10, ("smb_tag to hpux_tag failed\n"));
438 goto fail;
440 switch(hpux_entry.a_type) {
441 case USER:
442 DEBUG(10, ("got tag type USER with uid %d\n",
443 smb_entry->uid));
444 hpux_entry.a_id = (uid_t)smb_entry->uid;
445 break;
446 case GROUP:
447 DEBUG(10, ("got tag type GROUP with gid %d\n",
448 smb_entry->gid));
449 hpux_entry.a_id = (uid_t)smb_entry->gid;
450 break;
451 default:
452 break;
454 if (type == SMB_ACL_TYPE_DEFAULT) {
455 DEBUG(10, ("adding default bit to hpux ace\n"));
456 hpux_entry.a_type |= ACL_DEFAULT;
459 hpux_entry.a_perm =
460 smb_perm_to_hpux_perm(smb_entry->a_perm);
461 DEBUG(10, ("assembled the following hpux ace:\n"));
462 DEBUGADD(10, (" - type: 0x%04x\n", hpux_entry.a_type));
463 DEBUGADD(10, (" - id: %d\n", hpux_entry.a_id));
464 DEBUGADD(10, (" - perm: o%o\n", hpux_entry.a_perm));
465 if (!hpux_add_to_acl(hpux_acl, count, &hpux_entry,
466 1, type))
468 DEBUG(10, ("error adding acl entry\n"));
469 goto fail;
471 DEBUG(10, ("count after adding: %d (i: %d)\n", *count, i));
472 DEBUG(10, ("test, if entry has been copied into acl:\n"));
473 DEBUGADD(10, (" - type: 0x%04x\n",
474 (*hpux_acl)[(*count)-1].a_type));
475 DEBUGADD(10, (" - id: %d\n",
476 (*hpux_acl)[(*count)-1].a_id));
477 DEBUGADD(10, (" - perm: o%o\n",
478 (*hpux_acl)[(*count)-1].a_perm));
481 ret = True;
482 goto done;
484 fail:
485 SAFE_FREE(*hpux_acl);
486 done:
487 DEBUG(10, ("smb_acl_to_hpux_acl %s\n",
488 ((ret == True) ? "succeeded" : "failed")));
489 return ret;
493 * convert either the access or the default part of a
494 * soaris acl to the SMB_ACL format.
496 static SMB_ACL_T hpux_acl_to_smb_acl(HPUX_ACL_T hpux_acl, int count,
497 SMB_ACL_TYPE_T type)
499 SMB_ACL_T result;
500 int i;
502 if ((result = sys_acl_init(0)) == NULL) {
503 DEBUG(10, ("error allocating memory for SMB_ACL\n"));
504 goto fail;
506 for (i = 0; i < count; i++) {
507 SMB_ACL_ENTRY_T smb_entry;
508 SMB_ACL_PERM_T smb_perm;
510 if (!_IS_OF_TYPE(hpux_acl[i], type)) {
511 continue;
513 result = SMB_REALLOC(result,
514 sizeof(struct smb_acl_t) +
515 (sizeof(struct smb_acl_entry) *
516 (result->count + 1)));
517 if (result == NULL) {
518 DEBUG(10, ("error reallocating memory for SMB_ACL\n"));
519 goto fail;
521 smb_entry = &result->acl[result->count];
522 if (sys_acl_set_tag_type(smb_entry,
523 hpux_tag_to_smb_tag(hpux_acl[i].a_type)) != 0)
525 DEBUG(10, ("invalid tag type given: 0x%04x\n",
526 hpux_acl[i].a_type));
527 goto fail;
529 /* intentionally not checking return code here: */
530 sys_acl_set_qualifier(smb_entry, (void *)&hpux_acl[i].a_id);
531 smb_perm = hpux_perm_to_smb_perm(hpux_acl[i].a_perm);
532 if (sys_acl_set_permset(smb_entry, &smb_perm) != 0) {
533 DEBUG(10, ("invalid permset given: %d\n",
534 hpux_acl[i].a_perm));
535 goto fail;
537 result->count += 1;
539 goto done;
541 fail:
542 SAFE_FREE(result);
543 done:
544 DEBUG(10, ("hpux_acl_to_smb_acl %s\n",
545 ((result == NULL) ? "failed" : "succeeded")));
546 return result;
551 static HPUX_ACL_TAG_T smb_tag_to_hpux_tag(SMB_ACL_TAG_T smb_tag)
553 HPUX_ACL_TAG_T hpux_tag = 0;
555 DEBUG(10, ("smb_tag_to_hpux_tag\n"));
556 DEBUGADD(10, (" --> got smb tag 0x%04x\n", smb_tag));
558 switch (smb_tag) {
559 case SMB_ACL_USER:
560 hpux_tag = USER;
561 break;
562 case SMB_ACL_USER_OBJ:
563 hpux_tag = USER_OBJ;
564 break;
565 case SMB_ACL_GROUP:
566 hpux_tag = GROUP;
567 break;
568 case SMB_ACL_GROUP_OBJ:
569 hpux_tag = GROUP_OBJ;
570 break;
571 case SMB_ACL_OTHER:
572 hpux_tag = OTHER_OBJ;
573 break;
574 case SMB_ACL_MASK:
575 hpux_tag = CLASS_OBJ;
576 break;
577 default:
578 DEBUGADD(10, (" !!! unknown smb tag type 0x%04x\n", smb_tag));
579 break;
582 DEBUGADD(10, (" --> determined hpux tag 0x%04x\n", hpux_tag));
584 return hpux_tag;
587 static SMB_ACL_TAG_T hpux_tag_to_smb_tag(HPUX_ACL_TAG_T hpux_tag)
589 SMB_ACL_TAG_T smb_tag = 0;
591 DEBUG(10, ("hpux_tag_to_smb_tag:\n"));
592 DEBUGADD(10, (" --> got hpux tag 0x%04x\n", hpux_tag));
594 hpux_tag &= ~ACL_DEFAULT;
596 switch (hpux_tag) {
597 case USER:
598 smb_tag = SMB_ACL_USER;
599 break;
600 case USER_OBJ:
601 smb_tag = SMB_ACL_USER_OBJ;
602 break;
603 case GROUP:
604 smb_tag = SMB_ACL_GROUP;
605 break;
606 case GROUP_OBJ:
607 smb_tag = SMB_ACL_GROUP_OBJ;
608 break;
609 case OTHER_OBJ:
610 smb_tag = SMB_ACL_OTHER;
611 break;
612 case CLASS_OBJ:
613 smb_tag = SMB_ACL_MASK;
614 break;
615 default:
616 DEBUGADD(10, (" !!! unknown hpux tag type: 0x%04x\n",
617 hpux_tag));
618 break;
621 DEBUGADD(10, (" --> determined smb tag 0x%04x\n", smb_tag));
623 return smb_tag;
628 * The permission bits used in the following two permission conversion
629 * functions are same, but the functions make us independent of the concrete
630 * permission data types.
632 static SMB_ACL_PERM_T hpux_perm_to_smb_perm(const HPUX_PERM_T perm)
634 SMB_ACL_PERM_T smb_perm = 0;
635 smb_perm |= ((perm & SMB_ACL_READ) ? SMB_ACL_READ : 0);
636 smb_perm |= ((perm & SMB_ACL_WRITE) ? SMB_ACL_WRITE : 0);
637 smb_perm |= ((perm & SMB_ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
638 return smb_perm;
642 static HPUX_PERM_T smb_perm_to_hpux_perm(const SMB_ACL_PERM_T perm)
644 HPUX_PERM_T hpux_perm = 0;
645 hpux_perm |= ((perm & SMB_ACL_READ) ? SMB_ACL_READ : 0);
646 hpux_perm |= ((perm & SMB_ACL_WRITE) ? SMB_ACL_WRITE : 0);
647 hpux_perm |= ((perm & SMB_ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
648 return hpux_perm;
652 static bool hpux_acl_get_file(const char *name, HPUX_ACL_T *hpux_acl,
653 int *count)
655 bool result = False;
656 static HPUX_ACE_T dummy_ace;
658 DEBUG(10, ("hpux_acl_get_file called for file '%s'\n", name));
661 * The original code tries some INITIAL_ACL_SIZE
662 * and only did the ACL_CNT call upon failure
663 * (for performance reasons).
664 * For the sake of simplicity, I skip this for now.
666 * NOTE: There is a catch here on HP-UX: acl with cmd parameter
667 * ACL_CNT fails with errno EINVAL when called with a NULL
668 * pointer as last argument. So we need to use a dummy acl
669 * struct here (we make it static so it does not need to be
670 * instantiated or malloced each time this function is
671 * called). Btw: the count parameter does not seem to matter...
673 *count = acl(CONST_DISCARD(char *, name), ACL_CNT, 0, &dummy_ace);
674 if (*count < 0) {
675 DEBUG(10, ("acl ACL_CNT failed: %s\n", strerror(errno)));
676 goto done;
678 *hpux_acl = hpux_acl_init(*count);
679 if (*hpux_acl == NULL) {
680 DEBUG(10, ("error allocating memory for hpux acl...\n"));
681 goto done;
683 *count = acl(CONST_DISCARD(char *, name), ACL_GET, *count, *hpux_acl);
684 if (*count < 0) {
685 DEBUG(10, ("acl ACL_GET failed: %s\n", strerror(errno)));
686 goto done;
688 result = True;
690 done:
691 DEBUG(10, ("hpux_acl_get_file %s.\n",
692 ((result == True) ? "succeeded" : "failed" )));
693 return result;
700 * Add entries to a hpux ACL.
702 * Entries are directly added to the hpuxacl parameter.
703 * if memory allocation fails, this may result in hpuxacl
704 * being NULL. if the resulting acl is to be checked and is
705 * not valid, it is kept in hpuxacl but False is returned.
707 * The type of ACEs (access/default) to be added to the ACL can
708 * be selected via the type parameter.
709 * I use the SMB_ACL_TYPE_T type here. Since SMB_ACL_TYPE_ACCESS
710 * is defined as "0", this means that one can only add either
711 * access or default ACEs from the given ACL, not both at the same
712 * time. If it should become necessary to add all of an ACL, one
713 * would have to replace this parameter by another type.
715 static bool hpux_add_to_acl(HPUX_ACL_T *hpux_acl, int *count,
716 HPUX_ACL_T add_acl, int add_count,
717 SMB_ACL_TYPE_T type)
719 int i;
721 if ((type != SMB_ACL_TYPE_ACCESS) && (type != SMB_ACL_TYPE_DEFAULT))
723 DEBUG(10, ("invalid acl type given: %d\n", type));
724 errno = EINVAL;
725 return False;
727 for (i = 0; i < add_count; i++) {
728 if (!_IS_OF_TYPE(add_acl[i], type)) {
729 continue;
731 ADD_TO_ARRAY(NULL, HPUX_ACE_T, add_acl[i],
732 hpux_acl, count);
733 if (hpux_acl == NULL) {
734 DEBUG(10, ("error enlarging acl.\n"));
735 errno = ENOMEM;
736 return False;
739 return True;
744 * sort the ACL and check it for validity
746 * [original comment from lib/sysacls.c:]
748 * if it's a minimal ACL with only 4 entries then we
749 * need to recalculate the mask permissions to make
750 * sure that they are the same as the GROUP_OBJ
751 * permissions as required by the UnixWare acl() system call.
753 * (note: since POSIX allows minimal ACLs which only contain
754 * 3 entries - ie there is no mask entry - we should, in theory,
755 * check for this and add a mask entry if necessary - however
756 * we "know" that the caller of this interface always specifies
757 * a mask, so in practice "this never happens" (tm) - if it *does*
758 * happen aclsort() will fail and return an error and someone will
759 * have to fix it...)
761 static bool hpux_acl_sort(HPUX_ACL_T hpux_acl, int count)
763 int fixmask = (count <= 4);
765 if (hpux_internal_aclsort(count, fixmask, hpux_acl) != 0) {
766 errno = EINVAL;
767 return False;
769 return True;
774 * Helpers for hpux_internal_aclsort:
775 * - hpux_count_obj
776 * - hpux_swap_acl_entries
777 * - hpux_prohibited_duplicate_type
778 * - hpux_get_needed_class_perm
781 /* hpux_count_obj:
782 * Counts the different number of objects in a given array of ACL
783 * structures.
784 * Inputs:
786 * acl_count - Count of ACLs in the array of ACL strucutres.
787 * aclp - Array of ACL structures.
788 * acl_type_count - Pointer to acl_types structure. Should already be
789 * allocated.
790 * Output:
792 * acl_type_count - This structure is filled up with counts of various
793 * acl types.
796 static void hpux_count_obj(int acl_count, HPUX_ACL_T aclp, struct hpux_acl_types *acl_type_count)
798 int i;
800 memset(acl_type_count, 0, sizeof(struct hpux_acl_types));
802 for(i=0;i<acl_count;i++) {
803 switch(aclp[i].a_type) {
804 case USER:
805 acl_type_count->n_user++;
806 break;
807 case USER_OBJ:
808 acl_type_count->n_user_obj++;
809 break;
810 case DEF_USER_OBJ:
811 acl_type_count->n_def_user_obj++;
812 break;
813 case GROUP:
814 acl_type_count->n_group++;
815 break;
816 case GROUP_OBJ:
817 acl_type_count->n_group_obj++;
818 break;
819 case DEF_GROUP_OBJ:
820 acl_type_count->n_def_group_obj++;
821 break;
822 case OTHER_OBJ:
823 acl_type_count->n_other_obj++;
824 break;
825 case DEF_OTHER_OBJ:
826 acl_type_count->n_def_other_obj++;
827 break;
828 case CLASS_OBJ:
829 acl_type_count->n_class_obj++;
830 break;
831 case DEF_CLASS_OBJ:
832 acl_type_count->n_def_class_obj++;
833 break;
834 case DEF_USER:
835 acl_type_count->n_def_user++;
836 break;
837 case DEF_GROUP:
838 acl_type_count->n_def_group++;
839 break;
840 default:
841 acl_type_count->n_illegal_obj++;
842 break;
847 /* hpux_swap_acl_entries: Swaps two ACL entries.
849 * Inputs: aclp0, aclp1 - ACL entries to be swapped.
852 static void hpux_swap_acl_entries(HPUX_ACE_T *aclp0, HPUX_ACE_T *aclp1)
854 HPUX_ACE_T temp_acl;
856 temp_acl.a_type = aclp0->a_type;
857 temp_acl.a_id = aclp0->a_id;
858 temp_acl.a_perm = aclp0->a_perm;
860 aclp0->a_type = aclp1->a_type;
861 aclp0->a_id = aclp1->a_id;
862 aclp0->a_perm = aclp1->a_perm;
864 aclp1->a_type = temp_acl.a_type;
865 aclp1->a_id = temp_acl.a_id;
866 aclp1->a_perm = temp_acl.a_perm;
869 /* hpux_prohibited_duplicate_type
870 * Identifies if given ACL type can have duplicate entries or
871 * not.
873 * Inputs: acl_type - ACL Type.
875 * Outputs:
877 * Return..
879 * True - If the ACL type matches any of the prohibited types.
880 * False - If the ACL type doesn't match any of the prohibited types.
883 static bool hpux_prohibited_duplicate_type(int acl_type)
885 switch(acl_type) {
886 case USER:
887 case GROUP:
888 case DEF_USER:
889 case DEF_GROUP:
890 return True;
891 default:
892 return False;
896 /* hpux_get_needed_class_perm
897 * Returns the permissions of a ACL structure only if the ACL
898 * type matches one of the pre-determined types for computing
899 * CLASS_OBJ permissions.
901 * Inputs: aclp - Pointer to ACL structure.
904 static int hpux_get_needed_class_perm(struct acl *aclp)
906 switch(aclp->a_type) {
907 case USER:
908 case GROUP_OBJ:
909 case GROUP:
910 case DEF_USER_OBJ:
911 case DEF_USER:
912 case DEF_GROUP_OBJ:
913 case DEF_GROUP:
914 case DEF_CLASS_OBJ:
915 case DEF_OTHER_OBJ:
916 return aclp->a_perm;
917 default:
918 return 0;
922 /* hpux_internal_aclsort: aclsort for HPUX.
924 * -> The aclsort() system call is availabe on the latest HPUX General
925 * -> Patch Bundles. So for HPUX, we developed our version of aclsort
926 * -> function. Because, we don't want to update to a new
927 * -> HPUX GR bundle just for aclsort() call.
929 * aclsort sorts the array of ACL structures as per the description in
930 * aclsort man page. Refer to aclsort man page for more details
932 * Inputs:
934 * acl_count - Count of ACLs in the array of ACL structures.
935 * calclass - If this is not zero, then we compute the CLASS_OBJ
936 * permissions.
937 * aclp - Array of ACL structures.
939 * Outputs:
941 * aclp - Sorted array of ACL structures.
943 * Outputs:
945 * Returns 0 for success -1 for failure. Prints a message to the Samba
946 * debug log in case of failure.
949 static int hpux_internal_aclsort(int acl_count, int calclass, HPUX_ACL_T aclp)
951 struct hpux_acl_types acl_obj_count;
952 int n_class_obj_perm = 0;
953 int i, j;
955 DEBUG(10,("Entering hpux_internal_aclsort. (calclass = %d)\n", calclass));
957 if (hpux_aclsort_call_present()) {
958 DEBUG(10, ("calling hpux aclsort\n"));
959 return aclsort(acl_count, calclass, aclp);
962 DEBUG(10, ("using internal aclsort\n"));
964 if(!acl_count) {
965 DEBUG(10,("Zero acl count passed. Returning Success\n"));
966 return 0;
969 if(aclp == NULL) {
970 DEBUG(0,("Null ACL pointer in hpux_acl_sort. Returning Failure. \n"));
971 return -1;
974 /* Count different types of ACLs in the ACLs array */
976 hpux_count_obj(acl_count, aclp, &acl_obj_count);
978 /* There should be only one entry each of type USER_OBJ, GROUP_OBJ,
979 * CLASS_OBJ and OTHER_OBJ
982 if ( (acl_obj_count.n_user_obj != 1) ||
983 (acl_obj_count.n_group_obj != 1) ||
984 (acl_obj_count.n_class_obj != 1) ||
985 (acl_obj_count.n_other_obj != 1) )
987 DEBUG(0,("hpux_internal_aclsort: More than one entry or no entries for \
988 USER OBJ or GROUP_OBJ or OTHER_OBJ or CLASS_OBJ\n"));
989 return -1;
992 /* If any of the default objects are present, there should be only
993 * one of them each.
996 if ( (acl_obj_count.n_def_user_obj > 1) ||
997 (acl_obj_count.n_def_group_obj > 1) ||
998 (acl_obj_count.n_def_other_obj > 1) ||
999 (acl_obj_count.n_def_class_obj > 1) )
1001 DEBUG(0,("hpux_internal_aclsort: More than one entry for DEF_CLASS_OBJ \
1002 or DEF_USER_OBJ or DEF_GROUP_OBJ or DEF_OTHER_OBJ\n"));
1003 return -1;
1006 /* We now have proper number of OBJ and DEF_OBJ entries. Now sort the acl
1007 * structures.
1009 * Sorting crieteria - First sort by ACL type. If there are multiple entries of
1010 * same ACL type, sort by ACL id.
1012 * I am using the trival kind of sorting method here because, performance isn't
1013 * really effected by the ACLs feature. More over there aren't going to be more
1014 * than 17 entries on HPUX.
1017 for(i=0; i<acl_count;i++) {
1018 for (j=i+1; j<acl_count; j++) {
1019 if( aclp[i].a_type > aclp[j].a_type ) {
1020 /* ACL entries out of order, swap them */
1021 hpux_swap_acl_entries((aclp+i), (aclp+j));
1022 } else if ( aclp[i].a_type == aclp[j].a_type ) {
1023 /* ACL entries of same type, sort by id */
1024 if(aclp[i].a_id > aclp[j].a_id) {
1025 hpux_swap_acl_entries((aclp+i), (aclp+j));
1026 } else if (aclp[i].a_id == aclp[j].a_id) {
1027 /* We have a duplicate entry. */
1028 if(hpux_prohibited_duplicate_type(aclp[i].a_type)) {
1029 DEBUG(0, ("hpux_internal_aclsort: Duplicate entry: Type(hex): %x Id: %d\n",
1030 aclp[i].a_type, aclp[i].a_id));
1031 return -1;
1038 /* set the class obj permissions to the computed one. */
1039 if(calclass) {
1040 int n_class_obj_index = -1;
1042 for(i=0;i<acl_count;i++) {
1043 n_class_obj_perm |= hpux_get_needed_class_perm((aclp+i));
1045 if(aclp[i].a_type == CLASS_OBJ)
1046 n_class_obj_index = i;
1048 aclp[n_class_obj_index].a_perm = n_class_obj_perm;
1051 return 0;
1056 * hpux_acl_call_present:
1058 * This checks if the POSIX ACL system call is defined
1059 * which basically corresponds to whether JFS 3.3 or
1060 * higher is installed. If acl() was called when it
1061 * isn't defined, it causes the process to core dump
1062 * so it is important to check this and avoid acl()
1063 * calls if it isn't there.
1066 static bool hpux_acl_call_present(void)
1069 shl_t handle = NULL;
1070 void *value;
1071 int ret_val=0;
1072 static bool already_checked = False;
1074 if(already_checked)
1075 return True;
1077 errno = 0;
1079 ret_val = shl_findsym(&handle, "acl", TYPE_PROCEDURE, &value);
1081 if(ret_val != 0) {
1082 DEBUG(5, ("hpux_acl_call_present: shl_findsym() returned %d, errno = %d, error %s\n",
1083 ret_val, errno, strerror(errno)));
1084 DEBUG(5,("hpux_acl_call_present: acl() system call is not present. Check if you have JFS 3.3 and above?\n"));
1085 errno = ENOSYS;
1086 return False;
1089 DEBUG(10,("hpux_acl_call_present: acl() system call is present. We have JFS 3.3 or above \n"));
1091 already_checked = True;
1092 return True;
1096 * runtime check for presence of aclsort library call.
1097 * same code as for acl call. if there are more of these,
1098 * a dispatcher function could be handy...
1101 static bool hpux_aclsort_call_present(void)
1103 shl_t handle = NULL;
1104 void *value;
1105 int ret_val = 0;
1106 static bool already_checked = False;
1108 if (already_checked) {
1109 return True;
1112 errno = 0;
1113 ret_val = shl_findsym(&handle, "aclsort", TYPE_PROCEDURE, &value);
1114 if (ret_val != 0) {
1115 DEBUG(5, ("hpux_aclsort_call_present: shl_findsym "
1116 "returned %d, errno = %d, error %s",
1117 ret_val, errno, strerror(errno)));
1118 DEBUG(5, ("hpux_aclsort_call_present: "
1119 "aclsort() function not available.\n"));
1120 return False;
1122 DEBUG(10,("hpux_aclsort_call_present: aclsort() function present.\n"));
1123 already_checked = True;
1124 return True;
1127 #if 0
1129 * acl check function:
1130 * unused at the moment but could be used to get more
1131 * concrete error messages for debugging...
1132 * (acl sort just says that the acl is invalid...)
1134 static bool hpux_acl_check(HPUX_ACL_T hpux_acl, int count)
1136 int check_rc;
1137 int check_which;
1139 check_rc = aclcheck(hpux_acl, count, &check_which);
1140 if (check_rc != 0) {
1141 DEBUG(10, ("acl is not valid:\n"));
1142 DEBUGADD(10, (" - return code: %d\n", check_rc));
1143 DEBUGADD(10, (" - which: %d\n", check_which));
1144 if (check_which != -1) {
1145 DEBUGADD(10, (" - invalid entry:\n"));
1146 DEBUGADD(10, (" * type: %d:\n",
1147 hpux_acl[check_which].a_type));
1148 DEBUGADD(10, (" * id: %d\n",
1149 hpux_acl[check_which].a_id));
1150 DEBUGADD(10, (" * perm: 0o%o\n",
1151 hpux_acl[check_which].a_perm));
1153 return False;
1155 return True;
1157 #endif
1159 /* VFS operations structure */
1161 static struct vfs_fn_pointers hpuxacl_fns = {
1162 .sys_acl_get_file = hpuxacl_sys_acl_get_file,
1163 .sys_acl_get_fd = hpuxacl_sys_acl_get_fd,
1164 .sys_acl_set_file = hpuxacl_sys_acl_set_file,
1165 .sys_acl_set_fd = hpuxacl_sys_acl_set_fd,
1166 .sys_acl_delete_def_file = hpuxacl_sys_acl_delete_def_file,
1169 NTSTATUS vfs_hpuxacl_init(void)
1171 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "hpuxacl",
1172 &hpuxacl_fns);
1175 /* ENTE */