Add streams support
authorVolker Lendecke <vl@samba.org>
Sat, 19 Jan 2008 22:25:36 +0000 (23:25 +0100)
committerVolker Lendecke <vl@samba.org>
Sat, 19 Jan 2008 22:25:36 +0000 (23:25 +0100)
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.
(This used to be commit 6022873cc155bdbbd3fb620689715f07a24d6ed1)

source3/include/smb.h
source3/smbd/close.c
source3/smbd/filename.c
source3/smbd/open.c
source3/smbd/reply.c

index 15e51dbdd7f228e10ff3315ad2920a12249555f7..25421115c8509151600b5cc9c0af469d276e0f8d 100644 (file)
@@ -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
index 4c385d7611837f74823fa43f6d09aab48c3e07c2..4bd23a35fc804a43756acbf93e3b865b09705aab 100644 (file)
@@ -155,6 +155,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;
 }
index be4960cab819481720e7ce3015b0fa597d6dae4c..1d44c7498e951e3ae9d422d9692f4e93788b74fc 100644 (file)
 
 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;
+}
index 9d48bcc98b563e2af37a8e6a2d73364450f0aa9c..0d6e07a032374f0a65fbae3715b25675f0e12f26 100644 (file)
@@ -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)) {
@@ -2443,6 +2445,115 @@ static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx
        return result;
 }
 
+/*
+ * 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;
+               }
+
+               status = split_ntfs_stream_name(talloc_tos(), fname,
+                                               &base, NULL);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(10, ("split_ntfs_stream_name failed: %s\n",
+                                  nt_errstr(status)));
+                       goto fail;
+               }
+
+               SMB_ASSERT(!is_ntfs_stream_name(base)); /* paranoia.. */
+
+               switch (create_disposition) {
+               case FILE_OPEN:
+                       base_create_disposition = FILE_OPEN;
+                       break;
+               default:
+                       base_create_disposition = FILE_OPEN_IF;
+                       break;
+               }
+
+               status = create_file_unixpath(conn, NULL, base, 0,
+                                             FILE_SHARE_READ
+                                             | FILE_SHARE_WRITE
+                                             | FILE_SHARE_DELETE,
+                                             base_create_disposition,
+                                             0, 0, 0, 0, NULL, NULL,
+                                             &base_fsp, NULL, NULL);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(10, ("create_file_unixpath for base %s failed: "
+                                  "%s\n", base, nt_errstr(status)));
+                       goto fail;
+               }
+       }
+
        /*
         * If it's a request for a directory open, deal with it separately.
         */
 
        if (create_options & FILE_DIRECTORY_FILE) {
 
+               if (create_options & FILE_NON_DIRECTORY_FILE) {
+                       status = NT_STATUS_INVALID_PARAMETER;
+                       goto fail;
+               }
+
                /* Can't open a temp directory. IFS kit test. */
                if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
                        status = NT_STATUS_INVALID_PARAMETER;
@@ -2664,6 +2841,16 @@ NTSTATUS create_file_unixpath(connection_struct *conn,
 
        DEBUG(10, ("create_file: info=%d\n", info));
 
+       /*
+        * Set fsp->base_fsp late enough that we can't "goto fail" anymore. In
+        * the fail: branch we call close_file(fsp, ERROR_CLOSE) which would
+        * also close fsp->base_fsp which we have to also do explicitly in
+        * this routine here, as not in all "goto fail:" we have the fsp set
+        * up already to be initialized with the base_fsp.
+        */
+
+       fsp->base_fsp = base_fsp;
+
        *result = fsp;
        if (pinfo != NULL) {
                *pinfo = info;
@@ -2685,6 +2872,10 @@ NTSTATUS create_file_unixpath(connection_struct *conn,
                close_file(fsp, ERROR_CLOSE);
                fsp = NULL;
        }
+       if (base_fsp != NULL) {
+               close_file(base_fsp, ERROR_CLOSE);
+               base_fsp = NULL;
+       }
        return status;
 }
 
@@ -2812,19 +3003,18 @@ NTSTATUS create_file(connection_struct *conn,
                        status = NT_STATUS_NO_MEMORY;
                        goto fail;
                }
-       } else {
-               /*
-                * Check to see if this is a mac fork of some kind.
-                */
+       }
+
+       /*
+        * Check to see if this is a mac fork of some kind.
+        */
 
-               if (is_ntfs_stream_name(fname)) {
-                       enum FAKE_FILE_TYPE fake_file_type;
+       if (is_ntfs_stream_name(fname)) {
+               enum FAKE_FILE_TYPE fake_file_type;
 
-                       fake_file_type = is_fake_file(fname);
+               fake_file_type = is_fake_file(fname);
 
-                       if (fake_file_type == FAKE_FILE_TYPE_NONE) {
-                               return NT_STATUS_OBJECT_PATH_NOT_FOUND;
-                       }
+               if (fake_file_type != FAKE_FILE_TYPE_NONE) {
 
                        /*
                         * Here we go! support for changing the disk quotas
index 381ddfe1517b4f83d8030af1715127a86cd6394e..61ec611b6b5f6b83b3b4559370242d53735e2a38 100644 (file)
@@ -167,6 +167,10 @@ static NTSTATUS check_path_syntax_internal(char *path,
        }
 
        *d = '\0';
+
+       if (NT_STATUS_IS_OK(ret) && !posix_path) {
+               ret = split_ntfs_stream_name(NULL, path, NULL, NULL);
+       }
        return ret;
 }
 
@@ -2289,14 +2293,22 @@ static NTSTATUS do_unlink(connection_struct *conn,
        /* On open checks the open itself will check the share mode, so
           don't do it here as we'll get it wrong. */
 
-       status = open_file_ntcreate(conn, req, fname, &sbuf,
-                                   DELETE_ACCESS,
-                                   FILE_SHARE_NONE,
-                                   FILE_OPEN,
-                                   0,
-                                   FILE_ATTRIBUTE_NORMAL,
-                                   req != NULL ? 0 : INTERNAL_OPEN_ONLY,
-                                   NULL, &fsp);
+       status = create_file_unixpath
+               (conn,                  /* conn */
+                req,                   /* req */
+                fname,                 /* fname */
+                DELETE_ACCESS,         /* access_mask */
+                FILE_SHARE_NONE,       /* share_access */
+                FILE_OPEN,             /* create_disposition*/
+                FILE_NON_DIRECTORY_FILE, /* create_options */
+                FILE_ATTRIBUTE_NORMAL, /* file_attributes */
+                0,                     /* oplock_request */
+                0,                     /* allocation_size */
+                NULL,                  /* sd */
+                NULL,                  /* ea_list */
+                &fsp,                  /* result */
+                NULL,                  /* pinfo */
+                &sbuf);                /* psbuf */
 
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(10, ("open_file_ntcreate failed: %s\n",