2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Largely rewritten by Jeremy Allison 2005.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "librpc/gen_ndr/cli_epmapper.h"
22 #include "../librpc/gen_ndr/ndr_schannel.h"
23 #include "../librpc/gen_ndr/ndr_lsa.h"
24 #include "../librpc/gen_ndr/ndr_dssetup.h"
25 #include "../librpc/gen_ndr/ndr_samr.h"
26 #include "../librpc/gen_ndr/ndr_netlogon.h"
27 #include "../librpc/gen_ndr/ndr_srvsvc.h"
28 #include "../librpc/gen_ndr/ndr_wkssvc.h"
29 #include "../librpc/gen_ndr/ndr_winreg.h"
30 #include "../librpc/gen_ndr/ndr_spoolss.h"
31 #include "../librpc/gen_ndr/ndr_dfs.h"
32 #include "../librpc/gen_ndr/ndr_echo.h"
33 #include "../librpc/gen_ndr/ndr_initshutdown.h"
34 #include "../librpc/gen_ndr/ndr_svcctl.h"
35 #include "../librpc/gen_ndr/ndr_eventlog.h"
36 #include "../librpc/gen_ndr/ndr_ntsvcs.h"
37 #include "../librpc/gen_ndr/ndr_epmapper.h"
38 #include "../librpc/gen_ndr/ndr_drsuapi.h"
39 #include "../libcli/auth/schannel.h"
40 #include "../libcli/auth/spnego.h"
42 #include "../libcli/auth/ntlmssp.h"
43 #include "rpc_client/cli_netlogon.h"
44 #include "librpc/gen_ndr/ndr_dcerpc.h"
47 #define DBGC_CLASS DBGC_RPC_CLI
49 static const char *get_pipe_name_from_iface(
50 TALLOC_CTX *mem_ctx, const struct ndr_interface_table *interface)
53 const struct ndr_interface_string_array *ep = interface->endpoints;
56 for (i=0; i<ep->count; i++) {
57 if (strncmp(ep->names[i], "ncacn_np:[\\pipe\\", 16) == 0) {
66 * extract the pipe name without \\pipe from for example
67 * ncacn_np:[\\pipe\\epmapper]
69 p = strchr(ep->names[i]+15, ']');
73 return talloc_strndup(mem_ctx, ep->names[i]+15, p - ep->names[i] - 15);
76 static const struct ndr_interface_table **interfaces;
78 bool smb_register_ndr_interface(const struct ndr_interface_table *interface)
80 int num_interfaces = talloc_array_length(interfaces);
81 const struct ndr_interface_table **tmp;
84 for (i=0; i<num_interfaces; i++) {
85 if (ndr_syntax_id_equal(&interfaces[i]->syntax_id,
86 &interface->syntax_id)) {
91 tmp = talloc_realloc(NULL, interfaces,
92 const struct ndr_interface_table *,
95 DEBUG(1, ("smb_register_ndr_interface: talloc failed\n"));
99 interfaces[num_interfaces] = interface;
103 static bool initialize_interfaces(void)
105 if (!smb_register_ndr_interface(&ndr_table_lsarpc)) {
108 if (!smb_register_ndr_interface(&ndr_table_dssetup)) {
111 if (!smb_register_ndr_interface(&ndr_table_samr)) {
114 if (!smb_register_ndr_interface(&ndr_table_netlogon)) {
117 if (!smb_register_ndr_interface(&ndr_table_srvsvc)) {
120 if (!smb_register_ndr_interface(&ndr_table_wkssvc)) {
123 if (!smb_register_ndr_interface(&ndr_table_winreg)) {
126 if (!smb_register_ndr_interface(&ndr_table_spoolss)) {
129 if (!smb_register_ndr_interface(&ndr_table_netdfs)) {
132 if (!smb_register_ndr_interface(&ndr_table_rpcecho)) {
135 if (!smb_register_ndr_interface(&ndr_table_initshutdown)) {
138 if (!smb_register_ndr_interface(&ndr_table_svcctl)) {
141 if (!smb_register_ndr_interface(&ndr_table_eventlog)) {
144 if (!smb_register_ndr_interface(&ndr_table_ntsvcs)) {
147 if (!smb_register_ndr_interface(&ndr_table_epmapper)) {
150 if (!smb_register_ndr_interface(&ndr_table_drsuapi)) {
156 const struct ndr_interface_table *get_iface_from_syntax(
157 const struct ndr_syntax_id *syntax)
162 if (interfaces == NULL) {
163 if (!initialize_interfaces()) {
167 num_interfaces = talloc_array_length(interfaces);
169 for (i=0; i<num_interfaces; i++) {
170 if (ndr_syntax_id_equal(&interfaces[i]->syntax_id, syntax)) {
171 return interfaces[i];
178 /****************************************************************************
179 Return the pipe name from the interface.
180 ****************************************************************************/
182 const char *get_pipe_name_from_syntax(TALLOC_CTX *mem_ctx,
183 const struct ndr_syntax_id *syntax)
185 const struct ndr_interface_table *interface;
189 interface = get_iface_from_syntax(syntax);
190 if (interface != NULL) {
191 result = get_pipe_name_from_iface(mem_ctx, interface);
192 if (result != NULL) {
198 * Here we should ask \\epmapper, but for now our code is only
199 * interested in the known pipes mentioned in pipe_names[]
202 guid_str = GUID_string(talloc_tos(), &syntax->uuid);
203 if (guid_str == NULL) {
206 result = talloc_asprintf(mem_ctx, "Interface %s.%d", guid_str,
207 (int)syntax->if_version);
208 TALLOC_FREE(guid_str);
210 if (result == NULL) {
216 /********************************************************************
217 Map internal value to wire value.
218 ********************************************************************/
220 static int map_pipe_auth_type_to_rpc_auth_type(enum pipe_auth_type auth_type)
224 case PIPE_AUTH_TYPE_NONE:
225 return DCERPC_AUTH_TYPE_NONE;
227 case PIPE_AUTH_TYPE_NTLMSSP:
228 return DCERPC_AUTH_TYPE_NTLMSSP;
230 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
231 case PIPE_AUTH_TYPE_SPNEGO_KRB5:
232 return DCERPC_AUTH_TYPE_SPNEGO;
234 case PIPE_AUTH_TYPE_SCHANNEL:
235 return DCERPC_AUTH_TYPE_SCHANNEL;
237 case PIPE_AUTH_TYPE_KRB5:
238 return DCERPC_AUTH_TYPE_KRB5;
241 DEBUG(0,("map_pipe_auth_type_to_rpc_type: unknown pipe "
243 (unsigned int)auth_type ));
249 /********************************************************************
250 Pipe description for a DEBUG
251 ********************************************************************/
252 static const char *rpccli_pipe_txt(TALLOC_CTX *mem_ctx,
253 struct rpc_pipe_client *cli)
255 char *result = talloc_asprintf(mem_ctx, "host %s", cli->desthost);
256 if (result == NULL) {
262 /********************************************************************
264 ********************************************************************/
266 static uint32 get_rpc_call_id(void)
268 static uint32 call_id = 0;
273 * Realloc pdu to have a least "size" bytes
276 static bool rpc_grow_buffer(prs_struct *pdu, size_t size)
280 if (prs_data_size(pdu) >= size) {
284 extra_size = size - prs_data_size(pdu);
286 if (!prs_force_grow(pdu, extra_size)) {
287 DEBUG(0, ("rpc_grow_buffer: Failed to grow parse struct by "
288 "%d bytes.\n", (int)extra_size));
292 DEBUG(5, ("rpc_grow_buffer: grew buffer by %d bytes to %u\n",
293 (int)extra_size, prs_data_size(pdu)));
297 /*******************************************************************
298 *******************************************************************/
300 NTSTATUS dcerpc_push_ncacn_packet(TALLOC_CTX *mem_ctx,
301 enum dcerpc_pkt_type ptype,
303 uint16_t frag_length,
304 uint16_t auth_length,
306 union dcerpc_payload u,
309 struct ncacn_packet r;
310 enum ndr_err_code ndr_err;
313 r.rpc_vers_minor = 0;
315 r.pfc_flags = pfc_flags;
316 r.drep[0] = DCERPC_DREP_LE;
320 r.frag_length = frag_length;
321 r.auth_length = auth_length;
325 ndr_err = ndr_push_struct_blob(blob, mem_ctx, &r,
326 (ndr_push_flags_fn_t)ndr_push_ncacn_packet);
327 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
328 return ndr_map_error2ntstatus(ndr_err);
331 if (DEBUGLEVEL >= 10) {
332 NDR_PRINT_DEBUG(ncacn_packet, &r);
338 /*******************************************************************
339 *******************************************************************/
341 NTSTATUS dcerpc_pull_ncacn_packet(TALLOC_CTX *mem_ctx,
342 const DATA_BLOB *blob,
343 struct ncacn_packet *r)
345 enum ndr_err_code ndr_err;
347 ndr_err = ndr_pull_struct_blob(blob, mem_ctx, r,
348 (ndr_pull_flags_fn_t)ndr_pull_ncacn_packet);
349 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
350 return ndr_map_error2ntstatus(ndr_err);
353 if (DEBUGLEVEL >= 10) {
354 NDR_PRINT_DEBUG(ncacn_packet, r);
360 /*******************************************************************
361 *******************************************************************/
363 NTSTATUS dcerpc_pull_ncacn_packet_header(TALLOC_CTX *mem_ctx,
364 const DATA_BLOB *blob,
365 struct ncacn_packet_header *r)
367 enum ndr_err_code ndr_err;
369 ndr_err = ndr_pull_struct_blob(blob, mem_ctx, r,
370 (ndr_pull_flags_fn_t)ndr_pull_ncacn_packet_header);
371 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
372 return ndr_map_error2ntstatus(ndr_err);
375 if (DEBUGLEVEL >= 10) {
376 NDR_PRINT_DEBUG(ncacn_packet_header, r);
382 /*******************************************************************
383 ********************************************************************/
385 static NTSTATUS dcerpc_push_schannel_bind(TALLOC_CTX *mem_ctx,
386 struct NL_AUTH_MESSAGE *r,
389 enum ndr_err_code ndr_err;
391 ndr_err = ndr_push_struct_blob(blob, mem_ctx, r,
392 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
393 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
394 return ndr_map_error2ntstatus(ndr_err);
397 if (DEBUGLEVEL >= 10) {
398 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, r);
404 /*******************************************************************
405 ********************************************************************/
407 static NTSTATUS dcerpc_pull_dcerpc_auth(TALLOC_CTX *mem_ctx,
408 const DATA_BLOB *blob,
409 struct dcerpc_auth *r)
411 enum ndr_err_code ndr_err;
413 ndr_err = ndr_pull_struct_blob(blob, mem_ctx, r,
414 (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
415 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
416 return ndr_map_error2ntstatus(ndr_err);
419 if (DEBUGLEVEL >= 10) {
420 NDR_PRINT_DEBUG(dcerpc_auth, r);
426 /*******************************************************************
427 Use SMBreadX to get rest of one fragment's worth of rpc data.
428 Reads the whole size or give an error message
429 ********************************************************************/
431 struct rpc_read_state {
432 struct event_context *ev;
433 struct rpc_cli_transport *transport;
439 static void rpc_read_done(struct tevent_req *subreq);
441 static struct tevent_req *rpc_read_send(TALLOC_CTX *mem_ctx,
442 struct event_context *ev,
443 struct rpc_cli_transport *transport,
444 uint8_t *data, size_t size)
446 struct tevent_req *req, *subreq;
447 struct rpc_read_state *state;
449 req = tevent_req_create(mem_ctx, &state, struct rpc_read_state);
454 state->transport = transport;
459 DEBUG(5, ("rpc_read_send: data_to_read: %u\n", (unsigned int)size));
461 subreq = transport->read_send(state, ev, (uint8_t *)data, size,
463 if (subreq == NULL) {
466 tevent_req_set_callback(subreq, rpc_read_done, req);
474 static void rpc_read_done(struct tevent_req *subreq)
476 struct tevent_req *req = tevent_req_callback_data(
477 subreq, struct tevent_req);
478 struct rpc_read_state *state = tevent_req_data(
479 req, struct rpc_read_state);
483 status = state->transport->read_recv(subreq, &received);
485 if (!NT_STATUS_IS_OK(status)) {
486 tevent_req_nterror(req, status);
490 state->num_read += received;
491 if (state->num_read == state->size) {
492 tevent_req_done(req);
496 subreq = state->transport->read_send(state, state->ev,
497 state->data + state->num_read,
498 state->size - state->num_read,
499 state->transport->priv);
500 if (tevent_req_nomem(subreq, req)) {
503 tevent_req_set_callback(subreq, rpc_read_done, req);
506 static NTSTATUS rpc_read_recv(struct tevent_req *req)
508 return tevent_req_simple_recv_ntstatus(req);
511 struct rpc_write_state {
512 struct event_context *ev;
513 struct rpc_cli_transport *transport;
519 static void rpc_write_done(struct tevent_req *subreq);
521 static struct tevent_req *rpc_write_send(TALLOC_CTX *mem_ctx,
522 struct event_context *ev,
523 struct rpc_cli_transport *transport,
524 const uint8_t *data, size_t size)
526 struct tevent_req *req, *subreq;
527 struct rpc_write_state *state;
529 req = tevent_req_create(mem_ctx, &state, struct rpc_write_state);
534 state->transport = transport;
537 state->num_written = 0;
539 DEBUG(5, ("rpc_write_send: data_to_write: %u\n", (unsigned int)size));
541 subreq = transport->write_send(state, ev, data, size, transport->priv);
542 if (subreq == NULL) {
545 tevent_req_set_callback(subreq, rpc_write_done, req);
552 static void rpc_write_done(struct tevent_req *subreq)
554 struct tevent_req *req = tevent_req_callback_data(
555 subreq, struct tevent_req);
556 struct rpc_write_state *state = tevent_req_data(
557 req, struct rpc_write_state);
561 status = state->transport->write_recv(subreq, &written);
563 if (!NT_STATUS_IS_OK(status)) {
564 tevent_req_nterror(req, status);
568 state->num_written += written;
570 if (state->num_written == state->size) {
571 tevent_req_done(req);
575 subreq = state->transport->write_send(state, state->ev,
576 state->data + state->num_written,
577 state->size - state->num_written,
578 state->transport->priv);
579 if (tevent_req_nomem(subreq, req)) {
582 tevent_req_set_callback(subreq, rpc_write_done, req);
585 static NTSTATUS rpc_write_recv(struct tevent_req *req)
587 return tevent_req_simple_recv_ntstatus(req);
591 static NTSTATUS parse_rpc_header(struct rpc_pipe_client *cli,
592 struct ncacn_packet_header *prhdr,
596 DATA_BLOB blob = data_blob_const(prs_data_p(pdu), prs_data_size(pdu));
599 * This next call sets the endian bit correctly in current_pdu. We
600 * will propagate this to rbuf later.
603 status = dcerpc_pull_ncacn_packet_header(cli, &blob, prhdr);
604 if (!NT_STATUS_IS_OK(status)) {
608 if (!prs_set_offset(pdu, prs_offset(pdu) + RPC_HEADER_LEN)) {
609 return NT_STATUS_BUFFER_TOO_SMALL;
612 if (UNMARSHALLING(pdu) && prhdr->drep[0] == 0) {
613 DEBUG(10,("parse_rpc_header: PDU data format is big-endian. Setting flag.\n"));
614 prs_set_endian_data(pdu, RPC_BIG_ENDIAN);
617 if (prhdr->frag_length > cli->max_recv_frag) {
618 DEBUG(0, ("cli_pipe_get_current_pdu: Server sent fraglen %d,"
619 " we only allow %d\n", (int)prhdr->frag_length,
620 (int)cli->max_recv_frag));
621 return NT_STATUS_BUFFER_TOO_SMALL;
627 /****************************************************************************
628 Try and get a PDU's worth of data from current_pdu. If not, then read more
630 ****************************************************************************/
632 struct get_complete_frag_state {
633 struct event_context *ev;
634 struct rpc_pipe_client *cli;
635 struct ncacn_packet_header *prhdr;
639 static void get_complete_frag_got_header(struct tevent_req *subreq);
640 static void get_complete_frag_got_rest(struct tevent_req *subreq);
642 static struct tevent_req *get_complete_frag_send(TALLOC_CTX *mem_ctx,
643 struct event_context *ev,
644 struct rpc_pipe_client *cli,
645 struct ncacn_packet_header *prhdr,
648 struct tevent_req *req, *subreq;
649 struct get_complete_frag_state *state;
653 req = tevent_req_create(mem_ctx, &state,
654 struct get_complete_frag_state);
660 state->prhdr = prhdr;
663 pdu_len = prs_data_size(pdu);
664 if (pdu_len < RPC_HEADER_LEN) {
665 if (!rpc_grow_buffer(pdu, RPC_HEADER_LEN)) {
666 status = NT_STATUS_NO_MEMORY;
669 subreq = rpc_read_send(
671 state->cli->transport,
672 (uint8_t *)(prs_data_p(state->pdu) + pdu_len),
673 RPC_HEADER_LEN - pdu_len);
674 if (subreq == NULL) {
675 status = NT_STATUS_NO_MEMORY;
678 tevent_req_set_callback(subreq, get_complete_frag_got_header,
683 status = parse_rpc_header(cli, prhdr, pdu);
684 if (!NT_STATUS_IS_OK(status)) {
689 * Ensure we have frag_len bytes of data.
691 if (pdu_len < prhdr->frag_length) {
692 if (!rpc_grow_buffer(pdu, prhdr->frag_length)) {
693 status = NT_STATUS_NO_MEMORY;
696 subreq = rpc_read_send(state, state->ev,
697 state->cli->transport,
698 (uint8_t *)(prs_data_p(pdu) + pdu_len),
699 prhdr->frag_length - pdu_len);
700 if (subreq == NULL) {
701 status = NT_STATUS_NO_MEMORY;
704 tevent_req_set_callback(subreq, get_complete_frag_got_rest,
709 status = NT_STATUS_OK;
711 if (NT_STATUS_IS_OK(status)) {
712 tevent_req_done(req);
714 tevent_req_nterror(req, status);
716 return tevent_req_post(req, ev);
719 static void get_complete_frag_got_header(struct tevent_req *subreq)
721 struct tevent_req *req = tevent_req_callback_data(
722 subreq, struct tevent_req);
723 struct get_complete_frag_state *state = tevent_req_data(
724 req, struct get_complete_frag_state);
727 status = rpc_read_recv(subreq);
729 if (!NT_STATUS_IS_OK(status)) {
730 tevent_req_nterror(req, status);
734 status = parse_rpc_header(state->cli, state->prhdr, state->pdu);
735 if (!NT_STATUS_IS_OK(status)) {
736 tevent_req_nterror(req, status);
740 if (!rpc_grow_buffer(state->pdu, state->prhdr->frag_length)) {
741 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
746 * We're here in this piece of code because we've read exactly
747 * RPC_HEADER_LEN bytes into state->pdu.
750 subreq = rpc_read_send(
751 state, state->ev, state->cli->transport,
752 (uint8_t *)(prs_data_p(state->pdu) + RPC_HEADER_LEN),
753 state->prhdr->frag_length - RPC_HEADER_LEN);
754 if (tevent_req_nomem(subreq, req)) {
757 tevent_req_set_callback(subreq, get_complete_frag_got_rest, req);
760 static void get_complete_frag_got_rest(struct tevent_req *subreq)
762 struct tevent_req *req = tevent_req_callback_data(
763 subreq, struct tevent_req);
766 status = rpc_read_recv(subreq);
768 if (!NT_STATUS_IS_OK(status)) {
769 tevent_req_nterror(req, status);
772 tevent_req_done(req);
775 static NTSTATUS get_complete_frag_recv(struct tevent_req *req)
777 return tevent_req_simple_recv_ntstatus(req);
780 /****************************************************************************
781 NTLMSSP specific sign/seal.
782 Virtually identical to rpc_server/srv_pipe.c:api_pipe_ntlmssp_auth_process.
783 In fact I should probably abstract these into identical pieces of code... JRA.
784 ****************************************************************************/
786 static NTSTATUS cli_pipe_verify_ntlmssp(struct rpc_pipe_client *cli,
787 struct ncacn_packet_header *prhdr,
788 prs_struct *current_pdu,
789 uint8 *p_ss_padding_len)
791 struct dcerpc_auth auth_info;
792 uint32 save_offset = prs_offset(current_pdu);
793 uint32_t auth_len = prhdr->auth_length;
794 struct ntlmssp_state *ntlmssp_state = cli->auth->a_u.ntlmssp_state;
795 unsigned char *data = NULL;
797 unsigned char *full_packet_data = NULL;
798 size_t full_packet_data_len;
803 if (cli->auth->auth_level == DCERPC_AUTH_LEVEL_NONE
804 || cli->auth->auth_level == DCERPC_AUTH_LEVEL_CONNECT) {
808 if (!ntlmssp_state) {
809 return NT_STATUS_INVALID_PARAMETER;
812 /* Ensure there's enough data for an authenticated response. */
813 if (auth_len > RPC_MAX_PDU_FRAG_LEN ||
814 prhdr->frag_length < RPC_HEADER_LEN +
816 RPC_HDR_AUTH_LEN + auth_len) {
817 DEBUG(0,("cli_pipe_verify_ntlmssp: auth_len %u is too large.\n",
818 (unsigned int)auth_len ));
819 return NT_STATUS_BUFFER_TOO_SMALL;
823 * We need the full packet data + length (minus auth stuff) as well as the packet data + length
824 * after the RPC header.
825 * We need to pass in the full packet (minus auth len) to the NTLMSSP sign and check seal
826 * functions as NTLMv2 checks the rpc headers also.
829 data = (unsigned char *)(prs_data_p(current_pdu) + RPC_HEADER_LEN + RPC_HDR_RESP_LEN);
830 data_len = (size_t)(prhdr->frag_length - RPC_HEADER_LEN - RPC_HDR_RESP_LEN - RPC_HDR_AUTH_LEN - auth_len);
832 full_packet_data = (unsigned char *)prs_data_p(current_pdu);
833 full_packet_data_len = prhdr->frag_length - auth_len;
835 /* Pull the auth header and the following data into a blob. */
836 /* NB. The offset of the auth_header is relative to the *end*
837 * of the packet, not the start. */
838 if(!prs_set_offset(current_pdu, prhdr->frag_length - RPC_HDR_AUTH_LEN - auth_len)) {
839 DEBUG(0,("cli_pipe_verify_ntlmssp: cannot move offset to %u.\n",
840 (unsigned int)RPC_HEADER_LEN + (unsigned int)RPC_HDR_RESP_LEN + (unsigned int)data_len ));
841 return NT_STATUS_BUFFER_TOO_SMALL;
844 blob = data_blob_const(prs_data_p(current_pdu) + prs_offset(current_pdu),
845 prs_data_size(current_pdu) - prs_offset(current_pdu));
847 status = dcerpc_pull_dcerpc_auth(cli, &blob, &auth_info);
848 if (!NT_STATUS_IS_OK(status)) {
849 DEBUG(0,("cli_pipe_verify_ntlmssp: failed to unmarshall dcerpc_auth.\n"));
853 /* Ensure auth_pad_len fits into the packet. */
854 if (RPC_HEADER_LEN + RPC_HDR_REQ_LEN + auth_info.auth_pad_length +
855 RPC_HDR_AUTH_LEN + auth_len > prhdr->frag_length) {
856 DEBUG(0,("cli_pipe_verify_ntlmssp: auth_info.auth_pad_len "
857 "too large (%u), auth_len (%u), frag_len = (%u).\n",
858 (unsigned int)auth_info.auth_pad_length,
859 (unsigned int)auth_len,
860 (unsigned int)prhdr->frag_length));
861 return NT_STATUS_BUFFER_TOO_SMALL;
865 auth_blob = auth_info.credentials;
867 switch (cli->auth->auth_level) {
868 case DCERPC_AUTH_LEVEL_PRIVACY:
869 /* Data is encrypted. */
870 status = ntlmssp_unseal_packet(ntlmssp_state,
873 full_packet_data_len,
875 if (!NT_STATUS_IS_OK(status)) {
876 DEBUG(0,("cli_pipe_verify_ntlmssp: failed to unseal "
877 "packet from %s. Error was %s.\n",
878 rpccli_pipe_txt(talloc_tos(), cli),
879 nt_errstr(status) ));
883 case DCERPC_AUTH_LEVEL_INTEGRITY:
884 /* Data is signed. */
885 status = ntlmssp_check_packet(ntlmssp_state,
888 full_packet_data_len,
890 if (!NT_STATUS_IS_OK(status)) {
891 DEBUG(0,("cli_pipe_verify_ntlmssp: check signing failed on "
892 "packet from %s. Error was %s.\n",
893 rpccli_pipe_txt(talloc_tos(), cli),
894 nt_errstr(status) ));
899 DEBUG(0, ("cli_pipe_verify_ntlmssp: unknown internal "
900 "auth level %d\n", cli->auth->auth_level));
901 return NT_STATUS_INVALID_INFO_CLASS;
905 * Return the current pointer to the data offset.
908 if(!prs_set_offset(current_pdu, save_offset)) {
909 DEBUG(0,("api_pipe_auth_process: failed to set offset back to %u\n",
910 (unsigned int)save_offset ));
911 return NT_STATUS_BUFFER_TOO_SMALL;
915 * Remember the padding length. We must remove it from the real data
916 * stream once the sign/seal is done.
919 *p_ss_padding_len = auth_info.auth_pad_length;
924 /****************************************************************************
925 schannel specific sign/seal.
926 ****************************************************************************/
928 static NTSTATUS cli_pipe_verify_schannel(struct rpc_pipe_client *cli,
929 struct ncacn_packet_header *prhdr,
930 prs_struct *current_pdu,
931 uint8 *p_ss_padding_len)
933 struct dcerpc_auth auth_info;
934 uint32_t auth_len = prhdr->auth_length;
935 uint32 save_offset = prs_offset(current_pdu);
936 struct schannel_state *schannel_auth =
937 cli->auth->a_u.schannel_auth;
943 if (cli->auth->auth_level == DCERPC_AUTH_LEVEL_NONE
944 || cli->auth->auth_level == DCERPC_AUTH_LEVEL_CONNECT) {
948 if (auth_len < RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN) {
949 DEBUG(0,("cli_pipe_verify_schannel: auth_len %u.\n", (unsigned int)auth_len ));
950 return NT_STATUS_INVALID_PARAMETER;
953 if (!schannel_auth) {
954 return NT_STATUS_INVALID_PARAMETER;
957 /* Ensure there's enough data for an authenticated response. */
958 if ((auth_len > RPC_MAX_PDU_FRAG_LEN) ||
959 (RPC_HEADER_LEN + RPC_HDR_RESP_LEN + RPC_HDR_AUTH_LEN + auth_len > prhdr->frag_length)) {
960 DEBUG(0,("cli_pipe_verify_schannel: auth_len %u is too large.\n",
961 (unsigned int)auth_len ));
962 return NT_STATUS_INVALID_PARAMETER;
965 data_len = prhdr->frag_length - RPC_HEADER_LEN - RPC_HDR_RESP_LEN - RPC_HDR_AUTH_LEN - auth_len;
967 /* Pull the auth header and the following data into a blob. */
968 /* NB. The offset of the auth_header is relative to the *end*
969 * of the packet, not the start. */
970 if(!prs_set_offset(current_pdu,
971 prhdr->frag_length - RPC_HDR_AUTH_LEN - auth_len)) {
972 DEBUG(0,("cli_pipe_verify_schannel: cannot move "
974 (unsigned int)(prhdr->frag_length -
975 RPC_HDR_AUTH_LEN - auth_len) ));
976 return NT_STATUS_BUFFER_TOO_SMALL;
979 blob = data_blob_const(prs_data_p(current_pdu)
980 + prs_offset(current_pdu),
981 prs_data_size(current_pdu)
982 - prs_offset(current_pdu));
984 status = dcerpc_pull_dcerpc_auth(cli, &blob, &auth_info);
985 if (!NT_STATUS_IS_OK(status)) {
986 DEBUG(0,("cli_pipe_verify_ntlmssp: failed to unmarshall dcerpc_auth.\n"));
990 /* Ensure auth_pad_len fits into the packet. */
991 if (RPC_HEADER_LEN + RPC_HDR_REQ_LEN + auth_info.auth_pad_length +
992 RPC_HDR_AUTH_LEN + auth_len > prhdr->frag_length) {
993 DEBUG(0,("cli_pipe_verify_schannel: auth_info.auth_pad_len "
994 "too large (%u), auth_len (%u), frag_len = (%u).\n",
995 (unsigned int)auth_info.auth_pad_length,
996 (unsigned int)auth_len,
997 (unsigned int)prhdr->frag_length));
998 return NT_STATUS_BUFFER_TOO_SMALL;
1001 if (auth_info.auth_type != DCERPC_AUTH_TYPE_SCHANNEL) {
1002 DEBUG(0,("cli_pipe_verify_schannel: Invalid auth info %d on schannel\n",
1003 auth_info.auth_type));
1004 return NT_STATUS_BUFFER_TOO_SMALL;
1007 blob = data_blob_const(prs_data_p(current_pdu) + prs_offset(current_pdu), auth_len);
1009 if (DEBUGLEVEL >= 10) {
1010 dump_NL_AUTH_SIGNATURE(talloc_tos(), &blob);
1013 data = (uint8_t *)prs_data_p(current_pdu)+RPC_HEADER_LEN+RPC_HDR_RESP_LEN;
1015 switch (cli->auth->auth_level) {
1016 case DCERPC_AUTH_LEVEL_PRIVACY:
1017 status = netsec_incoming_packet(schannel_auth,
1024 case DCERPC_AUTH_LEVEL_INTEGRITY:
1025 status = netsec_incoming_packet(schannel_auth,
1033 status = NT_STATUS_INTERNAL_ERROR;
1037 if (!NT_STATUS_IS_OK(status)) {
1038 DEBUG(3,("cli_pipe_verify_schannel: failed to decode PDU "
1039 "Connection to %s (%s).\n",
1040 rpccli_pipe_txt(talloc_tos(), cli),
1041 nt_errstr(status)));
1042 return NT_STATUS_INVALID_PARAMETER;
1046 * Return the current pointer to the data offset.
1049 if(!prs_set_offset(current_pdu, save_offset)) {
1050 DEBUG(0,("api_pipe_auth_process: failed to set offset back to %u\n",
1051 (unsigned int)save_offset ));
1052 return NT_STATUS_BUFFER_TOO_SMALL;
1056 * Remember the padding length. We must remove it from the real data
1057 * stream once the sign/seal is done.
1060 *p_ss_padding_len = auth_info.auth_pad_length;
1062 return NT_STATUS_OK;
1065 /****************************************************************************
1066 Do the authentication checks on an incoming pdu. Check sign and unseal etc.
1067 ****************************************************************************/
1069 static NTSTATUS cli_pipe_validate_rpc_response(struct rpc_pipe_client *cli,
1070 struct ncacn_packet_header *prhdr,
1071 prs_struct *current_pdu,
1072 uint8 *p_ss_padding_len)
1074 NTSTATUS ret = NT_STATUS_OK;
1076 /* Paranioa checks for auth_len. */
1077 if (prhdr->auth_length) {
1078 if (prhdr->auth_length > prhdr->frag_length) {
1079 return NT_STATUS_INVALID_PARAMETER;
1082 if (prhdr->auth_length + (unsigned int)RPC_HDR_AUTH_LEN < prhdr->auth_length ||
1083 prhdr->auth_length + (unsigned int)RPC_HDR_AUTH_LEN < (unsigned int)RPC_HDR_AUTH_LEN) {
1084 /* Integer wrap attempt. */
1085 return NT_STATUS_INVALID_PARAMETER;
1090 * Now we have a complete RPC request PDU fragment, try and verify any auth data.
1093 switch(cli->auth->auth_type) {
1094 case PIPE_AUTH_TYPE_NONE:
1095 if (prhdr->auth_length) {
1096 DEBUG(3, ("cli_pipe_validate_rpc_response: "
1097 "Connection to %s - got non-zero "
1099 rpccli_pipe_txt(talloc_tos(), cli),
1100 (unsigned int)prhdr->auth_length));
1101 return NT_STATUS_INVALID_PARAMETER;
1105 case PIPE_AUTH_TYPE_NTLMSSP:
1106 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
1107 ret = cli_pipe_verify_ntlmssp(cli, prhdr, current_pdu, p_ss_padding_len);
1108 if (!NT_STATUS_IS_OK(ret)) {
1113 case PIPE_AUTH_TYPE_SCHANNEL:
1114 ret = cli_pipe_verify_schannel(cli, prhdr, current_pdu, p_ss_padding_len);
1115 if (!NT_STATUS_IS_OK(ret)) {
1120 case PIPE_AUTH_TYPE_KRB5:
1121 case PIPE_AUTH_TYPE_SPNEGO_KRB5:
1123 DEBUG(3, ("cli_pipe_validate_rpc_response: Connection "
1124 "to %s - unknown internal auth type %u.\n",
1125 rpccli_pipe_txt(talloc_tos(), cli),
1126 cli->auth->auth_type ));
1127 return NT_STATUS_INVALID_INFO_CLASS;
1130 return NT_STATUS_OK;
1133 /****************************************************************************
1134 Do basic authentication checks on an incoming pdu.
1135 ****************************************************************************/
1137 static NTSTATUS cli_pipe_validate_current_pdu(struct rpc_pipe_client *cli,
1138 struct ncacn_packet_header *prhdr,
1139 prs_struct *current_pdu,
1140 uint8 expected_pkt_type,
1143 prs_struct *return_data)
1146 NTSTATUS ret = NT_STATUS_OK;
1147 uint32 current_pdu_len = prs_data_size(current_pdu);
1149 if (current_pdu_len != prhdr->frag_length) {
1150 DEBUG(5,("cli_pipe_validate_current_pdu: incorrect pdu length %u, expected %u\n",
1151 (unsigned int)current_pdu_len, (unsigned int)prhdr->frag_length));
1152 return NT_STATUS_INVALID_PARAMETER;
1156 * Point the return values at the real data including the RPC
1157 * header. Just in case the caller wants it.
1159 *ppdata = prs_data_p(current_pdu);
1160 *pdata_len = current_pdu_len;
1162 /* Ensure we have the correct type. */
1163 switch (prhdr->ptype) {
1164 case DCERPC_PKT_ALTER_RESP:
1165 case DCERPC_PKT_BIND_ACK:
1167 /* Alter context and bind ack share the same packet definitions. */
1171 case DCERPC_PKT_RESPONSE:
1173 uint8 ss_padding_len = 0;
1175 struct ncacn_packet r;
1177 blob = data_blob_const(prs_data_p(current_pdu),
1178 prs_data_size(current_pdu));
1180 ret = dcerpc_pull_ncacn_packet(cli, &blob, &r);
1181 if (!NT_STATUS_IS_OK(ret)) {
1185 if (!prs_set_offset(current_pdu, prs_offset(current_pdu) + RPC_HDR_RESP_LEN)) {
1186 return NT_STATUS_BUFFER_TOO_SMALL;
1189 /* Here's where we deal with incoming sign/seal. */
1190 ret = cli_pipe_validate_rpc_response(cli, prhdr,
1191 current_pdu, &ss_padding_len);
1192 if (!NT_STATUS_IS_OK(ret)) {
1196 /* Point the return values at the NDR data. Remember to remove any ss padding. */
1197 *ppdata = prs_data_p(current_pdu) + RPC_HEADER_LEN + RPC_HDR_RESP_LEN;
1199 if (current_pdu_len < RPC_HEADER_LEN + RPC_HDR_RESP_LEN + ss_padding_len) {
1200 return NT_STATUS_BUFFER_TOO_SMALL;
1203 *pdata_len = current_pdu_len - RPC_HEADER_LEN - RPC_HDR_RESP_LEN - ss_padding_len;
1205 /* Remember to remove the auth footer. */
1206 if (prhdr->auth_length) {
1207 /* We've already done integer wrap tests on auth_len in
1208 cli_pipe_validate_rpc_response(). */
1209 if (*pdata_len < RPC_HDR_AUTH_LEN + prhdr->auth_length) {
1210 return NT_STATUS_BUFFER_TOO_SMALL;
1212 *pdata_len -= (RPC_HDR_AUTH_LEN + prhdr->auth_length);
1215 DEBUG(10,("cli_pipe_validate_current_pdu: got pdu len %u, data_len %u, ss_len %u\n",
1216 current_pdu_len, *pdata_len, ss_padding_len ));
1219 * If this is the first reply, and the allocation hint is reasonably, try and
1220 * set up the return_data parse_struct to the correct size.
1223 if ((prs_data_size(return_data) == 0) && r.u.response.alloc_hint && (r.u.response.alloc_hint < 15*1024*1024)) {
1224 if (!prs_set_buffer_size(return_data, r.u.response.alloc_hint)) {
1225 DEBUG(0,("cli_pipe_validate_current_pdu: reply alloc hint %u "
1226 "too large to allocate\n",
1227 (unsigned int)r.u.response.alloc_hint ));
1228 return NT_STATUS_NO_MEMORY;
1235 case DCERPC_PKT_BIND_NAK:
1236 DEBUG(1, ("cli_pipe_validate_current_pdu: Bind NACK "
1237 "received from %s!\n",
1238 rpccli_pipe_txt(talloc_tos(), cli)));
1239 /* Use this for now... */
1240 return NT_STATUS_NETWORK_ACCESS_DENIED;
1242 case DCERPC_PKT_FAULT:
1245 struct ncacn_packet r;
1247 blob = data_blob_const(prs_data_p(current_pdu),
1248 prs_data_size(current_pdu));
1250 ret = dcerpc_pull_ncacn_packet(cli, &blob, &r);
1251 if (!NT_STATUS_IS_OK(ret)) {
1254 DEBUG(1, ("cli_pipe_validate_current_pdu: RPC fault "
1255 "code %s received from %s!\n",
1256 dcerpc_errstr(talloc_tos(), r.u.fault.status),
1257 rpccli_pipe_txt(talloc_tos(), cli)));
1259 if (NT_STATUS_IS_OK(NT_STATUS(r.u.fault.status))) {
1260 return NT_STATUS_UNSUCCESSFUL;
1262 return NT_STATUS(r.u.fault.status);
1267 DEBUG(0, ("cli_pipe_validate_current_pdu: unknown packet type %u received "
1269 (unsigned int)prhdr->ptype,
1270 rpccli_pipe_txt(talloc_tos(), cli)));
1271 return NT_STATUS_INVALID_INFO_CLASS;
1274 if (prhdr->ptype != expected_pkt_type) {
1275 DEBUG(3, ("cli_pipe_validate_current_pdu: Connection to %s "
1276 "got an unexpected RPC packet type - %u, not %u\n",
1277 rpccli_pipe_txt(talloc_tos(), cli),
1279 expected_pkt_type));
1280 return NT_STATUS_INVALID_INFO_CLASS;
1283 /* Do this just before return - we don't want to modify any rpc header
1284 data before now as we may have needed to do cryptographic actions on
1287 if ((prhdr->ptype == DCERPC_PKT_BIND_ACK) && !(prhdr->pfc_flags & DCERPC_PFC_FLAG_LAST)) {
1288 DEBUG(5,("cli_pipe_validate_current_pdu: bug in server (AS/U?), "
1289 "setting fragment first/last ON.\n"));
1290 prhdr->pfc_flags |= DCERPC_PFC_FLAG_FIRST | DCERPC_PFC_FLAG_LAST;
1293 return NT_STATUS_OK;
1296 /****************************************************************************
1297 Ensure we eat the just processed pdu from the current_pdu prs_struct.
1298 Normally the frag_len and buffer size will match, but on the first trans
1299 reply there is a theoretical chance that buffer size > frag_len, so we must
1301 ****************************************************************************/
1303 static NTSTATUS cli_pipe_reset_current_pdu(struct rpc_pipe_client *cli,
1304 struct ncacn_packet_header *prhdr,
1305 prs_struct *current_pdu)
1307 uint32 current_pdu_len = prs_data_size(current_pdu);
1309 if (current_pdu_len < prhdr->frag_length) {
1310 return NT_STATUS_BUFFER_TOO_SMALL;
1314 if (current_pdu_len == (uint32)prhdr->frag_length) {
1315 prs_mem_free(current_pdu);
1316 prs_init_empty(current_pdu, prs_get_mem_context(current_pdu), UNMARSHALL);
1317 /* Make current_pdu dynamic with no memory. */
1318 prs_give_memory(current_pdu, 0, 0, True);
1319 return NT_STATUS_OK;
1323 * Oh no ! More data in buffer than we processed in current pdu.
1324 * Cheat. Move the data down and shrink the buffer.
1327 memcpy(prs_data_p(current_pdu), prs_data_p(current_pdu) + prhdr->frag_length,
1328 current_pdu_len - prhdr->frag_length);
1330 /* Remember to set the read offset back to zero. */
1331 prs_set_offset(current_pdu, 0);
1333 /* Shrink the buffer. */
1334 if (!prs_set_buffer_size(current_pdu, current_pdu_len - prhdr->frag_length)) {
1335 return NT_STATUS_BUFFER_TOO_SMALL;
1338 return NT_STATUS_OK;
1341 /****************************************************************************
1342 Call a remote api on an arbitrary pipe. takes param, data and setup buffers.
1343 ****************************************************************************/
1345 struct cli_api_pipe_state {
1346 struct event_context *ev;
1347 struct rpc_cli_transport *transport;
1352 static void cli_api_pipe_trans_done(struct tevent_req *subreq);
1353 static void cli_api_pipe_write_done(struct tevent_req *subreq);
1354 static void cli_api_pipe_read_done(struct tevent_req *subreq);
1356 static struct tevent_req *cli_api_pipe_send(TALLOC_CTX *mem_ctx,
1357 struct event_context *ev,
1358 struct rpc_cli_transport *transport,
1359 uint8_t *data, size_t data_len,
1360 uint32_t max_rdata_len)
1362 struct tevent_req *req, *subreq;
1363 struct cli_api_pipe_state *state;
1366 req = tevent_req_create(mem_ctx, &state, struct cli_api_pipe_state);
1371 state->transport = transport;
1373 if (max_rdata_len < RPC_HEADER_LEN) {
1375 * For a RPC reply we always need at least RPC_HEADER_LEN
1376 * bytes. We check this here because we will receive
1377 * RPC_HEADER_LEN bytes in cli_trans_sock_send_done.
1379 status = NT_STATUS_INVALID_PARAMETER;
1383 if (transport->trans_send != NULL) {
1384 subreq = transport->trans_send(state, ev, data, data_len,
1385 max_rdata_len, transport->priv);
1386 if (subreq == NULL) {
1389 tevent_req_set_callback(subreq, cli_api_pipe_trans_done, req);
1394 * If the transport does not provide a "trans" routine, i.e. for
1395 * example the ncacn_ip_tcp transport, do the write/read step here.
1398 subreq = rpc_write_send(state, ev, transport, data, data_len);
1399 if (subreq == NULL) {
1402 tevent_req_set_callback(subreq, cli_api_pipe_write_done, req);
1406 tevent_req_nterror(req, status);
1407 return tevent_req_post(req, ev);
1413 static void cli_api_pipe_trans_done(struct tevent_req *subreq)
1415 struct tevent_req *req = tevent_req_callback_data(
1416 subreq, struct tevent_req);
1417 struct cli_api_pipe_state *state = tevent_req_data(
1418 req, struct cli_api_pipe_state);
1421 status = state->transport->trans_recv(subreq, state, &state->rdata,
1423 TALLOC_FREE(subreq);
1424 if (!NT_STATUS_IS_OK(status)) {
1425 tevent_req_nterror(req, status);
1428 tevent_req_done(req);
1431 static void cli_api_pipe_write_done(struct tevent_req *subreq)
1433 struct tevent_req *req = tevent_req_callback_data(
1434 subreq, struct tevent_req);
1435 struct cli_api_pipe_state *state = tevent_req_data(
1436 req, struct cli_api_pipe_state);
1439 status = rpc_write_recv(subreq);
1440 TALLOC_FREE(subreq);
1441 if (!NT_STATUS_IS_OK(status)) {
1442 tevent_req_nterror(req, status);
1446 state->rdata = TALLOC_ARRAY(state, uint8_t, RPC_HEADER_LEN);
1447 if (tevent_req_nomem(state->rdata, req)) {
1452 * We don't need to use rpc_read_send here, the upper layer will cope
1453 * with a short read, transport->trans_send could also return less
1454 * than state->max_rdata_len.
1456 subreq = state->transport->read_send(state, state->ev, state->rdata,
1458 state->transport->priv);
1459 if (tevent_req_nomem(subreq, req)) {
1462 tevent_req_set_callback(subreq, cli_api_pipe_read_done, req);
1465 static void cli_api_pipe_read_done(struct tevent_req *subreq)
1467 struct tevent_req *req = tevent_req_callback_data(
1468 subreq, struct tevent_req);
1469 struct cli_api_pipe_state *state = tevent_req_data(
1470 req, struct cli_api_pipe_state);
1474 status = state->transport->read_recv(subreq, &received);
1475 TALLOC_FREE(subreq);
1476 if (!NT_STATUS_IS_OK(status)) {
1477 tevent_req_nterror(req, status);
1480 state->rdata_len = received;
1481 tevent_req_done(req);
1484 static NTSTATUS cli_api_pipe_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1485 uint8_t **prdata, uint32_t *prdata_len)
1487 struct cli_api_pipe_state *state = tevent_req_data(
1488 req, struct cli_api_pipe_state);
1491 if (tevent_req_is_nterror(req, &status)) {
1495 *prdata = talloc_move(mem_ctx, &state->rdata);
1496 *prdata_len = state->rdata_len;
1497 return NT_STATUS_OK;
1500 /****************************************************************************
1501 Send data on an rpc pipe via trans. The prs_struct data must be the last
1502 pdu fragment of an NDR data stream.
1504 Receive response data from an rpc pipe, which may be large...
1506 Read the first fragment: unfortunately have to use SMBtrans for the first
1507 bit, then SMBreadX for subsequent bits.
1509 If first fragment received also wasn't the last fragment, continue
1510 getting fragments until we _do_ receive the last fragment.
1512 Request/Response PDU's look like the following...
1514 |<------------------PDU len----------------------------------------------->|
1515 |<-HDR_LEN-->|<--REQ LEN------>|.............|<-AUTH_HDRLEN->|<-AUTH_LEN-->|
1517 +------------+-----------------+-------------+---------------+-------------+
1518 | RPC HEADER | REQ/RESP HEADER | DATA ...... | AUTH_HDR | AUTH DATA |
1519 +------------+-----------------+-------------+---------------+-------------+
1521 Where the presence of the AUTH_HDR and AUTH DATA are dependent on the
1522 signing & sealing being negotiated.
1524 ****************************************************************************/
1526 struct rpc_api_pipe_state {
1527 struct event_context *ev;
1528 struct rpc_pipe_client *cli;
1529 uint8_t expected_pkt_type;
1531 prs_struct incoming_frag;
1532 struct ncacn_packet_header rhdr;
1534 prs_struct incoming_pdu; /* Incoming reply */
1535 uint32_t incoming_pdu_offset;
1538 static void rpc_api_pipe_trans_done(struct tevent_req *subreq);
1539 static void rpc_api_pipe_got_pdu(struct tevent_req *subreq);
1541 static struct tevent_req *rpc_api_pipe_send(TALLOC_CTX *mem_ctx,
1542 struct event_context *ev,
1543 struct rpc_pipe_client *cli,
1544 prs_struct *data, /* Outgoing PDU */
1545 uint8_t expected_pkt_type)
1547 struct tevent_req *req, *subreq;
1548 struct rpc_api_pipe_state *state;
1549 uint16_t max_recv_frag;
1552 req = tevent_req_create(mem_ctx, &state, struct rpc_api_pipe_state);
1558 state->expected_pkt_type = expected_pkt_type;
1559 state->incoming_pdu_offset = 0;
1561 prs_init_empty(&state->incoming_frag, state, UNMARSHALL);
1563 prs_init_empty(&state->incoming_pdu, state, UNMARSHALL);
1564 /* Make incoming_pdu dynamic with no memory. */
1565 prs_give_memory(&state->incoming_pdu, NULL, 0, true);
1568 * Ensure we're not sending too much.
1570 if (prs_offset(data) > cli->max_xmit_frag) {
1571 status = NT_STATUS_INVALID_PARAMETER;
1575 DEBUG(5,("rpc_api_pipe: %s\n", rpccli_pipe_txt(talloc_tos(), cli)));
1577 max_recv_frag = cli->max_recv_frag;
1580 max_recv_frag = RPC_HEADER_LEN + 10 + (sys_random() % 32);
1583 subreq = cli_api_pipe_send(state, ev, cli->transport,
1584 (uint8_t *)prs_data_p(data),
1585 prs_offset(data), max_recv_frag);
1586 if (subreq == NULL) {
1589 tevent_req_set_callback(subreq, rpc_api_pipe_trans_done, req);
1593 tevent_req_nterror(req, status);
1594 return tevent_req_post(req, ev);
1600 static void rpc_api_pipe_trans_done(struct tevent_req *subreq)
1602 struct tevent_req *req = tevent_req_callback_data(
1603 subreq, struct tevent_req);
1604 struct rpc_api_pipe_state *state = tevent_req_data(
1605 req, struct rpc_api_pipe_state);
1607 uint8_t *rdata = NULL;
1608 uint32_t rdata_len = 0;
1610 status = cli_api_pipe_recv(subreq, state, &rdata, &rdata_len);
1611 TALLOC_FREE(subreq);
1612 if (!NT_STATUS_IS_OK(status)) {
1613 DEBUG(5, ("cli_api_pipe failed: %s\n", nt_errstr(status)));
1614 tevent_req_nterror(req, status);
1618 if (rdata == NULL) {
1619 DEBUG(3,("rpc_api_pipe: %s failed to return data.\n",
1620 rpccli_pipe_txt(talloc_tos(), state->cli)));
1621 tevent_req_done(req);
1626 * This is equivalent to a talloc_steal - gives rdata to
1627 * the prs_struct state->incoming_frag.
1629 prs_give_memory(&state->incoming_frag, (char *)rdata, rdata_len, true);
1632 /* Ensure we have enough data for a pdu. */
1633 subreq = get_complete_frag_send(state, state->ev, state->cli,
1634 &state->rhdr, &state->incoming_frag);
1635 if (tevent_req_nomem(subreq, req)) {
1638 tevent_req_set_callback(subreq, rpc_api_pipe_got_pdu, req);
1641 static void rpc_api_pipe_got_pdu(struct tevent_req *subreq)
1643 struct tevent_req *req = tevent_req_callback_data(
1644 subreq, struct tevent_req);
1645 struct rpc_api_pipe_state *state = tevent_req_data(
1646 req, struct rpc_api_pipe_state);
1649 uint32_t rdata_len = 0;
1651 status = get_complete_frag_recv(subreq);
1652 TALLOC_FREE(subreq);
1653 if (!NT_STATUS_IS_OK(status)) {
1654 DEBUG(5, ("get_complete_frag failed: %s\n",
1655 nt_errstr(status)));
1656 tevent_req_nterror(req, status);
1660 status = cli_pipe_validate_current_pdu(
1661 state->cli, &state->rhdr, &state->incoming_frag,
1662 state->expected_pkt_type, &rdata, &rdata_len,
1663 &state->incoming_pdu);
1665 DEBUG(10,("rpc_api_pipe: got frag len of %u at offset %u: %s\n",
1666 (unsigned)prs_data_size(&state->incoming_frag),
1667 (unsigned)state->incoming_pdu_offset,
1668 nt_errstr(status)));
1670 if (!NT_STATUS_IS_OK(status)) {
1671 tevent_req_nterror(req, status);
1675 if ((state->rhdr.pfc_flags & DCERPC_PFC_FLAG_FIRST)
1676 && (state->rhdr.drep[0] == 0)) {
1678 * Set the data type correctly for big-endian data on the
1681 DEBUG(10,("rpc_api_pipe: On %s PDU data format is "
1683 rpccli_pipe_txt(talloc_tos(), state->cli)));
1684 prs_set_endian_data(&state->incoming_pdu, RPC_BIG_ENDIAN);
1687 * Check endianness on subsequent packets.
1689 if (state->incoming_frag.bigendian_data
1690 != state->incoming_pdu.bigendian_data) {
1691 DEBUG(0,("rpc_api_pipe: Error : Endianness changed from %s to "
1693 state->incoming_pdu.bigendian_data?"big":"little",
1694 state->incoming_frag.bigendian_data?"big":"little"));
1695 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1699 /* Now copy the data portion out of the pdu into rbuf. */
1700 if (!prs_force_grow(&state->incoming_pdu, rdata_len)) {
1701 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1705 memcpy(prs_data_p(&state->incoming_pdu) + state->incoming_pdu_offset,
1706 rdata, (size_t)rdata_len);
1707 state->incoming_pdu_offset += rdata_len;
1709 status = cli_pipe_reset_current_pdu(state->cli, &state->rhdr,
1710 &state->incoming_frag);
1711 if (!NT_STATUS_IS_OK(status)) {
1712 tevent_req_nterror(req, status);
1716 if (state->rhdr.pfc_flags & DCERPC_PFC_FLAG_LAST) {
1717 DEBUG(10,("rpc_api_pipe: %s returned %u bytes.\n",
1718 rpccli_pipe_txt(talloc_tos(), state->cli),
1719 (unsigned)prs_data_size(&state->incoming_pdu)));
1720 tevent_req_done(req);
1724 subreq = get_complete_frag_send(state, state->ev, state->cli,
1725 &state->rhdr, &state->incoming_frag);
1726 if (tevent_req_nomem(subreq, req)) {
1729 tevent_req_set_callback(subreq, rpc_api_pipe_got_pdu, req);
1732 static NTSTATUS rpc_api_pipe_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1733 prs_struct *reply_pdu)
1735 struct rpc_api_pipe_state *state = tevent_req_data(
1736 req, struct rpc_api_pipe_state);
1739 if (tevent_req_is_nterror(req, &status)) {
1743 *reply_pdu = state->incoming_pdu;
1744 reply_pdu->mem_ctx = mem_ctx;
1747 * Prevent state->incoming_pdu from being freed
1748 * when state is freed.
1750 talloc_steal(mem_ctx, prs_data_p(reply_pdu));
1751 prs_init_empty(&state->incoming_pdu, state, UNMARSHALL);
1753 return NT_STATUS_OK;
1756 /*******************************************************************
1757 ********************************************************************/
1759 static NTSTATUS dcerpc_push_dcerpc_auth(TALLOC_CTX *mem_ctx,
1760 enum dcerpc_AuthType auth_type,
1761 enum dcerpc_AuthLevel auth_level,
1762 uint8_t auth_pad_length,
1763 uint32_t auth_context_id,
1764 const DATA_BLOB *credentials,
1767 struct dcerpc_auth r;
1768 enum ndr_err_code ndr_err;
1770 r.auth_type = auth_type;
1771 r.auth_level = auth_level;
1772 r.auth_pad_length = auth_pad_length;
1773 r.auth_reserved = 0;
1774 r.auth_context_id = auth_context_id;
1775 r.credentials = *credentials;
1777 ndr_err = ndr_push_struct_blob(blob, mem_ctx, &r,
1778 (ndr_push_flags_fn_t)ndr_push_dcerpc_auth);
1779 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1780 return ndr_map_error2ntstatus(ndr_err);
1783 if (DEBUGLEVEL >= 10) {
1784 NDR_PRINT_DEBUG(dcerpc_auth, &r);
1787 return NT_STATUS_OK;
1790 /*******************************************************************
1791 Creates krb5 auth bind.
1792 ********************************************************************/
1794 static NTSTATUS create_krb5_auth_bind_req(struct rpc_pipe_client *cli,
1795 enum dcerpc_AuthLevel auth_level,
1796 DATA_BLOB *auth_info)
1801 struct kerberos_auth_struct *a = cli->auth->a_u.kerberos_auth;
1802 DATA_BLOB tkt = data_blob_null;
1803 DATA_BLOB tkt_wrapped = data_blob_null;
1805 DEBUG(5, ("create_krb5_auth_bind_req: creating a service ticket for principal %s\n",
1806 a->service_principal ));
1808 /* Create the ticket for the service principal and return it in a gss-api wrapped blob. */
1810 ret = cli_krb5_get_ticket(a->service_principal, 0, &tkt,
1811 &a->session_key, (uint32)AP_OPTS_MUTUAL_REQUIRED, NULL, NULL, NULL);
1814 DEBUG(1,("create_krb5_auth_bind_req: cli_krb5_get_ticket for principal %s "
1816 a->service_principal,
1817 error_message(ret) ));
1819 data_blob_free(&tkt);
1820 return NT_STATUS_INVALID_PARAMETER;
1823 /* wrap that up in a nice GSS-API wrapping */
1824 tkt_wrapped = spnego_gen_krb5_wrap(tkt, TOK_ID_KRB_AP_REQ);
1826 data_blob_free(&tkt);
1828 status = dcerpc_push_dcerpc_auth(cli,
1829 DCERPC_AUTH_TYPE_KRB5,
1831 0, /* auth_pad_length */
1832 1, /* auth_context_id */
1835 if (!NT_STATUS_IS_OK(status)) {
1836 data_blob_free(&tkt_wrapped);
1840 DEBUG(5, ("create_krb5_auth_bind_req: Created krb5 GSS blob :\n"));
1841 dump_data(5, tkt_wrapped.data, tkt_wrapped.length);
1843 return NT_STATUS_OK;
1845 return NT_STATUS_INVALID_PARAMETER;
1849 /*******************************************************************
1850 Creates SPNEGO NTLMSSP auth bind.
1851 ********************************************************************/
1853 static NTSTATUS create_spnego_ntlmssp_auth_rpc_bind_req(struct rpc_pipe_client *cli,
1854 enum dcerpc_AuthLevel auth_level,
1855 DATA_BLOB *auth_info)
1858 DATA_BLOB null_blob = data_blob_null;
1859 DATA_BLOB request = data_blob_null;
1860 DATA_BLOB spnego_msg = data_blob_null;
1862 DEBUG(5, ("create_spnego_ntlmssp_auth_rpc_bind_req: Processing NTLMSSP Negotiate\n"));
1863 status = ntlmssp_update(cli->auth->a_u.ntlmssp_state,
1867 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1868 data_blob_free(&request);
1872 /* Wrap this in SPNEGO. */
1873 spnego_msg = gen_negTokenInit(OID_NTLMSSP, request);
1875 data_blob_free(&request);
1877 status = dcerpc_push_dcerpc_auth(cli,
1878 DCERPC_AUTH_TYPE_SPNEGO,
1880 0, /* auth_pad_length */
1881 1, /* auth_context_id */
1884 if (!NT_STATUS_IS_OK(status)) {
1885 data_blob_free(&spnego_msg);
1889 DEBUG(5, ("create_spnego_ntlmssp_auth_rpc_bind_req: NTLMSSP Negotiate:\n"));
1890 dump_data(5, spnego_msg.data, spnego_msg.length);
1892 return NT_STATUS_OK;
1895 /*******************************************************************
1896 Creates NTLMSSP auth bind.
1897 ********************************************************************/
1899 static NTSTATUS create_ntlmssp_auth_rpc_bind_req(struct rpc_pipe_client *cli,
1900 enum dcerpc_AuthLevel auth_level,
1901 DATA_BLOB *auth_info)
1904 DATA_BLOB null_blob = data_blob_null;
1905 DATA_BLOB request = data_blob_null;
1907 DEBUG(5, ("create_ntlmssp_auth_rpc_bind_req: Processing NTLMSSP Negotiate\n"));
1908 status = ntlmssp_update(cli->auth->a_u.ntlmssp_state,
1912 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1913 data_blob_free(&request);
1917 status = dcerpc_push_dcerpc_auth(cli,
1918 DCERPC_AUTH_TYPE_NTLMSSP,
1920 0, /* auth_pad_length */
1921 1, /* auth_context_id */
1924 if (!NT_STATUS_IS_OK(status)) {
1925 data_blob_free(&request);
1929 DEBUG(5, ("create_ntlmssp_auth_rpc_bind_req: NTLMSSP Negotiate:\n"));
1930 dump_data(5, request.data, request.length);
1932 return NT_STATUS_OK;
1935 /*******************************************************************
1936 Creates schannel auth bind.
1937 ********************************************************************/
1939 static NTSTATUS create_schannel_auth_rpc_bind_req(struct rpc_pipe_client *cli,
1940 enum dcerpc_AuthLevel auth_level,
1941 DATA_BLOB *auth_info)
1944 struct NL_AUTH_MESSAGE r;
1945 DATA_BLOB schannel_blob;
1947 /* Use lp_workgroup() if domain not specified */
1949 if (!cli->auth->domain || !cli->auth->domain[0]) {
1950 cli->auth->domain = talloc_strdup(cli, lp_workgroup());
1951 if (cli->auth->domain == NULL) {
1952 return NT_STATUS_NO_MEMORY;
1957 * Now marshall the data into the auth parse_struct.
1960 r.MessageType = NL_NEGOTIATE_REQUEST;
1961 r.Flags = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
1962 NL_FLAG_OEM_NETBIOS_COMPUTER_NAME;
1963 r.oem_netbios_domain.a = cli->auth->domain;
1964 r.oem_netbios_computer.a = global_myname();
1966 status = dcerpc_push_schannel_bind(cli, &r, &schannel_blob);
1967 if (!NT_STATUS_IS_OK(status)) {
1971 status = dcerpc_push_dcerpc_auth(cli,
1972 DCERPC_AUTH_TYPE_SCHANNEL,
1974 0, /* auth_pad_length */
1975 1, /* auth_context_id */
1978 if (!NT_STATUS_IS_OK(status)) {
1982 return NT_STATUS_OK;
1985 /*******************************************************************
1986 ********************************************************************/
1988 static NTSTATUS init_dcerpc_ctx_list(TALLOC_CTX *mem_ctx,
1989 const struct ndr_syntax_id *abstract_syntax,
1990 const struct ndr_syntax_id *transfer_syntax,
1991 struct dcerpc_ctx_list **ctx_list_p)
1993 struct dcerpc_ctx_list *ctx_list;
1995 ctx_list = talloc_array(mem_ctx, struct dcerpc_ctx_list, 1);
1996 NT_STATUS_HAVE_NO_MEMORY(ctx_list);
1998 ctx_list[0].context_id = 0;
1999 ctx_list[0].num_transfer_syntaxes = 1;
2000 ctx_list[0].abstract_syntax = *abstract_syntax;
2001 ctx_list[0].transfer_syntaxes = talloc_array(ctx_list,
2002 struct ndr_syntax_id,
2003 ctx_list[0].num_transfer_syntaxes);
2004 NT_STATUS_HAVE_NO_MEMORY(ctx_list[0].transfer_syntaxes);
2005 ctx_list[0].transfer_syntaxes[0] = *transfer_syntax;
2007 *ctx_list_p = ctx_list;
2009 return NT_STATUS_OK;
2012 /*******************************************************************
2013 Creates the internals of a DCE/RPC bind request or alter context PDU.
2014 ********************************************************************/
2016 static NTSTATUS create_bind_or_alt_ctx_internal(enum dcerpc_pkt_type ptype,
2017 prs_struct *rpc_out,
2019 const struct ndr_syntax_id *abstract,
2020 const struct ndr_syntax_id *transfer,
2021 const DATA_BLOB *auth_info)
2023 uint16 auth_len = auth_info->length;
2024 uint16 frag_len = 0;
2026 union dcerpc_payload u;
2028 struct dcerpc_ctx_list *ctx_list;
2030 status = init_dcerpc_ctx_list(rpc_out->mem_ctx, abstract, transfer,
2032 if (!NT_STATUS_IS_OK(status)) {
2036 u.bind.max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
2037 u.bind.max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
2038 u.bind.assoc_group_id = 0x0;
2039 u.bind.num_contexts = 1;
2040 u.bind.ctx_list = ctx_list;
2041 u.bind.auth_info = *auth_info;
2043 /* Start building the frag length. */
2044 frag_len = RPC_HEADER_LEN + RPC_HDR_RB_LEN(&u.bind) + auth_len;
2046 status = dcerpc_push_ncacn_packet(rpc_out->mem_ctx,
2048 DCERPC_PFC_FLAG_FIRST |
2049 DCERPC_PFC_FLAG_LAST,
2051 auth_len ? auth_len - RPC_HDR_AUTH_LEN : 0,
2055 if (!NT_STATUS_IS_OK(status)) {
2056 DEBUG(0,("create_bind_or_alt_ctx_internal: failed to marshall RPC_HDR_RB.\n"));
2060 if (!prs_copy_data_in(rpc_out, (char *)blob.data, blob.length)) {
2061 return NT_STATUS_NO_MEMORY;
2064 return NT_STATUS_OK;
2067 /*******************************************************************
2068 Creates a DCE/RPC bind request.
2069 ********************************************************************/
2071 static NTSTATUS create_rpc_bind_req(struct rpc_pipe_client *cli,
2072 prs_struct *rpc_out,
2074 const struct ndr_syntax_id *abstract,
2075 const struct ndr_syntax_id *transfer,
2076 enum pipe_auth_type auth_type,
2077 enum dcerpc_AuthLevel auth_level)
2079 DATA_BLOB auth_info = data_blob_null;
2080 NTSTATUS ret = NT_STATUS_OK;
2082 switch (auth_type) {
2083 case PIPE_AUTH_TYPE_SCHANNEL:
2084 ret = create_schannel_auth_rpc_bind_req(cli, auth_level, &auth_info);
2085 if (!NT_STATUS_IS_OK(ret)) {
2090 case PIPE_AUTH_TYPE_NTLMSSP:
2091 ret = create_ntlmssp_auth_rpc_bind_req(cli, auth_level, &auth_info);
2092 if (!NT_STATUS_IS_OK(ret)) {
2097 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
2098 ret = create_spnego_ntlmssp_auth_rpc_bind_req(cli, auth_level, &auth_info);
2099 if (!NT_STATUS_IS_OK(ret)) {
2104 case PIPE_AUTH_TYPE_KRB5:
2105 ret = create_krb5_auth_bind_req(cli, auth_level, &auth_info);
2106 if (!NT_STATUS_IS_OK(ret)) {
2111 case PIPE_AUTH_TYPE_NONE:
2115 /* "Can't" happen. */
2116 return NT_STATUS_INVALID_INFO_CLASS;
2119 ret = create_bind_or_alt_ctx_internal(DCERPC_PKT_BIND,
2128 /*******************************************************************
2129 Create and add the NTLMSSP sign/seal auth header and data.
2130 ********************************************************************/
2132 static NTSTATUS add_ntlmssp_auth_footer(struct rpc_pipe_client *cli,
2133 uint32 ss_padding_len,
2134 prs_struct *rpc_out)
2136 DATA_BLOB auth_info;
2138 DATA_BLOB auth_blob = data_blob_null;
2139 uint16_t data_and_pad_len =
2140 prs_offset(rpc_out) - RPC_HEADER_LEN - RPC_HDR_RESP_LEN;
2142 if (!cli->auth->a_u.ntlmssp_state) {
2143 return NT_STATUS_INVALID_PARAMETER;
2146 switch (cli->auth->auth_level) {
2147 case DCERPC_AUTH_LEVEL_PRIVACY:
2148 /* Data portion is encrypted. */
2149 status = ntlmssp_seal_packet(cli->auth->a_u.ntlmssp_state,
2150 prs_get_mem_context(rpc_out),
2151 (unsigned char *)prs_data_p(rpc_out)
2155 (unsigned char *)prs_data_p(rpc_out),
2156 (size_t)prs_offset(rpc_out),
2158 if (!NT_STATUS_IS_OK(status)) {
2163 case DCERPC_AUTH_LEVEL_INTEGRITY:
2164 /* Data is signed. */
2165 status = ntlmssp_sign_packet(cli->auth->a_u.ntlmssp_state,
2166 prs_get_mem_context(rpc_out),
2167 (unsigned char *)prs_data_p(rpc_out)
2171 (unsigned char *)prs_data_p(rpc_out),
2172 (size_t)prs_offset(rpc_out),
2174 if (!NT_STATUS_IS_OK(status)) {
2181 smb_panic("bad auth level");
2183 return NT_STATUS_INVALID_PARAMETER;
2186 /* Finally marshall the blob. */
2187 status = dcerpc_push_dcerpc_auth(prs_get_mem_context(rpc_out),
2188 map_pipe_auth_type_to_rpc_auth_type(cli->auth->auth_type),
2189 cli->auth->auth_level,
2191 1 /* context id. */,
2194 if (!NT_STATUS_IS_OK(status)) {
2198 if (!prs_copy_data_in(rpc_out, (const char *)auth_info.data, auth_info.length)) {
2199 DEBUG(0, ("add_ntlmssp_auth_footer: failed to add %u bytes auth blob.\n",
2200 (unsigned int)auth_info.length));
2201 return NT_STATUS_NO_MEMORY;
2204 return NT_STATUS_OK;
2207 /*******************************************************************
2208 Create and add the schannel sign/seal auth header and data.
2209 ********************************************************************/
2211 static NTSTATUS add_schannel_auth_footer(struct rpc_pipe_client *cli,
2212 uint32 ss_padding_len,
2213 prs_struct *rpc_out)
2215 DATA_BLOB auth_info;
2216 struct schannel_state *sas = cli->auth->a_u.schannel_auth;
2217 char *data_p = prs_data_p(rpc_out) + RPC_HEADER_LEN + RPC_HDR_RESP_LEN;
2218 size_t data_and_pad_len = prs_offset(rpc_out) - RPC_HEADER_LEN - RPC_HDR_RESP_LEN;
2223 return NT_STATUS_INVALID_PARAMETER;
2226 DEBUG(10,("add_schannel_auth_footer: SCHANNEL seq_num=%d\n",
2229 switch (cli->auth->auth_level) {
2230 case DCERPC_AUTH_LEVEL_PRIVACY:
2231 status = netsec_outgoing_packet(sas,
2238 case DCERPC_AUTH_LEVEL_INTEGRITY:
2239 status = netsec_outgoing_packet(sas,
2247 status = NT_STATUS_INTERNAL_ERROR;
2251 if (!NT_STATUS_IS_OK(status)) {
2252 DEBUG(1,("add_schannel_auth_footer: failed to process packet: %s\n",
2253 nt_errstr(status)));
2257 if (DEBUGLEVEL >= 10) {
2258 dump_NL_AUTH_SIGNATURE(talloc_tos(), &blob);
2261 /* Finally marshall the blob. */
2262 status = dcerpc_push_dcerpc_auth(prs_get_mem_context(rpc_out),
2263 map_pipe_auth_type_to_rpc_auth_type(cli->auth->auth_type),
2264 cli->auth->auth_level,
2266 1 /* context id. */,
2269 if (!NT_STATUS_IS_OK(status)) {
2273 if (!prs_copy_data_in(rpc_out, (const char *)auth_info.data, auth_info.length)) {
2274 return NT_STATUS_NO_MEMORY;
2277 return NT_STATUS_OK;
2280 /*******************************************************************
2281 Calculate how much data we're going to send in this packet, also
2282 work out any sign/seal padding length.
2283 ********************************************************************/
2285 static uint32 calculate_data_len_tosend(struct rpc_pipe_client *cli,
2289 uint32 *p_ss_padding)
2291 uint32 data_space, data_len;
2294 if ((data_left > 0) && (sys_random() % 2)) {
2295 data_left = MAX(data_left/2, 1);
2299 switch (cli->auth->auth_level) {
2300 case DCERPC_AUTH_LEVEL_NONE:
2301 case DCERPC_AUTH_LEVEL_CONNECT:
2302 data_space = cli->max_xmit_frag - RPC_HEADER_LEN - RPC_HDR_REQ_LEN;
2303 data_len = MIN(data_space, data_left);
2306 *p_frag_len = RPC_HEADER_LEN + RPC_HDR_REQ_LEN + data_len;
2309 case DCERPC_AUTH_LEVEL_INTEGRITY:
2310 case DCERPC_AUTH_LEVEL_PRIVACY:
2311 /* Treat the same for all authenticated rpc requests. */
2312 switch(cli->auth->auth_type) {
2313 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
2314 case PIPE_AUTH_TYPE_NTLMSSP:
2315 *p_auth_len = NTLMSSP_SIG_SIZE;
2317 case PIPE_AUTH_TYPE_SCHANNEL:
2318 *p_auth_len = RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN;
2321 smb_panic("bad auth type");
2325 data_space = cli->max_xmit_frag - RPC_HEADER_LEN - RPC_HDR_REQ_LEN -
2326 RPC_HDR_AUTH_LEN - *p_auth_len;
2328 data_len = MIN(data_space, data_left);
2330 if (data_len % CLIENT_NDR_PADDING_SIZE) {
2331 *p_ss_padding = CLIENT_NDR_PADDING_SIZE - (data_len % CLIENT_NDR_PADDING_SIZE);
2333 *p_frag_len = RPC_HEADER_LEN + RPC_HDR_REQ_LEN + /* Normal headers. */
2334 data_len + *p_ss_padding + /* data plus padding. */
2335 RPC_HDR_AUTH_LEN + *p_auth_len; /* Auth header and auth data. */
2339 smb_panic("bad auth level");
2345 /*******************************************************************
2347 Does an rpc request on a pipe. Incoming data is NDR encoded in in_data.
2348 Reply is NDR encoded in out_data. Splits the data stream into RPC PDU's
2349 and deals with signing/sealing details.
2350 ********************************************************************/
2352 struct rpc_api_pipe_req_state {
2353 struct event_context *ev;
2354 struct rpc_pipe_client *cli;
2357 prs_struct *req_data;
2358 uint32_t req_data_sent;
2359 prs_struct outgoing_frag;
2360 prs_struct reply_pdu;
2363 static void rpc_api_pipe_req_write_done(struct tevent_req *subreq);
2364 static void rpc_api_pipe_req_done(struct tevent_req *subreq);
2365 static NTSTATUS prepare_next_frag(struct rpc_api_pipe_req_state *state,
2366 bool *is_last_frag);
2368 struct tevent_req *rpc_api_pipe_req_send(TALLOC_CTX *mem_ctx,
2369 struct event_context *ev,
2370 struct rpc_pipe_client *cli,
2372 prs_struct *req_data)
2374 struct tevent_req *req, *subreq;
2375 struct rpc_api_pipe_req_state *state;
2379 req = tevent_req_create(mem_ctx, &state,
2380 struct rpc_api_pipe_req_state);
2386 state->op_num = op_num;
2387 state->req_data = req_data;
2388 state->req_data_sent = 0;
2389 state->call_id = get_rpc_call_id();
2391 if (cli->max_xmit_frag
2392 < RPC_HEADER_LEN + RPC_HDR_REQ_LEN + RPC_MAX_SIGN_SIZE) {
2393 /* Server is screwed up ! */
2394 status = NT_STATUS_INVALID_PARAMETER;
2398 prs_init_empty(&state->reply_pdu, state, UNMARSHALL);
2400 if (!prs_init(&state->outgoing_frag, cli->max_xmit_frag,
2405 status = prepare_next_frag(state, &is_last_frag);
2406 if (!NT_STATUS_IS_OK(status)) {
2411 subreq = rpc_api_pipe_send(state, ev, state->cli,
2412 &state->outgoing_frag,
2413 DCERPC_PKT_RESPONSE);
2414 if (subreq == NULL) {
2417 tevent_req_set_callback(subreq, rpc_api_pipe_req_done, req);
2419 subreq = rpc_write_send(
2420 state, ev, cli->transport,
2421 (uint8_t *)prs_data_p(&state->outgoing_frag),
2422 prs_offset(&state->outgoing_frag));
2423 if (subreq == NULL) {
2426 tevent_req_set_callback(subreq, rpc_api_pipe_req_write_done,
2432 tevent_req_nterror(req, status);
2433 return tevent_req_post(req, ev);
2439 static NTSTATUS prepare_next_frag(struct rpc_api_pipe_req_state *state,
2442 uint32_t data_sent_thistime;
2446 uint32_t ss_padding;
2448 char pad[8] = { 0, };
2450 union dcerpc_payload u;
2453 data_left = prs_offset(state->req_data) - state->req_data_sent;
2455 data_sent_thistime = calculate_data_len_tosend(
2456 state->cli, data_left, &frag_len, &auth_len, &ss_padding);
2458 if (state->req_data_sent == 0) {
2459 flags = DCERPC_PFC_FLAG_FIRST;
2462 if (data_sent_thistime == data_left) {
2463 flags |= DCERPC_PFC_FLAG_LAST;
2466 if (!prs_set_offset(&state->outgoing_frag, 0)) {
2467 return NT_STATUS_NO_MEMORY;
2470 ZERO_STRUCT(u.request);
2472 u.request.alloc_hint = prs_offset(state->req_data);
2473 u.request.context_id = 0;
2474 u.request.opnum = state->op_num;
2476 status = dcerpc_push_ncacn_packet(prs_get_mem_context(&state->outgoing_frag),
2484 if (!NT_STATUS_IS_OK(status)) {
2488 if (!prs_copy_data_in(&state->outgoing_frag, (const char *)blob.data, blob.length)) {
2489 return NT_STATUS_NO_MEMORY;
2492 /* Copy in the data, plus any ss padding. */
2493 if (!prs_append_some_prs_data(&state->outgoing_frag,
2494 state->req_data, state->req_data_sent,
2495 data_sent_thistime)) {
2496 return NT_STATUS_NO_MEMORY;
2499 /* Copy the sign/seal padding data. */
2500 if (!prs_copy_data_in(&state->outgoing_frag, pad, ss_padding)) {
2501 return NT_STATUS_NO_MEMORY;
2504 /* Generate any auth sign/seal and add the auth footer. */
2505 switch (state->cli->auth->auth_type) {
2506 case PIPE_AUTH_TYPE_NONE:
2507 status = NT_STATUS_OK;
2509 case PIPE_AUTH_TYPE_NTLMSSP:
2510 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
2511 status = add_ntlmssp_auth_footer(state->cli, ss_padding,
2512 &state->outgoing_frag);
2514 case PIPE_AUTH_TYPE_SCHANNEL:
2515 status = add_schannel_auth_footer(state->cli, ss_padding,
2516 &state->outgoing_frag);
2519 status = NT_STATUS_INVALID_PARAMETER;
2523 state->req_data_sent += data_sent_thistime;
2524 *is_last_frag = ((flags & DCERPC_PFC_FLAG_LAST) != 0);
2529 static void rpc_api_pipe_req_write_done(struct tevent_req *subreq)
2531 struct tevent_req *req = tevent_req_callback_data(
2532 subreq, struct tevent_req);
2533 struct rpc_api_pipe_req_state *state = tevent_req_data(
2534 req, struct rpc_api_pipe_req_state);
2538 status = rpc_write_recv(subreq);
2539 TALLOC_FREE(subreq);
2540 if (!NT_STATUS_IS_OK(status)) {
2541 tevent_req_nterror(req, status);
2545 status = prepare_next_frag(state, &is_last_frag);
2546 if (!NT_STATUS_IS_OK(status)) {
2547 tevent_req_nterror(req, status);
2552 subreq = rpc_api_pipe_send(state, state->ev, state->cli,
2553 &state->outgoing_frag,
2554 DCERPC_PKT_RESPONSE);
2555 if (tevent_req_nomem(subreq, req)) {
2558 tevent_req_set_callback(subreq, rpc_api_pipe_req_done, req);
2560 subreq = rpc_write_send(
2562 state->cli->transport,
2563 (uint8_t *)prs_data_p(&state->outgoing_frag),
2564 prs_offset(&state->outgoing_frag));
2565 if (tevent_req_nomem(subreq, req)) {
2568 tevent_req_set_callback(subreq, rpc_api_pipe_req_write_done,
2573 static void rpc_api_pipe_req_done(struct tevent_req *subreq)
2575 struct tevent_req *req = tevent_req_callback_data(
2576 subreq, struct tevent_req);
2577 struct rpc_api_pipe_req_state *state = tevent_req_data(
2578 req, struct rpc_api_pipe_req_state);
2581 status = rpc_api_pipe_recv(subreq, state, &state->reply_pdu);
2582 TALLOC_FREE(subreq);
2583 if (!NT_STATUS_IS_OK(status)) {
2584 tevent_req_nterror(req, status);
2587 tevent_req_done(req);
2590 NTSTATUS rpc_api_pipe_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
2591 prs_struct *reply_pdu)
2593 struct rpc_api_pipe_req_state *state = tevent_req_data(
2594 req, struct rpc_api_pipe_req_state);
2597 if (tevent_req_is_nterror(req, &status)) {
2599 * We always have to initialize to reply pdu, even if there is
2600 * none. The rpccli_* caller routines expect this.
2602 prs_init_empty(reply_pdu, mem_ctx, UNMARSHALL);
2606 *reply_pdu = state->reply_pdu;
2607 reply_pdu->mem_ctx = mem_ctx;
2610 * Prevent state->req_pdu from being freed
2611 * when state is freed.
2613 talloc_steal(mem_ctx, prs_data_p(reply_pdu));
2614 prs_init_empty(&state->reply_pdu, state, UNMARSHALL);
2616 return NT_STATUS_OK;
2620 /****************************************************************************
2621 Set the handle state.
2622 ****************************************************************************/
2624 static bool rpc_pipe_set_hnd_state(struct rpc_pipe_client *cli,
2625 const char *pipe_name, uint16 device_state)
2627 bool state_set = False;
2629 uint16 setup[2]; /* only need 2 uint16 setup parameters */
2630 char *rparam = NULL;
2632 uint32 rparam_len, rdata_len;
2634 if (pipe_name == NULL)
2637 DEBUG(5,("Set Handle state Pipe[%x]: %s - device state:%x\n",
2638 cli->fnum, pipe_name, device_state));
2640 /* create parameters: device state */
2641 SSVAL(param, 0, device_state);
2643 /* create setup parameters. */
2645 setup[1] = cli->fnum; /* pipe file handle. got this from an SMBOpenX. */
2647 /* send the data on \PIPE\ */
2648 if (cli_api_pipe(cli->cli, "\\PIPE\\",
2649 setup, 2, 0, /* setup, length, max */
2650 param, 2, 0, /* param, length, max */
2651 NULL, 0, 1024, /* data, length, max */
2652 &rparam, &rparam_len, /* return param, length */
2653 &rdata, &rdata_len)) /* return data, length */
2655 DEBUG(5, ("Set Handle state: return OK\n"));
2666 /****************************************************************************
2667 Check the rpc bind acknowledge response.
2668 ****************************************************************************/
2670 static bool check_bind_response(const struct dcerpc_bind_ack *r,
2671 const struct ndr_syntax_id *transfer)
2673 struct dcerpc_ack_ctx ctx;
2675 if (r->secondary_address_size == 0) {
2676 DEBUG(4,("Ignoring length check -- ASU bug (server didn't fill in the pipe name correctly)"));
2679 if (r->num_results < 1 || !r->ctx_list) {
2683 ctx = r->ctx_list[0];
2685 /* check the transfer syntax */
2686 if ((ctx.syntax.if_version != transfer->if_version) ||
2687 (memcmp(&ctx.syntax.uuid, &transfer->uuid, sizeof(transfer->uuid)) !=0)) {
2688 DEBUG(2,("bind_rpc_pipe: transfer syntax differs\n"));
2692 if (r->num_results != 0x1 || ctx.result != 0) {
2693 DEBUG(2,("bind_rpc_pipe: bind denied results: %d reason: %x\n",
2694 r->num_results, ctx.reason));
2697 DEBUG(5,("check_bind_response: accepted!\n"));
2701 /*******************************************************************
2702 Creates a DCE/RPC bind authentication response.
2703 This is the packet that is sent back to the server once we
2704 have received a BIND-ACK, to finish the third leg of
2705 the authentication handshake.
2706 ********************************************************************/
2708 static NTSTATUS create_rpc_bind_auth3(struct rpc_pipe_client *cli,
2710 enum pipe_auth_type auth_type,
2711 enum dcerpc_AuthLevel auth_level,
2712 DATA_BLOB *pauth_blob,
2713 prs_struct *rpc_out)
2715 uint16_t auth_len = pauth_blob->length;
2716 uint16_t frag_len = 0;
2718 union dcerpc_payload u;
2723 status = dcerpc_push_dcerpc_auth(prs_get_mem_context(rpc_out),
2724 map_pipe_auth_type_to_rpc_auth_type(auth_type),
2726 0, /* auth_pad_length */
2727 1, /* auth_context_id */
2729 &u.auth3.auth_info);
2730 if (!NT_STATUS_IS_OK(status)) {
2734 /* Start building the frag length. */
2735 frag_len = RPC_HEADER_LEN + 4 /* pad */ + RPC_HDR_AUTH_LEN + auth_len;
2737 status = dcerpc_push_ncacn_packet(prs_get_mem_context(rpc_out),
2739 DCERPC_PFC_FLAG_FIRST |
2740 DCERPC_PFC_FLAG_LAST,
2742 auth_len ? auth_len - RPC_HDR_AUTH_LEN : 0,
2746 if (!NT_STATUS_IS_OK(status)) {
2747 DEBUG(0,("create_bind_or_alt_ctx_internal: failed to marshall RPC_HDR_RB.\n"));
2751 if (!prs_copy_data_in(rpc_out, (char *)blob.data, blob.length)) {
2752 return NT_STATUS_NO_MEMORY;
2755 return NT_STATUS_OK;
2758 /*******************************************************************
2759 Creates a DCE/RPC bind alter context authentication request which
2760 may contain a spnego auth blobl
2761 ********************************************************************/
2763 static NTSTATUS create_rpc_alter_context(uint32 rpc_call_id,
2764 const struct ndr_syntax_id *abstract,
2765 const struct ndr_syntax_id *transfer,
2766 enum dcerpc_AuthLevel auth_level,
2767 const DATA_BLOB *pauth_blob, /* spnego auth blob already created. */
2768 prs_struct *rpc_out)
2770 DATA_BLOB auth_info;
2773 status = dcerpc_push_dcerpc_auth(prs_get_mem_context(rpc_out),
2774 DCERPC_AUTH_TYPE_SPNEGO,
2776 0, /* auth_pad_length */
2777 1, /* auth_context_id */
2780 if (!NT_STATUS_IS_OK(status)) {
2785 status = create_bind_or_alt_ctx_internal(DCERPC_PKT_ALTER,
2791 if (!NT_STATUS_IS_OK(status)) {
2798 /****************************************************************************
2800 ****************************************************************************/
2802 struct rpc_pipe_bind_state {
2803 struct event_context *ev;
2804 struct rpc_pipe_client *cli;
2806 uint32_t rpc_call_id;
2809 static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq);
2810 static NTSTATUS rpc_finish_auth3_bind_send(struct tevent_req *req,
2811 struct rpc_pipe_bind_state *state,
2812 struct ncacn_packet *r,
2813 prs_struct *reply_pdu);
2814 static void rpc_bind_auth3_write_done(struct tevent_req *subreq);
2815 static NTSTATUS rpc_finish_spnego_ntlmssp_bind_send(struct tevent_req *req,
2816 struct rpc_pipe_bind_state *state,
2817 struct ncacn_packet *r,
2818 prs_struct *reply_pdu);
2819 static void rpc_bind_ntlmssp_api_done(struct tevent_req *subreq);
2821 struct tevent_req *rpc_pipe_bind_send(TALLOC_CTX *mem_ctx,
2822 struct event_context *ev,
2823 struct rpc_pipe_client *cli,
2824 struct cli_pipe_auth_data *auth)
2826 struct tevent_req *req, *subreq;
2827 struct rpc_pipe_bind_state *state;
2830 req = tevent_req_create(mem_ctx, &state, struct rpc_pipe_bind_state);
2835 DEBUG(5,("Bind RPC Pipe: %s auth_type %u, auth_level %u\n",
2836 rpccli_pipe_txt(talloc_tos(), cli),
2837 (unsigned int)auth->auth_type,
2838 (unsigned int)auth->auth_level ));
2842 state->rpc_call_id = get_rpc_call_id();
2844 prs_init_empty(&state->rpc_out, state, MARSHALL);
2846 cli->auth = talloc_move(cli, &auth);
2848 /* Marshall the outgoing data. */
2849 status = create_rpc_bind_req(cli, &state->rpc_out,
2851 &cli->abstract_syntax,
2852 &cli->transfer_syntax,
2853 cli->auth->auth_type,
2854 cli->auth->auth_level);
2856 if (!NT_STATUS_IS_OK(status)) {
2860 subreq = rpc_api_pipe_send(state, ev, cli, &state->rpc_out,
2861 DCERPC_PKT_BIND_ACK);
2862 if (subreq == NULL) {
2865 tevent_req_set_callback(subreq, rpc_pipe_bind_step_one_done, req);
2869 tevent_req_nterror(req, status);
2870 return tevent_req_post(req, ev);
2876 static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq)
2878 struct tevent_req *req = tevent_req_callback_data(
2879 subreq, struct tevent_req);
2880 struct rpc_pipe_bind_state *state = tevent_req_data(
2881 req, struct rpc_pipe_bind_state);
2882 prs_struct reply_pdu;
2884 struct ncacn_packet r;
2887 status = rpc_api_pipe_recv(subreq, talloc_tos(), &reply_pdu);
2888 TALLOC_FREE(subreq);
2889 if (!NT_STATUS_IS_OK(status)) {
2890 DEBUG(3, ("rpc_pipe_bind: %s bind request returned %s\n",
2891 rpccli_pipe_txt(talloc_tos(), state->cli),
2892 nt_errstr(status)));
2893 tevent_req_nterror(req, status);
2897 blob = data_blob_const(prs_data_p(&reply_pdu),
2898 prs_data_size(&reply_pdu));
2900 status = dcerpc_pull_ncacn_packet(talloc_tos(), &blob, &r);
2901 if (!NT_STATUS_IS_OK(status)) {
2902 tevent_req_nterror(req, status);
2906 if (!check_bind_response(&r.u.bind_ack, &state->cli->transfer_syntax)) {
2907 DEBUG(2, ("rpc_pipe_bind: check_bind_response failed.\n"));
2908 tevent_req_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2912 state->cli->max_xmit_frag = r.u.bind_ack.max_xmit_frag;
2913 state->cli->max_recv_frag = r.u.bind_ack.max_recv_frag;
2916 * For authenticated binds we may need to do 3 or 4 leg binds.
2919 switch(state->cli->auth->auth_type) {
2921 case PIPE_AUTH_TYPE_NONE:
2922 case PIPE_AUTH_TYPE_SCHANNEL:
2923 /* Bind complete. */
2924 tevent_req_done(req);
2927 case PIPE_AUTH_TYPE_NTLMSSP:
2928 /* Need to send AUTH3 packet - no reply. */
2929 status = rpc_finish_auth3_bind_send(req, state, &r,
2931 if (!NT_STATUS_IS_OK(status)) {
2932 tevent_req_nterror(req, status);
2936 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
2937 /* Need to send alter context request and reply. */
2938 status = rpc_finish_spnego_ntlmssp_bind_send(req, state, &r,
2940 if (!NT_STATUS_IS_OK(status)) {
2941 tevent_req_nterror(req, status);
2945 case PIPE_AUTH_TYPE_KRB5:
2949 DEBUG(0,("cli_finish_bind_auth: unknown auth type %u\n",
2950 (unsigned int)state->cli->auth->auth_type));
2951 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
2955 static NTSTATUS rpc_finish_auth3_bind_send(struct tevent_req *req,
2956 struct rpc_pipe_bind_state *state,
2957 struct ncacn_packet *r,
2958 prs_struct *reply_pdu)
2960 DATA_BLOB server_response = data_blob_null;
2961 DATA_BLOB client_reply = data_blob_null;
2962 struct rpc_hdr_auth_info hdr_auth;
2963 struct tevent_req *subreq;
2966 if ((r->auth_length == 0)
2967 || (r->frag_length < r->auth_length + RPC_HDR_AUTH_LEN)) {
2968 return NT_STATUS_INVALID_PARAMETER;
2971 if (!prs_set_offset(
2973 r->frag_length - r->auth_length - RPC_HDR_AUTH_LEN)) {
2974 return NT_STATUS_INVALID_PARAMETER;
2977 if (!smb_io_rpc_hdr_auth("hdr_auth", &hdr_auth, reply_pdu, 0)) {
2978 return NT_STATUS_INVALID_PARAMETER;
2981 /* TODO - check auth_type/auth_level match. */
2983 server_response = data_blob_talloc(talloc_tos(), NULL, r->auth_length);
2984 prs_copy_data_out((char *)server_response.data, reply_pdu,
2987 status = ntlmssp_update(state->cli->auth->a_u.ntlmssp_state,
2988 server_response, &client_reply);
2990 if (!NT_STATUS_IS_OK(status)) {
2991 DEBUG(0, ("rpc_finish_auth3_bind: NTLMSSP update using server "
2992 "blob failed: %s.\n", nt_errstr(status)));
2996 prs_init_empty(&state->rpc_out, talloc_tos(), MARSHALL);
2998 status = create_rpc_bind_auth3(state->cli, state->rpc_call_id,
2999 state->cli->auth->auth_type,
3000 state->cli->auth->auth_level,
3001 &client_reply, &state->rpc_out);
3002 data_blob_free(&client_reply);
3004 if (!NT_STATUS_IS_OK(status)) {
3008 subreq = rpc_write_send(state, state->ev, state->cli->transport,
3009 (uint8_t *)prs_data_p(&state->rpc_out),
3010 prs_offset(&state->rpc_out));
3011 if (subreq == NULL) {
3012 return NT_STATUS_NO_MEMORY;
3014 tevent_req_set_callback(subreq, rpc_bind_auth3_write_done, req);
3015 return NT_STATUS_OK;
3018 static void rpc_bind_auth3_write_done(struct tevent_req *subreq)
3020 struct tevent_req *req = tevent_req_callback_data(
3021 subreq, struct tevent_req);
3024 status = rpc_write_recv(subreq);
3025 TALLOC_FREE(subreq);
3026 if (!NT_STATUS_IS_OK(status)) {
3027 tevent_req_nterror(req, status);
3030 tevent_req_done(req);
3033 static NTSTATUS rpc_finish_spnego_ntlmssp_bind_send(struct tevent_req *req,
3034 struct rpc_pipe_bind_state *state,
3035 struct ncacn_packet *r,
3038 DATA_BLOB server_ntlm_response = data_blob_null;
3039 DATA_BLOB client_reply = data_blob_null;
3040 DATA_BLOB tmp_blob = data_blob_null;
3041 struct dcerpc_auth auth_info;
3042 DATA_BLOB auth_blob;
3043 struct tevent_req *subreq;
3046 if ((r->auth_length == 0)
3047 || (r->frag_length < r->auth_length + RPC_HDR_AUTH_LEN)) {
3048 return NT_STATUS_INVALID_PARAMETER;
3051 /* Process the returned NTLMSSP blob first. */
3052 if (!prs_set_offset(
3054 r->frag_length - r->auth_length - RPC_HDR_AUTH_LEN)) {
3055 return NT_STATUS_INVALID_PARAMETER;
3058 auth_blob = data_blob_const(prs_data_p(rpc_in) + prs_offset(rpc_in),
3059 prs_data_size(rpc_in) - prs_offset(rpc_in));
3061 status = dcerpc_pull_dcerpc_auth(state, &auth_blob, &auth_info);
3062 if (!NT_STATUS_IS_OK(status)) {
3063 DEBUG(0, ("Failed to unmarshall dcerpc_auth.\n"));
3068 * The server might give us back two challenges - tmp_blob is for the
3071 if (!spnego_parse_challenge(auth_info.credentials,
3072 &server_ntlm_response, &tmp_blob)) {
3073 data_blob_free(&server_ntlm_response);
3074 data_blob_free(&tmp_blob);
3075 return NT_STATUS_INVALID_PARAMETER;
3078 /* We're finished with the server spnego response and the tmp_blob. */
3079 data_blob_free(&tmp_blob);
3081 status = ntlmssp_update(state->cli->auth->a_u.ntlmssp_state,
3082 server_ntlm_response, &client_reply);
3084 /* Finished with the server_ntlm response */
3085 data_blob_free(&server_ntlm_response);
3087 if (!NT_STATUS_IS_OK(status)) {
3088 DEBUG(0, ("rpc_finish_spnego_ntlmssp_bind: NTLMSSP update "
3089 "using server blob failed.\n"));
3090 data_blob_free(&client_reply);
3094 /* SPNEGO wrap the client reply. */
3095 tmp_blob = spnego_gen_auth(client_reply);
3096 data_blob_free(&client_reply);
3097 client_reply = tmp_blob;
3098 tmp_blob = data_blob_null;
3100 /* Now prepare the alter context pdu. */
3101 prs_init_empty(&state->rpc_out, state, MARSHALL);
3103 status = create_rpc_alter_context(state->rpc_call_id,
3104 &state->cli->abstract_syntax,
3105 &state->cli->transfer_syntax,
3106 state->cli->auth->auth_level,
3109 data_blob_free(&client_reply);
3111 if (!NT_STATUS_IS_OK(status)) {
3115 subreq = rpc_api_pipe_send(state, state->ev, state->cli,
3116 &state->rpc_out, DCERPC_PKT_ALTER_RESP);
3117 if (subreq == NULL) {
3118 return NT_STATUS_NO_MEMORY;
3120 tevent_req_set_callback(subreq, rpc_bind_ntlmssp_api_done, req);
3121 return NT_STATUS_OK;
3124 static void rpc_bind_ntlmssp_api_done(struct tevent_req *subreq)
3126 struct tevent_req *req = tevent_req_callback_data(
3127 subreq, struct tevent_req);
3128 struct rpc_pipe_bind_state *state = tevent_req_data(
3129 req, struct rpc_pipe_bind_state);
3130 DATA_BLOB server_spnego_response = data_blob_null;
3131 DATA_BLOB tmp_blob = data_blob_null;
3132 prs_struct reply_pdu;
3133 struct ncacn_packet_header hdr;
3134 struct rpc_hdr_auth_info hdr_auth;
3137 status = rpc_api_pipe_recv(subreq, talloc_tos(), &reply_pdu);
3138 TALLOC_FREE(subreq);
3139 if (!NT_STATUS_IS_OK(status)) {
3140 tevent_req_nterror(req, status);
3144 status = parse_rpc_header(state->cli, &hdr, &reply_pdu);
3145 if (!NT_STATUS_IS_OK(status)) {
3146 tevent_req_nterror(req, status);
3150 if (!prs_set_offset(
3152 hdr.frag_length - hdr.auth_length - RPC_HDR_AUTH_LEN)) {
3153 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
3157 if (!smb_io_rpc_hdr_auth("hdr_auth", &hdr_auth, &reply_pdu, 0)) {
3158 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
3162 server_spnego_response = data_blob(NULL, hdr.auth_length);
3163 prs_copy_data_out((char *)server_spnego_response.data, &reply_pdu,
3166 /* Check we got a valid auth response. */
3167 if (!spnego_parse_auth_response(server_spnego_response, NT_STATUS_OK,
3168 OID_NTLMSSP, &tmp_blob)) {
3169 data_blob_free(&server_spnego_response);
3170 data_blob_free(&tmp_blob);
3171 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
3175 data_blob_free(&server_spnego_response);
3176 data_blob_free(&tmp_blob);
3178 DEBUG(5,("rpc_finish_spnego_ntlmssp_bind: alter context request to "
3179 "%s.\n", rpccli_pipe_txt(talloc_tos(), state->cli)));
3180 tevent_req_done(req);
3183 NTSTATUS rpc_pipe_bind_recv(struct tevent_req *req)
3185 return tevent_req_simple_recv_ntstatus(req);
3188 NTSTATUS rpc_pipe_bind(struct rpc_pipe_client *cli,
3189 struct cli_pipe_auth_data *auth)
3191 TALLOC_CTX *frame = talloc_stackframe();
3192 struct event_context *ev;
3193 struct tevent_req *req;
3194 NTSTATUS status = NT_STATUS_OK;
3196 ev = event_context_init(frame);
3198 status = NT_STATUS_NO_MEMORY;
3202 req = rpc_pipe_bind_send(frame, ev, cli, auth);
3204 status = NT_STATUS_NO_MEMORY;
3208 if (!tevent_req_poll(req, ev)) {
3209 status = map_nt_error_from_unix(errno);
3213 status = rpc_pipe_bind_recv(req);
3219 #define RPCCLI_DEFAULT_TIMEOUT 10000 /* 10 seconds. */
3221 unsigned int rpccli_set_timeout(struct rpc_pipe_client *rpc_cli,
3222 unsigned int timeout)
3226 if (rpc_cli->transport == NULL) {
3227 return RPCCLI_DEFAULT_TIMEOUT;
3230 if (rpc_cli->transport->set_timeout == NULL) {
3231 return RPCCLI_DEFAULT_TIMEOUT;
3234 old = rpc_cli->transport->set_timeout(rpc_cli->transport->priv, timeout);
3236 return RPCCLI_DEFAULT_TIMEOUT;
3242 bool rpccli_is_connected(struct rpc_pipe_client *rpc_cli)
3244 if (rpc_cli == NULL) {
3248 if (rpc_cli->transport == NULL) {
3252 return rpc_cli->transport->is_connected(rpc_cli->transport->priv);
3255 bool rpccli_get_pwd_hash(struct rpc_pipe_client *rpc_cli, uint8_t nt_hash[16])
3257 struct cli_state *cli;
3259 if ((rpc_cli->auth->auth_type == PIPE_AUTH_TYPE_NTLMSSP)
3260 || (rpc_cli->auth->auth_type == PIPE_AUTH_TYPE_SPNEGO_NTLMSSP)) {
3261 memcpy(nt_hash, rpc_cli->auth->a_u.ntlmssp_state->nt_hash, 16);
3265 cli = rpc_pipe_np_smb_conn(rpc_cli);
3269 E_md4hash(cli->password ? cli->password : "", nt_hash);
3273 NTSTATUS rpccli_anon_bind_data(TALLOC_CTX *mem_ctx,
3274 struct cli_pipe_auth_data **presult)
3276 struct cli_pipe_auth_data *result;
3278 result = talloc(mem_ctx, struct cli_pipe_auth_data);
3279 if (result == NULL) {
3280 return NT_STATUS_NO_MEMORY;
3283 result->auth_type = PIPE_AUTH_TYPE_NONE;
3284 result->auth_level = DCERPC_AUTH_LEVEL_NONE;
3286 result->user_name = talloc_strdup(result, "");
3287 result->domain = talloc_strdup(result, "");
3288 if ((result->user_name == NULL) || (result->domain == NULL)) {
3289 TALLOC_FREE(result);
3290 return NT_STATUS_NO_MEMORY;
3294 return NT_STATUS_OK;
3297 static int cli_auth_ntlmssp_data_destructor(struct cli_pipe_auth_data *auth)
3299 ntlmssp_end(&auth->a_u.ntlmssp_state);
3303 static NTSTATUS rpccli_ntlmssp_bind_data(TALLOC_CTX *mem_ctx,
3304 enum pipe_auth_type auth_type,
3305 enum dcerpc_AuthLevel auth_level,
3307 const char *username,
3308 const char *password,
3309 struct cli_pipe_auth_data **presult)
3311 struct cli_pipe_auth_data *result;
3314 result = talloc(mem_ctx, struct cli_pipe_auth_data);
3315 if (result == NULL) {
3316 return NT_STATUS_NO_MEMORY;
3319 result->auth_type = auth_type;
3320 result->auth_level = auth_level;
3322 result->user_name = talloc_strdup(result, username);
3323 result->domain = talloc_strdup(result, domain);
3324 if ((result->user_name == NULL) || (result->domain == NULL)) {
3325 status = NT_STATUS_NO_MEMORY;
3329 status = ntlmssp_client_start(NULL,
3332 lp_client_ntlmv2_auth(),
3333 &result->a_u.ntlmssp_state);
3334 if (!NT_STATUS_IS_OK(status)) {
3338 talloc_set_destructor(result, cli_auth_ntlmssp_data_destructor);
3340 status = ntlmssp_set_username(result->a_u.ntlmssp_state, username);
3341 if (!NT_STATUS_IS_OK(status)) {
3345 status = ntlmssp_set_domain(result->a_u.ntlmssp_state, domain);
3346 if (!NT_STATUS_IS_OK(status)) {
3350 status = ntlmssp_set_password(result->a_u.ntlmssp_state, password);
3351 if (!NT_STATUS_IS_OK(status)) {
3356 * Turn off sign+seal to allow selected auth level to turn it back on.
3358 result->a_u.ntlmssp_state->neg_flags &=
3359 ~(NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_SEAL);
3361 if (auth_level == DCERPC_AUTH_LEVEL_INTEGRITY) {
3362 result->a_u.ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
3363 } else if (auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
3364 result->a_u.ntlmssp_state->neg_flags
3365 |= NTLMSSP_NEGOTIATE_SEAL | NTLMSSP_NEGOTIATE_SIGN;
3369 return NT_STATUS_OK;
3372 TALLOC_FREE(result);
3376 NTSTATUS rpccli_schannel_bind_data(TALLOC_CTX *mem_ctx, const char *domain,
3377 enum dcerpc_AuthLevel auth_level,
3378 struct netlogon_creds_CredentialState *creds,
3379 struct cli_pipe_auth_data **presult)
3381 struct cli_pipe_auth_data *result;
3383 result = talloc(mem_ctx, struct cli_pipe_auth_data);
3384 if (result == NULL) {
3385 return NT_STATUS_NO_MEMORY;
3388 result->auth_type = PIPE_AUTH_TYPE_SCHANNEL;
3389 result->auth_level = auth_level;
3391 result->user_name = talloc_strdup(result, "");
3392 result->domain = talloc_strdup(result, domain);
3393 if ((result->user_name == NULL) || (result->domain == NULL)) {
3397 result->a_u.schannel_auth = talloc(result, struct schannel_state);
3398 if (result->a_u.schannel_auth == NULL) {
3402 result->a_u.schannel_auth->state = SCHANNEL_STATE_START;
3403 result->a_u.schannel_auth->seq_num = 0;
3404 result->a_u.schannel_auth->initiator = true;
3405 result->a_u.schannel_auth->creds = creds;
3408 return NT_STATUS_OK;
3411 TALLOC_FREE(result);
3412 return NT_STATUS_NO_MEMORY;
3416 static int cli_auth_kerberos_data_destructor(struct kerberos_auth_struct *auth)
3418 data_blob_free(&auth->session_key);
3423 static NTSTATUS rpccli_kerberos_bind_data(TALLOC_CTX *mem_ctx,
3424 enum dcerpc_AuthLevel auth_level,
3425 const char *service_princ,
3426 const char *username,
3427 const char *password,
3428 struct cli_pipe_auth_data **presult)
3431 struct cli_pipe_auth_data *result;
3433 if ((username != NULL) && (password != NULL)) {
3434 int ret = kerberos_kinit_password(username, password, 0, NULL);
3436 return NT_STATUS_ACCESS_DENIED;
3440 result = talloc(mem_ctx, struct cli_pipe_auth_data);
3441 if (result == NULL) {
3442 return NT_STATUS_NO_MEMORY;
3445 result->auth_type = PIPE_AUTH_TYPE_KRB5;
3446 result->auth_level = auth_level;
3449 * Username / domain need fixing!
3451 result->user_name = talloc_strdup(result, "");
3452 result->domain = talloc_strdup(result, "");
3453 if ((result->user_name == NULL) || (result->domain == NULL)) {
3457 result->a_u.kerberos_auth = TALLOC_ZERO_P(
3458 result, struct kerberos_auth_struct);
3459 if (result->a_u.kerberos_auth == NULL) {
3462 talloc_set_destructor(result->a_u.kerberos_auth,
3463 cli_auth_kerberos_data_destructor);
3465 result->a_u.kerberos_auth->service_principal = talloc_strdup(
3466 result, service_princ);
3467 if (result->a_u.kerberos_auth->service_principal == NULL) {
3472 return NT_STATUS_OK;
3475 TALLOC_FREE(result);
3476 return NT_STATUS_NO_MEMORY;
3478 return NT_STATUS_NOT_SUPPORTED;
3483 * Create an rpc pipe client struct, connecting to a tcp port.
3485 static NTSTATUS rpc_pipe_open_tcp_port(TALLOC_CTX *mem_ctx, const char *host,
3487 const struct ndr_syntax_id *abstract_syntax,
3488 struct rpc_pipe_client **presult)
3490 struct rpc_pipe_client *result;
3491 struct sockaddr_storage addr;
3495 result = TALLOC_ZERO_P(mem_ctx, struct rpc_pipe_client);
3496 if (result == NULL) {
3497 return NT_STATUS_NO_MEMORY;
3500 result->abstract_syntax = *abstract_syntax;
3501 result->transfer_syntax = ndr_transfer_syntax;
3502 result->dispatch = cli_do_rpc_ndr;
3503 result->dispatch_send = cli_do_rpc_ndr_send;
3504 result->dispatch_recv = cli_do_rpc_ndr_recv;
3506 result->desthost = talloc_strdup(result, host);
3507 result->srv_name_slash = talloc_asprintf_strupper_m(
3508 result, "\\\\%s", result->desthost);
3509 if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
3510 status = NT_STATUS_NO_MEMORY;
3514 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
3515 result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
3517 if (!resolve_name(host, &addr, 0, false)) {
3518 status = NT_STATUS_NOT_FOUND;
3522 status = open_socket_out(&addr, port, 60, &fd);
3523 if (!NT_STATUS_IS_OK(status)) {
3526 set_socket_options(fd, lp_socket_options());
3528 status = rpc_transport_sock_init(result, fd, &result->transport);
3529 if (!NT_STATUS_IS_OK(status)) {
3534 result->transport->transport = NCACN_IP_TCP;
3537 return NT_STATUS_OK;
3540 TALLOC_FREE(result);
3545 * Determine the tcp port on which a dcerpc interface is listening
3546 * for the ncacn_ip_tcp transport via the endpoint mapper of the
3549 static NTSTATUS rpc_pipe_get_tcp_port(const char *host,
3550 const struct ndr_syntax_id *abstract_syntax,
3554 struct rpc_pipe_client *epm_pipe = NULL;
3555 struct cli_pipe_auth_data *auth = NULL;
3556 struct dcerpc_binding *map_binding = NULL;
3557 struct dcerpc_binding *res_binding = NULL;
3558 struct epm_twr_t *map_tower = NULL;
3559 struct epm_twr_t *res_towers = NULL;
3560 struct policy_handle *entry_handle = NULL;
3561 uint32_t num_towers = 0;
3562 uint32_t max_towers = 1;
3563 struct epm_twr_p_t towers;
3564 TALLOC_CTX *tmp_ctx = talloc_stackframe();
3566 if (pport == NULL) {
3567 status = NT_STATUS_INVALID_PARAMETER;
3571 /* open the connection to the endpoint mapper */
3572 status = rpc_pipe_open_tcp_port(tmp_ctx, host, 135,
3573 &ndr_table_epmapper.syntax_id,
3576 if (!NT_STATUS_IS_OK(status)) {
3580 status = rpccli_anon_bind_data(tmp_ctx, &auth);
3581 if (!NT_STATUS_IS_OK(status)) {
3585 status = rpc_pipe_bind(epm_pipe, auth);
3586 if (!NT_STATUS_IS_OK(status)) {
3590 /* create tower for asking the epmapper */
3592 map_binding = TALLOC_ZERO_P(tmp_ctx, struct dcerpc_binding);
3593 if (map_binding == NULL) {
3594 status = NT_STATUS_NO_MEMORY;
3598 map_binding->transport = NCACN_IP_TCP;
3599 map_binding->object = *abstract_syntax;
3600 map_binding->host = host; /* needed? */
3601 map_binding->endpoint = "0"; /* correct? needed? */
3603 map_tower = TALLOC_ZERO_P(tmp_ctx, struct epm_twr_t);
3604 if (map_tower == NULL) {
3605 status = NT_STATUS_NO_MEMORY;
3609 status = dcerpc_binding_build_tower(tmp_ctx, map_binding,
3610 &(map_tower->tower));
3611 if (!NT_STATUS_IS_OK(status)) {
3615 /* allocate further parameters for the epm_Map call */
3617 res_towers = TALLOC_ARRAY(tmp_ctx, struct epm_twr_t, max_towers);
3618 if (res_towers == NULL) {
3619 status = NT_STATUS_NO_MEMORY;
3622 towers.twr = res_towers;
3624 entry_handle = TALLOC_ZERO_P(tmp_ctx, struct policy_handle);
3625 if (entry_handle == NULL) {
3626 status = NT_STATUS_NO_MEMORY;
3630 /* ask the endpoint mapper for the port */
3632 status = rpccli_epm_Map(epm_pipe,
3634 CONST_DISCARD(struct GUID *,
3635 &(abstract_syntax->uuid)),
3642 if (!NT_STATUS_IS_OK(status)) {
3646 if (num_towers != 1) {
3647 status = NT_STATUS_UNSUCCESSFUL;
3651 /* extract the port from the answer */
3653 status = dcerpc_binding_from_tower(tmp_ctx,
3654 &(towers.twr->tower),
3656 if (!NT_STATUS_IS_OK(status)) {
3660 /* are further checks here necessary? */
3661 if (res_binding->transport != NCACN_IP_TCP) {
3662 status = NT_STATUS_UNSUCCESSFUL;
3666 *pport = (uint16_t)atoi(res_binding->endpoint);
3669 TALLOC_FREE(tmp_ctx);
3674 * Create a rpc pipe client struct, connecting to a host via tcp.
3675 * The port is determined by asking the endpoint mapper on the given
3678 NTSTATUS rpc_pipe_open_tcp(TALLOC_CTX *mem_ctx, const char *host,
3679 const struct ndr_syntax_id *abstract_syntax,
3680 struct rpc_pipe_client **presult)
3685 status = rpc_pipe_get_tcp_port(host, abstract_syntax, &port);
3686 if (!NT_STATUS_IS_OK(status)) {
3690 return rpc_pipe_open_tcp_port(mem_ctx, host, port,
3691 abstract_syntax, presult);
3694 /********************************************************************
3695 Create a rpc pipe client struct, connecting to a unix domain socket
3696 ********************************************************************/
3697 NTSTATUS rpc_pipe_open_ncalrpc(TALLOC_CTX *mem_ctx, const char *socket_path,
3698 const struct ndr_syntax_id *abstract_syntax,
3699 struct rpc_pipe_client **presult)
3701 struct rpc_pipe_client *result;
3702 struct sockaddr_un addr;
3706 result = talloc_zero(mem_ctx, struct rpc_pipe_client);
3707 if (result == NULL) {
3708 return NT_STATUS_NO_MEMORY;
3711 result->abstract_syntax = *abstract_syntax;
3712 result->transfer_syntax = ndr_transfer_syntax;
3713 result->dispatch = cli_do_rpc_ndr;
3714 result->dispatch_send = cli_do_rpc_ndr_send;
3715 result->dispatch_recv = cli_do_rpc_ndr_recv;
3717 result->desthost = get_myname(result);
3718 result->srv_name_slash = talloc_asprintf_strupper_m(
3719 result, "\\\\%s", result->desthost);
3720 if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
3721 status = NT_STATUS_NO_MEMORY;
3725 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
3726 result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
3728 fd = socket(AF_UNIX, SOCK_STREAM, 0);
3730 status = map_nt_error_from_unix(errno);
3735 addr.sun_family = AF_UNIX;
3736 strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
3738 if (sys_connect(fd, (struct sockaddr *)(void *)&addr) == -1) {
3739 DEBUG(0, ("connect(%s) failed: %s\n", socket_path,
3742 return map_nt_error_from_unix(errno);
3745 status = rpc_transport_sock_init(result, fd, &result->transport);
3746 if (!NT_STATUS_IS_OK(status)) {
3751 result->transport->transport = NCALRPC;
3754 return NT_STATUS_OK;
3757 TALLOC_FREE(result);
3761 struct rpc_pipe_client_np_ref {
3762 struct cli_state *cli;
3763 struct rpc_pipe_client *pipe;
3766 static int rpc_pipe_client_np_ref_destructor(struct rpc_pipe_client_np_ref *np_ref)
3768 DLIST_REMOVE(np_ref->cli->pipe_list, np_ref->pipe);
3772 /****************************************************************************
3773 Open a named pipe over SMB to a remote server.
3775 * CAVEAT CALLER OF THIS FUNCTION:
3776 * The returned rpc_pipe_client saves a copy of the cli_state cli pointer,
3777 * so be sure that this function is called AFTER any structure (vs pointer)
3778 * assignment of the cli. In particular, libsmbclient does structure
3779 * assignments of cli, which invalidates the data in the returned
3780 * rpc_pipe_client if this function is called before the structure assignment
3783 ****************************************************************************/
3785 static NTSTATUS rpc_pipe_open_np(struct cli_state *cli,
3786 const struct ndr_syntax_id *abstract_syntax,
3787 struct rpc_pipe_client **presult)
3789 struct rpc_pipe_client *result;
3791 struct rpc_pipe_client_np_ref *np_ref;
3793 /* sanity check to protect against crashes */
3796 return NT_STATUS_INVALID_HANDLE;
3799 result = TALLOC_ZERO_P(NULL, struct rpc_pipe_client);
3800 if (result == NULL) {
3801 return NT_STATUS_NO_MEMORY;
3804 result->abstract_syntax = *abstract_syntax;
3805 result->transfer_syntax = ndr_transfer_syntax;
3806 result->dispatch = cli_do_rpc_ndr;
3807 result->dispatch_send = cli_do_rpc_ndr_send;
3808 result->dispatch_recv = cli_do_rpc_ndr_recv;
3809 result->desthost = talloc_strdup(result, cli->desthost);
3810 result->srv_name_slash = talloc_asprintf_strupper_m(
3811 result, "\\\\%s", result->desthost);
3813 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
3814 result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
3816 if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
3817 TALLOC_FREE(result);
3818 return NT_STATUS_NO_MEMORY;
3821 status = rpc_transport_np_init(result, cli, abstract_syntax,
3822 &result->transport);
3823 if (!NT_STATUS_IS_OK(status)) {
3824 TALLOC_FREE(result);
3828 result->transport->transport = NCACN_NP;
3830 np_ref = talloc(result->transport, struct rpc_pipe_client_np_ref);
3831 if (np_ref == NULL) {
3832 TALLOC_FREE(result);
3833 return NT_STATUS_NO_MEMORY;
3836 np_ref->pipe = result;
3838 DLIST_ADD(np_ref->cli->pipe_list, np_ref->pipe);
3839 talloc_set_destructor(np_ref, rpc_pipe_client_np_ref_destructor);
3842 return NT_STATUS_OK;
3845 NTSTATUS rpc_pipe_open_local(TALLOC_CTX *mem_ctx,
3846 struct rpc_cli_smbd_conn *conn,
3847 const struct ndr_syntax_id *syntax,
3848 struct rpc_pipe_client **presult)
3850 struct rpc_pipe_client *result;
3851 struct cli_pipe_auth_data *auth;
3854 result = talloc(mem_ctx, struct rpc_pipe_client);
3855 if (result == NULL) {
3856 return NT_STATUS_NO_MEMORY;
3858 result->abstract_syntax = *syntax;
3859 result->transfer_syntax = ndr_transfer_syntax;
3860 result->dispatch = cli_do_rpc_ndr;
3861 result->dispatch_send = cli_do_rpc_ndr_send;
3862 result->dispatch_recv = cli_do_rpc_ndr_recv;
3863 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
3864 result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
3866 result->desthost = talloc_strdup(result, global_myname());
3867 result->srv_name_slash = talloc_asprintf_strupper_m(
3868 result, "\\\\%s", global_myname());
3869 if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
3870 TALLOC_FREE(result);
3871 return NT_STATUS_NO_MEMORY;
3874 status = rpc_transport_smbd_init(result, conn, syntax,
3875 &result->transport);
3876 if (!NT_STATUS_IS_OK(status)) {
3877 DEBUG(1, ("rpc_transport_smbd_init failed: %s\n",
3878 nt_errstr(status)));
3879 TALLOC_FREE(result);
3883 status = rpccli_anon_bind_data(result, &auth);
3884 if (!NT_STATUS_IS_OK(status)) {
3885 DEBUG(1, ("rpccli_anon_bind_data failed: %s\n",
3886 nt_errstr(status)));
3887 TALLOC_FREE(result);
3891 status = rpc_pipe_bind(result, auth);
3892 if (!NT_STATUS_IS_OK(status)) {
3893 DEBUG(1, ("rpc_pipe_bind failed: %s\n", nt_errstr(status)));
3894 TALLOC_FREE(result);
3898 result->transport->transport = NCACN_INTERNAL;
3901 return NT_STATUS_OK;
3904 /****************************************************************************
3905 Open a pipe to a remote server.
3906 ****************************************************************************/
3908 static NTSTATUS cli_rpc_pipe_open(struct cli_state *cli,
3909 enum dcerpc_transport_t transport,
3910 const struct ndr_syntax_id *interface,
3911 struct rpc_pipe_client **presult)
3913 switch (transport) {
3915 return rpc_pipe_open_tcp(NULL, cli->desthost, interface,
3918 return rpc_pipe_open_np(cli, interface, presult);
3920 return NT_STATUS_NOT_IMPLEMENTED;
3924 /****************************************************************************
3925 Open a named pipe to an SMB server and bind anonymously.
3926 ****************************************************************************/
3928 NTSTATUS cli_rpc_pipe_open_noauth_transport(struct cli_state *cli,
3929 enum dcerpc_transport_t transport,
3930 const struct ndr_syntax_id *interface,
3931 struct rpc_pipe_client **presult)
3933 struct rpc_pipe_client *result;
3934 struct cli_pipe_auth_data *auth;
3937 status = cli_rpc_pipe_open(cli, transport, interface, &result);
3938 if (!NT_STATUS_IS_OK(status)) {
3942 status = rpccli_anon_bind_data(result, &auth);
3943 if (!NT_STATUS_IS_OK(status)) {
3944 DEBUG(0, ("rpccli_anon_bind_data returned %s\n",
3945 nt_errstr(status)));
3946 TALLOC_FREE(result);
3951 * This is a bit of an abstraction violation due to the fact that an
3952 * anonymous bind on an authenticated SMB inherits the user/domain
3953 * from the enclosing SMB creds
3956 TALLOC_FREE(auth->user_name);
3957 TALLOC_FREE(auth->domain);
3959 auth->user_name = talloc_strdup(auth, cli->user_name);
3960 auth->domain = talloc_strdup(auth, cli->domain);
3961 auth->user_session_key = data_blob_talloc(auth,
3962 cli->user_session_key.data,
3963 cli->user_session_key.length);
3965 if ((auth->user_name == NULL) || (auth->domain == NULL)) {
3966 TALLOC_FREE(result);
3967 return NT_STATUS_NO_MEMORY;
3970 status = rpc_pipe_bind(result, auth);
3971 if (!NT_STATUS_IS_OK(status)) {
3973 if (ndr_syntax_id_equal(interface,
3974 &ndr_table_dssetup.syntax_id)) {
3975 /* non AD domains just don't have this pipe, avoid
3976 * level 0 statement in that case - gd */
3979 DEBUG(lvl, ("cli_rpc_pipe_open_noauth: rpc_pipe_bind for pipe "
3980 "%s failed with error %s\n",
3981 get_pipe_name_from_syntax(talloc_tos(), interface),
3982 nt_errstr(status) ));
3983 TALLOC_FREE(result);
3987 DEBUG(10,("cli_rpc_pipe_open_noauth: opened pipe %s to machine "
3988 "%s and bound anonymously.\n",
3989 get_pipe_name_from_syntax(talloc_tos(), interface),
3993 return NT_STATUS_OK;
3996 /****************************************************************************
3997 ****************************************************************************/
3999 NTSTATUS cli_rpc_pipe_open_noauth(struct cli_state *cli,
4000 const struct ndr_syntax_id *interface,
4001 struct rpc_pipe_client **presult)
4003 return cli_rpc_pipe_open_noauth_transport(cli, NCACN_NP,
4004 interface, presult);
4007 /****************************************************************************
4008 Open a named pipe to an SMB server and bind using NTLMSSP or SPNEGO NTLMSSP
4009 ****************************************************************************/
4011 static NTSTATUS cli_rpc_pipe_open_ntlmssp_internal(struct cli_state *cli,
4012 const struct ndr_syntax_id *interface,
4013 enum dcerpc_transport_t transport,
4014 enum pipe_auth_type auth_type,
4015 enum dcerpc_AuthLevel auth_level,
4017 const char *username,
4018 const char *password,
4019 struct rpc_pipe_client **presult)
4021 struct rpc_pipe_client *result;
4022 struct cli_pipe_auth_data *auth;
4025 status = cli_rpc_pipe_open(cli, transport, interface, &result);
4026 if (!NT_STATUS_IS_OK(status)) {
4030 status = rpccli_ntlmssp_bind_data(
4031 result, auth_type, auth_level, domain, username,
4033 if (!NT_STATUS_IS_OK(status)) {
4034 DEBUG(0, ("rpccli_ntlmssp_bind_data returned %s\n",
4035 nt_errstr(status)));
4039 status = rpc_pipe_bind(result, auth);
4040 if (!NT_STATUS_IS_OK(status)) {
4041 DEBUG(0, ("cli_rpc_pipe_open_ntlmssp_internal: cli_rpc_pipe_bind failed with error %s\n",
4042 nt_errstr(status) ));
4046 DEBUG(10,("cli_rpc_pipe_open_ntlmssp_internal: opened pipe %s to "
4047 "machine %s and bound NTLMSSP as user %s\\%s.\n",
4048 get_pipe_name_from_syntax(talloc_tos(), interface),
4049 cli->desthost, domain, username ));
4052 return NT_STATUS_OK;
4056 TALLOC_FREE(result);
4060 /****************************************************************************
4062 Open a named pipe to an SMB server and bind using NTLMSSP (bind type 10)
4063 ****************************************************************************/
4065 NTSTATUS cli_rpc_pipe_open_ntlmssp(struct cli_state *cli,
4066 const struct ndr_syntax_id *interface,
4067 enum dcerpc_transport_t transport,
4068 enum dcerpc_AuthLevel auth_level,
4070 const char *username,
4071 const char *password,
4072 struct rpc_pipe_client **presult)
4074 return cli_rpc_pipe_open_ntlmssp_internal(cli,
4077 PIPE_AUTH_TYPE_NTLMSSP,
4085 /****************************************************************************
4087 Open a named pipe to an SMB server and bind using spnego NTLMSSP (bind type 9)
4088 ****************************************************************************/
4090 NTSTATUS cli_rpc_pipe_open_spnego_ntlmssp(struct cli_state *cli,
4091 const struct ndr_syntax_id *interface,
4092 enum dcerpc_transport_t transport,
4093 enum dcerpc_AuthLevel auth_level,
4095 const char *username,
4096 const char *password,
4097 struct rpc_pipe_client **presult)
4099 return cli_rpc_pipe_open_ntlmssp_internal(cli,
4102 PIPE_AUTH_TYPE_SPNEGO_NTLMSSP,
4110 /****************************************************************************
4111 Get a the schannel session key out of an already opened netlogon pipe.
4112 ****************************************************************************/
4113 static NTSTATUS get_schannel_session_key_common(struct rpc_pipe_client *netlogon_pipe,
4114 struct cli_state *cli,
4118 enum netr_SchannelType sec_chan_type = 0;
4119 unsigned char machine_pwd[16];
4120 const char *machine_account;
4123 /* Get the machine account credentials from secrets.tdb. */
4124 if (!get_trust_pw_hash(domain, machine_pwd, &machine_account,
4127 DEBUG(0, ("get_schannel_session_key: could not fetch "
4128 "trust account password for domain '%s'\n",
4130 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
4133 status = rpccli_netlogon_setup_creds(netlogon_pipe,
4134 cli->desthost, /* server name */
4135 domain, /* domain */
4136 global_myname(), /* client name */
4137 machine_account, /* machine account name */
4142 if (!NT_STATUS_IS_OK(status)) {
4143 DEBUG(3, ("get_schannel_session_key_common: "
4144 "rpccli_netlogon_setup_creds failed with result %s "
4145 "to server %s, domain %s, machine account %s.\n",
4146 nt_errstr(status), cli->desthost, domain,
4151 if (((*pneg_flags) & NETLOGON_NEG_SCHANNEL) == 0) {
4152 DEBUG(3, ("get_schannel_session_key: Server %s did not offer schannel\n",
4154 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4157 return NT_STATUS_OK;;
4160 /****************************************************************************
4161 Open a netlogon pipe and get the schannel session key.
4162 Now exposed to external callers.
4163 ****************************************************************************/
4166 NTSTATUS get_schannel_session_key(struct cli_state *cli,
4169 struct rpc_pipe_client **presult)
4171 struct rpc_pipe_client *netlogon_pipe = NULL;
4174 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
4176 if (!NT_STATUS_IS_OK(status)) {
4180 status = get_schannel_session_key_common(netlogon_pipe, cli, domain,
4182 if (!NT_STATUS_IS_OK(status)) {
4183 TALLOC_FREE(netlogon_pipe);
4187 *presult = netlogon_pipe;
4188 return NT_STATUS_OK;
4191 /****************************************************************************
4193 Open a named pipe to an SMB server and bind using schannel (bind type 68)
4194 using session_key. sign and seal.
4196 The *pdc will be stolen onto this new pipe
4197 ****************************************************************************/
4199 NTSTATUS cli_rpc_pipe_open_schannel_with_key(struct cli_state *cli,
4200 const struct ndr_syntax_id *interface,
4201 enum dcerpc_transport_t transport,
4202 enum dcerpc_AuthLevel auth_level,
4204 struct netlogon_creds_CredentialState **pdc,
4205 struct rpc_pipe_client **presult)
4207 struct rpc_pipe_client *result;
4208 struct cli_pipe_auth_data *auth;
4211 status = cli_rpc_pipe_open(cli, transport, interface, &result);
4212 if (!NT_STATUS_IS_OK(status)) {
4216 status = rpccli_schannel_bind_data(result, domain, auth_level,
4218 if (!NT_STATUS_IS_OK(status)) {
4219 DEBUG(0, ("rpccli_schannel_bind_data returned %s\n",
4220 nt_errstr(status)));
4221 TALLOC_FREE(result);
4225 status = rpc_pipe_bind(result, auth);
4226 if (!NT_STATUS_IS_OK(status)) {
4227 DEBUG(0, ("cli_rpc_pipe_open_schannel_with_key: "
4228 "cli_rpc_pipe_bind failed with error %s\n",
4229 nt_errstr(status) ));
4230 TALLOC_FREE(result);
4235 * The credentials on a new netlogon pipe are the ones we are passed
4236 * in - reference them in
4238 result->dc = talloc_move(result, pdc);
4240 DEBUG(10,("cli_rpc_pipe_open_schannel_with_key: opened pipe %s to machine %s "
4241 "for domain %s and bound using schannel.\n",
4242 get_pipe_name_from_syntax(talloc_tos(), interface),
4243 cli->desthost, domain ));
4246 return NT_STATUS_OK;
4249 /****************************************************************************
4250 Open a named pipe to an SMB server and bind using schannel (bind type 68).
4251 Fetch the session key ourselves using a temporary netlogon pipe. This
4252 version uses an ntlmssp auth bound netlogon pipe to get the key.
4253 ****************************************************************************/
4255 static NTSTATUS get_schannel_session_key_auth_ntlmssp(struct cli_state *cli,
4257 const char *username,
4258 const char *password,
4260 struct rpc_pipe_client **presult)
4262 struct rpc_pipe_client *netlogon_pipe = NULL;
4265 status = cli_rpc_pipe_open_spnego_ntlmssp(
4266 cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
4267 DCERPC_AUTH_LEVEL_PRIVACY,
4268 domain, username, password, &netlogon_pipe);
4269 if (!NT_STATUS_IS_OK(status)) {
4273 status = get_schannel_session_key_common(netlogon_pipe, cli, domain,
4275 if (!NT_STATUS_IS_OK(status)) {
4276 TALLOC_FREE(netlogon_pipe);
4280 *presult = netlogon_pipe;
4281 return NT_STATUS_OK;
4284 /****************************************************************************
4285 Open a named pipe to an SMB server and bind using schannel (bind type 68).
4286 Fetch the session key ourselves using a temporary netlogon pipe. This version
4287 uses an ntlmssp bind to get the session key.
4288 ****************************************************************************/
4290 NTSTATUS cli_rpc_pipe_open_ntlmssp_auth_schannel(struct cli_state *cli,
4291 const struct ndr_syntax_id *interface,
4292 enum dcerpc_transport_t transport,
4293 enum dcerpc_AuthLevel auth_level,
4295 const char *username,
4296 const char *password,
4297 struct rpc_pipe_client **presult)
4299 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
4300 struct rpc_pipe_client *netlogon_pipe = NULL;
4301 struct rpc_pipe_client *result = NULL;
4304 status = get_schannel_session_key_auth_ntlmssp(
4305 cli, domain, username, password, &neg_flags, &netlogon_pipe);
4306 if (!NT_STATUS_IS_OK(status)) {
4307 DEBUG(0,("cli_rpc_pipe_open_ntlmssp_auth_schannel: failed to get schannel session "
4308 "key from server %s for domain %s.\n",
4309 cli->desthost, domain ));
4313 status = cli_rpc_pipe_open_schannel_with_key(
4314 cli, interface, transport, auth_level, domain, &netlogon_pipe->dc,
4317 /* Now we've bound using the session key we can close the netlog pipe. */
4318 TALLOC_FREE(netlogon_pipe);
4320 if (NT_STATUS_IS_OK(status)) {
4326 /****************************************************************************
4327 Open a named pipe to an SMB server and bind using schannel (bind type 68).
4328 Fetch the session key ourselves using a temporary netlogon pipe.
4329 ****************************************************************************/
4331 NTSTATUS cli_rpc_pipe_open_schannel(struct cli_state *cli,
4332 const struct ndr_syntax_id *interface,
4333 enum dcerpc_transport_t transport,
4334 enum dcerpc_AuthLevel auth_level,
4336 struct rpc_pipe_client **presult)
4338 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
4339 struct rpc_pipe_client *netlogon_pipe = NULL;
4340 struct rpc_pipe_client *result = NULL;
4343 status = get_schannel_session_key(cli, domain, &neg_flags,
4345 if (!NT_STATUS_IS_OK(status)) {
4346 DEBUG(0,("cli_rpc_pipe_open_schannel: failed to get schannel session "
4347 "key from server %s for domain %s.\n",
4348 cli->desthost, domain ));
4352 status = cli_rpc_pipe_open_schannel_with_key(
4353 cli, interface, transport, auth_level, domain, &netlogon_pipe->dc,
4356 /* Now we've bound using the session key we can close the netlog pipe. */
4357 TALLOC_FREE(netlogon_pipe);
4359 if (NT_STATUS_IS_OK(status)) {
4366 /****************************************************************************
4367 Open a named pipe to an SMB server and bind using krb5 (bind type 16).
4368 The idea is this can be called with service_princ, username and password all
4369 NULL so long as the caller has a TGT.
4370 ****************************************************************************/
4372 NTSTATUS cli_rpc_pipe_open_krb5(struct cli_state *cli,
4373 const struct ndr_syntax_id *interface,
4374 enum dcerpc_AuthLevel auth_level,
4375 const char *service_princ,
4376 const char *username,
4377 const char *password,
4378 struct rpc_pipe_client **presult)
4381 struct rpc_pipe_client *result;
4382 struct cli_pipe_auth_data *auth;
4385 status = cli_rpc_pipe_open(cli, NCACN_NP, interface, &result);
4386 if (!NT_STATUS_IS_OK(status)) {
4390 status = rpccli_kerberos_bind_data(result, auth_level, service_princ,
4391 username, password, &auth);
4392 if (!NT_STATUS_IS_OK(status)) {
4393 DEBUG(0, ("rpccli_kerberos_bind_data returned %s\n",
4394 nt_errstr(status)));
4395 TALLOC_FREE(result);
4399 status = rpc_pipe_bind(result, auth);
4400 if (!NT_STATUS_IS_OK(status)) {
4401 DEBUG(0, ("cli_rpc_pipe_open_krb5: cli_rpc_pipe_bind failed "
4402 "with error %s\n", nt_errstr(status)));
4403 TALLOC_FREE(result);
4408 return NT_STATUS_OK;
4410 DEBUG(0,("cli_rpc_pipe_open_krb5: kerberos not found at compile time.\n"));
4411 return NT_STATUS_NOT_IMPLEMENTED;
4415 NTSTATUS cli_get_session_key(TALLOC_CTX *mem_ctx,
4416 struct rpc_pipe_client *cli,
4417 DATA_BLOB *session_key)
4419 if (!session_key || !cli) {
4420 return NT_STATUS_INVALID_PARAMETER;
4424 return NT_STATUS_INVALID_PARAMETER;
4427 switch (cli->auth->auth_type) {
4428 case PIPE_AUTH_TYPE_SCHANNEL:
4429 *session_key = data_blob_talloc(mem_ctx,
4430 cli->auth->a_u.schannel_auth->creds->session_key, 16);
4432 case PIPE_AUTH_TYPE_NTLMSSP:
4433 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
4434 *session_key = data_blob_talloc(mem_ctx,
4435 cli->auth->a_u.ntlmssp_state->session_key.data,
4436 cli->auth->a_u.ntlmssp_state->session_key.length);
4438 case PIPE_AUTH_TYPE_KRB5:
4439 case PIPE_AUTH_TYPE_SPNEGO_KRB5:
4440 *session_key = data_blob_talloc(mem_ctx,
4441 cli->auth->a_u.kerberos_auth->session_key.data,
4442 cli->auth->a_u.kerberos_auth->session_key.length);
4444 case PIPE_AUTH_TYPE_NONE:
4445 *session_key = data_blob_talloc(mem_ctx,
4446 cli->auth->user_session_key.data,
4447 cli->auth->user_session_key.length);
4450 return NT_STATUS_NO_USER_SESSION_KEY;
4453 return NT_STATUS_OK;