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 "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"
31 #define DBGC_CLASS DBGC_VFS
34 * unmarshall tdb_xattrs
37 static NTSTATUS
xattr_tdb_pull_attrs(TALLOC_CTX
*mem_ctx
,
39 struct tdb_xattrs
**presult
)
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) {
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
)));
63 return ndr_map_error2ntstatus(ndr_err
);
74 static NTSTATUS
xattr_tdb_push_attrs(TALLOC_CTX
*mem_ctx
,
75 const struct tdb_xattrs
*attribs
,
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
);
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
)
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
)),
113 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
116 status
= xattr_tdb_pull_attrs(mem_ctx
, &data
, presult
);
117 TALLOC_FREE(data
.dptr
);
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
)
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
;
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",
155 status
= rec
->store(rec
, data
, 0);
157 TALLOC_FREE(data
.dptr
);
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
;
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",
187 for (i
=0; i
<attribs
->num_eas
; i
++) {
188 if (strcmp(attribs
->eas
[i
].name
, name
) == 0) {
193 if (i
== attribs
->num_eas
) {
198 if (attribs
->eas
[i
].value
.length
> size
) {
203 memcpy(value
, attribs
->eas
[i
].value
.data
,
204 attribs
->eas
[i
].value
.length
);
205 result
= attribs
->eas
[i
].value
.length
;
208 TALLOC_FREE(attribs
);
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
;
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) {
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
;
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) {
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
)
259 struct db_record
*rec
;
260 struct tdb_xattrs
*attribs
;
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
);
269 DEBUG(0, ("xattr_tdb_lock_attrs failed\n"));
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",
283 for (i
=0; i
<attribs
->num_eas
; i
++) {
284 if (strcmp(attribs
->eas
[i
].name
, name
) == 0) {
285 if (flags
& XATTR_CREATE
) {
294 if (i
== attribs
->num_eas
) {
295 struct xattr_EA
*tmp
;
297 if (flags
& XATTR_REPLACE
) {
303 tmp
= talloc_realloc(
304 attribs
, attribs
->eas
, struct xattr_EA
,
305 attribs
->num_eas
+ 1);
308 DEBUG(0, ("talloc_realloc failed\n"));
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
);
326 if (!NT_STATUS_IS_OK(status
)) {
327 DEBUG(1, ("save failed: %s\n", nt_errstr(status
)));
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
;
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) {
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
;
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) {
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
,
382 struct tdb_xattrs
*attribs
;
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",
395 DEBUG(10, ("xattr_tdb_listattr: Found %d xattrs\n",
398 for (i
=0; i
<attribs
->num_eas
; i
++) {
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
);
417 * Take care of the terminating NULL
423 TALLOC_FREE(attribs
);
430 for (i
=0; i
<attribs
->num_eas
; i
++) {
431 strlcpy(list
+len
, attribs
->eas
[i
].name
,
433 len
+= (strlen(attribs
->eas
[i
].name
) + 1);
436 TALLOC_FREE(attribs
);
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
;
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) {
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
,
462 SMB_STRUCT_STAT sbuf
;
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) {
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
)
485 struct db_record
*rec
;
486 struct tdb_xattrs
*attribs
;
489 rec
= xattr_tdb_lock_attrs(talloc_tos(), db_ctx
, id
);
492 DEBUG(0, ("xattr_tdb_lock_attrs failed\n"));
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",
506 for (i
=0; i
<attribs
->num_eas
; i
++) {
507 if (strcmp(attribs
->eas
[i
].name
, name
) == 0) {
512 if (i
== attribs
->num_eas
) {
519 attribs
->eas
[attribs
->num_eas
-1];
520 attribs
->num_eas
-= 1;
522 if (attribs
->num_eas
== 0) {
523 rec
->delete_rec(rec
);
528 status
= xattr_tdb_save_attrs(rec
, attribs
);
532 if (!NT_STATUS_IS_OK(status
)) {
533 DEBUG(1, ("save failed: %s\n", nt_errstr(status
)));
540 static int xattr_tdb_removexattr(struct vfs_handle_struct
*handle
,
541 const char *path
, const char *name
)
543 SMB_STRUCT_STAT sbuf
;
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) {
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
;
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) {
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
;
586 def_dbname
= state_path("xattr.tdb");
587 if (def_dbname
== NULL
) {
592 dbname
= lp_parm_const_string(snum
, "xattr_tdb", "file", def_dbname
);
594 /* now we know dbname is not NULL */
597 db
= db_open(NULL
, dbname
, 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
606 TALLOC_FREE(def_dbname
);
611 TALLOC_FREE(def_dbname
);
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
;
623 struct db_context
*db
;
624 struct db_record
*rec
;
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
);
637 if (lp_posix_pathnames()) {
638 ret
= SMB_VFS_LSTAT(handle
->conn
, smb_fname_tmp
);
640 ret
= SMB_VFS_STAT(handle
->conn
, smb_fname_tmp
);
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
);
657 if (!remove_record
) {
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
670 rec
->delete_rec(rec
);
675 TALLOC_FREE(smb_fname_tmp
);
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
;
686 struct db_context
*db
;
687 struct db_record
*rec
;
690 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
692 if (vfs_stat_smb_fname(handle
->conn
, path
, &sbuf
) == -1) {
696 ret
= SMB_VFS_NEXT_RMDIR(handle
, path
);
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
711 rec
->delete_rec(rec
);
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
;
728 static int xattr_tdb_connect(vfs_handle_struct
*handle
, const char *service
,
733 struct db_context
*db
;
735 res
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
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*.
748 if (!xattr_tdb_init(snum
, &db
)) {
749 DEBUG(5, ("Could not init xattr tdb\n"));
750 lp_do_parameter(snum
, "ea support", "False");
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);
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",