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(!conn->case_sensitive
226 && stat_cache_lookup(conn, &name, &dirpath, &start, &st)) {
232 * Make sure "dirpath" is an allocated string, we use this for
233 * building the directories with asprintf and free it.
236 if ((dirpath == NULL) && (!(dirpath = talloc_strdup(ctx,"")))) {
237 DEBUG(0, ("talloc_strdup failed\n"));
239 return NT_STATUS_NO_MEMORY;
243 * stat the name - if it exists then we are all done!
246 if (SMB_VFS_STAT(conn,name,&st) == 0) {
247 /* Ensure we catch all names with in "/."
248 this is disallowed under Windows. */
249 const char *p = strstr(name, "/."); /* mb safe. */
252 /* Error code within a pathname. */
253 result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
255 } else if (p[2] == '\0') {
256 /* Error code at the end of a pathname. */
257 result = NT_STATUS_OBJECT_NAME_INVALID;
261 stat_cache_add(orig_path, name, conn->case_sensitive);
262 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
267 DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n",
268 name, dirpath, start));
271 * A special case - if we don't have any mangling chars and are case
272 * sensitive then searching won't help.
275 if (conn->case_sensitive &&
276 !mangle_is_mangled(name, conn->params)) {
281 * is_mangled() was changed to look at an entire pathname, not
282 * just a component. JRA.
285 if (mangle_is_mangled(start, conn->params)) {
286 component_was_mangled = True;
290 * Now we need to recursively match the name against the real
291 * directory structure.
295 * Match each part of the path name separately, trying the names
296 * as is first, then trying to scan the directory for matching names.
299 for (; start ; start = (end?end+1:(char *)NULL)) {
301 * Pinpoint the end of this section of the filename.
303 /* mb safe. '/' can't be in any encoded char. */
304 end = strchr(start, '/');
307 * Chop the name at this point.
313 if (pp_saved_last_component) {
314 TALLOC_FREE(*pp_saved_last_component);
315 *pp_saved_last_component = talloc_strdup(ctx,
316 end ? end + 1 : start);
317 if (!*pp_saved_last_component) {
318 DEBUG(0, ("talloc failed\n"));
319 return NT_STATUS_NO_MEMORY;
323 /* The name cannot have a component of "." */
327 /* Error code at the end of a pathname. */
328 result = NT_STATUS_OBJECT_NAME_INVALID;
330 result = determine_path_error(end+1,
331 allow_wcard_last_component);
336 /* The name cannot have a wildcard if it's not
337 the last component. */
339 name_has_wildcard = ms_has_wild(start);
341 /* Wildcard not valid anywhere. */
342 if (name_has_wildcard && !allow_wcard_last_component) {
343 result = NT_STATUS_OBJECT_NAME_INVALID;
347 /* Wildcards never valid within a pathname. */
348 if (name_has_wildcard && end) {
349 result = NT_STATUS_OBJECT_NAME_INVALID;
354 * Check if the name exists up to this point.
357 if (SMB_VFS_STAT(conn,name, &st) == 0) {
359 * It exists. it must either be a directory or this must
360 * be the last part of the path for it to be OK.
362 if (end && !(st.st_mode & S_IFDIR)) {
364 * An intermediate part of the name isn't
367 DEBUG(5,("Not a dir %s\n",start));
370 * We need to return the fact that the
371 * intermediate name resolution failed. This
372 * is used to return an error of ERRbadpath
373 * rather than ERRbadfile. Some Windows
374 * applications depend on the difference between
377 result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
383 * We just scanned for, and found the end of
384 * the path. We must return the valid stat
392 char *found_name = NULL;
394 /* Stat failed - ensure we don't use it. */
395 SET_STAT_INVALID(st);
398 * Reset errno so we can detect
399 * directory open errors.
404 * Try to find this part of the path in the directory.
407 if (name_has_wildcard ||
408 !scan_directory(conn, dirpath,
409 start, &found_name)) {
414 * An intermediate part of the name
417 DEBUG(5,("Intermediate not found %s\n",
422 * We need to return the fact that the
423 * intermediate name resolution failed.
424 * This is used to return an error of
425 * ERRbadpath rather than ERRbadfile.
426 * Some Windows applications depend on
427 * the difference between these two
432 * ENOENT, ENOTDIR and ELOOP all map
433 * to NT_STATUS_OBJECT_PATH_NOT_FOUND
434 * in the filename walk.
437 if (errno == ENOENT ||
441 NT_STATUS_OBJECT_PATH_NOT_FOUND;
445 map_nt_error_from_unix(errno);
450 /* ENOENT is the only valid error here. */
451 if (errno != ENOENT) {
453 * ENOTDIR and ELOOP both map to
454 * NT_STATUS_OBJECT_PATH_NOT_FOUND
455 * in the filename walk.
457 if (errno == ENOTDIR ||
460 NT_STATUS_OBJECT_PATH_NOT_FOUND;
464 map_nt_error_from_unix(errno);
470 * Just the last part of the name doesn't exist.
471 * We need to strupper() or strlower() it as
472 * this conversion may be used for file creation
473 * purposes. Fix inspired by
474 * Thomas Neumann <t.neumann@iku-ag.de>.
476 if (!conn->case_preserve ||
477 (mangle_is_8_3(start, False,
479 !conn->short_case_preserve)) {
481 lp_defaultcase(SNUM(conn)));
485 * check on the mangled stack to see if we can
486 * recover the base of the filename.
489 if (mangle_is_mangled(start, conn->params)
490 && mangle_lookup_name_from_8_3(ctx,
495 size_t start_ofs = start - name;
497 if (*dirpath != '\0') {
498 tmp = talloc_asprintf(ctx,
501 TALLOC_FREE(unmangled);
507 DEBUG(0, ("talloc failed\n"));
508 return NT_STATUS_NO_MEMORY;
512 start = name + start_ofs;
513 end = start + strlen(start);
516 DEBUG(5,("New file %s\n",start));
522 * Restore the rest of the string. If the string was
523 * mangled the size may have changed.
527 size_t start_ofs = start - name;
529 if (*dirpath != '\0') {
530 tmp = talloc_asprintf(ctx,
535 tmp = talloc_asprintf(ctx,
540 DEBUG(0, ("talloc_asprintf failed\n"));
541 return NT_STATUS_NO_MEMORY;
545 start = name + start_ofs;
546 end = start + strlen(found_name);
550 size_t start_ofs = start - name;
552 if (*dirpath != '\0') {
553 tmp = talloc_asprintf(ctx,
557 tmp = talloc_strdup(ctx,
561 DEBUG(0, ("talloc failed\n"));
562 return NT_STATUS_NO_MEMORY;
566 start = name + start_ofs;
569 * We just scanned for, and found the end of
570 * the path. We must return a valid stat struct
574 if (SMB_VFS_STAT(conn,name, &st) == 0) {
577 SET_STAT_INVALID(st);
581 TALLOC_FREE(found_name);
585 if (VALID_STAT(st) &&
586 get_delete_on_close_flag(vfs_file_id_from_sbuf(conn,
588 result = NT_STATUS_DELETE_PENDING;
594 * Add to the dirpath that we have resolved so far.
597 if (*dirpath != '\0') {
598 char *tmp = talloc_asprintf(ctx,
599 "%s/%s", dirpath, start);
601 DEBUG(0, ("talloc_asprintf failed\n"));
602 return NT_STATUS_NO_MEMORY;
604 TALLOC_FREE(dirpath);
608 TALLOC_FREE(dirpath);
609 if (!(dirpath = talloc_strdup(ctx,start))) {
610 DEBUG(0, ("talloc_strdup failed\n"));
611 return NT_STATUS_NO_MEMORY;
616 * Don't cache a name with mangled or wildcard components
617 * as this can change the size.
620 if(!component_was_mangled && !name_has_wildcard) {
621 stat_cache_add(orig_path, dirpath,
622 conn->case_sensitive);
626 * Restore the / that we wiped out earlier.
634 * Don't cache a name with mangled or wildcard components
635 * as this can change the size.
638 if(!component_was_mangled && !name_has_wildcard) {
639 stat_cache_add(orig_path, name, conn->case_sensitive);
643 * The name has been resolved.
646 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
649 *pp_conv_path = name;
650 TALLOC_FREE(dirpath);
653 DEBUG(10, ("dirpath = [%s] start = [%s]\n", dirpath, start));
654 if (*dirpath != '\0') {
655 *pp_conv_path = talloc_asprintf(ctx,
656 "%s/%s", dirpath, start);
658 *pp_conv_path = talloc_strdup(ctx, start);
660 if (!*pp_conv_path) {
661 DEBUG(0, ("talloc_asprintf failed\n"));
662 return NT_STATUS_NO_MEMORY;
665 TALLOC_FREE(dirpath);
669 /****************************************************************************
670 Check a filename - possibly calling check_reduced_name.
671 This is called by every routine before it allows an operation on a filename.
672 It does any final confirmation necessary to ensure that the filename is
673 a valid one for the user to access.
674 ****************************************************************************/
676 NTSTATUS check_name(connection_struct *conn, const char *name)
678 if (IS_VETO_PATH(conn, name)) {
679 /* Is it not dot or dot dot. */
680 if (!((name[0] == '.') && (!name[1] ||
681 (name[1] == '.' && !name[2])))) {
682 DEBUG(5,("check_name: file path name %s vetoed\n",
684 return map_nt_error_from_unix(ENOENT);
688 if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
689 NTSTATUS status = check_reduced_name(conn,name);
690 if (!NT_STATUS_IS_OK(status)) {
691 DEBUG(5,("check_name: name %s failed with %s\n",name,
700 /****************************************************************************
701 Check if two filenames are equal.
702 This needs to be careful about whether we are case sensitive.
703 ****************************************************************************/
705 static bool fname_equal(const char *name1, const char *name2,
708 /* Normal filename handling */
709 if (case_sensitive) {
710 return(strcmp(name1,name2) == 0);
713 return(strequal(name1,name2));
716 /****************************************************************************
717 Scan a directory to find a filename, matching without case sensitivity.
718 If the name looks like a mangled name then try via the mangling functions
719 ****************************************************************************/
721 static bool scan_directory(connection_struct *conn, const char *path,
722 char *name, char **found_name)
724 struct smb_Dir *cur_dir;
727 char *unmangled_name = NULL;
729 TALLOC_CTX *ctx = talloc_tos();
731 mangled = mangle_is_mangled(name, conn->params);
733 /* handle null paths */
734 if ((path == NULL) || (*path == 0)) {
739 * The incoming name can be mangled, and if we de-mangle it
740 * here it will not compare correctly against the filename (name2)
741 * read from the directory and then mangled by the name_to_8_3()
742 * call. We need to mangle both names or neither.
745 * Fix for bug found by Dina Fine. If in case sensitive mode then
746 * the mangle cache is no good (3 letter extension could be wrong
747 * case - so don't demangle in this case - leave as mangled and
748 * allow the mangling of the directory entry read (which is done
749 * case insensitively) to match instead. This will lead to more
750 * false positive matches but we fail completely without it. JRA.
753 if (mangled && !conn->case_sensitive) {
754 mangled = !mangle_lookup_name_from_8_3(ctx,
759 /* Name is now unmangled. */
760 name = unmangled_name;
764 /* open the directory */
765 if (!(cur_dir = OpenDir(conn, path, NULL, 0))) {
766 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
767 TALLOC_FREE(unmangled_name);
771 /* now scan for matching names */
773 while ((dname = ReadDirName(cur_dir, &curpos))) {
775 /* Is it dot or dot dot. */
776 if (ISDOT(dname) || ISDOTDOT(dname)) {
781 * At this point dname is the unmangled name.
782 * name is either mangled or not, depending on the state
783 * of the "mangled" variable. JRA.
787 * Check mangled name against mangled name, or unmangled name
788 * against unmangled name.
791 if ((mangled && mangled_equal(name,dname,conn->params)) ||
792 fname_equal(name, dname, conn->case_sensitive)) {
793 /* we've found the file, change it's name and return */
794 *found_name = talloc_strdup(ctx,dname);
795 TALLOC_FREE(unmangled_name);
805 TALLOC_FREE(unmangled_name);