Revert "Another fix needed for bug #9236 - ACL masks incorrectly applied when setting...
[Samba.git] / source3 / smbd / posix_acls.c
blob02a680ee0751a11d3495fcd1d7728d2bbf4b7fad
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 struct canon_ace {
45 struct canon_ace *next, *prev;
46 SMB_ACL_TAG_T type;
47 mode_t perms; /* Only use S_I(R|W|X)USR mode bits here. */
48 struct dom_sid trustee;
49 enum ace_owner owner_type;
50 enum ace_attribute attr;
51 struct unixid unix_ug;
52 uint8_t ace_flags; /* From windows ACE entry. */
53 } canon_ace;
55 #define ALL_ACE_PERMS (S_IRUSR|S_IWUSR|S_IXUSR)
58 * EA format of user.SAMBA_PAI (Samba_Posix_Acl_Interitance)
59 * attribute on disk - version 1.
60 * All values are little endian.
62 * | 1 | 1 | 2 | 2 | ....
63 * +------+------+-------------+---------------------+-------------+--------------------+
64 * | vers | flag | num_entries | num_default_entries | ..entries.. | default_entries... |
65 * +------+------+-------------+---------------------+-------------+--------------------+
67 * Entry format is :
69 * | 1 | 4 |
70 * +------+-------------------+
71 * | value| uid/gid or world |
72 * | type | value |
73 * +------+-------------------+
75 * Version 2 format. Stores extra Windows metadata about an ACL.
77 * | 1 | 2 | 2 | 2 | ....
78 * +------+----------+-------------+---------------------+-------------+--------------------+
79 * | vers | ace | num_entries | num_default_entries | ..entries.. | default_entries... |
80 * | 2 | type | | | | |
81 * +------+----------+-------------+---------------------+-------------+--------------------+
83 * Entry format is :
85 * | 1 | 1 | 4 |
86 * +------+------+-------------------+
87 * | ace | value| uid/gid or world |
88 * | flag | type | value |
89 * +------+-------------------+------+
93 #define PAI_VERSION_OFFSET 0
95 #define PAI_V1_FLAG_OFFSET 1
96 #define PAI_V1_NUM_ENTRIES_OFFSET 2
97 #define PAI_V1_NUM_DEFAULT_ENTRIES_OFFSET 4
98 #define PAI_V1_ENTRIES_BASE 6
99 #define PAI_V1_ACL_FLAG_PROTECTED 0x1
100 #define PAI_V1_ENTRY_LENGTH 5
102 #define PAI_V1_VERSION 1
104 #define PAI_V2_TYPE_OFFSET 1
105 #define PAI_V2_NUM_ENTRIES_OFFSET 3
106 #define PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET 5
107 #define PAI_V2_ENTRIES_BASE 7
108 #define PAI_V2_ENTRY_LENGTH 6
110 #define PAI_V2_VERSION 2
113 * In memory format of user.SAMBA_PAI attribute.
116 struct pai_entry {
117 struct pai_entry *next, *prev;
118 uint8_t ace_flags;
119 enum ace_owner owner_type;
120 struct unixid unix_ug;
123 struct pai_val {
124 uint16_t sd_type;
125 unsigned int num_entries;
126 struct pai_entry *entry_list;
127 unsigned int num_def_entries;
128 struct pai_entry *def_entry_list;
131 /************************************************************************
132 Return a uint32 of the pai_entry principal.
133 ************************************************************************/
135 static uint32_t get_pai_entry_val(struct pai_entry *paie)
137 switch (paie->owner_type) {
138 case UID_ACE:
139 DEBUG(10,("get_pai_entry_val: uid = %u\n", (unsigned int)paie->unix_ug.id ));
140 return (uint32_t)paie->unix_ug.id;
141 case GID_ACE:
142 DEBUG(10,("get_pai_entry_val: gid = %u\n", (unsigned int)paie->unix_ug.id ));
143 return (uint32_t)paie->unix_ug.id;
144 case WORLD_ACE:
145 default:
146 DEBUG(10,("get_pai_entry_val: world ace\n"));
147 return (uint32_t)-1;
151 /************************************************************************
152 Return a uint32 of the entry principal.
153 ************************************************************************/
155 static uint32_t get_entry_val(canon_ace *ace_entry)
157 switch (ace_entry->owner_type) {
158 case UID_ACE:
159 DEBUG(10,("get_entry_val: uid = %u\n", (unsigned int)ace_entry->unix_ug.id ));
160 return (uint32_t)ace_entry->unix_ug.id;
161 case GID_ACE:
162 DEBUG(10,("get_entry_val: gid = %u\n", (unsigned int)ace_entry->unix_ug.id ));
163 return (uint32_t)ace_entry->unix_ug.id;
164 case WORLD_ACE:
165 default:
166 DEBUG(10,("get_entry_val: world ace\n"));
167 return (uint32_t)-1;
171 /************************************************************************
172 Create the on-disk format (always v2 now). Caller must free.
173 ************************************************************************/
175 static char *create_pai_buf_v2(canon_ace *file_ace_list,
176 canon_ace *dir_ace_list,
177 uint16_t sd_type,
178 size_t *store_size)
180 char *pai_buf = NULL;
181 canon_ace *ace_list = NULL;
182 char *entry_offset = NULL;
183 unsigned int num_entries = 0;
184 unsigned int num_def_entries = 0;
185 unsigned int i;
187 for (ace_list = file_ace_list; ace_list; ace_list = ace_list->next) {
188 num_entries++;
191 for (ace_list = dir_ace_list; ace_list; ace_list = ace_list->next) {
192 num_def_entries++;
195 DEBUG(10,("create_pai_buf_v2: num_entries = %u, num_def_entries = %u\n", num_entries, num_def_entries ));
197 *store_size = PAI_V2_ENTRIES_BASE +
198 ((num_entries + num_def_entries)*PAI_V2_ENTRY_LENGTH);
200 pai_buf = talloc_array(talloc_tos(), char, *store_size);
201 if (!pai_buf) {
202 return NULL;
205 /* Set up the header. */
206 memset(pai_buf, '\0', PAI_V2_ENTRIES_BASE);
207 SCVAL(pai_buf,PAI_VERSION_OFFSET,PAI_V2_VERSION);
208 SSVAL(pai_buf,PAI_V2_TYPE_OFFSET, sd_type);
209 SSVAL(pai_buf,PAI_V2_NUM_ENTRIES_OFFSET,num_entries);
210 SSVAL(pai_buf,PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET,num_def_entries);
212 DEBUG(10,("create_pai_buf_v2: sd_type = 0x%x\n",
213 (unsigned int)sd_type ));
215 entry_offset = pai_buf + PAI_V2_ENTRIES_BASE;
217 i = 0;
218 for (ace_list = file_ace_list; ace_list; ace_list = ace_list->next) {
219 uint8_t type_val = (uint8_t)ace_list->owner_type;
220 uint32_t entry_val = get_entry_val(ace_list);
222 SCVAL(entry_offset,0,ace_list->ace_flags);
223 SCVAL(entry_offset,1,type_val);
224 SIVAL(entry_offset,2,entry_val);
225 DEBUG(10,("create_pai_buf_v2: entry %u [0x%x] [0x%x] [0x%x]\n",
227 (unsigned int)ace_list->ace_flags,
228 (unsigned int)type_val,
229 (unsigned int)entry_val ));
230 i++;
231 entry_offset += PAI_V2_ENTRY_LENGTH;
234 for (ace_list = dir_ace_list; ace_list; ace_list = ace_list->next) {
235 uint8_t type_val = (uint8_t)ace_list->owner_type;
236 uint32_t entry_val = get_entry_val(ace_list);
238 SCVAL(entry_offset,0,ace_list->ace_flags);
239 SCVAL(entry_offset,1,type_val);
240 SIVAL(entry_offset,2,entry_val);
241 DEBUG(10,("create_pai_buf_v2: entry %u [0x%x] [0x%x] [0x%x]\n",
243 (unsigned int)ace_list->ace_flags,
244 (unsigned int)type_val,
245 (unsigned int)entry_val ));
246 i++;
247 entry_offset += PAI_V2_ENTRY_LENGTH;
250 return pai_buf;
253 /************************************************************************
254 Store the user.SAMBA_PAI attribute on disk.
255 ************************************************************************/
257 static void store_inheritance_attributes(files_struct *fsp,
258 canon_ace *file_ace_list,
259 canon_ace *dir_ace_list,
260 uint16_t sd_type)
262 int ret;
263 size_t store_size;
264 char *pai_buf;
266 if (!lp_map_acl_inherit(SNUM(fsp->conn))) {
267 return;
270 pai_buf = create_pai_buf_v2(file_ace_list, dir_ace_list,
271 sd_type, &store_size);
273 if (fsp->fh->fd != -1) {
274 ret = SMB_VFS_FSETXATTR(fsp, SAMBA_POSIX_INHERITANCE_EA_NAME,
275 pai_buf, store_size, 0);
276 } else {
277 ret = SMB_VFS_SETXATTR(fsp->conn, fsp->fsp_name->base_name,
278 SAMBA_POSIX_INHERITANCE_EA_NAME,
279 pai_buf, store_size, 0);
282 TALLOC_FREE(pai_buf);
284 DEBUG(10,("store_inheritance_attribute: type 0x%x for file %s\n",
285 (unsigned int)sd_type,
286 fsp_str_dbg(fsp)));
288 if (ret == -1 && !no_acl_syscall_error(errno)) {
289 DEBUG(1,("store_inheritance_attribute: Error %s\n", strerror(errno) ));
293 /************************************************************************
294 Delete the in memory inheritance info.
295 ************************************************************************/
297 static void free_inherited_info(struct pai_val *pal)
299 if (pal) {
300 struct pai_entry *paie, *paie_next;
301 for (paie = pal->entry_list; paie; paie = paie_next) {
302 paie_next = paie->next;
303 TALLOC_FREE(paie);
305 for (paie = pal->def_entry_list; paie; paie = paie_next) {
306 paie_next = paie->next;
307 TALLOC_FREE(paie);
309 TALLOC_FREE(pal);
313 /************************************************************************
314 Get any stored ACE flags.
315 ************************************************************************/
317 static uint16_t get_pai_flags(struct pai_val *pal, canon_ace *ace_entry, bool default_ace)
319 struct pai_entry *paie;
321 if (!pal) {
322 return 0;
325 /* If the entry exists it is inherited. */
326 for (paie = (default_ace ? pal->def_entry_list : pal->entry_list); paie; paie = paie->next) {
327 if (ace_entry->owner_type == paie->owner_type &&
328 get_entry_val(ace_entry) == get_pai_entry_val(paie))
329 return paie->ace_flags;
331 return 0;
334 /************************************************************************
335 Ensure an attribute just read is valid - v1.
336 ************************************************************************/
338 static bool check_pai_ok_v1(const char *pai_buf, size_t pai_buf_data_size)
340 uint16 num_entries;
341 uint16 num_def_entries;
343 if (pai_buf_data_size < PAI_V1_ENTRIES_BASE) {
344 /* Corrupted - too small. */
345 return false;
348 if (CVAL(pai_buf,PAI_VERSION_OFFSET) != PAI_V1_VERSION) {
349 return false;
352 num_entries = SVAL(pai_buf,PAI_V1_NUM_ENTRIES_OFFSET);
353 num_def_entries = SVAL(pai_buf,PAI_V1_NUM_DEFAULT_ENTRIES_OFFSET);
355 /* Check the entry lists match. */
356 /* Each entry is 5 bytes (type plus 4 bytes of uid or gid). */
358 if (((num_entries + num_def_entries)*PAI_V1_ENTRY_LENGTH) +
359 PAI_V1_ENTRIES_BASE != pai_buf_data_size) {
360 return false;
363 return true;
366 /************************************************************************
367 Ensure an attribute just read is valid - v2.
368 ************************************************************************/
370 static bool check_pai_ok_v2(const char *pai_buf, size_t pai_buf_data_size)
372 uint16 num_entries;
373 uint16 num_def_entries;
375 if (pai_buf_data_size < PAI_V2_ENTRIES_BASE) {
376 /* Corrupted - too small. */
377 return false;
380 if (CVAL(pai_buf,PAI_VERSION_OFFSET) != PAI_V2_VERSION) {
381 return false;
384 num_entries = SVAL(pai_buf,PAI_V2_NUM_ENTRIES_OFFSET);
385 num_def_entries = SVAL(pai_buf,PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET);
387 /* Check the entry lists match. */
388 /* Each entry is 6 bytes (flags + type + 4 bytes of uid or gid). */
390 if (((num_entries + num_def_entries)*PAI_V2_ENTRY_LENGTH) +
391 PAI_V2_ENTRIES_BASE != pai_buf_data_size) {
392 return false;
395 return true;
398 /************************************************************************
399 Decode the owner.
400 ************************************************************************/
402 static bool get_pai_owner_type(struct pai_entry *paie, const char *entry_offset)
404 paie->owner_type = (enum ace_owner)CVAL(entry_offset,0);
405 switch( paie->owner_type) {
406 case UID_ACE:
407 paie->unix_ug.type = ID_TYPE_UID;
408 paie->unix_ug.id = (uid_t)IVAL(entry_offset,1);
409 DEBUG(10,("get_pai_owner_type: uid = %u\n",
410 (unsigned int)paie->unix_ug.id ));
411 break;
412 case GID_ACE:
413 paie->unix_ug.type = ID_TYPE_GID;
414 paie->unix_ug.id = (gid_t)IVAL(entry_offset,1);
415 DEBUG(10,("get_pai_owner_type: gid = %u\n",
416 (unsigned int)paie->unix_ug.id ));
417 break;
418 case WORLD_ACE:
419 paie->unix_ug.type = ID_TYPE_NOT_SPECIFIED;
420 paie->unix_ug.id = -1;
421 DEBUG(10,("get_pai_owner_type: world ace\n"));
422 break;
423 default:
424 DEBUG(10,("get_pai_owner_type: unknown type %u\n",
425 (unsigned int)paie->owner_type ));
426 return false;
428 return true;
431 /************************************************************************
432 Process v2 entries.
433 ************************************************************************/
435 static const char *create_pai_v1_entries(struct pai_val *paiv,
436 const char *entry_offset,
437 bool def_entry)
439 int i;
441 for (i = 0; i < paiv->num_entries; i++) {
442 struct pai_entry *paie = talloc(talloc_tos(), struct pai_entry);
443 if (!paie) {
444 return NULL;
447 paie->ace_flags = SEC_ACE_FLAG_INHERITED_ACE;
448 if (!get_pai_owner_type(paie, entry_offset)) {
449 TALLOC_FREE(paie);
450 return NULL;
453 if (!def_entry) {
454 DLIST_ADD(paiv->entry_list, paie);
455 } else {
456 DLIST_ADD(paiv->def_entry_list, paie);
458 entry_offset += PAI_V1_ENTRY_LENGTH;
460 return entry_offset;
463 /************************************************************************
464 Convert to in-memory format from version 1.
465 ************************************************************************/
467 static struct pai_val *create_pai_val_v1(const char *buf, size_t size)
469 const char *entry_offset;
470 struct pai_val *paiv = NULL;
472 if (!check_pai_ok_v1(buf, size)) {
473 return NULL;
476 paiv = talloc(talloc_tos(), struct pai_val);
477 if (!paiv) {
478 return NULL;
481 memset(paiv, '\0', sizeof(struct pai_val));
483 paiv->sd_type = (CVAL(buf,PAI_V1_FLAG_OFFSET) == PAI_V1_ACL_FLAG_PROTECTED) ?
484 SEC_DESC_DACL_PROTECTED : 0;
486 paiv->num_entries = SVAL(buf,PAI_V1_NUM_ENTRIES_OFFSET);
487 paiv->num_def_entries = SVAL(buf,PAI_V1_NUM_DEFAULT_ENTRIES_OFFSET);
489 entry_offset = buf + PAI_V1_ENTRIES_BASE;
491 DEBUG(10,("create_pai_val: num_entries = %u, num_def_entries = %u\n",
492 paiv->num_entries, paiv->num_def_entries ));
494 entry_offset = create_pai_v1_entries(paiv, entry_offset, false);
495 if (entry_offset == NULL) {
496 free_inherited_info(paiv);
497 return NULL;
499 entry_offset = create_pai_v1_entries(paiv, entry_offset, true);
500 if (entry_offset == NULL) {
501 free_inherited_info(paiv);
502 return NULL;
505 return paiv;
508 /************************************************************************
509 Process v2 entries.
510 ************************************************************************/
512 static const char *create_pai_v2_entries(struct pai_val *paiv,
513 unsigned int num_entries,
514 const char *entry_offset,
515 bool def_entry)
517 unsigned int i;
519 for (i = 0; i < num_entries; i++) {
520 struct pai_entry *paie = talloc(talloc_tos(), struct pai_entry);
521 if (!paie) {
522 return NULL;
525 paie->ace_flags = CVAL(entry_offset,0);
527 if (!get_pai_owner_type(paie, entry_offset+1)) {
528 TALLOC_FREE(paie);
529 return NULL;
531 if (!def_entry) {
532 DLIST_ADD(paiv->entry_list, paie);
533 } else {
534 DLIST_ADD(paiv->def_entry_list, paie);
536 entry_offset += PAI_V2_ENTRY_LENGTH;
538 return entry_offset;
541 /************************************************************************
542 Convert to in-memory format from version 2.
543 ************************************************************************/
545 static struct pai_val *create_pai_val_v2(const char *buf, size_t size)
547 const char *entry_offset;
548 struct pai_val *paiv = NULL;
550 if (!check_pai_ok_v2(buf, size)) {
551 return NULL;
554 paiv = talloc(talloc_tos(), struct pai_val);
555 if (!paiv) {
556 return NULL;
559 memset(paiv, '\0', sizeof(struct pai_val));
561 paiv->sd_type = SVAL(buf,PAI_V2_TYPE_OFFSET);
563 paiv->num_entries = SVAL(buf,PAI_V2_NUM_ENTRIES_OFFSET);
564 paiv->num_def_entries = SVAL(buf,PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET);
566 entry_offset = buf + PAI_V2_ENTRIES_BASE;
568 DEBUG(10,("create_pai_val_v2: sd_type = 0x%x num_entries = %u, num_def_entries = %u\n",
569 (unsigned int)paiv->sd_type,
570 paiv->num_entries, paiv->num_def_entries ));
572 entry_offset = create_pai_v2_entries(paiv, paiv->num_entries,
573 entry_offset, false);
574 if (entry_offset == NULL) {
575 free_inherited_info(paiv);
576 return NULL;
578 entry_offset = create_pai_v2_entries(paiv, paiv->num_def_entries,
579 entry_offset, true);
580 if (entry_offset == NULL) {
581 free_inherited_info(paiv);
582 return NULL;
585 return paiv;
588 /************************************************************************
589 Convert to in-memory format - from either version 1 or 2.
590 ************************************************************************/
592 static struct pai_val *create_pai_val(const char *buf, size_t size)
594 if (size < 1) {
595 return NULL;
597 if (CVAL(buf,PAI_VERSION_OFFSET) == PAI_V1_VERSION) {
598 return create_pai_val_v1(buf, size);
599 } else if (CVAL(buf,PAI_VERSION_OFFSET) == PAI_V2_VERSION) {
600 return create_pai_val_v2(buf, size);
601 } else {
602 return NULL;
606 /************************************************************************
607 Load the user.SAMBA_PAI attribute.
608 ************************************************************************/
610 static struct pai_val *fload_inherited_info(files_struct *fsp)
612 char *pai_buf;
613 size_t pai_buf_size = 1024;
614 struct pai_val *paiv = NULL;
615 ssize_t ret;
617 if (!lp_map_acl_inherit(SNUM(fsp->conn))) {
618 return NULL;
621 if ((pai_buf = talloc_array(talloc_tos(), char, pai_buf_size)) == NULL) {
622 return NULL;
625 do {
626 if (fsp->fh->fd != -1) {
627 ret = SMB_VFS_FGETXATTR(fsp, SAMBA_POSIX_INHERITANCE_EA_NAME,
628 pai_buf, pai_buf_size);
629 } else {
630 ret = SMB_VFS_GETXATTR(fsp->conn,
631 fsp->fsp_name->base_name,
632 SAMBA_POSIX_INHERITANCE_EA_NAME,
633 pai_buf, pai_buf_size);
636 if (ret == -1) {
637 if (errno != ERANGE) {
638 break;
640 /* Buffer too small - enlarge it. */
641 pai_buf_size *= 2;
642 TALLOC_FREE(pai_buf);
643 if (pai_buf_size > 1024*1024) {
644 return NULL; /* Limit malloc to 1mb. */
646 if ((pai_buf = talloc_array(talloc_tos(), char, pai_buf_size)) == NULL)
647 return NULL;
649 } while (ret == -1);
651 DEBUG(10,("load_inherited_info: ret = %lu for file %s\n",
652 (unsigned long)ret, fsp_str_dbg(fsp)));
654 if (ret == -1) {
655 /* No attribute or not supported. */
656 #if defined(ENOATTR)
657 if (errno != ENOATTR)
658 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno) ));
659 #else
660 if (errno != ENOSYS)
661 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno) ));
662 #endif
663 TALLOC_FREE(pai_buf);
664 return NULL;
667 paiv = create_pai_val(pai_buf, ret);
669 if (paiv) {
670 DEBUG(10,("load_inherited_info: ACL type is 0x%x for file %s\n",
671 (unsigned int)paiv->sd_type, fsp_str_dbg(fsp)));
674 TALLOC_FREE(pai_buf);
675 return paiv;
678 /************************************************************************
679 Load the user.SAMBA_PAI attribute.
680 ************************************************************************/
682 static struct pai_val *load_inherited_info(const struct connection_struct *conn,
683 const char *fname)
685 char *pai_buf;
686 size_t pai_buf_size = 1024;
687 struct pai_val *paiv = NULL;
688 ssize_t ret;
690 if (!lp_map_acl_inherit(SNUM(conn))) {
691 return NULL;
694 if ((pai_buf = talloc_array(talloc_tos(), char, pai_buf_size)) == NULL) {
695 return NULL;
698 do {
699 ret = SMB_VFS_GETXATTR(conn, fname,
700 SAMBA_POSIX_INHERITANCE_EA_NAME,
701 pai_buf, pai_buf_size);
703 if (ret == -1) {
704 if (errno != ERANGE) {
705 break;
707 /* Buffer too small - enlarge it. */
708 pai_buf_size *= 2;
709 TALLOC_FREE(pai_buf);
710 if (pai_buf_size > 1024*1024) {
711 return NULL; /* Limit malloc to 1mb. */
713 if ((pai_buf = talloc_array(talloc_tos(), char, pai_buf_size)) == NULL)
714 return NULL;
716 } while (ret == -1);
718 DEBUG(10,("load_inherited_info: ret = %lu for file %s\n", (unsigned long)ret, fname));
720 if (ret == -1) {
721 /* No attribute or not supported. */
722 #if defined(ENOATTR)
723 if (errno != ENOATTR)
724 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno) ));
725 #else
726 if (errno != ENOSYS)
727 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno) ));
728 #endif
729 TALLOC_FREE(pai_buf);
730 return NULL;
733 paiv = create_pai_val(pai_buf, ret);
735 if (paiv) {
736 DEBUG(10,("load_inherited_info: ACL type 0x%x for file %s\n",
737 (unsigned int)paiv->sd_type,
738 fname));
741 TALLOC_FREE(pai_buf);
742 return paiv;
745 /****************************************************************************
746 Functions to manipulate the internal ACE format.
747 ****************************************************************************/
749 /****************************************************************************
750 Count a linked list of canonical ACE entries.
751 ****************************************************************************/
753 static size_t count_canon_ace_list( canon_ace *l_head )
755 size_t count = 0;
756 canon_ace *ace;
758 for (ace = l_head; ace; ace = ace->next)
759 count++;
761 return count;
764 /****************************************************************************
765 Free a linked list of canonical ACE entries.
766 ****************************************************************************/
768 static void free_canon_ace_list( canon_ace *l_head )
770 canon_ace *list, *next;
772 for (list = l_head; list; list = next) {
773 next = list->next;
774 DLIST_REMOVE(l_head, list);
775 TALLOC_FREE(list);
779 /****************************************************************************
780 Function to duplicate a canon_ace entry.
781 ****************************************************************************/
783 static canon_ace *dup_canon_ace( canon_ace *src_ace)
785 canon_ace *dst_ace = talloc(talloc_tos(), canon_ace);
787 if (dst_ace == NULL)
788 return NULL;
790 *dst_ace = *src_ace;
791 dst_ace->prev = dst_ace->next = NULL;
792 return dst_ace;
795 /****************************************************************************
796 Print out a canon ace.
797 ****************************************************************************/
799 static void print_canon_ace(canon_ace *pace, int num)
801 dbgtext( "canon_ace index %d. Type = %s ", num, pace->attr == ALLOW_ACE ? "allow" : "deny" );
802 dbgtext( "SID = %s ", sid_string_dbg(&pace->trustee));
803 if (pace->owner_type == UID_ACE) {
804 const char *u_name = uidtoname(pace->unix_ug.id);
805 dbgtext( "uid %u (%s) ", (unsigned int)pace->unix_ug.id, u_name );
806 } else if (pace->owner_type == GID_ACE) {
807 char *g_name = gidtoname(pace->unix_ug.id);
808 dbgtext( "gid %u (%s) ", (unsigned int)pace->unix_ug.id, g_name );
809 } else
810 dbgtext( "other ");
811 switch (pace->type) {
812 case SMB_ACL_USER:
813 dbgtext( "SMB_ACL_USER ");
814 break;
815 case SMB_ACL_USER_OBJ:
816 dbgtext( "SMB_ACL_USER_OBJ ");
817 break;
818 case SMB_ACL_GROUP:
819 dbgtext( "SMB_ACL_GROUP ");
820 break;
821 case SMB_ACL_GROUP_OBJ:
822 dbgtext( "SMB_ACL_GROUP_OBJ ");
823 break;
824 case SMB_ACL_OTHER:
825 dbgtext( "SMB_ACL_OTHER ");
826 break;
827 default:
828 dbgtext( "MASK " );
829 break;
832 dbgtext( "ace_flags = 0x%x ", (unsigned int)pace->ace_flags);
833 dbgtext( "perms ");
834 dbgtext( "%c", pace->perms & S_IRUSR ? 'r' : '-');
835 dbgtext( "%c", pace->perms & S_IWUSR ? 'w' : '-');
836 dbgtext( "%c\n", pace->perms & S_IXUSR ? 'x' : '-');
839 /****************************************************************************
840 Print out a canon ace list.
841 ****************************************************************************/
843 static void print_canon_ace_list(const char *name, canon_ace *ace_list)
845 int count = 0;
847 if( DEBUGLVL( 10 )) {
848 dbgtext( "print_canon_ace_list: %s\n", name );
849 for (;ace_list; ace_list = ace_list->next, count++)
850 print_canon_ace(ace_list, count );
854 /****************************************************************************
855 Map POSIX ACL perms to canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits).
856 ****************************************************************************/
858 static mode_t convert_permset_to_mode_t(SMB_ACL_PERMSET_T permset)
860 mode_t ret = 0;
862 ret |= (sys_acl_get_perm(permset, SMB_ACL_READ) ? S_IRUSR : 0);
863 ret |= (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? S_IWUSR : 0);
864 ret |= (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? S_IXUSR : 0);
866 return ret;
869 /****************************************************************************
870 Map generic UNIX permissions to canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits).
871 ****************************************************************************/
873 static mode_t unix_perms_to_acl_perms(mode_t mode, int r_mask, int w_mask, int x_mask)
875 mode_t ret = 0;
877 if (mode & r_mask)
878 ret |= S_IRUSR;
879 if (mode & w_mask)
880 ret |= S_IWUSR;
881 if (mode & x_mask)
882 ret |= S_IXUSR;
884 return ret;
887 /****************************************************************************
888 Map canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits) to
889 an SMB_ACL_PERMSET_T.
890 ****************************************************************************/
892 static int map_acl_perms_to_permset(connection_struct *conn, mode_t mode, SMB_ACL_PERMSET_T *p_permset)
894 if (sys_acl_clear_perms(*p_permset) == -1)
895 return -1;
896 if (mode & S_IRUSR) {
897 if (sys_acl_add_perm(*p_permset, SMB_ACL_READ) == -1)
898 return -1;
900 if (mode & S_IWUSR) {
901 if (sys_acl_add_perm(*p_permset, SMB_ACL_WRITE) == -1)
902 return -1;
904 if (mode & S_IXUSR) {
905 if (sys_acl_add_perm(*p_permset, SMB_ACL_EXECUTE) == -1)
906 return -1;
908 return 0;
911 /****************************************************************************
912 Function to create owner and group SIDs from a SMB_STRUCT_STAT.
913 ****************************************************************************/
915 void create_file_sids(const SMB_STRUCT_STAT *psbuf, struct dom_sid *powner_sid, struct dom_sid *pgroup_sid)
917 uid_to_sid( powner_sid, psbuf->st_ex_uid );
918 gid_to_sid( pgroup_sid, psbuf->st_ex_gid );
921 /****************************************************************************
922 Merge aces with a common UID or GID - if both are allow or deny, OR the permissions together and
923 delete the second one. If the first is deny, mask the permissions off and delete the allow
924 if the permissions become zero, delete the deny if the permissions are non zero.
925 ****************************************************************************/
927 static void merge_aces( canon_ace **pp_list_head, bool dir_acl)
929 canon_ace *l_head = *pp_list_head;
930 canon_ace *curr_ace_outer;
931 canon_ace *curr_ace_outer_next;
934 * First, merge allow entries with identical SIDs, and deny entries
935 * with identical SIDs.
938 for (curr_ace_outer = l_head; curr_ace_outer; curr_ace_outer = curr_ace_outer_next) {
939 canon_ace *curr_ace;
940 canon_ace *curr_ace_next;
942 curr_ace_outer_next = curr_ace_outer->next; /* Save the link in case we delete. */
944 for (curr_ace = curr_ace_outer->next; curr_ace; curr_ace = curr_ace_next) {
945 bool can_merge = false;
947 curr_ace_next = curr_ace->next; /* Save the link in case of delete. */
949 /* For file ACLs we can merge if the SIDs and ALLOW/DENY
950 * types are the same. For directory acls we must also
951 * ensure the POSIX ACL types are the same.
953 * For the IDMAP_BOTH case, we must not merge
954 * the UID and GID ACE values for same SID
957 if (!dir_acl) {
958 can_merge = (curr_ace->unix_ug.id == curr_ace_outer->unix_ug.id &&
959 curr_ace->owner_type == curr_ace_outer->owner_type &&
960 (curr_ace->attr == curr_ace_outer->attr));
961 } else {
962 can_merge = (curr_ace->unix_ug.id == curr_ace_outer->unix_ug.id &&
963 curr_ace->owner_type == curr_ace_outer->owner_type &&
964 (curr_ace->type == curr_ace_outer->type) &&
965 (curr_ace->attr == curr_ace_outer->attr));
968 if (can_merge) {
969 if( DEBUGLVL( 10 )) {
970 dbgtext("merge_aces: Merging ACE's\n");
971 print_canon_ace( curr_ace_outer, 0);
972 print_canon_ace( curr_ace, 0);
975 /* Merge two allow or two deny ACE's. */
977 /* Theoretically we shouldn't merge a dir ACE if
978 * one ACE has the CI flag set, and the other
979 * ACE has the OI flag set, but this is rare
980 * enough we can ignore it. */
982 curr_ace_outer->perms |= curr_ace->perms;
983 curr_ace_outer->ace_flags |= curr_ace->ace_flags;
984 DLIST_REMOVE(l_head, curr_ace);
985 TALLOC_FREE(curr_ace);
986 curr_ace_outer_next = curr_ace_outer->next; /* We may have deleted the link. */
992 * Now go through and mask off allow permissions with deny permissions.
993 * We can delete either the allow or deny here as we know that each SID
994 * appears only once in the list.
997 for (curr_ace_outer = l_head; curr_ace_outer; curr_ace_outer = curr_ace_outer_next) {
998 canon_ace *curr_ace;
999 canon_ace *curr_ace_next;
1001 curr_ace_outer_next = curr_ace_outer->next; /* Save the link in case we delete. */
1003 for (curr_ace = curr_ace_outer->next; curr_ace; curr_ace = curr_ace_next) {
1005 curr_ace_next = curr_ace->next; /* Save the link in case of delete. */
1008 * Subtract ACE's with different entries. Due to the ordering constraints
1009 * we've put on the ACL, we know the deny must be the first one.
1012 if (curr_ace->unix_ug.id == curr_ace_outer->unix_ug.id &&
1013 (curr_ace->owner_type == curr_ace_outer->owner_type) &&
1014 (curr_ace_outer->attr == DENY_ACE) && (curr_ace->attr == ALLOW_ACE)) {
1016 if( DEBUGLVL( 10 )) {
1017 dbgtext("merge_aces: Masking ACE's\n");
1018 print_canon_ace( curr_ace_outer, 0);
1019 print_canon_ace( curr_ace, 0);
1022 curr_ace->perms &= ~curr_ace_outer->perms;
1024 if (curr_ace->perms == 0) {
1027 * The deny overrides the allow. Remove the allow.
1030 DLIST_REMOVE(l_head, curr_ace);
1031 TALLOC_FREE(curr_ace);
1032 curr_ace_outer_next = curr_ace_outer->next; /* We may have deleted the link. */
1034 } else {
1037 * Even after removing permissions, there
1038 * are still allow permissions - delete the deny.
1039 * It is safe to delete the deny here,
1040 * as we are guarenteed by the deny first
1041 * ordering that all the deny entries for
1042 * this SID have already been merged into one
1043 * before we can get to an allow ace.
1046 DLIST_REMOVE(l_head, curr_ace_outer);
1047 TALLOC_FREE(curr_ace_outer);
1048 break;
1052 } /* end for curr_ace */
1053 } /* end for curr_ace_outer */
1055 /* We may have modified the list. */
1057 *pp_list_head = l_head;
1060 /****************************************************************************
1061 Map canon_ace perms to permission bits NT.
1062 The attr element is not used here - we only process deny entries on set,
1063 not get. Deny entries are implicit on get with ace->perms = 0.
1064 ****************************************************************************/
1066 uint32_t map_canon_ace_perms(int snum,
1067 enum security_ace_type *pacl_type,
1068 mode_t perms,
1069 bool directory_ace)
1071 uint32_t nt_mask = 0;
1073 *pacl_type = SEC_ACE_TYPE_ACCESS_ALLOWED;
1075 if (lp_acl_map_full_control(snum) && ((perms & ALL_ACE_PERMS) == ALL_ACE_PERMS)) {
1076 if (directory_ace) {
1077 nt_mask = UNIX_DIRECTORY_ACCESS_RWX;
1078 } else {
1079 nt_mask = (UNIX_ACCESS_RWX & ~DELETE_ACCESS);
1081 } else if ((perms & ALL_ACE_PERMS) == (mode_t)0) {
1083 * Windows NT refuses to display ACEs with no permissions in them (but
1084 * they are perfectly legal with Windows 2000). If the ACE has empty
1085 * permissions we cannot use 0, so we use the otherwise unused
1086 * WRITE_OWNER permission, which we ignore when we set an ACL.
1087 * We abstract this into a #define of UNIX_ACCESS_NONE to allow this
1088 * to be changed in the future.
1091 nt_mask = 0;
1092 } else {
1093 if (directory_ace) {
1094 nt_mask |= ((perms & S_IRUSR) ? UNIX_DIRECTORY_ACCESS_R : 0 );
1095 nt_mask |= ((perms & S_IWUSR) ? UNIX_DIRECTORY_ACCESS_W : 0 );
1096 nt_mask |= ((perms & S_IXUSR) ? UNIX_DIRECTORY_ACCESS_X : 0 );
1097 } else {
1098 nt_mask |= ((perms & S_IRUSR) ? UNIX_ACCESS_R : 0 );
1099 nt_mask |= ((perms & S_IWUSR) ? UNIX_ACCESS_W : 0 );
1100 nt_mask |= ((perms & S_IXUSR) ? UNIX_ACCESS_X : 0 );
1104 if ((perms & S_IWUSR) && lp_dos_filemode(snum)) {
1105 nt_mask |= (SEC_STD_WRITE_DAC|SEC_STD_WRITE_OWNER|DELETE_ACCESS);
1108 DEBUG(10,("map_canon_ace_perms: Mapped (UNIX) %x to (NT) %x\n",
1109 (unsigned int)perms, (unsigned int)nt_mask ));
1111 return nt_mask;
1114 /****************************************************************************
1115 Map NT perms to a UNIX mode_t.
1116 ****************************************************************************/
1118 #define FILE_SPECIFIC_READ_BITS (FILE_READ_DATA|FILE_READ_EA)
1119 #define FILE_SPECIFIC_WRITE_BITS (FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_WRITE_EA)
1120 #define FILE_SPECIFIC_EXECUTE_BITS (FILE_EXECUTE)
1122 static mode_t map_nt_perms( uint32 *mask, int type)
1124 mode_t mode = 0;
1126 switch(type) {
1127 case S_IRUSR:
1128 if((*mask) & GENERIC_ALL_ACCESS)
1129 mode = S_IRUSR|S_IWUSR|S_IXUSR;
1130 else {
1131 mode |= ((*mask) & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IRUSR : 0;
1132 mode |= ((*mask) & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWUSR : 0;
1133 mode |= ((*mask) & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXUSR : 0;
1135 break;
1136 case S_IRGRP:
1137 if((*mask) & GENERIC_ALL_ACCESS)
1138 mode = S_IRGRP|S_IWGRP|S_IXGRP;
1139 else {
1140 mode |= ((*mask) & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IRGRP : 0;
1141 mode |= ((*mask) & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWGRP : 0;
1142 mode |= ((*mask) & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXGRP : 0;
1144 break;
1145 case S_IROTH:
1146 if((*mask) & GENERIC_ALL_ACCESS)
1147 mode = S_IROTH|S_IWOTH|S_IXOTH;
1148 else {
1149 mode |= ((*mask) & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IROTH : 0;
1150 mode |= ((*mask) & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWOTH : 0;
1151 mode |= ((*mask) & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXOTH : 0;
1153 break;
1156 return mode;
1159 /****************************************************************************
1160 Unpack a struct security_descriptor into a UNIX owner and group.
1161 ****************************************************************************/
1163 NTSTATUS unpack_nt_owners(struct connection_struct *conn,
1164 uid_t *puser, gid_t *pgrp,
1165 uint32 security_info_sent, const struct
1166 security_descriptor *psd)
1168 struct dom_sid owner_sid;
1169 struct dom_sid grp_sid;
1171 *puser = (uid_t)-1;
1172 *pgrp = (gid_t)-1;
1174 if(security_info_sent == 0) {
1175 DEBUG(0,("unpack_nt_owners: no security info sent !\n"));
1176 return NT_STATUS_OK;
1180 * Validate the owner and group SID's.
1183 memset(&owner_sid, '\0', sizeof(owner_sid));
1184 memset(&grp_sid, '\0', sizeof(grp_sid));
1186 DEBUG(5,("unpack_nt_owners: validating owner_sids.\n"));
1189 * Don't immediately fail if the owner sid cannot be validated.
1190 * This may be a group chown only set.
1193 if (security_info_sent & SECINFO_OWNER) {
1194 sid_copy(&owner_sid, psd->owner_sid);
1195 if (!sid_to_uid(&owner_sid, puser)) {
1196 if (lp_force_unknown_acl_user(SNUM(conn))) {
1197 /* this allows take ownership to work
1198 * reasonably */
1199 *puser = get_current_uid(conn);
1200 } else {
1201 DEBUG(3,("unpack_nt_owners: unable to validate"
1202 " owner sid for %s\n",
1203 sid_string_dbg(&owner_sid)));
1204 return NT_STATUS_INVALID_OWNER;
1207 DEBUG(3,("unpack_nt_owners: owner sid mapped to uid %u\n",
1208 (unsigned int)*puser ));
1212 * Don't immediately fail if the group sid cannot be validated.
1213 * This may be an owner chown only set.
1216 if (security_info_sent & SECINFO_GROUP) {
1217 sid_copy(&grp_sid, psd->group_sid);
1218 if (!sid_to_gid( &grp_sid, pgrp)) {
1219 if (lp_force_unknown_acl_user(SNUM(conn))) {
1220 /* this allows take group ownership to work
1221 * reasonably */
1222 *pgrp = get_current_gid(conn);
1223 } else {
1224 DEBUG(3,("unpack_nt_owners: unable to validate"
1225 " group sid.\n"));
1226 return NT_STATUS_INVALID_OWNER;
1229 DEBUG(3,("unpack_nt_owners: group sid mapped to gid %u\n",
1230 (unsigned int)*pgrp));
1233 DEBUG(5,("unpack_nt_owners: owner_sids validated.\n"));
1235 return NT_STATUS_OK;
1238 /****************************************************************************
1239 Ensure the enforced permissions for this share apply.
1240 ****************************************************************************/
1242 static void apply_default_perms(const struct share_params *params,
1243 const bool is_directory, canon_ace *pace,
1244 mode_t type)
1246 mode_t and_bits = (mode_t)0;
1247 mode_t or_bits = (mode_t)0;
1249 /* Get the initial bits to apply. */
1251 if (is_directory) {
1252 and_bits = lp_dir_mask(params->service);
1253 or_bits = lp_force_dir_mode(params->service);
1254 } else {
1255 and_bits = lp_create_mask(params->service);
1256 or_bits = lp_force_create_mode(params->service);
1259 /* Now bounce them into the S_USR space. */
1260 switch(type) {
1261 case S_IRUSR:
1262 /* Ensure owner has read access. */
1263 pace->perms |= S_IRUSR;
1264 if (is_directory)
1265 pace->perms |= (S_IWUSR|S_IXUSR);
1266 and_bits = unix_perms_to_acl_perms(and_bits, S_IRUSR, S_IWUSR, S_IXUSR);
1267 or_bits = unix_perms_to_acl_perms(or_bits, S_IRUSR, S_IWUSR, S_IXUSR);
1268 break;
1269 case S_IRGRP:
1270 and_bits = unix_perms_to_acl_perms(and_bits, S_IRGRP, S_IWGRP, S_IXGRP);
1271 or_bits = unix_perms_to_acl_perms(or_bits, S_IRGRP, S_IWGRP, S_IXGRP);
1272 break;
1273 case S_IROTH:
1274 and_bits = unix_perms_to_acl_perms(and_bits, S_IROTH, S_IWOTH, S_IXOTH);
1275 or_bits = unix_perms_to_acl_perms(or_bits, S_IROTH, S_IWOTH, S_IXOTH);
1276 break;
1279 pace->perms = ((pace->perms & and_bits)|or_bits);
1282 /****************************************************************************
1283 Check if a given uid/SID is in a group gid/SID. This is probably very
1284 expensive and will need optimisation. A *lot* of optimisation :-). JRA.
1285 ****************************************************************************/
1287 static bool uid_entry_in_group(connection_struct *conn, canon_ace *uid_ace, canon_ace *group_ace )
1289 /* "Everyone" always matches every uid. */
1291 if (dom_sid_equal(&group_ace->trustee, &global_sid_World))
1292 return True;
1295 * if it's the current user, we already have the unix token
1296 * and don't need to do the complex user_in_group_sid() call
1298 if (uid_ace->unix_ug.id == get_current_uid(conn)) {
1299 const struct security_unix_token *curr_utok = NULL;
1300 size_t i;
1302 if (group_ace->unix_ug.id == get_current_gid(conn)) {
1303 return True;
1306 curr_utok = get_current_utok(conn);
1307 for (i=0; i < curr_utok->ngroups; i++) {
1308 if (group_ace->unix_ug.id == curr_utok->groups[i]) {
1309 return True;
1315 * user_in_group_sid() uses create_token_from_sid()
1316 * which creates an artificial NT token given just a username,
1317 * so this is not reliable for users from foreign domains
1318 * exported by winbindd!
1320 return user_sid_in_group_sid(&uid_ace->trustee, &group_ace->trustee);
1323 /****************************************************************************
1324 A well formed POSIX file or default ACL has at least 3 entries, a
1325 SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ.
1326 In addition, the owner must always have at least read access.
1327 When using this call on get_acl, the pst struct is valid and contains
1328 the mode of the file.
1329 ****************************************************************************/
1331 static bool ensure_canon_entry_valid_on_get(connection_struct *conn,
1332 canon_ace **pp_ace,
1333 const struct dom_sid *pfile_owner_sid,
1334 const struct dom_sid *pfile_grp_sid,
1335 const SMB_STRUCT_STAT *pst)
1337 canon_ace *pace;
1338 bool got_user = false;
1339 bool got_group = false;
1340 bool got_other = false;
1342 for (pace = *pp_ace; pace; pace = pace->next) {
1343 if (pace->type == SMB_ACL_USER_OBJ) {
1344 got_user = true;
1345 } else if (pace->type == SMB_ACL_GROUP_OBJ) {
1346 got_group = true;
1347 } else if (pace->type == SMB_ACL_OTHER) {
1348 got_other = true;
1352 if (!got_user) {
1353 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1354 DEBUG(0,("malloc fail.\n"));
1355 return false;
1358 ZERO_STRUCTP(pace);
1359 pace->type = SMB_ACL_USER_OBJ;
1360 pace->owner_type = UID_ACE;
1361 pace->unix_ug.type = ID_TYPE_UID;
1362 pace->unix_ug.id = pst->st_ex_uid;
1363 pace->trustee = *pfile_owner_sid;
1364 pace->attr = ALLOW_ACE;
1365 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IRUSR, S_IWUSR, S_IXUSR);
1366 DLIST_ADD(*pp_ace, pace);
1369 if (!got_group) {
1370 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1371 DEBUG(0,("malloc fail.\n"));
1372 return false;
1375 ZERO_STRUCTP(pace);
1376 pace->type = SMB_ACL_GROUP_OBJ;
1377 pace->owner_type = GID_ACE;
1378 pace->unix_ug.type = ID_TYPE_GID;
1379 pace->unix_ug.id = pst->st_ex_gid;
1380 pace->trustee = *pfile_grp_sid;
1381 pace->attr = ALLOW_ACE;
1382 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IRGRP, S_IWGRP, S_IXGRP);
1383 DLIST_ADD(*pp_ace, pace);
1386 if (!got_other) {
1387 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1388 DEBUG(0,("malloc fail.\n"));
1389 return false;
1392 ZERO_STRUCTP(pace);
1393 pace->type = SMB_ACL_OTHER;
1394 pace->owner_type = WORLD_ACE;
1395 pace->unix_ug.type = ID_TYPE_NOT_SPECIFIED;
1396 pace->unix_ug.id = -1;
1397 pace->trustee = global_sid_World;
1398 pace->attr = ALLOW_ACE;
1399 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IROTH, S_IWOTH, S_IXOTH);
1400 DLIST_ADD(*pp_ace, pace);
1403 return true;
1406 /****************************************************************************
1407 A well formed POSIX file or default ACL has at least 3 entries, a
1408 SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ.
1409 In addition, the owner must always have at least read access.
1410 When using this call on get_acl, the pst struct is valid and contains
1411 the mode of the file. When using this call on set_acl, the pst struct has
1412 been modified to have a mode containing the default for this file or directory
1413 type.
1414 ****************************************************************************/
1416 static bool ensure_canon_entry_valid(connection_struct *conn,
1417 canon_ace **pp_ace,
1418 bool is_default_acl,
1419 const struct share_params *params,
1420 const bool is_directory,
1421 const struct dom_sid *pfile_owner_sid,
1422 const struct dom_sid *pfile_grp_sid,
1423 const SMB_STRUCT_STAT *pst,
1424 bool setting_acl)
1426 canon_ace *pace;
1427 canon_ace *pace_user = NULL;
1428 canon_ace *pace_group = NULL;
1429 canon_ace *pace_other = NULL;
1431 for (pace = *pp_ace; pace; pace = pace->next) {
1432 if (pace->type == SMB_ACL_USER_OBJ) {
1434 if (setting_acl && !is_default_acl) {
1435 apply_default_perms(params, is_directory, pace, S_IRUSR);
1437 pace_user = pace;
1439 } else if (pace->type == SMB_ACL_GROUP_OBJ) {
1442 * Ensure create mask/force create mode is respected on set.
1445 if (setting_acl && !is_default_acl) {
1446 apply_default_perms(params, is_directory, pace, S_IRGRP);
1448 pace_group = pace;
1450 } else if (pace->type == SMB_ACL_OTHER) {
1453 * Ensure create mask/force create mode is respected on set.
1456 if (setting_acl && !is_default_acl) {
1457 apply_default_perms(params, is_directory, pace, S_IROTH);
1459 pace_other = pace;
1461 } else if (pace->type == SMB_ACL_USER || pace->type == SMB_ACL_GROUP) {
1464 * Ensure create mask/force create mode is respected on set.
1467 if (setting_acl && !is_default_acl) {
1468 apply_default_perms(params, is_directory, pace, S_IRGRP);
1473 if (!pace_user) {
1474 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1475 DEBUG(0,("ensure_canon_entry_valid: malloc fail.\n"));
1476 return False;
1479 ZERO_STRUCTP(pace);
1480 pace->type = SMB_ACL_USER_OBJ;
1481 pace->owner_type = UID_ACE;
1482 pace->unix_ug.type = ID_TYPE_UID;
1483 pace->unix_ug.id = pst->st_ex_uid;
1484 pace->trustee = *pfile_owner_sid;
1485 pace->attr = ALLOW_ACE;
1486 /* Start with existing user permissions, principle of least
1487 surprises for the user. */
1488 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IRUSR, S_IWUSR, S_IXUSR);
1490 if (setting_acl) {
1491 /* See if the owning user is in any of the other groups in
1492 the ACE, or if there's a matching user entry (by uid
1493 or in the case of ID_TYPE_BOTH by SID).
1494 If so, OR in the permissions from that entry. */
1496 canon_ace *pace_iter;
1498 for (pace_iter = *pp_ace; pace_iter; pace_iter = pace_iter->next) {
1499 if (pace_iter->type == SMB_ACL_USER &&
1500 pace_iter->unix_ug.id == pace->unix_ug.id) {
1501 pace->perms |= pace_iter->perms;
1502 } else if (pace_iter->type == SMB_ACL_GROUP_OBJ || pace_iter->type == SMB_ACL_GROUP) {
1503 if (dom_sid_equal(&pace->trustee, &pace_iter->trustee)) {
1504 pace->perms |= pace_iter->perms;
1505 } else if (uid_entry_in_group(conn, pace, pace_iter)) {
1506 pace->perms |= pace_iter->perms;
1511 if (pace->perms == 0) {
1512 /* If we only got an "everyone" perm, just use that. */
1513 if (pace_other)
1514 pace->perms = pace_other->perms;
1517 if (!is_default_acl) {
1518 apply_default_perms(params, is_directory, pace, S_IRUSR);
1520 } else {
1521 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IRUSR, S_IWUSR, S_IXUSR);
1524 DLIST_ADD(*pp_ace, pace);
1525 pace_user = pace;
1528 if (!pace_group) {
1529 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1530 DEBUG(0,("ensure_canon_entry_valid: malloc fail.\n"));
1531 return False;
1534 ZERO_STRUCTP(pace);
1535 pace->type = SMB_ACL_GROUP_OBJ;
1536 pace->owner_type = GID_ACE;
1537 pace->unix_ug.type = ID_TYPE_GID;
1538 pace->unix_ug.id = pst->st_ex_gid;
1539 pace->trustee = *pfile_grp_sid;
1540 pace->attr = ALLOW_ACE;
1541 if (setting_acl) {
1542 /* If we only got an "everyone" perm, just use that. */
1543 if (pace_other)
1544 pace->perms = pace_other->perms;
1545 else
1546 pace->perms = 0;
1547 if (!is_default_acl) {
1548 apply_default_perms(params, is_directory, pace, S_IRGRP);
1550 } else {
1551 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IRGRP, S_IWGRP, S_IXGRP);
1554 DLIST_ADD(*pp_ace, pace);
1555 pace_group = pace;
1558 if (!pace_other) {
1559 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1560 DEBUG(0,("ensure_canon_entry_valid: malloc fail.\n"));
1561 return False;
1564 ZERO_STRUCTP(pace);
1565 pace->type = SMB_ACL_OTHER;
1566 pace->owner_type = WORLD_ACE;
1567 pace->unix_ug.type = ID_TYPE_NOT_SPECIFIED;
1568 pace->unix_ug.id = -1;
1569 pace->trustee = global_sid_World;
1570 pace->attr = ALLOW_ACE;
1571 if (setting_acl) {
1572 pace->perms = 0;
1573 if (!is_default_acl) {
1574 apply_default_perms(params, is_directory, pace, S_IROTH);
1576 } else
1577 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IROTH, S_IWOTH, S_IXOTH);
1579 DLIST_ADD(*pp_ace, pace);
1580 pace_other = pace;
1583 if (setting_acl) {
1584 /* Ensure when setting a POSIX ACL, that the uid for a
1585 SMB_ACL_USER_OBJ ACE (the owner ACE entry) has a duplicate
1586 permission entry as an SMB_ACL_USER, and a gid for a
1587 SMB_ACL_GROUP_OBJ ACE (the primary group ACE entry) also has
1588 a duplicate permission entry as an SMB_ACL_GROUP. If not,
1589 then if the ownership or group ownership of this file or
1590 directory gets changed, the user or group can lose their
1591 access. */
1592 bool got_duplicate_user = false;
1593 bool got_duplicate_group = false;
1595 for (pace = *pp_ace; pace; pace = pace->next) {
1596 if (pace->type == SMB_ACL_USER &&
1597 pace->unix_ug.id == pace_user->unix_ug.id) {
1598 /* Already got one. */
1599 got_duplicate_user = true;
1600 } else if (pace->type == SMB_ACL_GROUP &&
1601 pace->unix_ug.id == pace_group->unix_ug.id) {
1602 /* Already got one. */
1603 got_duplicate_group = true;
1604 } else if ((pace->type == SMB_ACL_GROUP)
1605 && (dom_sid_equal(&pace->trustee, &pace_user->trustee))) {
1606 /* If the SID owning the file appears
1607 * in a group entry, then we have
1608 * enough duplication, they will still
1609 * have access */
1610 got_duplicate_user = true;
1614 /* If the SID is equal for the user and group that we need
1615 to add the duplicate for, add only the group */
1616 if (!got_duplicate_user && !got_duplicate_group
1617 && dom_sid_equal(&pace_group->trustee,
1618 &pace_user->trustee)) {
1619 /* Add a duplicate SMB_ACL_GROUP entry, this
1620 * will cover the owning SID as well, as it
1621 * will always be mapped to both a uid and
1622 * gid. */
1624 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1625 DEBUG(0,("ensure_canon_entry_valid: talloc fail.\n"));
1626 return false;
1629 ZERO_STRUCTP(pace);
1630 pace->type = SMB_ACL_GROUP;;
1631 pace->owner_type = GID_ACE;
1632 pace->unix_ug.type = ID_TYPE_GID;
1633 pace->unix_ug.id = pace_group->unix_ug.id;
1634 pace->trustee = pace_group->trustee;
1635 pace->attr = pace_group->attr;
1636 pace->perms = pace_group->perms;
1638 DLIST_ADD(*pp_ace, pace);
1640 /* We're done here, make sure the
1641 statements below are not executed. */
1642 got_duplicate_user = true;
1643 got_duplicate_group = true;
1646 if (!got_duplicate_user) {
1647 /* Add a duplicate SMB_ACL_USER entry. */
1648 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1649 DEBUG(0,("ensure_canon_entry_valid: talloc fail.\n"));
1650 return false;
1653 ZERO_STRUCTP(pace);
1654 pace->type = SMB_ACL_USER;;
1655 pace->owner_type = UID_ACE;
1656 pace->unix_ug.type = ID_TYPE_UID;
1657 pace->unix_ug.id = pace_user->unix_ug.id;
1658 pace->trustee = pace_user->trustee;
1659 pace->attr = pace_user->attr;
1660 pace->perms = pace_user->perms;
1662 DLIST_ADD(*pp_ace, pace);
1664 got_duplicate_user = true;
1667 if (!got_duplicate_group) {
1668 /* Add a duplicate SMB_ACL_GROUP entry. */
1669 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1670 DEBUG(0,("ensure_canon_entry_valid: talloc fail.\n"));
1671 return false;
1674 ZERO_STRUCTP(pace);
1675 pace->type = SMB_ACL_GROUP;;
1676 pace->owner_type = GID_ACE;
1677 pace->unix_ug.type = ID_TYPE_GID;
1678 pace->unix_ug.id = pace_group->unix_ug.id;
1679 pace->trustee = pace_group->trustee;
1680 pace->attr = pace_group->attr;
1681 pace->perms = pace_group->perms;
1683 DLIST_ADD(*pp_ace, pace);
1685 got_duplicate_group = true;
1690 return True;
1693 /****************************************************************************
1694 Check if a POSIX ACL has the required SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ entries.
1695 If it does not have them, check if there are any entries where the trustee is the
1696 file owner or the owning group, and map these to SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ.
1697 Note we must not do this to default directory ACLs.
1698 ****************************************************************************/
1700 static void check_owning_objs(canon_ace *ace, struct dom_sid *pfile_owner_sid, struct dom_sid *pfile_grp_sid)
1702 bool got_user_obj, got_group_obj;
1703 canon_ace *current_ace;
1704 int i, entries;
1706 entries = count_canon_ace_list(ace);
1707 got_user_obj = False;
1708 got_group_obj = False;
1710 for (i=0, current_ace = ace; i < entries; i++, current_ace = current_ace->next) {
1711 if (current_ace->type == SMB_ACL_USER_OBJ)
1712 got_user_obj = True;
1713 else if (current_ace->type == SMB_ACL_GROUP_OBJ)
1714 got_group_obj = True;
1716 if (got_user_obj && got_group_obj) {
1717 DEBUG(10,("check_owning_objs: ACL had owning user/group entries.\n"));
1718 return;
1721 for (i=0, current_ace = ace; i < entries; i++, current_ace = current_ace->next) {
1722 if (!got_user_obj && current_ace->owner_type == UID_ACE &&
1723 dom_sid_equal(&current_ace->trustee, pfile_owner_sid)) {
1724 current_ace->type = SMB_ACL_USER_OBJ;
1725 got_user_obj = True;
1727 if (!got_group_obj && current_ace->owner_type == GID_ACE &&
1728 dom_sid_equal(&current_ace->trustee, pfile_grp_sid)) {
1729 current_ace->type = SMB_ACL_GROUP_OBJ;
1730 got_group_obj = True;
1733 if (!got_user_obj)
1734 DEBUG(10,("check_owning_objs: ACL is missing an owner entry.\n"));
1735 if (!got_group_obj)
1736 DEBUG(10,("check_owning_objs: ACL is missing an owning group entry.\n"));
1739 static bool add_current_ace_to_acl(files_struct *fsp, struct security_ace *psa,
1740 canon_ace **file_ace, canon_ace **dir_ace,
1741 bool *got_file_allow, bool *got_dir_allow,
1742 bool *all_aces_are_inherit_only,
1743 canon_ace *current_ace)
1747 * Map the given NT permissions into a UNIX mode_t containing only
1748 * S_I(R|W|X)USR bits.
1751 current_ace->perms |= map_nt_perms( &psa->access_mask, S_IRUSR);
1752 current_ace->attr = (psa->type == SEC_ACE_TYPE_ACCESS_ALLOWED) ? ALLOW_ACE : DENY_ACE;
1754 /* Store the ace_flag. */
1755 current_ace->ace_flags = psa->flags;
1758 * Now add the created ace to either the file list, the directory
1759 * list, or both. We *MUST* preserve the order here (hence we use
1760 * DLIST_ADD_END) as NT ACLs are order dependent.
1763 if (fsp->is_directory) {
1766 * We can only add to the default POSIX ACE list if the ACE is
1767 * designed to be inherited by both files and directories.
1770 if ((psa->flags & (SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT)) ==
1771 (SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT)) {
1773 canon_ace *current_dir_ace = current_ace;
1774 DLIST_ADD_END(*dir_ace, current_ace, canon_ace *);
1777 * Note if this was an allow ace. We can't process
1778 * any further deny ace's after this.
1781 if (current_ace->attr == ALLOW_ACE)
1782 *got_dir_allow = True;
1784 if ((current_ace->attr == DENY_ACE) && *got_dir_allow) {
1785 DEBUG(0,("add_current_ace_to_acl: "
1786 "malformed ACL in "
1787 "inheritable ACL! Deny entry "
1788 "after Allow entry. Failing "
1789 "to set on file %s.\n",
1790 fsp_str_dbg(fsp)));
1791 return False;
1794 if( DEBUGLVL( 10 )) {
1795 dbgtext("add_current_ace_to_acl: adding dir ACL:\n");
1796 print_canon_ace( current_ace, 0);
1800 * If this is not an inherit only ACE we need to add a duplicate
1801 * to the file acl.
1804 if (!(psa->flags & SEC_ACE_FLAG_INHERIT_ONLY)) {
1805 canon_ace *dup_ace = dup_canon_ace(current_ace);
1807 if (!dup_ace) {
1808 DEBUG(0,("add_current_ace_to_acl: malloc fail !\n"));
1809 return False;
1813 * We must not free current_ace here as its
1814 * pointer is now owned by the dir_ace list.
1816 current_ace = dup_ace;
1817 /* We've essentially split this ace into two,
1818 * and added the ace with inheritance request
1819 * bits to the directory ACL. Drop those bits for
1820 * the ACE we're adding to the file list. */
1821 current_ace->ace_flags &= ~(SEC_ACE_FLAG_OBJECT_INHERIT|
1822 SEC_ACE_FLAG_CONTAINER_INHERIT|
1823 SEC_ACE_FLAG_INHERIT_ONLY);
1824 } else {
1826 * We must not free current_ace here as its
1827 * pointer is now owned by the dir_ace list.
1829 current_ace = NULL;
1833 * current_ace is now either owned by file_ace
1834 * or is NULL. We can safely operate on current_dir_ace
1835 * to treat mapping for default acl entries differently
1836 * than access acl entries.
1839 if (current_dir_ace->owner_type == UID_ACE) {
1841 * We already decided above this is a uid,
1842 * for default acls ace's only CREATOR_OWNER
1843 * maps to ACL_USER_OBJ. All other uid
1844 * ace's are ACL_USER.
1846 if (dom_sid_equal(&current_dir_ace->trustee,
1847 &global_sid_Creator_Owner)) {
1848 current_dir_ace->type = SMB_ACL_USER_OBJ;
1849 } else {
1850 current_dir_ace->type = SMB_ACL_USER;
1854 if (current_dir_ace->owner_type == GID_ACE) {
1856 * We already decided above this is a gid,
1857 * for default acls ace's only CREATOR_GROUP
1858 * maps to ACL_GROUP_OBJ. All other uid
1859 * ace's are ACL_GROUP.
1861 if (dom_sid_equal(&current_dir_ace->trustee,
1862 &global_sid_Creator_Group)) {
1863 current_dir_ace->type = SMB_ACL_GROUP_OBJ;
1864 } else {
1865 current_dir_ace->type = SMB_ACL_GROUP;
1872 * Only add to the file ACL if not inherit only.
1875 if (current_ace && !(psa->flags & SEC_ACE_FLAG_INHERIT_ONLY)) {
1876 DLIST_ADD_END(*file_ace, current_ace, canon_ace *);
1879 * Note if this was an allow ace. We can't process
1880 * any further deny ace's after this.
1883 if (current_ace->attr == ALLOW_ACE)
1884 *got_file_allow = True;
1886 if ((current_ace->attr == DENY_ACE) && got_file_allow) {
1887 DEBUG(0,("add_current_ace_to_acl: malformed "
1888 "ACL in file ACL ! Deny entry after "
1889 "Allow entry. Failing to set on file "
1890 "%s.\n", fsp_str_dbg(fsp)));
1891 return False;
1894 if( DEBUGLVL( 10 )) {
1895 dbgtext("add_current_ace_to_acl: adding file ACL:\n");
1896 print_canon_ace( current_ace, 0);
1898 *all_aces_are_inherit_only = False;
1900 * We must not free current_ace here as its
1901 * pointer is now owned by the file_ace list.
1903 current_ace = NULL;
1907 * Free if ACE was not added.
1910 TALLOC_FREE(current_ace);
1911 return true;
1914 /****************************************************************************
1915 Unpack a struct security_descriptor into two canonical ace lists.
1916 ****************************************************************************/
1918 static bool create_canon_ace_lists(files_struct *fsp,
1919 const SMB_STRUCT_STAT *pst,
1920 struct dom_sid *pfile_owner_sid,
1921 struct dom_sid *pfile_grp_sid,
1922 canon_ace **ppfile_ace,
1923 canon_ace **ppdir_ace,
1924 const struct security_acl *dacl)
1926 bool all_aces_are_inherit_only = (fsp->is_directory ? True : False);
1927 canon_ace *file_ace = NULL;
1928 canon_ace *dir_ace = NULL;
1929 canon_ace *current_ace = NULL;
1930 bool got_dir_allow = False;
1931 bool got_file_allow = False;
1932 int i, j;
1934 *ppfile_ace = NULL;
1935 *ppdir_ace = NULL;
1938 * Convert the incoming ACL into a more regular form.
1941 for(i = 0; i < dacl->num_aces; i++) {
1942 struct security_ace *psa = &dacl->aces[i];
1944 if((psa->type != SEC_ACE_TYPE_ACCESS_ALLOWED) && (psa->type != SEC_ACE_TYPE_ACCESS_DENIED)) {
1945 DEBUG(3,("create_canon_ace_lists: unable to set anything but an ALLOW or DENY ACE.\n"));
1946 return False;
1951 * Deal with the fact that NT 4.x re-writes the canonical format
1952 * that we return for default ACLs. If a directory ACE is identical
1953 * to a inherited directory ACE then NT changes the bits so that the
1954 * first ACE is set to OI|IO and the second ACE for this SID is set
1955 * to CI. We need to repair this. JRA.
1958 for(i = 0; i < dacl->num_aces; i++) {
1959 struct security_ace *psa1 = &dacl->aces[i];
1961 for (j = i + 1; j < dacl->num_aces; j++) {
1962 struct security_ace *psa2 = &dacl->aces[j];
1964 if (psa1->access_mask != psa2->access_mask)
1965 continue;
1967 if (!dom_sid_equal(&psa1->trustee, &psa2->trustee))
1968 continue;
1971 * Ok - permission bits and SIDs are equal.
1972 * Check if flags were re-written.
1975 if (psa1->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
1977 psa1->flags |= (psa2->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT));
1978 psa2->flags &= ~(SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT);
1980 } else if (psa2->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
1982 psa2->flags |= (psa1->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT));
1983 psa1->flags &= ~(SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT);
1989 for(i = 0; i < dacl->num_aces; i++) {
1990 struct security_ace *psa = &dacl->aces[i];
1993 * Create a canon_ace entry representing this NT DACL ACE.
1996 if ((current_ace = talloc(talloc_tos(), canon_ace)) == NULL) {
1997 free_canon_ace_list(file_ace);
1998 free_canon_ace_list(dir_ace);
1999 DEBUG(0,("create_canon_ace_lists: malloc fail.\n"));
2000 return False;
2003 ZERO_STRUCTP(current_ace);
2005 sid_copy(&current_ace->trustee, &psa->trustee);
2008 * Try and work out if the SID is a user or group
2009 * as we need to flag these differently for POSIX.
2010 * Note what kind of a POSIX ACL this should map to.
2013 if( dom_sid_equal(&current_ace->trustee, &global_sid_World)) {
2014 current_ace->owner_type = WORLD_ACE;
2015 current_ace->unix_ug.type = ID_TYPE_NOT_SPECIFIED;
2016 current_ace->unix_ug.id = -1;
2017 current_ace->type = SMB_ACL_OTHER;
2018 } else if (dom_sid_equal(&current_ace->trustee, &global_sid_Creator_Owner)) {
2019 current_ace->owner_type = UID_ACE;
2020 current_ace->unix_ug.type = ID_TYPE_UID;
2021 current_ace->unix_ug.id = pst->st_ex_uid;
2022 current_ace->type = SMB_ACL_USER_OBJ;
2025 * The Creator Owner entry only specifies inheritable permissions,
2026 * never access permissions. WinNT doesn't always set the ACE to
2027 * INHERIT_ONLY, though.
2030 psa->flags |= SEC_ACE_FLAG_INHERIT_ONLY;
2032 } else if (dom_sid_equal(&current_ace->trustee, &global_sid_Creator_Group)) {
2033 current_ace->owner_type = GID_ACE;
2034 current_ace->unix_ug.type = ID_TYPE_GID;
2035 current_ace->unix_ug.id = pst->st_ex_gid;
2036 current_ace->type = SMB_ACL_GROUP_OBJ;
2039 * The Creator Group entry only specifies inheritable permissions,
2040 * never access permissions. WinNT doesn't always set the ACE to
2041 * INHERIT_ONLY, though.
2043 psa->flags |= SEC_ACE_FLAG_INHERIT_ONLY;
2045 } else {
2046 struct unixid unixid;
2048 if (!sids_to_unixids(&current_ace->trustee, 1, &unixid)) {
2049 free_canon_ace_list(file_ace);
2050 free_canon_ace_list(dir_ace);
2051 TALLOC_FREE(current_ace);
2052 DEBUG(0, ("create_canon_ace_lists: sids_to_unixids "
2053 "failed for %s (allocation failure)\n",
2054 sid_string_dbg(&current_ace->trustee)));
2055 return false;
2058 if (unixid.type == ID_TYPE_BOTH) {
2059 /* If it's the owning user, this is a
2060 * user_obj, not a user. This way, we
2061 * get a valid ACL for groups that own
2062 * files, without putting user ACL
2063 * entries in for groups otherwise */
2064 if (unixid.id == pst->st_ex_uid) {
2065 current_ace->owner_type = UID_ACE;
2066 current_ace->unix_ug.type = ID_TYPE_UID;
2067 current_ace->unix_ug.id = unixid.id;
2068 current_ace->type = SMB_ACL_USER_OBJ;
2070 /* Add the user object to the posix ACL,
2071 and proceed to the group mapping
2072 below. This handles the talloc_free
2073 of current_ace if not added for some
2074 reason */
2075 if (!add_current_ace_to_acl(fsp,
2076 psa,
2077 &file_ace,
2078 &dir_ace,
2079 &got_file_allow,
2080 &got_dir_allow,
2081 &all_aces_are_inherit_only,
2082 current_ace)) {
2083 free_canon_ace_list(file_ace);
2084 free_canon_ace_list(dir_ace);
2085 return false;
2088 if ((current_ace = talloc(talloc_tos(),
2089 canon_ace)) == NULL) {
2090 free_canon_ace_list(file_ace);
2091 free_canon_ace_list(dir_ace);
2092 DEBUG(0,("create_canon_ace_lists: "
2093 "malloc fail.\n"));
2094 return False;
2097 ZERO_STRUCTP(current_ace);
2100 sid_copy(&current_ace->trustee, &psa->trustee);
2102 current_ace->unix_ug.type = ID_TYPE_GID;
2103 current_ace->unix_ug.id = unixid.id;
2104 current_ace->owner_type = GID_ACE;
2105 /* If it's the primary group, this is a
2106 group_obj, not a group. */
2107 if (current_ace->unix_ug.id == pst->st_ex_gid) {
2108 current_ace->type = SMB_ACL_GROUP_OBJ;
2109 } else {
2110 current_ace->type = SMB_ACL_GROUP;
2113 } else if (unixid.type == ID_TYPE_UID) {
2114 current_ace->owner_type = UID_ACE;
2115 current_ace->unix_ug.type = ID_TYPE_UID;
2116 current_ace->unix_ug.id = unixid.id;
2117 /* If it's the owning user, this is a user_obj,
2118 not a user. */
2119 if (current_ace->unix_ug.id == pst->st_ex_uid) {
2120 current_ace->type = SMB_ACL_USER_OBJ;
2121 } else {
2122 current_ace->type = SMB_ACL_USER;
2124 } else if (unixid.type == ID_TYPE_GID) {
2125 current_ace->unix_ug.type = ID_TYPE_GID;
2126 current_ace->unix_ug.id = unixid.id;
2127 current_ace->owner_type = GID_ACE;
2128 /* If it's the primary group, this is a
2129 group_obj, not a group. */
2130 if (current_ace->unix_ug.id == pst->st_ex_gid) {
2131 current_ace->type = SMB_ACL_GROUP_OBJ;
2132 } else {
2133 current_ace->type = SMB_ACL_GROUP;
2135 } else {
2137 * Silently ignore map failures in non-mappable SIDs (NT Authority, BUILTIN etc).
2140 if (non_mappable_sid(&psa->trustee)) {
2141 DEBUG(10, ("create_canon_ace_lists: ignoring "
2142 "non-mappable SID %s\n",
2143 sid_string_dbg(&psa->trustee)));
2144 TALLOC_FREE(current_ace);
2145 continue;
2148 if (lp_force_unknown_acl_user(SNUM(fsp->conn))) {
2149 DEBUG(10, ("create_canon_ace_lists: ignoring "
2150 "unknown or foreign SID %s\n",
2151 sid_string_dbg(&psa->trustee)));
2152 TALLOC_FREE(current_ace);
2153 continue;
2156 free_canon_ace_list(file_ace);
2157 free_canon_ace_list(dir_ace);
2158 DEBUG(0, ("create_canon_ace_lists: unable to map SID "
2159 "%s to uid or gid.\n",
2160 sid_string_dbg(&current_ace->trustee)));
2161 TALLOC_FREE(current_ace);
2162 return false;
2166 /* handles the talloc_free of current_ace if not added for some reason */
2167 if (!add_current_ace_to_acl(fsp, psa, &file_ace, &dir_ace,
2168 &got_file_allow, &got_dir_allow,
2169 &all_aces_are_inherit_only, current_ace)) {
2170 free_canon_ace_list(file_ace);
2171 free_canon_ace_list(dir_ace);
2172 return false;
2176 if (fsp->is_directory && all_aces_are_inherit_only) {
2178 * Windows 2000 is doing one of these weird 'inherit acl'
2179 * traverses to conserve NTFS ACL resources. Just pretend
2180 * there was no DACL sent. JRA.
2183 DEBUG(10,("create_canon_ace_lists: Win2k inherit acl traverse. Ignoring DACL.\n"));
2184 free_canon_ace_list(file_ace);
2185 free_canon_ace_list(dir_ace);
2186 file_ace = NULL;
2187 dir_ace = NULL;
2188 } else {
2190 * Check if we have SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ entries in
2191 * the file ACL. If we don't have them, check if any SMB_ACL_USER/SMB_ACL_GROUP
2192 * entries can be converted to *_OBJ. Don't do this for the default
2193 * ACL, we will create them separately for this if needed inside
2194 * ensure_canon_entry_valid().
2196 if (file_ace) {
2197 check_owning_objs(file_ace, pfile_owner_sid, pfile_grp_sid);
2201 *ppfile_ace = file_ace;
2202 *ppdir_ace = dir_ace;
2204 return True;
2207 /****************************************************************************
2208 ASCII art time again... JRA :-).
2210 We have 4 cases to process when moving from an NT ACL to a POSIX ACL. Firstly,
2211 we insist the ACL is in canonical form (ie. all DENY entries preceede ALLOW
2212 entries). Secondly, the merge code has ensured that all duplicate SID entries for
2213 allow or deny have been merged, so the same SID can only appear once in the deny
2214 list or once in the allow list.
2216 We then process as follows :
2218 ---------------------------------------------------------------------------
2219 First pass - look for a Everyone DENY entry.
2221 If it is deny all (rwx) trunate the list at this point.
2222 Else, walk the list from this point and use the deny permissions of this
2223 entry as a mask on all following allow entries. Finally, delete
2224 the Everyone DENY entry (we have applied it to everything possible).
2226 In addition, in this pass we remove any DENY entries that have
2227 no permissions (ie. they are a DENY nothing).
2228 ---------------------------------------------------------------------------
2229 Second pass - only deal with deny user entries.
2231 DENY user1 (perms XXX)
2233 new_perms = 0
2234 for all following allow group entries where user1 is in group
2235 new_perms |= group_perms;
2237 user1 entry perms = new_perms & ~ XXX;
2239 Convert the deny entry to an allow entry with the new perms and
2240 push to the end of the list. Note if the user was in no groups
2241 this maps to a specific allow nothing entry for this user.
2243 The common case from the NT ACL choser (userX deny all) is
2244 optimised so we don't do the group lookup - we just map to
2245 an allow nothing entry.
2247 What we're doing here is inferring the allow permissions the
2248 person setting the ACE on user1 wanted by looking at the allow
2249 permissions on the groups the user is currently in. This will
2250 be a snapshot, depending on group membership but is the best
2251 we can do and has the advantage of failing closed rather than
2252 open.
2253 ---------------------------------------------------------------------------
2254 Third pass - only deal with deny group entries.
2256 DENY group1 (perms XXX)
2258 for all following allow user entries where user is in group1
2259 user entry perms = user entry perms & ~ XXX;
2261 If there is a group Everyone allow entry with permissions YYY,
2262 convert the group1 entry to an allow entry and modify its
2263 permissions to be :
2265 new_perms = YYY & ~ XXX
2267 and push to the end of the list.
2269 If there is no group Everyone allow entry then convert the
2270 group1 entry to a allow nothing entry and push to the end of the list.
2272 Note that the common case from the NT ACL choser (groupX deny all)
2273 cannot be optimised here as we need to modify user entries who are
2274 in the group to change them to a deny all also.
2276 What we're doing here is modifying the allow permissions of
2277 user entries (which are more specific in POSIX ACLs) to mask
2278 out the explicit deny set on the group they are in. This will
2279 be a snapshot depending on current group membership but is the
2280 best we can do and has the advantage of failing closed rather
2281 than open.
2282 ---------------------------------------------------------------------------
2283 Fourth pass - cope with cumulative permissions.
2285 for all allow user entries, if there exists an allow group entry with
2286 more permissive permissions, and the user is in that group, rewrite the
2287 allow user permissions to contain both sets of permissions.
2289 Currently the code for this is #ifdef'ed out as these semantics make
2290 no sense to me. JRA.
2291 ---------------------------------------------------------------------------
2293 Note we *MUST* do the deny user pass first as this will convert deny user
2294 entries into allow user entries which can then be processed by the deny
2295 group pass.
2297 The above algorithm took a *lot* of thinking about - hence this
2298 explaination :-). JRA.
2299 ****************************************************************************/
2301 /****************************************************************************
2302 Process a canon_ace list entries. This is very complex code. We need
2303 to go through and remove the "deny" permissions from any allow entry that matches
2304 the id of this entry. We have already refused any NT ACL that wasn't in correct
2305 order (DENY followed by ALLOW). If any allow entry ends up with zero permissions,
2306 we just remove it (to fail safe). We have already removed any duplicate ace
2307 entries. Treat an "Everyone" DENY_ACE as a special case - use it to mask all
2308 allow entries.
2309 ****************************************************************************/
2311 static void process_deny_list(connection_struct *conn, canon_ace **pp_ace_list )
2313 canon_ace *ace_list = *pp_ace_list;
2314 canon_ace *curr_ace = NULL;
2315 canon_ace *curr_ace_next = NULL;
2317 /* Pass 1 above - look for an Everyone, deny entry. */
2319 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2320 canon_ace *allow_ace_p;
2322 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2324 if (curr_ace->attr != DENY_ACE)
2325 continue;
2327 if (curr_ace->perms == (mode_t)0) {
2329 /* Deny nothing entry - delete. */
2331 DLIST_REMOVE(ace_list, curr_ace);
2332 continue;
2335 if (!dom_sid_equal(&curr_ace->trustee, &global_sid_World))
2336 continue;
2338 /* JRATEST - assert. */
2339 SMB_ASSERT(curr_ace->owner_type == WORLD_ACE);
2341 if (curr_ace->perms == ALL_ACE_PERMS) {
2344 * Optimisation. This is a DENY_ALL to Everyone. Truncate the
2345 * list at this point including this entry.
2348 canon_ace *prev_entry = DLIST_PREV(curr_ace);
2350 free_canon_ace_list( curr_ace );
2351 if (prev_entry)
2352 DLIST_REMOVE(ace_list, prev_entry);
2353 else {
2354 /* We deleted the entire list. */
2355 ace_list = NULL;
2357 break;
2360 for (allow_ace_p = curr_ace->next; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2363 * Only mask off allow entries.
2366 if (allow_ace_p->attr != ALLOW_ACE)
2367 continue;
2369 allow_ace_p->perms &= ~curr_ace->perms;
2373 * Now it's been applied, remove it.
2376 DLIST_REMOVE(ace_list, curr_ace);
2379 /* Pass 2 above - deal with deny user entries. */
2381 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2382 mode_t new_perms = (mode_t)0;
2383 canon_ace *allow_ace_p;
2385 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2387 if (curr_ace->attr != DENY_ACE)
2388 continue;
2390 if (curr_ace->owner_type != UID_ACE)
2391 continue;
2393 if (curr_ace->perms == ALL_ACE_PERMS) {
2396 * Optimisation - this is a deny everything to this user.
2397 * Convert to an allow nothing and push to the end of the list.
2400 curr_ace->attr = ALLOW_ACE;
2401 curr_ace->perms = (mode_t)0;
2402 DLIST_DEMOTE(ace_list, curr_ace, canon_ace *);
2403 continue;
2406 for (allow_ace_p = curr_ace->next; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2408 if (allow_ace_p->attr != ALLOW_ACE)
2409 continue;
2411 /* We process GID_ACE and WORLD_ACE entries only. */
2413 if (allow_ace_p->owner_type == UID_ACE)
2414 continue;
2416 if (uid_entry_in_group(conn, curr_ace, allow_ace_p))
2417 new_perms |= allow_ace_p->perms;
2421 * Convert to a allow entry, modify the perms and push to the end
2422 * of the list.
2425 curr_ace->attr = ALLOW_ACE;
2426 curr_ace->perms = (new_perms & ~curr_ace->perms);
2427 DLIST_DEMOTE(ace_list, curr_ace, canon_ace *);
2430 /* Pass 3 above - deal with deny group entries. */
2432 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2433 canon_ace *allow_ace_p;
2434 canon_ace *allow_everyone_p = NULL;
2436 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2438 if (curr_ace->attr != DENY_ACE)
2439 continue;
2441 if (curr_ace->owner_type != GID_ACE)
2442 continue;
2444 for (allow_ace_p = curr_ace->next; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2446 if (allow_ace_p->attr != ALLOW_ACE)
2447 continue;
2449 /* Store a pointer to the Everyone allow, if it exists. */
2450 if (allow_ace_p->owner_type == WORLD_ACE)
2451 allow_everyone_p = allow_ace_p;
2453 /* We process UID_ACE entries only. */
2455 if (allow_ace_p->owner_type != UID_ACE)
2456 continue;
2458 /* Mask off the deny group perms. */
2460 if (uid_entry_in_group(conn, allow_ace_p, curr_ace))
2461 allow_ace_p->perms &= ~curr_ace->perms;
2465 * Convert the deny to an allow with the correct perms and
2466 * push to the end of the list.
2469 curr_ace->attr = ALLOW_ACE;
2470 if (allow_everyone_p)
2471 curr_ace->perms = allow_everyone_p->perms & ~curr_ace->perms;
2472 else
2473 curr_ace->perms = (mode_t)0;
2474 DLIST_DEMOTE(ace_list, curr_ace, canon_ace *);
2477 /* Doing this fourth pass allows Windows semantics to be layered
2478 * on top of POSIX semantics. I'm not sure if this is desirable.
2479 * For example, in W2K ACLs there is no way to say, "Group X no
2480 * access, user Y full access" if user Y is a member of group X.
2481 * This seems completely broken semantics to me.... JRA.
2484 #if 0
2485 /* Pass 4 above - deal with allow entries. */
2487 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2488 canon_ace *allow_ace_p;
2490 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2492 if (curr_ace->attr != ALLOW_ACE)
2493 continue;
2495 if (curr_ace->owner_type != UID_ACE)
2496 continue;
2498 for (allow_ace_p = ace_list; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2500 if (allow_ace_p->attr != ALLOW_ACE)
2501 continue;
2503 /* We process GID_ACE entries only. */
2505 if (allow_ace_p->owner_type != GID_ACE)
2506 continue;
2508 /* OR in the group perms. */
2510 if (uid_entry_in_group(conn, curr_ace, allow_ace_p))
2511 curr_ace->perms |= allow_ace_p->perms;
2514 #endif
2516 *pp_ace_list = ace_list;
2519 /****************************************************************************
2520 Unpack a struct security_descriptor into two canonical ace lists. We don't depend on this
2521 succeeding.
2522 ****************************************************************************/
2524 static bool unpack_canon_ace(files_struct *fsp,
2525 const SMB_STRUCT_STAT *pst,
2526 struct dom_sid *pfile_owner_sid,
2527 struct dom_sid *pfile_grp_sid,
2528 canon_ace **ppfile_ace,
2529 canon_ace **ppdir_ace,
2530 uint32 security_info_sent,
2531 const struct security_descriptor *psd)
2533 canon_ace *file_ace = NULL;
2534 canon_ace *dir_ace = NULL;
2536 *ppfile_ace = NULL;
2537 *ppdir_ace = NULL;
2539 if(security_info_sent == 0) {
2540 DEBUG(0,("unpack_canon_ace: no security info sent !\n"));
2541 return False;
2545 * If no DACL then this is a chown only security descriptor.
2548 if(!(security_info_sent & SECINFO_DACL) || !psd->dacl)
2549 return True;
2552 * Now go through the DACL and create the canon_ace lists.
2555 if (!create_canon_ace_lists( fsp, pst, pfile_owner_sid, pfile_grp_sid,
2556 &file_ace, &dir_ace, psd->dacl))
2557 return False;
2559 if ((file_ace == NULL) && (dir_ace == NULL)) {
2560 /* W2K traverse DACL set - ignore. */
2561 return True;
2565 * Go through the canon_ace list and merge entries
2566 * belonging to identical users of identical allow or deny type.
2567 * We can do this as all deny entries come first, followed by
2568 * all allow entries (we have mandated this before accepting this acl).
2571 print_canon_ace_list( "file ace - before merge", file_ace);
2572 merge_aces( &file_ace, false);
2574 print_canon_ace_list( "dir ace - before merge", dir_ace);
2575 merge_aces( &dir_ace, true);
2578 * NT ACLs are order dependent. Go through the acl lists and
2579 * process DENY entries by masking the allow entries.
2582 print_canon_ace_list( "file ace - before deny", file_ace);
2583 process_deny_list(fsp->conn, &file_ace);
2585 print_canon_ace_list( "dir ace - before deny", dir_ace);
2586 process_deny_list(fsp->conn, &dir_ace);
2589 * A well formed POSIX file or default ACL has at least 3 entries, a
2590 * SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ
2591 * and optionally a mask entry. Ensure this is the case.
2594 print_canon_ace_list( "file ace - before valid", file_ace);
2596 if (!ensure_canon_entry_valid(fsp->conn, &file_ace, false, fsp->conn->params,
2597 fsp->is_directory, pfile_owner_sid, pfile_grp_sid, pst, True)) {
2598 free_canon_ace_list(file_ace);
2599 free_canon_ace_list(dir_ace);
2600 return False;
2603 print_canon_ace_list( "dir ace - before valid", dir_ace);
2605 if (dir_ace && !ensure_canon_entry_valid(fsp->conn, &dir_ace, true, fsp->conn->params,
2606 fsp->is_directory, pfile_owner_sid, pfile_grp_sid, pst, True)) {
2607 free_canon_ace_list(file_ace);
2608 free_canon_ace_list(dir_ace);
2609 return False;
2612 print_canon_ace_list( "file ace - return", file_ace);
2613 print_canon_ace_list( "dir ace - return", dir_ace);
2615 *ppfile_ace = file_ace;
2616 *ppdir_ace = dir_ace;
2617 return True;
2621 /******************************************************************************
2622 When returning permissions, try and fit NT display
2623 semantics if possible. Note the the canon_entries here must have been malloced.
2624 The list format should be - first entry = owner, followed by group and other user
2625 entries, last entry = other.
2627 Note that this doesn't exactly match the NT semantics for an ACL. As POSIX entries
2628 are not ordered, and match on the most specific entry rather than walking a list,
2629 then a simple POSIX permission of rw-r--r-- should really map to 5 entries,
2631 Entry 0: owner : deny all except read and write.
2632 Entry 1: owner : allow read and write.
2633 Entry 2: group : deny all except read.
2634 Entry 3: group : allow read.
2635 Entry 4: Everyone : allow read.
2637 But NT cannot display this in their ACL editor !
2638 ********************************************************************************/
2640 static void arrange_posix_perms(const char *filename, canon_ace **pp_list_head)
2642 canon_ace *l_head = *pp_list_head;
2643 canon_ace *owner_ace = NULL;
2644 canon_ace *other_ace = NULL;
2645 canon_ace *ace = NULL;
2647 for (ace = l_head; ace; ace = ace->next) {
2648 if (ace->type == SMB_ACL_USER_OBJ)
2649 owner_ace = ace;
2650 else if (ace->type == SMB_ACL_OTHER) {
2651 /* Last ace - this is "other" */
2652 other_ace = ace;
2656 if (!owner_ace || !other_ace) {
2657 DEBUG(0,("arrange_posix_perms: Invalid POSIX permissions for file %s, missing owner or other.\n",
2658 filename ));
2659 return;
2663 * The POSIX algorithm applies to owner first, and other last,
2664 * so ensure they are arranged in this order.
2667 if (owner_ace) {
2668 DLIST_PROMOTE(l_head, owner_ace);
2671 if (other_ace) {
2672 DLIST_DEMOTE(l_head, other_ace, canon_ace *);
2675 /* We have probably changed the head of the list. */
2677 *pp_list_head = l_head;
2680 /****************************************************************************
2681 Create a linked list of canonical ACE entries.
2682 ****************************************************************************/
2684 static canon_ace *canonicalise_acl(struct connection_struct *conn,
2685 const char *fname, SMB_ACL_T posix_acl,
2686 const SMB_STRUCT_STAT *psbuf,
2687 const struct dom_sid *powner, const struct dom_sid *pgroup, struct pai_val *pal, SMB_ACL_TYPE_T the_acl_type)
2689 mode_t acl_mask = (S_IRUSR|S_IWUSR|S_IXUSR);
2690 canon_ace *l_head = NULL;
2691 canon_ace *ace = NULL;
2692 canon_ace *next_ace = NULL;
2693 int entry_id = SMB_ACL_FIRST_ENTRY;
2694 bool is_default_acl = (the_acl_type == SMB_ACL_TYPE_DEFAULT);
2695 SMB_ACL_ENTRY_T entry;
2696 size_t ace_count;
2698 while ( posix_acl && (sys_acl_get_entry(posix_acl, entry_id, &entry) == 1)) {
2699 SMB_ACL_TAG_T tagtype;
2700 SMB_ACL_PERMSET_T permset;
2701 struct dom_sid sid;
2702 struct unixid unix_ug;
2703 enum ace_owner owner_type;
2705 entry_id = SMB_ACL_NEXT_ENTRY;
2707 /* Is this a MASK entry ? */
2708 if (sys_acl_get_tag_type(entry, &tagtype) == -1)
2709 continue;
2711 if (sys_acl_get_permset(entry, &permset) == -1)
2712 continue;
2714 /* Decide which SID to use based on the ACL type. */
2715 switch(tagtype) {
2716 case SMB_ACL_USER_OBJ:
2717 /* Get the SID from the owner. */
2718 sid_copy(&sid, powner);
2719 unix_ug.type = ID_TYPE_UID;
2720 unix_ug.id = psbuf->st_ex_uid;
2721 owner_type = UID_ACE;
2722 break;
2723 case SMB_ACL_USER:
2725 uid_t *puid = (uid_t *)sys_acl_get_qualifier(entry);
2726 if (puid == NULL) {
2727 DEBUG(0,("canonicalise_acl: Failed to get uid.\n"));
2728 continue;
2730 uid_to_sid( &sid, *puid);
2731 unix_ug.type = ID_TYPE_UID;
2732 unix_ug.id = *puid;
2733 owner_type = UID_ACE;
2734 break;
2736 case SMB_ACL_GROUP_OBJ:
2737 /* Get the SID from the owning group. */
2738 sid_copy(&sid, pgroup);
2739 unix_ug.type = ID_TYPE_GID;
2740 unix_ug.id = psbuf->st_ex_gid;
2741 owner_type = GID_ACE;
2742 break;
2743 case SMB_ACL_GROUP:
2745 gid_t *pgid = (gid_t *)sys_acl_get_qualifier(entry);
2746 if (pgid == NULL) {
2747 DEBUG(0,("canonicalise_acl: Failed to get gid.\n"));
2748 continue;
2750 gid_to_sid( &sid, *pgid);
2751 unix_ug.type = ID_TYPE_GID;
2752 unix_ug.id = *pgid;
2753 owner_type = GID_ACE;
2754 break;
2756 case SMB_ACL_MASK:
2757 acl_mask = convert_permset_to_mode_t(permset);
2758 continue; /* Don't count the mask as an entry. */
2759 case SMB_ACL_OTHER:
2760 /* Use the Everyone SID */
2761 sid = global_sid_World;
2762 unix_ug.type = ID_TYPE_NOT_SPECIFIED;
2763 unix_ug.id = -1;
2764 owner_type = WORLD_ACE;
2765 break;
2766 default:
2767 DEBUG(0,("canonicalise_acl: Unknown tagtype %u\n", (unsigned int)tagtype));
2768 continue;
2772 * Add this entry to the list.
2775 if ((ace = talloc(talloc_tos(), canon_ace)) == NULL)
2776 goto fail;
2778 ZERO_STRUCTP(ace);
2779 ace->type = tagtype;
2780 ace->perms = convert_permset_to_mode_t(permset);
2781 ace->attr = ALLOW_ACE;
2782 ace->trustee = sid;
2783 ace->unix_ug = unix_ug;
2784 ace->owner_type = owner_type;
2785 ace->ace_flags = get_pai_flags(pal, ace, is_default_acl);
2787 DLIST_ADD(l_head, ace);
2791 * This next call will ensure we have at least a user/group/world set.
2794 if (!ensure_canon_entry_valid_on_get(conn, &l_head,
2795 powner, pgroup,
2796 psbuf))
2797 goto fail;
2800 * Now go through the list, masking the permissions with the
2801 * acl_mask. Ensure all DENY Entries are at the start of the list.
2804 DEBUG(10,("canonicalise_acl: %s ace entries before arrange :\n", is_default_acl ? "Default" : "Access"));
2806 for ( ace_count = 0, ace = l_head; ace; ace = next_ace, ace_count++) {
2807 next_ace = ace->next;
2809 /* Masks are only applied to entries other than USER_OBJ and OTHER. */
2810 if (ace->type != SMB_ACL_OTHER && ace->type != SMB_ACL_USER_OBJ)
2811 ace->perms &= acl_mask;
2813 if (ace->perms == 0) {
2814 DLIST_PROMOTE(l_head, ace);
2817 if( DEBUGLVL( 10 ) ) {
2818 print_canon_ace(ace, ace_count);
2822 arrange_posix_perms(fname,&l_head );
2824 print_canon_ace_list( "canonicalise_acl: ace entries after arrange", l_head );
2826 return l_head;
2828 fail:
2830 free_canon_ace_list(l_head);
2831 return NULL;
2834 /****************************************************************************
2835 Check if the current user group list contains a given group.
2836 ****************************************************************************/
2838 bool current_user_in_group(connection_struct *conn, gid_t gid)
2840 int i;
2841 const struct security_unix_token *utok = get_current_utok(conn);
2843 for (i = 0; i < utok->ngroups; i++) {
2844 if (utok->groups[i] == gid) {
2845 return True;
2849 return False;
2852 /****************************************************************************
2853 Should we override a deny ? Check 'acl group control' and 'dos filemode'.
2854 ****************************************************************************/
2856 static bool acl_group_override(connection_struct *conn,
2857 const struct smb_filename *smb_fname)
2859 if ((errno != EPERM) && (errno != EACCES)) {
2860 return false;
2863 /* file primary group == user primary or supplementary group */
2864 if (lp_acl_group_control(SNUM(conn)) &&
2865 current_user_in_group(conn, smb_fname->st.st_ex_gid)) {
2866 return true;
2869 /* user has writeable permission */
2870 if (lp_dos_filemode(SNUM(conn)) &&
2871 can_write_to_file(conn, smb_fname)) {
2872 return true;
2875 return false;
2878 /****************************************************************************
2879 Attempt to apply an ACL to a file or directory.
2880 ****************************************************************************/
2882 static bool set_canon_ace_list(files_struct *fsp,
2883 canon_ace *the_ace,
2884 bool default_ace,
2885 const SMB_STRUCT_STAT *psbuf,
2886 bool *pacl_set_support)
2888 connection_struct *conn = fsp->conn;
2889 bool ret = False;
2890 SMB_ACL_T the_acl = sys_acl_init(talloc_tos());
2891 canon_ace *p_ace;
2892 int i;
2893 SMB_ACL_ENTRY_T mask_entry;
2894 bool got_mask_entry = False;
2895 SMB_ACL_PERMSET_T mask_permset;
2896 SMB_ACL_TYPE_T the_acl_type = (default_ace ? SMB_ACL_TYPE_DEFAULT : SMB_ACL_TYPE_ACCESS);
2897 bool needs_mask = False;
2898 mode_t mask_perms = 0;
2900 /* Use the psbuf that was passed in. */
2901 if (psbuf != &fsp->fsp_name->st) {
2902 fsp->fsp_name->st = *psbuf;
2905 #if defined(POSIX_ACL_NEEDS_MASK)
2906 /* HP-UX always wants to have a mask (called "class" there). */
2907 needs_mask = True;
2908 #endif
2910 if (the_acl == NULL) {
2911 DEBUG(0, ("sys_acl_init failed to allocate an ACL\n"));
2912 return false;
2915 if( DEBUGLVL( 10 )) {
2916 dbgtext("set_canon_ace_list: setting ACL:\n");
2917 for (i = 0, p_ace = the_ace; p_ace; p_ace = p_ace->next, i++ ) {
2918 print_canon_ace( p_ace, i);
2922 for (i = 0, p_ace = the_ace; p_ace; p_ace = p_ace->next, i++ ) {
2923 SMB_ACL_ENTRY_T the_entry;
2924 SMB_ACL_PERMSET_T the_permset;
2927 * ACLs only "need" an ACL_MASK entry if there are any named user or
2928 * named group entries. But if there is an ACL_MASK entry, it applies
2929 * to ACL_USER, ACL_GROUP, and ACL_GROUP_OBJ entries. Set the mask
2930 * so that it doesn't deny (i.e., mask off) any permissions.
2933 if (p_ace->type == SMB_ACL_USER || p_ace->type == SMB_ACL_GROUP) {
2934 needs_mask = True;
2935 mask_perms |= p_ace->perms;
2936 } else if (p_ace->type == SMB_ACL_GROUP_OBJ) {
2937 mask_perms |= p_ace->perms;
2941 * Get the entry for this ACE.
2944 if (sys_acl_create_entry(&the_acl, &the_entry) == -1) {
2945 DEBUG(0,("set_canon_ace_list: Failed to create entry %d. (%s)\n",
2946 i, strerror(errno) ));
2947 goto fail;
2950 if (p_ace->type == SMB_ACL_MASK) {
2951 mask_entry = the_entry;
2952 got_mask_entry = True;
2956 * Ok - we now know the ACL calls should be working, don't
2957 * allow fallback to chmod.
2960 *pacl_set_support = True;
2963 * Initialise the entry from the canon_ace.
2967 * First tell the entry what type of ACE this is.
2970 if (sys_acl_set_tag_type(the_entry, p_ace->type) == -1) {
2971 DEBUG(0,("set_canon_ace_list: Failed to set tag type on entry %d. (%s)\n",
2972 i, strerror(errno) ));
2973 goto fail;
2977 * Only set the qualifier (user or group id) if the entry is a user
2978 * or group id ACE.
2981 if ((p_ace->type == SMB_ACL_USER) || (p_ace->type == SMB_ACL_GROUP)) {
2982 if (sys_acl_set_qualifier(the_entry,(void *)&p_ace->unix_ug.id) == -1) {
2983 DEBUG(0,("set_canon_ace_list: Failed to set qualifier on entry %d. (%s)\n",
2984 i, strerror(errno) ));
2985 goto fail;
2990 * Convert the mode_t perms in the canon_ace to a POSIX permset.
2993 if (sys_acl_get_permset(the_entry, &the_permset) == -1) {
2994 DEBUG(0,("set_canon_ace_list: Failed to get permset on entry %d. (%s)\n",
2995 i, strerror(errno) ));
2996 goto fail;
2999 if (map_acl_perms_to_permset(conn, p_ace->perms, &the_permset) == -1) {
3000 DEBUG(0,("set_canon_ace_list: Failed to create permset for mode (%u) on entry %d. (%s)\n",
3001 (unsigned int)p_ace->perms, i, strerror(errno) ));
3002 goto fail;
3006 * ..and apply them to the entry.
3009 if (sys_acl_set_permset(the_entry, the_permset) == -1) {
3010 DEBUG(0,("set_canon_ace_list: Failed to add permset on entry %d. (%s)\n",
3011 i, strerror(errno) ));
3012 goto fail;
3015 if( DEBUGLVL( 10 ))
3016 print_canon_ace( p_ace, i);
3020 if (needs_mask && !got_mask_entry) {
3021 if (sys_acl_create_entry(&the_acl, &mask_entry) == -1) {
3022 DEBUG(0,("set_canon_ace_list: Failed to create mask entry. (%s)\n", strerror(errno) ));
3023 goto fail;
3026 if (sys_acl_set_tag_type(mask_entry, SMB_ACL_MASK) == -1) {
3027 DEBUG(0,("set_canon_ace_list: Failed to set tag type on mask entry. (%s)\n",strerror(errno) ));
3028 goto fail;
3031 if (sys_acl_get_permset(mask_entry, &mask_permset) == -1) {
3032 DEBUG(0,("set_canon_ace_list: Failed to get mask permset. (%s)\n", strerror(errno) ));
3033 goto fail;
3036 if (map_acl_perms_to_permset(conn, S_IRUSR|S_IWUSR|S_IXUSR, &mask_permset) == -1) {
3037 DEBUG(0,("set_canon_ace_list: Failed to create mask permset. (%s)\n", strerror(errno) ));
3038 goto fail;
3041 if (sys_acl_set_permset(mask_entry, mask_permset) == -1) {
3042 DEBUG(0,("set_canon_ace_list: Failed to add mask permset. (%s)\n", strerror(errno) ));
3043 goto fail;
3048 * Finally apply it to the file or directory.
3051 if(default_ace || fsp->is_directory || fsp->fh->fd == -1) {
3052 if (SMB_VFS_SYS_ACL_SET_FILE(conn, fsp->fsp_name->base_name,
3053 the_acl_type, the_acl) == -1) {
3055 * Some systems allow all the above calls and only fail with no ACL support
3056 * when attempting to apply the acl. HPUX with HFS is an example of this. JRA.
3058 if (no_acl_syscall_error(errno)) {
3059 *pacl_set_support = False;
3062 if (acl_group_override(conn, fsp->fsp_name)) {
3063 int sret;
3065 DEBUG(5,("set_canon_ace_list: acl group "
3066 "control on and current user in file "
3067 "%s primary group.\n",
3068 fsp_str_dbg(fsp)));
3070 become_root();
3071 sret = SMB_VFS_SYS_ACL_SET_FILE(conn,
3072 fsp->fsp_name->base_name, the_acl_type,
3073 the_acl);
3074 unbecome_root();
3075 if (sret == 0) {
3076 ret = True;
3080 if (ret == False) {
3081 DEBUG(2,("set_canon_ace_list: "
3082 "sys_acl_set_file type %s failed for "
3083 "file %s (%s).\n",
3084 the_acl_type == SMB_ACL_TYPE_DEFAULT ?
3085 "directory default" : "file",
3086 fsp_str_dbg(fsp), strerror(errno)));
3087 goto fail;
3090 } else {
3091 if (SMB_VFS_SYS_ACL_SET_FD(fsp, the_acl) == -1) {
3093 * Some systems allow all the above calls and only fail with no ACL support
3094 * when attempting to apply the acl. HPUX with HFS is an example of this. JRA.
3096 if (no_acl_syscall_error(errno)) {
3097 *pacl_set_support = False;
3100 if (acl_group_override(conn, fsp->fsp_name)) {
3101 int sret;
3103 DEBUG(5,("set_canon_ace_list: acl group "
3104 "control on and current user in file "
3105 "%s primary group.\n",
3106 fsp_str_dbg(fsp)));
3108 become_root();
3109 sret = SMB_VFS_SYS_ACL_SET_FD(fsp, the_acl);
3110 unbecome_root();
3111 if (sret == 0) {
3112 ret = True;
3116 if (ret == False) {
3117 DEBUG(2,("set_canon_ace_list: "
3118 "sys_acl_set_file failed for file %s "
3119 "(%s).\n",
3120 fsp_str_dbg(fsp), strerror(errno)));
3121 goto fail;
3126 ret = True;
3128 fail:
3130 if (the_acl != NULL) {
3131 TALLOC_FREE(the_acl);
3134 return ret;
3137 /****************************************************************************
3139 ****************************************************************************/
3141 SMB_ACL_T free_empty_sys_acl(connection_struct *conn, SMB_ACL_T the_acl)
3143 SMB_ACL_ENTRY_T entry;
3145 if (!the_acl)
3146 return NULL;
3147 if (sys_acl_get_entry(the_acl, SMB_ACL_FIRST_ENTRY, &entry) != 1) {
3148 TALLOC_FREE(the_acl);
3149 return NULL;
3151 return the_acl;
3154 /****************************************************************************
3155 Convert a canon_ace to a generic 3 element permission - if possible.
3156 ****************************************************************************/
3158 #define MAP_PERM(p,mask,result) (((p) & (mask)) ? (result) : 0 )
3160 static bool convert_canon_ace_to_posix_perms( files_struct *fsp, canon_ace *file_ace_list, mode_t *posix_perms)
3162 int snum = SNUM(fsp->conn);
3163 size_t ace_count = count_canon_ace_list(file_ace_list);
3164 canon_ace *ace_p;
3165 canon_ace *owner_ace = NULL;
3166 canon_ace *group_ace = NULL;
3167 canon_ace *other_ace = NULL;
3168 mode_t and_bits;
3169 mode_t or_bits;
3171 if (ace_count != 3) {
3172 DEBUG(3,("convert_canon_ace_to_posix_perms: Too many ACE "
3173 "entries for file %s to convert to posix perms.\n",
3174 fsp_str_dbg(fsp)));
3175 return False;
3178 for (ace_p = file_ace_list; ace_p; ace_p = ace_p->next) {
3179 if (ace_p->owner_type == UID_ACE)
3180 owner_ace = ace_p;
3181 else if (ace_p->owner_type == GID_ACE)
3182 group_ace = ace_p;
3183 else if (ace_p->owner_type == WORLD_ACE)
3184 other_ace = ace_p;
3187 if (!owner_ace || !group_ace || !other_ace) {
3188 DEBUG(3,("convert_canon_ace_to_posix_perms: Can't get "
3189 "standard entries for file %s.\n", fsp_str_dbg(fsp)));
3190 return False;
3193 *posix_perms = (mode_t)0;
3195 *posix_perms |= owner_ace->perms;
3196 *posix_perms |= MAP_PERM(group_ace->perms, S_IRUSR, S_IRGRP);
3197 *posix_perms |= MAP_PERM(group_ace->perms, S_IWUSR, S_IWGRP);
3198 *posix_perms |= MAP_PERM(group_ace->perms, S_IXUSR, S_IXGRP);
3199 *posix_perms |= MAP_PERM(other_ace->perms, S_IRUSR, S_IROTH);
3200 *posix_perms |= MAP_PERM(other_ace->perms, S_IWUSR, S_IWOTH);
3201 *posix_perms |= MAP_PERM(other_ace->perms, S_IXUSR, S_IXOTH);
3203 /* The owner must have at least read access. */
3205 *posix_perms |= S_IRUSR;
3206 if (fsp->is_directory)
3207 *posix_perms |= (S_IWUSR|S_IXUSR);
3209 /* If requested apply the masks. */
3211 /* Get the initial bits to apply. */
3213 if (fsp->is_directory) {
3214 and_bits = lp_dir_mask(snum);
3215 or_bits = lp_force_dir_mode(snum);
3216 } else {
3217 and_bits = lp_create_mask(snum);
3218 or_bits = lp_force_create_mode(snum);
3221 *posix_perms = (((*posix_perms) & and_bits)|or_bits);
3223 DEBUG(10,("convert_canon_ace_to_posix_perms: converted u=%o,g=%o,w=%o "
3224 "to perm=0%o for file %s.\n", (int)owner_ace->perms,
3225 (int)group_ace->perms, (int)other_ace->perms,
3226 (int)*posix_perms, fsp_str_dbg(fsp)));
3228 return True;
3231 /****************************************************************************
3232 Incoming NT ACLs on a directory can be split into a default POSIX acl (CI|OI|IO) and
3233 a normal POSIX acl. Win2k needs these split acls re-merging into one ACL
3234 with CI|OI set so it is inherited and also applies to the directory.
3235 Based on code from "Jim McDonough" <jmcd@us.ibm.com>.
3236 ****************************************************************************/
3238 static size_t merge_default_aces( struct security_ace *nt_ace_list, size_t num_aces)
3240 size_t i, j;
3242 for (i = 0; i < num_aces; i++) {
3243 for (j = i+1; j < num_aces; j++) {
3244 uint32 i_flags_ni = (nt_ace_list[i].flags & ~SEC_ACE_FLAG_INHERITED_ACE);
3245 uint32 j_flags_ni = (nt_ace_list[j].flags & ~SEC_ACE_FLAG_INHERITED_ACE);
3246 bool i_inh = (nt_ace_list[i].flags & SEC_ACE_FLAG_INHERITED_ACE) ? True : False;
3247 bool j_inh = (nt_ace_list[j].flags & SEC_ACE_FLAG_INHERITED_ACE) ? True : False;
3249 /* We know the lower number ACE's are file entries. */
3250 if ((nt_ace_list[i].type == nt_ace_list[j].type) &&
3251 (nt_ace_list[i].size == nt_ace_list[j].size) &&
3252 (nt_ace_list[i].access_mask == nt_ace_list[j].access_mask) &&
3253 dom_sid_equal(&nt_ace_list[i].trustee, &nt_ace_list[j].trustee) &&
3254 (i_inh == j_inh) &&
3255 (i_flags_ni == 0) &&
3256 (j_flags_ni == (SEC_ACE_FLAG_OBJECT_INHERIT|
3257 SEC_ACE_FLAG_CONTAINER_INHERIT|
3258 SEC_ACE_FLAG_INHERIT_ONLY))) {
3260 * W2K wants to have access allowed zero access ACE's
3261 * at the end of the list. If the mask is zero, merge
3262 * the non-inherited ACE onto the inherited ACE.
3265 if (nt_ace_list[i].access_mask == 0) {
3266 nt_ace_list[j].flags = SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT|
3267 (i_inh ? SEC_ACE_FLAG_INHERITED_ACE : 0);
3268 if (num_aces - i - 1 > 0)
3269 memmove(&nt_ace_list[i], &nt_ace_list[i+1], (num_aces-i-1) *
3270 sizeof(struct security_ace));
3272 DEBUG(10,("merge_default_aces: Merging zero access ACE %u onto ACE %u.\n",
3273 (unsigned int)i, (unsigned int)j ));
3274 } else {
3276 * These are identical except for the flags.
3277 * Merge the inherited ACE onto the non-inherited ACE.
3280 nt_ace_list[i].flags = SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT|
3281 (i_inh ? SEC_ACE_FLAG_INHERITED_ACE : 0);
3282 if (num_aces - j - 1 > 0)
3283 memmove(&nt_ace_list[j], &nt_ace_list[j+1], (num_aces-j-1) *
3284 sizeof(struct security_ace));
3286 DEBUG(10,("merge_default_aces: Merging ACE %u onto ACE %u.\n",
3287 (unsigned int)j, (unsigned int)i ));
3289 num_aces--;
3290 break;
3295 return num_aces;
3299 * Add or Replace ACE entry.
3300 * In some cases we need to add a specific ACE for compatibility reasons.
3301 * When doing that we must make sure we are not actually creating a duplicate
3302 * entry. So we need to search whether an ACE entry already exist and eventually
3303 * replacce the access mask, or add a completely new entry if none was found.
3305 * This function assumes the array has enough space to add a new entry without
3306 * any reallocation of memory.
3309 static void add_or_replace_ace(struct security_ace *nt_ace_list, size_t *num_aces,
3310 const struct dom_sid *sid, enum security_ace_type type,
3311 uint32_t mask, uint8_t flags)
3313 int i;
3315 /* first search for a duplicate */
3316 for (i = 0; i < *num_aces; i++) {
3317 if (dom_sid_equal(&nt_ace_list[i].trustee, sid) &&
3318 (nt_ace_list[i].flags == flags)) break;
3321 if (i < *num_aces) { /* found */
3322 nt_ace_list[i].type = type;
3323 nt_ace_list[i].access_mask = mask;
3324 DEBUG(10, ("Replacing ACE %d with SID %s and flags %02x\n",
3325 i, sid_string_dbg(sid), flags));
3326 return;
3329 /* not found, append it */
3330 init_sec_ace(&nt_ace_list[(*num_aces)++], sid, type, mask, flags);
3334 /****************************************************************************
3335 Reply to query a security descriptor from an fsp. If it succeeds it allocates
3336 the space for the return elements and returns the size needed to return the
3337 security descriptor. This should be the only external function needed for
3338 the UNIX style get ACL.
3339 ****************************************************************************/
3341 static NTSTATUS posix_get_nt_acl_common(struct connection_struct *conn,
3342 const char *name,
3343 const SMB_STRUCT_STAT *sbuf,
3344 struct pai_val *pal,
3345 SMB_ACL_T posix_acl,
3346 SMB_ACL_T def_acl,
3347 uint32_t security_info,
3348 TALLOC_CTX *mem_ctx,
3349 struct security_descriptor **ppdesc)
3351 struct dom_sid owner_sid;
3352 struct dom_sid group_sid;
3353 size_t sd_size = 0;
3354 struct security_acl *psa = NULL;
3355 size_t num_acls = 0;
3356 size_t num_def_acls = 0;
3357 size_t num_aces = 0;
3358 canon_ace *file_ace = NULL;
3359 canon_ace *dir_ace = NULL;
3360 struct security_ace *nt_ace_list = NULL;
3361 size_t num_profile_acls = 0;
3362 struct dom_sid orig_owner_sid;
3363 struct security_descriptor *psd = NULL;
3364 int i;
3367 * Get the owner, group and world SIDs.
3370 create_file_sids(sbuf, &owner_sid, &group_sid);
3372 if (lp_profile_acls(SNUM(conn))) {
3373 /* For WXP SP1 the owner must be administrators. */
3374 sid_copy(&orig_owner_sid, &owner_sid);
3375 sid_copy(&owner_sid, &global_sid_Builtin_Administrators);
3376 sid_copy(&group_sid, &global_sid_Builtin_Users);
3377 num_profile_acls = 3;
3380 if ((security_info & SECINFO_DACL) && !(security_info & SECINFO_PROTECTED_DACL)) {
3383 * In the optimum case Creator Owner and Creator Group would be used for
3384 * the ACL_USER_OBJ and ACL_GROUP_OBJ entries, respectively, but this
3385 * would lead to usability problems under Windows: The Creator entries
3386 * are only available in browse lists of directories and not for files;
3387 * additionally the identity of the owning group couldn't be determined.
3388 * We therefore use those identities only for Default ACLs.
3391 /* Create the canon_ace lists. */
3392 file_ace = canonicalise_acl(conn, name, posix_acl, sbuf,
3393 &owner_sid, &group_sid, pal,
3394 SMB_ACL_TYPE_ACCESS);
3396 /* We must have *some* ACLS. */
3398 if (count_canon_ace_list(file_ace) == 0) {
3399 DEBUG(0,("get_nt_acl : No ACLs on file (%s) !\n", name));
3400 goto done;
3403 if (S_ISDIR(sbuf->st_ex_mode) && def_acl) {
3404 dir_ace = canonicalise_acl(conn, name, def_acl,
3405 sbuf,
3406 &global_sid_Creator_Owner,
3407 &global_sid_Creator_Group,
3408 pal, SMB_ACL_TYPE_DEFAULT);
3412 * Create the NT ACE list from the canonical ace lists.
3416 canon_ace *ace;
3417 enum security_ace_type nt_acl_type;
3419 num_acls = count_canon_ace_list(file_ace);
3420 num_def_acls = count_canon_ace_list(dir_ace);
3422 /* Allocate the ace list. */
3423 if ((nt_ace_list = talloc_array(talloc_tos(), struct security_ace,num_acls + num_profile_acls + num_def_acls)) == NULL) {
3424 DEBUG(0,("get_nt_acl: Unable to malloc space for nt_ace_list.\n"));
3425 goto done;
3428 memset(nt_ace_list, '\0', (num_acls + num_def_acls) * sizeof(struct security_ace) );
3431 * Create the NT ACE list from the canonical ace lists.
3434 for (ace = file_ace; ace != NULL; ace = ace->next) {
3435 uint32_t acc = map_canon_ace_perms(SNUM(conn),
3436 &nt_acl_type,
3437 ace->perms,
3438 S_ISDIR(sbuf->st_ex_mode));
3439 init_sec_ace(&nt_ace_list[num_aces++],
3440 &ace->trustee,
3441 nt_acl_type,
3442 acc,
3443 ace->ace_flags);
3446 /* The User must have access to a profile share - even
3447 * if we can't map the SID. */
3448 if (lp_profile_acls(SNUM(conn))) {
3449 add_or_replace_ace(nt_ace_list, &num_aces,
3450 &global_sid_Builtin_Users,
3451 SEC_ACE_TYPE_ACCESS_ALLOWED,
3452 FILE_GENERIC_ALL, 0);
3455 for (ace = dir_ace; ace != NULL; ace = ace->next) {
3456 uint32_t acc = map_canon_ace_perms(SNUM(conn),
3457 &nt_acl_type,
3458 ace->perms,
3459 S_ISDIR(sbuf->st_ex_mode));
3460 init_sec_ace(&nt_ace_list[num_aces++],
3461 &ace->trustee,
3462 nt_acl_type,
3463 acc,
3464 ace->ace_flags |
3465 SEC_ACE_FLAG_OBJECT_INHERIT|
3466 SEC_ACE_FLAG_CONTAINER_INHERIT|
3467 SEC_ACE_FLAG_INHERIT_ONLY);
3470 /* The User must have access to a profile share - even
3471 * if we can't map the SID. */
3472 if (lp_profile_acls(SNUM(conn))) {
3473 add_or_replace_ace(nt_ace_list, &num_aces,
3474 &global_sid_Builtin_Users,
3475 SEC_ACE_TYPE_ACCESS_ALLOWED,
3476 FILE_GENERIC_ALL,
3477 SEC_ACE_FLAG_OBJECT_INHERIT |
3478 SEC_ACE_FLAG_CONTAINER_INHERIT |
3479 SEC_ACE_FLAG_INHERIT_ONLY);
3483 * Merge POSIX default ACLs and normal ACLs into one NT ACE.
3484 * Win2K needs this to get the inheritance correct when replacing ACLs
3485 * on a directory tree. Based on work by Jim @ IBM.
3488 num_aces = merge_default_aces(nt_ace_list, num_aces);
3490 if (lp_profile_acls(SNUM(conn))) {
3491 for (i = 0; i < num_aces; i++) {
3492 if (dom_sid_equal(&nt_ace_list[i].trustee, &owner_sid)) {
3493 add_or_replace_ace(nt_ace_list, &num_aces,
3494 &orig_owner_sid,
3495 nt_ace_list[i].type,
3496 nt_ace_list[i].access_mask,
3497 nt_ace_list[i].flags);
3498 break;
3504 if (num_aces) {
3505 if((psa = make_sec_acl( talloc_tos(), NT4_ACL_REVISION, num_aces, nt_ace_list)) == NULL) {
3506 DEBUG(0,("get_nt_acl: Unable to malloc space for acl.\n"));
3507 goto done;
3510 } /* security_info & SECINFO_DACL */
3512 psd = make_standard_sec_desc(mem_ctx,
3513 (security_info & SECINFO_OWNER) ? &owner_sid : NULL,
3514 (security_info & SECINFO_GROUP) ? &group_sid : NULL,
3515 psa,
3516 &sd_size);
3518 if(!psd) {
3519 DEBUG(0,("get_nt_acl: Unable to malloc space for security descriptor.\n"));
3520 sd_size = 0;
3521 goto done;
3525 * Windows 2000: The DACL_PROTECTED flag in the security
3526 * descriptor marks the ACL as non-inheriting, i.e., no
3527 * ACEs from higher level directories propagate to this
3528 * ACL. In the POSIX ACL model permissions are only
3529 * inherited at file create time, so ACLs never contain
3530 * any ACEs that are inherited dynamically. The DACL_PROTECTED
3531 * flag doesn't seem to bother Windows NT.
3532 * Always set this if map acl inherit is turned off.
3534 if (pal == NULL || !lp_map_acl_inherit(SNUM(conn))) {
3535 psd->type |= SEC_DESC_DACL_PROTECTED;
3536 } else {
3537 psd->type |= pal->sd_type;
3540 if (psd->dacl) {
3541 dacl_sort_into_canonical_order(psd->dacl->aces, (unsigned int)psd->dacl->num_aces);
3544 *ppdesc = psd;
3546 done:
3548 if (posix_acl) {
3549 TALLOC_FREE(posix_acl);
3551 if (def_acl) {
3552 TALLOC_FREE(def_acl);
3554 free_canon_ace_list(file_ace);
3555 free_canon_ace_list(dir_ace);
3556 free_inherited_info(pal);
3557 TALLOC_FREE(nt_ace_list);
3559 return NT_STATUS_OK;
3562 NTSTATUS posix_fget_nt_acl(struct files_struct *fsp, uint32_t security_info,
3563 TALLOC_CTX *mem_ctx,
3564 struct security_descriptor **ppdesc)
3566 SMB_STRUCT_STAT sbuf;
3567 SMB_ACL_T posix_acl = NULL;
3568 struct pai_val *pal;
3569 TALLOC_CTX *frame = talloc_stackframe();
3570 NTSTATUS status;
3572 *ppdesc = NULL;
3574 DEBUG(10,("posix_fget_nt_acl: called for file %s\n",
3575 fsp_str_dbg(fsp)));
3577 /* can it happen that fsp_name == NULL ? */
3578 if (fsp->is_directory || fsp->fh->fd == -1) {
3579 status = posix_get_nt_acl(fsp->conn, fsp->fsp_name->base_name,
3580 security_info, mem_ctx, ppdesc);
3581 TALLOC_FREE(frame);
3582 return status;
3585 /* Get the stat struct for the owner info. */
3586 if(SMB_VFS_FSTAT(fsp, &sbuf) != 0) {
3587 TALLOC_FREE(frame);
3588 return map_nt_error_from_unix(errno);
3591 /* Get the ACL from the fd. */
3592 posix_acl = SMB_VFS_SYS_ACL_GET_FD(fsp, frame);
3594 pal = fload_inherited_info(fsp);
3596 status = posix_get_nt_acl_common(fsp->conn, fsp->fsp_name->base_name,
3597 &sbuf, pal, posix_acl, NULL,
3598 security_info, mem_ctx, ppdesc);
3599 TALLOC_FREE(frame);
3600 return status;
3603 NTSTATUS posix_get_nt_acl(struct connection_struct *conn, const char *name,
3604 uint32_t security_info,
3605 TALLOC_CTX *mem_ctx,
3606 struct security_descriptor **ppdesc)
3608 SMB_ACL_T posix_acl = NULL;
3609 SMB_ACL_T def_acl = NULL;
3610 struct pai_val *pal;
3611 struct smb_filename smb_fname;
3612 int ret;
3613 TALLOC_CTX *frame = talloc_stackframe();
3614 NTSTATUS status;
3616 *ppdesc = NULL;
3618 DEBUG(10,("posix_get_nt_acl: called for file %s\n", name ));
3620 ZERO_STRUCT(smb_fname);
3621 smb_fname.base_name = discard_const_p(char, name);
3623 /* Get the stat struct for the owner info. */
3624 if (lp_posix_pathnames()) {
3625 ret = SMB_VFS_LSTAT(conn, &smb_fname);
3626 } else {
3627 ret = SMB_VFS_STAT(conn, &smb_fname);
3630 if (ret == -1) {
3631 TALLOC_FREE(frame);
3632 return map_nt_error_from_unix(errno);
3635 /* Get the ACL from the path. */
3636 posix_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, name,
3637 SMB_ACL_TYPE_ACCESS, frame);
3639 /* If it's a directory get the default POSIX ACL. */
3640 if(S_ISDIR(smb_fname.st.st_ex_mode)) {
3641 def_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, name,
3642 SMB_ACL_TYPE_DEFAULT, frame);
3643 def_acl = free_empty_sys_acl(conn, def_acl);
3646 pal = load_inherited_info(conn, name);
3648 status = posix_get_nt_acl_common(conn, name, &smb_fname.st, pal,
3649 posix_acl, def_acl, security_info,
3650 mem_ctx,
3651 ppdesc);
3652 TALLOC_FREE(frame);
3653 return status;
3656 /****************************************************************************
3657 Try to chown a file. We will be able to chown it under the following conditions.
3659 1) If we have root privileges, then it will just work.
3660 2) If we have SeRestorePrivilege we can change the user + group to any other user.
3661 3) If we have SeTakeOwnershipPrivilege we can change the user to the current user.
3662 4) If we have write permission to the file and dos_filemodes is set
3663 then allow chown to the currently authenticated user.
3664 ****************************************************************************/
3666 NTSTATUS try_chown(files_struct *fsp, uid_t uid, gid_t gid)
3668 NTSTATUS status;
3670 if(!CAN_WRITE(fsp->conn)) {
3671 return NT_STATUS_MEDIA_WRITE_PROTECTED;
3674 /* Case (1). */
3675 status = vfs_chown_fsp(fsp, uid, gid);
3676 if (NT_STATUS_IS_OK(status)) {
3677 return status;
3680 /* Case (2) / (3) */
3681 if (lp_enable_privileges()) {
3682 bool has_take_ownership_priv = security_token_has_privilege(
3683 get_current_nttok(fsp->conn),
3684 SEC_PRIV_TAKE_OWNERSHIP);
3685 bool has_restore_priv = security_token_has_privilege(
3686 get_current_nttok(fsp->conn),
3687 SEC_PRIV_RESTORE);
3689 if (has_restore_priv) {
3690 ; /* Case (2) */
3691 } else if (has_take_ownership_priv) {
3692 /* Case (3) */
3693 if (uid == get_current_uid(fsp->conn)) {
3694 gid = (gid_t)-1;
3695 } else {
3696 has_take_ownership_priv = false;
3700 if (has_take_ownership_priv || has_restore_priv) {
3701 become_root();
3702 status = vfs_chown_fsp(fsp, uid, gid);
3703 unbecome_root();
3704 return status;
3708 /* Case (4). */
3709 if (!lp_dos_filemode(SNUM(fsp->conn))) {
3710 return NT_STATUS_ACCESS_DENIED;
3713 /* only allow chown to the current user. This is more secure,
3714 and also copes with the case where the SID in a take ownership ACL is
3715 a local SID on the users workstation
3717 if (uid != get_current_uid(fsp->conn)) {
3718 return NT_STATUS_ACCESS_DENIED;
3721 become_root();
3722 /* Keep the current file gid the same. */
3723 status = vfs_chown_fsp(fsp, uid, (gid_t)-1);
3724 unbecome_root();
3726 return status;
3729 #if 0
3730 /* Disable this - prevents ACL inheritance from the ACL editor. JRA. */
3732 /****************************************************************************
3733 Take care of parent ACL inheritance.
3734 ****************************************************************************/
3736 NTSTATUS append_parent_acl(files_struct *fsp,
3737 const struct security_descriptor *pcsd,
3738 struct security_descriptor **pp_new_sd)
3740 struct smb_filename *smb_dname = NULL;
3741 struct security_descriptor *parent_sd = NULL;
3742 files_struct *parent_fsp = NULL;
3743 TALLOC_CTX *mem_ctx = talloc_tos();
3744 char *parent_name = NULL;
3745 struct security_ace *new_ace = NULL;
3746 unsigned int num_aces = pcsd->dacl->num_aces;
3747 NTSTATUS status;
3748 int info;
3749 unsigned int i, j;
3750 struct security_descriptor *psd = dup_sec_desc(talloc_tos(), pcsd);
3751 bool is_dacl_protected = (pcsd->type & SEC_DESC_DACL_PROTECTED);
3753 if (psd == NULL) {
3754 return NT_STATUS_NO_MEMORY;
3757 if (!parent_dirname(mem_ctx, fsp->fsp_name->base_name, &parent_name,
3758 NULL)) {
3759 return NT_STATUS_NO_MEMORY;
3762 status = create_synthetic_smb_fname(mem_ctx, parent_name, NULL, NULL,
3763 &smb_dname);
3764 if (!NT_STATUS_IS_OK(status)) {
3765 goto fail;
3768 status = SMB_VFS_CREATE_FILE(
3769 fsp->conn, /* conn */
3770 NULL, /* req */
3771 0, /* root_dir_fid */
3772 smb_dname, /* fname */
3773 FILE_READ_ATTRIBUTES, /* access_mask */
3774 FILE_SHARE_NONE, /* share_access */
3775 FILE_OPEN, /* create_disposition*/
3776 FILE_DIRECTORY_FILE, /* create_options */
3777 0, /* file_attributes */
3778 INTERNAL_OPEN_ONLY, /* oplock_request */
3779 0, /* allocation_size */
3780 NULL, /* sd */
3781 NULL, /* ea_list */
3782 &parent_fsp, /* result */
3783 &info); /* pinfo */
3785 if (!NT_STATUS_IS_OK(status)) {
3786 TALLOC_FREE(smb_dname);
3787 return status;
3790 status = SMB_VFS_GET_NT_ACL(parent_fsp->conn, smb_dname->base_name,
3791 SECINFO_DACL, &parent_sd );
3793 close_file(NULL, parent_fsp, NORMAL_CLOSE);
3794 TALLOC_FREE(smb_dname);
3796 if (!NT_STATUS_IS_OK(status)) {
3797 return status;
3801 * Make room for potentially all the ACLs from
3802 * the parent. We used to add the ugw triple here,
3803 * as we knew we were dealing with POSIX ACLs.
3804 * We no longer need to do so as we can guarentee
3805 * that a default ACL from the parent directory will
3806 * be well formed for POSIX ACLs if it came from a
3807 * POSIX ACL source, and if we're not writing to a
3808 * POSIX ACL sink then we don't care if it's not well
3809 * formed. JRA.
3812 num_aces += parent_sd->dacl->num_aces;
3814 if((new_ace = talloc_zero_array(mem_ctx, struct security_ace,
3815 num_aces)) == NULL) {
3816 return NT_STATUS_NO_MEMORY;
3819 /* Start by copying in all the given ACE entries. */
3820 for (i = 0; i < psd->dacl->num_aces; i++) {
3821 sec_ace_copy(&new_ace[i], &psd->dacl->aces[i]);
3825 * Note that we're ignoring "inherit permissions" here
3826 * as that really only applies to newly created files. JRA.
3829 /* Finally append any inherited ACEs. */
3830 for (j = 0; j < parent_sd->dacl->num_aces; j++) {
3831 struct security_ace *se = &parent_sd->dacl->aces[j];
3833 if (fsp->is_directory) {
3834 if (!(se->flags & SEC_ACE_FLAG_CONTAINER_INHERIT)) {
3835 /* Doesn't apply to a directory - ignore. */
3836 DEBUG(10,("append_parent_acl: directory %s "
3837 "ignoring non container "
3838 "inherit flags %u on ACE with sid %s "
3839 "from parent %s\n",
3840 fsp_str_dbg(fsp),
3841 (unsigned int)se->flags,
3842 sid_string_dbg(&se->trustee),
3843 parent_name));
3844 continue;
3846 } else {
3847 if (!(se->flags & SEC_ACE_FLAG_OBJECT_INHERIT)) {
3848 /* Doesn't apply to a file - ignore. */
3849 DEBUG(10,("append_parent_acl: file %s "
3850 "ignoring non object "
3851 "inherit flags %u on ACE with sid %s "
3852 "from parent %s\n",
3853 fsp_str_dbg(fsp),
3854 (unsigned int)se->flags,
3855 sid_string_dbg(&se->trustee),
3856 parent_name));
3857 continue;
3861 if (is_dacl_protected) {
3862 /* If the DACL is protected it means we must
3863 * not overwrite an existing ACE entry with the
3864 * same SID. This is order N^2. Ouch :-(. JRA. */
3865 unsigned int k;
3866 for (k = 0; k < psd->dacl->num_aces; k++) {
3867 if (dom_sid_equal(&psd->dacl->aces[k].trustee,
3868 &se->trustee)) {
3869 break;
3872 if (k < psd->dacl->num_aces) {
3873 /* SID matched. Ignore. */
3874 DEBUG(10,("append_parent_acl: path %s "
3875 "ignoring ACE with protected sid %s "
3876 "from parent %s\n",
3877 fsp_str_dbg(fsp),
3878 sid_string_dbg(&se->trustee),
3879 parent_name));
3880 continue;
3884 sec_ace_copy(&new_ace[i], se);
3885 if (se->flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) {
3886 new_ace[i].flags &= ~(SEC_ACE_FLAG_VALID_INHERIT);
3888 new_ace[i].flags |= SEC_ACE_FLAG_INHERITED_ACE;
3890 if (fsp->is_directory) {
3892 * Strip off any inherit only. It's applied.
3894 new_ace[i].flags &= ~(SEC_ACE_FLAG_INHERIT_ONLY);
3895 if (se->flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) {
3896 /* No further inheritance. */
3897 new_ace[i].flags &=
3898 ~(SEC_ACE_FLAG_CONTAINER_INHERIT|
3899 SEC_ACE_FLAG_OBJECT_INHERIT);
3901 } else {
3903 * Strip off any container or inherit
3904 * flags, they can't apply to objects.
3906 new_ace[i].flags &= ~(SEC_ACE_FLAG_CONTAINER_INHERIT|
3907 SEC_ACE_FLAG_INHERIT_ONLY|
3908 SEC_ACE_FLAG_NO_PROPAGATE_INHERIT);
3910 i++;
3912 DEBUG(10,("append_parent_acl: path %s "
3913 "inheriting ACE with sid %s "
3914 "from parent %s\n",
3915 fsp_str_dbg(fsp),
3916 sid_string_dbg(&se->trustee),
3917 parent_name));
3920 psd->dacl->aces = new_ace;
3921 psd->dacl->num_aces = i;
3922 psd->type &= ~(SEC_DESC_DACL_AUTO_INHERITED|
3923 SEC_DESC_DACL_AUTO_INHERIT_REQ);
3925 *pp_new_sd = psd;
3926 return status;
3928 #endif
3930 /****************************************************************************
3931 Reply to set a security descriptor on an fsp. security_info_sent is the
3932 description of the following NT ACL.
3933 This should be the only external function needed for the UNIX style set ACL.
3934 We make a copy of psd_orig as internal functions modify the elements inside
3935 it, even though it's a const pointer.
3936 ****************************************************************************/
3938 NTSTATUS set_nt_acl(files_struct *fsp, uint32 security_info_sent, const struct security_descriptor *psd_orig)
3940 connection_struct *conn = fsp->conn;
3941 uid_t user = (uid_t)-1;
3942 gid_t grp = (gid_t)-1;
3943 struct dom_sid file_owner_sid;
3944 struct dom_sid file_grp_sid;
3945 canon_ace *file_ace_list = NULL;
3946 canon_ace *dir_ace_list = NULL;
3947 bool acl_perms = False;
3948 mode_t orig_mode = (mode_t)0;
3949 NTSTATUS status;
3950 bool set_acl_as_root = false;
3951 bool acl_set_support = false;
3952 bool ret = false;
3953 struct security_descriptor *psd = NULL;
3955 DEBUG(10,("set_nt_acl: called for file %s\n",
3956 fsp_str_dbg(fsp)));
3958 if (!CAN_WRITE(conn)) {
3959 DEBUG(10,("set acl rejected on read-only share\n"));
3960 return NT_STATUS_MEDIA_WRITE_PROTECTED;
3963 if (!psd_orig) {
3964 return NT_STATUS_INVALID_PARAMETER;
3967 psd = dup_sec_desc(talloc_tos(), psd_orig);
3968 if (!psd) {
3969 return NT_STATUS_NO_MEMORY;
3973 * Get the current state of the file.
3976 status = vfs_stat_fsp(fsp);
3977 if (!NT_STATUS_IS_OK(status)) {
3978 return status;
3981 /* Save the original element we check against. */
3982 orig_mode = fsp->fsp_name->st.st_ex_mode;
3985 * Unpack the user/group/world id's.
3988 /* POSIX can't cope with missing owner/group. */
3989 if ((security_info_sent & SECINFO_OWNER) && (psd->owner_sid == NULL)) {
3990 security_info_sent &= ~SECINFO_OWNER;
3992 if ((security_info_sent & SECINFO_GROUP) && (psd->group_sid == NULL)) {
3993 security_info_sent &= ~SECINFO_GROUP;
3996 status = unpack_nt_owners( conn, &user, &grp, security_info_sent, psd);
3997 if (!NT_STATUS_IS_OK(status)) {
3998 return status;
4002 * Do we need to chown ? If so this must be done first as the incoming
4003 * CREATOR_OWNER acl will be relative to the *new* owner, not the old.
4004 * Noticed by Simo.
4007 if (((user != (uid_t)-1) && (fsp->fsp_name->st.st_ex_uid != user)) ||
4008 (( grp != (gid_t)-1) && (fsp->fsp_name->st.st_ex_gid != grp))) {
4010 DEBUG(3,("set_nt_acl: chown %s. uid = %u, gid = %u.\n",
4011 fsp_str_dbg(fsp), (unsigned int)user,
4012 (unsigned int)grp));
4014 status = try_chown(fsp, user, grp);
4015 if(!NT_STATUS_IS_OK(status)) {
4016 DEBUG(3,("set_nt_acl: chown %s, %u, %u failed. Error "
4017 "= %s.\n", fsp_str_dbg(fsp),
4018 (unsigned int)user,
4019 (unsigned int)grp,
4020 nt_errstr(status)));
4021 return status;
4025 * Recheck the current state of the file, which may have changed.
4026 * (suid/sgid bits, for instance)
4029 status = vfs_stat_fsp(fsp);
4030 if (!NT_STATUS_IS_OK(status)) {
4031 return status;
4034 /* Save the original element we check against. */
4035 orig_mode = fsp->fsp_name->st.st_ex_mode;
4037 /* If we successfully chowned, we know we must
4038 * be able to set the acl, so do it as root.
4040 set_acl_as_root = true;
4043 create_file_sids(&fsp->fsp_name->st, &file_owner_sid, &file_grp_sid);
4045 if((security_info_sent & SECINFO_DACL) &&
4046 (psd->type & SEC_DESC_DACL_PRESENT) &&
4047 (psd->dacl == NULL)) {
4048 struct security_ace ace[3];
4050 /* We can't have NULL DACL in POSIX.
4051 Use owner/group/Everyone -> full access. */
4053 init_sec_ace(&ace[0],
4054 &file_owner_sid,
4055 SEC_ACE_TYPE_ACCESS_ALLOWED,
4056 GENERIC_ALL_ACCESS,
4058 init_sec_ace(&ace[1],
4059 &file_grp_sid,
4060 SEC_ACE_TYPE_ACCESS_ALLOWED,
4061 GENERIC_ALL_ACCESS,
4063 init_sec_ace(&ace[2],
4064 &global_sid_World,
4065 SEC_ACE_TYPE_ACCESS_ALLOWED,
4066 GENERIC_ALL_ACCESS,
4068 psd->dacl = make_sec_acl(talloc_tos(),
4069 NT4_ACL_REVISION,
4071 ace);
4072 if (psd->dacl == NULL) {
4073 return NT_STATUS_NO_MEMORY;
4075 security_acl_map_generic(psd->dacl, &file_generic_mapping);
4078 acl_perms = unpack_canon_ace(fsp, &fsp->fsp_name->st, &file_owner_sid,
4079 &file_grp_sid, &file_ace_list,
4080 &dir_ace_list, security_info_sent, psd);
4082 /* Ignore W2K traverse DACL set. */
4083 if (!file_ace_list && !dir_ace_list) {
4084 return NT_STATUS_OK;
4087 if (!acl_perms) {
4088 DEBUG(3,("set_nt_acl: cannot set permissions\n"));
4089 free_canon_ace_list(file_ace_list);
4090 free_canon_ace_list(dir_ace_list);
4091 return NT_STATUS_ACCESS_DENIED;
4095 * Only change security if we got a DACL.
4098 if(!(security_info_sent & SECINFO_DACL) || (psd->dacl == NULL)) {
4099 free_canon_ace_list(file_ace_list);
4100 free_canon_ace_list(dir_ace_list);
4101 return NT_STATUS_OK;
4105 * Try using the POSIX ACL set first. Fall back to chmod if
4106 * we have no ACL support on this filesystem.
4109 if (acl_perms && file_ace_list) {
4110 if (set_acl_as_root) {
4111 become_root();
4113 ret = set_canon_ace_list(fsp, file_ace_list, false,
4114 &fsp->fsp_name->st, &acl_set_support);
4115 if (set_acl_as_root) {
4116 unbecome_root();
4118 if (acl_set_support && ret == false) {
4119 DEBUG(3,("set_nt_acl: failed to set file acl on file "
4120 "%s (%s).\n", fsp_str_dbg(fsp),
4121 strerror(errno)));
4122 free_canon_ace_list(file_ace_list);
4123 free_canon_ace_list(dir_ace_list);
4124 return map_nt_error_from_unix(errno);
4128 if (acl_perms && acl_set_support && fsp->is_directory) {
4129 if (dir_ace_list) {
4130 if (set_acl_as_root) {
4131 become_root();
4133 ret = set_canon_ace_list(fsp, dir_ace_list, true,
4134 &fsp->fsp_name->st,
4135 &acl_set_support);
4136 if (set_acl_as_root) {
4137 unbecome_root();
4139 if (ret == false) {
4140 DEBUG(3,("set_nt_acl: failed to set default "
4141 "acl on directory %s (%s).\n",
4142 fsp_str_dbg(fsp), strerror(errno)));
4143 free_canon_ace_list(file_ace_list);
4144 free_canon_ace_list(dir_ace_list);
4145 return map_nt_error_from_unix(errno);
4147 } else {
4148 int sret = -1;
4151 * No default ACL - delete one if it exists.
4154 if (set_acl_as_root) {
4155 become_root();
4157 sret = SMB_VFS_SYS_ACL_DELETE_DEF_FILE(conn,
4158 fsp->fsp_name->base_name);
4159 if (set_acl_as_root) {
4160 unbecome_root();
4162 if (sret == -1) {
4163 if (acl_group_override(conn, fsp->fsp_name)) {
4164 DEBUG(5,("set_nt_acl: acl group "
4165 "control on and current user "
4166 "in file %s primary group. "
4167 "Override delete_def_acl\n",
4168 fsp_str_dbg(fsp)));
4170 become_root();
4171 sret =
4172 SMB_VFS_SYS_ACL_DELETE_DEF_FILE(
4173 conn,
4174 fsp->fsp_name->base_name);
4175 unbecome_root();
4178 if (sret == -1) {
4179 DEBUG(3,("set_nt_acl: sys_acl_delete_def_file failed (%s)\n", strerror(errno)));
4180 free_canon_ace_list(file_ace_list);
4181 free_canon_ace_list(dir_ace_list);
4182 return map_nt_error_from_unix(errno);
4188 if (acl_set_support) {
4189 if (set_acl_as_root) {
4190 become_root();
4192 store_inheritance_attributes(fsp,
4193 file_ace_list,
4194 dir_ace_list,
4195 psd->type);
4196 if (set_acl_as_root) {
4197 unbecome_root();
4202 * If we cannot set using POSIX ACLs we fall back to checking if we need to chmod.
4205 if(!acl_set_support && acl_perms) {
4206 mode_t posix_perms;
4208 if (!convert_canon_ace_to_posix_perms( fsp, file_ace_list, &posix_perms)) {
4209 free_canon_ace_list(file_ace_list);
4210 free_canon_ace_list(dir_ace_list);
4211 DEBUG(3,("set_nt_acl: failed to convert file acl to "
4212 "posix permissions for file %s.\n",
4213 fsp_str_dbg(fsp)));
4214 return NT_STATUS_ACCESS_DENIED;
4217 if (orig_mode != posix_perms) {
4218 int sret = -1;
4220 DEBUG(3,("set_nt_acl: chmod %s. perms = 0%o.\n",
4221 fsp_str_dbg(fsp), (unsigned int)posix_perms));
4223 if (set_acl_as_root) {
4224 become_root();
4226 sret = SMB_VFS_CHMOD(conn, fsp->fsp_name->base_name,
4227 posix_perms);
4228 if (set_acl_as_root) {
4229 unbecome_root();
4231 if(sret == -1) {
4232 if (acl_group_override(conn, fsp->fsp_name)) {
4233 DEBUG(5,("set_nt_acl: acl group "
4234 "control on and current user "
4235 "in file %s primary group. "
4236 "Override chmod\n",
4237 fsp_str_dbg(fsp)));
4239 become_root();
4240 sret = SMB_VFS_CHMOD(conn,
4241 fsp->fsp_name->base_name,
4242 posix_perms);
4243 unbecome_root();
4246 if (sret == -1) {
4247 DEBUG(3,("set_nt_acl: chmod %s, 0%o "
4248 "failed. Error = %s.\n",
4249 fsp_str_dbg(fsp),
4250 (unsigned int)posix_perms,
4251 strerror(errno)));
4252 free_canon_ace_list(file_ace_list);
4253 free_canon_ace_list(dir_ace_list);
4254 return map_nt_error_from_unix(errno);
4260 free_canon_ace_list(file_ace_list);
4261 free_canon_ace_list(dir_ace_list);
4263 /* Ensure the stat struct in the fsp is correct. */
4264 status = vfs_stat_fsp(fsp);
4266 return NT_STATUS_OK;
4269 /****************************************************************************
4270 Get the actual group bits stored on a file with an ACL. Has no effect if
4271 the file has no ACL. Needed in dosmode code where the stat() will return
4272 the mask bits, not the real group bits, for a file with an ACL.
4273 ****************************************************************************/
4275 int get_acl_group_bits( connection_struct *conn, const char *fname, mode_t *mode )
4277 int entry_id = SMB_ACL_FIRST_ENTRY;
4278 SMB_ACL_ENTRY_T entry;
4279 SMB_ACL_T posix_acl;
4280 int result = -1;
4282 posix_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, fname,
4283 SMB_ACL_TYPE_ACCESS, talloc_tos());
4284 if (posix_acl == (SMB_ACL_T)NULL)
4285 return -1;
4287 while (sys_acl_get_entry(posix_acl, entry_id, &entry) == 1) {
4288 SMB_ACL_TAG_T tagtype;
4289 SMB_ACL_PERMSET_T permset;
4291 entry_id = SMB_ACL_NEXT_ENTRY;
4293 if (sys_acl_get_tag_type(entry, &tagtype) ==-1)
4294 break;
4296 if (tagtype == SMB_ACL_GROUP_OBJ) {
4297 if (sys_acl_get_permset(entry, &permset) == -1) {
4298 break;
4299 } else {
4300 *mode &= ~(S_IRGRP|S_IWGRP|S_IXGRP);
4301 *mode |= (sys_acl_get_perm(permset, SMB_ACL_READ) ? S_IRGRP : 0);
4302 *mode |= (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? S_IWGRP : 0);
4303 *mode |= (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? S_IXGRP : 0);
4304 result = 0;
4305 break;
4309 TALLOC_FREE(posix_acl);
4310 return result;
4313 /****************************************************************************
4314 Do a chmod by setting the ACL USER_OBJ, GROUP_OBJ and OTHER bits in an ACL
4315 and set the mask to rwx. Needed to preserve complex ACLs set by NT.
4316 ****************************************************************************/
4318 static int chmod_acl_internals( connection_struct *conn, SMB_ACL_T posix_acl, mode_t mode)
4320 int entry_id = SMB_ACL_FIRST_ENTRY;
4321 SMB_ACL_ENTRY_T entry;
4322 int num_entries = 0;
4324 while ( sys_acl_get_entry(posix_acl, entry_id, &entry) == 1) {
4325 SMB_ACL_TAG_T tagtype;
4326 SMB_ACL_PERMSET_T permset;
4327 mode_t perms;
4329 entry_id = SMB_ACL_NEXT_ENTRY;
4331 if (sys_acl_get_tag_type(entry, &tagtype) == -1)
4332 return -1;
4334 if (sys_acl_get_permset(entry, &permset) == -1)
4335 return -1;
4337 num_entries++;
4339 switch(tagtype) {
4340 case SMB_ACL_USER_OBJ:
4341 perms = unix_perms_to_acl_perms(mode, S_IRUSR, S_IWUSR, S_IXUSR);
4342 break;
4343 case SMB_ACL_GROUP_OBJ:
4344 perms = unix_perms_to_acl_perms(mode, S_IRGRP, S_IWGRP, S_IXGRP);
4345 break;
4346 case SMB_ACL_MASK:
4348 * FIXME: The ACL_MASK entry permissions should really be set to
4349 * the union of the permissions of all ACL_USER,
4350 * ACL_GROUP_OBJ, and ACL_GROUP entries. That's what
4351 * acl_calc_mask() does, but Samba ACLs doesn't provide it.
4353 perms = S_IRUSR|S_IWUSR|S_IXUSR;
4354 break;
4355 case SMB_ACL_OTHER:
4356 perms = unix_perms_to_acl_perms(mode, S_IROTH, S_IWOTH, S_IXOTH);
4357 break;
4358 default:
4359 continue;
4362 if (map_acl_perms_to_permset(conn, perms, &permset) == -1)
4363 return -1;
4365 if (sys_acl_set_permset(entry, permset) == -1)
4366 return -1;
4370 * If this is a simple 3 element ACL or no elements then it's a standard
4371 * UNIX permission set. Just use chmod...
4374 if ((num_entries == 3) || (num_entries == 0))
4375 return -1;
4377 return 0;
4380 /****************************************************************************
4381 Get the access ACL of FROM, do a chmod by setting the ACL USER_OBJ,
4382 GROUP_OBJ and OTHER bits in an ACL and set the mask to rwx. Set the
4383 resulting ACL on TO. Note that name is in UNIX character set.
4384 ****************************************************************************/
4386 static int copy_access_posix_acl(connection_struct *conn, const char *from, const char *to, mode_t mode)
4388 SMB_ACL_T posix_acl = NULL;
4389 int ret = -1;
4391 if ((posix_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, from,
4392 SMB_ACL_TYPE_ACCESS,
4393 talloc_tos())) == NULL)
4394 return -1;
4396 if ((ret = chmod_acl_internals(conn, posix_acl, mode)) == -1)
4397 goto done;
4399 ret = SMB_VFS_SYS_ACL_SET_FILE(conn, to, SMB_ACL_TYPE_ACCESS, posix_acl);
4401 done:
4403 TALLOC_FREE(posix_acl);
4404 return ret;
4407 /****************************************************************************
4408 Do a chmod by setting the ACL USER_OBJ, GROUP_OBJ and OTHER bits in an ACL
4409 and set the mask to rwx. Needed to preserve complex ACLs set by NT.
4410 Note that name is in UNIX character set.
4411 ****************************************************************************/
4413 int chmod_acl(connection_struct *conn, const char *name, mode_t mode)
4415 return copy_access_posix_acl(conn, name, name, mode);
4418 /****************************************************************************
4419 Check for an existing default POSIX ACL on a directory.
4420 ****************************************************************************/
4422 static bool directory_has_default_posix_acl(connection_struct *conn, const char *fname)
4424 SMB_ACL_T def_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, fname,
4425 SMB_ACL_TYPE_DEFAULT,
4426 talloc_tos());
4427 bool has_acl = False;
4428 SMB_ACL_ENTRY_T entry;
4430 if (def_acl != NULL && (sys_acl_get_entry(def_acl, SMB_ACL_FIRST_ENTRY, &entry) == 1)) {
4431 has_acl = True;
4434 if (def_acl) {
4435 TALLOC_FREE(def_acl);
4437 return has_acl;
4440 /****************************************************************************
4441 If the parent directory has no default ACL but it does have an Access ACL,
4442 inherit this Access ACL to file name.
4443 ****************************************************************************/
4445 int inherit_access_posix_acl(connection_struct *conn, const char *inherit_from_dir,
4446 const char *name, mode_t mode)
4448 if (directory_has_default_posix_acl(conn, inherit_from_dir))
4449 return 0;
4451 return copy_access_posix_acl(conn, inherit_from_dir, name, mode);
4454 /****************************************************************************
4455 Do an fchmod by setting the ACL USER_OBJ, GROUP_OBJ and OTHER bits in an ACL
4456 and set the mask to rwx. Needed to preserve complex ACLs set by NT.
4457 ****************************************************************************/
4459 int fchmod_acl(files_struct *fsp, mode_t mode)
4461 connection_struct *conn = fsp->conn;
4462 SMB_ACL_T posix_acl = NULL;
4463 int ret = -1;
4465 if ((posix_acl = SMB_VFS_SYS_ACL_GET_FD(fsp, talloc_tos())) == NULL)
4466 return -1;
4468 if ((ret = chmod_acl_internals(conn, posix_acl, mode)) == -1)
4469 goto done;
4471 ret = SMB_VFS_SYS_ACL_SET_FD(fsp, posix_acl);
4473 done:
4475 TALLOC_FREE(posix_acl);
4476 return ret;
4479 /****************************************************************************
4480 Map from wire type to permset.
4481 ****************************************************************************/
4483 static bool unix_ex_wire_to_permset(connection_struct *conn, unsigned char wire_perm, SMB_ACL_PERMSET_T *p_permset)
4485 if (wire_perm & ~(SMB_POSIX_ACL_READ|SMB_POSIX_ACL_WRITE|SMB_POSIX_ACL_EXECUTE)) {
4486 return False;
4489 if (sys_acl_clear_perms(*p_permset) == -1) {
4490 return False;
4493 if (wire_perm & SMB_POSIX_ACL_READ) {
4494 if (sys_acl_add_perm(*p_permset, SMB_ACL_READ) == -1) {
4495 return False;
4498 if (wire_perm & SMB_POSIX_ACL_WRITE) {
4499 if (sys_acl_add_perm(*p_permset, SMB_ACL_WRITE) == -1) {
4500 return False;
4503 if (wire_perm & SMB_POSIX_ACL_EXECUTE) {
4504 if (sys_acl_add_perm(*p_permset, SMB_ACL_EXECUTE) == -1) {
4505 return False;
4508 return True;
4511 /****************************************************************************
4512 Map from wire type to tagtype.
4513 ****************************************************************************/
4515 static bool unix_ex_wire_to_tagtype(unsigned char wire_tt, SMB_ACL_TAG_T *p_tt)
4517 switch (wire_tt) {
4518 case SMB_POSIX_ACL_USER_OBJ:
4519 *p_tt = SMB_ACL_USER_OBJ;
4520 break;
4521 case SMB_POSIX_ACL_USER:
4522 *p_tt = SMB_ACL_USER;
4523 break;
4524 case SMB_POSIX_ACL_GROUP_OBJ:
4525 *p_tt = SMB_ACL_GROUP_OBJ;
4526 break;
4527 case SMB_POSIX_ACL_GROUP:
4528 *p_tt = SMB_ACL_GROUP;
4529 break;
4530 case SMB_POSIX_ACL_MASK:
4531 *p_tt = SMB_ACL_MASK;
4532 break;
4533 case SMB_POSIX_ACL_OTHER:
4534 *p_tt = SMB_ACL_OTHER;
4535 break;
4536 default:
4537 return False;
4539 return True;
4542 /****************************************************************************
4543 Create a new POSIX acl from wire permissions.
4544 FIXME ! How does the share mask/mode fit into this.... ?
4545 ****************************************************************************/
4547 static SMB_ACL_T create_posix_acl_from_wire(connection_struct *conn,
4548 uint16 num_acls,
4549 const char *pdata,
4550 TALLOC_CTX *mem_ctx)
4552 unsigned int i;
4553 SMB_ACL_T the_acl = sys_acl_init(mem_ctx);
4555 if (the_acl == NULL) {
4556 return NULL;
4559 for (i = 0; i < num_acls; i++) {
4560 SMB_ACL_ENTRY_T the_entry;
4561 SMB_ACL_PERMSET_T the_permset;
4562 SMB_ACL_TAG_T tag_type;
4564 if (sys_acl_create_entry(&the_acl, &the_entry) == -1) {
4565 DEBUG(0,("create_posix_acl_from_wire: Failed to create entry %u. (%s)\n",
4566 i, strerror(errno) ));
4567 goto fail;
4570 if (!unix_ex_wire_to_tagtype(CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)), &tag_type)) {
4571 DEBUG(0,("create_posix_acl_from_wire: invalid wire tagtype %u on entry %u.\n",
4572 CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)), i ));
4573 goto fail;
4576 if (sys_acl_set_tag_type(the_entry, tag_type) == -1) {
4577 DEBUG(0,("create_posix_acl_from_wire: Failed to set tagtype on entry %u. (%s)\n",
4578 i, strerror(errno) ));
4579 goto fail;
4582 /* Get the permset pointer from the new ACL entry. */
4583 if (sys_acl_get_permset(the_entry, &the_permset) == -1) {
4584 DEBUG(0,("create_posix_acl_from_wire: Failed to get permset on entry %u. (%s)\n",
4585 i, strerror(errno) ));
4586 goto fail;
4589 /* Map from wire to permissions. */
4590 if (!unix_ex_wire_to_permset(conn, CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)+1), &the_permset)) {
4591 DEBUG(0,("create_posix_acl_from_wire: invalid permset %u on entry %u.\n",
4592 CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE) + 1), i ));
4593 goto fail;
4596 /* Now apply to the new ACL entry. */
4597 if (sys_acl_set_permset(the_entry, the_permset) == -1) {
4598 DEBUG(0,("create_posix_acl_from_wire: Failed to add permset on entry %u. (%s)\n",
4599 i, strerror(errno) ));
4600 goto fail;
4603 if (tag_type == SMB_ACL_USER) {
4604 uint32 uidval = IVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
4605 uid_t uid = (uid_t)uidval;
4606 if (sys_acl_set_qualifier(the_entry,(void *)&uid) == -1) {
4607 DEBUG(0,("create_posix_acl_from_wire: Failed to set uid %u on entry %u. (%s)\n",
4608 (unsigned int)uid, i, strerror(errno) ));
4609 goto fail;
4613 if (tag_type == SMB_ACL_GROUP) {
4614 uint32 gidval = IVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
4615 gid_t gid = (uid_t)gidval;
4616 if (sys_acl_set_qualifier(the_entry,(void *)&gid) == -1) {
4617 DEBUG(0,("create_posix_acl_from_wire: Failed to set gid %u on entry %u. (%s)\n",
4618 (unsigned int)gid, i, strerror(errno) ));
4619 goto fail;
4624 return the_acl;
4626 fail:
4628 if (the_acl != NULL) {
4629 TALLOC_FREE(the_acl);
4631 return NULL;
4634 /****************************************************************************
4635 Calls from UNIX extensions - Default POSIX ACL set.
4636 If num_def_acls == 0 and not a directory just return. If it is a directory
4637 and num_def_acls == 0 then remove the default acl. Else set the default acl
4638 on the directory.
4639 ****************************************************************************/
4641 bool set_unix_posix_default_acl(connection_struct *conn, const char *fname, const SMB_STRUCT_STAT *psbuf,
4642 uint16 num_def_acls, const char *pdata)
4644 SMB_ACL_T def_acl = NULL;
4646 if (!S_ISDIR(psbuf->st_ex_mode)) {
4647 if (num_def_acls) {
4648 DEBUG(5,("set_unix_posix_default_acl: Can't set default ACL on non-directory file %s\n", fname ));
4649 errno = EISDIR;
4650 return False;
4651 } else {
4652 return True;
4656 if (!num_def_acls) {
4657 /* Remove the default ACL. */
4658 if (SMB_VFS_SYS_ACL_DELETE_DEF_FILE(conn, fname) == -1) {
4659 DEBUG(5,("set_unix_posix_default_acl: acl_delete_def_file failed on directory %s (%s)\n",
4660 fname, strerror(errno) ));
4661 return False;
4663 return True;
4666 if ((def_acl = create_posix_acl_from_wire(conn, num_def_acls,
4667 pdata,
4668 talloc_tos())) == NULL) {
4669 return False;
4672 if (SMB_VFS_SYS_ACL_SET_FILE(conn, fname, SMB_ACL_TYPE_DEFAULT, def_acl) == -1) {
4673 DEBUG(5,("set_unix_posix_default_acl: acl_set_file failed on directory %s (%s)\n",
4674 fname, strerror(errno) ));
4675 TALLOC_FREE(def_acl);
4676 return False;
4679 DEBUG(10,("set_unix_posix_default_acl: set default acl for file %s\n", fname ));
4680 TALLOC_FREE(def_acl);
4681 return True;
4684 /****************************************************************************
4685 Remove an ACL from a file. As we don't have acl_delete_entry() available
4686 we must read the current acl and copy all entries except MASK, USER and GROUP
4687 to a new acl, then set that. This (at least on Linux) causes any ACL to be
4688 removed.
4689 FIXME ! How does the share mask/mode fit into this.... ?
4690 ****************************************************************************/
4692 static bool remove_posix_acl(connection_struct *conn, files_struct *fsp, const char *fname)
4694 SMB_ACL_T file_acl = NULL;
4695 int entry_id = SMB_ACL_FIRST_ENTRY;
4696 SMB_ACL_ENTRY_T entry;
4697 bool ret = False;
4698 /* Create a new ACL with only 3 entries, u/g/w. */
4699 SMB_ACL_T new_file_acl = sys_acl_init(talloc_tos());
4700 SMB_ACL_ENTRY_T user_ent = NULL;
4701 SMB_ACL_ENTRY_T group_ent = NULL;
4702 SMB_ACL_ENTRY_T other_ent = NULL;
4704 if (new_file_acl == NULL) {
4705 DEBUG(5,("remove_posix_acl: failed to init new ACL with 3 entries for file %s.\n", fname));
4706 return False;
4709 /* Now create the u/g/w entries. */
4710 if (sys_acl_create_entry(&new_file_acl, &user_ent) == -1) {
4711 DEBUG(5,("remove_posix_acl: Failed to create user entry for file %s. (%s)\n",
4712 fname, strerror(errno) ));
4713 goto done;
4715 if (sys_acl_set_tag_type(user_ent, SMB_ACL_USER_OBJ) == -1) {
4716 DEBUG(5,("remove_posix_acl: Failed to set user entry for file %s. (%s)\n",
4717 fname, strerror(errno) ));
4718 goto done;
4721 if (sys_acl_create_entry(&new_file_acl, &group_ent) == -1) {
4722 DEBUG(5,("remove_posix_acl: Failed to create group entry for file %s. (%s)\n",
4723 fname, strerror(errno) ));
4724 goto done;
4726 if (sys_acl_set_tag_type(group_ent, SMB_ACL_GROUP_OBJ) == -1) {
4727 DEBUG(5,("remove_posix_acl: Failed to set group entry for file %s. (%s)\n",
4728 fname, strerror(errno) ));
4729 goto done;
4732 if (sys_acl_create_entry(&new_file_acl, &other_ent) == -1) {
4733 DEBUG(5,("remove_posix_acl: Failed to create other entry for file %s. (%s)\n",
4734 fname, strerror(errno) ));
4735 goto done;
4737 if (sys_acl_set_tag_type(other_ent, SMB_ACL_OTHER) == -1) {
4738 DEBUG(5,("remove_posix_acl: Failed to set other entry for file %s. (%s)\n",
4739 fname, strerror(errno) ));
4740 goto done;
4743 /* Get the current file ACL. */
4744 if (fsp && fsp->fh->fd != -1) {
4745 file_acl = SMB_VFS_SYS_ACL_GET_FD(fsp, talloc_tos());
4746 } else {
4747 file_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, fname,
4748 SMB_ACL_TYPE_ACCESS,
4749 talloc_tos());
4752 if (file_acl == NULL) {
4753 /* This is only returned if an error occurred. Even for a file with
4754 no acl a u/g/w acl should be returned. */
4755 DEBUG(5,("remove_posix_acl: failed to get ACL from file %s (%s).\n",
4756 fname, strerror(errno) ));
4757 goto done;
4760 while ( sys_acl_get_entry(file_acl, entry_id, &entry) == 1) {
4761 SMB_ACL_TAG_T tagtype;
4762 SMB_ACL_PERMSET_T permset;
4764 entry_id = SMB_ACL_NEXT_ENTRY;
4766 if (sys_acl_get_tag_type(entry, &tagtype) == -1) {
4767 DEBUG(5,("remove_posix_acl: failed to get tagtype from ACL on file %s (%s).\n",
4768 fname, strerror(errno) ));
4769 goto done;
4772 if (sys_acl_get_permset(entry, &permset) == -1) {
4773 DEBUG(5,("remove_posix_acl: failed to get permset from ACL on file %s (%s).\n",
4774 fname, strerror(errno) ));
4775 goto done;
4778 if (tagtype == SMB_ACL_USER_OBJ) {
4779 if (sys_acl_set_permset(user_ent, permset) == -1) {
4780 DEBUG(5,("remove_posix_acl: failed to set permset from ACL on file %s (%s).\n",
4781 fname, strerror(errno) ));
4783 } else if (tagtype == SMB_ACL_GROUP_OBJ) {
4784 if (sys_acl_set_permset(group_ent, permset) == -1) {
4785 DEBUG(5,("remove_posix_acl: failed to set permset from ACL on file %s (%s).\n",
4786 fname, strerror(errno) ));
4788 } else if (tagtype == SMB_ACL_OTHER) {
4789 if (sys_acl_set_permset(other_ent, permset) == -1) {
4790 DEBUG(5,("remove_posix_acl: failed to set permset from ACL on file %s (%s).\n",
4791 fname, strerror(errno) ));
4796 /* Set the new empty file ACL. */
4797 if (fsp && fsp->fh->fd != -1) {
4798 if (SMB_VFS_SYS_ACL_SET_FD(fsp, new_file_acl) == -1) {
4799 DEBUG(5,("remove_posix_acl: acl_set_file failed on %s (%s)\n",
4800 fname, strerror(errno) ));
4801 goto done;
4803 } else {
4804 if (SMB_VFS_SYS_ACL_SET_FILE(conn, fname, SMB_ACL_TYPE_ACCESS, new_file_acl) == -1) {
4805 DEBUG(5,("remove_posix_acl: acl_set_file failed on %s (%s)\n",
4806 fname, strerror(errno) ));
4807 goto done;
4811 ret = True;
4813 done:
4815 if (file_acl) {
4816 TALLOC_FREE(file_acl);
4818 if (new_file_acl) {
4819 TALLOC_FREE(new_file_acl);
4821 return ret;
4824 /****************************************************************************
4825 Calls from UNIX extensions - POSIX ACL set.
4826 If num_def_acls == 0 then read/modify/write acl after removing all entries
4827 except SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER.
4828 ****************************************************************************/
4830 bool set_unix_posix_acl(connection_struct *conn, files_struct *fsp, const char *fname, uint16 num_acls, const char *pdata)
4832 SMB_ACL_T file_acl = NULL;
4834 if (!num_acls) {
4835 /* Remove the ACL from the file. */
4836 return remove_posix_acl(conn, fsp, fname);
4839 if ((file_acl = create_posix_acl_from_wire(conn, num_acls,
4840 pdata,
4841 talloc_tos())) == NULL) {
4842 return False;
4845 if (fsp && fsp->fh->fd != -1) {
4846 /* The preferred way - use an open fd. */
4847 if (SMB_VFS_SYS_ACL_SET_FD(fsp, file_acl) == -1) {
4848 DEBUG(5,("set_unix_posix_acl: acl_set_file failed on %s (%s)\n",
4849 fname, strerror(errno) ));
4850 TALLOC_FREE(file_acl);
4851 return False;
4853 } else {
4854 if (SMB_VFS_SYS_ACL_SET_FILE(conn, fname, SMB_ACL_TYPE_ACCESS, file_acl) == -1) {
4855 DEBUG(5,("set_unix_posix_acl: acl_set_file failed on %s (%s)\n",
4856 fname, strerror(errno) ));
4857 TALLOC_FREE(file_acl);
4858 return False;
4862 DEBUG(10,("set_unix_posix_acl: set acl for file %s\n", fname ));
4863 TALLOC_FREE(file_acl);
4864 return True;
4867 /********************************************************************
4868 Pull the NT ACL from a file on disk or the OpenEventlog() access
4869 check. Caller is responsible for freeing the returned security
4870 descriptor via TALLOC_FREE(). This is designed for dealing with
4871 user space access checks in smbd outside of the VFS. For example,
4872 checking access rights in OpenEventlog().
4874 Assume we are dealing with files (for now)
4875 ********************************************************************/
4877 struct security_descriptor *get_nt_acl_no_snum( TALLOC_CTX *ctx, const char *fname, uint32 security_info_wanted)
4879 struct security_descriptor *ret_sd;
4880 connection_struct *conn;
4881 files_struct finfo;
4882 struct fd_handle fh;
4883 NTSTATUS status;
4884 TALLOC_CTX *frame = talloc_stackframe();
4886 conn = talloc_zero(frame, connection_struct);
4887 if (conn == NULL) {
4888 DEBUG(0, ("talloc failed\n"));
4889 return NULL;
4892 if (!(conn->params = talloc(conn, struct share_params))) {
4893 DEBUG(0,("get_nt_acl_no_snum: talloc() failed!\n"));
4894 TALLOC_FREE(frame);
4895 return NULL;
4898 conn->params->service = -1;
4900 set_conn_connectpath(conn, "/");
4902 if (!smbd_vfs_init(conn)) {
4903 DEBUG(0,("get_nt_acl_no_snum: Unable to create a fake connection struct!\n"));
4904 conn_free(conn);
4905 TALLOC_FREE(frame);
4906 return NULL;
4909 ZERO_STRUCT( finfo );
4910 ZERO_STRUCT( fh );
4912 finfo.fnum = FNUM_FIELD_INVALID;
4913 finfo.conn = conn;
4914 finfo.fh = &fh;
4915 finfo.fh->fd = -1;
4917 status = create_synthetic_smb_fname(frame, fname, NULL, NULL,
4918 &finfo.fsp_name);
4919 if (!NT_STATUS_IS_OK(status)) {
4920 conn_free(conn);
4921 TALLOC_FREE(frame);
4922 return NULL;
4925 if (!NT_STATUS_IS_OK(SMB_VFS_FGET_NT_ACL( &finfo,
4926 security_info_wanted,
4927 ctx, &ret_sd))) {
4928 DEBUG(0,("get_nt_acl_no_snum: get_nt_acl returned zero.\n"));
4929 TALLOC_FREE(finfo.fsp_name);
4930 conn_free(conn);
4931 TALLOC_FREE(frame);
4932 return NULL;
4935 TALLOC_FREE(finfo.fsp_name);
4936 conn_free(conn);
4937 TALLOC_FREE(frame);
4939 return ret_sd;
4942 /* Stolen shamelessly from pvfs_default_acl() in source4 :-). */
4944 NTSTATUS make_default_filesystem_acl(TALLOC_CTX *ctx,
4945 const char *name,
4946 SMB_STRUCT_STAT *psbuf,
4947 struct security_descriptor **ppdesc)
4949 struct dom_sid owner_sid, group_sid;
4950 size_t size = 0;
4951 struct security_ace aces[4];
4952 uint32_t access_mask = 0;
4953 mode_t mode = psbuf->st_ex_mode;
4954 struct security_acl *new_dacl = NULL;
4955 int idx = 0;
4957 DEBUG(10,("make_default_filesystem_acl: file %s mode = 0%o\n",
4958 name, (int)mode ));
4960 uid_to_sid(&owner_sid, psbuf->st_ex_uid);
4961 gid_to_sid(&group_sid, psbuf->st_ex_gid);
4964 We provide up to 4 ACEs
4965 - Owner
4966 - Group
4967 - Everyone
4968 - NT System
4971 if (mode & S_IRUSR) {
4972 if (mode & S_IWUSR) {
4973 access_mask |= SEC_RIGHTS_FILE_ALL;
4974 } else {
4975 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
4978 if (mode & S_IWUSR) {
4979 access_mask |= SEC_RIGHTS_FILE_WRITE | SEC_STD_DELETE;
4982 init_sec_ace(&aces[idx],
4983 &owner_sid,
4984 SEC_ACE_TYPE_ACCESS_ALLOWED,
4985 access_mask,
4987 idx++;
4989 access_mask = 0;
4990 if (mode & S_IRGRP) {
4991 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
4993 if (mode & S_IWGRP) {
4994 /* note that delete is not granted - this matches posix behaviour */
4995 access_mask |= SEC_RIGHTS_FILE_WRITE;
4997 if (access_mask) {
4998 init_sec_ace(&aces[idx],
4999 &group_sid,
5000 SEC_ACE_TYPE_ACCESS_ALLOWED,
5001 access_mask,
5003 idx++;
5006 access_mask = 0;
5007 if (mode & S_IROTH) {
5008 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
5010 if (mode & S_IWOTH) {
5011 access_mask |= SEC_RIGHTS_FILE_WRITE;
5013 if (access_mask) {
5014 init_sec_ace(&aces[idx],
5015 &global_sid_World,
5016 SEC_ACE_TYPE_ACCESS_ALLOWED,
5017 access_mask,
5019 idx++;
5022 init_sec_ace(&aces[idx],
5023 &global_sid_System,
5024 SEC_ACE_TYPE_ACCESS_ALLOWED,
5025 SEC_RIGHTS_FILE_ALL,
5027 idx++;
5029 new_dacl = make_sec_acl(ctx,
5030 NT4_ACL_REVISION,
5031 idx,
5032 aces);
5034 if (!new_dacl) {
5035 return NT_STATUS_NO_MEMORY;
5038 *ppdesc = make_sec_desc(ctx,
5039 SECURITY_DESCRIPTOR_REVISION_1,
5040 SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
5041 &owner_sid,
5042 &group_sid,
5043 NULL,
5044 new_dacl,
5045 &size);
5046 if (!*ppdesc) {
5047 return NT_STATUS_NO_MEMORY;
5049 return NT_STATUS_OK;