gpo: Test certificate policy without NDES
[Samba.git] / librpc / rpc / dcesrv_auth.c
blob99d8e016216031eb6e233fb83eddbb2349c9e48f
1 /*
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/>.
23 #include "includes.h"
24 #include "librpc/rpc/dcesrv_core.h"
25 #include "librpc/rpc/dcesrv_core_proto.h"
26 #include "librpc/rpc/dcerpc_util.h"
27 #include "librpc/rpc/dcerpc_pkt_auth.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"
34 static NTSTATUS dcesrv_auth_negotiate_hdr_signing(struct dcesrv_call_state *call,
35 struct ncacn_packet *pkt)
37 struct dcesrv_connection *dce_conn = call->conn;
38 struct dcesrv_auth *a = NULL;
40 if (!(call->pkt.pfc_flags & DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN)) {
41 return NT_STATUS_OK;
44 if (dce_conn->client_hdr_signing) {
45 if (dce_conn->negotiated_hdr_signing && pkt != NULL) {
46 pkt->pfc_flags |= DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN;
48 return NT_STATUS_OK;
51 dce_conn->client_hdr_signing = true;
52 dce_conn->negotiated_hdr_signing = dce_conn->support_hdr_signing;
54 if (!dce_conn->negotiated_hdr_signing) {
55 return NT_STATUS_OK;
58 if (pkt != NULL) {
59 pkt->pfc_flags |= DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN;
62 a = call->conn->default_auth_state;
63 if (a->gensec_security != NULL) {
64 gensec_want_feature(a->gensec_security,
65 GENSEC_FEATURE_SIGN_PKT_HEADER);
68 for (a = call->conn->auth_states; a != NULL; a = a->next) {
69 if (a->gensec_security == NULL) {
70 continue;
73 gensec_want_feature(a->gensec_security,
74 GENSEC_FEATURE_SIGN_PKT_HEADER);
77 return NT_STATUS_OK;
80 static bool dcesrv_auth_prepare_gensec(struct dcesrv_call_state *call)
82 struct dcesrv_connection *dce_conn = call->conn;
83 struct dcesrv_auth *auth = call->auth_state;
84 struct dcesrv_context_callbacks *cb = call->conn->dce_ctx->callbacks;
85 NTSTATUS status;
87 if (auth->auth_started) {
88 return false;
91 auth->auth_started = true;
93 if (auth->auth_invalid) {
94 return false;
97 if (auth->auth_finished) {
98 return false;
101 if (auth->gensec_security != NULL) {
102 return false;
105 switch (call->in_auth_info.auth_level) {
106 case DCERPC_AUTH_LEVEL_CONNECT:
107 case DCERPC_AUTH_LEVEL_CALL:
108 case DCERPC_AUTH_LEVEL_PACKET:
109 case DCERPC_AUTH_LEVEL_INTEGRITY:
110 case DCERPC_AUTH_LEVEL_PRIVACY:
112 * We evaluate auth_type only if auth_level was valid
114 break;
115 default:
117 * Setting DCERPC_AUTH_LEVEL_NONE,
118 * gives the caller the reject_reason
119 * as auth_context_id.
121 * Note: DCERPC_AUTH_LEVEL_NONE == 1
123 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
124 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
125 auth->auth_context_id = DCERPC_BIND_NAK_REASON_NOT_SPECIFIED;
126 return false;
129 auth->auth_type = call->in_auth_info.auth_type;
130 auth->auth_level = call->in_auth_info.auth_level;
131 auth->auth_context_id = call->in_auth_info.auth_context_id;
133 cb->auth.become_root();
134 status = cb->auth.gensec_prepare(
135 auth,
136 call,
137 &auth->gensec_security,
138 cb->auth.private_data);
139 cb->auth.unbecome_root();
140 if (!NT_STATUS_IS_OK(status)) {
141 DEBUG(1, ("Failed to call samba_server_gensec_start %s\n",
142 nt_errstr(status)));
143 return false;
147 * We have to call this because we set the target_service for
148 * Kerberos to NULL above, and in any case we wish to log a
149 * more specific service target.
152 status = gensec_set_target_service_description(auth->gensec_security,
153 "DCE/RPC");
154 if (!NT_STATUS_IS_OK(status)) {
155 DEBUG(1, ("Failed to call gensec_set_target_service_description %s\n",
156 nt_errstr(status)));
157 return false;
160 if (call->conn->remote_address != NULL) {
161 status = gensec_set_remote_address(auth->gensec_security,
162 call->conn->remote_address);
163 if (!NT_STATUS_IS_OK(status)) {
164 DEBUG(1, ("Failed to call gensec_set_remote_address() %s\n",
165 nt_errstr(status)));
166 return false;
170 if (call->conn->local_address != NULL) {
171 status = gensec_set_local_address(auth->gensec_security,
172 call->conn->local_address);
173 if (!NT_STATUS_IS_OK(status)) {
174 DEBUG(1, ("Failed to call gensec_set_local_address() %s\n",
175 nt_errstr(status)));
176 return false;
180 status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_type,
181 auth->auth_level);
182 if (!NT_STATUS_IS_OK(status)) {
183 const char *backend_name =
184 gensec_get_name_by_authtype(auth->gensec_security,
185 auth->auth_type);
187 DEBUG(3, ("Failed to start GENSEC mechanism for DCERPC server: "
188 "auth_type=%d (%s), auth_level=%d: %s\n",
189 (int)auth->auth_type, backend_name,
190 (int)auth->auth_level,
191 nt_errstr(status)));
194 * Setting DCERPC_AUTH_LEVEL_NONE,
195 * gives the caller the reject_reason
196 * as auth_context_id.
198 * Note: DCERPC_AUTH_LEVEL_NONE == 1
200 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
201 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
202 if (backend_name != NULL) {
203 auth->auth_context_id =
204 DCERPC_BIND_NAK_REASON_INVALID_CHECKSUM;
205 } else {
206 auth->auth_context_id =
207 DCERPC_BIND_NAK_REASON_INVALID_AUTH_TYPE;
209 return false;
212 if (dce_conn->negotiated_hdr_signing) {
213 gensec_want_feature(auth->gensec_security,
214 GENSEC_FEATURE_SIGN_PKT_HEADER);
217 return true;
220 static void dcesrv_default_auth_state_finish_bind(struct dcesrv_call_state *call)
222 SMB_ASSERT(call->pkt.ptype == DCERPC_PKT_BIND);
224 if (call->auth_state == call->conn->default_auth_state) {
225 return;
228 if (call->conn->default_auth_state->auth_started) {
229 return;
232 if (call->conn->default_auth_state->auth_invalid) {
233 return;
236 call->conn->default_auth_state->auth_type = DCERPC_AUTH_TYPE_NONE;
237 call->conn->default_auth_state->auth_level = DCERPC_AUTH_LEVEL_NONE;
238 call->conn->default_auth_state->auth_context_id = 0;
239 call->conn->default_auth_state->auth_started = true;
240 call->conn->default_auth_state->auth_finished = true;
244 * We defer log_successful_dcesrv_authz_event()
245 * to dcesrv_default_auth_state_prepare_request()
247 * As we don't want to trigger authz_events
248 * just for alter_context requests without authentication
252 void dcesrv_default_auth_state_prepare_request(struct dcesrv_call_state *call)
254 struct dcesrv_connection *dce_conn = call->conn;
255 struct dcesrv_auth *auth = call->auth_state;
256 struct dcesrv_context_callbacks *cb = call->conn->dce_ctx->callbacks;
258 if (auth->auth_audited) {
259 return;
262 if (call->pkt.ptype != DCERPC_PKT_REQUEST) {
263 return;
266 if (auth != dce_conn->default_auth_state) {
267 return;
270 if (auth->auth_invalid) {
271 return;
274 if (!auth->auth_finished) {
275 return;
278 if (cb->log.successful_authz == NULL) {
279 return;
282 cb->log.successful_authz(call, cb->log.private_data);
286 parse any auth information from a dcerpc bind request
287 return false if we can't handle the auth request for some
288 reason (in which case we send a bind_nak)
290 bool dcesrv_auth_bind(struct dcesrv_call_state *call)
292 struct ncacn_packet *pkt = &call->pkt;
293 struct dcesrv_auth *auth = call->auth_state;
294 struct dcesrv_context_callbacks *cb = call->conn->dce_ctx->callbacks;
295 NTSTATUS status;
297 if (pkt->auth_length == 0) {
298 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
299 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
300 auth->auth_context_id = 0;
301 auth->auth_started = true;
303 if (cb->log.successful_authz != NULL) {
304 cb->log.successful_authz(call, cb->log.private_data);
307 return true;
310 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.bind.auth_info,
311 &call->in_auth_info,
312 NULL, true);
313 if (!NT_STATUS_IS_OK(status)) {
315 * Setting DCERPC_AUTH_LEVEL_NONE,
316 * gives the caller the reject_reason
317 * as auth_context_id.
319 * Note: DCERPC_AUTH_LEVEL_NONE == 1
321 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
322 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
323 auth->auth_context_id =
324 DCERPC_BIND_NAK_REASON_PROTOCOL_VERSION_NOT_SUPPORTED;
325 return false;
328 return dcesrv_auth_prepare_gensec(call);
331 NTSTATUS dcesrv_auth_complete(struct dcesrv_call_state *call, NTSTATUS status)
333 struct dcesrv_auth *auth = call->auth_state;
334 struct dcesrv_context_callbacks *cb = call->conn->dce_ctx->callbacks;
335 const char *pdu = "<unknown>";
337 switch (call->pkt.ptype) {
338 case DCERPC_PKT_BIND:
339 pdu = "BIND";
340 break;
341 case DCERPC_PKT_ALTER:
342 pdu = "ALTER";
343 break;
344 case DCERPC_PKT_AUTH3:
345 pdu = "AUTH3";
346 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
347 DEBUG(4, ("GENSEC not finished at at %s\n", pdu));
348 return NT_STATUS_RPC_SEC_PKG_ERROR;
350 break;
351 default:
352 return NT_STATUS_INTERNAL_ERROR;
355 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
356 return NT_STATUS_OK;
359 if (!NT_STATUS_IS_OK(status)) {
360 DEBUG(4, ("GENSEC mech rejected the incoming authentication "
361 "at %s: %s\n", pdu, nt_errstr(status)));
362 return status;
365 cb->auth.become_root();
366 status = gensec_session_info(auth->gensec_security,
367 auth,
368 &auth->session_info);
369 cb->auth.unbecome_root();
370 if (!NT_STATUS_IS_OK(status)) {
371 DEBUG(1, ("Failed to establish session_info: %s\n",
372 nt_errstr(status)));
373 return status;
375 auth->auth_finished = true;
377 if (auth->auth_level == DCERPC_AUTH_LEVEL_CONNECT &&
378 !call->conn->got_explicit_auth_level_connect)
380 call->conn->default_auth_level_connect = auth;
383 if (call->pkt.ptype != DCERPC_PKT_AUTH3) {
384 return NT_STATUS_OK;
387 if (call->out_auth_info->credentials.length != 0) {
388 DEBUG(4, ("GENSEC produced output token (len=%zu) at %s\n",
389 call->out_auth_info->credentials.length, pdu));
390 return NT_STATUS_RPC_SEC_PKG_ERROR;
393 return NT_STATUS_OK;
397 add any auth information needed in a bind ack, and process the authentication
398 information found in the bind.
400 NTSTATUS dcesrv_auth_prepare_bind_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
402 struct dcesrv_connection *dce_conn = call->conn;
403 struct dcesrv_auth *auth = call->auth_state;
404 NTSTATUS status;
406 status = dcesrv_auth_negotiate_hdr_signing(call, pkt);
407 if (!NT_STATUS_IS_OK(status)) {
408 return status;
411 dce_conn->allow_alter = true;
412 dcesrv_default_auth_state_finish_bind(call);
414 if (call->pkt.auth_length == 0) {
415 auth->auth_finished = true;
416 return NT_STATUS_OK;
419 /* We can't work without an existing gensec state */
420 if (auth->gensec_security == NULL) {
421 return NT_STATUS_INTERNAL_ERROR;
424 call->_out_auth_info = (struct dcerpc_auth) {
425 .auth_type = auth->auth_type,
426 .auth_level = auth->auth_level,
427 .auth_context_id = auth->auth_context_id,
429 call->out_auth_info = &call->_out_auth_info;
431 return NT_STATUS_OK;
435 process the final stage of a auth request
437 bool dcesrv_auth_prepare_auth3(struct dcesrv_call_state *call)
439 struct ncacn_packet *pkt = &call->pkt;
440 struct dcesrv_auth *auth = call->auth_state;
441 NTSTATUS status;
443 if (pkt->auth_length == 0) {
444 return false;
447 if (auth->auth_finished) {
448 return false;
451 if (auth->auth_invalid) {
452 return false;
455 /* We can't work without an existing gensec state */
456 if (auth->gensec_security == NULL) {
457 return false;
460 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.auth3.auth_info,
461 &call->in_auth_info, NULL, true);
462 if (!NT_STATUS_IS_OK(status)) {
464 * Windows returns DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY
465 * instead of DCERPC_NCA_S_PROTO_ERROR.
467 call->fault_code = DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY;
468 return false;
471 if (call->in_auth_info.auth_type != auth->auth_type) {
472 return false;
475 if (call->in_auth_info.auth_level != auth->auth_level) {
476 return false;
479 if (call->in_auth_info.auth_context_id != auth->auth_context_id) {
480 return false;
483 call->_out_auth_info = (struct dcerpc_auth) {
484 .auth_type = auth->auth_type,
485 .auth_level = auth->auth_level,
486 .auth_context_id = auth->auth_context_id,
488 call->out_auth_info = &call->_out_auth_info;
490 return true;
494 parse any auth information from a dcerpc alter request
495 return false if we can't handle the auth request for some
496 reason (in which case we send a bind_nak (is this true for here?))
498 bool dcesrv_auth_alter(struct dcesrv_call_state *call)
500 struct ncacn_packet *pkt = &call->pkt;
501 struct dcesrv_auth *auth = call->auth_state;
502 NTSTATUS status;
504 /* on a pure interface change there is no auth blob */
505 if (pkt->auth_length == 0) {
506 if (!auth->auth_finished) {
507 return false;
509 return true;
512 if (auth->auth_finished) {
513 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
514 return false;
517 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.alter.auth_info,
518 &call->in_auth_info, NULL, true);
519 if (!NT_STATUS_IS_OK(status)) {
520 call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
521 return false;
524 if (!auth->auth_started) {
525 bool ok;
527 ok = dcesrv_auth_prepare_gensec(call);
528 if (!ok) {
529 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
530 return false;
533 return true;
536 if (call->in_auth_info.auth_type == DCERPC_AUTH_TYPE_NONE) {
537 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
538 return false;
541 if (auth->auth_invalid) {
542 return false;
545 if (call->in_auth_info.auth_type != auth->auth_type) {
546 return false;
549 if (call->in_auth_info.auth_level != auth->auth_level) {
550 return false;
553 if (call->in_auth_info.auth_context_id != auth->auth_context_id) {
554 return false;
557 return true;
561 add any auth information needed in a alter ack, and process the authentication
562 information found in the alter.
564 NTSTATUS dcesrv_auth_prepare_alter_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
566 struct dcesrv_auth *auth = call->auth_state;
567 NTSTATUS status;
569 status = dcesrv_auth_negotiate_hdr_signing(call, pkt);
570 if (!NT_STATUS_IS_OK(status)) {
571 return status;
574 /* on a pure interface change there is no auth_info structure
575 setup */
576 if (call->pkt.auth_length == 0) {
577 return NT_STATUS_OK;
580 if (auth->gensec_security == NULL) {
581 return NT_STATUS_INTERNAL_ERROR;
584 call->_out_auth_info = (struct dcerpc_auth) {
585 .auth_type = auth->auth_type,
586 .auth_level = auth->auth_level,
587 .auth_context_id = auth->auth_context_id,
589 call->out_auth_info = &call->_out_auth_info;
591 return NT_STATUS_OK;
595 check credentials on a packet
597 bool dcesrv_auth_pkt_pull(struct dcesrv_call_state *call,
598 DATA_BLOB *full_packet,
599 uint8_t required_flags,
600 uint8_t optional_flags,
601 uint8_t payload_offset,
602 DATA_BLOB *payload_and_verifier)
604 struct ncacn_packet *pkt = &call->pkt;
605 struct dcesrv_auth *auth = call->auth_state;
606 const struct dcerpc_auth tmp_auth = {
607 .auth_type = auth->auth_type,
608 .auth_level = auth->auth_level,
609 .auth_context_id = auth->auth_context_id,
611 bool check_pkt_auth_fields;
612 NTSTATUS status;
614 if (!auth->auth_started) {
615 return false;
618 if (!auth->auth_finished) {
619 call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
620 return false;
623 if (auth->auth_invalid) {
624 return false;
627 if (call->pkt.pfc_flags & DCERPC_PFC_FLAG_FIRST) {
629 * The caller most likely checked this
630 * already, but we better double check.
632 check_pkt_auth_fields = true;
633 } else {
635 * The caller already found first fragment
636 * and is passing the auth_state of it.
637 * A server is supposed to use the
638 * setting of the first fragment and
639 * completely ignore the values
640 * on the remaining fragments
642 check_pkt_auth_fields = false;
645 status = dcerpc_ncacn_pull_pkt_auth(&tmp_auth,
646 auth->gensec_security,
647 check_pkt_auth_fields,
648 call,
649 pkt->ptype,
650 required_flags,
651 optional_flags,
652 payload_offset,
653 payload_and_verifier,
654 full_packet,
655 pkt);
656 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTOCOL_ERROR)) {
657 call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
658 return false;
660 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_UNSUPPORTED_AUTHN_LEVEL)) {
661 call->fault_code = DCERPC_NCA_S_UNSUPPORTED_AUTHN_LEVEL;
662 return false;
664 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_SEC_PKG_ERROR)) {
665 call->fault_code = DCERPC_FAULT_SEC_PKG_ERROR;
666 return false;
668 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
669 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
670 return false;
672 if (!NT_STATUS_IS_OK(status)) {
673 return false;
676 return true;
680 push a signed or sealed dcerpc request packet into a blob
682 bool dcesrv_auth_pkt_push(struct dcesrv_call_state *call,
683 DATA_BLOB *blob, size_t sig_size,
684 uint8_t payload_offset,
685 const DATA_BLOB *payload,
686 const struct ncacn_packet *pkt)
688 struct dcesrv_auth *auth = call->auth_state;
689 const struct dcerpc_auth tmp_auth = {
690 .auth_type = auth->auth_type,
691 .auth_level = auth->auth_level,
692 .auth_context_id = auth->auth_context_id,
694 NTSTATUS status;
696 status = dcerpc_ncacn_push_pkt_auth(&tmp_auth,
697 auth->gensec_security,
698 call, blob, sig_size,
699 payload_offset,
700 payload,
701 pkt);
702 return NT_STATUS_IS_OK(status);