r11585: Implement the possibility to have AFS users as SIDs in pts.
[samba.git] / source3 / modules / vfs_afsacl.c
1 /* 
2  * Convert AFS acls to NT acls and vice versa.
3  *
4  * Copyright (C) Volker Lendecke, 2003
5  *
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.
10  *  
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.
15  *  
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.
19  */
20
21 #include "includes.h"
22
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_VFS
25
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>
31
32 #define MAXSIZE 2048
33
34 extern const DOM_SID global_sid_World;
35 extern const DOM_SID global_sid_Builtin_Administrators;
36 extern const DOM_SID global_sid_Builtin_Backup_Operators;
37 extern const DOM_SID global_sid_Authenticated_Users;
38 extern const DOM_SID global_sid_NULL;
39
40 static char space_replacement = '%';
41
42 /* Do we expect SIDs as pts names? */
43 static BOOL sidpts;
44
45 extern int afs_syscall(int, char *, int, char *, int);
46
47 struct afs_ace {
48         BOOL positive;
49         char *name;
50         DOM_SID sid;
51         enum SID_NAME_USE type;
52         uint32 rights;
53         struct afs_ace *next;
54 };
55
56 struct afs_acl {
57         TALLOC_CTX *ctx;
58         int type;
59         int num_aces;
60         struct afs_ace *acelist;
61 };
62
63 struct afs_iob {
64         char *in, *out;
65         uint16 in_size, out_size;
66 };
67
68
69 static BOOL init_afs_acl(struct afs_acl *acl)
70 {
71         ZERO_STRUCT(*acl);
72         acl->ctx = talloc_init("afs_acl");
73         if (acl->ctx == NULL) {
74                 DEBUG(10, ("Could not init afs_acl"));
75                 return False;
76         }
77         return True;
78 }
79
80 static void free_afs_acl(struct afs_acl *acl)
81 {
82         if (acl->ctx != NULL)
83                 talloc_destroy(acl->ctx);
84         acl->ctx = NULL;
85         acl->num_aces = 0;
86         acl->acelist = NULL;
87 }
88
89 static struct afs_ace *clone_afs_ace(TALLOC_CTX *mem_ctx, struct afs_ace *ace)
90 {
91         struct afs_ace *result = TALLOC_P(mem_ctx, struct afs_ace);
92
93         if (result == NULL)
94                 return NULL;
95
96         *result = *ace;
97
98         result->next = NULL;
99         result->name = talloc_strdup(mem_ctx, ace->name);
100
101         if (result->name == NULL) {
102                 return NULL;
103         }
104
105         return result;
106 }
107         
108
109 /* Ok, this is sort-of a hack. We assume here that we have winbind users in
110  * AFS. And yet another copy of parse_domain_user.... */
111
112 static BOOL parse_domain_user(const char *domuser, fstring domain,
113                               fstring user)
114 {
115         char *p = strchr(domuser,*lp_winbind_separator());
116
117         if (p==NULL) {
118                 return False;
119         }
120
121         fstrcpy(user, p+1);
122         fstrcpy(domain, domuser);
123         domain[PTR_DIFF(p, domuser)] = 0;
124         strupper_m(domain);
125         
126         return True;
127 }
128
129 static struct afs_ace *new_afs_ace(TALLOC_CTX *mem_ctx,
130                                    BOOL positive,
131                                    const char *name, uint32 rights)
132 {
133         DOM_SID sid;
134         enum SID_NAME_USE type;
135         struct afs_ace *result;
136
137         if (strcmp(name, "system:administrators") == 0) {
138
139                 sid_copy(&sid, &global_sid_Builtin_Administrators);
140                 type = SID_NAME_ALIAS;
141
142         } else if (strcmp(name, "system:anyuser") == 0) {
143
144                 sid_copy(&sid, &global_sid_World);
145                 type = SID_NAME_ALIAS;
146
147         } else if (strcmp(name, "system:authuser") == 0) {
148
149                 sid_copy(&sid, &global_sid_Authenticated_Users);
150                 type = SID_NAME_WKN_GRP;
151
152         } else if (strcmp(name, "system:backup") == 0) {
153
154                 sid_copy(&sid, &global_sid_Builtin_Backup_Operators);
155                 type = SID_NAME_ALIAS;
156
157         } else if (sidpts) {
158                 /* All PTS users/groups are expressed as SIDs */
159
160                 sid_copy(&sid, &global_sid_NULL);
161                 type = SID_NAME_UNKNOWN;
162
163                 if (string_to_sid(&sid, name)) {
164                         fstring user, domain;
165                         /* We have to find the type, look up the SID */
166                         lookup_sid(&sid, domain, user, &type);
167                 }
168
169         } else {
170
171                 fstring user, domain;
172
173                 if (!parse_domain_user(name, domain, user)) {
174                         fstrcpy(user, name);
175                         fstrcpy(domain, lp_workgroup());
176                 }
177                     
178                 if (!lookup_name(domain, user, &sid, &type)) {
179                         DEBUG(10, ("Could not find AFS user %s\n", name));
180
181                         sid_copy(&sid, &global_sid_NULL);
182                         type = SID_NAME_UNKNOWN;
183
184                 }
185         }
186
187         result = TALLOC_P(mem_ctx, struct afs_ace);
188
189         if (result == NULL) {
190                 DEBUG(0, ("Could not talloc AFS ace\n"));
191                 return NULL;
192         }
193
194         result->name = talloc_strdup(mem_ctx, name);
195         if (result->name == NULL) {
196                 DEBUG(0, ("Could not talloc AFS ace name\n"));
197                 return NULL;
198         }
199
200         result->sid = sid;
201         result->type = type;
202
203         result->positive = positive;
204         result->rights = rights;
205
206         return result;
207 }
208
209 static void add_afs_ace(struct afs_acl *acl,
210                         BOOL positive,
211                         const char *name, uint32 rights)
212 {
213         struct afs_ace *ace;
214
215         for (ace = acl->acelist; ace != NULL; ace = ace->next) {
216                 if ((ace->positive == positive) &&
217                     (strequal(ace->name, name))) {
218                         ace->rights |= rights;
219                         return;
220                 }
221         }
222
223         ace = new_afs_ace(acl->ctx, positive, name, rights);
224
225         ace->next = acl->acelist;
226         acl->acelist = ace;
227
228         acl->num_aces += 1;
229
230         DEBUG(10, ("add_afs_ace: Added %s entry for %s with rights %d\n",
231                    ace->positive?"positive":"negative",
232                    ace->name, ace->rights));
233
234         return;
235 }
236
237 /* AFS ACLs in string form are a long string of fields delimited with \n.
238  *
239  * First line:  Number of positive entries
240  * Second line: Number of negative entries
241  * Third and following lines: The entries themselves
242  *
243  * An ACE is a line of two fields, delimited by \t.
244  *
245  * First field:  Name
246  * Second field: Rights
247  */
248
249 static BOOL parse_afs_acl(struct afs_acl *acl, const char *acl_str)
250 {
251         int nplus, nminus;
252         int aces;
253
254         char str[MAXSIZE+1];
255         char *p = str;
256
257         strncpy(str, acl_str, MAXSIZE);
258
259         if (sscanf(p, "%d", &nplus) != 1)
260                 return False;
261
262         DEBUG(10, ("Found %d positive entries\n", nplus));
263
264         if ((p = strchr(p, '\n')) == NULL)
265                 return False;
266         p += 1;
267
268         if (sscanf(p, "%d", &nminus) != 1)
269                 return False;
270         
271         DEBUG(10, ("Found %d negative entries\n", nminus));
272
273         if ((p = strchr(p, '\n')) == NULL)
274                 return False;
275         p += 1;
276
277         for (aces = nplus+nminus; aces > 0; aces--)
278         {
279
280                 const char *namep;
281                 fstring name;
282                 uint32 rights;
283                 char *space;
284
285                 namep = p;
286
287                 if ((p = strchr(p, '\t')) == NULL)
288                         return False;
289                 *p = '\0';
290                 p += 1;
291
292                 if (sscanf(p, "%d", &rights) != 1)
293                         return False;
294
295                 if ((p = strchr(p, '\n')) == NULL)
296                         return False;
297                 p += 1;
298
299                 fstrcpy(name, namep);
300
301                 while ((space = strchr_m(name, space_replacement)) != NULL)
302                         *space = ' ';
303
304                 add_afs_ace(acl, nplus>0, name, rights);
305
306                 nplus -= 1;
307         }
308
309         return True;
310 }
311
312 static BOOL unparse_afs_acl(struct afs_acl *acl, char *acl_str)
313 {
314         /* TODO: String length checks!!!! */
315
316         int positives = 0;
317         int negatives = 0;
318         fstring line;
319
320         *acl_str = 0;
321
322         struct afs_ace *ace = acl->acelist;
323
324         while (ace != NULL) {
325                 if (ace->positive)
326                         positives++;
327                 else
328                         negatives++;
329                 ace = ace->next;
330         }
331
332         fstr_sprintf(line, "%d\n", positives);
333         safe_strcat(acl_str, line, MAXSIZE);
334
335         fstr_sprintf(line, "%d\n", negatives);
336         safe_strcat(acl_str, line, MAXSIZE);
337
338         ace = acl->acelist;
339
340         while (ace != NULL) {
341                 fstr_sprintf(line, "%s\t%d\n", ace->name, ace->rights);
342                 safe_strcat(acl_str, line, MAXSIZE);
343                 ace = ace->next;
344         }
345         return True;
346 }
347
348 static uint32 afs_to_nt_file_rights(uint32 rights)
349 {
350         uint32 result = 0;
351
352         if (rights & PRSFS_READ)
353                 result |= FILE_READ_DATA | FILE_READ_EA | 
354                         FILE_EXECUTE | FILE_READ_ATTRIBUTES |
355                         READ_CONTROL_ACCESS | SYNCHRONIZE_ACCESS;
356
357         if (rights & PRSFS_WRITE)
358                 result |= FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES |
359                         FILE_WRITE_EA | FILE_APPEND_DATA;
360
361         if (rights & PRSFS_LOCK)
362                 result |= WRITE_OWNER_ACCESS;
363
364         if (rights & PRSFS_DELETE)
365                 result |= DELETE_ACCESS;
366
367         return result;
368 }
369
370 static void afs_to_nt_dir_rights(uint32 afs_rights, uint32 *nt_rights,
371                                  uint8 *flag)
372 {
373         *nt_rights = 0;
374         *flag = SEC_ACE_FLAG_OBJECT_INHERIT |
375                 SEC_ACE_FLAG_CONTAINER_INHERIT;
376
377         if (afs_rights & PRSFS_INSERT)
378                 *nt_rights |= FILE_ADD_FILE | FILE_ADD_SUBDIRECTORY;
379
380         if (afs_rights & PRSFS_LOOKUP)
381                 *nt_rights |= FILE_READ_DATA | FILE_READ_EA | 
382                         FILE_EXECUTE | FILE_READ_ATTRIBUTES |
383                         READ_CONTROL_ACCESS | SYNCHRONIZE_ACCESS;
384
385         if (afs_rights & PRSFS_WRITE)
386                 *nt_rights |= FILE_WRITE_ATTRIBUTES | FILE_WRITE_DATA |
387                         FILE_APPEND_DATA | FILE_WRITE_EA;
388
389         if ((afs_rights & (PRSFS_INSERT|PRSFS_LOOKUP|PRSFS_DELETE)) ==
390             (PRSFS_INSERT|PRSFS_LOOKUP|PRSFS_DELETE))
391                 *nt_rights |= FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA |
392                         GENERIC_WRITE_ACCESS;
393
394         if (afs_rights & PRSFS_DELETE)
395                 *nt_rights |= DELETE_ACCESS;
396
397         if (afs_rights & PRSFS_ADMINISTER)
398                 *nt_rights |= FILE_DELETE_CHILD | WRITE_DAC_ACCESS |
399                         WRITE_OWNER_ACCESS;
400
401         if ( (afs_rights & PRSFS_LOOKUP) ==
402              (afs_rights & (PRSFS_LOOKUP|PRSFS_READ)) ) {
403                 /* Only lookup right */
404                 *flag = SEC_ACE_FLAG_CONTAINER_INHERIT;
405         }
406
407         return;
408 }
409
410 #define AFS_FILE_RIGHTS (PRSFS_READ|PRSFS_WRITE|PRSFS_LOCK)
411 #define AFS_DIR_RIGHTS  (PRSFS_INSERT|PRSFS_LOOKUP|PRSFS_DELETE|PRSFS_ADMINISTER)
412
413 static void split_afs_acl(struct afs_acl *acl,
414                           struct afs_acl *dir_acl,
415                           struct afs_acl *file_acl)
416 {
417         struct afs_ace *ace;
418
419         init_afs_acl(dir_acl);
420         init_afs_acl(file_acl);
421
422         for (ace = acl->acelist; ace != NULL; ace = ace->next) {
423                 if (ace->rights & AFS_FILE_RIGHTS) {
424                         add_afs_ace(file_acl, ace->positive, ace->name,
425                                     ace->rights & AFS_FILE_RIGHTS);
426                 }
427
428                 if (ace->rights & AFS_DIR_RIGHTS) {
429                         add_afs_ace(dir_acl, ace->positive, ace->name,
430                                     ace->rights & AFS_DIR_RIGHTS);
431                 }
432         }
433         return;
434 }
435
436 static BOOL same_principal(struct afs_ace *x, struct afs_ace *y)
437 {
438         return ( (x->positive == y->positive) &&
439                  (sid_compare(&x->sid, &y->sid) == 0) );
440 }
441
442 static void merge_afs_acls(struct afs_acl *dir_acl,
443                            struct afs_acl *file_acl,
444                            struct afs_acl *target)
445 {
446         struct afs_ace *ace;
447
448         init_afs_acl(target);
449
450         for (ace = dir_acl->acelist; ace != NULL; ace = ace->next) {
451                 struct afs_ace *file_ace;
452                 BOOL found = False;
453
454                 for (file_ace = file_acl->acelist;
455                      file_ace != NULL;
456                      file_ace = file_ace->next) {
457                         if (!same_principal(ace, file_ace))
458                                 continue;
459
460                         add_afs_ace(target, ace->positive, ace->name,
461                                     ace->rights | file_ace->rights);
462                         found = True;
463                         break;
464                 }
465                 if (!found)
466                         add_afs_ace(target, ace->positive, ace->name,
467                                     ace->rights);
468         }
469
470         for (ace = file_acl->acelist; ace != NULL; ace = ace->next) {
471                 struct afs_ace *dir_ace;
472                 BOOL already_seen = False;
473
474                 for (dir_ace = dir_acl->acelist;
475                      dir_ace != NULL;
476                      dir_ace = dir_ace->next) {
477                         if (!same_principal(ace, dir_ace))
478                                 continue;
479                         already_seen = True;
480                         break;
481                 }
482                 if (!already_seen)
483                         add_afs_ace(target, ace->positive, ace->name,
484                                     ace->rights);
485         }
486 }
487
488 #define PERMS_READ   0x001200a9
489 #define PERMS_CHANGE 0x001301bf
490 #define PERMS_FULL   0x001f01ff
491
492 static struct static_dir_ace_mapping {
493         uint8 type;
494         uint8 flags;
495         uint32 mask;
496         uint32 afs_rights;
497 } ace_mappings[] = {
498
499         /* Full control */
500         { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT,
501           PERMS_FULL, 127 /* rlidwka */ },
502
503         /* Change (write) */
504         { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT,
505           PERMS_CHANGE, 63 /* rlidwk */ },
506
507         /* Read (including list folder content) */
508         { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT,
509           PERMS_READ, 9 /* rl */ },
510
511         /* Read without list folder content -- same as "l" */
512         { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT,
513           0x00120089, 8 /* l */ },
514
515         /* some stupid workaround for preventing fallbacks */   
516         { 0, 0x3, 0x0012019F, 9 /* rl */ },
517         { 0, 0x13, PERMS_FULL, 127 /* full */ },
518         
519         /* read, delete and execute access plus synchronize */
520         { 0, 0x3, 0x001300A9, 9 /* should be rdl, set to rl */},
521         /* classical read list */
522         { 0, 0x13, 0x001200A9, 9 /* rl */},
523         /* almost full control, no delete */
524         { 0, 0x13, PERMS_CHANGE, 63 /* rwidlk */},
525
526         /* List folder */
527         { 0, SEC_ACE_FLAG_CONTAINER_INHERIT,
528           PERMS_READ, 8 /* l */ },
529
530         /* FULL without inheritance -- in all cases here we also get
531            the corresponding INHERIT_ONLY ACE in the same ACL */
532         { 0, 0, PERMS_FULL, 127 /* rlidwka */ },
533
534         /* FULL 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_FULL | GENERIC_RIGHT_WRITE_ACCESS, 127 /* rlidwka */ },
537
538         /* CHANGE without inheritance -- in all cases here we also get
539            the corresponding INHERIT_ONLY ACE in the same ACL */
540         { 0, 0, PERMS_CHANGE, 63 /* rlidwk */ },
541
542         /* CHANGE inherit only -- counterpart to previous one */
543         { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_INHERIT_ONLY,
544           PERMS_CHANGE | GENERIC_RIGHT_WRITE_ACCESS, 63 /* rlidwk */ },
545
546         /* End marker, hopefully there's no afs right 9999 :-) */
547         { 0, 0, 0, 9999 }
548 };
549
550 static uint32 nt_to_afs_dir_rights(const char *filename, const SEC_ACE *ace)
551 {
552         uint32 result = 0;
553         uint32 rights = ace->info.mask;
554         uint8 flags = ace->flags;
555
556         struct static_dir_ace_mapping *m;
557
558         for (m = &ace_mappings[0]; m->afs_rights != 9999; m++) {
559                 if ( (ace->type == m->type) &&
560                      (ace->flags == m->flags) &&
561                      (ace->info.mask == m->mask) )
562                         return m->afs_rights;
563         }
564
565         DEBUG(1, ("AFSACL FALLBACK: 0x%X 0x%X 0x%X %s %X\n",
566                   ace->type, ace->flags, ace->info.mask, filename, rights));
567
568         if (rights & (GENERIC_ALL_ACCESS|WRITE_DAC_ACCESS)) {
569                 result |= PRSFS_READ | PRSFS_WRITE | PRSFS_INSERT |
570                         PRSFS_LOOKUP | PRSFS_DELETE | PRSFS_LOCK |
571                         PRSFS_ADMINISTER;
572         }
573
574         if (rights & (GENERIC_READ_ACCESS|FILE_READ_DATA)) {
575                 result |= PRSFS_LOOKUP;
576                 if (flags & SEC_ACE_FLAG_OBJECT_INHERIT) {
577                         result |= PRSFS_READ;
578                 }
579         }
580
581         if (rights & (GENERIC_WRITE_ACCESS|FILE_WRITE_DATA)) {
582                 result |= PRSFS_INSERT | PRSFS_DELETE;
583                 if (flags & SEC_ACE_FLAG_OBJECT_INHERIT) {
584                         result |= PRSFS_WRITE | PRSFS_LOCK;
585                 }
586         }
587
588         return result;
589 }
590
591 static uint32 nt_to_afs_file_rights(const char *filename, const SEC_ACE *ace)
592 {
593         uint32 result = 0;
594         uint32 rights = ace->info.mask;
595
596         if (rights & (GENERIC_READ_ACCESS|FILE_READ_DATA)) {
597                 result |= PRSFS_READ;
598         }
599
600         if (rights & (GENERIC_WRITE_ACCESS|FILE_WRITE_DATA)) {
601                 result |= PRSFS_WRITE | PRSFS_LOCK;
602         }
603
604         return result;
605 }
606
607 static size_t afs_to_nt_acl(struct afs_acl *afs_acl, 
608                             struct files_struct *fsp,
609                             uint32 security_info,
610                             struct security_descriptor_info **ppdesc)
611 {
612         SEC_ACE *nt_ace_list;
613         DOM_SID owner_sid, group_sid;
614         SEC_ACCESS mask;
615         SMB_STRUCT_STAT sbuf;
616         SEC_ACL *psa = NULL;
617         int good_aces;
618         size_t sd_size;
619         TALLOC_CTX *mem_ctx = main_loop_talloc_get();
620
621         struct afs_ace *afs_ace;
622
623         if (fsp->is_directory || fsp->fh->fd == -1) {
624                 /* Get the stat struct for the owner info. */
625                 if(SMB_VFS_STAT(fsp->conn,fsp->fsp_name, &sbuf) != 0) {
626                         return 0;
627                 }
628         } else {
629                 if(SMB_VFS_FSTAT(fsp,fsp->fh->fd,&sbuf) != 0) {
630                         return 0;
631                 }
632         }
633
634         uid_to_sid(&owner_sid, sbuf.st_uid);
635         gid_to_sid(&group_sid, sbuf.st_gid);
636
637         nt_ace_list = TALLOC_ARRAY(mem_ctx, SEC_ACE, afs_acl->num_aces);
638
639         if (nt_ace_list == NULL)
640                 return 0;
641
642         afs_ace = afs_acl->acelist;
643         good_aces = 0;
644
645         while (afs_ace != NULL) {
646                 uint32 nt_rights;
647                 uint8 flag = SEC_ACE_FLAG_OBJECT_INHERIT |
648                         SEC_ACE_FLAG_CONTAINER_INHERIT;
649
650                 if (afs_ace->type == SID_NAME_UNKNOWN) {
651                         DEBUG(10, ("Ignoring unknown name %s\n",
652                                    afs_ace->name));
653                         afs_ace = afs_ace->next;
654                         continue;
655                 }
656
657                 if (fsp->is_directory)
658                         afs_to_nt_dir_rights(afs_ace->rights, &nt_rights,
659                                              &flag);
660                 else
661                         nt_rights = afs_to_nt_file_rights(afs_ace->rights);
662
663                 init_sec_access(&mask, nt_rights);
664                 init_sec_ace(&nt_ace_list[good_aces++], &(afs_ace->sid),
665                              SEC_ACE_TYPE_ACCESS_ALLOWED, mask, flag);
666                 afs_ace = afs_ace->next;
667         }
668
669         psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION,
670                            good_aces, nt_ace_list);
671         if (psa == NULL)
672                 return 0;
673
674         
675         *ppdesc = make_sec_desc(mem_ctx, SEC_DESC_REVISION,
676                                 SEC_DESC_SELF_RELATIVE,
677                                 (security_info & OWNER_SECURITY_INFORMATION)
678                                 ? &owner_sid : NULL,
679                                 (security_info & GROUP_SECURITY_INFORMATION)
680                                 ? &group_sid : NULL,
681                                 NULL, psa, &sd_size);
682
683         return sd_size;
684 }
685
686 static BOOL mappable_sid(const DOM_SID *sid)
687 {
688         DOM_SID domain_sid;
689         
690         if (sid_compare(sid, &global_sid_Builtin_Administrators) == 0)
691                 return True;
692
693         if (sid_compare(sid, &global_sid_World) == 0)
694                 return True;
695
696         if (sid_compare(sid, &global_sid_Authenticated_Users) == 0)
697                 return True;
698
699         if (sid_compare(sid, &global_sid_Builtin_Backup_Operators) == 0)
700                 return True;
701
702         string_to_sid(&domain_sid, "S-1-5-21");
703
704         if (sid_compare_domain(sid, &domain_sid) == 0)
705                 return True;
706
707         return False;
708 }
709
710 static BOOL nt_to_afs_acl(const char *filename,
711                           uint32 security_info_sent,
712                           struct security_descriptor_info *psd,
713                           uint32 (*nt_to_afs_rights)(const char *filename,
714                                                      const SEC_ACE *ace),
715                           struct afs_acl *afs_acl)
716 {
717         SEC_ACL *dacl;
718         int i;
719
720         /* Currently we *only* look at the dacl */
721
722         if (((security_info_sent & DACL_SECURITY_INFORMATION) == 0) ||
723             (psd->dacl == NULL))
724                 return True;
725
726         if (!init_afs_acl(afs_acl))
727                 return False;
728
729         dacl = psd->dacl;
730
731         for (i = 0; i < dacl->num_aces; i++) {
732                 SEC_ACE *ace = &(dacl->ace[i]);
733                 fstring dom_name;
734                 fstring name;
735                 enum SID_NAME_USE name_type;
736                 char *p;
737
738                 if (ace->type != SEC_ACE_TYPE_ACCESS_ALLOWED) {
739                         /* First cut: Only positive ACEs */
740                         return False;
741                 }
742
743                 if (!mappable_sid(&ace->trustee)) {
744                         DEBUG(10, ("Ignoring unmappable SID %s\n",
745                                    sid_string_static(&ace->trustee)));
746                         continue;
747                 }
748
749                 if (sid_compare(&ace->trustee,
750                                 &global_sid_Builtin_Administrators) == 0) {
751
752                         fstrcpy(name, "system:administrators");
753
754                 } else if (sid_compare(&ace->trustee,
755                                        &global_sid_World) == 0) {
756
757                         fstrcpy(name, "system:anyuser");
758
759                 } else if (sid_compare(&ace->trustee,
760                                        &global_sid_Authenticated_Users) == 0) {
761
762                         fstrcpy(name, "system:authuser");
763
764                 } else if (sid_compare(&ace->trustee,
765                                        &global_sid_Builtin_Backup_Operators)
766                            == 0) {
767
768                         fstrcpy(name, "system:backup");
769
770                 } else {
771
772                         if (!lookup_sid(&ace->trustee,
773                                         dom_name, name, &name_type)) {
774                                 DEBUG(1, ("AFSACL: Could not lookup SID %s on file %s\n",
775                                           sid_string_static(&ace->trustee), filename));
776                                 continue;
777                         }
778
779                         if ( (name_type == SID_NAME_USER) ||
780                              (name_type == SID_NAME_DOM_GRP) ||
781                              (name_type == SID_NAME_ALIAS) ) { 
782                                 fstring only_username;
783                                 fstrcpy(only_username, name);
784                                 fstr_sprintf(name, "%s%s%s",
785                                              dom_name, lp_winbind_separator(),
786                                              only_username);
787                                 strlower_m(name);
788                         }
789
790                         if (sidpts) {
791                                 /* Expect all users/groups in pts as SIDs */
792                                 sid_to_string(name, &ace->trustee);
793                         }
794                 }
795
796                 while ((p = strchr_m(name, ' ')) != NULL)
797                         *p = space_replacement;
798
799                 add_afs_ace(afs_acl, True, name,
800                             nt_to_afs_rights(filename, ace));
801         }
802
803         return True;
804 }
805
806 static BOOL afs_get_afs_acl(char *filename, struct afs_acl *acl)
807 {
808         struct afs_iob iob;
809
810         int ret;
811
812         char space[MAXSIZE];
813
814         DEBUG(5, ("afs_get_afs_acl: %s\n", filename));
815
816         iob.in_size = 0;
817         iob.out_size = MAXSIZE;
818         iob.in = iob.out = space;
819
820         ret = afs_syscall(AFSCALL_PIOCTL, filename, VIOCGETAL,
821                           (char *)&iob, 0);
822
823         if (ret) {
824                 DEBUG(1, ("got error from PIOCTL: %d\n", ret));
825                 return False;
826         }
827
828         if (!init_afs_acl(acl))
829                 return False;
830
831         if (!parse_afs_acl(acl, space)) {
832                 DEBUG(1, ("Could not parse AFS acl\n"));
833                 free_afs_acl(acl);
834                 return False;
835         }
836
837         return True;
838 }
839
840 static size_t afs_get_nt_acl(struct files_struct *fsp, uint32 security_info,
841                              struct security_descriptor_info **ppdesc)
842 {
843         struct afs_acl acl;
844         size_t sd_size;
845
846         DEBUG(5, ("afs_get_nt_acl: %s\n", fsp->fsp_name));
847
848         sidpts = lp_parm_bool(SNUM(fsp->conn), "afsacl", "sidpts", False);
849
850         if (!afs_get_afs_acl(fsp->fsp_name, &acl)) {
851                 return 0;
852         }
853
854         sd_size = afs_to_nt_acl(&acl, fsp, security_info, ppdesc);
855
856         free_afs_acl(&acl);
857
858         return sd_size;
859 }
860
861 /* For setting an AFS ACL we have to take care of the ACEs we could
862  * not properly map to SIDs. Merge all of them into the new ACL. */
863
864 static void merge_unknown_aces(struct afs_acl *src, struct afs_acl *dst)
865 {
866         struct afs_ace *ace;
867
868         for (ace = src->acelist; ace != NULL; ace = ace->next)
869         {
870                 struct afs_ace *copy;
871
872                 if (ace->type != SID_NAME_UNKNOWN) {
873                         DEBUG(10, ("Not merging known ACE for %s\n",
874                                    ace->name));
875                         continue;
876                 }
877
878                 DEBUG(10, ("Merging unknown ACE for %s\n", ace->name));
879
880                 copy = clone_afs_ace(dst->ctx, ace);
881
882                 if (copy == NULL) {
883                         DEBUG(0, ("Could not clone ACE for %s\n", ace->name));
884                         continue;
885                 }
886
887                 copy->next = dst->acelist;
888                 dst->acelist = copy;
889                 dst->num_aces += 1;
890         }
891 }
892
893 static BOOL afs_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
894                            uint32 security_info_sent,
895                            struct security_descriptor_info *psd)
896 {
897         struct afs_acl old_afs_acl, new_afs_acl;
898         struct afs_acl dir_acl, file_acl;
899         char acl_string[2049];
900         struct afs_iob iob;
901         int ret = -1;
902         pstring name;
903         const char *fileacls;
904
905         fileacls = lp_parm_const_string(SNUM(handle->conn), "afsacl", "fileacls",
906                                         "yes");
907
908         sidpts = lp_parm_bool(SNUM(handle->conn), "afsacl", "sidpts", False);
909
910         ZERO_STRUCT(old_afs_acl);
911         ZERO_STRUCT(new_afs_acl);
912         ZERO_STRUCT(dir_acl);
913         ZERO_STRUCT(file_acl);
914
915         pstr_sprintf(name, fsp->fsp_name);
916
917         if (!fsp->is_directory) {
918                 /* We need to get the name of the directory containing the
919                  * file, this is where the AFS acls live */
920                 char *p = strrchr(name, '/');
921                 if (p != NULL) {
922                         *p = '\0';
923                 } else {
924                         pstrcpy(name, ".");
925                 }
926         }
927
928         if (!afs_get_afs_acl(name, &old_afs_acl)) {
929                 DEBUG(3, ("Could not get old ACL of %s\n", fsp->fsp_name));
930                 goto done;
931         }
932
933         split_afs_acl(&old_afs_acl, &dir_acl, &file_acl);
934
935         if (fsp->is_directory) {
936
937                 if (!strequal(fileacls, "yes")) {
938                         /* Throw away file acls, we depend on the
939                          * inheritance ACEs that also give us file
940                          * permissions */
941                         free_afs_acl(&file_acl);
942                 }
943
944                 free_afs_acl(&dir_acl);
945                 if (!nt_to_afs_acl(fsp->fsp_name, security_info_sent, psd,
946                                    nt_to_afs_dir_rights, &dir_acl))
947                         goto done;
948         } else {
949                 if (strequal(fileacls, "no")) {
950                         ret = -1;
951                         goto done;
952                 }
953
954                 if (strequal(fileacls, "ignore")) {
955                         ret = 0;
956                         goto done;
957                 }
958
959                 free_afs_acl(&file_acl);
960                 if (!nt_to_afs_acl(fsp->fsp_name, security_info_sent, psd,
961                                    nt_to_afs_file_rights, &file_acl))
962                         goto done;
963         }
964
965         merge_afs_acls(&dir_acl, &file_acl, &new_afs_acl);
966
967         merge_unknown_aces(&old_afs_acl, &new_afs_acl);
968
969         unparse_afs_acl(&new_afs_acl, acl_string);
970
971         iob.in = acl_string;
972         iob.in_size = 1+strlen(iob.in);
973         iob.out = NULL;
974         iob.out_size = 0;
975
976         DEBUG(10, ("trying to set acl '%s' on file %s\n", iob.in, name));
977
978         ret = afs_syscall(AFSCALL_PIOCTL, name, VIOCSETAL, (char *)&iob, 0);
979
980         if (ret != 0) {
981                 DEBUG(10, ("VIOCSETAL returned %d\n", ret));
982         }
983
984  done:
985         free_afs_acl(&dir_acl);
986         free_afs_acl(&file_acl);
987         free_afs_acl(&old_afs_acl);
988         free_afs_acl(&new_afs_acl);
989
990         return (ret == 0);
991 }
992
993 static size_t afsacl_fget_nt_acl(struct vfs_handle_struct *handle,
994                                  struct files_struct *fsp,
995                                  int fd,  uint32 security_info,
996                                  struct security_descriptor_info **ppdesc)
997 {
998         return afs_get_nt_acl(fsp, security_info, ppdesc);
999 }
1000 static size_t afsacl_get_nt_acl(struct vfs_handle_struct *handle,
1001                                 struct files_struct *fsp,
1002                                 const char *name,  uint32 security_info,
1003                                 struct security_descriptor_info **ppdesc)
1004 {
1005         return afs_get_nt_acl(fsp, security_info, ppdesc);
1006 }
1007
1008 BOOL afsacl_fset_nt_acl(vfs_handle_struct *handle,
1009                          files_struct *fsp,
1010                          int fd, uint32 security_info_sent,
1011                          SEC_DESC *psd)
1012 {
1013         return afs_set_nt_acl(handle, fsp, security_info_sent, psd);
1014 }
1015
1016 BOOL afsacl_set_nt_acl(vfs_handle_struct *handle,
1017                        files_struct *fsp,
1018                        const char *name, uint32 security_info_sent,
1019                        SEC_DESC *psd)
1020 {
1021         return afs_set_nt_acl(handle, fsp, security_info_sent, psd);
1022 }
1023
1024 static int afsacl_connect(vfs_handle_struct *handle, 
1025                           connection_struct *conn, 
1026                           const char *service, 
1027                           const char *user)
1028 {
1029         char *spc;
1030
1031         spc = lp_parm_const_string(SNUM(handle->conn), "afsacl", "space", "%");
1032
1033         if (spc != NULL)
1034                 space_replacement = spc[0];
1035         
1036         return SMB_VFS_NEXT_CONNECT(handle, conn, service, user);
1037 }
1038
1039 /* VFS operations structure */
1040
1041 static vfs_op_tuple afsacl_ops[] = {    
1042         {SMB_VFS_OP(afsacl_connect), SMB_VFS_OP_CONNECT,
1043          SMB_VFS_LAYER_TRANSPARENT},
1044         {SMB_VFS_OP(afsacl_fget_nt_acl), SMB_VFS_OP_FGET_NT_ACL,
1045          SMB_VFS_LAYER_TRANSPARENT},
1046         {SMB_VFS_OP(afsacl_get_nt_acl), SMB_VFS_OP_GET_NT_ACL,
1047          SMB_VFS_LAYER_TRANSPARENT},
1048         {SMB_VFS_OP(afsacl_fset_nt_acl), SMB_VFS_OP_FSET_NT_ACL,
1049          SMB_VFS_LAYER_TRANSPARENT},
1050         {SMB_VFS_OP(afsacl_set_nt_acl), SMB_VFS_OP_SET_NT_ACL,
1051          SMB_VFS_LAYER_TRANSPARENT},
1052         {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
1053 };
1054
1055 NTSTATUS vfs_afsacl_init(void)
1056 {
1057         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "afsacl",
1058                                 afsacl_ops);
1059 }