2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client routines
4 * Largely rewritten by Jeremy Allison 2005.
5 * Heavily modified by Simo Sorce 2010.
6 * Copyright Andrew Bartlett 2011.
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 "../lib/util/tevent_ntstatus.h"
24 #include "librpc/gen_ndr/ndr_epmapper_c.h"
25 #include "../librpc/gen_ndr/ndr_dssetup.h"
26 #include "../libcli/auth/schannel.h"
27 #include "../libcli/auth/netlogon_creds_cli.h"
28 #include "auth_generic.h"
29 #include "librpc/gen_ndr/ndr_dcerpc.h"
30 #include "librpc/gen_ndr/ndr_netlogon_c.h"
31 #include "librpc/rpc/dcerpc.h"
34 #include "libsmb/libsmb.h"
35 #include "auth/gensec/gensec.h"
36 #include "auth/credentials/credentials.h"
37 #include "../libcli/smb/smbXcli_base.h"
40 #define DBGC_CLASS DBGC_RPC_CLI
42 /********************************************************************
43 Pipe description for a DEBUG
44 ********************************************************************/
45 static const char *rpccli_pipe_txt(TALLOC_CTX *mem_ctx,
46 struct rpc_pipe_client *cli)
48 char *result = talloc_asprintf(mem_ctx, "host %s", cli->desthost);
55 /********************************************************************
57 ********************************************************************/
59 static uint32 get_rpc_call_id(void)
61 static uint32 call_id = 0;
65 /*******************************************************************
66 Use SMBreadX to get rest of one fragment's worth of rpc data.
67 Reads the whole size or give an error message
68 ********************************************************************/
70 struct rpc_read_state {
71 struct tevent_context *ev;
72 struct rpc_cli_transport *transport;
78 static void rpc_read_done(struct tevent_req *subreq);
80 static struct tevent_req *rpc_read_send(TALLOC_CTX *mem_ctx,
81 struct tevent_context *ev,
82 struct rpc_cli_transport *transport,
83 uint8_t *data, size_t size)
85 struct tevent_req *req, *subreq;
86 struct rpc_read_state *state;
88 req = tevent_req_create(mem_ctx, &state, struct rpc_read_state);
93 state->transport = transport;
98 DEBUG(5, ("rpc_read_send: data_to_read: %u\n", (unsigned int)size));
100 subreq = transport->read_send(state, ev, (uint8_t *)data, size,
102 if (subreq == NULL) {
105 tevent_req_set_callback(subreq, rpc_read_done, req);
113 static void rpc_read_done(struct tevent_req *subreq)
115 struct tevent_req *req = tevent_req_callback_data(
116 subreq, struct tevent_req);
117 struct rpc_read_state *state = tevent_req_data(
118 req, struct rpc_read_state);
122 status = state->transport->read_recv(subreq, &received);
124 if (!NT_STATUS_IS_OK(status)) {
125 tevent_req_nterror(req, status);
129 state->num_read += received;
130 if (state->num_read == state->size) {
131 tevent_req_done(req);
135 subreq = state->transport->read_send(state, state->ev,
136 state->data + state->num_read,
137 state->size - state->num_read,
138 state->transport->priv);
139 if (tevent_req_nomem(subreq, req)) {
142 tevent_req_set_callback(subreq, rpc_read_done, req);
145 static NTSTATUS rpc_read_recv(struct tevent_req *req)
147 return tevent_req_simple_recv_ntstatus(req);
150 struct rpc_write_state {
151 struct tevent_context *ev;
152 struct rpc_cli_transport *transport;
158 static void rpc_write_done(struct tevent_req *subreq);
160 static struct tevent_req *rpc_write_send(TALLOC_CTX *mem_ctx,
161 struct tevent_context *ev,
162 struct rpc_cli_transport *transport,
163 const uint8_t *data, size_t size)
165 struct tevent_req *req, *subreq;
166 struct rpc_write_state *state;
168 req = tevent_req_create(mem_ctx, &state, struct rpc_write_state);
173 state->transport = transport;
176 state->num_written = 0;
178 DEBUG(5, ("rpc_write_send: data_to_write: %u\n", (unsigned int)size));
180 subreq = transport->write_send(state, ev, data, size, transport->priv);
181 if (subreq == NULL) {
184 tevent_req_set_callback(subreq, rpc_write_done, req);
191 static void rpc_write_done(struct tevent_req *subreq)
193 struct tevent_req *req = tevent_req_callback_data(
194 subreq, struct tevent_req);
195 struct rpc_write_state *state = tevent_req_data(
196 req, struct rpc_write_state);
200 status = state->transport->write_recv(subreq, &written);
202 if (!NT_STATUS_IS_OK(status)) {
203 tevent_req_nterror(req, status);
207 state->num_written += written;
209 if (state->num_written == state->size) {
210 tevent_req_done(req);
214 subreq = state->transport->write_send(state, state->ev,
215 state->data + state->num_written,
216 state->size - state->num_written,
217 state->transport->priv);
218 if (tevent_req_nomem(subreq, req)) {
221 tevent_req_set_callback(subreq, rpc_write_done, req);
224 static NTSTATUS rpc_write_recv(struct tevent_req *req)
226 return tevent_req_simple_recv_ntstatus(req);
230 /****************************************************************************
231 Try and get a PDU's worth of data from current_pdu. If not, then read more
233 ****************************************************************************/
235 struct get_complete_frag_state {
236 struct tevent_context *ev;
237 struct rpc_pipe_client *cli;
242 static void get_complete_frag_got_header(struct tevent_req *subreq);
243 static void get_complete_frag_got_rest(struct tevent_req *subreq);
245 static struct tevent_req *get_complete_frag_send(TALLOC_CTX *mem_ctx,
246 struct tevent_context *ev,
247 struct rpc_pipe_client *cli,
250 struct tevent_req *req, *subreq;
251 struct get_complete_frag_state *state;
255 req = tevent_req_create(mem_ctx, &state,
256 struct get_complete_frag_state);
262 state->frag_len = RPC_HEADER_LEN;
265 received = pdu->length;
266 if (received < RPC_HEADER_LEN) {
267 if (!data_blob_realloc(mem_ctx, pdu, RPC_HEADER_LEN)) {
268 status = NT_STATUS_NO_MEMORY;
271 subreq = rpc_read_send(state, state->ev,
272 state->cli->transport,
273 pdu->data + received,
274 RPC_HEADER_LEN - received);
275 if (subreq == NULL) {
276 status = NT_STATUS_NO_MEMORY;
279 tevent_req_set_callback(subreq, get_complete_frag_got_header,
284 state->frag_len = dcerpc_get_frag_length(pdu);
285 if (state->frag_len < RPC_HEADER_LEN) {
286 tevent_req_nterror(req, NT_STATUS_RPC_PROTOCOL_ERROR);
287 return tevent_req_post(req, ev);
291 * Ensure we have frag_len bytes of data.
293 if (received < state->frag_len) {
294 if (!data_blob_realloc(NULL, pdu, state->frag_len)) {
295 status = NT_STATUS_NO_MEMORY;
298 subreq = rpc_read_send(state, state->ev,
299 state->cli->transport,
300 pdu->data + received,
301 state->frag_len - received);
302 if (subreq == NULL) {
303 status = NT_STATUS_NO_MEMORY;
306 tevent_req_set_callback(subreq, get_complete_frag_got_rest,
311 status = NT_STATUS_OK;
313 if (NT_STATUS_IS_OK(status)) {
314 tevent_req_done(req);
316 tevent_req_nterror(req, status);
318 return tevent_req_post(req, ev);
321 static void get_complete_frag_got_header(struct tevent_req *subreq)
323 struct tevent_req *req = tevent_req_callback_data(
324 subreq, struct tevent_req);
325 struct get_complete_frag_state *state = tevent_req_data(
326 req, struct get_complete_frag_state);
329 status = rpc_read_recv(subreq);
331 if (!NT_STATUS_IS_OK(status)) {
332 tevent_req_nterror(req, status);
336 state->frag_len = dcerpc_get_frag_length(state->pdu);
337 if (state->frag_len < RPC_HEADER_LEN) {
338 tevent_req_nterror(req, NT_STATUS_RPC_PROTOCOL_ERROR);
342 if (!data_blob_realloc(NULL, state->pdu, state->frag_len)) {
343 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
348 * We're here in this piece of code because we've read exactly
349 * RPC_HEADER_LEN bytes into state->pdu.
352 subreq = rpc_read_send(state, state->ev, state->cli->transport,
353 state->pdu->data + RPC_HEADER_LEN,
354 state->frag_len - RPC_HEADER_LEN);
355 if (tevent_req_nomem(subreq, req)) {
358 tevent_req_set_callback(subreq, get_complete_frag_got_rest, req);
361 static void get_complete_frag_got_rest(struct tevent_req *subreq)
363 struct tevent_req *req = tevent_req_callback_data(
364 subreq, struct tevent_req);
367 status = rpc_read_recv(subreq);
369 if (!NT_STATUS_IS_OK(status)) {
370 tevent_req_nterror(req, status);
373 tevent_req_done(req);
376 static NTSTATUS get_complete_frag_recv(struct tevent_req *req)
378 return tevent_req_simple_recv_ntstatus(req);
381 /****************************************************************************
382 Do basic authentication checks on an incoming pdu.
383 ****************************************************************************/
385 static NTSTATUS cli_pipe_validate_current_pdu(TALLOC_CTX *mem_ctx,
386 struct rpc_pipe_client *cli,
387 struct ncacn_packet *pkt,
389 uint8_t expected_pkt_type,
392 DATA_BLOB *reply_pdu)
394 struct dcerpc_response *r;
395 NTSTATUS ret = NT_STATUS_OK;
399 * Point the return values at the real data including the RPC
400 * header. Just in case the caller wants it.
404 /* Ensure we have the correct type. */
405 switch (pkt->ptype) {
406 case DCERPC_PKT_ALTER_RESP:
407 case DCERPC_PKT_BIND_ACK:
409 /* Client code never receives this kind of packets */
413 case DCERPC_PKT_RESPONSE:
415 r = &pkt->u.response;
417 /* Here's where we deal with incoming sign/seal. */
418 ret = dcerpc_check_auth(cli->auth, pkt,
419 &r->stub_and_verifier,
420 DCERPC_RESPONSE_LENGTH,
422 if (!NT_STATUS_IS_OK(ret)) {
426 if (pkt->frag_length < DCERPC_RESPONSE_LENGTH + pad_len) {
427 return NT_STATUS_BUFFER_TOO_SMALL;
430 /* Point the return values at the NDR data. */
431 rdata->data = r->stub_and_verifier.data;
433 if (pkt->auth_length) {
434 /* We've already done integer wrap tests in
435 * dcerpc_check_auth(). */
436 rdata->length = r->stub_and_verifier.length
438 - DCERPC_AUTH_TRAILER_LENGTH
441 rdata->length = r->stub_and_verifier.length;
444 DEBUG(10, ("Got pdu len %lu, data_len %lu, ss_len %u\n",
445 (long unsigned int)pdu->length,
446 (long unsigned int)rdata->length,
447 (unsigned int)pad_len));
450 * If this is the first reply, and the allocation hint is
451 * reasonable, try and set up the reply_pdu DATA_BLOB to the
455 if ((reply_pdu->length == 0) &&
456 r->alloc_hint && (r->alloc_hint < 15*1024*1024)) {
457 if (!data_blob_realloc(mem_ctx, reply_pdu,
459 DEBUG(0, ("reply alloc hint %d too "
460 "large to allocate\n",
461 (int)r->alloc_hint));
462 return NT_STATUS_NO_MEMORY;
468 case DCERPC_PKT_BIND_NAK:
469 DEBUG(1, (__location__ ": Bind NACK received from %s!\n",
470 rpccli_pipe_txt(talloc_tos(), cli)));
471 /* Use this for now... */
472 return NT_STATUS_NETWORK_ACCESS_DENIED;
474 case DCERPC_PKT_FAULT:
476 DEBUG(1, (__location__ ": RPC fault code %s received "
478 dcerpc_errstr(talloc_tos(),
479 pkt->u.fault.status),
480 rpccli_pipe_txt(talloc_tos(), cli)));
482 return dcerpc_fault_to_nt_status(pkt->u.fault.status);
485 DEBUG(0, (__location__ "Unknown packet type %u received "
487 (unsigned int)pkt->ptype,
488 rpccli_pipe_txt(talloc_tos(), cli)));
489 return NT_STATUS_RPC_PROTOCOL_ERROR;
492 if (pkt->ptype != expected_pkt_type) {
493 DEBUG(3, (__location__ ": Connection to %s got an unexpected "
494 "RPC packet type - %u, not %u\n",
495 rpccli_pipe_txt(talloc_tos(), cli),
496 pkt->ptype, expected_pkt_type));
497 return NT_STATUS_RPC_PROTOCOL_ERROR;
500 if (pkt->call_id != call_id) {
501 DEBUG(3, (__location__ ": Connection to %s got an unexpected "
502 "RPC call_id - %u, not %u\n",
503 rpccli_pipe_txt(talloc_tos(), cli),
504 pkt->call_id, call_id));
505 return NT_STATUS_RPC_PROTOCOL_ERROR;
508 /* Do this just before return - we don't want to modify any rpc header
509 data before now as we may have needed to do cryptographic actions on
512 if ((pkt->ptype == DCERPC_PKT_BIND_ACK) &&
513 !(pkt->pfc_flags & DCERPC_PFC_FLAG_LAST)) {
514 DEBUG(5, (__location__ ": bug in server (AS/U?), setting "
515 "fragment first/last ON.\n"));
516 pkt->pfc_flags |= DCERPC_PFC_FLAG_FIRST | DCERPC_PFC_FLAG_LAST;
522 /****************************************************************************
523 Call a remote api on an arbitrary pipe. takes param, data and setup buffers.
524 ****************************************************************************/
526 struct cli_api_pipe_state {
527 struct tevent_context *ev;
528 struct rpc_cli_transport *transport;
533 static void cli_api_pipe_trans_done(struct tevent_req *subreq);
534 static void cli_api_pipe_write_done(struct tevent_req *subreq);
535 static void cli_api_pipe_read_done(struct tevent_req *subreq);
537 static struct tevent_req *cli_api_pipe_send(TALLOC_CTX *mem_ctx,
538 struct tevent_context *ev,
539 struct rpc_cli_transport *transport,
540 uint8_t *data, size_t data_len,
541 uint32_t max_rdata_len)
543 struct tevent_req *req, *subreq;
544 struct cli_api_pipe_state *state;
547 req = tevent_req_create(mem_ctx, &state, struct cli_api_pipe_state);
552 state->transport = transport;
554 if (max_rdata_len < RPC_HEADER_LEN) {
556 * For a RPC reply we always need at least RPC_HEADER_LEN
557 * bytes. We check this here because we will receive
558 * RPC_HEADER_LEN bytes in cli_trans_sock_send_done.
560 status = NT_STATUS_INVALID_PARAMETER;
564 if (transport->trans_send != NULL) {
565 subreq = transport->trans_send(state, ev, data, data_len,
566 max_rdata_len, transport->priv);
567 if (subreq == NULL) {
570 tevent_req_set_callback(subreq, cli_api_pipe_trans_done, req);
575 * If the transport does not provide a "trans" routine, i.e. for
576 * example the ncacn_ip_tcp transport, do the write/read step here.
579 subreq = rpc_write_send(state, ev, transport, data, data_len);
580 if (subreq == NULL) {
583 tevent_req_set_callback(subreq, cli_api_pipe_write_done, req);
587 tevent_req_nterror(req, status);
588 return tevent_req_post(req, ev);
594 static void cli_api_pipe_trans_done(struct tevent_req *subreq)
596 struct tevent_req *req = tevent_req_callback_data(
597 subreq, struct tevent_req);
598 struct cli_api_pipe_state *state = tevent_req_data(
599 req, struct cli_api_pipe_state);
602 status = state->transport->trans_recv(subreq, state, &state->rdata,
605 if (!NT_STATUS_IS_OK(status)) {
606 tevent_req_nterror(req, status);
609 tevent_req_done(req);
612 static void cli_api_pipe_write_done(struct tevent_req *subreq)
614 struct tevent_req *req = tevent_req_callback_data(
615 subreq, struct tevent_req);
616 struct cli_api_pipe_state *state = tevent_req_data(
617 req, struct cli_api_pipe_state);
620 status = rpc_write_recv(subreq);
622 if (!NT_STATUS_IS_OK(status)) {
623 tevent_req_nterror(req, status);
627 state->rdata = talloc_array(state, uint8_t, RPC_HEADER_LEN);
628 if (tevent_req_nomem(state->rdata, req)) {
633 * We don't need to use rpc_read_send here, the upper layer will cope
634 * with a short read, transport->trans_send could also return less
635 * than state->max_rdata_len.
637 subreq = state->transport->read_send(state, state->ev, state->rdata,
639 state->transport->priv);
640 if (tevent_req_nomem(subreq, req)) {
643 tevent_req_set_callback(subreq, cli_api_pipe_read_done, req);
646 static void cli_api_pipe_read_done(struct tevent_req *subreq)
648 struct tevent_req *req = tevent_req_callback_data(
649 subreq, struct tevent_req);
650 struct cli_api_pipe_state *state = tevent_req_data(
651 req, struct cli_api_pipe_state);
655 status = state->transport->read_recv(subreq, &received);
657 if (!NT_STATUS_IS_OK(status)) {
658 tevent_req_nterror(req, status);
661 state->rdata_len = received;
662 tevent_req_done(req);
665 static NTSTATUS cli_api_pipe_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
666 uint8_t **prdata, uint32_t *prdata_len)
668 struct cli_api_pipe_state *state = tevent_req_data(
669 req, struct cli_api_pipe_state);
672 if (tevent_req_is_nterror(req, &status)) {
676 *prdata = talloc_move(mem_ctx, &state->rdata);
677 *prdata_len = state->rdata_len;
681 /****************************************************************************
682 Send data on an rpc pipe via trans. The data must be the last
683 pdu fragment of an NDR data stream.
685 Receive response data from an rpc pipe, which may be large...
687 Read the first fragment: unfortunately have to use SMBtrans for the first
688 bit, then SMBreadX for subsequent bits.
690 If first fragment received also wasn't the last fragment, continue
691 getting fragments until we _do_ receive the last fragment.
693 Request/Response PDU's look like the following...
695 |<------------------PDU len----------------------------------------------->|
696 |<-HDR_LEN-->|<--REQ LEN------>|.............|<-AUTH_HDRLEN->|<-AUTH_LEN-->|
698 +------------+-----------------+-------------+---------------+-------------+
699 | RPC HEADER | REQ/RESP HEADER | DATA ...... | AUTH_HDR | AUTH DATA |
700 +------------+-----------------+-------------+---------------+-------------+
702 Where the presence of the AUTH_HDR and AUTH DATA are dependent on the
703 signing & sealing being negotiated.
705 ****************************************************************************/
707 struct rpc_api_pipe_state {
708 struct tevent_context *ev;
709 struct rpc_pipe_client *cli;
710 uint8_t expected_pkt_type;
713 DATA_BLOB incoming_frag;
714 struct ncacn_packet *pkt;
718 size_t reply_pdu_offset;
722 static void rpc_api_pipe_trans_done(struct tevent_req *subreq);
723 static void rpc_api_pipe_got_pdu(struct tevent_req *subreq);
724 static void rpc_api_pipe_auth3_done(struct tevent_req *subreq);
726 static struct tevent_req *rpc_api_pipe_send(TALLOC_CTX *mem_ctx,
727 struct tevent_context *ev,
728 struct rpc_pipe_client *cli,
729 DATA_BLOB *data, /* Outgoing PDU */
730 uint8_t expected_pkt_type,
733 struct tevent_req *req, *subreq;
734 struct rpc_api_pipe_state *state;
735 uint16_t max_recv_frag;
738 req = tevent_req_create(mem_ctx, &state, struct rpc_api_pipe_state);
744 state->expected_pkt_type = expected_pkt_type;
745 state->call_id = call_id;
746 state->endianess = DCERPC_DREP_LE;
749 * Ensure we're not sending too much.
751 if (data->length > cli->max_xmit_frag) {
752 status = NT_STATUS_INVALID_PARAMETER;
756 DEBUG(5,("rpc_api_pipe: %s\n", rpccli_pipe_txt(talloc_tos(), cli)));
758 if (state->expected_pkt_type == DCERPC_PKT_AUTH3) {
759 subreq = rpc_write_send(state, ev, cli->transport,
760 data->data, data->length);
761 if (subreq == NULL) {
764 tevent_req_set_callback(subreq, rpc_api_pipe_auth3_done, req);
768 /* get the header first, then fetch the rest once we have
769 * the frag_length available */
770 max_recv_frag = RPC_HEADER_LEN;
772 subreq = cli_api_pipe_send(state, ev, cli->transport,
773 data->data, data->length, max_recv_frag);
774 if (subreq == NULL) {
777 tevent_req_set_callback(subreq, rpc_api_pipe_trans_done, req);
781 tevent_req_nterror(req, status);
782 return tevent_req_post(req, ev);
788 static void rpc_api_pipe_auth3_done(struct tevent_req *subreq)
790 struct tevent_req *req =
791 tevent_req_callback_data(subreq,
795 status = rpc_write_recv(subreq);
797 if (!NT_STATUS_IS_OK(status)) {
798 tevent_req_nterror(req, status);
802 tevent_req_done(req);
805 static void rpc_api_pipe_trans_done(struct tevent_req *subreq)
807 struct tevent_req *req = tevent_req_callback_data(
808 subreq, struct tevent_req);
809 struct rpc_api_pipe_state *state = tevent_req_data(
810 req, struct rpc_api_pipe_state);
812 uint8_t *rdata = NULL;
813 uint32_t rdata_len = 0;
815 status = cli_api_pipe_recv(subreq, state, &rdata, &rdata_len);
817 if (!NT_STATUS_IS_OK(status)) {
818 DEBUG(5, ("cli_api_pipe failed: %s\n", nt_errstr(status)));
819 tevent_req_nterror(req, status);
824 DEBUG(3,("rpc_api_pipe: %s failed to return data.\n",
825 rpccli_pipe_txt(talloc_tos(), state->cli)));
826 tevent_req_done(req);
831 * Move data on state->incoming_frag.
833 state->incoming_frag.data = talloc_move(state, &rdata);
834 state->incoming_frag.length = rdata_len;
835 if (!state->incoming_frag.data) {
836 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
840 /* Ensure we have enough data for a pdu. */
841 subreq = get_complete_frag_send(state, state->ev, state->cli,
842 &state->incoming_frag);
843 if (tevent_req_nomem(subreq, req)) {
846 tevent_req_set_callback(subreq, rpc_api_pipe_got_pdu, req);
849 static void rpc_api_pipe_got_pdu(struct tevent_req *subreq)
851 struct tevent_req *req = tevent_req_callback_data(
852 subreq, struct tevent_req);
853 struct rpc_api_pipe_state *state = tevent_req_data(
854 req, struct rpc_api_pipe_state);
856 DATA_BLOB rdata = data_blob_null;
858 status = get_complete_frag_recv(subreq);
860 if (!NT_STATUS_IS_OK(status)) {
861 DEBUG(5, ("get_complete_frag failed: %s\n",
863 tevent_req_nterror(req, status);
867 state->pkt = talloc(state, struct ncacn_packet);
869 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
873 status = dcerpc_pull_ncacn_packet(state->pkt,
874 &state->incoming_frag,
877 if (!NT_STATUS_IS_OK(status)) {
878 tevent_req_nterror(req, status);
882 if (state->incoming_frag.length != state->pkt->frag_length) {
883 DEBUG(5, ("Incorrect pdu length %u, expected %u\n",
884 (unsigned int)state->incoming_frag.length,
885 (unsigned int)state->pkt->frag_length));
886 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
890 status = cli_pipe_validate_current_pdu(state,
891 state->cli, state->pkt,
892 &state->incoming_frag,
893 state->expected_pkt_type,
898 DEBUG(10,("rpc_api_pipe: got frag len of %u at offset %u: %s\n",
899 (unsigned)state->incoming_frag.length,
900 (unsigned)state->reply_pdu_offset,
903 if (!NT_STATUS_IS_OK(status)) {
904 tevent_req_nterror(req, status);
908 if ((state->pkt->pfc_flags & DCERPC_PFC_FLAG_FIRST)
909 && (state->pkt->drep[0] != DCERPC_DREP_LE)) {
911 * Set the data type correctly for big-endian data on the
914 DEBUG(10,("rpc_api_pipe: On %s PDU data format is "
916 rpccli_pipe_txt(talloc_tos(), state->cli)));
917 state->endianess = 0x00; /* BIG ENDIAN */
920 * Check endianness on subsequent packets.
922 if (state->endianess != state->pkt->drep[0]) {
923 DEBUG(0,("rpc_api_pipe: Error : Endianness changed from %s to "
925 state->endianess?"little":"big",
926 state->pkt->drep[0]?"little":"big"));
927 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
931 /* Now copy the data portion out of the pdu into rbuf. */
932 if (state->reply_pdu.length < state->reply_pdu_offset + rdata.length) {
933 if (!data_blob_realloc(NULL, &state->reply_pdu,
934 state->reply_pdu_offset + rdata.length)) {
935 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
940 memcpy(state->reply_pdu.data + state->reply_pdu_offset,
941 rdata.data, rdata.length);
942 state->reply_pdu_offset += rdata.length;
944 /* reset state->incoming_frag, there is no need to free it,
945 * it will be reallocated to the right size the next time
947 state->incoming_frag.length = 0;
949 if (state->pkt->pfc_flags & DCERPC_PFC_FLAG_LAST) {
950 /* make sure the pdu length is right now that we
951 * have all the data available (alloc hint may
952 * have allocated more than was actually used) */
953 state->reply_pdu.length = state->reply_pdu_offset;
954 DEBUG(10,("rpc_api_pipe: %s returned %u bytes.\n",
955 rpccli_pipe_txt(talloc_tos(), state->cli),
956 (unsigned)state->reply_pdu.length));
957 tevent_req_done(req);
961 subreq = get_complete_frag_send(state, state->ev, state->cli,
962 &state->incoming_frag);
963 if (tevent_req_nomem(subreq, req)) {
966 tevent_req_set_callback(subreq, rpc_api_pipe_got_pdu, req);
969 static NTSTATUS rpc_api_pipe_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
970 struct ncacn_packet **pkt,
971 DATA_BLOB *reply_pdu)
973 struct rpc_api_pipe_state *state = tevent_req_data(
974 req, struct rpc_api_pipe_state);
977 if (tevent_req_is_nterror(req, &status)) {
981 /* return data to caller and assign it ownership of memory */
983 reply_pdu->data = talloc_move(mem_ctx, &state->reply_pdu.data);
984 reply_pdu->length = state->reply_pdu.length;
985 state->reply_pdu.length = 0;
987 data_blob_free(&state->reply_pdu);
991 *pkt = talloc_steal(mem_ctx, state->pkt);
997 /*******************************************************************
998 Creates NTLMSSP auth bind.
999 ********************************************************************/
1001 static NTSTATUS create_generic_auth_rpc_bind_req(struct rpc_pipe_client *cli,
1002 TALLOC_CTX *mem_ctx,
1003 DATA_BLOB *auth_token,
1004 bool *client_hdr_signing)
1006 struct gensec_security *gensec_security;
1007 DATA_BLOB null_blob = data_blob_null;
1010 gensec_security = talloc_get_type_abort(cli->auth->auth_ctx,
1011 struct gensec_security);
1013 DEBUG(5, ("create_generic_auth_rpc_bind_req: generate first token\n"));
1014 status = gensec_update(gensec_security, mem_ctx, null_blob, auth_token);
1016 if (!NT_STATUS_IS_OK(status) &&
1017 !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED))
1022 if (client_hdr_signing == NULL) {
1026 if (cli->auth->auth_level < DCERPC_AUTH_LEVEL_INTEGRITY) {
1027 *client_hdr_signing = false;
1031 *client_hdr_signing = gensec_have_feature(gensec_security,
1032 GENSEC_FEATURE_SIGN_PKT_HEADER);
1037 /*******************************************************************
1038 Creates the internals of a DCE/RPC bind request or alter context PDU.
1039 ********************************************************************/
1041 static NTSTATUS create_bind_or_alt_ctx_internal(TALLOC_CTX *mem_ctx,
1042 enum dcerpc_pkt_type ptype,
1044 const struct ndr_syntax_id *abstract,
1045 const struct ndr_syntax_id *transfer,
1046 const DATA_BLOB *auth_info,
1047 bool client_hdr_signing,
1050 uint16 auth_len = auth_info->length;
1052 union dcerpc_payload u;
1053 struct dcerpc_ctx_list ctx_list;
1054 uint8_t pfc_flags = DCERPC_PFC_FLAG_FIRST | DCERPC_PFC_FLAG_LAST;
1057 auth_len -= DCERPC_AUTH_TRAILER_LENGTH;
1060 if (client_hdr_signing) {
1061 pfc_flags |= DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN;
1064 ctx_list.context_id = 0;
1065 ctx_list.num_transfer_syntaxes = 1;
1066 ctx_list.abstract_syntax = *abstract;
1067 ctx_list.transfer_syntaxes = (struct ndr_syntax_id *)discard_const(transfer);
1069 u.bind.max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
1070 u.bind.max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
1071 u.bind.assoc_group_id = 0x0;
1072 u.bind.num_contexts = 1;
1073 u.bind.ctx_list = &ctx_list;
1074 u.bind.auth_info = *auth_info;
1076 status = dcerpc_push_ncacn_packet(mem_ctx,
1082 if (!NT_STATUS_IS_OK(status)) {
1083 DEBUG(0, ("Failed to marshall bind/alter ncacn_packet.\n"));
1087 return NT_STATUS_OK;
1090 /*******************************************************************
1091 Creates a DCE/RPC bind request.
1092 ********************************************************************/
1094 static NTSTATUS create_rpc_bind_req(TALLOC_CTX *mem_ctx,
1095 struct rpc_pipe_client *cli,
1096 struct pipe_auth_data *auth,
1098 const struct ndr_syntax_id *abstract,
1099 const struct ndr_syntax_id *transfer,
1102 DATA_BLOB auth_token = data_blob_null;
1103 DATA_BLOB auth_info = data_blob_null;
1104 NTSTATUS ret = NT_STATUS_OK;
1106 switch (auth->auth_type) {
1107 case DCERPC_AUTH_TYPE_NONE:
1110 case DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM:
1111 auth_token = data_blob_talloc(mem_ctx,
1112 "NCALRPC_AUTH_TOKEN",
1117 ret = create_generic_auth_rpc_bind_req(cli, mem_ctx,
1119 &auth->client_hdr_signing);
1121 if (!NT_STATUS_IS_OK(ret) &&
1122 !NT_STATUS_EQUAL(ret, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1128 if (auth_token.length != 0) {
1129 ret = dcerpc_push_dcerpc_auth(cli,
1132 0, /* auth_pad_length */
1133 1, /* auth_context_id */
1136 if (!NT_STATUS_IS_OK(ret)) {
1139 data_blob_free(&auth_token);
1142 ret = create_bind_or_alt_ctx_internal(mem_ctx,
1148 auth->client_hdr_signing,
1153 /*******************************************************************
1155 Does an rpc request on a pipe. Incoming data is NDR encoded in in_data.
1156 Reply is NDR encoded in out_data. Splits the data stream into RPC PDU's
1157 and deals with signing/sealing details.
1158 ********************************************************************/
1160 struct rpc_api_pipe_req_state {
1161 struct tevent_context *ev;
1162 struct rpc_pipe_client *cli;
1165 const DATA_BLOB *req_data;
1166 uint32_t req_data_sent;
1167 DATA_BLOB req_trailer;
1168 uint32_t req_trailer_sent;
1169 bool verify_bitmask1;
1170 bool verify_pcontext;
1172 DATA_BLOB reply_pdu;
1175 static void rpc_api_pipe_req_write_done(struct tevent_req *subreq);
1176 static void rpc_api_pipe_req_done(struct tevent_req *subreq);
1177 static NTSTATUS prepare_verification_trailer(struct rpc_api_pipe_req_state *state);
1178 static NTSTATUS prepare_next_frag(struct rpc_api_pipe_req_state *state,
1179 bool *is_last_frag);
1181 static struct tevent_req *rpc_api_pipe_req_send(TALLOC_CTX *mem_ctx,
1182 struct tevent_context *ev,
1183 struct rpc_pipe_client *cli,
1185 const DATA_BLOB *req_data)
1187 struct tevent_req *req, *subreq;
1188 struct rpc_api_pipe_req_state *state;
1192 req = tevent_req_create(mem_ctx, &state,
1193 struct rpc_api_pipe_req_state);
1199 state->op_num = op_num;
1200 state->req_data = req_data;
1201 state->req_data_sent = 0;
1202 state->call_id = get_rpc_call_id();
1203 state->reply_pdu = data_blob_null;
1204 state->rpc_out = data_blob_null;
1206 if (cli->max_xmit_frag < DCERPC_REQUEST_LENGTH
1207 + RPC_MAX_SIGN_SIZE) {
1208 /* Server is screwed up ! */
1209 status = NT_STATUS_INVALID_PARAMETER;
1213 status = prepare_verification_trailer(state);
1214 if (!NT_STATUS_IS_OK(status)) {
1218 status = prepare_next_frag(state, &is_last_frag);
1219 if (!NT_STATUS_IS_OK(status)) {
1224 subreq = rpc_api_pipe_send(state, ev, state->cli,
1226 DCERPC_PKT_RESPONSE,
1228 if (subreq == NULL) {
1231 tevent_req_set_callback(subreq, rpc_api_pipe_req_done, req);
1233 subreq = rpc_write_send(state, ev, cli->transport,
1234 state->rpc_out.data,
1235 state->rpc_out.length);
1236 if (subreq == NULL) {
1239 tevent_req_set_callback(subreq, rpc_api_pipe_req_write_done,
1245 tevent_req_nterror(req, status);
1246 return tevent_req_post(req, ev);
1252 static NTSTATUS prepare_verification_trailer(struct rpc_api_pipe_req_state *state)
1254 struct pipe_auth_data *a = state->cli->auth;
1255 struct dcerpc_sec_verification_trailer *t;
1256 struct dcerpc_sec_vt *c = NULL;
1257 struct ndr_push *ndr = NULL;
1258 enum ndr_err_code ndr_err;
1263 return NT_STATUS_OK;
1266 if (a->auth_level < DCERPC_AUTH_LEVEL_INTEGRITY) {
1267 return NT_STATUS_OK;
1270 t = talloc_zero(state, struct dcerpc_sec_verification_trailer);
1272 return NT_STATUS_NO_MEMORY;
1275 if (!a->verified_bitmask1) {
1276 t->commands = talloc_realloc(t, t->commands,
1277 struct dcerpc_sec_vt,
1278 t->count.count + 1);
1279 if (t->commands == NULL) {
1280 return NT_STATUS_NO_MEMORY;
1282 c = &t->commands[t->count.count++];
1285 c->command = DCERPC_SEC_VT_COMMAND_BITMASK1;
1286 if (a->client_hdr_signing) {
1287 c->u.bitmask1 = DCERPC_SEC_VT_CLIENT_SUPPORTS_HEADER_SIGNING;
1289 state->verify_bitmask1 = true;
1292 if (!state->cli->verified_pcontext) {
1293 t->commands = talloc_realloc(t, t->commands,
1294 struct dcerpc_sec_vt,
1295 t->count.count + 1);
1296 if (t->commands == NULL) {
1297 return NT_STATUS_NO_MEMORY;
1299 c = &t->commands[t->count.count++];
1302 c->command = DCERPC_SEC_VT_COMMAND_PCONTEXT;
1303 c->u.pcontext.abstract_syntax = state->cli->abstract_syntax;
1304 c->u.pcontext.transfer_syntax = state->cli->transfer_syntax;
1306 state->verify_pcontext = true;
1309 if (!a->hdr_signing) {
1310 t->commands = talloc_realloc(t, t->commands,
1311 struct dcerpc_sec_vt,
1312 t->count.count + 1);
1313 if (t->commands == NULL) {
1314 return NT_STATUS_NO_MEMORY;
1316 c = &t->commands[t->count.count++];
1319 c->command = DCERPC_SEC_VT_COMMAND_HEADER2;
1320 c->u.header2.ptype = DCERPC_PKT_REQUEST;
1321 c->u.header2.drep[0] = DCERPC_DREP_LE;
1322 c->u.header2.drep[1] = 0;
1323 c->u.header2.drep[2] = 0;
1324 c->u.header2.drep[3] = 0;
1325 c->u.header2.call_id = state->call_id;
1326 c->u.header2.context_id = 0;
1327 c->u.header2.opnum = state->op_num;
1330 if (t->count.count == 0) {
1332 return NT_STATUS_OK;
1335 c = &t->commands[t->count.count - 1];
1336 c->command |= DCERPC_SEC_VT_COMMAND_END;
1338 if (DEBUGLEVEL >= 10) {
1339 NDR_PRINT_DEBUG(dcerpc_sec_verification_trailer, t);
1342 ndr = ndr_push_init_ctx(state);
1344 return NT_STATUS_NO_MEMORY;
1347 ndr_err = ndr_push_dcerpc_sec_verification_trailer(ndr,
1348 NDR_SCALARS | NDR_BUFFERS,
1350 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1351 return ndr_map_error2ntstatus(ndr_err);
1353 state->req_trailer = ndr_push_blob(ndr);
1355 align = state->req_data->length & 0x3;
1362 const uint8_t zeros[4] = { 0, };
1364 ok = data_blob_append(ndr, &state->req_trailer, zeros, pad);
1366 return NT_STATUS_NO_MEMORY;
1369 /* move the padding to the start */
1370 p = state->req_trailer.data;
1371 memmove(p + pad, p, state->req_trailer.length - pad);
1375 return NT_STATUS_OK;
1378 static NTSTATUS prepare_next_frag(struct rpc_api_pipe_req_state *state,
1386 size_t data_thistime;
1387 size_t trailer_left;
1388 size_t trailer_thistime = 0;
1390 size_t total_thistime;
1393 union dcerpc_payload u;
1395 data_left = state->req_data->length - state->req_data_sent;
1396 trailer_left = state->req_trailer.length - state->req_trailer_sent;
1397 total_left = data_left + trailer_left;
1398 if ((total_left < data_left) || (total_left < trailer_left)) {
1402 return NT_STATUS_INVALID_PARAMETER_MIX;
1405 status = dcerpc_guess_sizes(state->cli->auth,
1406 DCERPC_REQUEST_LENGTH, total_left,
1407 state->cli->max_xmit_frag,
1408 CLIENT_NDR_PADDING_SIZE,
1410 &frag_len, &auth_len, &pad_len);
1411 if (!NT_STATUS_IS_OK(status)) {
1415 if (state->req_data_sent == 0) {
1416 flags = DCERPC_PFC_FLAG_FIRST;
1419 if (total_thistime == total_left) {
1420 flags |= DCERPC_PFC_FLAG_LAST;
1423 data_thistime = MIN(total_thistime, data_left);
1424 if (data_thistime < total_thistime) {
1425 trailer_thistime = total_thistime - data_thistime;
1428 data_blob_free(&state->rpc_out);
1430 ZERO_STRUCT(u.request);
1432 u.request.alloc_hint = total_left;
1433 u.request.context_id = 0;
1434 u.request.opnum = state->op_num;
1436 status = dcerpc_push_ncacn_packet(state,
1443 if (!NT_STATUS_IS_OK(status)) {
1447 /* explicitly set frag_len here as dcerpc_push_ncacn_packet() can't
1448 * compute it right for requests because the auth trailer is missing
1450 dcerpc_set_frag_length(&state->rpc_out, frag_len);
1452 if (data_thistime > 0) {
1453 /* Copy in the data. */
1454 ok = data_blob_append(NULL, &state->rpc_out,
1455 state->req_data->data + state->req_data_sent,
1458 return NT_STATUS_NO_MEMORY;
1460 state->req_data_sent += data_thistime;
1463 if (trailer_thistime > 0) {
1464 /* Copy in the verification trailer. */
1465 ok = data_blob_append(NULL, &state->rpc_out,
1466 state->req_trailer.data + state->req_trailer_sent,
1469 return NT_STATUS_NO_MEMORY;
1471 state->req_trailer_sent += trailer_thistime;
1474 switch (state->cli->auth->auth_level) {
1475 case DCERPC_AUTH_LEVEL_NONE:
1476 case DCERPC_AUTH_LEVEL_CONNECT:
1477 case DCERPC_AUTH_LEVEL_PACKET:
1479 case DCERPC_AUTH_LEVEL_INTEGRITY:
1480 case DCERPC_AUTH_LEVEL_PRIVACY:
1481 status = dcerpc_add_auth_footer(state->cli->auth, pad_len,
1483 if (!NT_STATUS_IS_OK(status)) {
1488 return NT_STATUS_INVALID_PARAMETER;
1491 *is_last_frag = ((flags & DCERPC_PFC_FLAG_LAST) != 0);
1496 static void rpc_api_pipe_req_write_done(struct tevent_req *subreq)
1498 struct tevent_req *req = tevent_req_callback_data(
1499 subreq, struct tevent_req);
1500 struct rpc_api_pipe_req_state *state = tevent_req_data(
1501 req, struct rpc_api_pipe_req_state);
1505 status = rpc_write_recv(subreq);
1506 TALLOC_FREE(subreq);
1507 if (!NT_STATUS_IS_OK(status)) {
1508 tevent_req_nterror(req, status);
1512 status = prepare_next_frag(state, &is_last_frag);
1513 if (!NT_STATUS_IS_OK(status)) {
1514 tevent_req_nterror(req, status);
1519 subreq = rpc_api_pipe_send(state, state->ev, state->cli,
1521 DCERPC_PKT_RESPONSE,
1523 if (tevent_req_nomem(subreq, req)) {
1526 tevent_req_set_callback(subreq, rpc_api_pipe_req_done, req);
1528 subreq = rpc_write_send(state, state->ev,
1529 state->cli->transport,
1530 state->rpc_out.data,
1531 state->rpc_out.length);
1532 if (tevent_req_nomem(subreq, req)) {
1535 tevent_req_set_callback(subreq, rpc_api_pipe_req_write_done,
1540 static void rpc_api_pipe_req_done(struct tevent_req *subreq)
1542 struct tevent_req *req = tevent_req_callback_data(
1543 subreq, struct tevent_req);
1544 struct rpc_api_pipe_req_state *state = tevent_req_data(
1545 req, struct rpc_api_pipe_req_state);
1548 status = rpc_api_pipe_recv(subreq, state, NULL, &state->reply_pdu);
1549 TALLOC_FREE(subreq);
1550 if (!NT_STATUS_IS_OK(status)) {
1551 tevent_req_nterror(req, status);
1555 if (state->cli->auth == NULL) {
1556 tevent_req_done(req);
1560 if (state->verify_bitmask1) {
1561 state->cli->auth->verified_bitmask1 = true;
1564 if (state->verify_pcontext) {
1565 state->cli->verified_pcontext = true;
1568 tevent_req_done(req);
1571 static NTSTATUS rpc_api_pipe_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1572 DATA_BLOB *reply_pdu)
1574 struct rpc_api_pipe_req_state *state = tevent_req_data(
1575 req, struct rpc_api_pipe_req_state);
1578 if (tevent_req_is_nterror(req, &status)) {
1580 * We always have to initialize to reply pdu, even if there is
1581 * none. The rpccli_* caller routines expect this.
1583 *reply_pdu = data_blob_null;
1587 /* return data to caller and assign it ownership of memory */
1588 reply_pdu->data = talloc_move(mem_ctx, &state->reply_pdu.data);
1589 reply_pdu->length = state->reply_pdu.length;
1590 state->reply_pdu.length = 0;
1592 return NT_STATUS_OK;
1595 /****************************************************************************
1596 Check the rpc bind acknowledge response.
1597 ****************************************************************************/
1599 static bool check_bind_response(const struct dcerpc_bind_ack *r,
1600 const struct ndr_syntax_id *transfer)
1602 struct dcerpc_ack_ctx ctx;
1604 if (r->secondary_address_size == 0) {
1605 DEBUG(4,("Ignoring length check -- ASU bug (server didn't fill in the pipe name correctly)"));
1608 if (r->num_results < 1 || !r->ctx_list) {
1612 ctx = r->ctx_list[0];
1614 /* check the transfer syntax */
1615 if ((ctx.syntax.if_version != transfer->if_version) ||
1616 (memcmp(&ctx.syntax.uuid, &transfer->uuid, sizeof(transfer->uuid)) !=0)) {
1617 DEBUG(2,("bind_rpc_pipe: transfer syntax differs\n"));
1621 if (r->num_results != 0x1 || ctx.result != 0) {
1622 DEBUG(2,("bind_rpc_pipe: bind denied results: %d reason: %x\n",
1623 r->num_results, ctx.reason.value));
1626 DEBUG(5,("check_bind_response: accepted!\n"));
1630 /*******************************************************************
1631 Creates a DCE/RPC bind authentication response.
1632 This is the packet that is sent back to the server once we
1633 have received a BIND-ACK, to finish the third leg of
1634 the authentication handshake.
1635 ********************************************************************/
1637 static NTSTATUS create_rpc_bind_auth3(TALLOC_CTX *mem_ctx,
1638 struct rpc_pipe_client *cli,
1640 enum dcerpc_AuthType auth_type,
1641 enum dcerpc_AuthLevel auth_level,
1642 DATA_BLOB *pauth_blob,
1646 union dcerpc_payload u;
1650 status = dcerpc_push_dcerpc_auth(mem_ctx,
1653 0, /* auth_pad_length */
1654 1, /* auth_context_id */
1656 &u.auth3.auth_info);
1657 if (!NT_STATUS_IS_OK(status)) {
1661 status = dcerpc_push_ncacn_packet(mem_ctx,
1663 DCERPC_PFC_FLAG_FIRST |
1664 DCERPC_PFC_FLAG_LAST,
1669 data_blob_free(&u.auth3.auth_info);
1670 if (!NT_STATUS_IS_OK(status)) {
1671 DEBUG(0,("create_bind_or_alt_ctx_internal: failed to marshall RPC_HDR_RB.\n"));
1675 return NT_STATUS_OK;
1678 /*******************************************************************
1679 Creates a DCE/RPC bind alter context authentication request which
1680 may contain a spnego auth blobl
1681 ********************************************************************/
1683 static NTSTATUS create_rpc_alter_context(TALLOC_CTX *mem_ctx,
1684 enum dcerpc_AuthType auth_type,
1685 enum dcerpc_AuthLevel auth_level,
1687 const struct ndr_syntax_id *abstract,
1688 const struct ndr_syntax_id *transfer,
1689 const DATA_BLOB *pauth_blob, /* spnego auth blob already created. */
1692 DATA_BLOB auth_info;
1695 status = dcerpc_push_dcerpc_auth(mem_ctx,
1698 0, /* auth_pad_length */
1699 1, /* auth_context_id */
1702 if (!NT_STATUS_IS_OK(status)) {
1706 status = create_bind_or_alt_ctx_internal(mem_ctx,
1712 false, /* client_hdr_signing */
1714 data_blob_free(&auth_info);
1718 /****************************************************************************
1720 ****************************************************************************/
1722 struct rpc_pipe_bind_state {
1723 struct tevent_context *ev;
1724 struct rpc_pipe_client *cli;
1727 uint32_t rpc_call_id;
1730 static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq);
1731 static NTSTATUS rpc_bind_next_send(struct tevent_req *req,
1732 struct rpc_pipe_bind_state *state,
1733 DATA_BLOB *credentials);
1734 static NTSTATUS rpc_bind_finish_send(struct tevent_req *req,
1735 struct rpc_pipe_bind_state *state,
1736 DATA_BLOB *credentials);
1738 struct tevent_req *rpc_pipe_bind_send(TALLOC_CTX *mem_ctx,
1739 struct tevent_context *ev,
1740 struct rpc_pipe_client *cli,
1741 struct pipe_auth_data *auth)
1743 struct tevent_req *req, *subreq;
1744 struct rpc_pipe_bind_state *state;
1747 req = tevent_req_create(mem_ctx, &state, struct rpc_pipe_bind_state);
1752 DEBUG(5,("Bind RPC Pipe: %s auth_type %u, auth_level %u\n",
1753 rpccli_pipe_txt(talloc_tos(), cli),
1754 (unsigned int)auth->auth_type,
1755 (unsigned int)auth->auth_level ));
1759 state->rpc_call_id = get_rpc_call_id();
1761 cli->auth = talloc_move(cli, &auth);
1763 /* Marshall the outgoing data. */
1764 status = create_rpc_bind_req(state, cli,
1767 &cli->abstract_syntax,
1768 &cli->transfer_syntax,
1771 if (!NT_STATUS_IS_OK(status) &&
1772 !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1776 subreq = rpc_api_pipe_send(state, ev, cli, &state->rpc_out,
1777 DCERPC_PKT_BIND_ACK, state->rpc_call_id);
1778 if (subreq == NULL) {
1781 tevent_req_set_callback(subreq, rpc_pipe_bind_step_one_done, req);
1785 tevent_req_nterror(req, status);
1786 return tevent_req_post(req, ev);
1792 static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq)
1794 struct tevent_req *req = tevent_req_callback_data(
1795 subreq, struct tevent_req);
1796 struct rpc_pipe_bind_state *state = tevent_req_data(
1797 req, struct rpc_pipe_bind_state);
1798 struct pipe_auth_data *pauth = state->cli->auth;
1799 struct gensec_security *gensec_security;
1800 struct ncacn_packet *pkt = NULL;
1801 struct dcerpc_auth auth;
1802 DATA_BLOB auth_token = data_blob_null;
1805 status = rpc_api_pipe_recv(subreq, talloc_tos(), &pkt, NULL);
1806 TALLOC_FREE(subreq);
1807 if (!NT_STATUS_IS_OK(status)) {
1808 DEBUG(3, ("rpc_pipe_bind: %s bind request returned %s\n",
1809 rpccli_pipe_txt(talloc_tos(), state->cli),
1810 nt_errstr(status)));
1811 tevent_req_nterror(req, status);
1816 tevent_req_done(req);
1820 if (!check_bind_response(&pkt->u.bind_ack, &state->cli->transfer_syntax)) {
1821 DEBUG(2, ("rpc_pipe_bind: check_bind_response failed.\n"));
1822 tevent_req_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
1826 state->cli->max_xmit_frag = pkt->u.bind_ack.max_xmit_frag;
1827 state->cli->max_recv_frag = pkt->u.bind_ack.max_recv_frag;
1829 switch(pauth->auth_type) {
1831 case DCERPC_AUTH_TYPE_NONE:
1832 case DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM:
1833 /* Bind complete. */
1834 tevent_req_done(req);
1838 /* Paranoid lenght checks */
1839 if (pkt->frag_length < DCERPC_AUTH_TRAILER_LENGTH
1840 + pkt->auth_length) {
1841 tevent_req_nterror(req,
1842 NT_STATUS_INFO_LENGTH_MISMATCH);
1845 /* get auth credentials */
1846 status = dcerpc_pull_dcerpc_auth(talloc_tos(),
1847 &pkt->u.bind_ack.auth_info,
1849 if (!NT_STATUS_IS_OK(status)) {
1850 DEBUG(0, ("Failed to pull dcerpc auth: %s.\n",
1851 nt_errstr(status)));
1852 tevent_req_nterror(req, status);
1859 * For authenticated binds we may need to do 3 or 4 leg binds.
1862 switch(pauth->auth_type) {
1864 case DCERPC_AUTH_TYPE_NONE:
1865 case DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM:
1866 /* Bind complete. */
1867 tevent_req_done(req);
1871 gensec_security = talloc_get_type_abort(pauth->auth_ctx,
1872 struct gensec_security);
1874 if (pkt->pfc_flags & DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN) {
1875 if (pauth->client_hdr_signing) {
1876 pauth->hdr_signing = true;
1877 gensec_want_feature(gensec_security,
1878 GENSEC_FEATURE_SIGN_PKT_HEADER);
1882 status = gensec_update(gensec_security, state,
1883 auth.credentials, &auth_token);
1884 if (NT_STATUS_EQUAL(status,
1885 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1886 status = rpc_bind_next_send(req, state,
1888 } else if (NT_STATUS_IS_OK(status)) {
1889 if (auth_token.length == 0) {
1890 /* Bind complete. */
1891 tevent_req_done(req);
1894 status = rpc_bind_finish_send(req, state,
1900 if (!NT_STATUS_IS_OK(status)) {
1901 tevent_req_nterror(req, status);
1906 static NTSTATUS rpc_bind_next_send(struct tevent_req *req,
1907 struct rpc_pipe_bind_state *state,
1908 DATA_BLOB *auth_token)
1910 struct pipe_auth_data *auth = state->cli->auth;
1911 struct tevent_req *subreq;
1914 /* Now prepare the alter context pdu. */
1915 data_blob_free(&state->rpc_out);
1917 status = create_rpc_alter_context(state,
1921 &state->cli->abstract_syntax,
1922 &state->cli->transfer_syntax,
1925 if (!NT_STATUS_IS_OK(status)) {
1929 subreq = rpc_api_pipe_send(state, state->ev, state->cli,
1930 &state->rpc_out, DCERPC_PKT_ALTER_RESP,
1931 state->rpc_call_id);
1932 if (subreq == NULL) {
1933 return NT_STATUS_NO_MEMORY;
1935 tevent_req_set_callback(subreq, rpc_pipe_bind_step_one_done, req);
1936 return NT_STATUS_OK;
1939 static NTSTATUS rpc_bind_finish_send(struct tevent_req *req,
1940 struct rpc_pipe_bind_state *state,
1941 DATA_BLOB *auth_token)
1943 struct pipe_auth_data *auth = state->cli->auth;
1944 struct tevent_req *subreq;
1947 state->auth3 = true;
1949 /* Now prepare the auth3 context pdu. */
1950 data_blob_free(&state->rpc_out);
1952 status = create_rpc_bind_auth3(state, state->cli,
1958 if (!NT_STATUS_IS_OK(status)) {
1962 subreq = rpc_api_pipe_send(state, state->ev, state->cli,
1963 &state->rpc_out, DCERPC_PKT_AUTH3,
1964 state->rpc_call_id);
1965 if (subreq == NULL) {
1966 return NT_STATUS_NO_MEMORY;
1968 tevent_req_set_callback(subreq, rpc_pipe_bind_step_one_done, req);
1969 return NT_STATUS_OK;
1972 NTSTATUS rpc_pipe_bind_recv(struct tevent_req *req)
1974 return tevent_req_simple_recv_ntstatus(req);
1977 NTSTATUS rpc_pipe_bind(struct rpc_pipe_client *cli,
1978 struct pipe_auth_data *auth)
1980 TALLOC_CTX *frame = talloc_stackframe();
1981 struct tevent_context *ev;
1982 struct tevent_req *req;
1983 NTSTATUS status = NT_STATUS_OK;
1985 ev = samba_tevent_context_init(frame);
1987 status = NT_STATUS_NO_MEMORY;
1991 req = rpc_pipe_bind_send(frame, ev, cli, auth);
1993 status = NT_STATUS_NO_MEMORY;
1997 if (!tevent_req_poll(req, ev)) {
1998 status = map_nt_error_from_unix(errno);
2002 status = rpc_pipe_bind_recv(req);
2008 #define RPCCLI_DEFAULT_TIMEOUT 10000 /* 10 seconds. */
2010 unsigned int rpccli_set_timeout(struct rpc_pipe_client *rpc_cli,
2011 unsigned int timeout)
2015 if (rpc_cli->transport == NULL) {
2016 return RPCCLI_DEFAULT_TIMEOUT;
2019 if (rpc_cli->transport->set_timeout == NULL) {
2020 return RPCCLI_DEFAULT_TIMEOUT;
2023 old = rpc_cli->transport->set_timeout(rpc_cli->transport->priv, timeout);
2025 return RPCCLI_DEFAULT_TIMEOUT;
2031 bool rpccli_is_connected(struct rpc_pipe_client *rpc_cli)
2033 if (rpc_cli == NULL) {
2037 if (rpc_cli->transport == NULL) {
2041 return rpc_cli->transport->is_connected(rpc_cli->transport->priv);
2044 struct rpccli_bh_state {
2045 struct rpc_pipe_client *rpc_cli;
2048 static bool rpccli_bh_is_connected(struct dcerpc_binding_handle *h)
2050 struct rpccli_bh_state *hs = dcerpc_binding_handle_data(h,
2051 struct rpccli_bh_state);
2053 return rpccli_is_connected(hs->rpc_cli);
2056 static uint32_t rpccli_bh_set_timeout(struct dcerpc_binding_handle *h,
2059 struct rpccli_bh_state *hs = dcerpc_binding_handle_data(h,
2060 struct rpccli_bh_state);
2062 return rpccli_set_timeout(hs->rpc_cli, timeout);
2065 static void rpccli_bh_auth_info(struct dcerpc_binding_handle *h,
2066 enum dcerpc_AuthType *auth_type,
2067 enum dcerpc_AuthLevel *auth_level)
2069 struct rpccli_bh_state *hs = dcerpc_binding_handle_data(h,
2070 struct rpccli_bh_state);
2072 if (hs->rpc_cli == NULL) {
2076 if (hs->rpc_cli->auth == NULL) {
2080 *auth_type = hs->rpc_cli->auth->auth_type;
2081 *auth_level = hs->rpc_cli->auth->auth_level;
2084 struct rpccli_bh_raw_call_state {
2090 static void rpccli_bh_raw_call_done(struct tevent_req *subreq);
2092 static struct tevent_req *rpccli_bh_raw_call_send(TALLOC_CTX *mem_ctx,
2093 struct tevent_context *ev,
2094 struct dcerpc_binding_handle *h,
2095 const struct GUID *object,
2098 const uint8_t *in_data,
2101 struct rpccli_bh_state *hs = dcerpc_binding_handle_data(h,
2102 struct rpccli_bh_state);
2103 struct tevent_req *req;
2104 struct rpccli_bh_raw_call_state *state;
2106 struct tevent_req *subreq;
2108 req = tevent_req_create(mem_ctx, &state,
2109 struct rpccli_bh_raw_call_state);
2113 state->in_data.data = discard_const_p(uint8_t, in_data);
2114 state->in_data.length = in_length;
2116 ok = rpccli_bh_is_connected(h);
2118 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
2119 return tevent_req_post(req, ev);
2122 subreq = rpc_api_pipe_req_send(state, ev, hs->rpc_cli,
2123 opnum, &state->in_data);
2124 if (tevent_req_nomem(subreq, req)) {
2125 return tevent_req_post(req, ev);
2127 tevent_req_set_callback(subreq, rpccli_bh_raw_call_done, req);
2132 static void rpccli_bh_raw_call_done(struct tevent_req *subreq)
2134 struct tevent_req *req =
2135 tevent_req_callback_data(subreq,
2137 struct rpccli_bh_raw_call_state *state =
2138 tevent_req_data(req,
2139 struct rpccli_bh_raw_call_state);
2142 state->out_flags = 0;
2144 /* TODO: support bigendian responses */
2146 status = rpc_api_pipe_req_recv(subreq, state, &state->out_data);
2147 TALLOC_FREE(subreq);
2148 if (!NT_STATUS_IS_OK(status)) {
2149 tevent_req_nterror(req, status);
2153 tevent_req_done(req);
2156 static NTSTATUS rpccli_bh_raw_call_recv(struct tevent_req *req,
2157 TALLOC_CTX *mem_ctx,
2160 uint32_t *out_flags)
2162 struct rpccli_bh_raw_call_state *state =
2163 tevent_req_data(req,
2164 struct rpccli_bh_raw_call_state);
2167 if (tevent_req_is_nterror(req, &status)) {
2168 tevent_req_received(req);
2172 *out_data = talloc_move(mem_ctx, &state->out_data.data);
2173 *out_length = state->out_data.length;
2174 *out_flags = state->out_flags;
2175 tevent_req_received(req);
2176 return NT_STATUS_OK;
2179 struct rpccli_bh_disconnect_state {
2183 static struct tevent_req *rpccli_bh_disconnect_send(TALLOC_CTX *mem_ctx,
2184 struct tevent_context *ev,
2185 struct dcerpc_binding_handle *h)
2187 struct rpccli_bh_state *hs = dcerpc_binding_handle_data(h,
2188 struct rpccli_bh_state);
2189 struct tevent_req *req;
2190 struct rpccli_bh_disconnect_state *state;
2193 req = tevent_req_create(mem_ctx, &state,
2194 struct rpccli_bh_disconnect_state);
2199 ok = rpccli_bh_is_connected(h);
2201 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
2202 return tevent_req_post(req, ev);
2206 * TODO: do a real async disconnect ...
2208 * For now the caller needs to free rpc_cli
2212 tevent_req_done(req);
2213 return tevent_req_post(req, ev);
2216 static NTSTATUS rpccli_bh_disconnect_recv(struct tevent_req *req)
2220 if (tevent_req_is_nterror(req, &status)) {
2221 tevent_req_received(req);
2225 tevent_req_received(req);
2226 return NT_STATUS_OK;
2229 static bool rpccli_bh_ref_alloc(struct dcerpc_binding_handle *h)
2234 static void rpccli_bh_do_ndr_print(struct dcerpc_binding_handle *h,
2236 const void *_struct_ptr,
2237 const struct ndr_interface_call *call)
2239 void *struct_ptr = discard_const(_struct_ptr);
2241 if (DEBUGLEVEL < 10) {
2245 if (ndr_flags & NDR_IN) {
2246 ndr_print_function_debug(call->ndr_print,
2251 if (ndr_flags & NDR_OUT) {
2252 ndr_print_function_debug(call->ndr_print,
2259 static const struct dcerpc_binding_handle_ops rpccli_bh_ops = {
2261 .is_connected = rpccli_bh_is_connected,
2262 .set_timeout = rpccli_bh_set_timeout,
2263 .auth_info = rpccli_bh_auth_info,
2264 .raw_call_send = rpccli_bh_raw_call_send,
2265 .raw_call_recv = rpccli_bh_raw_call_recv,
2266 .disconnect_send = rpccli_bh_disconnect_send,
2267 .disconnect_recv = rpccli_bh_disconnect_recv,
2269 .ref_alloc = rpccli_bh_ref_alloc,
2270 .do_ndr_print = rpccli_bh_do_ndr_print,
2273 /* initialise a rpc_pipe_client binding handle */
2274 struct dcerpc_binding_handle *rpccli_bh_create(struct rpc_pipe_client *c,
2275 const struct GUID *object,
2276 const struct ndr_interface_table *table)
2278 struct dcerpc_binding_handle *h;
2279 struct rpccli_bh_state *hs;
2281 h = dcerpc_binding_handle_create(c,
2286 struct rpccli_bh_state,
2296 NTSTATUS rpccli_ncalrpc_bind_data(TALLOC_CTX *mem_ctx,
2297 struct pipe_auth_data **presult)
2299 struct pipe_auth_data *result;
2301 result = talloc_zero(mem_ctx, struct pipe_auth_data);
2302 if (result == NULL) {
2303 return NT_STATUS_NO_MEMORY;
2306 result->auth_type = DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM;
2307 result->auth_level = DCERPC_AUTH_LEVEL_CONNECT;
2309 result->user_name = talloc_strdup(result, "");
2310 result->domain = talloc_strdup(result, "");
2311 if ((result->user_name == NULL) || (result->domain == NULL)) {
2312 TALLOC_FREE(result);
2313 return NT_STATUS_NO_MEMORY;
2317 return NT_STATUS_OK;
2320 NTSTATUS rpccli_anon_bind_data(TALLOC_CTX *mem_ctx,
2321 struct pipe_auth_data **presult)
2323 struct pipe_auth_data *result;
2325 result = talloc_zero(mem_ctx, struct pipe_auth_data);
2326 if (result == NULL) {
2327 return NT_STATUS_NO_MEMORY;
2330 result->auth_type = DCERPC_AUTH_TYPE_NONE;
2331 result->auth_level = DCERPC_AUTH_LEVEL_NONE;
2333 result->user_name = talloc_strdup(result, "");
2334 result->domain = talloc_strdup(result, "");
2335 if ((result->user_name == NULL) || (result->domain == NULL)) {
2336 TALLOC_FREE(result);
2337 return NT_STATUS_NO_MEMORY;
2341 return NT_STATUS_OK;
2344 static NTSTATUS rpccli_generic_bind_data(TALLOC_CTX *mem_ctx,
2345 enum dcerpc_AuthType auth_type,
2346 enum dcerpc_AuthLevel auth_level,
2348 const char *target_service,
2350 const char *username,
2351 const char *password,
2352 enum credentials_use_kerberos use_kerberos,
2353 struct netlogon_creds_CredentialState *creds,
2354 struct pipe_auth_data **presult)
2356 struct auth_generic_state *auth_generic_ctx;
2357 struct pipe_auth_data *result;
2360 result = talloc_zero(mem_ctx, struct pipe_auth_data);
2361 if (result == NULL) {
2362 return NT_STATUS_NO_MEMORY;
2365 result->auth_type = auth_type;
2366 result->auth_level = auth_level;
2368 result->user_name = talloc_strdup(result, username);
2369 result->domain = talloc_strdup(result, domain);
2370 if ((result->user_name == NULL) || (result->domain == NULL)) {
2371 status = NT_STATUS_NO_MEMORY;
2375 status = auth_generic_client_prepare(result,
2377 if (!NT_STATUS_IS_OK(status)) {
2381 status = auth_generic_set_username(auth_generic_ctx, username);
2382 if (!NT_STATUS_IS_OK(status)) {
2386 status = auth_generic_set_domain(auth_generic_ctx, domain);
2387 if (!NT_STATUS_IS_OK(status)) {
2391 status = auth_generic_set_password(auth_generic_ctx, password);
2392 if (!NT_STATUS_IS_OK(status)) {
2396 status = gensec_set_target_service(auth_generic_ctx->gensec_security, target_service);
2397 if (!NT_STATUS_IS_OK(status)) {
2401 status = gensec_set_target_hostname(auth_generic_ctx->gensec_security, server);
2402 if (!NT_STATUS_IS_OK(status)) {
2406 cli_credentials_set_kerberos_state(auth_generic_ctx->credentials, use_kerberos);
2407 cli_credentials_set_netlogon_creds(auth_generic_ctx->credentials, creds);
2409 status = auth_generic_client_start_by_authtype(auth_generic_ctx, auth_type, auth_level);
2410 if (!NT_STATUS_IS_OK(status)) {
2414 result->auth_ctx = talloc_move(result, &auth_generic_ctx->gensec_security);
2415 talloc_free(auth_generic_ctx);
2417 return NT_STATUS_OK;
2420 TALLOC_FREE(result);
2425 * Create an rpc pipe client struct, connecting to a tcp port.
2427 static NTSTATUS rpc_pipe_open_tcp_port(TALLOC_CTX *mem_ctx, const char *host,
2428 const struct sockaddr_storage *ss_addr,
2430 const struct ndr_interface_table *table,
2431 struct rpc_pipe_client **presult)
2433 struct rpc_pipe_client *result;
2434 struct sockaddr_storage addr;
2438 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
2439 if (result == NULL) {
2440 return NT_STATUS_NO_MEMORY;
2443 result->abstract_syntax = table->syntax_id;
2444 result->transfer_syntax = ndr_transfer_syntax_ndr;
2446 result->desthost = talloc_strdup(result, host);
2447 result->srv_name_slash = talloc_asprintf_strupper_m(
2448 result, "\\\\%s", result->desthost);
2449 if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
2450 status = NT_STATUS_NO_MEMORY;
2454 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
2455 result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
2457 if (ss_addr == NULL) {
2458 if (!resolve_name(host, &addr, NBT_NAME_SERVER, false)) {
2459 status = NT_STATUS_NOT_FOUND;
2466 status = open_socket_out(&addr, port, 60*1000, &fd);
2467 if (!NT_STATUS_IS_OK(status)) {
2470 set_socket_options(fd, lp_socket_options());
2472 status = rpc_transport_sock_init(result, fd, &result->transport);
2473 if (!NT_STATUS_IS_OK(status)) {
2478 result->transport->transport = NCACN_IP_TCP;
2480 result->binding_handle = rpccli_bh_create(result, NULL, table);
2481 if (result->binding_handle == NULL) {
2482 TALLOC_FREE(result);
2483 return NT_STATUS_NO_MEMORY;
2487 return NT_STATUS_OK;
2490 TALLOC_FREE(result);
2495 * Determine the tcp port on which a dcerpc interface is listening
2496 * for the ncacn_ip_tcp transport via the endpoint mapper of the
2499 static NTSTATUS rpc_pipe_get_tcp_port(const char *host,
2500 const struct sockaddr_storage *addr,
2501 const struct ndr_interface_table *table,
2505 struct rpc_pipe_client *epm_pipe = NULL;
2506 struct dcerpc_binding_handle *epm_handle = NULL;
2507 struct pipe_auth_data *auth = NULL;
2508 struct dcerpc_binding *map_binding = NULL;
2509 struct dcerpc_binding *res_binding = NULL;
2510 enum dcerpc_transport_t transport;
2511 const char *endpoint = NULL;
2512 struct epm_twr_t *map_tower = NULL;
2513 struct epm_twr_t *res_towers = NULL;
2514 struct policy_handle *entry_handle = NULL;
2515 uint32_t num_towers = 0;
2516 uint32_t max_towers = 1;
2517 struct epm_twr_p_t towers;
2518 TALLOC_CTX *tmp_ctx = talloc_stackframe();
2519 uint32_t result = 0;
2521 if (pport == NULL) {
2522 status = NT_STATUS_INVALID_PARAMETER;
2526 if (ndr_syntax_id_equal(&table->syntax_id,
2527 &ndr_table_epmapper.syntax_id)) {
2529 status = NT_STATUS_OK;
2533 /* open the connection to the endpoint mapper */
2534 status = rpc_pipe_open_tcp_port(tmp_ctx, host, addr, 135,
2535 &ndr_table_epmapper,
2538 if (!NT_STATUS_IS_OK(status)) {
2541 epm_handle = epm_pipe->binding_handle;
2543 status = rpccli_anon_bind_data(tmp_ctx, &auth);
2544 if (!NT_STATUS_IS_OK(status)) {
2548 status = rpc_pipe_bind(epm_pipe, auth);
2549 if (!NT_STATUS_IS_OK(status)) {
2553 /* create tower for asking the epmapper */
2555 status = dcerpc_parse_binding(tmp_ctx, "ncacn_ip_tcp:[135]",
2557 if (!NT_STATUS_IS_OK(status)) {
2561 status = dcerpc_binding_set_abstract_syntax(map_binding,
2563 if (!NT_STATUS_IS_OK(status)) {
2567 map_tower = talloc_zero(tmp_ctx, struct epm_twr_t);
2568 if (map_tower == NULL) {
2569 status = NT_STATUS_NO_MEMORY;
2573 status = dcerpc_binding_build_tower(tmp_ctx, map_binding,
2574 &(map_tower->tower));
2575 if (!NT_STATUS_IS_OK(status)) {
2579 /* allocate further parameters for the epm_Map call */
2581 res_towers = talloc_array(tmp_ctx, struct epm_twr_t, max_towers);
2582 if (res_towers == NULL) {
2583 status = NT_STATUS_NO_MEMORY;
2586 towers.twr = res_towers;
2588 entry_handle = talloc_zero(tmp_ctx, struct policy_handle);
2589 if (entry_handle == NULL) {
2590 status = NT_STATUS_NO_MEMORY;
2594 /* ask the endpoint mapper for the port */
2596 status = dcerpc_epm_Map(epm_handle,
2598 discard_const_p(struct GUID,
2599 &(table->syntax_id.uuid)),
2607 if (!NT_STATUS_IS_OK(status)) {
2611 if (result != EPMAPPER_STATUS_OK) {
2612 status = NT_STATUS_UNSUCCESSFUL;
2616 if (num_towers != 1) {
2617 status = NT_STATUS_UNSUCCESSFUL;
2621 /* extract the port from the answer */
2623 status = dcerpc_binding_from_tower(tmp_ctx,
2624 &(towers.twr->tower),
2626 if (!NT_STATUS_IS_OK(status)) {
2630 transport = dcerpc_binding_get_transport(res_binding);
2631 endpoint = dcerpc_binding_get_string_option(res_binding, "endpoint");
2633 /* are further checks here necessary? */
2634 if (transport != NCACN_IP_TCP) {
2635 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2639 if (endpoint == NULL) {
2640 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2644 *pport = (uint16_t)atoi(endpoint);
2647 TALLOC_FREE(tmp_ctx);
2652 * Create a rpc pipe client struct, connecting to a host via tcp.
2653 * The port is determined by asking the endpoint mapper on the given
2656 NTSTATUS rpc_pipe_open_tcp(TALLOC_CTX *mem_ctx, const char *host,
2657 const struct sockaddr_storage *addr,
2658 const struct ndr_interface_table *table,
2659 struct rpc_pipe_client **presult)
2664 status = rpc_pipe_get_tcp_port(host, addr, table, &port);
2665 if (!NT_STATUS_IS_OK(status)) {
2669 return rpc_pipe_open_tcp_port(mem_ctx, host, addr, port,
2673 /********************************************************************
2674 Create a rpc pipe client struct, connecting to a unix domain socket
2675 ********************************************************************/
2676 NTSTATUS rpc_pipe_open_ncalrpc(TALLOC_CTX *mem_ctx, const char *socket_path,
2677 const struct ndr_interface_table *table,
2678 struct rpc_pipe_client **presult)
2680 struct rpc_pipe_client *result;
2681 struct sockaddr_un addr;
2686 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
2687 if (result == NULL) {
2688 return NT_STATUS_NO_MEMORY;
2691 result->abstract_syntax = table->syntax_id;
2692 result->transfer_syntax = ndr_transfer_syntax_ndr;
2694 result->desthost = get_myname(result);
2695 result->srv_name_slash = talloc_asprintf_strupper_m(
2696 result, "\\\\%s", result->desthost);
2697 if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
2698 status = NT_STATUS_NO_MEMORY;
2702 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
2703 result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
2705 fd = socket(AF_UNIX, SOCK_STREAM, 0);
2707 status = map_nt_error_from_unix(errno);
2712 addr.sun_family = AF_UNIX;
2713 strlcpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
2714 salen = sizeof(struct sockaddr_un);
2716 if (connect(fd, (struct sockaddr *)(void *)&addr, salen) == -1) {
2717 DEBUG(0, ("connect(%s) failed: %s\n", socket_path,
2720 return map_nt_error_from_unix(errno);
2723 status = rpc_transport_sock_init(result, fd, &result->transport);
2724 if (!NT_STATUS_IS_OK(status)) {
2729 result->transport->transport = NCALRPC;
2731 result->binding_handle = rpccli_bh_create(result, NULL, table);
2732 if (result->binding_handle == NULL) {
2733 TALLOC_FREE(result);
2734 return NT_STATUS_NO_MEMORY;
2738 return NT_STATUS_OK;
2741 TALLOC_FREE(result);
2745 struct rpc_pipe_client_np_ref {
2746 struct cli_state *cli;
2747 struct rpc_pipe_client *pipe;
2750 static int rpc_pipe_client_np_ref_destructor(struct rpc_pipe_client_np_ref *np_ref)
2752 DLIST_REMOVE(np_ref->cli->pipe_list, np_ref->pipe);
2756 /****************************************************************************
2757 Open a named pipe over SMB to a remote server.
2759 * CAVEAT CALLER OF THIS FUNCTION:
2760 * The returned rpc_pipe_client saves a copy of the cli_state cli pointer,
2761 * so be sure that this function is called AFTER any structure (vs pointer)
2762 * assignment of the cli. In particular, libsmbclient does structure
2763 * assignments of cli, which invalidates the data in the returned
2764 * rpc_pipe_client if this function is called before the structure assignment
2767 ****************************************************************************/
2769 static NTSTATUS rpc_pipe_open_np(struct cli_state *cli,
2770 const struct ndr_interface_table *table,
2771 struct rpc_pipe_client **presult)
2773 struct rpc_pipe_client *result;
2775 struct rpc_pipe_client_np_ref *np_ref;
2777 /* sanity check to protect against crashes */
2780 return NT_STATUS_INVALID_HANDLE;
2783 result = talloc_zero(NULL, struct rpc_pipe_client);
2784 if (result == NULL) {
2785 return NT_STATUS_NO_MEMORY;
2788 result->abstract_syntax = table->syntax_id;
2789 result->transfer_syntax = ndr_transfer_syntax_ndr;
2790 result->desthost = talloc_strdup(result, smbXcli_conn_remote_name(cli->conn));
2791 result->srv_name_slash = talloc_asprintf_strupper_m(
2792 result, "\\\\%s", result->desthost);
2794 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
2795 result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
2797 if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
2798 TALLOC_FREE(result);
2799 return NT_STATUS_NO_MEMORY;
2802 status = rpc_transport_np_init(result, cli, table,
2803 &result->transport);
2804 if (!NT_STATUS_IS_OK(status)) {
2805 TALLOC_FREE(result);
2809 result->transport->transport = NCACN_NP;
2811 np_ref = talloc(result->transport, struct rpc_pipe_client_np_ref);
2812 if (np_ref == NULL) {
2813 TALLOC_FREE(result);
2814 return NT_STATUS_NO_MEMORY;
2817 np_ref->pipe = result;
2819 DLIST_ADD(np_ref->cli->pipe_list, np_ref->pipe);
2820 talloc_set_destructor(np_ref, rpc_pipe_client_np_ref_destructor);
2822 result->binding_handle = rpccli_bh_create(result, NULL, table);
2823 if (result->binding_handle == NULL) {
2824 TALLOC_FREE(result);
2825 return NT_STATUS_NO_MEMORY;
2829 return NT_STATUS_OK;
2832 /****************************************************************************
2833 Open a pipe to a remote server.
2834 ****************************************************************************/
2836 static NTSTATUS cli_rpc_pipe_open(struct cli_state *cli,
2837 enum dcerpc_transport_t transport,
2838 const struct ndr_interface_table *table,
2839 struct rpc_pipe_client **presult)
2841 switch (transport) {
2843 return rpc_pipe_open_tcp(NULL,
2844 smbXcli_conn_remote_name(cli->conn),
2845 smbXcli_conn_remote_sockaddr(cli->conn),
2848 return rpc_pipe_open_np(cli, table, presult);
2850 return NT_STATUS_NOT_IMPLEMENTED;
2854 /****************************************************************************
2855 Open a named pipe to an SMB server and bind anonymously.
2856 ****************************************************************************/
2858 NTSTATUS cli_rpc_pipe_open_noauth_transport(struct cli_state *cli,
2859 enum dcerpc_transport_t transport,
2860 const struct ndr_interface_table *table,
2861 struct rpc_pipe_client **presult)
2863 struct rpc_pipe_client *result;
2864 struct pipe_auth_data *auth;
2867 status = cli_rpc_pipe_open(cli, transport, table, &result);
2868 if (!NT_STATUS_IS_OK(status)) {
2872 status = rpccli_anon_bind_data(result, &auth);
2873 if (!NT_STATUS_IS_OK(status)) {
2874 DEBUG(0, ("rpccli_anon_bind_data returned %s\n",
2875 nt_errstr(status)));
2876 TALLOC_FREE(result);
2881 * This is a bit of an abstraction violation due to the fact that an
2882 * anonymous bind on an authenticated SMB inherits the user/domain
2883 * from the enclosing SMB creds
2886 TALLOC_FREE(auth->user_name);
2887 TALLOC_FREE(auth->domain);
2889 auth->user_name = talloc_strdup(auth, cli->user_name);
2890 auth->domain = talloc_strdup(auth, cli->domain);
2892 if (transport == NCACN_NP) {
2893 struct smbXcli_session *session;
2895 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2896 session = cli->smb2.session;
2898 session = cli->smb1.session;
2901 status = smbXcli_session_application_key(session, auth,
2902 &auth->transport_session_key);
2903 if (!NT_STATUS_IS_OK(status)) {
2904 auth->transport_session_key = data_blob_null;
2908 if ((auth->user_name == NULL) || (auth->domain == NULL)) {
2909 TALLOC_FREE(result);
2910 return NT_STATUS_NO_MEMORY;
2913 status = rpc_pipe_bind(result, auth);
2914 if (!NT_STATUS_IS_OK(status)) {
2916 if (ndr_syntax_id_equal(&table->syntax_id,
2917 &ndr_table_dssetup.syntax_id)) {
2918 /* non AD domains just don't have this pipe, avoid
2919 * level 0 statement in that case - gd */
2922 DEBUG(lvl, ("cli_rpc_pipe_open_noauth: rpc_pipe_bind for pipe "
2923 "%s failed with error %s\n",
2925 nt_errstr(status) ));
2926 TALLOC_FREE(result);
2930 DEBUG(10,("cli_rpc_pipe_open_noauth: opened pipe %s to machine "
2931 "%s and bound anonymously.\n",
2936 return NT_STATUS_OK;
2939 /****************************************************************************
2940 ****************************************************************************/
2942 NTSTATUS cli_rpc_pipe_open_noauth(struct cli_state *cli,
2943 const struct ndr_interface_table *table,
2944 struct rpc_pipe_client **presult)
2946 return cli_rpc_pipe_open_noauth_transport(cli, NCACN_NP,
2950 /****************************************************************************
2951 Open a named pipe to an SMB server and bind using the mech specified
2952 ****************************************************************************/
2954 NTSTATUS cli_rpc_pipe_open_generic_auth(struct cli_state *cli,
2955 const struct ndr_interface_table *table,
2956 enum dcerpc_transport_t transport,
2957 enum dcerpc_AuthType auth_type,
2958 enum dcerpc_AuthLevel auth_level,
2961 const char *username,
2962 const char *password,
2963 struct rpc_pipe_client **presult)
2965 struct rpc_pipe_client *result;
2966 struct pipe_auth_data *auth = NULL;
2967 const char *target_service = table->authservices->names[0];
2971 status = cli_rpc_pipe_open(cli, transport, table, &result);
2972 if (!NT_STATUS_IS_OK(status)) {
2976 status = rpccli_generic_bind_data(result,
2977 auth_type, auth_level,
2978 server, target_service,
2979 domain, username, password,
2980 CRED_AUTO_USE_KERBEROS,
2983 if (!NT_STATUS_IS_OK(status)) {
2984 DEBUG(0, ("rpccli_generic_bind_data returned %s\n",
2985 nt_errstr(status)));
2989 status = rpc_pipe_bind(result, auth);
2990 if (!NT_STATUS_IS_OK(status)) {
2991 DEBUG(0, ("cli_rpc_pipe_open_generic_auth: cli_rpc_pipe_bind failed with error %s\n",
2992 nt_errstr(status) ));
2996 DEBUG(10,("cli_rpc_pipe_open_generic_auth: opened pipe %s to "
2997 "machine %s and bound as user %s\\%s.\n", table->name,
2998 result->desthost, domain, username));
3001 return NT_STATUS_OK;
3005 TALLOC_FREE(result);
3009 /****************************************************************************
3011 Open a named pipe to an SMB server and bind using schannel (bind type 68)
3012 using session_key. sign and seal.
3014 The *pdc will be stolen onto this new pipe
3015 ****************************************************************************/
3017 NTSTATUS cli_rpc_pipe_open_schannel_with_key(struct cli_state *cli,
3018 const struct ndr_interface_table *table,
3019 enum dcerpc_transport_t transport,
3021 struct netlogon_creds_cli_context *netlogon_creds,
3022 struct rpc_pipe_client **_rpccli)
3024 struct rpc_pipe_client *rpccli;
3025 struct pipe_auth_data *rpcauth;
3026 struct netlogon_creds_CredentialState *creds = NULL;
3027 enum dcerpc_AuthLevel auth_level;
3029 const char *target_service = table->authservices->names[0];
3030 int rpc_pipe_bind_dbglvl = 0;
3032 status = cli_rpc_pipe_open(cli, transport, table, &rpccli);
3033 if (!NT_STATUS_IS_OK(status)) {
3037 status = netlogon_creds_cli_lock(netlogon_creds, rpccli, &creds);
3038 if (!NT_STATUS_IS_OK(status)) {
3039 DEBUG(0, ("netlogon_creds_cli_get returned %s\n",
3040 nt_errstr(status)));
3041 TALLOC_FREE(rpccli);
3045 auth_level = netlogon_creds_cli_auth_level(netlogon_creds);
3047 status = rpccli_generic_bind_data(rpccli,
3048 DCERPC_AUTH_TYPE_SCHANNEL,
3053 creds->computer_name,
3055 CRED_AUTO_USE_KERBEROS,
3058 if (!NT_STATUS_IS_OK(status)) {
3059 DEBUG(0, ("rpccli_generic_bind_data returned %s\n",
3060 nt_errstr(status)));
3061 TALLOC_FREE(rpccli);
3065 status = rpc_pipe_bind(rpccli, rpcauth);
3066 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_ACCESS_DENIED)) {
3067 rpc_pipe_bind_dbglvl = 1;
3068 netlogon_creds_cli_delete(netlogon_creds, &creds);
3070 if (!NT_STATUS_IS_OK(status)) {
3071 DEBUG(rpc_pipe_bind_dbglvl,
3072 ("cli_rpc_pipe_open_schannel_with_key: "
3073 "rpc_pipe_bind failed with error %s\n",
3074 nt_errstr(status)));
3075 TALLOC_FREE(rpccli);
3081 if (!ndr_syntax_id_equal(&table->syntax_id, &ndr_table_netlogon.syntax_id)) {
3085 status = netlogon_creds_cli_check(netlogon_creds,
3086 rpccli->binding_handle);
3087 if (!NT_STATUS_IS_OK(status)) {
3088 DEBUG(0, ("netlogon_creds_cli_check failed with %s\n",
3089 nt_errstr(status)));
3090 TALLOC_FREE(rpccli);
3096 DEBUG(10,("cli_rpc_pipe_open_schannel_with_key: opened pipe %s to machine %s "
3097 "for domain %s and bound using schannel.\n",
3099 rpccli->desthost, domain));
3102 return NT_STATUS_OK;
3105 NTSTATUS cli_rpc_pipe_open_spnego(struct cli_state *cli,
3106 const struct ndr_interface_table *table,
3107 enum dcerpc_transport_t transport,
3109 enum dcerpc_AuthLevel auth_level,
3112 const char *username,
3113 const char *password,
3114 struct rpc_pipe_client **presult)
3116 struct rpc_pipe_client *result;
3117 struct pipe_auth_data *auth = NULL;
3118 const char *target_service = table->authservices->names[0];
3121 enum credentials_use_kerberos use_kerberos;
3123 if (strcmp(oid, GENSEC_OID_KERBEROS5) == 0) {
3124 use_kerberos = CRED_MUST_USE_KERBEROS;
3125 } else if (strcmp(oid, GENSEC_OID_NTLMSSP) == 0) {
3126 use_kerberos = CRED_DONT_USE_KERBEROS;
3128 return NT_STATUS_INVALID_PARAMETER;
3131 status = cli_rpc_pipe_open(cli, transport, table, &result);
3132 if (!NT_STATUS_IS_OK(status)) {
3136 status = rpccli_generic_bind_data(result,
3137 DCERPC_AUTH_TYPE_SPNEGO, auth_level,
3138 server, target_service,
3139 domain, username, password,
3142 if (!NT_STATUS_IS_OK(status)) {
3143 DEBUG(0, ("rpccli_generic_bind_data returned %s\n",
3144 nt_errstr(status)));
3148 status = rpc_pipe_bind(result, auth);
3149 if (!NT_STATUS_IS_OK(status)) {
3150 DEBUG(0, ("cli_rpc_pipe_open_spnego: cli_rpc_pipe_bind failed with error %s\n",
3151 nt_errstr(status) ));
3155 DEBUG(10,("cli_rpc_pipe_open_spnego: opened pipe %s to "
3156 "machine %s.\n", table->name,
3160 return NT_STATUS_OK;
3164 TALLOC_FREE(result);
3168 NTSTATUS cli_get_session_key(TALLOC_CTX *mem_ctx,
3169 struct rpc_pipe_client *cli,
3170 DATA_BLOB *session_key)
3173 struct pipe_auth_data *a;
3174 struct gensec_security *gensec_security;
3175 DATA_BLOB sk = data_blob_null;
3176 bool make_dup = false;
3178 if (!session_key || !cli) {
3179 return NT_STATUS_INVALID_PARAMETER;
3185 return NT_STATUS_INVALID_PARAMETER;
3188 switch (cli->auth->auth_type) {
3189 case DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM:
3190 case DCERPC_AUTH_TYPE_NONE:
3191 sk = data_blob_const(a->transport_session_key.data,
3192 a->transport_session_key.length);
3196 gensec_security = talloc_get_type(a->auth_ctx,
3197 struct gensec_security);
3198 status = gensec_session_key(gensec_security, mem_ctx, &sk);
3199 if (!NT_STATUS_IS_OK(status)) {
3207 return NT_STATUS_NO_USER_SESSION_KEY;
3211 *session_key = data_blob_dup_talloc(mem_ctx, sk);
3216 return NT_STATUS_OK;