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 struct smb_filename *smb_fname,
212 struct db_context *db;
214 TALLOC_CTX *frame = talloc_stackframe();
216 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
217 if (!xattr_tdb_init(-1, frame, &db))
219 TALLOC_FREE(frame); return -1;
222 ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
228 ret = xattr_tdb_listattr(db, &id, list, size);
234 static ssize_t xattr_tdb_flistxattr(struct vfs_handle_struct *handle,
235 struct files_struct *fsp, char *list,
238 SMB_STRUCT_STAT sbuf;
240 struct db_context *db;
242 TALLOC_CTX *frame = talloc_stackframe();
244 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
245 if (!xattr_tdb_init(-1, frame, &db))
247 TALLOC_FREE(frame); return -1;
250 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
255 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
257 ret = xattr_tdb_listattr(db, &id, list, size);
262 static int xattr_tdb_removexattr(struct vfs_handle_struct *handle,
263 const char *path, const char *name)
266 struct db_context *db;
268 TALLOC_CTX *frame = talloc_stackframe();
270 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
271 if (!xattr_tdb_init(-1, frame, &db))
273 TALLOC_FREE(frame); return -1;
276 ret = xattr_tdb_get_file_id(handle, path, &id);
283 ret = xattr_tdb_removeattr(db, &id, name);
288 static int xattr_tdb_fremovexattr(struct vfs_handle_struct *handle,
289 struct files_struct *fsp, const char *name)
291 SMB_STRUCT_STAT sbuf;
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 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
308 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
310 ret = xattr_tdb_removeattr(db, &id, name);
316 * Open the tdb file upon VFS_CONNECT
319 static bool xattr_tdb_init(int snum, TALLOC_CTX *mem_ctx, struct db_context **p_db)
321 struct db_context *db;
325 def_dbname = state_path("xattr.tdb");
326 if (def_dbname == NULL) {
331 dbname = lp_parm_const_string(snum, "xattr_tdb", "file", def_dbname);
333 /* now we know dbname is not NULL */
336 db = db_open(NULL, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
337 DBWRAP_LOCK_ORDER_2, DBWRAP_FLAG_NONE);
346 TALLOC_FREE(def_dbname);
351 TALLOC_FREE(def_dbname);
355 static int xattr_tdb_open(vfs_handle_struct *handle,
356 struct smb_filename *smb_fname,
361 struct db_context *db = NULL;
362 TALLOC_CTX *frame = NULL;
365 fsp->fh->fd = SMB_VFS_NEXT_OPEN(handle,
370 if (fsp->fh->fd < 0) {
374 if ((flags & (O_CREAT|O_EXCL)) != (O_CREAT|O_EXCL)) {
379 * We know we used O_CREAT|O_EXCL and it worked.
380 * We must have created the file.
383 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
385 /* Can't happen... */
386 DBG_WARNING("SMB_VFS_FSTAT failed on file %s (%s)\n",
387 smb_fname_str_dbg(smb_fname),
391 fsp->file_id = SMB_VFS_FILE_ID_CREATE(fsp->conn, &smb_fname->st);
393 frame = talloc_stackframe();
394 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
395 if (!xattr_tdb_init(-1, frame, &db))
397 TALLOC_FREE(frame); return -1;
400 xattr_tdb_remove_all_attrs(db, &fsp->file_id);
405 static int xattr_tdb_mkdir(vfs_handle_struct *handle,
406 const struct smb_filename *smb_fname,
409 struct db_context *db = NULL;
410 TALLOC_CTX *frame = NULL;
411 struct file_id fileid;
413 struct smb_filename *smb_fname_tmp = NULL;
415 ret = SMB_VFS_NEXT_MKDIR(handle, smb_fname, mode);
420 frame = talloc_stackframe();
421 smb_fname_tmp = cp_smb_filename(frame, smb_fname);
422 if (smb_fname_tmp == NULL) {
428 /* Always use LSTAT here - we just creaded the directory. */
429 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_tmp);
431 /* Rename race. Let upper level take care of it. */
435 if (!S_ISDIR(smb_fname_tmp->st.st_ex_mode)) {
436 /* Rename race. Let upper level take care of it. */
441 fileid = SMB_VFS_FILE_ID_CREATE(handle->conn, &smb_fname_tmp->st);
443 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
444 if (!xattr_tdb_init(-1, frame, &db))
446 TALLOC_FREE(frame); return -1;
449 xattr_tdb_remove_all_attrs(db, &fileid);
455 * On unlink we need to delete the tdb record
457 static int xattr_tdb_unlink(vfs_handle_struct *handle,
458 const struct smb_filename *smb_fname)
460 struct smb_filename *smb_fname_tmp = NULL;
462 struct db_context *db;
464 bool remove_record = false;
465 TALLOC_CTX *frame = talloc_stackframe();
467 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
468 if (!xattr_tdb_init(-1, frame, &db))
470 TALLOC_FREE(frame); return -1;
473 smb_fname_tmp = cp_smb_filename(frame, smb_fname);
474 if (smb_fname_tmp == NULL) {
480 if (smb_fname_tmp->flags & SMB_FILENAME_POSIX_PATH) {
481 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_tmp);
483 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_tmp);
489 if (smb_fname_tmp->st.st_ex_nlink == 1) {
490 /* Only remove record on last link to file. */
491 remove_record = true;
494 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
500 if (!remove_record) {
504 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname_tmp->st);
506 xattr_tdb_remove_all_attrs(db, &id);
514 * On rmdir we need to delete the tdb record
516 static int xattr_tdb_rmdir(vfs_handle_struct *handle,
517 const struct smb_filename *smb_fname)
519 SMB_STRUCT_STAT sbuf;
521 struct db_context *db;
523 TALLOC_CTX *frame = talloc_stackframe();
525 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
526 if (!xattr_tdb_init(-1, frame, &db))
528 TALLOC_FREE(frame); return -1;
531 if (vfs_stat_smb_basename(handle->conn,
538 ret = SMB_VFS_NEXT_RMDIR(handle, smb_fname);
545 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
547 xattr_tdb_remove_all_attrs(db, &id);
554 * Destructor for the VFS private data
557 static void close_xattr_db(void **data)
559 struct db_context **p_db = (struct db_context **)data;
563 static int xattr_tdb_connect(vfs_handle_struct *handle, const char *service,
568 struct db_context *db;
570 res = SMB_VFS_NEXT_CONNECT(handle, service, user);
575 snum = find_service(talloc_tos(), service, &sname);
576 if (snum == -1 || sname == NULL) {
578 * Should not happen, but we should not fail just *here*.
583 if (!xattr_tdb_init(snum, NULL, &db)) {
584 DEBUG(5, ("Could not init xattr tdb\n"));
585 lp_do_parameter(snum, "ea support", "False");
589 lp_do_parameter(snum, "ea support", "True");
591 SMB_VFS_HANDLE_SET_DATA(handle, db, close_xattr_db,
592 struct db_context, return -1);
597 static struct vfs_fn_pointers vfs_xattr_tdb_fns = {
598 .getxattr_fn = xattr_tdb_getxattr,
599 .fgetxattr_fn = xattr_tdb_fgetxattr,
600 .setxattr_fn = xattr_tdb_setxattr,
601 .fsetxattr_fn = xattr_tdb_fsetxattr,
602 .listxattr_fn = xattr_tdb_listxattr,
603 .flistxattr_fn = xattr_tdb_flistxattr,
604 .removexattr_fn = xattr_tdb_removexattr,
605 .fremovexattr_fn = xattr_tdb_fremovexattr,
606 .open_fn = xattr_tdb_open,
607 .mkdir_fn = xattr_tdb_mkdir,
608 .unlink_fn = xattr_tdb_unlink,
609 .rmdir_fn = xattr_tdb_rmdir,
610 .connect_fn = xattr_tdb_connect,
613 NTSTATUS vfs_xattr_tdb_init(TALLOC_CTX *);
614 NTSTATUS vfs_xattr_tdb_init(TALLOC_CTX *ctx)
616 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "xattr_tdb",