Revert "Replace cli_rpc_pipe_close by a talloc destructor on rpc_pipe_struct"
authorVolker Lendecke <vl@samba.org>
Fri, 25 Apr 2008 14:33:35 +0000 (16:33 +0200)
committerVolker Lendecke <vl@samba.org>
Fri, 25 Apr 2008 14:33:35 +0000 (16:33 +0200)
This reverts commit 99fc3283c4ecc791f5a242bd1983b4352ce3e6cf.

15 files changed:
source/client/client.c
source/libnet/libnet_join.c
source/libsmb/clientgen.c
source/libsmb/libsmb_dir.c
source/libsmb/passchange.c
source/rpc_client/cli_pipe.c
source/rpcclient/cmd_test.c
source/rpcclient/rpcclient.c
source/utils/net_rpc.c
source/utils/net_rpc_join.c
source/utils/net_rpc_shell.c
source/utils/net_util.c
source/utils/smbcacls.c
source/utils/smbtree.c
source/winbindd/winbindd_cm.c

index b4e1985a837b3a14eb1fed04106ff8469c4102d0..1c9c21e979f442a8b6fee829e81a42f9b670d92b 100644 (file)
@@ -3658,7 +3658,7 @@ static bool browse_host_rpc(bool sort)
                                              &werr);
 
        if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
-               TALLOC_FREE(pipe_hnd);
+               cli_rpc_pipe_close(pipe_hnd);
                TALLOC_FREE(frame);
                return false;
        }
@@ -3668,7 +3668,7 @@ static bool browse_host_rpc(bool sort)
                browse_fn(info.name, info.type, info.comment, NULL);
        }
 
-       TALLOC_FREE(pipe_hnd);
+       cli_rpc_pipe_close(pipe_hnd);
        TALLOC_FREE(frame);
        return true;
 }
index 7e348e25a576dcb5030c10601642fc506bf2f19e..370509840a5d40a650e27b5a10cee1fbd53723c4 100644 (file)
@@ -706,7 +706,7 @@ static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
        }
 
        rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
-       TALLOC_FREE(pipe_hnd);
+       cli_rpc_pipe_close(pipe_hnd);
 
  done:
        return status;
@@ -951,7 +951,7 @@ static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
        if (is_valid_policy_hnd(&user_pol)) {
                rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
        }
-       TALLOC_FREE(pipe_hnd);
+       cli_rpc_pipe_close(pipe_hnd);
 
        return status;
 }
@@ -1217,7 +1217,7 @@ done:
        if (pipe_hnd) {
                rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
                rpccli_samr_Close(pipe_hnd, mem_ctx, &sam_pol);
-               TALLOC_FREE(pipe_hnd);
+               cli_rpc_pipe_close(pipe_hnd);
        }
 
        if (cli) {
index e64b6fa278a233f08ac24102c5ff23e33e90ff9b..ef2c2639cd531db778ed8e43290f424e7bbcfc00 100644 (file)
@@ -616,17 +616,55 @@ struct cli_state *cli_initialise(void)
         return NULL;
 }
 
+/****************************************************************************
+ External interface.
+ Close an open named pipe over SMB. Free any authentication data.
+ Returns false if the cli_close call failed.
+ ****************************************************************************/
+
+bool cli_rpc_pipe_close(struct rpc_pipe_client *cli)
+{
+       bool ret;
+
+       if (!cli) {
+               return false;
+       }
+
+       ret = cli_close(cli->cli, cli->fnum);
+
+       if (!ret) {
+               DEBUG(1,("cli_rpc_pipe_close: cli_close failed on pipe %s, "
+                         "fnum 0x%x "
+                         "to machine %s.  Error was %s\n",
+                         cli->pipe_name,
+                         (int) cli->fnum,
+                         cli->desthost,
+                         cli_errstr(cli->cli)));
+       }
+
+       if (cli->auth.cli_auth_data_free_func) {
+               (*cli->auth.cli_auth_data_free_func)(&cli->auth);
+       }
+
+       DEBUG(10,("cli_rpc_pipe_close: closed pipe %s to machine %s\n",
+               cli->pipe_name, cli->desthost ));
+
+       DLIST_REMOVE(cli->cli->pipe_list, cli);
+       talloc_destroy(cli);
+       return ret;
+}
+
 /****************************************************************************
  Close all pipes open on this session.
 ****************************************************************************/
 
 void cli_nt_pipes_close(struct cli_state *cli)
 {
-       while (cli->pipe_list != NULL) {
-               /*
-                * No TALLOC_FREE here!
-                */
-               talloc_free(cli->pipe_list);
+       struct rpc_pipe_client *cp, *next;
+
+       for (cp = cli->pipe_list; cp; cp = next) {
+               next = cp->next;
+               cli_rpc_pipe_close(cp);
        }
 }
 
index aea4f103b6f314c2aa8375c4e8aef1de3c7f1778..612a8772c0efdb6a93882bb4c890a1450b1cbb2d 100644 (file)
@@ -319,7 +319,7 @@ net_share_enum_rpc(struct cli_state *cli,
 
 done:
         /* Close the server service pipe */
-        TALLOC_FREE(pipe_hnd);
+        cli_rpc_pipe_close(pipe_hnd);
 
         /* Tell 'em if it worked */
         return W_ERROR_IS_OK(result) ? 0 : -1;
index 8f7cbf265e96fd89578f26d5f8366a2df0c03ce3..2f9a87dee4a70aa10a8c0fe19531b5bccc0cc181 100644 (file)
@@ -196,7 +196,7 @@ NTSTATUS remote_password_change(const char *remote_machine, const char *user_nam
        }
 
        /* OK, that failed, so try again... */
-       TALLOC_FREE(pipe_hnd);
+       cli_rpc_pipe_close(pipe_hnd);
        
        /* Try anonymous NTLMSSP... */
        cli_init_creds(cli, "", "", NULL);
index b0d6f8eafd6c5f6fe05bd9db264b29d1be440c39..3e03887bfdb00c842aa2f461dbab7bac2271e392 100644 (file)
@@ -1066,10 +1066,7 @@ static NTSTATUS create_schannel_auth_rpc_bind_req( struct rpc_pipe_client *cli,
        /* Use lp_workgroup() if domain not specified */
 
        if (!cli->domain || !cli->domain[0]) {
-               cli->domain = talloc_strdup(cli, lp_workgroup());
-               if (cli->domain == NULL) {
-                       return NT_STATUS_NO_MEMORY;
-               }
+               cli->domain = lp_workgroup();
        }
 
        init_rpc_auth_schannel_neg(&schannel_neg, cli->domain, global_myname());
@@ -2140,30 +2137,6 @@ bool rpccli_is_pipe_idx(struct rpc_pipe_client *cli, int pipe_idx)
        return (cli->abstract_syntax == pipe_names[pipe_idx].abstr_syntax);
 }
 
-static int rpc_pipe_destructor(struct rpc_pipe_client *p)
-{
-       bool ret;
-
-       ret = cli_close(p->cli, p->fnum);
-       if (!ret) {
-               DEBUG(1, ("rpc_pipe_destructor: cli_close failed on pipe %s, "
-                         "fnum 0x%x to machine %s.  Error was %s\n",
-                         p->pipe_name, (int) p->fnum,
-                         p->desthost, cli_errstr(p->cli)));
-       }
-
-       if (p->auth.cli_auth_data_free_func) {
-               (*p->auth.cli_auth_data_free_func)(&p->auth);
-       }
-
-       DEBUG(10, ("rpc_pipe_destructor: closed pipe %s to machine %s\n",
-                  p->pipe_name, p->desthost ));
-
-       DLIST_REMOVE(p->cli->pipe_list, p);
-
-       return ret ? -1 : 0;
-}
-
 /****************************************************************************
  Open a named pipe over SMB to a remote server.
  *
@@ -2247,8 +2220,6 @@ static struct rpc_pipe_client *cli_rpc_pipe_open(struct cli_state *cli, int pipe
        result->fnum = fnum;
 
        DLIST_ADD(cli->pipe_list, result);
-       talloc_set_destructor(result, rpc_pipe_destructor);
-
        *perr = NT_STATUS_OK;
 
        return result;
@@ -2277,7 +2248,7 @@ struct rpc_pipe_client *cli_rpc_pipe_open_noauth(struct cli_state *cli, int pipe
                }
                DEBUG(lvl, ("cli_rpc_pipe_open_noauth: rpc_pipe_bind for pipe %s failed with error %s\n",
                        cli_get_pipe_name(pipe_idx), nt_errstr(*perr) ));
-               TALLOC_FREE(result);
+               cli_rpc_pipe_close(result);
                return NULL;
        }
 
@@ -2389,7 +2360,7 @@ static struct rpc_pipe_client *cli_rpc_pipe_open_ntlmssp_internal(struct cli_sta
 
   err:
 
-       TALLOC_FREE(result);
+       cli_rpc_pipe_close(result);
        return NULL;
 }
 
@@ -2510,7 +2481,7 @@ struct rpc_pipe_client *get_schannel_session_key(struct cli_state *cli,
        if (!get_schannel_session_key_common(netlogon_pipe, cli, domain,
                                             pneg_flags, perr))
        {
-               TALLOC_FREE(netlogon_pipe);
+               cli_rpc_pipe_close(netlogon_pipe);
                return NULL;
        }
 
@@ -2540,7 +2511,7 @@ struct rpc_pipe_client *cli_rpc_pipe_open_schannel_with_key(struct cli_state *cl
        result->auth.a_u.schannel_auth = TALLOC_ZERO_P(
                result, struct schannel_auth_struct);
        if (!result->auth.a_u.schannel_auth) {
-               TALLOC_FREE(result);
+               cli_rpc_pipe_close(result);
                *perr = NT_STATUS_NO_MEMORY;
                return NULL;
        }
@@ -2548,7 +2519,7 @@ struct rpc_pipe_client *cli_rpc_pipe_open_schannel_with_key(struct cli_state *cl
        TALLOC_FREE(result->domain);
        result->domain = talloc_strdup(result, domain);
        if (result->domain == NULL) {
-               TALLOC_FREE(result);
+               cli_rpc_pipe_close(result);
                *perr = NT_STATUS_NO_MEMORY;
                return NULL;
        }
@@ -2559,7 +2530,7 @@ struct rpc_pipe_client *cli_rpc_pipe_open_schannel_with_key(struct cli_state *cl
        if (!NT_STATUS_IS_OK(*perr)) {
                DEBUG(0, ("cli_rpc_pipe_open_schannel_with_key: cli_rpc_pipe_bind failed with error %s\n",
                        nt_errstr(*perr) ));
-               TALLOC_FREE(result);
+               cli_rpc_pipe_close(result);
                return NULL;
        }
 
@@ -2599,7 +2570,7 @@ static struct rpc_pipe_client *get_schannel_session_key_auth_ntlmssp(struct cli_
        if (!get_schannel_session_key_common(netlogon_pipe, cli, domain,
                                             pneg_flags, perr))
        {
-               TALLOC_FREE(netlogon_pipe);
+               cli_rpc_pipe_close(netlogon_pipe);
                return NULL;
        }
 
@@ -2638,7 +2609,7 @@ struct rpc_pipe_client *cli_rpc_pipe_open_ntlmssp_auth_schannel(struct cli_state
                                domain, netlogon_pipe->dc, perr);
 
        /* Now we've bound using the session key we can close the netlog pipe. */
-       TALLOC_FREE(netlogon_pipe);
+       cli_rpc_pipe_close(netlogon_pipe);
 
        return result;
 }
@@ -2671,7 +2642,7 @@ struct rpc_pipe_client *cli_rpc_pipe_open_schannel(struct cli_state *cli,
                                domain, netlogon_pipe->dc, perr);
 
        /* Now we've bound using the session key we can close the netlog pipe. */
-       TALLOC_FREE(netlogon_pipe);
+       cli_rpc_pipe_close(netlogon_pipe);
 
        return result;
 }
@@ -2716,7 +2687,7 @@ struct rpc_pipe_client *cli_rpc_pipe_open_krb5(struct cli_state *cli,
                service_princ = talloc_asprintf(result, "%s$@%s",
                                                cli->desthost, lp_realm() );
                if (!service_princ) {
-                       TALLOC_FREE(result);
+                       cli_rpc_pipe_close(result);
                        return NULL;
                }
        }
@@ -2725,7 +2696,7 @@ struct rpc_pipe_client *cli_rpc_pipe_open_krb5(struct cli_state *cli,
        if (username && password) {
                int ret = kerberos_kinit_password(username, password, 0, NULL);
                if (ret) {
-                       TALLOC_FREE(result);
+                       cli_rpc_pipe_close(result);
                        return NULL;
                }
        }
@@ -2733,7 +2704,7 @@ struct rpc_pipe_client *cli_rpc_pipe_open_krb5(struct cli_state *cli,
        result->auth.a_u.kerberos_auth = TALLOC_ZERO_P(
                result, struct kerberos_auth_struct);
        if (!result->auth.a_u.kerberos_auth) {
-               TALLOC_FREE(result);
+               cli_rpc_pipe_close(result);
                *perr = NT_STATUS_NO_MEMORY;
                return NULL;
        }
@@ -2745,7 +2716,7 @@ struct rpc_pipe_client *cli_rpc_pipe_open_krb5(struct cli_state *cli,
        if (!NT_STATUS_IS_OK(*perr)) {
                DEBUG(0, ("cli_rpc_pipe_open_krb5: cli_rpc_pipe_bind failed with error %s\n",
                        nt_errstr(*perr) ));
-               TALLOC_FREE(result);
+               cli_rpc_pipe_close(result);
                return NULL;
        }
 
index f9ea5c0fc2d6167947f3cdb84ad4ebe123a709a8..089d7bcaa3f8a2fc511b64eb4218b4e550c7d692 100644 (file)
@@ -48,8 +48,8 @@ static NTSTATUS cmd_testme(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
                goto done;
 
  done:
-       TALLOC_FREE(lsa_pipe);
-       TALLOC_FREE(samr_pipe);
+       if (lsa_pipe != NULL) cli_rpc_pipe_close(lsa_pipe);
+       if (samr_pipe != NULL) cli_rpc_pipe_close(samr_pipe);
 
        return status;
 }
index ebd38044b84636e2c2b20118ce01c33138f810f0..8592e0a3b60ce6479db8a44df9c0d21badbfe79d 100644 (file)
@@ -169,7 +169,7 @@ static void fetch_machine_sid(struct cli_state *cli)
        sid_copy(&domain_sid, info->account_domain.sid);
 
        rpccli_lsa_Close(lsapipe, mem_ctx, &pol);
-       TALLOC_FREE(lsapipe);
+       cli_rpc_pipe_close(lsapipe);
        talloc_destroy(mem_ctx);
 
        return;
@@ -177,7 +177,7 @@ static void fetch_machine_sid(struct cli_state *cli)
  error:
 
        if (lsapipe) {
-               TALLOC_FREE(lsapipe);
+               cli_rpc_pipe_close(lsapipe);
        }
 
        fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
@@ -336,7 +336,7 @@ static NTSTATUS cmd_set_ss_level(void)
 
                        if (tmp_set->rpc_pipe->auth.auth_type != pipe_default_auth_type ||
                                        tmp_set->rpc_pipe->auth.auth_level != pipe_default_auth_level) {
-                               TALLOC_FREE(tmp_set->rpc_pipe);
+                               cli_rpc_pipe_close(tmp_set->rpc_pipe);
                                tmp_set->rpc_pipe = NULL;
                        }
                }
index 136272b8b1a4115289f63c7f1afdd062dcf0f7be..f4e1bf601660444271f1abd6d7fb2e7afea62693 100644 (file)
@@ -88,7 +88,7 @@ NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
        *domain_sid = info->account_domain.sid;
 
        rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
-       TALLOC_FREE(lsa_pipe);
+       cli_rpc_pipe_close(lsa_pipe);
 
        return NT_STATUS_OK;
 }
@@ -185,7 +185,7 @@ int run_rpc_command(struct cli_state *cli_arg,
                
        if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
                if (pipe_hnd) {
-                       TALLOC_FREE(pipe_hnd);
+                       cli_rpc_pipe_close(pipe_hnd);
                }
        }
 
@@ -1972,7 +1972,7 @@ static NTSTATUS get_sid_from_name(struct cli_state *cli,
 
  done:
        if (pipe_hnd) {
-               TALLOC_FREE(pipe_hnd);
+               cli_rpc_pipe_close(pipe_hnd);
        }
 
        if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
@@ -2747,14 +2747,14 @@ static NTSTATUS rpc_list_alias_members(struct rpc_pipe_client *pipe_hnd,
 
        if (!NT_STATUS_IS_OK(result)) {
                d_fprintf(stderr, "Couldn't open LSA policy handle\n");
-               TALLOC_FREE(lsa_pipe);
+               cli_rpc_pipe_close(lsa_pipe);
                return result;
        }
 
        alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
        if (!alias_sids) {
                d_fprintf(stderr, "Out of memory\n");
-               TALLOC_FREE(lsa_pipe);
+               cli_rpc_pipe_close(lsa_pipe);
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -2769,7 +2769,7 @@ static NTSTATUS rpc_list_alias_members(struct rpc_pipe_client *pipe_hnd,
        if (!NT_STATUS_IS_OK(result) &&
            !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
                d_fprintf(stderr, "Couldn't lookup SIDs\n");
-               TALLOC_FREE(lsa_pipe);
+               cli_rpc_pipe_close(lsa_pipe);
                return result;
        }
 
@@ -2789,7 +2789,7 @@ static NTSTATUS rpc_list_alias_members(struct rpc_pipe_client *pipe_hnd,
                }
        }
 
-       TALLOC_FREE(lsa_pipe);
+       cli_rpc_pipe_close(lsa_pipe);
        return NT_STATUS_OK;
 }
  
@@ -5651,7 +5651,7 @@ static NTSTATUS rpc_trustdom_get_pdc(struct cli_state *cli,
                                       domain_name,
                                       &buffer,
                                       NULL);
-       TALLOC_FREE(netr);
+       cli_rpc_pipe_close(netr);
 
        if (NT_STATUS_IS_OK(status)) {
                return status;
@@ -6247,7 +6247,7 @@ static int rpc_trustdom_list(int argc, const char **argv)
                return -1;
        };
        
-       TALLOC_FREE(pipe_hnd);
+       cli_rpc_pipe_close(pipe_hnd);
 
        /*
         * Listing trusting domains (stored in passdb backend, if local)
index c94e9d1a4089c2f0755bc988fc65f44bc9cddb4f..b868ea824fb6745167b679238fcf3332f53ccd59 100644 (file)
@@ -221,7 +221,7 @@ int net_rpc_join_newstyle(int argc, const char **argv)
        domain_sid = info->account_domain.sid;
 
        rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
-       TALLOC_FREE(pipe_hnd); /* Done with this pipe */
+       cli_rpc_pipe_close(pipe_hnd); /* Done with this pipe */
 
        /* Bail out if domain didn't get set. */
        if (!domain) {
@@ -368,7 +368,7 @@ int net_rpc_join_newstyle(int argc, const char **argv)
                                         &set_info);
 
        rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
-       TALLOC_FREE(pipe_hnd); /* Done with this pipe */
+       cli_rpc_pipe_close(pipe_hnd); /* Done with this pipe */
 
        /* Now check the whole process from top-to-bottom */
 
@@ -429,10 +429,10 @@ int net_rpc_join_newstyle(int argc, const char **argv)
 
                        goto done;
                }
-               TALLOC_FREE(netlogon_schannel_pipe);
+               cli_rpc_pipe_close(netlogon_schannel_pipe);
        }
 
-       TALLOC_FREE(pipe_hnd);
+       cli_rpc_pipe_close(pipe_hnd);
 
        /* Now store the secret in the secrets database */
 
index 7bd726e614f6fbc9e0097970dae26d7b1a73e189..e6302b652edaccead48d98be41962533f336d061 100644 (file)
@@ -85,7 +85,7 @@ static NTSTATUS net_sh_run(struct rpc_sh_ctx *ctx, struct rpc_sh_cmd *cmd,
 
        status = cmd->fn(mem_ctx, ctx, pipe_hnd, argc, argv);
 
-       TALLOC_FREE(pipe_hnd);
+       cli_rpc_pipe_close(pipe_hnd);
 
        talloc_destroy(mem_ctx);
 
index 576c2191b31763f16972b83599f7bde68103dfde..f844992d5622071a1723c7cd54a3e39318850227 100644 (file)
@@ -75,7 +75,7 @@ NTSTATUS net_rpc_lookup_name(TALLOC_CTX *mem_ctx, struct cli_state *cli,
        if (is_valid_policy_hnd(&pol)) {
                rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
        }
-       TALLOC_FREE(lsa_pipe);
+       cli_rpc_pipe_close(lsa_pipe);
 
        return result;
 }
index af14c622dc9616e685945eded431f88b97c84a58..134f5617603ffc44f35ac81af6c24ad08e9c69d7 100644 (file)
@@ -103,7 +103,9 @@ static NTSTATUS cli_lsa_lookup_sid(struct cli_state *cli,
 
        status = NT_STATUS_OK;
  fail:
-       TALLOC_FREE(p);
+       if (p != NULL) {
+               cli_rpc_pipe_close(p);
+       }
        cli_tdis(cli);
        cli->cnum = orig_cnum;
        TALLOC_FREE(frame);
@@ -149,7 +151,9 @@ static NTSTATUS cli_lsa_lookup_name(struct cli_state *cli,
 
        status = NT_STATUS_OK;
  fail:
-       TALLOC_FREE(p);
+       if (p != NULL) {
+               cli_rpc_pipe_close(p);
+       }
        cli_tdis(cli);
        cli->cnum = orig_cnum;
        TALLOC_FREE(frame);
index c2b364d1e9ac80250dbb3d60ab97bbf36f361933..d2dd1b49d32ac6d192d4a043c8c31bb4e85479ee 100644 (file)
@@ -188,7 +188,7 @@ static bool get_rpc_shares(struct cli_state *cli,
 
        if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
                TALLOC_FREE(mem_ctx);
-               TALLOC_FREE(pipe_hnd);
+               cli_rpc_pipe_close(pipe_hnd);
                return False;
        }
 
@@ -198,7 +198,7 @@ static bool get_rpc_shares(struct cli_state *cli,
        }
 
        TALLOC_FREE(mem_ctx);
-       TALLOC_FREE(pipe_hnd);
+       cli_rpc_pipe_close(pipe_hnd);
        return True;
 }
 
index d27f3e47f58d6f59917a995b7b12686f4a6d7120..ba682d9745b78c0245ce680f7002c0aea96ebea0 100644 (file)
@@ -1436,27 +1436,33 @@ void invalidate_cm_connection(struct winbindd_cm_conn *conn)
        }
 
        if (conn->samr_pipe != NULL) {
-               TALLOC_FREE(conn->samr_pipe);
-               /* Ok, it must be dead. Drop timeout to 0.5 sec. */
-               if (conn->cli) {
-                       cli_set_timeout(conn->cli, 500);
+               if (!cli_rpc_pipe_close(conn->samr_pipe)) {
+                       /* Ok, it must be dead. Drop timeout to 0.5 sec. */
+                       if (conn->cli) {
+                               cli_set_timeout(conn->cli, 500);
+                       }
                }
+               conn->samr_pipe = NULL;
        }
 
        if (conn->lsa_pipe != NULL) {
-               TALLOC_FREE(conn->lsa_pipe);
-               /* Ok, it must be dead. Drop timeout to 0.5 sec. */
-               if (conn->cli) {
-                       cli_set_timeout(conn->cli, 500);
+               if (!cli_rpc_pipe_close(conn->lsa_pipe)) {
+                       /* Ok, it must be dead. Drop timeout to 0.5 sec. */
+                       if (conn->cli) {
+                               cli_set_timeout(conn->cli, 500);
+                       }
                }
+               conn->lsa_pipe = NULL;
        }
 
        if (conn->netlogon_pipe != NULL) {
-               TALLOC_FREE(conn->netlogon_pipe);
-               /* Ok, it must be dead. Drop timeout to 0.5 sec. */
-               if (conn->cli) {
-                       cli_set_timeout(conn->cli, 500);
+               if (!cli_rpc_pipe_close(conn->netlogon_pipe)) {
+                       /* Ok, it must be dead. Drop timeout to 0.5 sec. */
+                       if (conn->cli) {
+                               cli_set_timeout(conn->cli, 500);
+                       }
                }
+               conn->netlogon_pipe = NULL;
        }
 
        if (conn->cli) {
@@ -1709,7 +1715,7 @@ static void set_dc_type_and_flags_connect( struct winbindd_domain *domain )
                                                                  DS_ROLE_BASIC_INFORMATION,
                                                                  &info,
                                                                  &werr);
-       TALLOC_FREE(cli);
+       cli_rpc_pipe_close(cli);
 
        if (!NT_STATUS_IS_OK(result)) {
                DEBUG(5, ("set_dc_type_and_flags_connect: rpccli_ds_getprimarydominfo "
@@ -1743,7 +1749,7 @@ no_dssetup:
                DEBUG(5, ("set_dc_type_and_flags_connect: Could not bind to "
                          "PI_LSARPC on domain %s: (%s)\n",
                          domain->name, nt_errstr(result)));
-               TALLOC_FREE(cli);
+               cli_rpc_pipe_close(cli);
                TALLOC_FREE(mem_ctx);
                return;
        }
@@ -1823,7 +1829,7 @@ done:
        DEBUG(5,("set_dc_type_and_flags_connect: domain %s is %srunning active directory.\n",
                  domain->name, domain->active_directory ? "" : "NOT "));
 
-       TALLOC_FREE(cli);
+       cli_rpc_pipe_close(cli);
 
        TALLOC_FREE(mem_ctx);
 
@@ -1969,7 +1975,7 @@ NTSTATUS cm_connect_sam(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
        DEBUG(10,("cm_connect_sam: ntlmssp-sealed rpccli_samr_Connect2 "
                  "failed for domain %s, error was %s. Trying schannel\n",
                  domain->name, nt_errstr(result) ));
-       TALLOC_FREE(conn->samr_pipe);
+       cli_rpc_pipe_close(conn->samr_pipe);
 
  schannel:
 
@@ -2004,7 +2010,7 @@ NTSTATUS cm_connect_sam(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
        DEBUG(10,("cm_connect_sam: schannel-sealed rpccli_samr_Connect2 failed "
                  "for domain %s, error was %s. Trying anonymous\n",
                  domain->name, nt_errstr(result) ));
-       TALLOC_FREE(conn->samr_pipe);
+       cli_rpc_pipe_close(conn->samr_pipe);
 
  anonymous:
 
@@ -2106,7 +2112,7 @@ NTSTATUS cm_connect_lsa(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
        DEBUG(10,("cm_connect_lsa: rpccli_lsa_open_policy failed, trying "
                  "schannel\n"));
 
-       TALLOC_FREE(conn->lsa_pipe);
+       cli_rpc_pipe_close(conn->lsa_pipe);
 
  schannel:
 
@@ -2141,7 +2147,7 @@ NTSTATUS cm_connect_lsa(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
        DEBUG(10,("cm_connect_lsa: rpccli_lsa_open_policy failed, trying "
                  "anonymous\n"));
 
-       TALLOC_FREE(conn->lsa_pipe);
+       cli_rpc_pipe_close(conn->lsa_pipe);
 
  anonymous:
 
@@ -2216,7 +2222,7 @@ NTSTATUS cm_connect_netlogon(struct winbindd_domain *domain,
        if (!get_trust_pw_hash(domain->name, mach_pwd, &account_name,
                               &sec_chan_type))
        {
-               TALLOC_FREE(netlogon_pipe);
+               cli_rpc_pipe_close(netlogon_pipe);
                return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
        }
 
@@ -2231,14 +2237,14 @@ NTSTATUS cm_connect_netlogon(struct winbindd_domain *domain,
                 &neg_flags);
 
        if (!NT_STATUS_IS_OK(result)) {
-               TALLOC_FREE(netlogon_pipe);
+               cli_rpc_pipe_close(netlogon_pipe);
                return result;
        }
 
        if ((lp_client_schannel() == True) &&
                        ((neg_flags & NETLOGON_NEG_SCHANNEL) == 0)) {
                DEBUG(3, ("Server did not offer schannel\n"));
-               TALLOC_FREE(netlogon_pipe);
+               cli_rpc_pipe_close(netlogon_pipe);
                return NT_STATUS_ACCESS_DENIED;
        }
 
@@ -2271,7 +2277,7 @@ NTSTATUS cm_connect_netlogon(struct winbindd_domain *domain,
                                                    &result);
 
        /* We can now close the initial netlogon pipe. */
-       TALLOC_FREE(netlogon_pipe);
+       cli_rpc_pipe_close(netlogon_pipe);
 
        if (conn->netlogon_pipe == NULL) {
                DEBUG(3, ("Could not open schannel'ed NETLOGON pipe. Error "