s3:smbd: Fix typo in got_duplicate_group check
[Samba/wip.git] / source3 / smbd / posix_acls.c
blobd46a16dbc13e1a06ae00958a37bb7e1323fdbb76
1 /*
2 Unix SMB/CIFS implementation.
3 SMB NT Security Descriptor / Unix permission conversion.
4 Copyright (C) Jeremy Allison 1994-2009.
5 Copyright (C) Andreas Gruenbacher 2002.
6 Copyright (C) Simo Sorce <idra@samba.org> 2009.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "smbd/smbd.h"
24 #include "system/filesys.h"
25 #include "../libcli/security/security.h"
26 #include "trans2.h"
27 #include "passdb/lookup_sid.h"
28 #include "auth.h"
29 #include "../librpc/gen_ndr/idmap.h"
30 #include "../librpc/gen_ndr/ndr_smb_acl.h"
31 #include "lib/param/loadparm.h"
33 extern const struct generic_mapping file_generic_mapping;
35 #undef DBGC_CLASS
36 #define DBGC_CLASS DBGC_ACLS
38 /****************************************************************************
39 Data structures representing the internal ACE format.
40 ****************************************************************************/
42 enum ace_owner {UID_ACE, GID_ACE, WORLD_ACE};
43 enum ace_attribute {ALLOW_ACE, DENY_ACE}; /* Used for incoming NT ACLS. */
45 typedef struct canon_ace {
46 struct canon_ace *next, *prev;
47 SMB_ACL_TAG_T type;
48 mode_t perms; /* Only use S_I(R|W|X)USR mode bits here. */
49 struct dom_sid trustee;
50 enum ace_owner owner_type;
51 enum ace_attribute attr;
52 struct unixid unix_ug;
53 uint8_t ace_flags; /* From windows ACE entry. */
54 } canon_ace;
56 #define ALL_ACE_PERMS (S_IRUSR|S_IWUSR|S_IXUSR)
59 * EA format of user.SAMBA_PAI (Samba_Posix_Acl_Interitance)
60 * attribute on disk - version 1.
61 * All values are little endian.
63 * | 1 | 1 | 2 | 2 | ....
64 * +------+------+-------------+---------------------+-------------+--------------------+
65 * | vers | flag | num_entries | num_default_entries | ..entries.. | default_entries... |
66 * +------+------+-------------+---------------------+-------------+--------------------+
68 * Entry format is :
70 * | 1 | 4 |
71 * +------+-------------------+
72 * | value| uid/gid or world |
73 * | type | value |
74 * +------+-------------------+
76 * Version 2 format. Stores extra Windows metadata about an ACL.
78 * | 1 | 2 | 2 | 2 | ....
79 * +------+----------+-------------+---------------------+-------------+--------------------+
80 * | vers | ace | num_entries | num_default_entries | ..entries.. | default_entries... |
81 * | 2 | type | | | | |
82 * +------+----------+-------------+---------------------+-------------+--------------------+
84 * Entry format is :
86 * | 1 | 1 | 4 |
87 * +------+------+-------------------+
88 * | ace | value| uid/gid or world |
89 * | flag | type | value |
90 * +------+-------------------+------+
94 #define PAI_VERSION_OFFSET 0
96 #define PAI_V1_FLAG_OFFSET 1
97 #define PAI_V1_NUM_ENTRIES_OFFSET 2
98 #define PAI_V1_NUM_DEFAULT_ENTRIES_OFFSET 4
99 #define PAI_V1_ENTRIES_BASE 6
100 #define PAI_V1_ACL_FLAG_PROTECTED 0x1
101 #define PAI_V1_ENTRY_LENGTH 5
103 #define PAI_V1_VERSION 1
105 #define PAI_V2_TYPE_OFFSET 1
106 #define PAI_V2_NUM_ENTRIES_OFFSET 3
107 #define PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET 5
108 #define PAI_V2_ENTRIES_BASE 7
109 #define PAI_V2_ENTRY_LENGTH 6
111 #define PAI_V2_VERSION 2
114 * In memory format of user.SAMBA_PAI attribute.
117 struct pai_entry {
118 struct pai_entry *next, *prev;
119 uint8_t ace_flags;
120 enum ace_owner owner_type;
121 struct unixid unix_ug;
124 struct pai_val {
125 uint16_t sd_type;
126 unsigned int num_entries;
127 struct pai_entry *entry_list;
128 unsigned int num_def_entries;
129 struct pai_entry *def_entry_list;
132 /************************************************************************
133 Return a uint32 of the pai_entry principal.
134 ************************************************************************/
136 static uint32_t get_pai_entry_val(struct pai_entry *paie)
138 switch (paie->owner_type) {
139 case UID_ACE:
140 DEBUG(10,("get_pai_entry_val: uid = %u\n", (unsigned int)paie->unix_ug.id ));
141 return (uint32_t)paie->unix_ug.id;
142 case GID_ACE:
143 DEBUG(10,("get_pai_entry_val: gid = %u\n", (unsigned int)paie->unix_ug.id ));
144 return (uint32_t)paie->unix_ug.id;
145 case WORLD_ACE:
146 default:
147 DEBUG(10,("get_pai_entry_val: world ace\n"));
148 return (uint32_t)-1;
152 /************************************************************************
153 Return a uint32 of the entry principal.
154 ************************************************************************/
156 static uint32_t get_entry_val(canon_ace *ace_entry)
158 switch (ace_entry->owner_type) {
159 case UID_ACE:
160 DEBUG(10,("get_entry_val: uid = %u\n", (unsigned int)ace_entry->unix_ug.id ));
161 return (uint32_t)ace_entry->unix_ug.id;
162 case GID_ACE:
163 DEBUG(10,("get_entry_val: gid = %u\n", (unsigned int)ace_entry->unix_ug.id ));
164 return (uint32_t)ace_entry->unix_ug.id;
165 case WORLD_ACE:
166 default:
167 DEBUG(10,("get_entry_val: world ace\n"));
168 return (uint32_t)-1;
172 /************************************************************************
173 Create the on-disk format (always v2 now). Caller must free.
174 ************************************************************************/
176 static char *create_pai_buf_v2(canon_ace *file_ace_list,
177 canon_ace *dir_ace_list,
178 uint16_t sd_type,
179 size_t *store_size)
181 char *pai_buf = NULL;
182 canon_ace *ace_list = NULL;
183 char *entry_offset = NULL;
184 unsigned int num_entries = 0;
185 unsigned int num_def_entries = 0;
186 unsigned int i;
188 for (ace_list = file_ace_list; ace_list; ace_list = ace_list->next) {
189 num_entries++;
192 for (ace_list = dir_ace_list; ace_list; ace_list = ace_list->next) {
193 num_def_entries++;
196 DEBUG(10,("create_pai_buf_v2: num_entries = %u, num_def_entries = %u\n", num_entries, num_def_entries ));
198 *store_size = PAI_V2_ENTRIES_BASE +
199 ((num_entries + num_def_entries)*PAI_V2_ENTRY_LENGTH);
201 pai_buf = talloc_array(talloc_tos(), char, *store_size);
202 if (!pai_buf) {
203 return NULL;
206 /* Set up the header. */
207 memset(pai_buf, '\0', PAI_V2_ENTRIES_BASE);
208 SCVAL(pai_buf,PAI_VERSION_OFFSET,PAI_V2_VERSION);
209 SSVAL(pai_buf,PAI_V2_TYPE_OFFSET, sd_type);
210 SSVAL(pai_buf,PAI_V2_NUM_ENTRIES_OFFSET,num_entries);
211 SSVAL(pai_buf,PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET,num_def_entries);
213 DEBUG(10,("create_pai_buf_v2: sd_type = 0x%x\n",
214 (unsigned int)sd_type ));
216 entry_offset = pai_buf + PAI_V2_ENTRIES_BASE;
218 i = 0;
219 for (ace_list = file_ace_list; ace_list; ace_list = ace_list->next) {
220 uint8_t type_val = (uint8_t)ace_list->owner_type;
221 uint32_t entry_val = get_entry_val(ace_list);
223 SCVAL(entry_offset,0,ace_list->ace_flags);
224 SCVAL(entry_offset,1,type_val);
225 SIVAL(entry_offset,2,entry_val);
226 DEBUG(10,("create_pai_buf_v2: entry %u [0x%x] [0x%x] [0x%x]\n",
228 (unsigned int)ace_list->ace_flags,
229 (unsigned int)type_val,
230 (unsigned int)entry_val ));
231 i++;
232 entry_offset += PAI_V2_ENTRY_LENGTH;
235 for (ace_list = dir_ace_list; ace_list; ace_list = ace_list->next) {
236 uint8_t type_val = (uint8_t)ace_list->owner_type;
237 uint32_t entry_val = get_entry_val(ace_list);
239 SCVAL(entry_offset,0,ace_list->ace_flags);
240 SCVAL(entry_offset,1,type_val);
241 SIVAL(entry_offset,2,entry_val);
242 DEBUG(10,("create_pai_buf_v2: entry %u [0x%x] [0x%x] [0x%x]\n",
244 (unsigned int)ace_list->ace_flags,
245 (unsigned int)type_val,
246 (unsigned int)entry_val ));
247 i++;
248 entry_offset += PAI_V2_ENTRY_LENGTH;
251 return pai_buf;
254 /************************************************************************
255 Store the user.SAMBA_PAI attribute on disk.
256 ************************************************************************/
258 static void store_inheritance_attributes(files_struct *fsp,
259 canon_ace *file_ace_list,
260 canon_ace *dir_ace_list,
261 uint16_t sd_type)
263 int ret;
264 size_t store_size;
265 char *pai_buf;
267 if (!lp_map_acl_inherit(SNUM(fsp->conn))) {
268 return;
271 pai_buf = create_pai_buf_v2(file_ace_list, dir_ace_list,
272 sd_type, &store_size);
274 if (fsp->fh->fd != -1) {
275 ret = SMB_VFS_FSETXATTR(fsp, SAMBA_POSIX_INHERITANCE_EA_NAME,
276 pai_buf, store_size, 0);
277 } else {
278 ret = SMB_VFS_SETXATTR(fsp->conn, fsp->fsp_name->base_name,
279 SAMBA_POSIX_INHERITANCE_EA_NAME,
280 pai_buf, store_size, 0);
283 TALLOC_FREE(pai_buf);
285 DEBUG(10,("store_inheritance_attribute: type 0x%x for file %s\n",
286 (unsigned int)sd_type,
287 fsp_str_dbg(fsp)));
289 if (ret == -1 && !no_acl_syscall_error(errno)) {
290 DEBUG(1,("store_inheritance_attribute: Error %s\n", strerror(errno) ));
294 /************************************************************************
295 Delete the in memory inheritance info.
296 ************************************************************************/
298 static void free_inherited_info(struct pai_val *pal)
300 if (pal) {
301 struct pai_entry *paie, *paie_next;
302 for (paie = pal->entry_list; paie; paie = paie_next) {
303 paie_next = paie->next;
304 TALLOC_FREE(paie);
306 for (paie = pal->def_entry_list; paie; paie = paie_next) {
307 paie_next = paie->next;
308 TALLOC_FREE(paie);
310 TALLOC_FREE(pal);
314 /************************************************************************
315 Get any stored ACE flags.
316 ************************************************************************/
318 static uint16_t get_pai_flags(struct pai_val *pal, canon_ace *ace_entry, bool default_ace)
320 struct pai_entry *paie;
322 if (!pal) {
323 return 0;
326 /* If the entry exists it is inherited. */
327 for (paie = (default_ace ? pal->def_entry_list : pal->entry_list); paie; paie = paie->next) {
328 if (ace_entry->owner_type == paie->owner_type &&
329 get_entry_val(ace_entry) == get_pai_entry_val(paie))
330 return paie->ace_flags;
332 return 0;
335 /************************************************************************
336 Ensure an attribute just read is valid - v1.
337 ************************************************************************/
339 static bool check_pai_ok_v1(const char *pai_buf, size_t pai_buf_data_size)
341 uint16 num_entries;
342 uint16 num_def_entries;
344 if (pai_buf_data_size < PAI_V1_ENTRIES_BASE) {
345 /* Corrupted - too small. */
346 return false;
349 if (CVAL(pai_buf,PAI_VERSION_OFFSET) != PAI_V1_VERSION) {
350 return false;
353 num_entries = SVAL(pai_buf,PAI_V1_NUM_ENTRIES_OFFSET);
354 num_def_entries = SVAL(pai_buf,PAI_V1_NUM_DEFAULT_ENTRIES_OFFSET);
356 /* Check the entry lists match. */
357 /* Each entry is 5 bytes (type plus 4 bytes of uid or gid). */
359 if (((num_entries + num_def_entries)*PAI_V1_ENTRY_LENGTH) +
360 PAI_V1_ENTRIES_BASE != pai_buf_data_size) {
361 return false;
364 return true;
367 /************************************************************************
368 Ensure an attribute just read is valid - v2.
369 ************************************************************************/
371 static bool check_pai_ok_v2(const char *pai_buf, size_t pai_buf_data_size)
373 uint16 num_entries;
374 uint16 num_def_entries;
376 if (pai_buf_data_size < PAI_V2_ENTRIES_BASE) {
377 /* Corrupted - too small. */
378 return false;
381 if (CVAL(pai_buf,PAI_VERSION_OFFSET) != PAI_V2_VERSION) {
382 return false;
385 num_entries = SVAL(pai_buf,PAI_V2_NUM_ENTRIES_OFFSET);
386 num_def_entries = SVAL(pai_buf,PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET);
388 /* Check the entry lists match. */
389 /* Each entry is 6 bytes (flags + type + 4 bytes of uid or gid). */
391 if (((num_entries + num_def_entries)*PAI_V2_ENTRY_LENGTH) +
392 PAI_V2_ENTRIES_BASE != pai_buf_data_size) {
393 return false;
396 return true;
399 /************************************************************************
400 Decode the owner.
401 ************************************************************************/
403 static bool get_pai_owner_type(struct pai_entry *paie, const char *entry_offset)
405 paie->owner_type = (enum ace_owner)CVAL(entry_offset,0);
406 switch( paie->owner_type) {
407 case UID_ACE:
408 paie->unix_ug.type = ID_TYPE_UID;
409 paie->unix_ug.id = (uid_t)IVAL(entry_offset,1);
410 DEBUG(10,("get_pai_owner_type: uid = %u\n",
411 (unsigned int)paie->unix_ug.id ));
412 break;
413 case GID_ACE:
414 paie->unix_ug.type = ID_TYPE_GID;
415 paie->unix_ug.id = (gid_t)IVAL(entry_offset,1);
416 DEBUG(10,("get_pai_owner_type: gid = %u\n",
417 (unsigned int)paie->unix_ug.id ));
418 break;
419 case WORLD_ACE:
420 paie->unix_ug.type = ID_TYPE_NOT_SPECIFIED;
421 paie->unix_ug.id = -1;
422 DEBUG(10,("get_pai_owner_type: world ace\n"));
423 break;
424 default:
425 DEBUG(10,("get_pai_owner_type: unknown type %u\n",
426 (unsigned int)paie->owner_type ));
427 return false;
429 return true;
432 /************************************************************************
433 Process v2 entries.
434 ************************************************************************/
436 static const char *create_pai_v1_entries(struct pai_val *paiv,
437 const char *entry_offset,
438 bool def_entry)
440 int i;
442 for (i = 0; i < paiv->num_entries; i++) {
443 struct pai_entry *paie = talloc(talloc_tos(), struct pai_entry);
444 if (!paie) {
445 return NULL;
448 paie->ace_flags = SEC_ACE_FLAG_INHERITED_ACE;
449 if (!get_pai_owner_type(paie, entry_offset)) {
450 TALLOC_FREE(paie);
451 return NULL;
454 if (!def_entry) {
455 DLIST_ADD(paiv->entry_list, paie);
456 } else {
457 DLIST_ADD(paiv->def_entry_list, paie);
459 entry_offset += PAI_V1_ENTRY_LENGTH;
461 return entry_offset;
464 /************************************************************************
465 Convert to in-memory format from version 1.
466 ************************************************************************/
468 static struct pai_val *create_pai_val_v1(const char *buf, size_t size)
470 const char *entry_offset;
471 struct pai_val *paiv = NULL;
473 if (!check_pai_ok_v1(buf, size)) {
474 return NULL;
477 paiv = talloc(talloc_tos(), struct pai_val);
478 if (!paiv) {
479 return NULL;
482 memset(paiv, '\0', sizeof(struct pai_val));
484 paiv->sd_type = (CVAL(buf,PAI_V1_FLAG_OFFSET) == PAI_V1_ACL_FLAG_PROTECTED) ?
485 SEC_DESC_DACL_PROTECTED : 0;
487 paiv->num_entries = SVAL(buf,PAI_V1_NUM_ENTRIES_OFFSET);
488 paiv->num_def_entries = SVAL(buf,PAI_V1_NUM_DEFAULT_ENTRIES_OFFSET);
490 entry_offset = buf + PAI_V1_ENTRIES_BASE;
492 DEBUG(10,("create_pai_val: num_entries = %u, num_def_entries = %u\n",
493 paiv->num_entries, paiv->num_def_entries ));
495 entry_offset = create_pai_v1_entries(paiv, entry_offset, false);
496 if (entry_offset == NULL) {
497 free_inherited_info(paiv);
498 return NULL;
500 entry_offset = create_pai_v1_entries(paiv, entry_offset, true);
501 if (entry_offset == NULL) {
502 free_inherited_info(paiv);
503 return NULL;
506 return paiv;
509 /************************************************************************
510 Process v2 entries.
511 ************************************************************************/
513 static const char *create_pai_v2_entries(struct pai_val *paiv,
514 unsigned int num_entries,
515 const char *entry_offset,
516 bool def_entry)
518 unsigned int i;
520 for (i = 0; i < num_entries; i++) {
521 struct pai_entry *paie = talloc(talloc_tos(), struct pai_entry);
522 if (!paie) {
523 return NULL;
526 paie->ace_flags = CVAL(entry_offset,0);
528 if (!get_pai_owner_type(paie, entry_offset+1)) {
529 TALLOC_FREE(paie);
530 return NULL;
532 if (!def_entry) {
533 DLIST_ADD(paiv->entry_list, paie);
534 } else {
535 DLIST_ADD(paiv->def_entry_list, paie);
537 entry_offset += PAI_V2_ENTRY_LENGTH;
539 return entry_offset;
542 /************************************************************************
543 Convert to in-memory format from version 2.
544 ************************************************************************/
546 static struct pai_val *create_pai_val_v2(const char *buf, size_t size)
548 const char *entry_offset;
549 struct pai_val *paiv = NULL;
551 if (!check_pai_ok_v2(buf, size)) {
552 return NULL;
555 paiv = talloc(talloc_tos(), struct pai_val);
556 if (!paiv) {
557 return NULL;
560 memset(paiv, '\0', sizeof(struct pai_val));
562 paiv->sd_type = SVAL(buf,PAI_V2_TYPE_OFFSET);
564 paiv->num_entries = SVAL(buf,PAI_V2_NUM_ENTRIES_OFFSET);
565 paiv->num_def_entries = SVAL(buf,PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET);
567 entry_offset = buf + PAI_V2_ENTRIES_BASE;
569 DEBUG(10,("create_pai_val_v2: sd_type = 0x%x num_entries = %u, num_def_entries = %u\n",
570 (unsigned int)paiv->sd_type,
571 paiv->num_entries, paiv->num_def_entries ));
573 entry_offset = create_pai_v2_entries(paiv, paiv->num_entries,
574 entry_offset, false);
575 if (entry_offset == NULL) {
576 free_inherited_info(paiv);
577 return NULL;
579 entry_offset = create_pai_v2_entries(paiv, paiv->num_def_entries,
580 entry_offset, true);
581 if (entry_offset == NULL) {
582 free_inherited_info(paiv);
583 return NULL;
586 return paiv;
589 /************************************************************************
590 Convert to in-memory format - from either version 1 or 2.
591 ************************************************************************/
593 static struct pai_val *create_pai_val(const char *buf, size_t size)
595 if (size < 1) {
596 return NULL;
598 if (CVAL(buf,PAI_VERSION_OFFSET) == PAI_V1_VERSION) {
599 return create_pai_val_v1(buf, size);
600 } else if (CVAL(buf,PAI_VERSION_OFFSET) == PAI_V2_VERSION) {
601 return create_pai_val_v2(buf, size);
602 } else {
603 return NULL;
607 /************************************************************************
608 Load the user.SAMBA_PAI attribute.
609 ************************************************************************/
611 static struct pai_val *fload_inherited_info(files_struct *fsp)
613 char *pai_buf;
614 size_t pai_buf_size = 1024;
615 struct pai_val *paiv = NULL;
616 ssize_t ret;
618 if (!lp_map_acl_inherit(SNUM(fsp->conn))) {
619 return NULL;
622 if ((pai_buf = talloc_array(talloc_tos(), char, pai_buf_size)) == NULL) {
623 return NULL;
626 do {
627 if (fsp->fh->fd != -1) {
628 ret = SMB_VFS_FGETXATTR(fsp, SAMBA_POSIX_INHERITANCE_EA_NAME,
629 pai_buf, pai_buf_size);
630 } else {
631 ret = SMB_VFS_GETXATTR(fsp->conn,
632 fsp->fsp_name->base_name,
633 SAMBA_POSIX_INHERITANCE_EA_NAME,
634 pai_buf, pai_buf_size);
637 if (ret == -1) {
638 if (errno != ERANGE) {
639 break;
641 /* Buffer too small - enlarge it. */
642 pai_buf_size *= 2;
643 TALLOC_FREE(pai_buf);
644 if (pai_buf_size > 1024*1024) {
645 return NULL; /* Limit malloc to 1mb. */
647 if ((pai_buf = talloc_array(talloc_tos(), char, pai_buf_size)) == NULL)
648 return NULL;
650 } while (ret == -1);
652 DEBUG(10,("load_inherited_info: ret = %lu for file %s\n",
653 (unsigned long)ret, fsp_str_dbg(fsp)));
655 if (ret == -1) {
656 /* No attribute or not supported. */
657 #if defined(ENOATTR)
658 if (errno != ENOATTR)
659 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno) ));
660 #else
661 if (errno != ENOSYS)
662 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno) ));
663 #endif
664 TALLOC_FREE(pai_buf);
665 return NULL;
668 paiv = create_pai_val(pai_buf, ret);
670 if (paiv) {
671 DEBUG(10,("load_inherited_info: ACL type is 0x%x for file %s\n",
672 (unsigned int)paiv->sd_type, fsp_str_dbg(fsp)));
675 TALLOC_FREE(pai_buf);
676 return paiv;
679 /************************************************************************
680 Load the user.SAMBA_PAI attribute.
681 ************************************************************************/
683 static struct pai_val *load_inherited_info(const struct connection_struct *conn,
684 const char *fname)
686 char *pai_buf;
687 size_t pai_buf_size = 1024;
688 struct pai_val *paiv = NULL;
689 ssize_t ret;
691 if (!lp_map_acl_inherit(SNUM(conn))) {
692 return NULL;
695 if ((pai_buf = talloc_array(talloc_tos(), char, pai_buf_size)) == NULL) {
696 return NULL;
699 do {
700 ret = SMB_VFS_GETXATTR(conn, fname,
701 SAMBA_POSIX_INHERITANCE_EA_NAME,
702 pai_buf, pai_buf_size);
704 if (ret == -1) {
705 if (errno != ERANGE) {
706 break;
708 /* Buffer too small - enlarge it. */
709 pai_buf_size *= 2;
710 TALLOC_FREE(pai_buf);
711 if (pai_buf_size > 1024*1024) {
712 return NULL; /* Limit malloc to 1mb. */
714 if ((pai_buf = talloc_array(talloc_tos(), char, pai_buf_size)) == NULL)
715 return NULL;
717 } while (ret == -1);
719 DEBUG(10,("load_inherited_info: ret = %lu for file %s\n", (unsigned long)ret, fname));
721 if (ret == -1) {
722 /* No attribute or not supported. */
723 #if defined(ENOATTR)
724 if (errno != ENOATTR)
725 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno) ));
726 #else
727 if (errno != ENOSYS)
728 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno) ));
729 #endif
730 TALLOC_FREE(pai_buf);
731 return NULL;
734 paiv = create_pai_val(pai_buf, ret);
736 if (paiv) {
737 DEBUG(10,("load_inherited_info: ACL type 0x%x for file %s\n",
738 (unsigned int)paiv->sd_type,
739 fname));
742 TALLOC_FREE(pai_buf);
743 return paiv;
746 /****************************************************************************
747 Functions to manipulate the internal ACE format.
748 ****************************************************************************/
750 /****************************************************************************
751 Count a linked list of canonical ACE entries.
752 ****************************************************************************/
754 static size_t count_canon_ace_list( canon_ace *l_head )
756 size_t count = 0;
757 canon_ace *ace;
759 for (ace = l_head; ace; ace = ace->next)
760 count++;
762 return count;
765 /****************************************************************************
766 Free a linked list of canonical ACE entries.
767 ****************************************************************************/
769 static void free_canon_ace_list( canon_ace *l_head )
771 canon_ace *list, *next;
773 for (list = l_head; list; list = next) {
774 next = list->next;
775 DLIST_REMOVE(l_head, list);
776 TALLOC_FREE(list);
780 /****************************************************************************
781 Function to duplicate a canon_ace entry.
782 ****************************************************************************/
784 static canon_ace *dup_canon_ace( canon_ace *src_ace)
786 canon_ace *dst_ace = talloc(talloc_tos(), canon_ace);
788 if (dst_ace == NULL)
789 return NULL;
791 *dst_ace = *src_ace;
792 dst_ace->prev = dst_ace->next = NULL;
793 return dst_ace;
796 /****************************************************************************
797 Print out a canon ace.
798 ****************************************************************************/
800 static void print_canon_ace(canon_ace *pace, int num)
802 dbgtext( "canon_ace index %d. Type = %s ", num, pace->attr == ALLOW_ACE ? "allow" : "deny" );
803 dbgtext( "SID = %s ", sid_string_dbg(&pace->trustee));
804 if (pace->owner_type == UID_ACE) {
805 const char *u_name = uidtoname(pace->unix_ug.id);
806 dbgtext( "uid %u (%s) ", (unsigned int)pace->unix_ug.id, u_name );
807 } else if (pace->owner_type == GID_ACE) {
808 char *g_name = gidtoname(pace->unix_ug.id);
809 dbgtext( "gid %u (%s) ", (unsigned int)pace->unix_ug.id, g_name );
810 } else
811 dbgtext( "other ");
812 switch (pace->type) {
813 case SMB_ACL_USER:
814 dbgtext( "SMB_ACL_USER ");
815 break;
816 case SMB_ACL_USER_OBJ:
817 dbgtext( "SMB_ACL_USER_OBJ ");
818 break;
819 case SMB_ACL_GROUP:
820 dbgtext( "SMB_ACL_GROUP ");
821 break;
822 case SMB_ACL_GROUP_OBJ:
823 dbgtext( "SMB_ACL_GROUP_OBJ ");
824 break;
825 case SMB_ACL_OTHER:
826 dbgtext( "SMB_ACL_OTHER ");
827 break;
828 default:
829 dbgtext( "MASK " );
830 break;
833 dbgtext( "ace_flags = 0x%x ", (unsigned int)pace->ace_flags);
834 dbgtext( "perms ");
835 dbgtext( "%c", pace->perms & S_IRUSR ? 'r' : '-');
836 dbgtext( "%c", pace->perms & S_IWUSR ? 'w' : '-');
837 dbgtext( "%c\n", pace->perms & S_IXUSR ? 'x' : '-');
840 /****************************************************************************
841 Print out a canon ace list.
842 ****************************************************************************/
844 static void print_canon_ace_list(const char *name, canon_ace *ace_list)
846 int count = 0;
848 if( DEBUGLVL( 10 )) {
849 dbgtext( "print_canon_ace_list: %s\n", name );
850 for (;ace_list; ace_list = ace_list->next, count++)
851 print_canon_ace(ace_list, count );
855 /****************************************************************************
856 Map POSIX ACL perms to canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits).
857 ****************************************************************************/
859 static mode_t convert_permset_to_mode_t(SMB_ACL_PERMSET_T permset)
861 mode_t ret = 0;
863 ret |= (sys_acl_get_perm(permset, SMB_ACL_READ) ? S_IRUSR : 0);
864 ret |= (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? S_IWUSR : 0);
865 ret |= (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? S_IXUSR : 0);
867 return ret;
870 /****************************************************************************
871 Map generic UNIX permissions to canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits).
872 ****************************************************************************/
874 static mode_t unix_perms_to_acl_perms(mode_t mode, int r_mask, int w_mask, int x_mask)
876 mode_t ret = 0;
878 if (mode & r_mask)
879 ret |= S_IRUSR;
880 if (mode & w_mask)
881 ret |= S_IWUSR;
882 if (mode & x_mask)
883 ret |= S_IXUSR;
885 return ret;
888 /****************************************************************************
889 Map canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits) to
890 an SMB_ACL_PERMSET_T.
891 ****************************************************************************/
893 static int map_acl_perms_to_permset(connection_struct *conn, mode_t mode, SMB_ACL_PERMSET_T *p_permset)
895 if (sys_acl_clear_perms(*p_permset) == -1)
896 return -1;
897 if (mode & S_IRUSR) {
898 if (sys_acl_add_perm(*p_permset, SMB_ACL_READ) == -1)
899 return -1;
901 if (mode & S_IWUSR) {
902 if (sys_acl_add_perm(*p_permset, SMB_ACL_WRITE) == -1)
903 return -1;
905 if (mode & S_IXUSR) {
906 if (sys_acl_add_perm(*p_permset, SMB_ACL_EXECUTE) == -1)
907 return -1;
909 return 0;
912 /****************************************************************************
913 Function to create owner and group SIDs from a SMB_STRUCT_STAT.
914 ****************************************************************************/
916 void create_file_sids(const SMB_STRUCT_STAT *psbuf, struct dom_sid *powner_sid, struct dom_sid *pgroup_sid)
918 uid_to_sid( powner_sid, psbuf->st_ex_uid );
919 gid_to_sid( pgroup_sid, psbuf->st_ex_gid );
922 /****************************************************************************
923 Merge aces with a common UID or GID - if both are allow or deny, OR the permissions together and
924 delete the second one. If the first is deny, mask the permissions off and delete the allow
925 if the permissions become zero, delete the deny if the permissions are non zero.
926 ****************************************************************************/
928 static void merge_aces( canon_ace **pp_list_head, bool dir_acl)
930 canon_ace *l_head = *pp_list_head;
931 canon_ace *curr_ace_outer;
932 canon_ace *curr_ace_outer_next;
935 * First, merge allow entries with identical SIDs, and deny entries
936 * with identical SIDs.
939 for (curr_ace_outer = l_head; curr_ace_outer; curr_ace_outer = curr_ace_outer_next) {
940 canon_ace *curr_ace;
941 canon_ace *curr_ace_next;
943 curr_ace_outer_next = curr_ace_outer->next; /* Save the link in case we delete. */
945 for (curr_ace = curr_ace_outer->next; curr_ace; curr_ace = curr_ace_next) {
946 bool can_merge = false;
948 curr_ace_next = curr_ace->next; /* Save the link in case of delete. */
950 /* For file ACLs we can merge if the SIDs and ALLOW/DENY
951 * types are the same. For directory acls we must also
952 * ensure the POSIX ACL types are the same.
954 * For the IDMAP_BOTH case, we must not merge
955 * the UID and GID ACE values for same SID
958 if (!dir_acl) {
959 can_merge = (curr_ace->unix_ug.id == curr_ace_outer->unix_ug.id &&
960 curr_ace->owner_type == curr_ace_outer->owner_type &&
961 (curr_ace->attr == curr_ace_outer->attr));
962 } else {
963 can_merge = (curr_ace->unix_ug.id == curr_ace_outer->unix_ug.id &&
964 curr_ace->owner_type == curr_ace_outer->owner_type &&
965 (curr_ace->type == curr_ace_outer->type) &&
966 (curr_ace->attr == curr_ace_outer->attr));
969 if (can_merge) {
970 if( DEBUGLVL( 10 )) {
971 dbgtext("merge_aces: Merging ACE's\n");
972 print_canon_ace( curr_ace_outer, 0);
973 print_canon_ace( curr_ace, 0);
976 /* Merge two allow or two deny ACE's. */
978 /* Theoretically we shouldn't merge a dir ACE if
979 * one ACE has the CI flag set, and the other
980 * ACE has the OI flag set, but this is rare
981 * enough we can ignore it. */
983 curr_ace_outer->perms |= curr_ace->perms;
984 curr_ace_outer->ace_flags |= curr_ace->ace_flags;
985 DLIST_REMOVE(l_head, curr_ace);
986 TALLOC_FREE(curr_ace);
987 curr_ace_outer_next = curr_ace_outer->next; /* We may have deleted the link. */
993 * Now go through and mask off allow permissions with deny permissions.
994 * We can delete either the allow or deny here as we know that each SID
995 * appears only once in the list.
998 for (curr_ace_outer = l_head; curr_ace_outer; curr_ace_outer = curr_ace_outer_next) {
999 canon_ace *curr_ace;
1000 canon_ace *curr_ace_next;
1002 curr_ace_outer_next = curr_ace_outer->next; /* Save the link in case we delete. */
1004 for (curr_ace = curr_ace_outer->next; curr_ace; curr_ace = curr_ace_next) {
1006 curr_ace_next = curr_ace->next; /* Save the link in case of delete. */
1009 * Subtract ACE's with different entries. Due to the ordering constraints
1010 * we've put on the ACL, we know the deny must be the first one.
1013 if (curr_ace->unix_ug.id == curr_ace_outer->unix_ug.id &&
1014 (curr_ace->owner_type == curr_ace_outer->owner_type) &&
1015 (curr_ace_outer->attr == DENY_ACE) && (curr_ace->attr == ALLOW_ACE)) {
1017 if( DEBUGLVL( 10 )) {
1018 dbgtext("merge_aces: Masking ACE's\n");
1019 print_canon_ace( curr_ace_outer, 0);
1020 print_canon_ace( curr_ace, 0);
1023 curr_ace->perms &= ~curr_ace_outer->perms;
1025 if (curr_ace->perms == 0) {
1028 * The deny overrides the allow. Remove the allow.
1031 DLIST_REMOVE(l_head, curr_ace);
1032 TALLOC_FREE(curr_ace);
1033 curr_ace_outer_next = curr_ace_outer->next; /* We may have deleted the link. */
1035 } else {
1038 * Even after removing permissions, there
1039 * are still allow permissions - delete the deny.
1040 * It is safe to delete the deny here,
1041 * as we are guarenteed by the deny first
1042 * ordering that all the deny entries for
1043 * this SID have already been merged into one
1044 * before we can get to an allow ace.
1047 DLIST_REMOVE(l_head, curr_ace_outer);
1048 TALLOC_FREE(curr_ace_outer);
1049 break;
1053 } /* end for curr_ace */
1054 } /* end for curr_ace_outer */
1056 /* We may have modified the list. */
1058 *pp_list_head = l_head;
1061 /****************************************************************************
1062 Check if we need to return NT4.x compatible ACL entries.
1063 ****************************************************************************/
1065 bool nt4_compatible_acls(void)
1067 int compat = lp_acl_compatibility();
1069 if (compat == ACL_COMPAT_AUTO) {
1070 enum remote_arch_types ra_type = get_remote_arch();
1072 /* Automatically adapt to client */
1073 return (ra_type <= RA_WINNT);
1074 } else
1075 return (compat == ACL_COMPAT_WINNT);
1079 /****************************************************************************
1080 Map canon_ace perms to permission bits NT.
1081 The attr element is not used here - we only process deny entries on set,
1082 not get. Deny entries are implicit on get with ace->perms = 0.
1083 ****************************************************************************/
1085 uint32_t map_canon_ace_perms(int snum,
1086 enum security_ace_type *pacl_type,
1087 mode_t perms,
1088 bool directory_ace)
1090 uint32_t nt_mask = 0;
1092 *pacl_type = SEC_ACE_TYPE_ACCESS_ALLOWED;
1094 if (lp_acl_map_full_control(snum) && ((perms & ALL_ACE_PERMS) == ALL_ACE_PERMS)) {
1095 if (directory_ace) {
1096 nt_mask = UNIX_DIRECTORY_ACCESS_RWX;
1097 } else {
1098 nt_mask = (UNIX_ACCESS_RWX & ~DELETE_ACCESS);
1100 } else if ((perms & ALL_ACE_PERMS) == (mode_t)0) {
1102 * Windows NT refuses to display ACEs with no permissions in them (but
1103 * they are perfectly legal with Windows 2000). If the ACE has empty
1104 * permissions we cannot use 0, so we use the otherwise unused
1105 * WRITE_OWNER permission, which we ignore when we set an ACL.
1106 * We abstract this into a #define of UNIX_ACCESS_NONE to allow this
1107 * to be changed in the future.
1110 if (nt4_compatible_acls())
1111 nt_mask = UNIX_ACCESS_NONE;
1112 else
1113 nt_mask = 0;
1114 } else {
1115 if (directory_ace) {
1116 nt_mask |= ((perms & S_IRUSR) ? UNIX_DIRECTORY_ACCESS_R : 0 );
1117 nt_mask |= ((perms & S_IWUSR) ? UNIX_DIRECTORY_ACCESS_W : 0 );
1118 nt_mask |= ((perms & S_IXUSR) ? UNIX_DIRECTORY_ACCESS_X : 0 );
1119 } else {
1120 nt_mask |= ((perms & S_IRUSR) ? UNIX_ACCESS_R : 0 );
1121 nt_mask |= ((perms & S_IWUSR) ? UNIX_ACCESS_W : 0 );
1122 nt_mask |= ((perms & S_IXUSR) ? UNIX_ACCESS_X : 0 );
1126 if ((perms & S_IWUSR) && lp_dos_filemode(snum)) {
1127 nt_mask |= (SEC_STD_WRITE_DAC|SEC_STD_WRITE_OWNER|DELETE_ACCESS);
1130 DEBUG(10,("map_canon_ace_perms: Mapped (UNIX) %x to (NT) %x\n",
1131 (unsigned int)perms, (unsigned int)nt_mask ));
1133 return nt_mask;
1136 /****************************************************************************
1137 Map NT perms to a UNIX mode_t.
1138 ****************************************************************************/
1140 #define FILE_SPECIFIC_READ_BITS (FILE_READ_DATA|FILE_READ_EA)
1141 #define FILE_SPECIFIC_WRITE_BITS (FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_WRITE_EA)
1142 #define FILE_SPECIFIC_EXECUTE_BITS (FILE_EXECUTE)
1144 static mode_t map_nt_perms( uint32 *mask, int type)
1146 mode_t mode = 0;
1148 switch(type) {
1149 case S_IRUSR:
1150 if((*mask) & GENERIC_ALL_ACCESS)
1151 mode = S_IRUSR|S_IWUSR|S_IXUSR;
1152 else {
1153 mode |= ((*mask) & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IRUSR : 0;
1154 mode |= ((*mask) & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWUSR : 0;
1155 mode |= ((*mask) & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXUSR : 0;
1157 break;
1158 case S_IRGRP:
1159 if((*mask) & GENERIC_ALL_ACCESS)
1160 mode = S_IRGRP|S_IWGRP|S_IXGRP;
1161 else {
1162 mode |= ((*mask) & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IRGRP : 0;
1163 mode |= ((*mask) & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWGRP : 0;
1164 mode |= ((*mask) & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXGRP : 0;
1166 break;
1167 case S_IROTH:
1168 if((*mask) & GENERIC_ALL_ACCESS)
1169 mode = S_IROTH|S_IWOTH|S_IXOTH;
1170 else {
1171 mode |= ((*mask) & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IROTH : 0;
1172 mode |= ((*mask) & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWOTH : 0;
1173 mode |= ((*mask) & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXOTH : 0;
1175 break;
1178 return mode;
1181 /****************************************************************************
1182 Unpack a struct security_descriptor into a UNIX owner and group.
1183 ****************************************************************************/
1185 NTSTATUS unpack_nt_owners(struct connection_struct *conn,
1186 uid_t *puser, gid_t *pgrp,
1187 uint32 security_info_sent, const struct
1188 security_descriptor *psd)
1190 struct dom_sid owner_sid;
1191 struct dom_sid grp_sid;
1193 *puser = (uid_t)-1;
1194 *pgrp = (gid_t)-1;
1196 if(security_info_sent == 0) {
1197 DEBUG(0,("unpack_nt_owners: no security info sent !\n"));
1198 return NT_STATUS_OK;
1202 * Validate the owner and group SID's.
1205 memset(&owner_sid, '\0', sizeof(owner_sid));
1206 memset(&grp_sid, '\0', sizeof(grp_sid));
1208 DEBUG(5,("unpack_nt_owners: validating owner_sids.\n"));
1211 * Don't immediately fail if the owner sid cannot be validated.
1212 * This may be a group chown only set.
1215 if (security_info_sent & SECINFO_OWNER) {
1216 sid_copy(&owner_sid, psd->owner_sid);
1217 if (!sid_to_uid(&owner_sid, puser)) {
1218 if (lp_force_unknown_acl_user(SNUM(conn))) {
1219 /* this allows take ownership to work
1220 * reasonably */
1221 *puser = get_current_uid(conn);
1222 } else {
1223 DEBUG(3,("unpack_nt_owners: unable to validate"
1224 " owner sid for %s\n",
1225 sid_string_dbg(&owner_sid)));
1226 return NT_STATUS_INVALID_OWNER;
1229 DEBUG(3,("unpack_nt_owners: owner sid mapped to uid %u\n",
1230 (unsigned int)*puser ));
1234 * Don't immediately fail if the group sid cannot be validated.
1235 * This may be an owner chown only set.
1238 if (security_info_sent & SECINFO_GROUP) {
1239 sid_copy(&grp_sid, psd->group_sid);
1240 if (!sid_to_gid( &grp_sid, pgrp)) {
1241 if (lp_force_unknown_acl_user(SNUM(conn))) {
1242 /* this allows take group ownership to work
1243 * reasonably */
1244 *pgrp = get_current_gid(conn);
1245 } else {
1246 DEBUG(3,("unpack_nt_owners: unable to validate"
1247 " group sid.\n"));
1248 return NT_STATUS_INVALID_OWNER;
1251 DEBUG(3,("unpack_nt_owners: group sid mapped to gid %u\n",
1252 (unsigned int)*pgrp));
1255 DEBUG(5,("unpack_nt_owners: owner_sids validated.\n"));
1257 return NT_STATUS_OK;
1260 /****************************************************************************
1261 Ensure the enforced permissions for this share apply.
1262 ****************************************************************************/
1264 static void apply_default_perms(const struct share_params *params,
1265 const bool is_directory, canon_ace *pace,
1266 mode_t type)
1268 mode_t and_bits = (mode_t)0;
1269 mode_t or_bits = (mode_t)0;
1271 /* Get the initial bits to apply. */
1273 if (is_directory) {
1274 and_bits = lp_dir_mask(params->service);
1275 or_bits = lp_force_dir_mode(params->service);
1276 } else {
1277 and_bits = lp_create_mask(params->service);
1278 or_bits = lp_force_create_mode(params->service);
1281 /* Now bounce them into the S_USR space. */
1282 switch(type) {
1283 case S_IRUSR:
1284 /* Ensure owner has read access. */
1285 pace->perms |= S_IRUSR;
1286 if (is_directory)
1287 pace->perms |= (S_IWUSR|S_IXUSR);
1288 and_bits = unix_perms_to_acl_perms(and_bits, S_IRUSR, S_IWUSR, S_IXUSR);
1289 or_bits = unix_perms_to_acl_perms(or_bits, S_IRUSR, S_IWUSR, S_IXUSR);
1290 break;
1291 case S_IRGRP:
1292 and_bits = unix_perms_to_acl_perms(and_bits, S_IRGRP, S_IWGRP, S_IXGRP);
1293 or_bits = unix_perms_to_acl_perms(or_bits, S_IRGRP, S_IWGRP, S_IXGRP);
1294 break;
1295 case S_IROTH:
1296 and_bits = unix_perms_to_acl_perms(and_bits, S_IROTH, S_IWOTH, S_IXOTH);
1297 or_bits = unix_perms_to_acl_perms(or_bits, S_IROTH, S_IWOTH, S_IXOTH);
1298 break;
1301 pace->perms = ((pace->perms & and_bits)|or_bits);
1304 /****************************************************************************
1305 Check if a given uid/SID is in a group gid/SID. This is probably very
1306 expensive and will need optimisation. A *lot* of optimisation :-). JRA.
1307 ****************************************************************************/
1309 static bool uid_entry_in_group(connection_struct *conn, canon_ace *uid_ace, canon_ace *group_ace )
1311 /* "Everyone" always matches every uid. */
1313 if (dom_sid_equal(&group_ace->trustee, &global_sid_World))
1314 return True;
1317 * if it's the current user, we already have the unix token
1318 * and don't need to do the complex user_in_group_sid() call
1320 if (uid_ace->unix_ug.id == get_current_uid(conn)) {
1321 const struct security_unix_token *curr_utok = NULL;
1322 size_t i;
1324 if (group_ace->unix_ug.id == get_current_gid(conn)) {
1325 return True;
1328 curr_utok = get_current_utok(conn);
1329 for (i=0; i < curr_utok->ngroups; i++) {
1330 if (group_ace->unix_ug.id == curr_utok->groups[i]) {
1331 return True;
1337 * user_in_group_sid() uses create_token_from_sid()
1338 * which creates an artificial NT token given just a username,
1339 * so this is not reliable for users from foreign domains
1340 * exported by winbindd!
1342 return user_sid_in_group_sid(&uid_ace->trustee, &group_ace->trustee);
1345 /****************************************************************************
1346 A well formed POSIX file or default ACL has at least 3 entries, a
1347 SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ.
1348 In addition, the owner must always have at least read access.
1349 When using this call on get_acl, the pst struct is valid and contains
1350 the mode of the file.
1351 ****************************************************************************/
1353 static bool ensure_canon_entry_valid_on_get(connection_struct *conn,
1354 canon_ace **pp_ace,
1355 const struct dom_sid *pfile_owner_sid,
1356 const struct dom_sid *pfile_grp_sid,
1357 const SMB_STRUCT_STAT *pst)
1359 canon_ace *pace;
1360 bool got_user = false;
1361 bool got_group = false;
1362 bool got_other = false;
1364 for (pace = *pp_ace; pace; pace = pace->next) {
1365 if (pace->type == SMB_ACL_USER_OBJ) {
1366 got_user = true;
1367 } else if (pace->type == SMB_ACL_GROUP_OBJ) {
1368 got_group = true;
1369 } else if (pace->type == SMB_ACL_OTHER) {
1370 got_other = true;
1374 if (!got_user) {
1375 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1376 DEBUG(0,("malloc fail.\n"));
1377 return false;
1380 ZERO_STRUCTP(pace);
1381 pace->type = SMB_ACL_USER_OBJ;
1382 pace->owner_type = UID_ACE;
1383 pace->unix_ug.type = ID_TYPE_UID;
1384 pace->unix_ug.id = pst->st_ex_uid;
1385 pace->trustee = *pfile_owner_sid;
1386 pace->attr = ALLOW_ACE;
1387 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IRUSR, S_IWUSR, S_IXUSR);
1388 DLIST_ADD(*pp_ace, pace);
1391 if (!got_group) {
1392 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1393 DEBUG(0,("malloc fail.\n"));
1394 return false;
1397 ZERO_STRUCTP(pace);
1398 pace->type = SMB_ACL_GROUP_OBJ;
1399 pace->owner_type = GID_ACE;
1400 pace->unix_ug.type = ID_TYPE_GID;
1401 pace->unix_ug.id = pst->st_ex_gid;
1402 pace->trustee = *pfile_grp_sid;
1403 pace->attr = ALLOW_ACE;
1404 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IRGRP, S_IWGRP, S_IXGRP);
1405 DLIST_ADD(*pp_ace, pace);
1408 if (!got_other) {
1409 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1410 DEBUG(0,("malloc fail.\n"));
1411 return false;
1414 ZERO_STRUCTP(pace);
1415 pace->type = SMB_ACL_OTHER;
1416 pace->owner_type = WORLD_ACE;
1417 pace->unix_ug.type = ID_TYPE_NOT_SPECIFIED;
1418 pace->unix_ug.id = -1;
1419 pace->trustee = global_sid_World;
1420 pace->attr = ALLOW_ACE;
1421 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IROTH, S_IWOTH, S_IXOTH);
1422 DLIST_ADD(*pp_ace, pace);
1425 return true;
1428 /****************************************************************************
1429 A well formed POSIX file or default ACL has at least 3 entries, a
1430 SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ.
1431 In addition, the owner must always have at least read access.
1432 When using this call on set_acl, the pst struct has
1433 been modified to have a mode containing the default for this file or directory
1434 type.
1435 ****************************************************************************/
1437 static bool ensure_canon_entry_valid_on_set(connection_struct *conn,
1438 canon_ace **pp_ace,
1439 bool is_default_acl,
1440 const struct share_params *params,
1441 const bool is_directory,
1442 const struct dom_sid *pfile_owner_sid,
1443 const struct dom_sid *pfile_grp_sid,
1444 const SMB_STRUCT_STAT *pst)
1446 canon_ace *pace;
1447 canon_ace *pace_user = NULL;
1448 canon_ace *pace_group = NULL;
1449 canon_ace *pace_other = NULL;
1450 bool got_duplicate_user = false;
1451 bool got_duplicate_group = false;
1453 for (pace = *pp_ace; pace; pace = pace->next) {
1454 if (pace->type == SMB_ACL_USER_OBJ) {
1456 if (!is_default_acl) {
1457 apply_default_perms(params, is_directory, pace, S_IRUSR);
1459 pace_user = pace;
1461 } else if (pace->type == SMB_ACL_GROUP_OBJ) {
1464 * Ensure create mask/force create mode is respected on set.
1467 if (!is_default_acl) {
1468 apply_default_perms(params, is_directory, pace, S_IRGRP);
1470 pace_group = pace;
1472 } else if (pace->type == SMB_ACL_OTHER) {
1475 * Ensure create mask/force create mode is respected on set.
1478 if (!is_default_acl) {
1479 apply_default_perms(params, is_directory, pace, S_IROTH);
1481 pace_other = pace;
1483 } else if (pace->type == SMB_ACL_USER || pace->type == SMB_ACL_GROUP) {
1486 * Ensure create mask/force create mode is respected on set.
1489 if (!is_default_acl) {
1490 apply_default_perms(params, is_directory, pace, S_IRGRP);
1495 if (!pace_user) {
1496 canon_ace *pace_iter;
1498 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1499 DEBUG(0,("talloc fail.\n"));
1500 return false;
1503 ZERO_STRUCTP(pace);
1504 pace->type = SMB_ACL_USER_OBJ;
1505 pace->owner_type = UID_ACE;
1506 pace->unix_ug.type = ID_TYPE_UID;
1507 pace->unix_ug.id = pst->st_ex_uid;
1508 pace->trustee = *pfile_owner_sid;
1509 pace->attr = ALLOW_ACE;
1510 /* Start with existing user permissions, principle of least
1511 surprises for the user. */
1512 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IRUSR, S_IWUSR, S_IXUSR);
1514 /* See if the owning user is in any of the other groups in
1515 the ACE, or if there's a matching user entry (by uid
1516 or in the case of ID_TYPE_BOTH by SID).
1517 If so, OR in the permissions from that entry. */
1520 for (pace_iter = *pp_ace; pace_iter; pace_iter = pace_iter->next) {
1521 if (pace_iter->type == SMB_ACL_USER &&
1522 pace_iter->unix_ug.id == pace->unix_ug.id) {
1523 pace->perms |= pace_iter->perms;
1524 } else if (pace_iter->type == SMB_ACL_GROUP_OBJ || pace_iter->type == SMB_ACL_GROUP) {
1525 if (dom_sid_equal(&pace->trustee, &pace_iter->trustee)) {
1526 pace->perms |= pace_iter->perms;
1527 } else if (uid_entry_in_group(conn, pace, pace_iter)) {
1528 pace->perms |= pace_iter->perms;
1533 if (pace->perms == 0) {
1534 /* If we only got an "everyone" perm, just use that. */
1535 if (pace_other)
1536 pace->perms = pace_other->perms;
1539 if (!is_default_acl) {
1540 apply_default_perms(params, is_directory, pace, S_IRUSR);
1543 DLIST_ADD(*pp_ace, pace);
1544 pace_user = pace;
1547 if (!pace_group) {
1548 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1549 DEBUG(0,("talloc fail.\n"));
1550 return false;
1553 ZERO_STRUCTP(pace);
1554 pace->type = SMB_ACL_GROUP_OBJ;
1555 pace->owner_type = GID_ACE;
1556 pace->unix_ug.type = ID_TYPE_GID;
1557 pace->unix_ug.id = pst->st_ex_gid;
1558 pace->trustee = *pfile_grp_sid;
1559 pace->attr = ALLOW_ACE;
1561 /* If we only got an "everyone" perm, just use that. */
1562 if (pace_other) {
1563 pace->perms = pace_other->perms;
1564 } else {
1565 pace->perms = 0;
1567 if (!is_default_acl) {
1568 apply_default_perms(params, is_directory, pace, S_IRGRP);
1571 DLIST_ADD(*pp_ace, pace);
1572 pace_group = pace;
1575 if (!pace_other) {
1576 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1577 DEBUG(0,("talloc fail.\n"));
1578 return false;
1581 ZERO_STRUCTP(pace);
1582 pace->type = SMB_ACL_OTHER;
1583 pace->owner_type = WORLD_ACE;
1584 pace->unix_ug.type = ID_TYPE_NOT_SPECIFIED;
1585 pace->unix_ug.id = -1;
1586 pace->trustee = global_sid_World;
1587 pace->attr = ALLOW_ACE;
1588 pace->perms = 0;
1589 if (!is_default_acl) {
1590 apply_default_perms(params, is_directory, pace, S_IROTH);
1593 DLIST_ADD(*pp_ace, pace);
1594 pace_other = pace;
1597 /* Ensure when setting a POSIX ACL, that the uid for a
1598 SMB_ACL_USER_OBJ ACE (the owner ACE entry) has a duplicate
1599 permission entry as an SMB_ACL_USER, and a gid for a
1600 SMB_ACL_GROUP_OBJ ACE (the primary group ACE entry) also has
1601 a duplicate permission entry as an SMB_ACL_GROUP. If not,
1602 then if the ownership or group ownership of this file or
1603 directory gets changed, the user or group can lose their
1604 access. */
1606 for (pace = *pp_ace; pace; pace = pace->next) {
1607 if (pace->type == SMB_ACL_USER &&
1608 pace->unix_ug.id == pace_user->unix_ug.id) {
1609 /* Already got one. */
1610 got_duplicate_user = true;
1611 } else if (pace->type == SMB_ACL_GROUP &&
1612 pace->unix_ug.id == pace_group->unix_ug.id) {
1613 /* Already got one. */
1614 got_duplicate_group = true;
1615 } else if ((pace->type == SMB_ACL_GROUP)
1616 && (dom_sid_equal(&pace->trustee, &pace_user->trustee))) {
1617 /* If the SID owning the file appears
1618 * in a group entry, then we have
1619 * enough duplication, they will still
1620 * have access */
1621 got_duplicate_user = true;
1625 /* If the SID is equal for the user and group that we need
1626 to add the duplicate for, add only the group */
1627 if (!got_duplicate_user && !got_duplicate_group
1628 && dom_sid_equal(&pace_group->trustee,
1629 &pace_user->trustee)) {
1630 /* Add a duplicate SMB_ACL_GROUP entry, this
1631 * will cover the owning SID as well, as it
1632 * will always be mapped to both a uid and
1633 * gid. */
1635 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1636 DEBUG(0,("talloc fail.\n"));
1637 return false;
1640 ZERO_STRUCTP(pace);
1641 pace->type = SMB_ACL_GROUP;;
1642 pace->owner_type = GID_ACE;
1643 pace->unix_ug.type = ID_TYPE_GID;
1644 pace->unix_ug.id = pace_group->unix_ug.id;
1645 pace->trustee = pace_group->trustee;
1646 pace->attr = pace_group->attr;
1647 pace->perms = pace_group->perms;
1649 DLIST_ADD(*pp_ace, pace);
1651 /* We're done here, make sure the
1652 statements below are not executed. */
1653 got_duplicate_user = true;
1654 got_duplicate_group = true;
1657 if (!got_duplicate_user) {
1658 /* Add a duplicate SMB_ACL_USER entry. */
1659 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1660 DEBUG(0,("talloc fail.\n"));
1661 return false;
1664 ZERO_STRUCTP(pace);
1665 pace->type = SMB_ACL_USER;;
1666 pace->owner_type = UID_ACE;
1667 pace->unix_ug.type = ID_TYPE_UID;
1668 pace->unix_ug.id = pace_user->unix_ug.id;
1669 pace->trustee = pace_user->trustee;
1670 pace->attr = pace_user->attr;
1671 pace->perms = pace_user->perms;
1673 DLIST_ADD(*pp_ace, pace);
1675 got_duplicate_user = true;
1678 if (!got_duplicate_group) {
1679 /* Add a duplicate SMB_ACL_GROUP entry. */
1680 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1681 DEBUG(0,("talloc fail.\n"));
1682 return false;
1685 ZERO_STRUCTP(pace);
1686 pace->type = SMB_ACL_GROUP;;
1687 pace->owner_type = GID_ACE;
1688 pace->unix_ug.type = ID_TYPE_GID;
1689 pace->unix_ug.id = pace_group->unix_ug.id;
1690 pace->trustee = pace_group->trustee;
1691 pace->attr = pace_group->attr;
1692 pace->perms = pace_group->perms;
1694 DLIST_ADD(*pp_ace, pace);
1696 got_duplicate_group = true;
1699 return true;
1702 /****************************************************************************
1703 Check if a POSIX ACL has the required SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ entries.
1704 If it does not have them, check if there are any entries where the trustee is the
1705 file owner or the owning group, and map these to SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ.
1706 Note we must not do this to default directory ACLs.
1707 ****************************************************************************/
1709 static void check_owning_objs(canon_ace *ace, struct dom_sid *pfile_owner_sid, struct dom_sid *pfile_grp_sid)
1711 bool got_user_obj, got_group_obj;
1712 canon_ace *current_ace;
1713 int i, entries;
1715 entries = count_canon_ace_list(ace);
1716 got_user_obj = False;
1717 got_group_obj = False;
1719 for (i=0, current_ace = ace; i < entries; i++, current_ace = current_ace->next) {
1720 if (current_ace->type == SMB_ACL_USER_OBJ)
1721 got_user_obj = True;
1722 else if (current_ace->type == SMB_ACL_GROUP_OBJ)
1723 got_group_obj = True;
1725 if (got_user_obj && got_group_obj) {
1726 DEBUG(10,("check_owning_objs: ACL had owning user/group entries.\n"));
1727 return;
1730 for (i=0, current_ace = ace; i < entries; i++, current_ace = current_ace->next) {
1731 if (!got_user_obj && current_ace->owner_type == UID_ACE &&
1732 dom_sid_equal(&current_ace->trustee, pfile_owner_sid)) {
1733 current_ace->type = SMB_ACL_USER_OBJ;
1734 got_user_obj = True;
1736 if (!got_group_obj && current_ace->owner_type == GID_ACE &&
1737 dom_sid_equal(&current_ace->trustee, pfile_grp_sid)) {
1738 current_ace->type = SMB_ACL_GROUP_OBJ;
1739 got_group_obj = True;
1742 if (!got_user_obj)
1743 DEBUG(10,("check_owning_objs: ACL is missing an owner entry.\n"));
1744 if (!got_group_obj)
1745 DEBUG(10,("check_owning_objs: ACL is missing an owning group entry.\n"));
1748 static bool add_current_ace_to_acl(files_struct *fsp, struct security_ace *psa,
1749 canon_ace **file_ace, canon_ace **dir_ace,
1750 bool *got_file_allow, bool *got_dir_allow,
1751 bool *all_aces_are_inherit_only,
1752 canon_ace *current_ace)
1756 * Map the given NT permissions into a UNIX mode_t containing only
1757 * S_I(R|W|X)USR bits.
1760 current_ace->perms |= map_nt_perms( &psa->access_mask, S_IRUSR);
1761 current_ace->attr = (psa->type == SEC_ACE_TYPE_ACCESS_ALLOWED) ? ALLOW_ACE : DENY_ACE;
1763 /* Store the ace_flag. */
1764 current_ace->ace_flags = psa->flags;
1767 * Now add the created ace to either the file list, the directory
1768 * list, or both. We *MUST* preserve the order here (hence we use
1769 * DLIST_ADD_END) as NT ACLs are order dependent.
1772 if (fsp->is_directory) {
1775 * We can only add to the default POSIX ACE list if the ACE is
1776 * designed to be inherited by both files and directories.
1779 if ((psa->flags & (SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT)) ==
1780 (SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT)) {
1782 canon_ace *current_dir_ace = current_ace;
1783 DLIST_ADD_END(*dir_ace, current_ace, canon_ace *);
1786 * Note if this was an allow ace. We can't process
1787 * any further deny ace's after this.
1790 if (current_ace->attr == ALLOW_ACE)
1791 *got_dir_allow = True;
1793 if ((current_ace->attr == DENY_ACE) && *got_dir_allow) {
1794 DEBUG(0,("add_current_ace_to_acl: "
1795 "malformed ACL in "
1796 "inheritable ACL! Deny entry "
1797 "after Allow entry. Failing "
1798 "to set on file %s.\n",
1799 fsp_str_dbg(fsp)));
1800 return False;
1803 if( DEBUGLVL( 10 )) {
1804 dbgtext("add_current_ace_to_acl: adding dir ACL:\n");
1805 print_canon_ace( current_ace, 0);
1809 * If this is not an inherit only ACE we need to add a duplicate
1810 * to the file acl.
1813 if (!(psa->flags & SEC_ACE_FLAG_INHERIT_ONLY)) {
1814 canon_ace *dup_ace = dup_canon_ace(current_ace);
1816 if (!dup_ace) {
1817 DEBUG(0,("add_current_ace_to_acl: malloc fail !\n"));
1818 return False;
1822 * We must not free current_ace here as its
1823 * pointer is now owned by the dir_ace list.
1825 current_ace = dup_ace;
1826 /* We've essentially split this ace into two,
1827 * and added the ace with inheritance request
1828 * bits to the directory ACL. Drop those bits for
1829 * the ACE we're adding to the file list. */
1830 current_ace->ace_flags &= ~(SEC_ACE_FLAG_OBJECT_INHERIT|
1831 SEC_ACE_FLAG_CONTAINER_INHERIT|
1832 SEC_ACE_FLAG_INHERIT_ONLY);
1833 } else {
1835 * We must not free current_ace here as its
1836 * pointer is now owned by the dir_ace list.
1838 current_ace = NULL;
1842 * current_ace is now either owned by file_ace
1843 * or is NULL. We can safely operate on current_dir_ace
1844 * to treat mapping for default acl entries differently
1845 * than access acl entries.
1848 if (current_dir_ace->owner_type == UID_ACE) {
1850 * We already decided above this is a uid,
1851 * for default acls ace's only CREATOR_OWNER
1852 * maps to ACL_USER_OBJ. All other uid
1853 * ace's are ACL_USER.
1855 if (dom_sid_equal(&current_dir_ace->trustee,
1856 &global_sid_Creator_Owner)) {
1857 current_dir_ace->type = SMB_ACL_USER_OBJ;
1858 } else {
1859 current_dir_ace->type = SMB_ACL_USER;
1863 if (current_dir_ace->owner_type == GID_ACE) {
1865 * We already decided above this is a gid,
1866 * for default acls ace's only CREATOR_GROUP
1867 * maps to ACL_GROUP_OBJ. All other uid
1868 * ace's are ACL_GROUP.
1870 if (dom_sid_equal(&current_dir_ace->trustee,
1871 &global_sid_Creator_Group)) {
1872 current_dir_ace->type = SMB_ACL_GROUP_OBJ;
1873 } else {
1874 current_dir_ace->type = SMB_ACL_GROUP;
1881 * Only add to the file ACL if not inherit only.
1884 if (current_ace && !(psa->flags & SEC_ACE_FLAG_INHERIT_ONLY)) {
1885 DLIST_ADD_END(*file_ace, current_ace, canon_ace *);
1888 * Note if this was an allow ace. We can't process
1889 * any further deny ace's after this.
1892 if (current_ace->attr == ALLOW_ACE)
1893 *got_file_allow = True;
1895 if ((current_ace->attr == DENY_ACE) && got_file_allow) {
1896 DEBUG(0,("add_current_ace_to_acl: malformed "
1897 "ACL in file ACL ! Deny entry after "
1898 "Allow entry. Failing to set on file "
1899 "%s.\n", fsp_str_dbg(fsp)));
1900 return False;
1903 if( DEBUGLVL( 10 )) {
1904 dbgtext("add_current_ace_to_acl: adding file ACL:\n");
1905 print_canon_ace( current_ace, 0);
1907 *all_aces_are_inherit_only = False;
1909 * We must not free current_ace here as its
1910 * pointer is now owned by the file_ace list.
1912 current_ace = NULL;
1916 * Free if ACE was not added.
1919 TALLOC_FREE(current_ace);
1920 return true;
1923 /****************************************************************************
1924 Unpack a struct security_descriptor into two canonical ace lists.
1925 ****************************************************************************/
1927 static bool create_canon_ace_lists(files_struct *fsp,
1928 const SMB_STRUCT_STAT *pst,
1929 struct dom_sid *pfile_owner_sid,
1930 struct dom_sid *pfile_grp_sid,
1931 canon_ace **ppfile_ace,
1932 canon_ace **ppdir_ace,
1933 const struct security_acl *dacl)
1935 bool all_aces_are_inherit_only = (fsp->is_directory ? True : False);
1936 canon_ace *file_ace = NULL;
1937 canon_ace *dir_ace = NULL;
1938 canon_ace *current_ace = NULL;
1939 bool got_dir_allow = False;
1940 bool got_file_allow = False;
1941 int i, j;
1943 *ppfile_ace = NULL;
1944 *ppdir_ace = NULL;
1947 * Convert the incoming ACL into a more regular form.
1950 for(i = 0; i < dacl->num_aces; i++) {
1951 struct security_ace *psa = &dacl->aces[i];
1953 if((psa->type != SEC_ACE_TYPE_ACCESS_ALLOWED) && (psa->type != SEC_ACE_TYPE_ACCESS_DENIED)) {
1954 DEBUG(3,("create_canon_ace_lists: unable to set anything but an ALLOW or DENY ACE.\n"));
1955 return False;
1958 if (nt4_compatible_acls()) {
1960 * The security mask may be UNIX_ACCESS_NONE which should map into
1961 * no permissions (we overload the WRITE_OWNER bit for this) or it
1962 * should be one of the ALL/EXECUTE/READ/WRITE bits. Arrange for this
1963 * to be so. Any other bits override the UNIX_ACCESS_NONE bit.
1967 * Convert GENERIC bits to specific bits.
1970 se_map_generic(&psa->access_mask, &file_generic_mapping);
1972 psa->access_mask &= (UNIX_ACCESS_NONE|FILE_ALL_ACCESS);
1974 if(psa->access_mask != UNIX_ACCESS_NONE)
1975 psa->access_mask &= ~UNIX_ACCESS_NONE;
1980 * Deal with the fact that NT 4.x re-writes the canonical format
1981 * that we return for default ACLs. If a directory ACE is identical
1982 * to a inherited directory ACE then NT changes the bits so that the
1983 * first ACE is set to OI|IO and the second ACE for this SID is set
1984 * to CI. We need to repair this. JRA.
1987 for(i = 0; i < dacl->num_aces; i++) {
1988 struct security_ace *psa1 = &dacl->aces[i];
1990 for (j = i + 1; j < dacl->num_aces; j++) {
1991 struct security_ace *psa2 = &dacl->aces[j];
1993 if (psa1->access_mask != psa2->access_mask)
1994 continue;
1996 if (!dom_sid_equal(&psa1->trustee, &psa2->trustee))
1997 continue;
2000 * Ok - permission bits and SIDs are equal.
2001 * Check if flags were re-written.
2004 if (psa1->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
2006 psa1->flags |= (psa2->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT));
2007 psa2->flags &= ~(SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT);
2009 } else if (psa2->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
2011 psa2->flags |= (psa1->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT));
2012 psa1->flags &= ~(SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT);
2018 for(i = 0; i < dacl->num_aces; i++) {
2019 struct security_ace *psa = &dacl->aces[i];
2022 * Create a canon_ace entry representing this NT DACL ACE.
2025 if ((current_ace = talloc(talloc_tos(), canon_ace)) == NULL) {
2026 free_canon_ace_list(file_ace);
2027 free_canon_ace_list(dir_ace);
2028 DEBUG(0,("create_canon_ace_lists: malloc fail.\n"));
2029 return False;
2032 ZERO_STRUCTP(current_ace);
2034 sid_copy(&current_ace->trustee, &psa->trustee);
2037 * Try and work out if the SID is a user or group
2038 * as we need to flag these differently for POSIX.
2039 * Note what kind of a POSIX ACL this should map to.
2042 if( dom_sid_equal(&current_ace->trustee, &global_sid_World)) {
2043 current_ace->owner_type = WORLD_ACE;
2044 current_ace->unix_ug.type = ID_TYPE_NOT_SPECIFIED;
2045 current_ace->unix_ug.id = -1;
2046 current_ace->type = SMB_ACL_OTHER;
2047 } else if (dom_sid_equal(&current_ace->trustee, &global_sid_Creator_Owner)) {
2048 current_ace->owner_type = UID_ACE;
2049 current_ace->unix_ug.type = ID_TYPE_UID;
2050 current_ace->unix_ug.id = pst->st_ex_uid;
2051 current_ace->type = SMB_ACL_USER_OBJ;
2054 * The Creator Owner entry only specifies inheritable permissions,
2055 * never access permissions. WinNT doesn't always set the ACE to
2056 * INHERIT_ONLY, though.
2059 psa->flags |= SEC_ACE_FLAG_INHERIT_ONLY;
2061 } else if (dom_sid_equal(&current_ace->trustee, &global_sid_Creator_Group)) {
2062 current_ace->owner_type = GID_ACE;
2063 current_ace->unix_ug.type = ID_TYPE_GID;
2064 current_ace->unix_ug.id = pst->st_ex_gid;
2065 current_ace->type = SMB_ACL_GROUP_OBJ;
2068 * The Creator Group entry only specifies inheritable permissions,
2069 * never access permissions. WinNT doesn't always set the ACE to
2070 * INHERIT_ONLY, though.
2072 psa->flags |= SEC_ACE_FLAG_INHERIT_ONLY;
2074 } else {
2075 struct unixid unixid;
2077 if (!sids_to_unixids(&current_ace->trustee, 1, &unixid)) {
2078 free_canon_ace_list(file_ace);
2079 free_canon_ace_list(dir_ace);
2080 TALLOC_FREE(current_ace);
2081 DEBUG(0, ("create_canon_ace_lists: sids_to_unixids "
2082 "failed for %s (allocation failure)\n",
2083 sid_string_dbg(&current_ace->trustee)));
2084 return false;
2087 if (unixid.type == ID_TYPE_BOTH) {
2088 /* If it's the owning user, this is a
2089 * user_obj, not a user. This way, we
2090 * get a valid ACL for groups that own
2091 * files, without putting user ACL
2092 * entries in for groups otherwise */
2093 if (unixid.id == pst->st_ex_uid) {
2094 current_ace->owner_type = UID_ACE;
2095 current_ace->unix_ug.type = ID_TYPE_UID;
2096 current_ace->unix_ug.id = unixid.id;
2097 current_ace->type = SMB_ACL_USER_OBJ;
2099 /* Add the user object to the posix ACL,
2100 and proceed to the group mapping
2101 below. This handles the talloc_free
2102 of current_ace if not added for some
2103 reason */
2104 if (!add_current_ace_to_acl(fsp,
2105 psa,
2106 &file_ace,
2107 &dir_ace,
2108 &got_file_allow,
2109 &got_dir_allow,
2110 &all_aces_are_inherit_only,
2111 current_ace)) {
2112 free_canon_ace_list(file_ace);
2113 free_canon_ace_list(dir_ace);
2114 return false;
2117 if ((current_ace = talloc(talloc_tos(),
2118 canon_ace)) == NULL) {
2119 free_canon_ace_list(file_ace);
2120 free_canon_ace_list(dir_ace);
2121 DEBUG(0,("create_canon_ace_lists: "
2122 "malloc fail.\n"));
2123 return False;
2126 ZERO_STRUCTP(current_ace);
2129 sid_copy(&current_ace->trustee, &psa->trustee);
2131 current_ace->unix_ug.type = ID_TYPE_GID;
2132 current_ace->unix_ug.id = unixid.id;
2133 current_ace->owner_type = GID_ACE;
2134 /* If it's the primary group, this is a
2135 group_obj, not a group. */
2136 if (current_ace->unix_ug.id == pst->st_ex_gid) {
2137 current_ace->type = SMB_ACL_GROUP_OBJ;
2138 } else {
2139 current_ace->type = SMB_ACL_GROUP;
2142 } else if (unixid.type == ID_TYPE_UID) {
2143 current_ace->owner_type = UID_ACE;
2144 current_ace->unix_ug.type = ID_TYPE_UID;
2145 current_ace->unix_ug.id = unixid.id;
2146 /* If it's the owning user, this is a user_obj,
2147 not a user. */
2148 if (current_ace->unix_ug.id == pst->st_ex_uid) {
2149 current_ace->type = SMB_ACL_USER_OBJ;
2150 } else {
2151 current_ace->type = SMB_ACL_USER;
2153 } else if (unixid.type == ID_TYPE_GID) {
2154 current_ace->unix_ug.type = ID_TYPE_GID;
2155 current_ace->unix_ug.id = unixid.id;
2156 current_ace->owner_type = GID_ACE;
2157 /* If it's the primary group, this is a
2158 group_obj, not a group. */
2159 if (current_ace->unix_ug.id == pst->st_ex_gid) {
2160 current_ace->type = SMB_ACL_GROUP_OBJ;
2161 } else {
2162 current_ace->type = SMB_ACL_GROUP;
2164 } else {
2166 * Silently ignore map failures in non-mappable SIDs (NT Authority, BUILTIN etc).
2169 if (non_mappable_sid(&psa->trustee)) {
2170 DEBUG(10, ("create_canon_ace_lists: ignoring "
2171 "non-mappable SID %s\n",
2172 sid_string_dbg(&psa->trustee)));
2173 TALLOC_FREE(current_ace);
2174 continue;
2177 if (lp_force_unknown_acl_user(SNUM(fsp->conn))) {
2178 DEBUG(10, ("create_canon_ace_lists: ignoring "
2179 "unknown or foreign SID %s\n",
2180 sid_string_dbg(&psa->trustee)));
2181 TALLOC_FREE(current_ace);
2182 continue;
2185 free_canon_ace_list(file_ace);
2186 free_canon_ace_list(dir_ace);
2187 DEBUG(0, ("create_canon_ace_lists: unable to map SID "
2188 "%s to uid or gid.\n",
2189 sid_string_dbg(&current_ace->trustee)));
2190 TALLOC_FREE(current_ace);
2191 return false;
2195 /* handles the talloc_free of current_ace if not added for some reason */
2196 if (!add_current_ace_to_acl(fsp, psa, &file_ace, &dir_ace,
2197 &got_file_allow, &got_dir_allow,
2198 &all_aces_are_inherit_only, current_ace)) {
2199 free_canon_ace_list(file_ace);
2200 free_canon_ace_list(dir_ace);
2201 return false;
2205 if (fsp->is_directory && all_aces_are_inherit_only) {
2207 * Windows 2000 is doing one of these weird 'inherit acl'
2208 * traverses to conserve NTFS ACL resources. Just pretend
2209 * there was no DACL sent. JRA.
2212 DEBUG(10,("create_canon_ace_lists: Win2k inherit acl traverse. Ignoring DACL.\n"));
2213 free_canon_ace_list(file_ace);
2214 free_canon_ace_list(dir_ace);
2215 file_ace = NULL;
2216 dir_ace = NULL;
2217 } else {
2219 * Check if we have SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ entries in
2220 * the file ACL. If we don't have them, check if any SMB_ACL_USER/SMB_ACL_GROUP
2221 * entries can be converted to *_OBJ. Don't do this for the default
2222 * ACL, we will create them separately for this if needed inside
2223 * ensure_canon_entry_valid_on_set().
2225 if (file_ace) {
2226 check_owning_objs(file_ace, pfile_owner_sid, pfile_grp_sid);
2230 *ppfile_ace = file_ace;
2231 *ppdir_ace = dir_ace;
2233 return True;
2236 /****************************************************************************
2237 ASCII art time again... JRA :-).
2239 We have 4 cases to process when moving from an NT ACL to a POSIX ACL. Firstly,
2240 we insist the ACL is in canonical form (ie. all DENY entries preceede ALLOW
2241 entries). Secondly, the merge code has ensured that all duplicate SID entries for
2242 allow or deny have been merged, so the same SID can only appear once in the deny
2243 list or once in the allow list.
2245 We then process as follows :
2247 ---------------------------------------------------------------------------
2248 First pass - look for a Everyone DENY entry.
2250 If it is deny all (rwx) trunate the list at this point.
2251 Else, walk the list from this point and use the deny permissions of this
2252 entry as a mask on all following allow entries. Finally, delete
2253 the Everyone DENY entry (we have applied it to everything possible).
2255 In addition, in this pass we remove any DENY entries that have
2256 no permissions (ie. they are a DENY nothing).
2257 ---------------------------------------------------------------------------
2258 Second pass - only deal with deny user entries.
2260 DENY user1 (perms XXX)
2262 new_perms = 0
2263 for all following allow group entries where user1 is in group
2264 new_perms |= group_perms;
2266 user1 entry perms = new_perms & ~ XXX;
2268 Convert the deny entry to an allow entry with the new perms and
2269 push to the end of the list. Note if the user was in no groups
2270 this maps to a specific allow nothing entry for this user.
2272 The common case from the NT ACL choser (userX deny all) is
2273 optimised so we don't do the group lookup - we just map to
2274 an allow nothing entry.
2276 What we're doing here is inferring the allow permissions the
2277 person setting the ACE on user1 wanted by looking at the allow
2278 permissions on the groups the user is currently in. This will
2279 be a snapshot, depending on group membership but is the best
2280 we can do and has the advantage of failing closed rather than
2281 open.
2282 ---------------------------------------------------------------------------
2283 Third pass - only deal with deny group entries.
2285 DENY group1 (perms XXX)
2287 for all following allow user entries where user is in group1
2288 user entry perms = user entry perms & ~ XXX;
2290 If there is a group Everyone allow entry with permissions YYY,
2291 convert the group1 entry to an allow entry and modify its
2292 permissions to be :
2294 new_perms = YYY & ~ XXX
2296 and push to the end of the list.
2298 If there is no group Everyone allow entry then convert the
2299 group1 entry to a allow nothing entry and push to the end of the list.
2301 Note that the common case from the NT ACL choser (groupX deny all)
2302 cannot be optimised here as we need to modify user entries who are
2303 in the group to change them to a deny all also.
2305 What we're doing here is modifying the allow permissions of
2306 user entries (which are more specific in POSIX ACLs) to mask
2307 out the explicit deny set on the group they are in. This will
2308 be a snapshot depending on current group membership but is the
2309 best we can do and has the advantage of failing closed rather
2310 than open.
2311 ---------------------------------------------------------------------------
2312 Fourth pass - cope with cumulative permissions.
2314 for all allow user entries, if there exists an allow group entry with
2315 more permissive permissions, and the user is in that group, rewrite the
2316 allow user permissions to contain both sets of permissions.
2318 Currently the code for this is #ifdef'ed out as these semantics make
2319 no sense to me. JRA.
2320 ---------------------------------------------------------------------------
2322 Note we *MUST* do the deny user pass first as this will convert deny user
2323 entries into allow user entries which can then be processed by the deny
2324 group pass.
2326 The above algorithm took a *lot* of thinking about - hence this
2327 explaination :-). JRA.
2328 ****************************************************************************/
2330 /****************************************************************************
2331 Process a canon_ace list entries. This is very complex code. We need
2332 to go through and remove the "deny" permissions from any allow entry that matches
2333 the id of this entry. We have already refused any NT ACL that wasn't in correct
2334 order (DENY followed by ALLOW). If any allow entry ends up with zero permissions,
2335 we just remove it (to fail safe). We have already removed any duplicate ace
2336 entries. Treat an "Everyone" DENY_ACE as a special case - use it to mask all
2337 allow entries.
2338 ****************************************************************************/
2340 static void process_deny_list(connection_struct *conn, canon_ace **pp_ace_list )
2342 canon_ace *ace_list = *pp_ace_list;
2343 canon_ace *curr_ace = NULL;
2344 canon_ace *curr_ace_next = NULL;
2346 /* Pass 1 above - look for an Everyone, deny entry. */
2348 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2349 canon_ace *allow_ace_p;
2351 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2353 if (curr_ace->attr != DENY_ACE)
2354 continue;
2356 if (curr_ace->perms == (mode_t)0) {
2358 /* Deny nothing entry - delete. */
2360 DLIST_REMOVE(ace_list, curr_ace);
2361 continue;
2364 if (!dom_sid_equal(&curr_ace->trustee, &global_sid_World))
2365 continue;
2367 /* JRATEST - assert. */
2368 SMB_ASSERT(curr_ace->owner_type == WORLD_ACE);
2370 if (curr_ace->perms == ALL_ACE_PERMS) {
2373 * Optimisation. This is a DENY_ALL to Everyone. Truncate the
2374 * list at this point including this entry.
2377 canon_ace *prev_entry = DLIST_PREV(curr_ace);
2379 free_canon_ace_list( curr_ace );
2380 if (prev_entry)
2381 DLIST_REMOVE(ace_list, prev_entry);
2382 else {
2383 /* We deleted the entire list. */
2384 ace_list = NULL;
2386 break;
2389 for (allow_ace_p = curr_ace->next; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2392 * Only mask off allow entries.
2395 if (allow_ace_p->attr != ALLOW_ACE)
2396 continue;
2398 allow_ace_p->perms &= ~curr_ace->perms;
2402 * Now it's been applied, remove it.
2405 DLIST_REMOVE(ace_list, curr_ace);
2408 /* Pass 2 above - deal with deny user entries. */
2410 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2411 mode_t new_perms = (mode_t)0;
2412 canon_ace *allow_ace_p;
2414 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2416 if (curr_ace->attr != DENY_ACE)
2417 continue;
2419 if (curr_ace->owner_type != UID_ACE)
2420 continue;
2422 if (curr_ace->perms == ALL_ACE_PERMS) {
2425 * Optimisation - this is a deny everything to this user.
2426 * Convert to an allow nothing and push to the end of the list.
2429 curr_ace->attr = ALLOW_ACE;
2430 curr_ace->perms = (mode_t)0;
2431 DLIST_DEMOTE(ace_list, curr_ace, canon_ace *);
2432 continue;
2435 for (allow_ace_p = curr_ace->next; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2437 if (allow_ace_p->attr != ALLOW_ACE)
2438 continue;
2440 /* We process GID_ACE and WORLD_ACE entries only. */
2442 if (allow_ace_p->owner_type == UID_ACE)
2443 continue;
2445 if (uid_entry_in_group(conn, curr_ace, allow_ace_p))
2446 new_perms |= allow_ace_p->perms;
2450 * Convert to a allow entry, modify the perms and push to the end
2451 * of the list.
2454 curr_ace->attr = ALLOW_ACE;
2455 curr_ace->perms = (new_perms & ~curr_ace->perms);
2456 DLIST_DEMOTE(ace_list, curr_ace, canon_ace *);
2459 /* Pass 3 above - deal with deny group entries. */
2461 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2462 canon_ace *allow_ace_p;
2463 canon_ace *allow_everyone_p = NULL;
2465 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2467 if (curr_ace->attr != DENY_ACE)
2468 continue;
2470 if (curr_ace->owner_type != GID_ACE)
2471 continue;
2473 for (allow_ace_p = curr_ace->next; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2475 if (allow_ace_p->attr != ALLOW_ACE)
2476 continue;
2478 /* Store a pointer to the Everyone allow, if it exists. */
2479 if (allow_ace_p->owner_type == WORLD_ACE)
2480 allow_everyone_p = allow_ace_p;
2482 /* We process UID_ACE entries only. */
2484 if (allow_ace_p->owner_type != UID_ACE)
2485 continue;
2487 /* Mask off the deny group perms. */
2489 if (uid_entry_in_group(conn, allow_ace_p, curr_ace))
2490 allow_ace_p->perms &= ~curr_ace->perms;
2494 * Convert the deny to an allow with the correct perms and
2495 * push to the end of the list.
2498 curr_ace->attr = ALLOW_ACE;
2499 if (allow_everyone_p)
2500 curr_ace->perms = allow_everyone_p->perms & ~curr_ace->perms;
2501 else
2502 curr_ace->perms = (mode_t)0;
2503 DLIST_DEMOTE(ace_list, curr_ace, canon_ace *);
2506 /* Doing this fourth pass allows Windows semantics to be layered
2507 * on top of POSIX semantics. I'm not sure if this is desirable.
2508 * For example, in W2K ACLs there is no way to say, "Group X no
2509 * access, user Y full access" if user Y is a member of group X.
2510 * This seems completely broken semantics to me.... JRA.
2513 #if 0
2514 /* Pass 4 above - deal with allow entries. */
2516 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2517 canon_ace *allow_ace_p;
2519 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2521 if (curr_ace->attr != ALLOW_ACE)
2522 continue;
2524 if (curr_ace->owner_type != UID_ACE)
2525 continue;
2527 for (allow_ace_p = ace_list; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2529 if (allow_ace_p->attr != ALLOW_ACE)
2530 continue;
2532 /* We process GID_ACE entries only. */
2534 if (allow_ace_p->owner_type != GID_ACE)
2535 continue;
2537 /* OR in the group perms. */
2539 if (uid_entry_in_group(conn, curr_ace, allow_ace_p))
2540 curr_ace->perms |= allow_ace_p->perms;
2543 #endif
2545 *pp_ace_list = ace_list;
2548 /****************************************************************************
2549 Unpack a struct security_descriptor into two canonical ace lists. We don't depend on this
2550 succeeding.
2551 ****************************************************************************/
2553 static bool unpack_canon_ace(files_struct *fsp,
2554 const SMB_STRUCT_STAT *pst,
2555 struct dom_sid *pfile_owner_sid,
2556 struct dom_sid *pfile_grp_sid,
2557 canon_ace **ppfile_ace,
2558 canon_ace **ppdir_ace,
2559 uint32 security_info_sent,
2560 const struct security_descriptor *psd)
2562 canon_ace *file_ace = NULL;
2563 canon_ace *dir_ace = NULL;
2565 *ppfile_ace = NULL;
2566 *ppdir_ace = NULL;
2568 if(security_info_sent == 0) {
2569 DEBUG(0,("unpack_canon_ace: no security info sent !\n"));
2570 return False;
2574 * If no DACL then this is a chown only security descriptor.
2577 if(!(security_info_sent & SECINFO_DACL) || !psd->dacl)
2578 return True;
2581 * Now go through the DACL and create the canon_ace lists.
2584 if (!create_canon_ace_lists( fsp, pst, pfile_owner_sid, pfile_grp_sid,
2585 &file_ace, &dir_ace, psd->dacl))
2586 return False;
2588 if ((file_ace == NULL) && (dir_ace == NULL)) {
2589 /* W2K traverse DACL set - ignore. */
2590 return True;
2594 * Go through the canon_ace list and merge entries
2595 * belonging to identical users of identical allow or deny type.
2596 * We can do this as all deny entries come first, followed by
2597 * all allow entries (we have mandated this before accepting this acl).
2600 print_canon_ace_list( "file ace - before merge", file_ace);
2601 merge_aces( &file_ace, false);
2603 print_canon_ace_list( "dir ace - before merge", dir_ace);
2604 merge_aces( &dir_ace, true);
2607 * NT ACLs are order dependent. Go through the acl lists and
2608 * process DENY entries by masking the allow entries.
2611 print_canon_ace_list( "file ace - before deny", file_ace);
2612 process_deny_list(fsp->conn, &file_ace);
2614 print_canon_ace_list( "dir ace - before deny", dir_ace);
2615 process_deny_list(fsp->conn, &dir_ace);
2618 * A well formed POSIX file or default ACL has at least 3 entries, a
2619 * SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ
2620 * and optionally a mask entry. Ensure this is the case.
2623 print_canon_ace_list( "file ace - before valid", file_ace);
2625 if (!ensure_canon_entry_valid_on_set(fsp->conn, &file_ace, false, fsp->conn->params,
2626 fsp->is_directory, pfile_owner_sid, pfile_grp_sid, pst)) {
2627 free_canon_ace_list(file_ace);
2628 free_canon_ace_list(dir_ace);
2629 return False;
2632 print_canon_ace_list( "dir ace - before valid", dir_ace);
2634 if (dir_ace && !ensure_canon_entry_valid_on_set(fsp->conn, &dir_ace, true, fsp->conn->params,
2635 fsp->is_directory, pfile_owner_sid, pfile_grp_sid, pst)) {
2636 free_canon_ace_list(file_ace);
2637 free_canon_ace_list(dir_ace);
2638 return False;
2641 print_canon_ace_list( "file ace - return", file_ace);
2642 print_canon_ace_list( "dir ace - return", dir_ace);
2644 *ppfile_ace = file_ace;
2645 *ppdir_ace = dir_ace;
2646 return True;
2650 /******************************************************************************
2651 When returning permissions, try and fit NT display
2652 semantics if possible. Note the the canon_entries here must have been malloced.
2653 The list format should be - first entry = owner, followed by group and other user
2654 entries, last entry = other.
2656 Note that this doesn't exactly match the NT semantics for an ACL. As POSIX entries
2657 are not ordered, and match on the most specific entry rather than walking a list,
2658 then a simple POSIX permission of rw-r--r-- should really map to 5 entries,
2660 Entry 0: owner : deny all except read and write.
2661 Entry 1: owner : allow read and write.
2662 Entry 2: group : deny all except read.
2663 Entry 3: group : allow read.
2664 Entry 4: Everyone : allow read.
2666 But NT cannot display this in their ACL editor !
2667 ********************************************************************************/
2669 static void arrange_posix_perms(const char *filename, canon_ace **pp_list_head)
2671 canon_ace *l_head = *pp_list_head;
2672 canon_ace *owner_ace = NULL;
2673 canon_ace *other_ace = NULL;
2674 canon_ace *ace = NULL;
2676 for (ace = l_head; ace; ace = ace->next) {
2677 if (ace->type == SMB_ACL_USER_OBJ)
2678 owner_ace = ace;
2679 else if (ace->type == SMB_ACL_OTHER) {
2680 /* Last ace - this is "other" */
2681 other_ace = ace;
2685 if (!owner_ace || !other_ace) {
2686 DEBUG(0,("arrange_posix_perms: Invalid POSIX permissions for file %s, missing owner or other.\n",
2687 filename ));
2688 return;
2692 * The POSIX algorithm applies to owner first, and other last,
2693 * so ensure they are arranged in this order.
2696 if (owner_ace) {
2697 DLIST_PROMOTE(l_head, owner_ace);
2700 if (other_ace) {
2701 DLIST_DEMOTE(l_head, other_ace, canon_ace *);
2704 /* We have probably changed the head of the list. */
2706 *pp_list_head = l_head;
2709 /****************************************************************************
2710 Create a linked list of canonical ACE entries.
2711 ****************************************************************************/
2713 static canon_ace *canonicalise_acl(struct connection_struct *conn,
2714 const char *fname, SMB_ACL_T posix_acl,
2715 const SMB_STRUCT_STAT *psbuf,
2716 const struct dom_sid *powner, const struct dom_sid *pgroup, struct pai_val *pal, SMB_ACL_TYPE_T the_acl_type)
2718 mode_t acl_mask = (S_IRUSR|S_IWUSR|S_IXUSR);
2719 canon_ace *l_head = NULL;
2720 canon_ace *ace = NULL;
2721 canon_ace *next_ace = NULL;
2722 int entry_id = SMB_ACL_FIRST_ENTRY;
2723 bool is_default_acl = (the_acl_type == SMB_ACL_TYPE_DEFAULT);
2724 SMB_ACL_ENTRY_T entry;
2725 size_t ace_count;
2727 while ( posix_acl && (sys_acl_get_entry(posix_acl, entry_id, &entry) == 1)) {
2728 SMB_ACL_TAG_T tagtype;
2729 SMB_ACL_PERMSET_T permset;
2730 struct dom_sid sid;
2731 struct unixid unix_ug;
2732 enum ace_owner owner_type;
2734 entry_id = SMB_ACL_NEXT_ENTRY;
2736 /* Is this a MASK entry ? */
2737 if (sys_acl_get_tag_type(entry, &tagtype) == -1)
2738 continue;
2740 if (sys_acl_get_permset(entry, &permset) == -1)
2741 continue;
2743 /* Decide which SID to use based on the ACL type. */
2744 switch(tagtype) {
2745 case SMB_ACL_USER_OBJ:
2746 /* Get the SID from the owner. */
2747 sid_copy(&sid, powner);
2748 unix_ug.type = ID_TYPE_UID;
2749 unix_ug.id = psbuf->st_ex_uid;
2750 owner_type = UID_ACE;
2751 break;
2752 case SMB_ACL_USER:
2754 uid_t *puid = (uid_t *)sys_acl_get_qualifier(entry);
2755 if (puid == NULL) {
2756 DEBUG(0,("canonicalise_acl: Failed to get uid.\n"));
2757 continue;
2759 uid_to_sid( &sid, *puid);
2760 unix_ug.type = ID_TYPE_UID;
2761 unix_ug.id = *puid;
2762 owner_type = UID_ACE;
2763 break;
2765 case SMB_ACL_GROUP_OBJ:
2766 /* Get the SID from the owning group. */
2767 sid_copy(&sid, pgroup);
2768 unix_ug.type = ID_TYPE_GID;
2769 unix_ug.id = psbuf->st_ex_gid;
2770 owner_type = GID_ACE;
2771 break;
2772 case SMB_ACL_GROUP:
2774 gid_t *pgid = (gid_t *)sys_acl_get_qualifier(entry);
2775 if (pgid == NULL) {
2776 DEBUG(0,("canonicalise_acl: Failed to get gid.\n"));
2777 continue;
2779 gid_to_sid( &sid, *pgid);
2780 unix_ug.type = ID_TYPE_GID;
2781 unix_ug.id = *pgid;
2782 owner_type = GID_ACE;
2783 break;
2785 case SMB_ACL_MASK:
2786 acl_mask = convert_permset_to_mode_t(permset);
2787 continue; /* Don't count the mask as an entry. */
2788 case SMB_ACL_OTHER:
2789 /* Use the Everyone SID */
2790 sid = global_sid_World;
2791 unix_ug.type = ID_TYPE_NOT_SPECIFIED;
2792 unix_ug.id = -1;
2793 owner_type = WORLD_ACE;
2794 break;
2795 default:
2796 DEBUG(0,("canonicalise_acl: Unknown tagtype %u\n", (unsigned int)tagtype));
2797 continue;
2801 * Add this entry to the list.
2804 if ((ace = talloc(talloc_tos(), canon_ace)) == NULL)
2805 goto fail;
2807 ZERO_STRUCTP(ace);
2808 ace->type = tagtype;
2809 ace->perms = convert_permset_to_mode_t(permset);
2810 ace->attr = ALLOW_ACE;
2811 ace->trustee = sid;
2812 ace->unix_ug = unix_ug;
2813 ace->owner_type = owner_type;
2814 ace->ace_flags = get_pai_flags(pal, ace, is_default_acl);
2816 DLIST_ADD(l_head, ace);
2820 * This next call will ensure we have at least a user/group/world set.
2823 if (!ensure_canon_entry_valid_on_get(conn, &l_head,
2824 powner, pgroup,
2825 psbuf))
2826 goto fail;
2829 * Now go through the list, masking the permissions with the
2830 * acl_mask. Ensure all DENY Entries are at the start of the list.
2833 DEBUG(10,("canonicalise_acl: %s ace entries before arrange :\n", is_default_acl ? "Default" : "Access"));
2835 for ( ace_count = 0, ace = l_head; ace; ace = next_ace, ace_count++) {
2836 next_ace = ace->next;
2838 /* Masks are only applied to entries other than USER_OBJ and OTHER. */
2839 if (ace->type != SMB_ACL_OTHER && ace->type != SMB_ACL_USER_OBJ)
2840 ace->perms &= acl_mask;
2842 if (ace->perms == 0) {
2843 DLIST_PROMOTE(l_head, ace);
2846 if( DEBUGLVL( 10 ) ) {
2847 print_canon_ace(ace, ace_count);
2851 arrange_posix_perms(fname,&l_head );
2853 print_canon_ace_list( "canonicalise_acl: ace entries after arrange", l_head );
2855 return l_head;
2857 fail:
2859 free_canon_ace_list(l_head);
2860 return NULL;
2863 /****************************************************************************
2864 Check if the current user group list contains a given group.
2865 ****************************************************************************/
2867 bool current_user_in_group(connection_struct *conn, gid_t gid)
2869 int i;
2870 const struct security_unix_token *utok = get_current_utok(conn);
2872 for (i = 0; i < utok->ngroups; i++) {
2873 if (utok->groups[i] == gid) {
2874 return True;
2878 return False;
2881 /****************************************************************************
2882 Should we override a deny ? Check 'acl group control' and 'dos filemode'.
2883 ****************************************************************************/
2885 static bool acl_group_override(connection_struct *conn,
2886 const struct smb_filename *smb_fname)
2888 if ((errno != EPERM) && (errno != EACCES)) {
2889 return false;
2892 /* file primary group == user primary or supplementary group */
2893 if (lp_acl_group_control(SNUM(conn)) &&
2894 current_user_in_group(conn, smb_fname->st.st_ex_gid)) {
2895 return true;
2898 /* user has writeable permission */
2899 if (lp_dos_filemode(SNUM(conn)) &&
2900 can_write_to_file(conn, smb_fname)) {
2901 return true;
2904 return false;
2907 /****************************************************************************
2908 Attempt to apply an ACL to a file or directory.
2909 ****************************************************************************/
2911 static bool set_canon_ace_list(files_struct *fsp,
2912 canon_ace *the_ace,
2913 bool default_ace,
2914 const SMB_STRUCT_STAT *psbuf,
2915 bool *pacl_set_support)
2917 connection_struct *conn = fsp->conn;
2918 bool ret = False;
2919 SMB_ACL_T the_acl = sys_acl_init(talloc_tos());
2920 canon_ace *p_ace;
2921 int i;
2922 SMB_ACL_ENTRY_T mask_entry;
2923 bool got_mask_entry = False;
2924 SMB_ACL_PERMSET_T mask_permset;
2925 SMB_ACL_TYPE_T the_acl_type = (default_ace ? SMB_ACL_TYPE_DEFAULT : SMB_ACL_TYPE_ACCESS);
2926 bool needs_mask = False;
2927 mode_t mask_perms = 0;
2929 /* Use the psbuf that was passed in. */
2930 if (psbuf != &fsp->fsp_name->st) {
2931 fsp->fsp_name->st = *psbuf;
2934 #if defined(POSIX_ACL_NEEDS_MASK)
2935 /* HP-UX always wants to have a mask (called "class" there). */
2936 needs_mask = True;
2937 #endif
2939 if (the_acl == NULL) {
2940 DEBUG(0, ("sys_acl_init failed to allocate an ACL\n"));
2941 return false;
2944 if( DEBUGLVL( 10 )) {
2945 dbgtext("set_canon_ace_list: setting ACL:\n");
2946 for (i = 0, p_ace = the_ace; p_ace; p_ace = p_ace->next, i++ ) {
2947 print_canon_ace( p_ace, i);
2951 for (i = 0, p_ace = the_ace; p_ace; p_ace = p_ace->next, i++ ) {
2952 SMB_ACL_ENTRY_T the_entry;
2953 SMB_ACL_PERMSET_T the_permset;
2956 * ACLs only "need" an ACL_MASK entry if there are any named user or
2957 * named group entries. But if there is an ACL_MASK entry, it applies
2958 * to ACL_USER, ACL_GROUP, and ACL_GROUP_OBJ entries. Set the mask
2959 * so that it doesn't deny (i.e., mask off) any permissions.
2962 if (p_ace->type == SMB_ACL_USER || p_ace->type == SMB_ACL_GROUP) {
2963 needs_mask = True;
2964 mask_perms |= p_ace->perms;
2965 } else if (p_ace->type == SMB_ACL_GROUP_OBJ) {
2966 mask_perms |= p_ace->perms;
2970 * Get the entry for this ACE.
2973 if (sys_acl_create_entry(&the_acl, &the_entry) == -1) {
2974 DEBUG(0,("set_canon_ace_list: Failed to create entry %d. (%s)\n",
2975 i, strerror(errno) ));
2976 goto fail;
2979 if (p_ace->type == SMB_ACL_MASK) {
2980 mask_entry = the_entry;
2981 got_mask_entry = True;
2985 * Ok - we now know the ACL calls should be working, don't
2986 * allow fallback to chmod.
2989 *pacl_set_support = True;
2992 * Initialise the entry from the canon_ace.
2996 * First tell the entry what type of ACE this is.
2999 if (sys_acl_set_tag_type(the_entry, p_ace->type) == -1) {
3000 DEBUG(0,("set_canon_ace_list: Failed to set tag type on entry %d. (%s)\n",
3001 i, strerror(errno) ));
3002 goto fail;
3006 * Only set the qualifier (user or group id) if the entry is a user
3007 * or group id ACE.
3010 if ((p_ace->type == SMB_ACL_USER) || (p_ace->type == SMB_ACL_GROUP)) {
3011 if (sys_acl_set_qualifier(the_entry,(void *)&p_ace->unix_ug.id) == -1) {
3012 DEBUG(0,("set_canon_ace_list: Failed to set qualifier on entry %d. (%s)\n",
3013 i, strerror(errno) ));
3014 goto fail;
3019 * Convert the mode_t perms in the canon_ace to a POSIX permset.
3022 if (sys_acl_get_permset(the_entry, &the_permset) == -1) {
3023 DEBUG(0,("set_canon_ace_list: Failed to get permset on entry %d. (%s)\n",
3024 i, strerror(errno) ));
3025 goto fail;
3028 if (map_acl_perms_to_permset(conn, p_ace->perms, &the_permset) == -1) {
3029 DEBUG(0,("set_canon_ace_list: Failed to create permset for mode (%u) on entry %d. (%s)\n",
3030 (unsigned int)p_ace->perms, i, strerror(errno) ));
3031 goto fail;
3035 * ..and apply them to the entry.
3038 if (sys_acl_set_permset(the_entry, the_permset) == -1) {
3039 DEBUG(0,("set_canon_ace_list: Failed to add permset on entry %d. (%s)\n",
3040 i, strerror(errno) ));
3041 goto fail;
3044 if( DEBUGLVL( 10 ))
3045 print_canon_ace( p_ace, i);
3049 if (needs_mask && !got_mask_entry) {
3050 if (sys_acl_create_entry(&the_acl, &mask_entry) == -1) {
3051 DEBUG(0,("set_canon_ace_list: Failed to create mask entry. (%s)\n", strerror(errno) ));
3052 goto fail;
3055 if (sys_acl_set_tag_type(mask_entry, SMB_ACL_MASK) == -1) {
3056 DEBUG(0,("set_canon_ace_list: Failed to set tag type on mask entry. (%s)\n",strerror(errno) ));
3057 goto fail;
3060 if (sys_acl_get_permset(mask_entry, &mask_permset) == -1) {
3061 DEBUG(0,("set_canon_ace_list: Failed to get mask permset. (%s)\n", strerror(errno) ));
3062 goto fail;
3065 if (map_acl_perms_to_permset(conn, S_IRUSR|S_IWUSR|S_IXUSR, &mask_permset) == -1) {
3066 DEBUG(0,("set_canon_ace_list: Failed to create mask permset. (%s)\n", strerror(errno) ));
3067 goto fail;
3070 if (sys_acl_set_permset(mask_entry, mask_permset) == -1) {
3071 DEBUG(0,("set_canon_ace_list: Failed to add mask permset. (%s)\n", strerror(errno) ));
3072 goto fail;
3077 * Finally apply it to the file or directory.
3080 if(default_ace || fsp->is_directory || fsp->fh->fd == -1) {
3081 if (SMB_VFS_SYS_ACL_SET_FILE(conn, fsp->fsp_name->base_name,
3082 the_acl_type, the_acl) == -1) {
3084 * Some systems allow all the above calls and only fail with no ACL support
3085 * when attempting to apply the acl. HPUX with HFS is an example of this. JRA.
3087 if (no_acl_syscall_error(errno)) {
3088 *pacl_set_support = False;
3091 if (acl_group_override(conn, fsp->fsp_name)) {
3092 int sret;
3094 DEBUG(5,("set_canon_ace_list: acl group "
3095 "control on and current user in file "
3096 "%s primary group.\n",
3097 fsp_str_dbg(fsp)));
3099 become_root();
3100 sret = SMB_VFS_SYS_ACL_SET_FILE(conn,
3101 fsp->fsp_name->base_name, the_acl_type,
3102 the_acl);
3103 unbecome_root();
3104 if (sret == 0) {
3105 ret = True;
3109 if (ret == False) {
3110 DEBUG(2,("set_canon_ace_list: "
3111 "sys_acl_set_file type %s failed for "
3112 "file %s (%s).\n",
3113 the_acl_type == SMB_ACL_TYPE_DEFAULT ?
3114 "directory default" : "file",
3115 fsp_str_dbg(fsp), strerror(errno)));
3116 goto fail;
3119 } else {
3120 if (SMB_VFS_SYS_ACL_SET_FD(fsp, the_acl) == -1) {
3122 * Some systems allow all the above calls and only fail with no ACL support
3123 * when attempting to apply the acl. HPUX with HFS is an example of this. JRA.
3125 if (no_acl_syscall_error(errno)) {
3126 *pacl_set_support = False;
3129 if (acl_group_override(conn, fsp->fsp_name)) {
3130 int sret;
3132 DEBUG(5,("set_canon_ace_list: acl group "
3133 "control on and current user in file "
3134 "%s primary group.\n",
3135 fsp_str_dbg(fsp)));
3137 become_root();
3138 sret = SMB_VFS_SYS_ACL_SET_FD(fsp, the_acl);
3139 unbecome_root();
3140 if (sret == 0) {
3141 ret = True;
3145 if (ret == False) {
3146 DEBUG(2,("set_canon_ace_list: "
3147 "sys_acl_set_file failed for file %s "
3148 "(%s).\n",
3149 fsp_str_dbg(fsp), strerror(errno)));
3150 goto fail;
3155 ret = True;
3157 fail:
3159 if (the_acl != NULL) {
3160 TALLOC_FREE(the_acl);
3163 return ret;
3166 /****************************************************************************
3167 Find a particular canon_ace entry.
3168 ****************************************************************************/
3170 static struct canon_ace *canon_ace_entry_for(struct canon_ace *list, SMB_ACL_TAG_T type, struct unixid *id)
3172 while (list) {
3173 if (list->type == type && ((type != SMB_ACL_USER && type != SMB_ACL_GROUP) ||
3174 (type == SMB_ACL_USER && id && id->id == list->unix_ug.id) ||
3175 (type == SMB_ACL_GROUP && id && id->id == list->unix_ug.id)))
3176 break;
3177 list = list->next;
3179 return list;
3182 /****************************************************************************
3184 ****************************************************************************/
3186 SMB_ACL_T free_empty_sys_acl(connection_struct *conn, SMB_ACL_T the_acl)
3188 SMB_ACL_ENTRY_T entry;
3190 if (!the_acl)
3191 return NULL;
3192 if (sys_acl_get_entry(the_acl, SMB_ACL_FIRST_ENTRY, &entry) != 1) {
3193 TALLOC_FREE(the_acl);
3194 return NULL;
3196 return the_acl;
3199 /****************************************************************************
3200 Convert a canon_ace to a generic 3 element permission - if possible.
3201 ****************************************************************************/
3203 #define MAP_PERM(p,mask,result) (((p) & (mask)) ? (result) : 0 )
3205 static bool convert_canon_ace_to_posix_perms( files_struct *fsp, canon_ace *file_ace_list, mode_t *posix_perms)
3207 int snum = SNUM(fsp->conn);
3208 size_t ace_count = count_canon_ace_list(file_ace_list);
3209 canon_ace *ace_p;
3210 canon_ace *owner_ace = NULL;
3211 canon_ace *group_ace = NULL;
3212 canon_ace *other_ace = NULL;
3213 mode_t and_bits;
3214 mode_t or_bits;
3216 if (ace_count != 3) {
3217 DEBUG(3,("convert_canon_ace_to_posix_perms: Too many ACE "
3218 "entries for file %s to convert to posix perms.\n",
3219 fsp_str_dbg(fsp)));
3220 return False;
3223 for (ace_p = file_ace_list; ace_p; ace_p = ace_p->next) {
3224 if (ace_p->owner_type == UID_ACE)
3225 owner_ace = ace_p;
3226 else if (ace_p->owner_type == GID_ACE)
3227 group_ace = ace_p;
3228 else if (ace_p->owner_type == WORLD_ACE)
3229 other_ace = ace_p;
3232 if (!owner_ace || !group_ace || !other_ace) {
3233 DEBUG(3,("convert_canon_ace_to_posix_perms: Can't get "
3234 "standard entries for file %s.\n", fsp_str_dbg(fsp)));
3235 return False;
3238 *posix_perms = (mode_t)0;
3240 *posix_perms |= owner_ace->perms;
3241 *posix_perms |= MAP_PERM(group_ace->perms, S_IRUSR, S_IRGRP);
3242 *posix_perms |= MAP_PERM(group_ace->perms, S_IWUSR, S_IWGRP);
3243 *posix_perms |= MAP_PERM(group_ace->perms, S_IXUSR, S_IXGRP);
3244 *posix_perms |= MAP_PERM(other_ace->perms, S_IRUSR, S_IROTH);
3245 *posix_perms |= MAP_PERM(other_ace->perms, S_IWUSR, S_IWOTH);
3246 *posix_perms |= MAP_PERM(other_ace->perms, S_IXUSR, S_IXOTH);
3248 /* The owner must have at least read access. */
3250 *posix_perms |= S_IRUSR;
3251 if (fsp->is_directory)
3252 *posix_perms |= (S_IWUSR|S_IXUSR);
3254 /* If requested apply the masks. */
3256 /* Get the initial bits to apply. */
3258 if (fsp->is_directory) {
3259 and_bits = lp_dir_mask(snum);
3260 or_bits = lp_force_dir_mode(snum);
3261 } else {
3262 and_bits = lp_create_mask(snum);
3263 or_bits = lp_force_create_mode(snum);
3266 *posix_perms = (((*posix_perms) & and_bits)|or_bits);
3268 DEBUG(10,("convert_canon_ace_to_posix_perms: converted u=%o,g=%o,w=%o "
3269 "to perm=0%o for file %s.\n", (int)owner_ace->perms,
3270 (int)group_ace->perms, (int)other_ace->perms,
3271 (int)*posix_perms, fsp_str_dbg(fsp)));
3273 return True;
3276 /****************************************************************************
3277 Incoming NT ACLs on a directory can be split into a default POSIX acl (CI|OI|IO) and
3278 a normal POSIX acl. Win2k needs these split acls re-merging into one ACL
3279 with CI|OI set so it is inherited and also applies to the directory.
3280 Based on code from "Jim McDonough" <jmcd@us.ibm.com>.
3281 ****************************************************************************/
3283 static size_t merge_default_aces( struct security_ace *nt_ace_list, size_t num_aces)
3285 size_t i, j;
3287 for (i = 0; i < num_aces; i++) {
3288 for (j = i+1; j < num_aces; j++) {
3289 uint32 i_flags_ni = (nt_ace_list[i].flags & ~SEC_ACE_FLAG_INHERITED_ACE);
3290 uint32 j_flags_ni = (nt_ace_list[j].flags & ~SEC_ACE_FLAG_INHERITED_ACE);
3291 bool i_inh = (nt_ace_list[i].flags & SEC_ACE_FLAG_INHERITED_ACE) ? True : False;
3292 bool j_inh = (nt_ace_list[j].flags & SEC_ACE_FLAG_INHERITED_ACE) ? True : False;
3294 /* We know the lower number ACE's are file entries. */
3295 if ((nt_ace_list[i].type == nt_ace_list[j].type) &&
3296 (nt_ace_list[i].size == nt_ace_list[j].size) &&
3297 (nt_ace_list[i].access_mask == nt_ace_list[j].access_mask) &&
3298 dom_sid_equal(&nt_ace_list[i].trustee, &nt_ace_list[j].trustee) &&
3299 (i_inh == j_inh) &&
3300 (i_flags_ni == 0) &&
3301 (j_flags_ni == (SEC_ACE_FLAG_OBJECT_INHERIT|
3302 SEC_ACE_FLAG_CONTAINER_INHERIT|
3303 SEC_ACE_FLAG_INHERIT_ONLY))) {
3305 * W2K wants to have access allowed zero access ACE's
3306 * at the end of the list. If the mask is zero, merge
3307 * the non-inherited ACE onto the inherited ACE.
3310 if (nt_ace_list[i].access_mask == 0) {
3311 nt_ace_list[j].flags = SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT|
3312 (i_inh ? SEC_ACE_FLAG_INHERITED_ACE : 0);
3313 if (num_aces - i - 1 > 0)
3314 memmove(&nt_ace_list[i], &nt_ace_list[i+1], (num_aces-i-1) *
3315 sizeof(struct security_ace));
3317 DEBUG(10,("merge_default_aces: Merging zero access ACE %u onto ACE %u.\n",
3318 (unsigned int)i, (unsigned int)j ));
3319 } else {
3321 * These are identical except for the flags.
3322 * Merge the inherited ACE onto the non-inherited ACE.
3325 nt_ace_list[i].flags = SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT|
3326 (i_inh ? SEC_ACE_FLAG_INHERITED_ACE : 0);
3327 if (num_aces - j - 1 > 0)
3328 memmove(&nt_ace_list[j], &nt_ace_list[j+1], (num_aces-j-1) *
3329 sizeof(struct security_ace));
3331 DEBUG(10,("merge_default_aces: Merging ACE %u onto ACE %u.\n",
3332 (unsigned int)j, (unsigned int)i ));
3334 num_aces--;
3335 break;
3340 return num_aces;
3344 * Add or Replace ACE entry.
3345 * In some cases we need to add a specific ACE for compatibility reasons.
3346 * When doing that we must make sure we are not actually creating a duplicate
3347 * entry. So we need to search whether an ACE entry already exist and eventually
3348 * replacce the access mask, or add a completely new entry if none was found.
3350 * This function assumes the array has enough space to add a new entry without
3351 * any reallocation of memory.
3354 static void add_or_replace_ace(struct security_ace *nt_ace_list, size_t *num_aces,
3355 const struct dom_sid *sid, enum security_ace_type type,
3356 uint32_t mask, uint8_t flags)
3358 int i;
3360 /* first search for a duplicate */
3361 for (i = 0; i < *num_aces; i++) {
3362 if (dom_sid_equal(&nt_ace_list[i].trustee, sid) &&
3363 (nt_ace_list[i].flags == flags)) break;
3366 if (i < *num_aces) { /* found */
3367 nt_ace_list[i].type = type;
3368 nt_ace_list[i].access_mask = mask;
3369 DEBUG(10, ("Replacing ACE %d with SID %s and flags %02x\n",
3370 i, sid_string_dbg(sid), flags));
3371 return;
3374 /* not found, append it */
3375 init_sec_ace(&nt_ace_list[(*num_aces)++], sid, type, mask, flags);
3379 /****************************************************************************
3380 Reply to query a security descriptor from an fsp. If it succeeds it allocates
3381 the space for the return elements and returns the size needed to return the
3382 security descriptor. This should be the only external function needed for
3383 the UNIX style get ACL.
3384 ****************************************************************************/
3386 static NTSTATUS posix_get_nt_acl_common(struct connection_struct *conn,
3387 const char *name,
3388 const SMB_STRUCT_STAT *sbuf,
3389 struct pai_val *pal,
3390 SMB_ACL_T posix_acl,
3391 SMB_ACL_T def_acl,
3392 uint32_t security_info,
3393 TALLOC_CTX *mem_ctx,
3394 struct security_descriptor **ppdesc)
3396 struct dom_sid owner_sid;
3397 struct dom_sid group_sid;
3398 size_t sd_size = 0;
3399 struct security_acl *psa = NULL;
3400 size_t num_acls = 0;
3401 size_t num_def_acls = 0;
3402 size_t num_aces = 0;
3403 canon_ace *file_ace = NULL;
3404 canon_ace *dir_ace = NULL;
3405 struct security_ace *nt_ace_list = NULL;
3406 size_t num_profile_acls = 0;
3407 struct dom_sid orig_owner_sid;
3408 struct security_descriptor *psd = NULL;
3409 int i;
3412 * Get the owner, group and world SIDs.
3415 create_file_sids(sbuf, &owner_sid, &group_sid);
3417 if (lp_profile_acls(SNUM(conn))) {
3418 /* For WXP SP1 the owner must be administrators. */
3419 sid_copy(&orig_owner_sid, &owner_sid);
3420 sid_copy(&owner_sid, &global_sid_Builtin_Administrators);
3421 sid_copy(&group_sid, &global_sid_Builtin_Users);
3422 num_profile_acls = 3;
3425 if ((security_info & SECINFO_DACL) && !(security_info & SECINFO_PROTECTED_DACL)) {
3428 * In the optimum case Creator Owner and Creator Group would be used for
3429 * the ACL_USER_OBJ and ACL_GROUP_OBJ entries, respectively, but this
3430 * would lead to usability problems under Windows: The Creator entries
3431 * are only available in browse lists of directories and not for files;
3432 * additionally the identity of the owning group couldn't be determined.
3433 * We therefore use those identities only for Default ACLs.
3436 /* Create the canon_ace lists. */
3437 file_ace = canonicalise_acl(conn, name, posix_acl, sbuf,
3438 &owner_sid, &group_sid, pal,
3439 SMB_ACL_TYPE_ACCESS);
3441 /* We must have *some* ACLS. */
3443 if (count_canon_ace_list(file_ace) == 0) {
3444 DEBUG(0,("get_nt_acl : No ACLs on file (%s) !\n", name));
3445 goto done;
3448 if (S_ISDIR(sbuf->st_ex_mode) && def_acl) {
3449 dir_ace = canonicalise_acl(conn, name, def_acl,
3450 sbuf,
3451 &global_sid_Creator_Owner,
3452 &global_sid_Creator_Group,
3453 pal, SMB_ACL_TYPE_DEFAULT);
3457 * Create the NT ACE list from the canonical ace lists.
3461 canon_ace *ace;
3462 enum security_ace_type nt_acl_type;
3464 if (nt4_compatible_acls() && dir_ace) {
3466 * NT 4 chokes if an ACL contains an INHERIT_ONLY entry
3467 * but no non-INHERIT_ONLY entry for one SID. So we only
3468 * remove entries from the Access ACL if the
3469 * corresponding Default ACL entries have also been
3470 * removed. ACEs for CREATOR-OWNER and CREATOR-GROUP
3471 * are exceptions. We can do nothing
3472 * intelligent if the Default ACL contains entries that
3473 * are not also contained in the Access ACL, so this
3474 * case will still fail under NT 4.
3477 ace = canon_ace_entry_for(dir_ace, SMB_ACL_OTHER, NULL);
3478 if (ace && !ace->perms) {
3479 DLIST_REMOVE(dir_ace, ace);
3480 TALLOC_FREE(ace);
3482 ace = canon_ace_entry_for(file_ace, SMB_ACL_OTHER, NULL);
3483 if (ace && !ace->perms) {
3484 DLIST_REMOVE(file_ace, ace);
3485 TALLOC_FREE(ace);
3490 * WinNT doesn't usually have Creator Group
3491 * in browse lists, so we send this entry to
3492 * WinNT even if it contains no relevant
3493 * permissions. Once we can add
3494 * Creator Group to browse lists we can
3495 * re-enable this.
3498 #if 0
3499 ace = canon_ace_entry_for(dir_ace, SMB_ACL_GROUP_OBJ, NULL);
3500 if (ace && !ace->perms) {
3501 DLIST_REMOVE(dir_ace, ace);
3502 TALLOC_FREE(ace);
3504 #endif
3506 ace = canon_ace_entry_for(file_ace, SMB_ACL_GROUP_OBJ, NULL);
3507 if (ace && !ace->perms) {
3508 DLIST_REMOVE(file_ace, ace);
3509 TALLOC_FREE(ace);
3513 num_acls = count_canon_ace_list(file_ace);
3514 num_def_acls = count_canon_ace_list(dir_ace);
3516 /* Allocate the ace list. */
3517 if ((nt_ace_list = talloc_array(talloc_tos(), struct security_ace,num_acls + num_profile_acls + num_def_acls)) == NULL) {
3518 DEBUG(0,("get_nt_acl: Unable to malloc space for nt_ace_list.\n"));
3519 goto done;
3522 memset(nt_ace_list, '\0', (num_acls + num_def_acls) * sizeof(struct security_ace) );
3525 * Create the NT ACE list from the canonical ace lists.
3528 for (ace = file_ace; ace != NULL; ace = ace->next) {
3529 uint32_t acc = map_canon_ace_perms(SNUM(conn),
3530 &nt_acl_type,
3531 ace->perms,
3532 S_ISDIR(sbuf->st_ex_mode));
3533 init_sec_ace(&nt_ace_list[num_aces++],
3534 &ace->trustee,
3535 nt_acl_type,
3536 acc,
3537 ace->ace_flags);
3540 /* The User must have access to a profile share - even
3541 * if we can't map the SID. */
3542 if (lp_profile_acls(SNUM(conn))) {
3543 add_or_replace_ace(nt_ace_list, &num_aces,
3544 &global_sid_Builtin_Users,
3545 SEC_ACE_TYPE_ACCESS_ALLOWED,
3546 FILE_GENERIC_ALL, 0);
3549 for (ace = dir_ace; ace != NULL; ace = ace->next) {
3550 uint32_t acc = map_canon_ace_perms(SNUM(conn),
3551 &nt_acl_type,
3552 ace->perms,
3553 S_ISDIR(sbuf->st_ex_mode));
3554 init_sec_ace(&nt_ace_list[num_aces++],
3555 &ace->trustee,
3556 nt_acl_type,
3557 acc,
3558 ace->ace_flags |
3559 SEC_ACE_FLAG_OBJECT_INHERIT|
3560 SEC_ACE_FLAG_CONTAINER_INHERIT|
3561 SEC_ACE_FLAG_INHERIT_ONLY);
3564 /* The User must have access to a profile share - even
3565 * if we can't map the SID. */
3566 if (lp_profile_acls(SNUM(conn))) {
3567 add_or_replace_ace(nt_ace_list, &num_aces,
3568 &global_sid_Builtin_Users,
3569 SEC_ACE_TYPE_ACCESS_ALLOWED,
3570 FILE_GENERIC_ALL,
3571 SEC_ACE_FLAG_OBJECT_INHERIT |
3572 SEC_ACE_FLAG_CONTAINER_INHERIT |
3573 SEC_ACE_FLAG_INHERIT_ONLY);
3577 * Merge POSIX default ACLs and normal ACLs into one NT ACE.
3578 * Win2K needs this to get the inheritance correct when replacing ACLs
3579 * on a directory tree. Based on work by Jim @ IBM.
3582 num_aces = merge_default_aces(nt_ace_list, num_aces);
3584 if (lp_profile_acls(SNUM(conn))) {
3585 for (i = 0; i < num_aces; i++) {
3586 if (dom_sid_equal(&nt_ace_list[i].trustee, &owner_sid)) {
3587 add_or_replace_ace(nt_ace_list, &num_aces,
3588 &orig_owner_sid,
3589 nt_ace_list[i].type,
3590 nt_ace_list[i].access_mask,
3591 nt_ace_list[i].flags);
3592 break;
3598 if (num_aces) {
3599 if((psa = make_sec_acl( talloc_tos(), NT4_ACL_REVISION, num_aces, nt_ace_list)) == NULL) {
3600 DEBUG(0,("get_nt_acl: Unable to malloc space for acl.\n"));
3601 goto done;
3604 } /* security_info & SECINFO_DACL */
3606 psd = make_standard_sec_desc(mem_ctx,
3607 (security_info & SECINFO_OWNER) ? &owner_sid : NULL,
3608 (security_info & SECINFO_GROUP) ? &group_sid : NULL,
3609 psa,
3610 &sd_size);
3612 if(!psd) {
3613 DEBUG(0,("get_nt_acl: Unable to malloc space for security descriptor.\n"));
3614 sd_size = 0;
3615 goto done;
3619 * Windows 2000: The DACL_PROTECTED flag in the security
3620 * descriptor marks the ACL as non-inheriting, i.e., no
3621 * ACEs from higher level directories propagate to this
3622 * ACL. In the POSIX ACL model permissions are only
3623 * inherited at file create time, so ACLs never contain
3624 * any ACEs that are inherited dynamically. The DACL_PROTECTED
3625 * flag doesn't seem to bother Windows NT.
3626 * Always set this if map acl inherit is turned off.
3628 if (pal == NULL || !lp_map_acl_inherit(SNUM(conn))) {
3629 psd->type |= SEC_DESC_DACL_PROTECTED;
3630 } else {
3631 psd->type |= pal->sd_type;
3634 if (psd->dacl) {
3635 dacl_sort_into_canonical_order(psd->dacl->aces, (unsigned int)psd->dacl->num_aces);
3638 *ppdesc = psd;
3640 done:
3642 if (posix_acl) {
3643 TALLOC_FREE(posix_acl);
3645 if (def_acl) {
3646 TALLOC_FREE(def_acl);
3648 free_canon_ace_list(file_ace);
3649 free_canon_ace_list(dir_ace);
3650 free_inherited_info(pal);
3651 TALLOC_FREE(nt_ace_list);
3653 return NT_STATUS_OK;
3656 NTSTATUS posix_fget_nt_acl(struct files_struct *fsp, uint32_t security_info,
3657 TALLOC_CTX *mem_ctx,
3658 struct security_descriptor **ppdesc)
3660 SMB_STRUCT_STAT sbuf;
3661 SMB_ACL_T posix_acl = NULL;
3662 struct pai_val *pal;
3663 TALLOC_CTX *frame = talloc_stackframe();
3664 NTSTATUS status;
3666 *ppdesc = NULL;
3668 DEBUG(10,("posix_fget_nt_acl: called for file %s\n",
3669 fsp_str_dbg(fsp)));
3671 /* can it happen that fsp_name == NULL ? */
3672 if (fsp->is_directory || fsp->fh->fd == -1) {
3673 status = posix_get_nt_acl(fsp->conn, fsp->fsp_name->base_name,
3674 security_info, mem_ctx, ppdesc);
3675 TALLOC_FREE(frame);
3676 return status;
3679 /* Get the stat struct for the owner info. */
3680 if(SMB_VFS_FSTAT(fsp, &sbuf) != 0) {
3681 TALLOC_FREE(frame);
3682 return map_nt_error_from_unix(errno);
3685 /* Get the ACL from the fd. */
3686 posix_acl = SMB_VFS_SYS_ACL_GET_FD(fsp, frame);
3688 pal = fload_inherited_info(fsp);
3690 status = posix_get_nt_acl_common(fsp->conn, fsp->fsp_name->base_name,
3691 &sbuf, pal, posix_acl, NULL,
3692 security_info, mem_ctx, ppdesc);
3693 TALLOC_FREE(frame);
3694 return status;
3697 NTSTATUS posix_get_nt_acl(struct connection_struct *conn, const char *name,
3698 uint32_t security_info,
3699 TALLOC_CTX *mem_ctx,
3700 struct security_descriptor **ppdesc)
3702 SMB_ACL_T posix_acl = NULL;
3703 SMB_ACL_T def_acl = NULL;
3704 struct pai_val *pal;
3705 struct smb_filename smb_fname;
3706 int ret;
3707 TALLOC_CTX *frame = talloc_stackframe();
3708 NTSTATUS status;
3710 *ppdesc = NULL;
3712 DEBUG(10,("posix_get_nt_acl: called for file %s\n", name ));
3714 ZERO_STRUCT(smb_fname);
3715 smb_fname.base_name = discard_const_p(char, name);
3717 /* Get the stat struct for the owner info. */
3718 if (lp_posix_pathnames()) {
3719 ret = SMB_VFS_LSTAT(conn, &smb_fname);
3720 } else {
3721 ret = SMB_VFS_STAT(conn, &smb_fname);
3724 if (ret == -1) {
3725 TALLOC_FREE(frame);
3726 return map_nt_error_from_unix(errno);
3729 /* Get the ACL from the path. */
3730 posix_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, name,
3731 SMB_ACL_TYPE_ACCESS, frame);
3733 /* If it's a directory get the default POSIX ACL. */
3734 if(S_ISDIR(smb_fname.st.st_ex_mode)) {
3735 def_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, name,
3736 SMB_ACL_TYPE_DEFAULT, frame);
3737 def_acl = free_empty_sys_acl(conn, def_acl);
3740 pal = load_inherited_info(conn, name);
3742 status = posix_get_nt_acl_common(conn, name, &smb_fname.st, pal,
3743 posix_acl, def_acl, security_info,
3744 mem_ctx,
3745 ppdesc);
3746 TALLOC_FREE(frame);
3747 return status;
3750 /****************************************************************************
3751 Try to chown a file. We will be able to chown it under the following conditions.
3753 1) If we have root privileges, then it will just work.
3754 2) If we have SeRestorePrivilege we can change the user + group to any other user.
3755 3) If we have SeTakeOwnershipPrivilege we can change the user to the current user.
3756 4) If we have write permission to the file and dos_filemodes is set
3757 then allow chown to the currently authenticated user.
3758 ****************************************************************************/
3760 NTSTATUS try_chown(files_struct *fsp, uid_t uid, gid_t gid)
3762 NTSTATUS status;
3764 if(!CAN_WRITE(fsp->conn)) {
3765 return NT_STATUS_MEDIA_WRITE_PROTECTED;
3768 /* Case (1). */
3769 status = vfs_chown_fsp(fsp, uid, gid);
3770 if (NT_STATUS_IS_OK(status)) {
3771 return status;
3774 /* Case (2) / (3) */
3775 if (lp_enable_privileges()) {
3776 bool has_take_ownership_priv = security_token_has_privilege(
3777 get_current_nttok(fsp->conn),
3778 SEC_PRIV_TAKE_OWNERSHIP);
3779 bool has_restore_priv = security_token_has_privilege(
3780 get_current_nttok(fsp->conn),
3781 SEC_PRIV_RESTORE);
3783 if (has_restore_priv) {
3784 ; /* Case (2) */
3785 } else if (has_take_ownership_priv) {
3786 /* Case (3) */
3787 if (uid == get_current_uid(fsp->conn)) {
3788 gid = (gid_t)-1;
3789 } else {
3790 has_take_ownership_priv = false;
3794 if (has_take_ownership_priv || has_restore_priv) {
3795 become_root();
3796 status = vfs_chown_fsp(fsp, uid, gid);
3797 unbecome_root();
3798 return status;
3802 /* Case (4). */
3803 if (!lp_dos_filemode(SNUM(fsp->conn))) {
3804 return NT_STATUS_ACCESS_DENIED;
3807 /* only allow chown to the current user. This is more secure,
3808 and also copes with the case where the SID in a take ownership ACL is
3809 a local SID on the users workstation
3811 if (uid != get_current_uid(fsp->conn)) {
3812 return NT_STATUS_ACCESS_DENIED;
3815 become_root();
3816 /* Keep the current file gid the same. */
3817 status = vfs_chown_fsp(fsp, uid, (gid_t)-1);
3818 unbecome_root();
3820 return status;
3823 #if 0
3824 /* Disable this - prevents ACL inheritance from the ACL editor. JRA. */
3826 /****************************************************************************
3827 Take care of parent ACL inheritance.
3828 ****************************************************************************/
3830 NTSTATUS append_parent_acl(files_struct *fsp,
3831 const struct security_descriptor *pcsd,
3832 struct security_descriptor **pp_new_sd)
3834 struct smb_filename *smb_dname = NULL;
3835 struct security_descriptor *parent_sd = NULL;
3836 files_struct *parent_fsp = NULL;
3837 TALLOC_CTX *mem_ctx = talloc_tos();
3838 char *parent_name = NULL;
3839 struct security_ace *new_ace = NULL;
3840 unsigned int num_aces = pcsd->dacl->num_aces;
3841 NTSTATUS status;
3842 int info;
3843 unsigned int i, j;
3844 struct security_descriptor *psd = dup_sec_desc(talloc_tos(), pcsd);
3845 bool is_dacl_protected = (pcsd->type & SEC_DESC_DACL_PROTECTED);
3847 if (psd == NULL) {
3848 return NT_STATUS_NO_MEMORY;
3851 if (!parent_dirname(mem_ctx, fsp->fsp_name->base_name, &parent_name,
3852 NULL)) {
3853 return NT_STATUS_NO_MEMORY;
3856 status = create_synthetic_smb_fname(mem_ctx, parent_name, NULL, NULL,
3857 &smb_dname);
3858 if (!NT_STATUS_IS_OK(status)) {
3859 goto fail;
3862 status = SMB_VFS_CREATE_FILE(
3863 fsp->conn, /* conn */
3864 NULL, /* req */
3865 0, /* root_dir_fid */
3866 smb_dname, /* fname */
3867 FILE_READ_ATTRIBUTES, /* access_mask */
3868 FILE_SHARE_NONE, /* share_access */
3869 FILE_OPEN, /* create_disposition*/
3870 FILE_DIRECTORY_FILE, /* create_options */
3871 0, /* file_attributes */
3872 INTERNAL_OPEN_ONLY, /* oplock_request */
3873 0, /* allocation_size */
3874 NULL, /* sd */
3875 NULL, /* ea_list */
3876 &parent_fsp, /* result */
3877 &info); /* pinfo */
3879 if (!NT_STATUS_IS_OK(status)) {
3880 TALLOC_FREE(smb_dname);
3881 return status;
3884 status = SMB_VFS_GET_NT_ACL(parent_fsp->conn, smb_dname->base_name,
3885 SECINFO_DACL, &parent_sd );
3887 close_file(NULL, parent_fsp, NORMAL_CLOSE);
3888 TALLOC_FREE(smb_dname);
3890 if (!NT_STATUS_IS_OK(status)) {
3891 return status;
3895 * Make room for potentially all the ACLs from
3896 * the parent. We used to add the ugw triple here,
3897 * as we knew we were dealing with POSIX ACLs.
3898 * We no longer need to do so as we can guarentee
3899 * that a default ACL from the parent directory will
3900 * be well formed for POSIX ACLs if it came from a
3901 * POSIX ACL source, and if we're not writing to a
3902 * POSIX ACL sink then we don't care if it's not well
3903 * formed. JRA.
3906 num_aces += parent_sd->dacl->num_aces;
3908 if((new_ace = talloc_zero_array(mem_ctx, struct security_ace,
3909 num_aces)) == NULL) {
3910 return NT_STATUS_NO_MEMORY;
3913 /* Start by copying in all the given ACE entries. */
3914 for (i = 0; i < psd->dacl->num_aces; i++) {
3915 sec_ace_copy(&new_ace[i], &psd->dacl->aces[i]);
3919 * Note that we're ignoring "inherit permissions" here
3920 * as that really only applies to newly created files. JRA.
3923 /* Finally append any inherited ACEs. */
3924 for (j = 0; j < parent_sd->dacl->num_aces; j++) {
3925 struct security_ace *se = &parent_sd->dacl->aces[j];
3927 if (fsp->is_directory) {
3928 if (!(se->flags & SEC_ACE_FLAG_CONTAINER_INHERIT)) {
3929 /* Doesn't apply to a directory - ignore. */
3930 DEBUG(10,("append_parent_acl: directory %s "
3931 "ignoring non container "
3932 "inherit flags %u on ACE with sid %s "
3933 "from parent %s\n",
3934 fsp_str_dbg(fsp),
3935 (unsigned int)se->flags,
3936 sid_string_dbg(&se->trustee),
3937 parent_name));
3938 continue;
3940 } else {
3941 if (!(se->flags & SEC_ACE_FLAG_OBJECT_INHERIT)) {
3942 /* Doesn't apply to a file - ignore. */
3943 DEBUG(10,("append_parent_acl: file %s "
3944 "ignoring non object "
3945 "inherit flags %u on ACE with sid %s "
3946 "from parent %s\n",
3947 fsp_str_dbg(fsp),
3948 (unsigned int)se->flags,
3949 sid_string_dbg(&se->trustee),
3950 parent_name));
3951 continue;
3955 if (is_dacl_protected) {
3956 /* If the DACL is protected it means we must
3957 * not overwrite an existing ACE entry with the
3958 * same SID. This is order N^2. Ouch :-(. JRA. */
3959 unsigned int k;
3960 for (k = 0; k < psd->dacl->num_aces; k++) {
3961 if (dom_sid_equal(&psd->dacl->aces[k].trustee,
3962 &se->trustee)) {
3963 break;
3966 if (k < psd->dacl->num_aces) {
3967 /* SID matched. Ignore. */
3968 DEBUG(10,("append_parent_acl: path %s "
3969 "ignoring ACE with protected sid %s "
3970 "from parent %s\n",
3971 fsp_str_dbg(fsp),
3972 sid_string_dbg(&se->trustee),
3973 parent_name));
3974 continue;
3978 sec_ace_copy(&new_ace[i], se);
3979 if (se->flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) {
3980 new_ace[i].flags &= ~(SEC_ACE_FLAG_VALID_INHERIT);
3982 new_ace[i].flags |= SEC_ACE_FLAG_INHERITED_ACE;
3984 if (fsp->is_directory) {
3986 * Strip off any inherit only. It's applied.
3988 new_ace[i].flags &= ~(SEC_ACE_FLAG_INHERIT_ONLY);
3989 if (se->flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) {
3990 /* No further inheritance. */
3991 new_ace[i].flags &=
3992 ~(SEC_ACE_FLAG_CONTAINER_INHERIT|
3993 SEC_ACE_FLAG_OBJECT_INHERIT);
3995 } else {
3997 * Strip off any container or inherit
3998 * flags, they can't apply to objects.
4000 new_ace[i].flags &= ~(SEC_ACE_FLAG_CONTAINER_INHERIT|
4001 SEC_ACE_FLAG_INHERIT_ONLY|
4002 SEC_ACE_FLAG_NO_PROPAGATE_INHERIT);
4004 i++;
4006 DEBUG(10,("append_parent_acl: path %s "
4007 "inheriting ACE with sid %s "
4008 "from parent %s\n",
4009 fsp_str_dbg(fsp),
4010 sid_string_dbg(&se->trustee),
4011 parent_name));
4014 psd->dacl->aces = new_ace;
4015 psd->dacl->num_aces = i;
4016 psd->type &= ~(SEC_DESC_DACL_AUTO_INHERITED|
4017 SEC_DESC_DACL_AUTO_INHERIT_REQ);
4019 *pp_new_sd = psd;
4020 return status;
4022 #endif
4024 /****************************************************************************
4025 Reply to set a security descriptor on an fsp. security_info_sent is the
4026 description of the following NT ACL.
4027 This should be the only external function needed for the UNIX style set ACL.
4028 We make a copy of psd_orig as internal functions modify the elements inside
4029 it, even though it's a const pointer.
4030 ****************************************************************************/
4032 NTSTATUS set_nt_acl(files_struct *fsp, uint32 security_info_sent, const struct security_descriptor *psd_orig)
4034 connection_struct *conn = fsp->conn;
4035 uid_t user = (uid_t)-1;
4036 gid_t grp = (gid_t)-1;
4037 struct dom_sid file_owner_sid;
4038 struct dom_sid file_grp_sid;
4039 canon_ace *file_ace_list = NULL;
4040 canon_ace *dir_ace_list = NULL;
4041 bool acl_perms = False;
4042 mode_t orig_mode = (mode_t)0;
4043 NTSTATUS status;
4044 bool set_acl_as_root = false;
4045 bool acl_set_support = false;
4046 bool ret = false;
4047 struct security_descriptor *psd = NULL;
4049 DEBUG(10,("set_nt_acl: called for file %s\n",
4050 fsp_str_dbg(fsp)));
4052 if (!CAN_WRITE(conn)) {
4053 DEBUG(10,("set acl rejected on read-only share\n"));
4054 return NT_STATUS_MEDIA_WRITE_PROTECTED;
4057 if (!psd_orig) {
4058 return NT_STATUS_INVALID_PARAMETER;
4061 psd = dup_sec_desc(talloc_tos(), psd_orig);
4062 if (!psd) {
4063 return NT_STATUS_NO_MEMORY;
4067 * Get the current state of the file.
4070 status = vfs_stat_fsp(fsp);
4071 if (!NT_STATUS_IS_OK(status)) {
4072 return status;
4075 /* Save the original element we check against. */
4076 orig_mode = fsp->fsp_name->st.st_ex_mode;
4079 * Unpack the user/group/world id's.
4082 /* POSIX can't cope with missing owner/group. */
4083 if ((security_info_sent & SECINFO_OWNER) && (psd->owner_sid == NULL)) {
4084 security_info_sent &= ~SECINFO_OWNER;
4086 if ((security_info_sent & SECINFO_GROUP) && (psd->group_sid == NULL)) {
4087 security_info_sent &= ~SECINFO_GROUP;
4090 status = unpack_nt_owners( conn, &user, &grp, security_info_sent, psd);
4091 if (!NT_STATUS_IS_OK(status)) {
4092 return status;
4096 * Do we need to chown ? If so this must be done first as the incoming
4097 * CREATOR_OWNER acl will be relative to the *new* owner, not the old.
4098 * Noticed by Simo.
4101 if (((user != (uid_t)-1) && (fsp->fsp_name->st.st_ex_uid != user)) ||
4102 (( grp != (gid_t)-1) && (fsp->fsp_name->st.st_ex_gid != grp))) {
4104 DEBUG(3,("set_nt_acl: chown %s. uid = %u, gid = %u.\n",
4105 fsp_str_dbg(fsp), (unsigned int)user,
4106 (unsigned int)grp));
4108 status = try_chown(fsp, user, grp);
4109 if(!NT_STATUS_IS_OK(status)) {
4110 DEBUG(3,("set_nt_acl: chown %s, %u, %u failed. Error "
4111 "= %s.\n", fsp_str_dbg(fsp),
4112 (unsigned int)user,
4113 (unsigned int)grp,
4114 nt_errstr(status)));
4115 return status;
4119 * Recheck the current state of the file, which may have changed.
4120 * (suid/sgid bits, for instance)
4123 status = vfs_stat_fsp(fsp);
4124 if (!NT_STATUS_IS_OK(status)) {
4125 return status;
4128 /* Save the original element we check against. */
4129 orig_mode = fsp->fsp_name->st.st_ex_mode;
4131 /* If we successfully chowned, we know we must
4132 * be able to set the acl, so do it as root.
4134 set_acl_as_root = true;
4137 create_file_sids(&fsp->fsp_name->st, &file_owner_sid, &file_grp_sid);
4139 if((security_info_sent & SECINFO_DACL) &&
4140 (psd->type & SEC_DESC_DACL_PRESENT) &&
4141 (psd->dacl == NULL)) {
4142 struct security_ace ace[3];
4144 /* We can't have NULL DACL in POSIX.
4145 Use owner/group/Everyone -> full access. */
4147 init_sec_ace(&ace[0],
4148 &file_owner_sid,
4149 SEC_ACE_TYPE_ACCESS_ALLOWED,
4150 GENERIC_ALL_ACCESS,
4152 init_sec_ace(&ace[1],
4153 &file_grp_sid,
4154 SEC_ACE_TYPE_ACCESS_ALLOWED,
4155 GENERIC_ALL_ACCESS,
4157 init_sec_ace(&ace[2],
4158 &global_sid_World,
4159 SEC_ACE_TYPE_ACCESS_ALLOWED,
4160 GENERIC_ALL_ACCESS,
4162 psd->dacl = make_sec_acl(talloc_tos(),
4163 NT4_ACL_REVISION,
4165 ace);
4166 if (psd->dacl == NULL) {
4167 return NT_STATUS_NO_MEMORY;
4169 security_acl_map_generic(psd->dacl, &file_generic_mapping);
4172 acl_perms = unpack_canon_ace(fsp, &fsp->fsp_name->st, &file_owner_sid,
4173 &file_grp_sid, &file_ace_list,
4174 &dir_ace_list, security_info_sent, psd);
4176 /* Ignore W2K traverse DACL set. */
4177 if (!file_ace_list && !dir_ace_list) {
4178 return NT_STATUS_OK;
4181 if (!acl_perms) {
4182 DEBUG(3,("set_nt_acl: cannot set permissions\n"));
4183 free_canon_ace_list(file_ace_list);
4184 free_canon_ace_list(dir_ace_list);
4185 return NT_STATUS_ACCESS_DENIED;
4189 * Only change security if we got a DACL.
4192 if(!(security_info_sent & SECINFO_DACL) || (psd->dacl == NULL)) {
4193 free_canon_ace_list(file_ace_list);
4194 free_canon_ace_list(dir_ace_list);
4195 return NT_STATUS_OK;
4199 * Try using the POSIX ACL set first. Fall back to chmod if
4200 * we have no ACL support on this filesystem.
4203 if (acl_perms && file_ace_list) {
4204 if (set_acl_as_root) {
4205 become_root();
4207 ret = set_canon_ace_list(fsp, file_ace_list, false,
4208 &fsp->fsp_name->st, &acl_set_support);
4209 if (set_acl_as_root) {
4210 unbecome_root();
4212 if (acl_set_support && ret == false) {
4213 DEBUG(3,("set_nt_acl: failed to set file acl on file "
4214 "%s (%s).\n", fsp_str_dbg(fsp),
4215 strerror(errno)));
4216 free_canon_ace_list(file_ace_list);
4217 free_canon_ace_list(dir_ace_list);
4218 return map_nt_error_from_unix(errno);
4222 if (acl_perms && acl_set_support && fsp->is_directory) {
4223 if (dir_ace_list) {
4224 if (set_acl_as_root) {
4225 become_root();
4227 ret = set_canon_ace_list(fsp, dir_ace_list, true,
4228 &fsp->fsp_name->st,
4229 &acl_set_support);
4230 if (set_acl_as_root) {
4231 unbecome_root();
4233 if (ret == false) {
4234 DEBUG(3,("set_nt_acl: failed to set default "
4235 "acl on directory %s (%s).\n",
4236 fsp_str_dbg(fsp), strerror(errno)));
4237 free_canon_ace_list(file_ace_list);
4238 free_canon_ace_list(dir_ace_list);
4239 return map_nt_error_from_unix(errno);
4241 } else {
4242 int sret = -1;
4245 * No default ACL - delete one if it exists.
4248 if (set_acl_as_root) {
4249 become_root();
4251 sret = SMB_VFS_SYS_ACL_DELETE_DEF_FILE(conn,
4252 fsp->fsp_name->base_name);
4253 if (set_acl_as_root) {
4254 unbecome_root();
4256 if (sret == -1) {
4257 if (acl_group_override(conn, fsp->fsp_name)) {
4258 DEBUG(5,("set_nt_acl: acl group "
4259 "control on and current user "
4260 "in file %s primary group. "
4261 "Override delete_def_acl\n",
4262 fsp_str_dbg(fsp)));
4264 become_root();
4265 sret =
4266 SMB_VFS_SYS_ACL_DELETE_DEF_FILE(
4267 conn,
4268 fsp->fsp_name->base_name);
4269 unbecome_root();
4272 if (sret == -1) {
4273 DEBUG(3,("set_nt_acl: sys_acl_delete_def_file failed (%s)\n", strerror(errno)));
4274 free_canon_ace_list(file_ace_list);
4275 free_canon_ace_list(dir_ace_list);
4276 return map_nt_error_from_unix(errno);
4282 if (acl_set_support) {
4283 if (set_acl_as_root) {
4284 become_root();
4286 store_inheritance_attributes(fsp,
4287 file_ace_list,
4288 dir_ace_list,
4289 psd->type);
4290 if (set_acl_as_root) {
4291 unbecome_root();
4296 * If we cannot set using POSIX ACLs we fall back to checking if we need to chmod.
4299 if(!acl_set_support && acl_perms) {
4300 mode_t posix_perms;
4302 if (!convert_canon_ace_to_posix_perms( fsp, file_ace_list, &posix_perms)) {
4303 free_canon_ace_list(file_ace_list);
4304 free_canon_ace_list(dir_ace_list);
4305 DEBUG(3,("set_nt_acl: failed to convert file acl to "
4306 "posix permissions for file %s.\n",
4307 fsp_str_dbg(fsp)));
4308 return NT_STATUS_ACCESS_DENIED;
4311 if (orig_mode != posix_perms) {
4312 int sret = -1;
4314 DEBUG(3,("set_nt_acl: chmod %s. perms = 0%o.\n",
4315 fsp_str_dbg(fsp), (unsigned int)posix_perms));
4317 if (set_acl_as_root) {
4318 become_root();
4320 sret = SMB_VFS_CHMOD(conn, fsp->fsp_name->base_name,
4321 posix_perms);
4322 if (set_acl_as_root) {
4323 unbecome_root();
4325 if(sret == -1) {
4326 if (acl_group_override(conn, fsp->fsp_name)) {
4327 DEBUG(5,("set_nt_acl: acl group "
4328 "control on and current user "
4329 "in file %s primary group. "
4330 "Override chmod\n",
4331 fsp_str_dbg(fsp)));
4333 become_root();
4334 sret = SMB_VFS_CHMOD(conn,
4335 fsp->fsp_name->base_name,
4336 posix_perms);
4337 unbecome_root();
4340 if (sret == -1) {
4341 DEBUG(3,("set_nt_acl: chmod %s, 0%o "
4342 "failed. Error = %s.\n",
4343 fsp_str_dbg(fsp),
4344 (unsigned int)posix_perms,
4345 strerror(errno)));
4346 free_canon_ace_list(file_ace_list);
4347 free_canon_ace_list(dir_ace_list);
4348 return map_nt_error_from_unix(errno);
4354 free_canon_ace_list(file_ace_list);
4355 free_canon_ace_list(dir_ace_list);
4357 /* Ensure the stat struct in the fsp is correct. */
4358 status = vfs_stat_fsp(fsp);
4360 return NT_STATUS_OK;
4363 /****************************************************************************
4364 Get the actual group bits stored on a file with an ACL. Has no effect if
4365 the file has no ACL. Needed in dosmode code where the stat() will return
4366 the mask bits, not the real group bits, for a file with an ACL.
4367 ****************************************************************************/
4369 int get_acl_group_bits( connection_struct *conn, const char *fname, mode_t *mode )
4371 int entry_id = SMB_ACL_FIRST_ENTRY;
4372 SMB_ACL_ENTRY_T entry;
4373 SMB_ACL_T posix_acl;
4374 int result = -1;
4376 posix_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, fname,
4377 SMB_ACL_TYPE_ACCESS, talloc_tos());
4378 if (posix_acl == (SMB_ACL_T)NULL)
4379 return -1;
4381 while (sys_acl_get_entry(posix_acl, entry_id, &entry) == 1) {
4382 SMB_ACL_TAG_T tagtype;
4383 SMB_ACL_PERMSET_T permset;
4385 entry_id = SMB_ACL_NEXT_ENTRY;
4387 if (sys_acl_get_tag_type(entry, &tagtype) ==-1)
4388 break;
4390 if (tagtype == SMB_ACL_GROUP_OBJ) {
4391 if (sys_acl_get_permset(entry, &permset) == -1) {
4392 break;
4393 } else {
4394 *mode &= ~(S_IRGRP|S_IWGRP|S_IXGRP);
4395 *mode |= (sys_acl_get_perm(permset, SMB_ACL_READ) ? S_IRGRP : 0);
4396 *mode |= (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? S_IWGRP : 0);
4397 *mode |= (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? S_IXGRP : 0);
4398 result = 0;
4399 break;
4403 TALLOC_FREE(posix_acl);
4404 return result;
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 ****************************************************************************/
4412 static int chmod_acl_internals( connection_struct *conn, SMB_ACL_T posix_acl, mode_t mode)
4414 int entry_id = SMB_ACL_FIRST_ENTRY;
4415 SMB_ACL_ENTRY_T entry;
4416 int num_entries = 0;
4418 while ( sys_acl_get_entry(posix_acl, entry_id, &entry) == 1) {
4419 SMB_ACL_TAG_T tagtype;
4420 SMB_ACL_PERMSET_T permset;
4421 mode_t perms;
4423 entry_id = SMB_ACL_NEXT_ENTRY;
4425 if (sys_acl_get_tag_type(entry, &tagtype) == -1)
4426 return -1;
4428 if (sys_acl_get_permset(entry, &permset) == -1)
4429 return -1;
4431 num_entries++;
4433 switch(tagtype) {
4434 case SMB_ACL_USER_OBJ:
4435 perms = unix_perms_to_acl_perms(mode, S_IRUSR, S_IWUSR, S_IXUSR);
4436 break;
4437 case SMB_ACL_GROUP_OBJ:
4438 perms = unix_perms_to_acl_perms(mode, S_IRGRP, S_IWGRP, S_IXGRP);
4439 break;
4440 case SMB_ACL_MASK:
4442 * FIXME: The ACL_MASK entry permissions should really be set to
4443 * the union of the permissions of all ACL_USER,
4444 * ACL_GROUP_OBJ, and ACL_GROUP entries. That's what
4445 * acl_calc_mask() does, but Samba ACLs doesn't provide it.
4447 perms = S_IRUSR|S_IWUSR|S_IXUSR;
4448 break;
4449 case SMB_ACL_OTHER:
4450 perms = unix_perms_to_acl_perms(mode, S_IROTH, S_IWOTH, S_IXOTH);
4451 break;
4452 default:
4453 continue;
4456 if (map_acl_perms_to_permset(conn, perms, &permset) == -1)
4457 return -1;
4459 if (sys_acl_set_permset(entry, permset) == -1)
4460 return -1;
4464 * If this is a simple 3 element ACL or no elements then it's a standard
4465 * UNIX permission set. Just use chmod...
4468 if ((num_entries == 3) || (num_entries == 0))
4469 return -1;
4471 return 0;
4474 /****************************************************************************
4475 Get the access ACL of FROM, do a chmod by setting the ACL USER_OBJ,
4476 GROUP_OBJ and OTHER bits in an ACL and set the mask to rwx. Set the
4477 resulting ACL on TO. Note that name is in UNIX character set.
4478 ****************************************************************************/
4480 static int copy_access_posix_acl(connection_struct *conn, const char *from, const char *to, mode_t mode)
4482 SMB_ACL_T posix_acl = NULL;
4483 int ret = -1;
4485 if ((posix_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, from,
4486 SMB_ACL_TYPE_ACCESS,
4487 talloc_tos())) == NULL)
4488 return -1;
4490 if ((ret = chmod_acl_internals(conn, posix_acl, mode)) == -1)
4491 goto done;
4493 ret = SMB_VFS_SYS_ACL_SET_FILE(conn, to, SMB_ACL_TYPE_ACCESS, posix_acl);
4495 done:
4497 TALLOC_FREE(posix_acl);
4498 return ret;
4501 /****************************************************************************
4502 Do a chmod by setting the ACL USER_OBJ, GROUP_OBJ and OTHER bits in an ACL
4503 and set the mask to rwx. Needed to preserve complex ACLs set by NT.
4504 Note that name is in UNIX character set.
4505 ****************************************************************************/
4507 int chmod_acl(connection_struct *conn, const char *name, mode_t mode)
4509 return copy_access_posix_acl(conn, name, name, mode);
4512 /****************************************************************************
4513 Check for an existing default POSIX ACL on a directory.
4514 ****************************************************************************/
4516 static bool directory_has_default_posix_acl(connection_struct *conn, const char *fname)
4518 SMB_ACL_T def_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, fname,
4519 SMB_ACL_TYPE_DEFAULT,
4520 talloc_tos());
4521 bool has_acl = False;
4522 SMB_ACL_ENTRY_T entry;
4524 if (def_acl != NULL && (sys_acl_get_entry(def_acl, SMB_ACL_FIRST_ENTRY, &entry) == 1)) {
4525 has_acl = True;
4528 if (def_acl) {
4529 TALLOC_FREE(def_acl);
4531 return has_acl;
4534 /****************************************************************************
4535 If the parent directory has no default ACL but it does have an Access ACL,
4536 inherit this Access ACL to file name.
4537 ****************************************************************************/
4539 int inherit_access_posix_acl(connection_struct *conn, const char *inherit_from_dir,
4540 const char *name, mode_t mode)
4542 if (directory_has_default_posix_acl(conn, inherit_from_dir))
4543 return 0;
4545 return copy_access_posix_acl(conn, inherit_from_dir, name, mode);
4548 /****************************************************************************
4549 Do an fchmod by setting the ACL USER_OBJ, GROUP_OBJ and OTHER bits in an ACL
4550 and set the mask to rwx. Needed to preserve complex ACLs set by NT.
4551 ****************************************************************************/
4553 int fchmod_acl(files_struct *fsp, mode_t mode)
4555 connection_struct *conn = fsp->conn;
4556 SMB_ACL_T posix_acl = NULL;
4557 int ret = -1;
4559 if ((posix_acl = SMB_VFS_SYS_ACL_GET_FD(fsp, talloc_tos())) == NULL)
4560 return -1;
4562 if ((ret = chmod_acl_internals(conn, posix_acl, mode)) == -1)
4563 goto done;
4565 ret = SMB_VFS_SYS_ACL_SET_FD(fsp, posix_acl);
4567 done:
4569 TALLOC_FREE(posix_acl);
4570 return ret;
4573 /****************************************************************************
4574 Map from wire type to permset.
4575 ****************************************************************************/
4577 static bool unix_ex_wire_to_permset(connection_struct *conn, unsigned char wire_perm, SMB_ACL_PERMSET_T *p_permset)
4579 if (wire_perm & ~(SMB_POSIX_ACL_READ|SMB_POSIX_ACL_WRITE|SMB_POSIX_ACL_EXECUTE)) {
4580 return False;
4583 if (sys_acl_clear_perms(*p_permset) == -1) {
4584 return False;
4587 if (wire_perm & SMB_POSIX_ACL_READ) {
4588 if (sys_acl_add_perm(*p_permset, SMB_ACL_READ) == -1) {
4589 return False;
4592 if (wire_perm & SMB_POSIX_ACL_WRITE) {
4593 if (sys_acl_add_perm(*p_permset, SMB_ACL_WRITE) == -1) {
4594 return False;
4597 if (wire_perm & SMB_POSIX_ACL_EXECUTE) {
4598 if (sys_acl_add_perm(*p_permset, SMB_ACL_EXECUTE) == -1) {
4599 return False;
4602 return True;
4605 /****************************************************************************
4606 Map from wire type to tagtype.
4607 ****************************************************************************/
4609 static bool unix_ex_wire_to_tagtype(unsigned char wire_tt, SMB_ACL_TAG_T *p_tt)
4611 switch (wire_tt) {
4612 case SMB_POSIX_ACL_USER_OBJ:
4613 *p_tt = SMB_ACL_USER_OBJ;
4614 break;
4615 case SMB_POSIX_ACL_USER:
4616 *p_tt = SMB_ACL_USER;
4617 break;
4618 case SMB_POSIX_ACL_GROUP_OBJ:
4619 *p_tt = SMB_ACL_GROUP_OBJ;
4620 break;
4621 case SMB_POSIX_ACL_GROUP:
4622 *p_tt = SMB_ACL_GROUP;
4623 break;
4624 case SMB_POSIX_ACL_MASK:
4625 *p_tt = SMB_ACL_MASK;
4626 break;
4627 case SMB_POSIX_ACL_OTHER:
4628 *p_tt = SMB_ACL_OTHER;
4629 break;
4630 default:
4631 return False;
4633 return True;
4636 /****************************************************************************
4637 Create a new POSIX acl from wire permissions.
4638 FIXME ! How does the share mask/mode fit into this.... ?
4639 ****************************************************************************/
4641 static SMB_ACL_T create_posix_acl_from_wire(connection_struct *conn,
4642 uint16 num_acls,
4643 const char *pdata,
4644 TALLOC_CTX *mem_ctx)
4646 unsigned int i;
4647 SMB_ACL_T the_acl = sys_acl_init(mem_ctx);
4649 if (the_acl == NULL) {
4650 return NULL;
4653 for (i = 0; i < num_acls; i++) {
4654 SMB_ACL_ENTRY_T the_entry;
4655 SMB_ACL_PERMSET_T the_permset;
4656 SMB_ACL_TAG_T tag_type;
4658 if (sys_acl_create_entry(&the_acl, &the_entry) == -1) {
4659 DEBUG(0,("create_posix_acl_from_wire: Failed to create entry %u. (%s)\n",
4660 i, strerror(errno) ));
4661 goto fail;
4664 if (!unix_ex_wire_to_tagtype(CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)), &tag_type)) {
4665 DEBUG(0,("create_posix_acl_from_wire: invalid wire tagtype %u on entry %u.\n",
4666 CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)), i ));
4667 goto fail;
4670 if (sys_acl_set_tag_type(the_entry, tag_type) == -1) {
4671 DEBUG(0,("create_posix_acl_from_wire: Failed to set tagtype on entry %u. (%s)\n",
4672 i, strerror(errno) ));
4673 goto fail;
4676 /* Get the permset pointer from the new ACL entry. */
4677 if (sys_acl_get_permset(the_entry, &the_permset) == -1) {
4678 DEBUG(0,("create_posix_acl_from_wire: Failed to get permset on entry %u. (%s)\n",
4679 i, strerror(errno) ));
4680 goto fail;
4683 /* Map from wire to permissions. */
4684 if (!unix_ex_wire_to_permset(conn, CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)+1), &the_permset)) {
4685 DEBUG(0,("create_posix_acl_from_wire: invalid permset %u on entry %u.\n",
4686 CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE) + 1), i ));
4687 goto fail;
4690 /* Now apply to the new ACL entry. */
4691 if (sys_acl_set_permset(the_entry, the_permset) == -1) {
4692 DEBUG(0,("create_posix_acl_from_wire: Failed to add permset on entry %u. (%s)\n",
4693 i, strerror(errno) ));
4694 goto fail;
4697 if (tag_type == SMB_ACL_USER) {
4698 uint32 uidval = IVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
4699 uid_t uid = (uid_t)uidval;
4700 if (sys_acl_set_qualifier(the_entry,(void *)&uid) == -1) {
4701 DEBUG(0,("create_posix_acl_from_wire: Failed to set uid %u on entry %u. (%s)\n",
4702 (unsigned int)uid, i, strerror(errno) ));
4703 goto fail;
4707 if (tag_type == SMB_ACL_GROUP) {
4708 uint32 gidval = IVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
4709 gid_t gid = (uid_t)gidval;
4710 if (sys_acl_set_qualifier(the_entry,(void *)&gid) == -1) {
4711 DEBUG(0,("create_posix_acl_from_wire: Failed to set gid %u on entry %u. (%s)\n",
4712 (unsigned int)gid, i, strerror(errno) ));
4713 goto fail;
4718 return the_acl;
4720 fail:
4722 if (the_acl != NULL) {
4723 TALLOC_FREE(the_acl);
4725 return NULL;
4728 /****************************************************************************
4729 Calls from UNIX extensions - Default POSIX ACL set.
4730 If num_def_acls == 0 and not a directory just return. If it is a directory
4731 and num_def_acls == 0 then remove the default acl. Else set the default acl
4732 on the directory.
4733 ****************************************************************************/
4735 bool set_unix_posix_default_acl(connection_struct *conn, const char *fname, const SMB_STRUCT_STAT *psbuf,
4736 uint16 num_def_acls, const char *pdata)
4738 SMB_ACL_T def_acl = NULL;
4740 if (!S_ISDIR(psbuf->st_ex_mode)) {
4741 if (num_def_acls) {
4742 DEBUG(5,("set_unix_posix_default_acl: Can't set default ACL on non-directory file %s\n", fname ));
4743 errno = EISDIR;
4744 return False;
4745 } else {
4746 return True;
4750 if (!num_def_acls) {
4751 /* Remove the default ACL. */
4752 if (SMB_VFS_SYS_ACL_DELETE_DEF_FILE(conn, fname) == -1) {
4753 DEBUG(5,("set_unix_posix_default_acl: acl_delete_def_file failed on directory %s (%s)\n",
4754 fname, strerror(errno) ));
4755 return False;
4757 return True;
4760 if ((def_acl = create_posix_acl_from_wire(conn, num_def_acls,
4761 pdata,
4762 talloc_tos())) == NULL) {
4763 return False;
4766 if (SMB_VFS_SYS_ACL_SET_FILE(conn, fname, SMB_ACL_TYPE_DEFAULT, def_acl) == -1) {
4767 DEBUG(5,("set_unix_posix_default_acl: acl_set_file failed on directory %s (%s)\n",
4768 fname, strerror(errno) ));
4769 TALLOC_FREE(def_acl);
4770 return False;
4773 DEBUG(10,("set_unix_posix_default_acl: set default acl for file %s\n", fname ));
4774 TALLOC_FREE(def_acl);
4775 return True;
4778 /****************************************************************************
4779 Remove an ACL from a file. As we don't have acl_delete_entry() available
4780 we must read the current acl and copy all entries except MASK, USER and GROUP
4781 to a new acl, then set that. This (at least on Linux) causes any ACL to be
4782 removed.
4783 FIXME ! How does the share mask/mode fit into this.... ?
4784 ****************************************************************************/
4786 static bool remove_posix_acl(connection_struct *conn, files_struct *fsp, const char *fname)
4788 SMB_ACL_T file_acl = NULL;
4789 int entry_id = SMB_ACL_FIRST_ENTRY;
4790 SMB_ACL_ENTRY_T entry;
4791 bool ret = False;
4792 /* Create a new ACL with only 3 entries, u/g/w. */
4793 SMB_ACL_T new_file_acl = sys_acl_init(talloc_tos());
4794 SMB_ACL_ENTRY_T user_ent = NULL;
4795 SMB_ACL_ENTRY_T group_ent = NULL;
4796 SMB_ACL_ENTRY_T other_ent = NULL;
4798 if (new_file_acl == NULL) {
4799 DEBUG(5,("remove_posix_acl: failed to init new ACL with 3 entries for file %s.\n", fname));
4800 return False;
4803 /* Now create the u/g/w entries. */
4804 if (sys_acl_create_entry(&new_file_acl, &user_ent) == -1) {
4805 DEBUG(5,("remove_posix_acl: Failed to create user entry for file %s. (%s)\n",
4806 fname, strerror(errno) ));
4807 goto done;
4809 if (sys_acl_set_tag_type(user_ent, SMB_ACL_USER_OBJ) == -1) {
4810 DEBUG(5,("remove_posix_acl: Failed to set user entry for file %s. (%s)\n",
4811 fname, strerror(errno) ));
4812 goto done;
4815 if (sys_acl_create_entry(&new_file_acl, &group_ent) == -1) {
4816 DEBUG(5,("remove_posix_acl: Failed to create group entry for file %s. (%s)\n",
4817 fname, strerror(errno) ));
4818 goto done;
4820 if (sys_acl_set_tag_type(group_ent, SMB_ACL_GROUP_OBJ) == -1) {
4821 DEBUG(5,("remove_posix_acl: Failed to set group entry for file %s. (%s)\n",
4822 fname, strerror(errno) ));
4823 goto done;
4826 if (sys_acl_create_entry(&new_file_acl, &other_ent) == -1) {
4827 DEBUG(5,("remove_posix_acl: Failed to create other entry for file %s. (%s)\n",
4828 fname, strerror(errno) ));
4829 goto done;
4831 if (sys_acl_set_tag_type(other_ent, SMB_ACL_OTHER) == -1) {
4832 DEBUG(5,("remove_posix_acl: Failed to set other entry for file %s. (%s)\n",
4833 fname, strerror(errno) ));
4834 goto done;
4837 /* Get the current file ACL. */
4838 if (fsp && fsp->fh->fd != -1) {
4839 file_acl = SMB_VFS_SYS_ACL_GET_FD(fsp, talloc_tos());
4840 } else {
4841 file_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, fname,
4842 SMB_ACL_TYPE_ACCESS,
4843 talloc_tos());
4846 if (file_acl == NULL) {
4847 /* This is only returned if an error occurred. Even for a file with
4848 no acl a u/g/w acl should be returned. */
4849 DEBUG(5,("remove_posix_acl: failed to get ACL from file %s (%s).\n",
4850 fname, strerror(errno) ));
4851 goto done;
4854 while ( sys_acl_get_entry(file_acl, entry_id, &entry) == 1) {
4855 SMB_ACL_TAG_T tagtype;
4856 SMB_ACL_PERMSET_T permset;
4858 entry_id = SMB_ACL_NEXT_ENTRY;
4860 if (sys_acl_get_tag_type(entry, &tagtype) == -1) {
4861 DEBUG(5,("remove_posix_acl: failed to get tagtype from ACL on file %s (%s).\n",
4862 fname, strerror(errno) ));
4863 goto done;
4866 if (sys_acl_get_permset(entry, &permset) == -1) {
4867 DEBUG(5,("remove_posix_acl: failed to get permset from ACL on file %s (%s).\n",
4868 fname, strerror(errno) ));
4869 goto done;
4872 if (tagtype == SMB_ACL_USER_OBJ) {
4873 if (sys_acl_set_permset(user_ent, permset) == -1) {
4874 DEBUG(5,("remove_posix_acl: failed to set permset from ACL on file %s (%s).\n",
4875 fname, strerror(errno) ));
4877 } else if (tagtype == SMB_ACL_GROUP_OBJ) {
4878 if (sys_acl_set_permset(group_ent, permset) == -1) {
4879 DEBUG(5,("remove_posix_acl: failed to set permset from ACL on file %s (%s).\n",
4880 fname, strerror(errno) ));
4882 } else if (tagtype == SMB_ACL_OTHER) {
4883 if (sys_acl_set_permset(other_ent, permset) == -1) {
4884 DEBUG(5,("remove_posix_acl: failed to set permset from ACL on file %s (%s).\n",
4885 fname, strerror(errno) ));
4890 /* Set the new empty file ACL. */
4891 if (fsp && fsp->fh->fd != -1) {
4892 if (SMB_VFS_SYS_ACL_SET_FD(fsp, new_file_acl) == -1) {
4893 DEBUG(5,("remove_posix_acl: acl_set_file failed on %s (%s)\n",
4894 fname, strerror(errno) ));
4895 goto done;
4897 } else {
4898 if (SMB_VFS_SYS_ACL_SET_FILE(conn, fname, SMB_ACL_TYPE_ACCESS, new_file_acl) == -1) {
4899 DEBUG(5,("remove_posix_acl: acl_set_file failed on %s (%s)\n",
4900 fname, strerror(errno) ));
4901 goto done;
4905 ret = True;
4907 done:
4909 if (file_acl) {
4910 TALLOC_FREE(file_acl);
4912 if (new_file_acl) {
4913 TALLOC_FREE(new_file_acl);
4915 return ret;
4918 /****************************************************************************
4919 Calls from UNIX extensions - POSIX ACL set.
4920 If num_def_acls == 0 then read/modify/write acl after removing all entries
4921 except SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER.
4922 ****************************************************************************/
4924 bool set_unix_posix_acl(connection_struct *conn, files_struct *fsp, const char *fname, uint16 num_acls, const char *pdata)
4926 SMB_ACL_T file_acl = NULL;
4928 if (!num_acls) {
4929 /* Remove the ACL from the file. */
4930 return remove_posix_acl(conn, fsp, fname);
4933 if ((file_acl = create_posix_acl_from_wire(conn, num_acls,
4934 pdata,
4935 talloc_tos())) == NULL) {
4936 return False;
4939 if (fsp && fsp->fh->fd != -1) {
4940 /* The preferred way - use an open fd. */
4941 if (SMB_VFS_SYS_ACL_SET_FD(fsp, file_acl) == -1) {
4942 DEBUG(5,("set_unix_posix_acl: acl_set_file failed on %s (%s)\n",
4943 fname, strerror(errno) ));
4944 TALLOC_FREE(file_acl);
4945 return False;
4947 } else {
4948 if (SMB_VFS_SYS_ACL_SET_FILE(conn, fname, SMB_ACL_TYPE_ACCESS, file_acl) == -1) {
4949 DEBUG(5,("set_unix_posix_acl: acl_set_file failed on %s (%s)\n",
4950 fname, strerror(errno) ));
4951 TALLOC_FREE(file_acl);
4952 return False;
4956 DEBUG(10,("set_unix_posix_acl: set acl for file %s\n", fname ));
4957 TALLOC_FREE(file_acl);
4958 return True;
4961 /********************************************************************
4962 Pull the NT ACL from a file on disk or the OpenEventlog() access
4963 check. Caller is responsible for freeing the returned security
4964 descriptor via TALLOC_FREE(). This is designed for dealing with
4965 user space access checks in smbd outside of the VFS. For example,
4966 checking access rights in OpenEventlog().
4968 Assume we are dealing with files (for now)
4969 ********************************************************************/
4971 struct security_descriptor *get_nt_acl_no_snum( TALLOC_CTX *ctx, const char *fname, uint32 security_info_wanted)
4973 struct security_descriptor *ret_sd;
4974 connection_struct *conn;
4975 files_struct finfo;
4976 struct fd_handle fh;
4977 NTSTATUS status;
4978 TALLOC_CTX *frame = talloc_stackframe();
4980 conn = talloc_zero(frame, connection_struct);
4981 if (conn == NULL) {
4982 DEBUG(0, ("talloc failed\n"));
4983 return NULL;
4986 if (!(conn->params = talloc(conn, struct share_params))) {
4987 DEBUG(0,("get_nt_acl_no_snum: talloc() failed!\n"));
4988 TALLOC_FREE(frame);
4989 return NULL;
4992 conn->params->service = -1;
4994 set_conn_connectpath(conn, "/");
4996 if (!smbd_vfs_init(conn)) {
4997 DEBUG(0,("get_nt_acl_no_snum: Unable to create a fake connection struct!\n"));
4998 conn_free(conn);
4999 TALLOC_FREE(frame);
5000 return NULL;
5003 ZERO_STRUCT( finfo );
5004 ZERO_STRUCT( fh );
5006 finfo.fnum = FNUM_FIELD_INVALID;
5007 finfo.conn = conn;
5008 finfo.fh = &fh;
5009 finfo.fh->fd = -1;
5011 status = create_synthetic_smb_fname(frame, fname, NULL, NULL,
5012 &finfo.fsp_name);
5013 if (!NT_STATUS_IS_OK(status)) {
5014 conn_free(conn);
5015 TALLOC_FREE(frame);
5016 return NULL;
5019 if (!NT_STATUS_IS_OK(SMB_VFS_FGET_NT_ACL( &finfo,
5020 security_info_wanted,
5021 ctx, &ret_sd))) {
5022 DEBUG(0,("get_nt_acl_no_snum: get_nt_acl returned zero.\n"));
5023 TALLOC_FREE(finfo.fsp_name);
5024 conn_free(conn);
5025 TALLOC_FREE(frame);
5026 return NULL;
5029 TALLOC_FREE(finfo.fsp_name);
5030 conn_free(conn);
5031 TALLOC_FREE(frame);
5033 return ret_sd;
5036 /* Stolen shamelessly from pvfs_default_acl() in source4 :-). */
5038 NTSTATUS make_default_filesystem_acl(TALLOC_CTX *ctx,
5039 const char *name,
5040 SMB_STRUCT_STAT *psbuf,
5041 struct security_descriptor **ppdesc)
5043 struct dom_sid owner_sid, group_sid;
5044 size_t size = 0;
5045 struct security_ace aces[4];
5046 uint32_t access_mask = 0;
5047 mode_t mode = psbuf->st_ex_mode;
5048 struct security_acl *new_dacl = NULL;
5049 int idx = 0;
5051 DEBUG(10,("make_default_filesystem_acl: file %s mode = 0%o\n",
5052 name, (int)mode ));
5054 uid_to_sid(&owner_sid, psbuf->st_ex_uid);
5055 gid_to_sid(&group_sid, psbuf->st_ex_gid);
5058 We provide up to 4 ACEs
5059 - Owner
5060 - Group
5061 - Everyone
5062 - NT System
5065 if (mode & S_IRUSR) {
5066 if (mode & S_IWUSR) {
5067 access_mask |= SEC_RIGHTS_FILE_ALL;
5068 } else {
5069 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
5072 if (mode & S_IWUSR) {
5073 access_mask |= SEC_RIGHTS_FILE_WRITE | SEC_STD_DELETE;
5076 init_sec_ace(&aces[idx],
5077 &owner_sid,
5078 SEC_ACE_TYPE_ACCESS_ALLOWED,
5079 access_mask,
5081 idx++;
5083 access_mask = 0;
5084 if (mode & S_IRGRP) {
5085 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
5087 if (mode & S_IWGRP) {
5088 /* note that delete is not granted - this matches posix behaviour */
5089 access_mask |= SEC_RIGHTS_FILE_WRITE;
5091 if (access_mask) {
5092 init_sec_ace(&aces[idx],
5093 &group_sid,
5094 SEC_ACE_TYPE_ACCESS_ALLOWED,
5095 access_mask,
5097 idx++;
5100 access_mask = 0;
5101 if (mode & S_IROTH) {
5102 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
5104 if (mode & S_IWOTH) {
5105 access_mask |= SEC_RIGHTS_FILE_WRITE;
5107 if (access_mask) {
5108 init_sec_ace(&aces[idx],
5109 &global_sid_World,
5110 SEC_ACE_TYPE_ACCESS_ALLOWED,
5111 access_mask,
5113 idx++;
5116 init_sec_ace(&aces[idx],
5117 &global_sid_System,
5118 SEC_ACE_TYPE_ACCESS_ALLOWED,
5119 SEC_RIGHTS_FILE_ALL,
5121 idx++;
5123 new_dacl = make_sec_acl(ctx,
5124 NT4_ACL_REVISION,
5125 idx,
5126 aces);
5128 if (!new_dacl) {
5129 return NT_STATUS_NO_MEMORY;
5132 *ppdesc = make_sec_desc(ctx,
5133 SECURITY_DESCRIPTOR_REVISION_1,
5134 SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
5135 &owner_sid,
5136 &group_sid,
5137 NULL,
5138 new_dacl,
5139 &size);
5140 if (!*ppdesc) {
5141 return NT_STATUS_NO_MEMORY;
5143 return NT_STATUS_OK;
5146 int posix_sys_acl_blob_get_file(vfs_handle_struct *handle,
5147 const char *path_p,
5148 TALLOC_CTX *mem_ctx,
5149 char **blob_description,
5150 DATA_BLOB *blob)
5152 int ret;
5153 TALLOC_CTX *frame = talloc_stackframe();
5154 struct smb_acl_wrapper acl_wrapper = {};
5155 struct smb_filename *smb_fname = NULL;
5156 NTSTATUS status = create_synthetic_smb_fname_split(frame, path_p,
5157 NULL,
5158 &smb_fname);
5159 if (!NT_STATUS_IS_OK(status)) {
5160 errno = map_errno_from_nt_status(status);
5161 TALLOC_FREE(frame);
5162 return -1;
5165 acl_wrapper.access_acl
5166 = smb_vfs_call_sys_acl_get_file(handle,
5167 path_p,
5168 SMB_ACL_TYPE_ACCESS,
5169 frame);
5171 ret = smb_vfs_call_stat(handle, smb_fname);
5172 if (ret == -1) {
5173 TALLOC_FREE(frame);
5174 return -1;
5177 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
5178 acl_wrapper.default_acl
5179 = smb_vfs_call_sys_acl_get_file(handle,
5180 path_p,
5181 SMB_ACL_TYPE_DEFAULT,
5182 frame);
5185 acl_wrapper.owner = smb_fname->st.st_ex_uid;
5186 acl_wrapper.group = smb_fname->st.st_ex_gid;
5187 acl_wrapper.mode = smb_fname->st.st_ex_mode;
5189 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(blob, mem_ctx,
5190 &acl_wrapper,
5191 (ndr_push_flags_fn_t)ndr_push_smb_acl_wrapper))) {
5192 errno = EINVAL;
5193 TALLOC_FREE(frame);
5194 return -1;
5197 *blob_description = talloc_strdup(mem_ctx, "posix_acl");
5198 if (!*blob_description) {
5199 errno = EINVAL;
5200 TALLOC_FREE(frame);
5201 return -1;
5204 TALLOC_FREE(frame);
5205 return 0;
5208 int posix_sys_acl_blob_get_fd(vfs_handle_struct *handle,
5209 files_struct *fsp,
5210 TALLOC_CTX *mem_ctx,
5211 char **blob_description,
5212 DATA_BLOB *blob)
5214 SMB_STRUCT_STAT sbuf;
5215 TALLOC_CTX *frame;
5216 struct smb_acl_wrapper acl_wrapper;
5217 int ret;
5219 /* This ensures that we also consider the default ACL */
5220 if (fsp->is_directory || fsp->fh->fd == -1) {
5221 return posix_sys_acl_blob_get_file(handle, fsp->fsp_name->base_name,
5222 mem_ctx, blob_description, blob);
5224 frame = talloc_stackframe();
5226 acl_wrapper.default_acl = NULL;
5228 acl_wrapper.access_acl = smb_vfs_call_sys_acl_get_file(handle, fsp->fsp_name->base_name,
5229 SMB_ACL_TYPE_ACCESS, frame);
5231 ret = smb_vfs_call_fstat(handle, fsp, &sbuf);
5232 if (ret == -1) {
5233 TALLOC_FREE(frame);
5234 return -1;
5237 acl_wrapper.owner = sbuf.st_ex_uid;
5238 acl_wrapper.group = sbuf.st_ex_gid;
5239 acl_wrapper.mode = sbuf.st_ex_mode;
5241 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(blob, mem_ctx,
5242 &acl_wrapper,
5243 (ndr_push_flags_fn_t)ndr_push_smb_acl_wrapper))) {
5244 errno = EINVAL;
5245 TALLOC_FREE(frame);
5246 return -1;
5249 *blob_description = talloc_strdup(mem_ctx, "posix_acl");
5250 if (!*blob_description) {
5251 errno = EINVAL;
5252 TALLOC_FREE(frame);
5253 return -1;
5256 TALLOC_FREE(frame);
5257 return 0;