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_SOLARIS_UNIXWARE_ACLS)
30 #include "modules/vfs_solarisacl.h"
33 #if defined(HAVE_HPUX_ACLS)
34 #include "modules/vfs_hpuxacl.h"
38 #define DBGC_CLASS DBGC_ACLS
41 * Note that while this code implements sufficient functionality
42 * to support the sys_acl_* interfaces it does not provide all
43 * of the semantics of the POSIX ACL interfaces.
45 * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned
46 * from a call to sys_acl_get_entry() should not be assumed to be
47 * valid after calling any of the following functions, which may
48 * reorder the entries in the ACL.
55 int sys_acl_get_entry(SMB_ACL_T acl_d
, int entry_id
, SMB_ACL_ENTRY_T
*entry_p
)
57 if (entry_id
!= SMB_ACL_FIRST_ENTRY
&& entry_id
!= SMB_ACL_NEXT_ENTRY
) {
62 if (entry_p
== NULL
) {
67 if (entry_id
== SMB_ACL_FIRST_ENTRY
) {
71 if (acl_d
->next
< 0) {
76 if (acl_d
->next
>= acl_d
->count
) {
80 *entry_p
= &acl_d
->acl
[acl_d
->next
++];
85 int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d
, SMB_ACL_TAG_T
*type_p
)
87 *type_p
= entry_d
->a_type
;
92 int sys_acl_get_permset(SMB_ACL_ENTRY_T entry_d
, SMB_ACL_PERMSET_T
*permset_p
)
94 *permset_p
= &entry_d
->a_perm
;
99 void *sys_acl_get_qualifier(SMB_ACL_ENTRY_T entry_d
)
101 if (entry_d
->a_type
== SMB_ACL_USER
) {
102 return &entry_d
->info
.user
.uid
;
105 if (entry_d
->a_type
== SMB_ACL_GROUP
) {
106 return &entry_d
->info
.group
.gid
;
113 int sys_acl_clear_perms(SMB_ACL_PERMSET_T permset_d
)
120 int sys_acl_add_perm(SMB_ACL_PERMSET_T permset_d
, SMB_ACL_PERM_T perm
)
122 if (perm
!= SMB_ACL_READ
&& perm
!= SMB_ACL_WRITE
123 && perm
!= SMB_ACL_EXECUTE
) {
128 if (permset_d
== NULL
) {
138 int sys_acl_get_perm(SMB_ACL_PERMSET_T permset_d
, SMB_ACL_PERM_T perm
)
140 return *permset_d
& perm
;
143 char *sys_acl_to_text(const struct smb_acl_t
*acl_d
, ssize_t
*len_p
)
150 * use an initial estimate of 20 bytes per ACL entry
151 * when allocating memory for the text representation
155 maxlen
= 20 * acl_d
->count
;
156 if ((text
= (char *)SMB_MALLOC(maxlen
)) == NULL
) {
161 for (i
= 0; i
< acl_d
->count
; i
++) {
162 struct smb_acl_entry
*ap
= &acl_d
->acl
[i
];
171 switch (ap
->a_type
) {
173 * for debugging purposes it's probably more
174 * useful to dump unknown tag types rather
175 * than just returning an error
178 slprintf(tagbuf
, sizeof(tagbuf
)-1, "0x%x",
184 id
= uidtoname(ap
->info
.user
.uid
);
187 case SMB_ACL_USER_OBJ
:
192 if ((gr
= getgrgid(ap
->info
.group
.gid
)) == NULL
) {
193 slprintf(idbuf
, sizeof(idbuf
)-1, "%ld",
194 (long)ap
->info
.group
.gid
);
201 case SMB_ACL_GROUP_OBJ
:
215 perms
[0] = (ap
->a_perm
& SMB_ACL_READ
) ? 'r' : '-';
216 perms
[1] = (ap
->a_perm
& SMB_ACL_WRITE
) ? 'w' : '-';
217 perms
[2] = (ap
->a_perm
& SMB_ACL_EXECUTE
) ? 'x' : '-';
220 /* <tag> : <qualifier> : rwx \n \0 */
221 nbytes
= strlen(tag
) + 1 + strlen(id
) + 1 + 3 + 1 + 1;
224 * If this entry would overflow the buffer
225 * allocate enough additional memory for this
226 * entry and an estimate of another 20 bytes
227 * for each entry still to be processed
229 if ((len
+ nbytes
) > maxlen
) {
230 maxlen
+= nbytes
+ 20 * (acl_d
->count
- i
);
231 if ((text
= (char *)SMB_REALLOC(text
, maxlen
)) == NULL
) {
238 slprintf(&text
[len
], nbytes
, "%s:%s:%s\n", tag
, id
, perms
);
248 SMB_ACL_T
sys_acl_init(TALLOC_CTX
*mem_ctx
)
252 if ((a
= talloc(mem_ctx
, struct smb_acl_t
)) == NULL
) {
260 a
->acl
= talloc_array(a
, struct smb_acl_entry
, 0);
270 int sys_acl_create_entry(SMB_ACL_T
*acl_p
, SMB_ACL_ENTRY_T
*entry_p
)
273 SMB_ACL_ENTRY_T entry_d
;
274 struct smb_acl_entry
*acl
;
276 if (acl_p
== NULL
|| entry_p
== NULL
|| (acl_d
= *acl_p
) == NULL
) {
281 acl
= talloc_realloc(acl_d
, acl_d
->acl
, struct smb_acl_entry
, acl_d
->count
+1);
287 entry_d
= &acl_d
->acl
[acl_d
->count
];
288 entry_d
->a_type
= SMB_ACL_TAG_INVALID
;
296 int sys_acl_set_tag_type(SMB_ACL_ENTRY_T entry_d
, SMB_ACL_TAG_T tag_type
)
300 case SMB_ACL_USER_OBJ
:
302 case SMB_ACL_GROUP_OBJ
:
305 entry_d
->a_type
= tag_type
;
315 int sys_acl_set_qualifier(SMB_ACL_ENTRY_T entry_d
, void *qual_p
)
317 if (entry_d
->a_type
== SMB_ACL_USER
) {
318 entry_d
->info
.user
.uid
= *((uid_t
*)qual_p
);
321 if (entry_d
->a_type
== SMB_ACL_GROUP
) {
322 entry_d
->info
.group
.gid
= *((gid_t
*)qual_p
);
330 int sys_acl_set_permset(SMB_ACL_ENTRY_T entry_d
, SMB_ACL_PERMSET_T permset_d
)
332 if (*permset_d
& ~(SMB_ACL_READ
|SMB_ACL_WRITE
|SMB_ACL_EXECUTE
)) {
337 entry_d
->a_perm
= *permset_d
;
342 int sys_acl_free_text(char *text
)
348 int sys_acl_valid(SMB_ACL_T acl_d
)
355 * acl_get_file, acl_get_fd, acl_set_file, acl_set_fd and
356 * sys_acl_delete_def_fd are to be redirected to the default
357 * statically-bound acl vfs module, but they are replacable.
360 #if defined(HAVE_POSIX_ACLS)
362 SMB_ACL_T
sys_acl_get_file(vfs_handle_struct
*handle
,
363 const struct smb_filename
*smb_fname
,
367 return posixacl_sys_acl_get_file(handle
, smb_fname
, type
, mem_ctx
);
370 SMB_ACL_T
sys_acl_get_fd(vfs_handle_struct
*handle
, files_struct
*fsp
, TALLOC_CTX
*mem_ctx
)
372 return posixacl_sys_acl_get_fd(handle
, fsp
, mem_ctx
);
375 int sys_acl_set_file(vfs_handle_struct
*handle
,
376 const struct smb_filename
*smb_fname
,
380 return posixacl_sys_acl_set_file(handle
, smb_fname
, type
, acl_d
);
383 int sys_acl_set_fd(vfs_handle_struct
*handle
, files_struct
*fsp
,
386 return posixacl_sys_acl_set_fd(handle
, fsp
, SMB_ACL_TYPE_ACCESS
, acl_d
);
389 int sys_acl_delete_def_fd(vfs_handle_struct
*handle
,
392 return posixacl_sys_acl_delete_def_fd(handle
, fsp
);
395 #elif defined(HAVE_AIX_ACLS)
397 SMB_ACL_T
sys_acl_get_file(vfs_handle_struct
*handle
,
398 const struct smb_filename
*smb_fname
,
402 return aixacl_sys_acl_get_file(handle
, smb_fname
, type
, mem_ctx
);
405 SMB_ACL_T
sys_acl_get_fd(vfs_handle_struct
*handle
, files_struct
*fsp
,
408 return aixacl_sys_acl_get_fd(handle
, fsp
, mem_ctx
);
411 int sys_acl_set_file(vfs_handle_struct
*handle
,
412 const struct smb_filename
*smb_fname
,
416 return aixacl_sys_acl_set_file(handle
, smb_fname
, type
, acl_d
);
419 int sys_acl_set_fd(vfs_handle_struct
*handle
, files_struct
*fsp
,
422 return aixacl_sys_acl_set_fd(handle
, fsp
, acl_d
);
425 int sys_acl_delete_def_fd(vfs_handle_struct
*handle
,
428 return aixacl_sys_acl_delete_def_fd(handle
, fsp
);
430 #elif defined(HAVE_SOLARIS_UNIXWARE_ACLS)
432 SMB_ACL_T
sys_acl_get_file(vfs_handle_struct
*handle
,
433 const struct smb_filename
*smb_fname
,
437 return solarisacl_sys_acl_get_file(handle
, smb_fname
, type
,
441 SMB_ACL_T
sys_acl_get_fd(vfs_handle_struct
*handle
, files_struct
*fsp
,
444 return solarisacl_sys_acl_get_fd(handle
, fsp
,
448 int sys_acl_set_file(vfs_handle_struct
*handle
,
449 const struct smb_filename
*smb_fname
,
453 return solarisacl_sys_acl_set_file(handle
, smb_fname
, type
, acl_d
);
456 int sys_acl_set_fd(vfs_handle_struct
*handle
, files_struct
*fsp
,
459 return solarisacl_sys_acl_set_fd(handle
, fsp
, acl_d
);
462 int sys_acl_delete_def_fd(vfs_handle_struct
*handle
,
465 return solarisacl_sys_acl_delete_def_fd(handle
, fsp
);
467 #elif defined(HAVE_HPUX_ACLS)
469 SMB_ACL_T
sys_acl_get_file(vfs_handle_struct
*handle
,
470 const struct smb_filename
*smb_fname
,
474 return hpuxacl_sys_acl_get_file(handle
, smb_fname
, type
, mem_ctx
);
477 SMB_ACL_T
sys_acl_get_fd(vfs_handle_struct
*handle
, files_struct
*fsp
,
480 return hpuxacl_sys_acl_get_fd(handle
, fsp
, mem_ctx
);
483 int sys_acl_set_file(vfs_handle_struct
*handle
,
484 const struct smb_filename
*smb_fname
,
488 return hpuxacl_sys_acl_set_file(handle
, smb_fname
, type
, acl_d
);
491 int sys_acl_set_fd(vfs_handle_struct
*handle
, files_struct
*fsp
,
494 return hpuxacl_sys_acl_set_fd(handle
, fsp
, acl_d
);
497 int sys_acl_delete_def_fd(vfs_handle_struct
*handle
,
500 return hpuxacl_sys_acl_delete_def_fd(handle
, fsp
);
504 SMB_ACL_T
sys_acl_get_file(vfs_handle_struct
*handle
,
505 const struct smb_filename
*smb_fname
,
517 SMB_ACL_T
sys_acl_get_fd(vfs_handle_struct
*handle
, files_struct
*fsp
,
528 int sys_acl_set_file(vfs_handle_struct
*handle
,
529 const struct smb_filename
*smb_fname
,
541 int sys_acl_set_fd(vfs_handle_struct
*handle
, files_struct
*fsp
,
552 int sys_acl_delete_def_fd(vfs_handle_struct
*handle
,
564 /************************************************************************
565 Deliberately outside the ACL defines. Return 1 if this is a "no acls"
567 ************************************************************************/
569 int no_acl_syscall_error(int err
)
577 if (err
== ENOTSUP
) {