s3: Change SMB_VFS_OPEN to take an smb_filename struct
[samba.git] / source3 / modules / vfs_acl_tdb.c
1 /*
2  * Store Windows ACLs in a tdb.
3  *
4  * Copyright (C) Volker Lendecke, 2008
5  * Copyright (C) Jeremy Allison, 2008
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 /* NOTE: This is an experimental module, not yet finished. JRA. */
22
23 #include "includes.h"
24 #include "librpc/gen_ndr/xattr.h"
25 #include "librpc/gen_ndr/ndr_xattr.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_VFS
29
30 static unsigned int ref_count;
31 static struct db_context *acl_db;
32
33 /*******************************************************************
34  Open acl_db if not already open, increment ref count.
35 *******************************************************************/
36
37 static bool acl_tdb_init(struct db_context **pp_db)
38 {
39         char *dbname;
40
41         if (acl_db) {
42                 *pp_db = acl_db;
43                 ref_count++;
44                 return true;
45         }
46
47         dbname = state_path("file_ntacls.tdb");
48
49         if (dbname == NULL) {
50                 errno = ENOSYS;
51                 return false;
52         }
53
54         become_root();
55         *pp_db = db_open(NULL, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
56         unbecome_root();
57
58         if (*pp_db == NULL) {
59 #if defined(ENOTSUP)
60                 errno = ENOTSUP;
61 #else
62                 errno = ENOSYS;
63 #endif
64                 TALLOC_FREE(dbname);
65                 return false;
66         }
67
68         ref_count++;
69         TALLOC_FREE(dbname);
70         return true;
71 }
72
73 /*******************************************************************
74  Lower ref count and close acl_db if zero.
75 *******************************************************************/
76
77 static void free_acl_tdb_data(void **pptr)
78 {
79         struct db_context **pp_db = (struct db_context **)pptr;
80
81         ref_count--;
82         if (ref_count == 0) {
83                 TALLOC_FREE(*pp_db);
84                 acl_db = NULL;
85         }
86 }
87
88 /*******************************************************************
89  Fetch_lock the tdb acl record for a file
90 *******************************************************************/
91
92 static struct db_record *acl_tdb_lock(TALLOC_CTX *mem_ctx,
93                                         struct db_context *db,
94                                         const struct file_id *id)
95 {
96         uint8 id_buf[16];
97
98         /* For backwards compatibility only store the dev/inode. */
99         push_file_id_16((char *)id_buf, id);
100         return db->fetch_locked(db,
101                                 mem_ctx,
102                                 make_tdb_data(id_buf,
103                                         sizeof(id_buf)));
104 }
105
106 /*******************************************************************
107  Delete the tdb acl record for a file
108 *******************************************************************/
109
110 static NTSTATUS acl_tdb_delete(vfs_handle_struct *handle,
111                                 struct db_context *db,
112                                 SMB_STRUCT_STAT *psbuf)
113 {
114         NTSTATUS status;
115         struct file_id id = vfs_file_id_from_sbuf(handle->conn, psbuf);
116         struct db_record *rec = acl_tdb_lock(talloc_tos(), db, &id);
117
118         /*
119          * If rec == NULL there's not much we can do about it
120          */
121
122         if (rec == NULL) {
123                 DEBUG(10,("acl_tdb_delete: rec == NULL\n"));
124                 TALLOC_FREE(rec);
125                 return NT_STATUS_OK;
126         }
127
128         status = rec->delete_rec(rec);
129         TALLOC_FREE(rec);
130         return status;
131 }
132
133 /*******************************************************************
134  Parse out a struct security_descriptor from a DATA_BLOB.
135 *******************************************************************/
136
137 static NTSTATUS parse_acl_blob(const DATA_BLOB *pblob,
138                                 uint32 security_info,
139                                 struct security_descriptor **ppdesc)
140 {
141         TALLOC_CTX *ctx = talloc_tos();
142         struct xattr_NTACL xacl;
143         enum ndr_err_code ndr_err;
144         size_t sd_size;
145
146         ndr_err = ndr_pull_struct_blob(pblob, ctx, NULL, &xacl,
147                         (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
148
149         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
150                 DEBUG(5, ("parse_acl_blob: ndr_pull_xattr_NTACL failed: %s\n",
151                         ndr_errstr(ndr_err)));
152                 return ndr_map_error2ntstatus(ndr_err);;
153         }
154
155         if (xacl.version != 2) {
156                 return NT_STATUS_REVISION_MISMATCH;
157         }
158
159         *ppdesc = make_sec_desc(ctx, SEC_DESC_REVISION, xacl.info.sd_hs->sd->type | SEC_DESC_SELF_RELATIVE,
160                         (security_info & OWNER_SECURITY_INFORMATION)
161                         ? xacl.info.sd_hs->sd->owner_sid : NULL,
162                         (security_info & GROUP_SECURITY_INFORMATION)
163                         ? xacl.info.sd_hs->sd->group_sid : NULL,
164                         (security_info & SACL_SECURITY_INFORMATION)
165                         ? xacl.info.sd_hs->sd->sacl : NULL,
166                         (security_info & DACL_SECURITY_INFORMATION)
167                         ? xacl.info.sd_hs->sd->dacl : NULL,
168                         &sd_size);
169
170         TALLOC_FREE(xacl.info.sd);
171
172         return (*ppdesc != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
173 }
174
175 /*******************************************************************
176  Pull a security descriptor into a DATA_BLOB from a tdb store.
177 *******************************************************************/
178
179 static NTSTATUS get_acl_blob(TALLOC_CTX *ctx,
180                         vfs_handle_struct *handle,
181                         files_struct *fsp,
182                         const char *name,
183                         DATA_BLOB *pblob)
184 {
185         uint8 id_buf[16];
186         TDB_DATA data;
187         struct file_id id;
188         struct db_context *db;
189         int ret = -1;
190         SMB_STRUCT_STAT sbuf;
191
192         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
193                 return NT_STATUS_INTERNAL_DB_CORRUPTION);
194
195         if (fsp && fsp->fh->fd != -1) {
196                 ret = SMB_VFS_FSTAT(fsp, &sbuf);
197         } else {
198                 if (fsp && fsp->posix_open) {
199                         ret = SMB_VFS_LSTAT(handle->conn, name, &sbuf);
200                 } else {
201                         ret = SMB_VFS_STAT(handle->conn, name, &sbuf);
202                 }
203         }
204
205         if (ret == -1) {
206                 return map_nt_error_from_unix(errno);
207         }
208
209         id = vfs_file_id_from_sbuf(handle->conn, &sbuf);
210
211         /* For backwards compatibility only store the dev/inode. */
212         push_file_id_16((char *)id_buf, &id);
213
214         if (db->fetch(db,
215                         ctx,
216                         make_tdb_data(id_buf, sizeof(id_buf)),
217                         &data) == -1) {
218                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
219         }
220
221         pblob->data = data.dptr;
222         pblob->length = data.dsize;
223
224         DEBUG(10,("get_acl_blob: returned %u bytes from file %s\n",
225                 (unsigned int)data.dsize, name ));
226
227         if (pblob->length == 0 || pblob->data == NULL) {
228                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
229         }
230         return NT_STATUS_OK;
231 }
232
233 /*******************************************************************
234  Create a DATA_BLOB from a security descriptor.
235 *******************************************************************/
236
237 static NTSTATUS create_acl_blob(const struct security_descriptor *psd, DATA_BLOB *pblob)
238 {
239         struct xattr_NTACL xacl;
240         struct security_descriptor_hash sd_hs;
241         enum ndr_err_code ndr_err;
242         TALLOC_CTX *ctx = talloc_tos();
243
244         ZERO_STRUCT(xacl);
245         ZERO_STRUCT(sd_hs);
246
247         xacl.version = 2;
248         xacl.info.sd_hs = &sd_hs;
249         xacl.info.sd_hs->sd = CONST_DISCARD(struct security_descriptor *, psd);
250         memset(&xacl.info.sd_hs->hash[0], '\0', 16);
251
252         ndr_err = ndr_push_struct_blob(
253                         pblob, ctx, NULL, &xacl,
254                         (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
255
256         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
257                 DEBUG(5, ("create_acl_blob: ndr_push_xattr_NTACL failed: %s\n",
258                         ndr_errstr(ndr_err)));
259                 return ndr_map_error2ntstatus(ndr_err);;
260         }
261
262         return NT_STATUS_OK;
263 }
264
265 /*******************************************************************
266  Store a DATA_BLOB into a tdb record given an fsp pointer.
267 *******************************************************************/
268
269 static NTSTATUS store_acl_blob_fsp(vfs_handle_struct *handle,
270                                 files_struct *fsp,
271                                 DATA_BLOB *pblob)
272 {
273         uint8 id_buf[16];
274         struct file_id id;
275         SMB_STRUCT_STAT sbuf;
276         TDB_DATA data;
277         struct db_context *db;
278         struct db_record *rec;
279         int ret = -1;
280
281         DEBUG(10,("store_acl_blob_fsp: storing blob length %u on file %s\n",
282                         (unsigned int)pblob->length, fsp->fsp_name));
283
284         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
285                 return NT_STATUS_INTERNAL_DB_CORRUPTION);
286
287         if (fsp->fh->fd != -1) {
288                 ret = SMB_VFS_FSTAT(fsp, &sbuf);
289         } else {
290                 if (fsp->posix_open) {
291                         ret = SMB_VFS_LSTAT(handle->conn, fsp->fsp_name, &sbuf);
292                 } else {
293                         ret = SMB_VFS_STAT(handle->conn, fsp->fsp_name, &sbuf);
294                 }
295         }
296
297         if (ret == -1) {
298                 return map_nt_error_from_unix(errno);
299         }
300
301         id = vfs_file_id_from_sbuf(handle->conn, &sbuf);
302
303         /* For backwards compatibility only store the dev/inode. */
304         push_file_id_16((char *)id_buf, &id);
305         rec = db->fetch_locked(db, talloc_tos(),
306                                 make_tdb_data(id_buf,
307                                         sizeof(id_buf)));
308         if (rec == NULL) {
309                 DEBUG(0, ("store_acl_blob_fsp_tdb: fetch_lock failed\n"));
310                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
311         }
312         data.dptr = pblob->data;
313         data.dsize = pblob->length;
314         return rec->store(rec, data, 0);
315 }
316
317 /*******************************************************************
318  Store a DATA_BLOB into a tdb record given a pathname.
319 *******************************************************************/
320
321 static NTSTATUS store_acl_blob_pathname(vfs_handle_struct *handle,
322                                         const char *fname,
323                                         DATA_BLOB *pblob)
324 {
325         uint8 id_buf[16];
326         struct file_id id;
327         TDB_DATA data;
328         SMB_STRUCT_STAT sbuf;
329         struct db_context *db;
330         struct db_record *rec;
331         int ret = -1;
332
333         DEBUG(10,("store_acl_blob_pathname: storing blob "
334                         "length %u on file %s\n",
335                         (unsigned int)pblob->length, fname));
336
337         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
338                 return NT_STATUS_INTERNAL_DB_CORRUPTION);
339
340         if (lp_posix_pathnames()) {
341                 ret = SMB_VFS_LSTAT(handle->conn, fname, &sbuf);
342         } else {
343                 ret = SMB_VFS_STAT(handle->conn, fname, &sbuf);
344         }
345
346         if (ret == -1) {
347                 return map_nt_error_from_unix(errno);
348         }
349
350         id = vfs_file_id_from_sbuf(handle->conn, &sbuf);
351
352         /* For backwards compatibility only store the dev/inode. */
353         push_file_id_16((char *)id_buf, &id);
354
355         rec = db->fetch_locked(db, talloc_tos(),
356                                 make_tdb_data(id_buf,
357                                         sizeof(id_buf)));
358         if (rec == NULL) {
359                 DEBUG(0, ("store_acl_blob_pathname_tdb: fetch_lock failed\n"));
360                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
361         }
362         data.dptr = pblob->data;
363         data.dsize = pblob->length;
364         return rec->store(rec, data, 0);
365 }
366
367 /*******************************************************************
368  Store a DATA_BLOB into an tdb given a pathname.
369 *******************************************************************/
370
371 static NTSTATUS get_nt_acl_tdb_internal(vfs_handle_struct *handle,
372                                         files_struct *fsp,
373                                         const char *name,
374                                         uint32 security_info,
375                                         struct security_descriptor **ppdesc)
376 {
377         TALLOC_CTX *ctx = talloc_tos();
378         DATA_BLOB blob;
379         NTSTATUS status;
380
381         if (fsp && name == NULL) {
382                 name = fsp->fsp_name;
383         }
384
385         DEBUG(10, ("get_nt_acl_tdb_internal: name=%s\n", name));
386
387         status = get_acl_blob(ctx, handle, fsp, name, &blob);
388         if (!NT_STATUS_IS_OK(status)) {
389                 DEBUG(10, ("get_acl_blob returned %s\n", nt_errstr(status)));
390                 return status;
391         }
392
393         status = parse_acl_blob(&blob, security_info, ppdesc);
394         if (!NT_STATUS_IS_OK(status)) {
395                 DEBUG(10, ("parse_acl_blob returned %s\n",
396                                 nt_errstr(status)));
397                 return status;
398         }
399
400         TALLOC_FREE(blob.data);
401         return status;
402 }
403
404 /*********************************************************************
405  Create a default security descriptor for a file in case no inheritance
406  exists. All permissions to the owner and SYSTEM.
407 *********************************************************************/
408
409 static struct security_descriptor *default_file_sd(TALLOC_CTX *mem_ctx,
410                                                 SMB_STRUCT_STAT *psbuf)
411 {
412         struct dom_sid owner_sid, group_sid;
413         size_t sd_size;
414         struct security_ace *pace = NULL;
415         struct security_acl *pacl = NULL;
416
417         uid_to_sid(&owner_sid, psbuf->st_ex_uid);
418         gid_to_sid(&group_sid, psbuf->st_ex_gid);
419
420         pace = TALLOC_ARRAY(mem_ctx, struct security_ace, 2);
421         if (!pace) {
422                 return NULL;
423         }
424
425         init_sec_ace(&pace[0], &owner_sid, SEC_ACE_TYPE_ACCESS_ALLOWED,
426                         SEC_RIGHTS_FILE_ALL, 0);
427         init_sec_ace(&pace[1], &global_sid_System, SEC_ACE_TYPE_ACCESS_ALLOWED,
428                         SEC_RIGHTS_FILE_ALL, 0);
429
430         pacl = make_sec_acl(mem_ctx,
431                                 NT4_ACL_REVISION,
432                                 2,
433                                 pace);
434         if (!pacl) {
435                 return NULL;
436         }
437         return make_sec_desc(mem_ctx,
438                         SECURITY_DESCRIPTOR_REVISION_1,
439                         SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
440                         &owner_sid,
441                         &group_sid,
442                         NULL,
443                         pacl,
444                         &sd_size);
445 }
446
447 /*********************************************************************
448 *********************************************************************/
449
450 static NTSTATUS inherit_new_acl(vfs_handle_struct *handle,
451                                         const char *fname,
452                                         files_struct *fsp,
453                                         bool container)
454 {
455         TALLOC_CTX *ctx = talloc_tos();
456         NTSTATUS status;
457         struct security_descriptor *parent_desc = NULL;
458         struct security_descriptor *psd = NULL;
459         DATA_BLOB blob;
460         size_t size;
461         char *parent_name;
462
463         if (!parent_dirname(ctx, fname, &parent_name, NULL)) {
464                 return NT_STATUS_NO_MEMORY;
465         }
466
467         DEBUG(10,("inherit_new_acl: check directory %s\n",
468                         parent_name));
469
470         status = get_nt_acl_tdb_internal(handle,
471                                         NULL,
472                                         parent_name,
473                                         (OWNER_SECURITY_INFORMATION |
474                                          GROUP_SECURITY_INFORMATION |
475                                          DACL_SECURITY_INFORMATION),
476                                         &parent_desc);
477         if (NT_STATUS_IS_OK(status)) {
478                 /* Create an inherited descriptor from the parent. */
479
480                 if (DEBUGLEVEL >= 10) {
481                         DEBUG(10,("inherit_new_acl: parent acl is:\n"));
482                         NDR_PRINT_DEBUG(security_descriptor, parent_desc);
483                 }
484
485                 status = se_create_child_secdesc(ctx,
486                                 &psd,
487                                 &size,
488                                 parent_desc,
489                                 &handle->conn->server_info->ptok->user_sids[PRIMARY_USER_SID_INDEX],
490                                 &handle->conn->server_info->ptok->user_sids[PRIMARY_GROUP_SID_INDEX],
491                                 container);
492                 if (!NT_STATUS_IS_OK(status)) {
493                         return status;
494                 }
495
496                 if (DEBUGLEVEL >= 10) {
497                         DEBUG(10,("inherit_new_acl: child acl is:\n"));
498                         NDR_PRINT_DEBUG(security_descriptor, psd);
499                 }
500
501         } else {
502                 DEBUG(10,("inherit_new_acl: directory %s failed "
503                         "to get acl %s\n",
504                         parent_name,
505                         nt_errstr(status) ));
506         }
507
508         if (!psd || psd->dacl == NULL) {
509                 SMB_STRUCT_STAT sbuf;
510                 int ret;
511
512                 TALLOC_FREE(psd);
513                 if (fsp && !fsp->is_directory && fsp->fh->fd != -1) {
514                         ret = SMB_VFS_FSTAT(fsp, &sbuf);
515                 } else {
516                         if (fsp && fsp->posix_open) {
517                                 ret = SMB_VFS_LSTAT(handle->conn,fname, &sbuf);
518                         } else {
519                                 ret = SMB_VFS_STAT(handle->conn,fname, &sbuf);
520                         }
521                 }
522                 if (ret == -1) {
523                         return map_nt_error_from_unix(errno);
524                 }
525                 psd = default_file_sd(ctx, &sbuf);
526                 if (!psd) {
527                         return NT_STATUS_NO_MEMORY;
528                 }
529
530                 if (DEBUGLEVEL >= 10) {
531                         DEBUG(10,("inherit_new_acl: default acl is:\n"));
532                         NDR_PRINT_DEBUG(security_descriptor, psd);
533                 }
534         }
535
536         status = create_acl_blob(psd, &blob);
537         if (!NT_STATUS_IS_OK(status)) {
538                 return status;
539         }
540         if (fsp) {
541                 return store_acl_blob_fsp(handle, fsp, &blob);
542         } else {
543                 return store_acl_blob_pathname(handle, fname, &blob);
544         }
545 }
546
547 /*********************************************************************
548  Check ACL on open. For new files inherit from parent directory.
549 *********************************************************************/
550
551 static int open_acl_tdb(vfs_handle_struct *handle,
552                                         struct smb_filename *smb_fname,
553                                         files_struct *fsp,
554                                         int flags,
555                                         mode_t mode)
556 {
557         uint32_t access_granted = 0;
558         struct security_descriptor *pdesc = NULL;
559         bool file_existed = true;
560         char *fname = NULL;
561         NTSTATUS status;
562
563         status = get_full_smb_filename(talloc_tos(), smb_fname,
564                                        &fname);
565         if (!NT_STATUS_IS_OK(status)) {
566                 errno = map_errno_from_nt_status(status);
567                 return -1;
568         }
569
570         status = get_nt_acl_tdb_internal(handle,
571                                         NULL,
572                                         fname,
573                                         (OWNER_SECURITY_INFORMATION |
574                                          GROUP_SECURITY_INFORMATION |
575                                          DACL_SECURITY_INFORMATION),
576                                         &pdesc);
577         if (NT_STATUS_IS_OK(status)) {
578                 /* See if we can access it. */
579                 status = smb1_file_se_access_check(pdesc,
580                                         handle->conn->server_info->ptok,
581                                         fsp->access_mask,
582                                         &access_granted);
583                 if (!NT_STATUS_IS_OK(status)) {
584                         DEBUG(10,("open_acl_tdb: file %s open "
585                                 "refused with error %s\n",
586                                 smb_fname_str_dbg(smb_fname),
587                                 nt_errstr(status) ));
588                         errno = map_errno_from_nt_status(status);
589                         return -1;
590                 }
591         } else if (NT_STATUS_EQUAL(status,NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
592                 file_existed = false;
593         }
594
595         DEBUG(10,("open_acl_tdb: get_nt_acl_attr_internal for "
596                 "file %s returned %s\n",
597                 smb_fname_str_dbg(smb_fname),
598                 nt_errstr(status) ));
599
600         fsp->fh->fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
601
602         if (!file_existed && fsp->fh->fd != -1) {
603                 /* File was created. Inherit from parent directory. */
604                 string_set(&fsp->fsp_name, fname);
605                 inherit_new_acl(handle, fname, fsp, false);
606         }
607
608         return fsp->fh->fd;
609 }
610
611 /*********************************************************************
612  On unlink we need to delete the tdb record (if using tdb).
613 *********************************************************************/
614
615 static int unlink_acl_tdb(vfs_handle_struct *handle, const char *path)
616 {
617         SMB_STRUCT_STAT sbuf;
618         struct db_context *db;
619         int ret = -1;
620
621         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
622
623         if (lp_posix_pathnames()) {
624                 ret = SMB_VFS_LSTAT(handle->conn, path, &sbuf);
625         } else {
626                 ret = SMB_VFS_STAT(handle->conn, path, &sbuf);
627         }
628
629         if (ret == -1) {
630                 return -1;
631         }
632
633         ret = SMB_VFS_NEXT_UNLINK(handle, path);
634
635         if (ret == -1) {
636                 return -1;
637         }
638
639         acl_tdb_delete(handle, db, &sbuf);
640         return 0;
641 }
642
643 /*********************************************************************
644  Store an inherited SD on mkdir.
645 *********************************************************************/
646
647 static int mkdir_acl_tdb(vfs_handle_struct *handle, const char *path, mode_t mode)
648 {
649         int ret = SMB_VFS_NEXT_MKDIR(handle, path, mode);
650
651         if (ret == -1) {
652                 return ret;
653         }
654         /* New directory - inherit from parent. */
655         inherit_new_acl(handle, path, NULL, true);
656         return ret;
657 }
658
659 /*********************************************************************
660  On rmdir we need to delete the tdb record (if using tdb).
661 *********************************************************************/
662
663 static int rmdir_acl_tdb(vfs_handle_struct *handle, const char *path)
664 {
665
666         SMB_STRUCT_STAT sbuf;
667         struct db_context *db;
668         int ret = -1;
669
670         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
671
672         if (lp_posix_pathnames()) {
673                 ret = SMB_VFS_LSTAT(handle->conn, path, &sbuf);
674         } else {
675                 ret = SMB_VFS_STAT(handle->conn, path, &sbuf);
676         }
677
678         if (ret == -1) {
679                 return -1;
680         }
681
682         ret = SMB_VFS_NEXT_RMDIR(handle, path);
683         if (ret == -1) {
684                 return -1;
685         }
686
687         acl_tdb_delete(handle, db, &sbuf);
688         return 0;
689 }
690
691 /*********************************************************************
692  Fetch a security descriptor given an fsp.
693 *********************************************************************/
694
695 static NTSTATUS fget_nt_acl_tdb(vfs_handle_struct *handle, files_struct *fsp,
696         uint32 security_info, struct security_descriptor **ppdesc)
697 {
698         NTSTATUS status = get_nt_acl_tdb_internal(handle, fsp,
699                                 NULL, security_info, ppdesc);
700         if (NT_STATUS_IS_OK(status)) {
701                 if (DEBUGLEVEL >= 10) {
702                         DEBUG(10,("fget_nt_acl_tdb: returning tdb sd for file %s\n",
703                                 fsp->fsp_name));
704                         NDR_PRINT_DEBUG(security_descriptor, *ppdesc);
705                 }
706                 return NT_STATUS_OK;
707         }
708
709         DEBUG(10,("fget_nt_acl_tdb: failed to get tdb sd for file %s, Error %s\n",
710                         fsp->fsp_name,
711                         nt_errstr(status) ));
712
713         return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp,
714                         security_info, ppdesc);
715 }
716
717 /*********************************************************************
718  Fetch a security descriptor given a pathname.
719 *********************************************************************/
720
721 static NTSTATUS get_nt_acl_tdb(vfs_handle_struct *handle,
722         const char *name, uint32 security_info, struct security_descriptor **ppdesc)
723 {
724         NTSTATUS status = get_nt_acl_tdb_internal(handle, NULL,
725                                 name, security_info, ppdesc);
726         if (NT_STATUS_IS_OK(status)) {
727                 if (DEBUGLEVEL >= 10) {
728                         DEBUG(10,("get_nt_acl_tdb: returning tdb sd for file %s\n",
729                                 name));
730                         NDR_PRINT_DEBUG(security_descriptor, *ppdesc);
731                 }
732                 return NT_STATUS_OK;
733         }
734
735         DEBUG(10,("get_nt_acl_tdb: failed to get tdb sd for file %s, Error %s\n",
736                         name,
737                         nt_errstr(status) ));
738
739         return SMB_VFS_NEXT_GET_NT_ACL(handle, name,
740                         security_info, ppdesc);
741 }
742
743 /*********************************************************************
744  Store a security descriptor given an fsp.
745 *********************************************************************/
746
747 static NTSTATUS fset_nt_acl_tdb(vfs_handle_struct *handle, files_struct *fsp,
748         uint32 security_info_sent, const struct security_descriptor *psd)
749 {
750         NTSTATUS status;
751         DATA_BLOB blob;
752
753         if (DEBUGLEVEL >= 10) {
754                 DEBUG(10,("fset_nt_acl_tdb: incoming sd for file %s\n",
755                         fsp->fsp_name));
756                 NDR_PRINT_DEBUG(security_descriptor,
757                         CONST_DISCARD(struct security_descriptor *,psd));
758         }
759
760         status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
761         if (!NT_STATUS_IS_OK(status)) {
762                 return status;
763         }
764
765         /* Ensure owner and group are set. */
766         if (!psd->owner_sid || !psd->group_sid) {
767                 int ret;
768                 SMB_STRUCT_STAT sbuf;
769                 DOM_SID owner_sid, group_sid;
770                 struct security_descriptor *nc_psd = dup_sec_desc(talloc_tos(), psd);
771
772                 if (!nc_psd) {
773                         return NT_STATUS_OK;
774                 }
775                 if (fsp->is_directory || fsp->fh->fd == -1) {
776                         if (fsp->posix_open) {
777                                 ret = SMB_VFS_LSTAT(fsp->conn,fsp->fsp_name, &sbuf);
778                         } else {
779                                 ret = SMB_VFS_STAT(fsp->conn,fsp->fsp_name, &sbuf);
780                         }
781                 } else {
782                         ret = SMB_VFS_FSTAT(fsp, &sbuf);
783                 }
784                 if (ret == -1) {
785                         /* Lower level acl set succeeded,
786                          * so still return OK. */
787                         return NT_STATUS_OK;
788                 }
789                 create_file_sids(&sbuf, &owner_sid, &group_sid);
790                 /* This is safe as nc_psd is discarded at fn exit. */
791                 nc_psd->owner_sid = &owner_sid;
792                 nc_psd->group_sid = &group_sid;
793                 security_info_sent |= (OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION);
794                 psd = nc_psd;
795         }
796
797 #if 0
798         if ((security_info_sent & DACL_SECURITY_INFORMATION) &&
799                         psd->dacl != NULL &&
800                         (psd->type & (SE_DESC_DACL_AUTO_INHERITED|
801                                 SE_DESC_DACL_AUTO_INHERIT_REQ))==
802                                 (SE_DESC_DACL_AUTO_INHERITED|
803                                 SE_DESC_DACL_AUTO_INHERIT_REQ) ) {
804                 struct security_descriptor *new_psd = NULL;
805                 status = append_parent_acl(fsp, psd, &new_psd);
806                 if (!NT_STATUS_IS_OK(status)) {
807                         /* Lower level acl set succeeded,
808                          * so still return OK. */
809                         return NT_STATUS_OK;
810                 }
811                 psd = new_psd;
812         }
813 #endif
814
815         if (DEBUGLEVEL >= 10) {
816                 DEBUG(10,("fset_nt_acl_tdb: storing tdb sd for file %s\n",
817                         fsp->fsp_name));
818                 NDR_PRINT_DEBUG(security_descriptor,
819                         CONST_DISCARD(struct security_descriptor *,psd));
820         }
821         create_acl_blob(psd, &blob);
822         store_acl_blob_fsp(handle, fsp, &blob);
823
824         return NT_STATUS_OK;
825 }
826
827 /*******************************************************************
828  Handle opening the storage tdb if so configured.
829 *******************************************************************/
830
831 static int connect_acl_tdb(struct vfs_handle_struct *handle,
832                                 const char *service,
833                                 const char *user)
834 {
835         struct db_context *db;
836         int res;
837
838         res = SMB_VFS_NEXT_CONNECT(handle, service, user);
839         if (res < 0) {
840                 return res;
841         }
842
843         if (!acl_tdb_init(&db)) {
844                 SMB_VFS_NEXT_DISCONNECT(handle);
845                 return -1;
846         }
847
848         SMB_VFS_HANDLE_SET_DATA(handle, db, free_acl_tdb_data,
849                                 struct db_context, return -1);
850
851         return 0;
852 }
853
854 /*********************************************************************
855  Remove a Windows ACL - we're setting the underlying POSIX ACL.
856 *********************************************************************/
857
858 static int sys_acl_set_file_tdb(vfs_handle_struct *handle,
859                               const char *path,
860                               SMB_ACL_TYPE_T type,
861                               SMB_ACL_T theacl)
862 {
863         SMB_STRUCT_STAT sbuf;
864         struct db_context *db;
865         int ret = -1;
866
867         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
868
869         if (lp_posix_pathnames()) {
870                 ret = SMB_VFS_LSTAT(handle->conn, path, &sbuf);
871         } else {
872                 ret = SMB_VFS_STAT(handle->conn, path, &sbuf);
873         }
874
875         if (ret == -1) {
876                 return -1;
877         }
878
879         ret = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle,
880                                                 path,
881                                                 type,
882                                                 theacl);
883         if (ret == -1) {
884                 return -1;
885         }
886
887         acl_tdb_delete(handle, db, &sbuf);
888         return 0;
889 }
890
891 /*********************************************************************
892  Remove a Windows ACL - we're setting the underlying POSIX ACL.
893 *********************************************************************/
894
895 static int sys_acl_set_fd_tdb(vfs_handle_struct *handle,
896                             files_struct *fsp,
897                             SMB_ACL_T theacl)
898 {
899         SMB_STRUCT_STAT sbuf;
900         struct db_context *db;
901         int ret;
902
903         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
904
905         if (fsp->is_directory || fsp->fh->fd == -1) {
906                 if (fsp->posix_open) {
907                         ret = SMB_VFS_LSTAT(fsp->conn,fsp->fsp_name, &sbuf);
908                 } else {
909                         ret = SMB_VFS_STAT(fsp->conn,fsp->fsp_name, &sbuf);
910                 }
911         } else {
912                 ret = SMB_VFS_FSTAT(fsp, &sbuf);
913         }
914         if (ret == -1) {
915                 return -1;
916         }
917
918         ret = SMB_VFS_NEXT_SYS_ACL_SET_FD(handle,
919                                                 fsp,
920                                                 theacl);
921         if (ret == -1) {
922                 return -1;
923         }
924
925         acl_tdb_delete(handle, db, &sbuf);
926         return 0;
927 }
928
929 /* VFS operations structure */
930
931 static vfs_op_tuple skel_op_tuples[] =
932 {
933         {SMB_VFS_OP(connect_acl_tdb), SMB_VFS_OP_CONNECT,  SMB_VFS_LAYER_TRANSPARENT},
934
935         {SMB_VFS_OP(mkdir_acl_tdb), SMB_VFS_OP_MKDIR, SMB_VFS_LAYER_TRANSPARENT},
936         {SMB_VFS_OP(rmdir_acl_tdb), SMB_VFS_OP_RMDIR, SMB_VFS_LAYER_TRANSPARENT},
937
938         {SMB_VFS_OP(open_acl_tdb),  SMB_VFS_OP_OPEN,  SMB_VFS_LAYER_TRANSPARENT},
939         {SMB_VFS_OP(unlink_acl_tdb), SMB_VFS_OP_UNLINK, SMB_VFS_LAYER_TRANSPARENT},
940
941         /* NT File ACL operations */
942
943         {SMB_VFS_OP(fget_nt_acl_tdb),SMB_VFS_OP_FGET_NT_ACL,SMB_VFS_LAYER_TRANSPARENT},
944         {SMB_VFS_OP(get_nt_acl_tdb), SMB_VFS_OP_GET_NT_ACL, SMB_VFS_LAYER_TRANSPARENT},
945         {SMB_VFS_OP(fset_nt_acl_tdb),SMB_VFS_OP_FSET_NT_ACL,SMB_VFS_LAYER_TRANSPARENT},
946
947         /* POSIX ACL operations. */
948         {SMB_VFS_OP(sys_acl_set_file_tdb), SMB_VFS_OP_SYS_ACL_SET_FILE, SMB_VFS_LAYER_TRANSPARENT},
949         {SMB_VFS_OP(sys_acl_set_fd_tdb), SMB_VFS_OP_SYS_ACL_SET_FD, SMB_VFS_LAYER_TRANSPARENT},
950
951         {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
952 };
953
954 NTSTATUS vfs_acl_tdb_init(void)
955 {
956         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "acl_tdb", skel_op_tuples);
957 }