s3-docs: Add missing para end tag.
[Samba.git] / source / smbd / filename.c
blob5ea5cb2af52e4443f415b53b109f6334db257b11
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;
501 * ENOENT/EACCESS are the only valid errors
502 * here. EACCESS needs handling here for
503 * "dropboxes", i.e. directories where users
504 * can only put stuff with permission -wx.
506 if ((errno != 0) && (errno != ENOENT)
507 && (errno != EACCES)) {
509 * ENOTDIR and ELOOP both map to
510 * NT_STATUS_OBJECT_PATH_NOT_FOUND
511 * in the filename walk.
513 if (errno == ENOTDIR ||
514 errno == ELOOP) {
515 result =
516 NT_STATUS_OBJECT_PATH_NOT_FOUND;
517 } else {
518 result =
519 map_nt_error_from_unix(errno);
521 goto fail;
525 * Just the last part of the name doesn't exist.
526 * We need to strupper() or strlower() it as
527 * this conversion may be used for file creation
528 * purposes. Fix inspired by
529 * Thomas Neumann <t.neumann@iku-ag.de>.
531 if (!conn->case_preserve ||
532 (mangle_is_8_3(start, False,
533 conn->params) &&
534 !conn->short_case_preserve)) {
535 strnorm(start,
536 lp_defaultcase(SNUM(conn)));
540 * check on the mangled stack to see if we can
541 * recover the base of the filename.
544 if (mangle_is_mangled(start, conn->params)
545 && mangle_lookup_name_from_8_3(ctx,
546 start,
547 &unmangled,
548 conn->params)) {
549 char *tmp;
550 size_t start_ofs = start - name;
552 if (*dirpath != '\0') {
553 tmp = talloc_asprintf(ctx,
554 "%s/%s", dirpath,
555 unmangled);
556 TALLOC_FREE(unmangled);
558 else {
559 tmp = unmangled;
561 if (tmp == NULL) {
562 DEBUG(0, ("talloc failed\n"));
563 return NT_STATUS_NO_MEMORY;
565 TALLOC_FREE(name);
566 name = tmp;
567 start = name + start_ofs;
568 end = start + strlen(start);
571 DEBUG(5,("New file %s\n",start));
572 goto done;
577 * Restore the rest of the string. If the string was
578 * mangled the size may have changed.
580 if (end) {
581 char *tmp;
582 size_t start_ofs = start - name;
584 if (*dirpath != '\0') {
585 tmp = talloc_asprintf(ctx,
586 "%s/%s/%s", dirpath,
587 found_name, end+1);
589 else {
590 tmp = talloc_asprintf(ctx,
591 "%s/%s", found_name,
592 end+1);
594 if (tmp == NULL) {
595 DEBUG(0, ("talloc_asprintf failed\n"));
596 return NT_STATUS_NO_MEMORY;
598 TALLOC_FREE(name);
599 name = tmp;
600 start = name + start_ofs;
601 end = start + strlen(found_name);
602 *end = '\0';
603 } else {
604 char *tmp;
605 size_t start_ofs = start - name;
607 if (*dirpath != '\0') {
608 tmp = talloc_asprintf(ctx,
609 "%s/%s", dirpath,
610 found_name);
611 } else {
612 tmp = talloc_strdup(ctx,
613 found_name);
615 if (tmp == NULL) {
616 DEBUG(0, ("talloc failed\n"));
617 return NT_STATUS_NO_MEMORY;
619 TALLOC_FREE(name);
620 name = tmp;
621 start = name + start_ofs;
624 * We just scanned for, and found the end of
625 * the path. We must return a valid stat struct
626 * if it exists. JRA.
629 if (posix_pathnames) {
630 ret = SMB_VFS_LSTAT(conn,name, &st);
631 } else {
632 ret = SMB_VFS_STAT(conn,name, &st);
635 if (ret == 0) {
636 *pst = st;
637 } else {
638 SET_STAT_INVALID(st);
642 TALLOC_FREE(found_name);
643 } /* end else */
645 #ifdef DEVELOPER
647 * This sucks!
648 * We should never provide different behaviors
649 * depending on DEVELOPER!!!
651 if (VALID_STAT(st)) {
652 bool delete_pending;
653 get_file_infos(vfs_file_id_from_sbuf(conn, &st),
654 &delete_pending, NULL);
655 if (delete_pending) {
656 result = NT_STATUS_DELETE_PENDING;
657 goto fail;
660 #endif
663 * Add to the dirpath that we have resolved so far.
666 if (*dirpath != '\0') {
667 char *tmp = talloc_asprintf(ctx,
668 "%s/%s", dirpath, start);
669 if (!tmp) {
670 DEBUG(0, ("talloc_asprintf failed\n"));
671 return NT_STATUS_NO_MEMORY;
673 TALLOC_FREE(dirpath);
674 dirpath = tmp;
676 else {
677 TALLOC_FREE(dirpath);
678 if (!(dirpath = talloc_strdup(ctx,start))) {
679 DEBUG(0, ("talloc_strdup failed\n"));
680 return NT_STATUS_NO_MEMORY;
685 * Don't cache a name with mangled or wildcard components
686 * as this can change the size.
689 if(!component_was_mangled && !name_has_wildcard) {
690 stat_cache_add(orig_path, dirpath,
691 conn->case_sensitive);
695 * Restore the / that we wiped out earlier.
697 if (end) {
698 *end = '/';
703 * Don't cache a name with mangled or wildcard components
704 * as this can change the size.
707 if(!component_was_mangled && !name_has_wildcard) {
708 stat_cache_add(orig_path, name, conn->case_sensitive);
712 * The name has been resolved.
715 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
717 done:
718 if (stream != NULL) {
719 char *tmp = NULL;
721 result = build_stream_path(ctx, conn, orig_path, name, stream,
722 pst, &tmp);
723 if (!NT_STATUS_IS_OK(result)) {
724 goto fail;
727 DEBUG(10, ("build_stream_path returned %s\n", tmp));
729 TALLOC_FREE(name);
730 name = tmp;
732 *pp_conv_path = name;
733 TALLOC_FREE(dirpath);
734 return NT_STATUS_OK;
735 fail:
736 DEBUG(10, ("dirpath = [%s] start = [%s]\n", dirpath, start));
737 if (*dirpath != '\0') {
738 *pp_conv_path = talloc_asprintf(ctx,
739 "%s/%s", dirpath, start);
740 } else {
741 *pp_conv_path = talloc_strdup(ctx, start);
743 if (!*pp_conv_path) {
744 DEBUG(0, ("talloc_asprintf failed\n"));
745 return NT_STATUS_NO_MEMORY;
747 TALLOC_FREE(name);
748 TALLOC_FREE(dirpath);
749 return result;
752 /****************************************************************************
753 Check a filename - possibly calling check_reduced_name.
754 This is called by every routine before it allows an operation on a filename.
755 It does any final confirmation necessary to ensure that the filename is
756 a valid one for the user to access.
757 ****************************************************************************/
759 NTSTATUS check_name(connection_struct *conn, const char *name)
761 if (IS_VETO_PATH(conn, name)) {
762 /* Is it not dot or dot dot. */
763 if (!((name[0] == '.') && (!name[1] ||
764 (name[1] == '.' && !name[2])))) {
765 DEBUG(5,("check_name: file path name %s vetoed\n",
766 name));
767 return map_nt_error_from_unix(ENOENT);
771 if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
772 NTSTATUS status = check_reduced_name(conn,name);
773 if (!NT_STATUS_IS_OK(status)) {
774 DEBUG(5,("check_name: name %s failed with %s\n",name,
775 nt_errstr(status)));
776 return status;
780 return NT_STATUS_OK;
783 /****************************************************************************
784 Check if two filenames are equal.
785 This needs to be careful about whether we are case sensitive.
786 ****************************************************************************/
788 static bool fname_equal(const char *name1, const char *name2,
789 bool case_sensitive)
791 /* Normal filename handling */
792 if (case_sensitive) {
793 return(strcmp(name1,name2) == 0);
796 return(strequal(name1,name2));
799 /****************************************************************************
800 Scan a directory to find a filename, matching without case sensitivity.
801 If the name looks like a mangled name then try via the mangling functions
802 ****************************************************************************/
804 static int get_real_filename_mangled(connection_struct *conn, const char *path,
805 const char *name, TALLOC_CTX *mem_ctx,
806 char **found_name)
808 bool mangled;
809 char *unmangled_name = NULL;
811 mangled = mangle_is_mangled(name, conn->params);
813 /* handle null paths */
814 if ((path == NULL) || (*path == 0)) {
815 path = ".";
818 /* If we have a case-sensitive filesystem, it doesn't do us any
819 * good to search for a name. If a case variation of the name was
820 * there, then the original stat(2) would have found it.
822 if (!mangled && !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) {
823 errno = ENOENT;
824 return -1;
828 * The incoming name can be mangled, and if we de-mangle it
829 * here it will not compare correctly against the filename (name2)
830 * read from the directory and then mangled by the name_to_8_3()
831 * call. We need to mangle both names or neither.
832 * (JRA).
834 * Fix for bug found by Dina Fine. If in case sensitive mode then
835 * the mangle cache is no good (3 letter extension could be wrong
836 * case - so don't demangle in this case - leave as mangled and
837 * allow the mangling of the directory entry read (which is done
838 * case insensitively) to match instead. This will lead to more
839 * false positive matches but we fail completely without it. JRA.
842 if (mangled && !conn->case_sensitive) {
843 mangled = !mangle_lookup_name_from_8_3(talloc_tos(), name,
844 &unmangled_name,
845 conn->params);
846 if (!mangled) {
847 /* Name is now unmangled. */
848 name = unmangled_name;
849 } else {
851 * If we have mangled names, do not ask the VFS'es
852 * GET_REAL_FILENAME. The Unix file system below does
853 * not know about Samba's style of mangling.
855 * Boolean flags passed down are evil, the alternative
856 * would be to pass a comparison function down into
857 * the loop in get_real_filename_internal(). For now,
858 * do the quick&dirty boolean flag approach.
860 return get_real_filename_internal(conn, path, name,
861 true,
862 mem_ctx, found_name);
866 return SMB_VFS_GET_REAL_FILENAME(conn, path, name, mem_ctx,
867 found_name);
870 static int get_real_filename_internal(connection_struct *conn,
871 const char *path, const char *name,
872 bool mangled,
873 TALLOC_CTX *mem_ctx, char **found_name)
875 struct smb_Dir *cur_dir;
876 const char *dname;
877 char *unmangled_name = NULL;
878 long curpos;
880 /* open the directory */
881 if (!(cur_dir = OpenDir(talloc_tos(), conn, path, NULL, 0))) {
882 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
883 TALLOC_FREE(unmangled_name);
884 return -1;
887 /* now scan for matching names */
888 curpos = 0;
889 while ((dname = ReadDirName(cur_dir, &curpos))) {
891 /* Is it dot or dot dot. */
892 if (ISDOT(dname) || ISDOTDOT(dname)) {
893 continue;
897 * At this point dname is the unmangled name.
898 * name is either mangled or not, depending on the state
899 * of the "mangled" variable. JRA.
903 * Check mangled name against mangled name, or unmangled name
904 * against unmangled name.
907 if ((mangled && mangled_equal(name,dname,conn->params)) ||
908 fname_equal(name, dname, conn->case_sensitive)) {
909 /* we've found the file, change it's name and return */
910 *found_name = talloc_strdup(mem_ctx, dname);
911 TALLOC_FREE(unmangled_name);
912 TALLOC_FREE(cur_dir);
913 if (!*found_name) {
914 errno = ENOMEM;
915 return -1;
917 return 0;
921 TALLOC_FREE(unmangled_name);
922 TALLOC_FREE(cur_dir);
923 errno = ENOENT;
924 return -1;
929 int get_real_filename(connection_struct *conn,
930 const char *path, const char *name,
931 TALLOC_CTX *mem_ctx, char **found_name)
934 * This is the default VFS function. If we end up here, we know we
935 * don't have mangled names around.
937 return get_real_filename_internal(conn, path, name, false,
938 mem_ctx, found_name);
941 static NTSTATUS build_stream_path(TALLOC_CTX *mem_ctx,
942 connection_struct *conn,
943 const char *orig_path,
944 const char *basepath,
945 const char *streamname,
946 SMB_STRUCT_STAT *pst,
947 char **path)
949 SMB_STRUCT_STAT st;
950 char *result = NULL;
951 NTSTATUS status;
952 unsigned int i, num_streams;
953 struct stream_struct *streams = NULL;
955 result = talloc_asprintf(mem_ctx, "%s%s", basepath, streamname);
956 if (result == NULL) {
957 return NT_STATUS_NO_MEMORY;
960 if (SMB_VFS_STAT(conn, result, &st) == 0) {
961 *pst = st;
962 *path = result;
963 return NT_STATUS_OK;
966 if (errno != ENOENT) {
967 status = map_nt_error_from_unix(errno);
968 DEBUG(10, ("vfs_stat failed: %s\n", nt_errstr(status)));
969 goto fail;
972 status = SMB_VFS_STREAMINFO(conn, NULL, basepath, mem_ctx,
973 &num_streams, &streams);
975 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
976 SET_STAT_INVALID(*pst);
977 *path = result;
978 return NT_STATUS_OK;
981 if (!NT_STATUS_IS_OK(status)) {
982 DEBUG(10, ("vfs_streaminfo failed: %s\n", nt_errstr(status)));
983 goto fail;
986 for (i=0; i<num_streams; i++) {
987 DEBUG(10, ("comparing [%s] and [%s]: ",
988 streamname, streams[i].name));
989 if (fname_equal(streamname, streams[i].name,
990 conn->case_sensitive)) {
991 DEBUGADD(10, ("equal\n"));
992 break;
994 DEBUGADD(10, ("not equal\n"));
997 if (i == num_streams) {
998 SET_STAT_INVALID(*pst);
999 *path = result;
1000 TALLOC_FREE(streams);
1001 return NT_STATUS_OK;
1004 TALLOC_FREE(result);
1006 result = talloc_asprintf(mem_ctx, "%s%s", basepath, streams[i].name);
1007 if (result == NULL) {
1008 status = NT_STATUS_NO_MEMORY;
1009 goto fail;
1012 SET_STAT_INVALID(*pst);
1014 if (SMB_VFS_STAT(conn, result, pst) == 0) {
1015 stat_cache_add(orig_path, result, conn->case_sensitive);
1018 *path = result;
1019 TALLOC_FREE(streams);
1020 return NT_STATUS_OK;
1022 fail:
1023 TALLOC_FREE(result);
1024 TALLOC_FREE(streams);
1025 return status;