s3-epmap: add ncalrpc listener code
[nivanova/samba-autobuild/.git] / source3 / smbd / mangle_hash.c
index 1dc9c67dcc1a8202deca792caabb0b850c103840..13e797c0e546c5a4b8d2d13357659738063f5913 100644 (file)
@@ -21,6 +21,8 @@
 */
 
 #include "includes.h"
+#include "smbd/globals.h"
+#include "mangle.h"
 
 /* -------------------------------------------------------------------------- **
  * Other stuff...
  *
  */
 
-static char magic_char = '~';
-
-static const char basechars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-!@#$%";
+static const char basechars[43]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-!@#$%";
 #define MANGLE_BASE       (sizeof(basechars)/sizeof(char)-1)
 
-static unsigned char *chartest;
-
 #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)
@@ -281,6 +277,7 @@ static bool is_8_3(const char *fname, bool check_case, bool allow_wildcards,
        smb_ucs2_t *ucs2name;
        NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
        size_t size;
+       char magic_char;
 
        magic_char = lp_magicchar(p);
 
@@ -294,16 +291,15 @@ 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)) {
                return False;
@@ -336,6 +332,7 @@ static void init_chartest( void )
        chartest = SMB_MALLOC_ARRAY(unsigned char, 256);
 
        SMB_ASSERT(chartest != NULL);
+       memset(chartest, '\0', 256);
 
        for( s = (const unsigned char *)basechars; *s; s++ ) {
                chartest[*s] |= BASECHAR_MASK;
@@ -363,6 +360,7 @@ static void init_chartest( void )
 static bool is_mangled(const char *s, const struct share_params *p)
 {
        char *magic;
+       char magic_char;
 
        magic_char = lp_magicchar(p);
 
@@ -412,8 +410,8 @@ static void cache_mangled_name( const char mangled_name[13],
 {
        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 )
@@ -433,6 +431,13 @@ static void cache_mangled_name( const char mangled_name[13],
                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';
                }
        }
@@ -444,6 +449,10 @@ static void cache_mangled_name( const char mangled_name[13],
        } 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 = '.';
+       }
 }
 
 /* ************************************************************************** **
@@ -469,6 +478,7 @@ static bool lookup_name_from_8_3(TALLOC_CTX *ctx,
        TDB_DATA data_val;
        char *saved_ext = NULL;
        char *s = talloc_strdup(ctx, in);
+       char magic_char;
 
        magic_char = lp_magicchar(p);
 
@@ -526,7 +536,7 @@ static bool lookup_name_from_8_3(TALLOC_CTX *ctx,
  Do the actual mangling to 8.3 format.
 *****************************************************************************/
 
-static bool to_8_3(const char *in, char out[13], int default_case)
+static bool to_8_3(char magic_char, const char *in, char out[13], int default_case)
 {
        int csum;
        char *p;
@@ -604,15 +614,21 @@ static bool must_mangle(const char *name,
 {
        smb_ucs2_t *name_ucs2 = NULL;
        NTSTATUS status;
+       size_t converted_size;
+       char magic_char;
+
        magic_char = lp_magicchar(p);
 
-       if (push_ucs2_allocate(&name_ucs2, name) == (size_t)-1) {
-               DEBUG(0, ("push_ucs2_allocate failed!\n"));
+       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);
-       SAFE_FREE(name_ucs2);
-       return NT_STATUS_IS_OK(status);
+       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);
 }
 
 /*****************************************************************************
@@ -637,26 +653,29 @@ static bool hash_name_to_8_3(const char *in,
                        const struct share_params *p)
 {
        smb_ucs2_t *in_ucs2 = NULL;
+       size_t converted_size;
+       char magic_char;
+
        magic_char = lp_magicchar(p);
 
        DEBUG(5,("hash_name_to_8_3( %s, cache83 = %s)\n", in,
                 cache83 ? "True" : "False"));
 
-       if (push_ucs2_allocate(&in_ucs2, in) == (size_t)-1) {
-               DEBUG(0, ("push_ucs2_allocate failed!\n"));
+       if (!push_ucs2_talloc(NULL, &in_ucs2, in, &converted_size)) {
+               DEBUG(0, ("push_ucs2_talloc failed!\n"));
                return False;
        }
 
        /* 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))) {
-               SAFE_FREE(in_ucs2);
+               TALLOC_FREE(in_ucs2);
                safe_strcpy(out, in, 12);
                return True;
        }
 
-       SAFE_FREE(in_ucs2);
-       if (!to_8_3(in, out, default_case)) {
+       TALLOC_FREE(in_ucs2);
+       if (!to_8_3(magic_char, in, out, default_case)) {
                return False;
        }
 
@@ -670,7 +689,7 @@ static bool hash_name_to_8_3(const char *in,
   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,
@@ -680,7 +699,7 @@ static struct mangle_fns mangle_fns = {
 };
 
 /* return the methods for this mangling implementation */
-struct mangle_fns *mangle_hash_init(void)
+const struct mangle_fns *mangle_hash_init(void)
 {
        mangle_reset();
 
@@ -688,5 +707,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;
 }