Re-Add the "dropbox" functionality with -wx rights on a directory
[Samba/bb.git] / source / smbd / filename.c
blob7ff497f5cca5beadf69c426647c711f98ff7b5cc
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 const char *basepath,
33 const char *streamname,
34 SMB_STRUCT_STAT *pst,
35 char **path);
36 static int get_real_filename_mangled(connection_struct *conn, const char *path,
37 const char *name, TALLOC_CTX *mem_ctx,
38 char **found_name);
39 static int get_real_filename_internal(connection_struct *conn,
40 const char *path, const char *name,
41 bool mangled,
42 TALLOC_CTX *mem_ctx, char **found_name);
44 /****************************************************************************
45 Mangle the 2nd name and check if it is then equal to the first name.
46 ****************************************************************************/
48 static bool mangled_equal(const char *name1,
49 const char *name2,
50 const struct share_params *p)
52 char mname[13];
54 if (!name_to_8_3(name2, mname, False, p)) {
55 return False;
57 return strequal(name1, mname);
60 /****************************************************************************
61 Cope with the differing wildcard and non-wildcard error cases.
62 ****************************************************************************/
64 static NTSTATUS determine_path_error(const char *name,
65 bool allow_wcard_last_component)
67 const char *p;
69 if (!allow_wcard_last_component) {
70 /* Error code within a pathname. */
71 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
74 /* We're terminating here so we
75 * can be a little slower and get
76 * the error code right. Windows
77 * treats the last part of the pathname
78 * separately I think, so if the last
79 * component is a wildcard then we treat
80 * this ./ as "end of component" */
82 p = strchr(name, '/');
84 if (!p && (ms_has_wild(name) || ISDOT(name))) {
85 /* Error code at the end of a pathname. */
86 return NT_STATUS_OBJECT_NAME_INVALID;
87 } else {
88 /* Error code within a pathname. */
89 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
93 /****************************************************************************
94 This routine is called to convert names from the dos namespace to unix
95 namespace. It needs to handle any case conversions, mangling, format
96 changes etc.
98 We assume that we have already done a chdir() to the right "root" directory
99 for this service.
101 The function will return an NTSTATUS error if some part of the name except for
102 the last part cannot be resolved, else NT_STATUS_OK.
104 Note NT_STATUS_OK doesn't mean the name exists or is valid, just that we didn't
105 get any fatal errors that should immediately terminate the calling
106 SMB processing whilst resolving.
108 If the saved_last_component != 0, then the unmodified last component
109 of the pathname is returned there. If saved_last_component == 0 then nothing
110 is returned there.
112 If last_component_wcard is true then a MS wildcard was detected and
113 should be allowed in the last component of the path only.
115 On exit from unix_convert, if *pst was not null, then the file stat
116 struct will be returned if the file exists and was found, if not this
117 stat struct will be filled with zeros (and this can be detected by checking
118 for nlinks = 0, which can never be true for any file).
119 ****************************************************************************/
121 NTSTATUS unix_convert(TALLOC_CTX *ctx,
122 connection_struct *conn,
123 const char *orig_path,
124 bool allow_wcard_last_component,
125 char **pp_conv_path,
126 char **pp_saved_last_component,
127 SMB_STRUCT_STAT *pst)
129 SMB_STRUCT_STAT st;
130 char *start, *end;
131 char *dirpath = NULL;
132 char *name = NULL;
133 char *stream = NULL;
134 bool component_was_mangled = False;
135 bool name_has_wildcard = False;
136 bool posix_pathnames = false;
137 NTSTATUS result;
138 int ret = -1;
140 SET_STAT_INVALID(*pst);
141 *pp_conv_path = NULL;
142 if(pp_saved_last_component) {
143 *pp_saved_last_component = NULL;
146 if (conn->printer) {
147 /* we don't ever use the filenames on a printer share as a
148 filename - so don't convert them */
149 if (!(*pp_conv_path = talloc_strdup(ctx,orig_path))) {
150 return NT_STATUS_NO_MEMORY;
152 return NT_STATUS_OK;
155 DEBUG(5, ("unix_convert called on file \"%s\"\n", orig_path));
158 * Conversion to basic unix format is already done in
159 * check_path_syntax().
163 * Names must be relative to the root of the service - any leading /.
164 * and trailing /'s should have been trimmed by check_path_syntax().
167 #ifdef DEVELOPER
168 SMB_ASSERT(*orig_path != '/');
169 #endif
172 * If we trimmed down to a single '\0' character
173 * then we should use the "." directory to avoid
174 * searching the cache, but not if we are in a
175 * printing share.
176 * As we know this is valid we can return true here.
179 if (!*orig_path) {
180 if (!(name = talloc_strdup(ctx,"."))) {
181 return NT_STATUS_NO_MEMORY;
183 if (SMB_VFS_STAT(conn,name,&st) == 0) {
184 *pst = st;
185 } else {
186 return map_nt_error_from_unix(errno);
188 DEBUG(5,("conversion finished \"\" -> %s\n",name));
189 goto done;
192 if (orig_path[0] == '.' && (orig_path[1] == '/' ||
193 orig_path[1] == '\0')) {
194 /* Start of pathname can't be "." only. */
195 if (orig_path[1] == '\0' || orig_path[2] == '\0') {
196 result = NT_STATUS_OBJECT_NAME_INVALID;
197 } else {
198 result =determine_path_error(
199 &orig_path[2], allow_wcard_last_component);
201 return result;
204 if (!(name = talloc_strdup(ctx, orig_path))) {
205 DEBUG(0, ("talloc_strdup failed\n"));
206 return NT_STATUS_NO_MEMORY;
210 * Large directory fix normalization. If we're case sensitive, and
211 * the case preserving parameters are set to "no", normalize the case of
212 * the incoming filename from the client WHETHER IT EXISTS OR NOT !
213 * This is in conflict with the current (3.0.20) man page, but is
214 * what people expect from the "large directory howto". I'll update
215 * the man page. Thanks to jht@samba.org for finding this. JRA.
218 if (conn->case_sensitive && !conn->case_preserve &&
219 !conn->short_case_preserve) {
220 strnorm(name, lp_defaultcase(SNUM(conn)));
224 * Ensure saved_last_component is valid even if file exists.
227 if(pp_saved_last_component) {
228 end = strrchr_m(name, '/');
229 if (end) {
230 *pp_saved_last_component = talloc_strdup(ctx, end + 1);
231 } else {
232 *pp_saved_last_component = talloc_strdup(ctx,
233 name);
237 posix_pathnames = lp_posix_pathnames();
239 if (!posix_pathnames) {
240 stream = strchr_m(name, ':');
242 if (stream != NULL) {
243 char *tmp = talloc_strdup(ctx, stream);
244 if (tmp == NULL) {
245 TALLOC_FREE(name);
246 return NT_STATUS_NO_MEMORY;
248 *stream = '\0';
249 stream = tmp;
253 start = name;
255 /* If we're providing case insentive semantics or
256 * the underlying filesystem is case insensitive,
257 * then a case-normalized hit in the stat-cache is
258 * authoratitive. JRA.
261 if((!conn->case_sensitive || !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) &&
262 stat_cache_lookup(conn, &name, &dirpath, &start, &st)) {
263 *pst = st;
264 goto done;
268 * Make sure "dirpath" is an allocated string, we use this for
269 * building the directories with asprintf and free it.
272 if ((dirpath == NULL) && (!(dirpath = talloc_strdup(ctx,"")))) {
273 DEBUG(0, ("talloc_strdup failed\n"));
274 TALLOC_FREE(name);
275 return NT_STATUS_NO_MEMORY;
279 * stat the name - if it exists then we are all done!
282 if (posix_pathnames) {
283 ret = SMB_VFS_LSTAT(conn,name,&st);
284 } else {
285 ret = SMB_VFS_STAT(conn,name,&st);
288 if (ret == 0) {
289 /* Ensure we catch all names with in "/."
290 this is disallowed under Windows. */
291 const char *p = strstr(name, "/."); /* mb safe. */
292 if (p) {
293 if (p[2] == '/') {
294 /* Error code within a pathname. */
295 result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
296 goto fail;
297 } else if (p[2] == '\0') {
298 /* Error code at the end of a pathname. */
299 result = NT_STATUS_OBJECT_NAME_INVALID;
300 goto fail;
303 stat_cache_add(orig_path, name, conn->case_sensitive);
304 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
305 *pst = st;
306 goto done;
309 DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n",
310 name, dirpath, start));
313 * A special case - if we don't have any mangling chars and are case
314 * sensitive or the underlying filesystem is case insentive then searching
315 * won't help.
318 if ((conn->case_sensitive || !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) &&
319 !mangle_is_mangled(name, conn->params)) {
320 goto done;
324 * is_mangled() was changed to look at an entire pathname, not
325 * just a component. JRA.
328 if (mangle_is_mangled(start, conn->params)) {
329 component_was_mangled = True;
333 * Now we need to recursively match the name against the real
334 * directory structure.
338 * Match each part of the path name separately, trying the names
339 * as is first, then trying to scan the directory for matching names.
342 for (; start ; start = (end?end+1:(char *)NULL)) {
344 * Pinpoint the end of this section of the filename.
346 /* mb safe. '/' can't be in any encoded char. */
347 end = strchr(start, '/');
350 * Chop the name at this point.
352 if (end) {
353 *end = 0;
356 if (pp_saved_last_component) {
357 TALLOC_FREE(*pp_saved_last_component);
358 *pp_saved_last_component = talloc_strdup(ctx,
359 end ? end + 1 : start);
360 if (!*pp_saved_last_component) {
361 DEBUG(0, ("talloc failed\n"));
362 return NT_STATUS_NO_MEMORY;
366 /* The name cannot have a component of "." */
368 if (ISDOT(start)) {
369 if (!end) {
370 /* Error code at the end of a pathname. */
371 result = NT_STATUS_OBJECT_NAME_INVALID;
372 } else {
373 result = determine_path_error(end+1,
374 allow_wcard_last_component);
376 goto fail;
379 /* The name cannot have a wildcard if it's not
380 the last component. */
382 name_has_wildcard = ms_has_wild(start);
384 /* Wildcard not valid anywhere. */
385 if (name_has_wildcard && !allow_wcard_last_component) {
386 result = NT_STATUS_OBJECT_NAME_INVALID;
387 goto fail;
390 /* Wildcards never valid within a pathname. */
391 if (name_has_wildcard && end) {
392 result = NT_STATUS_OBJECT_NAME_INVALID;
393 goto fail;
397 * Check if the name exists up to this point.
400 if (posix_pathnames) {
401 ret = SMB_VFS_LSTAT(conn,name, &st);
402 } else {
403 ret = SMB_VFS_STAT(conn,name, &st);
406 if (ret == 0) {
408 * It exists. it must either be a directory or this must
409 * be the last part of the path for it to be OK.
411 if (end && !(st.st_mode & S_IFDIR)) {
413 * An intermediate part of the name isn't
414 * a directory.
416 DEBUG(5,("Not a dir %s\n",start));
417 *end = '/';
419 * We need to return the fact that the
420 * intermediate name resolution failed. This
421 * is used to return an error of ERRbadpath
422 * rather than ERRbadfile. Some Windows
423 * applications depend on the difference between
424 * these two errors.
426 result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
427 goto fail;
430 if (!end) {
432 * We just scanned for, and found the end of
433 * the path. We must return the valid stat
434 * struct. JRA.
437 *pst = st;
440 } else {
441 char *found_name = NULL;
443 /* Stat failed - ensure we don't use it. */
444 SET_STAT_INVALID(st);
447 * Reset errno so we can detect
448 * directory open errors.
450 errno = 0;
453 * Try to find this part of the path in the directory.
456 if (name_has_wildcard ||
457 (get_real_filename_mangled(
458 conn, dirpath, start,
459 talloc_tos(), &found_name) == -1)) {
460 char *unmangled;
462 if (end) {
464 * An intermediate part of the name
465 * can't be found.
467 DEBUG(5,("Intermediate not found %s\n",
468 start));
469 *end = '/';
472 * We need to return the fact that the
473 * intermediate name resolution failed.
474 * This is used to return an error of
475 * ERRbadpath rather than ERRbadfile.
476 * Some Windows applications depend on
477 * the difference between these two
478 * errors.
482 * ENOENT, ENOTDIR and ELOOP all map
483 * to NT_STATUS_OBJECT_PATH_NOT_FOUND
484 * in the filename walk.
487 if (errno == ENOENT ||
488 errno == ENOTDIR ||
489 errno == ELOOP) {
490 result =
491 NT_STATUS_OBJECT_PATH_NOT_FOUND;
493 else {
494 result =
495 map_nt_error_from_unix(errno);
497 goto fail;
500 /* ENOENT is the only valid error here. */
501 if ((errno != 0) && (errno != ENOENT)) {
503 * ENOTDIR and ELOOP both map to
504 * NT_STATUS_OBJECT_PATH_NOT_FOUND
505 * in the filename walk.
507 if (errno == ENOTDIR ||
508 errno == ELOOP) {
509 result =
510 NT_STATUS_OBJECT_PATH_NOT_FOUND;
511 goto fail;
512 } else if (errno != EACCES) {
513 result =
514 map_nt_error_from_unix(errno);
515 goto fail;
520 * Just the last part of the name doesn't exist.
521 * We need to strupper() or strlower() it as
522 * this conversion may be used for file creation
523 * purposes. Fix inspired by
524 * Thomas Neumann <t.neumann@iku-ag.de>.
526 if (!conn->case_preserve ||
527 (mangle_is_8_3(start, False,
528 conn->params) &&
529 !conn->short_case_preserve)) {
530 strnorm(start,
531 lp_defaultcase(SNUM(conn)));
535 * check on the mangled stack to see if we can
536 * recover the base of the filename.
539 if (mangle_is_mangled(start, conn->params)
540 && mangle_lookup_name_from_8_3(ctx,
541 start,
542 &unmangled,
543 conn->params)) {
544 char *tmp;
545 size_t start_ofs = start - name;
547 if (*dirpath != '\0') {
548 tmp = talloc_asprintf(ctx,
549 "%s/%s", dirpath,
550 unmangled);
551 TALLOC_FREE(unmangled);
553 else {
554 tmp = unmangled;
556 if (tmp == NULL) {
557 DEBUG(0, ("talloc failed\n"));
558 return NT_STATUS_NO_MEMORY;
560 TALLOC_FREE(name);
561 name = tmp;
562 start = name + start_ofs;
563 end = start + strlen(start);
566 DEBUG(5,("New file %s\n",start));
567 goto done;
572 * Restore the rest of the string. If the string was
573 * mangled the size may have changed.
575 if (end) {
576 char *tmp;
577 size_t start_ofs = start - name;
579 if (*dirpath != '\0') {
580 tmp = talloc_asprintf(ctx,
581 "%s/%s/%s", dirpath,
582 found_name, end+1);
584 else {
585 tmp = talloc_asprintf(ctx,
586 "%s/%s", found_name,
587 end+1);
589 if (tmp == NULL) {
590 DEBUG(0, ("talloc_asprintf failed\n"));
591 return NT_STATUS_NO_MEMORY;
593 TALLOC_FREE(name);
594 name = tmp;
595 start = name + start_ofs;
596 end = start + strlen(found_name);
597 *end = '\0';
598 } else {
599 char *tmp;
600 size_t start_ofs = start - name;
602 if (*dirpath != '\0') {
603 tmp = talloc_asprintf(ctx,
604 "%s/%s", dirpath,
605 found_name);
606 } else {
607 tmp = talloc_strdup(ctx,
608 found_name);
610 if (tmp == NULL) {
611 DEBUG(0, ("talloc failed\n"));
612 return NT_STATUS_NO_MEMORY;
614 TALLOC_FREE(name);
615 name = tmp;
616 start = name + start_ofs;
619 * We just scanned for, and found the end of
620 * the path. We must return a valid stat struct
621 * if it exists. JRA.
624 if (posix_pathnames) {
625 ret = SMB_VFS_LSTAT(conn,name, &st);
626 } else {
627 ret = SMB_VFS_STAT(conn,name, &st);
630 if (ret == 0) {
631 *pst = st;
632 } else {
633 SET_STAT_INVALID(st);
637 TALLOC_FREE(found_name);
638 } /* end else */
640 #ifdef DEVELOPER
642 * This sucks!
643 * We should never provide different behaviors
644 * depending on DEVELOPER!!!
646 if (VALID_STAT(st)) {
647 bool delete_pending;
648 get_file_infos(vfs_file_id_from_sbuf(conn, &st),
649 &delete_pending, NULL);
650 if (delete_pending) {
651 result = NT_STATUS_DELETE_PENDING;
652 goto fail;
655 #endif
658 * Add to the dirpath that we have resolved so far.
661 if (*dirpath != '\0') {
662 char *tmp = talloc_asprintf(ctx,
663 "%s/%s", dirpath, start);
664 if (!tmp) {
665 DEBUG(0, ("talloc_asprintf failed\n"));
666 return NT_STATUS_NO_MEMORY;
668 TALLOC_FREE(dirpath);
669 dirpath = tmp;
671 else {
672 TALLOC_FREE(dirpath);
673 if (!(dirpath = talloc_strdup(ctx,start))) {
674 DEBUG(0, ("talloc_strdup failed\n"));
675 return NT_STATUS_NO_MEMORY;
680 * Don't cache a name with mangled or wildcard components
681 * as this can change the size.
684 if(!component_was_mangled && !name_has_wildcard) {
685 stat_cache_add(orig_path, dirpath,
686 conn->case_sensitive);
690 * Restore the / that we wiped out earlier.
692 if (end) {
693 *end = '/';
698 * Don't cache a name with mangled or wildcard components
699 * as this can change the size.
702 if(!component_was_mangled && !name_has_wildcard) {
703 stat_cache_add(orig_path, name, conn->case_sensitive);
707 * The name has been resolved.
710 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
712 done:
713 if (stream != NULL) {
714 char *tmp = NULL;
716 result = build_stream_path(ctx, conn, orig_path, name, stream,
717 pst, &tmp);
718 if (!NT_STATUS_IS_OK(result)) {
719 goto fail;
722 DEBUG(10, ("build_stream_path returned %s\n", tmp));
724 TALLOC_FREE(name);
725 name = tmp;
727 *pp_conv_path = name;
728 TALLOC_FREE(dirpath);
729 return NT_STATUS_OK;
730 fail:
731 DEBUG(10, ("dirpath = [%s] start = [%s]\n", dirpath, start));
732 if (*dirpath != '\0') {
733 *pp_conv_path = talloc_asprintf(ctx,
734 "%s/%s", dirpath, start);
735 } else {
736 *pp_conv_path = talloc_strdup(ctx, start);
738 if (!*pp_conv_path) {
739 DEBUG(0, ("talloc_asprintf failed\n"));
740 return NT_STATUS_NO_MEMORY;
742 TALLOC_FREE(name);
743 TALLOC_FREE(dirpath);
744 return result;
747 /****************************************************************************
748 Check a filename - possibly calling check_reduced_name.
749 This is called by every routine before it allows an operation on a filename.
750 It does any final confirmation necessary to ensure that the filename is
751 a valid one for the user to access.
752 ****************************************************************************/
754 NTSTATUS check_name(connection_struct *conn, const char *name)
756 if (IS_VETO_PATH(conn, name)) {
757 /* Is it not dot or dot dot. */
758 if (!((name[0] == '.') && (!name[1] ||
759 (name[1] == '.' && !name[2])))) {
760 DEBUG(5,("check_name: file path name %s vetoed\n",
761 name));
762 return map_nt_error_from_unix(ENOENT);
766 if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
767 NTSTATUS status = check_reduced_name(conn,name);
768 if (!NT_STATUS_IS_OK(status)) {
769 DEBUG(5,("check_name: name %s failed with %s\n",name,
770 nt_errstr(status)));
771 return status;
775 return NT_STATUS_OK;
778 /****************************************************************************
779 Check if two filenames are equal.
780 This needs to be careful about whether we are case sensitive.
781 ****************************************************************************/
783 static bool fname_equal(const char *name1, const char *name2,
784 bool case_sensitive)
786 /* Normal filename handling */
787 if (case_sensitive) {
788 return(strcmp(name1,name2) == 0);
791 return(strequal(name1,name2));
794 /****************************************************************************
795 Scan a directory to find a filename, matching without case sensitivity.
796 If the name looks like a mangled name then try via the mangling functions
797 ****************************************************************************/
799 static int get_real_filename_mangled(connection_struct *conn, const char *path,
800 const char *name, TALLOC_CTX *mem_ctx,
801 char **found_name)
803 bool mangled;
804 char *unmangled_name = NULL;
806 mangled = mangle_is_mangled(name, conn->params);
808 /* handle null paths */
809 if ((path == NULL) || (*path == 0)) {
810 path = ".";
813 /* If we have a case-sensitive filesystem, it doesn't do us any
814 * good to search for a name. If a case variation of the name was
815 * there, then the original stat(2) would have found it.
817 if (!mangled && !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) {
818 errno = ENOENT;
819 return -1;
823 * The incoming name can be mangled, and if we de-mangle it
824 * here it will not compare correctly against the filename (name2)
825 * read from the directory and then mangled by the name_to_8_3()
826 * call. We need to mangle both names or neither.
827 * (JRA).
829 * Fix for bug found by Dina Fine. If in case sensitive mode then
830 * the mangle cache is no good (3 letter extension could be wrong
831 * case - so don't demangle in this case - leave as mangled and
832 * allow the mangling of the directory entry read (which is done
833 * case insensitively) to match instead. This will lead to more
834 * false positive matches but we fail completely without it. JRA.
837 if (mangled && !conn->case_sensitive) {
838 mangled = !mangle_lookup_name_from_8_3(talloc_tos(), name,
839 &unmangled_name,
840 conn->params);
841 if (!mangled) {
842 /* Name is now unmangled. */
843 name = unmangled_name;
844 } else {
846 * If we have mangled names, do not ask the VFS'es
847 * GET_REAL_FILENAME. The Unix file system below does
848 * not know about Samba's style of mangling.
850 * Boolean flags passed down are evil, the alternative
851 * would be to pass a comparison function down into
852 * the loop in get_real_filename_internal(). For now,
853 * do the quick&dirty boolean flag approach.
855 return get_real_filename_internal(conn, path, name,
856 true,
857 mem_ctx, found_name);
861 return SMB_VFS_GET_REAL_FILENAME(conn, path, name, mem_ctx,
862 found_name);
865 static int get_real_filename_internal(connection_struct *conn,
866 const char *path, const char *name,
867 bool mangled,
868 TALLOC_CTX *mem_ctx, char **found_name)
870 struct smb_Dir *cur_dir;
871 const char *dname;
872 char *unmangled_name = NULL;
873 long curpos;
875 /* open the directory */
876 if (!(cur_dir = OpenDir(talloc_tos(), conn, path, NULL, 0))) {
877 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
878 TALLOC_FREE(unmangled_name);
879 return -1;
882 /* now scan for matching names */
883 curpos = 0;
884 while ((dname = ReadDirName(cur_dir, &curpos))) {
886 /* Is it dot or dot dot. */
887 if (ISDOT(dname) || ISDOTDOT(dname)) {
888 continue;
892 * At this point dname is the unmangled name.
893 * name is either mangled or not, depending on the state
894 * of the "mangled" variable. JRA.
898 * Check mangled name against mangled name, or unmangled name
899 * against unmangled name.
902 if ((mangled && mangled_equal(name,dname,conn->params)) ||
903 fname_equal(name, dname, conn->case_sensitive)) {
904 /* we've found the file, change it's name and return */
905 *found_name = talloc_strdup(mem_ctx, dname);
906 TALLOC_FREE(unmangled_name);
907 TALLOC_FREE(cur_dir);
908 if (!*found_name) {
909 errno = ENOMEM;
910 return -1;
912 return 0;
916 TALLOC_FREE(unmangled_name);
917 TALLOC_FREE(cur_dir);
918 errno = ENOENT;
919 return -1;
924 int get_real_filename(connection_struct *conn,
925 const char *path, const char *name,
926 TALLOC_CTX *mem_ctx, char **found_name)
929 * This is the default VFS function. If we end up here, we know we
930 * don't have mangled names around.
932 return get_real_filename_internal(conn, path, name, false,
933 mem_ctx, found_name);
936 static NTSTATUS build_stream_path(TALLOC_CTX *mem_ctx,
937 connection_struct *conn,
938 const char *orig_path,
939 const char *basepath,
940 const char *streamname,
941 SMB_STRUCT_STAT *pst,
942 char **path)
944 SMB_STRUCT_STAT st;
945 char *result = NULL;
946 NTSTATUS status;
947 unsigned int i, num_streams;
948 struct stream_struct *streams = NULL;
950 result = talloc_asprintf(mem_ctx, "%s%s", basepath, streamname);
951 if (result == NULL) {
952 return NT_STATUS_NO_MEMORY;
955 if (SMB_VFS_STAT(conn, result, &st) == 0) {
956 *pst = st;
957 *path = result;
958 return NT_STATUS_OK;
961 if (errno != ENOENT) {
962 status = map_nt_error_from_unix(errno);
963 DEBUG(10, ("vfs_stat failed: %s\n", nt_errstr(status)));
964 goto fail;
967 status = SMB_VFS_STREAMINFO(conn, NULL, basepath, mem_ctx,
968 &num_streams, &streams);
970 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
971 SET_STAT_INVALID(*pst);
972 *path = result;
973 return NT_STATUS_OK;
976 if (!NT_STATUS_IS_OK(status)) {
977 DEBUG(10, ("vfs_streaminfo failed: %s\n", nt_errstr(status)));
978 goto fail;
981 for (i=0; i<num_streams; i++) {
982 DEBUG(10, ("comparing [%s] and [%s]: ",
983 streamname, streams[i].name));
984 if (fname_equal(streamname, streams[i].name,
985 conn->case_sensitive)) {
986 DEBUGADD(10, ("equal\n"));
987 break;
989 DEBUGADD(10, ("not equal\n"));
992 if (i == num_streams) {
993 SET_STAT_INVALID(*pst);
994 *path = result;
995 TALLOC_FREE(streams);
996 return NT_STATUS_OK;
999 TALLOC_FREE(result);
1001 result = talloc_asprintf(mem_ctx, "%s%s", basepath, streams[i].name);
1002 if (result == NULL) {
1003 status = NT_STATUS_NO_MEMORY;
1004 goto fail;
1007 SET_STAT_INVALID(*pst);
1009 if (SMB_VFS_STAT(conn, result, pst) == 0) {
1010 stat_cache_add(orig_path, result, conn->case_sensitive);
1013 *path = result;
1014 TALLOC_FREE(streams);
1015 return NT_STATUS_OK;
1017 fail:
1018 TALLOC_FREE(result);
1019 TALLOC_FREE(streams);
1020 return status;