r1564: checking in changes for 2.2.10
[Samba.git] / jerry2 / source / smbd / filename.c
blobc94fddf931f17718a6089a7fc946aed1e0814685
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 filename handling routines
5 Copyright (C) Andrew Tridgell 1992-1998
6 Copyright (C) Jeremy Allison 1999-200
7 Copyright (C) Ying Chen 2000
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * New hash table stat cache code added by Ying Chen.
28 #include "includes.h"
30 extern BOOL case_sensitive;
31 extern BOOL case_preserve;
32 extern BOOL short_case_preserve;
33 extern fstring remote_machine;
34 extern BOOL use_mangled_map;
36 static BOOL scan_directory(const char *path, char *name,size_t maxlength,
37 connection_struct *conn,BOOL docache);
39 /****************************************************************************
40 Check if two filenames are equal.
41 This needs to be careful about whether we are case sensitive.
42 ****************************************************************************/
44 static BOOL fname_equal(char *name1, char *name2)
46 /* Normal filename handling */
47 if (case_sensitive)
48 return(strcmp(name1,name2) == 0);
50 return(strequal(name1,name2));
53 /****************************************************************************
54 Mangle the 2nd name and check if it is then equal to the first name.
55 ****************************************************************************/
57 static BOOL mangled_equal(char *name1, const char *name2, int snum)
59 pstring tmpname;
61 pstrcpy(tmpname,name2);
62 mangle_map(tmpname,True,False,snum);
63 return strequal(name1,tmpname);
66 /****************************************************************************
67 This routine is called to convert names from the dos namespace to unix
68 namespace. It needs to handle any case conversions, mangling, format
69 changes etc.
71 We assume that we have already done a chdir() to the right "root" directory
72 for this service.
74 The function will return False if some part of the name except for the last
75 part cannot be resolved
77 If the saved_last_component != 0, then the unmodified last component
78 of the pathname is returned there. This is used in an exceptional
79 case in reply_mv (so far). If saved_last_component == 0 then nothing
80 is returned there.
82 The bad_path arg is set to True if the filename walk failed. This is
83 used to pick the correct error code to return between ENOENT and ENOTDIR
84 as Windows applications depend on ERRbadpath being returned if a component
85 of a pathname does not exist.
87 On exit from unix_convert, if *pst was not null, then the file stat
88 struct will be returned if the file exists and was found, if not this
89 stat struct will be filled with zeros (and this can be detected by checking
90 for nlinks = 0, which can never be true for any file).
91 ****************************************************************************/
93 BOOL unix_convert(char *name,connection_struct *conn,char *saved_last_component,
94 BOOL *bad_path, SMB_STRUCT_STAT *pst)
96 SMB_STRUCT_STAT st;
97 char *start, *end;
98 pstring dirpath;
99 pstring orig_path;
100 BOOL component_was_mangled = False;
101 BOOL name_has_wildcard = False;
102 #if 0
103 /* Andrew's conservative code... JRA. */
104 extern char magic_char;
105 #endif
107 ZERO_STRUCTP(pst);
109 *dirpath = 0;
110 *bad_path = False;
111 if(saved_last_component)
112 *saved_last_component = 0;
114 if (conn->printer) {
115 /* we don't ever use the filenames on a printer share as a
116 filename - so don't convert them */
117 return True;
120 DEBUG(5, ("unix_convert called on file \"%s\"\n", name));
123 * Convert to basic unix format - removing \ chars and cleaning it up.
126 unix_format(name);
127 unix_clean_name(name);
130 * Names must be relative to the root of the service - trim any leading /.
131 * also trim trailing /'s.
134 trim_string(name,"/","/");
137 * If we trimmed down to a single '\0' character
138 * then we should use the "." directory to avoid
139 * searching the cache, but not if we are in a
140 * printing share.
143 if (!*name) {
144 name[0] = '.';
145 name[1] = '\0';
149 * Ensure saved_last_component is valid even if file exists.
152 if(saved_last_component) {
153 end = strrchr(name, '/');
154 if(end)
155 pstrcpy(saved_last_component, end + 1);
156 else
157 pstrcpy(saved_last_component, name);
160 if (!case_sensitive && (!case_preserve || (mangle_is_8_3(name, False) && !short_case_preserve)))
161 strnorm(name);
164 * If we trimmed down to a single '\0' character
165 * then we will be using the "." directory.
166 * As we know this is valid we can return true here.
169 if(!*name)
170 return(True);
172 start = name;
173 while (strncmp(start,"./",2) == 0)
174 start += 2;
176 pstrcpy(orig_path, name);
178 if(stat_cache_lookup(conn, name, dirpath, &start, &st)) {
179 *pst = st;
180 return True;
184 * stat the name - if it exists then we are all done!
187 if (vfs_stat(conn,name,&st) == 0) {
188 stat_cache_add(orig_path, name);
189 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
190 *pst = st;
191 return(True);
194 DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n", name, dirpath, start));
197 * A special case - if we don't have any mangling chars and are case
198 * sensitive then searching won't help.
201 if (case_sensitive && !mangle_is_mangled(name) && !use_mangled_map)
202 return(False);
204 name_has_wildcard = ms_has_wild(start);
207 * is_mangled() was changed to look at an entire pathname, not
208 * just a component. JRA.
211 if(mangle_is_mangled(start))
212 component_was_mangled = True;
215 * Now we need to recursively match the name against the real
216 * directory structure.
220 * Match each part of the path name separately, trying the names
221 * as is first, then trying to scan the directory for matching names.
224 for (; start ; start = (end?end+1:(char *)NULL)) {
226 * Pinpoint the end of this section of the filename.
228 end = strchr(start, '/');
231 * Chop the name at this point.
233 if (end)
234 *end = 0;
236 if(saved_last_component != 0)
237 pstrcpy(saved_last_component, end ? end + 1 : start);
240 * Check if the name exists up to this point.
243 if (vfs_stat(conn,name, &st) == 0) {
245 * It exists. it must either be a directory or this must be
246 * the last part of the path for it to be OK.
248 if (end && !(st.st_mode & S_IFDIR)) {
250 * An intermediate part of the name isn't a directory.
252 DEBUG(5,("Not a dir %s\n",start));
253 *end = '/';
254 return(False);
257 } else {
258 pstring rest;
260 /* Stat failed - ensure we don't use it. */
261 ZERO_STRUCT(st);
262 *rest = 0;
265 * Remember the rest of the pathname so it can be restored
266 * later.
269 if (end)
270 pstrcpy(rest,end+1);
273 * Try to find this part of the path in the directory.
276 if (ms_has_wild(start) ||
277 !scan_directory(dirpath, start,
278 sizeof(pstring) - 1 - (start - name),
279 conn,
280 end?True:False)) {
281 if (end) {
283 * An intermediate part of the name can't be found.
285 DEBUG(5,("Intermediate not found %s\n",start));
286 *end = '/';
289 * We need to return the fact that the intermediate
290 * name resolution failed. This is used to return an
291 * error of ERRbadpath rather than ERRbadfile. Some
292 * Windows applications depend on the difference between
293 * these two errors.
295 *bad_path = True;
296 return(False);
300 * Just the last part of the name doesn't exist.
301 * We may need to strupper() or strlower() it in case
302 * this conversion is being used for file creation
303 * purposes. If the filename is of mixed case then
304 * don't normalise it.
307 if (!case_preserve && (!strhasupper(start) || !strhaslower(start)))
308 strnorm(start);
311 * check on the mangled stack to see if we can recover the
312 * base of the filename.
315 if (mangle_is_mangled(start)) {
316 mangle_check_cache( start, sizeof(pstring) - 1 - (start - name) );
319 DEBUG(5,("New file %s\n",start));
320 return(True);
324 * Restore the rest of the string. If the string was mangled the size
325 * may have changed.
327 if (end) {
328 end = start + strlen(start);
329 if (!safe_strcat(start, "/", sizeof(pstring) - 1 - (start - name)) ||
330 !safe_strcat(start, rest, sizeof(pstring) - 1 - (start - name))) {
331 return False;
333 *end = '\0';
334 } else {
336 * We just scanned for, and found the end of the path.
337 * We must return a valid stat struct if it exists.
338 * JRA.
341 if (vfs_stat(conn,name, &st) == 0) {
342 *pst = st;
343 } else {
344 ZERO_STRUCT(st);
347 } /* end else */
350 * Add to the dirpath that we have resolved so far.
352 if (*dirpath)
353 pstrcat(dirpath,"/");
355 pstrcat(dirpath,start);
358 * Don't cache a name with mangled or wildcard components
359 * as this can change the size.
362 if(!component_was_mangled && !name_has_wildcard)
363 stat_cache_add(orig_path, dirpath);
366 * Restore the / that we wiped out earlier.
368 if (end)
369 *end = '/';
373 * Don't cache a name with mangled or wildcard components
374 * as this can change the size.
377 if(!component_was_mangled && !name_has_wildcard)
378 stat_cache_add(orig_path, name);
381 * If we ended up resolving the entire path then return a valid
382 * stat struct if we got one.
385 if (VALID_STAT(st) && (strlen(orig_path) == strlen(name)))
386 *pst = st;
389 * The name has been resolved.
392 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
393 return(True);
396 /****************************************************************************
397 Check a filename - possibly caling reducename.
398 This is called by every routine before it allows an operation on a filename.
399 It does any final confirmation necessary to ensure that the filename is
400 a valid one for the user to access.
401 ****************************************************************************/
403 BOOL check_name(char *name,connection_struct *conn)
405 BOOL ret;
407 errno = 0;
409 if(IS_VETO_PATH(conn, name)) {
410 if(strcmp(name, ".") && strcmp(name, "..")) {
411 DEBUG(5,("file path name %s vetoed\n",name));
412 return(0);
416 ret = reduce_name(conn,name,conn->connectpath,lp_widelinks(SNUM(conn)));
418 /* Check if we are allowing users to follow symlinks */
419 /* Patch from David Clerc <David.Clerc@cui.unige.ch>
420 University of Geneva */
422 #ifdef S_ISLNK
423 if (!lp_symlinks(SNUM(conn))) {
424 SMB_STRUCT_STAT statbuf;
425 if ( (conn->vfs_ops.lstat(conn,dos_to_unix_static(name),&statbuf) != -1) &&
426 (S_ISLNK(statbuf.st_mode)) ) {
427 DEBUG(3,("check_name: denied: file path name %s is a symlink\n",name));
428 ret=0;
431 #endif
433 if (!ret)
434 DEBUG(5,("check_name on %s failed\n",name));
436 return(ret);
439 /****************************************************************************
440 Scan a directory to find a filename, matching without case sensitivity.
441 If the name looks like a mangled name then try via the mangling functions
442 ****************************************************************************/
444 static BOOL scan_directory(const char *path, char *name,size_t maxlength,
445 connection_struct *conn,BOOL docache)
447 void *cur_dir;
448 char *dname;
449 BOOL mangled;
451 mangled = mangle_is_mangled(name);
453 /* handle null paths */
454 if (*path == 0)
455 path = ".";
457 if (docache && (dname = DirCacheCheck(path,name,SNUM(conn)))) {
458 safe_strcpy(name, dname, maxlength);
459 return(True);
463 * The incoming name can be mangled, and if we de-mangle it
464 * here it will not compare correctly against the filename (name2)
465 * read from the directory and then mangled by the name_map_mangle()
466 * call. We need to mangle both names or neither.
467 * (JRA).
469 if (mangled)
470 mangled = !mangle_check_cache( name, maxlength );
472 /* open the directory */
473 if (!(cur_dir = OpenDir(conn, path, True))) {
474 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
475 return(False);
478 /* now scan for matching names */
479 while ((dname = ReadDirName(cur_dir))) {
480 if (*dname == '.' && (strequal(dname,".") || strequal(dname,"..")))
481 continue;
484 * At this point dname is the unmangled name.
485 * name is either mangled or not, depending on the state of the "mangled"
486 * variable. JRA.
490 * Check mangled name against mangled name, or unmangled name
491 * against unmangled name.
494 if ((mangled && mangled_equal(name,dname,SNUM(conn))) || fname_equal(name, dname)) {
495 /* we've found the file, change it's name and return */
496 if (docache)
497 DirCacheAdd(path,name,dname,SNUM(conn));
498 safe_strcpy(name, dname, maxlength);
499 CloseDir(cur_dir);
500 return(True);
504 CloseDir(cur_dir);
505 return(False);