s3:modules/vfs_afsacl remove some unnecessary whitespace
[kamenim/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 #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, 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
307         *acl_str = 0;
308
309         struct afs_ace *ace = acl->acelist;
310
311         while (ace != NULL) {
312                 if (ace->positive)
313                         positives++;
314                 else
315                         negatives++;
316                 ace = ace->next;
317         }
318
319         fstr_sprintf(line, "%d\n", positives);
320         strlcat(acl_str, line, MAXSIZE);
321
322         fstr_sprintf(line, "%d\n", negatives);
323         strlcat(acl_str, line, MAXSIZE);
324
325         ace = acl->acelist;
326
327         while (ace != NULL) {
328                 fstr_sprintf(line, "%s\t%d\n", ace->name, ace->rights);
329                 strlcat(acl_str, line, MAXSIZE);
330                 ace = ace->next;
331         }
332         return True;
333 }
334
335 static uint32 afs_to_nt_file_rights(uint32 rights)
336 {
337         uint32 result = 0;
338
339         if (rights & PRSFS_READ)
340                 result |= FILE_READ_DATA | FILE_READ_EA | 
341                         FILE_EXECUTE | FILE_READ_ATTRIBUTES |
342                         READ_CONTROL_ACCESS | SYNCHRONIZE_ACCESS;
343
344         if (rights & PRSFS_WRITE)
345                 result |= FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES |
346                         FILE_WRITE_EA | FILE_APPEND_DATA;
347
348         if (rights & PRSFS_LOCK)
349                 result |= WRITE_OWNER_ACCESS;
350
351         if (rights & PRSFS_DELETE)
352                 result |= DELETE_ACCESS;
353
354         return result;
355 }
356
357 static void afs_to_nt_dir_rights(uint32 afs_rights, uint32 *nt_rights,
358                                  uint8 *flag)
359 {
360         *nt_rights = 0;
361         *flag = SEC_ACE_FLAG_OBJECT_INHERIT |
362                 SEC_ACE_FLAG_CONTAINER_INHERIT;
363
364         if (afs_rights & PRSFS_INSERT)
365                 *nt_rights |= FILE_ADD_FILE | FILE_ADD_SUBDIRECTORY;
366
367         if (afs_rights & PRSFS_LOOKUP)
368                 *nt_rights |= FILE_READ_DATA | FILE_READ_EA | 
369                         FILE_EXECUTE | FILE_READ_ATTRIBUTES |
370                         READ_CONTROL_ACCESS | SYNCHRONIZE_ACCESS;
371
372         if (afs_rights & PRSFS_WRITE)
373                 *nt_rights |= FILE_WRITE_ATTRIBUTES | FILE_WRITE_DATA |
374                         FILE_APPEND_DATA | FILE_WRITE_EA;
375
376         if ((afs_rights & (PRSFS_INSERT|PRSFS_LOOKUP|PRSFS_DELETE)) ==
377             (PRSFS_INSERT|PRSFS_LOOKUP|PRSFS_DELETE))
378                 *nt_rights |= FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA |
379                         GENERIC_WRITE_ACCESS;
380
381         if (afs_rights & PRSFS_DELETE)
382                 *nt_rights |= DELETE_ACCESS;
383
384         if (afs_rights & PRSFS_ADMINISTER)
385                 *nt_rights |= FILE_DELETE_CHILD | WRITE_DAC_ACCESS |
386                         WRITE_OWNER_ACCESS;
387
388         if ( (afs_rights & PRSFS_LOOKUP) ==
389              (afs_rights & (PRSFS_LOOKUP|PRSFS_READ)) ) {
390                 /* Only lookup right */
391                 *flag = SEC_ACE_FLAG_CONTAINER_INHERIT;
392         }
393
394         return;
395 }
396
397 #define AFS_FILE_RIGHTS (PRSFS_READ|PRSFS_WRITE|PRSFS_LOCK)
398 #define AFS_DIR_RIGHTS  (PRSFS_INSERT|PRSFS_LOOKUP|PRSFS_DELETE|PRSFS_ADMINISTER)
399
400 static void split_afs_acl(struct afs_acl *acl,
401                           struct afs_acl *dir_acl,
402                           struct afs_acl *file_acl)
403 {
404         struct afs_ace *ace;
405
406         init_afs_acl(dir_acl);
407         init_afs_acl(file_acl);
408
409         for (ace = acl->acelist; ace != NULL; ace = ace->next) {
410                 if (ace->rights & AFS_FILE_RIGHTS) {
411                         add_afs_ace(file_acl, ace->positive, ace->name,
412                                     ace->rights & AFS_FILE_RIGHTS);
413                 }
414
415                 if (ace->rights & AFS_DIR_RIGHTS) {
416                         add_afs_ace(dir_acl, ace->positive, ace->name,
417                                     ace->rights & AFS_DIR_RIGHTS);
418                 }
419         }
420         return;
421 }
422
423 static bool same_principal(struct afs_ace *x, struct afs_ace *y)
424 {
425         return ( (x->positive == y->positive) &&
426                  (dom_sid_compare(&x->sid, &y->sid) == 0) );
427 }
428
429 static void merge_afs_acls(struct afs_acl *dir_acl,
430                            struct afs_acl *file_acl,
431                            struct afs_acl *target)
432 {
433         struct afs_ace *ace;
434
435         init_afs_acl(target);
436
437         for (ace = dir_acl->acelist; ace != NULL; ace = ace->next) {
438                 struct afs_ace *file_ace;
439                 bool found = False;
440
441                 for (file_ace = file_acl->acelist;
442                      file_ace != NULL;
443                      file_ace = file_ace->next) {
444                         if (!same_principal(ace, file_ace))
445                                 continue;
446
447                         add_afs_ace(target, ace->positive, ace->name,
448                                     ace->rights | file_ace->rights);
449                         found = True;
450                         break;
451                 }
452                 if (!found)
453                         add_afs_ace(target, ace->positive, ace->name,
454                                     ace->rights);
455         }
456
457         for (ace = file_acl->acelist; ace != NULL; ace = ace->next) {
458                 struct afs_ace *dir_ace;
459                 bool already_seen = False;
460
461                 for (dir_ace = dir_acl->acelist;
462                      dir_ace != NULL;
463                      dir_ace = dir_ace->next) {
464                         if (!same_principal(ace, dir_ace))
465                                 continue;
466                         already_seen = True;
467                         break;
468                 }
469                 if (!already_seen)
470                         add_afs_ace(target, ace->positive, ace->name,
471                                     ace->rights);
472         }
473 }
474
475 #define PERMS_READ   0x001200a9
476 #define PERMS_CHANGE 0x001301bf
477 #define PERMS_FULL   0x001f01ff
478
479 static struct static_dir_ace_mapping {
480         uint8 type;
481         uint8 flags;
482         uint32 mask;
483         uint32 afs_rights;
484 } ace_mappings[] = {
485
486         /* Full control */
487         { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT,
488           PERMS_FULL, 127 /* rlidwka */ },
489
490         /* Change (write) */
491         { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT,
492           PERMS_CHANGE, 63 /* rlidwk */ },
493
494         /* Read (including list folder content) */
495         { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT,
496           PERMS_READ, 9 /* rl */ },
497
498         /* Read without list folder content -- same as "l" */
499         { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT,
500           0x00120089, 8 /* l */ },
501
502         /* some stupid workaround for preventing fallbacks */   
503         { 0, 0x3, 0x0012019F, 9 /* rl */ },
504         { 0, 0x13, PERMS_FULL, 127 /* full */ },
505         
506         /* read, delete and execute access plus synchronize */
507         { 0, 0x3, 0x001300A9, 9 /* should be rdl, set to rl */},
508         /* classical read list */
509         { 0, 0x13, 0x001200A9, 9 /* rl */},
510         /* almost full control, no delete */
511         { 0, 0x13, PERMS_CHANGE, 63 /* rwidlk */},
512
513         /* List folder */
514         { 0, SEC_ACE_FLAG_CONTAINER_INHERIT,
515           PERMS_READ, 8 /* l */ },
516
517         /* FULL without inheritance -- in all cases here we also get
518            the corresponding INHERIT_ONLY ACE in the same ACL */
519         { 0, 0, PERMS_FULL, 127 /* rlidwka */ },
520
521         /* FULL inherit only -- counterpart to previous one */
522         { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_INHERIT_ONLY,
523           PERMS_FULL | SEC_GENERIC_WRITE, 127 /* rlidwka */ },
524
525         /* CHANGE without inheritance -- in all cases here we also get
526            the corresponding INHERIT_ONLY ACE in the same ACL */
527         { 0, 0, PERMS_CHANGE, 63 /* rlidwk */ },
528
529         /* CHANGE inherit only -- counterpart to previous one */
530         { 0, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_INHERIT_ONLY,
531           PERMS_CHANGE | SEC_GENERIC_WRITE, 63 /* rlidwk */ },
532
533         /* End marker, hopefully there's no afs right 9999 :-) */
534         { 0, 0, 0, 9999 }
535 };
536
537 static uint32 nt_to_afs_dir_rights(const char *filename, const struct security_ace *ace)
538 {
539         uint32 result = 0;
540         uint32 rights = ace->access_mask;
541         uint8 flags = ace->flags;
542
543         struct static_dir_ace_mapping *m;
544
545         for (m = &ace_mappings[0]; m->afs_rights != 9999; m++) {
546                 if ( (ace->type == m->type) &&
547                      (ace->flags == m->flags) &&
548                      (ace->access_mask == m->mask) )
549                         return m->afs_rights;
550         }
551
552         DEBUG(1, ("AFSACL FALLBACK: 0x%X 0x%X 0x%X %s %X\n",
553                   ace->type, ace->flags, ace->access_mask, filename, rights));
554
555         if (rights & (GENERIC_ALL_ACCESS|WRITE_DAC_ACCESS)) {
556                 result |= PRSFS_READ | PRSFS_WRITE | PRSFS_INSERT |
557                         PRSFS_LOOKUP | PRSFS_DELETE | PRSFS_LOCK |
558                         PRSFS_ADMINISTER;
559         }
560
561         if (rights & (GENERIC_READ_ACCESS|FILE_READ_DATA)) {
562                 result |= PRSFS_LOOKUP;
563                 if (flags & SEC_ACE_FLAG_OBJECT_INHERIT) {
564                         result |= PRSFS_READ;
565                 }
566         }
567
568         if (rights & (GENERIC_WRITE_ACCESS|FILE_WRITE_DATA)) {
569                 result |= PRSFS_INSERT | PRSFS_DELETE;
570                 if (flags & SEC_ACE_FLAG_OBJECT_INHERIT) {
571                         result |= PRSFS_WRITE | PRSFS_LOCK;
572                 }
573         }
574
575         return result;
576 }
577
578 static uint32 nt_to_afs_file_rights(const char *filename, const struct security_ace *ace)
579 {
580         uint32 result = 0;
581         uint32 rights = ace->access_mask;
582
583         if (rights & (GENERIC_READ_ACCESS|FILE_READ_DATA)) {
584                 result |= PRSFS_READ;
585         }
586
587         if (rights & (GENERIC_WRITE_ACCESS|FILE_WRITE_DATA)) {
588                 result |= PRSFS_WRITE | PRSFS_LOCK;
589         }
590
591         return result;
592 }
593
594 static size_t afs_to_nt_acl_common(struct afs_acl *afs_acl,
595                                    SMB_STRUCT_STAT *psbuf,
596                                    uint32 security_info,
597                                    struct security_descriptor **ppdesc)
598 {
599         struct security_ace *nt_ace_list;
600         struct dom_sid owner_sid, group_sid;
601         struct security_acl *psa = NULL;
602         int good_aces;
603         size_t sd_size;
604         TALLOC_CTX *mem_ctx = talloc_tos();
605
606         struct afs_ace *afs_ace;
607
608         uid_to_sid(&owner_sid, psbuf->st_ex_uid);
609         gid_to_sid(&group_sid, psbuf->st_ex_gid);
610
611         if (afs_acl->num_aces) {
612                 nt_ace_list = talloc_array(mem_ctx, struct security_ace, afs_acl->num_aces);
613
614                 if (nt_ace_list == NULL)
615                         return 0;
616         } else {
617                 nt_ace_list = NULL;
618         }
619
620         afs_ace = afs_acl->acelist;
621         good_aces = 0;
622
623         while (afs_ace != NULL) {
624                 uint32_t nt_rights;
625                 uint8 flag = SEC_ACE_FLAG_OBJECT_INHERIT |
626                         SEC_ACE_FLAG_CONTAINER_INHERIT;
627
628                 if (afs_ace->type == SID_NAME_UNKNOWN) {
629                         DEBUG(10, ("Ignoring unknown name %s\n",
630                                    afs_ace->name));
631                         afs_ace = afs_ace->next;
632                         continue;
633                 }
634
635                 if (S_ISDIR(psbuf->st_ex_mode))
636                         afs_to_nt_dir_rights(afs_ace->rights, &nt_rights,
637                                              &flag);
638                 else
639                         nt_rights = afs_to_nt_file_rights(afs_ace->rights);
640
641                 init_sec_ace(&nt_ace_list[good_aces++], &(afs_ace->sid),
642                              SEC_ACE_TYPE_ACCESS_ALLOWED, nt_rights, flag);
643                 afs_ace = afs_ace->next;
644         }
645
646         psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION,
647                            good_aces, nt_ace_list);
648         if (psa == NULL)
649                 return 0;
650
651         *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
652                                 SEC_DESC_SELF_RELATIVE,
653                                 (security_info & SECINFO_OWNER)
654                                 ? &owner_sid : NULL,
655                                 (security_info & SECINFO_GROUP)
656                                 ? &group_sid : NULL,
657                                 NULL, psa, &sd_size);
658
659         return sd_size;
660 }
661
662 static size_t afs_to_nt_acl(struct afs_acl *afs_acl,
663                             struct connection_struct *conn,
664                             struct smb_filename *smb_fname,
665                             uint32 security_info,
666                             struct security_descriptor **ppdesc)
667 {
668         int ret;
669
670         /* Get the stat struct for the owner info. */
671         if (lp_posix_pathnames()) {
672                 ret = SMB_VFS_LSTAT(conn, smb_fname);
673         } else {
674                 ret = SMB_VFS_STAT(conn, smb_fname);
675         }
676         if (ret == -1) {
677                 return 0;
678         }
679
680         return afs_to_nt_acl_common(afs_acl, &smb_fname->st, security_info,
681                                     ppdesc);
682 }
683
684 static size_t afs_fto_nt_acl(struct afs_acl *afs_acl,
685                              struct files_struct *fsp,
686                              uint32 security_info,
687                              struct security_descriptor **ppdesc)
688 {
689         SMB_STRUCT_STAT sbuf;
690
691         if (fsp->fh->fd == -1) {
692                 /* Get the stat struct for the owner info. */
693                 return afs_to_nt_acl(afs_acl, fsp->conn, fsp->fsp_name,
694                                      security_info, ppdesc);
695         }
696
697         if(SMB_VFS_FSTAT(fsp, &sbuf) != 0) {
698                 return 0;
699         }
700
701         return afs_to_nt_acl_common(afs_acl, &sbuf, security_info, ppdesc);
702 }
703
704 static bool mappable_sid(const struct dom_sid *sid)
705 {
706         struct dom_sid domain_sid;
707         
708         if (dom_sid_compare(sid, &global_sid_Builtin_Administrators) == 0)
709                 return True;
710
711         if (dom_sid_compare(sid, &global_sid_World) == 0)
712                 return True;
713
714         if (dom_sid_compare(sid, &global_sid_Authenticated_Users) == 0)
715                 return True;
716
717         if (dom_sid_compare(sid, &global_sid_Builtin_Backup_Operators) == 0)
718                 return True;
719
720         string_to_sid(&domain_sid, "S-1-5-21");
721
722         if (sid_compare_domain(sid, &domain_sid) == 0)
723                 return True;
724
725         return False;
726 }
727
728 static bool nt_to_afs_acl(const char *filename,
729                           uint32 security_info_sent,
730                           const struct security_descriptor *psd,
731                           uint32 (*nt_to_afs_rights)(const char *filename,
732                                                      const struct security_ace *ace),
733                           struct afs_acl *afs_acl)
734 {
735         const struct security_acl *dacl;
736         int i;
737
738         /* Currently we *only* look at the dacl */
739
740         if (((security_info_sent & SECINFO_DACL) == 0) ||
741             (psd->dacl == NULL))
742                 return True;
743
744         if (!init_afs_acl(afs_acl))
745                 return False;
746
747         dacl = psd->dacl;
748
749         for (i = 0; i < dacl->num_aces; i++) {
750                 const struct security_ace *ace = &(dacl->aces[i]);
751                 const char *dom_name, *name;
752                 enum lsa_SidType name_type;
753                 char *p;
754
755                 if (ace->type != SEC_ACE_TYPE_ACCESS_ALLOWED) {
756                         /* First cut: Only positive ACEs */
757                         return False;
758                 }
759
760                 if (!mappable_sid(&ace->trustee)) {
761                         DEBUG(10, ("Ignoring unmappable SID %s\n",
762                                    sid_string_dbg(&ace->trustee)));
763                         continue;
764                 }
765
766                 if (dom_sid_compare(&ace->trustee,
767                                 &global_sid_Builtin_Administrators) == 0) {
768
769                         name = "system:administrators";
770
771                 } else if (dom_sid_compare(&ace->trustee,
772                                        &global_sid_World) == 0) {
773
774                         name = "system:anyuser";
775
776                 } else if (dom_sid_compare(&ace->trustee,
777                                        &global_sid_Authenticated_Users) == 0) {
778
779                         name = "system:authuser";
780
781                 } else if (dom_sid_compare(&ace->trustee,
782                                        &global_sid_Builtin_Backup_Operators)
783                            == 0) {
784
785                         name = "system:backup";
786
787                 } else {
788
789                         if (!lookup_sid(talloc_tos(), &ace->trustee,
790                                         &dom_name, &name, &name_type)) {
791                                 DEBUG(1, ("AFSACL: Could not lookup SID %s on file %s\n",
792                                           sid_string_dbg(&ace->trustee),
793                                           filename));
794                                 continue;
795                         }
796
797                         if ( (name_type == SID_NAME_USER) ||
798                              (name_type == SID_NAME_DOM_GRP) ||
799                              (name_type == SID_NAME_ALIAS) ) {
800                                 char *tmp;
801                                 tmp = talloc_asprintf(talloc_tos(), "%s%s%s",
802                                                        dom_name, lp_winbind_separator(),
803                                                        name);
804                                 if (tmp == NULL) {
805                                         return False;
806                                 }
807                                 strlower_m(tmp);
808                                 name = tmp;
809                         }
810
811                         if (sidpts) {
812                                 /* Expect all users/groups in pts as SIDs */
813                                 name = talloc_strdup(
814                                         talloc_tos(),
815                                         sid_string_tos(&ace->trustee));
816                                 if (name == NULL) {
817                                         return False;
818                                 }
819                         }
820                 }
821
822                 while ((p = strchr_m(name, ' ')) != NULL)
823                         *p = space_replacement;
824
825                 add_afs_ace(afs_acl, True, name,
826                             nt_to_afs_rights(filename, ace));
827         }
828
829         return True;
830 }
831
832 static bool afs_get_afs_acl(char *filename, struct afs_acl *acl)
833 {
834         struct afs_iob iob;
835
836         int ret;
837
838         char space[MAXSIZE];
839
840         DEBUG(5, ("afs_get_afs_acl: %s\n", filename));
841
842         iob.in_size = 0;
843         iob.out_size = MAXSIZE;
844         iob.in = iob.out = space;
845
846         ret = afs_syscall(AFSCALL_PIOCTL, filename, VIOCGETAL,
847                           (char *)&iob, 0);
848
849         if (ret) {
850                 DEBUG(1, ("got error from PIOCTL: %d\n", ret));
851                 return False;
852         }
853
854         if (!init_afs_acl(acl))
855                 return False;
856
857         if (!parse_afs_acl(acl, space)) {
858                 DEBUG(1, ("Could not parse AFS acl\n"));
859                 free_afs_acl(acl);
860                 return False;
861         }
862
863         return True;
864 }
865
866 /* For setting an AFS ACL we have to take care of the ACEs we could
867  * not properly map to SIDs. Merge all of them into the new ACL. */
868
869 static void merge_unknown_aces(struct afs_acl *src, struct afs_acl *dst)
870 {
871         struct afs_ace *ace;
872
873         for (ace = src->acelist; ace != NULL; ace = ace->next)
874         {
875                 struct afs_ace *copy;
876
877                 if (ace->type != SID_NAME_UNKNOWN) {
878                         DEBUG(10, ("Not merging known ACE for %s\n",
879                                    ace->name));
880                         continue;
881                 }
882
883                 DEBUG(10, ("Merging unknown ACE for %s\n", ace->name));
884
885                 copy = clone_afs_ace(dst->ctx, ace);
886
887                 if (copy == NULL) {
888                         DEBUG(0, ("Could not clone ACE for %s\n", ace->name));
889                         continue;
890                 }
891
892                 copy->next = dst->acelist;
893                 dst->acelist = copy;
894                 dst->num_aces += 1;
895         }
896 }
897
898 static NTSTATUS afs_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
899                            uint32 security_info_sent,
900                            const struct security_descriptor *psd)
901 {
902         struct afs_acl old_afs_acl, new_afs_acl;
903         struct afs_acl dir_acl, file_acl;
904         char acl_string[MAXSIZE];
905         struct afs_iob iob;
906         int ret = -1;
907         char *name = NULL;
908         const char *fileacls;
909
910         fileacls = lp_parm_const_string(SNUM(handle->conn), "afsacl", "fileacls",
911                                         "yes");
912
913         sidpts = lp_parm_bool(SNUM(handle->conn), "afsacl", "sidpts", False);
914
915         ZERO_STRUCT(old_afs_acl);
916         ZERO_STRUCT(new_afs_acl);
917         ZERO_STRUCT(dir_acl);
918         ZERO_STRUCT(file_acl);
919
920         name = talloc_strdup(talloc_tos(), fsp->fsp_name->base_name);
921         if (!name) {
922                 return NT_STATUS_NO_MEMORY;
923         }
924
925         if (!fsp->is_directory) {
926                 /* We need to get the name of the directory containing the
927                  * file, this is where the AFS acls live */
928                 char *p = strrchr(name, '/');
929                 if (p != NULL) {
930                         *p = '\0';
931                 } else {
932                         name = talloc_strdup(talloc_tos(), ".");
933                         if (!name) {
934                                 return NT_STATUS_NO_MEMORY;
935                         }
936                 }
937         }
938
939         if (!afs_get_afs_acl(name, &old_afs_acl)) {
940                 DEBUG(3, ("Could not get old ACL of %s\n", fsp_str_dbg(fsp)));
941                 goto done;
942         }
943
944         split_afs_acl(&old_afs_acl, &dir_acl, &file_acl);
945
946         if (fsp->is_directory) {
947
948                 if (!strequal(fileacls, "yes")) {
949                         /* Throw away file acls, we depend on the
950                          * inheritance ACEs that also give us file
951                          * permissions */
952                         free_afs_acl(&file_acl);
953                 }
954
955                 free_afs_acl(&dir_acl);
956                 if (!nt_to_afs_acl(fsp->fsp_name->base_name,
957                                    security_info_sent, psd,
958                                    nt_to_afs_dir_rights, &dir_acl))
959                         goto done;
960         } else {
961                 if (strequal(fileacls, "no")) {
962                         ret = -1;
963                         goto done;
964                 }
965
966                 if (strequal(fileacls, "ignore")) {
967                         ret = 0;
968                         goto done;
969                 }
970
971                 free_afs_acl(&file_acl);
972                 if (!nt_to_afs_acl(fsp->fsp_name->base_name,
973                                    security_info_sent, psd,
974                                    nt_to_afs_file_rights, &file_acl))
975                         goto done;
976         }
977
978         merge_afs_acls(&dir_acl, &file_acl, &new_afs_acl);
979
980         merge_unknown_aces(&old_afs_acl, &new_afs_acl);
981
982         unparse_afs_acl(&new_afs_acl, acl_string);
983
984         iob.in = acl_string;
985         iob.in_size = 1+strlen(iob.in);
986         iob.out = NULL;
987         iob.out_size = 0;
988
989         DEBUG(10, ("trying to set acl '%s' on file %s\n", iob.in, name));
990
991         ret = afs_syscall(AFSCALL_PIOCTL, name, VIOCSETAL, (char *)&iob, 0);
992
993         if (ret != 0) {
994                 DEBUG(10, ("VIOCSETAL returned %d\n", ret));
995         }
996
997  done:
998         free_afs_acl(&dir_acl);
999         free_afs_acl(&file_acl);
1000         free_afs_acl(&old_afs_acl);
1001         free_afs_acl(&new_afs_acl);
1002
1003         return (ret == 0) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1004 }
1005
1006 static NTSTATUS afsacl_fget_nt_acl(struct vfs_handle_struct *handle,
1007                                    struct files_struct *fsp,
1008                                    uint32 security_info,
1009                                    struct security_descriptor **ppdesc)
1010 {
1011         struct afs_acl acl;
1012         size_t sd_size;
1013
1014         DEBUG(5, ("afsacl_fget_nt_acl: %s\n", fsp_str_dbg(fsp)));
1015
1016         sidpts = lp_parm_bool(SNUM(fsp->conn), "afsacl", "sidpts", False);
1017
1018         if (!afs_get_afs_acl(fsp->fsp_name->base_name, &acl)) {
1019                 return NT_STATUS_ACCESS_DENIED;
1020         }
1021
1022         sd_size = afs_fto_nt_acl(&acl, fsp, security_info, ppdesc);
1023
1024         free_afs_acl(&acl);
1025
1026         return (sd_size != 0) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1027 }
1028
1029 static NTSTATUS afsacl_get_nt_acl(struct vfs_handle_struct *handle,
1030                                   const char *name,  uint32 security_info,
1031                                   struct security_descriptor **ppdesc)
1032 {
1033         struct afs_acl acl;
1034         size_t sd_size;
1035         struct smb_filename *smb_fname = NULL;
1036         NTSTATUS status;
1037
1038         DEBUG(5, ("afsacl_get_nt_acl: %s\n", name));
1039
1040         sidpts = lp_parm_bool(SNUM(handle->conn), "afsacl", "sidpts", False);
1041
1042         if (!afs_get_afs_acl(name, &acl)) {
1043                 return NT_STATUS_ACCESS_DENIED;
1044         }
1045
1046         status = create_synthetic_smb_fname(talloc_tos(), name, NULL, NULL,
1047                                             &smb_fname);
1048         if (!NT_STATUS_IS_OK(status)) {
1049                 free_afs_acl(&acl);
1050                 return status;
1051         }
1052
1053         sd_size = afs_to_nt_acl(&acl, handle->conn, smb_fname, security_info,
1054                                 ppdesc);
1055         TALLOC_FREE(smb_fname);
1056
1057         free_afs_acl(&acl);
1058
1059         return (sd_size != 0) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
1060 }
1061
1062 NTSTATUS afsacl_fset_nt_acl(vfs_handle_struct *handle,
1063                          files_struct *fsp,
1064                          uint32 security_info_sent,
1065                          const struct security_descriptor *psd)
1066 {
1067         return afs_set_nt_acl(handle, fsp, security_info_sent, psd);
1068 }
1069
1070 static int afsacl_connect(vfs_handle_struct *handle, 
1071                           const char *service, 
1072                           const char *user)
1073 {
1074         const char *spc;
1075         int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
1076
1077         if (ret < 0) {
1078                 return ret;
1079         }
1080
1081         spc = lp_parm_const_string(SNUM(handle->conn), "afsacl", "space", "%");
1082
1083         if (spc != NULL)
1084                 space_replacement = spc[0];
1085
1086         return 0;
1087 }
1088
1089 static struct vfs_fn_pointers vfs_afsacl_fns = {
1090         .connect_fn = afsacl_connect,
1091         .fget_nt_acl = afsacl_fget_nt_acl,
1092         .get_nt_acl = afsacl_get_nt_acl,
1093         .fset_nt_acl = afsacl_fset_nt_acl
1094 };
1095
1096 NTSTATUS vfs_afsacl_init(void);
1097 NTSTATUS vfs_afsacl_init(void)
1098 {
1099         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "afsacl",
1100                                 &vfs_afsacl_fns);
1101 }