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