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"
29 #include "tdb_compat.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 char *path
, const char *name
,
81 void *value
, size_t size
)
85 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct tdb_wrap
, return -1);
87 return posix_eadb_getattr(db
, path
, -1, name
, value
, size
);
90 static ssize_t
posix_eadb_fgetxattr(struct vfs_handle_struct
*handle
,
91 struct files_struct
*fsp
,
92 const char *name
, void *value
, size_t size
)
96 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct tdb_wrap
, return -1);
98 return posix_eadb_getattr(db
, fsp
->fsp_name
->base_name
, fsp
->fh
->fd
, name
, value
, size
);
102 * Worker routine for setxattr and fsetxattr
105 static int posix_eadb_setattr(struct tdb_wrap
*db_ctx
,
106 const char *fname
, int fd
, const char *name
,
107 const void *value
, size_t size
, int flags
)
110 DATA_BLOB data
= data_blob_const(value
, size
);
112 DEBUG(10, ("posix_eadb_setattr called for file %s/fd %d, name %s\n",
115 status
= push_xattr_blob_tdb_raw(db_ctx
, name
, fname
, fd
, &data
);
117 if (!NT_STATUS_IS_OK(status
)) {
118 DEBUG(10, ("push_xattr_blob_tdb_raw failed: %s\n",
126 static int posix_eadb_setxattr(struct vfs_handle_struct
*handle
,
127 const char *path
, const char *name
,
128 const void *value
, size_t size
, int flags
)
132 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct tdb_wrap
, return -1);
134 return posix_eadb_setattr(db
, path
, -1, name
, value
, size
, flags
);
137 static int posix_eadb_fsetxattr(struct vfs_handle_struct
*handle
,
138 struct files_struct
*fsp
,
139 const char *name
, const void *value
,
140 size_t size
, int flags
)
144 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct tdb_wrap
, return -1);
146 return posix_eadb_setattr(db
, fsp
->fsp_name
->base_name
, fsp
->fh
->fd
, name
, value
, size
, flags
);
150 * Worker routine for listxattr and flistxattr
153 static ssize_t
posix_eadb_listattr(struct tdb_wrap
*db_ctx
,
154 const char *fname
, int fd
, char *list
,
160 status
= list_posix_eadb_raw(db_ctx
, talloc_tos(), fname
, fd
, &blob
);
162 if (!NT_STATUS_IS_OK(status
)) {
163 DEBUG(10, ("posix_eadb_fetch_attrs failed: %s\n",
169 if (blob
.length
> size
) {
171 TALLOC_FREE(blob
.data
);
175 memcpy(list
, blob
.data
, blob
.length
);
177 TALLOC_FREE(blob
.data
);
181 static ssize_t
posix_eadb_listxattr(struct vfs_handle_struct
*handle
,
182 const char *path
, char *list
, size_t size
)
186 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct tdb_wrap
, return -1);
188 return posix_eadb_listattr(db
, path
, -1, list
, size
);
191 static ssize_t
posix_eadb_flistxattr(struct vfs_handle_struct
*handle
,
192 struct files_struct
*fsp
, char *list
,
197 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct tdb_wrap
, return -1);
199 return posix_eadb_listattr(db
, fsp
->fsp_name
->base_name
, fsp
->fh
->fd
, list
, size
);
203 * Worker routine for removexattr and fremovexattr
206 static int posix_eadb_removeattr(struct tdb_wrap
*db_ctx
,
207 const char *fname
, int fd
, const char *name
)
211 status
= delete_posix_eadb_raw(db_ctx
, name
, fname
, fd
);
213 if (!NT_STATUS_IS_OK(status
)) {
214 DEBUG(10, ("delete_posix_eadb_raw failed: %s\n",
221 static int posix_eadb_removexattr(struct vfs_handle_struct
*handle
,
222 const char *path
, const char *name
)
226 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct tdb_wrap
, return -1);
228 return posix_eadb_removeattr(db
, path
, -1, name
);
231 static int posix_eadb_fremovexattr(struct vfs_handle_struct
*handle
,
232 struct files_struct
*fsp
, const char *name
)
236 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct tdb_wrap
, return -1);
238 return posix_eadb_removeattr(db
, fsp
->fsp_name
->base_name
, fsp
->fh
->fd
, name
);
242 * Open the tdb file upon VFS_CONNECT
245 static bool posix_eadb_init(int snum
, struct tdb_wrap
**p_db
)
248 struct loadparm_context
*lp_ctx
;
249 const char *eadb
= lp_parm_const_string(snum
, "posix", "eadb", NULL
);
252 DEBUG(0, ("Can not use vfs_posix_eadb without posix:eadb set\n"));
256 lp_ctx
= loadparm_init_s3(NULL
, loadparm_s3_helpers());
259 db
= tdb_wrap_open(NULL
, eadb
, 50000,
260 TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600,
264 talloc_unlink(NULL
, lp_ctx
);
265 /* now we know dbname is not NULL */
281 * On unlink we need to delete the tdb record
283 static int posix_eadb_unlink(vfs_handle_struct
*handle
,
284 const struct smb_filename
*smb_fname
)
286 struct smb_filename
*smb_fname_tmp
= NULL
;
290 struct tdb_wrap
*ea_tdb
;
292 SMB_VFS_HANDLE_GET_DATA(handle
, ea_tdb
, struct tdb_wrap
, return -1);
294 status
= copy_smb_filename(talloc_tos(), smb_fname
, &smb_fname_tmp
);
295 if (!NT_STATUS_IS_OK(status
)) {
296 errno
= map_errno_from_nt_status(status
);
300 if (lp_posix_pathnames()) {
301 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname_tmp
);
303 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname_tmp
);
309 if (smb_fname_tmp
->st
.st_ex_nlink
== 1) {
310 /* Only remove record on last link to file. */
312 if (tdb_transaction_start(ea_tdb
->tdb
) != 0) {
317 status
= unlink_posix_eadb_raw(ea_tdb
, smb_fname
->base_name
, -1);
318 if (!NT_STATUS_IS_OK(status
)) {
319 tdb_transaction_cancel(ea_tdb
->tdb
);
325 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname_tmp
);
328 tdb_transaction_cancel(ea_tdb
->tdb
);
331 if (tdb_transaction_commit(ea_tdb
->tdb
) != 0) {
338 TALLOC_FREE(smb_fname_tmp
);
343 * On rmdir we need to delete the tdb record
345 static int posix_eadb_rmdir(vfs_handle_struct
*handle
, const char *path
)
348 struct tdb_wrap
*ea_tdb
;
351 SMB_VFS_HANDLE_GET_DATA(handle
, ea_tdb
, struct tdb_wrap
, return -1);
353 if (tdb_transaction_start(ea_tdb
->tdb
) != 0) {
357 status
= unlink_posix_eadb_raw(ea_tdb
, path
, -1);
358 if (!NT_STATUS_IS_OK(status
)) {
359 tdb_transaction_cancel(ea_tdb
->tdb
);
362 ret
= SMB_VFS_NEXT_RMDIR(handle
, path
);
365 tdb_transaction_cancel(ea_tdb
->tdb
);
367 if (tdb_transaction_commit(ea_tdb
->tdb
) != 0) {
376 * Destructor for the VFS private data
379 static void close_xattr_db(void **data
)
381 struct tdb_wrap
**p_db
= (struct tdb_wrap
**)data
;
385 static int posix_eadb_connect(vfs_handle_struct
*handle
, const char *service
,
392 res
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
397 snum
= find_service(talloc_tos(), service
, &sname
);
398 if (snum
== -1 || sname
== NULL
) {
400 * Should not happen, but we should not fail just *here*.
405 if (!posix_eadb_init(snum
, &db
)) {
406 DEBUG(5, ("Could not init xattr tdb\n"));
407 lp_do_parameter(snum
, "ea support", "False");
411 lp_do_parameter(snum
, "ea support", "True");
413 SMB_VFS_HANDLE_SET_DATA(handle
, db
, close_xattr_db
,
414 struct tdb_wrap
, return -1);
419 static struct vfs_fn_pointers vfs_posix_eadb_fns
= {
420 .getxattr_fn
= posix_eadb_getxattr
,
421 .fgetxattr_fn
= posix_eadb_fgetxattr
,
422 .setxattr_fn
= posix_eadb_setxattr
,
423 .fsetxattr_fn
= posix_eadb_fsetxattr
,
424 .listxattr_fn
= posix_eadb_listxattr
,
425 .flistxattr_fn
= posix_eadb_flistxattr
,
426 .removexattr_fn
= posix_eadb_removexattr
,
427 .fremovexattr_fn
= posix_eadb_fremovexattr
,
428 .unlink_fn
= posix_eadb_unlink
,
429 .rmdir_fn
= posix_eadb_rmdir
,
430 .connect_fn
= posix_eadb_connect
,
433 NTSTATUS
vfs_posix_eadb_init(void);
434 NTSTATUS
vfs_posix_eadb_init(void)
436 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "posix_eadb",
437 &vfs_posix_eadb_fns
);