2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Almost completely rewritten by (C) Jeremy Allison 2005 - 2010
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/>.
20 /* this module apparently provides an implementation of DCE/RPC over a
21 * named pipe (IPC$ connection using SMBtrans). details of DCE/RPC
22 * documentation are available (in on-line form) from the X-Open group.
24 * this module should provide a level of abstraction between SMB
25 * and DCE/RPC, while minimising the amount of mallocs, unnecessary
26 * data copies, and network traffic.
31 #include "srv_pipe_internal.h"
32 #include "../librpc/gen_ndr/ndr_schannel.h"
33 #include "../libcli/auth/schannel.h"
34 #include "../libcli/auth/spnego.h"
35 #include "../libcli/auth/ntlmssp.h"
38 #define DBGC_CLASS DBGC_RPC_SRV
40 static void free_pipe_ntlmssp_auth_data(struct pipe_auth_data *auth)
42 struct auth_ntlmssp_state *a = auth->a_u.auth_ntlmssp_state;
47 auth->a_u.auth_ntlmssp_state = NULL;
50 static DATA_BLOB generic_session_key(void)
52 return data_blob("SystemLibraryDTC", 16);
55 /*******************************************************************
56 Generate the next PDU to be returned from the data in p->rdata.
58 ********************************************************************/
60 static bool create_next_pdu_ntlmssp(pipes_struct *p)
64 RPC_HDR_RESP hdr_resp;
65 uint32 ss_padding_len = 0;
66 uint32 data_space_available;
71 RPC_HDR_AUTH auth_info;
72 uint8 auth_type, auth_level;
73 struct auth_ntlmssp_state *a = p->auth.a_u.auth_ntlmssp_state;
77 * If we're in the fault state, keep returning fault PDU's until
78 * the pipe gets closed. JRA.
82 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
86 memset((char *)&hdr_resp, '\0', sizeof(hdr_resp));
88 /* Set up rpc header flags. */
89 if (p->out_data.data_sent_length == 0) {
90 hdr_flags = DCERPC_PFC_FLAG_FIRST;
96 * Work out how much we can fit in a single PDU.
99 data_len_left = prs_offset(&p->out_data.rdata) - p->out_data.data_sent_length;
102 * Ensure there really is data left to send.
106 DEBUG(0,("create_next_pdu_ntlmssp: no data left to send !\n"));
110 /* Space available - not including padding. */
111 data_space_available = RPC_MAX_PDU_FRAG_LEN - RPC_HEADER_LEN -
112 RPC_HDR_RESP_LEN - RPC_HDR_AUTH_LEN - NTLMSSP_SIG_SIZE;
115 * The amount we send is the minimum of the available
116 * space and the amount left to send.
119 data_len = MIN(data_len_left, data_space_available);
121 /* Work out any padding alignment requirements. */
122 if ((RPC_HEADER_LEN + RPC_HDR_RESP_LEN + data_len) % SERVER_NDR_PADDING_SIZE) {
123 ss_padding_len = SERVER_NDR_PADDING_SIZE -
124 ((RPC_HEADER_LEN + RPC_HDR_RESP_LEN + data_len) % SERVER_NDR_PADDING_SIZE);
125 DEBUG(10,("create_next_pdu_ntlmssp: adding sign/seal padding of %u\n",
127 /* If we're over filling the packet, we need to make space
128 * for the padding at the end of the data. */
129 if (data_len + ss_padding_len > data_space_available) {
130 data_len -= SERVER_NDR_PADDING_SIZE;
135 * Set up the alloc hint. This should be the data left to
139 hdr_resp.alloc_hint = data_len_left;
142 * Work out if this PDU will be the last.
144 if (p->out_data.data_sent_length + data_len >=
145 prs_offset(&p->out_data.rdata)) {
146 hdr_flags |= DCERPC_PFC_FLAG_LAST;
150 * Init the parse struct to point at the outgoing
153 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
155 status = dcerpc_push_ncacn_packet_header(
156 prs_get_mem_context(&p->out_data.frag),
159 RPC_HEADER_LEN + RPC_HDR_RESP_LEN +
160 data_len + ss_padding_len +
161 RPC_HDR_AUTH_LEN + NTLMSSP_SIG_SIZE,
165 if (!NT_STATUS_IS_OK(status)) {
166 DEBUG(0, ("Failed to marshall RPC Header.\n"));
167 prs_mem_free(&p->out_data.frag);
171 /* Store the header in the data stream. */
172 if (!prs_copy_data_in(&p->out_data.frag,
173 (char *)hdr.data, hdr.length)) {
174 DEBUG(0, ("Out of memory.\n"));
175 prs_mem_free(&p->out_data.frag);
179 if(!smb_io_rpc_hdr_resp("resp", &hdr_resp, &p->out_data.frag, 0)) {
180 DEBUG(0,("create_next_pdu_ntlmssp: failed to marshall RPC_HDR_RESP.\n"));
181 prs_mem_free(&p->out_data.frag);
185 /* Copy the data into the PDU. */
187 if(!prs_append_some_prs_data(&p->out_data.frag, &p->out_data.rdata,
188 p->out_data.data_sent_length, data_len)) {
189 DEBUG(0,("create_next_pdu_ntlmssp: failed to copy %u bytes of data.\n", (unsigned int)data_len));
190 prs_mem_free(&p->out_data.frag);
194 /* Copy the sign/seal padding data. */
195 if (ss_padding_len) {
196 char pad[SERVER_NDR_PADDING_SIZE];
198 memset(pad, '\0', SERVER_NDR_PADDING_SIZE);
199 if (!prs_copy_data_in(&p->out_data.frag, pad,
201 DEBUG(0,("create_next_pdu_ntlmssp: failed to add %u bytes of pad data.\n",
202 (unsigned int)ss_padding_len));
203 prs_mem_free(&p->out_data.frag);
209 /* Now write out the auth header and null blob. */
210 if (p->auth.auth_type == PIPE_AUTH_TYPE_NTLMSSP) {
211 auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
213 auth_type = DCERPC_AUTH_TYPE_SPNEGO;
215 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
216 auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
218 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
221 init_rpc_hdr_auth(&auth_info, auth_type, auth_level, ss_padding_len, 1 /* context id. */);
223 if (!smb_io_rpc_hdr_auth("hdr_auth", &auth_info,
224 &p->out_data.frag, 0)) {
225 DEBUG(0,("create_next_pdu_ntlmssp: failed to marshall RPC_HDR_AUTH.\n"));
226 prs_mem_free(&p->out_data.frag);
230 /* Generate the sign blob. */
232 frame = talloc_stackframe();
233 switch (p->auth.auth_level) {
234 case DCERPC_AUTH_LEVEL_PRIVACY:
235 /* Data portion is encrypted. */
236 status = auth_ntlmssp_seal_packet(
238 (uint8_t *)prs_data_p(&p->out_data.frag)
239 + RPC_HEADER_LEN + RPC_HDR_RESP_LEN,
240 data_len + ss_padding_len,
241 (unsigned char *)prs_data_p(&p->out_data.frag),
242 (size_t)prs_offset(&p->out_data.frag),
244 if (!NT_STATUS_IS_OK(status)) {
246 prs_mem_free(&p->out_data.frag);
250 case DCERPC_AUTH_LEVEL_INTEGRITY:
251 /* Data is signed. */
252 status = auth_ntlmssp_sign_packet(
254 (unsigned char *)prs_data_p(&p->out_data.frag)
255 + RPC_HEADER_LEN + RPC_HDR_RESP_LEN,
256 data_len + ss_padding_len,
257 (unsigned char *)prs_data_p(&p->out_data.frag),
258 (size_t)prs_offset(&p->out_data.frag),
260 if (!NT_STATUS_IS_OK(status)) {
262 prs_mem_free(&p->out_data.frag);
268 prs_mem_free(&p->out_data.frag);
272 /* Append the auth blob. */
273 if (!prs_copy_data_in(&p->out_data.frag, (char *)auth_blob.data,
275 DEBUG(0,("create_next_pdu_ntlmssp: failed to add %u bytes auth blob.\n",
276 (unsigned int)NTLMSSP_SIG_SIZE));
278 prs_mem_free(&p->out_data.frag);
284 * Setup the counts for this PDU.
287 p->out_data.data_sent_length += data_len;
288 p->out_data.current_pdu_sent = 0;
293 /*******************************************************************
294 Generate the next PDU to be returned from the data in p->rdata.
295 Return an schannel authenticated fragment.
296 ********************************************************************/
298 static bool create_next_pdu_schannel(pipes_struct *p)
300 RPC_HDR_RESP hdr_resp;
301 uint32 ss_padding_len = 0;
303 uint32 data_space_available;
304 uint32 data_len_left;
309 * If we're in the fault state, keep returning fault PDU's until
310 * the pipe gets closed. JRA.
314 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
318 memset((char *)&hdr_resp, '\0', sizeof(hdr_resp));
320 /* Change the incoming request header to a response. */
321 p->hdr.pkt_type = DCERPC_PKT_RESPONSE;
323 /* Set up rpc header flags. */
324 if (p->out_data.data_sent_length == 0) {
325 p->hdr.flags = DCERPC_PFC_FLAG_FIRST;
331 * Work out how much we can fit in a single PDU.
334 data_len_left = prs_offset(&p->out_data.rdata) - p->out_data.data_sent_length;
337 * Ensure there really is data left to send.
341 DEBUG(0,("create_next_pdu_schannel: no data left to send !\n"));
345 /* Space available - not including padding. */
346 data_space_available = RPC_MAX_PDU_FRAG_LEN - RPC_HEADER_LEN
347 - RPC_HDR_RESP_LEN - RPC_HDR_AUTH_LEN
348 - RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN;
351 * The amount we send is the minimum of the available
352 * space and the amount left to send.
355 data_len = MIN(data_len_left, data_space_available);
357 /* Work out any padding alignment requirements. */
358 if ((RPC_HEADER_LEN + RPC_HDR_RESP_LEN + data_len) % SERVER_NDR_PADDING_SIZE) {
359 ss_padding_len = SERVER_NDR_PADDING_SIZE -
360 ((RPC_HEADER_LEN + RPC_HDR_RESP_LEN + data_len) % SERVER_NDR_PADDING_SIZE);
361 DEBUG(10,("create_next_pdu_schannel: adding sign/seal padding of %u\n",
363 /* If we're over filling the packet, we need to make space
364 * for the padding at the end of the data. */
365 if (data_len + ss_padding_len > data_space_available) {
366 data_len -= SERVER_NDR_PADDING_SIZE;
371 * Set up the alloc hint. This should be the data left to
375 hdr_resp.alloc_hint = data_len_left;
378 * Work out if this PDU will be the last.
381 if(p->out_data.data_sent_length + data_len >= prs_offset(&p->out_data.rdata)) {
382 p->hdr.flags |= DCERPC_PFC_FLAG_LAST;
385 p->hdr.frag_len = RPC_HEADER_LEN + RPC_HDR_RESP_LEN + data_len + ss_padding_len +
386 RPC_HDR_AUTH_LEN + RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN;
387 p->hdr.auth_len = RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN;
390 * Init the parse struct to point at the outgoing
394 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
396 /* Store the header in the data stream. */
397 if(!smb_io_rpc_hdr("hdr", &p->hdr, &p->out_data.frag, 0)) {
398 DEBUG(0,("create_next_pdu_schannel: failed to marshall RPC_HDR.\n"));
399 prs_mem_free(&p->out_data.frag);
403 if(!smb_io_rpc_hdr_resp("resp", &hdr_resp, &p->out_data.frag, 0)) {
404 DEBUG(0,("create_next_pdu_schannel: failed to marshall RPC_HDR_RESP.\n"));
405 prs_mem_free(&p->out_data.frag);
409 /* Store the current offset. */
410 data_pos = prs_offset(&p->out_data.frag);
412 /* Copy the data into the PDU. */
414 if(!prs_append_some_prs_data(&p->out_data.frag, &p->out_data.rdata,
415 p->out_data.data_sent_length, data_len)) {
416 DEBUG(0,("create_next_pdu_schannel: failed to copy %u bytes of data.\n", (unsigned int)data_len));
417 prs_mem_free(&p->out_data.frag);
421 /* Copy the sign/seal padding data. */
422 if (ss_padding_len) {
423 char pad[SERVER_NDR_PADDING_SIZE];
424 memset(pad, '\0', SERVER_NDR_PADDING_SIZE);
425 if (!prs_copy_data_in(&p->out_data.frag, pad,
427 DEBUG(0,("create_next_pdu_schannel: failed to add %u bytes of pad data.\n", (unsigned int)ss_padding_len));
428 prs_mem_free(&p->out_data.frag);
435 * Schannel processing.
437 RPC_HDR_AUTH auth_info;
441 /* Check it's the type of reply we were expecting to decode */
443 init_rpc_hdr_auth(&auth_info,
444 DCERPC_AUTH_TYPE_SCHANNEL,
445 p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY ?
446 DCERPC_AUTH_LEVEL_PRIVACY : DCERPC_AUTH_LEVEL_INTEGRITY,
449 if (!smb_io_rpc_hdr_auth("hdr_auth", &auth_info,
450 &p->out_data.frag, 0)) {
451 DEBUG(0,("create_next_pdu_schannel: failed to marshall RPC_HDR_AUTH.\n"));
452 prs_mem_free(&p->out_data.frag);
456 data = (uint8_t *)prs_data_p(&p->out_data.frag) + data_pos;
458 switch (p->auth.auth_level) {
459 case DCERPC_AUTH_LEVEL_PRIVACY:
460 status = netsec_outgoing_packet(p->auth.a_u.schannel_auth,
464 data_len + ss_padding_len,
467 case DCERPC_AUTH_LEVEL_INTEGRITY:
468 status = netsec_outgoing_packet(p->auth.a_u.schannel_auth,
472 data_len + ss_padding_len,
476 status = NT_STATUS_INTERNAL_ERROR;
480 if (!NT_STATUS_IS_OK(status)) {
481 DEBUG(0,("create_next_pdu_schannel: failed to process packet: %s\n",
483 prs_mem_free(&p->out_data.frag);
487 /* Finally marshall the blob. */
489 if (DEBUGLEVEL >= 10) {
490 dump_NL_AUTH_SIGNATURE(talloc_tos(), &blob);
493 if (!prs_copy_data_in(&p->out_data.frag, (const char *)blob.data, blob.length)) {
494 prs_mem_free(&p->out_data.frag);
500 * Setup the counts for this PDU.
503 p->out_data.data_sent_length += data_len;
504 p->out_data.current_pdu_sent = 0;
509 /*******************************************************************
510 Generate the next PDU to be returned from the data in p->rdata.
511 No authentication done.
512 ********************************************************************/
514 static bool create_next_pdu_noauth(pipes_struct *p)
516 RPC_HDR_RESP hdr_resp;
518 uint32 data_space_available;
519 uint32 data_len_left;
522 * If we're in the fault state, keep returning fault PDU's until
523 * the pipe gets closed. JRA.
527 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
531 memset((char *)&hdr_resp, '\0', sizeof(hdr_resp));
533 /* Change the incoming request header to a response. */
534 p->hdr.pkt_type = DCERPC_PKT_RESPONSE;
536 /* Set up rpc header flags. */
537 if (p->out_data.data_sent_length == 0) {
538 p->hdr.flags = DCERPC_PFC_FLAG_FIRST;
544 * Work out how much we can fit in a single PDU.
547 data_len_left = prs_offset(&p->out_data.rdata) - p->out_data.data_sent_length;
550 * Ensure there really is data left to send.
554 DEBUG(0,("create_next_pdu_noath: no data left to send !\n"));
558 data_space_available = RPC_MAX_PDU_FRAG_LEN - RPC_HEADER_LEN
562 * The amount we send is the minimum of the available
563 * space and the amount left to send.
566 data_len = MIN(data_len_left, data_space_available);
569 * Set up the alloc hint. This should be the data left to
573 hdr_resp.alloc_hint = data_len_left;
576 * Work out if this PDU will be the last.
579 if(p->out_data.data_sent_length + data_len >= prs_offset(&p->out_data.rdata)) {
580 p->hdr.flags |= DCERPC_PFC_FLAG_LAST;
584 * Set up the header lengths.
587 p->hdr.frag_len = RPC_HEADER_LEN + RPC_HDR_RESP_LEN + data_len;
591 * Init the parse struct to point at the outgoing
595 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
597 /* Store the header in the data stream. */
598 if(!smb_io_rpc_hdr("hdr", &p->hdr, &p->out_data.frag, 0)) {
599 DEBUG(0,("create_next_pdu_noath: failed to marshall RPC_HDR.\n"));
600 prs_mem_free(&p->out_data.frag);
604 if(!smb_io_rpc_hdr_resp("resp", &hdr_resp, &p->out_data.frag, 0)) {
605 DEBUG(0,("create_next_pdu_noath: failed to marshall RPC_HDR_RESP.\n"));
606 prs_mem_free(&p->out_data.frag);
610 /* Copy the data into the PDU. */
612 if(!prs_append_some_prs_data(&p->out_data.frag, &p->out_data.rdata,
613 p->out_data.data_sent_length, data_len)) {
614 DEBUG(0,("create_next_pdu_noauth: failed to copy %u bytes of data.\n", (unsigned int)data_len));
615 prs_mem_free(&p->out_data.frag);
620 * Setup the counts for this PDU.
623 p->out_data.data_sent_length += data_len;
624 p->out_data.current_pdu_sent = 0;
629 /*******************************************************************
630 Generate the next PDU to be returned from the data in p->rdata.
631 ********************************************************************/
633 bool create_next_pdu(pipes_struct *p)
635 switch(p->auth.auth_level) {
636 case DCERPC_AUTH_LEVEL_NONE:
637 case DCERPC_AUTH_LEVEL_CONNECT:
638 /* This is incorrect for auth level connect. Fixme. JRA */
639 return create_next_pdu_noauth(p);
642 switch(p->auth.auth_type) {
643 case PIPE_AUTH_TYPE_NTLMSSP:
644 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
645 return create_next_pdu_ntlmssp(p);
646 case PIPE_AUTH_TYPE_SCHANNEL:
647 return create_next_pdu_schannel(p);
653 DEBUG(0,("create_next_pdu: invalid internal auth level %u / type %u",
654 (unsigned int)p->auth.auth_level,
655 (unsigned int)p->auth.auth_type));
659 /*******************************************************************
660 Process an NTLMSSP authentication response.
661 If this function succeeds, the user has been authenticated
662 and their domain, name and calling workstation stored in
664 *******************************************************************/
666 static bool pipe_ntlmssp_verify_final(pipes_struct *p, DATA_BLOB *p_resp_blob)
668 DATA_BLOB session_key, reply;
670 struct auth_ntlmssp_state *a = p->auth.a_u.auth_ntlmssp_state;
673 DEBUG(5,("pipe_ntlmssp_verify_final: pipe %s checking user details\n",
674 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
678 /* this has to be done as root in order to verify the password */
680 status = auth_ntlmssp_update(a, *p_resp_blob, &reply);
683 /* Don't generate a reply. */
684 data_blob_free(&reply);
686 if (!NT_STATUS_IS_OK(status)) {
690 /* Finally - if the pipe negotiated integrity (sign) or privacy (seal)
691 ensure the underlying NTLMSSP flags are also set. If not we should
694 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_INTEGRITY) {
695 if (!auth_ntlmssp_negotiated_sign(a)) {
696 DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet integrity requested "
697 "but client declined signing.\n",
698 get_pipe_name_from_syntax(talloc_tos(),
703 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
704 if (!auth_ntlmssp_negotiated_seal(a)) {
705 DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet privacy requested "
706 "but client declined sealing.\n",
707 get_pipe_name_from_syntax(talloc_tos(),
713 DEBUG(5, ("pipe_ntlmssp_verify_final: OK: user: %s domain: %s "
715 auth_ntlmssp_get_username(a),
716 auth_ntlmssp_get_domain(a),
717 auth_ntlmssp_get_client(a)));
719 TALLOC_FREE(p->server_info);
721 p->server_info = auth_ntlmssp_server_info(p, a);
722 if (p->server_info == NULL) {
723 DEBUG(0, ("auth_ntlmssp_server_info failed to obtain the server info for authenticated user\n"));
727 if (p->server_info->ptok == NULL) {
728 DEBUG(1,("Error: Authmodule failed to provide nt_user_token\n"));
733 * We're an authenticated bind over smb, so the session key needs to
734 * be set to "SystemLibraryDTC". Weird, but this is what Windows
735 * does. See the RPC-SAMBA3SESSIONKEY.
738 session_key = generic_session_key();
739 if (session_key.data == NULL) {
743 ret = server_info_set_session_key(p->server_info, session_key);
745 data_blob_free(&session_key);
750 /*******************************************************************
751 This is the "stage3" NTLMSSP response after a bind request and reply.
752 *******************************************************************/
754 bool api_pipe_bind_auth3(pipes_struct *p, prs_struct *rpc_in_p)
756 struct dcerpc_auth auth_info;
759 uint32_t auth_len = p->hdr.auth_len;
762 DEBUG(5,("api_pipe_bind_auth3: decode request. %d\n", __LINE__));
765 DEBUG(0,("api_pipe_bind_auth3: No auth field sent !\n"));
769 /* 4 bytes padding. */
770 if (!prs_uint32("pad", rpc_in_p, 0, &pad)) {
771 DEBUG(0,("api_pipe_bind_auth3: unmarshall of 4 byte pad failed.\n"));
775 /* Ensure there's enough data for an authenticated request. */
776 if (RPC_HEADER_LEN + RPC_HDR_AUTH_LEN + auth_len >
778 DEBUG(0,("api_pipe_ntlmssp_auth_process: auth_len "
779 "%u is too large.\n",
780 (unsigned int)auth_len ));
785 * Decode the authentication verifier response.
788 /* Pull the auth header and the following data into a blob. */
789 /* NB. The offset of the auth_header is relative to the *end*
790 * of the packet, not the start. Also, the length of the
791 * data in rpc_in_p is p->hdr.frag_len - RPC_HEADER_LEN,
792 * as the RPC header isn't included in rpc_in_p. */
793 if(!prs_set_offset(rpc_in_p,
794 p->hdr.frag_len - RPC_HEADER_LEN -
795 RPC_HDR_AUTH_LEN - auth_len)) {
796 DEBUG(0,("api_pipe_bind_auth3: cannot move "
798 (unsigned int)(p->hdr.frag_len -
799 RPC_HDR_AUTH_LEN - auth_len) ));
803 auth_blob = data_blob_const(prs_data_p(rpc_in_p)
804 + prs_offset(rpc_in_p),
805 prs_data_size(rpc_in_p)
806 - prs_offset(rpc_in_p));
808 status = dcerpc_pull_dcerpc_auth(prs_get_mem_context(rpc_in_p),
809 &auth_blob, &auth_info);
810 if (!NT_STATUS_IS_OK(status)) {
811 DEBUG(0, ("Failed to unmarshall dcerpc_auth.\n"));
815 /* We must NEVER look at auth_info->auth_pad_len here,
816 * as old Samba client code gets it wrong and sends it
820 if (auth_info.auth_type != DCERPC_AUTH_TYPE_NTLMSSP) {
821 DEBUG(0,("api_pipe_bind_auth3: incorrect auth type (%u).\n",
822 (unsigned int)auth_info.auth_type ));
827 * The following call actually checks the challenge/response data.
828 * for correctness against the given DOMAIN\user name.
831 if (!pipe_ntlmssp_verify_final(p, &auth_info.credentials)) {
835 p->pipe_bound = True;
841 free_pipe_ntlmssp_auth_data(&p->auth);
842 p->auth.a_u.auth_ntlmssp_state = NULL;
847 /*******************************************************************
848 Marshall a bind_nak pdu.
849 *******************************************************************/
851 static bool setup_bind_nak(pipes_struct *p)
854 union dcerpc_payload u;
857 /* Free any memory in the current return data buffer. */
858 prs_mem_free(&p->out_data.rdata);
861 * Marshall directly into the outgoing PDU space. We
862 * must do this as we need to set to the bind response
863 * header and are never sending more than one PDU here.
866 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
869 * Initialize a bind_nak header.
874 u.bind_nak.reject_reason = 0;
876 status = dcerpc_push_ncacn_packet(p->mem_ctx,
878 DCERPC_PFC_FLAG_FIRST |
879 DCERPC_PFC_FLAG_LAST,
884 if (!NT_STATUS_IS_OK(status)) {
885 prs_mem_free(&p->out_data.frag);
889 if (!prs_copy_data_in(&p->out_data.frag,
890 (char *)blob.data, blob.length)) {
891 prs_mem_free(&p->out_data.frag);
895 p->out_data.data_sent_length = 0;
896 p->out_data.current_pdu_sent = 0;
898 if (p->auth.auth_data_free_func) {
899 (*p->auth.auth_data_free_func)(&p->auth);
901 p->auth.auth_level = DCERPC_AUTH_LEVEL_NONE;
902 p->auth.auth_type = PIPE_AUTH_TYPE_NONE;
903 p->pipe_bound = False;
908 /*******************************************************************
909 Marshall a fault pdu.
910 *******************************************************************/
912 bool setup_fault_pdu(pipes_struct *p, NTSTATUS fault_status)
915 union dcerpc_payload u;
918 /* Free any memory in the current return data buffer. */
919 prs_mem_free(&p->out_data.rdata);
922 * Marshall directly into the outgoing PDU space. We
923 * must do this as we need to set to the bind response
924 * header and are never sending more than one PDU here.
927 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
930 * Initialize a fault header.
935 u.fault.status = NT_STATUS_V(fault_status);
936 u.fault._pad = data_blob_talloc_zero(p->mem_ctx, 4);
938 status = dcerpc_push_ncacn_packet(p->mem_ctx,
940 DCERPC_PFC_FLAG_FIRST |
941 DCERPC_PFC_FLAG_LAST |
942 DCERPC_PFC_FLAG_DID_NOT_EXECUTE,
947 if (!NT_STATUS_IS_OK(status)) {
948 prs_mem_free(&p->out_data.frag);
952 if (!prs_copy_data_in(&p->out_data.frag,
953 (char *)blob.data, blob.length)) {
954 prs_mem_free(&p->out_data.frag);
958 p->out_data.data_sent_length = 0;
959 p->out_data.current_pdu_sent = 0;
964 /*******************************************************************
965 Ensure a bind request has the correct abstract & transfer interface.
966 Used to reject unknown binds from Win2k.
967 *******************************************************************/
969 static bool check_bind_req(struct pipes_struct *p,
970 struct ndr_syntax_id* abstract,
971 struct ndr_syntax_id* transfer,
974 struct pipe_rpc_fns *context_fns;
976 DEBUG(3,("check_bind_req for %s\n",
977 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
979 /* we have to check all now since win2k introduced a new UUID on the lsaprpc pipe */
980 if (rpc_srv_pipe_exists_by_id(abstract) &&
981 ndr_syntax_id_equal(transfer, &ndr_transfer_syntax)) {
982 DEBUG(3, ("check_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
983 rpc_srv_get_pipe_cli_name(abstract),
984 rpc_srv_get_pipe_srv_name(abstract)));
989 context_fns = SMB_MALLOC_P(struct pipe_rpc_fns);
990 if (context_fns == NULL) {
991 DEBUG(0,("check_bind_req: malloc() failed!\n"));
995 context_fns->n_cmds = rpc_srv_get_pipe_num_cmds(abstract);
996 context_fns->cmds = rpc_srv_get_pipe_cmds(abstract);
997 context_fns->context_id = context_id;
999 /* add to the list of open contexts */
1001 DLIST_ADD( p->contexts, context_fns );
1007 * Is a named pipe known?
1008 * @param[in] cli_filename The pipe name requested by the client
1009 * @result Do we want to serve this?
1011 bool is_known_pipename(const char *cli_filename, struct ndr_syntax_id *syntax)
1013 const char *pipename = cli_filename;
1016 if (strnequal(pipename, "\\PIPE\\", 6)) {
1020 if (*pipename == '\\') {
1024 if (lp_disable_spoolss() && strequal(pipename, "spoolss")) {
1025 DEBUG(10, ("refusing spoolss access\n"));
1029 if (rpc_srv_get_pipe_interface_by_cli_name(pipename, syntax)) {
1033 status = smb_probe_module("rpc", pipename);
1034 if (!NT_STATUS_IS_OK(status)) {
1035 DEBUG(10, ("is_known_pipename: %s unknown\n", cli_filename));
1038 DEBUG(10, ("is_known_pipename: %s loaded dynamically\n", pipename));
1041 * Scan the list again for the interface id
1043 if (rpc_srv_get_pipe_interface_by_cli_name(pipename, syntax)) {
1047 DEBUG(10, ("is_known_pipename: pipe %s did not register itself!\n",
1053 /*******************************************************************
1054 Handle a SPNEGO krb5 bind auth.
1055 *******************************************************************/
1057 static bool pipe_spnego_auth_bind_kerberos(pipes_struct *p,
1058 prs_struct *rpc_in_p,
1059 struct dcerpc_auth *pauth_info,
1060 DATA_BLOB *psecblob,
1061 prs_struct *pout_auth)
1066 /*******************************************************************
1067 Handle the first part of a SPNEGO bind auth.
1068 *******************************************************************/
1070 static bool pipe_spnego_auth_bind_negotiate(pipes_struct *p,
1071 prs_struct *rpc_in_p,
1072 uint32_t ss_padding_len,
1073 struct dcerpc_auth *pauth_info,
1074 prs_struct *pout_auth)
1080 char *OIDs[ASN1_MAX_OIDS];
1083 bool got_kerberos_mechanism = false;
1084 struct auth_ntlmssp_state *a = NULL;
1086 ZERO_STRUCT(secblob);
1088 ZERO_STRUCT(response);
1090 if (pauth_info->credentials.data[0] != ASN1_APPLICATION(0)) {
1094 /* parse out the OIDs and the first sec blob */
1095 if (!parse_negTokenTarg(pauth_info->credentials, OIDs, &secblob)) {
1096 DEBUG(0,("pipe_spnego_auth_bind_negotiate: Failed to parse the security blob.\n"));
1100 if (strcmp(OID_KERBEROS5, OIDs[0]) == 0 || strcmp(OID_KERBEROS5_OLD, OIDs[0]) == 0) {
1101 got_kerberos_mechanism = true;
1104 for (i=0;OIDs[i];i++) {
1105 DEBUG(3,("pipe_spnego_auth_bind_negotiate: Got OID %s\n", OIDs[i]));
1106 TALLOC_FREE(OIDs[i]);
1108 DEBUG(3,("pipe_spnego_auth_bind_negotiate: Got secblob of size %lu\n", (unsigned long)secblob.length));
1110 if ( got_kerberos_mechanism && ((lp_security()==SEC_ADS) || USE_KERBEROS_KEYTAB) ) {
1111 bool ret = pipe_spnego_auth_bind_kerberos(p, rpc_in_p, pauth_info, &secblob, pout_auth);
1112 data_blob_free(&secblob);
1116 if (p->auth.auth_type == PIPE_AUTH_TYPE_SPNEGO_NTLMSSP && p->auth.a_u.auth_ntlmssp_state) {
1117 /* Free any previous auth type. */
1118 free_pipe_ntlmssp_auth_data(&p->auth);
1121 if (!got_kerberos_mechanism) {
1122 /* Initialize the NTLM engine. */
1123 status = auth_ntlmssp_start(&a);
1124 if (!NT_STATUS_IS_OK(status)) {
1128 switch (pauth_info->auth_level) {
1129 case DCERPC_AUTH_LEVEL_INTEGRITY:
1130 auth_ntlmssp_want_sign(a);
1132 case DCERPC_AUTH_LEVEL_PRIVACY:
1133 auth_ntlmssp_want_seal(a);
1139 * Pass the first security blob of data to it.
1140 * This can return an error or NT_STATUS_MORE_PROCESSING_REQUIRED
1141 * which means we need another packet to complete the bind.
1144 status = auth_ntlmssp_update(a, secblob, &chal);
1146 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1147 DEBUG(3,("pipe_spnego_auth_bind_negotiate: auth_ntlmssp_update failed.\n"));
1151 /* Generate the response blob we need for step 2 of the bind. */
1152 response = spnego_gen_auth_response(&chal, status, OID_NTLMSSP);
1155 * SPNEGO negotiate down to NTLMSSP. The subsequent
1156 * code to process follow-up packets is not complete
1159 response = spnego_gen_auth_response(NULL,
1160 NT_STATUS_MORE_PROCESSING_REQUIRED,
1164 /* auth_pad_len will be handled by the caller */
1166 status = dcerpc_push_dcerpc_auth(prs_get_mem_context(pout_auth),
1167 DCERPC_AUTH_TYPE_SPNEGO,
1168 pauth_info->auth_level,
1170 1, /* auth_context_id */
1173 if (!NT_STATUS_IS_OK(status)) {
1174 DEBUG(0, ("Marshalling of dcerpc_auth failed.\n"));
1178 /* Copy the blob into the pout_auth parse struct */
1179 if (!prs_copy_data_in(pout_auth, (char *)blob.data, blob.length)) {
1180 DEBUG(0,("pipe_spnego_auth_bind_negotiate: marshalling of data blob failed.\n"));
1184 p->auth.a_u.auth_ntlmssp_state = a;
1185 p->auth.auth_data_free_func = &free_pipe_ntlmssp_auth_data;
1186 p->auth.auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1188 data_blob_free(&secblob);
1189 data_blob_free(&chal);
1190 data_blob_free(&response);
1192 /* We can't set pipe_bound True yet - we need an RPC_ALTER_CONTEXT response packet... */
1197 data_blob_free(&secblob);
1198 data_blob_free(&chal);
1199 data_blob_free(&response);
1201 p->auth.a_u.auth_ntlmssp_state = NULL;
1206 /*******************************************************************
1207 Handle the second part of a SPNEGO bind auth.
1208 *******************************************************************/
1210 static bool pipe_spnego_auth_bind_continue(pipes_struct *p, prs_struct *rpc_in_p,
1211 uint32_t ss_padding_len,
1212 RPC_HDR_AUTH *pauth_info, prs_struct *pout_auth)
1214 RPC_HDR_AUTH auth_info;
1215 DATA_BLOB spnego_blob;
1216 DATA_BLOB auth_blob;
1217 DATA_BLOB auth_reply;
1219 struct auth_ntlmssp_state *a = p->auth.a_u.auth_ntlmssp_state;
1221 ZERO_STRUCT(spnego_blob);
1222 ZERO_STRUCT(auth_blob);
1223 ZERO_STRUCT(auth_reply);
1224 ZERO_STRUCT(response);
1227 * NB. If we've negotiated down from krb5 to NTLMSSP we'll currently
1228 * fail here as 'a' == NULL.
1230 if (p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP || !a) {
1231 DEBUG(0,("pipe_spnego_auth_bind_continue: not in NTLMSSP auth state.\n"));
1235 /* Grab the SPNEGO blob. */
1236 spnego_blob = data_blob(NULL,p->hdr.auth_len);
1238 if (!prs_copy_data_out((char *)spnego_blob.data, rpc_in_p, p->hdr.auth_len)) {
1239 DEBUG(0,("pipe_spnego_auth_bind_continue: Failed to pull %u bytes - the SPNEGO auth header.\n",
1240 (unsigned int)p->hdr.auth_len ));
1244 if (spnego_blob.data[0] != ASN1_CONTEXT(1)) {
1245 DEBUG(0,("pipe_spnego_auth_bind_continue: invalid SPNEGO blob type.\n"));
1249 if (!spnego_parse_auth(spnego_blob, &auth_blob)) {
1250 DEBUG(0,("pipe_spnego_auth_bind_continue: invalid SPNEGO blob.\n"));
1255 * The following call actually checks the challenge/response data.
1256 * for correctness against the given DOMAIN\user name.
1259 if (!pipe_ntlmssp_verify_final(p, &auth_blob)) {
1263 data_blob_free(&spnego_blob);
1264 data_blob_free(&auth_blob);
1266 /* Generate the spnego "accept completed" blob - no incoming data. */
1267 response = spnego_gen_auth_response(&auth_reply, NT_STATUS_OK, OID_NTLMSSP);
1269 /* FIXME - add auth_pad_len here ! */
1271 /* Copy the blob into the pout_auth parse struct */
1272 init_rpc_hdr_auth(&auth_info, DCERPC_AUTH_TYPE_SPNEGO,
1273 pauth_info->auth_level, ss_padding_len, 1);
1274 if(!smb_io_rpc_hdr_auth("", &auth_info, pout_auth, 0)) {
1275 DEBUG(0,("pipe_spnego_auth_bind_continue: marshalling of RPC_HDR_AUTH failed.\n"));
1279 if (!prs_copy_data_in(pout_auth, (char *)response.data, response.length)) {
1280 DEBUG(0,("pipe_spnego_auth_bind_continue: marshalling of data blob failed.\n"));
1284 data_blob_free(&auth_reply);
1285 data_blob_free(&response);
1287 p->pipe_bound = True;
1293 data_blob_free(&spnego_blob);
1294 data_blob_free(&auth_blob);
1295 data_blob_free(&auth_reply);
1296 data_blob_free(&response);
1298 free_pipe_ntlmssp_auth_data(&p->auth);
1299 p->auth.a_u.auth_ntlmssp_state = NULL;
1304 /*******************************************************************
1305 Handle an schannel bind auth.
1306 *******************************************************************/
1308 static bool pipe_schannel_auth_bind(pipes_struct *p,
1309 prs_struct *rpc_in_p,
1310 uint32_t ss_padding_len,
1311 struct dcerpc_auth *auth_info,
1312 prs_struct *pout_auth)
1314 DATA_BLOB auth_blob;
1315 struct NL_AUTH_MESSAGE neg;
1316 struct NL_AUTH_MESSAGE reply;
1319 struct netlogon_creds_CredentialState *creds;
1320 DATA_BLOB session_key;
1321 enum ndr_err_code ndr_err;
1324 ndr_err = ndr_pull_struct_blob(
1325 &auth_info->credentials,
1326 prs_get_mem_context(pout_auth), &neg,
1327 (ndr_pull_flags_fn_t)ndr_pull_NL_AUTH_MESSAGE);
1328 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1329 DEBUG(0,("pipe_schannel_auth_bind: Could not unmarshal SCHANNEL auth neg\n"));
1333 if (DEBUGLEVEL >= 10) {
1334 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &neg);
1337 if (!(neg.Flags & NL_FLAG_OEM_NETBIOS_COMPUTER_NAME)) {
1338 DEBUG(0,("pipe_schannel_auth_bind: Did not receive netbios computer name\n"));
1343 * The neg.oem_netbios_computer.a key here must match the remote computer name
1344 * given in the DOM_CLNT_SRV.uni_comp_name used on all netlogon pipe
1345 * operations that use credentials.
1349 status = schannel_get_creds_state(p, lp_private_dir(),
1350 neg.oem_netbios_computer.a, &creds);
1353 if (!NT_STATUS_IS_OK(status)) {
1354 DEBUG(0, ("pipe_schannel_auth_bind: Attempt to bind using schannel without successful serverauth2\n"));
1358 p->auth.a_u.schannel_auth = talloc(p, struct schannel_state);
1359 if (!p->auth.a_u.schannel_auth) {
1364 p->auth.a_u.schannel_auth->state = SCHANNEL_STATE_START;
1365 p->auth.a_u.schannel_auth->seq_num = 0;
1366 p->auth.a_u.schannel_auth->initiator = false;
1367 p->auth.a_u.schannel_auth->creds = creds;
1370 * JRA. Should we also copy the schannel session key into the pipe session key p->session_key
1371 * here ? We do that for NTLMSSP, but the session key is already set up from the vuser
1372 * struct of the person who opened the pipe. I need to test this further. JRA.
1374 * VL. As we are mapping this to guest set the generic key
1375 * "SystemLibraryDTC" key here. It's a bit difficult to test against
1376 * W2k3, as it does not allow schannel binds against SAMR and LSA
1380 session_key = generic_session_key();
1381 if (session_key.data == NULL) {
1382 DEBUG(0, ("pipe_schannel_auth_bind: Could not alloc session"
1387 ret = server_info_set_session_key(p->server_info, session_key);
1389 data_blob_free(&session_key);
1392 DEBUG(0, ("server_info_set_session_key failed\n"));
1396 /*** SCHANNEL verifier ***/
1398 reply.MessageType = NL_NEGOTIATE_RESPONSE;
1400 reply.Buffer.dummy = 5; /* ??? actually I don't think
1401 * this has any meaning
1404 ndr_err = ndr_push_struct_blob(&auth_blob, talloc_tos(), &reply,
1405 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
1406 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1407 DEBUG(0,("Failed to marshall NL_AUTH_MESSAGE.\n"));
1411 if (DEBUGLEVEL >= 10) {
1412 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &reply);
1415 status = dcerpc_push_dcerpc_auth(prs_get_mem_context(pout_auth),
1416 DCERPC_AUTH_TYPE_SCHANNEL,
1417 auth_info->auth_level,
1419 1, /* auth_context_id */
1422 if (!NT_STATUS_IS_OK(status)) {
1423 DEBUG(0, ("Marshalling of dcerpc_auth failed.\n"));
1427 if (!prs_copy_data_in(pout_auth, (const char *)blob.data, blob.length)) {
1431 DEBUG(10,("pipe_schannel_auth_bind: schannel auth: domain [%s] myname [%s]\n",
1432 neg.oem_netbios_domain.a, neg.oem_netbios_computer.a));
1434 /* We're finished with this bind - no more packets. */
1435 p->auth.auth_data_free_func = NULL;
1436 p->auth.auth_type = PIPE_AUTH_TYPE_SCHANNEL;
1438 p->pipe_bound = True;
1443 /*******************************************************************
1444 Handle an NTLMSSP bind auth.
1445 *******************************************************************/
1447 static bool pipe_ntlmssp_auth_bind(pipes_struct *p,
1448 prs_struct *rpc_in_p,
1449 uint32_t ss_padding_len,
1450 struct dcerpc_auth *auth_info,
1451 prs_struct *pout_auth)
1456 struct auth_ntlmssp_state *a = NULL;
1458 ZERO_STRUCT(response);
1460 if (strncmp((char *)auth_info->credentials.data, "NTLMSSP", 7) != 0) {
1461 DEBUG(0, ("Failed to read NTLMSSP in blob\n"));
1465 /* We have an NTLMSSP blob. */
1466 status = auth_ntlmssp_start(&a);
1467 if (!NT_STATUS_IS_OK(status)) {
1468 DEBUG(0,("pipe_ntlmssp_auth_bind: auth_ntlmssp_start failed: %s\n",
1469 nt_errstr(status) ));
1473 switch (auth_info->auth_level) {
1474 case DCERPC_AUTH_LEVEL_INTEGRITY:
1475 auth_ntlmssp_want_sign(a);
1477 case DCERPC_AUTH_LEVEL_PRIVACY:
1478 auth_ntlmssp_want_seal(a);
1484 status = auth_ntlmssp_update(a, auth_info->credentials, &response);
1485 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1486 DEBUG(0,("pipe_ntlmssp_auth_bind: auth_ntlmssp_update failed: %s\n",
1487 nt_errstr(status) ));
1491 status = dcerpc_push_dcerpc_auth(prs_get_mem_context(pout_auth),
1492 DCERPC_AUTH_TYPE_NTLMSSP,
1493 auth_info->auth_level,
1495 1, /* auth_context_id */
1498 if (!NT_STATUS_IS_OK(status)) {
1499 DEBUG(0, ("Marshalling of dcerpc_auth failed.\n"));
1503 /* Copy the blob into the pout_auth parse struct */
1504 if (!prs_copy_data_in(pout_auth, (char *)blob.data, blob.length)) {
1505 DEBUG(0,("pipe_ntlmssp_auth_bind: marshalling of data blob failed.\n"));
1509 p->auth.a_u.auth_ntlmssp_state = a;
1510 p->auth.auth_data_free_func = &free_pipe_ntlmssp_auth_data;
1511 p->auth.auth_type = PIPE_AUTH_TYPE_NTLMSSP;
1513 data_blob_free(&response);
1515 DEBUG(10,("pipe_ntlmssp_auth_bind: NTLMSSP auth started\n"));
1517 /* We can't set pipe_bound True yet - we need an DCERPC_PKT_AUTH3 response packet... */
1522 data_blob_free(&response);
1524 free_pipe_ntlmssp_auth_data(&p->auth);
1525 p->auth.a_u.auth_ntlmssp_state = NULL;
1529 /*******************************************************************
1530 Respond to a pipe bind request.
1531 *******************************************************************/
1533 bool api_pipe_bind_req(pipes_struct *p, prs_struct *rpc_in_p)
1536 struct dcerpc_bind rpc_bind;
1538 struct dcerpc_auth auth_info;
1539 DATA_BLOB auth_blob;
1541 fstring ack_pipe_name;
1542 prs_struct out_hdr_ba;
1543 prs_struct out_auth;
1545 unsigned int auth_type = DCERPC_AUTH_TYPE_NONE;
1546 uint32_t ss_padding_len = 0;
1548 struct ndr_syntax_id id;
1550 /* No rebinds on a bound pipe - use alter context. */
1551 if (p->pipe_bound) {
1552 DEBUG(2,("api_pipe_bind_req: rejecting bind request on bound "
1554 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1555 return setup_bind_nak(p);
1558 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
1561 * Marshall directly into the outgoing PDU space. We
1562 * must do this as we need to set to the bind response
1563 * header and are never sending more than one PDU here.
1567 * Setup the memory to marshall the ba header, and the
1571 if(!prs_init(&out_hdr_ba, 1024, p->mem_ctx, MARSHALL)) {
1572 DEBUG(0,("api_pipe_bind_req: malloc out_hdr_ba failed.\n"));
1573 prs_mem_free(&p->out_data.frag);
1577 if(!prs_init(&out_auth, 1024, p->mem_ctx, MARSHALL)) {
1578 DEBUG(0,("api_pipe_bind_req: malloc out_auth failed.\n"));
1579 prs_mem_free(&p->out_data.frag);
1580 prs_mem_free(&out_hdr_ba);
1584 DEBUG(5,("api_pipe_bind_req: decode request. %d\n", __LINE__));
1586 /* decode the bind request */
1587 blob_rb = data_blob_const(prs_data_p(rpc_in_p),
1588 prs_data_size(rpc_in_p));
1589 status = dcerpc_pull_dcerpc_bind(talloc_tos(), &blob_rb, &rpc_bind);
1590 if (!NT_STATUS_IS_OK(status)) {
1594 if (rpc_bind.num_contexts == 0) {
1595 DEBUG(0, ("api_pipe_bind_req: no rpc contexts around\n"));
1600 * Try and find the correct pipe name to ensure
1601 * that this is a pipe name we support.
1603 id = rpc_bind.ctx_list[0].abstract_syntax;
1604 if (rpc_srv_pipe_exists_by_id(&id)) {
1605 DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
1606 rpc_srv_get_pipe_cli_name(&id),
1607 rpc_srv_get_pipe_srv_name(&id)));
1609 status = smb_probe_module(
1610 "rpc", get_pipe_name_from_syntax(
1612 &rpc_bind.ctx_list[0].abstract_syntax));
1614 if (NT_STATUS_IS_ERR(status)) {
1615 DEBUG(3,("api_pipe_bind_req: Unknown pipe name %s in bind request.\n",
1616 get_pipe_name_from_syntax(
1618 &rpc_bind.ctx_list[0].abstract_syntax)));
1619 prs_mem_free(&p->out_data.frag);
1620 prs_mem_free(&out_hdr_ba);
1621 prs_mem_free(&out_auth);
1623 return setup_bind_nak(p);
1626 if (rpc_srv_get_pipe_interface_by_cli_name(
1627 get_pipe_name_from_syntax(talloc_tos(),
1630 DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
1631 rpc_srv_get_pipe_cli_name(&id),
1632 rpc_srv_get_pipe_srv_name(&id)));
1634 DEBUG(0, ("module %s doesn't provide functions for "
1636 get_pipe_name_from_syntax(talloc_tos(),
1638 get_pipe_name_from_syntax(talloc_tos(),
1644 /* name has to be \PIPE\xxxxx */
1645 fstrcpy(ack_pipe_name, "\\PIPE\\");
1646 fstrcat(ack_pipe_name, rpc_srv_get_pipe_srv_name(&id));
1648 DEBUG(5,("api_pipe_bind_req: make response. %d\n", __LINE__));
1650 if (rpc_bind.assoc_group_id != 0) {
1651 assoc_gid = rpc_bind.assoc_group_id;
1658 * Create the bind response struct.
1661 /* If the requested abstract synt uuid doesn't match our client pipe,
1662 reject the bind_ack & set the transfer interface synt to all 0's,
1663 ver 0 (observed when NT5 attempts to bind to abstract interfaces
1665 Needed when adding entries to a DACL from NT5 - SK */
1667 if (check_bind_req(p,
1668 &rpc_bind.ctx_list[0].abstract_syntax,
1669 &rpc_bind.ctx_list[0].transfer_syntaxes[0],
1670 rpc_bind.ctx_list[0].context_id)) {
1671 init_rpc_hdr_ba(&hdr_ba,
1672 RPC_MAX_PDU_FRAG_LEN,
1673 RPC_MAX_PDU_FRAG_LEN,
1677 &rpc_bind.ctx_list[0].transfer_syntaxes[0]);
1679 /* Rejection reason: abstract syntax not supported */
1680 init_rpc_hdr_ba(&hdr_ba, RPC_MAX_PDU_FRAG_LEN,
1681 RPC_MAX_PDU_FRAG_LEN, assoc_gid,
1682 ack_pipe_name, 0x1, 0x2, 0x1,
1683 &null_ndr_syntax_id);
1684 p->pipe_bound = False;
1691 if(!smb_io_rpc_hdr_ba("", &hdr_ba, &out_hdr_ba, 0)) {
1692 DEBUG(0,("api_pipe_bind_req: marshalling of RPC_HDR_BA failed.\n"));
1697 * Check if this is an authenticated bind request.
1700 if (p->hdr.auth_len) {
1702 * Decode the authentication verifier.
1705 /* Work out any padding needed before the auth footer. */
1706 if ((RPC_HEADER_LEN + prs_offset(&out_hdr_ba)) % SERVER_NDR_PADDING_SIZE) {
1707 ss_padding_len = SERVER_NDR_PADDING_SIZE -
1708 ((RPC_HEADER_LEN + prs_offset(&out_hdr_ba)) % SERVER_NDR_PADDING_SIZE);
1709 DEBUG(10,("api_pipe_bind_req: auth pad_len = %u\n",
1710 (unsigned int)ss_padding_len ));
1713 /* Quick length check. Won't catch a bad auth footer,
1714 * prevents overrun. */
1716 if (p->hdr.frag_len < RPC_HEADER_LEN + RPC_HDR_AUTH_LEN + p->hdr.auth_len) {
1717 DEBUG(0,("api_pipe_bind_req: auth_len (%u) "
1718 "too long for fragment %u.\n",
1719 (unsigned int)p->hdr.auth_len,
1720 (unsigned int)p->hdr.frag_len ));
1724 /* Pull the auth header and the following data into a blob. */
1725 /* NB. The offset of the auth_header is relative to the *end*
1726 * of the packet, not the start. Also, the length of the
1727 * data in rpc_in_p is p->hdr.frag_len - RPC_HEADER_LEN,
1728 * as the RPC header isn't included in rpc_in_p. */
1729 if(!prs_set_offset(rpc_in_p,
1730 p->hdr.frag_len - RPC_HEADER_LEN -
1731 RPC_HDR_AUTH_LEN - p->hdr.auth_len)) {
1732 DEBUG(0,("api_pipe_bind_req: cannot move "
1734 (unsigned int)(p->hdr.frag_len -
1735 RPC_HDR_AUTH_LEN - p->hdr.auth_len) ));
1739 auth_blob = data_blob_const(prs_data_p(rpc_in_p)
1740 + prs_offset(rpc_in_p),
1741 prs_data_size(rpc_in_p)
1742 - prs_offset(rpc_in_p));
1744 status = dcerpc_pull_dcerpc_auth(prs_get_mem_context(rpc_in_p),
1745 &auth_blob, &auth_info);
1746 if (!NT_STATUS_IS_OK(status)) {
1747 DEBUG(0, ("Unable to unmarshall dcerpc_auth.\n"));
1751 auth_type = auth_info.auth_type;
1753 /* Work out if we have to sign or seal etc. */
1754 switch (auth_info.auth_level) {
1755 case DCERPC_AUTH_LEVEL_INTEGRITY:
1756 p->auth.auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1758 case DCERPC_AUTH_LEVEL_PRIVACY:
1759 p->auth.auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1762 DEBUG(0, ("Unexpected auth level (%u).\n",
1763 (unsigned int)auth_info.auth_level ));
1767 ZERO_STRUCT(auth_info);
1771 case DCERPC_AUTH_TYPE_NTLMSSP:
1772 if (!pipe_ntlmssp_auth_bind(p, rpc_in_p,
1773 ss_padding_len, &auth_info, &out_auth)) {
1779 case DCERPC_AUTH_TYPE_SCHANNEL:
1780 if (!pipe_schannel_auth_bind(p, rpc_in_p,
1781 ss_padding_len, &auth_info, &out_auth)) {
1786 case DCERPC_AUTH_TYPE_SPNEGO:
1787 if (!pipe_spnego_auth_bind_negotiate(p, rpc_in_p,
1788 ss_padding_len, &auth_info, &out_auth)) {
1793 case DCERPC_AUTH_TYPE_NONE:
1794 /* Unauthenticated bind request. */
1795 /* We're finished - no more packets. */
1796 p->auth.auth_type = PIPE_AUTH_TYPE_NONE;
1797 /* We must set the pipe auth_level here also. */
1798 p->auth.auth_level = DCERPC_AUTH_LEVEL_NONE;
1799 p->pipe_bound = True;
1800 /* The session key was initialized from the SMB
1801 * session in make_internal_rpc_pipe_p */
1806 DEBUG(0,("api_pipe_bind_req: unknown auth type %x requested.\n", auth_type ));
1810 * Create the header, now we know the length.
1813 if (prs_offset(&out_auth)) {
1814 auth_len = prs_offset(&out_auth) - RPC_HDR_AUTH_LEN;
1817 init_rpc_hdr(&p->hdr, DCERPC_PKT_BIND_ACK, DCERPC_PFC_FLAG_FIRST | DCERPC_PFC_FLAG_LAST,
1819 RPC_HEADER_LEN + prs_offset(&out_hdr_ba) +
1820 ss_padding_len + prs_offset(&out_auth),
1824 * Marshall the header into the outgoing PDU.
1827 if(!smb_io_rpc_hdr("", &p->hdr, &p->out_data.frag, 0)) {
1828 DEBUG(0,("api_pipe_bind_req: marshalling of RPC_HDR failed.\n"));
1833 * Now add the RPC_HDR_BA and any auth needed.
1836 if(!prs_append_prs_data(&p->out_data.frag, &out_hdr_ba)) {
1837 DEBUG(0,("api_pipe_bind_req: append of RPC_HDR_BA failed.\n"));
1842 if (ss_padding_len) {
1843 char pad[SERVER_NDR_PADDING_SIZE];
1844 memset(pad, '\0', SERVER_NDR_PADDING_SIZE);
1845 if (!prs_copy_data_in(&p->out_data.frag, pad,
1847 DEBUG(0,("api_pipe_bind_req: failed to add %u "
1848 "bytes of pad data.\n",
1849 (unsigned int)ss_padding_len));
1854 if (!prs_append_prs_data( &p->out_data.frag, &out_auth)) {
1855 DEBUG(0,("api_pipe_bind_req: append of auth info failed.\n"));
1861 * Setup the lengths for the initial reply.
1864 p->out_data.data_sent_length = 0;
1865 p->out_data.current_pdu_sent = 0;
1867 prs_mem_free(&out_hdr_ba);
1868 prs_mem_free(&out_auth);
1874 prs_mem_free(&p->out_data.frag);
1875 prs_mem_free(&out_hdr_ba);
1876 prs_mem_free(&out_auth);
1877 return setup_bind_nak(p);
1880 /****************************************************************************
1881 Deal with an alter context call. Can be third part of 3 leg auth request for
1883 ****************************************************************************/
1885 bool api_pipe_alter_context(pipes_struct *p, prs_struct *rpc_in_p)
1888 struct dcerpc_bind rpc_bind;
1890 RPC_HDR_AUTH auth_info;
1892 fstring ack_pipe_name;
1893 prs_struct out_hdr_ba;
1894 prs_struct out_auth;
1896 uint32_t ss_padding_len = 0;
1899 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
1902 * Marshall directly into the outgoing PDU space. We
1903 * must do this as we need to set to the bind response
1904 * header and are never sending more than one PDU here.
1908 * Setup the memory to marshall the ba header, and the
1912 if(!prs_init(&out_hdr_ba, 1024, p->mem_ctx, MARSHALL)) {
1913 DEBUG(0,("api_pipe_alter_context: malloc out_hdr_ba failed.\n"));
1914 prs_mem_free(&p->out_data.frag);
1918 if(!prs_init(&out_auth, 1024, p->mem_ctx, MARSHALL)) {
1919 DEBUG(0,("api_pipe_alter_context: malloc out_auth failed.\n"));
1920 prs_mem_free(&p->out_data.frag);
1921 prs_mem_free(&out_hdr_ba);
1925 DEBUG(5,("api_pipe_alter_context: decode request. %d\n", __LINE__));
1927 /* decode the alter context request */
1928 blob_rb = data_blob_const(prs_data_p(rpc_in_p),
1929 prs_data_size(rpc_in_p));
1930 status = dcerpc_pull_dcerpc_bind(talloc_tos(), &blob_rb, &rpc_bind);
1931 if (!NT_STATUS_IS_OK(status)) {
1935 /* secondary address CAN be NULL
1936 * as the specs say it's ignored.
1937 * It MUST be NULL to have the spoolss working.
1939 fstrcpy(ack_pipe_name,"");
1941 DEBUG(5,("api_pipe_alter_context: make response. %d\n", __LINE__));
1943 if (rpc_bind.assoc_group_id != 0) {
1944 assoc_gid = rpc_bind.assoc_group_id;
1950 * Create the bind response struct.
1953 /* If the requested abstract synt uuid doesn't match our client pipe,
1954 reject the bind_ack & set the transfer interface synt to all 0's,
1955 ver 0 (observed when NT5 attempts to bind to abstract interfaces
1957 Needed when adding entries to a DACL from NT5 - SK */
1959 if (check_bind_req(p,
1960 &rpc_bind.ctx_list[0].abstract_syntax,
1961 &rpc_bind.ctx_list[0].transfer_syntaxes[0],
1962 rpc_bind.ctx_list[0].context_id)) {
1963 init_rpc_hdr_ba(&hdr_ba,
1964 RPC_MAX_PDU_FRAG_LEN,
1965 RPC_MAX_PDU_FRAG_LEN,
1969 &rpc_bind.ctx_list[0].transfer_syntaxes[0]);
1971 /* Rejection reason: abstract syntax not supported */
1972 init_rpc_hdr_ba(&hdr_ba, RPC_MAX_PDU_FRAG_LEN,
1973 RPC_MAX_PDU_FRAG_LEN, assoc_gid,
1974 ack_pipe_name, 0x1, 0x2, 0x1,
1975 &null_ndr_syntax_id);
1976 p->pipe_bound = False;
1983 if(!smb_io_rpc_hdr_ba("", &hdr_ba, &out_hdr_ba, 0)) {
1984 DEBUG(0,("api_pipe_alter_context: marshalling of RPC_HDR_BA failed.\n"));
1990 * Check if this is an authenticated alter context request.
1993 if (p->hdr.auth_len != 0) {
1995 * Decode the authentication verifier.
1998 /* Work out any padding needed before the auth footer. */
1999 if ((RPC_HEADER_LEN + prs_offset(&out_hdr_ba)) % SERVER_NDR_PADDING_SIZE) {
2000 ss_padding_len = SERVER_NDR_PADDING_SIZE -
2001 ((RPC_HEADER_LEN + prs_offset(&out_hdr_ba)) % SERVER_NDR_PADDING_SIZE);
2002 DEBUG(10,("api_pipe_alter_context: auth pad_len = %u\n",
2003 (unsigned int)ss_padding_len ));
2006 /* Quick length check. Won't catch a bad auth footer,
2007 * prevents overrun. */
2009 if (p->hdr.frag_len < RPC_HEADER_LEN + RPC_HDR_AUTH_LEN + p->hdr.auth_len) {
2010 DEBUG(0,("api_pipe_alter_context: auth_len (%u) "
2011 "too long for fragment %u.\n",
2012 (unsigned int)p->hdr.auth_len,
2013 (unsigned int)p->hdr.frag_len ));
2017 /* Pull the auth header and the following data into a blob. */
2018 /* NB. The offset of the auth_header is relative to the *end*
2019 * of the packet, not the start. Also, the length of the
2020 * data in rpc_in_p is p->hdr.frag_len - RPC_HEADER_LEN,
2021 * as the RPC header isn't included in rpc_in_p. */
2022 if(!prs_set_offset(rpc_in_p,
2023 p->hdr.frag_len - RPC_HEADER_LEN -
2024 RPC_HDR_AUTH_LEN - p->hdr.auth_len)) {
2025 DEBUG(0,("api_alter_context: cannot move "
2027 (unsigned int)(p->hdr.frag_len -
2028 RPC_HDR_AUTH_LEN - p->hdr.auth_len) ));
2032 if(!smb_io_rpc_hdr_auth("", &auth_info, rpc_in_p, 0)) {
2033 DEBUG(0,("api_pipe_alter_context: unable to unmarshall RPC_HDR_AUTH struct.\n"));
2038 * Currently only the SPNEGO auth type uses the alter ctx
2039 * response in place of the NTLMSSP auth3 type.
2042 if (auth_info.auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
2043 /* We can only finish if the pipe is unbound. */
2044 if (!p->pipe_bound) {
2045 if (!pipe_spnego_auth_bind_continue(p, rpc_in_p,
2046 ss_padding_len, &auth_info, &out_auth)) {
2054 ZERO_STRUCT(auth_info);
2057 * Create the header, now we know the length.
2060 if (prs_offset(&out_auth)) {
2061 auth_len = prs_offset(&out_auth) - RPC_HDR_AUTH_LEN;
2064 init_rpc_hdr(&p->hdr, DCERPC_PKT_ALTER_RESP, DCERPC_PFC_FLAG_FIRST | DCERPC_PFC_FLAG_LAST,
2066 RPC_HEADER_LEN + prs_offset(&out_hdr_ba) + prs_offset(&out_auth),
2070 * Marshall the header into the outgoing PDU.
2073 if(!smb_io_rpc_hdr("", &p->hdr, &p->out_data.frag, 0)) {
2074 DEBUG(0,("api_pipe_alter_context: marshalling of RPC_HDR failed.\n"));
2079 * Now add the RPC_HDR_BA and any auth needed.
2082 if(!prs_append_prs_data(&p->out_data.frag, &out_hdr_ba)) {
2083 DEBUG(0,("api_pipe_alter_context: append of RPC_HDR_BA failed.\n"));
2088 if (ss_padding_len) {
2089 char pad[SERVER_NDR_PADDING_SIZE];
2090 memset(pad, '\0', SERVER_NDR_PADDING_SIZE);
2091 if (!prs_copy_data_in(&p->out_data.frag, pad,
2093 DEBUG(0,("api_pipe_alter_context: failed to add %u "
2094 "bytes of pad data.\n",
2095 (unsigned int)ss_padding_len));
2100 if (!prs_append_prs_data( &p->out_data.frag, &out_auth)) {
2101 DEBUG(0,("api_pipe_alter_context: append of auth info failed.\n"));
2107 * Setup the lengths for the initial reply.
2110 p->out_data.data_sent_length = 0;
2111 p->out_data.current_pdu_sent = 0;
2113 prs_mem_free(&out_hdr_ba);
2114 prs_mem_free(&out_auth);
2119 prs_mem_free(&p->out_data.frag);
2120 prs_mem_free(&out_hdr_ba);
2121 prs_mem_free(&out_auth);
2122 return setup_bind_nak(p);
2125 /****************************************************************************
2126 Deal with NTLMSSP sign & seal processing on an RPC request.
2127 ****************************************************************************/
2129 bool api_pipe_ntlmssp_auth_process(pipes_struct *p, prs_struct *rpc_in,
2130 uint32 *p_ss_padding_len, NTSTATUS *pstatus)
2132 RPC_HDR_AUTH auth_info;
2133 uint32 auth_len = p->hdr.auth_len;
2134 uint32 save_offset = prs_offset(rpc_in);
2135 struct auth_ntlmssp_state *a = p->auth.a_u.auth_ntlmssp_state;
2136 unsigned char *data = NULL;
2138 unsigned char *full_packet_data = NULL;
2139 size_t full_packet_data_len;
2140 DATA_BLOB auth_blob;
2142 *pstatus = NT_STATUS_OK;
2144 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_NONE || p->auth.auth_level == DCERPC_AUTH_LEVEL_CONNECT) {
2149 *pstatus = NT_STATUS_INVALID_PARAMETER;
2153 /* Ensure there's enough data for an authenticated request. */
2154 if (RPC_HEADER_LEN + RPC_HDR_REQ_LEN + RPC_HDR_AUTH_LEN
2155 + auth_len > p->hdr.frag_len) {
2156 DEBUG(0,("api_pipe_ntlmssp_auth_process: auth_len %u is too large.\n",
2157 (unsigned int)auth_len ));
2158 *pstatus = NT_STATUS_INVALID_PARAMETER;
2163 * We need the full packet data + length (minus auth stuff) as well as the packet data + length
2164 * after the RPC header.
2165 * We need to pass in the full packet (minus auth len) to the NTLMSSP sign and check seal
2166 * functions as NTLMv2 checks the rpc headers also.
2167 * Both of these values include any auth_pad_len bytes.
2170 data = (unsigned char *)(prs_data_p(rpc_in) + RPC_HDR_REQ_LEN);
2171 data_len = (size_t)(p->hdr.frag_len - RPC_HEADER_LEN - RPC_HDR_REQ_LEN - RPC_HDR_AUTH_LEN - auth_len);
2173 full_packet_data = p->in_data.current_in_pdu;
2174 full_packet_data_len = p->hdr.frag_len - auth_len;
2176 /* Pull the auth header and the following data into a blob. */
2177 /* NB. The offset of the auth_header is relative to the *end*
2178 * of the packet, not the start. Also, the length of the
2179 * data in rpc_in_p is p->hdr.frag_len - RPC_HEADER_LEN,
2180 * as the RPC header isn't included in rpc_in_p. */
2181 if(!prs_set_offset(rpc_in,
2182 p->hdr.frag_len - RPC_HEADER_LEN -
2183 RPC_HDR_AUTH_LEN - auth_len)) {
2184 DEBUG(0,("api_pipe_ntlmssp_auth_process: cannot move "
2186 (unsigned int)(p->hdr.frag_len -
2187 RPC_HDR_AUTH_LEN - auth_len) ));
2188 *pstatus = NT_STATUS_INVALID_PARAMETER;
2192 if(!smb_io_rpc_hdr_auth("hdr_auth", &auth_info, rpc_in, 0)) {
2193 DEBUG(0,("api_pipe_ntlmssp_auth_process: failed to "
2194 "unmarshall RPC_HDR_AUTH.\n"));
2195 *pstatus = NT_STATUS_INVALID_PARAMETER;
2199 /* Ensure auth_pad_len fits into the packet. */
2200 if (RPC_HEADER_LEN + RPC_HDR_REQ_LEN + auth_info.auth_pad_len +
2201 RPC_HDR_AUTH_LEN + auth_len > p->hdr.frag_len) {
2202 DEBUG(0,("api_pipe_ntlmssp_auth_process: auth_info.auth_pad_len "
2203 "too large (%u), auth_len (%u), frag_len = (%u).\n",
2204 (unsigned int)auth_info.auth_pad_len,
2205 (unsigned int)auth_len,
2206 (unsigned int)p->hdr.frag_len ));
2207 *pstatus = NT_STATUS_INVALID_PARAMETER;
2211 auth_blob.data = (unsigned char *)prs_data_p(rpc_in) + prs_offset(rpc_in);
2212 auth_blob.length = auth_len;
2214 switch (p->auth.auth_level) {
2215 case DCERPC_AUTH_LEVEL_PRIVACY:
2216 /* Data is encrypted. */
2217 *pstatus = auth_ntlmssp_unseal_packet(a,
2220 full_packet_data_len,
2222 if (!NT_STATUS_IS_OK(*pstatus)) {
2226 case DCERPC_AUTH_LEVEL_INTEGRITY:
2227 /* Data is signed. */
2228 *pstatus = auth_ntlmssp_check_packet(a,
2231 full_packet_data_len,
2233 if (!NT_STATUS_IS_OK(*pstatus)) {
2238 *pstatus = NT_STATUS_INVALID_PARAMETER;
2243 * Return the current pointer to the data offset.
2246 if(!prs_set_offset(rpc_in, save_offset)) {
2247 DEBUG(0,("api_pipe_auth_process: failed to set offset back to %u\n",
2248 (unsigned int)save_offset ));
2249 *pstatus = NT_STATUS_INVALID_PARAMETER;
2254 * Remember the padding length. We must remove it from the real data
2255 * stream once the sign/seal is done.
2258 *p_ss_padding_len = auth_info.auth_pad_len;
2263 /****************************************************************************
2264 Deal with schannel processing on an RPC request.
2265 ****************************************************************************/
2267 bool api_pipe_schannel_process(pipes_struct *p, prs_struct *rpc_in, uint32 *p_ss_padding_len)
2271 uint32 save_offset = prs_offset(rpc_in);
2272 RPC_HDR_AUTH auth_info;
2277 auth_len = p->hdr.auth_len;
2279 if (auth_len < RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN ||
2280 auth_len > RPC_HEADER_LEN +
2284 DEBUG(0,("Incorrect auth_len %u.\n", (unsigned int)auth_len ));
2289 * The following is that length of the data we must verify or unseal.
2290 * This doesn't include the RPC headers or the auth_len or the RPC_HDR_AUTH_LEN
2291 * preceeding the auth_data, but does include the auth_pad_len bytes.
2294 if (p->hdr.frag_len < RPC_HEADER_LEN + RPC_HDR_REQ_LEN + RPC_HDR_AUTH_LEN + auth_len) {
2295 DEBUG(0,("Incorrect frag %u, auth %u.\n",
2296 (unsigned int)p->hdr.frag_len,
2297 (unsigned int)auth_len ));
2301 data_len = p->hdr.frag_len - RPC_HEADER_LEN - RPC_HDR_REQ_LEN -
2302 RPC_HDR_AUTH_LEN - auth_len;
2304 DEBUG(5,("data %d auth %d\n", data_len, auth_len));
2306 /* Pull the auth header and the following data into a blob. */
2307 /* NB. The offset of the auth_header is relative to the *end*
2308 * of the packet, not the start. Also, the length of the
2309 * data in rpc_in_p is p->hdr.frag_len - RPC_HEADER_LEN,
2310 * as the RPC header isn't included in rpc_in_p. */
2311 if(!prs_set_offset(rpc_in,
2312 p->hdr.frag_len - RPC_HEADER_LEN -
2313 RPC_HDR_AUTH_LEN - auth_len)) {
2314 DEBUG(0,("api_pipe_schannel_process: cannot move "
2316 (unsigned int)(p->hdr.frag_len -
2317 RPC_HDR_AUTH_LEN - auth_len) ));
2321 if(!smb_io_rpc_hdr_auth("hdr_auth", &auth_info, rpc_in, 0)) {
2322 DEBUG(0,("api_pipe_schannel_process: failed to "
2323 "unmarshall RPC_HDR_AUTH.\n"));
2327 /* Ensure auth_pad_len fits into the packet. */
2328 if (RPC_HEADER_LEN + RPC_HDR_REQ_LEN + auth_info.auth_pad_len +
2329 RPC_HDR_AUTH_LEN + auth_len > p->hdr.frag_len) {
2330 DEBUG(0,("api_pipe_schannel_process: auth_info.auth_pad_len "
2331 "too large (%u), auth_len (%u), frag_len = (%u).\n",
2332 (unsigned int)auth_info.auth_pad_len,
2333 (unsigned int)auth_len,
2334 (unsigned int)p->hdr.frag_len ));
2338 if (auth_info.auth_type != DCERPC_AUTH_TYPE_SCHANNEL) {
2339 DEBUG(0,("Invalid auth info %d on schannel\n",
2340 auth_info.auth_type));
2344 blob = data_blob_const(prs_data_p(rpc_in) + prs_offset(rpc_in), auth_len);
2346 if (DEBUGLEVEL >= 10) {
2347 dump_NL_AUTH_SIGNATURE(talloc_tos(), &blob);
2350 data = (uint8_t *)prs_data_p(rpc_in)+RPC_HDR_REQ_LEN;
2352 switch (auth_info.auth_level) {
2353 case DCERPC_AUTH_LEVEL_PRIVACY:
2354 status = netsec_incoming_packet(p->auth.a_u.schannel_auth,
2361 case DCERPC_AUTH_LEVEL_INTEGRITY:
2362 status = netsec_incoming_packet(p->auth.a_u.schannel_auth,
2370 status = NT_STATUS_INTERNAL_ERROR;
2374 if (!NT_STATUS_IS_OK(status)) {
2375 DEBUG(0,("failed to unseal packet: %s\n", nt_errstr(status)));
2380 * Return the current pointer to the data offset.
2383 if(!prs_set_offset(rpc_in, save_offset)) {
2384 DEBUG(0,("failed to set offset back to %u\n",
2385 (unsigned int)save_offset ));
2390 * Remember the padding length. We must remove it from the real data
2391 * stream once the sign/seal is done.
2394 *p_ss_padding_len = auth_info.auth_pad_len;
2399 /****************************************************************************
2400 Find the set of RPC functions associated with this context_id
2401 ****************************************************************************/
2403 static PIPE_RPC_FNS* find_pipe_fns_by_context( PIPE_RPC_FNS *list, uint32 context_id )
2405 PIPE_RPC_FNS *fns = NULL;
2408 DEBUG(0,("find_pipe_fns_by_context: ERROR! No context list for pipe!\n"));
2412 for (fns=list; fns; fns=fns->next ) {
2413 if ( fns->context_id == context_id )
2419 /****************************************************************************
2421 ****************************************************************************/
2423 void free_pipe_rpc_context( PIPE_RPC_FNS *list )
2425 PIPE_RPC_FNS *tmp = list;
2437 static bool api_rpcTNP(pipes_struct *p,
2438 const struct api_struct *api_rpc_cmds, int n_cmds);
2440 /****************************************************************************
2441 Find the correct RPC function to call for this request.
2442 If the pipe is authenticated then become the correct UNIX user
2443 before doing the call.
2444 ****************************************************************************/
2446 bool api_pipe_request(pipes_struct *p)
2449 bool changed_user = False;
2450 PIPE_RPC_FNS *pipe_fns;
2452 if (p->pipe_bound &&
2453 ((p->auth.auth_type == PIPE_AUTH_TYPE_NTLMSSP) ||
2454 (p->auth.auth_type == PIPE_AUTH_TYPE_SPNEGO_NTLMSSP))) {
2455 if(!become_authenticated_pipe_user(p)) {
2456 prs_mem_free(&p->out_data.rdata);
2459 changed_user = True;
2462 DEBUG(5, ("Requested \\PIPE\\%s\n",
2463 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
2465 /* get the set of RPC functions for this context */
2467 pipe_fns = find_pipe_fns_by_context(p->contexts, p->hdr_req.context_id);
2470 TALLOC_CTX *frame = talloc_stackframe();
2471 ret = api_rpcTNP(p, pipe_fns->cmds, pipe_fns->n_cmds);
2475 DEBUG(0,("api_pipe_request: No rpc function table associated with context [%d] on pipe [%s]\n",
2476 p->hdr_req.context_id,
2477 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
2481 unbecome_authenticated_pipe_user();
2487 /*******************************************************************
2488 Calls the underlying RPC function for a named pipe.
2489 ********************************************************************/
2491 static bool api_rpcTNP(pipes_struct *p,
2492 const struct api_struct *api_rpc_cmds, int n_cmds)
2495 uint32 offset1, offset2;
2497 /* interpret the command */
2498 DEBUG(4,("api_rpcTNP: %s op 0x%x - ",
2499 get_pipe_name_from_syntax(talloc_tos(), &p->syntax),
2502 if (DEBUGLEVEL >= 50) {
2504 slprintf(name, sizeof(name)-1, "in_%s",
2505 get_pipe_name_from_syntax(talloc_tos(), &p->syntax));
2506 prs_dump(name, p->hdr_req.opnum, &p->in_data.data);
2509 for (fn_num = 0; fn_num < n_cmds; fn_num++) {
2510 if (api_rpc_cmds[fn_num].opnum == p->hdr_req.opnum && api_rpc_cmds[fn_num].fn != NULL) {
2511 DEBUG(3,("api_rpcTNP: rpc command: %s\n", api_rpc_cmds[fn_num].name));
2516 if (fn_num == n_cmds) {
2518 * For an unknown RPC just return a fault PDU but
2519 * return True to allow RPC's on the pipe to continue
2520 * and not put the pipe into fault state. JRA.
2522 DEBUG(4, ("unknown\n"));
2523 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
2527 offset1 = prs_offset(&p->out_data.rdata);
2529 DEBUG(6, ("api_rpc_cmds[%d].fn == %p\n",
2530 fn_num, api_rpc_cmds[fn_num].fn));
2531 /* do the actual command */
2532 if(!api_rpc_cmds[fn_num].fn(p)) {
2533 DEBUG(0,("api_rpcTNP: %s: %s failed.\n",
2534 get_pipe_name_from_syntax(talloc_tos(), &p->syntax),
2535 api_rpc_cmds[fn_num].name));
2536 prs_mem_free(&p->out_data.rdata);
2540 if (p->bad_handle_fault_state) {
2541 DEBUG(4,("api_rpcTNP: bad handle fault return.\n"));
2542 p->bad_handle_fault_state = False;
2543 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_CONTEXT_MISMATCH));
2547 if (p->rng_fault_state) {
2548 DEBUG(4, ("api_rpcTNP: rng fault return\n"));
2549 p->rng_fault_state = False;
2550 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
2554 offset2 = prs_offset(&p->out_data.rdata);
2555 prs_set_offset(&p->out_data.rdata, offset1);
2556 if (DEBUGLEVEL >= 50) {
2558 slprintf(name, sizeof(name)-1, "out_%s",
2559 get_pipe_name_from_syntax(talloc_tos(), &p->syntax));
2560 prs_dump(name, p->hdr_req.opnum, &p->out_data.rdata);
2562 prs_set_offset(&p->out_data.rdata, offset2);
2564 DEBUG(5,("api_rpcTNP: called %s successfully\n",
2565 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
2567 /* Check for buffer underflow in rpc parsing */
2569 if ((DEBUGLEVEL >= 10) &&
2570 (prs_offset(&p->in_data.data) != prs_data_size(&p->in_data.data))) {
2571 size_t data_len = prs_data_size(&p->in_data.data) - prs_offset(&p->in_data.data);
2572 char *data = (char *)SMB_MALLOC(data_len);
2574 DEBUG(10, ("api_rpcTNP: rpc input buffer underflow (parse error?)\n"));
2576 prs_uint8s(False, "", &p->in_data.data, 0, (unsigned char *)data, (uint32)data_len);