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;
38 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
40 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
44 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
46 return xattr_tdb_getattr(db, &id, name, value, size);
49 static ssize_t xattr_tdb_fgetxattr(struct vfs_handle_struct *handle,
50 struct files_struct *fsp,
51 const char *name, void *value, size_t size)
55 struct db_context *db;
57 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
59 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
63 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
65 return xattr_tdb_getattr(db, &id, name, value, size);
68 static int xattr_tdb_setxattr(struct vfs_handle_struct *handle,
69 const char *path, const char *name,
70 const void *value, size_t size, int flags)
74 struct db_context *db;
76 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
78 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
82 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
84 return xattr_tdb_setattr(db, &id, name, value, size, flags);
87 static int xattr_tdb_fsetxattr(struct vfs_handle_struct *handle,
88 struct files_struct *fsp,
89 const char *name, const void *value,
90 size_t size, int flags)
94 struct db_context *db;
96 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
98 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
102 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
104 return xattr_tdb_setattr(db, &id, name, value, size, flags);
107 static ssize_t xattr_tdb_listxattr(struct vfs_handle_struct *handle,
108 const char *path, char *list, size_t size)
110 SMB_STRUCT_STAT sbuf;
112 struct db_context *db;
114 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
116 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
120 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
122 return xattr_tdb_listattr(db, &id, list, size);
125 static ssize_t xattr_tdb_flistxattr(struct vfs_handle_struct *handle,
126 struct files_struct *fsp, char *list,
129 SMB_STRUCT_STAT sbuf;
131 struct db_context *db;
133 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
135 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
139 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
141 return xattr_tdb_listattr(db, &id, list, size);
144 static int xattr_tdb_removexattr(struct vfs_handle_struct *handle,
145 const char *path, const char *name)
147 SMB_STRUCT_STAT sbuf;
149 struct db_context *db;
151 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
153 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
157 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
159 return xattr_tdb_removeattr(db, &id, name);
162 static int xattr_tdb_fremovexattr(struct vfs_handle_struct *handle,
163 struct files_struct *fsp, const char *name)
165 SMB_STRUCT_STAT sbuf;
167 struct db_context *db;
169 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
171 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
175 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
177 return xattr_tdb_removeattr(db, &id, name);
181 * Open the tdb file upon VFS_CONNECT
184 static bool xattr_tdb_init(int snum, struct db_context **p_db)
186 struct db_context *db;
190 def_dbname = state_path("xattr.tdb");
191 if (def_dbname == NULL) {
196 dbname = lp_parm_const_string(snum, "xattr_tdb", "file", def_dbname);
198 /* now we know dbname is not NULL */
201 db = db_open(NULL, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
202 DBWRAP_LOCK_ORDER_2);
211 TALLOC_FREE(def_dbname);
216 TALLOC_FREE(def_dbname);
221 * On unlink we need to delete the tdb record
223 static int xattr_tdb_unlink(vfs_handle_struct *handle,
224 const struct smb_filename *smb_fname)
226 struct smb_filename *smb_fname_tmp = NULL;
228 struct db_context *db;
231 bool remove_record = false;
233 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
235 status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp);
236 if (!NT_STATUS_IS_OK(status)) {
237 errno = map_errno_from_nt_status(status);
241 if (lp_posix_pathnames()) {
242 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_tmp);
244 ret = SMB_VFS_STAT(handle->conn, smb_fname_tmp);
250 if (smb_fname_tmp->st.st_ex_nlink == 1) {
251 /* Only remove record on last link to file. */
252 remove_record = true;
255 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
261 if (!remove_record) {
265 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &smb_fname_tmp->st);
267 xattr_tdb_remove_all_attrs(db, &id);
270 TALLOC_FREE(smb_fname_tmp);
275 * On rmdir we need to delete the tdb record
277 static int xattr_tdb_rmdir(vfs_handle_struct *handle, const char *path)
279 SMB_STRUCT_STAT sbuf;
281 struct db_context *db;
284 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
286 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
290 ret = SMB_VFS_NEXT_RMDIR(handle, path);
296 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
298 xattr_tdb_remove_all_attrs(db, &id);
304 * Destructor for the VFS private data
307 static void close_xattr_db(void **data)
309 struct db_context **p_db = (struct db_context **)data;
313 static int xattr_tdb_connect(vfs_handle_struct *handle, const char *service,
318 struct db_context *db;
320 res = SMB_VFS_NEXT_CONNECT(handle, service, user);
325 snum = find_service(talloc_tos(), service, &sname);
326 if (snum == -1 || sname == NULL) {
328 * Should not happen, but we should not fail just *here*.
333 if (!xattr_tdb_init(snum, &db)) {
334 DEBUG(5, ("Could not init xattr tdb\n"));
335 lp_do_parameter(snum, "ea support", "False");
339 lp_do_parameter(snum, "ea support", "True");
341 SMB_VFS_HANDLE_SET_DATA(handle, db, close_xattr_db,
342 struct db_context, return -1);
347 static struct vfs_fn_pointers vfs_xattr_tdb_fns = {
348 .getxattr_fn = xattr_tdb_getxattr,
349 .fgetxattr_fn = xattr_tdb_fgetxattr,
350 .setxattr_fn = xattr_tdb_setxattr,
351 .fsetxattr_fn = xattr_tdb_fsetxattr,
352 .listxattr_fn = xattr_tdb_listxattr,
353 .flistxattr_fn = xattr_tdb_flistxattr,
354 .removexattr_fn = xattr_tdb_removexattr,
355 .fremovexattr_fn = xattr_tdb_fremovexattr,
356 .unlink_fn = xattr_tdb_unlink,
357 .rmdir_fn = xattr_tdb_rmdir,
358 .connect_fn = xattr_tdb_connect,
361 NTSTATUS vfs_xattr_tdb_init(void);
362 NTSTATUS vfs_xattr_tdb_init(void)
364 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "xattr_tdb",