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,
33 const char *streamname,
36 static int get_real_filename_mangled(connection_struct *conn, const char *path,
37 const char *name, TALLOC_CTX *mem_ctx,
40 /****************************************************************************
41 Mangle the 2nd name and check if it is then equal to the first name.
42 ****************************************************************************/
44 static bool mangled_equal(const char *name1,
46 const struct share_params *p)
50 if (!name_to_8_3(name2, mname, False, p)) {
53 return strequal(name1, mname);
56 /****************************************************************************
57 Cope with the differing wildcard and non-wildcard error cases.
58 ****************************************************************************/
60 static NTSTATUS determine_path_error(const char *name,
61 bool allow_wcard_last_component)
65 if (!allow_wcard_last_component) {
66 /* Error code within a pathname. */
67 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
70 /* We're terminating here so we
71 * can be a little slower and get
72 * the error code right. Windows
73 * treats the last part of the pathname
74 * separately I think, so if the last
75 * component is a wildcard then we treat
76 * this ./ as "end of component" */
78 p = strchr(name, '/');
80 if (!p && (ms_has_wild(name) || ISDOT(name))) {
81 /* Error code at the end of a pathname. */
82 return NT_STATUS_OBJECT_NAME_INVALID;
84 /* Error code within a pathname. */
85 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
89 /****************************************************************************
90 This routine is called to convert names from the dos namespace to unix
91 namespace. It needs to handle any case conversions, mangling, format
94 We assume that we have already done a chdir() to the right "root" directory
97 The function will return an NTSTATUS error if some part of the name except for
98 the last part cannot be resolved, else NT_STATUS_OK.
100 Note NT_STATUS_OK doesn't mean the name exists or is valid, just that we didn't
101 get any fatal errors that should immediately terminate the calling
102 SMB processing whilst resolving.
104 If the saved_last_component != 0, then the unmodified last component
105 of the pathname is returned there. If saved_last_component == 0 then nothing
108 If last_component_wcard is true then a MS wildcard was detected and
109 should be allowed in the last component of the path only.
111 On exit from unix_convert, if *pst was not null, then the file stat
112 struct will be returned if the file exists and was found, if not this
113 stat struct will be filled with zeros (and this can be detected by checking
114 for nlinks = 0, which can never be true for any file).
115 ****************************************************************************/
117 NTSTATUS unix_convert(TALLOC_CTX *ctx,
118 connection_struct *conn,
119 const char *orig_path,
120 bool allow_wcard_last_component,
122 char **pp_saved_last_component,
123 SMB_STRUCT_STAT *pst)
127 char *dirpath = NULL;
130 bool component_was_mangled = False;
131 bool name_has_wildcard = False;
132 bool posix_pathnames = false;
136 SET_STAT_INVALID(*pst);
137 *pp_conv_path = NULL;
138 if(pp_saved_last_component) {
139 *pp_saved_last_component = NULL;
143 /* we don't ever use the filenames on a printer share as a
144 filename - so don't convert them */
145 if (!(*pp_conv_path = talloc_strdup(ctx,orig_path))) {
146 return NT_STATUS_NO_MEMORY;
151 DEBUG(5, ("unix_convert called on file \"%s\"\n", orig_path));
154 * Conversion to basic unix format is already done in
155 * check_path_syntax().
159 * Names must be relative to the root of the service - any leading /.
160 * and trailing /'s should have been trimmed by check_path_syntax().
164 SMB_ASSERT(*orig_path != '/');
168 * If we trimmed down to a single '\0' character
169 * then we should use the "." directory to avoid
170 * searching the cache, but not if we are in a
172 * As we know this is valid we can return true here.
176 if (!(name = talloc_strdup(ctx,"."))) {
177 return NT_STATUS_NO_MEMORY;
179 if (SMB_VFS_STAT(conn,name,&st) == 0) {
182 return map_nt_error_from_unix(errno);
184 DEBUG(5,("conversion finished \"\" -> %s\n",name));
188 if (orig_path[0] == '.' && (orig_path[1] == '/' ||
189 orig_path[1] == '\0')) {
190 /* Start of pathname can't be "." only. */
191 if (orig_path[1] == '\0' || orig_path[2] == '\0') {
192 result = NT_STATUS_OBJECT_NAME_INVALID;
194 result =determine_path_error(
195 &orig_path[2], allow_wcard_last_component);
200 if (!(name = talloc_strdup(ctx, orig_path))) {
201 DEBUG(0, ("talloc_strdup failed\n"));
202 return NT_STATUS_NO_MEMORY;
206 * Large directory fix normalization. If we're case sensitive, and
207 * the case preserving parameters are set to "no", normalize the case of
208 * the incoming filename from the client WHETHER IT EXISTS OR NOT !
209 * This is in conflict with the current (3.0.20) man page, but is
210 * what people expect from the "large directory howto". I'll update
211 * the man page. Thanks to jht@samba.org for finding this. JRA.
214 if (conn->case_sensitive && !conn->case_preserve &&
215 !conn->short_case_preserve) {
216 strnorm(name, lp_defaultcase(SNUM(conn)));
220 * Ensure saved_last_component is valid even if file exists.
223 if(pp_saved_last_component) {
224 end = strrchr_m(name, '/');
226 *pp_saved_last_component = talloc_strdup(ctx, end + 1);
228 *pp_saved_last_component = talloc_strdup(ctx,
233 posix_pathnames = lp_posix_pathnames();
235 if (!posix_pathnames) {
236 stream = strchr_m(name, ':');
238 if (stream != NULL) {
239 char *tmp = talloc_strdup(ctx, stream);
242 return NT_STATUS_NO_MEMORY;
251 /* If we're providing case insentive semantics or
252 * the underlying filesystem is case insensitive,
253 * then a case-normalized hit in the stat-cache is
254 * authoratitive. JRA.
257 if((!conn->case_sensitive || !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) &&
258 stat_cache_lookup(conn, &name, &dirpath, &start, &st)) {
264 * Make sure "dirpath" is an allocated string, we use this for
265 * building the directories with asprintf and free it.
268 if ((dirpath == NULL) && (!(dirpath = talloc_strdup(ctx,"")))) {
269 DEBUG(0, ("talloc_strdup failed\n"));
271 return NT_STATUS_NO_MEMORY;
275 * stat the name - if it exists then we are all done!
278 if (posix_pathnames) {
279 ret = SMB_VFS_LSTAT(conn,name,&st);
281 ret = SMB_VFS_STAT(conn,name,&st);
285 /* Ensure we catch all names with in "/."
286 this is disallowed under Windows. */
287 const char *p = strstr(name, "/."); /* mb safe. */
290 /* Error code within a pathname. */
291 result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
293 } else if (p[2] == '\0') {
294 /* Error code at the end of a pathname. */
295 result = NT_STATUS_OBJECT_NAME_INVALID;
299 stat_cache_add(orig_path, name, conn->case_sensitive);
300 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
305 DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n",
306 name, dirpath, start));
309 * A special case - if we don't have any mangling chars and are case
310 * sensitive or the underlying filesystem is case insentive then searching
314 if ((conn->case_sensitive || !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) &&
315 !mangle_is_mangled(name, conn->params)) {
320 * is_mangled() was changed to look at an entire pathname, not
321 * just a component. JRA.
324 if (mangle_is_mangled(start, conn->params)) {
325 component_was_mangled = True;
329 * Now we need to recursively match the name against the real
330 * directory structure.
334 * Match each part of the path name separately, trying the names
335 * as is first, then trying to scan the directory for matching names.
338 for (; start ; start = (end?end+1:(char *)NULL)) {
340 * Pinpoint the end of this section of the filename.
342 /* mb safe. '/' can't be in any encoded char. */
343 end = strchr(start, '/');
346 * Chop the name at this point.
352 if (pp_saved_last_component) {
353 TALLOC_FREE(*pp_saved_last_component);
354 *pp_saved_last_component = talloc_strdup(ctx,
355 end ? end + 1 : start);
356 if (!*pp_saved_last_component) {
357 DEBUG(0, ("talloc failed\n"));
358 return NT_STATUS_NO_MEMORY;
362 /* The name cannot have a component of "." */
366 /* Error code at the end of a pathname. */
367 result = NT_STATUS_OBJECT_NAME_INVALID;
369 result = determine_path_error(end+1,
370 allow_wcard_last_component);
375 /* The name cannot have a wildcard if it's not
376 the last component. */
378 name_has_wildcard = ms_has_wild(start);
380 /* Wildcard not valid anywhere. */
381 if (name_has_wildcard && !allow_wcard_last_component) {
382 result = NT_STATUS_OBJECT_NAME_INVALID;
386 /* Wildcards never valid within a pathname. */
387 if (name_has_wildcard && end) {
388 result = NT_STATUS_OBJECT_NAME_INVALID;
393 * Check if the name exists up to this point.
396 if (posix_pathnames) {
397 ret = SMB_VFS_LSTAT(conn,name, &st);
399 ret = SMB_VFS_STAT(conn,name, &st);
404 * It exists. it must either be a directory or this must
405 * be the last part of the path for it to be OK.
407 if (end && !(st.st_mode & S_IFDIR)) {
409 * An intermediate part of the name isn't
412 DEBUG(5,("Not a dir %s\n",start));
415 * We need to return the fact that the
416 * intermediate name resolution failed. This
417 * is used to return an error of ERRbadpath
418 * rather than ERRbadfile. Some Windows
419 * applications depend on the difference between
422 result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
428 * We just scanned for, and found the end of
429 * the path. We must return the valid stat
437 char *found_name = NULL;
439 /* Stat failed - ensure we don't use it. */
440 SET_STAT_INVALID(st);
443 * Reset errno so we can detect
444 * directory open errors.
449 * Try to find this part of the path in the directory.
452 if (name_has_wildcard ||
453 (get_real_filename_mangled(
454 conn, dirpath, start,
455 talloc_tos(), &found_name) == -1)) {
460 * An intermediate part of the name
463 DEBUG(5,("Intermediate not found %s\n",
468 * We need to return the fact that the
469 * intermediate name resolution failed.
470 * This is used to return an error of
471 * ERRbadpath rather than ERRbadfile.
472 * Some Windows applications depend on
473 * the difference between these two
478 * ENOENT, ENOTDIR and ELOOP all map
479 * to NT_STATUS_OBJECT_PATH_NOT_FOUND
480 * in the filename walk.
483 if (errno == ENOENT ||
487 NT_STATUS_OBJECT_PATH_NOT_FOUND;
491 map_nt_error_from_unix(errno);
496 /* ENOENT is the only valid error here. */
497 if ((errno != 0) && (errno != ENOENT)) {
499 * ENOTDIR and ELOOP both map to
500 * NT_STATUS_OBJECT_PATH_NOT_FOUND
501 * in the filename walk.
503 if (errno == ENOTDIR ||
506 NT_STATUS_OBJECT_PATH_NOT_FOUND;
510 map_nt_error_from_unix(errno);
516 * Just the last part of the name doesn't exist.
517 * We need to strupper() or strlower() it as
518 * this conversion may be used for file creation
519 * purposes. Fix inspired by
520 * Thomas Neumann <t.neumann@iku-ag.de>.
522 if (!conn->case_preserve ||
523 (mangle_is_8_3(start, False,
525 !conn->short_case_preserve)) {
527 lp_defaultcase(SNUM(conn)));
531 * check on the mangled stack to see if we can
532 * recover the base of the filename.
535 if (mangle_is_mangled(start, conn->params)
536 && mangle_lookup_name_from_8_3(ctx,
541 size_t start_ofs = start - name;
543 if (*dirpath != '\0') {
544 tmp = talloc_asprintf(ctx,
547 TALLOC_FREE(unmangled);
553 DEBUG(0, ("talloc failed\n"));
554 return NT_STATUS_NO_MEMORY;
558 start = name + start_ofs;
559 end = start + strlen(start);
562 DEBUG(5,("New file %s\n",start));
568 * Restore the rest of the string. If the string was
569 * mangled the size may have changed.
573 size_t start_ofs = start - name;
575 if (*dirpath != '\0') {
576 tmp = talloc_asprintf(ctx,
581 tmp = talloc_asprintf(ctx,
586 DEBUG(0, ("talloc_asprintf failed\n"));
587 return NT_STATUS_NO_MEMORY;
591 start = name + start_ofs;
592 end = start + strlen(found_name);
596 size_t start_ofs = start - name;
598 if (*dirpath != '\0') {
599 tmp = talloc_asprintf(ctx,
603 tmp = talloc_strdup(ctx,
607 DEBUG(0, ("talloc failed\n"));
608 return NT_STATUS_NO_MEMORY;
612 start = name + start_ofs;
615 * We just scanned for, and found the end of
616 * the path. We must return a valid stat struct
620 if (posix_pathnames) {
621 ret = SMB_VFS_LSTAT(conn,name, &st);
623 ret = SMB_VFS_STAT(conn,name, &st);
629 SET_STAT_INVALID(st);
633 TALLOC_FREE(found_name);
639 * We should never provide different behaviors
640 * depending on DEVELOPER!!!
642 if (VALID_STAT(st)) {
644 get_file_infos(vfs_file_id_from_sbuf(conn, &st),
645 &delete_pending, NULL);
646 if (delete_pending) {
647 result = NT_STATUS_DELETE_PENDING;
654 * Add to the dirpath that we have resolved so far.
657 if (*dirpath != '\0') {
658 char *tmp = talloc_asprintf(ctx,
659 "%s/%s", dirpath, start);
661 DEBUG(0, ("talloc_asprintf failed\n"));
662 return NT_STATUS_NO_MEMORY;
664 TALLOC_FREE(dirpath);
668 TALLOC_FREE(dirpath);
669 if (!(dirpath = talloc_strdup(ctx,start))) {
670 DEBUG(0, ("talloc_strdup failed\n"));
671 return NT_STATUS_NO_MEMORY;
676 * Don't cache a name with mangled or wildcard components
677 * as this can change the size.
680 if(!component_was_mangled && !name_has_wildcard) {
681 stat_cache_add(orig_path, dirpath,
682 conn->case_sensitive);
686 * Restore the / that we wiped out earlier.
694 * Don't cache a name with mangled or wildcard components
695 * as this can change the size.
698 if(!component_was_mangled && !name_has_wildcard) {
699 stat_cache_add(orig_path, name, conn->case_sensitive);
703 * The name has been resolved.
706 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
709 if (stream != NULL) {
712 result = build_stream_path(ctx, conn, orig_path, name, stream,
714 if (!NT_STATUS_IS_OK(result)) {
718 DEBUG(10, ("build_stream_path returned %s\n", tmp));
723 *pp_conv_path = name;
724 TALLOC_FREE(dirpath);
727 DEBUG(10, ("dirpath = [%s] start = [%s]\n", dirpath, start));
728 if (*dirpath != '\0') {
729 *pp_conv_path = talloc_asprintf(ctx,
730 "%s/%s", dirpath, start);
732 *pp_conv_path = talloc_strdup(ctx, start);
734 if (!*pp_conv_path) {
735 DEBUG(0, ("talloc_asprintf failed\n"));
736 return NT_STATUS_NO_MEMORY;
739 TALLOC_FREE(dirpath);
743 /****************************************************************************
744 Check a filename - possibly calling check_reduced_name.
745 This is called by every routine before it allows an operation on a filename.
746 It does any final confirmation necessary to ensure that the filename is
747 a valid one for the user to access.
748 ****************************************************************************/
750 NTSTATUS check_name(connection_struct *conn, const char *name)
752 if (IS_VETO_PATH(conn, name)) {
753 /* Is it not dot or dot dot. */
754 if (!((name[0] == '.') && (!name[1] ||
755 (name[1] == '.' && !name[2])))) {
756 DEBUG(5,("check_name: file path name %s vetoed\n",
758 return map_nt_error_from_unix(ENOENT);
762 if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
763 NTSTATUS status = check_reduced_name(conn,name);
764 if (!NT_STATUS_IS_OK(status)) {
765 DEBUG(5,("check_name: name %s failed with %s\n",name,
774 /****************************************************************************
775 Check if two filenames are equal.
776 This needs to be careful about whether we are case sensitive.
777 ****************************************************************************/
779 static bool fname_equal(const char *name1, const char *name2,
782 /* Normal filename handling */
783 if (case_sensitive) {
784 return(strcmp(name1,name2) == 0);
787 return(strequal(name1,name2));
790 /****************************************************************************
791 Scan a directory to find a filename, matching without case sensitivity.
792 If the name looks like a mangled name then try via the mangling functions
793 ****************************************************************************/
795 static int get_real_filename_mangled(connection_struct *conn, const char *path,
796 const char *name, TALLOC_CTX *mem_ctx,
800 char *unmangled_name = NULL;
802 mangled = mangle_is_mangled(name, conn->params);
804 /* handle null paths */
805 if ((path == NULL) || (*path == 0)) {
809 /* If we have a case-sensitive filesystem, it doesn't do us any
810 * good to search for a name. If a case variation of the name was
811 * there, then the original stat(2) would have found it.
813 if (!mangled && !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) {
819 * The incoming name can be mangled, and if we de-mangle it
820 * here it will not compare correctly against the filename (name2)
821 * read from the directory and then mangled by the name_to_8_3()
822 * call. We need to mangle both names or neither.
825 * Fix for bug found by Dina Fine. If in case sensitive mode then
826 * the mangle cache is no good (3 letter extension could be wrong
827 * case - so don't demangle in this case - leave as mangled and
828 * allow the mangling of the directory entry read (which is done
829 * case insensitively) to match instead. This will lead to more
830 * false positive matches but we fail completely without it. JRA.
833 if (mangled && !conn->case_sensitive) {
834 mangled = !mangle_lookup_name_from_8_3(talloc_tos(), name,
838 /* Name is now unmangled. */
839 name = unmangled_name;
841 return get_real_filename(conn, path, name, mem_ctx,
845 return SMB_VFS_GET_REAL_FILENAME(conn, path, name, mem_ctx,
849 int get_real_filename(connection_struct *conn, const char *path,
850 const char *name, TALLOC_CTX *mem_ctx,
853 struct smb_Dir *cur_dir;
856 char *unmangled_name = NULL;
859 /* open the directory */
860 if (!(cur_dir = OpenDir(talloc_tos(), conn, path, NULL, 0))) {
861 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
862 TALLOC_FREE(unmangled_name);
866 /* now scan for matching names */
868 while ((dname = ReadDirName(cur_dir, &curpos, NULL))) {
870 /* Is it dot or dot dot. */
871 if (ISDOT(dname) || ISDOTDOT(dname)) {
876 * At this point dname is the unmangled name.
877 * name is either mangled or not, depending on the state
878 * of the "mangled" variable. JRA.
882 * Check mangled name against mangled name, or unmangled name
883 * against unmangled name.
886 if ((mangled && mangled_equal(name,dname,conn->params)) ||
887 fname_equal(name, dname, conn->case_sensitive)) {
888 /* we've found the file, change it's name and return */
889 *found_name = talloc_strdup(mem_ctx, dname);
890 TALLOC_FREE(unmangled_name);
891 TALLOC_FREE(cur_dir);
900 TALLOC_FREE(unmangled_name);
901 TALLOC_FREE(cur_dir);
906 static NTSTATUS build_stream_path(TALLOC_CTX *mem_ctx,
907 connection_struct *conn,
908 const char *orig_path,
909 const char *basepath,
910 const char *streamname,
911 SMB_STRUCT_STAT *pst,
917 unsigned int i, num_streams;
918 struct stream_struct *streams = NULL;
920 result = talloc_asprintf(mem_ctx, "%s%s", basepath, streamname);
921 if (result == NULL) {
922 return NT_STATUS_NO_MEMORY;
925 if (SMB_VFS_STAT(conn, result, &st) == 0) {
931 if (errno != ENOENT) {
932 status = map_nt_error_from_unix(errno);
933 DEBUG(10, ("vfs_stat failed: %s\n", nt_errstr(status)));
937 status = SMB_VFS_STREAMINFO(conn, NULL, basepath, mem_ctx,
938 &num_streams, &streams);
940 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
941 SET_STAT_INVALID(*pst);
946 if (!NT_STATUS_IS_OK(status)) {
947 DEBUG(10, ("vfs_streaminfo failed: %s\n", nt_errstr(status)));
951 for (i=0; i<num_streams; i++) {
952 DEBUG(10, ("comparing [%s] and [%s]: ",
953 streamname, streams[i].name));
954 if (fname_equal(streamname, streams[i].name,
955 conn->case_sensitive)) {
956 DEBUGADD(10, ("equal\n"));
959 DEBUGADD(10, ("not equal\n"));
962 if (i == num_streams) {
963 SET_STAT_INVALID(*pst);
965 TALLOC_FREE(streams);
971 result = talloc_asprintf(mem_ctx, "%s%s", basepath, streams[i].name);
972 if (result == NULL) {
973 status = NT_STATUS_NO_MEMORY;
977 SET_STAT_INVALID(*pst);
979 if (SMB_VFS_STAT(conn, result, pst) == 0) {
980 stat_cache_add(orig_path, result, conn->case_sensitive);
984 TALLOC_FREE(streams);
989 TALLOC_FREE(streams);