2 * Store Windows ACLs in a tdb.
4 * Copyright (C) Volker Lendecke, 2008
5 * Copyright (C) Jeremy Allison, 2008
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.
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.
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/>.
21 /* NOTE: This is an experimental module, not yet finished. JRA. */
24 #include "librpc/gen_ndr/xattr.h"
25 #include "librpc/gen_ndr/ndr_xattr.h"
26 #include "../lib/crypto/crypto.h"
29 #define DBGC_CLASS DBGC_VFS
31 #include "modules/vfs_acl_common.c"
33 static unsigned int ref_count;
34 static struct db_context *acl_db;
36 /*******************************************************************
37 Open acl_db if not already open, increment ref count.
38 *******************************************************************/
40 static bool acl_tdb_init(struct db_context **pp_db)
50 dbname = state_path("file_ntacls.tdb");
58 *pp_db = db_open(NULL, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
76 /*******************************************************************
77 Lower ref count and close acl_db if zero.
78 *******************************************************************/
80 static void free_acl_tdb_data(void **pptr)
82 struct db_context **pp_db = (struct db_context **)pptr;
91 /*******************************************************************
92 Fetch_lock the tdb acl record for a file
93 *******************************************************************/
95 static struct db_record *acl_tdb_lock(TALLOC_CTX *mem_ctx,
96 struct db_context *db,
97 const struct file_id *id)
101 /* For backwards compatibility only store the dev/inode. */
102 push_file_id_16((char *)id_buf, id);
103 return db->fetch_locked(db,
105 make_tdb_data(id_buf,
109 /*******************************************************************
110 Delete the tdb acl record for a file
111 *******************************************************************/
113 static NTSTATUS acl_tdb_delete(vfs_handle_struct *handle,
114 struct db_context *db,
115 SMB_STRUCT_STAT *psbuf)
118 struct file_id id = vfs_file_id_from_sbuf(handle->conn, psbuf);
119 struct db_record *rec = acl_tdb_lock(talloc_tos(), db, &id);
122 * If rec == NULL there's not much we can do about it
126 DEBUG(10,("acl_tdb_delete: rec == NULL\n"));
131 status = rec->delete_rec(rec);
136 /*******************************************************************
137 Pull a security descriptor into a DATA_BLOB from a tdb store.
138 *******************************************************************/
140 static NTSTATUS get_acl_blob(TALLOC_CTX *ctx,
141 vfs_handle_struct *handle,
149 struct db_context *db;
151 SMB_STRUCT_STAT sbuf;
153 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
154 return NT_STATUS_INTERNAL_DB_CORRUPTION);
156 if (fsp && fsp->fh->fd != -1) {
157 ret = SMB_VFS_FSTAT(fsp, &sbuf);
159 if (fsp && fsp->posix_open) {
160 ret = vfs_lstat_smb_fname(handle->conn, name, &sbuf);
162 ret = vfs_stat_smb_fname(handle->conn, name, &sbuf);
167 return map_nt_error_from_unix(errno);
170 id = vfs_file_id_from_sbuf(handle->conn, &sbuf);
172 /* For backwards compatibility only store the dev/inode. */
173 push_file_id_16((char *)id_buf, &id);
177 make_tdb_data(id_buf, sizeof(id_buf)),
179 return NT_STATUS_INTERNAL_DB_CORRUPTION;
182 pblob->data = data.dptr;
183 pblob->length = data.dsize;
185 DEBUG(10,("get_acl_blob: returned %u bytes from file %s\n",
186 (unsigned int)data.dsize, name ));
188 if (pblob->length == 0 || pblob->data == NULL) {
189 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
194 /*******************************************************************
195 Store a DATA_BLOB into a tdb record given an fsp pointer.
196 *******************************************************************/
198 static NTSTATUS store_acl_blob_fsp(vfs_handle_struct *handle,
205 struct db_context *db;
206 struct db_record *rec;
209 DEBUG(10,("store_acl_blob_fsp: storing blob length %u on file %s\n",
210 (unsigned int)pblob->length, fsp_str_dbg(fsp)));
212 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
213 return NT_STATUS_INTERNAL_DB_CORRUPTION);
215 if (fsp->fh->fd != -1) {
216 ret = SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);
218 if (fsp->posix_open) {
219 ret = SMB_VFS_LSTAT(handle->conn, fsp->fsp_name);
221 ret = SMB_VFS_STAT(handle->conn, fsp->fsp_name);
226 return map_nt_error_from_unix(errno);
229 id = vfs_file_id_from_sbuf(handle->conn, &fsp->fsp_name->st);
231 /* For backwards compatibility only store the dev/inode. */
232 push_file_id_16((char *)id_buf, &id);
233 rec = db->fetch_locked(db, talloc_tos(),
234 make_tdb_data(id_buf,
237 DEBUG(0, ("store_acl_blob_fsp_tdb: fetch_lock failed\n"));
238 return NT_STATUS_INTERNAL_DB_CORRUPTION;
240 data.dptr = pblob->data;
241 data.dsize = pblob->length;
242 return rec->store(rec, data, 0);
245 /*******************************************************************
246 Store a DATA_BLOB into a tdb record given a pathname.
247 *******************************************************************/
249 static NTSTATUS store_acl_blob_pathname(vfs_handle_struct *handle,
256 SMB_STRUCT_STAT sbuf;
257 struct db_context *db;
258 struct db_record *rec;
261 DEBUG(10,("store_acl_blob_pathname: storing blob "
262 "length %u on file %s\n",
263 (unsigned int)pblob->length, fname));
265 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
266 return NT_STATUS_INTERNAL_DB_CORRUPTION);
268 if (lp_posix_pathnames()) {
269 ret = vfs_lstat_smb_fname(handle->conn, fname, &sbuf);
271 ret = vfs_stat_smb_fname(handle->conn, fname, &sbuf);
275 return map_nt_error_from_unix(errno);
278 id = vfs_file_id_from_sbuf(handle->conn, &sbuf);
280 /* For backwards compatibility only store the dev/inode. */
281 push_file_id_16((char *)id_buf, &id);
283 rec = db->fetch_locked(db, talloc_tos(),
284 make_tdb_data(id_buf,
287 DEBUG(0, ("store_acl_blob_pathname_tdb: fetch_lock failed\n"));
288 return NT_STATUS_INTERNAL_DB_CORRUPTION;
290 data.dptr = pblob->data;
291 data.dsize = pblob->length;
292 return rec->store(rec, data, 0);
295 /*********************************************************************
296 On unlink we need to delete the tdb record (if using tdb).
297 *********************************************************************/
299 static int unlink_acl_tdb(vfs_handle_struct *handle,
300 const struct smb_filename *smb_fname)
302 struct smb_filename *smb_fname_tmp = NULL;
303 struct db_context *db;
307 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
309 status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp);
310 if (!NT_STATUS_IS_OK(status)) {
311 errno = map_errno_from_nt_status(status);
315 if (lp_posix_pathnames()) {
316 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_tmp);
318 ret = SMB_VFS_STAT(handle->conn, smb_fname_tmp);
325 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
331 acl_tdb_delete(handle, db, &smb_fname_tmp->st);
336 /*********************************************************************
337 On rmdir we need to delete the tdb record (if using tdb).
338 *********************************************************************/
340 static int rmdir_acl_tdb(vfs_handle_struct *handle, const char *path)
343 SMB_STRUCT_STAT sbuf;
344 struct db_context *db;
347 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
349 if (lp_posix_pathnames()) {
350 ret = vfs_lstat_smb_fname(handle->conn, path, &sbuf);
352 ret = vfs_stat_smb_fname(handle->conn, path, &sbuf);
359 ret = SMB_VFS_NEXT_RMDIR(handle, path);
364 acl_tdb_delete(handle, db, &sbuf);
368 /*******************************************************************
369 Handle opening the storage tdb if so configured.
370 *******************************************************************/
372 static int connect_acl_tdb(struct vfs_handle_struct *handle,
376 struct db_context *db;
379 res = SMB_VFS_NEXT_CONNECT(handle, service, user);
384 if (!acl_tdb_init(&db)) {
385 SMB_VFS_NEXT_DISCONNECT(handle);
389 SMB_VFS_HANDLE_SET_DATA(handle, db, free_acl_tdb_data,
390 struct db_context, return -1);
395 /*********************************************************************
396 Remove a Windows ACL - we're setting the underlying POSIX ACL.
397 *********************************************************************/
399 static int sys_acl_set_file_tdb(vfs_handle_struct *handle,
404 SMB_STRUCT_STAT sbuf;
405 struct db_context *db;
408 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
410 if (lp_posix_pathnames()) {
411 ret = vfs_lstat_smb_fname(handle->conn, path, &sbuf);
413 ret = vfs_stat_smb_fname(handle->conn, path, &sbuf);
420 ret = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle,
428 acl_tdb_delete(handle, db, &sbuf);
432 /*********************************************************************
433 Remove a Windows ACL - we're setting the underlying POSIX ACL.
434 *********************************************************************/
436 static int sys_acl_set_fd_tdb(vfs_handle_struct *handle,
440 struct db_context *db;
443 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
445 if (fsp->is_directory || fsp->fh->fd == -1) {
446 if (fsp->posix_open) {
447 ret = SMB_VFS_LSTAT(fsp->conn, fsp->fsp_name);
449 ret = SMB_VFS_STAT(fsp->conn, fsp->fsp_name);
452 ret = SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);
458 ret = SMB_VFS_NEXT_SYS_ACL_SET_FD(handle,
465 acl_tdb_delete(handle, db, &fsp->fsp_name->st);
469 static struct vfs_fn_pointers vfs_acl_tdb_fns = {
470 .connect_fn = connect_acl_tdb,
471 .mkdir = mkdir_acl_common,
472 .open = open_acl_common,
473 .unlink = unlink_acl_tdb,
474 .rmdir = rmdir_acl_tdb,
475 .fget_nt_acl = fget_nt_acl_common,
476 .get_nt_acl = get_nt_acl_common,
477 .fset_nt_acl = fset_nt_acl_common,
478 .sys_acl_set_file = sys_acl_set_file_tdb,
479 .sys_acl_set_fd = sys_acl_set_fd_tdb
482 NTSTATUS vfs_acl_tdb_init(void)
484 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "acl_tdb",