s4:rpc_server: only use context within op_bind() hooks and dcesrv_interface_bind_...
[samba.git] / source4 / rpc_server / epmapper / rpc_epmapper.c
index eb9598b6be37f56b1e5969fe84e8a5bb24d24192..7e9c2614f298a6e28f9087ba3e652f62b30b2401 100644 (file)
@@ -8,7 +8,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
 #include "librpc/gen_ndr/ndr_epmapper.h"
 #include "rpc_server/dcerpc_server.h"
-#include "rpc_server/common/common.h"
+
+#define DCESRV_INTERFACE_EPMAPPER_BIND(call, iface) \
+       dcesrv_interface_epmapper_bind(call, iface)
+static NTSTATUS dcesrv_interface_epmapper_bind(struct dcesrv_call_state *dce_call,
+                                            const struct dcesrv_interface *iface)
+{
+       struct dcesrv_connection_context *context = dce_call->context;
+       return dcesrv_interface_bind_allow_connect(context, iface);
+}
 
 typedef uint32_t error_status_t;
 
@@ -37,18 +44,6 @@ struct dcesrv_ep_iface {
        struct epm_tower ep;
 };
 
-/*
-  simple routine to compare a GUID string to a GUID structure
-*/
-static int guid_cmp(TALLOC_CTX *mem_ctx, const struct GUID *guid, const char *uuid_str)
-{
-       const char *s = GUID_string(mem_ctx, guid);
-       if (!s || strcasecmp(s, uuid_str)) {
-               return -1;
-       }
-       return 0;
-}
-
 /*
   build a list of all interfaces handled by all endpoint servers
 */
@@ -64,9 +59,10 @@ static uint32_t build_ep_list(TALLOC_CTX *mem_ctx,
 
        for (d=endpoint_list; d; d=d->next) {
                struct dcesrv_if_list *iface;
-               struct dcerpc_binding description;
 
                for (iface=d->interface_list;iface;iface=iface->next) {
+                       struct dcerpc_binding *description;
+
                        (*eps) = talloc_realloc(mem_ctx, 
                                                  *eps, 
                                                  struct dcesrv_ep_iface,
@@ -76,13 +72,22 @@ static uint32_t build_ep_list(TALLOC_CTX *mem_ctx,
                        }
                        (*eps)[total].name = iface->iface.name;
 
-                       description = d->ep_description;
-                       GUID_from_string(iface->iface.uuid, &description.object);
-                       description.object_version = iface->iface.if_version;
+                       description = dcerpc_binding_dup(*eps, d->ep_description);
+                       if (description == NULL) {
+                               return 0;
+                       }
 
-                       status = dcerpc_binding_build_tower(mem_ctx, &description, &(*eps)[total].ep);
-                       if (NT_STATUS_IS_ERR(status)) {
-                               DEBUG(1, ("Unable to build tower for %s\n", iface->iface.name));
+                       status = dcerpc_binding_set_abstract_syntax(description,
+                                                       &iface->iface.syntax_id);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               return 0;
+                       }
+
+                       status = dcerpc_binding_build_tower(*eps, description, &(*eps)[total].ep);
+                       TALLOC_FREE(description);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               DEBUG(1, ("Unable to build tower for %s - %s\n",
+                                         iface->iface.name, nt_errstr(status)));
                                continue;
                        }
                        total++;
@@ -93,13 +98,12 @@ static uint32_t build_ep_list(TALLOC_CTX *mem_ctx,
 }
 
 
-static error_status_t epm_Insert(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
-                                struct epm_Insert *r)
+static error_status_t dcesrv_epm_Insert(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct epm_Insert *r)
 {
        DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
 }
 
-static error_status_t epm_Delete(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
+static error_status_t dcesrv_epm_Delete(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
                                 struct epm_Delete *r)
 {
        DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
@@ -110,7 +114,7 @@ static error_status_t epm_Delete(struct dcesrv_call_state *dce_call, TALLOC_CTX
   implement epm_Lookup. This call is used to enumerate the interfaces
   available on a rpc server
 */
-static error_status_t epm_Lookup(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
+static error_status_t dcesrv_epm_Lookup(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
                                 struct epm_Lookup *r)
 {
        struct dcesrv_handle *h;
@@ -119,7 +123,7 @@ static error_status_t epm_Lookup(struct dcesrv_call_state *dce_call, TALLOC_CTX
                struct dcesrv_ep_iface *e;
        } *eps;
        uint32_t num_ents;
-       int i;
+       unsigned int i;
 
        DCESRV_PULL_HANDLE_FAULT(h, r->in.entry_handle, HTYPE_LOOKUP);
 
@@ -144,7 +148,8 @@ static error_status_t epm_Lookup(struct dcesrv_call_state *dce_call, TALLOC_CTX
        }
 
        *r->out.entry_handle = h->wire_handle;
-       r->out.num_ents = num_ents;
+       r->out.num_ents = talloc(mem_ctx, uint32_t);
+       *r->out.num_ents = num_ents;
 
        if (num_ents == 0) {
                r->out.entries = NULL;
@@ -179,19 +184,24 @@ static error_status_t epm_Lookup(struct dcesrv_call_state *dce_call, TALLOC_CTX
   implement epm_Map. This is used to find the specific endpoint to talk to given
   a generic protocol tower
 */
-static error_status_t epm_Map(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
+static error_status_t dcesrv_epm_Map(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
                              struct epm_Map *r)
 {
        uint32_t count;
-       int i;
+       unsigned int i;
        struct dcesrv_ep_iface *eps;
        struct epm_floor *floors;
        enum dcerpc_transport_t transport;
+       struct ndr_syntax_id ndr_syntax;
 
        count = build_ep_list(mem_ctx, dce_call->conn->dce_ctx->endpoint_list, &eps);
 
        ZERO_STRUCT(*r->out.entry_handle);
-       r->out.num_towers = 1;
+       r->out.num_towers = talloc(mem_ctx, uint32_t);
+       if (!r->out.num_towers) {
+               return EPMAPPER_STATUS_NO_MEMORY;
+       }
+       *r->out.num_towers = 1;
        r->out.towers = talloc(mem_ctx, struct epm_twr_p_t);
        if (!r->out.towers) {
                return EPMAPPER_STATUS_NO_MEMORY;
@@ -208,9 +218,11 @@ static error_status_t epm_Map(struct dcesrv_call_state *dce_call, TALLOC_CTX *me
 
        floors = r->in.map_tower->tower.floors;
 
+       dcerpc_floor_get_lhs_data(&r->in.map_tower->tower.floors[1], &ndr_syntax);
+
        if (floors[1].lhs.protocol != EPM_PROTOCOL_UUID ||
-           guid_cmp(mem_ctx, &floors[1].lhs.info.uuid.uuid, NDR_GUID) != 0 ||
-           floors[1].lhs.info.uuid.version != NDR_GUID_VERSION) {
+               !GUID_equal(&ndr_syntax.uuid, &ndr_transfer_syntax_ndr.uuid) ||
+           ndr_syntax.if_version != ndr_transfer_syntax_ndr.if_version) {
                goto failed;
        }
 
@@ -226,11 +238,10 @@ static error_status_t epm_Map(struct dcesrv_call_state *dce_call, TALLOC_CTX *me
        }
 
        for (i=0;i<count;i++) {
-               if (!GUID_equal(&r->in.map_tower->tower.floors[0].lhs.info.uuid.uuid,
-                                          &eps[i].ep.floors[0].lhs.info.uuid.uuid) ||
-                       r->in.map_tower->tower.floors[0].lhs.info.uuid.version != 
-                               eps[i].ep.floors[0].lhs.info.uuid.version ||
-                               transport != dcerpc_transport_by_tower(&eps[i].ep)) {
+               if (
+                       data_blob_cmp(&r->in.map_tower->tower.floors[0].lhs.lhs_data, 
+                       &eps[i].ep.floors[0].lhs.lhs_data) != 0 
+                       || transport != dcerpc_transport_by_tower(&eps[i].ep)) {
                        continue;
                }
                
@@ -241,31 +252,31 @@ static error_status_t epm_Map(struct dcesrv_call_state *dce_call, TALLOC_CTX *me
 
 
 failed:
-       r->out.num_towers = 0;
+       *r->out.num_towers = 0;
        r->out.towers->twr = NULL;
 
        return EPMAPPER_STATUS_NO_MORE_ENTRIES;
 }
 
-static error_status_t epm_LookupHandleFree(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
+static error_status_t dcesrv_epm_LookupHandleFree(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
                                           struct epm_LookupHandleFree *r)
 {
        DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
 }
 
-static error_status_t epm_InqObject(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
+static error_status_t dcesrv_epm_InqObject(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
                                    struct epm_InqObject *r)
 {
        DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
 }
 
-static error_status_t epm_MgmtDelete(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
+static error_status_t dcesrv_epm_MgmtDelete(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
                               struct epm_MgmtDelete *r)
 {
        DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
 }
 
-static error_status_t epm_MapAuth(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
+static error_status_t dcesrv_epm_MapAuth(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
                            struct epm_MapAuth *r)
 {
        DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);