r26654: libcli/smb_composite: Rather than specifying each of the gazillion options...
[jelmer/samba4-debian.git] / source / librpc / rpc / dcerpc_connect.c
index 3e49e6972b1e8c230a3fca137432ae0cf798726b..361be4adcd01d9ca93e83638253365f64720ee92 100644 (file)
@@ -31,6 +31,8 @@
 #include "libcli/smb2/smb2_calls.h"
 #include "librpc/rpc/dcerpc.h"
 #include "auth/credentials/credentials.h"
+#include "param/param.h"
+#include "libcli/resolve/resolve.h"
 
 
 struct pipe_np_smb_state {
@@ -87,8 +89,7 @@ static void continue_smb_connect(struct composite_context *ctx)
   Initiate async open of a rpc connection to a rpc pipe on SMB using
   the binding structure to determine the endpoint and options
 */
-static struct composite_context *dcerpc_pipe_connect_ncacn_np_smb_send(TALLOC_CTX *mem_ctx, 
-                                                               struct dcerpc_pipe_connect *io)
+static struct composite_context *dcerpc_pipe_connect_ncacn_np_smb_send(TALLOC_CTX *mem_ctx, struct dcerpc_pipe_connect *io, struct loadparm_context *lp_ctx)
 {
        struct composite_context *c;
        struct pipe_np_smb_state *s;
@@ -109,11 +110,16 @@ static struct composite_context *dcerpc_pipe_connect_ncacn_np_smb_send(TALLOC_CT
        /* prepare smb connection parameters: we're connecting to IPC$ share on
           remote rpc server */
        conn->in.dest_host              = s->io.binding->host;
-       conn->in.port                   = 0;
-       conn->in.called_name            = s->io.binding->target_hostname;
+       conn->in.dest_ports                  = lp_smb_ports(lp_ctx);
+       if (s->io.binding->target_hostname == NULL)
+               conn->in.called_name = "*SMBSERVER"; /* FIXME: This is invalid */
+       else
+               conn->in.called_name            = s->io.binding->target_hostname;
        conn->in.service                = "IPC$";
        conn->in.service_type           = NULL;
-       conn->in.workgroup              = lp_workgroup();
+       conn->in.workgroup              = lp_workgroup(lp_ctx);
+
+       lp_smbcli_options(lp_ctx, &conn->in.options);
 
        /*
         * provide proper credentials - user supplied, but allow a
@@ -123,13 +129,15 @@ static struct composite_context *dcerpc_pipe_connect_ncacn_np_smb_send(TALLOC_CT
         */
        s->conn.in.credentials = s->io.creds;
        if (s->io.binding->flags & DCERPC_SCHANNEL) {
-               conn->in.fallback_to_anonymous  = True;
+               conn->in.fallback_to_anonymous  = true;
        } else {
-               conn->in.fallback_to_anonymous  = False;
+               conn->in.fallback_to_anonymous  = false;
        }
 
        /* send smb connect request */
-       conn_req = smb_composite_connect_send(conn, s->io.pipe->conn, s->io.pipe->conn->event_ctx);
+       conn_req = smb_composite_connect_send(conn, s->io.pipe->conn, 
+                                             lp_resolve_context(lp_ctx), 
+                                             s->io.pipe->conn->event_ctx);
        if (composite_nomem(conn_req, c)) return c;
 
        composite_continue(c, conn_req, continue_smb_connect, c);
@@ -201,8 +209,10 @@ static void continue_smb2_connect(struct composite_context *ctx)
    Initiate async open of a rpc connection request on SMB2 using
    the binding structure to determine the endpoint and options
 */
-static struct composite_context *dcerpc_pipe_connect_ncacn_np_smb2_send(TALLOC_CTX *mem_ctx,
-                                                                       struct dcerpc_pipe_connect *io)
+static struct composite_context *dcerpc_pipe_connect_ncacn_np_smb2_send(
+                                       TALLOC_CTX *mem_ctx,
+                                       struct dcerpc_pipe_connect *io,
+                                       struct loadparm_context *lp_ctx)
 {
        struct composite_context *c;
        struct pipe_np_smb2_state *s;
@@ -226,12 +236,13 @@ static struct composite_context *dcerpc_pipe_connect_ncacn_np_smb2_send(TALLOC_C
                s->io.creds = cli_credentials_init(mem_ctx);
                if (composite_nomem(s->io.creds, c)) return c;
 
-               cli_credentials_set_anonymous(s->io.creds);
-               cli_credentials_guess(s->io.creds);
+               cli_credentials_guess(s->io.creds, lp_ctx);
        }
 
        /* send smb2 connect request */
-       conn_req = smb2_connect_send(mem_ctx, s->io.binding->host, "IPC$", s->io.creds,
+       conn_req = smb2_connect_send(mem_ctx, s->io.binding->host, "IPC$", 
+                                    s->io.resolve_ctx,
+                                    s->io.creds,
                                     c->event_ctx);
        composite_continue(c, conn_req, continue_smb2_connect, c);
        return c;
@@ -302,7 +313,7 @@ static struct composite_context* dcerpc_pipe_connect_ncacn_ip_tcp_send(TALLOC_CT
 
        /* send pipe open request on tcp/ip */
        pipe_req = dcerpc_pipe_open_tcp_send(s->io.pipe->conn, s->host, s->target_hostname, 
-                                            s->port);
+                                            s->port, io->resolve_ctx);
        composite_continue(c, pipe_req, continue_pipe_open_ncacn_ip_tcp, c);
        return c;
 }
@@ -420,7 +431,7 @@ static void continue_pipe_open_ncalrpc(struct composite_context *ctx)
    the binding structure to determine the endpoint and options
 */
 static struct composite_context* dcerpc_pipe_connect_ncalrpc_send(TALLOC_CTX *mem_ctx,
-                                                                 struct dcerpc_pipe_connect *io)
+                                                                 struct dcerpc_pipe_connect *io, struct loadparm_context *lp_ctx)
 {
        struct composite_context *c;
        struct pipe_ncalrpc_state *s;
@@ -438,7 +449,8 @@ static struct composite_context* dcerpc_pipe_connect_ncalrpc_send(TALLOC_CTX *me
        s->io  = *io;
 
        /* send pipe open request */
-       pipe_req = dcerpc_pipe_open_pipe_send(s->io.pipe->conn, s->io.binding->endpoint);
+       pipe_req = dcerpc_pipe_open_pipe_send(s->io.pipe->conn, lp_ncalrpc_dir(lp_ctx), 
+                                             s->io.binding->endpoint);
        composite_continue(c, pipe_req, continue_pipe_open_ncalrpc, c);
        return c;
 }
@@ -459,8 +471,9 @@ static NTSTATUS dcerpc_pipe_connect_ncalrpc_recv(struct composite_context *c)
 struct pipe_connect_state {
        struct dcerpc_pipe *pipe;
        struct dcerpc_binding *binding;
-       const struct dcerpc_interface_table *table;
+       const struct ndr_interface_table *table;
        struct cli_credentials *credentials;
+       struct loadparm_context *lp_ctx;
 };
 
 
@@ -513,19 +526,20 @@ static void continue_connect(struct composite_context *c, struct pipe_connect_st
        pc.binding      = s->binding;
        pc.interface    = s->table;
        pc.creds        = s->credentials;
+       pc.resolve_ctx  = lp_resolve_context(s->lp_ctx);
 
        /* connect dcerpc pipe depending on required transport */
        switch (s->binding->transport) {
        case NCACN_NP:
                if (pc.binding->flags & DCERPC_SMB2) {
                        /* new varient of SMB a.k.a. SMB2 */
-                       ncacn_np_smb2_req = dcerpc_pipe_connect_ncacn_np_smb2_send(c, &pc);
+                       ncacn_np_smb2_req = dcerpc_pipe_connect_ncacn_np_smb2_send(c, &pc, s->lp_ctx);
                        composite_continue(c, ncacn_np_smb2_req, continue_pipe_connect_ncacn_np_smb2, c);
                        return;
 
                } else {
                        /* good old ordinary SMB */
-                       ncacn_np_smb_req = dcerpc_pipe_connect_ncacn_np_smb_send(c, &pc);
+                       ncacn_np_smb_req = dcerpc_pipe_connect_ncacn_np_smb_send(c, &pc, s->lp_ctx);
                        composite_continue(c, ncacn_np_smb_req, continue_pipe_connect_ncacn_np_smb, c);
                        return;
                }
@@ -542,7 +556,7 @@ static void continue_connect(struct composite_context *c, struct pipe_connect_st
                return;
 
        case NCALRPC:
-               ncalrpc_req = dcerpc_pipe_connect_ncalrpc_send(c, &pc);
+               ncalrpc_req = dcerpc_pipe_connect_ncalrpc_send(c, &pc, s->lp_ctx);
                composite_continue(c, ncalrpc_req, continue_pipe_connect_ncalrpc, c);
                return;
 
@@ -655,7 +669,7 @@ static void continue_pipe_connect(struct composite_context *c, struct pipe_conne
        }
 
        auth_bind_req = dcerpc_pipe_auth_send(s->pipe, s->binding, s->table,
-                                             s->credentials);
+                                             s->credentials, s->lp_ctx);
        composite_continue(c, auth_bind_req, continue_pipe_auth, c);
 }
 
@@ -693,9 +707,10 @@ static void dcerpc_connect_timeout_handler(struct event_context *ev, struct time
 */
 struct composite_context* dcerpc_pipe_connect_b_send(TALLOC_CTX *parent_ctx,
                                                     struct dcerpc_binding *binding,
-                                                    const struct dcerpc_interface_table *table,
+                                                    const struct ndr_interface_table *table,
                                                     struct cli_credentials *credentials,
-                                                    struct event_context *ev)
+                                                    struct event_context *ev,
+                                                    struct loadparm_context *lp_ctx)
 {
        struct composite_context *c;
        struct pipe_connect_state *s;
@@ -720,26 +735,37 @@ struct composite_context* dcerpc_pipe_connect_b_send(TALLOC_CTX *parent_ctx,
        c->private_data = s;
 
        /* initialise dcerpc pipe structure */
-       s->pipe = dcerpc_pipe_init(c, ev);
+       s->pipe = dcerpc_pipe_init(c, ev, lp_iconv_convenience(lp_ctx));
        if (composite_nomem(s->pipe, c)) return c;
 
        /* store parameters in state structure */
        s->binding      = binding;
        s->table        = table;
        s->credentials  = credentials;
+       s->lp_ctx       = lp_ctx;
 
        event_add_timed(c->event_ctx, c,
                        timeval_current_ofs(DCERPC_REQUEST_TIMEOUT, 0),
                        dcerpc_connect_timeout_handler, c);
        
        switch (s->binding->transport) {
+       case NCA_UNKNOWN: {
+               struct composite_context *binding_req;
+               binding_req = dcerpc_epm_map_binding_send(c, s->binding, s->table,
+                                                         s->pipe->conn->event_ctx,
+                                                         s->lp_ctx);
+               composite_continue(c, binding_req, continue_map_binding, c);
+               return c;
+               }
+
        case NCACN_NP:
        case NCACN_IP_TCP:
        case NCALRPC:
                if (!s->binding->endpoint) {
                        struct composite_context *binding_req;
                        binding_req = dcerpc_epm_map_binding_send(c, s->binding, s->table,
-                                                                 s->pipe->conn->event_ctx);
+                                                                 s->pipe->conn->event_ctx,
+                                                                 s->lp_ctx);
                        composite_continue(c, binding_req, continue_map_binding, c);
                        return c;
                }
@@ -781,14 +807,15 @@ NTSTATUS dcerpc_pipe_connect_b_recv(struct composite_context *c, TALLOC_CTX *mem
 NTSTATUS dcerpc_pipe_connect_b(TALLOC_CTX *parent_ctx,
                               struct dcerpc_pipe **pp,
                               struct dcerpc_binding *binding,
-                              const struct dcerpc_interface_table *table,
+                              const struct ndr_interface_table *table,
                               struct cli_credentials *credentials,
-                              struct event_context *ev)
+                              struct event_context *ev,
+                              struct loadparm_context *lp_ctx)
 {
        struct composite_context *c;
        
        c = dcerpc_pipe_connect_b_send(parent_ctx, binding, table,
-                                      credentials, ev);
+                                      credentials, ev, lp_ctx);
        return dcerpc_pipe_connect_b_recv(c, parent_ctx, pp);
 }
 
@@ -808,9 +835,9 @@ static void continue_pipe_connect_b(struct composite_context *ctx);
 */
 struct composite_context* dcerpc_pipe_connect_send(TALLOC_CTX *parent_ctx,
                                                   const char *binding,
-                                                  const struct dcerpc_interface_table *table,
+                                                  const struct ndr_interface_table *table,
                                                   struct cli_credentials *credentials,
-                                                  struct event_context *ev)
+                                                  struct event_context *ev, struct loadparm_context *lp_ctx)
 {
        struct composite_context *c;
        struct pipe_conn_state *s;
@@ -851,7 +878,7 @@ struct composite_context* dcerpc_pipe_connect_send(TALLOC_CTX *parent_ctx,
           is established
         */
        pipe_conn_req = dcerpc_pipe_connect_b_send(c, b, table,
-                                                  credentials, ev);
+                                                  credentials, ev, lp_ctx);
        composite_continue(c, pipe_conn_req, continue_pipe_connect_b, c);
        return c;
 }
@@ -904,14 +931,14 @@ NTSTATUS dcerpc_pipe_connect_recv(struct composite_context *c,
 NTSTATUS dcerpc_pipe_connect(TALLOC_CTX *parent_ctx, 
                             struct dcerpc_pipe **pp, 
                             const char *binding,
-                            const struct dcerpc_interface_table *table,
+                            const struct ndr_interface_table *table,
                             struct cli_credentials *credentials,
-                            struct event_context *ev)
+                            struct event_context *ev,
+                            struct loadparm_context *lp_ctx)
 {
        struct composite_context *c;
        c = dcerpc_pipe_connect_send(parent_ctx, binding, 
-                                    table,
-                                    credentials, ev);
+                                    table, credentials, ev, lp_ctx);
        return dcerpc_pipe_connect_recv(c, parent_ctx, pp);
 }