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"
28 startup the cryptographic side of an authenticated dcerpc server
30 NTSTATUS dcesrv_crypto_select_type(struct dcesrv_connection *dce_conn,
31 struct dcesrv_auth *auth)
34 if (auth->auth_info->auth_level != DCERPC_AUTH_LEVEL_INTEGRITY &&
35 auth->auth_info->auth_level != DCERPC_AUTH_LEVEL_PRIVACY &&
36 auth->auth_info->auth_level != DCERPC_AUTH_LEVEL_CONNECT) {
37 DEBUG(2,("auth_level %d not supported in dcesrv auth\n",
38 auth->auth_info->auth_level));
39 return NT_STATUS_INVALID_PARAMETER;
42 if (auth->gensec_security != NULL) {
44 * this this function should not be called
45 * twice per dcesrv_connection!
47 * so we need to find out the right
48 * dcerpc error to return
52 status = gensec_server_start(dce_conn, &auth->gensec_security);
53 if (!NT_STATUS_IS_OK(status)) {
54 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
58 status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_info->auth_type,
59 auth->auth_info->auth_level);
61 if (!NT_STATUS_IS_OK(status)) {
62 DEBUG(1, ("Failed to start GENSEC mech-specific server code (%d): %s\n",
63 (int)auth->auth_info->auth_type,
72 parse any auth information from a dcerpc bind request
73 return False if we can't handle the auth request for some
74 reason (in which case we send a bind_nak)
76 BOOL dcesrv_auth_bind(struct dcesrv_call_state *call)
78 struct dcerpc_packet *pkt = &call->pkt;
79 struct dcesrv_connection *dce_conn = call->conn;
82 if (pkt->u.bind.auth_info.length == 0) {
83 dce_conn->auth_state.auth_info = NULL;
87 dce_conn->auth_state.auth_info = talloc_p(dce_conn, struct dcerpc_auth);
88 if (!dce_conn->auth_state.auth_info) {
92 status = ndr_pull_struct_blob(&pkt->u.bind.auth_info,
94 dce_conn->auth_state.auth_info,
95 (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
96 if (!NT_STATUS_IS_OK(status)) {
100 status = dcesrv_crypto_select_type(dce_conn, &dce_conn->auth_state);
101 if (!NT_STATUS_IS_OK(status)) {
109 add any auth information needed in a bind ack, and process the authentication
110 information found in the bind.
112 BOOL dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct dcerpc_packet *pkt)
114 struct dcesrv_connection *dce_conn = call->conn;
117 if (!call->conn->auth_state.gensec_security) {
121 status = gensec_update(dce_conn->auth_state.gensec_security,
123 dce_conn->auth_state.auth_info->credentials,
124 &dce_conn->auth_state.auth_info->credentials);
126 if (NT_STATUS_IS_OK(status)) {
127 status = gensec_session_info(dce_conn->auth_state.gensec_security,
128 &dce_conn->auth_state.session_info);
129 if (!NT_STATUS_IS_OK(status)) {
130 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
134 /* Now that we are authenticated, got back to the generic session key... */
135 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
137 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
138 dce_conn->auth_state.auth_info->auth_pad_length = 0;
139 dce_conn->auth_state.auth_info->auth_reserved = 0;
142 DEBUG(2, ("Failed to start dcesrv auth negotiate: %s\n", nt_errstr(status)));
149 process the final stage of a auth request
151 BOOL dcesrv_auth_auth3(struct dcesrv_call_state *call)
153 struct dcerpc_packet *pkt = &call->pkt;
154 struct dcesrv_connection *dce_conn = call->conn;
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.auth.auth_info.length == 0) {
164 status = ndr_pull_struct_blob(&pkt->u.auth.auth_info,
166 dce_conn->auth_state.auth_info,
167 (ndr_pull_flags_fn_t)ndr_pull_dcerpc_auth);
168 if (!NT_STATUS_IS_OK(status)) {
172 /* Pass the extra data we got from the client down to gensec for processing */
173 status = gensec_update(dce_conn->auth_state.gensec_security,
175 dce_conn->auth_state.auth_info->credentials,
176 &dce_conn->auth_state.auth_info->credentials);
177 if (NT_STATUS_IS_OK(status)) {
178 status = gensec_session_info(dce_conn->auth_state.gensec_security,
179 &dce_conn->auth_state.session_info);
180 if (!NT_STATUS_IS_OK(status)) {
181 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
184 /* Now that we are authenticated, got back to the generic session key... */
185 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
188 DEBUG(4, ("dcesrv_auth_auth3: failed to authenticate: %s\n",
198 generate a CONNECT level verifier
200 static NTSTATUS dcesrv_connect_verifier(TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
202 *blob = data_blob_talloc(mem_ctx, NULL, 16);
203 if (blob->data == NULL) {
204 return NT_STATUS_NO_MEMORY;
206 SIVAL(blob->data, 0, 1);
207 memset(blob->data+4, 0, 12);
212 generate a CONNECT level verifier
214 static NTSTATUS dcesrv_check_connect_verifier(DATA_BLOB *blob)
216 if (blob->length != 16 ||
217 IVAL(blob->data, 0) != 1) {
218 return NT_STATUS_ACCESS_DENIED;
225 check credentials on a request
227 BOOL dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
229 struct dcerpc_packet *pkt = &call->pkt;
230 struct dcesrv_connection *dce_conn = call->conn;
232 struct dcerpc_auth auth;
233 struct ndr_pull *ndr;
236 if (!dce_conn->auth_state.auth_info ||
237 !dce_conn->auth_state.gensec_security) {
241 auth_blob.length = 8 + pkt->auth_length;
243 /* check for a valid length */
244 if (pkt->u.request.stub_and_verifier.length < auth_blob.length) {
249 pkt->u.request.stub_and_verifier.data +
250 pkt->u.request.stub_and_verifier.length - auth_blob.length;
251 pkt->u.request.stub_and_verifier.length -= auth_blob.length;
253 /* pull the auth structure */
254 ndr = ndr_pull_init_blob(&auth_blob, call);
259 if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
260 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
263 status = ndr_pull_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, &auth);
264 if (!NT_STATUS_IS_OK(status)) {
269 /* check signature or unseal the packet */
270 switch (dce_conn->auth_state.auth_info->auth_level) {
271 case DCERPC_AUTH_LEVEL_PRIVACY:
272 status = gensec_unseal_packet(dce_conn->auth_state.gensec_security,
274 full_packet->data + DCERPC_REQUEST_LENGTH,
275 pkt->u.request.stub_and_verifier.length,
277 full_packet->length-auth.credentials.length,
279 memcpy(pkt->u.request.stub_and_verifier.data,
280 full_packet->data + DCERPC_REQUEST_LENGTH,
281 pkt->u.request.stub_and_verifier.length);
284 case DCERPC_AUTH_LEVEL_INTEGRITY:
285 status = gensec_check_packet(dce_conn->auth_state.gensec_security,
287 pkt->u.request.stub_and_verifier.data,
288 pkt->u.request.stub_and_verifier.length,
290 full_packet->length-auth.credentials.length,
294 case DCERPC_AUTH_LEVEL_CONNECT:
295 status = dcesrv_check_connect_verifier(&auth.credentials);
299 status = NT_STATUS_INVALID_LEVEL;
303 /* remove the indicated amount of padding */
304 if (pkt->u.request.stub_and_verifier.length < auth.auth_pad_length) {
308 pkt->u.request.stub_and_verifier.length -= auth.auth_pad_length;
311 return NT_STATUS_IS_OK(status);
316 push a signed or sealed dcerpc request packet into a blob
318 BOOL dcesrv_auth_response(struct dcesrv_call_state *call,
319 DATA_BLOB *blob, struct dcerpc_packet *pkt)
321 struct dcesrv_connection *dce_conn = call->conn;
323 struct ndr_push *ndr;
324 uint32_t payload_length;
326 /* non-signed packets are simple */
327 if (!dce_conn->auth_state.auth_info || !dce_conn->auth_state.gensec_security) {
328 status = dcerpc_push_auth(blob, call, pkt, NULL);
329 return NT_STATUS_IS_OK(status);
332 ndr = ndr_push_init_ctx(call);
337 if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
338 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
341 status = ndr_push_dcerpc_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
342 if (!NT_STATUS_IS_OK(status)) {
346 /* pad to 8 byte multiple */
347 dce_conn->auth_state.auth_info->auth_pad_length = NDR_ALIGN(ndr, 8);
348 ndr_push_zero(ndr, dce_conn->auth_state.auth_info->auth_pad_length);
350 payload_length = ndr->offset - DCERPC_REQUEST_LENGTH;
352 if (dce_conn->auth_state.auth_info->auth_level == DCERPC_AUTH_LEVEL_CONNECT) {
353 status = dcesrv_connect_verifier(call,
354 &dce_conn->auth_state.auth_info->credentials);
355 if (!NT_STATUS_IS_OK(status)) {
359 dce_conn->auth_state.auth_info->credentials
360 = data_blob_talloc(call, NULL,
361 gensec_sig_size(dce_conn->auth_state.gensec_security));
364 /* add the auth verifier */
365 status = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, dce_conn->auth_state.auth_info);
366 if (!NT_STATUS_IS_OK(status)) {
370 /* extract the whole packet as a blob */
371 *blob = ndr_push_blob(ndr);
373 /* fill in the fragment length and auth_length, we can't fill
374 in these earlier as we don't know the signature length (it
375 could be variable length) */
376 dcerpc_set_frag_length(blob, blob->length);
377 dcerpc_set_auth_length(blob, dce_conn->auth_state.auth_info->credentials.length);
379 /* sign or seal the packet */
380 switch (dce_conn->auth_state.auth_info->auth_level) {
381 case DCERPC_AUTH_LEVEL_PRIVACY:
382 status = gensec_seal_packet(dce_conn->auth_state.gensec_security,
384 ndr->data + DCERPC_REQUEST_LENGTH,
387 blob->length - dce_conn->auth_state.auth_info->credentials.length,
388 &dce_conn->auth_state.auth_info->credentials);
391 case DCERPC_AUTH_LEVEL_INTEGRITY:
392 status = gensec_sign_packet(dce_conn->auth_state.gensec_security,
394 ndr->data + DCERPC_REQUEST_LENGTH,
397 blob->length - dce_conn->auth_state.auth_info->credentials.length,
398 &dce_conn->auth_state.auth_info->credentials);
402 case DCERPC_AUTH_LEVEL_CONNECT:
406 status = NT_STATUS_INVALID_LEVEL;
410 if (!NT_STATUS_IS_OK(status)) {
414 memcpy(blob->data + blob->length - dce_conn->auth_state.auth_info->credentials.length,
415 dce_conn->auth_state.auth_info->credentials.data, dce_conn->auth_state.auth_info->credentials.length);
417 data_blob_free(&dce_conn->auth_state.auth_info->credentials);