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