2 * Store posix-level xattrs in a tdb
4 * Copyright (C) Volker Lendecke, 2007
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "system/filesys.h"
22 #include "smbd/smbd.h"
23 #include "dbwrap/dbwrap.h"
24 #include "dbwrap/dbwrap_open.h"
25 #include "source3/lib/xattr_tdb.h"
28 #define DBGC_CLASS DBGC_VFS
30 static int xattr_tdb_get_file_id(struct vfs_handle_struct *handle,
31 const char *path, struct file_id *id)
34 TALLOC_CTX *frame = talloc_stackframe();
35 struct smb_filename *smb_fname = NULL;
36 NTSTATUS status = create_synthetic_smb_fname_split(frame, path, NULL,
38 if (!NT_STATUS_IS_OK(status)) {
39 errno = map_errno_from_nt_status(status);
44 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
51 *id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname->st);
56 static ssize_t xattr_tdb_getxattr(struct vfs_handle_struct *handle,
57 const char *path, const char *name,
58 void *value, size_t size)
61 struct db_context *db;
65 TALLOC_CTX *frame = talloc_stackframe();
67 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
68 TALLOC_FREE(frame); return -1);
70 ret = xattr_tdb_get_file_id(handle, path, &id);
76 xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
82 if (blob.length > size) {
87 memcpy(value, blob.data, xattr_size);
92 static ssize_t xattr_tdb_fgetxattr(struct vfs_handle_struct *handle,
93 struct files_struct *fsp,
94 const char *name, void *value, size_t size)
98 struct db_context *db;
101 TALLOC_CTX *frame = talloc_stackframe();
103 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, TALLOC_FREE(frame); return -1);
105 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
110 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
112 xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
113 if (xattr_size < 0) {
118 if (blob.length > size) {
123 memcpy(value, blob.data, xattr_size);
128 static int xattr_tdb_setxattr(struct vfs_handle_struct *handle,
129 const char *path, const char *name,
130 const void *value, size_t size, int flags)
133 struct db_context *db;
136 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
138 ret = xattr_tdb_get_file_id(handle, path, &id);
143 return xattr_tdb_setattr(db, &id, name, value, size, flags);
146 static int xattr_tdb_fsetxattr(struct vfs_handle_struct *handle,
147 struct files_struct *fsp,
148 const char *name, const void *value,
149 size_t size, int flags)
151 SMB_STRUCT_STAT sbuf;
153 struct db_context *db;
155 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
157 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
161 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
163 return xattr_tdb_setattr(db, &id, name, value, size, flags);
166 static ssize_t xattr_tdb_listxattr(struct vfs_handle_struct *handle,
167 const char *path, char *list, size_t size)
170 struct db_context *db;
173 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
175 ret = xattr_tdb_get_file_id(handle, path, &id);
180 return xattr_tdb_listattr(db, &id, list, size);
183 static ssize_t xattr_tdb_flistxattr(struct vfs_handle_struct *handle,
184 struct files_struct *fsp, char *list,
187 SMB_STRUCT_STAT sbuf;
189 struct db_context *db;
191 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
193 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
197 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
199 return xattr_tdb_listattr(db, &id, list, size);
202 static int xattr_tdb_removexattr(struct vfs_handle_struct *handle,
203 const char *path, const char *name)
206 struct db_context *db;
209 TALLOC_CTX *frame = talloc_stackframe();
211 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, TALLOC_FREE(frame); return -1);
213 ret = xattr_tdb_get_file_id(handle, path, &id);
220 return xattr_tdb_removeattr(db, &id, name);
223 static int xattr_tdb_fremovexattr(struct vfs_handle_struct *handle,
224 struct files_struct *fsp, const char *name)
226 SMB_STRUCT_STAT sbuf;
228 struct db_context *db;
230 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
232 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
236 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
238 return xattr_tdb_removeattr(db, &id, name);
242 * Open the tdb file upon VFS_CONNECT
245 static bool xattr_tdb_init(int snum, struct db_context **p_db)
247 struct db_context *db;
251 def_dbname = state_path("xattr.tdb");
252 if (def_dbname == NULL) {
257 dbname = lp_parm_const_string(snum, "xattr_tdb", "file", def_dbname);
259 /* now we know dbname is not NULL */
262 db = db_open(NULL, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
263 DBWRAP_LOCK_ORDER_2);
272 TALLOC_FREE(def_dbname);
277 TALLOC_FREE(def_dbname);
282 * On unlink we need to delete the tdb record
284 static int xattr_tdb_unlink(vfs_handle_struct *handle,
285 const struct smb_filename *smb_fname)
287 struct smb_filename *smb_fname_tmp = NULL;
289 struct db_context *db;
292 bool remove_record = false;
294 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
296 status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp);
297 if (!NT_STATUS_IS_OK(status)) {
298 errno = map_errno_from_nt_status(status);
302 if (lp_posix_pathnames()) {
303 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_tmp);
305 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_tmp);
311 if (smb_fname_tmp->st.st_ex_nlink == 1) {
312 /* Only remove record on last link to file. */
313 remove_record = true;
316 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
322 if (!remove_record) {
326 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname_tmp->st);
328 xattr_tdb_remove_all_attrs(db, &id);
331 TALLOC_FREE(smb_fname_tmp);
336 * On rmdir we need to delete the tdb record
338 static int xattr_tdb_rmdir(vfs_handle_struct *handle, const char *path)
340 SMB_STRUCT_STAT sbuf;
342 struct db_context *db;
345 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
347 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
351 ret = SMB_VFS_NEXT_RMDIR(handle, path);
357 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
359 xattr_tdb_remove_all_attrs(db, &id);
365 * Destructor for the VFS private data
368 static void close_xattr_db(void **data)
370 struct db_context **p_db = (struct db_context **)data;
374 static int xattr_tdb_connect(vfs_handle_struct *handle, const char *service,
379 struct db_context *db;
381 res = SMB_VFS_NEXT_CONNECT(handle, service, user);
386 snum = find_service(talloc_tos(), service, &sname);
387 if (snum == -1 || sname == NULL) {
389 * Should not happen, but we should not fail just *here*.
394 if (!xattr_tdb_init(snum, &db)) {
395 DEBUG(5, ("Could not init xattr tdb\n"));
396 lp_do_parameter(snum, "ea support", "False");
400 lp_do_parameter(snum, "ea support", "True");
402 SMB_VFS_HANDLE_SET_DATA(handle, db, close_xattr_db,
403 struct db_context, return -1);
408 static struct vfs_fn_pointers vfs_xattr_tdb_fns = {
409 .getxattr_fn = xattr_tdb_getxattr,
410 .fgetxattr_fn = xattr_tdb_fgetxattr,
411 .setxattr_fn = xattr_tdb_setxattr,
412 .fsetxattr_fn = xattr_tdb_fsetxattr,
413 .listxattr_fn = xattr_tdb_listxattr,
414 .flistxattr_fn = xattr_tdb_flistxattr,
415 .removexattr_fn = xattr_tdb_removexattr,
416 .fremovexattr_fn = xattr_tdb_fremovexattr,
417 .unlink_fn = xattr_tdb_unlink,
418 .rmdir_fn = xattr_tdb_rmdir,
419 .connect_fn = xattr_tdb_connect,
422 NTSTATUS vfs_xattr_tdb_init(void);
423 NTSTATUS vfs_xattr_tdb_init(void)
425 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "xattr_tdb",