s3: VFS: Protect errno if sys_getwd() fails across free() call.
[Samba.git] / source3 / smbd / posix_acls.c
blob26ef113874ed19ae090371ae8f91eeee21fb5dee
1 /*
2 Unix SMB/CIFS implementation.
3 SMB NT Security Descriptor / Unix permission conversion.
4 Copyright (C) Jeremy Allison 1994-2009.
5 Copyright (C) Andreas Gruenbacher 2002.
6 Copyright (C) Simo Sorce <idra@samba.org> 2009.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "smbd/smbd.h"
24 #include "system/filesys.h"
25 #include "../libcli/security/security.h"
26 #include "trans2.h"
27 #include "passdb/lookup_sid.h"
28 #include "auth.h"
29 #include "../librpc/gen_ndr/idmap.h"
30 #include "../librpc/gen_ndr/ndr_smb_acl.h"
31 #include "lib/param/loadparm.h"
33 extern const struct generic_mapping file_generic_mapping;
35 #undef DBGC_CLASS
36 #define DBGC_CLASS DBGC_ACLS
38 /****************************************************************************
39 Data structures representing the internal ACE format.
40 ****************************************************************************/
42 enum ace_owner {UID_ACE, GID_ACE, WORLD_ACE};
43 enum ace_attribute {ALLOW_ACE, DENY_ACE}; /* Used for incoming NT ACLS. */
45 typedef struct canon_ace {
46 struct canon_ace *next, *prev;
47 SMB_ACL_TAG_T type;
48 mode_t perms; /* Only use S_I(R|W|X)USR mode bits here. */
49 struct dom_sid trustee;
50 enum ace_owner owner_type;
51 enum ace_attribute attr;
52 struct unixid unix_ug;
53 uint8_t ace_flags; /* From windows ACE entry. */
54 } canon_ace;
56 #define ALL_ACE_PERMS (S_IRUSR|S_IWUSR|S_IXUSR)
59 * EA format of user.SAMBA_PAI (Samba_Posix_Acl_Interitance)
60 * attribute on disk - version 1.
61 * All values are little endian.
63 * | 1 | 1 | 2 | 2 | ....
64 * +------+------+-------------+---------------------+-------------+--------------------+
65 * | vers | flag | num_entries | num_default_entries | ..entries.. | default_entries... |
66 * +------+------+-------------+---------------------+-------------+--------------------+
68 * Entry format is :
70 * | 1 | 4 |
71 * +------+-------------------+
72 * | value| uid/gid or world |
73 * | type | value |
74 * +------+-------------------+
76 * Version 2 format. Stores extra Windows metadata about an ACL.
78 * | 1 | 2 | 2 | 2 | ....
79 * +------+----------+-------------+---------------------+-------------+--------------------+
80 * | vers | ace | num_entries | num_default_entries | ..entries.. | default_entries... |
81 * | 2 | type | | | | |
82 * +------+----------+-------------+---------------------+-------------+--------------------+
84 * Entry format is :
86 * | 1 | 1 | 4 |
87 * +------+------+-------------------+
88 * | ace | value| uid/gid or world |
89 * | flag | type | value |
90 * +------+-------------------+------+
94 #define PAI_VERSION_OFFSET 0
96 #define PAI_V1_FLAG_OFFSET 1
97 #define PAI_V1_NUM_ENTRIES_OFFSET 2
98 #define PAI_V1_NUM_DEFAULT_ENTRIES_OFFSET 4
99 #define PAI_V1_ENTRIES_BASE 6
100 #define PAI_V1_ACL_FLAG_PROTECTED 0x1
101 #define PAI_V1_ENTRY_LENGTH 5
103 #define PAI_V1_VERSION 1
105 #define PAI_V2_TYPE_OFFSET 1
106 #define PAI_V2_NUM_ENTRIES_OFFSET 3
107 #define PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET 5
108 #define PAI_V2_ENTRIES_BASE 7
109 #define PAI_V2_ENTRY_LENGTH 6
111 #define PAI_V2_VERSION 2
114 * In memory format of user.SAMBA_PAI attribute.
117 struct pai_entry {
118 struct pai_entry *next, *prev;
119 uint8_t ace_flags;
120 enum ace_owner owner_type;
121 struct unixid unix_ug;
124 struct pai_val {
125 uint16_t sd_type;
126 unsigned int num_entries;
127 struct pai_entry *entry_list;
128 unsigned int num_def_entries;
129 struct pai_entry *def_entry_list;
132 /************************************************************************
133 Return a uint32_t of the pai_entry principal.
134 ************************************************************************/
136 static uint32_t get_pai_entry_val(struct pai_entry *paie)
138 switch (paie->owner_type) {
139 case UID_ACE:
140 DEBUG(10,("get_pai_entry_val: uid = %u\n", (unsigned int)paie->unix_ug.id ));
141 return (uint32_t)paie->unix_ug.id;
142 case GID_ACE:
143 DEBUG(10,("get_pai_entry_val: gid = %u\n", (unsigned int)paie->unix_ug.id ));
144 return (uint32_t)paie->unix_ug.id;
145 case WORLD_ACE:
146 default:
147 DEBUG(10,("get_pai_entry_val: world ace\n"));
148 return (uint32_t)-1;
152 /************************************************************************
153 Return a uint32_t of the entry principal.
154 ************************************************************************/
156 static uint32_t get_entry_val(canon_ace *ace_entry)
158 switch (ace_entry->owner_type) {
159 case UID_ACE:
160 DEBUG(10,("get_entry_val: uid = %u\n", (unsigned int)ace_entry->unix_ug.id ));
161 return (uint32_t)ace_entry->unix_ug.id;
162 case GID_ACE:
163 DEBUG(10,("get_entry_val: gid = %u\n", (unsigned int)ace_entry->unix_ug.id ));
164 return (uint32_t)ace_entry->unix_ug.id;
165 case WORLD_ACE:
166 default:
167 DEBUG(10,("get_entry_val: world ace\n"));
168 return (uint32_t)-1;
172 /************************************************************************
173 Create the on-disk format (always v2 now). Caller must free.
174 ************************************************************************/
176 static char *create_pai_buf_v2(canon_ace *file_ace_list,
177 canon_ace *dir_ace_list,
178 uint16_t sd_type,
179 size_t *store_size)
181 char *pai_buf = NULL;
182 canon_ace *ace_list = NULL;
183 char *entry_offset = NULL;
184 unsigned int num_entries = 0;
185 unsigned int num_def_entries = 0;
186 unsigned int i;
188 for (ace_list = file_ace_list; ace_list; ace_list = ace_list->next) {
189 num_entries++;
192 for (ace_list = dir_ace_list; ace_list; ace_list = ace_list->next) {
193 num_def_entries++;
196 DEBUG(10,("create_pai_buf_v2: num_entries = %u, num_def_entries = %u\n", num_entries, num_def_entries ));
198 *store_size = PAI_V2_ENTRIES_BASE +
199 ((num_entries + num_def_entries)*PAI_V2_ENTRY_LENGTH);
201 pai_buf = talloc_array(talloc_tos(), char, *store_size);
202 if (!pai_buf) {
203 return NULL;
206 /* Set up the header. */
207 memset(pai_buf, '\0', PAI_V2_ENTRIES_BASE);
208 SCVAL(pai_buf,PAI_VERSION_OFFSET,PAI_V2_VERSION);
209 SSVAL(pai_buf,PAI_V2_TYPE_OFFSET, sd_type);
210 SSVAL(pai_buf,PAI_V2_NUM_ENTRIES_OFFSET,num_entries);
211 SSVAL(pai_buf,PAI_V2_NUM_DEFAULT_ENTRIES_OFFSET,num_def_entries);
213 DEBUG(10,("create_pai_buf_v2: sd_type = 0x%x\n",
214 (unsigned int)sd_type ));
216 entry_offset = pai_buf + PAI_V2_ENTRIES_BASE;
218 i = 0;
219 for (ace_list = file_ace_list; ace_list; ace_list = ace_list->next) {
220 uint8_t type_val = (uint8_t)ace_list->owner_type;
221 uint32_t entry_val = get_entry_val(ace_list);
223 SCVAL(entry_offset,0,ace_list->ace_flags);
224 SCVAL(entry_offset,1,type_val);
225 SIVAL(entry_offset,2,entry_val);
226 DEBUG(10,("create_pai_buf_v2: entry %u [0x%x] [0x%x] [0x%x]\n",
228 (unsigned int)ace_list->ace_flags,
229 (unsigned int)type_val,
230 (unsigned int)entry_val ));
231 i++;
232 entry_offset += PAI_V2_ENTRY_LENGTH;
235 for (ace_list = dir_ace_list; ace_list; ace_list = ace_list->next) {
236 uint8_t type_val = (uint8_t)ace_list->owner_type;
237 uint32_t entry_val = get_entry_val(ace_list);
239 SCVAL(entry_offset,0,ace_list->ace_flags);
240 SCVAL(entry_offset,1,type_val);
241 SIVAL(entry_offset,2,entry_val);
242 DEBUG(10,("create_pai_buf_v2: entry %u [0x%x] [0x%x] [0x%x]\n",
244 (unsigned int)ace_list->ace_flags,
245 (unsigned int)type_val,
246 (unsigned int)entry_val ));
247 i++;
248 entry_offset += PAI_V2_ENTRY_LENGTH;
251 return pai_buf;
254 /************************************************************************
255 Store the user.SAMBA_PAI attribute on disk.
256 ************************************************************************/
258 static void store_inheritance_attributes(files_struct *fsp,
259 canon_ace *file_ace_list,
260 canon_ace *dir_ace_list,
261 uint16_t sd_type)
263 int ret;
264 size_t store_size;
265 char *pai_buf;
267 if (!lp_map_acl_inherit(SNUM(fsp->conn))) {
268 return;
271 pai_buf = create_pai_buf_v2(file_ace_list, dir_ace_list,
272 sd_type, &store_size);
274 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_t num_entries;
342 uint16_t 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_t num_entries;
374 uint16_t 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 dbgtext( "uid %u ", (unsigned int)pace->unix_ug.id);
806 } else if (pace->owner_type == GID_ACE) {
807 dbgtext( "gid %u ", (unsigned int)pace->unix_ug.id);
808 } else
809 dbgtext( "other ");
810 switch (pace->type) {
811 case SMB_ACL_USER:
812 dbgtext( "SMB_ACL_USER ");
813 break;
814 case SMB_ACL_USER_OBJ:
815 dbgtext( "SMB_ACL_USER_OBJ ");
816 break;
817 case SMB_ACL_GROUP:
818 dbgtext( "SMB_ACL_GROUP ");
819 break;
820 case SMB_ACL_GROUP_OBJ:
821 dbgtext( "SMB_ACL_GROUP_OBJ ");
822 break;
823 case SMB_ACL_OTHER:
824 dbgtext( "SMB_ACL_OTHER ");
825 break;
826 default:
827 dbgtext( "MASK " );
828 break;
831 dbgtext( "ace_flags = 0x%x ", (unsigned int)pace->ace_flags);
832 dbgtext( "perms ");
833 dbgtext( "%c", pace->perms & S_IRUSR ? 'r' : '-');
834 dbgtext( "%c", pace->perms & S_IWUSR ? 'w' : '-');
835 dbgtext( "%c\n", pace->perms & S_IXUSR ? 'x' : '-');
838 /****************************************************************************
839 Print out a canon ace list.
840 ****************************************************************************/
842 static void print_canon_ace_list(const char *name, canon_ace *ace_list)
844 int count = 0;
846 if( DEBUGLVL( 10 )) {
847 dbgtext( "print_canon_ace_list: %s\n", name );
848 for (;ace_list; ace_list = ace_list->next, count++)
849 print_canon_ace(ace_list, count );
853 /****************************************************************************
854 Map POSIX ACL perms to canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits).
855 ****************************************************************************/
857 static mode_t convert_permset_to_mode_t(SMB_ACL_PERMSET_T permset)
859 mode_t ret = 0;
861 ret |= (sys_acl_get_perm(permset, SMB_ACL_READ) ? S_IRUSR : 0);
862 ret |= (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? S_IWUSR : 0);
863 ret |= (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? S_IXUSR : 0);
865 return ret;
868 /****************************************************************************
869 Map generic UNIX permissions to canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits).
870 ****************************************************************************/
872 static mode_t unix_perms_to_acl_perms(mode_t mode, int r_mask, int w_mask, int x_mask)
874 mode_t ret = 0;
876 if (mode & r_mask)
877 ret |= S_IRUSR;
878 if (mode & w_mask)
879 ret |= S_IWUSR;
880 if (mode & x_mask)
881 ret |= S_IXUSR;
883 return ret;
886 /****************************************************************************
887 Map canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits) to
888 an SMB_ACL_PERMSET_T.
889 ****************************************************************************/
891 static int map_acl_perms_to_permset(connection_struct *conn, mode_t mode, SMB_ACL_PERMSET_T *p_permset)
893 if (sys_acl_clear_perms(*p_permset) == -1)
894 return -1;
895 if (mode & S_IRUSR) {
896 if (sys_acl_add_perm(*p_permset, SMB_ACL_READ) == -1)
897 return -1;
899 if (mode & S_IWUSR) {
900 if (sys_acl_add_perm(*p_permset, SMB_ACL_WRITE) == -1)
901 return -1;
903 if (mode & S_IXUSR) {
904 if (sys_acl_add_perm(*p_permset, SMB_ACL_EXECUTE) == -1)
905 return -1;
907 return 0;
910 /****************************************************************************
911 Function to create owner and group SIDs from a SMB_STRUCT_STAT.
912 ****************************************************************************/
914 void create_file_sids(const SMB_STRUCT_STAT *psbuf, struct dom_sid *powner_sid, struct dom_sid *pgroup_sid)
916 uid_to_sid( powner_sid, psbuf->st_ex_uid );
917 gid_to_sid( pgroup_sid, psbuf->st_ex_gid );
920 /****************************************************************************
921 Merge aces with a common UID or GID - if both are allow or deny, OR the permissions together and
922 delete the second one. If the first is deny, mask the permissions off and delete the allow
923 if the permissions become zero, delete the deny if the permissions are non zero.
924 ****************************************************************************/
926 static void merge_aces( canon_ace **pp_list_head, bool dir_acl)
928 canon_ace *l_head = *pp_list_head;
929 canon_ace *curr_ace_outer;
930 canon_ace *curr_ace_outer_next;
933 * First, merge allow entries with identical SIDs, and deny entries
934 * with identical SIDs.
937 for (curr_ace_outer = l_head; curr_ace_outer; curr_ace_outer = curr_ace_outer_next) {
938 canon_ace *curr_ace;
939 canon_ace *curr_ace_next;
941 curr_ace_outer_next = curr_ace_outer->next; /* Save the link in case we delete. */
943 for (curr_ace = curr_ace_outer->next; curr_ace; curr_ace = curr_ace_next) {
944 bool can_merge = false;
946 curr_ace_next = curr_ace->next; /* Save the link in case of delete. */
948 /* For file ACLs we can merge if the SIDs and ALLOW/DENY
949 * types are the same. For directory acls we must also
950 * ensure the POSIX ACL types are the same.
952 * For the IDMAP_BOTH case, we must not merge
953 * the UID and GID ACE values for same SID
956 if (!dir_acl) {
957 can_merge = (curr_ace->unix_ug.id == curr_ace_outer->unix_ug.id &&
958 curr_ace->owner_type == curr_ace_outer->owner_type &&
959 (curr_ace->attr == curr_ace_outer->attr));
960 } else {
961 can_merge = (curr_ace->unix_ug.id == curr_ace_outer->unix_ug.id &&
962 curr_ace->owner_type == curr_ace_outer->owner_type &&
963 (curr_ace->type == curr_ace_outer->type) &&
964 (curr_ace->attr == curr_ace_outer->attr));
967 if (can_merge) {
968 if( DEBUGLVL( 10 )) {
969 dbgtext("merge_aces: Merging ACE's\n");
970 print_canon_ace( curr_ace_outer, 0);
971 print_canon_ace( curr_ace, 0);
974 /* Merge two allow or two deny ACE's. */
976 /* Theoretically we shouldn't merge a dir ACE if
977 * one ACE has the CI flag set, and the other
978 * ACE has the OI flag set, but this is rare
979 * enough we can ignore it. */
981 curr_ace_outer->perms |= curr_ace->perms;
982 curr_ace_outer->ace_flags |= curr_ace->ace_flags;
983 DLIST_REMOVE(l_head, curr_ace);
984 TALLOC_FREE(curr_ace);
985 curr_ace_outer_next = curr_ace_outer->next; /* We may have deleted the link. */
991 * Now go through and mask off allow permissions with deny permissions.
992 * We can delete either the allow or deny here as we know that each SID
993 * appears only once in the list.
996 for (curr_ace_outer = l_head; curr_ace_outer; curr_ace_outer = curr_ace_outer_next) {
997 canon_ace *curr_ace;
998 canon_ace *curr_ace_next;
1000 curr_ace_outer_next = curr_ace_outer->next; /* Save the link in case we delete. */
1002 for (curr_ace = curr_ace_outer->next; curr_ace; curr_ace = curr_ace_next) {
1004 curr_ace_next = curr_ace->next; /* Save the link in case of delete. */
1007 * Subtract ACE's with different entries. Due to the ordering constraints
1008 * we've put on the ACL, we know the deny must be the first one.
1011 if (curr_ace->unix_ug.id == curr_ace_outer->unix_ug.id &&
1012 (curr_ace->owner_type == curr_ace_outer->owner_type) &&
1013 (curr_ace_outer->attr == DENY_ACE) && (curr_ace->attr == ALLOW_ACE)) {
1015 if( DEBUGLVL( 10 )) {
1016 dbgtext("merge_aces: Masking ACE's\n");
1017 print_canon_ace( curr_ace_outer, 0);
1018 print_canon_ace( curr_ace, 0);
1021 curr_ace->perms &= ~curr_ace_outer->perms;
1023 if (curr_ace->perms == 0) {
1026 * The deny overrides the allow. Remove the allow.
1029 DLIST_REMOVE(l_head, curr_ace);
1030 TALLOC_FREE(curr_ace);
1031 curr_ace_outer_next = curr_ace_outer->next; /* We may have deleted the link. */
1033 } else {
1036 * Even after removing permissions, there
1037 * are still allow permissions - delete the deny.
1038 * It is safe to delete the deny here,
1039 * as we are guarenteed by the deny first
1040 * ordering that all the deny entries for
1041 * this SID have already been merged into one
1042 * before we can get to an allow ace.
1045 DLIST_REMOVE(l_head, curr_ace_outer);
1046 TALLOC_FREE(curr_ace_outer);
1047 break;
1051 } /* end for curr_ace */
1052 } /* end for curr_ace_outer */
1054 /* We may have modified the list. */
1056 *pp_list_head = l_head;
1059 /****************************************************************************
1060 Map canon_ace perms to permission bits NT.
1061 The attr element is not used here - we only process deny entries on set,
1062 not get. Deny entries are implicit on get with ace->perms = 0.
1063 ****************************************************************************/
1065 uint32_t map_canon_ace_perms(int snum,
1066 enum security_ace_type *pacl_type,
1067 mode_t perms,
1068 bool directory_ace)
1070 uint32_t nt_mask = 0;
1072 *pacl_type = SEC_ACE_TYPE_ACCESS_ALLOWED;
1074 if (lp_acl_map_full_control(snum) && ((perms & ALL_ACE_PERMS) == ALL_ACE_PERMS)) {
1075 if (directory_ace) {
1076 nt_mask = UNIX_DIRECTORY_ACCESS_RWX;
1077 } else {
1078 nt_mask = (UNIX_ACCESS_RWX & ~DELETE_ACCESS);
1080 } else if ((perms & ALL_ACE_PERMS) == (mode_t)0) {
1082 * Windows NT refuses to display ACEs with no permissions in them (but
1083 * they are perfectly legal with Windows 2000). If the ACE has empty
1084 * permissions we cannot use 0, so we use the otherwise unused
1085 * WRITE_OWNER permission, which we ignore when we set an ACL.
1086 * We abstract this into a #define of UNIX_ACCESS_NONE to allow this
1087 * to be changed in the future.
1090 nt_mask = 0;
1091 } else {
1092 if (directory_ace) {
1093 nt_mask |= ((perms & S_IRUSR) ? UNIX_DIRECTORY_ACCESS_R : 0 );
1094 nt_mask |= ((perms & S_IWUSR) ? UNIX_DIRECTORY_ACCESS_W : 0 );
1095 nt_mask |= ((perms & S_IXUSR) ? UNIX_DIRECTORY_ACCESS_X : 0 );
1096 } else {
1097 nt_mask |= ((perms & S_IRUSR) ? UNIX_ACCESS_R : 0 );
1098 nt_mask |= ((perms & S_IWUSR) ? UNIX_ACCESS_W : 0 );
1099 nt_mask |= ((perms & S_IXUSR) ? UNIX_ACCESS_X : 0 );
1103 if ((perms & S_IWUSR) && lp_dos_filemode(snum)) {
1104 nt_mask |= (SEC_STD_WRITE_DAC|SEC_STD_WRITE_OWNER|DELETE_ACCESS);
1107 DEBUG(10,("map_canon_ace_perms: Mapped (UNIX) %x to (NT) %x\n",
1108 (unsigned int)perms, (unsigned int)nt_mask ));
1110 return nt_mask;
1113 /****************************************************************************
1114 Map NT perms to a UNIX mode_t.
1115 ****************************************************************************/
1117 #define FILE_SPECIFIC_READ_BITS (FILE_READ_DATA|FILE_READ_EA)
1118 #define FILE_SPECIFIC_WRITE_BITS (FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_WRITE_EA)
1119 #define FILE_SPECIFIC_EXECUTE_BITS (FILE_EXECUTE)
1121 static mode_t map_nt_perms( uint32_t *mask, int type)
1123 mode_t mode = 0;
1125 switch(type) {
1126 case S_IRUSR:
1127 if((*mask) & GENERIC_ALL_ACCESS)
1128 mode = S_IRUSR|S_IWUSR|S_IXUSR;
1129 else {
1130 mode |= ((*mask) & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IRUSR : 0;
1131 mode |= ((*mask) & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWUSR : 0;
1132 mode |= ((*mask) & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXUSR : 0;
1134 break;
1135 case S_IRGRP:
1136 if((*mask) & GENERIC_ALL_ACCESS)
1137 mode = S_IRGRP|S_IWGRP|S_IXGRP;
1138 else {
1139 mode |= ((*mask) & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IRGRP : 0;
1140 mode |= ((*mask) & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWGRP : 0;
1141 mode |= ((*mask) & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXGRP : 0;
1143 break;
1144 case S_IROTH:
1145 if((*mask) & GENERIC_ALL_ACCESS)
1146 mode = S_IROTH|S_IWOTH|S_IXOTH;
1147 else {
1148 mode |= ((*mask) & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IROTH : 0;
1149 mode |= ((*mask) & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWOTH : 0;
1150 mode |= ((*mask) & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXOTH : 0;
1152 break;
1155 return mode;
1158 /****************************************************************************
1159 Unpack a struct security_descriptor into a UNIX owner and group.
1160 ****************************************************************************/
1162 NTSTATUS unpack_nt_owners(struct connection_struct *conn,
1163 uid_t *puser, gid_t *pgrp,
1164 uint32_t security_info_sent, const struct
1165 security_descriptor *psd)
1167 *puser = (uid_t)-1;
1168 *pgrp = (gid_t)-1;
1170 if(security_info_sent == 0) {
1171 DEBUG(0,("unpack_nt_owners: no security info sent !\n"));
1172 return NT_STATUS_OK;
1176 * Validate the owner and group SID's.
1179 DEBUG(5,("unpack_nt_owners: validating owner_sids.\n"));
1182 * Don't immediately fail if the owner sid cannot be validated.
1183 * This may be a group chown only set.
1186 if (security_info_sent & SECINFO_OWNER) {
1187 if (!sid_to_uid(psd->owner_sid, puser)) {
1188 if (lp_force_unknown_acl_user(SNUM(conn))) {
1189 /* this allows take ownership to work
1190 * reasonably */
1191 *puser = get_current_uid(conn);
1192 } else {
1193 DEBUG(3,("unpack_nt_owners: unable to validate"
1194 " owner sid for %s\n",
1195 sid_string_dbg(psd->owner_sid)));
1196 return NT_STATUS_INVALID_OWNER;
1199 DEBUG(3,("unpack_nt_owners: owner sid mapped to uid %u\n",
1200 (unsigned int)*puser ));
1204 * Don't immediately fail if the group sid cannot be validated.
1205 * This may be an owner chown only set.
1208 if (security_info_sent & SECINFO_GROUP) {
1209 if (!sid_to_gid(psd->group_sid, pgrp)) {
1210 if (lp_force_unknown_acl_user(SNUM(conn))) {
1211 /* this allows take group ownership to work
1212 * reasonably */
1213 *pgrp = get_current_gid(conn);
1214 } else {
1215 DEBUG(3,("unpack_nt_owners: unable to validate"
1216 " group sid.\n"));
1217 return NT_STATUS_INVALID_OWNER;
1220 DEBUG(3,("unpack_nt_owners: group sid mapped to gid %u\n",
1221 (unsigned int)*pgrp));
1224 DEBUG(5,("unpack_nt_owners: owner_sids validated.\n"));
1226 return NT_STATUS_OK;
1230 static void trim_ace_perms(canon_ace *pace)
1232 pace->perms = pace->perms & (S_IXUSR|S_IWUSR|S_IRUSR);
1235 static void ensure_minimal_owner_ace_perms(const bool is_directory,
1236 canon_ace *pace)
1238 pace->perms |= S_IRUSR;
1239 if (is_directory) {
1240 pace->perms |= (S_IWUSR|S_IXUSR);
1244 /****************************************************************************
1245 Check if a given uid/SID is in a group gid/SID. This is probably very
1246 expensive and will need optimisation. A *lot* of optimisation :-). JRA.
1247 ****************************************************************************/
1249 static bool uid_entry_in_group(connection_struct *conn, canon_ace *uid_ace, canon_ace *group_ace )
1251 /* "Everyone" always matches every uid. */
1253 if (dom_sid_equal(&group_ace->trustee, &global_sid_World))
1254 return True;
1257 * if it's the current user, we already have the unix token
1258 * and don't need to do the complex user_in_group_sid() call
1260 if (uid_ace->unix_ug.id == get_current_uid(conn)) {
1261 const struct security_unix_token *curr_utok = NULL;
1262 size_t i;
1264 if (group_ace->unix_ug.id == get_current_gid(conn)) {
1265 return True;
1268 curr_utok = get_current_utok(conn);
1269 for (i=0; i < curr_utok->ngroups; i++) {
1270 if (group_ace->unix_ug.id == curr_utok->groups[i]) {
1271 return True;
1277 * user_in_group_sid() uses create_token_from_sid()
1278 * which creates an artificial NT token given just a username,
1279 * so this is not reliable for users from foreign domains
1280 * exported by winbindd!
1282 return user_sid_in_group_sid(&uid_ace->trustee, &group_ace->trustee);
1285 /****************************************************************************
1286 A well formed POSIX file or default ACL has at least 3 entries, a
1287 SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ.
1288 In addition, the owner must always have at least read access.
1289 When using this call on get_acl, the pst struct is valid and contains
1290 the mode of the file.
1291 ****************************************************************************/
1293 static bool ensure_canon_entry_valid_on_get(connection_struct *conn,
1294 canon_ace **pp_ace,
1295 const struct dom_sid *pfile_owner_sid,
1296 const struct dom_sid *pfile_grp_sid,
1297 const SMB_STRUCT_STAT *pst)
1299 canon_ace *pace;
1300 bool got_user = false;
1301 bool got_group = false;
1302 bool got_other = false;
1304 for (pace = *pp_ace; pace; pace = pace->next) {
1305 if (pace->type == SMB_ACL_USER_OBJ) {
1306 got_user = true;
1307 } else if (pace->type == SMB_ACL_GROUP_OBJ) {
1308 got_group = true;
1309 } else if (pace->type == SMB_ACL_OTHER) {
1310 got_other = true;
1314 if (!got_user) {
1315 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1316 DEBUG(0,("malloc fail.\n"));
1317 return false;
1320 ZERO_STRUCTP(pace);
1321 pace->type = SMB_ACL_USER_OBJ;
1322 pace->owner_type = UID_ACE;
1323 pace->unix_ug.type = ID_TYPE_UID;
1324 pace->unix_ug.id = pst->st_ex_uid;
1325 pace->trustee = *pfile_owner_sid;
1326 pace->attr = ALLOW_ACE;
1327 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IRUSR, S_IWUSR, S_IXUSR);
1328 DLIST_ADD(*pp_ace, pace);
1331 if (!got_group) {
1332 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1333 DEBUG(0,("malloc fail.\n"));
1334 return false;
1337 ZERO_STRUCTP(pace);
1338 pace->type = SMB_ACL_GROUP_OBJ;
1339 pace->owner_type = GID_ACE;
1340 pace->unix_ug.type = ID_TYPE_GID;
1341 pace->unix_ug.id = pst->st_ex_gid;
1342 pace->trustee = *pfile_grp_sid;
1343 pace->attr = ALLOW_ACE;
1344 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IRGRP, S_IWGRP, S_IXGRP);
1345 DLIST_ADD(*pp_ace, pace);
1348 if (!got_other) {
1349 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1350 DEBUG(0,("malloc fail.\n"));
1351 return false;
1354 ZERO_STRUCTP(pace);
1355 pace->type = SMB_ACL_OTHER;
1356 pace->owner_type = WORLD_ACE;
1357 pace->unix_ug.type = ID_TYPE_NOT_SPECIFIED;
1358 pace->unix_ug.id = -1;
1359 pace->trustee = global_sid_World;
1360 pace->attr = ALLOW_ACE;
1361 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IROTH, S_IWOTH, S_IXOTH);
1362 DLIST_ADD(*pp_ace, pace);
1365 return true;
1368 /****************************************************************************
1369 A well formed POSIX file or default ACL has at least 3 entries, a
1370 SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ.
1371 In addition, the owner must always have at least read access.
1372 When using this call on set_acl, the pst struct has
1373 been modified to have a mode containing the default for this file or directory
1374 type.
1375 ****************************************************************************/
1377 static bool ensure_canon_entry_valid_on_set(connection_struct *conn,
1378 canon_ace **pp_ace,
1379 bool is_default_acl,
1380 const struct share_params *params,
1381 const bool is_directory,
1382 const struct dom_sid *pfile_owner_sid,
1383 const struct dom_sid *pfile_grp_sid,
1384 const SMB_STRUCT_STAT *pst)
1386 canon_ace *pace;
1387 canon_ace *pace_user = NULL;
1388 canon_ace *pace_group = NULL;
1389 canon_ace *pace_other = NULL;
1390 bool got_duplicate_user = false;
1391 bool got_duplicate_group = false;
1393 for (pace = *pp_ace; pace; pace = pace->next) {
1394 trim_ace_perms(pace);
1395 if (pace->type == SMB_ACL_USER_OBJ) {
1396 ensure_minimal_owner_ace_perms(is_directory, pace);
1397 pace_user = pace;
1398 } else if (pace->type == SMB_ACL_GROUP_OBJ) {
1399 pace_group = pace;
1400 } else if (pace->type == SMB_ACL_OTHER) {
1401 pace_other = pace;
1405 if (!pace_user) {
1406 canon_ace *pace_iter;
1408 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1409 DEBUG(0,("talloc fail.\n"));
1410 return false;
1413 ZERO_STRUCTP(pace);
1414 pace->type = SMB_ACL_USER_OBJ;
1415 pace->owner_type = UID_ACE;
1416 pace->unix_ug.type = ID_TYPE_UID;
1417 pace->unix_ug.id = pst->st_ex_uid;
1418 pace->trustee = *pfile_owner_sid;
1419 pace->attr = ALLOW_ACE;
1420 /* Start with existing user permissions, principle of least
1421 surprises for the user. */
1422 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IRUSR, S_IWUSR, S_IXUSR);
1424 /* See if the owning user is in any of the other groups in
1425 the ACE, or if there's a matching user entry (by uid
1426 or in the case of ID_TYPE_BOTH by SID).
1427 If so, OR in the permissions from that entry. */
1430 for (pace_iter = *pp_ace; pace_iter; pace_iter = pace_iter->next) {
1431 if (pace_iter->type == SMB_ACL_USER &&
1432 pace_iter->unix_ug.id == pace->unix_ug.id) {
1433 pace->perms |= pace_iter->perms;
1434 } else if (pace_iter->type == SMB_ACL_GROUP_OBJ || pace_iter->type == SMB_ACL_GROUP) {
1435 if (dom_sid_equal(&pace->trustee, &pace_iter->trustee)) {
1436 pace->perms |= pace_iter->perms;
1437 } else if (uid_entry_in_group(conn, pace, pace_iter)) {
1438 pace->perms |= pace_iter->perms;
1443 if (pace->perms == 0) {
1444 /* If we only got an "everyone" perm, just use that. */
1445 if (pace_other)
1446 pace->perms = pace_other->perms;
1450 * Ensure we have default parameters for the
1451 * user (owner) even on default ACLs.
1453 ensure_minimal_owner_ace_perms(is_directory, pace);
1455 DLIST_ADD(*pp_ace, pace);
1456 pace_user = pace;
1459 if (!pace_group) {
1460 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1461 DEBUG(0,("talloc fail.\n"));
1462 return false;
1465 ZERO_STRUCTP(pace);
1466 pace->type = SMB_ACL_GROUP_OBJ;
1467 pace->owner_type = GID_ACE;
1468 pace->unix_ug.type = ID_TYPE_GID;
1469 pace->unix_ug.id = pst->st_ex_gid;
1470 pace->trustee = *pfile_grp_sid;
1471 pace->attr = ALLOW_ACE;
1473 /* If we only got an "everyone" perm, just use that. */
1474 if (pace_other) {
1475 pace->perms = pace_other->perms;
1476 } else {
1477 pace->perms = 0;
1480 DLIST_ADD(*pp_ace, pace);
1481 pace_group = pace;
1484 if (!pace_other) {
1485 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1486 DEBUG(0,("talloc fail.\n"));
1487 return false;
1490 ZERO_STRUCTP(pace);
1491 pace->type = SMB_ACL_OTHER;
1492 pace->owner_type = WORLD_ACE;
1493 pace->unix_ug.type = ID_TYPE_NOT_SPECIFIED;
1494 pace->unix_ug.id = -1;
1495 pace->trustee = global_sid_World;
1496 pace->attr = ALLOW_ACE;
1497 pace->perms = 0;
1499 DLIST_ADD(*pp_ace, pace);
1500 pace_other = pace;
1503 /* Ensure when setting a POSIX ACL, that the uid for a
1504 SMB_ACL_USER_OBJ ACE (the owner ACE entry) has a duplicate
1505 permission entry as an SMB_ACL_USER, and a gid for a
1506 SMB_ACL_GROUP_OBJ ACE (the primary group ACE entry) also has
1507 a duplicate permission entry as an SMB_ACL_GROUP. If not,
1508 then if the ownership or group ownership of this file or
1509 directory gets changed, the user or group can lose their
1510 access. */
1512 for (pace = *pp_ace; pace; pace = pace->next) {
1513 if (pace->type == SMB_ACL_USER &&
1514 pace->unix_ug.id == pace_user->unix_ug.id) {
1515 /* Already got one. */
1516 got_duplicate_user = true;
1517 } else if (pace->type == SMB_ACL_GROUP &&
1518 pace->unix_ug.id == pace_group->unix_ug.id) {
1519 /* Already got one. */
1520 got_duplicate_group = true;
1521 } else if ((pace->type == SMB_ACL_GROUP)
1522 && (dom_sid_equal(&pace->trustee, &pace_user->trustee))) {
1523 /* If the SID owning the file appears
1524 * in a group entry, then we have
1525 * enough duplication, they will still
1526 * have access */
1527 got_duplicate_user = true;
1531 /* If the SID is equal for the user and group that we need
1532 to add the duplicate for, add only the group */
1533 if (!got_duplicate_user && !got_duplicate_group
1534 && dom_sid_equal(&pace_group->trustee,
1535 &pace_user->trustee)) {
1536 /* Add a duplicate SMB_ACL_GROUP entry, this
1537 * will cover the owning SID as well, as it
1538 * will always be mapped to both a uid and
1539 * gid. */
1541 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1542 DEBUG(0,("talloc fail.\n"));
1543 return false;
1546 ZERO_STRUCTP(pace);
1547 pace->type = SMB_ACL_GROUP;;
1548 pace->owner_type = GID_ACE;
1549 pace->unix_ug.type = ID_TYPE_GID;
1550 pace->unix_ug.id = pace_group->unix_ug.id;
1551 pace->trustee = pace_group->trustee;
1552 pace->attr = pace_group->attr;
1553 pace->perms = pace_group->perms;
1555 DLIST_ADD(*pp_ace, pace);
1557 /* We're done here, make sure the
1558 statements below are not executed. */
1559 got_duplicate_user = true;
1560 got_duplicate_group = true;
1563 if (!got_duplicate_user) {
1564 /* Add a duplicate SMB_ACL_USER entry. */
1565 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1566 DEBUG(0,("talloc fail.\n"));
1567 return false;
1570 ZERO_STRUCTP(pace);
1571 pace->type = SMB_ACL_USER;;
1572 pace->owner_type = UID_ACE;
1573 pace->unix_ug.type = ID_TYPE_UID;
1574 pace->unix_ug.id = pace_user->unix_ug.id;
1575 pace->trustee = pace_user->trustee;
1576 pace->attr = pace_user->attr;
1577 pace->perms = pace_user->perms;
1579 DLIST_ADD(*pp_ace, pace);
1581 got_duplicate_user = true;
1584 if (!got_duplicate_group) {
1585 /* Add a duplicate SMB_ACL_GROUP entry. */
1586 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1587 DEBUG(0,("talloc fail.\n"));
1588 return false;
1591 ZERO_STRUCTP(pace);
1592 pace->type = SMB_ACL_GROUP;;
1593 pace->owner_type = GID_ACE;
1594 pace->unix_ug.type = ID_TYPE_GID;
1595 pace->unix_ug.id = pace_group->unix_ug.id;
1596 pace->trustee = pace_group->trustee;
1597 pace->attr = pace_group->attr;
1598 pace->perms = pace_group->perms;
1600 DLIST_ADD(*pp_ace, pace);
1602 got_duplicate_group = true;
1605 return true;
1608 /****************************************************************************
1609 Check if a POSIX ACL has the required SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ entries.
1610 If it does not have them, check if there are any entries where the trustee is the
1611 file owner or the owning group, and map these to SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ.
1612 Note we must not do this to default directory ACLs.
1613 ****************************************************************************/
1615 static void check_owning_objs(canon_ace *ace, struct dom_sid *pfile_owner_sid, struct dom_sid *pfile_grp_sid)
1617 bool got_user_obj, got_group_obj;
1618 canon_ace *current_ace;
1619 int i, entries;
1621 entries = count_canon_ace_list(ace);
1622 got_user_obj = False;
1623 got_group_obj = False;
1625 for (i=0, current_ace = ace; i < entries; i++, current_ace = current_ace->next) {
1626 if (current_ace->type == SMB_ACL_USER_OBJ)
1627 got_user_obj = True;
1628 else if (current_ace->type == SMB_ACL_GROUP_OBJ)
1629 got_group_obj = True;
1631 if (got_user_obj && got_group_obj) {
1632 DEBUG(10,("check_owning_objs: ACL had owning user/group entries.\n"));
1633 return;
1636 for (i=0, current_ace = ace; i < entries; i++, current_ace = current_ace->next) {
1637 if (!got_user_obj && current_ace->owner_type == UID_ACE &&
1638 dom_sid_equal(&current_ace->trustee, pfile_owner_sid)) {
1639 current_ace->type = SMB_ACL_USER_OBJ;
1640 got_user_obj = True;
1642 if (!got_group_obj && current_ace->owner_type == GID_ACE &&
1643 dom_sid_equal(&current_ace->trustee, pfile_grp_sid)) {
1644 current_ace->type = SMB_ACL_GROUP_OBJ;
1645 got_group_obj = True;
1648 if (!got_user_obj)
1649 DEBUG(10,("check_owning_objs: ACL is missing an owner entry.\n"));
1650 if (!got_group_obj)
1651 DEBUG(10,("check_owning_objs: ACL is missing an owning group entry.\n"));
1654 static bool add_current_ace_to_acl(files_struct *fsp, struct security_ace *psa,
1655 canon_ace **file_ace, canon_ace **dir_ace,
1656 bool *got_file_allow, bool *got_dir_allow,
1657 bool *all_aces_are_inherit_only,
1658 canon_ace *current_ace)
1662 * Map the given NT permissions into a UNIX mode_t containing only
1663 * S_I(R|W|X)USR bits.
1666 current_ace->perms |= map_nt_perms( &psa->access_mask, S_IRUSR);
1667 current_ace->attr = (psa->type == SEC_ACE_TYPE_ACCESS_ALLOWED) ? ALLOW_ACE : DENY_ACE;
1669 /* Store the ace_flag. */
1670 current_ace->ace_flags = psa->flags;
1673 * Now add the created ace to either the file list, the directory
1674 * list, or both. We *MUST* preserve the order here (hence we use
1675 * DLIST_ADD_END) as NT ACLs are order dependent.
1678 if (fsp->is_directory) {
1681 * We can only add to the default POSIX ACE list if the ACE is
1682 * designed to be inherited by both files and directories.
1685 if ((psa->flags & (SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT)) ==
1686 (SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT)) {
1688 canon_ace *current_dir_ace = current_ace;
1689 DLIST_ADD_END(*dir_ace, current_ace);
1692 * Note if this was an allow ace. We can't process
1693 * any further deny ace's after this.
1696 if (current_ace->attr == ALLOW_ACE)
1697 *got_dir_allow = True;
1699 if ((current_ace->attr == DENY_ACE) && *got_dir_allow) {
1700 DEBUG(0,("add_current_ace_to_acl: "
1701 "malformed ACL in "
1702 "inheritable ACL! Deny entry "
1703 "after Allow entry. Failing "
1704 "to set on file %s.\n",
1705 fsp_str_dbg(fsp)));
1706 return False;
1709 if( DEBUGLVL( 10 )) {
1710 dbgtext("add_current_ace_to_acl: adding dir ACL:\n");
1711 print_canon_ace( current_ace, 0);
1715 * If this is not an inherit only ACE we need to add a duplicate
1716 * to the file acl.
1719 if (!(psa->flags & SEC_ACE_FLAG_INHERIT_ONLY)) {
1720 canon_ace *dup_ace = dup_canon_ace(current_ace);
1722 if (!dup_ace) {
1723 DEBUG(0,("add_current_ace_to_acl: malloc fail !\n"));
1724 return False;
1728 * We must not free current_ace here as its
1729 * pointer is now owned by the dir_ace list.
1731 current_ace = dup_ace;
1732 /* We've essentially split this ace into two,
1733 * and added the ace with inheritance request
1734 * bits to the directory ACL. Drop those bits for
1735 * the ACE we're adding to the file list. */
1736 current_ace->ace_flags &= ~(SEC_ACE_FLAG_OBJECT_INHERIT|
1737 SEC_ACE_FLAG_CONTAINER_INHERIT|
1738 SEC_ACE_FLAG_INHERIT_ONLY);
1739 } else {
1741 * We must not free current_ace here as its
1742 * pointer is now owned by the dir_ace list.
1744 current_ace = NULL;
1748 * current_ace is now either owned by file_ace
1749 * or is NULL. We can safely operate on current_dir_ace
1750 * to treat mapping for default acl entries differently
1751 * than access acl entries.
1754 if (current_dir_ace->owner_type == UID_ACE) {
1756 * We already decided above this is a uid,
1757 * for default acls ace's only CREATOR_OWNER
1758 * maps to ACL_USER_OBJ. All other uid
1759 * ace's are ACL_USER.
1761 if (dom_sid_equal(&current_dir_ace->trustee,
1762 &global_sid_Creator_Owner)) {
1763 current_dir_ace->type = SMB_ACL_USER_OBJ;
1764 } else {
1765 current_dir_ace->type = SMB_ACL_USER;
1769 if (current_dir_ace->owner_type == GID_ACE) {
1771 * We already decided above this is a gid,
1772 * for default acls ace's only CREATOR_GROUP
1773 * maps to ACL_GROUP_OBJ. All other uid
1774 * ace's are ACL_GROUP.
1776 if (dom_sid_equal(&current_dir_ace->trustee,
1777 &global_sid_Creator_Group)) {
1778 current_dir_ace->type = SMB_ACL_GROUP_OBJ;
1779 } else {
1780 current_dir_ace->type = SMB_ACL_GROUP;
1787 * Only add to the file ACL if not inherit only.
1790 if (current_ace && !(psa->flags & SEC_ACE_FLAG_INHERIT_ONLY)) {
1791 DLIST_ADD_END(*file_ace, current_ace);
1794 * Note if this was an allow ace. We can't process
1795 * any further deny ace's after this.
1798 if (current_ace->attr == ALLOW_ACE)
1799 *got_file_allow = True;
1801 if ((current_ace->attr == DENY_ACE) && *got_file_allow) {
1802 DEBUG(0,("add_current_ace_to_acl: malformed "
1803 "ACL in file ACL ! Deny entry after "
1804 "Allow entry. Failing to set on file "
1805 "%s.\n", fsp_str_dbg(fsp)));
1806 return False;
1809 if( DEBUGLVL( 10 )) {
1810 dbgtext("add_current_ace_to_acl: adding file ACL:\n");
1811 print_canon_ace( current_ace, 0);
1813 *all_aces_are_inherit_only = False;
1815 * We must not free current_ace here as its
1816 * pointer is now owned by the file_ace list.
1818 current_ace = NULL;
1822 * Free if ACE was not added.
1825 TALLOC_FREE(current_ace);
1826 return true;
1829 /****************************************************************************
1830 Unpack a struct security_descriptor into two canonical ace lists.
1831 ****************************************************************************/
1833 static bool create_canon_ace_lists(files_struct *fsp,
1834 const SMB_STRUCT_STAT *pst,
1835 struct dom_sid *pfile_owner_sid,
1836 struct dom_sid *pfile_grp_sid,
1837 canon_ace **ppfile_ace,
1838 canon_ace **ppdir_ace,
1839 const struct security_acl *dacl)
1841 bool all_aces_are_inherit_only = (fsp->is_directory ? True : False);
1842 canon_ace *file_ace = NULL;
1843 canon_ace *dir_ace = NULL;
1844 canon_ace *current_ace = NULL;
1845 bool got_dir_allow = False;
1846 bool got_file_allow = False;
1847 int i, j;
1849 *ppfile_ace = NULL;
1850 *ppdir_ace = NULL;
1853 * Convert the incoming ACL into a more regular form.
1856 for(i = 0; i < dacl->num_aces; i++) {
1857 struct security_ace *psa = &dacl->aces[i];
1859 if((psa->type != SEC_ACE_TYPE_ACCESS_ALLOWED) && (psa->type != SEC_ACE_TYPE_ACCESS_DENIED)) {
1860 DEBUG(3,("create_canon_ace_lists: unable to set anything but an ALLOW or DENY ACE.\n"));
1861 return False;
1866 * Deal with the fact that NT 4.x re-writes the canonical format
1867 * that we return for default ACLs. If a directory ACE is identical
1868 * to a inherited directory ACE then NT changes the bits so that the
1869 * first ACE is set to OI|IO and the second ACE for this SID is set
1870 * to CI. We need to repair this. JRA.
1873 for(i = 0; i < dacl->num_aces; i++) {
1874 struct security_ace *psa1 = &dacl->aces[i];
1876 for (j = i + 1; j < dacl->num_aces; j++) {
1877 struct security_ace *psa2 = &dacl->aces[j];
1879 if (psa1->access_mask != psa2->access_mask)
1880 continue;
1882 if (!dom_sid_equal(&psa1->trustee, &psa2->trustee))
1883 continue;
1886 * Ok - permission bits and SIDs are equal.
1887 * Check if flags were re-written.
1890 if (psa1->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
1892 psa1->flags |= (psa2->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT));
1893 psa2->flags &= ~(SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT);
1895 } else if (psa2->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
1897 psa2->flags |= (psa1->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT));
1898 psa1->flags &= ~(SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT);
1904 for(i = 0; i < dacl->num_aces; i++) {
1905 struct security_ace *psa = &dacl->aces[i];
1908 * Create a canon_ace entry representing this NT DACL ACE.
1911 if ((current_ace = talloc(talloc_tos(), canon_ace)) == NULL) {
1912 free_canon_ace_list(file_ace);
1913 free_canon_ace_list(dir_ace);
1914 DEBUG(0,("create_canon_ace_lists: malloc fail.\n"));
1915 return False;
1918 ZERO_STRUCTP(current_ace);
1920 sid_copy(&current_ace->trustee, &psa->trustee);
1923 * Try and work out if the SID is a user or group
1924 * as we need to flag these differently for POSIX.
1925 * Note what kind of a POSIX ACL this should map to.
1928 if( dom_sid_equal(&current_ace->trustee, &global_sid_World)) {
1929 current_ace->owner_type = WORLD_ACE;
1930 current_ace->unix_ug.type = ID_TYPE_NOT_SPECIFIED;
1931 current_ace->unix_ug.id = -1;
1932 current_ace->type = SMB_ACL_OTHER;
1933 } else if (dom_sid_equal(&current_ace->trustee, &global_sid_Creator_Owner)) {
1934 current_ace->owner_type = UID_ACE;
1935 current_ace->unix_ug.type = ID_TYPE_UID;
1936 current_ace->unix_ug.id = pst->st_ex_uid;
1937 current_ace->type = SMB_ACL_USER_OBJ;
1940 * The Creator Owner entry only specifies inheritable permissions,
1941 * never access permissions. WinNT doesn't always set the ACE to
1942 * INHERIT_ONLY, though.
1945 psa->flags |= SEC_ACE_FLAG_INHERIT_ONLY;
1947 } else if (dom_sid_equal(&current_ace->trustee, &global_sid_Creator_Group)) {
1948 current_ace->owner_type = GID_ACE;
1949 current_ace->unix_ug.type = ID_TYPE_GID;
1950 current_ace->unix_ug.id = pst->st_ex_gid;
1951 current_ace->type = SMB_ACL_GROUP_OBJ;
1954 * The Creator Group entry only specifies inheritable permissions,
1955 * never access permissions. WinNT doesn't always set the ACE to
1956 * INHERIT_ONLY, though.
1958 psa->flags |= SEC_ACE_FLAG_INHERIT_ONLY;
1960 } else {
1961 struct unixid unixid;
1963 if (!sids_to_unixids(&current_ace->trustee, 1, &unixid)) {
1964 free_canon_ace_list(file_ace);
1965 free_canon_ace_list(dir_ace);
1966 TALLOC_FREE(current_ace);
1967 DEBUG(0, ("create_canon_ace_lists: sids_to_unixids "
1968 "failed for %s (allocation failure)\n",
1969 sid_string_dbg(&current_ace->trustee)));
1970 return false;
1973 if (unixid.type == ID_TYPE_BOTH) {
1975 * We must add both a user and group
1976 * entry POSIX_ACL.
1977 * This is due to the fact that in POSIX
1978 * user entries are more specific than
1979 * groups.
1981 current_ace->owner_type = UID_ACE;
1982 current_ace->unix_ug.type = ID_TYPE_UID;
1983 current_ace->unix_ug.id = unixid.id;
1984 current_ace->type =
1985 (unixid.id == pst->st_ex_uid) ?
1986 SMB_ACL_USER_OBJ :
1987 SMB_ACL_USER;
1989 /* Add the user object to the posix ACL,
1990 and proceed to the group mapping
1991 below. This handles the talloc_free
1992 of current_ace if not added for some
1993 reason */
1994 if (!add_current_ace_to_acl(fsp,
1995 psa,
1996 &file_ace,
1997 &dir_ace,
1998 &got_file_allow,
1999 &got_dir_allow,
2000 &all_aces_are_inherit_only,
2001 current_ace)) {
2002 free_canon_ace_list(file_ace);
2003 free_canon_ace_list(dir_ace);
2004 return false;
2007 if ((current_ace = talloc(talloc_tos(),
2008 canon_ace)) == NULL) {
2009 free_canon_ace_list(file_ace);
2010 free_canon_ace_list(dir_ace);
2011 DEBUG(0,("create_canon_ace_lists: "
2012 "malloc fail.\n"));
2013 return False;
2016 ZERO_STRUCTP(current_ace);
2018 sid_copy(&current_ace->trustee, &psa->trustee);
2020 current_ace->unix_ug.type = ID_TYPE_GID;
2021 current_ace->unix_ug.id = unixid.id;
2022 current_ace->owner_type = GID_ACE;
2023 /* If it's the primary group, this is a
2024 group_obj, not a group. */
2025 if (current_ace->unix_ug.id == pst->st_ex_gid) {
2026 current_ace->type = SMB_ACL_GROUP_OBJ;
2027 } else {
2028 current_ace->type = SMB_ACL_GROUP;
2031 } else if (unixid.type == ID_TYPE_UID) {
2032 current_ace->owner_type = UID_ACE;
2033 current_ace->unix_ug.type = ID_TYPE_UID;
2034 current_ace->unix_ug.id = unixid.id;
2035 /* If it's the owning user, this is a user_obj,
2036 not a user. */
2037 if (current_ace->unix_ug.id == pst->st_ex_uid) {
2038 current_ace->type = SMB_ACL_USER_OBJ;
2039 } else {
2040 current_ace->type = SMB_ACL_USER;
2042 } else if (unixid.type == ID_TYPE_GID) {
2043 current_ace->unix_ug.type = ID_TYPE_GID;
2044 current_ace->unix_ug.id = unixid.id;
2045 current_ace->owner_type = GID_ACE;
2046 /* If it's the primary group, this is a
2047 group_obj, not a group. */
2048 if (current_ace->unix_ug.id == pst->st_ex_gid) {
2049 current_ace->type = SMB_ACL_GROUP_OBJ;
2050 } else {
2051 current_ace->type = SMB_ACL_GROUP;
2053 } else {
2055 * Silently ignore map failures in non-mappable SIDs (NT Authority, BUILTIN etc).
2058 if (non_mappable_sid(&psa->trustee)) {
2059 DEBUG(10, ("create_canon_ace_lists: ignoring "
2060 "non-mappable SID %s\n",
2061 sid_string_dbg(&psa->trustee)));
2062 TALLOC_FREE(current_ace);
2063 continue;
2066 if (lp_force_unknown_acl_user(SNUM(fsp->conn))) {
2067 DEBUG(10, ("create_canon_ace_lists: ignoring "
2068 "unknown or foreign SID %s\n",
2069 sid_string_dbg(&psa->trustee)));
2070 TALLOC_FREE(current_ace);
2071 continue;
2074 free_canon_ace_list(file_ace);
2075 free_canon_ace_list(dir_ace);
2076 DEBUG(0, ("create_canon_ace_lists: unable to map SID "
2077 "%s to uid or gid.\n",
2078 sid_string_dbg(&current_ace->trustee)));
2079 TALLOC_FREE(current_ace);
2080 return false;
2084 /* handles the talloc_free of current_ace if not added for some reason */
2085 if (!add_current_ace_to_acl(fsp, psa, &file_ace, &dir_ace,
2086 &got_file_allow, &got_dir_allow,
2087 &all_aces_are_inherit_only, current_ace)) {
2088 free_canon_ace_list(file_ace);
2089 free_canon_ace_list(dir_ace);
2090 return false;
2094 if (fsp->is_directory && all_aces_are_inherit_only) {
2096 * Windows 2000 is doing one of these weird 'inherit acl'
2097 * traverses to conserve NTFS ACL resources. Just pretend
2098 * there was no DACL sent. JRA.
2101 DEBUG(10,("create_canon_ace_lists: Win2k inherit acl traverse. Ignoring DACL.\n"));
2102 free_canon_ace_list(file_ace);
2103 free_canon_ace_list(dir_ace);
2104 file_ace = NULL;
2105 dir_ace = NULL;
2106 } else {
2108 * Check if we have SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ entries in
2109 * the file ACL. If we don't have them, check if any SMB_ACL_USER/SMB_ACL_GROUP
2110 * entries can be converted to *_OBJ. Don't do this for the default
2111 * ACL, we will create them separately for this if needed inside
2112 * ensure_canon_entry_valid_on_set().
2114 if (file_ace) {
2115 check_owning_objs(file_ace, pfile_owner_sid, pfile_grp_sid);
2119 *ppfile_ace = file_ace;
2120 *ppdir_ace = dir_ace;
2122 return True;
2125 /****************************************************************************
2126 ASCII art time again... JRA :-).
2128 We have 4 cases to process when moving from an NT ACL to a POSIX ACL. Firstly,
2129 we insist the ACL is in canonical form (ie. all DENY entries preceede ALLOW
2130 entries). Secondly, the merge code has ensured that all duplicate SID entries for
2131 allow or deny have been merged, so the same SID can only appear once in the deny
2132 list or once in the allow list.
2134 We then process as follows :
2136 ---------------------------------------------------------------------------
2137 First pass - look for a Everyone DENY entry.
2139 If it is deny all (rwx) trunate the list at this point.
2140 Else, walk the list from this point and use the deny permissions of this
2141 entry as a mask on all following allow entries. Finally, delete
2142 the Everyone DENY entry (we have applied it to everything possible).
2144 In addition, in this pass we remove any DENY entries that have
2145 no permissions (ie. they are a DENY nothing).
2146 ---------------------------------------------------------------------------
2147 Second pass - only deal with deny user entries.
2149 DENY user1 (perms XXX)
2151 new_perms = 0
2152 for all following allow group entries where user1 is in group
2153 new_perms |= group_perms;
2155 user1 entry perms = new_perms & ~ XXX;
2157 Convert the deny entry to an allow entry with the new perms and
2158 push to the end of the list. Note if the user was in no groups
2159 this maps to a specific allow nothing entry for this user.
2161 The common case from the NT ACL choser (userX deny all) is
2162 optimised so we don't do the group lookup - we just map to
2163 an allow nothing entry.
2165 What we're doing here is inferring the allow permissions the
2166 person setting the ACE on user1 wanted by looking at the allow
2167 permissions on the groups the user is currently in. This will
2168 be a snapshot, depending on group membership but is the best
2169 we can do and has the advantage of failing closed rather than
2170 open.
2171 ---------------------------------------------------------------------------
2172 Third pass - only deal with deny group entries.
2174 DENY group1 (perms XXX)
2176 for all following allow user entries where user is in group1
2177 user entry perms = user entry perms & ~ XXX;
2179 If there is a group Everyone allow entry with permissions YYY,
2180 convert the group1 entry to an allow entry and modify its
2181 permissions to be :
2183 new_perms = YYY & ~ XXX
2185 and push to the end of the list.
2187 If there is no group Everyone allow entry then convert the
2188 group1 entry to a allow nothing entry and push to the end of the list.
2190 Note that the common case from the NT ACL choser (groupX deny all)
2191 cannot be optimised here as we need to modify user entries who are
2192 in the group to change them to a deny all also.
2194 What we're doing here is modifying the allow permissions of
2195 user entries (which are more specific in POSIX ACLs) to mask
2196 out the explicit deny set on the group they are in. This will
2197 be a snapshot depending on current group membership but is the
2198 best we can do and has the advantage of failing closed rather
2199 than open.
2200 ---------------------------------------------------------------------------
2201 Fourth pass - cope with cumulative permissions.
2203 for all allow user entries, if there exists an allow group entry with
2204 more permissive permissions, and the user is in that group, rewrite the
2205 allow user permissions to contain both sets of permissions.
2207 Currently the code for this is #ifdef'ed out as these semantics make
2208 no sense to me. JRA.
2209 ---------------------------------------------------------------------------
2211 Note we *MUST* do the deny user pass first as this will convert deny user
2212 entries into allow user entries which can then be processed by the deny
2213 group pass.
2215 The above algorithm took a *lot* of thinking about - hence this
2216 explaination :-). JRA.
2217 ****************************************************************************/
2219 /****************************************************************************
2220 Process a canon_ace list entries. This is very complex code. We need
2221 to go through and remove the "deny" permissions from any allow entry that matches
2222 the id of this entry. We have already refused any NT ACL that wasn't in correct
2223 order (DENY followed by ALLOW). If any allow entry ends up with zero permissions,
2224 we just remove it (to fail safe). We have already removed any duplicate ace
2225 entries. Treat an "Everyone" DENY_ACE as a special case - use it to mask all
2226 allow entries.
2227 ****************************************************************************/
2229 static void process_deny_list(connection_struct *conn, canon_ace **pp_ace_list )
2231 canon_ace *ace_list = *pp_ace_list;
2232 canon_ace *curr_ace = NULL;
2233 canon_ace *curr_ace_next = NULL;
2235 /* Pass 1 above - look for an Everyone, deny entry. */
2237 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2238 canon_ace *allow_ace_p;
2240 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2242 if (curr_ace->attr != DENY_ACE)
2243 continue;
2245 if (curr_ace->perms == (mode_t)0) {
2247 /* Deny nothing entry - delete. */
2249 DLIST_REMOVE(ace_list, curr_ace);
2250 continue;
2253 if (!dom_sid_equal(&curr_ace->trustee, &global_sid_World))
2254 continue;
2256 /* JRATEST - assert. */
2257 SMB_ASSERT(curr_ace->owner_type == WORLD_ACE);
2259 if (curr_ace->perms == ALL_ACE_PERMS) {
2262 * Optimisation. This is a DENY_ALL to Everyone. Truncate the
2263 * list at this point including this entry.
2266 canon_ace *prev_entry = DLIST_PREV(curr_ace);
2268 free_canon_ace_list( curr_ace );
2269 if (prev_entry)
2270 DLIST_REMOVE(ace_list, prev_entry);
2271 else {
2272 /* We deleted the entire list. */
2273 ace_list = NULL;
2275 break;
2278 for (allow_ace_p = curr_ace->next; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2281 * Only mask off allow entries.
2284 if (allow_ace_p->attr != ALLOW_ACE)
2285 continue;
2287 allow_ace_p->perms &= ~curr_ace->perms;
2291 * Now it's been applied, remove it.
2294 DLIST_REMOVE(ace_list, curr_ace);
2297 /* Pass 2 above - deal with deny user entries. */
2299 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2300 mode_t new_perms = (mode_t)0;
2301 canon_ace *allow_ace_p;
2303 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2305 if (curr_ace->attr != DENY_ACE)
2306 continue;
2308 if (curr_ace->owner_type != UID_ACE)
2309 continue;
2311 if (curr_ace->perms == ALL_ACE_PERMS) {
2314 * Optimisation - this is a deny everything to this user.
2315 * Convert to an allow nothing and push to the end of the list.
2318 curr_ace->attr = ALLOW_ACE;
2319 curr_ace->perms = (mode_t)0;
2320 DLIST_DEMOTE(ace_list, curr_ace);
2321 continue;
2324 for (allow_ace_p = curr_ace->next; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2326 if (allow_ace_p->attr != ALLOW_ACE)
2327 continue;
2329 /* We process GID_ACE and WORLD_ACE entries only. */
2331 if (allow_ace_p->owner_type == UID_ACE)
2332 continue;
2334 if (uid_entry_in_group(conn, curr_ace, allow_ace_p))
2335 new_perms |= allow_ace_p->perms;
2339 * Convert to a allow entry, modify the perms and push to the end
2340 * of the list.
2343 curr_ace->attr = ALLOW_ACE;
2344 curr_ace->perms = (new_perms & ~curr_ace->perms);
2345 DLIST_DEMOTE(ace_list, curr_ace);
2348 /* Pass 3 above - deal with deny group entries. */
2350 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2351 canon_ace *allow_ace_p;
2352 canon_ace *allow_everyone_p = NULL;
2354 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2356 if (curr_ace->attr != DENY_ACE)
2357 continue;
2359 if (curr_ace->owner_type != GID_ACE)
2360 continue;
2362 for (allow_ace_p = curr_ace->next; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2364 if (allow_ace_p->attr != ALLOW_ACE)
2365 continue;
2367 /* Store a pointer to the Everyone allow, if it exists. */
2368 if (allow_ace_p->owner_type == WORLD_ACE)
2369 allow_everyone_p = allow_ace_p;
2371 /* We process UID_ACE entries only. */
2373 if (allow_ace_p->owner_type != UID_ACE)
2374 continue;
2376 /* Mask off the deny group perms. */
2378 if (uid_entry_in_group(conn, allow_ace_p, curr_ace))
2379 allow_ace_p->perms &= ~curr_ace->perms;
2383 * Convert the deny to an allow with the correct perms and
2384 * push to the end of the list.
2387 curr_ace->attr = ALLOW_ACE;
2388 if (allow_everyone_p)
2389 curr_ace->perms = allow_everyone_p->perms & ~curr_ace->perms;
2390 else
2391 curr_ace->perms = (mode_t)0;
2392 DLIST_DEMOTE(ace_list, curr_ace);
2395 /* Doing this fourth pass allows Windows semantics to be layered
2396 * on top of POSIX semantics. I'm not sure if this is desirable.
2397 * For example, in W2K ACLs there is no way to say, "Group X no
2398 * access, user Y full access" if user Y is a member of group X.
2399 * This seems completely broken semantics to me.... JRA.
2402 #if 0
2403 /* Pass 4 above - deal with allow entries. */
2405 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2406 canon_ace *allow_ace_p;
2408 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2410 if (curr_ace->attr != ALLOW_ACE)
2411 continue;
2413 if (curr_ace->owner_type != UID_ACE)
2414 continue;
2416 for (allow_ace_p = ace_list; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2418 if (allow_ace_p->attr != ALLOW_ACE)
2419 continue;
2421 /* We process GID_ACE entries only. */
2423 if (allow_ace_p->owner_type != GID_ACE)
2424 continue;
2426 /* OR in the group perms. */
2428 if (uid_entry_in_group(conn, curr_ace, allow_ace_p))
2429 curr_ace->perms |= allow_ace_p->perms;
2432 #endif
2434 *pp_ace_list = ace_list;
2437 /****************************************************************************
2438 Unpack a struct security_descriptor into two canonical ace lists. We don't depend on this
2439 succeeding.
2440 ****************************************************************************/
2442 static bool unpack_canon_ace(files_struct *fsp,
2443 const SMB_STRUCT_STAT *pst,
2444 struct dom_sid *pfile_owner_sid,
2445 struct dom_sid *pfile_grp_sid,
2446 canon_ace **ppfile_ace,
2447 canon_ace **ppdir_ace,
2448 uint32_t security_info_sent,
2449 const struct security_descriptor *psd)
2451 canon_ace *file_ace = NULL;
2452 canon_ace *dir_ace = NULL;
2454 *ppfile_ace = NULL;
2455 *ppdir_ace = NULL;
2457 if(security_info_sent == 0) {
2458 DEBUG(0,("unpack_canon_ace: no security info sent !\n"));
2459 return False;
2463 * If no DACL then this is a chown only security descriptor.
2466 if(!(security_info_sent & SECINFO_DACL) || !psd->dacl)
2467 return True;
2470 * Now go through the DACL and create the canon_ace lists.
2473 if (!create_canon_ace_lists( fsp, pst, pfile_owner_sid, pfile_grp_sid,
2474 &file_ace, &dir_ace, psd->dacl))
2475 return False;
2477 if ((file_ace == NULL) && (dir_ace == NULL)) {
2478 /* W2K traverse DACL set - ignore. */
2479 return True;
2483 * Go through the canon_ace list and merge entries
2484 * belonging to identical users of identical allow or deny type.
2485 * We can do this as all deny entries come first, followed by
2486 * all allow entries (we have mandated this before accepting this acl).
2489 print_canon_ace_list( "file ace - before merge", file_ace);
2490 merge_aces( &file_ace, false);
2492 print_canon_ace_list( "dir ace - before merge", dir_ace);
2493 merge_aces( &dir_ace, true);
2496 * NT ACLs are order dependent. Go through the acl lists and
2497 * process DENY entries by masking the allow entries.
2500 print_canon_ace_list( "file ace - before deny", file_ace);
2501 process_deny_list(fsp->conn, &file_ace);
2503 print_canon_ace_list( "dir ace - before deny", dir_ace);
2504 process_deny_list(fsp->conn, &dir_ace);
2507 * A well formed POSIX file or default ACL has at least 3 entries, a
2508 * SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ
2509 * and optionally a mask entry. Ensure this is the case.
2512 print_canon_ace_list( "file ace - before valid", file_ace);
2514 if (!ensure_canon_entry_valid_on_set(fsp->conn, &file_ace, false, fsp->conn->params,
2515 fsp->is_directory, pfile_owner_sid, pfile_grp_sid, pst)) {
2516 free_canon_ace_list(file_ace);
2517 free_canon_ace_list(dir_ace);
2518 return False;
2521 print_canon_ace_list( "dir ace - before valid", dir_ace);
2523 if (dir_ace && !ensure_canon_entry_valid_on_set(fsp->conn, &dir_ace, true, fsp->conn->params,
2524 fsp->is_directory, pfile_owner_sid, pfile_grp_sid, pst)) {
2525 free_canon_ace_list(file_ace);
2526 free_canon_ace_list(dir_ace);
2527 return False;
2530 print_canon_ace_list( "file ace - return", file_ace);
2531 print_canon_ace_list( "dir ace - return", dir_ace);
2533 *ppfile_ace = file_ace;
2534 *ppdir_ace = dir_ace;
2535 return True;
2539 /******************************************************************************
2540 When returning permissions, try and fit NT display
2541 semantics if possible. Note the the canon_entries here must have been malloced.
2542 The list format should be - first entry = owner, followed by group and other user
2543 entries, last entry = other.
2545 Note that this doesn't exactly match the NT semantics for an ACL. As POSIX entries
2546 are not ordered, and match on the most specific entry rather than walking a list,
2547 then a simple POSIX permission of rw-r--r-- should really map to 5 entries,
2549 Entry 0: owner : deny all except read and write.
2550 Entry 1: owner : allow read and write.
2551 Entry 2: group : deny all except read.
2552 Entry 3: group : allow read.
2553 Entry 4: Everyone : allow read.
2555 But NT cannot display this in their ACL editor !
2556 ********************************************************************************/
2558 static void arrange_posix_perms(const char *filename, canon_ace **pp_list_head)
2560 canon_ace *l_head = *pp_list_head;
2561 canon_ace *owner_ace = NULL;
2562 canon_ace *other_ace = NULL;
2563 canon_ace *ace = NULL;
2565 for (ace = l_head; ace; ace = ace->next) {
2566 if (ace->type == SMB_ACL_USER_OBJ)
2567 owner_ace = ace;
2568 else if (ace->type == SMB_ACL_OTHER) {
2569 /* Last ace - this is "other" */
2570 other_ace = ace;
2574 if (!owner_ace || !other_ace) {
2575 DEBUG(0,("arrange_posix_perms: Invalid POSIX permissions for file %s, missing owner or other.\n",
2576 filename ));
2577 return;
2581 * The POSIX algorithm applies to owner first, and other last,
2582 * so ensure they are arranged in this order.
2585 if (owner_ace) {
2586 DLIST_PROMOTE(l_head, owner_ace);
2589 if (other_ace) {
2590 DLIST_DEMOTE(l_head, other_ace);
2593 /* We have probably changed the head of the list. */
2595 *pp_list_head = l_head;
2598 /****************************************************************************
2599 Create a linked list of canonical ACE entries.
2600 ****************************************************************************/
2602 static canon_ace *canonicalise_acl(struct connection_struct *conn,
2603 const char *fname, SMB_ACL_T posix_acl,
2604 const SMB_STRUCT_STAT *psbuf,
2605 const struct dom_sid *powner, const struct dom_sid *pgroup, struct pai_val *pal, SMB_ACL_TYPE_T the_acl_type)
2607 mode_t acl_mask = (S_IRUSR|S_IWUSR|S_IXUSR);
2608 canon_ace *l_head = NULL;
2609 canon_ace *ace = NULL;
2610 canon_ace *next_ace = NULL;
2611 int entry_id = SMB_ACL_FIRST_ENTRY;
2612 bool is_default_acl = (the_acl_type == SMB_ACL_TYPE_DEFAULT);
2613 SMB_ACL_ENTRY_T entry;
2614 size_t ace_count;
2616 while ( posix_acl && (sys_acl_get_entry(posix_acl, entry_id, &entry) == 1)) {
2617 SMB_ACL_TAG_T tagtype;
2618 SMB_ACL_PERMSET_T permset;
2619 struct dom_sid sid;
2620 struct unixid unix_ug;
2621 enum ace_owner owner_type;
2623 entry_id = SMB_ACL_NEXT_ENTRY;
2625 if (sys_acl_get_tag_type(entry, &tagtype) == -1)
2626 continue;
2628 if (sys_acl_get_permset(entry, &permset) == -1)
2629 continue;
2631 /* Decide which SID to use based on the ACL type. */
2632 switch(tagtype) {
2633 case SMB_ACL_USER_OBJ:
2634 /* Get the SID from the owner. */
2635 sid_copy(&sid, powner);
2636 unix_ug.type = ID_TYPE_UID;
2637 unix_ug.id = psbuf->st_ex_uid;
2638 owner_type = UID_ACE;
2639 break;
2640 case SMB_ACL_USER:
2642 uid_t *puid = (uid_t *)sys_acl_get_qualifier(entry);
2643 if (puid == NULL) {
2644 DEBUG(0,("canonicalise_acl: Failed to get uid.\n"));
2645 continue;
2647 uid_to_sid( &sid, *puid);
2648 unix_ug.type = ID_TYPE_UID;
2649 unix_ug.id = *puid;
2650 owner_type = UID_ACE;
2651 break;
2653 case SMB_ACL_GROUP_OBJ:
2654 /* Get the SID from the owning group. */
2655 sid_copy(&sid, pgroup);
2656 unix_ug.type = ID_TYPE_GID;
2657 unix_ug.id = psbuf->st_ex_gid;
2658 owner_type = GID_ACE;
2659 break;
2660 case SMB_ACL_GROUP:
2662 gid_t *pgid = (gid_t *)sys_acl_get_qualifier(entry);
2663 if (pgid == NULL) {
2664 DEBUG(0,("canonicalise_acl: Failed to get gid.\n"));
2665 continue;
2667 gid_to_sid( &sid, *pgid);
2668 unix_ug.type = ID_TYPE_GID;
2669 unix_ug.id = *pgid;
2670 owner_type = GID_ACE;
2671 break;
2673 case SMB_ACL_MASK:
2674 acl_mask = convert_permset_to_mode_t(permset);
2675 continue; /* Don't count the mask as an entry. */
2676 case SMB_ACL_OTHER:
2677 /* Use the Everyone SID */
2678 sid = global_sid_World;
2679 unix_ug.type = ID_TYPE_NOT_SPECIFIED;
2680 unix_ug.id = -1;
2681 owner_type = WORLD_ACE;
2682 break;
2683 default:
2684 DEBUG(0,("canonicalise_acl: Unknown tagtype %u\n", (unsigned int)tagtype));
2685 continue;
2689 * Add this entry to the list.
2692 if ((ace = talloc(talloc_tos(), canon_ace)) == NULL)
2693 goto fail;
2695 *ace = (canon_ace) {
2696 .type = tagtype,
2697 .perms = convert_permset_to_mode_t(permset),
2698 .attr = ALLOW_ACE,
2699 .trustee = sid,
2700 .unix_ug = unix_ug,
2701 .owner_type = owner_type
2703 ace->ace_flags = get_pai_flags(pal, ace, is_default_acl);
2705 DLIST_ADD(l_head, ace);
2709 * This next call will ensure we have at least a user/group/world set.
2712 if (!ensure_canon_entry_valid_on_get(conn, &l_head,
2713 powner, pgroup,
2714 psbuf))
2715 goto fail;
2718 * Now go through the list, masking the permissions with the
2719 * acl_mask. Ensure all DENY Entries are at the start of the list.
2722 DEBUG(10,("canonicalise_acl: %s ace entries before arrange :\n", is_default_acl ? "Default" : "Access"));
2724 for ( ace_count = 0, ace = l_head; ace; ace = next_ace, ace_count++) {
2725 next_ace = ace->next;
2727 /* Masks are only applied to entries other than USER_OBJ and OTHER. */
2728 if (ace->type != SMB_ACL_OTHER && ace->type != SMB_ACL_USER_OBJ)
2729 ace->perms &= acl_mask;
2731 if (ace->perms == 0) {
2732 DLIST_PROMOTE(l_head, ace);
2735 if( DEBUGLVL( 10 ) ) {
2736 print_canon_ace(ace, ace_count);
2740 arrange_posix_perms(fname,&l_head );
2742 print_canon_ace_list( "canonicalise_acl: ace entries after arrange", l_head );
2744 return l_head;
2746 fail:
2748 free_canon_ace_list(l_head);
2749 return NULL;
2752 /****************************************************************************
2753 Check if the current user group list contains a given group.
2754 ****************************************************************************/
2756 bool current_user_in_group(connection_struct *conn, gid_t gid)
2758 int i;
2759 const struct security_unix_token *utok = get_current_utok(conn);
2761 for (i = 0; i < utok->ngroups; i++) {
2762 if (utok->groups[i] == gid) {
2763 return True;
2767 return False;
2770 /****************************************************************************
2771 Should we override a deny ? Check 'acl group control' and 'dos filemode'.
2772 ****************************************************************************/
2774 static bool acl_group_override(connection_struct *conn,
2775 const struct smb_filename *smb_fname)
2777 if ((errno != EPERM) && (errno != EACCES)) {
2778 return false;
2781 /* file primary group == user primary or supplementary group */
2782 if (lp_acl_group_control(SNUM(conn)) &&
2783 current_user_in_group(conn, smb_fname->st.st_ex_gid)) {
2784 return true;
2787 /* user has writeable permission */
2788 if (lp_dos_filemode(SNUM(conn)) &&
2789 can_write_to_file(conn, smb_fname)) {
2790 return true;
2793 return false;
2796 /****************************************************************************
2797 Attempt to apply an ACL to a file or directory.
2798 ****************************************************************************/
2800 static bool set_canon_ace_list(files_struct *fsp,
2801 canon_ace *the_ace,
2802 bool default_ace,
2803 const SMB_STRUCT_STAT *psbuf,
2804 bool *pacl_set_support)
2806 connection_struct *conn = fsp->conn;
2807 bool ret = False;
2808 SMB_ACL_T the_acl = sys_acl_init(talloc_tos());
2809 canon_ace *p_ace;
2810 int i;
2811 SMB_ACL_ENTRY_T mask_entry;
2812 bool got_mask_entry = False;
2813 SMB_ACL_PERMSET_T mask_permset;
2814 SMB_ACL_TYPE_T the_acl_type = (default_ace ? SMB_ACL_TYPE_DEFAULT : SMB_ACL_TYPE_ACCESS);
2815 bool needs_mask = False;
2816 mode_t mask_perms = 0;
2818 /* Use the psbuf that was passed in. */
2819 if (psbuf != &fsp->fsp_name->st) {
2820 fsp->fsp_name->st = *psbuf;
2823 #if defined(POSIX_ACL_NEEDS_MASK)
2824 /* HP-UX always wants to have a mask (called "class" there). */
2825 needs_mask = True;
2826 #endif
2828 if (the_acl == NULL) {
2829 DEBUG(0, ("sys_acl_init failed to allocate an ACL\n"));
2830 return false;
2833 if( DEBUGLVL( 10 )) {
2834 dbgtext("set_canon_ace_list: setting ACL:\n");
2835 for (i = 0, p_ace = the_ace; p_ace; p_ace = p_ace->next, i++ ) {
2836 print_canon_ace( p_ace, i);
2840 for (i = 0, p_ace = the_ace; p_ace; p_ace = p_ace->next, i++ ) {
2841 SMB_ACL_ENTRY_T the_entry;
2842 SMB_ACL_PERMSET_T the_permset;
2845 * ACLs only "need" an ACL_MASK entry if there are any named user or
2846 * named group entries. But if there is an ACL_MASK entry, it applies
2847 * to ACL_USER, ACL_GROUP, and ACL_GROUP_OBJ entries. Set the mask
2848 * so that it doesn't deny (i.e., mask off) any permissions.
2851 if (p_ace->type == SMB_ACL_USER || p_ace->type == SMB_ACL_GROUP) {
2852 needs_mask = True;
2853 mask_perms |= p_ace->perms;
2854 } else if (p_ace->type == SMB_ACL_GROUP_OBJ) {
2855 mask_perms |= p_ace->perms;
2859 * Get the entry for this ACE.
2862 if (sys_acl_create_entry(&the_acl, &the_entry) == -1) {
2863 DEBUG(0,("set_canon_ace_list: Failed to create entry %d. (%s)\n",
2864 i, strerror(errno) ));
2865 goto fail;
2868 if (p_ace->type == SMB_ACL_MASK) {
2869 mask_entry = the_entry;
2870 got_mask_entry = True;
2874 * Ok - we now know the ACL calls should be working, don't
2875 * allow fallback to chmod.
2878 *pacl_set_support = True;
2881 * Initialise the entry from the canon_ace.
2885 * First tell the entry what type of ACE this is.
2888 if (sys_acl_set_tag_type(the_entry, p_ace->type) == -1) {
2889 DEBUG(0,("set_canon_ace_list: Failed to set tag type on entry %d. (%s)\n",
2890 i, strerror(errno) ));
2891 goto fail;
2895 * Only set the qualifier (user or group id) if the entry is a user
2896 * or group id ACE.
2899 if ((p_ace->type == SMB_ACL_USER) || (p_ace->type == SMB_ACL_GROUP)) {
2900 if (sys_acl_set_qualifier(the_entry,(void *)&p_ace->unix_ug.id) == -1) {
2901 DEBUG(0,("set_canon_ace_list: Failed to set qualifier on entry %d. (%s)\n",
2902 i, strerror(errno) ));
2903 goto fail;
2908 * Convert the mode_t perms in the canon_ace to a POSIX permset.
2911 if (sys_acl_get_permset(the_entry, &the_permset) == -1) {
2912 DEBUG(0,("set_canon_ace_list: Failed to get permset on entry %d. (%s)\n",
2913 i, strerror(errno) ));
2914 goto fail;
2917 if (map_acl_perms_to_permset(conn, p_ace->perms, &the_permset) == -1) {
2918 DEBUG(0,("set_canon_ace_list: Failed to create permset for mode (%u) on entry %d. (%s)\n",
2919 (unsigned int)p_ace->perms, i, strerror(errno) ));
2920 goto fail;
2924 * ..and apply them to the entry.
2927 if (sys_acl_set_permset(the_entry, the_permset) == -1) {
2928 DEBUG(0,("set_canon_ace_list: Failed to add permset on entry %d. (%s)\n",
2929 i, strerror(errno) ));
2930 goto fail;
2933 if( DEBUGLVL( 10 ))
2934 print_canon_ace( p_ace, i);
2938 if (needs_mask && !got_mask_entry) {
2939 if (sys_acl_create_entry(&the_acl, &mask_entry) == -1) {
2940 DEBUG(0,("set_canon_ace_list: Failed to create mask entry. (%s)\n", strerror(errno) ));
2941 goto fail;
2944 if (sys_acl_set_tag_type(mask_entry, SMB_ACL_MASK) == -1) {
2945 DEBUG(0,("set_canon_ace_list: Failed to set tag type on mask entry. (%s)\n",strerror(errno) ));
2946 goto fail;
2949 if (sys_acl_get_permset(mask_entry, &mask_permset) == -1) {
2950 DEBUG(0,("set_canon_ace_list: Failed to get mask permset. (%s)\n", strerror(errno) ));
2951 goto fail;
2954 if (map_acl_perms_to_permset(conn, S_IRUSR|S_IWUSR|S_IXUSR, &mask_permset) == -1) {
2955 DEBUG(0,("set_canon_ace_list: Failed to create mask permset. (%s)\n", strerror(errno) ));
2956 goto fail;
2959 if (sys_acl_set_permset(mask_entry, mask_permset) == -1) {
2960 DEBUG(0,("set_canon_ace_list: Failed to add mask permset. (%s)\n", strerror(errno) ));
2961 goto fail;
2966 * Finally apply it to the file or directory.
2969 if(default_ace || fsp->is_directory || fsp->fh->fd == -1) {
2970 if (SMB_VFS_SYS_ACL_SET_FILE(conn, fsp->fsp_name->base_name,
2971 the_acl_type, the_acl) == -1) {
2973 * Some systems allow all the above calls and only fail with no ACL support
2974 * when attempting to apply the acl. HPUX with HFS is an example of this. JRA.
2976 if (no_acl_syscall_error(errno)) {
2977 *pacl_set_support = False;
2980 if (acl_group_override(conn, fsp->fsp_name)) {
2981 int sret;
2983 DEBUG(5,("set_canon_ace_list: acl group "
2984 "control on and current user in file "
2985 "%s primary group.\n",
2986 fsp_str_dbg(fsp)));
2988 become_root();
2989 sret = SMB_VFS_SYS_ACL_SET_FILE(conn,
2990 fsp->fsp_name->base_name, the_acl_type,
2991 the_acl);
2992 unbecome_root();
2993 if (sret == 0) {
2994 ret = True;
2998 if (ret == False) {
2999 DEBUG(2,("set_canon_ace_list: "
3000 "sys_acl_set_file type %s failed for "
3001 "file %s (%s).\n",
3002 the_acl_type == SMB_ACL_TYPE_DEFAULT ?
3003 "directory default" : "file",
3004 fsp_str_dbg(fsp), strerror(errno)));
3005 goto fail;
3008 } else {
3009 if (SMB_VFS_SYS_ACL_SET_FD(fsp, the_acl) == -1) {
3011 * Some systems allow all the above calls and only fail with no ACL support
3012 * when attempting to apply the acl. HPUX with HFS is an example of this. JRA.
3014 if (no_acl_syscall_error(errno)) {
3015 *pacl_set_support = False;
3018 if (acl_group_override(conn, fsp->fsp_name)) {
3019 int sret;
3021 DEBUG(5,("set_canon_ace_list: acl group "
3022 "control on and current user in file "
3023 "%s primary group.\n",
3024 fsp_str_dbg(fsp)));
3026 become_root();
3027 sret = SMB_VFS_SYS_ACL_SET_FD(fsp, the_acl);
3028 unbecome_root();
3029 if (sret == 0) {
3030 ret = True;
3034 if (ret == False) {
3035 DEBUG(2,("set_canon_ace_list: "
3036 "sys_acl_set_file failed for file %s "
3037 "(%s).\n",
3038 fsp_str_dbg(fsp), strerror(errno)));
3039 goto fail;
3044 ret = True;
3046 fail:
3048 if (the_acl != NULL) {
3049 TALLOC_FREE(the_acl);
3052 return ret;
3055 /****************************************************************************
3057 ****************************************************************************/
3059 SMB_ACL_T free_empty_sys_acl(connection_struct *conn, SMB_ACL_T the_acl)
3061 SMB_ACL_ENTRY_T entry;
3063 if (!the_acl)
3064 return NULL;
3065 if (sys_acl_get_entry(the_acl, SMB_ACL_FIRST_ENTRY, &entry) != 1) {
3066 TALLOC_FREE(the_acl);
3067 return NULL;
3069 return the_acl;
3072 /****************************************************************************
3073 Convert a canon_ace to a generic 3 element permission - if possible.
3074 ****************************************************************************/
3076 #define MAP_PERM(p,mask,result) (((p) & (mask)) ? (result) : 0 )
3078 static bool convert_canon_ace_to_posix_perms( files_struct *fsp, canon_ace *file_ace_list, mode_t *posix_perms)
3080 size_t ace_count = count_canon_ace_list(file_ace_list);
3081 canon_ace *ace_p;
3082 canon_ace *owner_ace = NULL;
3083 canon_ace *group_ace = NULL;
3084 canon_ace *other_ace = NULL;
3086 if (ace_count > 5) {
3087 DEBUG(3,("convert_canon_ace_to_posix_perms: Too many ACE "
3088 "entries for file %s to convert to posix perms.\n",
3089 fsp_str_dbg(fsp)));
3090 return False;
3093 for (ace_p = file_ace_list; ace_p; ace_p = ace_p->next) {
3094 if (ace_p->owner_type == UID_ACE)
3095 owner_ace = ace_p;
3096 else if (ace_p->owner_type == GID_ACE)
3097 group_ace = ace_p;
3098 else if (ace_p->owner_type == WORLD_ACE)
3099 other_ace = ace_p;
3102 if (!owner_ace || !group_ace || !other_ace) {
3103 DEBUG(3,("convert_canon_ace_to_posix_perms: Can't get "
3104 "standard entries for file %s.\n", fsp_str_dbg(fsp)));
3105 return False;
3109 * Ensure all ACE entries are owner, group or other.
3110 * We can't set if there are any other SIDs.
3112 for (ace_p = file_ace_list; ace_p; ace_p = ace_p->next) {
3113 if (ace_p == owner_ace || ace_p == group_ace ||
3114 ace_p == other_ace) {
3115 continue;
3117 if (ace_p->owner_type == UID_ACE) {
3118 if (ace_p->unix_ug.id != owner_ace->unix_ug.id) {
3119 DEBUG(3,("Invalid uid %u in ACE for file %s.\n",
3120 (unsigned int)ace_p->unix_ug.id,
3121 fsp_str_dbg(fsp)));
3122 return false;
3124 } else if (ace_p->owner_type == GID_ACE) {
3125 if (ace_p->unix_ug.id != group_ace->unix_ug.id) {
3126 DEBUG(3,("Invalid gid %u in ACE for file %s.\n",
3127 (unsigned int)ace_p->unix_ug.id,
3128 fsp_str_dbg(fsp)));
3129 return false;
3131 } else {
3133 * There should be no duplicate WORLD_ACE entries.
3136 DEBUG(3,("Invalid type %u, uid %u in "
3137 "ACE for file %s.\n",
3138 (unsigned int)ace_p->owner_type,
3139 (unsigned int)ace_p->unix_ug.id,
3140 fsp_str_dbg(fsp)));
3141 return false;
3145 *posix_perms = (mode_t)0;
3147 *posix_perms |= owner_ace->perms;
3148 *posix_perms |= MAP_PERM(group_ace->perms, S_IRUSR, S_IRGRP);
3149 *posix_perms |= MAP_PERM(group_ace->perms, S_IWUSR, S_IWGRP);
3150 *posix_perms |= MAP_PERM(group_ace->perms, S_IXUSR, S_IXGRP);
3151 *posix_perms |= MAP_PERM(other_ace->perms, S_IRUSR, S_IROTH);
3152 *posix_perms |= MAP_PERM(other_ace->perms, S_IWUSR, S_IWOTH);
3153 *posix_perms |= MAP_PERM(other_ace->perms, S_IXUSR, S_IXOTH);
3155 /* The owner must have at least read access. */
3157 *posix_perms |= S_IRUSR;
3158 if (fsp->is_directory)
3159 *posix_perms |= (S_IWUSR|S_IXUSR);
3161 DEBUG(10,("convert_canon_ace_to_posix_perms: converted u=%o,g=%o,w=%o "
3162 "to perm=0%o for file %s.\n", (int)owner_ace->perms,
3163 (int)group_ace->perms, (int)other_ace->perms,
3164 (int)*posix_perms, fsp_str_dbg(fsp)));
3166 return True;
3169 /****************************************************************************
3170 Incoming NT ACLs on a directory can be split into a default POSIX acl (CI|OI|IO) and
3171 a normal POSIX acl. Win2k needs these split acls re-merging into one ACL
3172 with CI|OI set so it is inherited and also applies to the directory.
3173 Based on code from "Jim McDonough" <jmcd@us.ibm.com>.
3174 ****************************************************************************/
3176 static size_t merge_default_aces( struct security_ace *nt_ace_list, size_t num_aces)
3178 size_t i, j;
3180 for (i = 0; i < num_aces; i++) {
3181 for (j = i+1; j < num_aces; j++) {
3182 uint32_t i_flags_ni = (nt_ace_list[i].flags & ~SEC_ACE_FLAG_INHERITED_ACE);
3183 uint32_t j_flags_ni = (nt_ace_list[j].flags & ~SEC_ACE_FLAG_INHERITED_ACE);
3184 bool i_inh = (nt_ace_list[i].flags & SEC_ACE_FLAG_INHERITED_ACE) ? True : False;
3185 bool j_inh = (nt_ace_list[j].flags & SEC_ACE_FLAG_INHERITED_ACE) ? True : False;
3187 /* We know the lower number ACE's are file entries. */
3188 if ((nt_ace_list[i].type == nt_ace_list[j].type) &&
3189 (nt_ace_list[i].size == nt_ace_list[j].size) &&
3190 (nt_ace_list[i].access_mask == nt_ace_list[j].access_mask) &&
3191 dom_sid_equal(&nt_ace_list[i].trustee, &nt_ace_list[j].trustee) &&
3192 (i_inh == j_inh) &&
3193 (i_flags_ni == 0) &&
3194 (j_flags_ni == (SEC_ACE_FLAG_OBJECT_INHERIT|
3195 SEC_ACE_FLAG_CONTAINER_INHERIT|
3196 SEC_ACE_FLAG_INHERIT_ONLY))) {
3198 * W2K wants to have access allowed zero access ACE's
3199 * at the end of the list. If the mask is zero, merge
3200 * the non-inherited ACE onto the inherited ACE.
3203 if (nt_ace_list[i].access_mask == 0) {
3204 nt_ace_list[j].flags = SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT|
3205 (i_inh ? SEC_ACE_FLAG_INHERITED_ACE : 0);
3206 if (num_aces - i - 1 > 0)
3207 memmove(&nt_ace_list[i], &nt_ace_list[i+1], (num_aces-i-1) *
3208 sizeof(struct security_ace));
3210 DEBUG(10,("merge_default_aces: Merging zero access ACE %u onto ACE %u.\n",
3211 (unsigned int)i, (unsigned int)j ));
3212 } else {
3214 * These are identical except for the flags.
3215 * Merge the inherited ACE onto the non-inherited ACE.
3218 nt_ace_list[i].flags = SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT|
3219 (i_inh ? SEC_ACE_FLAG_INHERITED_ACE : 0);
3220 if (num_aces - j - 1 > 0)
3221 memmove(&nt_ace_list[j], &nt_ace_list[j+1], (num_aces-j-1) *
3222 sizeof(struct security_ace));
3224 DEBUG(10,("merge_default_aces: Merging ACE %u onto ACE %u.\n",
3225 (unsigned int)j, (unsigned int)i ));
3227 num_aces--;
3228 break;
3233 return num_aces;
3237 * Add or Replace ACE entry.
3238 * In some cases we need to add a specific ACE for compatibility reasons.
3239 * When doing that we must make sure we are not actually creating a duplicate
3240 * entry. So we need to search whether an ACE entry already exist and eventually
3241 * replacce the access mask, or add a completely new entry if none was found.
3243 * This function assumes the array has enough space to add a new entry without
3244 * any reallocation of memory.
3247 static void add_or_replace_ace(struct security_ace *nt_ace_list, size_t *num_aces,
3248 const struct dom_sid *sid, enum security_ace_type type,
3249 uint32_t mask, uint8_t flags)
3251 int i;
3253 /* first search for a duplicate */
3254 for (i = 0; i < *num_aces; i++) {
3255 if (dom_sid_equal(&nt_ace_list[i].trustee, sid) &&
3256 (nt_ace_list[i].flags == flags)) break;
3259 if (i < *num_aces) { /* found */
3260 nt_ace_list[i].type = type;
3261 nt_ace_list[i].access_mask = mask;
3262 DEBUG(10, ("Replacing ACE %d with SID %s and flags %02x\n",
3263 i, sid_string_dbg(sid), flags));
3264 return;
3267 /* not found, append it */
3268 init_sec_ace(&nt_ace_list[(*num_aces)++], sid, type, mask, flags);
3272 /****************************************************************************
3273 Reply to query a security descriptor from an fsp. If it succeeds it allocates
3274 the space for the return elements and returns the size needed to return the
3275 security descriptor. This should be the only external function needed for
3276 the UNIX style get ACL.
3277 ****************************************************************************/
3279 static NTSTATUS posix_get_nt_acl_common(struct connection_struct *conn,
3280 const char *name,
3281 const SMB_STRUCT_STAT *sbuf,
3282 struct pai_val *pal,
3283 SMB_ACL_T posix_acl,
3284 SMB_ACL_T def_acl,
3285 uint32_t security_info,
3286 TALLOC_CTX *mem_ctx,
3287 struct security_descriptor **ppdesc)
3289 struct dom_sid owner_sid;
3290 struct dom_sid group_sid;
3291 size_t sd_size = 0;
3292 struct security_acl *psa = NULL;
3293 size_t num_acls = 0;
3294 size_t num_def_acls = 0;
3295 size_t num_aces = 0;
3296 canon_ace *file_ace = NULL;
3297 canon_ace *dir_ace = NULL;
3298 struct security_ace *nt_ace_list = NULL;
3299 size_t num_profile_acls = 0;
3300 struct dom_sid orig_owner_sid;
3301 struct security_descriptor *psd = NULL;
3302 int i;
3305 * Get the owner, group and world SIDs.
3308 create_file_sids(sbuf, &owner_sid, &group_sid);
3310 if (lp_profile_acls(SNUM(conn))) {
3311 /* For WXP SP1 the owner must be administrators. */
3312 sid_copy(&orig_owner_sid, &owner_sid);
3313 sid_copy(&owner_sid, &global_sid_Builtin_Administrators);
3314 sid_copy(&group_sid, &global_sid_Builtin_Users);
3315 num_profile_acls = 3;
3318 if (security_info & SECINFO_DACL) {
3321 * In the optimum case Creator Owner and Creator Group would be used for
3322 * the ACL_USER_OBJ and ACL_GROUP_OBJ entries, respectively, but this
3323 * would lead to usability problems under Windows: The Creator entries
3324 * are only available in browse lists of directories and not for files;
3325 * additionally the identity of the owning group couldn't be determined.
3326 * We therefore use those identities only for Default ACLs.
3329 /* Create the canon_ace lists. */
3330 file_ace = canonicalise_acl(conn, name, posix_acl, sbuf,
3331 &owner_sid, &group_sid, pal,
3332 SMB_ACL_TYPE_ACCESS);
3334 /* We must have *some* ACLS. */
3336 if (count_canon_ace_list(file_ace) == 0) {
3337 DEBUG(0,("get_nt_acl : No ACLs on file (%s) !\n", name));
3338 goto done;
3341 if (S_ISDIR(sbuf->st_ex_mode) && def_acl) {
3342 dir_ace = canonicalise_acl(conn, name, def_acl,
3343 sbuf,
3344 &global_sid_Creator_Owner,
3345 &global_sid_Creator_Group,
3346 pal, SMB_ACL_TYPE_DEFAULT);
3350 * Create the NT ACE list from the canonical ace lists.
3354 canon_ace *ace;
3355 enum security_ace_type nt_acl_type;
3357 num_acls = count_canon_ace_list(file_ace);
3358 num_def_acls = count_canon_ace_list(dir_ace);
3360 /* Allocate the ace list. */
3361 if ((nt_ace_list = talloc_array(talloc_tos(), struct security_ace,num_acls + num_profile_acls + num_def_acls)) == NULL) {
3362 DEBUG(0,("get_nt_acl: Unable to malloc space for nt_ace_list.\n"));
3363 goto done;
3366 memset(nt_ace_list, '\0', (num_acls + num_def_acls) * sizeof(struct security_ace) );
3369 * Create the NT ACE list from the canonical ace lists.
3372 for (ace = file_ace; ace != NULL; ace = ace->next) {
3373 uint32_t acc = map_canon_ace_perms(SNUM(conn),
3374 &nt_acl_type,
3375 ace->perms,
3376 S_ISDIR(sbuf->st_ex_mode));
3377 init_sec_ace(&nt_ace_list[num_aces++],
3378 &ace->trustee,
3379 nt_acl_type,
3380 acc,
3381 ace->ace_flags);
3384 /* The User must have access to a profile share - even
3385 * if we can't map the SID. */
3386 if (lp_profile_acls(SNUM(conn))) {
3387 add_or_replace_ace(nt_ace_list, &num_aces,
3388 &global_sid_Builtin_Users,
3389 SEC_ACE_TYPE_ACCESS_ALLOWED,
3390 FILE_GENERIC_ALL, 0);
3393 for (ace = dir_ace; ace != NULL; ace = ace->next) {
3394 uint32_t acc = map_canon_ace_perms(SNUM(conn),
3395 &nt_acl_type,
3396 ace->perms,
3397 S_ISDIR(sbuf->st_ex_mode));
3398 init_sec_ace(&nt_ace_list[num_aces++],
3399 &ace->trustee,
3400 nt_acl_type,
3401 acc,
3402 ace->ace_flags |
3403 SEC_ACE_FLAG_OBJECT_INHERIT|
3404 SEC_ACE_FLAG_CONTAINER_INHERIT|
3405 SEC_ACE_FLAG_INHERIT_ONLY);
3408 /* The User must have access to a profile share - even
3409 * if we can't map the SID. */
3410 if (lp_profile_acls(SNUM(conn))) {
3411 add_or_replace_ace(nt_ace_list, &num_aces,
3412 &global_sid_Builtin_Users,
3413 SEC_ACE_TYPE_ACCESS_ALLOWED,
3414 FILE_GENERIC_ALL,
3415 SEC_ACE_FLAG_OBJECT_INHERIT |
3416 SEC_ACE_FLAG_CONTAINER_INHERIT |
3417 SEC_ACE_FLAG_INHERIT_ONLY);
3421 * Merge POSIX default ACLs and normal ACLs into one NT ACE.
3422 * Win2K needs this to get the inheritance correct when replacing ACLs
3423 * on a directory tree. Based on work by Jim @ IBM.
3426 num_aces = merge_default_aces(nt_ace_list, num_aces);
3428 if (lp_profile_acls(SNUM(conn))) {
3429 for (i = 0; i < num_aces; i++) {
3430 if (dom_sid_equal(&nt_ace_list[i].trustee, &owner_sid)) {
3431 add_or_replace_ace(nt_ace_list, &num_aces,
3432 &orig_owner_sid,
3433 nt_ace_list[i].type,
3434 nt_ace_list[i].access_mask,
3435 nt_ace_list[i].flags);
3436 break;
3442 if (num_aces) {
3443 if((psa = make_sec_acl( talloc_tos(), NT4_ACL_REVISION, num_aces, nt_ace_list)) == NULL) {
3444 DEBUG(0,("get_nt_acl: Unable to malloc space for acl.\n"));
3445 goto done;
3448 } /* security_info & SECINFO_DACL */
3450 psd = make_standard_sec_desc(mem_ctx,
3451 (security_info & SECINFO_OWNER) ? &owner_sid : NULL,
3452 (security_info & SECINFO_GROUP) ? &group_sid : NULL,
3453 psa,
3454 &sd_size);
3456 if(!psd) {
3457 DEBUG(0,("get_nt_acl: Unable to malloc space for security descriptor.\n"));
3458 sd_size = 0;
3459 goto done;
3463 * Windows 2000: The DACL_PROTECTED flag in the security
3464 * descriptor marks the ACL as non-inheriting, i.e., no
3465 * ACEs from higher level directories propagate to this
3466 * ACL. In the POSIX ACL model permissions are only
3467 * inherited at file create time, so ACLs never contain
3468 * any ACEs that are inherited dynamically. The DACL_PROTECTED
3469 * flag doesn't seem to bother Windows NT.
3470 * Always set this if map acl inherit is turned off.
3472 if (pal == NULL || !lp_map_acl_inherit(SNUM(conn))) {
3473 psd->type |= SEC_DESC_DACL_PROTECTED;
3474 } else {
3475 psd->type |= pal->sd_type;
3478 if (psd->dacl) {
3479 dacl_sort_into_canonical_order(psd->dacl->aces, (unsigned int)psd->dacl->num_aces);
3482 *ppdesc = psd;
3484 done:
3486 if (posix_acl) {
3487 TALLOC_FREE(posix_acl);
3489 if (def_acl) {
3490 TALLOC_FREE(def_acl);
3492 free_canon_ace_list(file_ace);
3493 free_canon_ace_list(dir_ace);
3494 free_inherited_info(pal);
3495 TALLOC_FREE(nt_ace_list);
3497 return NT_STATUS_OK;
3500 NTSTATUS posix_fget_nt_acl(struct files_struct *fsp, uint32_t security_info,
3501 TALLOC_CTX *mem_ctx,
3502 struct security_descriptor **ppdesc)
3504 SMB_STRUCT_STAT sbuf;
3505 SMB_ACL_T posix_acl = NULL;
3506 struct pai_val *pal;
3507 TALLOC_CTX *frame = talloc_stackframe();
3508 NTSTATUS status;
3510 *ppdesc = NULL;
3512 DEBUG(10,("posix_fget_nt_acl: called for file %s\n",
3513 fsp_str_dbg(fsp)));
3515 /* can it happen that fsp_name == NULL ? */
3516 if (fsp->is_directory || fsp->fh->fd == -1) {
3517 status = posix_get_nt_acl(fsp->conn, fsp->fsp_name,
3518 security_info, mem_ctx, ppdesc);
3519 TALLOC_FREE(frame);
3520 return status;
3523 /* Get the stat struct for the owner info. */
3524 if(SMB_VFS_FSTAT(fsp, &sbuf) != 0) {
3525 TALLOC_FREE(frame);
3526 return map_nt_error_from_unix(errno);
3529 /* Get the ACL from the fd. */
3530 posix_acl = SMB_VFS_SYS_ACL_GET_FD(fsp, frame);
3532 pal = fload_inherited_info(fsp);
3534 status = posix_get_nt_acl_common(fsp->conn, fsp->fsp_name->base_name,
3535 &sbuf, pal, posix_acl, NULL,
3536 security_info, mem_ctx, ppdesc);
3537 TALLOC_FREE(frame);
3538 return status;
3541 NTSTATUS posix_get_nt_acl(struct connection_struct *conn,
3542 const struct smb_filename *smb_fname_in,
3543 uint32_t security_info,
3544 TALLOC_CTX *mem_ctx,
3545 struct security_descriptor **ppdesc)
3547 SMB_ACL_T posix_acl = NULL;
3548 SMB_ACL_T def_acl = NULL;
3549 struct pai_val *pal;
3550 struct smb_filename *smb_fname = NULL;
3551 int ret;
3552 TALLOC_CTX *frame = talloc_stackframe();
3553 NTSTATUS status;
3555 *ppdesc = NULL;
3557 DEBUG(10,("posix_get_nt_acl: called for file %s\n",
3558 smb_fname_in->base_name ));
3560 smb_fname = cp_smb_filename(talloc_tos(), smb_fname_in);
3561 if (smb_fname == NULL) {
3562 TALLOC_FREE(frame);
3563 return NT_STATUS_NO_MEMORY;
3566 /* Get the stat struct for the owner info. */
3568 * We can directly use SMB_VFS_STAT here, as if this was a
3569 * POSIX call on a symlink, we've already refused it.
3570 * For a Windows acl mapped call on a symlink, we want to follow
3571 * it.
3573 ret = SMB_VFS_STAT(conn, smb_fname);
3575 if (ret == -1) {
3576 TALLOC_FREE(frame);
3577 return map_nt_error_from_unix(errno);
3580 /* Get the ACL from the path. */
3581 posix_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, smb_fname->base_name,
3582 SMB_ACL_TYPE_ACCESS, frame);
3584 /* If it's a directory get the default POSIX ACL. */
3585 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
3586 def_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, smb_fname->base_name,
3587 SMB_ACL_TYPE_DEFAULT, frame);
3588 def_acl = free_empty_sys_acl(conn, def_acl);
3591 pal = load_inherited_info(conn, smb_fname->base_name);
3593 status = posix_get_nt_acl_common(conn,
3594 smb_fname->base_name,
3595 &smb_fname->st,
3596 pal,
3597 posix_acl,
3598 def_acl,
3599 security_info,
3600 mem_ctx,
3601 ppdesc);
3602 TALLOC_FREE(frame);
3603 return status;
3606 /****************************************************************************
3607 Try to chown a file. We will be able to chown it under the following conditions.
3609 1) If we have root privileges, then it will just work.
3610 2) If we have SeRestorePrivilege we can change the user + group to any other user.
3611 3) If we have SeTakeOwnershipPrivilege we can change the user to the current user.
3612 4) If we have write permission to the file and dos_filemodes is set
3613 then allow chown to the currently authenticated user.
3614 ****************************************************************************/
3616 NTSTATUS try_chown(files_struct *fsp, uid_t uid, gid_t gid)
3618 NTSTATUS status;
3620 if(!CAN_WRITE(fsp->conn)) {
3621 return NT_STATUS_MEDIA_WRITE_PROTECTED;
3624 /* Case (1). */
3625 status = vfs_chown_fsp(fsp, uid, gid);
3626 if (NT_STATUS_IS_OK(status)) {
3627 return status;
3630 /* Case (2) / (3) */
3631 if (lp_enable_privileges()) {
3632 bool has_take_ownership_priv = security_token_has_privilege(
3633 get_current_nttok(fsp->conn),
3634 SEC_PRIV_TAKE_OWNERSHIP);
3635 bool has_restore_priv = security_token_has_privilege(
3636 get_current_nttok(fsp->conn),
3637 SEC_PRIV_RESTORE);
3639 if (has_restore_priv) {
3640 ; /* Case (2) */
3641 } else if (has_take_ownership_priv) {
3642 /* Case (3) */
3643 if (uid == get_current_uid(fsp->conn)) {
3644 gid = (gid_t)-1;
3645 } else {
3646 has_take_ownership_priv = false;
3650 if (has_take_ownership_priv || has_restore_priv) {
3651 become_root();
3652 status = vfs_chown_fsp(fsp, uid, gid);
3653 unbecome_root();
3654 return status;
3658 /* Case (4). */
3659 if (!lp_dos_filemode(SNUM(fsp->conn))) {
3660 return NT_STATUS_ACCESS_DENIED;
3663 /* only allow chown to the current user. This is more secure,
3664 and also copes with the case where the SID in a take ownership ACL is
3665 a local SID on the users workstation
3667 if (uid != get_current_uid(fsp->conn)) {
3668 return NT_STATUS_INVALID_OWNER;
3671 become_root();
3672 /* Keep the current file gid the same. */
3673 status = vfs_chown_fsp(fsp, uid, (gid_t)-1);
3674 unbecome_root();
3676 return status;
3679 /****************************************************************************
3680 Reply to set a security descriptor on an fsp. security_info_sent is the
3681 description of the following NT ACL.
3682 This should be the only external function needed for the UNIX style set ACL.
3683 We make a copy of psd_orig as internal functions modify the elements inside
3684 it, even though it's a const pointer.
3685 ****************************************************************************/
3687 NTSTATUS set_nt_acl(files_struct *fsp, uint32_t security_info_sent, const struct security_descriptor *psd_orig)
3689 connection_struct *conn = fsp->conn;
3690 uid_t user = (uid_t)-1;
3691 gid_t grp = (gid_t)-1;
3692 struct dom_sid file_owner_sid;
3693 struct dom_sid file_grp_sid;
3694 canon_ace *file_ace_list = NULL;
3695 canon_ace *dir_ace_list = NULL;
3696 bool acl_perms = False;
3697 mode_t orig_mode = (mode_t)0;
3698 NTSTATUS status;
3699 bool set_acl_as_root = false;
3700 bool acl_set_support = false;
3701 bool ret = false;
3702 struct security_descriptor *psd = NULL;
3704 DEBUG(10,("set_nt_acl: called for file %s\n",
3705 fsp_str_dbg(fsp)));
3707 if (!CAN_WRITE(conn)) {
3708 DEBUG(10,("set acl rejected on read-only share\n"));
3709 return NT_STATUS_MEDIA_WRITE_PROTECTED;
3712 if (psd_orig == NULL) {
3713 return NT_STATUS_INVALID_PARAMETER;
3717 * MS NFS mode, here's the deal: the client merely wants to
3718 * modify the mode, but roundtripping get_acl/set/acl would
3719 * add additional POSIX ACEs. So in case we get a request
3720 * containing a MS NFS mode SID, we do nothing here.
3722 if (security_descriptor_with_ms_nfs(psd_orig)) {
3723 return NT_STATUS_OK;
3726 psd = security_descriptor_copy(talloc_tos(), psd_orig);
3727 if (psd == NULL) {
3728 return NT_STATUS_NO_MEMORY;
3732 * Get the current state of the file.
3735 status = vfs_stat_fsp(fsp);
3736 if (!NT_STATUS_IS_OK(status)) {
3737 return status;
3740 /* Save the original element we check against. */
3741 orig_mode = fsp->fsp_name->st.st_ex_mode;
3744 * Unpack the user/group/world id's.
3747 /* POSIX can't cope with missing owner/group. */
3748 if ((security_info_sent & SECINFO_OWNER) && (psd->owner_sid == NULL)) {
3749 security_info_sent &= ~SECINFO_OWNER;
3751 if ((security_info_sent & SECINFO_GROUP) && (psd->group_sid == NULL)) {
3752 security_info_sent &= ~SECINFO_GROUP;
3755 /* If UNIX owner is inherited and Windows isn't, then
3756 * setting the UNIX owner based on Windows owner conflicts
3757 * with the inheritance rule
3759 if (lp_inherit_owner(SNUM(conn)) == INHERIT_OWNER_UNIX_ONLY) {
3760 security_info_sent &= ~SECINFO_OWNER;
3763 status = unpack_nt_owners( conn, &user, &grp, security_info_sent, psd);
3764 if (!NT_STATUS_IS_OK(status)) {
3765 return status;
3769 * Do we need to chown ? If so this must be done first as the incoming
3770 * CREATOR_OWNER acl will be relative to the *new* owner, not the old.
3771 * Noticed by Simo.
3774 if (((user != (uid_t)-1) && (fsp->fsp_name->st.st_ex_uid != user)) ||
3775 (( grp != (gid_t)-1) && (fsp->fsp_name->st.st_ex_gid != grp))) {
3777 DEBUG(3,("set_nt_acl: chown %s. uid = %u, gid = %u.\n",
3778 fsp_str_dbg(fsp), (unsigned int)user,
3779 (unsigned int)grp));
3781 status = try_chown(fsp, user, grp);
3782 if(!NT_STATUS_IS_OK(status)) {
3783 DEBUG(3,("set_nt_acl: chown %s, %u, %u failed. Error "
3784 "= %s.\n", fsp_str_dbg(fsp),
3785 (unsigned int)user,
3786 (unsigned int)grp,
3787 nt_errstr(status)));
3788 return status;
3792 * Recheck the current state of the file, which may have changed.
3793 * (suid/sgid bits, for instance)
3796 status = vfs_stat_fsp(fsp);
3797 if (!NT_STATUS_IS_OK(status)) {
3798 return status;
3801 /* Save the original element we check against. */
3802 orig_mode = fsp->fsp_name->st.st_ex_mode;
3804 /* If we successfully chowned, we know we must
3805 * be able to set the acl, so do it as root.
3807 set_acl_as_root = true;
3810 create_file_sids(&fsp->fsp_name->st, &file_owner_sid, &file_grp_sid);
3812 if((security_info_sent & SECINFO_DACL) &&
3813 (psd->type & SEC_DESC_DACL_PRESENT) &&
3814 (psd->dacl == NULL)) {
3815 struct security_ace ace[3];
3817 /* We can't have NULL DACL in POSIX.
3818 Use owner/group/Everyone -> full access. */
3820 init_sec_ace(&ace[0],
3821 &file_owner_sid,
3822 SEC_ACE_TYPE_ACCESS_ALLOWED,
3823 GENERIC_ALL_ACCESS,
3825 init_sec_ace(&ace[1],
3826 &file_grp_sid,
3827 SEC_ACE_TYPE_ACCESS_ALLOWED,
3828 GENERIC_ALL_ACCESS,
3830 init_sec_ace(&ace[2],
3831 &global_sid_World,
3832 SEC_ACE_TYPE_ACCESS_ALLOWED,
3833 GENERIC_ALL_ACCESS,
3835 psd->dacl = make_sec_acl(talloc_tos(),
3836 NT4_ACL_REVISION,
3838 ace);
3839 if (psd->dacl == NULL) {
3840 return NT_STATUS_NO_MEMORY;
3842 security_acl_map_generic(psd->dacl, &file_generic_mapping);
3845 acl_perms = unpack_canon_ace(fsp, &fsp->fsp_name->st, &file_owner_sid,
3846 &file_grp_sid, &file_ace_list,
3847 &dir_ace_list, security_info_sent, psd);
3849 /* Ignore W2K traverse DACL set. */
3850 if (!file_ace_list && !dir_ace_list) {
3851 return NT_STATUS_OK;
3854 if (!acl_perms) {
3855 DEBUG(3,("set_nt_acl: cannot set permissions\n"));
3856 free_canon_ace_list(file_ace_list);
3857 free_canon_ace_list(dir_ace_list);
3858 return NT_STATUS_ACCESS_DENIED;
3862 * Only change security if we got a DACL.
3865 if(!(security_info_sent & SECINFO_DACL) || (psd->dacl == NULL)) {
3866 free_canon_ace_list(file_ace_list);
3867 free_canon_ace_list(dir_ace_list);
3868 return NT_STATUS_OK;
3872 * Try using the POSIX ACL set first. Fall back to chmod if
3873 * we have no ACL support on this filesystem.
3876 if (acl_perms && file_ace_list) {
3877 if (set_acl_as_root) {
3878 become_root();
3880 ret = set_canon_ace_list(fsp, file_ace_list, false,
3881 &fsp->fsp_name->st, &acl_set_support);
3882 if (set_acl_as_root) {
3883 unbecome_root();
3885 if (acl_set_support && ret == false) {
3886 DEBUG(3,("set_nt_acl: failed to set file acl on file "
3887 "%s (%s).\n", fsp_str_dbg(fsp),
3888 strerror(errno)));
3889 free_canon_ace_list(file_ace_list);
3890 free_canon_ace_list(dir_ace_list);
3891 return map_nt_error_from_unix(errno);
3895 if (acl_perms && acl_set_support && fsp->is_directory) {
3896 if (dir_ace_list) {
3897 if (set_acl_as_root) {
3898 become_root();
3900 ret = set_canon_ace_list(fsp, dir_ace_list, true,
3901 &fsp->fsp_name->st,
3902 &acl_set_support);
3903 if (set_acl_as_root) {
3904 unbecome_root();
3906 if (ret == false) {
3907 DEBUG(3,("set_nt_acl: failed to set default "
3908 "acl on directory %s (%s).\n",
3909 fsp_str_dbg(fsp), strerror(errno)));
3910 free_canon_ace_list(file_ace_list);
3911 free_canon_ace_list(dir_ace_list);
3912 return map_nt_error_from_unix(errno);
3914 } else {
3915 int sret = -1;
3918 * No default ACL - delete one if it exists.
3921 if (set_acl_as_root) {
3922 become_root();
3924 sret = SMB_VFS_SYS_ACL_DELETE_DEF_FILE(conn,
3925 fsp->fsp_name->base_name);
3926 if (set_acl_as_root) {
3927 unbecome_root();
3929 if (sret == -1) {
3930 if (acl_group_override(conn, fsp->fsp_name)) {
3931 DEBUG(5,("set_nt_acl: acl group "
3932 "control on and current user "
3933 "in file %s primary group. "
3934 "Override delete_def_acl\n",
3935 fsp_str_dbg(fsp)));
3937 become_root();
3938 sret =
3939 SMB_VFS_SYS_ACL_DELETE_DEF_FILE(
3940 conn,
3941 fsp->fsp_name->base_name);
3942 unbecome_root();
3945 if (sret == -1) {
3946 DEBUG(3,("set_nt_acl: sys_acl_delete_def_file failed (%s)\n", strerror(errno)));
3947 free_canon_ace_list(file_ace_list);
3948 free_canon_ace_list(dir_ace_list);
3949 return map_nt_error_from_unix(errno);
3955 if (acl_set_support) {
3956 if (set_acl_as_root) {
3957 become_root();
3959 store_inheritance_attributes(fsp,
3960 file_ace_list,
3961 dir_ace_list,
3962 psd->type);
3963 if (set_acl_as_root) {
3964 unbecome_root();
3969 * If we cannot set using POSIX ACLs we fall back to checking if we need to chmod.
3972 if(!acl_set_support && acl_perms) {
3973 mode_t posix_perms;
3975 if (!convert_canon_ace_to_posix_perms( fsp, file_ace_list, &posix_perms)) {
3976 free_canon_ace_list(file_ace_list);
3977 free_canon_ace_list(dir_ace_list);
3978 DEBUG(3,("set_nt_acl: failed to convert file acl to "
3979 "posix permissions for file %s.\n",
3980 fsp_str_dbg(fsp)));
3981 return NT_STATUS_ACCESS_DENIED;
3984 if (orig_mode != posix_perms) {
3985 int sret = -1;
3987 DEBUG(3,("set_nt_acl: chmod %s. perms = 0%o.\n",
3988 fsp_str_dbg(fsp), (unsigned int)posix_perms));
3990 if (set_acl_as_root) {
3991 become_root();
3993 sret = SMB_VFS_CHMOD(conn, fsp->fsp_name,
3994 posix_perms);
3995 if (set_acl_as_root) {
3996 unbecome_root();
3998 if(sret == -1) {
3999 if (acl_group_override(conn, fsp->fsp_name)) {
4000 DEBUG(5,("set_nt_acl: acl group "
4001 "control on and current user "
4002 "in file %s primary group. "
4003 "Override chmod\n",
4004 fsp_str_dbg(fsp)));
4006 become_root();
4007 sret = SMB_VFS_CHMOD(conn,
4008 fsp->fsp_name,
4009 posix_perms);
4010 unbecome_root();
4013 if (sret == -1) {
4014 DEBUG(3,("set_nt_acl: chmod %s, 0%o "
4015 "failed. Error = %s.\n",
4016 fsp_str_dbg(fsp),
4017 (unsigned int)posix_perms,
4018 strerror(errno)));
4019 free_canon_ace_list(file_ace_list);
4020 free_canon_ace_list(dir_ace_list);
4021 return map_nt_error_from_unix(errno);
4027 free_canon_ace_list(file_ace_list);
4028 free_canon_ace_list(dir_ace_list);
4030 /* Ensure the stat struct in the fsp is correct. */
4031 status = vfs_stat_fsp(fsp);
4033 return NT_STATUS_OK;
4036 /****************************************************************************
4037 Get the actual group bits stored on a file with an ACL. Has no effect if
4038 the file has no ACL. Needed in dosmode code where the stat() will return
4039 the mask bits, not the real group bits, for a file with an ACL.
4040 ****************************************************************************/
4042 int get_acl_group_bits( connection_struct *conn, const char *fname, mode_t *mode )
4044 int entry_id = SMB_ACL_FIRST_ENTRY;
4045 SMB_ACL_ENTRY_T entry;
4046 SMB_ACL_T posix_acl;
4047 int result = -1;
4049 posix_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, fname,
4050 SMB_ACL_TYPE_ACCESS, talloc_tos());
4051 if (posix_acl == (SMB_ACL_T)NULL)
4052 return -1;
4054 while (sys_acl_get_entry(posix_acl, entry_id, &entry) == 1) {
4055 SMB_ACL_TAG_T tagtype;
4056 SMB_ACL_PERMSET_T permset;
4058 entry_id = SMB_ACL_NEXT_ENTRY;
4060 if (sys_acl_get_tag_type(entry, &tagtype) ==-1)
4061 break;
4063 if (tagtype == SMB_ACL_GROUP_OBJ) {
4064 if (sys_acl_get_permset(entry, &permset) == -1) {
4065 break;
4066 } else {
4067 *mode &= ~(S_IRGRP|S_IWGRP|S_IXGRP);
4068 *mode |= (sys_acl_get_perm(permset, SMB_ACL_READ) ? S_IRGRP : 0);
4069 *mode |= (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? S_IWGRP : 0);
4070 *mode |= (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? S_IXGRP : 0);
4071 result = 0;
4072 break;
4076 TALLOC_FREE(posix_acl);
4077 return result;
4080 /****************************************************************************
4081 Do a chmod by setting the ACL USER_OBJ, GROUP_OBJ and OTHER bits in an ACL
4082 and set the mask to rwx. Needed to preserve complex ACLs set by NT.
4083 ****************************************************************************/
4085 static int chmod_acl_internals( connection_struct *conn, SMB_ACL_T posix_acl, mode_t mode)
4087 int entry_id = SMB_ACL_FIRST_ENTRY;
4088 SMB_ACL_ENTRY_T entry;
4089 int num_entries = 0;
4091 while ( sys_acl_get_entry(posix_acl, entry_id, &entry) == 1) {
4092 SMB_ACL_TAG_T tagtype;
4093 SMB_ACL_PERMSET_T permset;
4094 mode_t perms;
4096 entry_id = SMB_ACL_NEXT_ENTRY;
4098 if (sys_acl_get_tag_type(entry, &tagtype) == -1)
4099 return -1;
4101 if (sys_acl_get_permset(entry, &permset) == -1)
4102 return -1;
4104 num_entries++;
4106 switch(tagtype) {
4107 case SMB_ACL_USER_OBJ:
4108 perms = unix_perms_to_acl_perms(mode, S_IRUSR, S_IWUSR, S_IXUSR);
4109 break;
4110 case SMB_ACL_GROUP_OBJ:
4111 perms = unix_perms_to_acl_perms(mode, S_IRGRP, S_IWGRP, S_IXGRP);
4112 break;
4113 case SMB_ACL_MASK:
4115 * FIXME: The ACL_MASK entry permissions should really be set to
4116 * the union of the permissions of all ACL_USER,
4117 * ACL_GROUP_OBJ, and ACL_GROUP entries. That's what
4118 * acl_calc_mask() does, but Samba ACLs doesn't provide it.
4120 perms = S_IRUSR|S_IWUSR|S_IXUSR;
4121 break;
4122 case SMB_ACL_OTHER:
4123 perms = unix_perms_to_acl_perms(mode, S_IROTH, S_IWOTH, S_IXOTH);
4124 break;
4125 default:
4126 continue;
4129 if (map_acl_perms_to_permset(conn, perms, &permset) == -1)
4130 return -1;
4132 if (sys_acl_set_permset(entry, permset) == -1)
4133 return -1;
4137 * If this is a simple 3 element ACL or no elements then it's a standard
4138 * UNIX permission set. Just use chmod...
4141 if ((num_entries == 3) || (num_entries == 0))
4142 return -1;
4144 return 0;
4147 /****************************************************************************
4148 Get the access ACL of FROM, do a chmod by setting the ACL USER_OBJ,
4149 GROUP_OBJ and OTHER bits in an ACL and set the mask to rwx. Set the
4150 resulting ACL on TO. Note that name is in UNIX character set.
4151 ****************************************************************************/
4153 static int copy_access_posix_acl(connection_struct *conn, const char *from, const char *to, mode_t mode)
4155 SMB_ACL_T posix_acl = NULL;
4156 int ret = -1;
4158 if ((posix_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, from,
4159 SMB_ACL_TYPE_ACCESS,
4160 talloc_tos())) == NULL)
4161 return -1;
4163 if ((ret = chmod_acl_internals(conn, posix_acl, mode)) == -1)
4164 goto done;
4166 ret = SMB_VFS_SYS_ACL_SET_FILE(conn, to, SMB_ACL_TYPE_ACCESS, posix_acl);
4168 done:
4170 TALLOC_FREE(posix_acl);
4171 return ret;
4174 /****************************************************************************
4175 Do a chmod by setting the ACL USER_OBJ, GROUP_OBJ and OTHER bits in an ACL
4176 and set the mask to rwx. Needed to preserve complex ACLs set by NT.
4177 Note that name is in UNIX character set.
4178 ****************************************************************************/
4180 int chmod_acl(connection_struct *conn, const char *name, mode_t mode)
4182 return copy_access_posix_acl(conn, name, name, mode);
4185 /****************************************************************************
4186 Check for an existing default POSIX ACL on a directory.
4187 ****************************************************************************/
4189 static bool directory_has_default_posix_acl(connection_struct *conn, const char *fname)
4191 SMB_ACL_T def_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, fname,
4192 SMB_ACL_TYPE_DEFAULT,
4193 talloc_tos());
4194 bool has_acl = False;
4195 SMB_ACL_ENTRY_T entry;
4197 if (def_acl != NULL && (sys_acl_get_entry(def_acl, SMB_ACL_FIRST_ENTRY, &entry) == 1)) {
4198 has_acl = True;
4201 if (def_acl) {
4202 TALLOC_FREE(def_acl);
4204 return has_acl;
4207 /****************************************************************************
4208 If the parent directory has no default ACL but it does have an Access ACL,
4209 inherit this Access ACL to file name.
4210 ****************************************************************************/
4212 int inherit_access_posix_acl(connection_struct *conn, const char *inherit_from_dir,
4213 const char *name, mode_t mode)
4215 if (directory_has_default_posix_acl(conn, inherit_from_dir))
4216 return 0;
4218 return copy_access_posix_acl(conn, inherit_from_dir, name, mode);
4221 /****************************************************************************
4222 Do an fchmod by setting the ACL USER_OBJ, GROUP_OBJ and OTHER bits in an ACL
4223 and set the mask to rwx. Needed to preserve complex ACLs set by NT.
4224 ****************************************************************************/
4226 int fchmod_acl(files_struct *fsp, mode_t mode)
4228 connection_struct *conn = fsp->conn;
4229 SMB_ACL_T posix_acl = NULL;
4230 int ret = -1;
4232 if ((posix_acl = SMB_VFS_SYS_ACL_GET_FD(fsp, talloc_tos())) == NULL)
4233 return -1;
4235 if ((ret = chmod_acl_internals(conn, posix_acl, mode)) == -1)
4236 goto done;
4238 ret = SMB_VFS_SYS_ACL_SET_FD(fsp, posix_acl);
4240 done:
4242 TALLOC_FREE(posix_acl);
4243 return ret;
4246 /****************************************************************************
4247 Map from wire type to permset.
4248 ****************************************************************************/
4250 static bool unix_ex_wire_to_permset(connection_struct *conn, unsigned char wire_perm, SMB_ACL_PERMSET_T *p_permset)
4252 if (wire_perm & ~(SMB_POSIX_ACL_READ|SMB_POSIX_ACL_WRITE|SMB_POSIX_ACL_EXECUTE)) {
4253 return False;
4256 if (sys_acl_clear_perms(*p_permset) == -1) {
4257 return False;
4260 if (wire_perm & SMB_POSIX_ACL_READ) {
4261 if (sys_acl_add_perm(*p_permset, SMB_ACL_READ) == -1) {
4262 return False;
4265 if (wire_perm & SMB_POSIX_ACL_WRITE) {
4266 if (sys_acl_add_perm(*p_permset, SMB_ACL_WRITE) == -1) {
4267 return False;
4270 if (wire_perm & SMB_POSIX_ACL_EXECUTE) {
4271 if (sys_acl_add_perm(*p_permset, SMB_ACL_EXECUTE) == -1) {
4272 return False;
4275 return True;
4278 /****************************************************************************
4279 Map from wire type to tagtype.
4280 ****************************************************************************/
4282 static bool unix_ex_wire_to_tagtype(unsigned char wire_tt, SMB_ACL_TAG_T *p_tt)
4284 switch (wire_tt) {
4285 case SMB_POSIX_ACL_USER_OBJ:
4286 *p_tt = SMB_ACL_USER_OBJ;
4287 break;
4288 case SMB_POSIX_ACL_USER:
4289 *p_tt = SMB_ACL_USER;
4290 break;
4291 case SMB_POSIX_ACL_GROUP_OBJ:
4292 *p_tt = SMB_ACL_GROUP_OBJ;
4293 break;
4294 case SMB_POSIX_ACL_GROUP:
4295 *p_tt = SMB_ACL_GROUP;
4296 break;
4297 case SMB_POSIX_ACL_MASK:
4298 *p_tt = SMB_ACL_MASK;
4299 break;
4300 case SMB_POSIX_ACL_OTHER:
4301 *p_tt = SMB_ACL_OTHER;
4302 break;
4303 default:
4304 return False;
4306 return True;
4309 /****************************************************************************
4310 Create a new POSIX acl from wire permissions.
4311 FIXME ! How does the share mask/mode fit into this.... ?
4312 ****************************************************************************/
4314 static SMB_ACL_T create_posix_acl_from_wire(connection_struct *conn,
4315 uint16_t num_acls,
4316 const char *pdata,
4317 TALLOC_CTX *mem_ctx)
4319 unsigned int i;
4320 SMB_ACL_T the_acl = sys_acl_init(mem_ctx);
4322 if (the_acl == NULL) {
4323 return NULL;
4326 for (i = 0; i < num_acls; i++) {
4327 SMB_ACL_ENTRY_T the_entry;
4328 SMB_ACL_PERMSET_T the_permset;
4329 SMB_ACL_TAG_T tag_type;
4331 if (sys_acl_create_entry(&the_acl, &the_entry) == -1) {
4332 DEBUG(0,("create_posix_acl_from_wire: Failed to create entry %u. (%s)\n",
4333 i, strerror(errno) ));
4334 goto fail;
4337 if (!unix_ex_wire_to_tagtype(CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)), &tag_type)) {
4338 DEBUG(0,("create_posix_acl_from_wire: invalid wire tagtype %u on entry %u.\n",
4339 CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)), i ));
4340 goto fail;
4343 if (sys_acl_set_tag_type(the_entry, tag_type) == -1) {
4344 DEBUG(0,("create_posix_acl_from_wire: Failed to set tagtype on entry %u. (%s)\n",
4345 i, strerror(errno) ));
4346 goto fail;
4349 /* Get the permset pointer from the new ACL entry. */
4350 if (sys_acl_get_permset(the_entry, &the_permset) == -1) {
4351 DEBUG(0,("create_posix_acl_from_wire: Failed to get permset on entry %u. (%s)\n",
4352 i, strerror(errno) ));
4353 goto fail;
4356 /* Map from wire to permissions. */
4357 if (!unix_ex_wire_to_permset(conn, CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)+1), &the_permset)) {
4358 DEBUG(0,("create_posix_acl_from_wire: invalid permset %u on entry %u.\n",
4359 CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE) + 1), i ));
4360 goto fail;
4363 /* Now apply to the new ACL entry. */
4364 if (sys_acl_set_permset(the_entry, the_permset) == -1) {
4365 DEBUG(0,("create_posix_acl_from_wire: Failed to add permset on entry %u. (%s)\n",
4366 i, strerror(errno) ));
4367 goto fail;
4370 if (tag_type == SMB_ACL_USER) {
4371 uint32_t uidval = IVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
4372 uid_t uid = (uid_t)uidval;
4373 if (sys_acl_set_qualifier(the_entry,(void *)&uid) == -1) {
4374 DEBUG(0,("create_posix_acl_from_wire: Failed to set uid %u on entry %u. (%s)\n",
4375 (unsigned int)uid, i, strerror(errno) ));
4376 goto fail;
4380 if (tag_type == SMB_ACL_GROUP) {
4381 uint32_t gidval = IVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
4382 gid_t gid = (uid_t)gidval;
4383 if (sys_acl_set_qualifier(the_entry,(void *)&gid) == -1) {
4384 DEBUG(0,("create_posix_acl_from_wire: Failed to set gid %u on entry %u. (%s)\n",
4385 (unsigned int)gid, i, strerror(errno) ));
4386 goto fail;
4391 return the_acl;
4393 fail:
4395 if (the_acl != NULL) {
4396 TALLOC_FREE(the_acl);
4398 return NULL;
4401 /****************************************************************************
4402 Calls from UNIX extensions - Default POSIX ACL set.
4403 If num_def_acls == 0 and not a directory just return. If it is a directory
4404 and num_def_acls == 0 then remove the default acl. Else set the default acl
4405 on the directory.
4406 ****************************************************************************/
4408 bool set_unix_posix_default_acl(connection_struct *conn, const char *fname, const SMB_STRUCT_STAT *psbuf,
4409 uint16_t num_def_acls, const char *pdata)
4411 SMB_ACL_T def_acl = NULL;
4413 if (!S_ISDIR(psbuf->st_ex_mode)) {
4414 if (num_def_acls) {
4415 DEBUG(5,("set_unix_posix_default_acl: Can't set default ACL on non-directory file %s\n", fname ));
4416 errno = EISDIR;
4417 return False;
4418 } else {
4419 return True;
4423 if (!num_def_acls) {
4424 /* Remove the default ACL. */
4425 if (SMB_VFS_SYS_ACL_DELETE_DEF_FILE(conn, fname) == -1) {
4426 DEBUG(5,("set_unix_posix_default_acl: acl_delete_def_file failed on directory %s (%s)\n",
4427 fname, strerror(errno) ));
4428 return False;
4430 return True;
4433 if ((def_acl = create_posix_acl_from_wire(conn, num_def_acls,
4434 pdata,
4435 talloc_tos())) == NULL) {
4436 return False;
4439 if (SMB_VFS_SYS_ACL_SET_FILE(conn, fname, SMB_ACL_TYPE_DEFAULT, def_acl) == -1) {
4440 DEBUG(5,("set_unix_posix_default_acl: acl_set_file failed on directory %s (%s)\n",
4441 fname, strerror(errno) ));
4442 TALLOC_FREE(def_acl);
4443 return False;
4446 DEBUG(10,("set_unix_posix_default_acl: set default acl for file %s\n", fname ));
4447 TALLOC_FREE(def_acl);
4448 return True;
4451 /****************************************************************************
4452 Remove an ACL from a file. As we don't have acl_delete_entry() available
4453 we must read the current acl and copy all entries except MASK, USER and GROUP
4454 to a new acl, then set that. This (at least on Linux) causes any ACL to be
4455 removed.
4456 FIXME ! How does the share mask/mode fit into this.... ?
4457 ****************************************************************************/
4459 static bool remove_posix_acl(connection_struct *conn, files_struct *fsp, const char *fname)
4461 SMB_ACL_T file_acl = NULL;
4462 int entry_id = SMB_ACL_FIRST_ENTRY;
4463 SMB_ACL_ENTRY_T entry;
4464 bool ret = False;
4465 /* Create a new ACL with only 3 entries, u/g/w. */
4466 SMB_ACL_T new_file_acl = sys_acl_init(talloc_tos());
4467 SMB_ACL_ENTRY_T user_ent = NULL;
4468 SMB_ACL_ENTRY_T group_ent = NULL;
4469 SMB_ACL_ENTRY_T other_ent = NULL;
4471 if (new_file_acl == NULL) {
4472 DEBUG(5,("remove_posix_acl: failed to init new ACL with 3 entries for file %s.\n", fname));
4473 return False;
4476 /* Now create the u/g/w entries. */
4477 if (sys_acl_create_entry(&new_file_acl, &user_ent) == -1) {
4478 DEBUG(5,("remove_posix_acl: Failed to create user entry for file %s. (%s)\n",
4479 fname, strerror(errno) ));
4480 goto done;
4482 if (sys_acl_set_tag_type(user_ent, SMB_ACL_USER_OBJ) == -1) {
4483 DEBUG(5,("remove_posix_acl: Failed to set user entry for file %s. (%s)\n",
4484 fname, strerror(errno) ));
4485 goto done;
4488 if (sys_acl_create_entry(&new_file_acl, &group_ent) == -1) {
4489 DEBUG(5,("remove_posix_acl: Failed to create group entry for file %s. (%s)\n",
4490 fname, strerror(errno) ));
4491 goto done;
4493 if (sys_acl_set_tag_type(group_ent, SMB_ACL_GROUP_OBJ) == -1) {
4494 DEBUG(5,("remove_posix_acl: Failed to set group entry for file %s. (%s)\n",
4495 fname, strerror(errno) ));
4496 goto done;
4499 if (sys_acl_create_entry(&new_file_acl, &other_ent) == -1) {
4500 DEBUG(5,("remove_posix_acl: Failed to create other entry for file %s. (%s)\n",
4501 fname, strerror(errno) ));
4502 goto done;
4504 if (sys_acl_set_tag_type(other_ent, SMB_ACL_OTHER) == -1) {
4505 DEBUG(5,("remove_posix_acl: Failed to set other entry for file %s. (%s)\n",
4506 fname, strerror(errno) ));
4507 goto done;
4510 /* Get the current file ACL. */
4511 if (fsp && fsp->fh->fd != -1) {
4512 file_acl = SMB_VFS_SYS_ACL_GET_FD(fsp, talloc_tos());
4513 } else {
4514 file_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, fname,
4515 SMB_ACL_TYPE_ACCESS,
4516 talloc_tos());
4519 if (file_acl == NULL) {
4520 /* This is only returned if an error occurred. Even for a file with
4521 no acl a u/g/w acl should be returned. */
4522 DEBUG(5,("remove_posix_acl: failed to get ACL from file %s (%s).\n",
4523 fname, strerror(errno) ));
4524 goto done;
4527 while ( sys_acl_get_entry(file_acl, entry_id, &entry) == 1) {
4528 SMB_ACL_TAG_T tagtype;
4529 SMB_ACL_PERMSET_T permset;
4531 entry_id = SMB_ACL_NEXT_ENTRY;
4533 if (sys_acl_get_tag_type(entry, &tagtype) == -1) {
4534 DEBUG(5,("remove_posix_acl: failed to get tagtype from ACL on file %s (%s).\n",
4535 fname, strerror(errno) ));
4536 goto done;
4539 if (sys_acl_get_permset(entry, &permset) == -1) {
4540 DEBUG(5,("remove_posix_acl: failed to get permset from ACL on file %s (%s).\n",
4541 fname, strerror(errno) ));
4542 goto done;
4545 if (tagtype == SMB_ACL_USER_OBJ) {
4546 if (sys_acl_set_permset(user_ent, permset) == -1) {
4547 DEBUG(5,("remove_posix_acl: failed to set permset from ACL on file %s (%s).\n",
4548 fname, strerror(errno) ));
4550 } else if (tagtype == SMB_ACL_GROUP_OBJ) {
4551 if (sys_acl_set_permset(group_ent, permset) == -1) {
4552 DEBUG(5,("remove_posix_acl: failed to set permset from ACL on file %s (%s).\n",
4553 fname, strerror(errno) ));
4555 } else if (tagtype == SMB_ACL_OTHER) {
4556 if (sys_acl_set_permset(other_ent, permset) == -1) {
4557 DEBUG(5,("remove_posix_acl: failed to set permset from ACL on file %s (%s).\n",
4558 fname, strerror(errno) ));
4563 /* Set the new empty file ACL. */
4564 if (fsp && fsp->fh->fd != -1) {
4565 if (SMB_VFS_SYS_ACL_SET_FD(fsp, new_file_acl) == -1) {
4566 DEBUG(5,("remove_posix_acl: acl_set_file failed on %s (%s)\n",
4567 fname, strerror(errno) ));
4568 goto done;
4570 } else {
4571 if (SMB_VFS_SYS_ACL_SET_FILE(conn, fname, SMB_ACL_TYPE_ACCESS, new_file_acl) == -1) {
4572 DEBUG(5,("remove_posix_acl: acl_set_file failed on %s (%s)\n",
4573 fname, strerror(errno) ));
4574 goto done;
4578 ret = True;
4580 done:
4582 if (file_acl) {
4583 TALLOC_FREE(file_acl);
4585 if (new_file_acl) {
4586 TALLOC_FREE(new_file_acl);
4588 return ret;
4591 /****************************************************************************
4592 Calls from UNIX extensions - POSIX ACL set.
4593 If num_def_acls == 0 then read/modify/write acl after removing all entries
4594 except SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER.
4595 ****************************************************************************/
4597 bool set_unix_posix_acl(connection_struct *conn, files_struct *fsp, const char *fname, uint16_t num_acls, const char *pdata)
4599 SMB_ACL_T file_acl = NULL;
4601 if (!num_acls) {
4602 /* Remove the ACL from the file. */
4603 return remove_posix_acl(conn, fsp, fname);
4606 if ((file_acl = create_posix_acl_from_wire(conn, num_acls,
4607 pdata,
4608 talloc_tos())) == NULL) {
4609 return False;
4612 if (fsp && fsp->fh->fd != -1) {
4613 /* The preferred way - use an open fd. */
4614 if (SMB_VFS_SYS_ACL_SET_FD(fsp, file_acl) == -1) {
4615 DEBUG(5,("set_unix_posix_acl: acl_set_file failed on %s (%s)\n",
4616 fname, strerror(errno) ));
4617 TALLOC_FREE(file_acl);
4618 return False;
4620 } else {
4621 if (SMB_VFS_SYS_ACL_SET_FILE(conn, fname, SMB_ACL_TYPE_ACCESS, file_acl) == -1) {
4622 DEBUG(5,("set_unix_posix_acl: acl_set_file failed on %s (%s)\n",
4623 fname, strerror(errno) ));
4624 TALLOC_FREE(file_acl);
4625 return False;
4629 DEBUG(10,("set_unix_posix_acl: set acl for file %s\n", fname ));
4630 TALLOC_FREE(file_acl);
4631 return True;
4634 /********************************************************************
4635 Pull the NT ACL from a file on disk or the OpenEventlog() access
4636 check. Caller is responsible for freeing the returned security
4637 descriptor via TALLOC_FREE(). This is designed for dealing with
4638 user space access checks in smbd outside of the VFS. For example,
4639 checking access rights in OpenEventlog() or from python.
4641 ********************************************************************/
4643 NTSTATUS get_nt_acl_no_snum(TALLOC_CTX *ctx, const char *fname,
4644 uint32_t security_info_wanted,
4645 struct security_descriptor **sd)
4647 TALLOC_CTX *frame = talloc_stackframe();
4648 connection_struct *conn;
4649 NTSTATUS status = NT_STATUS_OK;
4650 struct smb_filename *smb_fname = synthetic_smb_fname(talloc_tos(),
4651 fname,
4652 NULL,
4653 NULL,
4656 if (smb_fname == NULL) {
4657 TALLOC_FREE(frame);
4658 return NT_STATUS_NO_MEMORY;
4661 if (!posix_locking_init(false)) {
4662 TALLOC_FREE(frame);
4663 return NT_STATUS_NO_MEMORY;
4666 status = create_conn_struct(ctx,
4667 server_event_context(),
4668 server_messaging_context(),
4669 &conn,
4671 "/",
4672 NULL);
4674 if (!NT_STATUS_IS_OK(status)) {
4675 DEBUG(0,("create_conn_struct returned %s.\n",
4676 nt_errstr(status)));
4677 TALLOC_FREE(frame);
4678 return status;
4681 status = SMB_VFS_GET_NT_ACL(conn,
4682 smb_fname,
4683 security_info_wanted,
4684 ctx,
4685 sd);
4686 if (!NT_STATUS_IS_OK(status)) {
4687 DEBUG(0, ("get_nt_acl_no_snum: SMB_VFS_GET_NT_ACL returned %s.\n",
4688 nt_errstr(status)));
4691 conn_free(conn);
4692 TALLOC_FREE(frame);
4694 return status;
4697 int posix_sys_acl_blob_get_file(vfs_handle_struct *handle,
4698 const char *path_p,
4699 TALLOC_CTX *mem_ctx,
4700 char **blob_description,
4701 DATA_BLOB *blob)
4703 int ret;
4704 TALLOC_CTX *frame = talloc_stackframe();
4705 /* Initialise this to zero, in a portable way */
4706 struct smb_acl_wrapper acl_wrapper = {
4707 NULL
4709 struct smb_filename *smb_fname;
4711 smb_fname = synthetic_smb_fname(frame, path_p, NULL, NULL, 0);
4712 if (smb_fname == NULL) {
4713 TALLOC_FREE(frame);
4714 errno = ENOMEM;
4715 return -1;
4718 acl_wrapper.access_acl
4719 = smb_vfs_call_sys_acl_get_file(handle,
4720 path_p,
4721 SMB_ACL_TYPE_ACCESS,
4722 frame);
4724 ret = smb_vfs_call_stat(handle, smb_fname);
4725 if (ret == -1) {
4726 TALLOC_FREE(frame);
4727 return -1;
4730 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
4731 acl_wrapper.default_acl
4732 = smb_vfs_call_sys_acl_get_file(handle,
4733 path_p,
4734 SMB_ACL_TYPE_DEFAULT,
4735 frame);
4738 acl_wrapper.owner = smb_fname->st.st_ex_uid;
4739 acl_wrapper.group = smb_fname->st.st_ex_gid;
4740 acl_wrapper.mode = smb_fname->st.st_ex_mode;
4742 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(blob, mem_ctx,
4743 &acl_wrapper,
4744 (ndr_push_flags_fn_t)ndr_push_smb_acl_wrapper))) {
4745 errno = EINVAL;
4746 TALLOC_FREE(frame);
4747 return -1;
4750 *blob_description = talloc_strdup(mem_ctx, "posix_acl");
4751 if (!*blob_description) {
4752 errno = EINVAL;
4753 TALLOC_FREE(frame);
4754 return -1;
4757 TALLOC_FREE(frame);
4758 return 0;
4761 int posix_sys_acl_blob_get_fd(vfs_handle_struct *handle,
4762 files_struct *fsp,
4763 TALLOC_CTX *mem_ctx,
4764 char **blob_description,
4765 DATA_BLOB *blob)
4767 SMB_STRUCT_STAT sbuf;
4768 TALLOC_CTX *frame;
4769 struct smb_acl_wrapper acl_wrapper;
4770 int ret;
4772 /* This ensures that we also consider the default ACL */
4773 if (fsp->is_directory || fsp->fh->fd == -1) {
4774 return posix_sys_acl_blob_get_file(handle, fsp->fsp_name->base_name,
4775 mem_ctx, blob_description, blob);
4777 frame = talloc_stackframe();
4779 acl_wrapper.default_acl = NULL;
4781 acl_wrapper.access_acl = smb_vfs_call_sys_acl_get_file(handle, fsp->fsp_name->base_name,
4782 SMB_ACL_TYPE_ACCESS, frame);
4784 ret = smb_vfs_call_fstat(handle, fsp, &sbuf);
4785 if (ret == -1) {
4786 TALLOC_FREE(frame);
4787 return -1;
4790 acl_wrapper.owner = sbuf.st_ex_uid;
4791 acl_wrapper.group = sbuf.st_ex_gid;
4792 acl_wrapper.mode = sbuf.st_ex_mode;
4794 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(blob, mem_ctx,
4795 &acl_wrapper,
4796 (ndr_push_flags_fn_t)ndr_push_smb_acl_wrapper))) {
4797 errno = EINVAL;
4798 TALLOC_FREE(frame);
4799 return -1;
4802 *blob_description = talloc_strdup(mem_ctx, "posix_acl");
4803 if (!*blob_description) {
4804 errno = EINVAL;
4805 TALLOC_FREE(frame);
4806 return -1;
4809 TALLOC_FREE(frame);
4810 return 0;