v3-4-test: Pull in tevent_req_poll_ntstatus from master
[samba.git] / source3 / lib / util.c
index df01c0306f9e7a5c57ec6d6e1c873ba6d43cbfdf..c9afe7b6a9156e647ff8645b45f17d63c26ddc4b 100644 (file)
@@ -290,7 +290,7 @@ struct user_auth_info *user_auth_info_init(TALLOC_CTX *mem_ctx)
        return result;
 }
 
-const char *get_cmdline_auth_info_username(struct user_auth_info *auth_info)
+const char *get_cmdline_auth_info_username(const struct user_auth_info *auth_info)
 {
        if (!auth_info->username) {
                return "";
@@ -308,7 +308,7 @@ void set_cmdline_auth_info_username(struct user_auth_info *auth_info,
        }
 }
 
-const char *get_cmdline_auth_info_password(struct user_auth_info *auth_info)
+const char *get_cmdline_auth_info_password(const struct user_auth_info *auth_info)
 {
        if (!auth_info->password) {
                return "";
@@ -320,6 +320,9 @@ void set_cmdline_auth_info_password(struct user_auth_info *auth_info,
                                    const char *password)
 {
        TALLOC_FREE(auth_info->password);
+       if (password == NULL) {
+               password = "";
+       }
        auth_info->password = talloc_strdup(auth_info, password);
        if (!auth_info->password) {
                exit(ENOMEM);
@@ -346,7 +349,7 @@ bool set_cmdline_auth_info_signing_state(struct user_auth_info *auth_info,
        return true;
 }
 
-int get_cmdline_auth_info_signing_state(struct user_auth_info *auth_info)
+int get_cmdline_auth_info_signing_state(const struct user_auth_info *auth_info)
 {
        return auth_info->signing_state;
 }
@@ -357,11 +360,22 @@ void set_cmdline_auth_info_use_kerberos(struct user_auth_info *auth_info,
         auth_info->use_kerberos = b;
 }
 
-bool get_cmdline_auth_info_use_kerberos(struct user_auth_info *auth_info)
+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(struct user_auth_info *auth_info)
 {
@@ -380,23 +394,23 @@ void set_cmdline_auth_info_use_machine_account(struct user_auth_info *auth_info)
        auth_info->use_machine_account = true;
 }
 
-bool get_cmdline_auth_info_got_pass(struct user_auth_info *auth_info)
+bool get_cmdline_auth_info_got_pass(const struct user_auth_info *auth_info)
 {
        return auth_info->got_pass;
 }
 
-bool get_cmdline_auth_info_smb_encrypt(struct user_auth_info *auth_info)
+bool get_cmdline_auth_info_smb_encrypt(const struct user_auth_info *auth_info)
 {
        return auth_info->smb_encrypt;
 }
 
-bool get_cmdline_auth_info_use_machine_account(struct user_auth_info *auth_info)
+bool get_cmdline_auth_info_use_machine_account(const struct user_auth_info *auth_info)
 {
        return auth_info->use_machine_account;
 }
 
 struct user_auth_info *get_cmdline_auth_info_copy(TALLOC_CTX *mem_ctx,
-                                                 struct user_auth_info *src)
+                                                 const struct user_auth_info *src)
 {
        struct user_auth_info *result;
 
@@ -455,6 +469,32 @@ bool set_cmdline_auth_info_machine_account_creds(struct user_auth_info *auth_inf
        return true;
 }
 
+/****************************************************************************
+ Ensure we have a password if one not given.
+****************************************************************************/
+
+void set_cmdline_auth_info_getpass(struct user_auth_info *auth_info)
+{
+       char *label = NULL;
+       char *pass;
+       TALLOC_CTX *frame;
+
+       if (get_cmdline_auth_info_got_pass(auth_info) ||
+                       get_cmdline_auth_info_use_kerberos(auth_info)) {
+               /* Already got one... */
+               return;
+       }
+
+       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);
+       }
+       TALLOC_FREE(frame);
+}
+
 /****************************************************************************
  Add a gid to an array of gids if it's not already there.
 ****************************************************************************/
@@ -797,43 +837,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.
 ********************************************************************/
@@ -924,41 +927,11 @@ void smb_msleep(unsigned int t)
 #endif
 }
 
-/****************************************************************************
- Become a daemon, discarding the controlling terminal.
-****************************************************************************/
-
-void become_daemon(bool Fork, bool no_process_group)
-{
-       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,
+NTSTATUS reinit_after_fork(struct messaging_context *msg_ctx,
                       struct event_context *ev_ctx,
                       bool parent_longlived)
 {
-       NTSTATUS status;
+       NTSTATUS status = NT_STATUS_OK;
 
        /* Reset the state of the random
         * number generation system, so
@@ -969,7 +942,8 @@ 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) {
@@ -985,11 +959,10 @@ bool reinit_after_fork(struct messaging_context *msg_ctx,
                if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(0,("messaging_reinit() failed: %s\n",
                                 nt_errstr(status)));
-                       return false;
                }
        }
-
-       return true;
+ done:
+       return status;
 }
 
 /****************************************************************************
@@ -1208,35 +1181,6 @@ void add_to_large_array(TALLOC_CTX *mem_ctx, size_t element_size,
        *array_size = -1;
 }
 
-/****************************************************************************
- Get my own name and IP.
-****************************************************************************/
-
-char *talloc_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.
 ****************************************************************************/
@@ -2237,7 +2181,7 @@ char *myhostname(void)
        if (ret == NULL) {
                /* This is cached forever so
                 * use talloc_autofree_context() ctx. */
-               ret = talloc_get_myname(talloc_autofree_context());
+               ret = get_myname(talloc_autofree_context());
        }
        return ret;
 }
@@ -2392,7 +2336,7 @@ bool parent_dirname(TALLOC_CTX *mem_ctx, const char *dir, char **parent,
                        return False;
                }
                if (name) {
-                       *name = "";
+                       *name = dir;
                }
                return True;
        }
@@ -2799,14 +2743,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;
        }
@@ -2814,10 +2759,19 @@ 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;
+       }
+       return result;
 }
 
 char *procid_str(TALLOC_CTX *mem_ctx, const struct server_id *pid)
@@ -3198,9 +3152,9 @@ NTSTATUS split_ntfs_stream_name(TALLOC_CTX *mem_ctx, const char *fname,
        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);
 }
@@ -3234,3 +3188,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;
+}