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(&blob
, result
, result
,
51 (ndr_pull_flags_fn_t
)ndr_pull_tdb_xattrs
);
53 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
54 DEBUG(0, ("ndr_pull_tdb_xattrs failed: %s\n",
55 ndr_errstr(ndr_err
)));
57 return ndr_map_error2ntstatus(ndr_err
);;
68 static NTSTATUS
xattr_tdb_push_attrs(TALLOC_CTX
*mem_ctx
,
69 const struct tdb_xattrs
*attribs
,
73 enum ndr_err_code ndr_err
;
75 ndr_err
= ndr_push_struct_blob(&blob
, mem_ctx
, attribs
,
76 (ndr_push_flags_fn_t
)ndr_push_tdb_xattrs
);
78 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
79 DEBUG(0, ("ndr_push_tdb_xattrs failed: %s\n",
80 ndr_errstr(ndr_err
)));
81 return ndr_map_error2ntstatus(ndr_err
);;
84 *data
= make_tdb_data(blob
.data
, blob
.length
);
89 * Load tdb_xattrs for a file from the tdb
92 static NTSTATUS
xattr_tdb_load_attrs(TALLOC_CTX
*mem_ctx
,
93 struct db_context
*db_ctx
,
94 const struct file_id
*id
,
95 struct tdb_xattrs
**presult
)
101 /* For backwards compatibility only store the dev/inode. */
102 push_file_id_16((char *)id_buf
, id
);
104 if (db_ctx
->fetch(db_ctx
, mem_ctx
,
105 make_tdb_data(id_buf
, sizeof(id_buf
)),
107 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
110 status
= xattr_tdb_pull_attrs(mem_ctx
, &data
, presult
);
111 TALLOC_FREE(data
.dptr
);
116 * fetch_lock the tdb_ea record for a file
119 static struct db_record
*xattr_tdb_lock_attrs(TALLOC_CTX
*mem_ctx
,
120 struct db_context
*db_ctx
,
121 const struct file_id
*id
)
125 /* For backwards compatibility only store the dev/inode. */
126 push_file_id_16((char *)id_buf
, id
);
127 return db_ctx
->fetch_locked(db_ctx
, mem_ctx
,
128 make_tdb_data(id_buf
, sizeof(id_buf
)));
132 * Save tdb_xattrs to a previously fetch_locked record
135 static NTSTATUS
xattr_tdb_save_attrs(struct db_record
*rec
,
136 const struct tdb_xattrs
*attribs
)
138 TDB_DATA data
= tdb_null
;
141 status
= xattr_tdb_push_attrs(talloc_tos(), attribs
, &data
);
143 if (!NT_STATUS_IS_OK(status
)) {
144 DEBUG(0, ("xattr_tdb_push_attrs failed: %s\n",
149 status
= rec
->store(rec
, data
, 0);
151 TALLOC_FREE(data
.dptr
);
157 * Worker routine for getxattr and fgetxattr
160 static ssize_t
xattr_tdb_getattr(struct db_context
*db_ctx
,
161 const struct file_id
*id
,
162 const char *name
, void *value
, size_t size
)
164 struct tdb_xattrs
*attribs
;
169 DEBUG(10, ("xattr_tdb_getattr called for file %s, name %s\n",
170 file_id_string_tos(id
), name
));
172 status
= xattr_tdb_load_attrs(talloc_tos(), db_ctx
, id
, &attribs
);
174 if (!NT_STATUS_IS_OK(status
)) {
175 DEBUG(10, ("xattr_tdb_fetch_attrs failed: %s\n",
181 for (i
=0; i
<attribs
->num_eas
; i
++) {
182 if (strcmp(attribs
->eas
[i
].name
, name
) == 0) {
187 if (i
== attribs
->num_eas
) {
192 if (attribs
->eas
[i
].value
.length
> size
) {
197 memcpy(value
, attribs
->eas
[i
].value
.data
,
198 attribs
->eas
[i
].value
.length
);
199 result
= attribs
->eas
[i
].value
.length
;
202 TALLOC_FREE(attribs
);
206 static ssize_t
xattr_tdb_getxattr(struct vfs_handle_struct
*handle
,
207 const char *path
, const char *name
,
208 void *value
, size_t size
)
210 SMB_STRUCT_STAT sbuf
;
212 struct db_context
*db
;
214 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
216 if (vfs_stat_smb_fname(handle
->conn
, path
, &sbuf
) == -1) {
220 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
222 return xattr_tdb_getattr(db
, &id
, name
, value
, size
);
225 static ssize_t
xattr_tdb_fgetxattr(struct vfs_handle_struct
*handle
,
226 struct files_struct
*fsp
,
227 const char *name
, void *value
, size_t size
)
229 SMB_STRUCT_STAT sbuf
;
231 struct db_context
*db
;
233 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
235 if (SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
239 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
241 return xattr_tdb_getattr(db
, &id
, name
, value
, size
);
245 * Worker routine for setxattr and fsetxattr
248 static int xattr_tdb_setattr(struct db_context
*db_ctx
,
249 const struct file_id
*id
, const char *name
,
250 const void *value
, size_t size
, int flags
)
253 struct db_record
*rec
;
254 struct tdb_xattrs
*attribs
;
257 DEBUG(10, ("xattr_tdb_setattr called for file %s, name %s\n",
258 file_id_string_tos(id
), name
));
260 rec
= xattr_tdb_lock_attrs(talloc_tos(), db_ctx
, id
);
263 DEBUG(0, ("xattr_tdb_lock_attrs failed\n"));
268 status
= xattr_tdb_pull_attrs(rec
, &rec
->value
, &attribs
);
270 if (!NT_STATUS_IS_OK(status
)) {
271 DEBUG(10, ("xattr_tdb_fetch_attrs failed: %s\n",
277 for (i
=0; i
<attribs
->num_eas
; i
++) {
278 if (strcmp(attribs
->eas
[i
].name
, name
) == 0) {
279 if (flags
& XATTR_CREATE
) {
288 if (i
== attribs
->num_eas
) {
289 struct xattr_EA
*tmp
;
291 if (flags
& XATTR_REPLACE
) {
297 tmp
= TALLOC_REALLOC_ARRAY(
298 attribs
, attribs
->eas
, struct xattr_EA
,
299 attribs
->num_eas
+ 1);
302 DEBUG(0, ("TALLOC_REALLOC_ARRAY failed\n"));
309 attribs
->num_eas
+= 1;
312 attribs
->eas
[i
].name
= name
;
313 attribs
->eas
[i
].value
.data
= CONST_DISCARD(uint8
*, value
);
314 attribs
->eas
[i
].value
.length
= size
;
316 status
= xattr_tdb_save_attrs(rec
, attribs
);
320 if (!NT_STATUS_IS_OK(status
)) {
321 DEBUG(1, ("save failed: %s\n", nt_errstr(status
)));
328 static int xattr_tdb_setxattr(struct vfs_handle_struct
*handle
,
329 const char *path
, const char *name
,
330 const void *value
, size_t size
, int flags
)
332 SMB_STRUCT_STAT sbuf
;
334 struct db_context
*db
;
336 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
338 if (vfs_stat_smb_fname(handle
->conn
, path
, &sbuf
) == -1) {
342 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
344 return xattr_tdb_setattr(db
, &id
, name
, value
, size
, flags
);
347 static int xattr_tdb_fsetxattr(struct vfs_handle_struct
*handle
,
348 struct files_struct
*fsp
,
349 const char *name
, const void *value
,
350 size_t size
, int flags
)
352 SMB_STRUCT_STAT sbuf
;
354 struct db_context
*db
;
356 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
358 if (SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
362 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
364 return xattr_tdb_setattr(db
, &id
, name
, value
, size
, flags
);
368 * Worker routine for listxattr and flistxattr
371 static ssize_t
xattr_tdb_listattr(struct db_context
*db_ctx
,
372 const struct file_id
*id
, char *list
,
376 struct tdb_xattrs
*attribs
;
380 status
= xattr_tdb_load_attrs(talloc_tos(), db_ctx
, id
, &attribs
);
382 if (!NT_STATUS_IS_OK(status
)) {
383 DEBUG(10, ("xattr_tdb_fetch_attrs failed: %s\n",
389 DEBUG(10, ("xattr_tdb_listattr: Found %d xattrs\n",
392 for (i
=0; i
<attribs
->num_eas
; i
++) {
395 DEBUG(10, ("xattr_tdb_listattr: xattrs[i].name: %s\n",
396 attribs
->eas
[i
].name
));
398 tmp
= strlen(attribs
->eas
[i
].name
);
401 * Try to protect against overflow
404 if (len
+ (tmp
+1) < len
) {
405 TALLOC_FREE(attribs
);
411 * Take care of the terminating NULL
417 TALLOC_FREE(attribs
);
424 for (i
=0; i
<attribs
->num_eas
; i
++) {
425 strlcpy(list
+len
, attribs
->eas
[i
].name
,
427 len
+= (strlen(attribs
->eas
[i
].name
) + 1);
430 TALLOC_FREE(attribs
);
434 static ssize_t
xattr_tdb_listxattr(struct vfs_handle_struct
*handle
,
435 const char *path
, char *list
, size_t size
)
437 SMB_STRUCT_STAT sbuf
;
439 struct db_context
*db
;
441 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
443 if (vfs_stat_smb_fname(handle
->conn
, path
, &sbuf
) == -1) {
447 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
449 return xattr_tdb_listattr(db
, &id
, list
, size
);
452 static ssize_t
xattr_tdb_flistxattr(struct vfs_handle_struct
*handle
,
453 struct files_struct
*fsp
, char *list
,
456 SMB_STRUCT_STAT sbuf
;
458 struct db_context
*db
;
460 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
462 if (SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
466 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
468 return xattr_tdb_listattr(db
, &id
, list
, size
);
472 * Worker routine for removexattr and fremovexattr
475 static int xattr_tdb_removeattr(struct db_context
*db_ctx
,
476 const struct file_id
*id
, const char *name
)
479 struct db_record
*rec
;
480 struct tdb_xattrs
*attribs
;
483 rec
= xattr_tdb_lock_attrs(talloc_tos(), db_ctx
, id
);
486 DEBUG(0, ("xattr_tdb_lock_attrs failed\n"));
491 status
= xattr_tdb_pull_attrs(rec
, &rec
->value
, &attribs
);
493 if (!NT_STATUS_IS_OK(status
)) {
494 DEBUG(10, ("xattr_tdb_fetch_attrs failed: %s\n",
500 for (i
=0; i
<attribs
->num_eas
; i
++) {
501 if (strcmp(attribs
->eas
[i
].name
, name
) == 0) {
506 if (i
== attribs
->num_eas
) {
513 attribs
->eas
[attribs
->num_eas
-1];
514 attribs
->num_eas
-= 1;
516 if (attribs
->num_eas
== 0) {
517 rec
->delete_rec(rec
);
522 status
= xattr_tdb_save_attrs(rec
, attribs
);
526 if (!NT_STATUS_IS_OK(status
)) {
527 DEBUG(1, ("save failed: %s\n", nt_errstr(status
)));
534 static int xattr_tdb_removexattr(struct vfs_handle_struct
*handle
,
535 const char *path
, const char *name
)
537 SMB_STRUCT_STAT sbuf
;
539 struct db_context
*db
;
541 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
543 if (vfs_stat_smb_fname(handle
->conn
, path
, &sbuf
) == -1) {
547 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
549 return xattr_tdb_removeattr(db
, &id
, name
);
552 static int xattr_tdb_fremovexattr(struct vfs_handle_struct
*handle
,
553 struct files_struct
*fsp
, const char *name
)
555 SMB_STRUCT_STAT sbuf
;
557 struct db_context
*db
;
559 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
561 if (SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
565 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
567 return xattr_tdb_removeattr(db
, &id
, name
);
571 * Open the tdb file upon VFS_CONNECT
574 static bool xattr_tdb_init(int snum
, struct db_context
**p_db
)
576 struct db_context
*db
;
580 def_dbname
= state_path("xattr.tdb");
581 if (def_dbname
== NULL
) {
586 dbname
= lp_parm_const_string(snum
, "xattr_tdb", "file", def_dbname
);
588 /* now we know dbname is not NULL */
591 db
= db_open(NULL
, dbname
, 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
600 TALLOC_FREE(def_dbname
);
605 TALLOC_FREE(def_dbname
);
610 * On unlink we need to delete the tdb record
612 static int xattr_tdb_unlink(vfs_handle_struct
*handle
,
613 const struct smb_filename
*smb_fname
)
615 struct smb_filename
*smb_fname_tmp
= NULL
;
617 struct db_context
*db
;
618 struct db_record
*rec
;
621 bool remove_record
= false;
623 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
625 status
= copy_smb_filename(talloc_tos(), smb_fname
, &smb_fname_tmp
);
626 if (!NT_STATUS_IS_OK(status
)) {
627 errno
= map_errno_from_nt_status(status
);
631 if (lp_posix_pathnames()) {
632 ret
= SMB_VFS_LSTAT(handle
->conn
, smb_fname_tmp
);
634 ret
= SMB_VFS_STAT(handle
->conn
, smb_fname_tmp
);
640 if (smb_fname_tmp
->st
.st_ex_nlink
== 1) {
641 /* Only remove record on last link to file. */
642 remove_record
= true;
645 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname_tmp
);
651 if (!remove_record
) {
655 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &smb_fname_tmp
->st
);
657 rec
= xattr_tdb_lock_attrs(talloc_tos(), db
, &id
);
660 * If rec == NULL there's not much we can do about it
664 rec
->delete_rec(rec
);
669 TALLOC_FREE(smb_fname_tmp
);
674 * On rmdir we need to delete the tdb record
676 static int xattr_tdb_rmdir(vfs_handle_struct
*handle
, const char *path
)
678 SMB_STRUCT_STAT sbuf
;
680 struct db_context
*db
;
681 struct db_record
*rec
;
684 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
686 if (vfs_stat_smb_fname(handle
->conn
, path
, &sbuf
) == -1) {
690 ret
= SMB_VFS_NEXT_RMDIR(handle
, path
);
696 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
698 rec
= xattr_tdb_lock_attrs(talloc_tos(), db
, &id
);
701 * If rec == NULL there's not much we can do about it
705 rec
->delete_rec(rec
);
713 * Destructor for the VFS private data
716 static void close_xattr_db(void **data
)
718 struct db_context
**p_db
= (struct db_context
**)data
;
722 static int xattr_tdb_connect(vfs_handle_struct
*handle
, const char *service
,
727 struct db_context
*db
;
729 res
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
734 fstrcpy(sname
, service
);
735 snum
= find_service(sname
);
738 * Should not happen, but we should not fail just *here*.
743 if (!xattr_tdb_init(snum
, &db
)) {
744 DEBUG(5, ("Could not init xattr tdb\n"));
745 lp_do_parameter(snum
, "ea support", "False");
749 lp_do_parameter(snum
, "ea support", "True");
751 SMB_VFS_HANDLE_SET_DATA(handle
, db
, close_xattr_db
,
752 struct db_context
, return -1);
757 static struct vfs_fn_pointers vfs_xattr_tdb_fns
= {
758 .getxattr
= xattr_tdb_getxattr
,
759 .fgetxattr
= xattr_tdb_fgetxattr
,
760 .setxattr
= xattr_tdb_setxattr
,
761 .fsetxattr
= xattr_tdb_fsetxattr
,
762 .listxattr
= xattr_tdb_listxattr
,
763 .flistxattr
= xattr_tdb_flistxattr
,
764 .removexattr
= xattr_tdb_removexattr
,
765 .fremovexattr
= xattr_tdb_fremovexattr
,
766 .unlink
= xattr_tdb_unlink
,
767 .rmdir
= xattr_tdb_rmdir
,
768 .connect_fn
= xattr_tdb_connect
,
771 NTSTATUS
vfs_xattr_tdb_init(void);
772 NTSTATUS
vfs_xattr_tdb_init(void)
774 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "xattr_tdb",