s3:smbd: move pending_auth_data list to struct smbd_server_connection
[kai/samba.git] / source3 / smbd / filename.c
index 6ab179a3163ab5f3282aea7c7f2094042d7e8ac5..059dca29c8dfc3abfac337b953fd182c80401558 100644 (file)
@@ -1,21 +1,21 @@
-/* 
+/*
    Unix SMB/CIFS implementation.
    filename handling routines
    Copyright (C) Andrew Tridgell 1992-1998
-   Copyright (C) Jeremy Allison 1999-2004
+   Copyright (C) Jeremy Allison 1999-2007
    Copyright (C) Ying Chen 2000
    Copyright (C) Volker Lendecke 2007
-   
+
    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
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
 
-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,
+                                 struct smb_filename *smb_fname);
 
 /****************************************************************************
  Mangle the 2nd name and check if it is then equal to the first name.
 ****************************************************************************/
 
-static BOOL mangled_equal(const char *name1, const char *name2,
-                         const struct share_params *p)
+static bool mangled_equal(const char *name1,
+                       const char *name2,
+                       const struct share_params *p)
 {
-       pstring tmpname;
-       
-       pstrcpy(tmpname, name2);
-       mangle_map(tmpname, True, False, p);
-       return strequal(name1, tmpname);
+       char mname[13];
+
+       if (!name_to_8_3(name2, mname, False, p)) {
+               return False;
+       }
+       return strequal(name1, mname);
 }
 
 /****************************************************************************
  Cope with the differing wildcard and non-wildcard error cases.
 ****************************************************************************/
 
-static NTSTATUS determine_path_error(const char *name, BOOL allow_wcard_last_component)
+static NTSTATUS determine_path_error(const char *name,
+                       bool allow_wcard_last_component)
 {
        const char *p;
 
@@ -74,66 +79,98 @@ static NTSTATUS determine_path_error(const char *name, BOOL allow_wcard_last_com
                return NT_STATUS_OBJECT_PATH_NOT_FOUND;
        }
 }
-       
+
+NTSTATUS get_full_smb_filename(TALLOC_CTX *ctx, const struct smb_filename *smb_fname,
+                              char **full_name)
+{
+       if (smb_fname->stream_name) {
+               *full_name = talloc_asprintf(ctx, "%s%s", smb_fname->base_name,
+                                            smb_fname->stream_name);
+       } else {
+               *full_name = talloc_strdup(ctx, smb_fname->base_name);
+       }
+
+       if (!*full_name) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       return NT_STATUS_OK;
+}
+
 /****************************************************************************
 This routine is called to convert names from the dos namespace to unix
-namespace. It needs to handle any case conversions, mangling, format
-changes etc.
+namespace. It needs to handle any case conversions, mangling, format changes,
+streams etc.
 
 We assume that we have already done a chdir() to the right "root" directory
 for this service.
 
-The function will return an NTSTATUS error if some part of the name except for the last
-part cannot be resolved, else NT_STATUS_OK.
+The function will return an NTSTATUS error if some part of the name except for
+the last part cannot be resolved, else NT_STATUS_OK.
 
-Note NT_STATUS_OK doesn't mean the name exists or is valid, just that we didn't
-get any fatal errors that should immediately terminate the calling
-SMB processing whilst resolving.
+Note NT_STATUS_OK doesn't mean the name exists or is valid, just that we
+didn't get any fatal errors that should immediately terminate the calling SMB
+processing whilst resolving.
 
-If the saved_last_component != 0, then the unmodified last component
-of the pathname is returned there. This is used in an exceptional
-case in reply_mv (so far). If saved_last_component == 0 then nothing
-is returned there.
+If the UCF_SAVE_LCOMP flag is passed in, then the unmodified last component
+of the pathname is set in smb_filename->original_lcomp.
 
-If last_component_wcard is true then a MS wildcard was detected and
+If UCF_ALLOW_WCARD_LCOMP is passed in, then a MS wildcard was detected and
 should be allowed in the last component of the path only.
 
-On exit from unix_convert, if *pst was not null, then the file stat
-struct will be returned if the file exists and was found, if not this
-stat struct will be filled with zeros (and this can be detected by checking
-for nlinks = 0, which can never be true for any file).
+If the orig_path was a stream, smb_filename->base_name will point to the base
+filename, and smb_filename->stream_name will point to the stream name.  If
+orig_path was not a stream, then smb_filename->stream_name will be NULL.
+
+On exit from unix_convert, the smb_filename->st stat struct will be populated
+if the file exists and was found, if not this stat struct will be filled with
+zeros (and this can be detected by checking for nlinks = 0, which can never be
+true for any file).
 ****************************************************************************/
 
-NTSTATUS unix_convert(connection_struct *conn,
-                       pstring orig_path,
-                       BOOL allow_wcard_last_component,
-                       char *saved_last_component,
-                       SMB_STRUCT_STAT *pst)
+NTSTATUS unix_convert(TALLOC_CTX *ctx,
+                     connection_struct *conn,
+                     const char *orig_path,
+                     struct smb_filename **smb_fname_out,
+                     uint32_t ucf_flags)
 {
        SMB_STRUCT_STAT st;
+       struct smb_filename *smb_fname = NULL;
        char *start, *end;
        char *dirpath = NULL;
        char *name = NULL;
-       BOOL component_was_mangled = False;
-       BOOL name_has_wildcard = False;
+       char *stream = NULL;
+       bool component_was_mangled = False;
+       bool name_has_wildcard = False;
+       bool posix_pathnames = false;
+       bool allow_wcard_last_component = ucf_flags & UCF_ALLOW_WCARD_LCOMP;
+       bool save_last_component = ucf_flags & UCF_SAVE_LCOMP;
        NTSTATUS result;
+       int ret = -1;
 
-       SET_STAT_INVALID(*pst);
+       *smb_fname_out = NULL;
 
-       if(saved_last_component) {
-               *saved_last_component = 0;
+       smb_fname = TALLOC_ZERO_P(talloc_tos(), struct smb_filename);
+       if (smb_fname == NULL) {
+               return NT_STATUS_NO_MEMORY;
        }
 
        if (conn->printer) {
                /* we don't ever use the filenames on a printer share as a
                        filename - so don't convert them */
+               if (!(smb_fname->base_name = talloc_strdup(smb_fname,
+                                                          orig_path))) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+               *smb_fname_out = smb_fname;
                return NT_STATUS_OK;
        }
 
        DEBUG(5, ("unix_convert called on file \"%s\"\n", orig_path));
 
        /*
-        * Conversion to basic unix format is already done in check_path_syntax().
+        * Conversion to basic unix format is already done in
+        * check_path_syntax().
         */
 
        /*
@@ -154,18 +191,20 @@ NTSTATUS unix_convert(connection_struct *conn,
         */
 
        if (!*orig_path) {
-               if (!(name = SMB_STRDUP("."))) {
-                       result = NT_STATUS_NO_MEMORY;
-                       goto fail;
+               if (!(name = talloc_strdup(ctx,"."))) {
+                       return NT_STATUS_NO_MEMORY;
                }
                if (SMB_VFS_STAT(conn,name,&st) == 0) {
-                       *pst = st;
+                       smb_fname->st = st;
+               } else {
+                       return map_nt_error_from_unix(errno);
                }
                DEBUG(5,("conversion finished \"\" -> %s\n",name));
                goto done;
        }
 
-       if (orig_path[0] == '.' && (orig_path[1] == '/' || orig_path[1] == '\0')) {
+       if (orig_path[0] == '.' && (orig_path[1] == '/' ||
+                               orig_path[1] == '\0')) {
                /* Start of pathname can't be "." only. */
                if (orig_path[1] == '\0' || orig_path[2] == '\0') {
                        result = NT_STATUS_OBJECT_NAME_INVALID;
@@ -173,24 +212,11 @@ NTSTATUS unix_convert(connection_struct *conn,
                        result =determine_path_error(
                                &orig_path[2], allow_wcard_last_component);
                }
-               goto fail;
+               return result;
        }
 
-       /*
-        * Ensure saved_last_component is valid even if file exists.
-        */
-
-       if(saved_last_component) {
-               end = strrchr_m(orig_path, '/');
-               if (end) {
-                       pstrcpy(saved_last_component, end + 1);
-               } else {
-                       pstrcpy(saved_last_component, orig_path);
-               }
-       }
-
-       if (!(name = SMB_STRDUP(orig_path))) {
-               DEBUG(0, ("strdup failed\n"));
+       if (!(name = talloc_strdup(ctx, orig_path))) {
+               DEBUG(0, ("talloc_strdup failed\n"));
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -203,15 +229,54 @@ NTSTATUS unix_convert(connection_struct *conn,
         * the man page. Thanks to jht@samba.org for finding this. JRA.
         */
 
-       if (conn->case_sensitive && !conn->case_preserve && !conn->short_case_preserve) {
+       if (conn->case_sensitive && !conn->case_preserve &&
+                       !conn->short_case_preserve) {
                strnorm(name, lp_defaultcase(SNUM(conn)));
        }
 
+       /*
+        * Ensure saved_last_component is valid even if file exists.
+        */
+
+       if(save_last_component) {
+               end = strrchr_m(name, '/');
+               if (end) {
+                       smb_fname->original_lcomp = talloc_strdup(ctx,
+                                                                 end + 1);
+               } else {
+                       smb_fname->original_lcomp = talloc_strdup(ctx, name);
+               }
+       }
+
+       posix_pathnames = lp_posix_pathnames();
+
+       /* Strip off the stream. Should we use any of the other stream parsing
+        * at this point? Also, should we set the is_stream bit? */
+       if (!posix_pathnames) {
+               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;
+               }
+       }
+
        start = name;
 
-       if(!conn->case_sensitive
-          && stat_cache_lookup(conn, &name, &dirpath, &start, &st)) {
-               *pst = st;
+       /* If we're providing case insentive semantics or
+        * the underlying filesystem is case insensitive,
+        * then a case-normalized hit in the stat-cache is
+        * authoratitive. JRA.
+        */
+
+       if((!conn->case_sensitive || !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) &&
+                       stat_cache_lookup(conn, &name, &dirpath, &start, &st)) {
+               smb_fname->st = st;
                goto done;
        }
 
@@ -220,17 +285,23 @@ NTSTATUS unix_convert(connection_struct *conn,
         * building the directories with asprintf and free it.
         */
 
-       if ((dirpath == NULL) && (!(dirpath = SMB_STRDUP("")))) {
-               DEBUG(0, ("strdup failed\n"));
-               result = NT_STATUS_NO_MEMORY;
-               goto fail;
+       if ((dirpath == NULL) && (!(dirpath = talloc_strdup(ctx,"")))) {
+               DEBUG(0, ("talloc_strdup failed\n"));
+               TALLOC_FREE(name);
+               return NT_STATUS_NO_MEMORY;
        }
 
        /*
         * stat the name - if it exists then we are all done!
         */
 
-       if (SMB_VFS_STAT(conn,name,&st) == 0) {
+       if (posix_pathnames) {
+               ret = SMB_VFS_LSTAT(conn,name,&st);
+       } else {
+               ret = SMB_VFS_STAT(conn,name,&st);
+       }
+
+       if (ret == 0) {
                /* Ensure we catch all names with in "/."
                   this is disallowed under Windows. */
                const char *p = strstr(name, "/."); /* mb safe. */
@@ -247,20 +318,21 @@ NTSTATUS unix_convert(connection_struct *conn,
                }
                stat_cache_add(orig_path, name, conn->case_sensitive);
                DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
-               *pst = st;
+               smb_fname->st = st;
                goto done;
        }
 
-       DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n", name, dirpath, start));
+       DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n",
+                               name, dirpath, start));
 
        /*
         * A special case - if we don't have any mangling chars and are case
-        * sensitive then searching won't help.
+        * sensitive or the underlying filesystem is case insentive then searching
+        * won't help.
         */
 
-       if (conn->case_sensitive &&
-                       !mangle_is_mangled(name, conn->params) &&
-                       !*lp_mangled_map(conn->params)) {
+       if ((conn->case_sensitive || !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) &&
+                       !mangle_is_mangled(name, conn->params)) {
                goto done;
        }
 
@@ -287,7 +359,8 @@ NTSTATUS unix_convert(connection_struct *conn,
                /*
                 * Pinpoint the end of this section of the filename.
                 */
-               end = strchr(start, '/'); /* mb safe. '/' can't be in any encoded char. */
+               /* mb safe. '/' can't be in any encoded char. */
+               end = strchr(start, '/');
 
                /*
                 * Chop the name at this point.
@@ -296,8 +369,14 @@ NTSTATUS unix_convert(connection_struct *conn,
                        *end = 0;
                }
 
-               if (saved_last_component != 0) {
-                       pstrcpy(saved_last_component, end ? end + 1 : start);
+               if (save_last_component) {
+                       TALLOC_FREE(smb_fname->original_lcomp);
+                       smb_fname->original_lcomp = talloc_strdup(ctx,
+                                                       end ? end + 1 : start);
+                       if (!smb_fname->original_lcomp) {
+                               DEBUG(0, ("talloc failed\n"));
+                               return NT_STATUS_NO_MEMORY;
+                       }
                }
 
                /* The name cannot have a component of "." */
@@ -334,22 +413,30 @@ NTSTATUS unix_convert(connection_struct *conn,
                 * Check if the name exists up to this point.
                 */
 
-               if (SMB_VFS_STAT(conn,name, &st) == 0) {
+               if (posix_pathnames) {
+                       ret = SMB_VFS_LSTAT(conn,name, &st);
+               } else {
+                       ret = SMB_VFS_STAT(conn,name, &st);
+               }
+
+               if (ret == 0) {
                        /*
-                        * It exists. it must either be a directory or this must be
-                        * the last part of the path for it to be OK.
+                        * It exists. it must either be a directory or this must
+                        * be the last part of the path for it to be OK.
                         */
-                       if (end && !(st.st_mode & S_IFDIR)) {
+                       if (end && !S_ISDIR(st.st_ex_mode)) {
                                /*
-                                * An intermediate part of the name isn't a directory.
+                                * An intermediate part of the name isn't
+                                * a directory.
                                 */
                                DEBUG(5,("Not a dir %s\n",start));
                                *end = '/';
                                /*
-                                * We need to return the fact that the intermediate
-                                * name resolution failed. This is used to return an
-                                * error of ERRbadpath rather than ERRbadfile. Some
-                                * Windows applications depend on the difference between
+                                * We need to return the fact that the
+                                * intermediate name resolution failed. This
+                                * is used to return an error of ERRbadpath
+                                * rather than ERRbadfile. Some Windows
+                                * applications depend on the difference between
                                 * these two errors.
                                 */
                                result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
@@ -358,12 +445,12 @@ NTSTATUS unix_convert(connection_struct *conn,
 
                        if (!end) {
                                /*
-                                * We just scanned for, and found the end of the path.
-                                * We must return the valid stat struct.
-                                * JRA.
+                                * We just scanned for, and found the end of
+                                * the path. We must return the valid stat
+                                * struct. JRA.
                                 */
 
-                               *pst = st;
+                               smb_fname->st = st;
                        }
 
                } else {
@@ -372,30 +459,39 @@ NTSTATUS unix_convert(connection_struct *conn,
                        /* Stat failed - ensure we don't use it. */
                        SET_STAT_INVALID(st);
 
-                       /* Reset errno so we can detect directory open errors. */
+                       /*
+                        * Reset errno so we can detect
+                        * directory open errors.
+                        */
                        errno = 0;
 
                        /*
                         * Try to find this part of the path in the directory.
                         */
 
-                       if (name_has_wildcard || 
-                           !scan_directory(conn, dirpath, start, &found_name)) {
+                       if (name_has_wildcard ||
+                           (get_real_filename(conn, dirpath, start,
+                                              talloc_tos(),
+                                              &found_name) == -1)) {
                                char *unmangled;
 
                                if (end) {
                                        /*
-                                        * An intermediate part of the name can't be found.
+                                        * An intermediate part of the name
+                                        * can't be found.
                                         */
-                                       DEBUG(5,("Intermediate not found %s\n",start));
+                                       DEBUG(5,("Intermediate not found %s\n",
+                                                       start));
                                        *end = '/';
 
                                        /*
-                                        * We need to return the fact that the intermediate
-                                        * name resolution failed. This is used to return an
-                                        * error of ERRbadpath rather than ERRbadfile. Some
-                                        * Windows applications depend on the difference between
-                                        * these two errors.
+                                        * We need to return the fact that the
+                                        * intermediate name resolution failed.
+                                        * This is used to return an error of
+                                        * ERRbadpath rather than ERRbadfile.
+                                        * Some Windows applications depend on
+                                        * the difference between these two
+                                        * errors.
                                         */
 
                                        /*
@@ -407,16 +503,24 @@ NTSTATUS unix_convert(connection_struct *conn,
                                        if (errno == ENOENT ||
                                                        errno == ENOTDIR ||
                                                        errno == ELOOP) {
-                                               result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
+                                               result =
+                                               NT_STATUS_OBJECT_PATH_NOT_FOUND;
                                        }
                                        else {
-                                               result = map_nt_error_from_unix(errno);
+                                               result =
+                                               map_nt_error_from_unix(errno);
                                        }
                                        goto fail;
                                }
 
-                               /* ENOENT is the only valid error here. */
-                               if (errno != ENOENT) {
+                               /*
+                                * ENOENT/EACCESS are the only valid errors
+                                * here. EACCESS needs handling here for
+                                * "dropboxes", i.e. directories where users
+                                * can only put stuff with permission -wx.
+                                */
+                               if ((errno != 0) && (errno != ENOENT)
+                                   && (errno != EACCES)) {
                                        /*
                                         * ENOTDIR and ELOOP both map to
                                         * NT_STATUS_OBJECT_PATH_NOT_FOUND
@@ -424,10 +528,11 @@ NTSTATUS unix_convert(connection_struct *conn,
                                         */
                                        if (errno == ENOTDIR ||
                                                        errno == ELOOP) {
-                                               result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
-                                       }
-                                       else {
-                                               result = map_nt_error_from_unix(errno);
+                                               result =
+                                               NT_STATUS_OBJECT_PATH_NOT_FOUND;
+                                       } else {
+                                               result =
+                                               map_nt_error_from_unix(errno);
                                        }
                                        goto fail;
                                }
@@ -435,39 +540,45 @@ NTSTATUS unix_convert(connection_struct *conn,
                                /*
                                 * Just the last part of the name doesn't exist.
                                 * We need to strupper() or strlower() it as
-                                * this conversion may be used for file creation 
-                                * purposes. Fix inspired by Thomas Neumann <t.neumann@iku-ag.de>.
+                                * this conversion may be used for file creation
+                                * purposes. Fix inspired by
+                                * Thomas Neumann <t.neumann@iku-ag.de>.
                                 */
                                if (!conn->case_preserve ||
-                                   (mangle_is_8_3(start, False, conn->params) &&
+                                   (mangle_is_8_3(start, False,
+                                                  conn->params) &&
                                                 !conn->short_case_preserve)) {
-                                       strnorm(start, lp_defaultcase(SNUM(conn)));
+                                       strnorm(start,
+                                               lp_defaultcase(SNUM(conn)));
                                }
 
                                /*
-                                * check on the mangled stack to see if we can recover the 
-                                * base of the filename.
+                                * check on the mangled stack to see if we can
+                                * recover the base of the filename.
                                 */
 
                                if (mangle_is_mangled(start, conn->params)
-                                   && mangle_check_cache_alloc(start, &unmangled,
-                                                               conn->params)) {
+                                   && mangle_lookup_name_from_8_3(ctx,
+                                                       start,
+                                                       &unmangled,
+                                                       conn->params)) {
                                        char *tmp;
                                        size_t start_ofs = start - name;
 
                                        if (*dirpath != '\0') {
-                                               asprintf(&tmp, "%s/%s", dirpath,
-                                                        unmangled);
-                                               SAFE_FREE(unmangled);
+                                               tmp = talloc_asprintf(ctx,
+                                                       "%s/%s", dirpath,
+                                                       unmangled);
+                                               TALLOC_FREE(unmangled);
                                        }
                                        else {
                                                tmp = unmangled;
                                        }
                                        if (tmp == NULL) {
-                                               DEBUG(0, ("malloc failed\n"));
-                                               result = NT_STATUS_NO_MEMORY;
+                                               DEBUG(0, ("talloc failed\n"));
+                                               return NT_STATUS_NO_MEMORY;
                                        }
-                                       SAFE_FREE(name);
+                                       TALLOC_FREE(name);
                                        name = tmp;
                                        start = name + start_ofs;
                                        end = start + strlen(start);
@@ -479,26 +590,28 @@ NTSTATUS unix_convert(connection_struct *conn,
 
 
                        /*
-                        * Restore the rest of the string. If the string was mangled the size
-                        * may have changed.
+                        * Restore the rest of the string. If the string was
+                        * mangled the size may have changed.
                         */
                        if (end) {
                                char *tmp;
                                size_t start_ofs = start - name;
 
                                if (*dirpath != '\0') {
-                                       asprintf(&tmp, "%s/%s/%s", dirpath,
-                                                found_name, end+1);
+                                       tmp = talloc_asprintf(ctx,
+                                               "%s/%s/%s", dirpath,
+                                               found_name, end+1);
                                }
                                else {
-                                       asprintf(&tmp, "%s/%s", found_name,
-                                                end+1);
+                                       tmp = talloc_asprintf(ctx,
+                                               "%s/%s", found_name,
+                                               end+1);
                                }
                                if (tmp == NULL) {
-                                       DEBUG(0, ("asprintf failed\n"));
-                                       result = NT_STATUS_NO_MEMORY;
+                                       DEBUG(0, ("talloc_asprintf failed\n"));
+                                       return NT_STATUS_NO_MEMORY;
                                }
-                               SAFE_FREE(name);
+                               TALLOC_FREE(name);
                                name = tmp;
                                start = name + start_ofs;
                                end = start + strlen(found_name);
@@ -508,63 +621,78 @@ NTSTATUS unix_convert(connection_struct *conn,
                                size_t start_ofs = start - name;
 
                                if (*dirpath != '\0') {
-                                       asprintf(&tmp, "%s/%s", dirpath,
-                                                found_name);
-                               }
-                               else {
-                                       tmp = SMB_STRDUP(found_name);
+                                       tmp = talloc_asprintf(ctx,
+                                               "%s/%s", dirpath,
+                                               found_name);
+                               } else {
+                                       tmp = talloc_strdup(ctx,
+                                               found_name);
                                }
                                if (tmp == NULL) {
-                                       DEBUG(0, ("malloc failed\n"));
-                                       result = NT_STATUS_NO_MEMORY;
-                                       goto fail;
+                                       DEBUG(0, ("talloc failed\n"));
+                                       return NT_STATUS_NO_MEMORY;
                                }
-                               SAFE_FREE(name);
+                               TALLOC_FREE(name);
                                name = tmp;
                                start = name + start_ofs;
 
                                /*
-                                * We just scanned for, and found the end of the path.
-                                * We must return a valid stat struct if it exists.
-                                * JRA.
+                                * We just scanned for, and found the end of
+                                * the path. We must return a valid stat struct
+                                * if it exists. JRA.
                                 */
 
-                               if (SMB_VFS_STAT(conn,name, &st) == 0) {
-                                       *pst = st;
+                               if (posix_pathnames) {
+                                       ret = SMB_VFS_LSTAT(conn,name, &st);
+                               } else {
+                                       ret = SMB_VFS_STAT(conn,name, &st);
+                               }
+
+                               if (ret == 0) {
+                                       smb_fname->st = st;
                                } else {
                                        SET_STAT_INVALID(st);
                                }
                        }
 
-                       SAFE_FREE(found_name);
+                       TALLOC_FREE(found_name);
                } /* end else */
 
 #ifdef DEVELOPER
-               if (VALID_STAT(st) &&
-                   get_delete_on_close_flag(vfs_file_id_from_sbuf(conn, &st))) {
-                       result = NT_STATUS_DELETE_PENDING;
-                       goto fail;
+               /*
+                * This sucks!
+                * We should never provide different behaviors
+                * depending on DEVELOPER!!!
+                */
+               if (VALID_STAT(st)) {
+                       bool delete_pending;
+                       get_file_infos(vfs_file_id_from_sbuf(conn, &st),
+                                      &delete_pending, NULL);
+                       if (delete_pending) {
+                               result = NT_STATUS_DELETE_PENDING;
+                               goto fail;
+                       }
                }
 #endif
 
-               /* 
+               /*
                 * Add to the dirpath that we have resolved so far.
                 */
 
                if (*dirpath != '\0') {
-                       char *tmp;
-
-                       if (asprintf(&tmp, "%s/%s", dirpath, start) == -1) {
-                               DEBUG(0, ("asprintf failed\n"));
+                       char *tmp = talloc_asprintf(ctx,
+                                       "%s/%s", dirpath, start);
+                       if (!tmp) {
+                               DEBUG(0, ("talloc_asprintf failed\n"));
                                return NT_STATUS_NO_MEMORY;
                        }
-                       SAFE_FREE(dirpath);
+                       TALLOC_FREE(dirpath);
                        dirpath = tmp;
                }
                else {
-                       SAFE_FREE(dirpath);
-                       if (!(dirpath = SMB_STRDUP(start))) {
-                               DEBUG(0, ("strdup failed\n"));
+                       TALLOC_FREE(dirpath);
+                       if (!(dirpath = talloc_strdup(ctx,start))) {
+                               DEBUG(0, ("talloc_strdup failed\n"));
                                return NT_STATUS_NO_MEMORY;
                        }
                }
@@ -575,7 +703,8 @@ NTSTATUS unix_convert(connection_struct *conn,
                 */
 
                if(!component_was_mangled && !name_has_wildcard) {
-                       stat_cache_add(orig_path, dirpath, conn->case_sensitive);
+                       stat_cache_add(orig_path, dirpath,
+                                       conn->case_sensitive);
                }
 
                /*
@@ -602,11 +731,36 @@ NTSTATUS unix_convert(connection_struct *conn,
        DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
 
  done:
-       result = NT_STATUS_OK;
+       smb_fname->base_name = name;
+
+       if (stream != NULL) {
+               smb_fname->stream_name = stream;
+
+               /* Check path now that the base_name has been converted. */
+               result = build_stream_path(ctx, conn, orig_path, smb_fname);
+               if (!NT_STATUS_IS_OK(result)) {
+                       goto fail;
+               }
+       }
+       TALLOC_FREE(dirpath);
+       *smb_fname_out = smb_fname;
+       return NT_STATUS_OK;
  fail:
-       pstrcpy(orig_path, name);
-       SAFE_FREE(name);
-       SAFE_FREE(dirpath);
+       DEBUG(10, ("dirpath = [%s] start = [%s]\n", dirpath, start));
+       if (*dirpath != '\0') {
+               smb_fname->base_name = talloc_asprintf(ctx, "%s/%s", dirpath,
+                                                      start);
+       } else {
+               smb_fname->base_name = talloc_strdup(ctx, start);
+       }
+       if (!smb_fname->base_name) {
+               DEBUG(0, ("talloc_asprintf failed\n"));
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       *smb_fname_out = smb_fname;
+       TALLOC_FREE(name);
+       TALLOC_FREE(dirpath);
        return result;
 }
 
@@ -617,12 +771,14 @@ NTSTATUS unix_convert(connection_struct *conn,
  a valid one for the user to access.
 ****************************************************************************/
 
-NTSTATUS check_name(connection_struct *conn, const pstring name)
+NTSTATUS check_name(connection_struct *conn, const char *name)
 {
        if (IS_VETO_PATH(conn, name))  {
                /* Is it not dot or dot dot. */
-               if (!((name[0] == '.') && (!name[1] || (name[1] == '.' && !name[2])))) {
-                       DEBUG(5,("check_name: file path name %s vetoed\n",name));
+               if (!((name[0] == '.') && (!name[1] ||
+                                       (name[1] == '.' && !name[2])))) {
+                       DEBUG(5,("check_name: file path name %s vetoed\n",
+                                               name));
                        return map_nt_error_from_unix(ENOENT);
                }
        }
@@ -630,7 +786,8 @@ NTSTATUS check_name(connection_struct *conn, const pstring name)
        if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
                NTSTATUS status = check_reduced_name(conn,name);
                if (!NT_STATUS_IS_OK(status)) {
-                       DEBUG(5,("check_name: name %s failed with %s\n",name, nt_errstr(status)));
+                       DEBUG(5,("check_name: name %s failed with %s\n",name,
+                                               nt_errstr(status)));
                        return status;
                }
        }
@@ -643,11 +800,13 @@ NTSTATUS check_name(connection_struct *conn, const pstring name)
  This needs to be careful about whether we are case sensitive.
 ****************************************************************************/
 
-static BOOL fname_equal(const char *name1, const char *name2, BOOL case_sensitive)
+static bool fname_equal(const char *name1, const char *name2,
+               bool case_sensitive)
 {
        /* Normal filename handling */
-       if (case_sensitive)
+       if (case_sensitive) {
                return(strcmp(name1,name2) == 0);
+       }
 
        return(strequal(name1,name2));
 }
@@ -657,25 +816,34 @@ static BOOL fname_equal(const char *name1, const char *name2, BOOL case_sensitiv
  If the name looks like a mangled name then try via the mangling functions
 ****************************************************************************/
 
-static BOOL scan_directory(connection_struct *conn, const char *path,
-                          char *name, char **found_name)
+static int get_real_filename_full_scan(connection_struct *conn,
+                                      const char *path, const char *name,
+                                      bool mangled,
+                                      TALLOC_CTX *mem_ctx, char **found_name)
 {
        struct smb_Dir *cur_dir;
        const char *dname;
-       BOOL mangled;
        char *unmangled_name = NULL;
        long curpos;
 
-       mangled = mangle_is_mangled(name, conn->params);
-
        /* handle null paths */
-       if ((path == NULL) || (*path == 0))
+       if ((path == NULL) || (*path == 0)) {
                path = ".";
+       }
+
+       /* If we have a case-sensitive filesystem, it doesn't do us any
+        * good to search for a name. If a case variation of the name was
+        * there, then the original stat(2) would have found it.
+        */
+       if (!mangled && !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) {
+               errno = ENOENT;
+               return -1;
+       }
 
        /*
         * The incoming name can be mangled, and if we de-mangle it
         * here it will not compare correctly against the filename (name2)
-        * read from the directory and then mangled by the mangle_map()
+        * read from the directory and then mangled by the name_to_8_3()
         * call. We need to mangle both names or neither.
         * (JRA).
         *
@@ -688,31 +856,35 @@ static BOOL scan_directory(connection_struct *conn, const char *path,
         */
 
        if (mangled && !conn->case_sensitive) {
-               mangled = !mangle_check_cache_alloc(name, &unmangled_name,
-                                                   conn->params);
-               name = unmangled_name;
+               mangled = !mangle_lookup_name_from_8_3(talloc_tos(), name,
+                                                      &unmangled_name,
+                                                      conn->params);
+               if (!mangled) {
+                       /* Name is now unmangled. */
+                       name = unmangled_name;
+               }
        }
 
        /* open the directory */
-       if (!(cur_dir = OpenDir(conn, path, NULL, 0))) {
+       if (!(cur_dir = OpenDir(talloc_tos(), conn, path, NULL, 0))) {
                DEBUG(3,("scan dir didn't open dir [%s]\n",path));
-               SAFE_FREE(unmangled_name);
-               return(False);
+               TALLOC_FREE(unmangled_name);
+               return -1;
        }
 
        /* now scan for matching names */
        curpos = 0;
-       while ((dname = ReadDirName(cur_dir, &curpos))) {
+       while ((dname = ReadDirName(cur_dir, &curpos, NULL))) {
 
                /* Is it dot or dot dot. */
-               if ((dname[0] == '.') && (!dname[1] || (dname[1] == '.' && !dname[2]))) {
+               if (ISDOT(dname) || ISDOTDOT(dname)) {
                        continue;
                }
 
                /*
                 * At this point dname is the unmangled name.
-                * name is either mangled or not, depending on the state of the "mangled"
-                * variable. JRA.
+                * name is either mangled or not, depending on the state
+                * of the "mangled" variable. JRA.
                 */
 
                /*
@@ -720,17 +892,143 @@ static BOOL scan_directory(connection_struct *conn, const char *path,
                 * against unmangled name.
                 */
 
-               if ((mangled && mangled_equal(name,dname,conn->params)) || fname_equal(name, dname, conn->case_sensitive)) {
+               if ((mangled && mangled_equal(name,dname,conn->params)) ||
+                       fname_equal(name, dname, conn->case_sensitive)) {
                        /* we've found the file, change it's name and return */
-                       *found_name = SMB_STRDUP(dname);
-                       SAFE_FREE(unmangled_name);
-                       CloseDir(cur_dir);
-                       return(True);
+                       *found_name = talloc_strdup(mem_ctx, dname);
+                       TALLOC_FREE(unmangled_name);
+                       TALLOC_FREE(cur_dir);
+                       if (!*found_name) {
+                               errno = ENOMEM;
+                               return -1;
+                       }
+                       return 0;
                }
        }
 
-       SAFE_FREE(unmangled_name);
-       CloseDir(cur_dir);
+       TALLOC_FREE(unmangled_name);
+       TALLOC_FREE(cur_dir);
        errno = ENOENT;
-       return(False);
+       return -1;
+}
+
+/****************************************************************************
+ Wrapper around the vfs get_real_filename and the full directory scan
+ fallback.
+****************************************************************************/
+
+int get_real_filename(connection_struct *conn, const char *path,
+                     const char *name, TALLOC_CTX *mem_ctx,
+                     char **found_name)
+{
+       int ret;
+       bool mangled;
+
+       mangled = mangle_is_mangled(name, conn->params);
+
+       if (mangled) {
+               return get_real_filename_full_scan(conn, path, name, mangled,
+                                                  mem_ctx, found_name);
+       }
+
+       /* Try the vfs first to take advantage of case-insensitive stat. */
+       ret = SMB_VFS_GET_REAL_FILENAME(conn, path, name, mem_ctx, found_name);
+
+       /*
+        * If the case-insensitive stat was successful, or returned an error
+        * other than EOPNOTSUPP then there is no need to fall back on the
+        * full directory scan.
+        */
+       if (ret == 0 || (ret == -1 && errno != EOPNOTSUPP)) {
+               return ret;
+       }
+
+       return get_real_filename_full_scan(conn, path, name, mangled, mem_ctx,
+                                          found_name);
+}
+
+static NTSTATUS build_stream_path(TALLOC_CTX *mem_ctx,
+                                 connection_struct *conn,
+                                 const char *orig_path,
+                                 struct smb_filename *smb_fname)
+{
+       char *result = NULL;
+       NTSTATUS status;
+       unsigned int i, num_streams;
+       struct stream_struct *streams = NULL;
+
+       status = get_full_smb_filename(mem_ctx, smb_fname, &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       if (SMB_VFS_STAT(conn, result, &smb_fname->st) == 0) {
+               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;
+       }
+
+       /* Fall back to a case-insensitive scan of all streams on the file. */
+       status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, mem_ctx,
+                                   &num_streams, &streams);
+
+       if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
+               SET_STAT_INVALID(smb_fname->st);
+               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]: ",
+                          smb_fname->stream_name, streams[i].name));
+               if (fname_equal(smb_fname->stream_name, streams[i].name,
+                               conn->case_sensitive)) {
+                       DEBUGADD(10, ("equal\n"));
+                       break;
+               }
+               DEBUGADD(10, ("not equal\n"));
+       }
+
+       /* Couldn't find the stream. */
+       if (i == num_streams) {
+               SET_STAT_INVALID(smb_fname->st);
+               TALLOC_FREE(streams);
+               return NT_STATUS_OK;
+       }
+
+       DEBUG(10, ("case insensitive stream. requested: %s, actual: %s\n",
+               smb_fname->stream_name, streams[i].name));
+
+
+       TALLOC_FREE(smb_fname->stream_name);
+       smb_fname->stream_name = talloc_strdup(mem_ctx, streams[i].name);
+
+       TALLOC_FREE(result);
+       status = get_full_smb_filename(mem_ctx, smb_fname, &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
+       SET_STAT_INVALID(smb_fname->st);
+
+       if (SMB_VFS_STAT(conn, result, &smb_fname->st) == 0) {
+               stat_cache_add(orig_path, result, conn->case_sensitive);
+       }
+
+       TALLOC_FREE(streams);
+       return NT_STATUS_OK;
+
+ fail:
+       TALLOC_FREE(result);
+       TALLOC_FREE(streams);
+       return status;
 }