idl: rebuild drsuapi.idl
[Samba/aatanasov.git] / source3 / modules / vfs_acl_tdb.c
blob285c58ed9d19f00731d1d11f7c6400963912c6ff
1 /*
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. */
23 #include "includes.h"
24 #include "librpc/gen_ndr/xattr.h"
25 #include "librpc/gen_ndr/ndr_xattr.h"
26 #include "../lib/crypto/crypto.h"
28 #undef DBGC_CLASS
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)
42 char *dbname;
44 if (acl_db) {
45 *pp_db = acl_db;
46 ref_count++;
47 return true;
50 dbname = state_path("file_ntacls.tdb");
52 if (dbname == NULL) {
53 errno = ENOSYS;
54 return false;
57 become_root();
58 *pp_db = db_open(NULL, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
59 unbecome_root();
61 if (*pp_db == NULL) {
62 #if defined(ENOTSUP)
63 errno = ENOTSUP;
64 #else
65 errno = ENOSYS;
66 #endif
67 TALLOC_FREE(dbname);
68 return false;
71 ref_count++;
72 TALLOC_FREE(dbname);
73 return true;
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;
84 ref_count--;
85 if (ref_count == 0) {
86 TALLOC_FREE(*pp_db);
87 acl_db = NULL;
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)
99 uint8 id_buf[16];
101 /* For backwards compatibility only store the dev/inode. */
102 push_file_id_16((char *)id_buf, id);
103 return db->fetch_locked(db,
104 mem_ctx,
105 make_tdb_data(id_buf,
106 sizeof(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)
117 NTSTATUS status;
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
125 if (rec == NULL) {
126 DEBUG(10,("acl_tdb_delete: rec == NULL\n"));
127 TALLOC_FREE(rec);
128 return NT_STATUS_OK;
131 status = rec->delete_rec(rec);
132 TALLOC_FREE(rec);
133 return status;
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,
142 files_struct *fsp,
143 const char *name,
144 DATA_BLOB *pblob)
146 uint8 id_buf[16];
147 TDB_DATA data;
148 struct file_id id;
149 struct db_context *db;
150 int ret = -1;
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);
158 } else {
159 if (fsp && fsp->posix_open) {
160 ret = vfs_lstat_smb_fname(handle->conn, name, &sbuf);
161 } else {
162 ret = vfs_stat_smb_fname(handle->conn, name, &sbuf);
166 if (ret == -1) {
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);
175 if (db->fetch(db,
176 ctx,
177 make_tdb_data(id_buf, sizeof(id_buf)),
178 &data) == -1) {
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;
191 return NT_STATUS_OK;
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,
199 files_struct *fsp,
200 DATA_BLOB *pblob)
202 uint8 id_buf[16];
203 struct file_id id;
204 TDB_DATA data;
205 struct db_context *db;
206 struct db_record *rec;
207 int ret = -1;
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);
217 } else {
218 if (fsp->posix_open) {
219 ret = SMB_VFS_LSTAT(handle->conn, fsp->fsp_name);
220 } else {
221 ret = SMB_VFS_STAT(handle->conn, fsp->fsp_name);
225 if (ret == -1) {
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,
235 sizeof(id_buf)));
236 if (rec == NULL) {
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,
250 const char *fname,
251 DATA_BLOB *pblob)
253 uint8 id_buf[16];
254 struct file_id id;
255 TDB_DATA data;
256 SMB_STRUCT_STAT sbuf;
257 struct db_context *db;
258 struct db_record *rec;
259 int ret = -1;
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);
270 } else {
271 ret = vfs_stat_smb_fname(handle->conn, fname, &sbuf);
274 if (ret == -1) {
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,
285 sizeof(id_buf)));
286 if (rec == NULL) {
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;
304 NTSTATUS status;
305 int ret = -1;
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);
312 goto out;
315 if (lp_posix_pathnames()) {
316 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_tmp);
317 } else {
318 ret = SMB_VFS_STAT(handle->conn, smb_fname_tmp);
321 if (ret == -1) {
322 goto out;
325 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
327 if (ret == -1) {
328 goto out;
331 acl_tdb_delete(handle, db, &smb_fname_tmp->st);
332 out:
333 return ret;
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;
345 int ret = -1;
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);
351 } else {
352 ret = vfs_stat_smb_fname(handle->conn, path, &sbuf);
355 if (ret == -1) {
356 return -1;
359 ret = SMB_VFS_NEXT_RMDIR(handle, path);
360 if (ret == -1) {
361 return -1;
364 acl_tdb_delete(handle, db, &sbuf);
365 return 0;
368 /*******************************************************************
369 Handle opening the storage tdb if so configured.
370 *******************************************************************/
372 static int connect_acl_tdb(struct vfs_handle_struct *handle,
373 const char *service,
374 const char *user)
376 struct db_context *db;
377 int res;
379 res = SMB_VFS_NEXT_CONNECT(handle, service, user);
380 if (res < 0) {
381 return res;
384 if (!acl_tdb_init(&db)) {
385 SMB_VFS_NEXT_DISCONNECT(handle);
386 return -1;
389 SMB_VFS_HANDLE_SET_DATA(handle, db, free_acl_tdb_data,
390 struct db_context, return -1);
392 return 0;
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,
400 const char *path,
401 SMB_ACL_TYPE_T type,
402 SMB_ACL_T theacl)
404 SMB_STRUCT_STAT sbuf;
405 struct db_context *db;
406 int ret = -1;
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);
412 } else {
413 ret = vfs_stat_smb_fname(handle->conn, path, &sbuf);
416 if (ret == -1) {
417 return -1;
420 ret = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle,
421 path,
422 type,
423 theacl);
424 if (ret == -1) {
425 return -1;
428 acl_tdb_delete(handle, db, &sbuf);
429 return 0;
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,
437 files_struct *fsp,
438 SMB_ACL_T theacl)
440 struct db_context *db;
441 int ret;
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);
448 } else {
449 ret = SMB_VFS_STAT(fsp->conn, fsp->fsp_name);
451 } else {
452 ret = SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);
454 if (ret == -1) {
455 return -1;
458 ret = SMB_VFS_NEXT_SYS_ACL_SET_FD(handle,
459 fsp,
460 theacl);
461 if (ret == -1) {
462 return -1;
465 acl_tdb_delete(handle, db, &fsp->fsp_name->st);
466 return 0;
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",
485 &vfs_acl_tdb_fns);