s3-winbind: Fix Bug #7568: Make sure cm_connect_lsa_tcp does not reset the secure...
[amitay/samba.git] / source3 / winbindd / winbindd_cm.c
index e06e30e0a89fdbe1a40cd9011c0b6b321b8fd0a6..958daf794e8095e71ee952394c062cfdc6312ed6 100644 (file)
 
 #include "includes.h"
 #include "winbindd.h"
+#include "../libcli/auth/libcli_auth.h"
+#include "../librpc/gen_ndr/cli_netlogon.h"
+#include "rpc_client/cli_netlogon.h"
+#include "../librpc/gen_ndr/cli_samr.h"
+#include "../librpc/gen_ndr/cli_lsa.h"
+#include "rpc_client/cli_lsarpc.h"
+#include "../librpc/gen_ndr/cli_dssetup.h"
+#include "libads/sitename_cache.h"
+#include "librpc/gen_ndr/messaging.h"
+#include "libsmb/clidgram.h"
+#include "ads.h"
+#include "secrets.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
@@ -174,9 +186,6 @@ static bool fork_child_dc_connect(struct winbindd_domain *domain)
        pid_t parent_pid = sys_getpid();
        char *lfile = NULL;
 
-       /* Stop zombies */
-       CatchChild();
-
        if (domain->dc_probe_pid != (pid_t)-1) {
                /*
                 * We might already have a DC probe
@@ -355,7 +364,7 @@ void set_domain_offline(struct winbindd_domain *domain)
                return;
        }
 
-       /* If we're in statup mode, check again in 10 seconds, not in
+       /* If we're in startup mode, check again in 10 seconds, not in
           lp_winbind_reconnect_delay() seconds (which is 30 seconds by default). */
 
        calc_new_online_timeout_check(domain);
@@ -484,6 +493,12 @@ void set_domain_online_request(struct winbindd_domain *domain)
                return;
        }
 
+       if (domain->internal) {
+               DEBUG(10, ("set_domain_online_request: Internal domains are "
+                          "always online\n"));
+               return;
+       }
+
        /* We've been told it's safe to go online and
           try and connect to a DC. But I don't believe it
           because network manager seems to lie.
@@ -678,7 +693,7 @@ static bool get_dc_name_via_netlogon(struct winbindd_domain *domain,
 
        DEBUG(10,("rpccli_netr_GetAnyDCName returned %s\n", dcname));
 
-       if (!resolve_name(dcname, dc_ss, 0x20)) {
+       if (!resolve_name(dcname, dc_ss, 0x20, true)) {
                return False;
        }
 
@@ -768,7 +783,8 @@ static NTSTATUS cm_prepare_connection(const struct winbindd_domain *domain,
        struct sockaddr peeraddr;
        socklen_t peeraddr_len;
 
-       struct sockaddr_in *peeraddr_in = (struct sockaddr_in *)&peeraddr;
+       struct sockaddr_in *peeraddr_in =
+               (struct sockaddr_in *)(void *)&peeraddr;
 
        DEBUG(10,("cm_prepare_connection: connecting to DC %s for domain %s\n",
                controller, domain->name ));
@@ -797,11 +813,31 @@ static NTSTATUS cm_prepare_connection(const struct winbindd_domain *domain,
 
        peeraddr_len = sizeof(peeraddr);
 
-       if ((getpeername((*cli)->fd, &peeraddr, &peeraddr_len) != 0) ||
-           (peeraddr_len != sizeof(struct sockaddr_in)) ||
-           (peeraddr_in->sin_family != PF_INET))
-       {
-               DEBUG(0,("cm_prepare_connection: %s\n", strerror(errno)));
+       if ((getpeername((*cli)->fd, &peeraddr, &peeraddr_len) != 0)) {
+               DEBUG(0,("cm_prepare_connection: getpeername failed with: %s\n",
+                       strerror(errno)));
+               result = NT_STATUS_UNSUCCESSFUL;
+               goto done;
+       }
+
+       if ((peeraddr_len != sizeof(struct sockaddr_in))
+#ifdef HAVE_IPV6
+           && (peeraddr_len != sizeof(struct sockaddr_in6))
+#endif
+           ) {
+               DEBUG(0,("cm_prepare_connection: got unexpected peeraddr len %d\n",
+                       peeraddr_len));
+               result = NT_STATUS_UNSUCCESSFUL;
+               goto done;
+       }
+
+       if ((peeraddr_in->sin_family != PF_INET)
+#ifdef HAVE_IPV6
+           && (peeraddr_in->sin_family != PF_INET6)
+#endif
+           ) {
+               DEBUG(0,("cm_prepare_connection: got unexpected family %d\n",
+                       peeraddr_in->sin_family));
                result = NT_STATUS_UNSUCCESSFUL;
                goto done;
        }
@@ -856,7 +892,7 @@ static NTSTATUS cm_prepare_connection(const struct winbindd_domain *domain,
                                                              machine_krb5_principal, 
                                                              machine_password,
                                                              lp_workgroup(),
-                                                             domain->name);
+                                                             domain->alt_name);
 
                        if (!ADS_ERR_OK(ads_status)) {
                                DEBUG(4,("failed kerberos session setup with %s\n",
@@ -1039,7 +1075,9 @@ static bool add_one_dc_unique(TALLOC_CTX *mem_ctx, const char *domain_name,
 
        /* Make sure there's no duplicates in the list */
        for (i=0; i<*num; i++)
-               if (sockaddr_equal((struct sockaddr *)&(*dcs)[i].ss, (struct sockaddr *)pss))
+               if (sockaddr_equal(
+                           (struct sockaddr *)(void *)&(*dcs)[i].ss,
+                           (struct sockaddr *)(void *)pss))
                        return False;
 
        *dcs = TALLOC_REALLOC_ARRAY(mem_ctx, *dcs, struct dc_name_ip, (*num)+1);
@@ -1119,7 +1157,8 @@ static bool dcip_to_name(TALLOC_CTX *mem_ctx,
                                        create_local_private_krb5_conf_for_domain(domain->alt_name,
                                                                        domain->name,
                                                                        sitename,
-                                                                       pss);
+                                                                       pss,
+                                                                       name);
 
                                        SAFE_FREE(sitename);
                                } else {
@@ -1127,7 +1166,8 @@ static bool dcip_to_name(TALLOC_CTX *mem_ctx,
                                        create_local_private_krb5_conf_for_domain(domain->alt_name,
                                                                        domain->name,
                                                                        NULL,
-                                                                       pss);
+                                                                       pss,
+                                                                       name);
                                }
                                winbindd_set_locator_kdc_envs(domain);
 
@@ -1463,7 +1503,7 @@ static NTSTATUS cm_open_connection(struct winbindd_domain *domain,
 
                if (*domain->dcname 
                        && NT_STATUS_IS_OK(check_negative_conn_cache( domain->name, domain->dcname))
-                       && (resolve_name(domain->dcname, &domain->dcaddr, 0x20)))
+                       && (resolve_name(domain->dcname, &domain->dcaddr, 0x20, true)))
                {
                        struct sockaddr_storage *addrs = NULL;
                        int num_addrs = 0;
@@ -1536,6 +1576,10 @@ void invalidate_cm_connection(struct winbindd_cm_conn *conn)
        }
 
        if (conn->samr_pipe != NULL) {
+               if (is_valid_policy_hnd(&conn->sam_connect_handle)) {
+                       rpccli_samr_Close(conn->samr_pipe, talloc_tos(),
+                                         &conn->sam_connect_handle);
+               }
                TALLOC_FREE(conn->samr_pipe);
                /* Ok, it must be dead. Drop timeout to 0.5 sec. */
                if (conn->cli) {
@@ -1544,6 +1588,10 @@ void invalidate_cm_connection(struct winbindd_cm_conn *conn)
        }
 
        if (conn->lsa_pipe != NULL) {
+               if (is_valid_policy_hnd(&conn->lsa_policy)) {
+                       rpccli_lsa_Close(conn->lsa_pipe, talloc_tos(),
+                                        &conn->lsa_policy);
+               }
                TALLOC_FREE(conn->lsa_pipe);
                /* Ok, it must be dead. Drop timeout to 0.5 sec. */
                if (conn->cli) {
@@ -1551,6 +1599,18 @@ void invalidate_cm_connection(struct winbindd_cm_conn *conn)
                }
        }
 
+       if (conn->lsa_pipe_tcp != NULL) {
+               if (is_valid_policy_hnd(&conn->lsa_policy)) {
+                       rpccli_lsa_Close(conn->lsa_pipe, talloc_tos(),
+                                        &conn->lsa_policy);
+               }
+               TALLOC_FREE(conn->lsa_pipe_tcp);
+               /* Ok, it must be dead. Drop timeout to 0.5 sec. */
+               if (conn->cli) {
+                       cli_set_timeout(conn->cli, 500);
+               }
+       }
+
        if (conn->netlogon_pipe != NULL) {
                TALLOC_FREE(conn->netlogon_pipe);
                /* Ok, it must be dead. Drop timeout to 0.5 sec. */
@@ -1571,34 +1631,29 @@ void close_conns_after_fork(void)
        struct winbindd_domain *domain;
 
        for (domain = domain_list(); domain; domain = domain->next) {
-               if (domain->conn.cli == NULL)
-                       continue;
+               struct cli_state *cli = domain->conn.cli;
 
-               if (domain->conn.cli->fd == -1)
-                       continue;
+               /*
+                * first close the low level SMB TCP connection
+                * so that we don't generate any SMBclose
+                * requests in invalidate_cm_connection()
+                */
+               if (cli && cli->fd != -1) {
+                       close(domain->conn.cli->fd);
+                       domain->conn.cli->fd = -1;
+               }
 
-               close(domain->conn.cli->fd);
-               domain->conn.cli->fd = -1;
+               invalidate_cm_connection(&domain->conn);
        }
 }
 
 static bool connection_ok(struct winbindd_domain *domain)
 {
-       if (domain->conn.cli == NULL) {
-               DEBUG(8, ("connection_ok: Connection to %s for domain %s has NULL "
-                         "cli!\n", domain->dcname, domain->name));
-               return False;
-       }
+       bool ok;
 
-       if (!domain->conn.cli->initialised) {
-               DEBUG(3, ("connection_ok: Connection to %s for domain %s was never "
-                         "initialised!\n", domain->dcname, domain->name));
-               return False;
-       }
-
-       if (domain->conn.cli->fd == -1) {
-               DEBUG(3, ("connection_ok: Connection to %s for domain %s has died or was "
-                         "never started (fd == -1)\n", 
+       ok = cli_state_is_connected(domain->conn.cli);
+       if (!ok) {
+               DEBUG(3, ("connection_ok: Connection to %s for domain %s is not connected\n",
                          domain->dcname, domain->name));
                return False;
        }
@@ -1624,6 +1679,12 @@ static NTSTATUS init_dc_connection_network(struct winbindd_domain *domain)
                return NT_STATUS_OK;
        }
 
+       if (!winbindd_can_contact_domain(domain)) {
+               invalidate_cm_connection(&domain->conn);
+               domain->initialized = True;
+               return NT_STATUS_OK;
+       }
+
        if (connection_ok(domain)) {
                if (!domain->initialized) {
                        set_dc_type_and_flags(domain);
@@ -1644,6 +1705,10 @@ static NTSTATUS init_dc_connection_network(struct winbindd_domain *domain)
 
 NTSTATUS init_dc_connection(struct winbindd_domain *domain)
 {
+       if (domain->internal) {
+               return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
+       }
+
        if (domain->initialized && !domain->online) {
                /* We check for online status elsewhere. */
                return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
@@ -1652,6 +1717,23 @@ NTSTATUS init_dc_connection(struct winbindd_domain *domain)
        return init_dc_connection_network(domain);
 }
 
+static NTSTATUS init_dc_connection_rpc(struct winbindd_domain *domain)
+{
+       NTSTATUS status;
+
+       status = init_dc_connection(domain);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       if (!domain->internal && domain->conn.cli == NULL) {
+               /* happens for trusted domains without inbound trust */
+               return NT_STATUS_TRUSTED_DOMAIN_FAILURE;
+       }
+
+       return NT_STATUS_OK;
+}
+
 /******************************************************************************
  Set the trust flags (direction and forest location) for a domain
 ******************************************************************************/
@@ -1747,9 +1829,6 @@ static bool set_dc_type_and_flags_trustinfo( struct winbindd_domain *domain )
 
                        domain->initialized = True;
 
-                       if ( !winbindd_can_contact_domain( domain) )
-                               domain->internal = True;
-
                        break;
                }               
        }
@@ -1772,8 +1851,8 @@ static void set_dc_type_and_flags_connect( struct winbindd_domain *domain )
        NTSTATUS                result;
        WERROR werr;
        TALLOC_CTX              *mem_ctx = NULL;
-       struct rpc_pipe_client  *cli;
-       POLICY_HND pol;
+       struct rpc_pipe_client  *cli = NULL;
+       struct policy_handle pol;
        union dssetup_DsRoleInfo info;
        union lsa_PolicyInformation *lsa_info = NULL;
 
@@ -1851,7 +1930,7 @@ no_dssetup:
        }
 
        result = rpccli_lsa_open_policy2(cli, mem_ctx, True, 
-                                        SEC_RIGHTS_MAXIMUM_ALLOWED, &pol);
+                                        SEC_FLAG_MAXIMUM_ALLOWED, &pol);
 
        if (NT_STATUS_IS_OK(result)) {
                /* This particular query is exactly what Win2k clients use 
@@ -1893,7 +1972,7 @@ no_dssetup:
                domain->active_directory = False;
 
                result = rpccli_lsa_open_policy(cli, mem_ctx, True, 
-                                               SEC_RIGHTS_MAXIMUM_ALLOWED,
+                                               SEC_FLAG_MAXIMUM_ALLOWED,
                                                &pol);
 
                if (!NT_STATUS_IS_OK(result)) {
@@ -1925,6 +2004,8 @@ done:
        DEBUG(5,("set_dc_type_and_flags_connect: domain %s is %srunning active directory.\n",
                  domain->name, domain->active_directory ? "" : "NOT "));
 
+       domain->can_do_ncacn_ip_tcp = domain->active_directory;
+
        TALLOC_FREE(cli);
 
        TALLOC_FREE(mem_ctx);
@@ -1963,8 +2044,8 @@ static void set_dc_type_and_flags( struct winbindd_domain *domain )
 /**********************************************************************
 ***********************************************************************/
 
-static bool cm_get_schannel_dcinfo(struct winbindd_domain *domain,
-                                  struct dcinfo **ppdc)
+static bool cm_get_schannel_creds(struct winbindd_domain *domain,
+                                  struct netlogon_creds_CredentialState **ppdc)
 {
        NTSTATUS result;
        struct rpc_pipe_client *netlogon_pipe;
@@ -1978,7 +2059,7 @@ static bool cm_get_schannel_dcinfo(struct winbindd_domain *domain,
                return False;
        }
 
-       /* Return a pointer to the struct dcinfo from the
+       /* Return a pointer to the struct netlogon_creds_CredentialState from the
           netlogon pipe. */
 
        if (!domain->conn.netlogon_pipe->dc) {
@@ -1990,26 +2071,35 @@ static bool cm_get_schannel_dcinfo(struct winbindd_domain *domain,
 }
 
 NTSTATUS cm_connect_sam(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
-                       struct rpc_pipe_client **cli, POLICY_HND *sam_handle)
+                       struct rpc_pipe_client **cli, struct policy_handle *sam_handle)
 {
        struct winbindd_cm_conn *conn;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       struct dcinfo *p_dcinfo;
+       struct netlogon_creds_CredentialState *p_creds;
        char *machine_password = NULL;
        char *machine_account = NULL;
        char *domain_name = NULL;
 
-       result = init_dc_connection(domain);
+       if (strequal(domain->name, get_global_sam_name())) {
+               result = open_internal_samr_conn(mem_ctx, domain, cli, sam_handle);
+               if (!NT_STATUS_IS_OK(result)) {
+                       return result;
+               }
+               return NT_STATUS_OK;
+       }
+
+       result = init_dc_connection_rpc(domain);
        if (!NT_STATUS_IS_OK(result)) {
                return result;
        }
 
        conn = &domain->conn;
 
-       if (conn->samr_pipe != NULL) {
+       if (rpccli_is_connected(conn->samr_pipe)) {
                goto done;
        }
 
+       TALLOC_FREE(conn->samr_pipe);
 
        /*
         * No SAMR pipe yet. Attempt to get an NTLMSSP SPNEGO authenticated
@@ -2045,7 +2135,8 @@ NTSTATUS cm_connect_sam(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
           authenticated SAMR pipe with sign & seal. */
        result = cli_rpc_pipe_open_spnego_ntlmssp(conn->cli,
                                                  &ndr_table_samr.syntax_id,
-                                                 PIPE_AUTH_LEVEL_PRIVACY,
+                                                 NCACN_NP,
+                                                 DCERPC_AUTH_LEVEL_PRIVACY,
                                                  domain_name,
                                                  machine_account,
                                                  machine_password,
@@ -2067,7 +2158,7 @@ NTSTATUS cm_connect_sam(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
 
        result = rpccli_samr_Connect2(conn->samr_pipe, mem_ctx,
                                      conn->samr_pipe->desthost,
-                                     SEC_RIGHTS_MAXIMUM_ALLOWED,
+                                     SEC_FLAG_MAXIMUM_ALLOWED,
                                      &conn->sam_connect_handle);
        if (NT_STATUS_IS_OK(result)) {
                goto open_domain;
@@ -2081,15 +2172,16 @@ NTSTATUS cm_connect_sam(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
 
        /* Fall back to schannel if it's a W2K pre-SP1 box. */
 
-       if (!cm_get_schannel_dcinfo(domain, &p_dcinfo)) {
+       if (!cm_get_schannel_creds(domain, &p_creds)) {
                /* If this call fails - conn->cli can now be NULL ! */
                DEBUG(10, ("cm_connect_sam: Could not get schannel auth info "
                           "for domain %s, trying anon\n", domain->name));
                goto anonymous;
        }
        result = cli_rpc_pipe_open_schannel_with_key
-               (conn->cli, &ndr_table_samr.syntax_id, PIPE_AUTH_LEVEL_PRIVACY,
-                domain->name, p_dcinfo, &conn->samr_pipe);
+               (conn->cli, &ndr_table_samr.syntax_id, NCACN_NP,
+                DCERPC_AUTH_LEVEL_PRIVACY,
+                domain->name, &p_creds, &conn->samr_pipe);
 
        if (!NT_STATUS_IS_OK(result)) {
                DEBUG(10,("cm_connect_sam: failed to connect to SAMR pipe for "
@@ -2102,7 +2194,7 @@ NTSTATUS cm_connect_sam(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
 
        result = rpccli_samr_Connect2(conn->samr_pipe, mem_ctx,
                                      conn->samr_pipe->desthost,
-                                     SEC_RIGHTS_MAXIMUM_ALLOWED,
+                                     SEC_FLAG_MAXIMUM_ALLOWED,
                                      &conn->sam_connect_handle);
        if (NT_STATUS_IS_OK(result)) {
                goto open_domain;
@@ -2124,7 +2216,7 @@ NTSTATUS cm_connect_sam(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
 
        result = rpccli_samr_Connect2(conn->samr_pipe, mem_ctx,
                                      conn->samr_pipe->desthost,
-                                     SEC_RIGHTS_MAXIMUM_ALLOWED,
+                                     SEC_FLAG_MAXIMUM_ALLOWED,
                                      &conn->sam_connect_handle);
        if (!NT_STATUS_IS_OK(result)) {
                DEBUG(10,("cm_connect_sam: rpccli_samr_Connect2 failed "
@@ -2137,13 +2229,24 @@ NTSTATUS cm_connect_sam(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
        result = rpccli_samr_OpenDomain(conn->samr_pipe,
                                        mem_ctx,
                                        &conn->sam_connect_handle,
-                                       SEC_RIGHTS_MAXIMUM_ALLOWED,
+                                       SEC_FLAG_MAXIMUM_ALLOWED,
                                        &domain->sid,
                                        &conn->sam_domain_handle);
 
  done:
 
-       if (!NT_STATUS_IS_OK(result)) {
+       if (NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
+               /*
+                * if we got access denied, we might just have no access rights
+                * to talk to the remote samr server server (e.g. when we are a
+                * PDC and we are connecting a w2k8 pdc via an interdomain
+                * trust). In that case do not invalidate the whole connection
+                * stack
+                */
+               TALLOC_FREE(conn->samr_pipe);
+               ZERO_STRUCT(conn->sam_domain_handle);
+               return result;
+       } else if (!NT_STATUS_IS_OK(result)) {
                invalidate_cm_connection(conn);
                return result;
        }
@@ -2155,23 +2258,83 @@ NTSTATUS cm_connect_sam(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
        return result;
 }
 
+/**********************************************************************
+ open an schanneld ncacn_ip_tcp connection to LSA
+***********************************************************************/
+
+NTSTATUS cm_connect_lsa_tcp(struct winbindd_domain *domain,
+                           TALLOC_CTX *mem_ctx,
+                           struct rpc_pipe_client **cli)
+{
+       struct winbindd_cm_conn *conn;
+       struct netlogon_creds_CredentialState *creds;
+       NTSTATUS status;
+
+       DEBUG(10,("cm_connect_lsa_tcp\n"));
+
+       status = init_dc_connection_rpc(domain);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       conn = &domain->conn;
+
+       if (conn->lsa_pipe_tcp &&
+           conn->lsa_pipe_tcp->transport->transport == NCACN_IP_TCP &&
+           conn->lsa_pipe_tcp->auth->auth_level == DCERPC_AUTH_LEVEL_PRIVACY &&
+           rpccli_is_connected(conn->lsa_pipe_tcp)) {
+               goto done;
+       }
+
+       TALLOC_FREE(conn->lsa_pipe_tcp);
+
+       if (!cm_get_schannel_creds(domain, &creds)) {
+               goto done;
+       }
+
+       status = cli_rpc_pipe_open_schannel_with_key(conn->cli,
+                                                    &ndr_table_lsarpc.syntax_id,
+                                                    NCACN_IP_TCP,
+                                                    DCERPC_AUTH_LEVEL_PRIVACY,
+                                                    domain->name,
+                                                    &creds,
+                                                    &conn->lsa_pipe_tcp);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(10,("cli_rpc_pipe_open_schannel_with_key failed: %s\n",
+                       nt_errstr(status)));
+               goto done;
+       }
+
+ done:
+       if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(conn->lsa_pipe_tcp);
+               return status;
+       }
+
+       *cli = conn->lsa_pipe_tcp;
+
+       return status;
+}
+
 NTSTATUS cm_connect_lsa(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
-                       struct rpc_pipe_client **cli, POLICY_HND *lsa_policy)
+                       struct rpc_pipe_client **cli, struct policy_handle *lsa_policy)
 {
        struct winbindd_cm_conn *conn;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       struct dcinfo *p_dcinfo;
+       struct netlogon_creds_CredentialState *p_creds;
 
-       result = init_dc_connection(domain);
+       result = init_dc_connection_rpc(domain);
        if (!NT_STATUS_IS_OK(result))
                return result;
 
        conn = &domain->conn;
 
-       if (conn->lsa_pipe != NULL) {
+       if (rpccli_is_connected(conn->lsa_pipe)) {
                goto done;
        }
 
+       TALLOC_FREE(conn->lsa_pipe);
+
        if ((conn->cli->user_name[0] == '\0') ||
            (conn->cli->domain[0] == '\0') || 
            (conn->cli->password == NULL || conn->cli->password[0] == '\0')) {
@@ -2183,8 +2346,8 @@ NTSTATUS cm_connect_lsa(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
        /* We have an authenticated connection. Use a NTLMSSP SPNEGO
         * authenticated LSA pipe with sign & seal. */
        result = cli_rpc_pipe_open_spnego_ntlmssp
-               (conn->cli, &ndr_table_lsarpc.syntax_id,
-                PIPE_AUTH_LEVEL_PRIVACY,
+               (conn->cli, &ndr_table_lsarpc.syntax_id, NCACN_NP,
+                DCERPC_AUTH_LEVEL_PRIVACY,
                 conn->cli->domain, conn->cli->user_name, conn->cli->password,
                 &conn->lsa_pipe);
 
@@ -2202,7 +2365,7 @@ NTSTATUS cm_connect_lsa(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
                  domain->name, conn->cli->domain, conn->cli->user_name ));
 
        result = rpccli_lsa_open_policy(conn->lsa_pipe, mem_ctx, True,
-                                       SEC_RIGHTS_MAXIMUM_ALLOWED,
+                                       SEC_FLAG_MAXIMUM_ALLOWED,
                                        &conn->lsa_policy);
        if (NT_STATUS_IS_OK(result)) {
                goto done;
@@ -2217,16 +2380,16 @@ NTSTATUS cm_connect_lsa(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
 
        /* Fall back to schannel if it's a W2K pre-SP1 box. */
 
-       if (!cm_get_schannel_dcinfo(domain, &p_dcinfo)) {
+       if (!cm_get_schannel_creds(domain, &p_creds)) {
                /* If this call fails - conn->cli can now be NULL ! */
                DEBUG(10, ("cm_connect_lsa: Could not get schannel auth info "
                           "for domain %s, trying anon\n", domain->name));
                goto anonymous;
        }
        result = cli_rpc_pipe_open_schannel_with_key
-               (conn->cli, &ndr_table_lsarpc.syntax_id,
-                PIPE_AUTH_LEVEL_PRIVACY,
-                domain->name, p_dcinfo, &conn->lsa_pipe);
+               (conn->cli, &ndr_table_lsarpc.syntax_id, NCACN_NP,
+                DCERPC_AUTH_LEVEL_PRIVACY,
+                domain->name, &p_creds, &conn->lsa_pipe);
 
        if (!NT_STATUS_IS_OK(result)) {
                DEBUG(10,("cm_connect_lsa: failed to connect to LSA pipe for "
@@ -2238,7 +2401,7 @@ NTSTATUS cm_connect_lsa(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
                  "schannel.\n", domain->name ));
 
        result = rpccli_lsa_open_policy(conn->lsa_pipe, mem_ctx, True,
-                                       SEC_RIGHTS_MAXIMUM_ALLOWED,
+                                       SEC_FLAG_MAXIMUM_ALLOWED,
                                        &conn->lsa_policy);
        if (NT_STATUS_IS_OK(result)) {
                goto done;
@@ -2260,7 +2423,7 @@ NTSTATUS cm_connect_lsa(struct winbindd_domain *domain, TALLOC_CTX *mem_ctx,
        }
 
        result = rpccli_lsa_open_policy(conn->lsa_pipe, mem_ctx, True,
-                                       SEC_RIGHTS_MAXIMUM_ALLOWED,
+                                       SEC_FLAG_MAXIMUM_ALLOWED,
                                        &conn->lsa_policy);
  done:
        if (!NT_STATUS_IS_OK(result)) {
@@ -2286,24 +2449,26 @@ NTSTATUS cm_connect_netlogon(struct winbindd_domain *domain,
 
        uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
        uint8  mach_pwd[16];
-       uint32  sec_chan_type;
+       enum netr_SchannelType sec_chan_type;
        const char *account_name;
        struct rpc_pipe_client *netlogon_pipe = NULL;
 
        *cli = NULL;
 
-       result = init_dc_connection(domain);
+       result = init_dc_connection_rpc(domain);
        if (!NT_STATUS_IS_OK(result)) {
                return result;
        }
 
        conn = &domain->conn;
 
-       if (conn->netlogon_pipe != NULL) {
+       if (rpccli_is_connected(conn->netlogon_pipe)) {
                *cli = conn->netlogon_pipe;
                return NT_STATUS_OK;
        }
 
+       TALLOC_FREE(conn->netlogon_pipe);
+
        result = cli_rpc_pipe_open_noauth(conn->cli,
                                          &ndr_table_netlogon.syntax_id,
                                          &netlogon_pipe);
@@ -2371,8 +2536,8 @@ NTSTATUS cm_connect_netlogon(struct winbindd_domain *domain,
        */
 
        result = cli_rpc_pipe_open_schannel_with_key(
-               conn->cli, &ndr_table_netlogon.syntax_id,
-               PIPE_AUTH_LEVEL_PRIVACY, domain->name, netlogon_pipe->dc,
+               conn->cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
+               DCERPC_AUTH_LEVEL_PRIVACY, domain->name, &netlogon_pipe->dc,
                &conn->netlogon_pipe);
 
        /* We can now close the initial netlogon pipe. */
@@ -2382,14 +2547,18 @@ NTSTATUS cm_connect_netlogon(struct winbindd_domain *domain,
                DEBUG(3, ("Could not open schannel'ed NETLOGON pipe. Error "
                          "was %s\n", nt_errstr(result)));
 
-               /* make sure we return something besides OK */
-               return !NT_STATUS_IS_OK(result) ? result : NT_STATUS_PIPE_NOT_AVAILABLE;
+               invalidate_cm_connection(conn);
+               return result;
        }
 
        /*
-        * Try NetSamLogonEx for AD domains
+        * Always try netr_LogonSamLogonEx. We will fall back for NT4
+        * which gives DCERPC_FAULT_OP_RNG_ERROR (function not
+        * supported). We used to only try SamLogonEx for AD, but
+        * Samba DCs can also do it. And because we don't distinguish
+        * between Samba and NT4, always try it once.
         */
-       domain->can_do_samlogon_ex = domain->active_directory;
+       domain->can_do_samlogon_ex = true;
 
        *cli = conn->netlogon_pipe;
        return NT_STATUS_OK;