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 const struct smb_vfs_ev_glue *evg,
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_context *ev = smb_vfs_ev_glue_ev_ctx(evg);
123 struct tevent_req *req = NULL;
124 struct xattr_tdb_getxattrat_state *state = NULL;
125 struct smb_filename *cwd = NULL;
126 struct db_context *db = NULL;
131 DATA_BLOB xattr_blob;
133 req = tevent_req_create(mem_ctx, &state,
134 struct xattr_tdb_getxattrat_state);
138 state->xattr_size = -1;
140 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
141 if (!xattr_tdb_init(-1, state, &db)) {
142 tevent_req_error(req, EIO);
143 return tevent_req_post(req, ev);
146 cwd = SMB_VFS_GETWD(dir_fsp->conn, state);
147 if (tevent_req_nomem(cwd, req)) {
148 return tevent_req_post(req, ev);
151 ret = SMB_VFS_CHDIR(dir_fsp->conn, dir_fsp->fsp_name);
153 tevent_req_error(req, errno);
154 return tevent_req_post(req, ev);
157 ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
160 cwd_ret = SMB_VFS_CHDIR(dir_fsp->conn, cwd);
161 SMB_ASSERT(cwd_ret == 0);
164 tevent_req_error(req, error);
165 return tevent_req_post(req, ev);
168 state->xattr_size = xattr_tdb_getattr(db,
173 if (state->xattr_size == -1) {
174 tevent_req_error(req, errno);
175 return tevent_req_post(req, ev);
178 if (alloc_hint == 0) {
180 * The caller only wants to know the size.
182 tevent_req_done(req);
183 return tevent_req_post(req, ev);
186 if (state->xattr_size == 0) {
190 tevent_req_done(req);
191 return tevent_req_post(req, ev);
194 if (xattr_blob.length > alloc_hint) {
196 * The data doesn't fit.
198 state->xattr_size = -1;
199 tevent_req_error(req, ERANGE);
200 return tevent_req_post(req, ev);
204 * take the whole blob.
206 state->xattr_value = xattr_blob.data;
208 tevent_req_done(req);
209 return tevent_req_post(req, ev);
212 static ssize_t xattr_tdb_getxattrat_recv(struct tevent_req *req,
213 struct vfs_aio_state *aio_state,
215 uint8_t **xattr_value)
217 struct xattr_tdb_getxattrat_state *state = tevent_req_data(
218 req, struct xattr_tdb_getxattrat_state);
221 if (tevent_req_is_unix_error(req, &aio_state->error)) {
222 tevent_req_received(req);
226 *aio_state = state->vfs_aio_state;
227 xattr_size = state->xattr_size;
228 if (xattr_value != NULL) {
229 *xattr_value = talloc_move(mem_ctx, &state->xattr_value);
232 tevent_req_received(req);
236 static ssize_t xattr_tdb_fgetxattr(struct vfs_handle_struct *handle,
237 struct files_struct *fsp,
238 const char *name, void *value, size_t size)
240 SMB_STRUCT_STAT sbuf;
242 struct db_context *db;
245 TALLOC_CTX *frame = talloc_stackframe();
247 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
248 if (!xattr_tdb_init(-1, frame, &db))
250 TALLOC_FREE(frame); return -1;
253 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
258 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
260 xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
261 if (xattr_size < 0) {
272 if (blob.length > size) {
277 memcpy(value, blob.data, xattr_size);
282 static int xattr_tdb_setxattr(struct vfs_handle_struct *handle,
283 const struct smb_filename *smb_fname,
290 struct db_context *db;
292 TALLOC_CTX *frame = talloc_stackframe();
294 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
295 if (!xattr_tdb_init(-1, frame, &db))
297 TALLOC_FREE(frame); return -1;
300 ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
306 ret = xattr_tdb_setattr(db, &id, name, value, size, flags);
311 static int xattr_tdb_fsetxattr(struct vfs_handle_struct *handle,
312 struct files_struct *fsp,
313 const char *name, const void *value,
314 size_t size, int flags)
316 SMB_STRUCT_STAT sbuf;
318 struct db_context *db;
320 TALLOC_CTX *frame = talloc_stackframe();
322 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
323 if (!xattr_tdb_init(-1, frame, &db))
325 TALLOC_FREE(frame); return -1;
328 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
333 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
335 ret = xattr_tdb_setattr(db, &id, name, value, size, flags);
341 static ssize_t xattr_tdb_listxattr(struct vfs_handle_struct *handle,
342 const struct smb_filename *smb_fname,
347 struct db_context *db;
349 TALLOC_CTX *frame = talloc_stackframe();
351 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
352 if (!xattr_tdb_init(-1, frame, &db))
354 TALLOC_FREE(frame); return -1;
357 ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
363 ret = xattr_tdb_listattr(db, &id, list, size);
369 static ssize_t xattr_tdb_flistxattr(struct vfs_handle_struct *handle,
370 struct files_struct *fsp, char *list,
373 SMB_STRUCT_STAT sbuf;
375 struct db_context *db;
377 TALLOC_CTX *frame = talloc_stackframe();
379 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
380 if (!xattr_tdb_init(-1, frame, &db))
382 TALLOC_FREE(frame); return -1;
385 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
390 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
392 ret = xattr_tdb_listattr(db, &id, list, size);
397 static int xattr_tdb_removexattr(struct vfs_handle_struct *handle,
398 const struct smb_filename *smb_fname,
402 struct db_context *db;
404 TALLOC_CTX *frame = talloc_stackframe();
406 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
407 if (!xattr_tdb_init(-1, frame, &db))
409 TALLOC_FREE(frame); return -1;
412 ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
419 ret = xattr_tdb_removeattr(db, &id, name);
424 static int xattr_tdb_fremovexattr(struct vfs_handle_struct *handle,
425 struct files_struct *fsp, const char *name)
427 SMB_STRUCT_STAT sbuf;
429 struct db_context *db;
431 TALLOC_CTX *frame = talloc_stackframe();
433 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
434 if (!xattr_tdb_init(-1, frame, &db))
436 TALLOC_FREE(frame); return -1;
439 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
444 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
446 ret = xattr_tdb_removeattr(db, &id, name);
452 * Open the tdb file upon VFS_CONNECT
455 static bool xattr_tdb_init(int snum, TALLOC_CTX *mem_ctx, struct db_context **p_db)
457 struct db_context *db;
461 def_dbname = state_path(talloc_tos(), "xattr.tdb");
462 if (def_dbname == NULL) {
467 dbname = lp_parm_const_string(snum, "xattr_tdb", "file", def_dbname);
469 /* now we know dbname is not NULL */
472 db = db_open(NULL, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
473 DBWRAP_LOCK_ORDER_2, DBWRAP_FLAG_NONE);
482 TALLOC_FREE(def_dbname);
487 TALLOC_FREE(def_dbname);
491 static int xattr_tdb_open(vfs_handle_struct *handle,
492 struct smb_filename *smb_fname,
497 struct db_context *db = NULL;
498 TALLOC_CTX *frame = NULL;
501 fsp->fh->fd = SMB_VFS_NEXT_OPEN(handle,
506 if (fsp->fh->fd < 0) {
510 if ((flags & (O_CREAT|O_EXCL)) != (O_CREAT|O_EXCL)) {
515 * We know we used O_CREAT|O_EXCL and it worked.
516 * We must have created the file.
519 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
521 /* Can't happen... */
522 DBG_WARNING("SMB_VFS_FSTAT failed on file %s (%s)\n",
523 smb_fname_str_dbg(smb_fname),
527 fsp->file_id = SMB_VFS_FILE_ID_CREATE(fsp->conn, &smb_fname->st);
529 frame = talloc_stackframe();
530 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
531 if (!xattr_tdb_init(-1, frame, &db))
533 TALLOC_FREE(frame); return -1;
536 xattr_tdb_remove_all_attrs(db, &fsp->file_id);
541 static int xattr_tdb_mkdir(vfs_handle_struct *handle,
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_MKDIR(handle, smb_fname, mode);
556 frame = talloc_stackframe();
557 smb_fname_tmp = cp_smb_filename(frame, smb_fname);
558 if (smb_fname_tmp == NULL) {
564 /* Always use LSTAT here - we just creaded the directory. */
565 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_tmp);
567 /* Rename race. Let upper level take care of it. */
571 if (!S_ISDIR(smb_fname_tmp->st.st_ex_mode)) {
572 /* Rename race. Let upper level take care of it. */
577 fileid = SMB_VFS_FILE_ID_CREATE(handle->conn, &smb_fname_tmp->st);
579 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
580 if (!xattr_tdb_init(-1, frame, &db))
582 TALLOC_FREE(frame); return -1;
585 xattr_tdb_remove_all_attrs(db, &fileid);
591 * On unlink we need to delete the tdb record
593 static int xattr_tdb_unlink(vfs_handle_struct *handle,
594 const struct smb_filename *smb_fname)
596 struct smb_filename *smb_fname_tmp = NULL;
598 struct db_context *db;
600 bool remove_record = false;
601 TALLOC_CTX *frame = talloc_stackframe();
603 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
604 if (!xattr_tdb_init(-1, frame, &db))
606 TALLOC_FREE(frame); return -1;
609 smb_fname_tmp = cp_smb_filename(frame, smb_fname);
610 if (smb_fname_tmp == NULL) {
616 if (smb_fname_tmp->flags & SMB_FILENAME_POSIX_PATH) {
617 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_tmp);
619 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_tmp);
625 if (smb_fname_tmp->st.st_ex_nlink == 1) {
626 /* Only remove record on last link to file. */
627 remove_record = true;
630 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
636 if (!remove_record) {
640 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname_tmp->st);
642 xattr_tdb_remove_all_attrs(db, &id);
650 * On rmdir we need to delete the tdb record
652 static int xattr_tdb_rmdir(vfs_handle_struct *handle,
653 const struct smb_filename *smb_fname)
655 SMB_STRUCT_STAT sbuf;
657 struct db_context *db;
659 TALLOC_CTX *frame = talloc_stackframe();
661 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
662 if (!xattr_tdb_init(-1, frame, &db))
664 TALLOC_FREE(frame); return -1;
667 if (vfs_stat_smb_basename(handle->conn,
674 ret = SMB_VFS_NEXT_RMDIR(handle, smb_fname);
681 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
683 xattr_tdb_remove_all_attrs(db, &id);
690 * Destructor for the VFS private data
693 static void close_xattr_db(void **data)
695 struct db_context **p_db = (struct db_context **)data;
699 static int xattr_tdb_connect(vfs_handle_struct *handle, const char *service,
704 struct db_context *db;
706 res = SMB_VFS_NEXT_CONNECT(handle, service, user);
711 snum = find_service(talloc_tos(), service, &sname);
712 if (snum == -1 || sname == NULL) {
714 * Should not happen, but we should not fail just *here*.
719 if (!xattr_tdb_init(snum, NULL, &db)) {
720 DEBUG(5, ("Could not init xattr tdb\n"));
721 lp_do_parameter(snum, "ea support", "False");
725 lp_do_parameter(snum, "ea support", "True");
727 SMB_VFS_HANDLE_SET_DATA(handle, db, close_xattr_db,
728 struct db_context, return -1);
733 static struct vfs_fn_pointers vfs_xattr_tdb_fns = {
734 .getxattr_fn = xattr_tdb_getxattr,
735 .getxattrat_send_fn = xattr_tdb_getxattrat_send,
736 .getxattrat_recv_fn = xattr_tdb_getxattrat_recv,
737 .fgetxattr_fn = xattr_tdb_fgetxattr,
738 .setxattr_fn = xattr_tdb_setxattr,
739 .fsetxattr_fn = xattr_tdb_fsetxattr,
740 .listxattr_fn = xattr_tdb_listxattr,
741 .flistxattr_fn = xattr_tdb_flistxattr,
742 .removexattr_fn = xattr_tdb_removexattr,
743 .fremovexattr_fn = xattr_tdb_fremovexattr,
744 .open_fn = xattr_tdb_open,
745 .mkdir_fn = xattr_tdb_mkdir,
746 .unlink_fn = xattr_tdb_unlink,
747 .rmdir_fn = xattr_tdb_rmdir,
748 .connect_fn = xattr_tdb_connect,
752 NTSTATUS vfs_xattr_tdb_init(TALLOC_CTX *ctx)
754 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "xattr_tdb",