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 /* -------------------------------------------------------------------------- **
56 * magic_char - This is the magic char used for mangling. It's
57 * global. There is a call to lp_magicchar() in server.c
58 * that is used to override the initial value.
60 * MANGLE_BASE - This is the number of characters we use for name mangling.
62 * basechars - The set characters used for name mangling. This
63 * is static (scope is this file only).
65 * mangle() - Macro used to select a character from basechars (i.e.,
66 * mangle(n) will return the nth digit, modulo MANGLE_BASE).
68 * chartest - array 0..255. The index range is the set of all possible
69 * values of a byte. For each byte value, the content is a
70 * two nibble pair. See BASECHAR_MASK and ILLEGAL_MASK,
73 * ct_initialized - False until the chartest array has been initialized via
74 * a call to init_chartest().
76 * BASECHAR_MASK - Masks the upper nibble of a one-byte value.
78 * ILLEGAL_MASK - Masks the lower nibble of a one-byte value.
80 * isbasecahr() - Given a character, check the chartest array to see
81 * if that character is in the basechars set. This is
82 * faster than using strchr_m().
84 * isillegal() - Given a character, check the chartest array to see
85 * if that character is in the illegal characters set.
86 * This is faster than using strchr_m().
90 char magic_char
= '~';
92 static char basechars
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-!@#$%";
93 #define MANGLE_BASE (sizeof(basechars)/sizeof(char)-1)
95 static unsigned char chartest
[256] = { 0 };
96 static BOOL ct_initialized
= False
;
98 #define mangle(V) ((char)(basechars[(V) % MANGLE_BASE]))
99 #define BASECHAR_MASK 0xf0
100 #define ILLEGAL_MASK 0x0f
101 #define isbasechar(C) ( (chartest[ ((C) & 0xff) ]) & BASECHAR_MASK )
102 #define isillegal(C) ( (chartest[ ((C) & 0xff) ]) & ILLEGAL_MASK )
104 static TDB_CONTEXT
*tdb_mangled_cache
;
106 /* -------------------------------------------------------------------- */
108 static NTSTATUS
has_valid_83_chars(const smb_ucs2_t
*s
, BOOL allow_wildcards
)
111 return NT_STATUS_INVALID_PARAMETER
;
113 /* CHECK: this should not be necessary if the ms wild chars
114 are not valid in valid.dat --- simo */
115 if (!allow_wildcards
&& ms_has_wild_w(s
))
116 return NT_STATUS_UNSUCCESSFUL
;
120 return NT_STATUS_UNSUCCESSFUL
;
127 /* return False if something fail and
128 * return 2 alloced unicode strings that contain prefix and extension
131 static NTSTATUS
mangle_get_prefix(const smb_ucs2_t
*ucs2_string
, smb_ucs2_t
**prefix
,
132 smb_ucs2_t
**extension
, BOOL allow_wildcards
)
138 *prefix
= strdup_w(ucs2_string
);
140 return NT_STATUS_NO_MEMORY
;
142 if ((p
= strrchr_w(*prefix
, UCS2_CHAR('.')))) {
143 ext_len
= strlen_w(p
+1);
144 if ((ext_len
> 0) && (ext_len
< 4) && (p
!= *prefix
) &&
145 (NT_STATUS_IS_OK(has_valid_83_chars(p
+1,allow_wildcards
)))) /* check extension */ {
147 *extension
= strdup_w(p
+1);
150 return NT_STATUS_NO_MEMORY
;
157 /* ************************************************************************** **
158 * Return NT_STATUS_UNSUCCESSFUL if a name is a special msdos reserved name.
160 * Input: fname - String containing the name to be tested.
162 * Output: NT_STATUS_UNSUCCESSFUL, if the name matches one of the list of reserved names.
164 * Notes: This is a static function called by is_8_3(), below.
166 * ************************************************************************** **
169 static NTSTATUS
is_valid_name(const smb_ucs2_t
*fname
, BOOL allow_wildcards
, BOOL only_8_3
)
172 NTSTATUS ret
= NT_STATUS_OK
;
174 if (!fname
|| !*fname
)
175 return NT_STATUS_INVALID_PARAMETER
;
177 /* . and .. are valid names. */
178 if (strcmp_wa(fname
, ".")==0 || strcmp_wa(fname
, "..")==0)
181 /* Name cannot start with '.' */
182 if (*fname
== UCS2_CHAR('.'))
183 return NT_STATUS_UNSUCCESSFUL
;
186 ret
= has_valid_83_chars(fname
, allow_wildcards
);
187 if (!NT_STATUS_IS_OK(ret
))
191 str
= strdup_w(fname
);
192 p
= strchr_w(str
, UCS2_CHAR('.'));
193 if (p
&& p
[1] == UCS2_CHAR(0)) {
194 /* Name cannot end in '.' */
196 return NT_STATUS_UNSUCCESSFUL
;
206 if(strcmp_wa(p
, "UX") == 0)
207 ret
= NT_STATUS_UNSUCCESSFUL
;
210 if((strcmp_wa(p
, "LOCK$") == 0)
211 || (strcmp_wa(p
, "ON") == 0)
212 || (strcmp_wa(p
, "OM1") == 0)
213 || (strcmp_wa(p
, "OM2") == 0)
214 || (strcmp_wa(p
, "OM3") == 0)
215 || (strcmp_wa(p
, "OM4") == 0)
217 ret
= NT_STATUS_UNSUCCESSFUL
;
220 if((strcmp_wa(p
, "PT1") == 0)
221 || (strcmp_wa(p
, "PT2") == 0)
222 || (strcmp_wa(p
, "PT3") == 0)
224 ret
= NT_STATUS_UNSUCCESSFUL
;
227 if(strcmp_wa(p
, "UL") == 0)
228 ret
= NT_STATUS_UNSUCCESSFUL
;
231 if(strcmp_wa(p
, "RN") == 0)
232 ret
= NT_STATUS_UNSUCCESSFUL
;
242 static NTSTATUS
is_8_3_w(const smb_ucs2_t
*fname
, BOOL allow_wildcards
)
244 smb_ucs2_t
*pref
= 0, *ext
= 0;
246 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
248 if (!fname
|| !*fname
)
249 return NT_STATUS_INVALID_PARAMETER
;
251 if (strlen_w(fname
) > 12)
252 return NT_STATUS_UNSUCCESSFUL
;
254 if (strcmp_wa(fname
, ".") == 0 || strcmp_wa(fname
, "..") == 0)
257 if (!NT_STATUS_IS_OK(is_valid_name(fname
, allow_wildcards
, True
)))
260 if (!NT_STATUS_IS_OK(mangle_get_prefix(fname
, &pref
, &ext
, allow_wildcards
)))
262 plen
= strlen_w(pref
);
264 if (strchr_wa(pref
, '.'))
266 if (plen
< 1 || plen
> 8)
268 if (ext
&& (strlen_w(ext
) > 3))
279 static BOOL
is_8_3(const char *fname
, BOOL check_case
, BOOL allow_wildcards
)
282 smb_ucs2_t
*ucs2name
;
283 NTSTATUS ret
= NT_STATUS_UNSUCCESSFUL
;
286 if (!fname
|| !*fname
)
288 if ((f
= strrchr(fname
, '/')) == NULL
)
296 size
= push_ucs2_allocate(&ucs2name
, f
);
297 if (size
== (size_t)-1) {
298 DEBUG(0,("is_8_3: internal error push_ucs2_allocate() failed!\n"));
302 ret
= is_8_3_w(ucs2name
, allow_wildcards
);
307 if (!NT_STATUS_IS_OK(ret
)) {
316 /* -------------------------------------------------------------------------- **
320 /* ************************************************************************** **
321 * Initialize the static character test array.
327 * Notes: This function changes (loads) the contents of the <chartest>
328 * array. The scope of <chartest> is this file.
330 * ************************************************************************** **
332 static void init_chartest( void )
334 const char *illegalchars
= "*\\/?<>|\":";
335 const unsigned char *s
;
337 memset( (char *)chartest
, '\0', 256 );
339 for( s
= (const unsigned char *)illegalchars
; *s
; s
++ )
340 chartest
[*s
] = ILLEGAL_MASK
;
342 for( s
= (const unsigned char *)basechars
; *s
; s
++ )
343 chartest
[*s
] |= BASECHAR_MASK
;
345 ct_initialized
= True
;
348 /* ************************************************************************** **
349 * Return True if the name *could be* a mangled name.
351 * Input: s - A path name - in UNIX pathname format.
353 * Output: True if the name matches the pattern described below in the
356 * Notes: The input name is *not* tested for 8.3 compliance. This must be
357 * done separately. This function returns true if the name contains
358 * a magic character followed by excactly two characters from the
359 * basechars list (above), which in turn are followed either by the
360 * nul (end of string) byte or a dot (extension) or by a '/' (end of
363 * ************************************************************************** **
365 static BOOL
is_mangled(const char *s
)
369 if( !ct_initialized
)
372 magic
= strchr_m( s
, magic_char
);
373 while( magic
&& magic
[1] && magic
[2] ) { /* 3 chars, 1st is magic. */
374 if( ('.' == magic
[3] || '/' == magic
[3] || !(magic
[3])) /* Ends with '.' or nul or '/' ? */
375 && isbasechar( toupper(magic
[1]) ) /* is 2nd char basechar? */
376 && isbasechar( toupper(magic
[2]) ) ) /* is 3rd char basechar? */
377 return( True
); /* If all above, then true, */
378 magic
= strchr_m( magic
+1, magic_char
); /* else seek next magic. */
383 /***************************************************************************
384 Initializes or clears the mangled cache.
385 ***************************************************************************/
387 static void mangle_reset( void )
389 /* We could close and re-open the tdb here... should we ? The old code did
390 the equivalent... JRA. */
393 /***************************************************************************
394 Add a mangled name into the cache.
395 If the extension of the raw name maps directly to the
396 extension of the mangled name, then we'll store both names
397 *without* extensions. That way, we can provide consistent
398 reverse mangling for all names that match. The test here is
399 a bit more careful than the one done in earlier versions of
402 - the extension must exist on the raw name,
403 - it must be all lower case
404 - it must match the mangled extension (to prove that no
407 **************************************************************************/
409 static void cache_mangled_name( const char mangled_name
[13], char *raw_name
)
412 char mangled_name_key
[13];
416 /* If the cache isn't initialized, give up. */
417 if( !tdb_mangled_cache
)
420 /* Init the string lengths. */
421 safe_strcpy(mangled_name_key
, mangled_name
, sizeof(mangled_name_key
)-1);
423 /* See if the extensions are unmangled. If so, store the entry
424 * without the extension, thus creating a "group" reverse map.
426 s1
= strrchr( mangled_name_key
, '.' );
427 if( s1
&& (s2
= strrchr( raw_name
, '.' )) ) {
429 while( s1
[i
] && (tolower( s1
[i
] ) == s2
[i
]) )
431 if( !s1
[i
] && !s2
[i
] ) {
432 /* Truncate at the '.' */
438 /* Allocate a new cache entry. If the allocation fails, just return. */
439 data_val
.dptr
= raw_name
;
440 data_val
.dsize
= strlen(raw_name
)+1;
441 if (tdb_store_bystring(tdb_mangled_cache
, mangled_name_key
, data_val
, TDB_REPLACE
) != 0) {
442 DEBUG(0,("cache_mangled_name: Error storing entry %s -> %s\n", mangled_name_key
, raw_name
));
444 DEBUG(5,("cache_mangled_name: Stored entry %s -> %s\n", mangled_name_key
, raw_name
));
448 /* ************************************************************************** **
449 * Check for a name on the mangled name stack
451 * Input: s - Input *and* output string buffer.
452 * maxlen - space in i/o string buffer.
453 * Output: True if the name was found in the cache, else False.
455 * Notes: If a reverse map is found, the function will overwrite the string
456 * space indicated by the input pointer <s>. This is frightening.
457 * It should be rewritten to return NULL if the long name was not
458 * found, and a pointer to the long name if it was found.
460 * ************************************************************************** **
463 static BOOL
check_cache( char *s
, size_t maxlen
)
466 char *ext_start
= NULL
;
467 char *saved_ext
= NULL
;
469 /* If the cache isn't initialized, give up. */
470 if( !tdb_mangled_cache
)
473 data_val
= tdb_fetch_bystring(tdb_mangled_cache
, s
);
475 /* If we didn't find the name *with* the extension, try without. */
476 if(data_val
.dptr
== NULL
|| data_val
.dsize
== 0) {
477 ext_start
= strrchr( s
, '.' );
479 if((saved_ext
= SMB_STRDUP(ext_start
)) == NULL
)
483 data_val
= tdb_fetch_bystring(tdb_mangled_cache
, s
);
485 * At this point s is the name without the
486 * extension. We re-add the extension if saved_ext
487 * is not null, before freeing saved_ext.
492 /* Okay, if we haven't found it we're done. */
493 if(data_val
.dptr
== NULL
|| data_val
.dsize
== 0) {
495 /* Replace the saved_ext as it was truncated. */
496 (void)safe_strcat( s
, saved_ext
, maxlen
);
497 SAFE_FREE(saved_ext
);
502 /* If we *did* find it, we need to copy it into the string buffer. */
503 (void)safe_strcpy( s
, data_val
.dptr
, maxlen
);
505 /* Replace the saved_ext as it was truncated. */
506 (void)safe_strcat( s
, saved_ext
, maxlen
);
507 SAFE_FREE(saved_ext
);
509 SAFE_FREE(data_val
.dptr
);
513 /*****************************************************************************
514 * do the actual mangling to 8.3 format
515 * the buffer must be able to hold 13 characters (including the null)
516 *****************************************************************************
518 static void to_8_3(char *s
, int default_case
)
531 if( p
&& (strlen(p
+1) < (size_t)4) ) {
532 BOOL all_normal
= ( strisnormal(p
+1, default_case
) ); /* XXXXXXXXX */
534 if( all_normal
&& p
[1] != 0 ) {
536 csum
= str_checksum( s
);
539 csum
= str_checksum(s
);
541 csum
= str_checksum(s
);
547 safe_strcpy( extension
, "___", 3 );
550 while( *p
&& extlen
< 3 ) {
552 extension
[extlen
++] = p
[0];
556 extension
[extlen
] = 0;
562 while( *p
&& baselen
< 5 ) {
564 base
[baselen
++] = p
[0];
570 csum
= csum
% (MANGLE_BASE
*MANGLE_BASE
);
572 (void)slprintf(s
, 12, "%s%c%c%c",
573 base
, magic_char
, mangle( csum
/MANGLE_BASE
), mangle( csum
) );
576 (void)pstrcat( s
, "." );
577 (void)pstrcat( s
, extension
);
581 /*****************************************************************************
582 * Convert a filename to DOS format. Return True if successful.
584 * Input: OutName - Source *and* destination buffer.
586 * NOTE that OutName must point to a memory space that
587 * is at least 13 bytes in size!
589 * need83 - If False, name mangling will be skipped unless the
590 * name contains illegal characters. Mapping will still
591 * be done, if appropriate. This is probably used to
592 * signal that a client does not require name mangling,
593 * thus skipping the name mangling even on shares which
594 * have name-mangling turned on.
595 * cache83 - If False, the mangled name cache will not be updated.
596 * This is usually used to prevent that we overwrite
597 * a conflicting cache entry prematurely, i.e. before
598 * we know whether the client is really interested in the
599 * current name. (See PR#13758). UKD.
601 * Output: Returns False only if the name wanted mangling but the share does
602 * not have name mangling turned on.
604 * ****************************************************************************
607 static void name_map(char *OutName
, BOOL need83
, BOOL cache83
, int default_case
)
609 smb_ucs2_t
*OutName_ucs2
;
610 DEBUG(5,("name_map( %s, need83 = %s, cache83 = %s)\n", OutName
,
611 need83
? "True" : "False", cache83
? "True" : "False"));
613 if (push_ucs2_allocate(&OutName_ucs2
, OutName
) == (size_t)-1) {
614 DEBUG(0, ("push_ucs2_allocate failed!\n"));
618 if( !need83
&& !NT_STATUS_IS_OK(is_valid_name(OutName_ucs2
, False
, False
)))
621 /* check if it's already in 8.3 format */
622 if (need83
&& !NT_STATUS_IS_OK(is_8_3_w(OutName_ucs2
, False
))) {
625 /* mangle it into 8.3 */
627 tmp
= SMB_STRDUP(OutName
);
629 to_8_3(OutName
, default_case
);
632 cache_mangled_name(OutName
, tmp
);
637 DEBUG(5,("name_map() ==> [%s]\n", OutName
));
638 SAFE_FREE(OutName_ucs2
);
642 the following provides the abstraction layer to make it easier
643 to drop in an alternative mangling implementation
645 static struct mangle_fns mangle_fns
= {
653 /* return the methods for this mangling implementation */
654 struct mangle_fns
*mangle_hash_init(void)
658 /* Create the in-memory tdb using our custom hash function. */
659 tdb_mangled_cache
= tdb_open_ex("mangled_cache", 1031, TDB_INTERNAL
,
660 (O_RDWR
|O_CREAT
), 0644, NULL
, fast_string_hash
);