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