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