2 Unix SMB/CIFS implementation.
3 Samba system utilities for ACL support.
4 Copyright (C) Jeremy Allison 2000.
5 Copyright (C) Volker Lendecke 2006
6 Copyright (C) Michael Adam 2006,2008
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "system/passwd.h"
25 #if defined(HAVE_POSIX_ACLS)
26 #include "modules/vfs_posixacl.h"
29 #if defined(HAVE_TRU64_ACLS)
30 #include "modules/vfs_tru64acl.h"
33 #if defined(HAVE_SOLARIS_ACLS) || defined(HAVE_UNIXWARE_ACLS)
34 #include "modules/vfs_solarisacl.h"
37 #if defined(HAVE_HPUX_ACLS)
38 #include "modules/vfs_hpuxacl.h"
41 #if defined(HAVE_IRIX_ACLS)
42 #include "modules/vfs_irixacl.h"
46 #define DBGC_CLASS DBGC_ACLS
49 * Note that while this code implements sufficient functionality
50 * to support the sys_acl_* interfaces it does not provide all
51 * of the semantics of the POSIX ACL interfaces.
53 * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
54 * from a call to sys_acl_get_entry() should not be assumed to be
55 * valid after calling any of the following functions, which may
56 * reorder the entries in the ACL.
63 int sys_acl_get_entry(SMB_ACL_T acl_d
, int entry_id
, SMB_ACL_ENTRY_T
*entry_p
)
65 if (entry_id
!= SMB_ACL_FIRST_ENTRY
&& entry_id
!= SMB_ACL_NEXT_ENTRY
) {
70 if (entry_p
== NULL
) {
75 if (entry_id
== SMB_ACL_FIRST_ENTRY
) {
79 if (acl_d
->next
< 0) {
84 if (acl_d
->next
>= acl_d
->count
) {
88 *entry_p
= &acl_d
->acl
[acl_d
->next
++];
93 int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d
, SMB_ACL_TAG_T
*type_p
)
95 *type_p
= entry_d
->a_type
;
100 int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d
, SMB_ACL_PERMSET_T
*permset_p
)
102 *permset_p
= &entry_d
->a_perm
;
107 void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d
)
109 if (entry_d
->a_type
== SMB_ACL_USER
) {
110 return &entry_d
->uid
;
113 if (entry_d
->a_type
== SMB_ACL_GROUP
) {
114 return &entry_d
->gid
;
121 int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d
)
128 int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d
, SMB_ACL_PERM_T perm
)
130 if (perm
!= SMB_ACL_READ
&& perm
!= SMB_ACL_WRITE
131 && perm
!= SMB_ACL_EXECUTE
) {
136 if (permset_d
== NULL
) {
146 int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d
, SMB_ACL_PERM_T perm
)
148 return *permset_d
& perm
;
151 char *sys_acl_to_text(const struct smb_acl_t
*acl_d
, ssize_t
*len_p
)
158 * use an initial estimate of 20 bytes per ACL entry
159 * when allocating memory for the text representation
163 maxlen
= 20 * acl_d
->count
;
164 if ((text
= (char *)SMB_MALLOC(maxlen
)) == NULL
) {
169 for (i
= 0; i
< acl_d
->count
; i
++) {
170 struct smb_acl_entry
*ap
= &acl_d
->acl
[i
];
179 switch (ap
->a_type
) {
181 * for debugging purposes it's probably more
182 * useful to dump unknown tag types rather
183 * than just returning an error
186 slprintf(tagbuf
, sizeof(tagbuf
)-1, "0x%x",
192 id
= uidtoname(ap
->uid
);
193 case SMB_ACL_USER_OBJ
:
198 if ((gr
= getgrgid(ap
->gid
)) == NULL
) {
199 slprintf(idbuf
, sizeof(idbuf
)-1, "%ld",
205 case SMB_ACL_GROUP_OBJ
:
219 perms
[0] = (ap
->a_perm
& SMB_ACL_READ
) ? 'r' : '-';
220 perms
[1] = (ap
->a_perm
& SMB_ACL_WRITE
) ? 'w' : '-';
221 perms
[2] = (ap
->a_perm
& SMB_ACL_EXECUTE
) ? 'x' : '-';
224 /* <tag> : <qualifier> : rwx \n \0 */
225 nbytes
= strlen(tag
) + 1 + strlen(id
) + 1 + 3 + 1 + 1;
228 * If this entry would overflow the buffer
229 * allocate enough additional memory for this
230 * entry and an estimate of another 20 bytes
231 * for each entry still to be processed
233 if ((len
+ nbytes
) > maxlen
) {
234 maxlen
+= nbytes
+ 20 * (acl_d
->count
- i
);
235 if ((text
= (char *)SMB_REALLOC(text
, maxlen
)) == NULL
) {
242 slprintf(&text
[len
], nbytes
, "%s:%s:%s\n", tag
, id
, perms
);
252 SMB_ACL_T
sys_acl_init(int count
)
262 * note that since the definition of the structure pointed
263 * to by the SMB_ACL_T includes the first element of the
264 * acl[] array, this actually allocates an ACL with room
265 * for (count+1) entries
267 if ((a
= (struct smb_acl_t
*)SMB_MALLOC(
268 sizeof(struct smb_acl_t
) +
269 count
* sizeof(struct smb_acl_entry
))) == NULL
) {
281 int sys_acl_create_entry(SMB_ACL_T
*acl_p
, SMB_ACL_ENTRY_T
*entry_p
)
284 SMB_ACL_ENTRY_T entry_d
;
286 if (acl_p
== NULL
|| entry_p
== NULL
|| (acl_d
= *acl_p
) == NULL
) {
291 if (acl_d
->count
>= acl_d
->size
) {
296 entry_d
= &acl_d
->acl
[acl_d
->count
++];
297 entry_d
->a_type
= SMB_ACL_TAG_INVALID
;
306 int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d
, SMB_ACL_TAG_T tag_type
)
310 case SMB_ACL_USER_OBJ
:
312 case SMB_ACL_GROUP_OBJ
:
315 entry_d
->a_type
= tag_type
;
325 int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d
, void *qual_p
)
327 if (entry_d
->a_type
== SMB_ACL_USER
) {
328 entry_d
->uid
= *((uid_t
*)qual_p
);
331 if (entry_d
->a_type
== SMB_ACL_GROUP
) {
332 entry_d
->gid
= *((gid_t
*)qual_p
);
340 int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d
, SMB_ACL_PERMSET_T permset_d
)
342 if (*permset_d
& ~(SMB_ACL_READ
|SMB_ACL_WRITE
|SMB_ACL_EXECUTE
)) {
347 entry_d
->a_perm
= *permset_d
;
352 int sys_acl_free_text(char *text
)
358 int sys_acl_free_acl(SMB_ACL_T acl_d
)
364 int sys_acl_free_qualifier(void *qual
, SMB_ACL_TAG_T tagtype
)
369 int sys_acl_valid(SMB_ACL_T acl_d
)
376 * acl_get_file, acl_get_fd, acl_set_file, acl_set_fd and
377 * sys_acl_delete_def_file are to be redirected to the default
378 * statically-bound acl vfs module, but they are replacable.
381 #if defined(HAVE_POSIX_ACLS)
383 SMB_ACL_T
sys_acl_get_file(vfs_handle_struct
*handle
,
384 const char *path_p
, SMB_ACL_TYPE_T type
)
386 return posixacl_sys_acl_get_file(handle
, path_p
, type
);
389 SMB_ACL_T
sys_acl_get_fd(vfs_handle_struct
*handle
, files_struct
*fsp
)
391 return posixacl_sys_acl_get_fd(handle
, fsp
);
394 int sys_acl_set_file(vfs_handle_struct
*handle
,
395 const char *name
, SMB_ACL_TYPE_T type
, SMB_ACL_T acl_d
)
397 return posixacl_sys_acl_set_file(handle
, name
, type
, acl_d
);
400 int sys_acl_set_fd(vfs_handle_struct
*handle
, files_struct
*fsp
,
403 return posixacl_sys_acl_set_fd(handle
, fsp
, acl_d
);
406 int sys_acl_delete_def_file(vfs_handle_struct
*handle
,
409 return posixacl_sys_acl_delete_def_file(handle
, path
);
412 #elif defined(HAVE_AIX_ACLS)
414 SMB_ACL_T
sys_acl_get_file(vfs_handle_struct
*handle
,
415 const char *path_p
, SMB_ACL_TYPE_T type
)
417 return aixacl_sys_acl_get_file(handle
, path_p
, type
);
420 SMB_ACL_T
sys_acl_get_fd(vfs_handle_struct
*handle
, files_struct
*fsp
)
422 return aixacl_sys_acl_get_fd(handle
, fsp
);
425 int sys_acl_set_file(vfs_handle_struct
*handle
,
426 const char *name
, SMB_ACL_TYPE_T type
, SMB_ACL_T acl_d
)
428 return aixacl_sys_acl_set_file(handle
, name
, type
, acl_d
);
431 int sys_acl_set_fd(vfs_handle_struct
*handle
, files_struct
*fsp
,
434 return aixacl_sys_acl_set_fd(handle
, fsp
, acl_d
);
437 int sys_acl_delete_def_file(vfs_handle_struct
*handle
,
440 return aixacl_sys_acl_delete_def_file(handle
, path
);
443 #elif defined(HAVE_TRU64_ACLS)
445 SMB_ACL_T
sys_acl_get_file(vfs_handle_struct
*handle
,
446 const char *path_p
, SMB_ACL_TYPE_T type
)
448 return tru64acl_sys_acl_get_file(handle
, path_p
, type
);
451 SMB_ACL_T
sys_acl_get_fd(vfs_handle_struct
*handle
, files_struct
*fsp
)
453 return tru64acl_sys_acl_get_fd(handle
, fsp
);
456 int sys_acl_set_file(vfs_handle_struct
*handle
,
457 const char *name
, SMB_ACL_TYPE_T type
, SMB_ACL_T acl_d
)
459 return tru64acl_sys_acl_set_file(handle
, name
, type
, acl_d
);
462 int sys_acl_set_fd(vfs_handle_struct
*handle
, files_struct
*fsp
,
465 return tru64acl_sys_acl_set_fd(handle
, fsp
, acl_d
);
468 int sys_acl_delete_def_file(vfs_handle_struct
*handle
,
471 return tru64acl_sys_acl_delete_def_file(handle
, path
);
474 #elif defined(HAVE_SOLARIS_ACLS) || defined(HAVE_UNIXWARE_ACLS)
476 SMB_ACL_T
sys_acl_get_file(vfs_handle_struct
*handle
,
477 const char *path_p
, SMB_ACL_TYPE_T type
)
479 return solarisacl_sys_acl_get_file(handle
, path_p
, type
);
482 SMB_ACL_T
sys_acl_get_fd(vfs_handle_struct
*handle
, files_struct
*fsp
)
484 return solarisacl_sys_acl_get_fd(handle
, fsp
);
487 int sys_acl_set_file(vfs_handle_struct
*handle
,
488 const char *name
, SMB_ACL_TYPE_T type
, SMB_ACL_T acl_d
)
490 return solarisacl_sys_acl_set_file(handle
, name
, type
, acl_d
);
493 int sys_acl_set_fd(vfs_handle_struct
*handle
, files_struct
*fsp
,
496 return solarisacl_sys_acl_set_fd(handle
, fsp
, acl_d
);
499 int sys_acl_delete_def_file(vfs_handle_struct
*handle
,
502 return solarisacl_sys_acl_delete_def_file(handle
, path
);
505 #elif defined(HAVE_HPUX_ACLS)
507 SMB_ACL_T
sys_acl_get_file(vfs_handle_struct
*handle
,
508 const char *path_p
, SMB_ACL_TYPE_T type
)
510 return hpuxacl_sys_acl_get_file(handle
, path_p
, type
);
513 SMB_ACL_T
sys_acl_get_fd(vfs_handle_struct
*handle
, files_struct
*fsp
)
515 return hpuxacl_sys_acl_get_fd(handle
, fsp
);
518 int sys_acl_set_file(vfs_handle_struct
*handle
,
519 const char *name
, SMB_ACL_TYPE_T type
, SMB_ACL_T acl_d
)
521 return hpuxacl_sys_acl_set_file(handle
, name
, type
, acl_d
);
524 int sys_acl_set_fd(vfs_handle_struct
*handle
, files_struct
*fsp
,
527 return hpuxacl_sys_acl_set_fd(handle
, fsp
, acl_d
);
530 int sys_acl_delete_def_file(vfs_handle_struct
*handle
,
533 return hpuxacl_sys_acl_delete_def_file(handle
, path
);
536 #elif defined(HAVE_IRIX_ACLS)
538 SMB_ACL_T
sys_acl_get_file(vfs_handle_struct
*handle
,
539 const char *path_p
, SMB_ACL_TYPE_T type
)
541 return irixacl_sys_acl_get_file(handle
, path_p
, type
);
544 SMB_ACL_T
sys_acl_get_fd(vfs_handle_struct
*handle
, files_struct
*fsp
)
546 return irixacl_sys_acl_get_fd(handle
, fsp
);
549 int sys_acl_set_file(vfs_handle_struct
*handle
,
550 const char *name
, SMB_ACL_TYPE_T type
, SMB_ACL_T acl_d
)
552 return irixacl_sys_acl_set_file(handle
, name
, type
, acl_d
);
555 int sys_acl_set_fd(vfs_handle_struct
*handle
, files_struct
*fsp
,
558 return irixacl_sys_acl_set_fd(handle
, fsp
, acl_d
);
561 int sys_acl_delete_def_file(vfs_handle_struct
*handle
,
564 return irixacl_sys_acl_delete_def_file(handle
, path
);
569 SMB_ACL_T
sys_acl_get_file(vfs_handle_struct
*handle
,
570 const char *path_p
, SMB_ACL_TYPE_T type
)
580 SMB_ACL_T
sys_acl_get_fd(vfs_handle_struct
*handle
, files_struct
*fsp
)
590 int sys_acl_set_file(vfs_handle_struct
*handle
,
591 const char *name
, SMB_ACL_TYPE_T type
, SMB_ACL_T acl_d
)
601 int sys_acl_set_fd(vfs_handle_struct
*handle
, files_struct
*fsp
,
612 int sys_acl_delete_def_file(vfs_handle_struct
*handle
,
625 /************************************************************************
626 Deliberately outside the ACL defines. Return 1 if this is a "no acls"
628 ************************************************************************/
630 int no_acl_syscall_error(int err
)
638 if (err
== ENOTSUP
) {