* Store posix-level xattrs in a tdb
*
* Copyright (C) Volker Lendecke, 2007
+ * Copyright (C) Andrew Bartlett, 2012
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
#include "includes.h"
#include "system/filesys.h"
#include "smbd/smbd.h"
-#include "librpc/gen_ndr/xattr.h"
-#include "librpc/gen_ndr/ndr_xattr.h"
-#include "../librpc/gen_ndr/ndr_netlogon.h"
#include "dbwrap/dbwrap.h"
#include "dbwrap/dbwrap_open.h"
-#include "util_tdb.h"
+#include "source3/lib/xattr_tdb.h"
+#include "lib/util/tevent_unix.h"
#undef DBGC_CLASS
#define DBGC_CLASS DBGC_VFS
-/*
- * unmarshall tdb_xattrs
- */
-
-static NTSTATUS xattr_tdb_pull_attrs(TALLOC_CTX *mem_ctx,
- const TDB_DATA *data,
- struct tdb_xattrs **presult)
-{
- DATA_BLOB blob;
- enum ndr_err_code ndr_err;
- struct tdb_xattrs *result;
-
- if (!(result = talloc_zero(mem_ctx, struct tdb_xattrs))) {
- return NT_STATUS_NO_MEMORY;
- }
-
- if (data->dsize == 0) {
- *presult = result;
- return NT_STATUS_OK;
- }
-
- blob = data_blob_const(data->dptr, data->dsize);
-
- ndr_err = ndr_pull_struct_blob(&blob, result, result,
- (ndr_pull_flags_fn_t)ndr_pull_tdb_xattrs);
-
- if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
- DEBUG(0, ("ndr_pull_tdb_xattrs failed: %s\n",
- ndr_errstr(ndr_err)));
- TALLOC_FREE(result);
- return ndr_map_error2ntstatus(ndr_err);
- }
-
- *presult = result;
- return NT_STATUS_OK;
-}
+struct xattr_tdb_config {
+ struct db_context *db;
+ bool ignore_user_xattr;
+};
-/*
- * marshall tdb_xattrs
- */
+static bool xattr_tdb_init(struct vfs_handle_struct *handle,
+ struct xattr_tdb_config **_config);
-static NTSTATUS xattr_tdb_push_attrs(TALLOC_CTX *mem_ctx,
- const struct tdb_xattrs *attribs,
- TDB_DATA *data)
+static bool is_user_xattr(const char *xattr_name)
{
- DATA_BLOB blob;
- enum ndr_err_code ndr_err;
-
- ndr_err = ndr_push_struct_blob(&blob, mem_ctx, attribs,
- (ndr_push_flags_fn_t)ndr_push_tdb_xattrs);
+ int match;
- if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
- DEBUG(0, ("ndr_push_tdb_xattrs failed: %s\n",
- ndr_errstr(ndr_err)));
- return ndr_map_error2ntstatus(ndr_err);
- }
-
- *data = make_tdb_data(blob.data, blob.length);
- return NT_STATUS_OK;
+ match = strncmp(xattr_name, "user.", strlen("user."));
+ return (match == 0);
}
-/*
- * Load tdb_xattrs for a file from the tdb
- */
-
-static NTSTATUS xattr_tdb_load_attrs(TALLOC_CTX *mem_ctx,
- struct db_context *db_ctx,
- const struct file_id *id,
- struct tdb_xattrs **presult)
+static int xattr_tdb_get_file_id(struct vfs_handle_struct *handle,
+ const char *path, struct file_id *id)
{
- uint8 id_buf[16];
- NTSTATUS status;
- TDB_DATA data;
+ int ret;
+ TALLOC_CTX *frame = talloc_stackframe();
+ struct smb_filename *smb_fname;
+
+ smb_fname = synthetic_smb_fname(frame,
+ path,
+ NULL,
+ NULL,
+ 0,
+ 0);
+ if (smb_fname == NULL) {
+ TALLOC_FREE(frame);
+ errno = ENOMEM;
+ return -1;
+ }
- /* For backwards compatibility only store the dev/inode. */
- push_file_id_16((char *)id_buf, id);
+ ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
- status = dbwrap_fetch(db_ctx, mem_ctx,
- make_tdb_data(id_buf, sizeof(id_buf)),
- &data);
- if (!NT_STATUS_IS_OK(status)) {
- return NT_STATUS_INTERNAL_DB_CORRUPTION;
+ if (ret == -1) {
+ TALLOC_FREE(frame);
+ return -1;
}
- status = xattr_tdb_pull_attrs(mem_ctx, &data, presult);
- TALLOC_FREE(data.dptr);
- return status;
+ *id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname->st);
+ TALLOC_FREE(frame);
+ return 0;
}
-/*
- * fetch_lock the tdb_ea record for a file
- */
-
-static struct db_record *xattr_tdb_lock_attrs(TALLOC_CTX *mem_ctx,
- struct db_context *db_ctx,
- const struct file_id *id)
-{
- uint8 id_buf[16];
-
- /* For backwards compatibility only store the dev/inode. */
- push_file_id_16((char *)id_buf, id);
- return dbwrap_fetch_locked(db_ctx, mem_ctx,
- make_tdb_data(id_buf, sizeof(id_buf)));
-}
+struct xattr_tdb_getxattrat_state {
+ struct vfs_aio_state vfs_aio_state;
+ ssize_t xattr_size;
+ uint8_t *xattr_value;
+};
-/*
- * Save tdb_xattrs to a previously fetch_locked record
- */
+static void xattr_tdb_getxattrat_done(struct tevent_req *subreq);
-static NTSTATUS xattr_tdb_save_attrs(struct db_record *rec,
- const struct tdb_xattrs *attribs)
+static struct tevent_req *xattr_tdb_getxattrat_send(
+ TALLOC_CTX *mem_ctx,
+ struct tevent_context *ev,
+ struct vfs_handle_struct *handle,
+ files_struct *dir_fsp,
+ const struct smb_filename *smb_fname,
+ const char *xattr_name,
+ size_t alloc_hint)
{
- TDB_DATA data = tdb_null;
- NTSTATUS status;
-
- status = xattr_tdb_push_attrs(talloc_tos(), attribs, &data);
-
- if (!NT_STATUS_IS_OK(status)) {
- DEBUG(0, ("xattr_tdb_push_attrs failed: %s\n",
- nt_errstr(status)));
- return status;
+ struct xattr_tdb_config *config = NULL;
+ struct tevent_req *req = NULL;
+ struct tevent_req *subreq = NULL;
+ struct xattr_tdb_getxattrat_state *state = NULL;
+ struct smb_filename *cwd = NULL;
+ struct file_id id;
+ int ret;
+ int error;
+ int cwd_ret;
+ DATA_BLOB xattr_blob;
+
+ if (!xattr_tdb_init(handle, &config)) {
+ return NULL;
+ }
+
+ req = tevent_req_create(mem_ctx, &state,
+ struct xattr_tdb_getxattrat_state);
+ if (req == NULL) {
+ return NULL;
+ }
+ state->xattr_size = -1;
+
+ if (config->ignore_user_xattr && is_user_xattr(xattr_name)) {
+ subreq = SMB_VFS_NEXT_GETXATTRAT_SEND(state,
+ ev,
+ handle,
+ dir_fsp,
+ smb_fname,
+ xattr_name,
+ alloc_hint);
+ if (tevent_req_nomem(subreq, req)) {
+ return tevent_req_post(req, ev);
+ }
+ tevent_req_set_callback(subreq, xattr_tdb_getxattrat_done, req);
+ return req;
}
- status = dbwrap_record_store(rec, data, 0);
-
- TALLOC_FREE(data.dptr);
-
- return status;
-}
+ cwd = SMB_VFS_GETWD(dir_fsp->conn, state);
+ if (tevent_req_nomem(cwd, req)) {
+ return tevent_req_post(req, ev);
+ }
-/*
- * Worker routine for getxattr and fgetxattr
- */
+ ret = SMB_VFS_CHDIR(dir_fsp->conn, dir_fsp->fsp_name);
+ if (ret != 0) {
+ tevent_req_error(req, errno);
+ return tevent_req_post(req, ev);
+ }
-static ssize_t xattr_tdb_getattr(struct db_context *db_ctx,
- const struct file_id *id,
- const char *name, void *value, size_t size)
-{
- struct tdb_xattrs *attribs;
- uint32_t i;
- ssize_t result = -1;
- NTSTATUS status;
+ ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
+ error = errno;
- DEBUG(10, ("xattr_tdb_getattr called for file %s, name %s\n",
- file_id_string_tos(id), name));
+ cwd_ret = SMB_VFS_CHDIR(dir_fsp->conn, cwd);
+ SMB_ASSERT(cwd_ret == 0);
- status = xattr_tdb_load_attrs(talloc_tos(), db_ctx, id, &attribs);
+ if (ret == -1) {
+ tevent_req_error(req, error);
+ return tevent_req_post(req, ev);
+ }
- if (!NT_STATUS_IS_OK(status)) {
- DEBUG(10, ("xattr_tdb_fetch_attrs failed: %s\n",
- nt_errstr(status)));
- errno = EINVAL;
- return -1;
+ state->xattr_size = xattr_tdb_getattr(config->db,
+ state,
+ &id,
+ xattr_name,
+ &xattr_blob);
+ if (state->xattr_size == -1) {
+ tevent_req_error(req, errno);
+ return tevent_req_post(req, ev);
}
- for (i=0; i<attribs->num_eas; i++) {
- if (strcmp(attribs->eas[i].name, name) == 0) {
- break;
- }
+ if (alloc_hint == 0) {
+ /*
+ * The caller only wants to know the size.
+ */
+ tevent_req_done(req);
+ return tevent_req_post(req, ev);
}
- if (i == attribs->num_eas) {
- errno = ENOATTR;
- goto fail;
+ if (state->xattr_size == 0) {
+ /*
+ * There's no data.
+ */
+ tevent_req_done(req);
+ return tevent_req_post(req, ev);
}
- if (attribs->eas[i].value.length > size) {
- errno = ERANGE;
- goto fail;
+ if (xattr_blob.length > alloc_hint) {
+ /*
+ * The data doesn't fit.
+ */
+ state->xattr_size = -1;
+ tevent_req_error(req, ERANGE);
+ return tevent_req_post(req, ev);
}
- memcpy(value, attribs->eas[i].value.data,
- attribs->eas[i].value.length);
- result = attribs->eas[i].value.length;
+ /*
+ * take the whole blob.
+ */
+ state->xattr_value = xattr_blob.data;
- fail:
- TALLOC_FREE(attribs);
- return result;
+ tevent_req_done(req);
+ return tevent_req_post(req, ev);
}
-static ssize_t xattr_tdb_getxattr(struct vfs_handle_struct *handle,
- const char *path, const char *name,
- void *value, size_t size)
+static void xattr_tdb_getxattrat_done(struct tevent_req *subreq)
{
- SMB_STRUCT_STAT sbuf;
- struct file_id id;
- struct db_context *db;
+ struct tevent_req *req = tevent_req_callback_data(
+ subreq, struct tevent_req);
+ struct xattr_tdb_getxattrat_state *state = tevent_req_data(
+ req, struct xattr_tdb_getxattrat_state);
+
+ state->xattr_size = SMB_VFS_NEXT_GETXATTRAT_RECV(subreq,
+ &state->vfs_aio_state,
+ state,
+ &state->xattr_value);
+ TALLOC_FREE(subreq);
+ if (state->xattr_size == -1) {
+ tevent_req_error(req, state->vfs_aio_state.error);
+ return;
+ }
+
+ tevent_req_done(req);
+}
- SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
- if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
+static ssize_t xattr_tdb_getxattrat_recv(struct tevent_req *req,
+ struct vfs_aio_state *aio_state,
+ TALLOC_CTX *mem_ctx,
+ uint8_t **xattr_value)
+{
+ struct xattr_tdb_getxattrat_state *state = tevent_req_data(
+ req, struct xattr_tdb_getxattrat_state);
+ ssize_t xattr_size;
+
+ if (tevent_req_is_unix_error(req, &aio_state->error)) {
+ tevent_req_received(req);
return -1;
}
- id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
+ *aio_state = state->vfs_aio_state;
+ xattr_size = state->xattr_size;
+ if (xattr_value != NULL) {
+ *xattr_value = talloc_move(mem_ctx, &state->xattr_value);
+ }
- return xattr_tdb_getattr(db, &id, name, value, size);
+ tevent_req_received(req);
+ return xattr_size;
}
static ssize_t xattr_tdb_fgetxattr(struct vfs_handle_struct *handle,
struct files_struct *fsp,
const char *name, void *value, size_t size)
{
+ struct xattr_tdb_config *config = NULL;
SMB_STRUCT_STAT sbuf;
struct file_id id;
- struct db_context *db;
-
- SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
+ ssize_t xattr_size;
+ DATA_BLOB blob;
+ TALLOC_CTX *frame = NULL;
- if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
+ if (!xattr_tdb_init(handle, &config)) {
return -1;
}
- id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
-
- return xattr_tdb_getattr(db, &id, name, value, size);
-}
-
-/*
- * Worker routine for setxattr and fsetxattr
- */
-
-static int xattr_tdb_setattr(struct db_context *db_ctx,
- const struct file_id *id, const char *name,
- const void *value, size_t size, int flags)
-{
- NTSTATUS status;
- struct db_record *rec;
- struct tdb_xattrs *attribs;
- uint32_t i;
- TDB_DATA data;
-
- DEBUG(10, ("xattr_tdb_setattr called for file %s, name %s\n",
- file_id_string_tos(id), name));
-
- rec = xattr_tdb_lock_attrs(talloc_tos(), db_ctx, id);
-
- if (rec == NULL) {
- DEBUG(0, ("xattr_tdb_lock_attrs failed\n"));
- errno = EINVAL;
- return -1;
+ if (config->ignore_user_xattr && is_user_xattr(name)) {
+ return SMB_VFS_NEXT_FGETXATTR(
+ handle, fsp, name, value, size);
}
- data = dbwrap_record_get_value(rec);
-
- status = xattr_tdb_pull_attrs(rec, &data, &attribs);
-
- if (!NT_STATUS_IS_OK(status)) {
- DEBUG(10, ("xattr_tdb_fetch_attrs failed: %s\n",
- nt_errstr(status)));
- TALLOC_FREE(rec);
+ if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
return -1;
}
- for (i=0; i<attribs->num_eas; i++) {
- if (strcmp(attribs->eas[i].name, name) == 0) {
- if (flags & XATTR_CREATE) {
- TALLOC_FREE(rec);
- errno = EEXIST;
- return -1;
- }
- break;
- }
- }
-
- if (i == attribs->num_eas) {
- struct xattr_EA *tmp;
+ frame = talloc_stackframe();
- if (flags & XATTR_REPLACE) {
- TALLOC_FREE(rec);
- errno = ENOATTR;
- return -1;
- }
-
- tmp = talloc_realloc(
- attribs, attribs->eas, struct xattr_EA,
- attribs->num_eas+ 1);
-
- if (tmp == NULL) {
- DEBUG(0, ("talloc_realloc failed\n"));
- TALLOC_FREE(rec);
- errno = ENOMEM;
- return -1;
- }
-
- attribs->eas = tmp;
- attribs->num_eas += 1;
- }
-
- attribs->eas[i].name = name;
- attribs->eas[i].value.data = discard_const_p(uint8, value);
- attribs->eas[i].value.length = size;
-
- status = xattr_tdb_save_attrs(rec, attribs);
+ id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
- TALLOC_FREE(rec);
-
- if (!NT_STATUS_IS_OK(status)) {
- DEBUG(1, ("save failed: %s\n", nt_errstr(status)));
+ xattr_size = xattr_tdb_getattr(config->db, frame, &id, name, &blob);
+ if (xattr_size < 0) {
+ errno = ENOATTR;
+ TALLOC_FREE(frame);
return -1;
}
- return 0;
-}
-
-static int xattr_tdb_setxattr(struct vfs_handle_struct *handle,
- const char *path, const char *name,
- const void *value, size_t size, int flags)
-{
- SMB_STRUCT_STAT sbuf;
- struct file_id id;
- struct db_context *db;
-
- SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
+ if (size == 0) {
+ TALLOC_FREE(frame);
+ return xattr_size;
+ }
- if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
+ if (blob.length > size) {
+ TALLOC_FREE(frame);
+ errno = ERANGE;
return -1;
}
-
- id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
-
- return xattr_tdb_setattr(db, &id, name, value, size, flags);
+ memcpy(value, blob.data, xattr_size);
+ TALLOC_FREE(frame);
+ return xattr_size;
}
static int xattr_tdb_fsetxattr(struct vfs_handle_struct *handle,
const char *name, const void *value,
size_t size, int flags)
{
+ struct xattr_tdb_config *config = NULL;
SMB_STRUCT_STAT sbuf;
struct file_id id;
- struct db_context *db;
-
- SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
+ int ret;
- if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
+ if (!xattr_tdb_init(handle, &config)) {
return -1;
}
- id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
-
- return xattr_tdb_setattr(db, &id, name, value, size, flags);
-}
-
-/*
- * Worker routine for listxattr and flistxattr
- */
-
-static ssize_t xattr_tdb_listattr(struct db_context *db_ctx,
- const struct file_id *id, char *list,
- size_t size)
-{
- NTSTATUS status;
- struct tdb_xattrs *attribs;
- uint32_t i;
- size_t len = 0;
-
- status = xattr_tdb_load_attrs(talloc_tos(), db_ctx, id, &attribs);
-
- if (!NT_STATUS_IS_OK(status)) {
- DEBUG(10, ("xattr_tdb_fetch_attrs failed: %s\n",
- nt_errstr(status)));
- errno = EINVAL;
- return -1;
+ if (config->ignore_user_xattr && is_user_xattr(name)) {
+ return SMB_VFS_NEXT_FSETXATTR(
+ handle, fsp, name, value, size, flags);
}
- DEBUG(10, ("xattr_tdb_listattr: Found %d xattrs\n",
- attribs->num_eas));
-
- for (i=0; i<attribs->num_eas; i++) {
- size_t tmp;
-
- DEBUG(10, ("xattr_tdb_listattr: xattrs[i].name: %s\n",
- attribs->eas[i].name));
-
- tmp = strlen(attribs->eas[i].name);
-
- /*
- * Try to protect against overflow
- */
-
- if (len + (tmp+1) < len) {
- TALLOC_FREE(attribs);
- errno = EINVAL;
- return -1;
- }
-
- /*
- * Take care of the terminating NULL
- */
- len += (tmp + 1);
- }
-
- if (len > size) {
- TALLOC_FREE(attribs);
- errno = ERANGE;
+ if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
return -1;
}
- len = 0;
-
- for (i=0; i<attribs->num_eas; i++) {
- strlcpy(list+len, attribs->eas[i].name,
- size-len);
- len += (strlen(attribs->eas[i].name) + 1);
- }
-
- TALLOC_FREE(attribs);
- return len;
-}
-
-static ssize_t xattr_tdb_listxattr(struct vfs_handle_struct *handle,
- const char *path, char *list, size_t size)
-{
- SMB_STRUCT_STAT sbuf;
- struct file_id id;
- struct db_context *db;
-
- SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
-
- if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
- return -1;
- }
+ id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
- id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
+ ret = xattr_tdb_setattr(config->db, &id, name, value, size, flags);
+ return ret;
- return xattr_tdb_listattr(db, &id, list, size);
}
static ssize_t xattr_tdb_flistxattr(struct vfs_handle_struct *handle,
struct files_struct *fsp, char *list,
size_t size)
{
+ struct xattr_tdb_config *config = NULL;
SMB_STRUCT_STAT sbuf;
struct file_id id;
- struct db_context *db;
+ ssize_t backend_size;
+ ssize_t ret;
- SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
-
- if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
+ if (!xattr_tdb_init(handle, &config)) {
return -1;
}
- id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
-
- return xattr_tdb_listattr(db, &id, list, size);
-}
-
-/*
- * Worker routine for removexattr and fremovexattr
- */
-
-static int xattr_tdb_removeattr(struct db_context *db_ctx,
- const struct file_id *id, const char *name)
-{
- NTSTATUS status;
- struct db_record *rec;
- struct tdb_xattrs *attribs;
- uint32_t i;
- TDB_DATA value;
-
- rec = xattr_tdb_lock_attrs(talloc_tos(), db_ctx, id);
-
- if (rec == NULL) {
- DEBUG(0, ("xattr_tdb_lock_attrs failed\n"));
- errno = EINVAL;
+ if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
return -1;
}
- value = dbwrap_record_get_value(rec);
-
- status = xattr_tdb_pull_attrs(rec, &value, &attribs);
+ id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
- if (!NT_STATUS_IS_OK(status)) {
- DEBUG(10, ("xattr_tdb_fetch_attrs failed: %s\n",
- nt_errstr(status)));
- TALLOC_FREE(rec);
+ ret = xattr_tdb_listattr(config->db, &id, list, size);
+ if (ret == -1) {
return -1;
}
-
- for (i=0; i<attribs->num_eas; i++) {
- if (strcmp(attribs->eas[i].name, name) == 0) {
- break;
- }
- }
-
- if (i == attribs->num_eas) {
- TALLOC_FREE(rec);
- errno = ENOATTR;
- return -1;
+ if (ret == size) {
+ return ret;
}
-
- attribs->eas[i] =
- attribs->eas[attribs->num_eas-1];
- attribs->num_eas -= 1;
-
- if (attribs->num_eas == 0) {
- dbwrap_record_delete(rec);
- TALLOC_FREE(rec);
- return 0;
+ if (!config->ignore_user_xattr) {
+ return ret;
}
+ SMB_ASSERT(ret < size);
- status = xattr_tdb_save_attrs(rec, attribs);
-
- TALLOC_FREE(rec);
-
- if (!NT_STATUS_IS_OK(status)) {
- DEBUG(1, ("save failed: %s\n", nt_errstr(status)));
+ backend_size = SMB_VFS_NEXT_FLISTXATTR(
+ handle, fsp, list + ret, size - ret);
+ if (backend_size == -1) {
return -1;
}
- return 0;
+ return ret + backend_size;
}
-static int xattr_tdb_removexattr(struct vfs_handle_struct *handle,
- const char *path, const char *name)
+static int xattr_tdb_fremovexattr(struct vfs_handle_struct *handle,
+ struct files_struct *fsp, const char *name)
{
+ struct xattr_tdb_config *config = NULL;
SMB_STRUCT_STAT sbuf;
struct file_id id;
- struct db_context *db;
-
- SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
- if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
+ if (!xattr_tdb_init(handle, &config)) {
return -1;
}
- id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
-
- return xattr_tdb_removeattr(db, &id, name);
-}
-
-static int xattr_tdb_fremovexattr(struct vfs_handle_struct *handle,
- struct files_struct *fsp, const char *name)
-{
- SMB_STRUCT_STAT sbuf;
- struct file_id id;
- struct db_context *db;
-
- SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
+ if (config->ignore_user_xattr && is_user_xattr(name)) {
+ return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
+ }
- if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
+ if (SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf) == -1) {
return -1;
}
- id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
+ id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
- return xattr_tdb_removeattr(db, &id, name);
+ return xattr_tdb_removeattr(config->db, &id, name);
+}
+
+/*
+ * Destructor for the VFS private data
+ */
+
+static void config_destructor(void **data)
+{
+ struct xattr_tdb_config **config = (struct xattr_tdb_config **)data;
+ TALLOC_FREE((*config)->db);
}
/*
* Open the tdb file upon VFS_CONNECT
*/
-static bool xattr_tdb_init(int snum, struct db_context **p_db)
+static bool xattr_tdb_init(struct vfs_handle_struct *handle,
+ struct xattr_tdb_config **_config)
{
- struct db_context *db;
+ struct xattr_tdb_config *config = NULL;
const char *dbname;
char *def_dbname;
- def_dbname = state_path("xattr.tdb");
+ if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
+ SMB_VFS_HANDLE_GET_DATA(handle, config, struct xattr_tdb_config,
+ return false);
+ if (_config != NULL) {
+ *_config = config;
+ }
+ return true;
+ }
+
+ config = talloc_zero(handle->conn, struct xattr_tdb_config);
+ if (config == NULL) {
+ errno = ENOMEM;
+ goto error;
+ }
+
+ def_dbname = state_path(talloc_tos(), "xattr.tdb");
if (def_dbname == NULL) {
errno = ENOSYS;
- return false;
+ goto error;
}
- dbname = lp_parm_const_string(snum, "xattr_tdb", "file", def_dbname);
+ dbname = lp_parm_const_string(SNUM(handle->conn),
+ "xattr_tdb",
+ "file",
+ def_dbname);
/* now we know dbname is not NULL */
become_root();
- db = db_open(NULL, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
+ config->db = db_open(handle, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
+ DBWRAP_LOCK_ORDER_2, DBWRAP_FLAG_NONE);
unbecome_root();
- if (db == NULL) {
+ if (config->db == NULL) {
#if defined(ENOTSUP)
errno = ENOTSUP;
#else
errno = ENOSYS;
#endif
TALLOC_FREE(def_dbname);
- return false;
+ goto error;
}
-
- *p_db = db;
TALLOC_FREE(def_dbname);
+
+ config->ignore_user_xattr = lp_parm_bool(
+ SNUM(handle->conn), "xattr_tdb", "ignore_user_xattr", false);
+
+ SMB_VFS_HANDLE_SET_DATA(handle, config, config_destructor,
+ struct xattr_tdb_config, return false);
+
+ if (_config != NULL) {
+ *_config = config;
+ }
return true;
+
+error:
+ DBG_WARNING("Failed to initialize config: %s\n", strerror(errno));
+ lp_do_parameter(SNUM(handle->conn), "ea support", "False");
+ return false;
}
-/*
- * On unlink we need to delete the tdb record
- */
-static int xattr_tdb_unlink(vfs_handle_struct *handle,
- const struct smb_filename *smb_fname)
+static int xattr_tdb_openat(struct vfs_handle_struct *handle,
+ const struct files_struct *dirfsp,
+ const struct smb_filename *smb_fname,
+ struct files_struct *fsp,
+ const struct vfs_open_how *how)
{
- struct smb_filename *smb_fname_tmp = NULL;
- struct file_id id;
- struct db_context *db;
- struct db_record *rec;
- NTSTATUS status;
- int ret = -1;
- bool remove_record = false;
-
- SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
+ struct xattr_tdb_config *config = NULL;
+ SMB_STRUCT_STAT sbuf;
+ int fd;
+ int ret;
- status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp);
- if (!NT_STATUS_IS_OK(status)) {
- errno = map_errno_from_nt_status(status);
+ if (!xattr_tdb_init(handle, &config)) {
return -1;
}
- if (lp_posix_pathnames()) {
- ret = SMB_VFS_LSTAT(handle->conn, smb_fname_tmp);
- } else {
- ret = SMB_VFS_STAT(handle->conn, smb_fname_tmp);
- }
- if (ret == -1) {
- goto out;
+ fd = SMB_VFS_NEXT_OPENAT(handle,
+ dirfsp,
+ smb_fname,
+ fsp,
+ how);
+ if (fd == -1) {
+ return -1;
}
- if (smb_fname_tmp->st.st_ex_nlink == 1) {
- /* Only remove record on last link to file. */
- remove_record = true;
+ if ((how->flags & (O_CREAT|O_EXCL)) != (O_CREAT|O_EXCL)) {
+ return fd;
}
- ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
+ /*
+ * We know we used O_CREAT|O_EXCL and it worked.
+ * We must have created the file.
+ */
+ fsp_set_fd(fsp, fd);
+ ret = SMB_VFS_FSTAT(fsp, &sbuf);
+ fsp_set_fd(fsp, -1);
if (ret == -1) {
- goto out;
+ /* Can't happen... */
+ DBG_WARNING("SMB_VFS_FSTAT failed on file %s (%s)\n",
+ smb_fname_str_dbg(smb_fname),
+ strerror(errno));
+ return -1;
}
- if (!remove_record) {
- goto out;
- }
+ fsp->file_id = SMB_VFS_FILE_ID_CREATE(fsp->conn, &sbuf);
- id = SMB_VFS_FILE_ID_CREATE(handle->conn, &smb_fname_tmp->st);
+ xattr_tdb_remove_all_attrs(config->db, &fsp->file_id);
- rec = xattr_tdb_lock_attrs(talloc_tos(), db, &id);
+ return fd;
+}
- /*
- * If rec == NULL there's not much we can do about it
- */
+static int xattr_tdb_mkdirat(vfs_handle_struct *handle,
+ struct files_struct *dirfsp,
+ const struct smb_filename *smb_fname,
+ mode_t mode)
+{
+ struct xattr_tdb_config *config = NULL;
+ struct file_id fileid;
+ struct stat_ex sbuf = { .st_ex_nlink = 0, };
+ int ret;
- if (rec != NULL) {
- dbwrap_record_delete(rec);
- TALLOC_FREE(rec);
+ if (!xattr_tdb_init(handle, &config)) {
+ return -1;
}
- out:
- TALLOC_FREE(smb_fname_tmp);
- return ret;
+ ret = SMB_VFS_NEXT_MKDIRAT(handle,
+ dirfsp,
+ smb_fname,
+ mode);
+ if (ret < 0) {
+ return ret;
+ }
+
+ ret = SMB_VFS_NEXT_FSTATAT(
+ handle, dirfsp, smb_fname, &sbuf, AT_SYMLINK_NOFOLLOW);
+
+ if (ret == -1) {
+ /* Rename race. Let upper level take care of it. */
+ return -1;
+ }
+ if (!S_ISDIR(sbuf.st_ex_mode)) {
+ /* Rename race. Let upper level take care of it. */
+ return -1;
+ }
+
+ fileid = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
+
+ xattr_tdb_remove_all_attrs(config->db, &fileid);
+ return 0;
}
/*
- * On rmdir we need to delete the tdb record
+ * On unlink we need to delete the tdb record
*/
-static int xattr_tdb_rmdir(vfs_handle_struct *handle, const char *path)
+static int xattr_tdb_unlinkat(vfs_handle_struct *handle,
+ struct files_struct *dirfsp,
+ const struct smb_filename *smb_fname,
+ int flags)
{
- SMB_STRUCT_STAT sbuf;
+ struct xattr_tdb_config *config = NULL;
+ struct smb_filename *smb_fname_tmp = NULL;
+ struct smb_filename *full_fname = NULL;
struct file_id id;
- struct db_context *db;
- struct db_record *rec;
- int ret;
+ int ret = -1;
+ bool remove_record = false;
+ TALLOC_CTX *frame = NULL;
+
+ if (!xattr_tdb_init(handle, &config)) {
+ return -1;
+ }
- SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context, return -1);
+ frame = talloc_stackframe();
- if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
+ smb_fname_tmp = cp_smb_filename(frame, smb_fname);
+ if (smb_fname_tmp == NULL) {
+ TALLOC_FREE(frame);
+ errno = ENOMEM;
return -1;
}
- ret = SMB_VFS_NEXT_RMDIR(handle, path);
+ /*
+ * TODO: use SMB_VFS_STATX() once we have that
+ */
+
+ full_fname = full_path_from_dirfsp_atname(frame,
+ dirfsp,
+ smb_fname);
+ if (full_fname == NULL) {
+ goto out;
+ }
+ if (full_fname->flags & SMB_FILENAME_POSIX_PATH) {
+ ret = SMB_VFS_NEXT_LSTAT(handle, full_fname);
+ } else {
+ ret = SMB_VFS_NEXT_STAT(handle, full_fname);
+ if (ret == -1 && (errno == ENOENT || errno == ELOOP)) {
+ if (VALID_STAT(smb_fname->st) &&
+ S_ISLNK(smb_fname->st.st_ex_mode)) {
+ /*
+ * Original name was a link - Could be
+ * trying to remove a dangling symlink.
+ */
+ ret = SMB_VFS_NEXT_LSTAT(handle, full_fname);
+ }
+ }
+ }
if (ret == -1) {
- return -1;
+ goto out;
}
+ smb_fname_tmp->st = full_fname->st;
- id = SMB_VFS_FILE_ID_CREATE(handle->conn, &sbuf);
+ if (flags & AT_REMOVEDIR) {
+ /* Always remove record when removing a directory succeeds. */
+ remove_record = true;
+ } else {
+ if (smb_fname_tmp->st.st_ex_nlink == 1) {
+ /* Only remove record on last link to file. */
+ remove_record = true;
+ }
+ }
- rec = xattr_tdb_lock_attrs(talloc_tos(), db, &id);
+ ret = SMB_VFS_NEXT_UNLINKAT(handle,
+ dirfsp,
+ smb_fname_tmp,
+ flags);
- /*
- * If rec == NULL there's not much we can do about it
- */
+ if (ret == -1) {
+ goto out;
+ }
- if (rec != NULL) {
- dbwrap_record_delete(rec);
- TALLOC_FREE(rec);
+ if (!remove_record) {
+ goto out;
}
- return 0;
-}
+ id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &smb_fname_tmp->st);
-/*
- * Destructor for the VFS private data
- */
+ xattr_tdb_remove_all_attrs(config->db, &id);
-static void close_xattr_db(void **data)
-{
- struct db_context **p_db = (struct db_context **)data;
- TALLOC_FREE(*p_db);
+ out:
+ TALLOC_FREE(frame);
+ return ret;
}
static int xattr_tdb_connect(vfs_handle_struct *handle, const char *service,
{
char *sname = NULL;
int res, snum;
- struct db_context *db;
res = SMB_VFS_NEXT_CONNECT(handle, service, user);
if (res < 0) {
return 0;
}
- if (!xattr_tdb_init(snum, &db)) {
+ if (!xattr_tdb_init(handle, NULL)) {
DEBUG(5, ("Could not init xattr tdb\n"));
lp_do_parameter(snum, "ea support", "False");
return 0;
lp_do_parameter(snum, "ea support", "True");
- SMB_VFS_HANDLE_SET_DATA(handle, db, close_xattr_db,
- struct db_context, return -1);
-
return 0;
}
static struct vfs_fn_pointers vfs_xattr_tdb_fns = {
- .getxattr = xattr_tdb_getxattr,
- .fgetxattr = xattr_tdb_fgetxattr,
- .setxattr = xattr_tdb_setxattr,
- .fsetxattr = xattr_tdb_fsetxattr,
- .listxattr = xattr_tdb_listxattr,
- .flistxattr = xattr_tdb_flistxattr,
- .removexattr = xattr_tdb_removexattr,
- .fremovexattr = xattr_tdb_fremovexattr,
- .unlink = xattr_tdb_unlink,
- .rmdir = xattr_tdb_rmdir,
+ .getxattrat_send_fn = xattr_tdb_getxattrat_send,
+ .getxattrat_recv_fn = xattr_tdb_getxattrat_recv,
+ .fgetxattr_fn = xattr_tdb_fgetxattr,
+ .fsetxattr_fn = xattr_tdb_fsetxattr,
+ .flistxattr_fn = xattr_tdb_flistxattr,
+ .fremovexattr_fn = xattr_tdb_fremovexattr,
+ .openat_fn = xattr_tdb_openat,
+ .mkdirat_fn = xattr_tdb_mkdirat,
+ .unlinkat_fn = xattr_tdb_unlinkat,
.connect_fn = xattr_tdb_connect,
};
-NTSTATUS vfs_xattr_tdb_init(void);
-NTSTATUS vfs_xattr_tdb_init(void)
+static_decl_vfs;
+NTSTATUS vfs_xattr_tdb_init(TALLOC_CTX *ctx)
{
return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "xattr_tdb",
&vfs_xattr_tdb_fns);