Fix bug #7154 - mangling method = hash can crash storing a name not containing a '.'
[abartlet/samba.git/.git] / source3 / smbd / mangle_hash.c
index 4c02d9685fb7463a0efd5ce1a9626111bfb668be..1482d10b7a240388b8c1d739cda0d76c36cf1c07 100644 (file)
@@ -1,25 +1,27 @@
-/* 
+/*
    Unix SMB/CIFS implementation.
    Name mangling
    Copyright (C) Andrew Tridgell 1992-2002
    Copyright (C) Simo Sorce 2001
    Copyright (C) Andrew Bartlett 2002
-   
+   Copyright (C) Jeremy Allison 2007
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
+#include "smbd/globals.h"
 
 /* -------------------------------------------------------------------------- **
  * Other stuff...
  *
  */
 
-char magic_char = '~';
-
-static char basechars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-!@#$%";
+static const char basechars[43]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-!@#$%";
 #define MANGLE_BASE       (sizeof(basechars)/sizeof(char)-1)
 
-static unsigned char chartest[256]  = { 0 };
-static BOOL          ct_initialized = False;
-
 #define mangle(V) ((char)(basechars[(V) % MANGLE_BASE]))
 #define BASECHAR_MASK 0xf0
 #define isbasechar(C) ( (chartest[ ((C) & 0xff) ]) & BASECHAR_MASK )
 
-static TDB_CONTEXT *tdb_mangled_cache;
-
 /* -------------------------------------------------------------------- */
 
-static NTSTATUS has_valid_83_chars(const smb_ucs2_t *s, BOOL allow_wildcards)
+static NTSTATUS has_valid_83_chars(const smb_ucs2_t *s, bool allow_wildcards)
 {
        if (!*s) {
                return NT_STATUS_INVALID_PARAMETER;
@@ -87,7 +82,7 @@ static NTSTATUS has_valid_83_chars(const smb_ucs2_t *s, BOOL allow_wildcards)
        return NT_STATUS_OK;
 }
 
-static NTSTATUS has_illegal_chars(const smb_ucs2_t *s, BOOL allow_wildcards)
+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;
@@ -116,7 +111,7 @@ static NTSTATUS has_illegal_chars(const smb_ucs2_t *s, BOOL allow_wildcards)
  */
 
 static NTSTATUS mangle_get_prefix(const smb_ucs2_t *ucs2_string, smb_ucs2_t **prefix,
-               smb_ucs2_t **extension, BOOL allow_wildcards)
+               smb_ucs2_t **extension, bool allow_wildcards)
 {
        size_t ext_len;
        smb_ucs2_t *p;
@@ -154,7 +149,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)
+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;
@@ -233,7 +228,7 @@ static NTSTATUS is_valid_name(const smb_ucs2_t *fname, BOOL allow_wildcards, BOO
        return ret;
 }
 
-static NTSTATUS is_8_3_w(const smb_ucs2_t *fname, BOOL allow_wildcards)
+static NTSTATUS is_8_3_w(const smb_ucs2_t *fname, bool allow_wildcards)
 {
        smb_ucs2_t *pref = 0, *ext = 0;
        size_t plen;
@@ -244,14 +239,14 @@ static NTSTATUS is_8_3_w(const smb_ucs2_t *fname, BOOL allow_wildcards)
 
        if (strlen_w(fname) > 12)
                return NT_STATUS_UNSUCCESSFUL;
-       
+
        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;
 
@@ -274,13 +269,14 @@ 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;
+       char magic_char;
 
        magic_char = lp_magicchar(p);
 
@@ -293,27 +289,24 @@ static BOOL is_8_3(const char *fname, BOOL check_case, BOOL allow_wildcards,
 
        if (strlen(f) > 12)
                return False;
-       
-       size = push_ucs2_allocate(&ucs2name, f);
-       if (size == (size_t)-1) {
-               DEBUG(0,("is_8_3: internal error push_ucs2_allocate() failed!\n"));
+
+       if (!push_ucs2_talloc(NULL, &ucs2name, f, &size)) {
+               DEBUG(0,("is_8_3: internal error push_ucs2_talloc() failed!\n"));
                goto done;
        }
 
        ret = is_8_3_w(ucs2name, allow_wildcards);
 
 done:
-       SAFE_FREE(ucs2name);
+       TALLOC_FREE(ucs2name);
 
-       if (!NT_STATUS_IS_OK(ret)) { 
+       if (!NT_STATUS_IS_OK(ret)) {
                return False;
        }
-       
+
        return True;
 }
 
-
-
 /* -------------------------------------------------------------------------- **
  * Functions...
  */
@@ -330,17 +323,18 @@ done:
  *
  * ************************************************************************** **
  */
+
 static void init_chartest( void )
 {
        const unsigned char *s;
-  
-       memset( (char *)chartest, '\0', 256 );
+
+       chartest = SMB_MALLOC_ARRAY(unsigned char, 256);
+
+       SMB_ASSERT(chartest != NULL);
 
        for( s = (const unsigned char *)basechars; *s; s++ ) {
                chartest[*s] |= BASECHAR_MASK;
        }
-
-       ct_initialized = True;
 }
 
 /* ************************************************************************** **
@@ -360,14 +354,17 @@ static void init_chartest( void )
  *
  * ************************************************************************** **
  */
-static BOOL is_mangled(const char *s, const struct share_params *p)
+
+static bool is_mangled(const char *s, const struct share_params *p)
 {
        char *magic;
+       char magic_char;
 
        magic_char = lp_magicchar(p);
 
-       if( !ct_initialized )
+       if (chartest == NULL) {
                init_chartest();
+       }
 
        magic = strchr_m( s, magic_char );
        while( magic && magic[1] && magic[2] ) {         /* 3 chars, 1st is magic. */
@@ -406,12 +403,13 @@ static void mangle_reset( void )
   crh 07-Apr-1998
 **************************************************************************/
 
-static void cache_mangled_name( const char mangled_name[13], char *raw_name )
+static void cache_mangled_name( const char mangled_name[13],
+                               const char *raw_name )
 {
        TDB_DATA data_val;
        char mangled_name_key[13];
-       char *s1;
-       char *s2;
+       char *s1 = NULL;
+       char *s2 = NULL;
 
        /* If the cache isn't initialized, give up. */
        if( !tdb_mangled_cache )
@@ -431,6 +429,13 @@ static void cache_mangled_name( const char mangled_name[13], char *raw_name )
                if( !s1[i] && !s2[i] ) {
                        /* Truncate at the '.' */
                        *s1 = '\0';
+                       /*
+                        * DANGER WILL ROBINSON - this
+                        * is changing a const string via
+                        * an aliased pointer ! Remember to
+                        * put it back once we've used it.
+                        * JRA
+                        */
                        *s2 = '\0';
                }
        }
@@ -442,6 +447,10 @@ static void cache_mangled_name( const char mangled_name[13], char *raw_name )
        } else {
                DEBUG(5,("cache_mangled_name: Stored entry %s -> %s\n", mangled_name_key, raw_name));
        }
+       /* Restore the change we made to the const string. */
+       if (s2) {
+               *s2 = '.';
+       }
 }
 
 /* ************************************************************************** **
@@ -459,30 +468,38 @@ static void cache_mangled_name( const char mangled_name[13], char *raw_name )
  * ************************************************************************** **
  */
 
-static BOOL check_cache( char *s, size_t maxlen, const struct share_params *p )
+static bool lookup_name_from_8_3(TALLOC_CTX *ctx,
+                               const char *in,
+                               char **out, /* talloced on the given context. */
+                               const struct share_params *p)
 {
        TDB_DATA data_val;
-       char *ext_start = NULL;
        char *saved_ext = NULL;
+       char *s = talloc_strdup(ctx, in);
+       char magic_char;
 
        magic_char = lp_magicchar(p);
 
        /* If the cache isn't initialized, give up. */
-       if( !tdb_mangled_cache )
-               return( False );
+       if(!s || !tdb_mangled_cache ) {
+               TALLOC_FREE(s);
+               return False;
+       }
 
        data_val = tdb_fetch_bystring(tdb_mangled_cache, s);
 
        /* If we didn't find the name *with* the extension, try without. */
        if(data_val.dptr == NULL || data_val.dsize == 0) {
-               ext_start = strrchr( s, '.' );
+               char *ext_start = strrchr( s, '.' );
                if( ext_start ) {
-                       if((saved_ext = SMB_STRDUP(ext_start)) == NULL)
+                       if((saved_ext = talloc_strdup(ctx,ext_start)) == NULL) {
+                               TALLOC_FREE(s);
                                return False;
+                       }
 
                        *ext_start = '\0';
                        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
                         * is not null, before freeing saved_ext.
@@ -492,31 +509,32 @@ static BOOL check_cache( char *s, size_t maxlen, const struct share_params *p )
 
        /* Okay, if we haven't found it we're done. */
        if(data_val.dptr == NULL || data_val.dsize == 0) {
-               if(saved_ext) {
-                       /* Replace the saved_ext as it was truncated. */
-                       (void)safe_strcat( s, saved_ext, maxlen );
-                       SAFE_FREE(saved_ext);
-               }
-               return( False );
+               TALLOC_FREE(saved_ext);
+               TALLOC_FREE(s);
+               return False;
        }
 
-       /* If we *did* find it, we need to copy it into the string buffer. */
-       (void)safe_strcpy( s, (const char *)data_val.dptr, maxlen );
-       if( saved_ext ) {
-               /* Replace the saved_ext as it was truncated. */
-               (void)safe_strcat( s, saved_ext, maxlen );
-               SAFE_FREE(saved_ext);
+       /* If we *did* find it, we need to talloc it on the given ctx. */
+       if (saved_ext) {
+               *out = talloc_asprintf(ctx, "%s%s",
+                                       (char *)data_val.dptr,
+                                       saved_ext);
+       } else {
+               *out = talloc_strdup(ctx, (char *)data_val.dptr);
        }
+
+       TALLOC_FREE(s);
+       TALLOC_FREE(saved_ext);
        SAFE_FREE(data_val.dptr);
-       return( True );
+
+       return *out ? True : False;
 }
 
 /*****************************************************************************
- * do the actual mangling to 8.3 format
- * the buffer must be able to hold 13 characters (including the null)
- *****************************************************************************
- */
-static void to_8_3(char *s, int default_case)
+ Do the actual mangling to 8.3 format.
+*****************************************************************************/
+
+static bool to_8_3(char magic_char, const char *in, char out[13], int default_case)
 {
        int csum;
        char *p;
@@ -524,13 +542,18 @@ static void to_8_3(char *s, int default_case)
        char base[9];
        int baselen = 0;
        int extlen = 0;
+       char *s = SMB_STRDUP(in);
 
        extension[0] = 0;
        base[0] = 0;
 
-       p = strrchr(s,'.');  
+       if (!s) {
+               return False;
+       }
+
+       p = strrchr(s,'.');
        if( p && (strlen(p+1) < (size_t)4) ) {
-               BOOL all_normal = ( strisnormal(p+1, default_case) ); /* XXXXXXXXX */
+               bool all_normal = ( strisnormal(p+1, default_case) ); /* XXXXXXXXX */
 
                if( all_normal && p[1] != 0 ) {
                        *p = 0;
@@ -557,7 +580,7 @@ static void to_8_3(char *s, int default_case)
                        extension[extlen] = 0;
                }
        }
-  
+
        p = s;
 
        while( *p && baselen < 5 ) {
@@ -567,95 +590,114 @@ static void to_8_3(char *s, int default_case)
                p++;
        }
        base[baselen] = 0;
-  
+
        csum = csum % (MANGLE_BASE*MANGLE_BASE);
-  
-       (void)slprintf(s, 12, "%s%c%c%c",
-               base, magic_char, mangle( csum/MANGLE_BASE ), mangle( csum ) );
-  
+
+       memcpy(out, base, baselen);
+       out[baselen] = magic_char;
+       out[baselen+1] = mangle( csum/MANGLE_BASE );
+       out[baselen+2] = mangle( csum );
+
        if( *extension ) {
-               (void)pstrcat( s, "." );
-               (void)pstrcat( s, extension );
+               out[baselen+3] = '.';
+               safe_strcpy(&out[baselen+4], extension, 3);
        }
+
+       SAFE_FREE(s);
+       return True;
+}
+
+static bool must_mangle(const char *name,
+                       const struct share_params *p)
+{
+       smb_ucs2_t *name_ucs2 = NULL;
+       NTSTATUS status;
+       size_t converted_size;
+       char magic_char;
+
+       magic_char = lp_magicchar(p);
+
+       if (!push_ucs2_talloc(NULL, &name_ucs2, name, &converted_size)) {
+               DEBUG(0, ("push_ucs2_talloc failed!\n"));
+               return False;
+       }
+       status = is_valid_name(name_ucs2, False, False);
+       TALLOC_FREE(name_ucs2);
+       /* We return true if we *must* mangle, so if it's
+        * a valid name (status == OK) then we must return
+        * false. Bug #6939. */
+       return !NT_STATUS_IS_OK(status);
 }
 
 /*****************************************************************************
  * Convert a filename to DOS format.  Return True if successful.
+ *  Input:  in        Incoming name.
  *
- *  Input:  OutName - Source *and* destination buffer. 
- *
- *                    NOTE that OutName must point to a memory space that
- *                    is at least 13 bytes in size!
+ *          out       8.3 DOS name.
  *
- *          need83  - If False, name mangling will be skipped unless the
- *                    name contains illegal characters.  Mapping will still
- *                    be done, if appropriate.  This is probably used to
- *                    signal that a client does not require name mangling,
- *                    thus skipping the name mangling even on shares which
- *                    have name-mangling turned on.
  *          cache83 - If False, the mangled name cache will not be updated.
  *                    This is usually used to prevent that we overwrite
  *                    a conflicting cache entry prematurely, i.e. before
  *                    we know whether the client is really interested in the
  *                    current name.  (See PR#13758).  UKD.
  *
- *  Output: Returns False only if the name wanted mangling but the share does
- *          not have name mangling turned on.
- *
  * ****************************************************************************
  */
 
-static void name_map(char *OutName, BOOL need83, BOOL cache83,
-                    int default_case, const struct share_params *p)
+static bool hash_name_to_8_3(const char *in,
+                       char out[13],
+                       bool cache83,
+                       int default_case,
+                       const struct share_params *p)
 {
-       smb_ucs2_t *OutName_ucs2;
-       magic_char = lp_magicchar(p);
+       smb_ucs2_t *in_ucs2 = NULL;
+       size_t converted_size;
+       char magic_char;
 
-       DEBUG(5,("name_map( %s, need83 = %s, cache83 = %s)\n", OutName,
-                need83 ? "True" : "False", cache83 ? "True" : "False"));
-       
-       if (push_ucs2_allocate(&OutName_ucs2, OutName) == (size_t)-1) {
-               DEBUG(0, ("push_ucs2_allocate failed!\n"));
-               return;
-       }
-
-       if( !need83 && !NT_STATUS_IS_OK(is_valid_name(OutName_ucs2, False, False)))
-               need83 = True;
+       magic_char = lp_magicchar(p);
 
-       /* check if it's already in 8.3 format */
-       if (need83 && !NT_STATUS_IS_OK(is_8_3_w(OutName_ucs2, False))) {
-               char *tmp = NULL; 
+       DEBUG(5,("hash_name_to_8_3( %s, cache83 = %s)\n", in,
+                cache83 ? "True" : "False"));
 
-               /* mangle it into 8.3 */
-               if (cache83)
-                       tmp = SMB_STRDUP(OutName);
+       if (!push_ucs2_talloc(NULL, &in_ucs2, in, &converted_size)) {
+               DEBUG(0, ("push_ucs2_talloc failed!\n"));
+               return False;
+       }
 
-               to_8_3(OutName, default_case);
+       /* If it's already 8.3, just copy. */
+       if (NT_STATUS_IS_OK(is_valid_name(in_ucs2, False, False)) &&
+                               NT_STATUS_IS_OK(is_8_3_w(in_ucs2, False))) {
+               TALLOC_FREE(in_ucs2);
+               safe_strcpy(out, in, 12);
+               return True;
+       }
 
-               if(tmp != NULL) {
-                       cache_mangled_name(OutName, tmp);
-                       SAFE_FREE(tmp);
-               }
+       TALLOC_FREE(in_ucs2);
+       if (!to_8_3(magic_char, in, out, default_case)) {
+               return False;
        }
 
-       DEBUG(5,("name_map() ==> [%s]\n", OutName));
-       SAFE_FREE(OutName_ucs2);
+       cache_mangled_name(out, in);
+
+       DEBUG(5,("hash_name_to_8_3(%s) ==> [%s]\n", in, out));
+       return True;
 }
 
 /*
   the following provides the abstraction layer to make it easier
   to drop in an alternative mangling implementation
 */
-static struct mangle_fns mangle_fns = {
+static const struct mangle_fns mangle_hash_fns = {
        mangle_reset,
        is_mangled,
+       must_mangle,
        is_8_3,
-       check_cache,
-       name_map
+       lookup_name_from_8_3,
+       hash_name_to_8_3
 };
 
 /* return the methods for this mangling implementation */
-struct mangle_fns *mangle_hash_init(void)
+const struct mangle_fns *mangle_hash_init(void)
 {
        mangle_reset();
 
@@ -663,5 +705,5 @@ struct mangle_fns *mangle_hash_init(void)
        tdb_mangled_cache = tdb_open_ex("mangled_cache", 1031, TDB_INTERNAL,
                                (O_RDWR|O_CREAT), 0644, NULL, fast_string_hash);
 
-       return &mangle_fns;
+       return &mangle_hash_fns;
 }