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 "system/filesys.h"
32 #include "srv_pipe_internal.h"
33 #include "../librpc/gen_ndr/ndr_schannel.h"
34 #include "../libcli/auth/schannel.h"
35 #include "../libcli/auth/spnego.h"
36 #include "dcesrv_ntlmssp.h"
37 #include "dcesrv_gssapi.h"
38 #include "dcesrv_spnego.h"
39 #include "rpc_server.h"
41 #include "smbd/smbd.h"
44 #include "rpc_server/srv_pipe.h"
47 #define DBGC_CLASS DBGC_RPC_SRV
50 * Dump everything from the start of the end up of the provided data
51 * into a file, but only at debug level >= 50
53 static void dump_pdu_region(const char *name, int v,
54 DATA_BLOB *data, size_t start, size_t end)
60 if (DEBUGLEVEL < 50) return;
62 if (start > data->length || end > data->length || start > end) return;
64 for (i = 1; i < 100; i++) {
66 fname = talloc_asprintf(talloc_tos(),
70 fname = talloc_asprintf(talloc_tos(),
77 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0644);
78 if (fd != -1 || errno != EEXIST) break;
81 sz = write(fd, data->data + start, end - start);
83 if ((sz != end - start) || (i != 0) ) {
84 DEBUG(0, ("Error writing/closing %s: %ld!=%ld %d\n",
85 fname, (unsigned long)sz,
86 (unsigned long)end - start, i));
88 DEBUG(0,("created %s\n", fname));
94 static DATA_BLOB generic_session_key(void)
96 return data_blob_const("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 TALLOC_FREE(p->auth.auth_ctx);
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_type = DCERPC_AUTH_TYPE_SPNEGO;
449 DEBUG(10, ("SPNEGO auth started\n"));
454 /*******************************************************************
455 Handle an schannel bind auth.
456 *******************************************************************/
458 static bool pipe_schannel_auth_bind(struct pipes_struct *p,
460 struct dcerpc_auth *auth_info,
463 struct NL_AUTH_MESSAGE neg;
464 struct NL_AUTH_MESSAGE reply;
467 struct netlogon_creds_CredentialState *creds;
468 enum ndr_err_code ndr_err;
469 struct schannel_state *schannel_auth;
471 ndr_err = ndr_pull_struct_blob(
472 &auth_info->credentials, mem_ctx, &neg,
473 (ndr_pull_flags_fn_t)ndr_pull_NL_AUTH_MESSAGE);
474 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
475 DEBUG(0,("pipe_schannel_auth_bind: Could not unmarshal SCHANNEL auth neg\n"));
479 if (DEBUGLEVEL >= 10) {
480 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &neg);
483 if (!(neg.Flags & NL_FLAG_OEM_NETBIOS_COMPUTER_NAME)) {
484 DEBUG(0,("pipe_schannel_auth_bind: Did not receive netbios computer name\n"));
489 * The neg.oem_netbios_computer.a key here must match the remote computer name
490 * given in the DOM_CLNT_SRV.uni_comp_name used on all netlogon pipe
491 * operations that use credentials.
495 status = schannel_get_creds_state(p, lp_private_dir(),
496 neg.oem_netbios_computer.a, &creds);
499 if (!NT_STATUS_IS_OK(status)) {
500 DEBUG(0, ("pipe_schannel_auth_bind: Attempt to bind using schannel without successful serverauth2\n"));
504 schannel_auth = talloc(p, struct schannel_state);
505 if (!schannel_auth) {
510 schannel_auth->state = SCHANNEL_STATE_START;
511 schannel_auth->seq_num = 0;
512 schannel_auth->initiator = false;
513 schannel_auth->creds = creds;
516 * JRA. Should we also copy the schannel session key into the pipe session key p->session_key
517 * here ? We do that for NTLMSSP, but the session key is already set up from the vuser
518 * struct of the person who opened the pipe. I need to test this further. JRA.
520 * VL. As we are mapping this to guest set the generic key
521 * "SystemLibraryDTC" key here. It's a bit difficult to test against
522 * W2k3, as it does not allow schannel binds against SAMR and LSA
526 ret = session_info_set_session_key(p->session_info, generic_session_key());
529 DEBUG(0, ("session_info_set_session_key failed\n"));
533 /*** SCHANNEL verifier ***/
535 reply.MessageType = NL_NEGOTIATE_RESPONSE;
537 reply.Buffer.dummy = 5; /* ??? actually I don't think
538 * this has any meaning
541 ndr_err = ndr_push_struct_blob(response, mem_ctx, &reply,
542 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
543 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
544 DEBUG(0,("Failed to marshall NL_AUTH_MESSAGE.\n"));
548 if (DEBUGLEVEL >= 10) {
549 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &reply);
552 DEBUG(10,("pipe_schannel_auth_bind: schannel auth: domain [%s] myname [%s]\n",
553 neg.oem_netbios_domain.a, neg.oem_netbios_computer.a));
555 /* We're finished with this bind - no more packets. */
556 p->auth.auth_ctx = schannel_auth;
557 p->auth.auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
559 p->pipe_bound = True;
564 /*******************************************************************
565 Handle an NTLMSSP bind auth.
566 *******************************************************************/
568 static bool pipe_ntlmssp_auth_bind(struct pipes_struct *p,
570 struct dcerpc_auth *auth_info,
573 struct auth_ntlmssp_state *ntlmssp_state = NULL;
576 if (strncmp((char *)auth_info->credentials.data, "NTLMSSP", 7) != 0) {
577 DEBUG(0, ("Failed to read NTLMSSP in blob\n"));
581 /* We have an NTLMSSP blob. */
582 status = ntlmssp_server_auth_start(p,
583 (auth_info->auth_level ==
584 DCERPC_AUTH_LEVEL_INTEGRITY),
585 (auth_info->auth_level ==
586 DCERPC_AUTH_LEVEL_PRIVACY),
588 &auth_info->credentials,
591 if (!NT_STATUS_EQUAL(status, NT_STATUS_OK)) {
592 DEBUG(0, (__location__ ": auth_ntlmssp_start failed: %s\n",
597 /* Make sure data is bound to the memctx, to be freed the caller */
598 talloc_steal(mem_ctx, response->data);
600 p->auth.auth_ctx = ntlmssp_state;
601 p->auth.auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
603 DEBUG(10, (__location__ ": NTLMSSP auth started\n"));
608 /*******************************************************************
609 Process an NTLMSSP authentication response.
610 If this function succeeds, the user has been authenticated
611 and their domain, name and calling workstation stored in
613 *******************************************************************/
615 static bool pipe_ntlmssp_verify_final(TALLOC_CTX *mem_ctx,
616 struct auth_ntlmssp_state *ntlmssp_ctx,
617 enum dcerpc_AuthLevel auth_level,
618 struct client_address *client_id,
619 struct ndr_syntax_id *syntax,
620 struct auth_serversupplied_info **session_info)
625 DEBUG(5, (__location__ ": pipe %s checking user details\n",
626 get_pipe_name_from_syntax(talloc_tos(), syntax)));
628 /* Finally - if the pipe negotiated integrity (sign) or privacy (seal)
629 ensure the underlying NTLMSSP flags are also set. If not we should
632 status = ntlmssp_server_check_flags(ntlmssp_ctx,
634 DCERPC_AUTH_LEVEL_INTEGRITY),
636 DCERPC_AUTH_LEVEL_PRIVACY));
637 if (!NT_STATUS_IS_OK(status)) {
638 DEBUG(0, (__location__ ": Client failed to negotatie proper "
639 "security for pipe %s\n",
640 get_pipe_name_from_syntax(talloc_tos(), syntax)));
644 TALLOC_FREE(*session_info);
646 status = ntlmssp_server_get_user_info(ntlmssp_ctx,
647 mem_ctx, session_info);
648 if (!NT_STATUS_IS_OK(status)) {
649 DEBUG(0, (__location__ ": failed to obtain the server info "
650 "for authenticated user: %s\n", nt_errstr(status)));
654 if ((*session_info)->security_token == NULL) {
655 DEBUG(1, ("Auth module failed to provide nt_user_token\n"));
660 * We're an authenticated bind over smb, so the session key needs to
661 * be set to "SystemLibraryDTC". Weird, but this is what Windows
662 * does. See the RPC-SAMBA3SESSIONKEY.
665 ret = session_info_set_session_key((*session_info), generic_session_key());
667 DEBUG(0, ("Failed to set session key!\n"));
674 /*******************************************************************
675 Handle a GSSAPI bind auth.
676 *******************************************************************/
678 static bool pipe_gssapi_auth_bind(struct pipes_struct *p,
680 struct dcerpc_auth *auth_info,
684 struct gse_context *gse_ctx = NULL;
686 status = gssapi_server_auth_start(p,
687 (auth_info->auth_level ==
688 DCERPC_AUTH_LEVEL_INTEGRITY),
689 (auth_info->auth_level ==
690 DCERPC_AUTH_LEVEL_PRIVACY),
692 &auth_info->credentials,
695 if (!NT_STATUS_IS_OK(status)) {
696 DEBUG(0, ("Failed to init dcerpc gssapi server (%s)\n",
701 /* Make sure data is bound to the memctx, to be freed the caller */
702 talloc_steal(mem_ctx, response->data);
704 p->auth.auth_ctx = gse_ctx;
705 p->auth.auth_type = DCERPC_AUTH_TYPE_KRB5;
707 DEBUG(10, ("KRB5 auth started\n"));
712 TALLOC_FREE(gse_ctx);
716 static NTSTATUS pipe_gssapi_verify_final(TALLOC_CTX *mem_ctx,
717 struct gse_context *gse_ctx,
718 struct client_address *client_id,
719 struct auth_serversupplied_info **session_info)
724 /* Finally - if the pipe negotiated integrity (sign) or privacy (seal)
725 ensure the underlying flags are also set. If not we should
728 status = gssapi_server_check_flags(gse_ctx);
729 if (!NT_STATUS_IS_OK(status)) {
730 DEBUG(0, ("Requested Security Layers not honored!\n"));
734 status = gssapi_server_get_user_info(gse_ctx, mem_ctx,
735 client_id, session_info);
736 if (!NT_STATUS_IS_OK(status)) {
737 DEBUG(0, (__location__ ": failed to obtain the server info "
738 "for authenticated user: %s\n", nt_errstr(status)));
743 * We're an authenticated bind over smb, so the session key needs to
744 * be set to "SystemLibraryDTC". Weird, but this is what Windows
745 * does. See the RPC-SAMBA3SESSIONKEY.
748 bret = session_info_set_session_key((*session_info), generic_session_key());
750 return NT_STATUS_ACCESS_DENIED;
756 static NTSTATUS pipe_auth_verify_final(struct pipes_struct *p)
758 enum spnego_mech auth_type;
759 struct auth_ntlmssp_state *ntlmssp_ctx;
760 struct spnego_context *spnego_ctx;
761 struct gse_context *gse_ctx;
765 switch (p->auth.auth_type) {
766 case DCERPC_AUTH_TYPE_NTLMSSP:
767 ntlmssp_ctx = talloc_get_type_abort(p->auth.auth_ctx,
768 struct auth_ntlmssp_state);
769 if (!pipe_ntlmssp_verify_final(p, ntlmssp_ctx,
771 p->client_id, &p->syntax,
773 return NT_STATUS_ACCESS_DENIED;
776 case DCERPC_AUTH_TYPE_KRB5:
777 gse_ctx = talloc_get_type_abort(p->auth.auth_ctx,
779 status = pipe_gssapi_verify_final(p, gse_ctx,
782 if (!NT_STATUS_IS_OK(status)) {
783 DEBUG(1, ("gssapi bind failed with: %s",
788 case DCERPC_AUTH_TYPE_SPNEGO:
789 spnego_ctx = talloc_get_type_abort(p->auth.auth_ctx,
790 struct spnego_context);
791 status = spnego_get_negotiated_mech(spnego_ctx,
792 &auth_type, &mech_ctx);
793 if (!NT_STATUS_IS_OK(status)) {
794 DEBUG(0, ("Bad SPNEGO state (%s)\n",
800 gse_ctx = talloc_get_type_abort(mech_ctx,
802 status = pipe_gssapi_verify_final(p, gse_ctx,
805 if (!NT_STATUS_IS_OK(status)) {
806 DEBUG(1, ("gssapi bind failed with: %s",
812 ntlmssp_ctx = talloc_get_type_abort(mech_ctx,
813 struct auth_ntlmssp_state);
814 if (!pipe_ntlmssp_verify_final(p, ntlmssp_ctx,
819 return NT_STATUS_ACCESS_DENIED;
823 DEBUG(0, (__location__ ": incorrect spnego type "
824 "(%d).\n", auth_type));
825 return NT_STATUS_ACCESS_DENIED;
829 DEBUG(0, (__location__ ": incorrect auth type (%u).\n",
830 (unsigned int)p->auth.auth_type));
831 return NT_STATUS_ACCESS_DENIED;
834 p->pipe_bound = true;
839 /*******************************************************************
840 Respond to a pipe bind request.
841 *******************************************************************/
843 static bool api_pipe_bind_req(struct pipes_struct *p,
844 struct ncacn_packet *pkt)
846 struct dcerpc_auth auth_info;
848 unsigned int auth_type = DCERPC_AUTH_TYPE_NONE;
850 struct ndr_syntax_id id;
851 union dcerpc_payload u;
852 struct dcerpc_ack_ctx bind_ack_ctx;
853 DATA_BLOB auth_resp = data_blob_null;
854 DATA_BLOB auth_blob = data_blob_null;
856 /* No rebinds on a bound pipe - use alter context. */
858 DEBUG(2,("api_pipe_bind_req: rejecting bind request on bound "
860 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
861 return setup_bind_nak(p, pkt);
864 if (pkt->u.bind.num_contexts == 0) {
865 DEBUG(0, ("api_pipe_bind_req: no rpc contexts around\n"));
870 * Try and find the correct pipe name to ensure
871 * that this is a pipe name we support.
873 id = pkt->u.bind.ctx_list[0].abstract_syntax;
874 if (rpc_srv_pipe_exists_by_id(&id)) {
875 DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
876 rpc_srv_get_pipe_cli_name(&id),
877 rpc_srv_get_pipe_srv_name(&id)));
879 status = smb_probe_module(
880 "rpc", get_pipe_name_from_syntax(
882 &pkt->u.bind.ctx_list[0].abstract_syntax));
884 if (NT_STATUS_IS_ERR(status)) {
885 DEBUG(3,("api_pipe_bind_req: Unknown pipe name %s in bind request.\n",
886 get_pipe_name_from_syntax(
888 &pkt->u.bind.ctx_list[0].abstract_syntax)));
890 return setup_bind_nak(p, pkt);
893 if (rpc_srv_get_pipe_interface_by_cli_name(
894 get_pipe_name_from_syntax(talloc_tos(),
897 DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
898 rpc_srv_get_pipe_cli_name(&id),
899 rpc_srv_get_pipe_srv_name(&id)));
901 DEBUG(0, ("module %s doesn't provide functions for "
903 get_pipe_name_from_syntax(talloc_tos(),
905 get_pipe_name_from_syntax(talloc_tos(),
907 return setup_bind_nak(p, pkt);
911 DEBUG(5,("api_pipe_bind_req: make response. %d\n", __LINE__));
913 if (pkt->u.bind.assoc_group_id != 0) {
914 assoc_gid = pkt->u.bind.assoc_group_id;
920 * Create the bind response struct.
923 /* If the requested abstract synt uuid doesn't match our client pipe,
924 reject the bind_ack & set the transfer interface synt to all 0's,
925 ver 0 (observed when NT5 attempts to bind to abstract interfaces
927 Needed when adding entries to a DACL from NT5 - SK */
929 if (check_bind_req(p,
930 &pkt->u.bind.ctx_list[0].abstract_syntax,
931 &pkt->u.bind.ctx_list[0].transfer_syntaxes[0],
932 pkt->u.bind.ctx_list[0].context_id)) {
934 bind_ack_ctx.result = 0;
935 bind_ack_ctx.reason = 0;
936 bind_ack_ctx.syntax = pkt->u.bind.ctx_list[0].transfer_syntaxes[0];
938 p->pipe_bound = False;
939 /* Rejection reason: abstract syntax not supported */
940 bind_ack_ctx.result = DCERPC_BIND_PROVIDER_REJECT;
941 bind_ack_ctx.reason = DCERPC_BIND_REASON_ASYNTAX;
942 bind_ack_ctx.syntax = null_ndr_syntax_id;
946 * Check if this is an authenticated bind request.
948 if (pkt->auth_length) {
949 /* Quick length check. Won't catch a bad auth footer,
950 * prevents overrun. */
952 if (pkt->frag_length < RPC_HEADER_LEN +
953 DCERPC_AUTH_TRAILER_LENGTH +
955 DEBUG(0,("api_pipe_bind_req: auth_len (%u) "
956 "too long for fragment %u.\n",
957 (unsigned int)pkt->auth_length,
958 (unsigned int)pkt->frag_length));
963 * Decode the authentication verifier.
965 status = dcerpc_pull_dcerpc_auth(pkt,
966 &pkt->u.bind.auth_info,
967 &auth_info, p->endian);
968 if (!NT_STATUS_IS_OK(status)) {
969 DEBUG(0, ("Unable to unmarshall dcerpc_auth.\n"));
973 auth_type = auth_info.auth_type;
975 /* Work out if we have to sign or seal etc. */
976 switch (auth_info.auth_level) {
977 case DCERPC_AUTH_LEVEL_INTEGRITY:
978 p->auth.auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
980 case DCERPC_AUTH_LEVEL_PRIVACY:
981 p->auth.auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
983 case DCERPC_AUTH_LEVEL_CONNECT:
984 p->auth.auth_level = DCERPC_AUTH_LEVEL_CONNECT;
987 DEBUG(0, ("Unexpected auth level (%u).\n",
988 (unsigned int)auth_info.auth_level ));
993 case DCERPC_AUTH_TYPE_NTLMSSP:
994 if (!pipe_ntlmssp_auth_bind(p, pkt,
995 &auth_info, &auth_resp)) {
1001 case DCERPC_AUTH_TYPE_SCHANNEL:
1002 if (!pipe_schannel_auth_bind(p, pkt,
1003 &auth_info, &auth_resp)) {
1008 case DCERPC_AUTH_TYPE_SPNEGO:
1009 if (!pipe_spnego_auth_bind(p, pkt,
1010 &auth_info, &auth_resp)) {
1015 case DCERPC_AUTH_TYPE_KRB5:
1016 if (!pipe_gssapi_auth_bind(p, pkt,
1017 &auth_info, &auth_resp)) {
1022 case DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM:
1023 if (p->transport == NCALRPC && p->ncalrpc_as_system) {
1024 TALLOC_FREE(p->session_info);
1026 status = make_session_info_system(p,
1028 if (!NT_STATUS_IS_OK(status)) {
1032 auth_resp = data_blob_talloc(pkt,
1036 p->auth.auth_type = DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM;
1037 p->pipe_bound = true;
1043 case DCERPC_AUTH_TYPE_NONE:
1047 DEBUG(0, ("Unknown auth type %x requested.\n", auth_type));
1052 if (auth_type == DCERPC_AUTH_TYPE_NONE) {
1053 /* Unauthenticated bind request. */
1054 /* We're finished - no more packets. */
1055 p->auth.auth_type = DCERPC_AUTH_TYPE_NONE;
1056 /* We must set the pipe auth_level here also. */
1057 p->auth.auth_level = DCERPC_AUTH_LEVEL_NONE;
1058 p->pipe_bound = True;
1059 /* The session key was initialized from the SMB
1060 * session in make_internal_rpc_pipe_p */
1063 ZERO_STRUCT(u.bind_ack);
1064 u.bind_ack.max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
1065 u.bind_ack.max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
1066 u.bind_ack.assoc_group_id = assoc_gid;
1068 /* name has to be \PIPE\xxxxx */
1069 u.bind_ack.secondary_address =
1070 talloc_asprintf(pkt, "\\PIPE\\%s",
1071 rpc_srv_get_pipe_srv_name(&id));
1072 if (!u.bind_ack.secondary_address) {
1073 DEBUG(0, ("Out of memory!\n"));
1076 u.bind_ack.secondary_address_size =
1077 strlen(u.bind_ack.secondary_address) + 1;
1079 u.bind_ack.num_results = 1;
1080 u.bind_ack.ctx_list = &bind_ack_ctx;
1082 /* NOTE: We leave the auth_info empty so we can calculate the padding
1083 * later and then append the auth_info --simo */
1086 * Marshall directly into the outgoing PDU space. We
1087 * must do this as we need to set to the bind response
1088 * header and are never sending more than one PDU here.
1091 status = dcerpc_push_ncacn_packet(p->mem_ctx,
1092 DCERPC_PKT_BIND_ACK,
1093 DCERPC_PFC_FLAG_FIRST |
1094 DCERPC_PFC_FLAG_LAST,
1099 if (!NT_STATUS_IS_OK(status)) {
1100 DEBUG(0, ("Failed to marshall bind_ack packet. (%s)\n",
1101 nt_errstr(status)));
1104 if (auth_resp.length) {
1106 status = dcerpc_push_dcerpc_auth(pkt,
1108 auth_info.auth_level,
1110 1, /* auth_context_id */
1113 if (!NT_STATUS_IS_OK(status)) {
1114 DEBUG(0, ("Marshalling of dcerpc_auth failed.\n"));
1119 /* Now that we have the auth len store it into the right place in
1120 * the dcerpc header */
1121 dcerpc_set_frag_length(&p->out_data.frag,
1122 p->out_data.frag.length + auth_blob.length);
1124 if (auth_blob.length) {
1126 if (!data_blob_append(p->mem_ctx, &p->out_data.frag,
1127 auth_blob.data, auth_blob.length)) {
1128 DEBUG(0, ("Append of auth info failed.\n"));
1134 * Setup the lengths for the initial reply.
1137 p->out_data.data_sent_length = 0;
1138 p->out_data.current_pdu_sent = 0;
1140 TALLOC_FREE(auth_blob.data);
1145 data_blob_free(&p->out_data.frag);
1146 TALLOC_FREE(auth_blob.data);
1147 return setup_bind_nak(p, pkt);
1150 /*******************************************************************
1151 This is the "stage3" response after a bind request and reply.
1152 *******************************************************************/
1154 bool api_pipe_bind_auth3(struct pipes_struct *p, struct ncacn_packet *pkt)
1156 struct dcerpc_auth auth_info;
1157 DATA_BLOB response = data_blob_null;
1158 struct auth_ntlmssp_state *ntlmssp_ctx;
1159 struct spnego_context *spnego_ctx;
1160 struct gse_context *gse_ctx;
1163 DEBUG(5, ("api_pipe_bind_auth3: decode request. %d\n", __LINE__));
1165 if (pkt->auth_length == 0) {
1166 DEBUG(0, ("No auth field sent for bind request!\n"));
1170 /* Ensure there's enough data for an authenticated request. */
1171 if (pkt->frag_length < RPC_HEADER_LEN
1172 + DCERPC_AUTH_TRAILER_LENGTH
1173 + pkt->auth_length) {
1174 DEBUG(0,("api_pipe_ntlmssp_auth_process: auth_len "
1175 "%u is too large.\n",
1176 (unsigned int)pkt->auth_length));
1181 * Decode the authentication verifier response.
1184 status = dcerpc_pull_dcerpc_auth(pkt,
1185 &pkt->u.auth3.auth_info,
1186 &auth_info, p->endian);
1187 if (!NT_STATUS_IS_OK(status)) {
1188 DEBUG(0, ("Failed to unmarshall dcerpc_auth.\n"));
1192 /* We must NEVER look at auth_info->auth_pad_len here,
1193 * as old Samba client code gets it wrong and sends it
1197 if (auth_info.auth_type != p->auth.auth_type) {
1198 DEBUG(0, ("Auth type mismatch! Client sent %d, "
1199 "but auth was started as type %d!\n",
1200 auth_info.auth_type, p->auth.auth_type));
1204 switch (auth_info.auth_type) {
1205 case DCERPC_AUTH_TYPE_NTLMSSP:
1206 ntlmssp_ctx = talloc_get_type_abort(p->auth.auth_ctx,
1207 struct auth_ntlmssp_state);
1208 status = ntlmssp_server_step(ntlmssp_ctx,
1209 pkt, &auth_info.credentials,
1212 case DCERPC_AUTH_TYPE_KRB5:
1213 gse_ctx = talloc_get_type_abort(p->auth.auth_ctx,
1214 struct gse_context);
1215 status = gssapi_server_step(gse_ctx,
1216 pkt, &auth_info.credentials,
1219 case DCERPC_AUTH_TYPE_SPNEGO:
1220 spnego_ctx = talloc_get_type_abort(p->auth.auth_ctx,
1221 struct spnego_context);
1222 status = spnego_server_step(spnego_ctx,
1223 pkt, &auth_info.credentials,
1227 DEBUG(0, (__location__ ": incorrect auth type (%u).\n",
1228 (unsigned int)auth_info.auth_type));
1232 if (NT_STATUS_EQUAL(status,
1233 NT_STATUS_MORE_PROCESSING_REQUIRED) ||
1235 DEBUG(0, (__location__ ": This was supposed to be the final "
1236 "leg, but crypto machinery claims a response is "
1237 "needed, aborting auth!\n"));
1238 data_blob_free(&response);
1241 if (!NT_STATUS_IS_OK(status)) {
1242 DEBUG(0, ("Auth failed (%s)\n", nt_errstr(status)));
1246 /* Now verify auth was indeed successful and extract server info */
1247 status = pipe_auth_verify_final(p);
1248 if (!NT_STATUS_IS_OK(status)) {
1249 DEBUG(0, ("Auth Verify failed (%s)\n", nt_errstr(status)));
1257 TALLOC_FREE(p->auth.auth_ctx);
1261 /****************************************************************************
1262 Deal with an alter context call. Can be third part of 3 leg auth request for
1264 ****************************************************************************/
1266 static bool api_pipe_alter_context(struct pipes_struct *p,
1267 struct ncacn_packet *pkt)
1269 struct dcerpc_auth auth_info;
1272 union dcerpc_payload u;
1273 struct dcerpc_ack_ctx bind_ack_ctx;
1274 DATA_BLOB auth_resp = data_blob_null;
1275 DATA_BLOB auth_blob = data_blob_null;
1277 struct auth_ntlmssp_state *ntlmssp_ctx;
1278 struct spnego_context *spnego_ctx;
1279 struct gse_context *gse_ctx;
1281 DEBUG(5,("api_pipe_alter_context: make response. %d\n", __LINE__));
1283 if (pkt->u.bind.assoc_group_id != 0) {
1284 assoc_gid = pkt->u.bind.assoc_group_id;
1290 * Create the bind response struct.
1293 /* If the requested abstract synt uuid doesn't match our client pipe,
1294 reject the bind_ack & set the transfer interface synt to all 0's,
1295 ver 0 (observed when NT5 attempts to bind to abstract interfaces
1297 Needed when adding entries to a DACL from NT5 - SK */
1299 if (check_bind_req(p,
1300 &pkt->u.bind.ctx_list[0].abstract_syntax,
1301 &pkt->u.bind.ctx_list[0].transfer_syntaxes[0],
1302 pkt->u.bind.ctx_list[0].context_id)) {
1304 bind_ack_ctx.result = 0;
1305 bind_ack_ctx.reason = 0;
1306 bind_ack_ctx.syntax = pkt->u.bind.ctx_list[0].transfer_syntaxes[0];
1308 p->pipe_bound = False;
1309 /* Rejection reason: abstract syntax not supported */
1310 bind_ack_ctx.result = DCERPC_BIND_PROVIDER_REJECT;
1311 bind_ack_ctx.reason = DCERPC_BIND_REASON_ASYNTAX;
1312 bind_ack_ctx.syntax = null_ndr_syntax_id;
1316 * Check if this is an authenticated alter context request.
1318 if (pkt->auth_length) {
1319 /* Quick length check. Won't catch a bad auth footer,
1320 * prevents overrun. */
1322 if (pkt->frag_length < RPC_HEADER_LEN +
1323 DCERPC_AUTH_TRAILER_LENGTH +
1325 DEBUG(0,("api_pipe_alter_context: auth_len (%u) "
1326 "too long for fragment %u.\n",
1327 (unsigned int)pkt->auth_length,
1328 (unsigned int)pkt->frag_length ));
1332 status = dcerpc_pull_dcerpc_auth(pkt,
1333 &pkt->u.bind.auth_info,
1334 &auth_info, p->endian);
1335 if (!NT_STATUS_IS_OK(status)) {
1336 DEBUG(0, ("Unable to unmarshall dcerpc_auth.\n"));
1340 /* We can only finish if the pipe is unbound for now */
1341 if (p->pipe_bound) {
1342 DEBUG(0, (__location__ ": Pipe already bound, "
1343 "Altering Context not yet supported!\n"));
1347 if (auth_info.auth_type != p->auth.auth_type) {
1348 DEBUG(0, ("Auth type mismatch! Client sent %d, "
1349 "but auth was started as type %d!\n",
1350 auth_info.auth_type, p->auth.auth_type));
1355 switch (auth_info.auth_type) {
1356 case DCERPC_AUTH_TYPE_SPNEGO:
1357 spnego_ctx = talloc_get_type_abort(p->auth.auth_ctx,
1358 struct spnego_context);
1359 status = spnego_server_step(spnego_ctx,
1361 &auth_info.credentials,
1365 case DCERPC_AUTH_TYPE_KRB5:
1366 gse_ctx = talloc_get_type_abort(p->auth.auth_ctx,
1367 struct gse_context);
1368 status = gssapi_server_step(gse_ctx,
1370 &auth_info.credentials,
1373 case DCERPC_AUTH_TYPE_NTLMSSP:
1374 ntlmssp_ctx = talloc_get_type_abort(p->auth.auth_ctx,
1375 struct auth_ntlmssp_state);
1376 status = ntlmssp_server_step(ntlmssp_ctx,
1378 &auth_info.credentials,
1383 DEBUG(3, (__location__ ": Usupported auth type (%d) "
1384 "in alter-context call\n",
1385 auth_info.auth_type));
1389 if (NT_STATUS_IS_OK(status)) {
1390 /* third leg of auth, verify auth info */
1391 status = pipe_auth_verify_final(p);
1392 if (!NT_STATUS_IS_OK(status)) {
1393 DEBUG(0, ("Auth Verify failed (%s)\n",
1394 nt_errstr(status)));
1397 } else if (NT_STATUS_EQUAL(status,
1398 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1399 DEBUG(10, ("More auth legs required.\n"));
1401 DEBUG(0, ("Auth step returned an error (%s)\n",
1402 nt_errstr(status)));
1407 ZERO_STRUCT(u.alter_resp);
1408 u.alter_resp.max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
1409 u.alter_resp.max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
1410 u.alter_resp.assoc_group_id = assoc_gid;
1412 /* secondary address CAN be NULL
1413 * as the specs say it's ignored.
1414 * It MUST be NULL to have the spoolss working.
1416 u.alter_resp.secondary_address = "";
1417 u.alter_resp.secondary_address_size = 1;
1419 u.alter_resp.num_results = 1;
1420 u.alter_resp.ctx_list = &bind_ack_ctx;
1422 /* NOTE: We leave the auth_info empty so we can calculate the padding
1423 * later and then append the auth_info --simo */
1426 * Marshall directly into the outgoing PDU space. We
1427 * must do this as we need to set to the bind response
1428 * header and are never sending more than one PDU here.
1431 status = dcerpc_push_ncacn_packet(p->mem_ctx,
1432 DCERPC_PKT_ALTER_RESP,
1433 DCERPC_PFC_FLAG_FIRST |
1434 DCERPC_PFC_FLAG_LAST,
1439 if (!NT_STATUS_IS_OK(status)) {
1440 DEBUG(0, ("Failed to marshall bind_ack packet. (%s)\n",
1441 nt_errstr(status)));
1444 if (auth_resp.length) {
1446 /* Work out any padding needed before the auth footer. */
1447 pad_len = p->out_data.frag.length % SERVER_NDR_PADDING_SIZE;
1449 pad_len = SERVER_NDR_PADDING_SIZE - pad_len;
1450 DEBUG(10, ("auth pad_len = %u\n",
1451 (unsigned int)pad_len));
1454 status = dcerpc_push_dcerpc_auth(pkt,
1455 auth_info.auth_type,
1456 auth_info.auth_level,
1458 1, /* auth_context_id */
1461 if (!NT_STATUS_IS_OK(status)) {
1462 DEBUG(0, ("Marshalling of dcerpc_auth failed.\n"));
1467 /* Now that we have the auth len store it into the right place in
1468 * the dcerpc header */
1469 dcerpc_set_frag_length(&p->out_data.frag,
1470 p->out_data.frag.length +
1471 pad_len + auth_blob.length);
1473 if (auth_resp.length) {
1475 char pad[SERVER_NDR_PADDING_SIZE];
1476 memset(pad, '\0', SERVER_NDR_PADDING_SIZE);
1477 if (!data_blob_append(p->mem_ctx,
1480 DEBUG(0, ("api_pipe_bind_req: failed to add "
1481 "%u bytes of pad data.\n",
1482 (unsigned int)pad_len));
1487 if (!data_blob_append(p->mem_ctx, &p->out_data.frag,
1488 auth_blob.data, auth_blob.length)) {
1489 DEBUG(0, ("Append of auth info failed.\n"));
1495 * Setup the lengths for the initial reply.
1498 p->out_data.data_sent_length = 0;
1499 p->out_data.current_pdu_sent = 0;
1501 TALLOC_FREE(auth_blob.data);
1506 data_blob_free(&p->out_data.frag);
1507 TALLOC_FREE(auth_blob.data);
1508 return setup_bind_nak(p, pkt);
1511 /****************************************************************************
1512 Find the set of RPC functions associated with this context_id
1513 ****************************************************************************/
1515 static PIPE_RPC_FNS* find_pipe_fns_by_context( PIPE_RPC_FNS *list, uint32 context_id )
1517 PIPE_RPC_FNS *fns = NULL;
1520 DEBUG(0,("find_pipe_fns_by_context: ERROR! No context list for pipe!\n"));
1524 for (fns=list; fns; fns=fns->next ) {
1525 if ( fns->context_id == context_id )
1531 static bool api_rpcTNP(struct pipes_struct *p, struct ncacn_packet *pkt,
1532 const struct api_struct *api_rpc_cmds, int n_cmds);
1534 /****************************************************************************
1535 Find the correct RPC function to call for this request.
1536 If the pipe is authenticated then become the correct UNIX user
1537 before doing the call.
1538 ****************************************************************************/
1540 static bool api_pipe_request(struct pipes_struct *p,
1541 struct ncacn_packet *pkt)
1544 bool changed_user = False;
1545 PIPE_RPC_FNS *pipe_fns;
1547 if (p->pipe_bound &&
1548 ((p->auth.auth_type == DCERPC_AUTH_TYPE_NTLMSSP) ||
1549 (p->auth.auth_type == DCERPC_AUTH_TYPE_KRB5) ||
1550 (p->auth.auth_type == DCERPC_AUTH_TYPE_SPNEGO))) {
1551 if(!become_authenticated_pipe_user(p->session_info)) {
1552 data_blob_free(&p->out_data.rdata);
1555 changed_user = True;
1558 DEBUG(5, ("Requested \\PIPE\\%s\n",
1559 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1561 /* get the set of RPC functions for this context */
1563 pipe_fns = find_pipe_fns_by_context(p->contexts,
1564 pkt->u.request.context_id);
1567 TALLOC_CTX *frame = talloc_stackframe();
1568 ret = api_rpcTNP(p, pkt, pipe_fns->cmds, pipe_fns->n_cmds);
1572 DEBUG(0, ("No rpc function table associated with context "
1573 "[%d] on pipe [%s]\n",
1574 pkt->u.request.context_id,
1575 get_pipe_name_from_syntax(talloc_tos(),
1580 unbecome_authenticated_pipe_user();
1586 /*******************************************************************
1587 Calls the underlying RPC function for a named pipe.
1588 ********************************************************************/
1590 static bool api_rpcTNP(struct pipes_struct *p, struct ncacn_packet *pkt,
1591 const struct api_struct *api_rpc_cmds, int n_cmds)
1596 /* interpret the command */
1597 DEBUG(4,("api_rpcTNP: %s op 0x%x - ",
1598 get_pipe_name_from_syntax(talloc_tos(), &p->syntax),
1599 pkt->u.request.opnum));
1601 if (DEBUGLEVEL >= 50) {
1603 slprintf(name, sizeof(name)-1, "in_%s",
1604 get_pipe_name_from_syntax(talloc_tos(), &p->syntax));
1605 dump_pdu_region(name, pkt->u.request.opnum,
1606 &p->in_data.data, 0,
1607 p->in_data.data.length);
1610 for (fn_num = 0; fn_num < n_cmds; fn_num++) {
1611 if (api_rpc_cmds[fn_num].opnum == pkt->u.request.opnum &&
1612 api_rpc_cmds[fn_num].fn != NULL) {
1613 DEBUG(3, ("api_rpcTNP: rpc command: %s\n",
1614 api_rpc_cmds[fn_num].name));
1619 if (fn_num == n_cmds) {
1621 * For an unknown RPC just return a fault PDU but
1622 * return True to allow RPC's on the pipe to continue
1623 * and not put the pipe into fault state. JRA.
1625 DEBUG(4, ("unknown\n"));
1626 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
1630 offset1 = p->out_data.rdata.length;
1632 DEBUG(6, ("api_rpc_cmds[%d].fn == %p\n",
1633 fn_num, api_rpc_cmds[fn_num].fn));
1634 /* do the actual command */
1635 if(!api_rpc_cmds[fn_num].fn(p)) {
1636 DEBUG(0,("api_rpcTNP: %s: %s failed.\n",
1637 get_pipe_name_from_syntax(talloc_tos(), &p->syntax),
1638 api_rpc_cmds[fn_num].name));
1639 data_blob_free(&p->out_data.rdata);
1643 if (p->bad_handle_fault_state) {
1644 DEBUG(4,("api_rpcTNP: bad handle fault return.\n"));
1645 p->bad_handle_fault_state = False;
1646 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_CONTEXT_MISMATCH));
1650 if (p->rng_fault_state) {
1651 DEBUG(4, ("api_rpcTNP: rng fault return\n"));
1652 p->rng_fault_state = False;
1653 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
1657 if (DEBUGLEVEL >= 50) {
1659 slprintf(name, sizeof(name)-1, "out_%s",
1660 get_pipe_name_from_syntax(talloc_tos(), &p->syntax));
1661 dump_pdu_region(name, pkt->u.request.opnum,
1662 &p->out_data.rdata, offset1,
1663 p->out_data.rdata.length);
1666 DEBUG(5,("api_rpcTNP: called %s successfully\n",
1667 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1669 /* Check for buffer underflow in rpc parsing */
1670 if ((DEBUGLEVEL >= 10) &&
1671 (pkt->frag_length < p->in_data.data.length)) {
1672 DEBUG(10, ("api_rpcTNP: rpc input buffer underflow (parse error?)\n"));
1673 dump_data(10, p->in_data.data.data + pkt->frag_length,
1674 p->in_data.data.length - pkt->frag_length);
1680 /****************************************************************************
1681 Initialise an outgoing packet.
1682 ****************************************************************************/
1684 static bool pipe_init_outgoing_data(struct pipes_struct *p)
1686 output_data *o_data = &p->out_data;
1688 /* Reset the offset counters. */
1689 o_data->data_sent_length = 0;
1690 o_data->current_pdu_sent = 0;
1692 data_blob_free(&o_data->frag);
1694 /* Free any memory in the current return data buffer. */
1695 data_blob_free(&o_data->rdata);
1700 /****************************************************************************
1701 Sets the fault state on incoming packets.
1702 ****************************************************************************/
1704 void set_incoming_fault(struct pipes_struct *p)
1706 data_blob_free(&p->in_data.data);
1707 p->in_data.pdu_needed_len = 0;
1708 p->in_data.pdu.length = 0;
1709 p->fault_state = True;
1710 DEBUG(10, ("set_incoming_fault: Setting fault state on pipe %s\n",
1711 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1714 static NTSTATUS dcesrv_auth_request(struct pipe_auth_data *auth,
1715 struct ncacn_packet *pkt,
1719 size_t hdr_size = DCERPC_REQUEST_LENGTH;
1722 DEBUG(10, ("Checking request auth.\n"));
1724 if (pkt->pfc_flags & DCERPC_PFC_FLAG_OBJECT_UUID) {
1728 /* in case of sealing this function will unseal the data in place */
1729 status = dcerpc_check_auth(auth, pkt,
1730 &pkt->u.request.stub_and_verifier,
1733 if (!NT_STATUS_IS_OK(status)) {
1738 /* remove padding and auth trailer,
1739 * this way the caller will get just the data */
1740 if (pkt->auth_length) {
1741 size_t trail_len = pad_len
1742 + DCERPC_AUTH_TRAILER_LENGTH
1744 if (pkt->u.request.stub_and_verifier.length < trail_len) {
1745 return NT_STATUS_INFO_LENGTH_MISMATCH;
1747 pkt->u.request.stub_and_verifier.length -= trail_len;
1750 return NT_STATUS_OK;
1753 /****************************************************************************
1754 Processes a request pdu. This will do auth processing if needed, and
1755 appends the data into the complete stream if the LAST flag is not set.
1756 ****************************************************************************/
1758 static bool process_request_pdu(struct pipes_struct *p, struct ncacn_packet *pkt)
1763 if (!p->pipe_bound) {
1764 DEBUG(0,("process_request_pdu: rpc request with no bind.\n"));
1765 set_incoming_fault(p);
1769 /* Store the opnum */
1770 p->opnum = pkt->u.request.opnum;
1772 status = dcesrv_auth_request(&p->auth, pkt, &p->in_data.pdu);
1773 if (!NT_STATUS_IS_OK(status)) {
1774 DEBUG(0, ("Failed to check packet auth. (%s)\n",
1775 nt_errstr(status)));
1776 set_incoming_fault(p);
1780 data = pkt->u.request.stub_and_verifier;
1783 * Check the data length doesn't go over the 15Mb limit.
1784 * increased after observing a bug in the Windows NT 4.0 SP6a
1785 * spoolsv.exe when the response to a GETPRINTERDRIVER2 RPC
1786 * will not fit in the initial buffer of size 0x1068 --jerry 22/01/2002
1789 if (p->in_data.data.length + data.length > MAX_RPC_DATA_SIZE) {
1790 DEBUG(0, ("process_request_pdu: "
1791 "rpc data buffer too large (%u) + (%u)\n",
1792 (unsigned int)p->in_data.data.length,
1793 (unsigned int)data.length));
1794 set_incoming_fault(p);
1799 * Append the data portion into the buffer and return.
1803 if (!data_blob_append(p->mem_ctx, &p->in_data.data,
1804 data.data, data.length)) {
1805 DEBUG(0, ("Unable to append data size %u "
1806 "to parse buffer of size %u.\n",
1807 (unsigned int)data.length,
1808 (unsigned int)p->in_data.data.length));
1809 set_incoming_fault(p);
1814 if (pkt->pfc_flags & DCERPC_PFC_FLAG_LAST) {
1817 * Ok - we finally have a complete RPC stream.
1818 * Call the rpc command to process it.
1822 * Process the complete data stream here.
1824 if (pipe_init_outgoing_data(p)) {
1825 ret = api_pipe_request(p, pkt);
1834 /****************************************************************************
1835 Processes a finished PDU stored in p->in_data.pdu.
1836 ****************************************************************************/
1838 void process_complete_pdu(struct pipes_struct *p)
1840 struct ncacn_packet *pkt = NULL;
1844 if(p->fault_state) {
1845 DEBUG(10,("process_complete_pdu: pipe %s in fault state.\n",
1846 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1850 pkt = talloc(p->mem_ctx, struct ncacn_packet);
1852 DEBUG(0, ("Out of memory!\n"));
1857 * Ensure we're using the corrent endianness for both the
1858 * RPC header flags and the raw data we will be reading from.
1860 if (dcerpc_get_endian_flag(&p->in_data.pdu) & DCERPC_DREP_LE) {
1861 p->endian = RPC_LITTLE_ENDIAN;
1863 p->endian = RPC_BIG_ENDIAN;
1865 DEBUG(10, ("PDU is in %s Endian format!\n", p->endian?"Big":"Little"));
1867 status = dcerpc_pull_ncacn_packet(pkt, &p->in_data.pdu,
1869 if (!NT_STATUS_IS_OK(status)) {
1870 DEBUG(0, ("Failed to unmarshal rpc packet: %s!\n",
1871 nt_errstr(status)));
1875 /* Store the call_id */
1876 p->call_id = pkt->call_id;
1878 DEBUG(10, ("Processing packet type %d\n", (int)pkt->ptype));
1880 switch (pkt->ptype) {
1881 case DCERPC_PKT_REQUEST:
1882 reply = process_request_pdu(p, pkt);
1885 case DCERPC_PKT_PING: /* CL request - ignore... */
1886 DEBUG(0, ("process_complete_pdu: Error. "
1887 "Connectionless packet type %d received on "
1888 "pipe %s.\n", (int)pkt->ptype,
1889 get_pipe_name_from_syntax(talloc_tos(),
1893 case DCERPC_PKT_RESPONSE: /* No responses here. */
1894 DEBUG(0, ("process_complete_pdu: Error. "
1895 "DCERPC_PKT_RESPONSE received from client "
1897 get_pipe_name_from_syntax(talloc_tos(),
1901 case DCERPC_PKT_FAULT:
1902 case DCERPC_PKT_WORKING:
1903 /* CL request - reply to a ping when a call in process. */
1904 case DCERPC_PKT_NOCALL:
1905 /* CL - server reply to a ping call. */
1906 case DCERPC_PKT_REJECT:
1907 case DCERPC_PKT_ACK:
1908 case DCERPC_PKT_CL_CANCEL:
1909 case DCERPC_PKT_FACK:
1910 case DCERPC_PKT_CANCEL_ACK:
1911 DEBUG(0, ("process_complete_pdu: Error. "
1912 "Connectionless packet type %u received on "
1913 "pipe %s.\n", (unsigned int)pkt->ptype,
1914 get_pipe_name_from_syntax(talloc_tos(),
1918 case DCERPC_PKT_BIND:
1920 * We assume that a pipe bind is only in one pdu.
1922 if (pipe_init_outgoing_data(p)) {
1923 reply = api_pipe_bind_req(p, pkt);
1927 case DCERPC_PKT_BIND_ACK:
1928 case DCERPC_PKT_BIND_NAK:
1929 DEBUG(0, ("process_complete_pdu: Error. "
1930 "DCERPC_PKT_BINDACK/DCERPC_PKT_BINDNACK "
1931 "packet type %u received on pipe %s.\n",
1932 (unsigned int)pkt->ptype,
1933 get_pipe_name_from_syntax(talloc_tos(),
1938 case DCERPC_PKT_ALTER:
1940 * We assume that a pipe bind is only in one pdu.
1942 if (pipe_init_outgoing_data(p)) {
1943 reply = api_pipe_alter_context(p, pkt);
1947 case DCERPC_PKT_ALTER_RESP:
1948 DEBUG(0, ("process_complete_pdu: Error. "
1949 "DCERPC_PKT_ALTER_RESP on pipe %s: "
1950 "Should only be server -> client.\n",
1951 get_pipe_name_from_syntax(talloc_tos(),
1955 case DCERPC_PKT_AUTH3:
1957 * The third packet in an auth exchange.
1959 if (pipe_init_outgoing_data(p)) {
1960 reply = api_pipe_bind_auth3(p, pkt);
1964 case DCERPC_PKT_SHUTDOWN:
1965 DEBUG(0, ("process_complete_pdu: Error. "
1966 "DCERPC_PKT_SHUTDOWN on pipe %s: "
1967 "Should only be server -> client.\n",
1968 get_pipe_name_from_syntax(talloc_tos(),
1972 case DCERPC_PKT_CO_CANCEL:
1973 /* For now just free all client data and continue
1975 DEBUG(3,("process_complete_pdu: DCERPC_PKT_CO_CANCEL."
1976 " Abandoning rpc call.\n"));
1977 /* As we never do asynchronous RPC serving, we can
1978 * never cancel a call (as far as I know).
1979 * If we ever did we'd have to send a cancel_ack reply.
1980 * For now, just free all client data and continue
1986 /* Enable this if we're doing async rpc. */
1987 /* We must check the outstanding callid matches. */
1988 if (pipe_init_outgoing_data(p)) {
1989 /* Send a cancel_ack PDU reply. */
1990 /* We should probably check the auth-verifier here. */
1991 reply = setup_cancel_ack_reply(p, pkt);
1996 case DCERPC_PKT_ORPHANED:
1997 /* We should probably check the auth-verifier here.
1998 * For now just free all client data and continue
2000 DEBUG(3, ("process_complete_pdu: DCERPC_PKT_ORPHANED."
2001 " Abandoning rpc call.\n"));
2006 DEBUG(0, ("process_complete_pdu: "
2007 "Unknown rpc type = %u received.\n",
2008 (unsigned int)pkt->ptype));
2014 DEBUG(3,("process_complete_pdu: DCE/RPC fault sent on "
2015 "pipe %s\n", get_pipe_name_from_syntax(talloc_tos(),
2017 set_incoming_fault(p);
2018 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
2022 * Reset the lengths. We're ready for a new pdu.
2024 TALLOC_FREE(p->in_data.pdu.data);
2025 p->in_data.pdu_needed_len = 0;
2026 p->in_data.pdu.length = 0;