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();
41 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
42 TALLOC_FREE(frame); return -1);
44 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
49 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
51 xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
57 if (blob.length > size) {
62 memcpy(value, blob.data, xattr_size);
67 static ssize_t xattr_tdb_fgetxattr(struct vfs_handle_struct *handle,
68 struct files_struct *fsp,
69 const char *name, void *value, size_t size)
73 struct db_context *db;
76 TALLOC_CTX *frame = talloc_stackframe();
78 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, TALLOC_FREE(frame); return -1);
80 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
85 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
87 xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
93 if (blob.length > size) {
98 memcpy(value, blob.data, xattr_size);
103 static int xattr_tdb_setxattr(struct vfs_handle_struct *handle,
104 const char *path, const char *name,
105 const void *value, size_t size, int flags)
107 SMB_STRUCT_STAT sbuf;
109 struct db_context *db;
111 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
113 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
117 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
119 return xattr_tdb_setattr(db, &id, name, value, size, flags);
122 static int xattr_tdb_fsetxattr(struct vfs_handle_struct *handle,
123 struct files_struct *fsp,
124 const char *name, const void *value,
125 size_t size, int flags)
127 SMB_STRUCT_STAT sbuf;
129 struct db_context *db;
131 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
133 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
137 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
139 return xattr_tdb_setattr(db, &id, name, value, size, flags);
142 static ssize_t xattr_tdb_listxattr(struct vfs_handle_struct *handle,
143 const char *path, char *list, size_t size)
145 SMB_STRUCT_STAT sbuf;
147 struct db_context *db;
149 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
151 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
155 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
157 return xattr_tdb_listattr(db, &id, list, size);
160 static ssize_t xattr_tdb_flistxattr(struct vfs_handle_struct *handle,
161 struct files_struct *fsp, char *list,
164 SMB_STRUCT_STAT sbuf;
166 struct db_context *db;
168 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
170 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
174 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
176 return xattr_tdb_listattr(db, &id, list, size);
179 static int xattr_tdb_removexattr(struct vfs_handle_struct *handle,
180 const char *path, const char *name)
182 SMB_STRUCT_STAT sbuf;
184 struct db_context *db;
186 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
188 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
192 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
194 return xattr_tdb_removeattr(db, &id, name);
197 static int xattr_tdb_fremovexattr(struct vfs_handle_struct *handle,
198 struct files_struct *fsp, const char *name)
200 SMB_STRUCT_STAT sbuf;
202 struct db_context *db;
204 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
206 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
210 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
212 return xattr_tdb_removeattr(db, &id, name);
216 * Open the tdb file upon VFS_CONNECT
219 static bool xattr_tdb_init(int snum, struct db_context **p_db)
221 struct db_context *db;
225 def_dbname = state_path("xattr.tdb");
226 if (def_dbname == NULL) {
231 dbname = lp_parm_const_string(snum, "xattr_tdb", "file", def_dbname);
233 /* now we know dbname is not NULL */
236 db = db_open(NULL, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
237 DBWRAP_LOCK_ORDER_2);
246 TALLOC_FREE(def_dbname);
251 TALLOC_FREE(def_dbname);
256 * On unlink we need to delete the tdb record
258 static int xattr_tdb_unlink(vfs_handle_struct *handle,
259 const struct smb_filename *smb_fname)
261 struct smb_filename *smb_fname_tmp = NULL;
263 struct db_context *db;
266 bool remove_record = false;
268 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
270 status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp);
271 if (!NT_STATUS_IS_OK(status)) {
272 errno = map_errno_from_nt_status(status);
276 if (lp_posix_pathnames()) {
277 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_tmp);
279 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_tmp);
285 if (smb_fname_tmp->st.st_ex_nlink == 1) {
286 /* Only remove record on last link to file. */
287 remove_record = true;
290 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
296 if (!remove_record) {
300 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &smb_fname_tmp->st);
302 xattr_tdb_remove_all_attrs(db, &id);
305 TALLOC_FREE(smb_fname_tmp);
310 * On rmdir we need to delete the tdb record
312 static int xattr_tdb_rmdir(vfs_handle_struct *handle, const char *path)
314 SMB_STRUCT_STAT sbuf;
316 struct db_context *db;
319 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
321 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
325 ret = SMB_VFS_NEXT_RMDIR(handle, path);
331 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
333 xattr_tdb_remove_all_attrs(db, &id);
339 * Destructor for the VFS private data
342 static void close_xattr_db(void **data)
344 struct db_context **p_db = (struct db_context **)data;
348 static int xattr_tdb_connect(vfs_handle_struct *handle, const char *service,
353 struct db_context *db;
355 res = SMB_VFS_NEXT_CONNECT(handle, service, user);
360 snum = find_service(talloc_tos(), service, &sname);
361 if (snum == -1 || sname == NULL) {
363 * Should not happen, but we should not fail just *here*.
368 if (!xattr_tdb_init(snum, &db)) {
369 DEBUG(5, ("Could not init xattr tdb\n"));
370 lp_do_parameter(snum, "ea support", "False");
374 lp_do_parameter(snum, "ea support", "True");
376 SMB_VFS_HANDLE_SET_DATA(handle, db, close_xattr_db,
377 struct db_context, return -1);
382 static struct vfs_fn_pointers vfs_xattr_tdb_fns = {
383 .getxattr_fn = xattr_tdb_getxattr,
384 .fgetxattr_fn = xattr_tdb_fgetxattr,
385 .setxattr_fn = xattr_tdb_setxattr,
386 .fsetxattr_fn = xattr_tdb_fsetxattr,
387 .listxattr_fn = xattr_tdb_listxattr,
388 .flistxattr_fn = xattr_tdb_flistxattr,
389 .removexattr_fn = xattr_tdb_removexattr,
390 .fremovexattr_fn = xattr_tdb_fremovexattr,
391 .unlink_fn = xattr_tdb_unlink,
392 .rmdir_fn = xattr_tdb_rmdir,
393 .connect_fn = xattr_tdb_connect,
396 NTSTATUS vfs_xattr_tdb_init(void);
397 NTSTATUS vfs_xattr_tdb_init(void)
399 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "xattr_tdb",