2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Almost completely rewritten by (C) Jeremy Allison 2005 - 2010
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 /* this module apparently provides an implementation of DCE/RPC over a
21 * named pipe (IPC$ connection using SMBtrans). details of DCE/RPC
22 * documentation are available (in on-line form) from the X-Open group.
24 * this module should provide a level of abstraction between SMB
25 * and DCE/RPC, while minimising the amount of mallocs, unnecessary
26 * data copies, and network traffic.
31 #include "srv_pipe_internal.h"
32 #include "../librpc/gen_ndr/ndr_schannel.h"
33 #include "../libcli/auth/schannel.h"
34 #include "../libcli/auth/spnego.h"
35 #include "../libcli/auth/ntlmssp.h"
38 #define DBGC_CLASS DBGC_RPC_SRV
40 static void free_pipe_ntlmssp_auth_data(struct pipe_auth_data *auth)
42 struct auth_ntlmssp_state *a = auth->a_u.auth_ntlmssp_state;
47 auth->a_u.auth_ntlmssp_state = NULL;
50 static DATA_BLOB generic_session_key(void)
52 return data_blob("SystemLibraryDTC", 16);
55 /*******************************************************************
57 ********************************************************************/
59 static bool add_ntlmssp_auth(pipes_struct *p)
61 enum dcerpc_AuthLevel auth_level = p->auth.auth_level;
62 DATA_BLOB auth_blob = data_blob_null;
65 /* FIXME: Is this right ?
66 * Keeping only to avoid changing semantics during refactoring
69 if (auth_level != DCERPC_AUTH_LEVEL_PRIVACY) {
70 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
73 /* Generate the auth blob. */
75 case DCERPC_AUTH_LEVEL_PRIVACY:
76 /* Data portion is encrypted. */
77 status = auth_ntlmssp_seal_packet(
78 p->auth.a_u.auth_ntlmssp_state,
79 (TALLOC_CTX *)p->out_data.frag.data,
80 &p->out_data.frag.data[DCERPC_RESPONSE_LENGTH],
81 p->out_data.frag.length
82 - DCERPC_RESPONSE_LENGTH
83 - DCERPC_AUTH_TRAILER_LENGTH,
84 p->out_data.frag.data,
85 p->out_data.frag.length,
89 case DCERPC_AUTH_LEVEL_INTEGRITY:
91 status = auth_ntlmssp_sign_packet(
92 p->auth.a_u.auth_ntlmssp_state,
93 (TALLOC_CTX *)p->out_data.frag.data,
94 &p->out_data.frag.data[DCERPC_RESPONSE_LENGTH],
95 p->out_data.frag.length
96 - DCERPC_RESPONSE_LENGTH
97 - DCERPC_AUTH_TRAILER_LENGTH,
98 p->out_data.frag.data,
99 p->out_data.frag.length,
104 status = NT_STATUS_INTERNAL_ERROR;
108 if (!NT_STATUS_IS_OK(status)) {
109 DEBUG(0, ("Failed to add NTLMSSP auth blob: %s\n",
111 data_blob_free(&p->out_data.frag);
115 /* Finally append the auth blob. */
116 if (!data_blob_append(p->mem_ctx, &p->out_data.frag,
117 auth_blob.data, auth_blob.length)) {
118 DEBUG(0, ("Failed to add %u bytes auth blob.\n",
119 (unsigned int)auth_blob.length));
120 data_blob_free(&p->out_data.frag);
123 data_blob_free(&auth_blob);
128 /*******************************************************************
129 Append a schannel authenticated fragment.
130 ********************************************************************/
132 static bool add_schannel_auth(pipes_struct *p)
134 DATA_BLOB auth_blob = data_blob_null;
137 /* Schannel processing. */
138 switch (p->auth.auth_level) {
139 case DCERPC_AUTH_LEVEL_PRIVACY:
140 status = netsec_outgoing_packet(
141 p->auth.a_u.schannel_auth,
142 (TALLOC_CTX *)p->out_data.frag.data,
144 &p->out_data.frag.data[DCERPC_RESPONSE_LENGTH],
145 p->out_data.frag.length
146 - DCERPC_RESPONSE_LENGTH
147 - DCERPC_AUTH_TRAILER_LENGTH,
151 case DCERPC_AUTH_LEVEL_INTEGRITY:
152 status = netsec_outgoing_packet(
153 p->auth.a_u.schannel_auth,
154 (TALLOC_CTX *)p->out_data.frag.data,
156 &p->out_data.frag.data[DCERPC_RESPONSE_LENGTH],
157 p->out_data.frag.length
158 - DCERPC_RESPONSE_LENGTH
159 - DCERPC_AUTH_TRAILER_LENGTH,
164 status = NT_STATUS_INTERNAL_ERROR;
168 if (!NT_STATUS_IS_OK(status)) {
169 DEBUG(0, ("Failed to add SCHANNEL auth blob: %s\n",
171 data_blob_free(&p->out_data.frag);
175 if (DEBUGLEVEL >= 10) {
176 dump_NL_AUTH_SIGNATURE(talloc_tos(), &auth_blob);
179 if (!data_blob_append(p->mem_ctx, &p->out_data.frag,
180 auth_blob.data, auth_blob.length)) {
181 DEBUG(0, ("Failed to add %u bytes auth blob.\n",
182 (unsigned int)auth_blob.length));
183 data_blob_free(&p->out_data.frag);
186 data_blob_free(&auth_blob);
191 /*******************************************************************
192 Generate the next PDU to be returned from the data.
193 ********************************************************************/
195 static bool create_next_packet(pipes_struct *p,
196 enum dcerpc_AuthType auth_type,
197 enum dcerpc_AuthLevel auth_level,
200 union dcerpc_payload u;
202 size_t data_len_left;
208 ZERO_STRUCT(u.response);
210 /* Set up rpc packet pfc flags. */
211 if (p->out_data.data_sent_length == 0) {
212 pfc_flags = DCERPC_PFC_FLAG_FIRST;
217 /* Work out how much we can fit in a single PDU. */
218 data_len_left = p->out_data.rdata.length -
219 p->out_data.data_sent_length;
221 /* Ensure there really is data left to send. */
222 if (!data_len_left) {
223 DEBUG(0, ("No data left to send !\n"));
227 /* Max space available - not including padding. */
229 max_len = RPC_MAX_PDU_FRAG_LEN
230 - DCERPC_RESPONSE_LENGTH
231 - DCERPC_AUTH_TRAILER_LENGTH
234 max_len = RPC_MAX_PDU_FRAG_LEN - DCERPC_RESPONSE_LENGTH;
238 * The amount we send is the minimum of the max_len
239 * and the amount left to send.
241 data_len = MIN(data_len_left, max_len);
244 /* Work out any padding alignment requirements. */
245 pad_len = (DCERPC_RESPONSE_LENGTH + data_len) %
246 SERVER_NDR_PADDING_SIZE;
248 pad_len = SERVER_NDR_PADDING_SIZE - pad_len;
249 DEBUG(10, ("Padding size is: %d\n", (int)pad_len));
250 /* If we're over filling the packet, we need to make
251 * space for the padding at the end of the data. */
252 if (data_len + pad_len > max_len) {
253 data_len -= SERVER_NDR_PADDING_SIZE;
258 /* Set up the alloc hint. This should be the data left to send. */
259 u.response.alloc_hint = data_len_left;
261 /* Work out if this PDU will be the last. */
262 if (p->out_data.data_sent_length
263 + data_len >= p->out_data.rdata.length) {
264 pfc_flags |= DCERPC_PFC_FLAG_LAST;
267 /* Prepare data to be NDR encoded. */
268 u.response.stub_and_verifier =
269 data_blob_const(p->out_data.rdata.data +
270 p->out_data.data_sent_length, data_len);
272 /* Store the packet in the data stream. */
273 status = dcerpc_push_ncacn_packet(p->mem_ctx,
280 if (!NT_STATUS_IS_OK(status)) {
281 DEBUG(0, ("Failed to marshall RPC Packet.\n"));
286 DATA_BLOB empty = data_blob_null;
289 /* Set the proper length on the pdu, including padding.
290 * Only needed if an auth trailer will be appended. */
291 dcerpc_set_frag_length(&p->out_data.frag,
292 p->out_data.frag.length
294 + DCERPC_AUTH_TRAILER_LENGTH
298 size_t offset = p->out_data.frag.length;
300 if (!data_blob_realloc(p->mem_ctx,
303 DEBUG(0, ("Failed to add padding!\n"));
304 data_blob_free(&p->out_data.frag);
307 memset(&p->out_data.frag.data[offset], '\0', pad_len);
310 /* auth blob is intentionally empty,
311 * it will be appended later */
312 status = dcerpc_push_dcerpc_auth(p->out_data.frag.data,
319 if (!NT_STATUS_IS_OK(status)) {
320 DEBUG(0, ("Failed to marshall RPC Auth.\n"));
324 /* Store auth header in the data stream. */
325 if (!data_blob_append(p->mem_ctx, &p->out_data.frag,
326 auth_hdr.data, auth_hdr.length)) {
327 DEBUG(0, ("Out of memory.\n"));
328 data_blob_free(&p->out_data.frag);
331 data_blob_free(&auth_hdr);
334 /* Setup the counts for this PDU. */
335 p->out_data.data_sent_length += data_len;
336 p->out_data.current_pdu_sent = 0;
340 /*******************************************************************
341 Generate the next PDU to be returned from the data in p->rdata.
342 ********************************************************************/
344 bool create_next_pdu(pipes_struct *p)
346 enum dcerpc_AuthType auth_type =
347 map_pipe_auth_type_to_rpc_auth_type(p->auth.auth_type);
350 * If we're in the fault state, keep returning fault PDU's until
351 * the pipe gets closed. JRA.
353 if (p->fault_state) {
354 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
358 switch (p->auth.auth_level) {
359 case DCERPC_AUTH_LEVEL_NONE:
360 case DCERPC_AUTH_LEVEL_CONNECT:
361 /* This is incorrect for auth level connect. Fixme. JRA */
363 /* No authentication done. */
364 return create_next_packet(p, auth_type,
365 p->auth.auth_level, 0);
367 case DCERPC_AUTH_LEVEL_CALL:
368 case DCERPC_AUTH_LEVEL_PACKET:
369 case DCERPC_AUTH_LEVEL_INTEGRITY:
370 case DCERPC_AUTH_LEVEL_PRIVACY:
372 switch(p->auth.auth_type) {
373 case PIPE_AUTH_TYPE_NTLMSSP:
374 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
375 if (!create_next_packet(p, auth_type,
380 return add_ntlmssp_auth(p);
382 case PIPE_AUTH_TYPE_SCHANNEL:
383 if (!create_next_packet(p, auth_type,
385 SCHANNEL_SIG_SIZE)) {
388 return add_schannel_auth(p);
396 DEBUG(0, ("Invalid internal auth level %u / type %u\n",
397 (unsigned int)p->auth.auth_level,
398 (unsigned int)p->auth.auth_type));
402 /*******************************************************************
403 Process an NTLMSSP authentication response.
404 If this function succeeds, the user has been authenticated
405 and their domain, name and calling workstation stored in
407 *******************************************************************/
409 static bool pipe_ntlmssp_verify_final(pipes_struct *p, DATA_BLOB *p_resp_blob)
411 DATA_BLOB session_key, reply;
413 struct auth_ntlmssp_state *a = p->auth.a_u.auth_ntlmssp_state;
416 DEBUG(5,("pipe_ntlmssp_verify_final: pipe %s checking user details\n",
417 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
421 /* this has to be done as root in order to verify the password */
423 status = auth_ntlmssp_update(a, *p_resp_blob, &reply);
426 /* Don't generate a reply. */
427 data_blob_free(&reply);
429 if (!NT_STATUS_IS_OK(status)) {
433 /* Finally - if the pipe negotiated integrity (sign) or privacy (seal)
434 ensure the underlying NTLMSSP flags are also set. If not we should
437 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_INTEGRITY) {
438 if (!auth_ntlmssp_negotiated_sign(a)) {
439 DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet integrity requested "
440 "but client declined signing.\n",
441 get_pipe_name_from_syntax(talloc_tos(),
446 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
447 if (!auth_ntlmssp_negotiated_seal(a)) {
448 DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet privacy requested "
449 "but client declined sealing.\n",
450 get_pipe_name_from_syntax(talloc_tos(),
456 DEBUG(5, ("pipe_ntlmssp_verify_final: OK: user: %s domain: %s "
458 auth_ntlmssp_get_username(a),
459 auth_ntlmssp_get_domain(a),
460 auth_ntlmssp_get_client(a)));
462 TALLOC_FREE(p->server_info);
464 status = auth_ntlmssp_server_info(p, a, &p->server_info);
465 if (!NT_STATUS_IS_OK(status)) {
466 DEBUG(0, ("auth_ntlmssp_server_info failed to obtain the server info for authenticated user: %s\n",
471 if (p->server_info->ptok == NULL) {
472 DEBUG(1,("Error: Authmodule failed to provide nt_user_token\n"));
477 * We're an authenticated bind over smb, so the session key needs to
478 * be set to "SystemLibraryDTC". Weird, but this is what Windows
479 * does. See the RPC-SAMBA3SESSIONKEY.
482 session_key = generic_session_key();
483 if (session_key.data == NULL) {
487 ret = server_info_set_session_key(p->server_info, session_key);
489 data_blob_free(&session_key);
494 /*******************************************************************
495 This is the "stage3" NTLMSSP response after a bind request and reply.
496 *******************************************************************/
498 bool api_pipe_bind_auth3(pipes_struct *p, struct ncacn_packet *pkt)
500 struct dcerpc_auth auth_info;
501 uint32_t auth_len = pkt->auth_length;
504 DEBUG(5,("api_pipe_bind_auth3: decode request. %d\n", __LINE__));
507 DEBUG(0,("api_pipe_bind_auth3: No auth field sent !\n"));
511 /* Ensure there's enough data for an authenticated request. */
512 if (RPC_HEADER_LEN + RPC_HDR_AUTH_LEN + auth_len >
514 DEBUG(0,("api_pipe_ntlmssp_auth_process: auth_len "
515 "%u is too large.\n",
516 (unsigned int)auth_len ));
521 * Decode the authentication verifier response.
524 status = dcerpc_pull_dcerpc_auth(pkt,
525 &pkt->u.auth3.auth_info,
527 if (!NT_STATUS_IS_OK(status)) {
528 DEBUG(0, ("Failed to unmarshall dcerpc_auth.\n"));
532 /* We must NEVER look at auth_info->auth_pad_len here,
533 * as old Samba client code gets it wrong and sends it
537 if (auth_info.auth_type != DCERPC_AUTH_TYPE_NTLMSSP) {
538 DEBUG(0,("api_pipe_bind_auth3: incorrect auth type (%u).\n",
539 (unsigned int)auth_info.auth_type ));
544 * The following call actually checks the challenge/response data.
545 * for correctness against the given DOMAIN\user name.
548 if (!pipe_ntlmssp_verify_final(p, &auth_info.credentials)) {
552 p->pipe_bound = True;
558 free_pipe_ntlmssp_auth_data(&p->auth);
559 p->auth.a_u.auth_ntlmssp_state = NULL;
564 /*******************************************************************
565 Marshall a bind_nak pdu.
566 *******************************************************************/
568 static bool setup_bind_nak(pipes_struct *p, struct ncacn_packet *pkt)
571 union dcerpc_payload u;
573 /* Free any memory in the current return data buffer. */
574 data_blob_free(&p->out_data.rdata);
577 * Initialize a bind_nak header.
582 u.bind_nak.reject_reason = 0;
585 * Marshall directly into the outgoing PDU space. We
586 * must do this as we need to set to the bind response
587 * header and are never sending more than one PDU here.
590 status = dcerpc_push_ncacn_packet(p->mem_ctx,
592 DCERPC_PFC_FLAG_FIRST |
593 DCERPC_PFC_FLAG_LAST,
598 if (!NT_STATUS_IS_OK(status)) {
602 p->out_data.data_sent_length = 0;
603 p->out_data.current_pdu_sent = 0;
605 if (p->auth.auth_data_free_func) {
606 (*p->auth.auth_data_free_func)(&p->auth);
608 p->auth.auth_level = DCERPC_AUTH_LEVEL_NONE;
609 p->auth.auth_type = PIPE_AUTH_TYPE_NONE;
610 p->pipe_bound = False;
615 /*******************************************************************
616 Marshall a fault pdu.
617 *******************************************************************/
619 bool setup_fault_pdu(pipes_struct *p, NTSTATUS fault_status)
622 union dcerpc_payload u;
624 /* Free any memory in the current return data buffer. */
625 data_blob_free(&p->out_data.rdata);
628 * Initialize a fault header.
633 u.fault.status = NT_STATUS_V(fault_status);
634 u.fault._pad = data_blob_talloc_zero(p->mem_ctx, 4);
637 * Marshall directly into the outgoing PDU space. We
638 * must do this as we need to set to the bind response
639 * header and are never sending more than one PDU here.
642 status = dcerpc_push_ncacn_packet(p->mem_ctx,
644 DCERPC_PFC_FLAG_FIRST |
645 DCERPC_PFC_FLAG_LAST |
646 DCERPC_PFC_FLAG_DID_NOT_EXECUTE,
651 if (!NT_STATUS_IS_OK(status)) {
655 p->out_data.data_sent_length = 0;
656 p->out_data.current_pdu_sent = 0;
661 /*******************************************************************
662 Ensure a bind request has the correct abstract & transfer interface.
663 Used to reject unknown binds from Win2k.
664 *******************************************************************/
666 static bool check_bind_req(struct pipes_struct *p,
667 struct ndr_syntax_id* abstract,
668 struct ndr_syntax_id* transfer,
671 struct pipe_rpc_fns *context_fns;
673 DEBUG(3,("check_bind_req for %s\n",
674 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
676 /* we have to check all now since win2k introduced a new UUID on the lsaprpc pipe */
677 if (rpc_srv_pipe_exists_by_id(abstract) &&
678 ndr_syntax_id_equal(transfer, &ndr_transfer_syntax)) {
679 DEBUG(3, ("check_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
680 rpc_srv_get_pipe_cli_name(abstract),
681 rpc_srv_get_pipe_srv_name(abstract)));
686 context_fns = SMB_MALLOC_P(struct pipe_rpc_fns);
687 if (context_fns == NULL) {
688 DEBUG(0,("check_bind_req: malloc() failed!\n"));
692 context_fns->n_cmds = rpc_srv_get_pipe_num_cmds(abstract);
693 context_fns->cmds = rpc_srv_get_pipe_cmds(abstract);
694 context_fns->context_id = context_id;
696 /* add to the list of open contexts */
698 DLIST_ADD( p->contexts, context_fns );
704 * Is a named pipe known?
705 * @param[in] cli_filename The pipe name requested by the client
706 * @result Do we want to serve this?
708 bool is_known_pipename(const char *cli_filename, struct ndr_syntax_id *syntax)
710 const char *pipename = cli_filename;
713 if (strnequal(pipename, "\\PIPE\\", 6)) {
717 if (*pipename == '\\') {
721 if (lp_disable_spoolss() && strequal(pipename, "spoolss")) {
722 DEBUG(10, ("refusing spoolss access\n"));
726 if (rpc_srv_get_pipe_interface_by_cli_name(pipename, syntax)) {
730 status = smb_probe_module("rpc", pipename);
731 if (!NT_STATUS_IS_OK(status)) {
732 DEBUG(10, ("is_known_pipename: %s unknown\n", cli_filename));
735 DEBUG(10, ("is_known_pipename: %s loaded dynamically\n", pipename));
738 * Scan the list again for the interface id
740 if (rpc_srv_get_pipe_interface_by_cli_name(pipename, syntax)) {
744 DEBUG(10, ("is_known_pipename: pipe %s did not register itself!\n",
750 /*******************************************************************
751 Handle a SPNEGO krb5 bind auth.
752 *******************************************************************/
754 static bool pipe_spnego_auth_bind_kerberos(pipes_struct *p,
756 struct dcerpc_auth *pauth_info,
763 /*******************************************************************
764 Handle the first part of a SPNEGO bind auth.
765 *******************************************************************/
767 static bool pipe_spnego_auth_bind_negotiate(pipes_struct *p,
769 struct dcerpc_auth *pauth_info,
774 char *OIDs[ASN1_MAX_OIDS];
777 bool got_kerberos_mechanism = false;
778 struct auth_ntlmssp_state *a = NULL;
780 ZERO_STRUCT(secblob);
783 if (pauth_info->credentials.data[0] != ASN1_APPLICATION(0)) {
787 /* parse out the OIDs and the first sec blob */
788 if (!parse_negTokenTarg(pauth_info->credentials, OIDs, &secblob)) {
789 DEBUG(0,("pipe_spnego_auth_bind_negotiate: Failed to parse the security blob.\n"));
793 if (strcmp(OID_KERBEROS5, OIDs[0]) == 0 || strcmp(OID_KERBEROS5_OLD, OIDs[0]) == 0) {
794 got_kerberos_mechanism = true;
797 for (i=0;OIDs[i];i++) {
798 DEBUG(3,("pipe_spnego_auth_bind_negotiate: Got OID %s\n", OIDs[i]));
799 TALLOC_FREE(OIDs[i]);
801 DEBUG(3,("pipe_spnego_auth_bind_negotiate: Got secblob of size %lu\n", (unsigned long)secblob.length));
803 if ( got_kerberos_mechanism && ((lp_security()==SEC_ADS) || USE_KERBEROS_KEYTAB) ) {
805 ret = pipe_spnego_auth_bind_kerberos(p, mem_ctx, pauth_info,
807 data_blob_free(&secblob);
811 if (p->auth.auth_type == PIPE_AUTH_TYPE_SPNEGO_NTLMSSP && p->auth.a_u.auth_ntlmssp_state) {
812 /* Free any previous auth type. */
813 free_pipe_ntlmssp_auth_data(&p->auth);
816 if (!got_kerberos_mechanism) {
817 /* Initialize the NTLM engine. */
818 status = auth_ntlmssp_start(&a);
819 if (!NT_STATUS_IS_OK(status)) {
823 switch (pauth_info->auth_level) {
824 case DCERPC_AUTH_LEVEL_INTEGRITY:
825 auth_ntlmssp_want_sign(a);
827 case DCERPC_AUTH_LEVEL_PRIVACY:
828 auth_ntlmssp_want_seal(a);
834 * Pass the first security blob of data to it.
835 * This can return an error or NT_STATUS_MORE_PROCESSING_REQUIRED
836 * which means we need another packet to complete the bind.
839 status = auth_ntlmssp_update(a, secblob, &chal);
841 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
842 DEBUG(3,("pipe_spnego_auth_bind_negotiate: auth_ntlmssp_update failed.\n"));
846 /* Generate the response blob we need for step 2 of the bind. */
847 *response = spnego_gen_auth_response(&chal, status, OID_NTLMSSP);
850 * SPNEGO negotiate down to NTLMSSP. The subsequent
851 * code to process follow-up packets is not complete
854 *response = spnego_gen_auth_response(NULL,
855 NT_STATUS_MORE_PROCESSING_REQUIRED,
859 /* Make sure data is bound to the memctx, to be freed the caller */
860 talloc_steal(mem_ctx, response->data);
862 /* auth_pad_len will be handled by the caller */
864 p->auth.a_u.auth_ntlmssp_state = a;
865 p->auth.auth_data_free_func = &free_pipe_ntlmssp_auth_data;
866 p->auth.auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
868 data_blob_free(&secblob);
869 data_blob_free(&chal);
871 /* We can't set pipe_bound True yet - we need an RPC_ALTER_CONTEXT response packet... */
876 data_blob_free(&secblob);
877 data_blob_free(&chal);
879 p->auth.a_u.auth_ntlmssp_state = NULL;
884 /*******************************************************************
885 Handle the second part of a SPNEGO bind auth.
886 *******************************************************************/
888 static bool pipe_spnego_auth_bind_continue(pipes_struct *p,
890 struct dcerpc_auth *pauth_info,
894 DATA_BLOB auth_reply;
895 struct auth_ntlmssp_state *a = p->auth.a_u.auth_ntlmssp_state;
897 ZERO_STRUCT(auth_blob);
898 ZERO_STRUCT(auth_reply);
901 * NB. If we've negotiated down from krb5 to NTLMSSP we'll currently
902 * fail here as 'a' == NULL.
904 if (p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP || !a) {
905 DEBUG(0,("pipe_spnego_auth_bind_continue: not in NTLMSSP auth state.\n"));
909 if (pauth_info->credentials.data[0] != ASN1_CONTEXT(1)) {
910 DEBUG(0,("pipe_spnego_auth_bind_continue: invalid SPNEGO blob type.\n"));
914 if (!spnego_parse_auth(pauth_info->credentials, &auth_blob)) {
915 DEBUG(0,("pipe_spnego_auth_bind_continue: invalid SPNEGO blob.\n"));
920 * The following call actually checks the challenge/response data.
921 * for correctness against the given DOMAIN\user name.
924 if (!pipe_ntlmssp_verify_final(p, &auth_blob)) {
928 data_blob_free(&auth_blob);
930 /* Generate the spnego "accept completed" blob - no incoming data. */
931 *response = spnego_gen_auth_response(&auth_reply, NT_STATUS_OK, OID_NTLMSSP);
933 /* Make sure data is bound to the memctx, to be freed the caller */
934 talloc_steal(mem_ctx, response->data);
936 data_blob_free(&auth_reply);
938 p->pipe_bound = True;
944 data_blob_free(&auth_blob);
945 data_blob_free(&auth_reply);
947 free_pipe_ntlmssp_auth_data(&p->auth);
948 p->auth.a_u.auth_ntlmssp_state = NULL;
953 /*******************************************************************
954 Handle an schannel bind auth.
955 *******************************************************************/
957 static bool pipe_schannel_auth_bind(pipes_struct *p,
959 struct dcerpc_auth *auth_info,
962 struct NL_AUTH_MESSAGE neg;
963 struct NL_AUTH_MESSAGE reply;
966 struct netlogon_creds_CredentialState *creds;
967 DATA_BLOB session_key;
968 enum ndr_err_code ndr_err;
970 ndr_err = ndr_pull_struct_blob(
971 &auth_info->credentials, mem_ctx, &neg,
972 (ndr_pull_flags_fn_t)ndr_pull_NL_AUTH_MESSAGE);
973 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
974 DEBUG(0,("pipe_schannel_auth_bind: Could not unmarshal SCHANNEL auth neg\n"));
978 if (DEBUGLEVEL >= 10) {
979 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &neg);
982 if (!(neg.Flags & NL_FLAG_OEM_NETBIOS_COMPUTER_NAME)) {
983 DEBUG(0,("pipe_schannel_auth_bind: Did not receive netbios computer name\n"));
988 * The neg.oem_netbios_computer.a key here must match the remote computer name
989 * given in the DOM_CLNT_SRV.uni_comp_name used on all netlogon pipe
990 * operations that use credentials.
994 status = schannel_get_creds_state(p, lp_private_dir(),
995 neg.oem_netbios_computer.a, &creds);
998 if (!NT_STATUS_IS_OK(status)) {
999 DEBUG(0, ("pipe_schannel_auth_bind: Attempt to bind using schannel without successful serverauth2\n"));
1003 p->auth.a_u.schannel_auth = talloc(p, struct schannel_state);
1004 if (!p->auth.a_u.schannel_auth) {
1009 p->auth.a_u.schannel_auth->state = SCHANNEL_STATE_START;
1010 p->auth.a_u.schannel_auth->seq_num = 0;
1011 p->auth.a_u.schannel_auth->initiator = false;
1012 p->auth.a_u.schannel_auth->creds = creds;
1015 * JRA. Should we also copy the schannel session key into the pipe session key p->session_key
1016 * here ? We do that for NTLMSSP, but the session key is already set up from the vuser
1017 * struct of the person who opened the pipe. I need to test this further. JRA.
1019 * VL. As we are mapping this to guest set the generic key
1020 * "SystemLibraryDTC" key here. It's a bit difficult to test against
1021 * W2k3, as it does not allow schannel binds against SAMR and LSA
1025 session_key = generic_session_key();
1026 if (session_key.data == NULL) {
1027 DEBUG(0, ("pipe_schannel_auth_bind: Could not alloc session"
1032 ret = server_info_set_session_key(p->server_info, session_key);
1034 data_blob_free(&session_key);
1037 DEBUG(0, ("server_info_set_session_key failed\n"));
1041 /*** SCHANNEL verifier ***/
1043 reply.MessageType = NL_NEGOTIATE_RESPONSE;
1045 reply.Buffer.dummy = 5; /* ??? actually I don't think
1046 * this has any meaning
1049 ndr_err = ndr_push_struct_blob(response, mem_ctx, &reply,
1050 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
1051 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1052 DEBUG(0,("Failed to marshall NL_AUTH_MESSAGE.\n"));
1056 if (DEBUGLEVEL >= 10) {
1057 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &reply);
1060 DEBUG(10,("pipe_schannel_auth_bind: schannel auth: domain [%s] myname [%s]\n",
1061 neg.oem_netbios_domain.a, neg.oem_netbios_computer.a));
1063 /* We're finished with this bind - no more packets. */
1064 p->auth.auth_data_free_func = NULL;
1065 p->auth.auth_type = PIPE_AUTH_TYPE_SCHANNEL;
1067 p->pipe_bound = True;
1072 /*******************************************************************
1073 Handle an NTLMSSP bind auth.
1074 *******************************************************************/
1076 static bool pipe_ntlmssp_auth_bind(pipes_struct *p,
1077 TALLOC_CTX *mem_ctx,
1078 struct dcerpc_auth *auth_info,
1079 DATA_BLOB *response)
1082 struct auth_ntlmssp_state *a = NULL;
1084 if (strncmp((char *)auth_info->credentials.data, "NTLMSSP", 7) != 0) {
1085 DEBUG(0, ("Failed to read NTLMSSP in blob\n"));
1089 /* We have an NTLMSSP blob. */
1090 status = auth_ntlmssp_start(&a);
1091 if (!NT_STATUS_IS_OK(status)) {
1092 DEBUG(0,("pipe_ntlmssp_auth_bind: auth_ntlmssp_start failed: %s\n",
1093 nt_errstr(status) ));
1097 switch (auth_info->auth_level) {
1098 case DCERPC_AUTH_LEVEL_INTEGRITY:
1099 auth_ntlmssp_want_sign(a);
1101 case DCERPC_AUTH_LEVEL_PRIVACY:
1102 auth_ntlmssp_want_seal(a);
1108 status = auth_ntlmssp_update(a, auth_info->credentials, response);
1109 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1110 DEBUG(0,("pipe_ntlmssp_auth_bind: auth_ntlmssp_update failed: %s\n",
1111 nt_errstr(status) ));
1115 /* Make sure data is bound to the memctx, to be freed the caller */
1116 talloc_steal(mem_ctx, response->data);
1118 p->auth.a_u.auth_ntlmssp_state = a;
1119 p->auth.auth_data_free_func = &free_pipe_ntlmssp_auth_data;
1120 p->auth.auth_type = PIPE_AUTH_TYPE_NTLMSSP;
1122 DEBUG(10,("pipe_ntlmssp_auth_bind: NTLMSSP auth started\n"));
1124 /* We can't set pipe_bound True yet - we need an DCERPC_PKT_AUTH3 response packet... */
1129 free_pipe_ntlmssp_auth_data(&p->auth);
1130 p->auth.a_u.auth_ntlmssp_state = NULL;
1134 /*******************************************************************
1135 Respond to a pipe bind request.
1136 *******************************************************************/
1138 bool api_pipe_bind_req(pipes_struct *p, struct ncacn_packet *pkt)
1140 struct dcerpc_auth auth_info;
1142 unsigned int auth_type = DCERPC_AUTH_TYPE_NONE;
1144 struct ndr_syntax_id id;
1145 union dcerpc_payload u;
1146 struct dcerpc_ack_ctx bind_ack_ctx;
1147 DATA_BLOB auth_resp = data_blob_null;
1148 DATA_BLOB auth_blob = data_blob_null;
1150 /* No rebinds on a bound pipe - use alter context. */
1151 if (p->pipe_bound) {
1152 DEBUG(2,("api_pipe_bind_req: rejecting bind request on bound "
1154 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1155 return setup_bind_nak(p, pkt);
1158 if (pkt->u.bind.num_contexts == 0) {
1159 DEBUG(0, ("api_pipe_bind_req: no rpc contexts around\n"));
1164 * Try and find the correct pipe name to ensure
1165 * that this is a pipe name we support.
1167 id = pkt->u.bind.ctx_list[0].abstract_syntax;
1168 if (rpc_srv_pipe_exists_by_id(&id)) {
1169 DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
1170 rpc_srv_get_pipe_cli_name(&id),
1171 rpc_srv_get_pipe_srv_name(&id)));
1173 status = smb_probe_module(
1174 "rpc", get_pipe_name_from_syntax(
1176 &pkt->u.bind.ctx_list[0].abstract_syntax));
1178 if (NT_STATUS_IS_ERR(status)) {
1179 DEBUG(3,("api_pipe_bind_req: Unknown pipe name %s in bind request.\n",
1180 get_pipe_name_from_syntax(
1182 &pkt->u.bind.ctx_list[0].abstract_syntax)));
1184 return setup_bind_nak(p, pkt);
1187 if (rpc_srv_get_pipe_interface_by_cli_name(
1188 get_pipe_name_from_syntax(talloc_tos(),
1191 DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
1192 rpc_srv_get_pipe_cli_name(&id),
1193 rpc_srv_get_pipe_srv_name(&id)));
1195 DEBUG(0, ("module %s doesn't provide functions for "
1197 get_pipe_name_from_syntax(talloc_tos(),
1199 get_pipe_name_from_syntax(talloc_tos(),
1201 return setup_bind_nak(p, pkt);
1205 DEBUG(5,("api_pipe_bind_req: make response. %d\n", __LINE__));
1207 if (pkt->u.bind.assoc_group_id != 0) {
1208 assoc_gid = pkt->u.bind.assoc_group_id;
1214 * Create the bind response struct.
1217 /* If the requested abstract synt uuid doesn't match our client pipe,
1218 reject the bind_ack & set the transfer interface synt to all 0's,
1219 ver 0 (observed when NT5 attempts to bind to abstract interfaces
1221 Needed when adding entries to a DACL from NT5 - SK */
1223 if (check_bind_req(p,
1224 &pkt->u.bind.ctx_list[0].abstract_syntax,
1225 &pkt->u.bind.ctx_list[0].transfer_syntaxes[0],
1226 pkt->u.bind.ctx_list[0].context_id)) {
1228 bind_ack_ctx.result = 0;
1229 bind_ack_ctx.reason = 0;
1230 bind_ack_ctx.syntax = pkt->u.bind.ctx_list[0].transfer_syntaxes[0];
1232 p->pipe_bound = False;
1233 /* Rejection reason: abstract syntax not supported */
1234 bind_ack_ctx.result = DCERPC_BIND_PROVIDER_REJECT;
1235 bind_ack_ctx.reason = DCERPC_BIND_REASON_ASYNTAX;
1236 bind_ack_ctx.syntax = null_ndr_syntax_id;
1240 * Check if this is an authenticated bind request.
1242 if (pkt->auth_length) {
1243 /* Quick length check. Won't catch a bad auth footer,
1244 * prevents overrun. */
1246 if (pkt->frag_length < RPC_HEADER_LEN +
1249 DEBUG(0,("api_pipe_bind_req: auth_len (%u) "
1250 "too long for fragment %u.\n",
1251 (unsigned int)pkt->auth_length,
1252 (unsigned int)pkt->frag_length));
1257 * Decode the authentication verifier.
1259 status = dcerpc_pull_dcerpc_auth(pkt,
1260 &pkt->u.bind.auth_info,
1262 if (!NT_STATUS_IS_OK(status)) {
1263 DEBUG(0, ("Unable to unmarshall dcerpc_auth.\n"));
1267 auth_type = auth_info.auth_type;
1269 /* Work out if we have to sign or seal etc. */
1270 switch (auth_info.auth_level) {
1271 case DCERPC_AUTH_LEVEL_INTEGRITY:
1272 p->auth.auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1274 case DCERPC_AUTH_LEVEL_PRIVACY:
1275 p->auth.auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1278 DEBUG(0, ("Unexpected auth level (%u).\n",
1279 (unsigned int)auth_info.auth_level ));
1283 switch (auth_type) {
1284 case DCERPC_AUTH_TYPE_NTLMSSP:
1285 if (!pipe_ntlmssp_auth_bind(p, pkt,
1286 &auth_info, &auth_resp)) {
1292 case DCERPC_AUTH_TYPE_SCHANNEL:
1293 if (!pipe_schannel_auth_bind(p, pkt,
1294 &auth_info, &auth_resp)) {
1299 case DCERPC_AUTH_TYPE_SPNEGO:
1300 if (!pipe_spnego_auth_bind_negotiate(p, pkt,
1301 &auth_info, &auth_resp)) {
1306 case DCERPC_AUTH_TYPE_NONE:
1310 DEBUG(0, ("Unknown auth type %x requested.\n", auth_type));
1315 if (auth_type == DCERPC_AUTH_TYPE_NONE) {
1316 /* Unauthenticated bind request. */
1317 /* We're finished - no more packets. */
1318 p->auth.auth_type = PIPE_AUTH_TYPE_NONE;
1319 /* We must set the pipe auth_level here also. */
1320 p->auth.auth_level = DCERPC_AUTH_LEVEL_NONE;
1321 p->pipe_bound = True;
1322 /* The session key was initialized from the SMB
1323 * session in make_internal_rpc_pipe_p */
1326 ZERO_STRUCT(u.bind_ack);
1327 u.bind_ack.max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
1328 u.bind_ack.max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
1329 u.bind_ack.assoc_group_id = assoc_gid;
1331 /* name has to be \PIPE\xxxxx */
1332 u.bind_ack.secondary_address =
1333 talloc_asprintf(pkt, "\\PIPE\\%s",
1334 rpc_srv_get_pipe_srv_name(&id));
1335 if (!u.bind_ack.secondary_address) {
1336 DEBUG(0, ("Out of memory!\n"));
1339 u.bind_ack.secondary_address_size =
1340 strlen(u.bind_ack.secondary_address) + 1;
1342 u.bind_ack.num_results = 1;
1343 u.bind_ack.ctx_list = &bind_ack_ctx;
1345 /* NOTE: We leave the auth_info empty so we can calculate the padding
1346 * later and then append the auth_info --simo */
1349 * Marshall directly into the outgoing PDU space. We
1350 * must do this as we need to set to the bind response
1351 * header and are never sending more than one PDU here.
1354 status = dcerpc_push_ncacn_packet(p->mem_ctx,
1355 DCERPC_PKT_BIND_ACK,
1356 DCERPC_PFC_FLAG_FIRST |
1357 DCERPC_PFC_FLAG_LAST,
1362 if (!NT_STATUS_IS_OK(status)) {
1363 DEBUG(0, ("Failed to marshall bind_ack packet. (%s)\n",
1364 nt_errstr(status)));
1367 if (auth_resp.length) {
1369 status = dcerpc_push_dcerpc_auth(pkt,
1371 auth_info.auth_level,
1373 1, /* auth_context_id */
1376 if (!NT_STATUS_IS_OK(status)) {
1377 DEBUG(0, ("Marshalling of dcerpc_auth failed.\n"));
1382 /* Now that we have the auth len store it into the right place in
1383 * the dcerpc header */
1384 dcerpc_set_frag_length(&p->out_data.frag,
1385 p->out_data.frag.length + auth_blob.length);
1387 if (auth_blob.length) {
1389 if (!data_blob_append(p->mem_ctx, &p->out_data.frag,
1390 auth_blob.data, auth_blob.length)) {
1391 DEBUG(0, ("Append of auth info failed.\n"));
1397 * Setup the lengths for the initial reply.
1400 p->out_data.data_sent_length = 0;
1401 p->out_data.current_pdu_sent = 0;
1403 TALLOC_FREE(auth_blob.data);
1408 data_blob_free(&p->out_data.frag);
1409 TALLOC_FREE(auth_blob.data);
1410 return setup_bind_nak(p, pkt);
1413 /****************************************************************************
1414 Deal with an alter context call. Can be third part of 3 leg auth request for
1416 ****************************************************************************/
1418 bool api_pipe_alter_context(pipes_struct *p, struct ncacn_packet *pkt)
1420 struct dcerpc_auth auth_info;
1423 union dcerpc_payload u;
1424 struct dcerpc_ack_ctx bind_ack_ctx;
1425 DATA_BLOB auth_resp = data_blob_null;
1426 DATA_BLOB auth_blob = data_blob_null;
1429 DEBUG(5,("api_pipe_alter_context: make response. %d\n", __LINE__));
1431 if (pkt->u.bind.assoc_group_id != 0) {
1432 assoc_gid = pkt->u.bind.assoc_group_id;
1438 * Create the bind response struct.
1441 /* If the requested abstract synt uuid doesn't match our client pipe,
1442 reject the bind_ack & set the transfer interface synt to all 0's,
1443 ver 0 (observed when NT5 attempts to bind to abstract interfaces
1445 Needed when adding entries to a DACL from NT5 - SK */
1447 if (check_bind_req(p,
1448 &pkt->u.bind.ctx_list[0].abstract_syntax,
1449 &pkt->u.bind.ctx_list[0].transfer_syntaxes[0],
1450 pkt->u.bind.ctx_list[0].context_id)) {
1452 bind_ack_ctx.result = 0;
1453 bind_ack_ctx.reason = 0;
1454 bind_ack_ctx.syntax = pkt->u.bind.ctx_list[0].transfer_syntaxes[0];
1456 p->pipe_bound = False;
1457 /* Rejection reason: abstract syntax not supported */
1458 bind_ack_ctx.result = DCERPC_BIND_PROVIDER_REJECT;
1459 bind_ack_ctx.reason = DCERPC_BIND_REASON_ASYNTAX;
1460 bind_ack_ctx.syntax = null_ndr_syntax_id;
1464 * Check if this is an authenticated alter context request.
1466 if (pkt->auth_length) {
1467 /* Quick length check. Won't catch a bad auth footer,
1468 * prevents overrun. */
1470 if (pkt->frag_length < RPC_HEADER_LEN +
1473 DEBUG(0,("api_pipe_alter_context: auth_len (%u) "
1474 "too long for fragment %u.\n",
1475 (unsigned int)pkt->auth_length,
1476 (unsigned int)pkt->frag_length ));
1480 status = dcerpc_pull_dcerpc_auth(pkt,
1481 &pkt->u.bind.auth_info,
1483 if (!NT_STATUS_IS_OK(status)) {
1484 DEBUG(0, ("Unable to unmarshall dcerpc_auth.\n"));
1490 * Currently only the SPNEGO auth type uses the alter ctx
1491 * response in place of the NTLMSSP auth3 type.
1494 if (auth_info.auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1495 /* We can only finish if the pipe is unbound. */
1496 if (!p->pipe_bound) {
1497 if (!pipe_spnego_auth_bind_continue(p, pkt,
1498 &auth_info, &auth_resp)) {
1508 ZERO_STRUCT(u.alter_resp);
1509 u.alter_resp.max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
1510 u.alter_resp.max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
1511 u.alter_resp.assoc_group_id = assoc_gid;
1513 /* secondary address CAN be NULL
1514 * as the specs say it's ignored.
1515 * It MUST be NULL to have the spoolss working.
1517 u.alter_resp.secondary_address = "";
1518 u.alter_resp.secondary_address_size = 1;
1520 u.alter_resp.num_results = 1;
1521 u.alter_resp.ctx_list = &bind_ack_ctx;
1523 /* NOTE: We leave the auth_info empty so we can calculate the padding
1524 * later and then append the auth_info --simo */
1527 * Marshall directly into the outgoing PDU space. We
1528 * must do this as we need to set to the bind response
1529 * header and are never sending more than one PDU here.
1532 status = dcerpc_push_ncacn_packet(p->mem_ctx,
1533 DCERPC_PKT_ALTER_RESP,
1534 DCERPC_PFC_FLAG_FIRST |
1535 DCERPC_PFC_FLAG_LAST,
1540 if (!NT_STATUS_IS_OK(status)) {
1541 DEBUG(0, ("Failed to marshall bind_ack packet. (%s)\n",
1542 nt_errstr(status)));
1545 if (auth_resp.length) {
1547 /* Work out any padding needed before the auth footer. */
1548 pad_len = p->out_data.frag.length % SERVER_NDR_PADDING_SIZE;
1550 pad_len = SERVER_NDR_PADDING_SIZE - pad_len;
1551 DEBUG(10, ("auth pad_len = %u\n",
1552 (unsigned int)pad_len));
1555 status = dcerpc_push_dcerpc_auth(pkt,
1556 auth_info.auth_type,
1557 auth_info.auth_level,
1559 1, /* auth_context_id */
1562 if (!NT_STATUS_IS_OK(status)) {
1563 DEBUG(0, ("Marshalling of dcerpc_auth failed.\n"));
1568 /* Now that we have the auth len store it into the right place in
1569 * the dcerpc header */
1570 dcerpc_set_frag_length(&p->out_data.frag,
1571 p->out_data.frag.length +
1572 pad_len + auth_blob.length);
1574 if (auth_resp.length) {
1576 char pad[SERVER_NDR_PADDING_SIZE];
1577 memset(pad, '\0', SERVER_NDR_PADDING_SIZE);
1578 if (!data_blob_append(p->mem_ctx,
1581 DEBUG(0, ("api_pipe_bind_req: failed to add "
1582 "%u bytes of pad data.\n",
1583 (unsigned int)pad_len));
1588 if (!data_blob_append(p->mem_ctx, &p->out_data.frag,
1589 auth_blob.data, auth_blob.length)) {
1590 DEBUG(0, ("Append of auth info failed.\n"));
1596 * Setup the lengths for the initial reply.
1599 p->out_data.data_sent_length = 0;
1600 p->out_data.current_pdu_sent = 0;
1602 TALLOC_FREE(auth_blob.data);
1607 data_blob_free(&p->out_data.frag);
1608 TALLOC_FREE(auth_blob.data);
1609 return setup_bind_nak(p, pkt);
1612 /****************************************************************************
1613 Find the set of RPC functions associated with this context_id
1614 ****************************************************************************/
1616 static PIPE_RPC_FNS* find_pipe_fns_by_context( PIPE_RPC_FNS *list, uint32 context_id )
1618 PIPE_RPC_FNS *fns = NULL;
1621 DEBUG(0,("find_pipe_fns_by_context: ERROR! No context list for pipe!\n"));
1625 for (fns=list; fns; fns=fns->next ) {
1626 if ( fns->context_id == context_id )
1632 /****************************************************************************
1634 ****************************************************************************/
1636 void free_pipe_rpc_context( PIPE_RPC_FNS *list )
1638 PIPE_RPC_FNS *tmp = list;
1650 static bool api_rpcTNP(pipes_struct *p, struct ncacn_packet *pkt,
1651 const struct api_struct *api_rpc_cmds, int n_cmds);
1653 /****************************************************************************
1654 Find the correct RPC function to call for this request.
1655 If the pipe is authenticated then become the correct UNIX user
1656 before doing the call.
1657 ****************************************************************************/
1659 bool api_pipe_request(pipes_struct *p, struct ncacn_packet *pkt)
1662 bool changed_user = False;
1663 PIPE_RPC_FNS *pipe_fns;
1665 if (p->pipe_bound &&
1666 ((p->auth.auth_type == PIPE_AUTH_TYPE_NTLMSSP) ||
1667 (p->auth.auth_type == PIPE_AUTH_TYPE_SPNEGO_NTLMSSP))) {
1668 if(!become_authenticated_pipe_user(p)) {
1669 data_blob_free(&p->out_data.rdata);
1672 changed_user = True;
1675 DEBUG(5, ("Requested \\PIPE\\%s\n",
1676 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1678 /* get the set of RPC functions for this context */
1680 pipe_fns = find_pipe_fns_by_context(p->contexts,
1681 pkt->u.request.context_id);
1684 TALLOC_CTX *frame = talloc_stackframe();
1685 ret = api_rpcTNP(p, pkt, pipe_fns->cmds, pipe_fns->n_cmds);
1689 DEBUG(0, ("No rpc function table associated with context "
1690 "[%d] on pipe [%s]\n",
1691 pkt->u.request.context_id,
1692 get_pipe_name_from_syntax(talloc_tos(),
1697 unbecome_authenticated_pipe_user();
1703 /*******************************************************************
1704 Calls the underlying RPC function for a named pipe.
1705 ********************************************************************/
1707 static bool api_rpcTNP(pipes_struct *p, struct ncacn_packet *pkt,
1708 const struct api_struct *api_rpc_cmds, int n_cmds)
1713 /* interpret the command */
1714 DEBUG(4,("api_rpcTNP: %s op 0x%x - ",
1715 get_pipe_name_from_syntax(talloc_tos(), &p->syntax),
1716 pkt->u.request.opnum));
1718 if (DEBUGLEVEL >= 50) {
1720 slprintf(name, sizeof(name)-1, "in_%s",
1721 get_pipe_name_from_syntax(talloc_tos(), &p->syntax));
1722 prs_dump(name, pkt->u.request.opnum, &p->in_data.data);
1725 for (fn_num = 0; fn_num < n_cmds; fn_num++) {
1726 if (api_rpc_cmds[fn_num].opnum == pkt->u.request.opnum &&
1727 api_rpc_cmds[fn_num].fn != NULL) {
1728 DEBUG(3, ("api_rpcTNP: rpc command: %s\n",
1729 api_rpc_cmds[fn_num].name));
1734 if (fn_num == n_cmds) {
1736 * For an unknown RPC just return a fault PDU but
1737 * return True to allow RPC's on the pipe to continue
1738 * and not put the pipe into fault state. JRA.
1740 DEBUG(4, ("unknown\n"));
1741 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
1745 offset1 = p->out_data.rdata.length;
1747 DEBUG(6, ("api_rpc_cmds[%d].fn == %p\n",
1748 fn_num, api_rpc_cmds[fn_num].fn));
1749 /* do the actual command */
1750 if(!api_rpc_cmds[fn_num].fn(p)) {
1751 DEBUG(0,("api_rpcTNP: %s: %s failed.\n",
1752 get_pipe_name_from_syntax(talloc_tos(), &p->syntax),
1753 api_rpc_cmds[fn_num].name));
1754 data_blob_free(&p->out_data.rdata);
1758 if (p->bad_handle_fault_state) {
1759 DEBUG(4,("api_rpcTNP: bad handle fault return.\n"));
1760 p->bad_handle_fault_state = False;
1761 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_CONTEXT_MISMATCH));
1765 if (p->rng_fault_state) {
1766 DEBUG(4, ("api_rpcTNP: rng fault return\n"));
1767 p->rng_fault_state = False;
1768 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
1772 if (DEBUGLEVEL >= 50) {
1774 slprintf(name, sizeof(name)-1, "out_%s",
1775 get_pipe_name_from_syntax(talloc_tos(), &p->syntax));
1776 prs_dump_region(name, pkt->u.request.opnum,
1777 p->out_data.rdata.data,
1779 p->out_data.rdata.length);
1782 DEBUG(5,("api_rpcTNP: called %s successfully\n",
1783 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1785 /* Check for buffer underflow in rpc parsing */
1787 if ((DEBUGLEVEL >= 10) &&
1788 (prs_offset(&p->in_data.data) != prs_data_size(&p->in_data.data))) {
1789 size_t data_len = prs_data_size(&p->in_data.data) - prs_offset(&p->in_data.data);
1790 char *data = (char *)SMB_MALLOC(data_len);
1792 DEBUG(10, ("api_rpcTNP: rpc input buffer underflow (parse error?)\n"));
1794 prs_uint8s(False, "", &p->in_data.data, 0, (unsigned char *)data, (uint32)data_len);