2 Unix SMB/CIFS implementation.
4 server side dcerpc authentication code
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Stefan (metze) Metzmacher 2004
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "rpc_server/dcerpc_server.h"
25 #include "rpc_server/dcerpc_server_proto.h"
26 #include "rpc_server/common/proto.h"
27 #include "librpc/rpc/dcerpc_proto.h"
28 #include "librpc/gen_ndr/ndr_dcerpc.h"
29 #include "auth/credentials/credentials.h"
30 #include "auth/gensec/gensec.h"
31 #include "auth/auth.h"
32 #include "param/param.h"
33 #include "librpc/rpc/rpc_common.h"
35 static NTSTATUS dcesrv_auth_negotiate_hdr_signing(struct dcesrv_call_state *call,
36 struct ncacn_packet *pkt)
38 struct dcesrv_connection *dce_conn = call->conn;
39 struct dcesrv_auth *a = NULL;
41 if (!(call->pkt.pfc_flags & DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN)) {
45 if (dce_conn->client_hdr_signing) {
46 if (dce_conn->negotiated_hdr_signing && pkt != NULL) {
47 pkt->pfc_flags |= DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN;
52 dce_conn->client_hdr_signing = true;
53 dce_conn->negotiated_hdr_signing = dce_conn->support_hdr_signing;
55 if (!dce_conn->negotiated_hdr_signing) {
60 pkt->pfc_flags |= DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN;
63 a = call->conn->default_auth_state;
64 if (a->gensec_security != NULL) {
65 gensec_want_feature(a->gensec_security,
66 GENSEC_FEATURE_SIGN_PKT_HEADER);
69 for (a = call->conn->auth_states; a != NULL; a = a->next) {
70 if (a->gensec_security == NULL) {
74 gensec_want_feature(a->gensec_security,
75 GENSEC_FEATURE_SIGN_PKT_HEADER);
81 static bool dcesrv_auth_prepare_gensec(struct dcesrv_call_state *call)
83 struct cli_credentials *server_credentials = NULL;
84 struct dcesrv_connection *dce_conn = call->conn;
85 struct dcesrv_auth *auth = call->auth_state;
88 if (auth->auth_started) {
92 auth->auth_started = true;
94 if (auth->auth_invalid) {
98 if (auth->auth_finished) {
102 if (auth->gensec_security != NULL) {
106 switch (call->in_auth_info.auth_level) {
107 case DCERPC_AUTH_LEVEL_CONNECT:
108 case DCERPC_AUTH_LEVEL_CALL:
109 case DCERPC_AUTH_LEVEL_PACKET:
110 case DCERPC_AUTH_LEVEL_INTEGRITY:
111 case DCERPC_AUTH_LEVEL_PRIVACY:
113 * We evaluate auth_type only if auth_level was valid
118 * Setting DCERPC_AUTH_LEVEL_NONE,
119 * gives the caller the reject_reason
120 * as auth_context_id.
122 * Note: DCERPC_AUTH_LEVEL_NONE == 1
124 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
125 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
126 auth->auth_context_id = DCERPC_BIND_NAK_REASON_NOT_SPECIFIED;
130 auth->auth_type = call->in_auth_info.auth_type;
131 auth->auth_level = call->in_auth_info.auth_level;
132 auth->auth_context_id = call->in_auth_info.auth_context_id;
135 = cli_credentials_init(auth);
136 if (!server_credentials) {
137 DEBUG(1, ("Failed to init server credentials\n"));
141 cli_credentials_set_conf(server_credentials, call->conn->dce_ctx->lp_ctx);
142 status = cli_credentials_set_machine_account(server_credentials, call->conn->dce_ctx->lp_ctx);
143 if (!NT_STATUS_IS_OK(status)) {
144 DEBUG(1, ("Failed to obtain server credentials: %s\n",
149 status = samba_server_gensec_start(auth,
152 call->conn->dce_ctx->lp_ctx,
155 &auth->gensec_security);
156 if (!NT_STATUS_IS_OK(status)) {
157 DEBUG(1, ("Failed to call samba_server_gensec_start %s\n",
163 * We have to call this because we set the target_service for
164 * Kerberos to NULL above, and in any case we wish to log a
165 * more specific service target.
168 status = gensec_set_target_service_description(auth->gensec_security,
170 if (!NT_STATUS_IS_OK(status)) {
171 DEBUG(1, ("Failed to call gensec_set_target_service_description %s\n",
176 if (call->conn->remote_address != NULL) {
177 status = gensec_set_remote_address(auth->gensec_security,
178 call->conn->remote_address);
179 if (!NT_STATUS_IS_OK(status)) {
180 DEBUG(1, ("Failed to call gensec_set_remote_address() %s\n",
186 if (call->conn->local_address != NULL) {
187 status = gensec_set_local_address(auth->gensec_security,
188 call->conn->local_address);
189 if (!NT_STATUS_IS_OK(status)) {
190 DEBUG(1, ("Failed to call gensec_set_local_address() %s\n",
196 status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_type,
198 if (!NT_STATUS_IS_OK(status)) {
199 const char *backend_name =
200 gensec_get_name_by_authtype(auth->gensec_security,
203 DEBUG(3, ("Failed to start GENSEC mechanism for DCERPC server: "
204 "auth_type=%d (%s), auth_level=%d: %s\n",
205 (int)auth->auth_type, backend_name,
206 (int)auth->auth_level,
210 * Setting DCERPC_AUTH_LEVEL_NONE,
211 * gives the caller the reject_reason
212 * as auth_context_id.
214 * Note: DCERPC_AUTH_LEVEL_NONE == 1
216 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
217 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
218 if (backend_name != NULL) {
219 auth->auth_context_id =
220 DCERPC_BIND_NAK_REASON_INVALID_CHECKSUM;
222 auth->auth_context_id =
223 DCERPC_BIND_NAK_REASON_INVALID_AUTH_TYPE;
228 if (dce_conn->negotiated_hdr_signing) {
229 gensec_want_feature(auth->gensec_security,
230 GENSEC_FEATURE_SIGN_PKT_HEADER);
236 static void log_successful_dcesrv_authz_event(struct dcesrv_call_state *call)
238 struct dcesrv_auth *auth = call->auth_state;
239 enum dcerpc_transport_t transport =
240 dcerpc_binding_get_transport(call->conn->endpoint->ep_description);
241 const char *auth_type = derpc_transport_string_by_transport(transport);
242 const char *transport_protection = AUTHZ_TRANSPORT_PROTECTION_NONE;
244 if (transport == NCACN_NP) {
245 transport_protection = AUTHZ_TRANSPORT_PROTECTION_SMB;
249 * Log the authorization to this RPC interface. This
250 * covered ncacn_np pass-through auth, and anonymous
251 * DCE/RPC (eg epmapper, netlogon etc)
253 log_successful_authz_event(call->conn->msg_ctx,
254 call->conn->dce_ctx->lp_ctx,
255 call->conn->remote_address,
256 call->conn->local_address,
259 transport_protection,
262 auth->auth_audited = true;
265 static void dcesrv_default_auth_state_finish_bind(struct dcesrv_call_state *call)
267 SMB_ASSERT(call->pkt.ptype == DCERPC_PKT_BIND);
269 if (call->auth_state == call->conn->default_auth_state) {
273 if (call->conn->default_auth_state->auth_started) {
277 if (call->conn->default_auth_state->auth_invalid) {
281 call->conn->default_auth_state->auth_type = DCERPC_AUTH_TYPE_NONE;
282 call->conn->default_auth_state->auth_level = DCERPC_AUTH_LEVEL_NONE;
283 call->conn->default_auth_state->auth_context_id = 0;
284 call->conn->default_auth_state->auth_started = true;
285 call->conn->default_auth_state->auth_finished = true;
289 * We defer log_successful_dcesrv_authz_event()
290 * to dcesrv_default_auth_state_prepare_request()
292 * As we don't want to trigger authz_events
293 * just for alter_context requests without authentication
297 void dcesrv_default_auth_state_prepare_request(struct dcesrv_call_state *call)
299 struct dcesrv_connection *dce_conn = call->conn;
300 struct dcesrv_auth *auth = call->auth_state;
302 if (auth->auth_audited) {
306 if (call->pkt.ptype != DCERPC_PKT_REQUEST) {
310 if (auth != dce_conn->default_auth_state) {
314 if (auth->auth_invalid) {
318 if (!auth->auth_finished) {
322 log_successful_dcesrv_authz_event(call);
326 parse any auth information from a dcerpc bind request
327 return false if we can't handle the auth request for some
328 reason (in which case we send a bind_nak)
330 bool dcesrv_auth_bind(struct dcesrv_call_state *call)
332 struct ncacn_packet *pkt = &call->pkt;
333 struct dcesrv_auth *auth = call->auth_state;
336 if (pkt->auth_length == 0) {
337 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
338 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
339 auth->auth_context_id = 0;
340 auth->auth_started = true;
342 log_successful_dcesrv_authz_event(call);
347 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.bind.auth_info,
350 if (!NT_STATUS_IS_OK(status)) {
352 * Setting DCERPC_AUTH_LEVEL_NONE,
353 * gives the caller the reject_reason
354 * as auth_context_id.
356 * Note: DCERPC_AUTH_LEVEL_NONE == 1
358 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
359 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
360 auth->auth_context_id =
361 DCERPC_BIND_NAK_REASON_PROTOCOL_VERSION_NOT_SUPPORTED;
365 return dcesrv_auth_prepare_gensec(call);
368 NTSTATUS dcesrv_auth_complete(struct dcesrv_call_state *call, NTSTATUS status)
370 struct dcesrv_auth *auth = call->auth_state;
371 const char *pdu = "<unknown>";
373 switch (call->pkt.ptype) {
374 case DCERPC_PKT_BIND:
377 case DCERPC_PKT_ALTER:
380 case DCERPC_PKT_AUTH3:
382 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
383 DEBUG(4, ("GENSEC not finished at at %s\n", pdu));
384 return NT_STATUS_RPC_SEC_PKG_ERROR;
388 return NT_STATUS_INTERNAL_ERROR;
391 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
395 if (!NT_STATUS_IS_OK(status)) {
396 DEBUG(4, ("GENSEC mech rejected the incoming authentication "
397 "at %s: %s\n", pdu, nt_errstr(status)));
401 status = gensec_session_info(auth->gensec_security,
403 &auth->session_info);
404 if (!NT_STATUS_IS_OK(status)) {
405 DEBUG(1, ("Failed to establish session_info: %s\n",
409 auth->auth_finished = true;
411 if (auth->auth_level == DCERPC_AUTH_LEVEL_CONNECT &&
412 !call->conn->got_explicit_auth_level_connect)
414 call->conn->default_auth_level_connect = auth;
417 if (call->pkt.ptype != DCERPC_PKT_AUTH3) {
421 if (call->out_auth_info->credentials.length != 0) {
422 DEBUG(4, ("GENSEC produced output token (len=%zu) at %s\n",
423 call->out_auth_info->credentials.length, pdu));
424 return NT_STATUS_RPC_SEC_PKG_ERROR;
431 add any auth information needed in a bind ack, and process the authentication
432 information found in the bind.
434 NTSTATUS dcesrv_auth_prepare_bind_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
436 struct dcesrv_connection *dce_conn = call->conn;
437 struct dcesrv_auth *auth = call->auth_state;
440 status = dcesrv_auth_negotiate_hdr_signing(call, pkt);
441 if (!NT_STATUS_IS_OK(status)) {
445 dce_conn->allow_alter = true;
446 dcesrv_default_auth_state_finish_bind(call);
448 if (call->pkt.auth_length == 0) {
449 auth->auth_finished = true;
453 /* We can't work without an existing gensec state */
454 if (auth->gensec_security == NULL) {
455 return NT_STATUS_INTERNAL_ERROR;
458 call->_out_auth_info = (struct dcerpc_auth) {
459 .auth_type = auth->auth_type,
460 .auth_level = auth->auth_level,
461 .auth_context_id = auth->auth_context_id,
463 call->out_auth_info = &call->_out_auth_info;
469 process the final stage of a auth request
471 bool dcesrv_auth_prepare_auth3(struct dcesrv_call_state *call)
473 struct ncacn_packet *pkt = &call->pkt;
474 struct dcesrv_auth *auth = call->auth_state;
477 if (pkt->auth_length == 0) {
481 if (auth->auth_finished) {
485 /* We can't work without an existing gensec state */
486 if (auth->gensec_security == NULL) {
490 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.auth3.auth_info,
491 &call->in_auth_info, NULL, true);
492 if (!NT_STATUS_IS_OK(status)) {
494 * Windows returns DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY
495 * instead of DCERPC_NCA_S_PROTO_ERROR.
497 call->fault_code = DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY;
501 if (call->in_auth_info.auth_type != auth->auth_type) {
505 if (call->in_auth_info.auth_level != auth->auth_level) {
509 if (call->in_auth_info.auth_context_id != auth->auth_context_id) {
513 call->_out_auth_info = (struct dcerpc_auth) {
514 .auth_type = auth->auth_type,
515 .auth_level = auth->auth_level,
516 .auth_context_id = auth->auth_context_id,
518 call->out_auth_info = &call->_out_auth_info;
524 parse any auth information from a dcerpc alter request
525 return false if we can't handle the auth request for some
526 reason (in which case we send a bind_nak (is this true for here?))
528 bool dcesrv_auth_alter(struct dcesrv_call_state *call)
530 struct ncacn_packet *pkt = &call->pkt;
531 struct dcesrv_auth *auth = call->auth_state;
534 /* on a pure interface change there is no auth blob */
535 if (pkt->auth_length == 0) {
536 if (!auth->auth_finished) {
542 if (auth->auth_finished) {
543 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
547 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.alter.auth_info,
548 &call->in_auth_info, NULL, true);
549 if (!NT_STATUS_IS_OK(status)) {
550 call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
554 if (!auth->auth_started) {
557 ok = dcesrv_auth_prepare_gensec(call);
559 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
566 if (call->in_auth_info.auth_type == DCERPC_AUTH_TYPE_NONE) {
567 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
571 if (call->in_auth_info.auth_type != auth->auth_type) {
575 if (call->in_auth_info.auth_level != auth->auth_level) {
579 if (call->in_auth_info.auth_context_id != auth->auth_context_id) {
587 add any auth information needed in a alter ack, and process the authentication
588 information found in the alter.
590 NTSTATUS dcesrv_auth_prepare_alter_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
592 struct dcesrv_auth *auth = call->auth_state;
595 status = dcesrv_auth_negotiate_hdr_signing(call, pkt);
596 if (!NT_STATUS_IS_OK(status)) {
600 /* on a pure interface change there is no auth_info structure
602 if (call->pkt.auth_length == 0) {
606 if (auth->gensec_security == NULL) {
607 return NT_STATUS_INTERNAL_ERROR;
610 call->_out_auth_info = (struct dcerpc_auth) {
611 .auth_type = auth->auth_type,
612 .auth_level = auth->auth_level,
613 .auth_context_id = auth->auth_context_id,
615 call->out_auth_info = &call->_out_auth_info;
621 check credentials on a packet
623 bool dcesrv_auth_pkt_pull(struct dcesrv_call_state *call,
624 DATA_BLOB *full_packet,
625 uint8_t required_flags,
626 uint8_t optional_flags,
627 uint8_t payload_offset,
628 DATA_BLOB *payload_and_verifier)
630 struct ncacn_packet *pkt = &call->pkt;
631 struct dcesrv_auth *auth = call->auth_state;
632 const struct dcerpc_auth tmp_auth = {
633 .auth_type = auth->auth_type,
634 .auth_level = auth->auth_level,
635 .auth_context_id = auth->auth_context_id,
639 if (!auth->auth_started) {
643 if (!auth->auth_finished) {
644 call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
648 if (auth->auth_invalid) {
652 status = dcerpc_ncacn_pull_pkt_auth(&tmp_auth,
653 auth->gensec_security,
659 payload_and_verifier,
662 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTOCOL_ERROR)) {
663 call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
666 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_UNSUPPORTED_AUTHN_LEVEL)) {
667 call->fault_code = DCERPC_NCA_S_UNSUPPORTED_AUTHN_LEVEL;
670 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_SEC_PKG_ERROR)) {
671 call->fault_code = DCERPC_FAULT_SEC_PKG_ERROR;
674 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
675 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
678 if (!NT_STATUS_IS_OK(status)) {
686 push a signed or sealed dcerpc request packet into a blob
688 bool dcesrv_auth_pkt_push(struct dcesrv_call_state *call,
689 DATA_BLOB *blob, size_t sig_size,
690 uint8_t payload_offset,
691 const DATA_BLOB *payload,
692 const struct ncacn_packet *pkt)
694 struct dcesrv_auth *auth = call->auth_state;
695 const struct dcerpc_auth tmp_auth = {
696 .auth_type = auth->auth_type,
697 .auth_level = auth->auth_level,
698 .auth_context_id = auth->auth_context_id,
702 status = dcerpc_ncacn_push_pkt_auth(&tmp_auth,
703 auth->gensec_security,
704 call, blob, sig_size,
708 return NT_STATUS_IS_OK(status);