VERSION: Bump version up to 4.9.2...
[Samba.git] / source3 / smbd / posix_acls.c
blob8cc9cf1f2fc35c121f29adf89a87378cdf11a669
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,
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 unsigned 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,
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 struct smb_filename *smb_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, smb_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",
720 (unsigned long)ret, smb_fname->base_name));
722 if (ret == -1) {
723 /* No attribute or not supported. */
724 #if defined(ENOATTR)
725 if (errno != ENOATTR)
726 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno) ));
727 #else
728 if (errno != ENOSYS)
729 DEBUG(10,("load_inherited_info: Error %s\n", strerror(errno) ));
730 #endif
731 TALLOC_FREE(pai_buf);
732 return NULL;
735 paiv = create_pai_val(pai_buf, ret);
737 if (paiv) {
738 DEBUG(10,("load_inherited_info: ACL type 0x%x for file %s\n",
739 (unsigned int)paiv->sd_type,
740 smb_fname->base_name));
743 TALLOC_FREE(pai_buf);
744 return paiv;
747 /****************************************************************************
748 Functions to manipulate the internal ACE format.
749 ****************************************************************************/
751 /****************************************************************************
752 Count a linked list of canonical ACE entries.
753 ****************************************************************************/
755 static size_t count_canon_ace_list( canon_ace *l_head )
757 size_t count = 0;
758 canon_ace *ace;
760 for (ace = l_head; ace; ace = ace->next)
761 count++;
763 return count;
766 /****************************************************************************
767 Free a linked list of canonical ACE entries.
768 ****************************************************************************/
770 static void free_canon_ace_list( canon_ace *l_head )
772 canon_ace *list, *next;
774 for (list = l_head; list; list = next) {
775 next = list->next;
776 DLIST_REMOVE(l_head, list);
777 TALLOC_FREE(list);
781 /****************************************************************************
782 Function to duplicate a canon_ace entry.
783 ****************************************************************************/
785 static canon_ace *dup_canon_ace( canon_ace *src_ace)
787 canon_ace *dst_ace = talloc(talloc_tos(), canon_ace);
789 if (dst_ace == NULL)
790 return NULL;
792 *dst_ace = *src_ace;
793 dst_ace->prev = dst_ace->next = NULL;
794 return dst_ace;
797 /****************************************************************************
798 Print out a canon ace.
799 ****************************************************************************/
801 static void print_canon_ace(canon_ace *pace, int num)
803 dbgtext( "canon_ace index %d. Type = %s ", num, pace->attr == ALLOW_ACE ? "allow" : "deny" );
804 dbgtext( "SID = %s ", sid_string_dbg(&pace->trustee));
805 if (pace->owner_type == UID_ACE) {
806 dbgtext( "uid %u ", (unsigned int)pace->unix_ug.id);
807 } else if (pace->owner_type == GID_ACE) {
808 dbgtext( "gid %u ", (unsigned int)pace->unix_ug.id);
809 } else
810 dbgtext( "other ");
811 switch (pace->type) {
812 case SMB_ACL_USER:
813 dbgtext( "SMB_ACL_USER ");
814 break;
815 case SMB_ACL_USER_OBJ:
816 dbgtext( "SMB_ACL_USER_OBJ ");
817 break;
818 case SMB_ACL_GROUP:
819 dbgtext( "SMB_ACL_GROUP ");
820 break;
821 case SMB_ACL_GROUP_OBJ:
822 dbgtext( "SMB_ACL_GROUP_OBJ ");
823 break;
824 case SMB_ACL_OTHER:
825 dbgtext( "SMB_ACL_OTHER ");
826 break;
827 default:
828 dbgtext( "MASK " );
829 break;
832 dbgtext( "ace_flags = 0x%x ", (unsigned int)pace->ace_flags);
833 dbgtext( "perms ");
834 dbgtext( "%c", pace->perms & S_IRUSR ? 'r' : '-');
835 dbgtext( "%c", pace->perms & S_IWUSR ? 'w' : '-');
836 dbgtext( "%c\n", pace->perms & S_IXUSR ? 'x' : '-');
839 /****************************************************************************
840 Print out a canon ace list.
841 ****************************************************************************/
843 static void print_canon_ace_list(const char *name, canon_ace *ace_list)
845 int count = 0;
847 if( DEBUGLVL( 10 )) {
848 dbgtext( "print_canon_ace_list: %s\n", name );
849 for (;ace_list; ace_list = ace_list->next, count++)
850 print_canon_ace(ace_list, count );
854 /****************************************************************************
855 Map POSIX ACL perms to canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits).
856 ****************************************************************************/
858 static mode_t convert_permset_to_mode_t(SMB_ACL_PERMSET_T permset)
860 mode_t ret = 0;
862 ret |= (sys_acl_get_perm(permset, SMB_ACL_READ) ? S_IRUSR : 0);
863 ret |= (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? S_IWUSR : 0);
864 ret |= (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? S_IXUSR : 0);
866 return ret;
869 /****************************************************************************
870 Map generic UNIX permissions to canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits).
871 ****************************************************************************/
873 mode_t unix_perms_to_acl_perms(mode_t mode, int r_mask, int w_mask, int x_mask)
875 mode_t ret = 0;
877 if (mode & r_mask)
878 ret |= S_IRUSR;
879 if (mode & w_mask)
880 ret |= S_IWUSR;
881 if (mode & x_mask)
882 ret |= S_IXUSR;
884 return ret;
887 /****************************************************************************
888 Map canon_ace permissions (a mode_t containing only S_(R|W|X)USR bits) to
889 an SMB_ACL_PERMSET_T.
890 ****************************************************************************/
892 int map_acl_perms_to_permset(mode_t mode, SMB_ACL_PERMSET_T *p_permset)
894 if (sys_acl_clear_perms(*p_permset) == -1)
895 return -1;
896 if (mode & S_IRUSR) {
897 if (sys_acl_add_perm(*p_permset, SMB_ACL_READ) == -1)
898 return -1;
900 if (mode & S_IWUSR) {
901 if (sys_acl_add_perm(*p_permset, SMB_ACL_WRITE) == -1)
902 return -1;
904 if (mode & S_IXUSR) {
905 if (sys_acl_add_perm(*p_permset, SMB_ACL_EXECUTE) == -1)
906 return -1;
908 return 0;
911 /****************************************************************************
912 Function to create owner and group SIDs from a SMB_STRUCT_STAT.
913 ****************************************************************************/
915 static void create_file_sids(const SMB_STRUCT_STAT *psbuf,
916 struct dom_sid *powner_sid,
917 struct dom_sid *pgroup_sid)
919 uid_to_sid( powner_sid, psbuf->st_ex_uid );
920 gid_to_sid( pgroup_sid, psbuf->st_ex_gid );
923 /****************************************************************************
924 Merge aces with a common UID or GID - if both are allow or deny, OR the permissions together and
925 delete the second one. If the first is deny, mask the permissions off and delete the allow
926 if the permissions become zero, delete the deny if the permissions are non zero.
927 ****************************************************************************/
929 static void merge_aces( canon_ace **pp_list_head, bool dir_acl)
931 canon_ace *l_head = *pp_list_head;
932 canon_ace *curr_ace_outer;
933 canon_ace *curr_ace_outer_next;
936 * First, merge allow entries with identical SIDs, and deny entries
937 * with identical SIDs.
940 for (curr_ace_outer = l_head; curr_ace_outer; curr_ace_outer = curr_ace_outer_next) {
941 canon_ace *curr_ace;
942 canon_ace *curr_ace_next;
944 curr_ace_outer_next = curr_ace_outer->next; /* Save the link in case we delete. */
946 for (curr_ace = curr_ace_outer->next; curr_ace; curr_ace = curr_ace_next) {
947 bool can_merge = false;
949 curr_ace_next = curr_ace->next; /* Save the link in case of delete. */
951 /* For file ACLs we can merge if the SIDs and ALLOW/DENY
952 * types are the same. For directory acls we must also
953 * ensure the POSIX ACL types are the same.
955 * For the IDMAP_BOTH case, we must not merge
956 * the UID and GID ACE values for same SID
959 if (!dir_acl) {
960 can_merge = (curr_ace->unix_ug.id == curr_ace_outer->unix_ug.id &&
961 curr_ace->owner_type == curr_ace_outer->owner_type &&
962 (curr_ace->attr == curr_ace_outer->attr));
963 } else {
964 can_merge = (curr_ace->unix_ug.id == curr_ace_outer->unix_ug.id &&
965 curr_ace->owner_type == curr_ace_outer->owner_type &&
966 (curr_ace->type == curr_ace_outer->type) &&
967 (curr_ace->attr == curr_ace_outer->attr));
970 if (can_merge) {
971 if( DEBUGLVL( 10 )) {
972 dbgtext("merge_aces: Merging ACE's\n");
973 print_canon_ace( curr_ace_outer, 0);
974 print_canon_ace( curr_ace, 0);
977 /* Merge two allow or two deny ACE's. */
979 /* Theoretically we shouldn't merge a dir ACE if
980 * one ACE has the CI flag set, and the other
981 * ACE has the OI flag set, but this is rare
982 * enough we can ignore it. */
984 curr_ace_outer->perms |= curr_ace->perms;
985 curr_ace_outer->ace_flags |= curr_ace->ace_flags;
986 DLIST_REMOVE(l_head, curr_ace);
987 TALLOC_FREE(curr_ace);
988 curr_ace_outer_next = curr_ace_outer->next; /* We may have deleted the link. */
994 * Now go through and mask off allow permissions with deny permissions.
995 * We can delete either the allow or deny here as we know that each SID
996 * appears only once in the list.
999 for (curr_ace_outer = l_head; curr_ace_outer; curr_ace_outer = curr_ace_outer_next) {
1000 canon_ace *curr_ace;
1001 canon_ace *curr_ace_next;
1003 curr_ace_outer_next = curr_ace_outer->next; /* Save the link in case we delete. */
1005 for (curr_ace = curr_ace_outer->next; curr_ace; curr_ace = curr_ace_next) {
1007 curr_ace_next = curr_ace->next; /* Save the link in case of delete. */
1010 * Subtract ACE's with different entries. Due to the ordering constraints
1011 * we've put on the ACL, we know the deny must be the first one.
1014 if (curr_ace->unix_ug.id == curr_ace_outer->unix_ug.id &&
1015 (curr_ace->owner_type == curr_ace_outer->owner_type) &&
1016 (curr_ace_outer->attr == DENY_ACE) && (curr_ace->attr == ALLOW_ACE)) {
1018 if( DEBUGLVL( 10 )) {
1019 dbgtext("merge_aces: Masking ACE's\n");
1020 print_canon_ace( curr_ace_outer, 0);
1021 print_canon_ace( curr_ace, 0);
1024 curr_ace->perms &= ~curr_ace_outer->perms;
1026 if (curr_ace->perms == 0) {
1029 * The deny overrides the allow. Remove the allow.
1032 DLIST_REMOVE(l_head, curr_ace);
1033 TALLOC_FREE(curr_ace);
1034 curr_ace_outer_next = curr_ace_outer->next; /* We may have deleted the link. */
1036 } else {
1039 * Even after removing permissions, there
1040 * are still allow permissions - delete the deny.
1041 * It is safe to delete the deny here,
1042 * as we are guarenteed by the deny first
1043 * ordering that all the deny entries for
1044 * this SID have already been merged into one
1045 * before we can get to an allow ace.
1048 DLIST_REMOVE(l_head, curr_ace_outer);
1049 TALLOC_FREE(curr_ace_outer);
1050 break;
1054 } /* end for curr_ace */
1055 } /* end for curr_ace_outer */
1057 /* We may have modified the list. */
1059 *pp_list_head = l_head;
1062 /****************************************************************************
1063 Map canon_ace perms to permission bits NT.
1064 The attr element is not used here - we only process deny entries on set,
1065 not get. Deny entries are implicit on get with ace->perms = 0.
1066 ****************************************************************************/
1068 uint32_t map_canon_ace_perms(int snum,
1069 enum security_ace_type *pacl_type,
1070 mode_t perms,
1071 bool directory_ace)
1073 uint32_t nt_mask = 0;
1075 *pacl_type = SEC_ACE_TYPE_ACCESS_ALLOWED;
1077 if (lp_acl_map_full_control(snum) && ((perms & ALL_ACE_PERMS) == ALL_ACE_PERMS)) {
1078 if (directory_ace) {
1079 nt_mask = UNIX_DIRECTORY_ACCESS_RWX;
1080 } else {
1081 nt_mask = (UNIX_ACCESS_RWX & ~DELETE_ACCESS);
1083 } else if ((perms & ALL_ACE_PERMS) == (mode_t)0) {
1085 * Windows NT refuses to display ACEs with no permissions in them (but
1086 * they are perfectly legal with Windows 2000). If the ACE has empty
1087 * permissions we cannot use 0, so we use the otherwise unused
1088 * WRITE_OWNER permission, which we ignore when we set an ACL.
1089 * We abstract this into a #define of UNIX_ACCESS_NONE to allow this
1090 * to be changed in the future.
1093 nt_mask = 0;
1094 } else {
1095 if (directory_ace) {
1096 nt_mask |= ((perms & S_IRUSR) ? UNIX_DIRECTORY_ACCESS_R : 0 );
1097 nt_mask |= ((perms & S_IWUSR) ? UNIX_DIRECTORY_ACCESS_W : 0 );
1098 nt_mask |= ((perms & S_IXUSR) ? UNIX_DIRECTORY_ACCESS_X : 0 );
1099 } else {
1100 nt_mask |= ((perms & S_IRUSR) ? UNIX_ACCESS_R : 0 );
1101 nt_mask |= ((perms & S_IWUSR) ? UNIX_ACCESS_W : 0 );
1102 nt_mask |= ((perms & S_IXUSR) ? UNIX_ACCESS_X : 0 );
1106 if ((perms & S_IWUSR) && lp_dos_filemode(snum)) {
1107 nt_mask |= (SEC_STD_WRITE_DAC|SEC_STD_WRITE_OWNER|DELETE_ACCESS);
1110 DEBUG(10,("map_canon_ace_perms: Mapped (UNIX) %x to (NT) %x\n",
1111 (unsigned int)perms, (unsigned int)nt_mask ));
1113 return nt_mask;
1116 /****************************************************************************
1117 Map NT perms to a UNIX mode_t.
1118 ****************************************************************************/
1120 #define FILE_SPECIFIC_READ_BITS (FILE_READ_DATA|FILE_READ_EA)
1121 #define FILE_SPECIFIC_WRITE_BITS (FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_WRITE_EA)
1122 #define FILE_SPECIFIC_EXECUTE_BITS (FILE_EXECUTE)
1124 static mode_t map_nt_perms( uint32_t *mask, int type)
1126 mode_t mode = 0;
1128 switch(type) {
1129 case S_IRUSR:
1130 if((*mask) & GENERIC_ALL_ACCESS)
1131 mode = S_IRUSR|S_IWUSR|S_IXUSR;
1132 else {
1133 mode |= ((*mask) & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IRUSR : 0;
1134 mode |= ((*mask) & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWUSR : 0;
1135 mode |= ((*mask) & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXUSR : 0;
1137 break;
1138 case S_IRGRP:
1139 if((*mask) & GENERIC_ALL_ACCESS)
1140 mode = S_IRGRP|S_IWGRP|S_IXGRP;
1141 else {
1142 mode |= ((*mask) & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IRGRP : 0;
1143 mode |= ((*mask) & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWGRP : 0;
1144 mode |= ((*mask) & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXGRP : 0;
1146 break;
1147 case S_IROTH:
1148 if((*mask) & GENERIC_ALL_ACCESS)
1149 mode = S_IROTH|S_IWOTH|S_IXOTH;
1150 else {
1151 mode |= ((*mask) & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IROTH : 0;
1152 mode |= ((*mask) & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWOTH : 0;
1153 mode |= ((*mask) & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXOTH : 0;
1155 break;
1158 return mode;
1161 /****************************************************************************
1162 Unpack a struct security_descriptor into a UNIX owner and group.
1163 ****************************************************************************/
1165 NTSTATUS unpack_nt_owners(struct connection_struct *conn,
1166 uid_t *puser, gid_t *pgrp,
1167 uint32_t security_info_sent, const struct
1168 security_descriptor *psd)
1170 *puser = (uid_t)-1;
1171 *pgrp = (gid_t)-1;
1173 if(security_info_sent == 0) {
1174 DEBUG(0,("unpack_nt_owners: no security info sent !\n"));
1175 return NT_STATUS_OK;
1179 * Validate the owner and group SID's.
1182 DEBUG(5,("unpack_nt_owners: validating owner_sids.\n"));
1185 * Don't immediately fail if the owner sid cannot be validated.
1186 * This may be a group chown only set.
1189 if (security_info_sent & SECINFO_OWNER) {
1190 if (!sid_to_uid(psd->owner_sid, puser)) {
1191 if (lp_force_unknown_acl_user(SNUM(conn))) {
1192 /* this allows take ownership to work
1193 * reasonably */
1194 *puser = get_current_uid(conn);
1195 } else {
1196 DEBUG(3,("unpack_nt_owners: unable to validate"
1197 " owner sid for %s\n",
1198 sid_string_dbg(psd->owner_sid)));
1199 return NT_STATUS_INVALID_OWNER;
1202 DEBUG(3,("unpack_nt_owners: owner sid mapped to uid %u\n",
1203 (unsigned int)*puser ));
1207 * Don't immediately fail if the group sid cannot be validated.
1208 * This may be an owner chown only set.
1211 if (security_info_sent & SECINFO_GROUP) {
1212 if (!sid_to_gid(psd->group_sid, pgrp)) {
1213 if (lp_force_unknown_acl_user(SNUM(conn))) {
1214 /* this allows take group ownership to work
1215 * reasonably */
1216 *pgrp = get_current_gid(conn);
1217 } else {
1218 DEBUG(3,("unpack_nt_owners: unable to validate"
1219 " group sid.\n"));
1220 return NT_STATUS_INVALID_OWNER;
1223 DEBUG(3,("unpack_nt_owners: group sid mapped to gid %u\n",
1224 (unsigned int)*pgrp));
1227 DEBUG(5,("unpack_nt_owners: owner_sids validated.\n"));
1229 return NT_STATUS_OK;
1233 static void trim_ace_perms(canon_ace *pace)
1235 pace->perms = pace->perms & (S_IXUSR|S_IWUSR|S_IRUSR);
1238 static void ensure_minimal_owner_ace_perms(const bool is_directory,
1239 canon_ace *pace)
1241 pace->perms |= S_IRUSR;
1242 if (is_directory) {
1243 pace->perms |= (S_IWUSR|S_IXUSR);
1247 /****************************************************************************
1248 Check if a given uid/SID is in a group gid/SID. This is probably very
1249 expensive and will need optimisation. A *lot* of optimisation :-). JRA.
1250 ****************************************************************************/
1252 static bool uid_entry_in_group(connection_struct *conn, canon_ace *uid_ace, canon_ace *group_ace )
1254 bool is_sid = false;
1255 bool has_sid = false;
1256 struct security_token *security_token = NULL;
1258 /* "Everyone" always matches every uid. */
1260 if (dom_sid_equal(&group_ace->trustee, &global_sid_World))
1261 return True;
1264 * if we have session info in conn, we already have the (SID
1265 * based) NT token and don't need to do the complex
1266 * user_in_group_sid() call
1268 if (conn->session_info) {
1269 security_token = conn->session_info->security_token;
1270 /* security_token should not be NULL */
1271 SMB_ASSERT(security_token);
1272 is_sid = security_token_is_sid(security_token,
1273 &uid_ace->trustee);
1274 if (is_sid) {
1275 has_sid = security_token_has_sid(security_token,
1276 &group_ace->trustee);
1278 if (has_sid) {
1279 return true;
1286 * if it's the current user, we already have the unix token
1287 * and don't need to do the complex user_in_group_sid() call
1289 if (uid_ace->unix_ug.id == get_current_uid(conn)) {
1290 const struct security_unix_token *curr_utok = NULL;
1291 size_t i;
1293 if (group_ace->unix_ug.id == get_current_gid(conn)) {
1294 return True;
1297 curr_utok = get_current_utok(conn);
1298 for (i=0; i < curr_utok->ngroups; i++) {
1299 if (group_ace->unix_ug.id == curr_utok->groups[i]) {
1300 return True;
1306 * user_in_group_sid() uses create_token_from_sid()
1307 * which creates an artificial NT token given just a username,
1308 * so this is not reliable for users from foreign domains
1309 * exported by winbindd!
1311 return user_sid_in_group_sid(&uid_ace->trustee, &group_ace->trustee);
1314 /****************************************************************************
1315 A well formed POSIX file or default ACL has at least 3 entries, a
1316 SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ.
1317 In addition, the owner must always have at least read access.
1318 When using this call on get_acl, the pst struct is valid and contains
1319 the mode of the file.
1320 ****************************************************************************/
1322 static bool ensure_canon_entry_valid_on_get(connection_struct *conn,
1323 canon_ace **pp_ace,
1324 const struct dom_sid *pfile_owner_sid,
1325 const struct dom_sid *pfile_grp_sid,
1326 const SMB_STRUCT_STAT *pst)
1328 canon_ace *pace;
1329 bool got_user = false;
1330 bool got_group = false;
1331 bool got_other = false;
1333 for (pace = *pp_ace; pace; pace = pace->next) {
1334 if (pace->type == SMB_ACL_USER_OBJ) {
1335 got_user = true;
1336 } else if (pace->type == SMB_ACL_GROUP_OBJ) {
1337 got_group = true;
1338 } else if (pace->type == SMB_ACL_OTHER) {
1339 got_other = true;
1343 if (!got_user) {
1344 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1345 DEBUG(0,("malloc fail.\n"));
1346 return false;
1349 ZERO_STRUCTP(pace);
1350 pace->type = SMB_ACL_USER_OBJ;
1351 pace->owner_type = UID_ACE;
1352 pace->unix_ug.type = ID_TYPE_UID;
1353 pace->unix_ug.id = pst->st_ex_uid;
1354 pace->trustee = *pfile_owner_sid;
1355 pace->attr = ALLOW_ACE;
1356 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IRUSR, S_IWUSR, S_IXUSR);
1357 DLIST_ADD(*pp_ace, pace);
1360 if (!got_group) {
1361 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1362 DEBUG(0,("malloc fail.\n"));
1363 return false;
1366 ZERO_STRUCTP(pace);
1367 pace->type = SMB_ACL_GROUP_OBJ;
1368 pace->owner_type = GID_ACE;
1369 pace->unix_ug.type = ID_TYPE_GID;
1370 pace->unix_ug.id = pst->st_ex_gid;
1371 pace->trustee = *pfile_grp_sid;
1372 pace->attr = ALLOW_ACE;
1373 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IRGRP, S_IWGRP, S_IXGRP);
1374 DLIST_ADD(*pp_ace, pace);
1377 if (!got_other) {
1378 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1379 DEBUG(0,("malloc fail.\n"));
1380 return false;
1383 ZERO_STRUCTP(pace);
1384 pace->type = SMB_ACL_OTHER;
1385 pace->owner_type = WORLD_ACE;
1386 pace->unix_ug.type = ID_TYPE_NOT_SPECIFIED;
1387 pace->unix_ug.id = -1;
1388 pace->trustee = global_sid_World;
1389 pace->attr = ALLOW_ACE;
1390 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IROTH, S_IWOTH, S_IXOTH);
1391 DLIST_ADD(*pp_ace, pace);
1394 return true;
1397 /****************************************************************************
1398 A well formed POSIX file or default ACL has at least 3 entries, a
1399 SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ.
1400 In addition, the owner must always have at least read access.
1401 When using this call on set_acl, the pst struct has
1402 been modified to have a mode containing the default for this file or directory
1403 type.
1404 ****************************************************************************/
1406 static bool ensure_canon_entry_valid_on_set(connection_struct *conn,
1407 canon_ace **pp_ace,
1408 bool is_default_acl,
1409 const struct share_params *params,
1410 const bool is_directory,
1411 const struct dom_sid *pfile_owner_sid,
1412 const struct dom_sid *pfile_grp_sid,
1413 const SMB_STRUCT_STAT *pst)
1415 canon_ace *pace;
1416 canon_ace *pace_user = NULL;
1417 canon_ace *pace_group = NULL;
1418 canon_ace *pace_other = NULL;
1419 bool got_duplicate_user = false;
1420 bool got_duplicate_group = false;
1422 for (pace = *pp_ace; pace; pace = pace->next) {
1423 trim_ace_perms(pace);
1424 if (pace->type == SMB_ACL_USER_OBJ) {
1425 ensure_minimal_owner_ace_perms(is_directory, pace);
1426 pace_user = pace;
1427 } else if (pace->type == SMB_ACL_GROUP_OBJ) {
1428 pace_group = pace;
1429 } else if (pace->type == SMB_ACL_OTHER) {
1430 pace_other = pace;
1434 if (!pace_user) {
1435 canon_ace *pace_iter;
1437 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1438 DEBUG(0,("talloc fail.\n"));
1439 return false;
1442 ZERO_STRUCTP(pace);
1443 pace->type = SMB_ACL_USER_OBJ;
1444 pace->owner_type = UID_ACE;
1445 pace->unix_ug.type = ID_TYPE_UID;
1446 pace->unix_ug.id = pst->st_ex_uid;
1447 pace->trustee = *pfile_owner_sid;
1448 pace->attr = ALLOW_ACE;
1449 /* Start with existing user permissions, principle of least
1450 surprises for the user. */
1451 pace->perms = unix_perms_to_acl_perms(pst->st_ex_mode, S_IRUSR, S_IWUSR, S_IXUSR);
1453 /* See if the owning user is in any of the other groups in
1454 the ACE, or if there's a matching user entry (by uid
1455 or in the case of ID_TYPE_BOTH by SID).
1456 If so, OR in the permissions from that entry. */
1459 for (pace_iter = *pp_ace; pace_iter; pace_iter = pace_iter->next) {
1460 if (pace_iter->type == SMB_ACL_USER &&
1461 pace_iter->unix_ug.id == pace->unix_ug.id) {
1462 pace->perms |= pace_iter->perms;
1463 } else if (pace_iter->type == SMB_ACL_GROUP_OBJ || pace_iter->type == SMB_ACL_GROUP) {
1464 if (dom_sid_equal(&pace->trustee, &pace_iter->trustee)) {
1465 pace->perms |= pace_iter->perms;
1466 } else if (uid_entry_in_group(conn, pace, pace_iter)) {
1467 pace->perms |= pace_iter->perms;
1472 if (pace->perms == 0) {
1473 /* If we only got an "everyone" perm, just use that. */
1474 if (pace_other)
1475 pace->perms = pace_other->perms;
1479 * Ensure we have default parameters for the
1480 * user (owner) even on default ACLs.
1482 ensure_minimal_owner_ace_perms(is_directory, pace);
1484 DLIST_ADD(*pp_ace, pace);
1485 pace_user = pace;
1488 if (!pace_group) {
1489 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1490 DEBUG(0,("talloc fail.\n"));
1491 return false;
1494 ZERO_STRUCTP(pace);
1495 pace->type = SMB_ACL_GROUP_OBJ;
1496 pace->owner_type = GID_ACE;
1497 pace->unix_ug.type = ID_TYPE_GID;
1498 pace->unix_ug.id = pst->st_ex_gid;
1499 pace->trustee = *pfile_grp_sid;
1500 pace->attr = ALLOW_ACE;
1502 /* If we only got an "everyone" perm, just use that. */
1503 if (pace_other) {
1504 pace->perms = pace_other->perms;
1505 } else {
1506 pace->perms = 0;
1509 DLIST_ADD(*pp_ace, pace);
1510 pace_group = pace;
1513 if (!pace_other) {
1514 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1515 DEBUG(0,("talloc fail.\n"));
1516 return false;
1519 ZERO_STRUCTP(pace);
1520 pace->type = SMB_ACL_OTHER;
1521 pace->owner_type = WORLD_ACE;
1522 pace->unix_ug.type = ID_TYPE_NOT_SPECIFIED;
1523 pace->unix_ug.id = -1;
1524 pace->trustee = global_sid_World;
1525 pace->attr = ALLOW_ACE;
1526 pace->perms = 0;
1528 DLIST_ADD(*pp_ace, pace);
1529 pace_other = pace;
1532 /* Ensure when setting a POSIX ACL, that the uid for a
1533 SMB_ACL_USER_OBJ ACE (the owner ACE entry) has a duplicate
1534 permission entry as an SMB_ACL_USER, and a gid for a
1535 SMB_ACL_GROUP_OBJ ACE (the primary group ACE entry) also has
1536 a duplicate permission entry as an SMB_ACL_GROUP. If not,
1537 then if the ownership or group ownership of this file or
1538 directory gets changed, the user or group can lose their
1539 access. */
1541 for (pace = *pp_ace; pace; pace = pace->next) {
1542 if (pace->type == SMB_ACL_USER &&
1543 pace->unix_ug.id == pace_user->unix_ug.id) {
1544 /* Already got one. */
1545 got_duplicate_user = true;
1546 } else if (pace->type == SMB_ACL_GROUP &&
1547 pace->unix_ug.id == pace_group->unix_ug.id) {
1548 /* Already got one. */
1549 got_duplicate_group = true;
1550 } else if ((pace->type == SMB_ACL_GROUP)
1551 && (dom_sid_equal(&pace->trustee, &pace_user->trustee))) {
1552 /* If the SID owning the file appears
1553 * in a group entry, then we have
1554 * enough duplication, they will still
1555 * have access */
1556 got_duplicate_user = true;
1560 /* If the SID is equal for the user and group that we need
1561 to add the duplicate for, add only the group */
1562 if (!got_duplicate_user && !got_duplicate_group
1563 && dom_sid_equal(&pace_group->trustee,
1564 &pace_user->trustee)) {
1565 /* Add a duplicate SMB_ACL_GROUP entry, this
1566 * will cover the owning SID as well, as it
1567 * will always be mapped to both a uid and
1568 * gid. */
1570 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1571 DEBUG(0,("talloc fail.\n"));
1572 return false;
1575 ZERO_STRUCTP(pace);
1576 pace->type = SMB_ACL_GROUP;;
1577 pace->owner_type = GID_ACE;
1578 pace->unix_ug.type = ID_TYPE_GID;
1579 pace->unix_ug.id = pace_group->unix_ug.id;
1580 pace->trustee = pace_group->trustee;
1581 pace->attr = pace_group->attr;
1582 pace->perms = pace_group->perms;
1584 DLIST_ADD(*pp_ace, pace);
1586 /* We're done here, make sure the
1587 statements below are not executed. */
1588 got_duplicate_user = true;
1589 got_duplicate_group = true;
1592 if (!got_duplicate_user) {
1593 /* Add a duplicate SMB_ACL_USER entry. */
1594 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1595 DEBUG(0,("talloc fail.\n"));
1596 return false;
1599 ZERO_STRUCTP(pace);
1600 pace->type = SMB_ACL_USER;;
1601 pace->owner_type = UID_ACE;
1602 pace->unix_ug.type = ID_TYPE_UID;
1603 pace->unix_ug.id = pace_user->unix_ug.id;
1604 pace->trustee = pace_user->trustee;
1605 pace->attr = pace_user->attr;
1606 pace->perms = pace_user->perms;
1608 DLIST_ADD(*pp_ace, pace);
1610 got_duplicate_user = true;
1613 if (!got_duplicate_group) {
1614 /* Add a duplicate SMB_ACL_GROUP entry. */
1615 if ((pace = talloc(talloc_tos(), canon_ace)) == NULL) {
1616 DEBUG(0,("talloc fail.\n"));
1617 return false;
1620 ZERO_STRUCTP(pace);
1621 pace->type = SMB_ACL_GROUP;;
1622 pace->owner_type = GID_ACE;
1623 pace->unix_ug.type = ID_TYPE_GID;
1624 pace->unix_ug.id = pace_group->unix_ug.id;
1625 pace->trustee = pace_group->trustee;
1626 pace->attr = pace_group->attr;
1627 pace->perms = pace_group->perms;
1629 DLIST_ADD(*pp_ace, pace);
1631 got_duplicate_group = true;
1634 return true;
1637 /****************************************************************************
1638 Check if a POSIX ACL has the required SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ entries.
1639 If it does not have them, check if there are any entries where the trustee is the
1640 file owner or the owning group, and map these to SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ.
1641 Note we must not do this to default directory ACLs.
1642 ****************************************************************************/
1644 static void check_owning_objs(canon_ace *ace, struct dom_sid *pfile_owner_sid, struct dom_sid *pfile_grp_sid)
1646 bool got_user_obj, got_group_obj;
1647 canon_ace *current_ace;
1648 int i, entries;
1650 entries = count_canon_ace_list(ace);
1651 got_user_obj = False;
1652 got_group_obj = False;
1654 for (i=0, current_ace = ace; i < entries; i++, current_ace = current_ace->next) {
1655 if (current_ace->type == SMB_ACL_USER_OBJ)
1656 got_user_obj = True;
1657 else if (current_ace->type == SMB_ACL_GROUP_OBJ)
1658 got_group_obj = True;
1660 if (got_user_obj && got_group_obj) {
1661 DEBUG(10,("check_owning_objs: ACL had owning user/group entries.\n"));
1662 return;
1665 for (i=0, current_ace = ace; i < entries; i++, current_ace = current_ace->next) {
1666 if (!got_user_obj && current_ace->owner_type == UID_ACE &&
1667 dom_sid_equal(&current_ace->trustee, pfile_owner_sid)) {
1668 current_ace->type = SMB_ACL_USER_OBJ;
1669 got_user_obj = True;
1671 if (!got_group_obj && current_ace->owner_type == GID_ACE &&
1672 dom_sid_equal(&current_ace->trustee, pfile_grp_sid)) {
1673 current_ace->type = SMB_ACL_GROUP_OBJ;
1674 got_group_obj = True;
1677 if (!got_user_obj)
1678 DEBUG(10,("check_owning_objs: ACL is missing an owner entry.\n"));
1679 if (!got_group_obj)
1680 DEBUG(10,("check_owning_objs: ACL is missing an owning group entry.\n"));
1683 static bool add_current_ace_to_acl(files_struct *fsp, struct security_ace *psa,
1684 canon_ace **file_ace, canon_ace **dir_ace,
1685 bool *got_file_allow, bool *got_dir_allow,
1686 bool *all_aces_are_inherit_only,
1687 canon_ace *current_ace)
1691 * Map the given NT permissions into a UNIX mode_t containing only
1692 * S_I(R|W|X)USR bits.
1695 current_ace->perms |= map_nt_perms( &psa->access_mask, S_IRUSR);
1696 current_ace->attr = (psa->type == SEC_ACE_TYPE_ACCESS_ALLOWED) ? ALLOW_ACE : DENY_ACE;
1698 /* Store the ace_flag. */
1699 current_ace->ace_flags = psa->flags;
1702 * Now add the created ace to either the file list, the directory
1703 * list, or both. We *MUST* preserve the order here (hence we use
1704 * DLIST_ADD_END) as NT ACLs are order dependent.
1707 if (fsp->is_directory) {
1710 * We can only add to the default POSIX ACE list if the ACE is
1711 * designed to be inherited by both files and directories.
1714 if ((psa->flags & (SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT)) ==
1715 (SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT)) {
1717 canon_ace *current_dir_ace = current_ace;
1718 DLIST_ADD_END(*dir_ace, current_ace);
1721 * Note if this was an allow ace. We can't process
1722 * any further deny ace's after this.
1725 if (current_ace->attr == ALLOW_ACE)
1726 *got_dir_allow = True;
1728 if ((current_ace->attr == DENY_ACE) && *got_dir_allow) {
1729 DEBUG(0,("add_current_ace_to_acl: "
1730 "malformed ACL in "
1731 "inheritable ACL! Deny entry "
1732 "after Allow entry. Failing "
1733 "to set on file %s.\n",
1734 fsp_str_dbg(fsp)));
1735 return False;
1738 if( DEBUGLVL( 10 )) {
1739 dbgtext("add_current_ace_to_acl: adding dir ACL:\n");
1740 print_canon_ace( current_ace, 0);
1744 * If this is not an inherit only ACE we need to add a duplicate
1745 * to the file acl.
1748 if (!(psa->flags & SEC_ACE_FLAG_INHERIT_ONLY)) {
1749 canon_ace *dup_ace = dup_canon_ace(current_ace);
1751 if (!dup_ace) {
1752 DEBUG(0,("add_current_ace_to_acl: malloc fail !\n"));
1753 return False;
1757 * We must not free current_ace here as its
1758 * pointer is now owned by the dir_ace list.
1760 current_ace = dup_ace;
1761 /* We've essentially split this ace into two,
1762 * and added the ace with inheritance request
1763 * bits to the directory ACL. Drop those bits for
1764 * the ACE we're adding to the file list. */
1765 current_ace->ace_flags &= ~(SEC_ACE_FLAG_OBJECT_INHERIT|
1766 SEC_ACE_FLAG_CONTAINER_INHERIT|
1767 SEC_ACE_FLAG_INHERIT_ONLY);
1768 } else {
1770 * We must not free current_ace here as its
1771 * pointer is now owned by the dir_ace list.
1773 current_ace = NULL;
1777 * current_ace is now either owned by file_ace
1778 * or is NULL. We can safely operate on current_dir_ace
1779 * to treat mapping for default acl entries differently
1780 * than access acl entries.
1783 if (current_dir_ace->owner_type == UID_ACE) {
1785 * We already decided above this is a uid,
1786 * for default acls ace's only CREATOR_OWNER
1787 * maps to ACL_USER_OBJ. All other uid
1788 * ace's are ACL_USER.
1790 if (dom_sid_equal(&current_dir_ace->trustee,
1791 &global_sid_Creator_Owner)) {
1792 current_dir_ace->type = SMB_ACL_USER_OBJ;
1793 } else {
1794 current_dir_ace->type = SMB_ACL_USER;
1798 if (current_dir_ace->owner_type == GID_ACE) {
1800 * We already decided above this is a gid,
1801 * for default acls ace's only CREATOR_GROUP
1802 * maps to ACL_GROUP_OBJ. All other uid
1803 * ace's are ACL_GROUP.
1805 if (dom_sid_equal(&current_dir_ace->trustee,
1806 &global_sid_Creator_Group)) {
1807 current_dir_ace->type = SMB_ACL_GROUP_OBJ;
1808 } else {
1809 current_dir_ace->type = SMB_ACL_GROUP;
1816 * Only add to the file ACL if not inherit only.
1819 if (current_ace && !(psa->flags & SEC_ACE_FLAG_INHERIT_ONLY)) {
1820 DLIST_ADD_END(*file_ace, current_ace);
1823 * Note if this was an allow ace. We can't process
1824 * any further deny ace's after this.
1827 if (current_ace->attr == ALLOW_ACE)
1828 *got_file_allow = True;
1830 if ((current_ace->attr == DENY_ACE) && *got_file_allow) {
1831 DEBUG(0,("add_current_ace_to_acl: malformed "
1832 "ACL in file ACL ! Deny entry after "
1833 "Allow entry. Failing to set on file "
1834 "%s.\n", fsp_str_dbg(fsp)));
1835 return False;
1838 if( DEBUGLVL( 10 )) {
1839 dbgtext("add_current_ace_to_acl: adding file ACL:\n");
1840 print_canon_ace( current_ace, 0);
1842 *all_aces_are_inherit_only = False;
1844 * We must not free current_ace here as its
1845 * pointer is now owned by the file_ace list.
1847 current_ace = NULL;
1851 * Free if ACE was not added.
1854 TALLOC_FREE(current_ace);
1855 return true;
1858 /****************************************************************************
1859 Unpack a struct security_descriptor into two canonical ace lists.
1860 ****************************************************************************/
1862 static bool create_canon_ace_lists(files_struct *fsp,
1863 const SMB_STRUCT_STAT *pst,
1864 struct dom_sid *pfile_owner_sid,
1865 struct dom_sid *pfile_grp_sid,
1866 canon_ace **ppfile_ace,
1867 canon_ace **ppdir_ace,
1868 const struct security_acl *dacl)
1870 bool all_aces_are_inherit_only = (fsp->is_directory ? True : False);
1871 canon_ace *file_ace = NULL;
1872 canon_ace *dir_ace = NULL;
1873 canon_ace *current_ace = NULL;
1874 bool got_dir_allow = False;
1875 bool got_file_allow = False;
1876 uint32_t i, j;
1878 *ppfile_ace = NULL;
1879 *ppdir_ace = NULL;
1882 * Convert the incoming ACL into a more regular form.
1885 for(i = 0; i < dacl->num_aces; i++) {
1886 struct security_ace *psa = &dacl->aces[i];
1888 if((psa->type != SEC_ACE_TYPE_ACCESS_ALLOWED) && (psa->type != SEC_ACE_TYPE_ACCESS_DENIED)) {
1889 DEBUG(3,("create_canon_ace_lists: unable to set anything but an ALLOW or DENY ACE.\n"));
1890 return False;
1895 * Deal with the fact that NT 4.x re-writes the canonical format
1896 * that we return for default ACLs. If a directory ACE is identical
1897 * to a inherited directory ACE then NT changes the bits so that the
1898 * first ACE is set to OI|IO and the second ACE for this SID is set
1899 * to CI. We need to repair this. JRA.
1902 for(i = 0; i < dacl->num_aces; i++) {
1903 struct security_ace *psa1 = &dacl->aces[i];
1905 for (j = i + 1; j < dacl->num_aces; j++) {
1906 struct security_ace *psa2 = &dacl->aces[j];
1908 if (psa1->access_mask != psa2->access_mask)
1909 continue;
1911 if (!dom_sid_equal(&psa1->trustee, &psa2->trustee))
1912 continue;
1915 * Ok - permission bits and SIDs are equal.
1916 * Check if flags were re-written.
1919 if (psa1->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
1921 psa1->flags |= (psa2->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT));
1922 psa2->flags &= ~(SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT);
1924 } else if (psa2->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
1926 psa2->flags |= (psa1->flags & (SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT));
1927 psa1->flags &= ~(SEC_ACE_FLAG_CONTAINER_INHERIT|SEC_ACE_FLAG_OBJECT_INHERIT);
1933 for(i = 0; i < dacl->num_aces; i++) {
1934 struct security_ace *psa = &dacl->aces[i];
1937 * Create a canon_ace entry representing this NT DACL ACE.
1940 if ((current_ace = talloc(talloc_tos(), canon_ace)) == NULL) {
1941 free_canon_ace_list(file_ace);
1942 free_canon_ace_list(dir_ace);
1943 DEBUG(0,("create_canon_ace_lists: malloc fail.\n"));
1944 return False;
1947 ZERO_STRUCTP(current_ace);
1949 sid_copy(&current_ace->trustee, &psa->trustee);
1952 * Try and work out if the SID is a user or group
1953 * as we need to flag these differently for POSIX.
1954 * Note what kind of a POSIX ACL this should map to.
1957 if( dom_sid_equal(&current_ace->trustee, &global_sid_World)) {
1958 current_ace->owner_type = WORLD_ACE;
1959 current_ace->unix_ug.type = ID_TYPE_NOT_SPECIFIED;
1960 current_ace->unix_ug.id = -1;
1961 current_ace->type = SMB_ACL_OTHER;
1962 } else if (dom_sid_equal(&current_ace->trustee, &global_sid_Creator_Owner)) {
1963 current_ace->owner_type = UID_ACE;
1964 current_ace->unix_ug.type = ID_TYPE_UID;
1965 current_ace->unix_ug.id = pst->st_ex_uid;
1966 current_ace->type = SMB_ACL_USER_OBJ;
1969 * The Creator Owner entry only specifies inheritable permissions,
1970 * never access permissions. WinNT doesn't always set the ACE to
1971 * INHERIT_ONLY, though.
1974 psa->flags |= SEC_ACE_FLAG_INHERIT_ONLY;
1976 } else if (dom_sid_equal(&current_ace->trustee, &global_sid_Creator_Group)) {
1977 current_ace->owner_type = GID_ACE;
1978 current_ace->unix_ug.type = ID_TYPE_GID;
1979 current_ace->unix_ug.id = pst->st_ex_gid;
1980 current_ace->type = SMB_ACL_GROUP_OBJ;
1983 * The Creator Group entry only specifies inheritable permissions,
1984 * never access permissions. WinNT doesn't always set the ACE to
1985 * INHERIT_ONLY, though.
1987 psa->flags |= SEC_ACE_FLAG_INHERIT_ONLY;
1989 } else {
1990 struct unixid unixid;
1992 if (!sids_to_unixids(&current_ace->trustee, 1, &unixid)) {
1993 free_canon_ace_list(file_ace);
1994 free_canon_ace_list(dir_ace);
1995 TALLOC_FREE(current_ace);
1996 DEBUG(0, ("create_canon_ace_lists: sids_to_unixids "
1997 "failed for %s (allocation failure)\n",
1998 sid_string_dbg(&current_ace->trustee)));
1999 return false;
2002 if (unixid.type == ID_TYPE_BOTH) {
2004 * We must add both a user and group
2005 * entry POSIX_ACL.
2006 * This is due to the fact that in POSIX
2007 * user entries are more specific than
2008 * groups.
2010 current_ace->owner_type = UID_ACE;
2011 current_ace->unix_ug.type = ID_TYPE_UID;
2012 current_ace->unix_ug.id = unixid.id;
2013 current_ace->type =
2014 (unixid.id == pst->st_ex_uid) ?
2015 SMB_ACL_USER_OBJ :
2016 SMB_ACL_USER;
2018 /* Add the user object to the posix ACL,
2019 and proceed to the group mapping
2020 below. This handles the talloc_free
2021 of current_ace if not added for some
2022 reason */
2023 if (!add_current_ace_to_acl(fsp,
2024 psa,
2025 &file_ace,
2026 &dir_ace,
2027 &got_file_allow,
2028 &got_dir_allow,
2029 &all_aces_are_inherit_only,
2030 current_ace)) {
2031 free_canon_ace_list(file_ace);
2032 free_canon_ace_list(dir_ace);
2033 return false;
2036 if ((current_ace = talloc(talloc_tos(),
2037 canon_ace)) == NULL) {
2038 free_canon_ace_list(file_ace);
2039 free_canon_ace_list(dir_ace);
2040 DEBUG(0,("create_canon_ace_lists: "
2041 "malloc fail.\n"));
2042 return False;
2045 ZERO_STRUCTP(current_ace);
2047 sid_copy(&current_ace->trustee, &psa->trustee);
2049 current_ace->unix_ug.type = ID_TYPE_GID;
2050 current_ace->unix_ug.id = unixid.id;
2051 current_ace->owner_type = GID_ACE;
2052 /* If it's the primary group, this is a
2053 group_obj, not a group. */
2054 if (current_ace->unix_ug.id == pst->st_ex_gid) {
2055 current_ace->type = SMB_ACL_GROUP_OBJ;
2056 } else {
2057 current_ace->type = SMB_ACL_GROUP;
2060 } else if (unixid.type == ID_TYPE_UID) {
2061 current_ace->owner_type = UID_ACE;
2062 current_ace->unix_ug.type = ID_TYPE_UID;
2063 current_ace->unix_ug.id = unixid.id;
2064 /* If it's the owning user, this is a user_obj,
2065 not a user. */
2066 if (current_ace->unix_ug.id == pst->st_ex_uid) {
2067 current_ace->type = SMB_ACL_USER_OBJ;
2068 } else {
2069 current_ace->type = SMB_ACL_USER;
2071 } else if (unixid.type == ID_TYPE_GID) {
2072 current_ace->unix_ug.type = ID_TYPE_GID;
2073 current_ace->unix_ug.id = unixid.id;
2074 current_ace->owner_type = GID_ACE;
2075 /* If it's the primary group, this is a
2076 group_obj, not a group. */
2077 if (current_ace->unix_ug.id == pst->st_ex_gid) {
2078 current_ace->type = SMB_ACL_GROUP_OBJ;
2079 } else {
2080 current_ace->type = SMB_ACL_GROUP;
2082 } else {
2084 * Silently ignore map failures in non-mappable SIDs (NT Authority, BUILTIN etc).
2087 if (non_mappable_sid(&psa->trustee)) {
2088 DEBUG(10, ("create_canon_ace_lists: ignoring "
2089 "non-mappable SID %s\n",
2090 sid_string_dbg(&psa->trustee)));
2091 TALLOC_FREE(current_ace);
2092 continue;
2095 if (lp_force_unknown_acl_user(SNUM(fsp->conn))) {
2096 DEBUG(10, ("create_canon_ace_lists: ignoring "
2097 "unknown or foreign SID %s\n",
2098 sid_string_dbg(&psa->trustee)));
2099 TALLOC_FREE(current_ace);
2100 continue;
2103 free_canon_ace_list(file_ace);
2104 free_canon_ace_list(dir_ace);
2105 DEBUG(0, ("create_canon_ace_lists: unable to map SID "
2106 "%s to uid or gid.\n",
2107 sid_string_dbg(&current_ace->trustee)));
2108 TALLOC_FREE(current_ace);
2109 return false;
2113 /* handles the talloc_free of current_ace if not added for some reason */
2114 if (!add_current_ace_to_acl(fsp, psa, &file_ace, &dir_ace,
2115 &got_file_allow, &got_dir_allow,
2116 &all_aces_are_inherit_only, current_ace)) {
2117 free_canon_ace_list(file_ace);
2118 free_canon_ace_list(dir_ace);
2119 return false;
2123 if (fsp->is_directory && all_aces_are_inherit_only) {
2125 * Windows 2000 is doing one of these weird 'inherit acl'
2126 * traverses to conserve NTFS ACL resources. Just pretend
2127 * there was no DACL sent. JRA.
2130 DEBUG(10,("create_canon_ace_lists: Win2k inherit acl traverse. Ignoring DACL.\n"));
2131 free_canon_ace_list(file_ace);
2132 free_canon_ace_list(dir_ace);
2133 file_ace = NULL;
2134 dir_ace = NULL;
2135 } else {
2137 * Check if we have SMB_ACL_USER_OBJ and SMB_ACL_GROUP_OBJ entries in
2138 * the file ACL. If we don't have them, check if any SMB_ACL_USER/SMB_ACL_GROUP
2139 * entries can be converted to *_OBJ. Don't do this for the default
2140 * ACL, we will create them separately for this if needed inside
2141 * ensure_canon_entry_valid_on_set().
2143 if (file_ace) {
2144 check_owning_objs(file_ace, pfile_owner_sid, pfile_grp_sid);
2148 *ppfile_ace = file_ace;
2149 *ppdir_ace = dir_ace;
2151 return True;
2154 /****************************************************************************
2155 ASCII art time again... JRA :-).
2157 We have 4 cases to process when moving from an NT ACL to a POSIX ACL. Firstly,
2158 we insist the ACL is in canonical form (ie. all DENY entries preceede ALLOW
2159 entries). Secondly, the merge code has ensured that all duplicate SID entries for
2160 allow or deny have been merged, so the same SID can only appear once in the deny
2161 list or once in the allow list.
2163 We then process as follows :
2165 ---------------------------------------------------------------------------
2166 First pass - look for a Everyone DENY entry.
2168 If it is deny all (rwx) trunate the list at this point.
2169 Else, walk the list from this point and use the deny permissions of this
2170 entry as a mask on all following allow entries. Finally, delete
2171 the Everyone DENY entry (we have applied it to everything possible).
2173 In addition, in this pass we remove any DENY entries that have
2174 no permissions (ie. they are a DENY nothing).
2175 ---------------------------------------------------------------------------
2176 Second pass - only deal with deny user entries.
2178 DENY user1 (perms XXX)
2180 new_perms = 0
2181 for all following allow group entries where user1 is in group
2182 new_perms |= group_perms;
2184 user1 entry perms = new_perms & ~ XXX;
2186 Convert the deny entry to an allow entry with the new perms and
2187 push to the end of the list. Note if the user was in no groups
2188 this maps to a specific allow nothing entry for this user.
2190 The common case from the NT ACL choser (userX deny all) is
2191 optimised so we don't do the group lookup - we just map to
2192 an allow nothing entry.
2194 What we're doing here is inferring the allow permissions the
2195 person setting the ACE on user1 wanted by looking at the allow
2196 permissions on the groups the user is currently in. This will
2197 be a snapshot, depending on group membership but is the best
2198 we can do and has the advantage of failing closed rather than
2199 open.
2200 ---------------------------------------------------------------------------
2201 Third pass - only deal with deny group entries.
2203 DENY group1 (perms XXX)
2205 for all following allow user entries where user is in group1
2206 user entry perms = user entry perms & ~ XXX;
2208 If there is a group Everyone allow entry with permissions YYY,
2209 convert the group1 entry to an allow entry and modify its
2210 permissions to be :
2212 new_perms = YYY & ~ XXX
2214 and push to the end of the list.
2216 If there is no group Everyone allow entry then convert the
2217 group1 entry to a allow nothing entry and push to the end of the list.
2219 Note that the common case from the NT ACL choser (groupX deny all)
2220 cannot be optimised here as we need to modify user entries who are
2221 in the group to change them to a deny all also.
2223 What we're doing here is modifying the allow permissions of
2224 user entries (which are more specific in POSIX ACLs) to mask
2225 out the explicit deny set on the group they are in. This will
2226 be a snapshot depending on current group membership but is the
2227 best we can do and has the advantage of failing closed rather
2228 than open.
2229 ---------------------------------------------------------------------------
2230 Fourth pass - cope with cumulative permissions.
2232 for all allow user entries, if there exists an allow group entry with
2233 more permissive permissions, and the user is in that group, rewrite the
2234 allow user permissions to contain both sets of permissions.
2236 Currently the code for this is #ifdef'ed out as these semantics make
2237 no sense to me. JRA.
2238 ---------------------------------------------------------------------------
2240 Note we *MUST* do the deny user pass first as this will convert deny user
2241 entries into allow user entries which can then be processed by the deny
2242 group pass.
2244 The above algorithm took a *lot* of thinking about - hence this
2245 explaination :-). JRA.
2246 ****************************************************************************/
2248 /****************************************************************************
2249 Process a canon_ace list entries. This is very complex code. We need
2250 to go through and remove the "deny" permissions from any allow entry that matches
2251 the id of this entry. We have already refused any NT ACL that wasn't in correct
2252 order (DENY followed by ALLOW). If any allow entry ends up with zero permissions,
2253 we just remove it (to fail safe). We have already removed any duplicate ace
2254 entries. Treat an "Everyone" DENY_ACE as a special case - use it to mask all
2255 allow entries.
2256 ****************************************************************************/
2258 static void process_deny_list(connection_struct *conn, canon_ace **pp_ace_list )
2260 canon_ace *ace_list = *pp_ace_list;
2261 canon_ace *curr_ace = NULL;
2262 canon_ace *curr_ace_next = NULL;
2264 /* Pass 1 above - look for an Everyone, deny entry. */
2266 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2267 canon_ace *allow_ace_p;
2269 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2271 if (curr_ace->attr != DENY_ACE)
2272 continue;
2274 if (curr_ace->perms == (mode_t)0) {
2276 /* Deny nothing entry - delete. */
2278 DLIST_REMOVE(ace_list, curr_ace);
2279 continue;
2282 if (!dom_sid_equal(&curr_ace->trustee, &global_sid_World))
2283 continue;
2285 /* JRATEST - assert. */
2286 SMB_ASSERT(curr_ace->owner_type == WORLD_ACE);
2288 if (curr_ace->perms == ALL_ACE_PERMS) {
2291 * Optimisation. This is a DENY_ALL to Everyone. Truncate the
2292 * list at this point including this entry.
2295 canon_ace *prev_entry = DLIST_PREV(curr_ace);
2297 free_canon_ace_list( curr_ace );
2298 if (prev_entry)
2299 DLIST_REMOVE(ace_list, prev_entry);
2300 else {
2301 /* We deleted the entire list. */
2302 ace_list = NULL;
2304 break;
2307 for (allow_ace_p = curr_ace->next; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2310 * Only mask off allow entries.
2313 if (allow_ace_p->attr != ALLOW_ACE)
2314 continue;
2316 allow_ace_p->perms &= ~curr_ace->perms;
2320 * Now it's been applied, remove it.
2323 DLIST_REMOVE(ace_list, curr_ace);
2326 /* Pass 2 above - deal with deny user entries. */
2328 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2329 mode_t new_perms = (mode_t)0;
2330 canon_ace *allow_ace_p;
2332 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2334 if (curr_ace->attr != DENY_ACE)
2335 continue;
2337 if (curr_ace->owner_type != UID_ACE)
2338 continue;
2340 if (curr_ace->perms == ALL_ACE_PERMS) {
2343 * Optimisation - this is a deny everything to this user.
2344 * Convert to an allow nothing and push to the end of the list.
2347 curr_ace->attr = ALLOW_ACE;
2348 curr_ace->perms = (mode_t)0;
2349 DLIST_DEMOTE(ace_list, curr_ace);
2350 continue;
2353 for (allow_ace_p = curr_ace->next; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2355 if (allow_ace_p->attr != ALLOW_ACE)
2356 continue;
2358 /* We process GID_ACE and WORLD_ACE entries only. */
2360 if (allow_ace_p->owner_type == UID_ACE)
2361 continue;
2363 if (uid_entry_in_group(conn, curr_ace, allow_ace_p))
2364 new_perms |= allow_ace_p->perms;
2368 * Convert to a allow entry, modify the perms and push to the end
2369 * of the list.
2372 curr_ace->attr = ALLOW_ACE;
2373 curr_ace->perms = (new_perms & ~curr_ace->perms);
2374 DLIST_DEMOTE(ace_list, curr_ace);
2377 /* Pass 3 above - deal with deny group entries. */
2379 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2380 canon_ace *allow_ace_p;
2381 canon_ace *allow_everyone_p = NULL;
2383 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2385 if (curr_ace->attr != DENY_ACE)
2386 continue;
2388 if (curr_ace->owner_type != GID_ACE)
2389 continue;
2391 for (allow_ace_p = curr_ace->next; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2393 if (allow_ace_p->attr != ALLOW_ACE)
2394 continue;
2396 /* Store a pointer to the Everyone allow, if it exists. */
2397 if (allow_ace_p->owner_type == WORLD_ACE)
2398 allow_everyone_p = allow_ace_p;
2400 /* We process UID_ACE entries only. */
2402 if (allow_ace_p->owner_type != UID_ACE)
2403 continue;
2405 /* Mask off the deny group perms. */
2407 if (uid_entry_in_group(conn, allow_ace_p, curr_ace))
2408 allow_ace_p->perms &= ~curr_ace->perms;
2412 * Convert the deny to an allow with the correct perms and
2413 * push to the end of the list.
2416 curr_ace->attr = ALLOW_ACE;
2417 if (allow_everyone_p)
2418 curr_ace->perms = allow_everyone_p->perms & ~curr_ace->perms;
2419 else
2420 curr_ace->perms = (mode_t)0;
2421 DLIST_DEMOTE(ace_list, curr_ace);
2424 /* Doing this fourth pass allows Windows semantics to be layered
2425 * on top of POSIX semantics. I'm not sure if this is desirable.
2426 * For example, in W2K ACLs there is no way to say, "Group X no
2427 * access, user Y full access" if user Y is a member of group X.
2428 * This seems completely broken semantics to me.... JRA.
2431 #if 0
2432 /* Pass 4 above - deal with allow entries. */
2434 for (curr_ace = ace_list; curr_ace; curr_ace = curr_ace_next) {
2435 canon_ace *allow_ace_p;
2437 curr_ace_next = curr_ace->next; /* So we can't lose the link. */
2439 if (curr_ace->attr != ALLOW_ACE)
2440 continue;
2442 if (curr_ace->owner_type != UID_ACE)
2443 continue;
2445 for (allow_ace_p = ace_list; allow_ace_p; allow_ace_p = allow_ace_p->next) {
2447 if (allow_ace_p->attr != ALLOW_ACE)
2448 continue;
2450 /* We process GID_ACE entries only. */
2452 if (allow_ace_p->owner_type != GID_ACE)
2453 continue;
2455 /* OR in the group perms. */
2457 if (uid_entry_in_group(conn, curr_ace, allow_ace_p))
2458 curr_ace->perms |= allow_ace_p->perms;
2461 #endif
2463 *pp_ace_list = ace_list;
2466 /****************************************************************************
2467 Unpack a struct security_descriptor into two canonical ace lists. We don't depend on this
2468 succeeding.
2469 ****************************************************************************/
2471 static bool unpack_canon_ace(files_struct *fsp,
2472 const SMB_STRUCT_STAT *pst,
2473 struct dom_sid *pfile_owner_sid,
2474 struct dom_sid *pfile_grp_sid,
2475 canon_ace **ppfile_ace,
2476 canon_ace **ppdir_ace,
2477 uint32_t security_info_sent,
2478 const struct security_descriptor *psd)
2480 canon_ace *file_ace = NULL;
2481 canon_ace *dir_ace = NULL;
2483 *ppfile_ace = NULL;
2484 *ppdir_ace = NULL;
2486 if(security_info_sent == 0) {
2487 DEBUG(0,("unpack_canon_ace: no security info sent !\n"));
2488 return False;
2492 * If no DACL then this is a chown only security descriptor.
2495 if(!(security_info_sent & SECINFO_DACL) || !psd->dacl)
2496 return True;
2499 * Now go through the DACL and create the canon_ace lists.
2502 if (!create_canon_ace_lists(fsp, pst, pfile_owner_sid, pfile_grp_sid,
2503 &file_ace, &dir_ace, psd->dacl)) {
2504 return False;
2507 if ((file_ace == NULL) && (dir_ace == NULL)) {
2508 /* W2K traverse DACL set - ignore. */
2509 return True;
2513 * Go through the canon_ace list and merge entries
2514 * belonging to identical users of identical allow or deny type.
2515 * We can do this as all deny entries come first, followed by
2516 * all allow entries (we have mandated this before accepting this acl).
2519 print_canon_ace_list( "file ace - before merge", file_ace);
2520 merge_aces( &file_ace, false);
2522 print_canon_ace_list( "dir ace - before merge", dir_ace);
2523 merge_aces( &dir_ace, true);
2526 * NT ACLs are order dependent. Go through the acl lists and
2527 * process DENY entries by masking the allow entries.
2530 print_canon_ace_list( "file ace - before deny", file_ace);
2531 process_deny_list(fsp->conn, &file_ace);
2533 print_canon_ace_list( "dir ace - before deny", dir_ace);
2534 process_deny_list(fsp->conn, &dir_ace);
2537 * A well formed POSIX file or default ACL has at least 3 entries, a
2538 * SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER_OBJ
2539 * and optionally a mask entry. Ensure this is the case.
2542 print_canon_ace_list( "file ace - before valid", file_ace);
2544 if (!ensure_canon_entry_valid_on_set(fsp->conn, &file_ace, false, fsp->conn->params,
2545 fsp->is_directory, pfile_owner_sid, pfile_grp_sid, pst)) {
2546 free_canon_ace_list(file_ace);
2547 free_canon_ace_list(dir_ace);
2548 return False;
2551 print_canon_ace_list( "dir ace - before valid", dir_ace);
2553 if (dir_ace && !ensure_canon_entry_valid_on_set(fsp->conn, &dir_ace, true, fsp->conn->params,
2554 fsp->is_directory, pfile_owner_sid, pfile_grp_sid, pst)) {
2555 free_canon_ace_list(file_ace);
2556 free_canon_ace_list(dir_ace);
2557 return False;
2560 print_canon_ace_list( "file ace - return", file_ace);
2561 print_canon_ace_list( "dir ace - return", dir_ace);
2563 *ppfile_ace = file_ace;
2564 *ppdir_ace = dir_ace;
2565 return True;
2569 /******************************************************************************
2570 When returning permissions, try and fit NT display
2571 semantics if possible. Note the the canon_entries here must have been malloced.
2572 The list format should be - first entry = owner, followed by group and other user
2573 entries, last entry = other.
2575 Note that this doesn't exactly match the NT semantics for an ACL. As POSIX entries
2576 are not ordered, and match on the most specific entry rather than walking a list,
2577 then a simple POSIX permission of rw-r--r-- should really map to 5 entries,
2579 Entry 0: owner : deny all except read and write.
2580 Entry 1: owner : allow read and write.
2581 Entry 2: group : deny all except read.
2582 Entry 3: group : allow read.
2583 Entry 4: Everyone : allow read.
2585 But NT cannot display this in their ACL editor !
2586 ********************************************************************************/
2588 static void arrange_posix_perms(const char *filename, canon_ace **pp_list_head)
2590 canon_ace *l_head = *pp_list_head;
2591 canon_ace *owner_ace = NULL;
2592 canon_ace *other_ace = NULL;
2593 canon_ace *ace = NULL;
2595 for (ace = l_head; ace; ace = ace->next) {
2596 if (ace->type == SMB_ACL_USER_OBJ)
2597 owner_ace = ace;
2598 else if (ace->type == SMB_ACL_OTHER) {
2599 /* Last ace - this is "other" */
2600 other_ace = ace;
2604 if (!owner_ace || !other_ace) {
2605 DEBUG(0,("arrange_posix_perms: Invalid POSIX permissions for file %s, missing owner or other.\n",
2606 filename ));
2607 return;
2611 * The POSIX algorithm applies to owner first, and other last,
2612 * so ensure they are arranged in this order.
2615 if (owner_ace) {
2616 DLIST_PROMOTE(l_head, owner_ace);
2619 if (other_ace) {
2620 DLIST_DEMOTE(l_head, other_ace);
2623 /* We have probably changed the head of the list. */
2625 *pp_list_head = l_head;
2628 /****************************************************************************
2629 Create a linked list of canonical ACE entries.
2630 ****************************************************************************/
2632 static canon_ace *canonicalise_acl(struct connection_struct *conn,
2633 const char *fname, SMB_ACL_T posix_acl,
2634 const SMB_STRUCT_STAT *psbuf,
2635 const struct dom_sid *powner, const struct dom_sid *pgroup, struct pai_val *pal, SMB_ACL_TYPE_T the_acl_type)
2637 mode_t acl_mask = (S_IRUSR|S_IWUSR|S_IXUSR);
2638 canon_ace *l_head = NULL;
2639 canon_ace *ace = NULL;
2640 canon_ace *next_ace = NULL;
2641 int entry_id = SMB_ACL_FIRST_ENTRY;
2642 bool is_default_acl = (the_acl_type == SMB_ACL_TYPE_DEFAULT);
2643 SMB_ACL_ENTRY_T entry;
2644 size_t ace_count;
2646 while ( posix_acl && (sys_acl_get_entry(posix_acl, entry_id, &entry) == 1)) {
2647 SMB_ACL_TAG_T tagtype;
2648 SMB_ACL_PERMSET_T permset;
2649 struct dom_sid sid;
2650 struct unixid unix_ug;
2651 enum ace_owner owner_type;
2653 entry_id = SMB_ACL_NEXT_ENTRY;
2655 if (sys_acl_get_tag_type(entry, &tagtype) == -1)
2656 continue;
2658 if (sys_acl_get_permset(entry, &permset) == -1)
2659 continue;
2661 /* Decide which SID to use based on the ACL type. */
2662 switch(tagtype) {
2663 case SMB_ACL_USER_OBJ:
2664 /* Get the SID from the owner. */
2665 sid_copy(&sid, powner);
2666 unix_ug.type = ID_TYPE_UID;
2667 unix_ug.id = psbuf->st_ex_uid;
2668 owner_type = UID_ACE;
2669 break;
2670 case SMB_ACL_USER:
2672 uid_t *puid = (uid_t *)sys_acl_get_qualifier(entry);
2673 if (puid == NULL) {
2674 DEBUG(0,("canonicalise_acl: Failed to get uid.\n"));
2675 continue;
2677 uid_to_sid( &sid, *puid);
2678 unix_ug.type = ID_TYPE_UID;
2679 unix_ug.id = *puid;
2680 owner_type = UID_ACE;
2681 break;
2683 case SMB_ACL_GROUP_OBJ:
2684 /* Get the SID from the owning group. */
2685 sid_copy(&sid, pgroup);
2686 unix_ug.type = ID_TYPE_GID;
2687 unix_ug.id = psbuf->st_ex_gid;
2688 owner_type = GID_ACE;
2689 break;
2690 case SMB_ACL_GROUP:
2692 gid_t *pgid = (gid_t *)sys_acl_get_qualifier(entry);
2693 if (pgid == NULL) {
2694 DEBUG(0,("canonicalise_acl: Failed to get gid.\n"));
2695 continue;
2697 gid_to_sid( &sid, *pgid);
2698 unix_ug.type = ID_TYPE_GID;
2699 unix_ug.id = *pgid;
2700 owner_type = GID_ACE;
2701 break;
2703 case SMB_ACL_MASK:
2704 acl_mask = convert_permset_to_mode_t(permset);
2705 continue; /* Don't count the mask as an entry. */
2706 case SMB_ACL_OTHER:
2707 /* Use the Everyone SID */
2708 sid = global_sid_World;
2709 unix_ug.type = ID_TYPE_NOT_SPECIFIED;
2710 unix_ug.id = -1;
2711 owner_type = WORLD_ACE;
2712 break;
2713 default:
2714 DEBUG(0,("canonicalise_acl: Unknown tagtype %u\n", (unsigned int)tagtype));
2715 continue;
2719 * Add this entry to the list.
2722 if ((ace = talloc(talloc_tos(), canon_ace)) == NULL)
2723 goto fail;
2725 *ace = (canon_ace) {
2726 .type = tagtype,
2727 .perms = convert_permset_to_mode_t(permset),
2728 .attr = ALLOW_ACE,
2729 .trustee = sid,
2730 .unix_ug = unix_ug,
2731 .owner_type = owner_type
2733 ace->ace_flags = get_pai_flags(pal, ace, is_default_acl);
2735 DLIST_ADD(l_head, ace);
2739 * This next call will ensure we have at least a user/group/world set.
2742 if (!ensure_canon_entry_valid_on_get(conn, &l_head,
2743 powner, pgroup,
2744 psbuf))
2745 goto fail;
2748 * Now go through the list, masking the permissions with the
2749 * acl_mask. Ensure all DENY Entries are at the start of the list.
2752 DEBUG(10,("canonicalise_acl: %s ace entries before arrange :\n", is_default_acl ? "Default" : "Access"));
2754 for ( ace_count = 0, ace = l_head; ace; ace = next_ace, ace_count++) {
2755 next_ace = ace->next;
2757 /* Masks are only applied to entries other than USER_OBJ and OTHER. */
2758 if (ace->type != SMB_ACL_OTHER && ace->type != SMB_ACL_USER_OBJ)
2759 ace->perms &= acl_mask;
2761 if (ace->perms == 0) {
2762 DLIST_PROMOTE(l_head, ace);
2765 if( DEBUGLVL( 10 ) ) {
2766 print_canon_ace(ace, ace_count);
2770 arrange_posix_perms(fname,&l_head );
2772 print_canon_ace_list( "canonicalise_acl: ace entries after arrange", l_head );
2774 return l_head;
2776 fail:
2778 free_canon_ace_list(l_head);
2779 return NULL;
2782 /****************************************************************************
2783 Check if the current user group list contains a given group.
2784 ****************************************************************************/
2786 bool current_user_in_group(connection_struct *conn, gid_t gid)
2788 uint32_t i;
2789 const struct security_unix_token *utok = get_current_utok(conn);
2791 for (i = 0; i < utok->ngroups; i++) {
2792 if (utok->groups[i] == gid) {
2793 return True;
2797 return False;
2800 /****************************************************************************
2801 Should we override a deny ? Check 'acl group control' and 'dos filemode'.
2802 ****************************************************************************/
2804 static bool acl_group_override(connection_struct *conn,
2805 const struct smb_filename *smb_fname)
2807 if ((errno != EPERM) && (errno != EACCES)) {
2808 return false;
2811 /* file primary group == user primary or supplementary group */
2812 if (lp_acl_group_control(SNUM(conn)) &&
2813 current_user_in_group(conn, smb_fname->st.st_ex_gid)) {
2814 return true;
2817 /* user has writeable permission */
2818 if (lp_dos_filemode(SNUM(conn)) &&
2819 can_write_to_file(conn, smb_fname)) {
2820 return true;
2823 return false;
2826 /****************************************************************************
2827 Attempt to apply an ACL to a file or directory.
2828 ****************************************************************************/
2830 static bool set_canon_ace_list(files_struct *fsp,
2831 canon_ace *the_ace,
2832 bool default_ace,
2833 const SMB_STRUCT_STAT *psbuf,
2834 bool *pacl_set_support)
2836 connection_struct *conn = fsp->conn;
2837 bool ret = False;
2838 SMB_ACL_T the_acl = sys_acl_init(talloc_tos());
2839 canon_ace *p_ace;
2840 int i;
2841 SMB_ACL_ENTRY_T mask_entry;
2842 bool got_mask_entry = False;
2843 SMB_ACL_PERMSET_T mask_permset;
2844 SMB_ACL_TYPE_T the_acl_type = (default_ace ? SMB_ACL_TYPE_DEFAULT : SMB_ACL_TYPE_ACCESS);
2845 bool needs_mask = False;
2846 mode_t mask_perms = 0;
2848 /* Use the psbuf that was passed in. */
2849 if (psbuf != &fsp->fsp_name->st) {
2850 fsp->fsp_name->st = *psbuf;
2853 #if defined(POSIX_ACL_NEEDS_MASK)
2854 /* HP-UX always wants to have a mask (called "class" there). */
2855 needs_mask = True;
2856 #endif
2858 if (the_acl == NULL) {
2859 DEBUG(0, ("sys_acl_init failed to allocate an ACL\n"));
2860 return false;
2863 if( DEBUGLVL( 10 )) {
2864 dbgtext("set_canon_ace_list: setting ACL:\n");
2865 for (i = 0, p_ace = the_ace; p_ace; p_ace = p_ace->next, i++ ) {
2866 print_canon_ace( p_ace, i);
2870 for (i = 0, p_ace = the_ace; p_ace; p_ace = p_ace->next, i++ ) {
2871 SMB_ACL_ENTRY_T the_entry;
2872 SMB_ACL_PERMSET_T the_permset;
2875 * ACLs only "need" an ACL_MASK entry if there are any named user or
2876 * named group entries. But if there is an ACL_MASK entry, it applies
2877 * to ACL_USER, ACL_GROUP, and ACL_GROUP_OBJ entries. Set the mask
2878 * so that it doesn't deny (i.e., mask off) any permissions.
2881 if (p_ace->type == SMB_ACL_USER || p_ace->type == SMB_ACL_GROUP) {
2882 needs_mask = True;
2883 mask_perms |= p_ace->perms;
2884 } else if (p_ace->type == SMB_ACL_GROUP_OBJ) {
2885 mask_perms |= p_ace->perms;
2889 * Get the entry for this ACE.
2892 if (sys_acl_create_entry(&the_acl, &the_entry) == -1) {
2893 DEBUG(0,("set_canon_ace_list: Failed to create entry %d. (%s)\n",
2894 i, strerror(errno) ));
2895 goto fail;
2898 if (p_ace->type == SMB_ACL_MASK) {
2899 mask_entry = the_entry;
2900 got_mask_entry = True;
2904 * Ok - we now know the ACL calls should be working, don't
2905 * allow fallback to chmod.
2908 *pacl_set_support = True;
2911 * Initialise the entry from the canon_ace.
2915 * First tell the entry what type of ACE this is.
2918 if (sys_acl_set_tag_type(the_entry, p_ace->type) == -1) {
2919 DEBUG(0,("set_canon_ace_list: Failed to set tag type on entry %d. (%s)\n",
2920 i, strerror(errno) ));
2921 goto fail;
2925 * Only set the qualifier (user or group id) if the entry is a user
2926 * or group id ACE.
2929 if ((p_ace->type == SMB_ACL_USER) || (p_ace->type == SMB_ACL_GROUP)) {
2930 if (sys_acl_set_qualifier(the_entry,(void *)&p_ace->unix_ug.id) == -1) {
2931 DEBUG(0,("set_canon_ace_list: Failed to set qualifier on entry %d. (%s)\n",
2932 i, strerror(errno) ));
2933 goto fail;
2938 * Convert the mode_t perms in the canon_ace to a POSIX permset.
2941 if (sys_acl_get_permset(the_entry, &the_permset) == -1) {
2942 DEBUG(0,("set_canon_ace_list: Failed to get permset on entry %d. (%s)\n",
2943 i, strerror(errno) ));
2944 goto fail;
2947 if (map_acl_perms_to_permset(p_ace->perms, &the_permset) == -1) {
2948 DEBUG(0,("set_canon_ace_list: Failed to create permset for mode (%u) on entry %d. (%s)\n",
2949 (unsigned int)p_ace->perms, i, strerror(errno) ));
2950 goto fail;
2954 * ..and apply them to the entry.
2957 if (sys_acl_set_permset(the_entry, the_permset) == -1) {
2958 DEBUG(0,("set_canon_ace_list: Failed to add permset on entry %d. (%s)\n",
2959 i, strerror(errno) ));
2960 goto fail;
2963 if( DEBUGLVL( 10 ))
2964 print_canon_ace( p_ace, i);
2968 if (needs_mask && !got_mask_entry) {
2969 if (sys_acl_create_entry(&the_acl, &mask_entry) == -1) {
2970 DEBUG(0,("set_canon_ace_list: Failed to create mask entry. (%s)\n", strerror(errno) ));
2971 goto fail;
2974 if (sys_acl_set_tag_type(mask_entry, SMB_ACL_MASK) == -1) {
2975 DEBUG(0,("set_canon_ace_list: Failed to set tag type on mask entry. (%s)\n",strerror(errno) ));
2976 goto fail;
2979 if (sys_acl_get_permset(mask_entry, &mask_permset) == -1) {
2980 DEBUG(0,("set_canon_ace_list: Failed to get mask permset. (%s)\n", strerror(errno) ));
2981 goto fail;
2984 if (map_acl_perms_to_permset(S_IRUSR|S_IWUSR|S_IXUSR, &mask_permset) == -1) {
2985 DEBUG(0,("set_canon_ace_list: Failed to create mask permset. (%s)\n", strerror(errno) ));
2986 goto fail;
2989 if (sys_acl_set_permset(mask_entry, mask_permset) == -1) {
2990 DEBUG(0,("set_canon_ace_list: Failed to add mask permset. (%s)\n", strerror(errno) ));
2991 goto fail;
2996 * Finally apply it to the file or directory.
2999 if(default_ace || fsp->is_directory || fsp->fh->fd == -1) {
3000 if (SMB_VFS_SYS_ACL_SET_FILE(conn, fsp->fsp_name,
3001 the_acl_type, the_acl) == -1) {
3003 * Some systems allow all the above calls and only fail with no ACL support
3004 * when attempting to apply the acl. HPUX with HFS is an example of this. JRA.
3006 if (no_acl_syscall_error(errno)) {
3007 *pacl_set_support = False;
3010 if (acl_group_override(conn, fsp->fsp_name)) {
3011 int sret;
3013 DEBUG(5,("set_canon_ace_list: acl group "
3014 "control on and current user in file "
3015 "%s primary group.\n",
3016 fsp_str_dbg(fsp)));
3018 become_root();
3019 sret = SMB_VFS_SYS_ACL_SET_FILE(conn,
3020 fsp->fsp_name, the_acl_type,
3021 the_acl);
3022 unbecome_root();
3023 if (sret == 0) {
3024 ret = True;
3028 if (ret == False) {
3029 DEBUG(2,("set_canon_ace_list: "
3030 "sys_acl_set_file type %s failed for "
3031 "file %s (%s).\n",
3032 the_acl_type == SMB_ACL_TYPE_DEFAULT ?
3033 "directory default" : "file",
3034 fsp_str_dbg(fsp), strerror(errno)));
3035 goto fail;
3038 } else {
3039 if (SMB_VFS_SYS_ACL_SET_FD(fsp, the_acl) == -1) {
3041 * Some systems allow all the above calls and only fail with no ACL support
3042 * when attempting to apply the acl. HPUX with HFS is an example of this. JRA.
3044 if (no_acl_syscall_error(errno)) {
3045 *pacl_set_support = False;
3048 if (acl_group_override(conn, fsp->fsp_name)) {
3049 int sret;
3051 DEBUG(5,("set_canon_ace_list: acl group "
3052 "control on and current user in file "
3053 "%s primary group.\n",
3054 fsp_str_dbg(fsp)));
3056 become_root();
3057 sret = SMB_VFS_SYS_ACL_SET_FD(fsp, the_acl);
3058 unbecome_root();
3059 if (sret == 0) {
3060 ret = True;
3064 if (ret == False) {
3065 DEBUG(2,("set_canon_ace_list: "
3066 "sys_acl_set_file failed for file %s "
3067 "(%s).\n",
3068 fsp_str_dbg(fsp), strerror(errno)));
3069 goto fail;
3074 ret = True;
3076 fail:
3078 if (the_acl != NULL) {
3079 TALLOC_FREE(the_acl);
3082 return ret;
3085 /****************************************************************************
3087 ****************************************************************************/
3089 SMB_ACL_T free_empty_sys_acl(connection_struct *conn, SMB_ACL_T the_acl)
3091 SMB_ACL_ENTRY_T entry;
3093 if (!the_acl)
3094 return NULL;
3095 if (sys_acl_get_entry(the_acl, SMB_ACL_FIRST_ENTRY, &entry) != 1) {
3096 TALLOC_FREE(the_acl);
3097 return NULL;
3099 return the_acl;
3102 /****************************************************************************
3103 Convert a canon_ace to a generic 3 element permission - if possible.
3104 ****************************************************************************/
3106 #define MAP_PERM(p,mask,result) (((p) & (mask)) ? (result) : 0 )
3108 static bool convert_canon_ace_to_posix_perms( files_struct *fsp, canon_ace *file_ace_list, mode_t *posix_perms)
3110 size_t ace_count = count_canon_ace_list(file_ace_list);
3111 canon_ace *ace_p;
3112 canon_ace *owner_ace = NULL;
3113 canon_ace *group_ace = NULL;
3114 canon_ace *other_ace = NULL;
3116 if (ace_count > 5) {
3117 DEBUG(3,("convert_canon_ace_to_posix_perms: Too many ACE "
3118 "entries for file %s to convert to posix perms.\n",
3119 fsp_str_dbg(fsp)));
3120 return False;
3123 for (ace_p = file_ace_list; ace_p; ace_p = ace_p->next) {
3124 if (ace_p->owner_type == UID_ACE)
3125 owner_ace = ace_p;
3126 else if (ace_p->owner_type == GID_ACE)
3127 group_ace = ace_p;
3128 else if (ace_p->owner_type == WORLD_ACE)
3129 other_ace = ace_p;
3132 if (!owner_ace || !group_ace || !other_ace) {
3133 DEBUG(3,("convert_canon_ace_to_posix_perms: Can't get "
3134 "standard entries for file %s.\n", fsp_str_dbg(fsp)));
3135 return False;
3139 * Ensure all ACE entries are owner, group or other.
3140 * We can't set if there are any other SIDs.
3142 for (ace_p = file_ace_list; ace_p; ace_p = ace_p->next) {
3143 if (ace_p == owner_ace || ace_p == group_ace ||
3144 ace_p == other_ace) {
3145 continue;
3147 if (ace_p->owner_type == UID_ACE) {
3148 if (ace_p->unix_ug.id != owner_ace->unix_ug.id) {
3149 DEBUG(3,("Invalid uid %u in ACE for file %s.\n",
3150 (unsigned int)ace_p->unix_ug.id,
3151 fsp_str_dbg(fsp)));
3152 return false;
3154 } else if (ace_p->owner_type == GID_ACE) {
3155 if (ace_p->unix_ug.id != group_ace->unix_ug.id) {
3156 DEBUG(3,("Invalid gid %u in ACE for file %s.\n",
3157 (unsigned int)ace_p->unix_ug.id,
3158 fsp_str_dbg(fsp)));
3159 return false;
3161 } else {
3163 * There should be no duplicate WORLD_ACE entries.
3166 DEBUG(3,("Invalid type %u, uid %u in "
3167 "ACE for file %s.\n",
3168 (unsigned int)ace_p->owner_type,
3169 (unsigned int)ace_p->unix_ug.id,
3170 fsp_str_dbg(fsp)));
3171 return false;
3175 *posix_perms = (mode_t)0;
3177 *posix_perms |= owner_ace->perms;
3178 *posix_perms |= MAP_PERM(group_ace->perms, S_IRUSR, S_IRGRP);
3179 *posix_perms |= MAP_PERM(group_ace->perms, S_IWUSR, S_IWGRP);
3180 *posix_perms |= MAP_PERM(group_ace->perms, S_IXUSR, S_IXGRP);
3181 *posix_perms |= MAP_PERM(other_ace->perms, S_IRUSR, S_IROTH);
3182 *posix_perms |= MAP_PERM(other_ace->perms, S_IWUSR, S_IWOTH);
3183 *posix_perms |= MAP_PERM(other_ace->perms, S_IXUSR, S_IXOTH);
3185 /* The owner must have at least read access. */
3187 *posix_perms |= S_IRUSR;
3188 if (fsp->is_directory)
3189 *posix_perms |= (S_IWUSR|S_IXUSR);
3191 DEBUG(10,("convert_canon_ace_to_posix_perms: converted u=%o,g=%o,w=%o "
3192 "to perm=0%o for file %s.\n", (int)owner_ace->perms,
3193 (int)group_ace->perms, (int)other_ace->perms,
3194 (int)*posix_perms, fsp_str_dbg(fsp)));
3196 return True;
3199 /****************************************************************************
3200 Incoming NT ACLs on a directory can be split into a default POSIX acl (CI|OI|IO) and
3201 a normal POSIX acl. Win2k needs these split acls re-merging into one ACL
3202 with CI|OI set so it is inherited and also applies to the directory.
3203 Based on code from "Jim McDonough" <jmcd@us.ibm.com>.
3204 ****************************************************************************/
3206 static size_t merge_default_aces( struct security_ace *nt_ace_list, size_t num_aces)
3208 size_t i, j;
3210 for (i = 0; i < num_aces; i++) {
3211 for (j = i+1; j < num_aces; j++) {
3212 uint32_t i_flags_ni = (nt_ace_list[i].flags & ~SEC_ACE_FLAG_INHERITED_ACE);
3213 uint32_t j_flags_ni = (nt_ace_list[j].flags & ~SEC_ACE_FLAG_INHERITED_ACE);
3214 bool i_inh = (nt_ace_list[i].flags & SEC_ACE_FLAG_INHERITED_ACE) ? True : False;
3215 bool j_inh = (nt_ace_list[j].flags & SEC_ACE_FLAG_INHERITED_ACE) ? True : False;
3217 /* We know the lower number ACE's are file entries. */
3218 if ((nt_ace_list[i].type == nt_ace_list[j].type) &&
3219 (nt_ace_list[i].size == nt_ace_list[j].size) &&
3220 (nt_ace_list[i].access_mask == nt_ace_list[j].access_mask) &&
3221 dom_sid_equal(&nt_ace_list[i].trustee, &nt_ace_list[j].trustee) &&
3222 (i_inh == j_inh) &&
3223 (i_flags_ni == 0) &&
3224 (j_flags_ni == (SEC_ACE_FLAG_OBJECT_INHERIT|
3225 SEC_ACE_FLAG_CONTAINER_INHERIT|
3226 SEC_ACE_FLAG_INHERIT_ONLY))) {
3228 * W2K wants to have access allowed zero access ACE's
3229 * at the end of the list. If the mask is zero, merge
3230 * the non-inherited ACE onto the inherited ACE.
3233 if (nt_ace_list[i].access_mask == 0) {
3234 nt_ace_list[j].flags = SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT|
3235 (i_inh ? SEC_ACE_FLAG_INHERITED_ACE : 0);
3236 if (num_aces - i - 1 > 0)
3237 memmove(&nt_ace_list[i], &nt_ace_list[i+1], (num_aces-i-1) *
3238 sizeof(struct security_ace));
3240 DEBUG(10,("merge_default_aces: Merging zero access ACE %u onto ACE %u.\n",
3241 (unsigned int)i, (unsigned int)j ));
3242 } else {
3244 * These are identical except for the flags.
3245 * Merge the inherited ACE onto the non-inherited ACE.
3248 nt_ace_list[i].flags = SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT|
3249 (i_inh ? SEC_ACE_FLAG_INHERITED_ACE : 0);
3250 if (num_aces - j - 1 > 0)
3251 memmove(&nt_ace_list[j], &nt_ace_list[j+1], (num_aces-j-1) *
3252 sizeof(struct security_ace));
3254 DEBUG(10,("merge_default_aces: Merging ACE %u onto ACE %u.\n",
3255 (unsigned int)j, (unsigned int)i ));
3257 num_aces--;
3258 break;
3263 return num_aces;
3267 /****************************************************************************
3268 Reply to query a security descriptor from an fsp. If it succeeds it allocates
3269 the space for the return elements and returns the size needed to return the
3270 security descriptor. This should be the only external function needed for
3271 the UNIX style get ACL.
3272 ****************************************************************************/
3274 static NTSTATUS posix_get_nt_acl_common(struct connection_struct *conn,
3275 const char *name,
3276 const SMB_STRUCT_STAT *sbuf,
3277 struct pai_val *pal,
3278 SMB_ACL_T posix_acl,
3279 SMB_ACL_T def_acl,
3280 uint32_t security_info,
3281 TALLOC_CTX *mem_ctx,
3282 struct security_descriptor **ppdesc)
3284 struct dom_sid owner_sid;
3285 struct dom_sid group_sid;
3286 size_t sd_size = 0;
3287 struct security_acl *psa = NULL;
3288 size_t num_acls = 0;
3289 size_t num_def_acls = 0;
3290 size_t num_aces = 0;
3291 canon_ace *file_ace = NULL;
3292 canon_ace *dir_ace = NULL;
3293 struct security_ace *nt_ace_list = NULL;
3294 struct security_descriptor *psd = NULL;
3297 * Get the owner, group and world SIDs.
3300 create_file_sids(sbuf, &owner_sid, &group_sid);
3302 if (security_info & SECINFO_DACL) {
3305 * In the optimum case Creator Owner and Creator Group would be used for
3306 * the ACL_USER_OBJ and ACL_GROUP_OBJ entries, respectively, but this
3307 * would lead to usability problems under Windows: The Creator entries
3308 * are only available in browse lists of directories and not for files;
3309 * additionally the identity of the owning group couldn't be determined.
3310 * We therefore use those identities only for Default ACLs.
3313 /* Create the canon_ace lists. */
3314 file_ace = canonicalise_acl(conn, name, posix_acl, sbuf,
3315 &owner_sid, &group_sid, pal,
3316 SMB_ACL_TYPE_ACCESS);
3318 /* We must have *some* ACLS. */
3320 if (count_canon_ace_list(file_ace) == 0) {
3321 DEBUG(0,("get_nt_acl : No ACLs on file (%s) !\n", name));
3322 goto done;
3325 if (S_ISDIR(sbuf->st_ex_mode) && def_acl) {
3326 dir_ace = canonicalise_acl(conn, name, def_acl,
3327 sbuf,
3328 &global_sid_Creator_Owner,
3329 &global_sid_Creator_Group,
3330 pal, SMB_ACL_TYPE_DEFAULT);
3334 * Create the NT ACE list from the canonical ace lists.
3338 canon_ace *ace;
3339 enum security_ace_type nt_acl_type;
3341 num_acls = count_canon_ace_list(file_ace);
3342 num_def_acls = count_canon_ace_list(dir_ace);
3344 nt_ace_list = talloc_zero_array(
3345 talloc_tos(), struct security_ace,
3346 num_acls + num_def_acls);
3348 if (nt_ace_list == NULL) {
3349 DEBUG(0,("get_nt_acl: Unable to malloc space for nt_ace_list.\n"));
3350 goto done;
3354 * Create the NT ACE list from the canonical ace lists.
3357 for (ace = file_ace; ace != NULL; ace = ace->next) {
3358 uint32_t acc = map_canon_ace_perms(SNUM(conn),
3359 &nt_acl_type,
3360 ace->perms,
3361 S_ISDIR(sbuf->st_ex_mode));
3362 init_sec_ace(&nt_ace_list[num_aces++],
3363 &ace->trustee,
3364 nt_acl_type,
3365 acc,
3366 ace->ace_flags);
3369 for (ace = dir_ace; ace != NULL; ace = ace->next) {
3370 uint32_t acc = map_canon_ace_perms(SNUM(conn),
3371 &nt_acl_type,
3372 ace->perms,
3373 S_ISDIR(sbuf->st_ex_mode));
3374 init_sec_ace(&nt_ace_list[num_aces++],
3375 &ace->trustee,
3376 nt_acl_type,
3377 acc,
3378 ace->ace_flags |
3379 SEC_ACE_FLAG_OBJECT_INHERIT|
3380 SEC_ACE_FLAG_CONTAINER_INHERIT|
3381 SEC_ACE_FLAG_INHERIT_ONLY);
3385 * Merge POSIX default ACLs and normal ACLs into one NT ACE.
3386 * Win2K needs this to get the inheritance correct when replacing ACLs
3387 * on a directory tree. Based on work by Jim @ IBM.
3390 num_aces = merge_default_aces(nt_ace_list, num_aces);
3393 if (num_aces) {
3394 if((psa = make_sec_acl( talloc_tos(), NT4_ACL_REVISION, num_aces, nt_ace_list)) == NULL) {
3395 DEBUG(0,("get_nt_acl: Unable to malloc space for acl.\n"));
3396 goto done;
3399 } /* security_info & SECINFO_DACL */
3401 psd = make_standard_sec_desc(mem_ctx,
3402 (security_info & SECINFO_OWNER) ? &owner_sid : NULL,
3403 (security_info & SECINFO_GROUP) ? &group_sid : NULL,
3404 psa,
3405 &sd_size);
3407 if(!psd) {
3408 DEBUG(0,("get_nt_acl: Unable to malloc space for security descriptor.\n"));
3409 sd_size = 0;
3410 goto done;
3414 * Windows 2000: The DACL_PROTECTED flag in the security
3415 * descriptor marks the ACL as non-inheriting, i.e., no
3416 * ACEs from higher level directories propagate to this
3417 * ACL. In the POSIX ACL model permissions are only
3418 * inherited at file create time, so ACLs never contain
3419 * any ACEs that are inherited dynamically. The DACL_PROTECTED
3420 * flag doesn't seem to bother Windows NT.
3421 * Always set this if map acl inherit is turned off.
3423 if (pal == NULL || !lp_map_acl_inherit(SNUM(conn))) {
3424 psd->type |= SEC_DESC_DACL_PROTECTED;
3425 } else {
3426 psd->type |= pal->sd_type;
3429 if (psd->dacl) {
3430 dacl_sort_into_canonical_order(psd->dacl->aces, (unsigned int)psd->dacl->num_aces);
3433 *ppdesc = psd;
3435 done:
3437 if (posix_acl) {
3438 TALLOC_FREE(posix_acl);
3440 if (def_acl) {
3441 TALLOC_FREE(def_acl);
3443 free_canon_ace_list(file_ace);
3444 free_canon_ace_list(dir_ace);
3445 free_inherited_info(pal);
3446 TALLOC_FREE(nt_ace_list);
3448 return NT_STATUS_OK;
3451 NTSTATUS posix_fget_nt_acl(struct files_struct *fsp, uint32_t security_info,
3452 TALLOC_CTX *mem_ctx,
3453 struct security_descriptor **ppdesc)
3455 SMB_STRUCT_STAT sbuf;
3456 SMB_ACL_T posix_acl = NULL;
3457 struct pai_val *pal;
3458 TALLOC_CTX *frame = talloc_stackframe();
3459 NTSTATUS status;
3461 *ppdesc = NULL;
3463 DEBUG(10,("posix_fget_nt_acl: called for file %s\n",
3464 fsp_str_dbg(fsp)));
3466 /* can it happen that fsp_name == NULL ? */
3467 if (fsp->is_directory || fsp->fh->fd == -1) {
3468 status = posix_get_nt_acl(fsp->conn, fsp->fsp_name,
3469 security_info, mem_ctx, ppdesc);
3470 TALLOC_FREE(frame);
3471 return status;
3474 /* Get the stat struct for the owner info. */
3475 if(SMB_VFS_FSTAT(fsp, &sbuf) != 0) {
3476 TALLOC_FREE(frame);
3477 return map_nt_error_from_unix(errno);
3480 /* Get the ACL from the fd. */
3481 posix_acl = SMB_VFS_SYS_ACL_GET_FD(fsp, frame);
3483 pal = fload_inherited_info(fsp);
3485 status = posix_get_nt_acl_common(fsp->conn, fsp->fsp_name->base_name,
3486 &sbuf, pal, posix_acl, NULL,
3487 security_info, mem_ctx, ppdesc);
3488 TALLOC_FREE(frame);
3489 return status;
3492 NTSTATUS posix_get_nt_acl(struct connection_struct *conn,
3493 const struct smb_filename *smb_fname_in,
3494 uint32_t security_info,
3495 TALLOC_CTX *mem_ctx,
3496 struct security_descriptor **ppdesc)
3498 SMB_ACL_T posix_acl = NULL;
3499 SMB_ACL_T def_acl = NULL;
3500 struct pai_val *pal;
3501 struct smb_filename *smb_fname = NULL;
3502 int ret;
3503 TALLOC_CTX *frame = talloc_stackframe();
3504 NTSTATUS status;
3506 *ppdesc = NULL;
3508 DEBUG(10,("posix_get_nt_acl: called for file %s\n",
3509 smb_fname_in->base_name ));
3511 smb_fname = cp_smb_filename(talloc_tos(), smb_fname_in);
3512 if (smb_fname == NULL) {
3513 TALLOC_FREE(frame);
3514 return NT_STATUS_NO_MEMORY;
3517 /* Get the stat struct for the owner info. */
3519 * We can directly use SMB_VFS_STAT here, as if this was a
3520 * POSIX call on a symlink, we've already refused it.
3521 * For a Windows acl mapped call on a symlink, we want to follow
3522 * it.
3524 ret = SMB_VFS_STAT(conn, smb_fname);
3526 if (ret == -1) {
3527 TALLOC_FREE(frame);
3528 return map_nt_error_from_unix(errno);
3531 /* Get the ACL from the path. */
3532 posix_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, smb_fname,
3533 SMB_ACL_TYPE_ACCESS, frame);
3535 /* If it's a directory get the default POSIX ACL. */
3536 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
3537 def_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, smb_fname,
3538 SMB_ACL_TYPE_DEFAULT, frame);
3539 def_acl = free_empty_sys_acl(conn, def_acl);
3542 pal = load_inherited_info(conn, smb_fname);
3544 status = posix_get_nt_acl_common(conn,
3545 smb_fname->base_name,
3546 &smb_fname->st,
3547 pal,
3548 posix_acl,
3549 def_acl,
3550 security_info,
3551 mem_ctx,
3552 ppdesc);
3553 TALLOC_FREE(frame);
3554 return status;
3557 /****************************************************************************
3558 Try to chown a file. We will be able to chown it under the following conditions.
3560 1) If we have root privileges, then it will just work.
3561 2) If we have SeRestorePrivilege we can change the user + group to any other user.
3562 3) If we have SeTakeOwnershipPrivilege we can change the user to the current user.
3563 4) If we have write permission to the file and dos_filemodes is set
3564 then allow chown to the currently authenticated user.
3565 ****************************************************************************/
3567 NTSTATUS try_chown(files_struct *fsp, uid_t uid, gid_t gid)
3569 NTSTATUS status;
3571 if(!CAN_WRITE(fsp->conn)) {
3572 return NT_STATUS_MEDIA_WRITE_PROTECTED;
3575 /* Case (1). */
3576 status = vfs_chown_fsp(fsp, uid, gid);
3577 if (NT_STATUS_IS_OK(status)) {
3578 return status;
3581 /* Case (2) / (3) */
3582 if (lp_enable_privileges()) {
3583 bool has_take_ownership_priv = security_token_has_privilege(
3584 get_current_nttok(fsp->conn),
3585 SEC_PRIV_TAKE_OWNERSHIP);
3586 bool has_restore_priv = security_token_has_privilege(
3587 get_current_nttok(fsp->conn),
3588 SEC_PRIV_RESTORE);
3590 if (has_restore_priv) {
3591 ; /* Case (2) */
3592 } else if (has_take_ownership_priv) {
3593 /* Case (3) */
3594 if (uid == get_current_uid(fsp->conn)) {
3595 gid = (gid_t)-1;
3596 } else {
3597 has_take_ownership_priv = false;
3601 if (has_take_ownership_priv || has_restore_priv) {
3602 become_root();
3603 status = vfs_chown_fsp(fsp, uid, gid);
3604 unbecome_root();
3605 return status;
3609 /* Case (4). */
3610 if (!lp_dos_filemode(SNUM(fsp->conn))) {
3611 return NT_STATUS_ACCESS_DENIED;
3614 /* only allow chown to the current user. This is more secure,
3615 and also copes with the case where the SID in a take ownership ACL is
3616 a local SID on the users workstation
3618 if (uid != get_current_uid(fsp->conn)) {
3619 return NT_STATUS_INVALID_OWNER;
3622 become_root();
3623 /* Keep the current file gid the same. */
3624 status = vfs_chown_fsp(fsp, uid, (gid_t)-1);
3625 unbecome_root();
3627 return status;
3630 /****************************************************************************
3631 Reply to set a security descriptor on an fsp. security_info_sent is the
3632 description of the following NT ACL.
3633 This should be the only external function needed for the UNIX style set ACL.
3634 We make a copy of psd_orig as internal functions modify the elements inside
3635 it, even though it's a const pointer.
3636 ****************************************************************************/
3638 NTSTATUS set_nt_acl(files_struct *fsp, uint32_t security_info_sent, const struct security_descriptor *psd_orig)
3640 connection_struct *conn = fsp->conn;
3641 uid_t user = (uid_t)-1;
3642 gid_t grp = (gid_t)-1;
3643 struct dom_sid file_owner_sid;
3644 struct dom_sid file_grp_sid;
3645 canon_ace *file_ace_list = NULL;
3646 canon_ace *dir_ace_list = NULL;
3647 bool acl_perms = False;
3648 mode_t orig_mode = (mode_t)0;
3649 NTSTATUS status;
3650 bool set_acl_as_root = false;
3651 bool acl_set_support = false;
3652 bool ret = false;
3653 struct security_descriptor *psd = NULL;
3655 DEBUG(10,("set_nt_acl: called for file %s\n",
3656 fsp_str_dbg(fsp)));
3658 if (!CAN_WRITE(conn)) {
3659 DEBUG(10,("set acl rejected on read-only share\n"));
3660 return NT_STATUS_MEDIA_WRITE_PROTECTED;
3663 if (psd_orig == NULL) {
3664 return NT_STATUS_INVALID_PARAMETER;
3668 * MS NFS mode, here's the deal: the client merely wants to
3669 * modify the mode, but roundtripping get_acl/set/acl would
3670 * add additional POSIX ACEs. So in case we get a request
3671 * containing a MS NFS mode SID, we do nothing here.
3673 if (security_descriptor_with_ms_nfs(psd_orig)) {
3674 return NT_STATUS_OK;
3677 psd = security_descriptor_copy(talloc_tos(), psd_orig);
3678 if (psd == NULL) {
3679 return NT_STATUS_NO_MEMORY;
3683 * Get the current state of the file.
3686 status = vfs_stat_fsp(fsp);
3687 if (!NT_STATUS_IS_OK(status)) {
3688 return status;
3691 /* Save the original element we check against. */
3692 orig_mode = fsp->fsp_name->st.st_ex_mode;
3695 * Unpack the user/group/world id's.
3698 /* POSIX can't cope with missing owner/group. */
3699 if ((security_info_sent & SECINFO_OWNER) && (psd->owner_sid == NULL)) {
3700 security_info_sent &= ~SECINFO_OWNER;
3702 if ((security_info_sent & SECINFO_GROUP) && (psd->group_sid == NULL)) {
3703 security_info_sent &= ~SECINFO_GROUP;
3706 /* If UNIX owner is inherited and Windows isn't, then
3707 * setting the UNIX owner based on Windows owner conflicts
3708 * with the inheritance rule
3710 if (lp_inherit_owner(SNUM(conn)) == INHERIT_OWNER_UNIX_ONLY) {
3711 security_info_sent &= ~SECINFO_OWNER;
3714 status = unpack_nt_owners( conn, &user, &grp, security_info_sent, psd);
3715 if (!NT_STATUS_IS_OK(status)) {
3716 return status;
3720 * Do we need to chown ? If so this must be done first as the incoming
3721 * CREATOR_OWNER acl will be relative to the *new* owner, not the old.
3722 * Noticed by Simo.
3725 if (((user != (uid_t)-1) && (fsp->fsp_name->st.st_ex_uid != user)) ||
3726 (( grp != (gid_t)-1) && (fsp->fsp_name->st.st_ex_gid != grp))) {
3728 DEBUG(3,("set_nt_acl: chown %s. uid = %u, gid = %u.\n",
3729 fsp_str_dbg(fsp), (unsigned int)user,
3730 (unsigned int)grp));
3732 status = try_chown(fsp, user, grp);
3733 if(!NT_STATUS_IS_OK(status)) {
3734 DEBUG(3,("set_nt_acl: chown %s, %u, %u failed. Error "
3735 "= %s.\n", fsp_str_dbg(fsp),
3736 (unsigned int)user,
3737 (unsigned int)grp,
3738 nt_errstr(status)));
3739 return status;
3743 * Recheck the current state of the file, which may have changed.
3744 * (suid/sgid bits, for instance)
3747 status = vfs_stat_fsp(fsp);
3748 if (!NT_STATUS_IS_OK(status)) {
3749 return status;
3752 /* Save the original element we check against. */
3753 orig_mode = fsp->fsp_name->st.st_ex_mode;
3755 /* If we successfully chowned, we know we must
3756 * be able to set the acl, so do it as root.
3758 set_acl_as_root = true;
3761 create_file_sids(&fsp->fsp_name->st, &file_owner_sid, &file_grp_sid);
3763 if((security_info_sent & SECINFO_DACL) &&
3764 (psd->type & SEC_DESC_DACL_PRESENT) &&
3765 (psd->dacl == NULL)) {
3766 struct security_ace ace[3];
3768 /* We can't have NULL DACL in POSIX.
3769 Use owner/group/Everyone -> full access. */
3771 init_sec_ace(&ace[0],
3772 &file_owner_sid,
3773 SEC_ACE_TYPE_ACCESS_ALLOWED,
3774 GENERIC_ALL_ACCESS,
3776 init_sec_ace(&ace[1],
3777 &file_grp_sid,
3778 SEC_ACE_TYPE_ACCESS_ALLOWED,
3779 GENERIC_ALL_ACCESS,
3781 init_sec_ace(&ace[2],
3782 &global_sid_World,
3783 SEC_ACE_TYPE_ACCESS_ALLOWED,
3784 GENERIC_ALL_ACCESS,
3786 psd->dacl = make_sec_acl(talloc_tos(),
3787 NT4_ACL_REVISION,
3789 ace);
3790 if (psd->dacl == NULL) {
3791 return NT_STATUS_NO_MEMORY;
3793 security_acl_map_generic(psd->dacl, &file_generic_mapping);
3796 acl_perms = unpack_canon_ace(fsp, &fsp->fsp_name->st, &file_owner_sid,
3797 &file_grp_sid, &file_ace_list,
3798 &dir_ace_list, security_info_sent, psd);
3800 /* Ignore W2K traverse DACL set. */
3801 if (!file_ace_list && !dir_ace_list) {
3802 return NT_STATUS_OK;
3805 if (!acl_perms) {
3806 DEBUG(3,("set_nt_acl: cannot set permissions\n"));
3807 free_canon_ace_list(file_ace_list);
3808 free_canon_ace_list(dir_ace_list);
3809 return NT_STATUS_ACCESS_DENIED;
3813 * Only change security if we got a DACL.
3816 if(!(security_info_sent & SECINFO_DACL) || (psd->dacl == NULL)) {
3817 free_canon_ace_list(file_ace_list);
3818 free_canon_ace_list(dir_ace_list);
3819 return NT_STATUS_OK;
3823 * Try using the POSIX ACL set first. Fall back to chmod if
3824 * we have no ACL support on this filesystem.
3827 if (acl_perms && file_ace_list) {
3828 if (set_acl_as_root) {
3829 become_root();
3831 ret = set_canon_ace_list(fsp, file_ace_list, false,
3832 &fsp->fsp_name->st, &acl_set_support);
3833 if (set_acl_as_root) {
3834 unbecome_root();
3836 if (acl_set_support && ret == false) {
3837 DEBUG(3,("set_nt_acl: failed to set file acl on file "
3838 "%s (%s).\n", fsp_str_dbg(fsp),
3839 strerror(errno)));
3840 free_canon_ace_list(file_ace_list);
3841 free_canon_ace_list(dir_ace_list);
3842 return map_nt_error_from_unix(errno);
3846 if (acl_perms && acl_set_support && fsp->is_directory) {
3847 if (dir_ace_list) {
3848 if (set_acl_as_root) {
3849 become_root();
3851 ret = set_canon_ace_list(fsp, dir_ace_list, true,
3852 &fsp->fsp_name->st,
3853 &acl_set_support);
3854 if (set_acl_as_root) {
3855 unbecome_root();
3857 if (ret == false) {
3858 DEBUG(3,("set_nt_acl: failed to set default "
3859 "acl on directory %s (%s).\n",
3860 fsp_str_dbg(fsp), strerror(errno)));
3861 free_canon_ace_list(file_ace_list);
3862 free_canon_ace_list(dir_ace_list);
3863 return map_nt_error_from_unix(errno);
3865 } else {
3866 int sret = -1;
3869 * No default ACL - delete one if it exists.
3872 if (set_acl_as_root) {
3873 become_root();
3875 sret = SMB_VFS_SYS_ACL_DELETE_DEF_FILE(conn,
3876 fsp->fsp_name);
3877 if (set_acl_as_root) {
3878 unbecome_root();
3880 if (sret == -1) {
3881 if (acl_group_override(conn, fsp->fsp_name)) {
3882 DEBUG(5,("set_nt_acl: acl group "
3883 "control on and current user "
3884 "in file %s primary group. "
3885 "Override delete_def_acl\n",
3886 fsp_str_dbg(fsp)));
3888 become_root();
3889 sret =
3890 SMB_VFS_SYS_ACL_DELETE_DEF_FILE(
3891 conn,
3892 fsp->fsp_name);
3893 unbecome_root();
3896 if (sret == -1) {
3897 DEBUG(3,("set_nt_acl: sys_acl_delete_def_file failed (%s)\n", strerror(errno)));
3898 free_canon_ace_list(file_ace_list);
3899 free_canon_ace_list(dir_ace_list);
3900 return map_nt_error_from_unix(errno);
3906 if (acl_set_support) {
3907 if (set_acl_as_root) {
3908 become_root();
3910 store_inheritance_attributes(fsp,
3911 file_ace_list,
3912 dir_ace_list,
3913 psd->type);
3914 if (set_acl_as_root) {
3915 unbecome_root();
3920 * If we cannot set using POSIX ACLs we fall back to checking if we need to chmod.
3923 if(!acl_set_support && acl_perms) {
3924 mode_t posix_perms;
3926 if (!convert_canon_ace_to_posix_perms( fsp, file_ace_list, &posix_perms)) {
3927 free_canon_ace_list(file_ace_list);
3928 free_canon_ace_list(dir_ace_list);
3929 DEBUG(3,("set_nt_acl: failed to convert file acl to "
3930 "posix permissions for file %s.\n",
3931 fsp_str_dbg(fsp)));
3932 return NT_STATUS_ACCESS_DENIED;
3935 if (orig_mode != posix_perms) {
3936 int sret = -1;
3938 DEBUG(3,("set_nt_acl: chmod %s. perms = 0%o.\n",
3939 fsp_str_dbg(fsp), (unsigned int)posix_perms));
3941 if (set_acl_as_root) {
3942 become_root();
3944 sret = SMB_VFS_CHMOD(conn, fsp->fsp_name,
3945 posix_perms);
3946 if (set_acl_as_root) {
3947 unbecome_root();
3949 if(sret == -1) {
3950 if (acl_group_override(conn, fsp->fsp_name)) {
3951 DEBUG(5,("set_nt_acl: acl group "
3952 "control on and current user "
3953 "in file %s primary group. "
3954 "Override chmod\n",
3955 fsp_str_dbg(fsp)));
3957 become_root();
3958 sret = SMB_VFS_CHMOD(conn,
3959 fsp->fsp_name,
3960 posix_perms);
3961 unbecome_root();
3964 if (sret == -1) {
3965 DEBUG(3,("set_nt_acl: chmod %s, 0%o "
3966 "failed. Error = %s.\n",
3967 fsp_str_dbg(fsp),
3968 (unsigned int)posix_perms,
3969 strerror(errno)));
3970 free_canon_ace_list(file_ace_list);
3971 free_canon_ace_list(dir_ace_list);
3972 return map_nt_error_from_unix(errno);
3978 free_canon_ace_list(file_ace_list);
3979 free_canon_ace_list(dir_ace_list);
3981 /* Ensure the stat struct in the fsp is correct. */
3982 status = vfs_stat_fsp(fsp);
3984 return NT_STATUS_OK;
3987 /****************************************************************************
3988 Get the actual group bits stored on a file with an ACL. Has no effect if
3989 the file has no ACL. Needed in dosmode code where the stat() will return
3990 the mask bits, not the real group bits, for a file with an ACL.
3991 ****************************************************************************/
3993 int get_acl_group_bits( connection_struct *conn,
3994 const struct smb_filename *smb_fname,
3995 mode_t *mode )
3997 int entry_id = SMB_ACL_FIRST_ENTRY;
3998 SMB_ACL_ENTRY_T entry;
3999 SMB_ACL_T posix_acl;
4000 int result = -1;
4002 posix_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, smb_fname,
4003 SMB_ACL_TYPE_ACCESS, talloc_tos());
4004 if (posix_acl == (SMB_ACL_T)NULL)
4005 return -1;
4007 while (sys_acl_get_entry(posix_acl, entry_id, &entry) == 1) {
4008 SMB_ACL_TAG_T tagtype;
4009 SMB_ACL_PERMSET_T permset;
4011 entry_id = SMB_ACL_NEXT_ENTRY;
4013 if (sys_acl_get_tag_type(entry, &tagtype) ==-1)
4014 break;
4016 if (tagtype == SMB_ACL_GROUP_OBJ) {
4017 if (sys_acl_get_permset(entry, &permset) == -1) {
4018 break;
4019 } else {
4020 *mode &= ~(S_IRGRP|S_IWGRP|S_IXGRP);
4021 *mode |= (sys_acl_get_perm(permset, SMB_ACL_READ) ? S_IRGRP : 0);
4022 *mode |= (sys_acl_get_perm(permset, SMB_ACL_WRITE) ? S_IWGRP : 0);
4023 *mode |= (sys_acl_get_perm(permset, SMB_ACL_EXECUTE) ? S_IXGRP : 0);
4024 result = 0;
4025 break;
4029 TALLOC_FREE(posix_acl);
4030 return result;
4033 /****************************************************************************
4034 Do a chmod by setting the ACL USER_OBJ, GROUP_OBJ and OTHER bits in an ACL
4035 and set the mask to rwx. Needed to preserve complex ACLs set by NT.
4036 ****************************************************************************/
4038 static int chmod_acl_internals( connection_struct *conn, SMB_ACL_T posix_acl, mode_t mode)
4040 int entry_id = SMB_ACL_FIRST_ENTRY;
4041 SMB_ACL_ENTRY_T entry;
4042 int num_entries = 0;
4044 while ( sys_acl_get_entry(posix_acl, entry_id, &entry) == 1) {
4045 SMB_ACL_TAG_T tagtype;
4046 SMB_ACL_PERMSET_T permset;
4047 mode_t perms;
4049 entry_id = SMB_ACL_NEXT_ENTRY;
4051 if (sys_acl_get_tag_type(entry, &tagtype) == -1)
4052 return -1;
4054 if (sys_acl_get_permset(entry, &permset) == -1)
4055 return -1;
4057 num_entries++;
4059 switch(tagtype) {
4060 case SMB_ACL_USER_OBJ:
4061 perms = unix_perms_to_acl_perms(mode, S_IRUSR, S_IWUSR, S_IXUSR);
4062 break;
4063 case SMB_ACL_GROUP_OBJ:
4064 perms = unix_perms_to_acl_perms(mode, S_IRGRP, S_IWGRP, S_IXGRP);
4065 break;
4066 case SMB_ACL_MASK:
4068 * FIXME: The ACL_MASK entry permissions should really be set to
4069 * the union of the permissions of all ACL_USER,
4070 * ACL_GROUP_OBJ, and ACL_GROUP entries. That's what
4071 * acl_calc_mask() does, but Samba ACLs doesn't provide it.
4073 perms = S_IRUSR|S_IWUSR|S_IXUSR;
4074 break;
4075 case SMB_ACL_OTHER:
4076 perms = unix_perms_to_acl_perms(mode, S_IROTH, S_IWOTH, S_IXOTH);
4077 break;
4078 default:
4079 continue;
4082 if (map_acl_perms_to_permset(perms, &permset) == -1)
4083 return -1;
4085 if (sys_acl_set_permset(entry, permset) == -1)
4086 return -1;
4090 * If this is a simple 3 element ACL or no elements then it's a standard
4091 * UNIX permission set. Just use chmod...
4094 if ((num_entries == 3) || (num_entries == 0))
4095 return -1;
4097 return 0;
4100 /****************************************************************************
4101 Get the access ACL of FROM, do a chmod by setting the ACL USER_OBJ,
4102 GROUP_OBJ and OTHER bits in an ACL and set the mask to rwx. Set the
4103 resulting ACL on TO. Note that name is in UNIX character set.
4104 ****************************************************************************/
4106 static int copy_access_posix_acl(connection_struct *conn,
4107 const struct smb_filename *smb_fname_from,
4108 const struct smb_filename *smb_fname_to,
4109 mode_t mode)
4111 SMB_ACL_T posix_acl = NULL;
4112 int ret = -1;
4114 if ((posix_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, smb_fname_from,
4115 SMB_ACL_TYPE_ACCESS,
4116 talloc_tos())) == NULL)
4117 return -1;
4119 if ((ret = chmod_acl_internals(conn, posix_acl, mode)) == -1)
4120 goto done;
4122 ret = SMB_VFS_SYS_ACL_SET_FILE(conn, smb_fname_to,
4123 SMB_ACL_TYPE_ACCESS, posix_acl);
4125 done:
4127 TALLOC_FREE(posix_acl);
4128 return ret;
4131 /****************************************************************************
4132 Check for an existing default POSIX ACL on a directory.
4133 ****************************************************************************/
4135 static bool directory_has_default_posix_acl(connection_struct *conn,
4136 const struct smb_filename *smb_fname)
4138 SMB_ACL_T def_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, smb_fname,
4139 SMB_ACL_TYPE_DEFAULT,
4140 talloc_tos());
4141 bool has_acl = False;
4142 SMB_ACL_ENTRY_T entry;
4144 if (def_acl != NULL && (sys_acl_get_entry(def_acl, SMB_ACL_FIRST_ENTRY, &entry) == 1)) {
4145 has_acl = True;
4148 if (def_acl) {
4149 TALLOC_FREE(def_acl);
4151 return has_acl;
4154 /****************************************************************************
4155 If the parent directory has no default ACL but it does have an Access ACL,
4156 inherit this Access ACL to file name.
4157 ****************************************************************************/
4159 int inherit_access_posix_acl(connection_struct *conn,
4160 const char *inherit_from_dir,
4161 const struct smb_filename *smb_fname,
4162 mode_t mode)
4164 struct smb_filename *inherit_from_fname =
4165 synthetic_smb_fname(talloc_tos(),
4166 smb_fname->base_name,
4167 NULL,
4168 NULL,
4169 smb_fname->flags);
4170 if (inherit_from_fname == NULL) {
4171 return-1;
4174 if (directory_has_default_posix_acl(conn, inherit_from_fname))
4175 return 0;
4177 return copy_access_posix_acl(conn, inherit_from_fname, smb_fname, mode);
4180 /****************************************************************************
4181 Map from wire type to permset.
4182 ****************************************************************************/
4184 static bool unix_ex_wire_to_permset(connection_struct *conn, unsigned char wire_perm, SMB_ACL_PERMSET_T *p_permset)
4186 if (wire_perm & ~(SMB_POSIX_ACL_READ|SMB_POSIX_ACL_WRITE|SMB_POSIX_ACL_EXECUTE)) {
4187 return False;
4190 if (sys_acl_clear_perms(*p_permset) == -1) {
4191 return False;
4194 if (wire_perm & SMB_POSIX_ACL_READ) {
4195 if (sys_acl_add_perm(*p_permset, SMB_ACL_READ) == -1) {
4196 return False;
4199 if (wire_perm & SMB_POSIX_ACL_WRITE) {
4200 if (sys_acl_add_perm(*p_permset, SMB_ACL_WRITE) == -1) {
4201 return False;
4204 if (wire_perm & SMB_POSIX_ACL_EXECUTE) {
4205 if (sys_acl_add_perm(*p_permset, SMB_ACL_EXECUTE) == -1) {
4206 return False;
4209 return True;
4212 /****************************************************************************
4213 Map from wire type to tagtype.
4214 ****************************************************************************/
4216 static bool unix_ex_wire_to_tagtype(unsigned char wire_tt, SMB_ACL_TAG_T *p_tt)
4218 switch (wire_tt) {
4219 case SMB_POSIX_ACL_USER_OBJ:
4220 *p_tt = SMB_ACL_USER_OBJ;
4221 break;
4222 case SMB_POSIX_ACL_USER:
4223 *p_tt = SMB_ACL_USER;
4224 break;
4225 case SMB_POSIX_ACL_GROUP_OBJ:
4226 *p_tt = SMB_ACL_GROUP_OBJ;
4227 break;
4228 case SMB_POSIX_ACL_GROUP:
4229 *p_tt = SMB_ACL_GROUP;
4230 break;
4231 case SMB_POSIX_ACL_MASK:
4232 *p_tt = SMB_ACL_MASK;
4233 break;
4234 case SMB_POSIX_ACL_OTHER:
4235 *p_tt = SMB_ACL_OTHER;
4236 break;
4237 default:
4238 return False;
4240 return True;
4243 /****************************************************************************
4244 Create a new POSIX acl from wire permissions.
4245 FIXME ! How does the share mask/mode fit into this.... ?
4246 ****************************************************************************/
4248 static SMB_ACL_T create_posix_acl_from_wire(connection_struct *conn,
4249 uint16_t num_acls,
4250 const char *pdata,
4251 TALLOC_CTX *mem_ctx)
4253 unsigned int i;
4254 SMB_ACL_T the_acl = sys_acl_init(mem_ctx);
4256 if (the_acl == NULL) {
4257 return NULL;
4260 for (i = 0; i < num_acls; i++) {
4261 SMB_ACL_ENTRY_T the_entry;
4262 SMB_ACL_PERMSET_T the_permset;
4263 SMB_ACL_TAG_T tag_type;
4265 if (sys_acl_create_entry(&the_acl, &the_entry) == -1) {
4266 DEBUG(0,("create_posix_acl_from_wire: Failed to create entry %u. (%s)\n",
4267 i, strerror(errno) ));
4268 goto fail;
4271 if (!unix_ex_wire_to_tagtype(CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)), &tag_type)) {
4272 DEBUG(0,("create_posix_acl_from_wire: invalid wire tagtype %u on entry %u.\n",
4273 CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)), i ));
4274 goto fail;
4277 if (sys_acl_set_tag_type(the_entry, tag_type) == -1) {
4278 DEBUG(0,("create_posix_acl_from_wire: Failed to set tagtype on entry %u. (%s)\n",
4279 i, strerror(errno) ));
4280 goto fail;
4283 /* Get the permset pointer from the new ACL entry. */
4284 if (sys_acl_get_permset(the_entry, &the_permset) == -1) {
4285 DEBUG(0,("create_posix_acl_from_wire: Failed to get permset on entry %u. (%s)\n",
4286 i, strerror(errno) ));
4287 goto fail;
4290 /* Map from wire to permissions. */
4291 if (!unix_ex_wire_to_permset(conn, CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)+1), &the_permset)) {
4292 DEBUG(0,("create_posix_acl_from_wire: invalid permset %u on entry %u.\n",
4293 CVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE) + 1), i ));
4294 goto fail;
4297 /* Now apply to the new ACL entry. */
4298 if (sys_acl_set_permset(the_entry, the_permset) == -1) {
4299 DEBUG(0,("create_posix_acl_from_wire: Failed to add permset on entry %u. (%s)\n",
4300 i, strerror(errno) ));
4301 goto fail;
4304 if (tag_type == SMB_ACL_USER) {
4305 uint32_t uidval = IVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
4306 uid_t uid = (uid_t)uidval;
4307 if (sys_acl_set_qualifier(the_entry,(void *)&uid) == -1) {
4308 DEBUG(0,("create_posix_acl_from_wire: Failed to set uid %u on entry %u. (%s)\n",
4309 (unsigned int)uid, i, strerror(errno) ));
4310 goto fail;
4314 if (tag_type == SMB_ACL_GROUP) {
4315 uint32_t gidval = IVAL(pdata,(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
4316 gid_t gid = (uid_t)gidval;
4317 if (sys_acl_set_qualifier(the_entry,(void *)&gid) == -1) {
4318 DEBUG(0,("create_posix_acl_from_wire: Failed to set gid %u on entry %u. (%s)\n",
4319 (unsigned int)gid, i, strerror(errno) ));
4320 goto fail;
4325 return the_acl;
4327 fail:
4329 if (the_acl != NULL) {
4330 TALLOC_FREE(the_acl);
4332 return NULL;
4335 /****************************************************************************
4336 Calls from UNIX extensions - Default POSIX ACL set.
4337 If num_def_acls == 0 and not a directory just return. If it is a directory
4338 and num_def_acls == 0 then remove the default acl. Else set the default acl
4339 on the directory.
4340 ****************************************************************************/
4342 bool set_unix_posix_default_acl(connection_struct *conn,
4343 const struct smb_filename *smb_fname,
4344 uint16_t num_def_acls,
4345 const char *pdata)
4347 SMB_ACL_T def_acl = NULL;
4349 if (!S_ISDIR(smb_fname->st.st_ex_mode)) {
4350 if (num_def_acls) {
4351 DEBUG(5,("set_unix_posix_default_acl: Can't "
4352 "set default ACL on non-directory file %s\n",
4353 smb_fname->base_name ));
4354 errno = EISDIR;
4355 return False;
4356 } else {
4357 return True;
4361 if (!num_def_acls) {
4362 /* Remove the default ACL. */
4363 if (SMB_VFS_SYS_ACL_DELETE_DEF_FILE(conn, smb_fname) == -1) {
4364 DEBUG(5,("set_unix_posix_default_acl: acl_delete_def_file failed on directory %s (%s)\n",
4365 smb_fname->base_name, strerror(errno) ));
4366 return False;
4368 return True;
4371 if ((def_acl = create_posix_acl_from_wire(conn, num_def_acls,
4372 pdata,
4373 talloc_tos())) == NULL) {
4374 return False;
4377 if (SMB_VFS_SYS_ACL_SET_FILE(conn, smb_fname,
4378 SMB_ACL_TYPE_DEFAULT, def_acl) == -1) {
4379 DEBUG(5,("set_unix_posix_default_acl: acl_set_file failed on directory %s (%s)\n",
4380 smb_fname->base_name, strerror(errno) ));
4381 TALLOC_FREE(def_acl);
4382 return False;
4385 DEBUG(10,("set_unix_posix_default_acl: set default acl for file %s\n",
4386 smb_fname->base_name ));
4387 TALLOC_FREE(def_acl);
4388 return True;
4391 /****************************************************************************
4392 Remove an ACL from a file. As we don't have acl_delete_entry() available
4393 we must read the current acl and copy all entries except MASK, USER and GROUP
4394 to a new acl, then set that. This (at least on Linux) causes any ACL to be
4395 removed.
4396 FIXME ! How does the share mask/mode fit into this.... ?
4397 ****************************************************************************/
4399 static bool remove_posix_acl(connection_struct *conn,
4400 files_struct *fsp,
4401 const struct smb_filename *smb_fname)
4403 SMB_ACL_T file_acl = NULL;
4404 int entry_id = SMB_ACL_FIRST_ENTRY;
4405 SMB_ACL_ENTRY_T entry;
4406 bool ret = False;
4407 const char *fname = smb_fname->base_name;
4408 /* Create a new ACL with only 3 entries, u/g/w. */
4409 SMB_ACL_T new_file_acl = sys_acl_init(talloc_tos());
4410 SMB_ACL_ENTRY_T user_ent = NULL;
4411 SMB_ACL_ENTRY_T group_ent = NULL;
4412 SMB_ACL_ENTRY_T other_ent = NULL;
4414 if (new_file_acl == NULL) {
4415 DEBUG(5,("remove_posix_acl: failed to init new ACL with 3 entries for file %s.\n", fname));
4416 return False;
4419 /* Now create the u/g/w entries. */
4420 if (sys_acl_create_entry(&new_file_acl, &user_ent) == -1) {
4421 DEBUG(5,("remove_posix_acl: Failed to create user entry for file %s. (%s)\n",
4422 fname, strerror(errno) ));
4423 goto done;
4425 if (sys_acl_set_tag_type(user_ent, SMB_ACL_USER_OBJ) == -1) {
4426 DEBUG(5,("remove_posix_acl: Failed to set user entry for file %s. (%s)\n",
4427 fname, strerror(errno) ));
4428 goto done;
4431 if (sys_acl_create_entry(&new_file_acl, &group_ent) == -1) {
4432 DEBUG(5,("remove_posix_acl: Failed to create group entry for file %s. (%s)\n",
4433 fname, strerror(errno) ));
4434 goto done;
4436 if (sys_acl_set_tag_type(group_ent, SMB_ACL_GROUP_OBJ) == -1) {
4437 DEBUG(5,("remove_posix_acl: Failed to set group entry for file %s. (%s)\n",
4438 fname, strerror(errno) ));
4439 goto done;
4442 if (sys_acl_create_entry(&new_file_acl, &other_ent) == -1) {
4443 DEBUG(5,("remove_posix_acl: Failed to create other entry for file %s. (%s)\n",
4444 fname, strerror(errno) ));
4445 goto done;
4447 if (sys_acl_set_tag_type(other_ent, SMB_ACL_OTHER) == -1) {
4448 DEBUG(5,("remove_posix_acl: Failed to set other entry for file %s. (%s)\n",
4449 fname, strerror(errno) ));
4450 goto done;
4453 /* Get the current file ACL. */
4454 if (fsp && fsp->fh->fd != -1) {
4455 file_acl = SMB_VFS_SYS_ACL_GET_FD(fsp, talloc_tos());
4456 } else {
4457 file_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, smb_fname,
4458 SMB_ACL_TYPE_ACCESS,
4459 talloc_tos());
4462 if (file_acl == NULL) {
4463 /* This is only returned if an error occurred. Even for a file with
4464 no acl a u/g/w acl should be returned. */
4465 DEBUG(5,("remove_posix_acl: failed to get ACL from file %s (%s).\n",
4466 fname, strerror(errno) ));
4467 goto done;
4470 while ( sys_acl_get_entry(file_acl, entry_id, &entry) == 1) {
4471 SMB_ACL_TAG_T tagtype;
4472 SMB_ACL_PERMSET_T permset;
4474 entry_id = SMB_ACL_NEXT_ENTRY;
4476 if (sys_acl_get_tag_type(entry, &tagtype) == -1) {
4477 DEBUG(5,("remove_posix_acl: failed to get tagtype from ACL on file %s (%s).\n",
4478 fname, strerror(errno) ));
4479 goto done;
4482 if (sys_acl_get_permset(entry, &permset) == -1) {
4483 DEBUG(5,("remove_posix_acl: failed to get permset from ACL on file %s (%s).\n",
4484 fname, strerror(errno) ));
4485 goto done;
4488 if (tagtype == SMB_ACL_USER_OBJ) {
4489 if (sys_acl_set_permset(user_ent, permset) == -1) {
4490 DEBUG(5,("remove_posix_acl: failed to set permset from ACL on file %s (%s).\n",
4491 fname, strerror(errno) ));
4493 } else if (tagtype == SMB_ACL_GROUP_OBJ) {
4494 if (sys_acl_set_permset(group_ent, permset) == -1) {
4495 DEBUG(5,("remove_posix_acl: failed to set permset from ACL on file %s (%s).\n",
4496 fname, strerror(errno) ));
4498 } else if (tagtype == SMB_ACL_OTHER) {
4499 if (sys_acl_set_permset(other_ent, permset) == -1) {
4500 DEBUG(5,("remove_posix_acl: failed to set permset from ACL on file %s (%s).\n",
4501 fname, strerror(errno) ));
4506 /* Set the new empty file ACL. */
4507 if (fsp && fsp->fh->fd != -1) {
4508 if (SMB_VFS_SYS_ACL_SET_FD(fsp, new_file_acl) == -1) {
4509 DEBUG(5,("remove_posix_acl: acl_set_file failed on %s (%s)\n",
4510 fname, strerror(errno) ));
4511 goto done;
4513 } else {
4514 if (SMB_VFS_SYS_ACL_SET_FILE(conn,
4515 smb_fname,
4516 SMB_ACL_TYPE_ACCESS,
4517 new_file_acl) == -1) {
4518 DEBUG(5,("remove_posix_acl: acl_set_file failed on %s (%s)\n",
4519 fname, strerror(errno) ));
4520 goto done;
4524 ret = True;
4526 done:
4528 if (file_acl) {
4529 TALLOC_FREE(file_acl);
4531 if (new_file_acl) {
4532 TALLOC_FREE(new_file_acl);
4534 return ret;
4537 /****************************************************************************
4538 Calls from UNIX extensions - POSIX ACL set.
4539 If num_def_acls == 0 then read/modify/write acl after removing all entries
4540 except SMB_ACL_USER_OBJ, SMB_ACL_GROUP_OBJ, SMB_ACL_OTHER.
4541 ****************************************************************************/
4543 bool set_unix_posix_acl(connection_struct *conn, files_struct *fsp,
4544 const struct smb_filename *smb_fname,
4545 uint16_t num_acls,
4546 const char *pdata)
4548 SMB_ACL_T file_acl = NULL;
4549 const char *fname = smb_fname->base_name;
4551 if (!num_acls) {
4552 /* Remove the ACL from the file. */
4553 return remove_posix_acl(conn, fsp, smb_fname);
4556 if ((file_acl = create_posix_acl_from_wire(conn, num_acls,
4557 pdata,
4558 talloc_tos())) == NULL) {
4559 return False;
4562 if (fsp && fsp->fh->fd != -1) {
4563 /* The preferred way - use an open fd. */
4564 if (SMB_VFS_SYS_ACL_SET_FD(fsp, file_acl) == -1) {
4565 DEBUG(5,("set_unix_posix_acl: acl_set_file failed on %s (%s)\n",
4566 fname, strerror(errno) ));
4567 TALLOC_FREE(file_acl);
4568 return False;
4570 } else {
4571 if (SMB_VFS_SYS_ACL_SET_FILE(conn, smb_fname,
4572 SMB_ACL_TYPE_ACCESS, file_acl) == -1) {
4573 DEBUG(5,("set_unix_posix_acl: acl_set_file failed on %s (%s)\n",
4574 fname, strerror(errno) ));
4575 TALLOC_FREE(file_acl);
4576 return False;
4580 DEBUG(10,("set_unix_posix_acl: set acl for file %s\n", fname ));
4581 TALLOC_FREE(file_acl);
4582 return True;
4585 /********************************************************************
4586 Pull the NT ACL from a file on disk or the OpenEventlog() access
4587 check. Caller is responsible for freeing the returned security
4588 descriptor via TALLOC_FREE(). This is designed for dealing with
4589 user space access checks in smbd outside of the VFS. For example,
4590 checking access rights in OpenEventlog() or from python.
4592 ********************************************************************/
4594 NTSTATUS get_nt_acl_no_snum(TALLOC_CTX *ctx, const char *fname,
4595 uint32_t security_info_wanted,
4596 struct security_descriptor **sd)
4598 TALLOC_CTX *frame = talloc_stackframe();
4599 struct conn_struct_tos *c = NULL;
4600 NTSTATUS status = NT_STATUS_OK;
4601 struct smb_filename *smb_fname = synthetic_smb_fname(talloc_tos(),
4602 fname,
4603 NULL,
4604 NULL,
4607 if (smb_fname == NULL) {
4608 TALLOC_FREE(frame);
4609 return NT_STATUS_NO_MEMORY;
4612 if (!posix_locking_init(false)) {
4613 TALLOC_FREE(frame);
4614 return NT_STATUS_NO_MEMORY;
4617 status = create_conn_struct_tos(server_messaging_context(),
4619 "/",
4620 NULL,
4621 &c);
4622 if (!NT_STATUS_IS_OK(status)) {
4623 DEBUG(0,("create_conn_struct returned %s.\n",
4624 nt_errstr(status)));
4625 TALLOC_FREE(frame);
4626 return status;
4629 status = SMB_VFS_GET_NT_ACL(c->conn,
4630 smb_fname,
4631 security_info_wanted,
4632 ctx,
4633 sd);
4634 if (!NT_STATUS_IS_OK(status)) {
4635 DEBUG(0, ("get_nt_acl_no_snum: SMB_VFS_GET_NT_ACL returned %s.\n",
4636 nt_errstr(status)));
4639 TALLOC_FREE(frame);
4641 return status;
4644 int posix_sys_acl_blob_get_file(vfs_handle_struct *handle,
4645 const struct smb_filename *smb_fname_in,
4646 TALLOC_CTX *mem_ctx,
4647 char **blob_description,
4648 DATA_BLOB *blob)
4650 int ret;
4651 TALLOC_CTX *frame = talloc_stackframe();
4652 /* Initialise this to zero, in a portable way */
4653 struct smb_acl_wrapper acl_wrapper = {
4654 NULL
4656 struct smb_filename *smb_fname = cp_smb_filename_nostream(frame,
4657 smb_fname_in);
4658 if (smb_fname == NULL) {
4659 TALLOC_FREE(frame);
4660 errno = ENOMEM;
4661 return -1;
4664 acl_wrapper.access_acl
4665 = smb_vfs_call_sys_acl_get_file(handle,
4666 smb_fname,
4667 SMB_ACL_TYPE_ACCESS,
4668 frame);
4670 ret = smb_vfs_call_stat(handle, smb_fname);
4671 if (ret == -1) {
4672 TALLOC_FREE(frame);
4673 return -1;
4676 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
4677 acl_wrapper.default_acl
4678 = smb_vfs_call_sys_acl_get_file(handle,
4679 smb_fname,
4680 SMB_ACL_TYPE_DEFAULT,
4681 frame);
4684 acl_wrapper.owner = smb_fname->st.st_ex_uid;
4685 acl_wrapper.group = smb_fname->st.st_ex_gid;
4686 acl_wrapper.mode = smb_fname->st.st_ex_mode;
4688 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(blob, mem_ctx,
4689 &acl_wrapper,
4690 (ndr_push_flags_fn_t)ndr_push_smb_acl_wrapper))) {
4691 errno = EINVAL;
4692 TALLOC_FREE(frame);
4693 return -1;
4696 *blob_description = talloc_strdup(mem_ctx, "posix_acl");
4697 if (!*blob_description) {
4698 errno = EINVAL;
4699 TALLOC_FREE(frame);
4700 return -1;
4703 TALLOC_FREE(frame);
4704 return 0;
4707 int posix_sys_acl_blob_get_fd(vfs_handle_struct *handle,
4708 files_struct *fsp,
4709 TALLOC_CTX *mem_ctx,
4710 char **blob_description,
4711 DATA_BLOB *blob)
4713 SMB_STRUCT_STAT sbuf;
4714 TALLOC_CTX *frame;
4715 struct smb_acl_wrapper acl_wrapper;
4716 int ret;
4718 /* This ensures that we also consider the default ACL */
4719 if (fsp->is_directory || fsp->fh->fd == -1) {
4720 return posix_sys_acl_blob_get_file(handle,
4721 fsp->fsp_name,
4722 mem_ctx,
4723 blob_description,
4724 blob);
4726 frame = talloc_stackframe();
4728 acl_wrapper.default_acl = NULL;
4730 acl_wrapper.access_acl = smb_vfs_call_sys_acl_get_file(handle,
4731 fsp->fsp_name,
4732 SMB_ACL_TYPE_ACCESS,
4733 frame);
4735 ret = smb_vfs_call_fstat(handle, fsp, &sbuf);
4736 if (ret == -1) {
4737 TALLOC_FREE(frame);
4738 return -1;
4741 acl_wrapper.owner = sbuf.st_ex_uid;
4742 acl_wrapper.group = sbuf.st_ex_gid;
4743 acl_wrapper.mode = sbuf.st_ex_mode;
4745 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(blob, mem_ctx,
4746 &acl_wrapper,
4747 (ndr_push_flags_fn_t)ndr_push_smb_acl_wrapper))) {
4748 errno = EINVAL;
4749 TALLOC_FREE(frame);
4750 return -1;
4753 *blob_description = talloc_strdup(mem_ctx, "posix_acl");
4754 if (!*blob_description) {
4755 errno = EINVAL;
4756 TALLOC_FREE(frame);
4757 return -1;
4760 TALLOC_FREE(frame);
4761 return 0;
4764 static NTSTATUS make_default_acl_posix(TALLOC_CTX *ctx,
4765 const char *name,
4766 const SMB_STRUCT_STAT *psbuf,
4767 struct security_descriptor **ppdesc)
4769 struct dom_sid owner_sid, group_sid;
4770 size_t size = 0;
4771 struct security_ace aces[4];
4772 uint32_t access_mask = 0;
4773 mode_t mode = psbuf->st_ex_mode;
4774 struct security_acl *new_dacl = NULL;
4775 int idx = 0;
4777 DBG_DEBUG("file %s mode = 0%o\n",name, (int)mode);
4779 uid_to_sid(&owner_sid, psbuf->st_ex_uid);
4780 gid_to_sid(&group_sid, psbuf->st_ex_gid);
4783 We provide up to 4 ACEs
4784 - Owner
4785 - Group
4786 - Everyone
4787 - NT System
4790 if (mode & S_IRUSR) {
4791 if (mode & S_IWUSR) {
4792 access_mask |= SEC_RIGHTS_FILE_ALL;
4793 } else {
4794 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
4797 if (mode & S_IWUSR) {
4798 access_mask |= SEC_RIGHTS_FILE_WRITE | SEC_STD_DELETE;
4801 init_sec_ace(&aces[idx],
4802 &owner_sid,
4803 SEC_ACE_TYPE_ACCESS_ALLOWED,
4804 access_mask,
4806 idx++;
4808 access_mask = 0;
4809 if (mode & S_IRGRP) {
4810 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
4812 if (mode & S_IWGRP) {
4813 /* note that delete is not granted - this matches posix behaviour */
4814 access_mask |= SEC_RIGHTS_FILE_WRITE;
4816 if (access_mask) {
4817 init_sec_ace(&aces[idx],
4818 &group_sid,
4819 SEC_ACE_TYPE_ACCESS_ALLOWED,
4820 access_mask,
4822 idx++;
4825 access_mask = 0;
4826 if (mode & S_IROTH) {
4827 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
4829 if (mode & S_IWOTH) {
4830 access_mask |= SEC_RIGHTS_FILE_WRITE;
4832 if (access_mask) {
4833 init_sec_ace(&aces[idx],
4834 &global_sid_World,
4835 SEC_ACE_TYPE_ACCESS_ALLOWED,
4836 access_mask,
4838 idx++;
4841 init_sec_ace(&aces[idx],
4842 &global_sid_System,
4843 SEC_ACE_TYPE_ACCESS_ALLOWED,
4844 SEC_RIGHTS_FILE_ALL,
4846 idx++;
4848 new_dacl = make_sec_acl(ctx,
4849 NT4_ACL_REVISION,
4850 idx,
4851 aces);
4853 if (!new_dacl) {
4854 return NT_STATUS_NO_MEMORY;
4857 *ppdesc = make_sec_desc(ctx,
4858 SECURITY_DESCRIPTOR_REVISION_1,
4859 SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
4860 &owner_sid,
4861 &group_sid,
4862 NULL,
4863 new_dacl,
4864 &size);
4865 if (!*ppdesc) {
4866 return NT_STATUS_NO_MEMORY;
4868 return NT_STATUS_OK;
4871 static NTSTATUS make_default_acl_windows(TALLOC_CTX *ctx,
4872 const char *name,
4873 const SMB_STRUCT_STAT *psbuf,
4874 struct security_descriptor **ppdesc)
4876 struct dom_sid owner_sid, group_sid;
4877 size_t size = 0;
4878 struct security_ace aces[4];
4879 uint32_t access_mask = 0;
4880 mode_t mode = psbuf->st_ex_mode;
4881 struct security_acl *new_dacl = NULL;
4882 int idx = 0;
4884 DBG_DEBUG("file [%s] mode [0%o]\n", name, (int)mode);
4886 uid_to_sid(&owner_sid, psbuf->st_ex_uid);
4887 gid_to_sid(&group_sid, psbuf->st_ex_gid);
4890 * We provide 2 ACEs:
4891 * - Owner
4892 * - NT System
4895 if (mode & S_IRUSR) {
4896 if (mode & S_IWUSR) {
4897 access_mask |= SEC_RIGHTS_FILE_ALL;
4898 } else {
4899 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
4902 if (mode & S_IWUSR) {
4903 access_mask |= SEC_RIGHTS_FILE_WRITE | SEC_STD_DELETE;
4906 init_sec_ace(&aces[idx],
4907 &owner_sid,
4908 SEC_ACE_TYPE_ACCESS_ALLOWED,
4909 access_mask,
4911 idx++;
4913 init_sec_ace(&aces[idx],
4914 &global_sid_System,
4915 SEC_ACE_TYPE_ACCESS_ALLOWED,
4916 SEC_RIGHTS_FILE_ALL,
4918 idx++;
4920 new_dacl = make_sec_acl(ctx,
4921 NT4_ACL_REVISION,
4922 idx,
4923 aces);
4925 if (!new_dacl) {
4926 return NT_STATUS_NO_MEMORY;
4929 *ppdesc = make_sec_desc(ctx,
4930 SECURITY_DESCRIPTOR_REVISION_1,
4931 SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
4932 &owner_sid,
4933 &group_sid,
4934 NULL,
4935 new_dacl,
4936 &size);
4937 if (!*ppdesc) {
4938 return NT_STATUS_NO_MEMORY;
4940 return NT_STATUS_OK;
4943 static NTSTATUS make_default_acl_everyone(TALLOC_CTX *ctx,
4944 const char *name,
4945 const SMB_STRUCT_STAT *psbuf,
4946 struct security_descriptor **ppdesc)
4948 struct dom_sid owner_sid, group_sid;
4949 size_t size = 0;
4950 struct security_ace aces[1];
4951 mode_t mode = psbuf->st_ex_mode;
4952 struct security_acl *new_dacl = NULL;
4953 int idx = 0;
4955 DBG_DEBUG("file [%s] mode [0%o]\n", name, (int)mode);
4957 uid_to_sid(&owner_sid, psbuf->st_ex_uid);
4958 gid_to_sid(&group_sid, psbuf->st_ex_gid);
4961 * We provide one ACEs: full access for everyone
4964 init_sec_ace(&aces[idx],
4965 &global_sid_World,
4966 SEC_ACE_TYPE_ACCESS_ALLOWED,
4967 SEC_RIGHTS_FILE_ALL,
4969 idx++;
4971 new_dacl = make_sec_acl(ctx,
4972 NT4_ACL_REVISION,
4973 idx,
4974 aces);
4976 if (!new_dacl) {
4977 return NT_STATUS_NO_MEMORY;
4980 *ppdesc = make_sec_desc(ctx,
4981 SECURITY_DESCRIPTOR_REVISION_1,
4982 SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
4983 &owner_sid,
4984 &group_sid,
4985 NULL,
4986 new_dacl,
4987 &size);
4988 if (!*ppdesc) {
4989 return NT_STATUS_NO_MEMORY;
4991 return NT_STATUS_OK;
4994 static const struct enum_list default_acl_style_list[] = {
4995 {DEFAULT_ACL_POSIX, "posix"},
4996 {DEFAULT_ACL_WINDOWS, "windows"},
4997 {DEFAULT_ACL_EVERYONE, "everyone"},
5000 const struct enum_list *get_default_acl_style_list(void)
5002 return default_acl_style_list;
5005 NTSTATUS make_default_filesystem_acl(
5006 TALLOC_CTX *ctx,
5007 enum default_acl_style acl_style,
5008 const char *name,
5009 const SMB_STRUCT_STAT *psbuf,
5010 struct security_descriptor **ppdesc)
5012 NTSTATUS status;
5014 switch (acl_style) {
5015 case DEFAULT_ACL_POSIX:
5016 status = make_default_acl_posix(ctx, name, psbuf, ppdesc);
5017 break;
5019 case DEFAULT_ACL_WINDOWS:
5020 status = make_default_acl_windows(ctx, name, psbuf, ppdesc);
5021 break;
5023 case DEFAULT_ACL_EVERYONE:
5024 status = make_default_acl_everyone(ctx, name, psbuf, ppdesc);
5025 break;
5027 default:
5028 DBG_ERR("unknown acl style %d", acl_style);
5029 status = NT_STATUS_INTERNAL_ERROR;
5030 break;
5033 return status;