source: branches/samba-3.2.x/source/modules/vfs_afsacl.c

Last change on this file was 133, checked in by Paul Smedley, 17 years ago

Update trunk to 3.2.0pre3

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