s3-docs: Fix typo.
[Samba/gebeck_regimport.git] / source3 / modules / vfs_xattr_tdb.c
blobbec12310c281bfe601a7c5260b1dcdecbdc60f44
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 "librpc/gen_ndr/xattr.h"
24 #include "librpc/gen_ndr/ndr_xattr.h"
25 #include "../librpc/gen_ndr/ndr_netlogon.h"
26 #include "dbwrap/dbwrap.h"
27 #include "dbwrap/dbwrap_open.h"
28 #include "util_tdb.h"
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_VFS
34 * unmarshall tdb_xattrs
37 static NTSTATUS xattr_tdb_pull_attrs(TALLOC_CTX *mem_ctx,
38 const TDB_DATA *data,
39 struct tdb_xattrs **presult)
41 DATA_BLOB blob;
42 enum ndr_err_code ndr_err;
43 struct tdb_xattrs *result;
45 if (!(result = talloc_zero(mem_ctx, struct tdb_xattrs))) {
46 return NT_STATUS_NO_MEMORY;
49 if (data->dsize == 0) {
50 *presult = result;
51 return NT_STATUS_OK;
54 blob = data_blob_const(data->dptr, data->dsize);
56 ndr_err = ndr_pull_struct_blob(&blob, result, result,
57 (ndr_pull_flags_fn_t)ndr_pull_tdb_xattrs);
59 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
60 DEBUG(0, ("ndr_pull_tdb_xattrs failed: %s\n",
61 ndr_errstr(ndr_err)));
62 TALLOC_FREE(result);
63 return ndr_map_error2ntstatus(ndr_err);
66 *presult = result;
67 return NT_STATUS_OK;
71 * marshall tdb_xattrs
74 static NTSTATUS xattr_tdb_push_attrs(TALLOC_CTX *mem_ctx,
75 const struct tdb_xattrs *attribs,
76 TDB_DATA *data)
78 DATA_BLOB blob;
79 enum ndr_err_code ndr_err;
81 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, attribs,
82 (ndr_push_flags_fn_t)ndr_push_tdb_xattrs);
84 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
85 DEBUG(0, ("ndr_push_tdb_xattrs failed: %s\n",
86 ndr_errstr(ndr_err)));
87 return ndr_map_error2ntstatus(ndr_err);
90 *data = make_tdb_data(blob.data, blob.length);
91 return NT_STATUS_OK;
95 * Load tdb_xattrs for a file from the tdb
98 static NTSTATUS xattr_tdb_load_attrs(TALLOC_CTX *mem_ctx,
99 struct db_context *db_ctx,
100 const struct file_id *id,
101 struct tdb_xattrs **presult)
103 uint8 id_buf[16];
104 NTSTATUS status;
105 TDB_DATA data;
107 /* For backwards compatibility only store the dev/inode. */
108 push_file_id_16((char *)id_buf, id);
110 if (db_ctx->fetch(db_ctx, mem_ctx,
111 make_tdb_data(id_buf, sizeof(id_buf)),
112 &data) != 0) {
113 return NT_STATUS_INTERNAL_DB_CORRUPTION;
116 status = xattr_tdb_pull_attrs(mem_ctx, &data, presult);
117 TALLOC_FREE(data.dptr);
118 return status;
122 * fetch_lock the tdb_ea record for a file
125 static struct db_record *xattr_tdb_lock_attrs(TALLOC_CTX *mem_ctx,
126 struct db_context *db_ctx,
127 const struct file_id *id)
129 uint8 id_buf[16];
131 /* For backwards compatibility only store the dev/inode. */
132 push_file_id_16((char *)id_buf, id);
133 return db_ctx->fetch_locked(db_ctx, mem_ctx,
134 make_tdb_data(id_buf, sizeof(id_buf)));
138 * Save tdb_xattrs to a previously fetch_locked record
141 static NTSTATUS xattr_tdb_save_attrs(struct db_record *rec,
142 const struct tdb_xattrs *attribs)
144 TDB_DATA data = tdb_null;
145 NTSTATUS status;
147 status = xattr_tdb_push_attrs(talloc_tos(), attribs, &data);
149 if (!NT_STATUS_IS_OK(status)) {
150 DEBUG(0, ("xattr_tdb_push_attrs failed: %s\n",
151 nt_errstr(status)));
152 return status;
155 status = rec->store(rec, data, 0);
157 TALLOC_FREE(data.dptr);
159 return status;
163 * Worker routine for getxattr and fgetxattr
166 static ssize_t xattr_tdb_getattr(struct db_context *db_ctx,
167 const struct file_id *id,
168 const char *name, void *value, size_t size)
170 struct tdb_xattrs *attribs;
171 uint32_t i;
172 ssize_t result = -1;
173 NTSTATUS status;
175 DEBUG(10, ("xattr_tdb_getattr called for file %s, name %s\n",
176 file_id_string_tos(id), name));
178 status = xattr_tdb_load_attrs(talloc_tos(), db_ctx, id, &attribs);
180 if (!NT_STATUS_IS_OK(status)) {
181 DEBUG(10, ("xattr_tdb_fetch_attrs failed: %s\n",
182 nt_errstr(status)));
183 errno = EINVAL;
184 return -1;
187 for (i=0; i<attribs->num_eas; i++) {
188 if (strcmp(attribs->eas[i].name, name) == 0) {
189 break;
193 if (i == attribs->num_eas) {
194 errno = ENOATTR;
195 goto fail;
198 if (attribs->eas[i].value.length > size) {
199 errno = ERANGE;
200 goto fail;
203 memcpy(value, attribs->eas[i].value.data,
204 attribs->eas[i].value.length);
205 result = attribs->eas[i].value.length;
207 fail:
208 TALLOC_FREE(attribs);
209 return result;
212 static ssize_t xattr_tdb_getxattr(struct vfs_handle_struct *handle,
213 const char *path, const char *name,
214 void *value, size_t size)
216 SMB_STRUCT_STAT sbuf;
217 struct file_id id;
218 struct db_context *db;
220 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
222 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
223 return -1;
226 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
228 return xattr_tdb_getattr(db, &id, name, value, size);
231 static ssize_t xattr_tdb_fgetxattr(struct vfs_handle_struct *handle,
232 struct files_struct *fsp,
233 const char *name, void *value, size_t size)
235 SMB_STRUCT_STAT sbuf;
236 struct file_id id;
237 struct db_context *db;
239 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
241 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
242 return -1;
245 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
247 return xattr_tdb_getattr(db, &id, name, value, size);
251 * Worker routine for setxattr and fsetxattr
254 static int xattr_tdb_setattr(struct db_context *db_ctx,
255 const struct file_id *id, const char *name,
256 const void *value, size_t size, int flags)
258 NTSTATUS status;
259 struct db_record *rec;
260 struct tdb_xattrs *attribs;
261 uint32_t i;
263 DEBUG(10, ("xattr_tdb_setattr called for file %s, name %s\n",
264 file_id_string_tos(id), name));
266 rec = xattr_tdb_lock_attrs(talloc_tos(), db_ctx, id);
268 if (rec == NULL) {
269 DEBUG(0, ("xattr_tdb_lock_attrs failed\n"));
270 errno = EINVAL;
271 return -1;
274 status = xattr_tdb_pull_attrs(rec, &rec->value, &attribs);
276 if (!NT_STATUS_IS_OK(status)) {
277 DEBUG(10, ("xattr_tdb_fetch_attrs failed: %s\n",
278 nt_errstr(status)));
279 TALLOC_FREE(rec);
280 return -1;
283 for (i=0; i<attribs->num_eas; i++) {
284 if (strcmp(attribs->eas[i].name, name) == 0) {
285 if (flags & XATTR_CREATE) {
286 TALLOC_FREE(rec);
287 errno = EEXIST;
288 return -1;
290 break;
294 if (i == attribs->num_eas) {
295 struct xattr_EA *tmp;
297 if (flags & XATTR_REPLACE) {
298 TALLOC_FREE(rec);
299 errno = ENOATTR;
300 return -1;
303 tmp = talloc_realloc(
304 attribs, attribs->eas, struct xattr_EA,
305 attribs->num_eas+ 1);
307 if (tmp == NULL) {
308 DEBUG(0, ("talloc_realloc failed\n"));
309 TALLOC_FREE(rec);
310 errno = ENOMEM;
311 return -1;
314 attribs->eas = tmp;
315 attribs->num_eas += 1;
318 attribs->eas[i].name = name;
319 attribs->eas[i].value.data = discard_const_p(uint8, value);
320 attribs->eas[i].value.length = size;
322 status = xattr_tdb_save_attrs(rec, attribs);
324 TALLOC_FREE(rec);
326 if (!NT_STATUS_IS_OK(status)) {
327 DEBUG(1, ("save failed: %s\n", nt_errstr(status)));
328 return -1;
331 return 0;
334 static int xattr_tdb_setxattr(struct vfs_handle_struct *handle,
335 const char *path, const char *name,
336 const void *value, size_t size, int flags)
338 SMB_STRUCT_STAT sbuf;
339 struct file_id id;
340 struct db_context *db;
342 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
344 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
345 return -1;
348 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
350 return xattr_tdb_setattr(db, &id, name, value, size, flags);
353 static int xattr_tdb_fsetxattr(struct vfs_handle_struct *handle,
354 struct files_struct *fsp,
355 const char *name, const void *value,
356 size_t size, int flags)
358 SMB_STRUCT_STAT sbuf;
359 struct file_id id;
360 struct db_context *db;
362 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
364 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
365 return -1;
368 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
370 return xattr_tdb_setattr(db, &id, name, value, size, flags);
374 * Worker routine for listxattr and flistxattr
377 static ssize_t xattr_tdb_listattr(struct db_context *db_ctx,
378 const struct file_id *id, char *list,
379 size_t size)
381 NTSTATUS status;
382 struct tdb_xattrs *attribs;
383 uint32_t i;
384 size_t len = 0;
386 status = xattr_tdb_load_attrs(talloc_tos(), db_ctx, id, &attribs);
388 if (!NT_STATUS_IS_OK(status)) {
389 DEBUG(10, ("xattr_tdb_fetch_attrs failed: %s\n",
390 nt_errstr(status)));
391 errno = EINVAL;
392 return -1;
395 DEBUG(10, ("xattr_tdb_listattr: Found %d xattrs\n",
396 attribs->num_eas));
398 for (i=0; i<attribs->num_eas; i++) {
399 size_t tmp;
401 DEBUG(10, ("xattr_tdb_listattr: xattrs[i].name: %s\n",
402 attribs->eas[i].name));
404 tmp = strlen(attribs->eas[i].name);
407 * Try to protect against overflow
410 if (len + (tmp+1) < len) {
411 TALLOC_FREE(attribs);
412 errno = EINVAL;
413 return -1;
417 * Take care of the terminating NULL
419 len += (tmp + 1);
422 if (len > size) {
423 TALLOC_FREE(attribs);
424 errno = ERANGE;
425 return -1;
428 len = 0;
430 for (i=0; i<attribs->num_eas; i++) {
431 strlcpy(list+len, attribs->eas[i].name,
432 size-len);
433 len += (strlen(attribs->eas[i].name) + 1);
436 TALLOC_FREE(attribs);
437 return len;
440 static ssize_t xattr_tdb_listxattr(struct vfs_handle_struct *handle,
441 const char *path, char *list, size_t size)
443 SMB_STRUCT_STAT sbuf;
444 struct file_id id;
445 struct db_context *db;
447 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
449 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
450 return -1;
453 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
455 return xattr_tdb_listattr(db, &id, list, size);
458 static ssize_t xattr_tdb_flistxattr(struct vfs_handle_struct *handle,
459 struct files_struct *fsp, char *list,
460 size_t size)
462 SMB_STRUCT_STAT sbuf;
463 struct file_id id;
464 struct db_context *db;
466 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
468 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
469 return -1;
472 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
474 return xattr_tdb_listattr(db, &id, list, size);
478 * Worker routine for removexattr and fremovexattr
481 static int xattr_tdb_removeattr(struct db_context *db_ctx,
482 const struct file_id *id, const char *name)
484 NTSTATUS status;
485 struct db_record *rec;
486 struct tdb_xattrs *attribs;
487 uint32_t i;
489 rec = xattr_tdb_lock_attrs(talloc_tos(), db_ctx, id);
491 if (rec == NULL) {
492 DEBUG(0, ("xattr_tdb_lock_attrs failed\n"));
493 errno = EINVAL;
494 return -1;
497 status = xattr_tdb_pull_attrs(rec, &rec->value, &attribs);
499 if (!NT_STATUS_IS_OK(status)) {
500 DEBUG(10, ("xattr_tdb_fetch_attrs failed: %s\n",
501 nt_errstr(status)));
502 TALLOC_FREE(rec);
503 return -1;
506 for (i=0; i<attribs->num_eas; i++) {
507 if (strcmp(attribs->eas[i].name, name) == 0) {
508 break;
512 if (i == attribs->num_eas) {
513 TALLOC_FREE(rec);
514 errno = ENOATTR;
515 return -1;
518 attribs->eas[i] =
519 attribs->eas[attribs->num_eas-1];
520 attribs->num_eas -= 1;
522 if (attribs->num_eas == 0) {
523 rec->delete_rec(rec);
524 TALLOC_FREE(rec);
525 return 0;
528 status = xattr_tdb_save_attrs(rec, attribs);
530 TALLOC_FREE(rec);
532 if (!NT_STATUS_IS_OK(status)) {
533 DEBUG(1, ("save failed: %s\n", nt_errstr(status)));
534 return -1;
537 return 0;
540 static int xattr_tdb_removexattr(struct vfs_handle_struct *handle,
541 const char *path, const char *name)
543 SMB_STRUCT_STAT sbuf;
544 struct file_id id;
545 struct db_context *db;
547 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
549 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
550 return -1;
553 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
555 return xattr_tdb_removeattr(db, &id, name);
558 static int xattr_tdb_fremovexattr(struct vfs_handle_struct *handle,
559 struct files_struct *fsp, const char *name)
561 SMB_STRUCT_STAT sbuf;
562 struct file_id id;
563 struct db_context *db;
565 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
567 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
568 return -1;
571 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
573 return xattr_tdb_removeattr(db, &id, name);
577 * Open the tdb file upon VFS_CONNECT
580 static bool xattr_tdb_init(int snum, struct db_context **p_db)
582 struct db_context *db;
583 const char *dbname;
584 char *def_dbname;
586 def_dbname = state_path("xattr.tdb");
587 if (def_dbname == NULL) {
588 errno = ENOSYS;
589 return false;
592 dbname = lp_parm_const_string(snum, "xattr_tdb", "file", def_dbname);
594 /* now we know dbname is not NULL */
596 become_root();
597 db = db_open(NULL, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
598 unbecome_root();
600 if (db == NULL) {
601 #if defined(ENOTSUP)
602 errno = ENOTSUP;
603 #else
604 errno = ENOSYS;
605 #endif
606 TALLOC_FREE(def_dbname);
607 return false;
610 *p_db = db;
611 TALLOC_FREE(def_dbname);
612 return true;
616 * On unlink we need to delete the tdb record
618 static int xattr_tdb_unlink(vfs_handle_struct *handle,
619 const struct smb_filename *smb_fname)
621 struct smb_filename *smb_fname_tmp = NULL;
622 struct file_id id;
623 struct db_context *db;
624 struct db_record *rec;
625 NTSTATUS status;
626 int ret = -1;
627 bool remove_record = false;
629 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
631 status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp);
632 if (!NT_STATUS_IS_OK(status)) {
633 errno = map_errno_from_nt_status(status);
634 return -1;
637 if (lp_posix_pathnames()) {
638 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_tmp);
639 } else {
640 ret = SMB_VFS_STAT(handle->conn, smb_fname_tmp);
642 if (ret == -1) {
643 goto out;
646 if (smb_fname_tmp->st.st_ex_nlink == 1) {
647 /* Only remove record on last link to file. */
648 remove_record = true;
651 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
653 if (ret == -1) {
654 goto out;
657 if (!remove_record) {
658 goto out;
661 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &smb_fname_tmp->st);
663 rec = xattr_tdb_lock_attrs(talloc_tos(), db, &id);
666 * If rec == NULL there's not much we can do about it
669 if (rec != NULL) {
670 rec->delete_rec(rec);
671 TALLOC_FREE(rec);
674 out:
675 TALLOC_FREE(smb_fname_tmp);
676 return ret;
680 * On rmdir we need to delete the tdb record
682 static int xattr_tdb_rmdir(vfs_handle_struct *handle, const char *path)
684 SMB_STRUCT_STAT sbuf;
685 struct file_id id;
686 struct db_context *db;
687 struct db_record *rec;
688 int ret;
690 SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
692 if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
693 return -1;
696 ret = SMB_VFS_NEXT_RMDIR(handle, path);
698 if (ret == -1) {
699 return -1;
702 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
704 rec = xattr_tdb_lock_attrs(talloc_tos(), db, &id);
707 * If rec == NULL there's not much we can do about it
710 if (rec != NULL) {
711 rec->delete_rec(rec);
712 TALLOC_FREE(rec);
715 return 0;
719 * Destructor for the VFS private data
722 static void close_xattr_db(void **data)
724 struct db_context **p_db = (struct db_context **)data;
725 TALLOC_FREE(*p_db);
728 static int xattr_tdb_connect(vfs_handle_struct *handle, const char *service,
729 const char *user)
731 char *sname = NULL;
732 int res, snum;
733 struct db_context *db;
735 res = SMB_VFS_NEXT_CONNECT(handle, service, user);
736 if (res < 0) {
737 return res;
740 snum = find_service(talloc_tos(), service, &sname);
741 if (snum == -1 || sname == NULL) {
743 * Should not happen, but we should not fail just *here*.
745 return 0;
748 if (!xattr_tdb_init(snum, &db)) {
749 DEBUG(5, ("Could not init xattr tdb\n"));
750 lp_do_parameter(snum, "ea support", "False");
751 return 0;
754 lp_do_parameter(snum, "ea support", "True");
756 SMB_VFS_HANDLE_SET_DATA(handle, db, close_xattr_db,
757 struct db_context, return -1);
759 return 0;
762 static struct vfs_fn_pointers vfs_xattr_tdb_fns = {
763 .getxattr = xattr_tdb_getxattr,
764 .fgetxattr = xattr_tdb_fgetxattr,
765 .setxattr = xattr_tdb_setxattr,
766 .fsetxattr = xattr_tdb_fsetxattr,
767 .listxattr = xattr_tdb_listxattr,
768 .flistxattr = xattr_tdb_flistxattr,
769 .removexattr = xattr_tdb_removexattr,
770 .fremovexattr = xattr_tdb_fremovexattr,
771 .unlink = xattr_tdb_unlink,
772 .rmdir = xattr_tdb_rmdir,
773 .connect_fn = xattr_tdb_connect,
776 NTSTATUS vfs_xattr_tdb_init(void);
777 NTSTATUS vfs_xattr_tdb_init(void)
779 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "xattr_tdb",
780 &vfs_xattr_tdb_fns);