source: trunk/server/source4/rpc_server/dcesrv_auth.c

Last change on this file was 920, checked in by Silvan Scherrer, 9 years ago

Samba Server: apply latest security patches to trunk

File size: 14.9 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 server side dcerpc authentication code
5
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Stefan (metze) Metzmacher 2004
8
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.
13
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.
18
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/>.
21*/
22
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"
33#include "librpc/rpc/rpc_common.h"
34
35/*
36 parse any auth information from a dcerpc bind request
37 return false if we can't handle the auth request for some
38 reason (in which case we send a bind_nak)
39*/
40bool dcesrv_auth_bind(struct dcesrv_call_state *call)
41{
42 struct cli_credentials *server_credentials;
43 struct ncacn_packet *pkt = &call->pkt;
44 struct dcesrv_connection *dce_conn = call->conn;
45 struct dcesrv_auth *auth = &dce_conn->auth_state;
46 NTSTATUS status;
47 uint32_t auth_length;
48
49 if (pkt->auth_length == 0) {
50 dce_conn->auth_state.auth_info = NULL;
51 return true;
52 }
53
54 dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
55 if (!dce_conn->auth_state.auth_info) {
56 return false;
57 }
58
59 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.bind.auth_info,
60 dce_conn->auth_state.auth_info,
61 &auth_length, false);
62 server_credentials
63 = cli_credentials_init(call);
64 if (!server_credentials) {
65 DEBUG(1, ("Failed to init server credentials\n"));
66 return false;
67 }
68
69 cli_credentials_set_conf(server_credentials, call->conn->dce_ctx->lp_ctx);
70 status = cli_credentials_set_machine_account(server_credentials, call->conn->dce_ctx->lp_ctx);
71 if (!NT_STATUS_IS_OK(status)) {
72 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
73 talloc_free(server_credentials);
74 server_credentials = NULL;
75 }
76
77 status = samba_server_gensec_start(dce_conn, call->event_ctx,
78 call->msg_ctx,
79 call->conn->dce_ctx->lp_ctx,
80 server_credentials,
81 NULL,
82 &auth->gensec_security);
83
84 status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_info->auth_type,
85 auth->auth_info->auth_level);
86
87 if (!NT_STATUS_IS_OK(status)) {
88 DEBUG(3, ("Failed to start GENSEC mechanism for DCERPC server: auth_type=%d, auth_level=%d: %s\n",
89 (int)auth->auth_info->auth_type,
90 (int)auth->auth_info->auth_level,
91 nt_errstr(status)));
92 return false;
93 }
94
95 if (call->conn->state_flags & DCESRV_CALL_STATE_FLAG_HEADER_SIGNING) {
96 gensec_want_feature(auth->gensec_security, GENSEC_FEATURE_SIGN_PKT_HEADER);
97 }
98
99 return true;
100}
101
102/*
103 add any auth information needed in a bind ack, and process the authentication
104 information found in the bind.
105*/
106NTSTATUS dcesrv_auth_bind_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
107{
108 struct dcesrv_connection *dce_conn = call->conn;
109 NTSTATUS status;
110
111 if (call->pkt.auth_length == 0) {
112 return NT_STATUS_OK;
113 }
114
115 status = gensec_update(dce_conn->auth_state.gensec_security,
116 call,
117 dce_conn->auth_state.auth_info->credentials,
118 &dce_conn->auth_state.auth_info->credentials);
119
120 if (NT_STATUS_IS_OK(status)) {
121 status = gensec_session_info(dce_conn->auth_state.gensec_security,
122 &dce_conn->auth_state.session_info);
123 if (!NT_STATUS_IS_OK(status)) {
124 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
125 return status;
126 }
127
128 if (dce_conn->state_flags & DCESRV_CALL_STATE_FLAG_HEADER_SIGNING) {
129 gensec_want_feature(dce_conn->auth_state.gensec_security,
130 GENSEC_FEATURE_SIGN_PKT_HEADER);
131 }
132
133 /* Now that we are authenticated, go back to the generic session key... */
134 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
135 return NT_STATUS_OK;
136 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
137 dce_conn->auth_state.auth_info->auth_pad_length = 0;
138 dce_conn->auth_state.auth_info->auth_reserved = 0;
139 return NT_STATUS_OK;
140 } else {
141 DEBUG(4, ("GENSEC mech rejected the incoming authentication at bind_ack: %s\n",
142 nt_errstr(status)));
143 return status;
144 }
145}
146
147
148/*
149 process the final stage of a auth request
150*/
151bool dcesrv_auth_auth3(struct dcesrv_call_state *call)
152{
153 struct ncacn_packet *pkt = &call->pkt;
154 struct dcesrv_connection *dce_conn = call->conn;
155 NTSTATUS status;
156 uint32_t auth_length;
157
158 if (pkt->auth_length == 0) {
159 return false;
160 }
161
162 if (!dce_conn->auth_state.auth_info) {
163 return false;
164 }
165
166 /* We can't work without an existing gensec state */
167 if (!dce_conn->auth_state.gensec_security) {
168 return false;
169 }
170
171 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.auth3.auth_info,
172 dce_conn->auth_state.auth_info, &auth_length, true);
173 if (!NT_STATUS_IS_OK(status)) {
174 return false;
175 }
176
177 /* Pass the extra data we got from the client down to gensec for processing */
178 status = gensec_update(dce_conn->auth_state.gensec_security,
179 call,
180 dce_conn->auth_state.auth_info->credentials,
181 &dce_conn->auth_state.auth_info->credentials);
182 if (NT_STATUS_IS_OK(status)) {
183 status = gensec_session_info(dce_conn->auth_state.gensec_security,
184 &dce_conn->auth_state.session_info);
185 if (!NT_STATUS_IS_OK(status)) {
186 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
187 return false;
188 }
189 /* Now that we are authenticated, go back to the generic session key... */
190 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
191 return true;
192 } else {
193 DEBUG(4, ("GENSEC mech rejected the incoming authentication at bind_auth3: %s\n",
194 nt_errstr(status)));
195 return false;
196 }
197}
198
199/*
200 parse any auth information from a dcerpc alter request
201 return false if we can't handle the auth request for some
202 reason (in which case we send a bind_nak (is this true for here?))
203*/
204bool dcesrv_auth_alter(struct dcesrv_call_state *call)
205{
206 struct ncacn_packet *pkt = &call->pkt;
207 struct dcesrv_connection *dce_conn = call->conn;
208 NTSTATUS status;
209 uint32_t auth_length;
210
211 /* on a pure interface change there is no auth blob */
212 if (pkt->auth_length == 0) {
213 return true;
214 }
215
216 /* We can't work without an existing gensec state */
217 if (!dce_conn->auth_state.gensec_security) {
218 return false;
219 }
220
221 dce_conn->auth_state.auth_info = talloc(dce_conn, struct dcerpc_auth);
222 if (!dce_conn->auth_state.auth_info) {
223 return false;
224 }
225
226 status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.alter.auth_info,
227 dce_conn->auth_state.auth_info,
228 &auth_length, true);
229 if (!NT_STATUS_IS_OK(status)) {
230 return false;
231 }
232
233 return true;
234}
235
236/*
237 add any auth information needed in a alter ack, and process the authentication
238 information found in the alter.
239*/
240NTSTATUS dcesrv_auth_alter_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
241{
242 struct dcesrv_connection *dce_conn = call->conn;
243 NTSTATUS status;
244
245 /* on a pure interface change there is no auth_info structure
246 setup */
247 if (call->pkt.auth_length == 0) {
248 return NT_STATUS_OK;
249 }
250
251 if (!call->conn->auth_state.gensec_security) {
252 return NT_STATUS_INVALID_PARAMETER;
253 }
254
255 status = gensec_update(dce_conn->auth_state.gensec_security,
256 call,
257 dce_conn->auth_state.auth_info->credentials,
258 &dce_conn->auth_state.auth_info->credentials);
259
260 if (NT_STATUS_IS_OK(status)) {
261 status = gensec_session_info(dce_conn->auth_state.gensec_security,
262 &dce_conn->auth_state.session_info);
263 if (!NT_STATUS_IS_OK(status)) {
264 DEBUG(1, ("Failed to establish session_info: %s\n", nt_errstr(status)));
265 return status;
266 }
267
268 /* Now that we are authenticated, got back to the generic session key... */
269 dce_conn->auth_state.session_key = dcesrv_generic_session_key;
270 return NT_STATUS_OK;
271 } else if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
272 dce_conn->auth_state.auth_info->auth_pad_length = 0;
273 dce_conn->auth_state.auth_info->auth_reserved = 0;
274 return NT_STATUS_OK;
275 }
276
277 DEBUG(4, ("GENSEC mech rejected the incoming authentication at auth alter_ack: %s\n",
278 nt_errstr(status)));
279 return status;
280}
281
282/*
283 check credentials on a request
284*/
285bool dcesrv_auth_request(struct dcesrv_call_state *call, DATA_BLOB *full_packet)
286{
287 struct ncacn_packet *pkt = &call->pkt;
288 struct dcesrv_connection *dce_conn = call->conn;
289 struct dcerpc_auth auth;
290 NTSTATUS status;
291 uint32_t auth_length;
292 size_t hdr_size = DCERPC_REQUEST_LENGTH;
293
294 if (!dce_conn->auth_state.auth_info ||
295 !dce_conn->auth_state.gensec_security) {
296 return true;
297 }
298
299 if (pkt->pfc_flags & DCERPC_PFC_FLAG_OBJECT_UUID) {
300 hdr_size += 16;
301 }
302
303 switch (dce_conn->auth_state.auth_info->auth_level) {
304 case DCERPC_AUTH_LEVEL_PRIVACY:
305 case DCERPC_AUTH_LEVEL_INTEGRITY:
306 break;
307
308 case DCERPC_AUTH_LEVEL_CONNECT:
309 if (pkt->auth_length != 0) {
310 break;
311 }
312 return true;
313 case DCERPC_AUTH_LEVEL_NONE:
314 if (pkt->auth_length != 0) {
315 return false;
316 }
317 return true;
318
319 default:
320 return false;
321 }
322
323 if (pkt->auth_length == 0) {
324 DEBUG(1,("dcesrv_auth_request: unexpected auth_length of 0\n"));
325 return false;
326 }
327
328 status = dcerpc_pull_auth_trailer(pkt, call,
329 &pkt->u.request.stub_and_verifier,
330 &auth, &auth_length, false);
331 if (!NT_STATUS_IS_OK(status)) {
332 return false;
333 }
334
335 pkt->u.request.stub_and_verifier.length -= auth_length;
336
337 /* check signature or unseal the packet */
338 switch (dce_conn->auth_state.auth_info->auth_level) {
339 case DCERPC_AUTH_LEVEL_PRIVACY:
340 status = gensec_unseal_packet(dce_conn->auth_state.gensec_security,
341 call,
342 full_packet->data + hdr_size,
343 pkt->u.request.stub_and_verifier.length,
344 full_packet->data,
345 full_packet->length-auth.credentials.length,
346 &auth.credentials);
347 memcpy(pkt->u.request.stub_and_verifier.data,
348 full_packet->data + hdr_size,
349 pkt->u.request.stub_and_verifier.length);
350 break;
351
352 case DCERPC_AUTH_LEVEL_INTEGRITY:
353 status = gensec_check_packet(dce_conn->auth_state.gensec_security,
354 call,
355 pkt->u.request.stub_and_verifier.data,
356 pkt->u.request.stub_and_verifier.length,
357 full_packet->data,
358 full_packet->length-auth.credentials.length,
359 &auth.credentials);
360 break;
361
362 case DCERPC_AUTH_LEVEL_CONNECT:
363 /* for now we ignore possible signatures here */
364 status = NT_STATUS_OK;
365 break;
366
367 default:
368 status = NT_STATUS_INVALID_LEVEL;
369 break;
370 }
371
372 /* remove the indicated amount of padding */
373 if (pkt->u.request.stub_and_verifier.length < auth.auth_pad_length) {
374 return false;
375 }
376 pkt->u.request.stub_and_verifier.length -= auth.auth_pad_length;
377
378 return NT_STATUS_IS_OK(status);
379}
380
381
382/*
383 push a signed or sealed dcerpc request packet into a blob
384*/
385bool dcesrv_auth_response(struct dcesrv_call_state *call,
386 DATA_BLOB *blob, size_t sig_size,
387 struct ncacn_packet *pkt)
388{
389 struct dcesrv_connection *dce_conn = call->conn;
390 NTSTATUS status;
391 enum ndr_err_code ndr_err;
392 struct ndr_push *ndr;
393 uint32_t payload_length;
394 DATA_BLOB creds2;
395
396 /* non-signed packets are simple */
397 if (sig_size == 0) {
398 status = ncacn_push_auth(blob, call, pkt, NULL);
399 return NT_STATUS_IS_OK(status);
400 }
401
402 switch (dce_conn->auth_state.auth_info->auth_level) {
403 case DCERPC_AUTH_LEVEL_PRIVACY:
404 case DCERPC_AUTH_LEVEL_INTEGRITY:
405 break;
406
407 case DCERPC_AUTH_LEVEL_CONNECT:
408 /*
409 * TODO: let the gensec mech decide if it wants to generate a
410 * signature that might be needed for schannel...
411 */
412 status = ncacn_push_auth(blob, call, pkt, NULL);
413 return NT_STATUS_IS_OK(status);
414
415 case DCERPC_AUTH_LEVEL_NONE:
416 status = ncacn_push_auth(blob, call, pkt, NULL);
417 return NT_STATUS_IS_OK(status);
418
419 default:
420 return false;
421 }
422
423 ndr = ndr_push_init_ctx(call);
424 if (!ndr) {
425 return false;
426 }
427
428 if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
429 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
430 }
431
432 ndr_err = ndr_push_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
433 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
434 return false;
435 }
436
437 /* pad to 16 byte multiple in the payload portion of the
438 packet. This matches what w2k3 does. Note that we can't use
439 ndr_push_align() as that is relative to the start of the
440 whole packet, whereas w2k8 wants it relative to the start
441 of the stub */
442 dce_conn->auth_state.auth_info->auth_pad_length =
443 (16 - (pkt->u.response.stub_and_verifier.length & 15)) & 15;
444 ndr_err = ndr_push_zero(ndr,
445 dce_conn->auth_state.auth_info->auth_pad_length);
446 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
447 return false;
448 }
449
450 payload_length = pkt->u.response.stub_and_verifier.length +
451 dce_conn->auth_state.auth_info->auth_pad_length;
452
453 /* we start without signature, it will appended later */
454 dce_conn->auth_state.auth_info->credentials = data_blob(NULL, 0);
455
456 /* add the auth verifier */
457 ndr_err = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS,
458 dce_conn->auth_state.auth_info);
459 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
460 return false;
461 }
462
463 /* extract the whole packet as a blob */
464 *blob = ndr_push_blob(ndr);
465
466 /*
467 * Setup the frag and auth length in the packet buffer.
468 * This is needed if the GENSEC mech does AEAD signing
469 * of the packet headers. The signature itself will be
470 * appended later.
471 */
472 dcerpc_set_frag_length(blob, blob->length + sig_size);
473 dcerpc_set_auth_length(blob, sig_size);
474
475 /* sign or seal the packet */
476 switch (dce_conn->auth_state.auth_info->auth_level) {
477 case DCERPC_AUTH_LEVEL_PRIVACY:
478 status = gensec_seal_packet(dce_conn->auth_state.gensec_security,
479 call,
480 ndr->data + DCERPC_REQUEST_LENGTH,
481 payload_length,
482 blob->data,
483 blob->length,
484 &creds2);
485 break;
486
487 case DCERPC_AUTH_LEVEL_INTEGRITY:
488 status = gensec_sign_packet(dce_conn->auth_state.gensec_security,
489 call,
490 ndr->data + DCERPC_REQUEST_LENGTH,
491 payload_length,
492 blob->data,
493 blob->length,
494 &creds2);
495 break;
496
497 default:
498 status = NT_STATUS_INVALID_LEVEL;
499 break;
500 }
501
502 if (!NT_STATUS_IS_OK(status)) {
503 return false;
504 }
505
506 if (creds2.length != sig_size) {
507 DEBUG(3,("dcesrv_auth_response: creds2.length[%u] != sig_size[%u] pad[%u] stub[%u]\n",
508 (unsigned)creds2.length, (uint32_t)sig_size,
509 (unsigned)dce_conn->auth_state.auth_info->auth_pad_length,
510 (unsigned)pkt->u.response.stub_and_verifier.length));
511 dcerpc_set_frag_length(blob, blob->length + creds2.length);
512 dcerpc_set_auth_length(blob, creds2.length);
513 }
514
515 if (!data_blob_append(call, blob, creds2.data, creds2.length)) {
516 status = NT_STATUS_NO_MEMORY;
517 return false;
518 }
519 data_blob_free(&creds2);
520
521 return true;
522}
Note: See TracBrowser for help on using the repository browser.