Samba 3: added Samba 3.0.24 sources
[tomato.git] / release / src / router / samba3 / source / smbd / filename.c
blob6c0f8b77585ff06c56b15efdcadd3dbc0db8bc69
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(connection_struct *conn, const char *path, char *name,size_t maxlength);
31 /****************************************************************************
32 Check if two filenames are equal.
33 This needs to be careful about whether we are case sensitive.
34 ****************************************************************************/
36 static BOOL fname_equal(const char *name1, const char *name2, BOOL case_sensitive)
38 /* Normal filename handling */
39 if (case_sensitive)
40 return(strcmp(name1,name2) == 0);
42 return(strequal(name1,name2));
45 /****************************************************************************
46 Mangle the 2nd name and check if it is then equal to the first name.
47 ****************************************************************************/
49 static BOOL mangled_equal(const char *name1, const char *name2, int snum)
51 pstring tmpname;
53 pstrcpy(tmpname, name2);
54 mangle_map(tmpname, True, False, snum);
55 return strequal(name1, tmpname);
58 /****************************************************************************
59 This routine is called to convert names from the dos namespace to unix
60 namespace. It needs to handle any case conversions, mangling, format
61 changes etc.
63 We assume that we have already done a chdir() to the right "root" directory
64 for this service.
66 The function will return False if some part of the name except for the last
67 part cannot be resolved
69 If the saved_last_component != 0, then the unmodified last component
70 of the pathname is returned there. This is used in an exceptional
71 case in reply_mv (so far). If saved_last_component == 0 then nothing
72 is returned there.
74 The bad_path arg is set to True if the filename walk failed. This is
75 used to pick the correct error code to return between ENOENT and ENOTDIR
76 as Windows applications depend on ERRbadpath being returned if a component
77 of a pathname does not exist.
79 On exit from unix_convert, if *pst was not null, then the file stat
80 struct will be returned if the file exists and was found, if not this
81 stat struct will be filled with zeros (and this can be detected by checking
82 for nlinks = 0, which can never be true for any file).
83 ****************************************************************************/
85 BOOL unix_convert(pstring name,connection_struct *conn,char *saved_last_component,
86 BOOL *bad_path, SMB_STRUCT_STAT *pst)
88 SMB_STRUCT_STAT st;
89 char *start, *end;
90 pstring dirpath;
91 pstring orig_path;
92 BOOL component_was_mangled = False;
93 BOOL name_has_wildcard = False;
95 SET_STAT_INVALID(*pst);
97 *dirpath = 0;
98 *bad_path = False;
99 if(saved_last_component)
100 *saved_last_component = 0;
102 if (conn->printer) {
103 /* we don't ever use the filenames on a printer share as a
104 filename - so don't convert them */
105 return True;
108 DEBUG(5, ("unix_convert called on file \"%s\"\n", name));
111 * Conversion to basic unix format is already done in check_path_syntax().
115 * Names must be relative to the root of the service - any leading /.
116 * and trailing /'s should have been trimmed by check_path_syntax().
119 #ifdef DEVELOPER
120 SMB_ASSERT(*name != '/');
121 #endif
124 * If we trimmed down to a single '\0' character
125 * then we should use the "." directory to avoid
126 * searching the cache, but not if we are in a
127 * printing share.
128 * As we know this is valid we can return true here.
131 if (!*name) {
132 name[0] = '.';
133 name[1] = '\0';
134 if (SMB_VFS_STAT(conn,name,&st) == 0) {
135 *pst = st;
137 DEBUG(5,("conversion finished \"\" -> %s\n",name));
138 return(True);
142 * Ensure saved_last_component is valid even if file exists.
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);
154 * Large directory fix normalization. If we're case sensitive, and
155 * the case preserving parameters are set to "no", normalize the case of
156 * the incoming filename from the client WHETHER IT EXISTS OR NOT !
157 * This is in conflict with the current (3.0.20) man page, but is
158 * what people expect from the "large directory howto". I'll update
159 * the man page. Thanks to jht@samba.org for finding this. JRA.
162 if (conn->case_sensitive && !conn->case_preserve && !conn->short_case_preserve) {
163 strnorm(name, lp_defaultcase(SNUM(conn)));
166 start = name;
167 pstrcpy(orig_path, name);
169 if(!conn->case_sensitive && stat_cache_lookup(conn, name, dirpath, &start, &st)) {
170 *pst = st;
171 return True;
175 * stat the name - if it exists then we are all done!
178 if (SMB_VFS_STAT(conn,name,&st) == 0) {
179 stat_cache_add(orig_path, name, conn->case_sensitive);
180 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
181 *pst = st;
182 return(True);
185 DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n", name, dirpath, start));
188 * A special case - if we don't have any mangling chars and are case
189 * sensitive then searching won't help.
192 if (conn->case_sensitive && !mangle_is_mangled(name, SNUM(conn)) && !*lp_mangled_map(SNUM(conn)))
193 return(False);
195 name_has_wildcard = ms_has_wild(start);
198 * is_mangled() was changed to look at an entire pathname, not
199 * just a component. JRA.
202 if (mangle_is_mangled(start, SNUM(conn)))
203 component_was_mangled = True;
206 * Now we need to recursively match the name against the real
207 * directory structure.
211 * Match each part of the path name separately, trying the names
212 * as is first, then trying to scan the directory for matching names.
215 for (; start ; start = (end?end+1:(char *)NULL)) {
217 * Pinpoint the end of this section of the filename.
219 end = strchr_m(start, '/');
222 * Chop the name at this point.
224 if (end)
225 *end = 0;
227 if(saved_last_component != 0)
228 pstrcpy(saved_last_component, end ? end + 1 : start);
231 * Check if the name exists up to this point.
234 if (SMB_VFS_STAT(conn,name, &st) == 0) {
236 * It exists. it must either be a directory or this must be
237 * the last part of the path for it to be OK.
239 if (end && !(st.st_mode & S_IFDIR)) {
241 * An intermediate part of the name isn't a directory.
243 DEBUG(5,("Not a dir %s\n",start));
244 *end = '/';
246 * We need to return the fact that the intermediate
247 * name resolution failed. This is used to return an
248 * error of ERRbadpath rather than ERRbadfile. Some
249 * Windows applications depend on the difference between
250 * these two errors.
252 errno = ENOTDIR;
253 *bad_path = True;
254 return(False);
257 if (!end) {
259 * We just scanned for, and found the end of the path.
260 * We must return the valid stat struct.
261 * JRA.
264 *pst = st;
267 } else {
268 pstring rest;
270 /* Stat failed - ensure we don't use it. */
271 SET_STAT_INVALID(st);
272 *rest = 0;
275 * Remember the rest of the pathname so it can be restored
276 * later.
279 if (end)
280 pstrcpy(rest,end+1);
282 /* Reset errno so we can detect directory open errors. */
283 errno = 0;
286 * Try to find this part of the path in the directory.
289 if (ms_has_wild(start) ||
290 !scan_directory(conn, dirpath, start, sizeof(pstring) - 1 - (start - name))) {
291 if (end) {
293 * An intermediate part of the name can't be found.
295 DEBUG(5,("Intermediate not found %s\n",start));
296 *end = '/';
299 * We need to return the fact that the intermediate
300 * name resolution failed. This is used to return an
301 * error of ERRbadpath rather than ERRbadfile. Some
302 * Windows applications depend on the difference between
303 * these two errors.
305 *bad_path = True;
306 return(False);
309 if (errno == ENOTDIR) {
310 *bad_path = True;
311 return(False);
315 * Just the last part of the name doesn't exist.
316 * We need to strupper() or strlower() it as
317 * this conversion may be used for file creation
318 * purposes. Fix inspired by Thomas Neumann <t.neumann@iku-ag.de>.
320 if (!conn->case_preserve ||
321 (mangle_is_8_3(start, False, SNUM(conn)) &&
322 !conn->short_case_preserve)) {
323 strnorm(start, lp_defaultcase(SNUM(conn)));
327 * check on the mangled stack to see if we can recover the
328 * base of the filename.
331 if (mangle_is_mangled(start, SNUM(conn))) {
332 mangle_check_cache( start, sizeof(pstring) - 1 - (start - name), SNUM(conn));
335 DEBUG(5,("New file %s\n",start));
336 return(True);
340 * Restore the rest of the string. If the string was mangled the size
341 * may have changed.
343 if (end) {
344 end = start + strlen(start);
345 if (!safe_strcat(start, "/", sizeof(pstring) - 1 - (start - name)) ||
346 !safe_strcat(start, rest, sizeof(pstring) - 1 - (start - name))) {
347 return False;
349 *end = '\0';
350 } else {
352 * We just scanned for, and found the end of the path.
353 * We must return a valid stat struct if it exists.
354 * JRA.
357 if (SMB_VFS_STAT(conn,name, &st) == 0) {
358 *pst = st;
359 } else {
360 SET_STAT_INVALID(st);
363 } /* end else */
366 * Add to the dirpath that we have resolved so far.
368 if (*dirpath)
369 pstrcat(dirpath,"/");
371 pstrcat(dirpath,start);
374 * Don't cache a name with mangled or wildcard components
375 * as this can change the size.
378 if(!component_was_mangled && !name_has_wildcard)
379 stat_cache_add(orig_path, dirpath, conn->case_sensitive);
382 * Restore the / that we wiped out earlier.
384 if (end)
385 *end = '/';
389 * Don't cache a name with mangled or wildcard components
390 * as this can change the size.
393 if(!component_was_mangled && !name_has_wildcard)
394 stat_cache_add(orig_path, name, conn->case_sensitive);
397 * The name has been resolved.
400 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
401 return(True);
404 /****************************************************************************
405 Check a filename - possibly caling reducename.
406 This is called by every routine before it allows an operation on a filename.
407 It does any final confirmation necessary to ensure that the filename is
408 a valid one for the user to access.
409 ****************************************************************************/
411 BOOL check_name(const pstring name,connection_struct *conn)
413 BOOL ret = True;
415 if (IS_VETO_PATH(conn, name)) {
416 /* Is it not dot or dot dot. */
417 if (!((name[0] == '.') && (!name[1] || (name[1] == '.' && !name[2])))) {
418 DEBUG(5,("file path name %s vetoed\n",name));
419 errno = ENOENT;
420 return False;
424 if (!lp_widelinks(SNUM(conn)) || !lp_symlinks(SNUM(conn))) {
425 ret = reduce_name(conn,name);
428 if (!ret) {
429 DEBUG(5,("check_name on %s failed\n",name));
432 return(ret);
435 /****************************************************************************
436 Scan a directory to find a filename, matching without case sensitivity.
437 If the name looks like a mangled name then try via the mangling functions
438 ****************************************************************************/
440 static BOOL scan_directory(connection_struct *conn, const char *path, char *name, size_t maxlength)
442 struct smb_Dir *cur_dir;
443 const char *dname;
444 BOOL mangled;
445 long curpos;
447 mangled = mangle_is_mangled(name, SNUM(conn));
449 /* handle null paths */
450 if (*path == 0)
451 path = ".";
454 * The incoming name can be mangled, and if we de-mangle it
455 * here it will not compare correctly against the filename (name2)
456 * read from the directory and then mangled by the mangle_map()
457 * call. We need to mangle both names or neither.
458 * (JRA).
460 * Fix for bug found by Dina Fine. If in case sensitive mode then
461 * the mangle cache is no good (3 letter extension could be wrong
462 * case - so don't demangle in this case - leave as mangled and
463 * allow the mangling of the directory entry read (which is done
464 * case insensitively) to match instead. This will lead to more
465 * false positive matches but we fail completely without it. JRA.
468 if (mangled && !conn->case_sensitive) {
469 mangled = !mangle_check_cache( name, maxlength, SNUM(conn));
472 /* open the directory */
473 if (!(cur_dir = OpenDir(conn, path, NULL, 0))) {
474 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
475 return(False);
478 /* now scan for matching names */
479 curpos = 0;
480 while ((dname = ReadDirName(cur_dir, &curpos))) {
482 /* Is it dot or dot dot. */
483 if ((dname[0] == '.') && (!dname[1] || (dname[1] == '.' && !dname[2]))) {
484 continue;
488 * At this point dname is the unmangled name.
489 * name is either mangled or not, depending on the state of the "mangled"
490 * variable. JRA.
494 * Check mangled name against mangled name, or unmangled name
495 * against unmangled name.
498 if ((mangled && mangled_equal(name,dname,SNUM(conn))) || fname_equal(name, dname, conn->case_sensitive)) {
499 /* we've found the file, change it's name and return */
500 safe_strcpy(name, dname, maxlength);
501 CloseDir(cur_dir);
502 return(True);
506 CloseDir(cur_dir);
507 errno = ENOENT;
508 return(False);