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 "auth_generic.h"
28 #include "librpc/gen_ndr/ndr_dcerpc.h"
29 #include "librpc/gen_ndr/ndr_netlogon_c.h"
30 #include "librpc/rpc/dcerpc.h"
33 #include "libsmb/libsmb.h"
34 #include "auth/gensec/gensec.h"
35 #include "auth/credentials/credentials.h"
36 #include "../libcli/smb/smbXcli_base.h"
39 #define DBGC_CLASS DBGC_RPC_CLI
41 /********************************************************************
42 Pipe description for a DEBUG
43 ********************************************************************/
44 static const char *rpccli_pipe_txt(TALLOC_CTX
*mem_ctx
,
45 struct rpc_pipe_client
*cli
)
47 char *result
= talloc_asprintf(mem_ctx
, "host %s", cli
->desthost
);
54 /********************************************************************
56 ********************************************************************/
58 static uint32
get_rpc_call_id(void)
60 static uint32 call_id
= 0;
64 /*******************************************************************
65 Use SMBreadX to get rest of one fragment's worth of rpc data.
66 Reads the whole size or give an error message
67 ********************************************************************/
69 struct rpc_read_state
{
70 struct tevent_context
*ev
;
71 struct rpc_cli_transport
*transport
;
77 static void rpc_read_done(struct tevent_req
*subreq
);
79 static struct tevent_req
*rpc_read_send(TALLOC_CTX
*mem_ctx
,
80 struct tevent_context
*ev
,
81 struct rpc_cli_transport
*transport
,
82 uint8_t *data
, size_t size
)
84 struct tevent_req
*req
, *subreq
;
85 struct rpc_read_state
*state
;
87 req
= tevent_req_create(mem_ctx
, &state
, struct rpc_read_state
);
92 state
->transport
= transport
;
97 DEBUG(5, ("rpc_read_send: data_to_read: %u\n", (unsigned int)size
));
99 subreq
= transport
->read_send(state
, ev
, (uint8_t *)data
, size
,
101 if (subreq
== NULL
) {
104 tevent_req_set_callback(subreq
, rpc_read_done
, req
);
112 static void rpc_read_done(struct tevent_req
*subreq
)
114 struct tevent_req
*req
= tevent_req_callback_data(
115 subreq
, struct tevent_req
);
116 struct rpc_read_state
*state
= tevent_req_data(
117 req
, struct rpc_read_state
);
121 status
= state
->transport
->read_recv(subreq
, &received
);
123 if (!NT_STATUS_IS_OK(status
)) {
124 tevent_req_nterror(req
, status
);
128 state
->num_read
+= received
;
129 if (state
->num_read
== state
->size
) {
130 tevent_req_done(req
);
134 subreq
= state
->transport
->read_send(state
, state
->ev
,
135 state
->data
+ state
->num_read
,
136 state
->size
- state
->num_read
,
137 state
->transport
->priv
);
138 if (tevent_req_nomem(subreq
, req
)) {
141 tevent_req_set_callback(subreq
, rpc_read_done
, req
);
144 static NTSTATUS
rpc_read_recv(struct tevent_req
*req
)
146 return tevent_req_simple_recv_ntstatus(req
);
149 struct rpc_write_state
{
150 struct tevent_context
*ev
;
151 struct rpc_cli_transport
*transport
;
157 static void rpc_write_done(struct tevent_req
*subreq
);
159 static struct tevent_req
*rpc_write_send(TALLOC_CTX
*mem_ctx
,
160 struct tevent_context
*ev
,
161 struct rpc_cli_transport
*transport
,
162 const uint8_t *data
, size_t size
)
164 struct tevent_req
*req
, *subreq
;
165 struct rpc_write_state
*state
;
167 req
= tevent_req_create(mem_ctx
, &state
, struct rpc_write_state
);
172 state
->transport
= transport
;
175 state
->num_written
= 0;
177 DEBUG(5, ("rpc_write_send: data_to_write: %u\n", (unsigned int)size
));
179 subreq
= transport
->write_send(state
, ev
, data
, size
, transport
->priv
);
180 if (subreq
== NULL
) {
183 tevent_req_set_callback(subreq
, rpc_write_done
, req
);
190 static void rpc_write_done(struct tevent_req
*subreq
)
192 struct tevent_req
*req
= tevent_req_callback_data(
193 subreq
, struct tevent_req
);
194 struct rpc_write_state
*state
= tevent_req_data(
195 req
, struct rpc_write_state
);
199 status
= state
->transport
->write_recv(subreq
, &written
);
201 if (!NT_STATUS_IS_OK(status
)) {
202 tevent_req_nterror(req
, status
);
206 state
->num_written
+= written
;
208 if (state
->num_written
== state
->size
) {
209 tevent_req_done(req
);
213 subreq
= state
->transport
->write_send(state
, state
->ev
,
214 state
->data
+ state
->num_written
,
215 state
->size
- state
->num_written
,
216 state
->transport
->priv
);
217 if (tevent_req_nomem(subreq
, req
)) {
220 tevent_req_set_callback(subreq
, rpc_write_done
, req
);
223 static NTSTATUS
rpc_write_recv(struct tevent_req
*req
)
225 return tevent_req_simple_recv_ntstatus(req
);
229 /****************************************************************************
230 Try and get a PDU's worth of data from current_pdu. If not, then read more
232 ****************************************************************************/
234 struct get_complete_frag_state
{
235 struct tevent_context
*ev
;
236 struct rpc_pipe_client
*cli
;
241 static void get_complete_frag_got_header(struct tevent_req
*subreq
);
242 static void get_complete_frag_got_rest(struct tevent_req
*subreq
);
244 static struct tevent_req
*get_complete_frag_send(TALLOC_CTX
*mem_ctx
,
245 struct tevent_context
*ev
,
246 struct rpc_pipe_client
*cli
,
249 struct tevent_req
*req
, *subreq
;
250 struct get_complete_frag_state
*state
;
254 req
= tevent_req_create(mem_ctx
, &state
,
255 struct get_complete_frag_state
);
261 state
->frag_len
= RPC_HEADER_LEN
;
264 received
= pdu
->length
;
265 if (received
< RPC_HEADER_LEN
) {
266 if (!data_blob_realloc(mem_ctx
, pdu
, RPC_HEADER_LEN
)) {
267 status
= NT_STATUS_NO_MEMORY
;
270 subreq
= rpc_read_send(state
, state
->ev
,
271 state
->cli
->transport
,
272 pdu
->data
+ received
,
273 RPC_HEADER_LEN
- received
);
274 if (subreq
== NULL
) {
275 status
= NT_STATUS_NO_MEMORY
;
278 tevent_req_set_callback(subreq
, get_complete_frag_got_header
,
283 state
->frag_len
= dcerpc_get_frag_length(pdu
);
284 if (state
->frag_len
< RPC_HEADER_LEN
) {
285 tevent_req_nterror(req
, NT_STATUS_RPC_PROTOCOL_ERROR
);
286 return tevent_req_post(req
, ev
);
290 * Ensure we have frag_len bytes of data.
292 if (received
< state
->frag_len
) {
293 if (!data_blob_realloc(NULL
, pdu
, state
->frag_len
)) {
294 status
= NT_STATUS_NO_MEMORY
;
297 subreq
= rpc_read_send(state
, state
->ev
,
298 state
->cli
->transport
,
299 pdu
->data
+ received
,
300 state
->frag_len
- received
);
301 if (subreq
== NULL
) {
302 status
= NT_STATUS_NO_MEMORY
;
305 tevent_req_set_callback(subreq
, get_complete_frag_got_rest
,
310 status
= NT_STATUS_OK
;
312 if (NT_STATUS_IS_OK(status
)) {
313 tevent_req_done(req
);
315 tevent_req_nterror(req
, status
);
317 return tevent_req_post(req
, ev
);
320 static void get_complete_frag_got_header(struct tevent_req
*subreq
)
322 struct tevent_req
*req
= tevent_req_callback_data(
323 subreq
, struct tevent_req
);
324 struct get_complete_frag_state
*state
= tevent_req_data(
325 req
, struct get_complete_frag_state
);
328 status
= rpc_read_recv(subreq
);
330 if (!NT_STATUS_IS_OK(status
)) {
331 tevent_req_nterror(req
, status
);
335 state
->frag_len
= dcerpc_get_frag_length(state
->pdu
);
336 if (state
->frag_len
< RPC_HEADER_LEN
) {
337 tevent_req_nterror(req
, NT_STATUS_RPC_PROTOCOL_ERROR
);
341 if (!data_blob_realloc(NULL
, state
->pdu
, state
->frag_len
)) {
342 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
347 * We're here in this piece of code because we've read exactly
348 * RPC_HEADER_LEN bytes into state->pdu.
351 subreq
= rpc_read_send(state
, state
->ev
, state
->cli
->transport
,
352 state
->pdu
->data
+ RPC_HEADER_LEN
,
353 state
->frag_len
- RPC_HEADER_LEN
);
354 if (tevent_req_nomem(subreq
, req
)) {
357 tevent_req_set_callback(subreq
, get_complete_frag_got_rest
, req
);
360 static void get_complete_frag_got_rest(struct tevent_req
*subreq
)
362 struct tevent_req
*req
= tevent_req_callback_data(
363 subreq
, struct tevent_req
);
366 status
= rpc_read_recv(subreq
);
368 if (!NT_STATUS_IS_OK(status
)) {
369 tevent_req_nterror(req
, status
);
372 tevent_req_done(req
);
375 static NTSTATUS
get_complete_frag_recv(struct tevent_req
*req
)
377 return tevent_req_simple_recv_ntstatus(req
);
380 /****************************************************************************
381 Do basic authentication checks on an incoming pdu.
382 ****************************************************************************/
384 static NTSTATUS
cli_pipe_validate_current_pdu(TALLOC_CTX
*mem_ctx
,
385 struct rpc_pipe_client
*cli
,
386 struct ncacn_packet
*pkt
,
388 uint8_t expected_pkt_type
,
391 DATA_BLOB
*reply_pdu
)
393 struct dcerpc_response
*r
;
394 NTSTATUS ret
= NT_STATUS_OK
;
398 * Point the return values at the real data including the RPC
399 * header. Just in case the caller wants it.
403 /* Ensure we have the correct type. */
404 switch (pkt
->ptype
) {
405 case DCERPC_PKT_ALTER_RESP
:
406 case DCERPC_PKT_BIND_ACK
:
408 /* Client code never receives this kind of packets */
412 case DCERPC_PKT_RESPONSE
:
414 r
= &pkt
->u
.response
;
416 /* Here's where we deal with incoming sign/seal. */
417 ret
= dcerpc_check_auth(cli
->auth
, pkt
,
418 &r
->stub_and_verifier
,
419 DCERPC_RESPONSE_LENGTH
,
421 if (!NT_STATUS_IS_OK(ret
)) {
425 if (pkt
->frag_length
< DCERPC_RESPONSE_LENGTH
+ pad_len
) {
426 return NT_STATUS_BUFFER_TOO_SMALL
;
429 /* Point the return values at the NDR data. */
430 rdata
->data
= r
->stub_and_verifier
.data
;
432 if (pkt
->auth_length
) {
433 /* We've already done integer wrap tests in
434 * dcerpc_check_auth(). */
435 rdata
->length
= r
->stub_and_verifier
.length
437 - DCERPC_AUTH_TRAILER_LENGTH
440 rdata
->length
= r
->stub_and_verifier
.length
;
443 DEBUG(10, ("Got pdu len %lu, data_len %lu, ss_len %u\n",
444 (long unsigned int)pdu
->length
,
445 (long unsigned int)rdata
->length
,
446 (unsigned int)pad_len
));
449 * If this is the first reply, and the allocation hint is
450 * reasonable, try and set up the reply_pdu DATA_BLOB to the
454 if ((reply_pdu
->length
== 0) &&
455 r
->alloc_hint
&& (r
->alloc_hint
< 15*1024*1024)) {
456 if (!data_blob_realloc(mem_ctx
, reply_pdu
,
458 DEBUG(0, ("reply alloc hint %d too "
459 "large to allocate\n",
460 (int)r
->alloc_hint
));
461 return NT_STATUS_NO_MEMORY
;
467 case DCERPC_PKT_BIND_NAK
:
468 DEBUG(1, (__location__
": Bind NACK received from %s!\n",
469 rpccli_pipe_txt(talloc_tos(), cli
)));
470 /* Use this for now... */
471 return NT_STATUS_NETWORK_ACCESS_DENIED
;
473 case DCERPC_PKT_FAULT
:
475 DEBUG(1, (__location__
": RPC fault code %s received "
477 dcerpc_errstr(talloc_tos(),
478 pkt
->u
.fault
.status
),
479 rpccli_pipe_txt(talloc_tos(), cli
)));
481 return dcerpc_fault_to_nt_status(pkt
->u
.fault
.status
);
484 DEBUG(0, (__location__
"Unknown packet type %u received "
486 (unsigned int)pkt
->ptype
,
487 rpccli_pipe_txt(talloc_tos(), cli
)));
488 return NT_STATUS_RPC_PROTOCOL_ERROR
;
491 if (pkt
->ptype
!= expected_pkt_type
) {
492 DEBUG(3, (__location__
": Connection to %s got an unexpected "
493 "RPC packet type - %u, not %u\n",
494 rpccli_pipe_txt(talloc_tos(), cli
),
495 pkt
->ptype
, expected_pkt_type
));
496 return NT_STATUS_RPC_PROTOCOL_ERROR
;
499 if (pkt
->call_id
!= call_id
) {
500 DEBUG(3, (__location__
": Connection to %s got an unexpected "
501 "RPC call_id - %u, not %u\n",
502 rpccli_pipe_txt(talloc_tos(), cli
),
503 pkt
->call_id
, call_id
));
504 return NT_STATUS_RPC_PROTOCOL_ERROR
;
507 /* Do this just before return - we don't want to modify any rpc header
508 data before now as we may have needed to do cryptographic actions on
511 if ((pkt
->ptype
== DCERPC_PKT_BIND_ACK
) &&
512 !(pkt
->pfc_flags
& DCERPC_PFC_FLAG_LAST
)) {
513 DEBUG(5, (__location__
": bug in server (AS/U?), setting "
514 "fragment first/last ON.\n"));
515 pkt
->pfc_flags
|= DCERPC_PFC_FLAG_FIRST
| DCERPC_PFC_FLAG_LAST
;
521 /****************************************************************************
522 Call a remote api on an arbitrary pipe. takes param, data and setup buffers.
523 ****************************************************************************/
525 struct cli_api_pipe_state
{
526 struct tevent_context
*ev
;
527 struct rpc_cli_transport
*transport
;
532 static void cli_api_pipe_trans_done(struct tevent_req
*subreq
);
533 static void cli_api_pipe_write_done(struct tevent_req
*subreq
);
534 static void cli_api_pipe_read_done(struct tevent_req
*subreq
);
536 static struct tevent_req
*cli_api_pipe_send(TALLOC_CTX
*mem_ctx
,
537 struct tevent_context
*ev
,
538 struct rpc_cli_transport
*transport
,
539 uint8_t *data
, size_t data_len
,
540 uint32_t max_rdata_len
)
542 struct tevent_req
*req
, *subreq
;
543 struct cli_api_pipe_state
*state
;
546 req
= tevent_req_create(mem_ctx
, &state
, struct cli_api_pipe_state
);
551 state
->transport
= transport
;
553 if (max_rdata_len
< RPC_HEADER_LEN
) {
555 * For a RPC reply we always need at least RPC_HEADER_LEN
556 * bytes. We check this here because we will receive
557 * RPC_HEADER_LEN bytes in cli_trans_sock_send_done.
559 status
= NT_STATUS_INVALID_PARAMETER
;
563 if (transport
->trans_send
!= NULL
) {
564 subreq
= transport
->trans_send(state
, ev
, data
, data_len
,
565 max_rdata_len
, transport
->priv
);
566 if (subreq
== NULL
) {
569 tevent_req_set_callback(subreq
, cli_api_pipe_trans_done
, req
);
574 * If the transport does not provide a "trans" routine, i.e. for
575 * example the ncacn_ip_tcp transport, do the write/read step here.
578 subreq
= rpc_write_send(state
, ev
, transport
, data
, data_len
);
579 if (subreq
== NULL
) {
582 tevent_req_set_callback(subreq
, cli_api_pipe_write_done
, req
);
586 tevent_req_nterror(req
, status
);
587 return tevent_req_post(req
, ev
);
593 static void cli_api_pipe_trans_done(struct tevent_req
*subreq
)
595 struct tevent_req
*req
= tevent_req_callback_data(
596 subreq
, struct tevent_req
);
597 struct cli_api_pipe_state
*state
= tevent_req_data(
598 req
, struct cli_api_pipe_state
);
601 status
= state
->transport
->trans_recv(subreq
, state
, &state
->rdata
,
604 if (!NT_STATUS_IS_OK(status
)) {
605 tevent_req_nterror(req
, status
);
608 tevent_req_done(req
);
611 static void cli_api_pipe_write_done(struct tevent_req
*subreq
)
613 struct tevent_req
*req
= tevent_req_callback_data(
614 subreq
, struct tevent_req
);
615 struct cli_api_pipe_state
*state
= tevent_req_data(
616 req
, struct cli_api_pipe_state
);
619 status
= rpc_write_recv(subreq
);
621 if (!NT_STATUS_IS_OK(status
)) {
622 tevent_req_nterror(req
, status
);
626 state
->rdata
= talloc_array(state
, uint8_t, RPC_HEADER_LEN
);
627 if (tevent_req_nomem(state
->rdata
, req
)) {
632 * We don't need to use rpc_read_send here, the upper layer will cope
633 * with a short read, transport->trans_send could also return less
634 * than state->max_rdata_len.
636 subreq
= state
->transport
->read_send(state
, state
->ev
, state
->rdata
,
638 state
->transport
->priv
);
639 if (tevent_req_nomem(subreq
, req
)) {
642 tevent_req_set_callback(subreq
, cli_api_pipe_read_done
, req
);
645 static void cli_api_pipe_read_done(struct tevent_req
*subreq
)
647 struct tevent_req
*req
= tevent_req_callback_data(
648 subreq
, struct tevent_req
);
649 struct cli_api_pipe_state
*state
= tevent_req_data(
650 req
, struct cli_api_pipe_state
);
654 status
= state
->transport
->read_recv(subreq
, &received
);
656 if (!NT_STATUS_IS_OK(status
)) {
657 tevent_req_nterror(req
, status
);
660 state
->rdata_len
= received
;
661 tevent_req_done(req
);
664 static NTSTATUS
cli_api_pipe_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
665 uint8_t **prdata
, uint32_t *prdata_len
)
667 struct cli_api_pipe_state
*state
= tevent_req_data(
668 req
, struct cli_api_pipe_state
);
671 if (tevent_req_is_nterror(req
, &status
)) {
675 *prdata
= talloc_move(mem_ctx
, &state
->rdata
);
676 *prdata_len
= state
->rdata_len
;
680 /****************************************************************************
681 Send data on an rpc pipe via trans. The data must be the last
682 pdu fragment of an NDR data stream.
684 Receive response data from an rpc pipe, which may be large...
686 Read the first fragment: unfortunately have to use SMBtrans for the first
687 bit, then SMBreadX for subsequent bits.
689 If first fragment received also wasn't the last fragment, continue
690 getting fragments until we _do_ receive the last fragment.
692 Request/Response PDU's look like the following...
694 |<------------------PDU len----------------------------------------------->|
695 |<-HDR_LEN-->|<--REQ LEN------>|.............|<-AUTH_HDRLEN->|<-AUTH_LEN-->|
697 +------------+-----------------+-------------+---------------+-------------+
698 | RPC HEADER | REQ/RESP HEADER | DATA ...... | AUTH_HDR | AUTH DATA |
699 +------------+-----------------+-------------+---------------+-------------+
701 Where the presence of the AUTH_HDR and AUTH DATA are dependent on the
702 signing & sealing being negotiated.
704 ****************************************************************************/
706 struct rpc_api_pipe_state
{
707 struct tevent_context
*ev
;
708 struct rpc_pipe_client
*cli
;
709 uint8_t expected_pkt_type
;
712 DATA_BLOB incoming_frag
;
713 struct ncacn_packet
*pkt
;
717 size_t reply_pdu_offset
;
721 static void rpc_api_pipe_trans_done(struct tevent_req
*subreq
);
722 static void rpc_api_pipe_got_pdu(struct tevent_req
*subreq
);
723 static void rpc_api_pipe_auth3_done(struct tevent_req
*subreq
);
725 static struct tevent_req
*rpc_api_pipe_send(TALLOC_CTX
*mem_ctx
,
726 struct tevent_context
*ev
,
727 struct rpc_pipe_client
*cli
,
728 DATA_BLOB
*data
, /* Outgoing PDU */
729 uint8_t expected_pkt_type
,
732 struct tevent_req
*req
, *subreq
;
733 struct rpc_api_pipe_state
*state
;
734 uint16_t max_recv_frag
;
737 req
= tevent_req_create(mem_ctx
, &state
, struct rpc_api_pipe_state
);
743 state
->expected_pkt_type
= expected_pkt_type
;
744 state
->call_id
= call_id
;
745 state
->endianess
= DCERPC_DREP_LE
;
748 * Ensure we're not sending too much.
750 if (data
->length
> cli
->max_xmit_frag
) {
751 status
= NT_STATUS_INVALID_PARAMETER
;
755 DEBUG(5,("rpc_api_pipe: %s\n", rpccli_pipe_txt(talloc_tos(), cli
)));
757 if (state
->expected_pkt_type
== DCERPC_PKT_AUTH3
) {
758 subreq
= rpc_write_send(state
, ev
, cli
->transport
,
759 data
->data
, data
->length
);
760 if (subreq
== NULL
) {
763 tevent_req_set_callback(subreq
, rpc_api_pipe_auth3_done
, req
);
767 /* get the header first, then fetch the rest once we have
768 * the frag_length available */
769 max_recv_frag
= RPC_HEADER_LEN
;
771 subreq
= cli_api_pipe_send(state
, ev
, cli
->transport
,
772 data
->data
, data
->length
, max_recv_frag
);
773 if (subreq
== NULL
) {
776 tevent_req_set_callback(subreq
, rpc_api_pipe_trans_done
, req
);
780 tevent_req_nterror(req
, status
);
781 return tevent_req_post(req
, ev
);
787 static void rpc_api_pipe_auth3_done(struct tevent_req
*subreq
)
789 struct tevent_req
*req
=
790 tevent_req_callback_data(subreq
,
794 status
= rpc_write_recv(subreq
);
796 if (!NT_STATUS_IS_OK(status
)) {
797 tevent_req_nterror(req
, status
);
801 tevent_req_done(req
);
804 static void rpc_api_pipe_trans_done(struct tevent_req
*subreq
)
806 struct tevent_req
*req
= tevent_req_callback_data(
807 subreq
, struct tevent_req
);
808 struct rpc_api_pipe_state
*state
= tevent_req_data(
809 req
, struct rpc_api_pipe_state
);
811 uint8_t *rdata
= NULL
;
812 uint32_t rdata_len
= 0;
814 status
= cli_api_pipe_recv(subreq
, state
, &rdata
, &rdata_len
);
816 if (!NT_STATUS_IS_OK(status
)) {
817 DEBUG(5, ("cli_api_pipe failed: %s\n", nt_errstr(status
)));
818 tevent_req_nterror(req
, status
);
823 DEBUG(3,("rpc_api_pipe: %s failed to return data.\n",
824 rpccli_pipe_txt(talloc_tos(), state
->cli
)));
825 tevent_req_done(req
);
830 * Move data on state->incoming_frag.
832 state
->incoming_frag
.data
= talloc_move(state
, &rdata
);
833 state
->incoming_frag
.length
= rdata_len
;
834 if (!state
->incoming_frag
.data
) {
835 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
839 /* Ensure we have enough data for a pdu. */
840 subreq
= get_complete_frag_send(state
, state
->ev
, state
->cli
,
841 &state
->incoming_frag
);
842 if (tevent_req_nomem(subreq
, req
)) {
845 tevent_req_set_callback(subreq
, rpc_api_pipe_got_pdu
, req
);
848 static void rpc_api_pipe_got_pdu(struct tevent_req
*subreq
)
850 struct tevent_req
*req
= tevent_req_callback_data(
851 subreq
, struct tevent_req
);
852 struct rpc_api_pipe_state
*state
= tevent_req_data(
853 req
, struct rpc_api_pipe_state
);
855 DATA_BLOB rdata
= data_blob_null
;
857 status
= get_complete_frag_recv(subreq
);
859 if (!NT_STATUS_IS_OK(status
)) {
860 DEBUG(5, ("get_complete_frag failed: %s\n",
862 tevent_req_nterror(req
, status
);
866 state
->pkt
= talloc(state
, struct ncacn_packet
);
868 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
872 status
= dcerpc_pull_ncacn_packet(state
->pkt
,
873 &state
->incoming_frag
,
876 if (!NT_STATUS_IS_OK(status
)) {
877 tevent_req_nterror(req
, status
);
881 if (state
->incoming_frag
.length
!= state
->pkt
->frag_length
) {
882 DEBUG(5, ("Incorrect pdu length %u, expected %u\n",
883 (unsigned int)state
->incoming_frag
.length
,
884 (unsigned int)state
->pkt
->frag_length
));
885 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
889 status
= cli_pipe_validate_current_pdu(state
,
890 state
->cli
, state
->pkt
,
891 &state
->incoming_frag
,
892 state
->expected_pkt_type
,
897 DEBUG(10,("rpc_api_pipe: got frag len of %u at offset %u: %s\n",
898 (unsigned)state
->incoming_frag
.length
,
899 (unsigned)state
->reply_pdu_offset
,
902 if (!NT_STATUS_IS_OK(status
)) {
903 tevent_req_nterror(req
, status
);
907 if ((state
->pkt
->pfc_flags
& DCERPC_PFC_FLAG_FIRST
)
908 && (state
->pkt
->drep
[0] != DCERPC_DREP_LE
)) {
910 * Set the data type correctly for big-endian data on the
913 DEBUG(10,("rpc_api_pipe: On %s PDU data format is "
915 rpccli_pipe_txt(talloc_tos(), state
->cli
)));
916 state
->endianess
= 0x00; /* BIG ENDIAN */
919 * Check endianness on subsequent packets.
921 if (state
->endianess
!= state
->pkt
->drep
[0]) {
922 DEBUG(0,("rpc_api_pipe: Error : Endianness changed from %s to "
924 state
->endianess
?"little":"big",
925 state
->pkt
->drep
[0]?"little":"big"));
926 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
930 /* Now copy the data portion out of the pdu into rbuf. */
931 if (state
->reply_pdu
.length
< state
->reply_pdu_offset
+ rdata
.length
) {
932 if (!data_blob_realloc(NULL
, &state
->reply_pdu
,
933 state
->reply_pdu_offset
+ rdata
.length
)) {
934 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
939 memcpy(state
->reply_pdu
.data
+ state
->reply_pdu_offset
,
940 rdata
.data
, rdata
.length
);
941 state
->reply_pdu_offset
+= rdata
.length
;
943 /* reset state->incoming_frag, there is no need to free it,
944 * it will be reallocated to the right size the next time
946 state
->incoming_frag
.length
= 0;
948 if (state
->pkt
->pfc_flags
& DCERPC_PFC_FLAG_LAST
) {
949 /* make sure the pdu length is right now that we
950 * have all the data available (alloc hint may
951 * have allocated more than was actually used) */
952 state
->reply_pdu
.length
= state
->reply_pdu_offset
;
953 DEBUG(10,("rpc_api_pipe: %s returned %u bytes.\n",
954 rpccli_pipe_txt(talloc_tos(), state
->cli
),
955 (unsigned)state
->reply_pdu
.length
));
956 tevent_req_done(req
);
960 subreq
= get_complete_frag_send(state
, state
->ev
, state
->cli
,
961 &state
->incoming_frag
);
962 if (tevent_req_nomem(subreq
, req
)) {
965 tevent_req_set_callback(subreq
, rpc_api_pipe_got_pdu
, req
);
968 static NTSTATUS
rpc_api_pipe_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
969 struct ncacn_packet
**pkt
,
970 DATA_BLOB
*reply_pdu
)
972 struct rpc_api_pipe_state
*state
= tevent_req_data(
973 req
, struct rpc_api_pipe_state
);
976 if (tevent_req_is_nterror(req
, &status
)) {
980 /* return data to caller and assign it ownership of memory */
982 reply_pdu
->data
= talloc_move(mem_ctx
, &state
->reply_pdu
.data
);
983 reply_pdu
->length
= state
->reply_pdu
.length
;
984 state
->reply_pdu
.length
= 0;
986 data_blob_free(&state
->reply_pdu
);
990 *pkt
= talloc_steal(mem_ctx
, state
->pkt
);
996 /*******************************************************************
997 Creates NTLMSSP auth bind.
998 ********************************************************************/
1000 static NTSTATUS
create_generic_auth_rpc_bind_req(struct rpc_pipe_client
*cli
,
1001 TALLOC_CTX
*mem_ctx
,
1002 DATA_BLOB
*auth_token
)
1004 struct gensec_security
*gensec_security
;
1005 DATA_BLOB null_blob
= data_blob_null
;
1007 gensec_security
= talloc_get_type_abort(cli
->auth
->auth_ctx
,
1008 struct gensec_security
);
1010 DEBUG(5, ("create_generic_auth_rpc_bind_req: generate first token\n"));
1011 return gensec_update(gensec_security
, mem_ctx
, NULL
, null_blob
, auth_token
);
1014 /*******************************************************************
1015 Creates the internals of a DCE/RPC bind request or alter context PDU.
1016 ********************************************************************/
1018 static NTSTATUS
create_bind_or_alt_ctx_internal(TALLOC_CTX
*mem_ctx
,
1019 enum dcerpc_pkt_type ptype
,
1021 const struct ndr_syntax_id
*abstract
,
1022 const struct ndr_syntax_id
*transfer
,
1023 const DATA_BLOB
*auth_info
,
1026 uint16 auth_len
= auth_info
->length
;
1028 union dcerpc_payload u
;
1029 struct dcerpc_ctx_list ctx_list
;
1032 auth_len
-= DCERPC_AUTH_TRAILER_LENGTH
;
1035 ctx_list
.context_id
= 0;
1036 ctx_list
.num_transfer_syntaxes
= 1;
1037 ctx_list
.abstract_syntax
= *abstract
;
1038 ctx_list
.transfer_syntaxes
= (struct ndr_syntax_id
*)discard_const(transfer
);
1040 u
.bind
.max_xmit_frag
= RPC_MAX_PDU_FRAG_LEN
;
1041 u
.bind
.max_recv_frag
= RPC_MAX_PDU_FRAG_LEN
;
1042 u
.bind
.assoc_group_id
= 0x0;
1043 u
.bind
.num_contexts
= 1;
1044 u
.bind
.ctx_list
= &ctx_list
;
1045 u
.bind
.auth_info
= *auth_info
;
1047 status
= dcerpc_push_ncacn_packet(mem_ctx
,
1049 DCERPC_PFC_FLAG_FIRST
|
1050 DCERPC_PFC_FLAG_LAST
,
1055 if (!NT_STATUS_IS_OK(status
)) {
1056 DEBUG(0, ("Failed to marshall bind/alter ncacn_packet.\n"));
1060 return NT_STATUS_OK
;
1063 /*******************************************************************
1064 Creates a DCE/RPC bind request.
1065 ********************************************************************/
1067 static NTSTATUS
create_rpc_bind_req(TALLOC_CTX
*mem_ctx
,
1068 struct rpc_pipe_client
*cli
,
1069 struct pipe_auth_data
*auth
,
1071 const struct ndr_syntax_id
*abstract
,
1072 const struct ndr_syntax_id
*transfer
,
1075 DATA_BLOB auth_token
= data_blob_null
;
1076 DATA_BLOB auth_info
= data_blob_null
;
1077 NTSTATUS ret
= NT_STATUS_OK
;
1079 switch (auth
->auth_type
) {
1080 case DCERPC_AUTH_TYPE_SCHANNEL
:
1081 case DCERPC_AUTH_TYPE_NTLMSSP
:
1082 case DCERPC_AUTH_TYPE_KRB5
:
1083 case DCERPC_AUTH_TYPE_SPNEGO
:
1084 ret
= create_generic_auth_rpc_bind_req(cli
, mem_ctx
, &auth_token
);
1086 if (!NT_STATUS_IS_OK(ret
) &&
1087 !NT_STATUS_EQUAL(ret
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1092 case DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM
:
1093 auth_token
= data_blob_talloc(mem_ctx
,
1094 "NCALRPC_AUTH_TOKEN",
1098 case DCERPC_AUTH_TYPE_NONE
:
1102 /* "Can't" happen. */
1103 return NT_STATUS_INVALID_INFO_CLASS
;
1106 if (auth_token
.length
!= 0) {
1107 ret
= dcerpc_push_dcerpc_auth(cli
,
1110 0, /* auth_pad_length */
1111 1, /* auth_context_id */
1114 if (!NT_STATUS_IS_OK(ret
)) {
1117 data_blob_free(&auth_token
);
1120 ret
= create_bind_or_alt_ctx_internal(mem_ctx
,
1130 /*******************************************************************
1132 Does an rpc request on a pipe. Incoming data is NDR encoded in in_data.
1133 Reply is NDR encoded in out_data. Splits the data stream into RPC PDU's
1134 and deals with signing/sealing details.
1135 ********************************************************************/
1137 struct rpc_api_pipe_req_state
{
1138 struct tevent_context
*ev
;
1139 struct rpc_pipe_client
*cli
;
1142 DATA_BLOB
*req_data
;
1143 uint32_t req_data_sent
;
1145 DATA_BLOB reply_pdu
;
1148 static void rpc_api_pipe_req_write_done(struct tevent_req
*subreq
);
1149 static void rpc_api_pipe_req_done(struct tevent_req
*subreq
);
1150 static NTSTATUS
prepare_next_frag(struct rpc_api_pipe_req_state
*state
,
1151 bool *is_last_frag
);
1153 struct tevent_req
*rpc_api_pipe_req_send(TALLOC_CTX
*mem_ctx
,
1154 struct tevent_context
*ev
,
1155 struct rpc_pipe_client
*cli
,
1157 DATA_BLOB
*req_data
)
1159 struct tevent_req
*req
, *subreq
;
1160 struct rpc_api_pipe_req_state
*state
;
1164 req
= tevent_req_create(mem_ctx
, &state
,
1165 struct rpc_api_pipe_req_state
);
1171 state
->op_num
= op_num
;
1172 state
->req_data
= req_data
;
1173 state
->req_data_sent
= 0;
1174 state
->call_id
= get_rpc_call_id();
1175 state
->reply_pdu
= data_blob_null
;
1176 state
->rpc_out
= data_blob_null
;
1178 if (cli
->max_xmit_frag
< DCERPC_REQUEST_LENGTH
1179 + RPC_MAX_SIGN_SIZE
) {
1180 /* Server is screwed up ! */
1181 status
= NT_STATUS_INVALID_PARAMETER
;
1185 status
= prepare_next_frag(state
, &is_last_frag
);
1186 if (!NT_STATUS_IS_OK(status
)) {
1191 subreq
= rpc_api_pipe_send(state
, ev
, state
->cli
,
1193 DCERPC_PKT_RESPONSE
,
1195 if (subreq
== NULL
) {
1198 tevent_req_set_callback(subreq
, rpc_api_pipe_req_done
, req
);
1200 subreq
= rpc_write_send(state
, ev
, cli
->transport
,
1201 state
->rpc_out
.data
,
1202 state
->rpc_out
.length
);
1203 if (subreq
== NULL
) {
1206 tevent_req_set_callback(subreq
, rpc_api_pipe_req_write_done
,
1212 tevent_req_nterror(req
, status
);
1213 return tevent_req_post(req
, ev
);
1219 static NTSTATUS
prepare_next_frag(struct rpc_api_pipe_req_state
*state
,
1222 size_t data_sent_thistime
;
1229 union dcerpc_payload u
;
1231 data_left
= state
->req_data
->length
- state
->req_data_sent
;
1233 status
= dcerpc_guess_sizes(state
->cli
->auth
,
1234 DCERPC_REQUEST_LENGTH
, data_left
,
1235 state
->cli
->max_xmit_frag
,
1236 CLIENT_NDR_PADDING_SIZE
,
1237 &data_sent_thistime
,
1238 &frag_len
, &auth_len
, &pad_len
);
1239 if (!NT_STATUS_IS_OK(status
)) {
1243 if (state
->req_data_sent
== 0) {
1244 flags
= DCERPC_PFC_FLAG_FIRST
;
1247 if (data_sent_thistime
== data_left
) {
1248 flags
|= DCERPC_PFC_FLAG_LAST
;
1251 data_blob_free(&state
->rpc_out
);
1253 ZERO_STRUCT(u
.request
);
1255 u
.request
.alloc_hint
= state
->req_data
->length
;
1256 u
.request
.context_id
= 0;
1257 u
.request
.opnum
= state
->op_num
;
1259 status
= dcerpc_push_ncacn_packet(state
,
1266 if (!NT_STATUS_IS_OK(status
)) {
1270 /* explicitly set frag_len here as dcerpc_push_ncacn_packet() can't
1271 * compute it right for requests because the auth trailer is missing
1273 dcerpc_set_frag_length(&state
->rpc_out
, frag_len
);
1275 /* Copy in the data. */
1276 if (!data_blob_append(NULL
, &state
->rpc_out
,
1277 state
->req_data
->data
+ state
->req_data_sent
,
1278 data_sent_thistime
)) {
1279 return NT_STATUS_NO_MEMORY
;
1282 switch (state
->cli
->auth
->auth_level
) {
1283 case DCERPC_AUTH_LEVEL_NONE
:
1284 case DCERPC_AUTH_LEVEL_CONNECT
:
1285 case DCERPC_AUTH_LEVEL_PACKET
:
1287 case DCERPC_AUTH_LEVEL_INTEGRITY
:
1288 case DCERPC_AUTH_LEVEL_PRIVACY
:
1289 status
= dcerpc_add_auth_footer(state
->cli
->auth
, pad_len
,
1291 if (!NT_STATUS_IS_OK(status
)) {
1296 return NT_STATUS_INVALID_PARAMETER
;
1299 state
->req_data_sent
+= data_sent_thistime
;
1300 *is_last_frag
= ((flags
& DCERPC_PFC_FLAG_LAST
) != 0);
1305 static void rpc_api_pipe_req_write_done(struct tevent_req
*subreq
)
1307 struct tevent_req
*req
= tevent_req_callback_data(
1308 subreq
, struct tevent_req
);
1309 struct rpc_api_pipe_req_state
*state
= tevent_req_data(
1310 req
, struct rpc_api_pipe_req_state
);
1314 status
= rpc_write_recv(subreq
);
1315 TALLOC_FREE(subreq
);
1316 if (!NT_STATUS_IS_OK(status
)) {
1317 tevent_req_nterror(req
, status
);
1321 status
= prepare_next_frag(state
, &is_last_frag
);
1322 if (!NT_STATUS_IS_OK(status
)) {
1323 tevent_req_nterror(req
, status
);
1328 subreq
= rpc_api_pipe_send(state
, state
->ev
, state
->cli
,
1330 DCERPC_PKT_RESPONSE
,
1332 if (tevent_req_nomem(subreq
, req
)) {
1335 tevent_req_set_callback(subreq
, rpc_api_pipe_req_done
, req
);
1337 subreq
= rpc_write_send(state
, state
->ev
,
1338 state
->cli
->transport
,
1339 state
->rpc_out
.data
,
1340 state
->rpc_out
.length
);
1341 if (tevent_req_nomem(subreq
, req
)) {
1344 tevent_req_set_callback(subreq
, rpc_api_pipe_req_write_done
,
1349 static void rpc_api_pipe_req_done(struct tevent_req
*subreq
)
1351 struct tevent_req
*req
= tevent_req_callback_data(
1352 subreq
, struct tevent_req
);
1353 struct rpc_api_pipe_req_state
*state
= tevent_req_data(
1354 req
, struct rpc_api_pipe_req_state
);
1357 status
= rpc_api_pipe_recv(subreq
, state
, NULL
, &state
->reply_pdu
);
1358 TALLOC_FREE(subreq
);
1359 if (!NT_STATUS_IS_OK(status
)) {
1360 tevent_req_nterror(req
, status
);
1363 tevent_req_done(req
);
1366 NTSTATUS
rpc_api_pipe_req_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
1367 DATA_BLOB
*reply_pdu
)
1369 struct rpc_api_pipe_req_state
*state
= tevent_req_data(
1370 req
, struct rpc_api_pipe_req_state
);
1373 if (tevent_req_is_nterror(req
, &status
)) {
1375 * We always have to initialize to reply pdu, even if there is
1376 * none. The rpccli_* caller routines expect this.
1378 *reply_pdu
= data_blob_null
;
1382 /* return data to caller and assign it ownership of memory */
1383 reply_pdu
->data
= talloc_move(mem_ctx
, &state
->reply_pdu
.data
);
1384 reply_pdu
->length
= state
->reply_pdu
.length
;
1385 state
->reply_pdu
.length
= 0;
1387 return NT_STATUS_OK
;
1390 /****************************************************************************
1391 Check the rpc bind acknowledge response.
1392 ****************************************************************************/
1394 static bool check_bind_response(const struct dcerpc_bind_ack
*r
,
1395 const struct ndr_syntax_id
*transfer
)
1397 struct dcerpc_ack_ctx ctx
;
1399 if (r
->secondary_address_size
== 0) {
1400 DEBUG(4,("Ignoring length check -- ASU bug (server didn't fill in the pipe name correctly)"));
1403 if (r
->num_results
< 1 || !r
->ctx_list
) {
1407 ctx
= r
->ctx_list
[0];
1409 /* check the transfer syntax */
1410 if ((ctx
.syntax
.if_version
!= transfer
->if_version
) ||
1411 (memcmp(&ctx
.syntax
.uuid
, &transfer
->uuid
, sizeof(transfer
->uuid
)) !=0)) {
1412 DEBUG(2,("bind_rpc_pipe: transfer syntax differs\n"));
1416 if (r
->num_results
!= 0x1 || ctx
.result
!= 0) {
1417 DEBUG(2,("bind_rpc_pipe: bind denied results: %d reason: %x\n",
1418 r
->num_results
, ctx
.reason
));
1421 DEBUG(5,("check_bind_response: accepted!\n"));
1425 /*******************************************************************
1426 Creates a DCE/RPC bind authentication response.
1427 This is the packet that is sent back to the server once we
1428 have received a BIND-ACK, to finish the third leg of
1429 the authentication handshake.
1430 ********************************************************************/
1432 static NTSTATUS
create_rpc_bind_auth3(TALLOC_CTX
*mem_ctx
,
1433 struct rpc_pipe_client
*cli
,
1435 enum dcerpc_AuthType auth_type
,
1436 enum dcerpc_AuthLevel auth_level
,
1437 DATA_BLOB
*pauth_blob
,
1441 union dcerpc_payload u
;
1445 status
= dcerpc_push_dcerpc_auth(mem_ctx
,
1448 0, /* auth_pad_length */
1449 1, /* auth_context_id */
1451 &u
.auth3
.auth_info
);
1452 if (!NT_STATUS_IS_OK(status
)) {
1456 status
= dcerpc_push_ncacn_packet(mem_ctx
,
1458 DCERPC_PFC_FLAG_FIRST
|
1459 DCERPC_PFC_FLAG_LAST
,
1464 data_blob_free(&u
.auth3
.auth_info
);
1465 if (!NT_STATUS_IS_OK(status
)) {
1466 DEBUG(0,("create_bind_or_alt_ctx_internal: failed to marshall RPC_HDR_RB.\n"));
1470 return NT_STATUS_OK
;
1473 /*******************************************************************
1474 Creates a DCE/RPC bind alter context authentication request which
1475 may contain a spnego auth blobl
1476 ********************************************************************/
1478 static NTSTATUS
create_rpc_alter_context(TALLOC_CTX
*mem_ctx
,
1479 enum dcerpc_AuthType auth_type
,
1480 enum dcerpc_AuthLevel auth_level
,
1482 const struct ndr_syntax_id
*abstract
,
1483 const struct ndr_syntax_id
*transfer
,
1484 const DATA_BLOB
*pauth_blob
, /* spnego auth blob already created. */
1487 DATA_BLOB auth_info
;
1490 status
= dcerpc_push_dcerpc_auth(mem_ctx
,
1493 0, /* auth_pad_length */
1494 1, /* auth_context_id */
1497 if (!NT_STATUS_IS_OK(status
)) {
1501 status
= create_bind_or_alt_ctx_internal(mem_ctx
,
1508 data_blob_free(&auth_info
);
1512 /****************************************************************************
1514 ****************************************************************************/
1516 struct rpc_pipe_bind_state
{
1517 struct tevent_context
*ev
;
1518 struct rpc_pipe_client
*cli
;
1521 uint32_t rpc_call_id
;
1524 static void rpc_pipe_bind_step_one_done(struct tevent_req
*subreq
);
1525 static NTSTATUS
rpc_bind_next_send(struct tevent_req
*req
,
1526 struct rpc_pipe_bind_state
*state
,
1527 DATA_BLOB
*credentials
);
1528 static NTSTATUS
rpc_bind_finish_send(struct tevent_req
*req
,
1529 struct rpc_pipe_bind_state
*state
,
1530 DATA_BLOB
*credentials
);
1532 struct tevent_req
*rpc_pipe_bind_send(TALLOC_CTX
*mem_ctx
,
1533 struct tevent_context
*ev
,
1534 struct rpc_pipe_client
*cli
,
1535 struct pipe_auth_data
*auth
)
1537 struct tevent_req
*req
, *subreq
;
1538 struct rpc_pipe_bind_state
*state
;
1541 req
= tevent_req_create(mem_ctx
, &state
, struct rpc_pipe_bind_state
);
1546 DEBUG(5,("Bind RPC Pipe: %s auth_type %u, auth_level %u\n",
1547 rpccli_pipe_txt(talloc_tos(), cli
),
1548 (unsigned int)auth
->auth_type
,
1549 (unsigned int)auth
->auth_level
));
1553 state
->rpc_call_id
= get_rpc_call_id();
1555 cli
->auth
= talloc_move(cli
, &auth
);
1557 /* Marshall the outgoing data. */
1558 status
= create_rpc_bind_req(state
, cli
,
1561 &cli
->abstract_syntax
,
1562 &cli
->transfer_syntax
,
1565 if (!NT_STATUS_IS_OK(status
) &&
1566 !NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1570 subreq
= rpc_api_pipe_send(state
, ev
, cli
, &state
->rpc_out
,
1571 DCERPC_PKT_BIND_ACK
, state
->rpc_call_id
);
1572 if (subreq
== NULL
) {
1575 tevent_req_set_callback(subreq
, rpc_pipe_bind_step_one_done
, req
);
1579 tevent_req_nterror(req
, status
);
1580 return tevent_req_post(req
, ev
);
1586 static void rpc_pipe_bind_step_one_done(struct tevent_req
*subreq
)
1588 struct tevent_req
*req
= tevent_req_callback_data(
1589 subreq
, struct tevent_req
);
1590 struct rpc_pipe_bind_state
*state
= tevent_req_data(
1591 req
, struct rpc_pipe_bind_state
);
1592 struct pipe_auth_data
*pauth
= state
->cli
->auth
;
1593 struct gensec_security
*gensec_security
;
1594 struct ncacn_packet
*pkt
= NULL
;
1595 struct dcerpc_auth auth
;
1596 DATA_BLOB auth_token
= data_blob_null
;
1599 status
= rpc_api_pipe_recv(subreq
, talloc_tos(), &pkt
, NULL
);
1600 TALLOC_FREE(subreq
);
1601 if (!NT_STATUS_IS_OK(status
)) {
1602 DEBUG(3, ("rpc_pipe_bind: %s bind request returned %s\n",
1603 rpccli_pipe_txt(talloc_tos(), state
->cli
),
1604 nt_errstr(status
)));
1605 tevent_req_nterror(req
, status
);
1610 tevent_req_done(req
);
1614 if (!check_bind_response(&pkt
->u
.bind_ack
, &state
->cli
->transfer_syntax
)) {
1615 DEBUG(2, ("rpc_pipe_bind: check_bind_response failed.\n"));
1616 tevent_req_nterror(req
, NT_STATUS_BUFFER_TOO_SMALL
);
1620 state
->cli
->max_xmit_frag
= pkt
->u
.bind_ack
.max_xmit_frag
;
1621 state
->cli
->max_recv_frag
= pkt
->u
.bind_ack
.max_recv_frag
;
1623 switch(pauth
->auth_type
) {
1625 case DCERPC_AUTH_TYPE_NONE
:
1626 case DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM
:
1627 case DCERPC_AUTH_TYPE_SCHANNEL
:
1628 /* Bind complete. */
1629 tevent_req_done(req
);
1632 case DCERPC_AUTH_TYPE_NTLMSSP
:
1633 case DCERPC_AUTH_TYPE_SPNEGO
:
1634 case DCERPC_AUTH_TYPE_KRB5
:
1635 /* Paranoid lenght checks */
1636 if (pkt
->frag_length
< DCERPC_AUTH_TRAILER_LENGTH
1637 + pkt
->auth_length
) {
1638 tevent_req_nterror(req
,
1639 NT_STATUS_INFO_LENGTH_MISMATCH
);
1642 /* get auth credentials */
1643 status
= dcerpc_pull_dcerpc_auth(talloc_tos(),
1644 &pkt
->u
.bind_ack
.auth_info
,
1646 if (!NT_STATUS_IS_OK(status
)) {
1647 DEBUG(0, ("Failed to pull dcerpc auth: %s.\n",
1648 nt_errstr(status
)));
1649 tevent_req_nterror(req
, status
);
1659 * For authenticated binds we may need to do 3 or 4 leg binds.
1662 switch(pauth
->auth_type
) {
1664 case DCERPC_AUTH_TYPE_NONE
:
1665 case DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM
:
1666 case DCERPC_AUTH_TYPE_SCHANNEL
:
1667 /* Bind complete. */
1668 tevent_req_done(req
);
1671 case DCERPC_AUTH_TYPE_NTLMSSP
:
1672 case DCERPC_AUTH_TYPE_KRB5
:
1673 case DCERPC_AUTH_TYPE_SPNEGO
:
1674 gensec_security
= talloc_get_type_abort(pauth
->auth_ctx
,
1675 struct gensec_security
);
1676 status
= gensec_update(gensec_security
, state
, NULL
,
1677 auth
.credentials
, &auth_token
);
1678 if (NT_STATUS_EQUAL(status
,
1679 NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1680 status
= rpc_bind_next_send(req
, state
,
1682 } else if (NT_STATUS_IS_OK(status
)) {
1683 if (auth_token
.length
== 0) {
1684 /* Bind complete. */
1685 tevent_req_done(req
);
1688 status
= rpc_bind_finish_send(req
, state
,
1697 if (!NT_STATUS_IS_OK(status
)) {
1698 tevent_req_nterror(req
, status
);
1703 DEBUG(0,("cli_finish_bind_auth: unknown auth type %u\n",
1704 (unsigned int)state
->cli
->auth
->auth_type
));
1705 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
1708 static NTSTATUS
rpc_bind_next_send(struct tevent_req
*req
,
1709 struct rpc_pipe_bind_state
*state
,
1710 DATA_BLOB
*auth_token
)
1712 struct pipe_auth_data
*auth
= state
->cli
->auth
;
1713 struct tevent_req
*subreq
;
1716 /* Now prepare the alter context pdu. */
1717 data_blob_free(&state
->rpc_out
);
1719 status
= create_rpc_alter_context(state
,
1723 &state
->cli
->abstract_syntax
,
1724 &state
->cli
->transfer_syntax
,
1727 if (!NT_STATUS_IS_OK(status
)) {
1731 subreq
= rpc_api_pipe_send(state
, state
->ev
, state
->cli
,
1732 &state
->rpc_out
, DCERPC_PKT_ALTER_RESP
,
1733 state
->rpc_call_id
);
1734 if (subreq
== NULL
) {
1735 return NT_STATUS_NO_MEMORY
;
1737 tevent_req_set_callback(subreq
, rpc_pipe_bind_step_one_done
, req
);
1738 return NT_STATUS_OK
;
1741 static NTSTATUS
rpc_bind_finish_send(struct tevent_req
*req
,
1742 struct rpc_pipe_bind_state
*state
,
1743 DATA_BLOB
*auth_token
)
1745 struct pipe_auth_data
*auth
= state
->cli
->auth
;
1746 struct tevent_req
*subreq
;
1749 state
->auth3
= true;
1751 /* Now prepare the auth3 context pdu. */
1752 data_blob_free(&state
->rpc_out
);
1754 status
= create_rpc_bind_auth3(state
, state
->cli
,
1760 if (!NT_STATUS_IS_OK(status
)) {
1764 subreq
= rpc_api_pipe_send(state
, state
->ev
, state
->cli
,
1765 &state
->rpc_out
, DCERPC_PKT_AUTH3
,
1766 state
->rpc_call_id
);
1767 if (subreq
== NULL
) {
1768 return NT_STATUS_NO_MEMORY
;
1770 tevent_req_set_callback(subreq
, rpc_pipe_bind_step_one_done
, req
);
1771 return NT_STATUS_OK
;
1774 NTSTATUS
rpc_pipe_bind_recv(struct tevent_req
*req
)
1776 return tevent_req_simple_recv_ntstatus(req
);
1779 NTSTATUS
rpc_pipe_bind(struct rpc_pipe_client
*cli
,
1780 struct pipe_auth_data
*auth
)
1782 TALLOC_CTX
*frame
= talloc_stackframe();
1783 struct tevent_context
*ev
;
1784 struct tevent_req
*req
;
1785 NTSTATUS status
= NT_STATUS_OK
;
1787 ev
= samba_tevent_context_init(frame
);
1789 status
= NT_STATUS_NO_MEMORY
;
1793 req
= rpc_pipe_bind_send(frame
, ev
, cli
, auth
);
1795 status
= NT_STATUS_NO_MEMORY
;
1799 if (!tevent_req_poll(req
, ev
)) {
1800 status
= map_nt_error_from_unix(errno
);
1804 status
= rpc_pipe_bind_recv(req
);
1810 #define RPCCLI_DEFAULT_TIMEOUT 10000 /* 10 seconds. */
1812 unsigned int rpccli_set_timeout(struct rpc_pipe_client
*rpc_cli
,
1813 unsigned int timeout
)
1817 if (rpc_cli
->transport
== NULL
) {
1818 return RPCCLI_DEFAULT_TIMEOUT
;
1821 if (rpc_cli
->transport
->set_timeout
== NULL
) {
1822 return RPCCLI_DEFAULT_TIMEOUT
;
1825 old
= rpc_cli
->transport
->set_timeout(rpc_cli
->transport
->priv
, timeout
);
1827 return RPCCLI_DEFAULT_TIMEOUT
;
1833 bool rpccli_is_connected(struct rpc_pipe_client
*rpc_cli
)
1835 if (rpc_cli
== NULL
) {
1839 if (rpc_cli
->transport
== NULL
) {
1843 return rpc_cli
->transport
->is_connected(rpc_cli
->transport
->priv
);
1846 struct rpccli_bh_state
{
1847 struct rpc_pipe_client
*rpc_cli
;
1850 static bool rpccli_bh_is_connected(struct dcerpc_binding_handle
*h
)
1852 struct rpccli_bh_state
*hs
= dcerpc_binding_handle_data(h
,
1853 struct rpccli_bh_state
);
1855 return rpccli_is_connected(hs
->rpc_cli
);
1858 static uint32_t rpccli_bh_set_timeout(struct dcerpc_binding_handle
*h
,
1861 struct rpccli_bh_state
*hs
= dcerpc_binding_handle_data(h
,
1862 struct rpccli_bh_state
);
1864 return rpccli_set_timeout(hs
->rpc_cli
, timeout
);
1867 static void rpccli_bh_auth_info(struct dcerpc_binding_handle
*h
,
1868 enum dcerpc_AuthType
*auth_type
,
1869 enum dcerpc_AuthLevel
*auth_level
)
1871 struct rpccli_bh_state
*hs
= dcerpc_binding_handle_data(h
,
1872 struct rpccli_bh_state
);
1874 if (hs
->rpc_cli
== NULL
) {
1878 if (hs
->rpc_cli
->auth
== NULL
) {
1882 *auth_type
= hs
->rpc_cli
->auth
->auth_type
;
1883 *auth_level
= hs
->rpc_cli
->auth
->auth_level
;
1886 struct rpccli_bh_raw_call_state
{
1892 static void rpccli_bh_raw_call_done(struct tevent_req
*subreq
);
1894 static struct tevent_req
*rpccli_bh_raw_call_send(TALLOC_CTX
*mem_ctx
,
1895 struct tevent_context
*ev
,
1896 struct dcerpc_binding_handle
*h
,
1897 const struct GUID
*object
,
1900 const uint8_t *in_data
,
1903 struct rpccli_bh_state
*hs
= dcerpc_binding_handle_data(h
,
1904 struct rpccli_bh_state
);
1905 struct tevent_req
*req
;
1906 struct rpccli_bh_raw_call_state
*state
;
1908 struct tevent_req
*subreq
;
1910 req
= tevent_req_create(mem_ctx
, &state
,
1911 struct rpccli_bh_raw_call_state
);
1915 state
->in_data
.data
= discard_const_p(uint8_t, in_data
);
1916 state
->in_data
.length
= in_length
;
1918 ok
= rpccli_bh_is_connected(h
);
1920 tevent_req_nterror(req
, NT_STATUS_CONNECTION_DISCONNECTED
);
1921 return tevent_req_post(req
, ev
);
1924 subreq
= rpc_api_pipe_req_send(state
, ev
, hs
->rpc_cli
,
1925 opnum
, &state
->in_data
);
1926 if (tevent_req_nomem(subreq
, req
)) {
1927 return tevent_req_post(req
, ev
);
1929 tevent_req_set_callback(subreq
, rpccli_bh_raw_call_done
, req
);
1934 static void rpccli_bh_raw_call_done(struct tevent_req
*subreq
)
1936 struct tevent_req
*req
=
1937 tevent_req_callback_data(subreq
,
1939 struct rpccli_bh_raw_call_state
*state
=
1940 tevent_req_data(req
,
1941 struct rpccli_bh_raw_call_state
);
1944 state
->out_flags
= 0;
1946 /* TODO: support bigendian responses */
1948 status
= rpc_api_pipe_req_recv(subreq
, state
, &state
->out_data
);
1949 TALLOC_FREE(subreq
);
1950 if (!NT_STATUS_IS_OK(status
)) {
1951 tevent_req_nterror(req
, status
);
1955 tevent_req_done(req
);
1958 static NTSTATUS
rpccli_bh_raw_call_recv(struct tevent_req
*req
,
1959 TALLOC_CTX
*mem_ctx
,
1962 uint32_t *out_flags
)
1964 struct rpccli_bh_raw_call_state
*state
=
1965 tevent_req_data(req
,
1966 struct rpccli_bh_raw_call_state
);
1969 if (tevent_req_is_nterror(req
, &status
)) {
1970 tevent_req_received(req
);
1974 *out_data
= talloc_move(mem_ctx
, &state
->out_data
.data
);
1975 *out_length
= state
->out_data
.length
;
1976 *out_flags
= state
->out_flags
;
1977 tevent_req_received(req
);
1978 return NT_STATUS_OK
;
1981 struct rpccli_bh_disconnect_state
{
1985 static struct tevent_req
*rpccli_bh_disconnect_send(TALLOC_CTX
*mem_ctx
,
1986 struct tevent_context
*ev
,
1987 struct dcerpc_binding_handle
*h
)
1989 struct rpccli_bh_state
*hs
= dcerpc_binding_handle_data(h
,
1990 struct rpccli_bh_state
);
1991 struct tevent_req
*req
;
1992 struct rpccli_bh_disconnect_state
*state
;
1995 req
= tevent_req_create(mem_ctx
, &state
,
1996 struct rpccli_bh_disconnect_state
);
2001 ok
= rpccli_bh_is_connected(h
);
2003 tevent_req_nterror(req
, NT_STATUS_CONNECTION_DISCONNECTED
);
2004 return tevent_req_post(req
, ev
);
2008 * TODO: do a real async disconnect ...
2010 * For now the caller needs to free rpc_cli
2014 tevent_req_done(req
);
2015 return tevent_req_post(req
, ev
);
2018 static NTSTATUS
rpccli_bh_disconnect_recv(struct tevent_req
*req
)
2022 if (tevent_req_is_nterror(req
, &status
)) {
2023 tevent_req_received(req
);
2027 tevent_req_received(req
);
2028 return NT_STATUS_OK
;
2031 static bool rpccli_bh_ref_alloc(struct dcerpc_binding_handle
*h
)
2036 static void rpccli_bh_do_ndr_print(struct dcerpc_binding_handle
*h
,
2038 const void *_struct_ptr
,
2039 const struct ndr_interface_call
*call
)
2041 void *struct_ptr
= discard_const(_struct_ptr
);
2043 if (DEBUGLEVEL
< 10) {
2047 if (ndr_flags
& NDR_IN
) {
2048 ndr_print_function_debug(call
->ndr_print
,
2053 if (ndr_flags
& NDR_OUT
) {
2054 ndr_print_function_debug(call
->ndr_print
,
2061 static const struct dcerpc_binding_handle_ops rpccli_bh_ops
= {
2063 .is_connected
= rpccli_bh_is_connected
,
2064 .set_timeout
= rpccli_bh_set_timeout
,
2065 .auth_info
= rpccli_bh_auth_info
,
2066 .raw_call_send
= rpccli_bh_raw_call_send
,
2067 .raw_call_recv
= rpccli_bh_raw_call_recv
,
2068 .disconnect_send
= rpccli_bh_disconnect_send
,
2069 .disconnect_recv
= rpccli_bh_disconnect_recv
,
2071 .ref_alloc
= rpccli_bh_ref_alloc
,
2072 .do_ndr_print
= rpccli_bh_do_ndr_print
,
2075 /* initialise a rpc_pipe_client binding handle */
2076 struct dcerpc_binding_handle
*rpccli_bh_create(struct rpc_pipe_client
*c
,
2077 const struct GUID
*object
,
2078 const struct ndr_interface_table
*table
)
2080 struct dcerpc_binding_handle
*h
;
2081 struct rpccli_bh_state
*hs
;
2083 h
= dcerpc_binding_handle_create(c
,
2088 struct rpccli_bh_state
,
2098 NTSTATUS
rpccli_ncalrpc_bind_data(TALLOC_CTX
*mem_ctx
,
2099 struct pipe_auth_data
**presult
)
2101 struct pipe_auth_data
*result
;
2103 result
= talloc(mem_ctx
, struct pipe_auth_data
);
2104 if (result
== NULL
) {
2105 return NT_STATUS_NO_MEMORY
;
2108 result
->auth_type
= DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM
;
2109 result
->auth_level
= DCERPC_AUTH_LEVEL_CONNECT
;
2111 result
->user_name
= talloc_strdup(result
, "");
2112 result
->domain
= talloc_strdup(result
, "");
2113 if ((result
->user_name
== NULL
) || (result
->domain
== NULL
)) {
2114 TALLOC_FREE(result
);
2115 return NT_STATUS_NO_MEMORY
;
2119 return NT_STATUS_OK
;
2122 NTSTATUS
rpccli_anon_bind_data(TALLOC_CTX
*mem_ctx
,
2123 struct pipe_auth_data
**presult
)
2125 struct pipe_auth_data
*result
;
2127 result
= talloc(mem_ctx
, struct pipe_auth_data
);
2128 if (result
== NULL
) {
2129 return NT_STATUS_NO_MEMORY
;
2132 result
->auth_type
= DCERPC_AUTH_TYPE_NONE
;
2133 result
->auth_level
= DCERPC_AUTH_LEVEL_NONE
;
2135 result
->user_name
= talloc_strdup(result
, "");
2136 result
->domain
= talloc_strdup(result
, "");
2137 if ((result
->user_name
== NULL
) || (result
->domain
== NULL
)) {
2138 TALLOC_FREE(result
);
2139 return NT_STATUS_NO_MEMORY
;
2143 return NT_STATUS_OK
;
2146 static NTSTATUS
rpccli_generic_bind_data(TALLOC_CTX
*mem_ctx
,
2147 enum dcerpc_AuthType auth_type
,
2148 enum dcerpc_AuthLevel auth_level
,
2150 const char *target_service
,
2152 const char *username
,
2153 const char *password
,
2154 enum credentials_use_kerberos use_kerberos
,
2155 struct netlogon_creds_CredentialState
*creds
,
2156 struct pipe_auth_data
**presult
)
2158 struct auth_generic_state
*auth_generic_ctx
;
2159 struct pipe_auth_data
*result
;
2162 result
= talloc(mem_ctx
, struct pipe_auth_data
);
2163 if (result
== NULL
) {
2164 return NT_STATUS_NO_MEMORY
;
2167 result
->auth_type
= auth_type
;
2168 result
->auth_level
= auth_level
;
2170 result
->user_name
= talloc_strdup(result
, username
);
2171 result
->domain
= talloc_strdup(result
, domain
);
2172 if ((result
->user_name
== NULL
) || (result
->domain
== NULL
)) {
2173 status
= NT_STATUS_NO_MEMORY
;
2177 status
= auth_generic_client_prepare(result
,
2179 if (!NT_STATUS_IS_OK(status
)) {
2183 status
= auth_generic_set_username(auth_generic_ctx
, username
);
2184 if (!NT_STATUS_IS_OK(status
)) {
2188 status
= auth_generic_set_domain(auth_generic_ctx
, domain
);
2189 if (!NT_STATUS_IS_OK(status
)) {
2193 status
= auth_generic_set_password(auth_generic_ctx
, password
);
2194 if (!NT_STATUS_IS_OK(status
)) {
2198 status
= gensec_set_target_service(auth_generic_ctx
->gensec_security
, target_service
);
2199 if (!NT_STATUS_IS_OK(status
)) {
2203 status
= gensec_set_target_hostname(auth_generic_ctx
->gensec_security
, server
);
2204 if (!NT_STATUS_IS_OK(status
)) {
2208 cli_credentials_set_kerberos_state(auth_generic_ctx
->credentials
, use_kerberos
);
2209 cli_credentials_set_netlogon_creds(auth_generic_ctx
->credentials
, creds
);
2211 status
= auth_generic_client_start_by_authtype(auth_generic_ctx
, auth_type
, auth_level
);
2212 if (!NT_STATUS_IS_OK(status
)) {
2216 result
->auth_ctx
= talloc_move(result
, &auth_generic_ctx
->gensec_security
);
2217 talloc_free(auth_generic_ctx
);
2219 return NT_STATUS_OK
;
2222 TALLOC_FREE(result
);
2227 * Create an rpc pipe client struct, connecting to a tcp port.
2229 static NTSTATUS
rpc_pipe_open_tcp_port(TALLOC_CTX
*mem_ctx
, const char *host
,
2230 const struct sockaddr_storage
*ss_addr
,
2232 const struct ndr_interface_table
*table
,
2233 struct rpc_pipe_client
**presult
)
2235 struct rpc_pipe_client
*result
;
2236 struct sockaddr_storage addr
;
2240 result
= talloc_zero(mem_ctx
, struct rpc_pipe_client
);
2241 if (result
== NULL
) {
2242 return NT_STATUS_NO_MEMORY
;
2245 result
->abstract_syntax
= table
->syntax_id
;
2246 result
->transfer_syntax
= ndr_transfer_syntax_ndr
;
2248 result
->desthost
= talloc_strdup(result
, host
);
2249 result
->srv_name_slash
= talloc_asprintf_strupper_m(
2250 result
, "\\\\%s", result
->desthost
);
2251 if ((result
->desthost
== NULL
) || (result
->srv_name_slash
== NULL
)) {
2252 status
= NT_STATUS_NO_MEMORY
;
2256 result
->max_xmit_frag
= RPC_MAX_PDU_FRAG_LEN
;
2257 result
->max_recv_frag
= RPC_MAX_PDU_FRAG_LEN
;
2259 if (ss_addr
== NULL
) {
2260 if (!resolve_name(host
, &addr
, NBT_NAME_SERVER
, false)) {
2261 status
= NT_STATUS_NOT_FOUND
;
2268 status
= open_socket_out(&addr
, port
, 60*1000, &fd
);
2269 if (!NT_STATUS_IS_OK(status
)) {
2272 set_socket_options(fd
, lp_socket_options());
2274 status
= rpc_transport_sock_init(result
, fd
, &result
->transport
);
2275 if (!NT_STATUS_IS_OK(status
)) {
2280 result
->transport
->transport
= NCACN_IP_TCP
;
2282 result
->binding_handle
= rpccli_bh_create(result
, NULL
, table
);
2283 if (result
->binding_handle
== NULL
) {
2284 TALLOC_FREE(result
);
2285 return NT_STATUS_NO_MEMORY
;
2289 return NT_STATUS_OK
;
2292 TALLOC_FREE(result
);
2297 * Determine the tcp port on which a dcerpc interface is listening
2298 * for the ncacn_ip_tcp transport via the endpoint mapper of the
2301 static NTSTATUS
rpc_pipe_get_tcp_port(const char *host
,
2302 const struct sockaddr_storage
*addr
,
2303 const struct ndr_interface_table
*table
,
2307 struct rpc_pipe_client
*epm_pipe
= NULL
;
2308 struct dcerpc_binding_handle
*epm_handle
= NULL
;
2309 struct pipe_auth_data
*auth
= NULL
;
2310 struct dcerpc_binding
*map_binding
= NULL
;
2311 struct dcerpc_binding
*res_binding
= NULL
;
2312 struct epm_twr_t
*map_tower
= NULL
;
2313 struct epm_twr_t
*res_towers
= NULL
;
2314 struct policy_handle
*entry_handle
= NULL
;
2315 uint32_t num_towers
= 0;
2316 uint32_t max_towers
= 1;
2317 struct epm_twr_p_t towers
;
2318 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
2319 uint32_t result
= 0;
2321 if (pport
== NULL
) {
2322 status
= NT_STATUS_INVALID_PARAMETER
;
2326 if (ndr_syntax_id_equal(&table
->syntax_id
,
2327 &ndr_table_epmapper
.syntax_id
)) {
2329 status
= NT_STATUS_OK
;
2333 /* open the connection to the endpoint mapper */
2334 status
= rpc_pipe_open_tcp_port(tmp_ctx
, host
, addr
, 135,
2335 &ndr_table_epmapper
,
2338 if (!NT_STATUS_IS_OK(status
)) {
2341 epm_handle
= epm_pipe
->binding_handle
;
2343 status
= rpccli_anon_bind_data(tmp_ctx
, &auth
);
2344 if (!NT_STATUS_IS_OK(status
)) {
2348 status
= rpc_pipe_bind(epm_pipe
, auth
);
2349 if (!NT_STATUS_IS_OK(status
)) {
2353 /* create tower for asking the epmapper */
2355 map_binding
= talloc_zero(tmp_ctx
, struct dcerpc_binding
);
2356 if (map_binding
== NULL
) {
2357 status
= NT_STATUS_NO_MEMORY
;
2361 map_binding
->transport
= NCACN_IP_TCP
;
2362 map_binding
->object
= table
->syntax_id
;
2363 map_binding
->host
= host
; /* needed? */
2364 map_binding
->endpoint
= "0"; /* correct? needed? */
2366 map_tower
= talloc_zero(tmp_ctx
, struct epm_twr_t
);
2367 if (map_tower
== NULL
) {
2368 status
= NT_STATUS_NO_MEMORY
;
2372 status
= dcerpc_binding_build_tower(tmp_ctx
, map_binding
,
2373 &(map_tower
->tower
));
2374 if (!NT_STATUS_IS_OK(status
)) {
2378 /* allocate further parameters for the epm_Map call */
2380 res_towers
= talloc_array(tmp_ctx
, struct epm_twr_t
, max_towers
);
2381 if (res_towers
== NULL
) {
2382 status
= NT_STATUS_NO_MEMORY
;
2385 towers
.twr
= res_towers
;
2387 entry_handle
= talloc_zero(tmp_ctx
, struct policy_handle
);
2388 if (entry_handle
== NULL
) {
2389 status
= NT_STATUS_NO_MEMORY
;
2393 /* ask the endpoint mapper for the port */
2395 status
= dcerpc_epm_Map(epm_handle
,
2397 discard_const_p(struct GUID
,
2398 &(table
->syntax_id
.uuid
)),
2406 if (!NT_STATUS_IS_OK(status
)) {
2410 if (result
!= EPMAPPER_STATUS_OK
) {
2411 status
= NT_STATUS_UNSUCCESSFUL
;
2415 if (num_towers
!= 1) {
2416 status
= NT_STATUS_UNSUCCESSFUL
;
2420 /* extract the port from the answer */
2422 status
= dcerpc_binding_from_tower(tmp_ctx
,
2423 &(towers
.twr
->tower
),
2425 if (!NT_STATUS_IS_OK(status
)) {
2429 /* are further checks here necessary? */
2430 if (res_binding
->transport
!= NCACN_IP_TCP
) {
2431 status
= NT_STATUS_UNSUCCESSFUL
;
2435 *pport
= (uint16_t)atoi(res_binding
->endpoint
);
2438 TALLOC_FREE(tmp_ctx
);
2443 * Create a rpc pipe client struct, connecting to a host via tcp.
2444 * The port is determined by asking the endpoint mapper on the given
2447 NTSTATUS
rpc_pipe_open_tcp(TALLOC_CTX
*mem_ctx
, const char *host
,
2448 const struct sockaddr_storage
*addr
,
2449 const struct ndr_interface_table
*table
,
2450 struct rpc_pipe_client
**presult
)
2455 status
= rpc_pipe_get_tcp_port(host
, addr
, table
, &port
);
2456 if (!NT_STATUS_IS_OK(status
)) {
2460 return rpc_pipe_open_tcp_port(mem_ctx
, host
, addr
, port
,
2464 /********************************************************************
2465 Create a rpc pipe client struct, connecting to a unix domain socket
2466 ********************************************************************/
2467 NTSTATUS
rpc_pipe_open_ncalrpc(TALLOC_CTX
*mem_ctx
, const char *socket_path
,
2468 const struct ndr_interface_table
*table
,
2469 struct rpc_pipe_client
**presult
)
2471 struct rpc_pipe_client
*result
;
2472 struct sockaddr_un addr
;
2477 result
= talloc_zero(mem_ctx
, struct rpc_pipe_client
);
2478 if (result
== NULL
) {
2479 return NT_STATUS_NO_MEMORY
;
2482 result
->abstract_syntax
= table
->syntax_id
;
2483 result
->transfer_syntax
= ndr_transfer_syntax_ndr
;
2485 result
->desthost
= get_myname(result
);
2486 result
->srv_name_slash
= talloc_asprintf_strupper_m(
2487 result
, "\\\\%s", result
->desthost
);
2488 if ((result
->desthost
== NULL
) || (result
->srv_name_slash
== NULL
)) {
2489 status
= NT_STATUS_NO_MEMORY
;
2493 result
->max_xmit_frag
= RPC_MAX_PDU_FRAG_LEN
;
2494 result
->max_recv_frag
= RPC_MAX_PDU_FRAG_LEN
;
2496 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
2498 status
= map_nt_error_from_unix(errno
);
2503 addr
.sun_family
= AF_UNIX
;
2504 strlcpy(addr
.sun_path
, socket_path
, sizeof(addr
.sun_path
));
2505 salen
= sizeof(struct sockaddr_un
);
2507 if (connect(fd
, (struct sockaddr
*)(void *)&addr
, salen
) == -1) {
2508 DEBUG(0, ("connect(%s) failed: %s\n", socket_path
,
2511 return map_nt_error_from_unix(errno
);
2514 status
= rpc_transport_sock_init(result
, fd
, &result
->transport
);
2515 if (!NT_STATUS_IS_OK(status
)) {
2520 result
->transport
->transport
= NCALRPC
;
2522 result
->binding_handle
= rpccli_bh_create(result
, NULL
, table
);
2523 if (result
->binding_handle
== NULL
) {
2524 TALLOC_FREE(result
);
2525 return NT_STATUS_NO_MEMORY
;
2529 return NT_STATUS_OK
;
2532 TALLOC_FREE(result
);
2536 struct rpc_pipe_client_np_ref
{
2537 struct cli_state
*cli
;
2538 struct rpc_pipe_client
*pipe
;
2541 static int rpc_pipe_client_np_ref_destructor(struct rpc_pipe_client_np_ref
*np_ref
)
2543 DLIST_REMOVE(np_ref
->cli
->pipe_list
, np_ref
->pipe
);
2547 /****************************************************************************
2548 Open a named pipe over SMB to a remote server.
2550 * CAVEAT CALLER OF THIS FUNCTION:
2551 * The returned rpc_pipe_client saves a copy of the cli_state cli pointer,
2552 * so be sure that this function is called AFTER any structure (vs pointer)
2553 * assignment of the cli. In particular, libsmbclient does structure
2554 * assignments of cli, which invalidates the data in the returned
2555 * rpc_pipe_client if this function is called before the structure assignment
2558 ****************************************************************************/
2560 static NTSTATUS
rpc_pipe_open_np(struct cli_state
*cli
,
2561 const struct ndr_interface_table
*table
,
2562 struct rpc_pipe_client
**presult
)
2564 struct rpc_pipe_client
*result
;
2566 struct rpc_pipe_client_np_ref
*np_ref
;
2568 /* sanity check to protect against crashes */
2571 return NT_STATUS_INVALID_HANDLE
;
2574 result
= talloc_zero(NULL
, struct rpc_pipe_client
);
2575 if (result
== NULL
) {
2576 return NT_STATUS_NO_MEMORY
;
2579 result
->abstract_syntax
= table
->syntax_id
;
2580 result
->transfer_syntax
= ndr_transfer_syntax_ndr
;
2581 result
->desthost
= talloc_strdup(result
, smbXcli_conn_remote_name(cli
->conn
));
2582 result
->srv_name_slash
= talloc_asprintf_strupper_m(
2583 result
, "\\\\%s", result
->desthost
);
2585 result
->max_xmit_frag
= RPC_MAX_PDU_FRAG_LEN
;
2586 result
->max_recv_frag
= RPC_MAX_PDU_FRAG_LEN
;
2588 if ((result
->desthost
== NULL
) || (result
->srv_name_slash
== NULL
)) {
2589 TALLOC_FREE(result
);
2590 return NT_STATUS_NO_MEMORY
;
2593 status
= rpc_transport_np_init(result
, cli
, table
,
2594 &result
->transport
);
2595 if (!NT_STATUS_IS_OK(status
)) {
2596 TALLOC_FREE(result
);
2600 result
->transport
->transport
= NCACN_NP
;
2602 np_ref
= talloc(result
->transport
, struct rpc_pipe_client_np_ref
);
2603 if (np_ref
== NULL
) {
2604 TALLOC_FREE(result
);
2605 return NT_STATUS_NO_MEMORY
;
2608 np_ref
->pipe
= result
;
2610 DLIST_ADD(np_ref
->cli
->pipe_list
, np_ref
->pipe
);
2611 talloc_set_destructor(np_ref
, rpc_pipe_client_np_ref_destructor
);
2613 result
->binding_handle
= rpccli_bh_create(result
, NULL
, table
);
2614 if (result
->binding_handle
== NULL
) {
2615 TALLOC_FREE(result
);
2616 return NT_STATUS_NO_MEMORY
;
2620 return NT_STATUS_OK
;
2623 /****************************************************************************
2624 Open a pipe to a remote server.
2625 ****************************************************************************/
2627 static NTSTATUS
cli_rpc_pipe_open(struct cli_state
*cli
,
2628 enum dcerpc_transport_t transport
,
2629 const struct ndr_interface_table
*table
,
2630 struct rpc_pipe_client
**presult
)
2632 switch (transport
) {
2634 return rpc_pipe_open_tcp(NULL
,
2635 smbXcli_conn_remote_name(cli
->conn
),
2636 smbXcli_conn_remote_sockaddr(cli
->conn
),
2639 return rpc_pipe_open_np(cli
, table
, presult
);
2641 return NT_STATUS_NOT_IMPLEMENTED
;
2645 /****************************************************************************
2646 Open a named pipe to an SMB server and bind anonymously.
2647 ****************************************************************************/
2649 NTSTATUS
cli_rpc_pipe_open_noauth_transport(struct cli_state
*cli
,
2650 enum dcerpc_transport_t transport
,
2651 const struct ndr_interface_table
*table
,
2652 struct rpc_pipe_client
**presult
)
2654 struct rpc_pipe_client
*result
;
2655 struct pipe_auth_data
*auth
;
2658 status
= cli_rpc_pipe_open(cli
, transport
, table
, &result
);
2659 if (!NT_STATUS_IS_OK(status
)) {
2663 status
= rpccli_anon_bind_data(result
, &auth
);
2664 if (!NT_STATUS_IS_OK(status
)) {
2665 DEBUG(0, ("rpccli_anon_bind_data returned %s\n",
2666 nt_errstr(status
)));
2667 TALLOC_FREE(result
);
2672 * This is a bit of an abstraction violation due to the fact that an
2673 * anonymous bind on an authenticated SMB inherits the user/domain
2674 * from the enclosing SMB creds
2677 TALLOC_FREE(auth
->user_name
);
2678 TALLOC_FREE(auth
->domain
);
2680 auth
->user_name
= talloc_strdup(auth
, cli
->user_name
);
2681 auth
->domain
= talloc_strdup(auth
, cli
->domain
);
2683 if (transport
== NCACN_NP
) {
2684 struct smbXcli_session
*session
;
2686 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
2687 session
= cli
->smb2
.session
;
2689 session
= cli
->smb1
.session
;
2692 status
= smbXcli_session_application_key(session
, auth
,
2693 &auth
->transport_session_key
);
2694 if (!NT_STATUS_IS_OK(status
)) {
2695 auth
->transport_session_key
= data_blob_null
;
2699 if ((auth
->user_name
== NULL
) || (auth
->domain
== NULL
)) {
2700 TALLOC_FREE(result
);
2701 return NT_STATUS_NO_MEMORY
;
2704 status
= rpc_pipe_bind(result
, auth
);
2705 if (!NT_STATUS_IS_OK(status
)) {
2707 if (ndr_syntax_id_equal(&table
->syntax_id
,
2708 &ndr_table_dssetup
.syntax_id
)) {
2709 /* non AD domains just don't have this pipe, avoid
2710 * level 0 statement in that case - gd */
2713 DEBUG(lvl
, ("cli_rpc_pipe_open_noauth: rpc_pipe_bind for pipe "
2714 "%s failed with error %s\n",
2716 nt_errstr(status
) ));
2717 TALLOC_FREE(result
);
2721 DEBUG(10,("cli_rpc_pipe_open_noauth: opened pipe %s to machine "
2722 "%s and bound anonymously.\n",
2727 return NT_STATUS_OK
;
2730 /****************************************************************************
2731 ****************************************************************************/
2733 NTSTATUS
cli_rpc_pipe_open_noauth(struct cli_state
*cli
,
2734 const struct ndr_interface_table
*table
,
2735 struct rpc_pipe_client
**presult
)
2737 return cli_rpc_pipe_open_noauth_transport(cli
, NCACN_NP
,
2741 /****************************************************************************
2742 Open a named pipe to an SMB server and bind using the mech specified
2743 ****************************************************************************/
2745 NTSTATUS
cli_rpc_pipe_open_generic_auth(struct cli_state
*cli
,
2746 const struct ndr_interface_table
*table
,
2747 enum dcerpc_transport_t transport
,
2748 enum dcerpc_AuthType auth_type
,
2749 enum dcerpc_AuthLevel auth_level
,
2752 const char *username
,
2753 const char *password
,
2754 struct rpc_pipe_client
**presult
)
2756 struct rpc_pipe_client
*result
;
2757 struct pipe_auth_data
*auth
= NULL
;
2758 const char *target_service
= table
->authservices
->names
[0];
2762 status
= cli_rpc_pipe_open(cli
, transport
, table
, &result
);
2763 if (!NT_STATUS_IS_OK(status
)) {
2767 status
= rpccli_generic_bind_data(result
,
2768 auth_type
, auth_level
,
2769 server
, target_service
,
2770 domain
, username
, password
,
2771 CRED_AUTO_USE_KERBEROS
,
2774 if (!NT_STATUS_IS_OK(status
)) {
2775 DEBUG(0, ("rpccli_generic_bind_data returned %s\n",
2776 nt_errstr(status
)));
2780 status
= rpc_pipe_bind(result
, auth
);
2781 if (!NT_STATUS_IS_OK(status
)) {
2782 DEBUG(0, ("cli_rpc_pipe_open_generic_auth: cli_rpc_pipe_bind failed with error %s\n",
2783 nt_errstr(status
) ));
2787 DEBUG(10,("cli_rpc_pipe_open_generic_auth: opened pipe %s to "
2788 "machine %s and bound as user %s\\%s.\n", table
->name
,
2789 result
->desthost
, domain
, username
));
2792 return NT_STATUS_OK
;
2796 TALLOC_FREE(result
);
2800 /****************************************************************************
2802 Open a named pipe to an SMB server and bind using schannel (bind type 68)
2803 using session_key. sign and seal.
2805 The *pdc will be stolen onto this new pipe
2806 ****************************************************************************/
2808 NTSTATUS
cli_rpc_pipe_open_schannel_with_key(struct cli_state
*cli
,
2809 const struct ndr_interface_table
*table
,
2810 enum dcerpc_transport_t transport
,
2811 enum dcerpc_AuthLevel auth_level
,
2813 struct netlogon_creds_CredentialState
**pdc
,
2814 struct rpc_pipe_client
**_rpccli
)
2816 struct rpc_pipe_client
*rpccli
;
2817 struct pipe_auth_data
*rpcauth
;
2820 struct netlogon_creds_CredentialState save_creds
;
2821 struct netr_Authenticator auth
;
2822 struct netr_Authenticator return_auth
;
2823 union netr_Capabilities capabilities
;
2824 const char *target_service
= table
->authservices
->names
[0];
2826 status
= cli_rpc_pipe_open(cli
, transport
, table
, &rpccli
);
2827 if (!NT_STATUS_IS_OK(status
)) {
2831 status
= rpccli_generic_bind_data(rpccli
,
2832 DCERPC_AUTH_TYPE_SCHANNEL
,
2837 (*pdc
)->computer_name
,
2839 CRED_AUTO_USE_KERBEROS
,
2842 if (!NT_STATUS_IS_OK(status
)) {
2843 DEBUG(0, ("rpccli_generic_bind_data returned %s\n",
2844 nt_errstr(status
)));
2845 TALLOC_FREE(rpccli
);
2850 * The credentials on a new netlogon pipe are the ones we are passed
2851 * in - copy them over
2853 * This may get overwritten... in rpc_pipe_bind()...
2855 rpccli
->dc
= netlogon_creds_copy(rpccli
, *pdc
);
2856 if (rpccli
->dc
== NULL
) {
2857 TALLOC_FREE(rpccli
);
2858 return NT_STATUS_NO_MEMORY
;
2861 status
= rpc_pipe_bind(rpccli
, rpcauth
);
2862 if (!NT_STATUS_IS_OK(status
)) {
2863 DEBUG(0, ("cli_rpc_pipe_open_schannel_with_key: "
2864 "cli_rpc_pipe_bind failed with error %s\n",
2865 nt_errstr(status
) ));
2866 TALLOC_FREE(rpccli
);
2870 if (!ndr_syntax_id_equal(&table
->syntax_id
, &ndr_table_netlogon
.syntax_id
)) {
2874 save_creds
= *rpccli
->dc
;
2875 ZERO_STRUCT(return_auth
);
2876 ZERO_STRUCT(capabilities
);
2878 netlogon_creds_client_authenticator(&save_creds
, &auth
);
2880 status
= dcerpc_netr_LogonGetCapabilities(rpccli
->binding_handle
,
2882 rpccli
->srv_name_slash
,
2883 save_creds
.computer_name
,
2884 &auth
, &return_auth
,
2887 if (NT_STATUS_EQUAL(status
, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE
)) {
2888 if (save_creds
.negotiate_flags
& NETLOGON_NEG_SUPPORTS_AES
) {
2889 DEBUG(5, ("AES was negotiated and the error was %s - "
2890 "downgrade detected\n",
2891 nt_errstr(status
)));
2892 TALLOC_FREE(rpccli
);
2893 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2896 /* This is probably an old Samba Version */
2897 DEBUG(5, ("We are checking against an NT or old Samba - %s\n",
2898 nt_errstr(status
)));
2902 if (!NT_STATUS_IS_OK(status
)) {
2903 DEBUG(0, ("dcerpc_netr_LogonGetCapabilities failed with %s\n",
2904 nt_errstr(status
)));
2905 TALLOC_FREE(rpccli
);
2909 if (NT_STATUS_EQUAL(result
, NT_STATUS_NOT_IMPLEMENTED
)) {
2910 if (save_creds
.negotiate_flags
& NETLOGON_NEG_SUPPORTS_AES
) {
2911 /* This means AES isn't supported. */
2912 DEBUG(5, ("AES was negotiated and the result was %s - "
2913 "downgrade detected\n",
2914 nt_errstr(result
)));
2915 TALLOC_FREE(rpccli
);
2916 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2919 /* This is probably an old Windows version */
2920 DEBUG(5, ("We are checking against an win2k3 or Samba - %s\n",
2921 nt_errstr(result
)));
2926 * We need to check the credential state here, cause win2k3 and earlier
2927 * returns NT_STATUS_NOT_IMPLEMENTED
2929 if (!netlogon_creds_client_check(&save_creds
, &return_auth
.cred
)) {
2931 * Server replied with bad credential. Fail.
2933 DEBUG(0,("cli_rpc_pipe_open_schannel_with_key: server %s "
2934 "replied with bad credential\n",
2936 TALLOC_FREE(rpccli
);
2937 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2939 *rpccli
->dc
= save_creds
;
2941 if (!NT_STATUS_IS_OK(result
)) {
2942 DEBUG(0, ("dcerpc_netr_LogonGetCapabilities failed with %s\n",
2943 nt_errstr(result
)));
2944 TALLOC_FREE(rpccli
);
2948 if (!(save_creds
.negotiate_flags
& NETLOGON_NEG_SUPPORTS_AES
)) {
2949 /* This means AES isn't supported. */
2950 DEBUG(5, ("AES is not negotiated, but netr_LogonGetCapabilities "
2951 "was OK - downgrade detected\n"));
2952 TALLOC_FREE(rpccli
);
2953 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2956 if (save_creds
.negotiate_flags
!= capabilities
.server_capabilities
) {
2957 DEBUG(0, ("The client capabilities don't match the server "
2958 "capabilities: local[0x%08X] remote[0x%08X]\n",
2959 save_creds
.negotiate_flags
,
2960 capabilities
.server_capabilities
));
2961 TALLOC_FREE(rpccli
);
2962 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2966 DEBUG(10,("cli_rpc_pipe_open_schannel_with_key: opened pipe %s to machine %s "
2967 "for domain %s and bound using schannel.\n",
2969 rpccli
->desthost
, domain
));
2972 return NT_STATUS_OK
;
2975 NTSTATUS
cli_rpc_pipe_open_spnego(struct cli_state
*cli
,
2976 const struct ndr_interface_table
*table
,
2977 enum dcerpc_transport_t transport
,
2979 enum dcerpc_AuthLevel auth_level
,
2982 const char *username
,
2983 const char *password
,
2984 struct rpc_pipe_client
**presult
)
2986 struct rpc_pipe_client
*result
;
2987 struct pipe_auth_data
*auth
= NULL
;
2988 const char *target_service
= table
->authservices
->names
[0];
2991 enum credentials_use_kerberos use_kerberos
;
2993 if (strcmp(oid
, GENSEC_OID_KERBEROS5
) == 0) {
2994 use_kerberos
= CRED_MUST_USE_KERBEROS
;
2995 } else if (strcmp(oid
, GENSEC_OID_NTLMSSP
) == 0) {
2996 use_kerberos
= CRED_DONT_USE_KERBEROS
;
2998 return NT_STATUS_INVALID_PARAMETER
;
3001 status
= cli_rpc_pipe_open(cli
, transport
, table
, &result
);
3002 if (!NT_STATUS_IS_OK(status
)) {
3006 status
= rpccli_generic_bind_data(result
,
3007 DCERPC_AUTH_TYPE_SPNEGO
, auth_level
,
3008 server
, target_service
,
3009 domain
, username
, password
,
3012 if (!NT_STATUS_IS_OK(status
)) {
3013 DEBUG(0, ("rpccli_generic_bind_data returned %s\n",
3014 nt_errstr(status
)));
3018 status
= rpc_pipe_bind(result
, auth
);
3019 if (!NT_STATUS_IS_OK(status
)) {
3020 DEBUG(0, ("cli_rpc_pipe_open_spnego: cli_rpc_pipe_bind failed with error %s\n",
3021 nt_errstr(status
) ));
3025 DEBUG(10,("cli_rpc_pipe_open_spnego: opened pipe %s to "
3026 "machine %s.\n", table
->name
,
3030 return NT_STATUS_OK
;
3034 TALLOC_FREE(result
);
3038 NTSTATUS
cli_get_session_key(TALLOC_CTX
*mem_ctx
,
3039 struct rpc_pipe_client
*cli
,
3040 DATA_BLOB
*session_key
)
3043 struct pipe_auth_data
*a
;
3044 struct gensec_security
*gensec_security
;
3045 DATA_BLOB sk
= data_blob_null
;
3046 bool make_dup
= false;
3048 if (!session_key
|| !cli
) {
3049 return NT_STATUS_INVALID_PARAMETER
;
3055 return NT_STATUS_INVALID_PARAMETER
;
3058 switch (cli
->auth
->auth_type
) {
3059 case DCERPC_AUTH_TYPE_SPNEGO
:
3060 case DCERPC_AUTH_TYPE_NTLMSSP
:
3061 case DCERPC_AUTH_TYPE_KRB5
:
3062 gensec_security
= talloc_get_type_abort(a
->auth_ctx
,
3063 struct gensec_security
);
3064 status
= gensec_session_key(gensec_security
, mem_ctx
, &sk
);
3065 if (!NT_STATUS_IS_OK(status
)) {
3070 case DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM
:
3071 case DCERPC_AUTH_TYPE_NONE
:
3072 sk
= data_blob_const(a
->transport_session_key
.data
,
3073 a
->transport_session_key
.length
);
3081 return NT_STATUS_NO_USER_SESSION_KEY
;
3085 *session_key
= data_blob_dup_talloc(mem_ctx
, sk
);
3090 return NT_STATUS_OK
;