Removed lp_strip_dot code - ensured that mangling code treats names ending
[samba.git] / source3 / smbd / mangle_hash.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Name mangling
4    Copyright (C) Andrew Tridgell 1992-2002
5    Copyright (C) Simo Sorce 2001
6    Copyright (C) Andrew Bartlett 2002
7    
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.
12    
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.
17    
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.
21 */
22
23
24 /* -------------------------------------------------------------------------- **
25  * Notable problems...
26  *
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()
31  *    below.
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.
36  *
37  * -------------------------------------------------------------------------- **
38  */
39
40 /* -------------------------------------------------------------------------- **
41  * History...
42  *
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.
46  *  &c.
47  * -------------------------------------------------------------------------- **
48  */
49
50 #include "includes.h"
51
52
53 /* -------------------------------------------------------------------------- **
54  * External Variables...
55  */
56
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. */
59
60 /* -------------------------------------------------------------------------- **
61  * Other stuff...
62  *
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.
66  *
67  * MANGLE_BASE    - This is the number of characters we use for name mangling.
68  *
69  * basechars      - The set characters used for name mangling.  This
70  *                  is static (scope is this file only).
71  *
72  * mangle()       - Macro used to select a character from basechars (i.e.,
73  *                  mangle(n) will return the nth digit, modulo MANGLE_BASE).
74  *
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,
78  *                  below.
79  *
80  * ct_initialized - False until the chartest array has been initialized via
81  *                  a call to init_chartest().
82  *
83  * BASECHAR_MASK  - Masks the upper nibble of a one-byte value.
84  *
85  * ILLEGAL_MASK   - Masks the lower nibble of a one-byte value.
86  *
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().
90  *
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().
94  *
95  * mangled_cache  - Cache header used for storing mangled -> original
96  *                  reverse maps.
97  *
98  * mc_initialized - False until the mangled_cache structure has been
99  *                  initialized via a call to reset_mangled_cache().
100  *
101  * MANGLED_CACHE_MAX_ENTRIES - Default maximum number of entries for the
102  *                  cache.  A value of 0 indicates "infinite".
103  *
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
111  *                  than 50 entries.
112  */
113
114 char magic_char = '~';
115
116 static char basechars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-!@#$%";
117 #define MANGLE_BASE       (sizeof(basechars)/sizeof(char)-1)
118
119 static unsigned char chartest[256]  = { 0 };
120 static BOOL          ct_initialized = False;
121
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 )
127
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
132
133 /* -------------------------------------------------------------------------- **
134  * External Variables...
135  */
136
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. */
139
140 /* -------------------------------------------------------------------- */
141
142 static NTSTATUS has_valid_chars(const smb_ucs2_t *s)
143 {
144         if (!s || !*s) return NT_STATUS_INVALID_PARAMETER;
145
146         /* CHECK: this should not be necessary if the ms wild chars
147            are not valid in valid.dat  --- simo */
148         if (ms_has_wild_w(s)) return NT_STATUS_UNSUCCESSFUL;
149
150         while (*s) {
151                 if(!isvalid83_w(*s)) return NT_STATUS_UNSUCCESSFUL;
152                 s++;
153         }
154
155         return NT_STATUS_OK;
156 }
157
158 /* return False if something fail and
159  * return 2 alloced unicode strings that contain prefix and extension
160  */
161 static NTSTATUS mangle_get_prefix(const smb_ucs2_t *ucs2_string, smb_ucs2_t **prefix, smb_ucs2_t **extension)
162 {
163         size_t ext_len;
164         smb_ucs2_t *p;
165
166         *extension = 0;
167         *prefix = strdup_w(ucs2_string);
168         if (!*prefix) {
169                 return NT_STATUS_NO_MEMORY;
170         }
171         if ((p = strrchr_w(*prefix, UCS2_CHAR('.'))))
172         {
173                 ext_len = strlen_w(p+1);
174                 if ((ext_len > 0) && (ext_len < 4) && (p != *prefix) &&
175                     (NT_STATUS_IS_OK(has_valid_chars(p+1)))) /* check extension */
176                 {
177                         *p = 0;
178                         *extension = strdup_w(p+1);
179                         if (!*extension) {
180                                 SAFE_FREE(*prefix);
181                                 return NT_STATUS_NO_MEMORY;
182                         }
183                 }
184         }
185         return NT_STATUS_OK;
186 }
187
188 /* ************************************************************************** **
189  * Return NT_STATUS_UNSUCCESSFUL if a name is a special msdos reserved name.
190  *
191  *  Input:  fname - String containing the name to be tested.
192  *
193  *  Output: NT_STATUS_UNSUCCESSFUL, if the name matches one of the list of reserved names.
194  *
195  *  Notes:  This is a static function called by is_8_3(), below.
196  *
197  * ************************************************************************** **
198  */
199 static NTSTATUS is_valid_name(const smb_ucs2_t *fname)
200 {
201         smb_ucs2_t *str, *p;
202         NTSTATUS ret = NT_STATUS_OK;
203
204         if (!fname || !*fname) return NT_STATUS_INVALID_PARAMETER;
205
206         /* . and .. are valid names. */
207         if (strcmp_wa(fname, ".")==0 || strcmp_wa(fname, "..")==0)
208                 return NT_STATUS_OK;
209
210         /* Name cannot start with '.' */
211         if (*fname == UCS2_CHAR('.'))
212                 return NT_STATUS_UNSUCCESSFUL;
213         
214         ret = has_valid_chars(fname);
215         if (NT_STATUS_IS_ERR(ret)) return ret;
216
217         str = strdup_w(fname);
218         p = strchr_w(str, UCS2_CHAR('.'));
219         if (p && p[1] == UCS2_CHAR(0)) {
220                 /* Name cannot end in '.' */
221                 SAFE_FREE(str);
222                 return NT_STATUS_UNSUCCESSFUL;
223         }
224         if (p) *p = 0;
225         strupper_w(str);
226         p = &(str[1]);
227
228         switch(str[0])
229         {
230         case UCS2_CHAR('A'):
231                 if(strcmp_wa(p, "UX") == 0)
232                         ret = NT_STATUS_UNSUCCESSFUL;
233                 break;
234         case UCS2_CHAR('C'):
235                 if((strcmp_wa(p, "LOCK$") == 0)
236                 || (strcmp_wa(p, "ON") == 0)
237                 || (strcmp_wa(p, "OM1") == 0)
238                 || (strcmp_wa(p, "OM2") == 0)
239                 || (strcmp_wa(p, "OM3") == 0)
240                 || (strcmp_wa(p, "OM4") == 0)
241                 )
242                         ret = NT_STATUS_UNSUCCESSFUL;
243                 break;
244         case UCS2_CHAR('L'):
245                 if((strcmp_wa(p, "PT1") == 0)
246                 || (strcmp_wa(p, "PT2") == 0)
247                 || (strcmp_wa(p, "PT3") == 0)
248                 )
249                         ret = NT_STATUS_UNSUCCESSFUL;
250                 break;
251         case UCS2_CHAR('N'):
252                 if(strcmp_wa(p, "UL") == 0)
253                         ret = NT_STATUS_UNSUCCESSFUL;
254                 break;
255         case UCS2_CHAR('P'):
256                 if(strcmp_wa(p, "RN") == 0)
257                         ret = NT_STATUS_UNSUCCESSFUL;
258                 break;
259         default:
260                 break;
261         }
262
263         SAFE_FREE(str);
264         return ret;
265 }
266
267 static NTSTATUS is_8_3_w(const smb_ucs2_t *fname)
268 {
269         smb_ucs2_t *pref = 0, *ext = 0;
270         size_t plen;
271         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
272
273         if (!fname || !*fname) return NT_STATUS_INVALID_PARAMETER;
274
275         if (strlen_w(fname) > 12) return NT_STATUS_UNSUCCESSFUL;
276         
277         if (strcmp_wa(fname, ".") == 0 || strcmp_wa(fname, "..") == 0)
278                 return NT_STATUS_OK;
279
280         if (NT_STATUS_IS_ERR(is_valid_name(fname))) goto done;
281
282         if (NT_STATUS_IS_ERR(mangle_get_prefix(fname, &pref, &ext))) goto done;
283         plen = strlen_w(pref);
284
285         if (strchr_wa(pref, '.')) goto done;
286         if (plen < 1 || plen > 8) goto done;
287         if (ext) if (strlen_w(ext) > 3) goto done;
288
289         ret = NT_STATUS_OK;
290
291 done:
292         SAFE_FREE(pref);
293         SAFE_FREE(ext);
294         return ret;
295 }
296
297 static BOOL is_8_3(const char *fname, BOOL check_case)
298 {
299         const char *f;
300         smb_ucs2_t *ucs2name;
301         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
302
303         if (!fname || !*fname) return False;
304         if ((f = strrchr(fname, '/')) == NULL) f = fname;
305         else f++;
306
307         if (strlen(f) > 12) return False;
308         
309         ucs2name = acnv_uxu2(f);
310         if (!ucs2name)
311         {
312                 DEBUG(0,("is_8_3: internal error acnv_uxu2() failed!\n"));
313                 goto done;
314         }
315
316         ret = is_8_3_w(ucs2name);
317
318 done:
319         SAFE_FREE(ucs2name);
320
321         if (!NT_STATUS_IS_OK(ret)) { 
322                 return False;
323         }
324         
325         return True;
326 }
327
328
329
330 /* -------------------------------------------------------------------------- **
331  * Functions...
332  */
333
334 /* ************************************************************************** **
335  * Initialize the static character test array.
336  *
337  *  Input:  none
338  *
339  *  Output: none
340  *
341  *  Notes:  This function changes (loads) the contents of the <chartest>
342  *          array.  The scope of <chartest> is this file.
343  *
344  * ************************************************************************** **
345  */
346 static void init_chartest( void )
347   {
348   char          *illegalchars = "*\\/?<>|\":";
349   unsigned char *s;
350   
351   memset( (char *)chartest, '\0', 256 );
352
353   for( s = (unsigned char *)illegalchars; *s; s++ )
354     chartest[*s] = ILLEGAL_MASK;
355
356   for( s = (unsigned char *)basechars; *s; s++ )
357     chartest[*s] |= BASECHAR_MASK;
358
359   ct_initialized = True;
360   } /* init_chartest */
361
362
363 /* ************************************************************************** **
364  * Return True if the name *could be* a mangled name.
365  *
366  *  Input:  s - A path name - in UNIX pathname format.
367  *
368  *  Output: True if the name matches the pattern described below in the
369  *          notes, else False.
370  *
371  *  Notes:  The input name is *not* tested for 8.3 compliance.  This must be
372  *          done separately.  This function returns true if the name contains
373  *          a magic character followed by excactly two characters from the
374  *          basechars list (above), which in turn are followed either by the
375  *          nul (end of string) byte or a dot (extension) or by a '/' (end of
376  *          a directory name).
377  *
378  * ************************************************************************** **
379  */
380 static BOOL is_mangled(const char *s)
381   {
382   char *magic;
383
384   if( !ct_initialized )
385     init_chartest();
386
387   magic = strchr_m( s, magic_char );
388   while( magic && magic[1] && magic[2] )          /* 3 chars, 1st is magic. */
389     {
390     if( ('.' == magic[3] || '/' == magic[3] || !(magic[3]))          /* Ends with '.' or nul or '/' ?  */
391      && isbasechar( toupper(magic[1]) )           /* is 2nd char basechar?  */
392      && isbasechar( toupper(magic[2]) ) )         /* is 3rd char basechar?  */
393       return( True );                           /* If all above, then true, */
394     magic = strchr_m( magic+1, magic_char );      /*    else seek next magic. */
395     }
396   return( False );
397   } /* is_mangled */
398
399
400 /* ************************************************************************** **
401  * Compare two cache keys and return a value indicating their ordinal
402  * relationship.
403  *
404  *  Input:  ItemPtr - Pointer to a comparison key.  In this case, this will
405  *                    be a mangled name string.
406  *          NodePtr - Pointer to a node in the cache.  The node structure
407  *                    will be followed in memory by a mangled name string.
408  *
409  *  Output: A signed integer, as follows:
410  *            (x < 0)  <==> Key1 less than Key2
411  *            (x == 0) <==> Key1 equals Key2
412  *            (x > 0)  <==> Key1 greater than Key2
413  *
414  *  Notes:  This is a ubiqx-style comparison routine.  See ubi_BinTree for
415  *          more info.
416  *
417  * ************************************************************************** **
418  */
419 static signed int cache_compare( ubi_btItemPtr ItemPtr, ubi_btNodePtr NodePtr )
420   {
421   char *Key1 = (char *)ItemPtr;
422   char *Key2 = (char *)(((ubi_cacheEntryPtr)NodePtr) + 1);
423
424   return( StrCaseCmp( Key1, Key2 ) );
425   } /* cache_compare */
426
427 /* ************************************************************************** **
428  * Free a cache entry.
429  *
430  *  Input:  WarrenZevon - Pointer to the entry that is to be returned to
431  *                        Nirvana.
432  *  Output: none.
433  *
434  *  Notes:  This function gets around the possibility that the standard
435  *          free() function may be implemented as a macro, or other evil
436  *          subversions (oh, so much fun).
437  *
438  * ************************************************************************** **
439  */
440 static void cache_free_entry( ubi_trNodePtr WarrenZevon )
441   {
442           ZERO_STRUCTP(WarrenZevon);
443           SAFE_FREE( WarrenZevon );
444   } /* cache_free_entry */
445
446 /* ************************************************************************** **
447  * Initializes or clears the mangled cache.
448  *
449  *  Input:  none.
450  *  Output: none.
451  *
452  *  Notes:  There is a section below that is commented out.  It shows how
453  *          one might use lp_ calls to set the maximum memory and entry size
454  *          of the cache.  You might also want to remove the constants used
455  *          in ubi_cacheInit() and replace them with lp_ calls.  If so, then
456  *          the calls to ubi_cacheSetMax*() would be moved into the else
457  *          clause.  Another option would be to pass in the max_entries and
458  *          max_memory values as parameters.  crh 09-Apr-1998.
459  *
460  * ************************************************************************** **
461  */
462 static void mangle_reset( void )
463   {
464   if( !mc_initialized )
465     {
466     (void)ubi_cacheInit( mangled_cache,
467                          cache_compare,
468                          cache_free_entry,
469                          MANGLED_CACHE_MAX_ENTRIES,
470                          MANGLED_CACHE_MAX_MEMORY );
471     mc_initialized = True;
472     }
473   else
474     {
475     (void)ubi_cacheClear( mangled_cache );
476     }
477
478   /*
479   (void)ubi_cacheSetMaxEntries( mangled_cache, lp_mangled_cache_entries() );
480   (void)ubi_cacheSetMaxMemory(  mangled_cache, lp_mangled_cache_memory() );
481   */
482   } /* reset_mangled_cache  */
483
484
485 /* ************************************************************************** **
486  * Add a mangled name into the cache.
487  *
488  *  Notes:  If the mangled cache has not been initialized, then the
489  *          function will simply fail.  It could initialize the cache,
490  *          but that's not the way it was done before I changed the
491  *          cache mechanism, so I'm sticking with the old method.
492  *
493  *          If the extension of the raw name maps directly to the
494  *          extension of the mangled name, then we'll store both names
495  *          *without* extensions.  That way, we can provide consistent
496  *          reverse mangling for all names that match.  The test here is
497  *          a bit more careful than the one done in earlier versions of
498  *          mangle.c:
499  *
500  *            - the extension must exist on the raw name,
501  *            - it must be all lower case
502  *            - it must match the mangled extension (to prove that no
503  *              mangling occurred).
504  *
505  *  crh 07-Apr-1998
506  *
507  * ************************************************************************** **
508  */
509 static void cache_mangled_name( char *mangled_name, char *raw_name )
510   {
511   ubi_cacheEntryPtr new_entry;
512   char             *s1;
513   char             *s2;
514   size_t               mangled_len;
515   size_t               raw_len;
516   size_t               i;
517
518   /* If the cache isn't initialized, give up. */
519   if( !mc_initialized )
520     return;
521
522   /* Init the string lengths. */
523   mangled_len = strlen( mangled_name );
524   raw_len     = strlen( raw_name );
525
526   /* See if the extensions are unmangled.  If so, store the entry
527    * without the extension, thus creating a "group" reverse map.
528    */
529   s1 = strrchr( mangled_name, '.' );
530   if( s1 && (s2 = strrchr( raw_name, '.' )) )
531     {
532     i = 1;
533     while( s1[i] && (tolower( s1[i] ) == s2[i]) )
534       i++;
535     if( !s1[i] && !s2[i] )
536       {
537       mangled_len -= i;
538       raw_len     -= i;
539       }
540     }
541
542   /* Allocate a new cache entry.  If the allocation fails, just return. */
543   i = sizeof( ubi_cacheEntry ) + mangled_len + raw_len + 2;
544   new_entry = malloc( i );
545   if( !new_entry )
546     return;
547
548   /* Fill the new cache entry, and add it to the cache. */
549   s1 = (char *)(new_entry + 1);
550   s2 = (char *)&(s1[mangled_len + 1]);
551   (void)StrnCpy( s1, mangled_name, mangled_len );
552   (void)StrnCpy( s2, raw_name,     raw_len );
553   ubi_cachePut( mangled_cache, i, new_entry, s1 );
554   } /* cache_mangled_name */
555
556 /* ************************************************************************** **
557  * Check for a name on the mangled name stack
558  *
559  *  Input:  s - Input *and* output string buffer.
560  *
561  *  Output: True if the name was found in the cache, else False.
562  *
563  *  Notes:  If a reverse map is found, the function will overwrite the string
564  *          space indicated by the input pointer <s>.  This is frightening.
565  *          It should be rewritten to return NULL if the long name was not
566  *          found, and a pointer to the long name if it was found.
567  *
568  * ************************************************************************** **
569  */
570
571 static BOOL check_cache( char *s )
572 {
573   ubi_cacheEntryPtr FoundPtr;
574   char             *ext_start = NULL;
575   char             *found_name;
576   char             *saved_ext = NULL;
577
578   /* If the cache isn't initialized, give up. */
579   if( !mc_initialized )
580     return( False );
581
582   FoundPtr = ubi_cacheGet( mangled_cache, (ubi_trItemPtr)s );
583
584   /* If we didn't find the name *with* the extension, try without. */
585   if( !FoundPtr )
586   {
587     ext_start = strrchr( s, '.' );
588     if( ext_start )
589     {
590       if((saved_ext = strdup(ext_start)) == NULL)
591         return False;
592
593       *ext_start = '\0';
594       FoundPtr = ubi_cacheGet( mangled_cache, (ubi_trItemPtr)s );
595       /* 
596        * At this point s is the name without the
597        * extension. We re-add the extension if saved_ext
598        * is not null, before freeing saved_ext.
599        */
600     }
601   }
602
603   /* Okay, if we haven't found it we're done. */
604   if( !FoundPtr )
605   {
606     if(saved_ext)
607     {
608       /* Replace the saved_ext as it was truncated. */
609       (void)pstrcat( s, saved_ext );
610       SAFE_FREE(saved_ext);
611     }
612     return( False );
613   }
614
615   /* If we *did* find it, we need to copy it into the string buffer. */
616   found_name = (char *)(FoundPtr + 1);
617   found_name += (strlen( found_name ) + 1);
618
619   (void)pstrcpy( s, found_name );
620   if( saved_ext )
621   {
622     /* Replace the saved_ext as it was truncated. */
623     (void)pstrcat( s, saved_ext );
624     SAFE_FREE(saved_ext);
625   }
626
627   return( True );
628 } /* check_mangled_cache */
629
630
631 /*****************************************************************************
632  * do the actual mangling to 8.3 format
633  * the buffer must be able to hold 13 characters (including the null)
634  *****************************************************************************
635  */
636 static void to_8_3(char *s)
637   {
638   int csum;
639   char *p;
640   char extension[4];
641   char base[9];
642   int baselen = 0;
643   int extlen = 0;
644
645   extension[0] = 0;
646   base[0] = 0;
647
648   p = strrchr(s,'.');  
649   if( p && (strlen(p+1) < (size_t)4) )
650     {
651     BOOL all_normal = ( strisnormal(p+1) ); /* XXXXXXXXX */
652
653     if( all_normal && p[1] != 0 )
654       {
655       *p = 0;
656       csum = str_checksum( s );
657       *p = '.';
658       }
659     else
660       csum = str_checksum(s);
661     }
662   else
663     csum = str_checksum(s);
664
665   strupper( s );
666
667   if( p )
668   {
669           if( p == s )
670                   safe_strcpy( extension, "___", 3 );
671           else
672           {
673                   *p++ = 0;
674                   while( *p && extlen < 3 )
675                   {
676                           if ( *p != '.') {
677                                   extension[extlen++] = p[0];
678                           }
679                           p++;
680                   }
681                   extension[extlen] = 0;
682       }
683   }
684   
685   p = s;
686
687   while( *p && baselen < 5 )
688   {
689           if (*p != '.') {
690                   base[baselen++] = p[0];
691           }
692           p++;
693   }
694   base[baselen] = 0;
695   
696   csum = csum % (MANGLE_BASE*MANGLE_BASE);
697   
698   (void)slprintf(s, 12, "%s%c%c%c",
699                  base, magic_char, mangle( csum/MANGLE_BASE ), mangle( csum ) );
700   
701   if( *extension )
702   {
703           (void)pstrcat( s, "." );
704           (void)pstrcat( s, extension );
705   }
706   
707   } /* mangle_name_83 */
708
709 /*****************************************************************************
710  * Convert a filename to DOS format.  Return True if successful.
711  *
712  *  Input:  OutName - Source *and* destination buffer. 
713  *
714  *                    NOTE that OutName must point to a memory space that
715  *                    is at least 13 bytes in size!
716  *
717  *          need83  - If False, name mangling will be skipped unless the
718  *                    name contains illegal characters.  Mapping will still
719  *                    be done, if appropriate.  This is probably used to
720  *                    signal that a client does not require name mangling,
721  *                    thus skipping the name mangling even on shares which
722  *                    have name-mangling turned on.
723  *          cache83 - If False, the mangled name cache will not be updated.
724  *                    This is usually used to prevent that we overwrite
725  *                    a conflicting cache entry prematurely, i.e. before
726  *                    we know whether the client is really interested in the
727  *                    current name.  (See PR#13758).  UKD.
728  *
729  *  Output: Returns False only if the name wanted mangling but the share does
730  *          not have name mangling turned on.
731  *
732  * ****************************************************************************
733  */
734 static BOOL name_map(char *OutName, BOOL need83, BOOL cache83)
735 {
736         smb_ucs2_t *OutName_ucs2;
737         DEBUG(5,("name_map( %s, need83 = %s, cache83 = %s)\n", OutName,
738                  need83 ? "True" : "False", cache83 ? "True" : "False"));
739         
740         if (push_ucs2_allocate((void **)&OutName_ucs2, OutName) < 0) {
741                 DEBUG(0, ("push_ucs2_allocate failed!\n"));
742                 return False;
743         }
744
745         if( !need83 && NT_STATUS_IS_ERR(is_valid_name(OutName_ucs2)))
746                 need83 = True;
747
748         /* check if it's already in 8.3 format */
749         if (need83 && !NT_STATUS_IS_OK(is_8_3_w(OutName_ucs2))) {
750                 char *tmp = NULL; 
751
752                 /* mangle it into 8.3 */
753                 if (cache83)
754                         tmp = strdup(OutName);
755
756                 to_8_3(OutName);
757
758                 if(tmp != NULL) {
759                         cache_mangled_name(OutName, tmp);
760                         SAFE_FREE(tmp);
761                 }
762         }
763
764         DEBUG(5,("name_map() ==> [%s]\n", OutName));
765         SAFE_FREE(OutName_ucs2);
766         return(True);
767 } /* name_map */
768
769
770 /*
771   the following provides the abstraction layer to make it easier
772   to drop in an alternative mangling implementation
773 */
774 static struct mangle_fns mangle_fns = {
775         is_mangled,
776         is_8_3,
777         mangle_reset,
778         check_cache,
779         name_map
780 };
781
782 /* return the methods for this mangling implementation */
783 struct mangle_fns *mangle_hash_init(void)
784 {
785         mangle_reset();
786
787         return &mangle_fns;
788 }