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