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