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 cb
->auth
.become_root();
134 status
= cb
->auth
.gensec_prepare(
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",
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
,
154 if (!NT_STATUS_IS_OK(status
)) {
155 DEBUG(1, ("Failed to call gensec_set_target_service_description %s\n",
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",
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",
180 status
= gensec_start_mech_by_authtype(auth
->gensec_security
, auth
->auth_type
,
182 if (!NT_STATUS_IS_OK(status
)) {
183 const char *backend_name
=
184 gensec_get_name_by_authtype(auth
->gensec_security
,
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
,
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
;
206 auth
->auth_context_id
=
207 DCERPC_BIND_NAK_REASON_INVALID_AUTH_TYPE
;
212 if (dce_conn
->negotiated_hdr_signing
) {
213 gensec_want_feature(auth
->gensec_security
,
214 GENSEC_FEATURE_SIGN_PKT_HEADER
);
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
) {
228 if (call
->conn
->default_auth_state
->auth_started
) {
232 if (call
->conn
->default_auth_state
->auth_invalid
) {
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
) {
262 if (call
->pkt
.ptype
!= DCERPC_PKT_REQUEST
) {
266 if (auth
!= dce_conn
->default_auth_state
) {
270 if (auth
->auth_invalid
) {
274 if (!auth
->auth_finished
) {
278 if (cb
->log
.successful_authz
== NULL
) {
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
;
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
);
310 status
= dcerpc_pull_auth_trailer(pkt
, call
, &pkt
->u
.bind
.auth_info
,
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
;
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
:
341 case DCERPC_PKT_ALTER
:
344 case DCERPC_PKT_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
;
352 return NT_STATUS_INTERNAL_ERROR
;
355 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
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
)));
365 cb
->auth
.become_root();
366 status
= gensec_session_info(auth
->gensec_security
,
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",
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
) {
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
;
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
;
406 status
= dcesrv_auth_negotiate_hdr_signing(call
, pkt
);
407 if (!NT_STATUS_IS_OK(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;
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
;
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
;
443 if (pkt
->auth_length
== 0) {
447 if (auth
->auth_finished
) {
451 if (auth
->auth_invalid
) {
455 /* We can't work without an existing gensec state */
456 if (auth
->gensec_security
== NULL
) {
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
;
471 if (call
->in_auth_info
.auth_type
!= auth
->auth_type
) {
475 if (call
->in_auth_info
.auth_level
!= auth
->auth_level
) {
479 if (call
->in_auth_info
.auth_context_id
!= auth
->auth_context_id
) {
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
;
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
;
504 /* on a pure interface change there is no auth blob */
505 if (pkt
->auth_length
== 0) {
506 if (!auth
->auth_finished
) {
512 if (auth
->auth_finished
) {
513 call
->fault_code
= DCERPC_FAULT_ACCESS_DENIED
;
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
;
524 if (!auth
->auth_started
) {
527 ok
= dcesrv_auth_prepare_gensec(call
);
529 call
->fault_code
= DCERPC_FAULT_ACCESS_DENIED
;
536 if (call
->in_auth_info
.auth_type
== DCERPC_AUTH_TYPE_NONE
) {
537 call
->fault_code
= DCERPC_FAULT_ACCESS_DENIED
;
541 if (auth
->auth_invalid
) {
545 if (call
->in_auth_info
.auth_type
!= auth
->auth_type
) {
549 if (call
->in_auth_info
.auth_level
!= auth
->auth_level
) {
553 if (call
->in_auth_info
.auth_context_id
!= auth
->auth_context_id
) {
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
;
569 status
= dcesrv_auth_negotiate_hdr_signing(call
, pkt
);
570 if (!NT_STATUS_IS_OK(status
)) {
574 /* on a pure interface change there is no auth_info structure
576 if (call
->pkt
.auth_length
== 0) {
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
;
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
;
614 if (!auth
->auth_started
) {
618 if (!auth
->auth_finished
) {
619 call
->fault_code
= DCERPC_NCA_S_PROTO_ERROR
;
623 if (auth
->auth_invalid
) {
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;
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
,
653 payload_and_verifier
,
656 if (NT_STATUS_EQUAL(status
, NT_STATUS_RPC_PROTOCOL_ERROR
)) {
657 call
->fault_code
= DCERPC_NCA_S_PROTO_ERROR
;
660 if (NT_STATUS_EQUAL(status
, NT_STATUS_RPC_UNSUPPORTED_AUTHN_LEVEL
)) {
661 call
->fault_code
= DCERPC_NCA_S_UNSUPPORTED_AUTHN_LEVEL
;
664 if (NT_STATUS_EQUAL(status
, NT_STATUS_RPC_SEC_PKG_ERROR
)) {
665 call
->fault_code
= DCERPC_FAULT_SEC_PKG_ERROR
;
668 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
669 call
->fault_code
= DCERPC_FAULT_ACCESS_DENIED
;
672 if (!NT_STATUS_IS_OK(status
)) {
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
,
696 status
= dcerpc_ncacn_push_pkt_auth(&tmp_auth
,
697 auth
->gensec_security
,
698 call
, blob
, sig_size
,
702 return NT_STATUS_IS_OK(status
);