s3-rpc_client: use NL_AUTH_MESSAGE in create_schannel_auth_rpc_bind_req().
[ira/wip.git] / source3 / rpc_client / cli_pipe.c
index 6b83c170f5791d06097c2d0671dbbc8651fa30ee..82f6b105b7e3cd8910c42118345ae4686eaa299e 100644 (file)
@@ -18,7 +18,9 @@
  */
 
 #include "includes.h"
+#include "../libcli/auth/libcli_auth.h"
 #include "librpc/gen_ndr/cli_epmapper.h"
+#include "../librpc/gen_ndr/ndr_schannel.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_RPC_CLI
@@ -55,7 +57,7 @@ static const struct pipe_id_info {
        /* the names appear not to matter: the syntaxes _do_ matter */
 
        const char *client_pipe;
-       const RPC_IFACE *abstr_syntax; /* this one is the abstract syntax id */
+       const struct ndr_syntax_id *abstr_syntax; /* this one is the abstract syntax id */
 } pipe_names [] =
 {
        { PIPE_LSARPC,          &ndr_table_lsarpc.syntax_id },
@@ -680,7 +682,7 @@ static NTSTATUS cli_pipe_verify_schannel(struct rpc_pipe_client *cli, RPC_HDR *p
                return NT_STATUS_OK;
        }
 
-       if (auth_len != RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN) {
+       if (auth_len < RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN) {
                DEBUG(0,("cli_pipe_verify_schannel: auth_len %u.\n", (unsigned int)auth_len ));
                return NT_STATUS_INVALID_PARAMETER;
        }
@@ -1610,7 +1612,9 @@ static NTSTATUS create_schannel_auth_rpc_bind_req( struct rpc_pipe_client *cli,
                                                RPC_HDR_AUTH *pauth_out,
                                                prs_struct *auth_data)
 {
-       RPC_AUTH_SCHANNEL_NEG schannel_neg;
+       struct NL_AUTH_MESSAGE r;
+       enum ndr_err_code ndr_err;
+       DATA_BLOB blob;
 
        /* We may change the pad length before marshalling. */
        init_rpc_hdr_auth(pauth_out, RPC_SCHANNEL_AUTH_TYPE, (int)auth_level, 0, 1);
@@ -1624,16 +1628,30 @@ static NTSTATUS create_schannel_auth_rpc_bind_req( struct rpc_pipe_client *cli,
                }
        }
 
-       init_rpc_auth_schannel_neg(&schannel_neg, cli->auth->domain,
-                                  global_myname());
-
        /*
         * Now marshall the data into the auth parse_struct.
         */
 
-       if(!smb_io_rpc_auth_schannel_neg("schannel_neg",
-                                      &schannel_neg, auth_data, 0)) {
-               DEBUG(0,("Failed to marshall RPC_AUTH_SCHANNEL_NEG.\n"));
+       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        = global_myname();
+
+       ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), NULL, &r,
+                      (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               DEBUG(0,("Failed to marshall NL_AUTH_MESSAGE.\n"));
+               prs_mem_free(auth_data);
+               return ndr_map_error2ntstatus(ndr_err);
+       }
+
+       if (DEBUGLEVEL >= 10) {
+               NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &r);
+       }
+
+       if (!prs_copy_data_in(auth_data, (const char *)blob.data, blob.length))
+       {
                prs_mem_free(auth_data);
                return NT_STATUS_NO_MEMORY;
        }
@@ -1648,8 +1666,8 @@ static NTSTATUS create_schannel_auth_rpc_bind_req( struct rpc_pipe_client *cli,
 static NTSTATUS create_bind_or_alt_ctx_internal(enum RPC_PKT_TYPE pkt_type,
                                                prs_struct *rpc_out, 
                                                uint32 rpc_call_id,
-                                               const RPC_IFACE *abstract,
-                                               const RPC_IFACE *transfer,
+                                               const struct ndr_syntax_id *abstract,
+                                               const struct ndr_syntax_id *transfer,
                                                RPC_HDR_AUTH *phdr_auth,
                                                prs_struct *pauth_info)
 {
@@ -1730,8 +1748,8 @@ static NTSTATUS create_bind_or_alt_ctx_internal(enum RPC_PKT_TYPE pkt_type,
 static NTSTATUS create_rpc_bind_req(struct rpc_pipe_client *cli,
                                prs_struct *rpc_out, 
                                uint32 rpc_call_id,
-                               const RPC_IFACE *abstract,
-                               const RPC_IFACE *transfer,
+                               const struct ndr_syntax_id *abstract,
+                               const struct ndr_syntax_id *transfer,
                                enum pipe_auth_type auth_type,
                                enum pipe_auth_level auth_level)
 {
@@ -2283,34 +2301,6 @@ NTSTATUS rpc_api_pipe_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
        return NT_STATUS_OK;
 }
 
-NTSTATUS rpc_api_pipe_req(TALLOC_CTX *mem_ctx, struct rpc_pipe_client *cli,
-                       uint8 op_num,
-                       prs_struct *in_data,
-                       prs_struct *out_data)
-{
-       TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
-       struct tevent_req *req;
-       NTSTATUS status = NT_STATUS_NO_MEMORY;
-
-       ev = event_context_init(frame);
-       if (ev == NULL) {
-               goto fail;
-       }
-
-       req = rpc_api_pipe_req_send(frame, ev, cli, op_num, in_data);
-       if (req == NULL) {
-               goto fail;
-       }
-
-       tevent_req_poll(req, ev);
-
-       status = rpc_api_pipe_req_recv(req, mem_ctx, out_data);
- fail:
-       TALLOC_FREE(frame);
-       return status;
-}
-
 #if 0
 /****************************************************************************
  Set the handle state.
@@ -2362,7 +2352,8 @@ static bool rpc_pipe_set_hnd_state(struct rpc_pipe_client *cli,
  Check the rpc bind acknowledge response.
 ****************************************************************************/
 
-static bool check_bind_response(RPC_HDR_BA *hdr_ba, const RPC_IFACE *transfer)
+static bool check_bind_response(RPC_HDR_BA *hdr_ba,
+                               const struct ndr_syntax_id *transfer)
 {
        if ( hdr_ba->addr.len == 0) {
                DEBUG(4,("Ignoring length check -- ASU bug (server didn't fill in the pipe name correctly)"));
@@ -2452,8 +2443,8 @@ static NTSTATUS create_rpc_bind_auth3(struct rpc_pipe_client *cli,
  ********************************************************************/
 
 static NTSTATUS create_rpc_alter_context(uint32 rpc_call_id,
-                                       const RPC_IFACE *abstract,
-                                       const RPC_IFACE *transfer,
+                                       const struct ndr_syntax_id *abstract,
+                                       const struct ndr_syntax_id *transfer,
                                        enum pipe_auth_level auth_level,
                                        const DATA_BLOB *pauth_blob, /* spnego auth blob already created. */
                                        prs_struct *rpc_out)
@@ -2905,19 +2896,24 @@ NTSTATUS rpc_pipe_bind(struct rpc_pipe_client *cli,
        TALLOC_CTX *frame = talloc_stackframe();
        struct event_context *ev;
        struct tevent_req *req;
-       NTSTATUS status = NT_STATUS_NO_MEMORY;
+       NTSTATUS status = NT_STATUS_OK;
 
        ev = event_context_init(frame);
        if (ev == NULL) {
+               status = NT_STATUS_NO_MEMORY;
                goto fail;
        }
 
        req = rpc_pipe_bind_send(frame, ev, cli, auth);
        if (req == NULL) {
+               status = NT_STATUS_NO_MEMORY;
                goto fail;
        }
 
-       tevent_req_poll(req, ev);
+       if (!tevent_req_poll(req, ev)) {
+               status = map_nt_error_from_unix(errno);
+               goto fail;
+       }
 
        status = rpc_pipe_bind_recv(req);
  fail:
@@ -3180,6 +3176,8 @@ static NTSTATUS rpc_pipe_open_tcp_port(TALLOC_CTX *mem_ctx, const char *host,
        result->abstract_syntax = *abstract_syntax;
        result->transfer_syntax = ndr_transfer_syntax;
        result->dispatch = cli_do_rpc_ndr;
+       result->dispatch_send = cli_do_rpc_ndr_send;
+       result->dispatch_recv = cli_do_rpc_ndr_recv;
 
        result->desthost = talloc_strdup(result, host);
        result->srv_name_slash = talloc_asprintf_strupper_m(
@@ -3192,7 +3190,7 @@ 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)) {
+       if (!resolve_name(host, &addr, 0, false)) {
                status = NT_STATUS_NOT_FOUND;
                goto fail;
        }
@@ -3392,6 +3390,8 @@ NTSTATUS rpc_pipe_open_ncalrpc(TALLOC_CTX *mem_ctx, const char *socket_path,
        result->abstract_syntax = *abstract_syntax;
        result->transfer_syntax = ndr_transfer_syntax;
        result->dispatch = cli_do_rpc_ndr;
+       result->dispatch_send = cli_do_rpc_ndr_send;
+       result->dispatch_recv = cli_do_rpc_ndr_recv;
 
        result->desthost = get_myname(result);
        result->srv_name_slash = talloc_asprintf_strupper_m(
@@ -3414,7 +3414,7 @@ NTSTATUS rpc_pipe_open_ncalrpc(TALLOC_CTX *mem_ctx, const char *socket_path,
        addr.sun_family = AF_UNIX;
        strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
 
-       if (sys_connect(fd, (struct sockaddr *)&addr) == -1) {
+       if (sys_connect(fd, (struct sockaddr *)(void *)&addr) == -1) {
                DEBUG(0, ("connect(%s) failed: %s\n", socket_path,
                          strerror(errno)));
                close(fd);
@@ -3480,6 +3480,8 @@ static NTSTATUS rpc_pipe_open_np(struct cli_state *cli,
        result->abstract_syntax = *abstract_syntax;
        result->transfer_syntax = ndr_transfer_syntax;
        result->dispatch = cli_do_rpc_ndr;
+       result->dispatch_send = cli_do_rpc_ndr_send;
+       result->dispatch_recv = cli_do_rpc_ndr_recv;
        result->desthost = talloc_strdup(result, cli->desthost);
        result->srv_name_slash = talloc_asprintf_strupper_m(
                result, "\\\\%s", result->desthost);
@@ -3522,6 +3524,8 @@ NTSTATUS rpc_pipe_open_local(TALLOC_CTX *mem_ctx,
        result->abstract_syntax = *syntax;
        result->transfer_syntax = ndr_transfer_syntax;
        result->dispatch = cli_do_rpc_ndr;
+       result->dispatch_send = cli_do_rpc_ndr_send;
+       result->dispatch_recv = cli_do_rpc_ndr_recv;
        result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
        result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
 
@@ -3834,13 +3838,15 @@ NTSTATUS get_schannel_session_key(struct cli_state *cli,
  External interface.
  Open a named pipe to an SMB server and bind using schannel (bind type 68)
  using session_key. sign and seal.
+
+ The *pdc will be stolen onto this new pipe
  ****************************************************************************/
 
 NTSTATUS cli_rpc_pipe_open_schannel_with_key(struct cli_state *cli,
                                             const struct ndr_syntax_id *interface,
                                             enum pipe_auth_level auth_level,
                                             const char *domain,
-                                            const struct dcinfo *pdc,
+                                            struct netlogon_creds_CredentialState **pdc,
                                             struct rpc_pipe_client **presult)
 {
        struct rpc_pipe_client *result;
@@ -3853,7 +3859,7 @@ NTSTATUS cli_rpc_pipe_open_schannel_with_key(struct cli_state *cli,
        }
 
        status = rpccli_schannel_bind_data(result, domain, auth_level,
-                                          pdc->sess_key, &auth);
+                                          (*pdc)->session_key, &auth);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("rpccli_schannel_bind_data returned %s\n",
                          nt_errstr(status)));
@@ -3872,11 +3878,11 @@ NTSTATUS cli_rpc_pipe_open_schannel_with_key(struct cli_state *cli,
 
        /*
         * The credentials on a new netlogon pipe are the ones we are passed
-        * in - copy them over.
+        * in - reference them in
         */
-       result->dc = (struct dcinfo *)talloc_memdup(result, pdc, sizeof(*pdc));
+       result->dc = talloc_move(result, pdc);
        if (result->dc == NULL) {
-               DEBUG(0, ("talloc failed\n"));
+               DEBUG(0, ("talloc reference failed\n"));
                TALLOC_FREE(result);
                return NT_STATUS_NO_MEMORY;
        }
@@ -3953,7 +3959,7 @@ NTSTATUS cli_rpc_pipe_open_ntlmssp_auth_schannel(struct cli_state *cli,
        }
 
        status = cli_rpc_pipe_open_schannel_with_key(
-               cli, interface, auth_level, domain, netlogon_pipe->dc,
+               cli, interface, auth_level, domain, &netlogon_pipe->dc,
                &result);
 
        /* Now we've bound using the session key we can close the netlog pipe. */
@@ -3991,7 +3997,7 @@ NTSTATUS cli_rpc_pipe_open_schannel(struct cli_state *cli,
        }
 
        status = cli_rpc_pipe_open_schannel_with_key(
-               cli, interface, auth_level, domain, netlogon_pipe->dc,
+               cli, interface, auth_level, domain, &netlogon_pipe->dc,
                &result);
 
        /* Now we've bound using the session key we can close the netlog pipe. */