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 "dcesrv_ntlmssp.h"
36 #include "dcesrv_gssapi.h"
37 #include "dcesrv_spnego.h"
38 #include "rpc_server.h"
42 #define DBGC_CLASS DBGC_RPC_SRV
45 * Dump everything from the start of the end up of the provided data
46 * into a file, but only at debug level >= 50
48 static void dump_pdu_region(const char *name, int v,
49 DATA_BLOB *data, size_t start, size_t end)
55 if (DEBUGLEVEL < 50) return;
57 if (start > data->length || end > data->length || start > end) return;
59 for (i = 1; i < 100; i++) {
61 fname = talloc_asprintf(talloc_tos(),
65 fname = talloc_asprintf(talloc_tos(),
72 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0644);
73 if (fd != -1 || errno != EEXIST) break;
76 sz = write(fd, data->data + start, end - start);
78 if ((sz != end - start) || (i != 0) ) {
79 DEBUG(0, ("Error writing/closing %s: %ld!=%ld %d\n",
80 fname, (unsigned long)sz,
81 (unsigned long)end - start, i));
83 DEBUG(0,("created %s\n", fname));
89 static void free_pipe_auth_data(struct pipe_auth_data *auth)
91 TALLOC_FREE(auth->auth_ctx);
94 static DATA_BLOB generic_session_key(void)
96 return data_blob("SystemLibraryDTC", 16);
99 /*******************************************************************
100 Generate the next PDU to be returned from the data.
101 ********************************************************************/
103 static NTSTATUS create_next_packet(TALLOC_CTX *mem_ctx,
104 struct pipe_auth_data *auth,
107 size_t data_sent_length,
111 union dcerpc_payload u;
120 ZERO_STRUCT(u.response);
122 /* Set up rpc packet pfc flags. */
123 if (data_sent_length == 0) {
124 pfc_flags = DCERPC_PFC_FLAG_FIRST;
129 /* Work out how much we can fit in a single PDU. */
130 data_left = rdata->length - data_sent_length;
132 /* Ensure there really is data left to send. */
134 DEBUG(0, ("No data left to send !\n"));
135 return NT_STATUS_BUFFER_TOO_SMALL;
138 status = dcerpc_guess_sizes(auth,
139 DCERPC_RESPONSE_LENGTH,
141 RPC_MAX_PDU_FRAG_LEN,
142 SERVER_NDR_PADDING_SIZE,
143 &data_to_send, &frag_len,
144 &auth_len, &pad_len);
145 if (!NT_STATUS_IS_OK(status)) {
149 /* Set up the alloc hint. This should be the data left to send. */
150 u.response.alloc_hint = data_left;
152 /* Work out if this PDU will be the last. */
153 if (data_sent_length + data_to_send >= rdata->length) {
154 pfc_flags |= DCERPC_PFC_FLAG_LAST;
157 /* Prepare data to be NDR encoded. */
158 u.response.stub_and_verifier =
159 data_blob_const(rdata->data + data_sent_length, data_to_send);
161 /* Store the packet in the data stream. */
162 status = dcerpc_push_ncacn_packet(mem_ctx, DCERPC_PKT_RESPONSE,
163 pfc_flags, auth_len, call_id,
165 if (!NT_STATUS_IS_OK(status)) {
166 DEBUG(0, ("Failed to marshall RPC Packet.\n"));
171 /* Set the proper length on the pdu, including padding.
172 * Only needed if an auth trailer will be appended. */
173 dcerpc_set_frag_length(frag, frag->length
175 + DCERPC_AUTH_TRAILER_LENGTH
180 status = dcerpc_add_auth_footer(auth, pad_len, frag);
181 if (!NT_STATUS_IS_OK(status)) {
182 data_blob_free(frag);
187 *pdu_size = data_to_send;
191 /*******************************************************************
192 Generate the next PDU to be returned from the data in p->rdata.
193 ********************************************************************/
195 bool create_next_pdu(struct pipes_struct *p)
201 * If we're in the fault state, keep returning fault PDU's until
202 * the pipe gets closed. JRA.
204 if (p->fault_state) {
205 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
209 status = create_next_packet(p->mem_ctx, &p->auth,
210 p->call_id, &p->out_data.rdata,
211 p->out_data.data_sent_length,
212 &p->out_data.frag, &pdu_size);
213 if (!NT_STATUS_IS_OK(status)) {
214 DEBUG(0, ("Failed to create packet with error %s, "
215 "(auth level %u / type %u)\n",
217 (unsigned int)p->auth.auth_level,
218 (unsigned int)p->auth.auth_type));
222 /* Setup the counts for this PDU. */
223 p->out_data.data_sent_length += pdu_size;
224 p->out_data.current_pdu_sent = 0;
229 static bool pipe_init_outgoing_data(struct pipes_struct *p);
231 /*******************************************************************
232 Marshall a bind_nak pdu.
233 *******************************************************************/
235 static bool setup_bind_nak(struct pipes_struct *p, struct ncacn_packet *pkt)
238 union dcerpc_payload u;
240 /* Free any memory in the current return data buffer. */
241 pipe_init_outgoing_data(p);
244 * Initialize a bind_nak header.
249 u.bind_nak.reject_reason = 0;
252 * Marshall directly into the outgoing PDU space. We
253 * must do this as we need to set to the bind response
254 * header and are never sending more than one PDU here.
257 status = dcerpc_push_ncacn_packet(p->mem_ctx,
259 DCERPC_PFC_FLAG_FIRST |
260 DCERPC_PFC_FLAG_LAST,
265 if (!NT_STATUS_IS_OK(status)) {
269 p->out_data.data_sent_length = 0;
270 p->out_data.current_pdu_sent = 0;
272 free_pipe_auth_data(&p->auth);
273 p->auth.auth_level = DCERPC_AUTH_LEVEL_NONE;
274 p->auth.auth_type = DCERPC_AUTH_TYPE_NONE;
275 p->pipe_bound = False;
280 /*******************************************************************
281 Marshall a fault pdu.
282 *******************************************************************/
284 bool setup_fault_pdu(struct pipes_struct *p, NTSTATUS fault_status)
287 union dcerpc_payload u;
289 /* Free any memory in the current return data buffer. */
290 pipe_init_outgoing_data(p);
293 * Initialize a fault header.
298 u.fault.status = NT_STATUS_V(fault_status);
299 u.fault._pad = data_blob_talloc_zero(p->mem_ctx, 4);
302 * Marshall directly into the outgoing PDU space. We
303 * must do this as we need to set to the bind response
304 * header and are never sending more than one PDU here.
307 status = dcerpc_push_ncacn_packet(p->mem_ctx,
309 DCERPC_PFC_FLAG_FIRST |
310 DCERPC_PFC_FLAG_LAST |
311 DCERPC_PFC_FLAG_DID_NOT_EXECUTE,
316 if (!NT_STATUS_IS_OK(status)) {
320 p->out_data.data_sent_length = 0;
321 p->out_data.current_pdu_sent = 0;
326 /*******************************************************************
327 Ensure a bind request has the correct abstract & transfer interface.
328 Used to reject unknown binds from Win2k.
329 *******************************************************************/
331 static bool check_bind_req(struct pipes_struct *p,
332 struct ndr_syntax_id* abstract,
333 struct ndr_syntax_id* transfer,
336 struct pipe_rpc_fns *context_fns;
338 DEBUG(3,("check_bind_req for %s\n",
339 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
341 /* we have to check all now since win2k introduced a new UUID on the lsaprpc pipe */
342 if (rpc_srv_pipe_exists_by_id(abstract) &&
343 ndr_syntax_id_equal(transfer, &ndr_transfer_syntax)) {
344 DEBUG(3, ("check_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
345 rpc_srv_get_pipe_cli_name(abstract),
346 rpc_srv_get_pipe_srv_name(abstract)));
351 context_fns = SMB_MALLOC_P(struct pipe_rpc_fns);
352 if (context_fns == NULL) {
353 DEBUG(0,("check_bind_req: malloc() failed!\n"));
357 context_fns->next = context_fns->prev = NULL;
358 context_fns->n_cmds = rpc_srv_get_pipe_num_cmds(abstract);
359 context_fns->cmds = rpc_srv_get_pipe_cmds(abstract);
360 context_fns->context_id = context_id;
362 /* add to the list of open contexts */
364 DLIST_ADD( p->contexts, context_fns );
370 * Is a named pipe known?
371 * @param[in] cli_filename The pipe name requested by the client
372 * @result Do we want to serve this?
374 bool is_known_pipename(const char *cli_filename, struct ndr_syntax_id *syntax)
376 const char *pipename = cli_filename;
379 if (strnequal(pipename, "\\PIPE\\", 6)) {
383 if (*pipename == '\\') {
387 if (lp_disable_spoolss() && strequal(pipename, "spoolss")) {
388 DEBUG(10, ("refusing spoolss access\n"));
392 if (rpc_srv_get_pipe_interface_by_cli_name(pipename, syntax)) {
396 status = smb_probe_module("rpc", pipename);
397 if (!NT_STATUS_IS_OK(status)) {
398 DEBUG(10, ("is_known_pipename: %s unknown\n", cli_filename));
401 DEBUG(10, ("is_known_pipename: %s loaded dynamically\n", pipename));
404 * Scan the list again for the interface id
406 if (rpc_srv_get_pipe_interface_by_cli_name(pipename, syntax)) {
410 DEBUG(10, ("is_known_pipename: pipe %s did not register itself!\n",
416 /*******************************************************************
417 Handle the first part of a SPNEGO bind auth.
418 *******************************************************************/
420 static bool pipe_spnego_auth_bind(struct pipes_struct *p,
422 struct dcerpc_auth *auth_info,
425 struct spnego_context *spnego_ctx;
428 status = spnego_server_auth_start(p,
429 (auth_info->auth_level ==
430 DCERPC_AUTH_LEVEL_INTEGRITY),
431 (auth_info->auth_level ==
432 DCERPC_AUTH_LEVEL_PRIVACY),
434 &auth_info->credentials,
437 if (!NT_STATUS_IS_OK(status)) {
438 DEBUG(0, ("Failed SPNEGO negotiate (%s)\n",
443 /* Make sure data is bound to the memctx, to be freed the caller */
444 talloc_steal(mem_ctx, response->data);
446 p->auth.auth_ctx = spnego_ctx;
447 p->auth.auth_data_free_func = &free_pipe_auth_data;
448 p->auth.auth_type = DCERPC_AUTH_TYPE_SPNEGO;
450 DEBUG(10, ("SPNEGO auth started\n"));
455 /*******************************************************************
456 Handle an schannel bind auth.
457 *******************************************************************/
459 static bool pipe_schannel_auth_bind(struct pipes_struct *p,
461 struct dcerpc_auth *auth_info,
464 struct NL_AUTH_MESSAGE neg;
465 struct NL_AUTH_MESSAGE reply;
468 struct netlogon_creds_CredentialState *creds;
469 DATA_BLOB session_key;
470 enum ndr_err_code ndr_err;
471 struct schannel_state *schannel_auth;
473 ndr_err = ndr_pull_struct_blob(
474 &auth_info->credentials, mem_ctx, &neg,
475 (ndr_pull_flags_fn_t)ndr_pull_NL_AUTH_MESSAGE);
476 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
477 DEBUG(0,("pipe_schannel_auth_bind: Could not unmarshal SCHANNEL auth neg\n"));
481 if (DEBUGLEVEL >= 10) {
482 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &neg);
485 if (!(neg.Flags & NL_FLAG_OEM_NETBIOS_COMPUTER_NAME)) {
486 DEBUG(0,("pipe_schannel_auth_bind: Did not receive netbios computer name\n"));
491 * The neg.oem_netbios_computer.a key here must match the remote computer name
492 * given in the DOM_CLNT_SRV.uni_comp_name used on all netlogon pipe
493 * operations that use credentials.
497 status = schannel_get_creds_state(p, lp_private_dir(),
498 neg.oem_netbios_computer.a, &creds);
501 if (!NT_STATUS_IS_OK(status)) {
502 DEBUG(0, ("pipe_schannel_auth_bind: Attempt to bind using schannel without successful serverauth2\n"));
506 schannel_auth = talloc(p, struct schannel_state);
507 if (!schannel_auth) {
512 schannel_auth->state = SCHANNEL_STATE_START;
513 schannel_auth->seq_num = 0;
514 schannel_auth->initiator = false;
515 schannel_auth->creds = creds;
518 * JRA. Should we also copy the schannel session key into the pipe session key p->session_key
519 * here ? We do that for NTLMSSP, but the session key is already set up from the vuser
520 * struct of the person who opened the pipe. I need to test this further. JRA.
522 * VL. As we are mapping this to guest set the generic key
523 * "SystemLibraryDTC" key here. It's a bit difficult to test against
524 * W2k3, as it does not allow schannel binds against SAMR and LSA
528 session_key = generic_session_key();
529 if (session_key.data == NULL) {
530 DEBUG(0, ("pipe_schannel_auth_bind: Could not alloc session"
535 ret = server_info_set_session_key(p->server_info, session_key);
537 data_blob_free(&session_key);
540 DEBUG(0, ("server_info_set_session_key failed\n"));
544 /*** SCHANNEL verifier ***/
546 reply.MessageType = NL_NEGOTIATE_RESPONSE;
548 reply.Buffer.dummy = 5; /* ??? actually I don't think
549 * this has any meaning
552 ndr_err = ndr_push_struct_blob(response, mem_ctx, &reply,
553 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
554 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
555 DEBUG(0,("Failed to marshall NL_AUTH_MESSAGE.\n"));
559 if (DEBUGLEVEL >= 10) {
560 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &reply);
563 DEBUG(10,("pipe_schannel_auth_bind: schannel auth: domain [%s] myname [%s]\n",
564 neg.oem_netbios_domain.a, neg.oem_netbios_computer.a));
566 /* We're finished with this bind - no more packets. */
567 p->auth.auth_ctx = schannel_auth;
568 p->auth.auth_data_free_func = &free_pipe_auth_data;
569 p->auth.auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
571 p->pipe_bound = True;
576 /*******************************************************************
577 Handle an NTLMSSP bind auth.
578 *******************************************************************/
580 static bool pipe_ntlmssp_auth_bind(struct pipes_struct *p,
582 struct dcerpc_auth *auth_info,
585 struct auth_ntlmssp_state *ntlmssp_state = NULL;
588 if (strncmp((char *)auth_info->credentials.data, "NTLMSSP", 7) != 0) {
589 DEBUG(0, ("Failed to read NTLMSSP in blob\n"));
593 /* We have an NTLMSSP blob. */
594 status = ntlmssp_server_auth_start(p,
595 (auth_info->auth_level ==
596 DCERPC_AUTH_LEVEL_INTEGRITY),
597 (auth_info->auth_level ==
598 DCERPC_AUTH_LEVEL_PRIVACY),
600 &auth_info->credentials,
603 if (!NT_STATUS_EQUAL(status, NT_STATUS_OK)) {
604 DEBUG(0, (__location__ ": auth_ntlmssp_start failed: %s\n",
609 /* Make sure data is bound to the memctx, to be freed the caller */
610 talloc_steal(mem_ctx, response->data);
612 p->auth.auth_ctx = ntlmssp_state;
613 p->auth.auth_data_free_func = &free_pipe_auth_data;
614 p->auth.auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
616 DEBUG(10, (__location__ ": NTLMSSP auth started\n"));
621 /*******************************************************************
622 Process an NTLMSSP authentication response.
623 If this function succeeds, the user has been authenticated
624 and their domain, name and calling workstation stored in
626 *******************************************************************/
628 static bool pipe_ntlmssp_verify_final(TALLOC_CTX *mem_ctx,
629 struct auth_ntlmssp_state *ntlmssp_ctx,
630 enum dcerpc_AuthLevel auth_level,
631 struct client_address *client_id,
632 struct ndr_syntax_id *syntax,
633 struct auth_serversupplied_info **server_info)
635 DATA_BLOB session_key;
639 DEBUG(5, (__location__ ": pipe %s checking user details\n",
640 get_pipe_name_from_syntax(talloc_tos(), syntax)));
642 /* Finally - if the pipe negotiated integrity (sign) or privacy (seal)
643 ensure the underlying NTLMSSP flags are also set. If not we should
646 status = ntlmssp_server_check_flags(ntlmssp_ctx,
648 DCERPC_AUTH_LEVEL_INTEGRITY),
650 DCERPC_AUTH_LEVEL_PRIVACY));
651 if (!NT_STATUS_IS_OK(status)) {
652 DEBUG(0, (__location__ ": Client failed to negotatie proper "
653 "security for pipe %s\n",
654 get_pipe_name_from_syntax(talloc_tos(), syntax)));
658 TALLOC_FREE(*server_info);
660 status = ntlmssp_server_get_user_info(ntlmssp_ctx,
661 mem_ctx, server_info);
662 if (!NT_STATUS_IS_OK(status)) {
663 DEBUG(0, (__location__ ": failed to obtain the server info "
664 "for authenticated user: %s\n", nt_errstr(status)));
668 if ((*server_info)->ptok == NULL) {
669 DEBUG(1, ("Auth module failed to provide nt_user_token\n"));
674 * We're an authenticated bind over smb, so the session key needs to
675 * be set to "SystemLibraryDTC". Weird, but this is what Windows
676 * does. See the RPC-SAMBA3SESSIONKEY.
679 session_key = generic_session_key();
680 if (session_key.data == NULL) {
684 ret = server_info_set_session_key((*server_info), session_key);
685 data_blob_free(&session_key);
687 DEBUG(0, ("Failed to set session key!\n"));
694 /*******************************************************************
695 Handle a GSSAPI bind auth.
696 *******************************************************************/
698 static bool pipe_gssapi_auth_bind(struct pipes_struct *p,
700 struct dcerpc_auth *auth_info,
704 struct gse_context *gse_ctx = NULL;
706 status = gssapi_server_auth_start(p,
707 (auth_info->auth_level ==
708 DCERPC_AUTH_LEVEL_INTEGRITY),
709 (auth_info->auth_level ==
710 DCERPC_AUTH_LEVEL_PRIVACY),
712 &auth_info->credentials,
715 if (!NT_STATUS_IS_OK(status)) {
716 DEBUG(0, ("Failed to init dcerpc gssapi server (%s)\n",
721 /* Make sure data is bound to the memctx, to be freed the caller */
722 talloc_steal(mem_ctx, response->data);
724 p->auth.auth_ctx = gse_ctx;
725 p->auth.auth_data_free_func = &free_pipe_auth_data;
726 p->auth.auth_type = DCERPC_AUTH_TYPE_KRB5;
728 DEBUG(10, ("KRB5 auth started\n"));
733 TALLOC_FREE(gse_ctx);
737 static NTSTATUS pipe_gssapi_verify_final(TALLOC_CTX *mem_ctx,
738 struct gse_context *gse_ctx,
739 struct client_address *client_id,
740 struct auth_serversupplied_info **server_info)
742 DATA_BLOB session_key;
746 /* Finally - if the pipe negotiated integrity (sign) or privacy (seal)
747 ensure the underlying flags are also set. If not we should
750 status = gssapi_server_check_flags(gse_ctx);
751 if (!NT_STATUS_IS_OK(status)) {
752 DEBUG(0, ("Requested Security Layers not honored!\n"));
756 status = gssapi_server_get_user_info(gse_ctx, mem_ctx,
757 client_id, server_info);
758 if (!NT_STATUS_IS_OK(status)) {
759 DEBUG(0, (__location__ ": failed to obtain the server info "
760 "for authenticated user: %s\n", nt_errstr(status)));
764 if ((*server_info)->ptok == NULL) {
765 status = create_local_token(*server_info);
766 if (!NT_STATUS_IS_OK(status)) {
767 DEBUG(1, ("Failed to create local user token (%s)\n",
769 status = NT_STATUS_ACCESS_DENIED;
774 /* TODO: this is what the ntlmssp code does with the session_key, check
775 * it is ok with gssapi too */
777 * We're an authenticated bind over smb, so the session key needs to
778 * be set to "SystemLibraryDTC". Weird, but this is what Windows
779 * does. See the RPC-SAMBA3SESSIONKEY.
782 session_key = generic_session_key();
783 if (session_key.data == NULL) {
784 return NT_STATUS_ACCESS_DENIED;
787 bret = server_info_set_session_key((*server_info), session_key);
788 data_blob_free(&session_key);
790 return NT_STATUS_ACCESS_DENIED;
796 static NTSTATUS pipe_auth_verify_final(struct pipes_struct *p)
798 enum spnego_mech auth_type;
799 struct auth_ntlmssp_state *ntlmssp_ctx;
800 struct spnego_context *spnego_ctx;
801 struct gse_context *gse_ctx;
805 switch (p->auth.auth_type) {
806 case DCERPC_AUTH_TYPE_NTLMSSP:
807 ntlmssp_ctx = talloc_get_type_abort(p->auth.auth_ctx,
808 struct auth_ntlmssp_state);
809 if (!pipe_ntlmssp_verify_final(p, ntlmssp_ctx,
811 p->client_id, &p->syntax,
813 return NT_STATUS_ACCESS_DENIED;
816 case DCERPC_AUTH_TYPE_KRB5:
817 gse_ctx = talloc_get_type_abort(p->auth.auth_ctx,
819 status = pipe_gssapi_verify_final(p, gse_ctx,
822 if (!NT_STATUS_IS_OK(status)) {
823 DEBUG(1, ("gssapi bind failed with: %s",
828 case DCERPC_AUTH_TYPE_SPNEGO:
829 spnego_ctx = talloc_get_type_abort(p->auth.auth_ctx,
830 struct spnego_context);
831 status = spnego_get_negotiated_mech(spnego_ctx,
832 &auth_type, &mech_ctx);
833 if (!NT_STATUS_IS_OK(status)) {
834 DEBUG(0, ("Bad SPNEGO state (%s)\n",
840 gse_ctx = talloc_get_type_abort(mech_ctx,
842 status = pipe_gssapi_verify_final(p, gse_ctx,
845 if (!NT_STATUS_IS_OK(status)) {
846 DEBUG(1, ("gssapi bind failed with: %s",
852 ntlmssp_ctx = talloc_get_type_abort(mech_ctx,
853 struct auth_ntlmssp_state);
854 if (!pipe_ntlmssp_verify_final(p, ntlmssp_ctx,
859 return NT_STATUS_ACCESS_DENIED;
863 DEBUG(0, (__location__ ": incorrect spnego type "
864 "(%d).\n", auth_type));
865 return NT_STATUS_ACCESS_DENIED;
869 DEBUG(0, (__location__ ": incorrect auth type (%u).\n",
870 (unsigned int)p->auth.auth_type));
871 return NT_STATUS_ACCESS_DENIED;
874 p->pipe_bound = true;
879 /*******************************************************************
880 Respond to a pipe bind request.
881 *******************************************************************/
883 static bool api_pipe_bind_req(struct pipes_struct *p,
884 struct ncacn_packet *pkt)
886 struct dcerpc_auth auth_info;
888 unsigned int auth_type = DCERPC_AUTH_TYPE_NONE;
890 struct ndr_syntax_id id;
891 union dcerpc_payload u;
892 struct dcerpc_ack_ctx bind_ack_ctx;
893 DATA_BLOB auth_resp = data_blob_null;
894 DATA_BLOB auth_blob = data_blob_null;
896 /* No rebinds on a bound pipe - use alter context. */
898 DEBUG(2,("api_pipe_bind_req: rejecting bind request on bound "
900 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
901 return setup_bind_nak(p, pkt);
904 if (pkt->u.bind.num_contexts == 0) {
905 DEBUG(0, ("api_pipe_bind_req: no rpc contexts around\n"));
910 * Try and find the correct pipe name to ensure
911 * that this is a pipe name we support.
913 id = pkt->u.bind.ctx_list[0].abstract_syntax;
914 if (rpc_srv_pipe_exists_by_id(&id)) {
915 DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
916 rpc_srv_get_pipe_cli_name(&id),
917 rpc_srv_get_pipe_srv_name(&id)));
919 status = smb_probe_module(
920 "rpc", get_pipe_name_from_syntax(
922 &pkt->u.bind.ctx_list[0].abstract_syntax));
924 if (NT_STATUS_IS_ERR(status)) {
925 DEBUG(3,("api_pipe_bind_req: Unknown pipe name %s in bind request.\n",
926 get_pipe_name_from_syntax(
928 &pkt->u.bind.ctx_list[0].abstract_syntax)));
930 return setup_bind_nak(p, pkt);
933 if (rpc_srv_get_pipe_interface_by_cli_name(
934 get_pipe_name_from_syntax(talloc_tos(),
937 DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
938 rpc_srv_get_pipe_cli_name(&id),
939 rpc_srv_get_pipe_srv_name(&id)));
941 DEBUG(0, ("module %s doesn't provide functions for "
943 get_pipe_name_from_syntax(talloc_tos(),
945 get_pipe_name_from_syntax(talloc_tos(),
947 return setup_bind_nak(p, pkt);
951 DEBUG(5,("api_pipe_bind_req: make response. %d\n", __LINE__));
953 if (pkt->u.bind.assoc_group_id != 0) {
954 assoc_gid = pkt->u.bind.assoc_group_id;
960 * Create the bind response struct.
963 /* If the requested abstract synt uuid doesn't match our client pipe,
964 reject the bind_ack & set the transfer interface synt to all 0's,
965 ver 0 (observed when NT5 attempts to bind to abstract interfaces
967 Needed when adding entries to a DACL from NT5 - SK */
969 if (check_bind_req(p,
970 &pkt->u.bind.ctx_list[0].abstract_syntax,
971 &pkt->u.bind.ctx_list[0].transfer_syntaxes[0],
972 pkt->u.bind.ctx_list[0].context_id)) {
974 bind_ack_ctx.result = 0;
975 bind_ack_ctx.reason = 0;
976 bind_ack_ctx.syntax = pkt->u.bind.ctx_list[0].transfer_syntaxes[0];
978 p->pipe_bound = False;
979 /* Rejection reason: abstract syntax not supported */
980 bind_ack_ctx.result = DCERPC_BIND_PROVIDER_REJECT;
981 bind_ack_ctx.reason = DCERPC_BIND_REASON_ASYNTAX;
982 bind_ack_ctx.syntax = null_ndr_syntax_id;
986 * Check if this is an authenticated bind request.
988 if (pkt->auth_length) {
989 /* Quick length check. Won't catch a bad auth footer,
990 * prevents overrun. */
992 if (pkt->frag_length < RPC_HEADER_LEN +
993 DCERPC_AUTH_TRAILER_LENGTH +
995 DEBUG(0,("api_pipe_bind_req: auth_len (%u) "
996 "too long for fragment %u.\n",
997 (unsigned int)pkt->auth_length,
998 (unsigned int)pkt->frag_length));
1003 * Decode the authentication verifier.
1005 status = dcerpc_pull_dcerpc_auth(pkt,
1006 &pkt->u.bind.auth_info,
1007 &auth_info, p->endian);
1008 if (!NT_STATUS_IS_OK(status)) {
1009 DEBUG(0, ("Unable to unmarshall dcerpc_auth.\n"));
1013 auth_type = auth_info.auth_type;
1015 /* Work out if we have to sign or seal etc. */
1016 switch (auth_info.auth_level) {
1017 case DCERPC_AUTH_LEVEL_INTEGRITY:
1018 p->auth.auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1020 case DCERPC_AUTH_LEVEL_PRIVACY:
1021 p->auth.auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1024 DEBUG(0, ("Unexpected auth level (%u).\n",
1025 (unsigned int)auth_info.auth_level ));
1029 switch (auth_type) {
1030 case DCERPC_AUTH_TYPE_NTLMSSP:
1031 if (!pipe_ntlmssp_auth_bind(p, pkt,
1032 &auth_info, &auth_resp)) {
1038 case DCERPC_AUTH_TYPE_SCHANNEL:
1039 if (!pipe_schannel_auth_bind(p, pkt,
1040 &auth_info, &auth_resp)) {
1045 case DCERPC_AUTH_TYPE_SPNEGO:
1046 if (!pipe_spnego_auth_bind(p, pkt,
1047 &auth_info, &auth_resp)) {
1052 case DCERPC_AUTH_TYPE_KRB5:
1053 if (!pipe_gssapi_auth_bind(p, pkt,
1054 &auth_info, &auth_resp)) {
1059 case DCERPC_AUTH_TYPE_NONE:
1063 DEBUG(0, ("Unknown auth type %x requested.\n", auth_type));
1068 if (auth_type == DCERPC_AUTH_TYPE_NONE) {
1069 /* Unauthenticated bind request. */
1070 /* We're finished - no more packets. */
1071 p->auth.auth_type = DCERPC_AUTH_TYPE_NONE;
1072 /* We must set the pipe auth_level here also. */
1073 p->auth.auth_level = DCERPC_AUTH_LEVEL_NONE;
1074 p->pipe_bound = True;
1075 /* The session key was initialized from the SMB
1076 * session in make_internal_rpc_pipe_p */
1079 ZERO_STRUCT(u.bind_ack);
1080 u.bind_ack.max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
1081 u.bind_ack.max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
1082 u.bind_ack.assoc_group_id = assoc_gid;
1084 /* name has to be \PIPE\xxxxx */
1085 u.bind_ack.secondary_address =
1086 talloc_asprintf(pkt, "\\PIPE\\%s",
1087 rpc_srv_get_pipe_srv_name(&id));
1088 if (!u.bind_ack.secondary_address) {
1089 DEBUG(0, ("Out of memory!\n"));
1092 u.bind_ack.secondary_address_size =
1093 strlen(u.bind_ack.secondary_address) + 1;
1095 u.bind_ack.num_results = 1;
1096 u.bind_ack.ctx_list = &bind_ack_ctx;
1098 /* NOTE: We leave the auth_info empty so we can calculate the padding
1099 * later and then append the auth_info --simo */
1102 * Marshall directly into the outgoing PDU space. We
1103 * must do this as we need to set to the bind response
1104 * header and are never sending more than one PDU here.
1107 status = dcerpc_push_ncacn_packet(p->mem_ctx,
1108 DCERPC_PKT_BIND_ACK,
1109 DCERPC_PFC_FLAG_FIRST |
1110 DCERPC_PFC_FLAG_LAST,
1115 if (!NT_STATUS_IS_OK(status)) {
1116 DEBUG(0, ("Failed to marshall bind_ack packet. (%s)\n",
1117 nt_errstr(status)));
1120 if (auth_resp.length) {
1122 status = dcerpc_push_dcerpc_auth(pkt,
1124 auth_info.auth_level,
1126 1, /* auth_context_id */
1129 if (!NT_STATUS_IS_OK(status)) {
1130 DEBUG(0, ("Marshalling of dcerpc_auth failed.\n"));
1135 /* Now that we have the auth len store it into the right place in
1136 * the dcerpc header */
1137 dcerpc_set_frag_length(&p->out_data.frag,
1138 p->out_data.frag.length + auth_blob.length);
1140 if (auth_blob.length) {
1142 if (!data_blob_append(p->mem_ctx, &p->out_data.frag,
1143 auth_blob.data, auth_blob.length)) {
1144 DEBUG(0, ("Append of auth info failed.\n"));
1150 * Setup the lengths for the initial reply.
1153 p->out_data.data_sent_length = 0;
1154 p->out_data.current_pdu_sent = 0;
1156 TALLOC_FREE(auth_blob.data);
1161 data_blob_free(&p->out_data.frag);
1162 TALLOC_FREE(auth_blob.data);
1163 return setup_bind_nak(p, pkt);
1166 /*******************************************************************
1167 This is the "stage3" response after a bind request and reply.
1168 *******************************************************************/
1170 bool api_pipe_bind_auth3(struct pipes_struct *p, struct ncacn_packet *pkt)
1172 struct dcerpc_auth auth_info;
1173 DATA_BLOB response = data_blob_null;
1174 struct auth_ntlmssp_state *ntlmssp_ctx;
1175 struct spnego_context *spnego_ctx;
1176 struct gse_context *gse_ctx;
1179 DEBUG(5, ("api_pipe_bind_auth3: decode request. %d\n", __LINE__));
1181 if (pkt->auth_length == 0) {
1182 DEBUG(0, ("No auth field sent for bind request!\n"));
1186 /* Ensure there's enough data for an authenticated request. */
1187 if (pkt->frag_length < RPC_HEADER_LEN
1188 + DCERPC_AUTH_TRAILER_LENGTH
1189 + pkt->auth_length) {
1190 DEBUG(0,("api_pipe_ntlmssp_auth_process: auth_len "
1191 "%u is too large.\n",
1192 (unsigned int)pkt->auth_length));
1197 * Decode the authentication verifier response.
1200 status = dcerpc_pull_dcerpc_auth(pkt,
1201 &pkt->u.auth3.auth_info,
1202 &auth_info, p->endian);
1203 if (!NT_STATUS_IS_OK(status)) {
1204 DEBUG(0, ("Failed to unmarshall dcerpc_auth.\n"));
1208 /* We must NEVER look at auth_info->auth_pad_len here,
1209 * as old Samba client code gets it wrong and sends it
1213 switch (auth_info.auth_type) {
1214 case DCERPC_AUTH_TYPE_NTLMSSP:
1215 ntlmssp_ctx = talloc_get_type_abort(p->auth.auth_ctx,
1216 struct auth_ntlmssp_state);
1217 status = ntlmssp_server_step(ntlmssp_ctx,
1218 pkt, &auth_info.credentials,
1221 case DCERPC_AUTH_TYPE_KRB5:
1222 gse_ctx = talloc_get_type_abort(p->auth.auth_ctx,
1223 struct gse_context);
1224 status = gssapi_server_step(gse_ctx,
1225 pkt, &auth_info.credentials,
1228 case DCERPC_AUTH_TYPE_SPNEGO:
1229 spnego_ctx = talloc_get_type_abort(p->auth.auth_ctx,
1230 struct spnego_context);
1231 status = spnego_server_step(spnego_ctx,
1232 pkt, &auth_info.credentials,
1236 DEBUG(0, (__location__ ": incorrect auth type (%u).\n",
1237 (unsigned int)auth_info.auth_type));
1241 if (NT_STATUS_EQUAL(status,
1242 NT_STATUS_MORE_PROCESSING_REQUIRED) ||
1244 DEBUG(0, (__location__ ": This was supposed to be the final "
1245 "leg, but crypto machinery claims a response is "
1246 "needed, aborting auth!\n"));
1247 data_blob_free(&response);
1250 if (!NT_STATUS_IS_OK(status)) {
1251 DEBUG(0, ("Auth failed (%s)\n", nt_errstr(status)));
1255 /* Now verify auth was indeed successful and extract server info */
1256 status = pipe_auth_verify_final(p);
1257 if (!NT_STATUS_IS_OK(status)) {
1258 DEBUG(0, ("Auth Verify failed (%s)\n", nt_errstr(status)));
1266 free_pipe_auth_data(&p->auth);
1270 /****************************************************************************
1271 Deal with an alter context call. Can be third part of 3 leg auth request for
1273 ****************************************************************************/
1275 static bool api_pipe_alter_context(struct pipes_struct *p,
1276 struct ncacn_packet *pkt)
1278 struct dcerpc_auth auth_info;
1281 union dcerpc_payload u;
1282 struct dcerpc_ack_ctx bind_ack_ctx;
1283 DATA_BLOB auth_resp = data_blob_null;
1284 DATA_BLOB auth_blob = data_blob_null;
1286 struct auth_ntlmssp_state *ntlmssp_ctx;
1287 struct spnego_context *spnego_ctx;
1288 struct gse_context *gse_ctx;
1290 DEBUG(5,("api_pipe_alter_context: make response. %d\n", __LINE__));
1292 if (pkt->u.bind.assoc_group_id != 0) {
1293 assoc_gid = pkt->u.bind.assoc_group_id;
1299 * Create the bind response struct.
1302 /* If the requested abstract synt uuid doesn't match our client pipe,
1303 reject the bind_ack & set the transfer interface synt to all 0's,
1304 ver 0 (observed when NT5 attempts to bind to abstract interfaces
1306 Needed when adding entries to a DACL from NT5 - SK */
1308 if (check_bind_req(p,
1309 &pkt->u.bind.ctx_list[0].abstract_syntax,
1310 &pkt->u.bind.ctx_list[0].transfer_syntaxes[0],
1311 pkt->u.bind.ctx_list[0].context_id)) {
1313 bind_ack_ctx.result = 0;
1314 bind_ack_ctx.reason = 0;
1315 bind_ack_ctx.syntax = pkt->u.bind.ctx_list[0].transfer_syntaxes[0];
1317 p->pipe_bound = False;
1318 /* Rejection reason: abstract syntax not supported */
1319 bind_ack_ctx.result = DCERPC_BIND_PROVIDER_REJECT;
1320 bind_ack_ctx.reason = DCERPC_BIND_REASON_ASYNTAX;
1321 bind_ack_ctx.syntax = null_ndr_syntax_id;
1325 * Check if this is an authenticated alter context request.
1327 if (pkt->auth_length) {
1328 /* Quick length check. Won't catch a bad auth footer,
1329 * prevents overrun. */
1331 if (pkt->frag_length < RPC_HEADER_LEN +
1332 DCERPC_AUTH_TRAILER_LENGTH +
1334 DEBUG(0,("api_pipe_alter_context: auth_len (%u) "
1335 "too long for fragment %u.\n",
1336 (unsigned int)pkt->auth_length,
1337 (unsigned int)pkt->frag_length ));
1341 status = dcerpc_pull_dcerpc_auth(pkt,
1342 &pkt->u.bind.auth_info,
1343 &auth_info, p->endian);
1344 if (!NT_STATUS_IS_OK(status)) {
1345 DEBUG(0, ("Unable to unmarshall dcerpc_auth.\n"));
1349 /* We can only finish if the pipe is unbound for now */
1350 if (p->pipe_bound) {
1351 DEBUG(0, (__location__ ": Pipe already bound, "
1352 "Altering Context not yet supported!\n"));
1356 switch (auth_info.auth_type) {
1357 case DCERPC_AUTH_TYPE_SPNEGO:
1358 spnego_ctx = talloc_get_type_abort(p->auth.auth_ctx,
1359 struct spnego_context);
1360 status = spnego_server_step(spnego_ctx,
1362 &auth_info.credentials,
1366 case DCERPC_AUTH_TYPE_KRB5:
1367 gse_ctx = talloc_get_type_abort(p->auth.auth_ctx,
1368 struct gse_context);
1369 status = gssapi_server_step(gse_ctx,
1371 &auth_info.credentials,
1374 case DCERPC_AUTH_TYPE_NTLMSSP:
1375 ntlmssp_ctx = talloc_get_type_abort(p->auth.auth_ctx,
1376 struct auth_ntlmssp_state);
1377 status = ntlmssp_server_step(ntlmssp_ctx,
1379 &auth_info.credentials,
1384 DEBUG(3, (__location__ ": Usupported auth type (%d) "
1385 "in alter-context call\n",
1386 auth_info.auth_type));
1390 if (NT_STATUS_IS_OK(status)) {
1391 /* third leg of auth, verify auth info */
1392 status = pipe_auth_verify_final(p);
1393 if (!NT_STATUS_IS_OK(status)) {
1394 DEBUG(0, ("Auth Verify failed (%s)\n",
1395 nt_errstr(status)));
1398 } else if (NT_STATUS_EQUAL(status,
1399 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1400 DEBUG(10, ("More auth legs required.\n"));
1402 DEBUG(0, ("Auth step returned an error (%s)\n",
1403 nt_errstr(status)));
1408 ZERO_STRUCT(u.alter_resp);
1409 u.alter_resp.max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
1410 u.alter_resp.max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
1411 u.alter_resp.assoc_group_id = assoc_gid;
1413 /* secondary address CAN be NULL
1414 * as the specs say it's ignored.
1415 * It MUST be NULL to have the spoolss working.
1417 u.alter_resp.secondary_address = "";
1418 u.alter_resp.secondary_address_size = 1;
1420 u.alter_resp.num_results = 1;
1421 u.alter_resp.ctx_list = &bind_ack_ctx;
1423 /* NOTE: We leave the auth_info empty so we can calculate the padding
1424 * later and then append the auth_info --simo */
1427 * Marshall directly into the outgoing PDU space. We
1428 * must do this as we need to set to the bind response
1429 * header and are never sending more than one PDU here.
1432 status = dcerpc_push_ncacn_packet(p->mem_ctx,
1433 DCERPC_PKT_ALTER_RESP,
1434 DCERPC_PFC_FLAG_FIRST |
1435 DCERPC_PFC_FLAG_LAST,
1440 if (!NT_STATUS_IS_OK(status)) {
1441 DEBUG(0, ("Failed to marshall bind_ack packet. (%s)\n",
1442 nt_errstr(status)));
1445 if (auth_resp.length) {
1447 /* Work out any padding needed before the auth footer. */
1448 pad_len = p->out_data.frag.length % SERVER_NDR_PADDING_SIZE;
1450 pad_len = SERVER_NDR_PADDING_SIZE - pad_len;
1451 DEBUG(10, ("auth pad_len = %u\n",
1452 (unsigned int)pad_len));
1455 status = dcerpc_push_dcerpc_auth(pkt,
1456 auth_info.auth_type,
1457 auth_info.auth_level,
1459 1, /* auth_context_id */
1462 if (!NT_STATUS_IS_OK(status)) {
1463 DEBUG(0, ("Marshalling of dcerpc_auth failed.\n"));
1468 /* Now that we have the auth len store it into the right place in
1469 * the dcerpc header */
1470 dcerpc_set_frag_length(&p->out_data.frag,
1471 p->out_data.frag.length +
1472 pad_len + auth_blob.length);
1474 if (auth_resp.length) {
1476 char pad[SERVER_NDR_PADDING_SIZE];
1477 memset(pad, '\0', SERVER_NDR_PADDING_SIZE);
1478 if (!data_blob_append(p->mem_ctx,
1481 DEBUG(0, ("api_pipe_bind_req: failed to add "
1482 "%u bytes of pad data.\n",
1483 (unsigned int)pad_len));
1488 if (!data_blob_append(p->mem_ctx, &p->out_data.frag,
1489 auth_blob.data, auth_blob.length)) {
1490 DEBUG(0, ("Append of auth info failed.\n"));
1496 * Setup the lengths for the initial reply.
1499 p->out_data.data_sent_length = 0;
1500 p->out_data.current_pdu_sent = 0;
1502 TALLOC_FREE(auth_blob.data);
1507 data_blob_free(&p->out_data.frag);
1508 TALLOC_FREE(auth_blob.data);
1509 return setup_bind_nak(p, pkt);
1512 /****************************************************************************
1513 Find the set of RPC functions associated with this context_id
1514 ****************************************************************************/
1516 static PIPE_RPC_FNS* find_pipe_fns_by_context( PIPE_RPC_FNS *list, uint32 context_id )
1518 PIPE_RPC_FNS *fns = NULL;
1521 DEBUG(0,("find_pipe_fns_by_context: ERROR! No context list for pipe!\n"));
1525 for (fns=list; fns; fns=fns->next ) {
1526 if ( fns->context_id == context_id )
1532 static bool api_rpcTNP(struct pipes_struct *p, struct ncacn_packet *pkt,
1533 const struct api_struct *api_rpc_cmds, int n_cmds);
1535 /****************************************************************************
1536 Find the correct RPC function to call for this request.
1537 If the pipe is authenticated then become the correct UNIX user
1538 before doing the call.
1539 ****************************************************************************/
1541 static bool api_pipe_request(struct pipes_struct *p,
1542 struct ncacn_packet *pkt)
1545 bool changed_user = False;
1546 PIPE_RPC_FNS *pipe_fns;
1548 if (p->pipe_bound &&
1549 ((p->auth.auth_type == DCERPC_AUTH_TYPE_NTLMSSP) ||
1550 (p->auth.auth_type == DCERPC_AUTH_TYPE_KRB5) ||
1551 (p->auth.auth_type == DCERPC_AUTH_TYPE_SPNEGO))) {
1552 if(!become_authenticated_pipe_user(p)) {
1553 data_blob_free(&p->out_data.rdata);
1556 changed_user = True;
1559 DEBUG(5, ("Requested \\PIPE\\%s\n",
1560 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1562 /* get the set of RPC functions for this context */
1564 pipe_fns = find_pipe_fns_by_context(p->contexts,
1565 pkt->u.request.context_id);
1568 TALLOC_CTX *frame = talloc_stackframe();
1569 ret = api_rpcTNP(p, pkt, pipe_fns->cmds, pipe_fns->n_cmds);
1573 DEBUG(0, ("No rpc function table associated with context "
1574 "[%d] on pipe [%s]\n",
1575 pkt->u.request.context_id,
1576 get_pipe_name_from_syntax(talloc_tos(),
1581 unbecome_authenticated_pipe_user();
1587 /*******************************************************************
1588 Calls the underlying RPC function for a named pipe.
1589 ********************************************************************/
1591 static bool api_rpcTNP(struct pipes_struct *p, struct ncacn_packet *pkt,
1592 const struct api_struct *api_rpc_cmds, int n_cmds)
1597 /* interpret the command */
1598 DEBUG(4,("api_rpcTNP: %s op 0x%x - ",
1599 get_pipe_name_from_syntax(talloc_tos(), &p->syntax),
1600 pkt->u.request.opnum));
1602 if (DEBUGLEVEL >= 50) {
1604 slprintf(name, sizeof(name)-1, "in_%s",
1605 get_pipe_name_from_syntax(talloc_tos(), &p->syntax));
1606 dump_pdu_region(name, pkt->u.request.opnum,
1607 &p->in_data.data, 0,
1608 p->in_data.data.length);
1611 for (fn_num = 0; fn_num < n_cmds; fn_num++) {
1612 if (api_rpc_cmds[fn_num].opnum == pkt->u.request.opnum &&
1613 api_rpc_cmds[fn_num].fn != NULL) {
1614 DEBUG(3, ("api_rpcTNP: rpc command: %s\n",
1615 api_rpc_cmds[fn_num].name));
1620 if (fn_num == n_cmds) {
1622 * For an unknown RPC just return a fault PDU but
1623 * return True to allow RPC's on the pipe to continue
1624 * and not put the pipe into fault state. JRA.
1626 DEBUG(4, ("unknown\n"));
1627 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
1631 offset1 = p->out_data.rdata.length;
1633 DEBUG(6, ("api_rpc_cmds[%d].fn == %p\n",
1634 fn_num, api_rpc_cmds[fn_num].fn));
1635 /* do the actual command */
1636 if(!api_rpc_cmds[fn_num].fn(p)) {
1637 DEBUG(0,("api_rpcTNP: %s: %s failed.\n",
1638 get_pipe_name_from_syntax(talloc_tos(), &p->syntax),
1639 api_rpc_cmds[fn_num].name));
1640 data_blob_free(&p->out_data.rdata);
1644 if (p->bad_handle_fault_state) {
1645 DEBUG(4,("api_rpcTNP: bad handle fault return.\n"));
1646 p->bad_handle_fault_state = False;
1647 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_CONTEXT_MISMATCH));
1651 if (p->rng_fault_state) {
1652 DEBUG(4, ("api_rpcTNP: rng fault return\n"));
1653 p->rng_fault_state = False;
1654 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
1658 if (DEBUGLEVEL >= 50) {
1660 slprintf(name, sizeof(name)-1, "out_%s",
1661 get_pipe_name_from_syntax(talloc_tos(), &p->syntax));
1662 dump_pdu_region(name, pkt->u.request.opnum,
1663 &p->out_data.rdata, offset1,
1664 p->out_data.rdata.length);
1667 DEBUG(5,("api_rpcTNP: called %s successfully\n",
1668 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1670 /* Check for buffer underflow in rpc parsing */
1671 if ((DEBUGLEVEL >= 10) &&
1672 (pkt->frag_length < p->in_data.data.length)) {
1673 DEBUG(10, ("api_rpcTNP: rpc input buffer underflow (parse error?)\n"));
1674 dump_data(10, p->in_data.data.data + pkt->frag_length,
1675 p->in_data.data.length - pkt->frag_length);
1681 /****************************************************************************
1682 Initialise an outgoing packet.
1683 ****************************************************************************/
1685 static bool pipe_init_outgoing_data(struct pipes_struct *p)
1687 output_data *o_data = &p->out_data;
1689 /* Reset the offset counters. */
1690 o_data->data_sent_length = 0;
1691 o_data->current_pdu_sent = 0;
1693 data_blob_free(&o_data->frag);
1695 /* Free any memory in the current return data buffer. */
1696 data_blob_free(&o_data->rdata);
1701 /****************************************************************************
1702 Sets the fault state on incoming packets.
1703 ****************************************************************************/
1705 void set_incoming_fault(struct pipes_struct *p)
1707 data_blob_free(&p->in_data.data);
1708 p->in_data.pdu_needed_len = 0;
1709 p->in_data.pdu.length = 0;
1710 p->fault_state = True;
1711 DEBUG(10, ("set_incoming_fault: Setting fault state on pipe %s\n",
1712 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1715 static NTSTATUS dcesrv_auth_request(struct pipe_auth_data *auth,
1716 struct ncacn_packet *pkt,
1720 size_t hdr_size = DCERPC_REQUEST_LENGTH;
1723 DEBUG(10, ("Checking request auth.\n"));
1725 if (pkt->pfc_flags & DCERPC_PFC_FLAG_OBJECT_UUID) {
1729 /* in case of sealing this function will unseal the data in place */
1730 status = dcerpc_check_auth(auth, pkt,
1731 &pkt->u.request.stub_and_verifier,
1734 if (!NT_STATUS_IS_OK(status)) {
1739 /* remove padding and auth trailer,
1740 * this way the caller will get just the data */
1741 if (pkt->auth_length) {
1742 size_t trail_len = pad_len
1743 + DCERPC_AUTH_TRAILER_LENGTH
1745 if (pkt->u.request.stub_and_verifier.length < trail_len) {
1746 return NT_STATUS_INFO_LENGTH_MISMATCH;
1748 pkt->u.request.stub_and_verifier.length -= trail_len;
1751 return NT_STATUS_OK;
1754 /****************************************************************************
1755 Processes a request pdu. This will do auth processing if needed, and
1756 appends the data into the complete stream if the LAST flag is not set.
1757 ****************************************************************************/
1759 static bool process_request_pdu(struct pipes_struct *p, struct ncacn_packet *pkt)
1764 if (!p->pipe_bound) {
1765 DEBUG(0,("process_request_pdu: rpc request with no bind.\n"));
1766 set_incoming_fault(p);
1770 /* Store the opnum */
1771 p->opnum = pkt->u.request.opnum;
1773 status = dcesrv_auth_request(&p->auth, pkt, &p->in_data.pdu);
1774 if (!NT_STATUS_IS_OK(status)) {
1775 DEBUG(0, ("Failed to check packet auth. (%s)\n",
1776 nt_errstr(status)));
1777 set_incoming_fault(p);
1781 data = pkt->u.request.stub_and_verifier;
1784 * Check the data length doesn't go over the 15Mb limit.
1785 * increased after observing a bug in the Windows NT 4.0 SP6a
1786 * spoolsv.exe when the response to a GETPRINTERDRIVER2 RPC
1787 * will not fit in the initial buffer of size 0x1068 --jerry 22/01/2002
1790 if (p->in_data.data.length + data.length > MAX_RPC_DATA_SIZE) {
1791 DEBUG(0, ("process_request_pdu: "
1792 "rpc data buffer too large (%u) + (%u)\n",
1793 (unsigned int)p->in_data.data.length,
1794 (unsigned int)data.length));
1795 set_incoming_fault(p);
1800 * Append the data portion into the buffer and return.
1804 if (!data_blob_append(p->mem_ctx, &p->in_data.data,
1805 data.data, data.length)) {
1806 DEBUG(0, ("Unable to append data size %u "
1807 "to parse buffer of size %u.\n",
1808 (unsigned int)data.length,
1809 (unsigned int)p->in_data.data.length));
1810 set_incoming_fault(p);
1815 if (pkt->pfc_flags & DCERPC_PFC_FLAG_LAST) {
1818 * Ok - we finally have a complete RPC stream.
1819 * Call the rpc command to process it.
1823 * Process the complete data stream here.
1825 if (pipe_init_outgoing_data(p)) {
1826 ret = api_pipe_request(p, pkt);
1835 /****************************************************************************
1836 Processes a finished PDU stored in p->in_data.pdu.
1837 ****************************************************************************/
1839 void process_complete_pdu(struct pipes_struct *p)
1841 struct ncacn_packet *pkt = NULL;
1845 if(p->fault_state) {
1846 DEBUG(10,("process_complete_pdu: pipe %s in fault state.\n",
1847 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1851 pkt = talloc(p->mem_ctx, struct ncacn_packet);
1853 DEBUG(0, ("Out of memory!\n"));
1858 * Ensure we're using the corrent endianness for both the
1859 * RPC header flags and the raw data we will be reading from.
1861 if (dcerpc_get_endian_flag(&p->in_data.pdu) & DCERPC_DREP_LE) {
1862 p->endian = RPC_LITTLE_ENDIAN;
1864 p->endian = RPC_BIG_ENDIAN;
1866 DEBUG(10, ("PDU is in %s Endian format!\n", p->endian?"Big":"Little"));
1868 status = dcerpc_pull_ncacn_packet(pkt, &p->in_data.pdu,
1870 if (!NT_STATUS_IS_OK(status)) {
1871 DEBUG(0, ("Failed to unmarshal rpc packet: %s!\n",
1872 nt_errstr(status)));
1876 /* Store the call_id */
1877 p->call_id = pkt->call_id;
1879 DEBUG(10, ("Processing packet type %d\n", (int)pkt->ptype));
1881 switch (pkt->ptype) {
1882 case DCERPC_PKT_REQUEST:
1883 reply = process_request_pdu(p, pkt);
1886 case DCERPC_PKT_PING: /* CL request - ignore... */
1887 DEBUG(0, ("process_complete_pdu: Error. "
1888 "Connectionless packet type %d received on "
1889 "pipe %s.\n", (int)pkt->ptype,
1890 get_pipe_name_from_syntax(talloc_tos(),
1894 case DCERPC_PKT_RESPONSE: /* No responses here. */
1895 DEBUG(0, ("process_complete_pdu: Error. "
1896 "DCERPC_PKT_RESPONSE received from client "
1898 get_pipe_name_from_syntax(talloc_tos(),
1902 case DCERPC_PKT_FAULT:
1903 case DCERPC_PKT_WORKING:
1904 /* CL request - reply to a ping when a call in process. */
1905 case DCERPC_PKT_NOCALL:
1906 /* CL - server reply to a ping call. */
1907 case DCERPC_PKT_REJECT:
1908 case DCERPC_PKT_ACK:
1909 case DCERPC_PKT_CL_CANCEL:
1910 case DCERPC_PKT_FACK:
1911 case DCERPC_PKT_CANCEL_ACK:
1912 DEBUG(0, ("process_complete_pdu: Error. "
1913 "Connectionless packet type %u received on "
1914 "pipe %s.\n", (unsigned int)pkt->ptype,
1915 get_pipe_name_from_syntax(talloc_tos(),
1919 case DCERPC_PKT_BIND:
1921 * We assume that a pipe bind is only in one pdu.
1923 if (pipe_init_outgoing_data(p)) {
1924 reply = api_pipe_bind_req(p, pkt);
1928 case DCERPC_PKT_BIND_ACK:
1929 case DCERPC_PKT_BIND_NAK:
1930 DEBUG(0, ("process_complete_pdu: Error. "
1931 "DCERPC_PKT_BINDACK/DCERPC_PKT_BINDNACK "
1932 "packet type %u received on pipe %s.\n",
1933 (unsigned int)pkt->ptype,
1934 get_pipe_name_from_syntax(talloc_tos(),
1939 case DCERPC_PKT_ALTER:
1941 * We assume that a pipe bind is only in one pdu.
1943 if (pipe_init_outgoing_data(p)) {
1944 reply = api_pipe_alter_context(p, pkt);
1948 case DCERPC_PKT_ALTER_RESP:
1949 DEBUG(0, ("process_complete_pdu: Error. "
1950 "DCERPC_PKT_ALTER_RESP on pipe %s: "
1951 "Should only be server -> client.\n",
1952 get_pipe_name_from_syntax(talloc_tos(),
1956 case DCERPC_PKT_AUTH3:
1958 * The third packet in an auth exchange.
1960 if (pipe_init_outgoing_data(p)) {
1961 reply = api_pipe_bind_auth3(p, pkt);
1965 case DCERPC_PKT_SHUTDOWN:
1966 DEBUG(0, ("process_complete_pdu: Error. "
1967 "DCERPC_PKT_SHUTDOWN on pipe %s: "
1968 "Should only be server -> client.\n",
1969 get_pipe_name_from_syntax(talloc_tos(),
1973 case DCERPC_PKT_CO_CANCEL:
1974 /* For now just free all client data and continue
1976 DEBUG(3,("process_complete_pdu: DCERPC_PKT_CO_CANCEL."
1977 " Abandoning rpc call.\n"));
1978 /* As we never do asynchronous RPC serving, we can
1979 * never cancel a call (as far as I know).
1980 * If we ever did we'd have to send a cancel_ack reply.
1981 * For now, just free all client data and continue
1987 /* Enable this if we're doing async rpc. */
1988 /* We must check the outstanding callid matches. */
1989 if (pipe_init_outgoing_data(p)) {
1990 /* Send a cancel_ack PDU reply. */
1991 /* We should probably check the auth-verifier here. */
1992 reply = setup_cancel_ack_reply(p, pkt);
1997 case DCERPC_PKT_ORPHANED:
1998 /* We should probably check the auth-verifier here.
1999 * For now just free all client data and continue
2001 DEBUG(3, ("process_complete_pdu: DCERPC_PKT_ORPHANED."
2002 " Abandoning rpc call.\n"));
2007 DEBUG(0, ("process_complete_pdu: "
2008 "Unknown rpc type = %u received.\n",
2009 (unsigned int)pkt->ptype));
2015 DEBUG(3,("process_complete_pdu: DCE/RPC fault sent on "
2016 "pipe %s\n", get_pipe_name_from_syntax(talloc_tos(),
2018 set_incoming_fault(p);
2019 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
2023 * Reset the lengths. We're ready for a new pdu.
2025 TALLOC_FREE(p->in_data.pdu.data);
2026 p->in_data.pdu_needed_len = 0;
2027 p->in_data.pdu.length = 0;