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