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