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