s->pipe->binding = talloc_move(s->pipe, &s->binding);
[metze/samba/wip.git] / source4 / librpc / rpc / dcerpc_secondary.c
index fbad46c55ad7c560bcfd969f00a598b117953ebf..084413ac84c5fb281e2480dcb359de10ef345001 100644 (file)
 #include "lib/events/events.h"
 #include "librpc/rpc/dcerpc.h"
 #include "librpc/rpc/dcerpc_proto.h"
+#include "libcli/smb/smbXcli_base.h"
 #include "auth/credentials/credentials.h"
 #include "param/param.h"
 #include "libcli/resolve/resolve.h"
-#include "lib/socket/socket.h"
+#include "lib/util/util_net.h"
 
 struct sec_conn_state {
        struct dcerpc_pipe *pipe;
        struct dcerpc_pipe *pipe2;
-       const struct dcerpc_binding *binding;
-       struct socket_address *peer_addr;
-       const char *localaddress;
+       struct dcerpc_binding *binding;
+       struct {
+               struct smbXcli_conn *conn;
+               struct smbXcli_session *session;
+               struct smbXcli_tcon *tcon;
+       } smb;
 };
 
 
@@ -60,9 +64,11 @@ _PUBLIC_ struct composite_context* dcerpc_secondary_connection_send(struct dcerp
        struct sec_conn_state *s;
        struct composite_context *pipe_smb_req;
        struct composite_context *pipe_tcp_req;
+       const char *localaddress = NULL;
        struct composite_context *pipe_ncalrpc_req;
        const char *ncalrpc_dir = NULL;
        struct composite_context *pipe_unix_req;
+       const char *host;
        const char *target_hostname;
        const char *endpoint;
 
@@ -75,7 +81,8 @@ _PUBLIC_ struct composite_context* dcerpc_secondary_connection_send(struct dcerp
        c->private_data = s;
 
        s->pipe     = p;
-       s->binding  = b;
+       s->binding  = dcerpc_binding_dup(s, b);
+       if (composite_nomem(s->binding, c)) return c;
 
        /* initialise second dcerpc pipe based on primary pipe's event context */
        s->pipe2 = dcerpc_pipe_init(c, s->pipe->conn->event_ctx);
@@ -84,7 +91,22 @@ _PUBLIC_ struct composite_context* dcerpc_secondary_connection_send(struct dcerp
        if (DEBUGLEVEL >= 10)
                s->pipe2->conn->packet_log_dir = s->pipe->conn->packet_log_dir;
 
+       host = dcerpc_binding_get_string_option(s->binding, "host");
+       if (host == NULL) {
+               /*
+                * We may fallback to the host of the given connection
+                */
+               host = dcerpc_binding_get_string_option(s->pipe->binding,
+                                                       "host");
+       }
        target_hostname = dcerpc_binding_get_string_option(s->binding, "target_hostname");
+       if (target_hostname == NULL) {
+               /*
+                * We may fallback to the target_hostname of the given connection
+                */
+               target_hostname = dcerpc_binding_get_string_option(s->pipe->binding,
+                                                                  "target_hostname");
+       }
        endpoint = dcerpc_binding_get_string_option(s->binding, "endpoint");
        if (endpoint == NULL) {
                /*
@@ -100,25 +122,59 @@ _PUBLIC_ struct composite_context* dcerpc_secondary_connection_send(struct dcerp
        /* open second dcerpc pipe using the same transport as for primary pipe */
        switch (s->pipe->conn->transport.transport) {
        case NCACN_NP:
-               pipe_smb_req = dcerpc_secondary_smb_send(s->pipe->conn,
-                                                        s->pipe2->conn,
+               c->status = dcerpc_binding_get_smbXcli_pointers(s->pipe->binding,
+                                                               &s->smb.conn,
+                                                               &s->smb.session,
+                                                               &s->smb.tcon);
+               if (!NT_STATUS_IS_OK(c->status)) {
+                       composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
+                       return c;
+               }
+
+               pipe_smb_req = dcerpc_pipe_open_smb_send(s->pipe2->conn,
+                                                        s->smb.conn,
+                                                        s->smb.session,
+                                                        s->smb.tcon,
+                                                        DCERPC_REQUEST_TIMEOUT * 1000,
                                                         endpoint);
                composite_continue(c, pipe_smb_req, continue_open_smb, c);
                return c;
 
        case NCACN_IP_TCP:
-               s->peer_addr = dcerpc_socket_peer_addr(s->pipe->conn, s);
-               if (!s->peer_addr) {
-                       composite_error(c, NT_STATUS_INVALID_PARAMETER);
+               if (host == NULL) {
+                       composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
                        return c;
                }
 
-               s->localaddress = dcerpc_binding_get_string_option(s->binding,
+               if (!is_ipaddress(host)) {
+                       /*
+                        * We may fallback to the host of the given connection
+                        */
+                       host = dcerpc_binding_get_string_option(s->pipe->binding,
+                                                               "host");
+                       if (host == NULL) {
+                               composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
+                               return c;
+                       }
+                       if (!is_ipaddress(host)) {
+                               composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
+                               return c;
+                       }
+               }
+
+               localaddress = dcerpc_binding_get_string_option(s->binding,
                                                                "localaddress");
+               if (localaddress == NULL) {
+                       /*
+                        * We may fallback to the localaddress of the given connection
+                        */
+                       localaddress = dcerpc_binding_get_string_option(s->pipe->binding,
+                                                                       "localaddress");
+               }
 
                pipe_tcp_req = dcerpc_pipe_open_tcp_send(s->pipe2->conn,
-                                                        s->localaddress,
-                                                        s->peer_addr->addr,
+                                                        localaddress,
+                                                        host,
                                                         target_hostname,
                                                         atoi(endpoint),
                                                         resolve_context_init(s));
@@ -165,8 +221,16 @@ static void continue_open_smb(struct composite_context *ctx)
 {
        struct composite_context *c = talloc_get_type(ctx->async.private_data,
                                                      struct composite_context);
-       
-       c->status = dcerpc_secondary_smb_recv(ctx);
+       struct sec_conn_state *s = talloc_get_type_abort(c->private_data,
+                                                        struct sec_conn_state);
+
+       c->status = dcerpc_pipe_open_smb_recv(ctx);
+       if (!composite_is_ok(c)) return;
+
+       c->status = dcerpc_binding_set_smbXcli_pointers(s->binding,
+                                                       s->smb.conn,
+                                                       s->smb.session,
+                                                       s->smb.tcon);
        if (!composite_is_ok(c)) return;
 
        continue_pipe_open(c);
@@ -180,8 +244,22 @@ static void continue_open_tcp(struct composite_context *ctx)
 {
        struct composite_context *c = talloc_get_type(ctx->async.private_data,
                                                      struct composite_context);
-       
-       c->status = dcerpc_pipe_open_tcp_recv(ctx);
+       struct sec_conn_state *s = talloc_get_type_abort(c->private_data,
+                                                        struct sec_conn_state);
+       char *localaddr = NULL;
+       char *remoteaddr = NULL;
+
+       c->status = dcerpc_pipe_open_tcp_recv(ctx, s, &localaddr, &remoteaddr);
+       if (!composite_is_ok(c)) return;
+
+       c->status = dcerpc_binding_set_string_option(s->binding,
+                                                    "localaddress",
+                                                    localaddr);
+       if (!composite_is_ok(c)) return;
+
+       c->status = dcerpc_binding_set_string_option(s->binding,
+                                                    "host",
+                                                    remoteaddr);
        if (!composite_is_ok(c)) return;
 
        continue_pipe_open(c);
@@ -227,10 +305,7 @@ static void continue_pipe_open(struct composite_context *c)
        s = talloc_get_type(c->private_data, struct sec_conn_state);
 
        s->pipe2->conn->flags = s->pipe->conn->flags;
-       s->pipe2->binding     = dcerpc_binding_dup(s->pipe2, s->binding);
-       if (composite_nomem(s->pipe2->binding, c)) {
-               return;
-       }
+       s->pipe2->binding = talloc_move(s->pipe2, &s->binding);
 
        composite_done(c);
 }
@@ -256,23 +331,6 @@ _PUBLIC_ NTSTATUS dcerpc_secondary_connection_recv(struct composite_context *c,
        return status;
 }
 
-/*
-  Create a secondary dcerpc connection from a primary connection
-  - sync version
-
-  If the primary is a SMB connection then the secondary connection
-  will be on the same SMB connection, but using a new fnum
-*/
-_PUBLIC_ NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p,
-                                    struct dcerpc_pipe **p2,
-                                    const struct dcerpc_binding *b)
-{
-       struct composite_context *c;
-       
-       c = dcerpc_secondary_connection_send(p, b);
-       return dcerpc_secondary_connection_recv(c, p2);
-}
-
 /*
   Create a secondary DCERPC connection, then bind (and possibly
   authenticate) using the supplied credentials.
@@ -379,3 +437,18 @@ _PUBLIC_ NTSTATUS dcerpc_secondary_auth_connection_recv(struct composite_context
        talloc_free(c);
        return status;
 }
+
+_PUBLIC_ NTSTATUS dcerpc_secondary_auth_connection(struct dcerpc_pipe *p,
+                                       const struct dcerpc_binding *binding,
+                                       const struct ndr_interface_table *table,
+                                       struct cli_credentials *credentials,
+                                       struct loadparm_context *lp_ctx,
+                                       TALLOC_CTX *mem_ctx,
+                                       struct dcerpc_pipe **p2)
+{
+       struct composite_context *c;
+
+       c = dcerpc_secondary_auth_connection_send(p, binding, table,
+                                                 credentials, lp_ctx);
+       return dcerpc_secondary_auth_connection_recv(c, mem_ctx, p2);
+}