source: branches/samba-3.3.x/source/smbd/filename.c@ 693

Last change on this file since 693 was 274, checked in by Herwig Bauernfeind, 16 years ago

Update 3.3 branch to 3.3.5

File size: 26.7 KB
Line 
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
8
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.
13
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.
18
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/>.
21*/
22
23/*
24 * New hash table stat cache code added by Ying Chen.
25 */
26
27#include "includes.h"
28
29static NTSTATUS build_stream_path(TALLOC_CTX *mem_ctx,
30 connection_struct *conn,
31 const char *orig_path,
32 const char *basepath,
33 const char *streamname,
34 SMB_STRUCT_STAT *pst,
35 char **path);
36static int get_real_filename_mangled(connection_struct *conn, const char *path,
37 const char *name, TALLOC_CTX *mem_ctx,
38 char **found_name);
39static int get_real_filename_internal(connection_struct *conn,
40 const char *path, const char *name,
41 bool mangled,
42 TALLOC_CTX *mem_ctx, char **found_name);
43
44/****************************************************************************
45 Mangle the 2nd name and check if it is then equal to the first name.
46****************************************************************************/
47
48static bool mangled_equal(const char *name1,
49 const char *name2,
50 const struct share_params *p)
51{
52 char mname[13];
53
54 if (!name_to_8_3(name2, mname, False, p)) {
55 return False;
56 }
57 return strequal(name1, mname);
58}
59
60/****************************************************************************
61 Cope with the differing wildcard and non-wildcard error cases.
62****************************************************************************/
63
64static NTSTATUS determine_path_error(const char *name,
65 bool allow_wcard_last_component)
66{
67 const char *p;
68
69 if (!allow_wcard_last_component) {
70 /* Error code within a pathname. */
71 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
72 }
73
74 /* We're terminating here so we
75 * can be a little slower and get
76 * the error code right. Windows
77 * treats the last part of the pathname
78 * separately I think, so if the last
79 * component is a wildcard then we treat
80 * this ./ as "end of component" */
81
82 p = strchr(name, '/');
83
84 if (!p && (ms_has_wild(name) || ISDOT(name))) {
85 /* Error code at the end of a pathname. */
86 return NT_STATUS_OBJECT_NAME_INVALID;
87 } else {
88 /* Error code within a pathname. */
89 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
90 }
91}
92
93/****************************************************************************
94This routine is called to convert names from the dos namespace to unix
95namespace. It needs to handle any case conversions, mangling, format
96changes etc.
97
98We assume that we have already done a chdir() to the right "root" directory
99for this service.
100
101The function will return an NTSTATUS error if some part of the name except for
102the last part cannot be resolved, else NT_STATUS_OK.
103
104Note NT_STATUS_OK doesn't mean the name exists or is valid, just that we didn't
105get any fatal errors that should immediately terminate the calling
106SMB processing whilst resolving.
107
108If the saved_last_component != 0, then the unmodified last component
109of the pathname is returned there. If saved_last_component == 0 then nothing
110is returned there.
111
112If last_component_wcard is true then a MS wildcard was detected and
113should be allowed in the last component of the path only.
114
115On exit from unix_convert, if *pst was not null, then the file stat
116struct will be returned if the file exists and was found, if not this
117stat struct will be filled with zeros (and this can be detected by checking
118for nlinks = 0, which can never be true for any file).
119****************************************************************************/
120
121NTSTATUS unix_convert(TALLOC_CTX *ctx,
122 connection_struct *conn,
123 const char *orig_path,
124 bool allow_wcard_last_component,
125 char **pp_conv_path,
126 char **pp_saved_last_component,
127 SMB_STRUCT_STAT *pst)
128{
129 SMB_STRUCT_STAT st;
130 char *start, *end;
131 char *dirpath = NULL;
132 char *name = NULL;
133 char *stream = NULL;
134 bool component_was_mangled = False;
135 bool name_has_wildcard = False;
136 bool posix_pathnames = false;
137 NTSTATUS result;
138 int ret = -1;
139
140 SET_STAT_INVALID(*pst);
141 *pp_conv_path = NULL;
142 if(pp_saved_last_component) {
143 *pp_saved_last_component = NULL;
144 }
145
146 if (conn->printer) {
147 /* we don't ever use the filenames on a printer share as a
148 filename - so don't convert them */
149 if (!(*pp_conv_path = talloc_strdup(ctx,orig_path))) {
150 return NT_STATUS_NO_MEMORY;
151 }
152 return NT_STATUS_OK;
153 }
154
155 DEBUG(5, ("unix_convert called on file \"%s\"\n", orig_path));
156
157 /*
158 * Conversion to basic unix format is already done in
159 * check_path_syntax().
160 */
161
162 /*
163 * Names must be relative to the root of the service - any leading /.
164 * and trailing /'s should have been trimmed by check_path_syntax().
165 */
166
167#ifdef DEVELOPER
168 SMB_ASSERT(*orig_path != '/');
169#endif
170
171 /*
172 * If we trimmed down to a single '\0' character
173 * then we should use the "." directory to avoid
174 * searching the cache, but not if we are in a
175 * printing share.
176 * As we know this is valid we can return true here.
177 */
178
179 if (!*orig_path) {
180 if (!(name = talloc_strdup(ctx,"."))) {
181 return NT_STATUS_NO_MEMORY;
182 }
183 if (SMB_VFS_STAT(conn,name,&st) == 0) {
184 *pst = st;
185 } else {
186 return map_nt_error_from_unix(errno);
187 }
188 DEBUG(5,("conversion finished \"\" -> %s\n",name));
189 goto done;
190 }
191
192 if (orig_path[0] == '.' && (orig_path[1] == '/' ||
193 orig_path[1] == '\0')) {
194 /* Start of pathname can't be "." only. */
195 if (orig_path[1] == '\0' || orig_path[2] == '\0') {
196 result = NT_STATUS_OBJECT_NAME_INVALID;
197 } else {
198 result =determine_path_error(
199 &orig_path[2], allow_wcard_last_component);
200 }
201 return result;
202 }
203
204 if (!(name = talloc_strdup(ctx, orig_path))) {
205 DEBUG(0, ("talloc_strdup failed\n"));
206 return NT_STATUS_NO_MEMORY;
207 }
208
209 /*
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.
216 */
217
218 if (conn->case_sensitive && !conn->case_preserve &&
219 !conn->short_case_preserve) {
220 strnorm(name, lp_defaultcase(SNUM(conn)));
221 }
222
223 /*
224 * Ensure saved_last_component is valid even if file exists.
225 */
226
227 if(pp_saved_last_component) {
228 end = strrchr_m(name, '/');
229 if (end) {
230 *pp_saved_last_component = talloc_strdup(ctx, end + 1);
231 } else {
232 *pp_saved_last_component = talloc_strdup(ctx,
233 name);
234 }
235 }
236
237 posix_pathnames = lp_posix_pathnames();
238
239 if (!posix_pathnames) {
240 stream = strchr_m(name, ':');
241
242 if (stream != NULL) {
243 char *tmp = talloc_strdup(ctx, stream);
244 if (tmp == NULL) {
245 TALLOC_FREE(name);
246 return NT_STATUS_NO_MEMORY;
247 }
248 *stream = '\0';
249 stream = tmp;
250 }
251 }
252
253 start = name;
254
255 /* If we're providing case insentive semantics or
256 * the underlying filesystem is case insensitive,
257 * then a case-normalized hit in the stat-cache is
258 * authoratitive. JRA.
259 */
260
261 if((!conn->case_sensitive || !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) &&
262 stat_cache_lookup(conn, &name, &dirpath, &start, &st)) {
263 *pst = st;
264 goto done;
265 }
266
267 /*
268 * Make sure "dirpath" is an allocated string, we use this for
269 * building the directories with asprintf and free it.
270 */
271
272 if ((dirpath == NULL) && (!(dirpath = talloc_strdup(ctx,"")))) {
273 DEBUG(0, ("talloc_strdup failed\n"));
274 TALLOC_FREE(name);
275 return NT_STATUS_NO_MEMORY;
276 }
277
278 /*
279 * stat the name - if it exists then we are all done!
280 */
281
282 if (posix_pathnames) {
283 ret = SMB_VFS_LSTAT(conn,name,&st);
284 } else {
285 ret = SMB_VFS_STAT(conn,name,&st);
286 }
287
288 if (ret == 0) {
289 /* Ensure we catch all names with in "/."
290 this is disallowed under Windows. */
291 const char *p = strstr(name, "/."); /* mb safe. */
292 if (p) {
293 if (p[2] == '/') {
294 /* Error code within a pathname. */
295 result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
296 goto fail;
297 } else if (p[2] == '\0') {
298 /* Error code at the end of a pathname. */
299 result = NT_STATUS_OBJECT_NAME_INVALID;
300 goto fail;
301 }
302 }
303 stat_cache_add(orig_path, name, conn->case_sensitive);
304 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
305 *pst = st;
306 goto done;
307 }
308
309 DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n",
310 name, dirpath, start));
311
312 /*
313 * A special case - if we don't have any mangling chars and are case
314 * sensitive or the underlying filesystem is case insentive then searching
315 * won't help.
316 */
317
318 if ((conn->case_sensitive || !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) &&
319 !mangle_is_mangled(name, conn->params)) {
320 goto done;
321 }
322
323 /*
324 * is_mangled() was changed to look at an entire pathname, not
325 * just a component. JRA.
326 */
327
328 if (mangle_is_mangled(start, conn->params)) {
329 component_was_mangled = True;
330 }
331
332 /*
333 * Now we need to recursively match the name against the real
334 * directory structure.
335 */
336
337 /*
338 * Match each part of the path name separately, trying the names
339 * as is first, then trying to scan the directory for matching names.
340 */
341
342 for (; start ; start = (end?end+1:(char *)NULL)) {
343 /*
344 * Pinpoint the end of this section of the filename.
345 */
346 /* mb safe. '/' can't be in any encoded char. */
347 end = strchr(start, '/');
348
349 /*
350 * Chop the name at this point.
351 */
352 if (end) {
353 *end = 0;
354 }
355
356 if (pp_saved_last_component) {
357 TALLOC_FREE(*pp_saved_last_component);
358 *pp_saved_last_component = talloc_strdup(ctx,
359 end ? end + 1 : start);
360 if (!*pp_saved_last_component) {
361 DEBUG(0, ("talloc failed\n"));
362 return NT_STATUS_NO_MEMORY;
363 }
364 }
365
366 /* The name cannot have a component of "." */
367
368 if (ISDOT(start)) {
369 if (!end) {
370 /* Error code at the end of a pathname. */
371 result = NT_STATUS_OBJECT_NAME_INVALID;
372 } else {
373 result = determine_path_error(end+1,
374 allow_wcard_last_component);
375 }
376 goto fail;
377 }
378
379 /* The name cannot have a wildcard if it's not
380 the last component. */
381
382 name_has_wildcard = ms_has_wild(start);
383
384 /* Wildcard not valid anywhere. */
385 if (name_has_wildcard && !allow_wcard_last_component) {
386 result = NT_STATUS_OBJECT_NAME_INVALID;
387 goto fail;
388 }
389
390 /* Wildcards never valid within a pathname. */
391 if (name_has_wildcard && end) {
392 result = NT_STATUS_OBJECT_NAME_INVALID;
393 goto fail;
394 }
395
396 /*
397 * Check if the name exists up to this point.
398 */
399
400 if (posix_pathnames) {
401 ret = SMB_VFS_LSTAT(conn,name, &st);
402 } else {
403 ret = SMB_VFS_STAT(conn,name, &st);
404 }
405
406 if (ret == 0) {
407 /*
408 * It exists. it must either be a directory or this must
409 * be the last part of the path for it to be OK.
410 */
411 if (end && !(st.st_mode & S_IFDIR)) {
412 /*
413 * An intermediate part of the name isn't
414 * a directory.
415 */
416 DEBUG(5,("Not a dir %s\n",start));
417 *end = '/';
418 /*
419 * We need to return the fact that the
420 * intermediate name resolution failed. This
421 * is used to return an error of ERRbadpath
422 * rather than ERRbadfile. Some Windows
423 * applications depend on the difference between
424 * these two errors.
425 */
426 result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
427 goto fail;
428 }
429
430 if (!end) {
431 /*
432 * We just scanned for, and found the end of
433 * the path. We must return the valid stat
434 * struct. JRA.
435 */
436
437 *pst = st;
438 }
439
440 } else {
441 char *found_name = NULL;
442
443 /* Stat failed - ensure we don't use it. */
444 SET_STAT_INVALID(st);
445
446 /*
447 * Reset errno so we can detect
448 * directory open errors.
449 */
450 errno = 0;
451
452 /*
453 * Try to find this part of the path in the directory.
454 */
455
456 if (name_has_wildcard ||
457 (get_real_filename_mangled(
458 conn, dirpath, start,
459 talloc_tos(), &found_name) == -1)) {
460 char *unmangled;
461
462 if (end) {
463 /*
464 * An intermediate part of the name
465 * can't be found.
466 */
467 DEBUG(5,("Intermediate not found %s\n",
468 start));
469 *end = '/';
470
471 /*
472 * We need to return the fact that the
473 * intermediate name resolution failed.
474 * This is used to return an error of
475 * ERRbadpath rather than ERRbadfile.
476 * Some Windows applications depend on
477 * the difference between these two
478 * errors.
479 */
480
481 /*
482 * ENOENT, ENOTDIR and ELOOP all map
483 * to NT_STATUS_OBJECT_PATH_NOT_FOUND
484 * in the filename walk.
485 */
486
487 if (errno == ENOENT ||
488 errno == ENOTDIR ||
489 errno == ELOOP) {
490 result =
491 NT_STATUS_OBJECT_PATH_NOT_FOUND;
492 }
493 else {
494 result =
495 map_nt_error_from_unix(errno);
496 }
497 goto fail;
498 }
499
500 /*
501 * ENOENT/EACCESS are the only valid errors
502 * here. EACCESS needs handling here for
503 * "dropboxes", i.e. directories where users
504 * can only put stuff with permission -wx.
505 */
506 if ((errno != 0) && (errno != ENOENT)
507 && (errno != EACCES)) {
508 /*
509 * ENOTDIR and ELOOP both map to
510 * NT_STATUS_OBJECT_PATH_NOT_FOUND
511 * in the filename walk.
512 */
513 if (errno == ENOTDIR ||
514 errno == ELOOP) {
515 result =
516 NT_STATUS_OBJECT_PATH_NOT_FOUND;
517 } else {
518 result =
519 map_nt_error_from_unix(errno);
520 }
521 goto fail;
522 }
523
524 /*
525 * Just the last part of the name doesn't exist.
526 * We need to strupper() or strlower() it as
527 * this conversion may be used for file creation
528 * purposes. Fix inspired by
529 * Thomas Neumann <t.neumann@iku-ag.de>.
530 */
531 if (!conn->case_preserve ||
532 (mangle_is_8_3(start, False,
533 conn->params) &&
534 !conn->short_case_preserve)) {
535 strnorm(start,
536 lp_defaultcase(SNUM(conn)));
537 }
538
539 /*
540 * check on the mangled stack to see if we can
541 * recover the base of the filename.
542 */
543
544 if (mangle_is_mangled(start, conn->params)
545 && mangle_lookup_name_from_8_3(ctx,
546 start,
547 &unmangled,
548 conn->params)) {
549 char *tmp;
550 size_t start_ofs = start - name;
551
552 if (*dirpath != '\0') {
553 tmp = talloc_asprintf(ctx,
554 "%s/%s", dirpath,
555 unmangled);
556 TALLOC_FREE(unmangled);
557 }
558 else {
559 tmp = unmangled;
560 }
561 if (tmp == NULL) {
562 DEBUG(0, ("talloc failed\n"));
563 return NT_STATUS_NO_MEMORY;
564 }
565 TALLOC_FREE(name);
566 name = tmp;
567 start = name + start_ofs;
568 end = start + strlen(start);
569 }
570
571 DEBUG(5,("New file %s\n",start));
572 goto done;
573 }
574
575
576 /*
577 * Restore the rest of the string. If the string was
578 * mangled the size may have changed.
579 */
580 if (end) {
581 char *tmp;
582 size_t start_ofs = start - name;
583
584 if (*dirpath != '\0') {
585 tmp = talloc_asprintf(ctx,
586 "%s/%s/%s", dirpath,
587 found_name, end+1);
588 }
589 else {
590 tmp = talloc_asprintf(ctx,
591 "%s/%s", found_name,
592 end+1);
593 }
594 if (tmp == NULL) {
595 DEBUG(0, ("talloc_asprintf failed\n"));
596 return NT_STATUS_NO_MEMORY;
597 }
598 TALLOC_FREE(name);
599 name = tmp;
600 start = name + start_ofs;
601 end = start + strlen(found_name);
602 *end = '\0';
603 } else {
604 char *tmp;
605 size_t start_ofs = start - name;
606
607 if (*dirpath != '\0') {
608 tmp = talloc_asprintf(ctx,
609 "%s/%s", dirpath,
610 found_name);
611 } else {
612 tmp = talloc_strdup(ctx,
613 found_name);
614 }
615 if (tmp == NULL) {
616 DEBUG(0, ("talloc failed\n"));
617 return NT_STATUS_NO_MEMORY;
618 }
619 TALLOC_FREE(name);
620 name = tmp;
621 start = name + start_ofs;
622
623 /*
624 * We just scanned for, and found the end of
625 * the path. We must return a valid stat struct
626 * if it exists. JRA.
627 */
628
629 if (posix_pathnames) {
630 ret = SMB_VFS_LSTAT(conn,name, &st);
631 } else {
632 ret = SMB_VFS_STAT(conn,name, &st);
633 }
634
635 if (ret == 0) {
636 *pst = st;
637 } else {
638 SET_STAT_INVALID(st);
639 }
640 }
641
642 TALLOC_FREE(found_name);
643 } /* end else */
644
645#ifdef DEVELOPER
646 /*
647 * This sucks!
648 * We should never provide different behaviors
649 * depending on DEVELOPER!!!
650 */
651 if (VALID_STAT(st)) {
652 bool delete_pending;
653 get_file_infos(vfs_file_id_from_sbuf(conn, &st),
654 &delete_pending, NULL);
655 if (delete_pending) {
656 result = NT_STATUS_DELETE_PENDING;
657 goto fail;
658 }
659 }
660#endif
661
662 /*
663 * Add to the dirpath that we have resolved so far.
664 */
665
666 if (*dirpath != '\0') {
667 char *tmp = talloc_asprintf(ctx,
668 "%s/%s", dirpath, start);
669 if (!tmp) {
670 DEBUG(0, ("talloc_asprintf failed\n"));
671 return NT_STATUS_NO_MEMORY;
672 }
673 TALLOC_FREE(dirpath);
674 dirpath = tmp;
675 }
676 else {
677 TALLOC_FREE(dirpath);
678 if (!(dirpath = talloc_strdup(ctx,start))) {
679 DEBUG(0, ("talloc_strdup failed\n"));
680 return NT_STATUS_NO_MEMORY;
681 }
682 }
683
684 /*
685 * Don't cache a name with mangled or wildcard components
686 * as this can change the size.
687 */
688
689 if(!component_was_mangled && !name_has_wildcard) {
690 stat_cache_add(orig_path, dirpath,
691 conn->case_sensitive);
692 }
693
694 /*
695 * Restore the / that we wiped out earlier.
696 */
697 if (end) {
698 *end = '/';
699 }
700 }
701
702 /*
703 * Don't cache a name with mangled or wildcard components
704 * as this can change the size.
705 */
706
707 if(!component_was_mangled && !name_has_wildcard) {
708 stat_cache_add(orig_path, name, conn->case_sensitive);
709 }
710
711 /*
712 * The name has been resolved.
713 */
714
715 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
716
717 done:
718 if (stream != NULL) {
719 char *tmp = NULL;
720
721 result = build_stream_path(ctx, conn, orig_path, name, stream,
722 pst, &tmp);
723 if (!NT_STATUS_IS_OK(result)) {
724 goto fail;
725 }
726
727 DEBUG(10, ("build_stream_path returned %s\n", tmp));
728
729 TALLOC_FREE(name);
730 name = tmp;
731 }
732 *pp_conv_path = name;
733 TALLOC_FREE(dirpath);
734 return NT_STATUS_OK;
735 fail:
736 DEBUG(10, ("dirpath = [%s] start = [%s]\n", dirpath, start));
737 if (*dirpath != '\0') {
738 *pp_conv_path = talloc_asprintf(ctx,
739 "%s/%s", dirpath, start);
740 } else {
741 *pp_conv_path = talloc_strdup(ctx, start);
742 }
743 if (!*pp_conv_path) {
744 DEBUG(0, ("talloc_asprintf failed\n"));
745 return NT_STATUS_NO_MEMORY;
746 }
747 TALLOC_FREE(name);
748 TALLOC_FREE(dirpath);
749 return result;
750}
751
752/****************************************************************************
753 Check a filename - possibly calling check_reduced_name.
754 This is called by every routine before it allows an operation on a filename.
755 It does any final confirmation necessary to ensure that the filename is
756 a valid one for the user to access.
757****************************************************************************/
758
759NTSTATUS check_name(connection_struct *conn, const char *name)
760{
761 if (IS_VETO_PATH(conn, name)) {
762 /* Is it not dot or dot dot. */
763 if (!((name[0] == '.') && (!name[1] ||
764 (name[1] == '.' && !name[2])))) {
765 DEBUG(5,("check_name: file path name %s vetoed\n",
766 name));
767 return map_nt_error_from_unix(ENOENT);
768 }
769 }
770
771 if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
772 NTSTATUS status = check_reduced_name(conn,name);
773 if (!NT_STATUS_IS_OK(status)) {
774 DEBUG(5,("check_name: name %s failed with %s\n",name,
775 nt_errstr(status)));
776 return status;
777 }
778 }
779
780 return NT_STATUS_OK;
781}
782
783/****************************************************************************
784 Check if two filenames are equal.
785 This needs to be careful about whether we are case sensitive.
786****************************************************************************/
787
788static bool fname_equal(const char *name1, const char *name2,
789 bool case_sensitive)
790{
791 /* Normal filename handling */
792 if (case_sensitive) {
793 return(strcmp(name1,name2) == 0);
794 }
795
796 return(strequal(name1,name2));
797}
798
799/****************************************************************************
800 Scan a directory to find a filename, matching without case sensitivity.
801 If the name looks like a mangled name then try via the mangling functions
802****************************************************************************/
803
804static int get_real_filename_mangled(connection_struct *conn, const char *path,
805 const char *name, TALLOC_CTX *mem_ctx,
806 char **found_name)
807{
808 bool mangled;
809 char *unmangled_name = NULL;
810
811 mangled = mangle_is_mangled(name, conn->params);
812
813 /* handle null paths */
814 if ((path == NULL) || (*path == 0)) {
815 path = ".";
816 }
817
818 /* If we have a case-sensitive filesystem, it doesn't do us any
819 * good to search for a name. If a case variation of the name was
820 * there, then the original stat(2) would have found it.
821 */
822 if (!mangled && !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) {
823 errno = ENOENT;
824 return -1;
825 }
826
827 /*
828 * The incoming name can be mangled, and if we de-mangle it
829 * here it will not compare correctly against the filename (name2)
830 * read from the directory and then mangled by the name_to_8_3()
831 * call. We need to mangle both names or neither.
832 * (JRA).
833 *
834 * Fix for bug found by Dina Fine. If in case sensitive mode then
835 * the mangle cache is no good (3 letter extension could be wrong
836 * case - so don't demangle in this case - leave as mangled and
837 * allow the mangling of the directory entry read (which is done
838 * case insensitively) to match instead. This will lead to more
839 * false positive matches but we fail completely without it. JRA.
840 */
841
842 if (mangled && !conn->case_sensitive) {
843 mangled = !mangle_lookup_name_from_8_3(talloc_tos(), name,
844 &unmangled_name,
845 conn->params);
846 if (!mangled) {
847 /* Name is now unmangled. */
848 name = unmangled_name;
849 } else {
850 /*
851 * If we have mangled names, do not ask the VFS'es
852 * GET_REAL_FILENAME. The Unix file system below does
853 * not know about Samba's style of mangling.
854 *
855 * Boolean flags passed down are evil, the alternative
856 * would be to pass a comparison function down into
857 * the loop in get_real_filename_internal(). For now,
858 * do the quick&dirty boolean flag approach.
859 */
860 return get_real_filename_internal(conn, path, name,
861 true,
862 mem_ctx, found_name);
863 }
864 }
865
866 return SMB_VFS_GET_REAL_FILENAME(conn, path, name, mem_ctx,
867 found_name);
868}
869
870static int get_real_filename_internal(connection_struct *conn,
871 const char *path, const char *name,
872 bool mangled,
873 TALLOC_CTX *mem_ctx, char **found_name)
874{
875 struct smb_Dir *cur_dir;
876 const char *dname;
877 char *unmangled_name = NULL;
878 long curpos;
879
880 /* open the directory */
881 if (!(cur_dir = OpenDir(talloc_tos(), conn, path, NULL, 0))) {
882 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
883 TALLOC_FREE(unmangled_name);
884 return -1;
885 }
886
887 /* now scan for matching names */
888 curpos = 0;
889 while ((dname = ReadDirName(cur_dir, &curpos))) {
890
891 /* Is it dot or dot dot. */
892 if (ISDOT(dname) || ISDOTDOT(dname)) {
893 continue;
894 }
895
896 /*
897 * At this point dname is the unmangled name.
898 * name is either mangled or not, depending on the state
899 * of the "mangled" variable. JRA.
900 */
901
902 /*
903 * Check mangled name against mangled name, or unmangled name
904 * against unmangled name.
905 */
906
907 if ((mangled && mangled_equal(name,dname,conn->params)) ||
908 fname_equal(name, dname, conn->case_sensitive)) {
909 /* we've found the file, change it's name and return */
910 *found_name = talloc_strdup(mem_ctx, dname);
911 TALLOC_FREE(unmangled_name);
912 TALLOC_FREE(cur_dir);
913 if (!*found_name) {
914 errno = ENOMEM;
915 return -1;
916 }
917 return 0;
918 }
919 }
920
921 TALLOC_FREE(unmangled_name);
922 TALLOC_FREE(cur_dir);
923 errno = ENOENT;
924 return -1;
925}
926
927
928
929int get_real_filename(connection_struct *conn,
930 const char *path, const char *name,
931 TALLOC_CTX *mem_ctx, char **found_name)
932{
933 /*
934 * This is the default VFS function. If we end up here, we know we
935 * don't have mangled names around.
936 */
937 return get_real_filename_internal(conn, path, name, false,
938 mem_ctx, found_name);
939}
940
941static NTSTATUS build_stream_path(TALLOC_CTX *mem_ctx,
942 connection_struct *conn,
943 const char *orig_path,
944 const char *basepath,
945 const char *streamname,
946 SMB_STRUCT_STAT *pst,
947 char **path)
948{
949 SMB_STRUCT_STAT st;
950 char *result = NULL;
951 NTSTATUS status;
952 unsigned int i, num_streams;
953 struct stream_struct *streams = NULL;
954
955 result = talloc_asprintf(mem_ctx, "%s%s", basepath, streamname);
956 if (result == NULL) {
957 return NT_STATUS_NO_MEMORY;
958 }
959
960 if (SMB_VFS_STAT(conn, result, &st) == 0) {
961 *pst = st;
962 *path = result;
963 return NT_STATUS_OK;
964 }
965
966 if (errno != ENOENT) {
967 status = map_nt_error_from_unix(errno);
968 DEBUG(10, ("vfs_stat failed: %s\n", nt_errstr(status)));
969 goto fail;
970 }
971
972 status = SMB_VFS_STREAMINFO(conn, NULL, basepath, mem_ctx,
973 &num_streams, &streams);
974
975 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
976 SET_STAT_INVALID(*pst);
977 *path = result;
978 return NT_STATUS_OK;
979 }
980
981 if (!NT_STATUS_IS_OK(status)) {
982 DEBUG(10, ("vfs_streaminfo failed: %s\n", nt_errstr(status)));
983 goto fail;
984 }
985
986 for (i=0; i<num_streams; i++) {
987 DEBUG(10, ("comparing [%s] and [%s]: ",
988 streamname, streams[i].name));
989 if (fname_equal(streamname, streams[i].name,
990 conn->case_sensitive)) {
991 DEBUGADD(10, ("equal\n"));
992 break;
993 }
994 DEBUGADD(10, ("not equal\n"));
995 }
996
997 if (i == num_streams) {
998 SET_STAT_INVALID(*pst);
999 *path = result;
1000 TALLOC_FREE(streams);
1001 return NT_STATUS_OK;
1002 }
1003
1004 TALLOC_FREE(result);
1005
1006 result = talloc_asprintf(mem_ctx, "%s%s", basepath, streams[i].name);
1007 if (result == NULL) {
1008 status = NT_STATUS_NO_MEMORY;
1009 goto fail;
1010 }
1011
1012 SET_STAT_INVALID(*pst);
1013
1014 if (SMB_VFS_STAT(conn, result, pst) == 0) {
1015 stat_cache_add(orig_path, result, conn->case_sensitive);
1016 }
1017
1018 *path = result;
1019 TALLOC_FREE(streams);
1020 return NT_STATUS_OK;
1021
1022 fail:
1023 TALLOC_FREE(result);
1024 TALLOC_FREE(streams);
1025 return status;
1026}
Note: See TracBrowser for help on using the repository browser.