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 "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
)) {
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
;
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
) {
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
) {
73 gensec_want_feature(a
->gensec_security
,
74 GENSEC_FEATURE_SIGN_PKT_HEADER
);
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
;
87 if (auth
->auth_started
) {
91 auth
->auth_started
= true;
93 if (auth
->auth_invalid
) {
97 if (auth
->auth_finished
) {
101 if (auth
->gensec_security
!= NULL
) {
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
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
;
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 if (auth
->auth_level
== DCERPC_AUTH_LEVEL_CONNECT
&&
134 !call
->conn
->got_explicit_auth_level_connect
)
136 call
->conn
->default_auth_level_connect
= auth
;
139 cb
->auth
.become_root();
140 status
= cb
->auth
.gensec_prepare(
143 &auth
->gensec_security
,
144 cb
->auth
.private_data
);
145 cb
->auth
.unbecome_root();
146 if (!NT_STATUS_IS_OK(status
)) {
147 DEBUG(1, ("Failed to call samba_server_gensec_start %s\n",
153 * We have to call this because we set the target_service for
154 * Kerberos to NULL above, and in any case we wish to log a
155 * more specific service target.
158 status
= gensec_set_target_service_description(auth
->gensec_security
,
160 if (!NT_STATUS_IS_OK(status
)) {
161 DEBUG(1, ("Failed to call gensec_set_target_service_description %s\n",
166 if (call
->conn
->remote_address
!= NULL
) {
167 status
= gensec_set_remote_address(auth
->gensec_security
,
168 call
->conn
->remote_address
);
169 if (!NT_STATUS_IS_OK(status
)) {
170 DEBUG(1, ("Failed to call gensec_set_remote_address() %s\n",
176 if (call
->conn
->local_address
!= NULL
) {
177 status
= gensec_set_local_address(auth
->gensec_security
,
178 call
->conn
->local_address
);
179 if (!NT_STATUS_IS_OK(status
)) {
180 DEBUG(1, ("Failed to call gensec_set_local_address() %s\n",
186 status
= gensec_start_mech_by_authtype(auth
->gensec_security
, auth
->auth_type
,
188 if (!NT_STATUS_IS_OK(status
)) {
189 const char *backend_name
=
190 gensec_get_name_by_authtype(auth
->gensec_security
,
193 DEBUG(3, ("Failed to start GENSEC mechanism for DCERPC server: "
194 "auth_type=%d (%s), auth_level=%d: %s\n",
195 (int)auth
->auth_type
, backend_name
,
196 (int)auth
->auth_level
,
200 * Setting DCERPC_AUTH_LEVEL_NONE,
201 * gives the caller the reject_reason
202 * as auth_context_id.
204 * Note: DCERPC_AUTH_LEVEL_NONE == 1
206 auth
->auth_type
= DCERPC_AUTH_TYPE_NONE
;
207 auth
->auth_level
= DCERPC_AUTH_LEVEL_NONE
;
208 if (backend_name
!= NULL
) {
209 auth
->auth_context_id
=
210 DCERPC_BIND_NAK_REASON_INVALID_CHECKSUM
;
212 auth
->auth_context_id
=
213 DCERPC_BIND_NAK_REASON_INVALID_AUTH_TYPE
;
218 if (dce_conn
->negotiated_hdr_signing
) {
219 gensec_want_feature(auth
->gensec_security
,
220 GENSEC_FEATURE_SIGN_PKT_HEADER
);
226 static void dcesrv_default_auth_state_finish_bind(struct dcesrv_call_state
*call
)
228 SMB_ASSERT(call
->pkt
.ptype
== DCERPC_PKT_BIND
);
230 if (call
->auth_state
== call
->conn
->default_auth_state
) {
234 if (call
->conn
->default_auth_state
->auth_started
) {
238 if (call
->conn
->default_auth_state
->auth_invalid
) {
242 call
->conn
->default_auth_state
->auth_type
= DCERPC_AUTH_TYPE_NONE
;
243 call
->conn
->default_auth_state
->auth_level
= DCERPC_AUTH_LEVEL_NONE
;
244 call
->conn
->default_auth_state
->auth_context_id
= 0;
245 call
->conn
->default_auth_state
->auth_started
= true;
246 call
->conn
->default_auth_state
->auth_finished
= true;
250 * We defer log_successful_dcesrv_authz_event()
251 * to dcesrv_default_auth_state_prepare_request()
253 * As we don't want to trigger authz_events
254 * just for alter_context requests without authentication
258 void dcesrv_default_auth_state_prepare_request(struct dcesrv_call_state
*call
)
260 struct dcesrv_connection
*dce_conn
= call
->conn
;
261 struct dcesrv_auth
*auth
= call
->auth_state
;
262 struct dcesrv_context_callbacks
*cb
= call
->conn
->dce_ctx
->callbacks
;
264 if (auth
->auth_audited
) {
268 if (call
->pkt
.ptype
!= DCERPC_PKT_REQUEST
) {
272 if (auth
!= dce_conn
->default_auth_state
) {
276 if (auth
->auth_invalid
) {
280 if (!auth
->auth_finished
) {
284 if (cb
->log
.successful_authz
== NULL
) {
288 cb
->log
.successful_authz(call
, cb
->log
.private_data
);
292 parse any auth information from a dcerpc bind request
293 return false if we can't handle the auth request for some
294 reason (in which case we send a bind_nak)
296 bool dcesrv_auth_bind(struct dcesrv_call_state
*call
)
298 struct ncacn_packet
*pkt
= &call
->pkt
;
299 struct dcesrv_auth
*auth
= call
->auth_state
;
300 struct dcesrv_context_callbacks
*cb
= call
->conn
->dce_ctx
->callbacks
;
303 if (pkt
->auth_length
== 0) {
304 auth
->auth_type
= DCERPC_AUTH_TYPE_NONE
;
305 auth
->auth_level
= DCERPC_AUTH_LEVEL_NONE
;
306 auth
->auth_context_id
= 0;
307 auth
->auth_started
= true;
309 if (cb
->log
.successful_authz
!= NULL
) {
310 cb
->log
.successful_authz(call
, cb
->log
.private_data
);
316 status
= dcerpc_pull_auth_trailer(pkt
, call
, &pkt
->u
.bind
.auth_info
,
319 if (!NT_STATUS_IS_OK(status
)) {
321 * Setting DCERPC_AUTH_LEVEL_NONE,
322 * gives the caller the reject_reason
323 * as auth_context_id.
325 * Note: DCERPC_AUTH_LEVEL_NONE == 1
327 auth
->auth_type
= DCERPC_AUTH_TYPE_NONE
;
328 auth
->auth_level
= DCERPC_AUTH_LEVEL_NONE
;
329 if (NT_STATUS_EQUAL(status
, NT_STATUS_RPC_PROTOCOL_ERROR
)) {
330 auth
->auth_context_id
=
331 call
->in_auth_info
.auth_context_id
;
333 auth
->auth_context_id
=
334 DCERPC_BIND_NAK_REASON_NOT_SPECIFIED
;
339 return dcesrv_auth_prepare_gensec(call
);
342 NTSTATUS
dcesrv_auth_complete(struct dcesrv_call_state
*call
, NTSTATUS status
)
344 struct dcesrv_auth
*auth
= call
->auth_state
;
345 struct dcesrv_context_callbacks
*cb
= call
->conn
->dce_ctx
->callbacks
;
346 const char *pdu
= "<unknown>";
348 switch (call
->pkt
.ptype
) {
349 case DCERPC_PKT_BIND
:
352 case DCERPC_PKT_ALTER
:
355 case DCERPC_PKT_AUTH3
:
357 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
358 DEBUG(4, ("GENSEC not finished at %s\n", pdu
));
359 return NT_STATUS_RPC_SEC_PKG_ERROR
;
363 return NT_STATUS_INTERNAL_ERROR
;
366 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
370 if (!NT_STATUS_IS_OK(status
)) {
371 DEBUG(4, ("GENSEC mech rejected the incoming authentication "
372 "at %s: %s\n", pdu
, nt_errstr(status
)));
376 cb
->auth
.become_root();
377 status
= gensec_session_info(auth
->gensec_security
,
379 &auth
->session_info
);
380 cb
->auth
.unbecome_root();
381 if (!NT_STATUS_IS_OK(status
)) {
382 DEBUG(1, ("Failed to establish session_info: %s\n",
386 auth
->auth_finished
= true;
388 if (call
->pkt
.ptype
!= DCERPC_PKT_AUTH3
) {
392 if (call
->out_auth_info
->credentials
.length
!= 0) {
393 DEBUG(4, ("GENSEC produced output token (len=%zu) at %s\n",
394 call
->out_auth_info
->credentials
.length
, pdu
));
395 return NT_STATUS_RPC_SEC_PKG_ERROR
;
402 add any auth information needed in a bind ack, and process the authentication
403 information found in the bind.
405 NTSTATUS
dcesrv_auth_prepare_bind_ack(struct dcesrv_call_state
*call
, struct ncacn_packet
*pkt
)
407 struct dcesrv_connection
*dce_conn
= call
->conn
;
408 struct dcesrv_auth
*auth
= call
->auth_state
;
411 status
= dcesrv_auth_negotiate_hdr_signing(call
, pkt
);
412 if (!NT_STATUS_IS_OK(status
)) {
416 dce_conn
->allow_alter
= true;
417 dcesrv_default_auth_state_finish_bind(call
);
419 if (call
->pkt
.auth_length
== 0) {
420 auth
->auth_finished
= true;
424 /* We can't work without an existing gensec state */
425 if (auth
->gensec_security
== NULL
) {
426 return NT_STATUS_INTERNAL_ERROR
;
429 call
->_out_auth_info
= (struct dcerpc_auth
) {
430 .auth_type
= auth
->auth_type
,
431 .auth_level
= auth
->auth_level
,
432 .auth_context_id
= auth
->auth_context_id
,
434 call
->out_auth_info
= &call
->_out_auth_info
;
440 process the final stage of a auth request
442 bool dcesrv_auth_prepare_auth3(struct dcesrv_call_state
*call
)
444 struct ncacn_packet
*pkt
= &call
->pkt
;
445 struct dcesrv_auth
*auth
= call
->auth_state
;
448 if (pkt
->frag_length
> call
->conn
->transport_max_recv_frag
) {
450 * Note that we don't check against the negotiated
451 * max_recv_frag, but a hard coded value from
454 call
->fault_code
= DCERPC_NCA_S_PROTO_ERROR
;
458 if (pkt
->auth_length
> 4096) {
459 call
->fault_code
= DCERPC_NCA_S_PROTO_ERROR
;
463 if (auth
->auth_finished
) {
464 call
->fault_code
= DCERPC_NCA_S_PROTO_ERROR
;
468 if (!auth
->auth_started
) {
469 call
->fault_code
= DCERPC_NCA_S_PROTO_ERROR
;
473 if (auth
->auth_invalid
) {
474 call
->fault_code
= DCERPC_NCA_S_PROTO_ERROR
;
478 if (pkt
->auth_length
== 0) {
479 call
->fault_code
= DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY
;
483 if (auth
->auth_invalid
) {
487 /* We can't work without an existing gensec state */
488 if (auth
->gensec_security
== NULL
) {
492 status
= dcerpc_pull_auth_trailer(pkt
, call
, &pkt
->u
.auth3
.auth_info
,
493 &call
->in_auth_info
, NULL
, true);
494 if (!NT_STATUS_IS_OK(status
)) {
495 struct dcerpc_auth
*auth_info
= &call
->in_auth_info
;
496 uint32_t nr
= auth_info
->auth_context_id
;
499 * Windows returns DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY
500 * instead of DCERPC_NCA_S_PROTO_ERROR in most cases.
502 call
->fault_code
= DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY
;
504 if (NT_STATUS_EQUAL(status
, NT_STATUS_RPC_PROTOCOL_ERROR
) &&
505 nr
!= DCERPC_BIND_NAK_REASON_PROTOCOL_VERSION_NOT_SUPPORTED
)
507 call
->fault_code
= DCERPC_NCA_S_PROTO_ERROR
;
513 if (call
->in_auth_info
.auth_type
!= auth
->auth_type
) {
514 call
->fault_code
= DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY
;
518 if (call
->in_auth_info
.auth_level
!= auth
->auth_level
) {
519 call
->fault_code
= DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY
;
523 if (call
->in_auth_info
.auth_context_id
!= auth
->auth_context_id
) {
524 call
->fault_code
= DCERPC_FAULT_ACCESS_DENIED
;
528 call
->_out_auth_info
= (struct dcerpc_auth
) {
529 .auth_type
= auth
->auth_type
,
530 .auth_level
= auth
->auth_level
,
531 .auth_context_id
= auth
->auth_context_id
,
533 call
->out_auth_info
= &call
->_out_auth_info
;
539 parse any auth information from a dcerpc alter request
540 return false if we can't handle the auth request for some
541 reason (in which case we send a bind_nak (is this true for here?))
543 bool dcesrv_auth_alter(struct dcesrv_call_state
*call
)
545 struct ncacn_packet
*pkt
= &call
->pkt
;
546 struct dcesrv_auth
*auth
= call
->auth_state
;
549 /* on a pure interface change there is no auth blob */
550 if (pkt
->auth_length
== 0) {
551 if (!auth
->auth_finished
) {
557 if (auth
->auth_finished
) {
558 call
->fault_code
= DCERPC_FAULT_ACCESS_DENIED
;
562 status
= dcerpc_pull_auth_trailer(pkt
, call
, &pkt
->u
.alter
.auth_info
,
563 &call
->in_auth_info
, NULL
, true);
564 if (!NT_STATUS_IS_OK(status
)) {
565 call
->fault_code
= DCERPC_NCA_S_PROTO_ERROR
;
569 if (!auth
->auth_started
) {
572 ok
= dcesrv_auth_prepare_gensec(call
);
574 call
->fault_code
= DCERPC_FAULT_ACCESS_DENIED
;
581 if (call
->in_auth_info
.auth_type
== DCERPC_AUTH_TYPE_NONE
) {
582 call
->fault_code
= DCERPC_FAULT_ACCESS_DENIED
;
586 if (auth
->auth_invalid
) {
590 if (call
->in_auth_info
.auth_type
!= auth
->auth_type
) {
594 if (call
->in_auth_info
.auth_level
!= auth
->auth_level
) {
598 if (call
->in_auth_info
.auth_context_id
!= auth
->auth_context_id
) {
606 add any auth information needed in a alter ack, and process the authentication
607 information found in the alter.
609 NTSTATUS
dcesrv_auth_prepare_alter_ack(struct dcesrv_call_state
*call
, struct ncacn_packet
*pkt
)
611 struct dcesrv_auth
*auth
= call
->auth_state
;
614 status
= dcesrv_auth_negotiate_hdr_signing(call
, pkt
);
615 if (!NT_STATUS_IS_OK(status
)) {
619 /* on a pure interface change there is no auth_info structure
621 if (call
->pkt
.auth_length
== 0) {
625 if (auth
->gensec_security
== NULL
) {
626 return NT_STATUS_INTERNAL_ERROR
;
629 call
->_out_auth_info
= (struct dcerpc_auth
) {
630 .auth_type
= auth
->auth_type
,
631 .auth_level
= auth
->auth_level
,
632 .auth_context_id
= auth
->auth_context_id
,
634 call
->out_auth_info
= &call
->_out_auth_info
;
640 check credentials on a packet
642 bool dcesrv_auth_pkt_pull(struct dcesrv_call_state
*call
,
643 DATA_BLOB
*full_packet
,
644 uint8_t required_flags
,
645 uint8_t optional_flags
,
646 uint8_t payload_offset
,
647 DATA_BLOB
*payload_and_verifier
)
649 struct ncacn_packet
*pkt
= &call
->pkt
;
650 struct dcesrv_auth
*auth
= call
->auth_state
;
651 const struct dcerpc_auth tmp_auth
= {
652 .auth_type
= auth
->auth_type
,
653 .auth_level
= auth
->auth_level
,
654 .auth_context_id
= auth
->auth_context_id
,
656 bool check_pkt_auth_fields
;
659 if (!auth
->auth_started
) {
663 if (auth
->auth_invalid
) {
667 if (!auth
->auth_finished
) {
668 call
->fault_code
= DCERPC_NCA_S_PROTO_ERROR
;
672 if (call
->pkt
.pfc_flags
& DCERPC_PFC_FLAG_FIRST
) {
674 * The caller most likely checked this
675 * already, but we better double check.
677 check_pkt_auth_fields
= true;
680 * The caller already found first fragment
681 * and is passing the auth_state of it.
682 * A server is supposed to use the
683 * setting of the first fragment and
684 * completely ignore the values
685 * on the remaining fragments
687 check_pkt_auth_fields
= false;
690 status
= dcerpc_ncacn_pull_pkt_auth(&tmp_auth
,
691 auth
->gensec_security
,
692 check_pkt_auth_fields
,
698 payload_and_verifier
,
701 if (NT_STATUS_EQUAL(status
, NT_STATUS_RPC_PROTOCOL_ERROR
)) {
702 call
->fault_code
= DCERPC_NCA_S_PROTO_ERROR
;
705 if (NT_STATUS_EQUAL(status
, NT_STATUS_RPC_UNSUPPORTED_AUTHN_LEVEL
)) {
706 call
->fault_code
= DCERPC_NCA_S_UNSUPPORTED_AUTHN_LEVEL
;
709 if (NT_STATUS_EQUAL(status
, NT_STATUS_RPC_SEC_PKG_ERROR
)) {
710 call
->fault_code
= DCERPC_FAULT_SEC_PKG_ERROR
;
713 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
714 call
->fault_code
= DCERPC_FAULT_ACCESS_DENIED
;
717 if (!NT_STATUS_IS_OK(status
)) {
725 push a signed or sealed dcerpc request packet into a blob
727 bool dcesrv_auth_pkt_push(struct dcesrv_call_state
*call
,
728 DATA_BLOB
*blob
, size_t sig_size
,
729 uint8_t payload_offset
,
730 const DATA_BLOB
*payload
,
731 const struct ncacn_packet
*pkt
)
733 struct dcesrv_auth
*auth
= call
->auth_state
;
734 const struct dcerpc_auth tmp_auth
= {
735 .auth_type
= auth
->auth_type
,
736 .auth_level
= auth
->auth_level
,
737 .auth_context_id
= auth
->auth_context_id
,
741 status
= dcerpc_ncacn_push_pkt_auth(&tmp_auth
,
742 auth
->gensec_security
,
743 call
, blob
, sig_size
,
747 return NT_STATUS_IS_OK(status
);