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"
35 static bool dcesrv_auth_prepare_gensec(struct dcesrv_call_state *call)
37 struct cli_credentials *server_credentials = NULL;
38 struct dcesrv_connection *dce_conn = call->conn;
39 struct dcesrv_auth *auth = call->auth_state;
40 bool want_header_signing = false;
43 if (auth->auth_invalid) {
47 if (auth->auth_finished) {
51 if (auth->gensec_security != NULL) {
55 switch (call->in_auth_info.auth_level) {
56 case DCERPC_AUTH_LEVEL_CONNECT:
57 case DCERPC_AUTH_LEVEL_CALL:
58 case DCERPC_AUTH_LEVEL_PACKET:
59 case DCERPC_AUTH_LEVEL_INTEGRITY:
60 case DCERPC_AUTH_LEVEL_PRIVACY:
62 * We evaluate auth_type only if auth_level was valid
67 * Setting DCERPC_AUTH_LEVEL_NONE,
68 * gives the caller the reject_reason
71 * Note: DCERPC_AUTH_LEVEL_NONE == 1
73 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
74 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
75 auth->auth_context_id = DCERPC_BIND_NAK_REASON_NOT_SPECIFIED;
79 auth->auth_type = call->in_auth_info.auth_type;
80 auth->auth_level = call->in_auth_info.auth_level;
81 auth->auth_context_id = call->in_auth_info.auth_context_id;
84 = cli_credentials_init(auth);
85 if (!server_credentials) {
86 DEBUG(1, ("Failed to init server credentials\n"));
90 cli_credentials_set_conf(server_credentials, call->conn->dce_ctx->lp_ctx);
91 status = cli_credentials_set_machine_account(server_credentials, call->conn->dce_ctx->lp_ctx);
92 if (!NT_STATUS_IS_OK(status)) {
93 DEBUG(1, ("Failed to obtain server credentials: %s\n",
98 status = samba_server_gensec_start(auth,
101 call->conn->dce_ctx->lp_ctx,
104 &auth->gensec_security);
105 if (!NT_STATUS_IS_OK(status)) {
106 DEBUG(1, ("Failed to call samba_server_gensec_start %s\n",
112 * We have to call this because we set the target_service for
113 * Kerberos to NULL above, and in any case we wish to log a
114 * more specific service target.
117 status = gensec_set_target_service_description(auth->gensec_security,
119 if (!NT_STATUS_IS_OK(status)) {
120 DEBUG(1, ("Failed to call gensec_set_target_service_description %s\n",
125 if (call->conn->remote_address != NULL) {
126 status = gensec_set_remote_address(auth->gensec_security,
127 call->conn->remote_address);
128 if (!NT_STATUS_IS_OK(status)) {
129 DEBUG(1, ("Failed to call gensec_set_remote_address() %s\n",
135 if (call->conn->local_address != NULL) {
136 status = gensec_set_local_address(auth->gensec_security,
137 call->conn->local_address);
138 if (!NT_STATUS_IS_OK(status)) {
139 DEBUG(1, ("Failed to call gensec_set_local_address() %s\n",
145 status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_type,
147 if (!NT_STATUS_IS_OK(status)) {
148 const char *backend_name =
149 gensec_get_name_by_authtype(auth->gensec_security,
152 DEBUG(3, ("Failed to start GENSEC mechanism for DCERPC server: "
153 "auth_type=%d (%s), auth_level=%d: %s\n",
154 (int)auth->auth_type, backend_name,
155 (int)auth->auth_level,
159 * Setting DCERPC_AUTH_LEVEL_NONE,
160 * gives the caller the reject_reason
161 * as auth_context_id.
163 * Note: DCERPC_AUTH_LEVEL_NONE == 1
165 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
166 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
167 if (backend_name != NULL) {
168 auth->auth_context_id =
169 DCERPC_BIND_NAK_REASON_INVALID_CHECKSUM;
171 auth->auth_context_id =
172 DCERPC_BIND_NAK_REASON_INVALID_AUTH_TYPE;
177 if (call->pkt.pfc_flags & DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN) {
178 auth->client_hdr_signing = true;
179 want_header_signing = true;
182 if (want_header_signing) {
183 want_header_signing = gensec_have_feature(auth->gensec_security,
184 GENSEC_FEATURE_SIGN_PKT_HEADER);
187 if (want_header_signing) {
188 want_header_signing = lpcfg_parm_bool(dce_conn->dce_ctx->lp_ctx,
195 if (want_header_signing) {
196 gensec_want_feature(auth->gensec_security,
197 GENSEC_FEATURE_SIGN_PKT_HEADER);
198 auth->hdr_signing = true;
204 static void log_successful_dcesrv_authz_event(struct dcesrv_call_state *call)
206 struct dcesrv_auth *auth = call->auth_state;
207 enum dcerpc_transport_t transport =
208 dcerpc_binding_get_transport(call->conn->endpoint->ep_description);
209 const char *auth_type = derpc_transport_string_by_transport(transport);
210 const char *transport_protection = AUTHZ_TRANSPORT_PROTECTION_NONE;
212 if (transport == NCACN_NP) {
213 transport_protection = AUTHZ_TRANSPORT_PROTECTION_SMB;
217 * Log the authorization to this RPC interface. This
218 * covered ncacn_np pass-through auth, and anonymous
219 * DCE/RPC (eg epmapper, netlogon etc)
221 log_successful_authz_event(call->conn->msg_ctx,
222 call->conn->dce_ctx->lp_ctx,
223 call->conn->remote_address,
224 call->conn->local_address,
227 transport_protection,
232 parse any auth information from a dcerpc bind request
233 return false if we can't handle the auth request for some
234 reason (in which case we send a bind_nak)
236 bool dcesrv_auth_bind(struct dcesrv_call_state *call)
238 struct ncacn_packet *pkt = &call->pkt;
239 struct dcesrv_auth *auth = call->auth_state;
242 if (pkt->auth_length == 0) {
243 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
244 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
245 auth->auth_context_id = 0;
247 log_successful_dcesrv_authz_event(call);
252 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.bind.auth_info,
255 if (!NT_STATUS_IS_OK(status)) {
257 * Setting DCERPC_AUTH_LEVEL_NONE,
258 * gives the caller the reject_reason
259 * as auth_context_id.
261 * Note: DCERPC_AUTH_LEVEL_NONE == 1
263 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
264 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
265 auth->auth_context_id =
266 DCERPC_BIND_NAK_REASON_PROTOCOL_VERSION_NOT_SUPPORTED;
270 return dcesrv_auth_prepare_gensec(call);
273 NTSTATUS dcesrv_auth_complete(struct dcesrv_call_state *call, NTSTATUS status)
275 struct dcesrv_connection *dce_conn = call->conn;
276 struct dcesrv_auth *auth = call->auth_state;
277 const char *pdu = "<unknown>";
279 switch (call->pkt.ptype) {
280 case DCERPC_PKT_BIND:
283 case DCERPC_PKT_ALTER:
286 case DCERPC_PKT_AUTH3:
288 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
289 DEBUG(4, ("GENSEC not finished at at %s\n", pdu));
290 return NT_STATUS_RPC_SEC_PKG_ERROR;
294 return NT_STATUS_INTERNAL_ERROR;
297 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
301 if (!NT_STATUS_IS_OK(status)) {
302 DEBUG(4, ("GENSEC mech rejected the incoming authentication "
303 "at %s: %s\n", pdu, nt_errstr(status)));
307 status = gensec_session_info(auth->gensec_security,
309 &auth->session_info);
310 if (!NT_STATUS_IS_OK(status)) {
311 DEBUG(1, ("Failed to establish session_info: %s\n",
315 auth->auth_finished = true;
316 dce_conn->allow_request = true;
318 if (call->pkt.ptype != DCERPC_PKT_AUTH3) {
322 if (call->out_auth_info->credentials.length != 0) {
323 DEBUG(4, ("GENSEC produced output token (len=%zu) at %s\n",
324 call->out_auth_info->credentials.length, pdu));
325 return NT_STATUS_RPC_SEC_PKG_ERROR;
332 add any auth information needed in a bind ack, and process the authentication
333 information found in the bind.
335 NTSTATUS dcesrv_auth_prepare_bind_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
337 struct dcesrv_connection *dce_conn = call->conn;
338 struct dcesrv_auth *auth = call->auth_state;
340 dce_conn->allow_alter = true;
341 dce_conn->allow_auth3 = true;
343 if (call->pkt.auth_length == 0) {
344 auth->auth_finished = true;
345 dce_conn->allow_request = true;
349 /* We can't work without an existing gensec state */
350 if (auth->gensec_security == NULL) {
351 return NT_STATUS_INTERNAL_ERROR;
354 if (auth->hdr_signing) {
355 pkt->pfc_flags |= DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN;
358 call->_out_auth_info = (struct dcerpc_auth) {
359 .auth_type = auth->auth_type,
360 .auth_level = auth->auth_level,
361 .auth_context_id = auth->auth_context_id,
363 call->out_auth_info = &call->_out_auth_info;
369 process the final stage of a auth request
371 bool dcesrv_auth_prepare_auth3(struct dcesrv_call_state *call)
373 struct ncacn_packet *pkt = &call->pkt;
374 struct dcesrv_auth *auth = call->auth_state;
377 if (pkt->auth_length == 0) {
381 if (auth->auth_finished) {
385 /* We can't work without an existing gensec state */
386 if (auth->gensec_security == NULL) {
390 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.auth3.auth_info,
391 &call->in_auth_info, NULL, true);
392 if (!NT_STATUS_IS_OK(status)) {
394 * Windows returns DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY
395 * instead of DCERPC_NCA_S_PROTO_ERROR.
397 call->fault_code = DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY;
401 if (call->in_auth_info.auth_type != auth->auth_type) {
405 if (call->in_auth_info.auth_level != auth->auth_level) {
409 if (call->in_auth_info.auth_context_id != auth->auth_context_id) {
413 call->_out_auth_info = (struct dcerpc_auth) {
414 .auth_type = auth->auth_type,
415 .auth_level = auth->auth_level,
416 .auth_context_id = auth->auth_context_id,
418 call->out_auth_info = &call->_out_auth_info;
424 parse any auth information from a dcerpc alter request
425 return false if we can't handle the auth request for some
426 reason (in which case we send a bind_nak (is this true for here?))
428 bool dcesrv_auth_alter(struct dcesrv_call_state *call)
430 struct ncacn_packet *pkt = &call->pkt;
431 struct dcesrv_auth *auth = call->auth_state;
434 /* on a pure interface change there is no auth blob */
435 if (pkt->auth_length == 0) {
436 if (!auth->auth_finished) {
442 if (auth->auth_finished) {
443 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
447 /* We can't work without an existing gensec state */
448 if (auth->gensec_security == NULL) {
452 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.alter.auth_info,
453 &call->in_auth_info, NULL, true);
454 if (!NT_STATUS_IS_OK(status)) {
455 call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
459 if (call->in_auth_info.auth_type == DCERPC_AUTH_TYPE_NONE) {
460 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
464 if (call->in_auth_info.auth_type != auth->auth_type) {
468 if (call->in_auth_info.auth_level != auth->auth_level) {
472 if (call->in_auth_info.auth_context_id != auth->auth_context_id) {
480 add any auth information needed in a alter ack, and process the authentication
481 information found in the alter.
483 NTSTATUS dcesrv_auth_prepare_alter_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
485 struct dcesrv_auth *auth = call->auth_state;
487 /* on a pure interface change there is no auth_info structure
489 if (call->pkt.auth_length == 0) {
493 if (auth->gensec_security == NULL) {
494 return NT_STATUS_INTERNAL_ERROR;
497 call->_out_auth_info = (struct dcerpc_auth) {
498 .auth_type = auth->auth_type,
499 .auth_level = auth->auth_level,
500 .auth_context_id = auth->auth_context_id,
502 call->out_auth_info = &call->_out_auth_info;
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 struct dcesrv_auth *auth = call->auth_state;
520 const struct dcerpc_auth tmp_auth = {
521 .auth_type = auth->auth_type,
522 .auth_level = auth->auth_level,
523 .auth_context_id = auth->auth_context_id,
527 if (!dce_conn->allow_request) {
528 call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
532 if (auth->auth_invalid) {
536 status = dcerpc_ncacn_pull_pkt_auth(&tmp_auth,
537 auth->gensec_security,
543 payload_and_verifier,
546 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTOCOL_ERROR)) {
547 call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
550 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_UNSUPPORTED_AUTHN_LEVEL)) {
551 call->fault_code = DCERPC_NCA_S_UNSUPPORTED_AUTHN_LEVEL;
554 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_SEC_PKG_ERROR)) {
555 call->fault_code = DCERPC_FAULT_SEC_PKG_ERROR;
558 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
559 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
562 if (!NT_STATUS_IS_OK(status)) {
570 push a signed or sealed dcerpc request packet into a blob
572 bool dcesrv_auth_pkt_push(struct dcesrv_call_state *call,
573 DATA_BLOB *blob, size_t sig_size,
574 uint8_t payload_offset,
575 const DATA_BLOB *payload,
576 const struct ncacn_packet *pkt)
578 struct dcesrv_auth *auth = call->auth_state;
579 const struct dcerpc_auth tmp_auth = {
580 .auth_type = auth->auth_type,
581 .auth_level = auth->auth_level,
582 .auth_context_id = auth->auth_context_id,
586 status = dcerpc_ncacn_push_pkt_auth(&tmp_auth,
587 auth->gensec_security,
588 call, blob, sig_size,
592 return NT_STATUS_IS_OK(status);