Re-Add the "dropbox" functionality with -wx rights on a directory
[Samba/bb.git] / source3 / smbd / filename.c
blobbcd8560ad9f40574ee1b186dea6167947f1f6ce7
1 /*
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.
27 #include "includes.h"
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,
39 const char *name2,
40 const struct share_params *p)
42 char mname[13];
44 if (!name_to_8_3(name2, mname, False, p)) {
45 return False;
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)
57 const char *p;
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;
77 } else {
78 /* Error code within a pathname. */
79 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
83 NTSTATUS get_full_smb_filename(TALLOC_CTX *ctx, const struct smb_filename *smb_fname,
84 char **full_name)
86 if (smb_fname->stream_name) {
87 *full_name = talloc_asprintf(ctx, "%s%s", smb_fname->base_name,
88 smb_fname->stream_name);
89 } else {
90 *full_name = talloc_strdup(ctx, smb_fname->base_name);
93 if (!*full_name) {
94 return NT_STATUS_NO_MEMORY;
97 return NT_STATUS_OK;
100 /****************************************************************************
101 This routine is called to convert names from the dos namespace to unix
102 namespace. It needs to handle any case conversions, mangling, format changes,
103 streams etc.
105 We assume that we have already done a chdir() to the right "root" directory
106 for this service.
108 The function will return an NTSTATUS error if some part of the name except for
109 the last part cannot be resolved, else NT_STATUS_OK.
111 Note NT_STATUS_OK doesn't mean the name exists or is valid, just that we
112 didn't get any fatal errors that should immediately terminate the calling SMB
113 processing whilst resolving.
115 If the UCF_SAVE_LCOMP flag is passed in, then the unmodified last component
116 of the pathname is set in smb_filename->original_lcomp.
118 If UCF_ALLOW_WCARD_LCOMP is passed in, then a MS wildcard was detected and
119 should be allowed in the last component of the path only.
121 If the orig_path was a stream, smb_filename->base_name will point to the base
122 filename, and smb_filename->stream_name will point to the stream name. If
123 orig_path was not a stream, then smb_filename->stream_name will be NULL.
125 On exit from unix_convert, the smb_filename->st stat struct will be populated
126 if the file exists and was found, if not this stat struct will be filled with
127 zeros (and this can be detected by checking for nlinks = 0, which can never be
128 true for any file).
129 ****************************************************************************/
131 NTSTATUS unix_convert(TALLOC_CTX *ctx,
132 connection_struct *conn,
133 const char *orig_path,
134 struct smb_filename **smb_fname_out,
135 uint32_t ucf_flags)
137 SMB_STRUCT_STAT st;
138 struct smb_filename *smb_fname = NULL;
139 char *start, *end;
140 char *dirpath = NULL;
141 char *name = NULL;
142 char *stream = NULL;
143 bool component_was_mangled = False;
144 bool name_has_wildcard = False;
145 bool posix_pathnames = false;
146 bool allow_wcard_last_component = ucf_flags & UCF_ALLOW_WCARD_LCOMP;
147 bool save_last_component = ucf_flags & UCF_SAVE_LCOMP;
148 NTSTATUS result;
149 int ret = -1;
151 *smb_fname_out = NULL;
153 smb_fname = TALLOC_ZERO_P(talloc_tos(), struct smb_filename);
154 if (smb_fname == NULL) {
155 return NT_STATUS_NO_MEMORY;
158 if (conn->printer) {
159 /* we don't ever use the filenames on a printer share as a
160 filename - so don't convert them */
161 if (!(smb_fname->base_name = talloc_strdup(smb_fname,
162 orig_path))) {
163 return NT_STATUS_NO_MEMORY;
165 *smb_fname_out = smb_fname;
166 return NT_STATUS_OK;
169 DEBUG(5, ("unix_convert called on file \"%s\"\n", orig_path));
172 * Conversion to basic unix format is already done in
173 * check_path_syntax().
177 * Names must be relative to the root of the service - any leading /.
178 * and trailing /'s should have been trimmed by check_path_syntax().
181 #ifdef DEVELOPER
182 SMB_ASSERT(*orig_path != '/');
183 #endif
186 * If we trimmed down to a single '\0' character
187 * then we should use the "." directory to avoid
188 * searching the cache, but not if we are in a
189 * printing share.
190 * As we know this is valid we can return true here.
193 if (!*orig_path) {
194 if (!(name = talloc_strdup(ctx,"."))) {
195 return NT_STATUS_NO_MEMORY;
197 if (SMB_VFS_STAT(conn,name,&st) == 0) {
198 smb_fname->st = st;
199 } else {
200 return map_nt_error_from_unix(errno);
202 DEBUG(5,("conversion finished \"\" -> %s\n",name));
203 goto done;
206 if (orig_path[0] == '.' && (orig_path[1] == '/' ||
207 orig_path[1] == '\0')) {
208 /* Start of pathname can't be "." only. */
209 if (orig_path[1] == '\0' || orig_path[2] == '\0') {
210 result = NT_STATUS_OBJECT_NAME_INVALID;
211 } else {
212 result =determine_path_error(
213 &orig_path[2], allow_wcard_last_component);
215 return result;
218 if (!(name = talloc_strdup(ctx, orig_path))) {
219 DEBUG(0, ("talloc_strdup failed\n"));
220 return NT_STATUS_NO_MEMORY;
224 * Large directory fix normalization. If we're case sensitive, and
225 * the case preserving parameters are set to "no", normalize the case of
226 * the incoming filename from the client WHETHER IT EXISTS OR NOT !
227 * This is in conflict with the current (3.0.20) man page, but is
228 * what people expect from the "large directory howto". I'll update
229 * the man page. Thanks to jht@samba.org for finding this. JRA.
232 if (conn->case_sensitive && !conn->case_preserve &&
233 !conn->short_case_preserve) {
234 strnorm(name, lp_defaultcase(SNUM(conn)));
238 * Ensure saved_last_component is valid even if file exists.
241 if(save_last_component) {
242 end = strrchr_m(name, '/');
243 if (end) {
244 smb_fname->original_lcomp = talloc_strdup(ctx,
245 end + 1);
246 } else {
247 smb_fname->original_lcomp = talloc_strdup(ctx, name);
251 posix_pathnames = lp_posix_pathnames();
253 /* Strip off the stream. Should we use any of the other stream parsing
254 * at this point? Also, should we set the is_stream bit? */
255 if (!posix_pathnames) {
256 stream = strchr_m(name, ':');
258 if (stream != NULL) {
259 char *tmp = talloc_strdup(ctx, stream);
260 if (tmp == NULL) {
261 TALLOC_FREE(name);
262 return NT_STATUS_NO_MEMORY;
264 *stream = '\0';
265 stream = tmp;
269 start = name;
271 /* If we're providing case insentive semantics or
272 * the underlying filesystem is case insensitive,
273 * then a case-normalized hit in the stat-cache is
274 * authoratitive. JRA.
277 if((!conn->case_sensitive || !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) &&
278 stat_cache_lookup(conn, &name, &dirpath, &start, &st)) {
279 smb_fname->st = st;
280 goto done;
284 * Make sure "dirpath" is an allocated string, we use this for
285 * building the directories with asprintf and free it.
288 if ((dirpath == NULL) && (!(dirpath = talloc_strdup(ctx,"")))) {
289 DEBUG(0, ("talloc_strdup failed\n"));
290 TALLOC_FREE(name);
291 return NT_STATUS_NO_MEMORY;
295 * stat the name - if it exists then we are all done!
298 if (posix_pathnames) {
299 ret = SMB_VFS_LSTAT(conn,name,&st);
300 } else {
301 ret = SMB_VFS_STAT(conn,name,&st);
304 if (ret == 0) {
305 /* Ensure we catch all names with in "/."
306 this is disallowed under Windows. */
307 const char *p = strstr(name, "/."); /* mb safe. */
308 if (p) {
309 if (p[2] == '/') {
310 /* Error code within a pathname. */
311 result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
312 goto fail;
313 } else if (p[2] == '\0') {
314 /* Error code at the end of a pathname. */
315 result = NT_STATUS_OBJECT_NAME_INVALID;
316 goto fail;
319 stat_cache_add(orig_path, name, conn->case_sensitive);
320 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
321 smb_fname->st = st;
322 goto done;
325 DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n",
326 name, dirpath, start));
329 * A special case - if we don't have any mangling chars and are case
330 * sensitive or the underlying filesystem is case insentive then searching
331 * won't help.
334 if ((conn->case_sensitive || !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) &&
335 !mangle_is_mangled(name, conn->params)) {
336 goto done;
340 * is_mangled() was changed to look at an entire pathname, not
341 * just a component. JRA.
344 if (mangle_is_mangled(start, conn->params)) {
345 component_was_mangled = True;
349 * Now we need to recursively match the name against the real
350 * directory structure.
354 * Match each part of the path name separately, trying the names
355 * as is first, then trying to scan the directory for matching names.
358 for (; start ; start = (end?end+1:(char *)NULL)) {
360 * Pinpoint the end of this section of the filename.
362 /* mb safe. '/' can't be in any encoded char. */
363 end = strchr(start, '/');
366 * Chop the name at this point.
368 if (end) {
369 *end = 0;
372 if (save_last_component) {
373 TALLOC_FREE(smb_fname->original_lcomp);
374 smb_fname->original_lcomp = talloc_strdup(ctx,
375 end ? end + 1 : start);
376 if (!smb_fname->original_lcomp) {
377 DEBUG(0, ("talloc failed\n"));
378 return NT_STATUS_NO_MEMORY;
382 /* The name cannot have a component of "." */
384 if (ISDOT(start)) {
385 if (!end) {
386 /* Error code at the end of a pathname. */
387 result = NT_STATUS_OBJECT_NAME_INVALID;
388 } else {
389 result = determine_path_error(end+1,
390 allow_wcard_last_component);
392 goto fail;
395 /* The name cannot have a wildcard if it's not
396 the last component. */
398 name_has_wildcard = ms_has_wild(start);
400 /* Wildcard not valid anywhere. */
401 if (name_has_wildcard && !allow_wcard_last_component) {
402 result = NT_STATUS_OBJECT_NAME_INVALID;
403 goto fail;
406 /* Wildcards never valid within a pathname. */
407 if (name_has_wildcard && end) {
408 result = NT_STATUS_OBJECT_NAME_INVALID;
409 goto fail;
413 * Check if the name exists up to this point.
416 if (posix_pathnames) {
417 ret = SMB_VFS_LSTAT(conn,name, &st);
418 } else {
419 ret = SMB_VFS_STAT(conn,name, &st);
422 if (ret == 0) {
424 * It exists. it must either be a directory or this must
425 * be the last part of the path for it to be OK.
427 if (end && !S_ISDIR(st.st_ex_mode)) {
429 * An intermediate part of the name isn't
430 * a directory.
432 DEBUG(5,("Not a dir %s\n",start));
433 *end = '/';
435 * We need to return the fact that the
436 * intermediate name resolution failed. This
437 * is used to return an error of ERRbadpath
438 * rather than ERRbadfile. Some Windows
439 * applications depend on the difference between
440 * these two errors.
442 result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
443 goto fail;
446 if (!end) {
448 * We just scanned for, and found the end of
449 * the path. We must return the valid stat
450 * struct. JRA.
453 smb_fname->st = st;
456 } else {
457 char *found_name = NULL;
459 /* Stat failed - ensure we don't use it. */
460 SET_STAT_INVALID(st);
463 * Reset errno so we can detect
464 * directory open errors.
466 errno = 0;
469 * Try to find this part of the path in the directory.
472 if (name_has_wildcard ||
473 (get_real_filename(conn, dirpath, start,
474 talloc_tos(),
475 &found_name) == -1)) {
476 char *unmangled;
478 if (end) {
480 * An intermediate part of the name
481 * can't be found.
483 DEBUG(5,("Intermediate not found %s\n",
484 start));
485 *end = '/';
488 * We need to return the fact that the
489 * intermediate name resolution failed.
490 * This is used to return an error of
491 * ERRbadpath rather than ERRbadfile.
492 * Some Windows applications depend on
493 * the difference between these two
494 * errors.
498 * ENOENT, ENOTDIR and ELOOP all map
499 * to NT_STATUS_OBJECT_PATH_NOT_FOUND
500 * in the filename walk.
503 if (errno == ENOENT ||
504 errno == ENOTDIR ||
505 errno == ELOOP) {
506 result =
507 NT_STATUS_OBJECT_PATH_NOT_FOUND;
509 else {
510 result =
511 map_nt_error_from_unix(errno);
513 goto fail;
516 /* ENOENT is the only valid error here. */
517 if ((errno != 0) && (errno != ENOENT)) {
519 * ENOTDIR and ELOOP both map to
520 * NT_STATUS_OBJECT_PATH_NOT_FOUND
521 * in the filename walk.
523 if (errno == ENOTDIR ||
524 errno == ELOOP) {
525 result =
526 NT_STATUS_OBJECT_PATH_NOT_FOUND;
527 goto fail;
528 } else if (errno != EACCES) {
529 result =
530 map_nt_error_from_unix(errno);
531 goto fail;
536 * Just the last part of the name doesn't exist.
537 * We need to strupper() or strlower() it as
538 * this conversion may be used for file creation
539 * purposes. Fix inspired by
540 * Thomas Neumann <t.neumann@iku-ag.de>.
542 if (!conn->case_preserve ||
543 (mangle_is_8_3(start, False,
544 conn->params) &&
545 !conn->short_case_preserve)) {
546 strnorm(start,
547 lp_defaultcase(SNUM(conn)));
551 * check on the mangled stack to see if we can
552 * recover the base of the filename.
555 if (mangle_is_mangled(start, conn->params)
556 && mangle_lookup_name_from_8_3(ctx,
557 start,
558 &unmangled,
559 conn->params)) {
560 char *tmp;
561 size_t start_ofs = start - name;
563 if (*dirpath != '\0') {
564 tmp = talloc_asprintf(ctx,
565 "%s/%s", dirpath,
566 unmangled);
567 TALLOC_FREE(unmangled);
569 else {
570 tmp = unmangled;
572 if (tmp == NULL) {
573 DEBUG(0, ("talloc failed\n"));
574 return NT_STATUS_NO_MEMORY;
576 TALLOC_FREE(name);
577 name = tmp;
578 start = name + start_ofs;
579 end = start + strlen(start);
582 DEBUG(5,("New file %s\n",start));
583 goto done;
588 * Restore the rest of the string. If the string was
589 * mangled the size may have changed.
591 if (end) {
592 char *tmp;
593 size_t start_ofs = start - name;
595 if (*dirpath != '\0') {
596 tmp = talloc_asprintf(ctx,
597 "%s/%s/%s", dirpath,
598 found_name, end+1);
600 else {
601 tmp = talloc_asprintf(ctx,
602 "%s/%s", found_name,
603 end+1);
605 if (tmp == NULL) {
606 DEBUG(0, ("talloc_asprintf failed\n"));
607 return NT_STATUS_NO_MEMORY;
609 TALLOC_FREE(name);
610 name = tmp;
611 start = name + start_ofs;
612 end = start + strlen(found_name);
613 *end = '\0';
614 } else {
615 char *tmp;
616 size_t start_ofs = start - name;
618 if (*dirpath != '\0') {
619 tmp = talloc_asprintf(ctx,
620 "%s/%s", dirpath,
621 found_name);
622 } else {
623 tmp = talloc_strdup(ctx,
624 found_name);
626 if (tmp == NULL) {
627 DEBUG(0, ("talloc failed\n"));
628 return NT_STATUS_NO_MEMORY;
630 TALLOC_FREE(name);
631 name = tmp;
632 start = name + start_ofs;
635 * We just scanned for, and found the end of
636 * the path. We must return a valid stat struct
637 * if it exists. JRA.
640 if (posix_pathnames) {
641 ret = SMB_VFS_LSTAT(conn,name, &st);
642 } else {
643 ret = SMB_VFS_STAT(conn,name, &st);
646 if (ret == 0) {
647 smb_fname->st = st;
648 } else {
649 SET_STAT_INVALID(st);
653 TALLOC_FREE(found_name);
654 } /* end else */
656 #ifdef DEVELOPER
658 * This sucks!
659 * We should never provide different behaviors
660 * depending on DEVELOPER!!!
662 if (VALID_STAT(st)) {
663 bool delete_pending;
664 get_file_infos(vfs_file_id_from_sbuf(conn, &st),
665 &delete_pending, NULL);
666 if (delete_pending) {
667 result = NT_STATUS_DELETE_PENDING;
668 goto fail;
671 #endif
674 * Add to the dirpath that we have resolved so far.
677 if (*dirpath != '\0') {
678 char *tmp = talloc_asprintf(ctx,
679 "%s/%s", dirpath, start);
680 if (!tmp) {
681 DEBUG(0, ("talloc_asprintf failed\n"));
682 return NT_STATUS_NO_MEMORY;
684 TALLOC_FREE(dirpath);
685 dirpath = tmp;
687 else {
688 TALLOC_FREE(dirpath);
689 if (!(dirpath = talloc_strdup(ctx,start))) {
690 DEBUG(0, ("talloc_strdup failed\n"));
691 return NT_STATUS_NO_MEMORY;
696 * Don't cache a name with mangled or wildcard components
697 * as this can change the size.
700 if(!component_was_mangled && !name_has_wildcard) {
701 stat_cache_add(orig_path, dirpath,
702 conn->case_sensitive);
706 * Restore the / that we wiped out earlier.
708 if (end) {
709 *end = '/';
714 * Don't cache a name with mangled or wildcard components
715 * as this can change the size.
718 if(!component_was_mangled && !name_has_wildcard) {
719 stat_cache_add(orig_path, name, conn->case_sensitive);
723 * The name has been resolved.
726 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
728 done:
729 smb_fname->base_name = name;
731 if (stream != NULL) {
732 smb_fname->stream_name = stream;
734 /* Check path now that the base_name has been converted. */
735 result = build_stream_path(ctx, conn, orig_path, smb_fname);
736 if (!NT_STATUS_IS_OK(result)) {
737 goto fail;
740 TALLOC_FREE(dirpath);
741 *smb_fname_out = smb_fname;
742 return NT_STATUS_OK;
743 fail:
744 DEBUG(10, ("dirpath = [%s] start = [%s]\n", dirpath, start));
745 if (*dirpath != '\0') {
746 smb_fname->base_name = talloc_asprintf(ctx, "%s/%s", dirpath,
747 start);
748 } else {
749 smb_fname->base_name = talloc_strdup(ctx, start);
751 if (!smb_fname->base_name) {
752 DEBUG(0, ("talloc_asprintf failed\n"));
753 return NT_STATUS_NO_MEMORY;
756 *smb_fname_out = smb_fname;
757 TALLOC_FREE(name);
758 TALLOC_FREE(dirpath);
759 return result;
762 /****************************************************************************
763 Check a filename - possibly calling check_reduced_name.
764 This is called by every routine before it allows an operation on a filename.
765 It does any final confirmation necessary to ensure that the filename is
766 a valid one for the user to access.
767 ****************************************************************************/
769 NTSTATUS check_name(connection_struct *conn, const char *name)
771 if (IS_VETO_PATH(conn, name)) {
772 /* Is it not dot or dot dot. */
773 if (!((name[0] == '.') && (!name[1] ||
774 (name[1] == '.' && !name[2])))) {
775 DEBUG(5,("check_name: file path name %s vetoed\n",
776 name));
777 return map_nt_error_from_unix(ENOENT);
781 if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
782 NTSTATUS status = check_reduced_name(conn,name);
783 if (!NT_STATUS_IS_OK(status)) {
784 DEBUG(5,("check_name: name %s failed with %s\n",name,
785 nt_errstr(status)));
786 return status;
790 return NT_STATUS_OK;
793 /****************************************************************************
794 Check if two filenames are equal.
795 This needs to be careful about whether we are case sensitive.
796 ****************************************************************************/
798 static bool fname_equal(const char *name1, const char *name2,
799 bool case_sensitive)
801 /* Normal filename handling */
802 if (case_sensitive) {
803 return(strcmp(name1,name2) == 0);
806 return(strequal(name1,name2));
809 /****************************************************************************
810 Scan a directory to find a filename, matching without case sensitivity.
811 If the name looks like a mangled name then try via the mangling functions
812 ****************************************************************************/
814 static int get_real_filename_full_scan(connection_struct *conn,
815 const char *path, const char *name,
816 bool mangled,
817 TALLOC_CTX *mem_ctx, char **found_name)
819 struct smb_Dir *cur_dir;
820 const char *dname;
821 char *unmangled_name = NULL;
822 long curpos;
824 /* handle null paths */
825 if ((path == NULL) || (*path == 0)) {
826 path = ".";
829 /* If we have a case-sensitive filesystem, it doesn't do us any
830 * good to search for a name. If a case variation of the name was
831 * there, then the original stat(2) would have found it.
833 if (!mangled && !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) {
834 errno = ENOENT;
835 return -1;
839 * The incoming name can be mangled, and if we de-mangle it
840 * here it will not compare correctly against the filename (name2)
841 * read from the directory and then mangled by the name_to_8_3()
842 * call. We need to mangle both names or neither.
843 * (JRA).
845 * Fix for bug found by Dina Fine. If in case sensitive mode then
846 * the mangle cache is no good (3 letter extension could be wrong
847 * case - so don't demangle in this case - leave as mangled and
848 * allow the mangling of the directory entry read (which is done
849 * case insensitively) to match instead. This will lead to more
850 * false positive matches but we fail completely without it. JRA.
853 if (mangled && !conn->case_sensitive) {
854 mangled = !mangle_lookup_name_from_8_3(talloc_tos(), name,
855 &unmangled_name,
856 conn->params);
857 if (!mangled) {
858 /* Name is now unmangled. */
859 name = unmangled_name;
863 /* open the directory */
864 if (!(cur_dir = OpenDir(talloc_tos(), conn, path, NULL, 0))) {
865 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
866 TALLOC_FREE(unmangled_name);
867 return -1;
870 /* now scan for matching names */
871 curpos = 0;
872 while ((dname = ReadDirName(cur_dir, &curpos, NULL))) {
874 /* Is it dot or dot dot. */
875 if (ISDOT(dname) || ISDOTDOT(dname)) {
876 continue;
880 * At this point dname is the unmangled name.
881 * name is either mangled or not, depending on the state
882 * of the "mangled" variable. JRA.
886 * Check mangled name against mangled name, or unmangled name
887 * against unmangled name.
890 if ((mangled && mangled_equal(name,dname,conn->params)) ||
891 fname_equal(name, dname, conn->case_sensitive)) {
892 /* we've found the file, change it's name and return */
893 *found_name = talloc_strdup(mem_ctx, dname);
894 TALLOC_FREE(unmangled_name);
895 TALLOC_FREE(cur_dir);
896 if (!*found_name) {
897 errno = ENOMEM;
898 return -1;
900 return 0;
904 TALLOC_FREE(unmangled_name);
905 TALLOC_FREE(cur_dir);
906 errno = ENOENT;
907 return -1;
910 /****************************************************************************
911 Wrapper around the vfs get_real_filename and the full directory scan
912 fallback.
913 ****************************************************************************/
915 int get_real_filename(connection_struct *conn, const char *path,
916 const char *name, TALLOC_CTX *mem_ctx,
917 char **found_name)
919 int ret;
920 bool mangled;
922 mangled = mangle_is_mangled(name, conn->params);
924 if (mangled) {
925 return get_real_filename_full_scan(conn, path, name, mangled,
926 mem_ctx, found_name);
929 /* Try the vfs first to take advantage of case-insensitive stat. */
930 ret = SMB_VFS_GET_REAL_FILENAME(conn, path, name, mem_ctx, found_name);
933 * If the case-insensitive stat was successful, or returned an error
934 * other than EOPNOTSUPP then there is no need to fall back on the
935 * full directory scan.
937 if (ret == 0 || (ret == -1 && errno != EOPNOTSUPP)) {
938 return ret;
941 return get_real_filename_full_scan(conn, path, name, mangled, mem_ctx,
942 found_name);
945 static NTSTATUS build_stream_path(TALLOC_CTX *mem_ctx,
946 connection_struct *conn,
947 const char *orig_path,
948 struct smb_filename *smb_fname)
950 char *result = NULL;
951 NTSTATUS status;
952 unsigned int i, num_streams;
953 struct stream_struct *streams = NULL;
955 status = get_full_smb_filename(mem_ctx, smb_fname, &result);
956 if (!NT_STATUS_IS_OK(status)) {
957 return NT_STATUS_NO_MEMORY;
960 if (SMB_VFS_STAT(conn, result, &smb_fname->st) == 0) {
961 return NT_STATUS_OK;
964 if (errno != ENOENT) {
965 status = map_nt_error_from_unix(errno);
966 DEBUG(10, ("vfs_stat failed: %s\n", nt_errstr(status)));
967 goto fail;
970 /* Fall back to a case-insensitive scan of all streams on the file. */
971 status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, mem_ctx,
972 &num_streams, &streams);
974 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
975 SET_STAT_INVALID(smb_fname->st);
976 return NT_STATUS_OK;
979 if (!NT_STATUS_IS_OK(status)) {
980 DEBUG(10, ("vfs_streaminfo failed: %s\n", nt_errstr(status)));
981 goto fail;
984 for (i=0; i<num_streams; i++) {
985 DEBUG(10, ("comparing [%s] and [%s]: ",
986 smb_fname->stream_name, streams[i].name));
987 if (fname_equal(smb_fname->stream_name, streams[i].name,
988 conn->case_sensitive)) {
989 DEBUGADD(10, ("equal\n"));
990 break;
992 DEBUGADD(10, ("not equal\n"));
995 /* Couldn't find the stream. */
996 if (i == num_streams) {
997 SET_STAT_INVALID(smb_fname->st);
998 TALLOC_FREE(streams);
999 return NT_STATUS_OK;
1002 DEBUG(10, ("case insensitive stream. requested: %s, actual: %s\n",
1003 smb_fname->stream_name, streams[i].name));
1006 TALLOC_FREE(smb_fname->stream_name);
1007 smb_fname->stream_name = talloc_strdup(mem_ctx, streams[i].name);
1009 TALLOC_FREE(result);
1010 status = get_full_smb_filename(mem_ctx, smb_fname, &result);
1011 if (!NT_STATUS_IS_OK(status)) {
1012 status = NT_STATUS_NO_MEMORY;
1013 goto fail;
1016 SET_STAT_INVALID(smb_fname->st);
1018 if (SMB_VFS_STAT(conn, result, &smb_fname->st) == 0) {
1019 stat_cache_add(orig_path, result, conn->case_sensitive);
1022 TALLOC_FREE(streams);
1023 return NT_STATUS_OK;
1025 fail:
1026 TALLOC_FREE(result);
1027 TALLOC_FREE(streams);
1028 return status;