[GLUE] Rsync SAMBA_3_2_0 SVN r25598 in order to create the v3-2-test branch.
[samba.git] / source / lib / util.c
index 3852fccdd1e6ab75bde00ded7c2a41c82272677e..adbebb04d458544fd19ea489d705ebfdf03e1217 100644 (file)
@@ -456,7 +456,7 @@ SMB_OFF_T get_file_size(char *file_name)
 
 char *attrib_string(uint16 mode)
 {
-       static fstring attrstr;
+       fstring attrstr;
 
        attrstr[0] = 0;
 
@@ -467,7 +467,7 @@ char *attrib_string(uint16 mode)
        if (mode & aSYSTEM) fstrcat(attrstr,"S");
        if (mode & aRONLY) fstrcat(attrstr,"R");          
 
-       return(attrstr);
+       return talloc_strdup(talloc_tos(), attrstr);
 }
 
 /*******************************************************************
@@ -514,51 +514,32 @@ void show_msg(char *buf)
        dump_data(10, (uint8 *)smb_buf(buf), bcc);      
 }
 
-/*******************************************************************
- Set the length and marker of an encrypted smb packet.
-********************************************************************/
-
-void smb_set_enclen(char *buf,int len,uint16 enc_ctx_num)
-{
-       _smb_setlen(buf,len);
-
-       SCVAL(buf,4,0xFF);
-       SCVAL(buf,5,'E');
-       SSVAL(buf,6,enc_ctx_num);
-}
-
 /*******************************************************************
  Set the length and marker of an smb packet.
 ********************************************************************/
 
-void smb_setlen(const char *frombuf, char *buf, int len)
+void smb_setlen(char *buf,int len)
 {
        _smb_setlen(buf,len);
 
-       if (frombuf) {
-               if (buf != frombuf) {
-                       memcpy(buf+4, frombuf+4, 4);
-               }
-       } else {
-               SCVAL(buf,4,0xFF);
-               SCVAL(buf,5,'S');
-               SCVAL(buf,6,'M');
-               SCVAL(buf,7,'B');
-       }
+       SCVAL(buf,4,0xFF);
+       SCVAL(buf,5,'S');
+       SCVAL(buf,6,'M');
+       SCVAL(buf,7,'B');
 }
 
 /*******************************************************************
  Setup the word count and byte count for a smb message.
 ********************************************************************/
 
-int set_message(const char *frombuf, char *buf,int num_words,int num_bytes,BOOL zero)
+int set_message(char *buf,int num_words,int num_bytes,BOOL zero)
 {
        if (zero && (num_words || num_bytes)) {
                memset(buf + smb_size,'\0',num_words*2 + num_bytes);
        }
        SCVAL(buf,smb_wct,num_words);
        SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);  
-       smb_setlen(frombuf, buf,smb_size + num_words*2 + num_bytes - 4);
+       smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
        return (smb_size + num_words*2 + num_bytes);
 }
 
@@ -566,11 +547,11 @@ int set_message(const char *frombuf, char *buf,int num_words,int num_bytes,BOOL
  Setup only the byte count for a smb message.
 ********************************************************************/
 
-int set_message_bcc(const char *frombuf, char *buf,int num_bytes)
+int set_message_bcc(char *buf,int num_bytes)
 {
        int num_words = CVAL(buf,smb_wct);
        SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);  
-       smb_setlen(frombuf, buf,smb_size + num_words*2 + num_bytes - 4);
+       smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
        return (smb_size + num_words*2 + num_bytes);
 }
 
@@ -579,11 +560,9 @@ int set_message_bcc(const char *frombuf, char *buf,int num_bytes)
  message as a marker.
 ********************************************************************/
 
-int set_message_end(const char *frombuf, void *outbuf,void *end_ptr)
+int set_message_end(void *outbuf,void *end_ptr)
 {
-       return set_message_bcc(frombuf,
-                       (char *)outbuf,
-                       PTR_DIFF(end_ptr,smb_buf((char *)outbuf)));
+       return set_message_bcc((char *)outbuf,PTR_DIFF(end_ptr,smb_buf((char *)outbuf)));
 }
 
 /*******************************************************************
@@ -603,7 +582,7 @@ ssize_t message_push_blob(uint8 **outbuf, DATA_BLOB blob)
        *outbuf = tmp;
 
        memcpy(tmp + smb_len(tmp) + 4, blob.data, blob.length);
-       set_message_bcc(NULL, (char *)tmp, smb_buflen(tmp) + blob.length);
+       set_message_bcc((char *)tmp, smb_buflen(tmp) + blob.length);
        return blob.length;
 }
 
@@ -1203,7 +1182,7 @@ void safe_free(void *p)
 
 BOOL get_myname(char *my_name)
 {
-       pstring hostname;
+       fstring hostname;
 
        *hostname = 0;
 
@@ -1211,7 +1190,7 @@ BOOL get_myname(char *my_name)
        if (gethostname(hostname, sizeof(hostname)) == -1) {
                DEBUG(0,("gethostname failed\n"));
                return False;
-       } 
+       }
 
        /* Ensure null termination. */
        hostname[sizeof(hostname)-1] = '\0';
@@ -1222,10 +1201,10 @@ BOOL get_myname(char *my_name)
 
                if (p)
                        *p = 0;
-               
+
                fstrcpy(my_name,hostname);
        }
-       
+
        return(True);
 }
 
@@ -1277,6 +1256,7 @@ BOOL get_mydnsdomname(fstring my_domname)
        if (p) {
                p++;
                fstrcpy(my_domname, p);
+               return True;
        }
 
        return False;
@@ -1578,34 +1558,39 @@ BOOL process_exists_by_pid(pid_t pid)
 
 const char *uidtoname(uid_t uid)
 {
-       static fstring name;
-       struct passwd *pass;
+       TALLOC_CTX *ctx = talloc_tos();
+       char *name = NULL;
+       struct passwd *pass = NULL;
 
-       pass = getpwuid_alloc(NULL, uid);
+       pass = getpwuid_alloc(ctx,uid);
        if (pass) {
-               fstrcpy(name, pass->pw_name);
+               name = talloc_strdup(ctx,pass->pw_name);
                TALLOC_FREE(pass);
        } else {
-               slprintf(name, sizeof(name) - 1, "%ld",(long int)uid);
+               name = talloc_asprintf(ctx,
+                               "%ld",
+                               (long int)uid);
        }
        return name;
 }
 
-
 /*******************************************************************
  Convert a gid into a group name.
 ********************************************************************/
 
 char *gidtoname(gid_t gid)
 {
-       static fstring name;
+       fstring name;
        struct group *grp;
 
        grp = getgrgid(gid);
-       if (grp)
-               return(grp->gr_name);
-       slprintf(name,sizeof(name) - 1, "%d",(int)gid);
-       return(name);
+       if (grp) {
+               fstrcpy(name, grp->gr_name);
+       }
+       else {
+               slprintf(name,sizeof(name) - 1, "%d",(int)gid);
+       }
+       return talloc_strdup(talloc_tos(), name);
 }
 
 /*******************************************************************
@@ -1859,15 +1844,7 @@ const char *readdirname(SMB_STRUCT_DIR *p)
        dname = dname - 2;
 #endif
 
-       {
-               static pstring buf;
-               int len = NAMLEN(ptr);
-               memcpy(buf, dname, len);
-               buf[len] = 0;
-               dname = buf;
-       }
-
-       return(dname);
+       return talloc_strdup(talloc_tos(), dname);
 }
 
 /*******************************************************************
@@ -2140,7 +2117,7 @@ BOOL is_myname_or_ipaddr(const char *s)
 
        /* optimize for the common case */
 
-       if (strequal(servername, global_myname())) 
+       if (strequal(servername, global_myname()))
                return True;
 
        /* check for an alias */
@@ -2150,10 +2127,10 @@ BOOL is_myname_or_ipaddr(const char *s)
 
        /* check for loopback */
 
-       if (strequal(servername, "127.0.0.1")) 
+       if (strequal(servername, "127.0.0.1"))
                return True;
 
-       if (strequal(servername, "localhost")) 
+       if (strequal(servername, "localhost"))
                return True;
 
        /* maybe it's my dns name */
@@ -2161,7 +2138,7 @@ BOOL is_myname_or_ipaddr(const char *s)
        if ( get_mydnsfullname( dnsname ) )
                if ( strequal( servername, dnsname ) )
                        return True;
-               
+
        /* handle possible CNAME records */
 
        if ( !is_ipaddress( servername ) ) {
@@ -2173,25 +2150,25 @@ BOOL is_myname_or_ipaddr(const char *s)
                        putip( (char*)&return_ip, (char*)hp->h_addr );
                        fstrcpy( name, inet_ntoa( return_ip ) );
                        servername = name;
-               }       
+               }
        }
-               
+
        /* maybe its an IP address? */
        if (is_ipaddress(servername)) {
                struct iface_struct nics[MAX_INTERFACES];
                int i, n;
                uint32 ip;
-               
+
                ip = interpret_addr(servername);
                if ((ip==0) || (ip==0xffffffff))
                        return False;
-                       
+
                n = get_interfaces(nics, MAX_INTERFACES);
                for (i=0; i<n; i++) {
-                       if (ip == nics[i].ip.s_addr)
+                       if (ip == nics[i].iface_addr.ip.s_addr)
                                return True;
                }
-       }       
+       }
 
        /* no match */
        return False;
@@ -2219,14 +2196,14 @@ BOOL is_myworkgroup(const char *s)
    WinXP => "Windows 2002 5.1"
    WinXP 64bit => "Windows XP 5.2"
    Win2k => "Windows 2000 5.0"
-   NT4   => "Windows NT 4.0" 
+   NT4   => "Windows NT 4.0"
    Win9x => "Windows 4.0"
- Windows 2003 doesn't set the native lan manager string but 
+ 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 ( strcmp( native_lanman, "Windows 2002 5.1" ) == 0 )
                set_remote_arch( RA_WINXP );
        else if ( strcmp( native_lanman, "Windows XP 5.2" ) == 0 )
@@ -2298,8 +2275,9 @@ void print_asc(int level, const unsigned char *buf,int len)
                DEBUG(level,("%c", isprint(buf[i])?buf[i]:'.'));
 }
 
-void dump_data(int level, const unsigned char *buf,int len)
+void dump_data(int level, const unsigned char *buf1,int len)
 {
+       const unsigned char *buf = (const unsigned char *)buf1;
        int i=0;
        if (len<=0) return;
 
@@ -2629,7 +2607,7 @@ char *myhostname(void)
 
 char *lock_path(const char *name)
 {
-       static pstring fname;
+       pstring fname;
 
        pstrcpy(fname,lp_lockdir());
        trim_char(fname,'\0','/');
@@ -2640,7 +2618,7 @@ char *lock_path(const char *name)
        pstrcat(fname,"/");
        pstrcat(fname,name);
 
-       return fname;
+       return talloc_strdup(talloc_tos(), fname);
 }
 
 /*****************************************************************
@@ -2649,7 +2627,7 @@ char *lock_path(const char *name)
 
 char *pid_path(const char *name)
 {
-       static pstring fname;
+       pstring fname;
 
        pstrcpy(fname,lp_piddir());
        trim_char(fname,'\0','/');
@@ -2660,7 +2638,7 @@ char *pid_path(const char *name)
        pstrcat(fname,"/");
        pstrcat(fname,name);
 
-       return fname;
+       return talloc_strdup(talloc_tos(), fname);
 }
 
 /**
@@ -2673,9 +2651,7 @@ char *pid_path(const char *name)
 
 char *lib_path(const char *name)
 {
-       static pstring fname;
-       fstr_sprintf(fname, "%s/%s", dyn_LIBDIR, name);
-       return fname;
+       return talloc_asprintf(talloc_tos(), "%s/%s", dyn_LIBDIR, name);
 }
 
 /**
@@ -2692,29 +2668,18 @@ const char *shlib_ext(void)
 /*******************************************************************
  Given a filename - get its directory name
  NB: Returned in static storage.  Caveats:
- o  Not safe in thread environment.
- o  Caller must not free.
  o  If caller wishes to preserve, they should copy.
 ********************************************************************/
 
 char *parent_dirname(const char *path)
 {
-       static pstring dirpath;
-       char *p;
-
-       if (!path)
-               return(NULL);
+       char *parent;
 
-       pstrcpy(dirpath, path);
-       p = strrchr_m(dirpath, '/');  /* Find final '/', if any */
-       if (!p) {
-               pstrcpy(dirpath, ".");    /* No final "/", so dir is "." */
-       } else {
-               if (p == dirpath)
-                       ++p;    /* For root "/", leave "/" in place */
-               *p = '\0';
+       if (!parent_dirname_talloc(talloc_tos(), path, &parent, NULL)) {
+               return NULL;
        }
-       return dirpath;
+
+       return parent;
 }
 
 BOOL parent_dirname_talloc(TALLOC_CTX *mem_ctx, const char *dir,
@@ -3179,9 +3144,9 @@ struct server_id interpret_pid(const char *pid_string)
 #endif
 }
 
-char *procid_str_static(const struct server_id *pid)
+char *procid_str(TALLOC_CTX *mem_ctx, const struct server_id *pid)
 {
-       static fstring str;
+       fstring str;
 #ifdef CLUSTER_SUPPORT
        if (pid->vnn == NONCLUSTER_VNN) {
                fstr_sprintf(str, "%d", (int)pid->pid);
@@ -3192,12 +3157,12 @@ char *procid_str_static(const struct server_id *pid)
 #else
        fstr_sprintf(str, "%d", (int)pid->pid);
 #endif
-       return str;
+       return talloc_strdup(mem_ctx, str);
 }
 
-char *procid_str(TALLOC_CTX *mem_ctx, const struct server_id *pid)
+char *procid_str_static(const struct server_id *pid)
 {
-       return talloc_strdup(mem_ctx, procid_str_static(pid));
+       return procid_str(talloc_tos(), pid);
 }
 
 BOOL procid_valid(const struct server_id *pid)