s3-secrets: only include secrets.h when needed.
[samba.git] / source3 / lib / util.c
index 80a807d7e4ccedccaa24d824aeff6ccf632fe9ac..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,57 +56,27 @@ extern unsigned int global_clobber_region_line;
 #endif /* WITH_NISPLUS_HOME */
 #endif /* HAVE_NETGROUP && WITH_AUTOMOUNT */
 
-enum protocol_types Protocol = PROTOCOL_COREPLUS;
-
-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.
-***********************************************************************/
+static enum protocol_types Protocol = PROTOCOL_COREPLUS;
 
-bool set_global_myname(const char *myname)
+enum protocol_types get_Protocol(void)
 {
-       SAFE_FREE(smb_myname);
-       smb_myname = SMB_STRDUP(myname);
-       if (!smb_myname)
-               return False;
-       strupper_m(smb_myname);
-       return True;
+       return Protocol;
 }
 
-const char *global_myname(void)
+void set_Protocol(enum protocol_types  p)
 {
-       return smb_myname;
+       Protocol = p;
 }
 
+static enum remote_arch_types ra_type = RA_UNKNOWN;
+
 /***********************************************************************
Allocate and set myworkgroup. Ensure upper case.
Definitions for all names.
 ***********************************************************************/
 
-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;
-}
+static char *smb_scope;
+static int smb_num_netbios_names;
+static char **smb_my_netbios_names;
 
 /***********************************************************************
  Allocate and set scope. Ensure upper case.
@@ -174,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();
@@ -308,6 +278,24 @@ void set_cmdline_auth_info_username(struct user_auth_info *auth_info,
        }
 }
 
+const char *get_cmdline_auth_info_domain(const struct user_auth_info *auth_info)
+{
+       if (!auth_info->domain) {
+               return "";
+       }
+       return auth_info->domain;
+}
+
+void set_cmdline_auth_info_domain(struct user_auth_info *auth_info,
+                                 const char *domain)
+{
+       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(const struct user_auth_info *auth_info)
 {
        if (!auth_info->password) {
@@ -354,6 +342,16 @@ int get_cmdline_auth_info_signing_state(const struct user_auth_info *auth_info)
        return auth_info->signing_state;
 }
 
+void set_cmdline_auth_info_use_ccache(struct user_auth_info *auth_info, bool b)
+{
+        auth_info->use_ccache = b;
+}
+
+bool get_cmdline_auth_info_use_ccache(const struct user_auth_info *auth_info)
+{
+       return auth_info->use_ccache;
+}
+
 void set_cmdline_auth_info_use_kerberos(struct user_auth_info *auth_info,
                                        bool b)
 {
@@ -495,53 +493,21 @@ void set_cmdline_auth_info_getpass(struct user_auth_info *auth_info)
        TALLOC_FREE(frame);
 }
 
-/****************************************************************************
- Add a gid to an array of gids if it's not already there.
-****************************************************************************/
-
-bool add_gid_to_array_unique(TALLOC_CTX *mem_ctx, gid_t gid,
-                            gid_t **gids, size_t *num_gids)
-{
-       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;
-}
-
 /*******************************************************************
  Check if a file exists - call vfs_file_exist for samba files.
 ********************************************************************/
 
-bool file_exist_stat(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)));
 }
 
 /*******************************************************************
@@ -551,31 +517,10 @@ bool file_exist_stat(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);
-}
-
-/*******************************************************************
- Check if a directory exists.
-********************************************************************/
-
-bool directory_exist_stat(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 S_ISSOCK(st.st_ex_mode);
 }
 
 /*******************************************************************
@@ -584,7 +529,7 @@ bool directory_exist_stat(char *dname,SMB_STRUCT_STAT *st)
 
 uint64_t get_file_size_stat(const SMB_STRUCT_STAT *sbuf)
 {
-       return sbuf->st_size;
+       return sbuf->st_ex_size;
 }
 
 /*******************************************************************
@@ -594,8 +539,8 @@ uint64_t get_file_size_stat(const SMB_STRUCT_STAT *sbuf)
 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 get_file_size_stat(&buf);
 }
@@ -631,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),
@@ -650,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));
@@ -901,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);
@@ -927,11 +872,12 @@ void smb_msleep(unsigned int t)
 #endif
 }
 
-bool reinit_after_fork(struct messaging_context *msg_ctx,
-                      struct event_context *ev_ctx,
-                      bool parent_longlived)
+NTSTATUS reinit_after_fork(struct messaging_context *msg_ctx,
+                          struct event_context *ev_ctx,
+                          struct server_id id,
+                          bool parent_longlived)
 {
-       NTSTATUS status;
+       NTSTATUS status = NT_STATUS_OK;
 
        /* Reset the state of the random
         * number generation system, so
@@ -942,11 +888,12 @@ 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;
        }
 
-       if (ev_ctx) {
-               event_context_reinit(ev_ctx);
+       if (ev_ctx && tevent_re_initialise(ev_ctx) != 0) {
+               smb_panic(__location__ ": Failed to re-initialise event context");
        }
 
        if (msg_ctx) {
@@ -954,33 +901,14 @@ bool reinit_after_fork(struct messaging_context *msg_ctx,
                 * For clustering, we need to re-init our ctdbd connection after the
                 * fork
                 */
-               status = messaging_reinit(msg_ctx);
+               status = messaging_reinit(msg_ctx, id);
                if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(0,("messaging_reinit() failed: %s\n",
                                 nt_errstr(status)));
-                       return false;
                }
        }
-
-       return true;
-}
-
-/****************************************************************************
- Put up a yes/no prompt.
-****************************************************************************/
-
-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);
+ done:
+       return status;
 }
 
 #if defined(PARANOID_MALLOC_CHECKER)
@@ -1222,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);
 }
 
@@ -1327,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;
@@ -1364,8 +1295,8 @@ 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
@@ -1582,7 +1513,7 @@ libunwind_failed:
 
        DEBUG(0, ("BACKTRACE: %lu stack frames:\n", 
                  (unsigned long)backtrace_size));
-       
+
        if (backtrace_strings) {
                int i;
 
@@ -1646,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);
@@ -1720,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;
 
@@ -1744,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;
@@ -1765,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 == '/' ) {
@@ -1773,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"));
@@ -1791,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;
@@ -1851,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;
@@ -2113,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.
  *****************************************************************/  
@@ -2164,10 +2082,10 @@ void *smb_xmalloc_array(size_t size, unsigned int count)
        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;
 }
 
@@ -2328,7 +2246,7 @@ bool parent_dirname(TALLOC_CTX *mem_ctx, const char *dir, char **parent,
 {
        char *p;
        ptrdiff_t len;
+
        p = strrchr_m(dir, '/'); /* Find final '/', if any */
 
        if (p == NULL) {
@@ -2336,7 +2254,7 @@ bool parent_dirname(TALLOC_CTX *mem_ctx, const char *dir, char **parent,
                        return False;
                }
                if (name) {
-                       *name = "";
+                       *name = dir;
                }
                return True;
        }
@@ -2404,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;
 }
 
@@ -2420,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;
 }
 
@@ -2693,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
@@ -2708,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)
@@ -2743,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;
        }
@@ -2758,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)
@@ -3052,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)
+bool is_valid_policy_hnd(const struct policy_handle *hnd)
 {
-       char *base = NULL;
-       char *stream = NULL;
-       char *sname; /* stream name */
-       const char *stype; /* stream type */
-
-       DEBUG(10, ("split_ntfs_stream_name called for [%s]\n", fname));
-
-       sname = strchr_m(fname, ':');
-
-       if (lp_posix_pathnames() || (sname == NULL)) {
-               if (pbase != NULL) {
-                       base = talloc_strdup(mem_ctx, fname);
-                       NT_STATUS_HAVE_NO_MEMORY(base);
-               }
-               goto done;
-       }
-
-       if (pbase != NULL) {
-               base = talloc_strndup(mem_ctx, fname, PTR_DIFF(sname, fname));
-               NT_STATUS_HAVE_NO_MEMORY(base);
-       }
-
-       sname += 1;
-
-       stype = strchr_m(sname, ':');
-
-       if (stype == NULL) {
-               sname = talloc_strdup(mem_ctx, sname);
-               stype = "$DATA";
-       }
-       else {
-               if (StrCaseCmp(stype, ":$DATA") != 0) {
-                       /*
-                        * If there is an explicit stream type, so far we only
-                        * allow $DATA. Is there anything else allowed? -- vl
-                        */
-                       DEBUG(10, ("[%s] is an invalid stream type\n", stype));
-                       TALLOC_FREE(base);
-                       return NT_STATUS_OBJECT_NAME_INVALID;
-               }
-               sname = talloc_strndup(mem_ctx, sname, PTR_DIFF(stype, sname));
-               stype += 1;
-       }
-
-       if (sname == NULL) {
-               TALLOC_FREE(base);
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       if (sname[0] == '\0') {
-               /*
-                * no stream name, so no stream
-                */
-               goto done;
-       }
-
-       if (pstream != NULL) {
-               stream = talloc_asprintf(mem_ctx, "%s:%s", sname, stype);
-               if (stream == NULL) {
-                       TALLOC_FREE(sname);
-                       TALLOC_FREE(base);
-                       return NT_STATUS_NO_MEMORY;
-               }
-               /*
-                * upper-case the type field
-                */
-               strupper_m(strchr_m(stream, ':')+1);
-       }
-
- done:
-       if (pbase != NULL) {
-               *pbase = base;
-       }
-       if (pstream != NULL) {
-               *pstream = stream;
-       }
-       return NT_STATUS_OK;
-}
-
-bool is_valid_policy_hnd(const POLICY_HND *hnd)
-{
-       POLICY_HND tmp;
+       struct policy_handle tmp;
        ZERO_STRUCT(tmp);
        return (memcmp(&tmp, hnd, sizeof(tmp)) != 0);
 }
@@ -3178,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;
+}