r16945: Sync trunk -> 3.0 for 3.0.24 code. Still need
[kai/samba.git] / source3 / smbd / mangle_hash.c
index 7b8cbdbddba76bd328ccc1ee3cac1f4fe2ad5df3..320e31ab679e3796570d018511fde9ba60a43ddc 100644 (file)
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
-
-/* -------------------------------------------------------------------------- **
- * Notable problems...
- *
- *  March/April 1998  CRH
- *  - Many of the functions in this module overwrite string buffers passed to
- *    them.  This causes a variety of problems and is, generally speaking,
- *    dangerous and scarry.  See the kludge notes in name_map()
- *    below.
- *  - It seems that something is calling name_map() twice.  The
- *    first call is probably some sort of test.  Names which contain
- *    illegal characters are being doubly mangled.  I'm not sure, but
- *    I'm guessing the problem is in server.c.
- *
- * -------------------------------------------------------------------------- **
- */
-
-/* -------------------------------------------------------------------------- **
- * History...
- *
- *  March/April 1998  CRH
- *  Updated a bit.  Rewrote is_mangled() to be a bit more selective.
- *  Rewrote the mangled name cache.  Added comments here and there.
- *  &c.
- * -------------------------------------------------------------------------- **
- */
-
 #include "includes.h"
 
-
 /* -------------------------------------------------------------------------- **
  * Other stuff...
  *
  *
  * chartest       - array 0..255.  The index range is the set of all possible
  *                  values of a byte.  For each byte value, the content is a
- *                  two nibble pair.  See BASECHAR_MASK and ILLEGAL_MASK,
- *                  below.
+ *                  two nibble pair.  See BASECHAR_MASK below.
  *
  * ct_initialized - False until the chartest array has been initialized via
  *                  a call to init_chartest().
  *
  * BASECHAR_MASK  - Masks the upper nibble of a one-byte value.
  *
- * ILLEGAL_MASK   - Masks the lower nibble of a one-byte value.
- *
  * isbasecahr()   - Given a character, check the chartest array to see
  *                  if that character is in the basechars set.  This is
  *                  faster than using strchr_m().
  *
- * isillegal()    - Given a character, check the chartest array to see
- *                  if that character is in the illegal characters set.
- *                  This is faster than using strchr_m().
- *
- * mangled_cache  - Cache header used for storing mangled -> original
- *                  reverse maps.
- *
- * mc_initialized - False until the mangled_cache structure has been
- *                  initialized via a call to reset_mangled_cache().
- *
- * MANGLED_CACHE_MAX_ENTRIES - Default maximum number of entries for the
- *                  cache.  A value of 0 indicates "infinite".
- *
- * MANGLED_CACHE_MAX_MEMORY  - Default maximum amount of memory for the
- *                  cache.  When the cache was kept as an array of 256
- *                  byte strings, the default cache size was 50 entries.
- *                  This required a fixed 12.5Kbytes of memory.  The
- *                  mangled stack parameter is no longer used (though
- *                  this might change).  We're now using a fixed 16Kbyte
- *                  maximum cache size.  This will probably be much more
- *                  than 50 entries.
  */
 
 char magic_char = '~';
@@ -114,30 +62,50 @@ static BOOL          ct_initialized = False;
 
 #define mangle(V) ((char)(basechars[(V) % MANGLE_BASE]))
 #define BASECHAR_MASK 0xf0
-#define ILLEGAL_MASK  0x0f
 #define isbasechar(C) ( (chartest[ ((C) & 0xff) ]) & BASECHAR_MASK )
-#define isillegal(C) ( (chartest[ ((C) & 0xff) ]) & ILLEGAL_MASK )
 
-static ubi_cacheRoot mangled_cache[1] =  { { { 0, 0, 0, 0 }, 0, 0, 0, 0, 0, 0 } };
-static BOOL          mc_initialized   = False;
-#define MANGLED_CACHE_MAX_ENTRIES 1024
-#define MANGLED_CACHE_MAX_MEMORY 0
+static TDB_CONTEXT *tdb_mangled_cache;
 
 /* -------------------------------------------------------------------- */
 
 static NTSTATUS has_valid_83_chars(const smb_ucs2_t *s, BOOL allow_wildcards)
 {
-       if (!s || !*s)
+       if (!*s) {
                return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       if (!allow_wildcards && ms_has_wild_w(s)) {
+               return NT_STATUS_UNSUCCESSFUL;
+       }
+
+       while (*s) {
+               if(!isvalid83_w(*s)) {
+                       return NT_STATUS_UNSUCCESSFUL;
+               }
+               s++;
+       }
 
-       /* CHECK: this should not be necessary if the ms wild chars
-          are not valid in valid.dat  --- simo */
-       if (!allow_wildcards && ms_has_wild_w(s))
+       return NT_STATUS_OK;
+}
+
+static NTSTATUS has_illegal_chars(const smb_ucs2_t *s, BOOL allow_wildcards)
+{
+       if (!allow_wildcards && ms_has_wild_w(s)) {
                return NT_STATUS_UNSUCCESSFUL;
+       }
 
        while (*s) {
-               if(!isvalid83_w(*s))
+               if (*s <= 0x1f) {
+                       /* Control characters. */
                        return NT_STATUS_UNSUCCESSFUL;
+               }
+               switch(*s) {
+                       case UCS2_CHAR('\\'):
+                       case UCS2_CHAR('/'):
+                       case UCS2_CHAR('|'):
+                       case UCS2_CHAR(':'):
+                               return NT_STATUS_UNSUCCESSFUL;
+               }
                s++;
        }
 
@@ -176,10 +144,11 @@ static NTSTATUS mangle_get_prefix(const smb_ucs2_t *ucs2_string, smb_ucs2_t **pr
 
 /* ************************************************************************** **
  * Return NT_STATUS_UNSUCCESSFUL if a name is a special msdos reserved name.
+ * or contains illegal characters.
  *
  *  Input:  fname - String containing the name to be tested.
  *
- *  Output: NT_STATUS_UNSUCCESSFUL, if the name matches one of the list of reserved names.
+ *  Output: NT_STATUS_UNSUCCESSFUL, if the condition above is true.
  *
  *  Notes:  This is a static function called by is_8_3(), below.
  *
@@ -189,6 +158,7 @@ static NTSTATUS mangle_get_prefix(const smb_ucs2_t *ucs2_string, smb_ucs2_t **pr
 static NTSTATUS is_valid_name(const smb_ucs2_t *fname, BOOL allow_wildcards, BOOL only_8_3)
 {
        smb_ucs2_t *str, *p;
+       size_t num_ucs2_chars;
        NTSTATUS ret = NT_STATUS_OK;
 
        if (!fname || !*fname)
@@ -198,27 +168,32 @@ static NTSTATUS is_valid_name(const smb_ucs2_t *fname, BOOL allow_wildcards, BOO
        if (strcmp_wa(fname, ".")==0 || strcmp_wa(fname, "..")==0)
                return NT_STATUS_OK;
 
-       /* Name cannot start with '.' */
-       if (*fname == UCS2_CHAR('.'))
-               return NT_STATUS_UNSUCCESSFUL;
-       
        if (only_8_3) {
                ret = has_valid_83_chars(fname, allow_wildcards);
                if (!NT_STATUS_IS_OK(ret))
                        return ret;
        }
 
-       str = strdup_w(fname);
-       p = strchr_w(str, UCS2_CHAR('.'));
-       if (p && p[1] == UCS2_CHAR(0)) {
-               /* Name cannot end in '.' */
-               SAFE_FREE(str);
+       ret = has_illegal_chars(fname, allow_wildcards);
+       if (!NT_STATUS_IS_OK(ret))
+               return ret;
+
+       /* Name can't end in '.' or ' ' */
+       num_ucs2_chars = strlen_w(fname);
+       if (fname[num_ucs2_chars-1] == UCS2_CHAR('.') || fname[num_ucs2_chars-1] == UCS2_CHAR(' ')) {
                return NT_STATUS_UNSUCCESSFUL;
        }
-       if (p)
+
+       str = strdup_w(fname);
+
+       /* Truncate copy after the first dot. */
+       p = strchr_w(str, UCS2_CHAR('.'));
+       if (p) {
                *p = 0;
+       }
+
        strupper_w(str);
-       p = &(str[1]);
+       p = &str[1];
 
        switch(str[0])
        {
@@ -274,6 +249,10 @@ static NTSTATUS is_8_3_w(const smb_ucs2_t *fname, BOOL allow_wildcards)
        if (strcmp_wa(fname, ".") == 0 || strcmp_wa(fname, "..") == 0)
                return NT_STATUS_OK;
 
+       /* Name cannot start with '.' */
+       if (*fname == UCS2_CHAR('.'))
+               return NT_STATUS_UNSUCCESSFUL;
+       
        if (!NT_STATUS_IS_OK(is_valid_name(fname, allow_wildcards, True)))
                goto done;
 
@@ -296,13 +275,16 @@ done:
        return ret;
 }
 
-static BOOL is_8_3(const char *fname, BOOL check_case, BOOL allow_wildcards)
+static BOOL is_8_3(const char *fname, BOOL check_case, BOOL allow_wildcards,
+                  const struct share_params *p)
 {
        const char *f;
        smb_ucs2_t *ucs2name;
        NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
        size_t size;
 
+       magic_char = lp_magicchar(p);
+
        if (!fname || !*fname)
                return False;
        if ((f = strrchr(fname, '/')) == NULL)
@@ -351,16 +333,13 @@ done:
  */
 static void init_chartest( void )
 {
-       const char          *illegalchars = "*\\/?<>|\":";
        const unsigned char *s;
   
        memset( (char *)chartest, '\0', 256 );
 
-       for( s = (const unsigned char *)illegalchars; *s; s++ )
-               chartest[*s] = ILLEGAL_MASK;
-
-       for( s = (const unsigned char *)basechars; *s; s++ )
+       for( s = (const unsigned char *)basechars; *s; s++ ) {
                chartest[*s] |= BASECHAR_MASK;
+       }
 
        ct_initialized = True;
 }
@@ -382,180 +361,96 @@ static void init_chartest( void )
  *
  * ************************************************************************** **
  */
-static BOOL is_mangled(const char *s)
+static BOOL is_mangled(const char *s, const struct share_params *p)
 {
        char *magic;
 
+       magic_char = lp_magicchar(p);
+
        if( !ct_initialized )
                init_chartest();
 
        magic = strchr_m( s, magic_char );
        while( magic && magic[1] && magic[2] ) {         /* 3 chars, 1st is magic. */
                if( ('.' == magic[3] || '/' == magic[3] || !(magic[3]))          /* Ends with '.' or nul or '/' ?  */
-                               && isbasechar( toupper(magic[1]) )           /* is 2nd char basechar?  */
-                               && isbasechar( toupper(magic[2]) ) )         /* is 3rd char basechar?  */
+                               && isbasechar( toupper_ascii(magic[1]) )           /* is 2nd char basechar?  */
+                               && isbasechar( toupper_ascii(magic[2]) ) )         /* is 3rd char basechar?  */
                        return( True );                           /* If all above, then true, */
                magic = strchr_m( magic+1, magic_char );      /*    else seek next magic. */
        }
        return( False );
 }
 
-/* ************************************************************************** **
- * Compare two cache keys and return a value indicating their ordinal
- * relationship.
- *
- *  Input:  ItemPtr - Pointer to a comparison key.  In this case, this will
- *                    be a mangled name string.
- *          NodePtr - Pointer to a node in the cache.  The node structure
- *                    will be followed in memory by a mangled name string.
- *
- *  Output: A signed integer, as follows:
- *            (x < 0)  <==> Key1 less than Key2
- *            (x == 0) <==> Key1 equals Key2
- *            (x > 0)  <==> Key1 greater than Key2
- *
- *  Notes:  This is a ubiqx-style comparison routine.  See ubi_BinTree for
- *          more info.
- *
- * ************************************************************************** **
- */
-static signed int cache_compare( ubi_btItemPtr ItemPtr, ubi_btNodePtr NodePtr )
-{
-       char *Key1 = (char *)ItemPtr;
-       char *Key2 = (char *)(((ubi_cacheEntryPtr)NodePtr) + 1);
-
-       return( StrCaseCmp( Key1, Key2 ) );
-}
-
-/* ************************************************************************** **
- * Free a cache entry.
- *
- *  Input:  WarrenZevon - Pointer to the entry that is to be returned to
- *                        Nirvana.
- *  Output: none.
- *
- *  Notes:  This function gets around the possibility that the standard
- *          free() function may be implemented as a macro, or other evil
- *          subversions (oh, so much fun).
- *
- * ************************************************************************** **
- */
-static void cache_free_entry( ubi_trNodePtr WarrenZevon )
-{
-       ZERO_STRUCTP(WarrenZevon);
-       SAFE_FREE( WarrenZevon );
-}
-
-/* ************************************************************************** **
- * Initializes or clears the mangled cache.
- *
- *  Input:  none.
- *  Output: none.
- *
- *  Notes:  There is a section below that is commented out.  It shows how
- *          one might use lp_ calls to set the maximum memory and entry size
- *          of the cache.  You might also want to remove the constants used
- *          in ubi_cacheInit() and replace them with lp_ calls.  If so, then
- *          the calls to ubi_cacheSetMax*() would be moved into the else
- *          clause.  Another option would be to pass in the max_entries and
- *          max_memory values as parameters.  crh 09-Apr-1998.
- *
- * ************************************************************************** **
- */
+/***************************************************************************
+ Initializes or clears the mangled cache.
+***************************************************************************/
 
 static void mangle_reset( void )
 {
-       if( !mc_initialized ) {
-               (void)ubi_cacheInit( mangled_cache,
-                               cache_compare,
-                               cache_free_entry,
-                               MANGLED_CACHE_MAX_ENTRIES,
-                               MANGLED_CACHE_MAX_MEMORY );
-               mc_initialized = True;
-       } else {
-               (void)ubi_cacheClear( mangled_cache );
-       }
-
-       /*
-       (void)ubi_cacheSetMaxEntries( mangled_cache, lp_mangled_cache_entries() );
-       (void)ubi_cacheSetMaxMemory(  mangled_cache, lp_mangled_cache_memory() );
-       */
+       /* We could close and re-open the tdb here... should we ? The old code did
+          the equivalent... JRA. */
 }
 
-/* ************************************************************************** **
- * Add a mangled name into the cache.
- *
- *  Notes:  If the mangled cache has not been initialized, then the
- *          function will simply fail.  It could initialize the cache,
- *          but that's not the way it was done before I changed the
- *          cache mechanism, so I'm sticking with the old method.
- *
- *          If the extension of the raw name maps directly to the
- *          extension of the mangled name, then we'll store both names
- *          *without* extensions.  That way, we can provide consistent
- *          reverse mangling for all names that match.  The test here is
- *          a bit more careful than the one done in earlier versions of
- *          mangle.c:
- *
- *            - the extension must exist on the raw name,
- *            - it must be all lower case
- *            - it must match the mangled extension (to prove that no
- *              mangling occurred).
- *
- *  crh 07-Apr-1998
- *
- * ************************************************************************** **
- */
-static void cache_mangled_name( char *mangled_name, char *raw_name )
+/***************************************************************************
+ Add a mangled name into the cache.
+ If the extension of the raw name maps directly to the
+ extension of the mangled name, then we'll store both names
+ *without* extensions.  That way, we can provide consistent
+ reverse mangling for all names that match.  The test here is
+ a bit more careful than the one done in earlier versions of
+ mangle.c:
+
+    - the extension must exist on the raw name,
+    - it must be all lower case
+    - it must match the mangled extension (to prove that no
+      mangling occurred).
+  crh 07-Apr-1998
+**************************************************************************/
+
+static void cache_mangled_name( const char mangled_name[13], char *raw_name )
 {
-       ubi_cacheEntryPtr new_entry;
-       char             *s1;
-       char             *s2;
-       size_t               mangled_len;
-       size_t               raw_len;
-       size_t               i;
+       TDB_DATA data_val;
+       char mangled_name_key[13];
+       char *s1;
+       char *s2;
 
        /* If the cache isn't initialized, give up. */
-       if( !mc_initialized )
+       if( !tdb_mangled_cache )
                return;
 
        /* Init the string lengths. */
-       mangled_len = strlen( mangled_name );
-       raw_len     = strlen( raw_name );
+       safe_strcpy(mangled_name_key, mangled_name, sizeof(mangled_name_key)-1);
 
        /* See if the extensions are unmangled.  If so, store the entry
         * without the extension, thus creating a "group" reverse map.
         */
-       s1 = strrchr( mangled_name, '.' );
+       s1 = strrchr( mangled_name_key, '.' );
        if( s1 && (s2 = strrchr( raw_name, '.' )) ) {
-               i = 1;
-               while( s1[i] && (tolower( s1[i] ) == s2[i]) )
+               size_t i = 1;
+               while( s1[i] && (tolower_ascii( s1[i] ) == s2[i]) )
                        i++;
                if( !s1[i] && !s2[i] ) {
-                       mangled_len -= i;
-                       raw_len     -= i;
+                       /* Truncate at the '.' */
+                       *s1 = '\0';
+                       *s2 = '\0';
                }
        }
 
        /* Allocate a new cache entry.  If the allocation fails, just return. */
-       i = sizeof( ubi_cacheEntry ) + mangled_len + raw_len + 2;
-       new_entry = malloc( i );
-       if( !new_entry )
-               return;
-
-       /* Fill the new cache entry, and add it to the cache. */
-       s1 = (char *)(new_entry + 1);
-       s2 = (char *)&(s1[mangled_len + 1]);
-       safe_strcpy( s1, mangled_name, mangled_len );
-       safe_strcpy( s2, raw_name,     raw_len );
-       ubi_cachePut( mangled_cache, i, new_entry, s1 );
+       data_val.dptr = raw_name;
+       data_val.dsize = strlen(raw_name)+1;
+       if (tdb_store_bystring(tdb_mangled_cache, mangled_name_key, data_val, TDB_REPLACE) != 0) {
+               DEBUG(0,("cache_mangled_name: Error storing entry %s -> %s\n", mangled_name_key, raw_name));
+       } else {
+               DEBUG(5,("cache_mangled_name: Stored entry %s -> %s\n", mangled_name_key, raw_name));
+       }
 }
 
 /* ************************************************************************** **
  * Check for a name on the mangled name stack
  *
  *  Input:  s - Input *and* output string buffer.
- *
+ *         maxlen - space in i/o string buffer.
  *  Output: True if the name was found in the cache, else False.
  *
  *  Notes:  If a reverse map is found, the function will overwrite the string
@@ -566,28 +461,29 @@ static void cache_mangled_name( char *mangled_name, char *raw_name )
  * ************************************************************************** **
  */
 
-static BOOL check_cache( char *s )
+static BOOL check_cache( char *s, size_t maxlen, const struct share_params *p )
 {
-       ubi_cacheEntryPtr FoundPtr;
-       char             *ext_start = NULL;
-       char             *found_name;
-       char             *saved_ext = NULL;
+       TDB_DATA data_val;
+       char *ext_start = NULL;
+       char *saved_ext = NULL;
+
+       magic_char = lp_magicchar(p);
 
        /* If the cache isn't initialized, give up. */
-       if( !mc_initialized )
+       if( !tdb_mangled_cache )
                return( False );
 
-       FoundPtr = ubi_cacheGet( mangled_cache, (ubi_trItemPtr)s );
+       data_val = tdb_fetch_bystring(tdb_mangled_cache, s);
 
        /* If we didn't find the name *with* the extension, try without. */
-       if( !FoundPtr ) {
+       if(data_val.dptr == NULL || data_val.dsize == 0) {
                ext_start = strrchr( s, '.' );
                if( ext_start ) {
-                       if((saved_ext = strdup(ext_start)) == NULL)
+                       if((saved_ext = SMB_STRDUP(ext_start)) == NULL)
                                return False;
 
                        *ext_start = '\0';
-                       FoundPtr = ubi_cacheGet( mangled_cache, (ubi_trItemPtr)s );
+                       data_val = tdb_fetch_bystring(tdb_mangled_cache, s);
                        /* 
                         * At this point s is the name without the
                         * extension. We re-add the extension if saved_ext
@@ -597,26 +493,23 @@ static BOOL check_cache( char *s )
        }
 
        /* Okay, if we haven't found it we're done. */
-       if( !FoundPtr ) {
+       if(data_val.dptr == NULL || data_val.dsize == 0) {
                if(saved_ext) {
                        /* Replace the saved_ext as it was truncated. */
-                       (void)pstrcat( s, saved_ext );
+                       (void)safe_strcat( s, saved_ext, maxlen );
                        SAFE_FREE(saved_ext);
                }
                return( False );
        }
 
        /* If we *did* find it, we need to copy it into the string buffer. */
-       found_name = (char *)(FoundPtr + 1);
-       found_name += (strlen( found_name ) + 1);
-
-       (void)pstrcpy( s, found_name );
+       (void)safe_strcpy( s, data_val.dptr, maxlen );
        if( saved_ext ) {
                /* Replace the saved_ext as it was truncated. */
-               (void)pstrcat( s, saved_ext );
+               (void)safe_strcat( s, saved_ext, maxlen );
                SAFE_FREE(saved_ext);
        }
-
+       SAFE_FREE(data_val.dptr);
        return( True );
 }
 
@@ -670,7 +563,7 @@ static void to_8_3(char *s, int default_case)
        p = s;
 
        while( *p && baselen < 5 ) {
-               if (*p != '.') {
+               if (isbasechar(*p)) {
                        base[baselen++] = p[0];
                }
                p++;
@@ -714,9 +607,12 @@ static void to_8_3(char *s, int default_case)
  * ****************************************************************************
  */
 
-static void name_map(char *OutName, BOOL need83, BOOL cache83, int default_case)
+static void name_map(char *OutName, BOOL need83, BOOL cache83,
+                    int default_case, const struct share_params *p)
 {
        smb_ucs2_t *OutName_ucs2;
+       magic_char = lp_magicchar(p);
+
        DEBUG(5,("name_map( %s, need83 = %s, cache83 = %s)\n", OutName,
                 need83 ? "True" : "False", cache83 ? "True" : "False"));
        
@@ -734,7 +630,7 @@ static void name_map(char *OutName, BOOL need83, BOOL cache83, int default_case)
 
                /* mangle it into 8.3 */
                if (cache83)
-                       tmp = strdup(OutName);
+                       tmp = SMB_STRDUP(OutName);
 
                to_8_3(OutName, default_case);
 
@@ -753,9 +649,9 @@ static void name_map(char *OutName, BOOL need83, BOOL cache83, int default_case)
   to drop in an alternative mangling implementation
 */
 static struct mangle_fns mangle_fns = {
+       mangle_reset,
        is_mangled,
        is_8_3,
-       mangle_reset,
        check_cache,
        name_map
 };
@@ -765,5 +661,9 @@ struct mangle_fns *mangle_hash_init(void)
 {
        mangle_reset();
 
+       /* Create the in-memory tdb using our custom hash function. */
+       tdb_mangled_cache = tdb_open_ex("mangled_cache", 1031, TDB_INTERNAL,
+                               (O_RDWR|O_CREAT), 0644, NULL, fast_string_hash);
+
        return &mangle_fns;
 }