RIP BOOL. Convert BOOL -> bool. I found a few interesting
[kai/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(!conn->case_sensitive
226            && stat_cache_lookup(conn, &name, &dirpath, &start, &st)) {
227                 *pst = st;
228                 goto done;
229         }
230
231         /*
232          * Make sure "dirpath" is an allocated string, we use this for
233          * building the directories with asprintf and free it.
234          */
235
236         if ((dirpath == NULL) && (!(dirpath = talloc_strdup(ctx,"")))) {
237                 DEBUG(0, ("talloc_strdup failed\n"));
238                 TALLOC_FREE(name);
239                 return NT_STATUS_NO_MEMORY;
240         }
241
242         /*
243          * stat the name - if it exists then we are all done!
244          */
245
246         if (SMB_VFS_STAT(conn,name,&st) == 0) {
247                 /* Ensure we catch all names with in "/."
248                    this is disallowed under Windows. */
249                 const char *p = strstr(name, "/."); /* mb safe. */
250                 if (p) {
251                         if (p[2] == '/') {
252                                 /* Error code within a pathname. */
253                                 result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
254                                 goto fail;
255                         } else if (p[2] == '\0') {
256                                 /* Error code at the end of a pathname. */
257                                 result = NT_STATUS_OBJECT_NAME_INVALID;
258                                 goto fail;
259                         }
260                 }
261                 stat_cache_add(orig_path, name, conn->case_sensitive);
262                 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
263                 *pst = st;
264                 goto done;
265         }
266
267         DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n",
268                                 name, dirpath, start));
269
270         /*
271          * A special case - if we don't have any mangling chars and are case
272          * sensitive then searching won't help.
273          */
274
275         if (conn->case_sensitive &&
276                         !mangle_is_mangled(name, conn->params)) {
277                 goto done;
278         }
279
280         /*
281          * is_mangled() was changed to look at an entire pathname, not
282          * just a component. JRA.
283          */
284
285         if (mangle_is_mangled(start, conn->params)) {
286                 component_was_mangled = True;
287         }
288
289         /*
290          * Now we need to recursively match the name against the real
291          * directory structure.
292          */
293
294         /*
295          * Match each part of the path name separately, trying the names
296          * as is first, then trying to scan the directory for matching names.
297          */
298
299         for (; start ; start = (end?end+1:(char *)NULL)) {
300                 /*
301                  * Pinpoint the end of this section of the filename.
302                  */
303                 /* mb safe. '/' can't be in any encoded char. */
304                 end = strchr(start, '/');
305
306                 /*
307                  * Chop the name at this point.
308                  */
309                 if (end) {
310                         *end = 0;
311                 }
312
313                 if (pp_saved_last_component) {
314                         TALLOC_FREE(*pp_saved_last_component);
315                         *pp_saved_last_component = talloc_strdup(ctx,
316                                                         end ? end + 1 : start);
317                         if (!*pp_saved_last_component) {
318                                 DEBUG(0, ("talloc failed\n"));
319                                 return NT_STATUS_NO_MEMORY;
320                         }
321                 }
322
323                 /* The name cannot have a component of "." */
324
325                 if (ISDOT(start)) {
326                         if (!end)  {
327                                 /* Error code at the end of a pathname. */
328                                 result = NT_STATUS_OBJECT_NAME_INVALID;
329                         } else {
330                                 result = determine_path_error(end+1,
331                                                 allow_wcard_last_component);
332                         }
333                         goto fail;
334                 }
335
336                 /* The name cannot have a wildcard if it's not
337                    the last component. */
338
339                 name_has_wildcard = ms_has_wild(start);
340
341                 /* Wildcard not valid anywhere. */
342                 if (name_has_wildcard && !allow_wcard_last_component) {
343                         result = NT_STATUS_OBJECT_NAME_INVALID;
344                         goto fail;
345                 }
346
347                 /* Wildcards never valid within a pathname. */
348                 if (name_has_wildcard && end) {
349                         result = NT_STATUS_OBJECT_NAME_INVALID;
350                         goto fail;
351                 }
352
353                 /*
354                  * Check if the name exists up to this point.
355                  */
356
357                 if (SMB_VFS_STAT(conn,name, &st) == 0) {
358                         /*
359                          * It exists. it must either be a directory or this must
360                          * be the last part of the path for it to be OK.
361                          */
362                         if (end && !(st.st_mode & S_IFDIR)) {
363                                 /*
364                                  * An intermediate part of the name isn't
365                                  * a directory.
366                                  */
367                                 DEBUG(5,("Not a dir %s\n",start));
368                                 *end = '/';
369                                 /*
370                                  * We need to return the fact that the
371                                  * intermediate name resolution failed. This
372                                  * is used to return an error of ERRbadpath
373                                  * rather than ERRbadfile. Some Windows
374                                  * applications depend on the difference between
375                                  * these two errors.
376                                  */
377                                 result = NT_STATUS_OBJECT_PATH_NOT_FOUND;
378                                 goto fail;
379                         }
380
381                         if (!end) {
382                                 /*
383                                  * We just scanned for, and found the end of
384                                  * the path. We must return the valid stat
385                                  * struct. JRA.
386                                  */
387
388                                 *pst = st;
389                         }
390
391                 } else {
392                         char *found_name = NULL;
393
394                         /* Stat failed - ensure we don't use it. */
395                         SET_STAT_INVALID(st);
396
397                         /*
398                          * Reset errno so we can detect
399                          * directory open errors.
400                          */
401                         errno = 0;
402
403                         /*
404                          * Try to find this part of the path in the directory.
405                          */
406
407                         if (name_has_wildcard ||
408                             !scan_directory(conn, dirpath,
409                                     start, &found_name)) {
410                                 char *unmangled;
411
412                                 if (end) {
413                                         /*
414                                          * An intermediate part of the name
415                                          * can't be found.
416                                          */
417                                         DEBUG(5,("Intermediate not found %s\n",
418                                                         start));
419                                         *end = '/';
420
421                                         /*
422                                          * We need to return the fact that the
423                                          * intermediate name resolution failed.
424                                          * This is used to return an error of
425                                          * ERRbadpath rather than ERRbadfile.
426                                          * Some Windows applications depend on
427                                          * the difference between these two
428                                          * errors.
429                                          */
430
431                                         /*
432                                          * ENOENT, ENOTDIR and ELOOP all map
433                                          * to NT_STATUS_OBJECT_PATH_NOT_FOUND
434                                          * in the filename walk.
435                                          */
436
437                                         if (errno == ENOENT ||
438                                                         errno == ENOTDIR ||
439                                                         errno == ELOOP) {
440                                                 result =
441                                                 NT_STATUS_OBJECT_PATH_NOT_FOUND;
442                                         }
443                                         else {
444                                                 result =
445                                                 map_nt_error_from_unix(errno);
446                                         }
447                                         goto fail;
448                                 }
449
450                                 /* ENOENT is the only valid error here. */
451                                 if (errno != ENOENT) {
452                                         /*
453                                          * ENOTDIR and ELOOP both map to
454                                          * NT_STATUS_OBJECT_PATH_NOT_FOUND
455                                          * in the filename walk.
456                                          */
457                                         if (errno == ENOTDIR ||
458                                                         errno == ELOOP) {
459                                                 result =
460                                                 NT_STATUS_OBJECT_PATH_NOT_FOUND;
461                                         }
462                                         else {
463                                                 result =
464                                                 map_nt_error_from_unix(errno);
465                                         }
466                                         goto fail;
467                                 }
468
469                                 /*
470                                  * Just the last part of the name doesn't exist.
471                                  * We need to strupper() or strlower() it as
472                                  * this conversion may be used for file creation
473                                  * purposes. Fix inspired by
474                                  * Thomas Neumann <t.neumann@iku-ag.de>.
475                                  */
476                                 if (!conn->case_preserve ||
477                                     (mangle_is_8_3(start, False,
478                                                    conn->params) &&
479                                                  !conn->short_case_preserve)) {
480                                         strnorm(start,
481                                                 lp_defaultcase(SNUM(conn)));
482                                 }
483
484                                 /*
485                                  * check on the mangled stack to see if we can
486                                  * recover the base of the filename.
487                                  */
488
489                                 if (mangle_is_mangled(start, conn->params)
490                                     && mangle_lookup_name_from_8_3(ctx,
491                                                         start,
492                                                         &unmangled,
493                                                         conn->params)) {
494                                         char *tmp;
495                                         size_t start_ofs = start - name;
496
497                                         if (*dirpath != '\0') {
498                                                 tmp = talloc_asprintf(ctx,
499                                                         "%s/%s", dirpath,
500                                                         unmangled);
501                                                 TALLOC_FREE(unmangled);
502                                         }
503                                         else {
504                                                 tmp = unmangled;
505                                         }
506                                         if (tmp == NULL) {
507                                                 DEBUG(0, ("talloc failed\n"));
508                                                 return NT_STATUS_NO_MEMORY;
509                                         }
510                                         TALLOC_FREE(name);
511                                         name = tmp;
512                                         start = name + start_ofs;
513                                         end = start + strlen(start);
514                                 }
515
516                                 DEBUG(5,("New file %s\n",start));
517                                 goto done;
518                         }
519
520
521                         /*
522                          * Restore the rest of the string. If the string was
523                          * mangled the size may have changed.
524                          */
525                         if (end) {
526                                 char *tmp;
527                                 size_t start_ofs = start - name;
528
529                                 if (*dirpath != '\0') {
530                                         tmp = talloc_asprintf(ctx,
531                                                 "%s/%s/%s", dirpath,
532                                                 found_name, end+1);
533                                 }
534                                 else {
535                                         tmp = talloc_asprintf(ctx,
536                                                 "%s/%s", found_name,
537                                                 end+1);
538                                 }
539                                 if (tmp == NULL) {
540                                         DEBUG(0, ("talloc_asprintf failed\n"));
541                                         return NT_STATUS_NO_MEMORY;
542                                 }
543                                 TALLOC_FREE(name);
544                                 name = tmp;
545                                 start = name + start_ofs;
546                                 end = start + strlen(found_name);
547                                 *end = '\0';
548                         } else {
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                                                 found_name);
556                                 } else {
557                                         tmp = talloc_strdup(ctx,
558                                                 found_name);
559                                 }
560                                 if (tmp == NULL) {
561                                         DEBUG(0, ("talloc failed\n"));
562                                         return NT_STATUS_NO_MEMORY;
563                                 }
564                                 TALLOC_FREE(name);
565                                 name = tmp;
566                                 start = name + start_ofs;
567
568                                 /*
569                                  * We just scanned for, and found the end of
570                                  * the path. We must return a valid stat struct
571                                  * if it exists. JRA.
572                                  */
573
574                                 if (SMB_VFS_STAT(conn,name, &st) == 0) {
575                                         *pst = st;
576                                 } else {
577                                         SET_STAT_INVALID(st);
578                                 }
579                         }
580
581                         TALLOC_FREE(found_name);
582                 } /* end else */
583
584 #ifdef DEVELOPER
585                 if (VALID_STAT(st) &&
586                     get_delete_on_close_flag(vfs_file_id_from_sbuf(conn,
587                                     &st))) {
588                         result = NT_STATUS_DELETE_PENDING;
589                         goto fail;
590                 }
591 #endif
592
593                 /*
594                  * Add to the dirpath that we have resolved so far.
595                  */
596
597                 if (*dirpath != '\0') {
598                         char *tmp = talloc_asprintf(ctx,
599                                         "%s/%s", dirpath, start);
600                         if (!tmp) {
601                                 DEBUG(0, ("talloc_asprintf failed\n"));
602                                 return NT_STATUS_NO_MEMORY;
603                         }
604                         TALLOC_FREE(dirpath);
605                         dirpath = tmp;
606                 }
607                 else {
608                         TALLOC_FREE(dirpath);
609                         if (!(dirpath = talloc_strdup(ctx,start))) {
610                                 DEBUG(0, ("talloc_strdup failed\n"));
611                                 return NT_STATUS_NO_MEMORY;
612                         }
613                 }
614
615                 /*
616                  * Don't cache a name with mangled or wildcard components
617                  * as this can change the size.
618                  */
619
620                 if(!component_was_mangled && !name_has_wildcard) {
621                         stat_cache_add(orig_path, dirpath,
622                                         conn->case_sensitive);
623                 }
624
625                 /*
626                  * Restore the / that we wiped out earlier.
627                  */
628                 if (end) {
629                         *end = '/';
630                 }
631         }
632
633         /*
634          * Don't cache a name with mangled or wildcard components
635          * as this can change the size.
636          */
637
638         if(!component_was_mangled && !name_has_wildcard) {
639                 stat_cache_add(orig_path, name, conn->case_sensitive);
640         }
641
642         /*
643          * The name has been resolved.
644          */
645
646         DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
647
648  done:
649         *pp_conv_path = name;
650         TALLOC_FREE(dirpath);
651         return NT_STATUS_OK;
652  fail:
653         DEBUG(10, ("dirpath = [%s] start = [%s]\n", dirpath, start));
654         if (*dirpath != '\0') {
655                 *pp_conv_path = talloc_asprintf(ctx,
656                                 "%s/%s", dirpath, start);
657         } else {
658                 *pp_conv_path = talloc_strdup(ctx, start);
659         }
660         if (!*pp_conv_path) {
661                 DEBUG(0, ("talloc_asprintf failed\n"));
662                 return NT_STATUS_NO_MEMORY;
663         }
664         TALLOC_FREE(name);
665         TALLOC_FREE(dirpath);
666         return result;
667 }
668
669 /****************************************************************************
670  Check a filename - possibly calling check_reduced_name.
671  This is called by every routine before it allows an operation on a filename.
672  It does any final confirmation necessary to ensure that the filename is
673  a valid one for the user to access.
674 ****************************************************************************/
675
676 NTSTATUS check_name(connection_struct *conn, const char *name)
677 {
678         if (IS_VETO_PATH(conn, name))  {
679                 /* Is it not dot or dot dot. */
680                 if (!((name[0] == '.') && (!name[1] ||
681                                         (name[1] == '.' && !name[2])))) {
682                         DEBUG(5,("check_name: file path name %s vetoed\n",
683                                                 name));
684                         return map_nt_error_from_unix(ENOENT);
685                 }
686         }
687
688         if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
689                 NTSTATUS status = check_reduced_name(conn,name);
690                 if (!NT_STATUS_IS_OK(status)) {
691                         DEBUG(5,("check_name: name %s failed with %s\n",name,
692                                                 nt_errstr(status)));
693                         return status;
694                 }
695         }
696
697         return NT_STATUS_OK;
698 }
699
700 /****************************************************************************
701  Check if two filenames are equal.
702  This needs to be careful about whether we are case sensitive.
703 ****************************************************************************/
704
705 static bool fname_equal(const char *name1, const char *name2,
706                 bool case_sensitive)
707 {
708         /* Normal filename handling */
709         if (case_sensitive) {
710                 return(strcmp(name1,name2) == 0);
711         }
712
713         return(strequal(name1,name2));
714 }
715
716 /****************************************************************************
717  Scan a directory to find a filename, matching without case sensitivity.
718  If the name looks like a mangled name then try via the mangling functions
719 ****************************************************************************/
720
721 static bool scan_directory(connection_struct *conn, const char *path,
722                            char *name, char **found_name)
723 {
724         struct smb_Dir *cur_dir;
725         const char *dname;
726         bool mangled;
727         char *unmangled_name = NULL;
728         long curpos;
729         TALLOC_CTX *ctx = talloc_tos();
730
731         mangled = mangle_is_mangled(name, conn->params);
732
733         /* handle null paths */
734         if ((path == NULL) || (*path == 0)) {
735                 path = ".";
736         }
737
738         /*
739          * The incoming name can be mangled, and if we de-mangle it
740          * here it will not compare correctly against the filename (name2)
741          * read from the directory and then mangled by the name_to_8_3()
742          * call. We need to mangle both names or neither.
743          * (JRA).
744          *
745          * Fix for bug found by Dina Fine. If in case sensitive mode then
746          * the mangle cache is no good (3 letter extension could be wrong
747          * case - so don't demangle in this case - leave as mangled and
748          * allow the mangling of the directory entry read (which is done
749          * case insensitively) to match instead. This will lead to more
750          * false positive matches but we fail completely without it. JRA.
751          */
752
753         if (mangled && !conn->case_sensitive) {
754                 mangled = !mangle_lookup_name_from_8_3(ctx,
755                                                 name,
756                                                 &unmangled_name,
757                                                 conn->params);
758                 if (!mangled) {
759                         /* Name is now unmangled. */
760                         name = unmangled_name;
761                 }
762         }
763
764         /* open the directory */
765         if (!(cur_dir = OpenDir(conn, path, NULL, 0))) {
766                 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
767                 TALLOC_FREE(unmangled_name);
768                 return(False);
769         }
770
771         /* now scan for matching names */
772         curpos = 0;
773         while ((dname = ReadDirName(cur_dir, &curpos))) {
774
775                 /* Is it dot or dot dot. */
776                 if (ISDOT(dname) || ISDOTDOT(dname)) {
777                         continue;
778                 }
779
780                 /*
781                  * At this point dname is the unmangled name.
782                  * name is either mangled or not, depending on the state
783                  * of the "mangled" variable. JRA.
784                  */
785
786                 /*
787                  * Check mangled name against mangled name, or unmangled name
788                  * against unmangled name.
789                  */
790
791                 if ((mangled && mangled_equal(name,dname,conn->params)) ||
792                         fname_equal(name, dname, conn->case_sensitive)) {
793                         /* we've found the file, change it's name and return */
794                         *found_name = talloc_strdup(ctx,dname);
795                         TALLOC_FREE(unmangled_name);
796                         CloseDir(cur_dir);
797                         if (!*found_name) {
798                                 errno = ENOMEM;
799                                 return False;
800                         }
801                         return(True);
802                 }
803         }
804
805         TALLOC_FREE(unmangled_name);
806         CloseDir(cur_dir);
807         errno = ENOENT;
808         return False;
809 }