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