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