Remove tiny code duplication
[amitay/samba.git] / source3 / modules / nfs4_acls.c
1 /*
2  * NFS4 ACL handling
3  *
4  * Copyright (C) Jim McDonough, 2006
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "nfs4_acls.h"
22
23 #define SMBACL4_PARAM_TYPE_NAME "nfs4"
24
25 #define SMB_ACE4_INT_MAGIC 0x76F8A967
26 typedef struct _SMB_ACE4_INT_T
27 {
28         uint32  magic;
29         SMB_ACE4PROP_T  prop;
30         void    *next;
31 } SMB_ACE4_INT_T;
32
33 #define SMB_ACL4_INT_MAGIC 0x29A3E792
34 typedef struct _SMB_ACL4_INT_T
35 {
36         uint32  magic;
37         uint32  naces;
38         SMB_ACE4_INT_T  *first;
39         SMB_ACE4_INT_T  *last;
40 } SMB_ACL4_INT_T;
41
42 extern struct current_user current_user;
43 extern int try_chown(connection_struct *conn, const char *fname, uid_t uid, gid_t gid);
44 extern NTSTATUS unpack_nt_owners(int snum, uid_t *puser, gid_t *pgrp,
45         uint32 security_info_sent, SEC_DESC *psd);
46
47 static SMB_ACL4_INT_T *get_validated_aclint(SMB4ACL_T *acl)
48 {
49         SMB_ACL4_INT_T *aclint = (SMB_ACL4_INT_T *)acl;
50         if (acl==NULL)
51         {
52                 DEBUG(2, ("acl is NULL\n"));
53                 errno = EINVAL;
54                 return NULL;
55         }
56         if (aclint->magic!=SMB_ACL4_INT_MAGIC)
57         {
58                 DEBUG(2, ("aclint bad magic 0x%x\n", aclint->magic));
59                 errno = EINVAL;
60                 return NULL;
61         }
62         return aclint;
63 }
64
65 static SMB_ACE4_INT_T *get_validated_aceint(SMB4ACE_T *ace)
66 {
67         SMB_ACE4_INT_T *aceint = (SMB_ACE4_INT_T *)ace;
68         if (ace==NULL)
69         {
70                 DEBUG(2, ("ace is NULL\n"));
71                 errno = EINVAL;
72                 return NULL;
73         }
74         if (aceint->magic!=SMB_ACE4_INT_MAGIC)
75         {
76                 DEBUG(2, ("aceint bad magic 0x%x\n", aceint->magic));
77                 errno = EINVAL;
78                 return NULL;
79         }
80         return aceint;
81 }
82
83 SMB4ACL_T *smb_create_smb4acl(void)
84 {
85         TALLOC_CTX *mem_ctx = talloc_tos();
86         SMB_ACL4_INT_T  *acl = (SMB_ACL4_INT_T *)TALLOC_ZERO_SIZE(mem_ctx, sizeof(SMB_ACL4_INT_T));
87         if (acl==NULL)
88         {
89                 DEBUG(0, ("TALLOC_SIZE failed\n"));
90                 errno = ENOMEM;
91                 return NULL;
92         }
93         acl->magic = SMB_ACL4_INT_MAGIC;
94         /* acl->first, last = NULL not needed */
95         return (SMB4ACL_T *)acl;
96 }
97
98 SMB4ACE_T *smb_add_ace4(SMB4ACL_T *acl, SMB_ACE4PROP_T *prop)
99 {
100         SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
101         TALLOC_CTX *mem_ctx = talloc_tos();
102         SMB_ACE4_INT_T *ace;
103
104         ace = (SMB_ACE4_INT_T *)TALLOC_ZERO_SIZE(mem_ctx, sizeof(SMB_ACE4_INT_T));
105         if (ace==NULL)
106         {
107                 DEBUG(0, ("TALLOC_SIZE failed\n"));
108                 errno = ENOMEM;
109                 return NULL;
110         }
111         ace->magic = SMB_ACE4_INT_MAGIC;
112         /* ace->next = NULL not needed */
113         memcpy(&ace->prop, prop, sizeof(SMB_ACE4PROP_T));
114
115         if (aclint->first==NULL)
116         {
117                 aclint->first = ace;
118                 aclint->last = ace;
119         } else {
120                 aclint->last->next = (void *)ace;
121                 aclint->last = ace;
122         }
123         aclint->naces++;
124
125         return (SMB4ACE_T *)ace;
126 }
127
128 SMB_ACE4PROP_T *smb_get_ace4(SMB4ACE_T *ace)
129 {
130         SMB_ACE4_INT_T *aceint = get_validated_aceint(ace);
131         if (aceint==NULL)
132                 return NULL;
133
134         return &aceint->prop;
135 }
136
137 SMB4ACE_T *smb_next_ace4(SMB4ACE_T *ace)
138 {
139         SMB_ACE4_INT_T *aceint = get_validated_aceint(ace);
140         if (aceint==NULL)
141                 return NULL;
142
143         return (SMB4ACE_T *)aceint->next;
144 }
145
146 SMB4ACE_T *smb_first_ace4(SMB4ACL_T *acl)
147 {
148         SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
149         if (aclint==NULL)
150                 return NULL;
151
152         return (SMB4ACE_T *)aclint->first;
153 }
154
155 uint32 smb_get_naces(SMB4ACL_T *acl)
156 {
157         SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
158         if (aclint==NULL)
159                 return 0;
160
161         return aclint->naces;
162 }
163
164 static int smbacl4_GetFileOwner(struct connection_struct *conn,
165                                 const char *filename,
166                                 SMB_STRUCT_STAT *psbuf)
167 {
168         memset(psbuf, 0, sizeof(SMB_STRUCT_STAT));
169
170         /* Get the stat struct for the owner info. */
171         if (SMB_VFS_STAT(conn, filename, psbuf) != 0)
172         {
173                 DEBUG(8, ("SMB_VFS_STAT failed with error %s\n",
174                         strerror(errno)));
175                 return -1;
176         }
177
178         return 0;
179 }
180
181 static int smbacl4_fGetFileOwner(files_struct *fsp, SMB_STRUCT_STAT *psbuf)
182 {
183         memset(psbuf, 0, sizeof(SMB_STRUCT_STAT));
184
185         if (fsp->is_directory || fsp->fh->fd == -1) {
186                 return smbacl4_GetFileOwner(fsp->conn, fsp->fsp_name, psbuf);
187         }
188         if (SMB_VFS_FSTAT(fsp,fsp->fh->fd, psbuf) != 0)
189         {
190                 DEBUG(8, ("SMB_VFS_FSTAT failed with error %s\n",
191                         strerror(errno)));
192                 return -1;
193         }
194
195         return 0;
196 }
197
198 static bool smbacl4_nfs42win(TALLOC_CTX *mem_ctx, SMB4ACL_T *acl, /* in */
199         DOM_SID *psid_owner, /* in */
200         DOM_SID *psid_group, /* in */
201         SEC_ACE **ppnt_ace_list, /* out */
202         int *pgood_aces /* out */
203 )
204 {
205         SMB_ACL4_INT_T *aclint = (SMB_ACL4_INT_T *)acl;
206         SMB_ACE4_INT_T *aceint;
207         SEC_ACE *nt_ace_list = NULL;
208         int good_aces = 0;
209
210         DEBUG(10, ("smbacl_nfs42win entered"));
211
212         aclint = get_validated_aclint(acl);
213         /* We do not check for naces being 0 or acl being NULL here because it is done upstream */
214         /* in smb_get_nt_acl_nfs4(). */
215         nt_ace_list = (SEC_ACE *)TALLOC_ZERO_SIZE(mem_ctx, aclint->naces * sizeof(SEC_ACE));
216         if (nt_ace_list==NULL)
217         {
218                 DEBUG(10, ("talloc error"));
219                 errno = ENOMEM;
220                 return False;
221         }
222
223         for (aceint=aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
224                 SEC_ACCESS mask;
225                 DOM_SID sid;
226                 SMB_ACE4PROP_T  *ace = &aceint->prop;
227
228                 DEBUG(10, ("magic: 0x%x, type: %d, iflags: %x, flags: %x, mask: %x, "
229                         "who: %d\n", aceint->magic, ace->aceType, ace->flags,
230                         ace->aceFlags, ace->aceMask, ace->who.id));
231
232                 SMB_ASSERT(aceint->magic==SMB_ACE4_INT_MAGIC);
233
234                 if (ace->flags & SMB_ACE4_ID_SPECIAL) {
235                         switch (ace->who.special_id) {
236                         case SMB_ACE4_WHO_OWNER:
237                                 sid_copy(&sid, psid_owner);
238                                 break;
239                         case SMB_ACE4_WHO_GROUP:
240                                 sid_copy(&sid, psid_group);
241                                 break;
242                         case SMB_ACE4_WHO_EVERYONE:
243                                 sid_copy(&sid, &global_sid_World);
244                                 break;
245                         default:
246                                 DEBUG(8, ("invalid special who id %d "
247                                         "ignored\n", ace->who.special_id));
248                         }
249                 } else {
250                         if (ace->aceFlags & SMB_ACE4_IDENTIFIER_GROUP) {
251                                 gid_to_sid(&sid, ace->who.gid);
252                         } else {
253                                 uid_to_sid(&sid, ace->who.uid);
254                         }
255                 }
256                 DEBUG(10, ("mapped %d to %s\n", ace->who.id,
257                            sid_string_dbg(&sid)));
258
259                 init_sec_access(&mask, ace->aceMask);
260                 init_sec_ace(&nt_ace_list[good_aces++], &sid,
261                         ace->aceType, mask,
262                         ace->aceFlags & 0xf);
263         }
264
265         *ppnt_ace_list = nt_ace_list;
266         *pgood_aces = good_aces;
267
268         return True;
269 }
270
271 static NTSTATUS smb_get_nt_acl_nfs4_common(const SMB_STRUCT_STAT *sbuf,
272         uint32 security_info,
273         SEC_DESC **ppdesc, SMB4ACL_T *acl)
274 {
275         int     good_aces = 0;
276         DOM_SID sid_owner, sid_group;
277         size_t sd_size = 0;
278         SEC_ACE *nt_ace_list = NULL;
279         SEC_ACL *psa = NULL;
280         TALLOC_CTX *mem_ctx = talloc_tos();
281
282         if (acl==NULL || smb_get_naces(acl)==0)
283                 return NT_STATUS_ACCESS_DENIED; /* special because we
284                                                  * shouldn't alloc 0 for
285                                                  * win */
286
287         uid_to_sid(&sid_owner, sbuf->st_uid);
288         gid_to_sid(&sid_group, sbuf->st_gid);
289
290         if (smbacl4_nfs42win(mem_ctx, acl, &sid_owner, &sid_group, &nt_ace_list, &good_aces)==False) {
291                 DEBUG(8,("smbacl4_nfs42win failed\n"));
292                 return map_nt_error_from_unix(errno);
293         }
294
295         psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION, good_aces, nt_ace_list);
296         if (psa == NULL) {
297                 DEBUG(2,("make_sec_acl failed\n"));
298                 return NT_STATUS_NO_MEMORY;
299         }
300
301         DEBUG(10,("after make sec_acl\n"));
302         *ppdesc = make_sec_desc(mem_ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE,
303                                 (security_info & OWNER_SECURITY_INFORMATION) ? &sid_owner : NULL,
304                                 (security_info & GROUP_SECURITY_INFORMATION) ? &sid_group : NULL,
305                                 NULL, psa, &sd_size);
306         if (*ppdesc==NULL) {
307                 DEBUG(2,("make_sec_desc failed\n"));
308                 return NT_STATUS_NO_MEMORY;
309         }
310
311         DEBUG(10, ("smb_get_nt_acl_nfs4_common successfully exited with sd_size %d\n",
312                    ndr_size_security_descriptor(*ppdesc, 0)));
313
314         return NT_STATUS_OK;
315 }
316
317 NTSTATUS smb_fget_nt_acl_nfs4(files_struct *fsp,
318                                uint32 security_info,
319                                SEC_DESC **ppdesc, SMB4ACL_T *acl)
320 {
321         SMB_STRUCT_STAT sbuf;
322
323         DEBUG(10, ("smb_fget_nt_acl_nfs4 invoked for %s\n", fsp->fsp_name));
324
325         if (smbacl4_fGetFileOwner(fsp, &sbuf)) {
326                 return map_nt_error_from_unix(errno);
327         }
328
329         return smb_get_nt_acl_nfs4_common(&sbuf, security_info, ppdesc, acl);
330 }
331
332 NTSTATUS smb_get_nt_acl_nfs4(struct connection_struct *conn,
333                               const char *name,
334                               uint32 security_info,
335                               SEC_DESC **ppdesc, SMB4ACL_T *acl)
336 {
337         SMB_STRUCT_STAT sbuf;
338
339         DEBUG(10, ("smb_get_nt_acl_nfs4 invoked for %s\n", name));
340
341         if (smbacl4_GetFileOwner(conn, name, &sbuf)) {
342                 return map_nt_error_from_unix(errno);
343         }
344
345         return smb_get_nt_acl_nfs4_common(&sbuf, security_info, ppdesc, acl);
346 }
347
348 enum smbacl4_mode_enum {e_simple=0, e_special=1};
349 enum smbacl4_acedup_enum {e_dontcare=0, e_reject=1, e_ignore=2, e_merge=3};
350
351 typedef struct _smbacl4_vfs_params {
352         enum smbacl4_mode_enum mode;
353         bool do_chown;
354         enum smbacl4_acedup_enum acedup;
355 } smbacl4_vfs_params;
356
357 /*
358  * Gather special parameters for NFS4 ACL handling
359  */
360 static int smbacl4_get_vfs_params(
361         const char *type_name,
362         files_struct *fsp,
363         smbacl4_vfs_params *params
364 )
365 {
366         static const struct enum_list enum_smbacl4_modes[] = {
367                 { e_simple, "simple" },
368                 { e_special, "special" }
369         };
370         static const struct enum_list enum_smbacl4_acedups[] = {
371                 { e_dontcare, "dontcare" },
372                 { e_reject, "reject" },
373                 { e_ignore, "ignore" },
374                 { e_merge, "merge" },
375         };
376
377         memset(params, 0, sizeof(smbacl4_vfs_params));
378         params->mode = (enum smbacl4_mode_enum)lp_parm_enum(
379                 SNUM(fsp->conn), type_name,
380                 "mode", enum_smbacl4_modes, e_simple);
381         params->do_chown = lp_parm_bool(SNUM(fsp->conn), type_name,
382                 "chown", True);
383         params->acedup = (enum smbacl4_acedup_enum)lp_parm_enum(
384                 SNUM(fsp->conn), type_name,
385                 "acedup", enum_smbacl4_acedups, e_dontcare);
386
387         DEBUG(10, ("mode:%s, do_chown:%s, acedup: %s\n",
388                 enum_smbacl4_modes[params->mode].name,
389                 params->do_chown ? "true" : "false",
390                 enum_smbacl4_acedups[params->acedup].name));
391
392         return 0;
393 }
394
395 static void smbacl4_dump_nfs4acl(int level, SMB4ACL_T *acl)
396 {
397         SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
398         SMB_ACE4_INT_T *aceint;
399
400         DEBUG(level, ("NFS4ACL: size=%d\n", aclint->naces));
401
402         for(aceint = aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
403                 SMB_ACE4PROP_T *ace = &aceint->prop;
404
405                 DEBUG(level, ("\tACE: type=%d, flags=0x%x, fflags=0x%x, mask=0x%x, id=%d\n",
406                         ace->aceType,
407                         ace->aceFlags, ace->flags,
408                         ace->aceMask,
409                         ace->who.id));
410         }
411 }
412
413 /* 
414  * Find 2 NFS4 who-special ACE property (non-copy!!!)
415  * match nonzero if "special" and who is equal
416  * return ace if found matching; otherwise NULL
417  */
418 static SMB_ACE4PROP_T *smbacl4_find_equal_special(
419         SMB4ACL_T *acl,
420         SMB_ACE4PROP_T *aceNew)
421 {
422         SMB_ACL4_INT_T *aclint = get_validated_aclint(acl);
423         SMB_ACE4_INT_T *aceint;
424
425         for(aceint = aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
426                 SMB_ACE4PROP_T *ace = &aceint->prop;
427
428                 if (ace->flags == aceNew->flags &&
429                         ace->aceType==aceNew->aceType &&
430                         (ace->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)==
431                         (aceNew->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)
432                 ) {
433                         /* keep type safety; e.g. gid is an u.short */
434                         if (ace->flags & SMB_ACE4_ID_SPECIAL)
435                         {
436                                 if (ace->who.special_id==aceNew->who.special_id)
437                                         return ace;
438                         } else {
439                                 if (ace->aceFlags & SMB_ACE4_IDENTIFIER_GROUP)
440                                 {
441                                         if (ace->who.gid==aceNew->who.gid)
442                                                 return ace;
443                                 } else {
444                                         if (ace->who.uid==aceNew->who.uid)
445                                                 return ace;
446                                 }
447                         }
448                 }
449         }
450
451         return NULL;
452 }
453
454 static int smbacl4_fill_ace4(
455         TALLOC_CTX *mem_ctx,
456         smbacl4_vfs_params *params,
457         uid_t ownerUID,
458         gid_t ownerGID,
459         SEC_ACE *ace_nt, /* input */
460         SMB_ACE4PROP_T *ace_v4 /* output */
461 )
462 {
463         const char *dom, *name;
464         enum lsa_SidType type;
465         uid_t uid;
466         gid_t gid;
467
468         DEBUG(10, ("got ace for %s\n", sid_string_dbg(&ace_nt->trustee)));
469
470         memset(ace_v4, 0, sizeof(SMB_ACE4PROP_T));
471         ace_v4->aceType = ace_nt->type; /* only ACCESS|DENY supported right now */
472         ace_v4->aceFlags = ace_nt->flags & SEC_ACE_FLAG_VALID_INHERIT;
473         ace_v4->aceMask = ace_nt->access_mask &
474                 (STD_RIGHT_ALL_ACCESS | SA_RIGHT_FILE_ALL_ACCESS);
475
476         if (ace_v4->aceFlags!=ace_nt->flags)
477                 DEBUG(9, ("ace_v4->aceFlags(0x%x)!=ace_nt->flags(0x%x)\n",
478                         ace_v4->aceFlags, ace_nt->flags));
479
480         if (ace_v4->aceMask!=ace_nt->access_mask)
481                 DEBUG(9, ("ace_v4->aceMask(0x%x)!=ace_nt->access_mask(0x%x)\n",
482                         ace_v4->aceMask, ace_nt->access_mask));
483
484         if (sid_equal(&ace_nt->trustee, &global_sid_World)) {
485                 ace_v4->who.special_id = SMB_ACE4_WHO_EVERYONE;
486                 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
487         } else {
488                 if (!lookup_sid(mem_ctx, &ace_nt->trustee, &dom, &name, &type)) {
489                         DEBUG(8, ("Could not find %s' type\n",
490                                   sid_string_dbg(&ace_nt->trustee)));
491                         errno = EINVAL;
492                         return -1;
493                 }
494
495                 if (type == SID_NAME_USER) {
496                         if (!sid_to_uid(&ace_nt->trustee, &uid)) {
497                                 DEBUG(2, ("Could not convert %s to uid\n",
498                                           sid_string_dbg(&ace_nt->trustee)));
499                                 return -1;
500                         }
501
502                         if (params->mode==e_special && uid==ownerUID) {
503                                 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
504                                 ace_v4->who.special_id = SMB_ACE4_WHO_OWNER;
505                         } else {
506                                 ace_v4->who.uid = uid;
507                         }
508                 } else { /* else group? - TODO check it... */
509                         if (!sid_to_gid(&ace_nt->trustee, &gid)) {
510                                 DEBUG(2, ("Could not convert %s to gid\n",
511                                           sid_string_dbg(&ace_nt->trustee)));
512                                 return -1;
513                         }
514                         ace_v4->aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
515
516                         if (params->mode==e_special && gid==ownerGID) {
517                                 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
518                                 ace_v4->who.special_id = SMB_ACE4_WHO_GROUP;
519                         } else {
520                                 ace_v4->who.gid = gid;
521                         }
522                 }
523         }
524
525         return 0; /* OK */
526 }
527
528 static int smbacl4_MergeIgnoreReject(
529         enum smbacl4_acedup_enum acedup,
530         SMB4ACL_T *acl, /* may modify it */
531         SMB_ACE4PROP_T *ace, /* the "new" ACE */
532         bool    *paddNewACE,
533         int     i
534 )
535 {
536         int     result = 0;
537         SMB_ACE4PROP_T *ace4found = smbacl4_find_equal_special(acl, ace);
538         if (ace4found)
539         {
540                 switch(acedup)
541                 {
542                 case e_merge: /* "merge" flags */
543                         *paddNewACE = False;
544                         ace4found->aceFlags |= ace->aceFlags;
545                         ace4found->aceMask |= ace->aceMask;
546                         break;
547                 case e_ignore: /* leave out this record */
548                         *paddNewACE = False;
549                         break;
550                 case e_reject: /* do an error */
551                         DEBUG(8, ("ACL rejected by duplicate nt ace#%d\n", i));
552                         errno = EINVAL; /* SHOULD be set on any _real_ error */
553                         result = -1;
554                         break;
555                 default:
556                         break;
557                 }
558         }
559         return result;
560 }
561
562 static SMB4ACL_T *smbacl4_win2nfs4(
563         SEC_ACL *dacl,
564         smbacl4_vfs_params *pparams,
565         uid_t ownerUID,
566         gid_t ownerGID
567 )
568 {
569         SMB4ACL_T *acl;
570         uint32  i;
571         TALLOC_CTX *mem_ctx = talloc_tos();
572
573         DEBUG(10, ("smbacl4_win2nfs4 invoked\n"));
574
575         acl = smb_create_smb4acl();
576         if (acl==NULL)
577                 return NULL;
578
579         for(i=0; i<dacl->num_aces; i++) {
580                 SMB_ACE4PROP_T  ace_v4;
581                 bool    addNewACE = True;
582
583                 if (smbacl4_fill_ace4(mem_ctx, pparams, ownerUID, ownerGID,
584                         dacl->aces + i, &ace_v4))
585                         return NULL;
586
587                 if (pparams->acedup!=e_dontcare) {
588                         if (smbacl4_MergeIgnoreReject(pparams->acedup, acl,
589                                 &ace_v4, &addNewACE, i))
590                                 return NULL;
591                 }
592
593                 if (addNewACE)
594                         smb_add_ace4(acl, &ace_v4);
595         }
596
597         return acl;
598 }
599
600 NTSTATUS smb_set_nt_acl_nfs4(files_struct *fsp,
601         uint32 security_info_sent,
602         SEC_DESC *psd,
603         set_nfs4acl_native_fn_t set_nfs4_native)
604 {
605         smbacl4_vfs_params params;
606         SMB4ACL_T *acl = NULL;
607         bool    result;
608
609         SMB_STRUCT_STAT sbuf;
610         uid_t newUID = (uid_t)-1;
611         gid_t newGID = (gid_t)-1;
612
613         DEBUG(10, ("smb_set_nt_acl_nfs4 invoked for %s\n", fsp->fsp_name));
614
615         if ((security_info_sent & (DACL_SECURITY_INFORMATION |
616                 GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION)) == 0)
617         {
618                 DEBUG(9, ("security_info_sent (0x%x) ignored\n",
619                         security_info_sent));
620                 return NT_STATUS_OK; /* won't show error - later to be refined... */
621         }
622
623         /* Special behaviours */
624         if (smbacl4_get_vfs_params(SMBACL4_PARAM_TYPE_NAME, fsp, &params))
625                 return NT_STATUS_NO_MEMORY;
626
627         if (smbacl4_fGetFileOwner(fsp, &sbuf))
628                 return map_nt_error_from_unix(errno);
629
630         if (params.do_chown) {
631                 /* chown logic is a copy/paste from posix_acl.c:set_nt_acl */
632                 NTSTATUS status = unpack_nt_owners(SNUM(fsp->conn), &newUID, &newGID, security_info_sent, psd);
633                 if (!NT_STATUS_IS_OK(status)) {
634                         DEBUG(8, ("unpack_nt_owners failed"));
635                         return status;
636                 }
637                 if (((newUID != (uid_t)-1) && (sbuf.st_uid != newUID)) ||
638                                 ((newGID != (gid_t)-1) && (sbuf.st_gid != newGID))) {
639                         if(try_chown(fsp->conn, fsp->fsp_name, newUID, newGID)) {
640                                 DEBUG(3,("chown %s, %u, %u failed. Error = %s.\n",
641                                         fsp->fsp_name, (unsigned int)newUID, (unsigned int)newGID, strerror(errno) ));
642                                 if (errno == EPERM) {
643                                         return NT_STATUS_INVALID_OWNER;
644                                 }
645                                 return map_nt_error_from_unix(errno);
646                         }
647                         DEBUG(10,("chown %s, %u, %u succeeded.\n",
648                                 fsp->fsp_name, (unsigned int)newUID, (unsigned int)newGID));
649                         if (smbacl4_fGetFileOwner(fsp, &sbuf))
650                                 return map_nt_error_from_unix(errno);
651                 }
652         }
653
654         if ((security_info_sent & DACL_SECURITY_INFORMATION)!=0 && psd->dacl!=NULL)
655         {
656                 acl = smbacl4_win2nfs4(psd->dacl, &params, sbuf.st_uid, sbuf.st_gid);
657                 if (!acl)
658                         return map_nt_error_from_unix(errno);
659
660                 smbacl4_dump_nfs4acl(10, acl);
661
662                 result = set_nfs4_native(fsp, acl);
663                 if (result!=True)
664                 {
665                         DEBUG(10, ("set_nfs4_native failed with %s\n", strerror(errno)));
666                         return map_nt_error_from_unix(errno);
667                 }
668         } else
669                 DEBUG(10, ("no dacl found; security_info_sent = 0x%x\n", security_info_sent));
670
671         DEBUG(10, ("smb_set_nt_acl_nfs4 succeeded\n"));
672         return NT_STATUS_OK;
673 }