rpc: Always supply both the remote and local address to the auth subsystem
[Samba.git] / source4 / rpc_server / dcesrv_auth.c
blobece314dcbe6ec73c78a72a299aa281cc06123b3c
1 /*
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/>.
23 #include "includes.h"
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 NTSTATUS status;
48 if (pkt->auth_length == 0) {
49 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
50 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
51 auth->auth_context_id = 0;
52 return true;
55 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.bind.auth_info,
56 &call->in_auth_info,
57 NULL, true);
58 if (!NT_STATUS_IS_OK(status)) {
60 * Setting DCERPC_AUTH_LEVEL_NONE,
61 * gives the caller the reject_reason
62 * as auth_context_id.
64 * Note: DCERPC_AUTH_LEVEL_NONE == 1
66 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
67 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
68 auth->auth_context_id =
69 DCERPC_BIND_NAK_REASON_PROTOCOL_VERSION_NOT_SUPPORTED;
70 return false;
73 switch (call->in_auth_info.auth_level) {
74 case DCERPC_AUTH_LEVEL_CONNECT:
75 case DCERPC_AUTH_LEVEL_CALL:
76 case DCERPC_AUTH_LEVEL_PACKET:
77 case DCERPC_AUTH_LEVEL_INTEGRITY:
78 case DCERPC_AUTH_LEVEL_PRIVACY:
80 * We evaluate auth_type only if auth_level was valid
82 break;
83 default:
85 * Setting DCERPC_AUTH_LEVEL_NONE,
86 * gives the caller the reject_reason
87 * as auth_context_id.
89 * Note: DCERPC_AUTH_LEVEL_NONE == 1
91 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
92 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
93 auth->auth_context_id = DCERPC_BIND_NAK_REASON_NOT_SPECIFIED;
94 return false;
97 auth->auth_type = call->in_auth_info.auth_type;
98 auth->auth_level = call->in_auth_info.auth_level;
99 auth->auth_context_id = call->in_auth_info.auth_context_id;
101 server_credentials
102 = cli_credentials_init(call);
103 if (!server_credentials) {
104 DEBUG(1, ("Failed to init server credentials\n"));
105 return false;
108 cli_credentials_set_conf(server_credentials, call->conn->dce_ctx->lp_ctx);
109 status = cli_credentials_set_machine_account(server_credentials, call->conn->dce_ctx->lp_ctx);
110 if (!NT_STATUS_IS_OK(status)) {
111 DEBUG(1, ("Failed to obtain server credentials: %s\n",
112 nt_errstr(status)));
113 return false;
116 status = samba_server_gensec_start(dce_conn, call->event_ctx,
117 call->msg_ctx,
118 call->conn->dce_ctx->lp_ctx,
119 server_credentials,
120 NULL,
121 &auth->gensec_security);
122 if (!NT_STATUS_IS_OK(status)) {
123 DEBUG(1, ("Failed to call samba_server_gensec_start %s\n",
124 nt_errstr(status)));
125 return false;
129 * We have to call this because we set the target_service for
130 * Kerberos to NULL above, and in any case we wish to log a
131 * more specific service target.
134 status = gensec_set_target_service_description(auth->gensec_security,
135 "DCE/RPC");
136 if (!NT_STATUS_IS_OK(status)) {
137 DEBUG(1, ("Failed to call gensec_set_target_service_description %s\n",
138 nt_errstr(status)));
139 return false;
142 if (call->conn->remote_address != NULL) {
143 status = gensec_set_remote_address(auth->gensec_security,
144 call->conn->remote_address);
145 if (!NT_STATUS_IS_OK(status)) {
146 DEBUG(1, ("Failed to call gensec_set_remote_address() %s\n",
147 nt_errstr(status)));
148 return false;
152 if (call->conn->local_address != NULL) {
153 status = gensec_set_local_address(auth->gensec_security,
154 call->conn->local_address);
155 if (!NT_STATUS_IS_OK(status)) {
156 DEBUG(1, ("Failed to call gensec_set_local_address() %s\n",
157 nt_errstr(status)));
158 return false;
162 status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_type,
163 auth->auth_level);
164 if (!NT_STATUS_IS_OK(status)) {
165 const char *backend_name =
166 gensec_get_name_by_authtype(auth->gensec_security,
167 auth->auth_type);
169 DEBUG(3, ("Failed to start GENSEC mechanism for DCERPC server: "
170 "auth_type=%d (%s), auth_level=%d: %s\n",
171 (int)auth->auth_type, backend_name,
172 (int)auth->auth_level,
173 nt_errstr(status)));
176 * Setting DCERPC_AUTH_LEVEL_NONE,
177 * gives the caller the reject_reason
178 * as auth_context_id.
180 * Note: DCERPC_AUTH_LEVEL_NONE == 1
182 auth->auth_type = DCERPC_AUTH_TYPE_NONE;
183 auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
184 if (backend_name != NULL) {
185 auth->auth_context_id =
186 DCERPC_BIND_NAK_REASON_INVALID_CHECKSUM;
187 } else {
188 auth->auth_context_id =
189 DCERPC_BIND_NAK_REASON_INVALID_AUTH_TYPE;
191 return false;
194 return true;
198 add any auth information needed in a bind ack, and process the authentication
199 information found in the bind.
201 NTSTATUS dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
203 struct dcesrv_connection *dce_conn = call->conn;
204 NTSTATUS status;
205 bool want_header_signing = false;
207 dce_conn->allow_alter = true;
208 dce_conn->allow_auth3 = true;
210 if (call->pkt.auth_length == 0) {
211 dce_conn->auth_state.auth_finished = true;
212 dce_conn->allow_request = true;
213 return NT_STATUS_OK;
216 /* We can't work without an existing gensec state */
217 if (!call->conn->auth_state.gensec_security) {
218 return NT_STATUS_INTERNAL_ERROR;
221 if (call->pkt.pfc_flags & DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN) {
222 dce_conn->auth_state.client_hdr_signing = true;
223 want_header_signing = true;
226 if (!lpcfg_parm_bool(call->conn->dce_ctx->lp_ctx, NULL, "dcesrv","header signing", true)) {
227 want_header_signing = false;
230 call->_out_auth_info = (struct dcerpc_auth) {
231 .auth_type = dce_conn->auth_state.auth_type,
232 .auth_level = dce_conn->auth_state.auth_level,
233 .auth_context_id = dce_conn->auth_state.auth_context_id,
235 call->out_auth_info = &call->_out_auth_info;
237 status = gensec_update_ev(dce_conn->auth_state.gensec_security,
238 call, call->event_ctx,
239 call->in_auth_info.credentials,
240 &call->out_auth_info->credentials);
242 if (NT_STATUS_IS_OK(status)) {
243 status = gensec_session_info(dce_conn->auth_state.gensec_security,
244 dce_conn,
245 &dce_conn->auth_state.session_info);
246 if (!NT_STATUS_IS_OK(status)) {
247 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
248 return status;
250 dce_conn->auth_state.auth_finished = true;
251 dce_conn->allow_request = true;
253 if (!gensec_have_feature(dce_conn->auth_state.gensec_security,
254 GENSEC_FEATURE_SIGN_PKT_HEADER))
256 want_header_signing = false;
259 if (want_header_signing) {
260 gensec_want_feature(dce_conn->auth_state.gensec_security,
261 GENSEC_FEATURE_SIGN_PKT_HEADER);
262 dce_conn->auth_state.hdr_signing = true;
263 pkt->pfc_flags |= DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN;
266 /* Now that we are authenticated, go back to the generic session key... */
267 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
268 return NT_STATUS_OK;
269 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
270 if (!gensec_have_feature(dce_conn->auth_state.gensec_security,
271 GENSEC_FEATURE_SIGN_PKT_HEADER))
273 want_header_signing = false;
276 if (want_header_signing) {
277 gensec_want_feature(dce_conn->auth_state.gensec_security,
278 GENSEC_FEATURE_SIGN_PKT_HEADER);
279 dce_conn->auth_state.hdr_signing = true;
280 pkt->pfc_flags |= DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN;
283 return NT_STATUS_OK;
284 } else {
285 DEBUG(4, ("GENSEC mech rejected the incoming authentication at bind_ack: %s\n",
286 nt_errstr(status)));
287 return status;
293 process the final stage of a auth request
295 bool dcesrv_auth_auth3(struct dcesrv_call_state *call)
297 struct ncacn_packet *pkt = &call->pkt;
298 struct dcesrv_connection *dce_conn = call->conn;
299 NTSTATUS status;
301 if (pkt->auth_length == 0) {
302 return false;
305 if (dce_conn->auth_state.auth_finished) {
306 return false;
309 /* We can't work without an existing gensec state */
310 if (!dce_conn->auth_state.gensec_security) {
311 return false;
314 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.auth3.auth_info,
315 &call->in_auth_info, NULL, true);
316 if (!NT_STATUS_IS_OK(status)) {
318 * Windows returns DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY
319 * instead of DCERPC_NCA_S_PROTO_ERROR.
321 call->fault_code = DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY;
322 return false;
325 if (call->in_auth_info.auth_type != dce_conn->auth_state.auth_type) {
326 return false;
329 if (call->in_auth_info.auth_level != dce_conn->auth_state.auth_level) {
330 return false;
333 if (call->in_auth_info.auth_context_id != dce_conn->auth_state.auth_context_id) {
334 return false;
337 call->_out_auth_info = (struct dcerpc_auth) {
338 .auth_type = dce_conn->auth_state.auth_type,
339 .auth_level = dce_conn->auth_state.auth_level,
340 .auth_context_id = dce_conn->auth_state.auth_context_id,
342 call->out_auth_info = &call->_out_auth_info;
344 /* Pass the extra data we got from the client down to gensec for processing */
345 status = gensec_update_ev(dce_conn->auth_state.gensec_security,
346 call, call->event_ctx,
347 call->in_auth_info.credentials,
348 &call->out_auth_info->credentials);
349 if (NT_STATUS_IS_OK(status)) {
350 status = gensec_session_info(dce_conn->auth_state.gensec_security,
351 dce_conn,
352 &dce_conn->auth_state.session_info);
353 if (!NT_STATUS_IS_OK(status)) {
354 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
355 return false;
357 dce_conn->auth_state.auth_finished = true;
358 dce_conn->allow_request = true;
360 /* Now that we are authenticated, go back to the generic session key... */
361 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
363 if (call->out_auth_info->credentials.length != 0) {
365 DEBUG(4, ("GENSEC produced output token (len=%u) at bind_auth3\n",
366 (unsigned)call->out_auth_info->credentials.length));
367 return false;
369 return true;
370 } else {
371 DEBUG(4, ("GENSEC mech rejected the incoming authentication at bind_auth3: %s\n",
372 nt_errstr(status)));
373 return false;
378 parse any auth information from a dcerpc alter request
379 return false if we can't handle the auth request for some
380 reason (in which case we send a bind_nak (is this true for here?))
382 bool dcesrv_auth_alter(struct dcesrv_call_state *call)
384 struct ncacn_packet *pkt = &call->pkt;
385 struct dcesrv_connection *dce_conn = call->conn;
386 NTSTATUS status;
388 /* on a pure interface change there is no auth blob */
389 if (pkt->auth_length == 0) {
390 if (!dce_conn->auth_state.auth_finished) {
391 return false;
393 return true;
396 if (dce_conn->auth_state.auth_finished) {
397 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
398 return false;
401 /* We can't work without an existing gensec state */
402 if (!dce_conn->auth_state.gensec_security) {
403 return false;
406 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.alter.auth_info,
407 &call->in_auth_info, NULL, true);
408 if (!NT_STATUS_IS_OK(status)) {
409 call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
410 return false;
413 if (call->in_auth_info.auth_type == DCERPC_AUTH_TYPE_NONE) {
414 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
415 return false;
418 if (call->in_auth_info.auth_type != dce_conn->auth_state.auth_type) {
419 return false;
422 if (call->in_auth_info.auth_level != dce_conn->auth_state.auth_level) {
423 return false;
426 if (call->in_auth_info.auth_context_id != dce_conn->auth_state.auth_context_id) {
427 return false;
430 return true;
434 add any auth information needed in a alter ack, and process the authentication
435 information found in the alter.
437 NTSTATUS dcesrv_auth_alter_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
439 struct dcesrv_connection *dce_conn = call->conn;
440 NTSTATUS status;
442 /* on a pure interface change there is no auth_info structure
443 setup */
444 if (call->pkt.auth_length == 0) {
445 return NT_STATUS_OK;
448 if (!call->conn->auth_state.gensec_security) {
449 return NT_STATUS_INTERNAL_ERROR;
452 call->_out_auth_info = (struct dcerpc_auth) {
453 .auth_type = dce_conn->auth_state.auth_type,
454 .auth_level = dce_conn->auth_state.auth_level,
455 .auth_context_id = dce_conn->auth_state.auth_context_id,
457 call->out_auth_info = &call->_out_auth_info;
459 status = gensec_update_ev(dce_conn->auth_state.gensec_security,
460 call, call->event_ctx,
461 call->in_auth_info.credentials,
462 &call->out_auth_info->credentials);
464 if (NT_STATUS_IS_OK(status)) {
465 status = gensec_session_info(dce_conn->auth_state.gensec_security,
466 dce_conn,
467 &dce_conn->auth_state.session_info);
468 if (!NT_STATUS_IS_OK(status)) {
469 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
470 return status;
472 dce_conn->auth_state.auth_finished = true;
473 dce_conn->allow_request = true;
475 /* Now that we are authenticated, got back to the generic session key... */
476 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
477 return NT_STATUS_OK;
478 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
479 return NT_STATUS_OK;
482 DEBUG(4, ("GENSEC mech rejected the incoming authentication at auth alter_ack: %s\n",
483 nt_errstr(status)));
484 return status;
488 check credentials on a packet
490 bool dcesrv_auth_pkt_pull(struct dcesrv_call_state *call,
491 DATA_BLOB *full_packet,
492 uint8_t required_flags,
493 uint8_t optional_flags,
494 uint8_t payload_offset,
495 DATA_BLOB *payload_and_verifier)
497 struct ncacn_packet *pkt = &call->pkt;
498 struct dcesrv_connection *dce_conn = call->conn;
499 const struct dcerpc_auth tmp_auth = {
500 .auth_type = dce_conn->auth_state.auth_type,
501 .auth_level = dce_conn->auth_state.auth_level,
502 .auth_context_id = dce_conn->auth_state.auth_context_id,
504 NTSTATUS status;
506 if (!dce_conn->allow_request) {
507 call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
508 return false;
511 if (dce_conn->auth_state.auth_invalid) {
512 return false;
515 status = dcerpc_ncacn_pull_pkt_auth(&tmp_auth,
516 dce_conn->auth_state.gensec_security,
517 call,
518 pkt->ptype,
519 required_flags,
520 optional_flags,
521 payload_offset,
522 payload_and_verifier,
523 full_packet,
524 pkt);
525 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTOCOL_ERROR)) {
526 call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
527 return false;
529 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_UNSUPPORTED_AUTHN_LEVEL)) {
530 call->fault_code = DCERPC_NCA_S_UNSUPPORTED_AUTHN_LEVEL;
531 return false;
533 if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_SEC_PKG_ERROR)) {
534 call->fault_code = DCERPC_FAULT_SEC_PKG_ERROR;
535 return false;
537 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
538 call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
539 return false;
541 if (!NT_STATUS_IS_OK(status)) {
542 return false;
545 return true;
549 push a signed or sealed dcerpc request packet into a blob
551 bool dcesrv_auth_pkt_push(struct dcesrv_call_state *call,
552 DATA_BLOB *blob, size_t sig_size,
553 uint8_t payload_offset,
554 const DATA_BLOB *payload,
555 const struct ncacn_packet *pkt)
557 struct dcesrv_connection *dce_conn = call->conn;
558 const struct dcerpc_auth tmp_auth = {
559 .auth_type = dce_conn->auth_state.auth_type,
560 .auth_level = dce_conn->auth_state.auth_level,
561 .auth_context_id = dce_conn->auth_state.auth_context_id,
563 NTSTATUS status;
565 status = dcerpc_ncacn_push_pkt_auth(&tmp_auth,
566 dce_conn->auth_state.gensec_security,
567 call, blob, sig_size,
568 payload_offset,
569 payload,
570 pkt);
571 return NT_STATUS_IS_OK(status);