Fix smbfs to not trigger our paranoid checker.
[tprouty/samba.git] / source / 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-2004
6    Copyright (C) Ying Chen 2000
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 /*
24  * New hash table stat cache code added by Ying Chen.
25  */
26
27 #include "includes.h"
28
29 static BOOL scan_directory(connection_struct *conn, const char *path, char *name,size_t maxlength);
30
31 /****************************************************************************
32  Check if two filenames are equal.
33  This needs to be careful about whether we are case sensitive.
34 ****************************************************************************/
35
36 static BOOL fname_equal(const char *name1, const char *name2, BOOL case_sensitive)
37 {
38         /* Normal filename handling */
39         if (case_sensitive)
40                 return(strcmp(name1,name2) == 0);
41
42         return(strequal(name1,name2));
43 }
44
45 /****************************************************************************
46  Mangle the 2nd name and check if it is then equal to the first name.
47 ****************************************************************************/
48
49 static BOOL mangled_equal(const char *name1, const char *name2,
50                           const struct share_params *p)
51 {
52         pstring tmpname;
53         
54         pstrcpy(tmpname, name2);
55         mangle_map(tmpname, True, False, p);
56         return strequal(name1, tmpname);
57 }
58
59 /****************************************************************************
60  Cope with the differing wildcard and non-wildcard error cases.
61 ****************************************************************************/
62
63 static NTSTATUS determine_path_error(const char *name, BOOL allow_wcard_last_component)
64 {
65         const char *p;
66
67         if (!allow_wcard_last_component) {
68                 /* Error code within a pathname. */
69                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
70         }
71
72         /* We're terminating here so we
73          * can be a little slower and get
74          * the error code right. Windows
75          * treats the last part of the pathname
76          * separately I think, so if the last
77          * component is a wildcard then we treat
78          * this ./ as "end of component" */
79
80         p = strchr(name, '/');
81
82         if (!p && (ms_has_wild(name) || ISDOT(name))) {
83                 /* Error code at the end of a pathname. */
84                 return NT_STATUS_OBJECT_NAME_INVALID;
85         } else {
86                 /* Error code within a pathname. */
87                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
88         }
89 }
90         
91 /****************************************************************************
92 This routine is called to convert names from the dos namespace to unix
93 namespace. It needs to handle any case conversions, mangling, format
94 changes etc.
95
96 We assume that we have already done a chdir() to the right "root" directory
97 for this service.
98
99 The function will return an NTSTATUS error if some part of the name except for the last
100 part cannot be resolved, else NT_STATUS_OK.
101
102 Note NT_STATUS_OK doesn't mean the name exists or is valid, just that we didn't
103 get any fatal errors that should immediately terminate the calling
104 SMB processing whilst resolving.
105
106 If the saved_last_component != 0, then the unmodified last component
107 of the pathname is returned there. This is used in an exceptional
108 case in reply_mv (so far). If saved_last_component == 0 then nothing
109 is returned there.
110
111 If last_component_wcard is true then a MS wildcard was detected and
112 should be allowed in the last component of the path only.
113
114 On exit from unix_convert, if *pst was not null, then the file stat
115 struct will be returned if the file exists and was found, if not this
116 stat struct will be filled with zeros (and this can be detected by checking
117 for nlinks = 0, which can never be true for any file).
118 ****************************************************************************/
119
120 NTSTATUS unix_convert(connection_struct *conn,
121                         pstring name,
122                         BOOL allow_wcard_last_component,
123                         char *saved_last_component, 
124                         SMB_STRUCT_STAT *pst)
125 {
126         SMB_STRUCT_STAT st;
127         char *start, *end;
128         pstring dirpath;
129         pstring orig_path;
130         BOOL component_was_mangled = False;
131         BOOL name_has_wildcard = False;
132
133         SET_STAT_INVALID(*pst);
134
135         *dirpath = 0;
136
137         if(saved_last_component) {
138                 *saved_last_component = 0;
139         }
140
141         if (conn->printer) {
142                 /* we don't ever use the filenames on a printer share as a
143                         filename - so don't convert them */
144                 return NT_STATUS_OK;
145         }
146
147         DEBUG(5, ("unix_convert called on file \"%s\"\n", name));
148
149         /* 
150          * Conversion to basic unix format is already done in check_path_syntax().
151          */
152
153         /* 
154          * Names must be relative to the root of the service - any leading /.
155          * and trailing /'s should have been trimmed by check_path_syntax().
156          */
157
158 #ifdef DEVELOPER
159         SMB_ASSERT(*name != '/');
160 #endif
161
162         /*
163          * If we trimmed down to a single '\0' character
164          * then we should use the "." directory to avoid
165          * searching the cache, but not if we are in a
166          * printing share.
167          * As we know this is valid we can return true here.
168          */
169
170         if (!*name) {
171                 name[0] = '.';
172                 name[1] = '\0';
173                 if (SMB_VFS_STAT(conn,name,&st) == 0) {
174                         *pst = st;
175                 }
176                 DEBUG(5,("conversion finished \"\" -> %s\n",name));
177                 return NT_STATUS_OK;
178         }
179
180         if (name[0] == '.' && (name[1] == '/' || name[1] == '\0')) {
181                 /* Start of pathname can't be "." only. */
182                 if (name[1] == '\0' || name[2] == '\0') {
183                         return NT_STATUS_OBJECT_NAME_INVALID;
184                 } else {
185                         return determine_path_error(&name[2], allow_wcard_last_component);
186                 }
187         }
188
189         /*
190          * Ensure saved_last_component is valid even if file exists.
191          */
192
193         if(saved_last_component) {
194                 end = strrchr_m(name, '/');
195                 if (end) {
196                         pstrcpy(saved_last_component, end + 1);
197                 } else {
198                         pstrcpy(saved_last_component, name);
199                 }
200         }
201
202         /*
203          * Large directory fix normalization. If we're case sensitive, and
204          * the case preserving parameters are set to "no", normalize the case of
205          * the incoming filename from the client WHETHER IT EXISTS OR NOT !
206          * This is in conflict with the current (3.0.20) man page, but is
207          * what people expect from the "large directory howto". I'll update
208          * the man page. Thanks to jht@samba.org for finding this. JRA.
209          */
210
211         if (conn->case_sensitive && !conn->case_preserve && !conn->short_case_preserve) {
212                 strnorm(name, lp_defaultcase(SNUM(conn)));
213         }
214         
215         start = name;
216         pstrcpy(orig_path, name);
217
218         if(!conn->case_sensitive && stat_cache_lookup(conn, name, dirpath, &start, &st)) {
219                 *pst = st;
220                 return NT_STATUS_OK;
221         }
222
223         /* 
224          * stat the name - if it exists then we are all done!
225          */
226
227         if (SMB_VFS_STAT(conn,name,&st) == 0) {
228                 /* Ensure we catch all names with in "/."
229                    this is disallowed under Windows. */
230                 const char *p = strstr(name, "/."); /* mb safe. */
231                 if (p) {
232                         if (p[2] == '/') {
233                                 /* Error code within a pathname. */
234                                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
235                         } else if (p[2] == '\0') {
236                                 /* Error code at the end of a pathname. */
237                                 return NT_STATUS_OBJECT_NAME_INVALID;
238                         }
239                 }
240                 stat_cache_add(orig_path, name, conn->case_sensitive);
241                 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
242                 *pst = st;
243                 return NT_STATUS_OK;
244         }
245
246         DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n", name, dirpath, start));
247
248         /* 
249          * A special case - if we don't have any mangling chars and are case
250          * sensitive then searching won't help.
251          */
252
253         if (conn->case_sensitive && 
254                         !mangle_is_mangled(name, conn->params) &&
255                         !*lp_mangled_map(conn->params)) {
256                 return NT_STATUS_OK;
257         }
258
259         /* 
260          * is_mangled() was changed to look at an entire pathname, not 
261          * just a component. JRA.
262          */
263
264         if (mangle_is_mangled(start, conn->params)) {
265                 component_was_mangled = True;
266         }
267
268         /* 
269          * Now we need to recursively match the name against the real 
270          * directory structure.
271          */
272
273         /* 
274          * Match each part of the path name separately, trying the names
275          * as is first, then trying to scan the directory for matching names.
276          */
277
278         for (; start ; start = (end?end+1:(char *)NULL)) {
279                 /* 
280                  * Pinpoint the end of this section of the filename.
281                  */
282                 end = strchr(start, '/'); /* mb safe. '/' can't be in any encoded char. */
283
284                 /* 
285                  * Chop the name at this point.
286                  */
287                 if (end) {
288                         *end = 0;
289                 }
290
291                 if (saved_last_component != 0) {
292                         pstrcpy(saved_last_component, end ? end + 1 : start);
293                 }
294
295                 /* The name cannot have a component of "." */
296
297                 if (ISDOT(start)) {
298                         if (!end)  {
299                                 /* Error code at the end of a pathname. */
300                                 return NT_STATUS_OBJECT_NAME_INVALID;
301                         }
302                         return determine_path_error(end+1, allow_wcard_last_component);
303                 }
304
305                 /* The name cannot have a wildcard if it's not
306                    the last component. */
307
308                 name_has_wildcard = ms_has_wild(start);
309
310                 /* Wildcard not valid anywhere. */
311                 if (name_has_wildcard && !allow_wcard_last_component) {
312                         return NT_STATUS_OBJECT_NAME_INVALID;
313                 }
314
315                 /* Wildcards never valid within a pathname. */
316                 if (name_has_wildcard && end) {
317                         return NT_STATUS_OBJECT_NAME_INVALID;
318                 }
319
320                 /* 
321                  * Check if the name exists up to this point.
322                  */
323
324                 if (SMB_VFS_STAT(conn,name, &st) == 0) {
325                         /*
326                          * It exists. it must either be a directory or this must be
327                          * the last part of the path for it to be OK.
328                          */
329                         if (end && !(st.st_mode & S_IFDIR)) {
330                                 /*
331                                  * An intermediate part of the name isn't a directory.
332                                  */
333                                 DEBUG(5,("Not a dir %s\n",start));
334                                 *end = '/';
335                                 /* 
336                                  * We need to return the fact that the intermediate
337                                  * name resolution failed. This is used to return an
338                                  * error of ERRbadpath rather than ERRbadfile. Some
339                                  * Windows applications depend on the difference between
340                                  * these two errors.
341                                  */
342                                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
343                         }
344
345                         if (!end) {
346                                 /*
347                                  * We just scanned for, and found the end of the path.
348                                  * We must return the valid stat struct.
349                                  * JRA.
350                                  */
351
352                                 *pst = st;
353                         }
354
355                 } else {
356                         pstring rest;
357
358                         /* Stat failed - ensure we don't use it. */
359                         SET_STAT_INVALID(st);
360                         *rest = 0;
361
362                         /*
363                          * Remember the rest of the pathname so it can be restored
364                          * later.
365                          */
366
367                         if (end) {
368                                 pstrcpy(rest,end+1);
369                         }
370
371                         /* Reset errno so we can detect directory open errors. */
372                         errno = 0;
373
374                         /*
375                          * Try to find this part of the path in the directory.
376                          */
377
378                         if (name_has_wildcard || 
379                             !scan_directory(conn, dirpath, start, sizeof(pstring) - 1 - (start - name))) {
380                                 if (end) {
381                                         /*
382                                          * An intermediate part of the name can't be found.
383                                          */
384                                         DEBUG(5,("Intermediate not found %s\n",start));
385                                         *end = '/';
386
387                                         /* 
388                                          * We need to return the fact that the intermediate
389                                          * name resolution failed. This is used to return an
390                                          * error of ERRbadpath rather than ERRbadfile. Some
391                                          * Windows applications depend on the difference between
392                                          * these two errors.
393                                          */
394
395                                         /* ENOENT, ENOTDIR and ELOOP all map to
396                                          * NT_STATUS_OBJECT_PATH_NOT_FOUND
397                                          * in the filename walk. */
398
399                                         if (errno == ENOENT ||
400                                                         errno == ENOTDIR ||
401                                                         errno == ELOOP) {
402                                                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
403                                         }
404                                         return map_nt_error_from_unix(errno);
405                                 }
406
407                                 /* ENOENT is the only valid error here. */
408                                 if (errno != ENOENT) {
409                                         /* ENOTDIR and ELOOP both map to
410                                          * NT_STATUS_OBJECT_PATH_NOT_FOUND
411                                          * in the filename walk. */
412                                         if (errno == ENOTDIR ||
413                                                         errno == ELOOP) {
414                                                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
415                                         }
416                                         return map_nt_error_from_unix(errno);
417                                 }
418
419                                 /*
420                                  * Just the last part of the name doesn't exist.
421                                  * We need to strupper() or strlower() it as
422                                  * this conversion may be used for file creation 
423                                  * purposes. Fix inspired by Thomas Neumann <t.neumann@iku-ag.de>.
424                                  */
425                                 if (!conn->case_preserve ||
426                                     (mangle_is_8_3(start, False, conn->params) &&
427                                                  !conn->short_case_preserve)) {
428                                         strnorm(start, lp_defaultcase(SNUM(conn)));
429                                 }
430
431                                 /*
432                                  * check on the mangled stack to see if we can recover the 
433                                  * base of the filename.
434                                  */
435
436                                 if (mangle_is_mangled(start, conn->params)) {
437                                         mangle_check_cache( start, sizeof(pstring) - 1 - (start - name), conn->params);
438                                 }
439
440                                 DEBUG(5,("New file %s\n",start));
441                                 return NT_STATUS_OK;
442                         }
443
444                         /* 
445                          * Restore the rest of the string. If the string was mangled the size
446                          * may have changed.
447                          */
448                         if (end) {
449                                 end = start + strlen(start);
450                                 if (!safe_strcat(start, "/", sizeof(pstring) - 1 - (start - name)) ||
451                                     !safe_strcat(start, rest, sizeof(pstring) - 1 - (start - name))) {
452                                         return map_nt_error_from_unix(ENAMETOOLONG);
453                                 }
454                                 *end = '\0';
455                         } else {
456                                 /*
457                                  * We just scanned for, and found the end of the path.
458                                  * We must return a valid stat struct if it exists.
459                                  * JRA.
460                                  */
461
462                                 if (SMB_VFS_STAT(conn,name, &st) == 0) {
463                                         *pst = st;
464                                 } else {
465                                         SET_STAT_INVALID(st);
466                                 }
467                         }
468                 } /* end else */
469
470 #ifdef DEVELOPER
471                 if (VALID_STAT(st) && get_delete_on_close_flag(st.st_dev, st.st_ino)) {
472                         return NT_STATUS_DELETE_PENDING;
473                 }
474 #endif
475
476                 /* 
477                  * Add to the dirpath that we have resolved so far.
478                  */
479                 if (*dirpath) {
480                         pstrcat(dirpath,"/");
481                 }
482
483                 pstrcat(dirpath,start);
484
485                 /*
486                  * Don't cache a name with mangled or wildcard components
487                  * as this can change the size.
488                  */
489                 
490                 if(!component_was_mangled && !name_has_wildcard) {
491                         stat_cache_add(orig_path, dirpath, conn->case_sensitive);
492                 }
493         
494                 /* 
495                  * Restore the / that we wiped out earlier.
496                  */
497                 if (end) {
498                         *end = '/';
499                 }
500         }
501   
502         /*
503          * Don't cache a name with mangled or wildcard components
504          * as this can change the size.
505          */
506
507         if(!component_was_mangled && !name_has_wildcard) {
508                 stat_cache_add(orig_path, name, conn->case_sensitive);
509         }
510
511         /* 
512          * The name has been resolved.
513          */
514
515         DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
516         return NT_STATUS_OK;
517 }
518
519 /****************************************************************************
520  Check a filename - possibly caling reducename.
521  This is called by every routine before it allows an operation on a filename.
522  It does any final confirmation necessary to ensure that the filename is
523  a valid one for the user to access.
524 ****************************************************************************/
525
526 NTSTATUS check_name(connection_struct *conn, const pstring name)
527 {
528         if (IS_VETO_PATH(conn, name))  {
529                 /* Is it not dot or dot dot. */
530                 if (!((name[0] == '.') && (!name[1] || (name[1] == '.' && !name[2])))) {
531                         DEBUG(5,("check_name: file path name %s vetoed\n",name));
532                         return map_nt_error_from_unix(ENOENT);
533                 }
534         }
535
536         if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
537                 NTSTATUS status = reduce_name(conn,name);
538                 if (!NT_STATUS_IS_OK(status)) {
539                         DEBUG(5,("check_name: name %s failed with %s\n",name, nt_errstr(status)));
540                         return status;
541                 }
542         }
543
544         return NT_STATUS_OK;
545 }
546
547 /****************************************************************************
548  Scan a directory to find a filename, matching without case sensitivity.
549  If the name looks like a mangled name then try via the mangling functions
550 ****************************************************************************/
551
552 static BOOL scan_directory(connection_struct *conn, const char *path, char *name, size_t maxlength)
553 {
554         struct smb_Dir *cur_dir;
555         const char *dname;
556         BOOL mangled;
557         long curpos;
558
559         mangled = mangle_is_mangled(name, conn->params);
560
561         /* handle null paths */
562         if (*path == 0)
563                 path = ".";
564
565         /*
566          * The incoming name can be mangled, and if we de-mangle it
567          * here it will not compare correctly against the filename (name2)
568          * read from the directory and then mangled by the mangle_map()
569          * call. We need to mangle both names or neither.
570          * (JRA).
571          *
572          * Fix for bug found by Dina Fine. If in case sensitive mode then
573          * the mangle cache is no good (3 letter extension could be wrong
574          * case - so don't demangle in this case - leave as mangled and
575          * allow the mangling of the directory entry read (which is done
576          * case insensitively) to match instead. This will lead to more
577          * false positive matches but we fail completely without it. JRA.
578          */
579
580         if (mangled && !conn->case_sensitive) {
581                 mangled = !mangle_check_cache( name, maxlength, conn->params);
582         }
583
584         /* open the directory */
585         if (!(cur_dir = OpenDir(conn, path, NULL, 0))) {
586                 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
587                 return(False);
588         }
589
590         /* now scan for matching names */
591         curpos = 0;
592         while ((dname = ReadDirName(cur_dir, &curpos))) {
593
594                 /* Is it dot or dot dot. */
595                 if ((dname[0] == '.') && (!dname[1] || (dname[1] == '.' && !dname[2]))) {
596                         continue;
597                 }
598
599                 /*
600                  * At this point dname is the unmangled name.
601                  * name is either mangled or not, depending on the state of the "mangled"
602                  * variable. JRA.
603                  */
604
605                 /*
606                  * Check mangled name against mangled name, or unmangled name
607                  * against unmangled name.
608                  */
609
610                 if ((mangled && mangled_equal(name,dname,conn->params)) || fname_equal(name, dname, conn->case_sensitive)) {
611                         /* we've found the file, change it's name and return */
612                         safe_strcpy(name, dname, maxlength);
613                         CloseDir(cur_dir);
614                         return(True);
615                 }
616         }
617
618         CloseDir(cur_dir);
619         errno = ENOENT;
620         return(False);
621 }