r2710: continue with the new style of providing a parent context whenever
[jelmer/samba4-debian.git] / source / librpc / rpc / dcerpc_util.c
index bc10f4e92d494b71db6ca64b7c8e990cae78ce71..bcadd521049b6c5636ecafce454d01a69e536557 100644 (file)
@@ -52,8 +52,8 @@ size_t ndr_size_epm_towers(struct epm_towers *towers)
   work out what TCP port to use for a given interface on a given host
 */
 NTSTATUS dcerpc_epm_map_tcp_port(const char *server, 
-                                const char *uuid, unsigned version,
-                                uint32 *port)
+                                const char *uuid, uint_t version,
+                                uint32_t *port)
 {
        struct dcerpc_pipe *p;
        NTSTATUS status;
@@ -69,14 +69,14 @@ NTSTATUS dcerpc_epm_map_tcp_port(const char *server,
                return NT_STATUS_OK;
        }
 
-       status = dcerpc_pipe_open_tcp(&p, server, EPMAPPER_PORT);
+       status = dcerpc_pipe_open_tcp(&p, server, EPMAPPER_PORT, AF_UNSPEC );
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
        /* we can use the pipes memory context here as we will have a short
           lived connection */
-       status = dcerpc_bind_byuuid(p, p->mem_ctx
+       status = dcerpc_bind_byuuid(p, p, 
                                    DCERPC_EPMAPPER_UUID,
                                    DCERPC_EPMAPPER_VERSION);
        if (!NT_STATUS_IS_OK(status)) {
@@ -88,7 +88,7 @@ NTSTATUS dcerpc_epm_map_tcp_port(const char *server,
        ZERO_STRUCT(guid);
 
        twr.towers.num_floors = 5;
-       twr.towers.floors = talloc(p->mem_ctx, sizeof(twr.towers.floors[0]) * 5);
+       twr.towers.floors = talloc(p, sizeof(twr.towers.floors[0]) * 5);
 
        /* what I'd like for christmas ... */
 
@@ -96,28 +96,28 @@ NTSTATUS dcerpc_epm_map_tcp_port(const char *server,
        twr.towers.floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
        GUID_from_string(uuid, &twr.towers.floors[0].lhs.info.uuid.uuid);
        twr.towers.floors[0].lhs.info.uuid.version = version;
-       twr.towers.floors[0].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 2);
+       twr.towers.floors[0].rhs.rhs_data = data_blob_talloc_zero(p, 2);
 
        /* encoded with NDR ... */
        twr.towers.floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
        GUID_from_string(NDR_GUID, &twr.towers.floors[1].lhs.info.uuid.uuid);
        twr.towers.floors[1].lhs.info.uuid.version = NDR_GUID_VERSION;
-       twr.towers.floors[1].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 2);
+       twr.towers.floors[1].rhs.rhs_data = data_blob_talloc_zero(p, 2);
 
        /* on an RPC connection ... */
-       twr.towers.floors[2].lhs.protocol = EPM_PROTOCOL_RPC_C;
+       twr.towers.floors[2].lhs.protocol = EPM_PROTOCOL_NCACN_RPC_C;
        twr.towers.floors[2].lhs.info.lhs_data = data_blob(NULL, 0);
-       twr.towers.floors[2].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 2);
+       twr.towers.floors[2].rhs.rhs_data = data_blob_talloc_zero(p, 2);
 
        /* on a TCP port ... */
-       twr.towers.floors[3].lhs.protocol = EPM_PROTOCOL_TCP;
+       twr.towers.floors[3].lhs.protocol = EPM_PROTOCOL_NCACN_TCP;
        twr.towers.floors[3].lhs.info.lhs_data = data_blob(NULL, 0);
-       twr.towers.floors[3].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 2);
+       twr.towers.floors[3].rhs.rhs_data = data_blob_talloc_zero(p, 2);
 
        /* on an IP link ... */
-       twr.towers.floors[4].lhs.protocol = EPM_PROTOCOL_IP;
+       twr.towers.floors[4].lhs.protocol = EPM_PROTOCOL_NCACN_IP;
        twr.towers.floors[4].lhs.info.lhs_data = data_blob(NULL, 0);
-       twr.towers.floors[4].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 4);
+       twr.towers.floors[4].rhs.rhs_data = data_blob_talloc_zero(p, 4);
 
        /* with some nice pretty paper around it of course */
        r.in.object = &guid;
@@ -126,12 +126,12 @@ NTSTATUS dcerpc_epm_map_tcp_port(const char *server,
        r.in.max_towers = 1;
        r.out.entry_handle = &handle;
 
-       status = dcerpc_epm_Map(p, p->mem_ctx, &r);
+       status = dcerpc_epm_Map(p, p, &r);
        if (!NT_STATUS_IS_OK(status)) {
                dcerpc_pipe_close(p);
                return status;
        }
-       if (r.out.status != 0 || r.out.num_towers != 1) {
+       if (r.out.result != 0 || r.out.num_towers != 1) {
                dcerpc_pipe_close(p);
                return NT_STATUS_PORT_UNREACHABLE;
        }
@@ -160,7 +160,7 @@ NTSTATUS dcerpc_epm_map_tcp_port(const char *server,
 /*
   find the pipe name for a local IDL interface
 */
-const char *idl_pipe_name(const char *uuid, uint32 if_version)
+const char *idl_pipe_name(const char *uuid, uint32_t if_version)
 {
        int i;
        for (i=0;dcerpc_pipes[i];i++) {
@@ -175,7 +175,7 @@ const char *idl_pipe_name(const char *uuid, uint32 if_version)
 /*
   find the number of calls defined by local IDL
 */
-int idl_num_calls(const char *uuid, uint32 if_version)
+int idl_num_calls(const char *uuid, uint32_t if_version)
 {
        int i;
        for (i=0;dcerpc_pipes[i];i++) {
@@ -223,8 +223,7 @@ const struct dcerpc_interface_table *idl_iface_by_uuid(const char *uuid)
 */
 NTSTATUS dcerpc_push_auth(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
                          struct dcerpc_packet *pkt,
-                         struct dcerpc_auth *auth_info,
-                         unsigned flags)
+                         struct dcerpc_auth *auth_info)
 {
        NTSTATUS status;
        struct ndr_push *ndr;
@@ -234,7 +233,7 @@ NTSTATUS dcerpc_push_auth(DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
                return NT_STATUS_NO_MEMORY;
        }
 
-       if (flags & DCERPC_PUSH_BIGENDIAN) {
+       if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
                ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
        }
 
@@ -272,11 +271,14 @@ static const struct {
 
 static const struct {
        const char *name;
-       uint32 flag;
+       uint32_t flag;
 } ncacn_options[] = {
        {"sign", DCERPC_SIGN},
        {"seal", DCERPC_SEAL},
+       {"connect", DCERPC_CONNECT},
        {"validate", DCERPC_DEBUG_VALIDATE_BOTH},
+       {"print", DCERPC_DEBUG_PRINT_BOTH},
+       {"padcheck", DCERPC_DEBUG_PAD_CHECK},
        {"bigendian", DCERPC_PUSH_BIGENDIAN}
 };
 
@@ -304,7 +306,7 @@ const char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_bindi
        /* this is a *really* inefficent way of dealing with strings,
           but this is rarely called and the strings are always short,
           so I don't care */
-       for (i=0;b->options[i];i++) {
+       for (i=0;b->options && b->options[i];i++) {
                s = talloc_asprintf(mem_ctx, "%s%s,", s, b->options[i]);
                if (!s) return NULL;
        }
@@ -413,6 +415,7 @@ NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_
                                for (k=i;b->options[k];k++) {
                                        b->options[k] = b->options[k+1];
                                }
+                               i--;
                                break;
                        }
                }
@@ -422,19 +425,19 @@ NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_
 }
 
 
-/* open a rpc connection to a rpc pipe on SMP using the binding
+/* open a rpc connection to a rpc pipe on SMB using the binding
    structure to determine the endpoint and options */
 static NTSTATUS dcerpc_pipe_connect_ncacn_np(struct dcerpc_pipe **p, 
                                             struct dcerpc_binding *binding,
                                             const char *pipe_uuid, 
-                                            uint32 pipe_version,
+                                            uint32_t pipe_version,
                                             const char *domain,
                                             const char *username,
                                             const char *password)
 {
        NTSTATUS status;
        BOOL retry;
-       struct cli_state *cli;
+       struct smbcli_state *cli;
        const char *pipe_name;
        
        if (!binding->options || !binding->options[0]) {
@@ -456,11 +459,18 @@ static NTSTATUS dcerpc_pipe_connect_ncacn_np(struct dcerpc_pipe **p,
                pipe_name += 6;
        }
            
-       status = cli_full_connection(&cli, lp_netbios_name(),
-                                    binding->host, NULL, 
-                                    "ipc$", "?????", 
-                                    username, username[0]?domain:"",
-                                    password, 0, &retry);
+       if (!username || !username[0]) {
+               status = smbcli_full_connection(NULL, &cli, lp_netbios_name(),
+                                            binding->host, NULL, 
+                                            "ipc$", "?????", 
+                                            "", "", NULL, 0, &retry);
+       } else {
+               status = smbcli_full_connection(NULL, &cli, lp_netbios_name(),
+                                            binding->host, NULL, 
+                                            "ipc$", "?????", 
+                                            username, domain,
+                                            password, 0, &retry);
+       }
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("Failed to connect to %s - %s\n", binding->host, nt_errstr(status)));
                return status;
@@ -469,25 +479,34 @@ static NTSTATUS dcerpc_pipe_connect_ncacn_np(struct dcerpc_pipe **p,
        status = dcerpc_pipe_open_smb(p, cli->tree, pipe_name);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("Failed to open pipe %s - %s\n", pipe_name, nt_errstr(status)));
-               cli_tdis(cli);
-               cli_shutdown(cli);
+               smbcli_tdis(cli);
+               smbcli_shutdown(cli);
                 return status;
         }
        
        /* this ensures that the reference count is decremented so
           a pipe close will really close the link */
-       cli_tree_close(cli->tree);
-       
+       talloc_steal(*p, cli);
+
        (*p)->flags = binding->flags;
 
-       if (binding->flags & (DCERPC_SIGN | DCERPC_SEAL)) {
+       /* remember the binding string for possible secondary connections */
+       (*p)->binding_string = dcerpc_binding_string((*p), binding);
+
+       if (username && username[0] && (binding->flags & DCERPC_SCHANNEL_ANY)) {
+               status = dcerpc_bind_auth_schannel(*p, pipe_uuid, pipe_version, 
+                                                  domain, username, password);
+       } else if (username && username[0]) {
                status = dcerpc_bind_auth_ntlm(*p, pipe_uuid, pipe_version, domain, username, password);
        } else {    
                status = dcerpc_bind_auth_none(*p, pipe_uuid, pipe_version);
+
        }
+
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("Failed to bind to uuid %s - %s\n", pipe_uuid, nt_errstr(status)));
                dcerpc_pipe_close(*p);
+               *p = NULL;
                return status;
        }
 
@@ -500,13 +519,13 @@ static NTSTATUS dcerpc_pipe_connect_ncacn_np(struct dcerpc_pipe **p,
 static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp(struct dcerpc_pipe **p, 
                                                 struct dcerpc_binding *binding,
                                                 const char *pipe_uuid, 
-                                                uint32 pipe_version,
+                                                uint32_t pipe_version,
                                                 const char *domain,
                                                 const char *username,
                                                 const char *password)
 {
        NTSTATUS status;
-       uint32 port = 0;
+       uint32_t port = 0;
 
        if (binding->options && binding->options[0]) {
                port = atoi(binding->options[0]);
@@ -524,30 +543,31 @@ static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp(struct dcerpc_pipe **p,
                DEBUG(1,("Mapped to DCERPC/TCP port %u\n", port));
        }
 
-       status = dcerpc_pipe_open_tcp(p, binding->host, port);
+       status = dcerpc_pipe_open_tcp(p, binding->host, port, AF_UNSPEC);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("Failed to connect to %s:%d\n", binding->host, port));
                 return status;
         }
 
-       /* it doesn't seem to work to do a null NTLMSSP session without either sign
-          or seal, so force signing if we are doing ntlmssp */
-       if (username[0] && !(binding->flags & (DCERPC_SIGN|DCERPC_SEAL))) {
-               binding->flags |= DCERPC_SIGN;
-       }
-
        (*p)->flags = binding->flags;
 
-       if (!(binding->flags & (DCERPC_SIGN|DCERPC_SEAL)) && !username[0]) {
+       /* remember the binding string for possible secondary connections */
+       (*p)->binding_string = dcerpc_binding_string((*p), binding);
+
+       if (username && username[0] && (binding->flags & DCERPC_SCHANNEL_ANY)) {
+               status = dcerpc_bind_auth_schannel(*p, pipe_uuid, pipe_version, 
+                                                  domain, username, password);
+       } else if (username && username[0]) {
+               status = dcerpc_bind_auth_ntlm(*p, pipe_uuid, pipe_version, domain, username, password);
+       } else {    
                status = dcerpc_bind_auth_none(*p, pipe_uuid, pipe_version);
-       } else {
-               status = dcerpc_bind_auth_ntlm(*p, pipe_uuid, pipe_version,
-                                              domain, username, password);
        }
 
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0,("Failed to bind to uuid %s - %s\n", pipe_uuid, nt_errstr(status)));
+               DEBUG(0,("Failed to bind to uuid %s - %s\n", 
+                        pipe_uuid, nt_errstr(status)));
                dcerpc_pipe_close(*p);
+               *p = NULL;
                return status;
        }
  
@@ -560,7 +580,7 @@ static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp(struct dcerpc_pipe **p,
 NTSTATUS dcerpc_pipe_connect_b(struct dcerpc_pipe **p, 
                               struct dcerpc_binding *binding,
                               const char *pipe_uuid, 
-                              uint32 pipe_version,
+                              uint32_t pipe_version,
                               const char *domain,
                               const char *username,
                               const char *password)
@@ -587,7 +607,7 @@ NTSTATUS dcerpc_pipe_connect_b(struct dcerpc_pipe **p,
 NTSTATUS dcerpc_pipe_connect(struct dcerpc_pipe **p, 
                             const char *binding,
                             const char *pipe_uuid, 
-                            uint32 pipe_version,
+                            uint32_t pipe_version,
                             const char *domain,
                             const char *username,
                             const char *password)
@@ -613,3 +633,108 @@ NTSTATUS dcerpc_pipe_connect(struct dcerpc_pipe **p,
        talloc_destroy(mem_ctx);
        return status;
 }
+
+
+/*
+  create a secondary dcerpc connection from a primary connection
+
+  if the primary is a SMB connection then the secondary connection
+  will be on the same SMB connection, but use a new fnum
+*/
+NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p, struct dcerpc_pipe **p2,
+                                    const char *pipe_name,
+                                    const char *pipe_uuid,
+                                    uint32_t pipe_version)
+{
+       struct smbcli_tree *tree;
+       NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
+       struct dcerpc_binding b;
+
+       switch (p->transport.transport) {
+       case NCACN_NP:
+               tree = dcerpc_smb_tree(p);
+               if (!tree) {
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+
+               status = dcerpc_pipe_open_smb(p2, tree, pipe_name);
+               break;
+
+       case NCACN_IP_TCP:
+               status = dcerpc_parse_binding(p, p->binding_string, &b);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+               b.flags &= ~DCERPC_AUTH_OPTIONS;
+               status = dcerpc_pipe_connect_ncacn_ip_tcp(p2, &b, pipe_uuid,
+                                                         pipe_version, NULL, 
+                                                         NULL, NULL);
+               break;
+       }
+
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       (*p2)->flags = p->flags;
+
+       status = dcerpc_bind_auth_none(*p2, pipe_uuid, pipe_version);
+       if (!NT_STATUS_IS_OK(status)) {
+                return status;
+        }
+
+       return NT_STATUS_OK;
+}
+
+NTSTATUS dcerpc_generic_session_key(struct dcerpc_pipe *p,
+                                   DATA_BLOB *session_key)
+{
+       /* this took quite a few CPU cycles to find ... */
+       session_key->data = discard_const_p(char, "SystemLibraryDTC");
+       session_key->length = 16;
+       return NT_STATUS_OK;
+}
+
+/*
+  fetch the user session key - may be default (above) or the SMB session key
+*/
+NTSTATUS dcerpc_fetch_session_key(struct dcerpc_pipe *p,
+                                 DATA_BLOB *session_key)
+{
+       return p->security_state.session_key(p, session_key);
+}
+
+
+/*
+  log a rpc packet in a format suitable for ndrdump. This is especially useful
+  for sealed packets, where ethereal cannot easily see the contents
+
+  this triggers on a debug level of >= 10
+*/
+void dcerpc_log_packet(const struct dcerpc_interface_table *ndr,
+                      uint32_t opnum, uint32_t flags, DATA_BLOB *pkt)
+{
+       const int num_examples = 20;
+       int i;
+
+       if (DEBUGLEVEL < 10) return;
+
+       for (i=0;i<num_examples;i++) {
+               char *name=NULL;
+               asprintf(&name, "%s/rpclog/%s-%u.%d.%s", 
+                        lp_lockdir(), ndr->name, opnum, i,
+                        (flags&NDR_IN)?"in":"out");
+               if (name == NULL) {
+                       return;
+               }
+               if (!file_exist(name, NULL)) {
+                       if (file_save(name, pkt->data, pkt->length)) {
+                               DEBUG(10,("Logged rpc packet to %s\n", name));
+                       }
+                       free(name);
+                       break;
+               }
+               free(name);
+       }
+}
+