2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-2002
5 Copyright (C) Simo Sorce 2001
6 Copyright (C) Andrew Bartlett 2002
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 /* -------------------------------------------------------------------------- **
27 * March/April 1998 CRH
28 * - Many of the functions in this module overwrite string buffers passed to
29 * them. This causes a variety of problems and is, generally speaking,
30 * dangerous and scarry. See the kludge notes in name_map()
32 * - It seems that something is calling name_map() twice. The
33 * first call is probably some sort of test. Names which contain
34 * illegal characters are being doubly mangled. I'm not sure, but
35 * I'm guessing the problem is in server.c.
37 * -------------------------------------------------------------------------- **
40 /* -------------------------------------------------------------------------- **
43 * March/April 1998 CRH
44 * Updated a bit. Rewrote is_mangled() to be a bit more selective.
45 * Rewrote the mangled name cache. Added comments here and there.
47 * -------------------------------------------------------------------------- **
53 /* -------------------------------------------------------------------------- **
54 * External Variables...
57 extern int case_default
; /* Are conforming 8.3 names all upper or lower? */
58 extern BOOL case_mangle
; /* If true, all chars in 8.3 should be same case. */
60 /* -------------------------------------------------------------------------- **
63 * magic_char - This is the magic char used for mangling. It's
64 * global. There is a call to lp_magicchar() in server.c
65 * that is used to override the initial value.
67 * MANGLE_BASE - This is the number of characters we use for name mangling.
69 * basechars - The set characters used for name mangling. This
70 * is static (scope is this file only).
72 * mangle() - Macro used to select a character from basechars (i.e.,
73 * mangle(n) will return the nth digit, modulo MANGLE_BASE).
75 * chartest - array 0..255. The index range is the set of all possible
76 * values of a byte. For each byte value, the content is a
77 * two nibble pair. See BASECHAR_MASK and ILLEGAL_MASK,
80 * ct_initialized - False until the chartest array has been initialized via
81 * a call to init_chartest().
83 * BASECHAR_MASK - Masks the upper nibble of a one-byte value.
85 * ILLEGAL_MASK - Masks the lower nibble of a one-byte value.
87 * isbasecahr() - Given a character, check the chartest array to see
88 * if that character is in the basechars set. This is
89 * faster than using strchr_m().
91 * isillegal() - Given a character, check the chartest array to see
92 * if that character is in the illegal characters set.
93 * This is faster than using strchr_m().
95 * mangled_cache - Cache header used for storing mangled -> original
98 * mc_initialized - False until the mangled_cache structure has been
99 * initialized via a call to reset_mangled_cache().
101 * MANGLED_CACHE_MAX_ENTRIES - Default maximum number of entries for the
102 * cache. A value of 0 indicates "infinite".
104 * MANGLED_CACHE_MAX_MEMORY - Default maximum amount of memory for the
105 * cache. When the cache was kept as an array of 256
106 * byte strings, the default cache size was 50 entries.
107 * This required a fixed 12.5Kbytes of memory. The
108 * mangled stack parameter is no longer used (though
109 * this might change). We're now using a fixed 16Kbyte
110 * maximum cache size. This will probably be much more
114 char magic_char
= '~';
116 static char basechars
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-!@#$%";
117 #define MANGLE_BASE (sizeof(basechars)/sizeof(char)-1)
119 static unsigned char chartest
[256] = { 0 };
120 static BOOL ct_initialized
= False
;
122 #define mangle(V) ((char)(basechars[(V) % MANGLE_BASE]))
123 #define BASECHAR_MASK 0xf0
124 #define ILLEGAL_MASK 0x0f
125 #define isbasechar(C) ( (chartest[ ((C) & 0xff) ]) & BASECHAR_MASK )
126 #define isillegal(C) ( (chartest[ ((C) & 0xff) ]) & ILLEGAL_MASK )
128 static ubi_cacheRoot mangled_cache
[1] = { { { 0, 0, 0, 0 }, 0, 0, 0, 0, 0, 0 } };
129 static BOOL mc_initialized
= False
;
130 #define MANGLED_CACHE_MAX_ENTRIES 1024
131 #define MANGLED_CACHE_MAX_MEMORY 0
133 /* -------------------------------------------------------------------------- **
134 * External Variables...
137 extern int case_default
; /* Are conforming 8.3 names all upper or lower? */
138 extern BOOL case_mangle
; /* If true, all chars in 8.3 should be same case. */
140 /* -------------------------------------------------------------------- */
142 static NTSTATUS
has_valid_83_chars(const smb_ucs2_t
*s
, BOOL allow_wildcards
)
145 return NT_STATUS_INVALID_PARAMETER
;
147 /* CHECK: this should not be necessary if the ms wild chars
148 are not valid in valid.dat --- simo */
149 if (!allow_wildcards
&& ms_has_wild_w(s
))
150 return NT_STATUS_UNSUCCESSFUL
;
154 return NT_STATUS_UNSUCCESSFUL
;
161 /* return False if something fail and
162 * return 2 alloced unicode strings that contain prefix and extension
165 static NTSTATUS
mangle_get_prefix(const smb_ucs2_t
*ucs2_string
, smb_ucs2_t
**prefix
,
166 smb_ucs2_t
**extension
, BOOL allow_wildcards
)
172 *prefix
= strdup_w(ucs2_string
);
174 return NT_STATUS_NO_MEMORY
;
176 if ((p
= strrchr_w(*prefix
, UCS2_CHAR('.')))) {
177 ext_len
= strlen_w(p
+1);
178 if ((ext_len
> 0) && (ext_len
< 4) && (p
!= *prefix
) &&
179 (NT_STATUS_IS_OK(has_valid_83_chars(p
+1,allow_wildcards
)))) /* check extension */ {
181 *extension
= strdup_w(p
+1);
184 return NT_STATUS_NO_MEMORY
;
191 /* ************************************************************************** **
192 * Return NT_STATUS_UNSUCCESSFUL if a name is a special msdos reserved name.
194 * Input: fname - String containing the name to be tested.
196 * Output: NT_STATUS_UNSUCCESSFUL, if the name matches one of the list of reserved names.
198 * Notes: This is a static function called by is_8_3(), below.
200 * ************************************************************************** **
203 static NTSTATUS
is_valid_name(const smb_ucs2_t
*fname
, BOOL allow_wildcards
, BOOL only_8_3
)
206 NTSTATUS ret
= NT_STATUS_OK
;
208 if (!fname
|| !*fname
)
209 return NT_STATUS_INVALID_PARAMETER
;
211 /* . and .. are valid names. */
212 if (strcmp_wa(fname
, ".")==0 || strcmp_wa(fname
, "..")==0)
215 /* Name cannot start with '.' */
216 if (*fname
== UCS2_CHAR('.'))
217 return NT_STATUS_UNSUCCESSFUL
;
220 ret
= has_valid_83_chars(fname
, allow_wildcards
);
221 if (!NT_STATUS_IS_OK(ret
))
225 str
= strdup_w(fname
);
226 p
= strchr_w(str
, UCS2_CHAR('.'));
227 if (p
&& p
[1] == UCS2_CHAR(0)) {
228 /* Name cannot end in '.' */
230 return NT_STATUS_UNSUCCESSFUL
;
240 if(strcmp_wa(p
, "UX") == 0)
241 ret
= NT_STATUS_UNSUCCESSFUL
;
244 if((strcmp_wa(p
, "LOCK$") == 0)
245 || (strcmp_wa(p
, "ON") == 0)
246 || (strcmp_wa(p
, "OM1") == 0)
247 || (strcmp_wa(p
, "OM2") == 0)
248 || (strcmp_wa(p
, "OM3") == 0)
249 || (strcmp_wa(p
, "OM4") == 0)
251 ret
= NT_STATUS_UNSUCCESSFUL
;
254 if((strcmp_wa(p
, "PT1") == 0)
255 || (strcmp_wa(p
, "PT2") == 0)
256 || (strcmp_wa(p
, "PT3") == 0)
258 ret
= NT_STATUS_UNSUCCESSFUL
;
261 if(strcmp_wa(p
, "UL") == 0)
262 ret
= NT_STATUS_UNSUCCESSFUL
;
265 if(strcmp_wa(p
, "RN") == 0)
266 ret
= NT_STATUS_UNSUCCESSFUL
;
276 static NTSTATUS
is_8_3_w(const smb_ucs2_t
*fname
, BOOL allow_wildcards
)
278 smb_ucs2_t
*pref
= 0, *ext
= 0;
280 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
282 if (!fname
|| !*fname
)
283 return NT_STATUS_INVALID_PARAMETER
;
285 if (strlen_w(fname
) > 12)
286 return NT_STATUS_UNSUCCESSFUL
;
288 if (strcmp_wa(fname
, ".") == 0 || strcmp_wa(fname
, "..") == 0)
291 if (!NT_STATUS_IS_OK(is_valid_name(fname
, allow_wildcards
, True
)))
294 if (!NT_STATUS_IS_OK(mangle_get_prefix(fname
, &pref
, &ext
, allow_wildcards
)))
296 plen
= strlen_w(pref
);
298 if (strchr_wa(pref
, '.'))
300 if (plen
< 1 || plen
> 8)
302 if (ext
&& (strlen_w(ext
) > 3))
313 static BOOL
is_8_3(const char *fname
, BOOL check_case
, BOOL allow_wildcards
)
316 smb_ucs2_t
*ucs2name
;
317 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
320 if (!fname
|| !*fname
)
322 if ((f
= strrchr(fname
, '/')) == NULL
)
330 size
= push_ucs2_allocate(&ucs2name
, f
);
331 if (size
== (size_t)-1) {
332 DEBUG(0,("is_8_3: internal error push_ucs2_allocate() failed!\n"));
336 ret
= is_8_3_w(ucs2name
, allow_wildcards
);
341 if (!NT_STATUS_IS_OK(ret
)) {
350 /* -------------------------------------------------------------------------- **
354 /* ************************************************************************** **
355 * Initialize the static character test array.
361 * Notes: This function changes (loads) the contents of the <chartest>
362 * array. The scope of <chartest> is this file.
364 * ************************************************************************** **
366 static void init_chartest( void )
368 const char *illegalchars
= "*\\/?<>|\":";
369 const unsigned char *s
;
371 memset( (char *)chartest
, '\0', 256 );
373 for( s
= (const unsigned char *)illegalchars
; *s
; s
++ )
374 chartest
[*s
] = ILLEGAL_MASK
;
376 for( s
= (const unsigned char *)basechars
; *s
; s
++ )
377 chartest
[*s
] |= BASECHAR_MASK
;
379 ct_initialized
= True
;
382 /* ************************************************************************** **
383 * Return True if the name *could be* a mangled name.
385 * Input: s - A path name - in UNIX pathname format.
387 * Output: True if the name matches the pattern described below in the
390 * Notes: The input name is *not* tested for 8.3 compliance. This must be
391 * done separately. This function returns true if the name contains
392 * a magic character followed by excactly two characters from the
393 * basechars list (above), which in turn are followed either by the
394 * nul (end of string) byte or a dot (extension) or by a '/' (end of
397 * ************************************************************************** **
399 static BOOL
is_mangled(const char *s
)
403 if( !ct_initialized
)
406 magic
= strchr_m( s
, magic_char
);
407 while( magic
&& magic
[1] && magic
[2] ) { /* 3 chars, 1st is magic. */
408 if( ('.' == magic
[3] || '/' == magic
[3] || !(magic
[3])) /* Ends with '.' or nul or '/' ? */
409 && isbasechar( toupper(magic
[1]) ) /* is 2nd char basechar? */
410 && isbasechar( toupper(magic
[2]) ) ) /* is 3rd char basechar? */
411 return( True
); /* If all above, then true, */
412 magic
= strchr_m( magic
+1, magic_char
); /* else seek next magic. */
417 /* ************************************************************************** **
418 * Compare two cache keys and return a value indicating their ordinal
421 * Input: ItemPtr - Pointer to a comparison key. In this case, this will
422 * be a mangled name string.
423 * NodePtr - Pointer to a node in the cache. The node structure
424 * will be followed in memory by a mangled name string.
426 * Output: A signed integer, as follows:
427 * (x < 0) <==> Key1 less than Key2
428 * (x == 0) <==> Key1 equals Key2
429 * (x > 0) <==> Key1 greater than Key2
431 * Notes: This is a ubiqx-style comparison routine. See ubi_BinTree for
434 * ************************************************************************** **
436 static signed int cache_compare( ubi_btItemPtr ItemPtr
, ubi_btNodePtr NodePtr
)
438 char *Key1
= (char *)ItemPtr
;
439 char *Key2
= (char *)(((ubi_cacheEntryPtr
)NodePtr
) + 1);
441 return( StrCaseCmp( Key1
, Key2
) );
444 /* ************************************************************************** **
445 * Free a cache entry.
447 * Input: WarrenZevon - Pointer to the entry that is to be returned to
451 * Notes: This function gets around the possibility that the standard
452 * free() function may be implemented as a macro, or other evil
453 * subversions (oh, so much fun).
455 * ************************************************************************** **
457 static void cache_free_entry( ubi_trNodePtr WarrenZevon
)
459 ZERO_STRUCTP(WarrenZevon
);
460 SAFE_FREE( WarrenZevon
);
463 /* ************************************************************************** **
464 * Initializes or clears the mangled cache.
469 * Notes: There is a section below that is commented out. It shows how
470 * one might use lp_ calls to set the maximum memory and entry size
471 * of the cache. You might also want to remove the constants used
472 * in ubi_cacheInit() and replace them with lp_ calls. If so, then
473 * the calls to ubi_cacheSetMax*() would be moved into the else
474 * clause. Another option would be to pass in the max_entries and
475 * max_memory values as parameters. crh 09-Apr-1998.
477 * ************************************************************************** **
480 static void mangle_reset( void )
482 if( !mc_initialized
) {
483 (void)ubi_cacheInit( mangled_cache
,
486 MANGLED_CACHE_MAX_ENTRIES
,
487 MANGLED_CACHE_MAX_MEMORY
);
488 mc_initialized
= True
;
490 (void)ubi_cacheClear( mangled_cache
);
494 (void)ubi_cacheSetMaxEntries( mangled_cache, lp_mangled_cache_entries() );
495 (void)ubi_cacheSetMaxMemory( mangled_cache, lp_mangled_cache_memory() );
499 /* ************************************************************************** **
500 * Add a mangled name into the cache.
502 * Notes: If the mangled cache has not been initialized, then the
503 * function will simply fail. It could initialize the cache,
504 * but that's not the way it was done before I changed the
505 * cache mechanism, so I'm sticking with the old method.
507 * If the extension of the raw name maps directly to the
508 * extension of the mangled name, then we'll store both names
509 * *without* extensions. That way, we can provide consistent
510 * reverse mangling for all names that match. The test here is
511 * a bit more careful than the one done in earlier versions of
514 * - the extension must exist on the raw name,
515 * - it must be all lower case
516 * - it must match the mangled extension (to prove that no
517 * mangling occurred).
521 * ************************************************************************** **
523 static void cache_mangled_name( char *mangled_name
, char *raw_name
)
525 ubi_cacheEntryPtr new_entry
;
532 /* If the cache isn't initialized, give up. */
533 if( !mc_initialized
)
536 /* Init the string lengths. */
537 mangled_len
= strlen( mangled_name
);
538 raw_len
= strlen( raw_name
);
540 /* See if the extensions are unmangled. If so, store the entry
541 * without the extension, thus creating a "group" reverse map.
543 s1
= strrchr( mangled_name
, '.' );
544 if( s1
&& (s2
= strrchr( raw_name
, '.' )) ) {
546 while( s1
[i
] && (tolower( s1
[i
] ) == s2
[i
]) )
548 if( !s1
[i
] && !s2
[i
] ) {
554 /* Allocate a new cache entry. If the allocation fails, just return. */
555 i
= sizeof( ubi_cacheEntry
) + mangled_len
+ raw_len
+ 2;
556 new_entry
= malloc( i
);
560 /* Fill the new cache entry, and add it to the cache. */
561 s1
= (char *)(new_entry
+ 1);
562 s2
= (char *)&(s1
[mangled_len
+ 1]);
563 safe_strcpy( s1
, mangled_name
, mangled_len
);
564 safe_strcpy( s2
, raw_name
, raw_len
);
565 ubi_cachePut( mangled_cache
, i
, new_entry
, s1
);
568 /* ************************************************************************** **
569 * Check for a name on the mangled name stack
571 * Input: s - Input *and* output string buffer.
573 * Output: True if the name was found in the cache, else False.
575 * Notes: If a reverse map is found, the function will overwrite the string
576 * space indicated by the input pointer <s>. This is frightening.
577 * It should be rewritten to return NULL if the long name was not
578 * found, and a pointer to the long name if it was found.
580 * ************************************************************************** **
583 static BOOL
check_cache( char *s
)
585 ubi_cacheEntryPtr FoundPtr
;
586 char *ext_start
= NULL
;
588 char *saved_ext
= NULL
;
590 /* If the cache isn't initialized, give up. */
591 if( !mc_initialized
)
594 FoundPtr
= ubi_cacheGet( mangled_cache
, (ubi_trItemPtr
)s
);
596 /* If we didn't find the name *with* the extension, try without. */
598 ext_start
= strrchr( s
, '.' );
600 if((saved_ext
= strdup(ext_start
)) == NULL
)
604 FoundPtr
= ubi_cacheGet( mangled_cache
, (ubi_trItemPtr
)s
);
606 * At this point s is the name without the
607 * extension. We re-add the extension if saved_ext
608 * is not null, before freeing saved_ext.
613 /* Okay, if we haven't found it we're done. */
616 /* Replace the saved_ext as it was truncated. */
617 (void)pstrcat( s
, saved_ext
);
618 SAFE_FREE(saved_ext
);
623 /* If we *did* find it, we need to copy it into the string buffer. */
624 found_name
= (char *)(FoundPtr
+ 1);
625 found_name
+= (strlen( found_name
) + 1);
627 (void)pstrcpy( s
, found_name
);
629 /* Replace the saved_ext as it was truncated. */
630 (void)pstrcat( s
, saved_ext
);
631 SAFE_FREE(saved_ext
);
637 /*****************************************************************************
638 * do the actual mangling to 8.3 format
639 * the buffer must be able to hold 13 characters (including the null)
640 *****************************************************************************
642 static void to_8_3(char *s
)
655 if( p
&& (strlen(p
+1) < (size_t)4) ) {
656 BOOL all_normal
= ( strisnormal(p
+1) ); /* XXXXXXXXX */
658 if( all_normal
&& p
[1] != 0 ) {
660 csum
= str_checksum( s
);
663 csum
= str_checksum(s
);
665 csum
= str_checksum(s
);
671 safe_strcpy( extension
, "___", 3 );
674 while( *p
&& extlen
< 3 ) {
676 extension
[extlen
++] = p
[0];
680 extension
[extlen
] = 0;
686 while( *p
&& baselen
< 5 ) {
688 base
[baselen
++] = p
[0];
694 csum
= csum
% (MANGLE_BASE
*MANGLE_BASE
);
696 (void)slprintf(s
, 12, "%s%c%c%c",
697 base
, magic_char
, mangle( csum
/MANGLE_BASE
), mangle( csum
) );
700 (void)pstrcat( s
, "." );
701 (void)pstrcat( s
, extension
);
705 /*****************************************************************************
706 * Convert a filename to DOS format. Return True if successful.
708 * Input: OutName - Source *and* destination buffer.
710 * NOTE that OutName must point to a memory space that
711 * is at least 13 bytes in size!
713 * need83 - If False, name mangling will be skipped unless the
714 * name contains illegal characters. Mapping will still
715 * be done, if appropriate. This is probably used to
716 * signal that a client does not require name mangling,
717 * thus skipping the name mangling even on shares which
718 * have name-mangling turned on.
719 * cache83 - If False, the mangled name cache will not be updated.
720 * This is usually used to prevent that we overwrite
721 * a conflicting cache entry prematurely, i.e. before
722 * we know whether the client is really interested in the
723 * current name. (See PR#13758). UKD.
725 * Output: Returns False only if the name wanted mangling but the share does
726 * not have name mangling turned on.
728 * ****************************************************************************
731 static void name_map(char *OutName
, BOOL need83
, BOOL cache83
)
733 smb_ucs2_t
*OutName_ucs2
;
734 DEBUG(5,("name_map( %s, need83 = %s, cache83 = %s)\n", OutName
,
735 need83
? "True" : "False", cache83
? "True" : "False"));
737 if (push_ucs2_allocate(&OutName_ucs2
, OutName
) == (size_t)-1) {
738 DEBUG(0, ("push_ucs2_allocate failed!\n"));
742 if( !need83
&& !NT_STATUS_IS_OK(is_valid_name(OutName_ucs2
, False
, False
)))
745 /* check if it's already in 8.3 format */
746 if (need83
&& !NT_STATUS_IS_OK(is_8_3_w(OutName_ucs2
, False
))) {
749 /* mangle it into 8.3 */
751 tmp
= strdup(OutName
);
756 cache_mangled_name(OutName
, tmp
);
761 DEBUG(5,("name_map() ==> [%s]\n", OutName
));
762 SAFE_FREE(OutName_ucs2
);
766 the following provides the abstraction layer to make it easier
767 to drop in an alternative mangling implementation
769 static struct mangle_fns mangle_fns
= {
777 /* return the methods for this mangling implementation */
778 struct mangle_fns
*mangle_hash_init(void)