s3:rpc_client: implement dcerpc_binding_handle_auth_info()
[mat/samba.git] / source3 / rpc_client / cli_pipe.c
index 8b9e513b000ce0ff6c068a652b79cb3a1e5067b4..59dc7b6dc2a967f0feeadddbdc458ffb65586767 100644 (file)
 #include "includes.h"
 #include "../lib/util/tevent_ntstatus.h"
 #include "librpc/gen_ndr/ndr_epmapper_c.h"
-#include "../librpc/gen_ndr/ndr_schannel.h"
 #include "../librpc/gen_ndr/ndr_dssetup.h"
 #include "../libcli/auth/schannel.h"
-#include "../libcli/auth/spnego.h"
-#include "../auth/ntlmssp/ntlmssp.h"
 #include "auth_generic.h"
 #include "librpc/gen_ndr/ndr_dcerpc.h"
 #include "librpc/gen_ndr/ndr_netlogon_c.h"
@@ -70,7 +67,7 @@ static uint32 get_rpc_call_id(void)
  ********************************************************************/
 
 struct rpc_read_state {
-       struct event_context *ev;
+       struct tevent_context *ev;
        struct rpc_cli_transport *transport;
        uint8_t *data;
        size_t size;
@@ -80,7 +77,7 @@ struct rpc_read_state {
 static void rpc_read_done(struct tevent_req *subreq);
 
 static struct tevent_req *rpc_read_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+                                       struct tevent_context *ev,
                                        struct rpc_cli_transport *transport,
                                        uint8_t *data, size_t size)
 {
@@ -150,7 +147,7 @@ static NTSTATUS rpc_read_recv(struct tevent_req *req)
 }
 
 struct rpc_write_state {
-       struct event_context *ev;
+       struct tevent_context *ev;
        struct rpc_cli_transport *transport;
        const uint8_t *data;
        size_t size;
@@ -160,7 +157,7 @@ struct rpc_write_state {
 static void rpc_write_done(struct tevent_req *subreq);
 
 static struct tevent_req *rpc_write_send(TALLOC_CTX *mem_ctx,
-                                        struct event_context *ev,
+                                        struct tevent_context *ev,
                                         struct rpc_cli_transport *transport,
                                         const uint8_t *data, size_t size)
 {
@@ -235,7 +232,7 @@ static NTSTATUS rpc_write_recv(struct tevent_req *req)
  ****************************************************************************/
 
 struct get_complete_frag_state {
-       struct event_context *ev;
+       struct tevent_context *ev;
        struct rpc_pipe_client *cli;
        uint16_t frag_len;
        DATA_BLOB *pdu;
@@ -245,7 +242,7 @@ static void get_complete_frag_got_header(struct tevent_req *subreq);
 static void get_complete_frag_got_rest(struct tevent_req *subreq);
 
 static struct tevent_req *get_complete_frag_send(TALLOC_CTX *mem_ctx,
-                                                struct event_context *ev,
+                                                struct tevent_context *ev,
                                                 struct rpc_pipe_client *cli,
                                                 DATA_BLOB *pdu)
 {
@@ -509,7 +506,7 @@ static NTSTATUS cli_pipe_validate_current_pdu(TALLOC_CTX *mem_ctx,
 ****************************************************************************/
 
 struct cli_api_pipe_state {
-       struct event_context *ev;
+       struct tevent_context *ev;
        struct rpc_cli_transport *transport;
        uint8_t *rdata;
        uint32_t rdata_len;
@@ -520,7 +517,7 @@ static void cli_api_pipe_write_done(struct tevent_req *subreq);
 static void cli_api_pipe_read_done(struct tevent_req *subreq);
 
 static struct tevent_req *cli_api_pipe_send(TALLOC_CTX *mem_ctx,
-                                           struct event_context *ev,
+                                           struct tevent_context *ev,
                                            struct rpc_cli_transport *transport,
                                            uint8_t *data, size_t data_len,
                                            uint32_t max_rdata_len)
@@ -690,7 +687,7 @@ static NTSTATUS cli_api_pipe_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
  ****************************************************************************/
 
 struct rpc_api_pipe_state {
-       struct event_context *ev;
+       struct tevent_context *ev;
        struct rpc_pipe_client *cli;
        uint8_t expected_pkt_type;
 
@@ -708,7 +705,7 @@ static void rpc_api_pipe_got_pdu(struct tevent_req *subreq);
 static void rpc_api_pipe_auth3_done(struct tevent_req *subreq);
 
 static struct tevent_req *rpc_api_pipe_send(TALLOC_CTX *mem_ctx,
-                                           struct event_context *ev,
+                                           struct tevent_context *ev,
                                            struct rpc_pipe_client *cli,
                                            DATA_BLOB *data, /* Outgoing PDU */
                                            uint8_t expected_pkt_type)
@@ -725,9 +722,6 @@ static struct tevent_req *rpc_api_pipe_send(TALLOC_CTX *mem_ctx,
        state->ev = ev;
        state->cli = cli;
        state->expected_pkt_type = expected_pkt_type;
-       state->incoming_frag = data_blob_null;
-       state->reply_pdu = data_blob_null;
-       state->reply_pdu_offset = 0;
        state->endianess = DCERPC_DREP_LE;
 
        /*
@@ -996,43 +990,6 @@ static NTSTATUS create_generic_auth_rpc_bind_req(struct rpc_pipe_client *cli,
        return gensec_update(gensec_security, mem_ctx, NULL, null_blob, auth_token);
 }
 
-/*******************************************************************
- Creates schannel auth bind.
- ********************************************************************/
-
-static NTSTATUS create_schannel_auth_rpc_bind_req(struct rpc_pipe_client *cli,
-                                                 DATA_BLOB *auth_token)
-{
-       NTSTATUS status;
-       struct NL_AUTH_MESSAGE r;
-
-       /* Use lp_workgroup() if domain not specified */
-
-       if (!cli->auth->domain || !cli->auth->domain[0]) {
-               cli->auth->domain = talloc_strdup(cli, lp_workgroup());
-               if (cli->auth->domain == NULL) {
-                       return NT_STATUS_NO_MEMORY;
-               }
-       }
-
-       /*
-        * Now marshall the data into the auth parse_struct.
-        */
-
-       r.MessageType                   = NL_NEGOTIATE_REQUEST;
-       r.Flags                         = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
-                                         NL_FLAG_OEM_NETBIOS_COMPUTER_NAME;
-       r.oem_netbios_domain.a          = cli->auth->domain;
-       r.oem_netbios_computer.a        = lp_netbios_name();
-
-       status = dcerpc_push_schannel_bind(cli, &r, auth_token);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
-
-       return NT_STATUS_OK;
-}
-
 /*******************************************************************
  Creates the internals of a DCE/RPC bind request or alter context PDU.
  ********************************************************************/
@@ -1100,12 +1057,6 @@ static NTSTATUS create_rpc_bind_req(TALLOC_CTX *mem_ctx,
 
        switch (auth->auth_type) {
        case DCERPC_AUTH_TYPE_SCHANNEL:
-               ret = create_schannel_auth_rpc_bind_req(cli, &auth_token);
-               if (!NT_STATUS_IS_OK(ret)) {
-                       return ret;
-               }
-               break;
-
        case DCERPC_AUTH_TYPE_NTLMSSP:
        case DCERPC_AUTH_TYPE_KRB5:
        case DCERPC_AUTH_TYPE_SPNEGO:
@@ -1163,7 +1114,7 @@ static NTSTATUS create_rpc_bind_req(TALLOC_CTX *mem_ctx,
  ********************************************************************/
 
 struct rpc_api_pipe_req_state {
-       struct event_context *ev;
+       struct tevent_context *ev;
        struct rpc_pipe_client *cli;
        uint8_t op_num;
        uint32_t call_id;
@@ -1179,7 +1130,7 @@ static NTSTATUS prepare_next_frag(struct rpc_api_pipe_req_state *state,
                                  bool *is_last_frag);
 
 struct tevent_req *rpc_api_pipe_req_send(TALLOC_CTX *mem_ctx,
-                                        struct event_context *ev,
+                                        struct tevent_context *ev,
                                         struct rpc_pipe_client *cli,
                                         uint8_t op_num,
                                         DATA_BLOB *req_data)
@@ -1540,20 +1491,14 @@ static NTSTATUS create_rpc_alter_context(TALLOC_CTX *mem_ctx,
 ****************************************************************************/
 
 struct rpc_pipe_bind_state {
-       struct event_context *ev;
+       struct tevent_context *ev;
        struct rpc_pipe_client *cli;
        DATA_BLOB rpc_out;
        bool auth3;
        uint32_t rpc_call_id;
-       struct netr_Authenticator auth;
-       struct netr_Authenticator return_auth;
-       struct netlogon_creds_CredentialState *creds;
-       union netr_Capabilities capabilities;
-       struct netr_LogonGetCapabilities r;
 };
 
 static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq);
-static void rpc_pipe_bind_step_two_trigger(struct tevent_req *req);
 static NTSTATUS rpc_bind_next_send(struct tevent_req *req,
                                   struct rpc_pipe_bind_state *state,
                                   DATA_BLOB *credentials);
@@ -1562,7 +1507,7 @@ static NTSTATUS rpc_bind_finish_send(struct tevent_req *req,
                                     DATA_BLOB *credentials);
 
 struct tevent_req *rpc_pipe_bind_send(TALLOC_CTX *mem_ctx,
-                                     struct event_context *ev,
+                                     struct tevent_context *ev,
                                      struct rpc_pipe_client *cli,
                                      struct pipe_auth_data *auth)
 {
@@ -1656,14 +1601,11 @@ static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq)
 
        case DCERPC_AUTH_TYPE_NONE:
        case DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM:
+       case DCERPC_AUTH_TYPE_SCHANNEL:
                /* Bind complete. */
                tevent_req_done(req);
                return;
 
-       case DCERPC_AUTH_TYPE_SCHANNEL:
-               rpc_pipe_bind_step_two_trigger(req);
-               return;
-
        case DCERPC_AUTH_TYPE_NTLMSSP:
        case DCERPC_AUTH_TYPE_SPNEGO:
        case DCERPC_AUTH_TYPE_KRB5:
@@ -1740,150 +1682,6 @@ err_out:
        tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
 }
 
-static void rpc_pipe_bind_step_two_done(struct tevent_req *subreq);
-
-static void rpc_pipe_bind_step_two_trigger(struct tevent_req *req)
-{
-       struct rpc_pipe_bind_state *state =
-               tevent_req_data(req,
-                               struct rpc_pipe_bind_state);
-       struct dcerpc_binding_handle *b = state->cli->binding_handle;
-       struct schannel_state *schannel_auth =
-               talloc_get_type_abort(state->cli->auth->auth_ctx,
-                                     struct schannel_state);
-       struct tevent_req *subreq;
-
-       if (schannel_auth == NULL ||
-           !ndr_syntax_id_equal(&state->cli->abstract_syntax,
-                                &ndr_table_netlogon.syntax_id)) {
-               tevent_req_done(req);
-               return;
-       }
-
-       ZERO_STRUCT(state->return_auth);
-
-       state->creds = netlogon_creds_copy(state, schannel_auth->creds);
-       if (state->creds == NULL) {
-               tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
-               return;
-       }
-
-       netlogon_creds_client_authenticator(state->creds, &state->auth);
-
-       state->r.in.server_name = state->cli->srv_name_slash;
-       state->r.in.computer_name = state->creds->computer_name;
-       state->r.in.credential = &state->auth;
-       state->r.in.query_level = 1;
-       state->r.in.return_authenticator = &state->return_auth;
-
-       state->r.out.capabilities = &state->capabilities;
-       state->r.out.return_authenticator = &state->return_auth;
-
-       subreq = dcerpc_netr_LogonGetCapabilities_r_send(talloc_tos(),
-                                                        state->ev,
-                                                        b,
-                                                        &state->r);
-       if (subreq == NULL) {
-               tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
-               return;
-       }
-
-       tevent_req_set_callback(subreq, rpc_pipe_bind_step_two_done, req);
-       return;
-}
-
-static void rpc_pipe_bind_step_two_done(struct tevent_req *subreq)
-{
-       struct tevent_req *req =
-               tevent_req_callback_data(subreq,
-                                        struct tevent_req);
-       struct rpc_pipe_bind_state *state =
-               tevent_req_data(req,
-                               struct rpc_pipe_bind_state);
-       NTSTATUS status;
-
-       status = dcerpc_netr_LogonGetCapabilities_r_recv(subreq, talloc_tos());
-       TALLOC_FREE(subreq);
-       if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE)) {
-               if (state->cli->dc->negotiate_flags &
-                   NETLOGON_NEG_SUPPORTS_AES) {
-                       DEBUG(5, ("AES is not supported and the error was %s\n",
-                                 nt_errstr(status)));
-                       tevent_req_nterror(req,
-                                          NT_STATUS_INVALID_NETWORK_RESPONSE);
-                       return;
-               }
-
-               /* This is probably NT */
-               DEBUG(5, ("We are checking against an NT - %s\n",
-                         nt_errstr(status)));
-               tevent_req_done(req);
-               return;
-       } else if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("dcerpc_netr_LogonGetCapabilities_r_recv failed with %s\n",
-                         nt_errstr(status)));
-               tevent_req_nterror(req, status);
-               return;
-       }
-
-       if (NT_STATUS_EQUAL(state->r.out.result, NT_STATUS_NOT_IMPLEMENTED)) {
-               if (state->creds->negotiate_flags & NETLOGON_NEG_SUPPORTS_AES) {
-                       /* This means AES isn't supported. */
-                       DEBUG(5, ("AES is not supported and the error was %s\n",
-                                 nt_errstr(state->r.out.result)));
-                       tevent_req_nterror(req,
-                                          NT_STATUS_INVALID_NETWORK_RESPONSE);
-                       return;
-               }
-
-               /* This is probably an old Samba version */
-               DEBUG(5, ("We are checking against an old Samba version - %s\n",
-                         nt_errstr(state->r.out.result)));
-               tevent_req_done(req);
-               return;
-       }
-
-       /* We need to check the credential state here, cause win2k3 and earlier
-        * returns NT_STATUS_NOT_IMPLEMENTED */
-       if (!netlogon_creds_client_check(state->creds,
-                                        &state->r.out.return_authenticator->cred)) {
-               /*
-                * Server replied with bad credential. Fail.
-                */
-               DEBUG(0,("rpc_pipe_bind_step_two_done: server %s "
-                        "replied with bad credential\n",
-                        state->cli->desthost));
-               tevent_req_nterror(req, NT_STATUS_UNSUCCESSFUL);
-               return;
-       }
-
-       TALLOC_FREE(state->cli->dc);
-       state->cli->dc = talloc_steal(state->cli, state->creds);
-
-       if (!NT_STATUS_IS_OK(state->r.out.result)) {
-               DEBUG(0, ("dcerpc_netr_LogonGetCapabilities_r_recv failed with %s\n",
-                         nt_errstr(state->r.out.result)));
-               tevent_req_nterror(req, state->r.out.result);
-               return;
-       }
-
-       if (state->creds->negotiate_flags !=
-           state->r.out.capabilities->server_capabilities) {
-               DEBUG(0, ("The client capabilities don't match the server "
-                         "capabilities: local[0x%08X] remote[0x%08X]\n",
-                         state->creds->negotiate_flags,
-                         state->capabilities.server_capabilities));
-               tevent_req_nterror(req,
-                                  NT_STATUS_INVALID_NETWORK_RESPONSE);
-               return;
-       }
-
-       /* TODO: Add downgrade dectection. */
-
-       tevent_req_done(req);
-       return;
-}
-
 static NTSTATUS rpc_bind_next_send(struct tevent_req *req,
                                   struct rpc_pipe_bind_state *state,
                                   DATA_BLOB *auth_token)
@@ -1957,11 +1755,11 @@ NTSTATUS rpc_pipe_bind(struct rpc_pipe_client *cli,
                       struct pipe_auth_data *auth)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
+       struct tevent_context *ev;
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -2041,6 +1839,25 @@ static uint32_t rpccli_bh_set_timeout(struct dcerpc_binding_handle *h,
        return rpccli_set_timeout(hs->rpc_cli, timeout);
 }
 
+static void rpccli_bh_auth_info(struct dcerpc_binding_handle *h,
+                               enum dcerpc_AuthType *auth_type,
+                               enum dcerpc_AuthLevel *auth_level)
+{
+       struct rpccli_bh_state *hs = dcerpc_binding_handle_data(h,
+                                    struct rpccli_bh_state);
+
+       if (hs->rpc_cli == NULL) {
+               return;
+       }
+
+       if (hs->rpc_cli->auth == NULL) {
+               return;
+       }
+
+       *auth_type = hs->rpc_cli->auth->auth_type;
+       *auth_level = hs->rpc_cli->auth->auth_level;
+}
+
 struct rpccli_bh_raw_call_state {
        DATA_BLOB in_data;
        DATA_BLOB out_data;
@@ -2220,6 +2037,7 @@ static const struct dcerpc_binding_handle_ops rpccli_bh_ops = {
        .name                   = "rpccli",
        .is_connected           = rpccli_bh_is_connected,
        .set_timeout            = rpccli_bh_set_timeout,
+       .auth_info              = rpccli_bh_auth_info,
        .raw_call_send          = rpccli_bh_raw_call_send,
        .raw_call_recv          = rpccli_bh_raw_call_recv,
        .disconnect_send        = rpccli_bh_disconnect_send,
@@ -2307,6 +2125,7 @@ static NTSTATUS rpccli_generic_bind_data(TALLOC_CTX *mem_ctx,
                                         const char *username,
                                         const char *password,
                                         enum credentials_use_kerberos use_kerberos,
+                                        struct netlogon_creds_CredentialState *creds,
                                         struct pipe_auth_data **presult)
 {
        struct auth_generic_state *auth_generic_ctx;
@@ -2360,6 +2179,7 @@ static NTSTATUS rpccli_generic_bind_data(TALLOC_CTX *mem_ctx,
        }
 
        cli_credentials_set_kerberos_state(auth_generic_ctx->credentials, use_kerberos);
+       cli_credentials_set_netlogon_creds(auth_generic_ctx->credentials, creds);
 
        status = auth_generic_client_start_by_authtype(auth_generic_ctx, auth_type, auth_level);
        if (!NT_STATUS_IS_OK(status)) {
@@ -2376,52 +2196,13 @@ static NTSTATUS rpccli_generic_bind_data(TALLOC_CTX *mem_ctx,
        return status;
 }
 
-NTSTATUS rpccli_schannel_bind_data(TALLOC_CTX *mem_ctx, const char *domain,
-                                  enum dcerpc_AuthLevel auth_level,
-                                  struct netlogon_creds_CredentialState *creds,
-                                  struct pipe_auth_data **presult)
-{
-       struct schannel_state *schannel_auth;
-       struct pipe_auth_data *result;
-
-       result = talloc(mem_ctx, struct pipe_auth_data);
-       if (result == NULL) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       result->auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
-       result->auth_level = auth_level;
-
-       result->user_name = talloc_strdup(result, "");
-       result->domain = talloc_strdup(result, domain);
-       if ((result->user_name == NULL) || (result->domain == NULL)) {
-               goto fail;
-       }
-
-       schannel_auth = talloc_zero(result, struct schannel_state);
-       if (schannel_auth == NULL) {
-               goto fail;
-       }
-
-       schannel_auth->state = SCHANNEL_STATE_START;
-       schannel_auth->initiator = true;
-       schannel_auth->creds = netlogon_creds_copy(result, creds);
-
-       result->auth_ctx = schannel_auth;
-       *presult = result;
-       return NT_STATUS_OK;
-
- fail:
-       TALLOC_FREE(result);
-       return NT_STATUS_NO_MEMORY;
-}
-
 /**
  * Create an rpc pipe client struct, connecting to a tcp port.
  */
 static NTSTATUS rpc_pipe_open_tcp_port(TALLOC_CTX *mem_ctx, const char *host,
+                                      const struct sockaddr_storage *ss_addr,
                                       uint16_t port,
-                                      const struct ndr_syntax_id *abstract_syntax,
+                                      const struct ndr_interface_table *table,
                                       struct rpc_pipe_client **presult)
 {
        struct rpc_pipe_client *result;
@@ -2434,7 +2215,7 @@ static NTSTATUS rpc_pipe_open_tcp_port(TALLOC_CTX *mem_ctx, const char *host,
                return NT_STATUS_NO_MEMORY;
        }
 
-       result->abstract_syntax = *abstract_syntax;
+       result->abstract_syntax = table->syntax_id;
        result->transfer_syntax = ndr_transfer_syntax_ndr;
 
        result->desthost = talloc_strdup(result, host);
@@ -2448,9 +2229,13 @@ static NTSTATUS rpc_pipe_open_tcp_port(TALLOC_CTX *mem_ctx, const char *host,
        result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
        result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
 
-       if (!resolve_name(host, &addr, 0, false)) {
-               status = NT_STATUS_NOT_FOUND;
-               goto fail;
+       if (ss_addr == NULL) {
+               if (!resolve_name(host, &addr, NBT_NAME_SERVER, false)) {
+                       status = NT_STATUS_NOT_FOUND;
+                       goto fail;
+               }
+       } else {
+               addr = *ss_addr;
        }
 
        status = open_socket_out(&addr, port, 60*1000, &fd);
@@ -2487,7 +2272,8 @@ static NTSTATUS rpc_pipe_open_tcp_port(TALLOC_CTX *mem_ctx, const char *host,
  * target host.
  */
 static NTSTATUS rpc_pipe_get_tcp_port(const char *host,
-                                     const struct ndr_syntax_id *abstract_syntax,
+                                     const struct sockaddr_storage *addr,
+                                     const struct ndr_interface_table *table,
                                      uint16_t *pport)
 {
        NTSTATUS status;
@@ -2510,15 +2296,15 @@ static NTSTATUS rpc_pipe_get_tcp_port(const char *host,
                goto done;
        }
 
-       if (ndr_syntax_id_equal(abstract_syntax,
+       if (ndr_syntax_id_equal(&table->syntax_id,
                                &ndr_table_epmapper.syntax_id)) {
                *pport = 135;
                return NT_STATUS_OK;
        }
 
        /* open the connection to the endpoint mapper */
-       status = rpc_pipe_open_tcp_port(tmp_ctx, host, 135,
-                                       &ndr_table_epmapper.syntax_id,
+       status = rpc_pipe_open_tcp_port(tmp_ctx, host, addr, 135,
+                                       &ndr_table_epmapper,
                                        &epm_pipe);
 
        if (!NT_STATUS_IS_OK(status)) {
@@ -2545,7 +2331,7 @@ static NTSTATUS rpc_pipe_get_tcp_port(const char *host,
        }
 
        map_binding->transport = NCACN_IP_TCP;
-       map_binding->object = *abstract_syntax;
+       map_binding->object = table->syntax_id;
        map_binding->host = host; /* needed? */
        map_binding->endpoint = "0"; /* correct? needed? */
 
@@ -2581,7 +2367,7 @@ static NTSTATUS rpc_pipe_get_tcp_port(const char *host,
        status = dcerpc_epm_Map(epm_handle,
                                tmp_ctx,
                                discard_const_p(struct GUID,
-                                             &(abstract_syntax->uuid)),
+                                             &(table->syntax_id.uuid)),
                                map_tower,
                                entry_handle,
                                max_towers,
@@ -2631,26 +2417,27 @@ done:
  * host.
  */
 NTSTATUS rpc_pipe_open_tcp(TALLOC_CTX *mem_ctx, const char *host,
-                          const struct ndr_syntax_id *abstract_syntax,
+                          const struct sockaddr_storage *addr,
+                          const struct ndr_interface_table *table,
                           struct rpc_pipe_client **presult)
 {
        NTSTATUS status;
        uint16_t port = 0;
 
-       status = rpc_pipe_get_tcp_port(host, abstract_syntax, &port);
+       status = rpc_pipe_get_tcp_port(host, addr, table, &port);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
-       return rpc_pipe_open_tcp_port(mem_ctx, host, port,
-                                       abstract_syntax, presult);
+       return rpc_pipe_open_tcp_port(mem_ctx, host, addr, port,
+                                     table, presult);
 }
 
 /********************************************************************
  Create a rpc pipe client struct, connecting to a unix domain socket
  ********************************************************************/
 NTSTATUS rpc_pipe_open_ncalrpc(TALLOC_CTX *mem_ctx, const char *socket_path,
-                              const struct ndr_syntax_id *abstract_syntax,
+                              const struct ndr_interface_table *table,
                               struct rpc_pipe_client **presult)
 {
        struct rpc_pipe_client *result;
@@ -2664,7 +2451,7 @@ NTSTATUS rpc_pipe_open_ncalrpc(TALLOC_CTX *mem_ctx, const char *socket_path,
                return NT_STATUS_NO_MEMORY;
        }
 
-       result->abstract_syntax = *abstract_syntax;
+       result->abstract_syntax = table->syntax_id;
        result->transfer_syntax = ndr_transfer_syntax_ndr;
 
        result->desthost = get_myname(result);
@@ -2743,7 +2530,7 @@ static int rpc_pipe_client_np_ref_destructor(struct rpc_pipe_client_np_ref *np_r
  ****************************************************************************/
 
 static NTSTATUS rpc_pipe_open_np(struct cli_state *cli,
-                                const struct ndr_syntax_id *abstract_syntax,
+                                const struct ndr_interface_table *table,
                                 struct rpc_pipe_client **presult)
 {
        struct rpc_pipe_client *result;
@@ -2761,7 +2548,7 @@ static NTSTATUS rpc_pipe_open_np(struct cli_state *cli,
                return NT_STATUS_NO_MEMORY;
        }
 
-       result->abstract_syntax = *abstract_syntax;
+       result->abstract_syntax = table->syntax_id;
        result->transfer_syntax = ndr_transfer_syntax_ndr;
        result->desthost = talloc_strdup(result, smbXcli_conn_remote_name(cli->conn));
        result->srv_name_slash = talloc_asprintf_strupper_m(
@@ -2775,7 +2562,7 @@ static NTSTATUS rpc_pipe_open_np(struct cli_state *cli,
                return NT_STATUS_NO_MEMORY;
        }
 
-       status = rpc_transport_np_init(result, cli, abstract_syntax,
+       status = rpc_transport_np_init(result, cli, table,
                                       &result->transport);
        if (!NT_STATUS_IS_OK(status)) {
                TALLOC_FREE(result);
@@ -2811,15 +2598,17 @@ static NTSTATUS rpc_pipe_open_np(struct cli_state *cli,
 
 static NTSTATUS cli_rpc_pipe_open(struct cli_state *cli,
                                  enum dcerpc_transport_t transport,
-                                 const struct ndr_syntax_id *interface,
+                                 const struct ndr_interface_table *table,
                                  struct rpc_pipe_client **presult)
 {
        switch (transport) {
        case NCACN_IP_TCP:
-               return rpc_pipe_open_tcp(NULL, smbXcli_conn_remote_name(cli->conn),
-                                        interface, presult);
+               return rpc_pipe_open_tcp(NULL,
+                                        smbXcli_conn_remote_name(cli->conn),
+                                        smbXcli_conn_remote_sockaddr(cli->conn),
+                                        table, presult);
        case NCACN_NP:
-               return rpc_pipe_open_np(cli, interface, presult);
+               return rpc_pipe_open_np(cli, table, presult);
        default:
                return NT_STATUS_NOT_IMPLEMENTED;
        }
@@ -2831,14 +2620,14 @@ static NTSTATUS cli_rpc_pipe_open(struct cli_state *cli,
 
 NTSTATUS cli_rpc_pipe_open_noauth_transport(struct cli_state *cli,
                                            enum dcerpc_transport_t transport,
-                                           const struct ndr_syntax_id *interface,
+                                           const struct ndr_interface_table *table,
                                            struct rpc_pipe_client **presult)
 {
        struct rpc_pipe_client *result;
        struct pipe_auth_data *auth;
        NTSTATUS status;
 
-       status = cli_rpc_pipe_open(cli, transport, interface, &result);
+       status = cli_rpc_pipe_open(cli, transport, table, &result);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -2862,9 +2651,22 @@ NTSTATUS cli_rpc_pipe_open_noauth_transport(struct cli_state *cli,
 
        auth->user_name = talloc_strdup(auth, cli->user_name);
        auth->domain = talloc_strdup(auth, cli->domain);
-       auth->user_session_key = data_blob_talloc(auth,
-               cli->user_session_key.data,
-               cli->user_session_key.length);
+
+       if (transport == NCACN_NP) {
+               struct smbXcli_session *session;
+
+               if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+                       session = cli->smb2.session;
+               } else {
+                       session = cli->smb1.session;
+               }
+
+               status = smbXcli_session_application_key(session, auth,
+                                               &auth->transport_session_key);
+               if (!NT_STATUS_IS_OK(status)) {
+                       auth->transport_session_key = data_blob_null;
+               }
+       }
 
        if ((auth->user_name == NULL) || (auth->domain == NULL)) {
                TALLOC_FREE(result);
@@ -2874,7 +2676,7 @@ NTSTATUS cli_rpc_pipe_open_noauth_transport(struct cli_state *cli,
        status = rpc_pipe_bind(result, auth);
        if (!NT_STATUS_IS_OK(status)) {
                int lvl = 0;
-               if (ndr_syntax_id_equal(interface,
+               if (ndr_syntax_id_equal(&table->syntax_id,
                                        &ndr_table_dssetup.syntax_id)) {
                        /* non AD domains just don't have this pipe, avoid
                         * level 0 statement in that case - gd */
@@ -2882,7 +2684,7 @@ NTSTATUS cli_rpc_pipe_open_noauth_transport(struct cli_state *cli,
                }
                DEBUG(lvl, ("cli_rpc_pipe_open_noauth: rpc_pipe_bind for pipe "
                            "%s failed with error %s\n",
-                           get_pipe_name_from_syntax(talloc_tos(), interface),
+                           table->name,
                            nt_errstr(status) ));
                TALLOC_FREE(result);
                return status;
@@ -2890,7 +2692,7 @@ NTSTATUS cli_rpc_pipe_open_noauth_transport(struct cli_state *cli,
 
        DEBUG(10,("cli_rpc_pipe_open_noauth: opened pipe %s to machine "
                  "%s and bound anonymously.\n",
-                 get_pipe_name_from_syntax(talloc_tos(), interface),
+                 table->name,
                  result->desthost));
 
        *presult = result;
@@ -2901,11 +2703,11 @@ NTSTATUS cli_rpc_pipe_open_noauth_transport(struct cli_state *cli,
  ****************************************************************************/
 
 NTSTATUS cli_rpc_pipe_open_noauth(struct cli_state *cli,
-                                 const struct ndr_syntax_id *interface,
+                                 const struct ndr_interface_table *table,
                                  struct rpc_pipe_client **presult)
 {
        return cli_rpc_pipe_open_noauth_transport(cli, NCACN_NP,
-                                                 interface, presult);
+                                                 table, presult);
 }
 
 /****************************************************************************
@@ -2929,7 +2731,7 @@ NTSTATUS cli_rpc_pipe_open_generic_auth(struct cli_state *cli,
        
        NTSTATUS status;
 
-       status = cli_rpc_pipe_open(cli, transport, &table->syntax_id, &result);
+       status = cli_rpc_pipe_open(cli, transport, table, &result);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -2939,6 +2741,7 @@ NTSTATUS cli_rpc_pipe_open_generic_auth(struct cli_state *cli,
                                          server, target_service,
                                          domain, username, password, 
                                          CRED_AUTO_USE_KERBEROS,
+                                         NULL,
                                          &auth);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("rpccli_generic_bind_data returned %s\n",
@@ -2975,58 +2778,169 @@ NTSTATUS cli_rpc_pipe_open_generic_auth(struct cli_state *cli,
  ****************************************************************************/
 
 NTSTATUS cli_rpc_pipe_open_schannel_with_key(struct cli_state *cli,
-                                            const struct ndr_syntax_id *interface,
+                                            const struct ndr_interface_table *table,
                                             enum dcerpc_transport_t transport,
                                             enum dcerpc_AuthLevel auth_level,
                                             const char *domain,
                                             struct netlogon_creds_CredentialState **pdc,
-                                            struct rpc_pipe_client **presult)
+                                            struct rpc_pipe_client **_rpccli)
 {
-       struct rpc_pipe_client *result;
-       struct pipe_auth_data *auth;
+       struct rpc_pipe_client *rpccli;
+       struct pipe_auth_data *rpcauth;
        NTSTATUS status;
+       NTSTATUS result;
+       struct netlogon_creds_CredentialState save_creds;
+       struct netr_Authenticator auth;
+       struct netr_Authenticator return_auth;
+       union netr_Capabilities capabilities;
+       const char *target_service = table->authservices->names[0];
 
-       status = cli_rpc_pipe_open(cli, transport, interface, &result);
+       status = cli_rpc_pipe_open(cli, transport, table, &rpccli);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
-       status = rpccli_schannel_bind_data(result, domain, auth_level,
-                                          *pdc, &auth);
+       status = rpccli_generic_bind_data(rpccli,
+                                         DCERPC_AUTH_TYPE_SCHANNEL,
+                                         auth_level,
+                                         NULL,
+                                         target_service,
+                                         domain,
+                                         (*pdc)->computer_name,
+                                         NULL,
+                                         CRED_AUTO_USE_KERBEROS,
+                                         *pdc,
+                                         &rpcauth);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("rpccli_schannel_bind_data returned %s\n",
+               DEBUG(0, ("rpccli_generic_bind_data returned %s\n",
                          nt_errstr(status)));
-               TALLOC_FREE(result);
+               TALLOC_FREE(rpccli);
                return status;
        }
 
-       status = rpc_pipe_bind(result, auth);
+       /*
+        * The credentials on a new netlogon pipe are the ones we are passed
+        * in - copy them over
+        *
+        * This may get overwritten... in rpc_pipe_bind()...
+        */
+       rpccli->dc = netlogon_creds_copy(rpccli, *pdc);
+       if (rpccli->dc == NULL) {
+               TALLOC_FREE(rpccli);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       status = rpc_pipe_bind(rpccli, rpcauth);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("cli_rpc_pipe_open_schannel_with_key: "
                          "cli_rpc_pipe_bind failed with error %s\n",
                          nt_errstr(status) ));
-               TALLOC_FREE(result);
+               TALLOC_FREE(rpccli);
+               return status;
+       }
+
+       if (!ndr_syntax_id_equal(&table->syntax_id, &ndr_table_netlogon.syntax_id)) {
+               goto done;
+       }
+
+       save_creds = *rpccli->dc;
+       ZERO_STRUCT(return_auth);
+       ZERO_STRUCT(capabilities);
+
+       netlogon_creds_client_authenticator(&save_creds, &auth);
+
+       status = dcerpc_netr_LogonGetCapabilities(rpccli->binding_handle,
+                                                 talloc_tos(),
+                                                 rpccli->srv_name_slash,
+                                                 save_creds.computer_name,
+                                                 &auth, &return_auth,
+                                                 1, &capabilities,
+                                                 &result);
+       if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE)) {
+               if (save_creds.negotiate_flags & NETLOGON_NEG_SUPPORTS_AES) {
+                       DEBUG(5, ("AES was negotiated and the error was %s - "
+                                 "downgrade detected\n",
+                                 nt_errstr(status)));
+                       TALLOC_FREE(rpccli);
+                       return NT_STATUS_INVALID_NETWORK_RESPONSE;
+               }
+
+               /* This is probably an old Samba Version */
+               DEBUG(5, ("We are checking against an NT or old Samba - %s\n",
+                         nt_errstr(status)));
+               goto done;
+       }
+
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0, ("dcerpc_netr_LogonGetCapabilities failed with %s\n",
+                         nt_errstr(status)));
+               TALLOC_FREE(rpccli);
                return status;
        }
 
+       if (NT_STATUS_EQUAL(result, NT_STATUS_NOT_IMPLEMENTED)) {
+               if (save_creds.negotiate_flags & NETLOGON_NEG_SUPPORTS_AES) {
+                       /* This means AES isn't supported. */
+                       DEBUG(5, ("AES was negotiated and the result was %s - "
+                                 "downgrade detected\n",
+                                 nt_errstr(result)));
+                       TALLOC_FREE(rpccli);
+                       return NT_STATUS_INVALID_NETWORK_RESPONSE;
+               }
+
+               /* This is probably an old Windows version */
+               DEBUG(5, ("We are checking against an win2k3 or Samba - %s\n",
+                         nt_errstr(result)));
+               goto done;
+       }
+
        /*
-        * The credentials on a new netlogon pipe are the ones we are passed
-        * in - copy them over
+        * We need to check the credential state here, cause win2k3 and earlier
+        * returns NT_STATUS_NOT_IMPLEMENTED
         */
-       if (result->dc == NULL) {
-               result->dc = netlogon_creds_copy(result, *pdc);
-               if (result->dc == NULL) {
-                       TALLOC_FREE(result);
-                       return NT_STATUS_NO_MEMORY;
-               }
+       if (!netlogon_creds_client_check(&save_creds, &return_auth.cred)) {
+               /*
+                * Server replied with bad credential. Fail.
+                */
+               DEBUG(0,("cli_rpc_pipe_open_schannel_with_key: server %s "
+                        "replied with bad credential\n",
+                        rpccli->desthost));
+               TALLOC_FREE(rpccli);
+               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+       }
+       *rpccli->dc = save_creds;
+
+       if (!NT_STATUS_IS_OK(result)) {
+               DEBUG(0, ("dcerpc_netr_LogonGetCapabilities failed with %s\n",
+                         nt_errstr(result)));
+               TALLOC_FREE(rpccli);
+               return result;
+       }
+
+       if (!(save_creds.negotiate_flags & NETLOGON_NEG_SUPPORTS_AES)) {
+               /* This means AES isn't supported. */
+               DEBUG(5, ("AES is not negotiated, but netr_LogonGetCapabilities "
+                         "was OK - downgrade detected\n"));
+               TALLOC_FREE(rpccli);
+               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+       }
+
+       if (save_creds.negotiate_flags != capabilities.server_capabilities) {
+               DEBUG(0, ("The client capabilities don't match the server "
+                         "capabilities: local[0x%08X] remote[0x%08X]\n",
+                         save_creds.negotiate_flags,
+                         capabilities.server_capabilities));
+               TALLOC_FREE(rpccli);
+               return NT_STATUS_INVALID_NETWORK_RESPONSE;
        }
 
+done:
        DEBUG(10,("cli_rpc_pipe_open_schannel_with_key: opened pipe %s to machine %s "
                  "for domain %s and bound using schannel.\n",
-                 get_pipe_name_from_syntax(talloc_tos(), interface),
-                 result->desthost, domain));
+                 table->name,
+                 rpccli->desthost, domain));
 
-       *presult = result;
+       *_rpccli = rpccli;
        return NT_STATUS_OK;
 }
 
@@ -3056,7 +2970,7 @@ NTSTATUS cli_rpc_pipe_open_spnego(struct cli_state *cli,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       status = cli_rpc_pipe_open(cli, transport, &table->syntax_id, &result);
+       status = cli_rpc_pipe_open(cli, transport, table, &result);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -3065,7 +2979,7 @@ NTSTATUS cli_rpc_pipe_open_spnego(struct cli_state *cli,
                                          DCERPC_AUTH_TYPE_SPNEGO, auth_level,
                                          server, target_service,
                                          domain, username, password, 
-                                         use_kerberos,
+                                         use_kerberos, NULL,
                                          &auth);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("rpccli_generic_bind_data returned %s\n",
@@ -3099,7 +3013,6 @@ NTSTATUS cli_get_session_key(TALLOC_CTX *mem_ctx,
 {
        NTSTATUS status;
        struct pipe_auth_data *a;
-       struct schannel_state *schannel_auth;
        struct gensec_security *gensec_security;
        DATA_BLOB sk = data_blob_null;
        bool make_dup = false;
@@ -3115,12 +3028,6 @@ NTSTATUS cli_get_session_key(TALLOC_CTX *mem_ctx,
        }
 
        switch (cli->auth->auth_type) {
-       case DCERPC_AUTH_TYPE_SCHANNEL:
-               schannel_auth = talloc_get_type_abort(a->auth_ctx,
-                                                     struct schannel_state);
-               sk = data_blob_const(schannel_auth->creds->session_key, 16);
-               make_dup = true;
-               break;
        case DCERPC_AUTH_TYPE_SPNEGO:
        case DCERPC_AUTH_TYPE_NTLMSSP:
        case DCERPC_AUTH_TYPE_KRB5:
@@ -3134,8 +3041,8 @@ NTSTATUS cli_get_session_key(TALLOC_CTX *mem_ctx,
                break;
        case DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM:
        case DCERPC_AUTH_TYPE_NONE:
-               sk = data_blob_const(a->user_session_key.data,
-                                    a->user_session_key.length);
+               sk = data_blob_const(a->transport_session_key.data,
+                                    a->transport_session_key.length);
                make_dup = true;
                break;
        default: