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