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,
47 if (smb_fname == NULL) {
53 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
60 *id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname->st);
65 struct xattr_tdb_getxattrat_state {
66 struct vfs_aio_state vfs_aio_state;
71 static struct tevent_req *xattr_tdb_getxattrat_send(
73 struct tevent_context *ev,
74 struct vfs_handle_struct *handle,
75 files_struct *dir_fsp,
76 const struct smb_filename *smb_fname,
77 const char *xattr_name,
80 struct tevent_req *req = NULL;
81 struct xattr_tdb_getxattrat_state *state = NULL;
82 struct smb_filename *cwd = NULL;
83 struct db_context *db = NULL;
90 req = tevent_req_create(mem_ctx, &state,
91 struct xattr_tdb_getxattrat_state);
95 state->xattr_size = -1;
97 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
98 if (!xattr_tdb_init(-1, state, &db)) {
99 tevent_req_error(req, EIO);
100 return tevent_req_post(req, ev);
103 cwd = SMB_VFS_GETWD(dir_fsp->conn, state);
104 if (tevent_req_nomem(cwd, req)) {
105 return tevent_req_post(req, ev);
108 ret = SMB_VFS_CHDIR(dir_fsp->conn, dir_fsp->fsp_name);
110 tevent_req_error(req, errno);
111 return tevent_req_post(req, ev);
114 ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
117 cwd_ret = SMB_VFS_CHDIR(dir_fsp->conn, cwd);
118 SMB_ASSERT(cwd_ret == 0);
121 tevent_req_error(req, error);
122 return tevent_req_post(req, ev);
125 state->xattr_size = xattr_tdb_getattr(db,
130 if (state->xattr_size == -1) {
131 tevent_req_error(req, errno);
132 return tevent_req_post(req, ev);
135 if (alloc_hint == 0) {
137 * The caller only wants to know the size.
139 tevent_req_done(req);
140 return tevent_req_post(req, ev);
143 if (state->xattr_size == 0) {
147 tevent_req_done(req);
148 return tevent_req_post(req, ev);
151 if (xattr_blob.length > alloc_hint) {
153 * The data doesn't fit.
155 state->xattr_size = -1;
156 tevent_req_error(req, ERANGE);
157 return tevent_req_post(req, ev);
161 * take the whole blob.
163 state->xattr_value = xattr_blob.data;
165 tevent_req_done(req);
166 return tevent_req_post(req, ev);
169 static ssize_t xattr_tdb_getxattrat_recv(struct tevent_req *req,
170 struct vfs_aio_state *aio_state,
172 uint8_t **xattr_value)
174 struct xattr_tdb_getxattrat_state *state = tevent_req_data(
175 req, struct xattr_tdb_getxattrat_state);
178 if (tevent_req_is_unix_error(req, &aio_state->error)) {
179 tevent_req_received(req);
183 *aio_state = state->vfs_aio_state;
184 xattr_size = state->xattr_size;
185 if (xattr_value != NULL) {
186 *xattr_value = talloc_move(mem_ctx, &state->xattr_value);
189 tevent_req_received(req);
193 static ssize_t xattr_tdb_fgetxattr(struct vfs_handle_struct *handle,
194 struct files_struct *fsp,
195 const char *name, void *value, size_t size)
197 SMB_STRUCT_STAT sbuf;
199 struct db_context *db;
202 TALLOC_CTX *frame = talloc_stackframe();
204 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
205 if (!xattr_tdb_init(-1, frame, &db))
207 TALLOC_FREE(frame); return -1;
210 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
215 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
217 xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
218 if (xattr_size < 0) {
229 if (blob.length > size) {
234 memcpy(value, blob.data, xattr_size);
239 static int xattr_tdb_fsetxattr(struct vfs_handle_struct *handle,
240 struct files_struct *fsp,
241 const char *name, const void *value,
242 size_t size, int flags)
244 SMB_STRUCT_STAT sbuf;
246 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 ret = xattr_tdb_setattr(db, &id, name, value, size, flags);
269 static ssize_t xattr_tdb_flistxattr(struct vfs_handle_struct *handle,
270 struct files_struct *fsp, char *list,
273 SMB_STRUCT_STAT sbuf;
275 struct db_context *db;
277 TALLOC_CTX *frame = talloc_stackframe();
279 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
280 if (!xattr_tdb_init(-1, frame, &db))
282 TALLOC_FREE(frame); return -1;
285 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
290 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
292 ret = xattr_tdb_listattr(db, &id, list, size);
297 static int xattr_tdb_fremovexattr(struct vfs_handle_struct *handle,
298 struct files_struct *fsp, const char *name)
300 SMB_STRUCT_STAT sbuf;
302 struct db_context *db;
304 TALLOC_CTX *frame = talloc_stackframe();
306 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
307 if (!xattr_tdb_init(-1, frame, &db))
309 TALLOC_FREE(frame); return -1;
312 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
317 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
319 ret = xattr_tdb_removeattr(db, &id, name);
325 * Destructor for the VFS private data
328 static void close_xattr_db(void **data)
330 struct db_context **p_db = (struct db_context **)data;
335 * Open the tdb file upon VFS_CONNECT
338 static bool xattr_tdb_init(int snum, TALLOC_CTX *mem_ctx, struct db_context **p_db)
340 struct db_context *db;
344 def_dbname = state_path(talloc_tos(), "xattr.tdb");
345 if (def_dbname == NULL) {
350 dbname = lp_parm_const_string(snum, "xattr_tdb", "file", def_dbname);
352 /* now we know dbname is not NULL */
355 db = db_open(NULL, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
356 DBWRAP_LOCK_ORDER_2, DBWRAP_FLAG_NONE);
365 TALLOC_FREE(def_dbname);
370 TALLOC_FREE(def_dbname);
374 static int xattr_tdb_openat(struct vfs_handle_struct *handle,
375 const struct files_struct *dirfsp,
376 const struct smb_filename *smb_fname,
377 struct files_struct *fsp,
378 const struct vfs_open_how *how)
380 struct db_context *db = NULL;
381 TALLOC_CTX *frame = NULL;
382 SMB_STRUCT_STAT sbuf;
386 fd = SMB_VFS_NEXT_OPENAT(handle,
395 if ((how->flags & (O_CREAT|O_EXCL)) != (O_CREAT|O_EXCL)) {
400 * We know we used O_CREAT|O_EXCL and it worked.
401 * We must have created the file.
405 ret = SMB_VFS_FSTAT(fsp, &sbuf);
408 /* Can't happen... */
409 DBG_WARNING("SMB_VFS_FSTAT failed on file %s (%s)\n",
410 smb_fname_str_dbg(smb_fname),
415 fsp->file_id = SMB_VFS_FILE_ID_CREATE(fsp->conn, &sbuf);
417 frame = talloc_stackframe();
419 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
420 if (!xattr_tdb_init(-1, frame, &db))
422 TALLOC_FREE(frame); return -1;
425 xattr_tdb_remove_all_attrs(db, &fsp->file_id);
431 static int xattr_tdb_mkdirat(vfs_handle_struct *handle,
432 struct files_struct *dirfsp,
433 const struct smb_filename *smb_fname,
436 struct db_context *db = NULL;
437 TALLOC_CTX *frame = NULL;
438 struct file_id fileid;
440 struct smb_filename *full_fname = NULL;
442 ret = SMB_VFS_NEXT_MKDIRAT(handle,
450 frame = talloc_stackframe();
452 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
455 if (full_fname == NULL) {
460 /* Always use LSTAT here - we just created the directory. */
461 ret = SMB_VFS_LSTAT(handle->conn, full_fname);
463 /* Rename race. Let upper level take care of it. */
467 if (!S_ISDIR(full_fname->st.st_ex_mode)) {
468 /* Rename race. Let upper level take care of it. */
473 fileid = SMB_VFS_FILE_ID_CREATE(handle->conn, &full_fname->st);
475 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
476 if (!xattr_tdb_init(-1, frame, &db))
478 TALLOC_FREE(frame); return -1;
481 xattr_tdb_remove_all_attrs(db, &fileid);
487 * On unlink we need to delete the tdb record
489 static int xattr_tdb_unlinkat(vfs_handle_struct *handle,
490 struct files_struct *dirfsp,
491 const struct smb_filename *smb_fname,
494 struct smb_filename *smb_fname_tmp = NULL;
495 struct smb_filename *full_fname = NULL;
497 struct db_context *db;
499 bool remove_record = false;
500 TALLOC_CTX *frame = talloc_stackframe();
502 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
503 if (!xattr_tdb_init(-1, frame, &db))
505 TALLOC_FREE(frame); return -1;
508 smb_fname_tmp = cp_smb_filename(frame, smb_fname);
509 if (smb_fname_tmp == NULL) {
516 * TODO: use SMB_VFS_STATX() once we have that
519 full_fname = full_path_from_dirfsp_atname(frame,
522 if (full_fname == NULL) {
526 if (full_fname->flags & SMB_FILENAME_POSIX_PATH) {
527 ret = SMB_VFS_NEXT_LSTAT(handle, full_fname);
529 ret = SMB_VFS_NEXT_STAT(handle, full_fname);
530 if (ret == -1 && (errno == ENOENT || errno == ELOOP)) {
531 if (VALID_STAT(smb_fname->st) &&
532 S_ISLNK(smb_fname->st.st_ex_mode)) {
534 * Original name was a link - Could be
535 * trying to remove a dangling symlink.
537 ret = SMB_VFS_NEXT_LSTAT(handle, full_fname);
544 smb_fname_tmp->st = full_fname->st;
546 if (flags & AT_REMOVEDIR) {
547 /* Always remove record when removing a directory succeeds. */
548 remove_record = true;
550 if (smb_fname_tmp->st.st_ex_nlink == 1) {
551 /* Only remove record on last link to file. */
552 remove_record = true;
556 ret = SMB_VFS_NEXT_UNLINKAT(handle,
565 if (!remove_record) {
569 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname_tmp->st);
571 xattr_tdb_remove_all_attrs(db, &id);
578 static int xattr_tdb_connect(vfs_handle_struct *handle, const char *service,
583 struct db_context *db;
585 res = SMB_VFS_NEXT_CONNECT(handle, service, user);
590 snum = find_service(talloc_tos(), service, &sname);
591 if (snum == -1 || sname == NULL) {
593 * Should not happen, but we should not fail just *here*.
598 if (!xattr_tdb_init(snum, NULL, &db)) {
599 DEBUG(5, ("Could not init xattr tdb\n"));
600 lp_do_parameter(snum, "ea support", "False");
604 lp_do_parameter(snum, "ea support", "True");
606 SMB_VFS_HANDLE_SET_DATA(handle, db, close_xattr_db,
607 struct db_context, return -1);
612 static struct vfs_fn_pointers vfs_xattr_tdb_fns = {
613 .getxattrat_send_fn = xattr_tdb_getxattrat_send,
614 .getxattrat_recv_fn = xattr_tdb_getxattrat_recv,
615 .fgetxattr_fn = xattr_tdb_fgetxattr,
616 .fsetxattr_fn = xattr_tdb_fsetxattr,
617 .flistxattr_fn = xattr_tdb_flistxattr,
618 .fremovexattr_fn = xattr_tdb_fremovexattr,
619 .openat_fn = xattr_tdb_openat,
620 .mkdirat_fn = xattr_tdb_mkdirat,
621 .unlinkat_fn = xattr_tdb_unlinkat,
622 .connect_fn = xattr_tdb_connect,
626 NTSTATUS vfs_xattr_tdb_init(TALLOC_CTX *ctx)
628 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "xattr_tdb",