2 Unix SMB/CIFS implementation.
5 Copyright (C) Andrew Tridgell 2003-2005
6 Copyright (C) Jelmer Vernooij 2004-2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "system/network.h"
25 #include "lib/tsocket/tsocket.h"
26 #include "lib/util/tevent_ntstatus.h"
27 #include "librpc/rpc/dcerpc.h"
28 #include "librpc/gen_ndr/ndr_dcerpc.h"
29 #include "rpc_common.h"
30 #include "lib/util/bitmap.h"
32 /* we need to be able to get/set the fragment length without doing a full
34 void dcerpc_set_frag_length(DATA_BLOB *blob, uint16_t v)
36 if (CVAL(blob->data,DCERPC_DREP_OFFSET) & DCERPC_DREP_LE) {
37 SSVAL(blob->data, DCERPC_FRAG_LEN_OFFSET, v);
39 RSSVAL(blob->data, DCERPC_FRAG_LEN_OFFSET, v);
43 uint16_t dcerpc_get_frag_length(const DATA_BLOB *blob)
45 if (CVAL(blob->data,DCERPC_DREP_OFFSET) & DCERPC_DREP_LE) {
46 return SVAL(blob->data, DCERPC_FRAG_LEN_OFFSET);
48 return RSVAL(blob->data, DCERPC_FRAG_LEN_OFFSET);
52 void dcerpc_set_auth_length(DATA_BLOB *blob, uint16_t v)
54 if (CVAL(blob->data,DCERPC_DREP_OFFSET) & DCERPC_DREP_LE) {
55 SSVAL(blob->data, DCERPC_AUTH_LEN_OFFSET, v);
57 RSSVAL(blob->data, DCERPC_AUTH_LEN_OFFSET, v);
61 uint8_t dcerpc_get_endian_flag(DATA_BLOB *blob)
63 return blob->data[DCERPC_DREP_OFFSET];
68 * @brief Pull a dcerpc_auth structure, taking account of any auth
69 * padding in the blob. For request/response packets we pass
70 * the whole data blob, so auth_data_only must be set to false
71 * as the blob contains data+pad+auth and no just pad+auth.
73 * @param pkt - The ncacn_packet strcuture
74 * @param mem_ctx - The mem_ctx used to allocate dcerpc_auth elements
75 * @param pkt_trailer - The packet trailer data, usually the trailing
76 * auth_info blob, but in the request/response case
77 * this is the stub_and_verifier blob.
78 * @param auth - A preallocated dcerpc_auth *empty* structure
79 * @param auth_length - The length of the auth trail, sum of auth header
80 * lenght and pkt->auth_length
81 * @param auth_data_only - Whether the pkt_trailer includes only the auth_blob
82 * (+ padding) or also other data.
84 * @return - A NTSTATUS error code.
86 NTSTATUS dcerpc_pull_auth_trailer(struct ncacn_packet *pkt,
88 DATA_BLOB *pkt_trailer,
89 struct dcerpc_auth *auth,
90 uint32_t *auth_length,
94 enum ndr_err_code ndr_err;
95 uint32_t data_and_pad;
97 data_and_pad = pkt_trailer->length
98 - (DCERPC_AUTH_TRAILER_LENGTH + pkt->auth_length);
100 /* paranoia check for pad size. This would be caught anyway by
101 the ndr_pull_advance() a few lines down, but it scared
102 Jeremy enough for him to call me, so we might as well check
103 it now, just to prevent someone posting a bogus YouTube
106 if (data_and_pad > pkt_trailer->length) {
107 return NT_STATUS_INFO_LENGTH_MISMATCH;
110 *auth_length = pkt_trailer->length - data_and_pad;
112 ndr = ndr_pull_init_blob(pkt_trailer, mem_ctx);
114 return NT_STATUS_NO_MEMORY;
117 if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
118 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
121 ndr_err = ndr_pull_advance(ndr, data_and_pad);
122 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
124 return ndr_map_error2ntstatus(ndr_err);
127 ndr_err = ndr_pull_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, auth);
128 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
130 return ndr_map_error2ntstatus(ndr_err);
133 if (auth_data_only && data_and_pad != auth->auth_pad_length) {
134 DEBUG(1, (__location__ ": WARNING: pad length mismatch. "
135 "Calculated %u got %u\n",
136 (unsigned)data_and_pad,
137 (unsigned)auth->auth_pad_length));
140 DEBUG(6,(__location__ ": auth_pad_length %u\n",
141 (unsigned)auth->auth_pad_length));
143 talloc_steal(mem_ctx, auth->credentials.data);
149 struct dcerpc_read_ncacn_packet_state {
155 struct ncacn_packet *pkt;
158 static int dcerpc_read_ncacn_packet_next_vector(struct tstream_context *stream,
161 struct iovec **_vector,
163 static void dcerpc_read_ncacn_packet_done(struct tevent_req *subreq);
165 struct tevent_req *dcerpc_read_ncacn_packet_send(TALLOC_CTX *mem_ctx,
166 struct tevent_context *ev,
167 struct tstream_context *stream)
169 struct tevent_req *req;
170 struct dcerpc_read_ncacn_packet_state *state;
171 struct tevent_req *subreq;
173 req = tevent_req_create(mem_ctx, &state,
174 struct dcerpc_read_ncacn_packet_state);
179 state->buffer = data_blob_const(NULL, 0);
180 state->pkt = talloc(state, struct ncacn_packet);
181 if (tevent_req_nomem(state->pkt, req)) {
185 subreq = tstream_readv_pdu_send(state, ev,
187 dcerpc_read_ncacn_packet_next_vector,
189 if (tevent_req_nomem(subreq, req)) {
192 tevent_req_set_callback(subreq, dcerpc_read_ncacn_packet_done, req);
196 tevent_req_post(req, ev);
200 static int dcerpc_read_ncacn_packet_next_vector(struct tstream_context *stream,
203 struct iovec **_vector,
206 struct dcerpc_read_ncacn_packet_state *state =
207 talloc_get_type_abort(private_data,
208 struct dcerpc_read_ncacn_packet_state);
209 struct iovec *vector;
212 if (state->buffer.length == 0) {
214 * first get enough to read the fragment length
216 * We read the full fixed ncacn_packet header
217 * in order to make wireshark happy with
218 * pcap files from socket_wrapper.
221 state->buffer.length = DCERPC_NCACN_PAYLOAD_OFFSET;
222 state->buffer.data = talloc_array(state, uint8_t,
223 state->buffer.length);
224 if (!state->buffer.data) {
227 } else if (state->buffer.length == DCERPC_NCACN_PAYLOAD_OFFSET) {
228 /* now read the fragment length and allocate the full buffer */
229 size_t frag_len = dcerpc_get_frag_length(&state->buffer);
231 ofs = state->buffer.length;
233 if (frag_len < ofs) {
235 * something is wrong, let the caller deal with it
242 state->buffer.data = talloc_realloc(state,
245 if (!state->buffer.data) {
248 state->buffer.length = frag_len;
250 /* if we reach this we have a full fragment */
256 /* now create the vector that we want to be filled */
257 vector = talloc_array(mem_ctx, struct iovec, 1);
262 vector[0].iov_base = (void *) (state->buffer.data + ofs);
263 vector[0].iov_len = state->buffer.length - ofs;
270 static void dcerpc_read_ncacn_packet_done(struct tevent_req *subreq)
272 struct tevent_req *req = tevent_req_callback_data(subreq,
274 struct dcerpc_read_ncacn_packet_state *state = tevent_req_data(req,
275 struct dcerpc_read_ncacn_packet_state);
278 struct ndr_pull *ndr;
279 enum ndr_err_code ndr_err;
282 ret = tstream_readv_pdu_recv(subreq, &sys_errno);
285 status = map_nt_error_from_unix_common(sys_errno);
286 tevent_req_nterror(req, status);
290 ndr = ndr_pull_init_blob(&state->buffer, state->pkt);
291 if (tevent_req_nomem(ndr, req)) {
295 if (!(CVAL(ndr->data, DCERPC_DREP_OFFSET) & DCERPC_DREP_LE)) {
296 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
299 if (CVAL(ndr->data, DCERPC_PFC_OFFSET) & DCERPC_PFC_FLAG_OBJECT_UUID) {
300 ndr->flags |= LIBNDR_FLAG_OBJECT_PRESENT;
303 ndr_err = ndr_pull_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, state->pkt);
305 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
306 status = ndr_map_error2ntstatus(ndr_err);
307 tevent_req_nterror(req, status);
311 if (state->pkt->frag_length != state->buffer.length) {
312 tevent_req_nterror(req, NT_STATUS_RPC_PROTOCOL_ERROR);
316 tevent_req_done(req);
319 NTSTATUS dcerpc_read_ncacn_packet_recv(struct tevent_req *req,
321 struct ncacn_packet **pkt,
324 struct dcerpc_read_ncacn_packet_state *state = tevent_req_data(req,
325 struct dcerpc_read_ncacn_packet_state);
328 if (tevent_req_is_nterror(req, &status)) {
329 tevent_req_received(req);
333 *pkt = talloc_move(mem_ctx, &state->pkt);
335 buffer->data = talloc_move(mem_ctx, &state->buffer.data);
336 buffer->length = state->buffer.length;
339 tevent_req_received(req);
343 const char *dcerpc_default_transport_endpoint(TALLOC_CTX *mem_ctx,
344 enum dcerpc_transport_t transport,
345 const struct ndr_interface_table *table)
348 const char *p = NULL;
349 const char *endpoint = NULL;
351 struct dcerpc_binding *default_binding = NULL;
352 TALLOC_CTX *frame = talloc_stackframe();
354 /* Find one of the default pipes for this interface */
356 for (i = 0; i < table->endpoints->count; i++) {
357 enum dcerpc_transport_t dtransport;
358 const char *dendpoint;
360 status = dcerpc_parse_binding(frame, table->endpoints->names[i],
362 if (!NT_STATUS_IS_OK(status)) {
366 dtransport = dcerpc_binding_get_transport(default_binding);
367 dendpoint = dcerpc_binding_get_string_option(default_binding,
369 if (dendpoint == NULL) {
370 TALLOC_FREE(default_binding);
374 if (transport == NCA_UNKNOWN) {
375 transport = dtransport;
378 if (transport != dtransport) {
379 TALLOC_FREE(default_binding);
392 * extract the pipe name without \\pipe from for example
393 * ncacn_np:[\\pipe\\epmapper]
395 if (transport == NCACN_NP) {
396 if (strncasecmp(p, "\\pipe\\", 6) == 0) {
399 if (strncmp(p, "\\", 1) == 0) {
404 endpoint = talloc_strdup(mem_ctx, p);
411 struct dcerpc_sec_vt_header2 dcerpc_sec_vt_header2_from_ncacn_packet(const struct ncacn_packet *pkt)
413 struct dcerpc_sec_vt_header2 ret;
416 ret.ptype = pkt->ptype;
417 memcpy(&ret.drep, pkt->drep, sizeof(ret.drep));
418 ret.call_id = pkt->call_id;
420 switch (pkt->ptype) {
421 case DCERPC_PKT_REQUEST:
422 ret.context_id = pkt->u.request.context_id;
423 ret.opnum = pkt->u.request.opnum;
426 case DCERPC_PKT_RESPONSE:
427 ret.context_id = pkt->u.response.context_id;
430 case DCERPC_PKT_FAULT:
431 ret.context_id = pkt->u.fault.context_id;
441 bool dcerpc_sec_vt_header2_equal(const struct dcerpc_sec_vt_header2 *v1,
442 const struct dcerpc_sec_vt_header2 *v2)
444 if (v1->ptype != v2->ptype) {
448 if (memcmp(v1->drep, v2->drep, sizeof(v1->drep)) != 0) {
452 if (v1->call_id != v2->call_id) {
456 if (v1->context_id != v2->context_id) {
460 if (v1->opnum != v2->opnum) {
467 static bool dcerpc_sec_vt_is_valid(const struct dcerpc_sec_verification_trailer *r)
470 TALLOC_CTX *frame = talloc_stackframe();
471 struct bitmap *commands_seen;
474 if (r->count.count == 0) {
479 if (memcmp(r->magic, DCERPC_SEC_VT_MAGIC, sizeof(r->magic)) != 0) {
483 commands_seen = bitmap_talloc(frame, DCERPC_SEC_VT_COMMAND_ENUM + 1);
484 if (commands_seen == NULL) {
488 for (i=0; i < r->count.count; i++) {
489 enum dcerpc_sec_vt_command_enum cmd =
490 r->commands[i].command & DCERPC_SEC_VT_COMMAND_ENUM;
492 if (bitmap_query(commands_seen, cmd)) {
493 /* Each command must appear at most once. */
496 bitmap_set(commands_seen, cmd);
499 case DCERPC_SEC_VT_COMMAND_BITMASK1:
500 case DCERPC_SEC_VT_COMMAND_PCONTEXT:
501 case DCERPC_SEC_VT_COMMAND_HEADER2:
504 if ((r->commands[i].u._unknown.length % 4) != 0) {
516 static bool dcerpc_sec_vt_bitmask_check(const uint32_t *bitmask1,
517 struct dcerpc_sec_vt *c)
519 if (bitmask1 == NULL) {
520 if (c->command & DCERPC_SEC_VT_MUST_PROCESS) {
521 DEBUG(10, ("SEC_VT check Bitmask1 must_process_command "
529 if ((c->u.bitmask1 & DCERPC_SEC_VT_CLIENT_SUPPORTS_HEADER_SIGNING)
530 && (!(*bitmask1 & DCERPC_SEC_VT_CLIENT_SUPPORTS_HEADER_SIGNING))) {
531 DEBUG(10, ("SEC_VT check Bitmask1 client_header_signing "
538 static bool dcerpc_sec_vt_pctx_check(const struct dcerpc_sec_vt_pcontext *pcontext,
539 struct dcerpc_sec_vt *c)
544 if (pcontext == NULL) {
545 if (c->command & DCERPC_SEC_VT_MUST_PROCESS) {
546 DEBUG(10, ("SEC_VT check Pcontext must_process_command "
554 mem_ctx = talloc_stackframe();
555 ok = ndr_syntax_id_equal(&pcontext->abstract_syntax,
556 &c->u.pcontext.abstract_syntax);
558 DEBUG(10, ("SEC_VT check pcontext abstract_syntax failed: "
560 ndr_syntax_id_to_string(mem_ctx,
561 &pcontext->abstract_syntax),
562 ndr_syntax_id_to_string(mem_ctx,
563 &c->u.pcontext.abstract_syntax)));
566 ok = ndr_syntax_id_equal(&pcontext->transfer_syntax,
567 &c->u.pcontext.transfer_syntax);
569 DEBUG(10, ("SEC_VT check pcontext transfer_syntax failed: "
571 ndr_syntax_id_to_string(mem_ctx,
572 &pcontext->transfer_syntax),
573 ndr_syntax_id_to_string(mem_ctx,
574 &c->u.pcontext.transfer_syntax)));
580 talloc_free(mem_ctx);
584 static bool dcerpc_sec_vt_hdr2_check(const struct dcerpc_sec_vt_header2 *header2,
585 struct dcerpc_sec_vt *c)
587 if (header2 == NULL) {
588 if (c->command & DCERPC_SEC_VT_MUST_PROCESS) {
589 DEBUG(10, ("SEC_VT check Header2 must_process_command failed\n"));
596 if (!dcerpc_sec_vt_header2_equal(header2, &c->u.header2)) {
597 DEBUG(10, ("SEC_VT check Header2 failed\n"));
604 bool dcerpc_sec_verification_trailer_check(
605 const struct dcerpc_sec_verification_trailer *vt,
606 const uint32_t *bitmask1,
607 const struct dcerpc_sec_vt_pcontext *pcontext,
608 const struct dcerpc_sec_vt_header2 *header2)
612 if (!dcerpc_sec_vt_is_valid(vt)) {
616 for (i=0; i < vt->count.count; i++) {
618 struct dcerpc_sec_vt *c = &vt->commands[i];
620 switch (c->command & DCERPC_SEC_VT_COMMAND_ENUM) {
621 case DCERPC_SEC_VT_COMMAND_BITMASK1:
622 ok = dcerpc_sec_vt_bitmask_check(bitmask1, c);
628 case DCERPC_SEC_VT_COMMAND_PCONTEXT:
629 ok = dcerpc_sec_vt_pctx_check(pcontext, c);
635 case DCERPC_SEC_VT_COMMAND_HEADER2: {
636 ok = dcerpc_sec_vt_hdr2_check(header2, c);
644 if (c->command & DCERPC_SEC_VT_MUST_PROCESS) {
645 DEBUG(10, ("SEC_VT check Unknown must_process_command failed\n"));