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 "librpc/gen_ndr/xattr.h"
22 #include "librpc/gen_ndr/ndr_xattr.h"
25 #define DBGC_CLASS DBGC_VFS
28 * unmarshall tdb_xattrs
31 static NTSTATUS
xattr_tdb_pull_attrs(TALLOC_CTX
*mem_ctx
,
33 struct tdb_xattrs
**presult
)
36 enum ndr_err_code ndr_err
;
37 struct tdb_xattrs
*result
;
39 if (!(result
= TALLOC_ZERO_P(mem_ctx
, struct tdb_xattrs
))) {
40 return NT_STATUS_NO_MEMORY
;
43 if (data
->dsize
== 0) {
48 blob
= data_blob_const(data
->dptr
, data
->dsize
);
50 ndr_err
= ndr_pull_struct_blob(
51 &blob
, result
, NULL
, result
,
52 (ndr_pull_flags_fn_t
)ndr_pull_tdb_xattrs
);
54 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
55 DEBUG(0, ("ndr_pull_tdb_xattrs failed: %s\n",
56 ndr_errstr(ndr_err
)));
58 return ndr_map_error2ntstatus(ndr_err
);;
69 static NTSTATUS
xattr_tdb_push_attrs(TALLOC_CTX
*mem_ctx
,
70 const struct tdb_xattrs
*attribs
,
74 enum ndr_err_code ndr_err
;
76 ndr_err
= ndr_push_struct_blob(
77 &blob
, mem_ctx
, NULL
, attribs
,
78 (ndr_push_flags_fn_t
)ndr_push_tdb_xattrs
);
80 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
81 DEBUG(0, ("ndr_push_tdb_xattrs failed: %s\n",
82 ndr_errstr(ndr_err
)));
83 return ndr_map_error2ntstatus(ndr_err
);;
86 *data
= make_tdb_data(blob
.data
, blob
.length
);
91 * Load tdb_xattrs for a file from the tdb
94 static NTSTATUS
xattr_tdb_load_attrs(TALLOC_CTX
*mem_ctx
,
95 struct db_context
*db_ctx
,
96 const struct file_id
*id
,
97 struct tdb_xattrs
**presult
)
103 /* For backwards compatibility only store the dev/inode. */
104 push_file_id_16((char *)id_buf
, id
);
106 if (db_ctx
->fetch(db_ctx
, mem_ctx
,
107 make_tdb_data(id_buf
, sizeof(id_buf
)),
109 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
112 status
= xattr_tdb_pull_attrs(mem_ctx
, &data
, presult
);
113 TALLOC_FREE(data
.dptr
);
118 * fetch_lock the tdb_ea record for a file
121 static struct db_record
*xattr_tdb_lock_attrs(TALLOC_CTX
*mem_ctx
,
122 struct db_context
*db_ctx
,
123 const struct file_id
*id
)
127 /* For backwards compatibility only store the dev/inode. */
128 push_file_id_16((char *)id_buf
, id
);
129 return db_ctx
->fetch_locked(db_ctx
, mem_ctx
,
130 make_tdb_data(id_buf
, sizeof(id_buf
)));
134 * Save tdb_xattrs to a previously fetch_locked record
137 static NTSTATUS
xattr_tdb_save_attrs(struct db_record
*rec
,
138 const struct tdb_xattrs
*attribs
)
140 TDB_DATA data
= tdb_null
;
143 status
= xattr_tdb_push_attrs(talloc_tos(), attribs
, &data
);
145 if (!NT_STATUS_IS_OK(status
)) {
146 DEBUG(0, ("xattr_tdb_push_attrs failed: %s\n",
151 status
= rec
->store(rec
, data
, 0);
153 TALLOC_FREE(data
.dptr
);
159 * Worker routine for getxattr and fgetxattr
162 static ssize_t
xattr_tdb_getattr(struct db_context
*db_ctx
,
163 const struct file_id
*id
,
164 const char *name
, void *value
, size_t size
)
166 struct tdb_xattrs
*attribs
;
171 DEBUG(10, ("xattr_tdb_getattr called for file %s, name %s\n",
172 file_id_string_tos(id
), name
));
174 status
= xattr_tdb_load_attrs(talloc_tos(), db_ctx
, id
, &attribs
);
176 if (!NT_STATUS_IS_OK(status
)) {
177 DEBUG(10, ("xattr_tdb_fetch_attrs failed: %s\n",
183 for (i
=0; i
<attribs
->num_eas
; i
++) {
184 if (strcmp(attribs
->eas
[i
].name
, name
) == 0) {
189 if (i
== attribs
->num_eas
) {
194 if (attribs
->eas
[i
].value
.length
> size
) {
199 memcpy(value
, attribs
->eas
[i
].value
.data
,
200 attribs
->eas
[i
].value
.length
);
201 result
= attribs
->eas
[i
].value
.length
;
204 TALLOC_FREE(attribs
);
208 static ssize_t
xattr_tdb_getxattr(struct vfs_handle_struct
*handle
,
209 const char *path
, const char *name
,
210 void *value
, size_t size
)
212 SMB_STRUCT_STAT sbuf
;
214 struct db_context
*db
;
216 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
218 if (vfs_stat_smb_fname(handle
->conn
, path
, &sbuf
) == -1) {
222 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
224 return xattr_tdb_getattr(db
, &id
, name
, value
, size
);
227 static ssize_t
xattr_tdb_fgetxattr(struct vfs_handle_struct
*handle
,
228 struct files_struct
*fsp
,
229 const char *name
, void *value
, size_t size
)
231 SMB_STRUCT_STAT sbuf
;
233 struct db_context
*db
;
235 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
237 if (SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
241 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
243 return xattr_tdb_getattr(db
, &id
, name
, value
, size
);
247 * Worker routine for setxattr and fsetxattr
250 static int xattr_tdb_setattr(struct db_context
*db_ctx
,
251 const struct file_id
*id
, const char *name
,
252 const void *value
, size_t size
, int flags
)
255 struct db_record
*rec
;
256 struct tdb_xattrs
*attribs
;
259 DEBUG(10, ("xattr_tdb_setattr called for file %s, name %s\n",
260 file_id_string_tos(id
), name
));
262 rec
= xattr_tdb_lock_attrs(talloc_tos(), db_ctx
, id
);
265 DEBUG(0, ("xattr_tdb_lock_attrs failed\n"));
270 status
= xattr_tdb_pull_attrs(rec
, &rec
->value
, &attribs
);
272 if (!NT_STATUS_IS_OK(status
)) {
273 DEBUG(10, ("xattr_tdb_fetch_attrs failed: %s\n",
279 for (i
=0; i
<attribs
->num_eas
; i
++) {
280 if (strcmp(attribs
->eas
[i
].name
, name
) == 0) {
281 if (flags
& XATTR_CREATE
) {
290 if (i
== attribs
->num_eas
) {
291 struct xattr_EA
*tmp
;
293 if (flags
& XATTR_REPLACE
) {
299 tmp
= TALLOC_REALLOC_ARRAY(
300 attribs
, attribs
->eas
, struct xattr_EA
,
301 attribs
->num_eas
+ 1);
304 DEBUG(0, ("TALLOC_REALLOC_ARRAY failed\n"));
311 attribs
->num_eas
+= 1;
314 attribs
->eas
[i
].name
= name
;
315 attribs
->eas
[i
].value
.data
= CONST_DISCARD(uint8
*, value
);
316 attribs
->eas
[i
].value
.length
= size
;
318 status
= xattr_tdb_save_attrs(rec
, attribs
);
322 if (!NT_STATUS_IS_OK(status
)) {
323 DEBUG(1, ("save failed: %s\n", nt_errstr(status
)));
330 static int xattr_tdb_setxattr(struct vfs_handle_struct
*handle
,
331 const char *path
, const char *name
,
332 const void *value
, size_t size
, int flags
)
334 SMB_STRUCT_STAT sbuf
;
336 struct db_context
*db
;
338 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
340 if (vfs_stat_smb_fname(handle
->conn
, path
, &sbuf
) == -1) {
344 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
346 return xattr_tdb_setattr(db
, &id
, name
, value
, size
, flags
);
349 static int xattr_tdb_fsetxattr(struct vfs_handle_struct
*handle
,
350 struct files_struct
*fsp
,
351 const char *name
, const void *value
,
352 size_t size
, int flags
)
354 SMB_STRUCT_STAT sbuf
;
356 struct db_context
*db
;
358 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
360 if (SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
364 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
366 return xattr_tdb_setattr(db
, &id
, name
, value
, size
, flags
);
370 * Worker routine for listxattr and flistxattr
373 static ssize_t
xattr_tdb_listattr(struct db_context
*db_ctx
,
374 const struct file_id
*id
, char *list
,
378 struct tdb_xattrs
*attribs
;
382 status
= xattr_tdb_load_attrs(talloc_tos(), db_ctx
, id
, &attribs
);
384 if (!NT_STATUS_IS_OK(status
)) {
385 DEBUG(10, ("xattr_tdb_fetch_attrs failed: %s\n",
391 DEBUG(10, ("xattr_tdb_listattr: Found %d xattrs\n",
394 for (i
=0; i
<attribs
->num_eas
; i
++) {
397 DEBUG(10, ("xattr_tdb_listattr: xattrs[i].name: %s\n",
398 attribs
->eas
[i
].name
));
400 tmp
= strlen(attribs
->eas
[i
].name
);
403 * Try to protect against overflow
406 if (len
+ (tmp
+1) < len
) {
407 TALLOC_FREE(attribs
);
413 * Take care of the terminating NULL
419 TALLOC_FREE(attribs
);
426 for (i
=0; i
<attribs
->num_eas
; i
++) {
427 strlcpy(list
+len
, attribs
->eas
[i
].name
,
429 len
+= (strlen(attribs
->eas
[i
].name
) + 1);
432 TALLOC_FREE(attribs
);
436 static ssize_t
xattr_tdb_listxattr(struct vfs_handle_struct
*handle
,
437 const char *path
, char *list
, size_t size
)
439 SMB_STRUCT_STAT sbuf
;
441 struct db_context
*db
;
443 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
445 if (vfs_stat_smb_fname(handle
->conn
, path
, &sbuf
) == -1) {
449 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
451 return xattr_tdb_listattr(db
, &id
, list
, size
);
454 static ssize_t
xattr_tdb_flistxattr(struct vfs_handle_struct
*handle
,
455 struct files_struct
*fsp
, char *list
,
458 SMB_STRUCT_STAT sbuf
;
460 struct db_context
*db
;
462 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
464 if (SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
468 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
470 return xattr_tdb_listattr(db
, &id
, list
, size
);
474 * Worker routine for removexattr and fremovexattr
477 static int xattr_tdb_removeattr(struct db_context
*db_ctx
,
478 const struct file_id
*id
, const char *name
)
481 struct db_record
*rec
;
482 struct tdb_xattrs
*attribs
;
485 rec
= xattr_tdb_lock_attrs(talloc_tos(), db_ctx
, id
);
488 DEBUG(0, ("xattr_tdb_lock_attrs failed\n"));
493 status
= xattr_tdb_pull_attrs(rec
, &rec
->value
, &attribs
);
495 if (!NT_STATUS_IS_OK(status
)) {
496 DEBUG(10, ("xattr_tdb_fetch_attrs failed: %s\n",
502 for (i
=0; i
<attribs
->num_eas
; i
++) {
503 if (strcmp(attribs
->eas
[i
].name
, name
) == 0) {
508 if (i
== attribs
->num_eas
) {
515 attribs
->eas
[attribs
->num_eas
-1];
516 attribs
->num_eas
-= 1;
518 if (attribs
->num_eas
== 0) {
519 rec
->delete_rec(rec
);
524 status
= xattr_tdb_save_attrs(rec
, attribs
);
528 if (!NT_STATUS_IS_OK(status
)) {
529 DEBUG(1, ("save failed: %s\n", nt_errstr(status
)));
536 static int xattr_tdb_removexattr(struct vfs_handle_struct
*handle
,
537 const char *path
, const char *name
)
539 SMB_STRUCT_STAT sbuf
;
541 struct db_context
*db
;
543 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
545 if (vfs_stat_smb_fname(handle
->conn
, path
, &sbuf
) == -1) {
549 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
551 return xattr_tdb_removeattr(db
, &id
, name
);
554 static int xattr_tdb_fremovexattr(struct vfs_handle_struct
*handle
,
555 struct files_struct
*fsp
, const char *name
)
557 SMB_STRUCT_STAT sbuf
;
559 struct db_context
*db
;
561 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
563 if (SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
567 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
569 return xattr_tdb_removeattr(db
, &id
, name
);
573 * Open the tdb file upon VFS_CONNECT
576 static bool xattr_tdb_init(int snum
, struct db_context
**p_db
)
578 struct db_context
*db
;
582 def_dbname
= state_path("xattr.tdb");
583 if (def_dbname
== NULL
) {
588 dbname
= lp_parm_const_string(snum
, "xattr_tdb", "file", def_dbname
);
590 /* now we know dbname is not NULL */
593 db
= db_open(NULL
, dbname
, 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
602 TALLOC_FREE(def_dbname
);
607 TALLOC_FREE(def_dbname
);
612 * On unlink we need to delete the tdb record
614 static int xattr_tdb_unlink(vfs_handle_struct
*handle
,
615 const struct smb_filename
*smb_fname
)
617 struct smb_filename
*smb_fname_tmp
= NULL
;
619 struct db_context
*db
;
620 struct db_record
*rec
;
623 bool remove_record
= false;
625 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
627 status
= copy_smb_filename(talloc_tos(), smb_fname
, &smb_fname_tmp
);
628 if (!NT_STATUS_IS_OK(status
)) {
629 errno
= map_errno_from_nt_status(status
);
633 if (lp_posix_pathnames()) {
634 ret
= SMB_VFS_LSTAT(handle
->conn
, smb_fname_tmp
);
636 ret
= SMB_VFS_STAT(handle
->conn
, smb_fname_tmp
);
642 if (smb_fname_tmp
->st
.st_ex_nlink
== 1) {
643 /* Only remove record on last link to file. */
644 remove_record
= true;
647 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname_tmp
);
653 if (!remove_record
) {
657 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &smb_fname_tmp
->st
);
659 rec
= xattr_tdb_lock_attrs(talloc_tos(), db
, &id
);
662 * If rec == NULL there's not much we can do about it
666 rec
->delete_rec(rec
);
671 TALLOC_FREE(smb_fname_tmp
);
676 * On rmdir we need to delete the tdb record
678 static int xattr_tdb_rmdir(vfs_handle_struct
*handle
, const char *path
)
680 SMB_STRUCT_STAT sbuf
;
682 struct db_context
*db
;
683 struct db_record
*rec
;
686 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
688 if (vfs_stat_smb_fname(handle
->conn
, path
, &sbuf
) == -1) {
692 ret
= SMB_VFS_NEXT_RMDIR(handle
, path
);
698 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
700 rec
= xattr_tdb_lock_attrs(talloc_tos(), db
, &id
);
703 * If rec == NULL there's not much we can do about it
707 rec
->delete_rec(rec
);
715 * Destructor for the VFS private data
718 static void close_xattr_db(void **data
)
720 struct db_context
**p_db
= (struct db_context
**)data
;
724 static int xattr_tdb_connect(vfs_handle_struct
*handle
, const char *service
,
729 struct db_context
*db
;
731 res
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
736 fstrcpy(sname
, service
);
737 snum
= find_service(sname
);
740 * Should not happen, but we should not fail just *here*.
745 if (!xattr_tdb_init(snum
, &db
)) {
746 DEBUG(5, ("Could not init xattr tdb\n"));
747 lp_do_parameter(snum
, "ea support", "False");
751 lp_do_parameter(snum
, "ea support", "True");
753 SMB_VFS_HANDLE_SET_DATA(handle
, db
, close_xattr_db
,
754 struct db_context
, return -1);
759 static struct vfs_fn_pointers vfs_xattr_tdb_fns
= {
760 .getxattr
= xattr_tdb_getxattr
,
761 .fgetxattr
= xattr_tdb_fgetxattr
,
762 .setxattr
= xattr_tdb_setxattr
,
763 .fsetxattr
= xattr_tdb_fsetxattr
,
764 .listxattr
= xattr_tdb_listxattr
,
765 .flistxattr
= xattr_tdb_flistxattr
,
766 .removexattr
= xattr_tdb_removexattr
,
767 .fremovexattr
= xattr_tdb_fremovexattr
,
768 .unlink
= xattr_tdb_unlink
,
769 .rmdir
= xattr_tdb_rmdir
,
770 .connect_fn
= xattr_tdb_connect
,
773 NTSTATUS
vfs_xattr_tdb_init(void);
774 NTSTATUS
vfs_xattr_tdb_init(void)
776 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "xattr_tdb",