kcc: Add corresponding methods for repsTo
[Samba.git] / source3 / modules / vfs_xattr_tdb.c
blobb32fbc1e9eb1047dea4baef324ecc1bcf3e2ca3c
1 /*
2 * Store posix-level xattrs in a tdb
4 * Copyright (C) Volker Lendecke, 2007
5 * Copyright (C) Andrew Bartlett, 2012
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "dbwrap/dbwrap.h"
25 #include "dbwrap/dbwrap_open.h"
26 #include "source3/lib/xattr_tdb.h"
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_VFS
31 static bool xattr_tdb_init(int snum, TALLOC_CTX *mem_ctx, struct db_context **p_db);
33 static int xattr_tdb_get_file_id(struct vfs_handle_struct *handle,
34 const char *path, struct file_id *id)
36 int ret;
37 TALLOC_CTX *frame = talloc_stackframe();
38 struct smb_filename *smb_fname;
40 smb_fname = synthetic_smb_fname(frame, path, NULL, NULL, 0);
41 if (smb_fname == NULL) {
42 TALLOC_FREE(frame);
43 errno = ENOMEM;
44 return -1;
47 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
49 if (ret == -1) {
50 TALLOC_FREE(frame);
51 return -1;
54 *id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname->st);
55 TALLOC_FREE(frame);
56 return 0;
59 static ssize_t xattr_tdb_getxattr(struct vfs_handle_struct *handle,
60 const char *path, const char *name,
61 void *value, size_t size)
63 struct file_id id;
64 struct db_context *db;
65 ssize_t xattr_size;
66 int ret;
67 DATA_BLOB blob;
68 TALLOC_CTX *frame = talloc_stackframe();
70 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
71 if (!xattr_tdb_init(-1, frame, &db))
73 TALLOC_FREE(frame); return -1;
74 });
76 ret = xattr_tdb_get_file_id(handle, path, &id);
77 if (ret == -1) {
78 TALLOC_FREE(frame);
79 return -1;
82 xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
83 if (xattr_size < 0) {
84 errno = ENOATTR;
85 TALLOC_FREE(frame);
86 return -1;
88 if (blob.length > size) {
89 TALLOC_FREE(frame);
90 errno = ERANGE;
91 return -1;
93 memcpy(value, blob.data, xattr_size);
94 TALLOC_FREE(frame);
95 return xattr_size;
98 static ssize_t xattr_tdb_fgetxattr(struct vfs_handle_struct *handle,
99 struct files_struct *fsp,
100 const char *name, void *value, size_t size)
102 SMB_STRUCT_STAT sbuf;
103 struct file_id id;
104 struct db_context *db;
105 ssize_t xattr_size;
106 DATA_BLOB blob;
107 TALLOC_CTX *frame = talloc_stackframe();
109 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
110 if (!xattr_tdb_init(-1, frame, &db))
112 TALLOC_FREE(frame); return -1;
115 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
116 TALLOC_FREE(frame);
117 return -1;
120 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
122 xattr_size = xattr_tdb_getattr(db, frame, &id, name, &blob);
123 if (xattr_size < 0) {
124 errno = ENOATTR;
125 TALLOC_FREE(frame);
126 return -1;
128 if (blob.length > size) {
129 TALLOC_FREE(frame);
130 errno = ERANGE;
131 return -1;
133 memcpy(value, blob.data, xattr_size);
134 TALLOC_FREE(frame);
135 return xattr_size;
138 static int xattr_tdb_setxattr(struct vfs_handle_struct *handle,
139 const char *path, const char *name,
140 const void *value, size_t size, int flags)
142 struct file_id id;
143 struct db_context *db;
144 int ret;
145 TALLOC_CTX *frame = talloc_stackframe();
147 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
148 if (!xattr_tdb_init(-1, frame, &db))
150 TALLOC_FREE(frame); return -1;
153 ret = xattr_tdb_get_file_id(handle, path, &id);
154 if (ret == -1) {
155 TALLOC_FREE(frame);
156 return -1;
159 ret = xattr_tdb_setattr(db, &id, name, value, size, flags);
160 TALLOC_FREE(frame);
161 return ret;
164 static int xattr_tdb_fsetxattr(struct vfs_handle_struct *handle,
165 struct files_struct *fsp,
166 const char *name, const void *value,
167 size_t size, int flags)
169 SMB_STRUCT_STAT sbuf;
170 struct file_id id;
171 struct db_context *db;
172 int ret;
173 TALLOC_CTX *frame = talloc_stackframe();
175 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
176 if (!xattr_tdb_init(-1, frame, &db))
178 TALLOC_FREE(frame); return -1;
181 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
182 TALLOC_FREE(frame);
183 return -1;
186 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
188 ret = xattr_tdb_setattr(db, &id, name, value, size, flags);
189 TALLOC_FREE(frame);
190 return ret;
194 static ssize_t xattr_tdb_listxattr(struct vfs_handle_struct *handle,
195 const char *path, char *list, size_t size)
197 struct file_id id;
198 struct db_context *db;
199 int ret;
200 TALLOC_CTX *frame = talloc_stackframe();
202 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
203 if (!xattr_tdb_init(-1, frame, &db))
205 TALLOC_FREE(frame); return -1;
208 ret = xattr_tdb_get_file_id(handle, path, &id);
209 if (ret == -1) {
210 TALLOC_FREE(frame);
211 return -1;
214 ret = xattr_tdb_listattr(db, &id, list, size);
215 TALLOC_FREE(frame);
216 return ret;
220 static ssize_t xattr_tdb_flistxattr(struct vfs_handle_struct *handle,
221 struct files_struct *fsp, char *list,
222 size_t size)
224 SMB_STRUCT_STAT sbuf;
225 struct file_id id;
226 struct db_context *db;
227 int ret;
228 TALLOC_CTX *frame = talloc_stackframe();
230 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
231 if (!xattr_tdb_init(-1, frame, &db))
233 TALLOC_FREE(frame); return -1;
236 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
237 TALLOC_FREE(frame);
238 return -1;
241 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
243 ret = xattr_tdb_listattr(db, &id, list, size);
244 TALLOC_FREE(frame);
245 return ret;
248 static int xattr_tdb_removexattr(struct vfs_handle_struct *handle,
249 const char *path, const char *name)
251 struct file_id id;
252 struct db_context *db;
253 int ret;
254 TALLOC_CTX *frame = talloc_stackframe();
256 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
257 if (!xattr_tdb_init(-1, frame, &db))
259 TALLOC_FREE(frame); return -1;
262 ret = xattr_tdb_get_file_id(handle, path, &id);
263 if (ret == -1) {
264 TALLOC_FREE(frame);
265 return ret;
269 ret = xattr_tdb_removeattr(db, &id, name);
270 TALLOC_FREE(frame);
271 return ret;
274 static int xattr_tdb_fremovexattr(struct vfs_handle_struct *handle,
275 struct files_struct *fsp, const char *name)
277 SMB_STRUCT_STAT sbuf;
278 struct file_id id;
279 struct db_context *db;
280 int ret;
281 TALLOC_CTX *frame = talloc_stackframe();
283 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
284 if (!xattr_tdb_init(-1, frame, &db))
286 TALLOC_FREE(frame); return -1;
289 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
290 TALLOC_FREE(frame);
291 return -1;
294 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
296 ret = xattr_tdb_removeattr(db, &id, name);
297 TALLOC_FREE(frame);
298 return ret;
302 * Open the tdb file upon VFS_CONNECT
305 static bool xattr_tdb_init(int snum, TALLOC_CTX *mem_ctx, struct db_context **p_db)
307 struct db_context *db;
308 const char *dbname;
309 char *def_dbname;
311 def_dbname = state_path("xattr.tdb");
312 if (def_dbname == NULL) {
313 errno = ENOSYS;
314 return false;
317 dbname = lp_parm_const_string(snum, "xattr_tdb", "file", def_dbname);
319 /* now we know dbname is not NULL */
321 become_root();
322 db = db_open(NULL, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
323 DBWRAP_LOCK_ORDER_2, DBWRAP_FLAG_NONE);
324 unbecome_root();
326 if (db == NULL) {
327 #if defined(ENOTSUP)
328 errno = ENOTSUP;
329 #else
330 errno = ENOSYS;
331 #endif
332 TALLOC_FREE(def_dbname);
333 return false;
336 *p_db = db;
337 TALLOC_FREE(def_dbname);
338 return true;
341 static int xattr_tdb_open(vfs_handle_struct *handle,
342 struct smb_filename *smb_fname,
343 files_struct *fsp,
344 int flags,
345 mode_t mode)
347 struct db_context *db = NULL;
348 TALLOC_CTX *frame = NULL;
349 int ret;
351 fsp->fh->fd = SMB_VFS_NEXT_OPEN(handle,
352 smb_fname, fsp,
353 flags,
354 mode);
356 if (fsp->fh->fd < 0) {
357 return fsp->fh->fd;
360 if ((flags & (O_CREAT|O_EXCL)) != (O_CREAT|O_EXCL)) {
361 return fsp->fh->fd;
365 * We know we used O_CREAT|O_EXCL and it worked.
366 * We must have created the file.
369 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
370 if (ret == -1) {
371 /* Can't happen... */
372 DBG_WARNING("SMB_VFS_FSTAT failed on file %s (%s)\n",
373 smb_fname_str_dbg(smb_fname),
374 strerror(errno));
375 return -1;
377 fsp->file_id = SMB_VFS_FILE_ID_CREATE(fsp->conn, &smb_fname->st);
379 frame = talloc_stackframe();
380 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
381 if (!xattr_tdb_init(-1, frame, &db))
383 TALLOC_FREE(frame); return -1;
386 xattr_tdb_remove_all_attrs(db, &fsp->file_id);
387 TALLOC_FREE(frame);
388 return fsp->fh->fd;
391 static int xattr_tdb_mkdir(vfs_handle_struct *handle,
392 const struct smb_filename *smb_fname,
393 mode_t mode)
395 struct db_context *db = NULL;
396 TALLOC_CTX *frame = NULL;
397 struct file_id fileid;
398 int ret;
399 struct smb_filename *smb_fname_tmp = NULL;
401 ret = SMB_VFS_NEXT_MKDIR(handle, smb_fname, mode);
402 if (ret < 0) {
403 return ret;
406 frame = talloc_stackframe();
407 smb_fname_tmp = cp_smb_filename(frame, smb_fname);
408 if (smb_fname_tmp == NULL) {
409 TALLOC_FREE(frame);
410 errno = ENOMEM;
411 return -1;
414 /* Always use LSTAT here - we just creaded the directory. */
415 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_tmp);
416 if (ret == -1) {
417 /* Rename race. Let upper level take care of it. */
418 TALLOC_FREE(frame);
419 return -1;
421 if (!S_ISDIR(smb_fname_tmp->st.st_ex_mode)) {
422 /* Rename race. Let upper level take care of it. */
423 TALLOC_FREE(frame);
424 return -1;
427 fileid = SMB_VFS_FILE_ID_CREATE(handle->conn, &smb_fname_tmp->st);
429 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
430 if (!xattr_tdb_init(-1, frame, &db))
432 TALLOC_FREE(frame); return -1;
435 xattr_tdb_remove_all_attrs(db, &fileid);
436 TALLOC_FREE(frame);
437 return 0;
441 * On unlink we need to delete the tdb record
443 static int xattr_tdb_unlink(vfs_handle_struct *handle,
444 const struct smb_filename *smb_fname)
446 struct smb_filename *smb_fname_tmp = NULL;
447 struct file_id id;
448 struct db_context *db;
449 int ret = -1;
450 bool remove_record = false;
451 TALLOC_CTX *frame = talloc_stackframe();
453 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
454 if (!xattr_tdb_init(-1, frame, &db))
456 TALLOC_FREE(frame); return -1;
459 smb_fname_tmp = cp_smb_filename(frame, smb_fname);
460 if (smb_fname_tmp == NULL) {
461 TALLOC_FREE(frame);
462 errno = ENOMEM;
463 return -1;
466 if (smb_fname_tmp->flags & SMB_FILENAME_POSIX_PATH) {
467 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_tmp);
468 } else {
469 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_tmp);
471 if (ret == -1) {
472 goto out;
475 if (smb_fname_tmp->st.st_ex_nlink == 1) {
476 /* Only remove record on last link to file. */
477 remove_record = true;
480 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
482 if (ret == -1) {
483 goto out;
486 if (!remove_record) {
487 goto out;
490 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname_tmp->st);
492 xattr_tdb_remove_all_attrs(db, &id);
494 out:
495 TALLOC_FREE(frame);
496 return ret;
500 * On rmdir we need to delete the tdb record
502 static int xattr_tdb_rmdir(vfs_handle_struct *handle,
503 const struct smb_filename *smb_fname)
505 SMB_STRUCT_STAT sbuf;
506 struct file_id id;
507 struct db_context *db;
508 int ret;
509 TALLOC_CTX *frame = talloc_stackframe();
511 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
512 if (!xattr_tdb_init(-1, frame, &db))
514 TALLOC_FREE(frame); return -1;
517 if (vfs_stat_smb_basename(handle->conn,
518 smb_fname,
519 &sbuf) == -1) {
520 TALLOC_FREE(frame);
521 return -1;
524 ret = SMB_VFS_NEXT_RMDIR(handle, smb_fname);
526 if (ret == -1) {
527 TALLOC_FREE(frame);
528 return -1;
531 id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
533 xattr_tdb_remove_all_attrs(db, &id);
535 TALLOC_FREE(frame);
536 return 0;
540 * Destructor for the VFS private data
543 static void close_xattr_db(void **data)
545 struct db_context **p_db = (struct db_context **)data;
546 TALLOC_FREE(*p_db);
549 static int xattr_tdb_connect(vfs_handle_struct *handle, const char *service,
550 const char *user)
552 char *sname = NULL;
553 int res, snum;
554 struct db_context *db;
556 res = SMB_VFS_NEXT_CONNECT(handle, service, user);
557 if (res < 0) {
558 return res;
561 snum = find_service(talloc_tos(), service, &sname);
562 if (snum == -1 || sname == NULL) {
564 * Should not happen, but we should not fail just *here*.
566 return 0;
569 if (!xattr_tdb_init(snum, NULL, &db)) {
570 DEBUG(5, ("Could not init xattr tdb\n"));
571 lp_do_parameter(snum, "ea support", "False");
572 return 0;
575 lp_do_parameter(snum, "ea support", "True");
577 SMB_VFS_HANDLE_SET_DATA(handle, db, close_xattr_db,
578 struct db_context, return -1);
580 return 0;
583 static struct vfs_fn_pointers vfs_xattr_tdb_fns = {
584 .getxattr_fn = xattr_tdb_getxattr,
585 .fgetxattr_fn = xattr_tdb_fgetxattr,
586 .setxattr_fn = xattr_tdb_setxattr,
587 .fsetxattr_fn = xattr_tdb_fsetxattr,
588 .listxattr_fn = xattr_tdb_listxattr,
589 .flistxattr_fn = xattr_tdb_flistxattr,
590 .removexattr_fn = xattr_tdb_removexattr,
591 .fremovexattr_fn = xattr_tdb_fremovexattr,
592 .open_fn = xattr_tdb_open,
593 .mkdir_fn = xattr_tdb_mkdir,
594 .unlink_fn = xattr_tdb_unlink,
595 .rmdir_fn = xattr_tdb_rmdir,
596 .connect_fn = xattr_tdb_connect,
599 NTSTATUS vfs_xattr_tdb_init(void);
600 NTSTATUS vfs_xattr_tdb_init(void)
602 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "xattr_tdb",
603 &vfs_xattr_tdb_fns);