2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Almost completely rewritten by (C) Jeremy Allison 2005 - 2010
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 /* this module apparently provides an implementation of DCE/RPC over a
21 * named pipe (IPC$ connection using SMBtrans). details of DCE/RPC
22 * documentation are available (in on-line form) from the X-Open group.
24 * this module should provide a level of abstraction between SMB
25 * and DCE/RPC, while minimising the amount of mallocs, unnecessary
26 * data copies, and network traffic.
31 #include "srv_pipe_internal.h"
32 #include "../librpc/gen_ndr/ndr_schannel.h"
33 #include "../libcli/auth/schannel.h"
34 #include "../libcli/auth/spnego.h"
35 #include "../libcli/auth/ntlmssp.h"
36 #include "ntlmssp_wrap.h"
37 #include "rpc_server.h"
40 #define DBGC_CLASS DBGC_RPC_SRV
43 * Dump everything from the start of the end up of the provided data
44 * into a file, but only at debug level >= 50
46 static void dump_pdu_region(const char *name, int v,
47 DATA_BLOB *data, size_t start, size_t end)
53 if (DEBUGLEVEL < 50) return;
55 if (start > data->length || end > data->length || start > end) return;
57 for (i = 1; i < 100; i++) {
59 fname = talloc_asprintf(talloc_tos(),
63 fname = talloc_asprintf(talloc_tos(),
70 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0644);
71 if (fd != -1 || errno != EEXIST) break;
74 sz = write(fd, data->data + start, end - start);
76 if ((sz != end - start) || (i != 0) ) {
77 DEBUG(0, ("Error writing/closing %s: %ld!=%ld %d\n",
78 fname, (unsigned long)sz,
79 (unsigned long)end - start, i));
81 DEBUG(0,("created %s\n", fname));
87 static void free_pipe_ntlmssp_auth_data(struct pipe_auth_data *auth)
89 TALLOC_FREE(auth->a_u.auth_ntlmssp_state);
92 static DATA_BLOB generic_session_key(void)
94 return data_blob("SystemLibraryDTC", 16);
97 /*******************************************************************
99 ********************************************************************/
101 static bool add_ntlmssp_auth(pipes_struct *p)
103 enum dcerpc_AuthLevel auth_level = p->auth.auth_level;
104 DATA_BLOB auth_blob = data_blob_null;
107 /* FIXME: Is this right ?
108 * Keeping only to avoid changing semantics during refactoring
111 if (auth_level != DCERPC_AUTH_LEVEL_PRIVACY) {
112 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
115 /* Generate the auth blob. */
116 switch (auth_level) {
117 case DCERPC_AUTH_LEVEL_PRIVACY:
118 /* Data portion is encrypted. */
119 status = auth_ntlmssp_seal_packet(
120 p->auth.a_u.auth_ntlmssp_state,
121 (TALLOC_CTX *)p->out_data.frag.data,
122 &p->out_data.frag.data[DCERPC_RESPONSE_LENGTH],
123 p->out_data.frag.length
124 - DCERPC_RESPONSE_LENGTH
125 - DCERPC_AUTH_TRAILER_LENGTH,
126 p->out_data.frag.data,
127 p->out_data.frag.length,
131 case DCERPC_AUTH_LEVEL_INTEGRITY:
132 /* Data is signed. */
133 status = auth_ntlmssp_sign_packet(
134 p->auth.a_u.auth_ntlmssp_state,
135 (TALLOC_CTX *)p->out_data.frag.data,
136 &p->out_data.frag.data[DCERPC_RESPONSE_LENGTH],
137 p->out_data.frag.length
138 - DCERPC_RESPONSE_LENGTH
139 - DCERPC_AUTH_TRAILER_LENGTH,
140 p->out_data.frag.data,
141 p->out_data.frag.length,
146 status = NT_STATUS_INTERNAL_ERROR;
150 if (!NT_STATUS_IS_OK(status)) {
151 DEBUG(0, ("Failed to add NTLMSSP auth blob: %s\n",
153 data_blob_free(&p->out_data.frag);
157 /* Finally append the auth blob. */
158 if (!data_blob_append(p->mem_ctx, &p->out_data.frag,
159 auth_blob.data, auth_blob.length)) {
160 DEBUG(0, ("Failed to add %u bytes auth blob.\n",
161 (unsigned int)auth_blob.length));
162 data_blob_free(&p->out_data.frag);
165 data_blob_free(&auth_blob);
170 /*******************************************************************
171 Append a schannel authenticated fragment.
172 ********************************************************************/
174 static bool add_schannel_auth(pipes_struct *p)
176 DATA_BLOB auth_blob = data_blob_null;
179 /* Schannel processing. */
180 switch (p->auth.auth_level) {
181 case DCERPC_AUTH_LEVEL_PRIVACY:
182 status = netsec_outgoing_packet(
183 p->auth.a_u.schannel_auth,
184 (TALLOC_CTX *)p->out_data.frag.data,
186 &p->out_data.frag.data[DCERPC_RESPONSE_LENGTH],
187 p->out_data.frag.length
188 - DCERPC_RESPONSE_LENGTH
189 - DCERPC_AUTH_TRAILER_LENGTH,
193 case DCERPC_AUTH_LEVEL_INTEGRITY:
194 status = netsec_outgoing_packet(
195 p->auth.a_u.schannel_auth,
196 (TALLOC_CTX *)p->out_data.frag.data,
198 &p->out_data.frag.data[DCERPC_RESPONSE_LENGTH],
199 p->out_data.frag.length
200 - DCERPC_RESPONSE_LENGTH
201 - DCERPC_AUTH_TRAILER_LENGTH,
206 status = NT_STATUS_INTERNAL_ERROR;
210 if (!NT_STATUS_IS_OK(status)) {
211 DEBUG(0, ("Failed to add SCHANNEL auth blob: %s\n",
213 data_blob_free(&p->out_data.frag);
217 if (DEBUGLEVEL >= 10) {
218 dump_NL_AUTH_SIGNATURE(talloc_tos(), &auth_blob);
221 if (!data_blob_append(p->mem_ctx, &p->out_data.frag,
222 auth_blob.data, auth_blob.length)) {
223 DEBUG(0, ("Failed to add %u bytes auth blob.\n",
224 (unsigned int)auth_blob.length));
225 data_blob_free(&p->out_data.frag);
228 data_blob_free(&auth_blob);
233 /*******************************************************************
234 Generate the next PDU to be returned from the data.
235 ********************************************************************/
237 static bool create_next_packet(pipes_struct *p,
238 enum dcerpc_AuthType auth_type,
239 enum dcerpc_AuthLevel auth_level,
242 union dcerpc_payload u;
244 size_t data_len_left;
250 ZERO_STRUCT(u.response);
252 /* Set up rpc packet pfc flags. */
253 if (p->out_data.data_sent_length == 0) {
254 pfc_flags = DCERPC_PFC_FLAG_FIRST;
259 /* Work out how much we can fit in a single PDU. */
260 data_len_left = p->out_data.rdata.length -
261 p->out_data.data_sent_length;
263 /* Ensure there really is data left to send. */
264 if (!data_len_left) {
265 DEBUG(0, ("No data left to send !\n"));
269 /* Max space available - not including padding. */
271 max_len = RPC_MAX_PDU_FRAG_LEN
272 - DCERPC_RESPONSE_LENGTH
273 - DCERPC_AUTH_TRAILER_LENGTH
276 max_len = RPC_MAX_PDU_FRAG_LEN - DCERPC_RESPONSE_LENGTH;
280 * The amount we send is the minimum of the max_len
281 * and the amount left to send.
283 data_len = MIN(data_len_left, max_len);
286 /* Work out any padding alignment requirements. */
287 pad_len = (DCERPC_RESPONSE_LENGTH + data_len) %
288 SERVER_NDR_PADDING_SIZE;
290 pad_len = SERVER_NDR_PADDING_SIZE - pad_len;
291 DEBUG(10, ("Padding size is: %d\n", (int)pad_len));
292 /* If we're over filling the packet, we need to make
293 * space for the padding at the end of the data. */
294 if (data_len + pad_len > max_len) {
295 data_len -= SERVER_NDR_PADDING_SIZE;
300 /* Set up the alloc hint. This should be the data left to send. */
301 u.response.alloc_hint = data_len_left;
303 /* Work out if this PDU will be the last. */
304 if (p->out_data.data_sent_length
305 + data_len >= p->out_data.rdata.length) {
306 pfc_flags |= DCERPC_PFC_FLAG_LAST;
309 /* Prepare data to be NDR encoded. */
310 u.response.stub_and_verifier =
311 data_blob_const(p->out_data.rdata.data +
312 p->out_data.data_sent_length, data_len);
314 /* Store the packet in the data stream. */
315 status = dcerpc_push_ncacn_packet(p->mem_ctx,
322 if (!NT_STATUS_IS_OK(status)) {
323 DEBUG(0, ("Failed to marshall RPC Packet.\n"));
328 DATA_BLOB empty = data_blob_null;
331 /* Set the proper length on the pdu, including padding.
332 * Only needed if an auth trailer will be appended. */
333 dcerpc_set_frag_length(&p->out_data.frag,
334 p->out_data.frag.length
336 + DCERPC_AUTH_TRAILER_LENGTH
340 size_t offset = p->out_data.frag.length;
342 if (!data_blob_realloc(p->mem_ctx,
345 DEBUG(0, ("Failed to add padding!\n"));
346 data_blob_free(&p->out_data.frag);
349 memset(&p->out_data.frag.data[offset], '\0', pad_len);
352 /* auth blob is intentionally empty,
353 * it will be appended later */
354 status = dcerpc_push_dcerpc_auth(p->out_data.frag.data,
361 if (!NT_STATUS_IS_OK(status)) {
362 DEBUG(0, ("Failed to marshall RPC Auth.\n"));
366 /* Store auth header in the data stream. */
367 if (!data_blob_append(p->mem_ctx, &p->out_data.frag,
368 auth_hdr.data, auth_hdr.length)) {
369 DEBUG(0, ("Out of memory.\n"));
370 data_blob_free(&p->out_data.frag);
373 data_blob_free(&auth_hdr);
376 /* Setup the counts for this PDU. */
377 p->out_data.data_sent_length += data_len;
378 p->out_data.current_pdu_sent = 0;
382 /*******************************************************************
383 Generate the next PDU to be returned from the data in p->rdata.
384 ********************************************************************/
386 bool create_next_pdu(pipes_struct *p)
388 enum dcerpc_AuthType auth_type =
389 map_pipe_auth_type_to_rpc_auth_type(p->auth.auth_type);
392 * If we're in the fault state, keep returning fault PDU's until
393 * the pipe gets closed. JRA.
395 if (p->fault_state) {
396 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
400 switch (p->auth.auth_level) {
401 case DCERPC_AUTH_LEVEL_NONE:
402 case DCERPC_AUTH_LEVEL_CONNECT:
403 /* This is incorrect for auth level connect. Fixme. JRA */
405 /* No authentication done. */
406 return create_next_packet(p, auth_type,
407 p->auth.auth_level, 0);
409 case DCERPC_AUTH_LEVEL_CALL:
410 case DCERPC_AUTH_LEVEL_PACKET:
411 case DCERPC_AUTH_LEVEL_INTEGRITY:
412 case DCERPC_AUTH_LEVEL_PRIVACY:
414 switch(p->auth.auth_type) {
415 case PIPE_AUTH_TYPE_NTLMSSP:
416 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
417 if (!create_next_packet(p, auth_type,
422 return add_ntlmssp_auth(p);
424 case PIPE_AUTH_TYPE_SCHANNEL:
425 if (!create_next_packet(p, auth_type,
427 NL_AUTH_SIGNATURE_SIZE)) {
430 return add_schannel_auth(p);
438 DEBUG(0, ("Invalid internal auth level %u / type %u\n",
439 (unsigned int)p->auth.auth_level,
440 (unsigned int)p->auth.auth_type));
444 /*******************************************************************
445 Process an NTLMSSP authentication response.
446 If this function succeeds, the user has been authenticated
447 and their domain, name and calling workstation stored in
449 *******************************************************************/
451 static bool pipe_ntlmssp_verify_final(pipes_struct *p, DATA_BLOB *p_resp_blob)
453 DATA_BLOB session_key, reply;
455 struct auth_ntlmssp_state *a = p->auth.a_u.auth_ntlmssp_state;
458 DEBUG(5,("pipe_ntlmssp_verify_final: pipe %s checking user details\n",
459 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
463 /* this has to be done as root in order to verify the password */
465 status = auth_ntlmssp_update(a, *p_resp_blob, &reply);
468 /* Don't generate a reply. */
469 data_blob_free(&reply);
471 if (!NT_STATUS_IS_OK(status)) {
475 /* Finally - if the pipe negotiated integrity (sign) or privacy (seal)
476 ensure the underlying NTLMSSP flags are also set. If not we should
479 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_INTEGRITY) {
480 if (!auth_ntlmssp_negotiated_sign(a)) {
481 DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet integrity requested "
482 "but client declined signing.\n",
483 get_pipe_name_from_syntax(talloc_tos(),
488 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
489 if (!auth_ntlmssp_negotiated_seal(a)) {
490 DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet privacy requested "
491 "but client declined sealing.\n",
492 get_pipe_name_from_syntax(talloc_tos(),
498 DEBUG(5, ("pipe_ntlmssp_verify_final: OK: user: %s domain: %s "
500 auth_ntlmssp_get_username(a),
501 auth_ntlmssp_get_domain(a),
502 auth_ntlmssp_get_client(a)));
504 TALLOC_FREE(p->server_info);
506 status = auth_ntlmssp_steal_server_info(p, a, &p->server_info);
507 if (!NT_STATUS_IS_OK(status)) {
508 DEBUG(0, ("auth_ntlmssp_server_info failed to obtain the server info for authenticated user: %s\n",
513 if (p->server_info->ptok == NULL) {
514 DEBUG(1,("Error: Authmodule failed to provide nt_user_token\n"));
519 * We're an authenticated bind over smb, so the session key needs to
520 * be set to "SystemLibraryDTC". Weird, but this is what Windows
521 * does. See the RPC-SAMBA3SESSIONKEY.
524 session_key = generic_session_key();
525 if (session_key.data == NULL) {
529 ret = server_info_set_session_key(p->server_info, session_key);
531 data_blob_free(&session_key);
536 /*******************************************************************
537 This is the "stage3" NTLMSSP response after a bind request and reply.
538 *******************************************************************/
540 bool api_pipe_bind_auth3(pipes_struct *p, struct ncacn_packet *pkt)
542 struct dcerpc_auth auth_info;
545 DEBUG(5, ("api_pipe_bind_auth3: decode request. %d\n", __LINE__));
547 if (pkt->auth_length == 0) {
548 DEBUG(0, ("No auth field sent for bind request!\n"));
552 /* Ensure there's enough data for an authenticated request. */
553 if (pkt->frag_length < RPC_HEADER_LEN
554 + DCERPC_AUTH_TRAILER_LENGTH
555 + pkt->auth_length) {
556 DEBUG(0,("api_pipe_ntlmssp_auth_process: auth_len "
557 "%u is too large.\n",
558 (unsigned int)pkt->auth_length));
563 * Decode the authentication verifier response.
566 status = dcerpc_pull_dcerpc_auth(pkt,
567 &pkt->u.auth3.auth_info,
568 &auth_info, p->endian);
569 if (!NT_STATUS_IS_OK(status)) {
570 DEBUG(0, ("Failed to unmarshall dcerpc_auth.\n"));
574 /* We must NEVER look at auth_info->auth_pad_len here,
575 * as old Samba client code gets it wrong and sends it
579 if (auth_info.auth_type != DCERPC_AUTH_TYPE_NTLMSSP) {
580 DEBUG(0,("api_pipe_bind_auth3: incorrect auth type (%u).\n",
581 (unsigned int)auth_info.auth_type ));
586 * The following call actually checks the challenge/response data.
587 * for correctness against the given DOMAIN\user name.
590 if (!pipe_ntlmssp_verify_final(p, &auth_info.credentials)) {
594 p->pipe_bound = True;
600 free_pipe_ntlmssp_auth_data(&p->auth);
601 p->auth.a_u.auth_ntlmssp_state = NULL;
606 /*******************************************************************
607 Marshall a bind_nak pdu.
608 *******************************************************************/
610 static bool setup_bind_nak(pipes_struct *p, struct ncacn_packet *pkt)
613 union dcerpc_payload u;
615 /* Free any memory in the current return data buffer. */
616 data_blob_free(&p->out_data.rdata);
619 * Initialize a bind_nak header.
624 u.bind_nak.reject_reason = 0;
627 * Marshall directly into the outgoing PDU space. We
628 * must do this as we need to set to the bind response
629 * header and are never sending more than one PDU here.
632 status = dcerpc_push_ncacn_packet(p->mem_ctx,
634 DCERPC_PFC_FLAG_FIRST |
635 DCERPC_PFC_FLAG_LAST,
640 if (!NT_STATUS_IS_OK(status)) {
644 p->out_data.data_sent_length = 0;
645 p->out_data.current_pdu_sent = 0;
647 if (p->auth.auth_data_free_func) {
648 (*p->auth.auth_data_free_func)(&p->auth);
650 p->auth.auth_level = DCERPC_AUTH_LEVEL_NONE;
651 p->auth.auth_type = PIPE_AUTH_TYPE_NONE;
652 p->pipe_bound = False;
657 /*******************************************************************
658 Marshall a fault pdu.
659 *******************************************************************/
661 bool setup_fault_pdu(pipes_struct *p, NTSTATUS fault_status)
664 union dcerpc_payload u;
666 /* Free any memory in the current return data buffer. */
667 data_blob_free(&p->out_data.rdata);
670 * Initialize a fault header.
675 u.fault.status = NT_STATUS_V(fault_status);
676 u.fault._pad = data_blob_talloc_zero(p->mem_ctx, 4);
679 * Marshall directly into the outgoing PDU space. We
680 * must do this as we need to set to the bind response
681 * header and are never sending more than one PDU here.
684 status = dcerpc_push_ncacn_packet(p->mem_ctx,
686 DCERPC_PFC_FLAG_FIRST |
687 DCERPC_PFC_FLAG_LAST |
688 DCERPC_PFC_FLAG_DID_NOT_EXECUTE,
693 if (!NT_STATUS_IS_OK(status)) {
697 p->out_data.data_sent_length = 0;
698 p->out_data.current_pdu_sent = 0;
703 /*******************************************************************
704 Ensure a bind request has the correct abstract & transfer interface.
705 Used to reject unknown binds from Win2k.
706 *******************************************************************/
708 static bool check_bind_req(struct pipes_struct *p,
709 struct ndr_syntax_id* abstract,
710 struct ndr_syntax_id* transfer,
713 struct pipe_rpc_fns *context_fns;
715 DEBUG(3,("check_bind_req for %s\n",
716 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
718 /* we have to check all now since win2k introduced a new UUID on the lsaprpc pipe */
719 if (rpc_srv_pipe_exists_by_id(abstract) &&
720 ndr_syntax_id_equal(transfer, &ndr_transfer_syntax)) {
721 DEBUG(3, ("check_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
722 rpc_srv_get_pipe_cli_name(abstract),
723 rpc_srv_get_pipe_srv_name(abstract)));
728 context_fns = SMB_MALLOC_P(struct pipe_rpc_fns);
729 if (context_fns == NULL) {
730 DEBUG(0,("check_bind_req: malloc() failed!\n"));
734 context_fns->n_cmds = rpc_srv_get_pipe_num_cmds(abstract);
735 context_fns->cmds = rpc_srv_get_pipe_cmds(abstract);
736 context_fns->context_id = context_id;
738 /* add to the list of open contexts */
740 DLIST_ADD( p->contexts, context_fns );
746 * Is a named pipe known?
747 * @param[in] cli_filename The pipe name requested by the client
748 * @result Do we want to serve this?
750 bool is_known_pipename(const char *cli_filename, struct ndr_syntax_id *syntax)
752 const char *pipename = cli_filename;
755 if (strnequal(pipename, "\\PIPE\\", 6)) {
759 if (*pipename == '\\') {
763 if (lp_disable_spoolss() && strequal(pipename, "spoolss")) {
764 DEBUG(10, ("refusing spoolss access\n"));
768 if (rpc_srv_get_pipe_interface_by_cli_name(pipename, syntax)) {
772 status = smb_probe_module("rpc", pipename);
773 if (!NT_STATUS_IS_OK(status)) {
774 DEBUG(10, ("is_known_pipename: %s unknown\n", cli_filename));
777 DEBUG(10, ("is_known_pipename: %s loaded dynamically\n", pipename));
780 * Scan the list again for the interface id
782 if (rpc_srv_get_pipe_interface_by_cli_name(pipename, syntax)) {
786 DEBUG(10, ("is_known_pipename: pipe %s did not register itself!\n",
792 /*******************************************************************
793 Handle a SPNEGO krb5 bind auth.
794 *******************************************************************/
796 static bool pipe_spnego_auth_bind_kerberos(pipes_struct *p,
798 struct dcerpc_auth *pauth_info,
805 /*******************************************************************
806 Handle the first part of a SPNEGO bind auth.
807 *******************************************************************/
809 static bool pipe_spnego_auth_bind_negotiate(pipes_struct *p,
811 struct dcerpc_auth *pauth_info,
816 char *OIDs[ASN1_MAX_OIDS];
819 bool got_kerberos_mechanism = false;
820 struct auth_ntlmssp_state *a = NULL;
822 ZERO_STRUCT(secblob);
825 if (pauth_info->credentials.data[0] != ASN1_APPLICATION(0)) {
829 /* parse out the OIDs and the first sec blob */
830 if (!spnego_parse_negTokenInit(pauth_info->credentials, OIDs, NULL, &secblob)) {
831 DEBUG(0,("pipe_spnego_auth_bind_negotiate: Failed to parse the security blob.\n"));
835 if (strcmp(OID_KERBEROS5, OIDs[0]) == 0 || strcmp(OID_KERBEROS5_OLD, OIDs[0]) == 0) {
836 got_kerberos_mechanism = true;
839 for (i=0;OIDs[i];i++) {
840 DEBUG(3,("pipe_spnego_auth_bind_negotiate: Got OID %s\n", OIDs[i]));
841 TALLOC_FREE(OIDs[i]);
843 DEBUG(3,("pipe_spnego_auth_bind_negotiate: Got secblob of size %lu\n", (unsigned long)secblob.length));
845 if ( got_kerberos_mechanism && ((lp_security()==SEC_ADS) || USE_KERBEROS_KEYTAB) ) {
847 ret = pipe_spnego_auth_bind_kerberos(p, mem_ctx, pauth_info,
849 data_blob_free(&secblob);
853 if (p->auth.auth_type == PIPE_AUTH_TYPE_SPNEGO_NTLMSSP && p->auth.a_u.auth_ntlmssp_state) {
854 /* Free any previous auth type. */
855 free_pipe_ntlmssp_auth_data(&p->auth);
858 if (!got_kerberos_mechanism) {
859 /* Initialize the NTLM engine. */
860 status = auth_ntlmssp_start(&a);
861 if (!NT_STATUS_IS_OK(status)) {
865 switch (pauth_info->auth_level) {
866 case DCERPC_AUTH_LEVEL_INTEGRITY:
867 auth_ntlmssp_want_sign(a);
869 case DCERPC_AUTH_LEVEL_PRIVACY:
870 auth_ntlmssp_want_seal(a);
876 * Pass the first security blob of data to it.
877 * This can return an error or NT_STATUS_MORE_PROCESSING_REQUIRED
878 * which means we need another packet to complete the bind.
881 status = auth_ntlmssp_update(a, secblob, &chal);
883 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
884 DEBUG(3,("pipe_spnego_auth_bind_negotiate: auth_ntlmssp_update failed.\n"));
888 /* Generate the response blob we need for step 2 of the bind. */
889 *response = spnego_gen_auth_response(&chal, status, OID_NTLMSSP);
892 * SPNEGO negotiate down to NTLMSSP. The subsequent
893 * code to process follow-up packets is not complete
896 *response = spnego_gen_auth_response(NULL,
897 NT_STATUS_MORE_PROCESSING_REQUIRED,
901 /* Make sure data is bound to the memctx, to be freed the caller */
902 talloc_steal(mem_ctx, response->data);
904 /* auth_pad_len will be handled by the caller */
906 p->auth.a_u.auth_ntlmssp_state = a;
907 p->auth.auth_data_free_func = &free_pipe_ntlmssp_auth_data;
908 p->auth.auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
910 data_blob_free(&secblob);
911 data_blob_free(&chal);
913 /* We can't set pipe_bound True yet - we need an RPC_ALTER_CONTEXT response packet... */
918 data_blob_free(&secblob);
919 data_blob_free(&chal);
921 p->auth.a_u.auth_ntlmssp_state = NULL;
926 /*******************************************************************
927 Handle the second part of a SPNEGO bind auth.
928 *******************************************************************/
930 static bool pipe_spnego_auth_bind_continue(pipes_struct *p,
932 struct dcerpc_auth *pauth_info,
936 DATA_BLOB auth_reply;
937 struct auth_ntlmssp_state *a = p->auth.a_u.auth_ntlmssp_state;
939 ZERO_STRUCT(auth_blob);
940 ZERO_STRUCT(auth_reply);
943 * NB. If we've negotiated down from krb5 to NTLMSSP we'll currently
944 * fail here as 'a' == NULL.
946 if (p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP || !a) {
947 DEBUG(0,("pipe_spnego_auth_bind_continue: not in NTLMSSP auth state.\n"));
951 if (pauth_info->credentials.data[0] != ASN1_CONTEXT(1)) {
952 DEBUG(0,("pipe_spnego_auth_bind_continue: invalid SPNEGO blob type.\n"));
956 if (!spnego_parse_auth(pauth_info->credentials, &auth_blob)) {
957 DEBUG(0,("pipe_spnego_auth_bind_continue: invalid SPNEGO blob.\n"));
962 * The following call actually checks the challenge/response data.
963 * for correctness against the given DOMAIN\user name.
966 if (!pipe_ntlmssp_verify_final(p, &auth_blob)) {
970 data_blob_free(&auth_blob);
972 /* Generate the spnego "accept completed" blob - no incoming data. */
973 *response = spnego_gen_auth_response(&auth_reply, NT_STATUS_OK, OID_NTLMSSP);
975 /* Make sure data is bound to the memctx, to be freed the caller */
976 talloc_steal(mem_ctx, response->data);
978 data_blob_free(&auth_reply);
980 p->pipe_bound = True;
986 data_blob_free(&auth_blob);
987 data_blob_free(&auth_reply);
989 free_pipe_ntlmssp_auth_data(&p->auth);
990 p->auth.a_u.auth_ntlmssp_state = NULL;
995 /*******************************************************************
996 Handle an schannel bind auth.
997 *******************************************************************/
999 static bool pipe_schannel_auth_bind(pipes_struct *p,
1000 TALLOC_CTX *mem_ctx,
1001 struct dcerpc_auth *auth_info,
1002 DATA_BLOB *response)
1004 struct NL_AUTH_MESSAGE neg;
1005 struct NL_AUTH_MESSAGE reply;
1008 struct netlogon_creds_CredentialState *creds;
1009 DATA_BLOB session_key;
1010 enum ndr_err_code ndr_err;
1012 ndr_err = ndr_pull_struct_blob(
1013 &auth_info->credentials, mem_ctx, &neg,
1014 (ndr_pull_flags_fn_t)ndr_pull_NL_AUTH_MESSAGE);
1015 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1016 DEBUG(0,("pipe_schannel_auth_bind: Could not unmarshal SCHANNEL auth neg\n"));
1020 if (DEBUGLEVEL >= 10) {
1021 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &neg);
1024 if (!(neg.Flags & NL_FLAG_OEM_NETBIOS_COMPUTER_NAME)) {
1025 DEBUG(0,("pipe_schannel_auth_bind: Did not receive netbios computer name\n"));
1030 * The neg.oem_netbios_computer.a key here must match the remote computer name
1031 * given in the DOM_CLNT_SRV.uni_comp_name used on all netlogon pipe
1032 * operations that use credentials.
1036 status = schannel_get_creds_state(p, lp_private_dir(),
1037 neg.oem_netbios_computer.a, &creds);
1040 if (!NT_STATUS_IS_OK(status)) {
1041 DEBUG(0, ("pipe_schannel_auth_bind: Attempt to bind using schannel without successful serverauth2\n"));
1045 p->auth.a_u.schannel_auth = talloc(p, struct schannel_state);
1046 if (!p->auth.a_u.schannel_auth) {
1051 p->auth.a_u.schannel_auth->state = SCHANNEL_STATE_START;
1052 p->auth.a_u.schannel_auth->seq_num = 0;
1053 p->auth.a_u.schannel_auth->initiator = false;
1054 p->auth.a_u.schannel_auth->creds = creds;
1057 * JRA. Should we also copy the schannel session key into the pipe session key p->session_key
1058 * here ? We do that for NTLMSSP, but the session key is already set up from the vuser
1059 * struct of the person who opened the pipe. I need to test this further. JRA.
1061 * VL. As we are mapping this to guest set the generic key
1062 * "SystemLibraryDTC" key here. It's a bit difficult to test against
1063 * W2k3, as it does not allow schannel binds against SAMR and LSA
1067 session_key = generic_session_key();
1068 if (session_key.data == NULL) {
1069 DEBUG(0, ("pipe_schannel_auth_bind: Could not alloc session"
1074 ret = server_info_set_session_key(p->server_info, session_key);
1076 data_blob_free(&session_key);
1079 DEBUG(0, ("server_info_set_session_key failed\n"));
1083 /*** SCHANNEL verifier ***/
1085 reply.MessageType = NL_NEGOTIATE_RESPONSE;
1087 reply.Buffer.dummy = 5; /* ??? actually I don't think
1088 * this has any meaning
1091 ndr_err = ndr_push_struct_blob(response, mem_ctx, &reply,
1092 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
1093 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1094 DEBUG(0,("Failed to marshall NL_AUTH_MESSAGE.\n"));
1098 if (DEBUGLEVEL >= 10) {
1099 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &reply);
1102 DEBUG(10,("pipe_schannel_auth_bind: schannel auth: domain [%s] myname [%s]\n",
1103 neg.oem_netbios_domain.a, neg.oem_netbios_computer.a));
1105 /* We're finished with this bind - no more packets. */
1106 p->auth.auth_data_free_func = NULL;
1107 p->auth.auth_type = PIPE_AUTH_TYPE_SCHANNEL;
1109 p->pipe_bound = True;
1114 /*******************************************************************
1115 Handle an NTLMSSP bind auth.
1116 *******************************************************************/
1118 static bool pipe_ntlmssp_auth_bind(pipes_struct *p,
1119 TALLOC_CTX *mem_ctx,
1120 struct dcerpc_auth *auth_info,
1121 DATA_BLOB *response)
1124 struct auth_ntlmssp_state *a = NULL;
1126 if (strncmp((char *)auth_info->credentials.data, "NTLMSSP", 7) != 0) {
1127 DEBUG(0, ("Failed to read NTLMSSP in blob\n"));
1131 /* We have an NTLMSSP blob. */
1132 status = auth_ntlmssp_start(&a);
1133 if (!NT_STATUS_IS_OK(status)) {
1134 DEBUG(0,("pipe_ntlmssp_auth_bind: auth_ntlmssp_start failed: %s\n",
1135 nt_errstr(status) ));
1139 switch (auth_info->auth_level) {
1140 case DCERPC_AUTH_LEVEL_INTEGRITY:
1141 auth_ntlmssp_want_sign(a);
1143 case DCERPC_AUTH_LEVEL_PRIVACY:
1144 auth_ntlmssp_want_seal(a);
1150 status = auth_ntlmssp_update(a, auth_info->credentials, response);
1151 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1152 DEBUG(0,("pipe_ntlmssp_auth_bind: auth_ntlmssp_update failed: %s\n",
1153 nt_errstr(status) ));
1157 /* Make sure data is bound to the memctx, to be freed the caller */
1158 talloc_steal(mem_ctx, response->data);
1160 p->auth.a_u.auth_ntlmssp_state = a;
1161 p->auth.auth_data_free_func = &free_pipe_ntlmssp_auth_data;
1162 p->auth.auth_type = PIPE_AUTH_TYPE_NTLMSSP;
1164 DEBUG(10,("pipe_ntlmssp_auth_bind: NTLMSSP auth started\n"));
1166 /* We can't set pipe_bound True yet - we need an DCERPC_PKT_AUTH3 response packet... */
1171 free_pipe_ntlmssp_auth_data(&p->auth);
1172 p->auth.a_u.auth_ntlmssp_state = NULL;
1176 /*******************************************************************
1177 Respond to a pipe bind request.
1178 *******************************************************************/
1180 bool api_pipe_bind_req(pipes_struct *p, struct ncacn_packet *pkt)
1182 struct dcerpc_auth auth_info;
1184 unsigned int auth_type = DCERPC_AUTH_TYPE_NONE;
1186 struct ndr_syntax_id id;
1187 union dcerpc_payload u;
1188 struct dcerpc_ack_ctx bind_ack_ctx;
1189 DATA_BLOB auth_resp = data_blob_null;
1190 DATA_BLOB auth_blob = data_blob_null;
1192 /* No rebinds on a bound pipe - use alter context. */
1193 if (p->pipe_bound) {
1194 DEBUG(2,("api_pipe_bind_req: rejecting bind request on bound "
1196 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1197 return setup_bind_nak(p, pkt);
1200 if (pkt->u.bind.num_contexts == 0) {
1201 DEBUG(0, ("api_pipe_bind_req: no rpc contexts around\n"));
1206 * Try and find the correct pipe name to ensure
1207 * that this is a pipe name we support.
1209 id = pkt->u.bind.ctx_list[0].abstract_syntax;
1210 if (rpc_srv_pipe_exists_by_id(&id)) {
1211 DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
1212 rpc_srv_get_pipe_cli_name(&id),
1213 rpc_srv_get_pipe_srv_name(&id)));
1215 status = smb_probe_module(
1216 "rpc", get_pipe_name_from_syntax(
1218 &pkt->u.bind.ctx_list[0].abstract_syntax));
1220 if (NT_STATUS_IS_ERR(status)) {
1221 DEBUG(3,("api_pipe_bind_req: Unknown pipe name %s in bind request.\n",
1222 get_pipe_name_from_syntax(
1224 &pkt->u.bind.ctx_list[0].abstract_syntax)));
1226 return setup_bind_nak(p, pkt);
1229 if (rpc_srv_get_pipe_interface_by_cli_name(
1230 get_pipe_name_from_syntax(talloc_tos(),
1233 DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
1234 rpc_srv_get_pipe_cli_name(&id),
1235 rpc_srv_get_pipe_srv_name(&id)));
1237 DEBUG(0, ("module %s doesn't provide functions for "
1239 get_pipe_name_from_syntax(talloc_tos(),
1241 get_pipe_name_from_syntax(talloc_tos(),
1243 return setup_bind_nak(p, pkt);
1247 DEBUG(5,("api_pipe_bind_req: make response. %d\n", __LINE__));
1249 if (pkt->u.bind.assoc_group_id != 0) {
1250 assoc_gid = pkt->u.bind.assoc_group_id;
1256 * Create the bind response struct.
1259 /* If the requested abstract synt uuid doesn't match our client pipe,
1260 reject the bind_ack & set the transfer interface synt to all 0's,
1261 ver 0 (observed when NT5 attempts to bind to abstract interfaces
1263 Needed when adding entries to a DACL from NT5 - SK */
1265 if (check_bind_req(p,
1266 &pkt->u.bind.ctx_list[0].abstract_syntax,
1267 &pkt->u.bind.ctx_list[0].transfer_syntaxes[0],
1268 pkt->u.bind.ctx_list[0].context_id)) {
1270 bind_ack_ctx.result = 0;
1271 bind_ack_ctx.reason = 0;
1272 bind_ack_ctx.syntax = pkt->u.bind.ctx_list[0].transfer_syntaxes[0];
1274 p->pipe_bound = False;
1275 /* Rejection reason: abstract syntax not supported */
1276 bind_ack_ctx.result = DCERPC_BIND_PROVIDER_REJECT;
1277 bind_ack_ctx.reason = DCERPC_BIND_REASON_ASYNTAX;
1278 bind_ack_ctx.syntax = null_ndr_syntax_id;
1282 * Check if this is an authenticated bind request.
1284 if (pkt->auth_length) {
1285 /* Quick length check. Won't catch a bad auth footer,
1286 * prevents overrun. */
1288 if (pkt->frag_length < RPC_HEADER_LEN +
1289 DCERPC_AUTH_TRAILER_LENGTH +
1291 DEBUG(0,("api_pipe_bind_req: auth_len (%u) "
1292 "too long for fragment %u.\n",
1293 (unsigned int)pkt->auth_length,
1294 (unsigned int)pkt->frag_length));
1299 * Decode the authentication verifier.
1301 status = dcerpc_pull_dcerpc_auth(pkt,
1302 &pkt->u.bind.auth_info,
1303 &auth_info, p->endian);
1304 if (!NT_STATUS_IS_OK(status)) {
1305 DEBUG(0, ("Unable to unmarshall dcerpc_auth.\n"));
1309 auth_type = auth_info.auth_type;
1311 /* Work out if we have to sign or seal etc. */
1312 switch (auth_info.auth_level) {
1313 case DCERPC_AUTH_LEVEL_INTEGRITY:
1314 p->auth.auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1316 case DCERPC_AUTH_LEVEL_PRIVACY:
1317 p->auth.auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1320 DEBUG(0, ("Unexpected auth level (%u).\n",
1321 (unsigned int)auth_info.auth_level ));
1325 switch (auth_type) {
1326 case DCERPC_AUTH_TYPE_NTLMSSP:
1327 if (!pipe_ntlmssp_auth_bind(p, pkt,
1328 &auth_info, &auth_resp)) {
1334 case DCERPC_AUTH_TYPE_SCHANNEL:
1335 if (!pipe_schannel_auth_bind(p, pkt,
1336 &auth_info, &auth_resp)) {
1341 case DCERPC_AUTH_TYPE_SPNEGO:
1342 if (!pipe_spnego_auth_bind_negotiate(p, pkt,
1343 &auth_info, &auth_resp)) {
1348 case DCERPC_AUTH_TYPE_NONE:
1352 DEBUG(0, ("Unknown auth type %x requested.\n", auth_type));
1357 if (auth_type == DCERPC_AUTH_TYPE_NONE) {
1358 /* Unauthenticated bind request. */
1359 /* We're finished - no more packets. */
1360 p->auth.auth_type = PIPE_AUTH_TYPE_NONE;
1361 /* We must set the pipe auth_level here also. */
1362 p->auth.auth_level = DCERPC_AUTH_LEVEL_NONE;
1363 p->pipe_bound = True;
1364 /* The session key was initialized from the SMB
1365 * session in make_internal_rpc_pipe_p */
1368 ZERO_STRUCT(u.bind_ack);
1369 u.bind_ack.max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
1370 u.bind_ack.max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
1371 u.bind_ack.assoc_group_id = assoc_gid;
1373 /* name has to be \PIPE\xxxxx */
1374 u.bind_ack.secondary_address =
1375 talloc_asprintf(pkt, "\\PIPE\\%s",
1376 rpc_srv_get_pipe_srv_name(&id));
1377 if (!u.bind_ack.secondary_address) {
1378 DEBUG(0, ("Out of memory!\n"));
1381 u.bind_ack.secondary_address_size =
1382 strlen(u.bind_ack.secondary_address) + 1;
1384 u.bind_ack.num_results = 1;
1385 u.bind_ack.ctx_list = &bind_ack_ctx;
1387 /* NOTE: We leave the auth_info empty so we can calculate the padding
1388 * later and then append the auth_info --simo */
1391 * Marshall directly into the outgoing PDU space. We
1392 * must do this as we need to set to the bind response
1393 * header and are never sending more than one PDU here.
1396 status = dcerpc_push_ncacn_packet(p->mem_ctx,
1397 DCERPC_PKT_BIND_ACK,
1398 DCERPC_PFC_FLAG_FIRST |
1399 DCERPC_PFC_FLAG_LAST,
1404 if (!NT_STATUS_IS_OK(status)) {
1405 DEBUG(0, ("Failed to marshall bind_ack packet. (%s)\n",
1406 nt_errstr(status)));
1409 if (auth_resp.length) {
1411 status = dcerpc_push_dcerpc_auth(pkt,
1413 auth_info.auth_level,
1415 1, /* auth_context_id */
1418 if (!NT_STATUS_IS_OK(status)) {
1419 DEBUG(0, ("Marshalling of dcerpc_auth failed.\n"));
1424 /* Now that we have the auth len store it into the right place in
1425 * the dcerpc header */
1426 dcerpc_set_frag_length(&p->out_data.frag,
1427 p->out_data.frag.length + auth_blob.length);
1429 if (auth_blob.length) {
1431 if (!data_blob_append(p->mem_ctx, &p->out_data.frag,
1432 auth_blob.data, auth_blob.length)) {
1433 DEBUG(0, ("Append of auth info failed.\n"));
1439 * Setup the lengths for the initial reply.
1442 p->out_data.data_sent_length = 0;
1443 p->out_data.current_pdu_sent = 0;
1445 TALLOC_FREE(auth_blob.data);
1450 data_blob_free(&p->out_data.frag);
1451 TALLOC_FREE(auth_blob.data);
1452 return setup_bind_nak(p, pkt);
1455 /****************************************************************************
1456 Deal with an alter context call. Can be third part of 3 leg auth request for
1458 ****************************************************************************/
1460 bool api_pipe_alter_context(pipes_struct *p, struct ncacn_packet *pkt)
1462 struct dcerpc_auth auth_info;
1465 union dcerpc_payload u;
1466 struct dcerpc_ack_ctx bind_ack_ctx;
1467 DATA_BLOB auth_resp = data_blob_null;
1468 DATA_BLOB auth_blob = data_blob_null;
1471 DEBUG(5,("api_pipe_alter_context: make response. %d\n", __LINE__));
1473 if (pkt->u.bind.assoc_group_id != 0) {
1474 assoc_gid = pkt->u.bind.assoc_group_id;
1480 * Create the bind response struct.
1483 /* If the requested abstract synt uuid doesn't match our client pipe,
1484 reject the bind_ack & set the transfer interface synt to all 0's,
1485 ver 0 (observed when NT5 attempts to bind to abstract interfaces
1487 Needed when adding entries to a DACL from NT5 - SK */
1489 if (check_bind_req(p,
1490 &pkt->u.bind.ctx_list[0].abstract_syntax,
1491 &pkt->u.bind.ctx_list[0].transfer_syntaxes[0],
1492 pkt->u.bind.ctx_list[0].context_id)) {
1494 bind_ack_ctx.result = 0;
1495 bind_ack_ctx.reason = 0;
1496 bind_ack_ctx.syntax = pkt->u.bind.ctx_list[0].transfer_syntaxes[0];
1498 p->pipe_bound = False;
1499 /* Rejection reason: abstract syntax not supported */
1500 bind_ack_ctx.result = DCERPC_BIND_PROVIDER_REJECT;
1501 bind_ack_ctx.reason = DCERPC_BIND_REASON_ASYNTAX;
1502 bind_ack_ctx.syntax = null_ndr_syntax_id;
1506 * Check if this is an authenticated alter context request.
1508 if (pkt->auth_length) {
1509 /* Quick length check. Won't catch a bad auth footer,
1510 * prevents overrun. */
1512 if (pkt->frag_length < RPC_HEADER_LEN +
1513 DCERPC_AUTH_TRAILER_LENGTH +
1515 DEBUG(0,("api_pipe_alter_context: auth_len (%u) "
1516 "too long for fragment %u.\n",
1517 (unsigned int)pkt->auth_length,
1518 (unsigned int)pkt->frag_length ));
1522 status = dcerpc_pull_dcerpc_auth(pkt,
1523 &pkt->u.bind.auth_info,
1524 &auth_info, p->endian);
1525 if (!NT_STATUS_IS_OK(status)) {
1526 DEBUG(0, ("Unable to unmarshall dcerpc_auth.\n"));
1532 * Currently only the SPNEGO auth type uses the alter ctx
1533 * response in place of the NTLMSSP auth3 type.
1536 if (auth_info.auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1537 /* We can only finish if the pipe is unbound. */
1538 if (!p->pipe_bound) {
1539 if (!pipe_spnego_auth_bind_continue(p, pkt,
1540 &auth_info, &auth_resp)) {
1550 ZERO_STRUCT(u.alter_resp);
1551 u.alter_resp.max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
1552 u.alter_resp.max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
1553 u.alter_resp.assoc_group_id = assoc_gid;
1555 /* secondary address CAN be NULL
1556 * as the specs say it's ignored.
1557 * It MUST be NULL to have the spoolss working.
1559 u.alter_resp.secondary_address = "";
1560 u.alter_resp.secondary_address_size = 1;
1562 u.alter_resp.num_results = 1;
1563 u.alter_resp.ctx_list = &bind_ack_ctx;
1565 /* NOTE: We leave the auth_info empty so we can calculate the padding
1566 * later and then append the auth_info --simo */
1569 * Marshall directly into the outgoing PDU space. We
1570 * must do this as we need to set to the bind response
1571 * header and are never sending more than one PDU here.
1574 status = dcerpc_push_ncacn_packet(p->mem_ctx,
1575 DCERPC_PKT_ALTER_RESP,
1576 DCERPC_PFC_FLAG_FIRST |
1577 DCERPC_PFC_FLAG_LAST,
1582 if (!NT_STATUS_IS_OK(status)) {
1583 DEBUG(0, ("Failed to marshall bind_ack packet. (%s)\n",
1584 nt_errstr(status)));
1587 if (auth_resp.length) {
1589 /* Work out any padding needed before the auth footer. */
1590 pad_len = p->out_data.frag.length % SERVER_NDR_PADDING_SIZE;
1592 pad_len = SERVER_NDR_PADDING_SIZE - pad_len;
1593 DEBUG(10, ("auth pad_len = %u\n",
1594 (unsigned int)pad_len));
1597 status = dcerpc_push_dcerpc_auth(pkt,
1598 auth_info.auth_type,
1599 auth_info.auth_level,
1601 1, /* auth_context_id */
1604 if (!NT_STATUS_IS_OK(status)) {
1605 DEBUG(0, ("Marshalling of dcerpc_auth failed.\n"));
1610 /* Now that we have the auth len store it into the right place in
1611 * the dcerpc header */
1612 dcerpc_set_frag_length(&p->out_data.frag,
1613 p->out_data.frag.length +
1614 pad_len + auth_blob.length);
1616 if (auth_resp.length) {
1618 char pad[SERVER_NDR_PADDING_SIZE];
1619 memset(pad, '\0', SERVER_NDR_PADDING_SIZE);
1620 if (!data_blob_append(p->mem_ctx,
1623 DEBUG(0, ("api_pipe_bind_req: failed to add "
1624 "%u bytes of pad data.\n",
1625 (unsigned int)pad_len));
1630 if (!data_blob_append(p->mem_ctx, &p->out_data.frag,
1631 auth_blob.data, auth_blob.length)) {
1632 DEBUG(0, ("Append of auth info failed.\n"));
1638 * Setup the lengths for the initial reply.
1641 p->out_data.data_sent_length = 0;
1642 p->out_data.current_pdu_sent = 0;
1644 TALLOC_FREE(auth_blob.data);
1649 data_blob_free(&p->out_data.frag);
1650 TALLOC_FREE(auth_blob.data);
1651 return setup_bind_nak(p, pkt);
1654 /****************************************************************************
1655 Find the set of RPC functions associated with this context_id
1656 ****************************************************************************/
1658 static PIPE_RPC_FNS* find_pipe_fns_by_context( PIPE_RPC_FNS *list, uint32 context_id )
1660 PIPE_RPC_FNS *fns = NULL;
1663 DEBUG(0,("find_pipe_fns_by_context: ERROR! No context list for pipe!\n"));
1667 for (fns=list; fns; fns=fns->next ) {
1668 if ( fns->context_id == context_id )
1674 /****************************************************************************
1676 ****************************************************************************/
1678 void free_pipe_rpc_context( PIPE_RPC_FNS *list )
1680 PIPE_RPC_FNS *tmp = list;
1692 static bool api_rpcTNP(pipes_struct *p, struct ncacn_packet *pkt,
1693 const struct api_struct *api_rpc_cmds, int n_cmds);
1695 /****************************************************************************
1696 Find the correct RPC function to call for this request.
1697 If the pipe is authenticated then become the correct UNIX user
1698 before doing the call.
1699 ****************************************************************************/
1701 bool api_pipe_request(pipes_struct *p, struct ncacn_packet *pkt)
1704 bool changed_user = False;
1705 PIPE_RPC_FNS *pipe_fns;
1707 if (p->pipe_bound &&
1708 ((p->auth.auth_type == PIPE_AUTH_TYPE_NTLMSSP) ||
1709 (p->auth.auth_type == PIPE_AUTH_TYPE_SPNEGO_NTLMSSP))) {
1710 if(!become_authenticated_pipe_user(p)) {
1711 data_blob_free(&p->out_data.rdata);
1714 changed_user = True;
1717 DEBUG(5, ("Requested \\PIPE\\%s\n",
1718 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1720 /* get the set of RPC functions for this context */
1722 pipe_fns = find_pipe_fns_by_context(p->contexts,
1723 pkt->u.request.context_id);
1726 TALLOC_CTX *frame = talloc_stackframe();
1727 ret = api_rpcTNP(p, pkt, pipe_fns->cmds, pipe_fns->n_cmds);
1731 DEBUG(0, ("No rpc function table associated with context "
1732 "[%d] on pipe [%s]\n",
1733 pkt->u.request.context_id,
1734 get_pipe_name_from_syntax(talloc_tos(),
1739 unbecome_authenticated_pipe_user();
1745 /*******************************************************************
1746 Calls the underlying RPC function for a named pipe.
1747 ********************************************************************/
1749 static bool api_rpcTNP(pipes_struct *p, struct ncacn_packet *pkt,
1750 const struct api_struct *api_rpc_cmds, int n_cmds)
1755 /* interpret the command */
1756 DEBUG(4,("api_rpcTNP: %s op 0x%x - ",
1757 get_pipe_name_from_syntax(talloc_tos(), &p->syntax),
1758 pkt->u.request.opnum));
1760 if (DEBUGLEVEL >= 50) {
1762 slprintf(name, sizeof(name)-1, "in_%s",
1763 get_pipe_name_from_syntax(talloc_tos(), &p->syntax));
1764 dump_pdu_region(name, pkt->u.request.opnum,
1765 &p->in_data.data, 0,
1766 p->in_data.data.length);
1769 for (fn_num = 0; fn_num < n_cmds; fn_num++) {
1770 if (api_rpc_cmds[fn_num].opnum == pkt->u.request.opnum &&
1771 api_rpc_cmds[fn_num].fn != NULL) {
1772 DEBUG(3, ("api_rpcTNP: rpc command: %s\n",
1773 api_rpc_cmds[fn_num].name));
1778 if (fn_num == n_cmds) {
1780 * For an unknown RPC just return a fault PDU but
1781 * return True to allow RPC's on the pipe to continue
1782 * and not put the pipe into fault state. JRA.
1784 DEBUG(4, ("unknown\n"));
1785 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
1789 offset1 = p->out_data.rdata.length;
1791 DEBUG(6, ("api_rpc_cmds[%d].fn == %p\n",
1792 fn_num, api_rpc_cmds[fn_num].fn));
1793 /* do the actual command */
1794 if(!api_rpc_cmds[fn_num].fn(p)) {
1795 DEBUG(0,("api_rpcTNP: %s: %s failed.\n",
1796 get_pipe_name_from_syntax(talloc_tos(), &p->syntax),
1797 api_rpc_cmds[fn_num].name));
1798 data_blob_free(&p->out_data.rdata);
1802 if (p->bad_handle_fault_state) {
1803 DEBUG(4,("api_rpcTNP: bad handle fault return.\n"));
1804 p->bad_handle_fault_state = False;
1805 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_CONTEXT_MISMATCH));
1809 if (p->rng_fault_state) {
1810 DEBUG(4, ("api_rpcTNP: rng fault return\n"));
1811 p->rng_fault_state = False;
1812 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
1816 if (DEBUGLEVEL >= 50) {
1818 slprintf(name, sizeof(name)-1, "out_%s",
1819 get_pipe_name_from_syntax(talloc_tos(), &p->syntax));
1820 dump_pdu_region(name, pkt->u.request.opnum,
1821 &p->out_data.rdata, offset1,
1822 p->out_data.rdata.length);
1825 DEBUG(5,("api_rpcTNP: called %s successfully\n",
1826 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1828 /* Check for buffer underflow in rpc parsing */
1829 if ((DEBUGLEVEL >= 10) &&
1830 (pkt->frag_length < p->in_data.data.length)) {
1831 DEBUG(10, ("api_rpcTNP: rpc input buffer underflow (parse error?)\n"));
1832 dump_data(10, p->in_data.data.data + pkt->frag_length,
1833 p->in_data.data.length - pkt->frag_length);
1839 /****************************************************************************
1840 Initialise an outgoing packet.
1841 ****************************************************************************/
1843 static bool pipe_init_outgoing_data(pipes_struct *p)
1845 output_data *o_data = &p->out_data;
1847 /* Reset the offset counters. */
1848 o_data->data_sent_length = 0;
1849 o_data->current_pdu_sent = 0;
1851 data_blob_free(&o_data->frag);
1853 /* Free any memory in the current return data buffer. */
1854 data_blob_free(&o_data->rdata);
1859 /****************************************************************************
1860 Sets the fault state on incoming packets.
1861 ****************************************************************************/
1863 void set_incoming_fault(pipes_struct *p)
1865 data_blob_free(&p->in_data.data);
1866 p->in_data.pdu_needed_len = 0;
1867 p->in_data.pdu.length = 0;
1868 p->fault_state = True;
1869 DEBUG(10, ("set_incoming_fault: Setting fault state on pipe %s\n",
1870 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1873 static bool dcesrv_auth_request(pipes_struct *p, struct ncacn_packet *pkt)
1876 size_t hdr_size = DCERPC_REQUEST_LENGTH;
1877 struct dcerpc_auth auth;
1878 uint32_t auth_length;
1882 DEBUG(10, ("Checking request auth.\n"));
1884 if (pkt->pfc_flags & DCERPC_PFC_FLAG_OBJECT_UUID) {
1888 switch (p->auth.auth_level) {
1889 case DCERPC_AUTH_LEVEL_PRIVACY:
1890 DEBUG(10, ("Requested Privacy.\n"));
1893 case DCERPC_AUTH_LEVEL_INTEGRITY:
1894 DEBUG(10, ("Requested Integrity.\n"));
1897 case DCERPC_AUTH_LEVEL_CONNECT:
1898 if (pkt->auth_length != 0) {
1902 case DCERPC_AUTH_LEVEL_NONE:
1903 if (pkt->auth_length != 0) {
1912 status = dcerpc_pull_auth_trailer(pkt, pkt,
1913 &pkt->u.request.stub_and_verifier,
1914 &auth, &auth_length, false);
1915 if (!NT_STATUS_IS_OK(status)) {
1919 pkt->u.request.stub_and_verifier.length -= auth_length;
1921 data.data = p->in_data.pdu.data + hdr_size;
1922 data.length = pkt->u.request.stub_and_verifier.length;
1923 full_pkt.data = p->in_data.pdu.data;
1924 full_pkt.length = p->in_data.pdu.length - auth.credentials.length;
1926 switch (p->auth.auth_type) {
1927 case PIPE_AUTH_TYPE_NONE:
1930 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
1931 case PIPE_AUTH_TYPE_NTLMSSP:
1933 DEBUG(10, ("NTLMSSP auth\n"));
1935 if (!p->auth.a_u.auth_ntlmssp_state) {
1936 DEBUG(0, ("Invalid auth level, "
1937 "failed to process packet auth.\n"));
1941 switch (p->auth.auth_level) {
1942 case DCERPC_AUTH_LEVEL_PRIVACY:
1943 status = auth_ntlmssp_unseal_packet(
1944 p->auth.a_u.auth_ntlmssp_state,
1945 data.data, data.length,
1946 full_pkt.data, full_pkt.length,
1948 if (!NT_STATUS_IS_OK(status)) {
1951 memcpy(pkt->u.request.stub_and_verifier.data,
1952 data.data, data.length);
1955 case DCERPC_AUTH_LEVEL_INTEGRITY:
1956 status = auth_ntlmssp_check_packet(
1957 p->auth.a_u.auth_ntlmssp_state,
1958 data.data, data.length,
1959 full_pkt.data, full_pkt.length,
1961 if (!NT_STATUS_IS_OK(status)) {
1967 DEBUG(0, ("Invalid auth level, "
1968 "failed to process packet auth.\n"));
1973 case PIPE_AUTH_TYPE_SCHANNEL:
1975 DEBUG(10, ("SCHANNEL auth\n"));
1977 switch (p->auth.auth_level) {
1978 case DCERPC_AUTH_LEVEL_PRIVACY:
1979 status = netsec_incoming_packet(
1980 p->auth.a_u.schannel_auth,
1982 data.data, data.length,
1984 if (!NT_STATUS_IS_OK(status)) {
1987 memcpy(pkt->u.request.stub_and_verifier.data,
1988 data.data, data.length);
1991 case DCERPC_AUTH_LEVEL_INTEGRITY:
1992 status = netsec_incoming_packet(
1993 p->auth.a_u.schannel_auth,
1995 data.data, data.length,
1997 if (!NT_STATUS_IS_OK(status)) {
2003 DEBUG(0, ("Invalid auth level, "
2004 "failed to process packet auth.\n"));
2010 DEBUG(0, ("process_request_pdu: "
2011 "unknown auth type %u set.\n",
2012 (unsigned int)p->auth.auth_type));
2013 set_incoming_fault(p);
2017 /* remove the indicated amount of padding */
2018 if (pkt->u.request.stub_and_verifier.length < auth.auth_pad_length) {
2021 pkt->u.request.stub_and_verifier.length -= auth.auth_pad_length;
2026 /****************************************************************************
2027 Processes a request pdu. This will do auth processing if needed, and
2028 appends the data into the complete stream if the LAST flag is not set.
2029 ****************************************************************************/
2031 static bool process_request_pdu(pipes_struct *p, struct ncacn_packet *pkt)
2035 if (!p->pipe_bound) {
2036 DEBUG(0,("process_request_pdu: rpc request with no bind.\n"));
2037 set_incoming_fault(p);
2041 /* Store the opnum */
2042 p->opnum = pkt->u.request.opnum;
2044 if (!dcesrv_auth_request(p, pkt)) {
2045 DEBUG(0,("Failed to check packet auth.\n"));
2046 set_incoming_fault(p);
2050 data = pkt->u.request.stub_and_verifier;
2053 * Check the data length doesn't go over the 15Mb limit.
2054 * increased after observing a bug in the Windows NT 4.0 SP6a
2055 * spoolsv.exe when the response to a GETPRINTERDRIVER2 RPC
2056 * will not fit in the initial buffer of size 0x1068 --jerry 22/01/2002
2059 if (p->in_data.data.length + data.length > MAX_RPC_DATA_SIZE) {
2060 DEBUG(0, ("process_request_pdu: "
2061 "rpc data buffer too large (%u) + (%u)\n",
2062 (unsigned int)p->in_data.data.length,
2063 (unsigned int)data.length));
2064 set_incoming_fault(p);
2069 * Append the data portion into the buffer and return.
2073 if (!data_blob_append(p->mem_ctx, &p->in_data.data,
2074 data.data, data.length)) {
2075 DEBUG(0, ("Unable to append data size %u "
2076 "to parse buffer of size %u.\n",
2077 (unsigned int)data.length,
2078 (unsigned int)p->in_data.data.length));
2079 set_incoming_fault(p);
2084 if (pkt->pfc_flags & DCERPC_PFC_FLAG_LAST) {
2087 * Ok - we finally have a complete RPC stream.
2088 * Call the rpc command to process it.
2092 * Process the complete data stream here.
2094 if (pipe_init_outgoing_data(p)) {
2095 ret = api_pipe_request(p, pkt);
2104 /****************************************************************************
2105 Processes a finished PDU stored in p->in_data.pdu.
2106 ****************************************************************************/
2108 void process_complete_pdu(pipes_struct *p)
2110 struct ncacn_packet *pkt = NULL;
2114 if(p->fault_state) {
2115 DEBUG(10,("process_complete_pdu: pipe %s in fault state.\n",
2116 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
2120 pkt = talloc(p->mem_ctx, struct ncacn_packet);
2122 DEBUG(0, ("Out of memory!\n"));
2127 * Ensure we're using the corrent endianness for both the
2128 * RPC header flags and the raw data we will be reading from.
2130 if (dcerpc_get_endian_flag(&p->in_data.pdu) & DCERPC_DREP_LE) {
2131 p->endian = RPC_LITTLE_ENDIAN;
2133 p->endian = RPC_BIG_ENDIAN;
2135 DEBUG(10, ("PDU is in %s Endian format!\n", p->endian?"Big":"Little"));
2137 status = dcerpc_pull_ncacn_packet(pkt, &p->in_data.pdu,
2139 if (!NT_STATUS_IS_OK(status)) {
2140 DEBUG(0, ("Failed to unmarshal rpc packet: %s!\n",
2141 nt_errstr(status)));
2145 /* Store the call_id */
2146 p->call_id = pkt->call_id;
2148 DEBUG(10, ("Processing packet type %d\n", (int)pkt->ptype));
2150 switch (pkt->ptype) {
2151 case DCERPC_PKT_REQUEST:
2152 reply = process_request_pdu(p, pkt);
2155 case DCERPC_PKT_PING: /* CL request - ignore... */
2156 DEBUG(0, ("process_complete_pdu: Error. "
2157 "Connectionless packet type %d received on "
2158 "pipe %s.\n", (int)pkt->ptype,
2159 get_pipe_name_from_syntax(talloc_tos(),
2163 case DCERPC_PKT_RESPONSE: /* No responses here. */
2164 DEBUG(0, ("process_complete_pdu: Error. "
2165 "DCERPC_PKT_RESPONSE received from client "
2167 get_pipe_name_from_syntax(talloc_tos(),
2171 case DCERPC_PKT_FAULT:
2172 case DCERPC_PKT_WORKING:
2173 /* CL request - reply to a ping when a call in process. */
2174 case DCERPC_PKT_NOCALL:
2175 /* CL - server reply to a ping call. */
2176 case DCERPC_PKT_REJECT:
2177 case DCERPC_PKT_ACK:
2178 case DCERPC_PKT_CL_CANCEL:
2179 case DCERPC_PKT_FACK:
2180 case DCERPC_PKT_CANCEL_ACK:
2181 DEBUG(0, ("process_complete_pdu: Error. "
2182 "Connectionless packet type %u received on "
2183 "pipe %s.\n", (unsigned int)pkt->ptype,
2184 get_pipe_name_from_syntax(talloc_tos(),
2188 case DCERPC_PKT_BIND:
2190 * We assume that a pipe bind is only in one pdu.
2192 if (pipe_init_outgoing_data(p)) {
2193 reply = api_pipe_bind_req(p, pkt);
2197 case DCERPC_PKT_BIND_ACK:
2198 case DCERPC_PKT_BIND_NAK:
2199 DEBUG(0, ("process_complete_pdu: Error. "
2200 "DCERPC_PKT_BINDACK/DCERPC_PKT_BINDNACK "
2201 "packet type %u received on pipe %s.\n",
2202 (unsigned int)pkt->ptype,
2203 get_pipe_name_from_syntax(talloc_tos(),
2208 case DCERPC_PKT_ALTER:
2210 * We assume that a pipe bind is only in one pdu.
2212 if (pipe_init_outgoing_data(p)) {
2213 reply = api_pipe_alter_context(p, pkt);
2217 case DCERPC_PKT_ALTER_RESP:
2218 DEBUG(0, ("process_complete_pdu: Error. "
2219 "DCERPC_PKT_ALTER_RESP on pipe %s: "
2220 "Should only be server -> client.\n",
2221 get_pipe_name_from_syntax(talloc_tos(),
2225 case DCERPC_PKT_AUTH3:
2227 * The third packet in an NTLMSSP auth exchange.
2229 if (pipe_init_outgoing_data(p)) {
2230 reply = api_pipe_bind_auth3(p, pkt);
2234 case DCERPC_PKT_SHUTDOWN:
2235 DEBUG(0, ("process_complete_pdu: Error. "
2236 "DCERPC_PKT_SHUTDOWN on pipe %s: "
2237 "Should only be server -> client.\n",
2238 get_pipe_name_from_syntax(talloc_tos(),
2242 case DCERPC_PKT_CO_CANCEL:
2243 /* For now just free all client data and continue
2245 DEBUG(3,("process_complete_pdu: DCERPC_PKT_CO_CANCEL."
2246 " Abandoning rpc call.\n"));
2247 /* As we never do asynchronous RPC serving, we can
2248 * never cancel a call (as far as I know).
2249 * If we ever did we'd have to send a cancel_ack reply.
2250 * For now, just free all client data and continue
2256 /* Enable this if we're doing async rpc. */
2257 /* We must check the outstanding callid matches. */
2258 if (pipe_init_outgoing_data(p)) {
2259 /* Send a cancel_ack PDU reply. */
2260 /* We should probably check the auth-verifier here. */
2261 reply = setup_cancel_ack_reply(p, pkt);
2266 case DCERPC_PKT_ORPHANED:
2267 /* We should probably check the auth-verifier here.
2268 * For now just free all client data and continue
2270 DEBUG(3, ("process_complete_pdu: DCERPC_PKT_ORPHANED."
2271 " Abandoning rpc call.\n"));
2276 DEBUG(0, ("process_complete_pdu: "
2277 "Unknown rpc type = %u received.\n",
2278 (unsigned int)pkt->ptype));
2284 DEBUG(3,("process_complete_pdu: DCE/RPC fault sent on "
2285 "pipe %s\n", get_pipe_name_from_syntax(talloc_tos(),
2287 set_incoming_fault(p);
2288 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
2292 * Reset the lengths. We're ready for a new pdu.
2294 TALLOC_FREE(p->in_data.pdu.data);
2295 p->in_data.pdu_needed_len = 0;
2296 p->in_data.pdu.length = 0;