2 * Store posix-level xattrs in a tdb
4 * Copyright (C) Volker Lendecke, 2007
5 * Copyright (C) Andrew Bartlett, 2012
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/>.
22 #include "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "dbwrap/dbwrap.h"
25 #include "dbwrap/dbwrap_open.h"
26 #include "source3/lib/xattr_tdb.h"
27 #include "lib/util/tevent_unix.h"
30 #define DBGC_CLASS DBGC_VFS
32 static bool xattr_tdb_init(int snum, TALLOC_CTX *mem_ctx, struct db_context **p_db);
34 static int xattr_tdb_get_file_id(struct vfs_handle_struct *handle,
35 const char *path, struct file_id *id)
38 TALLOC_CTX *frame = talloc_stackframe();
39 struct smb_filename *smb_fname;
41 smb_fname = synthetic_smb_fname(frame, path, NULL, NULL, 0);
42 if (smb_fname == NULL) {
48 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
55 *id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname->st);
60 static ssize_t xattr_tdb_getxattr(struct vfs_handle_struct *handle,
61 const struct smb_filename *smb_fname,
67 struct db_context *db;
71 TALLOC_CTX *frame = talloc_stackframe();
73 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
74 if (!xattr_tdb_init(-1, frame, &db))
76 TALLOC_FREE(frame); return -1;
79 ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
85 xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
97 if (blob.length > size) {
102 memcpy(value, blob.data, xattr_size);
107 struct xattr_tdb_getxattrat_state {
108 struct vfs_aio_state vfs_aio_state;
110 uint8_t *xattr_value;
113 static struct tevent_req *xattr_tdb_getxattrat_send(
115 struct tevent_context *ev,
116 struct vfs_handle_struct *handle,
117 files_struct *dir_fsp,
118 const struct smb_filename *smb_fname,
119 const char *xattr_name,
122 struct tevent_req *req = NULL;
123 struct xattr_tdb_getxattrat_state *state = NULL;
124 struct smb_filename *cwd = NULL;
125 struct db_context *db = NULL;
130 DATA_BLOB xattr_blob;
132 req = tevent_req_create(mem_ctx, &state,
133 struct xattr_tdb_getxattrat_state);
137 state->xattr_size = -1;
139 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
140 if (!xattr_tdb_init(-1, state, &db)) {
141 tevent_req_error(req, EIO);
142 return tevent_req_post(req, ev);
145 cwd = SMB_VFS_GETWD(dir_fsp->conn, state);
146 if (tevent_req_nomem(cwd, req)) {
147 return tevent_req_post(req, ev);
150 ret = SMB_VFS_CHDIR(dir_fsp->conn, dir_fsp->fsp_name);
152 tevent_req_error(req, errno);
153 return tevent_req_post(req, ev);
156 ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
159 cwd_ret = SMB_VFS_CHDIR(dir_fsp->conn, cwd);
160 SMB_ASSERT(cwd_ret == 0);
163 tevent_req_error(req, error);
164 return tevent_req_post(req, ev);
167 state->xattr_size = xattr_tdb_getattr(db,
172 if (state->xattr_size == -1) {
173 tevent_req_error(req, errno);
174 return tevent_req_post(req, ev);
177 if (alloc_hint == 0) {
179 * The caller only wants to know the size.
181 tevent_req_done(req);
182 return tevent_req_post(req, ev);
185 if (state->xattr_size == 0) {
189 tevent_req_done(req);
190 return tevent_req_post(req, ev);
193 if (xattr_blob.length > alloc_hint) {
195 * The data doesn't fit.
197 state->xattr_size = -1;
198 tevent_req_error(req, ERANGE);
199 return tevent_req_post(req, ev);
203 * take the whole blob.
205 state->xattr_value = xattr_blob.data;
207 tevent_req_done(req);
208 return tevent_req_post(req, ev);
211 static ssize_t xattr_tdb_getxattrat_recv(struct tevent_req *req,
212 struct vfs_aio_state *aio_state,
214 uint8_t **xattr_value)
216 struct xattr_tdb_getxattrat_state *state = tevent_req_data(
217 req, struct xattr_tdb_getxattrat_state);
220 if (tevent_req_is_unix_error(req, &aio_state->error)) {
221 tevent_req_received(req);
225 *aio_state = state->vfs_aio_state;
226 xattr_size = state->xattr_size;
227 if (xattr_value != NULL) {
228 *xattr_value = talloc_move(mem_ctx, &state->xattr_value);
231 tevent_req_received(req);
235 static ssize_t xattr_tdb_fgetxattr(struct vfs_handle_struct *handle,
236 struct files_struct *fsp,
237 const char *name, void *value, size_t size)
239 SMB_STRUCT_STAT sbuf;
241 struct db_context *db;
244 TALLOC_CTX *frame = talloc_stackframe();
246 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
247 if (!xattr_tdb_init(-1, frame, &db))
249 TALLOC_FREE(frame); return -1;
252 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
257 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
259 xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
260 if (xattr_size < 0) {
271 if (blob.length > size) {
276 memcpy(value, blob.data, xattr_size);
281 static int xattr_tdb_setxattr(struct vfs_handle_struct *handle,
282 const struct smb_filename *smb_fname,
289 struct db_context *db;
291 TALLOC_CTX *frame = talloc_stackframe();
293 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
294 if (!xattr_tdb_init(-1, frame, &db))
296 TALLOC_FREE(frame); return -1;
299 ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
305 ret = xattr_tdb_setattr(db, &id, name, value, size, flags);
310 static int xattr_tdb_fsetxattr(struct vfs_handle_struct *handle,
311 struct files_struct *fsp,
312 const char *name, const void *value,
313 size_t size, int flags)
315 SMB_STRUCT_STAT sbuf;
317 struct db_context *db;
319 TALLOC_CTX *frame = talloc_stackframe();
321 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
322 if (!xattr_tdb_init(-1, frame, &db))
324 TALLOC_FREE(frame); return -1;
327 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
332 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
334 ret = xattr_tdb_setattr(db, &id, name, value, size, flags);
340 static ssize_t xattr_tdb_listxattr(struct vfs_handle_struct *handle,
341 const struct smb_filename *smb_fname,
346 struct db_context *db;
348 TALLOC_CTX *frame = talloc_stackframe();
350 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
351 if (!xattr_tdb_init(-1, frame, &db))
353 TALLOC_FREE(frame); return -1;
356 ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
362 ret = xattr_tdb_listattr(db, &id, list, size);
368 static ssize_t xattr_tdb_flistxattr(struct vfs_handle_struct *handle,
369 struct files_struct *fsp, char *list,
372 SMB_STRUCT_STAT sbuf;
374 struct db_context *db;
376 TALLOC_CTX *frame = talloc_stackframe();
378 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
379 if (!xattr_tdb_init(-1, frame, &db))
381 TALLOC_FREE(frame); return -1;
384 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
389 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
391 ret = xattr_tdb_listattr(db, &id, list, size);
396 static int xattr_tdb_removexattr(struct vfs_handle_struct *handle,
397 const struct smb_filename *smb_fname,
401 struct db_context *db;
403 TALLOC_CTX *frame = talloc_stackframe();
405 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
406 if (!xattr_tdb_init(-1, frame, &db))
408 TALLOC_FREE(frame); return -1;
411 ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
418 ret = xattr_tdb_removeattr(db, &id, name);
423 static int xattr_tdb_fremovexattr(struct vfs_handle_struct *handle,
424 struct files_struct *fsp, const char *name)
426 SMB_STRUCT_STAT sbuf;
428 struct db_context *db;
430 TALLOC_CTX *frame = talloc_stackframe();
432 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
433 if (!xattr_tdb_init(-1, frame, &db))
435 TALLOC_FREE(frame); return -1;
438 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
443 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
445 ret = xattr_tdb_removeattr(db, &id, name);
451 * Open the tdb file upon VFS_CONNECT
454 static bool xattr_tdb_init(int snum, TALLOC_CTX *mem_ctx, struct db_context **p_db)
456 struct db_context *db;
460 def_dbname = state_path(talloc_tos(), "xattr.tdb");
461 if (def_dbname == NULL) {
466 dbname = lp_parm_const_string(snum, "xattr_tdb", "file", def_dbname);
468 /* now we know dbname is not NULL */
471 db = db_open(NULL, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
472 DBWRAP_LOCK_ORDER_2, DBWRAP_FLAG_NONE);
481 TALLOC_FREE(def_dbname);
486 TALLOC_FREE(def_dbname);
490 static int xattr_tdb_open(vfs_handle_struct *handle,
491 struct smb_filename *smb_fname,
496 struct db_context *db = NULL;
497 TALLOC_CTX *frame = NULL;
500 fsp->fh->fd = SMB_VFS_NEXT_OPEN(handle,
505 if (fsp->fh->fd < 0) {
509 if ((flags & (O_CREAT|O_EXCL)) != (O_CREAT|O_EXCL)) {
514 * We know we used O_CREAT|O_EXCL and it worked.
515 * We must have created the file.
518 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
520 /* Can't happen... */
521 DBG_WARNING("SMB_VFS_FSTAT failed on file %s (%s)\n",
522 smb_fname_str_dbg(smb_fname),
526 fsp->file_id = SMB_VFS_FILE_ID_CREATE(fsp->conn, &smb_fname->st);
528 frame = talloc_stackframe();
529 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
530 if (!xattr_tdb_init(-1, frame, &db))
532 TALLOC_FREE(frame); return -1;
535 xattr_tdb_remove_all_attrs(db, &fsp->file_id);
540 static int xattr_tdb_mkdirat(vfs_handle_struct *handle,
541 struct files_struct *dirfsp,
542 const struct smb_filename *smb_fname,
545 struct db_context *db = NULL;
546 TALLOC_CTX *frame = NULL;
547 struct file_id fileid;
549 struct smb_filename *smb_fname_tmp = NULL;
551 ret = SMB_VFS_NEXT_MKDIRAT(handle,
559 frame = talloc_stackframe();
560 smb_fname_tmp = cp_smb_filename(frame, smb_fname);
561 if (smb_fname_tmp == NULL) {
567 /* Always use LSTAT here - we just creaded the directory. */
568 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_tmp);
570 /* Rename race. Let upper level take care of it. */
574 if (!S_ISDIR(smb_fname_tmp->st.st_ex_mode)) {
575 /* Rename race. Let upper level take care of it. */
580 fileid = SMB_VFS_FILE_ID_CREATE(handle->conn, &smb_fname_tmp->st);
582 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
583 if (!xattr_tdb_init(-1, frame, &db))
585 TALLOC_FREE(frame); return -1;
588 xattr_tdb_remove_all_attrs(db, &fileid);
594 * On unlink we need to delete the tdb record
596 static int xattr_tdb_unlinkat(vfs_handle_struct *handle,
597 struct files_struct *dirfsp,
598 const struct smb_filename *smb_fname,
601 struct smb_filename *smb_fname_tmp = NULL;
603 struct db_context *db;
605 bool remove_record = false;
606 TALLOC_CTX *frame = talloc_stackframe();
608 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
609 if (!xattr_tdb_init(-1, frame, &db))
611 TALLOC_FREE(frame); return -1;
614 smb_fname_tmp = cp_smb_filename(frame, smb_fname);
615 if (smb_fname_tmp == NULL) {
621 if (smb_fname_tmp->flags & SMB_FILENAME_POSIX_PATH) {
622 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_tmp);
624 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_tmp);
630 if (flags & AT_REMOVEDIR) {
631 /* Always remove record when removing a directory succeeds. */
632 remove_record = true;
634 if (smb_fname_tmp->st.st_ex_nlink == 1) {
635 /* Only remove record on last link to file. */
636 remove_record = true;
640 ret = SMB_VFS_NEXT_UNLINKAT(handle,
649 if (!remove_record) {
653 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname_tmp->st);
655 xattr_tdb_remove_all_attrs(db, &id);
663 * Destructor for the VFS private data
666 static void close_xattr_db(void **data)
668 struct db_context **p_db = (struct db_context **)data;
672 static int xattr_tdb_connect(vfs_handle_struct *handle, const char *service,
677 struct db_context *db;
679 res = SMB_VFS_NEXT_CONNECT(handle, service, user);
684 snum = find_service(talloc_tos(), service, &sname);
685 if (snum == -1 || sname == NULL) {
687 * Should not happen, but we should not fail just *here*.
692 if (!xattr_tdb_init(snum, NULL, &db)) {
693 DEBUG(5, ("Could not init xattr tdb\n"));
694 lp_do_parameter(snum, "ea support", "False");
698 lp_do_parameter(snum, "ea support", "True");
700 SMB_VFS_HANDLE_SET_DATA(handle, db, close_xattr_db,
701 struct db_context, return -1);
706 static struct vfs_fn_pointers vfs_xattr_tdb_fns = {
707 .getxattr_fn = xattr_tdb_getxattr,
708 .getxattrat_send_fn = xattr_tdb_getxattrat_send,
709 .getxattrat_recv_fn = xattr_tdb_getxattrat_recv,
710 .fgetxattr_fn = xattr_tdb_fgetxattr,
711 .setxattr_fn = xattr_tdb_setxattr,
712 .fsetxattr_fn = xattr_tdb_fsetxattr,
713 .listxattr_fn = xattr_tdb_listxattr,
714 .flistxattr_fn = xattr_tdb_flistxattr,
715 .removexattr_fn = xattr_tdb_removexattr,
716 .fremovexattr_fn = xattr_tdb_fremovexattr,
717 .open_fn = xattr_tdb_open,
718 .mkdirat_fn = xattr_tdb_mkdirat,
719 .unlinkat_fn = xattr_tdb_unlinkat,
720 .connect_fn = xattr_tdb_connect,
724 NTSTATUS vfs_xattr_tdb_init(TALLOC_CTX *ctx)
726 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "xattr_tdb",