Merge commit 'samba/v3-2-test' into v3-2-stable
[samba.git] / source3 / smbd / filename.c
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
29 static bool scan_directory(connection_struct *conn, const char *path,
30                            char *name, char **found_name);
31
32 /****************************************************************************
33  Mangle the 2nd name and check if it is then equal to the first name.
34 ****************************************************************************/
35
36 static bool mangled_equal(const char *name1,
37                         const char *name2,
38                         const struct share_params *p)
39 {
40         char mname[13];
41
42         if (!name_to_8_3(name2, mname, False, p)) {
43                 return False;
44         }
45         return strequal(name1, mname);
46 }
47
48 /****************************************************************************
49  Cope with the differing wildcard and non-wildcard error cases.
50 ****************************************************************************/
51
52 static NTSTATUS determine_path_error(const char *name,
53                         bool allow_wcard_last_component)
54 {
55         const char *p;
56
57         if (!allow_wcard_last_component) {
58                 /* Error code within a pathname. */
59                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
60         }
61
62         /* We're terminating here so we
63          * can be a little slower and get
64          * the error code right. Windows
65          * treats the last part of the pathname
66          * separately I think, so if the last
67          * component is a wildcard then we treat
68          * this ./ as "end of component" */
69
70         p = strchr(name, '/');
71
72         if (!p && (ms_has_wild(name) || ISDOT(name))) {
73                 /* Error code at the end of a pathname. */
74                 return NT_STATUS_OBJECT_NAME_INVALID;
75         } else {
76                 /* Error code within a pathname. */
77                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
78         }
79 }
80
81 /****************************************************************************
82 This routine is called to convert names from the dos namespace to unix
83 namespace. It needs to handle any case conversions, mangling, format
84 changes etc.
85
86 We assume that we have already done a chdir() to the right "root" directory
87 for this service.
88
89 The function will return an NTSTATUS error if some part of the name except for
90 the last part cannot be resolved, else NT_STATUS_OK.
91
92 Note NT_STATUS_OK doesn't mean the name exists or is valid, just that we didn't
93 get any fatal errors that should immediately terminate the calling
94 SMB processing whilst resolving.
95
96 If the saved_last_component != 0, then the unmodified last component
97 of the pathname is returned there. This is used in an exceptional
98 case in reply_mv (so far). If saved_last_component == 0 then nothing
99 is returned there.
100
101 If last_component_wcard is true then a MS wildcard was detected and
102 should be allowed in the last component of the path only.
103
104 On exit from unix_convert, if *pst was not null, then the file stat
105 struct will be returned if the file exists and was found, if not this
106 stat struct will be filled with zeros (and this can be detected by checking
107 for nlinks = 0, which can never be true for any file).
108 ****************************************************************************/
109
110 NTSTATUS unix_convert(TALLOC_CTX *ctx,
111                         connection_struct *conn,
112                         const char *orig_path,
113                         bool allow_wcard_last_component,
114                         char **pp_conv_path,
115                         char **pp_saved_last_component,
116                         SMB_STRUCT_STAT *pst)
117 {
118         SMB_STRUCT_STAT st;
119         char *start, *end;
120         char *dirpath = NULL;
121         char *name = NULL;
122         bool component_was_mangled = False;
123         bool name_has_wildcard = False;
124         NTSTATUS result;
125
126         SET_STAT_INVALID(*pst);
127         *pp_conv_path = NULL;
128         if(pp_saved_last_component) {
129                 *pp_saved_last_component = NULL;
130         }
131
132         if (conn->printer) {
133                 /* we don't ever use the filenames on a printer share as a
134                         filename - so don't convert them */
135                 if (!(*pp_conv_path = talloc_strdup(ctx,orig_path))) {
136                         return NT_STATUS_NO_MEMORY;
137                 }
138                 return NT_STATUS_OK;
139         }
140
141         DEBUG(5, ("unix_convert called on file \"%s\"\n", orig_path));
142
143         /*
144          * Conversion to basic unix format is already done in
145          * check_path_syntax().
146          */
147
148         /*
149          * Names must be relative to the root of the service - any leading /.
150          * and trailing /'s should have been trimmed by check_path_syntax().
151          */
152
153 #ifdef DEVELOPER
154         SMB_ASSERT(*orig_path != '/');
155 #endif
156
157         /*
158          * If we trimmed down to a single '\0' character
159          * then we should use the "." directory to avoid
160          * searching the cache, but not if we are in a
161          * printing share.
162          * As we know this is valid we can return true here.
163          */
164
165         if (!*orig_path) {
166                 if (!(name = talloc_strdup(ctx,"."))) {
167                         return NT_STATUS_NO_MEMORY;
168                 }
169                 if (SMB_VFS_STAT(conn,name,&st) == 0) {
170                         *pst = st;
171                 } else {
172                         return map_nt_error_from_unix(errno);
173                 }
174                 DEBUG(5,("conversion finished \"\" -> %s\n",name));
175                 goto done;
176         }
177
178         if (orig_path[0] == '.' && (orig_path[1] == '/' ||
179                                 orig_path[1] == '\0')) {
180                 /* Start of pathname can't be "." only. */
181                 if (orig_path[1] == '\0' || orig_path[2] == '\0') {
182                         result = NT_STATUS_OBJECT_NAME_INVALID;
183                 } else {
184                         result =determine_path_error(
185                                 &orig_path[2], allow_wcard_last_component);
186                 }
187                 return result;
188         }
189
190         /*
191          * Ensure saved_last_component is valid even if file exists.
192          */
193
194         if(pp_saved_last_component) {
195                 end = strrchr_m(orig_path, '/');
196                 if (end) {
197                         *pp_saved_last_component = talloc_strdup(ctx, end + 1);
198                 } else {
199                         *pp_saved_last_component = talloc_strdup(ctx,
200                                                         orig_path);
201                 }
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         start = name;
224
225         /* If we're providing case insentive semantics or
226          * the underlying filesystem is case insensitive,
227          * then a case-normalized hit in the stat-cache is
228          * authoratitive. JRA.
229          */
230
231         if((!conn->case_sensitive || !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) &&
232                         stat_cache_lookup(conn, &name, &dirpath, &start, &st)) {
233                 *pst = st;
234                 goto done;
235         }
236
237         /*
238          * Make sure "dirpath" is an allocated string, we use this for
239          * building the directories with asprintf and free it.
240          */
241
242         if ((dirpath == NULL) && (!(dirpath = talloc_strdup(ctx,"")))) {
243                 DEBUG(0, ("talloc_strdup failed\n"));
244                 TALLOC_FREE(name);
245                 return NT_STATUS_NO_MEMORY;
246         }
247
248         /*
249          * stat the name - if it exists then we are all done!
250          */
251
252         if (SMB_VFS_STAT(conn,name,&st) == 0) {
253                 /* Ensure we catch all names with in "/."
254                    this is disallowed under Windows. */
255                 const char *p = strstr(name, "/."); /* mb safe. */
256                 if (p) {
257                         if (p[2] == '/') {
258                                 /* Error code within a pathname. */
259                                 result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
260                                 goto fail;
261                         } else if (p[2] == '\0') {
262                                 /* Error code at the end of a pathname. */
263                                 result = NT_STATUS_OBJECT_NAME_INVALID;
264                                 goto fail;
265                         }
266                 }
267                 stat_cache_add(orig_path, name, conn->case_sensitive);
268                 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
269                 *pst = st;
270                 goto done;
271         }
272
273         DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n",
274                                 name, dirpath, start));
275
276         /*
277          * A special case - if we don't have any mangling chars and are case
278          * sensitive or the underlying filesystem is case insentive then searching
279          * won't help.
280          */
281
282         if ((conn->case_sensitive || !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) &&
283                         !mangle_is_mangled(name, conn->params)) {
284                 goto done;
285         }
286
287         /*
288          * is_mangled() was changed to look at an entire pathname, not
289          * just a component. JRA.
290          */
291
292         if (mangle_is_mangled(start, conn->params)) {
293                 component_was_mangled = True;
294         }
295
296         /*
297          * Now we need to recursively match the name against the real
298          * directory structure.
299          */
300
301         /*
302          * Match each part of the path name separately, trying the names
303          * as is first, then trying to scan the directory for matching names.
304          */
305
306         for (; start ; start = (end?end+1:(char *)NULL)) {
307                 /*
308                  * Pinpoint the end of this section of the filename.
309                  */
310                 /* mb safe. '/' can't be in any encoded char. */
311                 end = strchr(start, '/');
312
313                 /*
314                  * Chop the name at this point.
315                  */
316                 if (end) {
317                         *end = 0;
318                 }
319
320                 if (pp_saved_last_component) {
321                         TALLOC_FREE(*pp_saved_last_component);
322                         *pp_saved_last_component = talloc_strdup(ctx,
323                                                         end ? end + 1 : start);
324                         if (!*pp_saved_last_component) {
325                                 DEBUG(0, ("talloc failed\n"));
326                                 return NT_STATUS_NO_MEMORY;
327                         }
328                 }
329
330                 /* The name cannot have a component of "." */
331
332                 if (ISDOT(start)) {
333                         if (!end)  {
334                                 /* Error code at the end of a pathname. */
335                                 result = NT_STATUS_OBJECT_NAME_INVALID;
336                         } else {
337                                 result = determine_path_error(end+1,
338                                                 allow_wcard_last_component);
339                         }
340                         goto fail;
341                 }
342
343                 /* The name cannot have a wildcard if it's not
344                    the last component. */
345
346                 name_has_wildcard = ms_has_wild(start);
347
348                 /* Wildcard not valid anywhere. */
349                 if (name_has_wildcard && !allow_wcard_last_component) {
350                         result = NT_STATUS_OBJECT_NAME_INVALID;
351                         goto fail;
352                 }
353
354                 /* Wildcards never valid within a pathname. */
355                 if (name_has_wildcard && end) {
356                         result = NT_STATUS_OBJECT_NAME_INVALID;
357                         goto fail;
358                 }
359
360                 /*
361                  * Check if the name exists up to this point.
362                  */
363
364                 if (SMB_VFS_STAT(conn,name, &st) == 0) {
365                         /*
366                          * It exists. it must either be a directory or this must
367                          * be the last part of the path for it to be OK.
368                          */
369                         if (end && !(st.st_mode & S_IFDIR)) {
370                                 /*
371                                  * An intermediate part of the name isn't
372                                  * a directory.
373                                  */
374                                 DEBUG(5,("Not a dir %s\n",start));
375                                 *end = '/';
376                                 /*
377                                  * We need to return the fact that the
378                                  * intermediate name resolution failed. This
379                                  * is used to return an error of ERRbadpath
380                                  * rather than ERRbadfile. Some Windows
381                                  * applications depend on the difference between
382                                  * these two errors.
383                                  */
384                                 result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
385                                 goto fail;
386                         }
387
388                         if (!end) {
389                                 /*
390                                  * We just scanned for, and found the end of
391                                  * the path. We must return the valid stat
392                                  * struct. JRA.
393                                  */
394
395                                 *pst = st;
396                         }
397
398                 } else {
399                         char *found_name = NULL;
400
401                         /* Stat failed - ensure we don't use it. */
402                         SET_STAT_INVALID(st);
403
404                         /*
405                          * Reset errno so we can detect
406                          * directory open errors.
407                          */
408                         errno = 0;
409
410                         /*
411                          * Try to find this part of the path in the directory.
412                          */
413
414                         if (name_has_wildcard ||
415                             !scan_directory(conn, dirpath,
416                                     start, &found_name)) {
417                                 char *unmangled;
418
419                                 if (end) {
420                                         /*
421                                          * An intermediate part of the name
422                                          * can't be found.
423                                          */
424                                         DEBUG(5,("Intermediate not found %s\n",
425                                                         start));
426                                         *end = '/';
427
428                                         /*
429                                          * We need to return the fact that the
430                                          * intermediate name resolution failed.
431                                          * This is used to return an error of
432                                          * ERRbadpath rather than ERRbadfile.
433                                          * Some Windows applications depend on
434                                          * the difference between these two
435                                          * errors.
436                                          */
437
438                                         /*
439                                          * ENOENT, ENOTDIR and ELOOP all map
440                                          * to NT_STATUS_OBJECT_PATH_NOT_FOUND
441                                          * in the filename walk.
442                                          */
443
444                                         if (errno == ENOENT ||
445                                                         errno == ENOTDIR ||
446                                                         errno == ELOOP) {
447                                                 result =
448                                                 NT_STATUS_OBJECT_PATH_NOT_FOUND;
449                                         }
450                                         else {
451                                                 result =
452                                                 map_nt_error_from_unix(errno);
453                                         }
454                                         goto fail;
455                                 }
456
457                                 /* ENOENT is the only valid error here. */
458                                 if (errno != ENOENT) {
459                                         /*
460                                          * ENOTDIR and ELOOP both map to
461                                          * NT_STATUS_OBJECT_PATH_NOT_FOUND
462                                          * in the filename walk.
463                                          */
464                                         if (errno == ENOTDIR ||
465                                                         errno == ELOOP) {
466                                                 result =
467                                                 NT_STATUS_OBJECT_PATH_NOT_FOUND;
468                                         }
469                                         else {
470                                                 result =
471                                                 map_nt_error_from_unix(errno);
472                                         }
473                                         goto fail;
474                                 }
475
476                                 /*
477                                  * Just the last part of the name doesn't exist.
478                                  * We need to strupper() or strlower() it as
479                                  * this conversion may be used for file creation
480                                  * purposes. Fix inspired by
481                                  * Thomas Neumann <t.neumann@iku-ag.de>.
482                                  */
483                                 if (!conn->case_preserve ||
484                                     (mangle_is_8_3(start, False,
485                                                    conn->params) &&
486                                                  !conn->short_case_preserve)) {
487                                         strnorm(start,
488                                                 lp_defaultcase(SNUM(conn)));
489                                 }
490
491                                 /*
492                                  * check on the mangled stack to see if we can
493                                  * recover the base of the filename.
494                                  */
495
496                                 if (mangle_is_mangled(start, conn->params)
497                                     && mangle_lookup_name_from_8_3(ctx,
498                                                         start,
499                                                         &unmangled,
500                                                         conn->params)) {
501                                         char *tmp;
502                                         size_t start_ofs = start - name;
503
504                                         if (*dirpath != '\0') {
505                                                 tmp = talloc_asprintf(ctx,
506                                                         "%s/%s", dirpath,
507                                                         unmangled);
508                                                 TALLOC_FREE(unmangled);
509                                         }
510                                         else {
511                                                 tmp = unmangled;
512                                         }
513                                         if (tmp == NULL) {
514                                                 DEBUG(0, ("talloc failed\n"));
515                                                 return NT_STATUS_NO_MEMORY;
516                                         }
517                                         TALLOC_FREE(name);
518                                         name = tmp;
519                                         start = name + start_ofs;
520                                         end = start + strlen(start);
521                                 }
522
523                                 DEBUG(5,("New file %s\n",start));
524                                 goto done;
525                         }
526
527
528                         /*
529                          * Restore the rest of the string. If the string was
530                          * mangled the size may have changed.
531                          */
532                         if (end) {
533                                 char *tmp;
534                                 size_t start_ofs = start - name;
535
536                                 if (*dirpath != '\0') {
537                                         tmp = talloc_asprintf(ctx,
538                                                 "%s/%s/%s", dirpath,
539                                                 found_name, end+1);
540                                 }
541                                 else {
542                                         tmp = talloc_asprintf(ctx,
543                                                 "%s/%s", found_name,
544                                                 end+1);
545                                 }
546                                 if (tmp == NULL) {
547                                         DEBUG(0, ("talloc_asprintf failed\n"));
548                                         return NT_STATUS_NO_MEMORY;
549                                 }
550                                 TALLOC_FREE(name);
551                                 name = tmp;
552                                 start = name + start_ofs;
553                                 end = start + strlen(found_name);
554                                 *end = '\0';
555                         } else {
556                                 char *tmp;
557                                 size_t start_ofs = start - name;
558
559                                 if (*dirpath != '\0') {
560                                         tmp = talloc_asprintf(ctx,
561                                                 "%s/%s", dirpath,
562                                                 found_name);
563                                 } else {
564                                         tmp = talloc_strdup(ctx,
565                                                 found_name);
566                                 }
567                                 if (tmp == NULL) {
568                                         DEBUG(0, ("talloc failed\n"));
569                                         return NT_STATUS_NO_MEMORY;
570                                 }
571                                 TALLOC_FREE(name);
572                                 name = tmp;
573                                 start = name + start_ofs;
574
575                                 /*
576                                  * We just scanned for, and found the end of
577                                  * the path. We must return a valid stat struct
578                                  * if it exists. JRA.
579                                  */
580
581                                 if (SMB_VFS_STAT(conn,name, &st) == 0) {
582                                         *pst = st;
583                                 } else {
584                                         SET_STAT_INVALID(st);
585                                 }
586                         }
587
588                         TALLOC_FREE(found_name);
589                 } /* end else */
590
591 #ifdef DEVELOPER
592                 if (VALID_STAT(st) &&
593                     get_delete_on_close_flag(vfs_file_id_from_sbuf(conn,
594                                     &st))) {
595                         result = NT_STATUS_DELETE_PENDING;
596                         goto fail;
597                 }
598 #endif
599
600                 /*
601                  * Add to the dirpath that we have resolved so far.
602                  */
603
604                 if (*dirpath != '\0') {
605                         char *tmp = talloc_asprintf(ctx,
606                                         "%s/%s", dirpath, start);
607                         if (!tmp) {
608                                 DEBUG(0, ("talloc_asprintf failed\n"));
609                                 return NT_STATUS_NO_MEMORY;
610                         }
611                         TALLOC_FREE(dirpath);
612                         dirpath = tmp;
613                 }
614                 else {
615                         TALLOC_FREE(dirpath);
616                         if (!(dirpath = talloc_strdup(ctx,start))) {
617                                 DEBUG(0, ("talloc_strdup failed\n"));
618                                 return NT_STATUS_NO_MEMORY;
619                         }
620                 }
621
622                 /*
623                  * Don't cache a name with mangled or wildcard components
624                  * as this can change the size.
625                  */
626
627                 if(!component_was_mangled && !name_has_wildcard) {
628                         stat_cache_add(orig_path, dirpath,
629                                         conn->case_sensitive);
630                 }
631
632                 /*
633                  * Restore the / that we wiped out earlier.
634                  */
635                 if (end) {
636                         *end = '/';
637                 }
638         }
639
640         /*
641          * Don't cache a name with mangled or wildcard components
642          * as this can change the size.
643          */
644
645         if(!component_was_mangled && !name_has_wildcard) {
646                 stat_cache_add(orig_path, name, conn->case_sensitive);
647         }
648
649         /*
650          * The name has been resolved.
651          */
652
653         DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
654
655  done:
656         *pp_conv_path = name;
657         TALLOC_FREE(dirpath);
658         return NT_STATUS_OK;
659  fail:
660         DEBUG(10, ("dirpath = [%s] start = [%s]\n", dirpath, start));
661         if (*dirpath != '\0') {
662                 *pp_conv_path = talloc_asprintf(ctx,
663                                 "%s/%s", dirpath, start);
664         } else {
665                 *pp_conv_path = talloc_strdup(ctx, start);
666         }
667         if (!*pp_conv_path) {
668                 DEBUG(0, ("talloc_asprintf failed\n"));
669                 return NT_STATUS_NO_MEMORY;
670         }
671         TALLOC_FREE(name);
672         TALLOC_FREE(dirpath);
673         return result;
674 }
675
676 /****************************************************************************
677  Check a filename - possibly calling check_reduced_name.
678  This is called by every routine before it allows an operation on a filename.
679  It does any final confirmation necessary to ensure that the filename is
680  a valid one for the user to access.
681 ****************************************************************************/
682
683 NTSTATUS check_name(connection_struct *conn, const char *name)
684 {
685         if (IS_VETO_PATH(conn, name))  {
686                 /* Is it not dot or dot dot. */
687                 if (!((name[0] == '.') && (!name[1] ||
688                                         (name[1] == '.' && !name[2])))) {
689                         DEBUG(5,("check_name: file path name %s vetoed\n",
690                                                 name));
691                         return map_nt_error_from_unix(ENOENT);
692                 }
693         }
694
695         if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
696                 NTSTATUS status = check_reduced_name(conn,name);
697                 if (!NT_STATUS_IS_OK(status)) {
698                         DEBUG(5,("check_name: name %s failed with %s\n",name,
699                                                 nt_errstr(status)));
700                         return status;
701                 }
702         }
703
704         return NT_STATUS_OK;
705 }
706
707 /****************************************************************************
708  Check if two filenames are equal.
709  This needs to be careful about whether we are case sensitive.
710 ****************************************************************************/
711
712 static bool fname_equal(const char *name1, const char *name2,
713                 bool case_sensitive)
714 {
715         /* Normal filename handling */
716         if (case_sensitive) {
717                 return(strcmp(name1,name2) == 0);
718         }
719
720         return(strequal(name1,name2));
721 }
722
723 /****************************************************************************
724  Scan a directory to find a filename, matching without case sensitivity.
725  If the name looks like a mangled name then try via the mangling functions
726 ****************************************************************************/
727
728 static bool scan_directory(connection_struct *conn, const char *path,
729                            char *name, char **found_name)
730 {
731         struct smb_Dir *cur_dir;
732         const char *dname;
733         bool mangled;
734         char *unmangled_name = NULL;
735         long curpos;
736         TALLOC_CTX *ctx = talloc_tos();
737
738         mangled = mangle_is_mangled(name, conn->params);
739
740         /* handle null paths */
741         if ((path == NULL) || (*path == 0)) {
742                 path = ".";
743         }
744
745         /* If we have a case-sensitive filesystem, it doesn't do us any
746          * good to search for a name. If a case variation of the name was
747          * there, then the original stat(2) would have found it.
748          */
749         if (!mangled && !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) {
750                 errno = ENOENT;
751                 return False;
752         }
753
754         /*
755          * The incoming name can be mangled, and if we de-mangle it
756          * here it will not compare correctly against the filename (name2)
757          * read from the directory and then mangled by the name_to_8_3()
758          * call. We need to mangle both names or neither.
759          * (JRA).
760          *
761          * Fix for bug found by Dina Fine. If in case sensitive mode then
762          * the mangle cache is no good (3 letter extension could be wrong
763          * case - so don't demangle in this case - leave as mangled and
764          * allow the mangling of the directory entry read (which is done
765          * case insensitively) to match instead. This will lead to more
766          * false positive matches but we fail completely without it. JRA.
767          */
768
769         if (mangled && !conn->case_sensitive) {
770                 mangled = !mangle_lookup_name_from_8_3(ctx,
771                                                 name,
772                                                 &unmangled_name,
773                                                 conn->params);
774                 if (!mangled) {
775                         /* Name is now unmangled. */
776                         name = unmangled_name;
777                 }
778         }
779
780         /* open the directory */
781         if (!(cur_dir = OpenDir(talloc_tos(), conn, path, NULL, 0))) {
782                 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
783                 TALLOC_FREE(unmangled_name);
784                 return(False);
785         }
786
787         /* now scan for matching names */
788         curpos = 0;
789         while ((dname = ReadDirName(cur_dir, &curpos))) {
790
791                 /* Is it dot or dot dot. */
792                 if (ISDOT(dname) || ISDOTDOT(dname)) {
793                         continue;
794                 }
795
796                 /*
797                  * At this point dname is the unmangled name.
798                  * name is either mangled or not, depending on the state
799                  * of the "mangled" variable. JRA.
800                  */
801
802                 /*
803                  * Check mangled name against mangled name, or unmangled name
804                  * against unmangled name.
805                  */
806
807                 if ((mangled && mangled_equal(name,dname,conn->params)) ||
808                         fname_equal(name, dname, conn->case_sensitive)) {
809                         /* we've found the file, change it's name and return */
810                         *found_name = talloc_strdup(ctx,dname);
811                         TALLOC_FREE(unmangled_name);
812                         TALLOC_FREE(cur_dir);
813                         if (!*found_name) {
814                                 errno = ENOMEM;
815                                 return False;
816                         }
817                         return(True);
818                 }
819         }
820
821         TALLOC_FREE(unmangled_name);
822         TALLOC_FREE(cur_dir);
823         errno = ENOENT;
824         return False;
825 }