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
, 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
, 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 push_file_id_16((char *)id_buf
, id
);
105 if (db_ctx
->fetch(db_ctx
, mem_ctx
,
106 make_tdb_data(id_buf
, sizeof(id_buf
)),
108 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
111 status
= xattr_tdb_pull_attrs(mem_ctx
, &data
, presult
);
112 TALLOC_FREE(data
.dptr
);
117 * fetch_lock the tdb_ea record for a file
120 static struct db_record
*xattr_tdb_lock_attrs(TALLOC_CTX
*mem_ctx
,
121 struct db_context
*db_ctx
,
122 const struct file_id
*id
)
125 push_file_id_16((char *)id_buf
, id
);
126 return db_ctx
->fetch_locked(db_ctx
, mem_ctx
,
127 make_tdb_data(id_buf
, sizeof(id_buf
)));
131 * Save tdb_xattrs to a previously fetch_locked record
134 static NTSTATUS
xattr_tdb_save_attrs(struct db_record
*rec
,
135 const struct tdb_xattrs
*attribs
)
137 TDB_DATA data
= tdb_null
;
140 status
= xattr_tdb_push_attrs(talloc_tos(), attribs
, &data
);
142 if (!NT_STATUS_IS_OK(status
)) {
143 DEBUG(0, ("xattr_tdb_push_attrs failed: %s\n",
148 status
= rec
->store(rec
, data
, 0);
150 TALLOC_FREE(data
.dptr
);
156 * Worker routine for getxattr and fgetxattr
159 static ssize_t
xattr_tdb_getattr(struct db_context
*db_ctx
,
160 const struct file_id
*id
,
161 const char *name
, void *value
, size_t size
)
163 struct tdb_xattrs
*attribs
;
168 DEBUG(10, ("xattr_tdb_getattr called for file %s, name %s\n",
169 file_id_string_tos(id
), name
));
171 status
= xattr_tdb_load_attrs(talloc_tos(), db_ctx
, id
, &attribs
);
173 if (!NT_STATUS_IS_OK(status
)) {
174 DEBUG(10, ("xattr_tdb_fetch_attrs failed: %s\n",
180 for (i
=0; i
<attribs
->num_xattrs
; i
++) {
181 if (strcmp(attribs
->xattrs
[i
].name
, name
) == 0) {
186 if (i
== attribs
->num_xattrs
) {
191 if (attribs
->xattrs
[i
].value
.length
> size
) {
196 memcpy(value
, attribs
->xattrs
[i
].value
.data
,
197 attribs
->xattrs
[i
].value
.length
);
198 result
= attribs
->xattrs
[i
].value
.length
;
201 TALLOC_FREE(attribs
);
205 static ssize_t
xattr_tdb_getxattr(struct vfs_handle_struct
*handle
,
206 const char *path
, const char *name
,
207 void *value
, size_t size
)
209 SMB_STRUCT_STAT sbuf
;
211 struct db_context
*db
;
213 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
215 if (SMB_VFS_STAT(handle
->conn
, path
, &sbuf
) == -1) {
219 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, sbuf
.st_dev
, sbuf
.st_ino
);
221 return xattr_tdb_getattr(db
, &id
, name
, value
, size
);
224 static ssize_t
xattr_tdb_fgetxattr(struct vfs_handle_struct
*handle
,
225 struct files_struct
*fsp
,
226 const char *name
, void *value
, size_t size
)
228 SMB_STRUCT_STAT sbuf
;
230 struct db_context
*db
;
232 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
234 if (SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
238 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, sbuf
.st_dev
, sbuf
.st_ino
);
240 return xattr_tdb_getattr(db
, &id
, name
, value
, size
);
244 * Worker routine for setxattr and fsetxattr
247 static int xattr_tdb_setattr(struct db_context
*db_ctx
,
248 const struct file_id
*id
, const char *name
,
249 const void *value
, size_t size
, int flags
)
252 struct db_record
*rec
;
253 struct tdb_xattrs
*attribs
;
256 DEBUG(10, ("xattr_tdb_setattr called for file %s, name %s\n",
257 file_id_string_tos(id
), name
));
259 rec
= xattr_tdb_lock_attrs(talloc_tos(), db_ctx
, id
);
262 DEBUG(0, ("xattr_tdb_lock_attrs failed\n"));
267 status
= xattr_tdb_pull_attrs(rec
, &rec
->value
, &attribs
);
269 if (!NT_STATUS_IS_OK(status
)) {
270 DEBUG(10, ("xattr_tdb_fetch_attrs failed: %s\n",
276 for (i
=0; i
<attribs
->num_xattrs
; i
++) {
277 if (strcmp(attribs
->xattrs
[i
].name
, name
) == 0) {
278 if (flags
& XATTR_CREATE
) {
287 if (i
== attribs
->num_xattrs
) {
288 struct tdb_xattr
*tmp
;
290 if (flags
& XATTR_REPLACE
) {
296 tmp
= TALLOC_REALLOC_ARRAY(
297 attribs
, attribs
->xattrs
, struct tdb_xattr
,
298 attribs
->num_xattrs
+ 1);
301 DEBUG(0, ("TALLOC_REALLOC_ARRAY failed\n"));
307 attribs
->xattrs
= tmp
;
308 attribs
->num_xattrs
+= 1;
311 attribs
->xattrs
[i
].name
= name
;
312 attribs
->xattrs
[i
].value
.data
= CONST_DISCARD(uint8
*, value
);
313 attribs
->xattrs
[i
].value
.length
= size
;
315 status
= xattr_tdb_save_attrs(rec
, attribs
);
319 if (!NT_STATUS_IS_OK(status
)) {
320 DEBUG(1, ("save failed: %s\n", nt_errstr(status
)));
327 static int xattr_tdb_setxattr(struct vfs_handle_struct
*handle
,
328 const char *path
, const char *name
,
329 const void *value
, size_t size
, int flags
)
331 SMB_STRUCT_STAT sbuf
;
333 struct db_context
*db
;
335 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
337 if (SMB_VFS_STAT(handle
->conn
, path
, &sbuf
) == -1) {
341 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, sbuf
.st_dev
, sbuf
.st_ino
);
343 return xattr_tdb_setattr(db
, &id
, name
, value
, size
, flags
);
346 static int xattr_tdb_fsetxattr(struct vfs_handle_struct
*handle
,
347 struct files_struct
*fsp
,
348 const char *name
, const void *value
,
349 size_t size
, int flags
)
351 SMB_STRUCT_STAT sbuf
;
353 struct db_context
*db
;
355 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
357 if (SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
361 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, sbuf
.st_dev
, sbuf
.st_ino
);
363 return xattr_tdb_setattr(db
, &id
, name
, value
, size
, flags
);
367 * Worker routine for listxattr and flistxattr
370 static ssize_t
xattr_tdb_listattr(struct db_context
*db_ctx
,
371 const struct file_id
*id
, char *list
,
375 struct tdb_xattrs
*attribs
;
379 status
= xattr_tdb_load_attrs(talloc_tos(), db_ctx
, id
, &attribs
);
381 if (!NT_STATUS_IS_OK(status
)) {
382 DEBUG(10, ("xattr_tdb_fetch_attrs failed: %s\n",
388 DEBUG(10, ("xattr_tdb_listattr: Found %d xattrs\n",
389 attribs
->num_xattrs
));
391 for (i
=0; i
<attribs
->num_xattrs
; i
++) {
394 DEBUG(10, ("xattr_tdb_listattr: xattrs[i].name: %s\n",
395 attribs
->xattrs
[i
].name
));
397 tmp
= strlen(attribs
->xattrs
[i
].name
);
400 * Try to protect against overflow
403 if (len
+ (tmp
+1) < len
) {
404 TALLOC_FREE(attribs
);
410 * Take care of the terminating NULL
416 TALLOC_FREE(attribs
);
423 for (i
=0; i
<attribs
->num_xattrs
; i
++) {
424 strlcpy(list
+len
, attribs
->xattrs
[i
].name
,
426 len
+= (strlen(attribs
->xattrs
[i
].name
) + 1);
429 TALLOC_FREE(attribs
);
433 static ssize_t
xattr_tdb_listxattr(struct vfs_handle_struct
*handle
,
434 const char *path
, char *list
, size_t size
)
436 SMB_STRUCT_STAT sbuf
;
438 struct db_context
*db
;
440 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
442 if (SMB_VFS_STAT(handle
->conn
, path
, &sbuf
) == -1) {
446 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, sbuf
.st_dev
, sbuf
.st_ino
);
448 return xattr_tdb_listattr(db
, &id
, list
, size
);
451 static ssize_t
xattr_tdb_flistxattr(struct vfs_handle_struct
*handle
,
452 struct files_struct
*fsp
, char *list
,
455 SMB_STRUCT_STAT sbuf
;
457 struct db_context
*db
;
459 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
461 if (SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
465 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, sbuf
.st_dev
, sbuf
.st_ino
);
467 return xattr_tdb_listattr(db
, &id
, list
, size
);
471 * Worker routine for removexattr and fremovexattr
474 static int xattr_tdb_removeattr(struct db_context
*db_ctx
,
475 const struct file_id
*id
, const char *name
)
478 struct db_record
*rec
;
479 struct tdb_xattrs
*attribs
;
482 rec
= xattr_tdb_lock_attrs(talloc_tos(), db_ctx
, id
);
485 DEBUG(0, ("xattr_tdb_lock_attrs failed\n"));
490 status
= xattr_tdb_pull_attrs(rec
, &rec
->value
, &attribs
);
492 if (!NT_STATUS_IS_OK(status
)) {
493 DEBUG(10, ("xattr_tdb_fetch_attrs failed: %s\n",
499 for (i
=0; i
<attribs
->num_xattrs
; i
++) {
500 if (strcmp(attribs
->xattrs
[i
].name
, name
) == 0) {
505 if (i
== attribs
->num_xattrs
) {
512 attribs
->xattrs
[attribs
->num_xattrs
-1];
513 attribs
->num_xattrs
-= 1;
515 if (attribs
->num_xattrs
== 0) {
516 rec
->delete_rec(rec
);
521 status
= xattr_tdb_save_attrs(rec
, attribs
);
525 if (!NT_STATUS_IS_OK(status
)) {
526 DEBUG(1, ("save failed: %s\n", nt_errstr(status
)));
533 static int xattr_tdb_removexattr(struct vfs_handle_struct
*handle
,
534 const char *path
, const char *name
)
536 SMB_STRUCT_STAT sbuf
;
538 struct db_context
*db
;
540 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
542 if (SMB_VFS_STAT(handle
->conn
, path
, &sbuf
) == -1) {
546 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, sbuf
.st_dev
, sbuf
.st_ino
);
548 return xattr_tdb_removeattr(db
, &id
, name
);
551 static int xattr_tdb_fremovexattr(struct vfs_handle_struct
*handle
,
552 struct files_struct
*fsp
, const char *name
)
554 SMB_STRUCT_STAT sbuf
;
556 struct db_context
*db
;
558 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
560 if (SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
564 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, sbuf
.st_dev
, sbuf
.st_ino
);
566 return xattr_tdb_removeattr(db
, &id
, name
);
570 * Open the tdb file upon VFS_CONNECT
573 static bool xattr_tdb_init(int snum
, struct db_context
**p_db
)
575 struct db_context
*db
;
578 dbname
= lp_parm_const_string(snum
, "xattr_tdb", "file",
579 lock_path("xattr.tdb"));
581 if (dbname
== NULL
) {
587 db
= db_open(NULL
, dbname
, 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
604 * On unlink we need to delete the tdb record
606 static int xattr_tdb_unlink(vfs_handle_struct
*handle
, const char *path
)
608 SMB_STRUCT_STAT sbuf
;
610 struct db_context
*db
;
611 struct db_record
*rec
;
614 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
616 if (SMB_VFS_STAT(handle
->conn
, path
, &sbuf
) == -1) {
620 ret
= SMB_VFS_NEXT_UNLINK(handle
, path
);
626 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, sbuf
.st_dev
, sbuf
.st_ino
);
628 rec
= xattr_tdb_lock_attrs(talloc_tos(), db
, &id
);
631 * If rec == NULL there's not much we can do about it
635 rec
->delete_rec(rec
);
643 * On rmdir we need to delete the tdb record
645 static int xattr_tdb_rmdir(vfs_handle_struct
*handle
, const char *path
)
647 SMB_STRUCT_STAT sbuf
;
649 struct db_context
*db
;
650 struct db_record
*rec
;
653 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
655 if (SMB_VFS_STAT(handle
->conn
, path
, &sbuf
) == -1) {
659 ret
= SMB_VFS_NEXT_RMDIR(handle
, path
);
665 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, sbuf
.st_dev
, sbuf
.st_ino
);
667 rec
= xattr_tdb_lock_attrs(talloc_tos(), db
, &id
);
670 * If rec == NULL there's not much we can do about it
674 rec
->delete_rec(rec
);
682 * Destructor for the VFS private data
685 static void close_xattr_db(void **data
)
687 struct db_context
**p_db
= (struct db_context
**)data
;
691 static int xattr_tdb_connect(vfs_handle_struct
*handle
, const char *service
,
696 struct db_context
*db
;
698 res
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
703 fstrcpy(sname
, service
);
704 snum
= find_service(sname
);
707 * Should not happen, but we should not fail just *here*.
712 if (!xattr_tdb_init(snum
, &db
)) {
713 DEBUG(5, ("Could not init xattr tdb\n"));
714 lp_do_parameter(snum
, "ea support", "False");
718 lp_do_parameter(snum
, "ea support", "True");
720 SMB_VFS_HANDLE_SET_DATA(handle
, db
, close_xattr_db
,
721 struct db_context
, return -1);
726 /* VFS operations structure */
728 static const vfs_op_tuple xattr_tdb_ops
[] = {
729 {SMB_VFS_OP(xattr_tdb_getxattr
), SMB_VFS_OP_GETXATTR
,
730 SMB_VFS_LAYER_TRANSPARENT
},
731 {SMB_VFS_OP(xattr_tdb_fgetxattr
), SMB_VFS_OP_FGETXATTR
,
732 SMB_VFS_LAYER_TRANSPARENT
},
733 {SMB_VFS_OP(xattr_tdb_setxattr
), SMB_VFS_OP_SETXATTR
,
734 SMB_VFS_LAYER_TRANSPARENT
},
735 {SMB_VFS_OP(xattr_tdb_fsetxattr
), SMB_VFS_OP_FSETXATTR
,
736 SMB_VFS_LAYER_TRANSPARENT
},
737 {SMB_VFS_OP(xattr_tdb_listxattr
), SMB_VFS_OP_LISTXATTR
,
738 SMB_VFS_LAYER_TRANSPARENT
},
739 {SMB_VFS_OP(xattr_tdb_flistxattr
), SMB_VFS_OP_FLISTXATTR
,
740 SMB_VFS_LAYER_TRANSPARENT
},
741 {SMB_VFS_OP(xattr_tdb_removexattr
), SMB_VFS_OP_REMOVEXATTR
,
742 SMB_VFS_LAYER_TRANSPARENT
},
743 {SMB_VFS_OP(xattr_tdb_fremovexattr
), SMB_VFS_OP_FREMOVEXATTR
,
744 SMB_VFS_LAYER_TRANSPARENT
},
745 {SMB_VFS_OP(xattr_tdb_unlink
), SMB_VFS_OP_UNLINK
,
746 SMB_VFS_LAYER_TRANSPARENT
},
747 {SMB_VFS_OP(xattr_tdb_rmdir
), SMB_VFS_OP_RMDIR
,
748 SMB_VFS_LAYER_TRANSPARENT
},
749 {SMB_VFS_OP(xattr_tdb_connect
), SMB_VFS_OP_CONNECT
,
750 SMB_VFS_LAYER_TRANSPARENT
},
751 {SMB_VFS_OP(NULL
), SMB_VFS_OP_NOOP
, SMB_VFS_LAYER_NOOP
}
754 NTSTATUS
vfs_xattr_tdb_init(void);
755 NTSTATUS
vfs_xattr_tdb_init(void)
757 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "xattr_tdb",