2 Unix SMB/Netbios implementation.
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.
22 /* -------------------------------------------------------------------------- **
25 * March/April 1998 CRH
26 * - Many of the functions in this module overwrite string buffers passed to
27 * them. This causes a variety of problems and is, generally speaking,
28 * dangerous and scarry. See the kludge notes in name_map_mangle()
30 * - It seems that something is calling name_map_mangle() twice. The
31 * first call is probably some sort of test. Names which contain
32 * illegal characters are being doubly mangled. I'm not sure, but
33 * I'm guessing the problem is in server.c.
35 * -------------------------------------------------------------------------- **
38 /* -------------------------------------------------------------------------- **
41 * March/April 1998 CRH
42 * Updated a bit. Rewrote is_mangled() to be a bit more selective.
43 * Rewrote the mangled name cache. Added comments here and there.
45 * -------------------------------------------------------------------------- **
51 /* -------------------------------------------------------------------------- **
52 * External Variables...
55 extern int case_default
; /* Are conforming 8.3 names all upper or lower? */
56 extern BOOL case_mangle
; /* If true, all chars in 8.3 should be same case. */
58 /* -------------------------------------------------------------------------- **
61 * magic_char - This is the magic char used for mangling. It's
62 * global. There is a call to lp_magicchar() in server.c
63 * that is used to override the initial value.
65 * MANGLE_BASE - This is the number of characters we use for name mangling.
67 * basechars - The set characters used for name mangling. This
68 * is static (scope is this file only).
70 * mangle() - Macro used to select a character from basechars (i.e.,
71 * mangle(n) will return the nth digit, modulo MANGLE_BASE).
73 * chartest - array 0..255. The index range is the set of all possible
74 * values of a byte. For each byte value, the content is a
75 * two nibble pair. See BASECHAR_MASK and ILLEGAL_MASK,
78 * ct_initialized - False until the chartest array has been initialized via
79 * a call to init_chartest().
81 * BASECHAR_MASK - Masks the upper nibble of a one-byte value.
83 * ILLEGAL_MASK - Masks the lower nibble of a one-byte value.
85 * isbasecahr() - Given a character, check the chartest array to see
86 * if that character is in the basechars set. This is
87 * faster than using strchr().
89 * isillegal() - Given a character, check the chartest array to see
90 * if that character is in the illegal characters set.
91 * This is faster than using strchr().
93 * mangled_cache - Cache header used for storing mangled -> original
96 * mc_initialized - False until the mangled_cache structure has been
97 * initialized via a call to reset_mangled_cache().
99 * MANGLED_CACHE_MAX_ENTRIES - Default maximum number of entries for the
100 * cache. A value of 0 indicates "infinite".
102 * MANGLED_CACHE_MAX_MEMORY - Default maximum amount of memory for the
103 * cache. When the cache was kept as an array of 256
104 * byte strings, the default cache size was 50 entries.
105 * This required a fixed 12.5Kbytes of memory. The
106 * mangled stack parameter is no longer used (though
107 * this might change). We're now using a fixed 16Kbyte
108 * maximum cache size. This will probably be much more
112 char magic_char
= '~';
114 static char basechars
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-!@#$%";
115 #define MANGLE_BASE (sizeof(basechars)/sizeof(char)-1)
117 static unsigned char chartest
[256] = { 0 };
118 static BOOL ct_initialized
= False
;
120 #define mangle(V) ((char)(basechars[(V) % MANGLE_BASE]))
121 #define BASECHAR_MASK 0xf0
122 #define ILLEGAL_MASK 0x0f
123 #define isbasechar(C) ( (chartest[ ((C) & 0xff) ]) & BASECHAR_MASK )
124 #define isillegal(C) ( (chartest[ ((C) & 0xff) ]) & ILLEGAL_MASK )
126 static ubi_cacheRoot mangled_cache
[1] = { { { 0, 0, 0, 0 }, 0, 0, 0, 0, 0, 0 } };
127 static BOOL mc_initialized
= False
;
128 #define MANGLED_CACHE_MAX_ENTRIES 1024
129 #define MANGLED_CACHE_MAX_MEMORY 0
132 /* -------------------------------------------------------------------------- **
136 /* ************************************************************************** **
137 * Initialize the static character test array.
143 * Notes: This function changes (loads) the contents of the <chartest>
144 * array. The scope of <chartest> is this file.
146 * ************************************************************************** **
148 static void init_chartest( void )
150 const char *illegalchars
= "*\\/?<>|\":";
151 const unsigned char *s
;
153 memset( (char *)chartest
, '\0', 256 );
155 for( s
= (const unsigned char *)illegalchars
; *s
; s
++ )
156 chartest
[*s
] = ILLEGAL_MASK
;
158 for( s
= (const unsigned char *)basechars
; *s
; s
++ )
159 chartest
[*s
] |= BASECHAR_MASK
;
161 ct_initialized
= True
;
162 } /* init_chartest */
164 /* ************************************************************************** **
165 * Return True if a name is a special msdos reserved name.
167 * Input: fname - String containing the name to be tested.
169 * Output: True, if the name matches one of the list of reserved names.
171 * Notes: This is a static function called by is_8_3(), below.
173 * ************************************************************************** **
175 static BOOL
is_reserved_msdos( const char *fname
)
180 StrnCpy (upperFname
, fname
, 12);
182 /* lpt1.txt and con.txt etc are also illegal */
183 p
= strchr(upperFname
,'.');
187 strupper( upperFname
);
189 switch( upperFname
[0] )
192 if( 0 == strcmp( p
, "UX" ) )
196 if( (0 == strcmp( p
, "LOCK$" ))
197 || (0 == strcmp( p
, "ON" ))
198 || (0 == strcmp( p
, "OM1" ))
199 || (0 == strcmp( p
, "OM2" ))
200 || (0 == strcmp( p
, "OM3" ))
201 || (0 == strcmp( p
, "OM4" ))
206 if( (0 == strcmp( p
, "PT1" ))
207 || (0 == strcmp( p
, "PT2" ))
208 || (0 == strcmp( p
, "PT3" ))
213 if( 0 == strcmp( p
, "UL" ) )
217 if( 0 == strcmp( p
, "RN" ) )
223 } /* is_reserved_msdos */
225 /* ************************************************************************** **
226 * Determine whether or not a given name contains illegal characters, even
229 * Input: name - The name to be tested.
231 * Output: True if an illegal character was found in <name>, else False.
233 * Notes: This is used to test a name on the host system, long or short,
234 * for characters that would be illegal on most client systems,
235 * particularly DOS and Windows systems. Unix and AmigaOS, for
236 * example, allow a filenames which contain such oddities as
237 * quotes ("). If a name is found which does contain an illegal
238 * character, it is mangled even if it conforms to the 8.3
241 * ************************************************************************** **
243 static BOOL
is_illegal_name( char *name
)
252 if( !ct_initialized
)
255 namelen
= strlen(name
);
257 name
[namelen
-1] == '.' &&
258 !strequal(name
, ".") &&
259 !strequal(name
, ".."))
262 s
= (unsigned char *)name
;
265 skip
= get_character_len( *s
);
272 if( isillegal( *s
) )
280 } /* is_illegal_name */
282 /* ************************************************************************** **
283 * Return True if the name *could be* a mangled name.
285 * Input: s - A path name - in UNIX pathname format.
287 * Output: True if the name matches the pattern described below in the
290 * Notes: The input name is *not* tested for 8.3 compliance. This must be
291 * done separately. This function returns true if the name contains
292 * a magic character followed by excactly two characters from the
293 * basechars list (above), which in turn are followed either by the
294 * nul (end of string) byte or a dot (extension) or by a '/' (end of
297 * ************************************************************************** **
299 static BOOL
is_mangled( const char *s
)
304 if( !ct_initialized
)
307 magic
= strchr( s
, magic_char
);
308 while( magic
&& magic
[1] && magic
[2] ) {
309 /* 3 chars, 1st is magic. */
310 if( ('.' == magic
[3] || '/' == magic
[3] || !(magic
[3])) /* Ends with '.' or nul or '/' ? */
311 && isbasechar( toupper(magic
[1]) ) /* is 2nd char basechar? */
312 && isbasechar( toupper(magic
[2]) ) ) /* is 3rd char basechar? */
313 ret
= ( True
); /* If all above, then true, */
314 magic
= strchr( magic
+1, magic_char
); /* else seek next magic. */
317 DEBUG(10,("is_mangled: %s : %s\n", s
, ret
? "True" : "False"));
322 static BOOL
is_ms_wildchar(const char c
)
336 /* ************************************************************************** **
337 * Return True if the name is a valid DOS name in 8.3 DOS format.
339 * Input: fname - File name to be checked.
340 * check_case - If True, and if case_mangle is True, then the
341 * name will be checked to see if all characters
342 * are the correct case. See case_mangle and
343 * case_default above.
344 * allow_wildcards - If True dos wildcard characters *?<>" are allowed as 8.3.
346 * Output: True if the name is a valid DOS name, else False.
348 * ************************************************************************** **
351 static BOOL
is_8_3( const char *cfname
, BOOL check_case
, BOOL allow_wildcards
)
359 const char *fname
= cfname
;
361 slash_pos
= strrchr( fname
, '/' );
363 /* If there is a directory path, skip it. */
365 fname
= slash_pos
+ 1;
366 len
= strlen( fname
);
368 DEBUG( 5, ( "Checking %s for 8.3\n", fname
) );
370 /* Can't be 0 chars or longer than 12 chars */
371 if( (len
== 0) || (len
> 12) )
374 /* Mustn't be an MS-DOS Special file such as lpt1 or even lpt1.txt */
375 if( is_reserved_msdos( fname
) )
378 /* Check that all characters are the correct case, if asked to do so. */
379 if( check_case
&& case_mangle
) {
380 switch( case_default
) {
382 if( strhasupper( fname
) )
386 if( strhaslower( fname
) )
392 /* Can't contain invalid dos chars */
393 /* Windows use the ANSI charset.
394 But filenames are translated in the PC charset.
395 This Translation may be more or less relaxed depending
396 the Windows application. */
398 /* %%% A nice improvment to name mangling would be to translate
399 filename to ANSI charset on the smb server host */
404 if( (skip
= get_character_len( *p
)) != 0 )
407 if( *p
== '.' && !dot_pos
)
410 if( !isdoschar( *p
)) {
411 if (!allow_wildcards
)
413 if (!is_ms_wildchar(*p
))
421 /* no dot and less than 9 means OK */
425 l
= PTR_DIFF( dot_pos
, fname
);
427 /* base must be at least 1 char except special cases . and .. */
429 return( 0 == strcmp( fname
, "." ) || 0 == strcmp( fname
, ".." ) );
431 /* base can't be greater than 8 */
435 /* extension must be between 1 and 3 */
436 if( (len
- l
< 2 ) || (len
- l
> 4) )
439 /* extensions may not have a dot */
440 if( strchr( dot_pos
+1, '.' ) )
443 /* must be in 8.3 format */
447 /* ************************************************************************** **
448 * Compare two cache keys and return a value indicating their ordinal
451 * Input: ItemPtr - Pointer to a comparison key. In this case, this will
452 * be a mangled name string.
453 * NodePtr - Pointer to a node in the cache. The node structure
454 * will be followed in memory by a mangled name string.
456 * Output: A signed integer, as follows:
457 * (x < 0) <==> Key1 less than Key2
458 * (x == 0) <==> Key1 equals Key2
459 * (x > 0) <==> Key1 greater than Key2
461 * Notes: This is a ubiqx-style comparison routine. See ubi_BinTree for
464 * ************************************************************************** **
467 static signed int cache_compare( ubi_btItemPtr ItemPtr
, ubi_btNodePtr NodePtr
)
469 char *Key1
= (char *)ItemPtr
;
470 char *Key2
= (char *)(((ubi_cacheEntryPtr
)NodePtr
) + 1);
472 DEBUG(100,("cache_compare: %s %s\n", Key1
, Key2
));
474 return( StrCaseCmp( Key1
, Key2
) );
477 /* ************************************************************************** **
478 * Free a cache entry.
480 * Input: WarrenZevon - Pointer to the entry that is to be returned to
484 * Notes: This function gets around the possibility that the standard
485 * free() function may be implemented as a macro, or other evil
486 * subversions (oh, so much fun).
488 * ************************************************************************** **
491 static void cache_free_entry( ubi_trNodePtr WarrenZevon
)
493 ZERO_STRUCTP(WarrenZevon
);
494 SAFE_FREE( WarrenZevon
);
497 /* ************************************************************************** **
498 * Initializes or clears the mangled cache.
503 * Notes: There is a section below that is commented out. It shows how
504 * one might use lp_ calls to set the maximum memory and entry size
505 * of the cache. You might also want to remove the constants used
506 * in ubi_cacheInit() and replace them with lp_ calls. If so, then
507 * the calls to ubi_cacheSetMax*() would be moved into the else
508 * clause. Another option would be to pass in the max_entries and
509 * max_memory values as parameters. crh 09-Apr-1998.
511 * ************************************************************************** **
514 static void reset_mangled_cache( void )
516 if( !mc_initialized
) {
517 (void)ubi_cacheInit( mangled_cache
,
520 MANGLED_CACHE_MAX_ENTRIES
,
521 MANGLED_CACHE_MAX_MEMORY
);
522 mc_initialized
= True
;
524 (void)ubi_cacheClear( mangled_cache
);
528 (void)ubi_cacheSetMaxEntries( mangled_cache, lp_mangled_cache_entries() );
529 (void)ubi_cacheSetMaxMemory( mangled_cache, lp_mangled_cache_memory() );
533 /* ************************************************************************** **
534 * Add a mangled name into the cache.
536 * Notes: If the mangled cache has not been initialized, then the
537 * function will simply fail. It could initialize the cache,
538 * but that's not the way it was done before I changed the
539 * cache mechanism, so I'm sticking with the old method.
541 * If the extension of the raw name maps directly to the
542 * extension of the mangled name, then we'll store both names
543 * *without* extensions. That way, we can provide consistent
544 * reverse mangling for all names that match. The test here is
545 * a bit more careful than the one done in earlier versions of
548 * - the extension must exist on the raw name,
549 * - it must be all lower case
550 * - it must match the mangled extension (to prove that no
551 * mangling occurred).
555 * ************************************************************************** **
557 static void cache_mangled_name( char *mangled_name
, char *raw_name
)
559 ubi_cacheEntryPtr new_entry
;
566 /* If the cache isn't initialized, give up. */
567 if( !mc_initialized
)
570 /* Init the string lengths. */
571 mangled_len
= strlen( mangled_name
);
572 raw_len
= strlen( raw_name
);
574 /* See if the extensions are unmangled. If so, store the entry
575 * without the extension, thus creating a "group" reverse map.
577 s1
= strrchr( mangled_name
, '.' );
578 if( s1
&& (s2
= strrchr( raw_name
, '.' )) )
581 while( s1
[i
] && (tolower( s1
[i
] ) == s2
[i
]) )
583 if( !s1
[i
] && !s2
[i
] )
590 /* Allocate a new cache entry. If the allocation fails, just return. */
591 i
= sizeof( ubi_cacheEntry
) + mangled_len
+ raw_len
+ 2;
592 new_entry
= malloc( i
);
596 /* Fill the new cache entry, and add it to the cache. */
597 s1
= (char *)(new_entry
+ 1);
598 s2
= (char *)&(s1
[mangled_len
+ 1]);
599 (void)StrnCpy( s1
, mangled_name
, mangled_len
);
600 (void)StrnCpy( s2
, raw_name
, raw_len
);
601 ubi_cachePut( mangled_cache
, i
, new_entry
, s1
);
604 /* ************************************************************************** **
605 * Check for a name on the mangled name stack
607 * Input: s - Input *and* output string buffer.
609 * Output: True if the name was found in the cache, else False.
611 * Notes: If a reverse map is found, the function will overwrite the string
612 * space indicated by the input pointer <s>. This is frightening.
613 * It should be rewritten to return NULL if the long name was not
614 * found, and a pointer to the long name if it was found.
616 * ************************************************************************** **
619 static BOOL
check_mangled_cache( char *s
, size_t maxlen
)
621 ubi_cacheEntryPtr FoundPtr
;
622 char *ext_start
= NULL
;
624 char *saved_ext
= NULL
;
626 /* If the cache isn't initialized, give up. */
627 if( !mc_initialized
)
630 FoundPtr
= ubi_cacheGet( mangled_cache
, (ubi_trItemPtr
)s
);
632 /* If we didn't find the name *with* the extension, try without. */
635 ext_start
= strrchr( s
, '.' );
638 if((saved_ext
= strdup(ext_start
)) == NULL
)
642 FoundPtr
= ubi_cacheGet( mangled_cache
, (ubi_trItemPtr
)s
);
644 * At this point s is the name without the
645 * extension. We re-add the extension if saved_ext
646 * is not null, before freeing saved_ext.
651 /* Okay, if we haven't found it we're done. */
656 /* Replace the saved_ext as it was truncated. */
657 (void)safe_strcat( s
, saved_ext
, maxlen
);
658 SAFE_FREE(saved_ext
);
663 /* If we *did* find it, we need to copy it into the string buffer. */
664 found_name
= (char *)(FoundPtr
+ 1);
665 found_name
+= (strlen( found_name
) + 1);
667 DEBUG( 3, ("Found %s on mangled stack ", s
) );
669 (void)safe_strcpy( s
, found_name
, maxlen
);
672 /* Replace the saved_ext as it was truncated. */
673 (void)safe_strcat( s
, saved_ext
,maxlen
);
674 SAFE_FREE(saved_ext
);
677 DEBUG( 3, ("as %s\n", s
) );
682 /*****************************************************************************
683 * do the actual mangling to 8.3 format
684 * the buffer must be able to hold 13 characters (including the null)
685 *****************************************************************************
687 static void mangle_name_83( char *s
)
701 if( p
&& (strlen(p
+1) < (size_t)4) )
703 BOOL all_normal
= ( strisnormal(p
+1) ); /* XXXXXXXXX */
705 if( all_normal
&& p
[1] != 0 )
708 csum
= str_checksum( s
);
712 csum
= str_checksum(s
);
715 csum
= str_checksum(s
);
719 DEBUG( 5, ("Mangling name %s to ",s
) );
724 safe_strcpy( extension
, "___", 3 );
728 while( *p
&& extlen
< 3 )
730 skip
= get_character_len( *p
);
736 extension
[extlen
++] = p
[0];
737 extension
[extlen
++] = p
[1];
741 extension
[extlen
++] = mangle( (unsigned char)*p
);
746 extension
[extlen
++] = p
[0];
750 if( isdoschar (*p
) && *p
!= '.' )
751 extension
[extlen
++] = p
[0];
756 extension
[extlen
] = 0;
762 while( *p
&& baselen
< 5 )
764 skip
= get_character_len(*p
);
770 base
[baselen
++] = p
[0];
771 base
[baselen
++] = p
[1];
775 base
[baselen
++] = mangle( (unsigned char)*p
);
780 base
[baselen
++] = p
[0];
784 if( isdoschar( *p
) && *p
!= '.' )
785 base
[baselen
++] = p
[0];
792 csum
= csum
% (MANGLE_BASE
*MANGLE_BASE
);
794 (void)slprintf(s
, 12, "%s%c%c%c",
795 base
, magic_char
, mangle( csum
/MANGLE_BASE
), mangle( csum
) );
799 (void)pstrcat( s
, "." );
800 (void)pstrcat( s
, extension
);
803 DEBUG( 5, ( "%s\n", s
) );
807 /*****************************************************************************
808 * Convert a filename to DOS format. Return True if successful.
810 * Input: OutName - Source *and* destination buffer.
812 * NOTE that OutName must point to a memory space that
813 * is at least 13 bytes in size!
815 * need83 - If False, name mangling will be skipped unless the
816 * name contains illegal characters. Mapping will still
817 * be done, if appropriate. This is probably used to
818 * signal that a client does not require name mangling,
819 * thus skipping the name mangling even on shares which
820 * have name-mangling turned on.
821 * cache83 - If False, the mangled name cache will not be updated.
822 * This is usually used to prevent that we overwrite
823 * a conflicting cache entry prematurely, i.e. before
824 * we know whether the client is really interested in the
825 * current name. (See PR#13758). UKD.
826 * snum - Share number. This identifies the share in which the
829 * Output: Returns False only if the name wanted mangling but the share does
830 * not have name mangling turned on.
832 * ****************************************************************************
834 static void name_map_mangle(char *OutName
, BOOL need83
, BOOL cache83
)
836 DEBUG(5,("name_map_mangle( %s, need83 = %s, cache83 = %s )\n", OutName
,
837 need83
? "True" : "False", cache83
? "True" : "False" ));
839 #ifdef MANGLE_LONG_FILENAMES
840 if( !need83
&& is_illegal_name(OutName
) )
844 /* check if it's already in 8.3 format */
845 if (need83
&& !is_8_3(OutName
, True
, False
)) {
848 /* mangle it into 8.3 */
850 tmp
= strdup(OutName
);
852 mangle_name_83(OutName
);
855 cache_mangled_name(OutName
, tmp
);
860 DEBUG(5,("name_map_mangle() ==> [%s]\n", OutName
));
864 * the following provides the abstraction layer to make it easier
865 * to drop in an alternative mangling implementation
867 static struct mangle_fns mangle_fns
= {
875 /* return the methods for this mangling implementation */
876 struct mangle_fns
*mangle_hash_init(void)
878 reset_mangled_cache();