2 Unix SMB/Netbios implementation.
4 filename handling routines
5 Copyright (C) Andrew Tridgell 1992-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 extern int DEBUGLEVEL;
25 extern BOOL case_sensitive;
26 extern BOOL case_preserve;
27 extern BOOL short_case_preserve;
28 extern fstring remote_machine;
29 extern BOOL use_mangled_map;
31 static BOOL scan_directory(char *path, char *name,connection_struct *conn,BOOL docache);
33 /****************************************************************************
34 Check if two filenames are equal.
35 This needs to be careful about whether we are case sensitive.
36 ****************************************************************************/
37 static BOOL fname_equal(char *name1, char *name2)
39 int l1 = strlen(name1);
40 int l2 = strlen(name2);
42 /* handle filenames ending in a single dot */
43 if (l1-l2 == 1 && name1[l1-1] == '.' && lp_strip_dot())
47 ret = fname_equal(name1,name2);
52 if (l2-l1 == 1 && name2[l2-1] == '.' && lp_strip_dot())
56 ret = fname_equal(name1,name2);
61 /* now normal filename handling */
63 return(strcmp(name1,name2) == 0);
65 return(strequal(name1,name2));
69 /****************************************************************************
70 Mangle the 2nd name and check if it is then equal to the first name.
71 ****************************************************************************/
72 static BOOL mangled_equal(char *name1, char *name2)
76 if (is_8_3(name2, True))
79 pstrcpy(tmpname,name2);
80 mangle_name_83(tmpname);
82 return(strequal(name1,tmpname));
85 /****************************************************************************
86 Stat cache code used in unix_convert.
87 *****************************************************************************/
89 static int global_stat_cache_lookups;
90 static int global_stat_cache_misses;
91 static int global_stat_cache_hits;
93 /****************************************************************************
94 Stat cache statistics code.
95 *****************************************************************************/
97 void print_stat_cache_statistics(void)
99 double eff = (100.0* (double)global_stat_cache_hits)/(double)global_stat_cache_lookups;
101 DEBUG(0,("stat cache stats: lookups = %d, hits = %d, misses = %d, \
102 stat cache was %f%% effective.\n", global_stat_cache_lookups,
103 global_stat_cache_hits, global_stat_cache_misses, eff ));
110 pstring translated_name;
113 #define MAX_STAT_CACHE_SIZE 50
115 static ubi_dlList stat_cache = { NULL, (ubi_dlNodePtr)&stat_cache, 0};
117 /****************************************************************************
118 Compare two names in the stat cache - to check if we already have such an
120 *****************************************************************************/
122 static BOOL stat_name_equal( char *s1, char *s2)
124 return (case_sensitive ? (strcmp( s1, s2) == 0) : (StrCaseCmp(s1, s2) == 0));
127 /****************************************************************************
128 Compare a pathname to a name in the stat cache - of a given length.
129 Note - this code always checks that the next character in the pathname
130 is either a '/' character, or a '\0' character - to ensure we only
131 match *full* pathname components.
132 *****************************************************************************/
134 static BOOL stat_name_equal_len( char *stat_name, char *orig_name, int len)
136 BOOL matched = (case_sensitive ? (strncmp( stat_name, orig_name, len) == 0) :
137 (StrnCaseCmp(stat_name, orig_name, len) == 0));
138 if(orig_name[len] != '/' && orig_name[len] != '\0')
144 /****************************************************************************
145 Add an entry into the stat cache.
146 *****************************************************************************/
148 static void stat_cache_add( char *full_orig_name, char *orig_translated_path)
150 stat_cache_entry *scp;
152 pstring translated_path;
153 int namelen = strlen(orig_translated_path);
156 * Don't cache trivial valid directory entries.
158 if(strequal(full_orig_name, ".") || strequal(full_orig_name, ".."))
162 * If we are in case insentive mode, we need to
163 * store names that need no translation - else, it
167 if(case_sensitive && (strcmp(full_orig_name, orig_translated_path) == 0))
171 * Remove any trailing '/' characters from the
175 pstrcpy(translated_path, orig_translated_path);
176 if(translated_path[namelen-1] == '/') {
177 translated_path[namelen-1] = '\0';
182 * We will only replace namelen characters
184 * StrnCpy always null terminates.
187 StrnCpy(orig_name, full_orig_name, namelen);
190 * Check this name doesn't exist in the cache before we
194 for( scp = (stat_cache_entry *)ubi_dlFirst( &stat_cache); scp;
195 scp = (stat_cache_entry *)ubi_dlNext( scp )) {
196 if(stat_name_equal( scp->orig_name, orig_name) &&
197 (strcmp( scp->translated_name, translated_path) == 0)) {
199 * Name does exist - promote it.
201 if( (stat_cache_entry *)ubi_dlFirst( &stat_cache) != scp ) {
202 ubi_dlRemThis( &stat_cache, scp);
203 ubi_dlAddHead( &stat_cache, scp);
209 if((scp = (stat_cache_entry *)malloc(sizeof(stat_cache_entry))) == NULL) {
210 DEBUG(0,("stat_cache_add: Out of memory !\n"));
214 pstrcpy(scp->orig_name, orig_name);
215 pstrcpy(scp->translated_name, translated_path);
216 scp->name_len = namelen;
218 ubi_dlAddHead( &stat_cache, scp);
220 DEBUG(10,("stat_cache_add: Added entry %s -> %s\n", scp->orig_name, scp->translated_name ));
222 if(ubi_dlCount(&stat_cache) > lp_stat_cache_size()) {
223 scp = (stat_cache_entry *)ubi_dlRemTail( &stat_cache );
229 /****************************************************************************
230 Look through the stat cache for an entry - promote it to the top if found.
231 Return True if we translated (and did a scuccessful stat on) the entire name.
232 *****************************************************************************/
234 static BOOL stat_cache_lookup( char *name, char *dirpath, char **start, SMB_STRUCT_STAT *pst)
236 stat_cache_entry *scp;
237 stat_cache_entry *longest_hit = NULL;
238 int namelen = strlen(name);
241 global_stat_cache_lookups++;
244 * Don't lookup trivial valid directory entries.
246 if(strequal(name, ".") || strequal(name, "..")) {
247 global_stat_cache_misses++;
251 for( scp = (stat_cache_entry *)ubi_dlFirst( &stat_cache); scp;
252 scp = (stat_cache_entry *)ubi_dlNext( scp )) {
253 if(scp->name_len <= namelen) {
254 if(stat_name_equal_len(scp->orig_name, name, scp->name_len)) {
255 if((longest_hit == NULL) || (longest_hit->name_len <= scp->name_len))
261 if(longest_hit == NULL) {
262 DEBUG(10,("stat_cache_lookup: cache miss on %s\n", name));
263 global_stat_cache_misses++;
267 global_stat_cache_hits++;
269 DEBUG(10,("stat_cache_lookup: cache hit for name %s. %s -> %s\n",
270 name, longest_hit->orig_name, longest_hit->translated_name ));
273 * longest_hit is the longest match we got in the list.
274 * Check it exists - if so, overwrite the original name
275 * and then promote it to the top.
278 if(dos_stat( longest_hit->translated_name, pst) != 0) {
280 * Discard this entry.
282 ubi_dlRemThis( &stat_cache, longest_hit);
283 free((char *)longest_hit);
287 memcpy(name, longest_hit->translated_name, longest_hit->name_len);
288 if( (stat_cache_entry *)ubi_dlFirst( &stat_cache) != longest_hit ) {
289 ubi_dlRemThis( &stat_cache, longest_hit);
290 ubi_dlAddHead( &stat_cache, longest_hit);
293 *start = &name[longest_hit->name_len];
297 StrnCpy( dirpath, longest_hit->translated_name, name - (*start));
299 return (namelen == longest_hit->name_len);
302 /****************************************************************************
303 This routine is called to convert names from the dos namespace to unix
304 namespace. It needs to handle any case conversions, mangling, format
307 We assume that we have already done a chdir() to the right "root" directory
310 The function will return False if some part of the name except for the last
311 part cannot be resolved
313 If the saved_last_component != 0, then the unmodified last component
314 of the pathname is returned there. This is used in an exceptional
315 case in reply_mv (so far). If saved_last_component == 0 then nothing
318 The bad_path arg is set to True if the filename walk failed. This is
319 used to pick the correct error code to return between ENOENT and ENOTDIR
320 as Windows applications depend on ERRbadpath being returned if a component
321 of a pathname does not exist.
322 ****************************************************************************/
324 BOOL unix_convert(char *name,connection_struct *conn,char *saved_last_component,
325 BOOL *bad_path, SMB_STRUCT_STAT *pst)
328 char *start, *end, *orig_start;
332 BOOL component_was_mangled = False;
333 BOOL name_has_wildcard = False;
334 extern char magic_char;
342 if(saved_last_component)
343 *saved_last_component = 0;
346 * Convert to basic unix format - removing \ chars and cleaning it up.
350 unix_clean_name(name);
353 * Names must be relative to the root of the service - trim any leading /.
354 * also trim trailing /'s.
357 trim_string(name,"/","/");
360 * Ensure saved_last_component is valid even if file exists.
363 if(saved_last_component) {
364 end = strrchr(name, '/');
366 pstrcpy(saved_last_component, end + 1);
368 pstrcpy(saved_last_component, name);
371 if (!case_sensitive &&
372 (!case_preserve || (is_8_3(name, False) && !short_case_preserve)))
376 * Check if it's a printer file.
379 if ((! *name) || strchr(name,'/') || !is_8_3(name, True)) {
382 slprintf(name2,sizeof(name2)-1,"%.6s.XXXXXX",remote_machine);
388 for (s=name2 ; *s ; s++)
389 if (!issafe(*s)) *s = '_';
390 pstrcpy(name,(char *)mktemp(name2));
396 while (strncmp(start,"./",2) == 0)
399 pstrcpy(orig_path, name);
401 if(stat_cache_lookup( name, dirpath, &start, &st)) {
408 * stat the name - if it exists then we are all done!
411 if (dos_stat(name,&st) == 0) {
412 stat_cache_add(orig_path, name);
413 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
421 DEBUG(5,("unix_convert begin: name = %s, dirpath = %s, start = %s\n",
422 name, dirpath, start));
425 * A special case - if we don't have any mangling chars and are case
426 * sensitive then searching won't help.
429 if (case_sensitive && !is_mangled(name) &&
430 !lp_strip_dot() && !use_mangled_map && (saved_errno != ENOENT))
433 if(strchr(start,'?') || strchr(start,'*'))
434 name_has_wildcard = True;
436 /* this is an extremely conservative test for mangled names. */
437 if (strchr(start,magic_char))
438 component_was_mangled = True;
441 * Now we need to recursively match the name against the real
442 * directory structure.
446 * Match each part of the path name separately, trying the names
447 * as is first, then trying to scan the directory for matching names.
450 for (orig_start = start; start ; start = (end?end+1:(char *)NULL)) {
452 * Pinpoint the end of this section of the filename.
454 end = strchr(start, '/');
457 * Chop the name at this point.
462 if(saved_last_component != 0)
463 pstrcpy(saved_last_component, end ? end + 1 : start);
466 * Check if the name exists up to this point.
468 if (dos_stat(name, &st) == 0) {
470 * It exists. it must either be a directory or this must be
471 * the last part of the path for it to be OK.
473 if (end && !(st.st_mode & S_IFDIR)) {
475 * An intermediate part of the name isn't a directory.
477 DEBUG(5,("Not a dir %s\n",start));
488 * Remember the rest of the pathname so it can be restored
496 * Try to find this part of the path in the directory.
499 if (strchr(start,'?') || strchr(start,'*') ||
500 !scan_directory(dirpath, start, conn, end?True:False)) {
503 * An intermediate part of the name can't be found.
505 DEBUG(5,("Intermediate not found %s\n",start));
509 * We need to return the fact that the intermediate
510 * name resolution failed. This is used to return an
511 * error of ERRbadpath rather than ERRbadfile. Some
512 * Windows applications depend on the difference between
520 * Just the last part of the name doesn't exist.
521 * We may need to strupper() or strlower() it in case
522 * this conversion is being used for file creation
523 * purposes. If the filename is of mixed case then
524 * don't normalise it.
527 if (!case_preserve && (!strhasupper(start) || !strhaslower(start)))
531 * check on the mangled stack to see if we can recover the
532 * base of the filename.
535 if (is_mangled(start)) {
536 check_mangled_cache( start );
539 DEBUG(5,("New file %s\n",start));
544 * Restore the rest of the string.
547 pstrcpy(start+strlen(start)+1,rest);
548 end = start + strlen(start);
553 * Add to the dirpath that we have resolved so far.
556 pstrcat(dirpath,"/");
558 pstrcat(dirpath,start);
561 * Don't cache a name with mangled or wildcard components
562 * as this can change the size.
565 if(!component_was_mangled && !name_has_wildcard)
566 stat_cache_add(orig_path, dirpath);
569 * Restore the / that we wiped out earlier.
576 * Don't cache a name with mangled or wildcard components
577 * as this can change the size.
580 if(!component_was_mangled && !name_has_wildcard)
581 stat_cache_add(orig_path, name);
584 * The name has been resolved.
587 DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
592 /****************************************************************************
593 check a filename - possibly caling reducename
595 This is called by every routine before it allows an operation on a filename.
596 It does any final confirmation necessary to ensure that the filename is
597 a valid one for the user to access.
598 ****************************************************************************/
599 BOOL check_name(char *name,connection_struct *conn)
605 if (IS_VETO_PATH(conn, name)) {
606 DEBUG(5,("file path name %s vetoed\n",name));
610 ret = reduce_name(name,conn->connectpath,lp_widelinks(SNUM(conn)));
612 /* Check if we are allowing users to follow symlinks */
613 /* Patch from David Clerc <David.Clerc@cui.unige.ch>
614 University of Geneva */
617 if (!lp_symlinks(SNUM(conn)))
619 SMB_STRUCT_STAT statbuf;
620 if ( (dos_lstat(name,&statbuf) != -1) &&
621 (S_ISLNK(statbuf.st_mode)) )
623 DEBUG(3,("check_name: denied: file path name %s is a symlink\n",name));
630 DEBUG(5,("check_name on %s failed\n",name));
636 /****************************************************************************
637 scan a directory to find a filename, matching without case sensitivity
639 If the name looks like a mangled name then try via the mangling functions
640 ****************************************************************************/
641 static BOOL scan_directory(char *path, char *name,connection_struct *conn,BOOL docache)
648 mangled = is_mangled(name);
650 /* handle null paths */
654 if (docache && (dname = DirCacheCheck(path,name,SNUM(conn)))) {
655 pstrcpy(name, dname);
660 * The incoming name can be mangled, and if we de-mangle it
661 * here it will not compare correctly against the filename (name2)
662 * read from the directory and then mangled by the name_map_mangle()
663 * call. We need to mangle both names or neither.
667 mangled = !check_mangled_cache( name );
669 /* open the directory */
670 if (!(cur_dir = OpenDir(conn, path, True)))
672 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
676 /* now scan for matching names */
677 while ((dname = ReadDirName(cur_dir)))
680 (strequal(dname,".") || strequal(dname,"..")))
683 pstrcpy(name2,dname);
684 if (!name_map_mangle(name2,False,SNUM(conn))) continue;
686 if ((mangled && mangled_equal(name,name2))
687 || fname_equal(name, name2))
689 /* we've found the file, change it's name and return */
690 if (docache) DirCacheAdd(path,name,dname,SNUM(conn));
691 pstrcpy(name, dname);