2 Unix SMB/CIFS implementation.
3 filename handling routines
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 1999-2007
6 Copyright (C) Ying Chen 2000
7 Copyright (C) Volker Lendecke 2007
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * New hash table stat cache code added by Ying Chen.
29 static NTSTATUS build_stream_path(TALLOC_CTX *mem_ctx,
30 connection_struct *conn,
31 const char *orig_path,
32 struct smb_filename *smb_fname);
34 /****************************************************************************
35 Mangle the 2nd name and check if it is then equal to the first name.
36 ****************************************************************************/
38 static bool mangled_equal(const char *name1,
40 const struct share_params *p)
44 if (!name_to_8_3(name2, mname, False, p)) {
47 return strequal(name1, mname);
50 /****************************************************************************
51 Cope with the differing wildcard and non-wildcard error cases.
52 ****************************************************************************/
54 static NTSTATUS determine_path_error(const char *name,
55 bool allow_wcard_last_component)
59 if (!allow_wcard_last_component) {
60 /* Error code within a pathname. */
61 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
64 /* We're terminating here so we
65 * can be a little slower and get
66 * the error code right. Windows
67 * treats the last part of the pathname
68 * separately I think, so if the last
69 * component is a wildcard then we treat
70 * this ./ as "end of component" */
72 p = strchr(name, '/');
74 if (!p && (ms_has_wild(name) || ISDOT(name))) {
75 /* Error code at the end of a pathname. */
76 return NT_STATUS_OBJECT_NAME_INVALID;
78 /* Error code within a pathname. */
79 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
84 * XXX: This is temporary and there should be no callers of this outside of
85 * this file once smb_filename is plumbed through all path based operations.
86 * The one legitimate caller currently is smb_fname_str_dbg(), which this
87 * could be made static for.
89 NTSTATUS get_full_smb_filename(TALLOC_CTX *ctx, const struct smb_filename *smb_fname,
92 if (smb_fname->stream_name) {
93 *full_name = talloc_asprintf(ctx, "%s%s", smb_fname->base_name,
94 smb_fname->stream_name);
96 *full_name = talloc_strdup(ctx, smb_fname->base_name);
100 return NT_STATUS_NO_MEMORY;
107 * There are actually legitimate callers of this such as functions that
108 * enumerate streams using the SMB_VFS_STREAMINFO interface and then want to
109 * operate on each stream.
111 NTSTATUS create_synthetic_smb_fname(TALLOC_CTX *ctx, const char *base_name,
112 const char *stream_name,
113 const SMB_STRUCT_STAT *psbuf,
114 struct smb_filename **smb_fname_out)
116 struct smb_filename smb_fname_loc;
118 ZERO_STRUCT(smb_fname_loc);
120 /* Setup the base_name/stream_name. */
121 smb_fname_loc.base_name = CONST_DISCARD(char *, base_name);
122 smb_fname_loc.stream_name = CONST_DISCARD(char *, stream_name);
124 /* Copy the psbuf if one was given. */
126 smb_fname_loc.st = *psbuf;
128 /* Let copy_smb_filename() do the heavy lifting. */
129 return copy_smb_filename(ctx, &smb_fname_loc, smb_fname_out);
133 * XXX: This is temporary and there should be no callers of this once
134 * smb_filename is plumbed through all path based operations.
136 NTSTATUS create_synthetic_smb_fname_split(TALLOC_CTX *ctx,
138 const SMB_STRUCT_STAT *psbuf,
139 struct smb_filename **smb_fname_out)
142 const char *stream_name = NULL;
143 char *base_name = NULL;
145 if (!lp_posix_pathnames()) {
146 stream_name = strchr_m(fname, ':');
149 /* Setup the base_name/stream_name. */
151 base_name = talloc_strndup(ctx, fname,
152 PTR_DIFF(stream_name, fname));
154 base_name = talloc_strdup(ctx, fname);
158 return NT_STATUS_NO_MEMORY;
161 status = create_synthetic_smb_fname(ctx, base_name, stream_name, psbuf,
163 TALLOC_FREE(base_name);
168 * XXX: This is temporary and there should be no callers of this once
169 * smb_filename is plumbed through all path based operations.
171 int vfs_stat_smb_fname(struct connection_struct *conn, const char *fname,
172 SMB_STRUCT_STAT *psbuf)
174 struct smb_filename *smb_fname = NULL;
178 status = create_synthetic_smb_fname_split(talloc_tos(), fname, NULL,
180 if (!NT_STATUS_IS_OK(status)) {
181 errno = map_errno_from_nt_status(status);
185 ret = SMB_VFS_STAT(conn, smb_fname);
187 *psbuf = smb_fname->st;
190 TALLOC_FREE(smb_fname);
195 * XXX: This is temporary and there should be no callers of this once
196 * smb_filename is plumbed through all path based operations.
198 int vfs_lstat_smb_fname(struct connection_struct *conn, const char *fname,
199 SMB_STRUCT_STAT *psbuf)
201 struct smb_filename *smb_fname = NULL;
205 status = create_synthetic_smb_fname_split(talloc_tos(), fname, NULL,
207 if (!NT_STATUS_IS_OK(status)) {
208 errno = map_errno_from_nt_status(status);
212 ret = SMB_VFS_LSTAT(conn, smb_fname);
214 *psbuf = smb_fname->st;
217 TALLOC_FREE(smb_fname);
222 * Return a string using the debug_ctx()
224 const char *smb_fname_str_dbg(const struct smb_filename *smb_fname)
229 if (smb_fname == NULL) {
232 status = get_full_smb_filename(debug_ctx(), smb_fname, &fname);
233 if (!NT_STATUS_IS_OK(status)) {
239 NTSTATUS copy_smb_filename(TALLOC_CTX *ctx,
240 const struct smb_filename *smb_fname_in,
241 struct smb_filename **smb_fname_out)
244 *smb_fname_out = talloc_zero(ctx, struct smb_filename);
245 if (*smb_fname_out == NULL) {
246 return NT_STATUS_NO_MEMORY;
249 if (smb_fname_in->base_name) {
250 (*smb_fname_out)->base_name =
251 talloc_strdup(*smb_fname_out, smb_fname_in->base_name);
252 if (!(*smb_fname_out)->base_name)
256 if (smb_fname_in->stream_name) {
257 (*smb_fname_out)->stream_name =
258 talloc_strdup(*smb_fname_out, smb_fname_in->stream_name);
259 if (!(*smb_fname_out)->stream_name)
263 if (smb_fname_in->original_lcomp) {
264 (*smb_fname_out)->original_lcomp =
265 talloc_strdup(*smb_fname_out, smb_fname_in->original_lcomp);
266 if (!(*smb_fname_out)->original_lcomp)
270 (*smb_fname_out)->st = smb_fname_in->st;
274 TALLOC_FREE(*smb_fname_out);
275 return NT_STATUS_NO_MEMORY;
278 /****************************************************************************
279 This routine is called to convert names from the dos namespace to unix
280 namespace. It needs to handle any case conversions, mangling, format changes,
283 We assume that we have already done a chdir() to the right "root" directory
286 The function will return an NTSTATUS error if some part of the name except for
287 the last part cannot be resolved, else NT_STATUS_OK.
289 Note NT_STATUS_OK doesn't mean the name exists or is valid, just that we
290 didn't get any fatal errors that should immediately terminate the calling SMB
291 processing whilst resolving.
293 If the UCF_SAVE_LCOMP flag is passed in, then the unmodified last component
294 of the pathname is set in smb_filename->original_lcomp.
296 If UCF_ALLOW_WCARD_LCOMP is passed in, then a MS wildcard was detected and
297 should be allowed in the last component of the path only.
299 If the orig_path was a stream, smb_filename->base_name will point to the base
300 filename, and smb_filename->stream_name will point to the stream name. If
301 orig_path was not a stream, then smb_filename->stream_name will be NULL.
303 On exit from unix_convert, the smb_filename->st stat struct will be populated
304 if the file exists and was found, if not this stat struct will be filled with
305 zeros (and this can be detected by checking for nlinks = 0, which can never be
307 ****************************************************************************/
309 NTSTATUS unix_convert(TALLOC_CTX *ctx,
310 connection_struct *conn,
311 const char *orig_path,
312 struct smb_filename **smb_fname_out,
316 struct smb_filename *smb_fname = NULL;
318 char *dirpath = NULL;
321 bool component_was_mangled = False;
322 bool name_has_wildcard = False;
323 bool posix_pathnames = false;
324 bool allow_wcard_last_component = ucf_flags & UCF_ALLOW_WCARD_LCOMP;
325 bool save_last_component = ucf_flags & UCF_SAVE_LCOMP;
329 *smb_fname_out = NULL;
331 smb_fname = talloc_zero(talloc_tos(), struct smb_filename);
332 if (smb_fname == NULL) {
333 return NT_STATUS_NO_MEMORY;
337 /* we don't ever use the filenames on a printer share as a
338 filename - so don't convert them */
339 if (!(smb_fname->base_name = talloc_strdup(smb_fname,
341 return NT_STATUS_NO_MEMORY;
343 *smb_fname_out = smb_fname;
347 DEBUG(5, ("unix_convert called on file \"%s\"\n", orig_path));
350 * Conversion to basic unix format is already done in
351 * check_path_syntax().
355 * Names must be relative to the root of the service - any leading /.
356 * and trailing /'s should have been trimmed by check_path_syntax().
360 SMB_ASSERT(*orig_path != '/');
364 * If we trimmed down to a single '\0' character
365 * then we should use the "." directory to avoid
366 * searching the cache, but not if we are in a
368 * As we know this is valid we can return true here.
372 if (!(name = talloc_strdup(ctx,"."))) {
373 return NT_STATUS_NO_MEMORY;
375 if (vfs_stat_smb_fname(conn,name,&st) == 0) {
378 return map_nt_error_from_unix(errno);
380 DEBUG(5,("conversion finished \"\" -> %s\n",name));
384 if (orig_path[0] == '.' && (orig_path[1] == '/' ||
385 orig_path[1] == '\0')) {
386 /* Start of pathname can't be "." only. */
387 if (orig_path[1] == '\0' || orig_path[2] == '\0') {
388 result = NT_STATUS_OBJECT_NAME_INVALID;
390 result =determine_path_error(
391 &orig_path[2], allow_wcard_last_component);
396 if (!(name = talloc_strdup(ctx, orig_path))) {
397 DEBUG(0, ("talloc_strdup failed\n"));
398 return NT_STATUS_NO_MEMORY;
402 * Large directory fix normalization. If we're case sensitive, and
403 * the case preserving parameters are set to "no", normalize the case of
404 * the incoming filename from the client WHETHER IT EXISTS OR NOT !
405 * This is in conflict with the current (3.0.20) man page, but is
406 * what people expect from the "large directory howto". I'll update
407 * the man page. Thanks to jht@samba.org for finding this. JRA.
410 if (conn->case_sensitive && !conn->case_preserve &&
411 !conn->short_case_preserve) {
412 strnorm(name, lp_defaultcase(SNUM(conn)));
416 * Ensure saved_last_component is valid even if file exists.
419 if(save_last_component) {
420 end = strrchr_m(name, '/');
422 smb_fname->original_lcomp = talloc_strdup(ctx,
425 smb_fname->original_lcomp = talloc_strdup(ctx, name);
429 posix_pathnames = lp_posix_pathnames();
431 /* Strip off the stream. Should we use any of the other stream parsing
432 * at this point? Also, should we set the is_stream bit? */
433 if (!posix_pathnames) {
434 stream = strchr_m(name, ':');
436 if (stream != NULL) {
437 char *tmp = talloc_strdup(ctx, stream);
440 return NT_STATUS_NO_MEMORY;
449 /* If we're providing case insentive semantics or
450 * the underlying filesystem is case insensitive,
451 * then a case-normalized hit in the stat-cache is
452 * authoratitive. JRA.
455 if((!conn->case_sensitive || !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) &&
456 stat_cache_lookup(conn, &name, &dirpath, &start, &st)) {
462 * Make sure "dirpath" is an allocated string, we use this for
463 * building the directories with asprintf and free it.
466 if ((dirpath == NULL) && (!(dirpath = talloc_strdup(ctx,"")))) {
467 DEBUG(0, ("talloc_strdup failed\n"));
469 return NT_STATUS_NO_MEMORY;
473 * stat the name - if it exists then we are all done!
476 if (posix_pathnames) {
477 ret = vfs_lstat_smb_fname(conn,name,&st);
479 ret = vfs_stat_smb_fname(conn,name,&st);
483 /* Ensure we catch all names with in "/."
484 this is disallowed under Windows. */
485 const char *p = strstr(name, "/."); /* mb safe. */
488 /* Error code within a pathname. */
489 result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
491 } else if (p[2] == '\0') {
492 /* Error code at the end of a pathname. */
493 result = NT_STATUS_OBJECT_NAME_INVALID;
497 stat_cache_add(orig_path, name, conn->case_sensitive);
498 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
503 DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n",
504 name, dirpath, start));
507 * A special case - if we don't have any mangling chars and are case
508 * sensitive or the underlying filesystem is case insentive then searching
512 if ((conn->case_sensitive || !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) &&
513 !mangle_is_mangled(name, conn->params)) {
518 * is_mangled() was changed to look at an entire pathname, not
519 * just a component. JRA.
522 if (mangle_is_mangled(start, conn->params)) {
523 component_was_mangled = True;
527 * Now we need to recursively match the name against the real
528 * directory structure.
532 * Match each part of the path name separately, trying the names
533 * as is first, then trying to scan the directory for matching names.
536 for (; start ; start = (end?end+1:(char *)NULL)) {
538 * Pinpoint the end of this section of the filename.
540 /* mb safe. '/' can't be in any encoded char. */
541 end = strchr(start, '/');
544 * Chop the name at this point.
550 if (save_last_component) {
551 TALLOC_FREE(smb_fname->original_lcomp);
552 smb_fname->original_lcomp = talloc_strdup(ctx,
553 end ? end + 1 : start);
554 if (!smb_fname->original_lcomp) {
555 DEBUG(0, ("talloc failed\n"));
556 return NT_STATUS_NO_MEMORY;
560 /* The name cannot have a component of "." */
564 /* Error code at the end of a pathname. */
565 result = NT_STATUS_OBJECT_NAME_INVALID;
567 result = determine_path_error(end+1,
568 allow_wcard_last_component);
573 /* The name cannot have a wildcard if it's not
574 the last component. */
576 name_has_wildcard = ms_has_wild(start);
578 /* Wildcard not valid anywhere. */
579 if (name_has_wildcard && !allow_wcard_last_component) {
580 result = NT_STATUS_OBJECT_NAME_INVALID;
584 /* Wildcards never valid within a pathname. */
585 if (name_has_wildcard && end) {
586 result = NT_STATUS_OBJECT_NAME_INVALID;
591 * Check if the name exists up to this point.
594 if (posix_pathnames) {
595 ret = vfs_lstat_smb_fname(conn,name, &st);
597 ret = vfs_stat_smb_fname(conn,name, &st);
602 * It exists. it must either be a directory or this must
603 * be the last part of the path for it to be OK.
605 if (end && !S_ISDIR(st.st_ex_mode)) {
607 * An intermediate part of the name isn't
610 DEBUG(5,("Not a dir %s\n",start));
613 * We need to return the fact that the
614 * intermediate name resolution failed. This
615 * is used to return an error of ERRbadpath
616 * rather than ERRbadfile. Some Windows
617 * applications depend on the difference between
620 result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
626 * We just scanned for, and found the end of
627 * the path. We must return the valid stat
635 char *found_name = NULL;
637 /* Stat failed - ensure we don't use it. */
638 SET_STAT_INVALID(st);
641 * Reset errno so we can detect
642 * directory open errors.
647 * Try to find this part of the path in the directory.
650 if (name_has_wildcard ||
651 (get_real_filename(conn, dirpath, start,
653 &found_name) == -1)) {
658 * An intermediate part of the name
661 DEBUG(5,("Intermediate not found %s\n",
666 * We need to return the fact that the
667 * intermediate name resolution failed.
668 * This is used to return an error of
669 * ERRbadpath rather than ERRbadfile.
670 * Some Windows applications depend on
671 * the difference between these two
676 * ENOENT, ENOTDIR and ELOOP all map
677 * to NT_STATUS_OBJECT_PATH_NOT_FOUND
678 * in the filename walk.
681 if (errno == ENOENT ||
685 NT_STATUS_OBJECT_PATH_NOT_FOUND;
689 map_nt_error_from_unix(errno);
695 * ENOENT/EACCESS are the only valid errors
696 * here. EACCESS needs handling here for
697 * "dropboxes", i.e. directories where users
698 * can only put stuff with permission -wx.
700 if ((errno != 0) && (errno != ENOENT)
701 && (errno != EACCES)) {
703 * ENOTDIR and ELOOP both map to
704 * NT_STATUS_OBJECT_PATH_NOT_FOUND
705 * in the filename walk.
707 if (errno == ENOTDIR ||
710 NT_STATUS_OBJECT_PATH_NOT_FOUND;
713 map_nt_error_from_unix(errno);
719 * Just the last part of the name doesn't exist.
720 * We need to strupper() or strlower() it as
721 * this conversion may be used for file creation
722 * purposes. Fix inspired by
723 * Thomas Neumann <t.neumann@iku-ag.de>.
725 if (!conn->case_preserve ||
726 (mangle_is_8_3(start, False,
728 !conn->short_case_preserve)) {
730 lp_defaultcase(SNUM(conn)));
734 * check on the mangled stack to see if we can
735 * recover the base of the filename.
738 if (mangle_is_mangled(start, conn->params)
739 && mangle_lookup_name_from_8_3(ctx,
744 size_t start_ofs = start - name;
746 if (*dirpath != '\0') {
747 tmp = talloc_asprintf(ctx,
750 TALLOC_FREE(unmangled);
756 DEBUG(0, ("talloc failed\n"));
757 return NT_STATUS_NO_MEMORY;
761 start = name + start_ofs;
762 end = start + strlen(start);
765 DEBUG(5,("New file %s\n",start));
771 * Restore the rest of the string. If the string was
772 * mangled the size may have changed.
776 size_t start_ofs = start - name;
778 if (*dirpath != '\0') {
779 tmp = talloc_asprintf(ctx,
784 tmp = talloc_asprintf(ctx,
789 DEBUG(0, ("talloc_asprintf failed\n"));
790 return NT_STATUS_NO_MEMORY;
794 start = name + start_ofs;
795 end = start + strlen(found_name);
799 size_t start_ofs = start - name;
801 if (*dirpath != '\0') {
802 tmp = talloc_asprintf(ctx,
806 tmp = talloc_strdup(ctx,
810 DEBUG(0, ("talloc failed\n"));
811 return NT_STATUS_NO_MEMORY;
815 start = name + start_ofs;
818 * We just scanned for, and found the end of
819 * the path. We must return a valid stat struct
823 if (posix_pathnames) {
824 ret = vfs_lstat_smb_fname(conn,name,
827 ret = vfs_stat_smb_fname(conn,name,
834 SET_STAT_INVALID(st);
838 TALLOC_FREE(found_name);
844 * We should never provide different behaviors
845 * depending on DEVELOPER!!!
847 if (VALID_STAT(st)) {
849 get_file_infos(vfs_file_id_from_sbuf(conn, &st),
850 &delete_pending, NULL);
851 if (delete_pending) {
852 result = NT_STATUS_DELETE_PENDING;
859 * Add to the dirpath that we have resolved so far.
862 if (*dirpath != '\0') {
863 char *tmp = talloc_asprintf(ctx,
864 "%s/%s", dirpath, start);
866 DEBUG(0, ("talloc_asprintf failed\n"));
867 return NT_STATUS_NO_MEMORY;
869 TALLOC_FREE(dirpath);
873 TALLOC_FREE(dirpath);
874 if (!(dirpath = talloc_strdup(ctx,start))) {
875 DEBUG(0, ("talloc_strdup failed\n"));
876 return NT_STATUS_NO_MEMORY;
881 * Don't cache a name with mangled or wildcard components
882 * as this can change the size.
885 if(!component_was_mangled && !name_has_wildcard) {
886 stat_cache_add(orig_path, dirpath,
887 conn->case_sensitive);
891 * Restore the / that we wiped out earlier.
899 * Don't cache a name with mangled or wildcard components
900 * as this can change the size.
903 if(!component_was_mangled && !name_has_wildcard) {
904 stat_cache_add(orig_path, name, conn->case_sensitive);
908 * The name has been resolved.
911 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
914 smb_fname->base_name = name;
916 if (stream != NULL) {
917 smb_fname->stream_name = stream;
919 /* Check path now that the base_name has been converted. */
920 result = build_stream_path(ctx, conn, orig_path, smb_fname);
921 if (!NT_STATUS_IS_OK(result)) {
925 TALLOC_FREE(dirpath);
926 *smb_fname_out = smb_fname;
929 DEBUG(10, ("dirpath = [%s] start = [%s]\n", dirpath, start));
930 if (*dirpath != '\0') {
931 smb_fname->base_name = talloc_asprintf(ctx, "%s/%s", dirpath,
934 smb_fname->base_name = talloc_strdup(ctx, start);
936 if (!smb_fname->base_name) {
937 DEBUG(0, ("talloc_asprintf failed\n"));
938 return NT_STATUS_NO_MEMORY;
941 *smb_fname_out = smb_fname;
943 TALLOC_FREE(dirpath);
947 /****************************************************************************
948 Check a filename - possibly calling check_reduced_name.
949 This is called by every routine before it allows an operation on a filename.
950 It does any final confirmation necessary to ensure that the filename is
951 a valid one for the user to access.
952 ****************************************************************************/
954 NTSTATUS check_name(connection_struct *conn, const char *name)
956 if (IS_VETO_PATH(conn, name)) {
957 /* Is it not dot or dot dot. */
958 if (!((name[0] == '.') && (!name[1] ||
959 (name[1] == '.' && !name[2])))) {
960 DEBUG(5,("check_name: file path name %s vetoed\n",
962 return map_nt_error_from_unix(ENOENT);
966 if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
967 NTSTATUS status = check_reduced_name(conn,name);
968 if (!NT_STATUS_IS_OK(status)) {
969 DEBUG(5,("check_name: name %s failed with %s\n",name,
978 /****************************************************************************
979 Check if two filenames are equal.
980 This needs to be careful about whether we are case sensitive.
981 ****************************************************************************/
983 static bool fname_equal(const char *name1, const char *name2,
986 /* Normal filename handling */
987 if (case_sensitive) {
988 return(strcmp(name1,name2) == 0);
991 return(strequal(name1,name2));
994 /****************************************************************************
995 Scan a directory to find a filename, matching without case sensitivity.
996 If the name looks like a mangled name then try via the mangling functions
997 ****************************************************************************/
999 static int get_real_filename_full_scan(connection_struct *conn,
1000 const char *path, const char *name,
1002 TALLOC_CTX *mem_ctx, char **found_name)
1004 struct smb_Dir *cur_dir;
1006 char *unmangled_name = NULL;
1009 /* handle null paths */
1010 if ((path == NULL) || (*path == 0)) {
1014 /* If we have a case-sensitive filesystem, it doesn't do us any
1015 * good to search for a name. If a case variation of the name was
1016 * there, then the original stat(2) would have found it.
1018 if (!mangled && !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) {
1024 * The incoming name can be mangled, and if we de-mangle it
1025 * here it will not compare correctly against the filename (name2)
1026 * read from the directory and then mangled by the name_to_8_3()
1027 * call. We need to mangle both names or neither.
1030 * Fix for bug found by Dina Fine. If in case sensitive mode then
1031 * the mangle cache is no good (3 letter extension could be wrong
1032 * case - so don't demangle in this case - leave as mangled and
1033 * allow the mangling of the directory entry read (which is done
1034 * case insensitively) to match instead. This will lead to more
1035 * false positive matches but we fail completely without it. JRA.
1038 if (mangled && !conn->case_sensitive) {
1039 mangled = !mangle_lookup_name_from_8_3(talloc_tos(), name,
1043 /* Name is now unmangled. */
1044 name = unmangled_name;
1048 /* open the directory */
1049 if (!(cur_dir = OpenDir(talloc_tos(), conn, path, NULL, 0))) {
1050 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
1051 TALLOC_FREE(unmangled_name);
1055 /* now scan for matching names */
1057 while ((dname = ReadDirName(cur_dir, &curpos, NULL))) {
1059 /* Is it dot or dot dot. */
1060 if (ISDOT(dname) || ISDOTDOT(dname)) {
1065 * At this point dname is the unmangled name.
1066 * name is either mangled or not, depending on the state
1067 * of the "mangled" variable. JRA.
1071 * Check mangled name against mangled name, or unmangled name
1072 * against unmangled name.
1075 if ((mangled && mangled_equal(name,dname,conn->params)) ||
1076 fname_equal(name, dname, conn->case_sensitive)) {
1077 /* we've found the file, change it's name and return */
1078 *found_name = talloc_strdup(mem_ctx, dname);
1079 TALLOC_FREE(unmangled_name);
1080 TALLOC_FREE(cur_dir);
1089 TALLOC_FREE(unmangled_name);
1090 TALLOC_FREE(cur_dir);
1095 /****************************************************************************
1096 Wrapper around the vfs get_real_filename and the full directory scan
1098 ****************************************************************************/
1100 int get_real_filename(connection_struct *conn, const char *path,
1101 const char *name, TALLOC_CTX *mem_ctx,
1107 mangled = mangle_is_mangled(name, conn->params);
1110 return get_real_filename_full_scan(conn, path, name, mangled,
1111 mem_ctx, found_name);
1114 /* Try the vfs first to take advantage of case-insensitive stat. */
1115 ret = SMB_VFS_GET_REAL_FILENAME(conn, path, name, mem_ctx, found_name);
1118 * If the case-insensitive stat was successful, or returned an error
1119 * other than EOPNOTSUPP then there is no need to fall back on the
1120 * full directory scan.
1122 if (ret == 0 || (ret == -1 && errno != EOPNOTSUPP)) {
1126 return get_real_filename_full_scan(conn, path, name, mangled, mem_ctx,
1130 static NTSTATUS build_stream_path(TALLOC_CTX *mem_ctx,
1131 connection_struct *conn,
1132 const char *orig_path,
1133 struct smb_filename *smb_fname)
1136 unsigned int i, num_streams;
1137 struct stream_struct *streams = NULL;
1139 if (SMB_VFS_STAT(conn, smb_fname) == 0) {
1140 return NT_STATUS_OK;
1143 if (errno != ENOENT) {
1144 status = map_nt_error_from_unix(errno);
1145 DEBUG(10, ("vfs_stat failed: %s\n", nt_errstr(status)));
1149 /* Fall back to a case-insensitive scan of all streams on the file. */
1150 status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, mem_ctx,
1151 &num_streams, &streams);
1153 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1154 SET_STAT_INVALID(smb_fname->st);
1155 return NT_STATUS_OK;
1158 if (!NT_STATUS_IS_OK(status)) {
1159 DEBUG(10, ("vfs_streaminfo failed: %s\n", nt_errstr(status)));
1163 for (i=0; i<num_streams; i++) {
1164 DEBUG(10, ("comparing [%s] and [%s]: ",
1165 smb_fname->stream_name, streams[i].name));
1166 if (fname_equal(smb_fname->stream_name, streams[i].name,
1167 conn->case_sensitive)) {
1168 DEBUGADD(10, ("equal\n"));
1171 DEBUGADD(10, ("not equal\n"));
1174 /* Couldn't find the stream. */
1175 if (i == num_streams) {
1176 SET_STAT_INVALID(smb_fname->st);
1177 TALLOC_FREE(streams);
1178 return NT_STATUS_OK;
1181 DEBUG(10, ("case insensitive stream. requested: %s, actual: %s\n",
1182 smb_fname->stream_name, streams[i].name));
1185 TALLOC_FREE(smb_fname->stream_name);
1186 smb_fname->stream_name = talloc_strdup(mem_ctx, streams[i].name);
1188 SET_STAT_INVALID(smb_fname->st);
1190 if (SMB_VFS_STAT(conn, smb_fname) == 0) {
1191 char *result = NULL;
1193 status = get_full_smb_filename(mem_ctx, smb_fname, &result);
1194 if (!NT_STATUS_IS_OK(status)) {
1195 status = NT_STATUS_NO_MEMORY;
1199 stat_cache_add(orig_path, result, conn->case_sensitive);
1200 TALLOC_FREE(result);
1202 status = NT_STATUS_OK;
1204 TALLOC_FREE(streams);
1208 /****************************************************************************
1209 Go through all the steps to validate a filename.
1210 ****************************************************************************/
1212 NTSTATUS filename_convert(TALLOC_CTX *ctx,
1213 connection_struct *conn,
1215 const char *name_in,
1216 struct smb_filename **pp_smb_fname,
1221 *pp_smb_fname = NULL;
1224 status = resolve_dfspath(ctx, conn,
1228 if (!NT_STATUS_IS_OK(status)) {
1229 DEBUG(10,("filename_convert: resolve_dfspath failed "
1230 "for name %s with %s\n",
1232 nt_errstr(status) ));
1235 status = unix_convert(ctx, conn, *pp_name, pp_smb_fname, 0);
1236 if (!NT_STATUS_IS_OK(status)) {
1237 DEBUG(10,("filename_convert: unix_convert failed "
1238 "for name %s with %s\n",
1240 nt_errstr(status) ));
1244 status = get_full_smb_filename(ctx, *pp_smb_fname, pp_name);
1245 if (!NT_STATUS_IS_OK(status)) {
1249 status = check_name(conn, *pp_name);
1250 if (!NT_STATUS_IS_OK(status)) {
1251 DEBUG(3,("filename_convert: check_name failed "
1252 "for name %s with %s\n",
1254 nt_errstr(status) ));