r9791: r11611@blu: tridge | 2005-08-30 21:48:22 +1000
[ira/wip.git] / source4 / lib / util.c
index 5385174ce4589b5183de5e561127434bc72f4e51..2e0503b0d96ab2692843577042136ba53e9aaead 100644 (file)
 */
 
 #include "includes.h"
+#include "dynconfig.h"
 #include "system/network.h"
 #include "system/iconv.h"
+#include "system/dir.h"
+#include "system/filesys.h"
 
-/**************************************************************************n
+/***************************************************************************
  Find a suitable temporary directory. The result should be copied immediately
  as it may be overwritten by a subsequent call.
 ****************************************************************************/
@@ -42,16 +45,15 @@ const char *tmpdir(void)
 /*******************************************************************
  Check if a file exists - call vfs_file_exist for samba files.
 ********************************************************************/
-BOOL file_exist(const char *fname, struct stat *sbuf)
+BOOL file_exist(const char *fname)
 {
        struct stat st;
-       if (!sbuf)
-               sbuf = &st;
-  
-       if (stat(fname,sbuf) != 0) 
-               return(False);
 
-       return((S_ISREG(sbuf->st_mode)) || (S_ISFIFO(sbuf->st_mode)));
+       if (stat(fname, &st) != 0) {
+               return False;
+       }
+
+       return ((S_ISREG(st.st_mode)) || (S_ISFIFO(st.st_mode)));
 }
 
 /*******************************************************************
@@ -72,18 +74,16 @@ time_t file_modtime(const char *fname)
  Check if a directory exists.
 ********************************************************************/
 
-BOOL directory_exist(const char *dname,struct stat *st)
+BOOL directory_exist(const char *dname)
 {
-       struct stat st2;
+       struct stat st;
        BOOL ret;
 
-       if (!st)
-               st = &st2;
-
-       if (stat(dname,st) != 0) 
-               return(False);
+       if (stat(dname,&st) != 0) {
+               return False;
+       }
 
-       ret = S_ISDIR(st->st_mode);
+       ret = S_ISDIR(st.st_mode);
        if(!ret)
                errno = ENOTDIR;
        return ret;
@@ -214,31 +214,6 @@ void become_daemon(BOOL Fork)
 }
 
 
-/****************************************************************************
- Expand a pointer to be a particular size.
-****************************************************************************/
-
-void *Realloc(void *p,size_t size)
-{
-       void *ret=NULL;
-
-       if (size == 0) {
-               SAFE_FREE(p);
-               DEBUG(5,("Realloc asked for 0 bytes\n"));
-               return NULL;
-       }
-
-       if (!p)
-               ret = (void *)malloc(size);
-       else
-               ret = (void *)realloc(p,size);
-
-       if (!ret)
-               DEBUG(0,("Memory allocation error: failed to expand to %d bytes\n",(int)size));
-
-       return(ret);
-}
-
 /****************************************************************************
  Free memory, checks for NULL.
  Use directly SAFE_FREE()
@@ -306,62 +281,6 @@ char* get_myname(void)
        return hostname;
 }
 
-/****************************************************************************
- Get my own name, including domain.
-****************************************************************************/
-
-BOOL get_myfullname(char *my_name)
-{
-       pstring hostname;
-
-       *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';
-
-       if (my_name)
-               fstrcpy(my_name, hostname);
-       return True;
-}
-
-/****************************************************************************
- Get my own domain name.
-****************************************************************************/
-
-BOOL get_mydomname(fstring my_domname)
-{
-       pstring hostname;
-       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)
-               return False;
-
-       p++;
-       
-       if (my_domname)
-               fstrcpy(my_domname, p);
-
-       return True;
-}
-
 /****************************************************************************
  Interpret a protocol description string, with a default.
 ****************************************************************************/
@@ -413,13 +332,18 @@ uint32_t interpret_addr(const char *str)
        struct hostent *hp;
        uint32_t res;
 
-       if (str == NULL || 
+       if (str == NULL || *str == 0 ||
            strcmp(str,"0.0.0.0") == 0) {
                return 0;
        }
        if (strcmp(str,"255.255.255.255") == 0) {
                return 0xFFFFFFFF;
        }
+       /* recognise 'localhost' as a special name. This fixes problems with
+          some hosts that don't have localhost in /etc/hosts */
+       if (strcasecmp(str,"localhost") == 0) {
+               str = "127.0.0.1";
+       }
 
        /* if it's in the form of an IP address then get the lib to interpret it */
        if (is_ipaddress(str)) {
@@ -436,7 +360,7 @@ uint32_t interpret_addr(const char *str)
                        DEBUG(3,("sys_gethostbyname: host address is invalid for host %s\n",str));
                        return 0;
                }
-               putip((char *)&res,(char *)hp->h_addr);
+               memcpy((char *)&res,(char *)hp->h_addr, 4);
        }
 
        if (res == (uint32_t)-1)
@@ -462,9 +386,7 @@ struct ipv4_addr interpret_addr2(const char *str)
 
 BOOL is_zero_ip(struct ipv4_addr ip)
 {
-       uint32_t a;
-       putip((char *)&a,(char *)&ip);
-       return(a == 0);
+       return ip.addr == 0;
 }
 
 /*******************************************************************
@@ -564,9 +486,8 @@ static void print_asc(int level, const uint8_t *buf,int len)
                DEBUGADD(level,("%c", isprint(buf[i])?buf[i]:'.'));
 }
 
-void dump_data(int level, const char *buf1,int len)
+void dump_data(int level, const uint8_t *buf,int len)
 {
-       const uint8_t *buf = (const uint8_t *)buf1;
        int i=0;
        if (len<=0) return;
 
@@ -711,7 +632,7 @@ char *lock_path(TALLOC_CTX* mem_ctx, const char *name)
        dname = talloc_strdup(mem_ctx, lp_lockdir());
        trim_string(dname,"","/");
        
-       if (!directory_exist(dname,NULL)) {
+       if (!directory_exist(dname)) {
                mkdir(dname,0755);
        }
        
@@ -737,6 +658,27 @@ char *lib_path(TALLOC_CTX* mem_ctx, const char *name)
        return fname;
 }
 
+/**
+ * @brief Returns an absolute path to a file in the Samba private directory.
+ *
+ * @param name File to find, relative to PRIVATEDIR.
+ * if name is not relative, then use it as-is
+ *
+ * @retval Pointer to a talloc'ed string containing the full path.
+ **/
+char *private_path(TALLOC_CTX* mem_ctx, const char *name)
+{
+       char *fname;
+       if (name == NULL) {
+               return NULL;
+       }
+       if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
+               return talloc_strdup(mem_ctx, name);
+       }
+       fname = talloc_asprintf(mem_ctx, "%s/%s", lp_private_dir(), name);
+       return fname;
+}
+
 /*
   return a path in the smbd.tmp directory, where all temporary file
   for smbd go. If NULL is passed for name then return the directory 
@@ -747,7 +689,7 @@ char *smbd_tmp_path(TALLOC_CTX *mem_ctx, const char *name)
        char *fname, *dname;
 
        dname = lock_path(mem_ctx, "smbd.tmp");
-       if (!directory_exist(dname,NULL)) {
+       if (!directory_exist(dname)) {
                mkdir(dname,0755);
        }
 
@@ -787,7 +729,7 @@ void dump_data_pw(const char *msg, const uint8_t * data, size_t len)
 
 /* see if a range of memory is all zero. A NULL pointer is considered
    to be all zero */
-BOOL all_zero(const char *ptr, uint_t size)
+BOOL all_zero(const uint8_t *ptr, uint_t size)
 {
        int i;
        if (!ptr) return True;
@@ -797,4 +739,19 @@ BOOL all_zero(const char *ptr, uint_t size)
        return True;
 }
 
+/*
+  realloc an array, checking for integer overflow in the array size
+*/
+void *realloc_array(void *ptr, size_t el_size, unsigned count)
+{
+#define MAX_MALLOC_SIZE 0x7fffffff
+       if (count == 0 ||
+           count >= MAX_MALLOC_SIZE/el_size) {
+               return NULL;
+       }
+       if (!ptr) {
+               return malloc(el_size * count);
+       }
+       return realloc(ptr, el_size * count);
+}