s3/packaging: pam_winbind has been moved to section 8.
[Samba/gebeck_regimport.git] / source3 / modules / onefs_acl.c
blob81bdfd26cc4be5913d444ad9c74481d9866426ef
1 /*
2 * Unix SMB/CIFS implementation.
4 * Support for OneFS native NTFS ACLs
6 * Copyright (C) Steven Danneman, 2008
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 "onefs.h"
24 #include "onefs_config.h"
26 #include <isi_acl/isi_acl_util.h>
27 #include <ifs/ifs_syscalls.h>
28 #include <sys/isi_acl.h>
30 const struct enum_list enum_onefs_acl_wire_format[] = {
31 {ACL_FORMAT_RAW, "No Format"},
32 {ACL_FORMAT_WINDOWS_SD, "Format Windows SD"},
33 {ACL_FORMAT_ALWAYS, "Always Format SD"},
34 {-1, NULL}
37 /**
38 * Turn SID into UID/GID and setup a struct ifs_identity
40 static bool
41 onefs_sid_to_identity(const DOM_SID *sid, struct ifs_identity *id,
42 bool is_group)
44 enum ifs_identity_type type = IFS_ID_TYPE_LAST+1;
45 uid_t uid = 0;
46 gid_t gid = 0;
48 if (!sid || sid_equal(sid, &global_sid_NULL))
49 type = IFS_ID_TYPE_NULL;
50 else if (sid_equal(sid, &global_sid_World))
51 type = IFS_ID_TYPE_EVERYONE;
52 else if (sid_equal(sid, &global_sid_Creator_Owner))
53 type = IFS_ID_TYPE_CREATOR_OWNER;
54 else if (sid_equal(sid, &global_sid_Creator_Group))
55 type = IFS_ID_TYPE_CREATOR_GROUP;
56 else if (is_group) {
57 if (!sid_to_gid(sid, &gid))
58 return false;
59 type = IFS_ID_TYPE_GID;
60 } else {
61 if (sid_to_uid(sid, &uid))
62 type = IFS_ID_TYPE_UID;
63 else if (sid_to_gid(sid, &gid))
64 type = IFS_ID_TYPE_GID;
65 else
66 return false;
69 if (aclu_initialize_identity(id, type, uid, gid, is_group)) {
70 DEBUG(3, ("Call to aclu_initialize_identity failed! id=%x, "
71 "type=%d, uid=%u, gid=%u, is_group=%d\n",
72 (unsigned int)id, type, uid, gid, is_group));
73 return false;
76 return true;
79 /**
80 * Turn struct ifs_identity into SID
82 static bool
83 onefs_identity_to_sid(struct ifs_identity *id, DOM_SID *sid)
85 if (!id || !sid)
86 return false;
88 if (id->type >= IFS_ID_TYPE_LAST)
89 return false;
91 switch (id->type) {
92 case IFS_ID_TYPE_UID:
93 uid_to_sid(sid, id->id.uid);
94 break;
95 case IFS_ID_TYPE_GID:
96 gid_to_sid(sid, id->id.gid);
97 break;
98 case IFS_ID_TYPE_EVERYONE:
99 sid_copy(sid, &global_sid_World);
100 break;
101 case IFS_ID_TYPE_NULL:
102 sid_copy(sid, &global_sid_NULL);
103 break;
104 case IFS_ID_TYPE_CREATOR_OWNER:
105 sid_copy(sid, &global_sid_Creator_Owner);
106 break;
107 case IFS_ID_TYPE_CREATOR_GROUP:
108 sid_copy(sid, &global_sid_Creator_Group);
109 break;
110 default:
111 DEBUG(0, ("Unknown identity type: %d\n", id->type));
112 return false;
115 return true;
118 static bool
119 onefs_og_to_identity(DOM_SID *sid, struct ifs_identity * ident,
120 bool is_group, int snum)
122 const DOM_SID *b_admin_sid = &global_sid_Builtin_Administrators;
124 if (!onefs_sid_to_identity(sid, ident, is_group)) {
125 if (!lp_parm_bool(snum, PARM_ONEFS_TYPE,
126 PARM_UNMAPPABLE_SIDS_IGNORE,
127 PARM_UNMAPPABLE_SIDS_IGNORE_DEFAULT)) {
128 DEBUG(3, ("Unresolvable SID (%s) found.\n",
129 sid_string_dbg(sid)));
130 return false;
132 if (!onefs_sid_to_identity(b_admin_sid, ident, is_group)) {
133 return false;
135 DEBUG(3, ("Mapping unresolvable owner SID (%s) to Builtin "
136 "Administrators group.\n",
137 sid_string_dbg(sid)));
139 return true;
142 static bool
143 sid_in_ignore_list(DOM_SID * sid, int snum)
145 const char ** sid_list = NULL;
146 DOM_SID match;
148 sid_list = lp_parm_string_list(snum, PARM_ONEFS_TYPE,
149 PARM_UNMAPPABLE_SIDS_IGNORE_LIST,
150 PARM_UNMAPPABLE_SIDS_IGNORE_LIST_DEFAULT);
152 /* Fast path a NULL list */
153 if (!sid_list || *sid_list == NULL)
154 return false;
156 while (*sid_list) {
157 if (string_to_sid(&match, *sid_list))
158 if (sid_equal(sid, &match))
159 return true;
160 sid_list++;
163 return false;
167 * Convert a trustee to a struct identity
169 static bool
170 onefs_samba_ace_to_ace(SEC_ACE * samba_ace, struct ifs_ace * ace,
171 bool *mapped, int snum)
173 struct ifs_identity ident = {.type=IFS_ID_TYPE_LAST, .id.uid=0};
175 SMB_ASSERT(ace);
176 SMB_ASSERT(mapped);
177 SMB_ASSERT(samba_ace);
179 if (onefs_sid_to_identity(&samba_ace->trustee, &ident, false)) {
180 *mapped = true;
181 } else {
183 SMB_ASSERT(ident.id.uid >= 0);
185 /* Ignore the sid if it's in the list */
186 if (sid_in_ignore_list(&samba_ace->trustee, snum)) {
187 DEBUG(3, ("Silently failing to set ACE for SID (%s) "
188 "because it is in the ignore sids list\n",
189 sid_string_dbg(&samba_ace->trustee)));
190 *mapped = false;
191 } else if ((samba_ace->type == SEC_ACE_TYPE_ACCESS_DENIED) &&
192 lp_parm_bool(snum, PARM_ONEFS_TYPE,
193 PARM_UNMAPPABLE_SIDS_DENY_EVERYONE,
194 PARM_UNMAPPABLE_SIDS_DENY_EVERYONE_DEFAULT)) {
195 /* If the ace is deny translated to Everyone */
196 DEBUG(3, ("Mapping unresolvable deny ACE SID (%s) "
197 "to Everyone.\n",
198 sid_string_dbg(&samba_ace->trustee)));
199 if (aclu_initialize_identity(&ident,
200 IFS_ID_TYPE_EVERYONE, 0, 0, False) != 0) {
201 DEBUG(2, ("aclu_initialize_identity() "
202 "failed making Everyone\n"));
203 return false;
205 *mapped = true;
206 } else if (lp_parm_bool(snum, PARM_ONEFS_TYPE,
207 PARM_UNMAPPABLE_SIDS_IGNORE,
208 PARM_UNMAPPABLE_SIDS_IGNORE_DEFAULT)) {
209 DEBUG(3, ("Silently failing to set ACE for SID (%s) "
210 "because it is unresolvable\n",
211 sid_string_dbg(&samba_ace->trustee)));
212 *mapped = false;
213 } else {
214 /* Fail for lack of a better option */
215 return false;
219 if (*mapped) {
220 if (aclu_initialize_ace(ace, samba_ace->type,
221 samba_ace->access_mask, samba_ace->flags, 0,
222 &ident))
223 return false;
225 if ((ace->trustee.type == IFS_ID_TYPE_CREATOR_OWNER ||
226 ace->trustee.type == IFS_ID_TYPE_CREATOR_GROUP) &&
227 nt4_compatible_acls())
228 ace->flags |= SEC_ACE_FLAG_INHERIT_ONLY;
231 return true;
235 * Convert a SEC_ACL to a struct ifs_security_acl
237 static bool
238 onefs_samba_acl_to_acl(SEC_ACL *samba_acl, struct ifs_security_acl **acl,
239 bool * ignore_aces, int snum)
241 int num_aces = 0;
242 struct ifs_ace *aces = NULL;
243 SEC_ACE *samba_aces;
244 bool mapped;
245 int i, j;
247 SMB_ASSERT(ignore_aces);
249 if ((!acl) || (!samba_acl))
250 return false;
252 samba_aces = samba_acl->aces;
254 if (samba_acl->num_aces > 0 && samba_aces) {
255 /* Setup ACES */
256 num_aces = samba_acl->num_aces;
257 aces = SMB_MALLOC_ARRAY(struct ifs_ace, num_aces);
259 for (i = 0, j = 0; j < num_aces; i++, j++) {
260 if (!onefs_samba_ace_to_ace(&samba_aces[j],
261 &aces[i], &mapped, snum))
262 goto err_free;
264 if (!mapped)
265 i--;
267 num_aces = i;
270 /* If aces are given but we cannot apply them due to the reasons
271 * above we do not change the SD. However, if we are told to
272 * explicitly set an SD with 0 aces we honor this operation */
273 *ignore_aces = samba_acl->num_aces > 0 && num_aces < 1;
275 if (*ignore_aces == false)
276 if (aclu_initialize_acl(acl, aces, num_aces))
277 goto err_free;
279 /* Currently aclu_initialize_acl should copy the aces over, allowing
280 * us to immediately free */
281 free(aces);
282 return true;
284 err_free:
285 free(aces);
286 return false;
290 * Convert a struct ifs_security_acl to a SEC_ACL
292 static bool
293 onefs_acl_to_samba_acl(struct ifs_security_acl *acl, SEC_ACL **samba_acl)
295 SEC_ACE *samba_aces = NULL;
296 SEC_ACL *tmp_samba_acl = NULL;
297 int i, num_aces = 0;
299 if (!samba_acl)
300 return false;
302 /* NULL ACL */
303 if (!acl) {
304 *samba_acl = NULL;
305 return true;
308 /* Determine number of aces in ACL */
309 if (!acl->aces)
310 num_aces = 0;
311 else
312 num_aces = acl->num_aces;
314 /* Allocate the ace list. */
315 if (num_aces > 0) {
316 if ((samba_aces = SMB_MALLOC_ARRAY(SEC_ACE, num_aces)) == NULL)
318 DEBUG(0, ("Unable to malloc space for %d aces.\n",
319 num_aces));
320 return false;
322 memset(samba_aces, '\0', (num_aces) * sizeof(SEC_ACE));
325 for (i = 0; i < num_aces; i++) {
326 DOM_SID sid;
328 if (!onefs_identity_to_sid(&acl->aces[i].trustee, &sid))
329 goto err_free;
331 init_sec_ace(&samba_aces[i], &sid, acl->aces[i].type,
332 acl->aces[i].access_mask, acl->aces[i].flags);
335 if ((tmp_samba_acl = make_sec_acl(talloc_tos(), acl->revision, num_aces,
336 samba_aces)) == NULL) {
337 DEBUG(0, ("Unable to malloc space for acl.\n"));
338 goto err_free;
341 *samba_acl = tmp_samba_acl;
342 SAFE_FREE(samba_aces);
343 return true;
344 err_free:
345 SAFE_FREE(samba_aces);
346 return false;
350 * @brief Reorder ACLs into the "correct" order for Windows Explorer.
352 * Windows Explorer expects ACLs to be in a standard order (inherited first,
353 * then deny, then permit.) When ACLs are composed from POSIX file permissions
354 * bits, they may not match these expectations, generating an annoying warning
355 * dialog for the user. This function will, if configured appropriately,
356 * reorder the ACLs for these "synthetic" (POSIX-derived) descriptors to prevent
357 * this. The list is changed within the security descriptor passed in.
359 * @param fsp files_struct with service configs; must not be NULL
360 * @param sd security descriptor being normalized;
361 * sd->dacl->aces is rewritten in-place, so must not be NULL
362 * @return true on success, errno will be set on error
364 * @bug Although Windows Explorer likes the reordering, they seem to cause
365 * problems with Excel and Word sending back the reordered ACLs to us and
366 * changing policy; see Isilon bug 30165.
368 static bool
369 onefs_canon_acl(files_struct *fsp, struct ifs_security_descriptor *sd)
371 int error = 0;
372 int cur;
373 struct ifs_ace *new_aces = NULL;
374 int new_aces_count = 0;
375 SMB_STRUCT_STAT sbuf;
377 if (sd == NULL || sd->dacl == NULL || sd->dacl->num_aces == 0)
378 return true;
381 * Find out if this is a windows bit, and if the smb policy wants us to
382 * lie about the sd.
384 SMB_ASSERT(fsp != NULL);
385 switch (lp_parm_enum(SNUM(fsp->conn), PARM_ONEFS_TYPE,
386 PARM_ACL_WIRE_FORMAT, enum_onefs_acl_wire_format,
387 PARM_ACL_WIRE_FORMAT_DEFAULT)) {
388 case ACL_FORMAT_RAW:
389 return true;
391 case ACL_FORMAT_WINDOWS_SD:
392 error = SMB_VFS_FSTAT(fsp, &sbuf);
393 if (error)
394 return false;
396 if ((sbuf.st_ex_flags & SF_HASNTFSACL) != 0) {
397 DEBUG(10, ("Did not canonicalize ACLs because a "
398 "Windows ACL set was found for file %s\n",
399 fsp->fsp_name));
400 return true;
402 break;
404 case ACL_FORMAT_ALWAYS:
405 break;
407 default:
408 SMB_ASSERT(false);
409 return false;
412 new_aces = SMB_MALLOC_ARRAY(struct ifs_ace, sd->dacl->num_aces);
413 if (new_aces == NULL)
414 return false;
417 * By walking down the list 3 separate times, we can avoid the need
418 * to create multiple temp buffers and extra copies.
420 for (cur = 0; cur < sd->dacl->num_aces; cur++) {
421 if (sd->dacl->aces[cur].flags & IFS_ACE_FLAG_INHERITED_ACE)
422 new_aces[new_aces_count++] = sd->dacl->aces[cur];
425 for (cur = 0; cur < sd->dacl->num_aces; cur++) {
426 if (!(sd->dacl->aces[cur].flags & IFS_ACE_FLAG_INHERITED_ACE) &&
427 (sd->dacl->aces[cur].type == IFS_ACE_TYPE_ACCESS_DENIED))
428 new_aces[new_aces_count++] = sd->dacl->aces[cur];
431 for (cur = 0; cur < sd->dacl->num_aces; cur++) {
432 if (!(sd->dacl->aces[cur].flags & IFS_ACE_FLAG_INHERITED_ACE) &&
433 !(sd->dacl->aces[cur].type == IFS_ACE_TYPE_ACCESS_DENIED))
434 new_aces[new_aces_count++] = sd->dacl->aces[cur];
437 SMB_ASSERT(new_aces_count == sd->dacl->num_aces);
438 DEBUG(10, ("Performed canonicalization of ACLs for file %s\n",
439 fsp->fsp_name));
442 * At this point you would think we could just do this:
443 * SAFE_FREE(sd->dacl->aces);
444 * sd->dacl->aces = new_aces;
445 * However, in some cases the existing aces pointer does not point
446 * to the beginning of an allocated block. So we have to do a more
447 * expensive memcpy()
449 memcpy(sd->dacl->aces, new_aces,
450 sizeof(struct ifs_ace) * new_aces_count);
452 SAFE_FREE(new_aces);
453 return true;
458 * This enum is a helper for onefs_fget_nt_acl() to communicate with
459 * onefs_init_ace().
461 enum mode_ident { USR, GRP, OTH };
464 * Initializes an ACE for addition to a synthetic ACL.
466 static struct ifs_ace onefs_init_ace(struct connection_struct *conn,
467 mode_t mode,
468 bool isdir,
469 enum mode_ident ident)
471 struct ifs_ace result;
472 enum ifs_ace_rights r,w,x;
474 r = isdir ? UNIX_DIRECTORY_ACCESS_R : UNIX_ACCESS_R;
475 w = isdir ? UNIX_DIRECTORY_ACCESS_W : UNIX_ACCESS_W;
476 x = isdir ? UNIX_DIRECTORY_ACCESS_X : UNIX_ACCESS_X;
478 result.type = IFS_ACE_TYPE_ACCESS_ALLOWED;
479 result.ifs_flags = 0;
480 result.flags = isdir ? IFS_ACE_FLAG_CONTAINER_INHERIT :
481 IFS_ACE_FLAG_OBJECT_INHERIT;
482 result.flags |= IFS_ACE_FLAG_INHERIT_ONLY;
484 switch (ident) {
485 case USR:
486 result.access_mask =
487 ((mode & S_IRUSR) ? r : 0 ) |
488 ((mode & S_IWUSR) ? w : 0 ) |
489 ((mode & S_IXUSR) ? x : 0 );
490 if (lp_parm_bool(SNUM(conn), PARM_ONEFS_TYPE,
491 PARM_CREATOR_OWNER_GETS_FULL_CONTROL,
492 PARM_CREATOR_OWNER_GETS_FULL_CONTROL_DEFAULT))
493 result.access_mask |= GENERIC_ALL_ACCESS;
494 result.trustee.type = IFS_ID_TYPE_CREATOR_OWNER;
495 break;
496 case GRP:
497 result.access_mask =
498 ((mode & S_IRGRP) ? r : 0 ) |
499 ((mode & S_IWGRP) ? w : 0 ) |
500 ((mode & S_IXGRP) ? x : 0 );
501 result.trustee.type = IFS_ID_TYPE_CREATOR_GROUP;
502 break;
503 case OTH:
504 result.access_mask =
505 ((mode & S_IROTH) ? r : 0 ) |
506 ((mode & S_IWOTH) ? w : 0 ) |
507 ((mode & S_IXOTH) ? x : 0 );
508 result.trustee.type = IFS_ID_TYPE_EVERYONE;
509 break;
512 return result;
516 * This adds inheritable ACEs to the end of the DACL, with the ACEs
517 * being derived from the mode bits. This is useful for clients that have the
518 * MoveSecurityAttributes regkey set to 0 or are in Simple File Sharing Mode.
520 * On these clients, when copying files from one folder to another inside the
521 * same volume/share, the DACL is explicitely cleared. Without inheritable
522 * aces on the target folder the mode bits of the copied file are set to 000.
524 * See Isilon Bug 27990
526 * Note: This function allocates additional memory onto sd->dacl->aces, that
527 * must be freed by the caller.
529 static bool add_sfs_aces(files_struct *fsp, struct ifs_security_descriptor *sd)
531 int error;
532 SMB_STRUCT_STAT sbuf;
534 error = SMB_VFS_FSTAT(fsp, &sbuf);
535 if (error) {
536 DEBUG(0, ("Failed to stat %s in simple files sharing "
537 "compatibility mode. errno=%d\n",
538 fsp->fsp_name, errno));
539 return false;
542 /* Only continue if this is a synthetic ACL and a directory. */
543 if (S_ISDIR(sbuf.st_ex_mode) &&
544 (sbuf.st_ex_flags & SF_HASNTFSACL) == 0) {
545 struct ifs_ace new_aces[6];
546 struct ifs_ace *old_aces;
547 int i, num_aces_to_add = 0;
548 mode_t file_mode = 0, dir_mode = 0;
550 /* Use existing samba logic to derive the mode bits. */
551 file_mode = unix_mode(fsp->conn, 0, fsp->fsp_name, false);
552 dir_mode = unix_mode(fsp->conn, aDIR, fsp->fsp_name, false);
554 /* Initialize ACEs. */
555 new_aces[0] = onefs_init_ace(fsp->conn, file_mode, false, USR);
556 new_aces[1] = onefs_init_ace(fsp->conn, file_mode, false, GRP);
557 new_aces[2] = onefs_init_ace(fsp->conn, file_mode, false, OTH);
558 new_aces[3] = onefs_init_ace(fsp->conn, dir_mode, true, USR);
559 new_aces[4] = onefs_init_ace(fsp->conn, dir_mode, true, GRP);
560 new_aces[5] = onefs_init_ace(fsp->conn, dir_mode, true, OTH);
562 for (i = 0; i < 6; i++)
563 if (new_aces[i].access_mask != 0)
564 num_aces_to_add++;
566 /* Expand the ACEs array */
567 if (num_aces_to_add != 0) {
568 old_aces = sd->dacl->aces;
570 sd->dacl->aces = SMB_MALLOC_ARRAY(struct ifs_ace,
571 sd->dacl->num_aces + num_aces_to_add);
572 if (!sd->dacl->aces) {
573 DEBUG(0, ("Unable to malloc space for "
574 "new_aces: %d.\n",
575 sd->dacl->num_aces + num_aces_to_add));
576 return false;
578 memcpy(sd->dacl->aces, old_aces,
579 sizeof(struct ifs_ace) * sd->dacl->num_aces);
581 /* Add the new ACEs to the DACL. */
582 for (i = 0; i < 6; i++) {
583 if (new_aces[i].access_mask != 0) {
584 sd->dacl->aces[sd->dacl->num_aces] =
585 new_aces[i];
586 sd->dacl->num_aces++;
591 return true;
595 * Isilon-specific function for getting an NTFS ACL from an open file.
597 * @param[out] ppdesc SecDesc to allocate and fill in
599 * @return NTSTATUS based off errno on error
601 NTSTATUS
602 onefs_fget_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
603 uint32 security_info, SEC_DESC **ppdesc)
605 int error;
606 uint32_t sd_size = 0;
607 size_t size = 0;
608 struct ifs_security_descriptor *sd = NULL;
609 DOM_SID owner_sid, group_sid;
610 DOM_SID *ownerp, *groupp;
611 SEC_ACL *dacl, *sacl;
612 SEC_DESC *pdesc;
613 bool alloced = false;
614 bool new_aces_alloced = false;
615 bool fopened = false;
616 NTSTATUS status = NT_STATUS_OK;
618 START_PROFILE(syscall_get_sd);
620 *ppdesc = NULL;
622 DEBUG(5, ("Getting sd for file %s. security_info=%u\n",
623 fsp->fsp_name, security_info));
625 if (lp_parm_bool(SNUM(fsp->conn), PARM_ONEFS_TYPE,
626 PARM_IGNORE_SACLS, PARM_IGNORE_SACLS_DEFAULT)) {
627 DEBUG(5, ("Ignoring SACL on %s.\n", fsp->fsp_name));
628 security_info &= ~SACL_SECURITY_INFORMATION;
631 if (fsp->fh->fd == -1) {
632 if ((fsp->fh->fd = onefs_sys_create_file(handle->conn,
634 fsp->fsp_name,
641 INTERNAL_OPEN_ONLY,
643 NULL,
645 NULL)) == -1) {
646 DEBUG(0, ("Error opening file %s. errno=%d (%s)\n",
647 fsp->fsp_name, errno, strerror(errno)));
648 status = map_nt_error_from_unix(errno);
649 goto out;
651 fopened = true;
654 /* Get security descriptor */
655 sd_size = 0;
656 do {
657 /* Allocate memory for get_security_descriptor */
658 if (sd_size > 0) {
659 sd = SMB_REALLOC(sd, sd_size);
660 if (!sd) {
661 DEBUG(0, ("Unable to malloc %u bytes of space "
662 "for security descriptor.\n", sd_size));
663 status = map_nt_error_from_unix(errno);
664 goto out;
667 alloced = true;
670 error = ifs_get_security_descriptor(fsp->fh->fd, security_info,
671 sd_size, &sd_size, sd);
672 if (error && (errno != EMSGSIZE)) {
673 DEBUG(0, ("Failed getting size of security descriptor! "
674 "errno=%d\n", errno));
675 status = map_nt_error_from_unix(errno);
676 goto out;
678 } while (error);
680 DEBUG(5, ("Got sd, size=%u:\n", sd_size));
682 if (lp_parm_bool(SNUM(fsp->conn),
683 PARM_ONEFS_TYPE,
684 PARM_SIMPLE_FILE_SHARING_COMPATIBILITY_MODE,
685 PARM_SIMPLE_FILE_SHARING_COMPATIBILITY_MODE_DEFAULT) &&
686 sd->dacl) {
687 if(!(new_aces_alloced = add_sfs_aces(fsp, sd)))
688 goto out;
691 if (!(onefs_canon_acl(fsp, sd))) {
692 status = map_nt_error_from_unix(errno);
693 goto out;
696 DEBUG(5, ("Finished canonicalizing ACL\n"));
698 ownerp = NULL;
699 groupp = NULL;
700 dacl = NULL;
701 sacl = NULL;
703 /* Copy owner into ppdesc */
704 if (security_info & OWNER_SECURITY_INFORMATION) {
705 if (!onefs_identity_to_sid(sd->owner, &owner_sid)) {
706 status = NT_STATUS_INVALID_PARAMETER;
707 goto out;
710 ownerp = &owner_sid;
713 /* Copy group into ppdesc */
714 if (security_info & GROUP_SECURITY_INFORMATION) {
715 if (!onefs_identity_to_sid(sd->group, &group_sid)) {
716 status = NT_STATUS_INVALID_PARAMETER;
717 goto out;
720 groupp = &group_sid;
723 /* Copy DACL into ppdesc */
724 if (security_info & DACL_SECURITY_INFORMATION) {
725 if (!onefs_acl_to_samba_acl(sd->dacl, &dacl)) {
726 status = NT_STATUS_INVALID_PARAMETER;
727 goto out;
731 /* Copy SACL into ppdesc */
732 if (security_info & SACL_SECURITY_INFORMATION) {
733 if (!onefs_acl_to_samba_acl(sd->sacl, &sacl)) {
734 status = NT_STATUS_INVALID_PARAMETER;
735 goto out;
739 /* AUTO_INHERIT_REQ bits are not returned over the wire so strip them
740 * off. Eventually we should stop storing these in the kernel
741 * all together. See Isilon bug 40364 */
742 sd->control &= ~(IFS_SD_CTRL_DACL_AUTO_INHERIT_REQ |
743 IFS_SD_CTRL_SACL_AUTO_INHERIT_REQ);
745 pdesc = make_sec_desc(talloc_tos(), sd->revision, sd->control,
746 ownerp, groupp, sacl, dacl, &size);
748 if (!pdesc) {
749 DEBUG(0, ("Problem with make_sec_desc. Memory?\n"));
750 status = map_nt_error_from_unix(errno);
751 goto out;
754 *ppdesc = pdesc;
756 DEBUG(5, ("Finished retrieving/canonicalizing SD!\n"));
757 /* FALLTHROUGH */
758 out:
760 END_PROFILE(syscall_get_sd);
762 if (alloced && sd) {
763 if (new_aces_alloced && sd->dacl->aces)
764 SAFE_FREE(sd->dacl->aces);
766 SAFE_FREE(sd);
769 if (fopened) {
770 close(fsp->fh->fd);
771 fsp->fh->fd = -1;
774 return status;
778 * Isilon-specific function for getting an NTFS ACL from a file path.
780 * Since onefs_fget_nt_acl() needs to open a filepath if the fd is invalid,
781 * we just mock up a files_struct with the path and bad fd and call into it.
783 * @param[out] ppdesc SecDesc to allocate and fill in
785 * @return NTSTATUS based off errno on error
787 NTSTATUS
788 onefs_get_nt_acl(vfs_handle_struct *handle, const char* name,
789 uint32 security_info, SEC_DESC **ppdesc)
791 files_struct finfo;
792 struct fd_handle fh;
794 ZERO_STRUCT(finfo);
795 ZERO_STRUCT(fh);
797 finfo.fnum = -1;
798 finfo.conn = handle->conn;
799 finfo.fh = &fh;
800 finfo.fh->fd = -1;
801 finfo.fsp_name = CONST_DISCARD(char *, name);
803 return onefs_fget_nt_acl(handle, &finfo, security_info, ppdesc);
807 * Isilon-specific function for setting up an ifs_security_descriptor, given a
808 * samba SEC_DESC.
810 * @param[out] sd ifs_security_descriptor to fill in
812 * @return NTSTATUS_OK if successful
814 NTSTATUS onefs_samba_sd_to_sd(uint32_t security_info_sent, SEC_DESC *psd,
815 struct ifs_security_descriptor *sd, int snum,
816 uint32_t *security_info_effective)
818 struct ifs_security_acl *daclp, *saclp;
819 struct ifs_identity owner, group, *ownerp, *groupp;
820 bool ignore_aces;
822 ownerp = NULL;
823 groupp = NULL;
824 daclp = NULL;
825 saclp = NULL;
827 *security_info_effective = security_info_sent;
829 /* Setup owner */
830 if (security_info_sent & OWNER_SECURITY_INFORMATION) {
831 if (!onefs_og_to_identity(psd->owner_sid, &owner, false, snum))
832 return NT_STATUS_ACCESS_DENIED;
834 SMB_ASSERT(owner.id.uid >= 0);
836 ownerp = &owner;
839 /* Setup group */
840 if (security_info_sent & GROUP_SECURITY_INFORMATION) {
841 if (!onefs_og_to_identity(psd->group_sid, &group, true, snum))
842 return NT_STATUS_ACCESS_DENIED;
844 SMB_ASSERT(group.id.gid >= 0);
846 groupp = &group;
849 /* Setup DACL */
850 if ((security_info_sent & DACL_SECURITY_INFORMATION) && (psd->dacl)) {
851 if (!onefs_samba_acl_to_acl(psd->dacl, &daclp, &ignore_aces,
852 snum))
853 return NT_STATUS_ACCESS_DENIED;
855 if (ignore_aces == true)
856 *security_info_effective &= ~DACL_SECURITY_INFORMATION;
859 /* Setup SACL */
860 if (security_info_sent & SACL_SECURITY_INFORMATION) {
862 if (lp_parm_bool(snum, PARM_ONEFS_TYPE,
863 PARM_IGNORE_SACLS, PARM_IGNORE_SACLS_DEFAULT)) {
864 DEBUG(5, ("Ignoring SACL.\n"));
865 *security_info_effective &= ~SACL_SECURITY_INFORMATION;
866 } else {
867 if (psd->sacl) {
868 if (!onefs_samba_acl_to_acl(psd->sacl,
869 &saclp, &ignore_aces, snum))
870 return NT_STATUS_ACCESS_DENIED;
872 if (ignore_aces == true) {
873 *security_info_effective &=
874 ~SACL_SECURITY_INFORMATION;
880 /* Setup ifs_security_descriptor */
881 DEBUG(5,("Setting up SD\n"));
882 if (aclu_initialize_sd(sd, psd->type, ownerp, groupp,
883 (daclp ? &daclp : NULL), (saclp ? &saclp : NULL), false))
884 return NT_STATUS_ACCESS_DENIED;
886 DEBUG(10, ("sec_info_sent: 0x%x, sec_info_effective: 0x%x.\n",
887 security_info_sent, *security_info_effective));
889 return NT_STATUS_OK;
893 * Isilon-specific function for setting an NTFS ACL on an open file.
895 * @return NT_STATUS_UNSUCCESSFUL for userspace errors, NTSTATUS based off
896 * errno on syscall errors
898 NTSTATUS
899 onefs_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
900 uint32_t sec_info_sent, SEC_DESC *psd)
902 struct ifs_security_descriptor sd = {};
903 int fd = -1;
904 bool fopened = false;
905 NTSTATUS status;
906 uint32_t sec_info_effective = 0;
908 START_PROFILE(syscall_set_sd);
910 DEBUG(5,("Setting SD on file %s.\n", fsp->fsp_name ));
912 status = onefs_samba_sd_to_sd(sec_info_sent, psd, &sd,
913 SNUM(handle->conn), &sec_info_effective);
915 if (!NT_STATUS_IS_OK(status)) {
916 DEBUG(3, ("SD initialization failure: %s\n", nt_errstr(status)));
917 goto out;
920 fd = fsp->fh->fd;
921 if (fd == -1) {
922 DEBUG(10,("Reopening file %s.\n", fsp->fsp_name));
923 if ((fd = onefs_sys_create_file(handle->conn,
925 fsp->fsp_name,
932 INTERNAL_OPEN_ONLY,
934 NULL,
936 NULL)) == -1) {
937 DEBUG(0, ("Error opening file %s. errno=%d (%s)\n",
938 fsp->fsp_name, errno, strerror(errno)));
939 status = map_nt_error_from_unix(errno);
940 goto out;
942 fopened = true;
945 errno = 0;
946 if (ifs_set_security_descriptor(fd, sec_info_effective, &sd)) {
947 DEBUG(0, ("Error setting security descriptor = %s\n",
948 strerror(errno)));
949 status = map_nt_error_from_unix(errno);
950 goto out;
953 DEBUG(5, ("Security descriptor set correctly!\n"));
954 status = NT_STATUS_OK;
956 /* FALLTHROUGH */
957 out:
958 END_PROFILE(syscall_set_sd);
960 if (fopened)
961 close(fd);
963 aclu_free_sd(&sd, false);
964 return status;