some changes for OSF1 support in smbwrapper (just preliminary changes,
[Samba/gbeck.git] / source / smbd / mangle.c
blobf0d5a9d85ca06f26269f06d308dd8da23bbc1cd8
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 Name mangling
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 /* -------------------------------------------------------------------------- **
23 * Notable problems...
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()
29 * below.
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 /* -------------------------------------------------------------------------- **
39 * History...
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.
44 * &c.
45 * -------------------------------------------------------------------------- **
48 #include "includes.h"
51 /* -------------------------------------------------------------------------- **
52 * External Variables...
55 extern int DEBUGLEVEL; /* Global debug level. */
56 extern int case_default; /* Are conforming 8.3 names all upper or lower? */
57 extern BOOL case_mangle; /* If true, all chars in 8.3 should be same case. */
59 /* -------------------------------------------------------------------------- **
60 * Other stuff...
62 * magic_char - This is the magic char used for mangling. It's
63 * global. There is a call to lp_magicchar() in server.c
64 * that is used to override the initial value.
66 * basechars - The set of 36 characters used for name mangling. This
67 * is static (scope is this file only).
69 * base36() - Macro used to select a character from basechars (i.e.,
70 * base36(n) will return the nth digit, modulo 36).
72 * chartest - array 0..255. The index range is the set of all possible
73 * values of a byte. For each byte value, the content is a
74 * two nibble pair. See BASECHAR_MASK and ILLEGAL_MASK,
75 * below.
77 * ct_initialized - False until the chartest array has been initialized via
78 * a call to init_chartest().
80 * BASECHAR_MASK - Masks the upper nibble of a one-byte value.
82 * ILLEGAL_MASK - Masks the lower nibble of a one-byte value.
84 * isbasecahr() - Given a character, check the chartest array to see
85 * if that character is in the basechars set. This is
86 * faster than using strchr().
88 * isillegal() - Given a character, check the chartest array to see
89 * if that character is in the illegal characters set.
90 * This is faster than using strchr().
92 * mangled_cache - Cache header used for storing mangled -> original
93 * reverse maps.
95 * mc_initialized - False until the mangled_cache structure has been
96 * initialized via a call to reset_mangled_cache().
98 * MANGLED_CACHE_MAX_ENTRIES - Default maximum number of entries for the
99 * cache. A value of 0 indicates "infinite".
101 * MANGLED_CACHE_MAX_MEMORY - Default maximum amount of memory for the
102 * cache. When the cache was kept as an array of 256
103 * byte strings, the default cache size was 50 entries.
104 * This required a fixed 12.5Kbytes of memory. The
105 * mangled stack parameter is no longer used (though
106 * this might change). We're now using a fixed 16Kbyte
107 * maximum cache size. This will probably be much more
108 * than 50 entries.
111 char magic_char = '~';
113 static char basechars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
115 static unsigned char chartest[256] = { 0 };
116 static BOOL ct_initialized = False;
118 #define base36(V) ((char)(basechars[(V) % 36]))
119 #define BASECHAR_MASK 0xf0
120 #define ILLEGAL_MASK 0x0f
121 #define isbasechar(C) ( (chartest[ ((C) & 0xff) ]) & BASECHAR_MASK )
122 #define isillegal(C) ( (chartest[ ((C) & 0xff) ]) & ILLEGAL_MASK )
124 static ubi_cacheRoot mangled_cache[1] = { { { 0 }, 0, 0, 0, 0, 0, 0} };
125 static BOOL mc_initialized = False;
126 #define MANGLED_CACHE_MAX_ENTRIES 0
127 #define MANGLED_CACHE_MAX_MEMORY 16384
130 /* -------------------------------------------------------------------------- **
131 * Functions...
134 /* ************************************************************************** **
135 * Initialize the static character test array.
137 * Input: none
139 * Output: none
141 * Notes: This function changes (loads) the contents of the <chartest>
142 * array. The scope of <chartest> is this file.
144 * ************************************************************************** **
146 static void init_chartest( void )
148 char *illegalchars = "*\\/?<>|\":";
149 unsigned char *s;
151 bzero( (char *)chartest, 256 );
153 for( s = (unsigned char *)illegalchars; *s; s++ )
154 chartest[*s] = ILLEGAL_MASK;
156 for( s = (unsigned char *)basechars; *s; s++ )
157 chartest[*s] |= BASECHAR_MASK;
159 ct_initialized = True;
160 } /* init_chartest */
162 /* ************************************************************************** **
163 * Return True if a name is a special msdos reserved name.
165 * Input: fname - String containing the name to be tested.
167 * Output: True, if the name matches one of the list of reserved names.
169 * Notes: This is a static function called by is_8_3(), below.
171 * ************************************************************************** **
173 static BOOL is_reserved_msdos( char *fname )
175 char upperFname[13];
176 char *p;
178 StrnCpy (upperFname, fname, 12);
180 /* lpt1.txt and con.txt etc are also illegal */
181 p = strchr(upperFname,'.');
182 if( p )
183 *p = '\0';
185 strupper( upperFname );
186 p = upperFname + 1;
187 switch( upperFname[0] )
189 case 'A':
190 if( 0 == strcmp( p, "UX" ) )
191 return( True );
192 break;
193 case 'C':
194 if( (0 == strcmp( p, "LOCK$" ))
195 || (0 == strcmp( p, "ON" ))
196 || (0 == strcmp( p, "OM1" ))
197 || (0 == strcmp( p, "OM2" ))
198 || (0 == strcmp( p, "OM3" ))
199 || (0 == strcmp( p, "OM4" ))
201 return( True );
202 break;
203 case 'L':
204 if( (0 == strcmp( p, "PT1" ))
205 || (0 == strcmp( p, "PT2" ))
206 || (0 == strcmp( p, "PT3" ))
208 return( True );
209 break;
210 case 'N':
211 if( 0 == strcmp( p, "UL" ) )
212 return( True );
213 break;
214 case 'P':
215 if( 0 == strcmp( p, "RN" ) )
216 return( True );
217 break;
220 return( False );
221 } /* is_reserved_msdos */
223 /* ************************************************************************** **
224 * Determine whether or not a given name contains illegal characters, even
225 * long names.
227 * Input: name - The name to be tested.
229 * Output: True if an illegal character was found in <name>, else False.
231 * Notes: This is used to test a name on the host system, long or short,
232 * for characters that would be illegal on most client systems,
233 * particularly DOS and Windows systems. Unix and AmigaOS, for
234 * example, allow a filenames which contain such oddities as
235 * quotes ("). If a name is found which does contain an illegal
236 * character, it is mangled even if it conforms to the 8.3
237 * format.
239 * ************************************************************************** **
241 static BOOL is_illegal_name( char *name )
243 unsigned char *s;
244 int skip;
246 if( !name )
247 return( True );
249 if( !ct_initialized )
250 init_chartest();
252 s = (unsigned char *)name;
253 while( *s )
255 skip = skip_multibyte_char( *s );
256 if( skip != 0 )
258 s += skip;
260 else
262 if( isillegal( *s ) )
263 return( True );
264 else
265 s++;
269 return( False );
270 } /* is_illegal_name */
272 /* ************************************************************************** **
273 * Return True if the name *could be* a mangled name.
275 * Input: s - A path name - in UNIX pathname format.
277 * Output: True if the name matches the pattern described below in the
278 * notes, else False.
280 * Notes: The input name is *not* tested for 8.3 compliance. This must be
281 * done separately. This function returns true if the name contains
282 * a magic character followed by excactly two characters from the
283 * basechars list (above), which in turn are followed either by the
284 * nul (end of string) byte or a dot (extension) or by a '/' (end of
285 * a directory name).
287 * ************************************************************************** **
289 BOOL is_mangled( char *s )
291 char *magic;
293 if( !ct_initialized )
294 init_chartest();
296 magic = strchr( s, magic_char );
297 while( magic && magic[1] && magic[2] ) /* 3 chars, 1st is magic. */
299 if( ('.' == magic[3] || '/' == magic[3] || !(magic[3])) /* Ends with '.' or nul or '/' ? */
300 && isbasechar( toupper(magic[1]) ) /* is 2nd char basechar? */
301 && isbasechar( toupper(magic[2]) ) ) /* is 3rd char basechar? */
302 return( True ); /* If all above, then true, */
303 magic = strchr( magic+1, magic_char ); /* else seek next magic. */
305 return( False );
306 } /* is_mangled */
308 /* ************************************************************************** **
309 * Return True if the name is a valid DOS name in 8.3 DOS format.
311 * Input: fname - File name to be checked.
312 * check_case - If True, and if case_mangle is True, then the
313 * name will be checked to see if all characters
314 * are the correct case. See case_mangle and
315 * case_default above.
317 * Output: True if the name is a valid DOS name, else FALSE.
319 * ************************************************************************** **
321 BOOL is_8_3( char *fname, BOOL check_case )
323 int len;
324 int l;
325 int skip;
326 char *p;
327 char *dot_pos;
328 char *slash_pos = strrchr( fname, '/' );
330 /* If there is a directory path, skip it. */
331 if( slash_pos )
332 fname = slash_pos + 1;
333 len = strlen( fname );
335 DEBUG( 5, ( "Checking %s for 8.3\n", fname ) );
337 /* Can't be 0 chars or longer than 12 chars */
338 if( (len == 0) || (len > 12) )
339 return( False );
341 /* Mustn't be an MS-DOS Special file such as lpt1 or even lpt1.txt */
342 if( is_reserved_msdos( fname ) )
343 return( False );
345 /* Check that all characters are the correct case, if asked to do so. */
346 if( check_case && case_mangle )
348 switch( case_default )
350 case CASE_LOWER:
351 if( strhasupper( fname ) )
352 return(False);
353 break;
354 case CASE_UPPER:
355 if( strhaslower( fname ) )
356 return(False);
357 break;
361 /* Can't contain invalid dos chars */
362 /* Windows use the ANSI charset.
363 But filenames are translated in the PC charset.
364 This Translation may be more or less relaxed depending
365 the Windows application. */
367 /* %%% A nice improvment to name mangling would be to translate
368 filename to ANSI charset on the smb server host */
370 p = fname;
371 dot_pos = NULL;
372 while( *p )
374 if( (skip = skip_multibyte_char( *p )) != 0 )
375 p += skip;
376 else
378 if( *p == '.' && !dot_pos )
379 dot_pos = (char *)p;
380 else
381 if( !isdoschar( *p ) )
382 return( False );
383 p++;
387 /* no dot and less than 9 means OK */
388 if( !dot_pos )
389 return( len <= 8 );
391 l = PTR_DIFF( dot_pos, fname );
393 /* base must be at least 1 char except special cases . and .. */
394 if( l == 0 )
395 return( 0 == strcmp( fname, "." ) || 0 == strcmp( fname, ".." ) );
397 /* base can't be greater than 8 */
398 if( l > 8 )
399 return( False );
401 /* see smb.conf(5) for a description of the 'strip dot' parameter. */
402 if( lp_strip_dot()
403 && len - l == 1
404 && !strchr( dot_pos + 1, '.' ) )
406 *dot_pos = 0;
407 return( True );
410 /* extension must be between 1 and 3 */
411 if( (len - l < 2 ) || (len - l > 4) )
412 return( False );
414 /* extensions may not have a dot */
415 if( strchr( dot_pos+1, '.' ) )
416 return( False );
418 /* must be in 8.3 format */
419 return( True );
420 } /* is_8_3 */
423 /* ************************************************************************** **
424 * Compare two cache keys and return a value indicating their ordinal
425 * relationship.
427 * Input: ItemPtr - Pointer to a comparison key. In this case, this will
428 * be a mangled name string.
429 * NodePtr - Pointer to a node in the cache. The node structure
430 * will be followed in memory by a mangled name string.
432 * Output: A signed integer, as follows:
433 * (x < 0) <==> Key1 less than Key2
434 * (x == 0) <==> Key1 equals Key2
435 * (x > 0) <==> Key1 greater than Key2
437 * Notes: This is a ubiqx-style comparison routine. See ubi_BinTree for
438 * more info.
440 * ************************************************************************** **
442 static signed int cache_compare( ubi_btItemPtr ItemPtr, ubi_btNodePtr NodePtr )
444 char *Key1 = (char *)ItemPtr;
445 char *Key2 = (char *)(((ubi_cacheEntryPtr)NodePtr) + 1);
447 return( StrCaseCmp( Key1, Key2 ) );
448 } /* cache_compare */
450 /* ************************************************************************** **
451 * Free a cache entry.
453 * Input: WarrenZevon - Pointer to the entry that is to be returned to
454 * Nirvana.
455 * Output: none.
457 * Notes: This function gets around the possibility that the standard
458 * free() function may be implemented as a macro, or other evil
459 * subversions (oh, so much fun).
461 * ************************************************************************** **
463 static void cache_free_entry( ubi_trNodePtr WarrenZevon )
465 free( WarrenZevon );
466 } /* cache_free_entry */
468 /* ************************************************************************** **
469 * Initializes or clears the mangled cache.
471 * Input: none.
472 * Output: none.
474 * Notes: There is a section below that is commented out. It shows how
475 * one might use lp_ calls to set the maximum memory and entry size
476 * of the cache. You might also want to remove the constants used
477 * in ubi_cacheInit() and replace them with lp_ calls. If so, then
478 * the calls to ubi_cacheSetMax*() would be moved into the else
479 * clause. Another option would be to pass in the max_entries and
480 * max_memory values as parameters. crh 09-Apr-1998.
482 * ************************************************************************** **
484 void reset_mangled_cache( void )
486 if( !mc_initialized )
488 (void)ubi_cacheInit( mangled_cache,
489 cache_compare,
490 cache_free_entry,
491 MANGLED_CACHE_MAX_ENTRIES,
492 MANGLED_CACHE_MAX_MEMORY );
493 mc_initialized = True;
495 else
497 (void)ubi_cacheClear( mangled_cache );
501 (void)ubi_cacheSetMaxEntries( mangled_cache, lp_mangled_cache_entries() );
502 (void)ubi_cacheSetMaxMemory( mangled_cache, lp_mangled_cache_memory() );
504 } /* reset_mangled_cache */
507 /* ************************************************************************** **
508 * Add a mangled name into the cache.
510 * Notes: If the mangled cache has not been initialized, then the
511 * function will simply fail. It could initialize the cache,
512 * but that's not the way it was done before I changed the
513 * cache mechanism, so I'm sticking with the old method.
515 * If the extension of the raw name maps directly to the
516 * extension of the mangled name, then we'll store both names
517 * *without* extensions. That way, we can provide consistant
518 * reverse mangling for all names that match. The test here is
519 * a bit more careful than the one done in earlier versions of
520 * mangle.c:
522 * - the extension must exist on the raw name,
523 * - it must be all lower case
524 * - it must match the mangled extension (to prove that no
525 * mangling occurred).
527 * crh 07-Apr-1998
529 * ************************************************************************** **
531 static void cache_mangled_name( char *mangled_name, char *raw_name )
533 ubi_cacheEntryPtr new_entry;
534 char *s1;
535 char *s2;
536 int mangled_len;
537 int raw_len;
538 int i;
540 /* If the cache isn't initialized, give up. */
541 if( !mc_initialized )
542 return;
544 /* Init the string lengths. */
545 mangled_len = strlen( mangled_name );
546 raw_len = strlen( raw_name );
548 /* See if the extensions are unmangled. If so, store the entry
549 * without the extension, thus creating a "group" reverse map.
551 s1 = strrchr( mangled_name, '.' );
552 if( s1 && (s2 = strrchr( raw_name, '.' )) )
554 i = 1;
555 while( s1[i] && (tolower( s1[1] ) == s2[i]) )
556 i++;
557 if( !s1[i] && !s2[i] )
559 mangled_len -= i;
560 raw_len -= i;
564 /* Allocate a new cache entry. If the allcoation fails, just return. */
565 i = sizeof( ubi_cacheEntry ) + mangled_len + raw_len + 2;
566 new_entry = malloc( i );
567 if( !new_entry )
568 return;
570 /* Fill the new cache entry, and add it to the cache. */
571 s1 = (char *)(new_entry + 1);
572 s2 = (char *)&(s1[mangled_len + 1]);
573 (void)StrnCpy( s1, mangled_name, mangled_len );
574 (void)StrnCpy( s2, raw_name, raw_len );
575 ubi_cachePut( mangled_cache, i, new_entry, s1 );
576 } /* cache_mangled_name */
578 /* ************************************************************************** **
579 * Check for a name on the mangled name stack
581 * Input: s - Input *and* output string buffer.
583 * Output: True if the name was found in the cache, else False.
585 * Notes: If a reverse map is found, the function will overwrite the string
586 * space indicated by the input pointer <s>. This is frightening.
587 * It should be rewritten to return NULL if the long name was not
588 * found, and a pointer to the long name if it was found.
590 * ************************************************************************** **
592 BOOL check_mangled_cache( char *s )
594 ubi_cacheEntryPtr FoundPtr;
595 char *ext_start = NULL;
596 char *found_name;
598 /* If the cache isn't initialized, give up. */
599 if( !mc_initialized )
600 return( False );
602 FoundPtr = ubi_cacheGet( mangled_cache, (ubi_trItemPtr)s );
604 /* If we didn't find the name *with* the extension, try without. */
605 if( !FoundPtr )
607 ext_start = strrchr( s, '.' );
608 if( ext_start )
610 *ext_start = '\0';
611 FoundPtr = ubi_cacheGet( mangled_cache, (ubi_trItemPtr)s );
612 *ext_start = '.';
616 /* Okay, if we haven't found it we're done. */
617 if( !FoundPtr )
618 return( False );
620 /* If we *did* find it, we need to copy it into the string buffer. */
621 found_name = (char *)(FoundPtr + 1);
622 found_name += (strlen( found_name ) + 1);
624 DEBUG( 3, ("Found %s on mangled stack ", s) );
626 (void)pstrcpy( s, found_name );
627 if( ext_start )
628 (void)pstrcat( s, ext_start );
630 DEBUG( 3, ("as %s\n", s) );
632 return( True );
633 } /* check_mangled_cache */
636 /* ************************************************************************** **
637 * Used only in do_fwd_mangled_map(), below.
638 * ************************************************************************** **
640 static char *map_filename( char *s, /* This is null terminated */
641 char *pattern, /* This isn't. */
642 int len ) /* This is the length of pattern. */
644 static pstring matching_bit; /* The bit of the string which matches */
645 /* a * in pattern if indeed there is a * */
646 char *sp; /* Pointer into s. */
647 char *pp; /* Pointer into p. */
648 char *match_start; /* Where the matching bit starts. */
649 pstring pat;
651 StrnCpy( pat, pattern, len ); /* Get pattern into a proper string! */
652 pstrcpy( matching_bit, "" ); /* Match but no star gets this. */
653 pp = pat; /* Initialize the pointers. */
654 sp = s;
655 if( (len == 1) && (*pattern == '*') )
657 return NULL; /* Impossible, too ambiguous for */
658 } /* words! */
660 while( (*sp) /* Not the end of the string. */
661 && (*pp) /* Not the end of the pattern. */
662 && (*sp == *pp) /* The two match. */
663 && (*pp != '*') ) /* No wildcard. */
665 sp++; /* Keep looking. */
666 pp++;
669 if( !*sp && !*pp ) /* End of pattern. */
670 return( matching_bit ); /* Simple match. Return empty string. */
672 if( *pp == '*' )
674 pp++; /* Always interrested in the chacter */
675 /* after the '*' */
676 if( !*pp ) /* It is at the end of the pattern. */
678 StrnCpy( matching_bit, s, sp-s );
679 return( matching_bit );
681 else
683 /* The next character in pattern must match a character further */
684 /* along s than sp so look for that character. */
685 match_start = sp;
686 while( (*sp) /* Not the end of s. */
687 && (*sp != *pp) ) /* Not the same */
688 sp++; /* Keep looking. */
689 if( !*sp ) /* Got to the end without a match. */
691 return( NULL );
692 } /* Still hope for a match. */
693 else
695 /* Now sp should point to a matching character. */
696 StrnCpy(matching_bit, match_start, sp-match_start);
697 /* Back to needing a stright match again. */
698 while( (*sp) /* Not the end of the string. */
699 && (*pp) /* Not the end of the pattern. */
700 && (*sp == *pp) ) /* The two match. */
702 sp++; /* Keep looking. */
703 pp++;
705 if( !*sp && !*pp ) /* Both at end so it matched */
706 return( matching_bit );
707 else
708 return( NULL );
712 return( NULL ); /* No match. */
713 } /* map_filename */
716 /* ************************************************************************** **
717 * MangledMap is a series of name pairs in () separated by spaces.
718 * If s matches the first of the pair then the name given is the
719 * second of the pair. A * means any number of any character and if
720 * present in the second of the pair as well as the first the
721 * matching part of the first string takes the place of the * in the
722 * second.
724 * I wanted this so that we could have RCS files which can be used
725 * by UNIX and DOS programs. My mapping string is (RCS rcs) which
726 * converts the UNIX RCS file subdirectory to lowercase thus
727 * preventing mangling.
729 * (I think Andrew wrote the above, but I'm not sure. -- CRH)
731 * See 'mangled map' in smb.conf(5).
733 * ************************************************************************** **
735 static void do_fwd_mangled_map(char *s, char *MangledMap)
737 char *start=MangledMap; /* Use this to search for mappings. */
738 char *end; /* Used to find the end of strings. */
739 char *match_string;
740 pstring new_string; /* Make up the result here. */
741 char *np; /* Points into new_string. */
743 DEBUG( 5, ("Mangled Mapping '%s' map '%s'\n", s, MangledMap) );
744 while( *start )
746 while( (*start) && (*start != '(') )
747 start++;
748 if( !*start )
749 continue; /* Always check for the end. */
750 start++; /* Skip the ( */
751 end = start; /* Search for the ' ' or a ')' */
752 DEBUG( 5, ("Start of first in pair '%s'\n", start) );
753 while( (*end) && !((*end == ' ') || (*end == ')')) )
754 end++;
755 if( !*end )
757 start = end;
758 continue; /* Always check for the end. */
760 DEBUG( 5, ("End of first in pair '%s'\n", end) );
761 if( (match_string = map_filename( s, start, end-start )) )
763 DEBUG( 5, ("Found a match\n") );
764 /* Found a match. */
765 start = end + 1; /* Point to start of what it is to become. */
766 DEBUG( 5, ("Start of second in pair '%s'\n", start) );
767 end = start;
768 np = new_string;
769 while( (*end) /* Not the end of string. */
770 && (*end != ')') /* Not the end of the pattern. */
771 && (*end != '*') ) /* Not a wildcard. */
772 *np++ = *end++;
773 if( !*end )
775 start = end;
776 continue; /* Always check for the end. */
778 if( *end == '*' )
780 pstrcpy( np, match_string );
781 np += strlen( match_string );
782 end++; /* Skip the '*' */
783 while( (*end) /* Not the end of string. */
784 && (*end != ')') /* Not the end of the pattern. */
785 && (*end != '*') ) /* Not a wildcard. */
786 *np++ = *end++;
788 if( !*end )
790 start = end;
791 continue; /* Always check for the end. */
793 *np++ = '\0'; /* NULL terminate it. */
794 DEBUG(5,("End of second in pair '%s'\n", end));
795 pstrcpy( s, new_string ); /* Substitute with the new name. */
796 DEBUG( 5, ("s is now '%s'\n", s) );
798 start = end; /* Skip a bit which cannot be wanted anymore. */
799 start++;
801 } /* do_fwd_mangled_map */
803 /*****************************************************************************
804 * do the actual mangling to 8.3 format
805 * the buffer must be able to hold 13 characters (including the null)
806 *****************************************************************************
808 void mangle_name_83( char *s)
810 int csum = str_checksum(s);
811 char *p;
812 char extension[4];
813 char base[9];
814 int baselen = 0;
815 int extlen = 0;
816 int skip;
818 extension[0] = 0;
819 base[0] = 0;
821 p = strrchr(s,'.');
822 if( p && (strlen(p+1) < (size_t)4) )
824 BOOL all_normal = ( strisnormal(p+1) ); /* XXXXXXXXX */
826 if( all_normal && p[1] != 0 )
828 *p = 0;
829 csum = str_checksum( s );
830 *p = '.';
834 strupper( s );
836 DEBUG( 5, ("Mangling name %s to ",s) );
838 if( p )
840 if( p == s )
841 safe_strcpy( extension, "___", 3 );
842 else
844 *p++ = 0;
845 while( *p && extlen < 3 )
847 skip = skip_multibyte_char( *p );
848 switch( skip )
850 case 2:
851 if( extlen < 2 )
853 extension[extlen++] = p[0];
854 extension[extlen++] = p[1];
856 else
858 extension[extlen++] = base36( (unsigned char)*p );
860 p += 2;
861 break;
862 case 1:
863 extension[extlen++] = p[0];
864 p++;
865 break;
866 default:
867 if( isdoschar (*p) && *p != '.' )
868 extension[extlen++] = p[0];
869 p++;
870 break;
873 extension[extlen] = 0;
877 p = s;
879 while( *p && baselen < 5 )
881 skip = skip_multibyte_char(*p);
882 switch( skip )
884 case 2:
885 if( baselen < 4 )
887 base[baselen++] = p[0];
888 base[baselen++] = p[1];
890 else
892 base[baselen++] = base36( (unsigned char)*p );
894 p += 2;
895 break;
896 case 1:
897 base[baselen++] = p[0];
898 p++;
899 break;
900 default:
901 if( isdoschar( *p ) && *p != '.' )
902 base[baselen++] = p[0];
903 p++;
904 break;
907 base[baselen] = 0;
909 csum = csum % (36*36);
911 (void)slprintf(s, 12, "%s%c%c%c",
912 base, magic_char, base36( csum/36 ), base36( csum ) );
914 if( *extension )
916 (void)pstrcat( s, "." );
917 (void)pstrcat( s, extension );
920 DEBUG( 5, ( "%s\n", s ) );
922 } /* mangle_name_83 */
924 /*****************************************************************************
925 * Convert a filename to DOS format. Return True if successful.
927 * Input: OutName - Source *and* destination buffer.
929 * NOTE that OutName must point to a memory space that
930 * is at least 13 bytes in size!
932 * need83 - If False, name mangling will be skipped unless the
933 * name contains illegal characters. Mapping will still
934 * be done, if appropriate. This is probably used to
935 * signal that a client does not require name mangling,
936 * thus skipping the name mangling even on shares which
937 * have name-mangling turned on.
938 * snum - Share number. This identifies the share in which the
939 * name exists.
941 * Output: Returns False only if the name wanted mangling but the share does
942 * not have name mangling turned on.
944 * ****************************************************************************
946 BOOL name_map_mangle(char *OutName, BOOL need83, int snum)
948 char *map;
949 DEBUG(5,("name_map_mangle( %s, %s, %d )\n",
950 OutName, need83?"TRUE":"FALSE", snum));
952 #ifdef MANGLE_LONG_FILENAMES
953 if( !need83 && is_illegal_name(OutName) )
954 need83 = True;
955 #endif
957 /* apply any name mappings */
958 map = lp_mangled_map(snum);
960 if (map && *map) {
961 do_fwd_mangled_map( OutName, map );
964 /* check if it's already in 8.3 format */
965 if (need83 && !is_8_3(OutName, True)) {
966 char *tmp;
968 if (!lp_manglednames(snum)) {
969 return(False);
972 /* mangle it into 8.3 */
973 tmp = strdup(OutName);
974 mangle_name_83(OutName);
976 if(tmp) {
977 cache_mangled_name(OutName, tmp);
978 free(tmp);
982 DEBUG(5,("name_map_mangle() ==> [%s]\n", OutName));
983 return(True);
984 } /* name_map_mangle */
986 /* ========================================================================== */