s3-secrets: only include secrets.h when needed.
[samba.git] / source3 / lib / util.c
index 0fdc9956f1273eb8ffc8e8ac50e9c9c27fb53320..6770a715d8893dc0fea289db07be20981e75b520 100644 (file)
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
+#include "secrets.h"
 
 extern char *global_clobber_region_function;
 extern unsigned int global_clobber_region_line;
@@ -55,10 +56,17 @@ extern unsigned int global_clobber_region_line;
 #endif /* WITH_NISPLUS_HOME */
 #endif /* HAVE_NETGROUP && WITH_AUTOMOUNT */
 
-enum protocol_types Protocol = PROTOCOL_COREPLUS;
+static enum protocol_types Protocol = PROTOCOL_COREPLUS;
+
+enum protocol_types get_Protocol(void)
+{
+       return Protocol;
+}
 
-/* this is used by the chaining code */
-int chain_size = 0;
+void set_Protocol(enum protocol_types  p)
+{
+       Protocol = p;
+}
 
 static enum remote_arch_types ra_type = RA_UNKNOWN;
 
@@ -66,50 +74,10 @@ static enum remote_arch_types ra_type = RA_UNKNOWN;
  Definitions for all names.
 ***********************************************************************/
 
-static char *smb_myname;
-static char *smb_myworkgroup;
 static char *smb_scope;
 static int smb_num_netbios_names;
 static char **smb_my_netbios_names;
 
-/***********************************************************************
- Allocate and set myname. Ensure upper case.
-***********************************************************************/
-
-bool set_global_myname(const char *myname)
-{
-       SAFE_FREE(smb_myname);
-       smb_myname = SMB_STRDUP(myname);
-       if (!smb_myname)
-               return False;
-       strupper_m(smb_myname);
-       return True;
-}
-
-const char *global_myname(void)
-{
-       return smb_myname;
-}
-
-/***********************************************************************
- Allocate and set myworkgroup. Ensure upper case.
-***********************************************************************/
-
-bool set_global_myworkgroup(const char *myworkgroup)
-{
-       SAFE_FREE(smb_myworkgroup);
-       smb_myworkgroup = SMB_STRDUP(myworkgroup);
-       if (!smb_myworkgroup)
-               return False;
-       strupper_m(smb_myworkgroup);
-       return True;
-}
-
-const char *lp_workgroup(void)
-{
-       return smb_myworkgroup;
-}
-
 /***********************************************************************
  Allocate and set scope. Ensure upper case.
 ***********************************************************************/
@@ -177,8 +145,7 @@ static bool set_my_netbios_names(const char *name, int i)
 
 void gfree_names(void)
 {
-       SAFE_FREE( smb_myname );
-       SAFE_FREE( smb_myworkgroup );
+       gfree_netbios_names();
        SAFE_FREE( smb_scope );
        free_netbios_names_array();
        free_local_machine_name();
@@ -280,135 +247,196 @@ bool init_names(void)
   Used mainly in client tools.
 ****************************************************************************/
 
-static struct user_auth_info cmdline_auth_info = {
-       NULL,   /* username */
-       NULL,   /* password */
-       false,  /* got_pass */
-       false,  /* use_kerberos */
-       Undefined, /* signing state */
-       false,  /* smb_encrypt */
-       false   /* use machine account */
-};
+struct user_auth_info *user_auth_info_init(TALLOC_CTX *mem_ctx)
+{
+       struct user_auth_info *result;
 
-const char *get_cmdline_auth_info_username(void)
+       result = TALLOC_ZERO_P(mem_ctx, struct user_auth_info);
+       if (result == NULL) {
+               return NULL;
+       }
+
+       result->signing_state = Undefined;
+       return result;
+}
+
+const char *get_cmdline_auth_info_username(const struct user_auth_info *auth_info)
+{
+       if (!auth_info->username) {
+               return "";
+       }
+       return auth_info->username;
+}
+
+void set_cmdline_auth_info_username(struct user_auth_info *auth_info,
+                                   const char *username)
+{
+       TALLOC_FREE(auth_info->username);
+       auth_info->username = talloc_strdup(auth_info, username);
+       if (!auth_info->username) {
+               exit(ENOMEM);
+       }
+}
+
+const char *get_cmdline_auth_info_domain(const struct user_auth_info *auth_info)
 {
-       if (!cmdline_auth_info.username) {
+       if (!auth_info->domain) {
                return "";
        }
-       return cmdline_auth_info.username;
+       return auth_info->domain;
 }
 
-void set_cmdline_auth_info_username(const char *username)
+void set_cmdline_auth_info_domain(struct user_auth_info *auth_info,
+                                 const char *domain)
 {
-       SAFE_FREE(cmdline_auth_info.username);
-       cmdline_auth_info.username = SMB_STRDUP(username);
-       if (!cmdline_auth_info.username) {
+       TALLOC_FREE(auth_info->domain);
+       auth_info->domain = talloc_strdup(auth_info, domain);
+       if (!auth_info->domain) {
                exit(ENOMEM);
        }
 }
 
-const char *get_cmdline_auth_info_password(void)
+const char *get_cmdline_auth_info_password(const struct user_auth_info *auth_info)
 {
-       if (!cmdline_auth_info.password) {
+       if (!auth_info->password) {
                return "";
        }
-       return cmdline_auth_info.password;
+       return auth_info->password;
 }
 
-void set_cmdline_auth_info_password(const char *password)
+void set_cmdline_auth_info_password(struct user_auth_info *auth_info,
+                                   const char *password)
 {
-       SAFE_FREE(cmdline_auth_info.password);
-       cmdline_auth_info.password = SMB_STRDUP(password);
-       if (!cmdline_auth_info.password) {
+       TALLOC_FREE(auth_info->password);
+       if (password == NULL) {
+               password = "";
+       }
+       auth_info->password = talloc_strdup(auth_info, password);
+       if (!auth_info->password) {
                exit(ENOMEM);
        }
-       cmdline_auth_info.got_pass = true;
+       auth_info->got_pass = true;
 }
 
-bool set_cmdline_auth_info_signing_state(const char *arg)
+bool set_cmdline_auth_info_signing_state(struct user_auth_info *auth_info,
+                                        const char *arg)
 {
-       cmdline_auth_info.signing_state = -1;
+       auth_info->signing_state = -1;
        if (strequal(arg, "off") || strequal(arg, "no") ||
                        strequal(arg, "false")) {
-               cmdline_auth_info.signing_state = false;
+               auth_info->signing_state = false;
        } else if (strequal(arg, "on") || strequal(arg, "yes") ||
                        strequal(arg, "true") || strequal(arg, "auto")) {
-               cmdline_auth_info.signing_state = true;
+               auth_info->signing_state = true;
        } else if (strequal(arg, "force") || strequal(arg, "required") ||
                        strequal(arg, "forced")) {
-               cmdline_auth_info.signing_state = Required;
+               auth_info->signing_state = Required;
        } else {
                return false;
        }
        return true;
 }
 
-int get_cmdline_auth_info_signing_state(void)
+int get_cmdline_auth_info_signing_state(const struct user_auth_info *auth_info)
 {
-       return cmdline_auth_info.signing_state;
+       return auth_info->signing_state;
 }
 
-void set_cmdline_auth_info_use_kerberos(bool b)
+void set_cmdline_auth_info_use_ccache(struct user_auth_info *auth_info, bool b)
 {
-        cmdline_auth_info.use_kerberos = b;
+        auth_info->use_ccache = b;
 }
 
-bool get_cmdline_auth_info_use_kerberos(void)
+bool get_cmdline_auth_info_use_ccache(const struct user_auth_info *auth_info)
 {
-       return cmdline_auth_info.use_kerberos;
+       return auth_info->use_ccache;
+}
+
+void set_cmdline_auth_info_use_kerberos(struct user_auth_info *auth_info,
+                                       bool b)
+{
+        auth_info->use_kerberos = b;
+}
+
+bool get_cmdline_auth_info_use_kerberos(const struct user_auth_info *auth_info)
+{
+       return auth_info->use_kerberos;
+}
+
+void set_cmdline_auth_info_fallback_after_kerberos(struct user_auth_info *auth_info,
+                                       bool b)
+{
+       auth_info->fallback_after_kerberos = b;
+}
+
+bool get_cmdline_auth_info_fallback_after_kerberos(const struct user_auth_info *auth_info)
+{
+       return auth_info->fallback_after_kerberos;
 }
 
 /* This should only be used by lib/popt_common.c JRA */
-void set_cmdline_auth_info_use_krb5_ticket(void)
+void set_cmdline_auth_info_use_krb5_ticket(struct user_auth_info *auth_info)
 {
-       cmdline_auth_info.use_kerberos = true;
-       cmdline_auth_info.got_pass = true;
+       auth_info->use_kerberos = true;
+       auth_info->got_pass = true;
 }
 
 /* This should only be used by lib/popt_common.c JRA */
-void set_cmdline_auth_info_smb_encrypt(void)
+void set_cmdline_auth_info_smb_encrypt(struct user_auth_info *auth_info)
 {
-       cmdline_auth_info.smb_encrypt = true;
+       auth_info->smb_encrypt = true;
 }
 
-void set_cmdline_auth_info_use_machine_account(void)
+void set_cmdline_auth_info_use_machine_account(struct user_auth_info *auth_info)
 {
-       cmdline_auth_info.use_machine_account = true;
+       auth_info->use_machine_account = true;
 }
 
-bool get_cmdline_auth_info_got_pass(void)
+bool get_cmdline_auth_info_got_pass(const struct user_auth_info *auth_info)
 {
-       return cmdline_auth_info.got_pass;
+       return auth_info->got_pass;
 }
 
-bool get_cmdline_auth_info_smb_encrypt(void)
+bool get_cmdline_auth_info_smb_encrypt(const struct user_auth_info *auth_info)
 {
-       return cmdline_auth_info.smb_encrypt;
+       return auth_info->smb_encrypt;
 }
 
-bool get_cmdline_auth_info_use_machine_account(void)
+bool get_cmdline_auth_info_use_machine_account(const struct user_auth_info *auth_info)
 {
-       return cmdline_auth_info.use_machine_account;
+       return auth_info->use_machine_account;
 }
 
-bool get_cmdline_auth_info_copy(struct user_auth_info *info)
+struct user_auth_info *get_cmdline_auth_info_copy(TALLOC_CTX *mem_ctx,
+                                                 const struct user_auth_info *src)
 {
-       *info = cmdline_auth_info;
-       /* Now re-alloc the strings. */
-       info->username = SMB_STRDUP(get_cmdline_auth_info_username());
-       info->password = SMB_STRDUP(get_cmdline_auth_info_password());
-       if (!info->username || !info->password) {
-               return false;
+       struct user_auth_info *result;
+
+       result = user_auth_info_init(mem_ctx);
+       if (result == NULL) {
+               return NULL;
        }
-       return true;
+
+       *result = *src;
+
+       result->username = talloc_strdup(
+               result, get_cmdline_auth_info_username(src));
+       result->password = talloc_strdup(
+               result, get_cmdline_auth_info_password(src));
+       if ((result->username == NULL) || (result->password == NULL)) {
+               TALLOC_FREE(result);
+               return NULL;
+       }
+
+       return result;
 }
 
-bool set_cmdline_auth_info_machine_account_creds(void)
+bool set_cmdline_auth_info_machine_account_creds(struct user_auth_info *auth_info)
 {
        char *pass = NULL;
        char *account = NULL;
 
-       if (!get_cmdline_auth_info_use_machine_account()) {
+       if (!get_cmdline_auth_info_use_machine_account(auth_info)) {
                return false;
        }
 
@@ -430,8 +458,8 @@ bool set_cmdline_auth_info_machine_account_creds(void)
                return false;
        }
 
-       set_cmdline_auth_info_username(account);
-       set_cmdline_auth_info_password(pass);
+       set_cmdline_auth_info_username(auth_info, account);
+       set_cmdline_auth_info_password(auth_info, pass);
 
        SAFE_FREE(account);
        SAFE_FREE(pass);
@@ -439,117 +467,47 @@ bool set_cmdline_auth_info_machine_account_creds(void)
        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)
-{
-       char *p;
-       if ((p = getenv("TMPDIR")))
-               return p;
-       return "/tmp";
-}
-
 /****************************************************************************
Add a gid to an array of gids if it's not already there.
Ensure we have a password if one not given.
 ****************************************************************************/
 
-bool add_gid_to_array_unique(TALLOC_CTX *mem_ctx, gid_t gid,
-                            gid_t **gids, size_t *num_gids)
+void set_cmdline_auth_info_getpass(struct user_auth_info *auth_info)
 {
-       int i;
-
-       if ((*num_gids != 0) && (*gids == NULL)) {
-               /*
-                * A former call to this routine has failed to allocate memory
-                */
-               return False;
-       }
-
-       for (i=0; i<*num_gids; i++) {
-               if ((*gids)[i] == gid) {
-                       return True;
-               }
-       }
-
-       *gids = TALLOC_REALLOC_ARRAY(mem_ctx, *gids, gid_t, *num_gids+1);
-       if (*gids == NULL) {
-               *num_gids = 0;
-               return False;
-       }
-
-       (*gids)[*num_gids] = gid;
-       *num_gids += 1;
-       return True;
-}
-
-/****************************************************************************
- Like atoi but gets the value up to the separator character.
-****************************************************************************/
+       char *label = NULL;
+       char *pass;
+       TALLOC_CTX *frame;
 
-static const char *Atoic(const char *p, int *n, const char *c)
-{
-       if (!isdigit((int)*p)) {
-               DEBUG(5, ("Atoic: malformed number\n"));
-               return NULL;
-       }
-
-       (*n) = atoi(p);
-
-       while ((*p) && isdigit((int)*p))
-               p++;
-
-       if (strchr_m(c, *p) == NULL) {
-               DEBUG(5, ("Atoic: no separator characters (%s) not found\n", c));
-               return NULL;
+       if (get_cmdline_auth_info_got_pass(auth_info) ||
+                       get_cmdline_auth_info_use_kerberos(auth_info)) {
+               /* Already got one... */
+               return;
        }
 
-       return p;
-}
-
-/*************************************************************************
- Reads a list of numbers.
- *************************************************************************/
-
-const char *get_numlist(const char *p, uint32 **num, int *count)
-{
-       int val;
-
-       if (num == NULL || count == NULL)
-               return NULL;
-
-       (*count) = 0;
-       (*num  ) = NULL;
-
-       while ((p = Atoic(p, &val, ":,")) != NULL && (*p) != ':') {
-               *num = SMB_REALLOC_ARRAY((*num), uint32, (*count)+1);
-               if (!(*num)) {
-                       return NULL;
-               }
-               (*num)[(*count)] = val;
-               (*count)++;
-               p++;
+       frame = talloc_stackframe();
+       label = talloc_asprintf(frame, "Enter %s's password: ",
+                       get_cmdline_auth_info_username(auth_info));
+       pass = getpass(label);
+       if (pass) {
+               set_cmdline_auth_info_password(auth_info, pass);
        }
-
-       return p;
+       TALLOC_FREE(frame);
 }
 
 /*******************************************************************
  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,
+                    bool fake_dir_create_times)
 {
        SMB_STRUCT_STAT st;
        if (!sbuf)
                sbuf = &st;
-  
-       if (sys_stat(fname,sbuf) != 0) 
+
+       if (sys_stat(fname, sbuf, fake_dir_create_times) != 0)
                return(False);
 
-       return((S_ISREG(sbuf->st_mode)) || (S_ISFIFO(sbuf->st_mode)));
+       return((S_ISREG(sbuf->st_ex_mode)) || (S_ISFIFO(sbuf->st_ex_mode)));
 }
 
 /*******************************************************************
@@ -559,45 +517,19 @@ bool file_exist(const char *fname,SMB_STRUCT_STAT *sbuf)
 bool socket_exist(const char *fname)
 {
        SMB_STRUCT_STAT st;
-       if (sys_stat(fname,&st) != 0) 
+       if (sys_stat(fname, &st, false) != 0)
                return(False);
 
-       return S_ISSOCK(st.st_mode);
+       return S_ISSOCK(st.st_ex_mode);
 }
 
 /*******************************************************************
Check a files mod time.
Returns the size in bytes of the named given the stat struct.
 ********************************************************************/
 
-time_t file_modtime(const char *fname)
+uint64_t get_file_size_stat(const SMB_STRUCT_STAT *sbuf)
 {
-       SMB_STRUCT_STAT st;
-  
-       if (sys_stat(fname,&st) != 0) 
-               return(0);
-
-       return(st.st_mtime);
-}
-
-/*******************************************************************
- Check if a directory exists.
-********************************************************************/
-
-bool directory_exist(char *dname,SMB_STRUCT_STAT *st)
-{
-       SMB_STRUCT_STAT st2;
-       bool ret;
-
-       if (!st)
-               st = &st2;
-
-       if (sys_stat(dname,st) != 0) 
-               return(False);
-
-       ret = S_ISDIR(st->st_mode);
-       if(!ret)
-               errno = ENOTDIR;
-       return ret;
+       return sbuf->st_ex_size;
 }
 
 /*******************************************************************
@@ -607,10 +539,10 @@ bool directory_exist(char *dname,SMB_STRUCT_STAT *st)
 SMB_OFF_T get_file_size(char *file_name)
 {
        SMB_STRUCT_STAT buf;
-       buf.st_size = 0;
-       if(sys_stat(file_name,&buf) != 0)
+       buf.st_ex_size = 0;
+       if (sys_stat(file_name, &buf, false) != 0)
                return (SMB_OFF_T)-1;
-       return(buf.st_size);
+       return get_file_size_stat(&buf);
 }
 
 /*******************************************************************
@@ -644,7 +576,7 @@ void show_msg(char *buf)
 
        if (!DEBUGLVL(5))
                return;
-       
+
        DEBUG(5,("size=%d\nsmb_com=0x%x\nsmb_rcls=%d\nsmb_reh=%d\nsmb_err=%d\nsmb_flg=%d\nsmb_flg2=%d\n",
                        smb_len(buf),
                        (int)CVAL(buf,smb_com),
@@ -663,7 +595,7 @@ void show_msg(char *buf)
        for (i=0;i<(int)CVAL(buf,smb_wct);i++)
                DEBUGADD(5,("smb_vwv[%2d]=%5d (0x%X)\n",i,
                        SVAL(buf,smb_vwv+2*i),SVAL(buf,smb_vwv+2*i)));
-       
+
        bcc = (int)SVAL(buf,smb_vwv+2*(CVAL(buf,smb_wct)));
 
        DEBUGADD(5,("smb_bcc=%d\n",bcc));
@@ -850,43 +782,6 @@ char *clean_name(TALLOC_CTX *ctx, const char *s)
        return unix_clean_name(ctx, str);
 }
 
-/*******************************************************************
- Close the low 3 fd's and open dev/null in their place.
-********************************************************************/
-
-void close_low_fds(bool stderr_too)
-{
-#ifndef VALGRIND
-       int fd;
-       int i;
-
-       close(0);
-       close(1);
-
-       if (stderr_too)
-               close(2);
-
-       /* try and use up these file descriptors, so silly
-               library routines writing to stdout etc won't cause havoc */
-       for (i=0;i<3;i++) {
-               if (i == 2 && !stderr_too)
-                       continue;
-
-               fd = sys_open("/dev/null",O_RDWR,0);
-               if (fd < 0)
-                       fd = sys_open("/dev/null",O_WRONLY,0);
-               if (fd < 0) {
-                       DEBUG(0,("Can't open /dev/null\n"));
-                       return;
-               }
-               if (fd != i) {
-                       DEBUG(0,("Didn't get file descriptor %d\n",i));
-                       return;
-               }
-       }
-#endif
-}
-
 /*******************************************************************
  Write data into an fd at a given offset. Ignore seek errors.
 ********************************************************************/
@@ -927,36 +822,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
-}
-
 /*******************************************************************
  Sleep for a specified number of milliseconds.
 ********************************************************************/
@@ -981,7 +846,7 @@ void smb_msleep(unsigned int t)
 
        GetTimeOfDay(&t1);
        t2 = t1;
-  
+
        while (tdiff < t) {
                tval.tv_sec = (t-tdiff)/1000;
                tval.tv_usec = 1000*((t-tdiff)%1000);
@@ -1007,40 +872,12 @@ void smb_msleep(unsigned int t)
 #endif
 }
 
-/****************************************************************************
- Become a daemon, discarding the controlling terminal.
-****************************************************************************/
-
-void become_daemon(bool Fork, bool no_process_group)
+NTSTATUS reinit_after_fork(struct messaging_context *msg_ctx,
+                          struct event_context *ev_ctx,
+                          struct server_id id,
+                          bool parent_longlived)
 {
-       if (Fork) {
-               if (sys_fork()) {
-                       _exit(0);
-               }
-       }
-
-  /* detach from the terminal */
-#ifdef HAVE_SETSID
-       if (!no_process_group) setsid();
-#elif defined(TIOCNOTTY)
-       if (!no_process_group) {
-               int i = sys_open("/dev/tty", O_RDWR, 0);
-               if (i != -1) {
-                       ioctl(i, (int) TIOCNOTTY, (char *)0);      
-                       close(i);
-               }
-       }
-#endif /* HAVE_SETSID */
-
-       /* Close fd's 0,1,2. Needed if started by rsh */
-       close_low_fds(False);  /* Don't close stderr, let the debug system
-                                 attach it to the logfile */
-}
-
-bool reinit_after_fork(struct messaging_context *msg_ctx,
-                      bool parent_longlived)
-{
-       NTSTATUS status;
+       NTSTATUS status = NT_STATUS_OK;
 
        /* Reset the state of the random
         * number generation system, so
@@ -1051,39 +888,27 @@ bool reinit_after_fork(struct messaging_context *msg_ctx,
        /* tdb needs special fork handling */
        if (tdb_reopen_all(parent_longlived ? 1 : 0) == -1) {
                DEBUG(0,("tdb_reopen_all failed.\n"));
-               return false;
+               status = NT_STATUS_OPEN_FAILED;
+               goto done;
        }
 
-       /*
-        * 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;
+       if (ev_ctx && tevent_re_initialise(ev_ctx) != 0) {
+               smb_panic(__location__ ": Failed to re-initialise event context");
        }
 
-       return true;
-}
-
-/****************************************************************************
- Put up a yes/no prompt.
-****************************************************************************/
-
-bool yesno(const char *p)
-{
-       char ans[20];
-       printf("%s",p);
-
-       if (!fgets(ans,sizeof(ans)-1,stdin))
-               return(False);
-
-       if (*ans == 'y' || *ans == 'Y')
-               return(True);
-
-       return(False);
+       if (msg_ctx) {
+               /*
+                * For clustering, we need to re-init our ctdbd connection after the
+                * fork
+                */
+               status = messaging_reinit(msg_ctx, id);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(0,("messaging_reinit() failed: %s\n",
+                                nt_errstr(status)));
+               }
+       }
+ done:
+       return status;
 }
 
 #if defined(PARANOID_MALLOC_CHECKER)
@@ -1129,26 +954,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
 ****************************************************************************/
@@ -1249,21 +1054,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.
 ****************************************************************************/
@@ -1319,46 +1109,6 @@ 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 *p;
-       char hostname[HOST_NAME_MAX];
-
-       *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';
-
-       /* split off any parts after an initial . */
-       p = strchr_m(hostname,'.');
-       if (p) {
-               *p = 0;
-       }
-
-       return talloc_strdup(ctx, hostname);
-}
-
 /****************************************************************************
  Get my own domain name, or "" if we have none.
 ****************************************************************************/
@@ -1400,9 +1150,9 @@ int interpret_protocol(const char *str,int def)
                return(PROTOCOL_COREPLUS);
        if (strequal(str,"CORE+"))
                return(PROTOCOL_COREPLUS);
-  
+
        DEBUG(0,("Unrecognised protocol level %s\n",str));
-  
+
        return(def);
 }
 
@@ -1505,6 +1255,9 @@ char *automount_lookup(TALLOC_CTX *ctx, const char *user_name)
        if ((nis_error = yp_match(nis_domain, nis_map, user_name,
                                        strlen(user_name), &nis_result,
                                        &nis_result_len)) == 0) {
+               if (nis_result_len > 0 && nis_result[nis_result_len] == '\n') {
+                       nis_result[nis_result_len] = '\0';
+               }
                value = talloc_strdup(ctx, nis_result);
                if (!value) {
                        return NULL;
@@ -1542,21 +1295,13 @@ bool process_exists(const struct server_id pid)
        }
 
 #ifdef CLUSTER_SUPPORT
-       return ctdbd_process_exists(messaging_ctdbd_connection(), pid.vnn,
-                                   pid.pid);
+       return ctdbd_process_exists(messaging_ctdbd_connection(procid_self()),
+                                   pid.vnn, pid.pid);
 #else
        return False;
 #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.
 ********************************************************************/
@@ -1608,7 +1353,7 @@ uid_t nametouid(const char *name)
        char *p;
        uid_t u;
 
-       pass = getpwnam_alloc(NULL, name);
+       pass = getpwnam_alloc(talloc_autofree_context(), name);
        if (pass) {
                u = pass->pw_uid;
                TALLOC_FREE(pass);
@@ -1768,7 +1513,7 @@ libunwind_failed:
 
        DEBUG(0, ("BACKTRACE: %lu stack frames:\n", 
                  (unsigned long)backtrace_size));
-       
+
        if (backtrace_strings) {
                int i;
 
@@ -1832,7 +1577,7 @@ const char *readdirname(SMB_STRUCT_DIR *p)
 
        if (!p)
                return(NULL);
-  
+
        ptr = (SMB_STRUCT_DIRENT *)sys_readdir(p);
        if (!ptr)
                return(NULL);
@@ -1906,11 +1651,11 @@ bool is_in_path(const char *name, name_compare_entry *namelist, bool case_sensit
  remove a potentially expensive call to mask_match
  if possible.
 ********************************************************************/
+
 void set_namearray(name_compare_entry **ppname_array, const char *namelist)
 {
        char *name_end;
-       const char *nameptr = namelist;
+       char *nameptr = (char *)namelist;
        int num_entries = 0;
        int i;
 
@@ -1930,12 +1675,14 @@ void set_namearray(name_compare_entry **ppname_array, const char *namelist)
                        nameptr++;
                        continue;
                }
-               /* find the next / */
-               name_end = strchr_m(nameptr, '/');
+               /* anything left? */
+               if ( *nameptr == '\0' )
+                       break;
 
-               /* oops - the last check for a / didn't find one. */
+               /* find the next '/' or consume remaining */
+               name_end = strchr_m(nameptr, '/');
                if (name_end == NULL)
-                       break;
+                       name_end = (char *)nameptr + strlen(nameptr);
 
                /* next segment please */
                nameptr = name_end + 1;
@@ -1951,7 +1698,7 @@ void set_namearray(name_compare_entry **ppname_array, const char *namelist)
        }
 
        /* Now copy out the names */
-       nameptr = namelist;
+       nameptr = (char *)namelist;
        i = 0;
        while(*nameptr) {
                if ( *nameptr == '/' ) {
@@ -1959,14 +1706,17 @@ void set_namearray(name_compare_entry **ppname_array, const char *namelist)
                        nameptr++;
                        continue;
                }
-               /* find the next / */
-               if ((name_end = strchr_m(nameptr, '/')) != NULL)
-                       *name_end = 0;
-
-               /* oops - the last check for a / didn't find one. */
-               if(name_end == NULL) 
+               /* anything left? */
+               if ( *nameptr == '\0' )
                        break;
 
+               /* find the next '/' or consume remaining */
+               name_end = strchr_m(nameptr, '/');
+               if (name_end)
+                       *name_end = '\0';
+               else
+                       name_end = nameptr + strlen(nameptr);
+
                (*ppname_array)[i].is_wild = ms_has_wild(nameptr);
                if(((*ppname_array)[i].name = SMB_STRDUP(nameptr)) == NULL) {
                        DEBUG(0,("set_namearray: malloc fail (1)\n"));
@@ -1977,7 +1727,7 @@ void set_namearray(name_compare_entry **ppname_array, const char *namelist)
                nameptr = name_end + 1;
                i++;
        }
-  
+
        (*ppname_array)[i].name = NULL;
 
        return;
@@ -2002,42 +1752,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
@@ -2073,7 +1787,7 @@ bool fcntl_getlock(int fd, SMB_OFF_T *poffset, SMB_OFF_T *pcount, int *ptype, pi
        *poffset = lock.l_start;
        *pcount = lock.l_len;
        *ppid = lock.l_pid;
-       
+
        DEBUG(3,("fcntl_getlock: fd %d is returned info %d pid %u\n",
                        fd, (int)lock.l_type, (unsigned int)lock.l_pid));
        return True;
@@ -2210,57 +1924,6 @@ 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 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
-}
-
 const char *tab_depth(int level, int depth)
 {
        if( CHECK_DEBUGLVL(level) ) {
@@ -2386,24 +2049,6 @@ int set_maxfiles(int requested_max)
 #endif
 }
 
-/*****************************************************************
- Possibly replace mkstemp if it is broken.
-*****************************************************************/  
-
-int smb_mkstemp(char *name_template)
-{
-#if HAVE_SECURE_MKSTEMP
-       return mkstemp(name_template);
-#else
-       /* have a reasonable go at emulating it. Hope that
-          the system mktemp() isn't completly hopeless */
-       char *p = mktemp(name_template);
-       if (!p)
-               return -1;
-       return open(p, O_CREAT|O_EXCL|O_RDWR, 0600);
-#endif
-}
-
 /*****************************************************************
  malloc that aborts with smb_panic on fail or zero size.
  *****************************************************************/  
@@ -2425,78 +2070,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
 */
@@ -2506,32 +2079,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);
+       va_end(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.
 *****************************************************************/
@@ -2541,15 +2098,20 @@ char *myhostname(void)
        static char *ret;
        if (ret == NULL) {
                /* This is cached forever so
-                * use NULL talloc ctx. */
-               ret = get_myname(NULL);
+                * use talloc_autofree_context() ctx. */
+               ret = get_myname(talloc_autofree_context());
        }
        return ret;
 }
 
-/*****************************************************************
- A useful function for returning a path in the Samba pid directory.
-*****************************************************************/
+/**
+ * @brief Returns an absolute path to a file concatenating the provided
+ * @a rootpath and @a basename
+ *
+ * @param name Filename, relative to @a rootpath
+ *
+ * @retval Pointer to a string containing the full path.
+ **/
 
 static char *xx_path(const char *name, const char *rootpath)
 {
@@ -2561,8 +2123,10 @@ static char *xx_path(const char *name, const char *rootpath)
        }
        trim_string(fname,"","/");
 
-       if (!directory_exist(fname,NULL)) {
-               mkdir(fname,0755);
+       if (!directory_exist(fname)) {
+               if (!mkdir(fname,0755))
+                       DEBUG(1, ("Unable to create directory %s for file %s. "
+                             "Error was %s\n", fname, name, strerror(errno)));
        }
 
        return talloc_asprintf(talloc_tos(),
@@ -2571,18 +2135,26 @@ static char *xx_path(const char *name, const char *rootpath)
                                name);
 }
 
-/*****************************************************************
- A useful function for returning a path in the Samba lock directory.
-*****************************************************************/
+/**
+ * @brief Returns an absolute path to a file in the Samba lock directory.
+ *
+ * @param name File to find, relative to LOCKDIR.
+ *
+ * @retval Pointer to a talloc'ed string containing the full path.
+ **/
 
 char *lock_path(const char *name)
 {
        return xx_path(name, lp_lockdir());
 }
 
-/*****************************************************************
- A useful function for returning a path in the Samba pid directory.
-*****************************************************************/
+/**
+ * @brief Returns an absolute path to a file in the Samba pid directory.
+ *
+ * @param name File to find, relative to PIDDIR.
+ *
+ * @retval Pointer to a talloc'ed string containing the full path.
+ **/
 
 char *pid_path(const char *name)
 {
@@ -2602,6 +2174,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.
  *
@@ -2615,13 +2200,30 @@ char *data_path(const char *name)
        return talloc_asprintf(talloc_tos(), "%s/%s", get_dyn_CODEPAGEDIR(), name);
 }
 
-/*****************************************************************
-a useful function for returning a path in the Samba state directory
- *****************************************************************/
+/**
+ * @brief Returns an absolute path to a file in the Samba state directory.
+ *
+ * @param name File to find, relative to STATEDIR.
+ *
+ * @retval Pointer to a talloc'ed string containing the full path.
+ **/
 
 char *state_path(const char *name)
 {
-       return xx_path(name, get_dyn_STATEDIR());
+       return xx_path(name, lp_statedir());
+}
+
+/**
+ * @brief Returns an absolute path to a file in the Samba cache directory.
+ *
+ * @param name File to find, relative to CACHEDIR.
+ *
+ * @retval Pointer to a talloc'ed string containing the full path.
+ **/
+
+char *cache_path(const char *name)
+{
+       return xx_path(name, lp_cachedir());
 }
 
 /**
@@ -2637,27 +2239,14 @@ const char *shlib_ext(void)
 
 /*******************************************************************
  Given a filename - get its directory name
- NB: Returned in static storage.  Caveats:
- o  If caller wishes to preserve, they should copy.
 ********************************************************************/
 
-char *parent_dirname(const char *path)
-{
-       char *parent;
-
-       if (!parent_dirname_talloc(talloc_tos(), path, &parent, NULL)) {
-               return NULL;
-       }
-
-       return parent;
-}
-
-bool parent_dirname_talloc(TALLOC_CTX *mem_ctx, const char *dir,
-                          char **parent, const char **name)
+bool parent_dirname(TALLOC_CTX *mem_ctx, const char *dir, char **parent,
+                   const char **name)
 {
        char *p;
        ptrdiff_t len;
+
        p = strrchr_m(dir, '/'); /* Find final '/', if any */
 
        if (p == NULL) {
@@ -2665,17 +2254,16 @@ bool parent_dirname_talloc(TALLOC_CTX *mem_ctx, const char *dir,
                        return False;
                }
                if (name) {
-                       *name = "";
+                       *name = dir;
                }
                return True;
        }
 
        len = p-dir;
 
-       if (!(*parent = TALLOC_ARRAY(mem_ctx, char, len+1))) {
+       if (!(*parent = (char *)TALLOC_MEMDUP(mem_ctx, dir, len+1))) {
                return False;
        }
-       memcpy(*parent, dir, len);
        (*parent)[len] = '\0';
 
        if (name) {
@@ -2734,11 +2322,11 @@ bool ms_has_wild_w(const smb_ucs2_t *s)
 
 bool mask_match(const char *string, const char *pattern, bool is_case_sensitive)
 {
-       if (strcmp(string,"..") == 0)
+       if (ISDOTDOT(string))
                string = ".";
-       if (strcmp(pattern,".") == 0)
+       if (ISDOT(pattern))
                return False;
-       
+
        return ms_fnmatch(pattern, string, Protocol <= PROTOCOL_LANMAN2, is_case_sensitive) == 0;
 }
 
@@ -2750,11 +2338,11 @@ bool mask_match(const char *string, const char *pattern, bool is_case_sensitive)
 
 bool mask_match_search(const char *string, const char *pattern, bool is_case_sensitive)
 {
-       if (strcmp(string,"..") == 0)
+       if (ISDOTDOT(string))
                string = ".";
-       if (strcmp(pattern,".") == 0)
+       if (ISDOT(pattern))
                return False;
-       
+
        return ms_fnmatch(pattern, string, True, is_case_sensitive) == 0;
 }
 
@@ -2959,22 +2547,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)
@@ -3016,10 +2611,18 @@ uint32 get_my_vnn(void)
        return my_vnn;
 }
 
+static uint64_t my_unique_id = 0;
+
+void set_my_unique_id(uint64_t unique_id)
+{
+       my_unique_id = unique_id;
+}
+
 struct server_id pid_to_procid(pid_t pid)
 {
        struct server_id result;
        result.pid = pid;
+       result.unique_id = my_unique_id;
 #ifdef CLUSTER_SUPPORT
        result.vnn = my_vnn;
 #endif
@@ -3031,11 +2634,6 @@ struct server_id procid_self(void)
        return pid_to_procid(sys_getpid());
 }
 
-struct server_id server_id_self(void)
-{
-       return procid_self();
-}
-
 bool procid_equal(const struct server_id *p1, const struct server_id *p2)
 {
        if (p1->pid != p2->pid)
@@ -3066,14 +2664,15 @@ bool procid_is_me(const struct server_id *pid)
 
 struct server_id interpret_pid(const char *pid_string)
 {
-#ifdef CLUSTER_SUPPORT
-       unsigned int vnn, pid;
        struct server_id result;
-       if (sscanf(pid_string, "%u:%u", &vnn, &pid) == 2) {
+       int pid;
+#ifdef CLUSTER_SUPPORT
+       unsigned int vnn;
+       if (sscanf(pid_string, "%u:%d", &vnn, &pid) == 2) {
                result.vnn = vnn;
                result.pid = pid;
        }
-       else if (sscanf(pid_string, "%u", &pid) == 1) {
+       else if (sscanf(pid_string, "%d", &pid) == 1) {
                result.vnn = get_my_vnn();
                result.pid = pid;
        }
@@ -3081,10 +2680,20 @@ struct server_id interpret_pid(const char *pid_string)
                result.vnn = NONCLUSTER_VNN;
                result.pid = -1;
        }
-       return result;
 #else
-       return pid_to_procid(atoi(pid_string));
+       if (sscanf(pid_string, "%d", &pid) != 1) {
+               result.pid = -1;
+       } else {
+               result.pid = pid;
+       }
 #endif
+       /* Assigning to result.pid may have overflowed
+          Map negative pid to -1: i.e. error */
+       if (result.pid < 0) {
+               result.pid = -1;
+       }
+       result.unique_id = 0;
+       return result;
 }
 
 char *procid_str(TALLOC_CTX *mem_ctx, const struct server_id *pid)
@@ -3144,6 +2753,25 @@ int this_is_smp(void)
 #endif
 }
 
+/****************************************************************
+ Check if offset/length fit into bufsize. Should probably be
+ merged with is_offset_safe, but this would require a rewrite
+ of lanman.c. Later :-)
+****************************************************************/
+
+bool trans_oob(uint32_t bufsize, uint32_t offset, uint32_t length)
+{
+       if ((offset + length < offset) || (offset + length < length)) {
+               /* wrap */
+               return true;
+       }
+       if ((offset > bufsize) || (offset + length > bufsize)) {
+               /* overflow */
+               return true;
+       }
+       return false;
+}
+
 /****************************************************************
  Check if an offset into a buffer is safe.
  If this returns True it's safe to indirect into the byte at
@@ -3356,99 +2984,9 @@ void *talloc_zeronull(const void *context, size_t size, const char *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)
+bool is_valid_policy_hnd(const struct policy_handle *hnd)
 {
-       POLICY_HND tmp;
+       struct policy_handle tmp;
        ZERO_STRUCT(tmp);
        return (memcmp(&tmp, hnd, sizeof(tmp)) != 0);
 }
@@ -3482,3 +3020,14 @@ const char *strip_hostname(const char *s)
 
        return s;
 }
+
+bool tevent_req_poll_ntstatus(struct tevent_req *req,
+                             struct tevent_context *ev,
+                             NTSTATUS *status)
+{
+       bool ret = tevent_req_poll(req, ev);
+       if (!ret) {
+               *status = map_nt_error_from_unix(errno);
+       }
+       return ret;
+}