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,
46 if (smb_fname == NULL) {
52 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
59 *id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname->st);
64 static ssize_t xattr_tdb_getxattr(struct vfs_handle_struct *handle,
65 const struct smb_filename *smb_fname,
71 struct db_context *db;
75 TALLOC_CTX *frame = talloc_stackframe();
77 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
78 if (!xattr_tdb_init(-1, frame, &db))
80 TALLOC_FREE(frame); return -1;
83 ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
89 xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
101 if (blob.length > size) {
106 memcpy(value, blob.data, xattr_size);
111 struct xattr_tdb_getxattrat_state {
112 struct vfs_aio_state vfs_aio_state;
114 uint8_t *xattr_value;
117 static struct tevent_req *xattr_tdb_getxattrat_send(
119 struct tevent_context *ev,
120 struct vfs_handle_struct *handle,
121 files_struct *dir_fsp,
122 const struct smb_filename *smb_fname,
123 const char *xattr_name,
126 struct tevent_req *req = NULL;
127 struct xattr_tdb_getxattrat_state *state = NULL;
128 struct smb_filename *cwd = NULL;
129 struct db_context *db = NULL;
134 DATA_BLOB xattr_blob;
136 req = tevent_req_create(mem_ctx, &state,
137 struct xattr_tdb_getxattrat_state);
141 state->xattr_size = -1;
143 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
144 if (!xattr_tdb_init(-1, state, &db)) {
145 tevent_req_error(req, EIO);
146 return tevent_req_post(req, ev);
149 cwd = SMB_VFS_GETWD(dir_fsp->conn, state);
150 if (tevent_req_nomem(cwd, req)) {
151 return tevent_req_post(req, ev);
154 ret = SMB_VFS_CHDIR(dir_fsp->conn, dir_fsp->fsp_name);
156 tevent_req_error(req, errno);
157 return tevent_req_post(req, ev);
160 ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
163 cwd_ret = SMB_VFS_CHDIR(dir_fsp->conn, cwd);
164 SMB_ASSERT(cwd_ret == 0);
167 tevent_req_error(req, error);
168 return tevent_req_post(req, ev);
171 state->xattr_size = xattr_tdb_getattr(db,
176 if (state->xattr_size == -1) {
177 tevent_req_error(req, errno);
178 return tevent_req_post(req, ev);
181 if (alloc_hint == 0) {
183 * The caller only wants to know the size.
185 tevent_req_done(req);
186 return tevent_req_post(req, ev);
189 if (state->xattr_size == 0) {
193 tevent_req_done(req);
194 return tevent_req_post(req, ev);
197 if (xattr_blob.length > alloc_hint) {
199 * The data doesn't fit.
201 state->xattr_size = -1;
202 tevent_req_error(req, ERANGE);
203 return tevent_req_post(req, ev);
207 * take the whole blob.
209 state->xattr_value = xattr_blob.data;
211 tevent_req_done(req);
212 return tevent_req_post(req, ev);
215 static ssize_t xattr_tdb_getxattrat_recv(struct tevent_req *req,
216 struct vfs_aio_state *aio_state,
218 uint8_t **xattr_value)
220 struct xattr_tdb_getxattrat_state *state = tevent_req_data(
221 req, struct xattr_tdb_getxattrat_state);
224 if (tevent_req_is_unix_error(req, &aio_state->error)) {
225 tevent_req_received(req);
229 *aio_state = state->vfs_aio_state;
230 xattr_size = state->xattr_size;
231 if (xattr_value != NULL) {
232 *xattr_value = talloc_move(mem_ctx, &state->xattr_value);
235 tevent_req_received(req);
239 static ssize_t xattr_tdb_fgetxattr(struct vfs_handle_struct *handle,
240 struct files_struct *fsp,
241 const char *name, void *value, size_t size)
243 SMB_STRUCT_STAT sbuf;
245 struct db_context *db;
248 TALLOC_CTX *frame = talloc_stackframe();
250 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
251 if (!xattr_tdb_init(-1, frame, &db))
253 TALLOC_FREE(frame); return -1;
256 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
261 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
263 xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
264 if (xattr_size < 0) {
275 if (blob.length > size) {
280 memcpy(value, blob.data, xattr_size);
285 static int xattr_tdb_setxattr(struct vfs_handle_struct *handle,
286 const struct smb_filename *smb_fname,
293 struct db_context *db;
295 TALLOC_CTX *frame = talloc_stackframe();
297 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
298 if (!xattr_tdb_init(-1, frame, &db))
300 TALLOC_FREE(frame); return -1;
303 ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
309 ret = xattr_tdb_setattr(db, &id, name, value, size, flags);
314 static int xattr_tdb_fsetxattr(struct vfs_handle_struct *handle,
315 struct files_struct *fsp,
316 const char *name, const void *value,
317 size_t size, int flags)
319 SMB_STRUCT_STAT sbuf;
321 struct db_context *db;
323 TALLOC_CTX *frame = talloc_stackframe();
325 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
326 if (!xattr_tdb_init(-1, frame, &db))
328 TALLOC_FREE(frame); return -1;
331 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
336 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
338 ret = xattr_tdb_setattr(db, &id, name, value, size, flags);
344 static ssize_t xattr_tdb_listxattr(struct vfs_handle_struct *handle,
345 const struct smb_filename *smb_fname,
350 struct db_context *db;
352 TALLOC_CTX *frame = talloc_stackframe();
354 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
355 if (!xattr_tdb_init(-1, frame, &db))
357 TALLOC_FREE(frame); return -1;
360 ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
366 ret = xattr_tdb_listattr(db, &id, list, size);
372 static ssize_t xattr_tdb_flistxattr(struct vfs_handle_struct *handle,
373 struct files_struct *fsp, char *list,
376 SMB_STRUCT_STAT sbuf;
378 struct db_context *db;
380 TALLOC_CTX *frame = talloc_stackframe();
382 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
383 if (!xattr_tdb_init(-1, frame, &db))
385 TALLOC_FREE(frame); return -1;
388 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
393 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
395 ret = xattr_tdb_listattr(db, &id, list, size);
400 static int xattr_tdb_removexattr(struct vfs_handle_struct *handle,
401 const struct smb_filename *smb_fname,
405 struct db_context *db;
407 TALLOC_CTX *frame = talloc_stackframe();
409 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
410 if (!xattr_tdb_init(-1, frame, &db))
412 TALLOC_FREE(frame); return -1;
415 ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
422 ret = xattr_tdb_removeattr(db, &id, name);
427 static int xattr_tdb_fremovexattr(struct vfs_handle_struct *handle,
428 struct files_struct *fsp, const char *name)
430 SMB_STRUCT_STAT sbuf;
432 struct db_context *db;
434 TALLOC_CTX *frame = talloc_stackframe();
436 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
437 if (!xattr_tdb_init(-1, frame, &db))
439 TALLOC_FREE(frame); return -1;
442 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
447 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
449 ret = xattr_tdb_removeattr(db, &id, name);
455 * Open the tdb file upon VFS_CONNECT
458 static bool xattr_tdb_init(int snum, TALLOC_CTX *mem_ctx, struct db_context **p_db)
460 struct db_context *db;
464 def_dbname = state_path(talloc_tos(), "xattr.tdb");
465 if (def_dbname == NULL) {
470 dbname = lp_parm_const_string(snum, "xattr_tdb", "file", def_dbname);
472 /* now we know dbname is not NULL */
475 db = db_open(NULL, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
476 DBWRAP_LOCK_ORDER_2, DBWRAP_FLAG_NONE);
485 TALLOC_FREE(def_dbname);
490 TALLOC_FREE(def_dbname);
494 static int xattr_tdb_open(vfs_handle_struct *handle,
495 struct smb_filename *smb_fname,
500 struct db_context *db = NULL;
501 TALLOC_CTX *frame = NULL;
504 fsp->fh->fd = SMB_VFS_NEXT_OPEN(handle,
509 if (fsp->fh->fd < 0) {
513 if ((flags & (O_CREAT|O_EXCL)) != (O_CREAT|O_EXCL)) {
518 * We know we used O_CREAT|O_EXCL and it worked.
519 * We must have created the file.
522 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
524 /* Can't happen... */
525 DBG_WARNING("SMB_VFS_FSTAT failed on file %s (%s)\n",
526 smb_fname_str_dbg(smb_fname),
530 fsp->file_id = SMB_VFS_FILE_ID_CREATE(fsp->conn, &smb_fname->st);
532 frame = talloc_stackframe();
533 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
534 if (!xattr_tdb_init(-1, frame, &db))
536 TALLOC_FREE(frame); return -1;
539 xattr_tdb_remove_all_attrs(db, &fsp->file_id);
544 static int xattr_tdb_mkdirat(vfs_handle_struct *handle,
545 struct files_struct *dirfsp,
546 const struct smb_filename *smb_fname,
549 struct db_context *db = NULL;
550 TALLOC_CTX *frame = NULL;
551 struct file_id fileid;
553 struct smb_filename *smb_fname_tmp = NULL;
555 ret = SMB_VFS_NEXT_MKDIRAT(handle,
563 frame = talloc_stackframe();
564 smb_fname_tmp = cp_smb_filename(frame, smb_fname);
565 if (smb_fname_tmp == NULL) {
571 /* Always use LSTAT here - we just creaded the directory. */
572 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_tmp);
574 /* Rename race. Let upper level take care of it. */
578 if (!S_ISDIR(smb_fname_tmp->st.st_ex_mode)) {
579 /* Rename race. Let upper level take care of it. */
584 fileid = SMB_VFS_FILE_ID_CREATE(handle->conn, &smb_fname_tmp->st);
586 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
587 if (!xattr_tdb_init(-1, frame, &db))
589 TALLOC_FREE(frame); return -1;
592 xattr_tdb_remove_all_attrs(db, &fileid);
598 * On unlink we need to delete the tdb record
600 static int xattr_tdb_unlinkat(vfs_handle_struct *handle,
601 struct files_struct *dirfsp,
602 const struct smb_filename *smb_fname,
605 struct smb_filename *smb_fname_tmp = NULL;
607 struct db_context *db;
609 bool remove_record = false;
610 TALLOC_CTX *frame = talloc_stackframe();
612 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
613 if (!xattr_tdb_init(-1, frame, &db))
615 TALLOC_FREE(frame); return -1;
618 smb_fname_tmp = cp_smb_filename(frame, smb_fname);
619 if (smb_fname_tmp == NULL) {
625 if (smb_fname_tmp->flags & SMB_FILENAME_POSIX_PATH) {
626 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_tmp);
628 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_tmp);
634 if (flags & AT_REMOVEDIR) {
635 /* Always remove record when removing a directory succeeds. */
636 remove_record = true;
638 if (smb_fname_tmp->st.st_ex_nlink == 1) {
639 /* Only remove record on last link to file. */
640 remove_record = true;
644 ret = SMB_VFS_NEXT_UNLINKAT(handle,
653 if (!remove_record) {
657 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname_tmp->st);
659 xattr_tdb_remove_all_attrs(db, &id);
667 * Destructor for the VFS private data
670 static void close_xattr_db(void **data)
672 struct db_context **p_db = (struct db_context **)data;
676 static int xattr_tdb_connect(vfs_handle_struct *handle, const char *service,
681 struct db_context *db;
683 res = SMB_VFS_NEXT_CONNECT(handle, service, user);
688 snum = find_service(talloc_tos(), service, &sname);
689 if (snum == -1 || sname == NULL) {
691 * Should not happen, but we should not fail just *here*.
696 if (!xattr_tdb_init(snum, NULL, &db)) {
697 DEBUG(5, ("Could not init xattr tdb\n"));
698 lp_do_parameter(snum, "ea support", "False");
702 lp_do_parameter(snum, "ea support", "True");
704 SMB_VFS_HANDLE_SET_DATA(handle, db, close_xattr_db,
705 struct db_context, return -1);
710 static struct vfs_fn_pointers vfs_xattr_tdb_fns = {
711 .getxattr_fn = xattr_tdb_getxattr,
712 .getxattrat_send_fn = xattr_tdb_getxattrat_send,
713 .getxattrat_recv_fn = xattr_tdb_getxattrat_recv,
714 .fgetxattr_fn = xattr_tdb_fgetxattr,
715 .setxattr_fn = xattr_tdb_setxattr,
716 .fsetxattr_fn = xattr_tdb_fsetxattr,
717 .listxattr_fn = xattr_tdb_listxattr,
718 .flistxattr_fn = xattr_tdb_flistxattr,
719 .removexattr_fn = xattr_tdb_removexattr,
720 .fremovexattr_fn = xattr_tdb_fremovexattr,
721 .open_fn = xattr_tdb_open,
722 .mkdirat_fn = xattr_tdb_mkdirat,
723 .unlinkat_fn = xattr_tdb_unlinkat,
724 .connect_fn = xattr_tdb_connect,
728 NTSTATUS vfs_xattr_tdb_init(TALLOC_CTX *ctx)
730 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "xattr_tdb",