1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
|
/*
Unix SMB/CIFS implementation.
server side dcerpc authentication code
Copyright (C) Andrew Tridgell 2003
Copyright (C) Stefan (metze) Metzmacher 2004
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "librpc/rpc/dcesrv_core.h"
#include "librpc/rpc/dcesrv_core_proto.h"
#include "librpc/gen_ndr/ndr_dcerpc.h"
#include "auth/credentials/credentials.h"
#include "auth/gensec/gensec.h"
#include "auth/auth.h"
#include "param/param.h"
static NTSTATUS dcesrv_auth_negotiate_hdr_signing(struct dcesrv_call_state *call,
struct ncacn_packet *pkt)
{
struct dcesrv_connection *dce_conn = call->conn;
struct dcesrv_auth *a = NULL;
if (!(call->pkt.pfc_flags & DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN)) {
return NT_STATUS_OK;
}
if (dce_conn->client_hdr_signing) {
if (dce_conn->negotiated_hdr_signing && pkt != NULL) {
pkt->pfc_flags |= DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN;
}
return NT_STATUS_OK;
}
dce_conn->client_hdr_signing = true;
dce_conn->negotiated_hdr_signing = dce_conn->support_hdr_signing;
if (!dce_conn->negotiated_hdr_signing) {
return NT_STATUS_OK;
}
if (pkt != NULL) {
pkt->pfc_flags |= DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN;
}
a = call->conn->default_auth_state;
if (a->gensec_security != NULL) {
gensec_want_feature(a->gensec_security,
GENSEC_FEATURE_SIGN_PKT_HEADER);
}
for (a = call->conn->auth_states; a != NULL; a = a->next) {
if (a->gensec_security == NULL) {
continue;
}
gensec_want_feature(a->gensec_security,
GENSEC_FEATURE_SIGN_PKT_HEADER);
}
return NT_STATUS_OK;
}
static bool dcesrv_auth_prepare_gensec(struct dcesrv_call_state *call)
{
struct dcesrv_connection *dce_conn = call->conn;
struct dcesrv_auth *auth = call->auth_state;
struct dcesrv_context_callbacks *cb = call->conn->dce_ctx->callbacks;
NTSTATUS status;
if (auth->auth_started) {
return false;
}
auth->auth_started = true;
if (auth->auth_invalid) {
return false;
}
if (auth->auth_finished) {
return false;
}
if (auth->gensec_security != NULL) {
return false;
}
switch (call->in_auth_info.auth_level) {
case DCERPC_AUTH_LEVEL_CONNECT:
case DCERPC_AUTH_LEVEL_CALL:
case DCERPC_AUTH_LEVEL_PACKET:
case DCERPC_AUTH_LEVEL_INTEGRITY:
case DCERPC_AUTH_LEVEL_PRIVACY:
/*
* We evaluate auth_type only if auth_level was valid
*/
break;
default:
/*
* Setting DCERPC_AUTH_LEVEL_NONE,
* gives the caller the reject_reason
* as auth_context_id.
*
* Note: DCERPC_AUTH_LEVEL_NONE == 1
*/
auth->auth_type = DCERPC_AUTH_TYPE_NONE;
auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
auth->auth_context_id = DCERPC_BIND_NAK_REASON_NOT_SPECIFIED;
return false;
}
auth->auth_type = call->in_auth_info.auth_type;
auth->auth_level = call->in_auth_info.auth_level;
auth->auth_context_id = call->in_auth_info.auth_context_id;
status = cb->auth.gensec_prepare(
auth,
call,
&auth->gensec_security,
cb->auth.private_data);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(1, ("Failed to call samba_server_gensec_start %s\n",
nt_errstr(status)));
return false;
}
/*
* We have to call this because we set the target_service for
* Kerberos to NULL above, and in any case we wish to log a
* more specific service target.
*
*/
status = gensec_set_target_service_description(auth->gensec_security,
"DCE/RPC");
if (!NT_STATUS_IS_OK(status)) {
DEBUG(1, ("Failed to call gensec_set_target_service_description %s\n",
nt_errstr(status)));
return false;
}
if (call->conn->remote_address != NULL) {
status = gensec_set_remote_address(auth->gensec_security,
call->conn->remote_address);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(1, ("Failed to call gensec_set_remote_address() %s\n",
nt_errstr(status)));
return false;
}
}
if (call->conn->local_address != NULL) {
status = gensec_set_local_address(auth->gensec_security,
call->conn->local_address);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(1, ("Failed to call gensec_set_local_address() %s\n",
nt_errstr(status)));
return false;
}
}
status = gensec_start_mech_by_authtype(auth->gensec_security, auth->auth_type,
auth->auth_level);
if (!NT_STATUS_IS_OK(status)) {
const char *backend_name =
gensec_get_name_by_authtype(auth->gensec_security,
auth->auth_type);
DEBUG(3, ("Failed to start GENSEC mechanism for DCERPC server: "
"auth_type=%d (%s), auth_level=%d: %s\n",
(int)auth->auth_type, backend_name,
(int)auth->auth_level,
nt_errstr(status)));
/*
* Setting DCERPC_AUTH_LEVEL_NONE,
* gives the caller the reject_reason
* as auth_context_id.
*
* Note: DCERPC_AUTH_LEVEL_NONE == 1
*/
auth->auth_type = DCERPC_AUTH_TYPE_NONE;
auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
if (backend_name != NULL) {
auth->auth_context_id =
DCERPC_BIND_NAK_REASON_INVALID_CHECKSUM;
} else {
auth->auth_context_id =
DCERPC_BIND_NAK_REASON_INVALID_AUTH_TYPE;
}
return false;
}
if (dce_conn->negotiated_hdr_signing) {
gensec_want_feature(auth->gensec_security,
GENSEC_FEATURE_SIGN_PKT_HEADER);
}
return true;
}
static void dcesrv_default_auth_state_finish_bind(struct dcesrv_call_state *call)
{
SMB_ASSERT(call->pkt.ptype == DCERPC_PKT_BIND);
if (call->auth_state == call->conn->default_auth_state) {
return;
}
if (call->conn->default_auth_state->auth_started) {
return;
}
if (call->conn->default_auth_state->auth_invalid) {
return;
}
call->conn->default_auth_state->auth_type = DCERPC_AUTH_TYPE_NONE;
call->conn->default_auth_state->auth_level = DCERPC_AUTH_LEVEL_NONE;
call->conn->default_auth_state->auth_context_id = 0;
call->conn->default_auth_state->auth_started = true;
call->conn->default_auth_state->auth_finished = true;
/*
*
* We defer log_successful_dcesrv_authz_event()
* to dcesrv_default_auth_state_prepare_request()
*
* As we don't want to trigger authz_events
* just for alter_context requests without authentication
*/
}
void dcesrv_default_auth_state_prepare_request(struct dcesrv_call_state *call)
{
struct dcesrv_connection *dce_conn = call->conn;
struct dcesrv_auth *auth = call->auth_state;
struct dcesrv_context_callbacks *cb = call->conn->dce_ctx->callbacks;
if (auth->auth_audited) {
return;
}
if (call->pkt.ptype != DCERPC_PKT_REQUEST) {
return;
}
if (auth != dce_conn->default_auth_state) {
return;
}
if (auth->auth_invalid) {
return;
}
if (!auth->auth_finished) {
return;
}
if (cb->log.successful_authz == NULL) {
return;
}
cb->log.successful_authz(call, cb->log.private_data);
}
/*
parse any auth information from a dcerpc bind request
return false if we can't handle the auth request for some
reason (in which case we send a bind_nak)
*/
bool dcesrv_auth_bind(struct dcesrv_call_state *call)
{
struct ncacn_packet *pkt = &call->pkt;
struct dcesrv_auth *auth = call->auth_state;
struct dcesrv_context_callbacks *cb = call->conn->dce_ctx->callbacks;
NTSTATUS status;
if (pkt->auth_length == 0) {
auth->auth_type = DCERPC_AUTH_TYPE_NONE;
auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
auth->auth_context_id = 0;
auth->auth_started = true;
if (cb->log.successful_authz != NULL) {
cb->log.successful_authz(call, cb->log.private_data);
}
return true;
}
status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.bind.auth_info,
&call->in_auth_info,
NULL, true);
if (!NT_STATUS_IS_OK(status)) {
/*
* Setting DCERPC_AUTH_LEVEL_NONE,
* gives the caller the reject_reason
* as auth_context_id.
*
* Note: DCERPC_AUTH_LEVEL_NONE == 1
*/
auth->auth_type = DCERPC_AUTH_TYPE_NONE;
auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
auth->auth_context_id =
DCERPC_BIND_NAK_REASON_PROTOCOL_VERSION_NOT_SUPPORTED;
return false;
}
return dcesrv_auth_prepare_gensec(call);
}
NTSTATUS dcesrv_auth_complete(struct dcesrv_call_state *call, NTSTATUS status)
{
struct dcesrv_auth *auth = call->auth_state;
const char *pdu = "<unknown>";
switch (call->pkt.ptype) {
case DCERPC_PKT_BIND:
pdu = "BIND";
break;
case DCERPC_PKT_ALTER:
pdu = "ALTER";
break;
case DCERPC_PKT_AUTH3:
pdu = "AUTH3";
if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
DEBUG(4, ("GENSEC not finished at at %s\n", pdu));
return NT_STATUS_RPC_SEC_PKG_ERROR;
}
break;
default:
return NT_STATUS_INTERNAL_ERROR;
}
if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
return NT_STATUS_OK;
}
if (!NT_STATUS_IS_OK(status)) {
DEBUG(4, ("GENSEC mech rejected the incoming authentication "
"at %s: %s\n", pdu, nt_errstr(status)));
return status;
}
status = gensec_session_info(auth->gensec_security,
auth,
&auth->session_info);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(1, ("Failed to establish session_info: %s\n",
nt_errstr(status)));
return status;
}
auth->auth_finished = true;
if (auth->auth_level == DCERPC_AUTH_LEVEL_CONNECT &&
!call->conn->got_explicit_auth_level_connect)
{
call->conn->default_auth_level_connect = auth;
}
if (call->pkt.ptype != DCERPC_PKT_AUTH3) {
return NT_STATUS_OK;
}
if (call->out_auth_info->credentials.length != 0) {
DEBUG(4, ("GENSEC produced output token (len=%zu) at %s\n",
call->out_auth_info->credentials.length, pdu));
return NT_STATUS_RPC_SEC_PKG_ERROR;
}
return NT_STATUS_OK;
}
/*
add any auth information needed in a bind ack, and process the authentication
information found in the bind.
*/
NTSTATUS dcesrv_auth_prepare_bind_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
{
struct dcesrv_connection *dce_conn = call->conn;
struct dcesrv_auth *auth = call->auth_state;
NTSTATUS status;
status = dcesrv_auth_negotiate_hdr_signing(call, pkt);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
dce_conn->allow_alter = true;
dcesrv_default_auth_state_finish_bind(call);
if (call->pkt.auth_length == 0) {
auth->auth_finished = true;
return NT_STATUS_OK;
}
/* We can't work without an existing gensec state */
if (auth->gensec_security == NULL) {
return NT_STATUS_INTERNAL_ERROR;
}
call->_out_auth_info = (struct dcerpc_auth) {
.auth_type = auth->auth_type,
.auth_level = auth->auth_level,
.auth_context_id = auth->auth_context_id,
};
call->out_auth_info = &call->_out_auth_info;
return NT_STATUS_OK;
}
/*
process the final stage of a auth request
*/
bool dcesrv_auth_prepare_auth3(struct dcesrv_call_state *call)
{
struct ncacn_packet *pkt = &call->pkt;
struct dcesrv_auth *auth = call->auth_state;
NTSTATUS status;
if (pkt->auth_length == 0) {
return false;
}
if (auth->auth_finished) {
return false;
}
/* We can't work without an existing gensec state */
if (auth->gensec_security == NULL) {
return false;
}
status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.auth3.auth_info,
&call->in_auth_info, NULL, true);
if (!NT_STATUS_IS_OK(status)) {
/*
* Windows returns DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY
* instead of DCERPC_NCA_S_PROTO_ERROR.
*/
call->fault_code = DCERPC_NCA_S_FAULT_REMOTE_NO_MEMORY;
return false;
}
if (call->in_auth_info.auth_type != auth->auth_type) {
return false;
}
if (call->in_auth_info.auth_level != auth->auth_level) {
return false;
}
if (call->in_auth_info.auth_context_id != auth->auth_context_id) {
return false;
}
call->_out_auth_info = (struct dcerpc_auth) {
.auth_type = auth->auth_type,
.auth_level = auth->auth_level,
.auth_context_id = auth->auth_context_id,
};
call->out_auth_info = &call->_out_auth_info;
return true;
}
/*
parse any auth information from a dcerpc alter request
return false if we can't handle the auth request for some
reason (in which case we send a bind_nak (is this true for here?))
*/
bool dcesrv_auth_alter(struct dcesrv_call_state *call)
{
struct ncacn_packet *pkt = &call->pkt;
struct dcesrv_auth *auth = call->auth_state;
NTSTATUS status;
/* on a pure interface change there is no auth blob */
if (pkt->auth_length == 0) {
if (!auth->auth_finished) {
return false;
}
return true;
}
if (auth->auth_finished) {
call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
return false;
}
status = dcerpc_pull_auth_trailer(pkt, call, &pkt->u.alter.auth_info,
&call->in_auth_info, NULL, true);
if (!NT_STATUS_IS_OK(status)) {
call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
return false;
}
if (!auth->auth_started) {
bool ok;
ok = dcesrv_auth_prepare_gensec(call);
if (!ok) {
call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
return false;
}
return true;
}
if (call->in_auth_info.auth_type == DCERPC_AUTH_TYPE_NONE) {
call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
return false;
}
if (call->in_auth_info.auth_type != auth->auth_type) {
return false;
}
if (call->in_auth_info.auth_level != auth->auth_level) {
return false;
}
if (call->in_auth_info.auth_context_id != auth->auth_context_id) {
return false;
}
return true;
}
/*
add any auth information needed in a alter ack, and process the authentication
information found in the alter.
*/
NTSTATUS dcesrv_auth_prepare_alter_ack(struct dcesrv_call_state *call, struct ncacn_packet *pkt)
{
struct dcesrv_auth *auth = call->auth_state;
NTSTATUS status;
status = dcesrv_auth_negotiate_hdr_signing(call, pkt);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
/* on a pure interface change there is no auth_info structure
setup */
if (call->pkt.auth_length == 0) {
return NT_STATUS_OK;
}
if (auth->gensec_security == NULL) {
return NT_STATUS_INTERNAL_ERROR;
}
call->_out_auth_info = (struct dcerpc_auth) {
.auth_type = auth->auth_type,
.auth_level = auth->auth_level,
.auth_context_id = auth->auth_context_id,
};
call->out_auth_info = &call->_out_auth_info;
return NT_STATUS_OK;
}
/*
check credentials on a packet
*/
bool dcesrv_auth_pkt_pull(struct dcesrv_call_state *call,
DATA_BLOB *full_packet,
uint8_t required_flags,
uint8_t optional_flags,
uint8_t payload_offset,
DATA_BLOB *payload_and_verifier)
{
struct ncacn_packet *pkt = &call->pkt;
struct dcesrv_auth *auth = call->auth_state;
const struct dcerpc_auth tmp_auth = {
.auth_type = auth->auth_type,
.auth_level = auth->auth_level,
.auth_context_id = auth->auth_context_id,
};
NTSTATUS status;
if (!auth->auth_started) {
return false;
}
if (!auth->auth_finished) {
call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
return false;
}
if (auth->auth_invalid) {
return false;
}
status = dcerpc_ncacn_pull_pkt_auth(&tmp_auth,
auth->gensec_security,
call,
pkt->ptype,
required_flags,
optional_flags,
payload_offset,
payload_and_verifier,
full_packet,
pkt);
if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTOCOL_ERROR)) {
call->fault_code = DCERPC_NCA_S_PROTO_ERROR;
return false;
}
if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_UNSUPPORTED_AUTHN_LEVEL)) {
call->fault_code = DCERPC_NCA_S_UNSUPPORTED_AUTHN_LEVEL;
return false;
}
if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_SEC_PKG_ERROR)) {
call->fault_code = DCERPC_FAULT_SEC_PKG_ERROR;
return false;
}
if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
call->fault_code = DCERPC_FAULT_ACCESS_DENIED;
return false;
}
if (!NT_STATUS_IS_OK(status)) {
return false;
}
return true;
}
/*
push a signed or sealed dcerpc request packet into a blob
*/
bool dcesrv_auth_pkt_push(struct dcesrv_call_state *call,
DATA_BLOB *blob, size_t sig_size,
uint8_t payload_offset,
const DATA_BLOB *payload,
const struct ncacn_packet *pkt)
{
struct dcesrv_auth *auth = call->auth_state;
const struct dcerpc_auth tmp_auth = {
.auth_type = auth->auth_type,
.auth_level = auth->auth_level,
.auth_context_id = auth->auth_context_id,
};
NTSTATUS status;
status = dcerpc_ncacn_push_pkt_auth(&tmp_auth,
auth->gensec_security,
call, blob, sig_size,
payload_offset,
payload,
pkt);
return NT_STATUS_IS_OK(status);
}
|