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