libcli/smb: add basic session->smb2.channel_sequence handling
[Samba/gebeck_regimport.git] / source3 / smbd / posix_acls.c
blob59f8e0cd4412200ba7b4e6f63d74cbf62b3e56a7
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 "lib/param/loadparm.h"
32 extern const struct generic_mapping file_generic_mapping;
34 #undef DBGC_CLASS
35 #define DBGC_CLASS DBGC_ACLS
37 /****************************************************************************
38 Data structures representing the internal ACE format.
39 ****************************************************************************/
41 enum ace_owner {UID_ACE, GID_ACE, WORLD_ACE};
42 enum ace_attribute {ALLOW_ACE, DENY_ACE}; /* Used for incoming NT ACLS. */
44 typedef union posix_id {
45 uid_t uid;
46 gid_t gid;
47 int world;
48 } posix_id;
50 typedef struct canon_ace {
51 struct canon_ace *next, *prev;
52 SMB_ACL_TAG_T type;
53 mode_t perms; /* Only use S_I(R|W|X)USR mode bits here. */
54 struct dom_sid trustee;
55 enum ace_owner owner_type;
56 enum ace_attribute attr;
57 posix_id unix_ug;
58 uint8_t ace_flags; /* From windows ACE entry. */
59 } canon_ace;
61 #define ALL_ACE_PERMS (S_IRUSR|S_IWUSR|S_IXUSR)
64 * EA format of user.SAMBA_PAI (Samba_Posix_Acl_Interitance)
65 * attribute on disk - version 1.
66 * All values are little endian.
68 * | 1 | 1 | 2 | 2 | ....
69 * +------+------+-------------+---------------------+-------------+--------------------+
70 * | vers | flag | num_entries | num_default_entries | ..entries.. | default_entries... |
71 * +------+------+-------------+---------------------+-------------+--------------------+
73 * Entry format is :
75 * | 1 | 4 |
76 * +------+-------------------+
77 * | value| uid/gid or world |
78 * | type | value |
79 * +------+-------------------+
81 * Version 2 format. Stores extra Windows metadata about an ACL.
83 * | 1 | 2 | 2 | 2 | ....
84 * +------+----------+-------------+---------------------+-------------+--------------------+
85 * | vers | ace | num_entries | num_default_entries | ..entries.. | default_entries... |
86 * | 2 | type | | | | |
87 * +------+----------+-------------+---------------------+-------------+--------------------+
89 * Entry format is :
91 * | 1 | 1 | 4 |
92 * +------+------+-------------------+
93 * | ace | value| uid/gid or world |
94 * | flag | type | value |
95 * +------+-------------------+------+
99 #define PAI_VERSION_OFFSET 0
101 #define PAI_V1_FLAG_OFFSET 1
102 #define PAI_V1_NUM_ENTRIES_OFFSET 2
103 #define PAI_V1_NUM_DEFAULT_ENTRIES_OFFSET 4
104 #define PAI_V1_ENTRIES_BASE 6
105 #define PAI_V1_ACL_FLAG_PROTECTED 0x1
106 #define PAI_V1_ENTRY_LENGTH 5
108 #define PAI_V1_VERSION 1
110 #define PAI_V2_TYPE_OFFSET 1
111 #define PAI_V2_NUM_ENTRIES_OFFSET 3
112 #define PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET 5
113 #define PAI_V2_ENTRIES_BASE 7
114 #define PAI_V2_ENTRY_LENGTH 6
116 #define PAI_V2_VERSION 2
119 * In memory format of user.SAMBA_PAI attribute.
122 struct pai_entry {
123 struct pai_entry *next, *prev;
124 uint8_t ace_flags;
125 enum ace_owner owner_type;
126 posix_id unix_ug;
129 struct pai_val {
130 uint16_t sd_type;
131 unsigned int num_entries;
132 struct pai_entry *entry_list;
133 unsigned int num_def_entries;
134 struct pai_entry *def_entry_list;
137 /************************************************************************
138 Return a uint32 of the pai_entry principal.
139 ************************************************************************/
141 static uint32_t get_pai_entry_val(struct pai_entry *paie)
143 switch (paie->owner_type) {
144 case UID_ACE:
145 DEBUG(10,("get_pai_entry_val: uid = %u\n", (unsigned int)paie->unix_ug.uid ));
146 return (uint32_t)paie->unix_ug.uid;
147 case GID_ACE:
148 DEBUG(10,("get_pai_entry_val: gid = %u\n", (unsigned int)paie->unix_ug.gid ));
149 return (uint32_t)paie->unix_ug.gid;
150 case WORLD_ACE:
151 default:
152 DEBUG(10,("get_pai_entry_val: world ace\n"));
153 return (uint32_t)-1;
157 /************************************************************************
158 Return a uint32 of the entry principal.
159 ************************************************************************/
161 static uint32_t get_entry_val(canon_ace *ace_entry)
163 switch (ace_entry->owner_type) {
164 case UID_ACE:
165 DEBUG(10,("get_entry_val: uid = %u\n", (unsigned int)ace_entry->unix_ug.uid ));
166 return (uint32_t)ace_entry->unix_ug.uid;
167 case GID_ACE:
168 DEBUG(10,("get_entry_val: gid = %u\n", (unsigned int)ace_entry->unix_ug.gid ));
169 return (uint32_t)ace_entry->unix_ug.gid;
170 case WORLD_ACE:
171 default:
172 DEBUG(10,("get_entry_val: world ace\n"));
173 return (uint32_t)-1;
177 /************************************************************************
178 Create the on-disk format (always v2 now). Caller must free.
179 ************************************************************************/
181 static char *create_pai_buf_v2(canon_ace *file_ace_list,
182 canon_ace *dir_ace_list,
183 uint16_t sd_type,
184 size_t *store_size)
186 char *pai_buf = NULL;
187 canon_ace *ace_list = NULL;
188 char *entry_offset = NULL;
189 unsigned int num_entries = 0;
190 unsigned int num_def_entries = 0;
191 unsigned int i;
193 for (ace_list = file_ace_list; ace_list; ace_list = ace_list->next) {
194 num_entries++;
197 for (ace_list = dir_ace_list; ace_list; ace_list = ace_list->next) {
198 num_def_entries++;
201 DEBUG(10,("create_pai_buf_v2: num_entries = %u, num_def_entries = %u\n", num_entries, num_def_entries ));
203 *store_size = PAI_V2_ENTRIES_BASE +
204 ((num_entries + num_def_entries)*PAI_V2_ENTRY_LENGTH);
206 pai_buf = talloc_array(talloc_tos(), char, *store_size);
207 if (!pai_buf) {
208 return NULL;
211 /* Set up the header. */
212 memset(pai_buf, '\0', PAI_V2_ENTRIES_BASE);
213 SCVAL(pai_buf,PAI_VERSION_OFFSET,PAI_V2_VERSION);
214 SSVAL(pai_buf,PAI_V2_TYPE_OFFSET, sd_type);
215 SSVAL(pai_buf,PAI_V2_NUM_ENTRIES_OFFSET,num_entries);
216 SSVAL(pai_buf,PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET,num_def_entries);
218 DEBUG(10,("create_pai_buf_v2: sd_type = 0x%x\n",
219 (unsigned int)sd_type ));
221 entry_offset = pai_buf + PAI_V2_ENTRIES_BASE;
223 i = 0;
224 for (ace_list = file_ace_list; ace_list; ace_list = ace_list->next) {
225 uint8_t type_val = (uint8_t)ace_list->owner_type;
226 uint32_t entry_val = get_entry_val(ace_list);
228 SCVAL(entry_offset,0,ace_list->ace_flags);
229 SCVAL(entry_offset,1,type_val);
230 SIVAL(entry_offset,2,entry_val);
231 DEBUG(10,("create_pai_buf_v2: entry %u [0x%x] [0x%x] [0x%x]\n",
233 (unsigned int)ace_list->ace_flags,
234 (unsigned int)type_val,
235 (unsigned int)entry_val ));
236 i++;
237 entry_offset += PAI_V2_ENTRY_LENGTH;
240 for (ace_list = dir_ace_list; ace_list; ace_list = ace_list->next) {
241 uint8_t type_val = (uint8_t)ace_list->owner_type;
242 uint32_t entry_val = get_entry_val(ace_list);
244 SCVAL(entry_offset,0,ace_list->ace_flags);
245 SCVAL(entry_offset,1,type_val);
246 SIVAL(entry_offset,2,entry_val);
247 DEBUG(10,("create_pai_buf_v2: entry %u [0x%x] [0x%x] [0x%x]\n",
249 (unsigned int)ace_list->ace_flags,
250 (unsigned int)type_val,
251 (unsigned int)entry_val ));
252 i++;
253 entry_offset += PAI_V2_ENTRY_LENGTH;
256 return pai_buf;
259 /************************************************************************
260 Store the user.SAMBA_PAI attribute on disk.
261 ************************************************************************/
263 static void store_inheritance_attributes(files_struct *fsp,
264 canon_ace *file_ace_list,
265 canon_ace *dir_ace_list,
266 uint16_t sd_type)
268 int ret;
269 size_t store_size;
270 char *pai_buf;
272 if (!lp_map_acl_inherit(SNUM(fsp->conn))) {
273 return;
276 pai_buf = create_pai_buf_v2(file_ace_list, dir_ace_list,
277 sd_type, &store_size);
279 if (fsp->fh->fd != -1) {
280 ret = SMB_VFS_FSETXATTR(fsp, SAMBA_POSIX_INHERITANCE_EA_NAME,
281 pai_buf, store_size, 0);
282 } else {
283 ret = SMB_VFS_SETXATTR(fsp->conn, fsp->fsp_name->base_name,
284 SAMBA_POSIX_INHERITANCE_EA_NAME,
285 pai_buf, store_size, 0);
288 TALLOC_FREE(pai_buf);
290 DEBUG(10,("store_inheritance_attribute: type 0x%x for file %s\n",
291 (unsigned int)sd_type,
292 fsp_str_dbg(fsp)));
294 if (ret == -1 && !no_acl_syscall_error(errno)) {
295 DEBUG(1,("store_inheritance_attribute: Error %s\n", strerror(errno) ));
299 /************************************************************************
300 Delete the in memory inheritance info.
301 ************************************************************************/
303 static void free_inherited_info(struct pai_val *pal)
305 if (pal) {
306 struct pai_entry *paie, *paie_next;
307 for (paie = pal->entry_list; paie; paie = paie_next) {
308 paie_next = paie->next;
309 TALLOC_FREE(paie);
311 for (paie = pal->def_entry_list; paie; paie = paie_next) {
312 paie_next = paie->next;
313 TALLOC_FREE(paie);
315 TALLOC_FREE(pal);
319 /************************************************************************
320 Get any stored ACE flags.
321 ************************************************************************/
323 static uint16_t get_pai_flags(struct pai_val *pal, canon_ace *ace_entry, bool default_ace)
325 struct pai_entry *paie;
327 if (!pal) {
328 return 0;
331 /* If the entry exists it is inherited. */
332 for (paie = (default_ace ? pal->def_entry_list : pal->entry_list); paie; paie = paie->next) {
333 if (ace_entry->owner_type == paie->owner_type &&
334 get_entry_val(ace_entry) == get_pai_entry_val(paie))
335 return paie->ace_flags;
337 return 0;
340 /************************************************************************
341 Ensure an attribute just read is valid - v1.
342 ************************************************************************/
344 static bool check_pai_ok_v1(const char *pai_buf, size_t pai_buf_data_size)
346 uint16 num_entries;
347 uint16 num_def_entries;
349 if (pai_buf_data_size < PAI_V1_ENTRIES_BASE) {
350 /* Corrupted - too small. */
351 return false;
354 if (CVAL(pai_buf,PAI_VERSION_OFFSET) != PAI_V1_VERSION) {
355 return false;
358 num_entries = SVAL(pai_buf,PAI_V1_NUM_ENTRIES_OFFSET);
359 num_def_entries = SVAL(pai_buf,PAI_V1_NUM_DEFAULT_ENTRIES_OFFSET);
361 /* Check the entry lists match. */
362 /* Each entry is 5 bytes (type plus 4 bytes of uid or gid). */
364 if (((num_entries + num_def_entries)*PAI_V1_ENTRY_LENGTH) +
365 PAI_V1_ENTRIES_BASE != pai_buf_data_size) {
366 return false;
369 return true;
372 /************************************************************************
373 Ensure an attribute just read is valid - v2.
374 ************************************************************************/
376 static bool check_pai_ok_v2(const char *pai_buf, size_t pai_buf_data_size)
378 uint16 num_entries;
379 uint16 num_def_entries;
381 if (pai_buf_data_size < PAI_V2_ENTRIES_BASE) {
382 /* Corrupted - too small. */
383 return false;
386 if (CVAL(pai_buf,PAI_VERSION_OFFSET) != PAI_V2_VERSION) {
387 return false;
390 num_entries = SVAL(pai_buf,PAI_V2_NUM_ENTRIES_OFFSET);
391 num_def_entries = SVAL(pai_buf,PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET);
393 /* Check the entry lists match. */
394 /* Each entry is 6 bytes (flags + type + 4 bytes of uid or gid). */
396 if (((num_entries + num_def_entries)*PAI_V2_ENTRY_LENGTH) +
397 PAI_V2_ENTRIES_BASE != pai_buf_data_size) {
398 return false;
401 return true;
404 /************************************************************************
405 Decode the owner.
406 ************************************************************************/
408 static bool get_pai_owner_type(struct pai_entry *paie, const char *entry_offset)
410 paie->owner_type = (enum ace_owner)CVAL(entry_offset,0);
411 switch( paie->owner_type) {
412 case UID_ACE:
413 paie->unix_ug.uid = (uid_t)IVAL(entry_offset,1);
414 DEBUG(10,("get_pai_owner_type: uid = %u\n",
415 (unsigned int)paie->unix_ug.uid ));
416 break;
417 case GID_ACE:
418 paie->unix_ug.gid = (gid_t)IVAL(entry_offset,1);
419 DEBUG(10,("get_pai_owner_type: gid = %u\n",
420 (unsigned int)paie->unix_ug.gid ));
421 break;
422 case WORLD_ACE:
423 paie->unix_ug.world = -1;
424 DEBUG(10,("get_pai_owner_type: world ace\n"));
425 break;
426 default:
427 DEBUG(10,("get_pai_owner_type: unknown type %u\n",
428 (unsigned int)paie->owner_type ));
429 return false;
431 return true;
434 /************************************************************************
435 Process v2 entries.
436 ************************************************************************/
438 static const char *create_pai_v1_entries(struct pai_val *paiv,
439 const char *entry_offset,
440 bool def_entry)
442 int i;
444 for (i = 0; i < paiv->num_entries; i++) {
445 struct pai_entry *paie = talloc(talloc_tos(), struct pai_entry);
446 if (!paie) {
447 return NULL;
450 paie->ace_flags = SEC_ACE_FLAG_INHERITED_ACE;
451 if (!get_pai_owner_type(paie, entry_offset)) {
452 TALLOC_FREE(paie);
453 return NULL;
456 if (!def_entry) {
457 DLIST_ADD(paiv->entry_list, paie);
458 } else {
459 DLIST_ADD(paiv->def_entry_list, paie);
461 entry_offset += PAI_V1_ENTRY_LENGTH;
463 return entry_offset;
466 /************************************************************************
467 Convert to in-memory format from version 1.
468 ************************************************************************/
470 static struct pai_val *create_pai_val_v1(const char *buf, size_t size)
472 const char *entry_offset;
473 struct pai_val *paiv = NULL;
475 if (!check_pai_ok_v1(buf, size)) {
476 return NULL;
479 paiv = talloc(talloc_tos(), struct pai_val);
480 if (!paiv) {
481 return NULL;
484 memset(paiv, '\0', sizeof(struct pai_val));
486 paiv->sd_type = (CVAL(buf,PAI_V1_FLAG_OFFSET) == PAI_V1_ACL_FLAG_PROTECTED) ?
487 SEC_DESC_DACL_PROTECTED : 0;
489 paiv->num_entries = SVAL(buf,PAI_V1_NUM_ENTRIES_OFFSET);
490 paiv->num_def_entries = SVAL(buf,PAI_V1_NUM_DEFAULT_ENTRIES_OFFSET);
492 entry_offset = buf + PAI_V1_ENTRIES_BASE;
494 DEBUG(10,("create_pai_val: num_entries = %u, num_def_entries = %u\n",
495 paiv->num_entries, paiv->num_def_entries ));
497 entry_offset = create_pai_v1_entries(paiv, entry_offset, false);
498 if (entry_offset == NULL) {
499 free_inherited_info(paiv);
500 return NULL;
502 entry_offset = create_pai_v1_entries(paiv, entry_offset, true);
503 if (entry_offset == NULL) {
504 free_inherited_info(paiv);
505 return NULL;
508 return paiv;
511 /************************************************************************
512 Process v2 entries.
513 ************************************************************************/
515 static const char *create_pai_v2_entries(struct pai_val *paiv,
516 unsigned int num_entries,
517 const char *entry_offset,
518 bool def_entry)
520 unsigned int i;
522 for (i = 0; i < num_entries; i++) {
523 struct pai_entry *paie = talloc(talloc_tos(), struct pai_entry);
524 if (!paie) {
525 return NULL;
528 paie->ace_flags = CVAL(entry_offset,0);
530 if (!get_pai_owner_type(paie, entry_offset+1)) {
531 TALLOC_FREE(paie);
532 return NULL;
534 if (!def_entry) {
535 DLIST_ADD(paiv->entry_list, paie);
536 } else {
537 DLIST_ADD(paiv->def_entry_list, paie);
539 entry_offset += PAI_V2_ENTRY_LENGTH;
541 return entry_offset;
544 /************************************************************************
545 Convert to in-memory format from version 2.
546 ************************************************************************/
548 static struct pai_val *create_pai_val_v2(const char *buf, size_t size)
550 const char *entry_offset;
551 struct pai_val *paiv = NULL;
553 if (!check_pai_ok_v2(buf, size)) {
554 return NULL;
557 paiv = talloc(talloc_tos(), struct pai_val);
558 if (!paiv) {
559 return NULL;
562 memset(paiv, '\0', sizeof(struct pai_val));
564 paiv->sd_type = SVAL(buf,PAI_V2_TYPE_OFFSET);
566 paiv->num_entries = SVAL(buf,PAI_V2_NUM_ENTRIES_OFFSET);
567 paiv->num_def_entries = SVAL(buf,PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET);
569 entry_offset = buf + PAI_V2_ENTRIES_BASE;
571 DEBUG(10,("create_pai_val_v2: sd_type = 0x%x num_entries = %u, num_def_entries = %u\n",
572 (unsigned int)paiv->sd_type,
573 paiv->num_entries, paiv->num_def_entries ));
575 entry_offset = create_pai_v2_entries(paiv, paiv->num_entries,
576 entry_offset, false);
577 if (entry_offset == NULL) {
578 free_inherited_info(paiv);
579 return NULL;
581 entry_offset = create_pai_v2_entries(paiv, paiv->num_def_entries,
582 entry_offset, true);
583 if (entry_offset == NULL) {
584 free_inherited_info(paiv);
585 return NULL;
588 return paiv;
591 /************************************************************************
592 Convert to in-memory format - from either version 1 or 2.
593 ************************************************************************/
595 static struct pai_val *create_pai_val(const char *buf, size_t size)
597 if (size < 1) {
598 return NULL;
600 if (CVAL(buf,PAI_VERSION_OFFSET) == PAI_V1_VERSION) {
601 return create_pai_val_v1(buf, size);
602 } else if (CVAL(buf,PAI_VERSION_OFFSET) == PAI_V2_VERSION) {
603 return create_pai_val_v2(buf, size);
604 } else {
605 return NULL;
609 /************************************************************************
610 Load the user.SAMBA_PAI attribute.
611 ************************************************************************/
613 static struct pai_val *fload_inherited_info(files_struct *fsp)
615 char *pai_buf;
616 size_t pai_buf_size = 1024;
617 struct pai_val *paiv = NULL;
618 ssize_t ret;
620 if (!lp_map_acl_inherit(SNUM(fsp->conn))) {
621 return NULL;
624 if ((pai_buf = talloc_array(talloc_tos(), char, pai_buf_size)) == NULL) {
625 return NULL;
628 do {
629 if (fsp->fh->fd != -1) {
630 ret = SMB_VFS_FGETXATTR(fsp, SAMBA_POSIX_INHERITANCE_EA_NAME,
631 pai_buf, pai_buf_size);
632 } else {
633 ret = SMB_VFS_GETXATTR(fsp->conn,
634 fsp->fsp_name->base_name,
635 SAMBA_POSIX_INHERITANCE_EA_NAME,
636 pai_buf, pai_buf_size);
639 if (ret == -1) {
640 if (errno != ERANGE) {
641 break;
643 /* Buffer too small - enlarge it. */
644 pai_buf_size *= 2;
645 TALLOC_FREE(pai_buf);
646 if (pai_buf_size > 1024*1024) {
647 return NULL; /* Limit malloc to 1mb. */
649 if ((pai_buf = talloc_array(talloc_tos(), char, pai_buf_size)) == NULL)
650 return NULL;
652 } while (ret == -1);
654 DEBUG(10,("load_inherited_info: ret = %lu for file %s\n",
655 (unsigned long)ret, fsp_str_dbg(fsp)));
657 if (ret == -1) {
658 /* No attribute or not supported. */
659 #if defined(ENOATTR)
660 if (errno != ENOATTR)
661 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno) ));
662 #else
663 if (errno != ENOSYS)
664 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno) ));
665 #endif
666 TALLOC_FREE(pai_buf);
667 return NULL;
670 paiv = create_pai_val(pai_buf, ret);
672 if (paiv) {
673 DEBUG(10,("load_inherited_info: ACL type is 0x%x for file %s\n",
674 (unsigned int)paiv->sd_type, fsp_str_dbg(fsp)));
677 TALLOC_FREE(pai_buf);
678 return paiv;
681 /************************************************************************
682 Load the user.SAMBA_PAI attribute.
683 ************************************************************************/
685 static struct pai_val *load_inherited_info(const struct connection_struct *conn,
686 const char *fname)
688 char *pai_buf;
689 size_t pai_buf_size = 1024;
690 struct pai_val *paiv = NULL;
691 ssize_t ret;
693 if (!lp_map_acl_inherit(SNUM(conn))) {
694 return NULL;
697 if ((pai_buf = talloc_array(talloc_tos(), char, pai_buf_size)) == NULL) {
698 return NULL;
701 do {
702 ret = SMB_VFS_GETXATTR(conn, fname,
703 SAMBA_POSIX_INHERITANCE_EA_NAME,
704 pai_buf, pai_buf_size);
706 if (ret == -1) {
707 if (errno != ERANGE) {
708 break;
710 /* Buffer too small - enlarge it. */
711 pai_buf_size *= 2;
712 TALLOC_FREE(pai_buf);
713 if (pai_buf_size > 1024*1024) {
714 return NULL; /* Limit malloc to 1mb. */
716 if ((pai_buf = talloc_array(talloc_tos(), char, pai_buf_size)) == NULL)
717 return NULL;
719 } while (ret == -1);
721 DEBUG(10,("load_inherited_info: ret = %lu for file %s\n", (unsigned long)ret, fname));
723 if (ret == -1) {
724 /* No attribute or not supported. */
725 #if defined(ENOATTR)
726 if (errno != ENOATTR)
727 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno) ));
728 #else
729 if (errno != ENOSYS)
730 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno) ));
731 #endif
732 TALLOC_FREE(pai_buf);
733 return NULL;
736 paiv = create_pai_val(pai_buf, ret);
738 if (paiv) {
739 DEBUG(10,("load_inherited_info: ACL type 0x%x for file %s\n",
740 (unsigned int)paiv->sd_type,
741 fname));
744 TALLOC_FREE(pai_buf);
745 return paiv;
748 /****************************************************************************
749 Functions to manipulate the internal ACE format.
750 ****************************************************************************/
752 /****************************************************************************
753 Count a linked list of canonical ACE entries.
754 ****************************************************************************/
756 static size_t count_canon_ace_list( canon_ace *l_head )
758 size_t count = 0;
759 canon_ace *ace;
761 for (ace = l_head; ace; ace = ace->next)
762 count++;
764 return count;
767 /****************************************************************************
768 Free a linked list of canonical ACE entries.
769 ****************************************************************************/
771 static void free_canon_ace_list( canon_ace *l_head )
773 canon_ace *list, *next;
775 for (list = l_head; list; list = next) {
776 next = list->next;
777 DLIST_REMOVE(l_head, list);
778 TALLOC_FREE(list);
782 /****************************************************************************
783 Function to duplicate a canon_ace entry.
784 ****************************************************************************/
786 static canon_ace *dup_canon_ace( canon_ace *src_ace)
788 canon_ace *dst_ace = talloc(talloc_tos(), canon_ace);
790 if (dst_ace == NULL)
791 return NULL;
793 *dst_ace = *src_ace;
794 dst_ace->prev = dst_ace->next = NULL;
795 return dst_ace;
798 /****************************************************************************
799 Print out a canon ace.
800 ****************************************************************************/
802 static void print_canon_ace(canon_ace *pace, int num)
804 dbgtext( "canon_ace index %d. Type = %s ", num, pace->attr == ALLOW_ACE ? "allow" : "deny" );
805 dbgtext( "SID = %s ", sid_string_dbg(&pace->trustee));
806 if (pace->owner_type == UID_ACE) {
807 const char *u_name = uidtoname(pace->unix_ug.uid);
808 dbgtext( "uid %u (%s) ", (unsigned int)pace->unix_ug.uid, u_name );
809 } else if (pace->owner_type == GID_ACE) {
810 char *g_name = gidtoname(pace->unix_ug.gid);
811 dbgtext( "gid %u (%s) ", (unsigned int)pace->unix_ug.gid, g_name );
812 } else
813 dbgtext( "other ");
814 switch (pace->type) {
815 case SMB_ACL_USER:
816 dbgtext( "SMB_ACL_USER ");
817 break;
818 case SMB_ACL_USER_OBJ:
819 dbgtext( "SMB_ACL_USER_OBJ ");
820 break;
821 case SMB_ACL_GROUP:
822 dbgtext( "SMB_ACL_GROUP ");
823 break;
824 case SMB_ACL_GROUP_OBJ:
825 dbgtext( "SMB_ACL_GROUP_OBJ ");
826 break;
827 case SMB_ACL_OTHER:
828 dbgtext( "SMB_ACL_OTHER ");
829 break;
830 default:
831 dbgtext( "MASK " );
832 break;
835 dbgtext( "ace_flags = 0x%x ", (unsigned int)pace->ace_flags);
836 dbgtext( "perms ");
837 dbgtext( "%c", pace->perms & S_IRUSR ? 'r' : '-');
838 dbgtext( "%c", pace->perms & S_IWUSR ? 'w' : '-');
839 dbgtext( "%c\n", pace->perms & S_IXUSR ? 'x' : '-');
842 /****************************************************************************
843 Print out a canon ace list.
844 ****************************************************************************/
846 static void print_canon_ace_list(const char *name, canon_ace *ace_list)
848 int count = 0;
850 if( DEBUGLVL( 10 )) {
851 dbgtext( "print_canon_ace_list: %s\n", name );
852 for (;ace_list; ace_list = ace_list->next, count++)
853 print_canon_ace(ace_list, count );
857 /****************************************************************************
858 Map POSIX ACL perms to canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits).
859 ****************************************************************************/
861 static mode_t convert_permset_to_mode_t(connection_struct *conn, SMB_ACL_PERMSET_T permset)
863 mode_t ret = 0;
865 ret |= (SMB_VFS_SYS_ACL_GET_PERM(conn, permset, SMB_ACL_READ) ? S_IRUSR : 0);
866 ret |= (SMB_VFS_SYS_ACL_GET_PERM(conn, permset, SMB_ACL_WRITE) ? S_IWUSR : 0);
867 ret |= (SMB_VFS_SYS_ACL_GET_PERM(conn, permset, SMB_ACL_EXECUTE) ? S_IXUSR : 0);
869 return ret;
872 /****************************************************************************
873 Map generic UNIX permissions to canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits).
874 ****************************************************************************/
876 static mode_t unix_perms_to_acl_perms(mode_t mode, int r_mask, int w_mask, int x_mask)
878 mode_t ret = 0;
880 if (mode & r_mask)
881 ret |= S_IRUSR;
882 if (mode & w_mask)
883 ret |= S_IWUSR;
884 if (mode & x_mask)
885 ret |= S_IXUSR;
887 return ret;
890 /****************************************************************************
891 Map canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits) to
892 an SMB_ACL_PERMSET_T.
893 ****************************************************************************/
895 static int map_acl_perms_to_permset(connection_struct *conn, mode_t mode, SMB_ACL_PERMSET_T *p_permset)
897 if (SMB_VFS_SYS_ACL_CLEAR_PERMS(conn, *p_permset) == -1)
898 return -1;
899 if (mode & S_IRUSR) {
900 if (SMB_VFS_SYS_ACL_ADD_PERM(conn, *p_permset, SMB_ACL_READ) == -1)
901 return -1;
903 if (mode & S_IWUSR) {
904 if (SMB_VFS_SYS_ACL_ADD_PERM(conn, *p_permset, SMB_ACL_WRITE) == -1)
905 return -1;
907 if (mode & S_IXUSR) {
908 if (SMB_VFS_SYS_ACL_ADD_PERM(conn, *p_permset, SMB_ACL_EXECUTE) == -1)
909 return -1;
911 return 0;
914 /****************************************************************************
915 Function to create owner and group SIDs from a SMB_STRUCT_STAT.
916 ****************************************************************************/
918 void create_file_sids(const SMB_STRUCT_STAT *psbuf, struct dom_sid *powner_sid, struct dom_sid *pgroup_sid)
920 uid_to_sid( powner_sid, psbuf->st_ex_uid );
921 gid_to_sid( pgroup_sid, psbuf->st_ex_gid );
924 /****************************************************************************
925 Merge aces with a common sid - if both are allow or deny, OR the permissions together and
926 delete the second one. If the first is deny, mask the permissions off and delete the allow
927 if the permissions become zero, delete the deny if the permissions are non zero.
928 ****************************************************************************/
930 static void merge_aces( canon_ace **pp_list_head, bool dir_acl)
932 canon_ace *l_head = *pp_list_head;
933 canon_ace *curr_ace_outer;
934 canon_ace *curr_ace_outer_next;
937 * First, merge allow entries with identical SIDs, and deny entries
938 * with identical SIDs.
941 for (curr_ace_outer = l_head; curr_ace_outer; curr_ace_outer = curr_ace_outer_next) {
942 canon_ace *curr_ace;
943 canon_ace *curr_ace_next;
945 curr_ace_outer_next = curr_ace_outer->next; /* Save the link in case we delete. */
947 for (curr_ace = curr_ace_outer->next; curr_ace; curr_ace = curr_ace_next) {
948 bool can_merge = false;
950 curr_ace_next = curr_ace->next; /* Save the link in case of delete. */
952 /* For file ACLs we can merge if the SIDs and ALLOW/DENY
953 * types are the same. For directory acls we must also
954 * ensure the POSIX ACL types are the same.
956 * For the IDMAP_BOTH case, we must not merge
957 * the UID and GID ACE values for same SID
960 if (!dir_acl) {
961 can_merge = (dom_sid_equal(&curr_ace->trustee, &curr_ace_outer->trustee) &&
962 curr_ace->owner_type == curr_ace_outer->owner_type &&
963 (curr_ace->attr == curr_ace_outer->attr));
964 } else {
965 can_merge = (dom_sid_equal(&curr_ace->trustee, &curr_ace_outer->trustee) &&
966 curr_ace->owner_type == curr_ace_outer->owner_type &&
967 (curr_ace->type == curr_ace_outer->type) &&
968 (curr_ace->attr == curr_ace_outer->attr));
971 if (can_merge) {
972 if( DEBUGLVL( 10 )) {
973 dbgtext("merge_aces: Merging ACE's\n");
974 print_canon_ace( curr_ace_outer, 0);
975 print_canon_ace( curr_ace, 0);
978 /* Merge two allow or two deny ACE's. */
980 /* Theoretically we shouldn't merge a dir ACE if
981 * one ACE has the CI flag set, and the other
982 * ACE has the OI flag set, but this is rare
983 * enough we can ignore it. */
985 curr_ace_outer->perms |= curr_ace->perms;
986 curr_ace_outer->ace_flags |= curr_ace->ace_flags;
987 DLIST_REMOVE(l_head, curr_ace);
988 TALLOC_FREE(curr_ace);
989 curr_ace_outer_next = curr_ace_outer->next; /* We may have deleted the link. */
995 * Now go through and mask off allow permissions with deny permissions.
996 * We can delete either the allow or deny here as we know that each SID
997 * appears only once in the list.
1000 for (curr_ace_outer = l_head; curr_ace_outer; curr_ace_outer = curr_ace_outer_next) {
1001 canon_ace *curr_ace;
1002 canon_ace *curr_ace_next;
1004 curr_ace_outer_next = curr_ace_outer->next; /* Save the link in case we delete. */
1006 for (curr_ace = curr_ace_outer->next; curr_ace; curr_ace = curr_ace_next) {
1008 curr_ace_next = curr_ace->next; /* Save the link in case of delete. */
1011 * Subtract ACE's with different entries. Due to the ordering constraints
1012 * we've put on the ACL, we know the deny must be the first one.
1015 if (dom_sid_equal(&curr_ace->trustee, &curr_ace_outer->trustee) &&
1016 (curr_ace->owner_type == curr_ace_outer->owner_type) &&
1017 (curr_ace_outer->attr == DENY_ACE) && (curr_ace->attr == ALLOW_ACE)) {
1019 if( DEBUGLVL( 10 )) {
1020 dbgtext("merge_aces: Masking ACE's\n");
1021 print_canon_ace( curr_ace_outer, 0);
1022 print_canon_ace( curr_ace, 0);
1025 curr_ace->perms &= ~curr_ace_outer->perms;
1027 if (curr_ace->perms == 0) {
1030 * The deny overrides the allow. Remove the allow.
1033 DLIST_REMOVE(l_head, curr_ace);
1034 TALLOC_FREE(curr_ace);
1035 curr_ace_outer_next = curr_ace_outer->next; /* We may have deleted the link. */
1037 } else {
1040 * Even after removing permissions, there
1041 * are still allow permissions - delete the deny.
1042 * It is safe to delete the deny here,
1043 * as we are guarenteed by the deny first
1044 * ordering that all the deny entries for
1045 * this SID have already been merged into one
1046 * before we can get to an allow ace.
1049 DLIST_REMOVE(l_head, curr_ace_outer);
1050 TALLOC_FREE(curr_ace_outer);
1051 break;
1055 } /* end for curr_ace */
1056 } /* end for curr_ace_outer */
1058 /* We may have modified the list. */
1060 *pp_list_head = l_head;
1063 /****************************************************************************
1064 Check if we need to return NT4.x compatible ACL entries.
1065 ****************************************************************************/
1067 bool nt4_compatible_acls(void)
1069 int compat = lp_acl_compatibility();
1071 if (compat == ACL_COMPAT_AUTO) {
1072 enum remote_arch_types ra_type = get_remote_arch();
1074 /* Automatically adapt to client */
1075 return (ra_type <= RA_WINNT);
1076 } else
1077 return (compat == ACL_COMPAT_WINNT);
1081 /****************************************************************************
1082 Map canon_ace perms to permission bits NT.
1083 The attr element is not used here - we only process deny entries on set,
1084 not get. Deny entries are implicit on get with ace->perms = 0.
1085 ****************************************************************************/
1087 uint32_t map_canon_ace_perms(int snum,
1088 enum security_ace_type *pacl_type,
1089 mode_t perms,
1090 bool directory_ace)
1092 uint32_t nt_mask = 0;
1094 *pacl_type = SEC_ACE_TYPE_ACCESS_ALLOWED;
1096 if (lp_acl_map_full_control(snum) && ((perms & ALL_ACE_PERMS) == ALL_ACE_PERMS)) {
1097 if (directory_ace) {
1098 nt_mask = UNIX_DIRECTORY_ACCESS_RWX;
1099 } else {
1100 nt_mask = (UNIX_ACCESS_RWX & ~DELETE_ACCESS);
1102 } else if ((perms & ALL_ACE_PERMS) == (mode_t)0) {
1104 * Windows NT refuses to display ACEs with no permissions in them (but
1105 * they are perfectly legal with Windows 2000). If the ACE has empty
1106 * permissions we cannot use 0, so we use the otherwise unused
1107 * WRITE_OWNER permission, which we ignore when we set an ACL.
1108 * We abstract this into a #define of UNIX_ACCESS_NONE to allow this
1109 * to be changed in the future.
1112 if (nt4_compatible_acls())
1113 nt_mask = UNIX_ACCESS_NONE;
1114 else
1115 nt_mask = 0;
1116 } else {
1117 if (directory_ace) {
1118 nt_mask |= ((perms & S_IRUSR) ? UNIX_DIRECTORY_ACCESS_R : 0 );
1119 nt_mask |= ((perms & S_IWUSR) ? UNIX_DIRECTORY_ACCESS_W : 0 );
1120 nt_mask |= ((perms & S_IXUSR) ? UNIX_DIRECTORY_ACCESS_X : 0 );
1121 } else {
1122 nt_mask |= ((perms & S_IRUSR) ? UNIX_ACCESS_R : 0 );
1123 nt_mask |= ((perms & S_IWUSR) ? UNIX_ACCESS_W : 0 );
1124 nt_mask |= ((perms & S_IXUSR) ? UNIX_ACCESS_X : 0 );
1128 if ((perms & S_IWUSR) && lp_dos_filemode(snum)) {
1129 nt_mask |= (SEC_STD_WRITE_DAC|SEC_STD_WRITE_OWNER|DELETE_ACCESS);
1132 DEBUG(10,("map_canon_ace_perms: Mapped (UNIX) %x to (NT) %x\n",
1133 (unsigned int)perms, (unsigned int)nt_mask ));
1135 return nt_mask;
1138 /****************************************************************************
1139 Map NT perms to a UNIX mode_t.
1140 ****************************************************************************/
1142 #define FILE_SPECIFIC_READ_BITS (FILE_READ_DATA|FILE_READ_EA)
1143 #define FILE_SPECIFIC_WRITE_BITS (FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_WRITE_EA)
1144 #define FILE_SPECIFIC_EXECUTE_BITS (FILE_EXECUTE)
1146 static mode_t map_nt_perms( uint32 *mask, int type)
1148 mode_t mode = 0;
1150 switch(type) {
1151 case S_IRUSR:
1152 if((*mask) & GENERIC_ALL_ACCESS)
1153 mode = S_IRUSR|S_IWUSR|S_IXUSR;
1154 else {
1155 mode |= ((*mask) & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IRUSR : 0;
1156 mode |= ((*mask) & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWUSR : 0;
1157 mode |= ((*mask) & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXUSR : 0;
1159 break;
1160 case S_IRGRP:
1161 if((*mask) & GENERIC_ALL_ACCESS)
1162 mode = S_IRGRP|S_IWGRP|S_IXGRP;
1163 else {
1164 mode |= ((*mask) & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IRGRP : 0;
1165 mode |= ((*mask) & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWGRP : 0;
1166 mode |= ((*mask) & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXGRP : 0;
1168 break;
1169 case S_IROTH:
1170 if((*mask) & GENERIC_ALL_ACCESS)
1171 mode = S_IROTH|S_IWOTH|S_IXOTH;
1172 else {
1173 mode |= ((*mask) & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IROTH : 0;
1174 mode |= ((*mask) & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWOTH : 0;
1175 mode |= ((*mask) & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXOTH : 0;
1177 break;
1180 return mode;
1183 /****************************************************************************
1184 Unpack a struct security_descriptor into a UNIX owner and group.
1185 ****************************************************************************/
1187 NTSTATUS unpack_nt_owners(struct connection_struct *conn,
1188 uid_t *puser, gid_t *pgrp,
1189 uint32 security_info_sent, const struct
1190 security_descriptor *psd)
1192 struct dom_sid owner_sid;
1193 struct dom_sid grp_sid;
1195 *puser = (uid_t)-1;
1196 *pgrp = (gid_t)-1;
1198 if(security_info_sent == 0) {
1199 DEBUG(0,("unpack_nt_owners: no security info sent !\n"));
1200 return NT_STATUS_OK;
1204 * Validate the owner and group SID's.
1207 memset(&owner_sid, '\0', sizeof(owner_sid));
1208 memset(&grp_sid, '\0', sizeof(grp_sid));
1210 DEBUG(5,("unpack_nt_owners: validating owner_sids.\n"));
1213 * Don't immediately fail if the owner sid cannot be validated.
1214 * This may be a group chown only set.
1217 if (security_info_sent & SECINFO_OWNER) {
1218 sid_copy(&owner_sid, psd->owner_sid);
1219 if (!sid_to_uid(&owner_sid, puser)) {
1220 if (lp_force_unknown_acl_user(SNUM(conn))) {
1221 /* this allows take ownership to work
1222 * reasonably */
1223 *puser = get_current_uid(conn);
1224 } else {
1225 DEBUG(3,("unpack_nt_owners: unable to validate"
1226 " owner sid for %s\n",
1227 sid_string_dbg(&owner_sid)));
1228 return NT_STATUS_INVALID_OWNER;
1231 DEBUG(3,("unpack_nt_owners: owner sid mapped to uid %u\n",
1232 (unsigned int)*puser ));
1236 * Don't immediately fail if the group sid cannot be validated.
1237 * This may be an owner chown only set.
1240 if (security_info_sent & SECINFO_GROUP) {
1241 sid_copy(&grp_sid, psd->group_sid);
1242 if (!sid_to_gid( &grp_sid, pgrp)) {
1243 if (lp_force_unknown_acl_user(SNUM(conn))) {
1244 /* this allows take group ownership to work
1245 * reasonably */
1246 *pgrp = get_current_gid(conn);
1247 } else {
1248 DEBUG(3,("unpack_nt_owners: unable to validate"
1249 " group sid.\n"));
1250 return NT_STATUS_INVALID_OWNER;
1253 DEBUG(3,("unpack_nt_owners: group sid mapped to gid %u\n",
1254 (unsigned int)*pgrp));
1257 DEBUG(5,("unpack_nt_owners: owner_sids validated.\n"));
1259 return NT_STATUS_OK;
1262 /****************************************************************************
1263 Ensure the enforced permissions for this share apply.
1264 ****************************************************************************/
1266 static void apply_default_perms(const struct share_params *params,
1267 const bool is_directory, canon_ace *pace,
1268 mode_t type)
1270 mode_t and_bits = (mode_t)0;
1271 mode_t or_bits = (mode_t)0;
1273 /* Get the initial bits to apply. */
1275 if (is_directory) {
1276 and_bits = lp_dir_security_mask(params->service);
1277 or_bits = lp_force_dir_security_mode(params->service);
1278 } else {
1279 and_bits = lp_security_mask(params->service);
1280 or_bits = lp_force_security_mode(params->service);
1283 /* Now bounce them into the S_USR space. */
1284 switch(type) {
1285 case S_IRUSR:
1286 /* Ensure owner has read access. */
1287 pace->perms |= S_IRUSR;
1288 if (is_directory)
1289 pace->perms |= (S_IWUSR|S_IXUSR);
1290 and_bits = unix_perms_to_acl_perms(and_bits, S_IRUSR, S_IWUSR, S_IXUSR);
1291 or_bits = unix_perms_to_acl_perms(or_bits, S_IRUSR, S_IWUSR, S_IXUSR);
1292 break;
1293 case S_IRGRP:
1294 and_bits = unix_perms_to_acl_perms(and_bits, S_IRGRP, S_IWGRP, S_IXGRP);
1295 or_bits = unix_perms_to_acl_perms(or_bits, S_IRGRP, S_IWGRP, S_IXGRP);
1296 break;
1297 case S_IROTH:
1298 and_bits = unix_perms_to_acl_perms(and_bits, S_IROTH, S_IWOTH, S_IXOTH);
1299 or_bits = unix_perms_to_acl_perms(or_bits, S_IROTH, S_IWOTH, S_IXOTH);
1300 break;
1303 pace->perms = ((pace->perms & and_bits)|or_bits);
1306 /****************************************************************************
1307 Check if a given uid/SID is in a group gid/SID. This is probably very
1308 expensive and will need optimisation. A *lot* of optimisation :-). JRA.
1309 ****************************************************************************/
1311 static bool uid_entry_in_group(connection_struct *conn, canon_ace *uid_ace, canon_ace *group_ace )
1313 const char *u_name = NULL;
1315 /* "Everyone" always matches every uid. */
1317 if (dom_sid_equal(&group_ace->trustee, &global_sid_World))
1318 return True;
1321 * if it's the current user, we already have the unix token
1322 * and don't need to do the complex user_in_group_sid() call
1324 if (uid_ace->unix_ug.uid == get_current_uid(conn)) {
1325 const struct security_unix_token *curr_utok = NULL;
1326 size_t i;
1328 if (group_ace->unix_ug.gid == get_current_gid(conn)) {
1329 return True;
1332 curr_utok = get_current_utok(conn);
1333 for (i=0; i < curr_utok->ngroups; i++) {
1334 if (group_ace->unix_ug.gid == curr_utok->groups[i]) {
1335 return True;
1340 /* u_name talloc'ed off tos. */
1341 u_name = uidtoname(uid_ace->unix_ug.uid);
1342 if (!u_name) {
1343 return False;
1347 * user_in_group_sid() uses create_token_from_username()
1348 * which creates an artificial NT token given just a username,
1349 * so this is not reliable for users from foreign domains
1350 * exported by winbindd!
1352 return user_in_group_sid(u_name, &group_ace->trustee);
1355 /****************************************************************************
1356 A well formed POSIX file or default ACL has at least 3 entries, a
1357 SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ.
1358 In addition, the owner must always have at least read access.
1359 When using this call on get_acl, the pst struct is valid and contains
1360 the mode of the file. When using this call on set_acl, the pst struct has
1361 been modified to have a mode containing the default for this file or directory
1362 type.
1363 ****************************************************************************/
1365 static bool ensure_canon_entry_valid(connection_struct *conn, canon_ace **pp_ace,
1366 const struct share_params *params,
1367 const bool is_directory,
1368 const struct dom_sid *pfile_owner_sid,
1369 const struct dom_sid *pfile_grp_sid,
1370 const SMB_STRUCT_STAT *pst,
1371 bool setting_acl)
1373 canon_ace *pace;
1374 canon_ace *pace_user = NULL;
1375 canon_ace *pace_group = NULL;
1376 canon_ace *pace_other = NULL;
1378 for (pace = *pp_ace; pace; pace = pace->next) {
1379 if (pace->type == SMB_ACL_USER_OBJ) {
1381 if (setting_acl)
1382 apply_default_perms(params, is_directory, pace, S_IRUSR);
1383 pace_user = pace;
1385 } else if (pace->type == SMB_ACL_GROUP_OBJ) {
1388 * Ensure create mask/force create mode is respected on set.
1391 if (setting_acl)
1392 apply_default_perms(params, is_directory, pace, S_IRGRP);
1393 pace_group = pace;
1395 } else if (pace->type == SMB_ACL_OTHER) {
1398 * Ensure create mask/force create mode is respected on set.
1401 if (setting_acl)
1402 apply_default_perms(params, is_directory, pace, S_IROTH);
1403 pace_other = pace;
1407 if (!pace_user) {
1408 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1409 DEBUG(0,("ensure_canon_entry_valid: malloc fail.\n"));
1410 return False;
1413 ZERO_STRUCTP(pace);
1414 pace->type = SMB_ACL_USER_OBJ;
1415 pace->owner_type = UID_ACE;
1416 pace->unix_ug.uid = pst->st_ex_uid;
1417 pace->trustee = *pfile_owner_sid;
1418 pace->attr = ALLOW_ACE;
1419 /* Start with existing permissions, principle of least
1420 surprises for the user. */
1421 pace->perms = pst->st_ex_mode;
1423 if (setting_acl) {
1424 /* See if the owning user is in any of the other groups in
1425 the ACE, or if there's a matching user entry (by uid
1426 or in the case of ID_TYPE_BOTH by SID).
1427 If so, OR in the permissions from that entry. */
1429 canon_ace *pace_iter;
1431 for (pace_iter = *pp_ace; pace_iter; pace_iter = pace_iter->next) {
1432 if (pace_iter->type == SMB_ACL_USER &&
1433 pace_iter->unix_ug.uid == pace->unix_ug.uid) {
1434 pace->perms |= pace_iter->perms;
1435 } else if (pace_iter->type == SMB_ACL_GROUP_OBJ || pace_iter->type == SMB_ACL_GROUP) {
1436 if (dom_sid_equal(&pace->trustee, &pace_iter->trustee)) {
1437 pace->perms |= pace_iter->perms;
1438 } else if (uid_entry_in_group(conn, pace, pace_iter)) {
1439 pace->perms |= pace_iter->perms;
1444 if (pace->perms == 0) {
1445 /* If we only got an "everyone" perm, just use that. */
1446 if (pace_other)
1447 pace->perms = pace_other->perms;
1450 apply_default_perms(params, is_directory, pace, S_IRUSR);
1451 } else {
1452 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IRUSR, S_IWUSR, S_IXUSR);
1455 DLIST_ADD(*pp_ace, pace);
1456 pace_user = pace;
1459 if (!pace_group) {
1460 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1461 DEBUG(0,("ensure_canon_entry_valid: malloc fail.\n"));
1462 return False;
1465 ZERO_STRUCTP(pace);
1466 pace->type = SMB_ACL_GROUP_OBJ;
1467 pace->owner_type = GID_ACE;
1468 pace->unix_ug.gid = pst->st_ex_gid;
1469 pace->trustee = *pfile_grp_sid;
1470 pace->attr = ALLOW_ACE;
1471 if (setting_acl) {
1472 /* If we only got an "everyone" perm, just use that. */
1473 if (pace_other)
1474 pace->perms = pace_other->perms;
1475 else
1476 pace->perms = 0;
1477 apply_default_perms(params, is_directory, pace, S_IRGRP);
1478 } else {
1479 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IRGRP, S_IWGRP, S_IXGRP);
1482 DLIST_ADD(*pp_ace, pace);
1483 pace_group = pace;
1486 if (!pace_other) {
1487 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1488 DEBUG(0,("ensure_canon_entry_valid: malloc fail.\n"));
1489 return False;
1492 ZERO_STRUCTP(pace);
1493 pace->type = SMB_ACL_OTHER;
1494 pace->owner_type = WORLD_ACE;
1495 pace->unix_ug.world = -1;
1496 pace->trustee = global_sid_World;
1497 pace->attr = ALLOW_ACE;
1498 if (setting_acl) {
1499 pace->perms = 0;
1500 apply_default_perms(params, is_directory, pace, S_IROTH);
1501 } else
1502 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IROTH, S_IWOTH, S_IXOTH);
1504 DLIST_ADD(*pp_ace, pace);
1505 pace_other = pace;
1508 if (setting_acl) {
1509 /* Ensure when setting a POSIX ACL, that the uid for a
1510 SMB_ACL_USER_OBJ ACE (the owner ACE entry) has a duplicate
1511 permission entry as an SMB_ACL_USER, and a gid for a
1512 SMB_ACL_GROUP_OBJ ACE (the primary group ACE entry) also has
1513 a duplicate permission entry as an SMB_ACL_GROUP. If not,
1514 then if the ownership or group ownership of this file or
1515 directory gets changed, the user or group can lose their
1516 access. */
1517 bool got_duplicate_user = false;
1518 bool got_duplicate_group = false;
1520 for (pace = *pp_ace; pace; pace = pace->next) {
1521 if (pace->type == SMB_ACL_USER &&
1522 pace->unix_ug.uid == pace_user->unix_ug.uid) {
1523 /* Already got one. */
1524 got_duplicate_user = true;
1525 } else if (pace->type == SMB_ACL_GROUP &&
1526 pace->unix_ug.gid == pace_user->unix_ug.gid) {
1527 /* Already got one. */
1528 got_duplicate_group = true;
1529 } else if ((pace->type == SMB_ACL_GROUP)
1530 && (dom_sid_equal(&pace->trustee, &pace_user->trustee))) {
1531 /* If the SID owning the file appears
1532 * in a group entry, then we have
1533 * enough duplication, they will still
1534 * have access */
1535 got_duplicate_user = true;
1539 /* If the SID is equal for the user and group that we need
1540 to add the duplicate for, add only the group */
1541 if (!got_duplicate_user && !got_duplicate_group
1542 && dom_sid_equal(&pace_group->trustee,
1543 &pace_user->trustee)) {
1544 /* Add a duplicate SMB_ACL_GROUP entry, this
1545 * will cover the owning SID as well, as it
1546 * will always be mapped to both a uid and
1547 * gid. */
1549 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1550 DEBUG(0,("ensure_canon_entry_valid: talloc fail.\n"));
1551 return false;
1554 ZERO_STRUCTP(pace);
1555 pace->type = SMB_ACL_GROUP;;
1556 pace->owner_type = GID_ACE;
1557 pace->unix_ug.gid = pace_group->unix_ug.gid;
1558 pace->trustee = pace_group->trustee;
1559 pace->attr = pace_group->attr;
1560 pace->perms = pace_group->perms;
1562 DLIST_ADD(*pp_ace, pace);
1564 /* We're done here, make sure the
1565 statements below are not executed. */
1566 got_duplicate_user = true;
1567 got_duplicate_group = true;
1570 if (!got_duplicate_user) {
1571 /* Add a duplicate SMB_ACL_USER entry. */
1572 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1573 DEBUG(0,("ensure_canon_entry_valid: talloc fail.\n"));
1574 return false;
1577 ZERO_STRUCTP(pace);
1578 pace->type = SMB_ACL_USER;;
1579 pace->owner_type = UID_ACE;
1580 pace->unix_ug.uid = pace_user->unix_ug.uid;
1581 pace->trustee = pace_user->trustee;
1582 pace->attr = pace_user->attr;
1583 pace->perms = pace_user->perms;
1585 DLIST_ADD(*pp_ace, pace);
1587 got_duplicate_user = true;
1590 if (!got_duplicate_group) {
1591 /* Add a duplicate SMB_ACL_GROUP entry. */
1592 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1593 DEBUG(0,("ensure_canon_entry_valid: talloc fail.\n"));
1594 return false;
1597 ZERO_STRUCTP(pace);
1598 pace->type = SMB_ACL_GROUP;;
1599 pace->owner_type = GID_ACE;
1600 pace->unix_ug.gid = pace_group->unix_ug.gid;
1601 pace->trustee = pace_group->trustee;
1602 pace->attr = pace_group->attr;
1603 pace->perms = pace_group->perms;
1605 DLIST_ADD(*pp_ace, pace);
1607 got_duplicate_group = true;
1612 return True;
1615 /****************************************************************************
1616 Check if a POSIX ACL has the required SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ entries.
1617 If it does not have them, check if there are any entries where the trustee is the
1618 file owner or the owning group, and map these to SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ.
1619 Note we must not do this to default directory ACLs.
1620 ****************************************************************************/
1622 static void check_owning_objs(canon_ace *ace, struct dom_sid *pfile_owner_sid, struct dom_sid *pfile_grp_sid)
1624 bool got_user_obj, got_group_obj;
1625 canon_ace *current_ace;
1626 int i, entries;
1628 entries = count_canon_ace_list(ace);
1629 got_user_obj = False;
1630 got_group_obj = False;
1632 for (i=0, current_ace = ace; i < entries; i++, current_ace = current_ace->next) {
1633 if (current_ace->type == SMB_ACL_USER_OBJ)
1634 got_user_obj = True;
1635 else if (current_ace->type == SMB_ACL_GROUP_OBJ)
1636 got_group_obj = True;
1638 if (got_user_obj && got_group_obj) {
1639 DEBUG(10,("check_owning_objs: ACL had owning user/group entries.\n"));
1640 return;
1643 for (i=0, current_ace = ace; i < entries; i++, current_ace = current_ace->next) {
1644 if (!got_user_obj && current_ace->owner_type == UID_ACE &&
1645 dom_sid_equal(&current_ace->trustee, pfile_owner_sid)) {
1646 current_ace->type = SMB_ACL_USER_OBJ;
1647 got_user_obj = True;
1649 if (!got_group_obj && current_ace->owner_type == GID_ACE &&
1650 dom_sid_equal(&current_ace->trustee, pfile_grp_sid)) {
1651 current_ace->type = SMB_ACL_GROUP_OBJ;
1652 got_group_obj = True;
1655 if (!got_user_obj)
1656 DEBUG(10,("check_owning_objs: ACL is missing an owner entry.\n"));
1657 if (!got_group_obj)
1658 DEBUG(10,("check_owning_objs: ACL is missing an owning group entry.\n"));
1661 static bool add_current_ace_to_acl(files_struct *fsp, struct security_ace *psa,
1662 canon_ace **file_ace, canon_ace **dir_ace,
1663 bool *got_file_allow, bool *got_dir_allow,
1664 bool *all_aces_are_inherit_only,
1665 canon_ace *current_ace)
1669 * Map the given NT permissions into a UNIX mode_t containing only
1670 * S_I(R|W|X)USR bits.
1673 current_ace->perms |= map_nt_perms( &psa->access_mask, S_IRUSR);
1674 current_ace->attr = (psa->type == SEC_ACE_TYPE_ACCESS_ALLOWED) ? ALLOW_ACE : DENY_ACE;
1676 /* Store the ace_flag. */
1677 current_ace->ace_flags = psa->flags;
1680 * Now add the created ace to either the file list, the directory
1681 * list, or both. We *MUST* preserve the order here (hence we use
1682 * DLIST_ADD_END) as NT ACLs are order dependent.
1685 if (fsp->is_directory) {
1688 * We can only add to the default POSIX ACE list if the ACE is
1689 * designed to be inherited by both files and directories.
1692 if ((psa->flags & (SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT)) ==
1693 (SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT)) {
1695 canon_ace *current_dir_ace = current_ace;
1696 DLIST_ADD_END(*dir_ace, current_ace, canon_ace *);
1699 * Note if this was an allow ace. We can't process
1700 * any further deny ace's after this.
1703 if (current_ace->attr == ALLOW_ACE)
1704 *got_dir_allow = True;
1706 if ((current_ace->attr == DENY_ACE) && *got_dir_allow) {
1707 DEBUG(0,("add_current_ace_to_acl: "
1708 "malformed ACL in "
1709 "inheritable ACL! Deny entry "
1710 "after Allow entry. Failing "
1711 "to set on file %s.\n",
1712 fsp_str_dbg(fsp)));
1713 return False;
1716 if( DEBUGLVL( 10 )) {
1717 dbgtext("add_current_ace_to_acl: adding dir ACL:\n");
1718 print_canon_ace( current_ace, 0);
1722 * If this is not an inherit only ACE we need to add a duplicate
1723 * to the file acl.
1726 if (!(psa->flags & SEC_ACE_FLAG_INHERIT_ONLY)) {
1727 canon_ace *dup_ace = dup_canon_ace(current_ace);
1729 if (!dup_ace) {
1730 DEBUG(0,("add_current_ace_to_acl: malloc fail !\n"));
1731 return False;
1735 * We must not free current_ace here as its
1736 * pointer is now owned by the dir_ace list.
1738 current_ace = dup_ace;
1739 /* We've essentially split this ace into two,
1740 * and added the ace with inheritance request
1741 * bits to the directory ACL. Drop those bits for
1742 * the ACE we're adding to the file list. */
1743 current_ace->ace_flags &= ~(SEC_ACE_FLAG_OBJECT_INHERIT|
1744 SEC_ACE_FLAG_CONTAINER_INHERIT|
1745 SEC_ACE_FLAG_INHERIT_ONLY);
1746 } else {
1748 * We must not free current_ace here as its
1749 * pointer is now owned by the dir_ace list.
1751 current_ace = NULL;
1755 * current_ace is now either owned by file_ace
1756 * or is NULL. We can safely operate on current_dir_ace
1757 * to treat mapping for default acl entries differently
1758 * than access acl entries.
1761 if (current_dir_ace->owner_type == UID_ACE) {
1763 * We already decided above this is a uid,
1764 * for default acls ace's only CREATOR_OWNER
1765 * maps to ACL_USER_OBJ. All other uid
1766 * ace's are ACL_USER.
1768 if (dom_sid_equal(&current_dir_ace->trustee,
1769 &global_sid_Creator_Owner)) {
1770 current_dir_ace->type = SMB_ACL_USER_OBJ;
1771 } else {
1772 current_dir_ace->type = SMB_ACL_USER;
1776 if (current_dir_ace->owner_type == GID_ACE) {
1778 * We already decided above this is a gid,
1779 * for default acls ace's only CREATOR_GROUP
1780 * maps to ACL_GROUP_OBJ. All other uid
1781 * ace's are ACL_GROUP.
1783 if (dom_sid_equal(&current_dir_ace->trustee,
1784 &global_sid_Creator_Group)) {
1785 current_dir_ace->type = SMB_ACL_GROUP_OBJ;
1786 } else {
1787 current_dir_ace->type = SMB_ACL_GROUP;
1794 * Only add to the file ACL if not inherit only.
1797 if (current_ace && !(psa->flags & SEC_ACE_FLAG_INHERIT_ONLY)) {
1798 DLIST_ADD_END(*file_ace, current_ace, canon_ace *);
1801 * Note if this was an allow ace. We can't process
1802 * any further deny ace's after this.
1805 if (current_ace->attr == ALLOW_ACE)
1806 *got_file_allow = True;
1808 if ((current_ace->attr == DENY_ACE) && got_file_allow) {
1809 DEBUG(0,("add_current_ace_to_acl: malformed "
1810 "ACL in file ACL ! Deny entry after "
1811 "Allow entry. Failing to set on file "
1812 "%s.\n", fsp_str_dbg(fsp)));
1813 return False;
1816 if( DEBUGLVL( 10 )) {
1817 dbgtext("add_current_ace_to_acl: adding file ACL:\n");
1818 print_canon_ace( current_ace, 0);
1820 *all_aces_are_inherit_only = False;
1822 * We must not free current_ace here as its
1823 * pointer is now owned by the file_ace list.
1825 current_ace = NULL;
1829 * Free if ACE was not added.
1832 TALLOC_FREE(current_ace);
1833 return true;
1836 /****************************************************************************
1837 Unpack a struct security_descriptor into two canonical ace lists.
1838 ****************************************************************************/
1840 static bool create_canon_ace_lists(files_struct *fsp,
1841 const SMB_STRUCT_STAT *pst,
1842 struct dom_sid *pfile_owner_sid,
1843 struct dom_sid *pfile_grp_sid,
1844 canon_ace **ppfile_ace,
1845 canon_ace **ppdir_ace,
1846 const struct security_acl *dacl)
1848 bool all_aces_are_inherit_only = (fsp->is_directory ? True : False);
1849 canon_ace *file_ace = NULL;
1850 canon_ace *dir_ace = NULL;
1851 canon_ace *current_ace = NULL;
1852 bool got_dir_allow = False;
1853 bool got_file_allow = False;
1854 int i, j;
1856 *ppfile_ace = NULL;
1857 *ppdir_ace = NULL;
1860 * Convert the incoming ACL into a more regular form.
1863 for(i = 0; i < dacl->num_aces; i++) {
1864 struct security_ace *psa = &dacl->aces[i];
1866 if((psa->type != SEC_ACE_TYPE_ACCESS_ALLOWED) && (psa->type != SEC_ACE_TYPE_ACCESS_DENIED)) {
1867 DEBUG(3,("create_canon_ace_lists: unable to set anything but an ALLOW or DENY ACE.\n"));
1868 return False;
1871 if (nt4_compatible_acls()) {
1873 * The security mask may be UNIX_ACCESS_NONE which should map into
1874 * no permissions (we overload the WRITE_OWNER bit for this) or it
1875 * should be one of the ALL/EXECUTE/READ/WRITE bits. Arrange for this
1876 * to be so. Any other bits override the UNIX_ACCESS_NONE bit.
1880 * Convert GENERIC bits to specific bits.
1883 se_map_generic(&psa->access_mask, &file_generic_mapping);
1885 psa->access_mask &= (UNIX_ACCESS_NONE|FILE_ALL_ACCESS);
1887 if(psa->access_mask != UNIX_ACCESS_NONE)
1888 psa->access_mask &= ~UNIX_ACCESS_NONE;
1893 * Deal with the fact that NT 4.x re-writes the canonical format
1894 * that we return for default ACLs. If a directory ACE is identical
1895 * to a inherited directory ACE then NT changes the bits so that the
1896 * first ACE is set to OI|IO and the second ACE for this SID is set
1897 * to CI. We need to repair this. JRA.
1900 for(i = 0; i < dacl->num_aces; i++) {
1901 struct security_ace *psa1 = &dacl->aces[i];
1903 for (j = i + 1; j < dacl->num_aces; j++) {
1904 struct security_ace *psa2 = &dacl->aces[j];
1906 if (psa1->access_mask != psa2->access_mask)
1907 continue;
1909 if (!dom_sid_equal(&psa1->trustee, &psa2->trustee))
1910 continue;
1913 * Ok - permission bits and SIDs are equal.
1914 * Check if flags were re-written.
1917 if (psa1->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
1919 psa1->flags |= (psa2->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT));
1920 psa2->flags &= ~(SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT);
1922 } else if (psa2->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
1924 psa2->flags |= (psa1->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT));
1925 psa1->flags &= ~(SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT);
1931 for(i = 0; i < dacl->num_aces; i++) {
1932 struct security_ace *psa = &dacl->aces[i];
1935 * Create a canon_ace entry representing this NT DACL ACE.
1938 if ((current_ace = talloc(talloc_tos(), canon_ace)) == NULL) {
1939 free_canon_ace_list(file_ace);
1940 free_canon_ace_list(dir_ace);
1941 DEBUG(0,("create_canon_ace_lists: malloc fail.\n"));
1942 return False;
1945 ZERO_STRUCTP(current_ace);
1947 sid_copy(&current_ace->trustee, &psa->trustee);
1950 * Try and work out if the SID is a user or group
1951 * as we need to flag these differently for POSIX.
1952 * Note what kind of a POSIX ACL this should map to.
1955 if( dom_sid_equal(&current_ace->trustee, &global_sid_World)) {
1956 current_ace->owner_type = WORLD_ACE;
1957 current_ace->unix_ug.world = -1;
1958 current_ace->type = SMB_ACL_OTHER;
1959 } else if (dom_sid_equal(&current_ace->trustee, &global_sid_Creator_Owner)) {
1960 current_ace->owner_type = UID_ACE;
1961 current_ace->unix_ug.uid = pst->st_ex_uid;
1962 current_ace->type = SMB_ACL_USER_OBJ;
1965 * The Creator Owner entry only specifies inheritable permissions,
1966 * never access permissions. WinNT doesn't always set the ACE to
1967 * INHERIT_ONLY, though.
1970 psa->flags |= SEC_ACE_FLAG_INHERIT_ONLY;
1972 } else if (dom_sid_equal(&current_ace->trustee, &global_sid_Creator_Group)) {
1973 current_ace->owner_type = GID_ACE;
1974 current_ace->unix_ug.gid = pst->st_ex_gid;
1975 current_ace->type = SMB_ACL_GROUP_OBJ;
1978 * The Creator Group entry only specifies inheritable permissions,
1979 * never access permissions. WinNT doesn't always set the ACE to
1980 * INHERIT_ONLY, though.
1982 psa->flags |= SEC_ACE_FLAG_INHERIT_ONLY;
1984 } else {
1985 struct unixid unixid;
1987 if (!sids_to_unixids(&current_ace->trustee, 1, &unixid)) {
1988 free_canon_ace_list(file_ace);
1989 free_canon_ace_list(dir_ace);
1990 TALLOC_FREE(current_ace);
1991 DEBUG(0, ("create_canon_ace_lists: sids_to_unixids "
1992 "failed for %s (allocation failure)\n",
1993 sid_string_dbg(&current_ace->trustee)));
1994 return false;
1997 if (unixid.type == ID_TYPE_BOTH) {
1998 /* If it's the owning user, this is a
1999 * user_obj, not a user. This way, we
2000 * get a valid ACL for groups that own
2001 * files, without putting user ACL
2002 * entries in for groups otherwise */
2003 if (unixid.id == pst->st_ex_uid) {
2004 current_ace->owner_type = UID_ACE;
2005 current_ace->unix_ug.uid = unixid.id;
2006 current_ace->type = SMB_ACL_USER_OBJ;
2008 /* Add the user object to the posix ACL,
2009 and proceed to the group mapping
2010 below. This handles the talloc_free
2011 of current_ace if not added for some
2012 reason */
2013 if (!add_current_ace_to_acl(fsp,
2014 psa,
2015 &file_ace,
2016 &dir_ace,
2017 &got_file_allow,
2018 &got_dir_allow,
2019 &all_aces_are_inherit_only,
2020 current_ace)) {
2021 free_canon_ace_list(file_ace);
2022 free_canon_ace_list(dir_ace);
2023 return false;
2026 if ((current_ace = talloc(talloc_tos(),
2027 canon_ace)) == NULL) {
2028 free_canon_ace_list(file_ace);
2029 free_canon_ace_list(dir_ace);
2030 DEBUG(0,("create_canon_ace_lists: "
2031 "malloc fail.\n"));
2032 return False;
2035 ZERO_STRUCTP(current_ace);
2038 sid_copy(&current_ace->trustee, &psa->trustee);
2040 current_ace->unix_ug.gid = unixid.id;
2041 current_ace->owner_type = GID_ACE;
2042 /* If it's the primary group, this is a
2043 group_obj, not a group. */
2044 if (current_ace->unix_ug.gid == pst->st_ex_gid) {
2045 current_ace->type = SMB_ACL_GROUP_OBJ;
2046 } else {
2047 current_ace->type = SMB_ACL_GROUP;
2050 } else if (unixid.type == ID_TYPE_UID) {
2051 current_ace->owner_type = UID_ACE;
2052 current_ace->unix_ug.uid = unixid.id;
2053 /* If it's the owning user, this is a user_obj,
2054 not a user. */
2055 if (current_ace->unix_ug.uid == pst->st_ex_uid) {
2056 current_ace->type = SMB_ACL_USER_OBJ;
2057 } else {
2058 current_ace->type = SMB_ACL_USER;
2060 } else if (unixid.type == ID_TYPE_GID) {
2061 current_ace->unix_ug.gid = unixid.id;
2062 current_ace->owner_type = GID_ACE;
2063 /* If it's the primary group, this is a
2064 group_obj, not a group. */
2065 if (current_ace->unix_ug.gid == pst->st_ex_gid) {
2066 current_ace->type = SMB_ACL_GROUP_OBJ;
2067 } else {
2068 current_ace->type = SMB_ACL_GROUP;
2070 } else {
2072 * Silently ignore map failures in non-mappable SIDs (NT Authority, BUILTIN etc).
2075 if (non_mappable_sid(&psa->trustee)) {
2076 DEBUG(10, ("create_canon_ace_lists: ignoring "
2077 "non-mappable SID %s\n",
2078 sid_string_dbg(&psa->trustee)));
2079 TALLOC_FREE(current_ace);
2080 continue;
2083 if (lp_force_unknown_acl_user(SNUM(fsp->conn))) {
2084 DEBUG(10, ("create_canon_ace_lists: ignoring "
2085 "unknown or foreign SID %s\n",
2086 sid_string_dbg(&psa->trustee)));
2087 TALLOC_FREE(current_ace);
2088 continue;
2091 free_canon_ace_list(file_ace);
2092 free_canon_ace_list(dir_ace);
2093 DEBUG(0, ("create_canon_ace_lists: unable to map SID "
2094 "%s to uid or gid.\n",
2095 sid_string_dbg(&current_ace->trustee)));
2096 TALLOC_FREE(current_ace);
2097 return false;
2101 /* handles the talloc_free of current_ace if not added for some reason */
2102 if (!add_current_ace_to_acl(fsp, psa, &file_ace, &dir_ace,
2103 &got_file_allow, &got_dir_allow,
2104 &all_aces_are_inherit_only, current_ace)) {
2105 free_canon_ace_list(file_ace);
2106 free_canon_ace_list(dir_ace);
2107 return false;
2111 if (fsp->is_directory && all_aces_are_inherit_only) {
2113 * Windows 2000 is doing one of these weird 'inherit acl'
2114 * traverses to conserve NTFS ACL resources. Just pretend
2115 * there was no DACL sent. JRA.
2118 DEBUG(10,("create_canon_ace_lists: Win2k inherit acl traverse. Ignoring DACL.\n"));
2119 free_canon_ace_list(file_ace);
2120 free_canon_ace_list(dir_ace);
2121 file_ace = NULL;
2122 dir_ace = NULL;
2123 } else {
2125 * Check if we have SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ entries in
2126 * the file ACL. If we don't have them, check if any SMB_ACL_USER/SMB_ACL_GROUP
2127 * entries can be converted to *_OBJ. Don't do this for the default
2128 * ACL, we will create them separately for this if needed inside
2129 * ensure_canon_entry_valid().
2131 if (file_ace) {
2132 check_owning_objs(file_ace, pfile_owner_sid, pfile_grp_sid);
2136 *ppfile_ace = file_ace;
2137 *ppdir_ace = dir_ace;
2139 return True;
2142 /****************************************************************************
2143 ASCII art time again... JRA :-).
2145 We have 4 cases to process when moving from an NT ACL to a POSIX ACL. Firstly,
2146 we insist the ACL is in canonical form (ie. all DENY entries preceede ALLOW
2147 entries). Secondly, the merge code has ensured that all duplicate SID entries for
2148 allow or deny have been merged, so the same SID can only appear once in the deny
2149 list or once in the allow list.
2151 We then process as follows :
2153 ---------------------------------------------------------------------------
2154 First pass - look for a Everyone DENY entry.
2156 If it is deny all (rwx) trunate the list at this point.
2157 Else, walk the list from this point and use the deny permissions of this
2158 entry as a mask on all following allow entries. Finally, delete
2159 the Everyone DENY entry (we have applied it to everything possible).
2161 In addition, in this pass we remove any DENY entries that have
2162 no permissions (ie. they are a DENY nothing).
2163 ---------------------------------------------------------------------------
2164 Second pass - only deal with deny user entries.
2166 DENY user1 (perms XXX)
2168 new_perms = 0
2169 for all following allow group entries where user1 is in group
2170 new_perms |= group_perms;
2172 user1 entry perms = new_perms & ~ XXX;
2174 Convert the deny entry to an allow entry with the new perms and
2175 push to the end of the list. Note if the user was in no groups
2176 this maps to a specific allow nothing entry for this user.
2178 The common case from the NT ACL choser (userX deny all) is
2179 optimised so we don't do the group lookup - we just map to
2180 an allow nothing entry.
2182 What we're doing here is inferring the allow permissions the
2183 person setting the ACE on user1 wanted by looking at the allow
2184 permissions on the groups the user is currently in. This will
2185 be a snapshot, depending on group membership but is the best
2186 we can do and has the advantage of failing closed rather than
2187 open.
2188 ---------------------------------------------------------------------------
2189 Third pass - only deal with deny group entries.
2191 DENY group1 (perms XXX)
2193 for all following allow user entries where user is in group1
2194 user entry perms = user entry perms & ~ XXX;
2196 If there is a group Everyone allow entry with permissions YYY,
2197 convert the group1 entry to an allow entry and modify its
2198 permissions to be :
2200 new_perms = YYY & ~ XXX
2202 and push to the end of the list.
2204 If there is no group Everyone allow entry then convert the
2205 group1 entry to a allow nothing entry and push to the end of the list.
2207 Note that the common case from the NT ACL choser (groupX deny all)
2208 cannot be optimised here as we need to modify user entries who are
2209 in the group to change them to a deny all also.
2211 What we're doing here is modifying the allow permissions of
2212 user entries (which are more specific in POSIX ACLs) to mask
2213 out the explicit deny set on the group they are in. This will
2214 be a snapshot depending on current group membership but is the
2215 best we can do and has the advantage of failing closed rather
2216 than open.
2217 ---------------------------------------------------------------------------
2218 Fourth pass - cope with cumulative permissions.
2220 for all allow user entries, if there exists an allow group entry with
2221 more permissive permissions, and the user is in that group, rewrite the
2222 allow user permissions to contain both sets of permissions.
2224 Currently the code for this is #ifdef'ed out as these semantics make
2225 no sense to me. JRA.
2226 ---------------------------------------------------------------------------
2228 Note we *MUST* do the deny user pass first as this will convert deny user
2229 entries into allow user entries which can then be processed by the deny
2230 group pass.
2232 The above algorithm took a *lot* of thinking about - hence this
2233 explaination :-). JRA.
2234 ****************************************************************************/
2236 /****************************************************************************
2237 Process a canon_ace list entries. This is very complex code. We need
2238 to go through and remove the "deny" permissions from any allow entry that matches
2239 the id of this entry. We have already refused any NT ACL that wasn't in correct
2240 order (DENY followed by ALLOW). If any allow entry ends up with zero permissions,
2241 we just remove it (to fail safe). We have already removed any duplicate ace
2242 entries. Treat an "Everyone" DENY_ACE as a special case - use it to mask all
2243 allow entries.
2244 ****************************************************************************/
2246 static void process_deny_list(connection_struct *conn, canon_ace **pp_ace_list )
2248 canon_ace *ace_list = *pp_ace_list;
2249 canon_ace *curr_ace = NULL;
2250 canon_ace *curr_ace_next = NULL;
2252 /* Pass 1 above - look for an Everyone, deny entry. */
2254 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2255 canon_ace *allow_ace_p;
2257 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2259 if (curr_ace->attr != DENY_ACE)
2260 continue;
2262 if (curr_ace->perms == (mode_t)0) {
2264 /* Deny nothing entry - delete. */
2266 DLIST_REMOVE(ace_list, curr_ace);
2267 continue;
2270 if (!dom_sid_equal(&curr_ace->trustee, &global_sid_World))
2271 continue;
2273 /* JRATEST - assert. */
2274 SMB_ASSERT(curr_ace->owner_type == WORLD_ACE);
2276 if (curr_ace->perms == ALL_ACE_PERMS) {
2279 * Optimisation. This is a DENY_ALL to Everyone. Truncate the
2280 * list at this point including this entry.
2283 canon_ace *prev_entry = DLIST_PREV(curr_ace);
2285 free_canon_ace_list( curr_ace );
2286 if (prev_entry)
2287 DLIST_REMOVE(ace_list, prev_entry);
2288 else {
2289 /* We deleted the entire list. */
2290 ace_list = NULL;
2292 break;
2295 for (allow_ace_p = curr_ace->next; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2298 * Only mask off allow entries.
2301 if (allow_ace_p->attr != ALLOW_ACE)
2302 continue;
2304 allow_ace_p->perms &= ~curr_ace->perms;
2308 * Now it's been applied, remove it.
2311 DLIST_REMOVE(ace_list, curr_ace);
2314 /* Pass 2 above - deal with deny user entries. */
2316 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2317 mode_t new_perms = (mode_t)0;
2318 canon_ace *allow_ace_p;
2320 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2322 if (curr_ace->attr != DENY_ACE)
2323 continue;
2325 if (curr_ace->owner_type != UID_ACE)
2326 continue;
2328 if (curr_ace->perms == ALL_ACE_PERMS) {
2331 * Optimisation - this is a deny everything to this user.
2332 * Convert to an allow nothing and push to the end of the list.
2335 curr_ace->attr = ALLOW_ACE;
2336 curr_ace->perms = (mode_t)0;
2337 DLIST_DEMOTE(ace_list, curr_ace, canon_ace *);
2338 continue;
2341 for (allow_ace_p = curr_ace->next; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2343 if (allow_ace_p->attr != ALLOW_ACE)
2344 continue;
2346 /* We process GID_ACE and WORLD_ACE entries only. */
2348 if (allow_ace_p->owner_type == UID_ACE)
2349 continue;
2351 if (uid_entry_in_group(conn, curr_ace, allow_ace_p))
2352 new_perms |= allow_ace_p->perms;
2356 * Convert to a allow entry, modify the perms and push to the end
2357 * of the list.
2360 curr_ace->attr = ALLOW_ACE;
2361 curr_ace->perms = (new_perms & ~curr_ace->perms);
2362 DLIST_DEMOTE(ace_list, curr_ace, canon_ace *);
2365 /* Pass 3 above - deal with deny group entries. */
2367 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2368 canon_ace *allow_ace_p;
2369 canon_ace *allow_everyone_p = NULL;
2371 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2373 if (curr_ace->attr != DENY_ACE)
2374 continue;
2376 if (curr_ace->owner_type != GID_ACE)
2377 continue;
2379 for (allow_ace_p = curr_ace->next; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2381 if (allow_ace_p->attr != ALLOW_ACE)
2382 continue;
2384 /* Store a pointer to the Everyone allow, if it exists. */
2385 if (allow_ace_p->owner_type == WORLD_ACE)
2386 allow_everyone_p = allow_ace_p;
2388 /* We process UID_ACE entries only. */
2390 if (allow_ace_p->owner_type != UID_ACE)
2391 continue;
2393 /* Mask off the deny group perms. */
2395 if (uid_entry_in_group(conn, allow_ace_p, curr_ace))
2396 allow_ace_p->perms &= ~curr_ace->perms;
2400 * Convert the deny to an allow with the correct perms and
2401 * push to the end of the list.
2404 curr_ace->attr = ALLOW_ACE;
2405 if (allow_everyone_p)
2406 curr_ace->perms = allow_everyone_p->perms & ~curr_ace->perms;
2407 else
2408 curr_ace->perms = (mode_t)0;
2409 DLIST_DEMOTE(ace_list, curr_ace, canon_ace *);
2412 /* Doing this fourth pass allows Windows semantics to be layered
2413 * on top of POSIX semantics. I'm not sure if this is desirable.
2414 * For example, in W2K ACLs there is no way to say, "Group X no
2415 * access, user Y full access" if user Y is a member of group X.
2416 * This seems completely broken semantics to me.... JRA.
2419 #if 0
2420 /* Pass 4 above - deal with allow entries. */
2422 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2423 canon_ace *allow_ace_p;
2425 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2427 if (curr_ace->attr != ALLOW_ACE)
2428 continue;
2430 if (curr_ace->owner_type != UID_ACE)
2431 continue;
2433 for (allow_ace_p = ace_list; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2435 if (allow_ace_p->attr != ALLOW_ACE)
2436 continue;
2438 /* We process GID_ACE entries only. */
2440 if (allow_ace_p->owner_type != GID_ACE)
2441 continue;
2443 /* OR in the group perms. */
2445 if (uid_entry_in_group(conn, curr_ace, allow_ace_p))
2446 curr_ace->perms |= allow_ace_p->perms;
2449 #endif
2451 *pp_ace_list = ace_list;
2454 /****************************************************************************
2455 Unpack a struct security_descriptor into two canonical ace lists. We don't depend on this
2456 succeeding.
2457 ****************************************************************************/
2459 static bool unpack_canon_ace(files_struct *fsp,
2460 const SMB_STRUCT_STAT *pst,
2461 struct dom_sid *pfile_owner_sid,
2462 struct dom_sid *pfile_grp_sid,
2463 canon_ace **ppfile_ace,
2464 canon_ace **ppdir_ace,
2465 uint32 security_info_sent,
2466 const struct security_descriptor *psd)
2468 canon_ace *file_ace = NULL;
2469 canon_ace *dir_ace = NULL;
2471 *ppfile_ace = NULL;
2472 *ppdir_ace = NULL;
2474 if(security_info_sent == 0) {
2475 DEBUG(0,("unpack_canon_ace: no security info sent !\n"));
2476 return False;
2480 * If no DACL then this is a chown only security descriptor.
2483 if(!(security_info_sent & SECINFO_DACL) || !psd->dacl)
2484 return True;
2487 * Now go through the DACL and create the canon_ace lists.
2490 if (!create_canon_ace_lists( fsp, pst, pfile_owner_sid, pfile_grp_sid,
2491 &file_ace, &dir_ace, psd->dacl))
2492 return False;
2494 if ((file_ace == NULL) && (dir_ace == NULL)) {
2495 /* W2K traverse DACL set - ignore. */
2496 return True;
2500 * Go through the canon_ace list and merge entries
2501 * belonging to identical users of identical allow or deny type.
2502 * We can do this as all deny entries come first, followed by
2503 * all allow entries (we have mandated this before accepting this acl).
2506 print_canon_ace_list( "file ace - before merge", file_ace);
2507 merge_aces( &file_ace, false);
2509 print_canon_ace_list( "dir ace - before merge", dir_ace);
2510 merge_aces( &dir_ace, true);
2513 * NT ACLs are order dependent. Go through the acl lists and
2514 * process DENY entries by masking the allow entries.
2517 print_canon_ace_list( "file ace - before deny", file_ace);
2518 process_deny_list(fsp->conn, &file_ace);
2520 print_canon_ace_list( "dir ace - before deny", dir_ace);
2521 process_deny_list(fsp->conn, &dir_ace);
2524 * A well formed POSIX file or default ACL has at least 3 entries, a
2525 * SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ
2526 * and optionally a mask entry. Ensure this is the case.
2529 print_canon_ace_list( "file ace - before valid", file_ace);
2531 if (!ensure_canon_entry_valid(fsp->conn, &file_ace, fsp->conn->params,
2532 fsp->is_directory, pfile_owner_sid, pfile_grp_sid, pst, True)) {
2533 free_canon_ace_list(file_ace);
2534 free_canon_ace_list(dir_ace);
2535 return False;
2538 print_canon_ace_list( "dir ace - before valid", dir_ace);
2540 if (dir_ace && !ensure_canon_entry_valid(fsp->conn, &dir_ace, fsp->conn->params,
2541 fsp->is_directory, pfile_owner_sid, pfile_grp_sid, pst, True)) {
2542 free_canon_ace_list(file_ace);
2543 free_canon_ace_list(dir_ace);
2544 return False;
2547 print_canon_ace_list( "file ace - return", file_ace);
2548 print_canon_ace_list( "dir ace - return", dir_ace);
2550 *ppfile_ace = file_ace;
2551 *ppdir_ace = dir_ace;
2552 return True;
2556 /******************************************************************************
2557 When returning permissions, try and fit NT display
2558 semantics if possible. Note the the canon_entries here must have been malloced.
2559 The list format should be - first entry = owner, followed by group and other user
2560 entries, last entry = other.
2562 Note that this doesn't exactly match the NT semantics for an ACL. As POSIX entries
2563 are not ordered, and match on the most specific entry rather than walking a list,
2564 then a simple POSIX permission of rw-r--r-- should really map to 5 entries,
2566 Entry 0: owner : deny all except read and write.
2567 Entry 1: owner : allow read and write.
2568 Entry 2: group : deny all except read.
2569 Entry 3: group : allow read.
2570 Entry 4: Everyone : allow read.
2572 But NT cannot display this in their ACL editor !
2573 ********************************************************************************/
2575 static void arrange_posix_perms(const char *filename, canon_ace **pp_list_head)
2577 canon_ace *l_head = *pp_list_head;
2578 canon_ace *owner_ace = NULL;
2579 canon_ace *other_ace = NULL;
2580 canon_ace *ace = NULL;
2582 for (ace = l_head; ace; ace = ace->next) {
2583 if (ace->type == SMB_ACL_USER_OBJ)
2584 owner_ace = ace;
2585 else if (ace->type == SMB_ACL_OTHER) {
2586 /* Last ace - this is "other" */
2587 other_ace = ace;
2591 if (!owner_ace || !other_ace) {
2592 DEBUG(0,("arrange_posix_perms: Invalid POSIX permissions for file %s, missing owner or other.\n",
2593 filename ));
2594 return;
2598 * The POSIX algorithm applies to owner first, and other last,
2599 * so ensure they are arranged in this order.
2602 if (owner_ace) {
2603 DLIST_PROMOTE(l_head, owner_ace);
2606 if (other_ace) {
2607 DLIST_DEMOTE(l_head, other_ace, canon_ace *);
2610 /* We have probably changed the head of the list. */
2612 *pp_list_head = l_head;
2615 /****************************************************************************
2616 Create a linked list of canonical ACE entries.
2617 ****************************************************************************/
2619 static canon_ace *canonicalise_acl(struct connection_struct *conn,
2620 const char *fname, SMB_ACL_T posix_acl,
2621 const SMB_STRUCT_STAT *psbuf,
2622 const struct dom_sid *powner, const struct dom_sid *pgroup, struct pai_val *pal, SMB_ACL_TYPE_T the_acl_type)
2624 mode_t acl_mask = (S_IRUSR|S_IWUSR|S_IXUSR);
2625 canon_ace *l_head = NULL;
2626 canon_ace *ace = NULL;
2627 canon_ace *next_ace = NULL;
2628 int entry_id = SMB_ACL_FIRST_ENTRY;
2629 SMB_ACL_ENTRY_T entry;
2630 size_t ace_count;
2632 while ( posix_acl && (SMB_VFS_SYS_ACL_GET_ENTRY(conn, posix_acl, entry_id, &entry) == 1)) {
2633 SMB_ACL_TAG_T tagtype;
2634 SMB_ACL_PERMSET_T permset;
2635 struct dom_sid sid;
2636 posix_id unix_ug;
2637 enum ace_owner owner_type;
2639 entry_id = SMB_ACL_NEXT_ENTRY;
2641 /* Is this a MASK entry ? */
2642 if (SMB_VFS_SYS_ACL_GET_TAG_TYPE(conn, entry, &tagtype) == -1)
2643 continue;
2645 if (SMB_VFS_SYS_ACL_GET_PERMSET(conn, entry, &permset) == -1)
2646 continue;
2648 /* Decide which SID to use based on the ACL type. */
2649 switch(tagtype) {
2650 case SMB_ACL_USER_OBJ:
2651 /* Get the SID from the owner. */
2652 sid_copy(&sid, powner);
2653 unix_ug.uid = psbuf->st_ex_uid;
2654 owner_type = UID_ACE;
2655 break;
2656 case SMB_ACL_USER:
2658 uid_t *puid = (uid_t *)SMB_VFS_SYS_ACL_GET_QUALIFIER(conn, entry);
2659 if (puid == NULL) {
2660 DEBUG(0,("canonicalise_acl: Failed to get uid.\n"));
2661 continue;
2663 uid_to_sid( &sid, *puid);
2664 unix_ug.uid = *puid;
2665 owner_type = UID_ACE;
2666 SMB_VFS_SYS_ACL_FREE_QUALIFIER(conn, (void *)puid,tagtype);
2667 break;
2669 case SMB_ACL_GROUP_OBJ:
2670 /* Get the SID from the owning group. */
2671 sid_copy(&sid, pgroup);
2672 unix_ug.gid = psbuf->st_ex_gid;
2673 owner_type = GID_ACE;
2674 break;
2675 case SMB_ACL_GROUP:
2677 gid_t *pgid = (gid_t *)SMB_VFS_SYS_ACL_GET_QUALIFIER(conn, entry);
2678 if (pgid == NULL) {
2679 DEBUG(0,("canonicalise_acl: Failed to get gid.\n"));
2680 continue;
2682 gid_to_sid( &sid, *pgid);
2683 unix_ug.gid = *pgid;
2684 owner_type = GID_ACE;
2685 SMB_VFS_SYS_ACL_FREE_QUALIFIER(conn, (void *)pgid,tagtype);
2686 break;
2688 case SMB_ACL_MASK:
2689 acl_mask = convert_permset_to_mode_t(conn, permset);
2690 continue; /* Don't count the mask as an entry. */
2691 case SMB_ACL_OTHER:
2692 /* Use the Everyone SID */
2693 sid = global_sid_World;
2694 unix_ug.world = -1;
2695 owner_type = WORLD_ACE;
2696 break;
2697 default:
2698 DEBUG(0,("canonicalise_acl: Unknown tagtype %u\n", (unsigned int)tagtype));
2699 continue;
2703 * Add this entry to the list.
2706 if ((ace = talloc(talloc_tos(), canon_ace)) == NULL)
2707 goto fail;
2709 ZERO_STRUCTP(ace);
2710 ace->type = tagtype;
2711 ace->perms = convert_permset_to_mode_t(conn, permset);
2712 ace->attr = ALLOW_ACE;
2713 ace->trustee = sid;
2714 ace->unix_ug = unix_ug;
2715 ace->owner_type = owner_type;
2716 ace->ace_flags = get_pai_flags(pal, ace, (the_acl_type == SMB_ACL_TYPE_DEFAULT));
2718 DLIST_ADD(l_head, ace);
2722 * This next call will ensure we have at least a user/group/world set.
2725 if (!ensure_canon_entry_valid(conn, &l_head, conn->params,
2726 S_ISDIR(psbuf->st_ex_mode), powner, pgroup,
2727 psbuf, False))
2728 goto fail;
2731 * Now go through the list, masking the permissions with the
2732 * acl_mask. Ensure all DENY Entries are at the start of the list.
2735 DEBUG(10,("canonicalise_acl: %s ace entries before arrange :\n", the_acl_type == SMB_ACL_TYPE_ACCESS ? "Access" : "Default" ));
2737 for ( ace_count = 0, ace = l_head; ace; ace = next_ace, ace_count++) {
2738 next_ace = ace->next;
2740 /* Masks are only applied to entries other than USER_OBJ and OTHER. */
2741 if (ace->type != SMB_ACL_OTHER && ace->type != SMB_ACL_USER_OBJ)
2742 ace->perms &= acl_mask;
2744 if (ace->perms == 0) {
2745 DLIST_PROMOTE(l_head, ace);
2748 if( DEBUGLVL( 10 ) ) {
2749 print_canon_ace(ace, ace_count);
2753 arrange_posix_perms(fname,&l_head );
2755 print_canon_ace_list( "canonicalise_acl: ace entries after arrange", l_head );
2757 return l_head;
2759 fail:
2761 free_canon_ace_list(l_head);
2762 return NULL;
2765 /****************************************************************************
2766 Check if the current user group list contains a given group.
2767 ****************************************************************************/
2769 bool current_user_in_group(connection_struct *conn, gid_t gid)
2771 int i;
2772 const struct security_unix_token *utok = get_current_utok(conn);
2774 for (i = 0; i < utok->ngroups; i++) {
2775 if (utok->groups[i] == gid) {
2776 return True;
2780 return False;
2783 /****************************************************************************
2784 Should we override a deny ? Check 'acl group control' and 'dos filemode'.
2785 ****************************************************************************/
2787 static bool acl_group_override(connection_struct *conn,
2788 const struct smb_filename *smb_fname)
2790 if ((errno != EPERM) && (errno != EACCES)) {
2791 return false;
2794 /* file primary group == user primary or supplementary group */
2795 if (lp_acl_group_control(SNUM(conn)) &&
2796 current_user_in_group(conn, smb_fname->st.st_ex_gid)) {
2797 return true;
2800 /* user has writeable permission */
2801 if (lp_dos_filemode(SNUM(conn)) &&
2802 can_write_to_file(conn, smb_fname)) {
2803 return true;
2806 return false;
2809 /****************************************************************************
2810 Attempt to apply an ACL to a file or directory.
2811 ****************************************************************************/
2813 static bool set_canon_ace_list(files_struct *fsp,
2814 canon_ace *the_ace,
2815 bool default_ace,
2816 const SMB_STRUCT_STAT *psbuf,
2817 bool *pacl_set_support)
2819 connection_struct *conn = fsp->conn;
2820 bool ret = False;
2821 SMB_ACL_T the_acl = SMB_VFS_SYS_ACL_INIT(conn, (int)count_canon_ace_list(the_ace) + 1);
2822 canon_ace *p_ace;
2823 int i;
2824 SMB_ACL_ENTRY_T mask_entry;
2825 bool got_mask_entry = False;
2826 SMB_ACL_PERMSET_T mask_permset;
2827 SMB_ACL_TYPE_T the_acl_type = (default_ace ? SMB_ACL_TYPE_DEFAULT : SMB_ACL_TYPE_ACCESS);
2828 bool needs_mask = False;
2829 mode_t mask_perms = 0;
2831 /* Use the psbuf that was passed in. */
2832 if (psbuf != &fsp->fsp_name->st) {
2833 fsp->fsp_name->st = *psbuf;
2836 #if defined(POSIX_ACL_NEEDS_MASK)
2837 /* HP-UX always wants to have a mask (called "class" there). */
2838 needs_mask = True;
2839 #endif
2841 if (the_acl == NULL) {
2843 if (!no_acl_syscall_error(errno)) {
2845 * Only print this error message if we have some kind of ACL
2846 * support that's not working. Otherwise we would always get this.
2848 DEBUG(0,("set_canon_ace_list: Unable to init %s ACL. (%s)\n",
2849 default_ace ? "default" : "file", strerror(errno) ));
2851 *pacl_set_support = False;
2852 goto fail;
2855 if( DEBUGLVL( 10 )) {
2856 dbgtext("set_canon_ace_list: setting ACL:\n");
2857 for (i = 0, p_ace = the_ace; p_ace; p_ace = p_ace->next, i++ ) {
2858 print_canon_ace( p_ace, i);
2862 for (i = 0, p_ace = the_ace; p_ace; p_ace = p_ace->next, i++ ) {
2863 SMB_ACL_ENTRY_T the_entry;
2864 SMB_ACL_PERMSET_T the_permset;
2867 * ACLs only "need" an ACL_MASK entry if there are any named user or
2868 * named group entries. But if there is an ACL_MASK entry, it applies
2869 * to ACL_USER, ACL_GROUP, and ACL_GROUP_OBJ entries. Set the mask
2870 * so that it doesn't deny (i.e., mask off) any permissions.
2873 if (p_ace->type == SMB_ACL_USER || p_ace->type == SMB_ACL_GROUP) {
2874 needs_mask = True;
2875 mask_perms |= p_ace->perms;
2876 } else if (p_ace->type == SMB_ACL_GROUP_OBJ) {
2877 mask_perms |= p_ace->perms;
2881 * Get the entry for this ACE.
2884 if (SMB_VFS_SYS_ACL_CREATE_ENTRY(conn, &the_acl, &the_entry) == -1) {
2885 DEBUG(0,("set_canon_ace_list: Failed to create entry %d. (%s)\n",
2886 i, strerror(errno) ));
2887 goto fail;
2890 if (p_ace->type == SMB_ACL_MASK) {
2891 mask_entry = the_entry;
2892 got_mask_entry = True;
2896 * Ok - we now know the ACL calls should be working, don't
2897 * allow fallback to chmod.
2900 *pacl_set_support = True;
2903 * Initialise the entry from the canon_ace.
2907 * First tell the entry what type of ACE this is.
2910 if (SMB_VFS_SYS_ACL_SET_TAG_TYPE(conn, the_entry, p_ace->type) == -1) {
2911 DEBUG(0,("set_canon_ace_list: Failed to set tag type on entry %d. (%s)\n",
2912 i, strerror(errno) ));
2913 goto fail;
2917 * Only set the qualifier (user or group id) if the entry is a user
2918 * or group id ACE.
2921 if ((p_ace->type == SMB_ACL_USER) || (p_ace->type == SMB_ACL_GROUP)) {
2922 if (SMB_VFS_SYS_ACL_SET_QUALIFIER(conn, the_entry,(void *)&p_ace->unix_ug.uid) == -1) {
2923 DEBUG(0,("set_canon_ace_list: Failed to set qualifier on entry %d. (%s)\n",
2924 i, strerror(errno) ));
2925 goto fail;
2930 * Convert the mode_t perms in the canon_ace to a POSIX permset.
2933 if (SMB_VFS_SYS_ACL_GET_PERMSET(conn, the_entry, &the_permset) == -1) {
2934 DEBUG(0,("set_canon_ace_list: Failed to get permset on entry %d. (%s)\n",
2935 i, strerror(errno) ));
2936 goto fail;
2939 if (map_acl_perms_to_permset(conn, p_ace->perms, &the_permset) == -1) {
2940 DEBUG(0,("set_canon_ace_list: Failed to create permset for mode (%u) on entry %d. (%s)\n",
2941 (unsigned int)p_ace->perms, i, strerror(errno) ));
2942 goto fail;
2946 * ..and apply them to the entry.
2949 if (SMB_VFS_SYS_ACL_SET_PERMSET(conn, the_entry, the_permset) == -1) {
2950 DEBUG(0,("set_canon_ace_list: Failed to add permset on entry %d. (%s)\n",
2951 i, strerror(errno) ));
2952 goto fail;
2955 if( DEBUGLVL( 10 ))
2956 print_canon_ace( p_ace, i);
2960 if (needs_mask && !got_mask_entry) {
2961 if (SMB_VFS_SYS_ACL_CREATE_ENTRY(conn, &the_acl, &mask_entry) == -1) {
2962 DEBUG(0,("set_canon_ace_list: Failed to create mask entry. (%s)\n", strerror(errno) ));
2963 goto fail;
2966 if (SMB_VFS_SYS_ACL_SET_TAG_TYPE(conn, mask_entry, SMB_ACL_MASK) == -1) {
2967 DEBUG(0,("set_canon_ace_list: Failed to set tag type on mask entry. (%s)\n",strerror(errno) ));
2968 goto fail;
2971 if (SMB_VFS_SYS_ACL_GET_PERMSET(conn, mask_entry, &mask_permset) == -1) {
2972 DEBUG(0,("set_canon_ace_list: Failed to get mask permset. (%s)\n", strerror(errno) ));
2973 goto fail;
2976 if (map_acl_perms_to_permset(conn, S_IRUSR|S_IWUSR|S_IXUSR, &mask_permset) == -1) {
2977 DEBUG(0,("set_canon_ace_list: Failed to create mask permset. (%s)\n", strerror(errno) ));
2978 goto fail;
2981 if (SMB_VFS_SYS_ACL_SET_PERMSET(conn, mask_entry, mask_permset) == -1) {
2982 DEBUG(0,("set_canon_ace_list: Failed to add mask permset. (%s)\n", strerror(errno) ));
2983 goto fail;
2988 * Finally apply it to the file or directory.
2991 if(default_ace || fsp->is_directory || fsp->fh->fd == -1) {
2992 if (SMB_VFS_SYS_ACL_SET_FILE(conn, fsp->fsp_name->base_name,
2993 the_acl_type, the_acl) == -1) {
2995 * Some systems allow all the above calls and only fail with no ACL support
2996 * when attempting to apply the acl. HPUX with HFS is an example of this. JRA.
2998 if (no_acl_syscall_error(errno)) {
2999 *pacl_set_support = False;
3002 if (acl_group_override(conn, fsp->fsp_name)) {
3003 int sret;
3005 DEBUG(5,("set_canon_ace_list: acl group "
3006 "control on and current user in file "
3007 "%s primary group.\n",
3008 fsp_str_dbg(fsp)));
3010 become_root();
3011 sret = SMB_VFS_SYS_ACL_SET_FILE(conn,
3012 fsp->fsp_name->base_name, the_acl_type,
3013 the_acl);
3014 unbecome_root();
3015 if (sret == 0) {
3016 ret = True;
3020 if (ret == False) {
3021 DEBUG(2,("set_canon_ace_list: "
3022 "sys_acl_set_file type %s failed for "
3023 "file %s (%s).\n",
3024 the_acl_type == SMB_ACL_TYPE_DEFAULT ?
3025 "directory default" : "file",
3026 fsp_str_dbg(fsp), strerror(errno)));
3027 goto fail;
3030 } else {
3031 if (SMB_VFS_SYS_ACL_SET_FD(fsp, the_acl) == -1) {
3033 * Some systems allow all the above calls and only fail with no ACL support
3034 * when attempting to apply the acl. HPUX with HFS is an example of this. JRA.
3036 if (no_acl_syscall_error(errno)) {
3037 *pacl_set_support = False;
3040 if (acl_group_override(conn, fsp->fsp_name)) {
3041 int sret;
3043 DEBUG(5,("set_canon_ace_list: acl group "
3044 "control on and current user in file "
3045 "%s primary group.\n",
3046 fsp_str_dbg(fsp)));
3048 become_root();
3049 sret = SMB_VFS_SYS_ACL_SET_FD(fsp, the_acl);
3050 unbecome_root();
3051 if (sret == 0) {
3052 ret = True;
3056 if (ret == False) {
3057 DEBUG(2,("set_canon_ace_list: "
3058 "sys_acl_set_file failed for file %s "
3059 "(%s).\n",
3060 fsp_str_dbg(fsp), strerror(errno)));
3061 goto fail;
3066 ret = True;
3068 fail:
3070 if (the_acl != NULL) {
3071 SMB_VFS_SYS_ACL_FREE_ACL(conn, the_acl);
3074 return ret;
3077 /****************************************************************************
3078 Find a particular canon_ace entry.
3079 ****************************************************************************/
3081 static struct canon_ace *canon_ace_entry_for(struct canon_ace *list, SMB_ACL_TAG_T type, posix_id *id)
3083 while (list) {
3084 if (list->type == type && ((type != SMB_ACL_USER && type != SMB_ACL_GROUP) ||
3085 (type == SMB_ACL_USER && id && id->uid == list->unix_ug.uid) ||
3086 (type == SMB_ACL_GROUP && id && id->gid == list->unix_ug.gid)))
3087 break;
3088 list = list->next;
3090 return list;
3093 /****************************************************************************
3095 ****************************************************************************/
3097 SMB_ACL_T free_empty_sys_acl(connection_struct *conn, SMB_ACL_T the_acl)
3099 SMB_ACL_ENTRY_T entry;
3101 if (!the_acl)
3102 return NULL;
3103 if (SMB_VFS_SYS_ACL_GET_ENTRY(conn, the_acl, SMB_ACL_FIRST_ENTRY, &entry) != 1) {
3104 SMB_VFS_SYS_ACL_FREE_ACL(conn, the_acl);
3105 return NULL;
3107 return the_acl;
3110 /****************************************************************************
3111 Convert a canon_ace to a generic 3 element permission - if possible.
3112 ****************************************************************************/
3114 #define MAP_PERM(p,mask,result) (((p) & (mask)) ? (result) : 0 )
3116 static bool convert_canon_ace_to_posix_perms( files_struct *fsp, canon_ace *file_ace_list, mode_t *posix_perms)
3118 int snum = SNUM(fsp->conn);
3119 size_t ace_count = count_canon_ace_list(file_ace_list);
3120 canon_ace *ace_p;
3121 canon_ace *owner_ace = NULL;
3122 canon_ace *group_ace = NULL;
3123 canon_ace *other_ace = NULL;
3124 mode_t and_bits;
3125 mode_t or_bits;
3127 if (ace_count != 3) {
3128 DEBUG(3,("convert_canon_ace_to_posix_perms: Too many ACE "
3129 "entries for file %s to convert to posix perms.\n",
3130 fsp_str_dbg(fsp)));
3131 return False;
3134 for (ace_p = file_ace_list; ace_p; ace_p = ace_p->next) {
3135 if (ace_p->owner_type == UID_ACE)
3136 owner_ace = ace_p;
3137 else if (ace_p->owner_type == GID_ACE)
3138 group_ace = ace_p;
3139 else if (ace_p->owner_type == WORLD_ACE)
3140 other_ace = ace_p;
3143 if (!owner_ace || !group_ace || !other_ace) {
3144 DEBUG(3,("convert_canon_ace_to_posix_perms: Can't get "
3145 "standard entries for file %s.\n", fsp_str_dbg(fsp)));
3146 return False;
3149 *posix_perms = (mode_t)0;
3151 *posix_perms |= owner_ace->perms;
3152 *posix_perms |= MAP_PERM(group_ace->perms, S_IRUSR, S_IRGRP);
3153 *posix_perms |= MAP_PERM(group_ace->perms, S_IWUSR, S_IWGRP);
3154 *posix_perms |= MAP_PERM(group_ace->perms, S_IXUSR, S_IXGRP);
3155 *posix_perms |= MAP_PERM(other_ace->perms, S_IRUSR, S_IROTH);
3156 *posix_perms |= MAP_PERM(other_ace->perms, S_IWUSR, S_IWOTH);
3157 *posix_perms |= MAP_PERM(other_ace->perms, S_IXUSR, S_IXOTH);
3159 /* The owner must have at least read access. */
3161 *posix_perms |= S_IRUSR;
3162 if (fsp->is_directory)
3163 *posix_perms |= (S_IWUSR|S_IXUSR);
3165 /* If requested apply the masks. */
3167 /* Get the initial bits to apply. */
3169 if (fsp->is_directory) {
3170 and_bits = lp_dir_security_mask(snum);
3171 or_bits = lp_force_dir_security_mode(snum);
3172 } else {
3173 and_bits = lp_security_mask(snum);
3174 or_bits = lp_force_security_mode(snum);
3177 *posix_perms = (((*posix_perms) & and_bits)|or_bits);
3179 DEBUG(10,("convert_canon_ace_to_posix_perms: converted u=%o,g=%o,w=%o "
3180 "to perm=0%o for file %s.\n", (int)owner_ace->perms,
3181 (int)group_ace->perms, (int)other_ace->perms,
3182 (int)*posix_perms, fsp_str_dbg(fsp)));
3184 return True;
3187 /****************************************************************************
3188 Incoming NT ACLs on a directory can be split into a default POSIX acl (CI|OI|IO) and
3189 a normal POSIX acl. Win2k needs these split acls re-merging into one ACL
3190 with CI|OI set so it is inherited and also applies to the directory.
3191 Based on code from "Jim McDonough" <jmcd@us.ibm.com>.
3192 ****************************************************************************/
3194 static size_t merge_default_aces( struct security_ace *nt_ace_list, size_t num_aces)
3196 size_t i, j;
3198 for (i = 0; i < num_aces; i++) {
3199 for (j = i+1; j < num_aces; j++) {
3200 uint32 i_flags_ni = (nt_ace_list[i].flags & ~SEC_ACE_FLAG_INHERITED_ACE);
3201 uint32 j_flags_ni = (nt_ace_list[j].flags & ~SEC_ACE_FLAG_INHERITED_ACE);
3202 bool i_inh = (nt_ace_list[i].flags & SEC_ACE_FLAG_INHERITED_ACE) ? True : False;
3203 bool j_inh = (nt_ace_list[j].flags & SEC_ACE_FLAG_INHERITED_ACE) ? True : False;
3205 /* We know the lower number ACE's are file entries. */
3206 if ((nt_ace_list[i].type == nt_ace_list[j].type) &&
3207 (nt_ace_list[i].size == nt_ace_list[j].size) &&
3208 (nt_ace_list[i].access_mask == nt_ace_list[j].access_mask) &&
3209 dom_sid_equal(&nt_ace_list[i].trustee, &nt_ace_list[j].trustee) &&
3210 (i_inh == j_inh) &&
3211 (i_flags_ni == 0) &&
3212 (j_flags_ni == (SEC_ACE_FLAG_OBJECT_INHERIT|
3213 SEC_ACE_FLAG_CONTAINER_INHERIT|
3214 SEC_ACE_FLAG_INHERIT_ONLY))) {
3216 * W2K wants to have access allowed zero access ACE's
3217 * at the end of the list. If the mask is zero, merge
3218 * the non-inherited ACE onto the inherited ACE.
3221 if (nt_ace_list[i].access_mask == 0) {
3222 nt_ace_list[j].flags = SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT|
3223 (i_inh ? SEC_ACE_FLAG_INHERITED_ACE : 0);
3224 if (num_aces - i - 1 > 0)
3225 memmove(&nt_ace_list[i], &nt_ace_list[i+1], (num_aces-i-1) *
3226 sizeof(struct security_ace));
3228 DEBUG(10,("merge_default_aces: Merging zero access ACE %u onto ACE %u.\n",
3229 (unsigned int)i, (unsigned int)j ));
3230 } else {
3232 * These are identical except for the flags.
3233 * Merge the inherited ACE onto the non-inherited ACE.
3236 nt_ace_list[i].flags = SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT|
3237 (i_inh ? SEC_ACE_FLAG_INHERITED_ACE : 0);
3238 if (num_aces - j - 1 > 0)
3239 memmove(&nt_ace_list[j], &nt_ace_list[j+1], (num_aces-j-1) *
3240 sizeof(struct security_ace));
3242 DEBUG(10,("merge_default_aces: Merging ACE %u onto ACE %u.\n",
3243 (unsigned int)j, (unsigned int)i ));
3245 num_aces--;
3246 break;
3251 return num_aces;
3255 * Add or Replace ACE entry.
3256 * In some cases we need to add a specific ACE for compatibility reasons.
3257 * When doing that we must make sure we are not actually creating a duplicate
3258 * entry. So we need to search whether an ACE entry already exist and eventually
3259 * replacce the access mask, or add a completely new entry if none was found.
3261 * This function assumes the array has enough space to add a new entry without
3262 * any reallocation of memory.
3265 static void add_or_replace_ace(struct security_ace *nt_ace_list, size_t *num_aces,
3266 const struct dom_sid *sid, enum security_ace_type type,
3267 uint32_t mask, uint8_t flags)
3269 int i;
3271 /* first search for a duplicate */
3272 for (i = 0; i < *num_aces; i++) {
3273 if (dom_sid_equal(&nt_ace_list[i].trustee, sid) &&
3274 (nt_ace_list[i].flags == flags)) break;
3277 if (i < *num_aces) { /* found */
3278 nt_ace_list[i].type = type;
3279 nt_ace_list[i].access_mask = mask;
3280 DEBUG(10, ("Replacing ACE %d with SID %s and flags %02x\n",
3281 i, sid_string_dbg(sid), flags));
3282 return;
3285 /* not found, append it */
3286 init_sec_ace(&nt_ace_list[(*num_aces)++], sid, type, mask, flags);
3290 /****************************************************************************
3291 Reply to query a security descriptor from an fsp. If it succeeds it allocates
3292 the space for the return elements and returns the size needed to return the
3293 security descriptor. This should be the only external function needed for
3294 the UNIX style get ACL.
3295 ****************************************************************************/
3297 static NTSTATUS posix_get_nt_acl_common(struct connection_struct *conn,
3298 const char *name,
3299 const SMB_STRUCT_STAT *sbuf,
3300 struct pai_val *pal,
3301 SMB_ACL_T posix_acl,
3302 SMB_ACL_T def_acl,
3303 uint32_t security_info,
3304 struct security_descriptor **ppdesc)
3306 struct dom_sid owner_sid;
3307 struct dom_sid group_sid;
3308 size_t sd_size = 0;
3309 struct security_acl *psa = NULL;
3310 size_t num_acls = 0;
3311 size_t num_def_acls = 0;
3312 size_t num_aces = 0;
3313 canon_ace *file_ace = NULL;
3314 canon_ace *dir_ace = NULL;
3315 struct security_ace *nt_ace_list = NULL;
3316 size_t num_profile_acls = 0;
3317 struct dom_sid orig_owner_sid;
3318 struct security_descriptor *psd = NULL;
3319 int i;
3322 * Get the owner, group and world SIDs.
3325 create_file_sids(sbuf, &owner_sid, &group_sid);
3327 if (lp_profile_acls(SNUM(conn))) {
3328 /* For WXP SP1 the owner must be administrators. */
3329 sid_copy(&orig_owner_sid, &owner_sid);
3330 sid_copy(&owner_sid, &global_sid_Builtin_Administrators);
3331 sid_copy(&group_sid, &global_sid_Builtin_Users);
3332 num_profile_acls = 3;
3335 if ((security_info & SECINFO_DACL) && !(security_info & SECINFO_PROTECTED_DACL)) {
3338 * In the optimum case Creator Owner and Creator Group would be used for
3339 * the ACL_USER_OBJ and ACL_GROUP_OBJ entries, respectively, but this
3340 * would lead to usability problems under Windows: The Creator entries
3341 * are only available in browse lists of directories and not for files;
3342 * additionally the identity of the owning group couldn't be determined.
3343 * We therefore use those identities only for Default ACLs.
3346 /* Create the canon_ace lists. */
3347 file_ace = canonicalise_acl(conn, name, posix_acl, sbuf,
3348 &owner_sid, &group_sid, pal,
3349 SMB_ACL_TYPE_ACCESS);
3351 /* We must have *some* ACLS. */
3353 if (count_canon_ace_list(file_ace) == 0) {
3354 DEBUG(0,("get_nt_acl : No ACLs on file (%s) !\n", name));
3355 goto done;
3358 if (S_ISDIR(sbuf->st_ex_mode) && def_acl) {
3359 dir_ace = canonicalise_acl(conn, name, def_acl,
3360 sbuf,
3361 &global_sid_Creator_Owner,
3362 &global_sid_Creator_Group,
3363 pal, SMB_ACL_TYPE_DEFAULT);
3367 * Create the NT ACE list from the canonical ace lists.
3371 canon_ace *ace;
3372 enum security_ace_type nt_acl_type;
3374 if (nt4_compatible_acls() && dir_ace) {
3376 * NT 4 chokes if an ACL contains an INHERIT_ONLY entry
3377 * but no non-INHERIT_ONLY entry for one SID. So we only
3378 * remove entries from the Access ACL if the
3379 * corresponding Default ACL entries have also been
3380 * removed. ACEs for CREATOR-OWNER and CREATOR-GROUP
3381 * are exceptions. We can do nothing
3382 * intelligent if the Default ACL contains entries that
3383 * are not also contained in the Access ACL, so this
3384 * case will still fail under NT 4.
3387 ace = canon_ace_entry_for(dir_ace, SMB_ACL_OTHER, NULL);
3388 if (ace && !ace->perms) {
3389 DLIST_REMOVE(dir_ace, ace);
3390 TALLOC_FREE(ace);
3392 ace = canon_ace_entry_for(file_ace, SMB_ACL_OTHER, NULL);
3393 if (ace && !ace->perms) {
3394 DLIST_REMOVE(file_ace, ace);
3395 TALLOC_FREE(ace);
3400 * WinNT doesn't usually have Creator Group
3401 * in browse lists, so we send this entry to
3402 * WinNT even if it contains no relevant
3403 * permissions. Once we can add
3404 * Creator Group to browse lists we can
3405 * re-enable this.
3408 #if 0
3409 ace = canon_ace_entry_for(dir_ace, SMB_ACL_GROUP_OBJ, NULL);
3410 if (ace && !ace->perms) {
3411 DLIST_REMOVE(dir_ace, ace);
3412 TALLOC_FREE(ace);
3414 #endif
3416 ace = canon_ace_entry_for(file_ace, SMB_ACL_GROUP_OBJ, NULL);
3417 if (ace && !ace->perms) {
3418 DLIST_REMOVE(file_ace, ace);
3419 TALLOC_FREE(ace);
3423 num_acls = count_canon_ace_list(file_ace);
3424 num_def_acls = count_canon_ace_list(dir_ace);
3426 /* Allocate the ace list. */
3427 if ((nt_ace_list = talloc_array(talloc_tos(), struct security_ace,num_acls + num_profile_acls + num_def_acls)) == NULL) {
3428 DEBUG(0,("get_nt_acl: Unable to malloc space for nt_ace_list.\n"));
3429 goto done;
3432 memset(nt_ace_list, '\0', (num_acls + num_def_acls) * sizeof(struct security_ace) );
3435 * Create the NT ACE list from the canonical ace lists.
3438 for (ace = file_ace; ace != NULL; ace = ace->next) {
3439 uint32_t acc = map_canon_ace_perms(SNUM(conn),
3440 &nt_acl_type,
3441 ace->perms,
3442 S_ISDIR(sbuf->st_ex_mode));
3443 init_sec_ace(&nt_ace_list[num_aces++],
3444 &ace->trustee,
3445 nt_acl_type,
3446 acc,
3447 ace->ace_flags);
3450 /* The User must have access to a profile share - even
3451 * if we can't map the SID. */
3452 if (lp_profile_acls(SNUM(conn))) {
3453 add_or_replace_ace(nt_ace_list, &num_aces,
3454 &global_sid_Builtin_Users,
3455 SEC_ACE_TYPE_ACCESS_ALLOWED,
3456 FILE_GENERIC_ALL, 0);
3459 for (ace = dir_ace; ace != NULL; ace = ace->next) {
3460 uint32_t acc = map_canon_ace_perms(SNUM(conn),
3461 &nt_acl_type,
3462 ace->perms,
3463 S_ISDIR(sbuf->st_ex_mode));
3464 init_sec_ace(&nt_ace_list[num_aces++],
3465 &ace->trustee,
3466 nt_acl_type,
3467 acc,
3468 ace->ace_flags |
3469 SEC_ACE_FLAG_OBJECT_INHERIT|
3470 SEC_ACE_FLAG_CONTAINER_INHERIT|
3471 SEC_ACE_FLAG_INHERIT_ONLY);
3474 /* The User must have access to a profile share - even
3475 * if we can't map the SID. */
3476 if (lp_profile_acls(SNUM(conn))) {
3477 add_or_replace_ace(nt_ace_list, &num_aces,
3478 &global_sid_Builtin_Users,
3479 SEC_ACE_TYPE_ACCESS_ALLOWED,
3480 FILE_GENERIC_ALL,
3481 SEC_ACE_FLAG_OBJECT_INHERIT |
3482 SEC_ACE_FLAG_CONTAINER_INHERIT |
3483 SEC_ACE_FLAG_INHERIT_ONLY);
3487 * Merge POSIX default ACLs and normal ACLs into one NT ACE.
3488 * Win2K needs this to get the inheritance correct when replacing ACLs
3489 * on a directory tree. Based on work by Jim @ IBM.
3492 num_aces = merge_default_aces(nt_ace_list, num_aces);
3494 if (lp_profile_acls(SNUM(conn))) {
3495 for (i = 0; i < num_aces; i++) {
3496 if (dom_sid_equal(&nt_ace_list[i].trustee, &owner_sid)) {
3497 add_or_replace_ace(nt_ace_list, &num_aces,
3498 &orig_owner_sid,
3499 nt_ace_list[i].type,
3500 nt_ace_list[i].access_mask,
3501 nt_ace_list[i].flags);
3502 break;
3508 if (num_aces) {
3509 if((psa = make_sec_acl( talloc_tos(), NT4_ACL_REVISION, num_aces, nt_ace_list)) == NULL) {
3510 DEBUG(0,("get_nt_acl: Unable to malloc space for acl.\n"));
3511 goto done;
3514 } /* security_info & SECINFO_DACL */
3516 psd = make_standard_sec_desc( talloc_tos(),
3517 (security_info & SECINFO_OWNER) ? &owner_sid : NULL,
3518 (security_info & SECINFO_GROUP) ? &group_sid : NULL,
3519 psa,
3520 &sd_size);
3522 if(!psd) {
3523 DEBUG(0,("get_nt_acl: Unable to malloc space for security descriptor.\n"));
3524 sd_size = 0;
3525 goto done;
3529 * Windows 2000: The DACL_PROTECTED flag in the security
3530 * descriptor marks the ACL as non-inheriting, i.e., no
3531 * ACEs from higher level directories propagate to this
3532 * ACL. In the POSIX ACL model permissions are only
3533 * inherited at file create time, so ACLs never contain
3534 * any ACEs that are inherited dynamically. The DACL_PROTECTED
3535 * flag doesn't seem to bother Windows NT.
3536 * Always set this if map acl inherit is turned off.
3538 if (pal == NULL || !lp_map_acl_inherit(SNUM(conn))) {
3539 psd->type |= SEC_DESC_DACL_PROTECTED;
3540 } else {
3541 psd->type |= pal->sd_type;
3544 if (psd->dacl) {
3545 dacl_sort_into_canonical_order(psd->dacl->aces, (unsigned int)psd->dacl->num_aces);
3548 *ppdesc = psd;
3550 done:
3552 if (posix_acl) {
3553 SMB_VFS_SYS_ACL_FREE_ACL(conn, posix_acl);
3555 if (def_acl) {
3556 SMB_VFS_SYS_ACL_FREE_ACL(conn, def_acl);
3558 free_canon_ace_list(file_ace);
3559 free_canon_ace_list(dir_ace);
3560 free_inherited_info(pal);
3561 TALLOC_FREE(nt_ace_list);
3563 return NT_STATUS_OK;
3566 NTSTATUS posix_fget_nt_acl(struct files_struct *fsp, uint32_t security_info,
3567 struct security_descriptor **ppdesc)
3569 SMB_STRUCT_STAT sbuf;
3570 SMB_ACL_T posix_acl = NULL;
3571 struct pai_val *pal;
3573 *ppdesc = NULL;
3575 DEBUG(10,("posix_fget_nt_acl: called for file %s\n",
3576 fsp_str_dbg(fsp)));
3578 /* can it happen that fsp_name == NULL ? */
3579 if (fsp->is_directory || fsp->fh->fd == -1) {
3580 return posix_get_nt_acl(fsp->conn, fsp->fsp_name->base_name,
3581 security_info, ppdesc);
3584 /* Get the stat struct for the owner info. */
3585 if(SMB_VFS_FSTAT(fsp, &sbuf) != 0) {
3586 return map_nt_error_from_unix(errno);
3589 /* Get the ACL from the fd. */
3590 posix_acl = SMB_VFS_SYS_ACL_GET_FD(fsp);
3592 pal = fload_inherited_info(fsp);
3594 return posix_get_nt_acl_common(fsp->conn, fsp->fsp_name->base_name,
3595 &sbuf, pal, posix_acl, NULL,
3596 security_info, ppdesc);
3599 NTSTATUS posix_get_nt_acl(struct connection_struct *conn, const char *name,
3600 uint32_t security_info, struct security_descriptor **ppdesc)
3602 SMB_ACL_T posix_acl = NULL;
3603 SMB_ACL_T def_acl = NULL;
3604 struct pai_val *pal;
3605 struct smb_filename smb_fname;
3606 int ret;
3608 *ppdesc = NULL;
3610 DEBUG(10,("posix_get_nt_acl: called for file %s\n", name ));
3612 ZERO_STRUCT(smb_fname);
3613 smb_fname.base_name = discard_const_p(char, name);
3615 /* Get the stat struct for the owner info. */
3616 if (lp_posix_pathnames()) {
3617 ret = SMB_VFS_LSTAT(conn, &smb_fname);
3618 } else {
3619 ret = SMB_VFS_STAT(conn, &smb_fname);
3622 if (ret == -1) {
3623 return map_nt_error_from_unix(errno);
3626 /* Get the ACL from the path. */
3627 posix_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, name, SMB_ACL_TYPE_ACCESS);
3629 /* If it's a directory get the default POSIX ACL. */
3630 if(S_ISDIR(smb_fname.st.st_ex_mode)) {
3631 def_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, name, SMB_ACL_TYPE_DEFAULT);
3632 def_acl = free_empty_sys_acl(conn, def_acl);
3635 pal = load_inherited_info(conn, name);
3637 return posix_get_nt_acl_common(conn, name, &smb_fname.st, pal,
3638 posix_acl, def_acl, security_info,
3639 ppdesc);
3642 /****************************************************************************
3643 Try to chown a file. We will be able to chown it under the following conditions.
3645 1) If we have root privileges, then it will just work.
3646 2) If we have SeRestorePrivilege we can change the user + group to any other user.
3647 3) If we have SeTakeOwnershipPrivilege we can change the user to the current user.
3648 4) If we have write permission to the file and dos_filemodes is set
3649 then allow chown to the currently authenticated user.
3650 ****************************************************************************/
3652 NTSTATUS try_chown(files_struct *fsp, uid_t uid, gid_t gid)
3654 NTSTATUS status;
3656 if(!CAN_WRITE(fsp->conn)) {
3657 return NT_STATUS_MEDIA_WRITE_PROTECTED;
3660 /* Case (1). */
3661 status = vfs_chown_fsp(fsp, uid, gid);
3662 if (NT_STATUS_IS_OK(status)) {
3663 return status;
3666 /* Case (2) / (3) */
3667 if (lp_enable_privileges()) {
3668 bool has_take_ownership_priv = security_token_has_privilege(
3669 get_current_nttok(fsp->conn),
3670 SEC_PRIV_TAKE_OWNERSHIP);
3671 bool has_restore_priv = security_token_has_privilege(
3672 get_current_nttok(fsp->conn),
3673 SEC_PRIV_RESTORE);
3675 if (has_restore_priv) {
3676 ; /* Case (2) */
3677 } else if (has_take_ownership_priv) {
3678 /* Case (3) */
3679 if (uid == get_current_uid(fsp->conn)) {
3680 gid = (gid_t)-1;
3681 } else {
3682 has_take_ownership_priv = false;
3686 if (has_take_ownership_priv || has_restore_priv) {
3687 become_root();
3688 status = vfs_chown_fsp(fsp, uid, gid);
3689 unbecome_root();
3690 return status;
3694 /* Case (4). */
3695 if (!lp_dos_filemode(SNUM(fsp->conn))) {
3696 return NT_STATUS_ACCESS_DENIED;
3699 /* only allow chown to the current user. This is more secure,
3700 and also copes with the case where the SID in a take ownership ACL is
3701 a local SID on the users workstation
3703 if (uid != get_current_uid(fsp->conn)) {
3704 return NT_STATUS_ACCESS_DENIED;
3707 become_root();
3708 /* Keep the current file gid the same. */
3709 status = vfs_chown_fsp(fsp, uid, (gid_t)-1);
3710 unbecome_root();
3712 return status;
3715 #if 0
3716 /* Disable this - prevents ACL inheritance from the ACL editor. JRA. */
3718 /****************************************************************************
3719 Take care of parent ACL inheritance.
3720 ****************************************************************************/
3722 NTSTATUS append_parent_acl(files_struct *fsp,
3723 const struct security_descriptor *pcsd,
3724 struct security_descriptor **pp_new_sd)
3726 struct smb_filename *smb_dname = NULL;
3727 struct security_descriptor *parent_sd = NULL;
3728 files_struct *parent_fsp = NULL;
3729 TALLOC_CTX *mem_ctx = talloc_tos();
3730 char *parent_name = NULL;
3731 struct security_ace *new_ace = NULL;
3732 unsigned int num_aces = pcsd->dacl->num_aces;
3733 NTSTATUS status;
3734 int info;
3735 unsigned int i, j;
3736 struct security_descriptor *psd = dup_sec_desc(talloc_tos(), pcsd);
3737 bool is_dacl_protected = (pcsd->type & SEC_DESC_DACL_PROTECTED);
3739 if (psd == NULL) {
3740 return NT_STATUS_NO_MEMORY;
3743 if (!parent_dirname(mem_ctx, fsp->fsp_name->base_name, &parent_name,
3744 NULL)) {
3745 return NT_STATUS_NO_MEMORY;
3748 status = create_synthetic_smb_fname(mem_ctx, parent_name, NULL, NULL,
3749 &smb_dname);
3750 if (!NT_STATUS_IS_OK(status)) {
3751 goto fail;
3754 status = SMB_VFS_CREATE_FILE(
3755 fsp->conn, /* conn */
3756 NULL, /* req */
3757 0, /* root_dir_fid */
3758 smb_dname, /* fname */
3759 FILE_READ_ATTRIBUTES, /* access_mask */
3760 FILE_SHARE_NONE, /* share_access */
3761 FILE_OPEN, /* create_disposition*/
3762 FILE_DIRECTORY_FILE, /* create_options */
3763 0, /* file_attributes */
3764 INTERNAL_OPEN_ONLY, /* oplock_request */
3765 0, /* allocation_size */
3766 NULL, /* sd */
3767 NULL, /* ea_list */
3768 &parent_fsp, /* result */
3769 &info); /* pinfo */
3771 if (!NT_STATUS_IS_OK(status)) {
3772 TALLOC_FREE(smb_dname);
3773 return status;
3776 status = SMB_VFS_GET_NT_ACL(parent_fsp->conn, smb_dname->base_name,
3777 SECINFO_DACL, &parent_sd );
3779 close_file(NULL, parent_fsp, NORMAL_CLOSE);
3780 TALLOC_FREE(smb_dname);
3782 if (!NT_STATUS_IS_OK(status)) {
3783 return status;
3787 * Make room for potentially all the ACLs from
3788 * the parent. We used to add the ugw triple here,
3789 * as we knew we were dealing with POSIX ACLs.
3790 * We no longer need to do so as we can guarentee
3791 * that a default ACL from the parent directory will
3792 * be well formed for POSIX ACLs if it came from a
3793 * POSIX ACL source, and if we're not writing to a
3794 * POSIX ACL sink then we don't care if it's not well
3795 * formed. JRA.
3798 num_aces += parent_sd->dacl->num_aces;
3800 if((new_ace = talloc_zero_array(mem_ctx, struct security_ace,
3801 num_aces)) == NULL) {
3802 return NT_STATUS_NO_MEMORY;
3805 /* Start by copying in all the given ACE entries. */
3806 for (i = 0; i < psd->dacl->num_aces; i++) {
3807 sec_ace_copy(&new_ace[i], &psd->dacl->aces[i]);
3811 * Note that we're ignoring "inherit permissions" here
3812 * as that really only applies to newly created files. JRA.
3815 /* Finally append any inherited ACEs. */
3816 for (j = 0; j < parent_sd->dacl->num_aces; j++) {
3817 struct security_ace *se = &parent_sd->dacl->aces[j];
3819 if (fsp->is_directory) {
3820 if (!(se->flags & SEC_ACE_FLAG_CONTAINER_INHERIT)) {
3821 /* Doesn't apply to a directory - ignore. */
3822 DEBUG(10,("append_parent_acl: directory %s "
3823 "ignoring non container "
3824 "inherit flags %u on ACE with sid %s "
3825 "from parent %s\n",
3826 fsp_str_dbg(fsp),
3827 (unsigned int)se->flags,
3828 sid_string_dbg(&se->trustee),
3829 parent_name));
3830 continue;
3832 } else {
3833 if (!(se->flags & SEC_ACE_FLAG_OBJECT_INHERIT)) {
3834 /* Doesn't apply to a file - ignore. */
3835 DEBUG(10,("append_parent_acl: file %s "
3836 "ignoring non object "
3837 "inherit flags %u on ACE with sid %s "
3838 "from parent %s\n",
3839 fsp_str_dbg(fsp),
3840 (unsigned int)se->flags,
3841 sid_string_dbg(&se->trustee),
3842 parent_name));
3843 continue;
3847 if (is_dacl_protected) {
3848 /* If the DACL is protected it means we must
3849 * not overwrite an existing ACE entry with the
3850 * same SID. This is order N^2. Ouch :-(. JRA. */
3851 unsigned int k;
3852 for (k = 0; k < psd->dacl->num_aces; k++) {
3853 if (dom_sid_equal(&psd->dacl->aces[k].trustee,
3854 &se->trustee)) {
3855 break;
3858 if (k < psd->dacl->num_aces) {
3859 /* SID matched. Ignore. */
3860 DEBUG(10,("append_parent_acl: path %s "
3861 "ignoring ACE with protected sid %s "
3862 "from parent %s\n",
3863 fsp_str_dbg(fsp),
3864 sid_string_dbg(&se->trustee),
3865 parent_name));
3866 continue;
3870 sec_ace_copy(&new_ace[i], se);
3871 if (se->flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) {
3872 new_ace[i].flags &= ~(SEC_ACE_FLAG_VALID_INHERIT);
3874 new_ace[i].flags |= SEC_ACE_FLAG_INHERITED_ACE;
3876 if (fsp->is_directory) {
3878 * Strip off any inherit only. It's applied.
3880 new_ace[i].flags &= ~(SEC_ACE_FLAG_INHERIT_ONLY);
3881 if (se->flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) {
3882 /* No further inheritance. */
3883 new_ace[i].flags &=
3884 ~(SEC_ACE_FLAG_CONTAINER_INHERIT|
3885 SEC_ACE_FLAG_OBJECT_INHERIT);
3887 } else {
3889 * Strip off any container or inherit
3890 * flags, they can't apply to objects.
3892 new_ace[i].flags &= ~(SEC_ACE_FLAG_CONTAINER_INHERIT|
3893 SEC_ACE_FLAG_INHERIT_ONLY|
3894 SEC_ACE_FLAG_NO_PROPAGATE_INHERIT);
3896 i++;
3898 DEBUG(10,("append_parent_acl: path %s "
3899 "inheriting ACE with sid %s "
3900 "from parent %s\n",
3901 fsp_str_dbg(fsp),
3902 sid_string_dbg(&se->trustee),
3903 parent_name));
3906 psd->dacl->aces = new_ace;
3907 psd->dacl->num_aces = i;
3908 psd->type &= ~(SEC_DESC_DACL_AUTO_INHERITED|
3909 SEC_DESC_DACL_AUTO_INHERIT_REQ);
3911 *pp_new_sd = psd;
3912 return status;
3914 #endif
3916 /****************************************************************************
3917 Reply to set a security descriptor on an fsp. security_info_sent is the
3918 description of the following NT ACL.
3919 This should be the only external function needed for the UNIX style set ACL.
3920 We make a copy of psd_orig as internal functions modify the elements inside
3921 it, even though it's a const pointer.
3922 ****************************************************************************/
3924 NTSTATUS set_nt_acl(files_struct *fsp, uint32 security_info_sent, const struct security_descriptor *psd_orig)
3926 connection_struct *conn = fsp->conn;
3927 uid_t user = (uid_t)-1;
3928 gid_t grp = (gid_t)-1;
3929 struct dom_sid file_owner_sid;
3930 struct dom_sid file_grp_sid;
3931 canon_ace *file_ace_list = NULL;
3932 canon_ace *dir_ace_list = NULL;
3933 bool acl_perms = False;
3934 mode_t orig_mode = (mode_t)0;
3935 NTSTATUS status;
3936 bool set_acl_as_root = false;
3937 bool acl_set_support = false;
3938 bool ret = false;
3939 struct security_descriptor *psd = NULL;
3941 DEBUG(10,("set_nt_acl: called for file %s\n",
3942 fsp_str_dbg(fsp)));
3944 if (!CAN_WRITE(conn)) {
3945 DEBUG(10,("set acl rejected on read-only share\n"));
3946 return NT_STATUS_MEDIA_WRITE_PROTECTED;
3949 if (!psd_orig) {
3950 return NT_STATUS_INVALID_PARAMETER;
3953 psd = dup_sec_desc(talloc_tos(), psd_orig);
3954 if (!psd) {
3955 return NT_STATUS_NO_MEMORY;
3959 * Get the current state of the file.
3962 status = vfs_stat_fsp(fsp);
3963 if (!NT_STATUS_IS_OK(status)) {
3964 return status;
3967 /* Save the original element we check against. */
3968 orig_mode = fsp->fsp_name->st.st_ex_mode;
3971 * Unpack the user/group/world id's.
3974 /* POSIX can't cope with missing owner/group. */
3975 if ((security_info_sent & SECINFO_OWNER) && (psd->owner_sid == NULL)) {
3976 security_info_sent &= ~SECINFO_OWNER;
3978 if ((security_info_sent & SECINFO_GROUP) && (psd->group_sid == NULL)) {
3979 security_info_sent &= ~SECINFO_GROUP;
3982 status = unpack_nt_owners( conn, &user, &grp, security_info_sent, psd);
3983 if (!NT_STATUS_IS_OK(status)) {
3984 return status;
3988 * Do we need to chown ? If so this must be done first as the incoming
3989 * CREATOR_OWNER acl will be relative to the *new* owner, not the old.
3990 * Noticed by Simo.
3993 if (((user != (uid_t)-1) && (fsp->fsp_name->st.st_ex_uid != user)) ||
3994 (( grp != (gid_t)-1) && (fsp->fsp_name->st.st_ex_gid != grp))) {
3996 DEBUG(3,("set_nt_acl: chown %s. uid = %u, gid = %u.\n",
3997 fsp_str_dbg(fsp), (unsigned int)user,
3998 (unsigned int)grp));
4000 status = try_chown(fsp, user, grp);
4001 if(!NT_STATUS_IS_OK(status)) {
4002 DEBUG(3,("set_nt_acl: chown %s, %u, %u failed. Error "
4003 "= %s.\n", fsp_str_dbg(fsp),
4004 (unsigned int)user,
4005 (unsigned int)grp,
4006 nt_errstr(status)));
4007 return status;
4011 * Recheck the current state of the file, which may have changed.
4012 * (suid/sgid bits, for instance)
4015 status = vfs_stat_fsp(fsp);
4016 if (!NT_STATUS_IS_OK(status)) {
4017 return status;
4020 /* Save the original element we check against. */
4021 orig_mode = fsp->fsp_name->st.st_ex_mode;
4023 /* If we successfully chowned, we know we must
4024 * be able to set the acl, so do it as root.
4026 set_acl_as_root = true;
4029 create_file_sids(&fsp->fsp_name->st, &file_owner_sid, &file_grp_sid);
4031 if((security_info_sent & SECINFO_DACL) &&
4032 (psd->type & SEC_DESC_DACL_PRESENT) &&
4033 (psd->dacl == NULL)) {
4034 struct security_ace ace[3];
4036 /* We can't have NULL DACL in POSIX.
4037 Use owner/group/Everyone -> full access. */
4039 init_sec_ace(&ace[0],
4040 &file_owner_sid,
4041 SEC_ACE_TYPE_ACCESS_ALLOWED,
4042 GENERIC_ALL_ACCESS,
4044 init_sec_ace(&ace[1],
4045 &file_grp_sid,
4046 SEC_ACE_TYPE_ACCESS_ALLOWED,
4047 GENERIC_ALL_ACCESS,
4049 init_sec_ace(&ace[2],
4050 &global_sid_World,
4051 SEC_ACE_TYPE_ACCESS_ALLOWED,
4052 GENERIC_ALL_ACCESS,
4054 psd->dacl = make_sec_acl(talloc_tos(),
4055 NT4_ACL_REVISION,
4057 ace);
4058 if (psd->dacl == NULL) {
4059 return NT_STATUS_NO_MEMORY;
4061 security_acl_map_generic(psd->dacl, &file_generic_mapping);
4064 acl_perms = unpack_canon_ace(fsp, &fsp->fsp_name->st, &file_owner_sid,
4065 &file_grp_sid, &file_ace_list,
4066 &dir_ace_list, security_info_sent, psd);
4068 /* Ignore W2K traverse DACL set. */
4069 if (!file_ace_list && !dir_ace_list) {
4070 return NT_STATUS_OK;
4073 if (!acl_perms) {
4074 DEBUG(3,("set_nt_acl: cannot set permissions\n"));
4075 free_canon_ace_list(file_ace_list);
4076 free_canon_ace_list(dir_ace_list);
4077 return NT_STATUS_ACCESS_DENIED;
4081 * Only change security if we got a DACL.
4084 if(!(security_info_sent & SECINFO_DACL) || (psd->dacl == NULL)) {
4085 free_canon_ace_list(file_ace_list);
4086 free_canon_ace_list(dir_ace_list);
4087 return NT_STATUS_OK;
4091 * Try using the POSIX ACL set first. Fall back to chmod if
4092 * we have no ACL support on this filesystem.
4095 if (acl_perms && file_ace_list) {
4096 if (set_acl_as_root) {
4097 become_root();
4099 ret = set_canon_ace_list(fsp, file_ace_list, false,
4100 &fsp->fsp_name->st, &acl_set_support);
4101 if (set_acl_as_root) {
4102 unbecome_root();
4104 if (acl_set_support && ret == false) {
4105 DEBUG(3,("set_nt_acl: failed to set file acl on file "
4106 "%s (%s).\n", fsp_str_dbg(fsp),
4107 strerror(errno)));
4108 free_canon_ace_list(file_ace_list);
4109 free_canon_ace_list(dir_ace_list);
4110 return map_nt_error_from_unix(errno);
4114 if (acl_perms && acl_set_support && fsp->is_directory) {
4115 if (dir_ace_list) {
4116 if (set_acl_as_root) {
4117 become_root();
4119 ret = set_canon_ace_list(fsp, dir_ace_list, true,
4120 &fsp->fsp_name->st,
4121 &acl_set_support);
4122 if (set_acl_as_root) {
4123 unbecome_root();
4125 if (ret == false) {
4126 DEBUG(3,("set_nt_acl: failed to set default "
4127 "acl on directory %s (%s).\n",
4128 fsp_str_dbg(fsp), strerror(errno)));
4129 free_canon_ace_list(file_ace_list);
4130 free_canon_ace_list(dir_ace_list);
4131 return map_nt_error_from_unix(errno);
4133 } else {
4134 int sret = -1;
4137 * No default ACL - delete one if it exists.
4140 if (set_acl_as_root) {
4141 become_root();
4143 sret = SMB_VFS_SYS_ACL_DELETE_DEF_FILE(conn,
4144 fsp->fsp_name->base_name);
4145 if (set_acl_as_root) {
4146 unbecome_root();
4148 if (sret == -1) {
4149 if (acl_group_override(conn, fsp->fsp_name)) {
4150 DEBUG(5,("set_nt_acl: acl group "
4151 "control on and current user "
4152 "in file %s primary group. "
4153 "Override delete_def_acl\n",
4154 fsp_str_dbg(fsp)));
4156 become_root();
4157 sret =
4158 SMB_VFS_SYS_ACL_DELETE_DEF_FILE(
4159 conn,
4160 fsp->fsp_name->base_name);
4161 unbecome_root();
4164 if (sret == -1) {
4165 DEBUG(3,("set_nt_acl: sys_acl_delete_def_file failed (%s)\n", strerror(errno)));
4166 free_canon_ace_list(file_ace_list);
4167 free_canon_ace_list(dir_ace_list);
4168 return map_nt_error_from_unix(errno);
4174 if (acl_set_support) {
4175 if (set_acl_as_root) {
4176 become_root();
4178 store_inheritance_attributes(fsp,
4179 file_ace_list,
4180 dir_ace_list,
4181 psd->type);
4182 if (set_acl_as_root) {
4183 unbecome_root();
4188 * If we cannot set using POSIX ACLs we fall back to checking if we need to chmod.
4191 if(!acl_set_support && acl_perms) {
4192 mode_t posix_perms;
4194 if (!convert_canon_ace_to_posix_perms( fsp, file_ace_list, &posix_perms)) {
4195 free_canon_ace_list(file_ace_list);
4196 free_canon_ace_list(dir_ace_list);
4197 DEBUG(3,("set_nt_acl: failed to convert file acl to "
4198 "posix permissions for file %s.\n",
4199 fsp_str_dbg(fsp)));
4200 return NT_STATUS_ACCESS_DENIED;
4203 if (orig_mode != posix_perms) {
4204 int sret = -1;
4206 DEBUG(3,("set_nt_acl: chmod %s. perms = 0%o.\n",
4207 fsp_str_dbg(fsp), (unsigned int)posix_perms));
4209 if (set_acl_as_root) {
4210 become_root();
4212 sret = SMB_VFS_CHMOD(conn, fsp->fsp_name->base_name,
4213 posix_perms);
4214 if (set_acl_as_root) {
4215 unbecome_root();
4217 if(sret == -1) {
4218 if (acl_group_override(conn, fsp->fsp_name)) {
4219 DEBUG(5,("set_nt_acl: acl group "
4220 "control on and current user "
4221 "in file %s primary group. "
4222 "Override chmod\n",
4223 fsp_str_dbg(fsp)));
4225 become_root();
4226 sret = SMB_VFS_CHMOD(conn,
4227 fsp->fsp_name->base_name,
4228 posix_perms);
4229 unbecome_root();
4232 if (sret == -1) {
4233 DEBUG(3,("set_nt_acl: chmod %s, 0%o "
4234 "failed. Error = %s.\n",
4235 fsp_str_dbg(fsp),
4236 (unsigned int)posix_perms,
4237 strerror(errno)));
4238 free_canon_ace_list(file_ace_list);
4239 free_canon_ace_list(dir_ace_list);
4240 return map_nt_error_from_unix(errno);
4246 free_canon_ace_list(file_ace_list);
4247 free_canon_ace_list(dir_ace_list);
4249 /* Ensure the stat struct in the fsp is correct. */
4250 status = vfs_stat_fsp(fsp);
4252 return NT_STATUS_OK;
4255 /****************************************************************************
4256 Get the actual group bits stored on a file with an ACL. Has no effect if
4257 the file has no ACL. Needed in dosmode code where the stat() will return
4258 the mask bits, not the real group bits, for a file with an ACL.
4259 ****************************************************************************/
4261 int get_acl_group_bits( connection_struct *conn, const char *fname, mode_t *mode )
4263 int entry_id = SMB_ACL_FIRST_ENTRY;
4264 SMB_ACL_ENTRY_T entry;
4265 SMB_ACL_T posix_acl;
4266 int result = -1;
4268 posix_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, fname, SMB_ACL_TYPE_ACCESS);
4269 if (posix_acl == (SMB_ACL_T)NULL)
4270 return -1;
4272 while (SMB_VFS_SYS_ACL_GET_ENTRY(conn, posix_acl, entry_id, &entry) == 1) {
4273 SMB_ACL_TAG_T tagtype;
4274 SMB_ACL_PERMSET_T permset;
4276 entry_id = SMB_ACL_NEXT_ENTRY;
4278 if (SMB_VFS_SYS_ACL_GET_TAG_TYPE(conn, entry, &tagtype) ==-1)
4279 break;
4281 if (tagtype == SMB_ACL_GROUP_OBJ) {
4282 if (SMB_VFS_SYS_ACL_GET_PERMSET(conn, entry, &permset) == -1) {
4283 break;
4284 } else {
4285 *mode &= ~(S_IRGRP|S_IWGRP|S_IXGRP);
4286 *mode |= (SMB_VFS_SYS_ACL_GET_PERM(conn, permset, SMB_ACL_READ) ? S_IRGRP : 0);
4287 *mode |= (SMB_VFS_SYS_ACL_GET_PERM(conn, permset, SMB_ACL_WRITE) ? S_IWGRP : 0);
4288 *mode |= (SMB_VFS_SYS_ACL_GET_PERM(conn, permset, SMB_ACL_EXECUTE) ? S_IXGRP : 0);
4289 result = 0;
4290 break;
4294 SMB_VFS_SYS_ACL_FREE_ACL(conn, posix_acl);
4295 return result;
4298 /****************************************************************************
4299 Do a chmod by setting the ACL USER_OBJ, GROUP_OBJ and OTHER bits in an ACL
4300 and set the mask to rwx. Needed to preserve complex ACLs set by NT.
4301 ****************************************************************************/
4303 static int chmod_acl_internals( connection_struct *conn, SMB_ACL_T posix_acl, mode_t mode)
4305 int entry_id = SMB_ACL_FIRST_ENTRY;
4306 SMB_ACL_ENTRY_T entry;
4307 int num_entries = 0;
4309 while ( SMB_VFS_SYS_ACL_GET_ENTRY(conn, posix_acl, entry_id, &entry) == 1) {
4310 SMB_ACL_TAG_T tagtype;
4311 SMB_ACL_PERMSET_T permset;
4312 mode_t perms;
4314 entry_id = SMB_ACL_NEXT_ENTRY;
4316 if (SMB_VFS_SYS_ACL_GET_TAG_TYPE(conn, entry, &tagtype) == -1)
4317 return -1;
4319 if (SMB_VFS_SYS_ACL_GET_PERMSET(conn, entry, &permset) == -1)
4320 return -1;
4322 num_entries++;
4324 switch(tagtype) {
4325 case SMB_ACL_USER_OBJ:
4326 perms = unix_perms_to_acl_perms(mode, S_IRUSR, S_IWUSR, S_IXUSR);
4327 break;
4328 case SMB_ACL_GROUP_OBJ:
4329 perms = unix_perms_to_acl_perms(mode, S_IRGRP, S_IWGRP, S_IXGRP);
4330 break;
4331 case SMB_ACL_MASK:
4333 * FIXME: The ACL_MASK entry permissions should really be set to
4334 * the union of the permissions of all ACL_USER,
4335 * ACL_GROUP_OBJ, and ACL_GROUP entries. That's what
4336 * acl_calc_mask() does, but Samba ACLs doesn't provide it.
4338 perms = S_IRUSR|S_IWUSR|S_IXUSR;
4339 break;
4340 case SMB_ACL_OTHER:
4341 perms = unix_perms_to_acl_perms(mode, S_IROTH, S_IWOTH, S_IXOTH);
4342 break;
4343 default:
4344 continue;
4347 if (map_acl_perms_to_permset(conn, perms, &permset) == -1)
4348 return -1;
4350 if (SMB_VFS_SYS_ACL_SET_PERMSET(conn, entry, permset) == -1)
4351 return -1;
4355 * If this is a simple 3 element ACL or no elements then it's a standard
4356 * UNIX permission set. Just use chmod...
4359 if ((num_entries == 3) || (num_entries == 0))
4360 return -1;
4362 return 0;
4365 /****************************************************************************
4366 Get the access ACL of FROM, do a chmod by setting the ACL USER_OBJ,
4367 GROUP_OBJ and OTHER bits in an ACL and set the mask to rwx. Set the
4368 resulting ACL on TO. Note that name is in UNIX character set.
4369 ****************************************************************************/
4371 static int copy_access_posix_acl(connection_struct *conn, const char *from, const char *to, mode_t mode)
4373 SMB_ACL_T posix_acl = NULL;
4374 int ret = -1;
4376 if ((posix_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, from, SMB_ACL_TYPE_ACCESS)) == NULL)
4377 return -1;
4379 if ((ret = chmod_acl_internals(conn, posix_acl, mode)) == -1)
4380 goto done;
4382 ret = SMB_VFS_SYS_ACL_SET_FILE(conn, to, SMB_ACL_TYPE_ACCESS, posix_acl);
4384 done:
4386 SMB_VFS_SYS_ACL_FREE_ACL(conn, posix_acl);
4387 return ret;
4390 /****************************************************************************
4391 Do a chmod by setting the ACL USER_OBJ, GROUP_OBJ and OTHER bits in an ACL
4392 and set the mask to rwx. Needed to preserve complex ACLs set by NT.
4393 Note that name is in UNIX character set.
4394 ****************************************************************************/
4396 int chmod_acl(connection_struct *conn, const char *name, mode_t mode)
4398 return copy_access_posix_acl(conn, name, name, mode);
4401 /****************************************************************************
4402 Check for an existing default POSIX ACL on a directory.
4403 ****************************************************************************/
4405 static bool directory_has_default_posix_acl(connection_struct *conn, const char *fname)
4407 SMB_ACL_T def_acl = SMB_VFS_SYS_ACL_GET_FILE( conn, fname, SMB_ACL_TYPE_DEFAULT);
4408 bool has_acl = False;
4409 SMB_ACL_ENTRY_T entry;
4411 if (def_acl != NULL && (SMB_VFS_SYS_ACL_GET_ENTRY(conn, def_acl, SMB_ACL_FIRST_ENTRY, &entry) == 1)) {
4412 has_acl = True;
4415 if (def_acl) {
4416 SMB_VFS_SYS_ACL_FREE_ACL(conn, def_acl);
4418 return has_acl;
4421 /****************************************************************************
4422 If the parent directory has no default ACL but it does have an Access ACL,
4423 inherit this Access ACL to file name.
4424 ****************************************************************************/
4426 int inherit_access_posix_acl(connection_struct *conn, const char *inherit_from_dir,
4427 const char *name, mode_t mode)
4429 if (directory_has_default_posix_acl(conn, inherit_from_dir))
4430 return 0;
4432 return copy_access_posix_acl(conn, inherit_from_dir, name, mode);
4435 /****************************************************************************
4436 Do an fchmod by setting the ACL USER_OBJ, GROUP_OBJ and OTHER bits in an ACL
4437 and set the mask to rwx. Needed to preserve complex ACLs set by NT.
4438 ****************************************************************************/
4440 int fchmod_acl(files_struct *fsp, mode_t mode)
4442 connection_struct *conn = fsp->conn;
4443 SMB_ACL_T posix_acl = NULL;
4444 int ret = -1;
4446 if ((posix_acl = SMB_VFS_SYS_ACL_GET_FD(fsp)) == NULL)
4447 return -1;
4449 if ((ret = chmod_acl_internals(conn, posix_acl, mode)) == -1)
4450 goto done;
4452 ret = SMB_VFS_SYS_ACL_SET_FD(fsp, posix_acl);
4454 done:
4456 SMB_VFS_SYS_ACL_FREE_ACL(conn, posix_acl);
4457 return ret;
4460 /****************************************************************************
4461 Map from wire type to permset.
4462 ****************************************************************************/
4464 static bool unix_ex_wire_to_permset(connection_struct *conn, unsigned char wire_perm, SMB_ACL_PERMSET_T *p_permset)
4466 if (wire_perm & ~(SMB_POSIX_ACL_READ|SMB_POSIX_ACL_WRITE|SMB_POSIX_ACL_EXECUTE)) {
4467 return False;
4470 if (SMB_VFS_SYS_ACL_CLEAR_PERMS(conn, *p_permset) == -1) {
4471 return False;
4474 if (wire_perm & SMB_POSIX_ACL_READ) {
4475 if (SMB_VFS_SYS_ACL_ADD_PERM(conn, *p_permset, SMB_ACL_READ) == -1) {
4476 return False;
4479 if (wire_perm & SMB_POSIX_ACL_WRITE) {
4480 if (SMB_VFS_SYS_ACL_ADD_PERM(conn, *p_permset, SMB_ACL_WRITE) == -1) {
4481 return False;
4484 if (wire_perm & SMB_POSIX_ACL_EXECUTE) {
4485 if (SMB_VFS_SYS_ACL_ADD_PERM(conn, *p_permset, SMB_ACL_EXECUTE) == -1) {
4486 return False;
4489 return True;
4492 /****************************************************************************
4493 Map from wire type to tagtype.
4494 ****************************************************************************/
4496 static bool unix_ex_wire_to_tagtype(unsigned char wire_tt, SMB_ACL_TAG_T *p_tt)
4498 switch (wire_tt) {
4499 case SMB_POSIX_ACL_USER_OBJ:
4500 *p_tt = SMB_ACL_USER_OBJ;
4501 break;
4502 case SMB_POSIX_ACL_USER:
4503 *p_tt = SMB_ACL_USER;
4504 break;
4505 case SMB_POSIX_ACL_GROUP_OBJ:
4506 *p_tt = SMB_ACL_GROUP_OBJ;
4507 break;
4508 case SMB_POSIX_ACL_GROUP:
4509 *p_tt = SMB_ACL_GROUP;
4510 break;
4511 case SMB_POSIX_ACL_MASK:
4512 *p_tt = SMB_ACL_MASK;
4513 break;
4514 case SMB_POSIX_ACL_OTHER:
4515 *p_tt = SMB_ACL_OTHER;
4516 break;
4517 default:
4518 return False;
4520 return True;
4523 /****************************************************************************
4524 Create a new POSIX acl from wire permissions.
4525 FIXME ! How does the share mask/mode fit into this.... ?
4526 ****************************************************************************/
4528 static SMB_ACL_T create_posix_acl_from_wire(connection_struct *conn, uint16 num_acls, const char *pdata)
4530 unsigned int i;
4531 SMB_ACL_T the_acl = SMB_VFS_SYS_ACL_INIT(conn, num_acls);
4533 if (the_acl == NULL) {
4534 return NULL;
4537 for (i = 0; i < num_acls; i++) {
4538 SMB_ACL_ENTRY_T the_entry;
4539 SMB_ACL_PERMSET_T the_permset;
4540 SMB_ACL_TAG_T tag_type;
4542 if (SMB_VFS_SYS_ACL_CREATE_ENTRY(conn, &the_acl, &the_entry) == -1) {
4543 DEBUG(0,("create_posix_acl_from_wire: Failed to create entry %u. (%s)\n",
4544 i, strerror(errno) ));
4545 goto fail;
4548 if (!unix_ex_wire_to_tagtype(CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)), &tag_type)) {
4549 DEBUG(0,("create_posix_acl_from_wire: invalid wire tagtype %u on entry %u.\n",
4550 CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)), i ));
4551 goto fail;
4554 if (SMB_VFS_SYS_ACL_SET_TAG_TYPE(conn, the_entry, tag_type) == -1) {
4555 DEBUG(0,("create_posix_acl_from_wire: Failed to set tagtype on entry %u. (%s)\n",
4556 i, strerror(errno) ));
4557 goto fail;
4560 /* Get the permset pointer from the new ACL entry. */
4561 if (SMB_VFS_SYS_ACL_GET_PERMSET(conn, the_entry, &the_permset) == -1) {
4562 DEBUG(0,("create_posix_acl_from_wire: Failed to get permset on entry %u. (%s)\n",
4563 i, strerror(errno) ));
4564 goto fail;
4567 /* Map from wire to permissions. */
4568 if (!unix_ex_wire_to_permset(conn, CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)+1), &the_permset)) {
4569 DEBUG(0,("create_posix_acl_from_wire: invalid permset %u on entry %u.\n",
4570 CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE) + 1), i ));
4571 goto fail;
4574 /* Now apply to the new ACL entry. */
4575 if (SMB_VFS_SYS_ACL_SET_PERMSET(conn, the_entry, the_permset) == -1) {
4576 DEBUG(0,("create_posix_acl_from_wire: Failed to add permset on entry %u. (%s)\n",
4577 i, strerror(errno) ));
4578 goto fail;
4581 if (tag_type == SMB_ACL_USER) {
4582 uint32 uidval = IVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
4583 uid_t uid = (uid_t)uidval;
4584 if (SMB_VFS_SYS_ACL_SET_QUALIFIER(conn, the_entry,(void *)&uid) == -1) {
4585 DEBUG(0,("create_posix_acl_from_wire: Failed to set uid %u on entry %u. (%s)\n",
4586 (unsigned int)uid, i, strerror(errno) ));
4587 goto fail;
4591 if (tag_type == SMB_ACL_GROUP) {
4592 uint32 gidval = IVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
4593 gid_t gid = (uid_t)gidval;
4594 if (SMB_VFS_SYS_ACL_SET_QUALIFIER(conn, the_entry,(void *)&gid) == -1) {
4595 DEBUG(0,("create_posix_acl_from_wire: Failed to set gid %u on entry %u. (%s)\n",
4596 (unsigned int)gid, i, strerror(errno) ));
4597 goto fail;
4602 return the_acl;
4604 fail:
4606 if (the_acl != NULL) {
4607 SMB_VFS_SYS_ACL_FREE_ACL(conn, the_acl);
4609 return NULL;
4612 /****************************************************************************
4613 Calls from UNIX extensions - Default POSIX ACL set.
4614 If num_def_acls == 0 and not a directory just return. If it is a directory
4615 and num_def_acls == 0 then remove the default acl. Else set the default acl
4616 on the directory.
4617 ****************************************************************************/
4619 bool set_unix_posix_default_acl(connection_struct *conn, const char *fname, const SMB_STRUCT_STAT *psbuf,
4620 uint16 num_def_acls, const char *pdata)
4622 SMB_ACL_T def_acl = NULL;
4624 if (!S_ISDIR(psbuf->st_ex_mode)) {
4625 if (num_def_acls) {
4626 DEBUG(5,("set_unix_posix_default_acl: Can't set default ACL on non-directory file %s\n", fname ));
4627 errno = EISDIR;
4628 return False;
4629 } else {
4630 return True;
4634 if (!num_def_acls) {
4635 /* Remove the default ACL. */
4636 if (SMB_VFS_SYS_ACL_DELETE_DEF_FILE(conn, fname) == -1) {
4637 DEBUG(5,("set_unix_posix_default_acl: acl_delete_def_file failed on directory %s (%s)\n",
4638 fname, strerror(errno) ));
4639 return False;
4641 return True;
4644 if ((def_acl = create_posix_acl_from_wire(conn, num_def_acls, pdata)) == NULL) {
4645 return False;
4648 if (SMB_VFS_SYS_ACL_SET_FILE(conn, fname, SMB_ACL_TYPE_DEFAULT, def_acl) == -1) {
4649 DEBUG(5,("set_unix_posix_default_acl: acl_set_file failed on directory %s (%s)\n",
4650 fname, strerror(errno) ));
4651 SMB_VFS_SYS_ACL_FREE_ACL(conn, def_acl);
4652 return False;
4655 DEBUG(10,("set_unix_posix_default_acl: set default acl for file %s\n", fname ));
4656 SMB_VFS_SYS_ACL_FREE_ACL(conn, def_acl);
4657 return True;
4660 /****************************************************************************
4661 Remove an ACL from a file. As we don't have acl_delete_entry() available
4662 we must read the current acl and copy all entries except MASK, USER and GROUP
4663 to a new acl, then set that. This (at least on Linux) causes any ACL to be
4664 removed.
4665 FIXME ! How does the share mask/mode fit into this.... ?
4666 ****************************************************************************/
4668 static bool remove_posix_acl(connection_struct *conn, files_struct *fsp, const char *fname)
4670 SMB_ACL_T file_acl = NULL;
4671 int entry_id = SMB_ACL_FIRST_ENTRY;
4672 SMB_ACL_ENTRY_T entry;
4673 bool ret = False;
4674 /* Create a new ACL with only 3 entries, u/g/w. */
4675 SMB_ACL_T new_file_acl = SMB_VFS_SYS_ACL_INIT(conn, 3);
4676 SMB_ACL_ENTRY_T user_ent = NULL;
4677 SMB_ACL_ENTRY_T group_ent = NULL;
4678 SMB_ACL_ENTRY_T other_ent = NULL;
4680 if (new_file_acl == NULL) {
4681 DEBUG(5,("remove_posix_acl: failed to init new ACL with 3 entries for file %s.\n", fname));
4682 return False;
4685 /* Now create the u/g/w entries. */
4686 if (SMB_VFS_SYS_ACL_CREATE_ENTRY(conn, &new_file_acl, &user_ent) == -1) {
4687 DEBUG(5,("remove_posix_acl: Failed to create user entry for file %s. (%s)\n",
4688 fname, strerror(errno) ));
4689 goto done;
4691 if (SMB_VFS_SYS_ACL_SET_TAG_TYPE(conn, user_ent, SMB_ACL_USER_OBJ) == -1) {
4692 DEBUG(5,("remove_posix_acl: Failed to set user entry for file %s. (%s)\n",
4693 fname, strerror(errno) ));
4694 goto done;
4697 if (SMB_VFS_SYS_ACL_CREATE_ENTRY(conn, &new_file_acl, &group_ent) == -1) {
4698 DEBUG(5,("remove_posix_acl: Failed to create group entry for file %s. (%s)\n",
4699 fname, strerror(errno) ));
4700 goto done;
4702 if (SMB_VFS_SYS_ACL_SET_TAG_TYPE(conn, group_ent, SMB_ACL_GROUP_OBJ) == -1) {
4703 DEBUG(5,("remove_posix_acl: Failed to set group entry for file %s. (%s)\n",
4704 fname, strerror(errno) ));
4705 goto done;
4708 if (SMB_VFS_SYS_ACL_CREATE_ENTRY(conn, &new_file_acl, &other_ent) == -1) {
4709 DEBUG(5,("remove_posix_acl: Failed to create other entry for file %s. (%s)\n",
4710 fname, strerror(errno) ));
4711 goto done;
4713 if (SMB_VFS_SYS_ACL_SET_TAG_TYPE(conn, other_ent, SMB_ACL_OTHER) == -1) {
4714 DEBUG(5,("remove_posix_acl: Failed to set other entry for file %s. (%s)\n",
4715 fname, strerror(errno) ));
4716 goto done;
4719 /* Get the current file ACL. */
4720 if (fsp && fsp->fh->fd != -1) {
4721 file_acl = SMB_VFS_SYS_ACL_GET_FD(fsp);
4722 } else {
4723 file_acl = SMB_VFS_SYS_ACL_GET_FILE( conn, fname, SMB_ACL_TYPE_ACCESS);
4726 if (file_acl == NULL) {
4727 /* This is only returned if an error occurred. Even for a file with
4728 no acl a u/g/w acl should be returned. */
4729 DEBUG(5,("remove_posix_acl: failed to get ACL from file %s (%s).\n",
4730 fname, strerror(errno) ));
4731 goto done;
4734 while ( SMB_VFS_SYS_ACL_GET_ENTRY(conn, file_acl, entry_id, &entry) == 1) {
4735 SMB_ACL_TAG_T tagtype;
4736 SMB_ACL_PERMSET_T permset;
4738 entry_id = SMB_ACL_NEXT_ENTRY;
4740 if (SMB_VFS_SYS_ACL_GET_TAG_TYPE(conn, entry, &tagtype) == -1) {
4741 DEBUG(5,("remove_posix_acl: failed to get tagtype from ACL on file %s (%s).\n",
4742 fname, strerror(errno) ));
4743 goto done;
4746 if (SMB_VFS_SYS_ACL_GET_PERMSET(conn, entry, &permset) == -1) {
4747 DEBUG(5,("remove_posix_acl: failed to get permset from ACL on file %s (%s).\n",
4748 fname, strerror(errno) ));
4749 goto done;
4752 if (tagtype == SMB_ACL_USER_OBJ) {
4753 if (SMB_VFS_SYS_ACL_SET_PERMSET(conn, user_ent, permset) == -1) {
4754 DEBUG(5,("remove_posix_acl: failed to set permset from ACL on file %s (%s).\n",
4755 fname, strerror(errno) ));
4757 } else if (tagtype == SMB_ACL_GROUP_OBJ) {
4758 if (SMB_VFS_SYS_ACL_SET_PERMSET(conn, group_ent, permset) == -1) {
4759 DEBUG(5,("remove_posix_acl: failed to set permset from ACL on file %s (%s).\n",
4760 fname, strerror(errno) ));
4762 } else if (tagtype == SMB_ACL_OTHER) {
4763 if (SMB_VFS_SYS_ACL_SET_PERMSET(conn, other_ent, permset) == -1) {
4764 DEBUG(5,("remove_posix_acl: failed to set permset from ACL on file %s (%s).\n",
4765 fname, strerror(errno) ));
4770 /* Set the new empty file ACL. */
4771 if (fsp && fsp->fh->fd != -1) {
4772 if (SMB_VFS_SYS_ACL_SET_FD(fsp, new_file_acl) == -1) {
4773 DEBUG(5,("remove_posix_acl: acl_set_file failed on %s (%s)\n",
4774 fname, strerror(errno) ));
4775 goto done;
4777 } else {
4778 if (SMB_VFS_SYS_ACL_SET_FILE(conn, fname, SMB_ACL_TYPE_ACCESS, new_file_acl) == -1) {
4779 DEBUG(5,("remove_posix_acl: acl_set_file failed on %s (%s)\n",
4780 fname, strerror(errno) ));
4781 goto done;
4785 ret = True;
4787 done:
4789 if (file_acl) {
4790 SMB_VFS_SYS_ACL_FREE_ACL(conn, file_acl);
4792 if (new_file_acl) {
4793 SMB_VFS_SYS_ACL_FREE_ACL(conn, new_file_acl);
4795 return ret;
4798 /****************************************************************************
4799 Calls from UNIX extensions - POSIX ACL set.
4800 If num_def_acls == 0 then read/modify/write acl after removing all entries
4801 except SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER.
4802 ****************************************************************************/
4804 bool set_unix_posix_acl(connection_struct *conn, files_struct *fsp, const char *fname, uint16 num_acls, const char *pdata)
4806 SMB_ACL_T file_acl = NULL;
4808 if (!num_acls) {
4809 /* Remove the ACL from the file. */
4810 return remove_posix_acl(conn, fsp, fname);
4813 if ((file_acl = create_posix_acl_from_wire(conn, num_acls, pdata)) == NULL) {
4814 return False;
4817 if (fsp && fsp->fh->fd != -1) {
4818 /* The preferred way - use an open fd. */
4819 if (SMB_VFS_SYS_ACL_SET_FD(fsp, file_acl) == -1) {
4820 DEBUG(5,("set_unix_posix_acl: acl_set_file failed on %s (%s)\n",
4821 fname, strerror(errno) ));
4822 SMB_VFS_SYS_ACL_FREE_ACL(conn, file_acl);
4823 return False;
4825 } else {
4826 if (SMB_VFS_SYS_ACL_SET_FILE(conn, fname, SMB_ACL_TYPE_ACCESS, file_acl) == -1) {
4827 DEBUG(5,("set_unix_posix_acl: acl_set_file failed on %s (%s)\n",
4828 fname, strerror(errno) ));
4829 SMB_VFS_SYS_ACL_FREE_ACL(conn, file_acl);
4830 return False;
4834 DEBUG(10,("set_unix_posix_acl: set acl for file %s\n", fname ));
4835 SMB_VFS_SYS_ACL_FREE_ACL(conn, file_acl);
4836 return True;
4839 /********************************************************************
4840 Pull the NT ACL from a file on disk or the OpenEventlog() access
4841 check. Caller is responsible for freeing the returned security
4842 descriptor via TALLOC_FREE(). This is designed for dealing with
4843 user space access checks in smbd outside of the VFS. For example,
4844 checking access rights in OpenEventlog().
4846 Assume we are dealing with files (for now)
4847 ********************************************************************/
4849 struct security_descriptor *get_nt_acl_no_snum( TALLOC_CTX *ctx, const char *fname)
4851 struct security_descriptor *psd, *ret_sd;
4852 connection_struct *conn;
4853 files_struct finfo;
4854 struct fd_handle fh;
4855 NTSTATUS status;
4857 conn = talloc_zero(ctx, connection_struct);
4858 if (conn == NULL) {
4859 DEBUG(0, ("talloc failed\n"));
4860 return NULL;
4863 if (!(conn->params = talloc(conn, struct share_params))) {
4864 DEBUG(0,("get_nt_acl_no_snum: talloc() failed!\n"));
4865 TALLOC_FREE(conn);
4866 return NULL;
4869 conn->params->service = -1;
4871 set_conn_connectpath(conn, "/");
4873 if (!smbd_vfs_init(conn)) {
4874 DEBUG(0,("get_nt_acl_no_snum: Unable to create a fake connection struct!\n"));
4875 conn_free(conn);
4876 return NULL;
4879 ZERO_STRUCT( finfo );
4880 ZERO_STRUCT( fh );
4882 finfo.fnum = FNUM_FIELD_INVALID;
4883 finfo.conn = conn;
4884 finfo.fh = &fh;
4885 finfo.fh->fd = -1;
4887 status = create_synthetic_smb_fname(talloc_tos(), fname, NULL, NULL,
4888 &finfo.fsp_name);
4889 if (!NT_STATUS_IS_OK(status)) {
4890 conn_free(conn);
4891 return NULL;
4894 if (!NT_STATUS_IS_OK(SMB_VFS_FGET_NT_ACL( &finfo, SECINFO_DACL, &psd))) {
4895 DEBUG(0,("get_nt_acl_no_snum: get_nt_acl returned zero.\n"));
4896 TALLOC_FREE(finfo.fsp_name);
4897 conn_free(conn);
4898 return NULL;
4901 ret_sd = dup_sec_desc( ctx, psd );
4903 TALLOC_FREE(finfo.fsp_name);
4904 conn_free(conn);
4906 return ret_sd;
4909 /* Stolen shamelessly from pvfs_default_acl() in source4 :-). */
4911 NTSTATUS make_default_filesystem_acl(TALLOC_CTX *ctx,
4912 const char *name,
4913 SMB_STRUCT_STAT *psbuf,
4914 struct security_descriptor **ppdesc)
4916 struct dom_sid owner_sid, group_sid;
4917 size_t size = 0;
4918 struct security_ace aces[4];
4919 uint32_t access_mask = 0;
4920 mode_t mode = psbuf->st_ex_mode;
4921 struct security_acl *new_dacl = NULL;
4922 int idx = 0;
4924 DEBUG(10,("make_default_filesystem_acl: file %s mode = 0%o\n",
4925 name, (int)mode ));
4927 uid_to_sid(&owner_sid, psbuf->st_ex_uid);
4928 gid_to_sid(&group_sid, psbuf->st_ex_gid);
4931 We provide up to 4 ACEs
4932 - Owner
4933 - Group
4934 - Everyone
4935 - NT System
4938 if (mode & S_IRUSR) {
4939 if (mode & S_IWUSR) {
4940 access_mask |= SEC_RIGHTS_FILE_ALL;
4941 } else {
4942 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
4945 if (mode & S_IWUSR) {
4946 access_mask |= SEC_RIGHTS_FILE_WRITE | SEC_STD_DELETE;
4949 init_sec_ace(&aces[idx],
4950 &owner_sid,
4951 SEC_ACE_TYPE_ACCESS_ALLOWED,
4952 access_mask,
4954 idx++;
4956 access_mask = 0;
4957 if (mode & S_IRGRP) {
4958 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
4960 if (mode & S_IWGRP) {
4961 /* note that delete is not granted - this matches posix behaviour */
4962 access_mask |= SEC_RIGHTS_FILE_WRITE;
4964 if (access_mask) {
4965 init_sec_ace(&aces[idx],
4966 &group_sid,
4967 SEC_ACE_TYPE_ACCESS_ALLOWED,
4968 access_mask,
4970 idx++;
4973 access_mask = 0;
4974 if (mode & S_IROTH) {
4975 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
4977 if (mode & S_IWOTH) {
4978 access_mask |= SEC_RIGHTS_FILE_WRITE;
4980 if (access_mask) {
4981 init_sec_ace(&aces[idx],
4982 &global_sid_World,
4983 SEC_ACE_TYPE_ACCESS_ALLOWED,
4984 access_mask,
4986 idx++;
4989 init_sec_ace(&aces[idx],
4990 &global_sid_System,
4991 SEC_ACE_TYPE_ACCESS_ALLOWED,
4992 SEC_RIGHTS_FILE_ALL,
4994 idx++;
4996 new_dacl = make_sec_acl(ctx,
4997 NT4_ACL_REVISION,
4998 idx,
4999 aces);
5001 if (!new_dacl) {
5002 return NT_STATUS_NO_MEMORY;
5005 *ppdesc = make_sec_desc(ctx,
5006 SECURITY_DESCRIPTOR_REVISION_1,
5007 SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
5008 &owner_sid,
5009 &group_sid,
5010 NULL,
5011 new_dacl,
5012 &size);
5013 if (!*ppdesc) {
5014 return NT_STATUS_NO_MEMORY;
5016 return NT_STATUS_OK;