s3-vfs: include smbd/smbd.h in vfs modules.
[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 "smbd/smbd.h"
22 #include "nfs4_acls.h"
23 #include "librpc/gen_ndr/ndr_security.h"
24 #include "../libcli/security/dom_sid.h"
25 #include "../libcli/security/security.h"
26 #include "include/dbwrap.h"
27 #include "system/filesys.h"
28 #include "passdb/lookup_sid.h"
29
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_ACLS
32
33 #define SMBACL4_PARAM_TYPE_NAME "nfs4"
34
35 extern const struct generic_mapping file_generic_mapping;
36
37 #define SMB_ACE4_INT_MAGIC 0x76F8A967
38 typedef struct _SMB_ACE4_INT_T
39 {
40         uint32  magic;
41         SMB_ACE4PROP_T  prop;
42         void    *next;
43 } SMB_ACE4_INT_T;
44
45 #define SMB_ACL4_INT_MAGIC 0x29A3E792
46 typedef struct _SMB_ACL4_INT_T
47 {
48         uint32  magic;
49         uint32  naces;
50         SMB_ACE4_INT_T  *first;
51         SMB_ACE4_INT_T  *last;
52 } SMB_ACL4_INT_T;
53
54 static SMB_ACL4_INT_T *get_validated_aclint(SMB4ACL_T *theacl)
55 {
56         SMB_ACL4_INT_T *aclint = (SMB_ACL4_INT_T *)theacl;
57         if (theacl==NULL)
58         {
59                 DEBUG(2, ("acl is NULL\n"));
60                 errno = EINVAL;
61                 return NULL;
62         }
63         if (aclint->magic!=SMB_ACL4_INT_MAGIC)
64         {
65                 DEBUG(2, ("aclint bad magic 0x%x\n", aclint->magic));
66                 errno = EINVAL;
67                 return NULL;
68         }
69         return aclint;
70 }
71
72 static SMB_ACE4_INT_T *get_validated_aceint(SMB4ACE_T *ace)
73 {
74         SMB_ACE4_INT_T *aceint = (SMB_ACE4_INT_T *)ace;
75         if (ace==NULL)
76         {
77                 DEBUG(2, ("ace is NULL\n"));
78                 errno = EINVAL;
79                 return NULL;
80         }
81         if (aceint->magic!=SMB_ACE4_INT_MAGIC)
82         {
83                 DEBUG(2, ("aceint bad magic 0x%x\n", aceint->magic));
84                 errno = EINVAL;
85                 return NULL;
86         }
87         return aceint;
88 }
89
90 SMB4ACL_T *smb_create_smb4acl(void)
91 {
92         TALLOC_CTX *mem_ctx = talloc_tos();
93         SMB_ACL4_INT_T  *theacl = (SMB_ACL4_INT_T *)TALLOC_ZERO_SIZE(mem_ctx, sizeof(SMB_ACL4_INT_T));
94         if (theacl==NULL)
95         {
96                 DEBUG(0, ("TALLOC_SIZE failed\n"));
97                 errno = ENOMEM;
98                 return NULL;
99         }
100         theacl->magic = SMB_ACL4_INT_MAGIC;
101         /* theacl->first, last = NULL not needed */
102         return (SMB4ACL_T *)theacl;
103 }
104
105 SMB4ACE_T *smb_add_ace4(SMB4ACL_T *theacl, SMB_ACE4PROP_T *prop)
106 {
107         SMB_ACL4_INT_T *aclint = get_validated_aclint(theacl);
108         TALLOC_CTX *mem_ctx = talloc_tos();
109         SMB_ACE4_INT_T *ace;
110
111         ace = (SMB_ACE4_INT_T *)TALLOC_ZERO_SIZE(mem_ctx, sizeof(SMB_ACE4_INT_T));
112         if (ace==NULL)
113         {
114                 DEBUG(0, ("TALLOC_SIZE failed\n"));
115                 errno = ENOMEM;
116                 return NULL;
117         }
118         ace->magic = SMB_ACE4_INT_MAGIC;
119         /* ace->next = NULL not needed */
120         memcpy(&ace->prop, prop, sizeof(SMB_ACE4PROP_T));
121
122         if (aclint->first==NULL)
123         {
124                 aclint->first = ace;
125                 aclint->last = ace;
126         } else {
127                 aclint->last->next = (void *)ace;
128                 aclint->last = ace;
129         }
130         aclint->naces++;
131
132         return (SMB4ACE_T *)ace;
133 }
134
135 SMB_ACE4PROP_T *smb_get_ace4(SMB4ACE_T *ace)
136 {
137         SMB_ACE4_INT_T *aceint = get_validated_aceint(ace);
138         if (aceint==NULL)
139                 return NULL;
140
141         return &aceint->prop;
142 }
143
144 SMB4ACE_T *smb_next_ace4(SMB4ACE_T *ace)
145 {
146         SMB_ACE4_INT_T *aceint = get_validated_aceint(ace);
147         if (aceint==NULL)
148                 return NULL;
149
150         return (SMB4ACE_T *)aceint->next;
151 }
152
153 SMB4ACE_T *smb_first_ace4(SMB4ACL_T *theacl)
154 {
155         SMB_ACL4_INT_T *aclint = get_validated_aclint(theacl);
156         if (aclint==NULL)
157                 return NULL;
158
159         return (SMB4ACE_T *)aclint->first;
160 }
161
162 uint32 smb_get_naces(SMB4ACL_T *theacl)
163 {
164         SMB_ACL4_INT_T *aclint = get_validated_aclint(theacl);
165         if (aclint==NULL)
166                 return 0;
167
168         return aclint->naces;
169 }
170
171 static int smbacl4_GetFileOwner(struct connection_struct *conn,
172                                 const char *filename,
173                                 SMB_STRUCT_STAT *psbuf)
174 {
175         memset(psbuf, 0, sizeof(SMB_STRUCT_STAT));
176
177         /* Get the stat struct for the owner info. */
178         if (vfs_stat_smb_fname(conn, filename, psbuf) != 0)
179         {
180                 DEBUG(8, ("vfs_stat_smb_fname failed with error %s\n",
181                         strerror(errno)));
182                 return -1;
183         }
184
185         return 0;
186 }
187
188 static int smbacl4_fGetFileOwner(files_struct *fsp, SMB_STRUCT_STAT *psbuf)
189 {
190         memset(psbuf, 0, sizeof(SMB_STRUCT_STAT));
191
192         if (fsp->fh->fd == -1) {
193                 return smbacl4_GetFileOwner(fsp->conn,
194                                             fsp->fsp_name->base_name, psbuf);
195         }
196         if (SMB_VFS_FSTAT(fsp, psbuf) != 0)
197         {
198                 DEBUG(8, ("SMB_VFS_FSTAT failed with error %s\n",
199                         strerror(errno)));
200                 return -1;
201         }
202
203         return 0;
204 }
205
206 static bool smbacl4_nfs42win(TALLOC_CTX *mem_ctx, SMB4ACL_T *theacl, /* in */
207         struct dom_sid *psid_owner, /* in */
208         struct dom_sid *psid_group, /* in */
209         bool is_directory, /* in */
210         struct security_ace **ppnt_ace_list, /* out */
211         int *pgood_aces /* out */
212 )
213 {
214         SMB_ACL4_INT_T *aclint = (SMB_ACL4_INT_T *)theacl;
215         SMB_ACE4_INT_T *aceint;
216         struct security_ace *nt_ace_list = NULL;
217         int good_aces = 0;
218
219         DEBUG(10, ("smbacl_nfs42win entered\n"));
220
221         aclint = get_validated_aclint(theacl);
222         /* We do not check for naces being 0 or theacl being NULL here because it is done upstream */
223         /* in smb_get_nt_acl_nfs4(). */
224         nt_ace_list = (struct security_ace *)TALLOC_ZERO_SIZE(mem_ctx, aclint->naces * sizeof(struct security_ace));
225         if (nt_ace_list==NULL)
226         {
227                 DEBUG(10, ("talloc error"));
228                 errno = ENOMEM;
229                 return False;
230         }
231
232         for (aceint=aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
233                 uint32_t mask;
234                 struct dom_sid sid;
235                 SMB_ACE4PROP_T  *ace = &aceint->prop;
236                 uint32_t mapped_ace_flags;
237
238                 DEBUG(10, ("magic: 0x%x, type: %d, iflags: %x, flags: %x, mask: %x, "
239                         "who: %d\n", aceint->magic, ace->aceType, ace->flags,
240                         ace->aceFlags, ace->aceMask, ace->who.id));
241
242                 SMB_ASSERT(aceint->magic==SMB_ACE4_INT_MAGIC);
243
244                 if (ace->flags & SMB_ACE4_ID_SPECIAL) {
245                         switch (ace->who.special_id) {
246                         case SMB_ACE4_WHO_OWNER:
247                                 sid_copy(&sid, psid_owner);
248                                 break;
249                         case SMB_ACE4_WHO_GROUP:
250                                 sid_copy(&sid, psid_group);
251                                 break;
252                         case SMB_ACE4_WHO_EVERYONE:
253                                 sid_copy(&sid, &global_sid_World);
254                                 break;
255                         default:
256                                 DEBUG(8, ("invalid special who id %d "
257                                         "ignored\n", ace->who.special_id));
258                         }
259                 } else {
260                         if (ace->aceFlags & SMB_ACE4_IDENTIFIER_GROUP) {
261                                 gid_to_sid(&sid, ace->who.gid);
262                         } else {
263                                 uid_to_sid(&sid, ace->who.uid);
264                         }
265                 }
266                 DEBUG(10, ("mapped %d to %s\n", ace->who.id,
267                            sid_string_dbg(&sid)));
268
269                 if (is_directory && (ace->aceMask & SMB_ACE4_ADD_FILE)) {
270                         ace->aceMask |= SMB_ACE4_DELETE_CHILD;
271                 }
272
273                 mapped_ace_flags = ace->aceFlags & 0xf;
274                 if (!is_directory && (mapped_ace_flags & (SMB_ACE4_FILE_INHERIT_ACE|SMB_ACE4_DIRECTORY_INHERIT_ACE))) {
275                         /*
276                          * GPFS sets inherits dir_inhert and file_inherit flags
277                          * to files, too, which confuses windows, and seems to
278                          * be wrong anyways. ==> Map these bits away for files.
279                          */
280                         DEBUG(10, ("removing inherit flags from nfs4 ace\n"));
281                         mapped_ace_flags &= ~(SMB_ACE4_FILE_INHERIT_ACE|SMB_ACE4_DIRECTORY_INHERIT_ACE);
282                 }
283                 DEBUG(10, ("mapped ace flags: 0x%x => 0x%x\n",
284                       ace->aceFlags, mapped_ace_flags));
285
286                 /* Windows clients expect SYNC on acls to
287                    correctly allow rename. See bug #7909. */
288                 mask = ace->aceMask | SMB_ACE4_SYNCHRONIZE;
289                 init_sec_ace(&nt_ace_list[good_aces++], &sid,
290                         ace->aceType, mask,
291                         mapped_ace_flags);
292         }
293
294         *ppnt_ace_list = nt_ace_list;
295         *pgood_aces = good_aces;
296
297         return True;
298 }
299
300 static NTSTATUS smb_get_nt_acl_nfs4_common(const SMB_STRUCT_STAT *sbuf,
301         uint32 security_info,
302         struct security_descriptor **ppdesc, SMB4ACL_T *theacl)
303 {
304         int     good_aces = 0;
305         struct dom_sid sid_owner, sid_group;
306         size_t sd_size = 0;
307         struct security_ace *nt_ace_list = NULL;
308         struct security_acl *psa = NULL;
309         TALLOC_CTX *mem_ctx = talloc_tos();
310
311         if (theacl==NULL || smb_get_naces(theacl)==0)
312                 return NT_STATUS_ACCESS_DENIED; /* special because we
313                                                  * shouldn't alloc 0 for
314                                                  * win */
315
316         uid_to_sid(&sid_owner, sbuf->st_ex_uid);
317         gid_to_sid(&sid_group, sbuf->st_ex_gid);
318
319         if (smbacl4_nfs42win(mem_ctx, theacl, &sid_owner, &sid_group,
320                              S_ISDIR(sbuf->st_ex_mode),
321                                 &nt_ace_list, &good_aces)==False) {
322                 DEBUG(8,("smbacl4_nfs42win failed\n"));
323                 return map_nt_error_from_unix(errno);
324         }
325
326         psa = make_sec_acl(mem_ctx, NT4_ACL_REVISION, good_aces, nt_ace_list);
327         if (psa == NULL) {
328                 DEBUG(2,("make_sec_acl failed\n"));
329                 return NT_STATUS_NO_MEMORY;
330         }
331
332         DEBUG(10,("after make sec_acl\n"));
333         *ppdesc = make_sec_desc(mem_ctx, SD_REVISION, SEC_DESC_SELF_RELATIVE,
334                                 (security_info & SECINFO_OWNER) ? &sid_owner : NULL,
335                                 (security_info & SECINFO_GROUP) ? &sid_group : NULL,
336                                 NULL, psa, &sd_size);
337         if (*ppdesc==NULL) {
338                 DEBUG(2,("make_sec_desc failed\n"));
339                 return NT_STATUS_NO_MEMORY;
340         }
341
342         DEBUG(10, ("smb_get_nt_acl_nfs4_common successfully exited with sd_size %d\n",
343                    (int)ndr_size_security_descriptor(*ppdesc, 0)));
344
345         return NT_STATUS_OK;
346 }
347
348 NTSTATUS smb_fget_nt_acl_nfs4(files_struct *fsp,
349                                uint32 security_info,
350                                struct security_descriptor **ppdesc, SMB4ACL_T *theacl)
351 {
352         SMB_STRUCT_STAT sbuf;
353
354         DEBUG(10, ("smb_fget_nt_acl_nfs4 invoked for %s\n", fsp_str_dbg(fsp)));
355
356         if (smbacl4_fGetFileOwner(fsp, &sbuf)) {
357                 return map_nt_error_from_unix(errno);
358         }
359
360         return smb_get_nt_acl_nfs4_common(&sbuf, security_info, ppdesc, theacl);
361 }
362
363 NTSTATUS smb_get_nt_acl_nfs4(struct connection_struct *conn,
364                               const char *name,
365                               uint32 security_info,
366                               struct security_descriptor **ppdesc, SMB4ACL_T *theacl)
367 {
368         SMB_STRUCT_STAT sbuf;
369
370         DEBUG(10, ("smb_get_nt_acl_nfs4 invoked for %s\n", name));
371
372         if (smbacl4_GetFileOwner(conn, name, &sbuf)) {
373                 return map_nt_error_from_unix(errno);
374         }
375
376         return smb_get_nt_acl_nfs4_common(&sbuf, security_info, ppdesc, theacl);
377 }
378
379 enum smbacl4_mode_enum {e_simple=0, e_special=1};
380 enum smbacl4_acedup_enum {e_dontcare=0, e_reject=1, e_ignore=2, e_merge=3};
381
382 typedef struct _smbacl4_vfs_params {
383         enum smbacl4_mode_enum mode;
384         bool do_chown;
385         enum smbacl4_acedup_enum acedup;
386         struct db_context *sid_mapping_table;
387 } smbacl4_vfs_params;
388
389 /*
390  * Gather special parameters for NFS4 ACL handling
391  */
392 static int smbacl4_get_vfs_params(
393         const char *type_name,
394         files_struct *fsp,
395         smbacl4_vfs_params *params
396 )
397 {
398         static const struct enum_list enum_smbacl4_modes[] = {
399                 { e_simple, "simple" },
400                 { e_special, "special" }
401         };
402         static const struct enum_list enum_smbacl4_acedups[] = {
403                 { e_dontcare, "dontcare" },
404                 { e_reject, "reject" },
405                 { e_ignore, "ignore" },
406                 { e_merge, "merge" },
407         };
408
409         memset(params, 0, sizeof(smbacl4_vfs_params));
410         params->mode = (enum smbacl4_mode_enum)lp_parm_enum(
411                 SNUM(fsp->conn), type_name,
412                 "mode", enum_smbacl4_modes, e_simple);
413         params->do_chown = lp_parm_bool(SNUM(fsp->conn), type_name,
414                 "chown", True);
415         params->acedup = (enum smbacl4_acedup_enum)lp_parm_enum(
416                 SNUM(fsp->conn), type_name,
417                 "acedup", enum_smbacl4_acedups, e_dontcare);
418
419         DEBUG(10, ("mode:%s, do_chown:%s, acedup: %s\n",
420                 enum_smbacl4_modes[params->mode].name,
421                 params->do_chown ? "true" : "false",
422                 enum_smbacl4_acedups[params->acedup].name));
423
424         return 0;
425 }
426
427 static void smbacl4_dump_nfs4acl(int level, SMB4ACL_T *theacl)
428 {
429         SMB_ACL4_INT_T *aclint = get_validated_aclint(theacl);
430         SMB_ACE4_INT_T *aceint;
431
432         DEBUG(level, ("NFS4ACL: size=%d\n", aclint->naces));
433
434         for(aceint = aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
435                 SMB_ACE4PROP_T *ace = &aceint->prop;
436
437                 DEBUG(level, ("\tACE: type=%d, flags=0x%x, fflags=0x%x, mask=0x%x, id=%d\n",
438                         ace->aceType,
439                         ace->aceFlags, ace->flags,
440                         ace->aceMask,
441                         ace->who.id));
442         }
443 }
444
445 /* 
446  * Find 2 NFS4 who-special ACE property (non-copy!!!)
447  * match nonzero if "special" and who is equal
448  * return ace if found matching; otherwise NULL
449  */
450 static SMB_ACE4PROP_T *smbacl4_find_equal_special(
451         SMB4ACL_T *theacl,
452         SMB_ACE4PROP_T *aceNew)
453 {
454         SMB_ACL4_INT_T *aclint = get_validated_aclint(theacl);
455         SMB_ACE4_INT_T *aceint;
456
457         for(aceint = aclint->first; aceint!=NULL; aceint=(SMB_ACE4_INT_T *)aceint->next) {
458                 SMB_ACE4PROP_T *ace = &aceint->prop;
459
460                 DEBUG(10,("ace type:0x%x flags:0x%x aceFlags:0x%x "
461                           "new type:0x%x flags:0x%x aceFlags:0x%x\n",
462                           ace->aceType, ace->flags, ace->aceFlags,
463                           aceNew->aceType, aceNew->flags,aceNew->aceFlags));
464
465                 if (ace->flags == aceNew->flags &&
466                         ace->aceType==aceNew->aceType &&
467                         ((ace->aceFlags&SMB_ACE4_INHERIT_ONLY_ACE)==
468                          (aceNew->aceFlags&SMB_ACE4_INHERIT_ONLY_ACE)) &&
469                         (ace->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)==
470                         (aceNew->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)
471                 ) {
472                         /* keep type safety; e.g. gid is an u.short */
473                         if (ace->flags & SMB_ACE4_ID_SPECIAL)
474                         {
475                                 if (ace->who.special_id==aceNew->who.special_id)
476                                         return ace;
477                         } else {
478                                 if (ace->aceFlags & SMB_ACE4_IDENTIFIER_GROUP)
479                                 {
480                                         if (ace->who.gid==aceNew->who.gid)
481                                                 return ace;
482                                 } else {
483                                         if (ace->who.uid==aceNew->who.uid)
484                                                 return ace;
485                                 }
486                         }
487                 }
488         }
489
490         return NULL;
491 }
492
493 static bool nfs4_map_sid(smbacl4_vfs_params *params, const struct dom_sid *src,
494                          struct dom_sid *dst)
495 {
496         static struct db_context *mapping_db = NULL;
497         TDB_DATA data;
498         
499         if (mapping_db == NULL) {
500                 const char *dbname = lp_parm_const_string(
501                         -1, SMBACL4_PARAM_TYPE_NAME, "sidmap", NULL);
502                 
503                 if (dbname == NULL) {
504                         DEBUG(10, ("%s:sidmap not defined\n",
505                                    SMBACL4_PARAM_TYPE_NAME));
506                         return False;
507                 }
508                 
509                 become_root();
510                 mapping_db = db_open(NULL, dbname, 0, TDB_DEFAULT,
511                                      O_RDONLY, 0600);
512                 unbecome_root();
513                 
514                 if (mapping_db == NULL) {
515                         DEBUG(1, ("could not open sidmap: %s\n",
516                                   strerror(errno)));
517                         return False;
518                 }
519         }
520         
521         if (mapping_db->fetch(mapping_db, NULL,
522                               string_term_tdb_data(sid_string_tos(src)),
523                               &data) == -1) {
524                 DEBUG(10, ("could not find mapping for SID %s\n",
525                            sid_string_dbg(src)));
526                 return False;
527         }
528         
529         if ((data.dptr == NULL) || (data.dsize <= 0)
530             || (data.dptr[data.dsize-1] != '\0')) {
531                 DEBUG(5, ("invalid mapping for SID %s\n",
532                           sid_string_dbg(src)));
533                 TALLOC_FREE(data.dptr);
534                 return False;
535         }
536         
537         if (!string_to_sid(dst, (char *)data.dptr)) {
538                 DEBUG(1, ("invalid mapping %s for SID %s\n",
539                           (char *)data.dptr, sid_string_dbg(src)));
540                 TALLOC_FREE(data.dptr);
541                 return False;
542         }
543
544         TALLOC_FREE(data.dptr);
545         
546         return True;
547 }
548
549 static bool smbacl4_fill_ace4(
550         TALLOC_CTX *mem_ctx,
551         const char *filename,
552         smbacl4_vfs_params *params,
553         uid_t ownerUID,
554         gid_t ownerGID,
555         const struct security_ace *ace_nt, /* input */
556         SMB_ACE4PROP_T *ace_v4 /* output */
557 )
558 {
559         DEBUG(10, ("got ace for %s\n", sid_string_dbg(&ace_nt->trustee)));
560
561         memset(ace_v4, 0, sizeof(SMB_ACE4PROP_T));
562         ace_v4->aceType = ace_nt->type; /* only ACCESS|DENY supported right now */
563         ace_v4->aceFlags = ace_nt->flags & SEC_ACE_FLAG_VALID_INHERIT;
564         ace_v4->aceMask = ace_nt->access_mask &
565                 (SEC_STD_ALL | SEC_FILE_ALL);
566
567         se_map_generic(&ace_v4->aceMask, &file_generic_mapping);
568
569         if (ace_v4->aceFlags!=ace_nt->flags)
570                 DEBUG(9, ("ace_v4->aceFlags(0x%x)!=ace_nt->flags(0x%x)\n",
571                         ace_v4->aceFlags, ace_nt->flags));
572
573         if (ace_v4->aceMask!=ace_nt->access_mask)
574                 DEBUG(9, ("ace_v4->aceMask(0x%x)!=ace_nt->access_mask(0x%x)\n",
575                         ace_v4->aceMask, ace_nt->access_mask));
576
577         if (dom_sid_equal(&ace_nt->trustee, &global_sid_World)) {
578                 ace_v4->who.special_id = SMB_ACE4_WHO_EVERYONE;
579                 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
580         } else {
581                 const char *dom, *name;
582                 enum lsa_SidType type;
583                 uid_t uid;
584                 gid_t gid;
585                 struct dom_sid sid;
586                 
587                 sid_copy(&sid, &ace_nt->trustee);
588                 
589                 if (!lookup_sid(mem_ctx, &sid, &dom, &name, &type)) {
590                         
591                         struct dom_sid mapped;
592                         
593                         if (!nfs4_map_sid(params, &sid, &mapped)) {
594                                 DEBUG(1, ("nfs4_acls.c: file [%s]: SID %s "
595                                           "unknown\n", filename, sid_string_dbg(&sid)));
596                                 errno = EINVAL;
597                                 return False;
598                         }
599                         
600                         DEBUG(2, ("nfs4_acls.c: file [%s]: mapped SID %s "
601                                   "to %s\n", filename, sid_string_dbg(&sid), sid_string_dbg(&mapped)));
602                         
603                         if (!lookup_sid(mem_ctx, &mapped, &dom,
604                                         &name, &type)) {
605                                 DEBUG(1, ("nfs4_acls.c: file [%s]: SID %s "
606                                           "mapped from %s is unknown\n",
607                                           filename, sid_string_dbg(&mapped), sid_string_dbg(&sid)));
608                                 errno = EINVAL;
609                                 return False;
610                         }
611                         
612                         sid_copy(&sid, &mapped);
613                 }
614                 
615                 if (type == SID_NAME_USER) {
616                         if (!sid_to_uid(&sid, &uid)) {
617                                 DEBUG(1, ("nfs4_acls.c: file [%s]: could not "
618                                           "convert %s to uid\n", filename,
619                                           sid_string_dbg(&sid)));
620                                 return False;
621                         }
622
623                         if (params->mode==e_special && uid==ownerUID) {
624                                 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
625                                 ace_v4->who.special_id = SMB_ACE4_WHO_OWNER;
626                         } else {
627                                 ace_v4->who.uid = uid;
628                         }
629                 } else { /* else group? - TODO check it... */
630                         if (!sid_to_gid(&sid, &gid)) {
631                                 DEBUG(1, ("nfs4_acls.c: file [%s]: could not "
632                                           "convert %s to gid\n", filename,
633                                           sid_string_dbg(&sid)));
634                                 return False;
635                         }
636                                 
637                         ace_v4->aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
638
639                         if (params->mode==e_special && gid==ownerGID) {
640                                 ace_v4->flags |= SMB_ACE4_ID_SPECIAL;
641                                 ace_v4->who.special_id = SMB_ACE4_WHO_GROUP;
642                         } else {
643                                 ace_v4->who.gid = gid;
644                         }
645                 }
646         }
647
648         return True; /* OK */
649 }
650
651 static int smbacl4_MergeIgnoreReject(
652         enum smbacl4_acedup_enum acedup,
653         SMB4ACL_T *theacl, /* may modify it */
654         SMB_ACE4PROP_T *ace, /* the "new" ACE */
655         bool    *paddNewACE,
656         int     i
657 )
658 {
659         int     result = 0;
660         SMB_ACE4PROP_T *ace4found = smbacl4_find_equal_special(theacl, ace);
661         if (ace4found)
662         {
663                 switch(acedup)
664                 {
665                 case e_merge: /* "merge" flags */
666                         *paddNewACE = False;
667                         ace4found->aceFlags |= ace->aceFlags;
668                         ace4found->aceMask |= ace->aceMask;
669                         break;
670                 case e_ignore: /* leave out this record */
671                         *paddNewACE = False;
672                         break;
673                 case e_reject: /* do an error */
674                         DEBUG(8, ("ACL rejected by duplicate nt ace#%d\n", i));
675                         errno = EINVAL; /* SHOULD be set on any _real_ error */
676                         result = -1;
677                         break;
678                 default:
679                         break;
680                 }
681         }
682         return result;
683 }
684
685 static SMB4ACL_T *smbacl4_win2nfs4(
686         const char *filename,
687         const struct security_acl *dacl,
688         smbacl4_vfs_params *pparams,
689         uid_t ownerUID,
690         gid_t ownerGID
691 )
692 {
693         SMB4ACL_T *theacl;
694         uint32  i;
695         TALLOC_CTX *mem_ctx = talloc_tos();
696
697         DEBUG(10, ("smbacl4_win2nfs4 invoked\n"));
698
699         theacl = smb_create_smb4acl();
700         if (theacl==NULL)
701                 return NULL;
702
703         for(i=0; i<dacl->num_aces; i++) {
704                 SMB_ACE4PROP_T  ace_v4;
705                 bool    addNewACE = True;
706
707                 if (!smbacl4_fill_ace4(mem_ctx, filename, pparams,
708                                        ownerUID, ownerGID,
709                                        dacl->aces + i, &ace_v4)) {
710                         DEBUG(3, ("Could not fill ace for file %s, SID %s\n",
711                                   filename,
712                                   sid_string_dbg(&((dacl->aces+i)->trustee))));
713                         continue;
714                 }
715
716                 if (pparams->acedup!=e_dontcare) {
717                         if (smbacl4_MergeIgnoreReject(pparams->acedup, theacl,
718                                 &ace_v4, &addNewACE, i))
719                                 return NULL;
720                 }
721
722                 if (addNewACE)
723                         smb_add_ace4(theacl, &ace_v4);
724         }
725
726         return theacl;
727 }
728
729 NTSTATUS smb_set_nt_acl_nfs4(files_struct *fsp,
730         uint32 security_info_sent,
731         const struct security_descriptor *psd,
732         set_nfs4acl_native_fn_t set_nfs4_native)
733 {
734         smbacl4_vfs_params params;
735         SMB4ACL_T *theacl = NULL;
736         bool    result;
737
738         SMB_STRUCT_STAT sbuf;
739         bool set_acl_as_root = false;
740         uid_t newUID = (uid_t)-1;
741         gid_t newGID = (gid_t)-1;
742         int saved_errno;
743
744         DEBUG(10, ("smb_set_nt_acl_nfs4 invoked for %s\n", fsp_str_dbg(fsp)));
745
746         if ((security_info_sent & (SECINFO_DACL |
747                 SECINFO_GROUP | SECINFO_OWNER)) == 0)
748         {
749                 DEBUG(9, ("security_info_sent (0x%x) ignored\n",
750                         security_info_sent));
751                 return NT_STATUS_OK; /* won't show error - later to be refined... */
752         }
753
754         /* Special behaviours */
755         if (smbacl4_get_vfs_params(SMBACL4_PARAM_TYPE_NAME, fsp, &params))
756                 return NT_STATUS_NO_MEMORY;
757
758         if (smbacl4_fGetFileOwner(fsp, &sbuf))
759                 return map_nt_error_from_unix(errno);
760
761         if (params.do_chown) {
762                 /* chown logic is a copy/paste from posix_acl.c:set_nt_acl */
763                 NTSTATUS status = unpack_nt_owners(fsp->conn, &newUID, &newGID, security_info_sent, psd);
764                 if (!NT_STATUS_IS_OK(status)) {
765                         DEBUG(8, ("unpack_nt_owners failed"));
766                         return status;
767                 }
768                 if (((newUID != (uid_t)-1) && (sbuf.st_ex_uid != newUID)) ||
769                     ((newGID != (gid_t)-1) && (sbuf.st_ex_gid != newGID))) {
770
771                         status = try_chown(fsp, newUID, newGID);
772                         if (!NT_STATUS_IS_OK(status)) {
773                                 DEBUG(3,("chown %s, %u, %u failed. Error = "
774                                          "%s.\n", fsp_str_dbg(fsp),
775                                          (unsigned int)newUID,
776                                          (unsigned int)newGID,
777                                          nt_errstr(status)));
778                                 return status;
779                         }
780
781                         DEBUG(10,("chown %s, %u, %u succeeded.\n",
782                                   fsp_str_dbg(fsp), (unsigned int)newUID,
783                                   (unsigned int)newGID));
784                         if (smbacl4_GetFileOwner(fsp->conn,
785                                                  fsp->fsp_name->base_name,
786                                                  &sbuf))
787                                 return map_nt_error_from_unix(errno);
788
789                         /* If we successfully chowned, we know we must
790                          * be able to set the acl, so do it as root.
791                          */
792                         set_acl_as_root = true;
793                 }
794         }
795
796         if (!(security_info_sent & SECINFO_DACL) || psd->dacl ==NULL) {
797                 DEBUG(10, ("no dacl found; security_info_sent = 0x%x\n", security_info_sent));
798                 return NT_STATUS_OK;
799         }
800
801         theacl = smbacl4_win2nfs4(fsp->fsp_name->base_name, psd->dacl, &params,
802                                   sbuf.st_ex_uid, sbuf.st_ex_gid);
803         if (!theacl)
804                 return map_nt_error_from_unix(errno);
805
806         smbacl4_dump_nfs4acl(10, theacl);
807
808         if (set_acl_as_root) {
809                 become_root();
810         }
811         result = set_nfs4_native(fsp, theacl);
812         saved_errno = errno;
813         if (set_acl_as_root) {
814                 unbecome_root();
815         }
816         if (result!=True) {
817                 errno = saved_errno;
818                 DEBUG(10, ("set_nfs4_native failed with %s\n", strerror(errno)));
819                 return map_nt_error_from_unix(errno);
820         }
821
822         DEBUG(10, ("smb_set_nt_acl_nfs4 succeeded\n"));
823         return NT_STATUS_OK;
824 }