auth-kerberos: avoid crash with MIT krb5 1.10.0 in gss_get_name_attribute()
[Samba.git] / source3 / modules / vfs_afsacl.c
blob61a31458cf44f1013c62825b2f67c84d0f01398c
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 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/>.
20 #include "includes.h"
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"
26 #include "passdb.h"
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_VFS
31 #include <afs/stds.h>
32 #include <afs/afs.h>
33 #include <afs/auth.h>
34 #include <afs/venus.h>
35 #include <afs/prs_fs.h>
37 #define MAXSIZE 2049
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? */
48 static bool sidpts;
50 extern int afs_syscall(int, const char *, int, char *, int);
52 struct afs_ace {
53 bool positive;
54 char *name;
55 struct dom_sid sid;
56 enum lsa_SidType type;
57 uint32 rights;
58 struct afs_ace *next;
61 struct afs_acl {
62 TALLOC_CTX *ctx;
63 int type;
64 int num_aces;
65 struct afs_ace *acelist;
68 struct afs_iob {
69 char *in, *out;
70 uint16 in_size, out_size;
74 static bool init_afs_acl(struct afs_acl *acl)
76 ZERO_STRUCT(*acl);
77 acl->ctx = talloc_init("afs_acl");
78 if (acl->ctx == NULL) {
79 DEBUG(10, ("Could not init afs_acl"));
80 return false;
82 return true;
85 static void free_afs_acl(struct afs_acl *acl)
87 if (acl->ctx != NULL)
88 talloc_destroy(acl->ctx);
89 acl->ctx = NULL;
90 acl->num_aces = 0;
91 acl->acelist = NULL;
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);
98 if (result == NULL)
99 return NULL;
101 *result = *ace;
103 result->next = NULL;
104 result->name = talloc_strdup(mem_ctx, ace->name);
106 if (result->name == NULL) {
107 return NULL;
110 return result;
113 static struct afs_ace *new_afs_ace(TALLOC_CTX *mem_ctx,
114 bool positive,
115 const char *name, uint32 rights)
117 struct dom_sid sid;
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;
141 } else if (sidpts) {
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);
154 } else {
156 const char *domain, *uname;
157 char *p;
159 p = strchr_m(name, *lp_winbind_separator());
160 if (p != NULL) {
161 *p = '\\';
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"));
178 return NULL;
181 result->name = talloc_strdup(mem_ctx, name);
182 if (result->name == NULL) {
183 DEBUG(0, ("Could not talloc AFS ace name\n"));
184 return NULL;
187 result->sid = sid;
188 result->type = type;
190 result->positive = positive;
191 result->rights = rights;
193 return result;
196 static void add_afs_ace(struct afs_acl *acl,
197 bool positive,
198 const char *name, uint32 rights)
200 struct afs_ace *ace;
202 for (ace = acl->acelist; ace != NULL; ace = ace->next) {
203 if ((ace->positive == positive) &&
204 (strequal(ace->name, name))) {
205 ace->rights |= rights;
206 return;
210 ace = new_afs_ace(acl->ctx, positive, name, rights);
212 ace->next = acl->acelist;
213 acl->acelist = ace;
215 acl->num_aces += 1;
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));
221 return;
224 /* AFS ACLs in string form are a long string of fields delimited with \n.
226 * First line: Number of positive entries
227 * Second line: Number of negative entries
228 * Third and following lines: The entries themselves
230 * An ACE is a line of two fields, delimited by \t.
232 * First field: Name
233 * Second field: Rights
236 static bool parse_afs_acl(struct afs_acl *acl, const char *acl_str)
238 int nplus, nminus;
239 int aces;
241 char str[MAXSIZE];
242 char *p = str;
244 strlcpy(str, acl_str, MAXSIZE);
246 if (sscanf(p, "%d", &nplus) != 1)
247 return false;
249 DEBUG(10, ("Found %d positive entries\n", nplus));
251 if ((p = strchr(p, '\n')) == NULL)
252 return false;
253 p += 1;
255 if (sscanf(p, "%d", &nminus) != 1)
256 return false;
258 DEBUG(10, ("Found %d negative entries\n", nminus));
260 if ((p = strchr(p, '\n')) == NULL)
261 return false;
262 p += 1;
264 for (aces = nplus+nminus; aces > 0; aces--)
267 const char *namep;
268 fstring name;
269 uint32 rights;
270 char *space;
272 namep = p;
274 if ((p = strchr(p, '\t')) == NULL)
275 return false;
276 *p = '\0';
277 p += 1;
279 if (sscanf(p, "%d", &rights) != 1)
280 return false;
282 if ((p = strchr(p, '\n')) == NULL)
283 return false;
284 p += 1;
286 fstrcpy(name, namep);
288 while ((space = strchr_m(name, space_replacement)) != NULL)
289 *space = ' ';
291 add_afs_ace(acl, nplus>0, name, rights);
293 nplus -= 1;
296 return true;
299 static bool unparse_afs_acl(struct afs_acl *acl, char *acl_str)
301 /* TODO: String length checks!!!! */
303 int positives = 0;
304 int negatives = 0;
305 fstring line;
306 struct afs_ace *ace = acl->acelist;
308 *acl_str = 0;
310 while (ace != NULL) {
311 if (ace->positive)
312 positives++;
313 else
314 negatives++;
315 ace = ace->next;
318 fstr_sprintf(line, "%d\n", positives);
319 if (strlcat(acl_str, line, MAXSIZE) >= MAXSIZE) {
320 return false;
323 fstr_sprintf(line, "%d\n", negatives);
324 if (strlcat(acl_str, line, MAXSIZE) >= MAXSIZE) {
325 return false;
328 ace = acl->acelist;
330 while (ace != NULL) {
331 fstr_sprintf(line, "%s\t%d\n", ace->name, ace->rights);
332 if (strlcat(acl_str, line, MAXSIZE) >= MAXSIZE) {
333 return false;
335 ace = ace->next;
337 return true;
340 static uint32 afs_to_nt_file_rights(uint32 rights)
342 uint32 result = 0;
344 if (rights & PRSFS_READ)
345 result |= FILE_READ_DATA | FILE_READ_EA |
346 FILE_EXECUTE | FILE_READ_ATTRIBUTES |
347 READ_CONTROL_ACCESS | SYNCHRONIZE_ACCESS;
349 if (rights & PRSFS_WRITE)
350 result |= FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES |
351 FILE_WRITE_EA | FILE_APPEND_DATA;
353 if (rights & PRSFS_LOCK)
354 result |= WRITE_OWNER_ACCESS;
356 if (rights & PRSFS_DELETE)
357 result |= DELETE_ACCESS;
359 return result;
362 static void afs_to_nt_dir_rights(uint32 afs_rights, uint32 *nt_rights,
363 uint8 *flag)
365 *nt_rights = 0;
366 *flag = SEC_ACE_FLAG_OBJECT_INHERIT |
367 SEC_ACE_FLAG_CONTAINER_INHERIT;
369 if (afs_rights & PRSFS_INSERT)
370 *nt_rights |= FILE_ADD_FILE | FILE_ADD_SUBDIRECTORY;
372 if (afs_rights & PRSFS_LOOKUP)
373 *nt_rights |= FILE_READ_DATA | FILE_READ_EA |
374 FILE_EXECUTE | FILE_READ_ATTRIBUTES |
375 READ_CONTROL_ACCESS | SYNCHRONIZE_ACCESS;
377 if (afs_rights & PRSFS_WRITE)
378 *nt_rights |= FILE_WRITE_ATTRIBUTES | FILE_WRITE_DATA |
379 FILE_APPEND_DATA | FILE_WRITE_EA;
381 if ((afs_rights & (PRSFS_INSERT|PRSFS_LOOKUP|PRSFS_DELETE)) ==
382 (PRSFS_INSERT|PRSFS_LOOKUP|PRSFS_DELETE))
383 *nt_rights |= FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA |
384 GENERIC_WRITE_ACCESS;
386 if (afs_rights & PRSFS_DELETE)
387 *nt_rights |= DELETE_ACCESS;
389 if (afs_rights & PRSFS_ADMINISTER)
390 *nt_rights |= FILE_DELETE_CHILD | WRITE_DAC_ACCESS |
391 WRITE_OWNER_ACCESS;
393 if ( (afs_rights & PRSFS_LOOKUP) ==
394 (afs_rights & (PRSFS_LOOKUP|PRSFS_READ)) ) {
395 /* Only lookup right */
396 *flag = SEC_ACE_FLAG_CONTAINER_INHERIT;
399 return;
402 #define AFS_FILE_RIGHTS (PRSFS_READ|PRSFS_WRITE|PRSFS_LOCK)
403 #define AFS_DIR_RIGHTS (PRSFS_INSERT|PRSFS_LOOKUP|PRSFS_DELETE|PRSFS_ADMINISTER)
405 static void split_afs_acl(struct afs_acl *acl,
406 struct afs_acl *dir_acl,
407 struct afs_acl *file_acl)
409 struct afs_ace *ace;
411 init_afs_acl(dir_acl);
412 init_afs_acl(file_acl);
414 for (ace = acl->acelist; ace != NULL; ace = ace->next) {
415 if (ace->rights & AFS_FILE_RIGHTS) {
416 add_afs_ace(file_acl, ace->positive, ace->name,
417 ace->rights & AFS_FILE_RIGHTS);
420 if (ace->rights & AFS_DIR_RIGHTS) {
421 add_afs_ace(dir_acl, ace->positive, ace->name,
422 ace->rights & AFS_DIR_RIGHTS);
425 return;
428 static bool same_principal(struct afs_ace *x, struct afs_ace *y)
430 return ( (x->positive == y->positive) &&
431 (dom_sid_compare(&x->sid, &y->sid) == 0) );
434 static void merge_afs_acls(struct afs_acl *dir_acl,
435 struct afs_acl *file_acl,
436 struct afs_acl *target)
438 struct afs_ace *ace;
440 init_afs_acl(target);
442 for (ace = dir_acl->acelist; ace != NULL; ace = ace->next) {
443 struct afs_ace *file_ace;
444 bool found = false;
446 for (file_ace = file_acl->acelist;
447 file_ace != NULL;
448 file_ace = file_ace->next) {
449 if (!same_principal(ace, file_ace))
450 continue;
452 add_afs_ace(target, ace->positive, ace->name,
453 ace->rights | file_ace->rights);
454 found = true;
455 break;
457 if (!found)
458 add_afs_ace(target, ace->positive, ace->name,
459 ace->rights);
462 for (ace = file_acl->acelist; ace != NULL; ace = ace->next) {
463 struct afs_ace *dir_ace;
464 bool already_seen = false;
466 for (dir_ace = dir_acl->acelist;
467 dir_ace != NULL;
468 dir_ace = dir_ace->next) {
469 if (!same_principal(ace, dir_ace))
470 continue;
471 already_seen = true;
472 break;
474 if (!already_seen)
475 add_afs_ace(target, ace->positive, ace->name,
476 ace->rights);
480 #define PERMS_READ 0x001200a9
481 #define PERMS_CHANGE 0x001301bf
482 #define PERMS_FULL 0x001f01ff
484 static struct static_dir_ace_mapping {
485 uint8 type;
486 uint8 flags;
487 uint32 mask;
488 uint32 afs_rights;
489 } ace_mappings[] = {
491 /* Full control */
492 { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT,
493 PERMS_FULL, 127 /* rlidwka */ },
495 /* Change (write) */
496 { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT,
497 PERMS_CHANGE, 63 /* rlidwk */ },
499 /* Read (including list folder content) */
500 { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT,
501 PERMS_READ, 9 /* rl */ },
503 /* Read without list folder content -- same as "l" */
504 { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT,
505 0x00120089, 8 /* l */ },
507 /* some stupid workaround for preventing fallbacks */
508 { 0, 0x3, 0x0012019F, 9 /* rl */ },
509 { 0, 0x13, PERMS_FULL, 127 /* full */ },
511 /* read, delete and execute access plus synchronize */
512 { 0, 0x3, 0x001300A9, 9 /* should be rdl, set to rl */},
513 /* classical read list */
514 { 0, 0x13, 0x001200A9, 9 /* rl */},
515 /* almost full control, no delete */
516 { 0, 0x13, PERMS_CHANGE, 63 /* rwidlk */},
518 /* List folder */
519 { 0, SEC_ACE_FLAG_CONTAINER_INHERIT,
520 PERMS_READ, 8 /* l */ },
522 /* FULL without inheritance -- in all cases here we also get
523 the corresponding INHERIT_ONLY ACE in the same ACL */
524 { 0, 0, PERMS_FULL, 127 /* rlidwka */ },
526 /* FULL inherit only -- counterpart to previous one */
527 { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_INHERIT_ONLY,
528 PERMS_FULL | SEC_GENERIC_WRITE, 127 /* rlidwka */ },
530 /* CHANGE without inheritance -- in all cases here we also get
531 the corresponding INHERIT_ONLY ACE in the same ACL */
532 { 0, 0, PERMS_CHANGE, 63 /* rlidwk */ },
534 /* CHANGE inherit only -- counterpart to previous one */
535 { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_INHERIT_ONLY,
536 PERMS_CHANGE | SEC_GENERIC_WRITE, 63 /* rlidwk */ },
538 /* End marker, hopefully there's no afs right 9999 :-) */
539 { 0, 0, 0, 9999 }
542 static uint32 nt_to_afs_dir_rights(const char *filename, const struct security_ace *ace)
544 uint32 result = 0;
545 uint32 rights = ace->access_mask;
546 uint8 flags = ace->flags;
548 struct static_dir_ace_mapping *m;
550 for (m = &ace_mappings[0]; m->afs_rights != 9999; m++) {
551 if ( (ace->type == m->type) &&
552 (ace->flags == m->flags) &&
553 (ace->access_mask == m->mask) )
554 return m->afs_rights;
557 DEBUG(1, ("AFSACL FALLBACK: 0x%X 0x%X 0x%X %s %X\n",
558 ace->type, ace->flags, ace->access_mask, filename, rights));
560 if (rights & (GENERIC_ALL_ACCESS|WRITE_DAC_ACCESS)) {
561 result |= PRSFS_READ | PRSFS_WRITE | PRSFS_INSERT |
562 PRSFS_LOOKUP | PRSFS_DELETE | PRSFS_LOCK |
563 PRSFS_ADMINISTER;
566 if (rights & (GENERIC_READ_ACCESS|FILE_READ_DATA)) {
567 result |= PRSFS_LOOKUP;
568 if (flags & SEC_ACE_FLAG_OBJECT_INHERIT) {
569 result |= PRSFS_READ;
573 if (rights & (GENERIC_WRITE_ACCESS|FILE_WRITE_DATA)) {
574 result |= PRSFS_INSERT | PRSFS_DELETE;
575 if (flags & SEC_ACE_FLAG_OBJECT_INHERIT) {
576 result |= PRSFS_WRITE | PRSFS_LOCK;
580 return result;
583 static uint32 nt_to_afs_file_rights(const char *filename, const struct security_ace *ace)
585 uint32 result = 0;
586 uint32 rights = ace->access_mask;
588 if (rights & (GENERIC_READ_ACCESS|FILE_READ_DATA)) {
589 result |= PRSFS_READ;
592 if (rights & (GENERIC_WRITE_ACCESS|FILE_WRITE_DATA)) {
593 result |= PRSFS_WRITE | PRSFS_LOCK;
596 return result;
599 static size_t afs_to_nt_acl_common(struct afs_acl *afs_acl,
600 SMB_STRUCT_STAT *psbuf,
601 uint32 security_info,
602 struct security_descriptor **ppdesc)
604 struct security_ace *nt_ace_list;
605 struct dom_sid owner_sid, group_sid;
606 struct security_acl *psa = NULL;
607 int good_aces;
608 size_t sd_size;
609 TALLOC_CTX *mem_ctx = talloc_tos();
611 struct afs_ace *afs_ace;
613 uid_to_sid(&owner_sid, psbuf->st_ex_uid);
614 gid_to_sid(&group_sid, psbuf->st_ex_gid);
616 if (afs_acl->num_aces) {
617 nt_ace_list = talloc_array(mem_ctx, struct security_ace, afs_acl->num_aces);
619 if (nt_ace_list == NULL)
620 return 0;
621 } else {
622 nt_ace_list = NULL;
625 afs_ace = afs_acl->acelist;
626 good_aces = 0;
628 while (afs_ace != NULL) {
629 uint32_t nt_rights;
630 uint8 flag = SEC_ACE_FLAG_OBJECT_INHERIT |
631 SEC_ACE_FLAG_CONTAINER_INHERIT;
633 if (afs_ace->type == SID_NAME_UNKNOWN) {
634 DEBUG(10, ("Ignoring unknown name %s\n",
635 afs_ace->name));
636 afs_ace = afs_ace->next;
637 continue;
640 if (S_ISDIR(psbuf->st_ex_mode))
641 afs_to_nt_dir_rights(afs_ace->rights, &nt_rights,
642 &flag);
643 else
644 nt_rights = afs_to_nt_file_rights(afs_ace->rights);
646 init_sec_ace(&nt_ace_list[good_aces++], &(afs_ace->sid),
647 SEC_ACE_TYPE_ACCESS_ALLOWED, nt_rights, flag);
648 afs_ace = afs_ace->next;
651 psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION,
652 good_aces, nt_ace_list);
653 if (psa == NULL)
654 return 0;
656 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
657 SEC_DESC_SELF_RELATIVE,
658 (security_info & SECINFO_OWNER)
659 ? &owner_sid : NULL,
660 (security_info & SECINFO_GROUP)
661 ? &group_sid : NULL,
662 NULL, psa, &sd_size);
664 return sd_size;
667 static size_t afs_to_nt_acl(struct afs_acl *afs_acl,
668 struct connection_struct *conn,
669 struct smb_filename *smb_fname,
670 uint32 security_info,
671 struct security_descriptor **ppdesc)
673 int ret;
675 /* Get the stat struct for the owner info. */
676 if (lp_posix_pathnames()) {
677 ret = SMB_VFS_LSTAT(conn, smb_fname);
678 } else {
679 ret = SMB_VFS_STAT(conn, smb_fname);
681 if (ret == -1) {
682 return 0;
685 return afs_to_nt_acl_common(afs_acl, &smb_fname->st, security_info,
686 ppdesc);
689 static size_t afs_fto_nt_acl(struct afs_acl *afs_acl,
690 struct files_struct *fsp,
691 uint32 security_info,
692 struct security_descriptor **ppdesc)
694 SMB_STRUCT_STAT sbuf;
696 if (fsp->fh->fd == -1) {
697 /* Get the stat struct for the owner info. */
698 return afs_to_nt_acl(afs_acl, fsp->conn, fsp->fsp_name,
699 security_info, ppdesc);
702 if(SMB_VFS_FSTAT(fsp, &sbuf) != 0) {
703 return 0;
706 return afs_to_nt_acl_common(afs_acl, &sbuf, security_info, ppdesc);
709 static bool mappable_sid(const struct dom_sid *sid)
711 struct dom_sid domain_sid;
713 if (dom_sid_compare(sid, &global_sid_Builtin_Administrators) == 0)
714 return true;
716 if (dom_sid_compare(sid, &global_sid_World) == 0)
717 return true;
719 if (dom_sid_compare(sid, &global_sid_Authenticated_Users) == 0)
720 return true;
722 if (dom_sid_compare(sid, &global_sid_Builtin_Backup_Operators) == 0)
723 return true;
725 string_to_sid(&domain_sid, "S-1-5-21");
727 if (sid_compare_domain(sid, &domain_sid) == 0)
728 return true;
730 return false;
733 static bool nt_to_afs_acl(const char *filename,
734 uint32 security_info_sent,
735 const struct security_descriptor *psd,
736 uint32 (*nt_to_afs_rights)(const char *filename,
737 const struct security_ace *ace),
738 struct afs_acl *afs_acl)
740 const struct security_acl *dacl;
741 int i;
743 /* Currently we *only* look at the dacl */
745 if (((security_info_sent & SECINFO_DACL) == 0) ||
746 (psd->dacl == NULL))
747 return true;
749 if (!init_afs_acl(afs_acl))
750 return false;
752 dacl = psd->dacl;
754 for (i = 0; i < dacl->num_aces; i++) {
755 const struct security_ace *ace = &(dacl->aces[i]);
756 const char *dom_name, *name;
757 enum lsa_SidType name_type;
758 char *p;
760 if (ace->type != SEC_ACE_TYPE_ACCESS_ALLOWED) {
761 /* First cut: Only positive ACEs */
762 return false;
765 if (!mappable_sid(&ace->trustee)) {
766 DEBUG(10, ("Ignoring unmappable SID %s\n",
767 sid_string_dbg(&ace->trustee)));
768 continue;
771 if (dom_sid_compare(&ace->trustee,
772 &global_sid_Builtin_Administrators) == 0) {
774 name = "system:administrators";
776 } else if (dom_sid_compare(&ace->trustee,
777 &global_sid_World) == 0) {
779 name = "system:anyuser";
781 } else if (dom_sid_compare(&ace->trustee,
782 &global_sid_Authenticated_Users) == 0) {
784 name = "system:authuser";
786 } else if (dom_sid_compare(&ace->trustee,
787 &global_sid_Builtin_Backup_Operators)
788 == 0) {
790 name = "system:backup";
792 } else {
794 if (!lookup_sid(talloc_tos(), &ace->trustee,
795 &dom_name, &name, &name_type)) {
796 DEBUG(1, ("AFSACL: Could not lookup SID %s on file %s\n",
797 sid_string_dbg(&ace->trustee),
798 filename));
799 continue;
802 if ( (name_type == SID_NAME_USER) ||
803 (name_type == SID_NAME_DOM_GRP) ||
804 (name_type == SID_NAME_ALIAS) ) {
805 char *tmp;
806 tmp = talloc_asprintf(talloc_tos(), "%s%s%s",
807 dom_name, lp_winbind_separator(),
808 name);
809 if (tmp == NULL) {
810 return false;
812 strlower_m(tmp);
813 name = tmp;
816 if (sidpts) {
817 /* Expect all users/groups in pts as SIDs */
818 name = talloc_strdup(
819 talloc_tos(),
820 sid_string_tos(&ace->trustee));
821 if (name == NULL) {
822 return false;
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));
834 return true;
837 static bool afs_get_afs_acl(const char *filename, struct afs_acl *acl)
839 struct afs_iob iob;
841 int ret;
843 char space[MAXSIZE];
845 DEBUG(5, ("afs_get_afs_acl: %s\n", filename));
847 iob.in_size = 0;
848 iob.out_size = MAXSIZE;
849 iob.in = iob.out = space;
851 ret = afs_syscall(AFSCALL_PIOCTL, filename, VIOCGETAL,
852 (char *)&iob, 0);
854 if (ret) {
855 DEBUG(1, ("got error from PIOCTL: %d\n", ret));
856 return false;
859 if (!init_afs_acl(acl))
860 return false;
862 if (!parse_afs_acl(acl, space)) {
863 DEBUG(1, ("Could not parse AFS acl\n"));
864 free_afs_acl(acl);
865 return false;
868 return true;
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)
876 struct afs_ace *ace;
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",
884 ace->name));
885 continue;
888 DEBUG(10, ("Merging unknown ACE for %s\n", ace->name));
890 copy = clone_afs_ace(dst->ctx, ace);
892 if (copy == NULL) {
893 DEBUG(0, ("Could not clone ACE for %s\n", ace->name));
894 continue;
897 copy->next = dst->acelist;
898 dst->acelist = copy;
899 dst->num_aces += 1;
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];
910 struct afs_iob iob;
911 int ret = -1;
912 char *name = NULL;
913 const char *fileacls;
915 fileacls = lp_parm_const_string(SNUM(handle->conn), "afsacl", "fileacls",
916 "yes");
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);
926 if (!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, '/');
934 if (p != NULL) {
935 *p = '\0';
936 } else {
937 name = talloc_strdup(talloc_tos(), ".");
938 if (!name) {
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)));
946 goto done;
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
956 * permissions */
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))
964 goto done;
965 } else {
966 if (strequal(fileacls, "no")) {
967 ret = -1;
968 goto done;
971 if (strequal(fileacls, "ignore")) {
972 ret = 0;
973 goto done;
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))
980 goto done;
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);
989 iob.in = acl_string;
990 iob.in_size = 1+strlen(iob.in);
991 iob.out = NULL;
992 iob.out_size = 0;
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);
998 if (ret != 0) {
999 DEBUG(10, ("VIOCSETAL returned %d\n", ret));
1002 done:
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 struct security_descriptor **ppdesc)
1016 struct afs_acl acl;
1017 size_t sd_size;
1019 DEBUG(5, ("afsacl_fget_nt_acl: %s\n", fsp_str_dbg(fsp)));
1021 sidpts = lp_parm_bool(SNUM(fsp->conn), "afsacl", "sidpts", false);
1023 if (!afs_get_afs_acl(fsp->fsp_name->base_name, &acl)) {
1024 return NT_STATUS_ACCESS_DENIED;
1027 sd_size = afs_fto_nt_acl(&acl, fsp, security_info, ppdesc);
1029 free_afs_acl(&acl);
1031 return (sd_size != 0) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1034 static NTSTATUS afsacl_get_nt_acl(struct vfs_handle_struct *handle,
1035 const char *name, uint32 security_info,
1036 struct security_descriptor **ppdesc)
1038 struct afs_acl acl;
1039 size_t sd_size;
1040 struct smb_filename *smb_fname = NULL;
1041 NTSTATUS status;
1043 DEBUG(5, ("afsacl_get_nt_acl: %s\n", name));
1045 sidpts = lp_parm_bool(SNUM(handle->conn), "afsacl", "sidpts", false);
1047 if (!afs_get_afs_acl(name, &acl)) {
1048 return NT_STATUS_ACCESS_DENIED;
1051 status = create_synthetic_smb_fname(talloc_tos(), name, NULL, NULL,
1052 &smb_fname);
1053 if (!NT_STATUS_IS_OK(status)) {
1054 free_afs_acl(&acl);
1055 return status;
1058 sd_size = afs_to_nt_acl(&acl, handle->conn, smb_fname, security_info,
1059 ppdesc);
1060 TALLOC_FREE(smb_fname);
1062 free_afs_acl(&acl);
1064 return (sd_size != 0) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1067 NTSTATUS afsacl_fset_nt_acl(vfs_handle_struct *handle,
1068 files_struct *fsp,
1069 uint32 security_info_sent,
1070 const struct security_descriptor *psd)
1072 return afs_set_nt_acl(handle, fsp, security_info_sent, psd);
1075 static int afsacl_connect(vfs_handle_struct *handle,
1076 const char *service,
1077 const char *user)
1079 const char *spc;
1080 int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
1082 if (ret < 0) {
1083 return ret;
1086 spc = lp_parm_const_string(SNUM(handle->conn), "afsacl", "space", "%");
1088 if (spc != NULL)
1089 space_replacement = spc[0];
1091 return 0;
1094 static struct vfs_fn_pointers vfs_afsacl_fns = {
1095 .connect_fn = afsacl_connect,
1096 .fget_nt_acl_fn = afsacl_fget_nt_acl,
1097 .get_nt_acl_fn = afsacl_get_nt_acl,
1098 .fset_nt_acl_fn = afsacl_fset_nt_acl
1101 NTSTATUS vfs_afsacl_init(void);
1102 NTSTATUS vfs_afsacl_init(void)
1104 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "afsacl",
1105 &vfs_afsacl_fns);