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);
56 if (blob.length > size) {
61 memcpy(value, blob.data, xattr_size);
66 static ssize_t xattr_tdb_fgetxattr(struct vfs_handle_struct *handle,
67 struct files_struct *fsp,
68 const char *name, void *value, size_t size)
72 struct db_context *db;
75 TALLOC_CTX *frame = talloc_stackframe();
77 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, TALLOC_FREE(frame); return -1);
79 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
84 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
86 xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
91 if (blob.length > size) {
96 memcpy(value, blob.data, xattr_size);
101 static int xattr_tdb_setxattr(struct vfs_handle_struct *handle,
102 const char *path, const char *name,
103 const void *value, size_t size, int flags)
105 SMB_STRUCT_STAT sbuf;
107 struct db_context *db;
109 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
111 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
115 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
117 return xattr_tdb_setattr(db, &id, name, value, size, flags);
120 static int xattr_tdb_fsetxattr(struct vfs_handle_struct *handle,
121 struct files_struct *fsp,
122 const char *name, const void *value,
123 size_t size, int flags)
125 SMB_STRUCT_STAT sbuf;
127 struct db_context *db;
129 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
131 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
135 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
137 return xattr_tdb_setattr(db, &id, name, value, size, flags);
140 static ssize_t xattr_tdb_listxattr(struct vfs_handle_struct *handle,
141 const char *path, char *list, size_t size)
143 SMB_STRUCT_STAT sbuf;
145 struct db_context *db;
147 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
149 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
153 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
155 return xattr_tdb_listattr(db, &id, list, size);
158 static ssize_t xattr_tdb_flistxattr(struct vfs_handle_struct *handle,
159 struct files_struct *fsp, char *list,
162 SMB_STRUCT_STAT sbuf;
164 struct db_context *db;
166 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
168 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
172 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
174 return xattr_tdb_listattr(db, &id, list, size);
177 static int xattr_tdb_removexattr(struct vfs_handle_struct *handle,
178 const char *path, const char *name)
180 SMB_STRUCT_STAT sbuf;
182 struct db_context *db;
184 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
186 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
190 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
192 return xattr_tdb_removeattr(db, &id, name);
195 static int xattr_tdb_fremovexattr(struct vfs_handle_struct *handle,
196 struct files_struct *fsp, const char *name)
198 SMB_STRUCT_STAT sbuf;
200 struct db_context *db;
202 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
204 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
208 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
210 return xattr_tdb_removeattr(db, &id, name);
214 * Open the tdb file upon VFS_CONNECT
217 static bool xattr_tdb_init(int snum, struct db_context **p_db)
219 struct db_context *db;
223 def_dbname = state_path("xattr.tdb");
224 if (def_dbname == NULL) {
229 dbname = lp_parm_const_string(snum, "xattr_tdb", "file", def_dbname);
231 /* now we know dbname is not NULL */
234 db = db_open(NULL, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
235 DBWRAP_LOCK_ORDER_2);
244 TALLOC_FREE(def_dbname);
249 TALLOC_FREE(def_dbname);
254 * On unlink we need to delete the tdb record
256 static int xattr_tdb_unlink(vfs_handle_struct *handle,
257 const struct smb_filename *smb_fname)
259 struct smb_filename *smb_fname_tmp = NULL;
261 struct db_context *db;
264 bool remove_record = false;
266 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
268 status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp);
269 if (!NT_STATUS_IS_OK(status)) {
270 errno = map_errno_from_nt_status(status);
274 if (lp_posix_pathnames()) {
275 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_tmp);
277 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_tmp);
283 if (smb_fname_tmp->st.st_ex_nlink == 1) {
284 /* Only remove record on last link to file. */
285 remove_record = true;
288 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
294 if (!remove_record) {
298 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &smb_fname_tmp->st);
300 xattr_tdb_remove_all_attrs(db, &id);
303 TALLOC_FREE(smb_fname_tmp);
308 * On rmdir we need to delete the tdb record
310 static int xattr_tdb_rmdir(vfs_handle_struct *handle, const char *path)
312 SMB_STRUCT_STAT sbuf;
314 struct db_context *db;
317 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
319 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
323 ret = SMB_VFS_NEXT_RMDIR(handle, path);
329 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
331 xattr_tdb_remove_all_attrs(db, &id);
337 * Destructor for the VFS private data
340 static void close_xattr_db(void **data)
342 struct db_context **p_db = (struct db_context **)data;
346 static int xattr_tdb_connect(vfs_handle_struct *handle, const char *service,
351 struct db_context *db;
353 res = SMB_VFS_NEXT_CONNECT(handle, service, user);
358 snum = find_service(talloc_tos(), service, &sname);
359 if (snum == -1 || sname == NULL) {
361 * Should not happen, but we should not fail just *here*.
366 if (!xattr_tdb_init(snum, &db)) {
367 DEBUG(5, ("Could not init xattr tdb\n"));
368 lp_do_parameter(snum, "ea support", "False");
372 lp_do_parameter(snum, "ea support", "True");
374 SMB_VFS_HANDLE_SET_DATA(handle, db, close_xattr_db,
375 struct db_context, return -1);
380 static struct vfs_fn_pointers vfs_xattr_tdb_fns = {
381 .getxattr_fn = xattr_tdb_getxattr,
382 .fgetxattr_fn = xattr_tdb_fgetxattr,
383 .setxattr_fn = xattr_tdb_setxattr,
384 .fsetxattr_fn = xattr_tdb_fsetxattr,
385 .listxattr_fn = xattr_tdb_listxattr,
386 .flistxattr_fn = xattr_tdb_flistxattr,
387 .removexattr_fn = xattr_tdb_removexattr,
388 .fremovexattr_fn = xattr_tdb_fremovexattr,
389 .unlink_fn = xattr_tdb_unlink,
390 .rmdir_fn = xattr_tdb_rmdir,
391 .connect_fn = xattr_tdb_connect,
394 NTSTATUS vfs_xattr_tdb_init(void);
395 NTSTATUS vfs_xattr_tdb_init(void)
397 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "xattr_tdb",