2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Almost completely rewritten by (C) Jeremy Allison 2005.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 /* this module apparently provides an implementation of DCE/RPC over a
21 * named pipe (IPC$ connection using SMBtrans). details of DCE/RPC
22 * documentation are available (in on-line form) from the X-Open group.
24 * this module should provide a level of abstraction between SMB
25 * and DCE/RPC, while minimising the amount of mallocs, unnecessary
26 * data copies, and network traffic.
31 #include "../libcli/auth/libcli_auth.h"
32 #include "../librpc/gen_ndr/ndr_schannel.h"
33 #include "../libcli/auth/schannel.h"
34 #include "../libcli/auth/schannel_proto.h"
36 extern struct current_user current_user;
39 #define DBGC_CLASS DBGC_RPC_SRV
41 static void free_pipe_ntlmssp_auth_data(struct pipe_auth_data *auth)
43 AUTH_NTLMSSP_STATE *a = auth->a_u.auth_ntlmssp_state;
48 auth->a_u.auth_ntlmssp_state = NULL;
51 static DATA_BLOB generic_session_key(void)
53 return data_blob("SystemLibraryDTC", 16);
56 /*******************************************************************
57 Generate the next PDU to be returned from the data in p->rdata.
59 ********************************************************************/
61 static bool create_next_pdu_ntlmssp(pipes_struct *p)
63 RPC_HDR_RESP hdr_resp;
64 uint32 ss_padding_len = 0;
65 uint32 data_space_available;
70 RPC_HDR_AUTH auth_info;
71 uint8 auth_type, auth_level;
72 AUTH_NTLMSSP_STATE *a = p->auth.a_u.auth_ntlmssp_state;
75 * If we're in the fault state, keep returning fault PDU's until
76 * the pipe gets closed. JRA.
80 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
84 memset((char *)&hdr_resp, '\0', sizeof(hdr_resp));
86 /* Change the incoming request header to a response. */
87 p->hdr.pkt_type = RPC_RESPONSE;
89 /* Set up rpc header flags. */
90 if (p->out_data.data_sent_length == 0) {
91 p->hdr.flags = RPC_FLG_FIRST;
97 * Work out how much we can fit in a single PDU.
100 data_len_left = prs_offset(&p->out_data.rdata) - p->out_data.data_sent_length;
103 * Ensure there really is data left to send.
107 DEBUG(0,("create_next_pdu_ntlmssp: no data left to send !\n"));
111 data_space_available = RPC_MAX_PDU_FRAG_LEN - RPC_HEADER_LEN
112 - RPC_HDR_RESP_LEN - RPC_HDR_AUTH_LEN - NTLMSSP_SIG_SIZE;
115 * The amount we send is the minimum of the available
116 * space and the amount left to send.
119 data_len = MIN(data_len_left, data_space_available);
122 * Set up the alloc hint. This should be the data left to
126 hdr_resp.alloc_hint = data_len_left;
129 * Work out if this PDU will be the last.
132 if(p->out_data.data_sent_length + data_len >= prs_offset(&p->out_data.rdata)) {
133 p->hdr.flags |= RPC_FLG_LAST;
134 if (data_len_left % 8) {
135 ss_padding_len = 8 - (data_len_left % 8);
136 DEBUG(10,("create_next_pdu_ntlmssp: adding sign/seal padding of %u\n",
142 * Set up the header lengths.
145 p->hdr.frag_len = RPC_HEADER_LEN + RPC_HDR_RESP_LEN +
146 data_len + ss_padding_len +
147 RPC_HDR_AUTH_LEN + NTLMSSP_SIG_SIZE;
148 p->hdr.auth_len = NTLMSSP_SIG_SIZE;
152 * Init the parse struct to point at the outgoing
156 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
158 /* Store the header in the data stream. */
159 if(!smb_io_rpc_hdr("hdr", &p->hdr, &p->out_data.frag, 0)) {
160 DEBUG(0,("create_next_pdu_ntlmssp: failed to marshall RPC_HDR.\n"));
161 prs_mem_free(&p->out_data.frag);
165 if(!smb_io_rpc_hdr_resp("resp", &hdr_resp, &p->out_data.frag, 0)) {
166 DEBUG(0,("create_next_pdu_ntlmssp: failed to marshall RPC_HDR_RESP.\n"));
167 prs_mem_free(&p->out_data.frag);
171 /* Copy the data into the PDU. */
173 if(!prs_append_some_prs_data(&p->out_data.frag, &p->out_data.rdata,
174 p->out_data.data_sent_length, data_len)) {
175 DEBUG(0,("create_next_pdu_ntlmssp: failed to copy %u bytes of data.\n", (unsigned int)data_len));
176 prs_mem_free(&p->out_data.frag);
180 /* Copy the sign/seal padding data. */
181 if (ss_padding_len) {
184 memset(pad, '\0', 8);
185 if (!prs_copy_data_in(&p->out_data.frag, pad,
187 DEBUG(0,("create_next_pdu_ntlmssp: failed to add %u bytes of pad data.\n",
188 (unsigned int)ss_padding_len));
189 prs_mem_free(&p->out_data.frag);
195 /* Now write out the auth header and null blob. */
196 if (p->auth.auth_type == PIPE_AUTH_TYPE_NTLMSSP) {
197 auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
199 auth_type = DCERPC_AUTH_TYPE_SPNEGO;
201 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
202 auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
204 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
207 init_rpc_hdr_auth(&auth_info, auth_type, auth_level, ss_padding_len, 1 /* context id. */);
208 if(!smb_io_rpc_hdr_auth("hdr_auth", &auth_info, &p->out_data.frag,
210 DEBUG(0,("create_next_pdu_ntlmssp: failed to marshall RPC_HDR_AUTH.\n"));
211 prs_mem_free(&p->out_data.frag);
215 /* Generate the sign blob. */
217 switch (p->auth.auth_level) {
218 case DCERPC_AUTH_LEVEL_PRIVACY:
219 /* Data portion is encrypted. */
220 status = ntlmssp_seal_packet(
222 (uint8_t *)prs_data_p(&p->out_data.frag)
223 + RPC_HEADER_LEN + RPC_HDR_RESP_LEN,
224 data_len + ss_padding_len,
225 (unsigned char *)prs_data_p(&p->out_data.frag),
226 (size_t)prs_offset(&p->out_data.frag),
228 if (!NT_STATUS_IS_OK(status)) {
229 data_blob_free(&auth_blob);
230 prs_mem_free(&p->out_data.frag);
234 case DCERPC_AUTH_LEVEL_INTEGRITY:
235 /* Data is signed. */
236 status = ntlmssp_sign_packet(
238 (unsigned char *)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)) {
245 data_blob_free(&auth_blob);
246 prs_mem_free(&p->out_data.frag);
251 prs_mem_free(&p->out_data.frag);
255 /* Append the auth blob. */
256 if (!prs_copy_data_in(&p->out_data.frag, (char *)auth_blob.data,
258 DEBUG(0,("create_next_pdu_ntlmssp: failed to add %u bytes auth blob.\n",
259 (unsigned int)NTLMSSP_SIG_SIZE));
260 data_blob_free(&auth_blob);
261 prs_mem_free(&p->out_data.frag);
265 data_blob_free(&auth_blob);
268 * Setup the counts for this PDU.
271 p->out_data.data_sent_length += data_len;
272 p->out_data.current_pdu_sent = 0;
277 /*******************************************************************
278 Generate the next PDU to be returned from the data in p->rdata.
279 Return an schannel authenticated fragment.
280 ********************************************************************/
282 static bool create_next_pdu_schannel(pipes_struct *p)
284 RPC_HDR_RESP hdr_resp;
285 uint32 ss_padding_len = 0;
287 uint32 data_space_available;
288 uint32 data_len_left;
293 * If we're in the fault state, keep returning fault PDU's until
294 * the pipe gets closed. JRA.
298 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
302 memset((char *)&hdr_resp, '\0', sizeof(hdr_resp));
304 /* Change the incoming request header to a response. */
305 p->hdr.pkt_type = RPC_RESPONSE;
307 /* Set up rpc header flags. */
308 if (p->out_data.data_sent_length == 0) {
309 p->hdr.flags = RPC_FLG_FIRST;
315 * Work out how much we can fit in a single PDU.
318 data_len_left = prs_offset(&p->out_data.rdata) - p->out_data.data_sent_length;
321 * Ensure there really is data left to send.
325 DEBUG(0,("create_next_pdu_schannel: no data left to send !\n"));
329 data_space_available = RPC_MAX_PDU_FRAG_LEN - RPC_HEADER_LEN
330 - RPC_HDR_RESP_LEN - RPC_HDR_AUTH_LEN
331 - RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN;
334 * The amount we send is the minimum of the available
335 * space and the amount left to send.
338 data_len = MIN(data_len_left, data_space_available);
341 * Set up the alloc hint. This should be the data left to
345 hdr_resp.alloc_hint = data_len_left;
348 * Work out if this PDU will be the last.
351 if(p->out_data.data_sent_length + data_len >= prs_offset(&p->out_data.rdata)) {
352 p->hdr.flags |= RPC_FLG_LAST;
353 if (data_len_left % 8) {
354 ss_padding_len = 8 - (data_len_left % 8);
355 DEBUG(10,("create_next_pdu_schannel: adding sign/seal padding of %u\n",
360 p->hdr.frag_len = RPC_HEADER_LEN + RPC_HDR_RESP_LEN + data_len + ss_padding_len +
361 RPC_HDR_AUTH_LEN + RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN;
362 p->hdr.auth_len = RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN;
365 * Init the parse struct to point at the outgoing
369 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
371 /* Store the header in the data stream. */
372 if(!smb_io_rpc_hdr("hdr", &p->hdr, &p->out_data.frag, 0)) {
373 DEBUG(0,("create_next_pdu_schannel: failed to marshall RPC_HDR.\n"));
374 prs_mem_free(&p->out_data.frag);
378 if(!smb_io_rpc_hdr_resp("resp", &hdr_resp, &p->out_data.frag, 0)) {
379 DEBUG(0,("create_next_pdu_schannel: failed to marshall RPC_HDR_RESP.\n"));
380 prs_mem_free(&p->out_data.frag);
384 /* Store the current offset. */
385 data_pos = prs_offset(&p->out_data.frag);
387 /* Copy the data into the PDU. */
389 if(!prs_append_some_prs_data(&p->out_data.frag, &p->out_data.rdata,
390 p->out_data.data_sent_length, data_len)) {
391 DEBUG(0,("create_next_pdu_schannel: failed to copy %u bytes of data.\n", (unsigned int)data_len));
392 prs_mem_free(&p->out_data.frag);
396 /* Copy the sign/seal padding data. */
397 if (ss_padding_len) {
399 memset(pad, '\0', 8);
400 if (!prs_copy_data_in(&p->out_data.frag, pad,
402 DEBUG(0,("create_next_pdu_schannel: failed to add %u bytes of pad data.\n", (unsigned int)ss_padding_len));
403 prs_mem_free(&p->out_data.frag);
410 * Schannel processing.
412 RPC_HDR_AUTH auth_info;
413 struct NL_AUTH_SIGNATURE verf;
415 enum ndr_err_code ndr_err;
417 /* Check it's the type of reply we were expecting to decode */
419 init_rpc_hdr_auth(&auth_info,
420 DCERPC_AUTH_TYPE_SCHANNEL,
421 p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY ?
422 DCERPC_AUTH_LEVEL_PRIVACY : DCERPC_AUTH_LEVEL_INTEGRITY,
425 if(!smb_io_rpc_hdr_auth("hdr_auth", &auth_info,
426 &p->out_data.frag, 0)) {
427 DEBUG(0,("create_next_pdu_schannel: failed to marshall RPC_HDR_AUTH.\n"));
428 prs_mem_free(&p->out_data.frag);
432 switch (p->auth.auth_level) {
433 case DCERPC_AUTH_LEVEL_PRIVACY:
434 status = schannel_seal_packet(p->auth.a_u.schannel_auth,
436 (uint8_t *)prs_data_p(&p->out_data.frag) + data_pos,
437 data_len + ss_padding_len,
440 case DCERPC_AUTH_LEVEL_INTEGRITY:
441 status = schannel_sign_packet(p->auth.a_u.schannel_auth,
443 (uint8_t *)prs_data_p(&p->out_data.frag) + data_pos,
444 data_len + ss_padding_len,
448 status = NT_STATUS_INTERNAL_ERROR;
452 if (!NT_STATUS_IS_OK(status)) {
453 DEBUG(0,("create_next_pdu_schannel: failed to process packet: %s\n",
455 prs_mem_free(&p->out_data.frag);
459 /* Finally marshall the blob. */
462 ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), NULL, &verf,
463 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_SIGNATURE);
464 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
465 prs_mem_free(&p->out_data.frag);
469 if (DEBUGLEVEL >= 10) {
470 NDR_PRINT_DEBUG(NL_AUTH_SIGNATURE, &verf);
473 if (!prs_copy_data_in(&p->out_data.frag, (const char *)blob.data, blob.length)) {
474 prs_mem_free(&p->out_data.frag);
480 * Setup the counts for this PDU.
483 p->out_data.data_sent_length += data_len;
484 p->out_data.current_pdu_sent = 0;
489 /*******************************************************************
490 Generate the next PDU to be returned from the data in p->rdata.
491 No authentication done.
492 ********************************************************************/
494 static bool create_next_pdu_noauth(pipes_struct *p)
496 RPC_HDR_RESP hdr_resp;
498 uint32 data_space_available;
499 uint32 data_len_left;
502 * If we're in the fault state, keep returning fault PDU's until
503 * the pipe gets closed. JRA.
507 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
511 memset((char *)&hdr_resp, '\0', sizeof(hdr_resp));
513 /* Change the incoming request header to a response. */
514 p->hdr.pkt_type = RPC_RESPONSE;
516 /* Set up rpc header flags. */
517 if (p->out_data.data_sent_length == 0) {
518 p->hdr.flags = RPC_FLG_FIRST;
524 * Work out how much we can fit in a single PDU.
527 data_len_left = prs_offset(&p->out_data.rdata) - p->out_data.data_sent_length;
530 * Ensure there really is data left to send.
534 DEBUG(0,("create_next_pdu_noath: no data left to send !\n"));
538 data_space_available = RPC_MAX_PDU_FRAG_LEN - RPC_HEADER_LEN
542 * The amount we send is the minimum of the available
543 * space and the amount left to send.
546 data_len = MIN(data_len_left, data_space_available);
549 * Set up the alloc hint. This should be the data left to
553 hdr_resp.alloc_hint = data_len_left;
556 * Work out if this PDU will be the last.
559 if(p->out_data.data_sent_length + data_len >= prs_offset(&p->out_data.rdata)) {
560 p->hdr.flags |= RPC_FLG_LAST;
564 * Set up the header lengths.
567 p->hdr.frag_len = RPC_HEADER_LEN + RPC_HDR_RESP_LEN + data_len;
571 * Init the parse struct to point at the outgoing
575 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
577 /* Store the header in the data stream. */
578 if(!smb_io_rpc_hdr("hdr", &p->hdr, &p->out_data.frag, 0)) {
579 DEBUG(0,("create_next_pdu_noath: failed to marshall RPC_HDR.\n"));
580 prs_mem_free(&p->out_data.frag);
584 if(!smb_io_rpc_hdr_resp("resp", &hdr_resp, &p->out_data.frag, 0)) {
585 DEBUG(0,("create_next_pdu_noath: failed to marshall RPC_HDR_RESP.\n"));
586 prs_mem_free(&p->out_data.frag);
590 /* Copy the data into the PDU. */
592 if(!prs_append_some_prs_data(&p->out_data.frag, &p->out_data.rdata,
593 p->out_data.data_sent_length, data_len)) {
594 DEBUG(0,("create_next_pdu_noauth: failed to copy %u bytes of data.\n", (unsigned int)data_len));
595 prs_mem_free(&p->out_data.frag);
600 * Setup the counts for this PDU.
603 p->out_data.data_sent_length += data_len;
604 p->out_data.current_pdu_sent = 0;
609 /*******************************************************************
610 Generate the next PDU to be returned from the data in p->rdata.
611 ********************************************************************/
613 bool create_next_pdu(pipes_struct *p)
615 switch(p->auth.auth_level) {
616 case DCERPC_AUTH_LEVEL_NONE:
617 case DCERPC_AUTH_LEVEL_CONNECT:
618 /* This is incorrect for auth level connect. Fixme. JRA */
619 return create_next_pdu_noauth(p);
622 switch(p->auth.auth_type) {
623 case PIPE_AUTH_TYPE_NTLMSSP:
624 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
625 return create_next_pdu_ntlmssp(p);
626 case PIPE_AUTH_TYPE_SCHANNEL:
627 return create_next_pdu_schannel(p);
633 DEBUG(0,("create_next_pdu: invalid internal auth level %u / type %u",
634 (unsigned int)p->auth.auth_level,
635 (unsigned int)p->auth.auth_type));
639 /*******************************************************************
640 Process an NTLMSSP authentication response.
641 If this function succeeds, the user has been authenticated
642 and their domain, name and calling workstation stored in
644 *******************************************************************/
646 static bool pipe_ntlmssp_verify_final(pipes_struct *p, DATA_BLOB *p_resp_blob)
648 DATA_BLOB session_key, reply;
650 AUTH_NTLMSSP_STATE *a = p->auth.a_u.auth_ntlmssp_state;
653 DEBUG(5,("pipe_ntlmssp_verify_final: pipe %s checking user details\n",
654 get_pipe_name_from_iface(&p->syntax)));
658 /* this has to be done as root in order to verify the password */
660 status = auth_ntlmssp_update(a, *p_resp_blob, &reply);
663 /* Don't generate a reply. */
664 data_blob_free(&reply);
666 if (!NT_STATUS_IS_OK(status)) {
670 /* Finally - if the pipe negotiated integrity (sign) or privacy (seal)
671 ensure the underlying NTLMSSP flags are also set. If not we should
674 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_INTEGRITY) {
675 if (!(a->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN)) {
676 DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet integrity requested "
677 "but client declined signing.\n",
678 get_pipe_name_from_iface(&p->syntax)));
682 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
683 if (!(a->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL)) {
684 DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet privacy requested "
685 "but client declined sealing.\n",
686 get_pipe_name_from_iface(&p->syntax)));
691 DEBUG(5, ("pipe_ntlmssp_verify_final: OK: user: %s domain: %s "
692 "workstation: %s\n", a->ntlmssp_state->user,
693 a->ntlmssp_state->domain, a->ntlmssp_state->workstation));
695 if (a->server_info->ptok == NULL) {
696 DEBUG(1,("Error: Authmodule failed to provide nt_user_token\n"));
700 TALLOC_FREE(p->server_info);
702 p->server_info = copy_serverinfo(p, a->server_info);
703 if (p->server_info == NULL) {
704 DEBUG(0, ("copy_serverinfo failed\n"));
709 * We're an authenticated bind over smb, so the session key needs to
710 * be set to "SystemLibraryDTC". Weird, but this is what Windows
711 * does. See the RPC-SAMBA3SESSIONKEY.
714 session_key = generic_session_key();
715 if (session_key.data == NULL) {
719 ret = server_info_set_session_key(p->server_info, session_key);
721 data_blob_free(&session_key);
726 /*******************************************************************
727 The switch table for the pipe names and the functions to handle them.
728 *******************************************************************/
735 struct ndr_syntax_id rpc_interface;
736 const struct api_struct *cmds;
740 static struct rpc_table *rpc_lookup;
741 static int rpc_lookup_size;
743 /*******************************************************************
744 This is the "stage3" NTLMSSP response after a bind request and reply.
745 *******************************************************************/
747 bool api_pipe_bind_auth3(pipes_struct *p, prs_struct *rpc_in_p)
749 RPC_HDR_AUTH auth_info;
755 DEBUG(5,("api_pipe_bind_auth3: decode request. %d\n", __LINE__));
757 if (p->hdr.auth_len == 0) {
758 DEBUG(0,("api_pipe_bind_auth3: No auth field sent !\n"));
762 /* 4 bytes padding. */
763 if (!prs_uint32("pad", rpc_in_p, 0, &pad)) {
764 DEBUG(0,("api_pipe_bind_auth3: unmarshall of 4 byte pad failed.\n"));
769 * Decode the authentication verifier response.
772 if(!smb_io_rpc_hdr_auth("", &auth_info, rpc_in_p, 0)) {
773 DEBUG(0,("api_pipe_bind_auth3: unmarshall of RPC_HDR_AUTH failed.\n"));
777 if (auth_info.auth_type != DCERPC_AUTH_TYPE_NTLMSSP) {
778 DEBUG(0,("api_pipe_bind_auth3: incorrect auth type (%u).\n",
779 (unsigned int)auth_info.auth_type ));
783 blob = data_blob(NULL,p->hdr.auth_len);
785 if (!prs_copy_data_out((char *)blob.data, rpc_in_p, p->hdr.auth_len)) {
786 DEBUG(0,("api_pipe_bind_auth3: Failed to pull %u bytes - the response blob.\n",
787 (unsigned int)p->hdr.auth_len ));
792 * The following call actually checks the challenge/response data.
793 * for correctness against the given DOMAIN\user name.
796 if (!pipe_ntlmssp_verify_final(p, &blob)) {
800 data_blob_free(&blob);
802 p->pipe_bound = True;
808 data_blob_free(&blob);
809 free_pipe_ntlmssp_auth_data(&p->auth);
810 p->auth.a_u.auth_ntlmssp_state = NULL;
815 /*******************************************************************
816 Marshall a bind_nak pdu.
817 *******************************************************************/
819 static bool setup_bind_nak(pipes_struct *p)
824 /* Free any memory in the current return data buffer. */
825 prs_mem_free(&p->out_data.rdata);
828 * Marshall directly into the outgoing PDU space. We
829 * must do this as we need to set to the bind response
830 * header and are never sending more than one PDU here.
833 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
836 * Initialize a bind_nak header.
839 init_rpc_hdr(&nak_hdr, RPC_BINDNACK, RPC_FLG_FIRST | RPC_FLG_LAST,
840 p->hdr.call_id, RPC_HEADER_LEN + sizeof(uint16), 0);
843 * Marshall the header into the outgoing PDU.
846 if(!smb_io_rpc_hdr("", &nak_hdr, &p->out_data.frag, 0)) {
847 DEBUG(0,("setup_bind_nak: marshalling of RPC_HDR failed.\n"));
848 prs_mem_free(&p->out_data.frag);
853 * Now add the reject reason.
856 if(!prs_uint16("reject code", &p->out_data.frag, 0, &zero)) {
857 prs_mem_free(&p->out_data.frag);
861 p->out_data.data_sent_length = 0;
862 p->out_data.current_pdu_sent = 0;
864 if (p->auth.auth_data_free_func) {
865 (*p->auth.auth_data_free_func)(&p->auth);
867 p->auth.auth_level = DCERPC_AUTH_LEVEL_NONE;
868 p->auth.auth_type = PIPE_AUTH_TYPE_NONE;
869 p->pipe_bound = False;
874 /*******************************************************************
875 Marshall a fault pdu.
876 *******************************************************************/
878 bool setup_fault_pdu(pipes_struct *p, NTSTATUS status)
881 RPC_HDR_RESP hdr_resp;
882 RPC_HDR_FAULT fault_resp;
884 /* Free any memory in the current return data buffer. */
885 prs_mem_free(&p->out_data.rdata);
888 * Marshall directly into the outgoing PDU space. We
889 * must do this as we need to set to the bind response
890 * header and are never sending more than one PDU here.
893 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
896 * Initialize a fault header.
899 init_rpc_hdr(&fault_hdr, RPC_FAULT, RPC_FLG_FIRST | RPC_FLG_LAST | RPC_FLG_NOCALL,
900 p->hdr.call_id, RPC_HEADER_LEN + RPC_HDR_RESP_LEN + RPC_HDR_FAULT_LEN, 0);
903 * Initialize the HDR_RESP and FAULT parts of the PDU.
906 memset((char *)&hdr_resp, '\0', sizeof(hdr_resp));
908 fault_resp.status = status;
909 fault_resp.reserved = 0;
912 * Marshall the header into the outgoing PDU.
915 if(!smb_io_rpc_hdr("", &fault_hdr, &p->out_data.frag, 0)) {
916 DEBUG(0,("setup_fault_pdu: marshalling of RPC_HDR failed.\n"));
917 prs_mem_free(&p->out_data.frag);
921 if(!smb_io_rpc_hdr_resp("resp", &hdr_resp, &p->out_data.frag, 0)) {
922 DEBUG(0,("setup_fault_pdu: failed to marshall RPC_HDR_RESP.\n"));
923 prs_mem_free(&p->out_data.frag);
927 if(!smb_io_rpc_hdr_fault("fault", &fault_resp, &p->out_data.frag, 0)) {
928 DEBUG(0,("setup_fault_pdu: failed to marshall RPC_HDR_FAULT.\n"));
929 prs_mem_free(&p->out_data.frag);
933 p->out_data.data_sent_length = 0;
934 p->out_data.current_pdu_sent = 0;
940 /*******************************************************************
941 Marshall a cancel_ack pdu.
942 We should probably check the auth-verifier here.
943 *******************************************************************/
945 bool setup_cancel_ack_reply(pipes_struct *p, prs_struct *rpc_in_p)
947 prs_struct outgoing_pdu;
948 RPC_HDR ack_reply_hdr;
950 /* Free any memory in the current return data buffer. */
951 prs_mem_free(&p->out_data.rdata);
954 * Marshall directly into the outgoing PDU space. We
955 * must do this as we need to set to the bind response
956 * header and are never sending more than one PDU here.
959 prs_init_empty( &outgoing_pdu, p->mem_ctx, MARSHALL);
960 prs_give_memory( &outgoing_pdu, (char *)p->out_data.current_pdu, sizeof(p->out_data.current_pdu), False);
963 * Initialize a cancel_ack header.
966 init_rpc_hdr(&ack_reply_hdr, RPC_CANCEL_ACK, RPC_FLG_FIRST | RPC_FLG_LAST,
967 p->hdr.call_id, RPC_HEADER_LEN, 0);
970 * Marshall the header into the outgoing PDU.
973 if(!smb_io_rpc_hdr("", &ack_reply_hdr, &outgoing_pdu, 0)) {
974 DEBUG(0,("setup_cancel_ack_reply: marshalling of RPC_HDR failed.\n"));
975 prs_mem_free(&outgoing_pdu);
979 p->out_data.data_sent_length = 0;
980 p->out_data.current_pdu_len = prs_offset(&outgoing_pdu);
981 p->out_data.current_pdu_sent = 0;
983 prs_mem_free(&outgoing_pdu);
988 /*******************************************************************
989 Ensure a bind request has the correct abstract & transfer interface.
990 Used to reject unknown binds from Win2k.
991 *******************************************************************/
993 static bool check_bind_req(struct pipes_struct *p,
994 struct ndr_syntax_id* abstract,
995 struct ndr_syntax_id* transfer,
999 struct pipe_rpc_fns *context_fns;
1001 DEBUG(3,("check_bind_req for %s\n",
1002 get_pipe_name_from_iface(&p->syntax)));
1004 /* we have to check all now since win2k introduced a new UUID on the lsaprpc pipe */
1006 for (i=0; i<rpc_lookup_size; i++) {
1007 DEBUGADD(10, ("checking %s\n", rpc_lookup[i].pipe.clnt));
1008 if (ndr_syntax_id_equal(
1009 abstract, &rpc_lookup[i].rpc_interface)
1010 && ndr_syntax_id_equal(
1011 transfer, &ndr_transfer_syntax)) {
1016 if (i == rpc_lookup_size) {
1020 context_fns = SMB_MALLOC_P(struct pipe_rpc_fns);
1021 if (context_fns == NULL) {
1022 DEBUG(0,("check_bind_req: malloc() failed!\n"));
1026 context_fns->cmds = rpc_lookup[i].cmds;
1027 context_fns->n_cmds = rpc_lookup[i].n_cmds;
1028 context_fns->context_id = context_id;
1030 /* add to the list of open contexts */
1032 DLIST_ADD( p->contexts, context_fns );
1037 /*******************************************************************
1038 Register commands to an RPC pipe
1039 *******************************************************************/
1041 NTSTATUS rpc_srv_register(int version, const char *clnt, const char *srv,
1042 const struct ndr_interface_table *iface,
1043 const struct api_struct *cmds, int size)
1045 struct rpc_table *rpc_entry;
1047 if (!clnt || !srv || !cmds) {
1048 return NT_STATUS_INVALID_PARAMETER;
1051 if (version != SMB_RPC_INTERFACE_VERSION) {
1052 DEBUG(0,("Can't register rpc commands!\n"
1053 "You tried to register a rpc module with SMB_RPC_INTERFACE_VERSION %d"
1054 ", while this version of samba uses version %d!\n",
1055 version,SMB_RPC_INTERFACE_VERSION));
1056 return NT_STATUS_OBJECT_TYPE_MISMATCH;
1061 * we still need to make sure that don't register the same commands twice!!!
1066 /* We use a temporary variable because this call can fail and
1067 rpc_lookup will still be valid afterwards. It could then succeed if
1068 called again later */
1070 rpc_entry = SMB_REALLOC_ARRAY_KEEP_OLD_ON_ERROR(rpc_lookup, struct rpc_table, rpc_lookup_size);
1071 if (NULL == rpc_entry) {
1073 DEBUG(0, ("rpc_pipe_register_commands: memory allocation failed\n"));
1074 return NT_STATUS_NO_MEMORY;
1076 rpc_lookup = rpc_entry;
1079 rpc_entry = rpc_lookup + (rpc_lookup_size - 1);
1080 ZERO_STRUCTP(rpc_entry);
1081 rpc_entry->pipe.clnt = SMB_STRDUP(clnt);
1082 rpc_entry->pipe.srv = SMB_STRDUP(srv);
1083 rpc_entry->rpc_interface = iface->syntax_id;
1084 rpc_entry->cmds = cmds;
1085 rpc_entry->n_cmds = size;
1087 return NT_STATUS_OK;
1091 * Is a named pipe known?
1092 * @param[in] cli_filename The pipe name requested by the client
1093 * @result Do we want to serve this?
1095 bool is_known_pipename(const char *cli_filename, struct ndr_syntax_id *syntax)
1097 const char *pipename = cli_filename;
1100 if (strnequal(pipename, "\\PIPE\\", 6)) {
1104 if (*pipename == '\\') {
1108 if (lp_disable_spoolss() && strequal(pipename, "spoolss")) {
1109 DEBUG(10, ("refusing spoolss access\n"));
1113 for (i=0; i<rpc_lookup_size; i++) {
1114 if (strequal(pipename, rpc_lookup[i].pipe.clnt)) {
1115 *syntax = rpc_lookup[i].rpc_interface;
1120 DEBUG(10, ("is_known_pipename: %s unknown\n", cli_filename));
1124 /*******************************************************************
1125 Handle a SPNEGO krb5 bind auth.
1126 *******************************************************************/
1128 static bool pipe_spnego_auth_bind_kerberos(pipes_struct *p, prs_struct *rpc_in_p, RPC_HDR_AUTH *pauth_info,
1129 DATA_BLOB *psecblob, prs_struct *pout_auth)
1134 /*******************************************************************
1135 Handle the first part of a SPNEGO bind auth.
1136 *******************************************************************/
1138 static bool pipe_spnego_auth_bind_negotiate(pipes_struct *p, prs_struct *rpc_in_p,
1139 RPC_HDR_AUTH *pauth_info, prs_struct *pout_auth)
1145 char *OIDs[ASN1_MAX_OIDS];
1148 bool got_kerberos_mechanism = false;
1149 AUTH_NTLMSSP_STATE *a = NULL;
1150 RPC_HDR_AUTH auth_info;
1152 ZERO_STRUCT(secblob);
1154 ZERO_STRUCT(response);
1156 /* Grab the SPNEGO blob. */
1157 blob = data_blob(NULL,p->hdr.auth_len);
1159 if (!prs_copy_data_out((char *)blob.data, rpc_in_p, p->hdr.auth_len)) {
1160 DEBUG(0,("pipe_spnego_auth_bind_negotiate: Failed to pull %u bytes - the SPNEGO auth header.\n",
1161 (unsigned int)p->hdr.auth_len ));
1165 if (blob.data[0] != ASN1_APPLICATION(0)) {
1169 /* parse out the OIDs and the first sec blob */
1170 if (!parse_negTokenTarg(blob, OIDs, &secblob)) {
1171 DEBUG(0,("pipe_spnego_auth_bind_negotiate: Failed to parse the security blob.\n"));
1175 if (strcmp(OID_KERBEROS5, OIDs[0]) == 0 || strcmp(OID_KERBEROS5_OLD, OIDs[0]) == 0) {
1176 got_kerberos_mechanism = true;
1179 for (i=0;OIDs[i];i++) {
1180 DEBUG(3,("pipe_spnego_auth_bind_negotiate: Got OID %s\n", OIDs[i]));
1181 TALLOC_FREE(OIDs[i]);
1183 DEBUG(3,("pipe_spnego_auth_bind_negotiate: Got secblob of size %lu\n", (unsigned long)secblob.length));
1185 if ( got_kerberos_mechanism && ((lp_security()==SEC_ADS) || USE_KERBEROS_KEYTAB) ) {
1186 bool ret = pipe_spnego_auth_bind_kerberos(p, rpc_in_p, pauth_info, &secblob, pout_auth);
1187 data_blob_free(&secblob);
1188 data_blob_free(&blob);
1192 if (p->auth.auth_type == PIPE_AUTH_TYPE_SPNEGO_NTLMSSP && p->auth.a_u.auth_ntlmssp_state) {
1193 /* Free any previous auth type. */
1194 free_pipe_ntlmssp_auth_data(&p->auth);
1197 if (!got_kerberos_mechanism) {
1198 /* Initialize the NTLM engine. */
1199 status = auth_ntlmssp_start(&a);
1200 if (!NT_STATUS_IS_OK(status)) {
1205 * Pass the first security blob of data to it.
1206 * This can return an error or NT_STATUS_MORE_PROCESSING_REQUIRED
1207 * which means we need another packet to complete the bind.
1210 status = auth_ntlmssp_update(a, secblob, &chal);
1212 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1213 DEBUG(3,("pipe_spnego_auth_bind_negotiate: auth_ntlmssp_update failed.\n"));
1217 /* Generate the response blob we need for step 2 of the bind. */
1218 response = spnego_gen_auth_response(&chal, status, OID_NTLMSSP);
1221 * SPNEGO negotiate down to NTLMSSP. The subsequent
1222 * code to process follow-up packets is not complete
1225 response = spnego_gen_auth_response(NULL,
1226 NT_STATUS_MORE_PROCESSING_REQUIRED,
1230 /* Copy the blob into the pout_auth parse struct */
1231 init_rpc_hdr_auth(&auth_info, DCERPC_AUTH_TYPE_SPNEGO, pauth_info->auth_level, RPC_HDR_AUTH_LEN, 1);
1232 if(!smb_io_rpc_hdr_auth("", &auth_info, pout_auth, 0)) {
1233 DEBUG(0,("pipe_spnego_auth_bind_negotiate: marshalling of RPC_HDR_AUTH failed.\n"));
1237 if (!prs_copy_data_in(pout_auth, (char *)response.data, response.length)) {
1238 DEBUG(0,("pipe_spnego_auth_bind_negotiate: marshalling of data blob failed.\n"));
1242 p->auth.a_u.auth_ntlmssp_state = a;
1243 p->auth.auth_data_free_func = &free_pipe_ntlmssp_auth_data;
1244 p->auth.auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1246 data_blob_free(&blob);
1247 data_blob_free(&secblob);
1248 data_blob_free(&chal);
1249 data_blob_free(&response);
1251 /* We can't set pipe_bound True yet - we need an RPC_ALTER_CONTEXT response packet... */
1256 data_blob_free(&blob);
1257 data_blob_free(&secblob);
1258 data_blob_free(&chal);
1259 data_blob_free(&response);
1261 p->auth.a_u.auth_ntlmssp_state = NULL;
1266 /*******************************************************************
1267 Handle the second part of a SPNEGO bind auth.
1268 *******************************************************************/
1270 static bool pipe_spnego_auth_bind_continue(pipes_struct *p, prs_struct *rpc_in_p,
1271 RPC_HDR_AUTH *pauth_info, prs_struct *pout_auth)
1273 RPC_HDR_AUTH auth_info;
1274 DATA_BLOB spnego_blob;
1275 DATA_BLOB auth_blob;
1276 DATA_BLOB auth_reply;
1278 AUTH_NTLMSSP_STATE *a = p->auth.a_u.auth_ntlmssp_state;
1280 ZERO_STRUCT(spnego_blob);
1281 ZERO_STRUCT(auth_blob);
1282 ZERO_STRUCT(auth_reply);
1283 ZERO_STRUCT(response);
1286 * NB. If we've negotiated down from krb5 to NTLMSSP we'll currently
1287 * fail here as 'a' == NULL.
1289 if (p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP || !a) {
1290 DEBUG(0,("pipe_spnego_auth_bind_continue: not in NTLMSSP auth state.\n"));
1294 /* Grab the SPNEGO blob. */
1295 spnego_blob = data_blob(NULL,p->hdr.auth_len);
1297 if (!prs_copy_data_out((char *)spnego_blob.data, rpc_in_p, p->hdr.auth_len)) {
1298 DEBUG(0,("pipe_spnego_auth_bind_continue: Failed to pull %u bytes - the SPNEGO auth header.\n",
1299 (unsigned int)p->hdr.auth_len ));
1303 if (spnego_blob.data[0] != ASN1_CONTEXT(1)) {
1304 DEBUG(0,("pipe_spnego_auth_bind_continue: invalid SPNEGO blob type.\n"));
1308 if (!spnego_parse_auth(spnego_blob, &auth_blob)) {
1309 DEBUG(0,("pipe_spnego_auth_bind_continue: invalid SPNEGO blob.\n"));
1314 * The following call actually checks the challenge/response data.
1315 * for correctness against the given DOMAIN\user name.
1318 if (!pipe_ntlmssp_verify_final(p, &auth_blob)) {
1322 data_blob_free(&spnego_blob);
1323 data_blob_free(&auth_blob);
1325 /* Generate the spnego "accept completed" blob - no incoming data. */
1326 response = spnego_gen_auth_response(&auth_reply, NT_STATUS_OK, OID_NTLMSSP);
1328 /* Copy the blob into the pout_auth parse struct */
1329 init_rpc_hdr_auth(&auth_info, DCERPC_AUTH_TYPE_SPNEGO, pauth_info->auth_level, RPC_HDR_AUTH_LEN, 1);
1330 if(!smb_io_rpc_hdr_auth("", &auth_info, pout_auth, 0)) {
1331 DEBUG(0,("pipe_spnego_auth_bind_continue: marshalling of RPC_HDR_AUTH failed.\n"));
1335 if (!prs_copy_data_in(pout_auth, (char *)response.data, response.length)) {
1336 DEBUG(0,("pipe_spnego_auth_bind_continue: marshalling of data blob failed.\n"));
1340 data_blob_free(&auth_reply);
1341 data_blob_free(&response);
1343 p->pipe_bound = True;
1349 data_blob_free(&spnego_blob);
1350 data_blob_free(&auth_blob);
1351 data_blob_free(&auth_reply);
1352 data_blob_free(&response);
1354 free_pipe_ntlmssp_auth_data(&p->auth);
1355 p->auth.a_u.auth_ntlmssp_state = NULL;
1360 /*******************************************************************
1361 Handle an schannel bind auth.
1362 *******************************************************************/
1364 static bool pipe_schannel_auth_bind(pipes_struct *p, prs_struct *rpc_in_p,
1365 RPC_HDR_AUTH *pauth_info, prs_struct *pout_auth)
1367 RPC_HDR_AUTH auth_info;
1368 struct NL_AUTH_MESSAGE neg;
1369 struct NL_AUTH_MESSAGE reply;
1372 struct netlogon_creds_CredentialState *creds;
1373 DATA_BLOB session_key;
1374 enum ndr_err_code ndr_err;
1377 blob = data_blob_const(prs_data_p(rpc_in_p) + prs_offset(rpc_in_p),
1378 prs_data_size(rpc_in_p));
1380 ndr_err = ndr_pull_struct_blob(&blob, talloc_tos(), NULL, &neg,
1381 (ndr_pull_flags_fn_t)ndr_pull_NL_AUTH_MESSAGE);
1382 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1383 DEBUG(0,("pipe_schannel_auth_bind: Could not unmarshal SCHANNEL auth neg\n"));
1387 if (DEBUGLEVEL >= 10) {
1388 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &neg);
1391 if (!(neg.Flags & NL_FLAG_OEM_NETBIOS_COMPUTER_NAME)) {
1392 DEBUG(0,("pipe_schannel_auth_bind: Did not receive netbios computer name\n"));
1397 * The neg.oem_netbios_computer.a key here must match the remote computer name
1398 * given in the DOM_CLNT_SRV.uni_comp_name used on all netlogon pipe
1399 * operations that use credentials.
1403 status = schannel_fetch_session_key(p,
1404 neg.oem_netbios_computer.a,
1408 if (!NT_STATUS_IS_OK(status)) {
1409 DEBUG(0, ("pipe_schannel_auth_bind: Attempt to bind using schannel without successful serverauth2\n"));
1413 p->auth.a_u.schannel_auth = talloc(p, struct schannel_state);
1414 if (!p->auth.a_u.schannel_auth) {
1419 p->auth.a_u.schannel_auth->state = SCHANNEL_STATE_START;
1420 p->auth.a_u.schannel_auth->seq_num = 0;
1421 p->auth.a_u.schannel_auth->initiator = false;
1422 p->auth.a_u.schannel_auth->creds = creds;
1425 * JRA. Should we also copy the schannel session key into the pipe session key p->session_key
1426 * here ? We do that for NTLMSSP, but the session key is already set up from the vuser
1427 * struct of the person who opened the pipe. I need to test this further. JRA.
1429 * VL. As we are mapping this to guest set the generic key
1430 * "SystemLibraryDTC" key here. It's a bit difficult to test against
1431 * W2k3, as it does not allow schannel binds against SAMR and LSA
1435 session_key = generic_session_key();
1436 if (session_key.data == NULL) {
1437 DEBUG(0, ("pipe_schannel_auth_bind: Could not alloc session"
1442 ret = server_info_set_session_key(p->server_info, session_key);
1444 data_blob_free(&session_key);
1447 DEBUG(0, ("server_info_set_session_key failed\n"));
1451 init_rpc_hdr_auth(&auth_info, DCERPC_AUTH_TYPE_SCHANNEL, pauth_info->auth_level, RPC_HDR_AUTH_LEN, 1);
1452 if(!smb_io_rpc_hdr_auth("", &auth_info, pout_auth, 0)) {
1453 DEBUG(0,("pipe_schannel_auth_bind: marshalling of RPC_HDR_AUTH failed.\n"));
1457 /*** SCHANNEL verifier ***/
1459 reply.MessageType = NL_NEGOTIATE_RESPONSE;
1461 reply.Buffer.dummy = 5; /* ??? actually I don't think
1462 * this has any meaning
1465 ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), NULL, &reply,
1466 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
1467 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1468 DEBUG(0,("Failed to marshall NL_AUTH_MESSAGE.\n"));
1472 if (DEBUGLEVEL >= 10) {
1473 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &reply);
1476 if (!prs_copy_data_in(pout_auth, (const char *)blob.data, blob.length)) {
1480 DEBUG(10,("pipe_schannel_auth_bind: schannel auth: domain [%s] myname [%s]\n",
1481 neg.oem_netbios_domain.a, neg.oem_netbios_computer.a));
1483 /* We're finished with this bind - no more packets. */
1484 p->auth.auth_data_free_func = NULL;
1485 p->auth.auth_type = PIPE_AUTH_TYPE_SCHANNEL;
1487 p->pipe_bound = True;
1492 /*******************************************************************
1493 Handle an NTLMSSP bind auth.
1494 *******************************************************************/
1496 static bool pipe_ntlmssp_auth_bind(pipes_struct *p, prs_struct *rpc_in_p,
1497 RPC_HDR_AUTH *pauth_info, prs_struct *pout_auth)
1499 RPC_HDR_AUTH auth_info;
1503 AUTH_NTLMSSP_STATE *a = NULL;
1506 ZERO_STRUCT(response);
1508 /* Grab the NTLMSSP blob. */
1509 blob = data_blob(NULL,p->hdr.auth_len);
1511 if (!prs_copy_data_out((char *)blob.data, rpc_in_p, p->hdr.auth_len)) {
1512 DEBUG(0,("pipe_ntlmssp_auth_bind: Failed to pull %u bytes - the NTLM auth header.\n",
1513 (unsigned int)p->hdr.auth_len ));
1517 if (strncmp((char *)blob.data, "NTLMSSP", 7) != 0) {
1518 DEBUG(0,("pipe_ntlmssp_auth_bind: Failed to read NTLMSSP in blob\n"));
1522 /* We have an NTLMSSP blob. */
1523 status = auth_ntlmssp_start(&a);
1524 if (!NT_STATUS_IS_OK(status)) {
1525 DEBUG(0,("pipe_ntlmssp_auth_bind: auth_ntlmssp_start failed: %s\n",
1526 nt_errstr(status) ));
1530 status = auth_ntlmssp_update(a, blob, &response);
1531 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1532 DEBUG(0,("pipe_ntlmssp_auth_bind: auth_ntlmssp_update failed: %s\n",
1533 nt_errstr(status) ));
1537 data_blob_free(&blob);
1539 /* Copy the blob into the pout_auth parse struct */
1540 init_rpc_hdr_auth(&auth_info, DCERPC_AUTH_TYPE_NTLMSSP, pauth_info->auth_level, RPC_HDR_AUTH_LEN, 1);
1541 if(!smb_io_rpc_hdr_auth("", &auth_info, pout_auth, 0)) {
1542 DEBUG(0,("pipe_ntlmssp_auth_bind: marshalling of RPC_HDR_AUTH failed.\n"));
1546 if (!prs_copy_data_in(pout_auth, (char *)response.data, response.length)) {
1547 DEBUG(0,("pipe_ntlmssp_auth_bind: marshalling of data blob failed.\n"));
1551 p->auth.a_u.auth_ntlmssp_state = a;
1552 p->auth.auth_data_free_func = &free_pipe_ntlmssp_auth_data;
1553 p->auth.auth_type = PIPE_AUTH_TYPE_NTLMSSP;
1555 data_blob_free(&blob);
1556 data_blob_free(&response);
1558 DEBUG(10,("pipe_ntlmssp_auth_bind: NTLMSSP auth started\n"));
1560 /* We can't set pipe_bound True yet - we need an RPC_AUTH3 response packet... */
1565 data_blob_free(&blob);
1566 data_blob_free(&response);
1568 free_pipe_ntlmssp_auth_data(&p->auth);
1569 p->auth.a_u.auth_ntlmssp_state = NULL;
1573 /*******************************************************************
1574 Respond to a pipe bind request.
1575 *******************************************************************/
1577 bool api_pipe_bind_req(pipes_struct *p, prs_struct *rpc_in_p)
1581 RPC_HDR_AUTH auth_info;
1583 fstring ack_pipe_name;
1584 prs_struct out_hdr_ba;
1585 prs_struct out_auth;
1588 unsigned int auth_type = DCERPC_AUTH_TYPE_NONE;
1590 /* No rebinds on a bound pipe - use alter context. */
1591 if (p->pipe_bound) {
1592 DEBUG(2,("api_pipe_bind_req: rejecting bind request on bound "
1593 "pipe %s.\n", get_pipe_name_from_iface(&p->syntax)));
1594 return setup_bind_nak(p);
1597 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
1600 * Marshall directly into the outgoing PDU space. We
1601 * must do this as we need to set to the bind response
1602 * header and are never sending more than one PDU here.
1606 * Setup the memory to marshall the ba header, and the
1610 if(!prs_init(&out_hdr_ba, 1024, p->mem_ctx, MARSHALL)) {
1611 DEBUG(0,("api_pipe_bind_req: malloc out_hdr_ba failed.\n"));
1612 prs_mem_free(&p->out_data.frag);
1616 if(!prs_init(&out_auth, 1024, p->mem_ctx, MARSHALL)) {
1617 DEBUG(0,("api_pipe_bind_req: malloc out_auth failed.\n"));
1618 prs_mem_free(&p->out_data.frag);
1619 prs_mem_free(&out_hdr_ba);
1623 DEBUG(5,("api_pipe_bind_req: decode request. %d\n", __LINE__));
1625 ZERO_STRUCT(hdr_rb);
1627 /* decode the bind request */
1629 if(!smb_io_rpc_hdr_rb("", &hdr_rb, rpc_in_p, 0)) {
1630 DEBUG(0,("api_pipe_bind_req: unable to unmarshall RPC_HDR_RB "
1635 if (hdr_rb.num_contexts == 0) {
1636 DEBUG(0, ("api_pipe_bind_req: no rpc contexts around\n"));
1641 * Try and find the correct pipe name to ensure
1642 * that this is a pipe name we support.
1645 for (i = 0; i < rpc_lookup_size; i++) {
1646 if (ndr_syntax_id_equal(&rpc_lookup[i].rpc_interface,
1647 &hdr_rb.rpc_context[0].abstract)) {
1648 DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
1649 rpc_lookup[i].pipe.clnt, rpc_lookup[i].pipe.srv));
1654 if (i == rpc_lookup_size) {
1657 status = smb_probe_module(
1658 "rpc", get_pipe_name_from_iface(
1659 &hdr_rb.rpc_context[0].abstract));
1661 if (NT_STATUS_IS_ERR(status)) {
1662 DEBUG(3,("api_pipe_bind_req: Unknown pipe name %s in bind request.\n",
1663 get_pipe_name_from_iface(&hdr_rb.rpc_context[0].abstract)));
1664 prs_mem_free(&p->out_data.frag);
1665 prs_mem_free(&out_hdr_ba);
1666 prs_mem_free(&out_auth);
1668 return setup_bind_nak(p);
1671 for (i = 0; i < rpc_lookup_size; i++) {
1672 if (strequal(rpc_lookup[i].pipe.clnt,
1673 get_pipe_name_from_iface(&p->syntax))) {
1674 DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
1675 rpc_lookup[i].pipe.clnt, rpc_lookup[i].pipe.srv));
1680 if (i == rpc_lookup_size) {
1681 DEBUG(0, ("module %s doesn't provide functions for "
1683 get_pipe_name_from_iface(&p->syntax),
1684 get_pipe_name_from_iface(&p->syntax)));
1689 /* name has to be \PIPE\xxxxx */
1690 fstrcpy(ack_pipe_name, "\\PIPE\\");
1691 fstrcat(ack_pipe_name, rpc_lookup[i].pipe.srv);
1693 DEBUG(5,("api_pipe_bind_req: make response. %d\n", __LINE__));
1696 * Check if this is an authenticated bind request.
1699 if (p->hdr.auth_len) {
1701 * Decode the authentication verifier.
1704 if(!smb_io_rpc_hdr_auth("", &auth_info, rpc_in_p, 0)) {
1705 DEBUG(0,("api_pipe_bind_req: unable to unmarshall RPC_HDR_AUTH struct.\n"));
1709 auth_type = auth_info.auth_type;
1711 /* Work out if we have to sign or seal etc. */
1712 switch (auth_info.auth_level) {
1713 case DCERPC_AUTH_LEVEL_INTEGRITY:
1714 p->auth.auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1716 case DCERPC_AUTH_LEVEL_PRIVACY:
1717 p->auth.auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1720 DEBUG(0,("api_pipe_bind_req: unexpected auth level (%u).\n",
1721 (unsigned int)auth_info.auth_level ));
1725 ZERO_STRUCT(auth_info);
1728 assoc_gid = hdr_rb.bba.assoc_gid ? hdr_rb.bba.assoc_gid : 0x53f0;
1731 case DCERPC_AUTH_TYPE_NTLMSSP:
1732 if (!pipe_ntlmssp_auth_bind(p, rpc_in_p, &auth_info, &out_auth)) {
1738 case DCERPC_AUTH_TYPE_SCHANNEL:
1739 if (!pipe_schannel_auth_bind(p, rpc_in_p, &auth_info, &out_auth)) {
1744 case DCERPC_AUTH_TYPE_SPNEGO:
1745 if (!pipe_spnego_auth_bind_negotiate(p, rpc_in_p, &auth_info, &out_auth)) {
1750 case DCERPC_AUTH_TYPE_NONE:
1751 /* Unauthenticated bind request. */
1752 /* We're finished - no more packets. */
1753 p->auth.auth_type = PIPE_AUTH_TYPE_NONE;
1754 /* We must set the pipe auth_level here also. */
1755 p->auth.auth_level = DCERPC_AUTH_LEVEL_NONE;
1756 p->pipe_bound = True;
1757 /* The session key was initialized from the SMB
1758 * session in make_internal_rpc_pipe_p */
1762 DEBUG(0,("api_pipe_bind_req: unknown auth type %x requested.\n", auth_type ));
1767 * Create the bind response struct.
1770 /* If the requested abstract synt uuid doesn't match our client pipe,
1771 reject the bind_ack & set the transfer interface synt to all 0's,
1772 ver 0 (observed when NT5 attempts to bind to abstract interfaces
1774 Needed when adding entries to a DACL from NT5 - SK */
1776 if(check_bind_req(p, &hdr_rb.rpc_context[0].abstract, &hdr_rb.rpc_context[0].transfer[0],
1777 hdr_rb.rpc_context[0].context_id )) {
1778 init_rpc_hdr_ba(&hdr_ba,
1779 RPC_MAX_PDU_FRAG_LEN,
1780 RPC_MAX_PDU_FRAG_LEN,
1784 &hdr_rb.rpc_context[0].transfer[0]);
1786 /* Rejection reason: abstract syntax not supported */
1787 init_rpc_hdr_ba(&hdr_ba, RPC_MAX_PDU_FRAG_LEN,
1788 RPC_MAX_PDU_FRAG_LEN, assoc_gid,
1789 ack_pipe_name, 0x1, 0x2, 0x1,
1790 &null_ndr_syntax_id);
1791 p->pipe_bound = False;
1798 if(!smb_io_rpc_hdr_ba("", &hdr_ba, &out_hdr_ba, 0)) {
1799 DEBUG(0,("api_pipe_bind_req: marshalling of RPC_HDR_BA failed.\n"));
1804 * Create the header, now we know the length.
1807 if (prs_offset(&out_auth)) {
1808 auth_len = prs_offset(&out_auth) - RPC_HDR_AUTH_LEN;
1811 init_rpc_hdr(&p->hdr, RPC_BINDACK, RPC_FLG_FIRST | RPC_FLG_LAST,
1813 RPC_HEADER_LEN + prs_offset(&out_hdr_ba) + prs_offset(&out_auth),
1817 * Marshall the header into the outgoing PDU.
1820 if(!smb_io_rpc_hdr("", &p->hdr, &p->out_data.frag, 0)) {
1821 DEBUG(0,("api_pipe_bind_req: marshalling of RPC_HDR failed.\n"));
1826 * Now add the RPC_HDR_BA and any auth needed.
1829 if(!prs_append_prs_data(&p->out_data.frag, &out_hdr_ba)) {
1830 DEBUG(0,("api_pipe_bind_req: append of RPC_HDR_BA failed.\n"));
1834 if (auth_len && !prs_append_prs_data( &p->out_data.frag, &out_auth)) {
1835 DEBUG(0,("api_pipe_bind_req: append of auth info failed.\n"));
1840 * Setup the lengths for the initial reply.
1843 p->out_data.data_sent_length = 0;
1844 p->out_data.current_pdu_sent = 0;
1846 prs_mem_free(&out_hdr_ba);
1847 prs_mem_free(&out_auth);
1853 prs_mem_free(&p->out_data.frag);
1854 prs_mem_free(&out_hdr_ba);
1855 prs_mem_free(&out_auth);
1856 return setup_bind_nak(p);
1859 /****************************************************************************
1860 Deal with an alter context call. Can be third part of 3 leg auth request for
1862 ****************************************************************************/
1864 bool api_pipe_alter_context(pipes_struct *p, prs_struct *rpc_in_p)
1868 RPC_HDR_AUTH auth_info;
1870 fstring ack_pipe_name;
1871 prs_struct out_hdr_ba;
1872 prs_struct out_auth;
1875 prs_init_empty(&p->out_data.frag, p->mem_ctx, MARSHALL);
1878 * Marshall directly into the outgoing PDU space. We
1879 * must do this as we need to set to the bind response
1880 * header and are never sending more than one PDU here.
1884 * Setup the memory to marshall the ba header, and the
1888 if(!prs_init(&out_hdr_ba, 1024, p->mem_ctx, MARSHALL)) {
1889 DEBUG(0,("api_pipe_alter_context: malloc out_hdr_ba failed.\n"));
1890 prs_mem_free(&p->out_data.frag);
1894 if(!prs_init(&out_auth, 1024, p->mem_ctx, MARSHALL)) {
1895 DEBUG(0,("api_pipe_alter_context: malloc out_auth failed.\n"));
1896 prs_mem_free(&p->out_data.frag);
1897 prs_mem_free(&out_hdr_ba);
1901 ZERO_STRUCT(hdr_rb);
1903 DEBUG(5,("api_pipe_alter_context: decode request. %d\n", __LINE__));
1905 /* decode the alter context request */
1906 if(!smb_io_rpc_hdr_rb("", &hdr_rb, rpc_in_p, 0)) {
1907 DEBUG(0,("api_pipe_alter_context: unable to unmarshall RPC_HDR_RB struct.\n"));
1911 /* secondary address CAN be NULL
1912 * as the specs say it's ignored.
1913 * It MUST be NULL to have the spoolss working.
1915 fstrcpy(ack_pipe_name,"");
1917 DEBUG(5,("api_pipe_alter_context: make response. %d\n", __LINE__));
1920 * Check if this is an authenticated alter context request.
1923 if (p->hdr.auth_len != 0) {
1925 * Decode the authentication verifier.
1928 if(!smb_io_rpc_hdr_auth("", &auth_info, rpc_in_p, 0)) {
1929 DEBUG(0,("api_pipe_alter_context: unable to unmarshall RPC_HDR_AUTH struct.\n"));
1934 * Currently only the SPNEGO auth type uses the alter ctx
1935 * response in place of the NTLMSSP auth3 type.
1938 if (auth_info.auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1939 /* We can only finish if the pipe is unbound. */
1940 if (!p->pipe_bound) {
1941 if (!pipe_spnego_auth_bind_continue(p, rpc_in_p, &auth_info, &out_auth)) {
1949 ZERO_STRUCT(auth_info);
1952 assoc_gid = hdr_rb.bba.assoc_gid ? hdr_rb.bba.assoc_gid : 0x53f0;
1955 * Create the bind response struct.
1958 /* If the requested abstract synt uuid doesn't match our client pipe,
1959 reject the bind_ack & set the transfer interface synt to all 0's,
1960 ver 0 (observed when NT5 attempts to bind to abstract interfaces
1962 Needed when adding entries to a DACL from NT5 - SK */
1964 if(check_bind_req(p, &hdr_rb.rpc_context[0].abstract, &hdr_rb.rpc_context[0].transfer[0],
1965 hdr_rb.rpc_context[0].context_id )) {
1966 init_rpc_hdr_ba(&hdr_ba,
1967 RPC_MAX_PDU_FRAG_LEN,
1968 RPC_MAX_PDU_FRAG_LEN,
1972 &hdr_rb.rpc_context[0].transfer[0]);
1974 /* Rejection reason: abstract syntax not supported */
1975 init_rpc_hdr_ba(&hdr_ba, RPC_MAX_PDU_FRAG_LEN,
1976 RPC_MAX_PDU_FRAG_LEN, assoc_gid,
1977 ack_pipe_name, 0x1, 0x2, 0x1,
1978 &null_ndr_syntax_id);
1979 p->pipe_bound = False;
1986 if(!smb_io_rpc_hdr_ba("", &hdr_ba, &out_hdr_ba, 0)) {
1987 DEBUG(0,("api_pipe_alter_context: marshalling of RPC_HDR_BA failed.\n"));
1992 * Create the header, now we know the length.
1995 if (prs_offset(&out_auth)) {
1996 auth_len = prs_offset(&out_auth) - RPC_HDR_AUTH_LEN;
1999 init_rpc_hdr(&p->hdr, RPC_ALTCONTRESP, RPC_FLG_FIRST | RPC_FLG_LAST,
2001 RPC_HEADER_LEN + prs_offset(&out_hdr_ba) + prs_offset(&out_auth),
2005 * Marshall the header into the outgoing PDU.
2008 if(!smb_io_rpc_hdr("", &p->hdr, &p->out_data.frag, 0)) {
2009 DEBUG(0,("api_pipe_alter_context: marshalling of RPC_HDR failed.\n"));
2014 * Now add the RPC_HDR_BA and any auth needed.
2017 if(!prs_append_prs_data(&p->out_data.frag, &out_hdr_ba)) {
2018 DEBUG(0,("api_pipe_alter_context: append of RPC_HDR_BA failed.\n"));
2022 if (auth_len && !prs_append_prs_data(&p->out_data.frag, &out_auth)) {
2023 DEBUG(0,("api_pipe_alter_context: append of auth info failed.\n"));
2028 * Setup the lengths for the initial reply.
2031 p->out_data.data_sent_length = 0;
2032 p->out_data.current_pdu_sent = 0;
2034 prs_mem_free(&out_hdr_ba);
2035 prs_mem_free(&out_auth);
2041 prs_mem_free(&p->out_data.frag);
2042 prs_mem_free(&out_hdr_ba);
2043 prs_mem_free(&out_auth);
2044 return setup_bind_nak(p);
2047 /****************************************************************************
2048 Deal with NTLMSSP sign & seal processing on an RPC request.
2049 ****************************************************************************/
2051 bool api_pipe_ntlmssp_auth_process(pipes_struct *p, prs_struct *rpc_in,
2052 uint32 *p_ss_padding_len, NTSTATUS *pstatus)
2054 RPC_HDR_AUTH auth_info;
2055 uint32 auth_len = p->hdr.auth_len;
2056 uint32 save_offset = prs_offset(rpc_in);
2057 AUTH_NTLMSSP_STATE *a = p->auth.a_u.auth_ntlmssp_state;
2058 unsigned char *data = NULL;
2060 unsigned char *full_packet_data = NULL;
2061 size_t full_packet_data_len;
2062 DATA_BLOB auth_blob;
2064 *pstatus = NT_STATUS_OK;
2066 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_NONE || p->auth.auth_level == DCERPC_AUTH_LEVEL_CONNECT) {
2071 *pstatus = NT_STATUS_INVALID_PARAMETER;
2075 /* Ensure there's enough data for an authenticated request. */
2076 if ((auth_len > RPC_MAX_SIGN_SIZE) ||
2077 (RPC_HEADER_LEN + RPC_HDR_REQ_LEN + RPC_HDR_AUTH_LEN + auth_len > p->hdr.frag_len)) {
2078 DEBUG(0,("api_pipe_ntlmssp_auth_process: auth_len %u is too large.\n",
2079 (unsigned int)auth_len ));
2080 *pstatus = NT_STATUS_INVALID_PARAMETER;
2085 * We need the full packet data + length (minus auth stuff) as well as the packet data + length
2086 * after the RPC header.
2087 * We need to pass in the full packet (minus auth len) to the NTLMSSP sign and check seal
2088 * functions as NTLMv2 checks the rpc headers also.
2091 data = (unsigned char *)(prs_data_p(rpc_in) + RPC_HDR_REQ_LEN);
2092 data_len = (size_t)(p->hdr.frag_len - RPC_HEADER_LEN - RPC_HDR_REQ_LEN - RPC_HDR_AUTH_LEN - auth_len);
2094 full_packet_data = p->in_data.current_in_pdu;
2095 full_packet_data_len = p->hdr.frag_len - auth_len;
2097 /* Pull the auth header and the following data into a blob. */
2098 if(!prs_set_offset(rpc_in, RPC_HDR_REQ_LEN + data_len)) {
2099 DEBUG(0,("api_pipe_ntlmssp_auth_process: cannot move offset to %u.\n",
2100 (unsigned int)RPC_HDR_REQ_LEN + (unsigned int)data_len ));
2101 *pstatus = NT_STATUS_INVALID_PARAMETER;
2105 if(!smb_io_rpc_hdr_auth("hdr_auth", &auth_info, rpc_in, 0)) {
2106 DEBUG(0,("api_pipe_ntlmssp_auth_process: failed to unmarshall RPC_HDR_AUTH.\n"));
2107 *pstatus = NT_STATUS_INVALID_PARAMETER;
2111 auth_blob.data = (unsigned char *)prs_data_p(rpc_in) + prs_offset(rpc_in);
2112 auth_blob.length = auth_len;
2114 switch (p->auth.auth_level) {
2115 case DCERPC_AUTH_LEVEL_PRIVACY:
2116 /* Data is encrypted. */
2117 *pstatus = ntlmssp_unseal_packet(a->ntlmssp_state,
2120 full_packet_data_len,
2122 if (!NT_STATUS_IS_OK(*pstatus)) {
2126 case DCERPC_AUTH_LEVEL_INTEGRITY:
2127 /* Data is signed. */
2128 *pstatus = ntlmssp_check_packet(a->ntlmssp_state,
2131 full_packet_data_len,
2133 if (!NT_STATUS_IS_OK(*pstatus)) {
2138 *pstatus = NT_STATUS_INVALID_PARAMETER;
2143 * Return the current pointer to the data offset.
2146 if(!prs_set_offset(rpc_in, save_offset)) {
2147 DEBUG(0,("api_pipe_auth_process: failed to set offset back to %u\n",
2148 (unsigned int)save_offset ));
2149 *pstatus = NT_STATUS_INVALID_PARAMETER;
2154 * Remember the padding length. We must remove it from the real data
2155 * stream once the sign/seal is done.
2158 *p_ss_padding_len = auth_info.auth_pad_len;
2163 /****************************************************************************
2164 Deal with schannel processing on an RPC request.
2165 ****************************************************************************/
2167 bool api_pipe_schannel_process(pipes_struct *p, prs_struct *rpc_in, uint32 *p_ss_padding_len)
2171 uint32 save_offset = prs_offset(rpc_in);
2172 RPC_HDR_AUTH auth_info;
2173 struct NL_AUTH_SIGNATURE schannel_chk;
2174 enum ndr_err_code ndr_err;
2178 auth_len = p->hdr.auth_len;
2180 if (auth_len < RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN ||
2181 auth_len > RPC_HEADER_LEN +
2185 DEBUG(0,("Incorrect auth_len %u.\n", (unsigned int)auth_len ));
2190 * The following is that length of the data we must verify or unseal.
2191 * This doesn't include the RPC headers or the auth_len or the RPC_HDR_AUTH_LEN
2192 * preceeding the auth_data.
2195 if (p->hdr.frag_len < RPC_HEADER_LEN + RPC_HDR_REQ_LEN + RPC_HDR_AUTH_LEN + auth_len) {
2196 DEBUG(0,("Incorrect frag %u, auth %u.\n",
2197 (unsigned int)p->hdr.frag_len,
2198 (unsigned int)auth_len ));
2202 data_len = p->hdr.frag_len - RPC_HEADER_LEN - RPC_HDR_REQ_LEN -
2203 RPC_HDR_AUTH_LEN - auth_len;
2205 DEBUG(5,("data %d auth %d\n", data_len, auth_len));
2207 if(!prs_set_offset(rpc_in, RPC_HDR_REQ_LEN + data_len)) {
2208 DEBUG(0,("cannot move offset to %u.\n",
2209 (unsigned int)RPC_HDR_REQ_LEN + data_len ));
2213 if(!smb_io_rpc_hdr_auth("hdr_auth", &auth_info, rpc_in, 0)) {
2214 DEBUG(0,("failed to unmarshall RPC_HDR_AUTH.\n"));
2218 if (auth_info.auth_type != DCERPC_AUTH_TYPE_SCHANNEL) {
2219 DEBUG(0,("Invalid auth info %d on schannel\n",
2220 auth_info.auth_type));
2224 blob = data_blob_const(prs_data_p(rpc_in) + prs_offset(rpc_in), auth_len);
2226 ndr_err = ndr_pull_struct_blob(&blob, talloc_tos(), NULL, &schannel_chk,
2227 (ndr_pull_flags_fn_t)ndr_pull_NL_AUTH_SIGNATURE);
2228 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2229 DEBUG(0,("failed to pull NL_AUTH_SIGNATURE\n"));
2230 dump_data(2, blob.data, blob.length);
2234 if (DEBUGLEVEL >= 10) {
2235 NDR_PRINT_DEBUG(NL_AUTH_SIGNATURE, &schannel_chk);
2238 switch (auth_info.auth_level) {
2239 case DCERPC_AUTH_LEVEL_PRIVACY:
2240 status = schannel_unseal_packet(p->auth.a_u.schannel_auth,
2242 (uint8_t *)prs_data_p(rpc_in)+RPC_HDR_REQ_LEN,
2246 case DCERPC_AUTH_LEVEL_INTEGRITY:
2247 status = schannel_check_packet(p->auth.a_u.schannel_auth,
2249 (uint8_t *)prs_data_p(rpc_in)+RPC_HDR_REQ_LEN,
2254 status = NT_STATUS_INTERNAL_ERROR;
2258 if (!NT_STATUS_IS_OK(status)) {
2259 DEBUG(0,("failed to unseal packet: %s\n", nt_errstr(status)));
2264 * Return the current pointer to the data offset.
2267 if(!prs_set_offset(rpc_in, save_offset)) {
2268 DEBUG(0,("failed to set offset back to %u\n",
2269 (unsigned int)save_offset ));
2274 * Remember the padding length. We must remove it from the real data
2275 * stream once the sign/seal is done.
2278 *p_ss_padding_len = auth_info.auth_pad_len;
2283 /****************************************************************************
2284 Find the set of RPC functions associated with this context_id
2285 ****************************************************************************/
2287 static PIPE_RPC_FNS* find_pipe_fns_by_context( PIPE_RPC_FNS *list, uint32 context_id )
2289 PIPE_RPC_FNS *fns = NULL;
2292 DEBUG(0,("find_pipe_fns_by_context: ERROR! No context list for pipe!\n"));
2296 for (fns=list; fns; fns=fns->next ) {
2297 if ( fns->context_id == context_id )
2303 /****************************************************************************
2305 ****************************************************************************/
2307 void free_pipe_rpc_context( PIPE_RPC_FNS *list )
2309 PIPE_RPC_FNS *tmp = list;
2321 static bool api_rpcTNP(pipes_struct *p,
2322 const struct api_struct *api_rpc_cmds, int n_cmds);
2324 /****************************************************************************
2325 Find the correct RPC function to call for this request.
2326 If the pipe is authenticated then become the correct UNIX user
2327 before doing the call.
2328 ****************************************************************************/
2330 bool api_pipe_request(pipes_struct *p)
2333 bool changed_user = False;
2334 PIPE_RPC_FNS *pipe_fns;
2336 if (p->pipe_bound &&
2337 ((p->auth.auth_type == PIPE_AUTH_TYPE_NTLMSSP) ||
2338 (p->auth.auth_type == PIPE_AUTH_TYPE_SPNEGO_NTLMSSP))) {
2339 if(!become_authenticated_pipe_user(p)) {
2340 prs_mem_free(&p->out_data.rdata);
2343 changed_user = True;
2346 DEBUG(5, ("Requested \\PIPE\\%s\n",
2347 get_pipe_name_from_iface(&p->syntax)));
2349 /* get the set of RPC functions for this context */
2351 pipe_fns = find_pipe_fns_by_context(p->contexts, p->hdr_req.context_id);
2354 TALLOC_CTX *frame = talloc_stackframe();
2355 ret = api_rpcTNP(p, pipe_fns->cmds, pipe_fns->n_cmds);
2359 DEBUG(0,("api_pipe_request: No rpc function table associated with context [%d] on pipe [%s]\n",
2360 p->hdr_req.context_id,
2361 get_pipe_name_from_iface(&p->syntax)));
2365 unbecome_authenticated_pipe_user();
2371 /*******************************************************************
2372 Calls the underlying RPC function for a named pipe.
2373 ********************************************************************/
2375 static bool api_rpcTNP(pipes_struct *p,
2376 const struct api_struct *api_rpc_cmds, int n_cmds)
2379 uint32 offset1, offset2;
2381 /* interpret the command */
2382 DEBUG(4,("api_rpcTNP: %s op 0x%x - ",
2383 get_pipe_name_from_iface(&p->syntax), p->hdr_req.opnum));
2385 if (DEBUGLEVEL >= 50) {
2387 slprintf(name, sizeof(name)-1, "in_%s",
2388 get_pipe_name_from_iface(&p->syntax));
2389 prs_dump(name, p->hdr_req.opnum, &p->in_data.data);
2392 for (fn_num = 0; fn_num < n_cmds; fn_num++) {
2393 if (api_rpc_cmds[fn_num].opnum == p->hdr_req.opnum && api_rpc_cmds[fn_num].fn != NULL) {
2394 DEBUG(3,("api_rpcTNP: rpc command: %s\n", api_rpc_cmds[fn_num].name));
2399 if (fn_num == n_cmds) {
2401 * For an unknown RPC just return a fault PDU but
2402 * return True to allow RPC's on the pipe to continue
2403 * and not put the pipe into fault state. JRA.
2405 DEBUG(4, ("unknown\n"));
2406 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
2410 offset1 = prs_offset(&p->out_data.rdata);
2412 DEBUG(6, ("api_rpc_cmds[%d].fn == %p\n",
2413 fn_num, api_rpc_cmds[fn_num].fn));
2414 /* do the actual command */
2415 if(!api_rpc_cmds[fn_num].fn(p)) {
2416 DEBUG(0,("api_rpcTNP: %s: %s failed.\n",
2417 get_pipe_name_from_iface(&p->syntax),
2418 api_rpc_cmds[fn_num].name));
2419 prs_mem_free(&p->out_data.rdata);
2423 if (p->bad_handle_fault_state) {
2424 DEBUG(4,("api_rpcTNP: bad handle fault return.\n"));
2425 p->bad_handle_fault_state = False;
2426 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_CONTEXT_MISMATCH));
2430 if (p->rng_fault_state) {
2431 DEBUG(4, ("api_rpcTNP: rng fault return\n"));
2432 p->rng_fault_state = False;
2433 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
2437 offset2 = prs_offset(&p->out_data.rdata);
2438 prs_set_offset(&p->out_data.rdata, offset1);
2439 if (DEBUGLEVEL >= 50) {
2441 slprintf(name, sizeof(name)-1, "out_%s",
2442 get_pipe_name_from_iface(&p->syntax));
2443 prs_dump(name, p->hdr_req.opnum, &p->out_data.rdata);
2445 prs_set_offset(&p->out_data.rdata, offset2);
2447 DEBUG(5,("api_rpcTNP: called %s successfully\n",
2448 get_pipe_name_from_iface(&p->syntax)));
2450 /* Check for buffer underflow in rpc parsing */
2452 if ((DEBUGLEVEL >= 10) &&
2453 (prs_offset(&p->in_data.data) != prs_data_size(&p->in_data.data))) {
2454 size_t data_len = prs_data_size(&p->in_data.data) - prs_offset(&p->in_data.data);
2455 char *data = (char *)SMB_MALLOC(data_len);
2457 DEBUG(10, ("api_rpcTNP: rpc input buffer underflow (parse error?)\n"));
2459 prs_uint8s(False, "", &p->in_data.data, 0, (unsigned char *)data, (uint32)data_len);