2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Almost completely rewritten by (C) Jeremy Allison 2005 - 2010
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 /* this module apparently provides an implementation of DCE/RPC over a
21 * named pipe (IPC$ connection using SMBtrans). details of DCE/RPC
22 * documentation are available (in on-line form) from the X-Open group.
24 * this module should provide a level of abstraction between SMB
25 * and DCE/RPC, while minimising the amount of mallocs, unnecessary
26 * data copies, and network traffic.
31 #include "srv_pipe_internal.h"
32 #include "../librpc/gen_ndr/ndr_schannel.h"
33 #include "../libcli/auth/schannel.h"
34 #include "../libcli/auth/spnego.h"
35 #include "../libcli/auth/ntlmssp.h"
38 #define DBGC_CLASS DBGC_RPC_SRV
41 * Dump everything from the start of the end up of the provided data
42 * into a file, but only at debug level >= 50
44 static void dump_pdu_region(const char *name, int v,
45 DATA_BLOB *data, size_t start, size_t end)
51 if (DEBUGLEVEL < 50) return;
53 if (start > data->length || end > data->length || start > end) return;
55 for (i = 1; i < 100; i++) {
57 fname = talloc_asprintf(talloc_tos(),
61 fname = talloc_asprintf(talloc_tos(),
68 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0644);
69 if (fd != -1 || errno != EEXIST) break;
72 sz = write(fd, data->data + start, end - start);
74 if ((sz != end - start) || (i != 0) ) {
75 DEBUG(0, ("Error writing/closing %s: %ld!=%ld %d\n",
76 fname, (unsigned long)sz,
77 (unsigned long)end - start, i));
79 DEBUG(0,("created %s\n", fname));
85 static void free_pipe_ntlmssp_auth_data(struct pipe_auth_data *auth)
87 struct auth_ntlmssp_state *a = auth->a_u.auth_ntlmssp_state;
92 auth->a_u.auth_ntlmssp_state = NULL;
95 static DATA_BLOB generic_session_key(void)
97 return data_blob("SystemLibraryDTC", 16);
100 /*******************************************************************
102 ********************************************************************/
104 static bool add_ntlmssp_auth(pipes_struct *p)
106 enum dcerpc_AuthLevel auth_level = p->auth.auth_level;
107 DATA_BLOB auth_blob = data_blob_null;
110 /* FIXME: Is this right ?
111 * Keeping only to avoid changing semantics during refactoring
114 if (auth_level != DCERPC_AUTH_LEVEL_PRIVACY) {
115 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
118 /* Generate the auth blob. */
119 switch (auth_level) {
120 case DCERPC_AUTH_LEVEL_PRIVACY:
121 /* Data portion is encrypted. */
122 status = auth_ntlmssp_seal_packet(
123 p->auth.a_u.auth_ntlmssp_state,
124 (TALLOC_CTX *)p->out_data.frag.data,
125 &p->out_data.frag.data[DCERPC_RESPONSE_LENGTH],
126 p->out_data.frag.length
127 - DCERPC_RESPONSE_LENGTH
128 - DCERPC_AUTH_TRAILER_LENGTH,
129 p->out_data.frag.data,
130 p->out_data.frag.length,
134 case DCERPC_AUTH_LEVEL_INTEGRITY:
135 /* Data is signed. */
136 status = auth_ntlmssp_sign_packet(
137 p->auth.a_u.auth_ntlmssp_state,
138 (TALLOC_CTX *)p->out_data.frag.data,
139 &p->out_data.frag.data[DCERPC_RESPONSE_LENGTH],
140 p->out_data.frag.length
141 - DCERPC_RESPONSE_LENGTH
142 - DCERPC_AUTH_TRAILER_LENGTH,
143 p->out_data.frag.data,
144 p->out_data.frag.length,
149 status = NT_STATUS_INTERNAL_ERROR;
153 if (!NT_STATUS_IS_OK(status)) {
154 DEBUG(0, ("Failed to add NTLMSSP auth blob: %s\n",
156 data_blob_free(&p->out_data.frag);
160 /* Finally append the auth blob. */
161 if (!data_blob_append(p->mem_ctx, &p->out_data.frag,
162 auth_blob.data, auth_blob.length)) {
163 DEBUG(0, ("Failed to add %u bytes auth blob.\n",
164 (unsigned int)auth_blob.length));
165 data_blob_free(&p->out_data.frag);
168 data_blob_free(&auth_blob);
173 /*******************************************************************
174 Append a schannel authenticated fragment.
175 ********************************************************************/
177 static bool add_schannel_auth(pipes_struct *p)
179 DATA_BLOB auth_blob = data_blob_null;
182 /* Schannel processing. */
183 switch (p->auth.auth_level) {
184 case DCERPC_AUTH_LEVEL_PRIVACY:
185 status = netsec_outgoing_packet(
186 p->auth.a_u.schannel_auth,
187 (TALLOC_CTX *)p->out_data.frag.data,
189 &p->out_data.frag.data[DCERPC_RESPONSE_LENGTH],
190 p->out_data.frag.length
191 - DCERPC_RESPONSE_LENGTH
192 - DCERPC_AUTH_TRAILER_LENGTH,
196 case DCERPC_AUTH_LEVEL_INTEGRITY:
197 status = netsec_outgoing_packet(
198 p->auth.a_u.schannel_auth,
199 (TALLOC_CTX *)p->out_data.frag.data,
201 &p->out_data.frag.data[DCERPC_RESPONSE_LENGTH],
202 p->out_data.frag.length
203 - DCERPC_RESPONSE_LENGTH
204 - DCERPC_AUTH_TRAILER_LENGTH,
209 status = NT_STATUS_INTERNAL_ERROR;
213 if (!NT_STATUS_IS_OK(status)) {
214 DEBUG(0, ("Failed to add SCHANNEL auth blob: %s\n",
216 data_blob_free(&p->out_data.frag);
220 if (DEBUGLEVEL >= 10) {
221 dump_NL_AUTH_SIGNATURE(talloc_tos(), &auth_blob);
224 if (!data_blob_append(p->mem_ctx, &p->out_data.frag,
225 auth_blob.data, auth_blob.length)) {
226 DEBUG(0, ("Failed to add %u bytes auth blob.\n",
227 (unsigned int)auth_blob.length));
228 data_blob_free(&p->out_data.frag);
231 data_blob_free(&auth_blob);
236 /*******************************************************************
237 Generate the next PDU to be returned from the data.
238 ********************************************************************/
240 static bool create_next_packet(pipes_struct *p,
241 enum dcerpc_AuthType auth_type,
242 enum dcerpc_AuthLevel auth_level,
245 union dcerpc_payload u;
247 size_t data_len_left;
253 ZERO_STRUCT(u.response);
255 /* Set up rpc packet pfc flags. */
256 if (p->out_data.data_sent_length == 0) {
257 pfc_flags = DCERPC_PFC_FLAG_FIRST;
262 /* Work out how much we can fit in a single PDU. */
263 data_len_left = p->out_data.rdata.length -
264 p->out_data.data_sent_length;
266 /* Ensure there really is data left to send. */
267 if (!data_len_left) {
268 DEBUG(0, ("No data left to send !\n"));
272 /* Max space available - not including padding. */
274 max_len = RPC_MAX_PDU_FRAG_LEN
275 - DCERPC_RESPONSE_LENGTH
276 - DCERPC_AUTH_TRAILER_LENGTH
279 max_len = RPC_MAX_PDU_FRAG_LEN - DCERPC_RESPONSE_LENGTH;
283 * The amount we send is the minimum of the max_len
284 * and the amount left to send.
286 data_len = MIN(data_len_left, max_len);
289 /* Work out any padding alignment requirements. */
290 pad_len = (DCERPC_RESPONSE_LENGTH + data_len) %
291 SERVER_NDR_PADDING_SIZE;
293 pad_len = SERVER_NDR_PADDING_SIZE - pad_len;
294 DEBUG(10, ("Padding size is: %d\n", (int)pad_len));
295 /* If we're over filling the packet, we need to make
296 * space for the padding at the end of the data. */
297 if (data_len + pad_len > max_len) {
298 data_len -= SERVER_NDR_PADDING_SIZE;
303 /* Set up the alloc hint. This should be the data left to send. */
304 u.response.alloc_hint = data_len_left;
306 /* Work out if this PDU will be the last. */
307 if (p->out_data.data_sent_length
308 + data_len >= p->out_data.rdata.length) {
309 pfc_flags |= DCERPC_PFC_FLAG_LAST;
312 /* Prepare data to be NDR encoded. */
313 u.response.stub_and_verifier =
314 data_blob_const(p->out_data.rdata.data +
315 p->out_data.data_sent_length, data_len);
317 /* Store the packet in the data stream. */
318 status = dcerpc_push_ncacn_packet(p->mem_ctx,
325 if (!NT_STATUS_IS_OK(status)) {
326 DEBUG(0, ("Failed to marshall RPC Packet.\n"));
331 DATA_BLOB empty = data_blob_null;
334 /* Set the proper length on the pdu, including padding.
335 * Only needed if an auth trailer will be appended. */
336 dcerpc_set_frag_length(&p->out_data.frag,
337 p->out_data.frag.length
339 + DCERPC_AUTH_TRAILER_LENGTH
343 size_t offset = p->out_data.frag.length;
345 if (!data_blob_realloc(p->mem_ctx,
348 DEBUG(0, ("Failed to add padding!\n"));
349 data_blob_free(&p->out_data.frag);
352 memset(&p->out_data.frag.data[offset], '\0', pad_len);
355 /* auth blob is intentionally empty,
356 * it will be appended later */
357 status = dcerpc_push_dcerpc_auth(p->out_data.frag.data,
364 if (!NT_STATUS_IS_OK(status)) {
365 DEBUG(0, ("Failed to marshall RPC Auth.\n"));
369 /* Store auth header in the data stream. */
370 if (!data_blob_append(p->mem_ctx, &p->out_data.frag,
371 auth_hdr.data, auth_hdr.length)) {
372 DEBUG(0, ("Out of memory.\n"));
373 data_blob_free(&p->out_data.frag);
376 data_blob_free(&auth_hdr);
379 /* Setup the counts for this PDU. */
380 p->out_data.data_sent_length += data_len;
381 p->out_data.current_pdu_sent = 0;
385 /*******************************************************************
386 Generate the next PDU to be returned from the data in p->rdata.
387 ********************************************************************/
389 bool create_next_pdu(pipes_struct *p)
391 enum dcerpc_AuthType auth_type =
392 map_pipe_auth_type_to_rpc_auth_type(p->auth.auth_type);
395 * If we're in the fault state, keep returning fault PDU's until
396 * the pipe gets closed. JRA.
398 if (p->fault_state) {
399 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
403 switch (p->auth.auth_level) {
404 case DCERPC_AUTH_LEVEL_NONE:
405 case DCERPC_AUTH_LEVEL_CONNECT:
406 /* This is incorrect for auth level connect. Fixme. JRA */
408 /* No authentication done. */
409 return create_next_packet(p, auth_type,
410 p->auth.auth_level, 0);
412 case DCERPC_AUTH_LEVEL_CALL:
413 case DCERPC_AUTH_LEVEL_PACKET:
414 case DCERPC_AUTH_LEVEL_INTEGRITY:
415 case DCERPC_AUTH_LEVEL_PRIVACY:
417 switch(p->auth.auth_type) {
418 case PIPE_AUTH_TYPE_NTLMSSP:
419 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
420 if (!create_next_packet(p, auth_type,
425 return add_ntlmssp_auth(p);
427 case PIPE_AUTH_TYPE_SCHANNEL:
428 if (!create_next_packet(p, auth_type,
430 NL_AUTH_SIGNATURE_SIZE)) {
433 return add_schannel_auth(p);
441 DEBUG(0, ("Invalid internal auth level %u / type %u\n",
442 (unsigned int)p->auth.auth_level,
443 (unsigned int)p->auth.auth_type));
447 /*******************************************************************
448 Process an NTLMSSP authentication response.
449 If this function succeeds, the user has been authenticated
450 and their domain, name and calling workstation stored in
452 *******************************************************************/
454 static bool pipe_ntlmssp_verify_final(pipes_struct *p, DATA_BLOB *p_resp_blob)
456 DATA_BLOB session_key, reply;
458 struct auth_ntlmssp_state *a = p->auth.a_u.auth_ntlmssp_state;
461 DEBUG(5,("pipe_ntlmssp_verify_final: pipe %s checking user details\n",
462 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
466 /* this has to be done as root in order to verify the password */
468 status = auth_ntlmssp_update(a, *p_resp_blob, &reply);
471 /* Don't generate a reply. */
472 data_blob_free(&reply);
474 if (!NT_STATUS_IS_OK(status)) {
478 /* Finally - if the pipe negotiated integrity (sign) or privacy (seal)
479 ensure the underlying NTLMSSP flags are also set. If not we should
482 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_INTEGRITY) {
483 if (!auth_ntlmssp_negotiated_sign(a)) {
484 DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet integrity requested "
485 "but client declined signing.\n",
486 get_pipe_name_from_syntax(talloc_tos(),
491 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
492 if (!auth_ntlmssp_negotiated_seal(a)) {
493 DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet privacy requested "
494 "but client declined sealing.\n",
495 get_pipe_name_from_syntax(talloc_tos(),
501 DEBUG(5, ("pipe_ntlmssp_verify_final: OK: user: %s domain: %s "
503 auth_ntlmssp_get_username(a),
504 auth_ntlmssp_get_domain(a),
505 auth_ntlmssp_get_client(a)));
507 TALLOC_FREE(p->server_info);
509 status = auth_ntlmssp_server_info(p, a, &p->server_info);
510 if (!NT_STATUS_IS_OK(status)) {
511 DEBUG(0, ("auth_ntlmssp_server_info failed to obtain the server info for authenticated user: %s\n",
516 if (p->server_info->ptok == NULL) {
517 DEBUG(1,("Error: Authmodule failed to provide nt_user_token\n"));
522 * We're an authenticated bind over smb, so the session key needs to
523 * be set to "SystemLibraryDTC". Weird, but this is what Windows
524 * does. See the RPC-SAMBA3SESSIONKEY.
527 session_key = generic_session_key();
528 if (session_key.data == NULL) {
532 ret = server_info_set_session_key(p->server_info, session_key);
534 data_blob_free(&session_key);
539 /*******************************************************************
540 This is the "stage3" NTLMSSP response after a bind request and reply.
541 *******************************************************************/
543 bool api_pipe_bind_auth3(pipes_struct *p, struct ncacn_packet *pkt)
545 struct dcerpc_auth auth_info;
548 DEBUG(5, ("api_pipe_bind_auth3: decode request. %d\n", __LINE__));
550 if (pkt->auth_length == 0) {
551 DEBUG(0, ("No auth field sent for bind request!\n"));
555 /* Ensure there's enough data for an authenticated request. */
556 if (pkt->frag_length < RPC_HEADER_LEN
557 + DCERPC_AUTH_TRAILER_LENGTH
558 + pkt->auth_length) {
559 DEBUG(0,("api_pipe_ntlmssp_auth_process: auth_len "
560 "%u is too large.\n",
561 (unsigned int)pkt->auth_length));
566 * Decode the authentication verifier response.
569 status = dcerpc_pull_dcerpc_auth(pkt,
570 &pkt->u.auth3.auth_info,
571 &auth_info, p->endian);
572 if (!NT_STATUS_IS_OK(status)) {
573 DEBUG(0, ("Failed to unmarshall dcerpc_auth.\n"));
577 /* We must NEVER look at auth_info->auth_pad_len here,
578 * as old Samba client code gets it wrong and sends it
582 if (auth_info.auth_type != DCERPC_AUTH_TYPE_NTLMSSP) {
583 DEBUG(0,("api_pipe_bind_auth3: incorrect auth type (%u).\n",
584 (unsigned int)auth_info.auth_type ));
589 * The following call actually checks the challenge/response data.
590 * for correctness against the given DOMAIN\user name.
593 if (!pipe_ntlmssp_verify_final(p, &auth_info.credentials)) {
597 p->pipe_bound = True;
603 free_pipe_ntlmssp_auth_data(&p->auth);
604 p->auth.a_u.auth_ntlmssp_state = NULL;
609 /*******************************************************************
610 Marshall a bind_nak pdu.
611 *******************************************************************/
613 static bool setup_bind_nak(pipes_struct *p, struct ncacn_packet *pkt)
616 union dcerpc_payload u;
618 /* Free any memory in the current return data buffer. */
619 data_blob_free(&p->out_data.rdata);
622 * Initialize a bind_nak header.
627 u.bind_nak.reject_reason = 0;
630 * Marshall directly into the outgoing PDU space. We
631 * must do this as we need to set to the bind response
632 * header and are never sending more than one PDU here.
635 status = dcerpc_push_ncacn_packet(p->mem_ctx,
637 DCERPC_PFC_FLAG_FIRST |
638 DCERPC_PFC_FLAG_LAST,
643 if (!NT_STATUS_IS_OK(status)) {
647 p->out_data.data_sent_length = 0;
648 p->out_data.current_pdu_sent = 0;
650 if (p->auth.auth_data_free_func) {
651 (*p->auth.auth_data_free_func)(&p->auth);
653 p->auth.auth_level = DCERPC_AUTH_LEVEL_NONE;
654 p->auth.auth_type = PIPE_AUTH_TYPE_NONE;
655 p->pipe_bound = False;
660 /*******************************************************************
661 Marshall a fault pdu.
662 *******************************************************************/
664 bool setup_fault_pdu(pipes_struct *p, NTSTATUS fault_status)
667 union dcerpc_payload u;
669 /* Free any memory in the current return data buffer. */
670 data_blob_free(&p->out_data.rdata);
673 * Initialize a fault header.
678 u.fault.status = NT_STATUS_V(fault_status);
679 u.fault._pad = data_blob_talloc_zero(p->mem_ctx, 4);
682 * Marshall directly into the outgoing PDU space. We
683 * must do this as we need to set to the bind response
684 * header and are never sending more than one PDU here.
687 status = dcerpc_push_ncacn_packet(p->mem_ctx,
689 DCERPC_PFC_FLAG_FIRST |
690 DCERPC_PFC_FLAG_LAST |
691 DCERPC_PFC_FLAG_DID_NOT_EXECUTE,
696 if (!NT_STATUS_IS_OK(status)) {
700 p->out_data.data_sent_length = 0;
701 p->out_data.current_pdu_sent = 0;
706 /*******************************************************************
707 Ensure a bind request has the correct abstract & transfer interface.
708 Used to reject unknown binds from Win2k.
709 *******************************************************************/
711 static bool check_bind_req(struct pipes_struct *p,
712 struct ndr_syntax_id* abstract,
713 struct ndr_syntax_id* transfer,
716 struct pipe_rpc_fns *context_fns;
718 DEBUG(3,("check_bind_req for %s\n",
719 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
721 /* we have to check all now since win2k introduced a new UUID on the lsaprpc pipe */
722 if (rpc_srv_pipe_exists_by_id(abstract) &&
723 ndr_syntax_id_equal(transfer, &ndr_transfer_syntax)) {
724 DEBUG(3, ("check_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
725 rpc_srv_get_pipe_cli_name(abstract),
726 rpc_srv_get_pipe_srv_name(abstract)));
731 context_fns = SMB_MALLOC_P(struct pipe_rpc_fns);
732 if (context_fns == NULL) {
733 DEBUG(0,("check_bind_req: malloc() failed!\n"));
737 context_fns->n_cmds = rpc_srv_get_pipe_num_cmds(abstract);
738 context_fns->cmds = rpc_srv_get_pipe_cmds(abstract);
739 context_fns->context_id = context_id;
741 /* add to the list of open contexts */
743 DLIST_ADD( p->contexts, context_fns );
749 * Is a named pipe known?
750 * @param[in] cli_filename The pipe name requested by the client
751 * @result Do we want to serve this?
753 bool is_known_pipename(const char *cli_filename, struct ndr_syntax_id *syntax)
755 const char *pipename = cli_filename;
758 if (strnequal(pipename, "\\PIPE\\", 6)) {
762 if (*pipename == '\\') {
766 if (lp_disable_spoolss() && strequal(pipename, "spoolss")) {
767 DEBUG(10, ("refusing spoolss access\n"));
771 if (rpc_srv_get_pipe_interface_by_cli_name(pipename, syntax)) {
775 status = smb_probe_module("rpc", pipename);
776 if (!NT_STATUS_IS_OK(status)) {
777 DEBUG(10, ("is_known_pipename: %s unknown\n", cli_filename));
780 DEBUG(10, ("is_known_pipename: %s loaded dynamically\n", pipename));
783 * Scan the list again for the interface id
785 if (rpc_srv_get_pipe_interface_by_cli_name(pipename, syntax)) {
789 DEBUG(10, ("is_known_pipename: pipe %s did not register itself!\n",
795 /*******************************************************************
796 Handle a SPNEGO krb5 bind auth.
797 *******************************************************************/
799 static bool pipe_spnego_auth_bind_kerberos(pipes_struct *p,
801 struct dcerpc_auth *pauth_info,
808 /*******************************************************************
809 Handle the first part of a SPNEGO bind auth.
810 *******************************************************************/
812 static bool pipe_spnego_auth_bind_negotiate(pipes_struct *p,
814 struct dcerpc_auth *pauth_info,
819 char *OIDs[ASN1_MAX_OIDS];
822 bool got_kerberos_mechanism = false;
823 struct auth_ntlmssp_state *a = NULL;
825 ZERO_STRUCT(secblob);
828 if (pauth_info->credentials.data[0] != ASN1_APPLICATION(0)) {
832 /* parse out the OIDs and the first sec blob */
833 if (!parse_negTokenTarg(pauth_info->credentials, OIDs, &secblob)) {
834 DEBUG(0,("pipe_spnego_auth_bind_negotiate: Failed to parse the security blob.\n"));
838 if (strcmp(OID_KERBEROS5, OIDs[0]) == 0 || strcmp(OID_KERBEROS5_OLD, OIDs[0]) == 0) {
839 got_kerberos_mechanism = true;
842 for (i=0;OIDs[i];i++) {
843 DEBUG(3,("pipe_spnego_auth_bind_negotiate: Got OID %s\n", OIDs[i]));
844 TALLOC_FREE(OIDs[i]);
846 DEBUG(3,("pipe_spnego_auth_bind_negotiate: Got secblob of size %lu\n", (unsigned long)secblob.length));
848 if ( got_kerberos_mechanism && ((lp_security()==SEC_ADS) || USE_KERBEROS_KEYTAB) ) {
850 ret = pipe_spnego_auth_bind_kerberos(p, mem_ctx, pauth_info,
852 data_blob_free(&secblob);
856 if (p->auth.auth_type == PIPE_AUTH_TYPE_SPNEGO_NTLMSSP && p->auth.a_u.auth_ntlmssp_state) {
857 /* Free any previous auth type. */
858 free_pipe_ntlmssp_auth_data(&p->auth);
861 if (!got_kerberos_mechanism) {
862 /* Initialize the NTLM engine. */
863 status = auth_ntlmssp_start(&a);
864 if (!NT_STATUS_IS_OK(status)) {
868 switch (pauth_info->auth_level) {
869 case DCERPC_AUTH_LEVEL_INTEGRITY:
870 auth_ntlmssp_want_sign(a);
872 case DCERPC_AUTH_LEVEL_PRIVACY:
873 auth_ntlmssp_want_seal(a);
879 * Pass the first security blob of data to it.
880 * This can return an error or NT_STATUS_MORE_PROCESSING_REQUIRED
881 * which means we need another packet to complete the bind.
884 status = auth_ntlmssp_update(a, secblob, &chal);
886 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
887 DEBUG(3,("pipe_spnego_auth_bind_negotiate: auth_ntlmssp_update failed.\n"));
891 /* Generate the response blob we need for step 2 of the bind. */
892 *response = spnego_gen_auth_response(&chal, status, OID_NTLMSSP);
895 * SPNEGO negotiate down to NTLMSSP. The subsequent
896 * code to process follow-up packets is not complete
899 *response = spnego_gen_auth_response(NULL,
900 NT_STATUS_MORE_PROCESSING_REQUIRED,
904 /* Make sure data is bound to the memctx, to be freed the caller */
905 talloc_steal(mem_ctx, response->data);
907 /* auth_pad_len will be handled by the caller */
909 p->auth.a_u.auth_ntlmssp_state = a;
910 p->auth.auth_data_free_func = &free_pipe_ntlmssp_auth_data;
911 p->auth.auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
913 data_blob_free(&secblob);
914 data_blob_free(&chal);
916 /* We can't set pipe_bound True yet - we need an RPC_ALTER_CONTEXT response packet... */
921 data_blob_free(&secblob);
922 data_blob_free(&chal);
924 p->auth.a_u.auth_ntlmssp_state = NULL;
929 /*******************************************************************
930 Handle the second part of a SPNEGO bind auth.
931 *******************************************************************/
933 static bool pipe_spnego_auth_bind_continue(pipes_struct *p,
935 struct dcerpc_auth *pauth_info,
939 DATA_BLOB auth_reply;
940 struct auth_ntlmssp_state *a = p->auth.a_u.auth_ntlmssp_state;
942 ZERO_STRUCT(auth_blob);
943 ZERO_STRUCT(auth_reply);
946 * NB. If we've negotiated down from krb5 to NTLMSSP we'll currently
947 * fail here as 'a' == NULL.
949 if (p->auth.auth_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP || !a) {
950 DEBUG(0,("pipe_spnego_auth_bind_continue: not in NTLMSSP auth state.\n"));
954 if (pauth_info->credentials.data[0] != ASN1_CONTEXT(1)) {
955 DEBUG(0,("pipe_spnego_auth_bind_continue: invalid SPNEGO blob type.\n"));
959 if (!spnego_parse_auth(pauth_info->credentials, &auth_blob)) {
960 DEBUG(0,("pipe_spnego_auth_bind_continue: invalid SPNEGO blob.\n"));
965 * The following call actually checks the challenge/response data.
966 * for correctness against the given DOMAIN\user name.
969 if (!pipe_ntlmssp_verify_final(p, &auth_blob)) {
973 data_blob_free(&auth_blob);
975 /* Generate the spnego "accept completed" blob - no incoming data. */
976 *response = spnego_gen_auth_response(&auth_reply, NT_STATUS_OK, OID_NTLMSSP);
978 /* Make sure data is bound to the memctx, to be freed the caller */
979 talloc_steal(mem_ctx, response->data);
981 data_blob_free(&auth_reply);
983 p->pipe_bound = True;
989 data_blob_free(&auth_blob);
990 data_blob_free(&auth_reply);
992 free_pipe_ntlmssp_auth_data(&p->auth);
993 p->auth.a_u.auth_ntlmssp_state = NULL;
998 /*******************************************************************
999 Handle an schannel bind auth.
1000 *******************************************************************/
1002 static bool pipe_schannel_auth_bind(pipes_struct *p,
1003 TALLOC_CTX *mem_ctx,
1004 struct dcerpc_auth *auth_info,
1005 DATA_BLOB *response)
1007 struct NL_AUTH_MESSAGE neg;
1008 struct NL_AUTH_MESSAGE reply;
1011 struct netlogon_creds_CredentialState *creds;
1012 DATA_BLOB session_key;
1013 enum ndr_err_code ndr_err;
1015 ndr_err = ndr_pull_struct_blob(
1016 &auth_info->credentials, mem_ctx, &neg,
1017 (ndr_pull_flags_fn_t)ndr_pull_NL_AUTH_MESSAGE);
1018 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1019 DEBUG(0,("pipe_schannel_auth_bind: Could not unmarshal SCHANNEL auth neg\n"));
1023 if (DEBUGLEVEL >= 10) {
1024 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &neg);
1027 if (!(neg.Flags & NL_FLAG_OEM_NETBIOS_COMPUTER_NAME)) {
1028 DEBUG(0,("pipe_schannel_auth_bind: Did not receive netbios computer name\n"));
1033 * The neg.oem_netbios_computer.a key here must match the remote computer name
1034 * given in the DOM_CLNT_SRV.uni_comp_name used on all netlogon pipe
1035 * operations that use credentials.
1039 status = schannel_get_creds_state(p, lp_private_dir(),
1040 neg.oem_netbios_computer.a, &creds);
1043 if (!NT_STATUS_IS_OK(status)) {
1044 DEBUG(0, ("pipe_schannel_auth_bind: Attempt to bind using schannel without successful serverauth2\n"));
1048 p->auth.a_u.schannel_auth = talloc(p, struct schannel_state);
1049 if (!p->auth.a_u.schannel_auth) {
1054 p->auth.a_u.schannel_auth->state = SCHANNEL_STATE_START;
1055 p->auth.a_u.schannel_auth->seq_num = 0;
1056 p->auth.a_u.schannel_auth->initiator = false;
1057 p->auth.a_u.schannel_auth->creds = creds;
1060 * JRA. Should we also copy the schannel session key into the pipe session key p->session_key
1061 * here ? We do that for NTLMSSP, but the session key is already set up from the vuser
1062 * struct of the person who opened the pipe. I need to test this further. JRA.
1064 * VL. As we are mapping this to guest set the generic key
1065 * "SystemLibraryDTC" key here. It's a bit difficult to test against
1066 * W2k3, as it does not allow schannel binds against SAMR and LSA
1070 session_key = generic_session_key();
1071 if (session_key.data == NULL) {
1072 DEBUG(0, ("pipe_schannel_auth_bind: Could not alloc session"
1077 ret = server_info_set_session_key(p->server_info, session_key);
1079 data_blob_free(&session_key);
1082 DEBUG(0, ("server_info_set_session_key failed\n"));
1086 /*** SCHANNEL verifier ***/
1088 reply.MessageType = NL_NEGOTIATE_RESPONSE;
1090 reply.Buffer.dummy = 5; /* ??? actually I don't think
1091 * this has any meaning
1094 ndr_err = ndr_push_struct_blob(response, mem_ctx, &reply,
1095 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
1096 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1097 DEBUG(0,("Failed to marshall NL_AUTH_MESSAGE.\n"));
1101 if (DEBUGLEVEL >= 10) {
1102 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &reply);
1105 DEBUG(10,("pipe_schannel_auth_bind: schannel auth: domain [%s] myname [%s]\n",
1106 neg.oem_netbios_domain.a, neg.oem_netbios_computer.a));
1108 /* We're finished with this bind - no more packets. */
1109 p->auth.auth_data_free_func = NULL;
1110 p->auth.auth_type = PIPE_AUTH_TYPE_SCHANNEL;
1112 p->pipe_bound = True;
1117 /*******************************************************************
1118 Handle an NTLMSSP bind auth.
1119 *******************************************************************/
1121 static bool pipe_ntlmssp_auth_bind(pipes_struct *p,
1122 TALLOC_CTX *mem_ctx,
1123 struct dcerpc_auth *auth_info,
1124 DATA_BLOB *response)
1127 struct auth_ntlmssp_state *a = NULL;
1129 if (strncmp((char *)auth_info->credentials.data, "NTLMSSP", 7) != 0) {
1130 DEBUG(0, ("Failed to read NTLMSSP in blob\n"));
1134 /* We have an NTLMSSP blob. */
1135 status = auth_ntlmssp_start(&a);
1136 if (!NT_STATUS_IS_OK(status)) {
1137 DEBUG(0,("pipe_ntlmssp_auth_bind: auth_ntlmssp_start failed: %s\n",
1138 nt_errstr(status) ));
1142 switch (auth_info->auth_level) {
1143 case DCERPC_AUTH_LEVEL_INTEGRITY:
1144 auth_ntlmssp_want_sign(a);
1146 case DCERPC_AUTH_LEVEL_PRIVACY:
1147 auth_ntlmssp_want_seal(a);
1153 status = auth_ntlmssp_update(a, auth_info->credentials, response);
1154 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1155 DEBUG(0,("pipe_ntlmssp_auth_bind: auth_ntlmssp_update failed: %s\n",
1156 nt_errstr(status) ));
1160 /* Make sure data is bound to the memctx, to be freed the caller */
1161 talloc_steal(mem_ctx, response->data);
1163 p->auth.a_u.auth_ntlmssp_state = a;
1164 p->auth.auth_data_free_func = &free_pipe_ntlmssp_auth_data;
1165 p->auth.auth_type = PIPE_AUTH_TYPE_NTLMSSP;
1167 DEBUG(10,("pipe_ntlmssp_auth_bind: NTLMSSP auth started\n"));
1169 /* We can't set pipe_bound True yet - we need an DCERPC_PKT_AUTH3 response packet... */
1174 free_pipe_ntlmssp_auth_data(&p->auth);
1175 p->auth.a_u.auth_ntlmssp_state = NULL;
1179 /*******************************************************************
1180 Respond to a pipe bind request.
1181 *******************************************************************/
1183 bool api_pipe_bind_req(pipes_struct *p, struct ncacn_packet *pkt)
1185 struct dcerpc_auth auth_info;
1187 unsigned int auth_type = DCERPC_AUTH_TYPE_NONE;
1189 struct ndr_syntax_id id;
1190 union dcerpc_payload u;
1191 struct dcerpc_ack_ctx bind_ack_ctx;
1192 DATA_BLOB auth_resp = data_blob_null;
1193 DATA_BLOB auth_blob = data_blob_null;
1195 /* No rebinds on a bound pipe - use alter context. */
1196 if (p->pipe_bound) {
1197 DEBUG(2,("api_pipe_bind_req: rejecting bind request on bound "
1199 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1200 return setup_bind_nak(p, pkt);
1203 if (pkt->u.bind.num_contexts == 0) {
1204 DEBUG(0, ("api_pipe_bind_req: no rpc contexts around\n"));
1209 * Try and find the correct pipe name to ensure
1210 * that this is a pipe name we support.
1212 id = pkt->u.bind.ctx_list[0].abstract_syntax;
1213 if (rpc_srv_pipe_exists_by_id(&id)) {
1214 DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
1215 rpc_srv_get_pipe_cli_name(&id),
1216 rpc_srv_get_pipe_srv_name(&id)));
1218 status = smb_probe_module(
1219 "rpc", get_pipe_name_from_syntax(
1221 &pkt->u.bind.ctx_list[0].abstract_syntax));
1223 if (NT_STATUS_IS_ERR(status)) {
1224 DEBUG(3,("api_pipe_bind_req: Unknown pipe name %s in bind request.\n",
1225 get_pipe_name_from_syntax(
1227 &pkt->u.bind.ctx_list[0].abstract_syntax)));
1229 return setup_bind_nak(p, pkt);
1232 if (rpc_srv_get_pipe_interface_by_cli_name(
1233 get_pipe_name_from_syntax(talloc_tos(),
1236 DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
1237 rpc_srv_get_pipe_cli_name(&id),
1238 rpc_srv_get_pipe_srv_name(&id)));
1240 DEBUG(0, ("module %s doesn't provide functions for "
1242 get_pipe_name_from_syntax(talloc_tos(),
1244 get_pipe_name_from_syntax(talloc_tos(),
1246 return setup_bind_nak(p, pkt);
1250 DEBUG(5,("api_pipe_bind_req: make response. %d\n", __LINE__));
1252 if (pkt->u.bind.assoc_group_id != 0) {
1253 assoc_gid = pkt->u.bind.assoc_group_id;
1259 * Create the bind response struct.
1262 /* If the requested abstract synt uuid doesn't match our client pipe,
1263 reject the bind_ack & set the transfer interface synt to all 0's,
1264 ver 0 (observed when NT5 attempts to bind to abstract interfaces
1266 Needed when adding entries to a DACL from NT5 - SK */
1268 if (check_bind_req(p,
1269 &pkt->u.bind.ctx_list[0].abstract_syntax,
1270 &pkt->u.bind.ctx_list[0].transfer_syntaxes[0],
1271 pkt->u.bind.ctx_list[0].context_id)) {
1273 bind_ack_ctx.result = 0;
1274 bind_ack_ctx.reason = 0;
1275 bind_ack_ctx.syntax = pkt->u.bind.ctx_list[0].transfer_syntaxes[0];
1277 p->pipe_bound = False;
1278 /* Rejection reason: abstract syntax not supported */
1279 bind_ack_ctx.result = DCERPC_BIND_PROVIDER_REJECT;
1280 bind_ack_ctx.reason = DCERPC_BIND_REASON_ASYNTAX;
1281 bind_ack_ctx.syntax = null_ndr_syntax_id;
1285 * Check if this is an authenticated bind request.
1287 if (pkt->auth_length) {
1288 /* Quick length check. Won't catch a bad auth footer,
1289 * prevents overrun. */
1291 if (pkt->frag_length < RPC_HEADER_LEN +
1292 DCERPC_AUTH_TRAILER_LENGTH +
1294 DEBUG(0,("api_pipe_bind_req: auth_len (%u) "
1295 "too long for fragment %u.\n",
1296 (unsigned int)pkt->auth_length,
1297 (unsigned int)pkt->frag_length));
1302 * Decode the authentication verifier.
1304 status = dcerpc_pull_dcerpc_auth(pkt,
1305 &pkt->u.bind.auth_info,
1306 &auth_info, p->endian);
1307 if (!NT_STATUS_IS_OK(status)) {
1308 DEBUG(0, ("Unable to unmarshall dcerpc_auth.\n"));
1312 auth_type = auth_info.auth_type;
1314 /* Work out if we have to sign or seal etc. */
1315 switch (auth_info.auth_level) {
1316 case DCERPC_AUTH_LEVEL_INTEGRITY:
1317 p->auth.auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1319 case DCERPC_AUTH_LEVEL_PRIVACY:
1320 p->auth.auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1323 DEBUG(0, ("Unexpected auth level (%u).\n",
1324 (unsigned int)auth_info.auth_level ));
1328 switch (auth_type) {
1329 case DCERPC_AUTH_TYPE_NTLMSSP:
1330 if (!pipe_ntlmssp_auth_bind(p, pkt,
1331 &auth_info, &auth_resp)) {
1337 case DCERPC_AUTH_TYPE_SCHANNEL:
1338 if (!pipe_schannel_auth_bind(p, pkt,
1339 &auth_info, &auth_resp)) {
1344 case DCERPC_AUTH_TYPE_SPNEGO:
1345 if (!pipe_spnego_auth_bind_negotiate(p, pkt,
1346 &auth_info, &auth_resp)) {
1351 case DCERPC_AUTH_TYPE_NONE:
1355 DEBUG(0, ("Unknown auth type %x requested.\n", auth_type));
1360 if (auth_type == DCERPC_AUTH_TYPE_NONE) {
1361 /* Unauthenticated bind request. */
1362 /* We're finished - no more packets. */
1363 p->auth.auth_type = PIPE_AUTH_TYPE_NONE;
1364 /* We must set the pipe auth_level here also. */
1365 p->auth.auth_level = DCERPC_AUTH_LEVEL_NONE;
1366 p->pipe_bound = True;
1367 /* The session key was initialized from the SMB
1368 * session in make_internal_rpc_pipe_p */
1371 ZERO_STRUCT(u.bind_ack);
1372 u.bind_ack.max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
1373 u.bind_ack.max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
1374 u.bind_ack.assoc_group_id = assoc_gid;
1376 /* name has to be \PIPE\xxxxx */
1377 u.bind_ack.secondary_address =
1378 talloc_asprintf(pkt, "\\PIPE\\%s",
1379 rpc_srv_get_pipe_srv_name(&id));
1380 if (!u.bind_ack.secondary_address) {
1381 DEBUG(0, ("Out of memory!\n"));
1384 u.bind_ack.secondary_address_size =
1385 strlen(u.bind_ack.secondary_address) + 1;
1387 u.bind_ack.num_results = 1;
1388 u.bind_ack.ctx_list = &bind_ack_ctx;
1390 /* NOTE: We leave the auth_info empty so we can calculate the padding
1391 * later and then append the auth_info --simo */
1394 * Marshall directly into the outgoing PDU space. We
1395 * must do this as we need to set to the bind response
1396 * header and are never sending more than one PDU here.
1399 status = dcerpc_push_ncacn_packet(p->mem_ctx,
1400 DCERPC_PKT_BIND_ACK,
1401 DCERPC_PFC_FLAG_FIRST |
1402 DCERPC_PFC_FLAG_LAST,
1407 if (!NT_STATUS_IS_OK(status)) {
1408 DEBUG(0, ("Failed to marshall bind_ack packet. (%s)\n",
1409 nt_errstr(status)));
1412 if (auth_resp.length) {
1414 status = dcerpc_push_dcerpc_auth(pkt,
1416 auth_info.auth_level,
1418 1, /* auth_context_id */
1421 if (!NT_STATUS_IS_OK(status)) {
1422 DEBUG(0, ("Marshalling of dcerpc_auth failed.\n"));
1427 /* Now that we have the auth len store it into the right place in
1428 * the dcerpc header */
1429 dcerpc_set_frag_length(&p->out_data.frag,
1430 p->out_data.frag.length + auth_blob.length);
1432 if (auth_blob.length) {
1434 if (!data_blob_append(p->mem_ctx, &p->out_data.frag,
1435 auth_blob.data, auth_blob.length)) {
1436 DEBUG(0, ("Append of auth info failed.\n"));
1442 * Setup the lengths for the initial reply.
1445 p->out_data.data_sent_length = 0;
1446 p->out_data.current_pdu_sent = 0;
1448 TALLOC_FREE(auth_blob.data);
1453 data_blob_free(&p->out_data.frag);
1454 TALLOC_FREE(auth_blob.data);
1455 return setup_bind_nak(p, pkt);
1458 /****************************************************************************
1459 Deal with an alter context call. Can be third part of 3 leg auth request for
1461 ****************************************************************************/
1463 bool api_pipe_alter_context(pipes_struct *p, struct ncacn_packet *pkt)
1465 struct dcerpc_auth auth_info;
1468 union dcerpc_payload u;
1469 struct dcerpc_ack_ctx bind_ack_ctx;
1470 DATA_BLOB auth_resp = data_blob_null;
1471 DATA_BLOB auth_blob = data_blob_null;
1474 DEBUG(5,("api_pipe_alter_context: make response. %d\n", __LINE__));
1476 if (pkt->u.bind.assoc_group_id != 0) {
1477 assoc_gid = pkt->u.bind.assoc_group_id;
1483 * Create the bind response struct.
1486 /* If the requested abstract synt uuid doesn't match our client pipe,
1487 reject the bind_ack & set the transfer interface synt to all 0's,
1488 ver 0 (observed when NT5 attempts to bind to abstract interfaces
1490 Needed when adding entries to a DACL from NT5 - SK */
1492 if (check_bind_req(p,
1493 &pkt->u.bind.ctx_list[0].abstract_syntax,
1494 &pkt->u.bind.ctx_list[0].transfer_syntaxes[0],
1495 pkt->u.bind.ctx_list[0].context_id)) {
1497 bind_ack_ctx.result = 0;
1498 bind_ack_ctx.reason = 0;
1499 bind_ack_ctx.syntax = pkt->u.bind.ctx_list[0].transfer_syntaxes[0];
1501 p->pipe_bound = False;
1502 /* Rejection reason: abstract syntax not supported */
1503 bind_ack_ctx.result = DCERPC_BIND_PROVIDER_REJECT;
1504 bind_ack_ctx.reason = DCERPC_BIND_REASON_ASYNTAX;
1505 bind_ack_ctx.syntax = null_ndr_syntax_id;
1509 * Check if this is an authenticated alter context request.
1511 if (pkt->auth_length) {
1512 /* Quick length check. Won't catch a bad auth footer,
1513 * prevents overrun. */
1515 if (pkt->frag_length < RPC_HEADER_LEN +
1516 DCERPC_AUTH_TRAILER_LENGTH +
1518 DEBUG(0,("api_pipe_alter_context: auth_len (%u) "
1519 "too long for fragment %u.\n",
1520 (unsigned int)pkt->auth_length,
1521 (unsigned int)pkt->frag_length ));
1525 status = dcerpc_pull_dcerpc_auth(pkt,
1526 &pkt->u.bind.auth_info,
1527 &auth_info, p->endian);
1528 if (!NT_STATUS_IS_OK(status)) {
1529 DEBUG(0, ("Unable to unmarshall dcerpc_auth.\n"));
1535 * Currently only the SPNEGO auth type uses the alter ctx
1536 * response in place of the NTLMSSP auth3 type.
1539 if (auth_info.auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1540 /* We can only finish if the pipe is unbound. */
1541 if (!p->pipe_bound) {
1542 if (!pipe_spnego_auth_bind_continue(p, pkt,
1543 &auth_info, &auth_resp)) {
1553 ZERO_STRUCT(u.alter_resp);
1554 u.alter_resp.max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
1555 u.alter_resp.max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
1556 u.alter_resp.assoc_group_id = assoc_gid;
1558 /* secondary address CAN be NULL
1559 * as the specs say it's ignored.
1560 * It MUST be NULL to have the spoolss working.
1562 u.alter_resp.secondary_address = "";
1563 u.alter_resp.secondary_address_size = 1;
1565 u.alter_resp.num_results = 1;
1566 u.alter_resp.ctx_list = &bind_ack_ctx;
1568 /* NOTE: We leave the auth_info empty so we can calculate the padding
1569 * later and then append the auth_info --simo */
1572 * Marshall directly into the outgoing PDU space. We
1573 * must do this as we need to set to the bind response
1574 * header and are never sending more than one PDU here.
1577 status = dcerpc_push_ncacn_packet(p->mem_ctx,
1578 DCERPC_PKT_ALTER_RESP,
1579 DCERPC_PFC_FLAG_FIRST |
1580 DCERPC_PFC_FLAG_LAST,
1585 if (!NT_STATUS_IS_OK(status)) {
1586 DEBUG(0, ("Failed to marshall bind_ack packet. (%s)\n",
1587 nt_errstr(status)));
1590 if (auth_resp.length) {
1592 /* Work out any padding needed before the auth footer. */
1593 pad_len = p->out_data.frag.length % SERVER_NDR_PADDING_SIZE;
1595 pad_len = SERVER_NDR_PADDING_SIZE - pad_len;
1596 DEBUG(10, ("auth pad_len = %u\n",
1597 (unsigned int)pad_len));
1600 status = dcerpc_push_dcerpc_auth(pkt,
1601 auth_info.auth_type,
1602 auth_info.auth_level,
1604 1, /* auth_context_id */
1607 if (!NT_STATUS_IS_OK(status)) {
1608 DEBUG(0, ("Marshalling of dcerpc_auth failed.\n"));
1613 /* Now that we have the auth len store it into the right place in
1614 * the dcerpc header */
1615 dcerpc_set_frag_length(&p->out_data.frag,
1616 p->out_data.frag.length +
1617 pad_len + auth_blob.length);
1619 if (auth_resp.length) {
1621 char pad[SERVER_NDR_PADDING_SIZE];
1622 memset(pad, '\0', SERVER_NDR_PADDING_SIZE);
1623 if (!data_blob_append(p->mem_ctx,
1626 DEBUG(0, ("api_pipe_bind_req: failed to add "
1627 "%u bytes of pad data.\n",
1628 (unsigned int)pad_len));
1633 if (!data_blob_append(p->mem_ctx, &p->out_data.frag,
1634 auth_blob.data, auth_blob.length)) {
1635 DEBUG(0, ("Append of auth info failed.\n"));
1641 * Setup the lengths for the initial reply.
1644 p->out_data.data_sent_length = 0;
1645 p->out_data.current_pdu_sent = 0;
1647 TALLOC_FREE(auth_blob.data);
1652 data_blob_free(&p->out_data.frag);
1653 TALLOC_FREE(auth_blob.data);
1654 return setup_bind_nak(p, pkt);
1657 /****************************************************************************
1658 Find the set of RPC functions associated with this context_id
1659 ****************************************************************************/
1661 static PIPE_RPC_FNS* find_pipe_fns_by_context( PIPE_RPC_FNS *list, uint32 context_id )
1663 PIPE_RPC_FNS *fns = NULL;
1666 DEBUG(0,("find_pipe_fns_by_context: ERROR! No context list for pipe!\n"));
1670 for (fns=list; fns; fns=fns->next ) {
1671 if ( fns->context_id == context_id )
1677 /****************************************************************************
1679 ****************************************************************************/
1681 void free_pipe_rpc_context( PIPE_RPC_FNS *list )
1683 PIPE_RPC_FNS *tmp = list;
1695 static bool api_rpcTNP(pipes_struct *p, struct ncacn_packet *pkt,
1696 const struct api_struct *api_rpc_cmds, int n_cmds);
1698 /****************************************************************************
1699 Find the correct RPC function to call for this request.
1700 If the pipe is authenticated then become the correct UNIX user
1701 before doing the call.
1702 ****************************************************************************/
1704 bool api_pipe_request(pipes_struct *p, struct ncacn_packet *pkt)
1707 bool changed_user = False;
1708 PIPE_RPC_FNS *pipe_fns;
1710 if (p->pipe_bound &&
1711 ((p->auth.auth_type == PIPE_AUTH_TYPE_NTLMSSP) ||
1712 (p->auth.auth_type == PIPE_AUTH_TYPE_SPNEGO_NTLMSSP))) {
1713 if(!become_authenticated_pipe_user(p)) {
1714 data_blob_free(&p->out_data.rdata);
1717 changed_user = True;
1720 DEBUG(5, ("Requested \\PIPE\\%s\n",
1721 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1723 /* get the set of RPC functions for this context */
1725 pipe_fns = find_pipe_fns_by_context(p->contexts,
1726 pkt->u.request.context_id);
1729 TALLOC_CTX *frame = talloc_stackframe();
1730 ret = api_rpcTNP(p, pkt, pipe_fns->cmds, pipe_fns->n_cmds);
1734 DEBUG(0, ("No rpc function table associated with context "
1735 "[%d] on pipe [%s]\n",
1736 pkt->u.request.context_id,
1737 get_pipe_name_from_syntax(talloc_tos(),
1742 unbecome_authenticated_pipe_user();
1748 /*******************************************************************
1749 Calls the underlying RPC function for a named pipe.
1750 ********************************************************************/
1752 static bool api_rpcTNP(pipes_struct *p, struct ncacn_packet *pkt,
1753 const struct api_struct *api_rpc_cmds, int n_cmds)
1758 /* interpret the command */
1759 DEBUG(4,("api_rpcTNP: %s op 0x%x - ",
1760 get_pipe_name_from_syntax(talloc_tos(), &p->syntax),
1761 pkt->u.request.opnum));
1763 if (DEBUGLEVEL >= 50) {
1765 slprintf(name, sizeof(name)-1, "in_%s",
1766 get_pipe_name_from_syntax(talloc_tos(), &p->syntax));
1767 dump_pdu_region(name, pkt->u.request.opnum,
1768 &p->in_data.data, 0,
1769 p->in_data.data.length);
1772 for (fn_num = 0; fn_num < n_cmds; fn_num++) {
1773 if (api_rpc_cmds[fn_num].opnum == pkt->u.request.opnum &&
1774 api_rpc_cmds[fn_num].fn != NULL) {
1775 DEBUG(3, ("api_rpcTNP: rpc command: %s\n",
1776 api_rpc_cmds[fn_num].name));
1781 if (fn_num == n_cmds) {
1783 * For an unknown RPC just return a fault PDU but
1784 * return True to allow RPC's on the pipe to continue
1785 * and not put the pipe into fault state. JRA.
1787 DEBUG(4, ("unknown\n"));
1788 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
1792 offset1 = p->out_data.rdata.length;
1794 DEBUG(6, ("api_rpc_cmds[%d].fn == %p\n",
1795 fn_num, api_rpc_cmds[fn_num].fn));
1796 /* do the actual command */
1797 if(!api_rpc_cmds[fn_num].fn(p)) {
1798 DEBUG(0,("api_rpcTNP: %s: %s failed.\n",
1799 get_pipe_name_from_syntax(talloc_tos(), &p->syntax),
1800 api_rpc_cmds[fn_num].name));
1801 data_blob_free(&p->out_data.rdata);
1805 if (p->bad_handle_fault_state) {
1806 DEBUG(4,("api_rpcTNP: bad handle fault return.\n"));
1807 p->bad_handle_fault_state = False;
1808 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_CONTEXT_MISMATCH));
1812 if (p->rng_fault_state) {
1813 DEBUG(4, ("api_rpcTNP: rng fault return\n"));
1814 p->rng_fault_state = False;
1815 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
1819 if (DEBUGLEVEL >= 50) {
1821 slprintf(name, sizeof(name)-1, "out_%s",
1822 get_pipe_name_from_syntax(talloc_tos(), &p->syntax));
1823 dump_pdu_region(name, pkt->u.request.opnum,
1824 &p->out_data.rdata, offset1,
1825 p->out_data.rdata.length);
1828 DEBUG(5,("api_rpcTNP: called %s successfully\n",
1829 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1831 /* Check for buffer underflow in rpc parsing */
1832 if ((DEBUGLEVEL >= 10) &&
1833 (pkt->frag_length < p->in_data.data.length)) {
1834 DEBUG(10, ("api_rpcTNP: rpc input buffer underflow (parse error?)\n"));
1835 dump_data(10, p->in_data.data.data + pkt->frag_length,
1836 p->in_data.data.length - pkt->frag_length);