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"
29 parse any auth information from a dcerpc bind request
30 return False if we can't handle the auth request for some
31 reason (in which case we send a bind_nak)
33 BOOL dcesrv_auth_bind(struct dcesrv_call_state *call)
35 struct cli_credentials *server_credentials;
36 struct ncacn_packet *pkt = &call->pkt;
37 struct dcesrv_connection *dce_conn = call->conn;
38 struct dcesrv_auth *auth = &dce_conn->auth_state;
41 if (pkt->u.bind.auth_info.length == 0) {
42 dce_conn->auth_state.auth_info = NULL;
46 dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
47 if (!dce_conn->auth_state.auth_info) {
51 status = ndr_pull_struct_blob(&pkt->u.bind.auth_info,
53 dce_conn->auth_state.auth_info,
54 (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
55 if (!NT_STATUS_IS_OK(status)) {
59 status = gensec_server_start(dce_conn, &auth->gensec_security, call->event_ctx);
60 if (!NT_STATUS_IS_OK(status)) {
61 DEBUG(1, ("Failed to start GENSEC for DCERPC server: %s\n", nt_errstr(status)));
66 = cli_credentials_init(call);
67 if (!server_credentials) {
68 DEBUG(1, ("Failed to init server credentials\n"));
72 cli_credentials_set_conf(server_credentials);
73 status = cli_credentials_set_machine_account(server_credentials);
74 if (!NT_STATUS_IS_OK(status)) {
75 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
76 talloc_free(server_credentials);
77 server_credentials = NULL;
80 gensec_set_credentials(auth->gensec_security, server_credentials);
82 status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_info->auth_type,
83 auth->auth_info->auth_level);
85 if (!NT_STATUS_IS_OK(status)) {
86 DEBUG(1, ("Failed to start GENSEC mechanism for DCERPC server: auth_type=%d, auth_level=%d: %s\n",
87 (int)auth->auth_info->auth_type,
88 (int)auth->auth_info->auth_level,
97 add any auth information needed in a bind ack, and process the authentication
98 information found in the bind.
100 BOOL dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
102 struct dcesrv_connection *dce_conn = call->conn;
105 if (!call->conn->auth_state.gensec_security) {
109 status = gensec_update(dce_conn->auth_state.gensec_security,
111 dce_conn->auth_state.auth_info->credentials,
112 &dce_conn->auth_state.auth_info->credentials);
114 if (NT_STATUS_IS_OK(status)) {
115 status = gensec_session_info(dce_conn->auth_state.gensec_security,
116 &dce_conn->auth_state.session_info);
117 if (!NT_STATUS_IS_OK(status)) {
118 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
122 /* Now that we are authenticated, go back to the generic session key... */
123 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
125 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
126 dce_conn->auth_state.auth_info->auth_pad_length = 0;
127 dce_conn->auth_state.auth_info->auth_reserved = 0;
130 DEBUG(2, ("Failed to start dcesrv auth negotiate: %s\n", nt_errstr(status)));
137 process the final stage of a auth request
139 BOOL dcesrv_auth_auth3(struct dcesrv_call_state *call)
141 struct ncacn_packet *pkt = &call->pkt;
142 struct dcesrv_connection *dce_conn = call->conn;
145 /* We can't work without an existing gensec state, and an new blob to feed it */
146 if (!dce_conn->auth_state.auth_info ||
147 !dce_conn->auth_state.gensec_security ||
148 pkt->u.auth3.auth_info.length == 0) {
152 status = ndr_pull_struct_blob(&pkt->u.auth3.auth_info,
154 dce_conn->auth_state.auth_info,
155 (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
156 if (!NT_STATUS_IS_OK(status)) {
160 /* Pass the extra data we got from the client down to gensec for processing */
161 status = gensec_update(dce_conn->auth_state.gensec_security,
163 dce_conn->auth_state.auth_info->credentials,
164 &dce_conn->auth_state.auth_info->credentials);
165 if (NT_STATUS_IS_OK(status)) {
166 status = gensec_session_info(dce_conn->auth_state.gensec_security,
167 &dce_conn->auth_state.session_info);
168 if (!NT_STATUS_IS_OK(status)) {
169 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
172 /* Now that we are authenticated, go back to the generic session key... */
173 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
176 DEBUG(4, ("dcesrv_auth_auth3: failed to authenticate: %s\n",
185 parse any auth information from a dcerpc alter request
186 return False if we can't handle the auth request for some
187 reason (in which case we send a bind_nak (is this true for here?))
189 BOOL dcesrv_auth_alter(struct dcesrv_call_state *call)
191 struct ncacn_packet *pkt = &call->pkt;
192 struct dcesrv_connection *dce_conn = call->conn;
195 /* on a pure interface change there is no auth blob */
196 if (pkt->u.alter.auth_info.length == 0) {
200 /* We can't work without an existing gensec state */
201 if (!dce_conn->auth_state.gensec_security) {
205 dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
206 if (!dce_conn->auth_state.auth_info) {
210 status = ndr_pull_struct_blob(&pkt->u.alter.auth_info,
212 dce_conn->auth_state.auth_info,
213 (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
214 if (!NT_STATUS_IS_OK(status)) {
222 add any auth information needed in a alter ack, and process the authentication
223 information found in the alter.
225 BOOL dcesrv_auth_alter_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
227 struct dcesrv_connection *dce_conn = call->conn;
230 /* on a pure interface change there is no auth_info structure
232 if (!call->conn->auth_state.auth_info) {
236 if (!call->conn->auth_state.gensec_security) {
240 status = gensec_update(dce_conn->auth_state.gensec_security,
242 dce_conn->auth_state.auth_info->credentials,
243 &dce_conn->auth_state.auth_info->credentials);
245 if (NT_STATUS_IS_OK(status)) {
246 status = gensec_session_info(dce_conn->auth_state.gensec_security,
247 &dce_conn->auth_state.session_info);
248 if (!NT_STATUS_IS_OK(status)) {
249 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
253 /* Now that we are authenticated, got back to the generic session key... */
254 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
256 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
257 dce_conn->auth_state.auth_info->auth_pad_length = 0;
258 dce_conn->auth_state.auth_info->auth_reserved = 0;
261 DEBUG(2, ("Failed to finish dcesrv auth alter_ack: %s\n", nt_errstr(status)));
267 generate a CONNECT level verifier
269 static NTSTATUS dcesrv_connect_verifier(TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
271 *blob = data_blob_talloc(mem_ctx, NULL, 16);
272 if (blob->data == NULL) {
273 return NT_STATUS_NO_MEMORY;
275 SIVAL(blob->data, 0, 1);
276 memset(blob->data+4, 0, 12);
281 generate a CONNECT level verifier
283 static NTSTATUS dcesrv_check_connect_verifier(DATA_BLOB *blob)
285 if (blob->length != 16 ||
286 IVAL(blob->data, 0) != 1) {
287 return NT_STATUS_ACCESS_DENIED;
294 check credentials on a request
296 BOOL dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
298 struct ncacn_packet *pkt = &call->pkt;
299 struct dcesrv_connection *dce_conn = call->conn;
301 struct dcerpc_auth auth;
302 struct ndr_pull *ndr;
305 if (!dce_conn->auth_state.auth_info ||
306 !dce_conn->auth_state.gensec_security) {
310 auth_blob.length = 8 + pkt->auth_length;
312 /* check for a valid length */
313 if (pkt->u.request.stub_and_verifier.length < auth_blob.length) {
318 pkt->u.request.stub_and_verifier.data +
319 pkt->u.request.stub_and_verifier.length - auth_blob.length;
320 pkt->u.request.stub_and_verifier.length -= auth_blob.length;
322 /* pull the auth structure */
323 ndr = ndr_pull_init_blob(&auth_blob, call);
328 if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
329 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
332 status = ndr_pull_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, &auth);
333 if (!NT_STATUS_IS_OK(status)) {
338 /* check signature or unseal the packet */
339 switch (dce_conn->auth_state.auth_info->auth_level) {
340 case DCERPC_AUTH_LEVEL_PRIVACY:
341 status = gensec_unseal_packet(dce_conn->auth_state.gensec_security,
343 full_packet->data + DCERPC_REQUEST_LENGTH,
344 pkt->u.request.stub_and_verifier.length,
346 full_packet->length-auth.credentials.length,
348 memcpy(pkt->u.request.stub_and_verifier.data,
349 full_packet->data + DCERPC_REQUEST_LENGTH,
350 pkt->u.request.stub_and_verifier.length);
353 case DCERPC_AUTH_LEVEL_INTEGRITY:
354 status = gensec_check_packet(dce_conn->auth_state.gensec_security,
356 pkt->u.request.stub_and_verifier.data,
357 pkt->u.request.stub_and_verifier.length,
359 full_packet->length-auth.credentials.length,
363 case DCERPC_AUTH_LEVEL_CONNECT:
364 status = dcesrv_check_connect_verifier(&auth.credentials);
368 status = NT_STATUS_INVALID_LEVEL;
372 /* remove the indicated amount of padding */
373 if (pkt->u.request.stub_and_verifier.length < auth.auth_pad_length) {
377 pkt->u.request.stub_and_verifier.length -= auth.auth_pad_length;
380 return NT_STATUS_IS_OK(status);
385 push a signed or sealed dcerpc request packet into a blob
387 BOOL dcesrv_auth_response(struct dcesrv_call_state *call,
388 DATA_BLOB *blob, struct ncacn_packet *pkt)
390 struct dcesrv_connection *dce_conn = call->conn;
392 struct ndr_push *ndr;
393 uint32_t payload_length;
395 /* non-signed packets are simple */
396 if (!dce_conn->auth_state.auth_info || !dce_conn->auth_state.gensec_security) {
397 status = ncacn_push_auth(blob, call, pkt, NULL);
398 return NT_STATUS_IS_OK(status);
401 ndr = ndr_push_init_ctx(call);
406 if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
407 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
410 status = ndr_push_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
411 if (!NT_STATUS_IS_OK(status)) {
415 /* pad to 16 byte multiple, match win2k3 */
416 dce_conn->auth_state.auth_info->auth_pad_length = NDR_ALIGN(ndr, 16);
417 ndr_push_zero(ndr, dce_conn->auth_state.auth_info->auth_pad_length);
419 payload_length = ndr->offset - DCERPC_REQUEST_LENGTH;
421 if (dce_conn->auth_state.auth_info->auth_level == DCERPC_AUTH_LEVEL_CONNECT) {
422 status = dcesrv_connect_verifier(call,
423 &dce_conn->auth_state.auth_info->credentials);
424 if (!NT_STATUS_IS_OK(status)) {
428 dce_conn->auth_state.auth_info->credentials
429 = data_blob_talloc(call, NULL,
430 gensec_sig_size(dce_conn->auth_state.gensec_security,
434 /* add the auth verifier */
435 status = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, dce_conn->auth_state.auth_info);
436 if (!NT_STATUS_IS_OK(status)) {
440 /* extract the whole packet as a blob */
441 *blob = ndr_push_blob(ndr);
443 /* fill in the fragment length and auth_length, we can't fill
444 in these earlier as we don't know the signature length (it
445 could be variable length) */
446 dcerpc_set_frag_length(blob, blob->length);
447 dcerpc_set_auth_length(blob, dce_conn->auth_state.auth_info->credentials.length);
449 /* sign or seal the packet */
450 switch (dce_conn->auth_state.auth_info->auth_level) {
451 case DCERPC_AUTH_LEVEL_PRIVACY:
452 status = gensec_seal_packet(dce_conn->auth_state.gensec_security,
454 ndr->data + DCERPC_REQUEST_LENGTH,
457 blob->length - dce_conn->auth_state.auth_info->credentials.length,
458 &dce_conn->auth_state.auth_info->credentials);
461 case DCERPC_AUTH_LEVEL_INTEGRITY:
462 status = gensec_sign_packet(dce_conn->auth_state.gensec_security,
464 ndr->data + DCERPC_REQUEST_LENGTH,
467 blob->length - dce_conn->auth_state.auth_info->credentials.length,
468 &dce_conn->auth_state.auth_info->credentials);
472 case DCERPC_AUTH_LEVEL_CONNECT:
476 status = NT_STATUS_INVALID_LEVEL;
480 if (!NT_STATUS_IS_OK(status)) {
484 memcpy(blob->data + blob->length - dce_conn->auth_state.auth_info->credentials.length,
485 dce_conn->auth_state.auth_info->credentials.data, dce_conn->auth_state.auth_info->credentials.length);
487 data_blob_free(&dce_conn->auth_state.auth_info->credentials);