nsswitch: Fix getting data out of pam_get_data()
[Samba.git] / source3 / smbd / posix_acls.c
blob15c7f14062e21e239daa6576ea189d4ad24ada0b
1 /*
2 Unix SMB/CIFS implementation.
3 SMB NT Security Descriptor / Unix permission conversion.
4 Copyright (C) Jeremy Allison 1994-2009.
5 Copyright (C) Andreas Gruenbacher 2002.
6 Copyright (C) Simo Sorce <idra@samba.org> 2009.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "smbd/smbd.h"
24 #include "system/filesys.h"
25 #include "../libcli/security/security.h"
26 #include "trans2.h"
27 #include "passdb/lookup_sid.h"
28 #include "auth.h"
29 #include "../librpc/gen_ndr/idmap.h"
30 #include "../librpc/gen_ndr/ndr_smb_acl.h"
31 #include "lib/param/loadparm.h"
33 extern const struct generic_mapping file_generic_mapping;
35 #undef DBGC_CLASS
36 #define DBGC_CLASS DBGC_ACLS
38 /****************************************************************************
39 Data structures representing the internal ACE format.
40 ****************************************************************************/
42 enum ace_owner {UID_ACE, GID_ACE, WORLD_ACE};
43 enum ace_attribute {ALLOW_ACE, DENY_ACE}; /* Used for incoming NT ACLS. */
45 typedef struct canon_ace {
46 struct canon_ace *next, *prev;
47 SMB_ACL_TAG_T type;
48 mode_t perms; /* Only use S_I(R|W|X)USR mode bits here. */
49 struct dom_sid trustee;
50 enum ace_owner owner_type;
51 enum ace_attribute attr;
52 struct unixid unix_ug;
53 uint8_t ace_flags; /* From windows ACE entry. */
54 } canon_ace;
56 #define ALL_ACE_PERMS (S_IRUSR|S_IWUSR|S_IXUSR)
59 * EA format of user.SAMBA_PAI (Samba_Posix_Acl_Interitance)
60 * attribute on disk - version 1.
61 * All values are little endian.
63 * | 1 | 1 | 2 | 2 | ....
64 * +------+------+-------------+---------------------+-------------+--------------------+
65 * | vers | flag | num_entries | num_default_entries | ..entries.. | default_entries... |
66 * +------+------+-------------+---------------------+-------------+--------------------+
68 * Entry format is :
70 * | 1 | 4 |
71 * +------+-------------------+
72 * | value| uid/gid or world |
73 * | type | value |
74 * +------+-------------------+
76 * Version 2 format. Stores extra Windows metadata about an ACL.
78 * | 1 | 2 | 2 | 2 | ....
79 * +------+----------+-------------+---------------------+-------------+--------------------+
80 * | vers | ace | num_entries | num_default_entries | ..entries.. | default_entries... |
81 * | 2 | type | | | | |
82 * +------+----------+-------------+---------------------+-------------+--------------------+
84 * Entry format is :
86 * | 1 | 1 | 4 |
87 * +------+------+-------------------+
88 * | ace | value| uid/gid or world |
89 * | flag | type | value |
90 * +------+-------------------+------+
94 #define PAI_VERSION_OFFSET 0
96 #define PAI_V1_FLAG_OFFSET 1
97 #define PAI_V1_NUM_ENTRIES_OFFSET 2
98 #define PAI_V1_NUM_DEFAULT_ENTRIES_OFFSET 4
99 #define PAI_V1_ENTRIES_BASE 6
100 #define PAI_V1_ACL_FLAG_PROTECTED 0x1
101 #define PAI_V1_ENTRY_LENGTH 5
103 #define PAI_V1_VERSION 1
105 #define PAI_V2_TYPE_OFFSET 1
106 #define PAI_V2_NUM_ENTRIES_OFFSET 3
107 #define PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET 5
108 #define PAI_V2_ENTRIES_BASE 7
109 #define PAI_V2_ENTRY_LENGTH 6
111 #define PAI_V2_VERSION 2
114 * In memory format of user.SAMBA_PAI attribute.
117 struct pai_entry {
118 struct pai_entry *next, *prev;
119 uint8_t ace_flags;
120 enum ace_owner owner_type;
121 struct unixid unix_ug;
124 struct pai_val {
125 uint16_t sd_type;
126 unsigned int num_entries;
127 struct pai_entry *entry_list;
128 unsigned int num_def_entries;
129 struct pai_entry *def_entry_list;
132 /************************************************************************
133 Return a uint32_t of the pai_entry principal.
134 ************************************************************************/
136 static uint32_t get_pai_entry_val(struct pai_entry *paie)
138 switch (paie->owner_type) {
139 case UID_ACE:
140 DEBUG(10,("get_pai_entry_val: uid = %u\n", (unsigned int)paie->unix_ug.id ));
141 return (uint32_t)paie->unix_ug.id;
142 case GID_ACE:
143 DEBUG(10,("get_pai_entry_val: gid = %u\n", (unsigned int)paie->unix_ug.id ));
144 return (uint32_t)paie->unix_ug.id;
145 case WORLD_ACE:
146 default:
147 DEBUG(10,("get_pai_entry_val: world ace\n"));
148 return (uint32_t)-1;
152 /************************************************************************
153 Return a uint32_t of the entry principal.
154 ************************************************************************/
156 static uint32_t get_entry_val(canon_ace *ace_entry)
158 switch (ace_entry->owner_type) {
159 case UID_ACE:
160 DEBUG(10,("get_entry_val: uid = %u\n", (unsigned int)ace_entry->unix_ug.id ));
161 return (uint32_t)ace_entry->unix_ug.id;
162 case GID_ACE:
163 DEBUG(10,("get_entry_val: gid = %u\n", (unsigned int)ace_entry->unix_ug.id ));
164 return (uint32_t)ace_entry->unix_ug.id;
165 case WORLD_ACE:
166 default:
167 DEBUG(10,("get_entry_val: world ace\n"));
168 return (uint32_t)-1;
172 /************************************************************************
173 Create the on-disk format (always v2 now). Caller must free.
174 ************************************************************************/
176 static char *create_pai_buf_v2(canon_ace *file_ace_list,
177 canon_ace *dir_ace_list,
178 uint16_t sd_type,
179 size_t *store_size)
181 char *pai_buf = NULL;
182 canon_ace *ace_list = NULL;
183 char *entry_offset = NULL;
184 unsigned int num_entries = 0;
185 unsigned int num_def_entries = 0;
186 unsigned int i;
188 for (ace_list = file_ace_list; ace_list; ace_list = ace_list->next) {
189 num_entries++;
192 for (ace_list = dir_ace_list; ace_list; ace_list = ace_list->next) {
193 num_def_entries++;
196 DEBUG(10,("create_pai_buf_v2: num_entries = %u, num_def_entries = %u\n", num_entries, num_def_entries ));
198 *store_size = PAI_V2_ENTRIES_BASE +
199 ((num_entries + num_def_entries)*PAI_V2_ENTRY_LENGTH);
201 pai_buf = talloc_array(talloc_tos(), char, *store_size);
202 if (!pai_buf) {
203 return NULL;
206 /* Set up the header. */
207 memset(pai_buf, '\0', PAI_V2_ENTRIES_BASE);
208 SCVAL(pai_buf,PAI_VERSION_OFFSET,PAI_V2_VERSION);
209 SSVAL(pai_buf,PAI_V2_TYPE_OFFSET, sd_type);
210 SSVAL(pai_buf,PAI_V2_NUM_ENTRIES_OFFSET,num_entries);
211 SSVAL(pai_buf,PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET,num_def_entries);
213 DEBUG(10,("create_pai_buf_v2: sd_type = 0x%x\n",
214 (unsigned int)sd_type ));
216 entry_offset = pai_buf + PAI_V2_ENTRIES_BASE;
218 i = 0;
219 for (ace_list = file_ace_list; ace_list; ace_list = ace_list->next) {
220 uint8_t type_val = (uint8_t)ace_list->owner_type;
221 uint32_t entry_val = get_entry_val(ace_list);
223 SCVAL(entry_offset,0,ace_list->ace_flags);
224 SCVAL(entry_offset,1,type_val);
225 SIVAL(entry_offset,2,entry_val);
226 DEBUG(10,("create_pai_buf_v2: entry %u [0x%x] [0x%x] [0x%x]\n",
228 (unsigned int)ace_list->ace_flags,
229 (unsigned int)type_val,
230 (unsigned int)entry_val ));
231 i++;
232 entry_offset += PAI_V2_ENTRY_LENGTH;
235 for (ace_list = dir_ace_list; ace_list; ace_list = ace_list->next) {
236 uint8_t type_val = (uint8_t)ace_list->owner_type;
237 uint32_t entry_val = get_entry_val(ace_list);
239 SCVAL(entry_offset,0,ace_list->ace_flags);
240 SCVAL(entry_offset,1,type_val);
241 SIVAL(entry_offset,2,entry_val);
242 DEBUG(10,("create_pai_buf_v2: entry %u [0x%x] [0x%x] [0x%x]\n",
244 (unsigned int)ace_list->ace_flags,
245 (unsigned int)type_val,
246 (unsigned int)entry_val ));
247 i++;
248 entry_offset += PAI_V2_ENTRY_LENGTH;
251 return pai_buf;
254 /************************************************************************
255 Store the user.SAMBA_PAI attribute on disk.
256 ************************************************************************/
258 static void store_inheritance_attributes(files_struct *fsp,
259 canon_ace *file_ace_list,
260 canon_ace *dir_ace_list,
261 uint16_t sd_type)
263 int ret;
264 size_t store_size;
265 char *pai_buf;
267 if (!lp_map_acl_inherit(SNUM(fsp->conn))) {
268 return;
271 pai_buf = create_pai_buf_v2(file_ace_list, dir_ace_list,
272 sd_type, &store_size);
274 ret = SMB_VFS_FSETXATTR(fsp, SAMBA_POSIX_INHERITANCE_EA_NAME,
275 pai_buf, store_size, 0);
277 TALLOC_FREE(pai_buf);
279 DEBUG(10,("store_inheritance_attribute: type 0x%x for file %s\n",
280 (unsigned int)sd_type,
281 fsp_str_dbg(fsp)));
283 if (ret == -1 && !no_acl_syscall_error(errno)) {
284 DEBUG(1,("store_inheritance_attribute: Error %s\n", strerror(errno) ));
288 /************************************************************************
289 Delete the in memory inheritance info.
290 ************************************************************************/
292 static void free_inherited_info(struct pai_val *pal)
294 if (pal) {
295 struct pai_entry *paie, *paie_next;
296 for (paie = pal->entry_list; paie; paie = paie_next) {
297 paie_next = paie->next;
298 TALLOC_FREE(paie);
300 for (paie = pal->def_entry_list; paie; paie = paie_next) {
301 paie_next = paie->next;
302 TALLOC_FREE(paie);
304 TALLOC_FREE(pal);
308 /************************************************************************
309 Get any stored ACE flags.
310 ************************************************************************/
312 static uint16_t get_pai_flags(struct pai_val *pal, canon_ace *ace_entry, bool default_ace)
314 struct pai_entry *paie;
316 if (!pal) {
317 return 0;
320 /* If the entry exists it is inherited. */
321 for (paie = (default_ace ? pal->def_entry_list : pal->entry_list); paie; paie = paie->next) {
322 if (ace_entry->owner_type == paie->owner_type &&
323 get_entry_val(ace_entry) == get_pai_entry_val(paie))
324 return paie->ace_flags;
326 return 0;
329 /************************************************************************
330 Ensure an attribute just read is valid - v1.
331 ************************************************************************/
333 static bool check_pai_ok_v1(const char *pai_buf, size_t pai_buf_data_size)
335 uint16_t num_entries;
336 uint16_t num_def_entries;
338 if (pai_buf_data_size < PAI_V1_ENTRIES_BASE) {
339 /* Corrupted - too small. */
340 return false;
343 if (CVAL(pai_buf,PAI_VERSION_OFFSET) != PAI_V1_VERSION) {
344 return false;
347 num_entries = SVAL(pai_buf,PAI_V1_NUM_ENTRIES_OFFSET);
348 num_def_entries = SVAL(pai_buf,PAI_V1_NUM_DEFAULT_ENTRIES_OFFSET);
350 /* Check the entry lists match. */
351 /* Each entry is 5 bytes (type plus 4 bytes of uid or gid). */
353 if (((num_entries + num_def_entries)*PAI_V1_ENTRY_LENGTH) +
354 PAI_V1_ENTRIES_BASE != pai_buf_data_size) {
355 return false;
358 return true;
361 /************************************************************************
362 Ensure an attribute just read is valid - v2.
363 ************************************************************************/
365 static bool check_pai_ok_v2(const char *pai_buf, size_t pai_buf_data_size)
367 uint16_t num_entries;
368 uint16_t num_def_entries;
370 if (pai_buf_data_size < PAI_V2_ENTRIES_BASE) {
371 /* Corrupted - too small. */
372 return false;
375 if (CVAL(pai_buf,PAI_VERSION_OFFSET) != PAI_V2_VERSION) {
376 return false;
379 num_entries = SVAL(pai_buf,PAI_V2_NUM_ENTRIES_OFFSET);
380 num_def_entries = SVAL(pai_buf,PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET);
382 /* Check the entry lists match. */
383 /* Each entry is 6 bytes (flags + type + 4 bytes of uid or gid). */
385 if (((num_entries + num_def_entries)*PAI_V2_ENTRY_LENGTH) +
386 PAI_V2_ENTRIES_BASE != pai_buf_data_size) {
387 return false;
390 return true;
393 /************************************************************************
394 Decode the owner.
395 ************************************************************************/
397 static bool get_pai_owner_type(struct pai_entry *paie, const char *entry_offset)
399 paie->owner_type = (enum ace_owner)CVAL(entry_offset,0);
400 switch( paie->owner_type) {
401 case UID_ACE:
402 paie->unix_ug.type = ID_TYPE_UID;
403 paie->unix_ug.id = (uid_t)IVAL(entry_offset,1);
404 DEBUG(10,("get_pai_owner_type: uid = %u\n",
405 (unsigned int)paie->unix_ug.id ));
406 break;
407 case GID_ACE:
408 paie->unix_ug.type = ID_TYPE_GID;
409 paie->unix_ug.id = (gid_t)IVAL(entry_offset,1);
410 DEBUG(10,("get_pai_owner_type: gid = %u\n",
411 (unsigned int)paie->unix_ug.id ));
412 break;
413 case WORLD_ACE:
414 paie->unix_ug.type = ID_TYPE_NOT_SPECIFIED;
415 paie->unix_ug.id = -1;
416 DEBUG(10,("get_pai_owner_type: world ace\n"));
417 break;
418 default:
419 DEBUG(10,("get_pai_owner_type: unknown type %u\n",
420 (unsigned int)paie->owner_type ));
421 return false;
423 return true;
426 /************************************************************************
427 Process v2 entries.
428 ************************************************************************/
430 static const char *create_pai_v1_entries(struct pai_val *paiv,
431 const char *entry_offset,
432 bool def_entry)
434 unsigned int i;
436 for (i = 0; i < paiv->num_entries; i++) {
437 struct pai_entry *paie = talloc(talloc_tos(), struct pai_entry);
438 if (!paie) {
439 return NULL;
442 paie->ace_flags = SEC_ACE_FLAG_INHERITED_ACE;
443 if (!get_pai_owner_type(paie, entry_offset)) {
444 TALLOC_FREE(paie);
445 return NULL;
448 if (!def_entry) {
449 DLIST_ADD(paiv->entry_list, paie);
450 } else {
451 DLIST_ADD(paiv->def_entry_list, paie);
453 entry_offset += PAI_V1_ENTRY_LENGTH;
455 return entry_offset;
458 /************************************************************************
459 Convert to in-memory format from version 1.
460 ************************************************************************/
462 static struct pai_val *create_pai_val_v1(const char *buf, size_t size)
464 const char *entry_offset;
465 struct pai_val *paiv = NULL;
467 if (!check_pai_ok_v1(buf, size)) {
468 return NULL;
471 paiv = talloc(talloc_tos(), struct pai_val);
472 if (!paiv) {
473 return NULL;
476 memset(paiv, '\0', sizeof(struct pai_val));
478 paiv->sd_type = (CVAL(buf,PAI_V1_FLAG_OFFSET) == PAI_V1_ACL_FLAG_PROTECTED) ?
479 SEC_DESC_DACL_PROTECTED : 0;
481 paiv->num_entries = SVAL(buf,PAI_V1_NUM_ENTRIES_OFFSET);
482 paiv->num_def_entries = SVAL(buf,PAI_V1_NUM_DEFAULT_ENTRIES_OFFSET);
484 entry_offset = buf + PAI_V1_ENTRIES_BASE;
486 DEBUG(10,("create_pai_val: num_entries = %u, num_def_entries = %u\n",
487 paiv->num_entries, paiv->num_def_entries ));
489 entry_offset = create_pai_v1_entries(paiv, entry_offset, false);
490 if (entry_offset == NULL) {
491 free_inherited_info(paiv);
492 return NULL;
494 entry_offset = create_pai_v1_entries(paiv, entry_offset, true);
495 if (entry_offset == NULL) {
496 free_inherited_info(paiv);
497 return NULL;
500 return paiv;
503 /************************************************************************
504 Process v2 entries.
505 ************************************************************************/
507 static const char *create_pai_v2_entries(struct pai_val *paiv,
508 unsigned int num_entries,
509 const char *entry_offset,
510 bool def_entry)
512 unsigned int i;
514 for (i = 0; i < num_entries; i++) {
515 struct pai_entry *paie = talloc(talloc_tos(), struct pai_entry);
516 if (!paie) {
517 return NULL;
520 paie->ace_flags = CVAL(entry_offset,0);
522 if (!get_pai_owner_type(paie, entry_offset+1)) {
523 TALLOC_FREE(paie);
524 return NULL;
526 if (!def_entry) {
527 DLIST_ADD(paiv->entry_list, paie);
528 } else {
529 DLIST_ADD(paiv->def_entry_list, paie);
531 entry_offset += PAI_V2_ENTRY_LENGTH;
533 return entry_offset;
536 /************************************************************************
537 Convert to in-memory format from version 2.
538 ************************************************************************/
540 static struct pai_val *create_pai_val_v2(const char *buf, size_t size)
542 const char *entry_offset;
543 struct pai_val *paiv = NULL;
545 if (!check_pai_ok_v2(buf, size)) {
546 return NULL;
549 paiv = talloc(talloc_tos(), struct pai_val);
550 if (!paiv) {
551 return NULL;
554 memset(paiv, '\0', sizeof(struct pai_val));
556 paiv->sd_type = SVAL(buf,PAI_V2_TYPE_OFFSET);
558 paiv->num_entries = SVAL(buf,PAI_V2_NUM_ENTRIES_OFFSET);
559 paiv->num_def_entries = SVAL(buf,PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET);
561 entry_offset = buf + PAI_V2_ENTRIES_BASE;
563 DEBUG(10,("create_pai_val_v2: sd_type = 0x%x num_entries = %u, num_def_entries = %u\n",
564 (unsigned int)paiv->sd_type,
565 paiv->num_entries, paiv->num_def_entries ));
567 entry_offset = create_pai_v2_entries(paiv, paiv->num_entries,
568 entry_offset, false);
569 if (entry_offset == NULL) {
570 free_inherited_info(paiv);
571 return NULL;
573 entry_offset = create_pai_v2_entries(paiv, paiv->num_def_entries,
574 entry_offset, true);
575 if (entry_offset == NULL) {
576 free_inherited_info(paiv);
577 return NULL;
580 return paiv;
583 /************************************************************************
584 Convert to in-memory format - from either version 1 or 2.
585 ************************************************************************/
587 static struct pai_val *create_pai_val(const char *buf, size_t size)
589 if (size < 1) {
590 return NULL;
592 if (CVAL(buf,PAI_VERSION_OFFSET) == PAI_V1_VERSION) {
593 return create_pai_val_v1(buf, size);
594 } else if (CVAL(buf,PAI_VERSION_OFFSET) == PAI_V2_VERSION) {
595 return create_pai_val_v2(buf, size);
596 } else {
597 return NULL;
601 /************************************************************************
602 Load the user.SAMBA_PAI attribute.
603 ************************************************************************/
605 static struct pai_val *fload_inherited_info(files_struct *fsp)
607 char *pai_buf;
608 size_t pai_buf_size = 1024;
609 struct pai_val *paiv = NULL;
610 ssize_t ret;
612 if (!lp_map_acl_inherit(SNUM(fsp->conn))) {
613 return NULL;
616 if ((pai_buf = talloc_array(talloc_tos(), char, pai_buf_size)) == NULL) {
617 return NULL;
620 do {
621 ret = SMB_VFS_FGETXATTR(fsp, SAMBA_POSIX_INHERITANCE_EA_NAME,
622 pai_buf, pai_buf_size);
623 if (ret == -1) {
624 if (errno != ERANGE) {
625 break;
627 /* Buffer too small - enlarge it. */
628 pai_buf_size *= 2;
629 TALLOC_FREE(pai_buf);
630 if (pai_buf_size > 1024*1024) {
631 return NULL; /* Limit malloc to 1mb. */
633 if ((pai_buf = talloc_array(talloc_tos(), char, pai_buf_size)) == NULL)
634 return NULL;
636 } while (ret == -1);
638 DEBUG(10,("load_inherited_info: ret = %lu for file %s\n",
639 (unsigned long)ret, fsp_str_dbg(fsp)));
641 if (ret == -1) {
642 /* No attribute or not supported. */
643 #if defined(ENOATTR)
644 if (errno != ENOATTR)
645 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno) ));
646 #else
647 if (errno != ENOSYS)
648 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno) ));
649 #endif
650 TALLOC_FREE(pai_buf);
651 return NULL;
654 paiv = create_pai_val(pai_buf, ret);
656 if (paiv) {
657 DEBUG(10,("load_inherited_info: ACL type is 0x%x for file %s\n",
658 (unsigned int)paiv->sd_type, fsp_str_dbg(fsp)));
661 TALLOC_FREE(pai_buf);
662 return paiv;
665 /****************************************************************************
666 Functions to manipulate the internal ACE format.
667 ****************************************************************************/
669 /****************************************************************************
670 Count a linked list of canonical ACE entries.
671 ****************************************************************************/
673 static size_t count_canon_ace_list( canon_ace *l_head )
675 size_t count = 0;
676 canon_ace *ace;
678 for (ace = l_head; ace; ace = ace->next)
679 count++;
681 return count;
684 /****************************************************************************
685 Free a linked list of canonical ACE entries.
686 ****************************************************************************/
688 static void free_canon_ace_list( canon_ace *l_head )
690 canon_ace *list, *next;
692 for (list = l_head; list; list = next) {
693 next = list->next;
694 DLIST_REMOVE(l_head, list);
695 TALLOC_FREE(list);
699 /****************************************************************************
700 Function to duplicate a canon_ace entry.
701 ****************************************************************************/
703 static canon_ace *dup_canon_ace( canon_ace *src_ace)
705 canon_ace *dst_ace = talloc(talloc_tos(), canon_ace);
707 if (dst_ace == NULL)
708 return NULL;
710 *dst_ace = *src_ace;
711 dst_ace->prev = dst_ace->next = NULL;
712 return dst_ace;
715 /****************************************************************************
716 Print out a canon ace.
717 ****************************************************************************/
719 static void print_canon_ace(canon_ace *pace, int num)
721 struct dom_sid_buf buf;
722 dbgtext( "canon_ace index %d. Type = %s ", num, pace->attr == ALLOW_ACE ? "allow" : "deny" );
723 dbgtext( "SID = %s ", dom_sid_str_buf(&pace->trustee, &buf));
724 if (pace->owner_type == UID_ACE) {
725 dbgtext( "uid %u ", (unsigned int)pace->unix_ug.id);
726 } else if (pace->owner_type == GID_ACE) {
727 dbgtext( "gid %u ", (unsigned int)pace->unix_ug.id);
728 } else
729 dbgtext( "other ");
730 switch (pace->type) {
731 case SMB_ACL_USER:
732 dbgtext( "SMB_ACL_USER ");
733 break;
734 case SMB_ACL_USER_OBJ:
735 dbgtext( "SMB_ACL_USER_OBJ ");
736 break;
737 case SMB_ACL_GROUP:
738 dbgtext( "SMB_ACL_GROUP ");
739 break;
740 case SMB_ACL_GROUP_OBJ:
741 dbgtext( "SMB_ACL_GROUP_OBJ ");
742 break;
743 case SMB_ACL_OTHER:
744 dbgtext( "SMB_ACL_OTHER ");
745 break;
746 default:
747 dbgtext( "MASK " );
748 break;
751 dbgtext( "ace_flags = 0x%x ", (unsigned int)pace->ace_flags);
752 dbgtext( "perms ");
753 dbgtext( "%c", pace->perms & S_IRUSR ? 'r' : '-');
754 dbgtext( "%c", pace->perms & S_IWUSR ? 'w' : '-');
755 dbgtext( "%c\n", pace->perms & S_IXUSR ? 'x' : '-');
758 /****************************************************************************
759 Print out a canon ace list.
760 ****************************************************************************/
762 static void print_canon_ace_list(const char *name, canon_ace *ace_list)
764 int count = 0;
766 if( DEBUGLVL( 10 )) {
767 dbgtext( "print_canon_ace_list: %s\n", name );
768 for (;ace_list; ace_list = ace_list->next, count++)
769 print_canon_ace(ace_list, count );
773 /****************************************************************************
774 Map POSIX ACL perms to canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits).
775 ****************************************************************************/
777 static mode_t convert_permset_to_mode_t(SMB_ACL_PERMSET_T permset)
779 mode_t ret = 0;
781 ret |= (sys_acl_get_perm(permset, SMB_ACL_READ) ? S_IRUSR : 0);
782 ret |= (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? S_IWUSR : 0);
783 ret |= (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? S_IXUSR : 0);
785 return ret;
788 /****************************************************************************
789 Map generic UNIX permissions to canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits).
790 ****************************************************************************/
792 mode_t unix_perms_to_acl_perms(mode_t mode, int r_mask, int w_mask, int x_mask)
794 mode_t ret = 0;
796 if (mode & r_mask)
797 ret |= S_IRUSR;
798 if (mode & w_mask)
799 ret |= S_IWUSR;
800 if (mode & x_mask)
801 ret |= S_IXUSR;
803 return ret;
806 /****************************************************************************
807 Map canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits) to
808 an SMB_ACL_PERMSET_T.
809 ****************************************************************************/
811 int map_acl_perms_to_permset(mode_t mode, SMB_ACL_PERMSET_T *p_permset)
813 if (sys_acl_clear_perms(*p_permset) == -1)
814 return -1;
815 if (mode & S_IRUSR) {
816 if (sys_acl_add_perm(*p_permset, SMB_ACL_READ) == -1)
817 return -1;
819 if (mode & S_IWUSR) {
820 if (sys_acl_add_perm(*p_permset, SMB_ACL_WRITE) == -1)
821 return -1;
823 if (mode & S_IXUSR) {
824 if (sys_acl_add_perm(*p_permset, SMB_ACL_EXECUTE) == -1)
825 return -1;
827 return 0;
830 /****************************************************************************
831 Function to create owner and group SIDs from a SMB_STRUCT_STAT.
832 ****************************************************************************/
834 static void create_file_sids(const SMB_STRUCT_STAT *psbuf,
835 struct dom_sid *powner_sid,
836 struct dom_sid *pgroup_sid)
838 uid_to_sid( powner_sid, psbuf->st_ex_uid );
839 gid_to_sid( pgroup_sid, psbuf->st_ex_gid );
842 /****************************************************************************
843 Merge aces with a common UID or GID - if both are allow or deny, OR the permissions together and
844 delete the second one. If the first is deny, mask the permissions off and delete the allow
845 if the permissions become zero, delete the deny if the permissions are non zero.
846 ****************************************************************************/
848 static void merge_aces( canon_ace **pp_list_head, bool dir_acl)
850 canon_ace *l_head = *pp_list_head;
851 canon_ace *curr_ace_outer;
852 canon_ace *curr_ace_outer_next;
855 * First, merge allow entries with identical SIDs, and deny entries
856 * with identical SIDs.
859 for (curr_ace_outer = l_head; curr_ace_outer; curr_ace_outer = curr_ace_outer_next) {
860 canon_ace *curr_ace;
861 canon_ace *curr_ace_next;
863 curr_ace_outer_next = curr_ace_outer->next; /* Save the link in case we delete. */
865 for (curr_ace = curr_ace_outer->next; curr_ace; curr_ace = curr_ace_next) {
866 bool can_merge = false;
868 curr_ace_next = curr_ace->next; /* Save the link in case of delete. */
870 /* For file ACLs we can merge if the SIDs and ALLOW/DENY
871 * types are the same. For directory acls we must also
872 * ensure the POSIX ACL types are the same.
874 * For the IDMAP_BOTH case, we must not merge
875 * the UID and GID ACE values for same SID
878 if (!dir_acl) {
879 can_merge = (curr_ace->unix_ug.id == curr_ace_outer->unix_ug.id &&
880 curr_ace->owner_type == curr_ace_outer->owner_type &&
881 (curr_ace->attr == curr_ace_outer->attr));
882 } else {
883 can_merge = (curr_ace->unix_ug.id == curr_ace_outer->unix_ug.id &&
884 curr_ace->owner_type == curr_ace_outer->owner_type &&
885 (curr_ace->type == curr_ace_outer->type) &&
886 (curr_ace->attr == curr_ace_outer->attr));
889 if (can_merge) {
890 if( DEBUGLVL( 10 )) {
891 dbgtext("merge_aces: Merging ACE's\n");
892 print_canon_ace( curr_ace_outer, 0);
893 print_canon_ace( curr_ace, 0);
896 /* Merge two allow or two deny ACE's. */
898 /* Theoretically we shouldn't merge a dir ACE if
899 * one ACE has the CI flag set, and the other
900 * ACE has the OI flag set, but this is rare
901 * enough we can ignore it. */
903 curr_ace_outer->perms |= curr_ace->perms;
904 curr_ace_outer->ace_flags |= curr_ace->ace_flags;
905 DLIST_REMOVE(l_head, curr_ace);
906 TALLOC_FREE(curr_ace);
907 curr_ace_outer_next = curr_ace_outer->next; /* We may have deleted the link. */
913 * Now go through and mask off allow permissions with deny permissions.
914 * We can delete either the allow or deny here as we know that each SID
915 * appears only once in the list.
918 for (curr_ace_outer = l_head; curr_ace_outer; curr_ace_outer = curr_ace_outer_next) {
919 canon_ace *curr_ace;
920 canon_ace *curr_ace_next;
922 curr_ace_outer_next = curr_ace_outer->next; /* Save the link in case we delete. */
924 for (curr_ace = curr_ace_outer->next; curr_ace; curr_ace = curr_ace_next) {
926 curr_ace_next = curr_ace->next; /* Save the link in case of delete. */
929 * Subtract ACE's with different entries. Due to the ordering constraints
930 * we've put on the ACL, we know the deny must be the first one.
933 if (curr_ace->unix_ug.id == curr_ace_outer->unix_ug.id &&
934 (curr_ace->owner_type == curr_ace_outer->owner_type) &&
935 (curr_ace_outer->attr == DENY_ACE) && (curr_ace->attr == ALLOW_ACE)) {
937 if( DEBUGLVL( 10 )) {
938 dbgtext("merge_aces: Masking ACE's\n");
939 print_canon_ace( curr_ace_outer, 0);
940 print_canon_ace( curr_ace, 0);
943 curr_ace->perms &= ~curr_ace_outer->perms;
945 if (curr_ace->perms == 0) {
948 * The deny overrides the allow. Remove the allow.
951 DLIST_REMOVE(l_head, curr_ace);
952 TALLOC_FREE(curr_ace);
953 curr_ace_outer_next = curr_ace_outer->next; /* We may have deleted the link. */
955 } else {
958 * Even after removing permissions, there
959 * are still allow permissions - delete the deny.
960 * It is safe to delete the deny here,
961 * as we are guarenteed by the deny first
962 * ordering that all the deny entries for
963 * this SID have already been merged into one
964 * before we can get to an allow ace.
967 DLIST_REMOVE(l_head, curr_ace_outer);
968 TALLOC_FREE(curr_ace_outer);
969 break;
973 } /* end for curr_ace */
974 } /* end for curr_ace_outer */
976 /* We may have modified the list. */
978 *pp_list_head = l_head;
981 /****************************************************************************
982 Map canon_ace perms to permission bits NT.
983 The attr element is not used here - we only process deny entries on set,
984 not get. Deny entries are implicit on get with ace->perms = 0.
985 ****************************************************************************/
987 uint32_t map_canon_ace_perms(int snum,
988 enum security_ace_type *pacl_type,
989 mode_t perms,
990 bool directory_ace)
992 uint32_t nt_mask = 0;
994 *pacl_type = SEC_ACE_TYPE_ACCESS_ALLOWED;
996 if (lp_acl_map_full_control(snum) && ((perms & ALL_ACE_PERMS) == ALL_ACE_PERMS)) {
997 if (directory_ace) {
998 nt_mask = UNIX_DIRECTORY_ACCESS_RWX;
999 } else {
1000 nt_mask = (UNIX_ACCESS_RWX & ~DELETE_ACCESS);
1002 } else if ((perms & ALL_ACE_PERMS) == (mode_t)0) {
1004 * Windows NT refuses to display ACEs with no permissions in them (but
1005 * they are perfectly legal with Windows 2000). If the ACE has empty
1006 * permissions we cannot use 0, so we use the otherwise unused
1007 * WRITE_OWNER permission, which we ignore when we set an ACL.
1008 * We abstract this into a #define of UNIX_ACCESS_NONE to allow this
1009 * to be changed in the future.
1012 nt_mask = 0;
1013 } else {
1014 if (directory_ace) {
1015 nt_mask |= ((perms & S_IRUSR) ? UNIX_DIRECTORY_ACCESS_R : 0 );
1016 nt_mask |= ((perms & S_IWUSR) ? UNIX_DIRECTORY_ACCESS_W : 0 );
1017 nt_mask |= ((perms & S_IXUSR) ? UNIX_DIRECTORY_ACCESS_X : 0 );
1018 } else {
1019 nt_mask |= ((perms & S_IRUSR) ? UNIX_ACCESS_R : 0 );
1020 nt_mask |= ((perms & S_IWUSR) ? UNIX_ACCESS_W : 0 );
1021 nt_mask |= ((perms & S_IXUSR) ? UNIX_ACCESS_X : 0 );
1025 if ((perms & S_IWUSR) && lp_dos_filemode(snum)) {
1026 nt_mask |= (SEC_STD_WRITE_DAC|SEC_STD_WRITE_OWNER|DELETE_ACCESS);
1029 DEBUG(10,("map_canon_ace_perms: Mapped (UNIX) %x to (NT) %x\n",
1030 (unsigned int)perms, (unsigned int)nt_mask ));
1032 return nt_mask;
1035 /****************************************************************************
1036 Map NT perms to a UNIX mode_t.
1037 ****************************************************************************/
1039 #define FILE_SPECIFIC_READ_BITS (FILE_READ_DATA|FILE_READ_EA)
1040 #define FILE_SPECIFIC_WRITE_BITS (FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_WRITE_EA)
1041 #define FILE_SPECIFIC_EXECUTE_BITS (FILE_EXECUTE)
1043 static mode_t map_nt_perms( uint32_t *mask, int type)
1045 mode_t mode = 0;
1047 switch(type) {
1048 case S_IRUSR:
1049 if((*mask) & GENERIC_ALL_ACCESS)
1050 mode = S_IRUSR|S_IWUSR|S_IXUSR;
1051 else {
1052 mode |= ((*mask) & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IRUSR : 0;
1053 mode |= ((*mask) & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWUSR : 0;
1054 mode |= ((*mask) & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXUSR : 0;
1056 break;
1057 case S_IRGRP:
1058 if((*mask) & GENERIC_ALL_ACCESS)
1059 mode = S_IRGRP|S_IWGRP|S_IXGRP;
1060 else {
1061 mode |= ((*mask) & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IRGRP : 0;
1062 mode |= ((*mask) & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWGRP : 0;
1063 mode |= ((*mask) & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXGRP : 0;
1065 break;
1066 case S_IROTH:
1067 if((*mask) & GENERIC_ALL_ACCESS)
1068 mode = S_IROTH|S_IWOTH|S_IXOTH;
1069 else {
1070 mode |= ((*mask) & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IROTH : 0;
1071 mode |= ((*mask) & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWOTH : 0;
1072 mode |= ((*mask) & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXOTH : 0;
1074 break;
1077 return mode;
1080 /****************************************************************************
1081 Unpack a struct security_descriptor into a UNIX owner and group.
1082 ****************************************************************************/
1084 static NTSTATUS unpack_nt_owners(struct connection_struct *conn,
1085 uid_t *puser, gid_t *pgrp,
1086 uint32_t security_info_sent,
1087 const struct security_descriptor *psd)
1089 *puser = (uid_t)-1;
1090 *pgrp = (gid_t)-1;
1092 if(security_info_sent == 0) {
1093 DEBUG(0,("unpack_nt_owners: no security info sent !\n"));
1094 return NT_STATUS_OK;
1098 * Validate the owner and group SID's.
1101 DEBUG(5,("unpack_nt_owners: validating owner_sids.\n"));
1104 * Don't immediately fail if the owner sid cannot be validated.
1105 * This may be a group chown only set.
1108 if (security_info_sent & SECINFO_OWNER) {
1109 if (!sid_to_uid(psd->owner_sid, puser)) {
1110 if (lp_force_unknown_acl_user(SNUM(conn))) {
1111 /* this allows take ownership to work
1112 * reasonably */
1113 *puser = get_current_uid(conn);
1114 } else {
1115 struct dom_sid_buf buf;
1116 DBG_NOTICE("unable to validate"
1117 " owner sid for %s\n",
1118 dom_sid_str_buf(psd->owner_sid,
1119 &buf));
1120 return NT_STATUS_INVALID_OWNER;
1123 DEBUG(3,("unpack_nt_owners: owner sid mapped to uid %u\n",
1124 (unsigned int)*puser ));
1128 * Don't immediately fail if the group sid cannot be validated.
1129 * This may be an owner chown only set.
1132 if (security_info_sent & SECINFO_GROUP) {
1133 if (!sid_to_gid(psd->group_sid, pgrp)) {
1134 if (lp_force_unknown_acl_user(SNUM(conn))) {
1135 /* this allows take group ownership to work
1136 * reasonably */
1137 *pgrp = get_current_gid(conn);
1138 } else {
1139 DEBUG(3,("unpack_nt_owners: unable to validate"
1140 " group sid.\n"));
1141 return NT_STATUS_INVALID_OWNER;
1144 DEBUG(3,("unpack_nt_owners: group sid mapped to gid %u\n",
1145 (unsigned int)*pgrp));
1148 DEBUG(5,("unpack_nt_owners: owner_sids validated.\n"));
1150 return NT_STATUS_OK;
1154 static void trim_ace_perms(canon_ace *pace)
1156 pace->perms = pace->perms & (S_IXUSR|S_IWUSR|S_IRUSR);
1159 static void ensure_minimal_owner_ace_perms(const bool is_directory,
1160 canon_ace *pace)
1162 pace->perms |= S_IRUSR;
1163 if (is_directory) {
1164 pace->perms |= (S_IWUSR|S_IXUSR);
1168 /****************************************************************************
1169 Check if a given uid/SID is in a group gid/SID. This is probably very
1170 expensive and will need optimisation. A *lot* of optimisation :-). JRA.
1171 ****************************************************************************/
1173 static bool uid_entry_in_group(connection_struct *conn, canon_ace *uid_ace, canon_ace *group_ace )
1175 bool is_sid = false;
1176 bool has_sid = false;
1177 struct security_token *security_token = NULL;
1179 /* "Everyone" always matches every uid. */
1181 if (dom_sid_equal(&group_ace->trustee, &global_sid_World))
1182 return True;
1184 security_token = conn->session_info->security_token;
1185 /* security_token should not be NULL */
1186 SMB_ASSERT(security_token);
1187 is_sid = security_token_is_sid(security_token,
1188 &uid_ace->trustee);
1189 if (is_sid) {
1190 has_sid = security_token_has_sid(security_token,
1191 &group_ace->trustee);
1193 if (has_sid) {
1194 return true;
1199 * if it's the current user, we already have the unix token
1200 * and don't need to do the complex user_in_group_sid() call
1202 if (uid_ace->unix_ug.id == get_current_uid(conn)) {
1203 const struct security_unix_token *curr_utok = NULL;
1204 size_t i;
1206 if (group_ace->unix_ug.id == get_current_gid(conn)) {
1207 return True;
1210 curr_utok = get_current_utok(conn);
1211 for (i=0; i < curr_utok->ngroups; i++) {
1212 if (group_ace->unix_ug.id == curr_utok->groups[i]) {
1213 return True;
1219 * user_in_group_sid() uses create_token_from_sid()
1220 * which creates an artificial NT token given just a username,
1221 * so this is not reliable for users from foreign domains
1222 * exported by winbindd!
1224 return user_sid_in_group_sid(&uid_ace->trustee, &group_ace->trustee);
1227 /****************************************************************************
1228 A well formed POSIX file or default ACL has at least 3 entries, a
1229 SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ.
1230 In addition, the owner must always have at least read access.
1231 When using this call on get_acl, the pst struct is valid and contains
1232 the mode of the file.
1233 ****************************************************************************/
1235 static bool ensure_canon_entry_valid_on_get(connection_struct *conn,
1236 canon_ace **pp_ace,
1237 const struct dom_sid *pfile_owner_sid,
1238 const struct dom_sid *pfile_grp_sid,
1239 const SMB_STRUCT_STAT *pst)
1241 canon_ace *pace;
1242 bool got_user = false;
1243 bool got_group = false;
1244 bool got_other = false;
1246 for (pace = *pp_ace; pace; pace = pace->next) {
1247 if (pace->type == SMB_ACL_USER_OBJ) {
1248 got_user = true;
1249 } else if (pace->type == SMB_ACL_GROUP_OBJ) {
1250 got_group = true;
1251 } else if (pace->type == SMB_ACL_OTHER) {
1252 got_other = true;
1256 if (!got_user) {
1257 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1258 DEBUG(0,("malloc fail.\n"));
1259 return false;
1262 ZERO_STRUCTP(pace);
1263 pace->type = SMB_ACL_USER_OBJ;
1264 pace->owner_type = UID_ACE;
1265 pace->unix_ug.type = ID_TYPE_UID;
1266 pace->unix_ug.id = pst->st_ex_uid;
1267 pace->trustee = *pfile_owner_sid;
1268 pace->attr = ALLOW_ACE;
1269 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IRUSR, S_IWUSR, S_IXUSR);
1270 DLIST_ADD(*pp_ace, pace);
1273 if (!got_group) {
1274 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1275 DEBUG(0,("malloc fail.\n"));
1276 return false;
1279 ZERO_STRUCTP(pace);
1280 pace->type = SMB_ACL_GROUP_OBJ;
1281 pace->owner_type = GID_ACE;
1282 pace->unix_ug.type = ID_TYPE_GID;
1283 pace->unix_ug.id = pst->st_ex_gid;
1284 pace->trustee = *pfile_grp_sid;
1285 pace->attr = ALLOW_ACE;
1286 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IRGRP, S_IWGRP, S_IXGRP);
1287 DLIST_ADD(*pp_ace, pace);
1290 if (!got_other) {
1291 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1292 DEBUG(0,("malloc fail.\n"));
1293 return false;
1296 ZERO_STRUCTP(pace);
1297 pace->type = SMB_ACL_OTHER;
1298 pace->owner_type = WORLD_ACE;
1299 pace->unix_ug.type = ID_TYPE_NOT_SPECIFIED;
1300 pace->unix_ug.id = -1;
1301 pace->trustee = global_sid_World;
1302 pace->attr = ALLOW_ACE;
1303 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IROTH, S_IWOTH, S_IXOTH);
1304 DLIST_ADD(*pp_ace, pace);
1307 return true;
1310 /****************************************************************************
1311 A well formed POSIX file or default ACL has at least 3 entries, a
1312 SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ.
1313 In addition, the owner must always have at least read access.
1314 When using this call on set_acl, the pst struct has
1315 been modified to have a mode containing the default for this file or directory
1316 type.
1317 ****************************************************************************/
1319 static bool ensure_canon_entry_valid_on_set(connection_struct *conn,
1320 canon_ace **pp_ace,
1321 bool is_default_acl,
1322 const struct share_params *params,
1323 const bool is_directory,
1324 const struct dom_sid *pfile_owner_sid,
1325 const struct dom_sid *pfile_grp_sid,
1326 const SMB_STRUCT_STAT *pst)
1328 canon_ace *pace;
1329 canon_ace *pace_user = NULL;
1330 canon_ace *pace_group = NULL;
1331 canon_ace *pace_other = NULL;
1332 bool got_duplicate_user = false;
1333 bool got_duplicate_group = false;
1335 for (pace = *pp_ace; pace; pace = pace->next) {
1336 trim_ace_perms(pace);
1337 if (pace->type == SMB_ACL_USER_OBJ) {
1338 ensure_minimal_owner_ace_perms(is_directory, pace);
1339 pace_user = pace;
1340 } else if (pace->type == SMB_ACL_GROUP_OBJ) {
1341 pace_group = pace;
1342 } else if (pace->type == SMB_ACL_OTHER) {
1343 pace_other = pace;
1347 if (!pace_user) {
1348 canon_ace *pace_iter;
1350 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1351 DEBUG(0,("talloc fail.\n"));
1352 return false;
1355 ZERO_STRUCTP(pace);
1356 pace->type = SMB_ACL_USER_OBJ;
1357 pace->owner_type = UID_ACE;
1358 pace->unix_ug.type = ID_TYPE_UID;
1359 pace->unix_ug.id = pst->st_ex_uid;
1360 pace->trustee = *pfile_owner_sid;
1361 pace->attr = ALLOW_ACE;
1362 /* Start with existing user permissions, principle of least
1363 surprises for the user. */
1364 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IRUSR, S_IWUSR, S_IXUSR);
1366 /* See if the owning user is in any of the other groups in
1367 the ACE, or if there's a matching user entry (by uid
1368 or in the case of ID_TYPE_BOTH by SID).
1369 If so, OR in the permissions from that entry. */
1372 for (pace_iter = *pp_ace; pace_iter; pace_iter = pace_iter->next) {
1373 if (pace_iter->type == SMB_ACL_USER &&
1374 pace_iter->unix_ug.id == pace->unix_ug.id) {
1375 pace->perms |= pace_iter->perms;
1376 } else if (pace_iter->type == SMB_ACL_GROUP_OBJ || pace_iter->type == SMB_ACL_GROUP) {
1377 if (dom_sid_equal(&pace->trustee, &pace_iter->trustee)) {
1378 pace->perms |= pace_iter->perms;
1379 } else if (uid_entry_in_group(conn, pace, pace_iter)) {
1380 pace->perms |= pace_iter->perms;
1385 if (pace->perms == 0) {
1386 /* If we only got an "everyone" perm, just use that. */
1387 if (pace_other)
1388 pace->perms = pace_other->perms;
1392 * Ensure we have default parameters for the
1393 * user (owner) even on default ACLs.
1395 ensure_minimal_owner_ace_perms(is_directory, pace);
1397 DLIST_ADD(*pp_ace, pace);
1398 pace_user = pace;
1401 if (!pace_group) {
1402 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1403 DEBUG(0,("talloc fail.\n"));
1404 return false;
1407 ZERO_STRUCTP(pace);
1408 pace->type = SMB_ACL_GROUP_OBJ;
1409 pace->owner_type = GID_ACE;
1410 pace->unix_ug.type = ID_TYPE_GID;
1411 pace->unix_ug.id = pst->st_ex_gid;
1412 pace->trustee = *pfile_grp_sid;
1413 pace->attr = ALLOW_ACE;
1415 /* If we only got an "everyone" perm, just use that. */
1416 if (pace_other) {
1417 pace->perms = pace_other->perms;
1418 } else {
1419 pace->perms = 0;
1422 DLIST_ADD(*pp_ace, pace);
1423 pace_group = pace;
1426 if (!pace_other) {
1427 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1428 DEBUG(0,("talloc fail.\n"));
1429 return false;
1432 ZERO_STRUCTP(pace);
1433 pace->type = SMB_ACL_OTHER;
1434 pace->owner_type = WORLD_ACE;
1435 pace->unix_ug.type = ID_TYPE_NOT_SPECIFIED;
1436 pace->unix_ug.id = -1;
1437 pace->trustee = global_sid_World;
1438 pace->attr = ALLOW_ACE;
1439 pace->perms = 0;
1441 DLIST_ADD(*pp_ace, pace);
1442 pace_other = pace;
1445 /* Ensure when setting a POSIX ACL, that the uid for a
1446 SMB_ACL_USER_OBJ ACE (the owner ACE entry) has a duplicate
1447 permission entry as an SMB_ACL_USER, and a gid for a
1448 SMB_ACL_GROUP_OBJ ACE (the primary group ACE entry) also has
1449 a duplicate permission entry as an SMB_ACL_GROUP. If not,
1450 then if the ownership or group ownership of this file or
1451 directory gets changed, the user or group can lose their
1452 access. */
1454 for (pace = *pp_ace; pace; pace = pace->next) {
1455 if (pace->type == SMB_ACL_USER &&
1456 pace->unix_ug.id == pace_user->unix_ug.id) {
1457 /* Already got one. */
1458 got_duplicate_user = true;
1459 } else if (pace->type == SMB_ACL_GROUP &&
1460 pace->unix_ug.id == pace_group->unix_ug.id) {
1461 /* Already got one. */
1462 got_duplicate_group = true;
1463 } else if ((pace->type == SMB_ACL_GROUP)
1464 && (dom_sid_equal(&pace->trustee, &pace_user->trustee))) {
1465 /* If the SID owning the file appears
1466 * in a group entry, then we have
1467 * enough duplication, they will still
1468 * have access */
1469 got_duplicate_user = true;
1473 /* If the SID is equal for the user and group that we need
1474 to add the duplicate for, add only the group */
1475 if (!got_duplicate_user && !got_duplicate_group
1476 && dom_sid_equal(&pace_group->trustee,
1477 &pace_user->trustee)) {
1478 /* Add a duplicate SMB_ACL_GROUP entry, this
1479 * will cover the owning SID as well, as it
1480 * will always be mapped to both a uid and
1481 * gid. */
1483 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1484 DEBUG(0,("talloc fail.\n"));
1485 return false;
1488 ZERO_STRUCTP(pace);
1489 pace->type = SMB_ACL_GROUP;;
1490 pace->owner_type = GID_ACE;
1491 pace->unix_ug.type = ID_TYPE_GID;
1492 pace->unix_ug.id = pace_group->unix_ug.id;
1493 pace->trustee = pace_group->trustee;
1494 pace->attr = pace_group->attr;
1495 pace->perms = pace_group->perms;
1497 DLIST_ADD(*pp_ace, pace);
1499 /* We're done here, make sure the
1500 statements below are not executed. */
1501 got_duplicate_user = true;
1502 got_duplicate_group = true;
1505 if (!got_duplicate_user) {
1506 /* Add a duplicate SMB_ACL_USER entry. */
1507 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1508 DEBUG(0,("talloc fail.\n"));
1509 return false;
1512 ZERO_STRUCTP(pace);
1513 pace->type = SMB_ACL_USER;;
1514 pace->owner_type = UID_ACE;
1515 pace->unix_ug.type = ID_TYPE_UID;
1516 pace->unix_ug.id = pace_user->unix_ug.id;
1517 pace->trustee = pace_user->trustee;
1518 pace->attr = pace_user->attr;
1519 pace->perms = pace_user->perms;
1521 DLIST_ADD(*pp_ace, pace);
1523 got_duplicate_user = true;
1526 if (!got_duplicate_group) {
1527 /* Add a duplicate SMB_ACL_GROUP entry. */
1528 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1529 DEBUG(0,("talloc fail.\n"));
1530 return false;
1533 ZERO_STRUCTP(pace);
1534 pace->type = SMB_ACL_GROUP;;
1535 pace->owner_type = GID_ACE;
1536 pace->unix_ug.type = ID_TYPE_GID;
1537 pace->unix_ug.id = pace_group->unix_ug.id;
1538 pace->trustee = pace_group->trustee;
1539 pace->attr = pace_group->attr;
1540 pace->perms = pace_group->perms;
1542 DLIST_ADD(*pp_ace, pace);
1544 got_duplicate_group = true;
1547 return true;
1550 /****************************************************************************
1551 Check if a POSIX ACL has the required SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ entries.
1552 If it does not have them, check if there are any entries where the trustee is the
1553 file owner or the owning group, and map these to SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ.
1554 Note we must not do this to default directory ACLs.
1555 ****************************************************************************/
1557 static void check_owning_objs(canon_ace *ace, struct dom_sid *pfile_owner_sid, struct dom_sid *pfile_grp_sid)
1559 bool got_user_obj, got_group_obj;
1560 canon_ace *current_ace;
1561 int i, entries;
1563 entries = count_canon_ace_list(ace);
1564 got_user_obj = False;
1565 got_group_obj = False;
1567 for (i=0, current_ace = ace; i < entries; i++, current_ace = current_ace->next) {
1568 if (current_ace->type == SMB_ACL_USER_OBJ)
1569 got_user_obj = True;
1570 else if (current_ace->type == SMB_ACL_GROUP_OBJ)
1571 got_group_obj = True;
1573 if (got_user_obj && got_group_obj) {
1574 DEBUG(10,("check_owning_objs: ACL had owning user/group entries.\n"));
1575 return;
1578 for (i=0, current_ace = ace; i < entries; i++, current_ace = current_ace->next) {
1579 if (!got_user_obj && current_ace->owner_type == UID_ACE &&
1580 dom_sid_equal(&current_ace->trustee, pfile_owner_sid)) {
1581 current_ace->type = SMB_ACL_USER_OBJ;
1582 got_user_obj = True;
1584 if (!got_group_obj && current_ace->owner_type == GID_ACE &&
1585 dom_sid_equal(&current_ace->trustee, pfile_grp_sid)) {
1586 current_ace->type = SMB_ACL_GROUP_OBJ;
1587 got_group_obj = True;
1590 if (!got_user_obj)
1591 DEBUG(10,("check_owning_objs: ACL is missing an owner entry.\n"));
1592 if (!got_group_obj)
1593 DEBUG(10,("check_owning_objs: ACL is missing an owning group entry.\n"));
1596 static bool add_current_ace_to_acl(files_struct *fsp, struct security_ace *psa,
1597 canon_ace **file_ace, canon_ace **dir_ace,
1598 bool *got_file_allow, bool *got_dir_allow,
1599 bool *all_aces_are_inherit_only,
1600 canon_ace *current_ace)
1604 * Map the given NT permissions into a UNIX mode_t containing only
1605 * S_I(R|W|X)USR bits.
1608 current_ace->perms |= map_nt_perms( &psa->access_mask, S_IRUSR);
1609 current_ace->attr = (psa->type == SEC_ACE_TYPE_ACCESS_ALLOWED) ? ALLOW_ACE : DENY_ACE;
1611 /* Store the ace_flag. */
1612 current_ace->ace_flags = psa->flags;
1615 * Now add the created ace to either the file list, the directory
1616 * list, or both. We *MUST* preserve the order here (hence we use
1617 * DLIST_ADD_END) as NT ACLs are order dependent.
1620 if (fsp->fsp_flags.is_directory) {
1623 * We can only add to the default POSIX ACE list if the ACE is
1624 * designed to be inherited by both files and directories.
1627 if ((psa->flags & (SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT)) ==
1628 (SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT)) {
1630 canon_ace *current_dir_ace = current_ace;
1631 DLIST_ADD_END(*dir_ace, current_ace);
1634 * Note if this was an allow ace. We can't process
1635 * any further deny ace's after this.
1638 if (current_ace->attr == ALLOW_ACE)
1639 *got_dir_allow = True;
1641 if ((current_ace->attr == DENY_ACE) && *got_dir_allow) {
1642 DEBUG(0,("add_current_ace_to_acl: "
1643 "malformed ACL in "
1644 "inheritable ACL! Deny entry "
1645 "after Allow entry. Failing "
1646 "to set on file %s.\n",
1647 fsp_str_dbg(fsp)));
1648 return False;
1651 if( DEBUGLVL( 10 )) {
1652 dbgtext("add_current_ace_to_acl: adding dir ACL:\n");
1653 print_canon_ace( current_ace, 0);
1657 * If this is not an inherit only ACE we need to add a duplicate
1658 * to the file acl.
1661 if (!(psa->flags & SEC_ACE_FLAG_INHERIT_ONLY)) {
1662 canon_ace *dup_ace = dup_canon_ace(current_ace);
1664 if (!dup_ace) {
1665 DEBUG(0,("add_current_ace_to_acl: malloc fail !\n"));
1666 return False;
1670 * We must not free current_ace here as its
1671 * pointer is now owned by the dir_ace list.
1673 current_ace = dup_ace;
1674 /* We've essentially split this ace into two,
1675 * and added the ace with inheritance request
1676 * bits to the directory ACL. Drop those bits for
1677 * the ACE we're adding to the file list. */
1678 current_ace->ace_flags &= ~(SEC_ACE_FLAG_OBJECT_INHERIT|
1679 SEC_ACE_FLAG_CONTAINER_INHERIT|
1680 SEC_ACE_FLAG_INHERIT_ONLY);
1681 } else {
1683 * We must not free current_ace here as its
1684 * pointer is now owned by the dir_ace list.
1686 current_ace = NULL;
1690 * current_ace is now either owned by file_ace
1691 * or is NULL. We can safely operate on current_dir_ace
1692 * to treat mapping for default acl entries differently
1693 * than access acl entries.
1696 if (current_dir_ace->owner_type == UID_ACE) {
1698 * We already decided above this is a uid,
1699 * for default acls ace's only CREATOR_OWNER
1700 * maps to ACL_USER_OBJ. All other uid
1701 * ace's are ACL_USER.
1703 if (dom_sid_equal(&current_dir_ace->trustee,
1704 &global_sid_Creator_Owner)) {
1705 current_dir_ace->type = SMB_ACL_USER_OBJ;
1706 } else {
1707 current_dir_ace->type = SMB_ACL_USER;
1711 if (current_dir_ace->owner_type == GID_ACE) {
1713 * We already decided above this is a gid,
1714 * for default acls ace's only CREATOR_GROUP
1715 * maps to ACL_GROUP_OBJ. All other uid
1716 * ace's are ACL_GROUP.
1718 if (dom_sid_equal(&current_dir_ace->trustee,
1719 &global_sid_Creator_Group)) {
1720 current_dir_ace->type = SMB_ACL_GROUP_OBJ;
1721 } else {
1722 current_dir_ace->type = SMB_ACL_GROUP;
1729 * Only add to the file ACL if not inherit only.
1732 if (current_ace && !(psa->flags & SEC_ACE_FLAG_INHERIT_ONLY)) {
1733 DLIST_ADD_END(*file_ace, current_ace);
1736 * Note if this was an allow ace. We can't process
1737 * any further deny ace's after this.
1740 if (current_ace->attr == ALLOW_ACE)
1741 *got_file_allow = True;
1743 if ((current_ace->attr == DENY_ACE) && *got_file_allow) {
1744 DEBUG(0,("add_current_ace_to_acl: malformed "
1745 "ACL in file ACL ! Deny entry after "
1746 "Allow entry. Failing to set on file "
1747 "%s.\n", fsp_str_dbg(fsp)));
1748 return False;
1751 if( DEBUGLVL( 10 )) {
1752 dbgtext("add_current_ace_to_acl: adding file ACL:\n");
1753 print_canon_ace( current_ace, 0);
1755 *all_aces_are_inherit_only = False;
1757 * We must not free current_ace here as its
1758 * pointer is now owned by the file_ace list.
1760 current_ace = NULL;
1764 * Free if ACE was not added.
1767 TALLOC_FREE(current_ace);
1768 return true;
1771 /****************************************************************************
1772 Unpack a struct security_descriptor into two canonical ace lists.
1773 ****************************************************************************/
1775 static bool create_canon_ace_lists(files_struct *fsp,
1776 const SMB_STRUCT_STAT *pst,
1777 struct dom_sid *pfile_owner_sid,
1778 struct dom_sid *pfile_grp_sid,
1779 canon_ace **ppfile_ace,
1780 canon_ace **ppdir_ace,
1781 const struct security_acl *dacl)
1783 bool all_aces_are_inherit_only = (fsp->fsp_flags.is_directory);
1784 canon_ace *file_ace = NULL;
1785 canon_ace *dir_ace = NULL;
1786 canon_ace *current_ace = NULL;
1787 bool got_dir_allow = False;
1788 bool got_file_allow = False;
1789 uint32_t i, j;
1791 *ppfile_ace = NULL;
1792 *ppdir_ace = NULL;
1795 * Convert the incoming ACL into a more regular form.
1798 for(i = 0; i < dacl->num_aces; i++) {
1799 struct security_ace *psa = &dacl->aces[i];
1801 if((psa->type != SEC_ACE_TYPE_ACCESS_ALLOWED) && (psa->type != SEC_ACE_TYPE_ACCESS_DENIED)) {
1802 DEBUG(3,("create_canon_ace_lists: unable to set anything but an ALLOW or DENY ACE.\n"));
1803 return False;
1808 * Deal with the fact that NT 4.x re-writes the canonical format
1809 * that we return for default ACLs. If a directory ACE is identical
1810 * to a inherited directory ACE then NT changes the bits so that the
1811 * first ACE is set to OI|IO and the second ACE for this SID is set
1812 * to CI. We need to repair this. JRA.
1815 for(i = 0; i < dacl->num_aces; i++) {
1816 struct security_ace *psa1 = &dacl->aces[i];
1818 for (j = i + 1; j < dacl->num_aces; j++) {
1819 struct security_ace *psa2 = &dacl->aces[j];
1821 if (psa1->access_mask != psa2->access_mask)
1822 continue;
1824 if (!dom_sid_equal(&psa1->trustee, &psa2->trustee))
1825 continue;
1828 * Ok - permission bits and SIDs are equal.
1829 * Check if flags were re-written.
1832 if (psa1->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
1834 psa1->flags |= (psa2->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT));
1835 psa2->flags &= ~(SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT);
1837 } else if (psa2->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
1839 psa2->flags |= (psa1->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT));
1840 psa1->flags &= ~(SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT);
1846 for(i = 0; i < dacl->num_aces; i++) {
1847 struct security_ace *psa = &dacl->aces[i];
1850 * Create a canon_ace entry representing this NT DACL ACE.
1853 if ((current_ace = talloc(talloc_tos(), canon_ace)) == NULL) {
1854 free_canon_ace_list(file_ace);
1855 free_canon_ace_list(dir_ace);
1856 DEBUG(0,("create_canon_ace_lists: malloc fail.\n"));
1857 return False;
1860 ZERO_STRUCTP(current_ace);
1862 sid_copy(&current_ace->trustee, &psa->trustee);
1865 * Try and work out if the SID is a user or group
1866 * as we need to flag these differently for POSIX.
1867 * Note what kind of a POSIX ACL this should map to.
1870 if( dom_sid_equal(&current_ace->trustee, &global_sid_World)) {
1871 current_ace->owner_type = WORLD_ACE;
1872 current_ace->unix_ug.type = ID_TYPE_NOT_SPECIFIED;
1873 current_ace->unix_ug.id = -1;
1874 current_ace->type = SMB_ACL_OTHER;
1875 } else if (dom_sid_equal(&current_ace->trustee, &global_sid_Creator_Owner)) {
1876 current_ace->owner_type = UID_ACE;
1877 current_ace->unix_ug.type = ID_TYPE_UID;
1878 current_ace->unix_ug.id = pst->st_ex_uid;
1879 current_ace->type = SMB_ACL_USER_OBJ;
1882 * The Creator Owner entry only specifies inheritable permissions,
1883 * never access permissions. WinNT doesn't always set the ACE to
1884 * INHERIT_ONLY, though.
1887 psa->flags |= SEC_ACE_FLAG_INHERIT_ONLY;
1889 } else if (dom_sid_equal(&current_ace->trustee, &global_sid_Creator_Group)) {
1890 current_ace->owner_type = GID_ACE;
1891 current_ace->unix_ug.type = ID_TYPE_GID;
1892 current_ace->unix_ug.id = pst->st_ex_gid;
1893 current_ace->type = SMB_ACL_GROUP_OBJ;
1896 * The Creator Group entry only specifies inheritable permissions,
1897 * never access permissions. WinNT doesn't always set the ACE to
1898 * INHERIT_ONLY, though.
1900 psa->flags |= SEC_ACE_FLAG_INHERIT_ONLY;
1902 } else {
1903 struct unixid unixid;
1905 if (!sids_to_unixids(&current_ace->trustee, 1, &unixid)) {
1906 struct dom_sid_buf buf;
1907 free_canon_ace_list(file_ace);
1908 free_canon_ace_list(dir_ace);
1909 TALLOC_FREE(current_ace);
1910 DBG_ERR("sids_to_unixids failed for %s "
1911 "(allocation failure)\n",
1912 dom_sid_str_buf(&current_ace->trustee,
1913 &buf));
1914 return false;
1917 if (unixid.type == ID_TYPE_BOTH) {
1919 * We must add both a user and group
1920 * entry POSIX_ACL.
1921 * This is due to the fact that in POSIX
1922 * user entries are more specific than
1923 * groups.
1925 current_ace->owner_type = UID_ACE;
1926 current_ace->unix_ug.type = ID_TYPE_UID;
1927 current_ace->unix_ug.id = unixid.id;
1928 current_ace->type =
1929 (unixid.id == pst->st_ex_uid) ?
1930 SMB_ACL_USER_OBJ :
1931 SMB_ACL_USER;
1933 /* Add the user object to the posix ACL,
1934 and proceed to the group mapping
1935 below. This handles the talloc_free
1936 of current_ace if not added for some
1937 reason */
1938 if (!add_current_ace_to_acl(fsp,
1939 psa,
1940 &file_ace,
1941 &dir_ace,
1942 &got_file_allow,
1943 &got_dir_allow,
1944 &all_aces_are_inherit_only,
1945 current_ace)) {
1946 free_canon_ace_list(file_ace);
1947 free_canon_ace_list(dir_ace);
1948 return false;
1951 if ((current_ace = talloc(talloc_tos(),
1952 canon_ace)) == NULL) {
1953 free_canon_ace_list(file_ace);
1954 free_canon_ace_list(dir_ace);
1955 DEBUG(0,("create_canon_ace_lists: "
1956 "malloc fail.\n"));
1957 return False;
1960 ZERO_STRUCTP(current_ace);
1962 sid_copy(&current_ace->trustee, &psa->trustee);
1964 current_ace->unix_ug.type = ID_TYPE_GID;
1965 current_ace->unix_ug.id = unixid.id;
1966 current_ace->owner_type = GID_ACE;
1967 /* If it's the primary group, this is a
1968 group_obj, not a group. */
1969 if (current_ace->unix_ug.id == pst->st_ex_gid) {
1970 current_ace->type = SMB_ACL_GROUP_OBJ;
1971 } else {
1972 current_ace->type = SMB_ACL_GROUP;
1975 } else if (unixid.type == ID_TYPE_UID) {
1976 current_ace->owner_type = UID_ACE;
1977 current_ace->unix_ug.type = ID_TYPE_UID;
1978 current_ace->unix_ug.id = unixid.id;
1979 /* If it's the owning user, this is a user_obj,
1980 not a user. */
1981 if (current_ace->unix_ug.id == pst->st_ex_uid) {
1982 current_ace->type = SMB_ACL_USER_OBJ;
1983 } else {
1984 current_ace->type = SMB_ACL_USER;
1986 } else if (unixid.type == ID_TYPE_GID) {
1987 current_ace->unix_ug.type = ID_TYPE_GID;
1988 current_ace->unix_ug.id = unixid.id;
1989 current_ace->owner_type = GID_ACE;
1990 /* If it's the primary group, this is a
1991 group_obj, not a group. */
1992 if (current_ace->unix_ug.id == pst->st_ex_gid) {
1993 current_ace->type = SMB_ACL_GROUP_OBJ;
1994 } else {
1995 current_ace->type = SMB_ACL_GROUP;
1997 } else {
1998 struct dom_sid_buf buf;
2000 * Silently ignore map failures in non-mappable SIDs (NT Authority, BUILTIN etc).
2003 if (non_mappable_sid(&psa->trustee)) {
2004 DBG_DEBUG("ignoring "
2005 "non-mappable SID %s\n",
2006 dom_sid_str_buf(
2007 &psa->trustee,
2008 &buf));
2009 TALLOC_FREE(current_ace);
2010 continue;
2013 if (lp_force_unknown_acl_user(SNUM(fsp->conn))) {
2014 DBG_DEBUG("ignoring unknown or "
2015 "foreign SID %s\n",
2016 dom_sid_str_buf(
2017 &psa->trustee,
2018 &buf));
2019 TALLOC_FREE(current_ace);
2020 continue;
2023 free_canon_ace_list(file_ace);
2024 free_canon_ace_list(dir_ace);
2025 DBG_ERR("unable to map SID %s to uid or "
2026 "gid.\n",
2027 dom_sid_str_buf(&current_ace->trustee,
2028 &buf));
2029 TALLOC_FREE(current_ace);
2030 return false;
2034 /* handles the talloc_free of current_ace if not added for some reason */
2035 if (!add_current_ace_to_acl(fsp, psa, &file_ace, &dir_ace,
2036 &got_file_allow, &got_dir_allow,
2037 &all_aces_are_inherit_only, current_ace)) {
2038 free_canon_ace_list(file_ace);
2039 free_canon_ace_list(dir_ace);
2040 return false;
2044 if (fsp->fsp_flags.is_directory && all_aces_are_inherit_only) {
2046 * Windows 2000 is doing one of these weird 'inherit acl'
2047 * traverses to conserve NTFS ACL resources. Just pretend
2048 * there was no DACL sent. JRA.
2051 DEBUG(10,("create_canon_ace_lists: Win2k inherit acl traverse. Ignoring DACL.\n"));
2052 free_canon_ace_list(file_ace);
2053 free_canon_ace_list(dir_ace);
2054 file_ace = NULL;
2055 dir_ace = NULL;
2056 } else {
2058 * Check if we have SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ entries in
2059 * the file ACL. If we don't have them, check if any SMB_ACL_USER/SMB_ACL_GROUP
2060 * entries can be converted to *_OBJ. Don't do this for the default
2061 * ACL, we will create them separately for this if needed inside
2062 * ensure_canon_entry_valid_on_set().
2064 if (file_ace) {
2065 check_owning_objs(file_ace, pfile_owner_sid, pfile_grp_sid);
2069 *ppfile_ace = file_ace;
2070 *ppdir_ace = dir_ace;
2072 return True;
2075 /****************************************************************************
2076 ASCII art time again... JRA :-).
2078 We have 4 cases to process when moving from an NT ACL to a POSIX ACL. Firstly,
2079 we insist the ACL is in canonical form (ie. all DENY entries preceede ALLOW
2080 entries). Secondly, the merge code has ensured that all duplicate SID entries for
2081 allow or deny have been merged, so the same SID can only appear once in the deny
2082 list or once in the allow list.
2084 We then process as follows :
2086 ---------------------------------------------------------------------------
2087 First pass - look for a Everyone DENY entry.
2089 If it is deny all (rwx) trunate the list at this point.
2090 Else, walk the list from this point and use the deny permissions of this
2091 entry as a mask on all following allow entries. Finally, delete
2092 the Everyone DENY entry (we have applied it to everything possible).
2094 In addition, in this pass we remove any DENY entries that have
2095 no permissions (ie. they are a DENY nothing).
2096 ---------------------------------------------------------------------------
2097 Second pass - only deal with deny user entries.
2099 DENY user1 (perms XXX)
2101 new_perms = 0
2102 for all following allow group entries where user1 is in group
2103 new_perms |= group_perms;
2105 user1 entry perms = new_perms & ~ XXX;
2107 Convert the deny entry to an allow entry with the new perms and
2108 push to the end of the list. Note if the user was in no groups
2109 this maps to a specific allow nothing entry for this user.
2111 The common case from the NT ACL choser (userX deny all) is
2112 optimised so we don't do the group lookup - we just map to
2113 an allow nothing entry.
2115 What we're doing here is inferring the allow permissions the
2116 person setting the ACE on user1 wanted by looking at the allow
2117 permissions on the groups the user is currently in. This will
2118 be a snapshot, depending on group membership but is the best
2119 we can do and has the advantage of failing closed rather than
2120 open.
2121 ---------------------------------------------------------------------------
2122 Third pass - only deal with deny group entries.
2124 DENY group1 (perms XXX)
2126 for all following allow user entries where user is in group1
2127 user entry perms = user entry perms & ~ XXX;
2129 If there is a group Everyone allow entry with permissions YYY,
2130 convert the group1 entry to an allow entry and modify its
2131 permissions to be :
2133 new_perms = YYY & ~ XXX
2135 and push to the end of the list.
2137 If there is no group Everyone allow entry then convert the
2138 group1 entry to a allow nothing entry and push to the end of the list.
2140 Note that the common case from the NT ACL choser (groupX deny all)
2141 cannot be optimised here as we need to modify user entries who are
2142 in the group to change them to a deny all also.
2144 What we're doing here is modifying the allow permissions of
2145 user entries (which are more specific in POSIX ACLs) to mask
2146 out the explicit deny set on the group they are in. This will
2147 be a snapshot depending on current group membership but is the
2148 best we can do and has the advantage of failing closed rather
2149 than open.
2150 ---------------------------------------------------------------------------
2151 Fourth pass - cope with cumulative permissions.
2153 for all allow user entries, if there exists an allow group entry with
2154 more permissive permissions, and the user is in that group, rewrite the
2155 allow user permissions to contain both sets of permissions.
2157 Currently the code for this is #ifdef'ed out as these semantics make
2158 no sense to me. JRA.
2159 ---------------------------------------------------------------------------
2161 Note we *MUST* do the deny user pass first as this will convert deny user
2162 entries into allow user entries which can then be processed by the deny
2163 group pass.
2165 The above algorithm took a *lot* of thinking about - hence this
2166 explaination :-). JRA.
2167 ****************************************************************************/
2169 /****************************************************************************
2170 Process a canon_ace list entries. This is very complex code. We need
2171 to go through and remove the "deny" permissions from any allow entry that matches
2172 the id of this entry. We have already refused any NT ACL that wasn't in correct
2173 order (DENY followed by ALLOW). If any allow entry ends up with zero permissions,
2174 we just remove it (to fail safe). We have already removed any duplicate ace
2175 entries. Treat an "Everyone" DENY_ACE as a special case - use it to mask all
2176 allow entries.
2177 ****************************************************************************/
2179 static void process_deny_list(connection_struct *conn, canon_ace **pp_ace_list )
2181 canon_ace *ace_list = *pp_ace_list;
2182 canon_ace *curr_ace = NULL;
2183 canon_ace *curr_ace_next = NULL;
2185 /* Pass 1 above - look for an Everyone, deny entry. */
2187 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2188 canon_ace *allow_ace_p;
2190 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2192 if (curr_ace->attr != DENY_ACE)
2193 continue;
2195 if (curr_ace->perms == (mode_t)0) {
2197 /* Deny nothing entry - delete. */
2199 DLIST_REMOVE(ace_list, curr_ace);
2200 continue;
2203 if (!dom_sid_equal(&curr_ace->trustee, &global_sid_World))
2204 continue;
2206 /* JRATEST - assert. */
2207 SMB_ASSERT(curr_ace->owner_type == WORLD_ACE);
2209 if (curr_ace->perms == ALL_ACE_PERMS) {
2212 * Optimisation. This is a DENY_ALL to Everyone. Truncate the
2213 * list at this point including this entry.
2216 canon_ace *prev_entry = DLIST_PREV(curr_ace);
2218 free_canon_ace_list( curr_ace );
2219 if (prev_entry)
2220 DLIST_REMOVE(ace_list, prev_entry);
2221 else {
2222 /* We deleted the entire list. */
2223 ace_list = NULL;
2225 break;
2228 for (allow_ace_p = curr_ace->next; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2231 * Only mask off allow entries.
2234 if (allow_ace_p->attr != ALLOW_ACE)
2235 continue;
2237 allow_ace_p->perms &= ~curr_ace->perms;
2241 * Now it's been applied, remove it.
2244 DLIST_REMOVE(ace_list, curr_ace);
2247 /* Pass 2 above - deal with deny user entries. */
2249 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2250 mode_t new_perms = (mode_t)0;
2251 canon_ace *allow_ace_p;
2253 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2255 if (curr_ace->attr != DENY_ACE)
2256 continue;
2258 if (curr_ace->owner_type != UID_ACE)
2259 continue;
2261 if (curr_ace->perms == ALL_ACE_PERMS) {
2264 * Optimisation - this is a deny everything to this user.
2265 * Convert to an allow nothing and push to the end of the list.
2268 curr_ace->attr = ALLOW_ACE;
2269 curr_ace->perms = (mode_t)0;
2270 DLIST_DEMOTE(ace_list, curr_ace);
2271 continue;
2274 for (allow_ace_p = curr_ace->next; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2276 if (allow_ace_p->attr != ALLOW_ACE)
2277 continue;
2279 /* We process GID_ACE and WORLD_ACE entries only. */
2281 if (allow_ace_p->owner_type == UID_ACE)
2282 continue;
2284 if (uid_entry_in_group(conn, curr_ace, allow_ace_p))
2285 new_perms |= allow_ace_p->perms;
2289 * Convert to a allow entry, modify the perms and push to the end
2290 * of the list.
2293 curr_ace->attr = ALLOW_ACE;
2294 curr_ace->perms = (new_perms & ~curr_ace->perms);
2295 DLIST_DEMOTE(ace_list, curr_ace);
2298 /* Pass 3 above - deal with deny group entries. */
2300 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2301 canon_ace *allow_ace_p;
2302 canon_ace *allow_everyone_p = NULL;
2304 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2306 if (curr_ace->attr != DENY_ACE)
2307 continue;
2309 if (curr_ace->owner_type != GID_ACE)
2310 continue;
2312 for (allow_ace_p = curr_ace->next; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2314 if (allow_ace_p->attr != ALLOW_ACE)
2315 continue;
2317 /* Store a pointer to the Everyone allow, if it exists. */
2318 if (allow_ace_p->owner_type == WORLD_ACE)
2319 allow_everyone_p = allow_ace_p;
2321 /* We process UID_ACE entries only. */
2323 if (allow_ace_p->owner_type != UID_ACE)
2324 continue;
2326 /* Mask off the deny group perms. */
2328 if (uid_entry_in_group(conn, allow_ace_p, curr_ace))
2329 allow_ace_p->perms &= ~curr_ace->perms;
2333 * Convert the deny to an allow with the correct perms and
2334 * push to the end of the list.
2337 curr_ace->attr = ALLOW_ACE;
2338 if (allow_everyone_p)
2339 curr_ace->perms = allow_everyone_p->perms & ~curr_ace->perms;
2340 else
2341 curr_ace->perms = (mode_t)0;
2342 DLIST_DEMOTE(ace_list, curr_ace);
2345 /* Doing this fourth pass allows Windows semantics to be layered
2346 * on top of POSIX semantics. I'm not sure if this is desirable.
2347 * For example, in W2K ACLs there is no way to say, "Group X no
2348 * access, user Y full access" if user Y is a member of group X.
2349 * This seems completely broken semantics to me.... JRA.
2352 #if 0
2353 /* Pass 4 above - deal with allow entries. */
2355 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2356 canon_ace *allow_ace_p;
2358 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2360 if (curr_ace->attr != ALLOW_ACE)
2361 continue;
2363 if (curr_ace->owner_type != UID_ACE)
2364 continue;
2366 for (allow_ace_p = ace_list; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2368 if (allow_ace_p->attr != ALLOW_ACE)
2369 continue;
2371 /* We process GID_ACE entries only. */
2373 if (allow_ace_p->owner_type != GID_ACE)
2374 continue;
2376 /* OR in the group perms. */
2378 if (uid_entry_in_group(conn, curr_ace, allow_ace_p))
2379 curr_ace->perms |= allow_ace_p->perms;
2382 #endif
2384 *pp_ace_list = ace_list;
2387 /****************************************************************************
2388 Unpack a struct security_descriptor into two canonical ace lists. We don't depend on this
2389 succeeding.
2390 ****************************************************************************/
2392 static bool unpack_canon_ace(files_struct *fsp,
2393 const SMB_STRUCT_STAT *pst,
2394 struct dom_sid *pfile_owner_sid,
2395 struct dom_sid *pfile_grp_sid,
2396 canon_ace **ppfile_ace,
2397 canon_ace **ppdir_ace,
2398 uint32_t security_info_sent,
2399 const struct security_descriptor *psd)
2401 canon_ace *file_ace = NULL;
2402 canon_ace *dir_ace = NULL;
2403 bool ok;
2405 *ppfile_ace = NULL;
2406 *ppdir_ace = NULL;
2408 if(security_info_sent == 0) {
2409 DEBUG(0,("unpack_canon_ace: no security info sent !\n"));
2410 return False;
2414 * If no DACL then this is a chown only security descriptor.
2417 if(!(security_info_sent & SECINFO_DACL) || !psd->dacl)
2418 return True;
2421 * Now go through the DACL and create the canon_ace lists.
2424 if (!create_canon_ace_lists(fsp, pst, pfile_owner_sid, pfile_grp_sid,
2425 &file_ace, &dir_ace, psd->dacl)) {
2426 return False;
2429 if ((file_ace == NULL) && (dir_ace == NULL)) {
2430 /* W2K traverse DACL set - ignore. */
2431 return True;
2435 * Go through the canon_ace list and merge entries
2436 * belonging to identical users of identical allow or deny type.
2437 * We can do this as all deny entries come first, followed by
2438 * all allow entries (we have mandated this before accepting this acl).
2441 print_canon_ace_list( "file ace - before merge", file_ace);
2442 merge_aces( &file_ace, false);
2444 print_canon_ace_list( "dir ace - before merge", dir_ace);
2445 merge_aces( &dir_ace, true);
2448 * NT ACLs are order dependent. Go through the acl lists and
2449 * process DENY entries by masking the allow entries.
2452 print_canon_ace_list( "file ace - before deny", file_ace);
2453 process_deny_list(fsp->conn, &file_ace);
2455 print_canon_ace_list( "dir ace - before deny", dir_ace);
2456 process_deny_list(fsp->conn, &dir_ace);
2459 * A well formed POSIX file or default ACL has at least 3 entries, a
2460 * SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ
2461 * and optionally a mask entry. Ensure this is the case.
2464 print_canon_ace_list( "file ace - before valid", file_ace);
2466 ok = ensure_canon_entry_valid_on_set(
2467 fsp->conn,
2468 &file_ace,
2469 false,
2470 fsp->conn->params,
2471 fsp->fsp_flags.is_directory,
2472 pfile_owner_sid,
2473 pfile_grp_sid,
2474 pst);
2475 if (!ok) {
2476 free_canon_ace_list(file_ace);
2477 free_canon_ace_list(dir_ace);
2478 return False;
2481 print_canon_ace_list( "dir ace - before valid", dir_ace);
2483 if (dir_ace != NULL) {
2484 ok = ensure_canon_entry_valid_on_set(
2485 fsp->conn,
2486 &dir_ace,
2487 true,
2488 fsp->conn->params,
2489 fsp->fsp_flags.is_directory,
2490 pfile_owner_sid,
2491 pfile_grp_sid,
2492 pst);
2493 if (!ok) {
2494 free_canon_ace_list(file_ace);
2495 free_canon_ace_list(dir_ace);
2496 return False;
2500 print_canon_ace_list( "file ace - return", file_ace);
2501 print_canon_ace_list( "dir ace - return", dir_ace);
2503 *ppfile_ace = file_ace;
2504 *ppdir_ace = dir_ace;
2505 return True;
2509 /******************************************************************************
2510 When returning permissions, try and fit NT display
2511 semantics if possible. Note the the canon_entries here must have been malloced.
2512 The list format should be - first entry = owner, followed by group and other user
2513 entries, last entry = other.
2515 Note that this doesn't exactly match the NT semantics for an ACL. As POSIX entries
2516 are not ordered, and match on the most specific entry rather than walking a list,
2517 then a simple POSIX permission of rw-r--r-- should really map to 5 entries,
2519 Entry 0: owner : deny all except read and write.
2520 Entry 1: owner : allow read and write.
2521 Entry 2: group : deny all except read.
2522 Entry 3: group : allow read.
2523 Entry 4: Everyone : allow read.
2525 But NT cannot display this in their ACL editor !
2526 ********************************************************************************/
2528 static void arrange_posix_perms(const char *filename, canon_ace **pp_list_head)
2530 canon_ace *l_head = *pp_list_head;
2531 canon_ace *owner_ace = NULL;
2532 canon_ace *other_ace = NULL;
2533 canon_ace *ace = NULL;
2535 for (ace = l_head; ace; ace = ace->next) {
2536 if (ace->type == SMB_ACL_USER_OBJ)
2537 owner_ace = ace;
2538 else if (ace->type == SMB_ACL_OTHER) {
2539 /* Last ace - this is "other" */
2540 other_ace = ace;
2544 if (!owner_ace || !other_ace) {
2545 DEBUG(0,("arrange_posix_perms: Invalid POSIX permissions for file %s, missing owner or other.\n",
2546 filename ));
2547 return;
2551 * The POSIX algorithm applies to owner first, and other last,
2552 * so ensure they are arranged in this order.
2555 if (owner_ace) {
2556 DLIST_PROMOTE(l_head, owner_ace);
2559 if (other_ace) {
2560 DLIST_DEMOTE(l_head, other_ace);
2563 /* We have probably changed the head of the list. */
2565 *pp_list_head = l_head;
2568 /****************************************************************************
2569 Create a linked list of canonical ACE entries.
2570 ****************************************************************************/
2572 static canon_ace *canonicalise_acl(struct connection_struct *conn,
2573 const char *fname, SMB_ACL_T posix_acl,
2574 const SMB_STRUCT_STAT *psbuf,
2575 const struct dom_sid *powner, const struct dom_sid *pgroup, struct pai_val *pal, SMB_ACL_TYPE_T the_acl_type)
2577 mode_t acl_mask = (S_IRUSR|S_IWUSR|S_IXUSR);
2578 canon_ace *l_head = NULL;
2579 canon_ace *ace = NULL;
2580 canon_ace *next_ace = NULL;
2581 int entry_id = SMB_ACL_FIRST_ENTRY;
2582 bool is_default_acl = (the_acl_type == SMB_ACL_TYPE_DEFAULT);
2583 SMB_ACL_ENTRY_T entry;
2584 size_t ace_count;
2586 while ( posix_acl && (sys_acl_get_entry(posix_acl, entry_id, &entry) == 1)) {
2587 SMB_ACL_TAG_T tagtype;
2588 SMB_ACL_PERMSET_T permset;
2589 struct dom_sid sid;
2590 struct unixid unix_ug;
2591 enum ace_owner owner_type;
2593 entry_id = SMB_ACL_NEXT_ENTRY;
2595 if (sys_acl_get_tag_type(entry, &tagtype) == -1)
2596 continue;
2598 if (sys_acl_get_permset(entry, &permset) == -1)
2599 continue;
2601 /* Decide which SID to use based on the ACL type. */
2602 switch(tagtype) {
2603 case SMB_ACL_USER_OBJ:
2604 /* Get the SID from the owner. */
2605 sid_copy(&sid, powner);
2606 unix_ug.type = ID_TYPE_UID;
2607 unix_ug.id = psbuf->st_ex_uid;
2608 owner_type = UID_ACE;
2609 break;
2610 case SMB_ACL_USER:
2612 uid_t *puid = (uid_t *)sys_acl_get_qualifier(entry);
2613 if (puid == NULL) {
2614 DEBUG(0,("canonicalise_acl: Failed to get uid.\n"));
2615 continue;
2617 uid_to_sid( &sid, *puid);
2618 unix_ug.type = ID_TYPE_UID;
2619 unix_ug.id = *puid;
2620 owner_type = UID_ACE;
2621 break;
2623 case SMB_ACL_GROUP_OBJ:
2624 /* Get the SID from the owning group. */
2625 sid_copy(&sid, pgroup);
2626 unix_ug.type = ID_TYPE_GID;
2627 unix_ug.id = psbuf->st_ex_gid;
2628 owner_type = GID_ACE;
2629 break;
2630 case SMB_ACL_GROUP:
2632 gid_t *pgid = (gid_t *)sys_acl_get_qualifier(entry);
2633 if (pgid == NULL) {
2634 DEBUG(0,("canonicalise_acl: Failed to get gid.\n"));
2635 continue;
2637 gid_to_sid( &sid, *pgid);
2638 unix_ug.type = ID_TYPE_GID;
2639 unix_ug.id = *pgid;
2640 owner_type = GID_ACE;
2641 break;
2643 case SMB_ACL_MASK:
2644 acl_mask = convert_permset_to_mode_t(permset);
2645 continue; /* Don't count the mask as an entry. */
2646 case SMB_ACL_OTHER:
2647 /* Use the Everyone SID */
2648 sid = global_sid_World;
2649 unix_ug.type = ID_TYPE_NOT_SPECIFIED;
2650 unix_ug.id = -1;
2651 owner_type = WORLD_ACE;
2652 break;
2653 default:
2654 DEBUG(0,("canonicalise_acl: Unknown tagtype %u\n", (unsigned int)tagtype));
2655 continue;
2659 * Add this entry to the list.
2662 if ((ace = talloc(talloc_tos(), canon_ace)) == NULL)
2663 goto fail;
2665 *ace = (canon_ace) {
2666 .type = tagtype,
2667 .perms = convert_permset_to_mode_t(permset),
2668 .attr = ALLOW_ACE,
2669 .trustee = sid,
2670 .unix_ug = unix_ug,
2671 .owner_type = owner_type
2673 ace->ace_flags = get_pai_flags(pal, ace, is_default_acl);
2675 DLIST_ADD(l_head, ace);
2679 * This next call will ensure we have at least a user/group/world set.
2682 if (!ensure_canon_entry_valid_on_get(conn, &l_head,
2683 powner, pgroup,
2684 psbuf))
2685 goto fail;
2688 * Now go through the list, masking the permissions with the
2689 * acl_mask. Ensure all DENY Entries are at the start of the list.
2692 DEBUG(10,("canonicalise_acl: %s ace entries before arrange :\n", is_default_acl ? "Default" : "Access"));
2694 for ( ace_count = 0, ace = l_head; ace; ace = next_ace, ace_count++) {
2695 next_ace = ace->next;
2697 /* Masks are only applied to entries other than USER_OBJ and OTHER. */
2698 if (ace->type != SMB_ACL_OTHER && ace->type != SMB_ACL_USER_OBJ)
2699 ace->perms &= acl_mask;
2701 if (ace->perms == 0) {
2702 DLIST_PROMOTE(l_head, ace);
2705 if( DEBUGLVL( 10 ) ) {
2706 print_canon_ace(ace, ace_count);
2710 arrange_posix_perms(fname,&l_head );
2712 print_canon_ace_list( "canonicalise_acl: ace entries after arrange", l_head );
2714 return l_head;
2716 fail:
2718 free_canon_ace_list(l_head);
2719 return NULL;
2722 /****************************************************************************
2723 Check if the current user group list contains a given group.
2724 ****************************************************************************/
2726 bool current_user_in_group(connection_struct *conn, gid_t gid)
2728 uint32_t i;
2729 const struct security_unix_token *utok = get_current_utok(conn);
2731 for (i = 0; i < utok->ngroups; i++) {
2732 if (utok->groups[i] == gid) {
2733 return True;
2737 return False;
2740 /****************************************************************************
2741 Should we override a deny ? Check 'acl group control' and 'dos filemode'.
2742 ****************************************************************************/
2744 static bool acl_group_override_fsp(files_struct *fsp)
2746 if ((errno != EPERM) && (errno != EACCES)) {
2747 return false;
2750 /* file primary group == user primary or supplementary group */
2751 if (lp_acl_group_control(SNUM(fsp->conn)) &&
2752 current_user_in_group(fsp->conn, fsp->fsp_name->st.st_ex_gid)) {
2753 return true;
2756 /* user has writeable permission */
2757 if (lp_dos_filemode(SNUM(fsp->conn)) && can_write_to_fsp(fsp)) {
2758 return true;
2761 return false;
2764 /****************************************************************************
2765 Attempt to apply an ACL to a file or directory.
2766 ****************************************************************************/
2768 static bool set_canon_ace_list(files_struct *fsp,
2769 canon_ace *the_ace,
2770 bool default_ace,
2771 const SMB_STRUCT_STAT *psbuf,
2772 bool *pacl_set_support)
2774 bool ret = False;
2775 SMB_ACL_T the_acl = sys_acl_init(talloc_tos());
2776 canon_ace *p_ace;
2777 int i;
2778 SMB_ACL_ENTRY_T mask_entry;
2779 bool got_mask_entry = False;
2780 SMB_ACL_PERMSET_T mask_permset;
2781 SMB_ACL_TYPE_T the_acl_type = (default_ace ? SMB_ACL_TYPE_DEFAULT : SMB_ACL_TYPE_ACCESS);
2782 bool needs_mask = False;
2783 int sret;
2785 /* Use the psbuf that was passed in. */
2786 if (psbuf != &fsp->fsp_name->st) {
2787 fsp->fsp_name->st = *psbuf;
2790 #if defined(POSIX_ACL_NEEDS_MASK)
2791 /* HP-UX always wants to have a mask (called "class" there). */
2792 needs_mask = True;
2793 #endif
2795 if (the_acl == NULL) {
2796 DEBUG(0, ("sys_acl_init failed to allocate an ACL\n"));
2797 return false;
2800 if( DEBUGLVL( 10 )) {
2801 dbgtext("set_canon_ace_list: setting ACL:\n");
2802 for (i = 0, p_ace = the_ace; p_ace; p_ace = p_ace->next, i++ ) {
2803 print_canon_ace( p_ace, i);
2807 for (i = 0, p_ace = the_ace; p_ace; p_ace = p_ace->next, i++ ) {
2808 SMB_ACL_ENTRY_T the_entry;
2809 SMB_ACL_PERMSET_T the_permset;
2812 * ACLs only "need" an ACL_MASK entry if there are any named user or
2813 * named group entries. But if there is an ACL_MASK entry, it applies
2814 * to ACL_USER, ACL_GROUP, and ACL_GROUP_OBJ entries. Set the mask
2815 * so that it doesn't deny (i.e., mask off) any permissions.
2818 if (p_ace->type == SMB_ACL_USER || p_ace->type == SMB_ACL_GROUP) {
2819 needs_mask = True;
2823 * Get the entry for this ACE.
2826 if (sys_acl_create_entry(&the_acl, &the_entry) == -1) {
2827 DEBUG(0,("set_canon_ace_list: Failed to create entry %d. (%s)\n",
2828 i, strerror(errno) ));
2829 goto fail;
2832 if (p_ace->type == SMB_ACL_MASK) {
2833 mask_entry = the_entry;
2834 got_mask_entry = True;
2838 * Ok - we now know the ACL calls should be working, don't
2839 * allow fallback to chmod.
2842 *pacl_set_support = True;
2845 * Initialise the entry from the canon_ace.
2849 * First tell the entry what type of ACE this is.
2852 if (sys_acl_set_tag_type(the_entry, p_ace->type) == -1) {
2853 DEBUG(0,("set_canon_ace_list: Failed to set tag type on entry %d. (%s)\n",
2854 i, strerror(errno) ));
2855 goto fail;
2859 * Only set the qualifier (user or group id) if the entry is a user
2860 * or group id ACE.
2863 if ((p_ace->type == SMB_ACL_USER) || (p_ace->type == SMB_ACL_GROUP)) {
2864 if (sys_acl_set_qualifier(the_entry,(void *)&p_ace->unix_ug.id) == -1) {
2865 DEBUG(0,("set_canon_ace_list: Failed to set qualifier on entry %d. (%s)\n",
2866 i, strerror(errno) ));
2867 goto fail;
2872 * Convert the mode_t perms in the canon_ace to a POSIX permset.
2875 if (sys_acl_get_permset(the_entry, &the_permset) == -1) {
2876 DEBUG(0,("set_canon_ace_list: Failed to get permset on entry %d. (%s)\n",
2877 i, strerror(errno) ));
2878 goto fail;
2881 if (map_acl_perms_to_permset(p_ace->perms, &the_permset) == -1) {
2882 DEBUG(0,("set_canon_ace_list: Failed to create permset for mode (%u) on entry %d. (%s)\n",
2883 (unsigned int)p_ace->perms, i, strerror(errno) ));
2884 goto fail;
2888 * ..and apply them to the entry.
2891 if (sys_acl_set_permset(the_entry, the_permset) == -1) {
2892 DEBUG(0,("set_canon_ace_list: Failed to add permset on entry %d. (%s)\n",
2893 i, strerror(errno) ));
2894 goto fail;
2897 if( DEBUGLVL( 10 ))
2898 print_canon_ace( p_ace, i);
2902 if (needs_mask && !got_mask_entry) {
2903 if (sys_acl_create_entry(&the_acl, &mask_entry) == -1) {
2904 DEBUG(0,("set_canon_ace_list: Failed to create mask entry. (%s)\n", strerror(errno) ));
2905 goto fail;
2908 if (sys_acl_set_tag_type(mask_entry, SMB_ACL_MASK) == -1) {
2909 DEBUG(0,("set_canon_ace_list: Failed to set tag type on mask entry. (%s)\n",strerror(errno) ));
2910 goto fail;
2913 if (sys_acl_get_permset(mask_entry, &mask_permset) == -1) {
2914 DEBUG(0,("set_canon_ace_list: Failed to get mask permset. (%s)\n", strerror(errno) ));
2915 goto fail;
2918 if (map_acl_perms_to_permset(S_IRUSR|S_IWUSR|S_IXUSR, &mask_permset) == -1) {
2919 DEBUG(0,("set_canon_ace_list: Failed to create mask permset. (%s)\n", strerror(errno) ));
2920 goto fail;
2923 if (sys_acl_set_permset(mask_entry, mask_permset) == -1) {
2924 DEBUG(0,("set_canon_ace_list: Failed to add mask permset. (%s)\n", strerror(errno) ));
2925 goto fail;
2930 * Finally apply it to the file or directory.
2932 sret = SMB_VFS_SYS_ACL_SET_FD(fsp, the_acl_type, the_acl);
2933 if (sret == -1) {
2935 * Some systems allow all the above calls and only fail with no ACL support
2936 * when attempting to apply the acl. HPUX with HFS is an example of this. JRA.
2938 if (no_acl_syscall_error(errno)) {
2939 *pacl_set_support = false;
2942 if (acl_group_override_fsp(fsp)) {
2943 DBG_DEBUG("acl group control on and current user in "
2944 "file [%s] primary group.\n",
2945 fsp_str_dbg(fsp));
2947 become_root();
2948 sret = SMB_VFS_SYS_ACL_SET_FD(fsp,
2949 the_acl_type,
2950 the_acl);
2951 unbecome_root();
2952 if (sret == 0) {
2953 ret = true;
2957 if (ret == false) {
2958 DBG_WARNING("sys_acl_set_file on file [%s]: (%s)\n",
2959 fsp_str_dbg(fsp), strerror(errno));
2960 goto fail;
2964 ret = True;
2966 fail:
2968 if (the_acl != NULL) {
2969 TALLOC_FREE(the_acl);
2972 return ret;
2975 /****************************************************************************
2977 ****************************************************************************/
2979 SMB_ACL_T free_empty_sys_acl(connection_struct *conn, SMB_ACL_T the_acl)
2981 SMB_ACL_ENTRY_T entry;
2983 if (!the_acl)
2984 return NULL;
2985 if (sys_acl_get_entry(the_acl, SMB_ACL_FIRST_ENTRY, &entry) != 1) {
2986 TALLOC_FREE(the_acl);
2987 return NULL;
2989 return the_acl;
2992 /****************************************************************************
2993 Convert a canon_ace to a generic 3 element permission - if possible.
2994 ****************************************************************************/
2996 #define MAP_PERM(p,mask,result) (((p) & (mask)) ? (result) : 0 )
2998 static bool convert_canon_ace_to_posix_perms( files_struct *fsp, canon_ace *file_ace_list, mode_t *posix_perms)
3000 size_t ace_count = count_canon_ace_list(file_ace_list);
3001 canon_ace *ace_p;
3002 canon_ace *owner_ace = NULL;
3003 canon_ace *group_ace = NULL;
3004 canon_ace *other_ace = NULL;
3006 if (ace_count > 5) {
3007 DEBUG(3,("convert_canon_ace_to_posix_perms: Too many ACE "
3008 "entries for file %s to convert to posix perms.\n",
3009 fsp_str_dbg(fsp)));
3010 return False;
3013 for (ace_p = file_ace_list; ace_p; ace_p = ace_p->next) {
3014 if (ace_p->owner_type == UID_ACE)
3015 owner_ace = ace_p;
3016 else if (ace_p->owner_type == GID_ACE)
3017 group_ace = ace_p;
3018 else if (ace_p->owner_type == WORLD_ACE)
3019 other_ace = ace_p;
3022 if (!owner_ace || !group_ace || !other_ace) {
3023 DEBUG(3,("convert_canon_ace_to_posix_perms: Can't get "
3024 "standard entries for file %s.\n", fsp_str_dbg(fsp)));
3025 return False;
3029 * Ensure all ACE entries are owner, group or other.
3030 * We can't set if there are any other SIDs.
3032 for (ace_p = file_ace_list; ace_p; ace_p = ace_p->next) {
3033 if (ace_p == owner_ace || ace_p == group_ace ||
3034 ace_p == other_ace) {
3035 continue;
3037 if (ace_p->owner_type == UID_ACE) {
3038 if (ace_p->unix_ug.id != owner_ace->unix_ug.id) {
3039 DEBUG(3,("Invalid uid %u in ACE for file %s.\n",
3040 (unsigned int)ace_p->unix_ug.id,
3041 fsp_str_dbg(fsp)));
3042 return false;
3044 } else if (ace_p->owner_type == GID_ACE) {
3045 if (ace_p->unix_ug.id != group_ace->unix_ug.id) {
3046 DEBUG(3,("Invalid gid %u in ACE for file %s.\n",
3047 (unsigned int)ace_p->unix_ug.id,
3048 fsp_str_dbg(fsp)));
3049 return false;
3051 } else {
3053 * There should be no duplicate WORLD_ACE entries.
3056 DEBUG(3,("Invalid type %u, uid %u in "
3057 "ACE for file %s.\n",
3058 (unsigned int)ace_p->owner_type,
3059 (unsigned int)ace_p->unix_ug.id,
3060 fsp_str_dbg(fsp)));
3061 return false;
3065 *posix_perms = (mode_t)0;
3067 *posix_perms |= owner_ace->perms;
3068 *posix_perms |= MAP_PERM(group_ace->perms, S_IRUSR, S_IRGRP);
3069 *posix_perms |= MAP_PERM(group_ace->perms, S_IWUSR, S_IWGRP);
3070 *posix_perms |= MAP_PERM(group_ace->perms, S_IXUSR, S_IXGRP);
3071 *posix_perms |= MAP_PERM(other_ace->perms, S_IRUSR, S_IROTH);
3072 *posix_perms |= MAP_PERM(other_ace->perms, S_IWUSR, S_IWOTH);
3073 *posix_perms |= MAP_PERM(other_ace->perms, S_IXUSR, S_IXOTH);
3075 /* The owner must have at least read access. */
3077 *posix_perms |= S_IRUSR;
3078 if (fsp->fsp_flags.is_directory)
3079 *posix_perms |= (S_IWUSR|S_IXUSR);
3081 DEBUG(10,("convert_canon_ace_to_posix_perms: converted u=%o,g=%o,w=%o "
3082 "to perm=0%o for file %s.\n", (int)owner_ace->perms,
3083 (int)group_ace->perms, (int)other_ace->perms,
3084 (int)*posix_perms, fsp_str_dbg(fsp)));
3086 return True;
3089 /****************************************************************************
3090 Incoming NT ACLs on a directory can be split into a default POSIX acl (CI|OI|IO) and
3091 a normal POSIX acl. Win2k needs these split acls re-merging into one ACL
3092 with CI|OI set so it is inherited and also applies to the directory.
3093 Based on code from "Jim McDonough" <jmcd@us.ibm.com>.
3094 ****************************************************************************/
3096 static size_t merge_default_aces( struct security_ace *nt_ace_list, size_t num_aces)
3098 size_t i, j;
3100 for (i = 0; i < num_aces; i++) {
3101 for (j = i+1; j < num_aces; j++) {
3102 uint32_t i_flags_ni = (nt_ace_list[i].flags & ~SEC_ACE_FLAG_INHERITED_ACE);
3103 uint32_t j_flags_ni = (nt_ace_list[j].flags & ~SEC_ACE_FLAG_INHERITED_ACE);
3104 bool i_inh = (nt_ace_list[i].flags & SEC_ACE_FLAG_INHERITED_ACE) ? True : False;
3105 bool j_inh = (nt_ace_list[j].flags & SEC_ACE_FLAG_INHERITED_ACE) ? True : False;
3107 /* We know the lower number ACE's are file entries. */
3108 if ((nt_ace_list[i].type == nt_ace_list[j].type) &&
3109 (nt_ace_list[i].size == nt_ace_list[j].size) &&
3110 (nt_ace_list[i].access_mask == nt_ace_list[j].access_mask) &&
3111 dom_sid_equal(&nt_ace_list[i].trustee, &nt_ace_list[j].trustee) &&
3112 (i_inh == j_inh) &&
3113 (i_flags_ni == 0) &&
3114 (j_flags_ni == (SEC_ACE_FLAG_OBJECT_INHERIT|
3115 SEC_ACE_FLAG_CONTAINER_INHERIT|
3116 SEC_ACE_FLAG_INHERIT_ONLY))) {
3118 * W2K wants to have access allowed zero access ACE's
3119 * at the end of the list. If the mask is zero, merge
3120 * the non-inherited ACE onto the inherited ACE.
3123 if (nt_ace_list[i].access_mask == 0) {
3124 nt_ace_list[j].flags = SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT|
3125 (i_inh ? SEC_ACE_FLAG_INHERITED_ACE : 0);
3126 ARRAY_DEL_ELEMENT(nt_ace_list, i, num_aces);
3128 DEBUG(10,("merge_default_aces: Merging zero access ACE %u onto ACE %u.\n",
3129 (unsigned int)i, (unsigned int)j ));
3130 } else {
3132 * These are identical except for the flags.
3133 * Merge the inherited ACE onto the non-inherited ACE.
3136 nt_ace_list[i].flags = SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT|
3137 (i_inh ? SEC_ACE_FLAG_INHERITED_ACE : 0);
3138 ARRAY_DEL_ELEMENT(nt_ace_list, j, num_aces);
3140 DEBUG(10,("merge_default_aces: Merging ACE %u onto ACE %u.\n",
3141 (unsigned int)j, (unsigned int)i ));
3143 num_aces--;
3144 break;
3149 return num_aces;
3153 /****************************************************************************
3154 Reply to query a security descriptor from an fsp. If it succeeds it allocates
3155 the space for the return elements and returns the size needed to return the
3156 security descriptor. This should be the only external function needed for
3157 the UNIX style get ACL.
3158 ****************************************************************************/
3160 static NTSTATUS posix_get_nt_acl_common(struct connection_struct *conn,
3161 const char *name,
3162 const SMB_STRUCT_STAT *sbuf,
3163 struct pai_val *pal,
3164 SMB_ACL_T posix_acl,
3165 SMB_ACL_T def_acl,
3166 uint32_t security_info,
3167 TALLOC_CTX *mem_ctx,
3168 struct security_descriptor **ppdesc)
3170 struct dom_sid owner_sid;
3171 struct dom_sid group_sid;
3172 size_t sd_size = 0;
3173 struct security_acl *psa = NULL;
3174 size_t num_acls = 0;
3175 size_t num_def_acls = 0;
3176 size_t num_aces = 0;
3177 canon_ace *file_ace = NULL;
3178 canon_ace *dir_ace = NULL;
3179 struct security_ace *nt_ace_list = NULL;
3180 struct security_descriptor *psd = NULL;
3183 * Get the owner, group and world SIDs.
3186 create_file_sids(sbuf, &owner_sid, &group_sid);
3188 if (security_info & SECINFO_DACL) {
3191 * In the optimum case Creator Owner and Creator Group would be used for
3192 * the ACL_USER_OBJ and ACL_GROUP_OBJ entries, respectively, but this
3193 * would lead to usability problems under Windows: The Creator entries
3194 * are only available in browse lists of directories and not for files;
3195 * additionally the identity of the owning group couldn't be determined.
3196 * We therefore use those identities only for Default ACLs.
3199 /* Create the canon_ace lists. */
3200 file_ace = canonicalise_acl(conn, name, posix_acl, sbuf,
3201 &owner_sid, &group_sid, pal,
3202 SMB_ACL_TYPE_ACCESS);
3204 /* We must have *some* ACLS. */
3206 if (count_canon_ace_list(file_ace) == 0) {
3207 DEBUG(0,("get_nt_acl : No ACLs on file (%s) !\n", name));
3208 goto done;
3211 if (S_ISDIR(sbuf->st_ex_mode) && def_acl) {
3212 dir_ace = canonicalise_acl(conn, name, def_acl,
3213 sbuf,
3214 &global_sid_Creator_Owner,
3215 &global_sid_Creator_Group,
3216 pal, SMB_ACL_TYPE_DEFAULT);
3220 * Create the NT ACE list from the canonical ace lists.
3224 canon_ace *ace;
3225 enum security_ace_type nt_acl_type;
3227 num_acls = count_canon_ace_list(file_ace);
3228 num_def_acls = count_canon_ace_list(dir_ace);
3230 nt_ace_list = talloc_zero_array(
3231 talloc_tos(), struct security_ace,
3232 num_acls + num_def_acls);
3234 if (nt_ace_list == NULL) {
3235 DEBUG(0,("get_nt_acl: Unable to malloc space for nt_ace_list.\n"));
3236 goto done;
3240 * Create the NT ACE list from the canonical ace lists.
3243 for (ace = file_ace; ace != NULL; ace = ace->next) {
3244 uint32_t acc = map_canon_ace_perms(SNUM(conn),
3245 &nt_acl_type,
3246 ace->perms,
3247 S_ISDIR(sbuf->st_ex_mode));
3248 init_sec_ace(&nt_ace_list[num_aces++],
3249 &ace->trustee,
3250 nt_acl_type,
3251 acc,
3252 ace->ace_flags);
3255 for (ace = dir_ace; ace != NULL; ace = ace->next) {
3256 uint32_t acc = map_canon_ace_perms(SNUM(conn),
3257 &nt_acl_type,
3258 ace->perms,
3259 S_ISDIR(sbuf->st_ex_mode));
3260 init_sec_ace(&nt_ace_list[num_aces++],
3261 &ace->trustee,
3262 nt_acl_type,
3263 acc,
3264 ace->ace_flags |
3265 SEC_ACE_FLAG_OBJECT_INHERIT|
3266 SEC_ACE_FLAG_CONTAINER_INHERIT|
3267 SEC_ACE_FLAG_INHERIT_ONLY);
3271 * Merge POSIX default ACLs and normal ACLs into one NT ACE.
3272 * Win2K needs this to get the inheritance correct when replacing ACLs
3273 * on a directory tree. Based on work by Jim @ IBM.
3276 num_aces = merge_default_aces(nt_ace_list, num_aces);
3279 if (num_aces) {
3280 if((psa = make_sec_acl( talloc_tos(), NT4_ACL_REVISION, num_aces, nt_ace_list)) == NULL) {
3281 DEBUG(0,("get_nt_acl: Unable to malloc space for acl.\n"));
3282 goto done;
3285 } /* security_info & SECINFO_DACL */
3287 psd = make_standard_sec_desc(mem_ctx,
3288 (security_info & SECINFO_OWNER) ? &owner_sid : NULL,
3289 (security_info & SECINFO_GROUP) ? &group_sid : NULL,
3290 psa,
3291 &sd_size);
3293 if(!psd) {
3294 DEBUG(0,("get_nt_acl: Unable to malloc space for security descriptor.\n"));
3295 sd_size = 0;
3296 goto done;
3300 * Windows 2000: The DACL_PROTECTED flag in the security
3301 * descriptor marks the ACL as non-inheriting, i.e., no
3302 * ACEs from higher level directories propagate to this
3303 * ACL. In the POSIX ACL model permissions are only
3304 * inherited at file create time, so ACLs never contain
3305 * any ACEs that are inherited dynamically. The DACL_PROTECTED
3306 * flag doesn't seem to bother Windows NT.
3307 * Always set this if map acl inherit is turned off.
3309 if (pal == NULL || !lp_map_acl_inherit(SNUM(conn))) {
3310 psd->type |= SEC_DESC_DACL_PROTECTED;
3311 } else {
3312 psd->type |= pal->sd_type;
3315 if (psd->dacl) {
3316 dacl_sort_into_canonical_order(psd->dacl->aces, (unsigned int)psd->dacl->num_aces);
3319 *ppdesc = psd;
3321 done:
3323 if (posix_acl) {
3324 TALLOC_FREE(posix_acl);
3326 if (def_acl) {
3327 TALLOC_FREE(def_acl);
3329 free_canon_ace_list(file_ace);
3330 free_canon_ace_list(dir_ace);
3331 free_inherited_info(pal);
3332 TALLOC_FREE(nt_ace_list);
3334 return NT_STATUS_OK;
3337 NTSTATUS posix_fget_nt_acl(struct files_struct *fsp, uint32_t security_info,
3338 TALLOC_CTX *mem_ctx,
3339 struct security_descriptor **ppdesc)
3341 SMB_STRUCT_STAT sbuf;
3342 SMB_ACL_T posix_acl = NULL;
3343 SMB_ACL_T def_acl = NULL;
3344 struct pai_val *pal;
3345 TALLOC_CTX *frame = talloc_stackframe();
3346 NTSTATUS status;
3348 *ppdesc = NULL;
3350 DEBUG(10,("posix_fget_nt_acl: called for file %s\n",
3351 fsp_str_dbg(fsp)));
3353 /* Get the stat struct for the owner info. */
3354 if(SMB_VFS_FSTAT(fsp, &sbuf) != 0) {
3355 TALLOC_FREE(frame);
3356 return map_nt_error_from_unix(errno);
3359 /* Get the ACL from the fd. */
3360 posix_acl = SMB_VFS_SYS_ACL_GET_FD(fsp,
3361 SMB_ACL_TYPE_ACCESS,
3362 frame);
3364 /* If it's a directory get the default POSIX ACL. */
3365 if(fsp->fsp_flags.is_directory) {
3366 def_acl = SMB_VFS_SYS_ACL_GET_FD(fsp,
3367 SMB_ACL_TYPE_DEFAULT,
3368 frame);
3369 def_acl = free_empty_sys_acl(fsp->conn, def_acl);
3372 pal = fload_inherited_info(fsp);
3374 status = posix_get_nt_acl_common(fsp->conn, fsp->fsp_name->base_name,
3375 &sbuf, pal, posix_acl, def_acl,
3376 security_info, mem_ctx, ppdesc);
3377 TALLOC_FREE(frame);
3378 return status;
3381 /****************************************************************************
3382 Try to chown a file. We will be able to chown it under the following conditions.
3384 1) If we have root privileges, then it will just work.
3385 2) If we have SeRestorePrivilege we can change the user + group to any other user.
3386 3) If we have SeTakeOwnershipPrivilege we can change the user to the current user.
3387 4) If we have write permission to the file and dos_filemodes is set
3388 then allow chown to the currently authenticated user.
3389 ****************************************************************************/
3391 static NTSTATUS try_chown(files_struct *fsp, uid_t uid, gid_t gid)
3393 NTSTATUS status;
3394 int ret;
3396 if(!CAN_WRITE(fsp->conn)) {
3397 return NT_STATUS_MEDIA_WRITE_PROTECTED;
3400 /* Case (1). */
3401 ret = SMB_VFS_FCHOWN(fsp, uid, gid);
3402 if (ret == 0) {
3403 return NT_STATUS_OK;
3406 /* Case (2) / (3) */
3407 if (lp_enable_privileges()) {
3408 bool has_take_ownership_priv = security_token_has_privilege(
3409 get_current_nttok(fsp->conn),
3410 SEC_PRIV_TAKE_OWNERSHIP);
3411 bool has_restore_priv = security_token_has_privilege(
3412 get_current_nttok(fsp->conn),
3413 SEC_PRIV_RESTORE);
3415 if (has_restore_priv) {
3416 ; /* Case (2) */
3417 } else if (has_take_ownership_priv) {
3418 /* Case (3) */
3419 if (uid == get_current_uid(fsp->conn)) {
3420 gid = (gid_t)-1;
3421 } else {
3422 has_take_ownership_priv = false;
3426 if (has_take_ownership_priv || has_restore_priv) {
3427 status = NT_STATUS_OK;
3428 become_root();
3429 ret = SMB_VFS_FCHOWN(fsp, uid, gid);
3430 if (ret != 0) {
3431 status = map_nt_error_from_unix(errno);
3433 unbecome_root();
3434 return status;
3438 /* Case (4). */
3439 /* If "dos filemode" isn't set, we're done. */
3440 if (!lp_dos_filemode(SNUM(fsp->conn))) {
3441 return NT_STATUS_ACCESS_DENIED;
3444 * If we have a writable handle, obviously we
3445 * can write to the file.
3447 if (!fsp->fsp_flags.can_write) {
3449 * If we don't have a writable handle, we
3450 * need to read the ACL on the file to
3451 * see if we can write to it.
3453 if (!can_write_to_fsp(fsp)) {
3454 return NT_STATUS_ACCESS_DENIED;
3458 /* only allow chown to the current user. This is more secure,
3459 and also copes with the case where the SID in a take ownership ACL is
3460 a local SID on the users workstation
3462 if (uid != get_current_uid(fsp->conn)) {
3463 return NT_STATUS_INVALID_OWNER;
3466 status = NT_STATUS_OK;
3467 become_root();
3468 /* Keep the current file gid the same. */
3469 ret = SMB_VFS_FCHOWN(fsp, uid, (gid_t)-1);
3470 if (ret != 0) {
3471 status = map_nt_error_from_unix(errno);
3473 unbecome_root();
3475 return status;
3479 * Check whether a chown is needed and if so, attempt the chown
3480 * A returned error indicates that the chown failed.
3481 * NT_STATUS_OK with did_chown == false indicates that the chown was skipped.
3482 * NT_STATUS_OK with did_chown == true indicates that the chown succeeded
3484 NTSTATUS chown_if_needed(files_struct *fsp, uint32_t security_info_sent,
3485 const struct security_descriptor *psd,
3486 bool *did_chown)
3488 NTSTATUS status;
3489 uid_t uid = (uid_t)-1;
3490 gid_t gid = (gid_t)-1;
3492 status = unpack_nt_owners(fsp->conn, &uid, &gid, security_info_sent, psd);
3493 if (!NT_STATUS_IS_OK(status)) {
3494 return status;
3497 if (((uid == (uid_t)-1) || (fsp->fsp_name->st.st_ex_uid == uid)) &&
3498 ((gid == (gid_t)-1) || (fsp->fsp_name->st.st_ex_gid == gid))) {
3500 * Skip chown
3502 *did_chown = false;
3503 return NT_STATUS_OK;
3506 DBG_NOTICE("chown %s. uid = %u, gid = %u.\n",
3507 fsp_str_dbg(fsp), (unsigned int) uid, (unsigned int)gid);
3509 status = try_chown(fsp, uid, gid);
3510 if (!NT_STATUS_IS_OK(status)) {
3511 DBG_INFO("chown %s, %u, %u failed. Error = %s.\n",
3512 fsp_str_dbg(fsp), (unsigned int) uid,
3513 (unsigned int)gid, nt_errstr(status));
3514 return status;
3518 * Recheck the current state of the file, which may have changed.
3519 * (owner and suid/sgid bits, for instance)
3522 status = vfs_stat_fsp(fsp);
3523 if (!NT_STATUS_IS_OK(status)) {
3524 return status;
3527 *did_chown = true;
3528 return NT_STATUS_OK;
3531 /****************************************************************************
3532 Reply to set a security descriptor on an fsp. security_info_sent is the
3533 description of the following NT ACL.
3534 This should be the only external function needed for the UNIX style set ACL.
3535 We make a copy of psd_orig as internal functions modify the elements inside
3536 it, even though it's a const pointer.
3537 ****************************************************************************/
3539 NTSTATUS set_nt_acl(files_struct *fsp, uint32_t security_info_sent, const struct security_descriptor *psd_orig)
3541 connection_struct *conn = fsp->conn;
3542 struct dom_sid file_owner_sid;
3543 struct dom_sid file_grp_sid;
3544 canon_ace *file_ace_list = NULL;
3545 canon_ace *dir_ace_list = NULL;
3546 bool acl_perms = False;
3547 mode_t orig_mode = (mode_t)0;
3548 NTSTATUS status;
3549 bool set_acl_as_root = false;
3550 bool acl_set_support = false;
3551 bool ret = false;
3552 struct security_descriptor *psd = NULL;
3554 DEBUG(10,("set_nt_acl: called for file %s\n",
3555 fsp_str_dbg(fsp)));
3557 if (!CAN_WRITE(conn)) {
3558 DEBUG(10,("set acl rejected on read-only share\n"));
3559 return NT_STATUS_MEDIA_WRITE_PROTECTED;
3562 if (psd_orig == NULL) {
3563 return NT_STATUS_INVALID_PARAMETER;
3567 * MS NFS mode, here's the deal: the client merely wants to
3568 * modify the mode, but roundtripping get_acl/set/acl would
3569 * add additional POSIX ACEs. So in case we get a request
3570 * containing a MS NFS mode SID, we do nothing here.
3572 if (security_descriptor_with_ms_nfs(psd_orig)) {
3573 return NT_STATUS_OK;
3576 psd = security_descriptor_copy(talloc_tos(), psd_orig);
3577 if (psd == NULL) {
3578 return NT_STATUS_NO_MEMORY;
3582 * Get the current state of the file.
3585 status = vfs_stat_fsp(fsp);
3586 if (!NT_STATUS_IS_OK(status)) {
3587 return status;
3590 /* Save the original element we check against. */
3591 orig_mode = fsp->fsp_name->st.st_ex_mode;
3594 * Unpack the user/group/world id's.
3597 /* POSIX can't cope with missing owner/group. */
3598 if ((security_info_sent & SECINFO_OWNER) && (psd->owner_sid == NULL)) {
3599 security_info_sent &= ~SECINFO_OWNER;
3601 if ((security_info_sent & SECINFO_GROUP) && (psd->group_sid == NULL)) {
3602 security_info_sent &= ~SECINFO_GROUP;
3605 /* If UNIX owner is inherited and Windows isn't, then
3606 * setting the UNIX owner based on Windows owner conflicts
3607 * with the inheritance rule
3609 if (lp_inherit_owner(SNUM(conn)) == INHERIT_OWNER_UNIX_ONLY) {
3610 security_info_sent &= ~SECINFO_OWNER;
3614 * Do we need to chown ? If so this must be done first as the incoming
3615 * CREATOR_OWNER acl will be relative to the *new* owner, not the old.
3616 * Noticed by Simo.
3618 * If we successfully chowned, we know we must be able to set
3619 * the acl, so do it as root (set_acl_as_root).
3621 status = chown_if_needed(fsp, security_info_sent, psd, &set_acl_as_root);
3622 if (!NT_STATUS_IS_OK(status)) {
3623 return status;
3626 create_file_sids(&fsp->fsp_name->st, &file_owner_sid, &file_grp_sid);
3628 if((security_info_sent & SECINFO_DACL) &&
3629 (psd->type & SEC_DESC_DACL_PRESENT) &&
3630 (psd->dacl == NULL)) {
3631 struct security_ace ace[3];
3633 /* We can't have NULL DACL in POSIX.
3634 Use owner/group/Everyone -> full access. */
3636 init_sec_ace(&ace[0],
3637 &file_owner_sid,
3638 SEC_ACE_TYPE_ACCESS_ALLOWED,
3639 GENERIC_ALL_ACCESS,
3641 init_sec_ace(&ace[1],
3642 &file_grp_sid,
3643 SEC_ACE_TYPE_ACCESS_ALLOWED,
3644 GENERIC_ALL_ACCESS,
3646 init_sec_ace(&ace[2],
3647 &global_sid_World,
3648 SEC_ACE_TYPE_ACCESS_ALLOWED,
3649 GENERIC_ALL_ACCESS,
3651 psd->dacl = make_sec_acl(talloc_tos(),
3652 NT4_ACL_REVISION,
3654 ace);
3655 if (psd->dacl == NULL) {
3656 return NT_STATUS_NO_MEMORY;
3658 security_acl_map_generic(psd->dacl, &file_generic_mapping);
3661 acl_perms = unpack_canon_ace(fsp, &fsp->fsp_name->st, &file_owner_sid,
3662 &file_grp_sid, &file_ace_list,
3663 &dir_ace_list, security_info_sent, psd);
3665 /* Ignore W2K traverse DACL set. */
3666 if (!file_ace_list && !dir_ace_list) {
3667 return NT_STATUS_OK;
3670 if (!acl_perms) {
3671 DEBUG(3,("set_nt_acl: cannot set permissions\n"));
3672 free_canon_ace_list(file_ace_list);
3673 free_canon_ace_list(dir_ace_list);
3674 return NT_STATUS_ACCESS_DENIED;
3678 * Only change security if we got a DACL.
3681 if(!(security_info_sent & SECINFO_DACL) || (psd->dacl == NULL)) {
3682 free_canon_ace_list(file_ace_list);
3683 free_canon_ace_list(dir_ace_list);
3684 return NT_STATUS_OK;
3688 * Try using the POSIX ACL set first. Fall back to chmod if
3689 * we have no ACL support on this filesystem.
3692 if (acl_perms && file_ace_list) {
3693 if (set_acl_as_root) {
3694 become_root();
3696 ret = set_canon_ace_list(fsp, file_ace_list, false,
3697 &fsp->fsp_name->st, &acl_set_support);
3698 if (set_acl_as_root) {
3699 unbecome_root();
3701 if (acl_set_support && ret == false) {
3702 DEBUG(3,("set_nt_acl: failed to set file acl on file "
3703 "%s (%s).\n", fsp_str_dbg(fsp),
3704 strerror(errno)));
3705 free_canon_ace_list(file_ace_list);
3706 free_canon_ace_list(dir_ace_list);
3707 return map_nt_error_from_unix(errno);
3711 if (acl_perms && acl_set_support && fsp->fsp_flags.is_directory) {
3712 if (dir_ace_list) {
3713 if (set_acl_as_root) {
3714 become_root();
3716 ret = set_canon_ace_list(fsp, dir_ace_list, true,
3717 &fsp->fsp_name->st,
3718 &acl_set_support);
3719 if (set_acl_as_root) {
3720 unbecome_root();
3722 if (ret == false) {
3723 DEBUG(3,("set_nt_acl: failed to set default "
3724 "acl on directory %s (%s).\n",
3725 fsp_str_dbg(fsp), strerror(errno)));
3726 free_canon_ace_list(file_ace_list);
3727 free_canon_ace_list(dir_ace_list);
3728 return map_nt_error_from_unix(errno);
3730 } else {
3731 int sret = -1;
3734 * No default ACL - delete one if it exists.
3737 if (set_acl_as_root) {
3738 become_root();
3740 sret = SMB_VFS_SYS_ACL_DELETE_DEF_FD(fsp);
3741 if (set_acl_as_root) {
3742 unbecome_root();
3744 if (sret == -1) {
3745 if (acl_group_override_fsp(fsp)) {
3746 DEBUG(5,("set_nt_acl: acl group "
3747 "control on and current user "
3748 "in file %s primary group. "
3749 "Override delete_def_acl\n",
3750 fsp_str_dbg(fsp)));
3752 become_root();
3753 sret =
3754 SMB_VFS_SYS_ACL_DELETE_DEF_FD(fsp);
3755 unbecome_root();
3758 if (sret == -1) {
3759 DBG_NOTICE("sys_acl_delete_def_fd for "
3760 "directory %s failed (%s)\n",
3761 fsp_str_dbg(fsp),
3762 strerror(errno));
3763 free_canon_ace_list(file_ace_list);
3764 free_canon_ace_list(dir_ace_list);
3765 return map_nt_error_from_unix(errno);
3771 if (acl_set_support) {
3772 if (set_acl_as_root) {
3773 become_root();
3775 store_inheritance_attributes(fsp,
3776 file_ace_list,
3777 dir_ace_list,
3778 psd->type);
3779 if (set_acl_as_root) {
3780 unbecome_root();
3785 * If we cannot set using POSIX ACLs we fall back to checking if we need to chmod.
3788 if(!acl_set_support && acl_perms) {
3789 mode_t posix_perms;
3791 if (!convert_canon_ace_to_posix_perms( fsp, file_ace_list, &posix_perms)) {
3792 free_canon_ace_list(file_ace_list);
3793 free_canon_ace_list(dir_ace_list);
3794 DEBUG(3,("set_nt_acl: failed to convert file acl to "
3795 "posix permissions for file %s.\n",
3796 fsp_str_dbg(fsp)));
3797 return NT_STATUS_ACCESS_DENIED;
3800 if (orig_mode != posix_perms) {
3801 int sret = -1;
3803 DEBUG(3,("set_nt_acl: chmod %s. perms = 0%o.\n",
3804 fsp_str_dbg(fsp), (unsigned int)posix_perms));
3806 if (set_acl_as_root) {
3807 become_root();
3809 sret = SMB_VFS_FCHMOD(fsp, posix_perms);
3810 if (set_acl_as_root) {
3811 unbecome_root();
3813 if(sret == -1) {
3814 if (acl_group_override_fsp(fsp)) {
3815 DEBUG(5,("set_nt_acl: acl group "
3816 "control on and current user "
3817 "in file %s primary group. "
3818 "Override chmod\n",
3819 fsp_str_dbg(fsp)));
3821 become_root();
3822 sret = SMB_VFS_FCHMOD(fsp, posix_perms);
3823 unbecome_root();
3826 if (sret == -1) {
3827 DEBUG(3,("set_nt_acl: chmod %s, 0%o "
3828 "failed. Error = %s.\n",
3829 fsp_str_dbg(fsp),
3830 (unsigned int)posix_perms,
3831 strerror(errno)));
3832 free_canon_ace_list(file_ace_list);
3833 free_canon_ace_list(dir_ace_list);
3834 return map_nt_error_from_unix(errno);
3840 free_canon_ace_list(file_ace_list);
3841 free_canon_ace_list(dir_ace_list);
3843 /* Ensure the stat struct in the fsp is correct. */
3844 status = vfs_stat_fsp(fsp);
3846 return NT_STATUS_OK;
3849 /****************************************************************************
3850 Get the actual group bits stored on a file with an ACL. Has no effect if
3851 the file has no ACL. Needed in dosmode code where the stat() will return
3852 the mask bits, not the real group bits, for a file with an ACL.
3853 ****************************************************************************/
3855 int get_acl_group_bits(connection_struct *conn,
3856 struct files_struct *fsp,
3857 mode_t *mode )
3859 int entry_id = SMB_ACL_FIRST_ENTRY;
3860 SMB_ACL_ENTRY_T entry;
3861 SMB_ACL_T posix_acl;
3862 int result = -1;
3864 posix_acl = SMB_VFS_SYS_ACL_GET_FD(metadata_fsp(fsp),
3865 SMB_ACL_TYPE_ACCESS,
3866 talloc_tos());
3867 if (posix_acl == (SMB_ACL_T)NULL)
3868 return -1;
3870 while (sys_acl_get_entry(posix_acl, entry_id, &entry) == 1) {
3871 SMB_ACL_TAG_T tagtype;
3872 SMB_ACL_PERMSET_T permset;
3874 entry_id = SMB_ACL_NEXT_ENTRY;
3876 if (sys_acl_get_tag_type(entry, &tagtype) ==-1)
3877 break;
3879 if (tagtype == SMB_ACL_GROUP_OBJ) {
3880 if (sys_acl_get_permset(entry, &permset) == -1) {
3881 break;
3882 } else {
3883 *mode &= ~(S_IRGRP|S_IWGRP|S_IXGRP);
3884 *mode |= (sys_acl_get_perm(permset, SMB_ACL_READ) ? S_IRGRP : 0);
3885 *mode |= (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? S_IWGRP : 0);
3886 *mode |= (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? S_IXGRP : 0);
3887 result = 0;
3888 break;
3892 TALLOC_FREE(posix_acl);
3893 return result;
3896 /****************************************************************************
3897 Do a chmod by setting the ACL USER_OBJ, GROUP_OBJ and OTHER bits in an ACL
3898 and set the mask to rwx. Needed to preserve complex ACLs set by NT.
3899 ****************************************************************************/
3901 static int chmod_acl_internals(SMB_ACL_T posix_acl, mode_t mode)
3903 int entry_id = SMB_ACL_FIRST_ENTRY;
3904 SMB_ACL_ENTRY_T entry;
3905 int num_entries = 0;
3907 while ( sys_acl_get_entry(posix_acl, entry_id, &entry) == 1) {
3908 SMB_ACL_TAG_T tagtype;
3909 SMB_ACL_PERMSET_T permset;
3910 mode_t perms;
3912 entry_id = SMB_ACL_NEXT_ENTRY;
3914 if (sys_acl_get_tag_type(entry, &tagtype) == -1)
3915 return -1;
3917 if (sys_acl_get_permset(entry, &permset) == -1)
3918 return -1;
3920 num_entries++;
3922 switch(tagtype) {
3923 case SMB_ACL_USER_OBJ:
3924 perms = unix_perms_to_acl_perms(mode, S_IRUSR, S_IWUSR, S_IXUSR);
3925 break;
3926 case SMB_ACL_GROUP_OBJ:
3927 perms = unix_perms_to_acl_perms(mode, S_IRGRP, S_IWGRP, S_IXGRP);
3928 break;
3929 case SMB_ACL_MASK:
3931 * FIXME: The ACL_MASK entry permissions should really be set to
3932 * the union of the permissions of all ACL_USER,
3933 * ACL_GROUP_OBJ, and ACL_GROUP entries. That's what
3934 * acl_calc_mask() does, but Samba ACLs doesn't provide it.
3936 perms = S_IRUSR|S_IWUSR|S_IXUSR;
3937 break;
3938 case SMB_ACL_OTHER:
3939 perms = unix_perms_to_acl_perms(mode, S_IROTH, S_IWOTH, S_IXOTH);
3940 break;
3941 default:
3942 continue;
3945 if (map_acl_perms_to_permset(perms, &permset) == -1)
3946 return -1;
3948 if (sys_acl_set_permset(entry, permset) == -1)
3949 return -1;
3953 * If this is a simple 3 element ACL or no elements then it's a standard
3954 * UNIX permission set. Just use chmod...
3957 if ((num_entries == 3) || (num_entries == 0))
3958 return -1;
3960 return 0;
3963 /****************************************************************************
3964 Get the access ACL of FROM, do a chmod by setting the ACL USER_OBJ,
3965 GROUP_OBJ and OTHER bits in an ACL and set the mask to rwx. Set the
3966 resulting ACL on TO. Note that name is in UNIX character set.
3967 ****************************************************************************/
3969 static int copy_access_posix_acl(struct files_struct *from,
3970 struct files_struct *to,
3971 mode_t mode)
3973 SMB_ACL_T posix_acl = NULL;
3974 int ret = -1;
3976 posix_acl = SMB_VFS_SYS_ACL_GET_FD(
3977 from, SMB_ACL_TYPE_ACCESS, talloc_tos());
3978 if (posix_acl == NULL) {
3979 return -1;
3982 ret = chmod_acl_internals(posix_acl, mode);
3983 if (ret == -1) {
3984 goto done;
3987 ret = SMB_VFS_SYS_ACL_SET_FD(to, SMB_ACL_TYPE_ACCESS, posix_acl);
3989 done:
3991 TALLOC_FREE(posix_acl);
3992 return ret;
3995 /****************************************************************************
3996 Check for an existing default POSIX ACL on a directory.
3997 ****************************************************************************/
3999 static bool directory_has_default_posix_acl(struct files_struct *dirfsp)
4001 SMB_ACL_T def_acl = SMB_VFS_SYS_ACL_GET_FD(
4002 dirfsp, SMB_ACL_TYPE_DEFAULT, talloc_tos());
4003 bool has_acl = False;
4004 SMB_ACL_ENTRY_T entry;
4006 if (def_acl != NULL && (sys_acl_get_entry(def_acl, SMB_ACL_FIRST_ENTRY, &entry) == 1)) {
4007 has_acl = True;
4010 if (def_acl) {
4011 TALLOC_FREE(def_acl);
4013 return has_acl;
4016 /****************************************************************************
4017 If the parent directory has no default ACL but it does have an Access ACL,
4018 inherit this Access ACL to file name.
4019 ****************************************************************************/
4021 int inherit_access_posix_acl(connection_struct *conn,
4022 struct files_struct *inherit_from_dirfsp,
4023 const struct smb_filename *smb_fname,
4024 mode_t mode)
4026 int ret;
4028 if (directory_has_default_posix_acl(inherit_from_dirfsp))
4029 return 0;
4031 ret = copy_access_posix_acl(
4032 inherit_from_dirfsp, smb_fname->fsp, mode);
4033 return ret;
4036 /****************************************************************************
4037 Map from wire type to permset.
4038 ****************************************************************************/
4040 static bool unix_ex_wire_to_permset(connection_struct *conn, unsigned char wire_perm, SMB_ACL_PERMSET_T *p_permset)
4042 if (wire_perm & ~(SMB_POSIX_ACL_READ|SMB_POSIX_ACL_WRITE|SMB_POSIX_ACL_EXECUTE)) {
4043 return False;
4046 if (sys_acl_clear_perms(*p_permset) == -1) {
4047 return False;
4050 if (wire_perm & SMB_POSIX_ACL_READ) {
4051 if (sys_acl_add_perm(*p_permset, SMB_ACL_READ) == -1) {
4052 return False;
4055 if (wire_perm & SMB_POSIX_ACL_WRITE) {
4056 if (sys_acl_add_perm(*p_permset, SMB_ACL_WRITE) == -1) {
4057 return False;
4060 if (wire_perm & SMB_POSIX_ACL_EXECUTE) {
4061 if (sys_acl_add_perm(*p_permset, SMB_ACL_EXECUTE) == -1) {
4062 return False;
4065 return True;
4068 /****************************************************************************
4069 Map from wire type to tagtype.
4070 ****************************************************************************/
4072 static bool unix_ex_wire_to_tagtype(unsigned char wire_tt, SMB_ACL_TAG_T *p_tt)
4074 switch (wire_tt) {
4075 case SMB_POSIX_ACL_USER_OBJ:
4076 *p_tt = SMB_ACL_USER_OBJ;
4077 break;
4078 case SMB_POSIX_ACL_USER:
4079 *p_tt = SMB_ACL_USER;
4080 break;
4081 case SMB_POSIX_ACL_GROUP_OBJ:
4082 *p_tt = SMB_ACL_GROUP_OBJ;
4083 break;
4084 case SMB_POSIX_ACL_GROUP:
4085 *p_tt = SMB_ACL_GROUP;
4086 break;
4087 case SMB_POSIX_ACL_MASK:
4088 *p_tt = SMB_ACL_MASK;
4089 break;
4090 case SMB_POSIX_ACL_OTHER:
4091 *p_tt = SMB_ACL_OTHER;
4092 break;
4093 default:
4094 return False;
4096 return True;
4099 /****************************************************************************
4100 Create a new POSIX acl from wire permissions.
4101 FIXME ! How does the share mask/mode fit into this.... ?
4102 ****************************************************************************/
4104 static SMB_ACL_T create_posix_acl_from_wire(connection_struct *conn,
4105 uint16_t num_acls,
4106 const char *pdata,
4107 TALLOC_CTX *mem_ctx)
4109 unsigned int i;
4110 SMB_ACL_T the_acl = sys_acl_init(mem_ctx);
4112 if (the_acl == NULL) {
4113 return NULL;
4116 for (i = 0; i < num_acls; i++) {
4117 SMB_ACL_ENTRY_T the_entry;
4118 SMB_ACL_PERMSET_T the_permset;
4119 SMB_ACL_TAG_T tag_type;
4121 if (sys_acl_create_entry(&the_acl, &the_entry) == -1) {
4122 DEBUG(0,("create_posix_acl_from_wire: Failed to create entry %u. (%s)\n",
4123 i, strerror(errno) ));
4124 goto fail;
4127 if (!unix_ex_wire_to_tagtype(CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)), &tag_type)) {
4128 DEBUG(0,("create_posix_acl_from_wire: invalid wire tagtype %u on entry %u.\n",
4129 CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)), i ));
4130 goto fail;
4133 if (sys_acl_set_tag_type(the_entry, tag_type) == -1) {
4134 DEBUG(0,("create_posix_acl_from_wire: Failed to set tagtype on entry %u. (%s)\n",
4135 i, strerror(errno) ));
4136 goto fail;
4139 /* Get the permset pointer from the new ACL entry. */
4140 if (sys_acl_get_permset(the_entry, &the_permset) == -1) {
4141 DEBUG(0,("create_posix_acl_from_wire: Failed to get permset on entry %u. (%s)\n",
4142 i, strerror(errno) ));
4143 goto fail;
4146 /* Map from wire to permissions. */
4147 if (!unix_ex_wire_to_permset(conn, CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)+1), &the_permset)) {
4148 DEBUG(0,("create_posix_acl_from_wire: invalid permset %u on entry %u.\n",
4149 CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE) + 1), i ));
4150 goto fail;
4153 /* Now apply to the new ACL entry. */
4154 if (sys_acl_set_permset(the_entry, the_permset) == -1) {
4155 DEBUG(0,("create_posix_acl_from_wire: Failed to add permset on entry %u. (%s)\n",
4156 i, strerror(errno) ));
4157 goto fail;
4160 if (tag_type == SMB_ACL_USER) {
4161 uint32_t uidval = IVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
4162 uid_t uid = (uid_t)uidval;
4163 if (sys_acl_set_qualifier(the_entry,(void *)&uid) == -1) {
4164 DEBUG(0,("create_posix_acl_from_wire: Failed to set uid %u on entry %u. (%s)\n",
4165 (unsigned int)uid, i, strerror(errno) ));
4166 goto fail;
4170 if (tag_type == SMB_ACL_GROUP) {
4171 uint32_t gidval = IVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
4172 gid_t gid = (uid_t)gidval;
4173 if (sys_acl_set_qualifier(the_entry,(void *)&gid) == -1) {
4174 DEBUG(0,("create_posix_acl_from_wire: Failed to set gid %u on entry %u. (%s)\n",
4175 (unsigned int)gid, i, strerror(errno) ));
4176 goto fail;
4181 return the_acl;
4183 fail:
4185 if (the_acl != NULL) {
4186 TALLOC_FREE(the_acl);
4188 return NULL;
4191 /****************************************************************************
4192 Calls from UNIX extensions - Default POSIX ACL set.
4193 If num_def_acls == 0 and not a directory just return. If it is a directory
4194 and num_def_acls == 0 then remove the default acl. Else set the default acl
4195 on the directory.
4196 ****************************************************************************/
4198 NTSTATUS set_unix_posix_default_acl(connection_struct *conn,
4199 files_struct *fsp,
4200 uint16_t num_def_acls,
4201 const char *pdata)
4203 SMB_ACL_T def_acl = NULL;
4204 NTSTATUS status;
4205 int ret;
4207 if (!fsp->fsp_flags.is_directory) {
4208 return NT_STATUS_INVALID_HANDLE;
4211 if (!num_def_acls) {
4212 /* Remove the default ACL. */
4213 ret = SMB_VFS_SYS_ACL_DELETE_DEF_FD(fsp);
4214 if (ret == -1) {
4215 status = map_nt_error_from_unix(errno);
4216 DBG_INFO("acl_delete_def_fd failed on "
4217 "directory %s (%s)\n",
4218 fsp_str_dbg(fsp),
4219 strerror(errno));
4220 return status;
4222 return NT_STATUS_OK;
4225 def_acl = create_posix_acl_from_wire(conn,
4226 num_def_acls,
4227 pdata,
4228 talloc_tos());
4229 if (def_acl == NULL) {
4230 return map_nt_error_from_unix(errno);
4233 ret = SMB_VFS_SYS_ACL_SET_FD(fsp,
4234 SMB_ACL_TYPE_DEFAULT,
4235 def_acl);
4236 if (ret == -1) {
4237 status = map_nt_error_from_unix(errno);
4238 DBG_INFO("acl_set_file failed on directory %s (%s)\n",
4239 fsp_str_dbg(fsp),
4240 strerror(errno));
4241 TALLOC_FREE(def_acl);
4242 return status;
4245 DBG_DEBUG("set default acl for file %s\n",
4246 fsp_str_dbg(fsp));
4247 TALLOC_FREE(def_acl);
4248 return NT_STATUS_OK;
4251 /****************************************************************************
4252 Remove an ACL from a file. As we don't have acl_delete_entry() available
4253 we must read the current acl and copy all entries except MASK, USER and GROUP
4254 to a new acl, then set that. This (at least on Linux) causes any ACL to be
4255 removed.
4256 FIXME ! How does the share mask/mode fit into this.... ?
4257 ****************************************************************************/
4259 static NTSTATUS remove_posix_acl(connection_struct *conn,
4260 files_struct *fsp)
4262 SMB_ACL_T file_acl = NULL;
4263 int entry_id = SMB_ACL_FIRST_ENTRY;
4264 SMB_ACL_ENTRY_T entry;
4265 /* Create a new ACL with only 3 entries, u/g/w. */
4266 SMB_ACL_T new_file_acl = NULL;
4267 SMB_ACL_ENTRY_T user_ent = NULL;
4268 SMB_ACL_ENTRY_T group_ent = NULL;
4269 SMB_ACL_ENTRY_T other_ent = NULL;
4270 NTSTATUS status;
4271 int ret;
4273 new_file_acl = sys_acl_init(talloc_tos());
4274 if (new_file_acl == NULL) {
4275 status = map_nt_error_from_unix(errno);
4276 DBG_INFO("failed to init new ACL with 3 entries "
4277 "for file %s %s.\n",
4278 fsp_str_dbg(fsp),
4279 strerror(errno));
4280 goto done;
4283 /* Now create the u/g/w entries. */
4284 ret = sys_acl_create_entry(&new_file_acl, &user_ent);
4285 if (ret == -1) {
4286 status = map_nt_error_from_unix(errno);
4287 DBG_INFO("Failed to create user entry for file %s. (%s)\n",
4288 fsp_str_dbg(fsp),
4289 strerror(errno));
4290 goto done;
4292 ret = sys_acl_set_tag_type(user_ent, SMB_ACL_USER_OBJ);
4293 if (ret == -1) {
4294 status = map_nt_error_from_unix(errno);
4295 DBG_INFO("Failed to set user entry for file %s. (%s)\n",
4296 fsp_str_dbg(fsp),
4297 strerror(errno));
4298 goto done;
4301 ret = sys_acl_create_entry(&new_file_acl, &group_ent);
4302 if (ret == -1) {
4303 status = map_nt_error_from_unix(errno);
4304 DBG_INFO("Failed to create group entry for file %s. (%s)\n",
4305 fsp_str_dbg(fsp),
4306 strerror(errno));
4307 goto done;
4309 ret = sys_acl_set_tag_type(group_ent, SMB_ACL_GROUP_OBJ);
4310 if (ret == -1) {
4311 status = map_nt_error_from_unix(errno);
4312 DBG_INFO("Failed to set group entry for file %s. (%s)\n",
4313 fsp_str_dbg(fsp),
4314 strerror(errno));
4315 goto done;
4318 ret = sys_acl_create_entry(&new_file_acl, &other_ent);
4319 if (ret == -1) {
4320 status = map_nt_error_from_unix(errno);
4321 DBG_INFO("Failed to create other entry for file %s. (%s)\n",
4322 fsp_str_dbg(fsp),
4323 strerror(errno));
4324 goto done;
4326 ret = sys_acl_set_tag_type(other_ent, SMB_ACL_OTHER);
4327 if (ret == -1) {
4328 status = map_nt_error_from_unix(errno);
4329 DBG_INFO("Failed to set other entry for file %s. (%s)\n",
4330 fsp_str_dbg(fsp),
4331 strerror(errno));
4332 goto done;
4335 /* Get the current file ACL. */
4336 file_acl = SMB_VFS_SYS_ACL_GET_FD(fsp,
4337 SMB_ACL_TYPE_ACCESS,
4338 talloc_tos());
4340 if (file_acl == NULL) {
4341 status = map_nt_error_from_unix(errno);
4342 /* This is only returned if an error occurred. Even for a file with
4343 no acl a u/g/w acl should be returned. */
4344 DBG_INFO("failed to get ACL from file %s (%s).\n",
4345 fsp_str_dbg(fsp),
4346 strerror(errno));
4347 goto done;
4350 while ( sys_acl_get_entry(file_acl, entry_id, &entry) == 1) {
4351 SMB_ACL_TAG_T tagtype;
4352 SMB_ACL_PERMSET_T permset;
4354 entry_id = SMB_ACL_NEXT_ENTRY;
4356 ret = sys_acl_get_tag_type(entry, &tagtype);
4357 if (ret == -1) {
4358 status = map_nt_error_from_unix(errno);
4359 DBG_INFO("failed to get tagtype from ACL "
4360 "on file %s (%s).\n",
4361 fsp_str_dbg(fsp),
4362 strerror(errno));
4363 goto done;
4366 ret = sys_acl_get_permset(entry, &permset);
4367 if (ret == -1) {
4368 status = map_nt_error_from_unix(errno);
4369 DBG_INFO("failed to get permset from ACL "
4370 "on file %s (%s).\n",
4371 fsp_str_dbg(fsp),
4372 strerror(errno));
4373 goto done;
4376 if (tagtype == SMB_ACL_USER_OBJ) {
4377 ret = sys_acl_set_permset(user_ent, permset);
4378 if (ret == -1) {
4379 status = map_nt_error_from_unix(errno);
4380 DBG_INFO("failed to set permset from ACL "
4381 "on file %s (%s).\n",
4382 fsp_str_dbg(fsp),
4383 strerror(errno));
4384 goto done;
4386 } else if (tagtype == SMB_ACL_GROUP_OBJ) {
4387 ret = sys_acl_set_permset(group_ent, permset);
4388 if (ret == -1) {
4389 status = map_nt_error_from_unix(errno);
4390 DBG_INFO("failed to set permset from ACL "
4391 "on file %s (%s).\n",
4392 fsp_str_dbg(fsp),
4393 strerror(errno));
4394 goto done;
4396 } else if (tagtype == SMB_ACL_OTHER) {
4397 ret = sys_acl_set_permset(other_ent, permset);
4398 if (ret == -1) {
4399 status = map_nt_error_from_unix(errno);
4400 DBG_INFO("failed to set permset from ACL "
4401 "on file %s (%s).\n",
4402 fsp_str_dbg(fsp),
4403 strerror(errno));
4404 goto done;
4409 /* Set the new empty file ACL. */
4410 ret = SMB_VFS_SYS_ACL_SET_FD(fsp, SMB_ACL_TYPE_ACCESS, new_file_acl);
4411 if (ret == -1) {
4412 status = map_nt_error_from_unix(errno);
4413 DBG_INFO("acl_set_file failed on %s (%s)\n",
4414 fsp_str_dbg(fsp),
4415 strerror(errno));
4416 goto done;
4419 status = NT_STATUS_OK;
4421 done:
4423 TALLOC_FREE(file_acl);
4424 TALLOC_FREE(new_file_acl);
4425 return status;
4428 /****************************************************************************
4429 Calls from UNIX extensions - POSIX ACL set.
4430 If num_def_acls == 0 then read/modify/write acl after removing all entries
4431 except SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER.
4432 ****************************************************************************/
4434 NTSTATUS set_unix_posix_acl(connection_struct *conn,
4435 files_struct *fsp,
4436 uint16_t num_acls,
4437 const char *pdata)
4439 SMB_ACL_T file_acl = NULL;
4440 int ret;
4441 NTSTATUS status;
4443 if (!num_acls) {
4444 /* Remove the ACL from the file. */
4445 return remove_posix_acl(conn, fsp);
4448 file_acl = create_posix_acl_from_wire(conn,
4449 num_acls,
4450 pdata,
4451 talloc_tos());
4452 if (file_acl == NULL) {
4453 return map_nt_error_from_unix(errno);
4456 ret = SMB_VFS_SYS_ACL_SET_FD(fsp, SMB_ACL_TYPE_ACCESS, file_acl);
4457 if (ret == -1) {
4458 status = map_nt_error_from_unix(errno);
4459 DBG_INFO("acl_set_file failed on %s (%s)\n",
4460 fsp_str_dbg(fsp),
4461 strerror(errno));
4462 TALLOC_FREE(file_acl);
4463 return status;
4466 DBG_DEBUG("set acl for file %s\n",
4467 fsp_str_dbg(fsp));
4469 TALLOC_FREE(file_acl);
4470 return NT_STATUS_OK;
4473 int posix_sys_acl_blob_get_file(vfs_handle_struct *handle,
4474 const struct smb_filename *smb_fname_in,
4475 TALLOC_CTX *mem_ctx,
4476 char **blob_description,
4477 DATA_BLOB *blob)
4479 int ret;
4480 TALLOC_CTX *frame = talloc_stackframe();
4481 /* Initialise this to zero, in a portable way */
4482 struct smb_acl_wrapper acl_wrapper = {
4485 struct smb_filename *smb_fname = cp_smb_filename_nostream(frame,
4486 smb_fname_in);
4487 if (smb_fname == NULL) {
4488 TALLOC_FREE(frame);
4489 errno = ENOMEM;
4490 return -1;
4493 ret = smb_vfs_call_stat(handle, smb_fname);
4494 if (ret == -1) {
4495 TALLOC_FREE(frame);
4496 return -1;
4499 acl_wrapper.owner = smb_fname->st.st_ex_uid;
4500 acl_wrapper.group = smb_fname->st.st_ex_gid;
4501 acl_wrapper.mode = smb_fname->st.st_ex_mode;
4503 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(blob, mem_ctx,
4504 &acl_wrapper,
4505 (ndr_push_flags_fn_t)ndr_push_smb_acl_wrapper))) {
4506 errno = EINVAL;
4507 TALLOC_FREE(frame);
4508 return -1;
4511 *blob_description = talloc_strdup(mem_ctx, "posix_acl");
4512 if (!*blob_description) {
4513 errno = EINVAL;
4514 TALLOC_FREE(frame);
4515 return -1;
4518 TALLOC_FREE(frame);
4519 return 0;
4522 int posix_sys_acl_blob_get_fd(vfs_handle_struct *handle,
4523 files_struct *fsp,
4524 TALLOC_CTX *mem_ctx,
4525 char **blob_description,
4526 DATA_BLOB *blob)
4528 SMB_STRUCT_STAT sbuf;
4529 TALLOC_CTX *frame;
4530 struct smb_acl_wrapper acl_wrapper = { 0 };
4531 int ret;
4533 frame = talloc_stackframe();
4535 acl_wrapper.access_acl = smb_vfs_call_sys_acl_get_fd(handle,
4536 fsp,
4537 SMB_ACL_TYPE_ACCESS,
4538 frame);
4540 if (fsp->fsp_flags.is_directory) {
4541 acl_wrapper.default_acl = smb_vfs_call_sys_acl_get_fd(handle,
4542 fsp,
4543 SMB_ACL_TYPE_DEFAULT,
4544 frame);
4547 ret = smb_vfs_call_fstat(handle, fsp, &sbuf);
4548 if (ret == -1) {
4549 TALLOC_FREE(frame);
4550 return -1;
4553 acl_wrapper.owner = sbuf.st_ex_uid;
4554 acl_wrapper.group = sbuf.st_ex_gid;
4555 acl_wrapper.mode = sbuf.st_ex_mode;
4557 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(blob, mem_ctx,
4558 &acl_wrapper,
4559 (ndr_push_flags_fn_t)ndr_push_smb_acl_wrapper))) {
4560 errno = EINVAL;
4561 TALLOC_FREE(frame);
4562 return -1;
4565 *blob_description = talloc_strdup(mem_ctx, "posix_acl");
4566 if (!*blob_description) {
4567 errno = EINVAL;
4568 TALLOC_FREE(frame);
4569 return -1;
4572 TALLOC_FREE(frame);
4573 return 0;
4576 static NTSTATUS make_default_acl_posix(TALLOC_CTX *ctx,
4577 const char *name,
4578 const SMB_STRUCT_STAT *psbuf,
4579 struct security_descriptor **ppdesc)
4581 struct dom_sid owner_sid, group_sid;
4582 size_t size = 0;
4583 struct security_ace aces[4];
4584 uint32_t access_mask = 0;
4585 mode_t mode = psbuf->st_ex_mode;
4586 struct security_acl *new_dacl = NULL;
4587 int idx = 0;
4589 DBG_DEBUG("file %s mode = 0%o\n",name, (int)mode);
4591 uid_to_sid(&owner_sid, psbuf->st_ex_uid);
4592 gid_to_sid(&group_sid, psbuf->st_ex_gid);
4595 We provide up to 4 ACEs
4596 - Owner
4597 - Group
4598 - Everyone
4599 - NT System
4602 if (mode & S_IRUSR) {
4603 if (mode & S_IWUSR) {
4604 access_mask |= SEC_RIGHTS_FILE_ALL;
4605 } else {
4606 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
4609 if (mode & S_IWUSR) {
4610 access_mask |= SEC_RIGHTS_FILE_WRITE | SEC_STD_DELETE;
4613 init_sec_ace(&aces[idx],
4614 &owner_sid,
4615 SEC_ACE_TYPE_ACCESS_ALLOWED,
4616 access_mask,
4618 idx++;
4620 access_mask = 0;
4621 if (mode & S_IRGRP) {
4622 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
4624 if (mode & S_IWGRP) {
4625 /* note that delete is not granted - this matches posix behaviour */
4626 access_mask |= SEC_RIGHTS_FILE_WRITE;
4628 if (access_mask) {
4629 init_sec_ace(&aces[idx],
4630 &group_sid,
4631 SEC_ACE_TYPE_ACCESS_ALLOWED,
4632 access_mask,
4634 idx++;
4637 access_mask = 0;
4638 if (mode & S_IROTH) {
4639 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
4641 if (mode & S_IWOTH) {
4642 access_mask |= SEC_RIGHTS_FILE_WRITE;
4644 if (access_mask) {
4645 init_sec_ace(&aces[idx],
4646 &global_sid_World,
4647 SEC_ACE_TYPE_ACCESS_ALLOWED,
4648 access_mask,
4650 idx++;
4653 init_sec_ace(&aces[idx],
4654 &global_sid_System,
4655 SEC_ACE_TYPE_ACCESS_ALLOWED,
4656 SEC_RIGHTS_FILE_ALL,
4658 idx++;
4660 new_dacl = make_sec_acl(ctx,
4661 NT4_ACL_REVISION,
4662 idx,
4663 aces);
4665 if (!new_dacl) {
4666 return NT_STATUS_NO_MEMORY;
4669 *ppdesc = make_sec_desc(ctx,
4670 SECURITY_DESCRIPTOR_REVISION_1,
4671 SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
4672 &owner_sid,
4673 &group_sid,
4674 NULL,
4675 new_dacl,
4676 &size);
4677 if (!*ppdesc) {
4678 return NT_STATUS_NO_MEMORY;
4680 return NT_STATUS_OK;
4683 static NTSTATUS make_default_acl_windows(TALLOC_CTX *ctx,
4684 const char *name,
4685 const SMB_STRUCT_STAT *psbuf,
4686 struct security_descriptor **ppdesc)
4688 struct dom_sid owner_sid, group_sid;
4689 size_t size = 0;
4690 struct security_ace aces[4];
4691 uint32_t access_mask = 0;
4692 mode_t mode = psbuf->st_ex_mode;
4693 struct security_acl *new_dacl = NULL;
4694 int idx = 0;
4696 DBG_DEBUG("file [%s] mode [0%o]\n", name, (int)mode);
4698 uid_to_sid(&owner_sid, psbuf->st_ex_uid);
4699 gid_to_sid(&group_sid, psbuf->st_ex_gid);
4702 * We provide 2 ACEs:
4703 * - Owner
4704 * - NT System
4707 if (mode & S_IRUSR) {
4708 if (mode & S_IWUSR) {
4709 access_mask |= SEC_RIGHTS_FILE_ALL;
4710 } else {
4711 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
4714 if (mode & S_IWUSR) {
4715 access_mask |= SEC_RIGHTS_FILE_WRITE | SEC_STD_DELETE;
4718 init_sec_ace(&aces[idx],
4719 &owner_sid,
4720 SEC_ACE_TYPE_ACCESS_ALLOWED,
4721 access_mask,
4723 idx++;
4725 init_sec_ace(&aces[idx],
4726 &global_sid_System,
4727 SEC_ACE_TYPE_ACCESS_ALLOWED,
4728 SEC_RIGHTS_FILE_ALL,
4730 idx++;
4732 new_dacl = make_sec_acl(ctx,
4733 NT4_ACL_REVISION,
4734 idx,
4735 aces);
4737 if (!new_dacl) {
4738 return NT_STATUS_NO_MEMORY;
4741 *ppdesc = make_sec_desc(ctx,
4742 SECURITY_DESCRIPTOR_REVISION_1,
4743 SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
4744 &owner_sid,
4745 &group_sid,
4746 NULL,
4747 new_dacl,
4748 &size);
4749 if (!*ppdesc) {
4750 return NT_STATUS_NO_MEMORY;
4752 return NT_STATUS_OK;
4755 static NTSTATUS make_default_acl_everyone(TALLOC_CTX *ctx,
4756 const char *name,
4757 const SMB_STRUCT_STAT *psbuf,
4758 struct security_descriptor **ppdesc)
4760 struct dom_sid owner_sid, group_sid;
4761 size_t size = 0;
4762 struct security_ace aces[1];
4763 mode_t mode = psbuf->st_ex_mode;
4764 struct security_acl *new_dacl = NULL;
4765 int idx = 0;
4767 DBG_DEBUG("file [%s] mode [0%o]\n", name, (int)mode);
4769 uid_to_sid(&owner_sid, psbuf->st_ex_uid);
4770 gid_to_sid(&group_sid, psbuf->st_ex_gid);
4773 * We provide one ACEs: full access for everyone
4776 init_sec_ace(&aces[idx],
4777 &global_sid_World,
4778 SEC_ACE_TYPE_ACCESS_ALLOWED,
4779 SEC_RIGHTS_FILE_ALL,
4781 idx++;
4783 new_dacl = make_sec_acl(ctx,
4784 NT4_ACL_REVISION,
4785 idx,
4786 aces);
4788 if (!new_dacl) {
4789 return NT_STATUS_NO_MEMORY;
4792 *ppdesc = make_sec_desc(ctx,
4793 SECURITY_DESCRIPTOR_REVISION_1,
4794 SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
4795 &owner_sid,
4796 &group_sid,
4797 NULL,
4798 new_dacl,
4799 &size);
4800 if (!*ppdesc) {
4801 return NT_STATUS_NO_MEMORY;
4803 return NT_STATUS_OK;
4806 static const struct enum_list default_acl_style_list[] = {
4807 {DEFAULT_ACL_POSIX, "posix"},
4808 {DEFAULT_ACL_WINDOWS, "windows"},
4809 {DEFAULT_ACL_EVERYONE, "everyone"},
4812 const struct enum_list *get_default_acl_style_list(void)
4814 return default_acl_style_list;
4817 NTSTATUS make_default_filesystem_acl(
4818 TALLOC_CTX *ctx,
4819 enum default_acl_style acl_style,
4820 const char *name,
4821 const SMB_STRUCT_STAT *psbuf,
4822 struct security_descriptor **ppdesc)
4824 NTSTATUS status;
4826 switch (acl_style) {
4827 case DEFAULT_ACL_POSIX:
4828 status = make_default_acl_posix(ctx, name, psbuf, ppdesc);
4829 break;
4831 case DEFAULT_ACL_WINDOWS:
4832 status = make_default_acl_windows(ctx, name, psbuf, ppdesc);
4833 break;
4835 case DEFAULT_ACL_EVERYONE:
4836 status = make_default_acl_everyone(ctx, name, psbuf, ppdesc);
4837 break;
4839 default:
4840 DBG_ERR("unknown acl style %d", acl_style);
4841 status = NT_STATUS_INTERNAL_ERROR;
4842 break;
4845 return status;