r5100: We should only care about case-sensitivity when *reading* an incoming
[ira/wip.git] / source / lib / util.c
index bdf67a515aed8b7cd596796feeed952776e6a4f1..42ead313a9206c2046630f5b7836ad3bb2195133 100644 (file)
@@ -4,7 +4,7 @@
    Copyright (C) Andrew Tridgell 1992-1998
    Copyright (C) Jeremy Allison 2001-2002
    Copyright (C) Simo Sorce 2001
-   Copyright (C) Anthony Liguori 2003
+   Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
    
    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
@@ -23,6 +23,9 @@
 
 #include "includes.h"
 
+/* Max allowable allococation - 256mb - 0x10000000 */
+#define MAX_ALLOC_SIZE (1024*1024*256)
+
 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
 #ifdef WITH_NISPLUS_HOME
 #ifdef BROKEN_NISPLUS_INCLUDE_FILES
 
 #include <rpcsvc/nis.h>
 
-#else /* !WITH_NISPLUS_HOME */
-
-#include "rpcsvc/ypclnt.h"
-
 #endif /* WITH_NISPLUS_HOME */
 #endif /* HAVE_NETGROUP && WITH_AUTOMOUNT */
 
-int Protocol = PROTOCOL_COREPLUS;
+enum protocol_types Protocol = PROTOCOL_COREPLUS;
 
 /* a default finfo structure to ensure all fields are sensible */
 file_info def_finfo = {-1,0,0,0,0,0,0,"",""};
@@ -63,19 +62,6 @@ int chain_size = 0;
 
 int trans_num = 0;
 
-/*
-   case handling on filenames 
-*/
-int case_default = CASE_LOWER;
-
-/* the following control case operations - they are put here so the
-   client can link easily */
-BOOL case_sensitive;
-BOOL case_preserve;
-BOOL use_mangled_map = False;
-BOOL short_case_preserve;
-BOOL case_mangle;
-
 static enum remote_arch_types ra_type = RA_UNKNOWN;
 pstring user_socket_options=DEFAULT_SOCKET_OPTIONS;   
 
@@ -96,10 +82,10 @@ static char **smb_my_netbios_names;
 BOOL set_global_myname(const char *myname)
 {
        SAFE_FREE(smb_myname);
-       smb_myname = strdup(myname);
+       smb_myname = SMB_STRDUP(myname);
        if (!smb_myname)
                return False;
-       strupper(smb_myname);
+       strupper_m(smb_myname);
        return True;
 }
 
@@ -115,10 +101,10 @@ const char *global_myname(void)
 BOOL set_global_myworkgroup(const char *myworkgroup)
 {
        SAFE_FREE(smb_myworkgroup);
-       smb_myworkgroup = strdup(myworkgroup);
+       smb_myworkgroup = SMB_STRDUP(myworkgroup);
        if (!smb_myworkgroup)
                return False;
-       strupper(smb_myworkgroup);
+       strupper_m(smb_myworkgroup);
        return True;
 }
 
@@ -134,10 +120,10 @@ const char *lp_workgroup(void)
 BOOL set_global_scope(const char *scope)
 {
        SAFE_FREE(smb_scope);
-       smb_scope = strdup(scope);
+       smb_scope = SMB_STRDUP(scope);
        if (!smb_scope)
                return False;
-       strupper(smb_scope);
+       strupper_m(smb_scope);
        return True;
 }
 
@@ -168,7 +154,7 @@ static BOOL allocate_my_netbios_names_array(size_t number)
        free_netbios_names_array();
 
        smb_num_netbios_names = number + 1;
-       smb_my_netbios_names = (char **)malloc( sizeof(char *) * smb_num_netbios_names );
+       smb_my_netbios_names = SMB_MALLOC_ARRAY( char *, smb_num_netbios_names );
 
        if (!smb_my_netbios_names)
                return False;
@@ -181,10 +167,10 @@ static BOOL set_my_netbios_names(const char *name, int i)
 {
        SAFE_FREE(smb_my_netbios_names[i]);
 
-       smb_my_netbios_names[i] = strdup(name);
+       smb_my_netbios_names[i] = SMB_STRDUP(name);
        if (!smb_my_netbios_names[i])
                return False;
-       strupper(smb_my_netbios_names[i]);
+       strupper_m(smb_my_netbios_names[i]);
        return True;
 }
 
@@ -261,11 +247,11 @@ BOOL init_names(void)
        }                       
 
        fstrcpy( local_machine, global_myname() );
-       trim_string( local_machine, " ", " " );
+       trim_char( local_machine, ' ', ' ' );
        p = strchr( local_machine, ' ' );
        if (p)
                *p = 0;
-       strlower( local_machine );
+       strlower_m( local_machine );
 
        DEBUG( 5, ("Netbios name list:-\n") );
        for( n=0; my_netbios_names(n); n++ )
@@ -305,13 +291,35 @@ BOOL in_group(gid_t group, gid_t current_gid, int ngroups, const gid_t *groups)
        return(False);
 }
 
+/****************************************************************************
+ Add a gid to an array of gids if it's not already there.
+****************************************************************************/
+
+void add_gid_to_array_unique(gid_t gid, gid_t **gids, int *num)
+{
+       int i;
+
+       for (i=0; i<*num; i++) {
+               if ((*gids)[i] == gid)
+                       return;
+       }
+       
+       *gids = SMB_REALLOC_ARRAY(*gids, gid_t, *num+1);
+
+       if (*gids == NULL)
+               return;
+
+       (*gids)[*num] = gid;
+       *num += 1;
+}
+
 /****************************************************************************
  Like atoi but gets the value up to the separator character.
 ****************************************************************************/
 
 static const char *Atoic(const char *p, int *n, const char *c)
 {
-       if (!isdigit((const int)*p)) {
+       if (!isdigit((int)*p)) {
                DEBUG(5, ("Atoic: malformed number\n"));
                return NULL;
        }
@@ -346,7 +354,7 @@ const char *get_numlist(const char *p, uint32 **num, int *count)
        while ((p = Atoic(p, &val, ":,")) != NULL && (*p) != ':') {
                uint32 *tn;
                
-               tn = Realloc((*num), ((*count)+1) * sizeof(uint32));
+               tn = SMB_REALLOC_ARRAY((*num), uint32, (*count)+1);
                if (tn == NULL) {
                        SAFE_FREE(*num);
                        return NULL;
@@ -551,7 +559,7 @@ void dos_clean_name(char *s)
        /* remove any double slashes */
        all_string_sub(s, "\\\\", "\\", 0);
 
-       while ((p = strstr(s,"\\..\\")) != NULL) {
+       while ((p = strstr_m(s,"\\..\\")) != NULL) {
                pstring s1;
 
                *p = 0;
@@ -589,7 +597,7 @@ void unix_clean_name(char *s)
                        pstrcpy(s,"./");
        }
 
-       while ((p = strstr(s,"/../")) != NULL) {
+       while ((p = strstr_m(s,"/../")) != NULL) {
                pstring s1;
 
                *p = 0;
@@ -605,68 +613,6 @@ void unix_clean_name(char *s)
        trim_string(s,NULL,"/..");
 }
 
-/*******************************************************************
- Convert '\' to '/'.
- Reduce a file name, removing or reducing /../ , /./ , // elements.
- Remove also any trailing . and /
- Return a new allocated string.
-********************************************************************/
-
-smb_ucs2_t *unix_clean_path(const smb_ucs2_t *s)
-{
-       smb_ucs2_t *ns;
-       smb_ucs2_t *p, *r, *t;
-
-       DEBUG(3, ("unix_clean_path\n")); /*  [%unicode]\n")); */
-       if(!s)
-               return NULL;
-
-       /* convert '\' to '/' */
-       ns = strdup_w(s);
-       if (!ns)
-               return NULL;
-       unix_format_w(ns);
-
-       /* remove all double slashes */
-       p = ns;
-       ns = all_string_sub_wa(p, "//", "/");
-       SAFE_FREE(p);
-       if (!ns)
-               return NULL;
-
-       /* remove any /./ */
-       p = ns;
-       ns = all_string_sub_wa(p, "/./", "/");
-       SAFE_FREE(p);
-       if (!ns)
-               return NULL;
-
-       /* reduce any /../ */
-       t = ns;
-       while (*t && (r = strstr_wa(t, "/.."))) {
-               t = &(r[3]);
-               if (*t == UCS2_CHAR('/') || *t == 0) {
-                       *r = 0;
-                       p = strrchr_w(ns, UCS2_CHAR('/'));
-                       if (!p)
-                               p = ns;
-                       if (*t == 0)
-                               *p = 0;
-                       else
-                               memmove(p, t, (strlen_w(t) + 1) * sizeof(smb_ucs2_t));
-                       t = p;
-               }
-       }
-
-       /* remove any leading ./ trailing /. */
-       trim_string_wa(ns, "./", "/.");
-
-       /* remove any leading and trailing / */
-       trim_string_wa(ns, "/", "/");
-
-       return ns;
-}
-
 /****************************************************************************
  Make a dir struct.
 ****************************************************************************/
@@ -695,7 +641,7 @@ void make_dir_struct(char *buf, const char *mask, const char *fname,SMB_OFF_T si
        put_dos_date(buf,22,date);
        SSVAL(buf,26,size & 0xFFFF);
        SSVAL(buf,28,(size >> 16)&0xFFFF);
-       push_ascii(buf+30,fname,12, case_sensitive ? 0 : STR_UPPER);
+       push_ascii(buf+30,fname,12,0);
        DEBUG(8,("put name [%s] from [%s] into dir struct\n",buf+30, fname));
 }
 
@@ -784,7 +730,7 @@ ssize_t transfer_file_internal(int infd, int outfd, size_t n, ssize_t (*read_fn)
        size_t num_to_read_thistime;
        size_t num_written = 0;
 
-       if ((buf = malloc(TRANSFER_BUF_SIZE)) == NULL)
+       if ((buf = SMB_MALLOC(TRANSFER_BUF_SIZE)) == NULL)
                return -1;
 
        while (total < n) {
@@ -831,14 +777,26 @@ SMB_OFF_T transfer_file(int infd,int outfd,SMB_OFF_T n)
  Sleep for a specified number of milliseconds.
 ********************************************************************/
 
-void msleep(unsigned int t)
+void smb_msleep(unsigned int t)
 {
+#if defined(HAVE_NANOSLEEP)
+       struct timespec tval;
+       int ret;
+
+       tval.tv_sec = t/1000;
+       tval.tv_nsec = 1000000*(t%1000);
+
+       do {
+               errno = 0;
+               ret = nanosleep(&tval, &tval);
+       } while (ret < 0 && errno == EINTR && (tval.tv_sec > 0 || tval.tv_nsec > 0));
+#else
        unsigned int tdiff=0;
        struct timeval tval,t1,t2;  
        fd_set fds;
 
        GetTimeOfDay(&t1);
-       GetTimeOfDay(&t2);
+       t2 = t1;
   
        while (tdiff < t) {
                tval.tv_sec = (t-tdiff)/1000;
@@ -862,6 +820,7 @@ void msleep(unsigned int t)
 
                tdiff = TvalDiff(&t1,&t2);
        }
+#endif
 }
 
 /****************************************************************************
@@ -912,6 +871,76 @@ BOOL yesno(char *p)
        return(False);
 }
 
+#if defined(PARANOID_MALLOC_CHECKER)
+
+/****************************************************************************
+ Internal malloc wrapper. Externally visible.
+****************************************************************************/
+
+void *malloc_(size_t size)
+{
+#undef malloc
+       return malloc(size);
+#define malloc(s) __ERROR_DONT_USE_MALLOC_DIRECTLY
+}
+
+/****************************************************************************
+ Internal calloc wrapper. Not externally visible.
+****************************************************************************/
+
+static void *calloc_(size_t count, size_t size)
+{
+#undef calloc
+       return calloc(count, size);
+#define calloc(n,s) __ERROR_DONT_USE_CALLOC_DIRECTLY
+}
+
+/****************************************************************************
+ Internal realloc wrapper. Not externally visible.
+****************************************************************************/
+
+static void *realloc_(void *ptr, size_t size)
+{
+#undef realloc
+       return realloc(ptr, size);
+#define realloc(p,s) __ERROR_DONT_USE_RELLOC_DIRECTLY
+}
+
+#endif /* PARANOID_MALLOC_CHECKER */
+
+/****************************************************************************
+ Type-safe malloc.
+****************************************************************************/
+
+void *malloc_array(size_t el_size, unsigned int count)
+{
+       if (count >= MAX_ALLOC_SIZE/el_size) {
+               return NULL;
+       }
+
+#if defined(PARANOID_MALLOC_CHECKER)
+       return malloc_(el_size*count);
+#else
+       return malloc(el_size*count);
+#endif
+}
+
+/****************************************************************************
+ Type-safe calloc.
+****************************************************************************/
+
+void *calloc_array(size_t size, size_t nmemb)
+{
+       if (nmemb >= MAX_ALLOC_SIZE/size) {
+               return NULL;
+       }
+#if defined(PARANOID_MALLOC_CHECKER)
+       return calloc_(nmemb, size);
+#else
+       return calloc(nmemb, size);
+#endif
+}
+
 /****************************************************************************
  Expand a pointer to be a particular size.
 ****************************************************************************/
@@ -926,10 +955,17 @@ void *Realloc(void *p,size_t size)
                return NULL;
        }
 
+#if defined(PARANOID_MALLOC_CHECKER)
+       if (!p)
+               ret = (void *)malloc_(size);
+       else
+               ret = (void *)realloc_(p,size);
+#else
        if (!p)
                ret = (void *)malloc(size);
        else
                ret = (void *)realloc(p,size);
+#endif
 
        if (!ret)
                DEBUG(0,("Memory allocation error: failed to expand to %d bytes\n",(int)size));
@@ -937,6 +973,18 @@ void *Realloc(void *p,size_t size)
        return(ret);
 }
 
+/****************************************************************************
+ Type-safe realloc.
+****************************************************************************/
+
+void *realloc_array(void *p,size_t el_size, unsigned int count)
+{
+       if (count >= MAX_ALLOC_SIZE/el_size) {
+               return NULL;
+       }
+       return Realloc(p,el_size*count);
+}
+
 /****************************************************************************
  Free memory, checks for NULL.
  Use directly SAFE_FREE()
@@ -981,26 +1029,33 @@ BOOL get_myname(char *my_name)
 }
 
 /****************************************************************************
- Get my own name, including domain.
+ Get my own canonical name, including domain.
 ****************************************************************************/
 
-BOOL get_myfullname(char *my_name)
+BOOL get_mydnsfullname(fstring my_dnsname)
 {
-       pstring hostname;
-
-       *hostname = 0;
+       static fstring dnshostname;
+       struct hostent *hp;
 
-       /* get my host name */
-       if (gethostname(hostname, sizeof(hostname)) == -1) {
-               DEBUG(0,("gethostname failed\n"));
-               return False;
-       } 
+       if (!*dnshostname) {
+               /* get my host name */
+               if (gethostname(dnshostname, sizeof(dnshostname)) == -1) {
+                       *dnshostname = '\0';
+                       DEBUG(0,("gethostname failed\n"));
+                       return False;
+               } 
 
-       /* Ensure null termination. */
-       hostname[sizeof(hostname)-1] = '\0';
+               /* Ensure null termination. */
+               dnshostname[sizeof(dnshostname)-1] = '\0';
 
-       if (my_name)
-               fstrcpy(my_name, hostname);
+               /* Ensure we get the cannonical name. */
+               if (!(hp = sys_gethostbyname(dnshostname))) {
+                       *dnshostname = '\0';
+                       return False;
+               }
+               fstrcpy(dnshostname, hp->h_name);
+       }
+       fstrcpy(my_dnsname, dnshostname);
        return True;
 }
 
@@ -1008,39 +1063,29 @@ BOOL get_myfullname(char *my_name)
  Get my own domain name.
 ****************************************************************************/
 
-BOOL get_mydomname(fstring my_domname)
+BOOL get_mydnsdomname(fstring my_domname)
 {
-       pstring hostname;
+       fstring domname;
        char *p;
 
-       *hostname = 0;
-       /* get my host name */
-       if (gethostname(hostname, sizeof(hostname)) == -1) {
-               DEBUG(0,("gethostname failed\n"));
-               return False;
-       } 
-
-       /* Ensure null termination. */
-       hostname[sizeof(hostname)-1] = '\0';
-
-       p = strchr_m(hostname, '.');
-
-       if (!p)
+       *my_domname = '\0';
+       if (!get_mydnsfullname(domname)) {
                return False;
-
-       p++;
-       
-       if (my_domname)
+       }       
+       p = strchr_m(domname, '.');
+       if (p) {
+               p++;
                fstrcpy(my_domname, p);
+       }
 
-       return True;
+       return False;
 }
 
 /****************************************************************************
  Interpret a protocol description string, with a default.
 ****************************************************************************/
 
-int interpret_protocol(char *str,int def)
+int interpret_protocol(const char *str,int def)
 {
        if (strequal(str,"NT1"))
                return(PROTOCOL_NT1);
@@ -1188,7 +1233,7 @@ static void strip_mount_options( pstring *str)
 *******************************************************************/
 
 #ifdef WITH_NISPLUS_HOME
-char *automount_lookup(const char *user_name)
+char *automount_lookup( char *user_name)
 {
        static fstring last_key = "";
        static pstring last_value = "";
@@ -1231,7 +1276,7 @@ char *automount_lookup(const char *user_name)
 }
 #else /* WITH_NISPLUS_HOME */
 
-char *automount_lookup(const char *user_name)
+char *automount_lookup( char *user_name)
 {
        static fstring last_key = "";
        static pstring last_value = "";
@@ -1352,7 +1397,7 @@ char *gidtoname(gid_t gid)
  Convert a user name into a uid. 
 ********************************************************************/
 
-uid_t nametouid(char *name)
+uid_t nametouid(const char *name)
 {
        struct passwd *pass;
        char *p;
@@ -1392,34 +1437,133 @@ gid_t nametogid(const char *name)
        return (gid_t)-1;
 }
 
+/*******************************************************************
+ legacy wrapper for smb_panic2()
+********************************************************************/
+void smb_panic( const char *why )
+{
+       smb_panic2( why, True );
+}
+
 /*******************************************************************
  Something really nasty happened - panic !
 ********************************************************************/
 
-void smb_panic(const char *why)
+#ifdef HAVE_LIBEXC_H
+#include <libexc.h>
+#endif
+
+void smb_panic2(const char *why, BOOL decrement_pid_count )
 {
-       char *cmd = lp_panic_action();
+       char *cmd;
        int result;
+#ifdef HAVE_BACKTRACE_SYMBOLS
+       void *backtrace_stack[BACKTRACE_STACK_SIZE];
+       size_t backtrace_size;
+       char **backtrace_strings;
+#endif
 
+#ifdef DEVELOPER
+       {
+               extern char *global_clobber_region_function;
+               extern unsigned int global_clobber_region_line;
+
+               if (global_clobber_region_function) {
+                       DEBUG(0,("smb_panic: clobber_region() last called from [%s(%u)]\n",
+                                        global_clobber_region_function,
+                                        global_clobber_region_line));
+               } 
+       }
+#endif
+
+       /* only smbd needs to decrement the smbd counter in connections.tdb */
+       if ( decrement_pid_count )
+               decrement_smbd_process_count();
+
+       cmd = lp_panic_action();
        if (cmd && *cmd) {
                DEBUG(0, ("smb_panic(): calling panic action [%s]\n", cmd));
                result = system(cmd);
 
                if (result == -1)
                        DEBUG(0, ("smb_panic(): fork failed in panic action: %s\n",
-                                 strerror(errno)));
+                                         strerror(errno)));
                else
                        DEBUG(0, ("smb_panic(): action returned status %d\n",
-                                 WEXITSTATUS(result)));
+                                         WEXITSTATUS(result)));
        }
        DEBUG(0,("PANIC: %s\n", why));
+
+#ifdef HAVE_BACKTRACE_SYMBOLS
+       /* get the backtrace (stack frames) */
+       backtrace_size = backtrace(backtrace_stack,BACKTRACE_STACK_SIZE);
+       backtrace_strings = backtrace_symbols(backtrace_stack, backtrace_size);
+
+       DEBUG(0, ("BACKTRACE: %lu stack frames:\n", 
+                 (unsigned long)backtrace_size));
+       
+       if (backtrace_strings) {
+               int i;
+
+               for (i = 0; i < backtrace_size; i++)
+                       DEBUGADD(0, (" #%u %s\n", i, backtrace_strings[i]));
+
+               /* Leak the backtrace_strings, rather than risk what free() might do */
+       }
+
+#elif HAVE_LIBEXC
+
+#define NAMESIZE 32 /* Arbitrary */
+
+       /* The IRIX libexc library provides an API for unwinding the stack. See
+        * libexc(3) for details. Apparantly trace_back_stack leaks memory, but
+        * since we are about to abort anyway, it hardly matters.
+        *
+        * Note that if we paniced due to a SIGSEGV or SIGBUS (or similar) this
+        * will fail with a nasty message upon failing to open the /proc entry.
+        */
+       {
+               __uint64_t      addrs[BACKTRACE_STACK_SIZE];
+               char *          names[BACKTRACE_STACK_SIZE];
+               char            namebuf[BACKTRACE_STACK_SIZE * NAMESIZE];
+
+               int             i;
+               int             levels;
+
+               ZERO_ARRAY(addrs);
+               ZERO_ARRAY(names);
+               ZERO_ARRAY(namebuf);
+
+               /* We need to be root so we can open our /proc entry to walk
+                * our stack. It also helps when we want to dump core.
+                */
+               become_root();
+
+               for (i = 0; i < BACKTRACE_STACK_SIZE; i++) {
+                       names[i] = namebuf + (i * NAMESIZE);
+               }
+
+               levels = trace_back_stack(0, addrs, names,
+                               BACKTRACE_STACK_SIZE, NAMESIZE);
+
+               DEBUG(0, ("BACKTRACE: %d stack frames:\n", levels));
+               for (i = 0; i < levels; i++) {
+                       DEBUGADD(0, (" #%d 0x%llx %s\n", i, addrs[i], names[i]));
+               }
+     }
+#undef NAMESIZE
+#endif
+
        dbgflush();
+#ifdef SIGABRT
+       CatchSignal(SIGABRT,SIGNAL_CAST SIG_DFL);
+#endif
        abort();
 }
 
 /*******************************************************************
- A readdir wrapper which just returns the file name.
-********************************************************************/
 A readdir wrapper which just returns the file name.
+ ********************************************************************/
 
 const char *readdirname(DIR *p)
 {
@@ -1461,19 +1605,18 @@ const char *readdirname(DIR *p)
  of a path matches a (possibly wildcarded) entry in a namelist.
 ********************************************************************/
 
-BOOL is_in_path(const char *name, name_compare_entry *namelist)
+BOOL is_in_path(const char *name, name_compare_entry *namelist, BOOL case_sensitive)
 {
        pstring last_component;
        char *p;
 
-       DEBUG(8, ("is_in_path: %s\n", name));
-
        /* if we have no list it's obviously not in the path */
        if((namelist == NULL ) || ((namelist != NULL) && (namelist[0].name == NULL))) {
-               DEBUG(8,("is_in_path: no name list.\n"));
                return False;
        }
 
+       DEBUG(8, ("is_in_path: %s\n", name));
+
        /* Get the last component of the unix name. */
        p = strrchr_m(name, '/');
        strncpy(last_component, p ? ++p : name, sizeof(last_component)-1);
@@ -1549,8 +1692,7 @@ void set_namearray(name_compare_entry **ppname_array, char *namelist)
        if(num_entries == 0)
                return;
 
-       if(( (*ppname_array) = (name_compare_entry *)malloc(
-                                       (num_entries + 1) * sizeof(name_compare_entry))) == NULL) {
+       if(( (*ppname_array) = SMB_MALLOC_ARRAY(name_compare_entry, num_entries + 1)) == NULL) {
                DEBUG(0,("set_namearray: malloc fail\n"));
                return;
        }
@@ -1573,7 +1715,7 @@ void set_namearray(name_compare_entry **ppname_array, char *namelist)
                        break;
 
                (*ppname_array)[i].is_wild = ms_has_wild(nameptr);
-               if(((*ppname_array)[i].name = strdup(nameptr)) == NULL) {
+               if(((*ppname_array)[i].name = SMB_STRDUP(nameptr)) == NULL) {
                        DEBUG(0,("set_namearray: malloc fail (1)\n"));
                        return;
                }
@@ -1674,37 +1816,66 @@ BOOL is_myname(const char *s)
        return(ret);
 }
 
-/********************************************************************
- Return only the first IP address of our configured interfaces
- as a string
- *******************************************************************/
-
-const char* get_my_primary_ip (void)
+BOOL is_myname_or_ipaddr(const char *s)
 {
-       static fstring ip_string;
-       int n;
-       struct iface_struct nics[MAX_INTERFACES];
+       fstring name, dnsname;
+       char *servername;
 
-       if ((n=get_interfaces(nics, MAX_INTERFACES)) <= 0)
-               return NULL;
+       if ( !s )
+               return False;
 
-       fstrcpy(ip_string, inet_ntoa(nics[0].ip));
-       return ip_string;
-}
+       /* santize the string from '\\name' */
+
+       fstrcpy( name, s );
+
+       servername = strrchr_m( name, '\\' );
+       if ( !servername )
+               servername = name;
+       else
+               servername++;
 
-BOOL is_myname_or_ipaddr(const char *s)
-{
        /* optimize for the common case */
-       if (strequal(s, global_myname())) 
+
+       if (strequal(servername, global_myname())) 
+               return True;
+
+       /* check for an alias */
+
+       if (is_myname(servername))
+               return True;
+
+       /* check for loopback */
+
+       if (strequal(servername, "localhost")) 
                return True;
 
+       /* maybe it's my dns name */
+
+       if ( get_mydnsfullname( dnsname ) )
+               if ( strequal( servername, dnsname ) )
+                       return True;
+               
+       /* handle possible CNAME records */
+
+       if ( !is_ipaddress( servername ) ) {
+               /* use DNS to resolve the name, but only the first address */
+               struct hostent *hp;
+
+               if (((hp = sys_gethostbyname(name)) != NULL) && (hp->h_addr != NULL)) {
+                       struct in_addr return_ip;
+                       putip( (char*)&return_ip, (char*)hp->h_addr );
+                       fstrcpy( name, inet_ntoa( return_ip ) );
+                       servername = name;
+               }       
+       }
+               
        /* maybe its an IP address? */
-       if (is_ipaddress(s)) {
+       if (is_ipaddress(servername)) {
                struct iface_struct nics[MAX_INTERFACES];
                int i, n;
                uint32 ip;
                
-               ip = interpret_addr(s);
+               ip = interpret_addr(servername);
                if ((ip==0) || (ip==0xffffffff))
                        return False;
                        
@@ -1715,10 +1886,6 @@ BOOL is_myname_or_ipaddr(const char *s)
                }
        }       
 
-       /* check for an alias */
-       if (is_myname(s))
-               return True;
-       
        /* no match */
        return False;
 }
@@ -1746,13 +1913,15 @@ BOOL is_myworkgroup(const char *s)
    Win2k => "Windows 2000 5.0"
    NT4   => "Windows NT 4.0" 
    Win9x => "Windows 4.0"
+ Windows 2003 doesn't set the native lan manager string but 
+ they do set the domain to "Windows 2003 5.2" (probably a bug).
 ********************************************************************/
 
 void ra_lanman_string( const char *native_lanman )
 {               
-       if ( 0 == strcmp( native_lanman, "Windows 2002 5.1" ) )
+       if ( strcmp( native_lanman, "Windows 2002 5.1" ) == 0 )
                set_remote_arch( RA_WINXP );
-       else if ( 0 == strcmp( native_lanman, "Windows .NET 5.2" ) )
+       else if ( strcmp( native_lanman, "Windows Server 2003 5.2" ) == 0 )
                set_remote_arch( RA_WIN2K3 );
 }
 
@@ -1767,33 +1936,38 @@ void set_remote_arch(enum remote_arch_types type)
        switch( type ) {
        case RA_WFWG:
                fstrcpy(remote_arch, "WfWg");
-               return;
+               break;
        case RA_OS2:
                fstrcpy(remote_arch, "OS2");
-               return;
+               break;
        case RA_WIN95:
                fstrcpy(remote_arch, "Win95");
-               return;
+               break;
        case RA_WINNT:
                fstrcpy(remote_arch, "WinNT");
-               return;
+               break;
        case RA_WIN2K:
                fstrcpy(remote_arch, "Win2K");
-               return;
+               break;
        case RA_WINXP:
                fstrcpy(remote_arch, "WinXP");
-               return;
+               break;
        case RA_WIN2K3:
                fstrcpy(remote_arch, "Win2K3");
-               return;
+               break;
        case RA_SAMBA:
                fstrcpy(remote_arch,"Samba");
-               return;
+               break;
+       case RA_CIFSFS:
+               fstrcpy(remote_arch,"CIFSFS");
+               break;
        default:
                ra_type = RA_UNKNOWN;
                fstrcpy(remote_arch, "UNKNOWN");
                break;
        }
+
+       DEBUG(10,("set_remote_arch: Client arch is \'%s\'\n", remote_arch));
 }
 
 /*******************************************************************
@@ -1805,50 +1979,6 @@ enum remote_arch_types get_remote_arch(void)
        return ra_type;
 }
 
-
-void out_ascii(FILE *f, unsigned char *buf,int len)
-{
-       int i;
-       for (i=0;i<len;i++)
-               fprintf(f, "%c", isprint(buf[i])?buf[i]:'.');
-}
-
-void out_data(FILE *f,char *buf1,int len, int per_line)
-{
-       unsigned char *buf = (unsigned char *)buf1;
-       int i=0;
-       if (len<=0) {
-               return;
-       }
-
-       fprintf(f, "[%03X] ",i);
-       for (i=0;i<len;) {
-               fprintf(f, "%02X ",(int)buf[i]);
-               i++;
-               if (i%(per_line/2) == 0) fprintf(f, " ");
-               if (i%per_line == 0) {      
-                       out_ascii(f,&buf[i-per_line  ],per_line/2); fprintf(f, " ");
-                       out_ascii(f,&buf[i-per_line/2],per_line/2); fprintf(f, "\n");
-                       if (i<len) fprintf(f, "[%03X] ",i);
-               }
-       }
-       if ((i%per_line) != 0) {
-               int n;
-
-               n = per_line - (i%per_line);
-               fprintf(f, " ");
-               if (n>(per_line/2)) fprintf(f, " ");
-               while (n--) {
-                       fprintf(f, "   ");
-               }
-               n = MIN(per_line/2,i%per_line);
-               out_ascii(f,&buf[i-(i%per_line)],n); fprintf(f, " ");
-               n = (i%per_line) - n;
-               if (n>0) out_ascii(f,&buf[i-n],n); 
-               fprintf(f, "\n");    
-       }
-}
-
 void print_asc(int level, const unsigned char *buf,int len)
 {
        int i;
@@ -1889,6 +2019,17 @@ void dump_data(int level, const char *buf1,int len)
        }       
 }
 
+void dump_data_pw(const char *msg, const uchar * data, size_t len)
+{
+#ifdef DEBUG_PASSWORD
+       DEBUG(11, ("%s", msg));
+       if (data != NULL && len > 0)
+       {
+               dump_data(11, data, len);
+       }
+#endif
+}
+
 char *tab_depth(int depth)
 {
        static pstring spaces;
@@ -2070,13 +2211,19 @@ int smb_mkstemp(char *template)
  malloc that aborts with smb_panic on fail or zero size.
  *****************************************************************/  
 
-void *smb_xmalloc(size_t size)
+void *smb_xmalloc_array(size_t size, unsigned int count)
 {
        void *p;
        if (size == 0)
-               smb_panic("smb_xmalloc: called with zero size.\n");
-       if ((p = malloc(size)) == NULL)
-               smb_panic("smb_xmalloc: malloc fail.\n");
+               smb_panic("smb_xmalloc_array: called with zero size.\n");
+        if (count >= MAX_ALLOC_SIZE/size) {
+                smb_panic("smb_xmalloc: alloc size too large.\n");
+        }
+       if ((p = SMB_MALLOC(size*count)) == NULL) {
+               DEBUG(0, ("smb_xmalloc_array failed to allocate %lu * %lu bytes\n",
+                       (unsigned long)size, (unsigned long)count));
+               smb_panic("smb_xmalloc_array: malloc fail.\n");
+       }
        return p;
 }
 
@@ -2087,7 +2234,7 @@ void *smb_xmalloc(size_t size)
 void *smb_xmemdup(const void *p, size_t size)
 {
        void *p2;
-       p2 = smb_xmalloc(size);
+       p2 = SMB_XMALLOC_ARRAY(unsigned char,size);
        memcpy(p2, p, size);
        return p2;
 }
@@ -2098,10 +2245,19 @@ void *smb_xmemdup(const void *p, size_t size)
 
 char *smb_xstrdup(const char *s)
 {
+#if defined(PARANOID_MALLOC_CHECKER)
+#ifdef strdup
+#undef strdup
+#endif
+#endif
        char *s1 = strdup(s);
+#if defined(PARANOID_MALLOC_CHECKER)
+#define strdup(s) __ERROR_DONT_USE_STRDUP_DIRECTLY
+#endif
        if (!s1)
                smb_panic("smb_xstrdup: malloc fail\n");
        return s1;
+
 }
 
 /**
@@ -2110,7 +2266,15 @@ char *smb_xstrdup(const char *s)
 
 char *smb_xstrndup(const char *s, size_t n)
 {
+#if defined(PARANOID_MALLOC_CHECKER)
+#ifdef strndup
+#undef strndup
+#endif
+#endif
        char *s1 = strndup(s, n);
+#if defined(PARANOID_MALLOC_CHECKER)
+#define strndup(s,n) __ERROR_DONT_USE_STRNDUP_DIRECTLY
+#endif
        if (!s1)
                smb_panic("smb_xstrndup: malloc fail\n");
        return s1;
@@ -2142,7 +2306,7 @@ void *memdup(const void *p, size_t size)
        void *p2;
        if (size == 0)
                return NULL;
-       p2 = malloc(size);
+       p2 = SMB_MALLOC(size);
        if (!p2)
                return NULL;
        memcpy(p2, p, size);
@@ -2170,7 +2334,7 @@ char *lock_path(const char *name)
        static pstring fname;
 
        pstrcpy(fname,lp_lockdir());
-       trim_string(fname,"","/");
+       trim_char(fname,'\0','/');
        
        if (!directory_exist(fname,NULL))
                mkdir(fname,0755);
@@ -2190,7 +2354,7 @@ char *pid_path(const char *name)
        static pstring fname;
 
        pstrcpy(fname,lp_piddir());
-       trim_string(fname,"","/");
+       trim_char(fname,'\0','/');
 
        if (!directory_exist(fname,NULL))
                mkdir(fname,0755);
@@ -2212,7 +2376,7 @@ char *pid_path(const char *name)
 char *lib_path(const char *name)
 {
        static pstring fname;
-       snprintf(fname, sizeof(fname), "%s/%s", dyn_LIBDIR, name);
+       fstr_sprintf(fname, "%s/%s", dyn_LIBDIR, name);
        return fname;
 }
 
@@ -2260,7 +2424,7 @@ char *parent_dirname(const char *path)
  Determine if a pattern contains any Microsoft wildcard characters.
 *******************************************************************/
 
-BOOL ms_has_wild(char *s)
+BOOL ms_has_wild(const char *s)
 {
        char c;
        while ((c = *s++)) {
@@ -2300,30 +2464,35 @@ BOOL ms_has_wild_w(const smb_ucs2_t *s)
 
 BOOL mask_match(const char *string, char *pattern, BOOL is_case_sensitive)
 {
-       fstring p2, s2;
-
        if (strcmp(string,"..") == 0)
                string = ".";
        if (strcmp(pattern,".") == 0)
                return False;
        
-       if (is_case_sensitive)
-               return ms_fnmatch(pattern, string, Protocol) == 0;
+       return ms_fnmatch(pattern, string, Protocol, is_case_sensitive) == 0;
+}
+
+/*******************************************************************
+ A wrapper that handles a list of patters and calls mask_match()
+ on each.  Returns True if any of the patterns match.
+*******************************************************************/
 
-       fstrcpy(p2, pattern);
-       fstrcpy(s2, string);
-       strlower(p2); 
-       strlower(s2);
-       return ms_fnmatch(p2, s2, Protocol) == 0;
+BOOL mask_match_list(const char *string, char **list, int listLen, BOOL is_case_sensitive)
+{
+       while (listLen-- > 0) {
+               if (mask_match(string, *list++, is_case_sensitive))
+                       return True;
+       }
+       return False;
 }
 
 /*********************************************************
  Recursive routine that is called by unix_wild_match.
 *********************************************************/
 
-static BOOL unix_do_match(char *regexp, char *str)
+static BOOL unix_do_match(const char *regexp, const char *str)
 {
-       char *p;
+       const char *p;
 
        for( p = regexp; *p && *str; ) {
 
@@ -2429,8 +2598,8 @@ BOOL unix_wild_match(const char *pattern, const char *string)
 
        pstrcpy(p2, pattern);
        pstrcpy(s2, string);
-       strlower(p2);
-       strlower(s2);
+       strlower_m(p2);
+       strlower_m(s2);
 
        /* Remove any *? and ** from the pattern as they are meaningless */
        for(p = p2; *p; p++)
@@ -2443,6 +2612,22 @@ BOOL unix_wild_match(const char *pattern, const char *string)
        return unix_do_match(p2, s2) == 0;      
 }
 
+/**********************************************************************
+ Converts a name to a fully qalified domain name.
+***********************************************************************/
+                                                                                                                                                   
+void name_to_fqdn(fstring fqdn, const char *name)
+{
+       struct hostent *hp = sys_gethostbyname(name);
+       if ( hp && hp->h_name && *hp->h_name ) {
+               DEBUG(10,("name_to_fqdn: lookup for %s -> %s.\n", name, hp->h_name));
+               fstrcpy(fqdn,hp->h_name);
+       } else {
+               DEBUG(10,("name_to_fqdn: lookup for %s failed.\n", name));
+               fstrcpy(fqdn, name);
+       }
+}
+
 #ifdef __INSURE__
 
 /*******************************************************************