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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "rpc_server/dcerpc_server.h"
26 #include "librpc/gen_ndr/ndr_dcerpc.h"
27 #include "auth/gensec/gensec.h"
30 parse any auth information from a dcerpc bind request
31 return False if we can't handle the auth request for some
32 reason (in which case we send a bind_nak)
34 BOOL dcesrv_auth_bind(struct dcesrv_call_state *call)
36 struct cli_credentials *server_credentials;
37 struct ncacn_packet *pkt = &call->pkt;
38 struct dcesrv_connection *dce_conn = call->conn;
39 struct dcesrv_auth *auth = &dce_conn->auth_state;
42 if (pkt->u.bind.auth_info.length == 0) {
43 dce_conn->auth_state.auth_info = NULL;
47 dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
48 if (!dce_conn->auth_state.auth_info) {
52 status = ndr_pull_struct_blob(&pkt->u.bind.auth_info,
54 dce_conn->auth_state.auth_info,
55 (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
56 if (!NT_STATUS_IS_OK(status)) {
60 status = gensec_server_start(dce_conn, &auth->gensec_security, call->event_ctx);
61 if (!NT_STATUS_IS_OK(status)) {
62 DEBUG(1, ("Failed to start GENSEC for DCERPC server: %s\n", nt_errstr(status)));
67 = cli_credentials_init(call);
68 if (!server_credentials) {
69 DEBUG(1, ("Failed to init server credentials\n"));
73 cli_credentials_set_conf(server_credentials);
74 status = cli_credentials_set_machine_account(server_credentials);
75 if (!NT_STATUS_IS_OK(status)) {
76 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
77 talloc_free(server_credentials);
78 server_credentials = NULL;
81 gensec_set_credentials(auth->gensec_security, server_credentials);
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(1, ("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,
98 add any auth information needed in a bind ack, and process the authentication
99 information found in the bind.
101 BOOL dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
103 struct dcesrv_connection *dce_conn = call->conn;
106 if (!call->conn->auth_state.gensec_security) {
110 status = gensec_update(dce_conn->auth_state.gensec_security,
112 dce_conn->auth_state.auth_info->credentials,
113 &dce_conn->auth_state.auth_info->credentials);
115 if (NT_STATUS_IS_OK(status)) {
116 status = gensec_session_info(dce_conn->auth_state.gensec_security,
117 &dce_conn->auth_state.session_info);
118 if (!NT_STATUS_IS_OK(status)) {
119 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
123 /* Now that we are authenticated, go back to the generic session key... */
124 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
126 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
127 dce_conn->auth_state.auth_info->auth_pad_length = 0;
128 dce_conn->auth_state.auth_info->auth_reserved = 0;
131 DEBUG(2, ("Failed to start dcesrv auth negotiate: %s\n", nt_errstr(status)));
138 process the final stage of a auth request
140 BOOL dcesrv_auth_auth3(struct dcesrv_call_state *call)
142 struct ncacn_packet *pkt = &call->pkt;
143 struct dcesrv_connection *dce_conn = call->conn;
146 /* We can't work without an existing gensec state, and an new blob to feed it */
147 if (!dce_conn->auth_state.auth_info ||
148 !dce_conn->auth_state.gensec_security ||
149 pkt->u.auth3.auth_info.length == 0) {
153 status = ndr_pull_struct_blob(&pkt->u.auth3.auth_info,
155 dce_conn->auth_state.auth_info,
156 (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
157 if (!NT_STATUS_IS_OK(status)) {
161 /* Pass the extra data we got from the client down to gensec for processing */
162 status = gensec_update(dce_conn->auth_state.gensec_security,
164 dce_conn->auth_state.auth_info->credentials,
165 &dce_conn->auth_state.auth_info->credentials);
166 if (NT_STATUS_IS_OK(status)) {
167 status = gensec_session_info(dce_conn->auth_state.gensec_security,
168 &dce_conn->auth_state.session_info);
169 if (!NT_STATUS_IS_OK(status)) {
170 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
173 /* Now that we are authenticated, go back to the generic session key... */
174 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
177 DEBUG(4, ("dcesrv_auth_auth3: failed to authenticate: %s\n",
186 parse any auth information from a dcerpc alter request
187 return False if we can't handle the auth request for some
188 reason (in which case we send a bind_nak (is this true for here?))
190 BOOL dcesrv_auth_alter(struct dcesrv_call_state *call)
192 struct ncacn_packet *pkt = &call->pkt;
193 struct dcesrv_connection *dce_conn = call->conn;
196 /* on a pure interface change there is no auth blob */
197 if (pkt->u.alter.auth_info.length == 0) {
201 /* We can't work without an existing gensec state */
202 if (!dce_conn->auth_state.gensec_security) {
206 dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
207 if (!dce_conn->auth_state.auth_info) {
211 status = ndr_pull_struct_blob(&pkt->u.alter.auth_info,
213 dce_conn->auth_state.auth_info,
214 (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
215 if (!NT_STATUS_IS_OK(status)) {
223 add any auth information needed in a alter ack, and process the authentication
224 information found in the alter.
226 BOOL dcesrv_auth_alter_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
228 struct dcesrv_connection *dce_conn = call->conn;
231 /* on a pure interface change there is no auth_info structure
233 if (!call->conn->auth_state.auth_info) {
237 if (!call->conn->auth_state.gensec_security) {
241 status = gensec_update(dce_conn->auth_state.gensec_security,
243 dce_conn->auth_state.auth_info->credentials,
244 &dce_conn->auth_state.auth_info->credentials);
246 if (NT_STATUS_IS_OK(status)) {
247 status = gensec_session_info(dce_conn->auth_state.gensec_security,
248 &dce_conn->auth_state.session_info);
249 if (!NT_STATUS_IS_OK(status)) {
250 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
254 /* Now that we are authenticated, got back to the generic session key... */
255 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
257 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
258 dce_conn->auth_state.auth_info->auth_pad_length = 0;
259 dce_conn->auth_state.auth_info->auth_reserved = 0;
262 DEBUG(2, ("Failed to finish dcesrv auth alter_ack: %s\n", nt_errstr(status)));
268 generate a CONNECT level verifier
270 static NTSTATUS dcesrv_connect_verifier(TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
272 *blob = data_blob_talloc(mem_ctx, NULL, 16);
273 if (blob->data == NULL) {
274 return NT_STATUS_NO_MEMORY;
276 SIVAL(blob->data, 0, 1);
277 memset(blob->data+4, 0, 12);
282 generate a CONNECT level verifier
284 static NTSTATUS dcesrv_check_connect_verifier(DATA_BLOB *blob)
286 if (blob->length != 16 ||
287 IVAL(blob->data, 0) != 1) {
288 return NT_STATUS_ACCESS_DENIED;
295 check credentials on a request
297 BOOL dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
299 struct ncacn_packet *pkt = &call->pkt;
300 struct dcesrv_connection *dce_conn = call->conn;
302 struct dcerpc_auth auth;
303 struct ndr_pull *ndr;
306 if (!dce_conn->auth_state.auth_info ||
307 !dce_conn->auth_state.gensec_security) {
311 auth_blob.length = 8 + pkt->auth_length;
313 /* check for a valid length */
314 if (pkt->u.request.stub_and_verifier.length < auth_blob.length) {
319 pkt->u.request.stub_and_verifier.data +
320 pkt->u.request.stub_and_verifier.length - auth_blob.length;
321 pkt->u.request.stub_and_verifier.length -= auth_blob.length;
323 /* pull the auth structure */
324 ndr = ndr_pull_init_blob(&auth_blob, call);
329 if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
330 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
333 status = ndr_pull_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, &auth);
334 if (!NT_STATUS_IS_OK(status)) {
339 /* check signature or unseal the packet */
340 switch (dce_conn->auth_state.auth_info->auth_level) {
341 case DCERPC_AUTH_LEVEL_PRIVACY:
342 status = gensec_unseal_packet(dce_conn->auth_state.gensec_security,
344 full_packet->data + DCERPC_REQUEST_LENGTH,
345 pkt->u.request.stub_and_verifier.length,
347 full_packet->length-auth.credentials.length,
349 memcpy(pkt->u.request.stub_and_verifier.data,
350 full_packet->data + DCERPC_REQUEST_LENGTH,
351 pkt->u.request.stub_and_verifier.length);
354 case DCERPC_AUTH_LEVEL_INTEGRITY:
355 status = gensec_check_packet(dce_conn->auth_state.gensec_security,
357 pkt->u.request.stub_and_verifier.data,
358 pkt->u.request.stub_and_verifier.length,
360 full_packet->length-auth.credentials.length,
364 case DCERPC_AUTH_LEVEL_CONNECT:
365 status = dcesrv_check_connect_verifier(&auth.credentials);
369 status = NT_STATUS_INVALID_LEVEL;
373 /* remove the indicated amount of padding */
374 if (pkt->u.request.stub_and_verifier.length < auth.auth_pad_length) {
378 pkt->u.request.stub_and_verifier.length -= auth.auth_pad_length;
381 return NT_STATUS_IS_OK(status);
386 push a signed or sealed dcerpc request packet into a blob
388 BOOL dcesrv_auth_response(struct dcesrv_call_state *call,
389 DATA_BLOB *blob, struct ncacn_packet *pkt)
391 struct dcesrv_connection *dce_conn = call->conn;
393 struct ndr_push *ndr;
394 uint32_t payload_length;
396 /* non-signed packets are simple */
397 if (!dce_conn->auth_state.auth_info || !dce_conn->auth_state.gensec_security) {
398 status = ncacn_push_auth(blob, call, pkt, NULL);
399 return NT_STATUS_IS_OK(status);
402 ndr = ndr_push_init_ctx(call);
407 if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
408 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
411 status = ndr_push_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
412 if (!NT_STATUS_IS_OK(status)) {
416 /* pad to 16 byte multiple, match win2k3 */
417 dce_conn->auth_state.auth_info->auth_pad_length = NDR_ALIGN(ndr, 16);
418 ndr_push_zero(ndr, dce_conn->auth_state.auth_info->auth_pad_length);
420 payload_length = ndr->offset - DCERPC_REQUEST_LENGTH;
422 if (dce_conn->auth_state.auth_info->auth_level == DCERPC_AUTH_LEVEL_CONNECT) {
423 status = dcesrv_connect_verifier(call,
424 &dce_conn->auth_state.auth_info->credentials);
425 if (!NT_STATUS_IS_OK(status)) {
429 dce_conn->auth_state.auth_info->credentials
430 = data_blob_talloc(call, NULL,
431 gensec_sig_size(dce_conn->auth_state.gensec_security,
435 /* add the auth verifier */
436 status = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, dce_conn->auth_state.auth_info);
437 if (!NT_STATUS_IS_OK(status)) {
441 /* extract the whole packet as a blob */
442 *blob = ndr_push_blob(ndr);
444 /* fill in the fragment length and auth_length, we can't fill
445 in these earlier as we don't know the signature length (it
446 could be variable length) */
447 dcerpc_set_frag_length(blob, blob->length);
448 dcerpc_set_auth_length(blob, dce_conn->auth_state.auth_info->credentials.length);
450 /* sign or seal the packet */
451 switch (dce_conn->auth_state.auth_info->auth_level) {
452 case DCERPC_AUTH_LEVEL_PRIVACY:
453 status = gensec_seal_packet(dce_conn->auth_state.gensec_security,
455 ndr->data + DCERPC_REQUEST_LENGTH,
458 blob->length - dce_conn->auth_state.auth_info->credentials.length,
459 &dce_conn->auth_state.auth_info->credentials);
462 case DCERPC_AUTH_LEVEL_INTEGRITY:
463 status = gensec_sign_packet(dce_conn->auth_state.gensec_security,
465 ndr->data + DCERPC_REQUEST_LENGTH,
468 blob->length - dce_conn->auth_state.auth_info->credentials.length,
469 &dce_conn->auth_state.auth_info->credentials);
473 case DCERPC_AUTH_LEVEL_CONNECT:
477 status = NT_STATUS_INVALID_LEVEL;
481 if (!NT_STATUS_IS_OK(status)) {
485 memcpy(blob->data + blob->length - dce_conn->auth_state.auth_info->credentials.length,
486 dce_conn->auth_state.auth_info->credentials.data, dce_conn->auth_state.auth_info->credentials.length);
488 data_blob_free(&dce_conn->auth_state.auth_info->credentials);