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.
28 #include "system/filesys.h"
29 #include "fake_file.h"
30 #include "smbd/smbd.h"
31 #include "smbd/globals.h"
33 static NTSTATUS
build_stream_path(TALLOC_CTX
*mem_ctx
,
34 connection_struct
*conn
,
35 struct smb_filename
*smb_fname
);
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 bool name_has_wild
= false;
64 if (!allow_wcard_last_component
) {
65 /* Error code within a pathname. */
66 return NT_STATUS_OBJECT_PATH_NOT_FOUND
;
69 /* We're terminating here so we
70 * can be a little slower and get
71 * the error code right. Windows
72 * treats the last part of the pathname
73 * separately I think, so if the last
74 * component is a wildcard then we treat
75 * this ./ as "end of component" */
77 p
= strchr(name
, '/');
79 if (!posix_pathnames
) {
80 name_has_wild
= ms_has_wild(name
);
83 if (!p
&& (name_has_wild
|| ISDOT(name
))) {
84 /* Error code at the end of a pathname. */
85 return NT_STATUS_OBJECT_NAME_INVALID
;
87 /* Error code within a pathname. */
88 return NT_STATUS_OBJECT_PATH_NOT_FOUND
;
92 static NTSTATUS
check_for_dot_component(const struct smb_filename
*smb_fname
)
94 /* Ensure we catch all names with in "/."
95 this is disallowed under Windows and
96 in POSIX they've already been removed. */
97 const char *p
= strstr(smb_fname
->base_name
, "/."); /*mb safe*/
100 /* Error code within a pathname. */
101 return NT_STATUS_OBJECT_PATH_NOT_FOUND
;
102 } else if (p
[2] == '\0') {
103 /* Error code at the end of a pathname. */
104 return NT_STATUS_OBJECT_NAME_INVALID
;
110 /****************************************************************************
111 Optimization for common case where the missing part
112 is in the last component and the client already
113 sent the correct case.
114 Returns NT_STATUS_OK to mean continue the tree walk
115 (possibly with modified start pointer).
116 Any other NT_STATUS_XXX error means terminate the path
118 ****************************************************************************/
120 static NTSTATUS
check_parent_exists(TALLOC_CTX
*ctx
,
121 connection_struct
*conn
,
122 bool posix_pathnames
,
123 const struct smb_filename
*smb_fname
,
127 struct smb_filename parent_fname
;
128 const char *last_component
= NULL
;
131 bool parent_fname_has_wild
= false;
133 ZERO_STRUCT(parent_fname
);
134 if (!parent_dirname(ctx
, smb_fname
->base_name
,
135 &parent_fname
.base_name
,
137 return NT_STATUS_NO_MEMORY
;
140 if (!posix_pathnames
) {
141 parent_fname_has_wild
= ms_has_wild(parent_fname
.base_name
);
145 * If there was no parent component in
146 * smb_fname->base_name of the parent name
147 * contained a wildcard then don't do this
150 if ((smb_fname
->base_name
== last_component
) ||
151 parent_fname_has_wild
) {
155 if (posix_pathnames
) {
156 ret
= SMB_VFS_LSTAT(conn
, &parent_fname
);
158 ret
= SMB_VFS_STAT(conn
, &parent_fname
);
161 /* If the parent stat failed, just continue
162 with the normal tree walk. */
168 status
= check_for_dot_component(&parent_fname
);
169 if (!NT_STATUS_IS_OK(status
)) {
173 /* Parent exists - set "start" to be the
174 * last component to shorten the tree walk. */
177 * Safe to use discard_const_p
178 * here as last_component points
179 * into our smb_fname->base_name.
181 *pp_start
= discard_const_p(char, last_component
);
183 /* Update dirpath. */
184 TALLOC_FREE(*pp_dirpath
);
185 *pp_dirpath
= talloc_strdup(ctx
, parent_fname
.base_name
);
187 return NT_STATUS_NO_MEMORY
;
190 DEBUG(5,("check_parent_exists: name "
191 "= %s, dirpath = %s, "
193 smb_fname
->base_name
,
200 /****************************************************************************
201 This routine is called to convert names from the dos namespace to unix
202 namespace. It needs to handle any case conversions, mangling, format changes,
205 We assume that we have already done a chdir() to the right "root" directory
208 The function will return an NTSTATUS error if some part of the name except for
209 the last part cannot be resolved, else NT_STATUS_OK.
211 Note NT_STATUS_OK doesn't mean the name exists or is valid, just that we
212 didn't get any fatal errors that should immediately terminate the calling SMB
213 processing whilst resolving.
215 If the UCF_SAVE_LCOMP flag is passed in, then the unmodified last component
216 of the pathname is set in smb_filename->original_lcomp.
218 If UCF_ALWAYS_ALLOW_WCARD_LCOMP is passed in, then a MS wildcard was detected
219 and should be allowed in the last component of the path only.
221 If the orig_path was a stream, smb_filename->base_name will point to the base
222 filename, and smb_filename->stream_name will point to the stream name. If
223 orig_path was not a stream, then smb_filename->stream_name will be NULL.
225 On exit from unix_convert, the smb_filename->st stat struct will be populated
226 if the file exists and was found, if not this stat struct will be filled with
227 zeros (and this can be detected by checking for nlinks = 0, which can never be
229 ****************************************************************************/
231 NTSTATUS
unix_convert(TALLOC_CTX
*ctx
,
232 connection_struct
*conn
,
233 const char *orig_path
,
234 struct smb_filename
**smb_fname_out
,
237 struct smb_filename
*smb_fname
= NULL
;
239 char *dirpath
= NULL
;
241 bool component_was_mangled
= False
;
242 bool name_has_wildcard
= False
;
243 bool posix_pathnames
= (lp_posix_pathnames() ||
244 (ucf_flags
& UCF_POSIX_PATHNAMES
));
245 bool allow_wcard_last_component
=
246 (ucf_flags
& UCF_ALWAYS_ALLOW_WCARD_LCOMP
);
247 bool save_last_component
= ucf_flags
& UCF_SAVE_LCOMP
;
251 *smb_fname_out
= NULL
;
253 smb_fname
= talloc_zero(ctx
, struct smb_filename
);
254 if (smb_fname
== NULL
) {
255 return NT_STATUS_NO_MEMORY
;
259 /* we don't ever use the filenames on a printer share as a
260 filename - so don't convert them */
261 if (!(smb_fname
->base_name
= talloc_strdup(smb_fname
,
263 status
= NT_STATUS_NO_MEMORY
;
269 DEBUG(5, ("unix_convert called on file \"%s\"\n", orig_path
));
272 * Conversion to basic unix format is already done in
273 * check_path_syntax().
277 * Names must be relative to the root of the service - any leading /.
278 * and trailing /'s should have been trimmed by check_path_syntax().
282 SMB_ASSERT(*orig_path
!= '/');
286 * If we trimmed down to a single '\0' character
287 * then we should use the "." directory to avoid
288 * searching the cache, but not if we are in a
290 * As we know this is valid we can return true here.
294 if (!(smb_fname
->base_name
= talloc_strdup(smb_fname
, "."))) {
295 status
= NT_STATUS_NO_MEMORY
;
298 if (SMB_VFS_STAT(conn
, smb_fname
) != 0) {
299 status
= map_nt_error_from_unix(errno
);
302 DEBUG(5, ("conversion finished \"\" -> %s\n",
303 smb_fname
->base_name
));
307 if (orig_path
[0] == '.' && (orig_path
[1] == '/' ||
308 orig_path
[1] == '\0')) {
309 /* Start of pathname can't be "." only. */
310 if (orig_path
[1] == '\0' || orig_path
[2] == '\0') {
311 status
= NT_STATUS_OBJECT_NAME_INVALID
;
313 status
=determine_path_error(&orig_path
[2],
314 allow_wcard_last_component
,
320 /* Start with the full orig_path as given by the caller. */
321 if (!(smb_fname
->base_name
= talloc_strdup(smb_fname
, orig_path
))) {
322 DEBUG(0, ("talloc_strdup failed\n"));
323 status
= NT_STATUS_NO_MEMORY
;
328 * Large directory fix normalization. If we're case sensitive, and
329 * the case preserving parameters are set to "no", normalize the case of
330 * the incoming filename from the client WHETHER IT EXISTS OR NOT !
331 * This is in conflict with the current (3.0.20) man page, but is
332 * what people expect from the "large directory howto". I'll update
333 * the man page. Thanks to jht@samba.org for finding this. JRA.
336 if (conn
->case_sensitive
&& !conn
->case_preserve
&&
337 !conn
->short_case_preserve
) {
338 if (!strnorm(smb_fname
->base_name
, lp_default_case(SNUM(conn
)))) {
339 DEBUG(0, ("strnorm %s failed\n", smb_fname
->base_name
));
340 status
= NT_STATUS_INVALID_PARAMETER
;
346 * Ensure saved_last_component is valid even if file exists.
349 if(save_last_component
) {
350 end
= strrchr_m(smb_fname
->base_name
, '/');
352 smb_fname
->original_lcomp
= talloc_strdup(smb_fname
,
355 smb_fname
->original_lcomp
=
356 talloc_strdup(smb_fname
, smb_fname
->base_name
);
358 if (smb_fname
->original_lcomp
== NULL
) {
359 status
= NT_STATUS_NO_MEMORY
;
365 * Strip off the stream, and add it back when we're done with the
368 if (!posix_pathnames
) {
369 stream
= strchr_m(smb_fname
->base_name
, ':');
371 if (stream
!= NULL
) {
372 char *tmp
= talloc_strdup(smb_fname
, stream
);
374 status
= NT_STATUS_NO_MEMORY
;
378 * Since this is actually pointing into
379 * smb_fname->base_name this truncates base_name.
384 if (smb_fname
->base_name
[0] == '\0') {
386 * orig_name was just a stream name.
387 * This is a stream on the root of
388 * the share. Replace base_name with
391 smb_fname
->base_name
=
392 talloc_strdup(smb_fname
, ".");
393 if (smb_fname
->base_name
== NULL
) {
394 status
= NT_STATUS_NO_MEMORY
;
397 if (SMB_VFS_STAT(conn
, smb_fname
) != 0) {
398 status
= map_nt_error_from_unix(errno
);
401 /* dirpath must exist. */
402 dirpath
= talloc_strdup(ctx
,"");
403 if (dirpath
== NULL
) {
404 status
= NT_STATUS_NO_MEMORY
;
407 DEBUG(5, ("conversion finished %s -> %s\n",
409 smb_fname
->base_name
));
415 start
= smb_fname
->base_name
;
418 * If we're providing case insensitive semantics or
419 * the underlying filesystem is case insensitive,
420 * then a case-normalized hit in the stat-cache is
421 * authoratitive. JRA.
423 * Note: We're only checking base_name. The stream_name will be
424 * added and verified in build_stream_path().
427 if((!conn
->case_sensitive
|| !(conn
->fs_capabilities
&
428 FILE_CASE_SENSITIVE_SEARCH
)) &&
429 stat_cache_lookup(conn
, posix_pathnames
, &smb_fname
->base_name
, &dirpath
, &start
,
435 * Make sure "dirpath" is an allocated string, we use this for
436 * building the directories with talloc_asprintf and free it.
439 if ((dirpath
== NULL
) && (!(dirpath
= talloc_strdup(ctx
,"")))) {
440 DEBUG(0, ("talloc_strdup failed\n"));
441 status
= NT_STATUS_NO_MEMORY
;
446 * If we have a wildcard we must walk the path to
447 * find where the error is, even if case sensitive
451 if (!posix_pathnames
) {
452 /* POSIX pathnames have no wildcards. */
453 name_has_wildcard
= ms_has_wild(smb_fname
->base_name
);
454 if (name_has_wildcard
&& !allow_wcard_last_component
) {
455 /* Wildcard not valid anywhere. */
456 status
= NT_STATUS_OBJECT_NAME_INVALID
;
461 DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n",
462 smb_fname
->base_name
, dirpath
, start
));
464 if (!name_has_wildcard
) {
466 * stat the name - if it exists then we can add the stream back (if
467 * there was one) and be done!
470 if (posix_pathnames
) {
471 ret
= SMB_VFS_LSTAT(conn
, smb_fname
);
473 ret
= SMB_VFS_STAT(conn
, smb_fname
);
477 status
= check_for_dot_component(smb_fname
);
478 if (!NT_STATUS_IS_OK(status
)) {
481 /* Add the path (not including the stream) to the cache. */
482 stat_cache_add(orig_path
, smb_fname
->base_name
,
483 conn
->case_sensitive
);
484 DEBUG(5,("conversion of base_name finished %s -> %s\n",
485 orig_path
, smb_fname
->base_name
));
489 /* Stat failed - ensure we don't use it. */
490 SET_STAT_INVALID(smb_fname
->st
);
492 if (errno
== ENOENT
) {
493 /* Optimization when creating a new file - only
494 the last component doesn't exist.
495 NOTE : check_parent_exists() doesn't preserve errno.
497 int saved_errno
= errno
;
498 status
= check_parent_exists(ctx
,
505 if (!NT_STATUS_IS_OK(status
)) {
511 * A special case - if we don't have any wildcards or mangling chars and are case
512 * sensitive or the underlying filesystem is case insensitive then searching
516 if ((conn
->case_sensitive
|| !(conn
->fs_capabilities
&
517 FILE_CASE_SENSITIVE_SEARCH
)) &&
518 !mangle_is_mangled(smb_fname
->base_name
, conn
->params
)) {
520 status
= check_for_dot_component(smb_fname
);
521 if (!NT_STATUS_IS_OK(status
)) {
526 * The stat failed. Could be ok as it could be
530 if (errno
== ENOTDIR
|| errno
== ELOOP
) {
531 status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
533 } else if (errno
== ENOENT
) {
535 * Was it a missing last component ?
536 * or a missing intermediate component ?
538 struct smb_filename parent_fname
;
539 const char *last_component
= NULL
;
541 ZERO_STRUCT(parent_fname
);
542 if (!parent_dirname(ctx
, smb_fname
->base_name
,
543 &parent_fname
.base_name
,
545 status
= NT_STATUS_NO_MEMORY
;
548 if (posix_pathnames
) {
549 ret
= SMB_VFS_LSTAT(conn
, &parent_fname
);
551 ret
= SMB_VFS_STAT(conn
, &parent_fname
);
554 if (errno
== ENOTDIR
||
557 status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
563 * Missing last component is ok - new file.
564 * Also deal with permission denied elsewhere.
565 * Just drop out to done.
572 * We have a wildcard in the pathname.
574 * Optimization for common case where the wildcard
575 * is in the last component and the client already
576 * sent the correct case.
577 * NOTE : check_parent_exists() doesn't preserve errno.
579 int saved_errno
= errno
;
580 status
= check_parent_exists(ctx
,
587 if (!NT_STATUS_IS_OK(status
)) {
593 * is_mangled() was changed to look at an entire pathname, not
594 * just a component. JRA.
597 if (mangle_is_mangled(start
, conn
->params
)) {
598 component_was_mangled
= True
;
602 * Now we need to recursively match the name against the real
603 * directory structure.
607 * Match each part of the path name separately, trying the names
608 * as is first, then trying to scan the directory for matching names.
611 for (; start
; start
= (end
?end
+1:(char *)NULL
)) {
613 * Pinpoint the end of this section of the filename.
615 /* mb safe. '/' can't be in any encoded char. */
616 end
= strchr(start
, '/');
619 * Chop the name at this point.
625 if (save_last_component
) {
626 TALLOC_FREE(smb_fname
->original_lcomp
);
627 smb_fname
->original_lcomp
= talloc_strdup(smb_fname
,
628 end
? end
+ 1 : start
);
629 if (!smb_fname
->original_lcomp
) {
630 DEBUG(0, ("talloc failed\n"));
631 status
= NT_STATUS_NO_MEMORY
;
636 /* The name cannot have a component of "." */
640 /* Error code at the end of a pathname. */
641 status
= NT_STATUS_OBJECT_NAME_INVALID
;
643 status
= determine_path_error(end
+1,
644 allow_wcard_last_component
,
650 /* The name cannot have a wildcard if it's not
651 the last component. */
653 if (!posix_pathnames
) {
654 name_has_wildcard
= ms_has_wild(start
);
657 /* Wildcards never valid within a pathname. */
658 if (name_has_wildcard
&& end
) {
659 status
= NT_STATUS_OBJECT_NAME_INVALID
;
663 /* Skip the stat call if it's a wildcard end. */
664 if (name_has_wildcard
) {
665 DEBUG(5,("Wildcard %s\n",start
));
670 * Check if the name exists up to this point.
673 if (posix_pathnames
) {
674 ret
= SMB_VFS_LSTAT(conn
, smb_fname
);
676 ret
= SMB_VFS_STAT(conn
, smb_fname
);
681 * It exists. it must either be a directory or this must
682 * be the last part of the path for it to be OK.
684 if (end
&& !S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
686 * An intermediate part of the name isn't
689 DEBUG(5,("Not a dir %s\n",start
));
692 * We need to return the fact that the
693 * intermediate name resolution failed. This
694 * is used to return an error of ERRbadpath
695 * rather than ERRbadfile. Some Windows
696 * applications depend on the difference between
699 status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
704 char *found_name
= NULL
;
706 /* Stat failed - ensure we don't use it. */
707 SET_STAT_INVALID(smb_fname
->st
);
710 * Reset errno so we can detect
711 * directory open errors.
716 * Try to find this part of the path in the directory.
719 if (name_has_wildcard
||
720 (get_real_filename(conn
, dirpath
, start
,
722 &found_name
) == -1)) {
727 * An intermediate part of the name
730 DEBUG(5,("Intermediate not found %s\n",
735 * We need to return the fact that the
736 * intermediate name resolution failed.
737 * This is used to return an error of
738 * ERRbadpath rather than ERRbadfile.
739 * Some Windows applications depend on
740 * the difference between these two
745 * ENOENT, ENOTDIR and ELOOP all map
746 * to NT_STATUS_OBJECT_PATH_NOT_FOUND
747 * in the filename walk.
750 if (errno
== ENOENT
||
754 NT_STATUS_OBJECT_PATH_NOT_FOUND
;
758 map_nt_error_from_unix(errno
);
764 * ENOENT/EACCESS are the only valid errors
768 if (errno
== EACCES
) {
769 if ((ucf_flags
& UCF_PREP_CREATEFILE
) == 0) {
770 status
= NT_STATUS_ACCESS_DENIED
;
774 * This is the dropbox
775 * behaviour. A dropbox is a
776 * directory with only -wx
778 * get_real_filename fails
779 * with EACCESS, it needs to
780 * list the directory. We
781 * nevertheless want to allow
782 * users creating a file.
788 if ((errno
!= 0) && (errno
!= ENOENT
)) {
790 * ENOTDIR and ELOOP both map to
791 * NT_STATUS_OBJECT_PATH_NOT_FOUND
792 * in the filename walk.
794 if (errno
== ENOTDIR
||
797 NT_STATUS_OBJECT_PATH_NOT_FOUND
;
800 map_nt_error_from_unix(errno
);
806 * Just the last part of the name doesn't exist.
807 * We need to strupper() or strlower() it as
808 * this conversion may be used for file creation
809 * purposes. Fix inspired by
810 * Thomas Neumann <t.neumann@iku-ag.de>.
812 if (!conn
->case_preserve
||
813 (mangle_is_8_3(start
, False
,
815 !conn
->short_case_preserve
)) {
817 lp_default_case(SNUM(conn
)))) {
818 DEBUG(0, ("strnorm %s failed\n",
820 status
= NT_STATUS_INVALID_PARAMETER
;
826 * check on the mangled stack to see if we can
827 * recover the base of the filename.
830 if (mangle_is_mangled(start
, conn
->params
)
831 && mangle_lookup_name_from_8_3(ctx
,
837 start
- smb_fname
->base_name
;
839 if (*dirpath
!= '\0') {
840 tmp
= talloc_asprintf(
843 TALLOC_FREE(unmangled
);
849 DEBUG(0, ("talloc failed\n"));
850 status
= NT_STATUS_NO_MEMORY
;
853 TALLOC_FREE(smb_fname
->base_name
);
854 smb_fname
->base_name
= tmp
;
856 smb_fname
->base_name
+ start_ofs
;
857 end
= start
+ strlen(start
);
860 DEBUG(5,("New file %s\n",start
));
866 * Restore the rest of the string. If the string was
867 * mangled the size may have changed.
872 start
- smb_fname
->base_name
;
874 if (*dirpath
!= '\0') {
875 tmp
= talloc_asprintf(smb_fname
,
880 tmp
= talloc_asprintf(smb_fname
,
885 DEBUG(0, ("talloc_asprintf failed\n"));
886 status
= NT_STATUS_NO_MEMORY
;
889 TALLOC_FREE(smb_fname
->base_name
);
890 smb_fname
->base_name
= tmp
;
891 start
= smb_fname
->base_name
+ start_ofs
;
892 end
= start
+ strlen(found_name
);
897 start
- smb_fname
->base_name
;
899 if (*dirpath
!= '\0') {
900 tmp
= talloc_asprintf(smb_fname
,
904 tmp
= talloc_strdup(smb_fname
,
908 DEBUG(0, ("talloc failed\n"));
909 status
= NT_STATUS_NO_MEMORY
;
912 TALLOC_FREE(smb_fname
->base_name
);
913 smb_fname
->base_name
= tmp
;
914 start
= smb_fname
->base_name
+ start_ofs
;
917 * We just scanned for, and found the end of
918 * the path. We must return a valid stat struct
922 if (posix_pathnames
) {
923 ret
= SMB_VFS_LSTAT(conn
, smb_fname
);
925 ret
= SMB_VFS_STAT(conn
, smb_fname
);
929 SET_STAT_INVALID(smb_fname
->st
);
933 TALLOC_FREE(found_name
);
939 * We should never provide different behaviors
940 * depending on DEVELOPER!!!
942 if (VALID_STAT(smb_fname
->st
)) {
946 status
= file_name_hash(conn
,
947 smb_fname_str_dbg(smb_fname
),
949 if (!NT_STATUS_IS_OK(status
)) {
953 get_file_infos(vfs_file_id_from_sbuf(conn
,
956 &delete_pending
, NULL
);
957 if (delete_pending
) {
958 status
= NT_STATUS_DELETE_PENDING
;
965 * Add to the dirpath that we have resolved so far.
968 if (*dirpath
!= '\0') {
969 char *tmp
= talloc_asprintf(ctx
,
970 "%s/%s", dirpath
, start
);
972 DEBUG(0, ("talloc_asprintf failed\n"));
973 status
= NT_STATUS_NO_MEMORY
;
976 TALLOC_FREE(dirpath
);
980 TALLOC_FREE(dirpath
);
981 if (!(dirpath
= talloc_strdup(ctx
,start
))) {
982 DEBUG(0, ("talloc_strdup failed\n"));
983 status
= NT_STATUS_NO_MEMORY
;
989 * Cache the dirpath thus far. Don't cache a name with mangled
990 * or wildcard components as this can change the size.
992 if(!component_was_mangled
&& !name_has_wildcard
) {
993 stat_cache_add(orig_path
, dirpath
,
994 conn
->case_sensitive
);
998 * Restore the / that we wiped out earlier.
1006 * Cache the full path. Don't cache a name with mangled or wildcard
1007 * components as this can change the size.
1010 if(!component_was_mangled
&& !name_has_wildcard
) {
1011 stat_cache_add(orig_path
, smb_fname
->base_name
,
1012 conn
->case_sensitive
);
1016 * The name has been resolved.
1019 DEBUG(5,("conversion finished %s -> %s\n", orig_path
,
1020 smb_fname
->base_name
));
1023 /* Add back the stream if one was stripped off originally. */
1024 if (stream
!= NULL
) {
1025 smb_fname
->stream_name
= stream
;
1027 /* Check path now that the base_name has been converted. */
1028 status
= build_stream_path(ctx
, conn
, smb_fname
);
1029 if (!NT_STATUS_IS_OK(status
)) {
1033 TALLOC_FREE(dirpath
);
1034 *smb_fname_out
= smb_fname
;
1035 return NT_STATUS_OK
;
1037 DEBUG(10, ("dirpath = [%s] start = [%s]\n", dirpath
, start
));
1038 if (dirpath
&& *dirpath
!= '\0') {
1039 smb_fname
->base_name
= talloc_asprintf(smb_fname
, "%s/%s",
1042 smb_fname
->base_name
= talloc_strdup(smb_fname
, start
);
1044 if (!smb_fname
->base_name
) {
1045 DEBUG(0, ("talloc_asprintf failed\n"));
1046 status
= NT_STATUS_NO_MEMORY
;
1050 *smb_fname_out
= smb_fname
;
1051 TALLOC_FREE(dirpath
);
1054 TALLOC_FREE(smb_fname
);
1058 /****************************************************************************
1059 Ensure a path is not vetoed.
1060 ****************************************************************************/
1062 static NTSTATUS
check_veto_path(connection_struct
*conn
, const char *name
)
1064 if (IS_VETO_PATH(conn
, name
)) {
1065 /* Is it not dot or dot dot. */
1066 if (!(ISDOT(name
) || ISDOTDOT(name
))) {
1067 DEBUG(5,("check_veto_path: file path name %s vetoed\n",
1069 return map_nt_error_from_unix(ENOENT
);
1072 return NT_STATUS_OK
;
1075 /****************************************************************************
1076 Check a filename - possibly calling check_reduced_name.
1077 This is called by every routine before it allows an operation on a filename.
1078 It does any final confirmation necessary to ensure that the filename is
1079 a valid one for the user to access.
1080 ****************************************************************************/
1082 NTSTATUS
check_name(connection_struct
*conn
, const char *name
)
1084 NTSTATUS status
= check_veto_path(conn
, name
);
1086 if (!NT_STATUS_IS_OK(status
)) {
1090 if (!lp_widelinks(SNUM(conn
)) || !lp_follow_symlinks(SNUM(conn
))) {
1091 status
= check_reduced_name(conn
,name
);
1092 if (!NT_STATUS_IS_OK(status
)) {
1093 DEBUG(5,("check_name: name %s failed with %s\n",name
,
1094 nt_errstr(status
)));
1099 return NT_STATUS_OK
;
1102 /****************************************************************************
1103 Must be called as root. Creates the struct privilege_paths
1104 attached to the struct smb_request if this call is successful.
1105 ****************************************************************************/
1107 static NTSTATUS
check_name_with_privilege(connection_struct
*conn
,
1108 struct smb_request
*smbreq
,
1111 NTSTATUS status
= check_veto_path(conn
, name
);
1113 if (!NT_STATUS_IS_OK(status
)) {
1116 return check_reduced_name_with_privilege(conn
,
1121 /****************************************************************************
1122 Check if two filenames are equal.
1123 This needs to be careful about whether we are case sensitive.
1124 ****************************************************************************/
1126 static bool fname_equal(const char *name1
, const char *name2
,
1127 bool case_sensitive
)
1129 /* Normal filename handling */
1130 if (case_sensitive
) {
1131 return(strcmp(name1
,name2
) == 0);
1134 return(strequal(name1
,name2
));
1137 /****************************************************************************
1138 Scan a directory to find a filename, matching without case sensitivity.
1139 If the name looks like a mangled name then try via the mangling functions
1140 ****************************************************************************/
1142 static int get_real_filename_full_scan(connection_struct
*conn
,
1143 const char *path
, const char *name
,
1145 TALLOC_CTX
*mem_ctx
, char **found_name
)
1147 struct smb_Dir
*cur_dir
;
1148 const char *dname
= NULL
;
1149 char *talloced
= NULL
;
1150 char *unmangled_name
= NULL
;
1153 /* handle null paths */
1154 if ((path
== NULL
) || (*path
== 0)) {
1158 /* If we have a case-sensitive filesystem, it doesn't do us any
1159 * good to search for a name. If a case variation of the name was
1160 * there, then the original stat(2) would have found it.
1162 if (!mangled
&& !(conn
->fs_capabilities
& FILE_CASE_SENSITIVE_SEARCH
)) {
1168 * The incoming name can be mangled, and if we de-mangle it
1169 * here it will not compare correctly against the filename (name2)
1170 * read from the directory and then mangled by the name_to_8_3()
1171 * call. We need to mangle both names or neither.
1174 * Fix for bug found by Dina Fine. If in case sensitive mode then
1175 * the mangle cache is no good (3 letter extension could be wrong
1176 * case - so don't demangle in this case - leave as mangled and
1177 * allow the mangling of the directory entry read (which is done
1178 * case insensitively) to match instead. This will lead to more
1179 * false positive matches but we fail completely without it. JRA.
1182 if (mangled
&& !conn
->case_sensitive
) {
1183 mangled
= !mangle_lookup_name_from_8_3(talloc_tos(), name
,
1187 /* Name is now unmangled. */
1188 name
= unmangled_name
;
1192 /* open the directory */
1193 if (!(cur_dir
= OpenDir(talloc_tos(), conn
, path
, NULL
, 0))) {
1194 DEBUG(3,("scan dir didn't open dir [%s]\n",path
));
1195 TALLOC_FREE(unmangled_name
);
1199 /* now scan for matching names */
1201 while ((dname
= ReadDirName(cur_dir
, &curpos
, NULL
, &talloced
))) {
1203 /* Is it dot or dot dot. */
1204 if (ISDOT(dname
) || ISDOTDOT(dname
)) {
1205 TALLOC_FREE(talloced
);
1210 * At this point dname is the unmangled name.
1211 * name is either mangled or not, depending on the state
1212 * of the "mangled" variable. JRA.
1216 * Check mangled name against mangled name, or unmangled name
1217 * against unmangled name.
1220 if ((mangled
&& mangled_equal(name
,dname
,conn
->params
)) ||
1221 fname_equal(name
, dname
, conn
->case_sensitive
)) {
1222 /* we've found the file, change it's name and return */
1223 *found_name
= talloc_strdup(mem_ctx
, dname
);
1224 TALLOC_FREE(unmangled_name
);
1225 TALLOC_FREE(cur_dir
);
1228 TALLOC_FREE(talloced
);
1231 TALLOC_FREE(talloced
);
1234 TALLOC_FREE(talloced
);
1237 TALLOC_FREE(unmangled_name
);
1238 TALLOC_FREE(cur_dir
);
1243 /****************************************************************************
1244 Wrapper around the vfs get_real_filename and the full directory scan
1246 ****************************************************************************/
1248 int get_real_filename(connection_struct
*conn
, const char *path
,
1249 const char *name
, TALLOC_CTX
*mem_ctx
,
1255 mangled
= mangle_is_mangled(name
, conn
->params
);
1258 return get_real_filename_full_scan(conn
, path
, name
, mangled
,
1259 mem_ctx
, found_name
);
1262 /* Try the vfs first to take advantage of case-insensitive stat. */
1263 ret
= SMB_VFS_GET_REAL_FILENAME(conn
, path
, name
, mem_ctx
, found_name
);
1266 * If the case-insensitive stat was successful, or returned an error
1267 * other than EOPNOTSUPP then there is no need to fall back on the
1268 * full directory scan.
1270 if (ret
== 0 || (ret
== -1 && errno
!= EOPNOTSUPP
)) {
1274 return get_real_filename_full_scan(conn
, path
, name
, mangled
, mem_ctx
,
1278 static NTSTATUS
build_stream_path(TALLOC_CTX
*mem_ctx
,
1279 connection_struct
*conn
,
1280 struct smb_filename
*smb_fname
)
1283 unsigned int i
, num_streams
= 0;
1284 struct stream_struct
*streams
= NULL
;
1286 if (SMB_VFS_STAT(conn
, smb_fname
) == 0) {
1287 DEBUG(10, ("'%s' exists\n", smb_fname_str_dbg(smb_fname
)));
1288 return NT_STATUS_OK
;
1291 if (errno
!= ENOENT
) {
1292 DEBUG(10, ("vfs_stat failed: %s\n", strerror(errno
)));
1293 status
= map_nt_error_from_unix(errno
);
1297 /* Fall back to a case-insensitive scan of all streams on the file. */
1298 status
= vfs_streaminfo(conn
, NULL
, smb_fname
->base_name
, mem_ctx
,
1299 &num_streams
, &streams
);
1301 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
1302 SET_STAT_INVALID(smb_fname
->st
);
1303 return NT_STATUS_OK
;
1306 if (!NT_STATUS_IS_OK(status
)) {
1307 DEBUG(10, ("vfs_streaminfo failed: %s\n", nt_errstr(status
)));
1311 for (i
=0; i
<num_streams
; i
++) {
1312 DEBUG(10, ("comparing [%s] and [%s]: ",
1313 smb_fname
->stream_name
, streams
[i
].name
));
1314 if (fname_equal(smb_fname
->stream_name
, streams
[i
].name
,
1315 conn
->case_sensitive
)) {
1316 DEBUGADD(10, ("equal\n"));
1319 DEBUGADD(10, ("not equal\n"));
1322 /* Couldn't find the stream. */
1323 if (i
== num_streams
) {
1324 SET_STAT_INVALID(smb_fname
->st
);
1325 TALLOC_FREE(streams
);
1326 return NT_STATUS_OK
;
1329 DEBUG(10, ("case insensitive stream. requested: %s, actual: %s\n",
1330 smb_fname
->stream_name
, streams
[i
].name
));
1333 TALLOC_FREE(smb_fname
->stream_name
);
1334 smb_fname
->stream_name
= talloc_strdup(smb_fname
, streams
[i
].name
);
1335 if (smb_fname
->stream_name
== NULL
) {
1336 status
= NT_STATUS_NO_MEMORY
;
1340 SET_STAT_INVALID(smb_fname
->st
);
1342 if (SMB_VFS_STAT(conn
, smb_fname
) == 0) {
1343 DEBUG(10, ("'%s' exists\n", smb_fname_str_dbg(smb_fname
)));
1345 status
= NT_STATUS_OK
;
1347 TALLOC_FREE(streams
);
1352 * Go through all the steps to validate a filename.
1354 * @param ctx talloc_ctx to allocate memory with.
1355 * @param conn connection struct for vfs calls.
1356 * @param dfs_path Whether this path requires dfs resolution.
1357 * @param smbreq SMB request if we're using privileges.
1358 * @param name_in The unconverted name.
1359 * @param ucf_flags flags to pass through to unix_convert().
1360 * UCF_ALWAYS_ALLOW_WCARD_LCOMP will be OR'd in if
1361 * p_cont_wcard != NULL and is true and
1362 * UCF_COND_ALLOW_WCARD_LCOMP.
1363 * @param p_cont_wcard If not NULL, will be set to true if the dfs path
1364 * resolution detects a wildcard.
1365 * @param pp_smb_fname The final converted name will be allocated if the
1366 * return is NT_STATUS_OK.
1368 * @return NT_STATUS_OK if all operations completed succesfully, appropriate
1371 static NTSTATUS
filename_convert_internal(TALLOC_CTX
*ctx
,
1372 connection_struct
*conn
,
1374 struct smb_request
*smbreq
,
1375 const char *name_in
,
1377 bool *ppath_contains_wcard
,
1378 struct smb_filename
**pp_smb_fname
)
1381 bool allow_wcards
= (ucf_flags
& (UCF_COND_ALLOW_WCARD_LCOMP
|UCF_ALWAYS_ALLOW_WCARD_LCOMP
));
1384 *pp_smb_fname
= NULL
;
1386 status
= resolve_dfspath_wcard(ctx
, conn
,
1390 !conn
->sconn
->using_smb2
,
1392 ppath_contains_wcard
);
1393 if (!NT_STATUS_IS_OK(status
)) {
1394 DEBUG(10,("filename_convert_internal: resolve_dfspath failed "
1395 "for name %s with %s\n",
1397 nt_errstr(status
) ));
1401 if (is_fake_file_path(name_in
)) {
1405 *pp_smb_fname
= synthetic_smb_fname_split(ctx
,
1408 if (*pp_smb_fname
== NULL
) {
1409 return NT_STATUS_NO_MEMORY
;
1411 return NT_STATUS_OK
;
1415 * If the caller conditionally allows wildcard lookups, only add the
1416 * always allow if the path actually does contain a wildcard.
1418 if (ucf_flags
& UCF_COND_ALLOW_WCARD_LCOMP
&&
1419 ppath_contains_wcard
!= NULL
&& *ppath_contains_wcard
) {
1420 ucf_flags
|= UCF_ALWAYS_ALLOW_WCARD_LCOMP
;
1423 status
= unix_convert(ctx
, conn
, fname
, pp_smb_fname
, ucf_flags
);
1424 if (!NT_STATUS_IS_OK(status
)) {
1425 DEBUG(10,("filename_convert_internal: unix_convert failed "
1426 "for name %s with %s\n",
1428 nt_errstr(status
) ));
1432 if ((ucf_flags
& UCF_UNIX_NAME_LOOKUP
) &&
1433 VALID_STAT((*pp_smb_fname
)->st
) &&
1434 S_ISLNK((*pp_smb_fname
)->st
.st_ex_mode
)) {
1435 return check_veto_path(conn
, (*pp_smb_fname
)->base_name
);
1439 status
= check_name(conn
, (*pp_smb_fname
)->base_name
);
1441 status
= check_name_with_privilege(conn
, smbreq
, (*pp_smb_fname
)->base_name
);
1443 if (!NT_STATUS_IS_OK(status
)) {
1444 DEBUG(3,("filename_convert_internal: check_name failed "
1445 "for name %s with %s\n",
1446 smb_fname_str_dbg(*pp_smb_fname
),
1447 nt_errstr(status
) ));
1448 TALLOC_FREE(*pp_smb_fname
);
1456 * Go through all the steps to validate a filename.
1460 NTSTATUS
filename_convert(TALLOC_CTX
*ctx
,
1461 connection_struct
*conn
,
1463 const char *name_in
,
1465 bool *ppath_contains_wcard
,
1466 struct smb_filename
**pp_smb_fname
)
1468 return filename_convert_internal(ctx
,
1474 ppath_contains_wcard
,
1479 * Go through all the steps to validate a filename.
1480 * root (privileged) version.
1483 NTSTATUS
filename_convert_with_privilege(TALLOC_CTX
*ctx
,
1484 connection_struct
*conn
,
1485 struct smb_request
*smbreq
,
1486 const char *name_in
,
1488 bool *ppath_contains_wcard
,
1489 struct smb_filename
**pp_smb_fname
)
1491 return filename_convert_internal(ctx
,
1493 smbreq
->flags2
& FLAGS2_DFS_PATHNAMES
,
1497 ppath_contains_wcard
,