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