Copyright (C) Tim Potter 2003
Copyright (C) Andrew Tridgell 2003
+ Copyright (C) Jelmer Vernooij 2004
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
*/
#include "includes.h"
+#include "dlinklist.h"
+#include "librpc/gen_ndr/ndr_epmapper.h"
-/* initialise a dcerpc pipe. This currently assumes a SMB named pipe
- transport */
+struct dcerpc_interface_list *dcerpc_pipes = NULL;
+
+NTSTATUS librpc_register_interface (const struct dcerpc_interface_table *interface)
+{
+ struct dcerpc_interface_list *l = talloc_p(NULL, struct dcerpc_interface_list);
+
+ if (idl_iface_by_name (interface->name) != NULL) {
+ DEBUG(0, ("Attempt to register interface %s twice\n", interface->name));
+ return NT_STATUS_OBJECT_NAME_COLLISION;
+ }
+ l->table = interface;
+
+ DLIST_ADD(dcerpc_pipes, l);
+
+ return NT_STATUS_OK;
+}
+
+/* initialise a dcerpc pipe. */
struct dcerpc_pipe *dcerpc_pipe_init(void)
{
struct dcerpc_pipe *p;
if (!p) return;
p->reference_count--;
if (p->reference_count <= 0) {
- if (p->security_state.generic_state) {
- gensec_end(&p->security_state.generic_state);
- }
p->transport.shutdown_pipe(p);
talloc_free(p);
}
return ndr_pull_dcerpc_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
}
+/*
+ generate a CONNECT level verifier
+*/
+static NTSTATUS dcerpc_connect_verifier(TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
+{
+ *blob = data_blob_talloc(mem_ctx, NULL, 16);
+ if (blob->data == NULL) {
+ return NT_STATUS_NO_MEMORY;
+ }
+ SIVAL(blob->data, 0, 1);
+ memset(blob->data+4, 0, 12);
+ return NT_STATUS_OK;
+}
+
+/*
+ generate a CONNECT level verifier
+*/
+static NTSTATUS dcerpc_check_connect_verifier(DATA_BLOB *blob)
+{
+ if (blob->length != 16 ||
+ IVAL(blob->data, 0) != 1) {
+ return NT_STATUS_ACCESS_DENIED;
+ }
+ return NT_STATUS_OK;
+}
+
/*
parse a possibly signed blob into a dcerpc request packet structure
*/
DATA_BLOB auth_blob;
/* non-signed packets are simpler */
- if (!p->security_state.auth_info || !p->security_state.generic_state) {
+ if (!p->security_state.auth_info ||
+ !p->security_state.generic_state) {
return dcerpc_pull(p, blob, mem_ctx, pkt);
}
return status;
}
+ if (pkt->auth_length == 0 &&
+ p->security_state.auth_info->auth_level == DCERPC_AUTH_LEVEL_CONNECT) {
+ return NT_STATUS_OK;
+ }
+
auth_blob.length = 8 + pkt->auth_length;
/* check for a valid length */
blob->length - auth.credentials.length,
&auth.credentials);
break;
-
+
+ case DCERPC_AUTH_LEVEL_CONNECT:
+ status = dcerpc_check_connect_verifier(&auth.credentials);
+ break;
+
case DCERPC_AUTH_LEVEL_NONE:
break;
DATA_BLOB creds2;
/* non-signed packets are simpler */
- if (!p->security_state.auth_info || !p->security_state.generic_state) {
+ if (!p->security_state.auth_info ||
+ !p->security_state.generic_state) {
return dcerpc_push_auth(blob, mem_ctx, pkt, p->security_state.auth_info);
}
ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
}
+ if (pkt->pfc_flags & DCERPC_PFC_FLAG_ORPC) {
+ ndr->flags |= LIBNDR_FLAG_OBJECT_PRESENT;
+ }
+
status = ndr_push_dcerpc_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
if (!NT_STATUS_IS_OK(status)) {
return status;
data_blob_clear(&p->security_state.auth_info->credentials);
break;
+ case DCERPC_AUTH_LEVEL_CONNECT:
+ status = dcerpc_connect_verifier(mem_ctx, &p->security_state.auth_info->credentials);
+ break;
+
case DCERPC_AUTH_LEVEL_NONE:
p->security_state.auth_info->credentials = data_blob(NULL, 0);
break;
blob->length -
p->security_state.auth_info->credentials.length,
&creds2);
+ if (!NT_STATUS_IS_OK(status)) {
+ return status;
+ }
memcpy(blob->data + blob->length - creds2.length, creds2.data, creds2.length);
break;
blob->length -
p->security_state.auth_info->credentials.length,
&creds2);
+ if (!NT_STATUS_IS_OK(status)) {
+ return status;
+ }
memcpy(blob->data + blob->length - creds2.length, creds2.data, creds2.length);
break;
+ case DCERPC_AUTH_LEVEL_CONNECT:
+ break;
+
case DCERPC_AUTH_LEVEL_NONE:
p->security_state.auth_info->credentials = data_blob(NULL, 0);
break;
while (NT_STATUS_IS_OK(state->status) && state->reply_blob) {
struct event_context *ctx = p->transport.event_context(p);
- event_loop_once(ctx);
+ if (event_loop_once(ctx) != 0) {
+ return NT_STATUS_CONNECTION_DISCONNECTED;
+ }
}
return state->status;
pkt.u.bind.max_recv_frag = 5840;
pkt.u.bind.assoc_group_id = 0;
pkt.u.bind.num_contexts = 1;
- pkt.u.bind.ctx_list = talloc(mem_ctx, sizeof(pkt.u.bind.ctx_list[0]));
+ pkt.u.bind.ctx_list = talloc_p(mem_ctx, struct dcerpc_ctx_list);
if (!pkt.u.bind.ctx_list) {
return NT_STATUS_NO_MEMORY;
}
pkt.u.alter.max_recv_frag = 0x2000;
pkt.u.alter.assoc_group_id = 0;
pkt.u.alter.num_contexts = 1;
- pkt.u.alter.ctx_list = talloc(mem_ctx, sizeof(pkt.u.alter.ctx_list[0]));
+ pkt.u.alter.ctx_list = talloc_p(mem_ctx, struct dcerpc_ctx_list);
if (!pkt.u.alter.ctx_list) {
return NT_STATUS_NO_MEMORY;
}
pkt.pfc_flags = DCERPC_PFC_FLAG_FIRST | DCERPC_PFC_FLAG_LAST;
pkt.call_id = next_call_id(p);
pkt.auth_length = 0;
- pkt.u.auth._pad = 0;
- pkt.u.auth.auth_info = data_blob(NULL, 0);
+ pkt.u.auth3._pad = 0;
+ pkt.u.auth3.auth_info = data_blob(NULL, 0);
/* construct the NDR form of the packet */
status = dcerpc_push_auth(&blob, mem_ctx, &pkt, p->security_state.auth_info);
}
if (req == NULL) {
- DEBUG(2,("dcerpc_request: unmatched call_id in response packet\n"));
+ DEBUG(2,("dcerpc_request: unmatched call_id %u in response packet\n", pkt.call_id));
return;
}
}
if (pkt.ptype == DCERPC_PKT_FAULT) {
- DEBUG(5,("rpc fault 0x%x\n", pkt.u.fault.status));
+ DEBUG(5,("rpc fault: %s\n", dcerpc_errstr(p, pkt.u.fault.status)));
req->fault_code = pkt.u.fault.status;
req->status = NT_STATUS_NET_WRITE_FAULT;
req->state = RPC_REQUEST_DONE;
length = pkt.u.response.stub_and_verifier.length;
if (length > 0) {
- req->payload.data = talloc_realloc(req->payload.data,
+ req->payload.data = talloc_realloc(req,
+ req->payload.data,
req->payload.length + length);
if (!req->payload.data) {
req->status = NT_STATUS_NO_MEMORY;
}
+/*
+ make sure requests are cleaned up
+ */
+static int dcerpc_req_destructor(void *ptr)
+{
+ struct rpc_request *req = ptr;
+ DLIST_REMOVE(req->p->pending, req);
+ return 0;
+}
+
/*
perform the send size of a async dcerpc request
*/
struct rpc_request *dcerpc_request_send(struct dcerpc_pipe *p,
+ const struct GUID *object,
uint16_t opnum,
TALLOC_CTX *mem_ctx,
DATA_BLOB *stub_data)
struct dcerpc_packet pkt;
DATA_BLOB blob;
uint32_t remaining, chunk_size;
+ BOOL first_packet = True;
p->transport.recv_data = dcerpc_request_recv_data;
pkt.ptype = DCERPC_PKT_REQUEST;
pkt.call_id = req->call_id;
pkt.auth_length = 0;
+ pkt.pfc_flags = 0;
pkt.u.request.alloc_hint = remaining;
pkt.u.request.context_id = 0;
pkt.u.request.opnum = opnum;
+ if (object) {
+ pkt.u.request.object.object = *object;
+ pkt.pfc_flags |= DCERPC_PFC_FLAG_ORPC;
+ chunk_size -= ndr_size_GUID(0,object,0);
+ }
+
DLIST_ADD(p->pending, req);
/* we send a series of pdus without waiting for a reply */
- while (remaining > 0) {
+ while (remaining > 0 || first_packet) {
uint32_t chunk = MIN(chunk_size, remaining);
BOOL last_frag = False;
- pkt.pfc_flags = 0;
+ first_packet = False;
+ pkt.pfc_flags &= ~(DCERPC_PFC_FLAG_FIRST |DCERPC_PFC_FLAG_LAST);
if (remaining == stub_data->length) {
pkt.pfc_flags |= DCERPC_PFC_FLAG_FIRST;
remaining -= chunk;
}
+ talloc_set_destructor(req, dcerpc_req_destructor);
+
return req;
}
while (req->state == RPC_REQUEST_PENDING) {
struct event_context *ctx = dcerpc_event_context(req->p);
- event_loop_once(ctx);
+ if (event_loop_once(ctx) != 0) {
+ return NT_STATUS_CONNECTION_DISCONNECTED;
+ }
}
*stub_data = req->payload;
status = req->status;
perform a full request/response pair on a dcerpc pipe
*/
NTSTATUS dcerpc_request(struct dcerpc_pipe *p,
+ struct GUID *object,
uint16_t opnum,
TALLOC_CTX *mem_ctx,
DATA_BLOB *stub_data_in,
{
struct rpc_request *req;
- req = dcerpc_request_send(p, opnum, mem_ctx, stub_data_in);
+ req = dcerpc_request_send(p, object, opnum, mem_ctx, stub_data_in);
if (req == NULL) {
return NT_STATUS_NO_MEMORY;
}
/*
- send a rpc request with a given set of ndr helper functions
-
- call dcerpc_ndr_request_recv() to receive the answer
-*/
+ send a rpc request given a dcerpc_call structure
+ */
struct rpc_request *dcerpc_ndr_request_send(struct dcerpc_pipe *p,
- uint32_t opnum,
- TALLOC_CTX *mem_ctx,
- NTSTATUS (*ndr_push)(struct ndr_push *, int, void *),
- NTSTATUS (*ndr_pull)(struct ndr_pull *, int, void *),
- void *struct_ptr,
- size_t struct_size)
+ const struct GUID *object,
+ const struct dcerpc_interface_table *table,
+ uint32_t opnum,
+ TALLOC_CTX *mem_ctx,
+ void *r)
{
+ const struct dcerpc_interface_call *call;
struct ndr_push *push;
NTSTATUS status;
DATA_BLOB request;
struct rpc_request *req;
+ call = &table->calls[opnum];
+
/* setup for a ndr_push_* call */
push = ndr_push_init();
if (!push) {
}
/* push the structure into a blob */
- status = ndr_push(push, NDR_IN, struct_ptr);
+ status = call->ndr_push(push, NDR_IN, r);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(2,("Unable to ndr_push structure in dcerpc_ndr_request_send - %s\n",
nt_errstr(status)));
request = ndr_push_blob(push);
if (p->flags & DCERPC_DEBUG_VALIDATE_IN) {
- status = dcerpc_ndr_validate_in(p, mem_ctx, request, struct_size,
- ndr_push, ndr_pull);
+ status = dcerpc_ndr_validate_in(p, mem_ctx, request, call->struct_size,
+ call->ndr_push, call->ndr_pull);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(2,("Validation failed in dcerpc_ndr_request_send - %s\n",
nt_errstr(status)));
dump_data(10, request.data, request.length);
/* make the actual dcerpc request */
- req = dcerpc_request_send(p, opnum, mem_ctx, &request);
+ req = dcerpc_request_send(p, object, opnum, mem_ctx, &request);
if (req != NULL) {
- req->ndr.ndr_push = ndr_push;
- req->ndr.ndr_pull = ndr_pull;
- req->ndr.struct_ptr = struct_ptr;
- req->ndr.struct_size = struct_size;
+ req->ndr.table = table;
+ req->ndr.opnum = opnum;
+ req->ndr.struct_ptr = r;
req->ndr.mem_ctx = mem_ctx;
}
NTSTATUS status;
DATA_BLOB response;
struct ndr_pull *pull;
- struct rpc_request_ndr ndr = req->ndr;
uint_t flags;
+ TALLOC_CTX *mem_ctx = req->ndr.mem_ctx;
+ void *r = req->ndr.struct_ptr;
+ uint32_t opnum = req->ndr.opnum;
+ const struct dcerpc_interface_table *table = req->ndr.table;
+ const struct dcerpc_interface_call *call = &table->calls[opnum];
/* make sure the recv code doesn't free the request, as we
need to grab the flags element before it is freed */
talloc_increase_ref_count(req);
- status = dcerpc_request_recv(req, ndr.mem_ctx, &response);
+ status = dcerpc_request_recv(req, mem_ctx, &response);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
talloc_free(req);
/* prepare for ndr_pull_* */
- pull = ndr_pull_init_flags(p, &response, ndr.mem_ctx);
+ pull = ndr_pull_init_flags(p, &response, mem_ctx);
if (!pull) {
return NT_STATUS_NO_MEMORY;
}
dump_data(10, pull->data, pull->data_size);
/* pull the structure from the blob */
- status = ndr.ndr_pull(pull, NDR_OUT, ndr.struct_ptr);
+ status = call->ndr_pull(pull, NDR_OUT, r);
if (!NT_STATUS_IS_OK(status)) {
+ dcerpc_log_packet(table, opnum, NDR_OUT,
+ &response);
return status;
}
if (p->flags & DCERPC_DEBUG_VALIDATE_OUT) {
- status = dcerpc_ndr_validate_out(p, ndr.mem_ctx, ndr.struct_ptr, ndr.struct_size,
- ndr.ndr_push, ndr.ndr_pull);
+ status = dcerpc_ndr_validate_out(p, mem_ctx, r, call->struct_size,
+ call->ndr_push, call->ndr_pull);
if (!NT_STATUS_IS_OK(status)) {
+ dcerpc_log_packet(table, opnum, NDR_OUT,
+ &response);
return status;
}
}
standard format
*/
NTSTATUS dcerpc_ndr_request(struct dcerpc_pipe *p,
- uint32_t opnum,
- TALLOC_CTX *mem_ctx,
- NTSTATUS (*ndr_push)(struct ndr_push *, int, void *),
- NTSTATUS (*ndr_pull)(struct ndr_pull *, int, void *),
- void *struct_ptr,
- size_t struct_size)
+ const struct GUID *object,
+ const struct dcerpc_interface_table *table,
+ uint32_t opnum,
+ TALLOC_CTX *mem_ctx,
+ void *r)
{
struct rpc_request *req;
- req = dcerpc_ndr_request_send(p, opnum, mem_ctx, ndr_push, ndr_pull, struct_ptr, struct_size);
+ req = dcerpc_ndr_request_send(p, object, table, opnum, mem_ctx, r);
if (req == NULL) {
return NT_STATUS_NO_MEMORY;
}
auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
} else if (p->flags & DCERPC_SIGN) {
auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
+ } else if (p->flags & DCERPC_CONNECT) {
+ auth_level = DCERPC_AUTH_LEVEL_CONNECT;
} else {
auth_level = DCERPC_AUTH_LEVEL_NONE;
}