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"
34 extern struct current_user current_user
;
37 #define DBGC_CLASS DBGC_RPC_SRV
39 static void free_pipe_ntlmssp_auth_data(struct pipe_auth_data
*auth
)
41 AUTH_NTLMSSP_STATE
*a
= auth
->a_u
.auth_ntlmssp_state
;
46 auth
->a_u
.auth_ntlmssp_state
= NULL
;
49 static DATA_BLOB
generic_session_key(void)
51 return data_blob("SystemLibraryDTC", 16);
54 /*******************************************************************
55 Generate the next PDU to be returned from the data in p->rdata.
57 ********************************************************************/
59 static bool create_next_pdu_ntlmssp(pipes_struct
*p
)
61 RPC_HDR_RESP hdr_resp
;
62 uint32 ss_padding_len
= 0;
63 uint32 data_space_available
;
68 RPC_HDR_AUTH auth_info
;
69 uint8 auth_type
, auth_level
;
70 AUTH_NTLMSSP_STATE
*a
= p
->auth
.a_u
.auth_ntlmssp_state
;
73 * If we're in the fault state, keep returning fault PDU's until
74 * the pipe gets closed. JRA.
78 setup_fault_pdu(p
, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR
));
82 memset((char *)&hdr_resp
, '\0', sizeof(hdr_resp
));
84 /* Change the incoming request header to a response. */
85 p
->hdr
.pkt_type
= RPC_RESPONSE
;
87 /* Set up rpc header flags. */
88 if (p
->out_data
.data_sent_length
== 0) {
89 p
->hdr
.flags
= RPC_FLG_FIRST
;
95 * Work out how much we can fit in a single PDU.
98 data_len_left
= prs_offset(&p
->out_data
.rdata
) - 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 data_space_available
= RPC_MAX_PDU_FRAG_LEN
- RPC_HEADER_LEN
110 - RPC_HDR_RESP_LEN
- RPC_HDR_AUTH_LEN
- NTLMSSP_SIG_SIZE
;
113 * The amount we send is the minimum of the available
114 * space and the amount left to send.
117 data_len
= MIN(data_len_left
, data_space_available
);
120 * Set up the alloc hint. This should be the data left to
124 hdr_resp
.alloc_hint
= data_len_left
;
127 * Work out if this PDU will be the last.
130 if(p
->out_data
.data_sent_length
+ data_len
>= prs_offset(&p
->out_data
.rdata
)) {
131 p
->hdr
.flags
|= RPC_FLG_LAST
;
132 if (data_len_left
% 8) {
133 ss_padding_len
= 8 - (data_len_left
% 8);
134 DEBUG(10,("create_next_pdu_ntlmssp: adding sign/seal padding of %u\n",
140 * Set up the header lengths.
143 p
->hdr
.frag_len
= RPC_HEADER_LEN
+ RPC_HDR_RESP_LEN
+
144 data_len
+ ss_padding_len
+
145 RPC_HDR_AUTH_LEN
+ NTLMSSP_SIG_SIZE
;
146 p
->hdr
.auth_len
= NTLMSSP_SIG_SIZE
;
150 * Init the parse struct to point at the outgoing
154 prs_init_empty(&p
->out_data
.frag
, p
->mem_ctx
, MARSHALL
);
156 /* Store the header in the data stream. */
157 if(!smb_io_rpc_hdr("hdr", &p
->hdr
, &p
->out_data
.frag
, 0)) {
158 DEBUG(0,("create_next_pdu_ntlmssp: failed to marshall RPC_HDR.\n"));
159 prs_mem_free(&p
->out_data
.frag
);
163 if(!smb_io_rpc_hdr_resp("resp", &hdr_resp
, &p
->out_data
.frag
, 0)) {
164 DEBUG(0,("create_next_pdu_ntlmssp: failed to marshall RPC_HDR_RESP.\n"));
165 prs_mem_free(&p
->out_data
.frag
);
169 /* Copy the data into the PDU. */
171 if(!prs_append_some_prs_data(&p
->out_data
.frag
, &p
->out_data
.rdata
,
172 p
->out_data
.data_sent_length
, data_len
)) {
173 DEBUG(0,("create_next_pdu_ntlmssp: failed to copy %u bytes of data.\n", (unsigned int)data_len
));
174 prs_mem_free(&p
->out_data
.frag
);
178 /* Copy the sign/seal padding data. */
179 if (ss_padding_len
) {
182 memset(pad
, '\0', 8);
183 if (!prs_copy_data_in(&p
->out_data
.frag
, pad
,
185 DEBUG(0,("create_next_pdu_ntlmssp: failed to add %u bytes of pad data.\n",
186 (unsigned int)ss_padding_len
));
187 prs_mem_free(&p
->out_data
.frag
);
193 /* Now write out the auth header and null blob. */
194 if (p
->auth
.auth_type
== PIPE_AUTH_TYPE_NTLMSSP
) {
195 auth_type
= RPC_NTLMSSP_AUTH_TYPE
;
197 auth_type
= RPC_SPNEGO_AUTH_TYPE
;
199 if (p
->auth
.auth_level
== PIPE_AUTH_LEVEL_PRIVACY
) {
200 auth_level
= RPC_AUTH_LEVEL_PRIVACY
;
202 auth_level
= RPC_AUTH_LEVEL_INTEGRITY
;
205 init_rpc_hdr_auth(&auth_info
, auth_type
, auth_level
, ss_padding_len
, 1 /* context id. */);
206 if(!smb_io_rpc_hdr_auth("hdr_auth", &auth_info
, &p
->out_data
.frag
,
208 DEBUG(0,("create_next_pdu_ntlmssp: failed to marshall RPC_HDR_AUTH.\n"));
209 prs_mem_free(&p
->out_data
.frag
);
213 /* Generate the sign blob. */
215 switch (p
->auth
.auth_level
) {
216 case PIPE_AUTH_LEVEL_PRIVACY
:
217 /* Data portion is encrypted. */
218 status
= ntlmssp_seal_packet(
220 (uint8_t *)prs_data_p(&p
->out_data
.frag
)
221 + RPC_HEADER_LEN
+ RPC_HDR_RESP_LEN
,
222 data_len
+ ss_padding_len
,
223 (unsigned char *)prs_data_p(&p
->out_data
.frag
),
224 (size_t)prs_offset(&p
->out_data
.frag
),
226 if (!NT_STATUS_IS_OK(status
)) {
227 data_blob_free(&auth_blob
);
228 prs_mem_free(&p
->out_data
.frag
);
232 case PIPE_AUTH_LEVEL_INTEGRITY
:
233 /* Data is signed. */
234 status
= ntlmssp_sign_packet(
236 (unsigned char *)prs_data_p(&p
->out_data
.frag
)
237 + RPC_HEADER_LEN
+ RPC_HDR_RESP_LEN
,
238 data_len
+ ss_padding_len
,
239 (unsigned char *)prs_data_p(&p
->out_data
.frag
),
240 (size_t)prs_offset(&p
->out_data
.frag
),
242 if (!NT_STATUS_IS_OK(status
)) {
243 data_blob_free(&auth_blob
);
244 prs_mem_free(&p
->out_data
.frag
);
249 prs_mem_free(&p
->out_data
.frag
);
253 /* Append the auth blob. */
254 if (!prs_copy_data_in(&p
->out_data
.frag
, (char *)auth_blob
.data
,
256 DEBUG(0,("create_next_pdu_ntlmssp: failed to add %u bytes auth blob.\n",
257 (unsigned int)NTLMSSP_SIG_SIZE
));
258 data_blob_free(&auth_blob
);
259 prs_mem_free(&p
->out_data
.frag
);
263 data_blob_free(&auth_blob
);
266 * Setup the counts for this PDU.
269 p
->out_data
.data_sent_length
+= data_len
;
270 p
->out_data
.current_pdu_sent
= 0;
275 /*******************************************************************
276 Generate the next PDU to be returned from the data in p->rdata.
277 Return an schannel authenticated fragment.
278 ********************************************************************/
280 static bool create_next_pdu_schannel(pipes_struct
*p
)
282 RPC_HDR_RESP hdr_resp
;
283 uint32 ss_padding_len
= 0;
285 uint32 data_space_available
;
286 uint32 data_len_left
;
290 * If we're in the fault state, keep returning fault PDU's until
291 * the pipe gets closed. JRA.
295 setup_fault_pdu(p
, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR
));
299 memset((char *)&hdr_resp
, '\0', sizeof(hdr_resp
));
301 /* Change the incoming request header to a response. */
302 p
->hdr
.pkt_type
= RPC_RESPONSE
;
304 /* Set up rpc header flags. */
305 if (p
->out_data
.data_sent_length
== 0) {
306 p
->hdr
.flags
= RPC_FLG_FIRST
;
312 * Work out how much we can fit in a single PDU.
315 data_len_left
= prs_offset(&p
->out_data
.rdata
) - p
->out_data
.data_sent_length
;
318 * Ensure there really is data left to send.
322 DEBUG(0,("create_next_pdu_schannel: no data left to send !\n"));
326 data_space_available
= RPC_MAX_PDU_FRAG_LEN
- RPC_HEADER_LEN
327 - RPC_HDR_RESP_LEN
- RPC_HDR_AUTH_LEN
328 - RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN
;
331 * The amount we send is the minimum of the available
332 * space and the amount left to send.
335 data_len
= MIN(data_len_left
, data_space_available
);
338 * Set up the alloc hint. This should be the data left to
342 hdr_resp
.alloc_hint
= data_len_left
;
345 * Work out if this PDU will be the last.
348 if(p
->out_data
.data_sent_length
+ data_len
>= prs_offset(&p
->out_data
.rdata
)) {
349 p
->hdr
.flags
|= RPC_FLG_LAST
;
350 if (data_len_left
% 8) {
351 ss_padding_len
= 8 - (data_len_left
% 8);
352 DEBUG(10,("create_next_pdu_schannel: adding sign/seal padding of %u\n",
357 p
->hdr
.frag_len
= RPC_HEADER_LEN
+ RPC_HDR_RESP_LEN
+ data_len
+ ss_padding_len
+
358 RPC_HDR_AUTH_LEN
+ RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN
;
359 p
->hdr
.auth_len
= RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN
;
362 * Init the parse struct to point at the outgoing
366 prs_init_empty(&p
->out_data
.frag
, p
->mem_ctx
, MARSHALL
);
368 /* Store the header in the data stream. */
369 if(!smb_io_rpc_hdr("hdr", &p
->hdr
, &p
->out_data
.frag
, 0)) {
370 DEBUG(0,("create_next_pdu_schannel: failed to marshall RPC_HDR.\n"));
371 prs_mem_free(&p
->out_data
.frag
);
375 if(!smb_io_rpc_hdr_resp("resp", &hdr_resp
, &p
->out_data
.frag
, 0)) {
376 DEBUG(0,("create_next_pdu_schannel: failed to marshall RPC_HDR_RESP.\n"));
377 prs_mem_free(&p
->out_data
.frag
);
381 /* Store the current offset. */
382 data_pos
= prs_offset(&p
->out_data
.frag
);
384 /* Copy the data into the PDU. */
386 if(!prs_append_some_prs_data(&p
->out_data
.frag
, &p
->out_data
.rdata
,
387 p
->out_data
.data_sent_length
, data_len
)) {
388 DEBUG(0,("create_next_pdu_schannel: failed to copy %u bytes of data.\n", (unsigned int)data_len
));
389 prs_mem_free(&p
->out_data
.frag
);
393 /* Copy the sign/seal padding data. */
394 if (ss_padding_len
) {
396 memset(pad
, '\0', 8);
397 if (!prs_copy_data_in(&p
->out_data
.frag
, pad
,
399 DEBUG(0,("create_next_pdu_schannel: failed to add %u bytes of pad data.\n", (unsigned int)ss_padding_len
));
400 prs_mem_free(&p
->out_data
.frag
);
407 * Schannel processing.
409 RPC_HDR_AUTH auth_info
;
410 struct NL_AUTH_SIGNATURE verf
;
412 enum ndr_err_code ndr_err
;
414 /* Check it's the type of reply we were expecting to decode */
416 init_rpc_hdr_auth(&auth_info
,
417 RPC_SCHANNEL_AUTH_TYPE
,
418 p
->auth
.auth_level
== PIPE_AUTH_LEVEL_PRIVACY
?
419 RPC_AUTH_LEVEL_PRIVACY
: RPC_AUTH_LEVEL_INTEGRITY
,
422 if(!smb_io_rpc_hdr_auth("hdr_auth", &auth_info
,
423 &p
->out_data
.frag
, 0)) {
424 DEBUG(0,("create_next_pdu_schannel: failed to marshall RPC_HDR_AUTH.\n"));
425 prs_mem_free(&p
->out_data
.frag
);
429 schannel_encode(p
->auth
.a_u
.schannel_auth
,
430 p
->auth
.auth_level
, SENDER_IS_ACCEPTOR
, &verf
,
431 prs_data_p(&p
->out_data
.frag
) + data_pos
,
432 data_len
+ ss_padding_len
);
434 /* Finally marshall the blob. */
436 ndr_err
= ndr_push_struct_blob(&blob
, talloc_tos(), NULL
, &verf
,
437 (ndr_push_flags_fn_t
)ndr_push_NL_AUTH_SIGNATURE
);
438 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
439 prs_mem_free(&p
->out_data
.frag
);
443 if (DEBUGLEVEL
>= 10) {
444 NDR_PRINT_DEBUG(NL_AUTH_SIGNATURE
, &verf
);
447 if (!prs_copy_data_in(&p
->out_data
.frag
, (const char *)blob
.data
, blob
.length
)) {
448 prs_mem_free(&p
->out_data
.frag
);
452 p
->auth
.a_u
.schannel_auth
->seq_num
++;
456 * Setup the counts for this PDU.
459 p
->out_data
.data_sent_length
+= data_len
;
460 p
->out_data
.current_pdu_sent
= 0;
465 /*******************************************************************
466 Generate the next PDU to be returned from the data in p->rdata.
467 No authentication done.
468 ********************************************************************/
470 static bool create_next_pdu_noauth(pipes_struct
*p
)
472 RPC_HDR_RESP hdr_resp
;
474 uint32 data_space_available
;
475 uint32 data_len_left
;
478 * If we're in the fault state, keep returning fault PDU's until
479 * the pipe gets closed. JRA.
483 setup_fault_pdu(p
, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR
));
487 memset((char *)&hdr_resp
, '\0', sizeof(hdr_resp
));
489 /* Change the incoming request header to a response. */
490 p
->hdr
.pkt_type
= RPC_RESPONSE
;
492 /* Set up rpc header flags. */
493 if (p
->out_data
.data_sent_length
== 0) {
494 p
->hdr
.flags
= RPC_FLG_FIRST
;
500 * Work out how much we can fit in a single PDU.
503 data_len_left
= prs_offset(&p
->out_data
.rdata
) - p
->out_data
.data_sent_length
;
506 * Ensure there really is data left to send.
510 DEBUG(0,("create_next_pdu_noath: no data left to send !\n"));
514 data_space_available
= RPC_MAX_PDU_FRAG_LEN
- RPC_HEADER_LEN
518 * The amount we send is the minimum of the available
519 * space and the amount left to send.
522 data_len
= MIN(data_len_left
, data_space_available
);
525 * Set up the alloc hint. This should be the data left to
529 hdr_resp
.alloc_hint
= data_len_left
;
532 * Work out if this PDU will be the last.
535 if(p
->out_data
.data_sent_length
+ data_len
>= prs_offset(&p
->out_data
.rdata
)) {
536 p
->hdr
.flags
|= RPC_FLG_LAST
;
540 * Set up the header lengths.
543 p
->hdr
.frag_len
= RPC_HEADER_LEN
+ RPC_HDR_RESP_LEN
+ data_len
;
547 * Init the parse struct to point at the outgoing
551 prs_init_empty(&p
->out_data
.frag
, p
->mem_ctx
, MARSHALL
);
553 /* Store the header in the data stream. */
554 if(!smb_io_rpc_hdr("hdr", &p
->hdr
, &p
->out_data
.frag
, 0)) {
555 DEBUG(0,("create_next_pdu_noath: failed to marshall RPC_HDR.\n"));
556 prs_mem_free(&p
->out_data
.frag
);
560 if(!smb_io_rpc_hdr_resp("resp", &hdr_resp
, &p
->out_data
.frag
, 0)) {
561 DEBUG(0,("create_next_pdu_noath: failed to marshall RPC_HDR_RESP.\n"));
562 prs_mem_free(&p
->out_data
.frag
);
566 /* Copy the data into the PDU. */
568 if(!prs_append_some_prs_data(&p
->out_data
.frag
, &p
->out_data
.rdata
,
569 p
->out_data
.data_sent_length
, data_len
)) {
570 DEBUG(0,("create_next_pdu_noauth: failed to copy %u bytes of data.\n", (unsigned int)data_len
));
571 prs_mem_free(&p
->out_data
.frag
);
576 * Setup the counts for this PDU.
579 p
->out_data
.data_sent_length
+= data_len
;
580 p
->out_data
.current_pdu_sent
= 0;
585 /*******************************************************************
586 Generate the next PDU to be returned from the data in p->rdata.
587 ********************************************************************/
589 bool create_next_pdu(pipes_struct
*p
)
591 switch(p
->auth
.auth_level
) {
592 case PIPE_AUTH_LEVEL_NONE
:
593 case PIPE_AUTH_LEVEL_CONNECT
:
594 /* This is incorrect for auth level connect. Fixme. JRA */
595 return create_next_pdu_noauth(p
);
598 switch(p
->auth
.auth_type
) {
599 case PIPE_AUTH_TYPE_NTLMSSP
:
600 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
:
601 return create_next_pdu_ntlmssp(p
);
602 case PIPE_AUTH_TYPE_SCHANNEL
:
603 return create_next_pdu_schannel(p
);
609 DEBUG(0,("create_next_pdu: invalid internal auth level %u / type %u",
610 (unsigned int)p
->auth
.auth_level
,
611 (unsigned int)p
->auth
.auth_type
));
615 /*******************************************************************
616 Process an NTLMSSP authentication response.
617 If this function succeeds, the user has been authenticated
618 and their domain, name and calling workstation stored in
620 *******************************************************************/
622 static bool pipe_ntlmssp_verify_final(pipes_struct
*p
, DATA_BLOB
*p_resp_blob
)
624 DATA_BLOB session_key
, reply
;
626 AUTH_NTLMSSP_STATE
*a
= p
->auth
.a_u
.auth_ntlmssp_state
;
629 DEBUG(5,("pipe_ntlmssp_verify_final: pipe %s checking user details\n",
630 get_pipe_name_from_iface(&p
->syntax
)));
634 /* this has to be done as root in order to verify the password */
636 status
= auth_ntlmssp_update(a
, *p_resp_blob
, &reply
);
639 /* Don't generate a reply. */
640 data_blob_free(&reply
);
642 if (!NT_STATUS_IS_OK(status
)) {
646 /* Finally - if the pipe negotiated integrity (sign) or privacy (seal)
647 ensure the underlying NTLMSSP flags are also set. If not we should
650 if (p
->auth
.auth_level
== PIPE_AUTH_LEVEL_INTEGRITY
) {
651 if (!(a
->ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_SIGN
)) {
652 DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet integrity requested "
653 "but client declined signing.\n",
654 get_pipe_name_from_iface(&p
->syntax
)));
658 if (p
->auth
.auth_level
== PIPE_AUTH_LEVEL_PRIVACY
) {
659 if (!(a
->ntlmssp_state
->neg_flags
& NTLMSSP_NEGOTIATE_SEAL
)) {
660 DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet privacy requested "
661 "but client declined sealing.\n",
662 get_pipe_name_from_iface(&p
->syntax
)));
667 DEBUG(5, ("pipe_ntlmssp_verify_final: OK: user: %s domain: %s "
668 "workstation: %s\n", a
->ntlmssp_state
->user
,
669 a
->ntlmssp_state
->domain
, a
->ntlmssp_state
->workstation
));
671 if (a
->server_info
->ptok
== NULL
) {
672 DEBUG(1,("Error: Authmodule failed to provide nt_user_token\n"));
676 TALLOC_FREE(p
->server_info
);
678 p
->server_info
= copy_serverinfo(p
, a
->server_info
);
679 if (p
->server_info
== NULL
) {
680 DEBUG(0, ("copy_serverinfo failed\n"));
685 * We're an authenticated bind over smb, so the session key needs to
686 * be set to "SystemLibraryDTC". Weird, but this is what Windows
687 * does. See the RPC-SAMBA3SESSIONKEY.
690 session_key
= generic_session_key();
691 if (session_key
.data
== NULL
) {
695 ret
= server_info_set_session_key(p
->server_info
, session_key
);
697 data_blob_free(&session_key
);
702 /*******************************************************************
703 The switch table for the pipe names and the functions to handle them.
704 *******************************************************************/
711 struct ndr_syntax_id rpc_interface
;
712 const struct api_struct
*cmds
;
716 static struct rpc_table
*rpc_lookup
;
717 static int rpc_lookup_size
;
719 /*******************************************************************
720 This is the "stage3" NTLMSSP response after a bind request and reply.
721 *******************************************************************/
723 bool api_pipe_bind_auth3(pipes_struct
*p
, prs_struct
*rpc_in_p
)
725 RPC_HDR_AUTH auth_info
;
731 DEBUG(5,("api_pipe_bind_auth3: decode request. %d\n", __LINE__
));
733 if (p
->hdr
.auth_len
== 0) {
734 DEBUG(0,("api_pipe_bind_auth3: No auth field sent !\n"));
738 /* 4 bytes padding. */
739 if (!prs_uint32("pad", rpc_in_p
, 0, &pad
)) {
740 DEBUG(0,("api_pipe_bind_auth3: unmarshall of 4 byte pad failed.\n"));
745 * Decode the authentication verifier response.
748 if(!smb_io_rpc_hdr_auth("", &auth_info
, rpc_in_p
, 0)) {
749 DEBUG(0,("api_pipe_bind_auth3: unmarshall of RPC_HDR_AUTH failed.\n"));
753 if (auth_info
.auth_type
!= RPC_NTLMSSP_AUTH_TYPE
) {
754 DEBUG(0,("api_pipe_bind_auth3: incorrect auth type (%u).\n",
755 (unsigned int)auth_info
.auth_type
));
759 blob
= data_blob(NULL
,p
->hdr
.auth_len
);
761 if (!prs_copy_data_out((char *)blob
.data
, rpc_in_p
, p
->hdr
.auth_len
)) {
762 DEBUG(0,("api_pipe_bind_auth3: Failed to pull %u bytes - the response blob.\n",
763 (unsigned int)p
->hdr
.auth_len
));
768 * The following call actually checks the challenge/response data.
769 * for correctness against the given DOMAIN\user name.
772 if (!pipe_ntlmssp_verify_final(p
, &blob
)) {
776 data_blob_free(&blob
);
778 p
->pipe_bound
= True
;
784 data_blob_free(&blob
);
785 free_pipe_ntlmssp_auth_data(&p
->auth
);
786 p
->auth
.a_u
.auth_ntlmssp_state
= NULL
;
791 /*******************************************************************
792 Marshall a bind_nak pdu.
793 *******************************************************************/
795 static bool setup_bind_nak(pipes_struct
*p
)
800 /* Free any memory in the current return data buffer. */
801 prs_mem_free(&p
->out_data
.rdata
);
804 * Marshall directly into the outgoing PDU space. We
805 * must do this as we need to set to the bind response
806 * header and are never sending more than one PDU here.
809 prs_init_empty(&p
->out_data
.frag
, p
->mem_ctx
, MARSHALL
);
812 * Initialize a bind_nak header.
815 init_rpc_hdr(&nak_hdr
, RPC_BINDNACK
, RPC_FLG_FIRST
| RPC_FLG_LAST
,
816 p
->hdr
.call_id
, RPC_HEADER_LEN
+ sizeof(uint16
), 0);
819 * Marshall the header into the outgoing PDU.
822 if(!smb_io_rpc_hdr("", &nak_hdr
, &p
->out_data
.frag
, 0)) {
823 DEBUG(0,("setup_bind_nak: marshalling of RPC_HDR failed.\n"));
824 prs_mem_free(&p
->out_data
.frag
);
829 * Now add the reject reason.
832 if(!prs_uint16("reject code", &p
->out_data
.frag
, 0, &zero
)) {
833 prs_mem_free(&p
->out_data
.frag
);
837 p
->out_data
.data_sent_length
= 0;
838 p
->out_data
.current_pdu_sent
= 0;
840 if (p
->auth
.auth_data_free_func
) {
841 (*p
->auth
.auth_data_free_func
)(&p
->auth
);
843 p
->auth
.auth_level
= PIPE_AUTH_LEVEL_NONE
;
844 p
->auth
.auth_type
= PIPE_AUTH_TYPE_NONE
;
845 p
->pipe_bound
= False
;
850 /*******************************************************************
851 Marshall a fault pdu.
852 *******************************************************************/
854 bool setup_fault_pdu(pipes_struct
*p
, NTSTATUS status
)
857 RPC_HDR_RESP hdr_resp
;
858 RPC_HDR_FAULT fault_resp
;
860 /* Free any memory in the current return data buffer. */
861 prs_mem_free(&p
->out_data
.rdata
);
864 * Marshall directly into the outgoing PDU space. We
865 * must do this as we need to set to the bind response
866 * header and are never sending more than one PDU here.
869 prs_init_empty(&p
->out_data
.frag
, p
->mem_ctx
, MARSHALL
);
872 * Initialize a fault header.
875 init_rpc_hdr(&fault_hdr
, RPC_FAULT
, RPC_FLG_FIRST
| RPC_FLG_LAST
| RPC_FLG_NOCALL
,
876 p
->hdr
.call_id
, RPC_HEADER_LEN
+ RPC_HDR_RESP_LEN
+ RPC_HDR_FAULT_LEN
, 0);
879 * Initialize the HDR_RESP and FAULT parts of the PDU.
882 memset((char *)&hdr_resp
, '\0', sizeof(hdr_resp
));
884 fault_resp
.status
= status
;
885 fault_resp
.reserved
= 0;
888 * Marshall the header into the outgoing PDU.
891 if(!smb_io_rpc_hdr("", &fault_hdr
, &p
->out_data
.frag
, 0)) {
892 DEBUG(0,("setup_fault_pdu: marshalling of RPC_HDR failed.\n"));
893 prs_mem_free(&p
->out_data
.frag
);
897 if(!smb_io_rpc_hdr_resp("resp", &hdr_resp
, &p
->out_data
.frag
, 0)) {
898 DEBUG(0,("setup_fault_pdu: failed to marshall RPC_HDR_RESP.\n"));
899 prs_mem_free(&p
->out_data
.frag
);
903 if(!smb_io_rpc_hdr_fault("fault", &fault_resp
, &p
->out_data
.frag
, 0)) {
904 DEBUG(0,("setup_fault_pdu: failed to marshall RPC_HDR_FAULT.\n"));
905 prs_mem_free(&p
->out_data
.frag
);
909 p
->out_data
.data_sent_length
= 0;
910 p
->out_data
.current_pdu_sent
= 0;
916 /*******************************************************************
917 Marshall a cancel_ack pdu.
918 We should probably check the auth-verifier here.
919 *******************************************************************/
921 bool setup_cancel_ack_reply(pipes_struct
*p
, prs_struct
*rpc_in_p
)
923 prs_struct outgoing_pdu
;
924 RPC_HDR ack_reply_hdr
;
926 /* Free any memory in the current return data buffer. */
927 prs_mem_free(&p
->out_data
.rdata
);
930 * Marshall directly into the outgoing PDU space. We
931 * must do this as we need to set to the bind response
932 * header and are never sending more than one PDU here.
935 prs_init_empty( &outgoing_pdu
, p
->mem_ctx
, MARSHALL
);
936 prs_give_memory( &outgoing_pdu
, (char *)p
->out_data
.current_pdu
, sizeof(p
->out_data
.current_pdu
), False
);
939 * Initialize a cancel_ack header.
942 init_rpc_hdr(&ack_reply_hdr
, RPC_CANCEL_ACK
, RPC_FLG_FIRST
| RPC_FLG_LAST
,
943 p
->hdr
.call_id
, RPC_HEADER_LEN
, 0);
946 * Marshall the header into the outgoing PDU.
949 if(!smb_io_rpc_hdr("", &ack_reply_hdr
, &outgoing_pdu
, 0)) {
950 DEBUG(0,("setup_cancel_ack_reply: marshalling of RPC_HDR failed.\n"));
951 prs_mem_free(&outgoing_pdu
);
955 p
->out_data
.data_sent_length
= 0;
956 p
->out_data
.current_pdu_len
= prs_offset(&outgoing_pdu
);
957 p
->out_data
.current_pdu_sent
= 0;
959 prs_mem_free(&outgoing_pdu
);
964 /*******************************************************************
965 Ensure a bind request has the correct abstract & transfer interface.
966 Used to reject unknown binds from Win2k.
967 *******************************************************************/
969 static bool check_bind_req(struct pipes_struct
*p
,
970 struct ndr_syntax_id
* abstract
,
971 struct ndr_syntax_id
* transfer
,
975 struct pipe_rpc_fns
*context_fns
;
977 DEBUG(3,("check_bind_req for %s\n",
978 get_pipe_name_from_iface(&p
->syntax
)));
980 /* we have to check all now since win2k introduced a new UUID on the lsaprpc pipe */
982 for (i
=0; i
<rpc_lookup_size
; i
++) {
983 DEBUGADD(10, ("checking %s\n", rpc_lookup
[i
].pipe
.clnt
));
984 if (ndr_syntax_id_equal(
985 abstract
, &rpc_lookup
[i
].rpc_interface
)
986 && ndr_syntax_id_equal(
987 transfer
, &ndr_transfer_syntax
)) {
992 if (i
== rpc_lookup_size
) {
996 context_fns
= SMB_MALLOC_P(struct pipe_rpc_fns
);
997 if (context_fns
== NULL
) {
998 DEBUG(0,("check_bind_req: malloc() failed!\n"));
1002 context_fns
->cmds
= rpc_lookup
[i
].cmds
;
1003 context_fns
->n_cmds
= rpc_lookup
[i
].n_cmds
;
1004 context_fns
->context_id
= context_id
;
1006 /* add to the list of open contexts */
1008 DLIST_ADD( p
->contexts
, context_fns
);
1013 /*******************************************************************
1014 Register commands to an RPC pipe
1015 *******************************************************************/
1017 NTSTATUS
rpc_srv_register(int version
, const char *clnt
, const char *srv
,
1018 const struct ndr_interface_table
*iface
,
1019 const struct api_struct
*cmds
, int size
)
1021 struct rpc_table
*rpc_entry
;
1023 if (!clnt
|| !srv
|| !cmds
) {
1024 return NT_STATUS_INVALID_PARAMETER
;
1027 if (version
!= SMB_RPC_INTERFACE_VERSION
) {
1028 DEBUG(0,("Can't register rpc commands!\n"
1029 "You tried to register a rpc module with SMB_RPC_INTERFACE_VERSION %d"
1030 ", while this version of samba uses version %d!\n",
1031 version
,SMB_RPC_INTERFACE_VERSION
));
1032 return NT_STATUS_OBJECT_TYPE_MISMATCH
;
1037 * we still need to make sure that don't register the same commands twice!!!
1042 /* We use a temporary variable because this call can fail and
1043 rpc_lookup will still be valid afterwards. It could then succeed if
1044 called again later */
1046 rpc_entry
= SMB_REALLOC_ARRAY_KEEP_OLD_ON_ERROR(rpc_lookup
, struct rpc_table
, rpc_lookup_size
);
1047 if (NULL
== rpc_entry
) {
1049 DEBUG(0, ("rpc_pipe_register_commands: memory allocation failed\n"));
1050 return NT_STATUS_NO_MEMORY
;
1052 rpc_lookup
= rpc_entry
;
1055 rpc_entry
= rpc_lookup
+ (rpc_lookup_size
- 1);
1056 ZERO_STRUCTP(rpc_entry
);
1057 rpc_entry
->pipe
.clnt
= SMB_STRDUP(clnt
);
1058 rpc_entry
->pipe
.srv
= SMB_STRDUP(srv
);
1059 rpc_entry
->rpc_interface
= iface
->syntax_id
;
1060 rpc_entry
->cmds
= cmds
;
1061 rpc_entry
->n_cmds
= size
;
1063 return NT_STATUS_OK
;
1067 * Is a named pipe known?
1068 * @param[in] cli_filename The pipe name requested by the client
1069 * @result Do we want to serve this?
1071 bool is_known_pipename(const char *cli_filename
, struct ndr_syntax_id
*syntax
)
1073 const char *pipename
= cli_filename
;
1076 if (strnequal(pipename
, "\\PIPE\\", 6)) {
1080 if (*pipename
== '\\') {
1084 if (lp_disable_spoolss() && strequal(pipename
, "spoolss")) {
1085 DEBUG(10, ("refusing spoolss access\n"));
1089 for (i
=0; i
<rpc_lookup_size
; i
++) {
1090 if (strequal(pipename
, rpc_lookup
[i
].pipe
.clnt
)) {
1091 *syntax
= rpc_lookup
[i
].rpc_interface
;
1096 DEBUG(10, ("is_known_pipename: %s unknown\n", cli_filename
));
1100 /*******************************************************************
1101 Handle a SPNEGO krb5 bind auth.
1102 *******************************************************************/
1104 static bool pipe_spnego_auth_bind_kerberos(pipes_struct
*p
, prs_struct
*rpc_in_p
, RPC_HDR_AUTH
*pauth_info
,
1105 DATA_BLOB
*psecblob
, prs_struct
*pout_auth
)
1110 /*******************************************************************
1111 Handle the first part of a SPNEGO bind auth.
1112 *******************************************************************/
1114 static bool pipe_spnego_auth_bind_negotiate(pipes_struct
*p
, prs_struct
*rpc_in_p
,
1115 RPC_HDR_AUTH
*pauth_info
, prs_struct
*pout_auth
)
1121 char *OIDs
[ASN1_MAX_OIDS
];
1124 bool got_kerberos_mechanism
= false;
1125 AUTH_NTLMSSP_STATE
*a
= NULL
;
1126 RPC_HDR_AUTH auth_info
;
1128 ZERO_STRUCT(secblob
);
1130 ZERO_STRUCT(response
);
1132 /* Grab the SPNEGO blob. */
1133 blob
= data_blob(NULL
,p
->hdr
.auth_len
);
1135 if (!prs_copy_data_out((char *)blob
.data
, rpc_in_p
, p
->hdr
.auth_len
)) {
1136 DEBUG(0,("pipe_spnego_auth_bind_negotiate: Failed to pull %u bytes - the SPNEGO auth header.\n",
1137 (unsigned int)p
->hdr
.auth_len
));
1141 if (blob
.data
[0] != ASN1_APPLICATION(0)) {
1145 /* parse out the OIDs and the first sec blob */
1146 if (!parse_negTokenTarg(blob
, OIDs
, &secblob
)) {
1147 DEBUG(0,("pipe_spnego_auth_bind_negotiate: Failed to parse the security blob.\n"));
1151 if (strcmp(OID_KERBEROS5
, OIDs
[0]) == 0 || strcmp(OID_KERBEROS5_OLD
, OIDs
[0]) == 0) {
1152 got_kerberos_mechanism
= true;
1155 for (i
=0;OIDs
[i
];i
++) {
1156 DEBUG(3,("pipe_spnego_auth_bind_negotiate: Got OID %s\n", OIDs
[i
]));
1157 TALLOC_FREE(OIDs
[i
]);
1159 DEBUG(3,("pipe_spnego_auth_bind_negotiate: Got secblob of size %lu\n", (unsigned long)secblob
.length
));
1161 if ( got_kerberos_mechanism
&& ((lp_security()==SEC_ADS
) || USE_KERBEROS_KEYTAB
) ) {
1162 bool ret
= pipe_spnego_auth_bind_kerberos(p
, rpc_in_p
, pauth_info
, &secblob
, pout_auth
);
1163 data_blob_free(&secblob
);
1164 data_blob_free(&blob
);
1168 if (p
->auth
.auth_type
== PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
&& p
->auth
.a_u
.auth_ntlmssp_state
) {
1169 /* Free any previous auth type. */
1170 free_pipe_ntlmssp_auth_data(&p
->auth
);
1173 if (!got_kerberos_mechanism
) {
1174 /* Initialize the NTLM engine. */
1175 status
= auth_ntlmssp_start(&a
);
1176 if (!NT_STATUS_IS_OK(status
)) {
1181 * Pass the first security blob of data to it.
1182 * This can return an error or NT_STATUS_MORE_PROCESSING_REQUIRED
1183 * which means we need another packet to complete the bind.
1186 status
= auth_ntlmssp_update(a
, secblob
, &chal
);
1188 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1189 DEBUG(3,("pipe_spnego_auth_bind_negotiate: auth_ntlmssp_update failed.\n"));
1193 /* Generate the response blob we need for step 2 of the bind. */
1194 response
= spnego_gen_auth_response(&chal
, status
, OID_NTLMSSP
);
1197 * SPNEGO negotiate down to NTLMSSP. The subsequent
1198 * code to process follow-up packets is not complete
1201 response
= spnego_gen_auth_response(NULL
,
1202 NT_STATUS_MORE_PROCESSING_REQUIRED
,
1206 /* Copy the blob into the pout_auth parse struct */
1207 init_rpc_hdr_auth(&auth_info
, RPC_SPNEGO_AUTH_TYPE
, pauth_info
->auth_level
, RPC_HDR_AUTH_LEN
, 1);
1208 if(!smb_io_rpc_hdr_auth("", &auth_info
, pout_auth
, 0)) {
1209 DEBUG(0,("pipe_spnego_auth_bind_negotiate: marshalling of RPC_HDR_AUTH failed.\n"));
1213 if (!prs_copy_data_in(pout_auth
, (char *)response
.data
, response
.length
)) {
1214 DEBUG(0,("pipe_spnego_auth_bind_negotiate: marshalling of data blob failed.\n"));
1218 p
->auth
.a_u
.auth_ntlmssp_state
= a
;
1219 p
->auth
.auth_data_free_func
= &free_pipe_ntlmssp_auth_data
;
1220 p
->auth
.auth_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
1222 data_blob_free(&blob
);
1223 data_blob_free(&secblob
);
1224 data_blob_free(&chal
);
1225 data_blob_free(&response
);
1227 /* We can't set pipe_bound True yet - we need an RPC_ALTER_CONTEXT response packet... */
1232 data_blob_free(&blob
);
1233 data_blob_free(&secblob
);
1234 data_blob_free(&chal
);
1235 data_blob_free(&response
);
1237 p
->auth
.a_u
.auth_ntlmssp_state
= NULL
;
1242 /*******************************************************************
1243 Handle the second part of a SPNEGO bind auth.
1244 *******************************************************************/
1246 static bool pipe_spnego_auth_bind_continue(pipes_struct
*p
, prs_struct
*rpc_in_p
,
1247 RPC_HDR_AUTH
*pauth_info
, prs_struct
*pout_auth
)
1249 RPC_HDR_AUTH auth_info
;
1250 DATA_BLOB spnego_blob
;
1251 DATA_BLOB auth_blob
;
1252 DATA_BLOB auth_reply
;
1254 AUTH_NTLMSSP_STATE
*a
= p
->auth
.a_u
.auth_ntlmssp_state
;
1256 ZERO_STRUCT(spnego_blob
);
1257 ZERO_STRUCT(auth_blob
);
1258 ZERO_STRUCT(auth_reply
);
1259 ZERO_STRUCT(response
);
1262 * NB. If we've negotiated down from krb5 to NTLMSSP we'll currently
1263 * fail here as 'a' == NULL.
1265 if (p
->auth
.auth_type
!= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
|| !a
) {
1266 DEBUG(0,("pipe_spnego_auth_bind_continue: not in NTLMSSP auth state.\n"));
1270 /* Grab the SPNEGO blob. */
1271 spnego_blob
= data_blob(NULL
,p
->hdr
.auth_len
);
1273 if (!prs_copy_data_out((char *)spnego_blob
.data
, rpc_in_p
, p
->hdr
.auth_len
)) {
1274 DEBUG(0,("pipe_spnego_auth_bind_continue: Failed to pull %u bytes - the SPNEGO auth header.\n",
1275 (unsigned int)p
->hdr
.auth_len
));
1279 if (spnego_blob
.data
[0] != ASN1_CONTEXT(1)) {
1280 DEBUG(0,("pipe_spnego_auth_bind_continue: invalid SPNEGO blob type.\n"));
1284 if (!spnego_parse_auth(spnego_blob
, &auth_blob
)) {
1285 DEBUG(0,("pipe_spnego_auth_bind_continue: invalid SPNEGO blob.\n"));
1290 * The following call actually checks the challenge/response data.
1291 * for correctness against the given DOMAIN\user name.
1294 if (!pipe_ntlmssp_verify_final(p
, &auth_blob
)) {
1298 data_blob_free(&spnego_blob
);
1299 data_blob_free(&auth_blob
);
1301 /* Generate the spnego "accept completed" blob - no incoming data. */
1302 response
= spnego_gen_auth_response(&auth_reply
, NT_STATUS_OK
, OID_NTLMSSP
);
1304 /* Copy the blob into the pout_auth parse struct */
1305 init_rpc_hdr_auth(&auth_info
, RPC_SPNEGO_AUTH_TYPE
, pauth_info
->auth_level
, RPC_HDR_AUTH_LEN
, 1);
1306 if(!smb_io_rpc_hdr_auth("", &auth_info
, pout_auth
, 0)) {
1307 DEBUG(0,("pipe_spnego_auth_bind_continue: marshalling of RPC_HDR_AUTH failed.\n"));
1311 if (!prs_copy_data_in(pout_auth
, (char *)response
.data
, response
.length
)) {
1312 DEBUG(0,("pipe_spnego_auth_bind_continue: marshalling of data blob failed.\n"));
1316 data_blob_free(&auth_reply
);
1317 data_blob_free(&response
);
1319 p
->pipe_bound
= True
;
1325 data_blob_free(&spnego_blob
);
1326 data_blob_free(&auth_blob
);
1327 data_blob_free(&auth_reply
);
1328 data_blob_free(&response
);
1330 free_pipe_ntlmssp_auth_data(&p
->auth
);
1331 p
->auth
.a_u
.auth_ntlmssp_state
= NULL
;
1336 /*******************************************************************
1337 Handle an schannel bind auth.
1338 *******************************************************************/
1340 static bool pipe_schannel_auth_bind(pipes_struct
*p
, prs_struct
*rpc_in_p
,
1341 RPC_HDR_AUTH
*pauth_info
, prs_struct
*pout_auth
)
1343 RPC_HDR_AUTH auth_info
;
1344 struct NL_AUTH_MESSAGE neg
;
1345 struct NL_AUTH_MESSAGE reply
;
1348 struct netlogon_creds_CredentialState
*creds
;
1349 DATA_BLOB session_key
;
1350 enum ndr_err_code ndr_err
;
1353 blob
= data_blob_const(prs_data_p(rpc_in_p
) + prs_offset(rpc_in_p
),
1354 prs_data_size(rpc_in_p
));
1356 ndr_err
= ndr_pull_struct_blob(&blob
, talloc_tos(), NULL
, &neg
,
1357 (ndr_pull_flags_fn_t
)ndr_pull_NL_AUTH_MESSAGE
);
1358 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1359 DEBUG(0,("pipe_schannel_auth_bind: Could not unmarshal SCHANNEL auth neg\n"));
1363 if (DEBUGLEVEL
>= 10) {
1364 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE
, &neg
);
1367 if (!(neg
.Flags
& NL_FLAG_OEM_NETBIOS_COMPUTER_NAME
)) {
1368 DEBUG(0,("pipe_schannel_auth_bind: Did not receive netbios computer name\n"));
1373 * The neg.oem_netbios_computer.a key here must match the remote computer name
1374 * given in the DOM_CLNT_SRV.uni_comp_name used on all netlogon pipe
1375 * operations that use credentials.
1379 status
= schannel_fetch_session_key(p
->mem_ctx
,
1380 neg
.oem_netbios_computer
.a
,
1384 if (!NT_STATUS_IS_OK(status
)) {
1385 DEBUG(0, ("pipe_schannel_auth_bind: Attempt to bind using schannel without successful serverauth2\n"));
1389 p
->auth
.a_u
.schannel_auth
= talloc(p
, struct schannel_auth_struct
);
1390 if (!p
->auth
.a_u
.schannel_auth
) {
1395 memset(p
->auth
.a_u
.schannel_auth
->sess_key
, 0, sizeof(p
->auth
.a_u
.schannel_auth
->sess_key
));
1396 memcpy(p
->auth
.a_u
.schannel_auth
->sess_key
, creds
->session_key
,
1397 sizeof(creds
->session_key
));
1401 p
->auth
.a_u
.schannel_auth
->seq_num
= 0;
1404 * JRA. Should we also copy the schannel session key into the pipe session key p->session_key
1405 * here ? We do that for NTLMSSP, but the session key is already set up from the vuser
1406 * struct of the person who opened the pipe. I need to test this further. JRA.
1408 * VL. As we are mapping this to guest set the generic key
1409 * "SystemLibraryDTC" key here. It's a bit difficult to test against
1410 * W2k3, as it does not allow schannel binds against SAMR and LSA
1414 session_key
= generic_session_key();
1415 if (session_key
.data
== NULL
) {
1416 DEBUG(0, ("pipe_schannel_auth_bind: Could not alloc session"
1421 ret
= server_info_set_session_key(p
->server_info
, session_key
);
1423 data_blob_free(&session_key
);
1426 DEBUG(0, ("server_info_set_session_key failed\n"));
1430 init_rpc_hdr_auth(&auth_info
, RPC_SCHANNEL_AUTH_TYPE
, pauth_info
->auth_level
, RPC_HDR_AUTH_LEN
, 1);
1431 if(!smb_io_rpc_hdr_auth("", &auth_info
, pout_auth
, 0)) {
1432 DEBUG(0,("pipe_schannel_auth_bind: marshalling of RPC_HDR_AUTH failed.\n"));
1436 /*** SCHANNEL verifier ***/
1438 reply
.MessageType
= NL_NEGOTIATE_RESPONSE
;
1440 reply
.Buffer
.dummy
= 5; /* ??? actually I don't think
1441 * this has any meaning
1444 ndr_err
= ndr_push_struct_blob(&blob
, talloc_tos(), NULL
, &reply
,
1445 (ndr_push_flags_fn_t
)ndr_push_NL_AUTH_MESSAGE
);
1446 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1447 DEBUG(0,("Failed to marshall NL_AUTH_MESSAGE.\n"));
1451 if (DEBUGLEVEL
>= 10) {
1452 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE
, &reply
);
1455 if (!prs_copy_data_in(pout_auth
, (const char *)blob
.data
, blob
.length
)) {
1459 DEBUG(10,("pipe_schannel_auth_bind: schannel auth: domain [%s] myname [%s]\n",
1460 neg
.oem_netbios_domain
.a
, neg
.oem_netbios_computer
.a
));
1462 /* We're finished with this bind - no more packets. */
1463 p
->auth
.auth_data_free_func
= NULL
;
1464 p
->auth
.auth_type
= PIPE_AUTH_TYPE_SCHANNEL
;
1466 p
->pipe_bound
= True
;
1471 /*******************************************************************
1472 Handle an NTLMSSP bind auth.
1473 *******************************************************************/
1475 static bool pipe_ntlmssp_auth_bind(pipes_struct
*p
, prs_struct
*rpc_in_p
,
1476 RPC_HDR_AUTH
*pauth_info
, prs_struct
*pout_auth
)
1478 RPC_HDR_AUTH auth_info
;
1482 AUTH_NTLMSSP_STATE
*a
= NULL
;
1485 ZERO_STRUCT(response
);
1487 /* Grab the NTLMSSP blob. */
1488 blob
= data_blob(NULL
,p
->hdr
.auth_len
);
1490 if (!prs_copy_data_out((char *)blob
.data
, rpc_in_p
, p
->hdr
.auth_len
)) {
1491 DEBUG(0,("pipe_ntlmssp_auth_bind: Failed to pull %u bytes - the NTLM auth header.\n",
1492 (unsigned int)p
->hdr
.auth_len
));
1496 if (strncmp((char *)blob
.data
, "NTLMSSP", 7) != 0) {
1497 DEBUG(0,("pipe_ntlmssp_auth_bind: Failed to read NTLMSSP in blob\n"));
1501 /* We have an NTLMSSP blob. */
1502 status
= auth_ntlmssp_start(&a
);
1503 if (!NT_STATUS_IS_OK(status
)) {
1504 DEBUG(0,("pipe_ntlmssp_auth_bind: auth_ntlmssp_start failed: %s\n",
1505 nt_errstr(status
) ));
1509 status
= auth_ntlmssp_update(a
, blob
, &response
);
1510 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1511 DEBUG(0,("pipe_ntlmssp_auth_bind: auth_ntlmssp_update failed: %s\n",
1512 nt_errstr(status
) ));
1516 data_blob_free(&blob
);
1518 /* Copy the blob into the pout_auth parse struct */
1519 init_rpc_hdr_auth(&auth_info
, RPC_NTLMSSP_AUTH_TYPE
, pauth_info
->auth_level
, RPC_HDR_AUTH_LEN
, 1);
1520 if(!smb_io_rpc_hdr_auth("", &auth_info
, pout_auth
, 0)) {
1521 DEBUG(0,("pipe_ntlmssp_auth_bind: marshalling of RPC_HDR_AUTH failed.\n"));
1525 if (!prs_copy_data_in(pout_auth
, (char *)response
.data
, response
.length
)) {
1526 DEBUG(0,("pipe_ntlmssp_auth_bind: marshalling of data blob failed.\n"));
1530 p
->auth
.a_u
.auth_ntlmssp_state
= a
;
1531 p
->auth
.auth_data_free_func
= &free_pipe_ntlmssp_auth_data
;
1532 p
->auth
.auth_type
= PIPE_AUTH_TYPE_NTLMSSP
;
1534 data_blob_free(&blob
);
1535 data_blob_free(&response
);
1537 DEBUG(10,("pipe_ntlmssp_auth_bind: NTLMSSP auth started\n"));
1539 /* We can't set pipe_bound True yet - we need an RPC_AUTH3 response packet... */
1544 data_blob_free(&blob
);
1545 data_blob_free(&response
);
1547 free_pipe_ntlmssp_auth_data(&p
->auth
);
1548 p
->auth
.a_u
.auth_ntlmssp_state
= NULL
;
1552 /*******************************************************************
1553 Respond to a pipe bind request.
1554 *******************************************************************/
1556 bool api_pipe_bind_req(pipes_struct
*p
, prs_struct
*rpc_in_p
)
1560 RPC_HDR_AUTH auth_info
;
1562 fstring ack_pipe_name
;
1563 prs_struct out_hdr_ba
;
1564 prs_struct out_auth
;
1567 unsigned int auth_type
= RPC_ANONYMOUS_AUTH_TYPE
;
1569 /* No rebinds on a bound pipe - use alter context. */
1570 if (p
->pipe_bound
) {
1571 DEBUG(2,("api_pipe_bind_req: rejecting bind request on bound "
1572 "pipe %s.\n", get_pipe_name_from_iface(&p
->syntax
)));
1573 return setup_bind_nak(p
);
1576 prs_init_empty(&p
->out_data
.frag
, p
->mem_ctx
, MARSHALL
);
1579 * Marshall directly into the outgoing PDU space. We
1580 * must do this as we need to set to the bind response
1581 * header and are never sending more than one PDU here.
1585 * Setup the memory to marshall the ba header, and the
1589 if(!prs_init(&out_hdr_ba
, 1024, p
->mem_ctx
, MARSHALL
)) {
1590 DEBUG(0,("api_pipe_bind_req: malloc out_hdr_ba failed.\n"));
1591 prs_mem_free(&p
->out_data
.frag
);
1595 if(!prs_init(&out_auth
, 1024, p
->mem_ctx
, MARSHALL
)) {
1596 DEBUG(0,("api_pipe_bind_req: malloc out_auth failed.\n"));
1597 prs_mem_free(&p
->out_data
.frag
);
1598 prs_mem_free(&out_hdr_ba
);
1602 DEBUG(5,("api_pipe_bind_req: decode request. %d\n", __LINE__
));
1604 ZERO_STRUCT(hdr_rb
);
1606 /* decode the bind request */
1608 if(!smb_io_rpc_hdr_rb("", &hdr_rb
, rpc_in_p
, 0)) {
1609 DEBUG(0,("api_pipe_bind_req: unable to unmarshall RPC_HDR_RB "
1614 if (hdr_rb
.num_contexts
== 0) {
1615 DEBUG(0, ("api_pipe_bind_req: no rpc contexts around\n"));
1620 * Try and find the correct pipe name to ensure
1621 * that this is a pipe name we support.
1624 for (i
= 0; i
< rpc_lookup_size
; i
++) {
1625 if (ndr_syntax_id_equal(&rpc_lookup
[i
].rpc_interface
,
1626 &hdr_rb
.rpc_context
[0].abstract
)) {
1627 DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
1628 rpc_lookup
[i
].pipe
.clnt
, rpc_lookup
[i
].pipe
.srv
));
1633 if (i
== rpc_lookup_size
) {
1636 status
= smb_probe_module(
1637 "rpc", get_pipe_name_from_iface(
1638 &hdr_rb
.rpc_context
[0].abstract
));
1640 if (NT_STATUS_IS_ERR(status
)) {
1641 DEBUG(3,("api_pipe_bind_req: Unknown pipe name %s in bind request.\n",
1642 get_pipe_name_from_iface(&hdr_rb
.rpc_context
[0].abstract
)));
1643 prs_mem_free(&p
->out_data
.frag
);
1644 prs_mem_free(&out_hdr_ba
);
1645 prs_mem_free(&out_auth
);
1647 return setup_bind_nak(p
);
1650 for (i
= 0; i
< rpc_lookup_size
; i
++) {
1651 if (strequal(rpc_lookup
[i
].pipe
.clnt
,
1652 get_pipe_name_from_iface(&p
->syntax
))) {
1653 DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
1654 rpc_lookup
[i
].pipe
.clnt
, rpc_lookup
[i
].pipe
.srv
));
1659 if (i
== rpc_lookup_size
) {
1660 DEBUG(0, ("module %s doesn't provide functions for "
1662 get_pipe_name_from_iface(&p
->syntax
),
1663 get_pipe_name_from_iface(&p
->syntax
)));
1668 /* name has to be \PIPE\xxxxx */
1669 fstrcpy(ack_pipe_name
, "\\PIPE\\");
1670 fstrcat(ack_pipe_name
, rpc_lookup
[i
].pipe
.srv
);
1672 DEBUG(5,("api_pipe_bind_req: make response. %d\n", __LINE__
));
1675 * Check if this is an authenticated bind request.
1678 if (p
->hdr
.auth_len
) {
1680 * Decode the authentication verifier.
1683 if(!smb_io_rpc_hdr_auth("", &auth_info
, rpc_in_p
, 0)) {
1684 DEBUG(0,("api_pipe_bind_req: unable to unmarshall RPC_HDR_AUTH struct.\n"));
1688 auth_type
= auth_info
.auth_type
;
1690 /* Work out if we have to sign or seal etc. */
1691 switch (auth_info
.auth_level
) {
1692 case RPC_AUTH_LEVEL_INTEGRITY
:
1693 p
->auth
.auth_level
= PIPE_AUTH_LEVEL_INTEGRITY
;
1695 case RPC_AUTH_LEVEL_PRIVACY
:
1696 p
->auth
.auth_level
= PIPE_AUTH_LEVEL_PRIVACY
;
1699 DEBUG(0,("api_pipe_bind_req: unexpected auth level (%u).\n",
1700 (unsigned int)auth_info
.auth_level
));
1704 ZERO_STRUCT(auth_info
);
1707 assoc_gid
= hdr_rb
.bba
.assoc_gid
? hdr_rb
.bba
.assoc_gid
: 0x53f0;
1710 case RPC_NTLMSSP_AUTH_TYPE
:
1711 if (!pipe_ntlmssp_auth_bind(p
, rpc_in_p
, &auth_info
, &out_auth
)) {
1717 case RPC_SCHANNEL_AUTH_TYPE
:
1718 if (!pipe_schannel_auth_bind(p
, rpc_in_p
, &auth_info
, &out_auth
)) {
1723 case RPC_SPNEGO_AUTH_TYPE
:
1724 if (!pipe_spnego_auth_bind_negotiate(p
, rpc_in_p
, &auth_info
, &out_auth
)) {
1729 case RPC_ANONYMOUS_AUTH_TYPE
:
1730 /* Unauthenticated bind request. */
1731 /* We're finished - no more packets. */
1732 p
->auth
.auth_type
= PIPE_AUTH_TYPE_NONE
;
1733 /* We must set the pipe auth_level here also. */
1734 p
->auth
.auth_level
= PIPE_AUTH_LEVEL_NONE
;
1735 p
->pipe_bound
= True
;
1736 /* The session key was initialized from the SMB
1737 * session in make_internal_rpc_pipe_p */
1741 DEBUG(0,("api_pipe_bind_req: unknown auth type %x requested.\n", auth_type
));
1746 * Create the bind response struct.
1749 /* If the requested abstract synt uuid doesn't match our client pipe,
1750 reject the bind_ack & set the transfer interface synt to all 0's,
1751 ver 0 (observed when NT5 attempts to bind to abstract interfaces
1753 Needed when adding entries to a DACL from NT5 - SK */
1755 if(check_bind_req(p
, &hdr_rb
.rpc_context
[0].abstract
, &hdr_rb
.rpc_context
[0].transfer
[0],
1756 hdr_rb
.rpc_context
[0].context_id
)) {
1757 init_rpc_hdr_ba(&hdr_ba
,
1758 RPC_MAX_PDU_FRAG_LEN
,
1759 RPC_MAX_PDU_FRAG_LEN
,
1763 &hdr_rb
.rpc_context
[0].transfer
[0]);
1765 /* Rejection reason: abstract syntax not supported */
1766 init_rpc_hdr_ba(&hdr_ba
, RPC_MAX_PDU_FRAG_LEN
,
1767 RPC_MAX_PDU_FRAG_LEN
, assoc_gid
,
1768 ack_pipe_name
, 0x1, 0x2, 0x1,
1769 &null_ndr_syntax_id
);
1770 p
->pipe_bound
= False
;
1777 if(!smb_io_rpc_hdr_ba("", &hdr_ba
, &out_hdr_ba
, 0)) {
1778 DEBUG(0,("api_pipe_bind_req: marshalling of RPC_HDR_BA failed.\n"));
1783 * Create the header, now we know the length.
1786 if (prs_offset(&out_auth
)) {
1787 auth_len
= prs_offset(&out_auth
) - RPC_HDR_AUTH_LEN
;
1790 init_rpc_hdr(&p
->hdr
, RPC_BINDACK
, RPC_FLG_FIRST
| RPC_FLG_LAST
,
1792 RPC_HEADER_LEN
+ prs_offset(&out_hdr_ba
) + prs_offset(&out_auth
),
1796 * Marshall the header into the outgoing PDU.
1799 if(!smb_io_rpc_hdr("", &p
->hdr
, &p
->out_data
.frag
, 0)) {
1800 DEBUG(0,("api_pipe_bind_req: marshalling of RPC_HDR failed.\n"));
1805 * Now add the RPC_HDR_BA and any auth needed.
1808 if(!prs_append_prs_data(&p
->out_data
.frag
, &out_hdr_ba
)) {
1809 DEBUG(0,("api_pipe_bind_req: append of RPC_HDR_BA failed.\n"));
1813 if (auth_len
&& !prs_append_prs_data( &p
->out_data
.frag
, &out_auth
)) {
1814 DEBUG(0,("api_pipe_bind_req: append of auth info failed.\n"));
1819 * Setup the lengths for the initial reply.
1822 p
->out_data
.data_sent_length
= 0;
1823 p
->out_data
.current_pdu_sent
= 0;
1825 prs_mem_free(&out_hdr_ba
);
1826 prs_mem_free(&out_auth
);
1832 prs_mem_free(&p
->out_data
.frag
);
1833 prs_mem_free(&out_hdr_ba
);
1834 prs_mem_free(&out_auth
);
1835 return setup_bind_nak(p
);
1838 /****************************************************************************
1839 Deal with an alter context call. Can be third part of 3 leg auth request for
1841 ****************************************************************************/
1843 bool api_pipe_alter_context(pipes_struct
*p
, prs_struct
*rpc_in_p
)
1847 RPC_HDR_AUTH auth_info
;
1849 fstring ack_pipe_name
;
1850 prs_struct out_hdr_ba
;
1851 prs_struct out_auth
;
1854 prs_init_empty(&p
->out_data
.frag
, p
->mem_ctx
, MARSHALL
);
1857 * Marshall directly into the outgoing PDU space. We
1858 * must do this as we need to set to the bind response
1859 * header and are never sending more than one PDU here.
1863 * Setup the memory to marshall the ba header, and the
1867 if(!prs_init(&out_hdr_ba
, 1024, p
->mem_ctx
, MARSHALL
)) {
1868 DEBUG(0,("api_pipe_alter_context: malloc out_hdr_ba failed.\n"));
1869 prs_mem_free(&p
->out_data
.frag
);
1873 if(!prs_init(&out_auth
, 1024, p
->mem_ctx
, MARSHALL
)) {
1874 DEBUG(0,("api_pipe_alter_context: malloc out_auth failed.\n"));
1875 prs_mem_free(&p
->out_data
.frag
);
1876 prs_mem_free(&out_hdr_ba
);
1880 ZERO_STRUCT(hdr_rb
);
1882 DEBUG(5,("api_pipe_alter_context: decode request. %d\n", __LINE__
));
1884 /* decode the alter context request */
1885 if(!smb_io_rpc_hdr_rb("", &hdr_rb
, rpc_in_p
, 0)) {
1886 DEBUG(0,("api_pipe_alter_context: unable to unmarshall RPC_HDR_RB struct.\n"));
1890 /* secondary address CAN be NULL
1891 * as the specs say it's ignored.
1892 * It MUST be NULL to have the spoolss working.
1894 fstrcpy(ack_pipe_name
,"");
1896 DEBUG(5,("api_pipe_alter_context: make response. %d\n", __LINE__
));
1899 * Check if this is an authenticated alter context request.
1902 if (p
->hdr
.auth_len
!= 0) {
1904 * Decode the authentication verifier.
1907 if(!smb_io_rpc_hdr_auth("", &auth_info
, rpc_in_p
, 0)) {
1908 DEBUG(0,("api_pipe_alter_context: unable to unmarshall RPC_HDR_AUTH struct.\n"));
1913 * Currently only the SPNEGO auth type uses the alter ctx
1914 * response in place of the NTLMSSP auth3 type.
1917 if (auth_info
.auth_type
== RPC_SPNEGO_AUTH_TYPE
) {
1918 /* We can only finish if the pipe is unbound. */
1919 if (!p
->pipe_bound
) {
1920 if (!pipe_spnego_auth_bind_continue(p
, rpc_in_p
, &auth_info
, &out_auth
)) {
1928 ZERO_STRUCT(auth_info
);
1931 assoc_gid
= hdr_rb
.bba
.assoc_gid
? hdr_rb
.bba
.assoc_gid
: 0x53f0;
1934 * Create the bind response struct.
1937 /* If the requested abstract synt uuid doesn't match our client pipe,
1938 reject the bind_ack & set the transfer interface synt to all 0's,
1939 ver 0 (observed when NT5 attempts to bind to abstract interfaces
1941 Needed when adding entries to a DACL from NT5 - SK */
1943 if(check_bind_req(p
, &hdr_rb
.rpc_context
[0].abstract
, &hdr_rb
.rpc_context
[0].transfer
[0],
1944 hdr_rb
.rpc_context
[0].context_id
)) {
1945 init_rpc_hdr_ba(&hdr_ba
,
1946 RPC_MAX_PDU_FRAG_LEN
,
1947 RPC_MAX_PDU_FRAG_LEN
,
1951 &hdr_rb
.rpc_context
[0].transfer
[0]);
1953 /* Rejection reason: abstract syntax not supported */
1954 init_rpc_hdr_ba(&hdr_ba
, RPC_MAX_PDU_FRAG_LEN
,
1955 RPC_MAX_PDU_FRAG_LEN
, assoc_gid
,
1956 ack_pipe_name
, 0x1, 0x2, 0x1,
1957 &null_ndr_syntax_id
);
1958 p
->pipe_bound
= False
;
1965 if(!smb_io_rpc_hdr_ba("", &hdr_ba
, &out_hdr_ba
, 0)) {
1966 DEBUG(0,("api_pipe_alter_context: marshalling of RPC_HDR_BA failed.\n"));
1971 * Create the header, now we know the length.
1974 if (prs_offset(&out_auth
)) {
1975 auth_len
= prs_offset(&out_auth
) - RPC_HDR_AUTH_LEN
;
1978 init_rpc_hdr(&p
->hdr
, RPC_ALTCONTRESP
, RPC_FLG_FIRST
| RPC_FLG_LAST
,
1980 RPC_HEADER_LEN
+ prs_offset(&out_hdr_ba
) + prs_offset(&out_auth
),
1984 * Marshall the header into the outgoing PDU.
1987 if(!smb_io_rpc_hdr("", &p
->hdr
, &p
->out_data
.frag
, 0)) {
1988 DEBUG(0,("api_pipe_alter_context: marshalling of RPC_HDR failed.\n"));
1993 * Now add the RPC_HDR_BA and any auth needed.
1996 if(!prs_append_prs_data(&p
->out_data
.frag
, &out_hdr_ba
)) {
1997 DEBUG(0,("api_pipe_alter_context: append of RPC_HDR_BA failed.\n"));
2001 if (auth_len
&& !prs_append_prs_data(&p
->out_data
.frag
, &out_auth
)) {
2002 DEBUG(0,("api_pipe_alter_context: append of auth info failed.\n"));
2007 * Setup the lengths for the initial reply.
2010 p
->out_data
.data_sent_length
= 0;
2011 p
->out_data
.current_pdu_sent
= 0;
2013 prs_mem_free(&out_hdr_ba
);
2014 prs_mem_free(&out_auth
);
2020 prs_mem_free(&p
->out_data
.frag
);
2021 prs_mem_free(&out_hdr_ba
);
2022 prs_mem_free(&out_auth
);
2023 return setup_bind_nak(p
);
2026 /****************************************************************************
2027 Deal with NTLMSSP sign & seal processing on an RPC request.
2028 ****************************************************************************/
2030 bool api_pipe_ntlmssp_auth_process(pipes_struct
*p
, prs_struct
*rpc_in
,
2031 uint32
*p_ss_padding_len
, NTSTATUS
*pstatus
)
2033 RPC_HDR_AUTH auth_info
;
2034 uint32 auth_len
= p
->hdr
.auth_len
;
2035 uint32 save_offset
= prs_offset(rpc_in
);
2036 AUTH_NTLMSSP_STATE
*a
= p
->auth
.a_u
.auth_ntlmssp_state
;
2037 unsigned char *data
= NULL
;
2039 unsigned char *full_packet_data
= NULL
;
2040 size_t full_packet_data_len
;
2041 DATA_BLOB auth_blob
;
2043 *pstatus
= NT_STATUS_OK
;
2045 if (p
->auth
.auth_level
== PIPE_AUTH_LEVEL_NONE
|| p
->auth
.auth_level
== PIPE_AUTH_LEVEL_CONNECT
) {
2050 *pstatus
= NT_STATUS_INVALID_PARAMETER
;
2054 /* Ensure there's enough data for an authenticated request. */
2055 if ((auth_len
> RPC_MAX_SIGN_SIZE
) ||
2056 (RPC_HEADER_LEN
+ RPC_HDR_REQ_LEN
+ RPC_HDR_AUTH_LEN
+ auth_len
> p
->hdr
.frag_len
)) {
2057 DEBUG(0,("api_pipe_ntlmssp_auth_process: auth_len %u is too large.\n",
2058 (unsigned int)auth_len
));
2059 *pstatus
= NT_STATUS_INVALID_PARAMETER
;
2064 * We need the full packet data + length (minus auth stuff) as well as the packet data + length
2065 * after the RPC header.
2066 * We need to pass in the full packet (minus auth len) to the NTLMSSP sign and check seal
2067 * functions as NTLMv2 checks the rpc headers also.
2070 data
= (unsigned char *)(prs_data_p(rpc_in
) + RPC_HDR_REQ_LEN
);
2071 data_len
= (size_t)(p
->hdr
.frag_len
- RPC_HEADER_LEN
- RPC_HDR_REQ_LEN
- RPC_HDR_AUTH_LEN
- auth_len
);
2073 full_packet_data
= p
->in_data
.current_in_pdu
;
2074 full_packet_data_len
= p
->hdr
.frag_len
- auth_len
;
2076 /* Pull the auth header and the following data into a blob. */
2077 if(!prs_set_offset(rpc_in
, RPC_HDR_REQ_LEN
+ data_len
)) {
2078 DEBUG(0,("api_pipe_ntlmssp_auth_process: cannot move offset to %u.\n",
2079 (unsigned int)RPC_HDR_REQ_LEN
+ (unsigned int)data_len
));
2080 *pstatus
= NT_STATUS_INVALID_PARAMETER
;
2084 if(!smb_io_rpc_hdr_auth("hdr_auth", &auth_info
, rpc_in
, 0)) {
2085 DEBUG(0,("api_pipe_ntlmssp_auth_process: failed to unmarshall RPC_HDR_AUTH.\n"));
2086 *pstatus
= NT_STATUS_INVALID_PARAMETER
;
2090 auth_blob
.data
= (unsigned char *)prs_data_p(rpc_in
) + prs_offset(rpc_in
);
2091 auth_blob
.length
= auth_len
;
2093 switch (p
->auth
.auth_level
) {
2094 case PIPE_AUTH_LEVEL_PRIVACY
:
2095 /* Data is encrypted. */
2096 *pstatus
= ntlmssp_unseal_packet(a
->ntlmssp_state
,
2099 full_packet_data_len
,
2101 if (!NT_STATUS_IS_OK(*pstatus
)) {
2105 case PIPE_AUTH_LEVEL_INTEGRITY
:
2106 /* Data is signed. */
2107 *pstatus
= ntlmssp_check_packet(a
->ntlmssp_state
,
2110 full_packet_data_len
,
2112 if (!NT_STATUS_IS_OK(*pstatus
)) {
2117 *pstatus
= NT_STATUS_INVALID_PARAMETER
;
2122 * Return the current pointer to the data offset.
2125 if(!prs_set_offset(rpc_in
, save_offset
)) {
2126 DEBUG(0,("api_pipe_auth_process: failed to set offset back to %u\n",
2127 (unsigned int)save_offset
));
2128 *pstatus
= NT_STATUS_INVALID_PARAMETER
;
2133 * Remember the padding length. We must remove it from the real data
2134 * stream once the sign/seal is done.
2137 *p_ss_padding_len
= auth_info
.auth_pad_len
;
2142 /****************************************************************************
2143 Deal with schannel processing on an RPC request.
2144 ****************************************************************************/
2146 bool api_pipe_schannel_process(pipes_struct
*p
, prs_struct
*rpc_in
, uint32
*p_ss_padding_len
)
2150 uint32 save_offset
= prs_offset(rpc_in
);
2151 RPC_HDR_AUTH auth_info
;
2152 struct NL_AUTH_SIGNATURE schannel_chk
;
2153 enum ndr_err_code ndr_err
;
2156 auth_len
= p
->hdr
.auth_len
;
2158 if (auth_len
< RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN
||
2159 auth_len
> RPC_HEADER_LEN
+
2163 DEBUG(0,("Incorrect auth_len %u.\n", (unsigned int)auth_len
));
2168 * The following is that length of the data we must verify or unseal.
2169 * This doesn't include the RPC headers or the auth_len or the RPC_HDR_AUTH_LEN
2170 * preceeding the auth_data.
2173 if (p
->hdr
.frag_len
< RPC_HEADER_LEN
+ RPC_HDR_REQ_LEN
+ RPC_HDR_AUTH_LEN
+ auth_len
) {
2174 DEBUG(0,("Incorrect frag %u, auth %u.\n",
2175 (unsigned int)p
->hdr
.frag_len
,
2176 (unsigned int)auth_len
));
2180 data_len
= p
->hdr
.frag_len
- RPC_HEADER_LEN
- RPC_HDR_REQ_LEN
-
2181 RPC_HDR_AUTH_LEN
- auth_len
;
2183 DEBUG(5,("data %d auth %d\n", data_len
, auth_len
));
2185 if(!prs_set_offset(rpc_in
, RPC_HDR_REQ_LEN
+ data_len
)) {
2186 DEBUG(0,("cannot move offset to %u.\n",
2187 (unsigned int)RPC_HDR_REQ_LEN
+ data_len
));
2191 if(!smb_io_rpc_hdr_auth("hdr_auth", &auth_info
, rpc_in
, 0)) {
2192 DEBUG(0,("failed to unmarshall RPC_HDR_AUTH.\n"));
2196 if (auth_info
.auth_type
!= RPC_SCHANNEL_AUTH_TYPE
) {
2197 DEBUG(0,("Invalid auth info %d on schannel\n",
2198 auth_info
.auth_type
));
2202 blob
= data_blob_const(prs_data_p(rpc_in
) + prs_offset(rpc_in
), data_len
);
2204 ndr_err
= ndr_pull_struct_blob(&blob
, talloc_tos(), NULL
, &schannel_chk
,
2205 (ndr_pull_flags_fn_t
)ndr_pull_NL_AUTH_SIGNATURE
);
2206 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
2210 if (DEBUGLEVEL
>= 10) {
2211 NDR_PRINT_DEBUG(NL_AUTH_SIGNATURE
, &schannel_chk
);
2214 if (!schannel_decode(p
->auth
.a_u
.schannel_auth
,
2216 SENDER_IS_INITIATOR
,
2218 prs_data_p(rpc_in
)+RPC_HDR_REQ_LEN
, data_len
)) {
2219 DEBUG(3,("failed to decode PDU\n"));
2224 * Return the current pointer to the data offset.
2227 if(!prs_set_offset(rpc_in
, save_offset
)) {
2228 DEBUG(0,("failed to set offset back to %u\n",
2229 (unsigned int)save_offset
));
2233 /* The sequence number gets incremented on both send and receive. */
2234 p
->auth
.a_u
.schannel_auth
->seq_num
++;
2237 * Remember the padding length. We must remove it from the real data
2238 * stream once the sign/seal is done.
2241 *p_ss_padding_len
= auth_info
.auth_pad_len
;
2246 /****************************************************************************
2247 Find the set of RPC functions associated with this context_id
2248 ****************************************************************************/
2250 static PIPE_RPC_FNS
* find_pipe_fns_by_context( PIPE_RPC_FNS
*list
, uint32 context_id
)
2252 PIPE_RPC_FNS
*fns
= NULL
;
2255 DEBUG(0,("find_pipe_fns_by_context: ERROR! No context list for pipe!\n"));
2259 for (fns
=list
; fns
; fns
=fns
->next
) {
2260 if ( fns
->context_id
== context_id
)
2266 /****************************************************************************
2268 ****************************************************************************/
2270 void free_pipe_rpc_context( PIPE_RPC_FNS
*list
)
2272 PIPE_RPC_FNS
*tmp
= list
;
2284 static bool api_rpcTNP(pipes_struct
*p
,
2285 const struct api_struct
*api_rpc_cmds
, int n_cmds
);
2287 /****************************************************************************
2288 Find the correct RPC function to call for this request.
2289 If the pipe is authenticated then become the correct UNIX user
2290 before doing the call.
2291 ****************************************************************************/
2293 bool api_pipe_request(pipes_struct
*p
)
2296 bool changed_user
= False
;
2297 PIPE_RPC_FNS
*pipe_fns
;
2299 if (p
->pipe_bound
&&
2300 ((p
->auth
.auth_type
== PIPE_AUTH_TYPE_NTLMSSP
) ||
2301 (p
->auth
.auth_type
== PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
))) {
2302 if(!become_authenticated_pipe_user(p
)) {
2303 prs_mem_free(&p
->out_data
.rdata
);
2306 changed_user
= True
;
2309 DEBUG(5, ("Requested \\PIPE\\%s\n",
2310 get_pipe_name_from_iface(&p
->syntax
)));
2312 /* get the set of RPC functions for this context */
2314 pipe_fns
= find_pipe_fns_by_context(p
->contexts
, p
->hdr_req
.context_id
);
2317 TALLOC_CTX
*frame
= talloc_stackframe();
2318 ret
= api_rpcTNP(p
, pipe_fns
->cmds
, pipe_fns
->n_cmds
);
2322 DEBUG(0,("api_pipe_request: No rpc function table associated with context [%d] on pipe [%s]\n",
2323 p
->hdr_req
.context_id
,
2324 get_pipe_name_from_iface(&p
->syntax
)));
2328 unbecome_authenticated_pipe_user();
2334 /*******************************************************************
2335 Calls the underlying RPC function for a named pipe.
2336 ********************************************************************/
2338 static bool api_rpcTNP(pipes_struct
*p
,
2339 const struct api_struct
*api_rpc_cmds
, int n_cmds
)
2342 uint32 offset1
, offset2
;
2344 /* interpret the command */
2345 DEBUG(4,("api_rpcTNP: %s op 0x%x - ",
2346 get_pipe_name_from_iface(&p
->syntax
), p
->hdr_req
.opnum
));
2348 if (DEBUGLEVEL
>= 50) {
2350 slprintf(name
, sizeof(name
)-1, "in_%s",
2351 get_pipe_name_from_iface(&p
->syntax
));
2352 prs_dump(name
, p
->hdr_req
.opnum
, &p
->in_data
.data
);
2355 for (fn_num
= 0; fn_num
< n_cmds
; fn_num
++) {
2356 if (api_rpc_cmds
[fn_num
].opnum
== p
->hdr_req
.opnum
&& api_rpc_cmds
[fn_num
].fn
!= NULL
) {
2357 DEBUG(3,("api_rpcTNP: rpc command: %s\n", api_rpc_cmds
[fn_num
].name
));
2362 if (fn_num
== n_cmds
) {
2364 * For an unknown RPC just return a fault PDU but
2365 * return True to allow RPC's on the pipe to continue
2366 * and not put the pipe into fault state. JRA.
2368 DEBUG(4, ("unknown\n"));
2369 setup_fault_pdu(p
, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR
));
2373 offset1
= prs_offset(&p
->out_data
.rdata
);
2375 DEBUG(6, ("api_rpc_cmds[%d].fn == %p\n",
2376 fn_num
, api_rpc_cmds
[fn_num
].fn
));
2377 /* do the actual command */
2378 if(!api_rpc_cmds
[fn_num
].fn(p
)) {
2379 DEBUG(0,("api_rpcTNP: %s: %s failed.\n",
2380 get_pipe_name_from_iface(&p
->syntax
),
2381 api_rpc_cmds
[fn_num
].name
));
2382 prs_mem_free(&p
->out_data
.rdata
);
2386 if (p
->bad_handle_fault_state
) {
2387 DEBUG(4,("api_rpcTNP: bad handle fault return.\n"));
2388 p
->bad_handle_fault_state
= False
;
2389 setup_fault_pdu(p
, NT_STATUS(DCERPC_FAULT_CONTEXT_MISMATCH
));
2393 if (p
->rng_fault_state
) {
2394 DEBUG(4, ("api_rpcTNP: rng fault return\n"));
2395 p
->rng_fault_state
= False
;
2396 setup_fault_pdu(p
, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR
));
2400 offset2
= prs_offset(&p
->out_data
.rdata
);
2401 prs_set_offset(&p
->out_data
.rdata
, offset1
);
2402 if (DEBUGLEVEL
>= 50) {
2404 slprintf(name
, sizeof(name
)-1, "out_%s",
2405 get_pipe_name_from_iface(&p
->syntax
));
2406 prs_dump(name
, p
->hdr_req
.opnum
, &p
->out_data
.rdata
);
2408 prs_set_offset(&p
->out_data
.rdata
, offset2
);
2410 DEBUG(5,("api_rpcTNP: called %s successfully\n",
2411 get_pipe_name_from_iface(&p
->syntax
)));
2413 /* Check for buffer underflow in rpc parsing */
2415 if ((DEBUGLEVEL
>= 10) &&
2416 (prs_offset(&p
->in_data
.data
) != prs_data_size(&p
->in_data
.data
))) {
2417 size_t data_len
= prs_data_size(&p
->in_data
.data
) - prs_offset(&p
->in_data
.data
);
2418 char *data
= (char *)SMB_MALLOC(data_len
);
2420 DEBUG(10, ("api_rpcTNP: rpc input buffer underflow (parse error?)\n"));
2422 prs_uint8s(False
, "", &p
->in_data
.data
, 0, (unsigned char *)data
, (uint32
)data_len
);