[SCM] Samba Shared Repository - branch v3-2-test updated - initial-v3-2-test-1536-g6022873

Volker Lendecke vl at samba.org
Sat Jan 19 22:50:50 GMT 2008


The branch, v3-2-test has been updated
       via  6022873cc155bdbbd3fb620689715f07a24d6ed1 (commit)
      from  d813bd9e02d9baf916eb96c478be89f0c435e07c (commit)

http://gitweb.samba.org/?p=samba.git;a=shortlog;h=v3-2-test


- Log -----------------------------------------------------------------
commit 6022873cc155bdbbd3fb620689715f07a24d6ed1
Author: Volker Lendecke <vl at samba.org>
Date:   Sat Jan 19 23:25:36 2008 +0100

    Add streams support
    
    This is the core of the streams support. The main change is that in
    files_struct there is now a base_fsp pointer that holds the main file open
    while a stream is open. This is necessary to get the rather strange delete
    semantics right: You can't delete the main file while a stream is open without
    FILE_SHARE_DELETE, and while a stream is open a successful unlink of the main
    file leads to DELETE_PENDING for all further access on the main file or any
    stream.

-----------------------------------------------------------------------

Summary of changes:
 source/include/smb.h   |    5 +
 source/smbd/close.c    |  115 +++++++++++++++++++++++++-
 source/smbd/filename.c |  121 +++++++++++++++++++++++++++
 source/smbd/open.c     |  214 +++++++++++++++++++++++++++++++++++++++++++++---
 source/smbd/reply.c    |   28 +++++--
 5 files changed, 459 insertions(+), 24 deletions(-)


Changeset truncated at 500 lines:

diff --git a/source/include/smb.h b/source/include/smb.h
index 15e51db..2542111 100644
--- a/source/include/smb.h
+++ b/source/include/smb.h
@@ -513,6 +513,8 @@ typedef struct files_struct {
  	FAKE_FILE_HANDLE *fake_file_handle;
 
 	struct notify_change_buf *notify;
+
+	struct files_struct *base_fsp; /* placeholder for delete on close */
 } files_struct;
 
 #include "ntquotas.h"
@@ -1369,6 +1371,9 @@ struct bitmap {
 #define NTCREATEX_OPTIONS_PRIVATE_DENY_DOS     0x01000000
 #define NTCREATEX_OPTIONS_PRIVATE_DENY_FCB     0x02000000
 
+/* Private options for streams support */
+#define NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE 0x04000000
+
 /* Responses when opening a file. */
 #define FILE_WAS_SUPERSEDED 0
 #define FILE_WAS_OPENED 1
diff --git a/source/smbd/close.c b/source/smbd/close.c
index 4c385d7..4bd23a3 100644
--- a/source/smbd/close.c
+++ b/source/smbd/close.c
@@ -156,6 +156,75 @@ static void notify_deferred_opens(struct share_mode_lock *lck)
 }
 
 /****************************************************************************
+ Delete all streams
+****************************************************************************/
+
+static NTSTATUS delete_all_streams(connection_struct *conn, const char *fname)
+{
+	struct stream_struct *stream_info;
+	int i;
+	unsigned int num_streams;
+	TALLOC_CTX *frame = talloc_stackframe();
+	NTSTATUS status;
+
+	status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
+				    &num_streams, &stream_info);
+
+	if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
+		DEBUG(10, ("no streams around\n"));
+		TALLOC_FREE(frame);
+		return NT_STATUS_OK;
+	}
+
+	if (!NT_STATUS_IS_OK(status)) {
+		DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
+			   nt_errstr(status)));
+		goto fail;
+	}
+
+	DEBUG(10, ("delete_all_streams found %d streams\n",
+		   num_streams));
+
+	if (num_streams == 0) {
+		TALLOC_FREE(frame);
+		return NT_STATUS_OK;
+	}
+
+	for (i=0; i<num_streams; i++) {
+		int res;
+		char *streamname;
+
+		if (strequal(stream_info[i].name, "::$DATA")) {
+			continue;
+		}
+
+		streamname = talloc_asprintf(talloc_tos(), "%s%s", fname,
+					     stream_info[i].name);
+
+		if (streamname == NULL) {
+			DEBUG(0, ("talloc_aprintf failed\n"));
+			status = NT_STATUS_NO_MEMORY;
+			goto fail;
+		}
+
+		res = SMB_VFS_UNLINK(conn, streamname);
+
+		TALLOC_FREE(streamname);
+
+		if (res == -1) {
+			status = map_nt_error_from_unix(errno);
+			DEBUG(10, ("Could not delete stream %s: %s\n",
+				   streamname, strerror(errno)));
+			break;
+		}
+	}
+
+ fail:
+	TALLOC_FREE(frame);
+	return status;
+}
+
+/****************************************************************************
  Deal with removing a share mode on last close.
 ****************************************************************************/
 
@@ -305,6 +374,19 @@ static NTSTATUS close_remove_share_mode(files_struct *fsp,
 		goto done;
 	}
 
+	if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
+	    && !is_ntfs_stream_name(fsp->fsp_name)) {
+
+		status = delete_all_streams(conn, fsp->fsp_name);
+
+		if (!NT_STATUS_IS_OK(status)) {
+			DEBUG(5, ("delete_all_streams failed: %s\n",
+				  nt_errstr(status)));
+			goto done;
+		}
+	}
+
+
 	if (SMB_VFS_UNLINK(conn,fsp->fsp_name) != 0) {
 		/*
 		 * This call can potentially fail as another smbd may
@@ -570,12 +652,37 @@ static NTSTATUS close_stat(files_struct *fsp)
   
 NTSTATUS close_file(files_struct *fsp, enum file_close_type close_type)
 {
+	NTSTATUS status;
+	struct files_struct *base_fsp = fsp->base_fsp;
+
 	if(fsp->is_directory) {
-		return close_directory(fsp, close_type);
+		status = close_directory(fsp, close_type);
 	} else if (fsp->is_stat) {
-		return close_stat(fsp);
+		status = close_stat(fsp);
 	} else if (fsp->fake_file_handle != NULL) {
-		return close_fake_file(fsp);
+		status = close_fake_file(fsp);
+	} else {
+		status = close_normal_file(fsp, close_type);
+	}
+
+	if (!NT_STATUS_IS_OK(status)) {
+		return status;
 	}
-	return close_normal_file(fsp, close_type);
+
+	if ((base_fsp != NULL) && (close_type != SHUTDOWN_CLOSE)) {
+
+		/*
+		 * fsp was a stream, the base fsp can't be a stream as well
+		 *
+		 * For SHUTDOWN_CLOSE this is not possible here, because
+		 * SHUTDOWN_CLOSE only happens from files.c which walks the
+		 * complete list of files. If we mess with more than one fsp
+		 * those loops will become confused.
+		 */
+
+		SMB_ASSERT(base_fsp->base_fsp == NULL);
+		close_file(base_fsp, close_type);
+	}
+
+	return status;
 }
diff --git a/source/smbd/filename.c b/source/smbd/filename.c
index be4960c..1d44c74 100644
--- a/source/smbd/filename.c
+++ b/source/smbd/filename.c
@@ -28,6 +28,13 @@
 
 static bool scan_directory(connection_struct *conn, const char *path,
 			   char *name, char **found_name);
+static NTSTATUS build_stream_path(TALLOC_CTX *mem_ctx,
+				  connection_struct *conn,
+				  const char *orig_path,
+				  const char *basepath,
+				  const char *streamname,
+				  SMB_STRUCT_STAT *pst,
+				  char **path);
 
 /****************************************************************************
  Mangle the 2nd name and check if it is then equal to the first name.
@@ -119,6 +126,7 @@ NTSTATUS unix_convert(TALLOC_CTX *ctx,
 	char *start, *end;
 	char *dirpath = NULL;
 	char *name = NULL;
+	char *stream = NULL;
 	bool component_was_mangled = False;
 	bool name_has_wildcard = False;
 	NTSTATUS result;
@@ -206,6 +214,18 @@ NTSTATUS unix_convert(TALLOC_CTX *ctx,
 		return NT_STATUS_NO_MEMORY;
 	}
 
+	stream = strchr_m(name, ':');
+
+	if (stream != NULL) {
+		char *tmp = talloc_strdup(ctx, stream);
+		if (tmp == NULL) {
+			TALLOC_FREE(name);
+			return NT_STATUS_NO_MEMORY;
+		}
+		*stream = '\0';
+		stream = tmp;
+	}
+
 	/*
 	 * Large directory fix normalization. If we're case sensitive, and
 	 * the case preserving parameters are set to "no", normalize the case of
@@ -653,6 +673,20 @@ NTSTATUS unix_convert(TALLOC_CTX *ctx,
 	DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
 
  done:
+	if (stream != NULL) {
+		char *tmp = NULL;
+
+		result = build_stream_path(ctx, conn, orig_path, name, stream,
+					   pst, &tmp);
+		if (!NT_STATUS_IS_OK(result)) {
+			goto fail;
+		}
+
+		DEBUG(10, ("build_stream_path returned %s\n", tmp));
+
+		TALLOC_FREE(name);
+		name = tmp;
+	}
 	*pp_conv_path = name;
 	TALLOC_FREE(dirpath);
 	return NT_STATUS_OK;
@@ -823,3 +857,90 @@ static bool scan_directory(connection_struct *conn, const char *path,
 	errno = ENOENT;
 	return False;
 }
+
+static NTSTATUS build_stream_path(TALLOC_CTX *mem_ctx,
+				  connection_struct *conn,
+				  const char *orig_path,
+				  const char *basepath,
+				  const char *streamname,
+				  SMB_STRUCT_STAT *pst,
+				  char **path)
+{
+	SMB_STRUCT_STAT st;
+	char *result = NULL;
+	NTSTATUS status;
+	unsigned int i, num_streams;
+	struct stream_struct *streams = NULL;
+
+	result = talloc_asprintf(mem_ctx, "%s%s", basepath, streamname);
+	if (result == NULL) {
+		return NT_STATUS_NO_MEMORY;
+	}
+
+	if (SMB_VFS_STAT(conn, result, &st) == 0) {
+		*pst = st;
+		*path = result;
+		return NT_STATUS_OK;
+	}
+
+	if (errno != ENOENT) {
+		status = map_nt_error_from_unix(errno);
+		DEBUG(10, ("vfs_stat failed: %s\n", nt_errstr(status)));
+		goto fail;
+	}
+
+	status = SMB_VFS_STREAMINFO(conn, NULL, basepath, mem_ctx,
+				    &num_streams, &streams);
+
+	if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
+		SET_STAT_INVALID(*pst);
+		*path = result;
+		return NT_STATUS_OK;
+	}
+
+	if (!NT_STATUS_IS_OK(status)) {
+		DEBUG(10, ("vfs_streaminfo failed: %s\n", nt_errstr(status)));
+		goto fail;
+	}
+
+	for (i=0; i<num_streams; i++) {
+		DEBUG(10, ("comparing [%s] and [%s]: ",
+			   streamname, streams[i].name));
+		if (fname_equal(streamname, streams[i].name,
+				conn->case_sensitive)) {
+			DEBUGADD(10, ("equal\n"));
+			break;
+		}
+		DEBUGADD(10, ("not equal\n"));
+	}
+
+	if (i == num_streams) {
+		SET_STAT_INVALID(*pst);
+		*path = result;
+		TALLOC_FREE(streams);
+		return NT_STATUS_OK;
+	}
+
+	TALLOC_FREE(result);
+
+	result = talloc_asprintf(mem_ctx, "%s%s", basepath, streams[i].name);
+	if (result == NULL) {
+		status = NT_STATUS_NO_MEMORY;
+		goto fail;
+	}
+
+	SET_STAT_INVALID(*pst);
+
+	if (SMB_VFS_STAT(conn, result, pst) == 0) {
+		stat_cache_add(orig_path, result, conn->case_sensitive);
+	}
+
+	*path = result;
+	TALLOC_FREE(streams);
+	return NT_STATUS_OK;
+
+ fail:
+	TALLOC_FREE(result);
+	TALLOC_FREE(streams);
+	return status;
+}
diff --git a/source/smbd/open.c b/source/smbd/open.c
index 9d48bcc..0d6e07a 100644
--- a/source/smbd/open.c
+++ b/source/smbd/open.c
@@ -1842,7 +1842,9 @@ NTSTATUS open_file_ntcreate(connection_struct *conn,
 	set_share_mode(lck, fsp, current_user.ut.uid, 0, fsp->oplock_type, new_file_created);
 
 	/* Handle strange delete on close create semantics. */
-	if ((create_options & FILE_DELETE_ON_CLOSE) && can_set_initial_delete_on_close(lck)) {
+	if ((create_options & FILE_DELETE_ON_CLOSE)
+	    && (is_ntfs_stream_name(fname)
+		|| can_set_initial_delete_on_close(lck))) {
 		status = can_set_delete_on_close(fsp, True, new_dos_attributes);
 
 		if (!NT_STATUS_IS_OK(status)) {
@@ -2444,6 +2446,115 @@ static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx
 }
 
 /*
+ * If a main file is opened for delete, all streams need to be checked for
+ * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
+ * If that works, delete them all by setting the delete on close and close.
+ */
+
+static NTSTATUS open_streams_for_delete(connection_struct *conn,
+					const char *fname)
+{
+	struct stream_struct *stream_info;
+	files_struct **streams;
+	int i;
+	unsigned int num_streams;
+	TALLOC_CTX *frame = talloc_stackframe();
+	NTSTATUS status;
+
+	status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
+				    &num_streams, &stream_info);
+
+	if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
+		DEBUG(10, ("no streams around\n"));
+		TALLOC_FREE(frame);
+		return NT_STATUS_OK;
+	}
+
+	if (!NT_STATUS_IS_OK(status)) {
+		DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
+			   nt_errstr(status)));
+		goto fail;
+	}
+
+	DEBUG(10, ("open_streams_for_delete found %d streams\n",
+		   num_streams));
+
+	if (num_streams == 0) {
+		TALLOC_FREE(frame);
+		return NT_STATUS_OK;
+	}
+
+	streams = TALLOC_ARRAY(talloc_tos(), files_struct *, num_streams);
+	if (streams == NULL) {
+		DEBUG(0, ("talloc failed\n"));
+		status = NT_STATUS_NO_MEMORY;
+		goto fail;
+	}
+
+	for (i=0; i<num_streams; i++) {
+		char *streamname;
+
+		if (strequal(stream_info[i].name, "::$DATA")) {
+			streams[i] = NULL;
+			continue;
+		}
+
+		streamname = talloc_asprintf(talloc_tos(), "%s%s", fname,
+					     stream_info[i].name);
+
+		if (streamname == NULL) {
+			DEBUG(0, ("talloc_aprintf failed\n"));
+			status = NT_STATUS_NO_MEMORY;
+			goto fail;
+		}
+
+		status = create_file_unixpath
+			(conn,			/* conn */
+			 NULL,			/* req */
+			 streamname,		/* fname */
+			 DELETE_ACCESS,		/* access_mask */
+			 FILE_SHARE_READ | FILE_SHARE_WRITE
+			 | FILE_SHARE_DELETE,	/* share_access */
+			 FILE_OPEN,		/* create_disposition*/
+			 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* create_options */
+			 FILE_ATTRIBUTE_NORMAL,	/* file_attributes */
+			 0,			/* oplock_request */
+			 0,			/* allocation_size */
+			 NULL,			/* sd */
+			 NULL,			/* ea_list */
+			 &streams[i],		/* result */
+			 NULL,			/* pinfo */
+			 NULL);			/* psbuf */
+
+		TALLOC_FREE(streamname);
+
+		if (!NT_STATUS_IS_OK(status)) {
+			DEBUG(10, ("Could not open stream %s: %s\n",
+				   streamname, nt_errstr(status)));
+			break;
+		}
+	}
+
+	/*
+	 * don't touch the variable "status" beyond this point :-)
+	 */
+
+	for (i -= 1 ; i >= 0; i--) {
+		if (streams[i] == NULL) {
+			continue;
+		}
+
+		DEBUG(10, ("Closing stream # %d, %s\n", i,
+			   streams[i]->fsp_name));
+		close_file(streams[i], NORMAL_CLOSE);
+	}
+
+ fail:
+	TALLOC_FREE(frame);
+	return status;
+}
+
+/*
  * Wrapper around open_file_ntcreate and open_directory
  */
 
@@ -2466,6 +2577,7 @@ NTSTATUS create_file_unixpath(connection_struct *conn,
 {
 	SMB_STRUCT_STAT sbuf;
 	int info = FILE_WAS_OPENED;
+	files_struct *base_fsp = NULL;
 	files_struct *fsp = NULL;
 	NTSTATUS status;
 
@@ -2495,7 +2607,23 @@ NTSTATUS create_file_unixpath(connection_struct *conn,
 		sbuf = *psbuf;
 	}
 	else {
-		SET_STAT_INVALID(sbuf);
+		if (SMB_VFS_STAT(conn, fname, &sbuf) == -1) {
+			SET_STAT_INVALID(sbuf);
+		}
+	}
+
+	if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
+	    && (access_mask & DELETE_ACCESS)
+	    && !is_ntfs_stream_name(fname)) {
+		/*
+		 * We can't open a file with DELETE access if any of the
+		 * streams is open without FILE_SHARE_DELETE
+		 */
+		status = open_streams_for_delete(conn, fname);
+
+		if (!NT_STATUS_IS_OK(status)) {
+			goto fail;
+		}
 	}
 
 	/* This is the correct thing to do (check every time) but can_delete
@@ -2528,12 +2656,61 @@ NTSTATUS create_file_unixpath(connection_struct *conn,
 	}
 #endif
 
+	if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
+	    && is_ntfs_stream_name(fname)
+	    && (!(create_options & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
+		char *base;
+		uint32 base_create_disposition;
+
+		if (create_options & FILE_DIRECTORY_FILE) {
+			status = NT_STATUS_NOT_A_DIRECTORY;
+			goto fail;
+		}


-- 
Samba Shared Repository


More information about the samba-cvs mailing list