2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Almost completely rewritten by (C) Jeremy Allison 2005 - 2010
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 /* this module apparently provides an implementation of DCE/RPC over a
21 * named pipe (IPC$ connection using SMBtrans). details of DCE/RPC
22 * documentation are available (in on-line form) from the X-Open group.
24 * this module should provide a level of abstraction between SMB
25 * and DCE/RPC, while minimising the amount of mallocs, unnecessary
26 * data copies, and network traffic.
31 #include "srv_pipe_internal.h"
32 #include "../librpc/gen_ndr/ndr_schannel.h"
33 #include "../libcli/auth/schannel.h"
34 #include "../libcli/auth/spnego.h"
35 #include "../libcli/auth/ntlmssp.h"
38 #define DBGC_CLASS DBGC_RPC_SRV
40 static void free_pipe_ntlmssp_auth_data(struct pipe_auth_data *auth)
42 struct auth_ntlmssp_state *a = auth->a_u.auth_ntlmssp_state;
47 auth->a_u.auth_ntlmssp_state = NULL;
50 static DATA_BLOB generic_session_key(void)
52 return data_blob("SystemLibraryDTC", 16);
55 /*******************************************************************
56 Generate the next PDU to be returned from the data in p->rdata.
58 ********************************************************************/
60 static bool create_next_pdu_ntlmssp(pipes_struct *p)
64 uint32 ss_padding_len = 0;
65 uint32 data_space_available;
69 DATA_BLOB auth_blob = data_blob_null;
70 uint8 auth_type, auth_level;
71 struct auth_ntlmssp_state *a = p->auth.a_u.auth_ntlmssp_state;
72 union dcerpc_payload u;
76 * If we're in the fault state, keep returning fault PDU's until
77 * the pipe gets closed. JRA.
81 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
85 ZERO_STRUCT(u.response);
87 /* Set up rpc header flags. */
88 if (p->out_data.data_sent_length == 0) {
89 hdr_flags = DCERPC_PFC_FLAG_FIRST;
95 * Work out how much we can fit in a single PDU.
98 data_len_left = p->out_data.rdata.length - p->out_data.data_sent_length;
101 * Ensure there really is data left to send.
105 DEBUG(0,("create_next_pdu_ntlmssp: no data left to send !\n"));
109 /* Space available - not including padding. */
110 data_space_available = RPC_MAX_PDU_FRAG_LEN - RPC_HEADER_LEN -
111 RPC_HDR_RESP_LEN - RPC_HDR_AUTH_LEN - NTLMSSP_SIG_SIZE;
114 * The amount we send is the minimum of the available
115 * space and the amount left to send.
118 data_len = MIN(data_len_left, data_space_available);
120 /* Work out any padding alignment requirements. */
121 if ((RPC_HEADER_LEN + RPC_HDR_RESP_LEN + data_len) % SERVER_NDR_PADDING_SIZE) {
122 ss_padding_len = SERVER_NDR_PADDING_SIZE -
123 ((RPC_HEADER_LEN + RPC_HDR_RESP_LEN + data_len) % SERVER_NDR_PADDING_SIZE);
124 DEBUG(10,("create_next_pdu_ntlmssp: adding sign/seal padding of %u\n",
126 /* If we're over filling the packet, we need to make space
127 * for the padding at the end of the data. */
128 if (data_len + ss_padding_len > data_space_available) {
129 data_len -= SERVER_NDR_PADDING_SIZE;
134 * Set up the alloc hint. This should be the data left to
138 u.response.alloc_hint = data_len_left;
141 * Work out if this PDU will be the last.
143 if (p->out_data.data_sent_length + data_len >=
144 p->out_data.rdata.length) {
145 hdr_flags |= DCERPC_PFC_FLAG_LAST;
149 * Init the parse struct to point at the outgoing
152 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
154 /* Set the data into the PDU. */
155 u.response.stub_and_verifier =
156 data_blob_const(p->out_data.rdata.data, data_len);
158 status = dcerpc_push_ncacn_packet(
159 prs_get_mem_context(&p->out_data.frag),
166 if (!NT_STATUS_IS_OK(status)) {
167 DEBUG(0, ("Failed to marshall RPC Header.\n"));
168 prs_mem_free(&p->out_data.frag);
172 /* Set the proper length on the pdu */
173 dcerpc_set_frag_length(&blob, blob.length +
178 /* Store the packet in the data stream. */
179 if (!prs_copy_data_in(&p->out_data.frag,
180 (char *)blob.data, blob.length)) {
181 DEBUG(0, ("Out of memory.\n"));
182 prs_mem_free(&p->out_data.frag);
186 /* Append the sign/seal padding data. */
187 if (ss_padding_len) {
188 char pad[SERVER_NDR_PADDING_SIZE];
190 memset(pad, '\0', SERVER_NDR_PADDING_SIZE);
191 if (!prs_copy_data_in(&p->out_data.frag, pad,
193 DEBUG(0,("create_next_pdu_ntlmssp: failed to add %u bytes of pad data.\n",
194 (unsigned int)ss_padding_len));
195 prs_mem_free(&p->out_data.frag);
200 /* Now write out the auth header and null blob. */
201 if (p->auth.auth_type == PIPE_AUTH_TYPE_NTLMSSP) {
202 auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
204 auth_type = DCERPC_AUTH_TYPE_SPNEGO;
206 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
207 auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
209 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
212 /* auth_blob is intentionally null, it will be appended later */
213 status = dcerpc_push_dcerpc_auth(
214 prs_get_mem_context(&p->out_data.frag),
222 /* Store auth header in the data stream. */
223 if (!prs_copy_data_in(&p->out_data.frag,
224 (char *)blob.data, blob.length)) {
225 DEBUG(0, ("Out of memory.\n"));
226 prs_mem_free(&p->out_data.frag);
230 /* Generate the sign blob. */
232 frame = talloc_stackframe();
233 switch (p->auth.auth_level) {
234 case DCERPC_AUTH_LEVEL_PRIVACY:
235 /* Data portion is encrypted. */
236 status = auth_ntlmssp_seal_packet(
238 (uint8_t *)prs_data_p(&p->out_data.frag)
239 + RPC_HEADER_LEN + RPC_HDR_RESP_LEN,
240 data_len + ss_padding_len,
241 (unsigned char *)prs_data_p(&p->out_data.frag),
242 (size_t)prs_offset(&p->out_data.frag),
244 if (!NT_STATUS_IS_OK(status)) {
246 prs_mem_free(&p->out_data.frag);
250 case DCERPC_AUTH_LEVEL_INTEGRITY:
251 /* Data is signed. */
252 status = auth_ntlmssp_sign_packet(
254 (unsigned char *)prs_data_p(&p->out_data.frag)
255 + RPC_HEADER_LEN + RPC_HDR_RESP_LEN,
256 data_len + ss_padding_len,
257 (unsigned char *)prs_data_p(&p->out_data.frag),
258 (size_t)prs_offset(&p->out_data.frag),
260 if (!NT_STATUS_IS_OK(status)) {
262 prs_mem_free(&p->out_data.frag);
268 prs_mem_free(&p->out_data.frag);
272 /* Finally append the auth blob. */
273 if (!prs_copy_data_in(&p->out_data.frag, (char *)auth_blob.data,
275 DEBUG(0, ("Failed to add %u bytes auth blob.\n",
276 (unsigned int)NTLMSSP_SIG_SIZE));
278 prs_mem_free(&p->out_data.frag);
284 * Setup the counts for this PDU.
287 p->out_data.data_sent_length += data_len;
288 p->out_data.current_pdu_sent = 0;
293 /*******************************************************************
294 Generate the next PDU to be returned from the data in p->rdata.
295 Return an schannel authenticated fragment.
296 ********************************************************************/
298 static bool create_next_pdu_schannel(pipes_struct *p)
302 uint32 ss_padding_len = 0;
304 uint32 data_space_available;
305 uint32 data_len_left;
308 union dcerpc_payload u;
309 DATA_BLOB auth_blob = data_blob_null;
312 * If we're in the fault state, keep returning fault PDU's until
313 * the pipe gets closed. JRA.
317 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
321 ZERO_STRUCT(u.response);
323 /* Set up rpc header flags. */
324 if (p->out_data.data_sent_length == 0) {
325 hdr_flags = DCERPC_PFC_FLAG_FIRST;
331 * Work out how much we can fit in a single PDU.
334 data_len_left = p->out_data.rdata.length - p->out_data.data_sent_length;
337 * Ensure there really is data left to send.
341 DEBUG(0,("create_next_pdu_schannel: no data left to send !\n"));
345 /* Space available - not including padding. */
346 data_space_available = RPC_MAX_PDU_FRAG_LEN - RPC_HEADER_LEN
347 - RPC_HDR_RESP_LEN - RPC_HDR_AUTH_LEN
348 - RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN;
351 * The amount we send is the minimum of the available
352 * space and the amount left to send.
355 data_len = MIN(data_len_left, data_space_available);
357 /* Work out any padding alignment requirements. */
358 if ((RPC_HEADER_LEN + RPC_HDR_RESP_LEN + data_len) % SERVER_NDR_PADDING_SIZE) {
359 ss_padding_len = SERVER_NDR_PADDING_SIZE -
360 ((RPC_HEADER_LEN + RPC_HDR_RESP_LEN + data_len) % SERVER_NDR_PADDING_SIZE);
361 DEBUG(10,("create_next_pdu_schannel: adding sign/seal padding of %u\n",
363 /* If we're over filling the packet, we need to make space
364 * for the padding at the end of the data. */
365 if (data_len + ss_padding_len > data_space_available) {
366 data_len -= SERVER_NDR_PADDING_SIZE;
371 * Set up the alloc hint. This should be the data left to
375 u.response.alloc_hint = data_len_left;
378 * Work out if this PDU will be the last.
380 if (p->out_data.data_sent_length + data_len >=
381 p->out_data.rdata.length) {
382 hdr_flags |= DCERPC_PFC_FLAG_LAST;
386 * Init the parse struct to point at the outgoing
389 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
391 /* Set the data into the PDU. */
392 u.response.stub_and_verifier =
393 data_blob_const(p->out_data.rdata.data +
394 p->out_data.data_sent_length, data_len);
396 status = dcerpc_push_ncacn_packet(
397 prs_get_mem_context(&p->out_data.frag),
400 RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN,
404 if (!NT_STATUS_IS_OK(status)) {
405 DEBUG(0, ("Failed to marshall RPC Header.\n"));
406 prs_mem_free(&p->out_data.frag);
410 /* Store the data offset. */
411 data_pos = blob.length - data_len;
413 /* Set the proper length on the pdu */
414 dcerpc_set_frag_length(&blob, blob.length +
417 RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN);
419 /* Store the packet in the data stream. */
420 if (!prs_copy_data_in(&p->out_data.frag,
421 (char *)blob.data, blob.length)) {
422 DEBUG(0, ("Out of memory.\n"));
423 prs_mem_free(&p->out_data.frag);
427 /* Append the sign/seal padding data. */
428 if (ss_padding_len) {
429 char pad[SERVER_NDR_PADDING_SIZE];
430 memset(pad, '\0', SERVER_NDR_PADDING_SIZE);
431 if (!prs_copy_data_in(&p->out_data.frag, pad,
433 DEBUG(0,("create_next_pdu_schannel: failed to add %u bytes of pad data.\n", (unsigned int)ss_padding_len));
434 prs_mem_free(&p->out_data.frag);
439 /* auth_blob is intentionally null, it will be appended later */
440 status = dcerpc_push_dcerpc_auth(
441 prs_get_mem_context(&p->out_data.frag),
442 DCERPC_AUTH_TYPE_SCHANNEL,
449 /* Store auth header in the data stream. */
450 if (!prs_copy_data_in(&p->out_data.frag,
451 (char *)blob.data, blob.length)) {
452 DEBUG(0, ("Out of memory.\n"));
453 prs_mem_free(&p->out_data.frag);
458 * Schannel processing.
461 blob = data_blob_const(prs_data_p(&p->out_data.frag) + data_pos,
462 data_len + ss_padding_len);
464 switch (p->auth.auth_level) {
465 case DCERPC_AUTH_LEVEL_PRIVACY:
466 status = netsec_outgoing_packet(p->auth.a_u.schannel_auth,
473 case DCERPC_AUTH_LEVEL_INTEGRITY:
474 status = netsec_outgoing_packet(p->auth.a_u.schannel_auth,
482 status = NT_STATUS_INTERNAL_ERROR;
486 if (!NT_STATUS_IS_OK(status)) {
487 DEBUG(0,("create_next_pdu_schannel: failed to process packet: %s\n",
489 prs_mem_free(&p->out_data.frag);
493 /* Finally marshall the blob. */
495 if (DEBUGLEVEL >= 10) {
496 dump_NL_AUTH_SIGNATURE(talloc_tos(), &auth_blob);
499 if (!prs_copy_data_in(&p->out_data.frag,
500 (char *)auth_blob.data, auth_blob.length)) {
501 prs_mem_free(&p->out_data.frag);
506 * Setup the counts for this PDU.
509 p->out_data.data_sent_length += data_len;
510 p->out_data.current_pdu_sent = 0;
515 /*******************************************************************
516 Generate the next PDU to be returned from the data in p->rdata.
517 No authentication done.
518 ********************************************************************/
520 static bool create_next_pdu_noauth(pipes_struct *p)
526 uint32 data_space_available;
527 uint32 data_len_left;
528 union dcerpc_payload u;
531 * If we're in the fault state, keep returning fault PDU's until
532 * the pipe gets closed. JRA.
536 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
540 ZERO_STRUCT(u.response);
542 /* Set up rpc header flags. */
543 if (p->out_data.data_sent_length == 0) {
544 hdr_flags = DCERPC_PFC_FLAG_FIRST;
550 * Work out how much we can fit in a single PDU.
553 data_len_left = p->out_data.rdata.length - p->out_data.data_sent_length;
556 * Ensure there really is data left to send.
560 DEBUG(0,("create_next_pdu_noath: no data left to send !\n"));
564 data_space_available = RPC_MAX_PDU_FRAG_LEN - RPC_HEADER_LEN
568 * The amount we send is the minimum of the available
569 * space and the amount left to send.
572 data_len = MIN(data_len_left, data_space_available);
575 * Set up the alloc hint. This should be the data left to
579 u.response.alloc_hint = data_len_left;
582 * Work out if this PDU will be the last.
584 if(p->out_data.data_sent_length + data_len >= p->out_data.rdata.length) {
585 hdr_flags |= DCERPC_PFC_FLAG_LAST;
589 * Init the parse struct to point at the outgoing
592 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
594 /* Set the data into the PDU. */
595 u.response.stub_and_verifier =
596 data_blob_const(p->out_data.rdata.data +
597 p->out_data.data_sent_length, data_len);
599 status = dcerpc_push_ncacn_packet(
600 prs_get_mem_context(&p->out_data.frag),
607 if (!NT_STATUS_IS_OK(status)) {
608 DEBUG(0, ("Failed to marshall RPC Header.\n"));
609 prs_mem_free(&p->out_data.frag);
613 /* Store the packet in the data stream. */
614 if (!prs_copy_data_in(&p->out_data.frag,
615 (char *)blob.data, blob.length)) {
616 DEBUG(0, ("Out of memory.\n"));
617 prs_mem_free(&p->out_data.frag);
622 * Setup the counts for this PDU.
625 p->out_data.data_sent_length += data_len;
626 p->out_data.current_pdu_sent = 0;
631 /*******************************************************************
632 Generate the next PDU to be returned from the data in p->rdata.
633 ********************************************************************/
635 bool create_next_pdu(pipes_struct *p)
637 switch(p->auth.auth_level) {
638 case DCERPC_AUTH_LEVEL_NONE:
639 case DCERPC_AUTH_LEVEL_CONNECT:
640 /* This is incorrect for auth level connect. Fixme. JRA */
641 return create_next_pdu_noauth(p);
644 switch(p->auth.auth_type) {
645 case PIPE_AUTH_TYPE_NTLMSSP:
646 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
647 return create_next_pdu_ntlmssp(p);
648 case PIPE_AUTH_TYPE_SCHANNEL:
649 return create_next_pdu_schannel(p);
655 DEBUG(0,("create_next_pdu: invalid internal auth level %u / type %u",
656 (unsigned int)p->auth.auth_level,
657 (unsigned int)p->auth.auth_type));
661 /*******************************************************************
662 Process an NTLMSSP authentication response.
663 If this function succeeds, the user has been authenticated
664 and their domain, name and calling workstation stored in
666 *******************************************************************/
668 static bool pipe_ntlmssp_verify_final(pipes_struct *p, DATA_BLOB *p_resp_blob)
670 DATA_BLOB session_key, reply;
672 struct auth_ntlmssp_state *a = p->auth.a_u.auth_ntlmssp_state;
675 DEBUG(5,("pipe_ntlmssp_verify_final: pipe %s checking user details\n",
676 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
680 /* this has to be done as root in order to verify the password */
682 status = auth_ntlmssp_update(a, *p_resp_blob, &reply);
685 /* Don't generate a reply. */
686 data_blob_free(&reply);
688 if (!NT_STATUS_IS_OK(status)) {
692 /* Finally - if the pipe negotiated integrity (sign) or privacy (seal)
693 ensure the underlying NTLMSSP flags are also set. If not we should
696 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_INTEGRITY) {
697 if (!auth_ntlmssp_negotiated_sign(a)) {
698 DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet integrity requested "
699 "but client declined signing.\n",
700 get_pipe_name_from_syntax(talloc_tos(),
705 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
706 if (!auth_ntlmssp_negotiated_seal(a)) {
707 DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet privacy requested "
708 "but client declined sealing.\n",
709 get_pipe_name_from_syntax(talloc_tos(),
715 DEBUG(5, ("pipe_ntlmssp_verify_final: OK: user: %s domain: %s "
717 auth_ntlmssp_get_username(a),
718 auth_ntlmssp_get_domain(a),
719 auth_ntlmssp_get_client(a)));
721 TALLOC_FREE(p->server_info);
723 status = auth_ntlmssp_server_info(p, a, &p->server_info);
724 if (!NT_STATUS_IS_OK(status)) {
725 DEBUG(0, ("auth_ntlmssp_server_info failed to obtain the server info for authenticated user: %s\n",
730 if (p->server_info->ptok == NULL) {
731 DEBUG(1,("Error: Authmodule failed to provide nt_user_token\n"));
736 * We're an authenticated bind over smb, so the session key needs to
737 * be set to "SystemLibraryDTC". Weird, but this is what Windows
738 * does. See the RPC-SAMBA3SESSIONKEY.
741 session_key = generic_session_key();
742 if (session_key.data == NULL) {
746 ret = server_info_set_session_key(p->server_info, session_key);
748 data_blob_free(&session_key);
753 /*******************************************************************
754 This is the "stage3" NTLMSSP response after a bind request and reply.
755 *******************************************************************/
757 bool api_pipe_bind_auth3(pipes_struct *p, struct ncacn_packet *pkt)
759 struct dcerpc_auth auth_info;
760 uint32_t auth_len = pkt->auth_length;
763 DEBUG(5,("api_pipe_bind_auth3: decode request. %d\n", __LINE__));
766 DEBUG(0,("api_pipe_bind_auth3: No auth field sent !\n"));
770 /* Ensure there's enough data for an authenticated request. */
771 if (RPC_HEADER_LEN + RPC_HDR_AUTH_LEN + auth_len >
773 DEBUG(0,("api_pipe_ntlmssp_auth_process: auth_len "
774 "%u is too large.\n",
775 (unsigned int)auth_len ));
780 * Decode the authentication verifier response.
783 status = dcerpc_pull_dcerpc_auth(pkt,
784 &pkt->u.auth3.auth_info,
786 if (!NT_STATUS_IS_OK(status)) {
787 DEBUG(0, ("Failed to unmarshall dcerpc_auth.\n"));
791 /* We must NEVER look at auth_info->auth_pad_len here,
792 * as old Samba client code gets it wrong and sends it
796 if (auth_info.auth_type != DCERPC_AUTH_TYPE_NTLMSSP) {
797 DEBUG(0,("api_pipe_bind_auth3: incorrect auth type (%u).\n",
798 (unsigned int)auth_info.auth_type ));
803 * The following call actually checks the challenge/response data.
804 * for correctness against the given DOMAIN\user name.
807 if (!pipe_ntlmssp_verify_final(p, &auth_info.credentials)) {
811 p->pipe_bound = True;
817 free_pipe_ntlmssp_auth_data(&p->auth);
818 p->auth.a_u.auth_ntlmssp_state = NULL;
823 /*******************************************************************
824 Marshall a bind_nak pdu.
825 *******************************************************************/
827 static bool setup_bind_nak(pipes_struct *p, struct ncacn_packet *pkt)
830 union dcerpc_payload u;
833 /* Free any memory in the current return data buffer. */
834 data_blob_free(&p->out_data.rdata);
837 * Marshall directly into the outgoing PDU space. We
838 * must do this as we need to set to the bind response
839 * header and are never sending more than one PDU here.
842 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
845 * Initialize a bind_nak header.
850 u.bind_nak.reject_reason = 0;
852 status = dcerpc_push_ncacn_packet(p->mem_ctx,
854 DCERPC_PFC_FLAG_FIRST |
855 DCERPC_PFC_FLAG_LAST,
860 if (!NT_STATUS_IS_OK(status)) {
861 prs_mem_free(&p->out_data.frag);
865 if (!prs_copy_data_in(&p->out_data.frag,
866 (char *)blob.data, blob.length)) {
867 prs_mem_free(&p->out_data.frag);
871 p->out_data.data_sent_length = 0;
872 p->out_data.current_pdu_sent = 0;
874 if (p->auth.auth_data_free_func) {
875 (*p->auth.auth_data_free_func)(&p->auth);
877 p->auth.auth_level = DCERPC_AUTH_LEVEL_NONE;
878 p->auth.auth_type = PIPE_AUTH_TYPE_NONE;
879 p->pipe_bound = False;
884 /*******************************************************************
885 Marshall a fault pdu.
886 *******************************************************************/
888 bool setup_fault_pdu(pipes_struct *p, NTSTATUS fault_status)
891 union dcerpc_payload u;
894 /* Free any memory in the current return data buffer. */
895 data_blob_free(&p->out_data.rdata);
898 * Marshall directly into the outgoing PDU space. We
899 * must do this as we need to set to the bind response
900 * header and are never sending more than one PDU here.
903 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
906 * Initialize a fault header.
911 u.fault.status = NT_STATUS_V(fault_status);
912 u.fault._pad = data_blob_talloc_zero(p->mem_ctx, 4);
914 status = dcerpc_push_ncacn_packet(p->mem_ctx,
916 DCERPC_PFC_FLAG_FIRST |
917 DCERPC_PFC_FLAG_LAST |
918 DCERPC_PFC_FLAG_DID_NOT_EXECUTE,
923 if (!NT_STATUS_IS_OK(status)) {
924 prs_mem_free(&p->out_data.frag);
928 if (!prs_copy_data_in(&p->out_data.frag,
929 (char *)blob.data, blob.length)) {
930 prs_mem_free(&p->out_data.frag);
934 p->out_data.data_sent_length = 0;
935 p->out_data.current_pdu_sent = 0;
940 /*******************************************************************
941 Ensure a bind request has the correct abstract & transfer interface.
942 Used to reject unknown binds from Win2k.
943 *******************************************************************/
945 static bool check_bind_req(struct pipes_struct *p,
946 struct ndr_syntax_id* abstract,
947 struct ndr_syntax_id* transfer,
950 struct pipe_rpc_fns *context_fns;
952 DEBUG(3,("check_bind_req for %s\n",
953 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
955 /* we have to check all now since win2k introduced a new UUID on the lsaprpc pipe */
956 if (rpc_srv_pipe_exists_by_id(abstract) &&
957 ndr_syntax_id_equal(transfer, &ndr_transfer_syntax)) {
958 DEBUG(3, ("check_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
959 rpc_srv_get_pipe_cli_name(abstract),
960 rpc_srv_get_pipe_srv_name(abstract)));
965 context_fns = SMB_MALLOC_P(struct pipe_rpc_fns);
966 if (context_fns == NULL) {
967 DEBUG(0,("check_bind_req: malloc() failed!\n"));
971 context_fns->n_cmds = rpc_srv_get_pipe_num_cmds(abstract);
972 context_fns->cmds = rpc_srv_get_pipe_cmds(abstract);
973 context_fns->context_id = context_id;
975 /* add to the list of open contexts */
977 DLIST_ADD( p->contexts, context_fns );
983 * Is a named pipe known?
984 * @param[in] cli_filename The pipe name requested by the client
985 * @result Do we want to serve this?
987 bool is_known_pipename(const char *cli_filename, struct ndr_syntax_id *syntax)
989 const char *pipename = cli_filename;
992 if (strnequal(pipename, "\\PIPE\\", 6)) {
996 if (*pipename == '\\') {
1000 if (lp_disable_spoolss() && strequal(pipename, "spoolss")) {
1001 DEBUG(10, ("refusing spoolss access\n"));
1005 if (rpc_srv_get_pipe_interface_by_cli_name(pipename, syntax)) {
1009 status = smb_probe_module("rpc", pipename);
1010 if (!NT_STATUS_IS_OK(status)) {
1011 DEBUG(10, ("is_known_pipename: %s unknown\n", cli_filename));
1014 DEBUG(10, ("is_known_pipename: %s loaded dynamically\n", pipename));
1017 * Scan the list again for the interface id
1019 if (rpc_srv_get_pipe_interface_by_cli_name(pipename, syntax)) {
1023 DEBUG(10, ("is_known_pipename: pipe %s did not register itself!\n",
1029 /*******************************************************************
1030 Handle a SPNEGO krb5 bind auth.
1031 *******************************************************************/
1033 static bool pipe_spnego_auth_bind_kerberos(pipes_struct *p,
1034 TALLOC_CTX *mem_ctx,
1035 struct dcerpc_auth *pauth_info,
1036 DATA_BLOB *psecblob,
1037 DATA_BLOB *response)
1042 /*******************************************************************
1043 Handle the first part of a SPNEGO bind auth.
1044 *******************************************************************/
1046 static bool pipe_spnego_auth_bind_negotiate(pipes_struct *p,
1047 TALLOC_CTX *mem_ctx,
1048 struct dcerpc_auth *pauth_info,
1049 DATA_BLOB *response)
1053 char *OIDs[ASN1_MAX_OIDS];
1056 bool got_kerberos_mechanism = false;
1057 struct auth_ntlmssp_state *a = NULL;
1059 ZERO_STRUCT(secblob);
1062 if (pauth_info->credentials.data[0] != ASN1_APPLICATION(0)) {
1066 /* parse out the OIDs and the first sec blob */
1067 if (!parse_negTokenTarg(pauth_info->credentials, OIDs, &secblob)) {
1068 DEBUG(0,("pipe_spnego_auth_bind_negotiate: Failed to parse the security blob.\n"));
1072 if (strcmp(OID_KERBEROS5, OIDs[0]) == 0 || strcmp(OID_KERBEROS5_OLD, OIDs[0]) == 0) {
1073 got_kerberos_mechanism = true;
1076 for (i=0;OIDs[i];i++) {
1077 DEBUG(3,("pipe_spnego_auth_bind_negotiate: Got OID %s\n", OIDs[i]));
1078 TALLOC_FREE(OIDs[i]);
1080 DEBUG(3,("pipe_spnego_auth_bind_negotiate: Got secblob of size %lu\n", (unsigned long)secblob.length));
1082 if ( got_kerberos_mechanism && ((lp_security()==SEC_ADS) || USE_KERBEROS_KEYTAB) ) {
1084 ret = pipe_spnego_auth_bind_kerberos(p, mem_ctx, pauth_info,
1085 &secblob, response);
1086 data_blob_free(&secblob);
1090 if (p->auth.auth_type == PIPE_AUTH_TYPE_SPNEGO_NTLMSSP && p->auth.a_u.auth_ntlmssp_state) {
1091 /* Free any previous auth type. */
1092 free_pipe_ntlmssp_auth_data(&p->auth);
1095 if (!got_kerberos_mechanism) {
1096 /* Initialize the NTLM engine. */
1097 status = auth_ntlmssp_start(&a);
1098 if (!NT_STATUS_IS_OK(status)) {
1102 switch (pauth_info->auth_level) {
1103 case DCERPC_AUTH_LEVEL_INTEGRITY:
1104 auth_ntlmssp_want_sign(a);
1106 case DCERPC_AUTH_LEVEL_PRIVACY:
1107 auth_ntlmssp_want_seal(a);
1113 * Pass the first security blob of data to it.
1114 * This can return an error or NT_STATUS_MORE_PROCESSING_REQUIRED
1115 * which means we need another packet to complete the bind.
1118 status = auth_ntlmssp_update(a, secblob, &chal);
1120 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1121 DEBUG(3,("pipe_spnego_auth_bind_negotiate: auth_ntlmssp_update failed.\n"));
1125 /* Generate the response blob we need for step 2 of the bind. */
1126 *response = spnego_gen_auth_response(&chal, status, OID_NTLMSSP);
1129 * SPNEGO negotiate down to NTLMSSP. The subsequent
1130 * code to process follow-up packets is not complete
1133 *response = spnego_gen_auth_response(NULL,
1134 NT_STATUS_MORE_PROCESSING_REQUIRED,
1138 /* Make sure data is bound to the memctx, to be freed the caller */
1139 talloc_steal(mem_ctx, response->data);
1141 /* auth_pad_len will be handled by the caller */
1143 p->auth.a_u.auth_ntlmssp_state = a;
1144 p->auth.auth_data_free_func = &free_pipe_ntlmssp_auth_data;
1145 p->auth.auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1147 data_blob_free(&secblob);
1148 data_blob_free(&chal);
1150 /* We can't set pipe_bound True yet - we need an RPC_ALTER_CONTEXT response packet... */
1155 data_blob_free(&secblob);
1156 data_blob_free(&chal);
1158 p->auth.a_u.auth_ntlmssp_state = NULL;
1163 /*******************************************************************
1164 Handle the second part of a SPNEGO bind auth.
1165 *******************************************************************/
1167 static bool pipe_spnego_auth_bind_continue(pipes_struct *p,
1168 TALLOC_CTX *mem_ctx,
1169 struct dcerpc_auth *pauth_info,
1170 DATA_BLOB *response)
1172 DATA_BLOB auth_blob;
1173 DATA_BLOB auth_reply;
1174 struct auth_ntlmssp_state *a = p->auth.a_u.auth_ntlmssp_state;
1176 ZERO_STRUCT(auth_blob);
1177 ZERO_STRUCT(auth_reply);
1180 * NB. If we've negotiated down from krb5 to NTLMSSP we'll currently
1181 * fail here as 'a' == NULL.
1183 if (p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP || !a) {
1184 DEBUG(0,("pipe_spnego_auth_bind_continue: not in NTLMSSP auth state.\n"));
1188 if (pauth_info->credentials.data[0] != ASN1_CONTEXT(1)) {
1189 DEBUG(0,("pipe_spnego_auth_bind_continue: invalid SPNEGO blob type.\n"));
1193 if (!spnego_parse_auth(pauth_info->credentials, &auth_blob)) {
1194 DEBUG(0,("pipe_spnego_auth_bind_continue: invalid SPNEGO blob.\n"));
1199 * The following call actually checks the challenge/response data.
1200 * for correctness against the given DOMAIN\user name.
1203 if (!pipe_ntlmssp_verify_final(p, &auth_blob)) {
1207 data_blob_free(&auth_blob);
1209 /* Generate the spnego "accept completed" blob - no incoming data. */
1210 *response = spnego_gen_auth_response(&auth_reply, NT_STATUS_OK, OID_NTLMSSP);
1212 /* Make sure data is bound to the memctx, to be freed the caller */
1213 talloc_steal(mem_ctx, response->data);
1215 data_blob_free(&auth_reply);
1217 p->pipe_bound = True;
1223 data_blob_free(&auth_blob);
1224 data_blob_free(&auth_reply);
1226 free_pipe_ntlmssp_auth_data(&p->auth);
1227 p->auth.a_u.auth_ntlmssp_state = NULL;
1232 /*******************************************************************
1233 Handle an schannel bind auth.
1234 *******************************************************************/
1236 static bool pipe_schannel_auth_bind(pipes_struct *p,
1237 TALLOC_CTX *mem_ctx,
1238 struct dcerpc_auth *auth_info,
1239 DATA_BLOB *response)
1241 struct NL_AUTH_MESSAGE neg;
1242 struct NL_AUTH_MESSAGE reply;
1245 struct netlogon_creds_CredentialState *creds;
1246 DATA_BLOB session_key;
1247 enum ndr_err_code ndr_err;
1249 ndr_err = ndr_pull_struct_blob(
1250 &auth_info->credentials, mem_ctx, &neg,
1251 (ndr_pull_flags_fn_t)ndr_pull_NL_AUTH_MESSAGE);
1252 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1253 DEBUG(0,("pipe_schannel_auth_bind: Could not unmarshal SCHANNEL auth neg\n"));
1257 if (DEBUGLEVEL >= 10) {
1258 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &neg);
1261 if (!(neg.Flags & NL_FLAG_OEM_NETBIOS_COMPUTER_NAME)) {
1262 DEBUG(0,("pipe_schannel_auth_bind: Did not receive netbios computer name\n"));
1267 * The neg.oem_netbios_computer.a key here must match the remote computer name
1268 * given in the DOM_CLNT_SRV.uni_comp_name used on all netlogon pipe
1269 * operations that use credentials.
1273 status = schannel_get_creds_state(p, lp_private_dir(),
1274 neg.oem_netbios_computer.a, &creds);
1277 if (!NT_STATUS_IS_OK(status)) {
1278 DEBUG(0, ("pipe_schannel_auth_bind: Attempt to bind using schannel without successful serverauth2\n"));
1282 p->auth.a_u.schannel_auth = talloc(p, struct schannel_state);
1283 if (!p->auth.a_u.schannel_auth) {
1288 p->auth.a_u.schannel_auth->state = SCHANNEL_STATE_START;
1289 p->auth.a_u.schannel_auth->seq_num = 0;
1290 p->auth.a_u.schannel_auth->initiator = false;
1291 p->auth.a_u.schannel_auth->creds = creds;
1294 * JRA. Should we also copy the schannel session key into the pipe session key p->session_key
1295 * here ? We do that for NTLMSSP, but the session key is already set up from the vuser
1296 * struct of the person who opened the pipe. I need to test this further. JRA.
1298 * VL. As we are mapping this to guest set the generic key
1299 * "SystemLibraryDTC" key here. It's a bit difficult to test against
1300 * W2k3, as it does not allow schannel binds against SAMR and LSA
1304 session_key = generic_session_key();
1305 if (session_key.data == NULL) {
1306 DEBUG(0, ("pipe_schannel_auth_bind: Could not alloc session"
1311 ret = server_info_set_session_key(p->server_info, session_key);
1313 data_blob_free(&session_key);
1316 DEBUG(0, ("server_info_set_session_key failed\n"));
1320 /*** SCHANNEL verifier ***/
1322 reply.MessageType = NL_NEGOTIATE_RESPONSE;
1324 reply.Buffer.dummy = 5; /* ??? actually I don't think
1325 * this has any meaning
1328 ndr_err = ndr_push_struct_blob(response, mem_ctx, &reply,
1329 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
1330 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1331 DEBUG(0,("Failed to marshall NL_AUTH_MESSAGE.\n"));
1335 if (DEBUGLEVEL >= 10) {
1336 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &reply);
1339 DEBUG(10,("pipe_schannel_auth_bind: schannel auth: domain [%s] myname [%s]\n",
1340 neg.oem_netbios_domain.a, neg.oem_netbios_computer.a));
1342 /* We're finished with this bind - no more packets. */
1343 p->auth.auth_data_free_func = NULL;
1344 p->auth.auth_type = PIPE_AUTH_TYPE_SCHANNEL;
1346 p->pipe_bound = True;
1351 /*******************************************************************
1352 Handle an NTLMSSP bind auth.
1353 *******************************************************************/
1355 static bool pipe_ntlmssp_auth_bind(pipes_struct *p,
1356 TALLOC_CTX *mem_ctx,
1357 struct dcerpc_auth *auth_info,
1358 DATA_BLOB *response)
1361 struct auth_ntlmssp_state *a = NULL;
1363 if (strncmp((char *)auth_info->credentials.data, "NTLMSSP", 7) != 0) {
1364 DEBUG(0, ("Failed to read NTLMSSP in blob\n"));
1368 /* We have an NTLMSSP blob. */
1369 status = auth_ntlmssp_start(&a);
1370 if (!NT_STATUS_IS_OK(status)) {
1371 DEBUG(0,("pipe_ntlmssp_auth_bind: auth_ntlmssp_start failed: %s\n",
1372 nt_errstr(status) ));
1376 switch (auth_info->auth_level) {
1377 case DCERPC_AUTH_LEVEL_INTEGRITY:
1378 auth_ntlmssp_want_sign(a);
1380 case DCERPC_AUTH_LEVEL_PRIVACY:
1381 auth_ntlmssp_want_seal(a);
1387 status = auth_ntlmssp_update(a, auth_info->credentials, response);
1388 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1389 DEBUG(0,("pipe_ntlmssp_auth_bind: auth_ntlmssp_update failed: %s\n",
1390 nt_errstr(status) ));
1394 /* Make sure data is bound to the memctx, to be freed the caller */
1395 talloc_steal(mem_ctx, response->data);
1397 p->auth.a_u.auth_ntlmssp_state = a;
1398 p->auth.auth_data_free_func = &free_pipe_ntlmssp_auth_data;
1399 p->auth.auth_type = PIPE_AUTH_TYPE_NTLMSSP;
1401 DEBUG(10,("pipe_ntlmssp_auth_bind: NTLMSSP auth started\n"));
1403 /* We can't set pipe_bound True yet - we need an DCERPC_PKT_AUTH3 response packet... */
1408 free_pipe_ntlmssp_auth_data(&p->auth);
1409 p->auth.a_u.auth_ntlmssp_state = NULL;
1413 /*******************************************************************
1414 Respond to a pipe bind request.
1415 *******************************************************************/
1417 bool api_pipe_bind_req(pipes_struct *p, struct ncacn_packet *pkt)
1419 struct dcerpc_auth auth_info;
1421 unsigned int auth_type = DCERPC_AUTH_TYPE_NONE;
1423 struct ndr_syntax_id id;
1424 union dcerpc_payload u;
1425 struct dcerpc_ack_ctx bind_ack_ctx;
1426 DATA_BLOB auth_resp = data_blob_null;
1427 DATA_BLOB auth_blob = data_blob_null;
1428 DATA_BLOB blob = data_blob_null;
1431 /* No rebinds on a bound pipe - use alter context. */
1432 if (p->pipe_bound) {
1433 DEBUG(2,("api_pipe_bind_req: rejecting bind request on bound "
1435 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1436 return setup_bind_nak(p, pkt);
1439 if (pkt->u.bind.num_contexts == 0) {
1440 DEBUG(0, ("api_pipe_bind_req: no rpc contexts around\n"));
1445 * Try and find the correct pipe name to ensure
1446 * that this is a pipe name we support.
1448 id = pkt->u.bind.ctx_list[0].abstract_syntax;
1449 if (rpc_srv_pipe_exists_by_id(&id)) {
1450 DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
1451 rpc_srv_get_pipe_cli_name(&id),
1452 rpc_srv_get_pipe_srv_name(&id)));
1454 status = smb_probe_module(
1455 "rpc", get_pipe_name_from_syntax(
1457 &pkt->u.bind.ctx_list[0].abstract_syntax));
1459 if (NT_STATUS_IS_ERR(status)) {
1460 DEBUG(3,("api_pipe_bind_req: Unknown pipe name %s in bind request.\n",
1461 get_pipe_name_from_syntax(
1463 &pkt->u.bind.ctx_list[0].abstract_syntax)));
1465 return setup_bind_nak(p, pkt);
1468 if (rpc_srv_get_pipe_interface_by_cli_name(
1469 get_pipe_name_from_syntax(talloc_tos(),
1472 DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
1473 rpc_srv_get_pipe_cli_name(&id),
1474 rpc_srv_get_pipe_srv_name(&id)));
1476 DEBUG(0, ("module %s doesn't provide functions for "
1478 get_pipe_name_from_syntax(talloc_tos(),
1480 get_pipe_name_from_syntax(talloc_tos(),
1482 return setup_bind_nak(p, pkt);
1486 DEBUG(5,("api_pipe_bind_req: make response. %d\n", __LINE__));
1488 if (pkt->u.bind.assoc_group_id != 0) {
1489 assoc_gid = pkt->u.bind.assoc_group_id;
1495 * Marshall directly into the outgoing PDU space. We
1496 * must do this as we need to set to the bind response
1497 * header and are never sending more than one PDU here.
1500 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
1503 * Create the bind response struct.
1506 /* If the requested abstract synt uuid doesn't match our client pipe,
1507 reject the bind_ack & set the transfer interface synt to all 0's,
1508 ver 0 (observed when NT5 attempts to bind to abstract interfaces
1510 Needed when adding entries to a DACL from NT5 - SK */
1512 if (check_bind_req(p,
1513 &pkt->u.bind.ctx_list[0].abstract_syntax,
1514 &pkt->u.bind.ctx_list[0].transfer_syntaxes[0],
1515 pkt->u.bind.ctx_list[0].context_id)) {
1517 bind_ack_ctx.result = 0;
1518 bind_ack_ctx.reason = 0;
1519 bind_ack_ctx.syntax = pkt->u.bind.ctx_list[0].transfer_syntaxes[0];
1521 p->pipe_bound = False;
1522 /* Rejection reason: abstract syntax not supported */
1523 bind_ack_ctx.result = DCERPC_BIND_PROVIDER_REJECT;
1524 bind_ack_ctx.reason = DCERPC_BIND_REASON_ASYNTAX;
1525 bind_ack_ctx.syntax = null_ndr_syntax_id;
1529 * Check if this is an authenticated bind request.
1531 if (pkt->auth_length) {
1532 /* Quick length check. Won't catch a bad auth footer,
1533 * prevents overrun. */
1535 if (pkt->frag_length < RPC_HEADER_LEN +
1538 DEBUG(0,("api_pipe_bind_req: auth_len (%u) "
1539 "too long for fragment %u.\n",
1540 (unsigned int)pkt->auth_length,
1541 (unsigned int)pkt->frag_length));
1546 * Decode the authentication verifier.
1548 status = dcerpc_pull_dcerpc_auth(pkt,
1549 &pkt->u.bind.auth_info,
1551 if (!NT_STATUS_IS_OK(status)) {
1552 DEBUG(0, ("Unable to unmarshall dcerpc_auth.\n"));
1556 auth_type = auth_info.auth_type;
1558 /* Work out if we have to sign or seal etc. */
1559 switch (auth_info.auth_level) {
1560 case DCERPC_AUTH_LEVEL_INTEGRITY:
1561 p->auth.auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1563 case DCERPC_AUTH_LEVEL_PRIVACY:
1564 p->auth.auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1567 DEBUG(0, ("Unexpected auth level (%u).\n",
1568 (unsigned int)auth_info.auth_level ));
1572 switch (auth_type) {
1573 case DCERPC_AUTH_TYPE_NTLMSSP:
1574 if (!pipe_ntlmssp_auth_bind(p, pkt,
1575 &auth_info, &auth_resp)) {
1581 case DCERPC_AUTH_TYPE_SCHANNEL:
1582 if (!pipe_schannel_auth_bind(p, pkt,
1583 &auth_info, &auth_resp)) {
1588 case DCERPC_AUTH_TYPE_SPNEGO:
1589 if (!pipe_spnego_auth_bind_negotiate(p, pkt,
1590 &auth_info, &auth_resp)) {
1595 case DCERPC_AUTH_TYPE_NONE:
1599 DEBUG(0, ("Unknown auth type %x requested.\n", auth_type));
1604 if (auth_type == DCERPC_AUTH_TYPE_NONE) {
1605 /* Unauthenticated bind request. */
1606 /* We're finished - no more packets. */
1607 p->auth.auth_type = PIPE_AUTH_TYPE_NONE;
1608 /* We must set the pipe auth_level here also. */
1609 p->auth.auth_level = DCERPC_AUTH_LEVEL_NONE;
1610 p->pipe_bound = True;
1611 /* The session key was initialized from the SMB
1612 * session in make_internal_rpc_pipe_p */
1615 ZERO_STRUCT(u.bind_ack);
1616 u.bind_ack.max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
1617 u.bind_ack.max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
1618 u.bind_ack.assoc_group_id = assoc_gid;
1620 /* name has to be \PIPE\xxxxx */
1621 u.bind_ack.secondary_address =
1622 talloc_asprintf(pkt, "\\PIPE\\%s",
1623 rpc_srv_get_pipe_srv_name(&id));
1624 if (!u.bind_ack.secondary_address) {
1625 DEBUG(0, ("Out of memory!\n"));
1628 u.bind_ack.secondary_address_size =
1629 strlen(u.bind_ack.secondary_address) + 1;
1631 u.bind_ack.num_results = 1;
1632 u.bind_ack.ctx_list = &bind_ack_ctx;
1634 /* NOTE: We leave the auth_info empty so we can calculate the padding
1635 * later and then append the auth_info --simo */
1637 status = dcerpc_push_ncacn_packet(pkt, DCERPC_PKT_BIND_ACK,
1638 DCERPC_PFC_FLAG_FIRST |
1639 DCERPC_PFC_FLAG_LAST,
1643 if (!NT_STATUS_IS_OK(status)) {
1644 DEBUG(0, ("Failed to marshall bind_ack packet. (%s)\n",
1645 nt_errstr(status)));
1648 if (auth_resp.length) {
1650 /* Work out any padding needed before the auth footer. */
1651 pad_len = blob.length % SERVER_NDR_PADDING_SIZE;
1653 pad_len = SERVER_NDR_PADDING_SIZE - pad_len;
1654 DEBUG(10, ("auth pad_len = %u\n",
1655 (unsigned int)pad_len));
1658 status = dcerpc_push_dcerpc_auth(pkt,
1660 auth_info.auth_level,
1662 1, /* auth_context_id */
1665 if (!NT_STATUS_IS_OK(status)) {
1666 DEBUG(0, ("Marshalling of dcerpc_auth failed.\n"));
1671 /* Now that we have the auth len store it into the right place in
1672 * the dcerpc header */
1673 dcerpc_set_frag_length(&blob, blob.length + pad_len + auth_blob.length);
1675 /* And finally copy all bits in the output pdu */
1676 if (!prs_copy_data_in(&p->out_data.frag,
1677 (char *)blob.data, blob.length)) {
1678 DEBUG(0, ("Failed to copy data to output buffer.\n"));
1682 if (auth_blob.length) {
1684 char pad[SERVER_NDR_PADDING_SIZE];
1685 memset(pad, '\0', SERVER_NDR_PADDING_SIZE);
1686 if (!prs_copy_data_in(&p->out_data.frag, pad, pad_len)) {
1687 DEBUG(0, ("api_pipe_bind_req: failed to add "
1688 "%u bytes of pad data.\n",
1689 (unsigned int)pad_len));
1694 if (!prs_copy_data_in(&p->out_data.frag,
1695 (char *)auth_blob.data,
1696 auth_blob.length)) {
1697 DEBUG(0, ("Append of auth info failed.\n"));
1703 * Setup the lengths for the initial reply.
1706 p->out_data.data_sent_length = 0;
1707 p->out_data.current_pdu_sent = 0;
1709 TALLOC_FREE(auth_blob.data);
1710 TALLOC_FREE(blob.data);
1715 prs_mem_free(&p->out_data.frag);
1716 TALLOC_FREE(auth_blob.data);
1717 TALLOC_FREE(blob.data);
1718 return setup_bind_nak(p, pkt);
1721 /****************************************************************************
1722 Deal with an alter context call. Can be third part of 3 leg auth request for
1724 ****************************************************************************/
1726 bool api_pipe_alter_context(pipes_struct *p, struct ncacn_packet *pkt)
1728 struct dcerpc_auth auth_info;
1731 union dcerpc_payload u;
1732 struct dcerpc_ack_ctx bind_ack_ctx;
1733 DATA_BLOB auth_resp = data_blob_null;
1734 DATA_BLOB auth_blob = data_blob_null;
1735 DATA_BLOB blob = data_blob_null;
1738 DEBUG(5,("api_pipe_alter_context: make response. %d\n", __LINE__));
1740 if (pkt->u.bind.assoc_group_id != 0) {
1741 assoc_gid = pkt->u.bind.assoc_group_id;
1747 * Marshall directly into the outgoing PDU space. We
1748 * must do this as we need to set to the bind response
1749 * header and are never sending more than one PDU here.
1752 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
1755 * Create the bind response struct.
1758 /* If the requested abstract synt uuid doesn't match our client pipe,
1759 reject the bind_ack & set the transfer interface synt to all 0's,
1760 ver 0 (observed when NT5 attempts to bind to abstract interfaces
1762 Needed when adding entries to a DACL from NT5 - SK */
1764 if (check_bind_req(p,
1765 &pkt->u.bind.ctx_list[0].abstract_syntax,
1766 &pkt->u.bind.ctx_list[0].transfer_syntaxes[0],
1767 pkt->u.bind.ctx_list[0].context_id)) {
1769 bind_ack_ctx.result = 0;
1770 bind_ack_ctx.reason = 0;
1771 bind_ack_ctx.syntax = pkt->u.bind.ctx_list[0].transfer_syntaxes[0];
1773 p->pipe_bound = False;
1774 /* Rejection reason: abstract syntax not supported */
1775 bind_ack_ctx.result = DCERPC_BIND_PROVIDER_REJECT;
1776 bind_ack_ctx.reason = DCERPC_BIND_REASON_ASYNTAX;
1777 bind_ack_ctx.syntax = null_ndr_syntax_id;
1781 * Check if this is an authenticated alter context request.
1783 if (pkt->auth_length) {
1784 /* Quick length check. Won't catch a bad auth footer,
1785 * prevents overrun. */
1787 if (pkt->frag_length < RPC_HEADER_LEN +
1790 DEBUG(0,("api_pipe_alter_context: auth_len (%u) "
1791 "too long for fragment %u.\n",
1792 (unsigned int)pkt->auth_length,
1793 (unsigned int)pkt->frag_length ));
1797 status = dcerpc_pull_dcerpc_auth(pkt,
1798 &pkt->u.bind.auth_info,
1800 if (!NT_STATUS_IS_OK(status)) {
1801 DEBUG(0, ("Unable to unmarshall dcerpc_auth.\n"));
1807 * Currently only the SPNEGO auth type uses the alter ctx
1808 * response in place of the NTLMSSP auth3 type.
1811 if (auth_info.auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1812 /* We can only finish if the pipe is unbound. */
1813 if (!p->pipe_bound) {
1814 if (!pipe_spnego_auth_bind_continue(p, pkt,
1815 &auth_info, &auth_resp)) {
1825 ZERO_STRUCT(u.alter_resp);
1826 u.alter_resp.max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
1827 u.alter_resp.max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
1828 u.alter_resp.assoc_group_id = assoc_gid;
1830 /* secondary address CAN be NULL
1831 * as the specs say it's ignored.
1832 * It MUST be NULL to have the spoolss working.
1834 u.alter_resp.secondary_address = "";
1835 u.alter_resp.secondary_address_size = 1;
1837 u.alter_resp.num_results = 1;
1838 u.alter_resp.ctx_list = &bind_ack_ctx;
1840 /* NOTE: We leave the auth_info empty so we can calculate the padding
1841 * later and then append the auth_info --simo */
1843 status = dcerpc_push_ncacn_packet(pkt, DCERPC_PKT_ALTER_RESP,
1844 DCERPC_PFC_FLAG_FIRST |
1845 DCERPC_PFC_FLAG_LAST,
1849 if (!NT_STATUS_IS_OK(status)) {
1850 DEBUG(0, ("Failed to marshall bind_ack packet. (%s)\n",
1851 nt_errstr(status)));
1854 if (auth_resp.length) {
1856 /* Work out any padding needed before the auth footer. */
1857 pad_len = blob.length % SERVER_NDR_PADDING_SIZE;
1859 pad_len = SERVER_NDR_PADDING_SIZE - pad_len;
1860 DEBUG(10, ("auth pad_len = %u\n",
1861 (unsigned int)pad_len));
1864 status = dcerpc_push_dcerpc_auth(pkt,
1865 auth_info.auth_type,
1866 auth_info.auth_level,
1868 1, /* auth_context_id */
1871 if (!NT_STATUS_IS_OK(status)) {
1872 DEBUG(0, ("Marshalling of dcerpc_auth failed.\n"));
1877 /* Now that we have the auth len store it into the right place in
1878 * the dcerpc header */
1879 dcerpc_set_frag_length(&blob, blob.length + pad_len + auth_blob.length);
1881 /* And finally copy all bits in the output pdu */
1882 if (!prs_copy_data_in(&p->out_data.frag,
1883 (char *)blob.data, blob.length)) {
1884 DEBUG(0, ("Failed to copy data to output buffer.\n"));
1888 if (auth_resp.length) {
1890 char pad[SERVER_NDR_PADDING_SIZE];
1891 memset(pad, '\0', SERVER_NDR_PADDING_SIZE);
1892 if (!prs_copy_data_in(&p->out_data.frag, pad, pad_len)) {
1893 DEBUG(0, ("api_pipe_bind_req: failed to add "
1894 "%u bytes of pad data.\n",
1895 (unsigned int)pad_len));
1900 if (!prs_copy_data_in(&p->out_data.frag,
1901 (char *)auth_blob.data,
1902 auth_blob.length)) {
1903 DEBUG(0, ("Append of auth info failed.\n"));
1909 * Setup the lengths for the initial reply.
1912 p->out_data.data_sent_length = 0;
1913 p->out_data.current_pdu_sent = 0;
1915 TALLOC_FREE(auth_blob.data);
1916 TALLOC_FREE(blob.data);
1921 prs_mem_free(&p->out_data.frag);
1922 TALLOC_FREE(auth_blob.data);
1923 TALLOC_FREE(blob.data);
1924 return setup_bind_nak(p, pkt);
1927 /****************************************************************************
1928 Find the set of RPC functions associated with this context_id
1929 ****************************************************************************/
1931 static PIPE_RPC_FNS* find_pipe_fns_by_context( PIPE_RPC_FNS *list, uint32 context_id )
1933 PIPE_RPC_FNS *fns = NULL;
1936 DEBUG(0,("find_pipe_fns_by_context: ERROR! No context list for pipe!\n"));
1940 for (fns=list; fns; fns=fns->next ) {
1941 if ( fns->context_id == context_id )
1947 /****************************************************************************
1949 ****************************************************************************/
1951 void free_pipe_rpc_context( PIPE_RPC_FNS *list )
1953 PIPE_RPC_FNS *tmp = list;
1965 static bool api_rpcTNP(pipes_struct *p, struct ncacn_packet *pkt,
1966 const struct api_struct *api_rpc_cmds, int n_cmds);
1968 /****************************************************************************
1969 Find the correct RPC function to call for this request.
1970 If the pipe is authenticated then become the correct UNIX user
1971 before doing the call.
1972 ****************************************************************************/
1974 bool api_pipe_request(pipes_struct *p, struct ncacn_packet *pkt)
1977 bool changed_user = False;
1978 PIPE_RPC_FNS *pipe_fns;
1980 if (p->pipe_bound &&
1981 ((p->auth.auth_type == PIPE_AUTH_TYPE_NTLMSSP) ||
1982 (p->auth.auth_type == PIPE_AUTH_TYPE_SPNEGO_NTLMSSP))) {
1983 if(!become_authenticated_pipe_user(p)) {
1984 data_blob_free(&p->out_data.rdata);
1987 changed_user = True;
1990 DEBUG(5, ("Requested \\PIPE\\%s\n",
1991 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1993 /* get the set of RPC functions for this context */
1995 pipe_fns = find_pipe_fns_by_context(p->contexts,
1996 pkt->u.request.context_id);
1999 TALLOC_CTX *frame = talloc_stackframe();
2000 ret = api_rpcTNP(p, pkt, pipe_fns->cmds, pipe_fns->n_cmds);
2004 DEBUG(0, ("No rpc function table associated with context "
2005 "[%d] on pipe [%s]\n",
2006 pkt->u.request.context_id,
2007 get_pipe_name_from_syntax(talloc_tos(),
2012 unbecome_authenticated_pipe_user();
2018 /*******************************************************************
2019 Calls the underlying RPC function for a named pipe.
2020 ********************************************************************/
2022 static bool api_rpcTNP(pipes_struct *p, struct ncacn_packet *pkt,
2023 const struct api_struct *api_rpc_cmds, int n_cmds)
2028 /* interpret the command */
2029 DEBUG(4,("api_rpcTNP: %s op 0x%x - ",
2030 get_pipe_name_from_syntax(talloc_tos(), &p->syntax),
2031 pkt->u.request.opnum));
2033 if (DEBUGLEVEL >= 50) {
2035 slprintf(name, sizeof(name)-1, "in_%s",
2036 get_pipe_name_from_syntax(talloc_tos(), &p->syntax));
2037 prs_dump(name, pkt->u.request.opnum, &p->in_data.data);
2040 for (fn_num = 0; fn_num < n_cmds; fn_num++) {
2041 if (api_rpc_cmds[fn_num].opnum == pkt->u.request.opnum &&
2042 api_rpc_cmds[fn_num].fn != NULL) {
2043 DEBUG(3, ("api_rpcTNP: rpc command: %s\n",
2044 api_rpc_cmds[fn_num].name));
2049 if (fn_num == n_cmds) {
2051 * For an unknown RPC just return a fault PDU but
2052 * return True to allow RPC's on the pipe to continue
2053 * and not put the pipe into fault state. JRA.
2055 DEBUG(4, ("unknown\n"));
2056 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
2060 offset1 = p->out_data.rdata.length;
2062 DEBUG(6, ("api_rpc_cmds[%d].fn == %p\n",
2063 fn_num, api_rpc_cmds[fn_num].fn));
2064 /* do the actual command */
2065 if(!api_rpc_cmds[fn_num].fn(p)) {
2066 DEBUG(0,("api_rpcTNP: %s: %s failed.\n",
2067 get_pipe_name_from_syntax(talloc_tos(), &p->syntax),
2068 api_rpc_cmds[fn_num].name));
2069 data_blob_free(&p->out_data.rdata);
2073 if (p->bad_handle_fault_state) {
2074 DEBUG(4,("api_rpcTNP: bad handle fault return.\n"));
2075 p->bad_handle_fault_state = False;
2076 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_CONTEXT_MISMATCH));
2080 if (p->rng_fault_state) {
2081 DEBUG(4, ("api_rpcTNP: rng fault return\n"));
2082 p->rng_fault_state = False;
2083 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
2087 if (DEBUGLEVEL >= 50) {
2089 slprintf(name, sizeof(name)-1, "out_%s",
2090 get_pipe_name_from_syntax(talloc_tos(), &p->syntax));
2091 prs_dump_region(name, pkt->u.request.opnum,
2092 p->out_data.rdata.data,
2094 p->out_data.rdata.length);
2097 DEBUG(5,("api_rpcTNP: called %s successfully\n",
2098 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
2100 /* Check for buffer underflow in rpc parsing */
2102 if ((DEBUGLEVEL >= 10) &&
2103 (prs_offset(&p->in_data.data) != prs_data_size(&p->in_data.data))) {
2104 size_t data_len = prs_data_size(&p->in_data.data) - prs_offset(&p->in_data.data);
2105 char *data = (char *)SMB_MALLOC(data_len);
2107 DEBUG(10, ("api_rpcTNP: rpc input buffer underflow (parse error?)\n"));
2109 prs_uint8s(False, "", &p->in_data.data, 0, (unsigned char *)data, (uint32)data_len);