s3:vfs: Correctly check if OFD locks should be enabled or not
[kai/samba-autobuild/.git] / source3 / lib / util.c
index dcc02b5bafa97b9c7bc7c1288da3d49342520e21..7530ea6797301e474e54b496b79dc66640f1b8c6 100644 (file)
 #include "includes.h"
 #include "system/passwd.h"
 #include "system/filesys.h"
+#include "lib/util/server_id.h"
 #include "util_tdb.h"
 #include "ctdbd_conn.h"
 #include "../lib/util/util_pw.h"
 #include "messages.h"
-#include <ccan/hash/hash.h>
+#include "messages_dgm.h"
 #include "libcli/security/security.h"
+#include "serverid.h"
+#include "lib/util/sys_rw.h"
+#include "lib/util/sys_rw_data.h"
+#include "lib/util/util_process.h"
+#include "lib/dbwrap/dbwrap_ctdb.h"
+#include "lib/gencache.h"
 
 #ifdef HAVE_SYS_PRCTL_H
 #include <sys/prctl.h>
 #define MAX_ALLOC_SIZE (1024*1024*256)
 
 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
+/* rpc/xdr.h uses TRUE and FALSE */
+#ifdef TRUE
+#undef TRUE
+#endif
+
+#ifdef FALSE
+#undef FALSE
+#endif
+
+#include "system/nis.h"
+
 #ifdef WITH_NISPLUS_HOME
 #ifdef BROKEN_NISPLUS_INCLUDE_FILES
 /*
@@ -126,6 +144,35 @@ uint64_t get_file_size_stat(const SMB_STRUCT_STAT *sbuf)
        return sbuf->st_ex_size;
 }
 
+/****************************************************************************
+ Check two stats have identical dev and ino fields.
+****************************************************************************/
+
+bool check_same_dev_ino(const SMB_STRUCT_STAT *sbuf1,
+                        const SMB_STRUCT_STAT *sbuf2)
+{
+       if (sbuf1->st_ex_dev != sbuf2->st_ex_dev ||
+                       sbuf1->st_ex_ino != sbuf2->st_ex_ino) {
+               return false;
+       }
+       return true;
+}
+
+/****************************************************************************
+ Check if a stat struct is identical for use.
+****************************************************************************/
+
+bool check_same_stat(const SMB_STRUCT_STAT *sbuf1,
+                       const SMB_STRUCT_STAT *sbuf2)
+{
+       if (sbuf1->st_ex_uid != sbuf2->st_ex_uid ||
+                       sbuf1->st_ex_gid != sbuf2->st_ex_gid ||
+                       !check_same_dev_ino(sbuf1, sbuf2)) {
+               return false;
+       }
+       return true;
+}
+
 /*******************************************************************
  Show a smb message structure.
 ********************************************************************/
@@ -167,7 +214,7 @@ void show_msg(const char *buf)
        if (DEBUGLEVEL < 50)
                bcc = MIN(bcc, 512);
 
-       dump_data(10, (const uint8 *)smb_buf_const(buf), bcc);
+       dump_data(10, (const uint8_t *)smb_buf_const(buf), bcc);
 }
 
 /*******************************************************************
@@ -187,12 +234,12 @@ int set_message_bcc(char *buf,int num_bytes)
  Return the bytes added
 ********************************************************************/
 
-ssize_t message_push_blob(uint8 **outbuf, DATA_BLOB blob)
+ssize_t message_push_blob(uint8_t **outbuf, DATA_BLOB blob)
 {
        size_t newlen = smb_len(*outbuf) + 4 + blob.length;
-       uint8 *tmp;
+       uint8_t *tmp;
 
-       if (!(tmp = talloc_realloc(NULL, *outbuf, uint8, newlen))) {
+       if (!(tmp = talloc_realloc(NULL, *outbuf, uint8_t, newlen))) {
                DEBUG(0, ("talloc failed\n"));
                return -1;
        }
@@ -320,12 +367,12 @@ char *clean_name(TALLOC_CTX *ctx, const char *s)
  Write data into an fd at a given offset. Ignore seek errors.
 ********************************************************************/
 
-ssize_t write_data_at_offset(int fd, const char *buffer, size_t N, SMB_OFF_T pos)
+ssize_t write_data_at_offset(int fd, const char *buffer, size_t N, off_t pos)
 {
        size_t total=0;
        ssize_t ret;
 
-       if (pos == (SMB_OFF_T)-1) {
+       if (pos == (off_t)-1) {
                return write_data(fd, buffer, N);
        }
 #if defined(HAVE_PWRITE) || defined(HAVE_PRWITE64)
@@ -392,28 +439,32 @@ static void reinit_after_fork_pipe_handler(struct tevent_context *ev,
                 * we have reached EOF on stdin, which means the
                 * parent has exited. Shutdown the server
                 */
+               TALLOC_FREE(fde);
                (void)kill(getpid(), SIGTERM);
        }
 }
 
 
 NTSTATUS reinit_after_fork(struct messaging_context *msg_ctx,
-                          struct event_context *ev_ctx,
-                          bool parent_longlived)
+                          struct tevent_context *ev_ctx,
+                          bool parent_longlived,
+                          const char *comment)
 {
        NTSTATUS status = NT_STATUS_OK;
+       int ret;
+
+       /*
+        * The main process thread should never
+        * allow per_thread_cwd_enable() to be
+        * called.
+        */
+       per_thread_cwd_disable();
 
        if (reinit_after_fork_pipe[1] != -1) {
                close(reinit_after_fork_pipe[1]);
                reinit_after_fork_pipe[1] = -1;
        }
 
-       /* 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) != 0) {
                DEBUG(0,("tdb_reopen_all failed.\n"));
@@ -421,8 +472,11 @@ NTSTATUS reinit_after_fork(struct messaging_context *msg_ctx,
                goto done;
        }
 
-       if (ev_ctx && tevent_re_initialise(ev_ctx) != 0) {
-               smb_panic(__location__ ": Failed to re-initialise event context");
+       if (ev_ctx != NULL) {
+               tevent_set_trace_callback(ev_ctx, NULL, NULL);
+               if (tevent_re_initialise(ev_ctx) != 0) {
+                       smb_panic(__location__ ": Failed to re-initialise event context");
+               }
        }
 
        if (reinit_after_fork_pipe[0] != -1) {
@@ -446,7 +500,22 @@ NTSTATUS reinit_after_fork(struct messaging_context *msg_ctx,
                        DEBUG(0,("messaging_reinit() failed: %s\n",
                                 nt_errstr(status)));
                }
+
+               if (lp_clustering()) {
+                       ret = ctdb_async_ctx_reinit(
+                               NULL, messaging_tevent_context(msg_ctx));
+                       if (ret != 0) {
+                               DBG_ERR("db_ctdb_async_ctx_reinit failed: %s\n",
+                                       strerror(errno));
+                               return map_nt_error_from_unix(ret);
+                       }
+               }
        }
+
+       if (comment) {
+               prctl_set_comment(comment);
+       }
+
  done:
        return status;
 }
@@ -456,7 +525,7 @@ NTSTATUS reinit_after_fork(struct messaging_context *msg_ctx,
 ****************************************************************************/
 
 void add_to_large_array(TALLOC_CTX *mem_ctx, size_t element_size,
-                       void *element, void *_array, uint32 *num_elements,
+                       void *element, void *_array, uint32_t *num_elements,
                        ssize_t *array_size)
 {
        void **array = (void **)_array;
@@ -529,31 +598,6 @@ char *get_mydnsdomname(TALLOC_CTX *ctx)
        }
 }
 
-/****************************************************************************
- Interpret a protocol description string, with a default.
-****************************************************************************/
-
-int interpret_protocol(const char *str,int def)
-{
-       if (strequal(str,"NT1"))
-               return(PROTOCOL_NT1);
-       if (strequal(str,"LANMAN2"))
-               return(PROTOCOL_LANMAN2);
-       if (strequal(str,"LANMAN1"))
-               return(PROTOCOL_LANMAN1);
-       if (strequal(str,"CORE"))
-               return(PROTOCOL_CORE);
-       if (strequal(str,"COREPLUS"))
-               return(PROTOCOL_COREPLUS);
-       if (strequal(str,"CORE+"))
-               return(PROTOCOL_COREPLUS);
-
-       DEBUG(0,("Unrecognised protocol level %s\n",str));
-
-       return(def);
-}
-
-
 #if (defined(HAVE_NETGROUP) && defined(WITH_AUTOMOUNT))
 /******************************************************************
  Remove any mount options such as -rsize=2048,wsize=2048 etc.
@@ -588,7 +632,7 @@ char *automount_lookup(TALLOC_CTX *ctx, const char *user_name)
 {
        char *value = NULL;
 
-       char *nis_map = (char *)lp_nis_home_map_name();
+       char *nis_map = (char *)lp_homedir_map();
 
        char buffer[NIS_MAXATTRVAL + 1];
        nis_result *result;
@@ -640,7 +684,7 @@ char *automount_lookup(TALLOC_CTX *ctx, const char *user_name)
        char *nis_result;     /* yp_match inits this */
        int nis_result_len;  /* and set this */
        char *nis_domain;     /* yp_get_default_domain inits this */
-       char *nis_map = (char *)lp_nis_home_map_name();
+       char *nis_map = lp_homedir_map(talloc_tos());
 
        if ((nis_error = yp_get_default_domain(&nis_domain)) != 0) {
                DEBUG(3, ("YP Error: %s\n", yperr_string(nis_error)));
@@ -677,92 +721,9 @@ char *automount_lookup(TALLOC_CTX *ctx, const char *user_name)
 #endif /* WITH_NISPLUS_HOME */
 #endif
 
-/****************************************************************************
- Check if a process exists. Does this work on all unixes?
-****************************************************************************/
-
 bool process_exists(const struct server_id pid)
 {
-       if (procid_is_me(&pid)) {
-               return True;
-       }
-
-       if (procid_is_local(&pid)) {
-               return (kill(pid.pid,0) == 0 || errno != ESRCH);
-       }
-
-#ifdef CLUSTER_SUPPORT
-       return ctdbd_process_exists(messaging_ctdbd_connection(),
-                                   pid.vnn, pid.pid);
-#else
-       return False;
-#endif
-}
-
-bool processes_exist(const struct server_id *pids, int num_pids,
-                    bool *results)
-{
-       struct server_id *remote_pids = NULL;
-       int *remote_idx = NULL;
-       bool *remote_results = NULL;
-       int i, num_remote_pids;
-       bool result = false;
-
-       remote_pids = talloc_array(talloc_tos(), struct server_id, num_pids);
-       if (remote_pids == NULL) {
-               goto fail;
-       }
-       remote_idx = talloc_array(talloc_tos(), int, num_pids);
-       if (remote_idx == NULL) {
-               goto fail;
-       }
-       remote_results = talloc_array(talloc_tos(), bool, num_pids);
-       if (remote_results == NULL) {
-               goto fail;
-       }
-
-       num_remote_pids = 0;
-
-       for (i=0; i<num_pids; i++) {
-               if (procid_is_me(&pids[i])) {
-                       results[i] = true;
-                       continue;
-               }
-               if (procid_is_local(&pids[i])) {
-                       results[i] = ((kill(pids[i].pid,0) == 0) ||
-                                     (errno != ESRCH));
-                       continue;
-               }
-
-               remote_pids[num_remote_pids] = pids[i];
-               remote_idx[num_remote_pids] = i;
-               num_remote_pids += 1;
-       }
-
-       if (num_remote_pids != 0) {
-#ifdef CLUSTER_SUPPORT
-               if (!ctdb_processes_exist(messaging_ctdbd_connection(),
-                                         remote_pids, num_remote_pids,
-                                         remote_results)) {
-                       goto fail;
-               }
-#else
-               for (i=0; i<num_remote_pids; i++) {
-                       remote_results[i] = false;
-               }
-#endif
-
-               for (i=0; i<num_remote_pids; i++) {
-                       results[remote_idx[i]] = remote_results[i];
-               }
-       }
-
-       result = true;
-fail:
-       TALLOC_FREE(remote_results);
-       TALLOC_FREE(remote_idx);
-       TALLOC_FREE(remote_pids);
-       return result;
+       return serverid_exists(&pid);
 }
 
 /*******************************************************************
@@ -870,7 +831,7 @@ void smb_panic_s3(const char *why)
        prctl(PR_SET_PTRACER, getpid(), 0, 0, 0);
 #endif
 
-       cmd = lp_panic_action();
+       cmd = lp_panic_action(talloc_tos());
        if (cmd && *cmd) {
                DEBUG(0, ("smb_panic(): calling panic action [%s]\n", cmd));
                result = system(cmd);
@@ -886,158 +847,19 @@ void smb_panic_s3(const char *why)
        dump_core();
 }
 
-/*******************************************************************
- Print a backtrace of the stack to the debug log. This function
- DELIBERATELY LEAKS MEMORY. The expectation is that you should
- exit shortly after calling it.
-********************************************************************/
-
-#ifdef HAVE_LIBUNWIND_H
-#include <libunwind.h>
-#endif
-
-#ifdef HAVE_EXECINFO_H
-#include <execinfo.h>
-#endif
-
-#ifdef HAVE_LIBEXC_H
-#include <libexc.h>
-#endif
-
-void log_stack_trace(void)
-{
-#ifdef HAVE_LIBUNWIND
-       /* Try to use libunwind before any other technique since on ia64
-        * libunwind correctly walks the stack in more circumstances than
-        * backtrace.
-        */ 
-       unw_cursor_t cursor;
-       unw_context_t uc;
-       unsigned i = 0;
-
-       char procname[256];
-       unw_word_t ip, sp, off;
-
-       procname[sizeof(procname) - 1] = '\0';
-
-       if (unw_getcontext(&uc) != 0) {
-               goto libunwind_failed;
-       }
-
-       if (unw_init_local(&cursor, &uc) != 0) {
-               goto libunwind_failed;
-       }
-
-       DEBUG(0, ("BACKTRACE:\n"));
-
-       do {
-           ip = sp = 0;
-           unw_get_reg(&cursor, UNW_REG_IP, &ip);
-           unw_get_reg(&cursor, UNW_REG_SP, &sp);
-
-           switch (unw_get_proc_name(&cursor,
-                       procname, sizeof(procname) - 1, &off) ) {
-           case 0:
-                   /* Name found. */
-           case -UNW_ENOMEM:
-                   /* Name truncated. */
-                   DEBUGADD(0, (" #%u %s + %#llx [ip=%#llx] [sp=%#llx]\n",
-                           i, procname, (long long)off,
-                           (long long)ip, (long long) sp));
-                   break;
-           default:
-           /* case -UNW_ENOINFO: */
-           /* case -UNW_EUNSPEC: */
-                   /* No symbol name found. */
-                   DEBUGADD(0, (" #%u %s [ip=%#llx] [sp=%#llx]\n",
-                           i, "<unknown symbol>",
-                           (long long)ip, (long long) sp));
-           }
-           ++i;
-       } while (unw_step(&cursor) > 0);
-
-       return;
-
-libunwind_failed:
-       DEBUG(0, ("unable to produce a stack trace with libunwind\n"));
-
-#elif HAVE_BACKTRACE_SYMBOLS
-       void *backtrace_stack[BACKTRACE_STACK_SIZE];
-       size_t backtrace_size;
-       char **backtrace_strings;
-
-       /* get the backtrace (stack frames) */
-       backtrace_size = backtrace(backtrace_stack,BACKTRACE_STACK_SIZE);
-       backtrace_strings = backtrace_symbols(backtrace_stack, backtrace_size);
-
-       DEBUG(0, ("BACKTRACE: %lu stack frames:\n", 
-                 (unsigned long)backtrace_size));
-
-       if (backtrace_strings) {
-               int i;
-
-               for (i = 0; i < backtrace_size; i++)
-                       DEBUGADD(0, (" #%u %s\n", i, backtrace_strings[i]));
-
-               /* Leak the backtrace_strings, rather than risk what free() might do */
-       }
-
-#elif HAVE_LIBEXC
-
-       /* The IRIX libexc library provides an API for unwinding the stack. See
-        * libexc(3) for details. Apparantly trace_back_stack leaks memory, but
-        * since we are about to abort anyway, it hardly matters.
-        */
-
-#define NAMESIZE 32 /* Arbitrary */
-
-       __uint64_t      addrs[BACKTRACE_STACK_SIZE];
-       char *          names[BACKTRACE_STACK_SIZE];
-       char            namebuf[BACKTRACE_STACK_SIZE * NAMESIZE];
-
-       int             i;
-       int             levels;
-
-       ZERO_ARRAY(addrs);
-       ZERO_ARRAY(names);
-       ZERO_ARRAY(namebuf);
-
-       /* We need to be root so we can open our /proc entry to walk
-        * our stack. It also helps when we want to dump core.
-        */
-       become_root();
-
-       for (i = 0; i < BACKTRACE_STACK_SIZE; i++) {
-               names[i] = namebuf + (i * NAMESIZE);
-       }
-
-       levels = trace_back_stack(0, addrs, names,
-                       BACKTRACE_STACK_SIZE, NAMESIZE - 1);
-
-       DEBUG(0, ("BACKTRACE: %d stack frames:\n", levels));
-       for (i = 0; i < levels; i++) {
-               DEBUGADD(0, (" #%d 0x%llx %s\n", i, addrs[i], names[i]));
-       }
-#undef NAMESIZE
-
-#else
-       DEBUG(0, ("unable to produce a stack trace on this platform\n"));
-#endif
-}
-
 /*******************************************************************
   A readdir wrapper which just returns the file name.
  ********************************************************************/
 
-const char *readdirname(SMB_STRUCT_DIR *p)
+const char *readdirname(DIR *p)
 {
-       SMB_STRUCT_DIRENT *ptr;
+       struct dirent *ptr;
        char *dname;
 
        if (!p)
                return(NULL);
 
-       ptr = (SMB_STRUCT_DIRENT *)sys_readdir(p);
+       ptr = (struct dirent *)readdir(p);
        if (!ptr)
                return(NULL);
 
@@ -1113,6 +935,7 @@ void set_namearray(name_compare_entry **ppname_array, const char *namelist_in)
 {
        char *name_end;
        char *namelist;
+       char *namelist_end;
        char *nameptr;
        int num_entries = 0;
        int i;
@@ -1129,12 +952,14 @@ void set_namearray(name_compare_entry **ppname_array, const char *namelist_in)
        }
        nameptr = namelist;
 
+       namelist_end = &namelist[strlen(namelist)];
+
        /* We need to make two passes over the string. The
                first to count the number of elements, the second
                to split it.
        */
 
-       while(*nameptr) {
+       while(nameptr <= namelist_end) {
                if ( *nameptr == '/' ) {
                        /* cope with multiple (useless) /s) */
                        nameptr++;
@@ -1146,11 +971,13 @@ void set_namearray(name_compare_entry **ppname_array, const char *namelist_in)
 
                /* find the next '/' or consume remaining */
                name_end = strchr_m(nameptr, '/');
-               if (name_end == NULL)
-                       name_end = (char *)nameptr + strlen(nameptr);
-
-               /* next segment please */
-               nameptr = name_end + 1;
+               if (name_end == NULL) {
+                       /* Point nameptr at the terminating '\0' */
+                       nameptr += strlen(nameptr);
+               } else {
+                       /* next segment please */
+                       nameptr = name_end + 1;
+               }
                num_entries++;
        }
 
@@ -1168,7 +995,7 @@ void set_namearray(name_compare_entry **ppname_array, const char *namelist_in)
        /* Now copy out the names */
        nameptr = namelist;
        i = 0;
-       while(*nameptr) {
+       while(nameptr <= namelist_end) {
                if ( *nameptr == '/' ) {
                        /* cope with multiple (useless) /s) */
                        nameptr++;
@@ -1180,10 +1007,9 @@ void set_namearray(name_compare_entry **ppname_array, const char *namelist_in)
 
                /* find the next '/' or consume remaining */
                name_end = strchr_m(nameptr, '/');
-               if (name_end)
+               if (name_end != NULL) {
                        *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) {
@@ -1192,8 +1018,13 @@ void set_namearray(name_compare_entry **ppname_array, const char *namelist_in)
                        return;
                }
 
-               /* next segment please */
-               nameptr = name_end + 1;
+               if (name_end == NULL) {
+                       /* Point nameptr at the terminating '\0' */
+                       nameptr += strlen(nameptr);
+               } else {
+                       /* next segment please */
+                       nameptr = name_end + 1;
+               }
                i++;
        }
 
@@ -1213,13 +1044,13 @@ void set_namearray(name_compare_entry **ppname_array, const char *namelist_in)
  F_UNLCK in *ptype if the region is unlocked). False if the call failed.
 ****************************************************************************/
 
-bool fcntl_getlock(int fd, SMB_OFF_T *poffset, SMB_OFF_T *pcount, int *ptype, pid_t *ppid)
+bool fcntl_getlock(int fd, int op, off_t *poffset, off_t *pcount, int *ptype, pid_t *ppid)
 {
-       SMB_STRUCT_FLOCK lock;
+       struct flock lock;
        int ret;
 
-       DEBUG(8,("fcntl_getlock fd=%d offset=%.0f count=%.0f type=%d\n",
-                   fd,(double)*poffset,(double)*pcount,*ptype));
+       DEBUG(8,("fcntl_getlock fd=%d op=%d offset=%.0f count=%.0f type=%d\n",
+                   fd,op,(double)*poffset,(double)*pcount,*ptype));
 
        lock.l_type = *ptype;
        lock.l_whence = SEEK_SET;
@@ -1227,7 +1058,7 @@ bool fcntl_getlock(int fd, SMB_OFF_T *poffset, SMB_OFF_T *pcount, int *ptype, pi
        lock.l_len = *pcount;
        lock.l_pid = 0;
 
-       ret = sys_fcntl_ptr(fd,SMB_F_GETLK,&lock);
+       ret = sys_fcntl_ptr(fd,op,&lock);
 
        if (ret == -1) {
                int sav = errno;
@@ -1247,6 +1078,34 @@ bool fcntl_getlock(int fd, SMB_OFF_T *poffset, SMB_OFF_T *pcount, int *ptype, pi
        return True;
 }
 
+#if defined(HAVE_OFD_LOCKS)
+int map_process_lock_to_ofd_lock(int op)
+{
+       switch (op) {
+       case F_GETLK:
+       case F_OFD_GETLK:
+               op = F_OFD_GETLK;
+               break;
+       case F_SETLK:
+       case F_OFD_SETLK:
+               op = F_OFD_SETLK;
+               break;
+       case F_SETLKW:
+       case F_OFD_SETLKW:
+               op = F_OFD_SETLKW;
+               break;
+       default:
+               return -1;
+       }
+       return op;
+}
+#else /* HAVE_OFD_LOCKS */
+int map_process_lock_to_ofd_lock(int op)
+{
+       return op;
+}
+#endif /* HAVE_OFD_LOCKS */
+
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_ALL
 
@@ -1261,7 +1120,9 @@ bool is_myname(const char *s)
        bool ret = False;
 
        for (n=0; my_netbios_names(n); n++) {
-               if (strequal(my_netbios_names(n), s)) {
+               const char *nbt_name = my_netbios_names(n);
+
+               if (strncasecmp_m(nbt_name, s, MAX_NETBIOSNAME_LEN-1) == 0) {
                        ret=True;
                        break;
                }
@@ -1291,14 +1152,46 @@ void ra_lanman_string( const char *native_lanman )
                set_remote_arch( RA_WIN2K3 );
 }
 
-static const char *remote_arch_str;
+static const char *remote_arch_strings[] = {
+       [RA_UNKNOWN] =  "UNKNOWN",
+       [RA_WFWG] =     "WfWg",
+       [RA_OS2] =      "OS2",
+       [RA_WIN95] =    "Win95",
+       [RA_WINNT] =    "WinNT",
+       [RA_WIN2K] =    "Win2K",
+       [RA_WINXP] =    "WinXP",
+       [RA_WIN2K3] =   "Win2K3",
+       [RA_VISTA] =    "Vista",
+       [RA_SAMBA] =    "Samba",
+       [RA_CIFSFS] =   "CIFSFS",
+       [RA_WINXP64] =  "WinXP64",
+       [RA_OSX] =      "OSX",
+};
 
 const char *get_remote_arch_str(void)
 {
-       if (!remote_arch_str) {
-               return "UNKNOWN";
+       if (ra_type >= ARRAY_SIZE(remote_arch_strings)) {
+               /*
+                * set_remote_arch() already checks this so ra_type
+                * should be in the allowed range, but anyway, let's
+                * do another bound check here.
+                */
+               DBG_ERR("Remote arch info out of sync [%d] missing\n", ra_type);
+               ra_type = RA_UNKNOWN;
+       }
+       return remote_arch_strings[ra_type];
+}
+
+enum remote_arch_types get_remote_arch_from_str(const char *remote_arch_string)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(remote_arch_strings); i++) {
+               if (strcmp(remote_arch_string, remote_arch_strings[i]) == 0) {
+                       return i;
+               }
        }
-       return remote_arch_str;
+       return RA_UNKNOWN;
 }
 
 /*******************************************************************
@@ -1307,52 +1200,20 @@ const char *get_remote_arch_str(void)
 
 void set_remote_arch(enum remote_arch_types type)
 {
-       ra_type = type;
-       switch( type ) {
-       case RA_WFWG:
-               remote_arch_str = "WfWg";
-               break;
-       case RA_OS2:
-               remote_arch_str = "OS2";
-               break;
-       case RA_WIN95:
-               remote_arch_str = "Win95";
-               break;
-       case RA_WINNT:
-               remote_arch_str = "WinNT";
-               break;
-       case RA_WIN2K:
-               remote_arch_str = "Win2K";
-               break;
-       case RA_WINXP:
-               remote_arch_str = "WinXP";
-               break;
-       case RA_WINXP64:
-               remote_arch_str = "WinXP64";
-               break;
-       case RA_WIN2K3:
-               remote_arch_str = "Win2K3";
-               break;
-       case RA_VISTA:
-               remote_arch_str = "Vista";
-               break;
-       case RA_SAMBA:
-               remote_arch_str = "Samba";
-               break;
-       case RA_CIFSFS:
-               remote_arch_str = "CIFSFS";
-               break;
-       case RA_OSX:
-               remote_arch_str = "OSX";
-               break;
-       default:
+       if (ra_type >= ARRAY_SIZE(remote_arch_strings)) {
+               /*
+                * This protects against someone adding values to enum
+                * remote_arch_types without updating
+                * remote_arch_strings array.
+                */
+               DBG_ERR("Remote arch info out of sync [%d] missing\n", ra_type);
                ra_type = RA_UNKNOWN;
-               remote_arch_str = "UNKNOWN";
-               break;
+               return;
        }
 
+       ra_type = type;
        DEBUG(10,("set_remote_arch: Client arch is \'%s\'\n",
-                               remote_arch_str));
+                 get_remote_arch_str()));
 }
 
 /*******************************************************************
@@ -1364,6 +1225,148 @@ enum remote_arch_types get_remote_arch(void)
        return ra_type;
 }
 
+#define RA_CACHE_TTL 7*24*3600
+
+static bool remote_arch_cache_key(const struct GUID *client_guid,
+                                 fstring key)
+{
+       struct GUID_txt_buf guid_buf;
+       const char *guid_string = NULL;
+
+       guid_string = GUID_buf_string(client_guid, &guid_buf);
+       if (guid_string == NULL) {
+               return false;
+       }
+
+       fstr_sprintf(key, "RA/%s", guid_string);
+       return true;
+}
+
+struct ra_parser_state {
+       bool found;
+       enum remote_arch_types ra;
+};
+
+static void ra_parser(const struct gencache_timeout *t,
+                     DATA_BLOB blob,
+                     void *priv_data)
+{
+       struct ra_parser_state *state = (struct ra_parser_state *)priv_data;
+       const char *ra_str = NULL;
+
+       if (gencache_timeout_expired(t)) {
+               return;
+       }
+
+       if ((blob.length == 0) || (blob.data[blob.length-1] != '\0')) {
+               DBG_ERR("Remote arch cache key not a string\n");
+               return;
+       }
+
+       ra_str = (const char *)blob.data;
+       DBG_INFO("Got remote arch [%s] from cache\n", ra_str);
+
+       state->ra = get_remote_arch_from_str(ra_str);
+       state->found = true;
+       return;
+}
+
+static bool remote_arch_cache_get(const struct GUID *client_guid)
+{
+       bool ok;
+       fstring ra_key;
+       struct ra_parser_state state = (struct ra_parser_state) {
+               .found = false,
+               .ra = RA_UNKNOWN,
+       };
+
+       ok = remote_arch_cache_key(client_guid, ra_key);
+       if (!ok) {
+               return false;
+       }
+
+       ok = gencache_parse(ra_key, ra_parser, &state);
+       if (!ok || !state.found) {
+               return true;
+       }
+
+       if (state.ra == RA_UNKNOWN) {
+               return true;
+       }
+
+       set_remote_arch(state.ra);
+       return true;
+}
+
+static bool remote_arch_cache_set(const struct GUID *client_guid)
+{
+       bool ok;
+       fstring ra_key;
+       const char *ra_str = NULL;
+
+       if (get_remote_arch() == RA_UNKNOWN) {
+               return true;
+       }
+
+       ok = remote_arch_cache_key(client_guid, ra_key);
+       if (!ok) {
+               return false;
+       }
+
+       ra_str = get_remote_arch_str();
+       if (ra_str == NULL) {
+               return false;
+       }
+
+       ok = gencache_set(ra_key, ra_str, time(NULL) + RA_CACHE_TTL);
+       if (!ok) {
+               return false;
+       }
+
+       return true;
+}
+
+bool remote_arch_cache_update(const struct GUID *client_guid)
+{
+       bool ok;
+
+       if (get_remote_arch() == RA_UNKNOWN) {
+
+               become_root();
+               ok = remote_arch_cache_get(client_guid);
+               unbecome_root();
+
+               return ok;
+       }
+
+       become_root();
+       ok = remote_arch_cache_set(client_guid);
+       unbecome_root();
+
+       return ok;
+}
+
+bool remote_arch_cache_delete(const struct GUID *client_guid)
+{
+       bool ok;
+       fstring ra_key;
+
+       ok = remote_arch_cache_key(client_guid, ra_key);
+       if (!ok) {
+               return false;
+       }
+
+       become_root();
+       ok = gencache_del(ra_key);
+       unbecome_root();
+
+       if (!ok) {
+               return false;
+       }
+
+       return true;
+}
+
 const char *tab_depth(int level, int depth)
 {
        if( CHECK_DEBUGLVL(level) ) {
@@ -1383,9 +1386,14 @@ const char *tab_depth(int level, int depth)
 
 int str_checksum(const char *s)
 {
+       TDB_DATA key;
        if (s == NULL)
                return 0;
-       return hash(s, strlen(s), 0);
+
+       key = (TDB_DATA) { .dptr = discard_const_p(uint8_t, s),
+                          .dsize = strlen(s) };
+
+       return tdb_jenkins_hash(&key);
 }
 
 /*****************************************************************
@@ -1540,86 +1548,18 @@ char *myhostname(void)
 
 char *myhostname_upper(void)
 {
-       char *name;
        static char *ret;
        if (ret == NULL) {
-               name = get_myname(talloc_tos());
+               char *name = get_myname(NULL);
+               if (name == NULL) {
+                       return NULL;
+               }
                ret = strupper_talloc(NULL, name);
                talloc_free(name);
        }
        return ret;
 }
 
-/**
- * @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)
-{
-       char *fname = NULL;
-
-       fname = talloc_strdup(talloc_tos(), rootpath);
-       if (!fname) {
-               return NULL;
-       }
-       trim_string(fname,"","/");
-
-       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(),
-                               "%s/%s",
-                               fname,
-                               name);
-}
-
-/**
- * @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());
-}
-
-/**
- * @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, 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());
-}
-
 /*******************************************************************
  Given a filename - get its directory name
 ********************************************************************/
@@ -1663,11 +1603,6 @@ bool ms_has_wild(const char *s)
 {
        char c;
 
-       if (lp_posix_pathnames()) {
-               /* With posix pathnames no characters are wild. */
-               return False;
-       }
-
        while ((c = *s++)) {
                switch (c) {
                case '*':
@@ -1710,7 +1645,7 @@ bool mask_match(const char *string, const char *pattern, bool is_case_sensitive)
        if (ISDOT(pattern))
                return False;
 
-       return ms_fnmatch(pattern, string, Protocol <= PROTOCOL_LANMAN2, is_case_sensitive) == 0;
+       return ms_fnmatch_protocol(pattern, string, Protocol, is_case_sensitive) == 0;
 }
 
 /*******************************************************************
@@ -1743,219 +1678,65 @@ bool mask_match_list(const char *string, char **list, int listLen, bool is_case_
        return False;
 }
 
-/*********************************************************
- Recursive routine that is called by unix_wild_match.
-*********************************************************/
-
-static bool unix_do_match(const char *regexp, const char *str)
-{
-       const char *p;
-
-       for( p = regexp; *p && *str; ) {
-
-               switch(*p) {
-                       case '?':
-                               str++;
-                               p++;
-                               break;
-
-                       case '*':
-
-                               /*
-                                * Look for a character matching 
-                                * the one after the '*'.
-                                */
-                               p++;
-                               if(!*p)
-                                       return true; /* Automatic match */
-                               while(*str) {
-
-                                       while(*str && (*p != *str))
-                                               str++;
-
-                                       /*
-                                        * Patch from weidel@multichart.de. In the case of the regexp
-                                        * '*XX*' we want to ensure there are at least 2 'X' characters
-                                        * in the string after the '*' for a match to be made.
-                                        */
-
-                                       {
-                                               int matchcount=0;
-
-                                               /*
-                                                * Eat all the characters that match, but count how many there were.
-                                                */
-
-                                               while(*str && (*p == *str)) {
-                                                       str++;
-                                                       matchcount++;
-                                               }
-
-                                               /*
-                                                * Now check that if the regexp had n identical characters that
-                                                * matchcount had at least that many matches.
-                                                */
-
-                                               while ( *(p+1) && (*(p+1) == *p)) {
-                                                       p++;
-                                                       matchcount--;
-                                               }
-
-                                               if ( matchcount <= 0 )
-                                                       return false;
-                                       }
-
-                                       str--; /* We've eaten the match char after the '*' */
-
-                                       if(unix_do_match(p, str))
-                                               return true;
-
-                                       if(!*str)
-                                               return false;
-                                       else
-                                               str++;
-                               }
-                               return false;
-
-                       default:
-                               if(*str != *p)
-                                       return false;
-                               str++;
-                               p++;
-                               break;
-               }
-       }
-
-       if(!*p && !*str)
-               return true;
-
-       if (!*p && str[0] == '.' && str[1] == 0)
-               return true;
-
-       if (!*str && *p == '?') {
-               while (*p == '?')
-                       p++;
-               return(!*p);
-       }
-
-       if(!*str && (*p == '*' && p[1] == '\0'))
-               return true;
-
-       return false;
-}
-
-/*******************************************************************
- Simple case insensitive interface to a UNIX wildcard matcher.
- Returns True if match, False if not.
-*******************************************************************/
-
-bool unix_wild_match(const char *pattern, const char *string)
-{
-       TALLOC_CTX *ctx = talloc_stackframe();
-       char *p2;
-       char *s2;
-       char *p;
-       bool ret = false;
-
-       p2 = talloc_strdup(ctx,pattern);
-       s2 = talloc_strdup(ctx,string);
-       if (!p2 || !s2) {
-               TALLOC_FREE(ctx);
-               return false;
-       }
-       strlower_m(p2);
-       strlower_m(s2);
-
-       /* Remove any *? and ** from the pattern as they are meaningless */
-       for(p = p2; *p; p++) {
-               while( *p == '*' && (p[1] == '?' ||p[1] == '*')) {
-                       memmove(&p[1], &p[2], strlen(&p[2])+1);
-               }
-       }
-
-       if (strequal(p2,"*")) {
-               TALLOC_FREE(ctx);
-               return true;
-       }
-
-       ret = unix_do_match(p2, s2);
-       TALLOC_FREE(ctx);
-       return ret;
-}
-
 /**********************************************************************
- Converts a name to a fully qualified domain name.
- Returns true if lookup succeeded, false if not (then fqdn is set to name)
- Note we deliberately use gethostbyname here, not getaddrinfo as we want
- to examine the h_aliases and I don't know how to do that with getaddrinfo.
-***********************************************************************/
+  Converts a name to a fully qualified domain name.
+  Returns true if lookup succeeded, false if not (then fqdn is set to name)
+  Uses getaddrinfo() with AI_CANONNAME flag to obtain the official
+  canonical name of the host. getaddrinfo() may use a variety of sources
+  including /etc/hosts to obtain the domainname. It expects aliases in
+  /etc/hosts to NOT be the FQDN. The FQDN should come first.
+************************************************************************/
 
 bool name_to_fqdn(fstring fqdn, const char *name)
 {
        char *full = NULL;
-       struct hostent *hp = gethostbyname(name);
+       struct addrinfo hints;
+       struct addrinfo *result;
+       int s;
 
-       if (!hp || !hp->h_name || !*hp->h_name) {
+       /* Configure hints to obtain canonical name */
+
+       memset(&hints, 0, sizeof(struct addrinfo));
+       hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
+       hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
+       hints.ai_flags = AI_CANONNAME;  /* Get host's FQDN */
+       hints.ai_protocol = 0;          /* Any protocol */
+
+       s = getaddrinfo(name, NULL, &hints, &result);
+       if (s != 0) {
+               DEBUG(1, ("getaddrinfo: %s\n", gai_strerror(s)));
                DEBUG(10,("name_to_fqdn: lookup for %s failed.\n", name));
                fstrcpy(fqdn, name);
                return false;
        }
+       full = result->ai_canonname;
 
-       /* Find out if the fqdn is returned as an alias
+       /* Find out if the FQDN is returned as an alias
         * to cope with /etc/hosts files where the first
-        * name is not the fqdn but the short name */
-       if (hp->h_aliases && (! strchr_m(hp->h_name, '.'))) {
-               int i;
-               for (i = 0; hp->h_aliases[i]; i++) {
-                       if (strchr_m(hp->h_aliases[i], '.')) {
-                               full = hp->h_aliases[i];
-                               break;
-                       }
-               }
+        * name is not the FQDN but the short name.
+        * getaddrinfo provides no easy way of handling aliases
+        * in /etc/hosts. Users should make sure the FQDN
+        * comes first in /etc/hosts. */
+       if (full && (! strchr_m(full, '.'))) {
+               DEBUG(1, ("WARNING: your /etc/hosts file may be broken!\n"));
+               DEBUGADD(1, ("    Full qualified domain names (FQDNs) should not be specified\n"));
+               DEBUGADD(1, ("    as an alias in /etc/hosts. FQDN should be the first name\n"));
+               DEBUGADD(1, ("    prior to any aliases.\n"));
        }
        if (full && (strcasecmp_m(full, "localhost.localdomain") == 0)) {
                DEBUG(1, ("WARNING: your /etc/hosts file may be broken!\n"));
-               DEBUGADD(1, ("    Specifing the machine hostname for address 127.0.0.1 may lead\n"));
+               DEBUGADD(1, ("    Specifying the machine hostname for address 127.0.0.1 may lead\n"));
                DEBUGADD(1, ("    to Kerberos authentication problems as localhost.localdomain\n"));
                DEBUGADD(1, ("    may end up being used instead of the real machine FQDN.\n"));
-               full = hp->h_name;
-       }
-       if (!full) {
-               full = hp->h_name;
        }
 
        DEBUG(10,("name_to_fqdn: lookup for %s -> %s.\n", name, full));
        fstrcpy(fqdn, full);
+       freeaddrinfo(result);           /* No longer needed */
        return true;
 }
 
-/**********************************************************************
- Append a DATA_BLOB to a talloc'ed object
-***********************************************************************/
-
-void *talloc_append_blob(TALLOC_CTX *mem_ctx, void *buf, DATA_BLOB blob)
-{
-       size_t old_size = 0;
-       char *result;
-
-       if (blob.length == 0) {
-               return buf;
-       }
-
-       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)
+uint32_t map_share_mode_to_deny_mode(uint32_t share_access, uint32_t private_options)
 {
        switch (share_access & ~FILE_SHARE_DELETE) {
                case FILE_SHARE_NONE:
@@ -1973,124 +1754,12 @@ uint32 map_share_mode_to_deny_mode(uint32 share_access, uint32 private_options)
                return DENY_FCB;
        }
 
-       return (uint32)-1;
-}
-
-pid_t procid_to_pid(const struct server_id *proc)
-{
-       return proc->pid;
-}
-
-static uint32 my_vnn = NONCLUSTER_VNN;
-
-void set_my_vnn(uint32 vnn)
-{
-       DEBUG(10, ("vnn pid %d = %u\n", (int)getpid(), (unsigned int)vnn));
-       my_vnn = vnn;
-}
-
-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.task_id = 0;
-       result.unique_id = my_unique_id;
-       result.vnn = my_vnn;
-       return result;
-}
-
-struct server_id procid_self(void)
-{
-       return pid_to_procid(getpid());
-}
-
-bool procid_equal(const struct server_id *p1, const struct server_id *p2)
-{
-       if (p1->pid != p2->pid)
-               return False;
-       if (p1->task_id != p2->task_id)
-               return False;
-       if (p1->vnn != p2->vnn)
-               return False;
-       return True;
-}
-
-bool cluster_id_equal(const struct server_id *id1,
-                     const struct server_id *id2)
-{
-       return procid_equal(id1, id2);
-}
-
-bool procid_is_me(const struct server_id *pid)
-{
-       if (pid->pid != getpid())
-               return False;
-       if (pid->task_id != 0)
-               return False;
-       if (pid->vnn != my_vnn)
-               return False;
-       return True;
+       return (uint32_t)-1;
 }
 
 struct server_id interpret_pid(const char *pid_string)
 {
-       struct server_id result;
-       unsigned long long pid;
-       unsigned int vnn, task_id = 0;
-
-       ZERO_STRUCT(result);
-
-       /* We accept various forms with 1, 2 or 3 component forms
-        * because the server_id_str() can print different forms, and
-        * we want backwards compatibility for scripts that may call
-        * smbclient. */
-       if (sscanf(pid_string, "%u:%llu.%u", &vnn, &pid, &task_id) == 3) {
-               result.vnn = vnn;
-               result.pid = pid;
-               result.task_id = task_id;
-       } else if (sscanf(pid_string, "%u:%llu", &vnn, &pid) == 2) {
-               result.vnn = vnn;
-               result.pid = pid;
-               result.task_id = 0;
-       } else if (sscanf(pid_string, "%llu.%u", &pid, &task_id) == 2) {
-               result.vnn = get_my_vnn();
-               result.pid = pid;
-               result.task_id = task_id;
-       } else if (sscanf(pid_string, "%llu", &pid) == 1) {
-               result.vnn = get_my_vnn();
-               result.pid = pid;
-       } else {
-               result.vnn = NONCLUSTER_VNN;
-               result.pid = (uint64_t)-1;
-       }
-       return result;
-}
-
-char *procid_str_static(const struct server_id *pid)
-{
-       return server_id_str(talloc_tos(), pid);
-}
-
-bool procid_valid(const struct server_id *pid)
-{
-       return (pid->pid != (uint64_t)-1);
-}
-
-bool procid_is_local(const struct server_id *pid)
-{
-       return pid->vnn == my_vnn;
+       return server_id_from_string(get_my_vnn(), pid_string);
 }
 
 /****************************************************************
@@ -2181,7 +1850,7 @@ int get_safe_IVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, i
  call (they take care of winbind separator and other winbind specific settings).
 ****************************************************************/
 
-void split_domain_user(TALLOC_CTX *mem_ctx,
+bool split_domain_user(TALLOC_CTX *mem_ctx,
                       const char *full_name,
                       char **domain,
                       char **user)
@@ -2193,11 +1862,23 @@ void split_domain_user(TALLOC_CTX *mem_ctx,
        if (p != NULL) {
                *domain = talloc_strndup(mem_ctx, full_name,
                                         PTR_DIFF(p, full_name));
+               if (*domain == NULL) {
+                       return false;
+               }
                *user = talloc_strdup(mem_ctx, p+1);
+               if (*user == NULL) {
+                       TALLOC_FREE(*domain);
+                       return false;
+               }
        } else {
-               *domain = talloc_strdup(mem_ctx, "");
+               *domain = NULL;
                *user = talloc_strdup(mem_ctx, full_name);
+               if (*user == NULL) {
+                       return false;
+               }
        }
+
+       return true;
 }
 
 /****************************************************************
@@ -2220,17 +1901,6 @@ 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;
-}
-
 bool any_nt_status_not_ok(NTSTATUS err1, NTSTATUS err2, NTSTATUS *result)
 {
        if (!NT_STATUS_IS_OK(err1)) {
@@ -2306,16 +1976,16 @@ bool is_executable(const char *fname)
 
 bool map_open_params_to_ntcreate(const char *smb_base_fname,
                                 int deny_mode, int open_func,
-                                uint32 *paccess_mask,
-                                uint32 *pshare_mode,
-                                uint32 *pcreate_disposition,
-                                uint32 *pcreate_options,
+                                uint32_t *paccess_mask,
+                                uint32_t *pshare_mode,
+                                uint32_t *pcreate_disposition,
+                                uint32_t *pcreate_options,
                                 uint32_t *pprivate_flags)
 {
-       uint32 access_mask;
-       uint32 share_mode;
-       uint32 create_disposition;
-       uint32 create_options = FILE_NON_DIRECTORY_FILE;
+       uint32_t access_mask;
+       uint32_t share_mode;
+       uint32_t create_disposition;
+       uint32_t create_options = FILE_NON_DIRECTORY_FILE;
        uint32_t private_flags = 0;
 
        DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
@@ -2446,3 +2116,101 @@ bool map_open_params_to_ntcreate(const char *smb_base_fname,
        return True;
 
 }
+
+/*************************************************************************
+ Return a talloced copy of a struct security_unix_token. NULL on fail.
+*************************************************************************/
+
+struct security_unix_token *copy_unix_token(TALLOC_CTX *ctx, const struct security_unix_token *tok)
+{
+       struct security_unix_token *cpy;
+
+       cpy = talloc(ctx, struct security_unix_token);
+       if (!cpy) {
+               return NULL;
+       }
+
+       cpy->uid = tok->uid;
+       cpy->gid = tok->gid;
+       cpy->ngroups = tok->ngroups;
+       if (tok->ngroups) {
+               /* Make this a talloc child of cpy. */
+               cpy->groups = (gid_t *)talloc_memdup(
+                       cpy, tok->groups, tok->ngroups * sizeof(gid_t));
+               if (!cpy->groups) {
+                       TALLOC_FREE(cpy);
+                       return NULL;
+               }
+       } else {
+               cpy->groups = NULL;
+       }
+       return cpy;
+}
+
+/****************************************************************************
+ Return a root token
+****************************************************************************/
+
+struct security_unix_token *root_unix_token(TALLOC_CTX *mem_ctx)
+{
+       struct security_unix_token *t = NULL;
+
+       t = talloc_zero(mem_ctx, struct security_unix_token);
+       if (t == NULL) {
+               return NULL;
+       }
+
+       /*
+        * This is not needed, but lets make it explicit, not implicit.
+        */
+       *t = (struct security_unix_token) {
+               .uid = 0,
+               .gid = 0,
+               .ngroups = 0,
+               .groups = NULL
+       };
+
+       return t;
+}
+
+/****************************************************************************
+ Check that a file matches a particular file type.
+****************************************************************************/
+
+bool dir_check_ftype(uint32_t mode, uint32_t dirtype)
+{
+       uint32_t mask;
+
+       /* Check the "may have" search bits. */
+       if (((mode & ~dirtype) &
+                       (FILE_ATTRIBUTE_HIDDEN |
+                        FILE_ATTRIBUTE_SYSTEM |
+                        FILE_ATTRIBUTE_DIRECTORY)) != 0) {
+               return false;
+       }
+
+       /* Check the "must have" bits,
+          which are the may have bits shifted eight */
+       /* If must have bit is set, the file/dir can
+          not be returned in search unless the matching
+          file attribute is set */
+       mask = ((dirtype >> 8) & (FILE_ATTRIBUTE_DIRECTORY|
+                                   FILE_ATTRIBUTE_ARCHIVE|
+                                  FILE_ATTRIBUTE_READONLY|
+                                    FILE_ATTRIBUTE_HIDDEN|
+                                    FILE_ATTRIBUTE_SYSTEM)); /* & 0x37 */
+       if(mask) {
+               if((mask & (mode & (FILE_ATTRIBUTE_DIRECTORY|
+                                     FILE_ATTRIBUTE_ARCHIVE|
+                                    FILE_ATTRIBUTE_READONLY|
+                                      FILE_ATTRIBUTE_HIDDEN|
+                                       FILE_ATTRIBUTE_SYSTEM))) == mask) {
+                       /* check if matching attribute present */
+                       return true;
+               } else {
+                       return false;
+               }
+       }
+
+       return true;
+}