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.
29 extern BOOL case_sensitive
;
30 extern BOOL case_preserve
;
31 extern BOOL short_case_preserve
;
32 extern BOOL use_mangled_map
;
34 static BOOL
scan_directory(const char *path
, char *name
,size_t maxlength
,
35 connection_struct
*conn
,BOOL docache
);
37 /****************************************************************************
38 Check if two filenames are equal.
39 This needs to be careful about whether we are case sensitive.
40 ****************************************************************************/
42 static BOOL
fname_equal(const char *name1
, const char *name2
)
44 /* Normal filename handling */
46 return(strcmp(name1
,name2
) == 0);
48 return(strequal(name1
,name2
));
51 /****************************************************************************
52 Mangle the 2nd name and check if it is then equal to the first name.
53 ****************************************************************************/
55 static BOOL
mangled_equal(const char *name1
, const char *name2
, int snum
)
59 pstrcpy(tmpname
, name2
);
60 mangle_map(tmpname
, True
, False
, snum
);
61 return strequal(name1
, tmpname
);
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
69 We assume that we have already done a chdir() to the right "root" directory
72 The function will return False if some part of the name except for the last
73 part cannot be resolved
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
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.
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 ****************************************************************************/
91 BOOL
unix_convert(pstring name
,connection_struct
*conn
,char *saved_last_component
,
92 BOOL
*bad_path
, SMB_STRUCT_STAT
*pst
)
98 BOOL component_was_mangled
= False
;
99 BOOL name_has_wildcard
= False
;
105 if(saved_last_component
)
106 *saved_last_component
= 0;
109 /* we don't ever use the filenames on a printer share as a
110 filename - so don't convert them */
114 DEBUG(5, ("unix_convert called on file \"%s\"\n", name
));
117 * Conversion to basic unix format is already done in check_path_syntax().
121 * Names must be relative to the root of the service - any leading /.
122 * and trailing /'s should have been trimmed by check_path_syntax().
126 SMB_ASSERT(*name
!= '/');
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
134 * As we know this is valid we can return true here.
144 * Ensure saved_last_component is valid even if file exists.
147 if(saved_last_component
) {
148 end
= strrchr_m(name
, '/');
150 pstrcpy(saved_last_component
, end
+ 1);
152 pstrcpy(saved_last_component
, name
);
155 if (!case_sensitive
&& (!case_preserve
|| (mangle_is_8_3(name
, False
) && !short_case_preserve
)))
159 pstrcpy(orig_path
, name
);
161 if(!case_sensitive
&& stat_cache_lookup(conn
, name
, dirpath
, &start
, &st
)) {
167 * stat the name - if it exists then we are all done!
170 if (SMB_VFS_STAT(conn
,name
,&st
) == 0) {
171 stat_cache_add(orig_path
, name
);
172 DEBUG(5,("conversion finished %s -> %s\n",orig_path
, name
));
177 DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n", name
, dirpath
, start
));
180 * A special case - if we don't have any mangling chars and are case
181 * sensitive then searching won't help.
184 if (case_sensitive
&& !mangle_is_mangled(name
) && !use_mangled_map
)
187 name_has_wildcard
= ms_has_wild(start
);
190 * is_mangled() was changed to look at an entire pathname, not
191 * just a component. JRA.
194 if (mangle_is_mangled(start
))
195 component_was_mangled
= True
;
198 * Now we need to recursively match the name against the real
199 * directory structure.
203 * Match each part of the path name separately, trying the names
204 * as is first, then trying to scan the directory for matching names.
207 for (; start
; start
= (end
?end
+1:(char *)NULL
)) {
209 * Pinpoint the end of this section of the filename.
211 end
= strchr_m(start
, '/');
214 * Chop the name at this point.
219 if(saved_last_component
!= 0)
220 pstrcpy(saved_last_component
, end
? end
+ 1 : start
);
223 * Check if the name exists up to this point.
226 if (SMB_VFS_STAT(conn
,name
, &st
) == 0) {
228 * It exists. it must either be a directory or this must be
229 * the last part of the path for it to be OK.
231 if (end
&& !(st
.st_mode
& S_IFDIR
)) {
233 * An intermediate part of the name isn't a directory.
235 DEBUG(5,("Not a dir %s\n",start
));
242 * We just scanned for, and found the end of the path.
243 * We must return the valid stat struct.
253 /* Stat failed - ensure we don't use it. */
258 * Remember the rest of the pathname so it can be restored
266 * Try to find this part of the path in the directory.
269 if (ms_has_wild(start
) ||
270 !scan_directory(dirpath
, start
,
271 sizeof(pstring
) - 1 - (start
- name
),
276 * An intermediate part of the name can't be found.
278 DEBUG(5,("Intermediate not found %s\n",start
));
282 * We need to return the fact that the intermediate
283 * name resolution failed. This is used to return an
284 * error of ERRbadpath rather than ERRbadfile. Some
285 * Windows applications depend on the difference between
293 * Just the last part of the name doesn't exist.
294 * We may need to strupper() or strlower() it in case
295 * this conversion is being used for file creation
296 * purposes. If the filename is of mixed case then
297 * don't normalise it.
300 if (!case_preserve
&& (!strhasupper(start
) || !strhaslower(start
)))
304 * check on the mangled stack to see if we can recover the
305 * base of the filename.
308 if (mangle_is_mangled(start
)) {
309 mangle_check_cache( start
);
312 DEBUG(5,("New file %s\n",start
));
317 * Restore the rest of the string. If the string was mangled the size
321 end
= start
+ strlen(start
);
322 if (!safe_strcat(start
, "/", sizeof(pstring
) - 1 - (start
- name
)) ||
323 !safe_strcat(start
, rest
, sizeof(pstring
) - 1 - (start
- name
))) {
329 * We just scanned for, and found the end of the path.
330 * We must return a valid stat struct if it exists.
334 if (SMB_VFS_STAT(conn
,name
, &st
) == 0) {
343 * Add to the dirpath that we have resolved so far.
346 pstrcat(dirpath
,"/");
348 pstrcat(dirpath
,start
);
351 * Don't cache a name with mangled or wildcard components
352 * as this can change the size.
355 if(!component_was_mangled
&& !name_has_wildcard
)
356 stat_cache_add(orig_path
, dirpath
);
359 * Restore the / that we wiped out earlier.
366 * Don't cache a name with mangled or wildcard components
367 * as this can change the size.
370 if(!component_was_mangled
&& !name_has_wildcard
)
371 stat_cache_add(orig_path
, name
);
374 * The name has been resolved.
377 DEBUG(5,("conversion finished %s -> %s\n",orig_path
, name
));
381 /****************************************************************************
382 Check a filename - possibly caling reducename.
383 This is called by every routine before it allows an operation on a filename.
384 It does any final confirmation necessary to ensure that the filename is
385 a valid one for the user to access.
386 ****************************************************************************/
388 BOOL
check_name(pstring name
,connection_struct
*conn
)
394 if (IS_VETO_PATH(conn
, name
)) {
395 /* Is it not dot or dot dot. */
396 if (!((name
[0] == '.') && (!name
[1] || (name
[1] == '.' && !name
[2])))) {
397 DEBUG(5,("file path name %s vetoed\n",name
));
402 if (!lp_widelinks(SNUM(conn
))) {
403 ret
= reduce_name(conn
,name
,conn
->connectpath
);
406 /* Check if we are allowing users to follow symlinks */
407 /* Patch from David Clerc <David.Clerc@cui.unige.ch>
408 University of Geneva */
411 if (!lp_symlinks(SNUM(conn
))) {
412 SMB_STRUCT_STAT statbuf
;
413 if ( (SMB_VFS_LSTAT(conn
,name
,&statbuf
) != -1) &&
414 (S_ISLNK(statbuf
.st_mode
)) ) {
415 DEBUG(3,("check_name: denied: file path name %s is a symlink\n",name
));
422 DEBUG(5,("check_name on %s failed\n",name
));
427 /****************************************************************************
428 Scan a directory to find a filename, matching without case sensitivity.
429 If the name looks like a mangled name then try via the mangling functions
430 ****************************************************************************/
432 static BOOL
scan_directory(const char *path
, char *name
, size_t maxlength
,
433 connection_struct
*conn
,BOOL docache
)
439 mangled
= mangle_is_mangled(name
);
441 /* handle null paths */
445 if (docache
&& (dname
= DirCacheCheck(path
,name
,SNUM(conn
)))) {
446 safe_strcpy(name
, dname
, maxlength
);
451 * The incoming name can be mangled, and if we de-mangle it
452 * here it will not compare correctly against the filename (name2)
453 * read from the directory and then mangled by the mangle_map()
454 * call. We need to mangle both names or neither.
458 mangled
= !mangle_check_cache( name
);
460 /* open the directory */
461 if (!(cur_dir
= OpenDir(conn
, path
, True
))) {
462 DEBUG(3,("scan dir didn't open dir [%s]\n",path
));
466 /* now scan for matching names */
467 while ((dname
= ReadDirName(cur_dir
))) {
469 /* Is it dot or dot dot. */
470 if ((dname
[0] == '.') && (!dname
[1] || (dname
[1] == '.' && !dname
[2]))) {
475 * At this point dname is the unmangled name.
476 * name is either mangled or not, depending on the state of the "mangled"
481 * Check mangled name against mangled name, or unmangled name
482 * against unmangled name.
485 if ((mangled
&& mangled_equal(name
,dname
,SNUM(conn
))) || fname_equal(name
, dname
)) {
486 /* we've found the file, change it's name and return */
488 DirCacheAdd(path
,name
,dname
,SNUM(conn
));
489 safe_strcpy(name
, dname
, maxlength
);