2 * Convert AFS acls to NT acls and vice versa.
4 * Copyright (C) Volker Lendecke, 2003
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "system/filesys.h"
22 #include "smbd/smbd.h"
23 #include "../librpc/gen_ndr/lsa.h"
24 #include "../libcli/security/security.h"
25 #include "../libcli/security/dom_sid.h"
29 #define DBGC_CLASS DBGC_VFS
34 #include <afs/venus.h>
35 #include <afs/prs_fs.h>
39 extern const struct dom_sid global_sid_World
;
40 extern const struct dom_sid global_sid_Builtin_Administrators
;
41 extern const struct dom_sid global_sid_Builtin_Backup_Operators
;
42 extern const struct dom_sid global_sid_Authenticated_Users
;
43 extern const struct dom_sid global_sid_NULL
;
45 static char space_replacement
= '%';
47 /* Do we expect SIDs as pts names? */
50 extern int afs_syscall(int, const char *, int, char *, int);
56 enum lsa_SidType type
;
65 struct afs_ace
*acelist
;
70 uint16 in_size
, out_size
;
74 static bool init_afs_acl(struct afs_acl
*acl
)
77 acl
->ctx
= talloc_init("afs_acl");
78 if (acl
->ctx
== NULL
) {
79 DEBUG(10, ("Could not init afs_acl"));
85 static void free_afs_acl(struct afs_acl
*acl
)
88 talloc_destroy(acl
->ctx
);
94 static struct afs_ace
*clone_afs_ace(TALLOC_CTX
*mem_ctx
, struct afs_ace
*ace
)
96 struct afs_ace
*result
= talloc(mem_ctx
, struct afs_ace
);
104 result
->name
= talloc_strdup(mem_ctx
, ace
->name
);
106 if (result
->name
== NULL
) {
113 static struct afs_ace
*new_afs_ace(TALLOC_CTX
*mem_ctx
,
115 const char *name
, uint32 rights
)
118 enum lsa_SidType type
;
119 struct afs_ace
*result
;
121 if (strcmp(name
, "system:administrators") == 0) {
123 sid_copy(&sid
, &global_sid_Builtin_Administrators
);
124 type
= SID_NAME_ALIAS
;
126 } else if (strcmp(name
, "system:anyuser") == 0) {
128 sid_copy(&sid
, &global_sid_World
);
129 type
= SID_NAME_ALIAS
;
131 } else if (strcmp(name
, "system:authuser") == 0) {
133 sid_copy(&sid
, &global_sid_Authenticated_Users
);
134 type
= SID_NAME_WKN_GRP
;
136 } else if (strcmp(name
, "system:backup") == 0) {
138 sid_copy(&sid
, &global_sid_Builtin_Backup_Operators
);
139 type
= SID_NAME_ALIAS
;
142 /* All PTS users/groups are expressed as SIDs */
144 sid_copy(&sid
, &global_sid_NULL
);
145 type
= SID_NAME_UNKNOWN
;
147 if (string_to_sid(&sid
, name
)) {
148 const char *user
, *domain
;
149 /* We have to find the type, look up the SID */
150 lookup_sid(talloc_tos(), &sid
,
151 &domain
, &user
, &type
);
156 const char *domain
, *uname
;
159 p
= strchr_m(name
, *lp_winbind_separator());
164 if (!lookup_name(talloc_tos(), name
, LOOKUP_NAME_ALL
,
165 &domain
, &uname
, &sid
, &type
)) {
166 DEBUG(10, ("Could not find AFS user %s\n", name
));
168 sid_copy(&sid
, &global_sid_NULL
);
169 type
= SID_NAME_UNKNOWN
;
174 result
= talloc(mem_ctx
, struct afs_ace
);
176 if (result
== NULL
) {
177 DEBUG(0, ("Could not talloc AFS ace\n"));
181 result
->name
= talloc_strdup(mem_ctx
, name
);
182 if (result
->name
== NULL
) {
183 DEBUG(0, ("Could not talloc AFS ace name\n"));
190 result
->positive
= positive
;
191 result
->rights
= rights
;
196 static void add_afs_ace(struct afs_acl
*acl
,
198 const char *name
, uint32 rights
)
202 for (ace
= acl
->acelist
; ace
!= NULL
; ace
= ace
->next
) {
203 if ((ace
->positive
== positive
) &&
204 (strequal(ace
->name
, name
))) {
205 ace
->rights
|= rights
;
210 ace
= new_afs_ace(acl
->ctx
, positive
, name
, rights
);
212 ace
->next
= acl
->acelist
;
217 DEBUG(10, ("add_afs_ace: Added %s entry for %s with rights %d\n",
218 ace
->positive
?"positive":"negative",
219 ace
->name
, ace
->rights
));
222 /* AFS ACLs in string form are a long string of fields delimited with \n.
224 * First line: Number of positive entries
225 * Second line: Number of negative entries
226 * Third and following lines: The entries themselves
228 * An ACE is a line of two fields, delimited by \t.
231 * Second field: Rights
234 static bool parse_afs_acl(struct afs_acl
*acl
, const char *acl_str
)
242 strlcpy(str
, acl_str
, MAXSIZE
);
244 if (sscanf(p
, "%d", &nplus
) != 1)
247 DEBUG(10, ("Found %d positive entries\n", nplus
));
249 if ((p
= strchr(p
, '\n')) == NULL
)
253 if (sscanf(p
, "%d", &nminus
) != 1)
256 DEBUG(10, ("Found %d negative entries\n", nminus
));
258 if ((p
= strchr(p
, '\n')) == NULL
)
262 for (aces
= nplus
+nminus
; aces
> 0; aces
--)
272 if ((p
= strchr(p
, '\t')) == NULL
)
277 if (sscanf(p
, "%d", &rights
) != 1)
280 if ((p
= strchr(p
, '\n')) == NULL
)
284 fstrcpy(name
, namep
);
286 while ((space
= strchr_m(name
, space_replacement
)) != NULL
)
289 add_afs_ace(acl
, nplus
>0, name
, rights
);
297 static bool unparse_afs_acl(struct afs_acl
*acl
, char *acl_str
)
299 /* TODO: String length checks!!!! */
304 struct afs_ace
*ace
= acl
->acelist
;
308 while (ace
!= NULL
) {
316 fstr_sprintf(line
, "%d\n", positives
);
317 if (strlcat(acl_str
, line
, MAXSIZE
) >= MAXSIZE
) {
321 fstr_sprintf(line
, "%d\n", negatives
);
322 if (strlcat(acl_str
, line
, MAXSIZE
) >= MAXSIZE
) {
328 while (ace
!= NULL
) {
329 fstr_sprintf(line
, "%s\t%d\n", ace
->name
, ace
->rights
);
330 if (strlcat(acl_str
, line
, MAXSIZE
) >= MAXSIZE
) {
338 static uint32
afs_to_nt_file_rights(uint32 rights
)
342 if (rights
& PRSFS_READ
)
343 result
|= FILE_READ_DATA
| FILE_READ_EA
|
344 FILE_EXECUTE
| FILE_READ_ATTRIBUTES
|
345 READ_CONTROL_ACCESS
| SYNCHRONIZE_ACCESS
;
347 if (rights
& PRSFS_WRITE
)
348 result
|= FILE_WRITE_DATA
| FILE_WRITE_ATTRIBUTES
|
349 FILE_WRITE_EA
| FILE_APPEND_DATA
;
351 if (rights
& PRSFS_LOCK
)
352 result
|= WRITE_OWNER_ACCESS
;
354 if (rights
& PRSFS_DELETE
)
355 result
|= DELETE_ACCESS
;
360 static void afs_to_nt_dir_rights(uint32 afs_rights
, uint32
*nt_rights
,
364 *flag
= SEC_ACE_FLAG_OBJECT_INHERIT
|
365 SEC_ACE_FLAG_CONTAINER_INHERIT
;
367 if (afs_rights
& PRSFS_INSERT
)
368 *nt_rights
|= FILE_ADD_FILE
| FILE_ADD_SUBDIRECTORY
;
370 if (afs_rights
& PRSFS_LOOKUP
)
371 *nt_rights
|= FILE_READ_DATA
| FILE_READ_EA
|
372 FILE_EXECUTE
| FILE_READ_ATTRIBUTES
|
373 READ_CONTROL_ACCESS
| SYNCHRONIZE_ACCESS
;
375 if (afs_rights
& PRSFS_WRITE
)
376 *nt_rights
|= FILE_WRITE_ATTRIBUTES
| FILE_WRITE_DATA
|
377 FILE_APPEND_DATA
| FILE_WRITE_EA
;
379 if ((afs_rights
& (PRSFS_INSERT
|PRSFS_LOOKUP
|PRSFS_DELETE
)) ==
380 (PRSFS_INSERT
|PRSFS_LOOKUP
|PRSFS_DELETE
))
381 *nt_rights
|= FILE_WRITE_ATTRIBUTES
| FILE_WRITE_EA
|
382 GENERIC_WRITE_ACCESS
;
384 if (afs_rights
& PRSFS_DELETE
)
385 *nt_rights
|= DELETE_ACCESS
;
387 if (afs_rights
& PRSFS_ADMINISTER
)
388 *nt_rights
|= FILE_DELETE_CHILD
| WRITE_DAC_ACCESS
|
391 if ( (afs_rights
& PRSFS_LOOKUP
) ==
392 (afs_rights
& (PRSFS_LOOKUP
|PRSFS_READ
)) ) {
393 /* Only lookup right */
394 *flag
= SEC_ACE_FLAG_CONTAINER_INHERIT
;
398 #define AFS_FILE_RIGHTS (PRSFS_READ|PRSFS_WRITE|PRSFS_LOCK)
399 #define AFS_DIR_RIGHTS (PRSFS_INSERT|PRSFS_LOOKUP|PRSFS_DELETE|PRSFS_ADMINISTER)
401 static void split_afs_acl(struct afs_acl
*acl
,
402 struct afs_acl
*dir_acl
,
403 struct afs_acl
*file_acl
)
407 init_afs_acl(dir_acl
);
408 init_afs_acl(file_acl
);
410 for (ace
= acl
->acelist
; ace
!= NULL
; ace
= ace
->next
) {
411 if (ace
->rights
& AFS_FILE_RIGHTS
) {
412 add_afs_ace(file_acl
, ace
->positive
, ace
->name
,
413 ace
->rights
& AFS_FILE_RIGHTS
);
416 if (ace
->rights
& AFS_DIR_RIGHTS
) {
417 add_afs_ace(dir_acl
, ace
->positive
, ace
->name
,
418 ace
->rights
& AFS_DIR_RIGHTS
);
423 static bool same_principal(struct afs_ace
*x
, struct afs_ace
*y
)
425 return ( (x
->positive
== y
->positive
) &&
426 (dom_sid_compare(&x
->sid
, &y
->sid
) == 0) );
429 static void merge_afs_acls(struct afs_acl
*dir_acl
,
430 struct afs_acl
*file_acl
,
431 struct afs_acl
*target
)
435 init_afs_acl(target
);
437 for (ace
= dir_acl
->acelist
; ace
!= NULL
; ace
= ace
->next
) {
438 struct afs_ace
*file_ace
;
441 for (file_ace
= file_acl
->acelist
;
443 file_ace
= file_ace
->next
) {
444 if (!same_principal(ace
, file_ace
))
447 add_afs_ace(target
, ace
->positive
, ace
->name
,
448 ace
->rights
| file_ace
->rights
);
453 add_afs_ace(target
, ace
->positive
, ace
->name
,
457 for (ace
= file_acl
->acelist
; ace
!= NULL
; ace
= ace
->next
) {
458 struct afs_ace
*dir_ace
;
459 bool already_seen
= false;
461 for (dir_ace
= dir_acl
->acelist
;
463 dir_ace
= dir_ace
->next
) {
464 if (!same_principal(ace
, dir_ace
))
470 add_afs_ace(target
, ace
->positive
, ace
->name
,
475 #define PERMS_READ 0x001200a9
476 #define PERMS_CHANGE 0x001301bf
477 #define PERMS_FULL 0x001f01ff
479 static struct static_dir_ace_mapping
{
487 { 0, SEC_ACE_FLAG_OBJECT_INHERIT
|SEC_ACE_FLAG_CONTAINER_INHERIT
,
488 PERMS_FULL
, 127 /* rlidwka */ },
491 { 0, SEC_ACE_FLAG_OBJECT_INHERIT
|SEC_ACE_FLAG_CONTAINER_INHERIT
,
492 PERMS_CHANGE
, 63 /* rlidwk */ },
494 /* Read (including list folder content) */
495 { 0, SEC_ACE_FLAG_OBJECT_INHERIT
|SEC_ACE_FLAG_CONTAINER_INHERIT
,
496 PERMS_READ
, 9 /* rl */ },
498 /* Read without list folder content -- same as "l" */
499 { 0, SEC_ACE_FLAG_OBJECT_INHERIT
|SEC_ACE_FLAG_CONTAINER_INHERIT
,
500 0x00120089, 8 /* l */ },
502 /* some stupid workaround for preventing fallbacks */
503 { 0, 0x3, 0x0012019F, 9 /* rl */ },
504 { 0, 0x13, PERMS_FULL
, 127 /* full */ },
506 /* read, delete and execute access plus synchronize */
507 { 0, 0x3, 0x001300A9, 9 /* should be rdl, set to rl */},
508 /* classical read list */
509 { 0, 0x13, 0x001200A9, 9 /* rl */},
510 /* almost full control, no delete */
511 { 0, 0x13, PERMS_CHANGE
, 63 /* rwidlk */},
514 { 0, SEC_ACE_FLAG_CONTAINER_INHERIT
,
515 PERMS_READ
, 8 /* l */ },
517 /* FULL without inheritance -- in all cases here we also get
518 the corresponding INHERIT_ONLY ACE in the same ACL */
519 { 0, 0, PERMS_FULL
, 127 /* rlidwka */ },
521 /* FULL inherit only -- counterpart to previous one */
522 { 0, SEC_ACE_FLAG_OBJECT_INHERIT
|SEC_ACE_FLAG_CONTAINER_INHERIT
|SEC_ACE_FLAG_INHERIT_ONLY
,
523 PERMS_FULL
| SEC_GENERIC_WRITE
, 127 /* rlidwka */ },
525 /* CHANGE without inheritance -- in all cases here we also get
526 the corresponding INHERIT_ONLY ACE in the same ACL */
527 { 0, 0, PERMS_CHANGE
, 63 /* rlidwk */ },
529 /* CHANGE inherit only -- counterpart to previous one */
530 { 0, SEC_ACE_FLAG_OBJECT_INHERIT
|SEC_ACE_FLAG_CONTAINER_INHERIT
|SEC_ACE_FLAG_INHERIT_ONLY
,
531 PERMS_CHANGE
| SEC_GENERIC_WRITE
, 63 /* rlidwk */ },
533 /* End marker, hopefully there's no afs right 9999 :-) */
537 static uint32
nt_to_afs_dir_rights(const char *filename
, const struct security_ace
*ace
)
540 uint32 rights
= ace
->access_mask
;
541 uint8 flags
= ace
->flags
;
543 struct static_dir_ace_mapping
*m
;
545 for (m
= &ace_mappings
[0]; m
->afs_rights
!= 9999; m
++) {
546 if ( (ace
->type
== m
->type
) &&
547 (ace
->flags
== m
->flags
) &&
548 (ace
->access_mask
== m
->mask
) )
549 return m
->afs_rights
;
552 DEBUG(1, ("AFSACL FALLBACK: 0x%X 0x%X 0x%X %s %X\n",
553 ace
->type
, ace
->flags
, ace
->access_mask
, filename
, rights
));
555 if (rights
& (GENERIC_ALL_ACCESS
|WRITE_DAC_ACCESS
)) {
556 result
|= PRSFS_READ
| PRSFS_WRITE
| PRSFS_INSERT
|
557 PRSFS_LOOKUP
| PRSFS_DELETE
| PRSFS_LOCK
|
561 if (rights
& (GENERIC_READ_ACCESS
|FILE_READ_DATA
)) {
562 result
|= PRSFS_LOOKUP
;
563 if (flags
& SEC_ACE_FLAG_OBJECT_INHERIT
) {
564 result
|= PRSFS_READ
;
568 if (rights
& (GENERIC_WRITE_ACCESS
|FILE_WRITE_DATA
)) {
569 result
|= PRSFS_INSERT
| PRSFS_DELETE
;
570 if (flags
& SEC_ACE_FLAG_OBJECT_INHERIT
) {
571 result
|= PRSFS_WRITE
| PRSFS_LOCK
;
578 static uint32
nt_to_afs_file_rights(const char *filename
, const struct security_ace
*ace
)
581 uint32 rights
= ace
->access_mask
;
583 if (rights
& (GENERIC_READ_ACCESS
|FILE_READ_DATA
)) {
584 result
|= PRSFS_READ
;
587 if (rights
& (GENERIC_WRITE_ACCESS
|FILE_WRITE_DATA
)) {
588 result
|= PRSFS_WRITE
| PRSFS_LOCK
;
594 static size_t afs_to_nt_acl_common(struct afs_acl
*afs_acl
,
595 SMB_STRUCT_STAT
*psbuf
,
596 uint32 security_info
,
598 struct security_descriptor
**ppdesc
)
600 struct security_ace
*nt_ace_list
;
601 struct dom_sid owner_sid
, group_sid
;
602 struct security_acl
*psa
= NULL
;
606 struct afs_ace
*afs_ace
;
608 uid_to_sid(&owner_sid
, psbuf
->st_ex_uid
);
609 gid_to_sid(&group_sid
, psbuf
->st_ex_gid
);
611 if (afs_acl
->num_aces
) {
612 nt_ace_list
= talloc_array(mem_ctx
, struct security_ace
, afs_acl
->num_aces
);
614 if (nt_ace_list
== NULL
)
620 afs_ace
= afs_acl
->acelist
;
623 while (afs_ace
!= NULL
) {
625 uint8 flag
= SEC_ACE_FLAG_OBJECT_INHERIT
|
626 SEC_ACE_FLAG_CONTAINER_INHERIT
;
628 if (afs_ace
->type
== SID_NAME_UNKNOWN
) {
629 DEBUG(10, ("Ignoring unknown name %s\n",
631 afs_ace
= afs_ace
->next
;
635 if (S_ISDIR(psbuf
->st_ex_mode
))
636 afs_to_nt_dir_rights(afs_ace
->rights
, &nt_rights
,
639 nt_rights
= afs_to_nt_file_rights(afs_ace
->rights
);
641 init_sec_ace(&nt_ace_list
[good_aces
++], &(afs_ace
->sid
),
642 SEC_ACE_TYPE_ACCESS_ALLOWED
, nt_rights
, flag
);
643 afs_ace
= afs_ace
->next
;
646 psa
= make_sec_acl(mem_ctx
, NT4_ACL_REVISION
,
647 good_aces
, nt_ace_list
);
651 *ppdesc
= make_sec_desc(mem_ctx
, SD_REVISION
,
652 SEC_DESC_SELF_RELATIVE
,
653 (security_info
& SECINFO_OWNER
)
655 (security_info
& SECINFO_GROUP
)
657 NULL
, psa
, &sd_size
);
662 static size_t afs_to_nt_acl(struct afs_acl
*afs_acl
,
663 struct connection_struct
*conn
,
664 struct smb_filename
*smb_fname
,
665 uint32 security_info
,
667 struct security_descriptor
**ppdesc
)
671 /* Get the stat struct for the owner info. */
672 if (lp_posix_pathnames()) {
673 ret
= SMB_VFS_LSTAT(conn
, smb_fname
);
675 ret
= SMB_VFS_STAT(conn
, smb_fname
);
681 return afs_to_nt_acl_common(afs_acl
, &smb_fname
->st
, security_info
,
685 static size_t afs_fto_nt_acl(struct afs_acl
*afs_acl
,
686 struct files_struct
*fsp
,
687 uint32 security_info
,
689 struct security_descriptor
**ppdesc
)
691 SMB_STRUCT_STAT sbuf
;
693 if (fsp
->fh
->fd
== -1) {
694 /* Get the stat struct for the owner info. */
695 return afs_to_nt_acl(afs_acl
, fsp
->conn
, fsp
->fsp_name
,
696 security_info
, mem_ctx
, ppdesc
);
699 if(SMB_VFS_FSTAT(fsp
, &sbuf
) != 0) {
703 return afs_to_nt_acl_common(afs_acl
, &sbuf
, security_info
,
707 static bool mappable_sid(const struct dom_sid
*sid
)
709 struct dom_sid domain_sid
;
711 if (dom_sid_compare(sid
, &global_sid_Builtin_Administrators
) == 0)
714 if (dom_sid_compare(sid
, &global_sid_World
) == 0)
717 if (dom_sid_compare(sid
, &global_sid_Authenticated_Users
) == 0)
720 if (dom_sid_compare(sid
, &global_sid_Builtin_Backup_Operators
) == 0)
723 string_to_sid(&domain_sid
, "S-1-5-21");
725 if (sid_compare_domain(sid
, &domain_sid
) == 0)
731 static bool nt_to_afs_acl(const char *filename
,
732 uint32 security_info_sent
,
733 const struct security_descriptor
*psd
,
734 uint32 (*nt_to_afs_rights
)(const char *filename
,
735 const struct security_ace
*ace
),
736 struct afs_acl
*afs_acl
)
738 const struct security_acl
*dacl
;
741 /* Currently we *only* look at the dacl */
743 if (((security_info_sent
& SECINFO_DACL
) == 0) ||
747 if (!init_afs_acl(afs_acl
))
752 for (i
= 0; i
< dacl
->num_aces
; i
++) {
753 const struct security_ace
*ace
= &(dacl
->aces
[i
]);
754 const char *dom_name
, *name
;
755 enum lsa_SidType name_type
;
758 if (ace
->type
!= SEC_ACE_TYPE_ACCESS_ALLOWED
) {
759 /* First cut: Only positive ACEs */
763 if (!mappable_sid(&ace
->trustee
)) {
764 DEBUG(10, ("Ignoring unmappable SID %s\n",
765 sid_string_dbg(&ace
->trustee
)));
769 if (dom_sid_compare(&ace
->trustee
,
770 &global_sid_Builtin_Administrators
) == 0) {
772 name
= "system:administrators";
774 } else if (dom_sid_compare(&ace
->trustee
,
775 &global_sid_World
) == 0) {
777 name
= "system:anyuser";
779 } else if (dom_sid_compare(&ace
->trustee
,
780 &global_sid_Authenticated_Users
) == 0) {
782 name
= "system:authuser";
784 } else if (dom_sid_compare(&ace
->trustee
,
785 &global_sid_Builtin_Backup_Operators
)
788 name
= "system:backup";
792 if (!lookup_sid(talloc_tos(), &ace
->trustee
,
793 &dom_name
, &name
, &name_type
)) {
794 DEBUG(1, ("AFSACL: Could not lookup SID %s on file %s\n",
795 sid_string_dbg(&ace
->trustee
),
800 if ( (name_type
== SID_NAME_USER
) ||
801 (name_type
== SID_NAME_DOM_GRP
) ||
802 (name_type
== SID_NAME_ALIAS
) ) {
804 tmp
= talloc_asprintf(talloc_tos(), "%s%s%s",
805 dom_name
, lp_winbind_separator(),
810 if (!strlower_m(tmp
)) {
817 /* Expect all users/groups in pts as SIDs */
818 name
= talloc_strdup(
820 sid_string_tos(&ace
->trustee
));
827 while ((p
= strchr_m(name
, ' ')) != NULL
)
828 *p
= space_replacement
;
830 add_afs_ace(afs_acl
, true, name
,
831 nt_to_afs_rights(filename
, ace
));
837 static bool afs_get_afs_acl(const char *filename
, struct afs_acl
*acl
)
845 DEBUG(5, ("afs_get_afs_acl: %s\n", filename
));
848 iob
.out_size
= MAXSIZE
;
849 iob
.in
= iob
.out
= space
;
851 ret
= afs_syscall(AFSCALL_PIOCTL
, filename
, VIOCGETAL
,
855 DEBUG(1, ("got error from PIOCTL: %d\n", ret
));
859 if (!init_afs_acl(acl
))
862 if (!parse_afs_acl(acl
, space
)) {
863 DEBUG(1, ("Could not parse AFS acl\n"));
871 /* For setting an AFS ACL we have to take care of the ACEs we could
872 * not properly map to SIDs. Merge all of them into the new ACL. */
874 static void merge_unknown_aces(struct afs_acl
*src
, struct afs_acl
*dst
)
878 for (ace
= src
->acelist
; ace
!= NULL
; ace
= ace
->next
)
880 struct afs_ace
*copy
;
882 if (ace
->type
!= SID_NAME_UNKNOWN
) {
883 DEBUG(10, ("Not merging known ACE for %s\n",
888 DEBUG(10, ("Merging unknown ACE for %s\n", ace
->name
));
890 copy
= clone_afs_ace(dst
->ctx
, ace
);
893 DEBUG(0, ("Could not clone ACE for %s\n", ace
->name
));
897 copy
->next
= dst
->acelist
;
903 static NTSTATUS
afs_set_nt_acl(vfs_handle_struct
*handle
, files_struct
*fsp
,
904 uint32 security_info_sent
,
905 const struct security_descriptor
*psd
)
907 struct afs_acl old_afs_acl
, new_afs_acl
;
908 struct afs_acl dir_acl
, file_acl
;
909 char acl_string
[MAXSIZE
];
913 const char *fileacls
;
915 fileacls
= lp_parm_const_string(SNUM(handle
->conn
), "afsacl", "fileacls",
918 sidpts
= lp_parm_bool(SNUM(handle
->conn
), "afsacl", "sidpts", false);
920 ZERO_STRUCT(old_afs_acl
);
921 ZERO_STRUCT(new_afs_acl
);
922 ZERO_STRUCT(dir_acl
);
923 ZERO_STRUCT(file_acl
);
925 name
= talloc_strdup(talloc_tos(), fsp
->fsp_name
->base_name
);
927 return NT_STATUS_NO_MEMORY
;
930 if (!fsp
->is_directory
) {
931 /* We need to get the name of the directory containing the
932 * file, this is where the AFS acls live */
933 char *p
= strrchr(name
, '/');
937 name
= talloc_strdup(talloc_tos(), ".");
939 return NT_STATUS_NO_MEMORY
;
944 if (!afs_get_afs_acl(name
, &old_afs_acl
)) {
945 DEBUG(3, ("Could not get old ACL of %s\n", fsp_str_dbg(fsp
)));
949 split_afs_acl(&old_afs_acl
, &dir_acl
, &file_acl
);
951 if (fsp
->is_directory
) {
953 if (!strequal(fileacls
, "yes")) {
954 /* Throw away file acls, we depend on the
955 * inheritance ACEs that also give us file
957 free_afs_acl(&file_acl
);
960 free_afs_acl(&dir_acl
);
961 if (!nt_to_afs_acl(fsp
->fsp_name
->base_name
,
962 security_info_sent
, psd
,
963 nt_to_afs_dir_rights
, &dir_acl
))
966 if (strequal(fileacls
, "no")) {
971 if (strequal(fileacls
, "ignore")) {
976 free_afs_acl(&file_acl
);
977 if (!nt_to_afs_acl(fsp
->fsp_name
->base_name
,
978 security_info_sent
, psd
,
979 nt_to_afs_file_rights
, &file_acl
))
983 merge_afs_acls(&dir_acl
, &file_acl
, &new_afs_acl
);
985 merge_unknown_aces(&old_afs_acl
, &new_afs_acl
);
987 unparse_afs_acl(&new_afs_acl
, acl_string
);
990 iob
.in_size
= 1+strlen(iob
.in
);
994 DEBUG(10, ("trying to set acl '%s' on file %s\n", iob
.in
, name
));
996 ret
= afs_syscall(AFSCALL_PIOCTL
, name
, VIOCSETAL
, (char *)&iob
, 0);
999 DEBUG(10, ("VIOCSETAL returned %d\n", ret
));
1003 free_afs_acl(&dir_acl
);
1004 free_afs_acl(&file_acl
);
1005 free_afs_acl(&old_afs_acl
);
1006 free_afs_acl(&new_afs_acl
);
1008 return (ret
== 0) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1011 static NTSTATUS
afsacl_fget_nt_acl(struct vfs_handle_struct
*handle
,
1012 struct files_struct
*fsp
,
1013 uint32 security_info
,
1014 TALLOC_CTX
*mem_ctx
,
1015 struct security_descriptor
**ppdesc
)
1020 DEBUG(5, ("afsacl_fget_nt_acl: %s\n", fsp_str_dbg(fsp
)));
1022 sidpts
= lp_parm_bool(SNUM(fsp
->conn
), "afsacl", "sidpts", false);
1024 if (!afs_get_afs_acl(fsp
->fsp_name
->base_name
, &acl
)) {
1025 return NT_STATUS_ACCESS_DENIED
;
1028 sd_size
= afs_fto_nt_acl(&acl
, fsp
, security_info
, mem_ctx
, ppdesc
);
1032 return (sd_size
!= 0) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1035 static NTSTATUS
afsacl_get_nt_acl(struct vfs_handle_struct
*handle
,
1036 const char *name
, uint32 security_info
,
1037 TALLOC_CTX
*mem_ctx
,
1038 struct security_descriptor
**ppdesc
)
1042 struct smb_filename
*smb_fname
= NULL
;
1045 DEBUG(5, ("afsacl_get_nt_acl: %s\n", name
));
1047 sidpts
= lp_parm_bool(SNUM(handle
->conn
), "afsacl", "sidpts", false);
1049 if (!afs_get_afs_acl(name
, &acl
)) {
1050 return NT_STATUS_ACCESS_DENIED
;
1053 smb_fname
= synthetic_smb_fname(talloc_tos(), name
, NULL
, NULL
);
1054 if (smb_fname
== NULL
) {
1056 return NT_STATUS_NO_MEMORY
;
1059 sd_size
= afs_to_nt_acl(&acl
, handle
->conn
, smb_fname
, security_info
,
1061 TALLOC_FREE(smb_fname
);
1065 return (sd_size
!= 0) ? NT_STATUS_OK
: NT_STATUS_ACCESS_DENIED
;
1068 NTSTATUS
afsacl_fset_nt_acl(vfs_handle_struct
*handle
,
1070 uint32 security_info_sent
,
1071 const struct security_descriptor
*psd
)
1073 return afs_set_nt_acl(handle
, fsp
, security_info_sent
, psd
);
1076 static int afsacl_connect(vfs_handle_struct
*handle
,
1077 const char *service
,
1081 int ret
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
1087 spc
= lp_parm_const_string(SNUM(handle
->conn
), "afsacl", "space", "%");
1090 space_replacement
= spc
[0];
1095 /* We don't have a linear form of the AFS ACL yet */
1096 static int afsacl_sys_acl_blob_get_file(vfs_handle_struct
*handle
, const char *path_p
, TALLOC_CTX
*mem_ctx
, char **blob_description
, DATA_BLOB
*blob
)
1102 /* We don't have a linear form of the AFS ACL yet */
1103 static int afsacl_sys_acl_blob_get_fd(vfs_handle_struct
*handle
, files_struct
*fsp
, TALLOC_CTX
*mem_ctx
, char **blob_description
, DATA_BLOB
*blob
)
1109 static struct vfs_fn_pointers vfs_afsacl_fns
= {
1110 .connect_fn
= afsacl_connect
,
1111 .fget_nt_acl_fn
= afsacl_fget_nt_acl
,
1112 .get_nt_acl_fn
= afsacl_get_nt_acl
,
1113 .fset_nt_acl_fn
= afsacl_fset_nt_acl
,
1114 .sys_acl_blob_get_file_fn
= afsacl_sys_acl_blob_get_file
,
1115 .sys_acl_blob_get_fd_fn
= afsacl_sys_acl_blob_get_fd
1118 NTSTATUS
vfs_afsacl_init(void);
1119 NTSTATUS
vfs_afsacl_init(void)
1121 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "afsacl",