r4348: syncing up for 3.0.11pre1
[Samba.git] / source / modules / vfs_afsacl.c
blob6d6848eb120e2d3e7e4967ff00c64419c3192cd7
1 /*
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 2 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, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_VFS
26 #include <afs/stds.h>
27 #include <afs/afs.h>
28 #include <afs/auth.h>
29 #include <afs/venus.h>
30 #include <afs/prs_fs.h>
32 #define MAXSIZE 2048
34 extern DOM_SID global_sid_World;
35 extern DOM_SID global_sid_Builtin_Administrators;
36 extern DOM_SID global_sid_Builtin_Backup_Operators;
37 extern DOM_SID global_sid_Authenticated_Users;
38 extern DOM_SID global_sid_NULL;
40 static char space_replacement = '%';
42 extern int afs_syscall(int, char *, int, char *, int);
44 struct afs_ace {
45 BOOL positive;
46 char *name;
47 DOM_SID sid;
48 enum SID_NAME_USE type;
49 uint32 rights;
50 struct afs_ace *next;
53 struct afs_acl {
54 TALLOC_CTX *ctx;
55 int type;
56 int num_aces;
57 struct afs_ace *acelist;
60 struct afs_iob {
61 char *in, *out;
62 uint16 in_size, out_size;
66 static BOOL init_afs_acl(struct afs_acl *acl)
68 ZERO_STRUCT(*acl);
69 acl->ctx = talloc_init("afs_acl");
70 if (acl->ctx == NULL) {
71 DEBUG(10, ("Could not init afs_acl"));
72 return False;
74 return True;
77 static void free_afs_acl(struct afs_acl *acl)
79 if (acl->ctx != NULL)
80 talloc_destroy(acl->ctx);
81 acl->ctx = NULL;
82 acl->num_aces = 0;
83 acl->acelist = NULL;
86 static struct afs_ace *clone_afs_ace(TALLOC_CTX *mem_ctx, struct afs_ace *ace)
88 struct afs_ace *result = TALLOC_P(mem_ctx, struct afs_ace);
90 if (result == NULL)
91 return NULL;
93 *result = *ace;
95 result->next = NULL;
96 result->name = talloc_strdup(mem_ctx, ace->name);
98 if (result->name == NULL) {
99 return NULL;
102 return result;
106 /* Ok, this is sort-of a hack. We assume here that we have winbind users in
107 * AFS. And yet another copy of parse_domain_user.... */
109 static BOOL parse_domain_user(const char *domuser, fstring domain,
110 fstring user)
112 char *p = strchr(domuser,*lp_winbind_separator());
114 if (p==NULL) {
115 return False;
118 fstrcpy(user, p+1);
119 fstrcpy(domain, domuser);
120 domain[PTR_DIFF(p, domuser)] = 0;
121 strupper_m(domain);
123 return True;
126 static struct afs_ace *new_afs_ace(TALLOC_CTX *mem_ctx,
127 BOOL positive,
128 const char *name, uint32 rights)
130 DOM_SID sid;
131 enum SID_NAME_USE type;
132 struct afs_ace *result;
134 if (strcmp(name, "system:administrators") == 0) {
136 sid_copy(&sid, &global_sid_Builtin_Administrators);
137 type = SID_NAME_ALIAS;
139 } else if (strcmp(name, "system:anyuser") == 0) {
141 sid_copy(&sid, &global_sid_World);
142 type = SID_NAME_ALIAS;
144 } else if (strcmp(name, "system:authuser") == 0) {
146 sid_copy(&sid, &global_sid_Authenticated_Users);
147 type = SID_NAME_WKN_GRP;
149 } else if (strcmp(name, "system:backup") == 0) {
151 sid_copy(&sid, &global_sid_Builtin_Backup_Operators);
152 type = SID_NAME_ALIAS;
154 } else {
156 fstring user, domain;
158 if (!parse_domain_user(name, domain, user)) {
159 fstrcpy(user, name);
160 fstrcpy(domain, lp_workgroup());
163 if (!lookup_name(domain, user, &sid, &type)) {
164 DEBUG(10, ("Could not find AFS user %s\n", name));
166 sid_copy(&sid, &global_sid_NULL);
167 type = SID_NAME_UNKNOWN;
172 result = TALLOC_P(mem_ctx, struct afs_ace);
174 if (result == NULL) {
175 DEBUG(0, ("Could not talloc AFS ace\n"));
176 return NULL;
179 result->name = talloc_strdup(mem_ctx, name);
180 if (result->name == NULL) {
181 DEBUG(0, ("Could not talloc AFS ace name\n"));
182 return NULL;
185 result->sid = sid;
186 result->type = type;
188 result->positive = positive;
189 result->rights = rights;
191 return result;
194 static void add_afs_ace(struct afs_acl *acl,
195 BOOL positive,
196 const char *name, uint32 rights)
198 struct afs_ace *ace;
200 for (ace = acl->acelist; ace != NULL; ace = ace->next) {
201 if ((ace->positive == positive) &&
202 (strequal(ace->name, name))) {
203 ace->rights |= rights;
204 return;
208 ace = new_afs_ace(acl->ctx, positive, name, rights);
210 ace->next = acl->acelist;
211 acl->acelist = ace;
213 acl->num_aces += 1;
215 DEBUG(10, ("add_afs_ace: Added %s entry for %s with rights %d\n",
216 ace->positive?"positive":"negative",
217 ace->name, ace->rights));
219 return;
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.
230 * First field: Name
231 * Second field: Rights
234 static BOOL parse_afs_acl(struct afs_acl *acl, const char *acl_str)
236 int nplus, nminus;
237 int aces;
239 char str[MAXSIZE+1];
240 char *p = str;
242 strncpy(str, acl_str, MAXSIZE);
244 if (sscanf(p, "%d", &nplus) != 1)
245 return False;
247 DEBUG(10, ("Found %d positive entries\n", nplus));
249 if ((p = strchr(p, '\n')) == NULL)
250 return False;
251 p += 1;
253 if (sscanf(p, "%d", &nminus) != 1)
254 return False;
256 DEBUG(10, ("Found %d negative entries\n", nminus));
258 if ((p = strchr(p, '\n')) == NULL)
259 return False;
260 p += 1;
262 for (aces = nplus+nminus; aces > 0; aces--)
265 const char *namep;
266 fstring name;
267 uint32 rights;
268 char *space;
270 namep = p;
272 if ((p = strchr(p, '\t')) == NULL)
273 return False;
274 *p = '\0';
275 p += 1;
277 if (sscanf(p, "%d", &rights) != 1)
278 return False;
280 if ((p = strchr(p, '\n')) == NULL)
281 return False;
282 p += 1;
284 fstrcpy(name, namep);
286 while ((space = strchr_m(name, space_replacement)) != NULL)
287 *space = ' ';
289 add_afs_ace(acl, nplus>0, name, rights);
291 nplus -= 1;
294 return True;
297 static BOOL unparse_afs_acl(struct afs_acl *acl, char *acl_str)
299 /* TODO: String length checks!!!! */
301 int positives = 0;
302 int negatives = 0;
303 fstring line;
305 *acl_str = 0;
307 struct afs_ace *ace = acl->acelist;
309 while (ace != NULL) {
310 if (ace->positive)
311 positives++;
312 else
313 negatives++;
314 ace = ace->next;
317 fstr_sprintf(line, "%d\n", positives);
318 safe_strcat(acl_str, line, MAXSIZE);
320 fstr_sprintf(line, "%d\n", negatives);
321 safe_strcat(acl_str, line, MAXSIZE);
323 ace = acl->acelist;
325 while (ace != NULL) {
326 fstr_sprintf(line, "%s\t%d\n", ace->name, ace->rights);
327 safe_strcat(acl_str, line, MAXSIZE);
328 ace = ace->next;
330 return True;
333 static uint32 afs_to_nt_file_rights(uint32 rights)
335 uint32 result = 0;
337 if (rights & PRSFS_READ)
338 result |= FILE_READ_DATA | FILE_READ_EA |
339 FILE_EXECUTE | FILE_READ_ATTRIBUTES |
340 READ_CONTROL_ACCESS | SYNCHRONIZE_ACCESS;
342 if (rights & PRSFS_WRITE)
343 result |= FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES |
344 FILE_WRITE_EA | FILE_APPEND_DATA;
346 if (rights & PRSFS_LOCK)
347 result |= WRITE_OWNER_ACCESS;
349 if (rights & PRSFS_DELETE)
350 result |= DELETE_ACCESS;
352 return result;
355 static void afs_to_nt_dir_rights(uint32 afs_rights, uint32 *nt_rights,
356 uint8 *flag)
358 *nt_rights = 0;
359 *flag = SEC_ACE_FLAG_OBJECT_INHERIT |
360 SEC_ACE_FLAG_CONTAINER_INHERIT;
362 if (afs_rights & PRSFS_INSERT)
363 *nt_rights |= FILE_ADD_FILE | FILE_ADD_SUBDIRECTORY;
365 if (afs_rights & PRSFS_LOOKUP)
366 *nt_rights |= FILE_READ_DATA | FILE_READ_EA |
367 FILE_EXECUTE | FILE_READ_ATTRIBUTES |
368 READ_CONTROL_ACCESS | SYNCHRONIZE_ACCESS;
370 if (afs_rights & PRSFS_WRITE)
371 *nt_rights |= FILE_WRITE_ATTRIBUTES | FILE_WRITE_DATA |
372 FILE_APPEND_DATA | FILE_WRITE_EA;
374 if ((afs_rights & (PRSFS_INSERT|PRSFS_LOOKUP|PRSFS_DELETE)) ==
375 (PRSFS_INSERT|PRSFS_LOOKUP|PRSFS_DELETE))
376 *nt_rights |= FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA |
377 GENERIC_WRITE_ACCESS;
379 if (afs_rights & PRSFS_DELETE)
380 *nt_rights |= DELETE_ACCESS;
382 if (afs_rights & PRSFS_ADMINISTER)
383 *nt_rights |= FILE_DELETE_CHILD | WRITE_DAC_ACCESS |
384 WRITE_OWNER_ACCESS;
386 if ( (afs_rights & PRSFS_LOOKUP) ==
387 (afs_rights & (PRSFS_LOOKUP|PRSFS_READ)) ) {
388 /* Only lookup right */
389 *flag = SEC_ACE_FLAG_CONTAINER_INHERIT;
392 return;
395 #define AFS_FILE_RIGHTS (PRSFS_READ|PRSFS_WRITE|PRSFS_LOCK)
396 #define AFS_DIR_RIGHTS (PRSFS_INSERT|PRSFS_LOOKUP|PRSFS_DELETE|PRSFS_ADMINISTER)
398 static void split_afs_acl(struct afs_acl *acl,
399 struct afs_acl *dir_acl,
400 struct afs_acl *file_acl)
402 struct afs_ace *ace;
404 init_afs_acl(dir_acl);
405 init_afs_acl(file_acl);
407 for (ace = acl->acelist; ace != NULL; ace = ace->next) {
408 if (ace->rights & AFS_FILE_RIGHTS) {
409 add_afs_ace(file_acl, ace->positive, ace->name,
410 ace->rights & AFS_FILE_RIGHTS);
413 if (ace->rights & AFS_DIR_RIGHTS) {
414 add_afs_ace(dir_acl, ace->positive, ace->name,
415 ace->rights & AFS_DIR_RIGHTS);
418 return;
421 static BOOL same_principal(struct afs_ace *x, struct afs_ace *y)
423 return ( (x->positive == y->positive) &&
424 (sid_compare(&x->sid, &y->sid) == 0) );
427 static void merge_afs_acls(struct afs_acl *dir_acl,
428 struct afs_acl *file_acl,
429 struct afs_acl *target)
431 struct afs_ace *ace;
433 init_afs_acl(target);
435 for (ace = dir_acl->acelist; ace != NULL; ace = ace->next) {
436 struct afs_ace *file_ace;
437 BOOL found = False;
439 for (file_ace = file_acl->acelist;
440 file_ace != NULL;
441 file_ace = file_ace->next) {
442 if (!same_principal(ace, file_ace))
443 continue;
445 add_afs_ace(target, ace->positive, ace->name,
446 ace->rights | file_ace->rights);
447 found = True;
448 break;
450 if (!found)
451 add_afs_ace(target, ace->positive, ace->name,
452 ace->rights);
455 for (ace = file_acl->acelist; ace != NULL; ace = ace->next) {
456 struct afs_ace *dir_ace;
457 BOOL already_seen = False;
459 for (dir_ace = dir_acl->acelist;
460 dir_ace != NULL;
461 dir_ace = dir_ace->next) {
462 if (!same_principal(ace, dir_ace))
463 continue;
464 already_seen = True;
465 break;
467 if (!already_seen)
468 add_afs_ace(target, ace->positive, ace->name,
469 ace->rights);
473 #define PERMS_READ 0x001200a9
474 #define PERMS_CHANGE 0x001301bf
475 #define PERMS_FULL 0x001f01ff
477 static struct static_dir_ace_mapping {
478 uint8 type;
479 uint8 flags;
480 uint32 mask;
481 uint32 afs_rights;
482 } ace_mappings[] = {
484 /* Full control */
485 { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT,
486 PERMS_FULL, 127 /* rlidwka */ },
488 /* Change (write) */
489 { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT,
490 PERMS_CHANGE, 63 /* rlidwk */ },
492 /* Read (including list folder content) */
493 { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT,
494 PERMS_READ, 9 /* rl */ },
496 /* Read without list folder content -- same as "l" */
497 { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT,
498 0x00120089, 8 /* l */ },
500 /* some stupid workaround for preventing fallbacks */
501 { 0, 0x3, 0x0012019F, 9 /* rl */ },
502 { 0, 0x13, PERMS_FULL, 127 /* full */ },
504 /* read, delete and execute access plus synchronize */
505 { 0, 0x3, 0x001300A9, 9 /* should be rdl, set to rl */},
506 /* classical read list */
507 { 0, 0x13, 0x001200A9, 9 /* rl */},
508 /* almost full control, no delete */
509 { 0, 0x13, PERMS_CHANGE, 63 /* rwidlk */},
511 /* List folder */
512 { 0, SEC_ACE_FLAG_CONTAINER_INHERIT,
513 PERMS_READ, 8 /* l */ },
515 /* FULL without inheritance -- in all cases here we also get
516 the corresponding INHERIT_ONLY ACE in the same ACL */
517 { 0, 0, PERMS_FULL, 127 /* rlidwka */ },
519 /* FULL inherit only -- counterpart to previous one */
520 { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_INHERIT_ONLY,
521 PERMS_FULL | GENERIC_RIGHT_WRITE_ACCESS, 127 /* rlidwka */ },
523 /* CHANGE without inheritance -- in all cases here we also get
524 the corresponding INHERIT_ONLY ACE in the same ACL */
525 { 0, 0, PERMS_CHANGE, 63 /* rlidwk */ },
527 /* CHANGE inherit only -- counterpart to previous one */
528 { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_INHERIT_ONLY,
529 PERMS_CHANGE | GENERIC_RIGHT_WRITE_ACCESS, 63 /* rlidwk */ },
531 /* End marker, hopefully there's no afs right 9999 :-) */
532 { 0, 0, 0, 9999 }
535 static uint32 nt_to_afs_dir_rights(const char *filename, const SEC_ACE *ace)
537 uint32 result = 0;
538 uint32 rights = ace->info.mask;
539 uint8 flags = ace->flags;
541 struct static_dir_ace_mapping *m;
543 for (m = &ace_mappings[0]; m->afs_rights != 9999; m++) {
544 if ( (ace->type == m->type) &&
545 (ace->flags == m->flags) &&
546 (ace->info.mask == m->mask) )
547 return m->afs_rights;
550 DEBUG(1, ("AFSACL FALLBACK: 0x%X 0x%X 0x%X %s %X\n",
551 ace->type, ace->flags, ace->info.mask, filename, rights));
553 if (rights & (GENERIC_ALL_ACCESS|WRITE_DAC_ACCESS)) {
554 result |= PRSFS_READ | PRSFS_WRITE | PRSFS_INSERT |
555 PRSFS_LOOKUP | PRSFS_DELETE | PRSFS_LOCK |
556 PRSFS_ADMINISTER;
559 if (rights & (GENERIC_READ_ACCESS|FILE_READ_DATA)) {
560 result |= PRSFS_LOOKUP;
561 if (flags & SEC_ACE_FLAG_OBJECT_INHERIT) {
562 result |= PRSFS_READ;
566 if (rights & (GENERIC_WRITE_ACCESS|FILE_WRITE_DATA)) {
567 result |= PRSFS_INSERT | PRSFS_DELETE;
568 if (flags & SEC_ACE_FLAG_OBJECT_INHERIT) {
569 result |= PRSFS_WRITE | PRSFS_LOCK;
573 return result;
576 static uint32 nt_to_afs_file_rights(const char *filename, const SEC_ACE *ace)
578 uint32 result = 0;
579 uint32 rights = ace->info.mask;
581 if (rights & (GENERIC_READ_ACCESS|FILE_READ_DATA)) {
582 result |= PRSFS_READ;
585 if (rights & (GENERIC_WRITE_ACCESS|FILE_WRITE_DATA)) {
586 result |= PRSFS_WRITE | PRSFS_LOCK;
589 return result;
592 static size_t afs_to_nt_acl(struct afs_acl *afs_acl,
593 struct files_struct *fsp,
594 uint32 security_info,
595 struct security_descriptor_info **ppdesc)
597 SEC_ACE *nt_ace_list;
598 DOM_SID owner_sid, group_sid;
599 SEC_ACCESS mask;
600 SMB_STRUCT_STAT sbuf;
601 SEC_ACL *psa = NULL;
602 int good_aces;
603 size_t sd_size;
604 TALLOC_CTX *mem_ctx = main_loop_talloc_get();
606 struct afs_ace *afs_ace;
608 if (fsp->is_directory || fsp->fd == -1) {
609 /* Get the stat struct for the owner info. */
610 if(SMB_VFS_STAT(fsp->conn,fsp->fsp_name, &sbuf) != 0) {
611 return 0;
613 } else {
614 if(SMB_VFS_FSTAT(fsp,fsp->fd,&sbuf) != 0) {
615 return 0;
619 uid_to_sid(&owner_sid, sbuf.st_uid);
620 gid_to_sid(&group_sid, sbuf.st_gid);
622 nt_ace_list = SMB_MALLOC_ARRAY(SEC_ACE, afs_acl->num_aces);
624 if (nt_ace_list == NULL)
625 return 0;
627 afs_ace = afs_acl->acelist;
628 good_aces = 0;
630 while (afs_ace != NULL) {
631 uint32 nt_rights;
632 uint8 flag = SEC_ACE_FLAG_OBJECT_INHERIT |
633 SEC_ACE_FLAG_CONTAINER_INHERIT;
635 if (afs_ace->type == SID_NAME_UNKNOWN) {
636 DEBUG(10, ("Ignoring unknown name %s\n",
637 afs_ace->name));
638 afs_ace = afs_ace->next;
639 continue;
642 if (fsp->is_directory)
643 afs_to_nt_dir_rights(afs_ace->rights, &nt_rights,
644 &flag);
645 else
646 nt_rights = afs_to_nt_file_rights(afs_ace->rights);
648 init_sec_access(&mask, nt_rights);
649 init_sec_ace(&nt_ace_list[good_aces++], &(afs_ace->sid),
650 SEC_ACE_TYPE_ACCESS_ALLOWED, mask, flag);
651 afs_ace = afs_ace->next;
654 psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION,
655 good_aces, nt_ace_list);
656 if (psa == NULL)
657 return 0;
660 *ppdesc = make_sec_desc(mem_ctx, SEC_DESC_REVISION,
661 SEC_DESC_SELF_RELATIVE,
662 (security_info & OWNER_SECURITY_INFORMATION)
663 ? &owner_sid : NULL,
664 (security_info & GROUP_SECURITY_INFORMATION)
665 ? &group_sid : NULL,
666 NULL, psa, &sd_size);
668 return sd_size;
671 static BOOL mappable_sid(const DOM_SID *sid)
673 DOM_SID domain_sid;
675 if (sid_compare(sid, &global_sid_Builtin_Administrators) == 0)
676 return True;
678 if (sid_compare(sid, &global_sid_World) == 0)
679 return True;
681 if (sid_compare(sid, &global_sid_Authenticated_Users) == 0)
682 return True;
684 if (sid_compare(sid, &global_sid_Builtin_Backup_Operators) == 0)
685 return True;
687 string_to_sid(&domain_sid, "S-1-5-21");
689 if (sid_compare_domain(sid, &domain_sid) == 0)
690 return True;
692 return False;
695 static BOOL nt_to_afs_acl(const char *filename,
696 uint32 security_info_sent,
697 struct security_descriptor_info *psd,
698 uint32 (*nt_to_afs_rights)(const char *filename,
699 const SEC_ACE *ace),
700 struct afs_acl *afs_acl)
702 SEC_ACL *dacl;
703 int i;
705 /* Currently we *only* look at the dacl */
707 if (((security_info_sent & DACL_SECURITY_INFORMATION) == 0) ||
708 (psd->dacl == NULL))
709 return True;
711 if (!init_afs_acl(afs_acl))
712 return False;
714 dacl = psd->dacl;
716 for (i = 0; i < dacl->num_aces; i++) {
717 SEC_ACE *ace = &(dacl->ace[i]);
718 fstring dom_name;
719 fstring name;
720 enum SID_NAME_USE name_type;
721 char *p;
723 if (ace->type != SEC_ACE_TYPE_ACCESS_ALLOWED) {
724 /* First cut: Only positive ACEs */
725 return False;
728 if (!mappable_sid(&ace->trustee)) {
729 DEBUG(10, ("Ignoring unmappable SID %s\n",
730 sid_string_static(&ace->trustee)));
731 continue;
734 if (sid_compare(&ace->trustee,
735 &global_sid_Builtin_Administrators) == 0) {
737 fstrcpy(name, "system:administrators");
739 } else if (sid_compare(&ace->trustee,
740 &global_sid_World) == 0) {
742 fstrcpy(name, "system:anyuser");
744 } else if (sid_compare(&ace->trustee,
745 &global_sid_Authenticated_Users) == 0) {
747 fstrcpy(name, "system:authuser");
749 } else if (sid_compare(&ace->trustee,
750 &global_sid_Builtin_Backup_Operators)
751 == 0) {
753 fstrcpy(name, "system:backup");
755 } else {
757 if (!lookup_sid(&ace->trustee,
758 dom_name, name, &name_type)) {
759 DEBUG(1, ("AFSACL: Could not lookup SID %s on file %s\n",
760 sid_string_static(&ace->trustee), filename));
761 continue;
764 if ( (name_type == SID_NAME_USER) ||
765 (name_type == SID_NAME_DOM_GRP) ||
766 (name_type == SID_NAME_ALIAS) ) {
767 fstring only_username;
768 fstrcpy(only_username, name);
769 fstr_sprintf(name, "%s%s%s",
770 dom_name, lp_winbind_separator(),
771 only_username);
772 strlower_m(name);
776 while ((p = strchr_m(name, ' ')) != NULL)
777 *p = space_replacement;
779 add_afs_ace(afs_acl, True, name,
780 nt_to_afs_rights(filename, ace));
783 return True;
786 static BOOL afs_get_afs_acl(char *filename, struct afs_acl *acl)
788 struct afs_iob iob;
790 int ret;
792 char space[MAXSIZE];
794 DEBUG(5, ("afs_get_afs_acl: %s\n", filename));
796 iob.in_size = 0;
797 iob.out_size = MAXSIZE;
798 iob.in = iob.out = space;
800 ret = afs_syscall(AFSCALL_PIOCTL, filename, VIOCGETAL,
801 (char *)&iob, 0);
803 if (ret) {
804 DEBUG(1, ("got error from PIOCTL: %d\n", ret));
805 return False;
808 if (!init_afs_acl(acl))
809 return False;
811 if (!parse_afs_acl(acl, space)) {
812 DEBUG(1, ("Could not parse AFS acl\n"));
813 free_afs_acl(acl);
814 return False;
817 return True;
820 static size_t afs_get_nt_acl(struct files_struct *fsp, uint32 security_info,
821 struct security_descriptor_info **ppdesc)
823 struct afs_acl acl;
824 size_t sd_size;
826 DEBUG(5, ("afs_get_nt_acl: %s\n", fsp->fsp_name));
828 if (!afs_get_afs_acl(fsp->fsp_name, &acl)) {
829 return 0;
832 sd_size = afs_to_nt_acl(&acl, fsp, security_info, ppdesc);
834 free_afs_acl(&acl);
836 return sd_size;
839 /* For setting an AFS ACL we have to take care of the ACEs we could
840 * not properly map to SIDs. Merge all of them into the new ACL. */
842 static void merge_unknown_aces(struct afs_acl *src, struct afs_acl *dst)
844 struct afs_ace *ace;
846 for (ace = src->acelist; ace != NULL; ace = ace->next)
848 struct afs_ace *copy;
850 if (ace->type != SID_NAME_UNKNOWN) {
851 DEBUG(10, ("Not merging known ACE for %s\n",
852 ace->name));
853 continue;
856 DEBUG(10, ("Merging unknown ACE for %s\n", ace->name));
858 copy = clone_afs_ace(dst->ctx, ace);
860 if (copy == NULL) {
861 DEBUG(0, ("Could not clone ACE for %s\n", ace->name));
862 continue;
865 copy->next = dst->acelist;
866 dst->acelist = copy;
867 dst->num_aces += 1;
871 static BOOL afs_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
872 uint32 security_info_sent,
873 struct security_descriptor_info *psd)
875 struct afs_acl old_afs_acl, new_afs_acl;
876 struct afs_acl dir_acl, file_acl;
877 char acl_string[2049];
878 struct afs_iob iob;
879 int ret = -1;
880 pstring name;
881 const char *fileacls;
883 fileacls = lp_parm_const_string(SNUM(handle->conn), "afsacl", "fileacls",
884 "yes");
886 ZERO_STRUCT(old_afs_acl);
887 ZERO_STRUCT(new_afs_acl);
888 ZERO_STRUCT(dir_acl);
889 ZERO_STRUCT(file_acl);
891 pstr_sprintf(name, fsp->fsp_name);
893 if (!fsp->is_directory) {
894 char *p = strrchr(name, '/');
895 if (p == NULL) {
896 DEBUG(3, ("No / in file string\n"));
897 return False;
899 *p = '\0';
902 if (!afs_get_afs_acl(name, &old_afs_acl)) {
903 DEBUG(3, ("Could not get old ACL of %s\n", fsp->fsp_name));
904 goto done;
907 split_afs_acl(&old_afs_acl, &dir_acl, &file_acl);
909 if (fsp->is_directory) {
911 if (!strequal(fileacls, "yes")) {
912 /* Throw away file acls, we depend on the
913 * inheritance ACEs that also give us file
914 * permissions */
915 free_afs_acl(&file_acl);
918 free_afs_acl(&dir_acl);
919 if (!nt_to_afs_acl(fsp->fsp_name, security_info_sent, psd,
920 nt_to_afs_dir_rights, &dir_acl))
921 goto done;
922 } else {
923 if (strequal(fileacls, "no")) {
924 ret = -1;
925 goto done;
928 if (strequal(fileacls, "ignore")) {
929 ret = 0;
930 goto done;
933 free_afs_acl(&file_acl);
934 if (!nt_to_afs_acl(fsp->fsp_name, security_info_sent, psd,
935 nt_to_afs_file_rights, &file_acl))
936 goto done;
939 merge_afs_acls(&dir_acl, &file_acl, &new_afs_acl);
941 merge_unknown_aces(&old_afs_acl, &new_afs_acl);
943 unparse_afs_acl(&new_afs_acl, acl_string);
945 iob.in = acl_string;
946 iob.in_size = 1+strlen(iob.in);
947 iob.out = NULL;
948 iob.out_size = 0;
950 DEBUG(10, ("trying to set acl '%s' on file %s\n", iob.in, name));
952 ret = afs_syscall(AFSCALL_PIOCTL, name, VIOCSETAL, (char *)&iob, 0);
954 if (ret != 0) {
955 DEBUG(10, ("VIOCSETAL returned %d\n", ret));
958 done:
959 free_afs_acl(&dir_acl);
960 free_afs_acl(&file_acl);
961 free_afs_acl(&old_afs_acl);
962 free_afs_acl(&new_afs_acl);
964 return (ret == 0);
967 static size_t afsacl_fget_nt_acl(struct vfs_handle_struct *handle,
968 struct files_struct *fsp,
969 int fd, uint32 security_info,
970 struct security_descriptor_info **ppdesc)
972 return afs_get_nt_acl(fsp, security_info, ppdesc);
974 static size_t afsacl_get_nt_acl(struct vfs_handle_struct *handle,
975 struct files_struct *fsp,
976 const char *name, uint32 security_info,
977 struct security_descriptor_info **ppdesc)
979 return afs_get_nt_acl(fsp, security_info, ppdesc);
982 BOOL afsacl_fset_nt_acl(vfs_handle_struct *handle,
983 files_struct *fsp,
984 int fd, uint32 security_info_sent,
985 SEC_DESC *psd)
987 return afs_set_nt_acl(handle, fsp, security_info_sent, psd);
990 BOOL afsacl_set_nt_acl(vfs_handle_struct *handle,
991 files_struct *fsp,
992 const char *name, uint32 security_info_sent,
993 SEC_DESC *psd)
995 return afs_set_nt_acl(handle, fsp, security_info_sent, psd);
998 static int afsacl_connect(vfs_handle_struct *handle,
999 connection_struct *conn,
1000 const char *service,
1001 const char *user)
1003 char *spc;
1005 spc = lp_parm_const_string(SNUM(handle->conn), "afsacl", "space", "%");
1007 if (spc != NULL)
1008 space_replacement = spc[0];
1010 return SMB_VFS_NEXT_CONNECT(handle, conn, service, user);
1013 /* VFS operations structure */
1015 static vfs_op_tuple afsacl_ops[] = {
1016 {SMB_VFS_OP(afsacl_connect), SMB_VFS_OP_CONNECT,
1017 SMB_VFS_LAYER_TRANSPARENT},
1018 {SMB_VFS_OP(afsacl_fget_nt_acl), SMB_VFS_OP_FGET_NT_ACL,
1019 SMB_VFS_LAYER_TRANSPARENT},
1020 {SMB_VFS_OP(afsacl_get_nt_acl), SMB_VFS_OP_GET_NT_ACL,
1021 SMB_VFS_LAYER_TRANSPARENT},
1022 {SMB_VFS_OP(afsacl_fset_nt_acl), SMB_VFS_OP_FSET_NT_ACL,
1023 SMB_VFS_LAYER_TRANSPARENT},
1024 {SMB_VFS_OP(afsacl_set_nt_acl), SMB_VFS_OP_SET_NT_ACL,
1025 SMB_VFS_LAYER_TRANSPARENT},
1026 {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
1029 NTSTATUS vfs_afsacl_init(void)
1031 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "afsacl",
1032 afsacl_ops);