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