r2176: syncing necessary changes for 3.0.7
[Samba.git] / source / smbd / filename.c
blobc411ef0f79f26738a79e1343ea4f8bec522a82e7
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
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.
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.
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.
24 * New hash table stat cache code added by Ying Chen.
27 #include "includes.h"
29 static BOOL scan_directory(const char *path, char *name,size_t maxlength,
30 connection_struct *conn,BOOL docache);
32 /****************************************************************************
33 Check if two filenames are equal.
34 This needs to be careful about whether we are case sensitive.
35 ****************************************************************************/
37 static BOOL fname_equal(const char *name1, const char *name2, BOOL case_sensitive)
39 /* Normal filename handling */
40 if (case_sensitive)
41 return(strcmp(name1,name2) == 0);
43 return(strequal(name1,name2));
46 /****************************************************************************
47 Mangle the 2nd name and check if it is then equal to the first name.
48 ****************************************************************************/
50 static BOOL mangled_equal(const char *name1, const char *name2, int snum)
52 pstring tmpname;
54 pstrcpy(tmpname, name2);
55 mangle_map(tmpname, True, False, snum);
56 return strequal(name1, tmpname);
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.
64 We assume that we have already done a chdir() to the right "root" directory
65 for this service.
67 The function will return False if some part of the name except for the last
68 part cannot be resolved
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.
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.
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 ****************************************************************************/
86 BOOL unix_convert(pstring name,connection_struct *conn,char *saved_last_component,
87 BOOL *bad_path, SMB_STRUCT_STAT *pst)
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;
96 ZERO_STRUCTP(pst);
98 *dirpath = 0;
99 *bad_path = False;
100 if(saved_last_component)
101 *saved_last_component = 0;
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;
109 DEBUG(5, ("unix_convert called on file \"%s\"\n", name));
112 * Conversion to basic unix format is already done in check_path_syntax().
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().
120 #ifdef DEVELOPER
121 SMB_ASSERT(*name != '/');
122 #endif
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.
132 if (!*name) {
133 name[0] = '.';
134 name[1] = '\0';
135 if (SMB_VFS_STAT(conn,name,&st) == 0) {
136 *pst = st;
138 DEBUG(5,("conversion finished \"\" -> %s\n",name));
139 return(True);
143 * Ensure saved_last_component is valid even if file exists.
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);
154 #if 1
155 if (!conn->case_preserve || (mangle_is_8_3(name, False) && !conn->short_case_preserve))
156 #else
157 if (!conn->case_sensitive && (!conn->case_preserve || (mangle_is_8_3(name, False) && !conn->short_case_preserve)))
158 #endif
159 strnorm(name, lp_defaultcase(SNUM(conn)));
161 start = name;
162 pstrcpy(orig_path, name);
164 if(!conn->case_sensitive && stat_cache_lookup(conn, name, dirpath, &start, &st)) {
165 *pst = st;
166 return True;
170 * stat the name - if it exists then we are all done!
173 if (SMB_VFS_STAT(conn,name,&st) == 0) {
174 stat_cache_add(orig_path, name, conn->case_sensitive);
175 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
176 *pst = st;
177 return(True);
180 DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n", name, dirpath, start));
183 * A special case - if we don't have any mangling chars and are case
184 * sensitive then searching won't help.
187 if (conn->case_sensitive && !mangle_is_mangled(name) && !*lp_mangled_map(SNUM(conn)))
188 return(False);
190 name_has_wildcard = ms_has_wild(start);
193 * is_mangled() was changed to look at an entire pathname, not
194 * just a component. JRA.
197 if (mangle_is_mangled(start))
198 component_was_mangled = True;
201 * Now we need to recursively match the name against the real
202 * directory structure.
206 * Match each part of the path name separately, trying the names
207 * as is first, then trying to scan the directory for matching names.
210 for (; start ; start = (end?end+1:(char *)NULL)) {
212 * Pinpoint the end of this section of the filename.
214 end = strchr_m(start, '/');
217 * Chop the name at this point.
219 if (end)
220 *end = 0;
222 if(saved_last_component != 0)
223 pstrcpy(saved_last_component, end ? end + 1 : start);
226 * Check if the name exists up to this point.
229 if (SMB_VFS_STAT(conn,name, &st) == 0) {
231 * It exists. it must either be a directory or this must be
232 * the last part of the path for it to be OK.
234 if (end && !(st.st_mode & S_IFDIR)) {
236 * An intermediate part of the name isn't a directory.
238 DEBUG(5,("Not a dir %s\n",start));
239 *end = '/';
241 * We need to return the fact that the intermediate
242 * name resolution failed. This is used to return an
243 * error of ERRbadpath rather than ERRbadfile. Some
244 * Windows applications depend on the difference between
245 * these two errors.
247 errno = ENOTDIR;
248 *bad_path = True;
249 return(False);
252 if (!end) {
254 * We just scanned for, and found the end of the path.
255 * We must return the valid stat struct.
256 * JRA.
259 *pst = st;
262 } else {
263 pstring rest;
265 /* Stat failed - ensure we don't use it. */
266 ZERO_STRUCT(st);
267 *rest = 0;
270 * Remember the rest of the pathname so it can be restored
271 * later.
274 if (end)
275 pstrcpy(rest,end+1);
277 /* Reset errno so we can detect directory open errors. */
278 errno = 0;
281 * Try to find this part of the path in the directory.
284 if (ms_has_wild(start) ||
285 !scan_directory(dirpath, start,
286 sizeof(pstring) - 1 - (start - name),
287 conn,
288 end?True:False)) {
289 if (end) {
291 * An intermediate part of the name can't be found.
293 DEBUG(5,("Intermediate not found %s\n",start));
294 *end = '/';
297 * We need to return the fact that the intermediate
298 * name resolution failed. This is used to return an
299 * error of ERRbadpath rather than ERRbadfile. Some
300 * Windows applications depend on the difference between
301 * these two errors.
303 *bad_path = True;
304 return(False);
307 if (errno == ENOTDIR) {
308 *bad_path = True;
309 return(False);
313 * Just the last part of the name doesn't exist.
314 * We may need to strupper() or strlower() it in case
315 * this conversion is being used for file creation
316 * purposes. If the filename is of mixed case then
317 * don't normalise it.
320 if (!conn->case_preserve && (!strhasupper(start) || !strhaslower(start)))
321 strnorm(start, lp_defaultcase(SNUM(conn)));
324 * check on the mangled stack to see if we can recover the
325 * base of the filename.
328 if (mangle_is_mangled(start)) {
329 mangle_check_cache( start, sizeof(pstring) - 1 - (start - name) );
332 DEBUG(5,("New file %s\n",start));
333 return(True);
337 * Restore the rest of the string. If the string was mangled the size
338 * may have changed.
340 if (end) {
341 end = start + strlen(start);
342 if (!safe_strcat(start, "/", sizeof(pstring) - 1 - (start - name)) ||
343 !safe_strcat(start, rest, sizeof(pstring) - 1 - (start - name))) {
344 return False;
346 *end = '\0';
347 } else {
349 * We just scanned for, and found the end of the path.
350 * We must return a valid stat struct if it exists.
351 * JRA.
354 if (SMB_VFS_STAT(conn,name, &st) == 0) {
355 *pst = st;
356 } else {
357 ZERO_STRUCT(st);
360 } /* end else */
363 * Add to the dirpath that we have resolved so far.
365 if (*dirpath)
366 pstrcat(dirpath,"/");
368 pstrcat(dirpath,start);
371 * Don't cache a name with mangled or wildcard components
372 * as this can change the size.
375 if(!component_was_mangled && !name_has_wildcard)
376 stat_cache_add(orig_path, dirpath, conn->case_sensitive);
379 * Restore the / that we wiped out earlier.
381 if (end)
382 *end = '/';
386 * Don't cache a name with mangled or wildcard components
387 * as this can change the size.
390 if(!component_was_mangled && !name_has_wildcard)
391 stat_cache_add(orig_path, name, conn->case_sensitive);
394 * The name has been resolved.
397 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
398 return(True);
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 ****************************************************************************/
408 BOOL check_name(pstring name,connection_struct *conn)
410 BOOL ret = True;
412 if (IS_VETO_PATH(conn, name)) {
413 /* Is it not dot or dot dot. */
414 if (!((name[0] == '.') && (!name[1] || (name[1] == '.' && !name[2])))) {
415 DEBUG(5,("file path name %s vetoed\n",name));
416 errno = ENOENT;
417 return False;
421 if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
422 ret = reduce_name(conn,name);
425 if (!ret) {
426 DEBUG(5,("check_name on %s failed\n",name));
429 return(ret);
432 /****************************************************************************
433 Scan a directory to find a filename, matching without case sensitivity.
434 If the name looks like a mangled name then try via the mangling functions
435 ****************************************************************************/
437 static BOOL scan_directory(const char *path, char *name, size_t maxlength,
438 connection_struct *conn,BOOL docache)
440 void *cur_dir;
441 const char *dname;
442 BOOL mangled;
444 mangled = mangle_is_mangled(name);
446 /* handle null paths */
447 if (*path == 0)
448 path = ".";
450 if (docache && (dname = DirCacheCheck(path,name,SNUM(conn)))) {
451 safe_strcpy(name, dname, maxlength);
452 return(True);
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).
462 if (mangled)
463 mangled = !mangle_check_cache( name, maxlength );
465 /* open the directory */
466 if (!(cur_dir = OpenDir(conn, path, True))) {
467 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
468 return(False);
471 /* now scan for matching names */
472 while ((dname = ReadDirName(cur_dir))) {
474 /* Is it dot or dot dot. */
475 if ((dname[0] == '.') && (!dname[1] || (dname[1] == '.' && !dname[2]))) {
476 continue;
480 * At this point dname is the unmangled name.
481 * name is either mangled or not, depending on the state of the "mangled"
482 * variable. JRA.
486 * Check mangled name against mangled name, or unmangled name
487 * against unmangled name.
490 if ((mangled && mangled_equal(name,dname,SNUM(conn))) || fname_equal(name, dname, conn->case_sensitive)) {
491 /* we've found the file, change it's name and return */
492 if (docache)
493 DirCacheAdd(path,name,dname,SNUM(conn));
494 safe_strcpy(name, dname, maxlength);
495 CloseDir(cur_dir);
496 return(True);
500 CloseDir(cur_dir);
501 errno = ENOENT;
502 return(False);