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 ssize_t xattr_tdb_getxattr(struct vfs_handle_struct *handle,
31 const char *path, const char *name,
32 void *value, size_t size)
36 struct db_context *db;
39 TALLOC_CTX *frame = talloc_stackframe();
45 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
47 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
52 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
54 xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
59 if (blob.length > size) {
64 memcpy(value, blob.data, size);
68 static ssize_t xattr_tdb_fgetxattr(struct vfs_handle_struct *handle,
69 struct files_struct *fsp,
70 const char *name, void *value, size_t size)
74 struct db_context *db;
77 TALLOC_CTX *frame = talloc_stackframe();
83 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
85 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
90 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
92 xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
97 if (blob.length > size) {
102 memcpy(value, blob.data, size);
107 static int xattr_tdb_setxattr(struct vfs_handle_struct *handle,
108 const char *path, const char *name,
109 const void *value, size_t size, int flags)
111 SMB_STRUCT_STAT sbuf;
113 struct db_context *db;
115 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
117 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
121 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
123 return xattr_tdb_setattr(db, &id, name, value, size, flags);
126 static int xattr_tdb_fsetxattr(struct vfs_handle_struct *handle,
127 struct files_struct *fsp,
128 const char *name, const void *value,
129 size_t size, int flags)
131 SMB_STRUCT_STAT sbuf;
133 struct db_context *db;
135 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
137 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
141 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
143 return xattr_tdb_setattr(db, &id, name, value, size, flags);
146 static ssize_t xattr_tdb_listxattr(struct vfs_handle_struct *handle,
147 const char *path, char *list, size_t size)
149 SMB_STRUCT_STAT sbuf;
151 struct db_context *db;
153 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
155 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
159 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
161 return xattr_tdb_listattr(db, &id, list, size);
164 static ssize_t xattr_tdb_flistxattr(struct vfs_handle_struct *handle,
165 struct files_struct *fsp, char *list,
168 SMB_STRUCT_STAT sbuf;
170 struct db_context *db;
172 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
174 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
178 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
180 return xattr_tdb_listattr(db, &id, list, size);
183 static int xattr_tdb_removexattr(struct vfs_handle_struct *handle,
184 const char *path, const char *name)
186 SMB_STRUCT_STAT sbuf;
188 struct db_context *db;
190 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
192 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
196 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
198 return xattr_tdb_removeattr(db, &id, name);
201 static int xattr_tdb_fremovexattr(struct vfs_handle_struct *handle,
202 struct files_struct *fsp, const char *name)
204 SMB_STRUCT_STAT sbuf;
206 struct db_context *db;
208 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
210 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
214 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
216 return xattr_tdb_removeattr(db, &id, name);
220 * Open the tdb file upon VFS_CONNECT
223 static bool xattr_tdb_init(int snum, struct db_context **p_db)
225 struct db_context *db;
229 def_dbname = state_path("xattr.tdb");
230 if (def_dbname == NULL) {
235 dbname = lp_parm_const_string(snum, "xattr_tdb", "file", def_dbname);
237 /* now we know dbname is not NULL */
240 db = db_open(NULL, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
241 DBWRAP_LOCK_ORDER_2);
250 TALLOC_FREE(def_dbname);
255 TALLOC_FREE(def_dbname);
260 * On unlink we need to delete the tdb record
262 static int xattr_tdb_unlink(vfs_handle_struct *handle,
263 const struct smb_filename *smb_fname)
265 struct smb_filename *smb_fname_tmp = NULL;
267 struct db_context *db;
270 bool remove_record = false;
272 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
274 status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp);
275 if (!NT_STATUS_IS_OK(status)) {
276 errno = map_errno_from_nt_status(status);
280 if (lp_posix_pathnames()) {
281 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_tmp);
283 ret = SMB_VFS_STAT(handle->conn, smb_fname_tmp);
289 if (smb_fname_tmp->st.st_ex_nlink == 1) {
290 /* Only remove record on last link to file. */
291 remove_record = true;
294 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
300 if (!remove_record) {
304 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &smb_fname_tmp->st);
306 xattr_tdb_remove_all_attrs(db, &id);
309 TALLOC_FREE(smb_fname_tmp);
314 * On rmdir we need to delete the tdb record
316 static int xattr_tdb_rmdir(vfs_handle_struct *handle, const char *path)
318 SMB_STRUCT_STAT sbuf;
320 struct db_context *db;
323 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
325 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
329 ret = SMB_VFS_NEXT_RMDIR(handle, path);
335 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
337 xattr_tdb_remove_all_attrs(db, &id);
343 * Destructor for the VFS private data
346 static void close_xattr_db(void **data)
348 struct db_context **p_db = (struct db_context **)data;
352 static int xattr_tdb_connect(vfs_handle_struct *handle, const char *service,
357 struct db_context *db;
359 res = SMB_VFS_NEXT_CONNECT(handle, service, user);
364 snum = find_service(talloc_tos(), service, &sname);
365 if (snum == -1 || sname == NULL) {
367 * Should not happen, but we should not fail just *here*.
372 if (!xattr_tdb_init(snum, &db)) {
373 DEBUG(5, ("Could not init xattr tdb\n"));
374 lp_do_parameter(snum, "ea support", "False");
378 lp_do_parameter(snum, "ea support", "True");
380 SMB_VFS_HANDLE_SET_DATA(handle, db, close_xattr_db,
381 struct db_context, return -1);
386 static struct vfs_fn_pointers vfs_xattr_tdb_fns = {
387 .getxattr_fn = xattr_tdb_getxattr,
388 .fgetxattr_fn = xattr_tdb_fgetxattr,
389 .setxattr_fn = xattr_tdb_setxattr,
390 .fsetxattr_fn = xattr_tdb_fsetxattr,
391 .listxattr_fn = xattr_tdb_listxattr,
392 .flistxattr_fn = xattr_tdb_flistxattr,
393 .removexattr_fn = xattr_tdb_removexattr,
394 .fremovexattr_fn = xattr_tdb_fremovexattr,
395 .unlink_fn = xattr_tdb_unlink,
396 .rmdir_fn = xattr_tdb_rmdir,
397 .connect_fn = xattr_tdb_connect,
400 NTSTATUS vfs_xattr_tdb_init(void);
401 NTSTATUS vfs_xattr_tdb_init(void)
403 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "xattr_tdb",