Coverage Report

Created: 2025-07-23 07:04

/src/samba/source4/kdc/pac-glue.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
   Unix SMB/CIFS implementation.
3
4
   PAC Glue between Samba and the KDC
5
6
   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2009
7
   Copyright (C) Simo Sorce <idra@samba.org> 2010
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
20
   You should have received a copy of the GNU General Public License
21
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
*/
23
24
#include "lib/replace/replace.h"
25
#include "lib/replace/system/kerberos.h"
26
#include "lib/replace/system/filesys.h"
27
#include "lib/util/debug.h"
28
#include "lib/util/samba_util.h"
29
#include "lib/util/talloc_stack.h"
30
31
#include "auth/auth_sam_reply.h"
32
#include "auth/kerberos/kerberos.h"
33
#include "auth/kerberos/pac_utils.h"
34
#include "auth/authn_policy.h"
35
#include "libcli/security/security.h"
36
#include "librpc/gen_ndr/ndr_security.h"
37
#include "libds/common/flags.h"
38
#include "librpc/gen_ndr/ndr_krb5pac.h"
39
#include "librpc/gen_ndr/claims.h"
40
#include "param/param.h"
41
#include "source4/auth/auth.h"
42
#include "source4/dsdb/common/util.h"
43
#include "source4/dsdb/samdb/samdb.h"
44
#include "source4/kdc/authn_policy_util.h"
45
#include "source4/kdc/samba_kdc.h"
46
#include "source4/kdc/pac-glue.h"
47
#include "source4/kdc/ad_claims.h"
48
#include "source4/kdc/pac-blobs.h"
49
50
#include <ldb.h>
51
52
#undef DBGC_CLASS
53
0
#define DBGC_CLASS DBGC_KERBEROS
54
55
static
56
NTSTATUS samba_get_logon_info_pac_blob(TALLOC_CTX *mem_ctx,
57
               const struct auth_user_info_dc *info,
58
               const struct PAC_DOMAIN_GROUP_MEMBERSHIP *override_resource_groups,
59
               const enum auth_group_inclusion group_inclusion,
60
               DATA_BLOB *pac_data)
61
0
{
62
0
  TALLOC_CTX *tmp_ctx = NULL;
63
0
  struct netr_SamInfo3 *info3 = NULL;
64
0
  struct PAC_DOMAIN_GROUP_MEMBERSHIP *_resource_groups = NULL;
65
0
  struct PAC_DOMAIN_GROUP_MEMBERSHIP **resource_groups = NULL;
66
0
  union PAC_INFO pac_info = {};
67
0
  enum ndr_err_code ndr_err;
68
0
  NTSTATUS nt_status = NT_STATUS_OK;
69
70
0
  *pac_data = data_blob_null;
71
72
0
  tmp_ctx = talloc_new(mem_ctx);
73
0
  if (tmp_ctx == NULL) {
74
0
    return NT_STATUS_NO_MEMORY;
75
0
  }
76
77
0
  if (override_resource_groups == NULL) {
78
0
    resource_groups = &_resource_groups;
79
0
  } else if (group_inclusion != AUTH_EXCLUDE_RESOURCE_GROUPS) {
80
    /*
81
     * It doesn't make sense to override resource groups if we claim
82
     * to want resource groups from user_info_dc.
83
     */
84
0
    DBG_ERR("supplied resource groups with invalid group inclusion parameter: %u\n",
85
0
      group_inclusion);
86
0
    nt_status = NT_STATUS_INVALID_PARAMETER;
87
0
    goto out;
88
0
  }
89
90
0
  nt_status = auth_convert_user_info_dc_saminfo3(tmp_ctx, info,
91
0
                   group_inclusion,
92
0
                   &info3,
93
0
                   resource_groups);
94
0
  if (!NT_STATUS_IS_OK(nt_status)) {
95
0
    DBG_WARNING("Getting Samba info failed: %s\n",
96
0
          nt_errstr(nt_status));
97
0
    goto out;
98
0
  }
99
100
0
  pac_info.logon_info.info = talloc_zero(tmp_ctx, struct PAC_LOGON_INFO);
101
0
  if (!pac_info.logon_info.info) {
102
0
    nt_status = NT_STATUS_NO_MEMORY;
103
0
    goto out;
104
0
  }
105
106
0
  pac_info.logon_info.info->info3 = *info3;
107
0
  if (_resource_groups != NULL) {
108
0
    pac_info.logon_info.info->resource_groups = *_resource_groups;
109
0
  }
110
111
0
  if (override_resource_groups != NULL) {
112
0
    pac_info.logon_info.info->resource_groups = *override_resource_groups;
113
0
  }
114
115
0
  if (group_inclusion != AUTH_EXCLUDE_RESOURCE_GROUPS) {
116
    /*
117
     * Set the resource groups flag based on whether any groups are
118
     * present. Otherwise, the flag is propagated from the
119
     * originating PAC.
120
     */
121
0
    if (pac_info.logon_info.info->resource_groups.groups.count > 0) {
122
0
      pac_info.logon_info.info->info3.base.user_flags |= NETLOGON_RESOURCE_GROUPS;
123
0
    } else {
124
0
      pac_info.logon_info.info->info3.base.user_flags &= ~NETLOGON_RESOURCE_GROUPS;
125
0
    }
126
0
  }
127
128
0
  ndr_err = ndr_push_union_blob(pac_data, mem_ctx, &pac_info,
129
0
              PAC_TYPE_LOGON_INFO,
130
0
              (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
131
0
  if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
132
0
    nt_status = ndr_map_error2ntstatus(ndr_err);
133
0
    DBG_WARNING("PAC_LOGON_INFO (presig) push failed: %s\n",
134
0
          nt_errstr(nt_status));
135
0
    goto out;
136
0
  }
137
138
0
out:
139
0
  talloc_free(tmp_ctx);
140
0
  return nt_status;
141
0
}
142
143
static
144
NTSTATUS samba_get_upn_info_pac_blob(TALLOC_CTX *mem_ctx,
145
             const struct auth_user_info_dc *info,
146
             DATA_BLOB *upn_data)
147
0
{
148
0
  TALLOC_CTX *tmp_ctx = NULL;
149
0
  union PAC_INFO pac_upn = {};
150
0
  enum ndr_err_code ndr_err;
151
0
  NTSTATUS nt_status = NT_STATUS_OK;
152
0
  bool ok;
153
154
0
  *upn_data = data_blob_null;
155
156
0
  tmp_ctx = talloc_new(mem_ctx);
157
0
  if (tmp_ctx == NULL) {
158
0
    return NT_STATUS_NO_MEMORY;
159
0
  }
160
161
0
  pac_upn.upn_dns_info.upn_name = info->info->user_principal_name;
162
0
  pac_upn.upn_dns_info.dns_domain_name = strupper_talloc(tmp_ctx,
163
0
            info->info->dns_domain_name);
164
0
  if (pac_upn.upn_dns_info.dns_domain_name == NULL) {
165
0
    nt_status = NT_STATUS_NO_MEMORY;
166
0
    goto out;
167
0
  }
168
0
  if (info->info->user_principal_constructed) {
169
0
    pac_upn.upn_dns_info.flags |= PAC_UPN_DNS_FLAG_CONSTRUCTED;
170
0
  }
171
172
0
  pac_upn.upn_dns_info.flags |= PAC_UPN_DNS_FLAG_HAS_SAM_NAME_AND_SID;
173
174
0
  pac_upn.upn_dns_info.ex.sam_name_and_sid.samaccountname
175
0
    = info->info->account_name;
176
177
0
  pac_upn.upn_dns_info.ex.sam_name_and_sid.objectsid
178
0
    = &info->sids[PRIMARY_USER_SID_INDEX].sid;
179
180
0
  ndr_err = ndr_push_union_blob(upn_data, mem_ctx, &pac_upn,
181
0
              PAC_TYPE_UPN_DNS_INFO,
182
0
              (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
183
0
  if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
184
0
    nt_status = ndr_map_error2ntstatus(ndr_err);
185
0
    DBG_WARNING("PAC UPN_DNS_INFO (presig) push failed: %s\n",
186
0
          nt_errstr(nt_status));
187
0
    goto out;
188
0
  }
189
190
0
  ok = data_blob_pad(mem_ctx, upn_data, 8);
191
0
  if (!ok) {
192
0
    talloc_free(upn_data);
193
0
    nt_status = NT_STATUS_NO_MEMORY;
194
0
    goto out;
195
0
  }
196
197
0
out:
198
0
  talloc_free(tmp_ctx);
199
0
  return nt_status;
200
0
}
201
202
static
203
NTSTATUS samba_get_cred_info_ndr_blob(TALLOC_CTX *mem_ctx,
204
              const struct ldb_message *msg,
205
              DATA_BLOB *cred_blob)
206
0
{
207
0
  enum ndr_err_code ndr_err;
208
0
  NTSTATUS nt_status;
209
0
  struct samr_Password *lm_hash = NULL;
210
0
  struct samr_Password *nt_hash = NULL;
211
0
  struct PAC_CREDENTIAL_NTLM_SECPKG ntlm_secpkg = {
212
0
    .version = 0,
213
0
  };
214
0
  DATA_BLOB ntlm_blob = data_blob_null;
215
0
  struct PAC_CREDENTIAL_SUPPLEMENTAL_SECPKG secpkgs[1] = {{
216
0
    .credential_size = 0,
217
0
  }};
218
0
  struct PAC_CREDENTIAL_DATA cred_data = {
219
0
    .credential_count = 0,
220
0
  };
221
0
  struct PAC_CREDENTIAL_DATA_NDR cred_ndr = {};
222
223
0
  *cred_blob = data_blob_null;
224
225
0
  lm_hash = samdb_result_hash(mem_ctx, msg, "dBCSPwd");
226
0
  if (lm_hash != NULL) {
227
0
    bool zero = all_zero(lm_hash->hash, 16);
228
0
    if (zero) {
229
0
      lm_hash = NULL;
230
0
    }
231
0
  }
232
0
  if (lm_hash != NULL) {
233
0
    DBG_INFO("Passing LM password hash through credentials set\n");
234
0
    ntlm_secpkg.flags |= PAC_CREDENTIAL_NTLM_HAS_LM_HASH;
235
0
    ntlm_secpkg.lm_password = *lm_hash;
236
0
    ZERO_STRUCTP(lm_hash);
237
0
    TALLOC_FREE(lm_hash);
238
0
  }
239
240
0
  nt_hash = samdb_result_hash(mem_ctx, msg, "unicodePwd");
241
0
  if (nt_hash != NULL) {
242
0
    bool zero = all_zero(nt_hash->hash, 16);
243
0
    if (zero) {
244
0
      nt_hash = NULL;
245
0
    }
246
0
  }
247
0
  if (nt_hash != NULL) {
248
0
    DBG_INFO("Passing NT password hash through credentials set\n");
249
0
    ntlm_secpkg.flags |= PAC_CREDENTIAL_NTLM_HAS_NT_HASH;
250
0
    ntlm_secpkg.nt_password = *nt_hash;
251
0
    ZERO_STRUCTP(nt_hash);
252
0
    TALLOC_FREE(nt_hash);
253
0
  }
254
255
0
  if (ntlm_secpkg.flags == 0) {
256
0
    return NT_STATUS_OK;
257
0
  }
258
259
0
#ifdef DEBUG_PASSWORD
260
0
  if (DEBUGLVL(11)) {
261
0
    NDR_PRINT_DEBUG(PAC_CREDENTIAL_NTLM_SECPKG, &ntlm_secpkg);
262
0
  }
263
0
#endif
264
265
0
  ndr_err = ndr_push_struct_blob(&ntlm_blob, mem_ctx, &ntlm_secpkg,
266
0
      (ndr_push_flags_fn_t)ndr_push_PAC_CREDENTIAL_NTLM_SECPKG);
267
0
  ZERO_STRUCT(ntlm_secpkg);
268
0
  if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
269
0
    nt_status = ndr_map_error2ntstatus(ndr_err);
270
0
    DBG_WARNING("PAC_CREDENTIAL_NTLM_SECPKG (presig) push failed: %s\n",
271
0
          nt_errstr(nt_status));
272
0
    return nt_status;
273
0
  }
274
275
0
  DBG_DEBUG("NTLM credential BLOB (len %zu) for user\n",
276
0
      ntlm_blob.length);
277
0
  dump_data_pw("PAC_CREDENTIAL_NTLM_SECPKG",
278
0
         ntlm_blob.data, ntlm_blob.length);
279
280
0
  secpkgs[0].package_name.string = discard_const_p(char, "NTLM");
281
0
  secpkgs[0].credential_size = ntlm_blob.length;
282
0
  secpkgs[0].credential = ntlm_blob.data;
283
284
0
  cred_data.credential_count = ARRAY_SIZE(secpkgs);
285
0
  cred_data.credentials = secpkgs;
286
287
0
#ifdef DEBUG_PASSWORD
288
0
  if (DEBUGLVL(11)) {
289
0
    NDR_PRINT_DEBUG(PAC_CREDENTIAL_DATA, &cred_data);
290
0
  }
291
0
#endif
292
293
0
  cred_ndr.ctr.data = &cred_data;
294
295
0
#ifdef DEBUG_PASSWORD
296
0
  if (DEBUGLVL(11)) {
297
0
    NDR_PRINT_DEBUG(PAC_CREDENTIAL_DATA_NDR, &cred_ndr);
298
0
  }
299
0
#endif
300
301
0
  ndr_err = ndr_push_struct_blob(cred_blob, mem_ctx, &cred_ndr,
302
0
      (ndr_push_flags_fn_t)ndr_push_PAC_CREDENTIAL_DATA_NDR);
303
0
  data_blob_clear(&ntlm_blob);
304
0
  if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
305
0
    nt_status = ndr_map_error2ntstatus(ndr_err);
306
0
    DBG_WARNING("PAC_CREDENTIAL_DATA_NDR (presig) push failed: %s\n",
307
0
          nt_errstr(nt_status));
308
0
    return nt_status;
309
0
  }
310
311
0
  DBG_DEBUG("Created credential BLOB (len %zu) for user\n",
312
0
      cred_blob->length);
313
0
  dump_data_pw("PAC_CREDENTIAL_DATA_NDR",
314
0
         cred_blob->data, cred_blob->length);
315
316
0
  return NT_STATUS_OK;
317
0
}
318
319
static
320
krb5_error_code samba_kdc_encrypt_pac_credentials(krb5_context context,
321
              const krb5_keyblock *pkreplykey,
322
              const DATA_BLOB *cred_ndr_blob,
323
              TALLOC_CTX *mem_ctx,
324
              DATA_BLOB *cred_info_blob)
325
0
{
326
0
#ifdef SAMBA4_USES_HEIMDAL
327
0
  krb5_crypto cred_crypto;
328
0
  krb5_enctype cred_enctype;
329
0
  krb5_data cred_ndr_crypt;
330
0
  struct PAC_CREDENTIAL_INFO pac_cred_info = { .version = 0, };
331
0
  krb5_error_code ret;
332
0
  const char *krb5err;
333
0
  enum ndr_err_code ndr_err;
334
0
  NTSTATUS nt_status;
335
336
0
  *cred_info_blob = data_blob_null;
337
338
0
  ret = krb5_crypto_init(context, pkreplykey, ETYPE_NULL,
339
0
             &cred_crypto);
340
0
  if (ret != 0) {
341
0
    krb5err = krb5_get_error_message(context, ret);
342
0
    DBG_WARNING("Failed initializing cred data crypto: %s\n", krb5err);
343
0
    krb5_free_error_message(context, krb5err);
344
0
    return ret;
345
0
  }
346
347
0
  ret = krb5_crypto_getenctype(context, cred_crypto, &cred_enctype);
348
0
  if (ret != 0) {
349
0
    DBG_WARNING("Failed getting crypto type for key\n");
350
0
    krb5_crypto_destroy(context, cred_crypto);
351
0
    return ret;
352
0
  }
353
354
0
  DBG_DEBUG("Plain cred_ndr_blob (len %zu)\n",
355
0
      cred_ndr_blob->length);
356
0
  dump_data_pw("PAC_CREDENTIAL_DATA_NDR",
357
0
         cred_ndr_blob->data, cred_ndr_blob->length);
358
359
0
  ret = krb5_encrypt(context, cred_crypto,
360
0
         KRB5_KU_OTHER_ENCRYPTED,
361
0
         cred_ndr_blob->data, cred_ndr_blob->length,
362
0
         &cred_ndr_crypt);
363
0
  krb5_crypto_destroy(context, cred_crypto);
364
0
  if (ret != 0) {
365
0
    krb5err = krb5_get_error_message(context, ret);
366
0
    DBG_WARNING("Failed crypt of cred data: %s\n", krb5err);
367
0
    krb5_free_error_message(context, krb5err);
368
0
    return ret;
369
0
  }
370
371
0
  pac_cred_info.encryption_type = cred_enctype;
372
0
  pac_cred_info.encrypted_data.length = cred_ndr_crypt.length;
373
0
  pac_cred_info.encrypted_data.data = (uint8_t *)cred_ndr_crypt.data;
374
375
0
  if (DEBUGLVL(10)) {
376
0
    NDR_PRINT_DEBUG(PAC_CREDENTIAL_INFO, &pac_cred_info);
377
0
  }
378
379
0
  ndr_err = ndr_push_struct_blob(cred_info_blob, mem_ctx, &pac_cred_info,
380
0
      (ndr_push_flags_fn_t)ndr_push_PAC_CREDENTIAL_INFO);
381
0
  krb5_data_free(&cred_ndr_crypt);
382
0
  if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
383
0
    nt_status = ndr_map_error2ntstatus(ndr_err);
384
0
    DBG_WARNING("PAC_CREDENTIAL_INFO (presig) push failed: %s\n",
385
0
          nt_errstr(nt_status));
386
0
    return KRB5KDC_ERR_SVC_UNAVAILABLE;
387
0
  }
388
389
0
  DBG_DEBUG("Encrypted credential BLOB (len %zu) with alg %"PRId32"\n",
390
0
      cred_info_blob->length, pac_cred_info.encryption_type);
391
0
  dump_data_pw("PAC_CREDENTIAL_INFO",
392
0
          cred_info_blob->data, cred_info_blob->length);
393
394
0
  return 0;
395
#else /* SAMBA4_USES_HEIMDAL */
396
  TALLOC_CTX *tmp_ctx = NULL;
397
  krb5_key cred_key;
398
  krb5_enctype cred_enctype;
399
  struct PAC_CREDENTIAL_INFO pac_cred_info = { .version = 0, };
400
  krb5_error_code code = 0;
401
  const char *krb5err;
402
  enum ndr_err_code ndr_err;
403
  NTSTATUS nt_status;
404
  krb5_data cred_ndr_data;
405
  krb5_enc_data cred_ndr_crypt;
406
  size_t enc_len = 0;
407
408
  *cred_info_blob = data_blob_null;
409
410
  tmp_ctx = talloc_new(mem_ctx);
411
  if (tmp_ctx == NULL) {
412
    return ENOMEM;
413
  }
414
415
  code = krb5_k_create_key(context,
416
         pkreplykey,
417
         &cred_key);
418
  if (code != 0) {
419
    krb5err = krb5_get_error_message(context, code);
420
    DBG_WARNING("Failed initializing cred data crypto: %s\n", krb5err);
421
    krb5_free_error_message(context, krb5err);
422
    goto out;
423
  }
424
425
  cred_enctype = krb5_k_key_enctype(context, cred_key);
426
427
  DBG_DEBUG("Plain cred_ndr_blob (len %zu)\n",
428
      cred_ndr_blob->length);
429
  dump_data_pw("PAC_CREDENTIAL_DATA_NDR",
430
         cred_ndr_blob->data, cred_ndr_blob->length);
431
432
  pac_cred_info.encryption_type = cred_enctype;
433
434
  cred_ndr_data = smb_krb5_data_from_blob(*cred_ndr_blob);
435
436
  code = krb5_c_encrypt_length(context,
437
             cred_enctype,
438
             cred_ndr_data.length,
439
             &enc_len);
440
  if (code != 0) {
441
    krb5err = krb5_get_error_message(context, code);
442
    DBG_WARNING("Failed initializing cred data crypto: %s\n", krb5err);
443
    krb5_free_error_message(context, krb5err);
444
    goto out;
445
  }
446
447
  pac_cred_info.encrypted_data = data_blob_talloc_zero(tmp_ctx, enc_len);
448
  if (pac_cred_info.encrypted_data.data == NULL) {
449
    DBG_ERR("Out of memory\n");
450
    code = ENOMEM;
451
    goto out;
452
  }
453
454
  cred_ndr_crypt.ciphertext = smb_krb5_data_from_blob(pac_cred_info.encrypted_data);
455
456
  code = krb5_k_encrypt(context,
457
            cred_key,
458
            KRB5_KU_OTHER_ENCRYPTED,
459
            NULL,
460
            &cred_ndr_data,
461
            &cred_ndr_crypt);
462
  krb5_k_free_key(context, cred_key);
463
  if (code != 0) {
464
    krb5err = krb5_get_error_message(context, code);
465
    DBG_WARNING("Failed crypt of cred data: %s\n", krb5err);
466
    krb5_free_error_message(context, krb5err);
467
    goto out;
468
  }
469
470
  if (DEBUGLVL(10)) {
471
    NDR_PRINT_DEBUG(PAC_CREDENTIAL_INFO, &pac_cred_info);
472
  }
473
474
  ndr_err = ndr_push_struct_blob(cred_info_blob, mem_ctx, &pac_cred_info,
475
      (ndr_push_flags_fn_t)ndr_push_PAC_CREDENTIAL_INFO);
476
  TALLOC_FREE(pac_cred_info.encrypted_data.data);
477
  if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
478
    nt_status = ndr_map_error2ntstatus(ndr_err);
479
    DBG_WARNING("PAC_CREDENTIAL_INFO (presig) push failed: %s\n",
480
          nt_errstr(nt_status));
481
    code = KRB5KDC_ERR_SVC_UNAVAILABLE;
482
    goto out;
483
  }
484
485
  DBG_DEBUG("Encrypted credential BLOB (len %zu) with alg %"PRId32"\n",
486
      cred_info_blob->length, pac_cred_info.encryption_type);
487
  dump_data_pw("PAC_CREDENTIAL_INFO",
488
          cred_info_blob->data, cred_info_blob->length);
489
490
out:
491
  talloc_free(tmp_ctx);
492
  return code;
493
#endif /* SAMBA4_USES_HEIMDAL */
494
0
}
495
496
497
/**
498
 * @brief Create a PAC with the given blobs (logon, credentials, upn and
499
 * delegation).
500
 *
501
 * @param[in] context   The KRB5 context to use.
502
 *
503
 * @param[in] logon_blob Fill the logon info PAC buffer with the given blob,
504
 *                       use NULL to ignore it.
505
 *
506
 * @param[in] cred_blob  Fill the credentials info PAC buffer with the given
507
 *                       blob, use NULL to ignore it.
508
 *
509
 * @param[in] upn_blob  Fill the UPN info PAC buffer with the given blob, use
510
 *                      NULL to ignore it.
511
 *
512
 * @param[in] deleg_blob Fill the delegation info PAC buffer with the given
513
 *                       blob, use NULL to ignore it.
514
 *
515
 * @param[in] client_claims_blob Fill the client claims info PAC buffer with the
516
 *                               given blob, use NULL to ignore it.
517
 *
518
 * @param[in] device_info_blob Fill the device info PAC buffer with the given
519
 *                             blob, use NULL to ignore it.
520
 *
521
 * @param[in] device_claims_blob Fill the device claims info PAC buffer with the given
522
 *                               blob, use NULL to ignore it.
523
 *
524
 * @param[in] pac        The pac buffer to fill. This should be allocated with
525
 *                       krb5_pac_init() already.
526
 *
527
 * @returns 0 on success or a corresponding KRB5 error.
528
 */
529
static
530
krb5_error_code samba_make_krb5_pac(krb5_context context,
531
            const DATA_BLOB *logon_blob,
532
            const DATA_BLOB *cred_blob,
533
            const DATA_BLOB *upn_blob,
534
            const DATA_BLOB *pac_attrs_blob,
535
            const DATA_BLOB *requester_sid_blob,
536
            const DATA_BLOB *deleg_blob,
537
            const DATA_BLOB *client_claims_blob,
538
            const DATA_BLOB *device_info_blob,
539
            const DATA_BLOB *device_claims_blob,
540
            krb5_pac pac)
541
0
{
542
0
  krb5_data logon_data;
543
0
  krb5_error_code ret;
544
0
  char null_byte = '\0';
545
0
  krb5_data null_data = smb_krb5_make_data(&null_byte, 0);
546
547
  /* The user account may be set not to want the PAC */
548
0
  if (logon_blob == NULL) {
549
0
    return 0;
550
0
  }
551
552
0
  logon_data = smb_krb5_data_from_blob(*logon_blob);
553
0
  ret = krb5_pac_add_buffer(context, pac, PAC_TYPE_LOGON_INFO, &logon_data);
554
0
  if (ret != 0) {
555
0
    return ret;
556
0
  }
557
558
0
  if (device_info_blob != NULL) {
559
0
    krb5_data device_info_data = smb_krb5_data_from_blob(*device_info_blob);
560
0
    ret = krb5_pac_add_buffer(context, pac,
561
0
            PAC_TYPE_DEVICE_INFO,
562
0
            &device_info_data);
563
0
    if (ret != 0) {
564
0
      return ret;
565
0
    }
566
0
  }
567
568
0
  if (client_claims_blob != NULL) {
569
0
    krb5_data client_claims_data;
570
0
    krb5_data *data = NULL;
571
572
0
    if (client_claims_blob->length != 0) {
573
0
      client_claims_data = smb_krb5_data_from_blob(*client_claims_blob);
574
0
      data = &client_claims_data;
575
0
    } else {
576
0
      data = &null_data;
577
0
    }
578
579
0
    ret = krb5_pac_add_buffer(context, pac,
580
0
            PAC_TYPE_CLIENT_CLAIMS_INFO,
581
0
            data);
582
0
    if (ret != 0) {
583
0
      return ret;
584
0
    }
585
0
  }
586
587
0
  if (device_claims_blob != NULL) {
588
0
    krb5_data device_claims_data = smb_krb5_data_from_blob(*device_claims_blob);
589
0
    ret = krb5_pac_add_buffer(context, pac,
590
0
            PAC_TYPE_DEVICE_CLAIMS_INFO,
591
0
            &device_claims_data);
592
0
    if (ret != 0) {
593
0
      return ret;
594
0
    }
595
0
  }
596
597
0
  if (cred_blob != NULL) {
598
0
    krb5_data cred_data = smb_krb5_data_from_blob(*cred_blob);
599
0
    ret = krb5_pac_add_buffer(context, pac,
600
0
            PAC_TYPE_CREDENTIAL_INFO,
601
0
            &cred_data);
602
0
    if (ret != 0) {
603
0
      return ret;
604
0
    }
605
0
  }
606
607
0
#ifdef SAMBA4_USES_HEIMDAL
608
  /*
609
   * null_data will be filled by the generic KDC code in the caller
610
   * here we just add it in order to have it before
611
   * PAC_TYPE_UPN_DNS_INFO
612
   *
613
   * Not needed with MIT Kerberos - asn
614
   */
615
0
  ret = krb5_pac_add_buffer(context, pac,
616
0
          PAC_TYPE_LOGON_NAME,
617
0
          &null_data);
618
0
  if (ret != 0) {
619
0
    return ret;
620
0
  }
621
0
#endif
622
623
0
  if (upn_blob != NULL) {
624
0
    krb5_data upn_data = smb_krb5_data_from_blob(*upn_blob);
625
0
    ret = krb5_pac_add_buffer(context, pac,
626
0
            PAC_TYPE_UPN_DNS_INFO,
627
0
            &upn_data);
628
0
    if (ret != 0) {
629
0
      return ret;
630
0
    }
631
0
  }
632
633
0
  if (pac_attrs_blob != NULL) {
634
0
    krb5_data pac_attrs_data = smb_krb5_data_from_blob(*pac_attrs_blob);
635
0
    ret = krb5_pac_add_buffer(context, pac,
636
0
            PAC_TYPE_ATTRIBUTES_INFO,
637
0
            &pac_attrs_data);
638
0
    if (ret != 0) {
639
0
      return ret;
640
0
    }
641
0
  }
642
643
0
  if (requester_sid_blob != NULL) {
644
0
    krb5_data requester_sid_data = smb_krb5_data_from_blob(*requester_sid_blob);
645
0
    ret = krb5_pac_add_buffer(context, pac,
646
0
            PAC_TYPE_REQUESTER_SID,
647
0
            &requester_sid_data);
648
0
    if (ret != 0) {
649
0
      return ret;
650
0
    }
651
0
  }
652
653
0
  if (deleg_blob != NULL) {
654
0
    krb5_data deleg_data = smb_krb5_data_from_blob(*deleg_blob);
655
0
    ret = krb5_pac_add_buffer(context, pac,
656
0
            PAC_TYPE_CONSTRAINED_DELEGATION,
657
0
            &deleg_data);
658
0
    if (ret != 0) {
659
0
      return ret;
660
0
    }
661
0
  }
662
663
0
  return ret;
664
0
}
665
666
bool samba_princ_needs_pac(const struct samba_kdc_entry *skdc_entry)
667
0
{
668
669
0
  uint32_t userAccountControl;
670
671
  /* The service account may be set not to want the PAC */
672
0
  userAccountControl = ldb_msg_find_attr_as_uint(skdc_entry->msg, "userAccountControl", 0);
673
0
  if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
674
0
    return false;
675
0
  }
676
677
0
  return true;
678
0
}
679
680
static krb5_error_code samba_client_requested_pac(krb5_context context,
681
              const krb5_const_pac pac,
682
              TALLOC_CTX *mem_ctx,
683
              bool *requested_pac)
684
0
{
685
0
  enum ndr_err_code ndr_err;
686
0
  krb5_data k5pac_attrs_in;
687
0
  DATA_BLOB pac_attrs_in;
688
0
  union PAC_INFO pac_attrs;
689
0
  krb5_error_code ret;
690
691
0
  *requested_pac = true;
692
693
0
  ret = krb5_pac_get_buffer(context, pac, PAC_TYPE_ATTRIBUTES_INFO,
694
0
          &k5pac_attrs_in);
695
0
  if (ret != 0) {
696
0
    return ret == ENOENT ? 0 : ret;
697
0
  }
698
699
0
  pac_attrs_in = data_blob_const(k5pac_attrs_in.data,
700
0
               k5pac_attrs_in.length);
701
702
0
  ndr_err = ndr_pull_union_blob(&pac_attrs_in, mem_ctx, &pac_attrs,
703
0
              PAC_TYPE_ATTRIBUTES_INFO,
704
0
              (ndr_pull_flags_fn_t)ndr_pull_PAC_INFO);
705
0
  smb_krb5_free_data_contents(context, &k5pac_attrs_in);
706
0
  if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
707
0
    NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
708
0
    DBG_ERR("can't parse the PAC ATTRIBUTES_INFO: %s\n", nt_errstr(nt_status));
709
0
    return map_errno_from_nt_status(nt_status);
710
0
  }
711
712
0
  if (pac_attrs.attributes_info.flags & (PAC_ATTRIBUTE_FLAG_PAC_WAS_GIVEN_IMPLICITLY
713
0
                 | PAC_ATTRIBUTE_FLAG_PAC_WAS_REQUESTED)) {
714
0
    *requested_pac = true;
715
0
  } else {
716
0
    *requested_pac = false;
717
0
  }
718
719
0
  return 0;
720
0
}
721
722
/* Was the krbtgt in this DB (ie, should we check the incoming signature) and was it an RODC */
723
krb5_error_code samba_krbtgt_is_in_db(const struct samba_kdc_entry *p,
724
              bool *is_in_db,
725
              bool *is_trusted)
726
0
{
727
0
  NTSTATUS status;
728
0
  krb5_error_code ret;
729
0
  int rodc_krbtgt_number, trust_direction;
730
0
  struct dom_sid sid;
731
0
  uint32_t rid;
732
733
0
  trust_direction = ldb_msg_find_attr_as_int(p->msg, "trustDirection", 0);
734
735
0
  if (trust_direction != 0) {
736
    /* Domain trust - we cannot check the sig, but we trust it for a correct PAC
737
738
       This is exactly where we should flag for SID
739
       validation when we do inter-forest trusts
740
     */
741
0
    *is_trusted = true;
742
0
    *is_in_db = false;
743
0
    return 0;
744
0
  }
745
746
  /* The lack of password controls etc applies to krbtgt by
747
   * virtue of being that particular RID */
748
0
  ret = samdb_result_dom_sid_buf(p->msg, "objectSid", &sid);
749
0
  if (ret) {
750
0
    return ret;
751
0
  }
752
753
0
  status = dom_sid_split_rid(NULL, &sid, NULL, &rid);
754
0
  if (!NT_STATUS_IS_OK(status)) {
755
0
    return map_errno_from_nt_status(status);
756
0
  }
757
758
0
  rodc_krbtgt_number = ldb_msg_find_attr_as_int(p->msg, "msDS-SecondaryKrbTgtNumber", -1);
759
760
0
  if (p->kdc_db_ctx->my_krbtgt_number == 0) {
761
0
    if (rid == DOMAIN_RID_KRBTGT) {
762
0
      *is_trusted = true;
763
0
      *is_in_db = true;
764
0
      return 0;
765
0
    } else if (rodc_krbtgt_number != -1) {
766
0
      *is_in_db = true;
767
0
      *is_trusted = false;
768
0
      return 0;
769
0
    }
770
0
  } else if ((rid != DOMAIN_RID_KRBTGT) && (rodc_krbtgt_number == p->kdc_db_ctx->my_krbtgt_number)) {
771
0
    *is_trusted = true;
772
0
    *is_in_db = true;
773
0
    return 0;
774
0
  } else if (rid == DOMAIN_RID_KRBTGT) {
775
    /* krbtgt viewed from an RODC */
776
0
    *is_trusted = true;
777
0
    *is_in_db = false;
778
0
    return 0;
779
0
  }
780
781
  /* Another RODC */
782
0
  *is_trusted = false;
783
0
  *is_in_db = false;
784
0
  return 0;
785
0
}
786
787
/*
788
 * Because the KDC does not limit protocol transition, two new well-known SIDs
789
 * were introduced to give this control to the resource administrator. These
790
 * SIDs identify whether protocol transition has occurred, and can be used with
791
 * standard access control lists to grant or limit access as needed.
792
 *
793
 * https://docs.microsoft.com/en-us/windows-server/security/kerberos/kerberos-constrained-delegation-overview
794
 */
795
static
796
NTSTATUS samba_kdc_add_asserted_identity(enum samba_asserted_identity ai,
797
           struct auth_user_info_dc *user_info_dc)
798
0
{
799
0
  const struct dom_sid *ai_sid = NULL;
800
801
0
  switch (ai) {
802
0
  case SAMBA_ASSERTED_IDENTITY_SERVICE:
803
0
    ai_sid = &global_sid_Asserted_Identity_Service;
804
0
    break;
805
0
  case SAMBA_ASSERTED_IDENTITY_AUTHENTICATION_AUTHORITY:
806
0
    ai_sid = &global_sid_Asserted_Identity_Authentication_Authority;
807
0
    break;
808
0
  case SAMBA_ASSERTED_IDENTITY_IGNORE:
809
0
    return NT_STATUS_OK;
810
0
  default:
811
0
    return NT_STATUS_INVALID_PARAMETER;
812
0
  }
813
814
0
  return add_sid_to_array_attrs_unique(
815
0
    user_info_dc,
816
0
    ai_sid,
817
0
    SE_GROUP_DEFAULT_FLAGS,
818
0
    &user_info_dc->sids,
819
0
    &user_info_dc->num_sids);
820
0
}
821
822
static
823
NTSTATUS samba_kdc_add_claims_valid(struct auth_user_info_dc *user_info_dc)
824
0
{
825
0
  return add_sid_to_array_attrs_unique(
826
0
    user_info_dc,
827
0
    &global_sid_Claims_Valid,
828
0
    SE_GROUP_DEFAULT_FLAGS,
829
0
    &user_info_dc->sids,
830
0
    &user_info_dc->num_sids);
831
0
}
832
833
static
834
NTSTATUS samba_kdc_add_fresh_public_key_identity(struct auth_user_info_dc *user_info_dc)
835
0
{
836
0
  return add_sid_to_array_attrs_unique(
837
0
    user_info_dc,
838
0
    &global_sid_Fresh_Public_Key_Identity,
839
0
    SE_GROUP_DEFAULT_FLAGS,
840
0
    &user_info_dc->sids,
841
0
    &user_info_dc->num_sids);
842
0
}
843
844
static NTSTATUS samba_kdc_add_compounded_auth(struct auth_user_info_dc *user_info_dc)
845
0
{
846
  /*
847
   * Windows 2025 adds it even if it's
848
   * already there, so we do...
849
   */
850
0
  return add_sid_to_array_attrs(
851
0
    user_info_dc,
852
0
    &global_sid_Compounded_Authentication,
853
0
    SE_GROUP_DEFAULT_FLAGS,
854
0
    &user_info_dc->sids,
855
0
    &user_info_dc->num_sids);
856
0
}
857
858
static bool samba_kdc_entry_is_trust(const struct samba_kdc_entry *entry)
859
0
{
860
0
  return entry != NULL && entry->is_trust;
861
0
}
862
863
/*
864
 * Return true if this entry has an associated PAC issued or signed by a KDC
865
 * that our KDC trusts. We trust the main krbtgt account, but we don’t trust any
866
 * RODC krbtgt besides ourselves.
867
 */
868
static bool samba_krb5_pac_is_trusted(const struct samba_kdc_entry_pac pac)
869
0
{
870
0
  if (pac.pac == NULL) {
871
0
    return false;
872
0
  }
873
874
0
#ifdef HAVE_KRB5_PAC_IS_TRUSTED /* Heimdal */
875
0
  return krb5_pac_is_trusted(pac.pac);
876
#else /* MIT */
877
  return pac.pac_is_trusted;
878
#endif /* HAVE_KRB5_PAC_IS_TRUSTED */
879
0
}
880
881
#ifdef HAVE_KRB5_PAC_IS_TRUSTED /* Heimdal */
882
struct samba_kdc_entry_pac samba_kdc_entry_pac(krb5_const_pac pac,
883
                 krb5_const_principal pac_princ,
884
                 struct samba_kdc_entry *entry,
885
                 const struct samba_kdc_entry *krbtgt)
886
0
{
887
0
  if (pac != NULL) {
888
0
    SMB_ASSERT(pac_princ != NULL);
889
0
    SMB_ASSERT(krbtgt != NULL);
890
0
  } else {
891
0
    pac_princ = NULL;
892
0
    krbtgt = NULL;
893
0
    entry = NULL;
894
0
  }
895
896
0
  return (struct samba_kdc_entry_pac) {
897
0
    .pac = pac,
898
0
    .pac_princ = pac_princ,
899
0
    .krbtgt = krbtgt,
900
0
    .entry = entry,
901
0
  };
902
0
}
903
#else /* MIT */
904
struct samba_kdc_entry_pac samba_kdc_entry_pac_from_trusted(krb5_const_pac pac,
905
                  krb5_const_principal pac_princ,
906
                  struct samba_kdc_entry *entry,
907
                  const struct samba_kdc_entry *krbtgt,
908
                  bool is_trusted)
909
{
910
  if (pac != NULL) {
911
    /*
912
     * TODO: we can't assert this yet,
913
     * as mit_samba_update_pac() does not
914
     * get this for cross realm clients.
915
     *
916
     * SMB_ASSERT(pac_princ != NULL);
917
     */
918
    SMB_ASSERT(krbtgt != NULL);
919
  } else {
920
    pac_princ = NULL;
921
    krbtgt = NULL;
922
    entry = NULL;
923
    is_trusted = false;
924
  }
925
926
  return (struct samba_kdc_entry_pac) {
927
    .pac = pac,
928
    .pac_princ = pac_princ,
929
    .krbtgt = krbtgt,
930
    .entry = entry,
931
    .pac_is_trusted = is_trusted,
932
  };
933
}
934
#endif /* HAVE_KRB5_PAC_IS_TRUSTED */
935
936
static bool samba_kdc_entry_pac_issued_by_trust(const struct samba_kdc_entry_pac entry)
937
0
{
938
0
  return entry.pac != NULL && samba_kdc_entry_is_trust(entry.krbtgt);
939
0
}
940
941
/*
942
 * Return true if a principal is represented.
943
 *
944
 * This only returns false if the following are
945
 * all NULL pointers:
946
 *
947
 * struct samba_kdc_entry *entry;
948
 * const struct samba_kdc_entry *krbtgt;
949
 * krb5_const_pac pac;
950
 *
951
 * This should only for a 'device_pac_entry' if FAST was not used
952
 * and there's no decive ticket. Or similar cases where it
953
 * represents optional things.
954
 */
955
static bool samba_kdc_entry_pac_valid_principal(
956
    const struct samba_kdc_entry_pac entry)
957
0
{
958
0
  return entry.pac != NULL || entry.entry != NULL || entry.krbtgt != NULL;
959
0
}
960
961
static
962
NTSTATUS samba_kdc_get_logon_info_blob(TALLOC_CTX *mem_ctx,
963
               const struct auth_user_info_dc *user_info_dc,
964
               const struct PAC_DOMAIN_GROUP_MEMBERSHIP *override_resource_groups,
965
               const enum auth_group_inclusion group_inclusion,
966
               DATA_BLOB **_logon_info_blob)
967
0
{
968
0
  DATA_BLOB *logon_blob = NULL;
969
0
  NTSTATUS nt_status;
970
971
0
  *_logon_info_blob = NULL;
972
973
0
  logon_blob = talloc_zero(mem_ctx, DATA_BLOB);
974
0
  if (logon_blob == NULL) {
975
0
    return NT_STATUS_NO_MEMORY;
976
0
  }
977
978
0
  nt_status = samba_get_logon_info_pac_blob(logon_blob,
979
0
              user_info_dc,
980
0
              override_resource_groups,
981
0
              group_inclusion,
982
0
              logon_blob);
983
0
  if (!NT_STATUS_IS_OK(nt_status)) {
984
0
    DBG_ERR("Building PAC LOGON INFO failed: %s\n",
985
0
      nt_errstr(nt_status));
986
0
    talloc_free(logon_blob);
987
0
    return nt_status;
988
0
  }
989
990
0
  *_logon_info_blob = logon_blob;
991
992
0
  return NT_STATUS_OK;
993
0
}
994
995
static
996
NTSTATUS samba_kdc_get_cred_ndr_blob(TALLOC_CTX *mem_ctx,
997
             const struct samba_kdc_entry *p,
998
             DATA_BLOB **_cred_ndr_blob)
999
0
{
1000
0
  DATA_BLOB *cred_blob = NULL;
1001
0
  NTSTATUS nt_status;
1002
1003
0
  SMB_ASSERT(_cred_ndr_blob != NULL);
1004
1005
0
  *_cred_ndr_blob = NULL;
1006
1007
0
  cred_blob = talloc_zero(mem_ctx, DATA_BLOB);
1008
0
  if (cred_blob == NULL) {
1009
0
    return NT_STATUS_NO_MEMORY;
1010
0
  }
1011
1012
0
  nt_status = samba_get_cred_info_ndr_blob(cred_blob,
1013
0
             p->msg,
1014
0
             cred_blob);
1015
0
  if (!NT_STATUS_IS_OK(nt_status)) {
1016
0
    DBG_ERR("Building PAC CRED INFO failed: %s\n",
1017
0
      nt_errstr(nt_status));
1018
0
    talloc_free(cred_blob);
1019
0
    return nt_status;
1020
0
  }
1021
1022
0
  *_cred_ndr_blob = cred_blob;
1023
1024
0
  return NT_STATUS_OK;
1025
0
}
1026
1027
static
1028
NTSTATUS samba_kdc_get_upn_info_blob(TALLOC_CTX *mem_ctx,
1029
             const struct auth_user_info_dc *user_info_dc,
1030
             DATA_BLOB **_upn_info_blob)
1031
0
{
1032
0
  DATA_BLOB *upn_blob = NULL;
1033
0
  NTSTATUS nt_status;
1034
1035
0
  *_upn_info_blob = NULL;
1036
1037
0
  upn_blob = talloc_zero(mem_ctx, DATA_BLOB);
1038
0
  if (upn_blob == NULL) {
1039
0
    return NT_STATUS_NO_MEMORY;
1040
0
  }
1041
1042
0
  nt_status = samba_get_upn_info_pac_blob(upn_blob,
1043
0
            user_info_dc,
1044
0
            upn_blob);
1045
0
  if (!NT_STATUS_IS_OK(nt_status)) {
1046
0
    DBG_ERR("Building PAC UPN INFO failed: %s\n",
1047
0
      nt_errstr(nt_status));
1048
0
    talloc_free(upn_blob);
1049
0
    return nt_status;
1050
0
  }
1051
1052
0
  *_upn_info_blob = upn_blob;
1053
1054
0
  return NT_STATUS_OK;
1055
0
}
1056
1057
static
1058
NTSTATUS samba_kdc_get_pac_attrs_blob(TALLOC_CTX *mem_ctx,
1059
              uint64_t pac_attributes,
1060
              DATA_BLOB **_pac_attrs_blob)
1061
0
{
1062
0
  DATA_BLOB *pac_attrs_blob = NULL;
1063
0
  union PAC_INFO pac_attrs = {};
1064
0
  enum ndr_err_code ndr_err;
1065
0
  NTSTATUS nt_status;
1066
1067
0
  SMB_ASSERT(_pac_attrs_blob != NULL);
1068
1069
0
  *_pac_attrs_blob = NULL;
1070
1071
0
  pac_attrs_blob = talloc_zero(mem_ctx, DATA_BLOB);
1072
0
  if (pac_attrs_blob == NULL) {
1073
0
    return NT_STATUS_NO_MEMORY;
1074
0
  }
1075
1076
  /* Set the length of the flags in bits. */
1077
0
  pac_attrs.attributes_info.flags_length = 2;
1078
0
  pac_attrs.attributes_info.flags = pac_attributes;
1079
1080
0
  ndr_err = ndr_push_union_blob(pac_attrs_blob, pac_attrs_blob, &pac_attrs,
1081
0
              PAC_TYPE_ATTRIBUTES_INFO,
1082
0
              (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
1083
0
  if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1084
0
    nt_status = ndr_map_error2ntstatus(ndr_err);
1085
0
    DBG_WARNING("PAC ATTRIBUTES_INFO (presig) push failed: %s\n",
1086
0
          nt_errstr(nt_status));
1087
0
    DBG_ERR("Building PAC ATTRIBUTES failed: %s\n",
1088
0
      nt_errstr(nt_status));
1089
1090
0
    talloc_free(pac_attrs_blob);
1091
0
    return nt_status;
1092
0
  }
1093
1094
0
  *_pac_attrs_blob = pac_attrs_blob;
1095
1096
0
  return NT_STATUS_OK;
1097
0
}
1098
1099
static
1100
NTSTATUS samba_kdc_get_requester_sid_blob(TALLOC_CTX *mem_ctx,
1101
            const struct auth_user_info_dc *user_info_dc,
1102
            DATA_BLOB **_requester_sid_blob)
1103
0
{
1104
0
  DATA_BLOB *requester_sid_blob = NULL;
1105
0
  NTSTATUS nt_status;
1106
1107
0
  SMB_ASSERT(_requester_sid_blob != NULL);
1108
1109
0
  *_requester_sid_blob = NULL;
1110
1111
0
  requester_sid_blob = talloc_zero(mem_ctx, DATA_BLOB);
1112
0
  if (requester_sid_blob == NULL) {
1113
0
    return NT_STATUS_NO_MEMORY;
1114
0
  }
1115
1116
0
  if (user_info_dc->num_sids > 0) {
1117
0
    union PAC_INFO pac_requester_sid = {};
1118
0
    enum ndr_err_code ndr_err;
1119
1120
0
    pac_requester_sid.requester_sid.sid = user_info_dc->sids[PRIMARY_USER_SID_INDEX].sid;
1121
1122
0
    ndr_err = ndr_push_union_blob(requester_sid_blob, requester_sid_blob,
1123
0
                &pac_requester_sid,
1124
0
                PAC_TYPE_REQUESTER_SID,
1125
0
                (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
1126
0
    if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1127
0
      nt_status = ndr_map_error2ntstatus(ndr_err);
1128
0
      DBG_WARNING("PAC_REQUESTER_SID (presig) push failed: %s\n",
1129
0
            nt_errstr(nt_status));
1130
0
      DBG_ERR("Building PAC REQUESTER SID failed: %s\n",
1131
0
        nt_errstr(nt_status));
1132
1133
0
      talloc_free(requester_sid_blob);
1134
0
      return nt_status;
1135
0
    }
1136
0
  }
1137
1138
0
  *_requester_sid_blob = requester_sid_blob;
1139
1140
0
  return NT_STATUS_OK;
1141
0
}
1142
1143
static
1144
krb5_error_code samba_kdc_get_claims_data_from_db(struct ldb_context *samdb,
1145
              struct samba_kdc_entry *entry,
1146
              struct claims_data **claims_data_out);
1147
1148
static bool claims_tf_rule_set_is_deny_all(const struct claims_tf_rule_set *rs)
1149
0
{
1150
0
  if (rs->num_rules != 0) {
1151
0
    return false;
1152
0
  }
1153
1154
0
  return true;
1155
0
}
1156
1157
static bool claims_tf_rule_set_is_allow_all(const struct claims_tf_rule_set *rs)
1158
0
{
1159
0
  const struct claims_tf_rule *r = NULL;
1160
0
  const struct claims_tf_condition_set *cs = NULL;
1161
0
  const struct claims_tf_rule_action *a = NULL;
1162
0
  bool match;
1163
1164
0
  if (rs->num_rules != 1) {
1165
0
    return false;
1166
0
  }
1167
0
  r = &rs->rules[0];
1168
1169
0
  if (r->num_condition_sets != 1) {
1170
0
    return false;
1171
0
  }
1172
0
  cs = &r->condition_sets[0];
1173
0
  a = &r->action;
1174
1175
0
  if (cs->num_conditions != 0) {
1176
0
    return false;
1177
0
  }
1178
1179
0
  if (cs->opt_identifier == NULL) {
1180
0
    return false;
1181
0
  }
1182
1183
0
  if (a->type.ref.property != CLAIMS_TF_PROPERTY_TYPE) {
1184
0
    return false;
1185
0
  }
1186
0
  match = strequal(a->type.ref.identifier,
1187
0
       cs->opt_identifier);
1188
0
  if (!match) {
1189
0
    return false;
1190
0
  }
1191
1192
0
  if (a->value.ref.property != CLAIMS_TF_PROPERTY_VALUE) {
1193
0
    return false;
1194
0
  }
1195
0
  match = strequal(a->value.ref.identifier,
1196
0
       cs->opt_identifier);
1197
0
  if (!match) {
1198
0
    return false;
1199
0
  }
1200
1201
0
  if (a->value_type.ref.property != CLAIMS_TF_PROPERTY_VALUE_TYPE) {
1202
0
    return false;
1203
0
  }
1204
0
  match = strequal(a->value_type.ref.identifier,
1205
0
       cs->opt_identifier);
1206
0
  if (!match) {
1207
0
    return false;
1208
0
  }
1209
1210
0
  return true;
1211
0
}
1212
1213
static
1214
NTSTATUS samba_kdc_get_claims_blob(TALLOC_CTX *mem_ctx,
1215
           struct samba_kdc_db_context *kdc_db_ctx,
1216
           const struct samba_kdc_entry *server,
1217
           struct claims_data *claims_data,
1218
           const DATA_BLOB **_claims_blob)
1219
0
{
1220
0
  DATA_BLOB *claims_blob = NULL;
1221
0
  NTSTATUS nt_status;
1222
1223
0
  SMB_ASSERT(_claims_blob != NULL);
1224
1225
0
  *_claims_blob = NULL;
1226
1227
0
  claims_blob = talloc_zero(mem_ctx, DATA_BLOB);
1228
0
  if (claims_blob == NULL) {
1229
0
    return NT_STATUS_NO_MEMORY;
1230
0
  }
1231
1232
0
  if (samba_kdc_entry_is_trust(server)) {
1233
0
    const char *tdo_attr = "msDS-EgressClaimsTransformationPolicy";
1234
0
    struct claims_tf_rule_set *rule_set = NULL;
1235
0
    NTSTATUS status;
1236
0
    bool keep_claims = false;
1237
0
    bool clear_claims = false;
1238
1239
0
    status = dsdb_trust_get_claims_tf_policy(kdc_db_ctx->samdb,
1240
0
               server->msg,
1241
0
               tdo_attr,
1242
0
               mem_ctx,
1243
0
               &rule_set);
1244
0
    if (NT_STATUS_EQUAL(status, NT_STATUS_DS_NO_ATTRIBUTE_OR_VALUE)) {
1245
      /*
1246
       * No msDS-EgressClaimsTransformationPolicy
1247
       * or no msDS-TransformationRules attribute
1248
       *
1249
       * This is the default and matches
1250
       * an explicit rule of:
1251
       * 'C1:[] => ISSUE(Claim=C1);'
1252
       */
1253
0
      keep_claims = true;
1254
0
    } else if (NT_STATUS_IS_OK(status)) {
1255
      /*
1256
       * There's a policy defined.
1257
       *
1258
       * For now we only allow it an
1259
       * empty rule set, which is deny
1260
       * all claims policy. Or a single
1261
       * allow all policy.
1262
       *
1263
       * All others result in an error
1264
       * in order to avoid unexpected behavior
1265
       *
1266
       * TODO apply the transformation completely:
1267
       * only apply everything from rule_set
1268
       */
1269
1270
0
      clear_claims = claims_tf_rule_set_is_deny_all(rule_set);
1271
0
      keep_claims = claims_tf_rule_set_is_allow_all(rule_set);
1272
0
      if (!clear_claims && !keep_claims) {
1273
        /* TODO: is there some better status ? */
1274
0
        return NT_STATUS_ACCESS_DISABLED_BY_POLICY_OTHER;
1275
0
      }
1276
0
    } else {
1277
      /*
1278
       * We hit an error, which means we need to
1279
       * clear the claims
1280
       */
1281
0
      DBG_WARNING("dsdb_trust_get_claims_tf_policy() %s\n",
1282
0
            nt_errstr(status));
1283
0
      clear_claims = true;
1284
0
    }
1285
1286
0
    if (clear_claims) {
1287
0
      SMB_ASSERT(!keep_claims);
1288
0
      claims_data = NULL;
1289
0
    } else {
1290
0
      SMB_ASSERT(keep_claims);
1291
0
    }
1292
0
  }
1293
1294
0
  nt_status = claims_data_encoded_claims_set(claims_blob,
1295
0
               claims_data,
1296
0
               claims_blob);
1297
0
  if (!NT_STATUS_IS_OK(nt_status)) {
1298
0
    talloc_free(claims_blob);
1299
0
    return nt_status;
1300
0
  }
1301
1302
0
  *_claims_blob = claims_blob;
1303
1304
0
  return NT_STATUS_OK;
1305
0
}
1306
1307
krb5_error_code samba_kdc_get_user_info_from_db(TALLOC_CTX *mem_ctx,
1308
            struct samba_kdc_db_context *kdc_db_ctx,
1309
            struct samba_kdc_entry *entry,
1310
            const struct ldb_message *msg,
1311
            const struct auth_user_info_dc **info_out)
1312
0
{
1313
0
  NTSTATUS nt_status;
1314
1315
0
  if (kdc_db_ctx == NULL) {
1316
0
    return EINVAL;
1317
0
  }
1318
1319
0
  if (msg == NULL) {
1320
0
    return EINVAL;
1321
0
  }
1322
1323
0
  if (info_out == NULL) {
1324
0
    return EINVAL;
1325
0
  }
1326
1327
0
  if (entry == NULL) {
1328
0
    return KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
1329
0
  }
1330
1331
0
  *info_out = NULL;
1332
1333
0
  if (entry->info_from_db == NULL) {
1334
0
    struct auth_user_info_dc *info_from_db = NULL;
1335
0
    struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
1336
1337
0
    nt_status = authsam_make_user_info_dc(entry,
1338
0
                  kdc_db_ctx->samdb,
1339
0
                  lpcfg_netbios_name(lp_ctx),
1340
0
                  lpcfg_sam_name(lp_ctx),
1341
0
                  lpcfg_sam_dnsname(lp_ctx),
1342
0
                  entry->realm_dn,
1343
0
                  msg,
1344
0
                  data_blob_null,
1345
0
                  data_blob_null,
1346
0
                  &info_from_db);
1347
0
    if (!NT_STATUS_IS_OK(nt_status)) {
1348
0
      DBG_ERR("Getting user info for PAC failed: %s\n",
1349
0
        nt_errstr(nt_status));
1350
      /* NT_STATUS_OBJECT_NAME_NOT_FOUND is mapped to ENOENT. */
1351
0
      return map_errno_from_nt_status(nt_status);
1352
0
    }
1353
1354
0
    entry->info_from_db = info_from_db;
1355
0
  }
1356
1357
0
  *info_out = entry->info_from_db;
1358
1359
0
  return 0;
1360
0
}
1361
1362
/*
1363
 * Check whether a PAC contains the Authentication Authority Asserted Identity
1364
 * SID.
1365
 */
1366
static krb5_error_code samba_kdc_pac_contains_asserted_identity(
1367
  krb5_context context,
1368
  const struct samba_kdc_entry_pac entry,
1369
  bool *contains_out)
1370
0
{
1371
0
  TALLOC_CTX *frame = NULL;
1372
0
  struct auth_user_info_dc *info = NULL;
1373
0
  krb5_error_code ret = 0;
1374
1375
0
  if (contains_out == NULL) {
1376
0
    ret = EINVAL;
1377
0
    goto out;
1378
0
  }
1379
0
  *contains_out = false;
1380
1381
0
  frame = talloc_stackframe();
1382
1383
  /*
1384
   * Extract our info from the PAC. This does a bit of unnecessary work,
1385
   * setting up fields we don’t care about — we only want the SIDs.
1386
   */
1387
0
  ret = kerberos_pac_to_user_info_dc(frame,
1388
0
             entry.pac,
1389
0
             context,
1390
0
             &info,
1391
0
             AUTH_EXCLUDE_RESOURCE_GROUPS,
1392
0
             NULL /* pac_srv_sig */,
1393
0
             NULL /* pac_kdc_sig */,
1394
             /* Ignore the resource groups. */
1395
0
             NULL /* resource_groups */);
1396
0
  if (ret) {
1397
0
    const char *krb5err = krb5_get_error_message(context, ret);
1398
0
    DBG_ERR("kerberos_pac_to_user_info_dc failed: %s\n",
1399
0
      krb5err != NULL ? krb5err : "?");
1400
0
    krb5_free_error_message(context, krb5err);
1401
1402
0
    goto out;
1403
0
  }
1404
1405
  /* Determine whether the PAC contains the Asserted Identity SID. */
1406
0
  *contains_out = sid_attrs_contains_sid(
1407
0
    info->sids,
1408
0
    info->num_sids,
1409
0
    &global_sid_Asserted_Identity_Authentication_Authority);
1410
1411
0
out:
1412
0
  talloc_free(frame);
1413
0
  return ret;
1414
0
}
1415
1416
static krb5_error_code samba_kdc_get_user_info_from_pac(TALLOC_CTX *mem_ctx,
1417
              krb5_context context,
1418
              struct samba_kdc_db_context *kdc_db_ctx,
1419
              const struct samba_kdc_entry_pac entry,
1420
              const struct auth_user_info_dc **info_out,
1421
              const struct PAC_DOMAIN_GROUP_MEMBERSHIP **resource_groups_out)
1422
0
{
1423
0
  TALLOC_CTX *frame = NULL;
1424
0
  struct ldb_context *samdb = kdc_db_ctx->samdb;
1425
0
  struct auth_user_info_dc *info = NULL;
1426
0
  struct PAC_DOMAIN_GROUP_MEMBERSHIP *resource_groups = NULL;
1427
0
  krb5_error_code ret = 0;
1428
0
  NTSTATUS nt_status;
1429
1430
0
  if (samdb == NULL) {
1431
0
    ret = EINVAL;
1432
0
    goto out;
1433
0
  }
1434
1435
0
  if (!samba_krb5_pac_is_trusted(entry)) {
1436
0
    ret = EINVAL;
1437
0
    goto out;
1438
0
  }
1439
1440
0
  if (info_out == NULL) {
1441
0
    ret = EINVAL;
1442
0
    goto out;
1443
0
  }
1444
1445
0
  *info_out = NULL;
1446
0
  if (resource_groups_out != NULL) {
1447
0
    *resource_groups_out = NULL;
1448
0
  }
1449
1450
0
  if (entry.entry == NULL || entry.entry->info_from_pac == NULL) {
1451
0
    frame = talloc_stackframe();
1452
1453
0
    ret = kerberos_pac_to_user_info_dc(frame,
1454
0
               entry.pac,
1455
0
               context,
1456
0
               &info,
1457
0
               AUTH_EXCLUDE_RESOURCE_GROUPS,
1458
0
               NULL,
1459
0
               NULL,
1460
0
               &resource_groups);
1461
0
    if (ret) {
1462
0
      const char *krb5err = krb5_get_error_message(context, ret);
1463
0
      DBG_ERR("kerberos_pac_to_user_info_dc failed: %s\n",
1464
0
        krb5err != NULL ? krb5err : "?");
1465
0
      krb5_free_error_message(context, krb5err);
1466
1467
0
      goto out;
1468
0
    }
1469
1470
    /*
1471
     * We need to expand group memberships within our local domain,
1472
     * as the token might be generated by a trusted domain.
1473
     */
1474
0
    nt_status = authsam_update_user_info_dc(frame,
1475
0
              samdb,
1476
0
              info);
1477
0
    if (!NT_STATUS_IS_OK(nt_status)) {
1478
0
      DBG_ERR("authsam_update_user_info_dc failed: %s\n",
1479
0
        nt_errstr(nt_status));
1480
1481
0
      ret = map_errno_from_nt_status(nt_status);
1482
0
      goto out;
1483
0
    }
1484
1485
0
    if (entry.entry != NULL) {
1486
0
      entry.entry->info_from_pac = talloc_steal(entry.entry, info);
1487
0
      entry.entry->resource_groups_from_pac = talloc_steal(entry.entry, resource_groups);
1488
0
    }
1489
0
  }
1490
1491
1492
0
  if (entry.entry != NULL) {
1493
    /* Note: the caller does not own this! */
1494
0
    *info_out = entry.entry->info_from_pac;
1495
1496
0
    if (resource_groups_out != NULL) {
1497
      /* Note: the caller does not own this! */
1498
0
      *resource_groups_out = entry.entry->resource_groups_from_pac;
1499
0
    }
1500
0
  } else {
1501
0
    *info_out = talloc_steal(mem_ctx, info);
1502
1503
0
    if (resource_groups_out != NULL) {
1504
0
      *resource_groups_out = talloc_steal(mem_ctx, resource_groups);
1505
0
    }
1506
0
  }
1507
1508
0
out:
1509
0
  talloc_free(frame);
1510
0
  return ret;
1511
0
}
1512
1513
static
1514
krb5_error_code samba_kdc_get_user_info_dc(TALLOC_CTX *mem_ctx,
1515
             krb5_context context,
1516
             struct samba_kdc_db_context *kdc_db_ctx,
1517
             const struct samba_kdc_entry_pac entry,
1518
             const struct auth_user_info_dc **info_out,
1519
             const struct PAC_DOMAIN_GROUP_MEMBERSHIP **resource_groups_out)
1520
0
{
1521
0
  const struct auth_user_info_dc *info = NULL;
1522
0
  struct auth_user_info_dc *info_shallow_copy = NULL;
1523
0
  bool pac_contains_asserted_identity = false;
1524
0
  krb5_error_code ret = 0;
1525
0
  NTSTATUS nt_status;
1526
1527
0
  *info_out = NULL;
1528
0
  if (resource_groups_out != NULL) {
1529
0
    *resource_groups_out = NULL;
1530
0
  }
1531
1532
0
  if (samba_krb5_pac_is_trusted(entry)) {
1533
0
    return samba_kdc_get_user_info_from_pac(mem_ctx,
1534
0
              context,
1535
0
              kdc_db_ctx,
1536
0
              entry,
1537
0
              info_out,
1538
0
              resource_groups_out);
1539
0
  }
1540
1541
0
  if (entry.entry == NULL) {
1542
0
    return KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
1543
0
  }
1544
1545
  /*
1546
   * In this case the RWDC discards the PAC an RODC generated.
1547
   * Windows adds the asserted_identity in this case too.
1548
   *
1549
   * Note that SAMBA_KDC_FLAG_CONSTRAINED_DELEGATION
1550
   * generates KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN.
1551
   * So we can always use
1552
   * SAMBA_ASSERTED_IDENTITY_AUTHENTICATION_AUTHORITY
1553
   * here.
1554
   */
1555
0
  ret = samba_kdc_get_user_info_from_db(mem_ctx,
1556
0
                kdc_db_ctx,
1557
0
                entry.entry,
1558
0
                entry.entry->msg,
1559
0
                &info);
1560
0
  if (ret) {
1561
0
    const char *krb5err = krb5_get_error_message(context, ret);
1562
0
    DBG_ERR("samba_kdc_get_user_info_from_db: %s\n",
1563
0
      krb5err != NULL ? krb5err : "?");
1564
0
    krb5_free_error_message(context, krb5err);
1565
1566
0
    return KRB5KDC_ERR_TGT_REVOKED;
1567
0
  }
1568
1569
  /* Make a shallow copy of the user_info_dc structure. */
1570
0
  nt_status = authsam_shallow_copy_user_info_dc(mem_ctx,
1571
0
                  info,
1572
0
                  &info_shallow_copy);
1573
0
  info = NULL;
1574
1575
0
  if (!NT_STATUS_IS_OK(nt_status)) {
1576
0
    DBG_ERR("Failed to allocate user_info_dc SIDs: %s\n",
1577
0
      nt_errstr(nt_status));
1578
0
    return map_errno_from_nt_status(nt_status);
1579
0
  }
1580
1581
  /* Determine whether the PAC contains the Asserted Identity SID. */
1582
0
  ret = samba_kdc_pac_contains_asserted_identity(
1583
0
    context, entry, &pac_contains_asserted_identity);
1584
0
  if (ret) {
1585
0
    return ret;
1586
0
  }
1587
1588
0
  if (pac_contains_asserted_identity) {
1589
0
    nt_status = samba_kdc_add_asserted_identity(
1590
0
      SAMBA_ASSERTED_IDENTITY_AUTHENTICATION_AUTHORITY,
1591
0
      info_shallow_copy);
1592
0
    if (!NT_STATUS_IS_OK(nt_status)) {
1593
0
      DBG_ERR("Failed to add asserted identity: %s\n",
1594
0
        nt_errstr(nt_status));
1595
0
      TALLOC_FREE(info_shallow_copy);
1596
0
      return KRB5KDC_ERR_TGT_REVOKED;
1597
0
    }
1598
0
  }
1599
1600
0
  nt_status = samba_kdc_add_claims_valid(info_shallow_copy);
1601
0
  if (!NT_STATUS_IS_OK(nt_status)) {
1602
0
    DBG_ERR("Failed to add Claims Valid: %s\n",
1603
0
      nt_errstr(nt_status));
1604
0
    TALLOC_FREE(info_shallow_copy);
1605
0
    return KRB5KDC_ERR_TGT_REVOKED;
1606
0
  }
1607
1608
0
  *info_out = info_shallow_copy;
1609
1610
0
  return 0;
1611
0
}
1612
1613
static NTSTATUS samba_kdc_update_delegation_info_blob(TALLOC_CTX *mem_ctx,
1614
                  krb5_context context,
1615
                  const krb5_const_pac pac,
1616
                  const krb5_const_principal server_principal,
1617
                  const krb5_const_principal proxy_principal,
1618
                  DATA_BLOB *new_blob)
1619
0
{
1620
0
  krb5_data old_data = {};
1621
0
  DATA_BLOB old_blob;
1622
0
  krb5_error_code ret;
1623
0
  NTSTATUS nt_status = NT_STATUS_OK;
1624
0
  enum ndr_err_code ndr_err;
1625
0
  union PAC_INFO info = {};
1626
0
  struct PAC_CONSTRAINED_DELEGATION _d = {};
1627
0
  struct PAC_CONSTRAINED_DELEGATION *d = NULL;
1628
0
  char *server = NULL;
1629
0
  char *proxy = NULL;
1630
0
  uint32_t i;
1631
0
  TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1632
1633
0
  if (tmp_ctx == NULL) {
1634
0
    nt_status = NT_STATUS_NO_MEMORY;
1635
0
    goto out;
1636
0
  }
1637
1638
0
  ret = krb5_pac_get_buffer(context, pac, PAC_TYPE_CONSTRAINED_DELEGATION, &old_data);
1639
0
  if (ret == ENOENT) {
1640
    /* OK. */
1641
0
  } else if (ret) {
1642
0
    nt_status = NT_STATUS_UNSUCCESSFUL;
1643
0
    goto out;
1644
0
  }
1645
1646
0
  old_blob.length = old_data.length;
1647
0
  old_blob.data = (uint8_t *)old_data.data;
1648
1649
0
  if (old_blob.length > 0) {
1650
0
    ndr_err = ndr_pull_union_blob(&old_blob, tmp_ctx,
1651
0
        &info, PAC_TYPE_CONSTRAINED_DELEGATION,
1652
0
        (ndr_pull_flags_fn_t)ndr_pull_PAC_INFO);
1653
0
    if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1654
0
      smb_krb5_free_data_contents(context, &old_data);
1655
0
      nt_status = ndr_map_error2ntstatus(ndr_err);
1656
0
      DBG_ERR("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status));
1657
0
      goto out;
1658
0
    }
1659
0
  } else {
1660
0
    info.constrained_delegation.info = &_d;
1661
0
  }
1662
0
  smb_krb5_free_data_contents(context, &old_data);
1663
1664
0
  ret = krb5_unparse_name_flags(context, server_principal,
1665
0
              KRB5_PRINCIPAL_UNPARSE_NO_REALM, &server);
1666
0
  if (ret) {
1667
0
    nt_status = NT_STATUS_INTERNAL_ERROR;
1668
0
    goto out;
1669
0
  }
1670
1671
0
  ret = krb5_unparse_name(context, proxy_principal, &proxy);
1672
0
  if (ret) {
1673
0
    SAFE_FREE(server);
1674
0
    nt_status = NT_STATUS_INTERNAL_ERROR;
1675
0
    goto out;
1676
0
  }
1677
1678
0
  d = info.constrained_delegation.info;
1679
0
  i = d->num_transited_services;
1680
0
  d->proxy_target.string = server;
1681
0
  d->transited_services = talloc_realloc(mem_ctx, d->transited_services,
1682
0
                 struct lsa_String, i + 1);
1683
0
  if (d->transited_services == NULL) {
1684
0
    SAFE_FREE(server);
1685
0
    SAFE_FREE(proxy);
1686
0
    nt_status = NT_STATUS_INTERNAL_ERROR;
1687
0
    goto out;
1688
0
  }
1689
0
  d->transited_services[i].string = proxy;
1690
0
  d->num_transited_services = i + 1;
1691
1692
0
  ndr_err = ndr_push_union_blob(new_blob, mem_ctx,
1693
0
        &info, PAC_TYPE_CONSTRAINED_DELEGATION,
1694
0
        (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
1695
0
  SAFE_FREE(server);
1696
0
  SAFE_FREE(proxy);
1697
0
  if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1698
0
    smb_krb5_free_data_contents(context, &old_data);
1699
0
    nt_status = ndr_map_error2ntstatus(ndr_err);
1700
0
    DBG_ERR("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status));
1701
0
    goto out;
1702
0
  }
1703
1704
0
out:
1705
0
  talloc_free(tmp_ctx);
1706
0
  return nt_status;
1707
0
}
1708
1709
/* function to map policy errors */
1710
krb5_error_code samba_kdc_map_policy_err(NTSTATUS nt_status)
1711
0
{
1712
0
  krb5_error_code ret;
1713
1714
0
  if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE))
1715
0
    ret = KRB5KDC_ERR_KEY_EXP;
1716
0
  else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_EXPIRED))
1717
0
    ret = KRB5KDC_ERR_KEY_EXP;
1718
0
  else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_EXPIRED))
1719
0
    ret = KRB5KDC_ERR_CLIENT_REVOKED;
1720
0
  else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED))
1721
0
    ret = KRB5KDC_ERR_CLIENT_REVOKED;
1722
0
  else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_LOGON_HOURS))
1723
0
    ret = KRB5KDC_ERR_CLIENT_REVOKED;
1724
0
  else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT))
1725
0
    ret = KRB5KDC_ERR_CLIENT_REVOKED;
1726
0
  else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_WORKSTATION))
1727
0
    ret = KRB5KDC_ERR_POLICY;
1728
0
  else
1729
0
    ret = KRB5KDC_ERR_POLICY;
1730
1731
0
  return ret;
1732
0
}
1733
1734
/* Given a kdc entry, consult the account_ok routine in auth/auth_sam.c
1735
 * for consistency */
1736
NTSTATUS samba_kdc_check_client_access(struct samba_kdc_entry *kdc_entry,
1737
               const char *client_name,
1738
               const char *workstation,
1739
               bool password_change)
1740
0
{
1741
0
  TALLOC_CTX *tmp_ctx;
1742
0
  NTSTATUS nt_status;
1743
1744
0
  tmp_ctx = talloc_named(NULL, 0, "samba_kdc_check_client_access");
1745
0
  if (!tmp_ctx) {
1746
0
    return NT_STATUS_NO_MEMORY;
1747
0
  }
1748
1749
  /* we allow all kinds of trusts here */
1750
0
  nt_status = authsam_account_ok(tmp_ctx,
1751
0
               kdc_entry->kdc_db_ctx->samdb,
1752
0
               kdc_entry->current_nttime,
1753
0
               MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
1754
0
               MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
1755
0
               kdc_entry->realm_dn, kdc_entry->msg,
1756
0
               workstation, client_name,
1757
0
               true, password_change);
1758
1759
0
  kdc_entry->reject_status = nt_status;
1760
0
  talloc_free(tmp_ctx);
1761
0
  return nt_status;
1762
0
}
1763
1764
static krb5_error_code samba_get_requester_sid(TALLOC_CTX *mem_ctx,
1765
                 krb5_const_pac pac,
1766
                 krb5_context context,
1767
                 struct dom_sid *sid)
1768
0
{
1769
0
  NTSTATUS nt_status;
1770
0
  enum ndr_err_code ndr_err;
1771
0
  krb5_error_code ret = 0;
1772
1773
0
  DATA_BLOB pac_requester_sid_in;
1774
0
  krb5_data k5pac_requester_sid_in;
1775
1776
0
  union PAC_INFO info;
1777
1778
0
  TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1779
0
  if (tmp_ctx == NULL) {
1780
0
    ret = ENOMEM;
1781
0
    goto out;
1782
0
  }
1783
1784
0
  ret = krb5_pac_get_buffer(context, pac, PAC_TYPE_REQUESTER_SID,
1785
0
          &k5pac_requester_sid_in);
1786
0
  if (ret != 0) {
1787
0
    goto out;
1788
0
  }
1789
1790
0
  pac_requester_sid_in = data_blob_const(k5pac_requester_sid_in.data,
1791
0
                 k5pac_requester_sid_in.length);
1792
1793
0
  ndr_err = ndr_pull_union_blob(&pac_requester_sid_in, tmp_ctx, &info,
1794
0
              PAC_TYPE_REQUESTER_SID,
1795
0
              (ndr_pull_flags_fn_t)ndr_pull_PAC_INFO);
1796
0
  smb_krb5_free_data_contents(context, &k5pac_requester_sid_in);
1797
0
  if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1798
0
    nt_status = ndr_map_error2ntstatus(ndr_err);
1799
0
    DBG_ERR("can't parse the PAC REQUESTER_SID: %s\n", nt_errstr(nt_status));
1800
0
    ret = map_errno_from_nt_status(nt_status);
1801
0
    goto out;
1802
0
  }
1803
1804
0
  *sid = info.requester_sid.sid;
1805
1806
0
out:
1807
0
  talloc_free(tmp_ctx);
1808
0
  return ret;
1809
0
}
1810
1811
/* Does a parse and SID check, but no crypto. */
1812
static krb5_error_code samba_kdc_validate_pac_blob(
1813
    krb5_context context,
1814
    const struct samba_kdc_entry_pac client)
1815
0
{
1816
0
  TALLOC_CTX *frame = talloc_stackframe();
1817
0
  struct auth_user_info_dc *pac_user_info = NULL;
1818
0
  struct dom_sid client_sid;
1819
0
  struct dom_sid pac_sid;
1820
0
  krb5_error_code code;
1821
0
  bool ok;
1822
1823
  /*
1824
   * First, try to get the SID from the requester SID buffer in the PAC.
1825
   */
1826
0
  code = samba_get_requester_sid(frame, client.pac, context, &pac_sid);
1827
1828
0
  if (code == ENOENT) {
1829
    /*
1830
     * If the requester SID buffer isn't present, fall back to the
1831
     * SID in the LOGON_INFO PAC buffer.
1832
     */
1833
0
    code = kerberos_pac_to_user_info_dc(frame,
1834
0
                client.pac,
1835
0
                context,
1836
0
                &pac_user_info,
1837
0
                AUTH_EXCLUDE_RESOURCE_GROUPS,
1838
0
                NULL,
1839
0
                NULL,
1840
0
                NULL);
1841
0
    if (code != 0) {
1842
0
      goto out;
1843
0
    }
1844
1845
0
    if (pac_user_info->num_sids == 0) {
1846
0
      code = EINVAL;
1847
0
      goto out;
1848
0
    }
1849
1850
0
    pac_sid = pac_user_info->sids[PRIMARY_USER_SID_INDEX].sid;
1851
0
  } else if (code != 0) {
1852
0
    goto out;
1853
0
  }
1854
1855
0
  code = samdb_result_dom_sid_buf(client.entry->msg,
1856
0
          "objectSid",
1857
0
          &client_sid);
1858
0
  if (code) {
1859
0
    goto out;
1860
0
  }
1861
1862
0
  ok = dom_sid_equal(&pac_sid, &client_sid);
1863
0
  if (!ok) {
1864
0
    struct dom_sid_buf buf1;
1865
0
    struct dom_sid_buf buf2;
1866
1867
0
    DBG_ERR("SID mismatch between PAC and looked up client: "
1868
0
      "PAC[%s] != CLI[%s]\n",
1869
0
      dom_sid_str_buf(&pac_sid, &buf1),
1870
0
      dom_sid_str_buf(&client_sid, &buf2));
1871
0
      code = KRB5KDC_ERR_TGT_REVOKED;
1872
0
    goto out;
1873
0
  }
1874
1875
0
  code = 0;
1876
0
out:
1877
0
  TALLOC_FREE(frame);
1878
0
  return code;
1879
0
}
1880
1881
1882
/*
1883
 * In the RODC case, to confirm that the returned user is permitted to
1884
 * be replicated to the KDC (krbgtgt_xxx user) represented by *rodc
1885
 */
1886
static WERROR samba_rodc_confirm_user_is_allowed(uint32_t num_object_sids,
1887
             const struct dom_sid *object_sids,
1888
             const struct samba_kdc_entry *rodc,
1889
             const struct samba_kdc_entry *object)
1890
0
{
1891
0
  int ret;
1892
0
  WERROR werr;
1893
0
  TALLOC_CTX *frame = talloc_stackframe();
1894
0
  const char *rodc_attrs[] = { "msDS-KrbTgtLink",
1895
0
             "msDS-NeverRevealGroup",
1896
0
             "msDS-RevealOnDemandGroup",
1897
0
             "userAccountControl",
1898
0
             "objectSid",
1899
0
             NULL };
1900
0
  struct ldb_result *rodc_machine_account = NULL;
1901
0
  struct ldb_dn *rodc_machine_account_dn = samdb_result_dn(rodc->kdc_db_ctx->samdb,
1902
0
             frame,
1903
0
             rodc->msg,
1904
0
             "msDS-KrbTgtLinkBL",
1905
0
             NULL);
1906
0
  const struct dom_sid *rodc_machine_account_sid = NULL;
1907
1908
0
  if (rodc_machine_account_dn == NULL) {
1909
0
    DBG_ERR("krbtgt account %s has no msDS-KrbTgtLinkBL to find RODC machine account for allow/deny list\n",
1910
0
      ldb_dn_get_linearized(rodc->msg->dn));
1911
0
    TALLOC_FREE(frame);
1912
0
    return WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1913
0
  }
1914
1915
  /*
1916
   * Follow the link and get the RODC account (the krbtgt
1917
   * account is the krbtgt_XXX account, but the
1918
   * msDS-NeverRevealGroup and msDS-RevealOnDemandGroup is on
1919
   * the RODC$ account)
1920
   *
1921
   * We need DSDB_SEARCH_SHOW_EXTENDED_DN as we get a SID lists
1922
   * out of the extended DNs
1923
   */
1924
1925
0
  ret = dsdb_search_dn(rodc->kdc_db_ctx->samdb,
1926
0
           frame,
1927
0
           &rodc_machine_account,
1928
0
           rodc_machine_account_dn,
1929
0
           rodc_attrs,
1930
0
           DSDB_SEARCH_SHOW_EXTENDED_DN);
1931
0
  if (ret != LDB_SUCCESS) {
1932
0
    DBG_ERR("Failed to fetch RODC machine account %s pointed to by %s to check allow/deny list: %s\n",
1933
0
      ldb_dn_get_linearized(rodc_machine_account_dn),
1934
0
      ldb_dn_get_linearized(rodc->msg->dn),
1935
0
      ldb_errstring(rodc->kdc_db_ctx->samdb));
1936
0
    TALLOC_FREE(frame);
1937
0
    return WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1938
0
  }
1939
1940
0
  if (rodc_machine_account->count != 1) {
1941
0
    DBG_ERR("Failed to fetch RODC machine account %s pointed to by %s to check allow/deny list: (%d)\n",
1942
0
      ldb_dn_get_linearized(rodc_machine_account_dn),
1943
0
      ldb_dn_get_linearized(rodc->msg->dn),
1944
0
      rodc_machine_account->count);
1945
0
    TALLOC_FREE(frame);
1946
0
    return WERR_DS_DRA_BAD_DN;
1947
0
  }
1948
1949
  /* if the object SID is equal to the user_sid, allow */
1950
0
  rodc_machine_account_sid = samdb_result_dom_sid(frame,
1951
0
            rodc_machine_account->msgs[0],
1952
0
            "objectSid");
1953
0
  if (rodc_machine_account_sid == NULL) {
1954
0
    TALLOC_FREE(frame);
1955
0
    return WERR_DS_DRA_BAD_DN;
1956
0
  }
1957
1958
0
  werr = samdb_confirm_rodc_allowed_to_repl_to_sid_list(rodc->kdc_db_ctx->samdb,
1959
0
                    rodc_machine_account_sid,
1960
0
                    rodc_machine_account->msgs[0],
1961
0
                    object->msg,
1962
0
                    num_object_sids,
1963
0
                    object_sids);
1964
1965
0
  TALLOC_FREE(frame);
1966
0
  return werr;
1967
0
}
1968
1969
/*
1970
 * Perform an access check for the client attempting to authenticate to the
1971
 * server. ‘client_info’ must be talloc-allocated so that we can make a
1972
 * reference to it.
1973
 */
1974
static
1975
krb5_error_code samba_kdc_allowed_to_authenticate_to(TALLOC_CTX *mem_ctx,
1976
                 struct samba_kdc_db_context *kdc_db_ctx,
1977
                 const struct samba_kdc_entry *client,
1978
                 const struct auth_user_info_dc *client_info,
1979
                 const struct auth_user_info_dc *device_info,
1980
                 const struct auth_claims auth_claims,
1981
                 const struct samba_kdc_entry *server,
1982
                 struct authn_audit_info **server_audit_info_out,
1983
                 NTSTATUS *status_out)
1984
0
{
1985
0
  struct ldb_context *samdb = kdc_db_ctx->samdb;
1986
0
  struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
1987
0
  krb5_error_code ret = 0;
1988
0
  NTSTATUS status;
1989
0
  _UNUSED_ NTSTATUS _status;
1990
0
  struct dom_sid server_sid = {};
1991
0
  const struct authn_server_policy *server_policy = server->server_policy;
1992
1993
0
  if (status_out != NULL) {
1994
0
    *status_out = NT_STATUS_OK;
1995
0
  }
1996
1997
0
  ret = samdb_result_dom_sid_buf(server->msg, "objectSid", &server_sid);
1998
0
  if (ret) {
1999
    /*
2000
     * Ignore the return status — we are already in an error path,
2001
     * and overwriting the real error code with the audit info
2002
     * status is unhelpful.
2003
     */
2004
0
    _status = authn_server_policy_audit_info(mem_ctx,
2005
0
               server_policy,
2006
0
               client_info,
2007
0
               AUTHN_AUDIT_EVENT_OTHER_ERROR,
2008
0
               AUTHN_AUDIT_REASON_NONE,
2009
0
               dsdb_ldb_err_to_ntstatus(ret),
2010
0
               server_audit_info_out);
2011
0
    goto out;
2012
0
  }
2013
2014
0
  if (dom_sid_equal(&client_info->sids[PRIMARY_USER_SID_INDEX].sid, &server_sid)) {
2015
    /* Authenticating to ourselves is always allowed. */
2016
0
    status = authn_server_policy_audit_info(mem_ctx,
2017
0
              server_policy,
2018
0
              client_info,
2019
0
              AUTHN_AUDIT_EVENT_OK,
2020
0
              AUTHN_AUDIT_REASON_NONE,
2021
0
              NT_STATUS_OK,
2022
0
              server_audit_info_out);
2023
0
    if (!NT_STATUS_IS_OK(status)) {
2024
0
      ret = KRB5KRB_ERR_GENERIC;
2025
0
    }
2026
0
    goto out;
2027
0
  }
2028
2029
0
  status = authn_policy_authenticate_to_service(mem_ctx,
2030
0
                  samdb,
2031
0
                  lp_ctx,
2032
0
                  AUTHN_POLICY_AUTH_TYPE_KERBEROS,
2033
0
                  client_info,
2034
0
                  device_info,
2035
0
                  auth_claims,
2036
0
                  server_policy,
2037
0
                  (struct authn_policy_flags) { .force_compounded_authentication = true },
2038
0
                  server_audit_info_out);
2039
0
  if (!NT_STATUS_IS_OK(status)) {
2040
0
    if (status_out != NULL) {
2041
0
      *status_out = status;
2042
0
    }
2043
0
    if (NT_STATUS_EQUAL(status, NT_STATUS_AUTHENTICATION_FIREWALL_FAILED)) {
2044
0
      ret = KRB5KDC_ERR_POLICY;
2045
0
    } else if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
2046
0
      ret = KRB5KDC_ERR_POLICY;
2047
0
    } else {
2048
0
      ret = KRB5KRB_ERR_GENERIC;
2049
0
    }
2050
0
  }
2051
2052
0
out:
2053
0
  return ret;
2054
0
}
2055
2056
static krb5_error_code samba_kdc_add_domain_group_sid(struct PAC_DEVICE_INFO *info,
2057
                  const struct netr_SidAttr *sid)
2058
0
{
2059
0
  uint32_t i;
2060
0
  uint32_t rid;
2061
0
  NTSTATUS status;
2062
2063
0
  uint32_t domain_group_count = info->domain_group_count;
2064
0
  struct PAC_DOMAIN_GROUP_MEMBERSHIP *domain_group = NULL;
2065
0
  struct samr_RidWithAttribute *rids = NULL;
2066
2067
0
  for (i = 0; i < domain_group_count; ++i) {
2068
0
    struct PAC_DOMAIN_GROUP_MEMBERSHIP *this_domain_group
2069
0
      = &info->domain_groups[i];
2070
2071
0
    if (dom_sid_in_domain(this_domain_group->domain_sid, sid->sid)) {
2072
0
      domain_group = this_domain_group;
2073
0
      break;
2074
0
    }
2075
0
  }
2076
2077
0
  if (domain_group == NULL) {
2078
0
    struct PAC_DOMAIN_GROUP_MEMBERSHIP *domain_groups = NULL;
2079
2080
0
    if (domain_group_count == UINT32_MAX) {
2081
0
      return EINVAL;
2082
0
    }
2083
2084
0
    domain_groups = talloc_realloc(
2085
0
      info,
2086
0
      info->domain_groups,
2087
0
      struct PAC_DOMAIN_GROUP_MEMBERSHIP,
2088
0
      domain_group_count + 1);
2089
0
    if (domain_groups == NULL) {
2090
0
      return ENOMEM;
2091
0
    }
2092
2093
0
    info->domain_groups = domain_groups;
2094
2095
0
    domain_group = &info->domain_groups[domain_group_count++];
2096
0
    *domain_group = (struct PAC_DOMAIN_GROUP_MEMBERSHIP) {};
2097
2098
0
    status = dom_sid_split_rid(info->domain_groups,
2099
0
             sid->sid,
2100
0
             &domain_group->domain_sid,
2101
0
             &rid);
2102
0
    if (!NT_STATUS_IS_OK(status)) {
2103
0
      return map_errno_from_nt_status(status);
2104
0
    }
2105
0
  } else {
2106
0
    status = dom_sid_split_rid(NULL,
2107
0
             sid->sid,
2108
0
             NULL,
2109
0
             &rid);
2110
0
    if (!NT_STATUS_IS_OK(status)) {
2111
0
      return map_errno_from_nt_status(status);
2112
0
    }
2113
0
  }
2114
2115
0
  if (domain_group->groups.count == UINT32_MAX) {
2116
0
    return EINVAL;
2117
0
  }
2118
2119
0
  rids = talloc_realloc(info->domain_groups,
2120
0
            domain_group->groups.rids,
2121
0
            struct samr_RidWithAttribute,
2122
0
            domain_group->groups.count + 1);
2123
0
  if (rids == NULL) {
2124
0
    return ENOMEM;
2125
0
  }
2126
2127
0
  domain_group->groups.rids = rids;
2128
2129
0
  domain_group->groups.rids[domain_group->groups.count] = (struct samr_RidWithAttribute) {
2130
0
    .rid = rid,
2131
0
    .attributes = sid->attributes,
2132
0
  };
2133
2134
0
  ++domain_group->groups.count;
2135
2136
0
  info->domain_group_count = domain_group_count;
2137
2138
0
  return 0;
2139
0
}
2140
2141
static krb5_error_code samba_kdc_make_device_info(TALLOC_CTX *mem_ctx,
2142
              const struct netr_SamInfo3 *info3,
2143
              struct PAC_DOMAIN_GROUP_MEMBERSHIP *resource_groups,
2144
              union PAC_INFO *info)
2145
0
{
2146
0
  TALLOC_CTX *tmp_ctx = NULL;
2147
0
  struct PAC_DEVICE_INFO *device_info = NULL;
2148
0
  uint32_t i;
2149
0
  krb5_error_code ret = 0;
2150
2151
0
  *info = (union PAC_INFO) {};
2152
2153
0
  info->device_info.info = NULL;
2154
2155
0
  tmp_ctx = talloc_new(mem_ctx);
2156
0
  if (tmp_ctx == NULL) {
2157
0
    return ENOMEM;
2158
0
  }
2159
2160
0
  device_info = talloc(tmp_ctx, struct PAC_DEVICE_INFO);
2161
0
  if (device_info == NULL) {
2162
0
    ret = ENOMEM;
2163
0
    goto out;
2164
0
  }
2165
2166
0
  device_info->rid = info3->base.rid;
2167
0
  device_info->primary_gid = info3->base.primary_gid;
2168
0
  device_info->domain_sid = info3->base.domain_sid;
2169
0
  device_info->groups = info3->base.groups;
2170
2171
0
  device_info->sid_count = 0;
2172
0
  device_info->sids = NULL;
2173
2174
0
  if (resource_groups != NULL) {
2175
    /*
2176
     * The account's resource groups all belong to the same domain,
2177
     * so we can add them all in one go.
2178
     */
2179
0
    device_info->domain_group_count = 1;
2180
0
    device_info->domain_groups = talloc_move(device_info, &resource_groups);
2181
0
  } else {
2182
0
    device_info->domain_group_count = 0;
2183
0
    device_info->domain_groups = NULL;
2184
0
  }
2185
2186
0
  for (i = 0; i < info3->sidcount; ++i) {
2187
0
    const struct netr_SidAttr *device_sid = &info3->sids[i];
2188
2189
0
    if (dom_sid_has_account_domain(device_sid->sid)) {
2190
0
      ret = samba_kdc_add_domain_group_sid(device_info, device_sid);
2191
0
      if (ret != 0) {
2192
0
        goto out;
2193
0
      }
2194
0
    } else {
2195
0
      device_info->sids = talloc_realloc(device_info, device_info->sids,
2196
0
                 struct netr_SidAttr,
2197
0
                 device_info->sid_count + 1);
2198
0
      if (device_info->sids == NULL) {
2199
0
        ret = ENOMEM;
2200
0
        goto out;
2201
0
      }
2202
2203
0
      device_info->sids[device_info->sid_count].sid = dom_sid_dup(device_info->sids, device_sid->sid);
2204
0
      if (device_info->sids[device_info->sid_count].sid == NULL) {
2205
0
        ret = ENOMEM;
2206
0
        goto out;
2207
0
      }
2208
2209
0
      device_info->sids[device_info->sid_count].attributes = device_sid->attributes;
2210
2211
0
      ++device_info->sid_count;
2212
0
    }
2213
0
  }
2214
2215
0
  info->device_info.info = talloc_steal(mem_ctx, device_info);
2216
2217
0
out:
2218
0
  talloc_free(tmp_ctx);
2219
0
  return ret;
2220
0
}
2221
2222
static krb5_error_code samba_kdc_get_device_info_pac_blob(TALLOC_CTX *mem_ctx,
2223
                union PAC_INFO *info,
2224
                DATA_BLOB **_device_info_blob)
2225
0
{
2226
0
  DATA_BLOB *device_info_blob = NULL;
2227
0
  enum ndr_err_code ndr_err;
2228
2229
0
  *_device_info_blob = NULL;
2230
2231
0
  device_info_blob = talloc_zero(mem_ctx, DATA_BLOB);
2232
0
  if (device_info_blob == NULL) {
2233
0
    DBG_ERR("Out of memory\n");
2234
0
    return ENOMEM;
2235
0
  }
2236
2237
0
  ndr_err = ndr_push_union_blob(device_info_blob, device_info_blob,
2238
0
              info, PAC_TYPE_DEVICE_INFO,
2239
0
              (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
2240
0
  if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2241
0
    NTSTATUS nt_status = ndr_map_error2ntstatus(ndr_err);
2242
0
    DBG_WARNING("PAC_DEVICE_INFO (presig) push failed: %s\n",
2243
0
          nt_errstr(nt_status));
2244
0
    talloc_free(device_info_blob);
2245
0
    return map_errno_from_nt_status(nt_status);
2246
0
  }
2247
2248
0
  *_device_info_blob = device_info_blob;
2249
2250
0
  return 0;
2251
0
}
2252
2253
static krb5_error_code samba_kdc_get_device_info_blob(TALLOC_CTX *mem_ctx,
2254
                  krb5_context context,
2255
                  struct samba_kdc_db_context *kdc_db_ctx,
2256
                  const struct auth_user_info_dc *device_info,
2257
                  DATA_BLOB **device_info_blob)
2258
0
{
2259
0
  TALLOC_CTX *frame = NULL;
2260
0
  krb5_error_code code = EINVAL;
2261
0
  NTSTATUS nt_status;
2262
0
  struct netr_SamInfo3 *info3 = NULL;
2263
0
  struct PAC_DOMAIN_GROUP_MEMBERSHIP *resource_groups = NULL;
2264
2265
0
  union PAC_INFO info;
2266
2267
0
  frame = talloc_stackframe();
2268
2269
0
  nt_status = auth_convert_user_info_dc_saminfo3(frame, device_info,
2270
0
                   AUTH_INCLUDE_RESOURCE_GROUPS_COMPRESSED,
2271
0
                   &info3,
2272
0
                   &resource_groups);
2273
0
  if (!NT_STATUS_IS_OK(nt_status)) {
2274
0
    DBG_WARNING("Getting Samba info failed: %s\n",
2275
0
          nt_errstr(nt_status));
2276
0
    talloc_free(frame);
2277
0
    return nt_status_to_krb5(nt_status);
2278
0
  }
2279
2280
0
  code = samba_kdc_make_device_info(frame,
2281
0
            info3,
2282
0
            resource_groups,
2283
0
            &info);
2284
0
  if (code != 0) {
2285
0
    talloc_free(frame);
2286
0
    return code;
2287
0
  }
2288
2289
0
  code = samba_kdc_get_device_info_pac_blob(mem_ctx,
2290
0
              &info,
2291
0
              device_info_blob);
2292
2293
0
  talloc_free(frame);
2294
0
  return code;
2295
0
}
2296
2297
/**
2298
 * @brief Verify a PAC
2299
 *
2300
 * @param mem_ctx   A talloc memory context
2301
 *
2302
 * @param context   A krb5 context
2303
 *
2304
 * @param samdb     An open samdb connection.
2305
 *
2306
 * @param flags     Bitwise OR'ed flags
2307
 *
2308
 * @param client    The client samba kdc PAC entry.
2309
2310
 * @param krbtgt    The krbtgt samba kdc entry.
2311
 *
2312
 * @return A Kerberos error code.
2313
 */
2314
krb5_error_code samba_kdc_verify_pac(TALLOC_CTX *mem_ctx,
2315
             krb5_context context,
2316
             struct samba_kdc_db_context *kdc_db_ctx,
2317
             uint32_t flags,
2318
             const struct samba_kdc_entry_pac client,
2319
             const struct samba_kdc_entry *krbtgt)
2320
0
{
2321
0
  TALLOC_CTX *tmp_ctx = NULL;
2322
0
  struct pac_blobs *pac_blobs = NULL;
2323
0
  krb5_error_code code = EINVAL;
2324
2325
0
  tmp_ctx = talloc_new(mem_ctx);
2326
0
  if (tmp_ctx == NULL) {
2327
0
    code = ENOMEM;
2328
0
    goto done;
2329
0
  }
2330
2331
0
  if (client.entry != NULL) {
2332
    /*
2333
     * Check the objectSID of the client and pac data are the same.
2334
     * Does a parse and SID check, but no crypto.
2335
     */
2336
0
    code = samba_kdc_validate_pac_blob(context, client);
2337
0
    if (code != 0) {
2338
0
      goto done;
2339
0
    }
2340
0
  }
2341
2342
0
  if (!samba_krb5_pac_is_trusted(client)) {
2343
0
    const struct auth_user_info_dc *user_info_dc = NULL;
2344
0
    WERROR werr;
2345
2346
0
    struct dom_sid *object_sids = NULL;
2347
0
    uint32_t j;
2348
2349
0
    if (client.entry == NULL) {
2350
0
      code = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
2351
0
      goto done;
2352
0
    }
2353
2354
0
    code = samba_kdc_get_user_info_from_db(tmp_ctx,
2355
0
                   kdc_db_ctx,
2356
0
                   client.entry,
2357
0
                   client.entry->msg,
2358
0
                   &user_info_dc);
2359
0
    if (code) {
2360
0
      const char *krb5_err = krb5_get_error_message(context, code);
2361
0
      DBG_ERR("Getting user info for PAC failed: %s\n",
2362
0
        krb5_err != NULL ? krb5_err : "<unknown>");
2363
0
      krb5_free_error_message(context, krb5_err);
2364
2365
0
      code = KRB5KDC_ERR_TGT_REVOKED;
2366
0
      goto done;
2367
0
    }
2368
2369
    /*
2370
     * Check if the SID list in the user_info_dc intersects
2371
     * correctly with the RODC allow/deny lists.
2372
     */
2373
0
    object_sids = talloc_array(tmp_ctx, struct dom_sid, user_info_dc->num_sids);
2374
0
    if (object_sids == NULL) {
2375
0
      code = ENOMEM;
2376
0
      goto done;
2377
0
    }
2378
2379
0
    for (j = 0; j < user_info_dc->num_sids; ++j) {
2380
0
      object_sids[j] = user_info_dc->sids[j].sid;
2381
0
    }
2382
2383
0
    werr = samba_rodc_confirm_user_is_allowed(user_info_dc->num_sids,
2384
0
                object_sids,
2385
0
                krbtgt,
2386
0
                client.entry);
2387
0
    if (!W_ERROR_IS_OK(werr)) {
2388
0
      code = KRB5KDC_ERR_TGT_REVOKED;
2389
0
      if (W_ERROR_EQUAL(werr,
2390
0
            WERR_DOMAIN_CONTROLLER_NOT_FOUND)) {
2391
0
        code = KRB5KDC_ERR_POLICY;
2392
0
      }
2393
0
      goto done;
2394
0
    }
2395
2396
    /*
2397
     * The RODC PAC data isn't trusted for authorization as it may
2398
     * be stale. The only thing meaningful we can do with an RODC
2399
     * account on a full DC is exchange the RODC TGT for a 'real'
2400
     * TGT.
2401
     *
2402
     * So we match Windows (at least server 2022) and
2403
     * don't allow S4U2Self.
2404
     *
2405
     * https://lists.samba.org/archive/cifs-protocol/2022-April/003673.html
2406
     */
2407
0
    if (flags & SAMBA_KDC_FLAG_PROTOCOL_TRANSITION) {
2408
0
      code = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
2409
0
      goto done;
2410
0
    }
2411
0
  }
2412
2413
  /* Check the types of the given PAC */
2414
2415
0
  code = pac_blobs_from_krb5_pac(tmp_ctx,
2416
0
               context,
2417
0
               client.pac,
2418
0
               &pac_blobs);
2419
0
  if (code != 0) {
2420
0
    goto done;
2421
0
  }
2422
2423
0
  code = pac_blobs_ensure_exists(pac_blobs,
2424
0
               PAC_TYPE_LOGON_INFO);
2425
0
  if (code != 0) {
2426
0
    goto done;
2427
0
  }
2428
2429
0
  code = pac_blobs_ensure_exists(pac_blobs,
2430
0
               PAC_TYPE_LOGON_NAME);
2431
0
  if (code != 0) {
2432
0
    goto done;
2433
0
  }
2434
2435
0
  code = pac_blobs_ensure_exists(pac_blobs,
2436
0
               PAC_TYPE_SRV_CHECKSUM);
2437
0
  if (code != 0) {
2438
0
    goto done;
2439
0
  }
2440
2441
0
  code = pac_blobs_ensure_exists(pac_blobs,
2442
0
               PAC_TYPE_KDC_CHECKSUM);
2443
0
  if (code != 0) {
2444
0
    goto done;
2445
0
  }
2446
2447
0
  if (!(flags & SAMBA_KDC_FLAG_CONSTRAINED_DELEGATION)) {
2448
0
    code = pac_blobs_ensure_exists(pac_blobs,
2449
0
                 PAC_TYPE_REQUESTER_SID);
2450
0
    if (code != 0) {
2451
0
      code = KRB5KDC_ERR_TGT_REVOKED;
2452
0
      goto done;
2453
0
    }
2454
0
  }
2455
2456
0
  code = 0;
2457
2458
0
done:
2459
0
  talloc_free(tmp_ctx);
2460
2461
0
  return code;
2462
0
}
2463
2464
static
2465
krb5_error_code samba_kdc_get_claims_data(TALLOC_CTX *mem_ctx,
2466
            krb5_context context,
2467
            struct samba_kdc_db_context *kdc_db_ctx,
2468
            struct samba_kdc_entry_pac entry,
2469
            struct claims_data **claims_data_out,
2470
            bool *_need_regeneration);
2471
2472
krb5_error_code samba_kdc_get_pac(TALLOC_CTX *mem_ctx,
2473
          krb5_context context,
2474
          struct samba_kdc_db_context *kdc_db_ctx,
2475
          uint32_t flags,
2476
          struct samba_kdc_entry *client,
2477
          const krb5_const_principal server_principal,
2478
          const struct samba_kdc_entry *server,
2479
          const struct samba_kdc_entry_pac device,
2480
          const krb5_keyblock *pk_reply_key,
2481
          uint64_t pac_attributes,
2482
          krb5_pac new_pac,
2483
          struct authn_audit_info **server_audit_info_out,
2484
          NTSTATUS *status_out)
2485
0
{
2486
0
  TALLOC_CTX *frame = talloc_stackframe();
2487
0
  DATA_BLOB *logon_blob = NULL;
2488
0
  DATA_BLOB *cred_ndr = NULL;
2489
0
  DATA_BLOB **cred_ndr_ptr = NULL;
2490
0
  DATA_BLOB _cred_blob = data_blob_null;
2491
0
  DATA_BLOB *cred_blob = NULL;
2492
0
  DATA_BLOB *upn_blob = NULL;
2493
0
  DATA_BLOB *pac_attrs_blob = NULL;
2494
0
  DATA_BLOB *requester_sid_blob = NULL;
2495
0
  const DATA_BLOB *client_claims_blob = NULL;
2496
0
  krb5_error_code ret;
2497
0
  NTSTATUS nt_status;
2498
0
  bool is_krbtgt = false;
2499
0
  enum auth_group_inclusion group_inclusion;
2500
0
  bool is_s4u2self = flags & SAMBA_KDC_FLAG_PROTOCOL_TRANSITION;
2501
0
  enum samba_asserted_identity asserted_identity =
2502
0
    (is_s4u2self) ?
2503
0
      SAMBA_ASSERTED_IDENTITY_SERVICE :
2504
0
      SAMBA_ASSERTED_IDENTITY_AUTHENTICATION_AUTHORITY;
2505
0
  bool pkinit_freshness = flags & SAMBA_KDC_FLAG_PKINIT_FRESHNESS_USED;
2506
0
  const struct auth_user_info_dc *user_info_dc_const = NULL;
2507
0
  struct auth_user_info_dc *user_info_dc = NULL;
2508
0
  struct auth_claims auth_claims = {};
2509
2510
0
  if (server_audit_info_out != NULL) {
2511
0
    *server_audit_info_out = NULL;
2512
0
  }
2513
2514
0
  if (status_out != NULL) {
2515
0
    *status_out = NT_STATUS_OK;
2516
0
  }
2517
2518
0
  {
2519
0
    int result = smb_krb5_principal_is_tgs(context, server_principal);
2520
0
    if (result == -1) {
2521
0
      TALLOC_FREE(frame);
2522
0
      return ENOMEM;
2523
0
    }
2524
2525
0
    is_krbtgt = result;
2526
0
  }
2527
2528
  /* Only include resource groups in a service ticket. */
2529
0
  if (is_krbtgt) {
2530
0
    group_inclusion = AUTH_EXCLUDE_RESOURCE_GROUPS;
2531
0
  } else if (server->supported_enctypes & KERB_ENCTYPE_RESOURCE_SID_COMPRESSION_DISABLED) {
2532
0
    group_inclusion = AUTH_INCLUDE_RESOURCE_GROUPS;
2533
0
  } else {
2534
0
    group_inclusion = AUTH_INCLUDE_RESOURCE_GROUPS_COMPRESSED;
2535
0
  }
2536
2537
0
  if (pk_reply_key != NULL) {
2538
0
    cred_ndr_ptr = &cred_ndr;
2539
0
  }
2540
2541
0
  ret = samba_kdc_get_user_info_from_db(frame,
2542
0
                kdc_db_ctx,
2543
0
                client,
2544
0
                client->msg,
2545
0
                &user_info_dc_const);
2546
0
  if (ret) {
2547
0
    TALLOC_FREE(frame);
2548
0
    return ret;
2549
0
  }
2550
2551
  /* Make a shallow copy of the user_info_dc structure. */
2552
0
  nt_status = authsam_shallow_copy_user_info_dc(frame,
2553
0
                  user_info_dc_const,
2554
0
                  &user_info_dc);
2555
0
  user_info_dc_const = NULL;
2556
2557
0
  if (!NT_STATUS_IS_OK(nt_status)) {
2558
0
    DBG_ERR("Failed to allocate user_info_dc SIDs: %s\n",
2559
0
      nt_errstr(nt_status));
2560
0
    TALLOC_FREE(frame);
2561
0
    return map_errno_from_nt_status(nt_status);
2562
0
  }
2563
2564
0
  nt_status = samba_kdc_add_asserted_identity(asserted_identity,
2565
0
                user_info_dc);
2566
0
  if (!NT_STATUS_IS_OK(nt_status)) {
2567
0
    DBG_ERR("Failed to add asserted identity: %s\n",
2568
0
      nt_errstr(nt_status));
2569
0
    TALLOC_FREE(frame);
2570
0
    return map_errno_from_nt_status(nt_status);
2571
0
  }
2572
2573
0
  nt_status = samba_kdc_add_claims_valid(user_info_dc);
2574
0
  if (!NT_STATUS_IS_OK(nt_status)) {
2575
0
    DBG_ERR("Failed to add Claims Valid: %s\n",
2576
0
      nt_errstr(nt_status));
2577
0
    TALLOC_FREE(frame);
2578
0
    return map_errno_from_nt_status(nt_status);
2579
0
  }
2580
2581
0
  if (pkinit_freshness) {
2582
0
    nt_status = samba_kdc_add_fresh_public_key_identity(user_info_dc);
2583
0
    if (!NT_STATUS_IS_OK(nt_status)) {
2584
0
      DBG_ERR("Failed to add Fresh Public Key Identity: %s\n",
2585
0
        nt_errstr(nt_status));
2586
0
      TALLOC_FREE(frame);
2587
0
      return map_errno_from_nt_status(nt_status);
2588
0
    }
2589
0
  }
2590
2591
0
  ret = samba_kdc_get_claims_data_from_db(kdc_db_ctx->samdb,
2592
0
            client,
2593
0
            &auth_claims.user_claims);
2594
0
  if (ret) {
2595
0
    TALLOC_FREE(frame);
2596
0
    return ret;
2597
0
  }
2598
2599
0
  nt_status = samba_kdc_get_claims_blob(frame,
2600
0
                kdc_db_ctx,
2601
0
                server,
2602
0
                auth_claims.user_claims,
2603
0
                &client_claims_blob);
2604
0
  if (!NT_STATUS_IS_OK(nt_status)) {
2605
0
    talloc_free(mem_ctx);
2606
0
    return map_errno_from_nt_status(nt_status);
2607
0
  }
2608
2609
  /*
2610
   * For an S4U2Self request, the authentication policy is not enforced.
2611
   */
2612
0
  if (!is_s4u2self &&
2613
0
      authn_policy_restrictions_present(server->server_policy))
2614
0
  {
2615
0
    const struct auth_user_info_dc *device_info_dc = NULL;
2616
2617
0
    if (samba_kdc_entry_pac_valid_principal(device)) {
2618
0
      ret = samba_kdc_get_user_info_dc(frame,
2619
0
               context,
2620
0
               kdc_db_ctx,
2621
0
               device,
2622
0
               &device_info_dc,
2623
0
               NULL /* resource_groups_out */);
2624
0
      if (ret) {
2625
0
        TALLOC_FREE(frame);
2626
0
        return ret;
2627
0
      }
2628
2629
0
      ret = samba_kdc_get_claims_data(frame,
2630
0
              context,
2631
0
              kdc_db_ctx,
2632
0
              device,
2633
0
              &auth_claims.device_claims,
2634
0
              NULL); /* _need_regeneration */
2635
0
      if (ret) {
2636
0
        TALLOC_FREE(frame);
2637
0
        return ret;
2638
0
      }
2639
0
    }
2640
2641
    /*
2642
     * Allocate the audit info and output status on to the parent
2643
     * mem_ctx, not the temporary context.
2644
     */
2645
0
    ret = samba_kdc_allowed_to_authenticate_to(mem_ctx,
2646
0
                 kdc_db_ctx,
2647
0
                 client,
2648
0
                 user_info_dc,
2649
0
                 device_info_dc,
2650
0
                 auth_claims,
2651
0
                 server,
2652
0
                 server_audit_info_out,
2653
0
                 status_out);
2654
0
    if (ret) {
2655
0
      TALLOC_FREE(frame);
2656
0
      return ret;
2657
0
    }
2658
0
  }
2659
2660
0
  nt_status = samba_kdc_get_logon_info_blob(frame,
2661
0
              user_info_dc,
2662
0
              NULL, /* resource_groups */
2663
0
              group_inclusion,
2664
0
              &logon_blob);
2665
0
  if (!NT_STATUS_IS_OK(nt_status)) {
2666
0
    TALLOC_FREE(frame);
2667
0
    return map_errno_from_nt_status(nt_status);
2668
0
  }
2669
2670
0
  if (cred_ndr_ptr != NULL) {
2671
0
    nt_status = samba_kdc_get_cred_ndr_blob(frame,
2672
0
              client,
2673
0
              cred_ndr_ptr);
2674
0
    if (!NT_STATUS_IS_OK(nt_status)) {
2675
0
      TALLOC_FREE(frame);
2676
0
      return map_errno_from_nt_status(nt_status);
2677
0
    }
2678
0
  }
2679
2680
0
  nt_status = samba_kdc_get_upn_info_blob(frame,
2681
0
            user_info_dc,
2682
0
            &upn_blob);
2683
0
  if (!NT_STATUS_IS_OK(nt_status)) {
2684
0
    TALLOC_FREE(frame);
2685
0
    return map_errno_from_nt_status(nt_status);
2686
0
  }
2687
2688
0
  if (is_krbtgt) {
2689
0
    nt_status = samba_kdc_get_pac_attrs_blob(frame,
2690
0
               pac_attributes,
2691
0
               &pac_attrs_blob);
2692
0
    if (!NT_STATUS_IS_OK(nt_status)) {
2693
0
      TALLOC_FREE(frame);
2694
0
      return map_errno_from_nt_status(nt_status);
2695
0
    }
2696
2697
0
    nt_status = samba_kdc_get_requester_sid_blob(frame,
2698
0
                   user_info_dc,
2699
0
                   &requester_sid_blob);
2700
0
    if (!NT_STATUS_IS_OK(nt_status)) {
2701
0
      TALLOC_FREE(frame);
2702
0
      return map_errno_from_nt_status(nt_status);
2703
0
    }
2704
0
  }
2705
2706
0
  if (pk_reply_key != NULL && cred_ndr != NULL) {
2707
0
    ret = samba_kdc_encrypt_pac_credentials(context,
2708
0
              pk_reply_key,
2709
0
              cred_ndr,
2710
0
              frame,
2711
0
              &_cred_blob);
2712
0
    if (ret != 0) {
2713
0
      TALLOC_FREE(frame);
2714
0
      return ret;
2715
0
    }
2716
0
    cred_blob = &_cred_blob;
2717
0
  }
2718
2719
0
  ret = samba_make_krb5_pac(context,
2720
0
          logon_blob,
2721
0
          cred_blob,
2722
0
          upn_blob,
2723
0
          pac_attrs_blob,
2724
0
          requester_sid_blob,
2725
0
          NULL, /* deleg_blob */
2726
0
          client_claims_blob,
2727
0
          NULL, /* device_info_blob */
2728
0
          NULL, /* device_claims_blob */
2729
0
          new_pac);
2730
2731
0
  TALLOC_FREE(frame);
2732
0
  return ret;
2733
0
}
2734
2735
/**
2736
 * @brief Update a PAC
2737
 *
2738
 * @param mem_ctx   A talloc memory context
2739
 *
2740
 * @param context   A krb5 context
2741
 *
2742
 * @param samdb     An open samdb connection.
2743
 *
2744
 * @param lp_ctx    A loadparm context.
2745
 *
2746
 * @param flags     Bitwise OR'ed flags
2747
 *
2748
 * @param device_pac_is_trusted Whether the device's PAC was issued by a trusted server,
2749
 *                              as opposed to an RODC.
2750
 *
2751
 * @param client    The client samba kdc PAC entry.
2752
 *
2753
 * @param server_principal  The server principal
2754
 *
2755
 * @param server    The server samba kdc entry.
2756
 *
2757
 * @param delegated_proxy   The delegated proxy kdc PAC entry.
2758
 *
2759
 * @param device    The computer's samba kdc PAC entry; used for compound
2760
 *                  authentication.
2761
 *
2762
 * @param new_pac                   The new already allocated PAC
2763
 *
2764
 * @return A Kerberos error code. If no PAC should be returned, the code will be
2765
 * ENOATTR!
2766
 */
2767
krb5_error_code samba_kdc_update_pac(TALLOC_CTX *mem_ctx,
2768
             krb5_context context,
2769
             struct samba_kdc_db_context *kdc_db_ctx,
2770
             uint32_t flags,
2771
             const struct samba_kdc_entry_pac client,
2772
             const krb5_const_principal server_principal,
2773
             const struct samba_kdc_entry *server,
2774
             const struct samba_kdc_entry_pac delegated_proxy,
2775
             const struct samba_kdc_entry_pac device,
2776
             krb5_pac new_pac,
2777
             struct authn_audit_info **server_audit_info_out,
2778
             NTSTATUS *status_out)
2779
0
{
2780
0
  TALLOC_CTX *tmp_ctx = NULL;
2781
0
  krb5_error_code code = EINVAL;
2782
0
  NTSTATUS nt_status;
2783
0
  DATA_BLOB *pac_blob = NULL;
2784
0
  DATA_BLOB *upn_blob = NULL;
2785
0
  DATA_BLOB *deleg_blob = NULL;
2786
0
  DATA_BLOB *requester_sid_blob = NULL;
2787
0
  krb5_const_principal delegated_proxy_principal = delegated_proxy.pac_princ;
2788
0
  const DATA_BLOB *client_claims_blob = NULL;
2789
0
  const DATA_BLOB *device_claims_blob = NULL;
2790
0
  struct auth_claims pac_claims = {};
2791
0
  DATA_BLOB *device_info_blob = NULL;
2792
0
  bool is_tgs = false;
2793
0
  bool server_restrictions_present = false;
2794
0
  struct pac_blobs *pac_blobs = NULL;
2795
0
  const struct auth_user_info_dc *user_info_dc_const = NULL;
2796
0
  const struct auth_user_info_dc *device_info_dc = NULL;
2797
0
  const struct PAC_DOMAIN_GROUP_MEMBERSHIP *_resource_groups = NULL;
2798
0
  enum auth_group_inclusion group_inclusion;
2799
0
  bool compounded_auth = false;
2800
0
  bool need_device = false;
2801
0
  bool regenerate_client_claims = false;
2802
0
  bool regenerate_device_claims = false;
2803
0
  bool need_access_check = false;
2804
0
  const struct auth_user_info_dc *deleg_info_dc = NULL;
2805
0
  struct auth_claims deleg_claims = {};
2806
0
  size_t i = 0;
2807
2808
0
  if (server_audit_info_out != NULL) {
2809
0
    *server_audit_info_out = NULL;
2810
0
  }
2811
2812
0
  if (status_out != NULL) {
2813
0
    *status_out = NT_STATUS_OK;
2814
0
  }
2815
2816
0
  tmp_ctx = talloc_new(mem_ctx);
2817
0
  if (tmp_ctx == NULL) {
2818
0
    code = ENOMEM;
2819
0
    goto done;
2820
0
  }
2821
2822
0
  {
2823
0
    int result = smb_krb5_principal_is_tgs(context, server_principal);
2824
0
    if (result == -1) {
2825
0
      code = ENOMEM;
2826
0
      goto done;
2827
0
    }
2828
2829
0
    is_tgs = result;
2830
0
  }
2831
2832
  /* Only include resource groups in a service ticket. */
2833
0
  if (is_tgs) {
2834
0
    group_inclusion = AUTH_EXCLUDE_RESOURCE_GROUPS;
2835
0
  } else if (server->supported_enctypes & KERB_ENCTYPE_RESOURCE_SID_COMPRESSION_DISABLED) {
2836
0
    group_inclusion = AUTH_INCLUDE_RESOURCE_GROUPS;
2837
0
  } else {
2838
0
    group_inclusion = AUTH_INCLUDE_RESOURCE_GROUPS_COMPRESSED;
2839
0
  }
2840
2841
  /*
2842
   * If we are creating a TGT, resource groups from our domain are not to
2843
   * be put into the PAC. Instead, we take the resource groups directly
2844
   * from the original PAC and copy them unmodified into the new one.
2845
   */
2846
0
  code = samba_kdc_get_user_info_dc(tmp_ctx,
2847
0
            context,
2848
0
            kdc_db_ctx,
2849
0
            client,
2850
0
            &user_info_dc_const,
2851
0
            is_tgs ? &_resource_groups : NULL);
2852
0
  if (code != 0) {
2853
0
    const char *err_str = krb5_get_error_message(context, code);
2854
0
    DBG_ERR("samba_kdc_get_user_info_dc failed: %s\n",
2855
0
      err_str != NULL ? err_str : "<unknown>");
2856
0
    krb5_free_error_message(context, err_str);
2857
2858
0
    goto done;
2859
0
  }
2860
2861
  /* Fetch the user’s claims. */
2862
0
  code = samba_kdc_get_claims_data(tmp_ctx,
2863
0
           context,
2864
0
           kdc_db_ctx,
2865
0
           client,
2866
0
           &pac_claims.user_claims,
2867
0
           &regenerate_client_claims);
2868
0
  if (code) {
2869
0
    if (code == KRB5KDC_ERR_POLICY) {
2870
0
      if (status_out != NULL) {
2871
        /* TODO: is there some better status ? */
2872
0
        *status_out =
2873
0
        NT_STATUS_ACCESS_DISABLED_BY_POLICY_OTHER;
2874
0
      }
2875
0
    }
2876
0
    goto done;
2877
0
  }
2878
2879
0
  if (!is_tgs) {
2880
0
    server_restrictions_present = authn_policy_restrictions_present(
2881
0
              server->server_policy);
2882
2883
0
    if (server_restrictions_present) {
2884
0
      need_access_check = true;
2885
0
    }
2886
2887
0
    if (samba_kdc_entry_pac_valid_principal(device)) {
2888
0
      compounded_auth = server->supported_enctypes &
2889
0
        KERB_ENCTYPE_COMPOUND_IDENTITY_SUPPORTED;
2890
2891
0
      if (!(flags & SAMBA_KDC_FLAG_EXPLICIT_ARMOR_PRESENT)) {
2892
0
        compounded_auth = false;
2893
0
      }
2894
2895
0
      if (need_access_check || compounded_auth) {
2896
0
        need_device = true;
2897
0
      }
2898
0
    }
2899
0
  }
2900
2901
0
  if (need_device) {
2902
0
    code = samba_kdc_get_user_info_dc(tmp_ctx,
2903
0
              context,
2904
0
              kdc_db_ctx,
2905
0
              device,
2906
0
              &device_info_dc,
2907
0
              NULL /* resource_groups_out */);
2908
0
    if (code) {
2909
0
      goto done;
2910
0
    }
2911
2912
    /*
2913
     * [MS-KILE] 3.3.5.7.4 Compound Identity: the client claims from
2914
     * the device PAC become the device claims in the new PAC.
2915
     */
2916
0
    code = samba_kdc_get_claims_data(tmp_ctx,
2917
0
             context,
2918
0
             kdc_db_ctx,
2919
0
             device,
2920
0
             &pac_claims.device_claims,
2921
0
             &regenerate_device_claims);
2922
0
    if (code) {
2923
0
      if (code == KRB5KDC_ERR_POLICY) {
2924
0
        if (status_out != NULL) {
2925
          /* TODO: is there some better status ? */
2926
0
          *status_out =
2927
0
          NT_STATUS_ACCESS_DISABLED_BY_POLICY_OTHER;
2928
0
        }
2929
0
      }
2930
0
      goto done;
2931
0
    }
2932
0
  }
2933
2934
0
  if (delegated_proxy_principal != NULL) {
2935
0
    deleg_blob = talloc_zero(tmp_ctx, DATA_BLOB);
2936
0
    if (deleg_blob == NULL) {
2937
0
      code = ENOMEM;
2938
0
      goto done;
2939
0
    }
2940
2941
0
    nt_status = samba_kdc_update_delegation_info_blob(
2942
0
        deleg_blob,
2943
0
        context,
2944
0
        client.pac,
2945
0
        server_principal,
2946
0
        delegated_proxy_principal,
2947
0
        deleg_blob);
2948
0
    if (!NT_STATUS_IS_OK(nt_status)) {
2949
0
      DBG_ERR("update delegation info blob failed: %s\n",
2950
0
        nt_errstr(nt_status));
2951
0
      code = map_errno_from_nt_status(nt_status);
2952
0
      goto done;
2953
0
    }
2954
0
  }
2955
2956
0
  if (need_access_check &&
2957
0
      samba_kdc_entry_pac_valid_principal(delegated_proxy))
2958
0
  {
2959
0
    code = samba_kdc_get_user_info_dc(tmp_ctx,
2960
0
              context,
2961
0
              kdc_db_ctx,
2962
0
              delegated_proxy,
2963
0
              &deleg_info_dc,
2964
0
              NULL /* resource_groups_out */);
2965
0
    if (code) {
2966
0
      goto done;
2967
0
    }
2968
2969
    /* Fetch the delegated proxy claims. */
2970
0
    code = samba_kdc_get_claims_data(tmp_ctx,
2971
0
             context,
2972
0
             kdc_db_ctx,
2973
0
             delegated_proxy,
2974
0
             &deleg_claims.user_claims,
2975
0
             NULL); /* _need_regeneration */
2976
0
    if (code) {
2977
0
      goto done;
2978
0
    }
2979
2980
0
    deleg_claims.device_claims = pac_claims.device_claims;
2981
0
  }
2982
2983
  /*
2984
   * Enforce the AllowedToAuthenticateTo part of an authentication policy,
2985
   * if one is present.
2986
   */
2987
0
  if (server_restrictions_present) {
2988
0
    struct samba_kdc_entry_pac auth_entry;
2989
0
    const struct auth_user_info_dc *auth_user_info_dc = NULL;
2990
0
    struct auth_claims auth_claims = {};
2991
2992
0
    if (samba_kdc_entry_pac_valid_principal(delegated_proxy)) {
2993
0
      auth_entry = delegated_proxy;
2994
0
      auth_user_info_dc = deleg_info_dc;
2995
0
      auth_claims = deleg_claims;
2996
0
    } else {
2997
0
      auth_entry = client;
2998
0
      auth_user_info_dc = user_info_dc_const;
2999
0
      auth_claims = pac_claims;
3000
0
    }
3001
3002
    /*
3003
     * Allocate the audit info and output status on to the parent
3004
     * mem_ctx, not the temporary context.
3005
     */
3006
0
    code = samba_kdc_allowed_to_authenticate_to(mem_ctx,
3007
0
                  kdc_db_ctx,
3008
0
                  auth_entry.entry,
3009
0
                  auth_user_info_dc,
3010
0
                  device_info_dc,
3011
0
                  auth_claims,
3012
0
                  server,
3013
0
                  server_audit_info_out,
3014
0
                  status_out);
3015
0
    if (code) {
3016
0
      goto done;
3017
0
    }
3018
0
  }
3019
3020
0
  if (compounded_auth) {
3021
0
    struct auth_user_info_dc *user_info_dc_shallow_copy = NULL;
3022
3023
    /* Make a shallow copy of the user_info_dc structure. */
3024
0
    nt_status = authsam_shallow_copy_user_info_dc(tmp_ctx,
3025
0
                    user_info_dc_const,
3026
0
                    &user_info_dc_shallow_copy);
3027
0
    user_info_dc_const = NULL;
3028
3029
0
    if (!NT_STATUS_IS_OK(nt_status)) {
3030
0
      DBG_ERR("Failed to copy user_info_dc: %s\n",
3031
0
        nt_errstr(nt_status));
3032
3033
0
      code = KRB5KDC_ERR_TGT_REVOKED;
3034
0
      goto done;
3035
0
    }
3036
3037
0
    nt_status = samba_kdc_add_compounded_auth(user_info_dc_shallow_copy);
3038
0
    if (!NT_STATUS_IS_OK(nt_status)) {
3039
0
      DBG_ERR("Failed to add Compounded Authentication: %s\n",
3040
0
        nt_errstr(nt_status));
3041
3042
0
      code = KRB5KDC_ERR_TGT_REVOKED;
3043
0
      goto done;
3044
0
    }
3045
3046
    /* We can now set back to the const, it will not be modified */
3047
0
    user_info_dc_const = user_info_dc_shallow_copy;
3048
0
  }
3049
3050
0
  nt_status = samba_kdc_get_logon_info_blob(tmp_ctx,
3051
0
              user_info_dc_const,
3052
0
              _resource_groups,
3053
0
              group_inclusion,
3054
0
              &pac_blob);
3055
0
  if (!NT_STATUS_IS_OK(nt_status)) {
3056
0
    DBG_ERR("samba_kdc_get_logon_info_blob failed: %s\n",
3057
0
      nt_errstr(nt_status));
3058
0
    code = KRB5KDC_ERR_TGT_REVOKED;
3059
0
    goto done;
3060
0
  }
3061
3062
0
  nt_status = samba_kdc_get_upn_info_blob(tmp_ctx,
3063
0
            user_info_dc_const,
3064
0
            &upn_blob);
3065
0
  if (!NT_STATUS_IS_OK(nt_status)) {
3066
0
    DBG_ERR("samba_kdc_get_upn_info_blob failed: %s\n",
3067
0
      nt_errstr(nt_status));
3068
0
    code = KRB5KDC_ERR_TGT_REVOKED;
3069
0
    goto done;
3070
0
  }
3071
3072
0
  if (!samba_krb5_pac_is_trusted(client) && is_tgs) {
3073
0
    nt_status = samba_kdc_get_requester_sid_blob(tmp_ctx,
3074
0
                   user_info_dc_const,
3075
0
                   &requester_sid_blob);
3076
0
    if (!NT_STATUS_IS_OK(nt_status)) {
3077
0
      DBG_ERR("samba_kdc_get_requester_sid_blob failed: %s\n",
3078
0
        nt_errstr(nt_status));
3079
0
      code = KRB5KDC_ERR_TGT_REVOKED;
3080
0
      goto done;
3081
0
    }
3082
0
  }
3083
3084
0
  if (regenerate_client_claims) {
3085
0
    nt_status = samba_kdc_get_claims_blob(tmp_ctx,
3086
0
                  kdc_db_ctx,
3087
0
                  server,
3088
0
                  pac_claims.user_claims,
3089
0
                  &client_claims_blob);
3090
0
    if (!NT_STATUS_IS_OK(nt_status)) {
3091
0
      DBG_ERR("samba_kdc_get_claims_blob failed: %s\n",
3092
0
        nt_errstr(nt_status));
3093
0
      if (NT_STATUS_EQUAL(nt_status,
3094
0
        NT_STATUS_ACCESS_DISABLED_BY_POLICY_OTHER))
3095
0
      {
3096
0
        code = KRB5KDC_ERR_POLICY;
3097
0
        if (status_out != NULL) {
3098
          /* TODO: s there some better status ? */
3099
0
          *status_out = nt_status;
3100
0
        }
3101
0
      } else {
3102
0
        code = map_errno_from_nt_status(nt_status);
3103
0
      }
3104
0
      goto done;
3105
0
    }
3106
0
  }
3107
3108
0
  if (compounded_auth) {
3109
0
    code = samba_kdc_get_device_info_blob(tmp_ctx,
3110
0
                  context,
3111
0
                  kdc_db_ctx,
3112
0
                  device_info_dc,
3113
0
                  &device_info_blob);
3114
0
    if (code != 0) {
3115
0
      goto done;
3116
0
    }
3117
3118
0
    if (regenerate_device_claims) {
3119
0
      nt_status = samba_kdc_get_claims_blob(tmp_ctx,
3120
0
                    kdc_db_ctx,
3121
0
                    server,
3122
0
                    pac_claims.device_claims,
3123
0
                    &device_claims_blob);
3124
0
      if (!NT_STATUS_IS_OK(nt_status)) {
3125
0
        DBG_ERR("samba_kdc_get_claims_blob() failed: %s\n",
3126
0
          nt_errstr(nt_status));
3127
0
        if (NT_STATUS_EQUAL(nt_status,
3128
0
          NT_STATUS_ACCESS_DISABLED_BY_POLICY_OTHER))
3129
0
        {
3130
0
          code = KRB5KDC_ERR_POLICY;
3131
0
          if (status_out != NULL) {
3132
            /* TODO: s there some better status ? */
3133
0
            *status_out = nt_status;
3134
0
          }
3135
0
        } else {
3136
0
          code = map_errno_from_nt_status(nt_status);
3137
0
        }
3138
0
        goto done;
3139
0
      }
3140
0
    }
3141
0
  }
3142
3143
  /* Check the types of the given PAC */
3144
0
  code = pac_blobs_from_krb5_pac(tmp_ctx,
3145
0
               context,
3146
0
               client.pac,
3147
0
               &pac_blobs);
3148
0
  if (code != 0) {
3149
0
    goto done;
3150
0
  }
3151
3152
0
  code = pac_blobs_replace_existing(pac_blobs,
3153
0
            PAC_TYPE_LOGON_INFO,
3154
0
            pac_blob);
3155
0
  if (code != 0) {
3156
0
    goto done;
3157
0
  }
3158
3159
0
#ifdef SAMBA4_USES_HEIMDAL
3160
  /* Not needed with MIT Kerberos */
3161
0
  code = pac_blobs_replace_existing(pac_blobs,
3162
0
            PAC_TYPE_LOGON_NAME,
3163
0
            &data_blob_null);
3164
0
  if (code != 0) {
3165
0
    goto done;
3166
0
  }
3167
3168
0
  code = pac_blobs_replace_existing(pac_blobs,
3169
0
            PAC_TYPE_SRV_CHECKSUM,
3170
0
            &data_blob_null);
3171
0
  if (code != 0) {
3172
0
    goto done;
3173
0
  }
3174
3175
0
  code = pac_blobs_replace_existing(pac_blobs,
3176
0
            PAC_TYPE_KDC_CHECKSUM,
3177
0
            &data_blob_null);
3178
0
  if (code != 0) {
3179
0
    goto done;
3180
0
  }
3181
0
#endif
3182
3183
0
  code = pac_blobs_add_blob(pac_blobs,
3184
0
          PAC_TYPE_CONSTRAINED_DELEGATION,
3185
0
          deleg_blob);
3186
0
  if (code != 0) {
3187
0
    goto done;
3188
0
  }
3189
3190
0
  code = pac_blobs_add_blob(pac_blobs,
3191
0
          PAC_TYPE_UPN_DNS_INFO,
3192
0
          upn_blob);
3193
0
  if (code != 0) {
3194
0
    goto done;
3195
0
  }
3196
3197
0
  code = pac_blobs_add_blob(pac_blobs,
3198
0
          PAC_TYPE_CLIENT_CLAIMS_INFO,
3199
0
          client_claims_blob);
3200
0
  if (code != 0) {
3201
0
    goto done;
3202
0
  }
3203
3204
0
  code = pac_blobs_add_blob(pac_blobs,
3205
0
          PAC_TYPE_DEVICE_INFO,
3206
0
          device_info_blob);
3207
0
  if (code != 0) {
3208
0
    goto done;
3209
0
  }
3210
3211
0
  code = pac_blobs_add_blob(pac_blobs,
3212
0
          PAC_TYPE_DEVICE_CLAIMS_INFO,
3213
0
          device_claims_blob);
3214
0
  if (code != 0) {
3215
0
    goto done;
3216
0
  }
3217
3218
0
  if (!samba_krb5_pac_is_trusted(client) || !is_tgs) {
3219
0
    pac_blobs_remove_blob(pac_blobs,
3220
0
              PAC_TYPE_ATTRIBUTES_INFO);
3221
0
  }
3222
3223
0
  if (!is_tgs) {
3224
0
    pac_blobs_remove_blob(pac_blobs,
3225
0
              PAC_TYPE_REQUESTER_SID);
3226
0
  }
3227
3228
0
  code = pac_blobs_add_blob(pac_blobs,
3229
0
          PAC_TYPE_REQUESTER_SID,
3230
0
          requester_sid_blob);
3231
0
  if (code != 0) {
3232
0
    goto done;
3233
0
  }
3234
3235
  /*
3236
   * The server account may be set not to want the PAC.
3237
   *
3238
   * While this is wasteful if the above calculations were done
3239
   * and now thrown away, this is cleaner as we do any ticket
3240
   * signature checking etc always.
3241
   *
3242
   * UF_NO_AUTH_DATA_REQUIRED is the rare case and most of the
3243
   * time (eg not accepting a ticket from the RODC) we do not
3244
   * need to re-generate anything anyway.
3245
   */
3246
0
  if (!samba_princ_needs_pac(server)) {
3247
0
    code = ENOATTR;
3248
0
    goto done;
3249
0
  }
3250
3251
0
  if (samba_krb5_pac_is_trusted(client) && !is_tgs) {
3252
    /*
3253
     * The client may have requested no PAC when obtaining the
3254
     * TGT.
3255
     */
3256
0
    bool requested_pac = false;
3257
3258
0
    code = samba_client_requested_pac(context,
3259
0
              client.pac,
3260
0
              tmp_ctx,
3261
0
              &requested_pac);
3262
0
    if (code != 0 || !requested_pac) {
3263
0
      if (!requested_pac) {
3264
0
        code = ENOATTR;
3265
0
      }
3266
0
      goto done;
3267
0
    }
3268
0
  }
3269
3270
0
  for (i = 0; i < pac_blobs->num_types; ++i) {
3271
0
    krb5_data type_data;
3272
0
    const DATA_BLOB *type_blob = pac_blobs->type_blobs[i].data;
3273
0
    uint32_t type = pac_blobs->type_blobs[i].type;
3274
3275
0
    static char null_byte = '\0';
3276
0
    const krb5_data null_data = smb_krb5_make_data(&null_byte, 0);
3277
3278
#ifndef SAMBA4_USES_HEIMDAL
3279
    /* Not needed with MIT Kerberos */
3280
    switch(type) {
3281
    case PAC_TYPE_LOGON_NAME:
3282
    case PAC_TYPE_SRV_CHECKSUM:
3283
    case PAC_TYPE_KDC_CHECKSUM:
3284
    case PAC_TYPE_FULL_CHECKSUM:
3285
      continue;
3286
    default:
3287
      break;
3288
    }
3289
#endif
3290
3291
0
    if (type_blob != NULL) {
3292
0
      type_data = smb_krb5_data_from_blob(*type_blob);
3293
      /*
3294
       * Passing a NULL pointer into krb5_pac_add_buffer() is
3295
       * not allowed, so pass null_data instead if needed.
3296
       */
3297
0
      code = krb5_pac_add_buffer(context,
3298
0
               new_pac,
3299
0
               type,
3300
0
               (type_data.data != NULL) ? &type_data : &null_data);
3301
0
      if (code != 0) {
3302
0
        goto done;
3303
0
      }
3304
0
    } else if (samba_krb5_pac_is_trusted(client)) {
3305
      /*
3306
       * Convey the buffer from the original PAC if we can
3307
       * trust it.
3308
       */
3309
3310
0
      code = krb5_pac_get_buffer(context,
3311
0
               client.pac,
3312
0
               type,
3313
0
               &type_data);
3314
0
      if (code != 0) {
3315
0
        goto done;
3316
0
      }
3317
      /*
3318
       * Passing a NULL pointer into krb5_pac_add_buffer() is
3319
       * not allowed, so pass null_data instead if needed.
3320
       */
3321
0
      code = krb5_pac_add_buffer(context,
3322
0
               new_pac,
3323
0
               type,
3324
0
               (type_data.data != NULL) ? &type_data : &null_data);
3325
0
      smb_krb5_free_data_contents(context, &type_data);
3326
0
      if (code != 0) {
3327
0
        goto done;
3328
0
      }
3329
0
    }
3330
0
  }
3331
3332
0
  code = 0;
3333
0
done:
3334
0
  TALLOC_FREE(tmp_ctx);
3335
0
  return code;
3336
0
}
3337
3338
static
3339
krb5_error_code samba_kdc_get_claims_data_from_pac(TALLOC_CTX *mem_ctx,
3340
               krb5_context context,
3341
               struct samba_kdc_entry_pac entry,
3342
               struct claims_data **claims_data_out,
3343
               bool *was_found);
3344
3345
static
3346
krb5_error_code samba_kdc_get_claims_data(TALLOC_CTX *mem_ctx,
3347
            krb5_context context,
3348
            struct samba_kdc_db_context *kdc_db_ctx,
3349
            struct samba_kdc_entry_pac entry,
3350
            struct claims_data **claims_data_out,
3351
            bool *_need_regeneration)
3352
0
{
3353
0
  bool was_found = false;
3354
0
  krb5_error_code code;
3355
3356
0
  if (_need_regeneration != NULL) {
3357
0
    *_need_regeneration = false;
3358
0
  }
3359
3360
0
  if (!samba_krb5_pac_is_trusted(entry)) {
3361
0
    if (_need_regeneration != NULL) {
3362
0
      *_need_regeneration = true;
3363
0
    }
3364
3365
0
    return samba_kdc_get_claims_data_from_db(kdc_db_ctx->samdb,
3366
0
               entry.entry,
3367
0
               claims_data_out);
3368
0
  }
3369
3370
0
  code = samba_kdc_get_claims_data_from_pac(mem_ctx,
3371
0
              context,
3372
0
              entry,
3373
0
              claims_data_out,
3374
0
              &was_found);
3375
0
  if (code != 0) {
3376
0
    return code;
3377
0
  }
3378
3379
0
  if (was_found && samba_kdc_entry_pac_issued_by_trust(entry)) {
3380
0
    const char *tdo_attr = "msDS-IngressClaimsTransformationPolicy";
3381
0
    struct claims_tf_rule_set *rule_set = NULL;
3382
0
    NTSTATUS status;
3383
0
    bool clear_claims = false;
3384
3385
0
    status = dsdb_trust_get_claims_tf_policy(kdc_db_ctx->samdb,
3386
0
               entry.krbtgt->msg,
3387
0
               tdo_attr,
3388
0
               mem_ctx,
3389
0
               &rule_set);
3390
0
    if (NT_STATUS_EQUAL(status, NT_STATUS_DS_NO_ATTRIBUTE_OR_VALUE)) {
3391
      /*
3392
       * No msDS-IngressClaimsTransformationPolicy
3393
       * or no msDS-TransformationRules attribute
3394
       *
3395
       * The default is to clear all claims
3396
       */
3397
0
      clear_claims = true;
3398
0
    } else if (NT_STATUS_IS_OK(status)) {
3399
      /*
3400
       * There's a policy defined,
3401
       *
3402
       * For now we only allow it an
3403
       * empty rule set, which is deny
3404
       * all claims policy.
3405
       *
3406
       * All others result in an error
3407
       * in order to avoid unexpected behavior
3408
       *
3409
       * TODO apply the transformation completely:
3410
       * 1. apply everything from rule_set
3411
       * 2. validate and filter based on
3412
       *    the defined claims in our forest
3413
       *    (unknown claims or wrong value types
3414
       *    cause a claim to be removed, instead
3415
       *    of generating errors)
3416
       */
3417
3418
0
      clear_claims = claims_tf_rule_set_is_deny_all(rule_set);
3419
0
      if (!clear_claims) {
3420
0
        return KRB5KDC_ERR_POLICY;
3421
0
      }
3422
0
    } else {
3423
      /*
3424
       * We hit an error, which means we need to
3425
       * clear the claims
3426
       */
3427
0
      DBG_WARNING("dsdb_trust_get_claims_tf_policy() %s\n",
3428
0
            nt_errstr(status));
3429
0
      clear_claims = true;
3430
0
    }
3431
3432
0
    SMB_ASSERT(clear_claims);
3433
0
    TALLOC_FREE(*claims_data_out);
3434
0
    status = claims_data_from_encoded_claims_set(mem_ctx,
3435
0
                   NULL,
3436
0
                   claims_data_out);
3437
0
    if (!NT_STATUS_IS_OK(status)) {
3438
0
      return map_errno_from_nt_status(status);
3439
0
    }
3440
0
  }
3441
3442
0
  if (_need_regeneration != NULL) {
3443
0
    *_need_regeneration = was_found;
3444
0
  }
3445
3446
0
  return 0;
3447
0
}
3448
3449
static
3450
krb5_error_code samba_kdc_get_claims_data_from_pac(TALLOC_CTX *mem_ctx,
3451
               krb5_context context,
3452
               struct samba_kdc_entry_pac entry,
3453
               struct claims_data **claims_data_out,
3454
               bool *was_found)
3455
0
{
3456
0
  TALLOC_CTX *frame = NULL;
3457
0
  krb5_data claims_info = {};
3458
0
  struct claims_data *claims_data = NULL;
3459
0
  NTSTATUS status = NT_STATUS_OK;
3460
0
  krb5_error_code code;
3461
3462
0
  *was_found = false;
3463
3464
0
  if (!samba_krb5_pac_is_trusted(entry)) {
3465
0
    code = EINVAL;
3466
0
    goto out;
3467
0
  }
3468
3469
0
  if (claims_data_out == NULL) {
3470
0
    code = EINVAL;
3471
0
    goto out;
3472
0
  }
3473
3474
0
  *claims_data_out = NULL;
3475
3476
0
  if (entry.entry != NULL && entry.entry->claims_from_pac_are_initialized) {
3477
    /* Note: the caller does not own this! */
3478
0
    *claims_data_out = entry.entry->claims_from_pac;
3479
0
    return 0;
3480
0
  }
3481
3482
0
  frame = talloc_stackframe();
3483
3484
  /* Fetch the claims from the PAC. */
3485
0
  code = krb5_pac_get_buffer(context, entry.pac,
3486
0
           PAC_TYPE_CLIENT_CLAIMS_INFO,
3487
0
           &claims_info);
3488
0
  if (code == ENOENT) {
3489
    /* OK. */
3490
0
    krb5_clear_error_message(context);
3491
0
    code = 0;
3492
0
    claims_info.length = 0;
3493
0
  } else if (code != 0) {
3494
0
    DBG_ERR("Error getting CLIENT_CLAIMS_INFO from PAC\n");
3495
0
    goto out;
3496
0
  } else {
3497
0
    *was_found = true;
3498
0
  }
3499
3500
0
  if (claims_info.length) {
3501
0
    DATA_BLOB claims_blob = data_blob_const(claims_info.data,
3502
0
              claims_info.length);
3503
3504
0
    status = claims_data_from_encoded_claims_set(frame,
3505
0
                   &claims_blob,
3506
0
                   &claims_data);
3507
0
    if (!NT_STATUS_IS_OK(status)) {
3508
0
      code = map_errno_from_nt_status(status);
3509
0
      goto out;
3510
0
    }
3511
0
  }
3512
3513
0
  if (entry.entry != NULL) {
3514
    /* Note: the caller does not own this! */
3515
0
    entry.entry->claims_from_pac = talloc_steal(entry.entry,
3516
0
                  claims_data);
3517
0
    entry.entry->claims_from_pac_are_initialized = true;
3518
0
  } else {
3519
0
    talloc_steal(mem_ctx, claims_data);
3520
0
  }
3521
3522
0
  *claims_data_out = claims_data;
3523
3524
0
out:
3525
0
  smb_krb5_free_data_contents(context, &claims_info);
3526
0
  talloc_free(frame);
3527
0
  return code;
3528
0
}
3529
3530
static
3531
krb5_error_code samba_kdc_get_claims_data_from_db(struct ldb_context *samdb,
3532
              struct samba_kdc_entry *entry,
3533
              struct claims_data **claims_data_out)
3534
0
{
3535
0
  TALLOC_CTX *frame = NULL;
3536
3537
0
  struct claims_data *claims_data = NULL;
3538
0
  struct CLAIMS_SET *claims_set = NULL;
3539
0
  NTSTATUS status = NT_STATUS_OK;
3540
0
  krb5_error_code code;
3541
3542
0
  if (samdb == NULL) {
3543
0
    code = EINVAL;
3544
0
    goto out;
3545
0
  }
3546
3547
0
  if (claims_data_out == NULL) {
3548
0
    code = EINVAL;
3549
0
    goto out;
3550
0
  }
3551
3552
0
  if (entry == NULL) {
3553
0
    code = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
3554
0
    goto out;
3555
0
  }
3556
3557
0
  *claims_data_out = NULL;
3558
3559
0
  if (entry->claims_from_db_are_initialized) {
3560
    /* Note: the caller does not own this! */
3561
0
    *claims_data_out = entry->claims_from_db;
3562
0
    return 0;
3563
0
  }
3564
3565
0
  frame = talloc_stackframe();
3566
3567
0
  code = get_claims_set_for_principal(samdb,
3568
0
              frame,
3569
0
              entry->msg,
3570
0
              &claims_set);
3571
0
  if (code) {
3572
0
    DBG_ERR("Failed to fetch claims\n");
3573
0
    goto out;
3574
0
  }
3575
3576
0
  if (claims_set != NULL) {
3577
0
    status = claims_data_from_claims_set(claims_data,
3578
0
                 claims_set,
3579
0
                 &claims_data);
3580
0
    if (!NT_STATUS_IS_OK(status)) {
3581
0
      code = map_errno_from_nt_status(status);
3582
0
      goto out;
3583
0
    }
3584
0
  }
3585
3586
0
  entry->claims_from_db = talloc_steal(entry,
3587
0
               claims_data);
3588
0
  entry->claims_from_db_are_initialized = true;
3589
3590
  /* Note: the caller does not own this! */
3591
0
  *claims_data_out = entry->claims_from_db;
3592
3593
0
out:
3594
0
  talloc_free(frame);
3595
0
  return code;
3596
0
}
3597
3598
krb5_error_code samba_kdc_check_device(TALLOC_CTX *mem_ctx,
3599
               krb5_context context,
3600
               struct samba_kdc_db_context *kdc_db_ctx,
3601
               const struct samba_kdc_entry_pac device,
3602
               const struct authn_kerberos_client_policy *client_policy,
3603
               struct authn_audit_info **client_audit_info_out,
3604
               NTSTATUS *status_out)
3605
0
{
3606
0
  TALLOC_CTX *frame = NULL;
3607
0
  struct ldb_context *samdb = kdc_db_ctx->samdb;
3608
0
  struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
3609
0
  krb5_error_code code = 0;
3610
0
  NTSTATUS nt_status;
3611
0
  const struct auth_user_info_dc *device_info = NULL;
3612
0
  struct authn_audit_info *client_audit_info = NULL;
3613
0
  struct auth_claims auth_claims = {};
3614
3615
0
  if (status_out != NULL) {
3616
0
    *status_out = NT_STATUS_OK;
3617
0
  }
3618
3619
0
  if (!authn_policy_device_restrictions_present(client_policy)) {
3620
0
    return 0;
3621
0
  }
3622
3623
0
  if (device.entry == NULL || device.pac == NULL) {
3624
0
    NTSTATUS out_status = NT_STATUS_INVALID_WORKSTATION;
3625
3626
0
    nt_status = authn_kerberos_client_policy_audit_info(mem_ctx,
3627
0
                    client_policy,
3628
0
                    NULL /* client_info */,
3629
0
                    AUTHN_AUDIT_EVENT_KERBEROS_DEVICE_RESTRICTION,
3630
0
                    AUTHN_AUDIT_REASON_FAST_REQUIRED,
3631
0
                    out_status,
3632
0
                    client_audit_info_out);
3633
0
    if (!NT_STATUS_IS_OK(nt_status)) {
3634
0
      code = KRB5KRB_ERR_GENERIC;
3635
0
    } else if (authn_kerberos_client_policy_is_enforced(client_policy)) {
3636
0
      code = KRB5KDC_ERR_POLICY;
3637
3638
0
      if (status_out != NULL) {
3639
0
        *status_out = out_status;
3640
0
      }
3641
0
    } else {
3642
      /* OK. */
3643
0
      code = 0;
3644
0
    }
3645
3646
0
    goto out;
3647
0
  }
3648
3649
0
  frame = talloc_stackframe();
3650
3651
0
  code = samba_kdc_get_user_info_dc(frame,
3652
0
            context,
3653
0
            kdc_db_ctx,
3654
0
            device,
3655
0
            &device_info,
3656
0
            NULL);
3657
0
  if (code) {
3658
0
    goto out;
3659
0
  }
3660
3661
  /*
3662
   * The device claims become the *user* claims for the purpose of
3663
   * evaluating a conditional ACE expression.
3664
   */
3665
0
  code = samba_kdc_get_claims_data(frame,
3666
0
           context,
3667
0
           kdc_db_ctx,
3668
0
           device,
3669
0
           &auth_claims.user_claims,
3670
0
           NULL); /* _need_regeneration */
3671
0
  if (code) {
3672
0
    goto out;
3673
0
  }
3674
3675
0
  nt_status = authn_policy_authenticate_from_device(frame,
3676
0
                samdb,
3677
0
                lp_ctx,
3678
0
                device_info,
3679
0
                auth_claims,
3680
0
                client_policy,
3681
0
                &client_audit_info);
3682
0
  if (client_audit_info != NULL) {
3683
0
    *client_audit_info_out = talloc_move(mem_ctx, &client_audit_info);
3684
0
  }
3685
0
  if (!NT_STATUS_IS_OK(nt_status)) {
3686
0
    if (NT_STATUS_EQUAL(nt_status, NT_STATUS_AUTHENTICATION_FIREWALL_FAILED)) {
3687
0
      code = KRB5KDC_ERR_POLICY;
3688
0
    } else {
3689
0
      code = KRB5KRB_ERR_GENERIC;
3690
0
    }
3691
3692
0
    goto out;
3693
0
  }
3694
3695
0
out:
3696
0
  talloc_free(frame);
3697
0
  return code;
3698
0
}
3699
3700
/*
3701
 * This method is called for S4U2Proxy requests and implements the
3702
 * resource-based constrained delegation variant, which can support
3703
 * cross-realm delegation.
3704
 */
3705
krb5_error_code samba_kdc_check_s4u2proxy_rbcd(
3706
    krb5_context context,
3707
    struct samba_kdc_db_context *kdc_db_ctx,
3708
    krb5_const_principal client_principal,
3709
    krb5_const_principal server_principal,
3710
    struct samba_kdc_entry_pac client,
3711
    struct samba_kdc_entry_pac device,
3712
    struct samba_kdc_entry *proxy_skdc_entry)
3713
0
{
3714
0
  krb5_error_code code;
3715
0
  enum ndr_err_code ndr_err;
3716
0
  char *client_name = NULL;
3717
0
  char *server_name = NULL;
3718
0
  const char *proxy_dn = NULL;
3719
0
  const DATA_BLOB *data = NULL;
3720
0
  const struct auth_user_info_dc *user_info_dc = NULL;
3721
0
  const struct auth_user_info_dc *device_info_dc = NULL;
3722
0
  struct auth_claims auth_claims = {};
3723
0
  struct security_descriptor *rbcd_security_descriptor = NULL;
3724
0
  struct security_token *security_token = NULL;
3725
0
  uint32_t session_info_flags =
3726
0
    AUTH_SESSION_INFO_DEFAULT_GROUPS |
3727
0
    AUTH_SESSION_INFO_DEVICE_DEFAULT_GROUPS |
3728
0
    AUTH_SESSION_INFO_SIMPLE_PRIVILEGES |
3729
0
    AUTH_SESSION_INFO_FORCE_COMPOUNDED_AUTHENTICATION;
3730
  /*
3731
   * Testing shows that although Windows grants SEC_ADS_GENERIC_ALL access
3732
   * in security descriptors it creates for RBCD, its KDC only requires
3733
   * SEC_ADS_CONTROL_ACCESS for the access check to succeed.
3734
   */
3735
0
  uint32_t access_desired = SEC_ADS_CONTROL_ACCESS;
3736
0
  uint32_t access_granted = 0;
3737
0
  NTSTATUS nt_status;
3738
0
  TALLOC_CTX *mem_ctx = NULL;
3739
3740
0
  mem_ctx = talloc_named(kdc_db_ctx,
3741
0
             0,
3742
0
             "samba_kdc_check_s4u2proxy_rbcd");
3743
0
  if (mem_ctx == NULL) {
3744
0
    errno = ENOMEM;
3745
0
    code = errno;
3746
3747
0
    return code;
3748
0
  }
3749
3750
0
  code = samba_kdc_get_user_info_dc(mem_ctx,
3751
0
            context,
3752
0
            kdc_db_ctx,
3753
0
            client,
3754
0
            &user_info_dc,
3755
0
            NULL /* resource_groups_out */);
3756
0
  if (code != 0) {
3757
0
    goto out;
3758
0
  }
3759
3760
0
  code = samba_kdc_get_claims_data(mem_ctx,
3761
0
           context,
3762
0
           kdc_db_ctx,
3763
0
           client,
3764
0
           &auth_claims.user_claims,
3765
0
           NULL); /* _need_regeneration */
3766
0
  if (code) {
3767
0
    goto out;
3768
0
  }
3769
3770
0
  if (samba_kdc_entry_pac_valid_principal(device)) {
3771
0
    code = samba_kdc_get_user_info_dc(mem_ctx,
3772
0
              context,
3773
0
              kdc_db_ctx,
3774
0
              device,
3775
0
              &device_info_dc,
3776
0
              NULL /* resource_groups_out */);
3777
0
    if (code) {
3778
0
      goto out;
3779
0
    }
3780
3781
0
    code = samba_kdc_get_claims_data(mem_ctx,
3782
0
             context,
3783
0
             kdc_db_ctx,
3784
0
             device,
3785
0
             &auth_claims.device_claims,
3786
0
             NULL); /* _need_regeneration */
3787
0
    if (code) {
3788
0
      goto out;
3789
0
    }
3790
0
  }
3791
3792
0
  proxy_dn = ldb_dn_get_linearized(proxy_skdc_entry->msg->dn);
3793
0
  if (proxy_dn == NULL) {
3794
0
    DBG_ERR("ldb_dn_get_linearized failed for proxy_dn!\n");
3795
0
    if (errno == 0) {
3796
0
      errno = ENOMEM;
3797
0
    }
3798
0
    code = errno;
3799
3800
0
    goto out;
3801
0
  }
3802
3803
0
  rbcd_security_descriptor = talloc_zero(mem_ctx,
3804
0
                 struct security_descriptor);
3805
0
  if (rbcd_security_descriptor == NULL) {
3806
0
    errno = ENOMEM;
3807
0
    code = errno;
3808
3809
0
    goto out;
3810
0
  }
3811
3812
0
  code = krb5_unparse_name_flags(context,
3813
0
               client_principal,
3814
0
               KRB5_PRINCIPAL_UNPARSE_DISPLAY,
3815
0
               &client_name);
3816
0
  if (code != 0) {
3817
0
    DBG_ERR("Unable to parse client_principal!\n");
3818
0
    goto out;
3819
0
  }
3820
3821
0
  code = krb5_unparse_name_flags(context,
3822
0
               server_principal,
3823
0
               KRB5_PRINCIPAL_UNPARSE_DISPLAY,
3824
0
               &server_name);
3825
0
  if (code != 0) {
3826
0
    DBG_ERR("Unable to parse server_principal!\n");
3827
0
    goto out;
3828
0
  }
3829
3830
0
  DBG_INFO("Check delegation from client[%s] to server[%s] via "
3831
0
     "proxy[%s]\n",
3832
0
     client_name,
3833
0
     server_name,
3834
0
     proxy_dn);
3835
3836
0
  if (!(user_info_dc->info->user_flags & NETLOGON_GUEST)) {
3837
0
    session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
3838
0
  }
3839
3840
0
  if (device_info_dc != NULL && !(device_info_dc->info->user_flags & NETLOGON_GUEST)) {
3841
0
    session_info_flags |= AUTH_SESSION_INFO_DEVICE_AUTHENTICATED;
3842
0
  }
3843
3844
0
  nt_status = auth_generate_security_token(mem_ctx,
3845
0
             kdc_db_ctx->lp_ctx,
3846
0
             kdc_db_ctx->samdb,
3847
0
             user_info_dc,
3848
0
             device_info_dc,
3849
0
             auth_claims,
3850
0
             session_info_flags,
3851
0
             &security_token);
3852
0
  if (!NT_STATUS_IS_OK(nt_status)) {
3853
0
    code = map_errno_from_nt_status(nt_status);
3854
0
    goto out;
3855
0
  }
3856
3857
0
  data = ldb_msg_find_ldb_val(proxy_skdc_entry->msg,
3858
0
            "msDS-AllowedToActOnBehalfOfOtherIdentity");
3859
0
  if (data == NULL) {
3860
0
    DBG_WARNING("Could not find security descriptor "
3861
0
          "msDS-AllowedToActOnBehalfOfOtherIdentity in "
3862
0
          "proxy[%s]\n",
3863
0
          proxy_dn);
3864
0
    code = KRB5KDC_ERR_BADOPTION;
3865
0
    goto out;
3866
0
  }
3867
3868
0
  ndr_err = ndr_pull_struct_blob(
3869
0
      data,
3870
0
      mem_ctx,
3871
0
      rbcd_security_descriptor,
3872
0
      (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
3873
0
  if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3874
0
    errno = ndr_map_error2errno(ndr_err);
3875
0
    DBG_ERR("Failed to unmarshall "
3876
0
      "msDS-AllowedToActOnBehalfOfOtherIdentity "
3877
0
      "security descriptor of proxy[%s]\n",
3878
0
      proxy_dn);
3879
0
    code = KRB5KDC_ERR_BADOPTION;
3880
0
    goto out;
3881
0
  }
3882
3883
0
  if (DEBUGLEVEL >= 10) {
3884
0
    NDR_PRINT_DEBUG(security_token, security_token);
3885
0
    NDR_PRINT_DEBUG(security_descriptor, rbcd_security_descriptor);
3886
0
  }
3887
3888
0
  nt_status = sec_access_check_ds(rbcd_security_descriptor,
3889
0
          security_token,
3890
0
          access_desired,
3891
0
          &access_granted,
3892
0
          NULL,
3893
0
          NULL);
3894
3895
0
  if (!NT_STATUS_IS_OK(nt_status)) {
3896
0
    DBG_WARNING("RBCD: sec_access_check_ds(access_desired=%#08x, "
3897
0
          "access_granted:%#08x) failed with: %s\n",
3898
0
          access_desired,
3899
0
          access_granted,
3900
0
          nt_errstr(nt_status));
3901
3902
0
    code = KRB5KDC_ERR_BADOPTION;
3903
0
    goto out;
3904
0
  }
3905
3906
0
  DBG_NOTICE("RBCD: Access granted for client[%s]\n", client_name);
3907
3908
0
  code = 0;
3909
0
out:
3910
0
  SAFE_FREE(client_name);
3911
0
  SAFE_FREE(server_name);
3912
3913
0
  TALLOC_FREE(mem_ctx);
3914
0
  return code;
3915
0
}