s3: Fix Coverity ID 2195: NO_EFFECT
[Samba.git] / source3 / smbd / filename.c
blob03877218de57a4caba492c990680031246343440
1 /*
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.
27 #include "includes.h"
28 #include "fake_file.h"
30 static NTSTATUS build_stream_path(TALLOC_CTX *mem_ctx,
31 connection_struct *conn,
32 const char *orig_path,
33 struct smb_filename *smb_fname);
35 /****************************************************************************
36 Mangle the 2nd name and check if it is then equal to the first name.
37 ****************************************************************************/
39 static bool mangled_equal(const char *name1,
40 const char *name2,
41 const struct share_params *p)
43 char mname[13];
45 if (!name_to_8_3(name2, mname, False, p)) {
46 return False;
48 return strequal(name1, mname);
51 /****************************************************************************
52 Cope with the differing wildcard and non-wildcard error cases.
53 ****************************************************************************/
55 static NTSTATUS determine_path_error(const char *name,
56 bool allow_wcard_last_component)
58 const char *p;
60 if (!allow_wcard_last_component) {
61 /* Error code within a pathname. */
62 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
65 /* We're terminating here so we
66 * can be a little slower and get
67 * the error code right. Windows
68 * treats the last part of the pathname
69 * separately I think, so if the last
70 * component is a wildcard then we treat
71 * this ./ as "end of component" */
73 p = strchr(name, '/');
75 if (!p && (ms_has_wild(name) || ISDOT(name))) {
76 /* Error code at the end of a pathname. */
77 return NT_STATUS_OBJECT_NAME_INVALID;
78 } else {
79 /* Error code within a pathname. */
80 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
84 static NTSTATUS check_for_dot_component(const struct smb_filename *smb_fname)
86 /* Ensure we catch all names with in "/."
87 this is disallowed under Windows and
88 in POSIX they've already been removed. */
89 const char *p = strstr(smb_fname->base_name, "/."); /*mb safe*/
90 if (p) {
91 if (p[2] == '/') {
92 /* Error code within a pathname. */
93 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
94 } else if (p[2] == '\0') {
95 /* Error code at the end of a pathname. */
96 return NT_STATUS_OBJECT_NAME_INVALID;
99 return NT_STATUS_OK;
102 /****************************************************************************
103 Optimization for common case where the missing part
104 is in the last component and the client already
105 sent the correct case.
106 Returns NT_STATUS_OK to mean continue the tree walk
107 (possibly with modified start pointer).
108 Any other NT_STATUS_XXX error means terminate the path
109 lookup here.
110 ****************************************************************************/
112 static NTSTATUS check_parent_exists(TALLOC_CTX *ctx,
113 connection_struct *conn,
114 bool posix_pathnames,
115 struct smb_filename *smb_fname,
116 char **pp_dirpath,
117 char **pp_start)
119 struct smb_filename parent_fname;
120 const char *last_component = NULL;
121 NTSTATUS status;
122 int ret;
124 ZERO_STRUCT(parent_fname);
125 if (!parent_dirname(ctx, smb_fname->base_name,
126 &parent_fname.base_name,
127 &last_component)) {
128 return NT_STATUS_NO_MEMORY;
132 * If there was no parent component in
133 * smb_fname->base_name of the parent name
134 * contained a wildcard then don't do this
135 * optimization.
137 if ((smb_fname->base_name == last_component) ||
138 ms_has_wild(parent_fname.base_name)) {
139 return NT_STATUS_OK;
142 if (posix_pathnames) {
143 ret = SMB_VFS_LSTAT(conn, &parent_fname);
144 } else {
145 ret = SMB_VFS_STAT(conn, &parent_fname);
148 /* If the parent stat failed, just continue
149 with the normal tree walk. */
151 if (ret == -1) {
152 return NT_STATUS_OK;
155 status = check_for_dot_component(&parent_fname);
156 if (!NT_STATUS_IS_OK(status)) {
157 return status;
160 /* Parent exists - set "start" to be the
161 * last compnent to shorten the tree walk. */
164 * Safe to use CONST_DISCARD
165 * here as last_component points
166 * into our smb_fname->base_name.
168 *pp_start = CONST_DISCARD(char *,last_component);
170 /* Update dirpath. */
171 TALLOC_FREE(*pp_dirpath);
172 *pp_dirpath = talloc_strdup(ctx, parent_fname.base_name);
173 if (!*pp_dirpath) {
174 return NT_STATUS_NO_MEMORY;
177 DEBUG(5,("check_parent_exists: name "
178 "= %s, dirpath = %s, "
179 "start = %s\n",
180 smb_fname->base_name,
181 *pp_dirpath,
182 *pp_start));
184 return NT_STATUS_OK;
187 /****************************************************************************
188 This routine is called to convert names from the dos namespace to unix
189 namespace. It needs to handle any case conversions, mangling, format changes,
190 streams etc.
192 We assume that we have already done a chdir() to the right "root" directory
193 for this service.
195 The function will return an NTSTATUS error if some part of the name except for
196 the last part cannot be resolved, else NT_STATUS_OK.
198 Note NT_STATUS_OK doesn't mean the name exists or is valid, just that we
199 didn't get any fatal errors that should immediately terminate the calling SMB
200 processing whilst resolving.
202 If the UCF_SAVE_LCOMP flag is passed in, then the unmodified last component
203 of the pathname is set in smb_filename->original_lcomp.
205 If UCF_ALWAYS_ALLOW_WCARD_LCOMP is passed in, then a MS wildcard was detected
206 and should be allowed in the last component of the path only.
208 If the orig_path was a stream, smb_filename->base_name will point to the base
209 filename, and smb_filename->stream_name will point to the stream name. If
210 orig_path was not a stream, then smb_filename->stream_name will be NULL.
212 On exit from unix_convert, the smb_filename->st stat struct will be populated
213 if the file exists and was found, if not this stat struct will be filled with
214 zeros (and this can be detected by checking for nlinks = 0, which can never be
215 true for any file).
216 ****************************************************************************/
218 NTSTATUS unix_convert(TALLOC_CTX *ctx,
219 connection_struct *conn,
220 const char *orig_path,
221 struct smb_filename **smb_fname_out,
222 uint32_t ucf_flags)
224 struct smb_filename *smb_fname = NULL;
225 char *start, *end;
226 char *dirpath = NULL;
227 char *stream = NULL;
228 bool component_was_mangled = False;
229 bool name_has_wildcard = False;
230 bool posix_pathnames = false;
231 bool allow_wcard_last_component =
232 (ucf_flags & UCF_ALWAYS_ALLOW_WCARD_LCOMP);
233 bool save_last_component = ucf_flags & UCF_SAVE_LCOMP;
234 NTSTATUS status;
235 int ret = -1;
237 *smb_fname_out = NULL;
239 smb_fname = talloc_zero(ctx, struct smb_filename);
240 if (smb_fname == NULL) {
241 return NT_STATUS_NO_MEMORY;
244 if (conn->printer) {
245 /* we don't ever use the filenames on a printer share as a
246 filename - so don't convert them */
247 if (!(smb_fname->base_name = talloc_strdup(smb_fname,
248 orig_path))) {
249 status = NT_STATUS_NO_MEMORY;
250 goto err;
252 goto done;
255 DEBUG(5, ("unix_convert called on file \"%s\"\n", orig_path));
258 * Conversion to basic unix format is already done in
259 * check_path_syntax().
263 * Names must be relative to the root of the service - any leading /.
264 * and trailing /'s should have been trimmed by check_path_syntax().
267 #ifdef DEVELOPER
268 SMB_ASSERT(*orig_path != '/');
269 #endif
272 * If we trimmed down to a single '\0' character
273 * then we should use the "." directory to avoid
274 * searching the cache, but not if we are in a
275 * printing share.
276 * As we know this is valid we can return true here.
279 if (!*orig_path) {
280 if (!(smb_fname->base_name = talloc_strdup(smb_fname, "."))) {
281 status = NT_STATUS_NO_MEMORY;
282 goto err;
284 if (SMB_VFS_STAT(conn, smb_fname) != 0) {
285 status = map_nt_error_from_unix(errno);
286 goto err;
288 DEBUG(5, ("conversion finished \"\" -> %s\n",
289 smb_fname->base_name));
290 goto done;
293 if (orig_path[0] == '.' && (orig_path[1] == '/' ||
294 orig_path[1] == '\0')) {
295 /* Start of pathname can't be "." only. */
296 if (orig_path[1] == '\0' || orig_path[2] == '\0') {
297 status = NT_STATUS_OBJECT_NAME_INVALID;
298 } else {
299 status =determine_path_error(&orig_path[2],
300 allow_wcard_last_component);
302 goto err;
305 /* Start with the full orig_path as given by the caller. */
306 if (!(smb_fname->base_name = talloc_strdup(smb_fname, orig_path))) {
307 DEBUG(0, ("talloc_strdup failed\n"));
308 status = NT_STATUS_NO_MEMORY;
309 goto err;
313 * Large directory fix normalization. If we're case sensitive, and
314 * the case preserving parameters are set to "no", normalize the case of
315 * the incoming filename from the client WHETHER IT EXISTS OR NOT !
316 * This is in conflict with the current (3.0.20) man page, but is
317 * what people expect from the "large directory howto". I'll update
318 * the man page. Thanks to jht@samba.org for finding this. JRA.
321 if (conn->case_sensitive && !conn->case_preserve &&
322 !conn->short_case_preserve) {
323 strnorm(smb_fname->base_name, lp_defaultcase(SNUM(conn)));
327 * Ensure saved_last_component is valid even if file exists.
330 if(save_last_component) {
331 end = strrchr_m(smb_fname->base_name, '/');
332 if (end) {
333 smb_fname->original_lcomp = talloc_strdup(smb_fname,
334 end + 1);
335 } else {
336 smb_fname->original_lcomp =
337 talloc_strdup(smb_fname, smb_fname->base_name);
339 if (smb_fname->original_lcomp == NULL) {
340 status = NT_STATUS_NO_MEMORY;
341 goto err;
345 posix_pathnames = (lp_posix_pathnames() ||
346 (ucf_flags & UCF_POSIX_PATHNAMES));
349 * Strip off the stream, and add it back when we're done with the
350 * base_name.
352 if (!posix_pathnames) {
353 stream = strchr_m(smb_fname->base_name, ':');
355 if (stream != NULL) {
356 char *tmp = talloc_strdup(smb_fname, stream);
357 if (tmp == NULL) {
358 status = NT_STATUS_NO_MEMORY;
359 goto err;
362 * Since this is actually pointing into
363 * smb_fname->base_name this truncates base_name.
365 *stream = '\0';
366 stream = tmp;
370 start = smb_fname->base_name;
373 * If we're providing case insensitive semantics or
374 * the underlying filesystem is case insensitive,
375 * then a case-normalized hit in the stat-cache is
376 * authoratitive. JRA.
378 * Note: We're only checking base_name. The stream_name will be
379 * added and verified in build_stream_path().
382 if((!conn->case_sensitive || !(conn->fs_capabilities &
383 FILE_CASE_SENSITIVE_SEARCH)) &&
384 stat_cache_lookup(conn, &smb_fname->base_name, &dirpath, &start,
385 &smb_fname->st)) {
386 goto done;
390 * Make sure "dirpath" is an allocated string, we use this for
391 * building the directories with talloc_asprintf and free it.
394 if ((dirpath == NULL) && (!(dirpath = talloc_strdup(ctx,"")))) {
395 DEBUG(0, ("talloc_strdup failed\n"));
396 status = NT_STATUS_NO_MEMORY;
397 goto err;
401 * If we have a wildcard we must walk the path to
402 * find where the error is, even if case sensitive
403 * is true.
406 name_has_wildcard = ms_has_wild(smb_fname->base_name);
407 if (name_has_wildcard && !allow_wcard_last_component) {
408 /* Wildcard not valid anywhere. */
409 status = NT_STATUS_OBJECT_NAME_INVALID;
410 goto fail;
413 DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n",
414 smb_fname->base_name, dirpath, start));
416 if (!name_has_wildcard) {
418 * stat the name - if it exists then we can add the stream back (if
419 * there was one) and be done!
422 if (posix_pathnames) {
423 ret = SMB_VFS_LSTAT(conn, smb_fname);
424 } else {
425 ret = SMB_VFS_STAT(conn, smb_fname);
428 if (ret == 0) {
429 status = check_for_dot_component(smb_fname);
430 if (!NT_STATUS_IS_OK(status)) {
431 goto fail;
433 /* Add the path (not including the stream) to the cache. */
434 stat_cache_add(orig_path, smb_fname->base_name,
435 conn->case_sensitive);
436 DEBUG(5,("conversion of base_name finished %s -> %s\n",
437 orig_path, smb_fname->base_name));
438 goto done;
441 if (errno == ENOENT) {
442 /* Optimization when creating a new file - only
443 the last component doesn't exist. */
444 status = check_parent_exists(ctx,
445 conn,
446 posix_pathnames,
447 smb_fname,
448 &dirpath,
449 &start);
450 if (!NT_STATUS_IS_OK(status)) {
451 goto fail;
456 * A special case - if we don't have any wildcards or mangling chars and are case
457 * sensitive or the underlying filesystem is case insensitive then searching
458 * won't help.
461 if ((conn->case_sensitive || !(conn->fs_capabilities &
462 FILE_CASE_SENSITIVE_SEARCH)) &&
463 !mangle_is_mangled(smb_fname->base_name, conn->params)) {
465 status = check_for_dot_component(smb_fname);
466 if (!NT_STATUS_IS_OK(status)) {
467 goto fail;
471 * The stat failed. Could be ok as it could be
472 * a new file.
475 if (errno == ENOTDIR || errno == ELOOP) {
476 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
477 goto fail;
478 } else if (errno == ENOENT) {
480 * Was it a missing last component ?
481 * or a missing intermediate component ?
483 struct smb_filename parent_fname;
484 const char *last_component = NULL;
486 ZERO_STRUCT(parent_fname);
487 if (!parent_dirname(ctx, smb_fname->base_name,
488 &parent_fname.base_name,
489 &last_component)) {
490 status = NT_STATUS_NO_MEMORY;
491 goto fail;
493 if (posix_pathnames) {
494 ret = SMB_VFS_LSTAT(conn, &parent_fname);
495 } else {
496 ret = SMB_VFS_STAT(conn, &parent_fname);
498 if (ret == -1) {
499 if (errno == ENOTDIR ||
500 errno == ENOENT ||
501 errno == ELOOP) {
502 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
503 goto fail;
508 * Missing last component is ok - new file.
509 * Also deal with permission denied elsewhere.
510 * Just drop out to done.
512 goto done;
515 } else {
517 * We have a wildcard in the pathname.
519 * Optimization for common case where the wildcard
520 * is in the last component and the client already
521 * sent the correct case.
523 status = check_parent_exists(ctx,
524 conn,
525 posix_pathnames,
526 smb_fname,
527 &dirpath,
528 &start);
529 if (!NT_STATUS_IS_OK(status)) {
530 goto fail;
535 * is_mangled() was changed to look at an entire pathname, not
536 * just a component. JRA.
539 if (mangle_is_mangled(start, conn->params)) {
540 component_was_mangled = True;
544 * Now we need to recursively match the name against the real
545 * directory structure.
549 * Match each part of the path name separately, trying the names
550 * as is first, then trying to scan the directory for matching names.
553 for (; start ; start = (end?end+1:(char *)NULL)) {
555 * Pinpoint the end of this section of the filename.
557 /* mb safe. '/' can't be in any encoded char. */
558 end = strchr(start, '/');
561 * Chop the name at this point.
563 if (end) {
564 *end = 0;
567 if (save_last_component) {
568 TALLOC_FREE(smb_fname->original_lcomp);
569 smb_fname->original_lcomp = talloc_strdup(smb_fname,
570 end ? end + 1 : start);
571 if (!smb_fname->original_lcomp) {
572 DEBUG(0, ("talloc failed\n"));
573 status = NT_STATUS_NO_MEMORY;
574 goto err;
578 /* The name cannot have a component of "." */
580 if (ISDOT(start)) {
581 if (!end) {
582 /* Error code at the end of a pathname. */
583 status = NT_STATUS_OBJECT_NAME_INVALID;
584 } else {
585 status = determine_path_error(end+1,
586 allow_wcard_last_component);
588 goto fail;
591 /* The name cannot have a wildcard if it's not
592 the last component. */
594 name_has_wildcard = ms_has_wild(start);
596 /* Wildcards never valid within a pathname. */
597 if (name_has_wildcard && end) {
598 status = NT_STATUS_OBJECT_NAME_INVALID;
599 goto fail;
602 /* Skip the stat call if it's a wildcard end. */
603 if (name_has_wildcard) {
604 DEBUG(5,("Wildcard %s\n",start));
605 goto done;
609 * Check if the name exists up to this point.
612 if (posix_pathnames) {
613 ret = SMB_VFS_LSTAT(conn, smb_fname);
614 } else {
615 ret = SMB_VFS_STAT(conn, smb_fname);
618 if (ret == 0) {
620 * It exists. it must either be a directory or this must
621 * be the last part of the path for it to be OK.
623 if (end && !S_ISDIR(smb_fname->st.st_ex_mode)) {
625 * An intermediate part of the name isn't
626 * a directory.
628 DEBUG(5,("Not a dir %s\n",start));
629 *end = '/';
631 * We need to return the fact that the
632 * intermediate name resolution failed. This
633 * is used to return an error of ERRbadpath
634 * rather than ERRbadfile. Some Windows
635 * applications depend on the difference between
636 * these two errors.
638 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
639 goto fail;
642 } else {
643 char *found_name = NULL;
645 /* Stat failed - ensure we don't use it. */
646 SET_STAT_INVALID(smb_fname->st);
649 * Reset errno so we can detect
650 * directory open errors.
652 errno = 0;
655 * Try to find this part of the path in the directory.
658 if (name_has_wildcard ||
659 (get_real_filename(conn, dirpath, start,
660 talloc_tos(),
661 &found_name) == -1)) {
662 char *unmangled;
664 if (end) {
666 * An intermediate part of the name
667 * can't be found.
669 DEBUG(5,("Intermediate not found %s\n",
670 start));
671 *end = '/';
674 * We need to return the fact that the
675 * intermediate name resolution failed.
676 * This is used to return an error of
677 * ERRbadpath rather than ERRbadfile.
678 * Some Windows applications depend on
679 * the difference between these two
680 * errors.
684 * ENOENT, ENOTDIR and ELOOP all map
685 * to NT_STATUS_OBJECT_PATH_NOT_FOUND
686 * in the filename walk.
689 if (errno == ENOENT ||
690 errno == ENOTDIR ||
691 errno == ELOOP) {
692 status =
693 NT_STATUS_OBJECT_PATH_NOT_FOUND;
695 else {
696 status =
697 map_nt_error_from_unix(errno);
699 goto fail;
703 * ENOENT/EACCESS are the only valid errors
704 * here. EACCESS needs handling here for
705 * "dropboxes", i.e. directories where users
706 * can only put stuff with permission -wx.
708 if ((errno != 0) && (errno != ENOENT)
709 && (errno != EACCES)) {
711 * ENOTDIR and ELOOP both map to
712 * NT_STATUS_OBJECT_PATH_NOT_FOUND
713 * in the filename walk.
715 if (errno == ENOTDIR ||
716 errno == ELOOP) {
717 status =
718 NT_STATUS_OBJECT_PATH_NOT_FOUND;
719 } else {
720 status =
721 map_nt_error_from_unix(errno);
723 goto fail;
727 * Just the last part of the name doesn't exist.
728 * We need to strupper() or strlower() it as
729 * this conversion may be used for file creation
730 * purposes. Fix inspired by
731 * Thomas Neumann <t.neumann@iku-ag.de>.
733 if (!conn->case_preserve ||
734 (mangle_is_8_3(start, False,
735 conn->params) &&
736 !conn->short_case_preserve)) {
737 strnorm(start,
738 lp_defaultcase(SNUM(conn)));
742 * check on the mangled stack to see if we can
743 * recover the base of the filename.
746 if (mangle_is_mangled(start, conn->params)
747 && mangle_lookup_name_from_8_3(ctx,
748 start,
749 &unmangled,
750 conn->params)) {
751 char *tmp;
752 size_t start_ofs =
753 start - smb_fname->base_name;
755 if (*dirpath != '\0') {
756 tmp = talloc_asprintf(
757 smb_fname, "%s/%s",
758 dirpath, unmangled);
759 TALLOC_FREE(unmangled);
761 else {
762 tmp = unmangled;
764 if (tmp == NULL) {
765 DEBUG(0, ("talloc failed\n"));
766 status = NT_STATUS_NO_MEMORY;
767 goto err;
769 TALLOC_FREE(smb_fname->base_name);
770 smb_fname->base_name = tmp;
771 start =
772 smb_fname->base_name + start_ofs;
773 end = start + strlen(start);
776 DEBUG(5,("New file %s\n",start));
777 goto done;
782 * Restore the rest of the string. If the string was
783 * mangled the size may have changed.
785 if (end) {
786 char *tmp;
787 size_t start_ofs =
788 start - smb_fname->base_name;
790 if (*dirpath != '\0') {
791 tmp = talloc_asprintf(smb_fname,
792 "%s/%s/%s", dirpath,
793 found_name, end+1);
795 else {
796 tmp = talloc_asprintf(smb_fname,
797 "%s/%s", found_name,
798 end+1);
800 if (tmp == NULL) {
801 DEBUG(0, ("talloc_asprintf failed\n"));
802 status = NT_STATUS_NO_MEMORY;
803 goto err;
805 TALLOC_FREE(smb_fname->base_name);
806 smb_fname->base_name = tmp;
807 start = smb_fname->base_name + start_ofs;
808 end = start + strlen(found_name);
809 *end = '\0';
810 } else {
811 char *tmp;
812 size_t start_ofs =
813 start - smb_fname->base_name;
815 if (*dirpath != '\0') {
816 tmp = talloc_asprintf(smb_fname,
817 "%s/%s", dirpath,
818 found_name);
819 } else {
820 tmp = talloc_strdup(smb_fname,
821 found_name);
823 if (tmp == NULL) {
824 DEBUG(0, ("talloc failed\n"));
825 status = NT_STATUS_NO_MEMORY;
826 goto err;
828 TALLOC_FREE(smb_fname->base_name);
829 smb_fname->base_name = tmp;
830 start = smb_fname->base_name + start_ofs;
833 * We just scanned for, and found the end of
834 * the path. We must return a valid stat struct
835 * if it exists. JRA.
838 if (posix_pathnames) {
839 ret = SMB_VFS_LSTAT(conn, smb_fname);
840 } else {
841 ret = SMB_VFS_STAT(conn, smb_fname);
844 if (ret != 0) {
845 SET_STAT_INVALID(smb_fname->st);
849 TALLOC_FREE(found_name);
850 } /* end else */
852 #ifdef DEVELOPER
854 * This sucks!
855 * We should never provide different behaviors
856 * depending on DEVELOPER!!!
858 if (VALID_STAT(smb_fname->st)) {
859 bool delete_pending;
860 uint32_t name_hash;
862 status = file_name_hash(conn,
863 smb_fname_str_dbg(smb_fname),
864 &name_hash);
865 if (!NT_STATUS_IS_OK(status)) {
866 goto fail;
869 get_file_infos(vfs_file_id_from_sbuf(conn,
870 &smb_fname->st),
871 name_hash,
872 &delete_pending, NULL);
873 if (delete_pending) {
874 status = NT_STATUS_DELETE_PENDING;
875 goto fail;
878 #endif
881 * Add to the dirpath that we have resolved so far.
884 if (*dirpath != '\0') {
885 char *tmp = talloc_asprintf(ctx,
886 "%s/%s", dirpath, start);
887 if (!tmp) {
888 DEBUG(0, ("talloc_asprintf failed\n"));
889 status = NT_STATUS_NO_MEMORY;
890 goto err;
892 TALLOC_FREE(dirpath);
893 dirpath = tmp;
895 else {
896 TALLOC_FREE(dirpath);
897 if (!(dirpath = talloc_strdup(ctx,start))) {
898 DEBUG(0, ("talloc_strdup failed\n"));
899 status = NT_STATUS_NO_MEMORY;
900 goto err;
905 * Cache the dirpath thus far. Don't cache a name with mangled
906 * or wildcard components as this can change the size.
908 if(!component_was_mangled && !name_has_wildcard) {
909 stat_cache_add(orig_path, dirpath,
910 conn->case_sensitive);
914 * Restore the / that we wiped out earlier.
916 if (end) {
917 *end = '/';
922 * Cache the full path. Don't cache a name with mangled or wildcard
923 * components as this can change the size.
926 if(!component_was_mangled && !name_has_wildcard) {
927 stat_cache_add(orig_path, smb_fname->base_name,
928 conn->case_sensitive);
932 * The name has been resolved.
935 DEBUG(5,("conversion finished %s -> %s\n", orig_path,
936 smb_fname->base_name));
938 done:
939 /* Add back the stream if one was stripped off originally. */
940 if (stream != NULL) {
941 smb_fname->stream_name = stream;
943 /* Check path now that the base_name has been converted. */
944 status = build_stream_path(ctx, conn, orig_path, smb_fname);
945 if (!NT_STATUS_IS_OK(status)) {
946 goto fail;
949 TALLOC_FREE(dirpath);
950 *smb_fname_out = smb_fname;
951 return NT_STATUS_OK;
952 fail:
953 DEBUG(10, ("dirpath = [%s] start = [%s]\n", dirpath, start));
954 if (*dirpath != '\0') {
955 smb_fname->base_name = talloc_asprintf(smb_fname, "%s/%s",
956 dirpath, start);
957 } else {
958 smb_fname->base_name = talloc_strdup(smb_fname, start);
960 if (!smb_fname->base_name) {
961 DEBUG(0, ("talloc_asprintf failed\n"));
962 status = NT_STATUS_NO_MEMORY;
963 goto err;
966 *smb_fname_out = smb_fname;
967 TALLOC_FREE(dirpath);
968 return status;
969 err:
970 TALLOC_FREE(smb_fname);
971 return status;
974 /****************************************************************************
975 Check a filename - possibly calling check_reduced_name.
976 This is called by every routine before it allows an operation on a filename.
977 It does any final confirmation necessary to ensure that the filename is
978 a valid one for the user to access.
979 ****************************************************************************/
981 NTSTATUS check_name(connection_struct *conn, const char *name)
983 if (IS_VETO_PATH(conn, name)) {
984 /* Is it not dot or dot dot. */
985 if (!((name[0] == '.') && (!name[1] ||
986 (name[1] == '.' && !name[2])))) {
987 DEBUG(5,("check_name: file path name %s vetoed\n",
988 name));
989 return map_nt_error_from_unix(ENOENT);
993 if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
994 NTSTATUS status = check_reduced_name(conn,name);
995 if (!NT_STATUS_IS_OK(status)) {
996 DEBUG(5,("check_name: name %s failed with %s\n",name,
997 nt_errstr(status)));
998 return status;
1002 return NT_STATUS_OK;
1005 /****************************************************************************
1006 Check if two filenames are equal.
1007 This needs to be careful about whether we are case sensitive.
1008 ****************************************************************************/
1010 static bool fname_equal(const char *name1, const char *name2,
1011 bool case_sensitive)
1013 /* Normal filename handling */
1014 if (case_sensitive) {
1015 return(strcmp(name1,name2) == 0);
1018 return(strequal(name1,name2));
1021 /****************************************************************************
1022 Scan a directory to find a filename, matching without case sensitivity.
1023 If the name looks like a mangled name then try via the mangling functions
1024 ****************************************************************************/
1026 static int get_real_filename_full_scan(connection_struct *conn,
1027 const char *path, const char *name,
1028 bool mangled,
1029 TALLOC_CTX *mem_ctx, char **found_name)
1031 struct smb_Dir *cur_dir;
1032 const char *dname = NULL;
1033 char *talloced = NULL;
1034 char *unmangled_name = NULL;
1035 long curpos;
1037 /* handle null paths */
1038 if ((path == NULL) || (*path == 0)) {
1039 path = ".";
1042 /* If we have a case-sensitive filesystem, it doesn't do us any
1043 * good to search for a name. If a case variation of the name was
1044 * there, then the original stat(2) would have found it.
1046 if (!mangled && !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) {
1047 errno = ENOENT;
1048 return -1;
1052 * The incoming name can be mangled, and if we de-mangle it
1053 * here it will not compare correctly against the filename (name2)
1054 * read from the directory and then mangled by the name_to_8_3()
1055 * call. We need to mangle both names or neither.
1056 * (JRA).
1058 * Fix for bug found by Dina Fine. If in case sensitive mode then
1059 * the mangle cache is no good (3 letter extension could be wrong
1060 * case - so don't demangle in this case - leave as mangled and
1061 * allow the mangling of the directory entry read (which is done
1062 * case insensitively) to match instead. This will lead to more
1063 * false positive matches but we fail completely without it. JRA.
1066 if (mangled && !conn->case_sensitive) {
1067 mangled = !mangle_lookup_name_from_8_3(talloc_tos(), name,
1068 &unmangled_name,
1069 conn->params);
1070 if (!mangled) {
1071 /* Name is now unmangled. */
1072 name = unmangled_name;
1076 /* open the directory */
1077 if (!(cur_dir = OpenDir(talloc_tos(), conn, path, NULL, 0))) {
1078 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
1079 TALLOC_FREE(unmangled_name);
1080 return -1;
1083 /* now scan for matching names */
1084 curpos = 0;
1085 while ((dname = ReadDirName(cur_dir, &curpos, NULL, &talloced))) {
1087 /* Is it dot or dot dot. */
1088 if (ISDOT(dname) || ISDOTDOT(dname)) {
1089 TALLOC_FREE(talloced);
1090 continue;
1094 * At this point dname is the unmangled name.
1095 * name is either mangled or not, depending on the state
1096 * of the "mangled" variable. JRA.
1100 * Check mangled name against mangled name, or unmangled name
1101 * against unmangled name.
1104 if ((mangled && mangled_equal(name,dname,conn->params)) ||
1105 fname_equal(name, dname, conn->case_sensitive)) {
1106 /* we've found the file, change it's name and return */
1107 *found_name = talloc_strdup(mem_ctx, dname);
1108 TALLOC_FREE(unmangled_name);
1109 TALLOC_FREE(cur_dir);
1110 if (!*found_name) {
1111 errno = ENOMEM;
1112 TALLOC_FREE(talloced);
1113 return -1;
1115 TALLOC_FREE(talloced);
1116 return 0;
1118 TALLOC_FREE(talloced);
1121 TALLOC_FREE(unmangled_name);
1122 TALLOC_FREE(cur_dir);
1123 errno = ENOENT;
1124 return -1;
1127 /****************************************************************************
1128 Wrapper around the vfs get_real_filename and the full directory scan
1129 fallback.
1130 ****************************************************************************/
1132 int get_real_filename(connection_struct *conn, const char *path,
1133 const char *name, TALLOC_CTX *mem_ctx,
1134 char **found_name)
1136 int ret;
1137 bool mangled;
1139 mangled = mangle_is_mangled(name, conn->params);
1141 if (mangled) {
1142 return get_real_filename_full_scan(conn, path, name, mangled,
1143 mem_ctx, found_name);
1146 /* Try the vfs first to take advantage of case-insensitive stat. */
1147 ret = SMB_VFS_GET_REAL_FILENAME(conn, path, name, mem_ctx, found_name);
1150 * If the case-insensitive stat was successful, or returned an error
1151 * other than EOPNOTSUPP then there is no need to fall back on the
1152 * full directory scan.
1154 if (ret == 0 || (ret == -1 && errno != EOPNOTSUPP)) {
1155 return ret;
1158 return get_real_filename_full_scan(conn, path, name, mangled, mem_ctx,
1159 found_name);
1162 static NTSTATUS build_stream_path(TALLOC_CTX *mem_ctx,
1163 connection_struct *conn,
1164 const char *orig_path,
1165 struct smb_filename *smb_fname)
1167 NTSTATUS status;
1168 unsigned int i, num_streams;
1169 struct stream_struct *streams = NULL;
1171 if (SMB_VFS_STAT(conn, smb_fname) == 0) {
1172 DEBUG(10, ("'%s' exists\n", smb_fname_str_dbg(smb_fname)));
1173 return NT_STATUS_OK;
1176 if (errno != ENOENT) {
1177 DEBUG(10, ("vfs_stat failed: %s\n", strerror(errno)));
1178 status = map_nt_error_from_unix(errno);
1179 goto fail;
1182 /* Fall back to a case-insensitive scan of all streams on the file. */
1183 status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, mem_ctx,
1184 &num_streams, &streams);
1186 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1187 SET_STAT_INVALID(smb_fname->st);
1188 return NT_STATUS_OK;
1191 if (!NT_STATUS_IS_OK(status)) {
1192 DEBUG(10, ("vfs_streaminfo failed: %s\n", nt_errstr(status)));
1193 goto fail;
1196 for (i=0; i<num_streams; i++) {
1197 DEBUG(10, ("comparing [%s] and [%s]: ",
1198 smb_fname->stream_name, streams[i].name));
1199 if (fname_equal(smb_fname->stream_name, streams[i].name,
1200 conn->case_sensitive)) {
1201 DEBUGADD(10, ("equal\n"));
1202 break;
1204 DEBUGADD(10, ("not equal\n"));
1207 /* Couldn't find the stream. */
1208 if (i == num_streams) {
1209 SET_STAT_INVALID(smb_fname->st);
1210 TALLOC_FREE(streams);
1211 return NT_STATUS_OK;
1214 DEBUG(10, ("case insensitive stream. requested: %s, actual: %s\n",
1215 smb_fname->stream_name, streams[i].name));
1218 TALLOC_FREE(smb_fname->stream_name);
1219 smb_fname->stream_name = talloc_strdup(smb_fname, streams[i].name);
1220 if (smb_fname->stream_name == NULL) {
1221 status = NT_STATUS_NO_MEMORY;
1222 goto fail;
1225 SET_STAT_INVALID(smb_fname->st);
1227 if (SMB_VFS_STAT(conn, smb_fname) == 0) {
1228 DEBUG(10, ("'%s' exists\n", smb_fname_str_dbg(smb_fname)));
1230 status = NT_STATUS_OK;
1231 fail:
1232 TALLOC_FREE(streams);
1233 return status;
1237 * Go through all the steps to validate a filename.
1239 * @param ctx talloc_ctx to allocate memory with.
1240 * @param conn connection struct for vfs calls.
1241 * @param dfs_path Whether this path requires dfs resolution.
1242 * @param name_in The unconverted name.
1243 * @param ucf_flags flags to pass through to unix_convert().
1244 * UCF_ALWAYS_ALLOW_WCARD_LCOMP will be OR'd in if
1245 * p_cont_wcard != NULL and is true and
1246 * UCF_COND_ALLOW_WCARD_LCOMP.
1247 * @param p_cont_wcard If not NULL, will be set to true if the dfs path
1248 * resolution detects a wildcard.
1249 * @param pp_smb_fname The final converted name will be allocated if the
1250 * return is NT_STATUS_OK.
1252 * @return NT_STATUS_OK if all operations completed succesfully, appropriate
1253 * error otherwise.
1255 NTSTATUS filename_convert(TALLOC_CTX *ctx,
1256 connection_struct *conn,
1257 bool dfs_path,
1258 const char *name_in,
1259 uint32_t ucf_flags,
1260 bool *ppath_contains_wcard,
1261 struct smb_filename **pp_smb_fname)
1263 NTSTATUS status;
1264 bool allow_wcards = (ucf_flags & (UCF_COND_ALLOW_WCARD_LCOMP|UCF_ALWAYS_ALLOW_WCARD_LCOMP));
1265 char *fname = NULL;
1267 *pp_smb_fname = NULL;
1269 status = resolve_dfspath_wcard(ctx, conn,
1270 dfs_path,
1271 name_in,
1272 allow_wcards,
1273 &fname,
1274 ppath_contains_wcard);
1275 if (!NT_STATUS_IS_OK(status)) {
1276 DEBUG(10,("filename_convert: resolve_dfspath failed "
1277 "for name %s with %s\n",
1278 name_in,
1279 nt_errstr(status) ));
1280 return status;
1283 if (is_fake_file_path(name_in)) {
1284 SMB_STRUCT_STAT st;
1285 ZERO_STRUCT(st);
1286 st.st_ex_nlink = 1;
1287 status = create_synthetic_smb_fname_split(ctx,
1288 name_in,
1289 &st,
1290 pp_smb_fname);
1291 return status;
1295 * If the caller conditionally allows wildcard lookups, only add the
1296 * always allow if the path actually does contain a wildcard.
1298 if (ucf_flags & UCF_COND_ALLOW_WCARD_LCOMP &&
1299 ppath_contains_wcard != NULL && *ppath_contains_wcard) {
1300 ucf_flags |= UCF_ALWAYS_ALLOW_WCARD_LCOMP;
1303 status = unix_convert(ctx, conn, fname, pp_smb_fname, ucf_flags);
1304 if (!NT_STATUS_IS_OK(status)) {
1305 DEBUG(10,("filename_convert: unix_convert failed "
1306 "for name %s with %s\n",
1307 fname,
1308 nt_errstr(status) ));
1309 return status;
1312 status = check_name(conn, (*pp_smb_fname)->base_name);
1313 if (!NT_STATUS_IS_OK(status)) {
1314 DEBUG(3,("filename_convert: check_name failed "
1315 "for name %s with %s\n",
1316 smb_fname_str_dbg(*pp_smb_fname),
1317 nt_errstr(status) ));
1318 TALLOC_FREE(*pp_smb_fname);
1319 return status;
1322 return status;