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;
48 if (pkt->auth_length == 0) {
49 enum dcerpc_transport_t transport =
50 dcerpc_binding_get_transport(call->conn->endpoint->ep_description);
51 const char *auth_type = derpc_transport_string_by_transport(transport);
52 const char *transport_protection = AUTHZ_TRANSPORT_PROTECTION_NONE;
53 if (transport == NCACN_NP) {
54 transport_protection = AUTHZ_TRANSPORT_PROTECTION_SMB;
56 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
57 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
58 auth->auth_context_id = 0;
61 * Log the authorization to this RPC interface. This
62 * covered ncacn_np pass-through auth, and anonymous
63 * DCE/RPC (eg epmapper, netlogon etc)
65 log_successful_authz_event(call->conn->remote_address,
66 call->conn->local_address,
70 call->conn->auth_state.session_info);
75 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.bind.auth_info,
78 if (!NT_STATUS_IS_OK(status)) {
80 * Setting DCERPC_AUTH_LEVEL_NONE,
81 * gives the caller the reject_reason
84 * Note: DCERPC_AUTH_LEVEL_NONE == 1
86 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
87 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
88 auth->auth_context_id =
89 DCERPC_BIND_NAK_REASON_PROTOCOL_VERSION_NOT_SUPPORTED;
93 switch (call->in_auth_info.auth_level) {
94 case DCERPC_AUTH_LEVEL_CONNECT:
95 case DCERPC_AUTH_LEVEL_CALL:
96 case DCERPC_AUTH_LEVEL_PACKET:
97 case DCERPC_AUTH_LEVEL_INTEGRITY:
98 case DCERPC_AUTH_LEVEL_PRIVACY:
100 * We evaluate auth_type only if auth_level was valid
105 * Setting DCERPC_AUTH_LEVEL_NONE,
106 * gives the caller the reject_reason
107 * as auth_context_id.
109 * Note: DCERPC_AUTH_LEVEL_NONE == 1
111 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
112 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
113 auth->auth_context_id = DCERPC_BIND_NAK_REASON_NOT_SPECIFIED;
117 auth->auth_type = call->in_auth_info.auth_type;
118 auth->auth_level = call->in_auth_info.auth_level;
119 auth->auth_context_id = call->in_auth_info.auth_context_id;
122 = cli_credentials_init(call);
123 if (!server_credentials) {
124 DEBUG(1, ("Failed to init server credentials\n"));
128 cli_credentials_set_conf(server_credentials, call->conn->dce_ctx->lp_ctx);
129 status = cli_credentials_set_machine_account(server_credentials, call->conn->dce_ctx->lp_ctx);
130 if (!NT_STATUS_IS_OK(status)) {
131 DEBUG(1, ("Failed to obtain server credentials: %s\n",
136 status = samba_server_gensec_start(dce_conn, call->event_ctx,
138 call->conn->dce_ctx->lp_ctx,
141 &auth->gensec_security);
142 if (!NT_STATUS_IS_OK(status)) {
143 DEBUG(1, ("Failed to call samba_server_gensec_start %s\n",
149 * We have to call this because we set the target_service for
150 * Kerberos to NULL above, and in any case we wish to log a
151 * more specific service target.
154 status = gensec_set_target_service_description(auth->gensec_security,
156 if (!NT_STATUS_IS_OK(status)) {
157 DEBUG(1, ("Failed to call gensec_set_target_service_description %s\n",
162 if (call->conn->remote_address != NULL) {
163 status = gensec_set_remote_address(auth->gensec_security,
164 call->conn->remote_address);
165 if (!NT_STATUS_IS_OK(status)) {
166 DEBUG(1, ("Failed to call gensec_set_remote_address() %s\n",
172 if (call->conn->local_address != NULL) {
173 status = gensec_set_local_address(auth->gensec_security,
174 call->conn->local_address);
175 if (!NT_STATUS_IS_OK(status)) {
176 DEBUG(1, ("Failed to call gensec_set_local_address() %s\n",
182 status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_type,
184 if (!NT_STATUS_IS_OK(status)) {
185 const char *backend_name =
186 gensec_get_name_by_authtype(auth->gensec_security,
189 DEBUG(3, ("Failed to start GENSEC mechanism for DCERPC server: "
190 "auth_type=%d (%s), auth_level=%d: %s\n",
191 (int)auth->auth_type, backend_name,
192 (int)auth->auth_level,
196 * Setting DCERPC_AUTH_LEVEL_NONE,
197 * gives the caller the reject_reason
198 * as auth_context_id.
200 * Note: DCERPC_AUTH_LEVEL_NONE == 1
202 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
203 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
204 if (backend_name != NULL) {
205 auth->auth_context_id =
206 DCERPC_BIND_NAK_REASON_INVALID_CHECKSUM;
208 auth->auth_context_id =
209 DCERPC_BIND_NAK_REASON_INVALID_AUTH_TYPE;
218 add any auth information needed in a bind ack, and process the authentication
219 information found in the bind.
221 NTSTATUS dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
223 struct dcesrv_connection *dce_conn = call->conn;
225 bool want_header_signing = false;
227 dce_conn->allow_alter = true;
228 dce_conn->allow_auth3 = true;
230 if (call->pkt.auth_length == 0) {
231 dce_conn->auth_state.auth_finished = true;
232 dce_conn->allow_request = true;
236 /* We can't work without an existing gensec state */
237 if (!call->conn->auth_state.gensec_security) {
238 return NT_STATUS_INTERNAL_ERROR;
241 if (call->pkt.pfc_flags & DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN) {
242 dce_conn->auth_state.client_hdr_signing = true;
243 want_header_signing = true;
246 if (!lpcfg_parm_bool(call->conn->dce_ctx->lp_ctx, NULL, "dcesrv","header signing", true)) {
247 want_header_signing = false;
250 call->_out_auth_info = (struct dcerpc_auth) {
251 .auth_type = dce_conn->auth_state.auth_type,
252 .auth_level = dce_conn->auth_state.auth_level,
253 .auth_context_id = dce_conn->auth_state.auth_context_id,
255 call->out_auth_info = &call->_out_auth_info;
257 status = gensec_update_ev(dce_conn->auth_state.gensec_security,
258 call, call->event_ctx,
259 call->in_auth_info.credentials,
260 &call->out_auth_info->credentials);
262 if (NT_STATUS_IS_OK(status)) {
263 status = gensec_session_info(dce_conn->auth_state.gensec_security,
265 &dce_conn->auth_state.session_info);
266 if (!NT_STATUS_IS_OK(status)) {
267 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
270 dce_conn->auth_state.auth_finished = true;
271 dce_conn->allow_request = true;
273 if (!gensec_have_feature(dce_conn->auth_state.gensec_security,
274 GENSEC_FEATURE_SIGN_PKT_HEADER))
276 want_header_signing = false;
279 if (want_header_signing) {
280 gensec_want_feature(dce_conn->auth_state.gensec_security,
281 GENSEC_FEATURE_SIGN_PKT_HEADER);
282 dce_conn->auth_state.hdr_signing = true;
283 pkt->pfc_flags |= DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN;
286 /* Now that we are authenticated, go back to the generic session key... */
287 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
289 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
290 if (!gensec_have_feature(dce_conn->auth_state.gensec_security,
291 GENSEC_FEATURE_SIGN_PKT_HEADER))
293 want_header_signing = false;
296 if (want_header_signing) {
297 gensec_want_feature(dce_conn->auth_state.gensec_security,
298 GENSEC_FEATURE_SIGN_PKT_HEADER);
299 dce_conn->auth_state.hdr_signing = true;
300 pkt->pfc_flags |= DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN;
305 DEBUG(4, ("GENSEC mech rejected the incoming authentication at bind_ack: %s\n",
313 process the final stage of a auth request
315 bool dcesrv_auth_auth3(struct dcesrv_call_state *call)
317 struct ncacn_packet *pkt = &call->pkt;
318 struct dcesrv_connection *dce_conn = call->conn;
321 if (pkt->auth_length == 0) {
325 if (dce_conn->auth_state.auth_finished) {
329 /* We can't work without an existing gensec state */
330 if (!dce_conn->auth_state.gensec_security) {
334 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.auth3.auth_info,
335 &call->in_auth_info, NULL, true);
336 if (!NT_STATUS_IS_OK(status)) {
338 * Windows returns DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY
339 * instead of DCERPC_NCA_S_PROTO_ERROR.
341 call->fault_code = DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY;
345 if (call->in_auth_info.auth_type != dce_conn->auth_state.auth_type) {
349 if (call->in_auth_info.auth_level != dce_conn->auth_state.auth_level) {
353 if (call->in_auth_info.auth_context_id != dce_conn->auth_state.auth_context_id) {
357 call->_out_auth_info = (struct dcerpc_auth) {
358 .auth_type = dce_conn->auth_state.auth_type,
359 .auth_level = dce_conn->auth_state.auth_level,
360 .auth_context_id = dce_conn->auth_state.auth_context_id,
362 call->out_auth_info = &call->_out_auth_info;
364 /* Pass the extra data we got from the client down to gensec for processing */
365 status = gensec_update_ev(dce_conn->auth_state.gensec_security,
366 call, call->event_ctx,
367 call->in_auth_info.credentials,
368 &call->out_auth_info->credentials);
369 if (NT_STATUS_IS_OK(status)) {
370 status = gensec_session_info(dce_conn->auth_state.gensec_security,
372 &dce_conn->auth_state.session_info);
373 if (!NT_STATUS_IS_OK(status)) {
374 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
377 dce_conn->auth_state.auth_finished = true;
378 dce_conn->allow_request = true;
380 /* Now that we are authenticated, go back to the generic session key... */
381 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
383 if (call->out_auth_info->credentials.length != 0) {
385 DEBUG(4, ("GENSEC produced output token (len=%u) at bind_auth3\n",
386 (unsigned)call->out_auth_info->credentials.length));
391 DEBUG(4, ("GENSEC mech rejected the incoming authentication at bind_auth3: %s\n",
398 parse any auth information from a dcerpc alter request
399 return false if we can't handle the auth request for some
400 reason (in which case we send a bind_nak (is this true for here?))
402 bool dcesrv_auth_alter(struct dcesrv_call_state *call)
404 struct ncacn_packet *pkt = &call->pkt;
405 struct dcesrv_connection *dce_conn = call->conn;
408 /* on a pure interface change there is no auth blob */
409 if (pkt->auth_length == 0) {
410 if (!dce_conn->auth_state.auth_finished) {
416 if (dce_conn->auth_state.auth_finished) {
417 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
421 /* We can't work without an existing gensec state */
422 if (!dce_conn->auth_state.gensec_security) {
426 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.alter.auth_info,
427 &call->in_auth_info, NULL, true);
428 if (!NT_STATUS_IS_OK(status)) {
429 call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
433 if (call->in_auth_info.auth_type == DCERPC_AUTH_TYPE_NONE) {
434 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
438 if (call->in_auth_info.auth_type != dce_conn->auth_state.auth_type) {
442 if (call->in_auth_info.auth_level != dce_conn->auth_state.auth_level) {
446 if (call->in_auth_info.auth_context_id != dce_conn->auth_state.auth_context_id) {
454 add any auth information needed in a alter ack, and process the authentication
455 information found in the alter.
457 NTSTATUS dcesrv_auth_alter_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
459 struct dcesrv_connection *dce_conn = call->conn;
462 /* on a pure interface change there is no auth_info structure
464 if (call->pkt.auth_length == 0) {
468 if (!call->conn->auth_state.gensec_security) {
469 return NT_STATUS_INTERNAL_ERROR;
472 call->_out_auth_info = (struct dcerpc_auth) {
473 .auth_type = dce_conn->auth_state.auth_type,
474 .auth_level = dce_conn->auth_state.auth_level,
475 .auth_context_id = dce_conn->auth_state.auth_context_id,
477 call->out_auth_info = &call->_out_auth_info;
479 status = gensec_update_ev(dce_conn->auth_state.gensec_security,
480 call, call->event_ctx,
481 call->in_auth_info.credentials,
482 &call->out_auth_info->credentials);
484 if (NT_STATUS_IS_OK(status)) {
485 status = gensec_session_info(dce_conn->auth_state.gensec_security,
487 &dce_conn->auth_state.session_info);
488 if (!NT_STATUS_IS_OK(status)) {
489 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
492 dce_conn->auth_state.auth_finished = true;
493 dce_conn->allow_request = true;
495 /* Now that we are authenticated, got back to the generic session key... */
496 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
498 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
502 DEBUG(4, ("GENSEC mech rejected the incoming authentication at auth alter_ack: %s\n",
508 check credentials on a packet
510 bool dcesrv_auth_pkt_pull(struct dcesrv_call_state *call,
511 DATA_BLOB *full_packet,
512 uint8_t required_flags,
513 uint8_t optional_flags,
514 uint8_t payload_offset,
515 DATA_BLOB *payload_and_verifier)
517 struct ncacn_packet *pkt = &call->pkt;
518 struct dcesrv_connection *dce_conn = call->conn;
519 const struct dcerpc_auth tmp_auth = {
520 .auth_type = dce_conn->auth_state.auth_type,
521 .auth_level = dce_conn->auth_state.auth_level,
522 .auth_context_id = dce_conn->auth_state.auth_context_id,
526 if (!dce_conn->allow_request) {
527 call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
531 if (dce_conn->auth_state.auth_invalid) {
535 status = dcerpc_ncacn_pull_pkt_auth(&tmp_auth,
536 dce_conn->auth_state.gensec_security,
542 payload_and_verifier,
545 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTOCOL_ERROR)) {
546 call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
549 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_UNSUPPORTED_AUTHN_LEVEL)) {
550 call->fault_code = DCERPC_NCA_S_UNSUPPORTED_AUTHN_LEVEL;
553 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_SEC_PKG_ERROR)) {
554 call->fault_code = DCERPC_FAULT_SEC_PKG_ERROR;
557 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
558 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
561 if (!NT_STATUS_IS_OK(status)) {
569 push a signed or sealed dcerpc request packet into a blob
571 bool dcesrv_auth_pkt_push(struct dcesrv_call_state *call,
572 DATA_BLOB *blob, size_t sig_size,
573 uint8_t payload_offset,
574 const DATA_BLOB *payload,
575 const struct ncacn_packet *pkt)
577 struct dcesrv_connection *dce_conn = call->conn;
578 const struct dcerpc_auth tmp_auth = {
579 .auth_type = dce_conn->auth_state.auth_type,
580 .auth_level = dce_conn->auth_state.auth_level,
581 .auth_context_id = dce_conn->auth_state.auth_context_id,
585 status = dcerpc_ncacn_push_pkt_auth(&tmp_auth,
586 dce_conn->auth_state.gensec_security,
587 call, blob, sig_size,
591 return NT_STATUS_IS_OK(status);