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 "dbwrap/dbwrap.h"
24 #include "dbwrap/dbwrap_open.h"
25 #include "source3/lib/xattr_tdb.h"
28 #define DBGC_CLASS DBGC_VFS
30 static ssize_t
xattr_tdb_getxattr(struct vfs_handle_struct
*handle
,
31 const char *path
, const char *name
,
32 void *value
, size_t size
)
36 struct db_context
*db
;
39 TALLOC_CTX
*frame
= talloc_stackframe();
41 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
43 if (vfs_stat_smb_fname(handle
->conn
, path
, &sbuf
) == -1) {
48 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
50 xattr_size
= xattr_tdb_getattr(db
, frame
, &id
, name
, &blob
);
55 if (blob
.length
> size
) {
60 memcpy(value
, blob
.data
, xattr_size
);
64 static ssize_t
xattr_tdb_fgetxattr(struct vfs_handle_struct
*handle
,
65 struct files_struct
*fsp
,
66 const char *name
, void *value
, size_t size
)
70 struct db_context
*db
;
73 TALLOC_CTX
*frame
= talloc_stackframe();
75 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
77 if (SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
82 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
84 xattr_size
= xattr_tdb_getattr(db
, frame
, &id
, name
, &blob
);
89 if (blob
.length
> size
) {
94 memcpy(value
, blob
.data
, size
);
99 static int xattr_tdb_setxattr(struct vfs_handle_struct
*handle
,
100 const char *path
, const char *name
,
101 const void *value
, size_t size
, int flags
)
103 SMB_STRUCT_STAT sbuf
;
105 struct db_context
*db
;
107 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
109 if (vfs_stat_smb_fname(handle
->conn
, path
, &sbuf
) == -1) {
113 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
115 return xattr_tdb_setattr(db
, &id
, name
, value
, size
, flags
);
118 static int xattr_tdb_fsetxattr(struct vfs_handle_struct
*handle
,
119 struct files_struct
*fsp
,
120 const char *name
, const void *value
,
121 size_t size
, int flags
)
123 SMB_STRUCT_STAT sbuf
;
125 struct db_context
*db
;
127 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
129 if (SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
133 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
135 return xattr_tdb_setattr(db
, &id
, name
, value
, size
, flags
);
138 static ssize_t
xattr_tdb_listxattr(struct vfs_handle_struct
*handle
,
139 const char *path
, char *list
, size_t size
)
141 SMB_STRUCT_STAT sbuf
;
143 struct db_context
*db
;
145 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
147 if (vfs_stat_smb_fname(handle
->conn
, path
, &sbuf
) == -1) {
151 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
153 return xattr_tdb_listattr(db
, &id
, list
, size
);
156 static ssize_t
xattr_tdb_flistxattr(struct vfs_handle_struct
*handle
,
157 struct files_struct
*fsp
, char *list
,
160 SMB_STRUCT_STAT sbuf
;
162 struct db_context
*db
;
164 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
166 if (SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
170 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
172 return xattr_tdb_listattr(db
, &id
, list
, size
);
175 static int xattr_tdb_removexattr(struct vfs_handle_struct
*handle
,
176 const char *path
, const char *name
)
178 SMB_STRUCT_STAT sbuf
;
180 struct db_context
*db
;
182 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
184 if (vfs_stat_smb_fname(handle
->conn
, path
, &sbuf
) == -1) {
188 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
190 return xattr_tdb_removeattr(db
, &id
, name
);
193 static int xattr_tdb_fremovexattr(struct vfs_handle_struct
*handle
,
194 struct files_struct
*fsp
, const char *name
)
196 SMB_STRUCT_STAT sbuf
;
198 struct db_context
*db
;
200 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
202 if (SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
206 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
208 return xattr_tdb_removeattr(db
, &id
, name
);
212 * Open the tdb file upon VFS_CONNECT
215 static bool xattr_tdb_init(int snum
, struct db_context
**p_db
)
217 struct db_context
*db
;
221 def_dbname
= state_path("xattr.tdb");
222 if (def_dbname
== NULL
) {
227 dbname
= lp_parm_const_string(snum
, "xattr_tdb", "file", def_dbname
);
229 /* now we know dbname is not NULL */
232 db
= db_open(NULL
, dbname
, 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600,
233 DBWRAP_LOCK_ORDER_2
);
242 TALLOC_FREE(def_dbname
);
247 TALLOC_FREE(def_dbname
);
252 * On unlink we need to delete the tdb record
254 static int xattr_tdb_unlink(vfs_handle_struct
*handle
,
255 const struct smb_filename
*smb_fname
)
257 struct smb_filename
*smb_fname_tmp
= NULL
;
259 struct db_context
*db
;
262 bool remove_record
= false;
264 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
266 status
= copy_smb_filename(talloc_tos(), smb_fname
, &smb_fname_tmp
);
267 if (!NT_STATUS_IS_OK(status
)) {
268 errno
= map_errno_from_nt_status(status
);
272 if (lp_posix_pathnames()) {
273 ret
= SMB_VFS_LSTAT(handle
->conn
, smb_fname_tmp
);
275 ret
= SMB_VFS_STAT(handle
->conn
, smb_fname_tmp
);
281 if (smb_fname_tmp
->st
.st_ex_nlink
== 1) {
282 /* Only remove record on last link to file. */
283 remove_record
= true;
286 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname_tmp
);
292 if (!remove_record
) {
296 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &smb_fname_tmp
->st
);
298 xattr_tdb_remove_all_attrs(db
, &id
);
301 TALLOC_FREE(smb_fname_tmp
);
306 * On rmdir we need to delete the tdb record
308 static int xattr_tdb_rmdir(vfs_handle_struct
*handle
, const char *path
)
310 SMB_STRUCT_STAT sbuf
;
312 struct db_context
*db
;
315 SMB_VFS_HANDLE_GET_DATA(handle
, db
, struct db_context
, return -1);
317 if (vfs_stat_smb_fname(handle
->conn
, path
, &sbuf
) == -1) {
321 ret
= SMB_VFS_NEXT_RMDIR(handle
, path
);
327 id
= SMB_VFS_FILE_ID_CREATE(handle
->conn
, &sbuf
);
329 xattr_tdb_remove_all_attrs(db
, &id
);
335 * Destructor for the VFS private data
338 static void close_xattr_db(void **data
)
340 struct db_context
**p_db
= (struct db_context
**)data
;
344 static int xattr_tdb_connect(vfs_handle_struct
*handle
, const char *service
,
349 struct db_context
*db
;
351 res
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
356 snum
= find_service(talloc_tos(), service
, &sname
);
357 if (snum
== -1 || sname
== NULL
) {
359 * Should not happen, but we should not fail just *here*.
364 if (!xattr_tdb_init(snum
, &db
)) {
365 DEBUG(5, ("Could not init xattr tdb\n"));
366 lp_do_parameter(snum
, "ea support", "False");
370 lp_do_parameter(snum
, "ea support", "True");
372 SMB_VFS_HANDLE_SET_DATA(handle
, db
, close_xattr_db
,
373 struct db_context
, return -1);
378 static struct vfs_fn_pointers vfs_xattr_tdb_fns
= {
379 .getxattr_fn
= xattr_tdb_getxattr
,
380 .fgetxattr_fn
= xattr_tdb_fgetxattr
,
381 .setxattr_fn
= xattr_tdb_setxattr
,
382 .fsetxattr_fn
= xattr_tdb_fsetxattr
,
383 .listxattr_fn
= xattr_tdb_listxattr
,
384 .flistxattr_fn
= xattr_tdb_flistxattr
,
385 .removexattr_fn
= xattr_tdb_removexattr
,
386 .fremovexattr_fn
= xattr_tdb_fremovexattr
,
387 .unlink_fn
= xattr_tdb_unlink
,
388 .rmdir_fn
= xattr_tdb_rmdir
,
389 .connect_fn
= xattr_tdb_connect
,
392 NTSTATUS
vfs_xattr_tdb_init(void);
393 NTSTATUS
vfs_xattr_tdb_init(void)
395 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "xattr_tdb",