tevent: expose tevent_context_init_ops
[Samba/gebeck_regimport.git] / source3 / modules / vfs_xattr_tdb.c
blob859e06b8606cecb61a0adbaca007230fd02fc3b6
1 /*
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/>.
20 #include "includes.h"
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"
27 #undef DBGC_CLASS
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)
34 SMB_STRUCT_STAT sbuf;
35 struct file_id id;
36 struct db_context *db;
37 ssize_t xattr_size;
38 DATA_BLOB blob;
39 TALLOC_CTX *frame = talloc_stackframe();
41 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
43 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
44 TALLOC_FREE(frame);
45 return -1;
48 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
50 xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
51 if (xattr_size < 0) {
52 TALLOC_FREE(frame);
53 return -1;
55 if (blob.length > size) {
56 TALLOC_FREE(frame);
57 errno = ERANGE;
58 return -1;
60 memcpy(value, blob.data, xattr_size);
61 return xattr_size;
64 static ssize_t xattr_tdb_fgetxattr(struct vfs_handle_struct *handle,
65 struct files_struct *fsp,
66 const char *name, void *value, size_t size)
68 SMB_STRUCT_STAT sbuf;
69 struct file_id id;
70 struct db_context *db;
71 ssize_t xattr_size;
72 DATA_BLOB blob;
73 TALLOC_CTX *frame = talloc_stackframe();
75 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
77 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
78 TALLOC_FREE(frame);
79 return -1;
82 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
84 xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
85 if (xattr_size < 0) {
86 TALLOC_FREE(frame);
87 return -1;
89 if (blob.length > size) {
90 TALLOC_FREE(frame);
91 errno = ERANGE;
92 return -1;
94 memcpy(value, blob.data, xattr_size);
95 TALLOC_FREE(frame);
96 return xattr_size;
99 static int xattr_tdb_setxattr(struct vfs_handle_struct *handle,
100 const char *path, const char *name,
101 const void *value, size_t size, int flags)
103 SMB_STRUCT_STAT sbuf;
104 struct file_id id;
105 struct db_context *db;
107 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
109 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
110 return -1;
113 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
115 return xattr_tdb_setattr(db, &id, name, value, size, flags);
118 static int xattr_tdb_fsetxattr(struct vfs_handle_struct *handle,
119 struct files_struct *fsp,
120 const char *name, const void *value,
121 size_t size, int flags)
123 SMB_STRUCT_STAT sbuf;
124 struct file_id id;
125 struct db_context *db;
127 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
129 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
130 return -1;
133 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
135 return xattr_tdb_setattr(db, &id, name, value, size, flags);
138 static ssize_t xattr_tdb_listxattr(struct vfs_handle_struct *handle,
139 const char *path, char *list, size_t size)
141 SMB_STRUCT_STAT sbuf;
142 struct file_id id;
143 struct db_context *db;
145 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
147 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
148 return -1;
151 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
153 return xattr_tdb_listattr(db, &id, list, size);
156 static ssize_t xattr_tdb_flistxattr(struct vfs_handle_struct *handle,
157 struct files_struct *fsp, char *list,
158 size_t size)
160 SMB_STRUCT_STAT sbuf;
161 struct file_id id;
162 struct db_context *db;
164 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
166 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
167 return -1;
170 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
172 return xattr_tdb_listattr(db, &id, list, size);
175 static int xattr_tdb_removexattr(struct vfs_handle_struct *handle,
176 const char *path, const char *name)
178 SMB_STRUCT_STAT sbuf;
179 struct file_id id;
180 struct db_context *db;
182 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
184 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
185 return -1;
188 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
190 return xattr_tdb_removeattr(db, &id, name);
193 static int xattr_tdb_fremovexattr(struct vfs_handle_struct *handle,
194 struct files_struct *fsp, const char *name)
196 SMB_STRUCT_STAT sbuf;
197 struct file_id id;
198 struct db_context *db;
200 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
202 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
203 return -1;
206 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
208 return xattr_tdb_removeattr(db, &id, name);
212 * Open the tdb file upon VFS_CONNECT
215 static bool xattr_tdb_init(int snum, struct db_context **p_db)
217 struct db_context *db;
218 const char *dbname;
219 char *def_dbname;
221 def_dbname = state_path("xattr.tdb");
222 if (def_dbname == NULL) {
223 errno = ENOSYS;
224 return false;
227 dbname = lp_parm_const_string(snum, "xattr_tdb", "file", def_dbname);
229 /* now we know dbname is not NULL */
231 become_root();
232 db = db_open(NULL, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
233 DBWRAP_LOCK_ORDER_2);
234 unbecome_root();
236 if (db == NULL) {
237 #if defined(ENOTSUP)
238 errno = ENOTSUP;
239 #else
240 errno = ENOSYS;
241 #endif
242 TALLOC_FREE(def_dbname);
243 return false;
246 *p_db = db;
247 TALLOC_FREE(def_dbname);
248 return true;
252 * On unlink we need to delete the tdb record
254 static int xattr_tdb_unlink(vfs_handle_struct *handle,
255 const struct smb_filename *smb_fname)
257 struct smb_filename *smb_fname_tmp = NULL;
258 struct file_id id;
259 struct db_context *db;
260 NTSTATUS status;
261 int ret = -1;
262 bool remove_record = false;
264 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
266 status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp);
267 if (!NT_STATUS_IS_OK(status)) {
268 errno = map_errno_from_nt_status(status);
269 return -1;
272 if (lp_posix_pathnames()) {
273 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_tmp);
274 } else {
275 ret = SMB_VFS_STAT(handle->conn, smb_fname_tmp);
277 if (ret == -1) {
278 goto out;
281 if (smb_fname_tmp->st.st_ex_nlink == 1) {
282 /* Only remove record on last link to file. */
283 remove_record = true;
286 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
288 if (ret == -1) {
289 goto out;
292 if (!remove_record) {
293 goto out;
296 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &smb_fname_tmp->st);
298 xattr_tdb_remove_all_attrs(db, &id);
300 out:
301 TALLOC_FREE(smb_fname_tmp);
302 return ret;
306 * On rmdir we need to delete the tdb record
308 static int xattr_tdb_rmdir(vfs_handle_struct *handle, const char *path)
310 SMB_STRUCT_STAT sbuf;
311 struct file_id id;
312 struct db_context *db;
313 int ret;
315 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
317 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
318 return -1;
321 ret = SMB_VFS_NEXT_RMDIR(handle, path);
323 if (ret == -1) {
324 return -1;
327 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
329 xattr_tdb_remove_all_attrs(db, &id);
331 return 0;
335 * Destructor for the VFS private data
338 static void close_xattr_db(void **data)
340 struct db_context **p_db = (struct db_context **)data;
341 TALLOC_FREE(*p_db);
344 static int xattr_tdb_connect(vfs_handle_struct *handle, const char *service,
345 const char *user)
347 char *sname = NULL;
348 int res, snum;
349 struct db_context *db;
351 res = SMB_VFS_NEXT_CONNECT(handle, service, user);
352 if (res < 0) {
353 return res;
356 snum = find_service(talloc_tos(), service, &sname);
357 if (snum == -1 || sname == NULL) {
359 * Should not happen, but we should not fail just *here*.
361 return 0;
364 if (!xattr_tdb_init(snum, &db)) {
365 DEBUG(5, ("Could not init xattr tdb\n"));
366 lp_do_parameter(snum, "ea support", "False");
367 return 0;
370 lp_do_parameter(snum, "ea support", "True");
372 SMB_VFS_HANDLE_SET_DATA(handle, db, close_xattr_db,
373 struct db_context, return -1);
375 return 0;
378 static struct vfs_fn_pointers vfs_xattr_tdb_fns = {
379 .getxattr_fn = xattr_tdb_getxattr,
380 .fgetxattr_fn = xattr_tdb_fgetxattr,
381 .setxattr_fn = xattr_tdb_setxattr,
382 .fsetxattr_fn = xattr_tdb_fsetxattr,
383 .listxattr_fn = xattr_tdb_listxattr,
384 .flistxattr_fn = xattr_tdb_flistxattr,
385 .removexattr_fn = xattr_tdb_removexattr,
386 .fremovexattr_fn = xattr_tdb_fremovexattr,
387 .unlink_fn = xattr_tdb_unlink,
388 .rmdir_fn = xattr_tdb_rmdir,
389 .connect_fn = xattr_tdb_connect,
392 NTSTATUS vfs_xattr_tdb_init(void);
393 NTSTATUS vfs_xattr_tdb_init(void)
395 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "xattr_tdb",
396 &vfs_xattr_tdb_fns);