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 ncacn_packet *pkt = &call->pkt;
36 struct dcesrv_connection *dce_conn = call->conn;
37 struct dcesrv_auth *auth = &dce_conn->auth_state;
40 if (pkt->u.bind.auth_info.length == 0) {
41 dce_conn->auth_state.auth_info = NULL;
45 dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
46 if (!dce_conn->auth_state.auth_info) {
50 status = ndr_pull_struct_blob(&pkt->u.bind.auth_info,
52 dce_conn->auth_state.auth_info,
53 (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
54 if (!NT_STATUS_IS_OK(status)) {
58 status = gensec_server_start(dce_conn, &auth->gensec_security, call->event_ctx);
59 if (!NT_STATUS_IS_OK(status)) {
60 DEBUG(1, ("Failed to start GENSEC for DCERPC server: %s\n", nt_errstr(status)));
64 status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_info->auth_type,
65 auth->auth_info->auth_level);
67 if (!NT_STATUS_IS_OK(status)) {
68 DEBUG(1, ("Failed to start GENSEC mechanism for DCERPC server: auth_type=%d, auth_level=%d: %s\n",
69 (int)auth->auth_info->auth_type,
70 (int)auth->auth_info->auth_level,
79 add any auth information needed in a bind ack, and process the authentication
80 information found in the bind.
82 BOOL dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
84 struct dcesrv_connection *dce_conn = call->conn;
87 if (!call->conn->auth_state.gensec_security) {
91 status = gensec_update(dce_conn->auth_state.gensec_security,
93 dce_conn->auth_state.auth_info->credentials,
94 &dce_conn->auth_state.auth_info->credentials);
96 if (NT_STATUS_IS_OK(status)) {
97 status = gensec_session_info(dce_conn->auth_state.gensec_security,
98 &dce_conn->auth_state.session_info);
99 if (!NT_STATUS_IS_OK(status)) {
100 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
104 /* Now that we are authenticated, go back to the generic session key... */
105 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
107 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
108 dce_conn->auth_state.auth_info->auth_pad_length = 0;
109 dce_conn->auth_state.auth_info->auth_reserved = 0;
112 DEBUG(2, ("Failed to start dcesrv auth negotiate: %s\n", nt_errstr(status)));
119 process the final stage of a auth request
121 BOOL dcesrv_auth_auth3(struct dcesrv_call_state *call)
123 struct ncacn_packet *pkt = &call->pkt;
124 struct dcesrv_connection *dce_conn = call->conn;
127 /* We can't work without an existing gensec state, and an new blob to feed it */
128 if (!dce_conn->auth_state.auth_info ||
129 !dce_conn->auth_state.gensec_security ||
130 pkt->u.auth3.auth_info.length == 0) {
134 status = ndr_pull_struct_blob(&pkt->u.auth3.auth_info,
136 dce_conn->auth_state.auth_info,
137 (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
138 if (!NT_STATUS_IS_OK(status)) {
142 /* Pass the extra data we got from the client down to gensec for processing */
143 status = gensec_update(dce_conn->auth_state.gensec_security,
145 dce_conn->auth_state.auth_info->credentials,
146 &dce_conn->auth_state.auth_info->credentials);
147 if (NT_STATUS_IS_OK(status)) {
148 status = gensec_session_info(dce_conn->auth_state.gensec_security,
149 &dce_conn->auth_state.session_info);
150 if (!NT_STATUS_IS_OK(status)) {
151 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
154 /* Now that we are authenticated, go back to the generic session key... */
155 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
158 DEBUG(4, ("dcesrv_auth_auth3: failed to authenticate: %s\n",
167 parse any auth information from a dcerpc alter request
168 return False if we can't handle the auth request for some
169 reason (in which case we send a bind_nak (is this true for here?))
171 BOOL dcesrv_auth_alter(struct dcesrv_call_state *call)
173 struct ncacn_packet *pkt = &call->pkt;
174 struct dcesrv_connection *dce_conn = call->conn;
177 /* on a pure interface change there is no auth blob */
178 if (pkt->u.alter.auth_info.length == 0) {
182 /* We can't work without an existing gensec state */
183 if (!dce_conn->auth_state.gensec_security) {
187 dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
188 if (!dce_conn->auth_state.auth_info) {
192 status = ndr_pull_struct_blob(&pkt->u.alter.auth_info,
194 dce_conn->auth_state.auth_info,
195 (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
196 if (!NT_STATUS_IS_OK(status)) {
204 add any auth information needed in a alter ack, and process the authentication
205 information found in the alter.
207 BOOL dcesrv_auth_alter_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
209 struct dcesrv_connection *dce_conn = call->conn;
212 /* on a pure interface change there is no auth_info structure
214 if (!call->conn->auth_state.auth_info) {
218 if (!call->conn->auth_state.gensec_security) {
222 status = gensec_update(dce_conn->auth_state.gensec_security,
224 dce_conn->auth_state.auth_info->credentials,
225 &dce_conn->auth_state.auth_info->credentials);
227 if (NT_STATUS_IS_OK(status)) {
228 status = gensec_session_info(dce_conn->auth_state.gensec_security,
229 &dce_conn->auth_state.session_info);
230 if (!NT_STATUS_IS_OK(status)) {
231 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
235 /* Now that we are authenticated, got back to the generic session key... */
236 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
238 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
239 dce_conn->auth_state.auth_info->auth_pad_length = 0;
240 dce_conn->auth_state.auth_info->auth_reserved = 0;
243 DEBUG(2, ("Failed to finish dcesrv auth alter_ack: %s\n", nt_errstr(status)));
249 generate a CONNECT level verifier
251 static NTSTATUS dcesrv_connect_verifier(TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
253 *blob = data_blob_talloc(mem_ctx, NULL, 16);
254 if (blob->data == NULL) {
255 return NT_STATUS_NO_MEMORY;
257 SIVAL(blob->data, 0, 1);
258 memset(blob->data+4, 0, 12);
263 generate a CONNECT level verifier
265 static NTSTATUS dcesrv_check_connect_verifier(DATA_BLOB *blob)
267 if (blob->length != 16 ||
268 IVAL(blob->data, 0) != 1) {
269 return NT_STATUS_ACCESS_DENIED;
276 check credentials on a request
278 BOOL dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
280 struct ncacn_packet *pkt = &call->pkt;
281 struct dcesrv_connection *dce_conn = call->conn;
283 struct dcerpc_auth auth;
284 struct ndr_pull *ndr;
287 if (!dce_conn->auth_state.auth_info ||
288 !dce_conn->auth_state.gensec_security) {
292 auth_blob.length = 8 + pkt->auth_length;
294 /* check for a valid length */
295 if (pkt->u.request.stub_and_verifier.length < auth_blob.length) {
300 pkt->u.request.stub_and_verifier.data +
301 pkt->u.request.stub_and_verifier.length - auth_blob.length;
302 pkt->u.request.stub_and_verifier.length -= auth_blob.length;
304 /* pull the auth structure */
305 ndr = ndr_pull_init_blob(&auth_blob, call);
310 if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
311 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
314 status = ndr_pull_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, &auth);
315 if (!NT_STATUS_IS_OK(status)) {
320 /* check signature or unseal the packet */
321 switch (dce_conn->auth_state.auth_info->auth_level) {
322 case DCERPC_AUTH_LEVEL_PRIVACY:
323 status = gensec_unseal_packet(dce_conn->auth_state.gensec_security,
325 full_packet->data + DCERPC_REQUEST_LENGTH,
326 pkt->u.request.stub_and_verifier.length,
328 full_packet->length-auth.credentials.length,
330 memcpy(pkt->u.request.stub_and_verifier.data,
331 full_packet->data + DCERPC_REQUEST_LENGTH,
332 pkt->u.request.stub_and_verifier.length);
335 case DCERPC_AUTH_LEVEL_INTEGRITY:
336 status = gensec_check_packet(dce_conn->auth_state.gensec_security,
338 pkt->u.request.stub_and_verifier.data,
339 pkt->u.request.stub_and_verifier.length,
341 full_packet->length-auth.credentials.length,
345 case DCERPC_AUTH_LEVEL_CONNECT:
346 status = dcesrv_check_connect_verifier(&auth.credentials);
350 status = NT_STATUS_INVALID_LEVEL;
354 /* remove the indicated amount of padding */
355 if (pkt->u.request.stub_and_verifier.length < auth.auth_pad_length) {
359 pkt->u.request.stub_and_verifier.length -= auth.auth_pad_length;
362 return NT_STATUS_IS_OK(status);
367 push a signed or sealed dcerpc request packet into a blob
369 BOOL dcesrv_auth_response(struct dcesrv_call_state *call,
370 DATA_BLOB *blob, struct ncacn_packet *pkt)
372 struct dcesrv_connection *dce_conn = call->conn;
374 struct ndr_push *ndr;
375 uint32_t payload_length;
377 /* non-signed packets are simple */
378 if (!dce_conn->auth_state.auth_info || !dce_conn->auth_state.gensec_security) {
379 status = ncacn_push_auth(blob, call, pkt, NULL);
380 return NT_STATUS_IS_OK(status);
383 ndr = ndr_push_init_ctx(call);
388 if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
389 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
392 status = ndr_push_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
393 if (!NT_STATUS_IS_OK(status)) {
397 /* pad to 8 byte multiple */
398 dce_conn->auth_state.auth_info->auth_pad_length = NDR_ALIGN(ndr, 8);
399 ndr_push_zero(ndr, dce_conn->auth_state.auth_info->auth_pad_length);
401 payload_length = ndr->offset - DCERPC_REQUEST_LENGTH;
403 if (dce_conn->auth_state.auth_info->auth_level == DCERPC_AUTH_LEVEL_CONNECT) {
404 status = dcesrv_connect_verifier(call,
405 &dce_conn->auth_state.auth_info->credentials);
406 if (!NT_STATUS_IS_OK(status)) {
410 dce_conn->auth_state.auth_info->credentials
411 = data_blob_talloc(call, NULL,
412 gensec_sig_size(dce_conn->auth_state.gensec_security));
415 /* add the auth verifier */
416 status = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, dce_conn->auth_state.auth_info);
417 if (!NT_STATUS_IS_OK(status)) {
421 /* extract the whole packet as a blob */
422 *blob = ndr_push_blob(ndr);
424 /* fill in the fragment length and auth_length, we can't fill
425 in these earlier as we don't know the signature length (it
426 could be variable length) */
427 dcerpc_set_frag_length(blob, blob->length);
428 dcerpc_set_auth_length(blob, dce_conn->auth_state.auth_info->credentials.length);
430 /* sign or seal the packet */
431 switch (dce_conn->auth_state.auth_info->auth_level) {
432 case DCERPC_AUTH_LEVEL_PRIVACY:
433 status = gensec_seal_packet(dce_conn->auth_state.gensec_security,
435 ndr->data + DCERPC_REQUEST_LENGTH,
438 blob->length - dce_conn->auth_state.auth_info->credentials.length,
439 &dce_conn->auth_state.auth_info->credentials);
442 case DCERPC_AUTH_LEVEL_INTEGRITY:
443 status = gensec_sign_packet(dce_conn->auth_state.gensec_security,
445 ndr->data + DCERPC_REQUEST_LENGTH,
448 blob->length - dce_conn->auth_state.auth_info->credentials.length,
449 &dce_conn->auth_state.auth_info->credentials);
453 case DCERPC_AUTH_LEVEL_CONNECT:
457 status = NT_STATUS_INVALID_LEVEL;
461 if (!NT_STATUS_IS_OK(status)) {
465 memcpy(blob->data + blob->length - dce_conn->auth_state.auth_info->credentials.length,
466 dce_conn->auth_state.auth_info->credentials.data, dce_conn->auth_state.auth_info->credentials.length);
468 data_blob_free(&dce_conn->auth_state.auth_info->credentials);