57fe73c814cfe41e2a4e5483f57ffe1c8e632adb
[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         push_file_id_16((char *)id_buf, id);
98         return db->fetch_locked(db,
99                                 mem_ctx,
100                                 make_tdb_data(id_buf,
101                                         sizeof(id_buf)));
102 }
103
104 /*******************************************************************
105  Delete the tdb acl record for a file
106 *******************************************************************/
107
108 static NTSTATUS acl_tdb_delete(vfs_handle_struct *handle,
109                                 struct db_context *db,
110                                 SMB_STRUCT_STAT *psbuf)
111 {
112         NTSTATUS status;
113         struct file_id id = vfs_file_id_from_sbuf(handle->conn, psbuf);
114         struct db_record *rec = acl_tdb_lock(talloc_tos(), db, &id);
115
116         /*
117          * If rec == NULL there's not much we can do about it
118          */
119
120         if (rec == NULL) {
121                 DEBUG(10,("acl_tdb_delete: rec == NULL\n"));
122                 TALLOC_FREE(rec);
123                 return NT_STATUS_OK;
124         }
125
126         status = rec->delete_rec(rec);
127         TALLOC_FREE(rec);
128         return status;
129 }
130
131 /*******************************************************************
132  Parse out a struct security_descriptor from a DATA_BLOB.
133 *******************************************************************/
134
135 static NTSTATUS parse_acl_blob(const DATA_BLOB *pblob,
136                                 uint32 security_info,
137                                 struct security_descriptor **ppdesc)
138 {
139         TALLOC_CTX *ctx = talloc_tos();
140         struct xattr_NTACL xacl;
141         enum ndr_err_code ndr_err;
142         size_t sd_size;
143
144         ndr_err = ndr_pull_struct_blob(pblob, ctx, NULL, &xacl,
145                         (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
146
147         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
148                 DEBUG(5, ("parse_acl_blob: ndr_pull_xattr_NTACL failed: %s\n",
149                         ndr_errstr(ndr_err)));
150                 return ndr_map_error2ntstatus(ndr_err);;
151         }
152
153         if (xacl.version != 2) {
154                 return NT_STATUS_REVISION_MISMATCH;
155         }
156
157         *ppdesc = make_sec_desc(ctx, SEC_DESC_REVISION, xacl.info.sd_hs->sd->type | SEC_DESC_SELF_RELATIVE,
158                         (security_info & OWNER_SECURITY_INFORMATION)
159                         ? xacl.info.sd_hs->sd->owner_sid : NULL,
160                         (security_info & GROUP_SECURITY_INFORMATION)
161                         ? xacl.info.sd_hs->sd->group_sid : NULL,
162                         (security_info & SACL_SECURITY_INFORMATION)
163                         ? xacl.info.sd_hs->sd->sacl : NULL,
164                         (security_info & DACL_SECURITY_INFORMATION)
165                         ? xacl.info.sd_hs->sd->dacl : NULL,
166                         &sd_size);
167
168         TALLOC_FREE(xacl.info.sd);
169
170         return (*ppdesc != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
171 }
172
173 /*******************************************************************
174  Pull a security descriptor into a DATA_BLOB from a tdb store.
175 *******************************************************************/
176
177 static NTSTATUS get_acl_blob(TALLOC_CTX *ctx,
178                         vfs_handle_struct *handle,
179                         files_struct *fsp,
180                         const char *name,
181                         DATA_BLOB *pblob)
182 {
183         uint8 id_buf[16];
184         TDB_DATA data;
185         struct file_id id;
186         struct db_context *db;
187         SMB_STRUCT_STAT sbuf;
188
189         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
190                 return NT_STATUS_INTERNAL_DB_CORRUPTION);
191
192         if (fsp && fsp->fh->fd != -1) {
193                 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
194                         return map_nt_error_from_unix(errno);
195                 }
196         } else {
197                 if (SMB_VFS_STAT(handle->conn, name, &sbuf) == -1) {
198                         return map_nt_error_from_unix(errno);
199                 }
200         }
201         id = vfs_file_id_from_sbuf(handle->conn, &sbuf);
202
203         push_file_id_16((char *)id_buf, &id);
204
205         if (db->fetch(db,
206                         ctx,
207                         make_tdb_data(id_buf, sizeof(id_buf)),
208                         &data) == -1) {
209                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
210         }
211
212         pblob->data = data.dptr;
213         pblob->length = data.dsize;
214
215         DEBUG(10,("get_acl_blob: returned %u bytes from file %s\n",
216                 (unsigned int)data.dsize, name ));
217
218         if (pblob->length == 0 || pblob->data == NULL) {
219                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
220         }
221         return NT_STATUS_OK;
222 }
223
224 /*******************************************************************
225  Create a DATA_BLOB from a security descriptor.
226 *******************************************************************/
227
228 static NTSTATUS create_acl_blob(const struct security_descriptor *psd, DATA_BLOB *pblob)
229 {
230         struct xattr_NTACL xacl;
231         struct security_descriptor_hash sd_hs;
232         enum ndr_err_code ndr_err;
233         TALLOC_CTX *ctx = talloc_tos();
234
235         ZERO_STRUCT(xacl);
236         ZERO_STRUCT(sd_hs);
237
238         xacl.version = 2;
239         xacl.info.sd_hs = &sd_hs;
240         xacl.info.sd_hs->sd = CONST_DISCARD(struct security_descriptor *, psd);
241         memset(&xacl.info.sd_hs->hash[0], '\0', 16);
242
243         ndr_err = ndr_push_struct_blob(
244                         pblob, ctx, NULL, &xacl,
245                         (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
246
247         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
248                 DEBUG(5, ("create_acl_blob: ndr_push_xattr_NTACL failed: %s\n",
249                         ndr_errstr(ndr_err)));
250                 return ndr_map_error2ntstatus(ndr_err);;
251         }
252
253         return NT_STATUS_OK;
254 }
255
256 /*******************************************************************
257  Store a DATA_BLOB into a tdb record given an fsp pointer.
258 *******************************************************************/
259
260 static NTSTATUS store_acl_blob_fsp(vfs_handle_struct *handle,
261                                 files_struct *fsp,
262                                 DATA_BLOB *pblob)
263 {
264         uint8 id_buf[16];
265         struct file_id id;
266         SMB_STRUCT_STAT sbuf;
267         TDB_DATA data;
268         struct db_context *db;
269         struct db_record *rec;
270
271         DEBUG(10,("store_acl_blob_fsp: storing blob length %u on file %s\n",
272                         (unsigned int)pblob->length, fsp->fsp_name));
273
274         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
275                 return NT_STATUS_INTERNAL_DB_CORRUPTION);
276
277         if (fsp->fh->fd != -1) {
278                 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
279                         return map_nt_error_from_unix(errno);
280                 }
281         } else {
282                 if (SMB_VFS_STAT(handle->conn, fsp->fsp_name, &sbuf) == -1) {
283                         return map_nt_error_from_unix(errno);
284                 }
285         }
286         id = vfs_file_id_from_sbuf(handle->conn, &sbuf);
287
288         push_file_id_16((char *)id_buf, &id);
289         rec = db->fetch_locked(db, talloc_tos(),
290                                 make_tdb_data(id_buf,
291                                         sizeof(id_buf)));
292         if (rec == NULL) {
293                 DEBUG(0, ("store_acl_blob_fsp_tdb: fetch_lock failed\n"));
294                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
295         }
296         data.dptr = pblob->data;
297         data.dsize = pblob->length;
298         return rec->store(rec, data, 0);
299 }
300
301 /*******************************************************************
302  Store a DATA_BLOB into a tdb record given a pathname.
303 *******************************************************************/
304
305 static NTSTATUS store_acl_blob_pathname(vfs_handle_struct *handle,
306                                         const char *fname,
307                                         DATA_BLOB *pblob)
308 {
309         uint8 id_buf[16];
310         struct file_id id;
311         TDB_DATA data;
312         SMB_STRUCT_STAT sbuf;
313         struct db_context *db;
314         struct db_record *rec;
315
316         DEBUG(10,("store_acl_blob_pathname: storing blob "
317                         "length %u on file %s\n",
318                         (unsigned int)pblob->length, fname));
319
320         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
321                 return NT_STATUS_INTERNAL_DB_CORRUPTION);
322
323         if (SMB_VFS_STAT(handle->conn, fname, &sbuf) == -1) {
324                 return map_nt_error_from_unix(errno);
325         }
326
327         id = vfs_file_id_from_sbuf(handle->conn, &sbuf);
328         push_file_id_16((char *)id_buf, &id);
329
330         rec = db->fetch_locked(db, talloc_tos(),
331                                 make_tdb_data(id_buf,
332                                         sizeof(id_buf)));
333         if (rec == NULL) {
334                 DEBUG(0, ("store_acl_blob_pathname_tdb: fetch_lock failed\n"));
335                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
336         }
337         data.dptr = pblob->data;
338         data.dsize = pblob->length;
339         return rec->store(rec, data, 0);
340 }
341
342 /*******************************************************************
343  Store a DATA_BLOB into an tdb given a pathname.
344 *******************************************************************/
345
346 static NTSTATUS get_nt_acl_tdb_internal(vfs_handle_struct *handle,
347                                         files_struct *fsp,
348                                         const char *name,
349                                         uint32 security_info,
350                                         struct security_descriptor **ppdesc)
351 {
352         TALLOC_CTX *ctx = talloc_tos();
353         DATA_BLOB blob;
354         NTSTATUS status;
355
356         if (fsp && name == NULL) {
357                 name = fsp->fsp_name;
358         }
359
360         DEBUG(10, ("get_nt_acl_tdb_internal: name=%s\n", name));
361
362         status = get_acl_blob(ctx, handle, fsp, name, &blob);
363         if (!NT_STATUS_IS_OK(status)) {
364                 DEBUG(10, ("get_acl_blob returned %s\n", nt_errstr(status)));
365                 return status;
366         }
367
368         status = parse_acl_blob(&blob, security_info, ppdesc);
369         if (!NT_STATUS_IS_OK(status)) {
370                 DEBUG(10, ("parse_acl_blob returned %s\n",
371                                 nt_errstr(status)));
372                 return status;
373         }
374
375         TALLOC_FREE(blob.data);
376         return status;
377 }
378
379 /*********************************************************************
380  Create a default security descriptor for a file in case no inheritance
381  exists. All permissions to the owner and SYSTEM.
382 *********************************************************************/
383
384 static struct security_descriptor *default_file_sd(TALLOC_CTX *mem_ctx,
385                                                 SMB_STRUCT_STAT *psbuf)
386 {
387         struct dom_sid owner_sid, group_sid;
388         size_t sd_size;
389         struct security_ace *pace = NULL;
390         struct security_acl *pacl = NULL;
391
392         uid_to_sid(&owner_sid, psbuf->st_uid);
393         gid_to_sid(&group_sid, psbuf->st_gid);
394
395         pace = TALLOC_ARRAY(mem_ctx, struct security_ace, 2);
396         if (!pace) {
397                 return NULL;
398         }
399
400         init_sec_ace(&pace[0], &owner_sid, SEC_ACE_TYPE_ACCESS_ALLOWED,
401                         SEC_RIGHTS_FILE_ALL, 0);
402         init_sec_ace(&pace[1], &global_sid_System, SEC_ACE_TYPE_ACCESS_ALLOWED,
403                         SEC_RIGHTS_FILE_ALL, 0);
404
405         pacl = make_sec_acl(mem_ctx,
406                                 NT4_ACL_REVISION,
407                                 2,
408                                 pace);
409         if (!pacl) {
410                 return NULL;
411         }
412         return make_sec_desc(mem_ctx,
413                         SECURITY_DESCRIPTOR_REVISION_1,
414                         SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
415                         &owner_sid,
416                         &group_sid,
417                         NULL,
418                         pacl,
419                         &sd_size);
420 }
421
422 /*********************************************************************
423 *********************************************************************/
424
425 static NTSTATUS inherit_new_acl(vfs_handle_struct *handle,
426                                         const char *fname,
427                                         files_struct *fsp,
428                                         bool container)
429 {
430         TALLOC_CTX *ctx = talloc_tos();
431         NTSTATUS status;
432         struct security_descriptor *parent_desc = NULL;
433         struct security_descriptor *psd = NULL;
434         DATA_BLOB blob;
435         size_t size;
436         char *parent_name;
437
438         if (!parent_dirname(ctx, fname, &parent_name, NULL)) {
439                 return NT_STATUS_NO_MEMORY;
440         }
441
442         DEBUG(10,("inherit_new_acl: check directory %s\n",
443                         parent_name));
444
445         status = get_nt_acl_tdb_internal(handle,
446                                         NULL,
447                                         parent_name,
448                                         (OWNER_SECURITY_INFORMATION |
449                                          GROUP_SECURITY_INFORMATION |
450                                          DACL_SECURITY_INFORMATION),
451                                         &parent_desc);
452         if (NT_STATUS_IS_OK(status)) {
453                 /* Create an inherited descriptor from the parent. */
454
455                 if (DEBUGLEVEL >= 10) {
456                         DEBUG(10,("inherit_new_acl: parent acl is:\n"));
457                         NDR_PRINT_DEBUG(security_descriptor, parent_desc);
458                 }
459
460                 status = se_create_child_secdesc(ctx,
461                                 &psd,
462                                 &size,
463                                 parent_desc,
464                                 &handle->conn->server_info->ptok->user_sids[PRIMARY_USER_SID_INDEX],
465                                 &handle->conn->server_info->ptok->user_sids[PRIMARY_GROUP_SID_INDEX],
466                                 container);
467                 if (!NT_STATUS_IS_OK(status)) {
468                         return status;
469                 }
470
471                 if (DEBUGLEVEL >= 10) {
472                         DEBUG(10,("inherit_new_acl: child acl is:\n"));
473                         NDR_PRINT_DEBUG(security_descriptor, psd);
474                 }
475
476         } else {
477                 DEBUG(10,("inherit_new_acl: directory %s failed "
478                         "to get acl %s\n",
479                         parent_name,
480                         nt_errstr(status) ));
481         }
482
483         if (!psd || psd->dacl == NULL) {
484                 SMB_STRUCT_STAT sbuf;
485                 int ret;
486
487                 TALLOC_FREE(psd);
488                 if (fsp && !fsp->is_directory && fsp->fh->fd != -1) {
489                         ret = SMB_VFS_FSTAT(fsp, &sbuf);
490                 } else {
491                         ret = SMB_VFS_STAT(handle->conn,fname, &sbuf);
492                 }
493                 if (ret == -1) {
494                         return map_nt_error_from_unix(errno);
495                 }
496                 psd = default_file_sd(ctx, &sbuf);
497                 if (!psd) {
498                         return NT_STATUS_NO_MEMORY;
499                 }
500
501                 if (DEBUGLEVEL >= 10) {
502                         DEBUG(10,("inherit_new_acl: default acl is:\n"));
503                         NDR_PRINT_DEBUG(security_descriptor, psd);
504                 }
505         }
506
507         status = create_acl_blob(psd, &blob);
508         if (!NT_STATUS_IS_OK(status)) {
509                 return status;
510         }
511         if (fsp) {
512                 return store_acl_blob_fsp(handle, fsp, &blob);
513         } else {
514                 return store_acl_blob_pathname(handle, fname, &blob);
515         }
516 }
517
518 /*********************************************************************
519  Check ACL on open. For new files inherit from parent directory.
520 *********************************************************************/
521
522 static int open_acl_tdb(vfs_handle_struct *handle,
523                                         const char *fname,
524                                         files_struct *fsp,
525                                         int flags,
526                                         mode_t mode)
527 {
528         uint32_t access_granted = 0;
529         struct security_descriptor *pdesc = NULL;
530         bool file_existed = true;
531         NTSTATUS status = get_nt_acl_tdb_internal(handle,
532                                         NULL,
533                                         fname,
534                                         (OWNER_SECURITY_INFORMATION |
535                                          GROUP_SECURITY_INFORMATION |
536                                          DACL_SECURITY_INFORMATION),
537                                         &pdesc);
538         if (NT_STATUS_IS_OK(status)) {
539                 /* See if we can access it. */
540                 status = smb1_file_se_access_check(pdesc,
541                                         handle->conn->server_info->ptok,
542                                         fsp->access_mask,
543                                         &access_granted);
544                 if (!NT_STATUS_IS_OK(status)) {
545                         DEBUG(10,("open_acl_tdb: file %s open "
546                                 "refused with error %s\n",
547                                 fname,
548                                 nt_errstr(status) ));
549                         errno = map_errno_from_nt_status(status);
550                         return -1;
551                 }
552         } else if (NT_STATUS_EQUAL(status,NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
553                 file_existed = false;
554         }
555
556         DEBUG(10,("open_acl_tdb: get_nt_acl_attr_internal for "
557                 "file %s returned %s\n",
558                 fname,
559                 nt_errstr(status) ));
560
561         fsp->fh->fd = SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
562
563         if (!file_existed && fsp->fh->fd != -1) {
564                 /* File was created. Inherit from parent directory. */
565                 string_set(&fsp->fsp_name, fname);
566                 inherit_new_acl(handle, fname, fsp, false);
567         }
568
569         return fsp->fh->fd;
570 }
571
572 /*********************************************************************
573  On unlink we need to delete the tdb record (if using tdb).
574 *********************************************************************/
575
576 static int unlink_acl_tdb(vfs_handle_struct *handle, const char *path)
577 {
578         SMB_STRUCT_STAT sbuf;
579         struct db_context *db;
580         int ret;
581
582         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
583
584         if (SMB_VFS_STAT(handle->conn, path, &sbuf) == -1) {
585                 return -1;
586         }
587
588         ret = SMB_VFS_NEXT_UNLINK(handle, path);
589
590         if (ret == -1) {
591                 return -1;
592         }
593
594         acl_tdb_delete(handle, db, &sbuf);
595         return 0;
596 }
597
598 /*********************************************************************
599  Store an inherited SD on mkdir.
600 *********************************************************************/
601
602 static int mkdir_acl_tdb(vfs_handle_struct *handle, const char *path, mode_t mode)
603 {
604         int ret = SMB_VFS_NEXT_MKDIR(handle, path, mode);
605
606         if (ret == -1) {
607                 return ret;
608         }
609         /* New directory - inherit from parent. */
610         inherit_new_acl(handle, path, NULL, true);
611         return ret;
612 }
613
614 /*********************************************************************
615  On rmdir we need to delete the tdb record (if using tdb).
616 *********************************************************************/
617
618 static int rmdir_acl_tdb(vfs_handle_struct *handle, const char *path)
619 {
620
621         SMB_STRUCT_STAT sbuf;
622         struct db_context *db;
623         int ret;
624
625         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
626
627         if (SMB_VFS_STAT(handle->conn, path, &sbuf) == -1) {
628                 return -1;
629         }
630
631         ret = SMB_VFS_NEXT_RMDIR(handle, path);
632         if (ret == -1) {
633                 return -1;
634         }
635
636         acl_tdb_delete(handle, db, &sbuf);
637         return 0;
638 }
639
640 /*********************************************************************
641  Fetch a security descriptor given an fsp.
642 *********************************************************************/
643
644 static NTSTATUS fget_nt_acl_tdb(vfs_handle_struct *handle, files_struct *fsp,
645         uint32 security_info, struct security_descriptor **ppdesc)
646 {
647         NTSTATUS status = get_nt_acl_tdb_internal(handle, fsp,
648                                 NULL, security_info, ppdesc);
649         if (NT_STATUS_IS_OK(status)) {
650                 if (DEBUGLEVEL >= 10) {
651                         DEBUG(10,("fget_nt_acl_tdb: returning tdb sd for file %s\n",
652                                 fsp->fsp_name));
653                         NDR_PRINT_DEBUG(security_descriptor, *ppdesc);
654                 }
655                 return NT_STATUS_OK;
656         }
657
658         DEBUG(10,("fget_nt_acl_tdb: failed to get tdb sd for file %s, Error %s\n",
659                         fsp->fsp_name,
660                         nt_errstr(status) ));
661
662         return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp,
663                         security_info, ppdesc);
664 }
665
666 /*********************************************************************
667  Fetch a security descriptor given a pathname.
668 *********************************************************************/
669
670 static NTSTATUS get_nt_acl_tdb(vfs_handle_struct *handle,
671         const char *name, uint32 security_info, struct security_descriptor **ppdesc)
672 {
673         NTSTATUS status = get_nt_acl_tdb_internal(handle, NULL,
674                                 name, security_info, ppdesc);
675         if (NT_STATUS_IS_OK(status)) {
676                 if (DEBUGLEVEL >= 10) {
677                         DEBUG(10,("get_nt_acl_tdb: returning tdb sd for file %s\n",
678                                 name));
679                         NDR_PRINT_DEBUG(security_descriptor, *ppdesc);
680                 }
681                 return NT_STATUS_OK;
682         }
683
684         DEBUG(10,("get_nt_acl_tdb: failed to get tdb sd for file %s, Error %s\n",
685                         name,
686                         nt_errstr(status) ));
687
688         return SMB_VFS_NEXT_GET_NT_ACL(handle, name,
689                         security_info, ppdesc);
690 }
691
692 /*********************************************************************
693  Store a security descriptor given an fsp.
694 *********************************************************************/
695
696 static NTSTATUS fset_nt_acl_tdb(vfs_handle_struct *handle, files_struct *fsp,
697         uint32 security_info_sent, const struct security_descriptor *psd)
698 {
699         NTSTATUS status;
700         DATA_BLOB blob;
701
702         if (DEBUGLEVEL >= 10) {
703                 DEBUG(10,("fset_nt_acl_tdb: incoming sd for file %s\n",
704                         fsp->fsp_name));
705                 NDR_PRINT_DEBUG(security_descriptor,
706                         CONST_DISCARD(struct security_descriptor *,psd));
707         }
708
709         status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
710         if (!NT_STATUS_IS_OK(status)) {
711                 return status;
712         }
713
714         /* Ensure owner and group are set. */
715         if (!psd->owner_sid || !psd->group_sid) {
716                 int ret;
717                 SMB_STRUCT_STAT sbuf;
718                 DOM_SID owner_sid, group_sid;
719                 struct security_descriptor *nc_psd = dup_sec_desc(talloc_tos(), psd);
720
721                 if (!nc_psd) {
722                         return NT_STATUS_OK;
723                 }
724                 if (fsp->is_directory || fsp->fh->fd == -1) {
725                         ret = SMB_VFS_STAT(fsp->conn,fsp->fsp_name, &sbuf);
726                 } else {
727                         ret = SMB_VFS_FSTAT(fsp, &sbuf);
728                 }
729                 if (ret == -1) {
730                         /* Lower level acl set succeeded,
731                          * so still return OK. */
732                         return NT_STATUS_OK;
733                 }
734                 create_file_sids(&sbuf, &owner_sid, &group_sid);
735                 /* This is safe as nc_psd is discarded at fn exit. */
736                 nc_psd->owner_sid = &owner_sid;
737                 nc_psd->group_sid = &group_sid;
738                 security_info_sent |= (OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION);
739                 psd = nc_psd;
740         }
741
742 #if 0
743         if ((security_info_sent & DACL_SECURITY_INFORMATION) &&
744                         psd->dacl != NULL &&
745                         (psd->type & (SE_DESC_DACL_AUTO_INHERITED|
746                                 SE_DESC_DACL_AUTO_INHERIT_REQ))==
747                                 (SE_DESC_DACL_AUTO_INHERITED|
748                                 SE_DESC_DACL_AUTO_INHERIT_REQ) ) {
749                 struct security_descriptor *new_psd = NULL;
750                 status = append_parent_acl(fsp, psd, &new_psd);
751                 if (!NT_STATUS_IS_OK(status)) {
752                         /* Lower level acl set succeeded,
753                          * so still return OK. */
754                         return NT_STATUS_OK;
755                 }
756                 psd = new_psd;
757         }
758 #endif
759
760         if (DEBUGLEVEL >= 10) {
761                 DEBUG(10,("fset_nt_acl_tdb: storing tdb sd for file %s\n",
762                         fsp->fsp_name));
763                 NDR_PRINT_DEBUG(security_descriptor,
764                         CONST_DISCARD(struct security_descriptor *,psd));
765         }
766         create_acl_blob(psd, &blob);
767         store_acl_blob_fsp(handle, fsp, &blob);
768
769         return NT_STATUS_OK;
770 }
771
772 /*******************************************************************
773  Handle opening the storage tdb if so configured.
774 *******************************************************************/
775
776 static int connect_acl_tdb(struct vfs_handle_struct *handle,
777                                 const char *service,
778                                 const char *user)
779 {
780         struct db_context *db;
781         int res;
782
783         res = SMB_VFS_NEXT_CONNECT(handle, service, user);
784         if (res < 0) {
785                 return res;
786         }
787
788         if (!acl_tdb_init(&db)) {
789                 SMB_VFS_NEXT_DISCONNECT(handle);
790                 return -1;
791         }
792
793         SMB_VFS_HANDLE_SET_DATA(handle, db, free_acl_tdb_data,
794                                 struct db_context, return -1);
795
796         return 0;
797 }
798
799 /*********************************************************************
800  Remove a Windows ACL - we're setting the underlying POSIX ACL.
801 *********************************************************************/
802
803 static int sys_acl_set_file_tdb(vfs_handle_struct *handle,
804                               const char *path,
805                               SMB_ACL_TYPE_T type,
806                               SMB_ACL_T theacl)
807 {
808         SMB_STRUCT_STAT sbuf;
809         struct db_context *db;
810         int ret;
811
812         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
813
814         if (SMB_VFS_STAT(handle->conn, path, &sbuf) == -1) {
815                 return -1;
816         }
817
818         ret = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle,
819                                                 path,
820                                                 type,
821                                                 theacl);
822         if (ret == -1) {
823                 return -1;
824         }
825
826         acl_tdb_delete(handle, db, &sbuf);
827         return 0;
828 }
829
830 /*********************************************************************
831  Remove a Windows ACL - we're setting the underlying POSIX ACL.
832 *********************************************************************/
833
834 static int sys_acl_set_fd_tdb(vfs_handle_struct *handle,
835                             files_struct *fsp,
836                             SMB_ACL_T theacl)
837 {
838         SMB_STRUCT_STAT sbuf;
839         struct db_context *db;
840         int ret;
841
842         SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
843
844         if (fsp->is_directory || fsp->fh->fd == -1) {
845                 ret = SMB_VFS_STAT(fsp->conn,fsp->fsp_name, &sbuf);
846         } else {
847                 ret = SMB_VFS_FSTAT(fsp, &sbuf);
848         }
849         if (ret == -1) {
850                 return -1;
851         }
852
853         ret = SMB_VFS_NEXT_SYS_ACL_SET_FD(handle,
854                                                 fsp,
855                                                 theacl);
856         if (ret == -1) {
857                 return -1;
858         }
859
860         acl_tdb_delete(handle, db, &sbuf);
861         return 0;
862 }
863
864 /* VFS operations structure */
865
866 static vfs_op_tuple skel_op_tuples[] =
867 {
868         {SMB_VFS_OP(connect_acl_tdb), SMB_VFS_OP_CONNECT,  SMB_VFS_LAYER_TRANSPARENT},
869
870         {SMB_VFS_OP(mkdir_acl_tdb), SMB_VFS_OP_MKDIR, SMB_VFS_LAYER_TRANSPARENT},
871         {SMB_VFS_OP(rmdir_acl_tdb), SMB_VFS_OP_RMDIR, SMB_VFS_LAYER_TRANSPARENT},
872
873         {SMB_VFS_OP(open_acl_tdb),  SMB_VFS_OP_OPEN,  SMB_VFS_LAYER_TRANSPARENT},
874         {SMB_VFS_OP(unlink_acl_tdb), SMB_VFS_OP_UNLINK, SMB_VFS_LAYER_TRANSPARENT},
875
876         /* NT File ACL operations */
877
878         {SMB_VFS_OP(fget_nt_acl_tdb),SMB_VFS_OP_FGET_NT_ACL,SMB_VFS_LAYER_TRANSPARENT},
879         {SMB_VFS_OP(get_nt_acl_tdb), SMB_VFS_OP_GET_NT_ACL, SMB_VFS_LAYER_TRANSPARENT},
880         {SMB_VFS_OP(fset_nt_acl_tdb),SMB_VFS_OP_FSET_NT_ACL,SMB_VFS_LAYER_TRANSPARENT},
881
882         /* POSIX ACL operations. */
883         {SMB_VFS_OP(sys_acl_set_file_tdb), SMB_VFS_OP_SYS_ACL_SET_FILE, SMB_VFS_LAYER_TRANSPARENT},
884         {SMB_VFS_OP(sys_acl_set_fd_tdb), SMB_VFS_OP_SYS_ACL_SET_FD, SMB_VFS_LAYER_TRANSPARENT},
885
886         {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
887 };
888
889 NTSTATUS vfs_acl_tdb_init(void)
890 {
891         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "acl_tdb", skel_op_tuples);
892 }