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