summaryrefslogtreecommitdiffstats
path: root/source3/modules/onefs_acl.c
blob: 2593012805462063b9657a45fc24602c6a2f2587 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
/*
 * Unix SMB/CIFS implementation.
 *
 * Support for OneFS native NTFS ACLs
 *
 * Copyright (C) Steven Danneman, 2008
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "includes.h"
#include "onefs.h"
#include "onefs_config.h"

#include <isi_acl/isi_acl_util.h>
#include <ifs/ifs_syscalls.h>
#include <sys/isi_acl.h>

const struct enum_list enum_onefs_acl_wire_format[] = {
	{ACL_FORMAT_RAW,  "No Format"},
	{ACL_FORMAT_WINDOWS_SD, "Format Windows SD"},
	{ACL_FORMAT_ALWAYS, "Always Format SD"},
	{-1, NULL}
};

/**
 * Turn SID into UID/GID and setup a struct ifs_identity
 */
static bool
onefs_sid_to_identity(const DOM_SID *sid, struct ifs_identity *id,
    bool is_group)
{
	enum ifs_identity_type type = IFS_ID_TYPE_LAST+1;
	uid_t uid = 0;
	gid_t gid = 0;

	if (!sid || sid_equal(sid, &global_sid_NULL))
		type = IFS_ID_TYPE_NULL;
	else if (sid_equal(sid, &global_sid_World))
		type = IFS_ID_TYPE_EVERYONE;
	else if (sid_equal(sid, &global_sid_Creator_Owner))
		type = IFS_ID_TYPE_CREATOR_OWNER;
	else if (sid_equal(sid, &global_sid_Creator_Group))
		type = IFS_ID_TYPE_CREATOR_GROUP;
	else if (is_group) {
		if (!sid_to_gid(sid, &gid))
			return false;
		type = IFS_ID_TYPE_GID;
	} else {
		if (sid_to_uid(sid, &uid))
			type = IFS_ID_TYPE_UID;
		else if (sid_to_gid(sid, &gid))
			type = IFS_ID_TYPE_GID;
		else
			return false;
	}

	if (aclu_initialize_identity(id, type, uid, gid, is_group)) {
		DEBUG(3, ("Call to aclu_initialize_identity failed! id=%x, "
		    "type=%d, uid=%u, gid=%u, is_group=%d\n",
		    (unsigned int)id, type, uid, gid, is_group));
		return false;
	}

	return true;
}

/**
 * Turn struct ifs_identity into SID
 */
static bool
onefs_identity_to_sid(struct ifs_identity *id, DOM_SID *sid)
{
	if (!id || !sid)
		return false;

	if (id->type >= IFS_ID_TYPE_LAST)
		return false;

	switch (id->type) {
	    case IFS_ID_TYPE_UID:
	        uid_to_sid(sid, id->id.uid);
		break;
	    case IFS_ID_TYPE_GID:
		gid_to_sid(sid, id->id.gid);
		break;
	    case IFS_ID_TYPE_EVERYONE:
		sid_copy(sid, &global_sid_World);
		break;
	    case IFS_ID_TYPE_NULL:
		sid_copy(sid, &global_sid_NULL);
		break;
	    case IFS_ID_TYPE_CREATOR_OWNER:
		sid_copy(sid, &global_sid_Creator_Owner);
		break;
	    case IFS_ID_TYPE_CREATOR_GROUP:
		sid_copy(sid, &global_sid_Creator_Group);
		break;
	    default:
		DEBUG(0, ("Unknown identity type: %d\n", id->type));
		return false;
	}

	return true;
}

static bool
onefs_og_to_identity(DOM_SID *sid, struct ifs_identity * ident,
    bool is_group, int snum)
{
	const DOM_SID *b_admin_sid = &global_sid_Builtin_Administrators;

	if (!onefs_sid_to_identity(sid, ident, is_group)) {
		if (!lp_parm_bool(snum, PARM_ONEFS_TYPE,
		     PARM_UNMAPPABLE_SIDS_IGNORE,
		     PARM_UNMAPPABLE_SIDS_IGNORE_DEFAULT)) {
			DEBUG(3, ("Unresolvable SID (%s) found.\n",
				sid_string_dbg(sid)));
			return false;
		}
		if (!onefs_sid_to_identity(b_admin_sid, ident, is_group)) {
			return false;
		}
		DEBUG(3, ("Mapping unresolvable owner SID (%s) to Builtin "
			"Administrators group.\n",
			sid_string_dbg(sid)));
	}
	return true;
}

static bool
sid_in_ignore_list(DOM_SID * sid, int snum)
{
	const char ** sid_list = NULL;
	DOM_SID match;

	sid_list = lp_parm_string_list(snum, PARM_ONEFS_TYPE,
	    PARM_UNMAPPABLE_SIDS_IGNORE_LIST,
	    PARM_UNMAPPABLE_SIDS_IGNORE_LIST_DEFAULT);

	/* Fast path a NULL list */
	if (!sid_list || *sid_list == NULL)
		return false;

	while (*sid_list) {
		if (string_to_sid(&match, *sid_list))
			if (sid_equal(sid, &match))
				return true;
		sid_list++;
	}

	return false;
}

/**
 * Convert a trustee to a struct identity
 */
static bool
onefs_samba_ace_to_ace(SEC_ACE * samba_ace, struct ifs_ace * ace,
    bool *mapped, int snum)
{
	struct ifs_identity ident = {.type=IFS_ID_TYPE_LAST, .id.uid=0};

	SMB_ASSERT(ace);
	SMB_ASSERT(mapped);
	SMB_ASSERT(samba_ace);

	if (onefs_sid_to_identity(&samba_ace->trustee, &ident, false)) {
		*mapped = true;
	} else {

		SMB_ASSERT(ident.id.uid >= 0);

		/* Ignore the sid if it's in the list */
		if (sid_in_ignore_list(&samba_ace->trustee, snum)) {
			DEBUG(3, ("Silently failing to set ACE for SID (%s) "
				"because it is in the ignore sids list\n",
				sid_string_dbg(&samba_ace->trustee)));
			*mapped = false;
		} else if ((samba_ace->type == SEC_ACE_TYPE_ACCESS_DENIED) &&
		    lp_parm_bool(snum, PARM_ONEFS_TYPE,
		    PARM_UNMAPPABLE_SIDS_DENY_EVERYONE,
		    PARM_UNMAPPABLE_SIDS_DENY_EVERYONE_DEFAULT)) {
			/* If the ace is deny translated to Everyone */
			DEBUG(3, ("Mapping unresolvable deny ACE SID (%s) "
				"to Everyone.\n",
				sid_string_dbg(&samba_ace->trustee)));
			if (aclu_initialize_identity(&ident,
				IFS_ID_TYPE_EVERYONE, 0, 0, False) != 0) {
				DEBUG(2, ("aclu_initialize_identity() "
					"failed making Everyone\n"));
				return false;
			}
			*mapped = true;
		} else if (lp_parm_bool(snum, PARM_ONEFS_TYPE,
			   PARM_UNMAPPABLE_SIDS_IGNORE,
			   PARM_UNMAPPABLE_SIDS_IGNORE_DEFAULT)) {
			DEBUG(3, ("Silently failing to set ACE for SID (%s) "
				"because it is unresolvable\n",
				sid_string_dbg(&samba_ace->trustee)));
			*mapped = false;
		} else {
			/* Fail for lack of a better option */
			return false;
		}
	}

	if (*mapped) {
		if (aclu_initialize_ace(ace, samba_ace->type,
			samba_ace->access_mask, samba_ace->flags, 0,
			&ident))
			return false;

		if ((ace->trustee.type == IFS_ID_TYPE_CREATOR_OWNER ||
			ace->trustee.type == IFS_ID_TYPE_CREATOR_GROUP) &&
		    nt4_compatible_acls())
			ace->flags |= SEC_ACE_FLAG_INHERIT_ONLY;
	}

	return true;
}

/**
 * Convert a SEC_ACL to a struct ifs_security_acl
 */
static bool
onefs_samba_acl_to_acl(SEC_ACL *samba_acl, struct ifs_security_acl **acl,
    bool * ignore_aces, int snum)
{
	int num_aces = 0;
	struct ifs_ace *aces = NULL;
	SEC_ACE *samba_aces;
	bool mapped;
	int i, j;

	SMB_ASSERT(ignore_aces);

	if ((!acl) || (!samba_acl))
		return false;

	samba_aces = samba_acl->aces;

	if (samba_acl->num_aces > 0 && samba_aces) {
		/* Setup ACES */
		num_aces = samba_acl->num_aces;
		aces = SMB_MALLOC_ARRAY(struct ifs_ace, num_aces);

		for (i = 0, j = 0; j < num_aces; i++, j++) {
			if (!onefs_samba_ace_to_ace(&samba_aces[j],
				&aces[i], &mapped, snum))
				goto err_free;

			if (!mapped)
				i--;
		}
		num_aces = i;
	}

	/* If aces are given but we cannot apply them due to the reasons
	 * above we do not change the SD.  However, if we are told to
	 * explicitly set an SD with 0 aces we honor this operation */
	*ignore_aces = samba_acl->num_aces > 0 && num_aces < 1;

	if (*ignore_aces == false)
		if (aclu_initialize_acl(acl, aces, num_aces))
			goto err_free;

	/* Currently aclu_initialize_acl should copy the aces over, allowing
	 * us to immediately free */
	free(aces);
	return true;

err_free:
	free(aces);
	return false;
}

/**
 * Convert a struct ifs_security_acl to a SEC_ACL
 */
static bool
onefs_acl_to_samba_acl(struct ifs_security_acl *acl, SEC_ACL **samba_acl)
{
	SEC_ACE *samba_aces = NULL;
	SEC_ACL *tmp_samba_acl = NULL;
	int i, num_aces = 0;

	if (!samba_acl)
		return false;

	/* NULL ACL */
	if (!acl) {
		*samba_acl = NULL;
		return true;
	}

	/* Determine number of aces in ACL */
	if (!acl->aces)
		num_aces = 0;
	else
		num_aces = acl->num_aces;

	/* Allocate the ace list. */
	if (num_aces > 0) {
		if ((samba_aces = SMB_MALLOC_ARRAY(SEC_ACE, num_aces)) == NULL)
		{
			DEBUG(0, ("Unable to malloc space for %d aces.\n",
			    num_aces));
			return false;
		}
		memset(samba_aces, '\0', (num_aces) * sizeof(SEC_ACE));
	}

	for (i = 0; i < num_aces; i++) {
		DOM_SID sid;

		if (!onefs_identity_to_sid(&acl->aces[i].trustee, &sid))
			goto err_free;

		init_sec_ace(&samba_aces[i], &sid, acl->aces[i].type,
		    acl->aces[i].access_mask, acl->aces[i].flags);
	}

	if ((tmp_samba_acl = make_sec_acl(talloc_tos(), acl->revision, num_aces,
	    samba_aces)) == NULL) {
	       DEBUG(0, ("Unable to malloc space for acl.\n"));
	       goto err_free;
	}

	*samba_acl = tmp_samba_acl;
	SAFE_FREE(samba_aces);
	return true;
err_free:
	SAFE_FREE(samba_aces);
	return false;
}

/**
 * @brief Reorder ACLs into the "correct" order for Windows Explorer.
 *
 * Windows Explorer expects ACLs to be in a standard order (inherited first,
 * then deny, then permit.)  When ACLs are composed from POSIX file permissions
 * bits, they may not match these expectations, generating an annoying warning
 * dialog for the user.  This function will, if configured appropriately,
 * reorder the ACLs for these "synthetic" (POSIX-derived) descriptors to prevent
 * this.  The list is changed within the security descriptor passed in.
 *
 * @param fsp files_struct with service configs; must not be NULL
 * @param sd security descriptor being normalized;
 *           sd->dacl->aces is rewritten in-place, so must not be NULL
 * @return true on success, errno will be set on error
 *
 * @bug Although Windows Explorer likes the reordering, they seem to cause
 *  problems with Excel and Word sending back the reordered ACLs to us and
 *  changing policy; see Isilon bug 30165.
 */
static bool
onefs_canon_acl(files_struct *fsp, struct ifs_security_descriptor *sd)
{
	int error = 0;
	int cur;
	struct ifs_ace *new_aces = NULL;
	int new_aces_count = 0;
	SMB_STRUCT_STAT sbuf;

	if (sd == NULL || sd->dacl == NULL || sd->dacl->num_aces == 0)
		return true;

	/*
	 * Find out if this is a windows bit, and if the smb policy wants us to
	 * lie about the sd.
	 */
	SMB_ASSERT(fsp != NULL);
	switch (lp_parm_enum(SNUM(fsp->conn), PARM_ONEFS_TYPE,
		PARM_ACL_WIRE_FORMAT, enum_onefs_acl_wire_format,
		PARM_ACL_WIRE_FORMAT_DEFAULT))  {
	case ACL_FORMAT_RAW:
		return true;

	case ACL_FORMAT_WINDOWS_SD:
		error = SMB_VFS_FSTAT(fsp, &sbuf);
		if (error)
			return false;

		if ((sbuf.st_ex_flags & SF_HASNTFSACL) != 0) {
			DEBUG(10, ("Did not canonicalize ACLs because a "
				   "Windows ACL set was found for file %s\n",
				   fsp_str_dbg(fsp)));
			return true;
		}
		break;

	case ACL_FORMAT_ALWAYS:
		break;

	default:
		SMB_ASSERT(false);
		return false;
	}

	new_aces = SMB_MALLOC_ARRAY(struct ifs_ace, sd->dacl->num_aces);
	if (new_aces == NULL)
		return false;

	/*
	 * By walking down the list 3 separate times, we can avoid the need
	 * to create multiple temp buffers and extra copies.
	 */

	/* Explict deny aces first */
	for (cur = 0; cur < sd->dacl->num_aces; cur++)  {
		if (!(sd->dacl->aces[cur].flags & IFS_ACE_FLAG_INHERITED_ACE) &&
		    (sd->dacl->aces[cur].type == IFS_ACE_TYPE_ACCESS_DENIED))
			new_aces[new_aces_count++] = sd->dacl->aces[cur];
	}

	/* Explict allow aces second */
	for (cur = 0; cur < sd->dacl->num_aces; cur++)  {
		if (!(sd->dacl->aces[cur].flags & IFS_ACE_FLAG_INHERITED_ACE) &&
		    !(sd->dacl->aces[cur].type == IFS_ACE_TYPE_ACCESS_DENIED))
			new_aces[new_aces_count++] = sd->dacl->aces[cur];
	}

	/* Inherited deny/allow aces third */
	for (cur = 0; cur < sd->dacl->num_aces; cur++)  {
		if ((sd->dacl->aces[cur].flags & IFS_ACE_FLAG_INHERITED_ACE))
			new_aces[new_aces_count++] = sd->dacl->aces[cur];
	}

	SMB_ASSERT(new_aces_count == sd->dacl->num_aces);
	DEBUG(10, ("Performed canonicalization of ACLs for file %s\n",
		   fsp_str_dbg(fsp)));

	/*
	 * At this point you would think we could just do this:
	 *   SAFE_FREE(sd->dacl->aces);
	 *   sd->dacl->aces = new_aces;
	 * However, in some cases the existing aces pointer does not point
	 * to the beginning of an allocated block.  So we have to do a more
	 * expensive memcpy()
	 */
	memcpy(sd->dacl->aces, new_aces,
	    sizeof(struct ifs_ace) * new_aces_count);

	SAFE_FREE(new_aces);
	return true;
}


/**
 * This enum is a helper for onefs_fget_nt_acl() to communicate with
 * onefs_init_ace().
 */
enum mode_ident { USR, GRP, OTH };

/**
 * Initializes an ACE for addition to a synthetic ACL.
 */
static struct ifs_ace onefs_init_ace(struct connection_struct *conn,
				     mode_t mode,
				     bool isdir,
				     enum mode_ident ident)
{
	struct ifs_ace result;
	enum ifs_ace_rights r,w,x;

	r = isdir ? UNIX_DIRECTORY_ACCESS_R : UNIX_ACCESS_R;
	w = isdir ? UNIX_DIRECTORY_ACCESS_W : UNIX_ACCESS_W;
	x = isdir ? UNIX_DIRECTORY_ACCESS_X : UNIX_ACCESS_X;

	result.type = IFS_ACE_TYPE_ACCESS_ALLOWED;
	result.ifs_flags = 0;
	result.flags = isdir ? IFS_ACE_FLAG_CONTAINER_INHERIT :
	    IFS_ACE_FLAG_OBJECT_INHERIT;
	result.flags |= IFS_ACE_FLAG_INHERIT_ONLY;

	switch (ident) {
	case USR:
		result.access_mask =
		    ((mode & S_IRUSR) ? r : 0 ) |
		    ((mode & S_IWUSR) ? w : 0 ) |
		    ((mode & S_IXUSR) ? x : 0 );
		if (lp_parm_bool(SNUM(conn), PARM_ONEFS_TYPE,
		    PARM_CREATOR_OWNER_GETS_FULL_CONTROL,
		    PARM_CREATOR_OWNER_GETS_FULL_CONTROL_DEFAULT))
			result.access_mask |= GENERIC_ALL_ACCESS;
		result.trustee.type = IFS_ID_TYPE_CREATOR_OWNER;
		break;
	case GRP:
		result.access_mask =
		    ((mode & S_IRGRP) ? r : 0 ) |
		    ((mode & S_IWGRP) ? w : 0 ) |
		    ((mode & S_IXGRP) ? x : 0 );
		result.trustee.type = IFS_ID_TYPE_CREATOR_GROUP;
		break;
	case OTH:
		result.access_mask =
		    ((mode & S_IROTH) ? r : 0 ) |
		    ((mode & S_IWOTH) ? w : 0 ) |
		    ((mode & S_IXOTH) ? x : 0 );
		result.trustee.type = IFS_ID_TYPE_EVERYONE;
		break;
	}

	return result;
}

/**
 * This adds inheritable ACEs to the end of the DACL, with the ACEs
 * being derived from the mode bits.  This is useful for clients that have the
 * MoveSecurityAttributes regkey set to 0 or are in Simple File Sharing Mode.
 *
 * On these clients, when copying files from one folder to another inside the
 * same volume/share, the DACL is explicitely cleared.  Without inheritable
 * aces on the target folder the mode bits of the copied file are set to 000.
 *
 * See Isilon Bug 27990
 *
 * Note: This function allocates additional memory onto sd->dacl->aces, that
 * must be freed by the caller.
 */
static bool add_sfs_aces(files_struct *fsp, struct ifs_security_descriptor *sd)
{
	int error;
	SMB_STRUCT_STAT sbuf;

	error = SMB_VFS_FSTAT(fsp, &sbuf);
	if (error) {
		DEBUG(0, ("Failed to stat %s in simple files sharing "
			  "compatibility mode. errno=%d\n",
			  fsp_str_dbg(fsp), errno));
		return false;
	}

	/* Only continue if this is a synthetic ACL and a directory. */
	if (S_ISDIR(sbuf.st_ex_mode) &&
	    (sbuf.st_ex_flags & SF_HASNTFSACL) == 0) {
		struct ifs_ace new_aces[6];
		struct ifs_ace *old_aces;
		int i, num_aces_to_add = 0;
		mode_t file_mode = 0, dir_mode = 0;

		/* Use existing samba logic to derive the mode bits. */
		file_mode = unix_mode(fsp->conn, 0, fsp->fsp_name, NULL);
		dir_mode = unix_mode(fsp->conn, aDIR, fsp->fsp_name, NULL);

		/* Initialize ACEs. */
		new_aces[0] = onefs_init_ace(fsp->conn, file_mode, false, USR);
		new_aces[1] = onefs_init_ace(fsp->conn, file_mode, false, GRP);
		new_aces[2] = onefs_init_ace(fsp->conn, file_mode, false, OTH);
		new_aces[3] = onefs_init_ace(fsp->conn, dir_mode, true, USR);
		new_aces[4] = onefs_init_ace(fsp->conn, dir_mode, true, GRP);
		new_aces[5] = onefs_init_ace(fsp->conn, dir_mode, true, OTH);

		for (i = 0; i < 6; i++)
			if (new_aces[i].access_mask != 0)
				num_aces_to_add++;

		/* Expand the ACEs array */
		if (num_aces_to_add != 0) {
			old_aces = sd->dacl->aces;

			sd->dacl->aces = SMB_MALLOC_ARRAY(struct ifs_ace,
			    sd->dacl->num_aces + num_aces_to_add);
			if (!sd->dacl->aces) {
				DEBUG(0, ("Unable to malloc space for "
				    "new_aces: %d.\n",
				     sd->dacl->num_aces + num_aces_to_add));
				return false;
			}
			memcpy(sd->dacl->aces, old_aces,
			    sizeof(struct ifs_ace) * sd->dacl->num_aces);

			/* Add the new ACEs to the DACL. */
			for (i = 0; i < 6; i++) {
				if (new_aces[i].access_mask != 0) {
					sd->dacl->aces[sd->dacl->num_aces] =
					    new_aces[i];
					sd->dacl->num_aces++;
				}
			}
		}
	}
	return true;
}

/**
 * Isilon-specific function for getting an NTFS ACL from an open file.
 *
 * @param[out] ppdesc SecDesc to allocate and fill in
 *
 * @return NTSTATUS based off errno on error
 */
NTSTATUS
onefs_fget_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
		  uint32 security_info, SEC_DESC **ppdesc)
{
	int error;
	uint32_t sd_size = 0;
	size_t size = 0;
	struct ifs_security_descriptor *sd = NULL;
	DOM_SID owner_sid, group_sid;
	DOM_SID *ownerp, *groupp;
	SEC_ACL *dacl, *sacl;
	SEC_DESC *pdesc;
	bool alloced = false;
	bool new_aces_alloced = false;
	bool fopened = false;
	NTSTATUS status = NT_STATUS_OK;

	START_PROFILE(syscall_get_sd);

	*ppdesc = NULL;

	DEBUG(5, ("Getting sd for file %s. security_info=%u\n",
		  fsp_str_dbg(fsp), security_info));

	if (lp_parm_bool(SNUM(fsp->conn), PARM_ONEFS_TYPE,
		PARM_IGNORE_SACLS, PARM_IGNORE_SACLS_DEFAULT)) {
		DEBUG(5, ("Ignoring SACL on %s.\n", fsp_str_dbg(fsp)));
		security_info &= ~SACL_SECURITY_INFORMATION;
	}

	if (fsp->fh->fd == -1) {
		if ((fsp->fh->fd = onefs_sys_create_file(handle->conn,
							 -1,
							 fsp->fsp_name->base_name,
							 0,
							 0,
							 0,
							 0,
							 0,
							 0,
							 INTERNAL_OPEN_ONLY,
							 0,
							 NULL,
							 0,
							 NULL)) == -1) {
			DEBUG(0, ("Error opening file %s. errno=%d (%s)\n",
				  fsp_str_dbg(fsp), errno, strerror(errno)));
			status = map_nt_error_from_unix(errno);
			goto out;
		}
		fopened = true;
	}

        /* Get security descriptor */
        sd_size = 0;
        do {
                /* Allocate memory for get_security_descriptor */
		if (sd_size > 0) {
	                sd = SMB_REALLOC(sd, sd_size);
			if (!sd) {
				DEBUG(0, ("Unable to malloc %u bytes of space "
				    "for security descriptor.\n", sd_size));
				status = map_nt_error_from_unix(errno);
				goto out;
			}

			alloced = true;
		}

                error = ifs_get_security_descriptor(fsp->fh->fd, security_info,
		    sd_size, &sd_size, sd);
                if (error && (errno != EMSGSIZE)) {
			DEBUG(0, ("Failed getting size of security descriptor! "
			    "errno=%d\n", errno));
			status = map_nt_error_from_unix(errno);
			goto out;
                }
        } while (error);

	DEBUG(5, ("Got sd, size=%u:\n", sd_size));

	if (lp_parm_bool(SNUM(fsp->conn),
	    PARM_ONEFS_TYPE,
	    PARM_SIMPLE_FILE_SHARING_COMPATIBILITY_MODE,
	    PARM_SIMPLE_FILE_SHARING_COMPATIBILITY_MODE_DEFAULT) &&
	    sd->dacl) {
		if(!(new_aces_alloced = add_sfs_aces(fsp, sd)))
			goto out;
	}

	if (!(onefs_canon_acl(fsp, sd))) {
		status = map_nt_error_from_unix(errno);
		goto out;
	}

	DEBUG(5, ("Finished canonicalizing ACL\n"));

	ownerp = NULL;
	groupp = NULL;
	dacl = NULL;
	sacl = NULL;

	/* Copy owner into ppdesc */
	if (security_info & OWNER_SECURITY_INFORMATION) {
		if (!onefs_identity_to_sid(sd->owner, &owner_sid)) {
			status = NT_STATUS_INVALID_PARAMETER;
			goto out;
		}

		ownerp = &owner_sid;
	}

	/* Copy group into ppdesc */
	if (security_info & GROUP_SECURITY_INFORMATION) {
		if (!onefs_identity_to_sid(sd->group, &group_sid)) {
			status = NT_STATUS_INVALID_PARAMETER;
			goto out;
		}

		groupp = &group_sid;
	}

	/* Copy DACL into ppdesc */
	if (security_info & DACL_SECURITY_INFORMATION) {
		if (!onefs_acl_to_samba_acl(sd->dacl, &dacl)) {
			status = NT_STATUS_INVALID_PARAMETER;
			goto out;
		}
	}

	/* Copy SACL into ppdesc */
	if (security_info & SACL_SECURITY_INFORMATION) {
		if (!onefs_acl_to_samba_acl(sd->sacl, &sacl)) {
			status = NT_STATUS_INVALID_PARAMETER;
			goto out;
		}
	}

	/* AUTO_INHERIT_REQ bits are not returned over the wire so strip them
	 * off.  Eventually we should stop storing these in the kernel
	 * all together. See Isilon bug 40364 */
	sd->control &= ~(IFS_SD_CTRL_DACL_AUTO_INHERIT_REQ |
			 IFS_SD_CTRL_SACL_AUTO_INHERIT_REQ);

	pdesc = make_sec_desc(talloc_tos(), sd->revision, sd->control,
	    ownerp, groupp, sacl, dacl, &size);

	if (!pdesc) {
		DEBUG(0, ("Problem with make_sec_desc. Memory?\n"));
		status = map_nt_error_from_unix(errno);
		goto out;
	}

	*ppdesc = pdesc;

	DEBUG(5, ("Finished retrieving/canonicalizing SD!\n"));
	/* FALLTHROUGH */
out:

	END_PROFILE(syscall_get_sd);

	if (alloced && sd) {
		if (new_aces_alloced && sd->dacl->aces)
			SAFE_FREE(sd->dacl->aces);

		SAFE_FREE(sd);
	}

	if (fopened) {
		close(fsp->fh->fd);
		fsp->fh->fd = -1;
	}

	return status;
}

/**
 * Isilon-specific function for getting an NTFS ACL from a file path.
 *
 * Since onefs_fget_nt_acl() needs to open a filepath if the fd is invalid,
 * we just mock up a files_struct with the path and bad fd and call into it.
 *
 * @param[out] ppdesc SecDesc to allocate and fill in
 *
 * @return NTSTATUS based off errno on error
 */
NTSTATUS
onefs_get_nt_acl(vfs_handle_struct *handle, const char* name,
		 uint32 security_info, SEC_DESC **ppdesc)
{
	files_struct finfo;
	struct fd_handle fh;
	NTSTATUS status;

	ZERO_STRUCT(finfo);
	ZERO_STRUCT(fh);

	finfo.fnum = -1;
	finfo.conn = handle->conn;
	finfo.fh = &fh;
	finfo.fh->fd = -1;
	status = create_synthetic_smb_fname(talloc_tos(), name, NULL, NULL,
					    &finfo.fsp_name);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	status = onefs_fget_nt_acl(handle, &finfo, security_info, ppdesc);

	TALLOC_FREE(finfo.fsp_name);
	return status;
}

/**
 * Isilon-specific function for setting up an ifs_security_descriptor, given a
 * samba SEC_DESC.
 *
 * @param[out] sd ifs_security_descriptor to fill in
 *
 * @return NTSTATUS_OK if successful
 */
NTSTATUS onefs_samba_sd_to_sd(uint32_t security_info_sent, const SEC_DESC *psd,
			      struct ifs_security_descriptor *sd, int snum,
			      uint32_t *security_info_effective)
{
	struct ifs_security_acl *daclp, *saclp;
	struct ifs_identity owner, group, *ownerp, *groupp;
	bool ignore_aces;

	ownerp = NULL;
	groupp = NULL;
	daclp = NULL;
	saclp = NULL;

	*security_info_effective = security_info_sent;

	/* Setup owner */
	if (security_info_sent & OWNER_SECURITY_INFORMATION) {
		if (!onefs_og_to_identity(psd->owner_sid, &owner, false, snum))
			return NT_STATUS_ACCESS_DENIED;

		SMB_ASSERT(owner.id.uid >= 0);

		ownerp = &owner;
	}

	/* Setup group */
	if (security_info_sent & GROUP_SECURITY_INFORMATION) {
		if (!onefs_og_to_identity(psd->group_sid, &group, true, snum))
			return NT_STATUS_ACCESS_DENIED;

		SMB_ASSERT(group.id.gid >= 0);

		groupp = &group;
	}

	/* Setup DACL */
	if ((security_info_sent & DACL_SECURITY_INFORMATION) && (psd->dacl)) {
		if (!onefs_samba_acl_to_acl(psd->dacl, &daclp, &ignore_aces,
			snum))
			return NT_STATUS_ACCESS_DENIED;

		if (ignore_aces == true)
			*security_info_effective &= ~DACL_SECURITY_INFORMATION;
	}

	/* Setup SACL */
	if (security_info_sent & SACL_SECURITY_INFORMATION) {

		if (lp_parm_bool(snum, PARM_ONEFS_TYPE,
			    PARM_IGNORE_SACLS, PARM_IGNORE_SACLS_DEFAULT)) {
			DEBUG(5, ("Ignoring SACL.\n"));
			*security_info_effective &= ~SACL_SECURITY_INFORMATION;
		} else {
			if (psd->sacl) {
				if (!onefs_samba_acl_to_acl(psd->sacl,
					&saclp, &ignore_aces, snum))
					return NT_STATUS_ACCESS_DENIED;

				if (ignore_aces == true) {
					*security_info_effective &=
					    ~SACL_SECURITY_INFORMATION;
				}
			}
		}
	}

	/* Setup ifs_security_descriptor */
	DEBUG(5,("Setting up SD\n"));
	if (aclu_initialize_sd(sd, psd->type, ownerp, groupp,
		(daclp ? &daclp : NULL), (saclp ? &saclp : NULL), false))
		return NT_STATUS_ACCESS_DENIED;

	DEBUG(10, ("sec_info_sent: 0x%x, sec_info_effective: 0x%x.\n",
		   security_info_sent, *security_info_effective));

	return NT_STATUS_OK;
}

/**
 * Isilon-specific function for setting an NTFS ACL on an open file.
 *
 * @return NT_STATUS_UNSUCCESSFUL for userspace errors, NTSTATUS based off
 * errno on syscall errors
 */
NTSTATUS
onefs_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
		  uint32_t sec_info_sent, const SEC_DESC *psd)
{
	struct ifs_security_descriptor sd = {};
	int fd = -1;
	bool fopened = false;
	NTSTATUS status;
	uint32_t sec_info_effective = 0;

	START_PROFILE(syscall_set_sd);

	DEBUG(5,("Setting SD on file %s.\n", fsp_str_dbg(fsp)));

	status = onefs_samba_sd_to_sd(sec_info_sent, psd, &sd,
				      SNUM(handle->conn), &sec_info_effective);

	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(3, ("SD initialization failure: %s\n", nt_errstr(status)));
		goto out;
	}

	fd = fsp->fh->fd;
	if (fd == -1) {
		DEBUG(10,("Reopening file %s.\n", fsp_str_dbg(fsp)));
		if ((fd = onefs_sys_create_file(handle->conn,
						-1,
						fsp->fsp_name->base_name,
						0,
						0,
						0,
						0,
						0,
						0,
						INTERNAL_OPEN_ONLY,
						0,
						NULL,
						0,
						NULL)) == -1) {
			DEBUG(0, ("Error opening file %s. errno=%d (%s)\n",
				  fsp_str_dbg(fsp), errno, strerror(errno)));
			status = map_nt_error_from_unix(errno);
			goto out;
		}
		fopened = true;
	}

        errno = 0;
	if (ifs_set_security_descriptor(fd, sec_info_effective, &sd)) {
		DEBUG(0, ("Error setting security descriptor = %s\n",
			  strerror(errno)));
		status = map_nt_error_from_unix(errno);
		goto out;
	}

	DEBUG(5, ("Security descriptor set correctly!\n"));
	status = NT_STATUS_OK;

	/* FALLTHROUGH */
out:
	END_PROFILE(syscall_set_sd);

	if (fopened)
		close(fd);

	aclu_free_sd(&sd, false);
	return status;
}