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;
49 if (pkt->auth_length == 0) {
50 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
51 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
52 auth->auth_context_id = 0;
56 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.bind.auth_info,
59 if (!NT_STATUS_IS_OK(status)) {
63 auth->auth_type = call->in_auth_info.auth_type;
64 auth->auth_level = call->in_auth_info.auth_level;
65 auth->auth_context_id = call->in_auth_info.auth_context_id;
68 = cli_credentials_init(call);
69 if (!server_credentials) {
70 DEBUG(1, ("Failed to init server credentials\n"));
74 cli_credentials_set_conf(server_credentials, call->conn->dce_ctx->lp_ctx);
75 status = cli_credentials_set_machine_account(server_credentials, call->conn->dce_ctx->lp_ctx);
76 if (!NT_STATUS_IS_OK(status)) {
77 DEBUG(1, ("Failed to obtain server credentials: %s\n",
82 status = samba_server_gensec_start(dce_conn, call->event_ctx,
84 call->conn->dce_ctx->lp_ctx,
87 &auth->gensec_security);
88 if (!NT_STATUS_IS_OK(status)) {
89 DEBUG(1, ("Failed to call samba_server_gensec_start %s\n",
94 if (call->conn->remote_address != NULL) {
95 status = gensec_set_remote_address(auth->gensec_security,
96 call->conn->remote_address);
97 if (!NT_STATUS_IS_OK(status)) {
98 DEBUG(1, ("Failed to call gensec_set_remote_address() %s\n",
104 status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_type,
106 if (!NT_STATUS_IS_OK(status)) {
107 DEBUG(3, ("Failed to start GENSEC mechanism for DCERPC server: auth_type=%d, auth_level=%d: %s\n",
108 (int)auth->auth_type,
109 (int)auth->auth_level,
118 add any auth information needed in a bind ack, and process the authentication
119 information found in the bind.
121 NTSTATUS dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
123 struct dcesrv_connection *dce_conn = call->conn;
125 bool want_header_signing = false;
127 dce_conn->allow_alter = true;
128 dce_conn->allow_auth3 = true;
130 if (call->pkt.auth_length == 0) {
131 dce_conn->auth_state.auth_finished = true;
132 dce_conn->allow_request = true;
136 /* We can't work without an existing gensec state */
137 if (!call->conn->auth_state.gensec_security) {
138 return NT_STATUS_INTERNAL_ERROR;
141 if (call->pkt.pfc_flags & DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN) {
142 dce_conn->auth_state.client_hdr_signing = true;
143 want_header_signing = true;
146 if (!lpcfg_parm_bool(call->conn->dce_ctx->lp_ctx, NULL, "dcesrv","header signing", true)) {
147 want_header_signing = false;
150 call->_out_auth_info = (struct dcerpc_auth) {
151 .auth_type = dce_conn->auth_state.auth_type,
152 .auth_level = dce_conn->auth_state.auth_level,
153 .auth_context_id = dce_conn->auth_state.auth_context_id,
155 call->out_auth_info = &call->_out_auth_info;
157 status = gensec_update_ev(dce_conn->auth_state.gensec_security,
158 call, call->event_ctx,
159 call->in_auth_info.credentials,
160 &call->out_auth_info->credentials);
162 if (NT_STATUS_IS_OK(status)) {
163 status = gensec_session_info(dce_conn->auth_state.gensec_security,
165 &dce_conn->auth_state.session_info);
166 if (!NT_STATUS_IS_OK(status)) {
167 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
170 dce_conn->auth_state.auth_finished = true;
171 dce_conn->allow_request = true;
173 if (!gensec_have_feature(dce_conn->auth_state.gensec_security,
174 GENSEC_FEATURE_SIGN_PKT_HEADER))
176 want_header_signing = false;
179 if (want_header_signing) {
180 gensec_want_feature(dce_conn->auth_state.gensec_security,
181 GENSEC_FEATURE_SIGN_PKT_HEADER);
182 dce_conn->auth_state.hdr_signing = true;
183 pkt->pfc_flags |= DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN;
186 /* Now that we are authenticated, go back to the generic session key... */
187 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
189 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
190 if (!gensec_have_feature(dce_conn->auth_state.gensec_security,
191 GENSEC_FEATURE_SIGN_PKT_HEADER))
193 want_header_signing = false;
196 if (want_header_signing) {
197 gensec_want_feature(dce_conn->auth_state.gensec_security,
198 GENSEC_FEATURE_SIGN_PKT_HEADER);
199 dce_conn->auth_state.hdr_signing = true;
200 pkt->pfc_flags |= DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN;
205 DEBUG(4, ("GENSEC mech rejected the incoming authentication at bind_ack: %s\n",
213 process the final stage of a auth request
215 bool dcesrv_auth_auth3(struct dcesrv_call_state *call)
217 struct ncacn_packet *pkt = &call->pkt;
218 struct dcesrv_connection *dce_conn = call->conn;
220 uint32_t auth_length;
222 if (pkt->auth_length == 0) {
226 if (dce_conn->auth_state.auth_finished) {
230 /* We can't work without an existing gensec state */
231 if (!dce_conn->auth_state.gensec_security) {
235 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.auth3.auth_info,
236 &call->in_auth_info, &auth_length, true);
237 if (!NT_STATUS_IS_OK(status)) {
241 call->_out_auth_info = (struct dcerpc_auth) {
242 .auth_type = dce_conn->auth_state.auth_type,
243 .auth_level = dce_conn->auth_state.auth_level,
244 .auth_context_id = dce_conn->auth_state.auth_context_id,
246 call->out_auth_info = &call->_out_auth_info;
248 /* Pass the extra data we got from the client down to gensec for processing */
249 status = gensec_update_ev(dce_conn->auth_state.gensec_security,
250 call, call->event_ctx,
251 call->in_auth_info.credentials,
252 &call->out_auth_info->credentials);
253 if (NT_STATUS_IS_OK(status)) {
254 status = gensec_session_info(dce_conn->auth_state.gensec_security,
256 &dce_conn->auth_state.session_info);
257 if (!NT_STATUS_IS_OK(status)) {
258 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
261 dce_conn->auth_state.auth_finished = true;
262 dce_conn->allow_request = true;
264 /* Now that we are authenticated, go back to the generic session key... */
265 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
268 DEBUG(4, ("GENSEC mech rejected the incoming authentication at bind_auth3: %s\n",
275 parse any auth information from a dcerpc alter request
276 return false if we can't handle the auth request for some
277 reason (in which case we send a bind_nak (is this true for here?))
279 bool dcesrv_auth_alter(struct dcesrv_call_state *call)
281 struct ncacn_packet *pkt = &call->pkt;
282 struct dcesrv_connection *dce_conn = call->conn;
284 uint32_t auth_length;
286 /* on a pure interface change there is no auth blob */
287 if (pkt->auth_length == 0) {
288 if (!dce_conn->auth_state.auth_finished) {
294 if (dce_conn->auth_state.auth_finished) {
298 /* We can't work without an existing gensec state */
299 if (!dce_conn->auth_state.gensec_security) {
303 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.alter.auth_info,
304 &call->in_auth_info, &auth_length, true);
305 if (!NT_STATUS_IS_OK(status)) {
313 add any auth information needed in a alter ack, and process the authentication
314 information found in the alter.
316 NTSTATUS dcesrv_auth_alter_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
318 struct dcesrv_connection *dce_conn = call->conn;
321 /* on a pure interface change there is no auth_info structure
323 if (call->pkt.auth_length == 0) {
327 if (!call->conn->auth_state.gensec_security) {
328 return NT_STATUS_INTERNAL_ERROR;
331 call->_out_auth_info = (struct dcerpc_auth) {
332 .auth_type = dce_conn->auth_state.auth_type,
333 .auth_level = dce_conn->auth_state.auth_level,
334 .auth_context_id = dce_conn->auth_state.auth_context_id,
336 call->out_auth_info = &call->_out_auth_info;
338 status = gensec_update_ev(dce_conn->auth_state.gensec_security,
339 call, call->event_ctx,
340 call->in_auth_info.credentials,
341 &call->out_auth_info->credentials);
343 if (NT_STATUS_IS_OK(status)) {
344 status = gensec_session_info(dce_conn->auth_state.gensec_security,
346 &dce_conn->auth_state.session_info);
347 if (!NT_STATUS_IS_OK(status)) {
348 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
351 dce_conn->auth_state.auth_finished = true;
352 dce_conn->allow_request = true;
354 /* Now that we are authenticated, got back to the generic session key... */
355 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
357 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
361 DEBUG(4, ("GENSEC mech rejected the incoming authentication at auth alter_ack: %s\n",
367 check credentials on a request
369 bool dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
371 struct ncacn_packet *pkt = &call->pkt;
372 struct dcesrv_connection *dce_conn = call->conn;
374 uint32_t auth_length;
375 size_t hdr_size = DCERPC_REQUEST_LENGTH;
377 if (!dce_conn->allow_request) {
381 if (pkt->pfc_flags & DCERPC_PFC_FLAG_OBJECT_UUID) {
385 switch (dce_conn->auth_state.auth_level) {
386 case DCERPC_AUTH_LEVEL_PRIVACY:
387 case DCERPC_AUTH_LEVEL_INTEGRITY:
390 case DCERPC_AUTH_LEVEL_CONNECT:
391 if (pkt->auth_length != 0) {
395 case DCERPC_AUTH_LEVEL_NONE:
396 if (pkt->auth_length != 0) {
405 if (!dce_conn->auth_state.gensec_security) {
409 status = dcerpc_pull_auth_trailer(pkt, call,
410 &pkt->u.request.stub_and_verifier,
411 &call->in_auth_info, &auth_length, false);
412 if (!NT_STATUS_IS_OK(status)) {
416 if (call->in_auth_info.auth_type != dce_conn->auth_state.auth_type) {
420 if (call->in_auth_info.auth_level != dce_conn->auth_state.auth_level) {
424 if (call->in_auth_info.auth_context_id != dce_conn->auth_state.auth_context_id) {
428 pkt->u.request.stub_and_verifier.length -= auth_length;
430 /* check signature or unseal the packet */
431 switch (dce_conn->auth_state.auth_level) {
432 case DCERPC_AUTH_LEVEL_PRIVACY:
433 status = gensec_unseal_packet(dce_conn->auth_state.gensec_security,
434 full_packet->data + hdr_size,
435 pkt->u.request.stub_and_verifier.length,
438 call->in_auth_info.credentials.length,
439 &call->in_auth_info.credentials);
440 memcpy(pkt->u.request.stub_and_verifier.data,
441 full_packet->data + hdr_size,
442 pkt->u.request.stub_and_verifier.length);
445 case DCERPC_AUTH_LEVEL_INTEGRITY:
446 status = gensec_check_packet(dce_conn->auth_state.gensec_security,
447 pkt->u.request.stub_and_verifier.data,
448 pkt->u.request.stub_and_verifier.length,
451 call->in_auth_info.credentials.length,
452 &call->in_auth_info.credentials);
455 case DCERPC_AUTH_LEVEL_CONNECT:
456 /* for now we ignore possible signatures here */
457 status = NT_STATUS_OK;
461 status = NT_STATUS_INVALID_LEVEL;
465 /* remove the indicated amount of padding */
466 if (pkt->u.request.stub_and_verifier.length < call->in_auth_info.auth_pad_length) {
469 pkt->u.request.stub_and_verifier.length -= call->in_auth_info.auth_pad_length;
471 return NT_STATUS_IS_OK(status);
476 push a signed or sealed dcerpc request packet into a blob
478 bool dcesrv_auth_response(struct dcesrv_call_state *call,
479 DATA_BLOB *blob, size_t sig_size,
480 struct ncacn_packet *pkt)
482 struct dcesrv_connection *dce_conn = call->conn;
484 enum ndr_err_code ndr_err;
485 struct ndr_push *ndr;
486 uint32_t payload_length;
489 switch (dce_conn->auth_state.auth_level) {
490 case DCERPC_AUTH_LEVEL_PRIVACY:
491 case DCERPC_AUTH_LEVEL_INTEGRITY:
498 case DCERPC_AUTH_LEVEL_CONNECT:
500 * TODO: let the gensec mech decide if it wants to generate a
501 * signature that might be needed for schannel...
503 status = ncacn_push_auth(blob, call, pkt, NULL);
504 return NT_STATUS_IS_OK(status);
506 case DCERPC_AUTH_LEVEL_NONE:
507 status = ncacn_push_auth(blob, call, pkt, NULL);
508 return NT_STATUS_IS_OK(status);
514 if (!dce_conn->auth_state.gensec_security) {
518 ndr = ndr_push_init_ctx(call);
523 if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
524 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
527 ndr_err = ndr_push_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
528 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
532 call->_out_auth_info = (struct dcerpc_auth) {
533 .auth_type = dce_conn->auth_state.auth_type,
534 .auth_level = dce_conn->auth_state.auth_level,
535 .auth_context_id = dce_conn->auth_state.auth_context_id,
537 call->out_auth_info = &call->_out_auth_info;
539 /* pad to 16 byte multiple in the payload portion of the
540 packet. This matches what w2k3 does. Note that we can't use
541 ndr_push_align() as that is relative to the start of the
542 whole packet, whereas w2k8 wants it relative to the start
544 call->out_auth_info->auth_pad_length =
545 DCERPC_AUTH_PAD_LENGTH(pkt->u.response.stub_and_verifier.length);
546 ndr_err = ndr_push_zero(ndr, call->out_auth_info->auth_pad_length);
547 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
551 payload_length = pkt->u.response.stub_and_verifier.length +
552 call->out_auth_info->auth_pad_length;
554 /* add the auth verifier */
555 ndr_err = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS,
556 call->out_auth_info);
557 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
561 /* extract the whole packet as a blob */
562 *blob = ndr_push_blob(ndr);
565 * Setup the frag and auth length in the packet buffer.
566 * This is needed if the GENSEC mech does AEAD signing
567 * of the packet headers. The signature itself will be
570 dcerpc_set_frag_length(blob, blob->length + sig_size);
571 dcerpc_set_auth_length(blob, sig_size);
573 /* sign or seal the packet */
574 switch (dce_conn->auth_state.auth_level) {
575 case DCERPC_AUTH_LEVEL_PRIVACY:
576 status = gensec_seal_packet(dce_conn->auth_state.gensec_security,
578 ndr->data + DCERPC_REQUEST_LENGTH,
585 case DCERPC_AUTH_LEVEL_INTEGRITY:
586 status = gensec_sign_packet(dce_conn->auth_state.gensec_security,
588 ndr->data + DCERPC_REQUEST_LENGTH,
596 status = NT_STATUS_INVALID_LEVEL;
600 if (!NT_STATUS_IS_OK(status)) {
604 if (creds2.length != sig_size) {
605 DEBUG(3,("dcesrv_auth_response: creds2.length[%u] != sig_size[%u] pad[%u] stub[%u]\n",
606 (unsigned)creds2.length, (uint32_t)sig_size,
607 (unsigned)call->out_auth_info->auth_pad_length,
608 (unsigned)pkt->u.response.stub_and_verifier.length));
609 dcerpc_set_frag_length(blob, blob->length + creds2.length);
610 dcerpc_set_auth_length(blob, creds2.length);
613 if (!data_blob_append(call, blob, creds2.data, creds2.length)) {
614 status = NT_STATUS_NO_MEMORY;
617 data_blob_free(&creds2);