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