2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Almost completely rewritten by (C) 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/>.
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 "../librpc/gen_ndr/ndr_schannel.h"
32 #include "../libcli/auth/schannel.h"
33 #include "../libcli/auth/spnego.h"
36 extern struct current_user current_user;
39 #define DBGC_CLASS DBGC_RPC_SRV
41 static void free_pipe_ntlmssp_auth_data(struct pipe_auth_data *auth)
43 AUTH_NTLMSSP_STATE *a = auth->a_u.auth_ntlmssp_state;
48 auth->a_u.auth_ntlmssp_state = NULL;
51 static DATA_BLOB generic_session_key(void)
53 return data_blob("SystemLibraryDTC", 16);
56 /*******************************************************************
57 Generate the next PDU to be returned from the data in p->rdata.
59 ********************************************************************/
61 static bool create_next_pdu_ntlmssp(pipes_struct *p)
63 RPC_HDR_RESP hdr_resp;
64 uint32 ss_padding_len = 0;
65 uint32 data_space_available;
70 RPC_HDR_AUTH auth_info;
71 uint8 auth_type, auth_level;
72 AUTH_NTLMSSP_STATE *a = p->auth.a_u.auth_ntlmssp_state;
75 * If we're in the fault state, keep returning fault PDU's until
76 * the pipe gets closed. JRA.
80 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
84 memset((char *)&hdr_resp, '\0', sizeof(hdr_resp));
86 /* Change the incoming request header to a response. */
87 p->hdr.pkt_type = DCERPC_PKT_RESPONSE;
89 /* Set up rpc header flags. */
90 if (p->out_data.data_sent_length == 0) {
91 p->hdr.flags = DCERPC_PFC_FLAG_FIRST;
97 * Work out how much we can fit in a single PDU.
100 data_len_left = prs_offset(&p->out_data.rdata) - p->out_data.data_sent_length;
103 * Ensure there really is data left to send.
107 DEBUG(0,("create_next_pdu_ntlmssp: no data left to send !\n"));
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);
122 * Set up the alloc hint. This should be the data left to
126 hdr_resp.alloc_hint = data_len_left;
129 * Work out if this PDU will be the last.
132 if(p->out_data.data_sent_length + data_len >= prs_offset(&p->out_data.rdata)) {
133 p->hdr.flags |= DCERPC_PFC_FLAG_LAST;
136 if (data_len_left % SERVER_NDR_PADDING_SIZE) {
137 ss_padding_len = SERVER_NDR_PADDING_SIZE - (data_len_left % SERVER_NDR_PADDING_SIZE);
138 DEBUG(10,("create_next_pdu_ntlmssp: adding sign/seal padding of %u\n",
143 * Set up the header lengths.
146 p->hdr.frag_len = RPC_HEADER_LEN + RPC_HDR_RESP_LEN +
147 data_len + ss_padding_len +
148 RPC_HDR_AUTH_LEN + NTLMSSP_SIG_SIZE;
149 p->hdr.auth_len = NTLMSSP_SIG_SIZE;
153 * Init the parse struct to point at the outgoing
157 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
159 /* Store the header in the data stream. */
160 if(!smb_io_rpc_hdr("hdr", &p->hdr, &p->out_data.frag, 0)) {
161 DEBUG(0,("create_next_pdu_ntlmssp: failed to marshall RPC_HDR.\n"));
162 prs_mem_free(&p->out_data.frag);
166 if(!smb_io_rpc_hdr_resp("resp", &hdr_resp, &p->out_data.frag, 0)) {
167 DEBUG(0,("create_next_pdu_ntlmssp: failed to marshall RPC_HDR_RESP.\n"));
168 prs_mem_free(&p->out_data.frag);
172 /* Copy the data into the PDU. */
174 if(!prs_append_some_prs_data(&p->out_data.frag, &p->out_data.rdata,
175 p->out_data.data_sent_length, data_len)) {
176 DEBUG(0,("create_next_pdu_ntlmssp: failed to copy %u bytes of data.\n", (unsigned int)data_len));
177 prs_mem_free(&p->out_data.frag);
181 /* Copy the sign/seal padding data. */
182 if (ss_padding_len) {
183 char pad[SERVER_NDR_PADDING_SIZE];
185 memset(pad, '\0', SERVER_NDR_PADDING_SIZE);
186 if (!prs_copy_data_in(&p->out_data.frag, pad,
188 DEBUG(0,("create_next_pdu_ntlmssp: failed to add %u bytes of pad data.\n",
189 (unsigned int)ss_padding_len));
190 prs_mem_free(&p->out_data.frag);
196 /* Now write out the auth header and null blob. */
197 if (p->auth.auth_type == PIPE_AUTH_TYPE_NTLMSSP) {
198 auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
200 auth_type = DCERPC_AUTH_TYPE_SPNEGO;
202 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
203 auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
205 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
208 init_rpc_hdr_auth(&auth_info, auth_type, auth_level, ss_padding_len, 1 /* context id. */);
210 if (!smb_io_rpc_hdr_auth("hdr_auth", &auth_info,
211 &p->out_data.frag, 0)) {
212 DEBUG(0,("create_next_pdu_ntlmssp: failed to marshall RPC_HDR_AUTH.\n"));
213 prs_mem_free(&p->out_data.frag);
217 /* Generate the sign blob. */
219 switch (p->auth.auth_level) {
220 case DCERPC_AUTH_LEVEL_PRIVACY:
221 /* Data portion is encrypted. */
222 status = ntlmssp_seal_packet(
224 (uint8_t *)prs_data_p(&p->out_data.frag)
225 + RPC_HEADER_LEN + RPC_HDR_RESP_LEN,
226 data_len + ss_padding_len,
227 (unsigned char *)prs_data_p(&p->out_data.frag),
228 (size_t)prs_offset(&p->out_data.frag),
230 if (!NT_STATUS_IS_OK(status)) {
231 data_blob_free(&auth_blob);
232 prs_mem_free(&p->out_data.frag);
236 case DCERPC_AUTH_LEVEL_INTEGRITY:
237 /* Data is signed. */
238 status = ntlmssp_sign_packet(
240 (unsigned char *)prs_data_p(&p->out_data.frag)
241 + RPC_HEADER_LEN + RPC_HDR_RESP_LEN,
242 data_len + ss_padding_len,
243 (unsigned char *)prs_data_p(&p->out_data.frag),
244 (size_t)prs_offset(&p->out_data.frag),
246 if (!NT_STATUS_IS_OK(status)) {
247 data_blob_free(&auth_blob);
248 prs_mem_free(&p->out_data.frag);
253 prs_mem_free(&p->out_data.frag);
257 /* Append the auth blob. */
258 if (!prs_copy_data_in(&p->out_data.frag, (char *)auth_blob.data,
260 DEBUG(0,("create_next_pdu_ntlmssp: failed to add %u bytes auth blob.\n",
261 (unsigned int)NTLMSSP_SIG_SIZE));
262 data_blob_free(&auth_blob);
263 prs_mem_free(&p->out_data.frag);
267 data_blob_free(&auth_blob);
270 * Setup the counts for this PDU.
273 p->out_data.data_sent_length += data_len;
274 p->out_data.current_pdu_sent = 0;
279 /*******************************************************************
280 Generate the next PDU to be returned from the data in p->rdata.
281 Return an schannel authenticated fragment.
282 ********************************************************************/
284 static bool create_next_pdu_schannel(pipes_struct *p)
286 RPC_HDR_RESP hdr_resp;
287 uint32 ss_padding_len = 0;
289 uint32 data_space_available;
290 uint32 data_len_left;
295 * If we're in the fault state, keep returning fault PDU's until
296 * the pipe gets closed. JRA.
300 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
304 memset((char *)&hdr_resp, '\0', sizeof(hdr_resp));
306 /* Change the incoming request header to a response. */
307 p->hdr.pkt_type = DCERPC_PKT_RESPONSE;
309 /* Set up rpc header flags. */
310 if (p->out_data.data_sent_length == 0) {
311 p->hdr.flags = DCERPC_PFC_FLAG_FIRST;
317 * Work out how much we can fit in a single PDU.
320 data_len_left = prs_offset(&p->out_data.rdata) - p->out_data.data_sent_length;
323 * Ensure there really is data left to send.
327 DEBUG(0,("create_next_pdu_schannel: no data left to send !\n"));
331 data_space_available = RPC_MAX_PDU_FRAG_LEN - RPC_HEADER_LEN
332 - RPC_HDR_RESP_LEN - RPC_HDR_AUTH_LEN
333 - RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN;
336 * The amount we send is the minimum of the available
337 * space and the amount left to send.
340 data_len = MIN(data_len_left, data_space_available);
343 * Set up the alloc hint. This should be the data left to
347 hdr_resp.alloc_hint = data_len_left;
350 * Work out if this PDU will be the last.
353 if(p->out_data.data_sent_length + data_len >= prs_offset(&p->out_data.rdata)) {
354 p->hdr.flags |= DCERPC_PFC_FLAG_LAST;
356 if (data_len_left % SERVER_NDR_PADDING_SIZE) {
357 ss_padding_len = SERVER_NDR_PADDING_SIZE - (data_len_left % SERVER_NDR_PADDING_SIZE);
358 DEBUG(10,("create_next_pdu_schannel: adding sign/seal padding of %u\n",
362 p->hdr.frag_len = RPC_HEADER_LEN + RPC_HDR_RESP_LEN + data_len + ss_padding_len +
363 RPC_HDR_AUTH_LEN + RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN;
364 p->hdr.auth_len = RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN;
367 * Init the parse struct to point at the outgoing
371 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
373 /* Store the header in the data stream. */
374 if(!smb_io_rpc_hdr("hdr", &p->hdr, &p->out_data.frag, 0)) {
375 DEBUG(0,("create_next_pdu_schannel: failed to marshall RPC_HDR.\n"));
376 prs_mem_free(&p->out_data.frag);
380 if(!smb_io_rpc_hdr_resp("resp", &hdr_resp, &p->out_data.frag, 0)) {
381 DEBUG(0,("create_next_pdu_schannel: failed to marshall RPC_HDR_RESP.\n"));
382 prs_mem_free(&p->out_data.frag);
386 /* Store the current offset. */
387 data_pos = prs_offset(&p->out_data.frag);
389 /* Copy the data into the PDU. */
391 if(!prs_append_some_prs_data(&p->out_data.frag, &p->out_data.rdata,
392 p->out_data.data_sent_length, data_len)) {
393 DEBUG(0,("create_next_pdu_schannel: failed to copy %u bytes of data.\n", (unsigned int)data_len));
394 prs_mem_free(&p->out_data.frag);
398 /* Copy the sign/seal padding data. */
399 if (ss_padding_len) {
400 char pad[SERVER_NDR_PADDING_SIZE];
401 memset(pad, '\0', SERVER_NDR_PADDING_SIZE);
402 if (!prs_copy_data_in(&p->out_data.frag, pad,
404 DEBUG(0,("create_next_pdu_schannel: failed to add %u bytes of pad data.\n", (unsigned int)ss_padding_len));
405 prs_mem_free(&p->out_data.frag);
412 * Schannel processing.
414 RPC_HDR_AUTH auth_info;
418 /* Check it's the type of reply we were expecting to decode */
420 init_rpc_hdr_auth(&auth_info,
421 DCERPC_AUTH_TYPE_SCHANNEL,
422 p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY ?
423 DCERPC_AUTH_LEVEL_PRIVACY : DCERPC_AUTH_LEVEL_INTEGRITY,
426 if (!smb_io_rpc_hdr_auth("hdr_auth", &auth_info,
427 &p->out_data.frag, 0)) {
428 DEBUG(0,("create_next_pdu_schannel: failed to marshall RPC_HDR_AUTH.\n"));
429 prs_mem_free(&p->out_data.frag);
433 data = (uint8_t *)prs_data_p(&p->out_data.frag) + data_pos;
435 switch (p->auth.auth_level) {
436 case DCERPC_AUTH_LEVEL_PRIVACY:
437 status = netsec_outgoing_packet(p->auth.a_u.schannel_auth,
441 data_len + ss_padding_len,
444 case DCERPC_AUTH_LEVEL_INTEGRITY:
445 status = netsec_outgoing_packet(p->auth.a_u.schannel_auth,
449 data_len + ss_padding_len,
453 status = NT_STATUS_INTERNAL_ERROR;
457 if (!NT_STATUS_IS_OK(status)) {
458 DEBUG(0,("create_next_pdu_schannel: failed to process packet: %s\n",
460 prs_mem_free(&p->out_data.frag);
464 /* Finally marshall the blob. */
466 if (DEBUGLEVEL >= 10) {
467 dump_NL_AUTH_SIGNATURE(talloc_tos(), &blob);
470 if (!prs_copy_data_in(&p->out_data.frag, (const char *)blob.data, blob.length)) {
471 prs_mem_free(&p->out_data.frag);
477 * Setup the counts for this PDU.
480 p->out_data.data_sent_length += data_len;
481 p->out_data.current_pdu_sent = 0;
486 /*******************************************************************
487 Generate the next PDU to be returned from the data in p->rdata.
488 No authentication done.
489 ********************************************************************/
491 static bool create_next_pdu_noauth(pipes_struct *p)
493 RPC_HDR_RESP hdr_resp;
495 uint32 data_space_available;
496 uint32 data_len_left;
499 * If we're in the fault state, keep returning fault PDU's until
500 * the pipe gets closed. JRA.
504 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
508 memset((char *)&hdr_resp, '\0', sizeof(hdr_resp));
510 /* Change the incoming request header to a response. */
511 p->hdr.pkt_type = DCERPC_PKT_RESPONSE;
513 /* Set up rpc header flags. */
514 if (p->out_data.data_sent_length == 0) {
515 p->hdr.flags = DCERPC_PFC_FLAG_FIRST;
521 * Work out how much we can fit in a single PDU.
524 data_len_left = prs_offset(&p->out_data.rdata) - p->out_data.data_sent_length;
527 * Ensure there really is data left to send.
531 DEBUG(0,("create_next_pdu_noath: no data left to send !\n"));
535 data_space_available = RPC_MAX_PDU_FRAG_LEN - RPC_HEADER_LEN
539 * The amount we send is the minimum of the available
540 * space and the amount left to send.
543 data_len = MIN(data_len_left, data_space_available);
546 * Set up the alloc hint. This should be the data left to
550 hdr_resp.alloc_hint = data_len_left;
553 * Work out if this PDU will be the last.
556 if(p->out_data.data_sent_length + data_len >= prs_offset(&p->out_data.rdata)) {
557 p->hdr.flags |= DCERPC_PFC_FLAG_LAST;
561 * Set up the header lengths.
564 p->hdr.frag_len = RPC_HEADER_LEN + RPC_HDR_RESP_LEN + data_len;
568 * Init the parse struct to point at the outgoing
572 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
574 /* Store the header in the data stream. */
575 if(!smb_io_rpc_hdr("hdr", &p->hdr, &p->out_data.frag, 0)) {
576 DEBUG(0,("create_next_pdu_noath: failed to marshall RPC_HDR.\n"));
577 prs_mem_free(&p->out_data.frag);
581 if(!smb_io_rpc_hdr_resp("resp", &hdr_resp, &p->out_data.frag, 0)) {
582 DEBUG(0,("create_next_pdu_noath: failed to marshall RPC_HDR_RESP.\n"));
583 prs_mem_free(&p->out_data.frag);
587 /* Copy the data into the PDU. */
589 if(!prs_append_some_prs_data(&p->out_data.frag, &p->out_data.rdata,
590 p->out_data.data_sent_length, data_len)) {
591 DEBUG(0,("create_next_pdu_noauth: failed to copy %u bytes of data.\n", (unsigned int)data_len));
592 prs_mem_free(&p->out_data.frag);
597 * Setup the counts for this PDU.
600 p->out_data.data_sent_length += data_len;
601 p->out_data.current_pdu_sent = 0;
606 /*******************************************************************
607 Generate the next PDU to be returned from the data in p->rdata.
608 ********************************************************************/
610 bool create_next_pdu(pipes_struct *p)
612 switch(p->auth.auth_level) {
613 case DCERPC_AUTH_LEVEL_NONE:
614 case DCERPC_AUTH_LEVEL_CONNECT:
615 /* This is incorrect for auth level connect. Fixme. JRA */
616 return create_next_pdu_noauth(p);
619 switch(p->auth.auth_type) {
620 case PIPE_AUTH_TYPE_NTLMSSP:
621 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
622 return create_next_pdu_ntlmssp(p);
623 case PIPE_AUTH_TYPE_SCHANNEL:
624 return create_next_pdu_schannel(p);
630 DEBUG(0,("create_next_pdu: invalid internal auth level %u / type %u",
631 (unsigned int)p->auth.auth_level,
632 (unsigned int)p->auth.auth_type));
636 /*******************************************************************
637 Process an NTLMSSP authentication response.
638 If this function succeeds, the user has been authenticated
639 and their domain, name and calling workstation stored in
641 *******************************************************************/
643 static bool pipe_ntlmssp_verify_final(pipes_struct *p, DATA_BLOB *p_resp_blob)
645 DATA_BLOB session_key, reply;
647 AUTH_NTLMSSP_STATE *a = p->auth.a_u.auth_ntlmssp_state;
650 DEBUG(5,("pipe_ntlmssp_verify_final: pipe %s checking user details\n",
651 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
655 /* this has to be done as root in order to verify the password */
657 status = auth_ntlmssp_update(a, *p_resp_blob, &reply);
660 /* Don't generate a reply. */
661 data_blob_free(&reply);
663 if (!NT_STATUS_IS_OK(status)) {
667 /* Finally - if the pipe negotiated integrity (sign) or privacy (seal)
668 ensure the underlying NTLMSSP flags are also set. If not we should
671 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_INTEGRITY) {
672 if (!(a->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN)) {
673 DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet integrity requested "
674 "but client declined signing.\n",
675 get_pipe_name_from_syntax(talloc_tos(),
680 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
681 if (!(a->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL)) {
682 DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet privacy requested "
683 "but client declined sealing.\n",
684 get_pipe_name_from_syntax(talloc_tos(),
690 DEBUG(5, ("pipe_ntlmssp_verify_final: OK: user: %s domain: %s "
691 "workstation: %s\n", a->ntlmssp_state->user,
692 a->ntlmssp_state->domain, a->ntlmssp_state->workstation));
694 if (a->server_info->ptok == NULL) {
695 DEBUG(1,("Error: Authmodule failed to provide nt_user_token\n"));
699 TALLOC_FREE(p->server_info);
701 p->server_info = copy_serverinfo(p, a->server_info);
702 if (p->server_info == NULL) {
703 DEBUG(0, ("copy_serverinfo failed\n"));
708 * We're an authenticated bind over smb, so the session key needs to
709 * be set to "SystemLibraryDTC". Weird, but this is what Windows
710 * does. See the RPC-SAMBA3SESSIONKEY.
713 session_key = generic_session_key();
714 if (session_key.data == NULL) {
718 ret = server_info_set_session_key(p->server_info, session_key);
720 data_blob_free(&session_key);
725 /*******************************************************************
726 The switch table for the pipe names and the functions to handle them.
727 *******************************************************************/
734 struct ndr_syntax_id rpc_interface;
735 const struct api_struct *cmds;
739 static struct rpc_table *rpc_lookup;
740 static int rpc_lookup_size;
742 /*******************************************************************
743 This is the "stage3" NTLMSSP response after a bind request and reply.
744 *******************************************************************/
746 bool api_pipe_bind_auth3(pipes_struct *p, prs_struct *rpc_in_p)
748 RPC_HDR_AUTH auth_info;
751 uint32_t auth_len = p->hdr.auth_len;
755 DEBUG(5,("api_pipe_bind_auth3: decode request. %d\n", __LINE__));
758 DEBUG(0,("api_pipe_bind_auth3: No auth field sent !\n"));
762 /* 4 bytes padding. */
763 if (!prs_uint32("pad", rpc_in_p, 0, &pad)) {
764 DEBUG(0,("api_pipe_bind_auth3: unmarshall of 4 byte pad failed.\n"));
768 /* Ensure there's enough data for an authenticated request. */
769 if (RPC_HEADER_LEN + RPC_HDR_AUTH_LEN + auth_len >
771 DEBUG(0,("api_pipe_ntlmssp_auth_process: auth_len "
772 "%u is too large.\n",
773 (unsigned int)auth_len ));
778 * Decode the authentication verifier response.
781 /* Pull the auth header and the following data into a blob. */
782 /* NB. The offset of the auth_header is relative to the *end*
783 * of the packet, not the start. Also, the length of the
784 * data in rpc_in_p is p->hdr.frag_len - RPC_HEADER_LEN,
785 * as the RPC header isn't included in rpc_in_p. */
786 if(!prs_set_offset(rpc_in_p,
787 p->hdr.frag_len - RPC_HEADER_LEN -
788 RPC_HDR_AUTH_LEN - auth_len)) {
789 DEBUG(0,("api_pipe_bind_auth3: cannot move "
791 (unsigned int)(p->hdr.frag_len -
792 RPC_HDR_AUTH_LEN - auth_len) ));
796 if(!smb_io_rpc_hdr_auth("hdr_auth", &auth_info, rpc_in_p, 0)) {
797 DEBUG(0,("api_pipe_bind_auth3: failed to "
798 "unmarshall RPC_HDR_AUTH.\n"));
802 /* We must NEVER look at auth_info->auth_pad_len here,
803 * as old Samba client code gets it wrong and sends it
807 if (auth_info.auth_type != DCERPC_AUTH_TYPE_NTLMSSP) {
808 DEBUG(0,("api_pipe_bind_auth3: incorrect auth type (%u).\n",
809 (unsigned int)auth_info.auth_type ));
813 blob = data_blob(NULL,p->hdr.auth_len);
815 if (!prs_copy_data_out((char *)blob.data, rpc_in_p, p->hdr.auth_len)) {
816 DEBUG(0,("api_pipe_bind_auth3: Failed to pull %u bytes - the response blob.\n",
817 (unsigned int)p->hdr.auth_len ));
822 * The following call actually checks the challenge/response data.
823 * for correctness against the given DOMAIN\user name.
826 if (!pipe_ntlmssp_verify_final(p, &blob)) {
830 data_blob_free(&blob);
832 p->pipe_bound = True;
838 data_blob_free(&blob);
839 free_pipe_ntlmssp_auth_data(&p->auth);
840 p->auth.a_u.auth_ntlmssp_state = NULL;
845 /*******************************************************************
846 Marshall a bind_nak pdu.
847 *******************************************************************/
849 static bool setup_bind_nak(pipes_struct *p)
854 /* Free any memory in the current return data buffer. */
855 prs_mem_free(&p->out_data.rdata);
858 * Marshall directly into the outgoing PDU space. We
859 * must do this as we need to set to the bind response
860 * header and are never sending more than one PDU here.
863 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
866 * Initialize a bind_nak header.
869 init_rpc_hdr(&nak_hdr, DCERPC_PKT_BIND_NAK, DCERPC_PFC_FLAG_FIRST | DCERPC_PFC_FLAG_LAST,
870 p->hdr.call_id, RPC_HEADER_LEN + sizeof(uint16), 0);
873 * Marshall the header into the outgoing PDU.
876 if(!smb_io_rpc_hdr("", &nak_hdr, &p->out_data.frag, 0)) {
877 DEBUG(0,("setup_bind_nak: marshalling of RPC_HDR failed.\n"));
878 prs_mem_free(&p->out_data.frag);
883 * Now add the reject reason.
886 if(!prs_uint16("reject code", &p->out_data.frag, 0, &zero)) {
887 prs_mem_free(&p->out_data.frag);
891 p->out_data.data_sent_length = 0;
892 p->out_data.current_pdu_sent = 0;
894 if (p->auth.auth_data_free_func) {
895 (*p->auth.auth_data_free_func)(&p->auth);
897 p->auth.auth_level = DCERPC_AUTH_LEVEL_NONE;
898 p->auth.auth_type = PIPE_AUTH_TYPE_NONE;
899 p->pipe_bound = False;
904 /*******************************************************************
905 Marshall a fault pdu.
906 *******************************************************************/
908 bool setup_fault_pdu(pipes_struct *p, NTSTATUS status)
911 RPC_HDR_RESP hdr_resp;
912 RPC_HDR_FAULT fault_resp;
914 /* Free any memory in the current return data buffer. */
915 prs_mem_free(&p->out_data.rdata);
918 * Marshall directly into the outgoing PDU space. We
919 * must do this as we need to set to the bind response
920 * header and are never sending more than one PDU here.
923 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
926 * Initialize a fault header.
929 init_rpc_hdr(&fault_hdr, DCERPC_PKT_FAULT, DCERPC_PFC_FLAG_FIRST | DCERPC_PFC_FLAG_LAST | DCERPC_PFC_FLAG_DID_NOT_EXECUTE,
930 p->hdr.call_id, RPC_HEADER_LEN + RPC_HDR_RESP_LEN + RPC_HDR_FAULT_LEN, 0);
933 * Initialize the HDR_RESP and FAULT parts of the PDU.
936 memset((char *)&hdr_resp, '\0', sizeof(hdr_resp));
938 fault_resp.status = status;
939 fault_resp.reserved = 0;
942 * Marshall the header into the outgoing PDU.
945 if(!smb_io_rpc_hdr("", &fault_hdr, &p->out_data.frag, 0)) {
946 DEBUG(0,("setup_fault_pdu: marshalling of RPC_HDR failed.\n"));
947 prs_mem_free(&p->out_data.frag);
951 if(!smb_io_rpc_hdr_resp("resp", &hdr_resp, &p->out_data.frag, 0)) {
952 DEBUG(0,("setup_fault_pdu: failed to marshall RPC_HDR_RESP.\n"));
953 prs_mem_free(&p->out_data.frag);
957 if(!smb_io_rpc_hdr_fault("fault", &fault_resp, &p->out_data.frag, 0)) {
958 DEBUG(0,("setup_fault_pdu: failed to marshall RPC_HDR_FAULT.\n"));
959 prs_mem_free(&p->out_data.frag);
963 p->out_data.data_sent_length = 0;
964 p->out_data.current_pdu_sent = 0;
970 /*******************************************************************
971 Marshall a cancel_ack pdu.
972 We should probably check the auth-verifier here.
973 *******************************************************************/
975 bool setup_cancel_ack_reply(pipes_struct *p, prs_struct *rpc_in_p)
977 prs_struct outgoing_pdu;
978 RPC_HDR ack_reply_hdr;
980 /* Free any memory in the current return data buffer. */
981 prs_mem_free(&p->out_data.rdata);
984 * Marshall directly into the outgoing PDU space. We
985 * must do this as we need to set to the bind response
986 * header and are never sending more than one PDU here.
989 prs_init_empty( &outgoing_pdu, p->mem_ctx, MARSHALL);
990 prs_give_memory( &outgoing_pdu, (char *)p->out_data.current_pdu, sizeof(p->out_data.current_pdu), False);
993 * Initialize a cancel_ack header.
996 init_rpc_hdr(&ack_reply_hdr, DCERPC_PKT_CANCEL_ACK, DCERPC_PFC_FLAG_FIRST | DCERPC_PFC_FLAG_LAST,
997 p->hdr.call_id, RPC_HEADER_LEN, 0);
1000 * Marshall the header into the outgoing PDU.
1003 if(!smb_io_rpc_hdr("", &ack_reply_hdr, &outgoing_pdu, 0)) {
1004 DEBUG(0,("setup_cancel_ack_reply: marshalling of RPC_HDR failed.\n"));
1005 prs_mem_free(&outgoing_pdu);
1009 p->out_data.data_sent_length = 0;
1010 p->out_data.current_pdu_len = prs_offset(&outgoing_pdu);
1011 p->out_data.current_pdu_sent = 0;
1013 prs_mem_free(&outgoing_pdu);
1018 /*******************************************************************
1019 Ensure a bind request has the correct abstract & transfer interface.
1020 Used to reject unknown binds from Win2k.
1021 *******************************************************************/
1023 static bool check_bind_req(struct pipes_struct *p,
1024 struct ndr_syntax_id* abstract,
1025 struct ndr_syntax_id* transfer,
1029 struct pipe_rpc_fns *context_fns;
1031 DEBUG(3,("check_bind_req for %s\n",
1032 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1034 /* we have to check all now since win2k introduced a new UUID on the lsaprpc pipe */
1036 for (i=0; i<rpc_lookup_size; i++) {
1037 DEBUGADD(10, ("checking %s\n", rpc_lookup[i].pipe.clnt));
1038 if (ndr_syntax_id_equal(
1039 abstract, &rpc_lookup[i].rpc_interface)
1040 && ndr_syntax_id_equal(
1041 transfer, &ndr_transfer_syntax)) {
1046 if (i == rpc_lookup_size) {
1050 context_fns = SMB_MALLOC_P(struct pipe_rpc_fns);
1051 if (context_fns == NULL) {
1052 DEBUG(0,("check_bind_req: malloc() failed!\n"));
1056 context_fns->cmds = rpc_lookup[i].cmds;
1057 context_fns->n_cmds = rpc_lookup[i].n_cmds;
1058 context_fns->context_id = context_id;
1060 /* add to the list of open contexts */
1062 DLIST_ADD( p->contexts, context_fns );
1067 /*******************************************************************
1068 Register commands to an RPC pipe
1069 *******************************************************************/
1071 NTSTATUS rpc_srv_register(int version, const char *clnt, const char *srv,
1072 const struct ndr_interface_table *iface,
1073 const struct api_struct *cmds, int size)
1075 struct rpc_table *rpc_entry;
1077 if (!clnt || !srv || !cmds) {
1078 return NT_STATUS_INVALID_PARAMETER;
1081 if (version != SMB_RPC_INTERFACE_VERSION) {
1082 DEBUG(0,("Can't register rpc commands!\n"
1083 "You tried to register a rpc module with SMB_RPC_INTERFACE_VERSION %d"
1084 ", while this version of samba uses version %d!\n",
1085 version,SMB_RPC_INTERFACE_VERSION));
1086 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1091 * we still need to make sure that don't register the same commands twice!!!
1096 /* We use a temporary variable because this call can fail and
1097 rpc_lookup will still be valid afterwards. It could then succeed if
1098 called again later */
1100 rpc_entry = SMB_REALLOC_ARRAY_KEEP_OLD_ON_ERROR(rpc_lookup, struct rpc_table, rpc_lookup_size);
1101 if (NULL == rpc_entry) {
1103 DEBUG(0, ("rpc_pipe_register_commands: memory allocation failed\n"));
1104 return NT_STATUS_NO_MEMORY;
1106 rpc_lookup = rpc_entry;
1109 rpc_entry = rpc_lookup + (rpc_lookup_size - 1);
1110 ZERO_STRUCTP(rpc_entry);
1111 rpc_entry->pipe.clnt = SMB_STRDUP(clnt);
1112 rpc_entry->pipe.srv = SMB_STRDUP(srv);
1113 rpc_entry->rpc_interface = iface->syntax_id;
1114 rpc_entry->cmds = cmds;
1115 rpc_entry->n_cmds = size;
1117 return NT_STATUS_OK;
1121 * Is a named pipe known?
1122 * @param[in] cli_filename The pipe name requested by the client
1123 * @result Do we want to serve this?
1125 bool is_known_pipename(const char *cli_filename, struct ndr_syntax_id *syntax)
1127 const char *pipename = cli_filename;
1131 if (strnequal(pipename, "\\PIPE\\", 6)) {
1135 if (*pipename == '\\') {
1139 if (lp_disable_spoolss() && strequal(pipename, "spoolss")) {
1140 DEBUG(10, ("refusing spoolss access\n"));
1144 for (i=0; i<rpc_lookup_size; i++) {
1145 if (strequal(pipename, rpc_lookup[i].pipe.clnt)) {
1146 *syntax = rpc_lookup[i].rpc_interface;
1151 status = smb_probe_module("rpc", pipename);
1152 if (!NT_STATUS_IS_OK(status)) {
1153 DEBUG(10, ("is_known_pipename: %s unknown\n", cli_filename));
1156 DEBUG(10, ("is_known_pipename: %s loaded dynamically\n", pipename));
1159 * Scan the list again for the interface id
1162 for (i=0; i<rpc_lookup_size; i++) {
1163 if (strequal(pipename, rpc_lookup[i].pipe.clnt)) {
1164 *syntax = rpc_lookup[i].rpc_interface;
1169 DEBUG(10, ("is_known_pipename: pipe %s did not register itself!\n",
1175 /*******************************************************************
1176 Handle a SPNEGO krb5 bind auth.
1177 *******************************************************************/
1179 static bool pipe_spnego_auth_bind_kerberos(pipes_struct *p, prs_struct *rpc_in_p, RPC_HDR_AUTH *pauth_info,
1180 DATA_BLOB *psecblob, prs_struct *pout_auth)
1185 /*******************************************************************
1186 Handle the first part of a SPNEGO bind auth.
1187 *******************************************************************/
1189 static bool pipe_spnego_auth_bind_negotiate(pipes_struct *p, prs_struct *rpc_in_p,
1190 uint32_t ss_padding_len,
1191 RPC_HDR_AUTH *pauth_info, prs_struct *pout_auth)
1197 char *OIDs[ASN1_MAX_OIDS];
1200 bool got_kerberos_mechanism = false;
1201 AUTH_NTLMSSP_STATE *a = NULL;
1202 RPC_HDR_AUTH auth_info;
1204 ZERO_STRUCT(secblob);
1206 ZERO_STRUCT(response);
1208 /* Grab the SPNEGO blob. */
1209 blob = data_blob(NULL,p->hdr.auth_len);
1211 if (!prs_copy_data_out((char *)blob.data, rpc_in_p, p->hdr.auth_len)) {
1212 DEBUG(0,("pipe_spnego_auth_bind_negotiate: Failed to pull %u bytes - the SPNEGO auth header.\n",
1213 (unsigned int)p->hdr.auth_len ));
1217 if (blob.data[0] != ASN1_APPLICATION(0)) {
1221 /* parse out the OIDs and the first sec blob */
1222 if (!parse_negTokenTarg(blob, OIDs, &secblob)) {
1223 DEBUG(0,("pipe_spnego_auth_bind_negotiate: Failed to parse the security blob.\n"));
1227 if (strcmp(OID_KERBEROS5, OIDs[0]) == 0 || strcmp(OID_KERBEROS5_OLD, OIDs[0]) == 0) {
1228 got_kerberos_mechanism = true;
1231 for (i=0;OIDs[i];i++) {
1232 DEBUG(3,("pipe_spnego_auth_bind_negotiate: Got OID %s\n", OIDs[i]));
1233 TALLOC_FREE(OIDs[i]);
1235 DEBUG(3,("pipe_spnego_auth_bind_negotiate: Got secblob of size %lu\n", (unsigned long)secblob.length));
1237 if ( got_kerberos_mechanism && ((lp_security()==SEC_ADS) || USE_KERBEROS_KEYTAB) ) {
1238 bool ret = pipe_spnego_auth_bind_kerberos(p, rpc_in_p, pauth_info, &secblob, pout_auth);
1239 data_blob_free(&secblob);
1240 data_blob_free(&blob);
1244 if (p->auth.auth_type == PIPE_AUTH_TYPE_SPNEGO_NTLMSSP && p->auth.a_u.auth_ntlmssp_state) {
1245 /* Free any previous auth type. */
1246 free_pipe_ntlmssp_auth_data(&p->auth);
1249 if (!got_kerberos_mechanism) {
1250 /* Initialize the NTLM engine. */
1251 status = auth_ntlmssp_start(&a);
1252 if (!NT_STATUS_IS_OK(status)) {
1257 * Pass the first security blob of data to it.
1258 * This can return an error or NT_STATUS_MORE_PROCESSING_REQUIRED
1259 * which means we need another packet to complete the bind.
1262 status = auth_ntlmssp_update(a, secblob, &chal);
1264 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1265 DEBUG(3,("pipe_spnego_auth_bind_negotiate: auth_ntlmssp_update failed.\n"));
1269 /* Generate the response blob we need for step 2 of the bind. */
1270 response = spnego_gen_auth_response(&chal, status, OID_NTLMSSP);
1273 * SPNEGO negotiate down to NTLMSSP. The subsequent
1274 * code to process follow-up packets is not complete
1277 response = spnego_gen_auth_response(NULL,
1278 NT_STATUS_MORE_PROCESSING_REQUIRED,
1282 /* auth_pad_len will be handled by the caller */
1284 /* Copy the blob into the pout_auth parse struct */
1285 init_rpc_hdr_auth(&auth_info, DCERPC_AUTH_TYPE_SPNEGO,
1286 pauth_info->auth_level, ss_padding_len, 1);
1287 if(!smb_io_rpc_hdr_auth("", &auth_info, pout_auth, 0)) {
1288 DEBUG(0,("pipe_spnego_auth_bind_negotiate: marshalling of RPC_HDR_AUTH failed.\n"));
1292 if (!prs_copy_data_in(pout_auth, (char *)response.data, response.length)) {
1293 DEBUG(0,("pipe_spnego_auth_bind_negotiate: marshalling of data blob failed.\n"));
1297 p->auth.a_u.auth_ntlmssp_state = a;
1298 p->auth.auth_data_free_func = &free_pipe_ntlmssp_auth_data;
1299 p->auth.auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1301 data_blob_free(&blob);
1302 data_blob_free(&secblob);
1303 data_blob_free(&chal);
1304 data_blob_free(&response);
1306 /* We can't set pipe_bound True yet - we need an RPC_ALTER_CONTEXT response packet... */
1311 data_blob_free(&blob);
1312 data_blob_free(&secblob);
1313 data_blob_free(&chal);
1314 data_blob_free(&response);
1316 p->auth.a_u.auth_ntlmssp_state = NULL;
1321 /*******************************************************************
1322 Handle the second part of a SPNEGO bind auth.
1323 *******************************************************************/
1325 static bool pipe_spnego_auth_bind_continue(pipes_struct *p, prs_struct *rpc_in_p,
1326 uint32_t ss_padding_len,
1327 RPC_HDR_AUTH *pauth_info, prs_struct *pout_auth)
1329 RPC_HDR_AUTH auth_info;
1330 DATA_BLOB spnego_blob;
1331 DATA_BLOB auth_blob;
1332 DATA_BLOB auth_reply;
1334 AUTH_NTLMSSP_STATE *a = p->auth.a_u.auth_ntlmssp_state;
1336 ZERO_STRUCT(spnego_blob);
1337 ZERO_STRUCT(auth_blob);
1338 ZERO_STRUCT(auth_reply);
1339 ZERO_STRUCT(response);
1342 * NB. If we've negotiated down from krb5 to NTLMSSP we'll currently
1343 * fail here as 'a' == NULL.
1345 if (p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP || !a) {
1346 DEBUG(0,("pipe_spnego_auth_bind_continue: not in NTLMSSP auth state.\n"));
1350 /* Grab the SPNEGO blob. */
1351 spnego_blob = data_blob(NULL,p->hdr.auth_len);
1353 if (!prs_copy_data_out((char *)spnego_blob.data, rpc_in_p, p->hdr.auth_len)) {
1354 DEBUG(0,("pipe_spnego_auth_bind_continue: Failed to pull %u bytes - the SPNEGO auth header.\n",
1355 (unsigned int)p->hdr.auth_len ));
1359 if (spnego_blob.data[0] != ASN1_CONTEXT(1)) {
1360 DEBUG(0,("pipe_spnego_auth_bind_continue: invalid SPNEGO blob type.\n"));
1364 if (!spnego_parse_auth(spnego_blob, &auth_blob)) {
1365 DEBUG(0,("pipe_spnego_auth_bind_continue: invalid SPNEGO blob.\n"));
1370 * The following call actually checks the challenge/response data.
1371 * for correctness against the given DOMAIN\user name.
1374 if (!pipe_ntlmssp_verify_final(p, &auth_blob)) {
1378 data_blob_free(&spnego_blob);
1379 data_blob_free(&auth_blob);
1381 /* Generate the spnego "accept completed" blob - no incoming data. */
1382 response = spnego_gen_auth_response(&auth_reply, NT_STATUS_OK, OID_NTLMSSP);
1384 /* FIXME - add auth_pad_len here ! */
1386 /* Copy the blob into the pout_auth parse struct */
1387 init_rpc_hdr_auth(&auth_info, DCERPC_AUTH_TYPE_SPNEGO,
1388 pauth_info->auth_level, ss_padding_len, 1);
1389 if(!smb_io_rpc_hdr_auth("", &auth_info, pout_auth, 0)) {
1390 DEBUG(0,("pipe_spnego_auth_bind_continue: marshalling of RPC_HDR_AUTH failed.\n"));
1394 if (!prs_copy_data_in(pout_auth, (char *)response.data, response.length)) {
1395 DEBUG(0,("pipe_spnego_auth_bind_continue: marshalling of data blob failed.\n"));
1399 data_blob_free(&auth_reply);
1400 data_blob_free(&response);
1402 p->pipe_bound = True;
1408 data_blob_free(&spnego_blob);
1409 data_blob_free(&auth_blob);
1410 data_blob_free(&auth_reply);
1411 data_blob_free(&response);
1413 free_pipe_ntlmssp_auth_data(&p->auth);
1414 p->auth.a_u.auth_ntlmssp_state = NULL;
1419 /*******************************************************************
1420 Handle an schannel bind auth.
1421 *******************************************************************/
1423 static bool pipe_schannel_auth_bind(pipes_struct *p, prs_struct *rpc_in_p,
1424 uint32_t ss_padding_len,
1425 RPC_HDR_AUTH *pauth_info, prs_struct *pout_auth)
1427 RPC_HDR_AUTH auth_info;
1428 struct NL_AUTH_MESSAGE neg;
1429 struct NL_AUTH_MESSAGE reply;
1432 struct netlogon_creds_CredentialState *creds;
1433 DATA_BLOB session_key;
1434 enum ndr_err_code ndr_err;
1437 blob = data_blob_const(prs_data_p(rpc_in_p) + prs_offset(rpc_in_p),
1438 prs_data_size(rpc_in_p));
1440 ndr_err = ndr_pull_struct_blob(&blob, talloc_tos(), NULL, &neg,
1441 (ndr_pull_flags_fn_t)ndr_pull_NL_AUTH_MESSAGE);
1442 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1443 DEBUG(0,("pipe_schannel_auth_bind: Could not unmarshal SCHANNEL auth neg\n"));
1447 if (DEBUGLEVEL >= 10) {
1448 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &neg);
1451 if (!(neg.Flags & NL_FLAG_OEM_NETBIOS_COMPUTER_NAME)) {
1452 DEBUG(0,("pipe_schannel_auth_bind: Did not receive netbios computer name\n"));
1457 * The neg.oem_netbios_computer.a key here must match the remote computer name
1458 * given in the DOM_CLNT_SRV.uni_comp_name used on all netlogon pipe
1459 * operations that use credentials.
1463 status = schannel_fetch_session_key(p,
1464 neg.oem_netbios_computer.a,
1468 if (!NT_STATUS_IS_OK(status)) {
1469 DEBUG(0, ("pipe_schannel_auth_bind: Attempt to bind using schannel without successful serverauth2\n"));
1473 p->auth.a_u.schannel_auth = talloc(p, struct schannel_state);
1474 if (!p->auth.a_u.schannel_auth) {
1479 p->auth.a_u.schannel_auth->state = SCHANNEL_STATE_START;
1480 p->auth.a_u.schannel_auth->seq_num = 0;
1481 p->auth.a_u.schannel_auth->initiator = false;
1482 p->auth.a_u.schannel_auth->creds = creds;
1485 * JRA. Should we also copy the schannel session key into the pipe session key p->session_key
1486 * here ? We do that for NTLMSSP, but the session key is already set up from the vuser
1487 * struct of the person who opened the pipe. I need to test this further. JRA.
1489 * VL. As we are mapping this to guest set the generic key
1490 * "SystemLibraryDTC" key here. It's a bit difficult to test against
1491 * W2k3, as it does not allow schannel binds against SAMR and LSA
1495 session_key = generic_session_key();
1496 if (session_key.data == NULL) {
1497 DEBUG(0, ("pipe_schannel_auth_bind: Could not alloc session"
1502 ret = server_info_set_session_key(p->server_info, session_key);
1504 data_blob_free(&session_key);
1507 DEBUG(0, ("server_info_set_session_key failed\n"));
1511 init_rpc_hdr_auth(&auth_info, DCERPC_AUTH_TYPE_SCHANNEL,
1512 pauth_info->auth_level, ss_padding_len, 1);
1513 if(!smb_io_rpc_hdr_auth("", &auth_info, pout_auth, 0)) {
1514 DEBUG(0,("pipe_schannel_auth_bind: marshalling of RPC_HDR_AUTH failed.\n"));
1518 /*** SCHANNEL verifier ***/
1520 reply.MessageType = NL_NEGOTIATE_RESPONSE;
1522 reply.Buffer.dummy = 5; /* ??? actually I don't think
1523 * this has any meaning
1526 ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), NULL, &reply,
1527 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
1528 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1529 DEBUG(0,("Failed to marshall NL_AUTH_MESSAGE.\n"));
1533 if (DEBUGLEVEL >= 10) {
1534 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &reply);
1537 if (!prs_copy_data_in(pout_auth, (const char *)blob.data, blob.length)) {
1541 DEBUG(10,("pipe_schannel_auth_bind: schannel auth: domain [%s] myname [%s]\n",
1542 neg.oem_netbios_domain.a, neg.oem_netbios_computer.a));
1544 /* We're finished with this bind - no more packets. */
1545 p->auth.auth_data_free_func = NULL;
1546 p->auth.auth_type = PIPE_AUTH_TYPE_SCHANNEL;
1548 p->pipe_bound = True;
1553 /*******************************************************************
1554 Handle an NTLMSSP bind auth.
1555 *******************************************************************/
1557 static bool pipe_ntlmssp_auth_bind(pipes_struct *p, prs_struct *rpc_in_p,
1558 uint32_t ss_padding_len,
1559 RPC_HDR_AUTH *pauth_info, prs_struct *pout_auth)
1561 RPC_HDR_AUTH auth_info;
1565 AUTH_NTLMSSP_STATE *a = NULL;
1568 ZERO_STRUCT(response);
1570 /* Grab the NTLMSSP blob. */
1571 blob = data_blob(NULL,p->hdr.auth_len);
1573 if (!prs_copy_data_out((char *)blob.data, rpc_in_p, p->hdr.auth_len)) {
1574 DEBUG(0,("pipe_ntlmssp_auth_bind: Failed to pull %u bytes - the NTLM auth header.\n",
1575 (unsigned int)p->hdr.auth_len ));
1579 if (strncmp((char *)blob.data, "NTLMSSP", 7) != 0) {
1580 DEBUG(0,("pipe_ntlmssp_auth_bind: Failed to read NTLMSSP in blob\n"));
1584 /* We have an NTLMSSP blob. */
1585 status = auth_ntlmssp_start(&a);
1586 if (!NT_STATUS_IS_OK(status)) {
1587 DEBUG(0,("pipe_ntlmssp_auth_bind: auth_ntlmssp_start failed: %s\n",
1588 nt_errstr(status) ));
1592 status = auth_ntlmssp_update(a, blob, &response);
1593 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1594 DEBUG(0,("pipe_ntlmssp_auth_bind: auth_ntlmssp_update failed: %s\n",
1595 nt_errstr(status) ));
1599 data_blob_free(&blob);
1601 /* Copy the blob into the pout_auth parse struct */
1602 init_rpc_hdr_auth(&auth_info, DCERPC_AUTH_TYPE_NTLMSSP,
1603 pauth_info->auth_level, ss_padding_len, 1);
1604 if(!smb_io_rpc_hdr_auth("", &auth_info, pout_auth, 0)) {
1605 DEBUG(0,("pipe_ntlmssp_auth_bind: marshalling of RPC_HDR_AUTH failed.\n"));
1609 if (!prs_copy_data_in(pout_auth, (char *)response.data, response.length)) {
1610 DEBUG(0,("pipe_ntlmssp_auth_bind: marshalling of data blob failed.\n"));
1614 p->auth.a_u.auth_ntlmssp_state = a;
1615 p->auth.auth_data_free_func = &free_pipe_ntlmssp_auth_data;
1616 p->auth.auth_type = PIPE_AUTH_TYPE_NTLMSSP;
1618 data_blob_free(&blob);
1619 data_blob_free(&response);
1621 DEBUG(10,("pipe_ntlmssp_auth_bind: NTLMSSP auth started\n"));
1623 /* We can't set pipe_bound True yet - we need an DCERPC_PKT_AUTH3 response packet... */
1628 data_blob_free(&blob);
1629 data_blob_free(&response);
1631 free_pipe_ntlmssp_auth_data(&p->auth);
1632 p->auth.a_u.auth_ntlmssp_state = NULL;
1636 /*******************************************************************
1637 Respond to a pipe bind request.
1638 *******************************************************************/
1640 bool api_pipe_bind_req(pipes_struct *p, prs_struct *rpc_in_p)
1644 RPC_HDR_AUTH auth_info;
1646 fstring ack_pipe_name;
1647 prs_struct out_hdr_ba;
1648 prs_struct out_auth;
1651 unsigned int auth_type = DCERPC_AUTH_TYPE_NONE;
1652 uint32_t ss_padding_len = 0;
1654 /* No rebinds on a bound pipe - use alter context. */
1655 if (p->pipe_bound) {
1656 DEBUG(2,("api_pipe_bind_req: rejecting bind request on bound "
1658 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1659 return setup_bind_nak(p);
1662 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
1665 * Marshall directly into the outgoing PDU space. We
1666 * must do this as we need to set to the bind response
1667 * header and are never sending more than one PDU here.
1671 * Setup the memory to marshall the ba header, and the
1675 if(!prs_init(&out_hdr_ba, 1024, p->mem_ctx, MARSHALL)) {
1676 DEBUG(0,("api_pipe_bind_req: malloc out_hdr_ba failed.\n"));
1677 prs_mem_free(&p->out_data.frag);
1681 if(!prs_init(&out_auth, 1024, p->mem_ctx, MARSHALL)) {
1682 DEBUG(0,("api_pipe_bind_req: malloc out_auth failed.\n"));
1683 prs_mem_free(&p->out_data.frag);
1684 prs_mem_free(&out_hdr_ba);
1688 DEBUG(5,("api_pipe_bind_req: decode request. %d\n", __LINE__));
1690 ZERO_STRUCT(hdr_rb);
1692 /* decode the bind request */
1694 if(!smb_io_rpc_hdr_rb("", &hdr_rb, rpc_in_p, 0)) {
1695 DEBUG(0,("api_pipe_bind_req: unable to unmarshall RPC_HDR_RB "
1700 if (hdr_rb.num_contexts == 0) {
1701 DEBUG(0, ("api_pipe_bind_req: no rpc contexts around\n"));
1706 * Try and find the correct pipe name to ensure
1707 * that this is a pipe name we support.
1710 for (i = 0; i < rpc_lookup_size; i++) {
1711 if (ndr_syntax_id_equal(&rpc_lookup[i].rpc_interface,
1712 &hdr_rb.rpc_context[0].abstract)) {
1713 DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
1714 rpc_lookup[i].pipe.clnt, rpc_lookup[i].pipe.srv));
1719 if (i == rpc_lookup_size) {
1722 status = smb_probe_module(
1723 "rpc", get_pipe_name_from_syntax(
1725 &hdr_rb.rpc_context[0].abstract));
1727 if (NT_STATUS_IS_ERR(status)) {
1728 DEBUG(3,("api_pipe_bind_req: Unknown pipe name %s in bind request.\n",
1729 get_pipe_name_from_syntax(
1731 &hdr_rb.rpc_context[0].abstract)));
1732 prs_mem_free(&p->out_data.frag);
1733 prs_mem_free(&out_hdr_ba);
1734 prs_mem_free(&out_auth);
1736 return setup_bind_nak(p);
1739 for (i = 0; i < rpc_lookup_size; i++) {
1740 if (strequal(rpc_lookup[i].pipe.clnt,
1741 get_pipe_name_from_syntax(talloc_tos(),
1743 DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
1744 rpc_lookup[i].pipe.clnt, rpc_lookup[i].pipe.srv));
1749 if (i == rpc_lookup_size) {
1750 DEBUG(0, ("module %s doesn't provide functions for "
1752 get_pipe_name_from_syntax(talloc_tos(),
1754 get_pipe_name_from_syntax(talloc_tos(),
1760 /* name has to be \PIPE\xxxxx */
1761 fstrcpy(ack_pipe_name, "\\PIPE\\");
1762 fstrcat(ack_pipe_name, rpc_lookup[i].pipe.srv);
1764 DEBUG(5,("api_pipe_bind_req: make response. %d\n", __LINE__));
1766 assoc_gid = hdr_rb.bba.assoc_gid ? hdr_rb.bba.assoc_gid : 0x53f0;
1769 * Create the bind response struct.
1772 /* If the requested abstract synt uuid doesn't match our client pipe,
1773 reject the bind_ack & set the transfer interface synt to all 0's,
1774 ver 0 (observed when NT5 attempts to bind to abstract interfaces
1776 Needed when adding entries to a DACL from NT5 - SK */
1778 if(check_bind_req(p, &hdr_rb.rpc_context[0].abstract, &hdr_rb.rpc_context[0].transfer[0],
1779 hdr_rb.rpc_context[0].context_id )) {
1780 init_rpc_hdr_ba(&hdr_ba,
1781 RPC_MAX_PDU_FRAG_LEN,
1782 RPC_MAX_PDU_FRAG_LEN,
1786 &hdr_rb.rpc_context[0].transfer[0]);
1788 /* Rejection reason: abstract syntax not supported */
1789 init_rpc_hdr_ba(&hdr_ba, RPC_MAX_PDU_FRAG_LEN,
1790 RPC_MAX_PDU_FRAG_LEN, assoc_gid,
1791 ack_pipe_name, 0x1, 0x2, 0x1,
1792 &null_ndr_syntax_id);
1793 p->pipe_bound = False;
1800 if(!smb_io_rpc_hdr_ba("", &hdr_ba, &out_hdr_ba, 0)) {
1801 DEBUG(0,("api_pipe_bind_req: marshalling of RPC_HDR_BA failed.\n"));
1806 * Check if this is an authenticated bind request.
1809 if (p->hdr.auth_len) {
1811 * Decode the authentication verifier.
1814 /* Work out any padding needed before the auth footer. */
1815 if ((RPC_HEADER_LEN + prs_offset(&out_hdr_ba)) % SERVER_NDR_PADDING_SIZE) {
1816 ss_padding_len = SERVER_NDR_PADDING_SIZE -
1817 ((RPC_HEADER_LEN + prs_offset(&out_hdr_ba)) % SERVER_NDR_PADDING_SIZE);
1818 DEBUG(10,("api_pipe_bind_req: auth pad_len = %u\n",
1819 (unsigned int)ss_padding_len ));
1822 /* Quick length check. Won't catch a bad auth footer,
1823 * prevents overrun. */
1825 if (p->hdr.frag_len < RPC_HEADER_LEN + RPC_HDR_AUTH_LEN + p->hdr.auth_len) {
1826 DEBUG(0,("api_pipe_bind_req: auth_len (%u) "
1827 "too long for fragment %u.\n",
1828 (unsigned int)p->hdr.auth_len,
1829 (unsigned int)p->hdr.frag_len ));
1833 /* Pull the auth header and the following data into a blob. */
1834 /* NB. The offset of the auth_header is relative to the *end*
1835 * of the packet, not the start. Also, the length of the
1836 * data in rpc_in_p is p->hdr.frag_len - RPC_HEADER_LEN,
1837 * as the RPC header isn't included in rpc_in_p. */
1838 if(!prs_set_offset(rpc_in_p,
1839 p->hdr.frag_len - RPC_HEADER_LEN -
1840 RPC_HDR_AUTH_LEN - p->hdr.auth_len)) {
1841 DEBUG(0,("api_pipe_bind_req: cannot move "
1843 (unsigned int)(p->hdr.frag_len -
1844 RPC_HDR_AUTH_LEN - p->hdr.auth_len) ));
1848 if(!smb_io_rpc_hdr_auth("", &auth_info, rpc_in_p, 0)) {
1849 DEBUG(0,("api_pipe_bind_req: unable to unmarshall RPC_HDR_AUTH struct.\n"));
1853 auth_type = auth_info.auth_type;
1855 /* Work out if we have to sign or seal etc. */
1856 switch (auth_info.auth_level) {
1857 case DCERPC_AUTH_LEVEL_INTEGRITY:
1858 p->auth.auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1860 case DCERPC_AUTH_LEVEL_PRIVACY:
1861 p->auth.auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1864 DEBUG(0,("api_pipe_bind_req: unexpected auth level (%u).\n",
1865 (unsigned int)auth_info.auth_level ));
1869 ZERO_STRUCT(auth_info);
1873 case DCERPC_AUTH_TYPE_NTLMSSP:
1874 if (!pipe_ntlmssp_auth_bind(p, rpc_in_p,
1875 ss_padding_len, &auth_info, &out_auth)) {
1881 case DCERPC_AUTH_TYPE_SCHANNEL:
1882 if (!pipe_schannel_auth_bind(p, rpc_in_p,
1883 ss_padding_len, &auth_info, &out_auth)) {
1888 case DCERPC_AUTH_TYPE_SPNEGO:
1889 if (!pipe_spnego_auth_bind_negotiate(p, rpc_in_p,
1890 ss_padding_len, &auth_info, &out_auth)) {
1895 case DCERPC_AUTH_TYPE_NONE:
1896 /* Unauthenticated bind request. */
1897 /* We're finished - no more packets. */
1898 p->auth.auth_type = PIPE_AUTH_TYPE_NONE;
1899 /* We must set the pipe auth_level here also. */
1900 p->auth.auth_level = DCERPC_AUTH_LEVEL_NONE;
1901 p->pipe_bound = True;
1902 /* The session key was initialized from the SMB
1903 * session in make_internal_rpc_pipe_p */
1908 DEBUG(0,("api_pipe_bind_req: unknown auth type %x requested.\n", auth_type ));
1912 * Create the header, now we know the length.
1915 if (prs_offset(&out_auth)) {
1916 auth_len = prs_offset(&out_auth) - RPC_HDR_AUTH_LEN;
1919 init_rpc_hdr(&p->hdr, DCERPC_PKT_BIND_ACK, DCERPC_PFC_FLAG_FIRST | DCERPC_PFC_FLAG_LAST,
1921 RPC_HEADER_LEN + prs_offset(&out_hdr_ba) +
1922 ss_padding_len + prs_offset(&out_auth),
1926 * Marshall the header into the outgoing PDU.
1929 if(!smb_io_rpc_hdr("", &p->hdr, &p->out_data.frag, 0)) {
1930 DEBUG(0,("api_pipe_bind_req: marshalling of RPC_HDR failed.\n"));
1935 * Now add the RPC_HDR_BA and any auth needed.
1938 if(!prs_append_prs_data(&p->out_data.frag, &out_hdr_ba)) {
1939 DEBUG(0,("api_pipe_bind_req: append of RPC_HDR_BA failed.\n"));
1944 if (ss_padding_len) {
1945 char pad[SERVER_NDR_PADDING_SIZE];
1946 memset(pad, '\0', SERVER_NDR_PADDING_SIZE);
1947 if (!prs_copy_data_in(&p->out_data.frag, pad,
1949 DEBUG(0,("api_pipe_bind_req: failed to add %u "
1950 "bytes of pad data.\n",
1951 (unsigned int)ss_padding_len));
1956 if (!prs_append_prs_data( &p->out_data.frag, &out_auth)) {
1957 DEBUG(0,("api_pipe_bind_req: append of auth info failed.\n"));
1963 * Setup the lengths for the initial reply.
1966 p->out_data.data_sent_length = 0;
1967 p->out_data.current_pdu_sent = 0;
1969 prs_mem_free(&out_hdr_ba);
1970 prs_mem_free(&out_auth);
1976 prs_mem_free(&p->out_data.frag);
1977 prs_mem_free(&out_hdr_ba);
1978 prs_mem_free(&out_auth);
1979 return setup_bind_nak(p);
1982 /****************************************************************************
1983 Deal with an alter context call. Can be third part of 3 leg auth request for
1985 ****************************************************************************/
1987 bool api_pipe_alter_context(pipes_struct *p, prs_struct *rpc_in_p)
1991 RPC_HDR_AUTH auth_info;
1993 fstring ack_pipe_name;
1994 prs_struct out_hdr_ba;
1995 prs_struct out_auth;
1997 uint32_t ss_padding_len = 0;
1999 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
2002 * Marshall directly into the outgoing PDU space. We
2003 * must do this as we need to set to the bind response
2004 * header and are never sending more than one PDU here.
2008 * Setup the memory to marshall the ba header, and the
2012 if(!prs_init(&out_hdr_ba, 1024, p->mem_ctx, MARSHALL)) {
2013 DEBUG(0,("api_pipe_alter_context: malloc out_hdr_ba failed.\n"));
2014 prs_mem_free(&p->out_data.frag);
2018 if(!prs_init(&out_auth, 1024, p->mem_ctx, MARSHALL)) {
2019 DEBUG(0,("api_pipe_alter_context: malloc out_auth failed.\n"));
2020 prs_mem_free(&p->out_data.frag);
2021 prs_mem_free(&out_hdr_ba);
2025 ZERO_STRUCT(hdr_rb);
2027 DEBUG(5,("api_pipe_alter_context: decode request. %d\n", __LINE__));
2029 /* decode the alter context request */
2030 if(!smb_io_rpc_hdr_rb("", &hdr_rb, rpc_in_p, 0)) {
2031 DEBUG(0,("api_pipe_alter_context: unable to unmarshall RPC_HDR_RB struct.\n"));
2035 /* secondary address CAN be NULL
2036 * as the specs say it's ignored.
2037 * It MUST be NULL to have the spoolss working.
2039 fstrcpy(ack_pipe_name,"");
2041 DEBUG(5,("api_pipe_alter_context: make response. %d\n", __LINE__));
2043 assoc_gid = hdr_rb.bba.assoc_gid ? hdr_rb.bba.assoc_gid : 0x53f0;
2046 * Create the bind response struct.
2049 /* If the requested abstract synt uuid doesn't match our client pipe,
2050 reject the bind_ack & set the transfer interface synt to all 0's,
2051 ver 0 (observed when NT5 attempts to bind to abstract interfaces
2053 Needed when adding entries to a DACL from NT5 - SK */
2055 if(check_bind_req(p, &hdr_rb.rpc_context[0].abstract, &hdr_rb.rpc_context[0].transfer[0],
2056 hdr_rb.rpc_context[0].context_id )) {
2057 init_rpc_hdr_ba(&hdr_ba,
2058 RPC_MAX_PDU_FRAG_LEN,
2059 RPC_MAX_PDU_FRAG_LEN,
2063 &hdr_rb.rpc_context[0].transfer[0]);
2065 /* Rejection reason: abstract syntax not supported */
2066 init_rpc_hdr_ba(&hdr_ba, RPC_MAX_PDU_FRAG_LEN,
2067 RPC_MAX_PDU_FRAG_LEN, assoc_gid,
2068 ack_pipe_name, 0x1, 0x2, 0x1,
2069 &null_ndr_syntax_id);
2070 p->pipe_bound = False;
2077 if(!smb_io_rpc_hdr_ba("", &hdr_ba, &out_hdr_ba, 0)) {
2078 DEBUG(0,("api_pipe_alter_context: marshalling of RPC_HDR_BA failed.\n"));
2084 * Check if this is an authenticated alter context request.
2087 if (p->hdr.auth_len != 0) {
2089 * Decode the authentication verifier.
2092 /* Work out any padding needed before the auth footer. */
2093 if ((RPC_HEADER_LEN + prs_offset(&out_hdr_ba)) % SERVER_NDR_PADDING_SIZE) {
2094 ss_padding_len = SERVER_NDR_PADDING_SIZE -
2095 ((RPC_HEADER_LEN + prs_offset(&out_hdr_ba)) % SERVER_NDR_PADDING_SIZE);
2096 DEBUG(10,("api_pipe_alter_context: auth pad_len = %u\n",
2097 (unsigned int)ss_padding_len ));
2100 /* Quick length check. Won't catch a bad auth footer,
2101 * prevents overrun. */
2103 if (p->hdr.frag_len < RPC_HEADER_LEN + RPC_HDR_AUTH_LEN + p->hdr.auth_len) {
2104 DEBUG(0,("api_pipe_alter_context: auth_len (%u) "
2105 "too long for fragment %u.\n",
2106 (unsigned int)p->hdr.auth_len,
2107 (unsigned int)p->hdr.frag_len ));
2111 /* Pull the auth header and the following data into a blob. */
2112 /* NB. The offset of the auth_header is relative to the *end*
2113 * of the packet, not the start. Also, the length of the
2114 * data in rpc_in_p is p->hdr.frag_len - RPC_HEADER_LEN,
2115 * as the RPC header isn't included in rpc_in_p. */
2116 if(!prs_set_offset(rpc_in_p,
2117 p->hdr.frag_len - RPC_HEADER_LEN -
2118 RPC_HDR_AUTH_LEN - p->hdr.auth_len)) {
2119 DEBUG(0,("api_alter_context: cannot move "
2121 (unsigned int)(p->hdr.frag_len -
2122 RPC_HDR_AUTH_LEN - p->hdr.auth_len) ));
2126 if(!smb_io_rpc_hdr_auth("", &auth_info, rpc_in_p, 0)) {
2127 DEBUG(0,("api_pipe_alter_context: unable to unmarshall RPC_HDR_AUTH struct.\n"));
2132 * Currently only the SPNEGO auth type uses the alter ctx
2133 * response in place of the NTLMSSP auth3 type.
2136 if (auth_info.auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
2137 /* We can only finish if the pipe is unbound. */
2138 if (!p->pipe_bound) {
2139 if (!pipe_spnego_auth_bind_continue(p, rpc_in_p,
2140 ss_padding_len, &auth_info, &out_auth)) {
2148 ZERO_STRUCT(auth_info);
2151 * Create the header, now we know the length.
2154 if (prs_offset(&out_auth)) {
2155 auth_len = prs_offset(&out_auth) - RPC_HDR_AUTH_LEN;
2158 init_rpc_hdr(&p->hdr, DCERPC_PKT_ALTER_RESP, DCERPC_PFC_FLAG_FIRST | DCERPC_PFC_FLAG_LAST,
2160 RPC_HEADER_LEN + prs_offset(&out_hdr_ba) + prs_offset(&out_auth),
2164 * Marshall the header into the outgoing PDU.
2167 if(!smb_io_rpc_hdr("", &p->hdr, &p->out_data.frag, 0)) {
2168 DEBUG(0,("api_pipe_alter_context: marshalling of RPC_HDR failed.\n"));
2173 * Now add the RPC_HDR_BA and any auth needed.
2176 if(!prs_append_prs_data(&p->out_data.frag, &out_hdr_ba)) {
2177 DEBUG(0,("api_pipe_alter_context: append of RPC_HDR_BA failed.\n"));
2182 if (ss_padding_len) {
2183 char pad[SERVER_NDR_PADDING_SIZE];
2184 memset(pad, '\0', SERVER_NDR_PADDING_SIZE);
2185 if (!prs_copy_data_in(&p->out_data.frag, pad,
2187 DEBUG(0,("api_pipe_alter_context: failed to add %u "
2188 "bytes of pad data.\n",
2189 (unsigned int)ss_padding_len));
2194 if (!prs_append_prs_data( &p->out_data.frag, &out_auth)) {
2195 DEBUG(0,("api_pipe_alter_context: append of auth info failed.\n"));
2201 * Setup the lengths for the initial reply.
2204 p->out_data.data_sent_length = 0;
2205 p->out_data.current_pdu_sent = 0;
2207 prs_mem_free(&out_hdr_ba);
2208 prs_mem_free(&out_auth);
2214 prs_mem_free(&p->out_data.frag);
2215 prs_mem_free(&out_hdr_ba);
2216 prs_mem_free(&out_auth);
2217 return setup_bind_nak(p);
2220 /****************************************************************************
2221 Deal with NTLMSSP sign & seal processing on an RPC request.
2222 ****************************************************************************/
2224 bool api_pipe_ntlmssp_auth_process(pipes_struct *p, prs_struct *rpc_in,
2225 uint32 *p_ss_padding_len, NTSTATUS *pstatus)
2227 RPC_HDR_AUTH auth_info;
2228 uint32 auth_len = p->hdr.auth_len;
2229 uint32 save_offset = prs_offset(rpc_in);
2230 AUTH_NTLMSSP_STATE *a = p->auth.a_u.auth_ntlmssp_state;
2231 unsigned char *data = NULL;
2233 unsigned char *full_packet_data = NULL;
2234 size_t full_packet_data_len;
2235 DATA_BLOB auth_blob;
2237 *pstatus = NT_STATUS_OK;
2239 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_NONE || p->auth.auth_level == DCERPC_AUTH_LEVEL_CONNECT) {
2244 *pstatus = NT_STATUS_INVALID_PARAMETER;
2248 /* Ensure there's enough data for an authenticated request. */
2249 if (RPC_HEADER_LEN + RPC_HDR_REQ_LEN + RPC_HDR_AUTH_LEN
2250 + auth_len > p->hdr.frag_len) {
2251 DEBUG(0,("api_pipe_ntlmssp_auth_process: auth_len %u is too large.\n",
2252 (unsigned int)auth_len ));
2253 *pstatus = NT_STATUS_INVALID_PARAMETER;
2258 * We need the full packet data + length (minus auth stuff) as well as the packet data + length
2259 * after the RPC header.
2260 * We need to pass in the full packet (minus auth len) to the NTLMSSP sign and check seal
2261 * functions as NTLMv2 checks the rpc headers also.
2262 * Both of these values include any auth_pad_len bytes.
2265 data = (unsigned char *)(prs_data_p(rpc_in) + RPC_HDR_REQ_LEN);
2266 data_len = (size_t)(p->hdr.frag_len - RPC_HEADER_LEN - RPC_HDR_REQ_LEN - RPC_HDR_AUTH_LEN - auth_len);
2268 full_packet_data = p->in_data.current_in_pdu;
2269 full_packet_data_len = p->hdr.frag_len - auth_len;
2271 /* Pull the auth header and the following data into a blob. */
2272 /* NB. The offset of the auth_header is relative to the *end*
2273 * of the packet, not the start. Also, the length of the
2274 * data in rpc_in_p is p->hdr.frag_len - RPC_HEADER_LEN,
2275 * as the RPC header isn't included in rpc_in_p. */
2276 if(!prs_set_offset(rpc_in,
2277 p->hdr.frag_len - RPC_HEADER_LEN -
2278 RPC_HDR_AUTH_LEN - auth_len)) {
2279 DEBUG(0,("api_pipe_ntlmssp_auth_process: cannot move "
2281 (unsigned int)(p->hdr.frag_len -
2282 RPC_HDR_AUTH_LEN - auth_len) ));
2283 *pstatus = NT_STATUS_INVALID_PARAMETER;
2287 if(!smb_io_rpc_hdr_auth("hdr_auth", &auth_info, rpc_in, 0)) {
2288 DEBUG(0,("api_pipe_ntlmssp_auth_process: failed to "
2289 "unmarshall RPC_HDR_AUTH.\n"));
2290 *pstatus = NT_STATUS_INVALID_PARAMETER;
2294 /* Ensure auth_pad_len fits into the packet. */
2295 if (RPC_HEADER_LEN + RPC_HDR_REQ_LEN + auth_info.auth_pad_len +
2296 RPC_HDR_AUTH_LEN + auth_len > p->hdr.frag_len) {
2297 DEBUG(0,("api_pipe_ntlmssp_auth_process: auth_info.auth_pad_len "
2298 "too large (%u), auth_len (%u), frag_len = (%u).\n",
2299 (unsigned int)auth_info.auth_pad_len,
2300 (unsigned int)auth_len,
2301 (unsigned int)p->hdr.frag_len ));
2302 *pstatus = NT_STATUS_INVALID_PARAMETER;
2306 auth_blob.data = (unsigned char *)prs_data_p(rpc_in) + prs_offset(rpc_in);
2307 auth_blob.length = auth_len;
2309 switch (p->auth.auth_level) {
2310 case DCERPC_AUTH_LEVEL_PRIVACY:
2311 /* Data is encrypted. */
2312 *pstatus = ntlmssp_unseal_packet(a->ntlmssp_state,
2315 full_packet_data_len,
2317 if (!NT_STATUS_IS_OK(*pstatus)) {
2321 case DCERPC_AUTH_LEVEL_INTEGRITY:
2322 /* Data is signed. */
2323 *pstatus = ntlmssp_check_packet(a->ntlmssp_state,
2326 full_packet_data_len,
2328 if (!NT_STATUS_IS_OK(*pstatus)) {
2333 *pstatus = NT_STATUS_INVALID_PARAMETER;
2338 * Return the current pointer to the data offset.
2341 if(!prs_set_offset(rpc_in, save_offset)) {
2342 DEBUG(0,("api_pipe_auth_process: failed to set offset back to %u\n",
2343 (unsigned int)save_offset ));
2344 *pstatus = NT_STATUS_INVALID_PARAMETER;
2349 * Remember the padding length. We must remove it from the real data
2350 * stream once the sign/seal is done.
2353 *p_ss_padding_len = auth_info.auth_pad_len;
2358 /****************************************************************************
2359 Deal with schannel processing on an RPC request.
2360 ****************************************************************************/
2362 bool api_pipe_schannel_process(pipes_struct *p, prs_struct *rpc_in, uint32 *p_ss_padding_len)
2366 uint32 save_offset = prs_offset(rpc_in);
2367 RPC_HDR_AUTH auth_info;
2372 auth_len = p->hdr.auth_len;
2374 if (auth_len < RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN ||
2375 auth_len > RPC_HEADER_LEN +
2379 DEBUG(0,("Incorrect auth_len %u.\n", (unsigned int)auth_len ));
2384 * The following is that length of the data we must verify or unseal.
2385 * This doesn't include the RPC headers or the auth_len or the RPC_HDR_AUTH_LEN
2386 * preceeding the auth_data, but does include the auth_pad_len bytes.
2389 if (p->hdr.frag_len < RPC_HEADER_LEN + RPC_HDR_REQ_LEN + RPC_HDR_AUTH_LEN + auth_len) {
2390 DEBUG(0,("Incorrect frag %u, auth %u.\n",
2391 (unsigned int)p->hdr.frag_len,
2392 (unsigned int)auth_len ));
2396 data_len = p->hdr.frag_len - RPC_HEADER_LEN - RPC_HDR_REQ_LEN -
2397 RPC_HDR_AUTH_LEN - auth_len;
2399 DEBUG(5,("data %d auth %d\n", data_len, auth_len));
2401 /* Pull the auth header and the following data into a blob. */
2402 /* NB. The offset of the auth_header is relative to the *end*
2403 * of the packet, not the start. Also, the length of the
2404 * data in rpc_in_p is p->hdr.frag_len - RPC_HEADER_LEN,
2405 * as the RPC header isn't included in rpc_in_p. */
2406 if(!prs_set_offset(rpc_in,
2407 p->hdr.frag_len - RPC_HEADER_LEN -
2408 RPC_HDR_AUTH_LEN - auth_len)) {
2409 DEBUG(0,("api_pipe_schannel_process: cannot move "
2411 (unsigned int)(p->hdr.frag_len -
2412 RPC_HDR_AUTH_LEN - auth_len) ));
2416 if(!smb_io_rpc_hdr_auth("hdr_auth", &auth_info, rpc_in, 0)) {
2417 DEBUG(0,("api_pipe_schannel_process: failed to "
2418 "unmarshall RPC_HDR_AUTH.\n"));
2422 /* Ensure auth_pad_len fits into the packet. */
2423 if (RPC_HEADER_LEN + RPC_HDR_REQ_LEN + auth_info.auth_pad_len +
2424 RPC_HDR_AUTH_LEN + auth_len > p->hdr.frag_len) {
2425 DEBUG(0,("api_pipe_schannel_process: auth_info.auth_pad_len "
2426 "too large (%u), auth_len (%u), frag_len = (%u).\n",
2427 (unsigned int)auth_info.auth_pad_len,
2428 (unsigned int)auth_len,
2429 (unsigned int)p->hdr.frag_len ));
2433 if (auth_info.auth_type != DCERPC_AUTH_TYPE_SCHANNEL) {
2434 DEBUG(0,("Invalid auth info %d on schannel\n",
2435 auth_info.auth_type));
2439 blob = data_blob_const(prs_data_p(rpc_in) + prs_offset(rpc_in), auth_len);
2441 if (DEBUGLEVEL >= 10) {
2442 dump_NL_AUTH_SIGNATURE(talloc_tos(), &blob);
2445 data = (uint8_t *)prs_data_p(rpc_in)+RPC_HDR_REQ_LEN;
2447 switch (auth_info.auth_level) {
2448 case DCERPC_AUTH_LEVEL_PRIVACY:
2449 status = netsec_incoming_packet(p->auth.a_u.schannel_auth,
2456 case DCERPC_AUTH_LEVEL_INTEGRITY:
2457 status = netsec_incoming_packet(p->auth.a_u.schannel_auth,
2465 status = NT_STATUS_INTERNAL_ERROR;
2469 if (!NT_STATUS_IS_OK(status)) {
2470 DEBUG(0,("failed to unseal packet: %s\n", nt_errstr(status)));
2475 * Return the current pointer to the data offset.
2478 if(!prs_set_offset(rpc_in, save_offset)) {
2479 DEBUG(0,("failed to set offset back to %u\n",
2480 (unsigned int)save_offset ));
2485 * Remember the padding length. We must remove it from the real data
2486 * stream once the sign/seal is done.
2489 *p_ss_padding_len = auth_info.auth_pad_len;
2494 /****************************************************************************
2495 Find the set of RPC functions associated with this context_id
2496 ****************************************************************************/
2498 static PIPE_RPC_FNS* find_pipe_fns_by_context( PIPE_RPC_FNS *list, uint32 context_id )
2500 PIPE_RPC_FNS *fns = NULL;
2503 DEBUG(0,("find_pipe_fns_by_context: ERROR! No context list for pipe!\n"));
2507 for (fns=list; fns; fns=fns->next ) {
2508 if ( fns->context_id == context_id )
2514 /****************************************************************************
2516 ****************************************************************************/
2518 void free_pipe_rpc_context( PIPE_RPC_FNS *list )
2520 PIPE_RPC_FNS *tmp = list;
2532 static bool api_rpcTNP(pipes_struct *p,
2533 const struct api_struct *api_rpc_cmds, int n_cmds);
2535 /****************************************************************************
2536 Find the correct RPC function to call for this request.
2537 If the pipe is authenticated then become the correct UNIX user
2538 before doing the call.
2539 ****************************************************************************/
2541 bool api_pipe_request(pipes_struct *p)
2544 bool changed_user = False;
2545 PIPE_RPC_FNS *pipe_fns;
2547 if (p->pipe_bound &&
2548 ((p->auth.auth_type == PIPE_AUTH_TYPE_NTLMSSP) ||
2549 (p->auth.auth_type == PIPE_AUTH_TYPE_SPNEGO_NTLMSSP))) {
2550 if(!become_authenticated_pipe_user(p)) {
2551 prs_mem_free(&p->out_data.rdata);
2554 changed_user = True;
2557 DEBUG(5, ("Requested \\PIPE\\%s\n",
2558 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
2560 /* get the set of RPC functions for this context */
2562 pipe_fns = find_pipe_fns_by_context(p->contexts, p->hdr_req.context_id);
2565 TALLOC_CTX *frame = talloc_stackframe();
2566 ret = api_rpcTNP(p, pipe_fns->cmds, pipe_fns->n_cmds);
2570 DEBUG(0,("api_pipe_request: No rpc function table associated with context [%d] on pipe [%s]\n",
2571 p->hdr_req.context_id,
2572 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
2576 unbecome_authenticated_pipe_user();
2582 /*******************************************************************
2583 Calls the underlying RPC function for a named pipe.
2584 ********************************************************************/
2586 static bool api_rpcTNP(pipes_struct *p,
2587 const struct api_struct *api_rpc_cmds, int n_cmds)
2590 uint32 offset1, offset2;
2592 /* interpret the command */
2593 DEBUG(4,("api_rpcTNP: %s op 0x%x - ",
2594 get_pipe_name_from_syntax(talloc_tos(), &p->syntax),
2597 if (DEBUGLEVEL >= 50) {
2599 slprintf(name, sizeof(name)-1, "in_%s",
2600 get_pipe_name_from_syntax(talloc_tos(), &p->syntax));
2601 prs_dump(name, p->hdr_req.opnum, &p->in_data.data);
2604 for (fn_num = 0; fn_num < n_cmds; fn_num++) {
2605 if (api_rpc_cmds[fn_num].opnum == p->hdr_req.opnum && api_rpc_cmds[fn_num].fn != NULL) {
2606 DEBUG(3,("api_rpcTNP: rpc command: %s\n", api_rpc_cmds[fn_num].name));
2611 if (fn_num == n_cmds) {
2613 * For an unknown RPC just return a fault PDU but
2614 * return True to allow RPC's on the pipe to continue
2615 * and not put the pipe into fault state. JRA.
2617 DEBUG(4, ("unknown\n"));
2618 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
2622 offset1 = prs_offset(&p->out_data.rdata);
2624 DEBUG(6, ("api_rpc_cmds[%d].fn == %p\n",
2625 fn_num, api_rpc_cmds[fn_num].fn));
2626 /* do the actual command */
2627 if(!api_rpc_cmds[fn_num].fn(p)) {
2628 DEBUG(0,("api_rpcTNP: %s: %s failed.\n",
2629 get_pipe_name_from_syntax(talloc_tos(), &p->syntax),
2630 api_rpc_cmds[fn_num].name));
2631 prs_mem_free(&p->out_data.rdata);
2635 if (p->bad_handle_fault_state) {
2636 DEBUG(4,("api_rpcTNP: bad handle fault return.\n"));
2637 p->bad_handle_fault_state = False;
2638 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_CONTEXT_MISMATCH));
2642 if (p->rng_fault_state) {
2643 DEBUG(4, ("api_rpcTNP: rng fault return\n"));
2644 p->rng_fault_state = False;
2645 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
2649 offset2 = prs_offset(&p->out_data.rdata);
2650 prs_set_offset(&p->out_data.rdata, offset1);
2651 if (DEBUGLEVEL >= 50) {
2653 slprintf(name, sizeof(name)-1, "out_%s",
2654 get_pipe_name_from_syntax(talloc_tos(), &p->syntax));
2655 prs_dump(name, p->hdr_req.opnum, &p->out_data.rdata);
2657 prs_set_offset(&p->out_data.rdata, offset2);
2659 DEBUG(5,("api_rpcTNP: called %s successfully\n",
2660 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
2662 /* Check for buffer underflow in rpc parsing */
2664 if ((DEBUGLEVEL >= 10) &&
2665 (prs_offset(&p->in_data.data) != prs_data_size(&p->in_data.data))) {
2666 size_t data_len = prs_data_size(&p->in_data.data) - prs_offset(&p->in_data.data);
2667 char *data = (char *)SMB_MALLOC(data_len);
2669 DEBUG(10, ("api_rpcTNP: rpc input buffer underflow (parse error?)\n"));
2671 prs_uint8s(False, "", &p->in_data.data, 0, (unsigned char *)data, (uint32)data_len);