s3 onefs: rename custom parameter to plural to match previous usage
[samba.git] / source3 / modules / onefs_acl.c
1 /*
2  * Unix SMB/CIFS implementation.
3  *
4  * Support for OneFS native NTFS ACLs
5  *
6  * Copyright (C) Steven Danneman, 2008
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
22 #include "onefs.h"
23
24 #include <isi_acl/isi_acl_util.h>
25 #include <ifs/ifs_syscalls.h>
26
27 const struct enum_list enum_onefs_acl_wire_format[] = {
28         {ACL_FORMAT_RAW,  "No Format"},
29         {ACL_FORMAT_WINDOWS_SD, "Format Windows SD"},
30         {ACL_FORMAT_ALWAYS, "Always Format SD"},
31         {-1, NULL}
32 };
33
34 /**
35  * Turn SID into UID/GID and setup a struct ifs_identity
36  */
37 static bool
38 onefs_sid_to_identity(const DOM_SID *sid, struct ifs_identity *id,
39     bool is_group)
40 {
41         enum ifs_identity_type type = IFS_ID_TYPE_LAST+1;
42         uid_t uid = 0;
43         gid_t gid = 0;
44
45         if (!sid || sid_equal(sid, &global_sid_NULL))
46                 type = IFS_ID_TYPE_NULL;
47         else if (sid_equal(sid, &global_sid_World))
48                 type = IFS_ID_TYPE_EVERYONE;
49         else if (sid_equal(sid, &global_sid_Creator_Owner))
50                 type = IFS_ID_TYPE_CREATOR_OWNER;
51         else if (sid_equal(sid, &global_sid_Creator_Group))
52                 type = IFS_ID_TYPE_CREATOR_GROUP;
53         else if (is_group) {
54                 if (!sid_to_gid(sid, &gid))
55                         return false;
56                 type = IFS_ID_TYPE_GID;
57         } else {
58                 if (sid_to_uid(sid, &uid))
59                         type = IFS_ID_TYPE_UID;
60                 else if (sid_to_gid(sid, &gid))
61                         type = IFS_ID_TYPE_GID;
62                 else
63                         return false;
64         }
65
66         if (aclu_initialize_identity(id, type, uid, gid, is_group)) {
67                 DEBUG(3, ("Call to aclu_initialize_identity failed! id=%x, "
68                     "type=%d, uid=%u, gid=%u, is_group=%d\n",
69                     (unsigned int)id, type, uid, gid, is_group));
70                 return false;
71         }
72
73         return true;
74 }
75
76 /**
77  * Turn struct ifs_identity into SID
78  */
79 static bool
80 onefs_identity_to_sid(struct ifs_identity *id, DOM_SID *sid)
81 {
82         if (!id || !sid)
83                 return false;
84
85         if (id->type >= IFS_ID_TYPE_LAST)
86                 return false;
87
88         switch (id->type) {
89             case IFS_ID_TYPE_UID:
90                 uid_to_sid(sid, id->id.uid);
91                 break;
92             case IFS_ID_TYPE_GID:
93                 gid_to_sid(sid, id->id.gid);
94                 break;
95             case IFS_ID_TYPE_EVERYONE:
96                 sid_copy(sid, &global_sid_World);
97                 break;
98             case IFS_ID_TYPE_NULL:
99                 sid_copy(sid, &global_sid_NULL);
100                 break;
101             case IFS_ID_TYPE_CREATOR_OWNER:
102                 sid_copy(sid, &global_sid_Creator_Owner);
103                 break;
104             case IFS_ID_TYPE_CREATOR_GROUP:
105                 sid_copy(sid, &global_sid_Creator_Group);
106                 break;
107             default:
108                 DEBUG(0, ("Unknown identity type: %d\n", id->type));
109                 return false;
110         }
111
112         return true;
113 }
114
115 static bool
116 onefs_og_to_identity(DOM_SID *sid, struct ifs_identity * ident,
117     bool is_group, int snum)
118 {
119         const DOM_SID *b_admin_sid = &global_sid_Builtin_Administrators;
120
121         if (!onefs_sid_to_identity(sid, ident, is_group)) {
122                 if (!lp_parm_bool(snum, PARM_ONEFS_TYPE,
123                      PARM_UNMAPPABLE_SIDS_IGNORE,
124                      PARM_UNMAPPABLE_SIDS_IGNORE_DEFAULT)) {
125                         DEBUG(3, ("Unresolvable SID (%s) found.\n",
126                                 sid_string_dbg(sid)));
127                         return false;
128                 }
129                 if (!onefs_sid_to_identity(b_admin_sid, ident, is_group)) {
130                         return false;
131                 }
132                 DEBUG(3, ("Mapping unresolvable owner SID (%s) to Builtin "
133                         "Administrators group.\n",
134                         sid_string_dbg(sid)));
135         }
136         return true;
137 }
138
139 static bool
140 sid_in_ignore_list(DOM_SID * sid, int snum)
141 {
142         const char ** sid_list = NULL;
143         DOM_SID match;
144
145         sid_list = lp_parm_string_list(snum, PARM_ONEFS_TYPE,
146             PARM_UNMAPPABLE_SIDS_IGNORE_LIST,
147             PARM_UNMAPPABLE_SIDS_IGNORE_LIST_DEFAULT);
148
149         /* Fast path a NULL list */
150         if (!sid_list || *sid_list == NULL)
151                 return false;
152
153         while (*sid_list) {
154                 if (string_to_sid(&match, *sid_list))
155                         if (sid_equal(sid, &match))
156                                 return true;
157                 sid_list++;
158         }
159
160         return false;
161 }
162
163 /**
164  * Convert a trustee to a struct identity
165  */
166 static bool
167 onefs_samba_ace_to_ace(SEC_ACE * samba_ace, struct ifs_ace * ace,
168     bool *mapped, int snum)
169 {
170         struct ifs_identity ident = {.type=IFS_ID_TYPE_LAST, .id.uid=0};
171
172         SMB_ASSERT(ace);
173         SMB_ASSERT(mapped);
174         SMB_ASSERT(samba_ace);
175
176         if (onefs_sid_to_identity(&samba_ace->trustee, &ident, false)) {
177                 *mapped = true;
178         } else {
179
180                 SMB_ASSERT(ident.id.uid >= 0);
181
182                 /* Ignore the sid if it's in the list */
183                 if (sid_in_ignore_list(&samba_ace->trustee, snum)) {
184                         DEBUG(3, ("Silently failing to set ACE for SID (%s) "
185                                 "because it is in the ignore sids list\n",
186                                 sid_string_dbg(&samba_ace->trustee)));
187                         *mapped = false;
188                 } else if ((samba_ace->type == SEC_ACE_TYPE_ACCESS_DENIED) &&
189                     lp_parm_bool(snum, PARM_ONEFS_TYPE,
190                     PARM_UNMAPPABLE_SIDS_DENY_EVERYONE,
191                     PARM_UNMAPPABLE_SIDS_DENY_EVERYONE_DEFAULT)) {
192                         /* If the ace is deny translated to Everyone */
193                         DEBUG(3, ("Mapping unresolvable deny ACE SID (%s) "
194                                 "to Everyone.\n",
195                                 sid_string_dbg(&samba_ace->trustee)));
196                         if (aclu_initialize_identity(&ident,
197                                 IFS_ID_TYPE_EVERYONE, 0, 0, False) != 0) {
198                                 DEBUG(2, ("aclu_initialize_identity() "
199                                         "failed making Everyone\n"));
200                                 return false;
201                         }
202                         *mapped = true;
203                 } else if (lp_parm_bool(snum, PARM_ONEFS_TYPE,
204                            PARM_UNMAPPABLE_SIDS_IGNORE,
205                            PARM_UNMAPPABLE_SIDS_IGNORE_DEFAULT)) {
206                         DEBUG(3, ("Silently failing to set ACE for SID (%s) "
207                                 "because it is unresolvable\n",
208                                 sid_string_dbg(&samba_ace->trustee)));
209                         *mapped = false;
210                 } else {
211                         /* Fail for lack of a better option */
212                         return false;
213                 }
214         }
215
216         if (*mapped) {
217                 if (aclu_initialize_ace(ace, samba_ace->type,
218                         samba_ace->access_mask, samba_ace->flags, 0,
219                         &ident))
220                         return false;
221
222                 if ((ace->trustee.type == IFS_ID_TYPE_CREATOR_OWNER ||
223                         ace->trustee.type == IFS_ID_TYPE_CREATOR_GROUP) &&
224                     nt4_compatible_acls())
225                         ace->flags |= SEC_ACE_FLAG_INHERIT_ONLY;
226         }
227
228         return true;
229 }
230
231 /**
232  * Convert a SEC_ACL to a struct ifs_security_acl
233  */
234 static bool
235 onefs_samba_acl_to_acl(SEC_ACL *samba_acl, struct ifs_security_acl **acl,
236     bool * ignore_aces, int snum)
237 {
238         int num_aces = 0;
239         struct ifs_ace *aces = NULL;
240         SEC_ACE *samba_aces;
241         bool mapped;
242         int i, j;
243
244         SMB_ASSERT(ignore_aces);
245
246         if ((!acl) || (!samba_acl))
247                 return false;
248
249         samba_aces = samba_acl->aces;
250
251         if (samba_acl->num_aces > 0 && samba_aces) {
252                 /* Setup ACES */
253                 num_aces = samba_acl->num_aces;
254                 aces = SMB_MALLOC_ARRAY(struct ifs_ace, num_aces);
255
256                 for (i = 0, j = 0; j < num_aces; i++, j++) {
257                         if (!onefs_samba_ace_to_ace(&samba_aces[j],
258                                 &aces[i], &mapped, snum))
259                                 goto err_free;
260
261                         if (!mapped)
262                                 i--;
263                 }
264                 num_aces = i;
265         }
266
267         /* If aces are given but we cannot apply them due to the reasons
268          * above we do not change the SD.  However, if we are told to
269          * explicitly set an SD with 0 aces we honor this operation */
270         *ignore_aces = samba_acl->num_aces > 0 && num_aces < 1;
271
272         if (*ignore_aces == false)
273                 if (aclu_initialize_acl(acl, aces, num_aces))
274                         goto err_free;
275
276         if (aclu_initialize_acl(acl, aces, num_aces))
277                 goto err_free;
278
279         /* Currently aclu_initialize_acl should copy the aces over, allowing
280          * us to immediately free */
281         free(aces);
282         return true;
283
284 err_free:
285         free(aces);
286         return false;
287 }
288
289 /**
290  * Convert a struct ifs_security_acl to a SEC_ACL
291  */
292 static bool
293 onefs_acl_to_samba_acl(struct ifs_security_acl *acl, SEC_ACL **samba_acl)
294 {
295         SEC_ACE *samba_aces = NULL;
296         SEC_ACL *tmp_samba_acl = NULL;
297         int i, num_aces = 0;
298
299         if (!samba_acl)
300                 return false;
301
302         /* NULL ACL */
303         if (!acl) {
304                 *samba_acl = NULL;
305                 return true;
306         }
307
308         /* Determine number of aces in ACL */
309         if (!acl->aces)
310                 num_aces = 0;
311         else
312                 num_aces = acl->num_aces;
313
314         /* Allocate the ace list. */
315         if (num_aces > 0) {
316                 if ((samba_aces = SMB_MALLOC_ARRAY(SEC_ACE, num_aces)) == NULL)
317                 {
318                         DEBUG(0, ("Unable to malloc space for %d aces.\n",
319                             num_aces));
320                         return false;
321                 }
322                 memset(samba_aces, '\0', (num_aces) * sizeof(SEC_ACE));
323         }
324
325         for (i = 0; i < num_aces; i++) {
326                 DOM_SID sid;
327
328                 if (!onefs_identity_to_sid(&acl->aces[i].trustee, &sid))
329                         goto err_free;
330
331                 init_sec_ace(&samba_aces[i], &sid, acl->aces[i].type,
332                     acl->aces[i].access_mask, acl->aces[i].flags);
333         }
334
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;
339         }
340
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;
347 }
348
349 /**
350  * @brief Reorder ACLs into the "correct" order for Windows Explorer.
351  *
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.
358  *
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
363  *
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.
367  */
368 static bool
369 onefs_canon_acl(files_struct *fsp, struct ifs_security_descriptor *sd)
370 {
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;
376
377         if (sd == NULL || sd->dacl == NULL || sd->dacl->num_aces == 0)
378                 return true;
379
380         /*
381          * Find out if this is a windows bit, and if the smb policy wants us to
382          * lie about the sd.
383          */
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;
390
391         case ACL_FORMAT_WINDOWS_SD:
392                 error = SMB_VFS_FSTAT(fsp, &sbuf);
393                 if (error)
394                         return false;
395
396                 if ((sbuf.st_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;
401                 }
402                 break;
403
404         case ACL_FORMAT_ALWAYS:
405                 break;
406
407         default:
408                 SMB_ASSERT(false);
409                 return false;
410         }
411
412         new_aces = SMB_MALLOC_ARRAY(struct ifs_ace, sd->dacl->num_aces);
413         if (new_aces == NULL)
414                 return false;
415
416         /*
417          * By walking down the list 3 separate times, we can avoid the need
418          * to create multiple temp buffers and extra copies.
419          */
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];
423         }
424
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];
429         }
430
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];
435         }
436
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));
440
441         /*
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()
448          */
449         memcpy(sd->dacl->aces, new_aces,
450             sizeof(struct ifs_ace) * new_aces_count);
451
452         SAFE_FREE(new_aces);
453         return true;
454 }
455
456
457 /**
458  * This enum is a helper for onefs_fget_nt_acl() to communicate with
459  * onefs_init_ace().
460  */
461 enum mode_ident { USR, GRP, OTH };
462
463 /**
464  * Initializes an ACE for addition to a synthetic ACL.
465  */
466 static struct ifs_ace onefs_init_ace(struct connection_struct *conn,
467                                      mode_t mode,
468                                      bool isdir,
469                                      enum mode_ident ident)
470 {
471         struct ifs_ace result;
472         enum ifs_ace_rights r,w,x;
473
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;
477
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;
483
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;
510         }
511
512         return result;
513 }
514
515 /**
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.
519  *
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.
523  *
524  * See Isilon Bug 27990
525  *
526  * Note: This function allocates additional memory onto sd->dacl->aces, that
527  * must be freed by the caller.
528  */
529 static bool add_sfs_aces(files_struct *fsp, struct ifs_security_descriptor *sd)
530 {
531         int error;
532         SMB_STRUCT_STAT sbuf;
533
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;
540         }
541
542         /* Only continue if this is a synthetic ACL and a directory. */
543         if (S_ISDIR(sbuf.st_mode) && (sbuf.st_flags & SF_HASNTFSACL) == 0) {
544                 struct ifs_ace new_aces[6];
545                 struct ifs_ace *old_aces;
546                 int i, num_aces_to_add = 0;
547                 mode_t file_mode = 0, dir_mode = 0;
548
549                 /* Use existing samba logic to derive the mode bits. */
550                 file_mode = unix_mode(fsp->conn, 0, fsp->fsp_name, false);
551                 dir_mode = unix_mode(fsp->conn, aDIR, fsp->fsp_name, false);
552
553                 /* Initialize ACEs. */
554                 new_aces[0] = onefs_init_ace(fsp->conn, file_mode, false, USR);
555                 new_aces[1] = onefs_init_ace(fsp->conn, file_mode, false, GRP);
556                 new_aces[2] = onefs_init_ace(fsp->conn, file_mode, false, OTH);
557                 new_aces[3] = onefs_init_ace(fsp->conn, dir_mode, true, USR);
558                 new_aces[4] = onefs_init_ace(fsp->conn, dir_mode, true, GRP);
559                 new_aces[5] = onefs_init_ace(fsp->conn, dir_mode, true, OTH);
560
561                 for (i = 0; i < 6; i++)
562                         if (new_aces[i].access_mask != 0)
563                                 num_aces_to_add++;
564
565                 /* Expand the ACEs array */
566                 if (num_aces_to_add != 0) {
567                         old_aces = sd->dacl->aces;
568
569                         sd->dacl->aces = SMB_MALLOC_ARRAY(struct ifs_ace,
570                             sd->dacl->num_aces + num_aces_to_add);
571                         if (!sd->dacl->aces) {
572                                 DEBUG(0, ("Unable to malloc space for "
573                                     "new_aces: %d.\n",
574                                      sd->dacl->num_aces + num_aces_to_add));
575                                 return false;
576                         }
577                         memcpy(sd->dacl->aces, old_aces,
578                             sizeof(struct ifs_ace) * sd->dacl->num_aces);
579
580                         /* Add the new ACEs to the DACL. */
581                         for (i = 0; i < 6; i++) {
582                                 if (new_aces[i].access_mask != 0) {
583                                         sd->dacl->aces[sd->dacl->num_aces] =
584                                             new_aces[i];
585                                         sd->dacl->num_aces++;
586                                 }
587                         }
588                 }
589         }
590         return true;
591 }
592
593 /**
594  * Isilon-specific function for getting an NTFS ACL from an open file.
595  *
596  * @param[out] ppdesc SecDesc to allocate and fill in
597  *
598  * @return NTSTATUS based off errno on error
599  */
600 NTSTATUS
601 onefs_fget_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
602                   uint32 security_info, SEC_DESC **ppdesc)
603 {
604         int error;
605         uint32_t sd_size = 0;
606         size_t size = 0;
607         struct ifs_security_descriptor *sd = NULL;
608         DOM_SID owner_sid, group_sid;
609         DOM_SID *ownerp, *groupp;
610         SEC_ACL *dacl, *sacl;
611         SEC_DESC *pdesc;
612         bool alloced = false;
613         bool new_aces_alloced = false;
614         bool fopened = false;
615         NTSTATUS status = NT_STATUS_OK;
616
617         *ppdesc = NULL;
618
619         DEBUG(5, ("Getting sd for file %s. security_info=%u\n",
620             fsp->fsp_name, security_info));
621
622         if (lp_parm_bool(SNUM(fsp->conn), PARM_ONEFS_TYPE,
623                 PARM_IGNORE_SACLS, PARM_IGNORE_SACLS_DEFAULT)) {
624                 DEBUG(5, ("Ignoring SACL on %s.\n", fsp->fsp_name));
625                 security_info &= ~SACL_SECURITY_INFORMATION;
626         }
627
628         if (fsp->fh->fd == -1) {
629                 if ((fsp->fh->fd = onefs_sys_create_file(handle->conn,
630                                                          -1,
631                                                          fsp->fsp_name,
632                                                          0,
633                                                          0,
634                                                          0,
635                                                          0,
636                                                          0,
637                                                          0,
638                                                          INTERNAL_OPEN_ONLY,
639                                                          0,
640                                                          NULL,
641                                                          0,
642                                                          NULL)) == -1) {
643                         DEBUG(0, ("Error opening file %s. errno=%d (%s)\n",
644                                   fsp->fsp_name, errno, strerror(errno)));
645                         status = map_nt_error_from_unix(errno);
646                         goto out;
647                 }
648                 fopened = true;
649         }
650
651         /* Get security descriptor */
652         sd_size = 0;
653         do {
654                 /* Allocate memory for get_security_descriptor */
655                 if (sd_size > 0) {
656                         sd = SMB_REALLOC(sd, sd_size);
657                         if (!sd) {
658                                 DEBUG(0, ("Unable to malloc %u bytes of space "
659                                     "for security descriptor.\n", sd_size));
660                                 status = map_nt_error_from_unix(errno);
661                                 goto out;
662                         }
663
664                         alloced = true;
665                 }
666
667                 error = ifs_get_security_descriptor(fsp->fh->fd, security_info,
668                     sd_size, &sd_size, sd);
669                 if (error && (errno != EMSGSIZE)) {
670                         DEBUG(0, ("Failed getting size of security descriptor! "
671                             "errno=%d\n", errno));
672                         status = map_nt_error_from_unix(errno);
673                         goto out;
674                 }
675         } while (error);
676
677         DEBUG(5, ("Got sd, size=%u:\n", sd_size));
678
679         if (lp_parm_bool(SNUM(fsp->conn),
680             PARM_ONEFS_TYPE,
681             PARM_SIMPLE_FILE_SHARING_COMPATIBILITY_MODE,
682             PARM_SIMPLE_FILE_SHARING_COMPATIBILITY_MODE_DEFAULT) &&
683             sd->dacl) {
684                 if(!(new_aces_alloced = add_sfs_aces(fsp, sd)))
685                         goto out;
686         }
687
688         if (!(onefs_canon_acl(fsp, sd))) {
689                 status = map_nt_error_from_unix(errno);
690                 goto out;
691         }
692
693         DEBUG(5, ("Finished canonicalizing ACL\n"));
694
695         ownerp = NULL;
696         groupp = NULL;
697         dacl = NULL;
698         sacl = NULL;
699
700         /* Copy owner into ppdesc */
701         if (security_info & OWNER_SECURITY_INFORMATION) {
702                 if (!onefs_identity_to_sid(sd->owner, &owner_sid)) {
703                         status = NT_STATUS_INVALID_PARAMETER;
704                         goto out;
705                 }
706
707                 ownerp = &owner_sid;
708         }
709
710         /* Copy group into ppdesc */
711         if (security_info & GROUP_SECURITY_INFORMATION) {
712                 if (!onefs_identity_to_sid(sd->group, &group_sid)) {
713                         status = NT_STATUS_INVALID_PARAMETER;
714                         goto out;
715                 }
716
717                 groupp = &group_sid;
718         }
719
720         /* Copy DACL into ppdesc */
721         if (security_info & DACL_SECURITY_INFORMATION) {
722                 if (!onefs_acl_to_samba_acl(sd->dacl, &dacl)) {
723                         status = NT_STATUS_INVALID_PARAMETER;
724                         goto out;
725                 }
726         }
727
728         /* Copy SACL into ppdesc */
729         if (security_info & SACL_SECURITY_INFORMATION) {
730                 if (!onefs_acl_to_samba_acl(sd->sacl, &sacl)) {
731                         status = NT_STATUS_INVALID_PARAMETER;
732                         goto out;
733                 }
734         }
735
736         /* AUTO_INHERIT_REQ bits are not returned over the wire so strip them
737          * off.  Eventually we should stop storing these in the kernel
738          * all together. See Isilon bug 40364 */
739         sd->control &= ~(IFS_SD_CTRL_DACL_AUTO_INHERIT_REQ |
740                          IFS_SD_CTRL_SACL_AUTO_INHERIT_REQ);
741
742         pdesc = make_sec_desc(talloc_tos(), sd->revision, sd->control,
743             ownerp, groupp, sacl, dacl, &size);
744
745         if (!pdesc) {
746                 DEBUG(0, ("Problem with make_sec_desc. Memory?\n"));
747                 status = map_nt_error_from_unix(errno);
748                 goto out;
749         }
750
751         *ppdesc = pdesc;
752
753         DEBUG(5, ("Finished retrieving/canonicalizing SD!\n"));
754         /* FALLTHROUGH */
755 out:
756         if (alloced && sd) {
757                 if (new_aces_alloced && sd->dacl->aces)
758                         SAFE_FREE(sd->dacl->aces);
759
760                 SAFE_FREE(sd);
761         }
762
763         if (fopened) {
764                 close(fsp->fh->fd);
765                 fsp->fh->fd = -1;
766         }
767
768         return status;
769 }
770
771 /**
772  * Isilon-specific function for getting an NTFS ACL from a file path.
773  *
774  * Since onefs_fget_nt_acl() needs to open a filepath if the fd is invalid,
775  * we just mock up a files_struct with the path and bad fd and call into it.
776  *
777  * @param[out] ppdesc SecDesc to allocate and fill in
778  *
779  * @return NTSTATUS based off errno on error
780  */
781 NTSTATUS
782 onefs_get_nt_acl(vfs_handle_struct *handle, const char* name,
783                  uint32 security_info, SEC_DESC **ppdesc)
784 {
785         files_struct finfo;
786         struct fd_handle fh;
787
788         ZERO_STRUCT(finfo);
789         ZERO_STRUCT(fh);
790
791         finfo.fnum = -1;
792         finfo.conn = handle->conn;
793         finfo.fh = &fh;
794         finfo.fh->fd = -1;
795         finfo.fsp_name = CONST_DISCARD(char *, name);
796
797         return onefs_fget_nt_acl(handle, &finfo, security_info, ppdesc);
798 }
799
800 /**
801  * Isilon-specific function for setting up an ifs_security_descriptor, given a
802  * samba SEC_DESC.
803  *
804  * @param[out] sd ifs_security_descriptor to fill in
805  *
806  * @return NTSTATUS_OK if successful
807  */
808 NTSTATUS onefs_samba_sd_to_sd(uint32 security_info_sent, SEC_DESC *psd,
809                               struct ifs_security_descriptor *sd, int snum)
810 {
811         struct ifs_security_acl *daclp, *saclp;
812         struct ifs_identity owner, group, *ownerp, *groupp;
813         bool ignore_aces;
814
815         ownerp = NULL;
816         groupp = NULL;
817         daclp = NULL;
818         saclp = NULL;
819
820         /* Setup owner */
821         if (security_info_sent & OWNER_SECURITY_INFORMATION) {
822                 if (!onefs_og_to_identity(psd->owner_sid, &owner, false, snum))
823                         return NT_STATUS_UNSUCCESSFUL;
824
825                 SMB_ASSERT(owner.id.uid >= 0);
826
827                 ownerp = &owner;
828         }
829
830         /* Setup group */
831         if (security_info_sent & GROUP_SECURITY_INFORMATION) {
832                 if (!onefs_og_to_identity(psd->group_sid, &group, true, snum))
833                         return NT_STATUS_UNSUCCESSFUL;
834
835                 SMB_ASSERT(group.id.gid >= 0);
836
837                 groupp = &group;
838         }
839
840         /* Setup DACL */
841         if ((security_info_sent & DACL_SECURITY_INFORMATION) && (psd->dacl)) {
842                 if (!onefs_samba_acl_to_acl(psd->dacl, &daclp, &ignore_aces,
843                         snum))
844                         return NT_STATUS_UNSUCCESSFUL;
845
846                 if (ignore_aces == true)
847                         security_info_sent &= ~DACL_SECURITY_INFORMATION;
848         }
849
850         /* Setup SACL */
851         if (security_info_sent & SACL_SECURITY_INFORMATION) {
852
853                 if (lp_parm_bool(snum, PARM_ONEFS_TYPE,
854                             PARM_IGNORE_SACLS, PARM_IGNORE_SACLS_DEFAULT)) {
855                         DEBUG(5, ("Ignoring SACLs.\n"));
856                         security_info_sent &= ~SACL_SECURITY_INFORMATION;
857                 } else {
858                         if (psd->sacl) {
859                                 if (!onefs_samba_acl_to_acl(psd->sacl,
860                                         &saclp, &ignore_aces, snum))
861                                         return NT_STATUS_UNSUCCESSFUL;
862
863                                 if (ignore_aces == true) {
864                                         security_info_sent &=
865                                             ~SACL_SECURITY_INFORMATION;
866                                 }
867                         }
868                 }
869         }
870
871         /* Setup ifs_security_descriptor */
872         DEBUG(5,("Setting up SD\n"));
873         if (aclu_initialize_sd(sd, psd->type, ownerp, groupp,
874                 (daclp ? &daclp : NULL), (saclp ? &saclp : NULL), false))
875                 return NT_STATUS_UNSUCCESSFUL;
876
877         return NT_STATUS_OK;
878 }
879
880 /**
881  * Isilon-specific function for setting an NTFS ACL on an open file.
882  *
883  * @return NT_STATUS_UNSUCCESSFUL for userspace errors, NTSTATUS based off
884  * errno on syscall errors
885  */
886 NTSTATUS
887 onefs_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
888                   uint32 security_info_sent, SEC_DESC *psd)
889 {
890         struct ifs_security_descriptor sd = {};
891         int fd;
892         bool fopened = false;
893         NTSTATUS status;
894
895         DEBUG(5,("Setting SD on file %s.\n", fsp->fsp_name ));
896
897         status = onefs_samba_sd_to_sd(security_info_sent, psd, &sd,
898                                       SNUM(handle->conn));
899
900         if (!NT_STATUS_IS_OK(status)) {
901                 DEBUG(3, ("SD initialization failure: %s", nt_errstr(status)));
902                 return status;
903         }
904
905         fd = fsp->fh->fd;
906         if (fd == -1) {
907                 if ((fd = onefs_sys_create_file(handle->conn,
908                                                 -1,
909                                                 fsp->fsp_name,
910                                                 0,
911                                                 0,
912                                                 0,
913                                                 0,
914                                                 0,
915                                                 0,
916                                                 INTERNAL_OPEN_ONLY,
917                                                 0,
918                                                 NULL,
919                                                 0,
920                                                 NULL)) == -1) {
921                         DEBUG(0, ("Error opening file %s. errno=%d (%s)\n",
922                                   fsp->fsp_name, errno, strerror(errno)));
923                         status = map_nt_error_from_unix(errno);
924                         goto out;
925                 }
926                 fopened = true;
927         }
928
929         errno = 0;
930         if (ifs_set_security_descriptor(fd, security_info_sent, &sd)) {
931                 DEBUG(0, ("Error setting security descriptor = %d\n", errno));
932                 status = map_nt_error_from_unix(errno);
933                 goto out;
934         }
935
936         DEBUG(5, ("Security descriptor set correctly!\n"));
937         status = NT_STATUS_OK;
938
939         /* FALLTHROUGH */
940 out:
941         if (fopened)
942                 close(fd);
943
944         aclu_free_sd(&sd, false);
945         return status;
946 }