r5603: add "authservice()" property to the interface property list
[samba.git] / source4 / librpc / rpc / dcerpc_util.c
index 7639ca3fdd018da77b7615c6b5e8bc2c5a8eacb5..7b753d1b3070a9dfaf9b762d5bbdf5e8d793115b 100644 (file)
@@ -184,6 +184,80 @@ static const struct {
        {"bigendian", DCERPC_PUSH_BIGENDIAN}
 };
 
+const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *fl)
+{
+       struct GUID uuid;
+       uint16_t if_version;
+       NTSTATUS status;
+
+       switch(fl->lhs.protocol) {
+               case EPM_PROTOCOL_UUID:
+                       status = dcerpc_floor_get_lhs_data(fl, &uuid, &if_version);
+                       if (NT_STATUS_IS_OK(status)) {
+                               /* lhs is used: UUID */
+                               char *uuidstr;
+
+                               uuidstr = GUID_string(mem_ctx, &uuid);
+
+                               if (strcasecmp(uuidstr, NDR_GUID) == 0) {
+                                       return "NDR";
+                               } 
+
+                               return talloc_asprintf(mem_ctx, " uuid %s/0x%02x", uuidstr, if_version);
+                       } else { /* IPX */
+                               return talloc_asprintf(mem_ctx, "IPX:%s", 
+                                               data_blob_hex_string(mem_ctx, &fl->rhs.uuid.unknown));
+                       }
+
+               case EPM_PROTOCOL_NCACN:
+                       return "RPC-C";
+
+               case EPM_PROTOCOL_NCADG:
+                       return "RPC";
+
+               case EPM_PROTOCOL_NCALRPC:
+                       return "NCALRPC";
+
+               case EPM_PROTOCOL_DNET_NSP:
+                       return "DNET/NSP";
+
+               case EPM_PROTOCOL_IP:
+                       return talloc_asprintf(mem_ctx, "IP:%s", fl->rhs.ip.ipaddr);
+
+               case EPM_PROTOCOL_PIPE:
+                       return talloc_asprintf(mem_ctx, "PIPE:%s", fl->rhs.pipe.path);
+
+               case EPM_PROTOCOL_SMB:
+                       return talloc_asprintf(mem_ctx, "SMB:%s", fl->rhs.smb.unc);
+
+               case EPM_PROTOCOL_UNIX_DS:
+                       return talloc_asprintf(mem_ctx, "Unix:%s", fl->rhs.unix_ds.path);
+
+               case EPM_PROTOCOL_NETBIOS:
+                       return talloc_asprintf(mem_ctx, "NetBIOS:%s", fl->rhs.netbios.name);
+
+               case EPM_PROTOCOL_NETBEUI:
+                       return "NETBeui";
+
+               case EPM_PROTOCOL_SPX:
+                       return "SPX";
+
+               case EPM_PROTOCOL_NB_IPX:
+                       return "NB_IPX";
+
+               case EPM_PROTOCOL_HTTP:
+                       return talloc_asprintf(mem_ctx, "HTTP:%d", fl->rhs.http.port);
+
+               case EPM_PROTOCOL_TCP:
+                       return talloc_asprintf(mem_ctx, "TCP:%d", fl->rhs.tcp.port);
+
+               case EPM_PROTOCOL_UDP:
+                       return talloc_asprintf(mem_ctx, "UDP:%d", fl->rhs.udp.port);
+
+               default:
+                       return talloc_asprintf(mem_ctx, "UNK(%02x):", fl->lhs.protocol);
+       }
+}
 
 
 /*
@@ -370,6 +444,39 @@ NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_
        return NT_STATUS_OK;
 }
 
+NTSTATUS dcerpc_floor_get_lhs_data(struct epm_floor *floor, struct GUID *uuid, uint16_t *if_version)
+{
+       TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
+       struct ndr_pull *ndr = ndr_pull_init_blob(&floor->lhs.lhs_data, mem_ctx);
+       NTSTATUS status;
+       
+       ndr->flags |= LIBNDR_FLAG_NOALIGN;
+
+       status = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, uuid);
+       if (NT_STATUS_IS_ERR(status)) {
+               talloc_free(mem_ctx);
+               return status;
+       }
+
+       status = ndr_pull_uint16(ndr, NDR_SCALARS, if_version);
+
+       talloc_free(mem_ctx);
+
+       return status;
+}
+
+DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, struct GUID *uuid, uint32_t if_version)
+{
+       struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx);
+
+       ndr->flags |= LIBNDR_FLAG_NOALIGN;
+
+       ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, uuid);
+       ndr_push_uint16(ndr, NDR_SCALARS, if_version);
+
+       return ndr_push_blob(ndr);
+}
+
 const char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *floor)
 {
        switch (floor->lhs.protocol) {
@@ -386,15 +493,7 @@ const char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *flo
                return talloc_asprintf(mem_ctx, "%d", floor->rhs.http.port);
 
        case EPM_PROTOCOL_IP:
-               if (floor->rhs.ip.ipaddr.addr == 0) {
-                       return NULL; 
-               }
-
-               {
-                       struct ipv4_addr in;
-                       in.addr = htonl(floor->rhs.ip.ipaddr.addr);
-                       return talloc_strdup(mem_ctx, sys_inet_ntoa(in));
-               }
+               return talloc_strdup(mem_ctx, floor->rhs.ip.ipaddr);
 
        case EPM_PROTOCOL_NCACN:
                return NULL;
@@ -457,11 +556,8 @@ static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor
                return NT_STATUS_OK;
 
        case EPM_PROTOCOL_IP:
-               if (strlen(data) > 0) {
-                       floor->rhs.ip.ipaddr.addr = ntohl(interpret_addr(data));
-               } else {
-                       floor->rhs.ip.ipaddr.addr = 0;
-               }
+               floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
+               NT_STATUS_HAVE_NO_MEMORY(floor->rhs.ip.ipaddr);
                return NT_STATUS_OK;
 
        case EPM_PROTOCOL_NCACN:
@@ -474,23 +570,17 @@ static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor
 
        case EPM_PROTOCOL_SMB:
                floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
-               if (!floor->rhs.smb.unc) {
-                       return NT_STATUS_NO_MEMORY;
-               }
+               NT_STATUS_HAVE_NO_MEMORY(floor->rhs.smb.unc);
                return NT_STATUS_OK;
 
        case EPM_PROTOCOL_PIPE:
                floor->rhs.pipe.path = talloc_strdup(mem_ctx, data);
-               if (!floor->rhs.pipe.path) {
-                       return NT_STATUS_NO_MEMORY;
-               }
+               NT_STATUS_HAVE_NO_MEMORY(floor->rhs.pipe.path);
                return NT_STATUS_OK;
 
        case EPM_PROTOCOL_NETBIOS:
                floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
-               if (!floor->rhs.netbios.name) {
-                       return NT_STATUS_NO_MEMORY;
-               }
+               NT_STATUS_HAVE_NO_MEMORY(floor->rhs.netbios.name);
                return NT_STATUS_OK;
 
        case EPM_PROTOCOL_NCALRPC:
@@ -506,16 +596,12 @@ static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor
                
        case EPM_PROTOCOL_STREETTALK:
                floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
-               if (!floor->rhs.streettalk.streettalk) {
-                       return NT_STATUS_NO_MEMORY;
-               }
+               NT_STATUS_HAVE_NO_MEMORY(floor->rhs.streettalk.streettalk);
                return NT_STATUS_OK;
                
        case EPM_PROTOCOL_UNIX_DS:
                floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
-               if (!floor->rhs.unix_ds.path) {
-                       return NT_STATUS_NO_MEMORY;
-               }
+               NT_STATUS_HAVE_NO_MEMORY(floor->rhs.unix_ds.path);
                return NT_STATUS_OK;
                
        case EPM_PROTOCOL_NULL:
@@ -573,6 +659,8 @@ enum dcerpc_transport_t dcerpc_transport_by_tower(struct epm_tower *tower)
 
 NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx, struct epm_tower *tower, struct dcerpc_binding *binding)
 {
+       NTSTATUS status;
+
        ZERO_STRUCT(binding->object);
        binding->options = NULL;
        binding->host = NULL;
@@ -589,8 +677,12 @@ NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx, struct epm_tower *tower,
        }
 
        /* Set object uuid */
-       binding->object = tower->floors[0].lhs.info.uuid.uuid;
-       binding->object_version = tower->floors[0].lhs.info.uuid.version;
+       status = dcerpc_floor_get_lhs_data(&tower->floors[0], &binding->object, &binding->object_version);
+       
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("Error pulling object uuid and version: %s", nt_errstr(status)));     
+               return status;
+       }
 
        /* Ignore floor 1, it contains the NDR version info */
        
@@ -614,6 +706,7 @@ NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx, struct dcerpc_binding *
 {
        const enum epm_protocol *protseq = NULL;
        int num_protocols = -1, i;
+       struct GUID ndr_guid;
        NTSTATUS status;
        
        /* Find transport */
@@ -635,23 +728,27 @@ NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx, struct dcerpc_binding *
 
        /* Floor 0 */
        tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
-       tower->floors[0].lhs.info.uuid.uuid = binding->object;
-       tower->floors[0].lhs.info.uuid.version = binding->object_version;
-       tower->floors[0].rhs.uuid.unknown = 0;
+
+       tower->floors[0].lhs.lhs_data = dcerpc_floor_pack_lhs_data(mem_ctx, &binding->object, binding->object_version);
+
+       tower->floors[0].rhs.uuid.unknown = data_blob_talloc_zero(mem_ctx, 2);
        
        /* Floor 1 */
        tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
-       tower->floors[1].lhs.info.uuid.version = NDR_GUID_VERSION;
-       tower->floors[1].rhs.uuid.unknown = 0;
-       status = GUID_from_string(NDR_GUID, &tower->floors[1].lhs.info.uuid.uuid);
+
+       status = GUID_from_string(NDR_GUID, &ndr_guid);
        if (NT_STATUS_IS_ERR(status)) {
                return status;
        }
 
+       tower->floors[1].lhs.lhs_data = dcerpc_floor_pack_lhs_data(mem_ctx, &ndr_guid, NDR_GUID_VERSION);
+       
+       tower->floors[1].rhs.uuid.unknown = data_blob_talloc_zero(mem_ctx, 2);
+       
        /* Floor 2 to num_protocols */
        for (i = 0; i < num_protocols; i++) {
                tower->floors[2 + i].lhs.protocol = protseq[i];
-               tower->floors[2 + i].lhs.info.lhs_data = data_blob_talloc(mem_ctx, NULL, 0);
+               tower->floors[2 + i].lhs.lhs_data = data_blob_talloc(mem_ctx, NULL, 0);
                ZERO_STRUCT(tower->floors[2 + i].rhs);
                dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[2 + i], "");
        }
@@ -692,13 +789,15 @@ NTSTATUS dcerpc_epm_map_binding(TALLOC_CTX *mem_ctx, struct dcerpc_binding *bind
 
        if (table) {
                struct dcerpc_binding default_binding;
-               
+
+               binding->authservice = talloc_strdup(mem_ctx, table->authservices->names[0]);
+
                /* Find one of the default pipes for this interface */
                for (i = 0; i < table->endpoints->count; i++) {
                        status = dcerpc_parse_binding(mem_ctx, table->endpoints->names[i], &default_binding);
 
                        if (NT_STATUS_IS_OK(status) && default_binding.transport == binding->transport && default_binding.endpoint) {
-                               binding->endpoint = talloc_strdup(mem_ctx, default_binding.endpoint);   
+                               binding->endpoint = talloc_strdup(mem_ctx, default_binding.endpoint);
                                return NT_STATUS_OK;
                        }
                }
@@ -711,6 +810,7 @@ NTSTATUS dcerpc_epm_map_binding(TALLOC_CTX *mem_ctx, struct dcerpc_binding *bind
        epmapper_binding.options = NULL;
        epmapper_binding.flags = 0;
        epmapper_binding.endpoint = NULL;
+       epmapper_binding.authservice = NULL;
        
        status = dcerpc_pipe_connect_b(&p,
                                        &epmapper_binding,
@@ -806,8 +906,9 @@ static NTSTATUS dcerpc_pipe_auth(struct dcerpc_pipe *p,
 
                status = dcerpc_bind_auth_password(p, pipe_uuid, pipe_version, 
                                                   domain, username, password, 
-                                                  auth_type);
-       } else {    
+                                                  auth_type,
+                                                  binding->authservice);
+       } else {
                status = dcerpc_bind_auth_none(p, pipe_uuid, pipe_version);
        }
 
@@ -1241,7 +1342,7 @@ void dcerpc_log_packet(const struct dcerpc_interface_table *ndr,
                if (name == NULL) {
                        return;
                }
-               if (!file_exist(name, NULL)) {
+               if (!file_exist(name)) {
                        if (file_save(name, pkt->data, pkt->length)) {
                                DEBUG(10,("Logged rpc packet to %s\n", name));
                        }