docs: mention AD prerequirements for using idmap_ad
[Samba.git] / source4 / rpc_server / dcesrv_auth.c
blob1e6aa24c82097bc2b99265183adbad1138ed9629
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;
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;
47 uint32_t auth_length;
49 if (pkt->u.bind.auth_info.length == 0) {
50 dce_conn->auth_state.auth_info = NULL;
51 return true;
54 dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
55 if (!dce_conn->auth_state.auth_info) {
56 return false;
59 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.bind.auth_info,
60 dce_conn->auth_state.auth_info,
61 &auth_length, false);
62 server_credentials
63 = cli_credentials_init(call);
64 if (!server_credentials) {
65 DEBUG(1, ("Failed to init server credentials\n"));
66 return false;
69 cli_credentials_set_conf(server_credentials, call->conn->dce_ctx->lp_ctx);
70 status = cli_credentials_set_machine_account(server_credentials, call->conn->dce_ctx->lp_ctx);
71 if (!NT_STATUS_IS_OK(status)) {
72 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
73 talloc_free(server_credentials);
74 server_credentials = NULL;
77 status = samba_server_gensec_start(dce_conn, call->event_ctx,
78 call->msg_ctx,
79 call->conn->dce_ctx->lp_ctx,
80 server_credentials,
81 NULL,
82 &auth->gensec_security);
84 status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_info->auth_type,
85 auth->auth_info->auth_level);
87 if (!NT_STATUS_IS_OK(status)) {
88 DEBUG(3, ("Failed to start GENSEC mechanism for DCERPC server: auth_type=%d, auth_level=%d: %s\n",
89 (int)auth->auth_info->auth_type,
90 (int)auth->auth_info->auth_level,
91 nt_errstr(status)));
92 return false;
95 if (call->conn->state_flags & DCESRV_CALL_STATE_FLAG_HEADER_SIGNING) {
96 gensec_want_feature(auth->gensec_security, GENSEC_FEATURE_SIGN_PKT_HEADER);
99 return true;
103 add any auth information needed in a bind ack, and process the authentication
104 information found in the bind.
106 NTSTATUS dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
108 struct dcesrv_connection *dce_conn = call->conn;
109 NTSTATUS status;
111 if (!call->conn->auth_state.gensec_security) {
112 return NT_STATUS_OK;
115 status = gensec_update(dce_conn->auth_state.gensec_security,
116 call,
117 dce_conn->auth_state.auth_info->credentials,
118 &dce_conn->auth_state.auth_info->credentials);
120 if (NT_STATUS_IS_OK(status)) {
121 status = gensec_session_info(dce_conn->auth_state.gensec_security,
122 &dce_conn->auth_state.session_info);
123 if (!NT_STATUS_IS_OK(status)) {
124 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
125 return status;
128 if (dce_conn->state_flags & DCESRV_CALL_STATE_FLAG_HEADER_SIGNING) {
129 gensec_want_feature(dce_conn->auth_state.gensec_security,
130 GENSEC_FEATURE_SIGN_PKT_HEADER);
133 /* Now that we are authenticated, go back to the generic session key... */
134 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
135 return NT_STATUS_OK;
136 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
137 dce_conn->auth_state.auth_info->auth_pad_length = 0;
138 dce_conn->auth_state.auth_info->auth_reserved = 0;
139 return NT_STATUS_OK;
140 } else {
141 DEBUG(4, ("GENSEC mech rejected the incoming authentication at bind_ack: %s\n",
142 nt_errstr(status)));
143 return status;
149 process the final stage of a auth request
151 bool dcesrv_auth_auth3(struct dcesrv_call_state *call)
153 struct ncacn_packet *pkt = &call->pkt;
154 struct dcesrv_connection *dce_conn = call->conn;
155 NTSTATUS status;
156 uint32_t auth_length;
158 /* We can't work without an existing gensec state, and an new blob to feed it */
159 if (!dce_conn->auth_state.auth_info ||
160 !dce_conn->auth_state.gensec_security ||
161 pkt->u.auth3.auth_info.length == 0) {
162 return false;
165 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.auth3.auth_info,
166 dce_conn->auth_state.auth_info, &auth_length, true);
167 if (!NT_STATUS_IS_OK(status)) {
168 return false;
171 /* Pass the extra data we got from the client down to gensec for processing */
172 status = gensec_update(dce_conn->auth_state.gensec_security,
173 call,
174 dce_conn->auth_state.auth_info->credentials,
175 &dce_conn->auth_state.auth_info->credentials);
176 if (NT_STATUS_IS_OK(status)) {
177 status = gensec_session_info(dce_conn->auth_state.gensec_security,
178 &dce_conn->auth_state.session_info);
179 if (!NT_STATUS_IS_OK(status)) {
180 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
181 return false;
183 /* Now that we are authenticated, go back to the generic session key... */
184 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
185 return true;
186 } else {
187 DEBUG(4, ("GENSEC mech rejected the incoming authentication at bind_auth3: %s\n",
188 nt_errstr(status)));
189 return false;
194 parse any auth information from a dcerpc alter request
195 return false if we can't handle the auth request for some
196 reason (in which case we send a bind_nak (is this true for here?))
198 bool dcesrv_auth_alter(struct dcesrv_call_state *call)
200 struct ncacn_packet *pkt = &call->pkt;
201 struct dcesrv_connection *dce_conn = call->conn;
202 NTSTATUS status;
203 uint32_t auth_length;
205 /* on a pure interface change there is no auth blob */
206 if (pkt->u.alter.auth_info.length == 0) {
207 return true;
210 /* We can't work without an existing gensec state */
211 if (!dce_conn->auth_state.gensec_security) {
212 return false;
215 dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
216 if (!dce_conn->auth_state.auth_info) {
217 return false;
220 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.alter.auth_info,
221 dce_conn->auth_state.auth_info,
222 &auth_length, true);
223 if (!NT_STATUS_IS_OK(status)) {
224 return false;
227 return true;
231 add any auth information needed in a alter ack, and process the authentication
232 information found in the alter.
234 NTSTATUS dcesrv_auth_alter_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
236 struct dcesrv_connection *dce_conn = call->conn;
237 NTSTATUS status;
239 /* on a pure interface change there is no auth_info structure
240 setup */
241 if (!call->conn->auth_state.auth_info ||
242 dce_conn->auth_state.auth_info->credentials.length == 0) {
243 return NT_STATUS_OK;
246 if (!call->conn->auth_state.gensec_security) {
247 return NT_STATUS_INVALID_PARAMETER;
250 status = gensec_update(dce_conn->auth_state.gensec_security,
251 call,
252 dce_conn->auth_state.auth_info->credentials,
253 &dce_conn->auth_state.auth_info->credentials);
255 if (NT_STATUS_IS_OK(status)) {
256 status = gensec_session_info(dce_conn->auth_state.gensec_security,
257 &dce_conn->auth_state.session_info);
258 if (!NT_STATUS_IS_OK(status)) {
259 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
260 return status;
263 /* Now that we are authenticated, got back to the generic session key... */
264 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
265 return NT_STATUS_OK;
266 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
267 dce_conn->auth_state.auth_info->auth_pad_length = 0;
268 dce_conn->auth_state.auth_info->auth_reserved = 0;
269 return NT_STATUS_OK;
272 DEBUG(4, ("GENSEC mech rejected the incoming authentication at auth alter_ack: %s\n",
273 nt_errstr(status)));
274 return status;
278 check credentials on a request
280 bool dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
282 struct ncacn_packet *pkt = &call->pkt;
283 struct dcesrv_connection *dce_conn = call->conn;
284 struct dcerpc_auth auth;
285 NTSTATUS status;
286 uint32_t auth_length;
287 size_t hdr_size = DCERPC_REQUEST_LENGTH;
289 if (!dce_conn->auth_state.auth_info ||
290 !dce_conn->auth_state.gensec_security) {
291 return true;
294 if (pkt->pfc_flags & DCERPC_PFC_FLAG_OBJECT_UUID) {
295 hdr_size += 16;
298 switch (dce_conn->auth_state.auth_info->auth_level) {
299 case DCERPC_AUTH_LEVEL_PRIVACY:
300 case DCERPC_AUTH_LEVEL_INTEGRITY:
301 break;
303 case DCERPC_AUTH_LEVEL_CONNECT:
304 if (pkt->auth_length != 0) {
305 break;
307 return true;
308 case DCERPC_AUTH_LEVEL_NONE:
309 if (pkt->auth_length != 0) {
310 return false;
312 return true;
314 default:
315 return false;
318 status = dcerpc_pull_auth_trailer(pkt, call,
319 &pkt->u.request.stub_and_verifier,
320 &auth, &auth_length, false);
321 if (!NT_STATUS_IS_OK(status)) {
322 return false;
325 pkt->u.request.stub_and_verifier.length -= auth_length;
327 /* check signature or unseal the packet */
328 switch (dce_conn->auth_state.auth_info->auth_level) {
329 case DCERPC_AUTH_LEVEL_PRIVACY:
330 status = gensec_unseal_packet(dce_conn->auth_state.gensec_security,
331 call,
332 full_packet->data + hdr_size,
333 pkt->u.request.stub_and_verifier.length,
334 full_packet->data,
335 full_packet->length-auth.credentials.length,
336 &auth.credentials);
337 memcpy(pkt->u.request.stub_and_verifier.data,
338 full_packet->data + hdr_size,
339 pkt->u.request.stub_and_verifier.length);
340 break;
342 case DCERPC_AUTH_LEVEL_INTEGRITY:
343 status = gensec_check_packet(dce_conn->auth_state.gensec_security,
344 call,
345 pkt->u.request.stub_and_verifier.data,
346 pkt->u.request.stub_and_verifier.length,
347 full_packet->data,
348 full_packet->length-auth.credentials.length,
349 &auth.credentials);
350 break;
352 case DCERPC_AUTH_LEVEL_CONNECT:
353 /* for now we ignore possible signatures here */
354 status = NT_STATUS_OK;
355 break;
357 default:
358 status = NT_STATUS_INVALID_LEVEL;
359 break;
362 /* remove the indicated amount of padding */
363 if (pkt->u.request.stub_and_verifier.length < auth.auth_pad_length) {
364 return false;
366 pkt->u.request.stub_and_verifier.length -= auth.auth_pad_length;
368 return NT_STATUS_IS_OK(status);
373 push a signed or sealed dcerpc request packet into a blob
375 bool dcesrv_auth_response(struct dcesrv_call_state *call,
376 DATA_BLOB *blob, size_t sig_size,
377 struct ncacn_packet *pkt)
379 struct dcesrv_connection *dce_conn = call->conn;
380 NTSTATUS status;
381 enum ndr_err_code ndr_err;
382 struct ndr_push *ndr;
383 uint32_t payload_length;
384 DATA_BLOB creds2;
386 /* non-signed packets are simple */
387 if (sig_size == 0) {
388 status = ncacn_push_auth(blob, call, pkt, NULL);
389 return NT_STATUS_IS_OK(status);
392 switch (dce_conn->auth_state.auth_info->auth_level) {
393 case DCERPC_AUTH_LEVEL_PRIVACY:
394 case DCERPC_AUTH_LEVEL_INTEGRITY:
395 break;
397 case DCERPC_AUTH_LEVEL_CONNECT:
399 * TODO: let the gensec mech decide if it wants to generate a
400 * signature that might be needed for schannel...
402 status = ncacn_push_auth(blob, call, pkt, NULL);
403 return NT_STATUS_IS_OK(status);
405 case DCERPC_AUTH_LEVEL_NONE:
406 status = ncacn_push_auth(blob, call, pkt, NULL);
407 return NT_STATUS_IS_OK(status);
409 default:
410 return false;
413 ndr = ndr_push_init_ctx(call);
414 if (!ndr) {
415 return false;
418 if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
419 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
422 ndr_err = ndr_push_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
423 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
424 return false;
427 /* pad to 16 byte multiple in the payload portion of the
428 packet. This matches what w2k3 does. Note that we can't use
429 ndr_push_align() as that is relative to the start of the
430 whole packet, whereas w2k8 wants it relative to the start
431 of the stub */
432 dce_conn->auth_state.auth_info->auth_pad_length =
433 (16 - (pkt->u.response.stub_and_verifier.length & 15)) & 15;
434 ndr_err = ndr_push_zero(ndr,
435 dce_conn->auth_state.auth_info->auth_pad_length);
436 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
437 return false;
440 payload_length = pkt->u.response.stub_and_verifier.length +
441 dce_conn->auth_state.auth_info->auth_pad_length;
443 /* we start without signature, it will appended later */
444 dce_conn->auth_state.auth_info->credentials = data_blob(NULL, 0);
446 /* add the auth verifier */
447 ndr_err = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS,
448 dce_conn->auth_state.auth_info);
449 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
450 return false;
453 /* extract the whole packet as a blob */
454 *blob = ndr_push_blob(ndr);
457 * Setup the frag and auth length in the packet buffer.
458 * This is needed if the GENSEC mech does AEAD signing
459 * of the packet headers. The signature itself will be
460 * appended later.
462 dcerpc_set_frag_length(blob, blob->length + sig_size);
463 dcerpc_set_auth_length(blob, sig_size);
465 /* sign or seal the packet */
466 switch (dce_conn->auth_state.auth_info->auth_level) {
467 case DCERPC_AUTH_LEVEL_PRIVACY:
468 status = gensec_seal_packet(dce_conn->auth_state.gensec_security,
469 call,
470 ndr->data + DCERPC_REQUEST_LENGTH,
471 payload_length,
472 blob->data,
473 blob->length,
474 &creds2);
475 break;
477 case DCERPC_AUTH_LEVEL_INTEGRITY:
478 status = gensec_sign_packet(dce_conn->auth_state.gensec_security,
479 call,
480 ndr->data + DCERPC_REQUEST_LENGTH,
481 payload_length,
482 blob->data,
483 blob->length,
484 &creds2);
485 break;
487 default:
488 status = NT_STATUS_INVALID_LEVEL;
489 break;
492 if (!NT_STATUS_IS_OK(status)) {
493 return false;
496 if (creds2.length != sig_size) {
497 DEBUG(3,("dcesrv_auth_response: creds2.length[%u] != sig_size[%u] pad[%u] stub[%u]\n",
498 (unsigned)creds2.length, (uint32_t)sig_size,
499 (unsigned)dce_conn->auth_state.auth_info->auth_pad_length,
500 (unsigned)pkt->u.response.stub_and_verifier.length));
501 dcerpc_set_frag_length(blob, blob->length + creds2.length);
502 dcerpc_set_auth_length(blob, creds2.length);
505 if (!data_blob_append(call, blob, creds2.data, creds2.length)) {
506 status = NT_STATUS_NO_MEMORY;
507 return false;
509 data_blob_free(&creds2);
511 return true;