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