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,
37 /****************************************************************************
38 Mangle the 2nd name and check if it is then equal to the first name.
39 ****************************************************************************/
41 static bool mangled_equal(const char *name1,
43 const struct share_params *p)
47 if (!name_to_8_3(name2, mname, False, p)) {
50 return strequal(name1, mname);
53 /****************************************************************************
54 Cope with the differing wildcard and non-wildcard error cases.
55 ****************************************************************************/
57 static NTSTATUS determine_path_error(const char *name,
58 bool allow_wcard_last_component)
62 if (!allow_wcard_last_component) {
63 /* Error code within a pathname. */
64 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
67 /* We're terminating here so we
68 * can be a little slower and get
69 * the error code right. Windows
70 * treats the last part of the pathname
71 * separately I think, so if the last
72 * component is a wildcard then we treat
73 * this ./ as "end of component" */
75 p = strchr(name, '/');
77 if (!p && (ms_has_wild(name) || ISDOT(name))) {
78 /* Error code at the end of a pathname. */
79 return NT_STATUS_OBJECT_NAME_INVALID;
81 /* Error code within a pathname. */
82 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
86 /****************************************************************************
87 This routine is called to convert names from the dos namespace to unix
88 namespace. It needs to handle any case conversions, mangling, format
91 We assume that we have already done a chdir() to the right "root" directory
94 The function will return an NTSTATUS error if some part of the name except for
95 the last part cannot be resolved, else NT_STATUS_OK.
97 Note NT_STATUS_OK doesn't mean the name exists or is valid, just that we didn't
98 get any fatal errors that should immediately terminate the calling
99 SMB processing whilst resolving.
101 If the saved_last_component != 0, then the unmodified last component
102 of the pathname is returned there. If saved_last_component == 0 then nothing
105 If last_component_wcard is true then a MS wildcard was detected and
106 should be allowed in the last component of the path only.
108 On exit from unix_convert, if *pst was not null, then the file stat
109 struct will be returned if the file exists and was found, if not this
110 stat struct will be filled with zeros (and this can be detected by checking
111 for nlinks = 0, which can never be true for any file).
112 ****************************************************************************/
114 NTSTATUS unix_convert(TALLOC_CTX *ctx,
115 connection_struct *conn,
116 const char *orig_path,
117 bool allow_wcard_last_component,
119 char **pp_saved_last_component,
120 SMB_STRUCT_STAT *pst)
124 char *dirpath = NULL;
127 bool component_was_mangled = False;
128 bool name_has_wildcard = False;
129 bool posix_pathnames = false;
133 SET_STAT_INVALID(*pst);
134 *pp_conv_path = NULL;
135 if(pp_saved_last_component) {
136 *pp_saved_last_component = NULL;
140 /* we don't ever use the filenames on a printer share as a
141 filename - so don't convert them */
142 if (!(*pp_conv_path = talloc_strdup(ctx,orig_path))) {
143 return NT_STATUS_NO_MEMORY;
148 DEBUG(5, ("unix_convert called on file \"%s\"\n", orig_path));
151 * Conversion to basic unix format is already done in
152 * check_path_syntax().
156 * Names must be relative to the root of the service - any leading /.
157 * and trailing /'s should have been trimmed by check_path_syntax().
161 SMB_ASSERT(*orig_path != '/');
165 * If we trimmed down to a single '\0' character
166 * then we should use the "." directory to avoid
167 * searching the cache, but not if we are in a
169 * As we know this is valid we can return true here.
173 if (!(name = talloc_strdup(ctx,"."))) {
174 return NT_STATUS_NO_MEMORY;
176 if (SMB_VFS_STAT(conn,name,&st) == 0) {
179 return map_nt_error_from_unix(errno);
181 DEBUG(5,("conversion finished \"\" -> %s\n",name));
185 if (orig_path[0] == '.' && (orig_path[1] == '/' ||
186 orig_path[1] == '\0')) {
187 /* Start of pathname can't be "." only. */
188 if (orig_path[1] == '\0' || orig_path[2] == '\0') {
189 result = NT_STATUS_OBJECT_NAME_INVALID;
191 result =determine_path_error(
192 &orig_path[2], allow_wcard_last_component);
197 if (!(name = talloc_strdup(ctx, orig_path))) {
198 DEBUG(0, ("talloc_strdup failed\n"));
199 return NT_STATUS_NO_MEMORY;
203 * Large directory fix normalization. If we're case sensitive, and
204 * the case preserving parameters are set to "no", normalize the case of
205 * the incoming filename from the client WHETHER IT EXISTS OR NOT !
206 * This is in conflict with the current (3.0.20) man page, but is
207 * what people expect from the "large directory howto". I'll update
208 * the man page. Thanks to jht@samba.org for finding this. JRA.
211 if (conn->case_sensitive && !conn->case_preserve &&
212 !conn->short_case_preserve) {
213 strnorm(name, lp_defaultcase(SNUM(conn)));
217 * Ensure saved_last_component is valid even if file exists.
220 if(pp_saved_last_component) {
221 end = strrchr_m(name, '/');
223 *pp_saved_last_component = talloc_strdup(ctx, end + 1);
225 *pp_saved_last_component = talloc_strdup(ctx,
230 posix_pathnames = lp_posix_pathnames();
232 if (!posix_pathnames) {
233 stream = strchr_m(name, ':');
235 if (stream != NULL) {
236 char *tmp = talloc_strdup(ctx, stream);
239 return NT_STATUS_NO_MEMORY;
248 /* If we're providing case insentive semantics or
249 * the underlying filesystem is case insensitive,
250 * then a case-normalized hit in the stat-cache is
251 * authoratitive. JRA.
254 if((!conn->case_sensitive || !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) &&
255 stat_cache_lookup(conn, &name, &dirpath, &start, &st)) {
261 * Make sure "dirpath" is an allocated string, we use this for
262 * building the directories with asprintf and free it.
265 if ((dirpath == NULL) && (!(dirpath = talloc_strdup(ctx,"")))) {
266 DEBUG(0, ("talloc_strdup failed\n"));
268 return NT_STATUS_NO_MEMORY;
271 name_has_wildcard = ms_has_wild(name);
273 /* Wildcard not valid anywhere. */
274 if (name_has_wildcard && !allow_wcard_last_component) {
275 result = NT_STATUS_OBJECT_NAME_INVALID;
279 if (!name_has_wildcard) {
281 * stat the name - if it exists then we are all done!
284 if (posix_pathnames) {
285 ret = SMB_VFS_LSTAT(conn,name,&st);
287 ret = SMB_VFS_STAT(conn,name,&st);
291 /* Ensure we catch all names with in "/."
292 this is disallowed under Windows. */
293 const char *p = strstr(name, "/."); /* mb safe. */
296 /* Error code within a pathname. */
297 result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
299 } else if (p[2] == '\0') {
300 /* Error code at the end of a pathname. */
301 result = NT_STATUS_OBJECT_NAME_INVALID;
305 stat_cache_add(orig_path, name, conn->case_sensitive);
306 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
312 DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n",
313 name, dirpath, start));
316 * A special case - if we don't have any mangling chars and are case
317 * sensitive or the underlying filesystem is case insentive then searching
321 if ((conn->case_sensitive || !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) &&
322 !mangle_is_mangled(name, conn->params)) {
327 * is_mangled() was changed to look at an entire pathname, not
328 * just a component. JRA.
331 if (mangle_is_mangled(start, conn->params)) {
332 component_was_mangled = True;
336 * Now we need to recursively match the name against the real
337 * directory structure.
341 * Match each part of the path name separately, trying the names
342 * as is first, then trying to scan the directory for matching names.
345 for (; start ; start = (end?end+1:(char *)NULL)) {
347 * Pinpoint the end of this section of the filename.
349 /* mb safe. '/' can't be in any encoded char. */
350 end = strchr(start, '/');
353 * Chop the name at this point.
359 if (pp_saved_last_component) {
360 TALLOC_FREE(*pp_saved_last_component);
361 *pp_saved_last_component = talloc_strdup(ctx,
362 end ? end + 1 : start);
363 if (!*pp_saved_last_component) {
364 DEBUG(0, ("talloc failed\n"));
365 return NT_STATUS_NO_MEMORY;
369 /* The name cannot have a component of "." */
373 /* Error code at the end of a pathname. */
374 result = NT_STATUS_OBJECT_NAME_INVALID;
376 result = determine_path_error(end+1,
377 allow_wcard_last_component);
382 /* The name cannot have a wildcard if it's not
383 the last component. */
385 name_has_wildcard = ms_has_wild(start);
387 /* Wildcards never valid within a pathname. */
388 if (name_has_wildcard && end) {
389 result = NT_STATUS_OBJECT_NAME_INVALID;
393 if (name_has_wildcard) {
394 DEBUG(5,("Wildcard %s\n",start));
399 * Check if the name exists up to this point.
402 if (posix_pathnames) {
403 ret = SMB_VFS_LSTAT(conn,name, &st);
405 ret = SMB_VFS_STAT(conn,name, &st);
410 * It exists. it must either be a directory or this must
411 * be the last part of the path for it to be OK.
413 if (end && !S_ISDIR(st.st_ex_mode)) {
415 * An intermediate part of the name isn't
418 DEBUG(5,("Not a dir %s\n",start));
421 * We need to return the fact that the
422 * intermediate name resolution failed. This
423 * is used to return an error of ERRbadpath
424 * rather than ERRbadfile. Some Windows
425 * applications depend on the difference between
428 result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
434 * We just scanned for, and found the end of
435 * the path. We must return the valid stat
443 char *found_name = NULL;
445 /* Stat failed - ensure we don't use it. */
446 SET_STAT_INVALID(st);
449 * Reset errno so we can detect
450 * directory open errors.
455 * Try to find this part of the path in the directory.
458 if (name_has_wildcard ||
459 (get_real_filename(conn, dirpath, start,
461 &found_name) == -1)) {
466 * An intermediate part of the name
469 DEBUG(5,("Intermediate not found %s\n",
474 * We need to return the fact that the
475 * intermediate name resolution failed.
476 * This is used to return an error of
477 * ERRbadpath rather than ERRbadfile.
478 * Some Windows applications depend on
479 * the difference between these two
484 * ENOENT, ENOTDIR and ELOOP all map
485 * to NT_STATUS_OBJECT_PATH_NOT_FOUND
486 * in the filename walk.
489 if (errno == ENOENT ||
493 NT_STATUS_OBJECT_PATH_NOT_FOUND;
497 map_nt_error_from_unix(errno);
503 * ENOENT/EACCESS are the only valid errors
504 * here. EACCESS needs handling here for
505 * "dropboxes", i.e. directories where users
506 * can only put stuff with permission -wx.
508 if ((errno != 0) && (errno != ENOENT)
509 && (errno != EACCES)) {
511 * ENOTDIR and ELOOP both map to
512 * NT_STATUS_OBJECT_PATH_NOT_FOUND
513 * in the filename walk.
515 if (errno == ENOTDIR ||
518 NT_STATUS_OBJECT_PATH_NOT_FOUND;
521 map_nt_error_from_unix(errno);
527 * Just the last part of the name doesn't exist.
528 * We need to strupper() or strlower() it as
529 * this conversion may be used for file creation
530 * purposes. Fix inspired by
531 * Thomas Neumann <t.neumann@iku-ag.de>.
533 if (!conn->case_preserve ||
534 (mangle_is_8_3(start, False,
536 !conn->short_case_preserve)) {
538 lp_defaultcase(SNUM(conn)));
542 * check on the mangled stack to see if we can
543 * recover the base of the filename.
546 if (mangle_is_mangled(start, conn->params)
547 && mangle_lookup_name_from_8_3(ctx,
552 size_t start_ofs = start - name;
554 if (*dirpath != '\0') {
555 tmp = talloc_asprintf(ctx,
558 TALLOC_FREE(unmangled);
564 DEBUG(0, ("talloc failed\n"));
565 return NT_STATUS_NO_MEMORY;
569 start = name + start_ofs;
570 end = start + strlen(start);
573 DEBUG(5,("New file %s\n",start));
579 * Restore the rest of the string. If the string was
580 * mangled the size may have changed.
584 size_t start_ofs = start - name;
586 if (*dirpath != '\0') {
587 tmp = talloc_asprintf(ctx,
592 tmp = talloc_asprintf(ctx,
597 DEBUG(0, ("talloc_asprintf failed\n"));
598 return NT_STATUS_NO_MEMORY;
602 start = name + start_ofs;
603 end = start + strlen(found_name);
607 size_t start_ofs = start - name;
609 if (*dirpath != '\0') {
610 tmp = talloc_asprintf(ctx,
614 tmp = talloc_strdup(ctx,
618 DEBUG(0, ("talloc failed\n"));
619 return NT_STATUS_NO_MEMORY;
623 start = name + start_ofs;
626 * We just scanned for, and found the end of
627 * the path. We must return a valid stat struct
631 if (posix_pathnames) {
632 ret = SMB_VFS_LSTAT(conn,name, &st);
634 ret = SMB_VFS_STAT(conn,name, &st);
640 SET_STAT_INVALID(st);
644 TALLOC_FREE(found_name);
650 * We should never provide different behaviors
651 * depending on DEVELOPER!!!
653 if (VALID_STAT(st)) {
655 get_file_infos(vfs_file_id_from_sbuf(conn, &st),
656 &delete_pending, NULL);
657 if (delete_pending) {
658 result = NT_STATUS_DELETE_PENDING;
665 * Add to the dirpath that we have resolved so far.
668 if (*dirpath != '\0') {
669 char *tmp = talloc_asprintf(ctx,
670 "%s/%s", dirpath, start);
672 DEBUG(0, ("talloc_asprintf failed\n"));
673 return NT_STATUS_NO_MEMORY;
675 TALLOC_FREE(dirpath);
679 TALLOC_FREE(dirpath);
680 if (!(dirpath = talloc_strdup(ctx,start))) {
681 DEBUG(0, ("talloc_strdup failed\n"));
682 return NT_STATUS_NO_MEMORY;
687 * Don't cache a name with mangled or wildcard components
688 * as this can change the size.
691 if(!component_was_mangled && !name_has_wildcard) {
692 stat_cache_add(orig_path, dirpath,
693 conn->case_sensitive);
697 * Restore the / that we wiped out earlier.
705 * Don't cache a name with mangled or wildcard components
706 * as this can change the size.
709 if(!component_was_mangled && !name_has_wildcard) {
710 stat_cache_add(orig_path, name, conn->case_sensitive);
714 * The name has been resolved.
717 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
720 if (stream != NULL) {
723 result = build_stream_path(ctx, conn, orig_path, name, stream,
725 if (!NT_STATUS_IS_OK(result)) {
729 DEBUG(10, ("build_stream_path returned %s\n", tmp));
734 *pp_conv_path = name;
735 TALLOC_FREE(dirpath);
738 DEBUG(10, ("dirpath = [%s] start = [%s]\n", dirpath, start));
739 if (*dirpath != '\0') {
740 *pp_conv_path = talloc_asprintf(ctx,
741 "%s/%s", dirpath, start);
743 *pp_conv_path = talloc_strdup(ctx, start);
745 if (!*pp_conv_path) {
746 DEBUG(0, ("talloc_asprintf failed\n"));
747 return NT_STATUS_NO_MEMORY;
750 TALLOC_FREE(dirpath);
754 /****************************************************************************
755 Check a filename - possibly calling check_reduced_name.
756 This is called by every routine before it allows an operation on a filename.
757 It does any final confirmation necessary to ensure that the filename is
758 a valid one for the user to access.
759 ****************************************************************************/
761 NTSTATUS check_name(connection_struct *conn, const char *name)
763 if (IS_VETO_PATH(conn, name)) {
764 /* Is it not dot or dot dot. */
765 if (!((name[0] == '.') && (!name[1] ||
766 (name[1] == '.' && !name[2])))) {
767 DEBUG(5,("check_name: file path name %s vetoed\n",
769 return map_nt_error_from_unix(ENOENT);
773 if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
774 NTSTATUS status = check_reduced_name(conn,name);
775 if (!NT_STATUS_IS_OK(status)) {
776 DEBUG(5,("check_name: name %s failed with %s\n",name,
785 /****************************************************************************
786 Check if two filenames are equal.
787 This needs to be careful about whether we are case sensitive.
788 ****************************************************************************/
790 static bool fname_equal(const char *name1, const char *name2,
793 /* Normal filename handling */
794 if (case_sensitive) {
795 return(strcmp(name1,name2) == 0);
798 return(strequal(name1,name2));
801 /****************************************************************************
802 Scan a directory to find a filename, matching without case sensitivity.
803 If the name looks like a mangled name then try via the mangling functions
804 ****************************************************************************/
806 static int get_real_filename_full_scan(connection_struct *conn,
807 const char *path, const char *name,
809 TALLOC_CTX *mem_ctx, char **found_name)
811 struct smb_Dir *cur_dir;
813 char *unmangled_name = NULL;
816 /* handle null paths */
817 if ((path == NULL) || (*path == 0)) {
821 /* If we have a case-sensitive filesystem, it doesn't do us any
822 * good to search for a name. If a case variation of the name was
823 * there, then the original stat(2) would have found it.
825 if (!mangled && !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) {
831 * The incoming name can be mangled, and if we de-mangle it
832 * here it will not compare correctly against the filename (name2)
833 * read from the directory and then mangled by the name_to_8_3()
834 * call. We need to mangle both names or neither.
837 * Fix for bug found by Dina Fine. If in case sensitive mode then
838 * the mangle cache is no good (3 letter extension could be wrong
839 * case - so don't demangle in this case - leave as mangled and
840 * allow the mangling of the directory entry read (which is done
841 * case insensitively) to match instead. This will lead to more
842 * false positive matches but we fail completely without it. JRA.
845 if (mangled && !conn->case_sensitive) {
846 mangled = !mangle_lookup_name_from_8_3(talloc_tos(), name,
850 /* Name is now unmangled. */
851 name = unmangled_name;
855 /* open the directory */
856 if (!(cur_dir = OpenDir(talloc_tos(), conn, path, NULL, 0))) {
857 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
858 TALLOC_FREE(unmangled_name);
862 /* now scan for matching names */
864 while ((dname = ReadDirName(cur_dir, &curpos, NULL))) {
866 /* Is it dot or dot dot. */
867 if (ISDOT(dname) || ISDOTDOT(dname)) {
872 * At this point dname is the unmangled name.
873 * name is either mangled or not, depending on the state
874 * of the "mangled" variable. JRA.
878 * Check mangled name against mangled name, or unmangled name
879 * against unmangled name.
882 if ((mangled && mangled_equal(name,dname,conn->params)) ||
883 fname_equal(name, dname, conn->case_sensitive)) {
884 /* we've found the file, change it's name and return */
885 *found_name = talloc_strdup(mem_ctx, dname);
886 TALLOC_FREE(unmangled_name);
887 TALLOC_FREE(cur_dir);
896 TALLOC_FREE(unmangled_name);
897 TALLOC_FREE(cur_dir);
902 /****************************************************************************
903 Wrapper around the vfs get_real_filename and the full directory scan
905 ****************************************************************************/
907 int get_real_filename(connection_struct *conn, const char *path,
908 const char *name, TALLOC_CTX *mem_ctx,
914 mangled = mangle_is_mangled(name, conn->params);
917 return get_real_filename_full_scan(conn, path, name, mangled,
918 mem_ctx, found_name);
921 /* Try the vfs first to take advantage of case-insensitive stat. */
922 ret = SMB_VFS_GET_REAL_FILENAME(conn, path, name, mem_ctx, found_name);
925 * If the case-insensitive stat was successful, or returned an error
926 * other than EOPNOTSUPP then there is no need to fall back on the
927 * full directory scan.
929 if (ret == 0 || (ret == -1 && errno != EOPNOTSUPP)) {
933 return get_real_filename_full_scan(conn, path, name, mangled, mem_ctx,
937 static NTSTATUS build_stream_path(TALLOC_CTX *mem_ctx,
938 connection_struct *conn,
939 const char *orig_path,
940 const char *basepath,
941 const char *streamname,
942 SMB_STRUCT_STAT *pst,
948 unsigned int i, num_streams;
949 struct stream_struct *streams = NULL;
951 result = talloc_asprintf(mem_ctx, "%s%s", basepath, streamname);
952 if (result == NULL) {
953 return NT_STATUS_NO_MEMORY;
956 if (SMB_VFS_STAT(conn, result, &st) == 0) {
962 if (errno != ENOENT) {
963 status = map_nt_error_from_unix(errno);
964 DEBUG(10, ("vfs_stat failed: %s\n", nt_errstr(status)));
968 status = SMB_VFS_STREAMINFO(conn, NULL, basepath, mem_ctx,
969 &num_streams, &streams);
971 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
972 SET_STAT_INVALID(*pst);
977 if (!NT_STATUS_IS_OK(status)) {
978 DEBUG(10, ("vfs_streaminfo failed: %s\n", nt_errstr(status)));
982 for (i=0; i<num_streams; i++) {
983 DEBUG(10, ("comparing [%s] and [%s]: ",
984 streamname, streams[i].name));
985 if (fname_equal(streamname, streams[i].name,
986 conn->case_sensitive)) {
987 DEBUGADD(10, ("equal\n"));
990 DEBUGADD(10, ("not equal\n"));
993 if (i == num_streams) {
994 SET_STAT_INVALID(*pst);
996 TALLOC_FREE(streams);
1000 TALLOC_FREE(result);
1002 result = talloc_asprintf(mem_ctx, "%s%s", basepath, streams[i].name);
1003 if (result == NULL) {
1004 status = NT_STATUS_NO_MEMORY;
1008 SET_STAT_INVALID(*pst);
1010 if (SMB_VFS_STAT(conn, result, pst) == 0) {
1011 stat_cache_add(orig_path, result, conn->case_sensitive);
1015 TALLOC_FREE(streams);
1016 return NT_STATUS_OK;
1019 TALLOC_FREE(result);
1020 TALLOC_FREE(streams);