smbprinting: fix wrong == in shell tests
[Samba/gebeck_regimport.git] / source4 / rpc_server / dcesrv_auth.c
blob4c91fcb392b8d3b210b932f323f1b380b4d4dfb8
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"
35 parse any auth information from a dcerpc bind request
36 return false if we can't handle the auth request for some
37 reason (in which case we send a bind_nak)
39 bool dcesrv_auth_bind(struct dcesrv_call_state *call)
41 struct cli_credentials *server_credentials;
42 struct ncacn_packet *pkt = &call->pkt;
43 struct dcesrv_connection *dce_conn = call->conn;
44 struct dcesrv_auth *auth = &dce_conn->auth_state;
45 NTSTATUS status;
46 uint32_t auth_length;
48 if (pkt->u.bind.auth_info.length == 0) {
49 dce_conn->auth_state.auth_info = NULL;
50 return true;
53 dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
54 if (!dce_conn->auth_state.auth_info) {
55 return false;
58 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.bind.auth_info,
59 dce_conn->auth_state.auth_info,
60 &auth_length, false);
61 server_credentials
62 = cli_credentials_init(call);
63 if (!server_credentials) {
64 DEBUG(1, ("Failed to init server credentials\n"));
65 return false;
68 cli_credentials_set_conf(server_credentials, call->conn->dce_ctx->lp_ctx);
69 status = cli_credentials_set_machine_account(server_credentials, call->conn->dce_ctx->lp_ctx);
70 if (!NT_STATUS_IS_OK(status)) {
71 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
72 talloc_free(server_credentials);
73 server_credentials = NULL;
76 status = samba_server_gensec_start(dce_conn, call->event_ctx,
77 call->msg_ctx,
78 call->conn->dce_ctx->lp_ctx,
79 server_credentials,
80 NULL,
81 &auth->gensec_security);
83 status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_info->auth_type,
84 auth->auth_info->auth_level);
86 if (!NT_STATUS_IS_OK(status)) {
87 DEBUG(3, ("Failed to start GENSEC mechanism for DCERPC server: auth_type=%d, auth_level=%d: %s\n",
88 (int)auth->auth_info->auth_type,
89 (int)auth->auth_info->auth_level,
90 nt_errstr(status)));
91 return false;
94 if (call->conn->state_flags & DCESRV_CALL_STATE_FLAG_HEADER_SIGNING) {
95 gensec_want_feature(auth->gensec_security, GENSEC_FEATURE_SIGN_PKT_HEADER);
98 return true;
102 add any auth information needed in a bind ack, and process the authentication
103 information found in the bind.
105 NTSTATUS dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
107 struct dcesrv_connection *dce_conn = call->conn;
108 NTSTATUS status;
110 if (!call->conn->auth_state.gensec_security) {
111 return NT_STATUS_OK;
114 status = gensec_update(dce_conn->auth_state.gensec_security,
115 call,
116 dce_conn->auth_state.auth_info->credentials,
117 &dce_conn->auth_state.auth_info->credentials);
119 if (NT_STATUS_IS_OK(status)) {
120 status = gensec_session_info(dce_conn->auth_state.gensec_security,
121 &dce_conn->auth_state.session_info);
122 if (!NT_STATUS_IS_OK(status)) {
123 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
124 return status;
127 if (dce_conn->state_flags & DCESRV_CALL_STATE_FLAG_HEADER_SIGNING) {
128 gensec_want_feature(dce_conn->auth_state.gensec_security,
129 GENSEC_FEATURE_SIGN_PKT_HEADER);
132 /* Now that we are authenticated, go back to the generic session key... */
133 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
134 return NT_STATUS_OK;
135 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
136 dce_conn->auth_state.auth_info->auth_pad_length = 0;
137 dce_conn->auth_state.auth_info->auth_reserved = 0;
138 return NT_STATUS_OK;
139 } else {
140 DEBUG(4, ("GENSEC mech rejected the incoming authentication at bind_ack: %s\n",
141 nt_errstr(status)));
142 return status;
148 process the final stage of a auth request
150 bool dcesrv_auth_auth3(struct dcesrv_call_state *call)
152 struct ncacn_packet *pkt = &call->pkt;
153 struct dcesrv_connection *dce_conn = call->conn;
154 NTSTATUS status;
155 uint32_t auth_length;
157 /* We can't work without an existing gensec state, and an new blob to feed it */
158 if (!dce_conn->auth_state.auth_info ||
159 !dce_conn->auth_state.gensec_security ||
160 pkt->u.auth3.auth_info.length == 0) {
161 return false;
164 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.auth3.auth_info,
165 dce_conn->auth_state.auth_info, &auth_length, true);
166 if (!NT_STATUS_IS_OK(status)) {
167 return false;
170 /* Pass the extra data we got from the client down to gensec for processing */
171 status = gensec_update(dce_conn->auth_state.gensec_security,
172 call,
173 dce_conn->auth_state.auth_info->credentials,
174 &dce_conn->auth_state.auth_info->credentials);
175 if (NT_STATUS_IS_OK(status)) {
176 status = gensec_session_info(dce_conn->auth_state.gensec_security,
177 &dce_conn->auth_state.session_info);
178 if (!NT_STATUS_IS_OK(status)) {
179 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
180 return false;
182 /* Now that we are authenticated, go back to the generic session key... */
183 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
184 return true;
185 } else {
186 DEBUG(4, ("GENSEC mech rejected the incoming authentication at bind_auth3: %s\n",
187 nt_errstr(status)));
188 return false;
193 parse any auth information from a dcerpc alter request
194 return false if we can't handle the auth request for some
195 reason (in which case we send a bind_nak (is this true for here?))
197 bool dcesrv_auth_alter(struct dcesrv_call_state *call)
199 struct ncacn_packet *pkt = &call->pkt;
200 struct dcesrv_connection *dce_conn = call->conn;
201 NTSTATUS status;
202 uint32_t auth_length;
204 /* on a pure interface change there is no auth blob */
205 if (pkt->u.alter.auth_info.length == 0) {
206 return true;
209 /* We can't work without an existing gensec state */
210 if (!dce_conn->auth_state.gensec_security) {
211 return false;
214 dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
215 if (!dce_conn->auth_state.auth_info) {
216 return false;
219 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.alter.auth_info,
220 dce_conn->auth_state.auth_info,
221 &auth_length, true);
222 if (!NT_STATUS_IS_OK(status)) {
223 return false;
226 return true;
230 add any auth information needed in a alter ack, and process the authentication
231 information found in the alter.
233 NTSTATUS dcesrv_auth_alter_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
235 struct dcesrv_connection *dce_conn = call->conn;
236 NTSTATUS status;
238 /* on a pure interface change there is no auth_info structure
239 setup */
240 if (!call->conn->auth_state.auth_info ||
241 dce_conn->auth_state.auth_info->credentials.length == 0) {
242 return NT_STATUS_OK;
245 if (!call->conn->auth_state.gensec_security) {
246 return NT_STATUS_INVALID_PARAMETER;
249 status = gensec_update(dce_conn->auth_state.gensec_security,
250 call,
251 dce_conn->auth_state.auth_info->credentials,
252 &dce_conn->auth_state.auth_info->credentials);
254 if (NT_STATUS_IS_OK(status)) {
255 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)));
259 return status;
262 /* Now that we are authenticated, got back to the generic session key... */
263 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
264 return NT_STATUS_OK;
265 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
266 dce_conn->auth_state.auth_info->auth_pad_length = 0;
267 dce_conn->auth_state.auth_info->auth_reserved = 0;
268 return NT_STATUS_OK;
271 DEBUG(4, ("GENSEC mech rejected the incoming authentication at auth alter_ack: %s\n",
272 nt_errstr(status)));
273 return status;
277 check credentials on a request
279 bool dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
281 struct ncacn_packet *pkt = &call->pkt;
282 struct dcesrv_connection *dce_conn = call->conn;
283 struct dcerpc_auth auth;
284 NTSTATUS status;
285 uint32_t auth_length;
286 size_t hdr_size = DCERPC_REQUEST_LENGTH;
288 if (!dce_conn->auth_state.auth_info ||
289 !dce_conn->auth_state.gensec_security) {
290 return true;
293 if (pkt->pfc_flags & DCERPC_PFC_FLAG_OBJECT_UUID) {
294 hdr_size += 16;
297 switch (dce_conn->auth_state.auth_info->auth_level) {
298 case DCERPC_AUTH_LEVEL_PRIVACY:
299 case DCERPC_AUTH_LEVEL_INTEGRITY:
300 break;
302 case DCERPC_AUTH_LEVEL_CONNECT:
303 if (pkt->auth_length != 0) {
304 break;
306 return true;
307 case DCERPC_AUTH_LEVEL_NONE:
308 if (pkt->auth_length != 0) {
309 return false;
311 return true;
313 default:
314 return false;
317 status = dcerpc_pull_auth_trailer(pkt, call,
318 &pkt->u.request.stub_and_verifier,
319 &auth, &auth_length, false);
320 if (!NT_STATUS_IS_OK(status)) {
321 return false;
324 pkt->u.request.stub_and_verifier.length -= auth_length;
326 /* check signature or unseal the packet */
327 switch (dce_conn->auth_state.auth_info->auth_level) {
328 case DCERPC_AUTH_LEVEL_PRIVACY:
329 status = gensec_unseal_packet(dce_conn->auth_state.gensec_security,
330 call,
331 full_packet->data + hdr_size,
332 pkt->u.request.stub_and_verifier.length,
333 full_packet->data,
334 full_packet->length-auth.credentials.length,
335 &auth.credentials);
336 memcpy(pkt->u.request.stub_and_verifier.data,
337 full_packet->data + hdr_size,
338 pkt->u.request.stub_and_verifier.length);
339 break;
341 case DCERPC_AUTH_LEVEL_INTEGRITY:
342 status = gensec_check_packet(dce_conn->auth_state.gensec_security,
343 call,
344 pkt->u.request.stub_and_verifier.data,
345 pkt->u.request.stub_and_verifier.length,
346 full_packet->data,
347 full_packet->length-auth.credentials.length,
348 &auth.credentials);
349 break;
351 case DCERPC_AUTH_LEVEL_CONNECT:
352 /* for now we ignore possible signatures here */
353 status = NT_STATUS_OK;
354 break;
356 default:
357 status = NT_STATUS_INVALID_LEVEL;
358 break;
361 /* remove the indicated amount of padding */
362 if (pkt->u.request.stub_and_verifier.length < auth.auth_pad_length) {
363 return false;
365 pkt->u.request.stub_and_verifier.length -= auth.auth_pad_length;
367 return NT_STATUS_IS_OK(status);
372 push a signed or sealed dcerpc request packet into a blob
374 bool dcesrv_auth_response(struct dcesrv_call_state *call,
375 DATA_BLOB *blob, size_t sig_size,
376 struct ncacn_packet *pkt)
378 struct dcesrv_connection *dce_conn = call->conn;
379 NTSTATUS status;
380 enum ndr_err_code ndr_err;
381 struct ndr_push *ndr;
382 uint32_t payload_length;
383 DATA_BLOB creds2;
385 /* non-signed packets are simple */
386 if (sig_size == 0) {
387 status = ncacn_push_auth(blob, call, pkt, NULL);
388 return NT_STATUS_IS_OK(status);
391 switch (dce_conn->auth_state.auth_info->auth_level) {
392 case DCERPC_AUTH_LEVEL_PRIVACY:
393 case DCERPC_AUTH_LEVEL_INTEGRITY:
394 break;
396 case DCERPC_AUTH_LEVEL_CONNECT:
398 * TODO: let the gensec mech decide if it wants to generate a
399 * signature that might be needed for schannel...
401 status = ncacn_push_auth(blob, call, pkt, NULL);
402 return NT_STATUS_IS_OK(status);
404 case DCERPC_AUTH_LEVEL_NONE:
405 status = ncacn_push_auth(blob, call, pkt, NULL);
406 return NT_STATUS_IS_OK(status);
408 default:
409 return false;
412 ndr = ndr_push_init_ctx(call);
413 if (!ndr) {
414 return false;
417 if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
418 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
421 ndr_err = ndr_push_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
422 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
423 return false;
426 /* pad to 16 byte multiple in the payload portion of the
427 packet. This matches what w2k3 does. Note that we can't use
428 ndr_push_align() as that is relative to the start of the
429 whole packet, whereas w2k8 wants it relative to the start
430 of the stub */
431 dce_conn->auth_state.auth_info->auth_pad_length =
432 (16 - (pkt->u.response.stub_and_verifier.length & 15)) & 15;
433 ndr_err = ndr_push_zero(ndr,
434 dce_conn->auth_state.auth_info->auth_pad_length);
435 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
436 return false;
439 payload_length = pkt->u.response.stub_and_verifier.length +
440 dce_conn->auth_state.auth_info->auth_pad_length;
442 /* we start without signature, it will appended later */
443 dce_conn->auth_state.auth_info->credentials = data_blob(NULL, 0);
445 /* add the auth verifier */
446 ndr_err = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS,
447 dce_conn->auth_state.auth_info);
448 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
449 return false;
452 /* extract the whole packet as a blob */
453 *blob = ndr_push_blob(ndr);
456 * Setup the frag and auth length in the packet buffer.
457 * This is needed if the GENSEC mech does AEAD signing
458 * of the packet headers. The signature itself will be
459 * appended later.
461 dcerpc_set_frag_length(blob, blob->length + sig_size);
462 dcerpc_set_auth_length(blob, sig_size);
464 /* sign or seal the packet */
465 switch (dce_conn->auth_state.auth_info->auth_level) {
466 case DCERPC_AUTH_LEVEL_PRIVACY:
467 status = gensec_seal_packet(dce_conn->auth_state.gensec_security,
468 call,
469 ndr->data + DCERPC_REQUEST_LENGTH,
470 payload_length,
471 blob->data,
472 blob->length,
473 &creds2);
474 break;
476 case DCERPC_AUTH_LEVEL_INTEGRITY:
477 status = gensec_sign_packet(dce_conn->auth_state.gensec_security,
478 call,
479 ndr->data + DCERPC_REQUEST_LENGTH,
480 payload_length,
481 blob->data,
482 blob->length,
483 &creds2);
484 break;
486 default:
487 status = NT_STATUS_INVALID_LEVEL;
488 break;
491 if (!NT_STATUS_IS_OK(status)) {
492 return false;
495 if (creds2.length != sig_size) {
496 DEBUG(3,("dcesrv_auth_response: creds2.length[%u] != sig_size[%u] pad[%u] stub[%u]\n",
497 (unsigned)creds2.length, (uint32_t)sig_size,
498 (unsigned)dce_conn->auth_state.auth_info->auth_pad_length,
499 (unsigned)pkt->u.response.stub_and_verifier.length));
500 dcerpc_set_frag_length(blob, blob->length + creds2.length);
501 dcerpc_set_auth_length(blob, creds2.length);
504 if (!data_blob_append(call, blob, creds2.data, creds2.length)) {
505 status = NT_STATUS_NO_MEMORY;
506 return false;
508 data_blob_free(&creds2);
510 return true;