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_fgetxattr(struct vfs_handle_struct
*handle
,
80 struct files_struct
*fsp
,
81 const char *name
, void *value
, size_t size
)
85 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct tdb_wrap
, return -1);
87 return posix_eadb_getattr(db
, fsp
->fsp_name
->base_name
, fsp_get_io_fd(fsp
), name
, value
, size
);
91 * Worker routine for setxattr and fsetxattr
94 static int posix_eadb_setattr(struct tdb_wrap
*db_ctx
,
95 const char *fname
, int fd
, const char *name
,
96 const void *value
, size_t size
, int flags
)
99 DATA_BLOB data
= data_blob_const(value
, size
);
101 DEBUG(10, ("posix_eadb_setattr called for file %s/fd %d, name %s\n",
104 status
= push_xattr_blob_tdb_raw(db_ctx
, name
, fname
, fd
, &data
);
106 if (!NT_STATUS_IS_OK(status
)) {
107 DEBUG(10, ("push_xattr_blob_tdb_raw failed: %s\n",
115 static int posix_eadb_fsetxattr(struct vfs_handle_struct
*handle
,
116 struct files_struct
*fsp
,
117 const char *name
, const void *value
,
118 size_t size
, int flags
)
122 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct tdb_wrap
, return -1);
124 return posix_eadb_setattr(db
, fsp
->fsp_name
->base_name
, fsp_get_io_fd(fsp
), name
, value
, size
, flags
);
128 * Worker routine for listxattr and flistxattr
131 static ssize_t
posix_eadb_listattr(struct tdb_wrap
*db_ctx
,
132 const char *fname
, int fd
, char *list
,
138 status
= list_posix_eadb_raw(db_ctx
, talloc_tos(), fname
, fd
, &blob
);
140 if (!NT_STATUS_IS_OK(status
)) {
141 DEBUG(10, ("posix_eadb_fetch_attrs failed: %s\n",
147 if (blob
.length
> size
) {
149 TALLOC_FREE(blob
.data
);
153 memcpy(list
, blob
.data
, blob
.length
);
155 TALLOC_FREE(blob
.data
);
159 static ssize_t
posix_eadb_flistxattr(struct vfs_handle_struct
*handle
,
160 struct files_struct
*fsp
, char *list
,
165 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct tdb_wrap
, return -1);
167 return posix_eadb_listattr(db
, fsp
->fsp_name
->base_name
, fsp_get_io_fd(fsp
), list
, size
);
171 * Worker routine for removexattr and fremovexattr
174 static int posix_eadb_removeattr(struct tdb_wrap
*db_ctx
,
175 const char *fname
, int fd
, const char *name
)
179 status
= delete_posix_eadb_raw(db_ctx
, name
, fname
, fd
);
181 if (!NT_STATUS_IS_OK(status
)) {
182 DEBUG(10, ("delete_posix_eadb_raw failed: %s\n",
189 static int posix_eadb_fremovexattr(struct vfs_handle_struct
*handle
,
190 struct files_struct
*fsp
, const char *name
)
194 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct tdb_wrap
, return -1);
196 return posix_eadb_removeattr(db
, fsp
->fsp_name
->base_name
, fsp_get_io_fd(fsp
), name
);
200 * Open the tdb file upon VFS_CONNECT
203 static bool posix_eadb_init(int snum
, struct tdb_wrap
**p_db
)
206 struct loadparm_context
*lp_ctx
;
207 const char *eadb
= lp_parm_const_string(snum
, "posix", "eadb", NULL
);
210 DEBUG(0, ("Can not use vfs_posix_eadb without posix:eadb set\n"));
214 lp_ctx
= loadparm_init_s3(NULL
, loadparm_s3_helpers());
217 db
= tdb_wrap_open(NULL
, eadb
, 50000,
218 lpcfg_tdb_flags(lp_ctx
, TDB_DEFAULT
),
219 O_RDWR
|O_CREAT
, 0600);
222 talloc_unlink(NULL
, lp_ctx
);
223 /* now we know dbname is not NULL */
239 * On unlink we need to delete the tdb record
241 static int posix_eadb_unlink_internal(vfs_handle_struct
*handle
,
242 struct files_struct
*dirfsp
,
243 const struct smb_filename
*smb_fname
,
246 struct smb_filename
*full_fname
= NULL
;
247 struct smb_filename
*smb_fname_tmp
= NULL
;
250 struct tdb_wrap
*ea_tdb
;
252 SMB_VFS_HANDLE_GET_DATA(handle
, ea_tdb
, struct tdb_wrap
, return -1);
254 smb_fname_tmp
= cp_smb_filename(talloc_tos(), smb_fname
);
255 if (smb_fname_tmp
== NULL
) {
261 * TODO: use SMB_VFS_STATX() once we have that.
264 full_fname
= full_path_from_dirfsp_atname(talloc_tos(),
267 if (full_fname
== NULL
) {
271 if (smb_fname
->flags
& SMB_FILENAME_POSIX_PATH
) {
272 ret
= SMB_VFS_NEXT_LSTAT(handle
, full_fname
);
274 ret
= SMB_VFS_NEXT_STAT(handle
, full_fname
);
279 smb_fname_tmp
->st
= full_fname
->st
;
281 if (smb_fname_tmp
->st
.st_ex_nlink
== 1) {
284 /* Only remove record on last link to file. */
286 if (tdb_transaction_start(ea_tdb
->tdb
) != 0) {
291 status
= unlink_posix_eadb_raw(ea_tdb
,
292 full_fname
->base_name
,
294 if (!NT_STATUS_IS_OK(status
)) {
295 tdb_transaction_cancel(ea_tdb
->tdb
);
301 ret
= SMB_VFS_NEXT_UNLINKAT(handle
,
307 tdb_transaction_cancel(ea_tdb
->tdb
);
310 if (tdb_transaction_commit(ea_tdb
->tdb
) != 0) {
317 TALLOC_FREE(smb_fname_tmp
);
318 TALLOC_FREE(full_fname
);
323 * On rmdir we need to delete the tdb record
325 static int posix_eadb_rmdir_internal(vfs_handle_struct
*handle
,
326 struct files_struct
*dirfsp
,
327 const struct smb_filename
*smb_fname
)
330 struct tdb_wrap
*ea_tdb
;
332 struct smb_filename
*full_fname
= NULL
;
334 SMB_VFS_HANDLE_GET_DATA(handle
, ea_tdb
, struct tdb_wrap
, return -1);
336 full_fname
= full_path_from_dirfsp_atname(talloc_tos(),
339 if (full_fname
== NULL
) {
343 if (tdb_transaction_start(ea_tdb
->tdb
) != 0) {
344 TALLOC_FREE(full_fname
);
348 status
= unlink_posix_eadb_raw(ea_tdb
, full_fname
->base_name
, -1);
349 TALLOC_FREE(full_fname
);
350 if (!NT_STATUS_IS_OK(status
)) {
351 tdb_transaction_cancel(ea_tdb
->tdb
);
354 ret
= SMB_VFS_NEXT_UNLINKAT(handle
,
360 tdb_transaction_cancel(ea_tdb
->tdb
);
362 if (tdb_transaction_commit(ea_tdb
->tdb
) != 0) {
370 static int posix_eadb_unlinkat(vfs_handle_struct
*handle
,
371 struct files_struct
*dirfsp
,
372 const struct smb_filename
*smb_fname
,
377 if (flags
& AT_REMOVEDIR
) {
378 ret
= posix_eadb_rmdir_internal(handle
,
382 ret
= posix_eadb_unlink_internal(handle
,
391 * Destructor for the VFS private data
394 static void close_xattr_db(void **data
)
396 struct tdb_wrap
**p_db
= (struct tdb_wrap
**)data
;
400 static int posix_eadb_connect(vfs_handle_struct
*handle
, const char *service
,
407 res
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
412 snum
= find_service(talloc_tos(), service
, &sname
);
413 if (snum
== -1 || sname
== NULL
) {
415 * Should not happen, but we should not fail just *here*.
420 if (!posix_eadb_init(snum
, &db
)) {
421 DEBUG(5, ("Could not init xattr tdb\n"));
422 lp_do_parameter(snum
, "ea support", "False");
426 lp_do_parameter(snum
, "ea support", "True");
428 SMB_VFS_HANDLE_SET_DATA(handle
, db
, close_xattr_db
,
429 struct tdb_wrap
, return -1);
434 static struct vfs_fn_pointers vfs_posix_eadb_fns
= {
435 .getxattrat_send_fn
= vfs_not_implemented_getxattrat_send
,
436 .getxattrat_recv_fn
= vfs_not_implemented_getxattrat_recv
,
437 .fgetxattr_fn
= posix_eadb_fgetxattr
,
438 .fsetxattr_fn
= posix_eadb_fsetxattr
,
439 .flistxattr_fn
= posix_eadb_flistxattr
,
440 .fremovexattr_fn
= posix_eadb_fremovexattr
,
441 .unlinkat_fn
= posix_eadb_unlinkat
,
442 .connect_fn
= posix_eadb_connect
,
446 NTSTATUS
vfs_posix_eadb_init(TALLOC_CTX
*ctx
)
448 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "posix_eadb",
449 &vfs_posix_eadb_fns
);