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 "../librpc/gen_ndr/ndr_krb5pac.h"
34 #include "../libcli/auth/schannel.h"
35 #include "../libcli/auth/spnego.h"
36 #include "../libcli/auth/ntlmssp.h"
37 #include "ntlmssp_wrap.h"
38 #include "rpc_server.h"
40 #include "librpc/crypto/gse.h"
43 #define DBGC_CLASS DBGC_RPC_SRV
46 * Dump everything from the start of the end up of the provided data
47 * into a file, but only at debug level >= 50
49 static void dump_pdu_region(const char *name, int v,
50 DATA_BLOB *data, size_t start, size_t end)
56 if (DEBUGLEVEL < 50) return;
58 if (start > data->length || end > data->length || start > end) return;
60 for (i = 1; i < 100; i++) {
62 fname = talloc_asprintf(talloc_tos(),
66 fname = talloc_asprintf(talloc_tos(),
73 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0644);
74 if (fd != -1 || errno != EEXIST) break;
77 sz = write(fd, data->data + start, end - start);
79 if ((sz != end - start) || (i != 0) ) {
80 DEBUG(0, ("Error writing/closing %s: %ld!=%ld %d\n",
81 fname, (unsigned long)sz,
82 (unsigned long)end - start, i));
84 DEBUG(0,("created %s\n", fname));
90 static void free_pipe_ntlmssp_auth_data(struct pipe_auth_data *auth)
92 TALLOC_FREE(auth->a_u.auth_ntlmssp_state);
95 static void free_pipe_schannel_auth_data(struct pipe_auth_data *auth)
97 TALLOC_FREE(auth->a_u.schannel_auth);
100 static void free_pipe_gssapi_auth_data(struct pipe_auth_data *auth)
102 TALLOC_FREE(auth->a_u.gssapi_state);
105 static void free_pipe_auth_data(struct pipe_auth_data *auth)
107 if (auth->auth_data_free_func) {
108 (*auth->auth_data_free_func)(auth);
109 auth->auth_data_free_func = NULL;
113 static DATA_BLOB generic_session_key(void)
115 return data_blob("SystemLibraryDTC", 16);
118 /*******************************************************************
119 Generate the next PDU to be returned from the data.
120 ********************************************************************/
122 static NTSTATUS create_next_packet(TALLOC_CTX *mem_ctx,
123 struct pipe_auth_data *auth,
126 size_t data_sent_length,
130 union dcerpc_payload u;
139 ZERO_STRUCT(u.response);
141 /* Set up rpc packet pfc flags. */
142 if (data_sent_length == 0) {
143 pfc_flags = DCERPC_PFC_FLAG_FIRST;
148 /* Work out how much we can fit in a single PDU. */
149 data_left = rdata->length - data_sent_length;
151 /* Ensure there really is data left to send. */
153 DEBUG(0, ("No data left to send !\n"));
154 return NT_STATUS_BUFFER_TOO_SMALL;
157 status = dcerpc_guess_sizes(auth,
158 DCERPC_RESPONSE_LENGTH,
160 RPC_MAX_PDU_FRAG_LEN,
161 SERVER_NDR_PADDING_SIZE,
162 &data_to_send, &frag_len,
163 &auth_len, &pad_len);
164 if (!NT_STATUS_IS_OK(status)) {
168 /* Set up the alloc hint. This should be the data left to send. */
169 u.response.alloc_hint = data_left;
171 /* Work out if this PDU will be the last. */
172 if (data_sent_length + data_to_send >= rdata->length) {
173 pfc_flags |= DCERPC_PFC_FLAG_LAST;
176 /* Prepare data to be NDR encoded. */
177 u.response.stub_and_verifier =
178 data_blob_const(rdata->data + data_sent_length, data_to_send);
180 /* Store the packet in the data stream. */
181 status = dcerpc_push_ncacn_packet(mem_ctx, DCERPC_PKT_RESPONSE,
182 pfc_flags, auth_len, call_id,
184 if (!NT_STATUS_IS_OK(status)) {
185 DEBUG(0, ("Failed to marshall RPC Packet.\n"));
190 /* Set the proper length on the pdu, including padding.
191 * Only needed if an auth trailer will be appended. */
192 dcerpc_set_frag_length(frag, frag->length
194 + DCERPC_AUTH_TRAILER_LENGTH
199 status = dcerpc_add_auth_footer(auth, pad_len, frag);
200 if (!NT_STATUS_IS_OK(status)) {
201 data_blob_free(frag);
206 *pdu_size = data_to_send;
210 /*******************************************************************
211 Generate the next PDU to be returned from the data in p->rdata.
212 ********************************************************************/
214 bool create_next_pdu(struct pipes_struct *p)
220 * If we're in the fault state, keep returning fault PDU's until
221 * the pipe gets closed. JRA.
223 if (p->fault_state) {
224 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
228 status = create_next_packet(p->mem_ctx, &p->auth,
229 p->call_id, &p->out_data.rdata,
230 p->out_data.data_sent_length,
231 &p->out_data.frag, &pdu_size);
232 if (!NT_STATUS_IS_OK(status)) {
233 DEBUG(0, ("Failed to create packet with error %s, "
234 "(auth level %u / type %u)\n",
236 (unsigned int)p->auth.auth_level,
237 (unsigned int)p->auth.auth_type));
241 /* Setup the counts for this PDU. */
242 p->out_data.data_sent_length += pdu_size;
243 p->out_data.current_pdu_sent = 0;
247 /*******************************************************************
248 Process an NTLMSSP authentication response.
249 If this function succeeds, the user has been authenticated
250 and their domain, name and calling workstation stored in
252 *******************************************************************/
254 static bool pipe_ntlmssp_verify_final(struct pipes_struct *p,
255 DATA_BLOB *p_resp_blob)
257 DATA_BLOB session_key, reply;
259 struct auth_ntlmssp_state *a = p->auth.a_u.auth_ntlmssp_state;
262 DEBUG(5,("pipe_ntlmssp_verify_final: pipe %s checking user details\n",
263 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
267 /* this has to be done as root in order to verify the password */
269 status = auth_ntlmssp_update(a, *p_resp_blob, &reply);
272 /* Don't generate a reply. */
273 data_blob_free(&reply);
275 if (!NT_STATUS_IS_OK(status)) {
279 /* Finally - if the pipe negotiated integrity (sign) or privacy (seal)
280 ensure the underlying NTLMSSP flags are also set. If not we should
283 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_INTEGRITY) {
284 if (!auth_ntlmssp_negotiated_sign(a)) {
285 DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet integrity requested "
286 "but client declined signing.\n",
287 get_pipe_name_from_syntax(talloc_tos(),
292 if (p->auth.auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
293 if (!auth_ntlmssp_negotiated_seal(a)) {
294 DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet privacy requested "
295 "but client declined sealing.\n",
296 get_pipe_name_from_syntax(talloc_tos(),
302 DEBUG(5, ("pipe_ntlmssp_verify_final: OK: user: %s domain: %s "
304 auth_ntlmssp_get_username(a),
305 auth_ntlmssp_get_domain(a),
306 auth_ntlmssp_get_client(a)));
308 TALLOC_FREE(p->server_info);
310 status = auth_ntlmssp_steal_server_info(p, a, &p->server_info);
311 if (!NT_STATUS_IS_OK(status)) {
312 DEBUG(0, ("auth_ntlmssp_server_info failed to obtain the server info for authenticated user: %s\n",
317 if (p->server_info->ptok == NULL) {
318 DEBUG(1,("Error: Authmodule failed to provide nt_user_token\n"));
323 * We're an authenticated bind over smb, so the session key needs to
324 * be set to "SystemLibraryDTC". Weird, but this is what Windows
325 * does. See the RPC-SAMBA3SESSIONKEY.
328 session_key = generic_session_key();
329 if (session_key.data == NULL) {
333 ret = server_info_set_session_key(p->server_info, session_key);
335 data_blob_free(&session_key);
340 static NTSTATUS pipe_gssapi_auth_bind_next(struct pipes_struct *p,
342 struct dcerpc_auth *pauth_info,
343 DATA_BLOB *response);
345 /*******************************************************************
346 This is the "stage3" response after a bind request and reply.
347 *******************************************************************/
349 bool api_pipe_bind_auth3(struct pipes_struct *p, struct ncacn_packet *pkt)
351 struct dcerpc_auth auth_info;
352 DATA_BLOB response = data_blob_null;
355 DEBUG(5, ("api_pipe_bind_auth3: decode request. %d\n", __LINE__));
357 if (pkt->auth_length == 0) {
358 DEBUG(0, ("No auth field sent for bind request!\n"));
362 /* Ensure there's enough data for an authenticated request. */
363 if (pkt->frag_length < RPC_HEADER_LEN
364 + DCERPC_AUTH_TRAILER_LENGTH
365 + pkt->auth_length) {
366 DEBUG(0,("api_pipe_ntlmssp_auth_process: auth_len "
367 "%u is too large.\n",
368 (unsigned int)pkt->auth_length));
373 * Decode the authentication verifier response.
376 status = dcerpc_pull_dcerpc_auth(pkt,
377 &pkt->u.auth3.auth_info,
378 &auth_info, p->endian);
379 if (!NT_STATUS_IS_OK(status)) {
380 DEBUG(0, ("Failed to unmarshall dcerpc_auth.\n"));
384 /* We must NEVER look at auth_info->auth_pad_len here,
385 * as old Samba client code gets it wrong and sends it
389 switch (auth_info.auth_type) {
390 case DCERPC_AUTH_TYPE_NTLMSSP:
391 if (!pipe_ntlmssp_verify_final(p, &auth_info.credentials)) {
395 case DCERPC_AUTH_TYPE_KRB5:
396 status = pipe_gssapi_auth_bind_next(p, pkt,
397 &auth_info, &response);
398 if (!NT_STATUS_IS_OK(status)) {
401 if (response.length) {
402 DEBUG(0, (__location__ ": This was supposed to be "
403 "the final leg, but gssapi machinery "
404 "claims a response is needed, "
405 "aborting auth!\n"));
406 data_blob_free(&response);
412 DEBUG(0, (__location__ ": incorrect auth type (%u).\n",
413 (unsigned int)auth_info.auth_type));
418 * The following call actually checks the challenge/response data.
419 * for correctness against the given DOMAIN\user name.
422 p->pipe_bound = true;
427 free_pipe_auth_data(&p->auth);
431 static bool pipe_init_outgoing_data(struct pipes_struct *p);
433 /*******************************************************************
434 Marshall a bind_nak pdu.
435 *******************************************************************/
437 static bool setup_bind_nak(struct pipes_struct *p, struct ncacn_packet *pkt)
440 union dcerpc_payload u;
442 /* Free any memory in the current return data buffer. */
443 pipe_init_outgoing_data(p);
446 * Initialize a bind_nak header.
451 u.bind_nak.reject_reason = 0;
454 * Marshall directly into the outgoing PDU space. We
455 * must do this as we need to set to the bind response
456 * header and are never sending more than one PDU here.
459 status = dcerpc_push_ncacn_packet(p->mem_ctx,
461 DCERPC_PFC_FLAG_FIRST |
462 DCERPC_PFC_FLAG_LAST,
467 if (!NT_STATUS_IS_OK(status)) {
471 p->out_data.data_sent_length = 0;
472 p->out_data.current_pdu_sent = 0;
474 free_pipe_auth_data(&p->auth);
475 p->auth.auth_level = DCERPC_AUTH_LEVEL_NONE;
476 p->auth.auth_type = DCERPC_AUTH_TYPE_NONE;
477 p->auth.spnego_type = PIPE_AUTH_TYPE_SPNEGO_NONE;
478 p->pipe_bound = False;
483 /*******************************************************************
484 Marshall a fault pdu.
485 *******************************************************************/
487 bool setup_fault_pdu(struct pipes_struct *p, NTSTATUS fault_status)
490 union dcerpc_payload u;
492 /* Free any memory in the current return data buffer. */
493 pipe_init_outgoing_data(p);
496 * Initialize a fault header.
501 u.fault.status = NT_STATUS_V(fault_status);
502 u.fault._pad = data_blob_talloc_zero(p->mem_ctx, 4);
505 * Marshall directly into the outgoing PDU space. We
506 * must do this as we need to set to the bind response
507 * header and are never sending more than one PDU here.
510 status = dcerpc_push_ncacn_packet(p->mem_ctx,
512 DCERPC_PFC_FLAG_FIRST |
513 DCERPC_PFC_FLAG_LAST |
514 DCERPC_PFC_FLAG_DID_NOT_EXECUTE,
519 if (!NT_STATUS_IS_OK(status)) {
523 p->out_data.data_sent_length = 0;
524 p->out_data.current_pdu_sent = 0;
529 /*******************************************************************
530 Ensure a bind request has the correct abstract & transfer interface.
531 Used to reject unknown binds from Win2k.
532 *******************************************************************/
534 static bool check_bind_req(struct pipes_struct *p,
535 struct ndr_syntax_id* abstract,
536 struct ndr_syntax_id* transfer,
539 struct pipe_rpc_fns *context_fns;
541 DEBUG(3,("check_bind_req for %s\n",
542 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
544 /* we have to check all now since win2k introduced a new UUID on the lsaprpc pipe */
545 if (rpc_srv_pipe_exists_by_id(abstract) &&
546 ndr_syntax_id_equal(transfer, &ndr_transfer_syntax)) {
547 DEBUG(3, ("check_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
548 rpc_srv_get_pipe_cli_name(abstract),
549 rpc_srv_get_pipe_srv_name(abstract)));
554 context_fns = SMB_MALLOC_P(struct pipe_rpc_fns);
555 if (context_fns == NULL) {
556 DEBUG(0,("check_bind_req: malloc() failed!\n"));
560 context_fns->next = context_fns->prev = NULL;
561 context_fns->n_cmds = rpc_srv_get_pipe_num_cmds(abstract);
562 context_fns->cmds = rpc_srv_get_pipe_cmds(abstract);
563 context_fns->context_id = context_id;
565 /* add to the list of open contexts */
567 DLIST_ADD( p->contexts, context_fns );
573 * Is a named pipe known?
574 * @param[in] cli_filename The pipe name requested by the client
575 * @result Do we want to serve this?
577 bool is_known_pipename(const char *cli_filename, struct ndr_syntax_id *syntax)
579 const char *pipename = cli_filename;
582 if (strnequal(pipename, "\\PIPE\\", 6)) {
586 if (*pipename == '\\') {
590 if (lp_disable_spoolss() && strequal(pipename, "spoolss")) {
591 DEBUG(10, ("refusing spoolss access\n"));
595 if (rpc_srv_get_pipe_interface_by_cli_name(pipename, syntax)) {
599 status = smb_probe_module("rpc", pipename);
600 if (!NT_STATUS_IS_OK(status)) {
601 DEBUG(10, ("is_known_pipename: %s unknown\n", cli_filename));
604 DEBUG(10, ("is_known_pipename: %s loaded dynamically\n", pipename));
607 * Scan the list again for the interface id
609 if (rpc_srv_get_pipe_interface_by_cli_name(pipename, syntax)) {
613 DEBUG(10, ("is_known_pipename: pipe %s did not register itself!\n",
619 /*******************************************************************
620 Handle a SPNEGO krb5 bind auth.
621 *******************************************************************/
623 static bool pipe_spnego_auth_bind_kerberos(struct pipes_struct *p,
625 struct dcerpc_auth *pauth_info,
632 /*******************************************************************
633 Handle the first part of a SPNEGO bind auth.
634 *******************************************************************/
636 static bool pipe_spnego_auth_bind_negotiate(struct pipes_struct *p,
638 struct dcerpc_auth *pauth_info,
643 char *OIDs[ASN1_MAX_OIDS];
646 bool got_kerberos_mechanism = false;
647 struct auth_ntlmssp_state *a = NULL;
649 ZERO_STRUCT(secblob);
652 if (pauth_info->credentials.data[0] != ASN1_APPLICATION(0)) {
656 /* parse out the OIDs and the first sec blob */
657 if (!spnego_parse_negTokenInit(talloc_tos(),
658 pauth_info->credentials, OIDs, NULL, &secblob)) {
659 DEBUG(0,("pipe_spnego_auth_bind_negotiate: Failed to parse the security blob.\n"));
663 if (strcmp(OID_KERBEROS5, OIDs[0]) == 0 || strcmp(OID_KERBEROS5_OLD, OIDs[0]) == 0) {
664 got_kerberos_mechanism = true;
667 for (i=0;OIDs[i];i++) {
668 DEBUG(3,("pipe_spnego_auth_bind_negotiate: Got OID %s\n", OIDs[i]));
669 TALLOC_FREE(OIDs[i]);
671 DEBUG(3,("pipe_spnego_auth_bind_negotiate: Got secblob of size %lu\n", (unsigned long)secblob.length));
673 if ( got_kerberos_mechanism && ((lp_security()==SEC_ADS) || USE_KERBEROS_KEYTAB) ) {
675 ret = pipe_spnego_auth_bind_kerberos(p, mem_ctx, pauth_info,
677 data_blob_free(&secblob);
681 /* Free any previous auth type. */
682 free_pipe_auth_data(&p->auth);
684 if (!got_kerberos_mechanism) {
685 /* Initialize the NTLM engine. */
686 status = auth_ntlmssp_start(&a);
687 if (!NT_STATUS_IS_OK(status)) {
692 * then set them according to requested Auth Level */
693 auth_ntlmssp_and_flags(a, ~(NTLMSSP_NEGOTIATE_SIGN |
694 NTLMSSP_NEGOTIATE_SEAL));
695 switch (pauth_info->auth_level) {
696 case DCERPC_AUTH_LEVEL_INTEGRITY:
697 auth_ntlmssp_or_flags(a,
698 NTLMSSP_NEGOTIATE_SIGN);
700 case DCERPC_AUTH_LEVEL_PRIVACY:
701 /* Privacy always implies both sign and seal
703 auth_ntlmssp_or_flags(a,
704 NTLMSSP_NEGOTIATE_SIGN |
705 NTLMSSP_NEGOTIATE_SEAL);
711 * Pass the first security blob of data to it.
712 * This can return an error or NT_STATUS_MORE_PROCESSING_REQUIRED
713 * which means we need another packet to complete the bind.
716 status = auth_ntlmssp_update(a, secblob, &chal);
718 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
719 DEBUG(3,("pipe_spnego_auth_bind_negotiate: auth_ntlmssp_update failed.\n"));
723 /* Generate the response blob we need for step 2 of the bind. */
724 *response = spnego_gen_auth_response(mem_ctx, &chal, status, OID_NTLMSSP);
727 * SPNEGO negotiate down to NTLMSSP. The subsequent
728 * code to process follow-up packets is not complete
731 *response = spnego_gen_auth_response(mem_ctx, NULL,
732 NT_STATUS_MORE_PROCESSING_REQUIRED,
736 /* auth_pad_len will be handled by the caller */
738 p->auth.a_u.auth_ntlmssp_state = a;
739 p->auth.auth_data_free_func = &free_pipe_ntlmssp_auth_data;
740 p->auth.auth_type = DCERPC_AUTH_TYPE_SPNEGO;
741 p->auth.spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
743 data_blob_free(&secblob);
744 data_blob_free(&chal);
746 /* We can't set pipe_bound True yet - we need an RPC_ALTER_CONTEXT response packet... */
751 data_blob_free(&secblob);
752 data_blob_free(&chal);
754 p->auth.a_u.auth_ntlmssp_state = NULL;
759 /*******************************************************************
760 Handle the second part of a SPNEGO bind auth.
761 *******************************************************************/
763 static bool pipe_spnego_auth_bind_continue(struct pipes_struct *p,
765 struct dcerpc_auth *pauth_info,
769 DATA_BLOB auth_reply;
770 struct auth_ntlmssp_state *a = p->auth.a_u.auth_ntlmssp_state;
772 ZERO_STRUCT(auth_blob);
773 ZERO_STRUCT(auth_reply);
776 * NB. If we've negotiated down from krb5 to NTLMSSP we'll currently
777 * fail here as 'a' == NULL.
779 if (p->auth.auth_type != DCERPC_AUTH_TYPE_SPNEGO ||
780 p->auth.spnego_type != PIPE_AUTH_TYPE_SPNEGO_NTLMSSP || !a) {
781 DEBUG(0,("pipe_spnego_auth_bind_continue: not in NTLMSSP auth state.\n"));
785 if (pauth_info->credentials.data[0] != ASN1_CONTEXT(1)) {
786 DEBUG(0,("pipe_spnego_auth_bind_continue: invalid SPNEGO blob type.\n"));
790 if (!spnego_parse_auth(talloc_tos(), pauth_info->credentials, &auth_blob)) {
791 DEBUG(0,("pipe_spnego_auth_bind_continue: invalid SPNEGO blob.\n"));
796 * The following call actually checks the challenge/response data.
797 * for correctness against the given DOMAIN\user name.
800 if (!pipe_ntlmssp_verify_final(p, &auth_blob)) {
804 data_blob_free(&auth_blob);
806 /* Generate the spnego "accept completed" blob - no incoming data. */
807 *response = spnego_gen_auth_response(mem_ctx, &auth_reply, NT_STATUS_OK, OID_NTLMSSP);
809 data_blob_free(&auth_reply);
811 p->pipe_bound = True;
817 data_blob_free(&auth_blob);
818 data_blob_free(&auth_reply);
820 free_pipe_auth_data(&p->auth);
825 /*******************************************************************
826 Handle an schannel bind auth.
827 *******************************************************************/
829 static bool pipe_schannel_auth_bind(struct pipes_struct *p,
831 struct dcerpc_auth *auth_info,
834 struct NL_AUTH_MESSAGE neg;
835 struct NL_AUTH_MESSAGE reply;
838 struct netlogon_creds_CredentialState *creds;
839 DATA_BLOB session_key;
840 enum ndr_err_code ndr_err;
842 ndr_err = ndr_pull_struct_blob(
843 &auth_info->credentials, mem_ctx, &neg,
844 (ndr_pull_flags_fn_t)ndr_pull_NL_AUTH_MESSAGE);
845 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
846 DEBUG(0,("pipe_schannel_auth_bind: Could not unmarshal SCHANNEL auth neg\n"));
850 if (DEBUGLEVEL >= 10) {
851 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &neg);
854 if (!(neg.Flags & NL_FLAG_OEM_NETBIOS_COMPUTER_NAME)) {
855 DEBUG(0,("pipe_schannel_auth_bind: Did not receive netbios computer name\n"));
860 * The neg.oem_netbios_computer.a key here must match the remote computer name
861 * given in the DOM_CLNT_SRV.uni_comp_name used on all netlogon pipe
862 * operations that use credentials.
866 status = schannel_get_creds_state(p, lp_private_dir(),
867 neg.oem_netbios_computer.a, &creds);
870 if (!NT_STATUS_IS_OK(status)) {
871 DEBUG(0, ("pipe_schannel_auth_bind: Attempt to bind using schannel without successful serverauth2\n"));
875 p->auth.a_u.schannel_auth = talloc(p, struct schannel_state);
876 if (!p->auth.a_u.schannel_auth) {
881 p->auth.a_u.schannel_auth->state = SCHANNEL_STATE_START;
882 p->auth.a_u.schannel_auth->seq_num = 0;
883 p->auth.a_u.schannel_auth->initiator = false;
884 p->auth.a_u.schannel_auth->creds = creds;
887 * JRA. Should we also copy the schannel session key into the pipe session key p->session_key
888 * here ? We do that for NTLMSSP, but the session key is already set up from the vuser
889 * struct of the person who opened the pipe. I need to test this further. JRA.
891 * VL. As we are mapping this to guest set the generic key
892 * "SystemLibraryDTC" key here. It's a bit difficult to test against
893 * W2k3, as it does not allow schannel binds against SAMR and LSA
897 session_key = generic_session_key();
898 if (session_key.data == NULL) {
899 DEBUG(0, ("pipe_schannel_auth_bind: Could not alloc session"
904 ret = server_info_set_session_key(p->server_info, session_key);
906 data_blob_free(&session_key);
909 DEBUG(0, ("server_info_set_session_key failed\n"));
913 /*** SCHANNEL verifier ***/
915 reply.MessageType = NL_NEGOTIATE_RESPONSE;
917 reply.Buffer.dummy = 5; /* ??? actually I don't think
918 * this has any meaning
921 ndr_err = ndr_push_struct_blob(response, mem_ctx, &reply,
922 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
923 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
924 DEBUG(0,("Failed to marshall NL_AUTH_MESSAGE.\n"));
928 if (DEBUGLEVEL >= 10) {
929 NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &reply);
932 DEBUG(10,("pipe_schannel_auth_bind: schannel auth: domain [%s] myname [%s]\n",
933 neg.oem_netbios_domain.a, neg.oem_netbios_computer.a));
935 /* We're finished with this bind - no more packets. */
936 p->auth.auth_data_free_func = &free_pipe_schannel_auth_data;
937 p->auth.auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
939 p->pipe_bound = True;
944 /*******************************************************************
945 Handle an NTLMSSP bind auth.
946 *******************************************************************/
948 static bool pipe_ntlmssp_auth_bind(struct pipes_struct *p,
950 struct dcerpc_auth *auth_info,
954 struct auth_ntlmssp_state *a = NULL;
956 if (strncmp((char *)auth_info->credentials.data, "NTLMSSP", 7) != 0) {
957 DEBUG(0, ("Failed to read NTLMSSP in blob\n"));
961 /* We have an NTLMSSP blob. */
962 status = auth_ntlmssp_start(&a);
963 if (!NT_STATUS_IS_OK(status)) {
964 DEBUG(0,("pipe_ntlmssp_auth_bind: auth_ntlmssp_start failed: %s\n",
965 nt_errstr(status) ));
969 /* Clear flags, then set them according to requested Auth Level */
970 auth_ntlmssp_and_flags(a, ~(NTLMSSP_NEGOTIATE_SIGN |
971 NTLMSSP_NEGOTIATE_SEAL));
973 switch (auth_info->auth_level) {
974 case DCERPC_AUTH_LEVEL_INTEGRITY:
975 auth_ntlmssp_or_flags(a, NTLMSSP_NEGOTIATE_SIGN);
977 case DCERPC_AUTH_LEVEL_PRIVACY:
978 /* Privacy always implies both sign and seal for ntlmssp */
979 auth_ntlmssp_or_flags(a, NTLMSSP_NEGOTIATE_SIGN |
980 NTLMSSP_NEGOTIATE_SEAL);
986 status = auth_ntlmssp_update(a, auth_info->credentials, response);
987 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
988 DEBUG(0,("pipe_ntlmssp_auth_bind: auth_ntlmssp_update failed: %s\n",
989 nt_errstr(status) ));
993 /* Make sure data is bound to the memctx, to be freed the caller */
994 talloc_steal(mem_ctx, response->data);
996 p->auth.a_u.auth_ntlmssp_state = a;
997 p->auth.auth_data_free_func = &free_pipe_ntlmssp_auth_data;
998 p->auth.auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1000 DEBUG(10,("pipe_ntlmssp_auth_bind: NTLMSSP auth started\n"));
1002 /* We can't set pipe_bound True yet - we need an DCERPC_PKT_AUTH3 response packet... */
1011 /*******************************************************************
1012 Handle a GSSAPI bind auth.
1013 *******************************************************************/
1015 static bool pipe_gssapi_auth_bind(struct pipes_struct *p,
1016 TALLOC_CTX *mem_ctx,
1017 struct dcerpc_auth *auth_info,
1018 DATA_BLOB *response)
1021 struct gse_context *gse_ctx = NULL;
1023 /* Let's init the gssapi machinery for this connection */
1024 /* passing a NULL server name means the server will try
1025 * to accept any connection regardless of the name used as
1026 * long as it can find a decryption key */
1027 /* by passing NULL, the code will attempt to set a default
1028 * keytab based on configuration options */
1029 status = gse_init_server(p,
1030 (auth_info->auth_level ==
1031 DCERPC_AUTH_LEVEL_INTEGRITY),
1032 (auth_info->auth_level ==
1033 DCERPC_AUTH_LEVEL_PRIVACY),
1037 if (!NT_STATUS_IS_OK(status)) {
1038 DEBUG(0, ("Failed to init dcerpc gssapi server (%s)\n",
1039 nt_errstr(status)));
1043 status = gse_get_server_auth_token(mem_ctx, gse_ctx,
1044 &auth_info->credentials,
1046 if (!NT_STATUS_IS_OK(status)) {
1047 DEBUG(0, ("Failed to parse initial client token (%s)\n",
1048 nt_errstr(status)));
1052 p->auth.a_u.gssapi_state = gse_ctx;
1053 p->auth.auth_data_free_func = &free_pipe_gssapi_auth_data;
1054 p->auth.auth_type = DCERPC_AUTH_TYPE_KRB5;
1056 DEBUG(10, ("KRB5 auth started\n"));
1061 TALLOC_FREE(gse_ctx);
1065 static NTSTATUS finalize_gssapi_bind(TALLOC_CTX *mem_ctx,
1066 struct gse_context *gse_ctx,
1067 struct client_address *client_id,
1068 struct auth_serversupplied_info **sinfo)
1070 TALLOC_CTX *tmp_ctx;
1071 DATA_BLOB session_key;
1072 DATA_BLOB auth_data;
1077 time_t tgs_authtime;
1078 NTTIME tgs_authtime_nttime;
1079 struct PAC_DATA *pac_data;
1080 struct PAC_LOGON_NAME *logon_name = NULL;
1081 struct PAC_LOGON_INFO *logon_info = NULL;
1082 enum ndr_err_code ndr_err;
1090 struct auth_serversupplied_info *server_info;
1092 tmp_ctx = talloc_new(mem_ctx);
1094 return NT_STATUS_NO_MEMORY;
1097 /* Finally - if the pipe negotiated integrity (sign) or privacy (seal)
1098 ensure the underlying flags are also set. If not we should
1101 status = gse_verify_server_auth_flags(gse_ctx);
1102 if (!NT_STATUS_IS_OK(status)) {
1103 DEBUG(0, ("Requested Security Layers not honored!\n"));
1107 status = gse_get_authz_data(gse_ctx, tmp_ctx, &auth_data);
1108 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
1109 /* TODO: Fetch user by principal name ? */
1110 status = NT_STATUS_ACCESS_DENIED;
1113 if (!NT_STATUS_IS_OK(status)) {
1117 bret = unwrap_pac(tmp_ctx, &auth_data, &pac);
1119 DEBUG(1, ("Failed to unwrap PAC\n"));
1120 status = NT_STATUS_ACCESS_DENIED;
1124 status = gse_get_client_name(gse_ctx, tmp_ctx, &princ_name);
1125 if (!NT_STATUS_IS_OK(status)) {
1129 status = gse_get_authtime(gse_ctx, &tgs_authtime);
1130 if (!NT_STATUS_IS_OK(status)) {
1133 unix_to_nt_time(&tgs_authtime_nttime, tgs_authtime);
1135 pac_data = talloc_zero(tmp_ctx, struct PAC_DATA);
1137 status = NT_STATUS_NO_MEMORY;
1141 ndr_err = ndr_pull_struct_blob(&pac, pac_data, pac_data,
1142 (ndr_pull_flags_fn_t)ndr_pull_PAC_DATA);
1143 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1144 DEBUG(1, ("Failed to parse the PAC for %s\n", princ_name));
1145 status = ndr_map_error2ntstatus(ndr_err);
1149 /* get logon name and logon info */
1150 for (i = 0; i < pac_data->num_buffers; i++) {
1151 struct PAC_BUFFER *data_buf = &pac_data->buffers[i];
1153 switch (data_buf->type) {
1154 case PAC_TYPE_LOGON_INFO:
1155 if (!data_buf->info) {
1158 logon_info = data_buf->info->logon_info.info;
1160 case PAC_TYPE_LOGON_NAME:
1161 logon_name = &data_buf->info->logon_name;
1168 DEBUG(1, ("Invalid PAC data, missing logon info!\n"));
1169 status = NT_STATUS_NOT_FOUND;
1173 DEBUG(1, ("Invalid PAC data, missing logon info!\n"));
1174 status = NT_STATUS_NOT_FOUND;
1179 if (tgs_authtime_nttime != logon_name->logon_time) {
1180 DEBUG(1, ("Logon time mismatch between ticket and PAC!\n"
1181 "PAC Time = %s | Ticket Time = %s\n",
1182 nt_time_string(tmp_ctx, logon_name->logon_time),
1183 nt_time_string(tmp_ctx, tgs_authtime_nttime)));
1184 status = NT_STATUS_ACCESS_DENIED;
1188 /* TODO: Should we check princ_name against account_name in
1189 * logon_name ? Are they supposed to be identical, or can an
1190 * account_name be different from the UPN ? */
1192 status = get_user_from_kerberos_info(talloc_tos(), client_id->name,
1193 princ_name, logon_info,
1194 &is_mapped, &is_guest,
1197 if (!NT_STATUS_IS_OK(status)) {
1198 DEBUG(1, ("Failed to map kerberos principal to system user "
1199 "(%s)\n", nt_errstr(status)));
1200 status = NT_STATUS_ACCESS_DENIED;
1204 /* TODO: save PAC data in netsamlogon cache ? */
1206 status = make_server_info_krb5(mem_ctx,
1207 ntuser, ntdomain, username, pw,
1208 logon_info, is_guest, &server_info);
1209 if (!NT_STATUS_IS_OK(status)) {
1210 DEBUG(1, ("Failed to map kerberos pac to server info (%s)\n",
1211 nt_errstr(status)));
1212 status = NT_STATUS_ACCESS_DENIED;
1216 if (server_info->ptok == NULL) {
1217 status = create_local_token(server_info);
1218 if (!NT_STATUS_IS_OK(status)) {
1219 DEBUG(1, ("Failed to create local user token (%s)\n",
1220 nt_errstr(status)));
1221 status = NT_STATUS_ACCESS_DENIED;
1226 /* TODO: this is what the ntlmssp code does with the session_key,
1227 * probably need to be moved to an upper level as this will not apply
1228 * to tcp/ip connections */
1231 * We're an authenticated bind over smb, so the session key needs to
1232 * be set to "SystemLibraryDTC". Weird, but this is what Windows
1233 * does. See the RPC-SAMBA3SESSIONKEY.
1236 session_key = generic_session_key();
1237 if (session_key.data == NULL) {
1238 status = NT_STATUS_ACCESS_DENIED;
1242 bret = server_info_set_session_key(server_info, session_key);
1244 status = NT_STATUS_ACCESS_DENIED;
1248 data_blob_free(&session_key);
1250 *sinfo = server_info;
1251 status = NT_STATUS_OK;
1254 TALLOC_FREE(tmp_ctx);
1258 /*******************************************************************
1259 Process a KRB5 authentication response.
1260 If this function succeeds, the user has been authenticated
1261 and their domain, name and calling workstation stored in
1263 *******************************************************************/
1265 static NTSTATUS pipe_gssapi_auth_bind_next(struct pipes_struct *p,
1266 TALLOC_CTX *mem_ctx,
1267 struct dcerpc_auth *pauth_info,
1268 DATA_BLOB *response)
1270 struct gse_context *gse_ctx = p->auth.a_u.gssapi_state;
1273 DEBUG(5, (__location__ ": pipe %s checking user details\n",
1274 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1276 status = gse_get_server_auth_token(mem_ctx, gse_ctx,
1277 &pauth_info->credentials,
1279 if (!NT_STATUS_IS_OK(status)) {
1280 data_blob_free(response);
1284 if (gse_require_more_processing(gse_ctx)) {
1285 /* ask for next leg */
1286 return NT_STATUS_OK;
1289 status = finalize_gssapi_bind(p, gse_ctx,
1290 p->client_id, &p->server_info);
1291 if (!NT_STATUS_IS_OK(status)) {
1292 DEBUG(1, ("gssapi bind failed with: %s", nt_errstr(status)));
1293 return NT_STATUS_ACCESS_DENIED;
1296 return NT_STATUS_OK;
1299 /*******************************************************************
1300 Respond to a pipe bind request.
1301 *******************************************************************/
1303 static bool api_pipe_bind_req(struct pipes_struct *p,
1304 struct ncacn_packet *pkt)
1306 struct dcerpc_auth auth_info;
1308 unsigned int auth_type = DCERPC_AUTH_TYPE_NONE;
1310 struct ndr_syntax_id id;
1311 union dcerpc_payload u;
1312 struct dcerpc_ack_ctx bind_ack_ctx;
1313 DATA_BLOB auth_resp = data_blob_null;
1314 DATA_BLOB auth_blob = data_blob_null;
1316 /* No rebinds on a bound pipe - use alter context. */
1317 if (p->pipe_bound) {
1318 DEBUG(2,("api_pipe_bind_req: rejecting bind request on bound "
1320 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1321 return setup_bind_nak(p, pkt);
1324 if (pkt->u.bind.num_contexts == 0) {
1325 DEBUG(0, ("api_pipe_bind_req: no rpc contexts around\n"));
1330 * Try and find the correct pipe name to ensure
1331 * that this is a pipe name we support.
1333 id = pkt->u.bind.ctx_list[0].abstract_syntax;
1334 if (rpc_srv_pipe_exists_by_id(&id)) {
1335 DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
1336 rpc_srv_get_pipe_cli_name(&id),
1337 rpc_srv_get_pipe_srv_name(&id)));
1339 status = smb_probe_module(
1340 "rpc", get_pipe_name_from_syntax(
1342 &pkt->u.bind.ctx_list[0].abstract_syntax));
1344 if (NT_STATUS_IS_ERR(status)) {
1345 DEBUG(3,("api_pipe_bind_req: Unknown pipe name %s in bind request.\n",
1346 get_pipe_name_from_syntax(
1348 &pkt->u.bind.ctx_list[0].abstract_syntax)));
1350 return setup_bind_nak(p, pkt);
1353 if (rpc_srv_get_pipe_interface_by_cli_name(
1354 get_pipe_name_from_syntax(talloc_tos(),
1357 DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
1358 rpc_srv_get_pipe_cli_name(&id),
1359 rpc_srv_get_pipe_srv_name(&id)));
1361 DEBUG(0, ("module %s doesn't provide functions for "
1363 get_pipe_name_from_syntax(talloc_tos(),
1365 get_pipe_name_from_syntax(talloc_tos(),
1367 return setup_bind_nak(p, pkt);
1371 DEBUG(5,("api_pipe_bind_req: make response. %d\n", __LINE__));
1373 if (pkt->u.bind.assoc_group_id != 0) {
1374 assoc_gid = pkt->u.bind.assoc_group_id;
1380 * Create the bind response struct.
1383 /* If the requested abstract synt uuid doesn't match our client pipe,
1384 reject the bind_ack & set the transfer interface synt to all 0's,
1385 ver 0 (observed when NT5 attempts to bind to abstract interfaces
1387 Needed when adding entries to a DACL from NT5 - SK */
1389 if (check_bind_req(p,
1390 &pkt->u.bind.ctx_list[0].abstract_syntax,
1391 &pkt->u.bind.ctx_list[0].transfer_syntaxes[0],
1392 pkt->u.bind.ctx_list[0].context_id)) {
1394 bind_ack_ctx.result = 0;
1395 bind_ack_ctx.reason = 0;
1396 bind_ack_ctx.syntax = pkt->u.bind.ctx_list[0].transfer_syntaxes[0];
1398 p->pipe_bound = False;
1399 /* Rejection reason: abstract syntax not supported */
1400 bind_ack_ctx.result = DCERPC_BIND_PROVIDER_REJECT;
1401 bind_ack_ctx.reason = DCERPC_BIND_REASON_ASYNTAX;
1402 bind_ack_ctx.syntax = null_ndr_syntax_id;
1406 * Check if this is an authenticated bind request.
1408 if (pkt->auth_length) {
1409 /* Quick length check. Won't catch a bad auth footer,
1410 * prevents overrun. */
1412 if (pkt->frag_length < RPC_HEADER_LEN +
1413 DCERPC_AUTH_TRAILER_LENGTH +
1415 DEBUG(0,("api_pipe_bind_req: auth_len (%u) "
1416 "too long for fragment %u.\n",
1417 (unsigned int)pkt->auth_length,
1418 (unsigned int)pkt->frag_length));
1423 * Decode the authentication verifier.
1425 status = dcerpc_pull_dcerpc_auth(pkt,
1426 &pkt->u.bind.auth_info,
1427 &auth_info, p->endian);
1428 if (!NT_STATUS_IS_OK(status)) {
1429 DEBUG(0, ("Unable to unmarshall dcerpc_auth.\n"));
1433 auth_type = auth_info.auth_type;
1435 /* Work out if we have to sign or seal etc. */
1436 switch (auth_info.auth_level) {
1437 case DCERPC_AUTH_LEVEL_INTEGRITY:
1438 p->auth.auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1440 case DCERPC_AUTH_LEVEL_PRIVACY:
1441 p->auth.auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1444 DEBUG(0, ("Unexpected auth level (%u).\n",
1445 (unsigned int)auth_info.auth_level ));
1449 switch (auth_type) {
1450 case DCERPC_AUTH_TYPE_NTLMSSP:
1451 if (!pipe_ntlmssp_auth_bind(p, pkt,
1452 &auth_info, &auth_resp)) {
1458 case DCERPC_AUTH_TYPE_SCHANNEL:
1459 if (!pipe_schannel_auth_bind(p, pkt,
1460 &auth_info, &auth_resp)) {
1465 case DCERPC_AUTH_TYPE_SPNEGO:
1466 if (!pipe_spnego_auth_bind_negotiate(p, pkt,
1467 &auth_info, &auth_resp)) {
1472 case DCERPC_AUTH_TYPE_KRB5:
1473 if (!pipe_gssapi_auth_bind(p, pkt,
1474 &auth_info, &auth_resp)) {
1479 case DCERPC_AUTH_TYPE_NONE:
1483 DEBUG(0, ("Unknown auth type %x requested.\n", auth_type));
1488 if (auth_type == DCERPC_AUTH_TYPE_NONE) {
1489 /* Unauthenticated bind request. */
1490 /* We're finished - no more packets. */
1491 p->auth.auth_type = DCERPC_AUTH_TYPE_NONE;
1492 p->auth.spnego_type = PIPE_AUTH_TYPE_SPNEGO_NONE;
1493 /* We must set the pipe auth_level here also. */
1494 p->auth.auth_level = DCERPC_AUTH_LEVEL_NONE;
1495 p->pipe_bound = True;
1496 /* The session key was initialized from the SMB
1497 * session in make_internal_rpc_pipe_p */
1500 ZERO_STRUCT(u.bind_ack);
1501 u.bind_ack.max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
1502 u.bind_ack.max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
1503 u.bind_ack.assoc_group_id = assoc_gid;
1505 /* name has to be \PIPE\xxxxx */
1506 u.bind_ack.secondary_address =
1507 talloc_asprintf(pkt, "\\PIPE\\%s",
1508 rpc_srv_get_pipe_srv_name(&id));
1509 if (!u.bind_ack.secondary_address) {
1510 DEBUG(0, ("Out of memory!\n"));
1513 u.bind_ack.secondary_address_size =
1514 strlen(u.bind_ack.secondary_address) + 1;
1516 u.bind_ack.num_results = 1;
1517 u.bind_ack.ctx_list = &bind_ack_ctx;
1519 /* NOTE: We leave the auth_info empty so we can calculate the padding
1520 * later and then append the auth_info --simo */
1523 * Marshall directly into the outgoing PDU space. We
1524 * must do this as we need to set to the bind response
1525 * header and are never sending more than one PDU here.
1528 status = dcerpc_push_ncacn_packet(p->mem_ctx,
1529 DCERPC_PKT_BIND_ACK,
1530 DCERPC_PFC_FLAG_FIRST |
1531 DCERPC_PFC_FLAG_LAST,
1536 if (!NT_STATUS_IS_OK(status)) {
1537 DEBUG(0, ("Failed to marshall bind_ack packet. (%s)\n",
1538 nt_errstr(status)));
1541 if (auth_resp.length) {
1543 status = dcerpc_push_dcerpc_auth(pkt,
1545 auth_info.auth_level,
1547 1, /* auth_context_id */
1550 if (!NT_STATUS_IS_OK(status)) {
1551 DEBUG(0, ("Marshalling of dcerpc_auth failed.\n"));
1556 /* Now that we have the auth len store it into the right place in
1557 * the dcerpc header */
1558 dcerpc_set_frag_length(&p->out_data.frag,
1559 p->out_data.frag.length + auth_blob.length);
1561 if (auth_blob.length) {
1563 if (!data_blob_append(p->mem_ctx, &p->out_data.frag,
1564 auth_blob.data, auth_blob.length)) {
1565 DEBUG(0, ("Append of auth info failed.\n"));
1571 * Setup the lengths for the initial reply.
1574 p->out_data.data_sent_length = 0;
1575 p->out_data.current_pdu_sent = 0;
1577 TALLOC_FREE(auth_blob.data);
1582 data_blob_free(&p->out_data.frag);
1583 TALLOC_FREE(auth_blob.data);
1584 return setup_bind_nak(p, pkt);
1587 /****************************************************************************
1588 Deal with an alter context call. Can be third part of 3 leg auth request for
1590 ****************************************************************************/
1592 static bool api_pipe_alter_context(struct pipes_struct *p,
1593 struct ncacn_packet *pkt)
1595 struct dcerpc_auth auth_info;
1598 union dcerpc_payload u;
1599 struct dcerpc_ack_ctx bind_ack_ctx;
1600 DATA_BLOB auth_resp = data_blob_null;
1601 DATA_BLOB auth_blob = data_blob_null;
1604 DEBUG(5,("api_pipe_alter_context: make response. %d\n", __LINE__));
1606 if (pkt->u.bind.assoc_group_id != 0) {
1607 assoc_gid = pkt->u.bind.assoc_group_id;
1613 * Create the bind response struct.
1616 /* If the requested abstract synt uuid doesn't match our client pipe,
1617 reject the bind_ack & set the transfer interface synt to all 0's,
1618 ver 0 (observed when NT5 attempts to bind to abstract interfaces
1620 Needed when adding entries to a DACL from NT5 - SK */
1622 if (check_bind_req(p,
1623 &pkt->u.bind.ctx_list[0].abstract_syntax,
1624 &pkt->u.bind.ctx_list[0].transfer_syntaxes[0],
1625 pkt->u.bind.ctx_list[0].context_id)) {
1627 bind_ack_ctx.result = 0;
1628 bind_ack_ctx.reason = 0;
1629 bind_ack_ctx.syntax = pkt->u.bind.ctx_list[0].transfer_syntaxes[0];
1631 p->pipe_bound = False;
1632 /* Rejection reason: abstract syntax not supported */
1633 bind_ack_ctx.result = DCERPC_BIND_PROVIDER_REJECT;
1634 bind_ack_ctx.reason = DCERPC_BIND_REASON_ASYNTAX;
1635 bind_ack_ctx.syntax = null_ndr_syntax_id;
1639 * Check if this is an authenticated alter context request.
1641 if (pkt->auth_length) {
1642 /* Quick length check. Won't catch a bad auth footer,
1643 * prevents overrun. */
1645 if (pkt->frag_length < RPC_HEADER_LEN +
1646 DCERPC_AUTH_TRAILER_LENGTH +
1648 DEBUG(0,("api_pipe_alter_context: auth_len (%u) "
1649 "too long for fragment %u.\n",
1650 (unsigned int)pkt->auth_length,
1651 (unsigned int)pkt->frag_length ));
1655 status = dcerpc_pull_dcerpc_auth(pkt,
1656 &pkt->u.bind.auth_info,
1657 &auth_info, p->endian);
1658 if (!NT_STATUS_IS_OK(status)) {
1659 DEBUG(0, ("Unable to unmarshall dcerpc_auth.\n"));
1663 /* We can only finish if the pipe is unbound for now */
1664 if (p->pipe_bound) {
1665 DEBUG(0, (__location__ ": Pipe already bound, "
1666 "Altering Context not yet supported!\n"));
1670 switch (auth_info.auth_type) {
1671 case DCERPC_AUTH_TYPE_SPNEGO:
1672 if (!pipe_spnego_auth_bind_continue(p, pkt,
1679 case DCERPC_AUTH_TYPE_KRB5:
1680 status = pipe_gssapi_auth_bind_next(p, pkt,
1683 if (!NT_STATUS_IS_OK(status)) {
1689 DEBUG(3, (__location__ ": Usupported auth type (%d) "
1690 "in alter-context call\n",
1691 auth_info.auth_type));
1696 ZERO_STRUCT(u.alter_resp);
1697 u.alter_resp.max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
1698 u.alter_resp.max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
1699 u.alter_resp.assoc_group_id = assoc_gid;
1701 /* secondary address CAN be NULL
1702 * as the specs say it's ignored.
1703 * It MUST be NULL to have the spoolss working.
1705 u.alter_resp.secondary_address = "";
1706 u.alter_resp.secondary_address_size = 1;
1708 u.alter_resp.num_results = 1;
1709 u.alter_resp.ctx_list = &bind_ack_ctx;
1711 /* NOTE: We leave the auth_info empty so we can calculate the padding
1712 * later and then append the auth_info --simo */
1715 * Marshall directly into the outgoing PDU space. We
1716 * must do this as we need to set to the bind response
1717 * header and are never sending more than one PDU here.
1720 status = dcerpc_push_ncacn_packet(p->mem_ctx,
1721 DCERPC_PKT_ALTER_RESP,
1722 DCERPC_PFC_FLAG_FIRST |
1723 DCERPC_PFC_FLAG_LAST,
1728 if (!NT_STATUS_IS_OK(status)) {
1729 DEBUG(0, ("Failed to marshall bind_ack packet. (%s)\n",
1730 nt_errstr(status)));
1733 if (auth_resp.length) {
1735 /* Work out any padding needed before the auth footer. */
1736 pad_len = p->out_data.frag.length % SERVER_NDR_PADDING_SIZE;
1738 pad_len = SERVER_NDR_PADDING_SIZE - pad_len;
1739 DEBUG(10, ("auth pad_len = %u\n",
1740 (unsigned int)pad_len));
1743 status = dcerpc_push_dcerpc_auth(pkt,
1744 auth_info.auth_type,
1745 auth_info.auth_level,
1747 1, /* auth_context_id */
1750 if (!NT_STATUS_IS_OK(status)) {
1751 DEBUG(0, ("Marshalling of dcerpc_auth failed.\n"));
1756 /* Now that we have the auth len store it into the right place in
1757 * the dcerpc header */
1758 dcerpc_set_frag_length(&p->out_data.frag,
1759 p->out_data.frag.length +
1760 pad_len + auth_blob.length);
1762 if (auth_resp.length) {
1764 char pad[SERVER_NDR_PADDING_SIZE];
1765 memset(pad, '\0', SERVER_NDR_PADDING_SIZE);
1766 if (!data_blob_append(p->mem_ctx,
1769 DEBUG(0, ("api_pipe_bind_req: failed to add "
1770 "%u bytes of pad data.\n",
1771 (unsigned int)pad_len));
1776 if (!data_blob_append(p->mem_ctx, &p->out_data.frag,
1777 auth_blob.data, auth_blob.length)) {
1778 DEBUG(0, ("Append of auth info failed.\n"));
1784 * Setup the lengths for the initial reply.
1787 p->out_data.data_sent_length = 0;
1788 p->out_data.current_pdu_sent = 0;
1790 TALLOC_FREE(auth_blob.data);
1795 data_blob_free(&p->out_data.frag);
1796 TALLOC_FREE(auth_blob.data);
1797 return setup_bind_nak(p, pkt);
1800 /****************************************************************************
1801 Find the set of RPC functions associated with this context_id
1802 ****************************************************************************/
1804 static PIPE_RPC_FNS* find_pipe_fns_by_context( PIPE_RPC_FNS *list, uint32 context_id )
1806 PIPE_RPC_FNS *fns = NULL;
1809 DEBUG(0,("find_pipe_fns_by_context: ERROR! No context list for pipe!\n"));
1813 for (fns=list; fns; fns=fns->next ) {
1814 if ( fns->context_id == context_id )
1820 static bool api_rpcTNP(struct pipes_struct *p, struct ncacn_packet *pkt,
1821 const struct api_struct *api_rpc_cmds, int n_cmds);
1823 /****************************************************************************
1824 Find the correct RPC function to call for this request.
1825 If the pipe is authenticated then become the correct UNIX user
1826 before doing the call.
1827 ****************************************************************************/
1829 static bool api_pipe_request(struct pipes_struct *p,
1830 struct ncacn_packet *pkt)
1833 bool changed_user = False;
1834 PIPE_RPC_FNS *pipe_fns;
1836 if (p->pipe_bound &&
1837 ((p->auth.auth_type == DCERPC_AUTH_TYPE_NTLMSSP) ||
1838 ((p->auth.auth_type == DCERPC_AUTH_TYPE_SPNEGO) &&
1839 (p->auth.spnego_type == PIPE_AUTH_TYPE_SPNEGO_NTLMSSP)))) {
1840 if(!become_authenticated_pipe_user(p)) {
1841 data_blob_free(&p->out_data.rdata);
1844 changed_user = True;
1847 DEBUG(5, ("Requested \\PIPE\\%s\n",
1848 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1850 /* get the set of RPC functions for this context */
1852 pipe_fns = find_pipe_fns_by_context(p->contexts,
1853 pkt->u.request.context_id);
1856 TALLOC_CTX *frame = talloc_stackframe();
1857 ret = api_rpcTNP(p, pkt, pipe_fns->cmds, pipe_fns->n_cmds);
1861 DEBUG(0, ("No rpc function table associated with context "
1862 "[%d] on pipe [%s]\n",
1863 pkt->u.request.context_id,
1864 get_pipe_name_from_syntax(talloc_tos(),
1869 unbecome_authenticated_pipe_user();
1875 /*******************************************************************
1876 Calls the underlying RPC function for a named pipe.
1877 ********************************************************************/
1879 static bool api_rpcTNP(struct pipes_struct *p, struct ncacn_packet *pkt,
1880 const struct api_struct *api_rpc_cmds, int n_cmds)
1885 /* interpret the command */
1886 DEBUG(4,("api_rpcTNP: %s op 0x%x - ",
1887 get_pipe_name_from_syntax(talloc_tos(), &p->syntax),
1888 pkt->u.request.opnum));
1890 if (DEBUGLEVEL >= 50) {
1892 slprintf(name, sizeof(name)-1, "in_%s",
1893 get_pipe_name_from_syntax(talloc_tos(), &p->syntax));
1894 dump_pdu_region(name, pkt->u.request.opnum,
1895 &p->in_data.data, 0,
1896 p->in_data.data.length);
1899 for (fn_num = 0; fn_num < n_cmds; fn_num++) {
1900 if (api_rpc_cmds[fn_num].opnum == pkt->u.request.opnum &&
1901 api_rpc_cmds[fn_num].fn != NULL) {
1902 DEBUG(3, ("api_rpcTNP: rpc command: %s\n",
1903 api_rpc_cmds[fn_num].name));
1908 if (fn_num == n_cmds) {
1910 * For an unknown RPC just return a fault PDU but
1911 * return True to allow RPC's on the pipe to continue
1912 * and not put the pipe into fault state. JRA.
1914 DEBUG(4, ("unknown\n"));
1915 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
1919 offset1 = p->out_data.rdata.length;
1921 DEBUG(6, ("api_rpc_cmds[%d].fn == %p\n",
1922 fn_num, api_rpc_cmds[fn_num].fn));
1923 /* do the actual command */
1924 if(!api_rpc_cmds[fn_num].fn(p)) {
1925 DEBUG(0,("api_rpcTNP: %s: %s failed.\n",
1926 get_pipe_name_from_syntax(talloc_tos(), &p->syntax),
1927 api_rpc_cmds[fn_num].name));
1928 data_blob_free(&p->out_data.rdata);
1932 if (p->bad_handle_fault_state) {
1933 DEBUG(4,("api_rpcTNP: bad handle fault return.\n"));
1934 p->bad_handle_fault_state = False;
1935 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_CONTEXT_MISMATCH));
1939 if (p->rng_fault_state) {
1940 DEBUG(4, ("api_rpcTNP: rng fault return\n"));
1941 p->rng_fault_state = False;
1942 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
1946 if (DEBUGLEVEL >= 50) {
1948 slprintf(name, sizeof(name)-1, "out_%s",
1949 get_pipe_name_from_syntax(talloc_tos(), &p->syntax));
1950 dump_pdu_region(name, pkt->u.request.opnum,
1951 &p->out_data.rdata, offset1,
1952 p->out_data.rdata.length);
1955 DEBUG(5,("api_rpcTNP: called %s successfully\n",
1956 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
1958 /* Check for buffer underflow in rpc parsing */
1959 if ((DEBUGLEVEL >= 10) &&
1960 (pkt->frag_length < p->in_data.data.length)) {
1961 DEBUG(10, ("api_rpcTNP: rpc input buffer underflow (parse error?)\n"));
1962 dump_data(10, p->in_data.data.data + pkt->frag_length,
1963 p->in_data.data.length - pkt->frag_length);
1969 /****************************************************************************
1970 Initialise an outgoing packet.
1971 ****************************************************************************/
1973 static bool pipe_init_outgoing_data(struct pipes_struct *p)
1975 output_data *o_data = &p->out_data;
1977 /* Reset the offset counters. */
1978 o_data->data_sent_length = 0;
1979 o_data->current_pdu_sent = 0;
1981 data_blob_free(&o_data->frag);
1983 /* Free any memory in the current return data buffer. */
1984 data_blob_free(&o_data->rdata);
1989 /****************************************************************************
1990 Sets the fault state on incoming packets.
1991 ****************************************************************************/
1993 void set_incoming_fault(struct pipes_struct *p)
1995 data_blob_free(&p->in_data.data);
1996 p->in_data.pdu_needed_len = 0;
1997 p->in_data.pdu.length = 0;
1998 p->fault_state = True;
1999 DEBUG(10, ("set_incoming_fault: Setting fault state on pipe %s\n",
2000 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
2003 static NTSTATUS dcesrv_auth_request(struct pipe_auth_data *auth,
2004 struct ncacn_packet *pkt,
2008 size_t hdr_size = DCERPC_REQUEST_LENGTH;
2011 DEBUG(10, ("Checking request auth.\n"));
2013 if (pkt->pfc_flags & DCERPC_PFC_FLAG_OBJECT_UUID) {
2017 /* in case of sealing this function will unseal the data in place */
2018 status = dcerpc_check_auth(auth, pkt,
2019 &pkt->u.request.stub_and_verifier,
2022 if (!NT_STATUS_IS_OK(status)) {
2027 /* remove padding and auth trailer,
2028 * this way the caller will get just the data */
2029 if (pkt->auth_length) {
2030 size_t trail_len = pad_len
2031 + DCERPC_AUTH_TRAILER_LENGTH
2033 if (pkt->u.request.stub_and_verifier.length < trail_len) {
2034 return NT_STATUS_INFO_LENGTH_MISMATCH;
2036 pkt->u.request.stub_and_verifier.length -= trail_len;
2039 return NT_STATUS_OK;
2042 /****************************************************************************
2043 Processes a request pdu. This will do auth processing if needed, and
2044 appends the data into the complete stream if the LAST flag is not set.
2045 ****************************************************************************/
2047 static bool process_request_pdu(struct pipes_struct *p, struct ncacn_packet *pkt)
2052 if (!p->pipe_bound) {
2053 DEBUG(0,("process_request_pdu: rpc request with no bind.\n"));
2054 set_incoming_fault(p);
2058 /* Store the opnum */
2059 p->opnum = pkt->u.request.opnum;
2061 status = dcesrv_auth_request(&p->auth, pkt, &p->in_data.pdu);
2062 if (!NT_STATUS_IS_OK(status)) {
2063 DEBUG(0, ("Failed to check packet auth. (%s)\n",
2064 nt_errstr(status)));
2065 set_incoming_fault(p);
2069 data = pkt->u.request.stub_and_verifier;
2072 * Check the data length doesn't go over the 15Mb limit.
2073 * increased after observing a bug in the Windows NT 4.0 SP6a
2074 * spoolsv.exe when the response to a GETPRINTERDRIVER2 RPC
2075 * will not fit in the initial buffer of size 0x1068 --jerry 22/01/2002
2078 if (p->in_data.data.length + data.length > MAX_RPC_DATA_SIZE) {
2079 DEBUG(0, ("process_request_pdu: "
2080 "rpc data buffer too large (%u) + (%u)\n",
2081 (unsigned int)p->in_data.data.length,
2082 (unsigned int)data.length));
2083 set_incoming_fault(p);
2088 * Append the data portion into the buffer and return.
2092 if (!data_blob_append(p->mem_ctx, &p->in_data.data,
2093 data.data, data.length)) {
2094 DEBUG(0, ("Unable to append data size %u "
2095 "to parse buffer of size %u.\n",
2096 (unsigned int)data.length,
2097 (unsigned int)p->in_data.data.length));
2098 set_incoming_fault(p);
2103 if (pkt->pfc_flags & DCERPC_PFC_FLAG_LAST) {
2106 * Ok - we finally have a complete RPC stream.
2107 * Call the rpc command to process it.
2111 * Process the complete data stream here.
2113 if (pipe_init_outgoing_data(p)) {
2114 ret = api_pipe_request(p, pkt);
2123 /****************************************************************************
2124 Processes a finished PDU stored in p->in_data.pdu.
2125 ****************************************************************************/
2127 void process_complete_pdu(struct pipes_struct *p)
2129 struct ncacn_packet *pkt = NULL;
2133 if(p->fault_state) {
2134 DEBUG(10,("process_complete_pdu: pipe %s in fault state.\n",
2135 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
2139 pkt = talloc(p->mem_ctx, struct ncacn_packet);
2141 DEBUG(0, ("Out of memory!\n"));
2146 * Ensure we're using the corrent endianness for both the
2147 * RPC header flags and the raw data we will be reading from.
2149 if (dcerpc_get_endian_flag(&p->in_data.pdu) & DCERPC_DREP_LE) {
2150 p->endian = RPC_LITTLE_ENDIAN;
2152 p->endian = RPC_BIG_ENDIAN;
2154 DEBUG(10, ("PDU is in %s Endian format!\n", p->endian?"Big":"Little"));
2156 status = dcerpc_pull_ncacn_packet(pkt, &p->in_data.pdu,
2158 if (!NT_STATUS_IS_OK(status)) {
2159 DEBUG(0, ("Failed to unmarshal rpc packet: %s!\n",
2160 nt_errstr(status)));
2164 /* Store the call_id */
2165 p->call_id = pkt->call_id;
2167 DEBUG(10, ("Processing packet type %d\n", (int)pkt->ptype));
2169 switch (pkt->ptype) {
2170 case DCERPC_PKT_REQUEST:
2171 reply = process_request_pdu(p, pkt);
2174 case DCERPC_PKT_PING: /* CL request - ignore... */
2175 DEBUG(0, ("process_complete_pdu: Error. "
2176 "Connectionless packet type %d received on "
2177 "pipe %s.\n", (int)pkt->ptype,
2178 get_pipe_name_from_syntax(talloc_tos(),
2182 case DCERPC_PKT_RESPONSE: /* No responses here. */
2183 DEBUG(0, ("process_complete_pdu: Error. "
2184 "DCERPC_PKT_RESPONSE received from client "
2186 get_pipe_name_from_syntax(talloc_tos(),
2190 case DCERPC_PKT_FAULT:
2191 case DCERPC_PKT_WORKING:
2192 /* CL request - reply to a ping when a call in process. */
2193 case DCERPC_PKT_NOCALL:
2194 /* CL - server reply to a ping call. */
2195 case DCERPC_PKT_REJECT:
2196 case DCERPC_PKT_ACK:
2197 case DCERPC_PKT_CL_CANCEL:
2198 case DCERPC_PKT_FACK:
2199 case DCERPC_PKT_CANCEL_ACK:
2200 DEBUG(0, ("process_complete_pdu: Error. "
2201 "Connectionless packet type %u received on "
2202 "pipe %s.\n", (unsigned int)pkt->ptype,
2203 get_pipe_name_from_syntax(talloc_tos(),
2207 case DCERPC_PKT_BIND:
2209 * We assume that a pipe bind is only in one pdu.
2211 if (pipe_init_outgoing_data(p)) {
2212 reply = api_pipe_bind_req(p, pkt);
2216 case DCERPC_PKT_BIND_ACK:
2217 case DCERPC_PKT_BIND_NAK:
2218 DEBUG(0, ("process_complete_pdu: Error. "
2219 "DCERPC_PKT_BINDACK/DCERPC_PKT_BINDNACK "
2220 "packet type %u received on pipe %s.\n",
2221 (unsigned int)pkt->ptype,
2222 get_pipe_name_from_syntax(talloc_tos(),
2227 case DCERPC_PKT_ALTER:
2229 * We assume that a pipe bind is only in one pdu.
2231 if (pipe_init_outgoing_data(p)) {
2232 reply = api_pipe_alter_context(p, pkt);
2236 case DCERPC_PKT_ALTER_RESP:
2237 DEBUG(0, ("process_complete_pdu: Error. "
2238 "DCERPC_PKT_ALTER_RESP on pipe %s: "
2239 "Should only be server -> client.\n",
2240 get_pipe_name_from_syntax(talloc_tos(),
2244 case DCERPC_PKT_AUTH3:
2246 * The third packet in an auth exchange.
2248 if (pipe_init_outgoing_data(p)) {
2249 reply = api_pipe_bind_auth3(p, pkt);
2253 case DCERPC_PKT_SHUTDOWN:
2254 DEBUG(0, ("process_complete_pdu: Error. "
2255 "DCERPC_PKT_SHUTDOWN on pipe %s: "
2256 "Should only be server -> client.\n",
2257 get_pipe_name_from_syntax(talloc_tos(),
2261 case DCERPC_PKT_CO_CANCEL:
2262 /* For now just free all client data and continue
2264 DEBUG(3,("process_complete_pdu: DCERPC_PKT_CO_CANCEL."
2265 " Abandoning rpc call.\n"));
2266 /* As we never do asynchronous RPC serving, we can
2267 * never cancel a call (as far as I know).
2268 * If we ever did we'd have to send a cancel_ack reply.
2269 * For now, just free all client data and continue
2275 /* Enable this if we're doing async rpc. */
2276 /* We must check the outstanding callid matches. */
2277 if (pipe_init_outgoing_data(p)) {
2278 /* Send a cancel_ack PDU reply. */
2279 /* We should probably check the auth-verifier here. */
2280 reply = setup_cancel_ack_reply(p, pkt);
2285 case DCERPC_PKT_ORPHANED:
2286 /* We should probably check the auth-verifier here.
2287 * For now just free all client data and continue
2289 DEBUG(3, ("process_complete_pdu: DCERPC_PKT_ORPHANED."
2290 " Abandoning rpc call.\n"));
2295 DEBUG(0, ("process_complete_pdu: "
2296 "Unknown rpc type = %u received.\n",
2297 (unsigned int)pkt->ptype));
2303 DEBUG(3,("process_complete_pdu: DCE/RPC fault sent on "
2304 "pipe %s\n", get_pipe_name_from_syntax(talloc_tos(),
2306 set_incoming_fault(p);
2307 setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
2311 * Reset the lengths. We're ready for a new pdu.
2313 TALLOC_FREE(p->in_data.pdu.data);
2314 p->in_data.pdu_needed_len = 0;
2315 p->in_data.pdu.length = 0;