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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
|
/*
Unix SMB/CIFS implementation.
Authentication utility functions
Copyright (C) Andrew Tridgell 1992-1998
Copyright (C) Andrew Bartlett 2001-2010
Copyright (C) Jeremy Allison 2000-2001
Copyright (C) Rafal Szczesniak 2002
Copyright (C) Stefan Metzmacher 2005
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 "auth/auth.h"
#include "auth/auth_sam.h"
#include "auth/credentials/credentials.h"
#include "auth/credentials/credentials_krb5.h"
#include "libcli/security/security.h"
#include "libcli/security/claims-conversions.h"
#include "libcli/auth/libcli_auth.h"
#include "librpc/gen_ndr/claims.h"
#include "librpc/gen_ndr/ndr_claims.h"
#include "dsdb/samdb/samdb.h"
#include "auth/session_proto.h"
#include "system/kerberos.h"
#include <gssapi/gssapi.h>
#include "libcli/wbclient/wbclient.h"
#undef DBGC_CLASS
#define DBGC_CLASS DBGC_AUTH
_PUBLIC_ struct auth_session_info *anonymous_session(TALLOC_CTX *mem_ctx,
struct loadparm_context *lp_ctx)
{
NTSTATUS nt_status;
struct auth_session_info *session_info = NULL;
nt_status = auth_anonymous_session_info(mem_ctx, lp_ctx, &session_info);
if (!NT_STATUS_IS_OK(nt_status)) {
return NULL;
}
return session_info;
}
_PUBLIC_ NTSTATUS auth_generate_security_token(TALLOC_CTX *mem_ctx,
struct loadparm_context *lp_ctx, /* Optional, if you don't want privileges */
struct ldb_context *sam_ctx, /* Optional, if you don't want local groups */
const struct auth_user_info_dc *user_info_dc,
const struct auth_user_info_dc *device_info_dc,
const struct auth_claims auth_claims,
uint32_t session_info_flags,
struct security_token **_security_token)
{
struct security_token *security_token = NULL;
NTSTATUS nt_status;
uint32_t i;
uint32_t num_sids = 0;
uint32_t num_device_sids = 0;
const char *filter = NULL;
struct auth_SidAttr *sids = NULL;
struct auth_SidAttr *device_sids = NULL;
TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
if (tmp_ctx == NULL) {
return NT_STATUS_NO_MEMORY;
}
sids = talloc_array(tmp_ctx, struct auth_SidAttr, user_info_dc->num_sids);
if (sids == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
num_sids = user_info_dc->num_sids;
for (i=0; i < user_info_dc->num_sids; i++) {
sids[i] = user_info_dc->sids[i];
}
/*
* Finally add the "standard" sids.
* The only difference between guest and "anonymous"
* is the addition of Authenticated_Users.
*/
if (session_info_flags & AUTH_SESSION_INFO_DEFAULT_GROUPS) {
sids = talloc_realloc(tmp_ctx, sids, struct auth_SidAttr, num_sids + 2);
if (sids == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
sid_copy(&sids[num_sids].sid, &global_sid_World);
sids[num_sids].attrs = SE_GROUP_DEFAULT_FLAGS;
num_sids++;
sid_copy(&sids[num_sids].sid, &global_sid_Network);
sids[num_sids].attrs = SE_GROUP_DEFAULT_FLAGS;
num_sids++;
}
if (session_info_flags & AUTH_SESSION_INFO_AUTHENTICATED) {
sids = talloc_realloc(tmp_ctx, sids, struct auth_SidAttr, num_sids + 1);
if (sids == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
sid_copy(&sids[num_sids].sid, &global_sid_Authenticated_Users);
sids[num_sids].attrs = SE_GROUP_DEFAULT_FLAGS;
num_sids++;
}
if (session_info_flags & AUTH_SESSION_INFO_NTLM) {
sids = talloc_realloc(tmp_ctx, sids, struct auth_SidAttr, num_sids + 1);
if (sids == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
if (!dom_sid_parse(SID_NT_NTLM_AUTHENTICATION, &sids[num_sids].sid)) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_INTERNAL_ERROR;
}
sids[num_sids].attrs = SE_GROUP_DEFAULT_FLAGS;
num_sids++;
}
if (num_sids > PRIMARY_USER_SID_INDEX && dom_sid_equal(&global_sid_Anonymous, &sids[PRIMARY_USER_SID_INDEX].sid)) {
/* Don't expand nested groups of system, anonymous etc*/
} else if (num_sids > PRIMARY_USER_SID_INDEX && dom_sid_equal(&global_sid_System, &sids[PRIMARY_USER_SID_INDEX].sid)) {
/* Don't expand nested groups of system, anonymous etc*/
} else if (sam_ctx != NULL) {
filter = talloc_asprintf(tmp_ctx, "(&(objectClass=group)(groupType:"LDB_OID_COMPARATOR_AND":=%u))",
GROUP_TYPE_BUILTIN_LOCAL_GROUP);
/* Search for each group in the token */
for (i = 0; i < num_sids; i++) {
struct dom_sid_buf buf;
const char *sid_dn;
DATA_BLOB sid_blob;
sid_dn = talloc_asprintf(
tmp_ctx,
"<SID=%s>",
dom_sid_str_buf(&sids[i].sid, &buf));
if (sid_dn == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
sid_blob = data_blob_string_const(sid_dn);
/* This function takes in memberOf values and expands
* them, as long as they meet the filter - so only
* builtin groups
*
* We already have the SID in the token, so set
* 'only childs' flag to true */
nt_status = dsdb_expand_nested_groups(sam_ctx, &sid_blob, true, filter,
tmp_ctx, &sids, &num_sids);
if (!NT_STATUS_IS_OK(nt_status)) {
talloc_free(tmp_ctx);
return nt_status;
}
}
}
if (device_info_dc != NULL) {
/*
* Make a copy of the device SIDs in case we need to add extra SIDs on
* the end. One can never have too much copying.
*/
num_device_sids = device_info_dc->num_sids;
device_sids = talloc_array(tmp_ctx,
struct auth_SidAttr,
num_device_sids);
if (device_sids == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
for (i = 0; i < num_device_sids; i++) {
device_sids[i] = device_info_dc->sids[i];
}
if (session_info_flags & AUTH_SESSION_INFO_DEVICE_DEFAULT_GROUPS) {
device_sids = talloc_realloc(tmp_ctx,
device_sids,
struct auth_SidAttr,
num_device_sids + 2);
if (device_sids == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
device_sids[num_device_sids++] = (struct auth_SidAttr) {
.sid = global_sid_World,
.attrs = SE_GROUP_DEFAULT_FLAGS,
};
device_sids[num_device_sids++] = (struct auth_SidAttr) {
.sid = global_sid_Network,
.attrs = SE_GROUP_DEFAULT_FLAGS,
};
}
if (session_info_flags & AUTH_SESSION_INFO_DEVICE_AUTHENTICATED) {
device_sids = talloc_realloc(tmp_ctx,
device_sids,
struct auth_SidAttr,
num_device_sids + 1);
if (device_sids == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
device_sids[num_device_sids++] = (struct auth_SidAttr) {
.sid = global_sid_Authenticated_Users,
.attrs = SE_GROUP_DEFAULT_FLAGS,
};
}
}
nt_status = security_token_create(mem_ctx,
lp_ctx,
num_sids,
sids,
num_device_sids,
device_sids,
auth_claims,
session_info_flags,
&security_token);
if (!NT_STATUS_IS_OK(nt_status)) {
TALLOC_FREE(tmp_ctx);
return nt_status;
}
talloc_steal(mem_ctx, security_token);
*_security_token = security_token;
talloc_free(tmp_ctx);
return NT_STATUS_OK;
}
_PUBLIC_ NTSTATUS auth_generate_session_info(TALLOC_CTX *mem_ctx,
struct loadparm_context *lp_ctx, /* Optional, if you don't want privileges */
struct ldb_context *sam_ctx, /* Optional, if you don't want local groups */
const struct auth_user_info_dc *user_info_dc,
uint32_t session_info_flags,
struct auth_session_info **_session_info)
{
struct auth_session_info *session_info;
NTSTATUS nt_status;
TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
session_info = talloc_zero(tmp_ctx, struct auth_session_info);
if (session_info == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
session_info->info = talloc_reference(session_info, user_info_dc->info);
if (session_info->info == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
session_info->torture = talloc_zero(session_info, struct auth_user_info_torture);
if (session_info->torture == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
session_info->torture->num_dc_sids = user_info_dc->num_sids;
session_info->torture->dc_sids = talloc_reference(session_info, user_info_dc->sids);
if (session_info->torture->dc_sids == NULL) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
/* unless set otherwise, the session key is the user session
* key from the auth subsystem */
session_info->session_key = data_blob_talloc(session_info, user_info_dc->user_session_key.data, user_info_dc->user_session_key.length);
if (!session_info->session_key.data && user_info_dc->user_session_key.length) {
TALLOC_FREE(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
nt_status = auth_generate_security_token(session_info,
lp_ctx,
sam_ctx,
user_info_dc,
NULL /*device_info_dc */,
(struct auth_claims) {},
session_info_flags,
&session_info->security_token);
if (!NT_STATUS_IS_OK(nt_status)) {
TALLOC_FREE(tmp_ctx);
return nt_status;
}
session_info->unique_session_token = GUID_random();
session_info->credentials = NULL;
session_info->ticket_type = user_info_dc->ticket_type;
talloc_steal(mem_ctx, session_info);
*_session_info = session_info;
talloc_free(tmp_ctx);
return NT_STATUS_OK;
}
/* Fill out the auth_session_info with a cli_credentials based on the
* auth_session_info we were forwarded over named pipe forwarding.
*
* NOTE: The structure members of session_info_transport are stolen
* with talloc_move() into auth_session_info for long term use
*/
struct auth_session_info *auth_session_info_from_transport(TALLOC_CTX *mem_ctx,
struct auth_session_info_transport *session_info_transport,
struct loadparm_context *lp_ctx,
const char **reason)
{
struct auth_session_info *session_info;
session_info = talloc_steal(mem_ctx, session_info_transport->session_info);
/*
* This is to allow us to check the type of this pointer using
* talloc_get_type()
*/
talloc_set_name(session_info, "struct auth_session_info");
#ifdef HAVE_GSS_IMPORT_CRED
if (session_info_transport->exported_gssapi_credentials.length) {
struct cli_credentials *creds;
OM_uint32 minor_status;
gss_buffer_desc cred_token;
gss_cred_id_t cred_handle;
const char *error_string;
int ret;
bool ok;
DEBUG(10, ("Delegated credentials supplied by client\n"));
cred_token.value = session_info_transport->exported_gssapi_credentials.data;
cred_token.length = session_info_transport->exported_gssapi_credentials.length;
ret = gss_import_cred(&minor_status,
&cred_token,
&cred_handle);
if (ret != GSS_S_COMPLETE) {
*reason = "Internal error in gss_import_cred()";
return NULL;
}
creds = cli_credentials_init(session_info);
if (!creds) {
*reason = "Out of memory in cli_credentials_init()";
return NULL;
}
session_info->credentials = creds;
ok = cli_credentials_set_conf(creds, lp_ctx);
if (!ok) {
*reason = "Failed to load smb.conf";
return NULL;
}
/* Just so we don't segfault trying to get at a username */
cli_credentials_set_anonymous(creds);
ret = cli_credentials_set_client_gss_creds(creds,
lp_ctx,
cred_handle,
CRED_SPECIFIED,
&error_string);
if (ret) {
*reason = talloc_asprintf(mem_ctx,
"Failed to set pipe forwarded "
"creds: %s\n", error_string);
return NULL;
}
/* This credential handle isn't useful for password
* authentication, so ensure nobody tries to do that */
cli_credentials_set_kerberos_state(creds,
CRED_USE_KERBEROS_REQUIRED,
CRED_SPECIFIED);
}
#endif
return session_info;
}
/* Create a auth_session_info_transport from an auth_session_info.
*
* NOTE: Members of the auth_session_info_transport structure are
* talloc_referenced() into this structure, and should not be changed.
*/
NTSTATUS auth_session_info_transport_from_session(TALLOC_CTX *mem_ctx,
struct auth_session_info *session_info,
struct tevent_context *event_ctx,
struct loadparm_context *lp_ctx,
struct auth_session_info_transport **transport_out)
{
struct auth_session_info_transport *session_info_transport
= talloc_zero(mem_ctx, struct auth_session_info_transport);
if (!session_info_transport) {
return NT_STATUS_NO_MEMORY;
};
session_info_transport->session_info = talloc_reference(session_info_transport, session_info);
if (!session_info_transport->session_info) {
return NT_STATUS_NO_MEMORY;
};
#ifdef HAVE_GSS_EXPORT_CRED
if (session_info->credentials) {
struct gssapi_creds_container *gcc;
OM_uint32 gret;
OM_uint32 minor_status;
gss_buffer_desc cred_token;
const char *error_string;
int ret;
ret = cli_credentials_get_client_gss_creds(session_info->credentials,
event_ctx,
lp_ctx,
&gcc, &error_string);
if (ret != 0) {
*transport_out = session_info_transport;
return NT_STATUS_OK;
}
gret = gss_export_cred(&minor_status,
gcc->creds,
&cred_token);
if (gret != GSS_S_COMPLETE) {
return NT_STATUS_INTERNAL_ERROR;
}
if (cred_token.length) {
session_info_transport->exported_gssapi_credentials
= data_blob_talloc(session_info_transport,
cred_token.value,
cred_token.length);
gss_release_buffer(&minor_status, &cred_token);
NT_STATUS_HAVE_NO_MEMORY(session_info_transport->exported_gssapi_credentials.data);
}
}
#endif
*transport_out = session_info_transport;
return NT_STATUS_OK;
}
/* Produce a session_info for an arbitrary DN or principal in the local
* DB, assuming the local DB holds all the groups
*
* Supply either a principal or a DN
*/
NTSTATUS authsam_get_session_info_principal(TALLOC_CTX *mem_ctx,
struct loadparm_context *lp_ctx,
struct ldb_context *sam_ctx,
const char *principal,
struct ldb_dn *user_dn,
uint32_t session_info_flags,
struct auth_session_info **session_info)
{
NTSTATUS nt_status;
struct auth_user_info_dc *user_info_dc;
TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
if (!tmp_ctx) {
return NT_STATUS_NO_MEMORY;
}
nt_status = authsam_get_user_info_dc_principal(tmp_ctx, lp_ctx, sam_ctx,
principal, user_dn,
&user_info_dc);
if (!NT_STATUS_IS_OK(nt_status)) {
talloc_free(tmp_ctx);
return nt_status;
}
nt_status = auth_generate_session_info(tmp_ctx, lp_ctx, sam_ctx,
user_info_dc,
session_info_flags,
session_info);
if (NT_STATUS_IS_OK(nt_status)) {
talloc_steal(mem_ctx, *session_info);
}
talloc_free(tmp_ctx);
return nt_status;
}
/**
* prints a struct auth_session_info security token to debug output.
*/
void auth_session_info_debug(int dbg_lev,
const struct auth_session_info *session_info)
{
if (!session_info) {
DEBUG(dbg_lev, ("Session Info: (NULL)\n"));
return;
}
security_token_debug(DBGC_AUTH, dbg_lev,
session_info->security_token);
}
NTSTATUS encode_claims_set(TALLOC_CTX *mem_ctx,
struct CLAIMS_SET *claims_set,
DATA_BLOB *claims_blob)
{
TALLOC_CTX *tmp_ctx = NULL;
enum ndr_err_code ndr_err;
struct CLAIMS_SET_NDR *claims_set_info = NULL;
struct CLAIMS_SET_METADATA *metadata = NULL;
struct CLAIMS_SET_METADATA_NDR *metadata_ndr = NULL;
if (claims_blob == NULL) {
return NT_STATUS_INVALID_PARAMETER_3;
}
tmp_ctx = talloc_new(mem_ctx);
if (tmp_ctx == NULL) {
return NT_STATUS_NO_MEMORY;
}
metadata_ndr = talloc(tmp_ctx, struct CLAIMS_SET_METADATA_NDR);
if (metadata_ndr == NULL) {
talloc_free(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
metadata = talloc(metadata_ndr, struct CLAIMS_SET_METADATA);
if (metadata == NULL) {
talloc_free(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
claims_set_info = talloc(metadata, struct CLAIMS_SET_NDR);
if (claims_set_info == NULL) {
talloc_free(tmp_ctx);
return NT_STATUS_NO_MEMORY;
}
*metadata_ndr = (struct CLAIMS_SET_METADATA_NDR) {
.claims.metadata = metadata,
};
*metadata = (struct CLAIMS_SET_METADATA) {
.claims_set = claims_set_info,
.compression_format = CLAIMS_COMPRESSION_FORMAT_XPRESS_HUFF,
};
*claims_set_info = (struct CLAIMS_SET_NDR) {
.claims.claims = claims_set,
};
ndr_err = ndr_push_struct_blob(claims_blob, mem_ctx, metadata_ndr,
(ndr_push_flags_fn_t)ndr_push_CLAIMS_SET_METADATA_NDR);
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
DBG_ERR("CLAIMS_SET_METADATA_NDR push failed: %s\n",
nt_errstr(nt_status));
talloc_free(tmp_ctx);
return nt_status;
}
talloc_free(tmp_ctx);
return NT_STATUS_OK;
}
/*
* Construct a ‘claims_data’ structure from a claims blob, such as is found in a
* PAC.
*/
NTSTATUS claims_data_from_encoded_claims_set(TALLOC_CTX *claims_data_ctx,
const DATA_BLOB *encoded_claims_set,
struct claims_data **out)
{
struct claims_data *claims_data = NULL;
DATA_BLOB data = {};
if (out == NULL) {
return NT_STATUS_INVALID_PARAMETER;
}
*out = NULL;
claims_data = talloc(claims_data_ctx, struct claims_data);
if (claims_data == NULL) {
return NT_STATUS_NO_MEMORY;
}
if (encoded_claims_set != NULL) {
/*
* We make a copy of the data, for it might not be
* talloc‐allocated — we might have obtained it directly with
* krb5_pac_get_buffer().
*/
data = data_blob_dup_talloc(claims_data, *encoded_claims_set);
if (data.length != encoded_claims_set->length) {
talloc_free(claims_data);
return NT_STATUS_NO_MEMORY;
}
}
*claims_data = (struct claims_data) {
.encoded_claims_set = data,
.flags = CLAIMS_DATA_ENCODED_CLAIMS_PRESENT,
};
*out = claims_data;
return NT_STATUS_OK;
}
/*
* Construct a ‘claims_data’ structure from a talloc‐allocated claims set, such
* as we might build from searching the database. If this function returns
* successfully, it assumes ownership of the claims set.
*/
NTSTATUS claims_data_from_claims_set(TALLOC_CTX *claims_data_ctx,
struct CLAIMS_SET *claims_set,
struct claims_data **out)
{
struct claims_data *claims_data = NULL;
if (out == NULL) {
return NT_STATUS_INVALID_PARAMETER;
}
*out = NULL;
claims_data = talloc(claims_data_ctx, struct claims_data);
if (claims_data == NULL) {
return NT_STATUS_NO_MEMORY;
}
*claims_data = (struct claims_data) {
.claims_set = talloc_steal(claims_data, claims_set),
.flags = CLAIMS_DATA_CLAIMS_PRESENT,
};
*out = claims_data;
return NT_STATUS_OK;
}
/*
* From a ‘claims_data’ structure, return an encoded claims blob that can be put
* into a PAC.
*/
NTSTATUS claims_data_encoded_claims_set(TALLOC_CTX *mem_ctx,
struct claims_data *claims_data,
DATA_BLOB *encoded_claims_set_out)
{
uint8_t *data = NULL;
size_t len;
if (encoded_claims_set_out == NULL) {
return NT_STATUS_INVALID_PARAMETER;
}
*encoded_claims_set_out = data_blob_null;
if (claims_data == NULL) {
return NT_STATUS_OK;
}
if (!(claims_data->flags & CLAIMS_DATA_ENCODED_CLAIMS_PRESENT)) {
NTSTATUS status;
/* See whether we have a claims set that we can encode. */
if (!(claims_data->flags & CLAIMS_DATA_CLAIMS_PRESENT)) {
return NT_STATUS_OK;
}
status = encode_claims_set(claims_data,
claims_data->claims_set,
&claims_data->encoded_claims_set);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
claims_data->flags |= CLAIMS_DATA_ENCODED_CLAIMS_PRESENT;
}
if (claims_data->encoded_claims_set.data != NULL) {
data = talloc_reference(mem_ctx, claims_data->encoded_claims_set.data);
if (data == NULL) {
return NT_STATUS_NO_MEMORY;
}
}
len = claims_data->encoded_claims_set.length;
*encoded_claims_set_out = data_blob_const(data, len);
return NT_STATUS_OK;
}
/*
* From a ‘claims_data’ structure, return an array of security claims that can
* be put in a security token for access checks.
*/
NTSTATUS claims_data_security_claims(TALLOC_CTX *mem_ctx,
struct claims_data *claims_data,
struct CLAIM_SECURITY_ATTRIBUTE_RELATIVE_V1 **security_claims_out,
uint32_t *n_security_claims_out)
{
struct CLAIM_SECURITY_ATTRIBUTE_RELATIVE_V1 *security_claims = NULL;
uint32_t n_security_claims;
NTSTATUS status;
if (security_claims_out == NULL) {
return NT_STATUS_INVALID_PARAMETER;
}
if (n_security_claims_out == NULL) {
return NT_STATUS_INVALID_PARAMETER;
}
*security_claims_out = NULL;
*n_security_claims_out = 0;
if (claims_data == NULL) {
return NT_STATUS_OK;
}
if (!(claims_data->flags & CLAIMS_DATA_SECURITY_CLAIMS_PRESENT)) {
struct CLAIM_SECURITY_ATTRIBUTE_RELATIVE_V1 *decoded_claims = NULL;
uint32_t n_decoded_claims = 0;
/* See whether we have a claims set that we can convert. */
if (!(claims_data->flags & CLAIMS_DATA_CLAIMS_PRESENT)) {
/*
* See whether we have an encoded claims set that we can
* decode.
*/
if (!(claims_data->flags & CLAIMS_DATA_ENCODED_CLAIMS_PRESENT)) {
/* We don’t have anything. */
return NT_STATUS_OK;
}
/* Decode an existing claims set. */
if (claims_data->encoded_claims_set.length) {
TALLOC_CTX *tmp_ctx = NULL;
struct CLAIMS_SET_METADATA_NDR claims;
const struct CLAIMS_SET_METADATA *metadata = NULL;
enum ndr_err_code ndr_err;
tmp_ctx = talloc_new(claims_data);
if (tmp_ctx == NULL) {
return NT_STATUS_NO_MEMORY;
}
ndr_err = ndr_pull_struct_blob(&claims_data->encoded_claims_set,
tmp_ctx,
&claims,
(ndr_pull_flags_fn_t)ndr_pull_CLAIMS_SET_METADATA_NDR);
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
status = ndr_map_error2ntstatus(ndr_err);
DBG_ERR("Failed to parse encoded claims set: %s\n",
nt_errstr(status));
talloc_free(tmp_ctx);
return status;
}
metadata = claims.claims.metadata;
if (metadata != NULL) {
struct CLAIMS_SET_NDR *claims_set_ndr = metadata->claims_set;
if (claims_set_ndr != NULL) {
struct CLAIMS_SET **claims_set = &claims_set_ndr->claims.claims;
claims_data->claims_set = talloc_move(claims_data, claims_set);
}
}
talloc_free(tmp_ctx);
}
claims_data->flags |= CLAIMS_DATA_CLAIMS_PRESENT;
}
/*
* Convert the decoded claims set to the security attribute
* claims format.
*/
status = token_claims_to_claims_v1(claims_data,
claims_data->claims_set,
&decoded_claims,
&n_decoded_claims);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
claims_data->security_claims = decoded_claims;
claims_data->n_security_claims = n_decoded_claims;
claims_data->flags |= CLAIMS_DATA_SECURITY_CLAIMS_PRESENT;
}
if (claims_data->security_claims != NULL) {
security_claims = talloc_reference(mem_ctx, claims_data->security_claims);
if (security_claims == NULL) {
return NT_STATUS_NO_MEMORY;
}
}
n_security_claims = claims_data->n_security_claims;
*security_claims_out = security_claims;
*n_security_claims_out = n_security_claims;
return NT_STATUS_OK;
}
|