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)
62 RPC_HDR_RESP hdr_resp;
63 uint32 ss_padding_len = 0;
64 uint32 data_space_available;
69 RPC_HDR_AUTH auth_info;
70 uint8 auth_type, auth_level;
71 struct 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 /* Space available - not including padding. */
112 data_space_available = RPC_MAX_PDU_FRAG_LEN - RPC_HEADER_LEN -
113 RPC_HDR_RESP_LEN - RPC_HDR_AUTH_LEN - NTLMSSP_SIG_SIZE;
116 * The amount we send is the minimum of the available
117 * space and the amount left to send.
120 data_len = MIN(data_len_left, data_space_available);
122 /* Work out any padding alignment requirements. */
123 if ((RPC_HEADER_LEN + RPC_HDR_RESP_LEN + data_len) % SERVER_NDR_PADDING_SIZE) {
124 ss_padding_len = SERVER_NDR_PADDING_SIZE -
125 ((RPC_HEADER_LEN + RPC_HDR_RESP_LEN + data_len) % SERVER_NDR_PADDING_SIZE);
126 DEBUG(10,("create_next_pdu_ntlmssp: adding sign/seal padding of %u\n",
128 /* If we're over filling the packet, we need to make space
129 * for the padding at the end of the data. */
130 if (data_len + ss_padding_len > data_space_available) {
131 data_len -= SERVER_NDR_PADDING_SIZE;
136 * Set up the alloc hint. This should be the data left to
140 hdr_resp.alloc_hint = data_len_left;
143 * Work out if this PDU will be the last.
146 if(p->out_data.data_sent_length + data_len >= prs_offset(&p->out_data.rdata)) {
147 p->hdr.flags |= DCERPC_PFC_FLAG_LAST;
151 * Set up the header lengths.
154 p->hdr.frag_len = RPC_HEADER_LEN + RPC_HDR_RESP_LEN +
155 data_len + ss_padding_len +
156 RPC_HDR_AUTH_LEN + NTLMSSP_SIG_SIZE;
157 p->hdr.auth_len = NTLMSSP_SIG_SIZE;
161 * Init the parse struct to point at the outgoing
165 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
167 /* Store the header in the data stream. */
168 if(!smb_io_rpc_hdr("hdr", &p->hdr, &p->out_data.frag, 0)) {
169 DEBUG(0,("create_next_pdu_ntlmssp: failed to marshall RPC_HDR.\n"));
170 prs_mem_free(&p->out_data.frag);
174 if(!smb_io_rpc_hdr_resp("resp", &hdr_resp, &p->out_data.frag, 0)) {
175 DEBUG(0,("create_next_pdu_ntlmssp: failed to marshall RPC_HDR_RESP.\n"));
176 prs_mem_free(&p->out_data.frag);
180 /* Copy the data into the PDU. */
182 if(!prs_append_some_prs_data(&p->out_data.frag, &p->out_data.rdata,
183 p->out_data.data_sent_length, data_len)) {
184 DEBUG(0,("create_next_pdu_ntlmssp: failed to copy %u bytes of data.\n", (unsigned int)data_len));
185 prs_mem_free(&p->out_data.frag);
189 /* Copy the sign/seal padding data. */
190 if (ss_padding_len) {
191 char pad[SERVER_NDR_PADDING_SIZE];
193 memset(pad, '\0', SERVER_NDR_PADDING_SIZE);
194 if (!prs_copy_data_in(&p->out_data.frag, pad,
196 DEBUG(0,("create_next_pdu_ntlmssp: failed to add %u bytes of pad data.\n",
197 (unsigned int)ss_padding_len));
198 prs_mem_free(&p->out_data.frag);
204 /* Now write out the auth header and null blob. */
205 if (p->auth.auth_type == PIPE_AUTH_TYPE_NTLMSSP) {
206 auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
208 auth_type = DCERPC_AUTH_TYPE_SPNEGO;
210 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
211 auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
213 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
216 init_rpc_hdr_auth(&auth_info, auth_type, auth_level, ss_padding_len, 1 /* context id. */);
218 if (!smb_io_rpc_hdr_auth("hdr_auth", &auth_info,
219 &p->out_data.frag, 0)) {
220 DEBUG(0,("create_next_pdu_ntlmssp: failed to marshall RPC_HDR_AUTH.\n"));
221 prs_mem_free(&p->out_data.frag);
225 /* Generate the sign blob. */
227 frame = talloc_stackframe();
228 switch (p->auth.auth_level) {
229 case DCERPC_AUTH_LEVEL_PRIVACY:
230 /* Data portion is encrypted. */
231 status = auth_ntlmssp_seal_packet(
233 (uint8_t *)prs_data_p(&p->out_data.frag)
234 + RPC_HEADER_LEN + RPC_HDR_RESP_LEN,
235 data_len + ss_padding_len,
236 (unsigned char *)prs_data_p(&p->out_data.frag),
237 (size_t)prs_offset(&p->out_data.frag),
239 if (!NT_STATUS_IS_OK(status)) {
241 prs_mem_free(&p->out_data.frag);
245 case DCERPC_AUTH_LEVEL_INTEGRITY:
246 /* Data is signed. */
247 status = auth_ntlmssp_sign_packet(
249 (unsigned char *)prs_data_p(&p->out_data.frag)
250 + RPC_HEADER_LEN + RPC_HDR_RESP_LEN,
251 data_len + ss_padding_len,
252 (unsigned char *)prs_data_p(&p->out_data.frag),
253 (size_t)prs_offset(&p->out_data.frag),
255 if (!NT_STATUS_IS_OK(status)) {
257 prs_mem_free(&p->out_data.frag);
263 prs_mem_free(&p->out_data.frag);
267 /* Append the auth blob. */
268 if (!prs_copy_data_in(&p->out_data.frag, (char *)auth_blob.data,
270 DEBUG(0,("create_next_pdu_ntlmssp: failed to add %u bytes auth blob.\n",
271 (unsigned int)NTLMSSP_SIG_SIZE));
273 prs_mem_free(&p->out_data.frag);
279 * Setup the counts for this PDU.
282 p->out_data.data_sent_length += data_len;
283 p->out_data.current_pdu_sent = 0;
288 /*******************************************************************
289 Generate the next PDU to be returned from the data in p->rdata.
290 Return an schannel authenticated fragment.
291 ********************************************************************/
293 static bool create_next_pdu_schannel(pipes_struct *p)
295 RPC_HDR_RESP hdr_resp;
296 uint32 ss_padding_len = 0;
298 uint32 data_space_available;
299 uint32 data_len_left;
304 * If we're in the fault state, keep returning fault PDU's until
305 * the pipe gets closed. JRA.
309 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
313 memset((char *)&hdr_resp, '\0', sizeof(hdr_resp));
315 /* Change the incoming request header to a response. */
316 p->hdr.pkt_type = DCERPC_PKT_RESPONSE;
318 /* Set up rpc header flags. */
319 if (p->out_data.data_sent_length == 0) {
320 p->hdr.flags = DCERPC_PFC_FLAG_FIRST;
326 * Work out how much we can fit in a single PDU.
329 data_len_left = prs_offset(&p->out_data.rdata) - p->out_data.data_sent_length;
332 * Ensure there really is data left to send.
336 DEBUG(0,("create_next_pdu_schannel: no data left to send !\n"));
340 /* Space available - not including padding. */
341 data_space_available = RPC_MAX_PDU_FRAG_LEN - RPC_HEADER_LEN
342 - RPC_HDR_RESP_LEN - RPC_HDR_AUTH_LEN
343 - RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN;
346 * The amount we send is the minimum of the available
347 * space and the amount left to send.
350 data_len = MIN(data_len_left, data_space_available);
352 /* Work out any padding alignment requirements. */
353 if ((RPC_HEADER_LEN + RPC_HDR_RESP_LEN + data_len) % SERVER_NDR_PADDING_SIZE) {
354 ss_padding_len = SERVER_NDR_PADDING_SIZE -
355 ((RPC_HEADER_LEN + RPC_HDR_RESP_LEN + data_len) % SERVER_NDR_PADDING_SIZE);
356 DEBUG(10,("create_next_pdu_schannel: adding sign/seal padding of %u\n",
358 /* If we're over filling the packet, we need to make space
359 * for the padding at the end of the data. */
360 if (data_len + ss_padding_len > data_space_available) {
361 data_len -= SERVER_NDR_PADDING_SIZE;
366 * Set up the alloc hint. This should be the data left to
370 hdr_resp.alloc_hint = data_len_left;
373 * Work out if this PDU will be the last.
376 if(p->out_data.data_sent_length + data_len >= prs_offset(&p->out_data.rdata)) {
377 p->hdr.flags |= DCERPC_PFC_FLAG_LAST;
380 p->hdr.frag_len = RPC_HEADER_LEN + RPC_HDR_RESP_LEN + data_len + ss_padding_len +
381 RPC_HDR_AUTH_LEN + RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN;
382 p->hdr.auth_len = RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN;
385 * Init the parse struct to point at the outgoing
389 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
391 /* Store the header in the data stream. */
392 if(!smb_io_rpc_hdr("hdr", &p->hdr, &p->out_data.frag, 0)) {
393 DEBUG(0,("create_next_pdu_schannel: failed to marshall RPC_HDR.\n"));
394 prs_mem_free(&p->out_data.frag);
398 if(!smb_io_rpc_hdr_resp("resp", &hdr_resp, &p->out_data.frag, 0)) {
399 DEBUG(0,("create_next_pdu_schannel: failed to marshall RPC_HDR_RESP.\n"));
400 prs_mem_free(&p->out_data.frag);
404 /* Store the current offset. */
405 data_pos = prs_offset(&p->out_data.frag);
407 /* Copy the data into the PDU. */
409 if(!prs_append_some_prs_data(&p->out_data.frag, &p->out_data.rdata,
410 p->out_data.data_sent_length, data_len)) {
411 DEBUG(0,("create_next_pdu_schannel: failed to copy %u bytes of data.\n", (unsigned int)data_len));
412 prs_mem_free(&p->out_data.frag);
416 /* Copy the sign/seal padding data. */
417 if (ss_padding_len) {
418 char pad[SERVER_NDR_PADDING_SIZE];
419 memset(pad, '\0', SERVER_NDR_PADDING_SIZE);
420 if (!prs_copy_data_in(&p->out_data.frag, pad,
422 DEBUG(0,("create_next_pdu_schannel: failed to add %u bytes of pad data.\n", (unsigned int)ss_padding_len));
423 prs_mem_free(&p->out_data.frag);
430 * Schannel processing.
432 RPC_HDR_AUTH auth_info;
436 /* Check it's the type of reply we were expecting to decode */
438 init_rpc_hdr_auth(&auth_info,
439 DCERPC_AUTH_TYPE_SCHANNEL,
440 p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY ?
441 DCERPC_AUTH_LEVEL_PRIVACY : DCERPC_AUTH_LEVEL_INTEGRITY,
444 if (!smb_io_rpc_hdr_auth("hdr_auth", &auth_info,
445 &p->out_data.frag, 0)) {
446 DEBUG(0,("create_next_pdu_schannel: failed to marshall RPC_HDR_AUTH.\n"));
447 prs_mem_free(&p->out_data.frag);
451 data = (uint8_t *)prs_data_p(&p->out_data.frag) + data_pos;
453 switch (p->auth.auth_level) {
454 case DCERPC_AUTH_LEVEL_PRIVACY:
455 status = netsec_outgoing_packet(p->auth.a_u.schannel_auth,
459 data_len + ss_padding_len,
462 case DCERPC_AUTH_LEVEL_INTEGRITY:
463 status = netsec_outgoing_packet(p->auth.a_u.schannel_auth,
467 data_len + ss_padding_len,
471 status = NT_STATUS_INTERNAL_ERROR;
475 if (!NT_STATUS_IS_OK(status)) {
476 DEBUG(0,("create_next_pdu_schannel: failed to process packet: %s\n",
478 prs_mem_free(&p->out_data.frag);
482 /* Finally marshall the blob. */
484 if (DEBUGLEVEL >= 10) {
485 dump_NL_AUTH_SIGNATURE(talloc_tos(), &blob);
488 if (!prs_copy_data_in(&p->out_data.frag, (const char *)blob.data, blob.length)) {
489 prs_mem_free(&p->out_data.frag);
495 * Setup the counts for this PDU.
498 p->out_data.data_sent_length += data_len;
499 p->out_data.current_pdu_sent = 0;
504 /*******************************************************************
505 Generate the next PDU to be returned from the data in p->rdata.
506 No authentication done.
507 ********************************************************************/
509 static bool create_next_pdu_noauth(pipes_struct *p)
511 RPC_HDR_RESP hdr_resp;
513 uint32 data_space_available;
514 uint32 data_len_left;
517 * If we're in the fault state, keep returning fault PDU's until
518 * the pipe gets closed. JRA.
522 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
526 memset((char *)&hdr_resp, '\0', sizeof(hdr_resp));
528 /* Change the incoming request header to a response. */
529 p->hdr.pkt_type = DCERPC_PKT_RESPONSE;
531 /* Set up rpc header flags. */
532 if (p->out_data.data_sent_length == 0) {
533 p->hdr.flags = DCERPC_PFC_FLAG_FIRST;
539 * Work out how much we can fit in a single PDU.
542 data_len_left = prs_offset(&p->out_data.rdata) - p->out_data.data_sent_length;
545 * Ensure there really is data left to send.
549 DEBUG(0,("create_next_pdu_noath: no data left to send !\n"));
553 data_space_available = RPC_MAX_PDU_FRAG_LEN - RPC_HEADER_LEN
557 * The amount we send is the minimum of the available
558 * space and the amount left to send.
561 data_len = MIN(data_len_left, data_space_available);
564 * Set up the alloc hint. This should be the data left to
568 hdr_resp.alloc_hint = data_len_left;
571 * Work out if this PDU will be the last.
574 if(p->out_data.data_sent_length + data_len >= prs_offset(&p->out_data.rdata)) {
575 p->hdr.flags |= DCERPC_PFC_FLAG_LAST;
579 * Set up the header lengths.
582 p->hdr.frag_len = RPC_HEADER_LEN + RPC_HDR_RESP_LEN + data_len;
586 * Init the parse struct to point at the outgoing
590 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
592 /* Store the header in the data stream. */
593 if(!smb_io_rpc_hdr("hdr", &p->hdr, &p->out_data.frag, 0)) {
594 DEBUG(0,("create_next_pdu_noath: failed to marshall RPC_HDR.\n"));
595 prs_mem_free(&p->out_data.frag);
599 if(!smb_io_rpc_hdr_resp("resp", &hdr_resp, &p->out_data.frag, 0)) {
600 DEBUG(0,("create_next_pdu_noath: failed to marshall RPC_HDR_RESP.\n"));
601 prs_mem_free(&p->out_data.frag);
605 /* Copy the data into the PDU. */
607 if(!prs_append_some_prs_data(&p->out_data.frag, &p->out_data.rdata,
608 p->out_data.data_sent_length, data_len)) {
609 DEBUG(0,("create_next_pdu_noauth: failed to copy %u bytes of data.\n", (unsigned int)data_len));
610 prs_mem_free(&p->out_data.frag);
615 * Setup the counts for this PDU.
618 p->out_data.data_sent_length += data_len;
619 p->out_data.current_pdu_sent = 0;
624 /*******************************************************************
625 Generate the next PDU to be returned from the data in p->rdata.
626 ********************************************************************/
628 bool create_next_pdu(pipes_struct *p)
630 switch(p->auth.auth_level) {
631 case DCERPC_AUTH_LEVEL_NONE:
632 case DCERPC_AUTH_LEVEL_CONNECT:
633 /* This is incorrect for auth level connect. Fixme. JRA */
634 return create_next_pdu_noauth(p);
637 switch(p->auth.auth_type) {
638 case PIPE_AUTH_TYPE_NTLMSSP:
639 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
640 return create_next_pdu_ntlmssp(p);
641 case PIPE_AUTH_TYPE_SCHANNEL:
642 return create_next_pdu_schannel(p);
648 DEBUG(0,("create_next_pdu: invalid internal auth level %u / type %u",
649 (unsigned int)p->auth.auth_level,
650 (unsigned int)p->auth.auth_type));
654 /*******************************************************************
655 Process an NTLMSSP authentication response.
656 If this function succeeds, the user has been authenticated
657 and their domain, name and calling workstation stored in
659 *******************************************************************/
661 static bool pipe_ntlmssp_verify_final(pipes_struct *p, DATA_BLOB *p_resp_blob)
663 DATA_BLOB session_key, reply;
665 struct auth_ntlmssp_state *a = p->auth.a_u.auth_ntlmssp_state;
668 DEBUG(5,("pipe_ntlmssp_verify_final: pipe %s checking user details\n",
669 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
673 /* this has to be done as root in order to verify the password */
675 status = auth_ntlmssp_update(a, *p_resp_blob, &reply);
678 /* Don't generate a reply. */
679 data_blob_free(&reply);
681 if (!NT_STATUS_IS_OK(status)) {
685 /* Finally - if the pipe negotiated integrity (sign) or privacy (seal)
686 ensure the underlying NTLMSSP flags are also set. If not we should
689 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_INTEGRITY) {
690 if (!auth_ntlmssp_negotiated_sign(a)) {
691 DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet integrity requested "
692 "but client declined signing.\n",
693 get_pipe_name_from_syntax(talloc_tos(),
698 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
699 if (!auth_ntlmssp_negotiated_seal(a)) {
700 DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet privacy requested "
701 "but client declined sealing.\n",
702 get_pipe_name_from_syntax(talloc_tos(),
708 DEBUG(5, ("pipe_ntlmssp_verify_final: OK: user: %s domain: %s "
710 auth_ntlmssp_get_username(a),
711 auth_ntlmssp_get_domain(a),
712 auth_ntlmssp_get_client(a)));
714 TALLOC_FREE(p->server_info);
716 p->server_info = auth_ntlmssp_server_info(p, a);
717 if (p->server_info == NULL) {
718 DEBUG(0, ("auth_ntlmssp_server_info failed to obtain the server info for authenticated user\n"));
722 if (p->server_info->ptok == NULL) {
723 DEBUG(1,("Error: Authmodule failed to provide nt_user_token\n"));
728 * We're an authenticated bind over smb, so the session key needs to
729 * be set to "SystemLibraryDTC". Weird, but this is what Windows
730 * does. See the RPC-SAMBA3SESSIONKEY.
733 session_key = generic_session_key();
734 if (session_key.data == NULL) {
738 ret = server_info_set_session_key(p->server_info, session_key);
740 data_blob_free(&session_key);
745 /*******************************************************************
746 This is the "stage3" NTLMSSP response after a bind request and reply.
747 *******************************************************************/
749 bool api_pipe_bind_auth3(pipes_struct *p, prs_struct *rpc_in_p)
751 RPC_HDR_AUTH auth_info;
754 uint32_t auth_len = p->hdr.auth_len;
758 DEBUG(5,("api_pipe_bind_auth3: decode request. %d\n", __LINE__));
761 DEBUG(0,("api_pipe_bind_auth3: No auth field sent !\n"));
765 /* 4 bytes padding. */
766 if (!prs_uint32("pad", rpc_in_p, 0, &pad)) {
767 DEBUG(0,("api_pipe_bind_auth3: unmarshall of 4 byte pad failed.\n"));
771 /* Ensure there's enough data for an authenticated request. */
772 if (RPC_HEADER_LEN + RPC_HDR_AUTH_LEN + auth_len >
774 DEBUG(0,("api_pipe_ntlmssp_auth_process: auth_len "
775 "%u is too large.\n",
776 (unsigned int)auth_len ));
781 * Decode the authentication verifier response.
784 /* Pull the auth header and the following data into a blob. */
785 /* NB. The offset of the auth_header is relative to the *end*
786 * of the packet, not the start. Also, the length of the
787 * data in rpc_in_p is p->hdr.frag_len - RPC_HEADER_LEN,
788 * as the RPC header isn't included in rpc_in_p. */
789 if(!prs_set_offset(rpc_in_p,
790 p->hdr.frag_len - RPC_HEADER_LEN -
791 RPC_HDR_AUTH_LEN - auth_len)) {
792 DEBUG(0,("api_pipe_bind_auth3: cannot move "
794 (unsigned int)(p->hdr.frag_len -
795 RPC_HDR_AUTH_LEN - auth_len) ));
799 if(!smb_io_rpc_hdr_auth("hdr_auth", &auth_info, rpc_in_p, 0)) {
800 DEBUG(0,("api_pipe_bind_auth3: failed to "
801 "unmarshall RPC_HDR_AUTH.\n"));
805 /* We must NEVER look at auth_info->auth_pad_len here,
806 * as old Samba client code gets it wrong and sends it
810 if (auth_info.auth_type != DCERPC_AUTH_TYPE_NTLMSSP) {
811 DEBUG(0,("api_pipe_bind_auth3: incorrect auth type (%u).\n",
812 (unsigned int)auth_info.auth_type ));
816 blob = data_blob(NULL,p->hdr.auth_len);
818 if (!prs_copy_data_out((char *)blob.data, rpc_in_p, p->hdr.auth_len)) {
819 DEBUG(0,("api_pipe_bind_auth3: Failed to pull %u bytes - the response blob.\n",
820 (unsigned int)p->hdr.auth_len ));
825 * The following call actually checks the challenge/response data.
826 * for correctness against the given DOMAIN\user name.
829 if (!pipe_ntlmssp_verify_final(p, &blob)) {
833 data_blob_free(&blob);
835 p->pipe_bound = True;
841 data_blob_free(&blob);
842 free_pipe_ntlmssp_auth_data(&p->auth);
843 p->auth.a_u.auth_ntlmssp_state = NULL;
848 /*******************************************************************
849 Marshall a bind_nak pdu.
850 *******************************************************************/
852 static bool setup_bind_nak(pipes_struct *p)
855 union dcerpc_payload u;
858 /* Free any memory in the current return data buffer. */
859 prs_mem_free(&p->out_data.rdata);
862 * Marshall directly into the outgoing PDU space. We
863 * must do this as we need to set to the bind response
864 * header and are never sending more than one PDU here.
867 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
870 * Initialize a bind_nak header.
875 u.bind_nak.reject_reason = 0;
877 status = dcerpc_push_ncacn_packet(p->mem_ctx,
879 DCERPC_PFC_FLAG_FIRST | DCERPC_PFC_FLAG_LAST,
880 RPC_HEADER_LEN + sizeof(uint16_t) /* FIXME - gd */,
885 if (!NT_STATUS_IS_OK(status)) {
886 prs_mem_free(&p->out_data.frag);
890 if (!prs_copy_data_in(&p->out_data.frag,
891 (char *)blob.data, blob.length)) {
892 prs_mem_free(&p->out_data.frag);
896 p->out_data.data_sent_length = 0;
897 p->out_data.current_pdu_sent = 0;
899 if (p->auth.auth_data_free_func) {
900 (*p->auth.auth_data_free_func)(&p->auth);
902 p->auth.auth_level = DCERPC_AUTH_LEVEL_NONE;
903 p->auth.auth_type = PIPE_AUTH_TYPE_NONE;
904 p->pipe_bound = False;
909 /*******************************************************************
910 Marshall a fault pdu.
911 *******************************************************************/
913 bool setup_fault_pdu(pipes_struct *p, NTSTATUS fault_status)
916 union dcerpc_payload u;
919 /* Free any memory in the current return data buffer. */
920 prs_mem_free(&p->out_data.rdata);
923 * Marshall directly into the outgoing PDU space. We
924 * must do this as we need to set to the bind response
925 * header and are never sending more than one PDU here.
928 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
931 * Initialize a fault header.
936 u.fault.status = NT_STATUS_V(fault_status);
937 u.fault._pad = data_blob_talloc_zero(p->mem_ctx, 4);
939 status = dcerpc_push_ncacn_packet(p->mem_ctx,
941 DCERPC_PFC_FLAG_FIRST | DCERPC_PFC_FLAG_LAST | DCERPC_PFC_FLAG_DID_NOT_EXECUTE,
942 RPC_HEADER_LEN + RPC_HDR_RESP_LEN + RPC_HDR_FAULT_LEN /* FIXME - gd */,
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);