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"
36 parse any auth information from a dcerpc bind request
37 return false if we can't handle the auth request for some
38 reason (in which case we send a bind_nak)
40 bool dcesrv_auth_bind(struct dcesrv_call_state *call)
42 struct cli_credentials *server_credentials = NULL;
43 struct ncacn_packet *pkt = &call->pkt;
44 struct dcesrv_connection *dce_conn = call->conn;
45 struct dcesrv_auth *auth = &dce_conn->auth_state;
46 bool want_header_signing = false;
49 if (pkt->auth_length == 0) {
50 enum dcerpc_transport_t transport =
51 dcerpc_binding_get_transport(call->conn->endpoint->ep_description);
52 const char *auth_type = derpc_transport_string_by_transport(transport);
53 const char *transport_protection = AUTHZ_TRANSPORT_PROTECTION_NONE;
54 if (transport == NCACN_NP) {
55 transport_protection = AUTHZ_TRANSPORT_PROTECTION_SMB;
57 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
58 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
59 auth->auth_context_id = 0;
62 * Log the authorization to this RPC interface. This
63 * covered ncacn_np pass-through auth, and anonymous
64 * DCE/RPC (eg epmapper, netlogon etc)
66 log_successful_authz_event(call->conn->msg_ctx,
67 call->conn->dce_ctx->lp_ctx,
68 call->conn->remote_address,
69 call->conn->local_address,
73 call->conn->auth_state.session_info);
78 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.bind.auth_info,
81 if (!NT_STATUS_IS_OK(status)) {
83 * Setting DCERPC_AUTH_LEVEL_NONE,
84 * gives the caller the reject_reason
87 * Note: DCERPC_AUTH_LEVEL_NONE == 1
89 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
90 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
91 auth->auth_context_id =
92 DCERPC_BIND_NAK_REASON_PROTOCOL_VERSION_NOT_SUPPORTED;
96 switch (call->in_auth_info.auth_level) {
97 case DCERPC_AUTH_LEVEL_CONNECT:
98 case DCERPC_AUTH_LEVEL_CALL:
99 case DCERPC_AUTH_LEVEL_PACKET:
100 case DCERPC_AUTH_LEVEL_INTEGRITY:
101 case DCERPC_AUTH_LEVEL_PRIVACY:
103 * We evaluate auth_type only if auth_level was valid
108 * Setting DCERPC_AUTH_LEVEL_NONE,
109 * gives the caller the reject_reason
110 * as auth_context_id.
112 * Note: DCERPC_AUTH_LEVEL_NONE == 1
114 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
115 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
116 auth->auth_context_id = DCERPC_BIND_NAK_REASON_NOT_SPECIFIED;
120 auth->auth_type = call->in_auth_info.auth_type;
121 auth->auth_level = call->in_auth_info.auth_level;
122 auth->auth_context_id = call->in_auth_info.auth_context_id;
125 = cli_credentials_init(call);
126 if (!server_credentials) {
127 DEBUG(1, ("Failed to init server credentials\n"));
131 cli_credentials_set_conf(server_credentials, call->conn->dce_ctx->lp_ctx);
132 status = cli_credentials_set_machine_account(server_credentials, call->conn->dce_ctx->lp_ctx);
133 if (!NT_STATUS_IS_OK(status)) {
134 DEBUG(1, ("Failed to obtain server credentials: %s\n",
139 status = samba_server_gensec_start(dce_conn, call->event_ctx,
141 call->conn->dce_ctx->lp_ctx,
144 &auth->gensec_security);
145 if (!NT_STATUS_IS_OK(status)) {
146 DEBUG(1, ("Failed to call samba_server_gensec_start %s\n",
152 * We have to call this because we set the target_service for
153 * Kerberos to NULL above, and in any case we wish to log a
154 * more specific service target.
157 status = gensec_set_target_service_description(auth->gensec_security,
159 if (!NT_STATUS_IS_OK(status)) {
160 DEBUG(1, ("Failed to call gensec_set_target_service_description %s\n",
165 if (call->conn->remote_address != NULL) {
166 status = gensec_set_remote_address(auth->gensec_security,
167 call->conn->remote_address);
168 if (!NT_STATUS_IS_OK(status)) {
169 DEBUG(1, ("Failed to call gensec_set_remote_address() %s\n",
175 if (call->conn->local_address != NULL) {
176 status = gensec_set_local_address(auth->gensec_security,
177 call->conn->local_address);
178 if (!NT_STATUS_IS_OK(status)) {
179 DEBUG(1, ("Failed to call gensec_set_local_address() %s\n",
185 status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_type,
187 if (!NT_STATUS_IS_OK(status)) {
188 const char *backend_name =
189 gensec_get_name_by_authtype(auth->gensec_security,
192 DEBUG(3, ("Failed to start GENSEC mechanism for DCERPC server: "
193 "auth_type=%d (%s), auth_level=%d: %s\n",
194 (int)auth->auth_type, backend_name,
195 (int)auth->auth_level,
199 * Setting DCERPC_AUTH_LEVEL_NONE,
200 * gives the caller the reject_reason
201 * as auth_context_id.
203 * Note: DCERPC_AUTH_LEVEL_NONE == 1
205 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
206 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
207 if (backend_name != NULL) {
208 auth->auth_context_id =
209 DCERPC_BIND_NAK_REASON_INVALID_CHECKSUM;
211 auth->auth_context_id =
212 DCERPC_BIND_NAK_REASON_INVALID_AUTH_TYPE;
217 if (call->pkt.pfc_flags & DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN) {
218 auth->client_hdr_signing = true;
219 want_header_signing = true;
222 if (want_header_signing) {
223 want_header_signing = gensec_have_feature(auth->gensec_security,
224 GENSEC_FEATURE_SIGN_PKT_HEADER);
227 if (want_header_signing) {
228 want_header_signing = lpcfg_parm_bool(dce_conn->dce_ctx->lp_ctx,
235 if (want_header_signing) {
236 gensec_want_feature(auth->gensec_security,
237 GENSEC_FEATURE_SIGN_PKT_HEADER);
238 auth->hdr_signing = true;
244 NTSTATUS dcesrv_auth_complete(struct dcesrv_call_state *call, NTSTATUS status)
246 struct dcesrv_connection *dce_conn = call->conn;
247 const char *pdu = "<unknown>";
249 switch (call->pkt.ptype) {
250 case DCERPC_PKT_BIND:
253 case DCERPC_PKT_ALTER:
256 case DCERPC_PKT_AUTH3:
260 return NT_STATUS_INTERNAL_ERROR;
263 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
267 if (!NT_STATUS_IS_OK(status)) {
268 DEBUG(4, ("GENSEC mech rejected the incoming authentication "
269 "at %s: %s\n", pdu, nt_errstr(status)));
273 status = gensec_session_info(dce_conn->auth_state.gensec_security,
275 &dce_conn->auth_state.session_info);
276 if (!NT_STATUS_IS_OK(status)) {
277 DEBUG(1, ("Failed to establish session_info: %s\n",
281 dce_conn->auth_state.auth_finished = true;
282 dce_conn->allow_request = true;
284 /* Now that we are authenticated, go back to the generic session key... */
285 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
290 add any auth information needed in a bind ack, and process the authentication
291 information found in the bind.
293 NTSTATUS dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
295 struct dcesrv_connection *dce_conn = call->conn;
298 dce_conn->allow_alter = true;
299 dce_conn->allow_auth3 = true;
301 if (call->pkt.auth_length == 0) {
302 dce_conn->auth_state.auth_finished = true;
303 dce_conn->allow_request = true;
307 /* We can't work without an existing gensec state */
308 if (!call->conn->auth_state.gensec_security) {
309 return NT_STATUS_INTERNAL_ERROR;
312 if (dce_conn->auth_state.hdr_signing) {
313 pkt->pfc_flags |= DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN;
316 call->_out_auth_info = (struct dcerpc_auth) {
317 .auth_type = dce_conn->auth_state.auth_type,
318 .auth_level = dce_conn->auth_state.auth_level,
319 .auth_context_id = dce_conn->auth_state.auth_context_id,
321 call->out_auth_info = &call->_out_auth_info;
323 status = gensec_update_ev(dce_conn->auth_state.gensec_security,
324 call, call->event_ctx,
325 call->in_auth_info.credentials,
326 &call->out_auth_info->credentials);
328 return dcesrv_auth_complete(call, status);
333 process the final stage of a auth request
335 bool dcesrv_auth_auth3(struct dcesrv_call_state *call)
337 struct ncacn_packet *pkt = &call->pkt;
338 struct dcesrv_connection *dce_conn = call->conn;
341 if (pkt->auth_length == 0) {
345 if (dce_conn->auth_state.auth_finished) {
349 /* We can't work without an existing gensec state */
350 if (!dce_conn->auth_state.gensec_security) {
354 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.auth3.auth_info,
355 &call->in_auth_info, NULL, true);
356 if (!NT_STATUS_IS_OK(status)) {
358 * Windows returns DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY
359 * instead of DCERPC_NCA_S_PROTO_ERROR.
361 call->fault_code = DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY;
365 if (call->in_auth_info.auth_type != dce_conn->auth_state.auth_type) {
369 if (call->in_auth_info.auth_level != dce_conn->auth_state.auth_level) {
373 if (call->in_auth_info.auth_context_id != dce_conn->auth_state.auth_context_id) {
377 call->_out_auth_info = (struct dcerpc_auth) {
378 .auth_type = dce_conn->auth_state.auth_type,
379 .auth_level = dce_conn->auth_state.auth_level,
380 .auth_context_id = dce_conn->auth_state.auth_context_id,
382 call->out_auth_info = &call->_out_auth_info;
384 /* Pass the extra data we got from the client down to gensec for processing */
385 status = gensec_update_ev(dce_conn->auth_state.gensec_security,
386 call, call->event_ctx,
387 call->in_auth_info.credentials,
388 &call->out_auth_info->credentials);
389 if (NT_STATUS_IS_OK(status)) {
390 status = gensec_session_info(dce_conn->auth_state.gensec_security,
392 &dce_conn->auth_state.session_info);
393 if (!NT_STATUS_IS_OK(status)) {
394 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
397 dce_conn->auth_state.auth_finished = true;
398 dce_conn->allow_request = true;
400 /* Now that we are authenticated, go back to the generic session key... */
401 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
403 if (call->out_auth_info->credentials.length != 0) {
405 DEBUG(4, ("GENSEC produced output token (len=%u) at bind_auth3\n",
406 (unsigned)call->out_auth_info->credentials.length));
411 DEBUG(4, ("GENSEC mech rejected the incoming authentication at bind_auth3: %s\n",
418 parse any auth information from a dcerpc alter request
419 return false if we can't handle the auth request for some
420 reason (in which case we send a bind_nak (is this true for here?))
422 bool dcesrv_auth_alter(struct dcesrv_call_state *call)
424 struct ncacn_packet *pkt = &call->pkt;
425 struct dcesrv_connection *dce_conn = call->conn;
428 /* on a pure interface change there is no auth blob */
429 if (pkt->auth_length == 0) {
430 if (!dce_conn->auth_state.auth_finished) {
436 if (dce_conn->auth_state.auth_finished) {
437 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
441 /* We can't work without an existing gensec state */
442 if (!dce_conn->auth_state.gensec_security) {
446 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.alter.auth_info,
447 &call->in_auth_info, NULL, true);
448 if (!NT_STATUS_IS_OK(status)) {
449 call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
453 if (call->in_auth_info.auth_type == DCERPC_AUTH_TYPE_NONE) {
454 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
458 if (call->in_auth_info.auth_type != dce_conn->auth_state.auth_type) {
462 if (call->in_auth_info.auth_level != dce_conn->auth_state.auth_level) {
466 if (call->in_auth_info.auth_context_id != dce_conn->auth_state.auth_context_id) {
474 add any auth information needed in a alter ack, and process the authentication
475 information found in the alter.
477 NTSTATUS dcesrv_auth_alter_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
479 struct dcesrv_connection *dce_conn = call->conn;
482 /* on a pure interface change there is no auth_info structure
484 if (call->pkt.auth_length == 0) {
488 if (!call->conn->auth_state.gensec_security) {
489 return NT_STATUS_INTERNAL_ERROR;
492 call->_out_auth_info = (struct dcerpc_auth) {
493 .auth_type = dce_conn->auth_state.auth_type,
494 .auth_level = dce_conn->auth_state.auth_level,
495 .auth_context_id = dce_conn->auth_state.auth_context_id,
497 call->out_auth_info = &call->_out_auth_info;
499 status = gensec_update_ev(dce_conn->auth_state.gensec_security,
500 call, call->event_ctx,
501 call->in_auth_info.credentials,
502 &call->out_auth_info->credentials);
504 if (NT_STATUS_IS_OK(status)) {
505 status = gensec_session_info(dce_conn->auth_state.gensec_security,
507 &dce_conn->auth_state.session_info);
508 if (!NT_STATUS_IS_OK(status)) {
509 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
512 dce_conn->auth_state.auth_finished = true;
513 dce_conn->allow_request = true;
515 /* Now that we are authenticated, got back to the generic session key... */
516 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
518 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
522 DEBUG(4, ("GENSEC mech rejected the incoming authentication at auth alter_ack: %s\n",
528 check credentials on a packet
530 bool dcesrv_auth_pkt_pull(struct dcesrv_call_state *call,
531 DATA_BLOB *full_packet,
532 uint8_t required_flags,
533 uint8_t optional_flags,
534 uint8_t payload_offset,
535 DATA_BLOB *payload_and_verifier)
537 struct ncacn_packet *pkt = &call->pkt;
538 struct dcesrv_connection *dce_conn = call->conn;
539 const struct dcerpc_auth tmp_auth = {
540 .auth_type = dce_conn->auth_state.auth_type,
541 .auth_level = dce_conn->auth_state.auth_level,
542 .auth_context_id = dce_conn->auth_state.auth_context_id,
546 if (!dce_conn->allow_request) {
547 call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
551 if (dce_conn->auth_state.auth_invalid) {
555 status = dcerpc_ncacn_pull_pkt_auth(&tmp_auth,
556 dce_conn->auth_state.gensec_security,
562 payload_and_verifier,
565 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTOCOL_ERROR)) {
566 call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
569 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_UNSUPPORTED_AUTHN_LEVEL)) {
570 call->fault_code = DCERPC_NCA_S_UNSUPPORTED_AUTHN_LEVEL;
573 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_SEC_PKG_ERROR)) {
574 call->fault_code = DCERPC_FAULT_SEC_PKG_ERROR;
577 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
578 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
581 if (!NT_STATUS_IS_OK(status)) {
589 push a signed or sealed dcerpc request packet into a blob
591 bool dcesrv_auth_pkt_push(struct dcesrv_call_state *call,
592 DATA_BLOB *blob, size_t sig_size,
593 uint8_t payload_offset,
594 const DATA_BLOB *payload,
595 const struct ncacn_packet *pkt)
597 struct dcesrv_connection *dce_conn = call->conn;
598 const struct dcerpc_auth tmp_auth = {
599 .auth_type = dce_conn->auth_state.auth_type,
600 .auth_level = dce_conn->auth_state.auth_level,
601 .auth_context_id = dce_conn->auth_state.auth_context_id,
605 status = dcerpc_ncacn_push_pkt_auth(&tmp_auth,
606 dce_conn->auth_state.gensec_security,
607 call, blob, sig_size,
611 return NT_STATUS_IS_OK(status);