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 bool scan_directory(connection_struct *conn, const char *path,
30 char *name, char **found_name);
32 /****************************************************************************
33 Mangle the 2nd name and check if it is then equal to the first name.
34 ****************************************************************************/
36 static bool mangled_equal(const char *name1,
38 const struct share_params *p)
42 if (!name_to_8_3(name2, mname, False, p)) {
45 return strequal(name1, mname);
48 /****************************************************************************
49 Cope with the differing wildcard and non-wildcard error cases.
50 ****************************************************************************/
52 static NTSTATUS determine_path_error(const char *name,
53 bool allow_wcard_last_component)
57 if (!allow_wcard_last_component) {
58 /* Error code within a pathname. */
59 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
62 /* We're terminating here so we
63 * can be a little slower and get
64 * the error code right. Windows
65 * treats the last part of the pathname
66 * separately I think, so if the last
67 * component is a wildcard then we treat
68 * this ./ as "end of component" */
70 p = strchr(name, '/');
72 if (!p && (ms_has_wild(name) || ISDOT(name))) {
73 /* Error code at the end of a pathname. */
74 return NT_STATUS_OBJECT_NAME_INVALID;
76 /* Error code within a pathname. */
77 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
81 /****************************************************************************
82 This routine is called to convert names from the dos namespace to unix
83 namespace. It needs to handle any case conversions, mangling, format
86 We assume that we have already done a chdir() to the right "root" directory
89 The function will return an NTSTATUS error if some part of the name except for
90 the last part cannot be resolved, else NT_STATUS_OK.
92 Note NT_STATUS_OK doesn't mean the name exists or is valid, just that we didn't
93 get any fatal errors that should immediately terminate the calling
94 SMB processing whilst resolving.
96 If the saved_last_component != 0, then the unmodified last component
97 of the pathname is returned there. This is used in an exceptional
98 case in reply_mv (so far). If saved_last_component == 0 then nothing
101 If last_component_wcard is true then a MS wildcard was detected and
102 should be allowed in the last component of the path only.
104 On exit from unix_convert, if *pst was not null, then the file stat
105 struct will be returned if the file exists and was found, if not this
106 stat struct will be filled with zeros (and this can be detected by checking
107 for nlinks = 0, which can never be true for any file).
108 ****************************************************************************/
110 NTSTATUS unix_convert(TALLOC_CTX *ctx,
111 connection_struct *conn,
112 const char *orig_path,
113 bool allow_wcard_last_component,
115 char **pp_saved_last_component,
116 SMB_STRUCT_STAT *pst)
120 char *dirpath = NULL;
122 bool component_was_mangled = False;
123 bool name_has_wildcard = False;
126 SET_STAT_INVALID(*pst);
127 *pp_conv_path = NULL;
128 if(pp_saved_last_component) {
129 *pp_saved_last_component = NULL;
133 /* we don't ever use the filenames on a printer share as a
134 filename - so don't convert them */
135 if (!(*pp_conv_path = talloc_strdup(ctx,orig_path))) {
136 return NT_STATUS_NO_MEMORY;
141 DEBUG(5, ("unix_convert called on file \"%s\"\n", orig_path));
144 * Conversion to basic unix format is already done in
145 * check_path_syntax().
149 * Names must be relative to the root of the service - any leading /.
150 * and trailing /'s should have been trimmed by check_path_syntax().
154 SMB_ASSERT(*orig_path != '/');
158 * If we trimmed down to a single '\0' character
159 * then we should use the "." directory to avoid
160 * searching the cache, but not if we are in a
162 * As we know this is valid we can return true here.
166 if (!(name = talloc_strdup(ctx,"."))) {
167 return NT_STATUS_NO_MEMORY;
169 if (SMB_VFS_STAT(conn,name,&st) == 0) {
172 return map_nt_error_from_unix(errno);
174 DEBUG(5,("conversion finished \"\" -> %s\n",name));
178 if (orig_path[0] == '.' && (orig_path[1] == '/' ||
179 orig_path[1] == '\0')) {
180 /* Start of pathname can't be "." only. */
181 if (orig_path[1] == '\0' || orig_path[2] == '\0') {
182 result = NT_STATUS_OBJECT_NAME_INVALID;
184 result =determine_path_error(
185 &orig_path[2], allow_wcard_last_component);
191 * Ensure saved_last_component is valid even if file exists.
194 if(pp_saved_last_component) {
195 end = strrchr_m(orig_path, '/');
197 *pp_saved_last_component = talloc_strdup(ctx, end + 1);
199 *pp_saved_last_component = talloc_strdup(ctx,
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)));
225 /* If we're providing case insentive semantics or
226 * the underlying filesystem is case insensitive,
227 * then a case-normalized hit in the stat-cache is
228 * authoratitive. JRA.
231 if((!conn->case_sensitive || !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) &&
232 stat_cache_lookup(conn, &name, &dirpath, &start, &st)) {
238 * Make sure "dirpath" is an allocated string, we use this for
239 * building the directories with asprintf and free it.
242 if ((dirpath == NULL) && (!(dirpath = talloc_strdup(ctx,"")))) {
243 DEBUG(0, ("talloc_strdup failed\n"));
245 return NT_STATUS_NO_MEMORY;
249 * stat the name - if it exists then we are all done!
252 if (SMB_VFS_STAT(conn,name,&st) == 0) {
253 /* Ensure we catch all names with in "/."
254 this is disallowed under Windows. */
255 const char *p = strstr(name, "/."); /* mb safe. */
258 /* Error code within a pathname. */
259 result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
261 } else if (p[2] == '\0') {
262 /* Error code at the end of a pathname. */
263 result = NT_STATUS_OBJECT_NAME_INVALID;
267 stat_cache_add(orig_path, name, conn->case_sensitive);
268 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
273 DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n",
274 name, dirpath, start));
277 * A special case - if we don't have any mangling chars and are case
278 * sensitive or the underlying filesystem is case insentive then searching
282 if ((conn->case_sensitive || !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) &&
283 !mangle_is_mangled(name, conn->params)) {
288 * is_mangled() was changed to look at an entire pathname, not
289 * just a component. JRA.
292 if (mangle_is_mangled(start, conn->params)) {
293 component_was_mangled = True;
297 * Now we need to recursively match the name against the real
298 * directory structure.
302 * Match each part of the path name separately, trying the names
303 * as is first, then trying to scan the directory for matching names.
306 for (; start ; start = (end?end+1:(char *)NULL)) {
308 * Pinpoint the end of this section of the filename.
310 /* mb safe. '/' can't be in any encoded char. */
311 end = strchr(start, '/');
314 * Chop the name at this point.
320 if (pp_saved_last_component) {
321 TALLOC_FREE(*pp_saved_last_component);
322 *pp_saved_last_component = talloc_strdup(ctx,
323 end ? end + 1 : start);
324 if (!*pp_saved_last_component) {
325 DEBUG(0, ("talloc failed\n"));
326 return NT_STATUS_NO_MEMORY;
330 /* The name cannot have a component of "." */
334 /* Error code at the end of a pathname. */
335 result = NT_STATUS_OBJECT_NAME_INVALID;
337 result = determine_path_error(end+1,
338 allow_wcard_last_component);
343 /* The name cannot have a wildcard if it's not
344 the last component. */
346 name_has_wildcard = ms_has_wild(start);
348 /* Wildcard not valid anywhere. */
349 if (name_has_wildcard && !allow_wcard_last_component) {
350 result = NT_STATUS_OBJECT_NAME_INVALID;
354 /* Wildcards never valid within a pathname. */
355 if (name_has_wildcard && end) {
356 result = NT_STATUS_OBJECT_NAME_INVALID;
361 * Check if the name exists up to this point.
364 if (SMB_VFS_STAT(conn,name, &st) == 0) {
366 * It exists. it must either be a directory or this must
367 * be the last part of the path for it to be OK.
369 if (end && !(st.st_mode & S_IFDIR)) {
371 * An intermediate part of the name isn't
374 DEBUG(5,("Not a dir %s\n",start));
377 * We need to return the fact that the
378 * intermediate name resolution failed. This
379 * is used to return an error of ERRbadpath
380 * rather than ERRbadfile. Some Windows
381 * applications depend on the difference between
384 result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
390 * We just scanned for, and found the end of
391 * the path. We must return the valid stat
399 char *found_name = NULL;
401 /* Stat failed - ensure we don't use it. */
402 SET_STAT_INVALID(st);
405 * Reset errno so we can detect
406 * directory open errors.
411 * Try to find this part of the path in the directory.
414 if (name_has_wildcard ||
415 !scan_directory(conn, dirpath,
416 start, &found_name)) {
421 * An intermediate part of the name
424 DEBUG(5,("Intermediate not found %s\n",
429 * We need to return the fact that the
430 * intermediate name resolution failed.
431 * This is used to return an error of
432 * ERRbadpath rather than ERRbadfile.
433 * Some Windows applications depend on
434 * the difference between these two
439 * ENOENT, ENOTDIR and ELOOP all map
440 * to NT_STATUS_OBJECT_PATH_NOT_FOUND
441 * in the filename walk.
444 if (errno == ENOENT ||
448 NT_STATUS_OBJECT_PATH_NOT_FOUND;
452 map_nt_error_from_unix(errno);
457 /* ENOENT is the only valid error here. */
458 if (errno != ENOENT) {
460 * ENOTDIR and ELOOP both map to
461 * NT_STATUS_OBJECT_PATH_NOT_FOUND
462 * in the filename walk.
464 if (errno == ENOTDIR ||
467 NT_STATUS_OBJECT_PATH_NOT_FOUND;
471 map_nt_error_from_unix(errno);
477 * Just the last part of the name doesn't exist.
478 * We need to strupper() or strlower() it as
479 * this conversion may be used for file creation
480 * purposes. Fix inspired by
481 * Thomas Neumann <t.neumann@iku-ag.de>.
483 if (!conn->case_preserve ||
484 (mangle_is_8_3(start, False,
486 !conn->short_case_preserve)) {
488 lp_defaultcase(SNUM(conn)));
492 * check on the mangled stack to see if we can
493 * recover the base of the filename.
496 if (mangle_is_mangled(start, conn->params)
497 && mangle_lookup_name_from_8_3(ctx,
502 size_t start_ofs = start - name;
504 if (*dirpath != '\0') {
505 tmp = talloc_asprintf(ctx,
508 TALLOC_FREE(unmangled);
514 DEBUG(0, ("talloc failed\n"));
515 return NT_STATUS_NO_MEMORY;
519 start = name + start_ofs;
520 end = start + strlen(start);
523 DEBUG(5,("New file %s\n",start));
529 * Restore the rest of the string. If the string was
530 * mangled the size may have changed.
534 size_t start_ofs = start - name;
536 if (*dirpath != '\0') {
537 tmp = talloc_asprintf(ctx,
542 tmp = talloc_asprintf(ctx,
547 DEBUG(0, ("talloc_asprintf failed\n"));
548 return NT_STATUS_NO_MEMORY;
552 start = name + start_ofs;
553 end = start + strlen(found_name);
557 size_t start_ofs = start - name;
559 if (*dirpath != '\0') {
560 tmp = talloc_asprintf(ctx,
564 tmp = talloc_strdup(ctx,
568 DEBUG(0, ("talloc failed\n"));
569 return NT_STATUS_NO_MEMORY;
573 start = name + start_ofs;
576 * We just scanned for, and found the end of
577 * the path. We must return a valid stat struct
581 if (SMB_VFS_STAT(conn,name, &st) == 0) {
584 SET_STAT_INVALID(st);
588 TALLOC_FREE(found_name);
592 if (VALID_STAT(st) &&
593 get_delete_on_close_flag(vfs_file_id_from_sbuf(conn,
595 result = NT_STATUS_DELETE_PENDING;
601 * Add to the dirpath that we have resolved so far.
604 if (*dirpath != '\0') {
605 char *tmp = talloc_asprintf(ctx,
606 "%s/%s", dirpath, start);
608 DEBUG(0, ("talloc_asprintf failed\n"));
609 return NT_STATUS_NO_MEMORY;
611 TALLOC_FREE(dirpath);
615 TALLOC_FREE(dirpath);
616 if (!(dirpath = talloc_strdup(ctx,start))) {
617 DEBUG(0, ("talloc_strdup failed\n"));
618 return NT_STATUS_NO_MEMORY;
623 * Don't cache a name with mangled or wildcard components
624 * as this can change the size.
627 if(!component_was_mangled && !name_has_wildcard) {
628 stat_cache_add(orig_path, dirpath,
629 conn->case_sensitive);
633 * Restore the / that we wiped out earlier.
641 * Don't cache a name with mangled or wildcard components
642 * as this can change the size.
645 if(!component_was_mangled && !name_has_wildcard) {
646 stat_cache_add(orig_path, name, conn->case_sensitive);
650 * The name has been resolved.
653 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
656 *pp_conv_path = name;
657 TALLOC_FREE(dirpath);
660 DEBUG(10, ("dirpath = [%s] start = [%s]\n", dirpath, start));
661 if (*dirpath != '\0') {
662 *pp_conv_path = talloc_asprintf(ctx,
663 "%s/%s", dirpath, start);
665 *pp_conv_path = talloc_strdup(ctx, start);
667 if (!*pp_conv_path) {
668 DEBUG(0, ("talloc_asprintf failed\n"));
669 return NT_STATUS_NO_MEMORY;
672 TALLOC_FREE(dirpath);
676 /****************************************************************************
677 Check a filename - possibly calling check_reduced_name.
678 This is called by every routine before it allows an operation on a filename.
679 It does any final confirmation necessary to ensure that the filename is
680 a valid one for the user to access.
681 ****************************************************************************/
683 NTSTATUS check_name(connection_struct *conn, const char *name)
685 if (IS_VETO_PATH(conn, name)) {
686 /* Is it not dot or dot dot. */
687 if (!((name[0] == '.') && (!name[1] ||
688 (name[1] == '.' && !name[2])))) {
689 DEBUG(5,("check_name: file path name %s vetoed\n",
691 return map_nt_error_from_unix(ENOENT);
695 if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
696 NTSTATUS status = check_reduced_name(conn,name);
697 if (!NT_STATUS_IS_OK(status)) {
698 DEBUG(5,("check_name: name %s failed with %s\n",name,
707 /****************************************************************************
708 Check if two filenames are equal.
709 This needs to be careful about whether we are case sensitive.
710 ****************************************************************************/
712 static bool fname_equal(const char *name1, const char *name2,
715 /* Normal filename handling */
716 if (case_sensitive) {
717 return(strcmp(name1,name2) == 0);
720 return(strequal(name1,name2));
723 /****************************************************************************
724 Scan a directory to find a filename, matching without case sensitivity.
725 If the name looks like a mangled name then try via the mangling functions
726 ****************************************************************************/
728 static bool scan_directory(connection_struct *conn, const char *path,
729 char *name, char **found_name)
731 struct smb_Dir *cur_dir;
734 char *unmangled_name = NULL;
736 TALLOC_CTX *ctx = talloc_tos();
738 mangled = mangle_is_mangled(name, conn->params);
740 /* handle null paths */
741 if ((path == NULL) || (*path == 0)) {
745 /* If we have a case-sensitive filesystem, it doesn't do us any
746 * good to search for a name. If a case variation of the name was
747 * there, then the original stat(2) would have found it.
749 if (!mangled && !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) {
755 * The incoming name can be mangled, and if we de-mangle it
756 * here it will not compare correctly against the filename (name2)
757 * read from the directory and then mangled by the name_to_8_3()
758 * call. We need to mangle both names or neither.
761 * Fix for bug found by Dina Fine. If in case sensitive mode then
762 * the mangle cache is no good (3 letter extension could be wrong
763 * case - so don't demangle in this case - leave as mangled and
764 * allow the mangling of the directory entry read (which is done
765 * case insensitively) to match instead. This will lead to more
766 * false positive matches but we fail completely without it. JRA.
769 if (mangled && !conn->case_sensitive) {
770 mangled = !mangle_lookup_name_from_8_3(ctx,
775 /* Name is now unmangled. */
776 name = unmangled_name;
780 /* open the directory */
781 if (!(cur_dir = OpenDir(talloc_tos(), conn, path, NULL, 0))) {
782 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
783 TALLOC_FREE(unmangled_name);
787 /* now scan for matching names */
789 while ((dname = ReadDirName(cur_dir, &curpos))) {
791 /* Is it dot or dot dot. */
792 if (ISDOT(dname) || ISDOTDOT(dname)) {
797 * At this point dname is the unmangled name.
798 * name is either mangled or not, depending on the state
799 * of the "mangled" variable. JRA.
803 * Check mangled name against mangled name, or unmangled name
804 * against unmangled name.
807 if ((mangled && mangled_equal(name,dname,conn->params)) ||
808 fname_equal(name, dname, conn->case_sensitive)) {
809 /* we've found the file, change it's name and return */
810 *found_name = talloc_strdup(ctx,dname);
811 TALLOC_FREE(unmangled_name);
812 TALLOC_FREE(cur_dir);
821 TALLOC_FREE(unmangled_name);
822 TALLOC_FREE(cur_dir);