Merge branch 'master' of ssh://git.samba.org/data/git/samba into regsrv
[kai/samba.git] / source3 / lib / util.c
index 19c3e0c65b0bc3c5a7500ba479378e997f96f1fe..820cf376beb79baf2d6552101f4b89474a47d8e1 100644 (file)
@@ -60,8 +60,6 @@ enum protocol_types Protocol = PROTOCOL_COREPLUS;
 /* this is used by the chaining code */
 int chain_size = 0;
 
-int trans_num = 0;
-
 static enum remote_arch_types ra_type = RA_UNKNOWN;
 
 /***********************************************************************
@@ -183,6 +181,7 @@ void gfree_names(void)
        SAFE_FREE( smb_myworkgroup );
        SAFE_FREE( smb_scope );
        free_netbios_names_array();
+       free_local_machine_name();
 }
 
 void gfree_all( void )
@@ -190,12 +189,9 @@ void gfree_all( void )
        gfree_names();
        gfree_loadparm();
        gfree_case_tables();
-       gfree_debugsyms();
        gfree_charcnv();
        gfree_interfaces();
-
-       /* release the talloc null_context memory last */
-       talloc_disable_null_tracking();
+       gfree_debugsyms();
 }
 
 const char *my_netbios_names(int i)
@@ -289,7 +285,9 @@ static struct user_auth_info cmdline_auth_info = {
        NULL,   /* password */
        false,  /* got_pass */
        false,  /* use_kerberos */
-       Undefined /* signing state */
+       Undefined, /* signing state */
+       false,  /* smb_encrypt */
+       false   /* use machine account */
 };
 
 const char *get_cmdline_auth_info_username(void)
@@ -350,6 +348,11 @@ int get_cmdline_auth_info_signing_state(void)
        return cmdline_auth_info.signing_state;
 }
 
+void set_cmdline_auth_info_use_kerberos(bool b)
+{
+        cmdline_auth_info.use_kerberos = b;
+}
+
 bool get_cmdline_auth_info_use_kerberos(void)
 {
        return cmdline_auth_info.use_kerberos;
@@ -362,11 +365,32 @@ void set_cmdline_auth_info_use_krb5_ticket(void)
        cmdline_auth_info.got_pass = true;
 }
 
+/* This should only be used by lib/popt_common.c JRA */
+void set_cmdline_auth_info_smb_encrypt(void)
+{
+       cmdline_auth_info.smb_encrypt = true;
+}
+
+void set_cmdline_auth_info_use_machine_account(void)
+{
+       cmdline_auth_info.use_machine_account = true;
+}
+
 bool get_cmdline_auth_info_got_pass(void)
 {
        return cmdline_auth_info.got_pass;
 }
 
+bool get_cmdline_auth_info_smb_encrypt(void)
+{
+       return cmdline_auth_info.smb_encrypt;
+}
+
+bool get_cmdline_auth_info_use_machine_account(void)
+{
+       return cmdline_auth_info.use_machine_account;
+}
+
 bool get_cmdline_auth_info_copy(struct user_auth_info *info)
 {
        *info = cmdline_auth_info;
@@ -379,17 +403,40 @@ bool get_cmdline_auth_info_copy(struct user_auth_info *info)
        return true;
 }
 
-/**************************************************************************n
- Find a suitable temporary directory. The result should be copied immediately
- as it may be overwritten by a subsequent call.
-****************************************************************************/
-
-const char *tmpdir(void)
+bool set_cmdline_auth_info_machine_account_creds(void)
 {
-       char *p;
-       if ((p = getenv("TMPDIR")))
-               return p;
-       return "/tmp";
+       char *pass = NULL;
+       char *account = NULL;
+
+       if (!get_cmdline_auth_info_use_machine_account()) {
+               return false;
+       }
+
+       if (!secrets_init()) {
+               d_printf("ERROR: Unable to open secrets database\n");
+               return false;
+       }
+
+       if (asprintf(&account, "%s$@%s", global_myname(), lp_realm()) < 0) {
+               return false;
+       }
+
+       pass = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
+       if (!pass) {
+               d_printf("ERROR: Unable to fetch machine password for "
+                       "%s in domain %s\n",
+                       account, lp_workgroup());
+               SAFE_FREE(account);
+               return false;
+       }
+
+       set_cmdline_auth_info_username(account);
+       set_cmdline_auth_info_password(pass);
+
+       SAFE_FREE(account);
+       SAFE_FREE(pass);
+
+       return true;
 }
 
 /****************************************************************************
@@ -480,7 +527,7 @@ const char *get_numlist(const char *p, uint32 **num, int *count)
  Check if a file exists - call vfs_file_exist for samba files.
 ********************************************************************/
 
-bool file_exist(const char *fname,SMB_STRUCT_STAT *sbuf)
+bool file_exist_stat(const char *fname,SMB_STRUCT_STAT *sbuf)
 {
        SMB_STRUCT_STAT st;
        if (!sbuf)
@@ -493,24 +540,23 @@ bool file_exist(const char *fname,SMB_STRUCT_STAT *sbuf)
 }
 
 /*******************************************************************
- Check a files mod time.
+ Check if a unix domain socket exists - call vfs_file_exist for samba files.
 ********************************************************************/
 
-time_t file_modtime(const char *fname)
+bool socket_exist(const char *fname)
 {
        SMB_STRUCT_STAT st;
-  
        if (sys_stat(fname,&st) != 0) 
-               return(0);
+               return(False);
 
-       return(st.st_mtime);
+       return S_ISSOCK(st.st_mode);
 }
 
 /*******************************************************************
  Check if a directory exists.
 ********************************************************************/
 
-bool directory_exist(char *dname,SMB_STRUCT_STAT *st)
+bool directory_exist_stat(char *dname,SMB_STRUCT_STAT *st)
 {
        SMB_STRUCT_STAT st2;
        bool ret;
@@ -605,32 +651,30 @@ void show_msg(char *buf)
 }
 
 /*******************************************************************
- Set the length and marker of an smb packet.
+ Set the length and marker of an encrypted smb packet.
 ********************************************************************/
 
-void smb_setlen(char *buf,int len)
+void smb_set_enclen(char *buf,int len,uint16 enc_ctx_num)
 {
        _smb_setlen(buf,len);
 
        SCVAL(buf,4,0xFF);
-       SCVAL(buf,5,'S');
-       SCVAL(buf,6,'M');
-       SCVAL(buf,7,'B');
+       SCVAL(buf,5,'E');
+       SSVAL(buf,6,enc_ctx_num);
 }
 
 /*******************************************************************
- Setup the word count and byte count for a smb message.
+ Set the length and marker of an smb packet.
 ********************************************************************/
 
-int set_message(char *buf,int num_words,int num_bytes,bool zero)
+void smb_setlen(char *buf,int len)
 {
-       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(buf,smb_size + num_words*2 + num_bytes - 4);
-       return (smb_size + num_words*2 + num_bytes);
+       _smb_setlen(buf,len);
+
+       SCVAL(buf,4,0xFF);
+       SCVAL(buf,5,'S');
+       SCVAL(buf,6,'M');
+       SCVAL(buf,7,'B');
 }
 
 /*******************************************************************
@@ -641,20 +685,10 @@ 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(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);
 }
 
-/*******************************************************************
- Setup only the byte count for a smb message, using the end of the
- message as a marker.
-********************************************************************/
-
-int set_message_end(void *outbuf,void *end_ptr)
-{
-       return set_message_bcc((char *)outbuf,PTR_DIFF(end_ptr,smb_buf((char *)outbuf)));
-}
-
 /*******************************************************************
  Add a data blob to the end of a smb_buf, adjusting bcc and smb_len.
  Return the bytes added
@@ -866,97 +900,6 @@ ssize_t write_data_at_offset(int fd, const char *buffer, size_t N, SMB_OFF_T pos
 #endif
 }
 
-/****************************************************************************
- Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
- else
-  if SYSV use O_NDELAY
-  if BSD use FNDELAY
-****************************************************************************/
-
-int set_blocking(int fd, bool set)
-{
-       int val;
-#ifdef O_NONBLOCK
-#define FLAG_TO_SET O_NONBLOCK
-#else
-#ifdef SYSV
-#define FLAG_TO_SET O_NDELAY
-#else /* BSD */
-#define FLAG_TO_SET FNDELAY
-#endif
-#endif
-
-       if((val = sys_fcntl_long(fd, F_GETFL, 0)) == -1)
-               return -1;
-       if(set) /* Turn blocking on - ie. clear nonblock flag */
-               val &= ~FLAG_TO_SET;
-       else
-               val |= FLAG_TO_SET;
-       return sys_fcntl_long( fd, F_SETFL, val);
-#undef FLAG_TO_SET
-}
-
-/****************************************************************************
- Transfer some data between two fd's.
-****************************************************************************/
-
-#ifndef TRANSFER_BUF_SIZE
-#define TRANSFER_BUF_SIZE 65536
-#endif
-
-ssize_t transfer_file_internal(int infd, int outfd, size_t n, ssize_t (*read_fn)(int, void *, size_t),
-                                               ssize_t (*write_fn)(int, const void *, size_t))
-{
-       char *buf;
-       size_t total = 0;
-       ssize_t read_ret;
-       ssize_t write_ret;
-       size_t num_to_read_thistime;
-       size_t num_written = 0;
-
-       if ((buf = SMB_MALLOC_ARRAY(char, TRANSFER_BUF_SIZE)) == NULL)
-               return -1;
-
-       while (total < n) {
-               num_to_read_thistime = MIN((n - total), TRANSFER_BUF_SIZE);
-
-               read_ret = (*read_fn)(infd, buf, num_to_read_thistime);
-               if (read_ret == -1) {
-                       DEBUG(0,("transfer_file_internal: read failure. Error = %s\n", strerror(errno) ));
-                       SAFE_FREE(buf);
-                       return -1;
-               }
-               if (read_ret == 0)
-                       break;
-
-               num_written = 0;
-               while (num_written < read_ret) {
-                       write_ret = (*write_fn)(outfd,buf + num_written, read_ret - num_written);
-                       if (write_ret == -1) {
-                               DEBUG(0,("transfer_file_internal: write failure. Error = %s\n", strerror(errno) ));
-                               SAFE_FREE(buf);
-                               return -1;
-                       }
-                       if (write_ret == 0)
-                               return (ssize_t)total;
-                       num_written += (size_t)write_ret;
-               }
-
-               total += (size_t)read_ret;
-       }
-
-       SAFE_FREE(buf);
-       return (ssize_t)total;          
-}
-
-SMB_OFF_T transfer_file(int infd,int outfd,SMB_OFF_T n)
-{
-       return (SMB_OFF_T)transfer_file_internal(infd, outfd, (size_t)n, sys_read, sys_write);
-}
-
 /*******************************************************************
  Sleep for a specified number of milliseconds.
 ********************************************************************/
@@ -1037,6 +980,37 @@ void become_daemon(bool Fork, bool no_process_group)
                                  attach it to the logfile */
 }
 
+bool reinit_after_fork(struct messaging_context *msg_ctx,
+                      bool parent_longlived)
+{
+       NTSTATUS status;
+
+       /* Reset the state of the random
+        * number generation system, so
+        * children do not get the same random
+        * numbers as each other */
+       set_need_random_reseed();
+
+       /* tdb needs special fork handling */
+       if (tdb_reopen_all(parent_longlived ? 1 : 0) == -1) {
+               DEBUG(0,("tdb_reopen_all failed.\n"));
+               return false;
+       }
+
+       /*
+        * For clustering, we need to re-init our ctdbd connection after the
+        * fork
+        */
+       status = messaging_reinit(msg_ctx);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("messaging_reinit() failed: %s\n",
+                        nt_errstr(status)));
+               return false;
+       }
+
+       return true;
+}
+
 /****************************************************************************
  Put up a yes/no prompt.
 ****************************************************************************/
@@ -1098,26 +1072,6 @@ static void *realloc_(void *ptr, size_t size)
 
 #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 (el_size == 0 || count == 0) {
-               return NULL;
-       }
-#if defined(PARANOID_MALLOC_CHECKER)
-       return malloc_(el_size*count);
-#else
-       return malloc(el_size*count);
-#endif
-}
-
 /****************************************************************************
  Type-safe memalign
 ****************************************************************************/
@@ -1218,21 +1172,6 @@ void *Realloc(void *p, size_t size, bool free_old_on_error)
        return(ret);
 }
 
-/****************************************************************************
- Type-safe realloc.
-****************************************************************************/
-
-void *realloc_array(void *p, size_t el_size, unsigned int count, bool free_old_on_error)
-{
-       if (count >= MAX_ALLOC_SIZE/el_size) {
-               if (free_old_on_error) {
-                       SAFE_FREE(p);
-               }
-               return NULL;
-       }
-       return Realloc(p, el_size*count, free_old_on_error);
-}
-
 /****************************************************************************
  (Hopefully) efficient array append.
 ****************************************************************************/
@@ -1288,22 +1227,11 @@ void add_to_large_array(TALLOC_CTX *mem_ctx, size_t element_size,
        *array_size = -1;
 }
 
-/****************************************************************************
- Free memory, checks for NULL.
- Use directly SAFE_FREE()
- Exists only because we need to pass a function pointer somewhere --SSS
-****************************************************************************/
-
-void safe_free(void *p)
-{
-       SAFE_FREE(p);
-}
-
 /****************************************************************************
  Get my own name and IP.
 ****************************************************************************/
 
-char *get_myname(TALLOC_CTX *ctx)
+char *talloc_get_myname(TALLOC_CTX *ctx)
 {
        char *p;
        char hostname[HOST_NAME_MAX];
@@ -1518,14 +1446,6 @@ bool process_exists(const struct server_id pid)
 #endif
 }
 
-bool process_exists_by_pid(pid_t pid)
-{
-       /* Doing kill with a non-positive pid causes messages to be
-        * sent to places we don't want. */
-       SMB_ASSERT(pid > 0);
-       return(kill(pid,0) == 0 || errno != ESRCH);
-}
-
 /*******************************************************************
  Convert a uid into a user name.
 ********************************************************************/
@@ -1876,10 +1796,10 @@ bool is_in_path(const char *name, name_compare_entry *namelist, bool case_sensit
  if possible.
 ********************************************************************/
  
-void set_namearray(name_compare_entry **ppname_array, char *namelist)
+void set_namearray(name_compare_entry **ppname_array, const char *namelist)
 {
        char *name_end;
-       char *nameptr = namelist;
+       const char *nameptr = namelist;
        int num_entries = 0;
        int i;
 
@@ -1971,42 +1891,6 @@ void free_namearray(name_compare_entry *name_array)
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_LOCKING
 
-/****************************************************************************
- Simple routine to do POSIX file locking. Cruft in NFS and 64->32 bit mapping
- is dealt with in posix.c
- Returns True if the lock was granted, False otherwise.
-****************************************************************************/
-
-bool fcntl_lock(int fd, int op, SMB_OFF_T offset, SMB_OFF_T count, int type)
-{
-       SMB_STRUCT_FLOCK lock;
-       int ret;
-
-       DEBUG(8,("fcntl_lock fd=%d op=%d offset=%.0f count=%.0f type=%d\n",
-               fd,op,(double)offset,(double)count,type));
-
-       lock.l_type = type;
-       lock.l_whence = SEEK_SET;
-       lock.l_start = offset;
-       lock.l_len = count;
-       lock.l_pid = 0;
-
-       ret = sys_fcntl_ptr(fd,op,&lock);
-
-       if (ret == -1) {
-               int sav = errno;
-               DEBUG(3,("fcntl_lock: lock failed at offset %.0f count %.0f op %d type %d (%s)\n",
-                       (double)offset,(double)count,op,type,strerror(errno)));
-               errno = sav;
-               return False;
-       }
-
-       /* everything went OK */
-       DEBUG(8,("fcntl_lock: Lock call successful\n"));
-
-       return True;
-}
-
 /****************************************************************************
  Simple routine to query existing file locks. Cruft in NFS and 64->32 bit mapping
  is dealt with in posix.c
@@ -2104,7 +1988,7 @@ 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 )
-               set_remote_arch( RA_WINXP );
+               set_remote_arch( RA_WINXP64 );
        else if ( strcmp( native_lanman, "Windows Server 2003 5.2" ) == 0 )
                set_remote_arch( RA_WIN2K3 );
 }
@@ -2145,6 +2029,9 @@ void set_remote_arch(enum remote_arch_types type)
        case RA_WINXP:
                remote_arch_str = "WinXP";
                break;
+       case RA_WINXP64:
+               remote_arch_str = "WinXP64";
+               break;
        case RA_WIN2K3:
                remote_arch_str = "Win2K3";
                break;
@@ -2176,68 +2063,12 @@ enum remote_arch_types get_remote_arch(void)
        return ra_type;
 }
 
-void print_asc(int level, const unsigned char *buf,int len)
-{
-       int i;
-       for (i=0;i<len;i++)
-               DEBUG(level,("%c", isprint(buf[i])?buf[i]:'.'));
-}
-
-void dump_data(int level, const unsigned char *buf1,int len)
+const char *tab_depth(int level, int depth)
 {
-       const unsigned char *buf = (const unsigned char *)buf1;
-       int i=0;
-       if (len<=0) return;
-
-       if (!DEBUGLVL(level)) return;
-       
-       DEBUGADD(level,("[%03X] ",i));
-       for (i=0;i<len;) {
-               DEBUGADD(level,("%02X ",(int)buf[i]));
-               i++;
-               if (i%8 == 0) DEBUGADD(level,(" "));
-               if (i%16 == 0) {      
-                       print_asc(level,&buf[i-16],8); DEBUGADD(level,(" "));
-                       print_asc(level,&buf[i-8],8); DEBUGADD(level,("\n"));
-                       if (i<len) DEBUGADD(level,("[%03X] ",i));
-               }
-       }
-       if (i%16) {
-               int n;
-               n = 16 - (i%16);
-               DEBUGADD(level,(" "));
-               if (n>8) DEBUGADD(level,(" "));
-               while (n--) DEBUGADD(level,("   "));
-               n = MIN(8,i%16);
-               print_asc(level,&buf[i-(i%16)],n); DEBUGADD(level,( " " ));
-               n = (i%16) - n;
-               if (n>0) print_asc(level,&buf[i-n],n);
-               DEBUGADD(level,("\n"));
-       }
-}
-
-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 fstring spaces;
-       size_t len = depth * 4;
-       if (len > sizeof(fstring)-1) {
-               len = sizeof(fstring)-1;
+       if( CHECK_DEBUGLVL(level) ) {
+               dbgtext("%*s", depth*4, "");
        }
-
-       memset(spaces, ' ', len);
-       spaces[len] = 0;
-       return spaces;
+       return "";
 }
 
 /*****************************************************************************
@@ -2396,78 +2227,6 @@ void *smb_xmalloc_array(size_t size, unsigned int count)
        return p;
 }
 
-/**
- Memdup with smb_panic on fail.
-**/
-
-void *smb_xmemdup(const void *p, size_t size)
-{
-       void *p2;
-       p2 = SMB_XMALLOC_ARRAY(unsigned char,size);
-       memcpy(p2, p, size);
-       return p2;
-}
-
-/**
- strdup that aborts on malloc fail.
-**/
-
-char *smb_xstrdup(const char *s)
-{
-#if defined(PARANOID_MALLOC_CHECKER)
-#ifdef strdup
-#undef strdup
-#endif
-#endif
-
-#ifndef HAVE_STRDUP
-#define strdup rep_strdup
-#endif
-
-       char *s1 = strdup(s);
-#if defined(PARANOID_MALLOC_CHECKER)
-#ifdef strdup
-#undef strdup
-#endif
-#define strdup(s) __ERROR_DONT_USE_STRDUP_DIRECTLY
-#endif
-       if (!s1) {
-               smb_panic("smb_xstrdup: malloc failed");
-       }
-       return s1;
-
-}
-
-/**
- strndup that aborts on malloc fail.
-**/
-
-char *smb_xstrndup(const char *s, size_t n)
-{
-#if defined(PARANOID_MALLOC_CHECKER)
-#ifdef strndup
-#undef strndup
-#endif
-#endif
-
-#if (defined(BROKEN_STRNDUP) || !defined(HAVE_STRNDUP))
-#undef HAVE_STRNDUP
-#define strndup rep_strndup
-#endif
-
-       char *s1 = strndup(s, n);
-#if defined(PARANOID_MALLOC_CHECKER)
-#ifdef strndup
-#undef strndup
-#endif
-#define strndup(s,n) __ERROR_DONT_USE_STRNDUP_DIRECTLY
-#endif
-       if (!s1) {
-               smb_panic("smb_xstrndup: malloc failed");
-       }
-       return s1;
-}
-
 /*
   vasprintf that aborts on malloc fail
 */
@@ -2477,31 +2236,16 @@ char *smb_xstrndup(const char *s, size_t n)
        int n;
        va_list ap2;
 
-       VA_COPY(ap2, ap);
+       va_copy(ap2, ap);
 
        n = vasprintf(ptr, format, ap2);
        if (n == -1 || ! *ptr) {
                smb_panic("smb_xvasprintf: out of memory");
        }
+       va_end(ap2);
        return n;
 }
 
-/*****************************************************************
- Like strdup but for memory.
-*****************************************************************/
-
-void *memdup(const void *p, size_t size)
-{
-       void *p2;
-       if (size == 0)
-               return NULL;
-       p2 = SMB_MALLOC(size);
-       if (!p2)
-               return NULL;
-       memcpy(p2, p, size);
-       return p2;
-}
-
 /*****************************************************************
  Get local hostname and cache result.
 *****************************************************************/
@@ -2512,7 +2256,7 @@ char *myhostname(void)
        if (ret == NULL) {
                /* This is cached forever so
                 * use NULL talloc ctx. */
-               ret = get_myname(NULL);
+               ret = talloc_get_myname(NULL);
        }
        return ret;
 }
@@ -2531,7 +2275,7 @@ static char *xx_path(const char *name, const char *rootpath)
        }
        trim_string(fname,"","/");
 
-       if (!directory_exist(fname,NULL)) {
+       if (!directory_exist(fname)) {
                mkdir(fname,0755);
        }
 
@@ -2572,6 +2316,19 @@ char *lib_path(const char *name)
        return talloc_asprintf(talloc_tos(), "%s/%s", get_dyn_LIBDIR(), name);
 }
 
+/**
+ * @brief Returns an absolute path to a file in the Samba modules directory.
+ *
+ * @param name File to find, relative to MODULESDIR.
+ *
+ * @retval Pointer to a string containing the full path.
+ **/
+
+char *modules_path(const char *name)
+{
+       return talloc_asprintf(talloc_tos(), "%s/%s", get_dyn_MODULESDIR(), name);
+}
+
 /**
  * @brief Returns an absolute path to a file in the Samba data directory.
  *
@@ -2929,22 +2686,29 @@ bool name_to_fqdn(fstring fqdn, const char *name)
 }
 
 /**********************************************************************
- Extension to talloc_get_type: Abort on type mismatch
+ Append a DATA_BLOB to a talloc'ed object
 ***********************************************************************/
 
-void *talloc_check_name_abort(const void *ptr, const char *name)
+void *talloc_append_blob(TALLOC_CTX *mem_ctx, void *buf, DATA_BLOB blob)
 {
-       void *result;
+       size_t old_size = 0;
+       char *result;
 
-       result = talloc_check_name(ptr, name);
-       if (result != NULL)
-               return result;
+       if (blob.length == 0) {
+               return buf;
+       }
 
-       DEBUG(0, ("Talloc type mismatch, expected %s, got %s\n",
-                 name, talloc_get_name(ptr)));
-       smb_panic("talloc type mismatch");
-       /* Keep the compiler happy */
-       return NULL;
+       if (buf != NULL) {
+               old_size = talloc_get_size(buf);
+       }
+
+       result = (char *)TALLOC_REALLOC(mem_ctx, buf, old_size + blob.length);
+       if (result == NULL) {
+               return NULL;
+       }
+
+       memcpy(result + old_size, blob.data, blob.length);
+       return result;
 }
 
 uint32 map_share_mode_to_deny_mode(uint32 share_access, uint32 private_options)
@@ -3044,7 +2808,7 @@ struct server_id interpret_pid(const char *pid_string)
                result.pid = pid;
        }
        else if (sscanf(pid_string, "%u", &pid) == 1) {
-               result.vnn = NONCLUSTER_VNN;
+               result.vnn = get_my_vnn();
                result.pid = pid;
        }
        else {
@@ -3197,6 +2961,30 @@ int get_safe_IVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, i
        return IVAL(ptr,off);
 }
 
+/****************************************************************
+ Split DOM\user into DOM and user. Do not mix with winbind variants of that
+ call (they take care of winbind separator and other winbind specific settings).
+****************************************************************/
+
+void split_domain_user(TALLOC_CTX *mem_ctx,
+                      const char *full_name,
+                      char **domain,
+                      char **user)
+{
+       const char *p = NULL;
+
+       p = strchr_m(full_name, '\\');
+
+       if (p != NULL) {
+               *domain = talloc_strndup(mem_ctx, full_name,
+                                        PTR_DIFF(p, full_name));
+               *user = talloc_strdup(mem_ctx, p+1);
+       } else {
+               *domain = talloc_strdup(mem_ctx, "");
+               *user = talloc_strdup(mem_ctx, full_name);
+       }
+}
+
 #if 0
 
 Disable these now we have checked all code paths and ensured
@@ -3301,3 +3089,130 @@ void *talloc_zeronull(const void *context, size_t size, const char *name)
        return talloc_named_const(context, size, name);
 }
 #endif
+
+/* Split a path name into filename and stream name components. Canonicalise
+ * such that an implicit $DATA token is always explicit.
+ *
+ * The "specification" of this function can be found in the
+ * run_local_stream_name() function in torture.c, I've tried those
+ * combinations against a W2k3 server.
+ */
+
+NTSTATUS split_ntfs_stream_name(TALLOC_CTX *mem_ctx, const char *fname,
+                               char **pbase, char **pstream)
+{
+       char *base = NULL;
+       char *stream = NULL;
+       char *sname; /* stream name */
+       const char *stype; /* stream type */
+
+       DEBUG(10, ("split_ntfs_stream_name called for [%s]\n", fname));
+
+       sname = strchr_m(fname, ':');
+
+       if (lp_posix_pathnames() || (sname == NULL)) {
+               if (pbase != NULL) {
+                       base = talloc_strdup(mem_ctx, fname);
+                       NT_STATUS_HAVE_NO_MEMORY(base);
+               }
+               goto done;
+       }
+
+       if (pbase != NULL) {
+               base = talloc_strndup(mem_ctx, fname, PTR_DIFF(sname, fname));
+               NT_STATUS_HAVE_NO_MEMORY(base);
+       }
+
+       sname += 1;
+
+       stype = strchr_m(sname, ':');
+
+       if (stype == NULL) {
+               sname = talloc_strdup(mem_ctx, sname);
+               stype = "$DATA";
+       }
+       else {
+               if (StrCaseCmp(stype, ":$DATA") != 0) {
+                       /*
+                        * If there is an explicit stream type, so far we only
+                        * allow $DATA. Is there anything else allowed? -- vl
+                        */
+                       DEBUG(10, ("[%s] is an invalid stream type\n", stype));
+                       TALLOC_FREE(base);
+                       return NT_STATUS_OBJECT_NAME_INVALID;
+               }
+               sname = talloc_strndup(mem_ctx, sname, PTR_DIFF(stype, sname));
+               stype += 1;
+       }
+
+       if (sname == NULL) {
+               TALLOC_FREE(base);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       if (sname[0] == '\0') {
+               /*
+                * no stream name, so no stream
+                */
+               goto done;
+       }
+
+       if (pstream != NULL) {
+               stream = talloc_asprintf(mem_ctx, "%s:%s", sname, stype);
+               if (stream == NULL) {
+                       TALLOC_FREE(sname);
+                       TALLOC_FREE(base);
+                       return NT_STATUS_NO_MEMORY;
+               }
+               /*
+                * upper-case the type field
+                */
+               strupper_m(strchr_m(stream, ':')+1);
+       }
+
+ done:
+       if (pbase != NULL) {
+               *pbase = base;
+       }
+       if (pstream != NULL) {
+               *pstream = stream;
+       }
+       return NT_STATUS_OK;
+}
+
+bool is_valid_policy_hnd(const POLICY_HND *hnd)
+{
+       POLICY_HND tmp;
+       ZERO_STRUCT(tmp);
+       return (memcmp(&tmp, hnd, sizeof(tmp)) != 0);
+}
+
+bool policy_hnd_equal(const struct policy_handle *hnd1,
+                     const struct policy_handle *hnd2)
+{
+       if (!hnd1 || !hnd2) {
+               return false;
+       }
+
+       return (memcmp(hnd1, hnd2, sizeof(*hnd1)) == 0);
+}
+
+/****************************************************************
+ strip off leading '\\' from a hostname
+****************************************************************/
+
+const char *strip_hostname(const char *s)
+{
+       if (!s) {
+               return NULL;
+       }
+
+       if (strlen_m(s) < 3) {
+               return s;
+       }
+
+       if (s[0] == '\\') s++;
+       if (s[0] == '\\') s++;
+
+       return s;
+}