r11861: Fix inspired by Thomas Neumann <t.neumann@iku-ag.de> to ensure that
[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, int snum)
50 {
51         pstring tmpname;
52         
53         pstrcpy(tmpname, name2);
54         mangle_map(tmpname, True, False, snum);
55         return strequal(name1, tmpname);
56 }
57
58 /****************************************************************************
59 This routine is called to convert names from the dos namespace to unix
60 namespace. It needs to handle any case conversions, mangling, format
61 changes etc.
62
63 We assume that we have already done a chdir() to the right "root" directory
64 for this service.
65
66 The function will return False if some part of the name except for the last
67 part cannot be resolved
68
69 If the saved_last_component != 0, then the unmodified last component
70 of the pathname is returned there. This is used in an exceptional
71 case in reply_mv (so far). If saved_last_component == 0 then nothing
72 is returned there.
73
74 The bad_path arg is set to True if the filename walk failed. This is
75 used to pick the correct error code to return between ENOENT and ENOTDIR
76 as Windows applications depend on ERRbadpath being returned if a component
77 of a pathname does not exist.
78
79 On exit from unix_convert, if *pst was not null, then the file stat
80 struct will be returned if the file exists and was found, if not this
81 stat struct will be filled with zeros (and this can be detected by checking
82 for nlinks = 0, which can never be true for any file).
83 ****************************************************************************/
84
85 BOOL unix_convert(pstring name,connection_struct *conn,char *saved_last_component, 
86                   BOOL *bad_path, SMB_STRUCT_STAT *pst)
87 {
88         SMB_STRUCT_STAT st;
89         char *start, *end;
90         pstring dirpath;
91         pstring orig_path;
92         BOOL component_was_mangled = False;
93         BOOL name_has_wildcard = False;
94
95         SET_STAT_INVALID(*pst);
96
97         *dirpath = 0;
98         *bad_path = False;
99         if(saved_last_component)
100                 *saved_last_component = 0;
101
102         if (conn->printer) {
103                 /* we don't ever use the filenames on a printer share as a
104                         filename - so don't convert them */
105                 return True;
106         }
107
108         DEBUG(5, ("unix_convert called on file \"%s\"\n", name));
109
110         /* 
111          * Conversion to basic unix format is already done in check_path_syntax().
112          */
113
114         /* 
115          * Names must be relative to the root of the service - any leading /.
116          * and trailing /'s should have been trimmed by check_path_syntax().
117          */
118
119 #ifdef DEVELOPER
120         SMB_ASSERT(*name != '/');
121 #endif
122
123         /*
124          * If we trimmed down to a single '\0' character
125          * then we should use the "." directory to avoid
126          * searching the cache, but not if we are in a
127          * printing share.
128          * As we know this is valid we can return true here.
129          */
130
131         if (!*name) {
132                 name[0] = '.';
133                 name[1] = '\0';
134                 if (SMB_VFS_STAT(conn,name,&st) == 0) {
135                         *pst = st;
136                 }
137                 DEBUG(5,("conversion finished \"\" -> %s\n",name));
138                 return(True);
139         }
140
141         /*
142          * Ensure saved_last_component is valid even if file exists.
143          */
144
145         if(saved_last_component) {
146                 end = strrchr_m(name, '/');
147                 if(end)
148                         pstrcpy(saved_last_component, end + 1);
149                 else
150                         pstrcpy(saved_last_component, name);
151         }
152
153         start = name;
154         pstrcpy(orig_path, name);
155
156         if(!conn->case_sensitive && stat_cache_lookup(conn, name, dirpath, &start, &st)) {
157                 *pst = st;
158                 return True;
159         }
160
161         /* 
162          * stat the name - if it exists then we are all done!
163          */
164
165         if (SMB_VFS_STAT(conn,name,&st) == 0) {
166                 stat_cache_add(orig_path, name, conn->case_sensitive);
167                 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
168                 *pst = st;
169                 return(True);
170         }
171
172         DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n", name, dirpath, start));
173
174         /* 
175          * A special case - if we don't have any mangling chars and are case
176          * sensitive then searching won't help.
177          */
178
179         if (conn->case_sensitive && !mangle_is_mangled(name, SNUM(conn)) && !*lp_mangled_map(SNUM(conn)))
180                 return(False);
181
182         name_has_wildcard = ms_has_wild(start);
183
184         /* 
185          * is_mangled() was changed to look at an entire pathname, not 
186          * just a component. JRA.
187          */
188
189         if (mangle_is_mangled(start, SNUM(conn)))
190                 component_was_mangled = True;
191
192         /* 
193          * Now we need to recursively match the name against the real 
194          * directory structure.
195          */
196
197         /* 
198          * Match each part of the path name separately, trying the names
199          * as is first, then trying to scan the directory for matching names.
200          */
201
202         for (; start ; start = (end?end+1:(char *)NULL)) {
203                 /* 
204                  * Pinpoint the end of this section of the filename.
205                  */
206                 end = strchr_m(start, '/');
207
208                 /* 
209                  * Chop the name at this point.
210                  */
211                 if (end) 
212                         *end = 0;
213
214                 if(saved_last_component != 0)
215                         pstrcpy(saved_last_component, end ? end + 1 : start);
216
217                 /* 
218                  * Check if the name exists up to this point.
219                  */
220
221                 if (SMB_VFS_STAT(conn,name, &st) == 0) {
222                         /*
223                          * It exists. it must either be a directory or this must be
224                          * the last part of the path for it to be OK.
225                          */
226                         if (end && !(st.st_mode & S_IFDIR)) {
227                                 /*
228                                  * An intermediate part of the name isn't a directory.
229                                  */
230                                 DEBUG(5,("Not a dir %s\n",start));
231                                 *end = '/';
232                                 /* 
233                                  * We need to return the fact that the intermediate
234                                  * name resolution failed. This is used to return an
235                                  * error of ERRbadpath rather than ERRbadfile. Some
236                                  * Windows applications depend on the difference between
237                                  * these two errors.
238                                  */
239                                 errno = ENOTDIR;
240                                 *bad_path = True;
241                                 return(False);
242                         }
243
244                         if (!end) {
245                                 /*
246                                  * We just scanned for, and found the end of the path.
247                                  * We must return the valid stat struct.
248                                  * JRA.
249                                  */
250
251                                 *pst = st;
252                         }
253
254                 } else {
255                         pstring rest;
256
257                         /* Stat failed - ensure we don't use it. */
258                         SET_STAT_INVALID(st);
259                         *rest = 0;
260
261                         /*
262                          * Remember the rest of the pathname so it can be restored
263                          * later.
264                          */
265
266                         if (end)
267                                 pstrcpy(rest,end+1);
268
269                         /* Reset errno so we can detect directory open errors. */
270                         errno = 0;
271
272                         /*
273                          * Try to find this part of the path in the directory.
274                          */
275
276                         if (ms_has_wild(start) || 
277                             !scan_directory(conn, dirpath, start, sizeof(pstring) - 1 - (start - name))) {
278                                 if (end) {
279                                         /*
280                                          * An intermediate part of the name can't be found.
281                                          */
282                                         DEBUG(5,("Intermediate not found %s\n",start));
283                                         *end = '/';
284
285                                         /* 
286                                          * We need to return the fact that the intermediate
287                                          * name resolution failed. This is used to return an
288                                          * error of ERRbadpath rather than ERRbadfile. Some
289                                          * Windows applications depend on the difference between
290                                          * these two errors.
291                                          */
292                                         *bad_path = True;
293                                         return(False);
294                                 }
295               
296                                 if (errno == ENOTDIR) {
297                                         *bad_path = True;
298                                         return(False);
299                                 }
300
301                                 /*
302                                  * Just the last part of the name doesn't exist.
303                                  * We need to strupper() or strlower() it as
304                                  * this conversion may be used for file creation 
305                                  * purposes. Fix inspired by Thomas Neumann <t.neumann@iku-ag.de>.
306                                  */
307                                 if (!conn->case_preserve ||
308                                                 (mangle_is_8_3(start, False, SNUM(conn)) &&
309                                                  !conn->short_case_preserve)) {
310                                         strnorm(start, lp_defaultcase(SNUM(conn)));
311                                 }
312
313                                 /*
314                                  * check on the mangled stack to see if we can recover the 
315                                  * base of the filename.
316                                  */
317
318                                 if (mangle_is_mangled(start, SNUM(conn))) {
319                                         mangle_check_cache( start, sizeof(pstring) - 1 - (start - name), SNUM(conn));
320                                 }
321
322                                 DEBUG(5,("New file %s\n",start));
323                                 return(True); 
324                         }
325
326                         /* 
327                          * Restore the rest of the string. If the string was mangled the size
328                          * may have changed.
329                          */
330                         if (end) {
331                                 end = start + strlen(start);
332                                 if (!safe_strcat(start, "/", sizeof(pstring) - 1 - (start - name)) ||
333                                     !safe_strcat(start, rest, sizeof(pstring) - 1 - (start - name))) {
334                                         return False;
335                                 }
336                                 *end = '\0';
337                         } else {
338                                 /*
339                                  * We just scanned for, and found the end of the path.
340                                  * We must return a valid stat struct if it exists.
341                                  * JRA.
342                                  */
343
344                                 if (SMB_VFS_STAT(conn,name, &st) == 0) {
345                                         *pst = st;
346                                 } else {
347                                         SET_STAT_INVALID(st);
348                                 }
349                         }
350                 } /* end else */
351
352                 /* 
353                  * Add to the dirpath that we have resolved so far.
354                  */
355                 if (*dirpath)
356                         pstrcat(dirpath,"/");
357
358                 pstrcat(dirpath,start);
359
360                 /*
361                  * Don't cache a name with mangled or wildcard components
362                  * as this can change the size.
363                  */
364                 
365                 if(!component_was_mangled && !name_has_wildcard)
366                         stat_cache_add(orig_path, dirpath, conn->case_sensitive);
367         
368                 /* 
369                  * Restore the / that we wiped out earlier.
370                  */
371                 if (end)
372                         *end = '/';
373         }
374   
375         /*
376          * Don't cache a name with mangled or wildcard components
377          * as this can change the size.
378          */
379
380         if(!component_was_mangled && !name_has_wildcard)
381                 stat_cache_add(orig_path, name, conn->case_sensitive);
382
383         /* 
384          * The name has been resolved.
385          */
386
387         DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
388         return(True);
389 }
390
391 /****************************************************************************
392  Check a filename - possibly caling reducename.
393  This is called by every routine before it allows an operation on a filename.
394  It does any final confirmation necessary to ensure that the filename is
395  a valid one for the user to access.
396 ****************************************************************************/
397
398 BOOL check_name(const pstring name,connection_struct *conn)
399 {
400         BOOL ret = True;
401
402         if (IS_VETO_PATH(conn, name))  {
403                 /* Is it not dot or dot dot. */
404                 if (!((name[0] == '.') && (!name[1] || (name[1] == '.' && !name[2])))) {
405                         DEBUG(5,("file path name %s vetoed\n",name));
406                         errno = ENOENT;
407                         return False;
408                 }
409         }
410
411         if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
412                 ret = reduce_name(conn,name);
413         }
414
415         if (!ret) {
416                 DEBUG(5,("check_name on %s failed\n",name));
417         }
418
419         return(ret);
420 }
421
422 /****************************************************************************
423  Scan a directory to find a filename, matching without case sensitivity.
424  If the name looks like a mangled name then try via the mangling functions
425 ****************************************************************************/
426
427 static BOOL scan_directory(connection_struct *conn, const char *path, char *name, size_t maxlength)
428 {
429         struct smb_Dir *cur_dir;
430         const char *dname;
431         BOOL mangled;
432         long curpos;
433
434         mangled = mangle_is_mangled(name, SNUM(conn));
435
436         /* handle null paths */
437         if (*path == 0)
438                 path = ".";
439
440         /*
441          * The incoming name can be mangled, and if we de-mangle it
442          * here it will not compare correctly against the filename (name2)
443          * read from the directory and then mangled by the mangle_map()
444          * call. We need to mangle both names or neither.
445          * (JRA).
446          *
447          * Fix for bug found by Dina Fine. If in case sensitive mode then
448          * the mangle cache is no good (3 letter extension could be wrong
449          * case - so don't demangle in this case - leave as mangled and
450          * allow the mangling of the directory entry read (which is done
451          * case insensitively) to match instead. This will lead to more
452          * false positive matches but we fail completely without it. JRA.
453          */
454
455         if (mangled && !conn->case_sensitive) {
456                 mangled = !mangle_check_cache( name, maxlength, SNUM(conn));
457         }
458
459         /* open the directory */
460         if (!(cur_dir = OpenDir(conn, path, NULL, 0))) {
461                 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
462                 return(False);
463         }
464
465         /* now scan for matching names */
466         curpos = 0;
467         while ((dname = ReadDirName(cur_dir, &curpos))) {
468
469                 /* Is it dot or dot dot. */
470                 if ((dname[0] == '.') && (!dname[1] || (dname[1] == '.' && !dname[2]))) {
471                         continue;
472                 }
473
474                 /*
475                  * At this point dname is the unmangled name.
476                  * name is either mangled or not, depending on the state of the "mangled"
477                  * variable. JRA.
478                  */
479
480                 /*
481                  * Check mangled name against mangled name, or unmangled name
482                  * against unmangled name.
483                  */
484
485                 if ((mangled && mangled_equal(name,dname,SNUM(conn))) || fname_equal(name, dname, conn->case_sensitive)) {
486                         /* we've found the file, change it's name and return */
487                         safe_strcpy(name, dname, maxlength);
488                         CloseDir(cur_dir);
489                         return(True);
490                 }
491         }
492
493         CloseDir(cur_dir);
494         errno = ENOENT;
495         return(False);
496 }