2 * Store posix-level xattrs in a tdb (posix:eadb format)
4 * Copyright (C) Andrew Bartlett, 2011
6 * Based on vfs_xattr_tdb by
7 * Copyright (C) Volker Lendecke, 2007
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "system/filesys.h"
25 #include "smbd/smbd.h"
26 #include "librpc/gen_ndr/xattr.h"
27 #include "librpc/gen_ndr/ndr_xattr.h"
28 #include "../librpc/gen_ndr/ndr_netlogon.h"
30 #include "lib/tdb_wrap/tdb_wrap.h"
31 #include "ntvfs/posix/posix_eadb.h"
32 #include "param/param.h"
33 #include "lib/param/loadparm.h"
36 #define DBGC_CLASS DBGC_VFS
39 * Worker routine for getxattr and fgetxattr
42 static ssize_t posix_eadb_getattr(struct tdb_wrap *db_ctx,
43 const char *fname, int fd,
44 const char *name, void *value, size_t size)
50 DEBUG(10, ("posix_eadb_getattr called for file %s/fd %d, name %s\n",
53 status = pull_xattr_blob_tdb_raw(db_ctx, talloc_tos(), name, fname, fd, size, &blob);
55 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
60 if (!NT_STATUS_IS_OK(status)) {
61 DEBUG(10, ("posix_eadb_fetch_attrs failed: %s\n",
67 if (blob.length > size) {
72 memcpy(value, blob.data, blob.length);
79 static ssize_t posix_eadb_getxattr(struct vfs_handle_struct *handle,
80 const struct smb_filename *smb_fname,
87 SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
89 return posix_eadb_getattr(db, smb_fname->base_name,
90 -1, name, value, size);
93 static ssize_t posix_eadb_fgetxattr(struct vfs_handle_struct *handle,
94 struct files_struct *fsp,
95 const char *name, void *value, size_t size)
99 SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
101 return posix_eadb_getattr(db, fsp->fsp_name->base_name, fsp->fh->fd, name, value, size);
105 * Worker routine for setxattr and fsetxattr
108 static int posix_eadb_setattr(struct tdb_wrap *db_ctx,
109 const char *fname, int fd, const char *name,
110 const void *value, size_t size, int flags)
113 DATA_BLOB data = data_blob_const(value, size);
115 DEBUG(10, ("posix_eadb_setattr called for file %s/fd %d, name %s\n",
118 status = push_xattr_blob_tdb_raw(db_ctx, name, fname, fd, &data);
120 if (!NT_STATUS_IS_OK(status)) {
121 DEBUG(10, ("push_xattr_blob_tdb_raw failed: %s\n",
129 static int posix_eadb_setxattr(struct vfs_handle_struct *handle,
130 const struct smb_filename *smb_fname,
138 SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
140 return posix_eadb_setattr(db, smb_fname->base_name,
141 -1, name, value, size, flags);
144 static int posix_eadb_fsetxattr(struct vfs_handle_struct *handle,
145 struct files_struct *fsp,
146 const char *name, const void *value,
147 size_t size, int flags)
151 SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
153 return posix_eadb_setattr(db, fsp->fsp_name->base_name, fsp->fh->fd, name, value, size, flags);
157 * Worker routine for listxattr and flistxattr
160 static ssize_t posix_eadb_listattr(struct tdb_wrap *db_ctx,
161 const char *fname, int fd, char *list,
167 status = list_posix_eadb_raw(db_ctx, talloc_tos(), fname, fd, &blob);
169 if (!NT_STATUS_IS_OK(status)) {
170 DEBUG(10, ("posix_eadb_fetch_attrs failed: %s\n",
176 if (blob.length > size) {
178 TALLOC_FREE(blob.data);
182 memcpy(list, blob.data, blob.length);
184 TALLOC_FREE(blob.data);
188 static ssize_t posix_eadb_listxattr(struct vfs_handle_struct *handle,
189 const struct smb_filename *smb_fname,
195 SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
197 return posix_eadb_listattr(db, smb_fname->base_name, -1, list, size);
200 static ssize_t posix_eadb_flistxattr(struct vfs_handle_struct *handle,
201 struct files_struct *fsp, char *list,
206 SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
208 return posix_eadb_listattr(db, fsp->fsp_name->base_name, fsp->fh->fd, list, size);
212 * Worker routine for removexattr and fremovexattr
215 static int posix_eadb_removeattr(struct tdb_wrap *db_ctx,
216 const char *fname, int fd, const char *name)
220 status = delete_posix_eadb_raw(db_ctx, name, fname, fd);
222 if (!NT_STATUS_IS_OK(status)) {
223 DEBUG(10, ("delete_posix_eadb_raw failed: %s\n",
230 static int posix_eadb_removexattr(struct vfs_handle_struct *handle,
231 const struct smb_filename *smb_fname,
236 SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
238 return posix_eadb_removeattr(db, smb_fname->base_name, -1, name);
241 static int posix_eadb_fremovexattr(struct vfs_handle_struct *handle,
242 struct files_struct *fsp, const char *name)
246 SMB_VFS_HANDLE_GET_DATA(handle, db, struct tdb_wrap, return -1);
248 return posix_eadb_removeattr(db, fsp->fsp_name->base_name, fsp->fh->fd, name);
252 * Open the tdb file upon VFS_CONNECT
255 static bool posix_eadb_init(int snum, struct tdb_wrap **p_db)
258 struct loadparm_context *lp_ctx;
259 const char *eadb = lp_parm_const_string(snum, "posix", "eadb", NULL);
262 DEBUG(0, ("Can not use vfs_posix_eadb without posix:eadb set\n"));
266 lp_ctx = loadparm_init_s3(NULL, loadparm_s3_helpers());
269 db = tdb_wrap_open(NULL, eadb, 50000,
270 lpcfg_tdb_flags(lp_ctx, TDB_DEFAULT),
271 O_RDWR|O_CREAT, 0600);
274 talloc_unlink(NULL, lp_ctx);
275 /* now we know dbname is not NULL */
291 * On unlink we need to delete the tdb record
293 static int posix_eadb_unlink(vfs_handle_struct *handle,
294 const struct smb_filename *smb_fname)
296 struct smb_filename *smb_fname_tmp = NULL;
299 struct tdb_wrap *ea_tdb;
301 SMB_VFS_HANDLE_GET_DATA(handle, ea_tdb, struct tdb_wrap, return -1);
303 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
304 if (smb_fname_tmp == NULL) {
309 if (smb_fname->flags & SMB_FILENAME_POSIX_PATH) {
310 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_tmp);
312 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_tmp);
318 if (smb_fname_tmp->st.st_ex_nlink == 1) {
321 /* Only remove record on last link to file. */
323 if (tdb_transaction_start(ea_tdb->tdb) != 0) {
328 status = unlink_posix_eadb_raw(ea_tdb, smb_fname->base_name, -1);
329 if (!NT_STATUS_IS_OK(status)) {
330 tdb_transaction_cancel(ea_tdb->tdb);
336 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
339 tdb_transaction_cancel(ea_tdb->tdb);
342 if (tdb_transaction_commit(ea_tdb->tdb) != 0) {
349 TALLOC_FREE(smb_fname_tmp);
354 * On rmdir we need to delete the tdb record
356 static int posix_eadb_rmdir(vfs_handle_struct *handle,
357 const struct smb_filename *smb_fname)
360 struct tdb_wrap *ea_tdb;
362 const char *path = smb_fname->base_name;
364 SMB_VFS_HANDLE_GET_DATA(handle, ea_tdb, struct tdb_wrap, return -1);
366 if (tdb_transaction_start(ea_tdb->tdb) != 0) {
370 status = unlink_posix_eadb_raw(ea_tdb, path, -1);
371 if (!NT_STATUS_IS_OK(status)) {
372 tdb_transaction_cancel(ea_tdb->tdb);
375 ret = SMB_VFS_NEXT_RMDIR(handle, smb_fname);
378 tdb_transaction_cancel(ea_tdb->tdb);
380 if (tdb_transaction_commit(ea_tdb->tdb) != 0) {
389 * Destructor for the VFS private data
392 static void close_xattr_db(void **data)
394 struct tdb_wrap **p_db = (struct tdb_wrap **)data;
398 static int posix_eadb_connect(vfs_handle_struct *handle, const char *service,
405 res = SMB_VFS_NEXT_CONNECT(handle, service, user);
410 snum = find_service(talloc_tos(), service, &sname);
411 if (snum == -1 || sname == NULL) {
413 * Should not happen, but we should not fail just *here*.
418 if (!posix_eadb_init(snum, &db)) {
419 DEBUG(5, ("Could not init xattr tdb\n"));
420 lp_do_parameter(snum, "ea support", "False");
424 lp_do_parameter(snum, "ea support", "True");
426 SMB_VFS_HANDLE_SET_DATA(handle, db, close_xattr_db,
427 struct tdb_wrap, return -1);
432 static struct vfs_fn_pointers vfs_posix_eadb_fns = {
433 .getxattr_fn = posix_eadb_getxattr,
434 .fgetxattr_fn = posix_eadb_fgetxattr,
435 .setxattr_fn = posix_eadb_setxattr,
436 .fsetxattr_fn = posix_eadb_fsetxattr,
437 .listxattr_fn = posix_eadb_listxattr,
438 .flistxattr_fn = posix_eadb_flistxattr,
439 .removexattr_fn = posix_eadb_removexattr,
440 .fremovexattr_fn = posix_eadb_fremovexattr,
441 .unlink_fn = posix_eadb_unlink,
442 .rmdir_fn = posix_eadb_rmdir,
443 .connect_fn = posix_eadb_connect,
447 NTSTATUS vfs_posix_eadb_init(TALLOC_CTX *ctx)
449 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "posix_eadb",
450 &vfs_posix_eadb_fns);