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"
29 #define DBGC_CLASS DBGC_VFS
31 static bool xattr_tdb_init(int snum, TALLOC_CTX *mem_ctx, struct db_context **p_db);
33 static int xattr_tdb_get_file_id(struct vfs_handle_struct *handle,
34 const char *path, struct file_id *id)
37 TALLOC_CTX *frame = talloc_stackframe();
38 struct smb_filename *smb_fname;
40 smb_fname = synthetic_smb_fname(frame, path, NULL, NULL, 0);
41 if (smb_fname == NULL) {
47 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
54 *id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname->st);
59 static ssize_t xattr_tdb_getxattr(struct vfs_handle_struct *handle,
60 const char *path, const char *name,
61 void *value, size_t size)
64 struct db_context *db;
68 TALLOC_CTX *frame = talloc_stackframe();
70 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
71 if (!xattr_tdb_init(-1, frame, &db))
73 TALLOC_FREE(frame); return -1;
76 ret = xattr_tdb_get_file_id(handle, path, &id);
82 xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
94 if (blob.length > size) {
99 memcpy(value, blob.data, xattr_size);
104 static ssize_t xattr_tdb_fgetxattr(struct vfs_handle_struct *handle,
105 struct files_struct *fsp,
106 const char *name, void *value, size_t size)
108 SMB_STRUCT_STAT sbuf;
110 struct db_context *db;
113 TALLOC_CTX *frame = talloc_stackframe();
115 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
116 if (!xattr_tdb_init(-1, frame, &db))
118 TALLOC_FREE(frame); return -1;
121 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
126 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
128 xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
129 if (xattr_size < 0) {
140 if (blob.length > size) {
145 memcpy(value, blob.data, xattr_size);
150 static int xattr_tdb_setxattr(struct vfs_handle_struct *handle,
151 const char *path, const char *name,
152 const void *value, size_t size, int flags)
155 struct db_context *db;
157 TALLOC_CTX *frame = talloc_stackframe();
159 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
160 if (!xattr_tdb_init(-1, frame, &db))
162 TALLOC_FREE(frame); return -1;
165 ret = xattr_tdb_get_file_id(handle, path, &id);
171 ret = xattr_tdb_setattr(db, &id, name, value, size, flags);
176 static int xattr_tdb_fsetxattr(struct vfs_handle_struct *handle,
177 struct files_struct *fsp,
178 const char *name, const void *value,
179 size_t size, int flags)
181 SMB_STRUCT_STAT sbuf;
183 struct db_context *db;
185 TALLOC_CTX *frame = talloc_stackframe();
187 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
188 if (!xattr_tdb_init(-1, frame, &db))
190 TALLOC_FREE(frame); return -1;
193 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
198 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
200 ret = xattr_tdb_setattr(db, &id, name, value, size, flags);
206 static ssize_t xattr_tdb_listxattr(struct vfs_handle_struct *handle,
207 const char *path, char *list, size_t size)
210 struct db_context *db;
212 TALLOC_CTX *frame = talloc_stackframe();
214 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
215 if (!xattr_tdb_init(-1, frame, &db))
217 TALLOC_FREE(frame); return -1;
220 ret = xattr_tdb_get_file_id(handle, path, &id);
226 ret = xattr_tdb_listattr(db, &id, list, size);
232 static ssize_t xattr_tdb_flistxattr(struct vfs_handle_struct *handle,
233 struct files_struct *fsp, char *list,
236 SMB_STRUCT_STAT sbuf;
238 struct db_context *db;
240 TALLOC_CTX *frame = talloc_stackframe();
242 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
243 if (!xattr_tdb_init(-1, frame, &db))
245 TALLOC_FREE(frame); return -1;
248 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
253 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
255 ret = xattr_tdb_listattr(db, &id, list, size);
260 static int xattr_tdb_removexattr(struct vfs_handle_struct *handle,
261 const char *path, const char *name)
264 struct db_context *db;
266 TALLOC_CTX *frame = talloc_stackframe();
268 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
269 if (!xattr_tdb_init(-1, frame, &db))
271 TALLOC_FREE(frame); return -1;
274 ret = xattr_tdb_get_file_id(handle, path, &id);
281 ret = xattr_tdb_removeattr(db, &id, name);
286 static int xattr_tdb_fremovexattr(struct vfs_handle_struct *handle,
287 struct files_struct *fsp, const char *name)
289 SMB_STRUCT_STAT sbuf;
291 struct db_context *db;
293 TALLOC_CTX *frame = talloc_stackframe();
295 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
296 if (!xattr_tdb_init(-1, frame, &db))
298 TALLOC_FREE(frame); return -1;
301 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
306 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
308 ret = xattr_tdb_removeattr(db, &id, name);
314 * Open the tdb file upon VFS_CONNECT
317 static bool xattr_tdb_init(int snum, TALLOC_CTX *mem_ctx, struct db_context **p_db)
319 struct db_context *db;
323 def_dbname = state_path("xattr.tdb");
324 if (def_dbname == NULL) {
329 dbname = lp_parm_const_string(snum, "xattr_tdb", "file", def_dbname);
331 /* now we know dbname is not NULL */
334 db = db_open(NULL, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
335 DBWRAP_LOCK_ORDER_2, DBWRAP_FLAG_NONE);
344 TALLOC_FREE(def_dbname);
349 TALLOC_FREE(def_dbname);
353 static int xattr_tdb_open(vfs_handle_struct *handle,
354 struct smb_filename *smb_fname,
359 struct db_context *db = NULL;
360 TALLOC_CTX *frame = NULL;
363 fsp->fh->fd = SMB_VFS_NEXT_OPEN(handle,
368 if (fsp->fh->fd < 0) {
372 if ((flags & (O_CREAT|O_EXCL)) != (O_CREAT|O_EXCL)) {
377 * We know we used O_CREAT|O_EXCL and it worked.
378 * We must have created the file.
381 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
383 /* Can't happen... */
384 DBG_WARNING("SMB_VFS_FSTAT failed on file %s (%s)\n",
385 smb_fname_str_dbg(smb_fname),
389 fsp->file_id = SMB_VFS_FILE_ID_CREATE(fsp->conn, &smb_fname->st);
391 frame = talloc_stackframe();
392 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
393 if (!xattr_tdb_init(-1, frame, &db))
395 TALLOC_FREE(frame); return -1;
398 xattr_tdb_remove_all_attrs(db, &fsp->file_id);
403 static int xattr_tdb_mkdir(vfs_handle_struct *handle,
404 const struct smb_filename *smb_fname,
407 struct db_context *db = NULL;
408 TALLOC_CTX *frame = NULL;
409 struct file_id fileid;
411 struct smb_filename *smb_fname_tmp = NULL;
413 ret = SMB_VFS_NEXT_MKDIR(handle, smb_fname, mode);
418 frame = talloc_stackframe();
419 smb_fname_tmp = cp_smb_filename(frame, smb_fname);
420 if (smb_fname_tmp == NULL) {
426 /* Always use LSTAT here - we just creaded the directory. */
427 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_tmp);
429 /* Rename race. Let upper level take care of it. */
433 if (!S_ISDIR(smb_fname_tmp->st.st_ex_mode)) {
434 /* Rename race. Let upper level take care of it. */
439 fileid = SMB_VFS_FILE_ID_CREATE(handle->conn, &smb_fname_tmp->st);
441 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
442 if (!xattr_tdb_init(-1, frame, &db))
444 TALLOC_FREE(frame); return -1;
447 xattr_tdb_remove_all_attrs(db, &fileid);
453 * On unlink we need to delete the tdb record
455 static int xattr_tdb_unlink(vfs_handle_struct *handle,
456 const struct smb_filename *smb_fname)
458 struct smb_filename *smb_fname_tmp = NULL;
460 struct db_context *db;
462 bool remove_record = false;
463 TALLOC_CTX *frame = talloc_stackframe();
465 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
466 if (!xattr_tdb_init(-1, frame, &db))
468 TALLOC_FREE(frame); return -1;
471 smb_fname_tmp = cp_smb_filename(frame, smb_fname);
472 if (smb_fname_tmp == NULL) {
478 if (smb_fname_tmp->flags & SMB_FILENAME_POSIX_PATH) {
479 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_tmp);
481 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_tmp);
487 if (smb_fname_tmp->st.st_ex_nlink == 1) {
488 /* Only remove record on last link to file. */
489 remove_record = true;
492 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
498 if (!remove_record) {
502 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname_tmp->st);
504 xattr_tdb_remove_all_attrs(db, &id);
512 * On rmdir we need to delete the tdb record
514 static int xattr_tdb_rmdir(vfs_handle_struct *handle,
515 const struct smb_filename *smb_fname)
517 SMB_STRUCT_STAT sbuf;
519 struct db_context *db;
521 TALLOC_CTX *frame = talloc_stackframe();
523 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
524 if (!xattr_tdb_init(-1, frame, &db))
526 TALLOC_FREE(frame); return -1;
529 if (vfs_stat_smb_basename(handle->conn,
536 ret = SMB_VFS_NEXT_RMDIR(handle, smb_fname);
543 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
545 xattr_tdb_remove_all_attrs(db, &id);
552 * Destructor for the VFS private data
555 static void close_xattr_db(void **data)
557 struct db_context **p_db = (struct db_context **)data;
561 static int xattr_tdb_connect(vfs_handle_struct *handle, const char *service,
566 struct db_context *db;
568 res = SMB_VFS_NEXT_CONNECT(handle, service, user);
573 snum = find_service(talloc_tos(), service, &sname);
574 if (snum == -1 || sname == NULL) {
576 * Should not happen, but we should not fail just *here*.
581 if (!xattr_tdb_init(snum, NULL, &db)) {
582 DEBUG(5, ("Could not init xattr tdb\n"));
583 lp_do_parameter(snum, "ea support", "False");
587 lp_do_parameter(snum, "ea support", "True");
589 SMB_VFS_HANDLE_SET_DATA(handle, db, close_xattr_db,
590 struct db_context, return -1);
595 static struct vfs_fn_pointers vfs_xattr_tdb_fns = {
596 .getxattr_fn = xattr_tdb_getxattr,
597 .fgetxattr_fn = xattr_tdb_fgetxattr,
598 .setxattr_fn = xattr_tdb_setxattr,
599 .fsetxattr_fn = xattr_tdb_fsetxattr,
600 .listxattr_fn = xattr_tdb_listxattr,
601 .flistxattr_fn = xattr_tdb_flistxattr,
602 .removexattr_fn = xattr_tdb_removexattr,
603 .fremovexattr_fn = xattr_tdb_fremovexattr,
604 .open_fn = xattr_tdb_open,
605 .mkdir_fn = xattr_tdb_mkdir,
606 .unlink_fn = xattr_tdb_unlink,
607 .rmdir_fn = xattr_tdb_rmdir,
608 .connect_fn = xattr_tdb_connect,
611 NTSTATUS vfs_xattr_tdb_init(TALLOC_CTX *);
612 NTSTATUS vfs_xattr_tdb_init(TALLOC_CTX *ctx)
614 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "xattr_tdb",