2 Unix SMB/CIFS implementation.
4 Kerberos utility functions for GENSEC
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "system/kerberos.h"
25 #include "auth/kerberos/kerberos.h"
26 #include "auth/credentials/credentials.h"
27 #include "auth/credentials/credentials_proto.h"
28 #include "auth/credentials/credentials_krb5.h"
29 #include "auth/kerberos/kerberos_credentials.h"
30 #include "auth/kerberos/kerberos_util.h"
32 #include "param/secrets.h"
34 struct principal_container {
35 struct smb_krb5_context *smb_krb5_context;
36 krb5_principal principal;
37 const char *string_form; /* Optional */
40 static krb5_error_code free_principal(struct principal_container *pc)
42 /* current heimdal - 0.6.3, which we need anyway, fixes segfaults here */
43 krb5_free_principal(pc->smb_krb5_context->krb5_context, pc->principal);
49 static krb5_error_code parse_principal(TALLOC_CTX *parent_ctx,
50 const char *princ_string,
51 struct smb_krb5_context *smb_krb5_context,
52 krb5_principal *princ,
53 const char **error_string)
56 struct principal_container *mem_ctx;
57 if (princ_string == NULL) {
62 ret = krb5_parse_name(smb_krb5_context->krb5_context,
66 (*error_string) = smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, parent_ctx);
70 mem_ctx = talloc(parent_ctx, struct principal_container);
72 (*error_string) = error_message(ENOMEM);
76 /* This song-and-dance effectivly puts the principal
77 * into talloc, so we can't loose it. */
78 mem_ctx->smb_krb5_context = talloc_reference(mem_ctx, smb_krb5_context);
79 mem_ctx->principal = *princ;
80 talloc_set_destructor(mem_ctx, free_principal);
84 static krb5_error_code principals_from_msg(TALLOC_CTX *parent_ctx,
85 struct ldb_message *msg,
86 struct smb_krb5_context *smb_krb5_context,
87 struct principal_container ***principals_out,
88 const char **error_string)
93 const char *realm = ldb_msg_find_attr_as_string(msg, "realm", NULL);
94 const char *samAccountName = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
95 struct ldb_message_element *spn_el = ldb_msg_find_element(msg, "servicePrincipalName");
97 struct principal_container **principals;
98 tmp_ctx = talloc_new(parent_ctx);
100 *error_string = "Cannot allocate tmp_ctx";
105 *error_string = "Cannot have a kerberos secret in secrets.ldb without a realm";
109 upper_realm = strupper_talloc(tmp_ctx, realm);
111 talloc_free(tmp_ctx);
112 *error_string = "Cannot allocate full upper case realm";
116 principals = talloc_array(tmp_ctx, struct principal_container *, spn_el ? (spn_el->num_values + 2) : 2);
118 spn_el = ldb_msg_find_element(msg, "servicePrincipalName");
119 for (i=0; spn_el && i < spn_el->num_values; i++) {
120 principals[i] = talloc(principals, struct principal_container);
121 if (!principals[i]) {
122 talloc_free(tmp_ctx);
123 *error_string = "Cannot allocate mem_ctx";
127 principals[i]->smb_krb5_context = talloc_reference(principals[i], smb_krb5_context);
128 principals[i]->string_form = talloc_asprintf(principals[i], "%*.*s@%s",
129 (int)spn_el->values[i].length,
130 (int)spn_el->values[i].length,
131 (const char *)spn_el->values[i].data, upper_realm);
132 if (!principals[i]->string_form) {
133 talloc_free(tmp_ctx);
134 *error_string = "Cannot allocate full samAccountName";
138 ret = krb5_parse_name(smb_krb5_context->krb5_context,
139 principals[i]->string_form, &principals[i]->principal);
142 talloc_free(tmp_ctx);
143 (*error_string) = smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, parent_ctx);
147 /* This song-and-dance effectivly puts the principal
148 * into talloc, so we can't loose it. */
149 talloc_set_destructor(principals[i], free_principal);
152 if (samAccountName) {
153 principals[i] = talloc(principals, struct principal_container);
154 if (!principals[i]) {
155 talloc_free(tmp_ctx);
156 *error_string = "Cannot allocate mem_ctx";
160 principals[i]->smb_krb5_context = talloc_reference(principals[i], smb_krb5_context);
161 principals[i]->string_form = talloc_asprintf(parent_ctx, "%s@%s", samAccountName, upper_realm);
162 if (!principals[i]->string_form) {
163 talloc_free(tmp_ctx);
164 *error_string = "Cannot allocate full samAccountName";
168 ret = krb5_make_principal(smb_krb5_context->krb5_context, &principals[i]->principal, upper_realm, samAccountName,
171 talloc_free(tmp_ctx);
172 (*error_string) = smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, parent_ctx);
176 /* This song-and-dance effectivly puts the principal
177 * into talloc, so we can't loose it. */
178 talloc_set_destructor(principals[i], free_principal);
182 principals[i] = NULL;
183 *principals_out = talloc_steal(parent_ctx, principals);
185 talloc_free(tmp_ctx);
189 static krb5_error_code salt_principal_from_msg(TALLOC_CTX *parent_ctx,
190 struct ldb_message *msg,
191 struct smb_krb5_context *smb_krb5_context,
192 krb5_principal *salt_princ,
193 const char **error_string)
195 const char *salt_principal = ldb_msg_find_attr_as_string(msg, "saltPrincipal", NULL);
196 const char *samAccountName = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
197 const char *realm = ldb_msg_find_attr_as_string(msg, "realm", NULL);
198 if (salt_principal) {
199 return parse_principal(parent_ctx, salt_principal, smb_krb5_context, salt_princ, error_string);
200 } else if (samAccountName) {
202 char *machine_username;
208 struct principal_container *mem_ctx = talloc(parent_ctx, struct principal_container);
210 *error_string = "Cannot allocate mem_ctx";
214 tmp_ctx = talloc_new(mem_ctx);
216 talloc_free(mem_ctx);
217 *error_string = "Cannot allocate tmp_ctx";
222 *error_string = "Cannot have a kerberos secret in secrets.ldb without a realm";
226 machine_username = talloc_strdup(tmp_ctx, samAccountName);
227 if (!machine_username) {
228 talloc_free(mem_ctx);
229 *error_string = "Cannot duplicate samAccountName";
233 if (machine_username[strlen(machine_username)-1] == '$') {
234 machine_username[strlen(machine_username)-1] = '\0';
237 lower_realm = strlower_talloc(tmp_ctx, realm);
239 talloc_free(mem_ctx);
240 *error_string = "Cannot allocate to lower case realm";
244 upper_realm = strupper_talloc(tmp_ctx, realm);
246 talloc_free(mem_ctx);
247 *error_string = "Cannot allocate to upper case realm";
251 salt_body = talloc_asprintf(tmp_ctx, "%s.%s", machine_username,
253 talloc_free(lower_realm);
254 talloc_free(machine_username);
256 talloc_free(mem_ctx);
257 *error_string = "Cannot form salt principal body";
261 ret = krb5_make_principal(smb_krb5_context->krb5_context, salt_princ,
263 "host", salt_body, NULL);
265 /* This song-and-dance effectivly puts the principal
266 * into talloc, so we can't loose it. */
267 mem_ctx->smb_krb5_context = talloc_reference(mem_ctx, smb_krb5_context);
268 mem_ctx->principal = *salt_princ;
269 talloc_set_destructor(mem_ctx, free_principal);
271 (*error_string) = smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, parent_ctx);
273 talloc_free(tmp_ctx);
276 (*error_string) = "Cannot determine salt principal, no saltPrincipal or samAccountName specified";
281 /* Obtain the principal set on this context. Requires a
282 * smb_krb5_context because we are doing krb5 principal parsing with
283 * the library routines. The returned princ is placed in the talloc
284 * system by means of a destructor (do *not* free). */
286 krb5_error_code principal_from_credentials(TALLOC_CTX *parent_ctx,
287 struct cli_credentials *credentials,
288 struct smb_krb5_context *smb_krb5_context,
289 krb5_principal *princ,
290 enum credentials_obtained *obtained,
291 const char **error_string)
294 const char *princ_string;
295 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
297 (*error_string) = error_message(ENOMEM);
300 princ_string = cli_credentials_get_principal_and_obtained(credentials, mem_ctx, obtained);
302 (*error_string) = error_message(ENOMEM);
306 ret = parse_principal(parent_ctx, princ_string,
307 smb_krb5_context, princ, error_string);
308 talloc_free(mem_ctx);
312 /* Obtain the principal set on this context. Requires a
313 * smb_krb5_context because we are doing krb5 principal parsing with
314 * the library routines. The returned princ is placed in the talloc
315 * system by means of a destructor (do *not* free). */
317 krb5_error_code impersonate_principal_from_credentials(TALLOC_CTX *parent_ctx,
318 struct cli_credentials *credentials,
319 struct smb_krb5_context *smb_krb5_context,
320 krb5_principal *princ,
321 const char **error_string)
323 return parse_principal(parent_ctx, cli_credentials_get_impersonate_principal(credentials),
324 smb_krb5_context, princ, error_string);
328 * Return a freshly allocated ccache (destroyed by destructor on child
329 * of parent_ctx), for a given set of client credentials
332 krb5_error_code kinit_to_ccache(TALLOC_CTX *parent_ctx,
333 struct cli_credentials *credentials,
334 struct smb_krb5_context *smb_krb5_context,
336 enum credentials_obtained *obtained,
337 const char **error_string)
340 const char *password, *target_service;
342 krb5_principal princ;
343 krb5_principal impersonate_principal;
345 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
346 krb5_get_init_creds_opt *krb_options;
349 (*error_string) = strerror(ENOMEM);
353 ret = principal_from_credentials(mem_ctx, credentials, smb_krb5_context, &princ, obtained, error_string);
355 talloc_free(mem_ctx);
359 ret = impersonate_principal_from_credentials(mem_ctx, credentials, smb_krb5_context, &impersonate_principal, error_string);
361 talloc_free(mem_ctx);
365 target_service = cli_credentials_get_target_service(credentials);
367 password = cli_credentials_get_password(credentials);
369 /* setup the krb5 options we want */
370 if ((ret = krb5_get_init_creds_opt_alloc(smb_krb5_context->krb5_context, &krb_options))) {
371 (*error_string) = talloc_asprintf(credentials, "krb5_get_init_creds_opt_alloc failed (%s)\n",
372 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
374 talloc_free(mem_ctx);
378 /* get the defaults */
379 krb5_get_init_creds_opt_set_default_flags(smb_krb5_context->krb5_context, NULL, NULL, krb_options);
381 /* set if we want a forwardable ticket */
382 switch (cli_credentials_get_krb_forwardable(credentials)) {
383 case CRED_AUTO_KRB_FORWARDABLE:
385 case CRED_NO_KRB_FORWARDABLE:
386 krb5_get_init_creds_opt_set_forwardable(krb_options, FALSE);
388 case CRED_FORCE_KRB_FORWARDABLE:
389 krb5_get_init_creds_opt_set_forwardable(krb_options, TRUE);
396 ret = kerberos_kinit_password_cc(smb_krb5_context->krb5_context, ccache,
398 impersonate_principal, target_service,
401 } else if (impersonate_principal) {
402 (*error_string) = "INTERNAL error: Cannot impersonate principal with just a keyblock. A password must be specified in the credentials";
405 /* No password available, try to use a keyblock instead */
407 krb5_keyblock keyblock;
408 const struct samr_Password *mach_pwd;
409 mach_pwd = cli_credentials_get_nt_hash(credentials, mem_ctx);
411 talloc_free(mem_ctx);
412 (*error_string) = "kinit_to_ccache: No password available for kinit\n";
413 krb5_get_init_creds_opt_free(smb_krb5_context->krb5_context, krb_options);
416 ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
417 ENCTYPE_ARCFOUR_HMAC,
418 mach_pwd->hash, sizeof(mach_pwd->hash),
422 ret = kerberos_kinit_keyblock_cc(smb_krb5_context->krb5_context, ccache,
424 target_service, krb_options,
426 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &keyblock);
430 if (ret == KRB5KRB_AP_ERR_SKEW || ret == KRB5_KDCREP_SKEW) {
431 /* Perhaps we have been given an invalid skew, so try again without it */
432 time_t t = time(NULL);
433 krb5_set_real_time(smb_krb5_context->krb5_context, t, 0);
435 /* not a skew problem */
440 krb5_get_init_creds_opt_free(smb_krb5_context->krb5_context, krb_options);
442 if (ret == KRB5KRB_AP_ERR_SKEW || ret == KRB5_KDCREP_SKEW) {
443 (*error_string) = talloc_asprintf(credentials, "kinit for %s failed (%s)\n",
444 cli_credentials_get_principal(credentials, mem_ctx),
445 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
447 talloc_free(mem_ctx);
451 /* cope with ticket being in the future due to clock skew */
452 if ((unsigned)kdc_time > time(NULL)) {
453 time_t t = time(NULL);
454 int time_offset =(unsigned)kdc_time-t;
455 DEBUG(4,("Advancing clock by %d seconds to cope with clock skew\n", time_offset));
456 krb5_set_real_time(smb_krb5_context->krb5_context, t + time_offset + 1, 0);
459 if (ret == KRB5KDC_ERR_PREAUTH_FAILED && cli_credentials_wrong_password(credentials)) {
460 ret = kinit_to_ccache(parent_ctx,
468 (*error_string) = talloc_asprintf(credentials, "kinit for %s failed (%s)\n",
469 cli_credentials_get_principal(credentials, mem_ctx),
470 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
472 talloc_free(mem_ctx);
475 talloc_free(mem_ctx);
479 static krb5_error_code free_keytab(struct keytab_container *ktc)
481 return krb5_kt_close(ktc->smb_krb5_context->krb5_context, ktc->keytab);
484 krb5_error_code smb_krb5_open_keytab(TALLOC_CTX *mem_ctx,
485 struct smb_krb5_context *smb_krb5_context,
486 const char *keytab_name, struct keytab_container **ktc)
490 ret = krb5_kt_resolve(smb_krb5_context->krb5_context, keytab_name, &keytab);
492 DEBUG(1,("failed to open krb5 keytab: %s\n",
493 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
498 *ktc = talloc(mem_ctx, struct keytab_container);
503 (*ktc)->smb_krb5_context = talloc_reference(*ktc, smb_krb5_context);
504 (*ktc)->keytab = keytab;
505 talloc_set_destructor(*ktc, free_keytab);
510 static krb5_error_code keytab_add_keys(TALLOC_CTX *parent_ctx,
511 struct principal_container **principals,
512 krb5_principal salt_princ,
514 const char *password_s,
515 struct smb_krb5_context *smb_krb5_context,
516 krb5_enctype *enctypes,
518 const char **error_string)
524 password.data = discard_const_p(char *, password_s);
525 password.length = strlen(password_s);
527 for (i=0; enctypes[i]; i++) {
528 krb5_keytab_entry entry;
532 ret = create_kerberos_key_from_string(smb_krb5_context->krb5_context,
533 salt_princ, &password, &entry.keyblock, enctypes[i]);
540 for (p=0; principals[p]; p++) {
541 entry.principal = principals[p]->principal;
542 ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
544 char *k5_error_string = smb_get_krb5_error_message(smb_krb5_context->krb5_context,
546 *error_string = talloc_asprintf(parent_ctx, "Failed to add enctype %d entry for %s(kvno %d) to keytab: %s\n",
548 principals[p]->string_form,
551 talloc_free(k5_error_string);
552 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
556 DEBUG(5, ("Added %s(kvno %d) to keytab (enctype %d)\n",
557 principals[p]->string_form, kvno,
560 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
565 static krb5_error_code create_keytab(TALLOC_CTX *parent_ctx,
566 struct ldb_message *msg,
567 struct principal_container **principals,
568 struct smb_krb5_context *smb_krb5_context,
571 const char **error_string)
574 const char *password_s;
575 const char *old_secret;
577 uint32_t enctype_bitmap;
578 krb5_principal salt_princ;
579 krb5_enctype *enctypes;
580 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
582 *error_string = "unable to allocate tmp_ctx for create_keytab";
586 /* The salt used to generate these entries may be different however, fetch that */
587 ret = salt_principal_from_msg(mem_ctx, msg,
589 &salt_princ, error_string);
591 talloc_free(mem_ctx);
595 kvno = ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0);
597 /* Finally, do the dance to get the password to put in the entry */
598 password_s = ldb_msg_find_attr_as_string(msg, "secret", NULL);
601 /* There is no password here, so nothing to do */
602 talloc_free(mem_ctx);
606 if (add_old && kvno != 0) {
607 old_secret = ldb_msg_find_attr_as_string(msg, "priorSecret", NULL);
612 enctype_bitmap = (uint32_t)ldb_msg_find_attr_as_int(msg, "msDS-SupportedEncryptionTypes", ENC_ALL_TYPES);
614 ret = kerberos_enctype_bitmap_to_enctypes(mem_ctx, enctype_bitmap, &enctypes);
616 *error_string = talloc_asprintf(parent_ctx, "create_keytab: generating list of encryption types failed (%s)\n",
617 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
619 talloc_free(mem_ctx);
623 ret = keytab_add_keys(mem_ctx, principals,
625 kvno, password_s, smb_krb5_context,
626 enctypes, keytab, error_string);
628 talloc_free(mem_ctx);
633 ret = keytab_add_keys(mem_ctx, principals,
635 kvno - 1, old_secret, smb_krb5_context,
636 enctypes, keytab, error_string);
638 talloc_free(mem_ctx);
643 talloc_free(mem_ctx);
648 * Walk the keytab, looking for entries of this principal name, with KVNO other than current kvno -1.
650 * These entries are now stale, we only keep the current, and previous entries around.
652 * Inspired by the code in Samba3 for 'use kerberos keytab'.
656 static krb5_error_code remove_old_entries(TALLOC_CTX *parent_ctx,
657 struct ldb_message *msg,
658 struct principal_container **principals,
659 bool delete_all_kvno,
660 struct smb_krb5_context *smb_krb5_context,
661 krb5_keytab keytab, bool *found_previous,
662 const char **error_string)
664 krb5_error_code ret, ret2;
665 krb5_kt_cursor cursor;
667 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
673 *found_previous = false;
675 kvno = ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0);
677 /* for each entry in the keytab */
678 ret = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
682 case HEIM_ERR_OPNOTSUPP:
685 /* no point enumerating if there isn't anything here */
686 talloc_free(mem_ctx);
689 *error_string = talloc_asprintf(parent_ctx, "failed to open keytab for read of old entries: %s\n",
690 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
692 talloc_free(mem_ctx);
698 bool matched = false;
699 krb5_keytab_entry entry;
700 ret = krb5_kt_next_entry(smb_krb5_context->krb5_context, keytab, &entry, &cursor);
704 for (i = 0; principals[i]; i++) {
705 /* if it matches our principal */
706 if (krb5_kt_compare(smb_krb5_context->krb5_context, &entry, principals[i]->principal, 0, 0)) {
713 /* Free the entry, it wasn't the one we were looking for anyway */
714 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
718 /* delete it, if it is not kvno -1 */
719 if (entry.vno != (kvno - 1 )) {
720 /* Release the enumeration. We are going to
721 * have to start this from the top again,
722 * because deletes during enumeration may not
723 * always be consistant.
725 * Also, the enumeration locks a FILE: keytab
728 krb5_kt_end_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
730 ret = krb5_kt_remove_entry(smb_krb5_context->krb5_context, keytab, &entry);
731 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
733 /* Deleted: Restart from the top */
734 ret2 = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
736 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
737 DEBUG(1,("failed to restart enumeration of keytab: %s\n",
738 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
741 talloc_free(mem_ctx);
750 *found_previous = true;
753 /* Free the entry, we don't need it any more */
754 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
758 krb5_kt_end_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
768 *error_string = talloc_asprintf(parent_ctx, "failed in deleting old entries for principal: %s\n",
769 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
772 talloc_free(mem_ctx);
776 krb5_error_code smb_krb5_update_keytab(TALLOC_CTX *parent_ctx,
777 struct smb_krb5_context *smb_krb5_context,
778 struct ldb_context *ldb,
779 struct ldb_message *msg,
780 bool delete_all_kvno,
781 const char **error_string)
785 TALLOC_CTX *mem_ctx = talloc_new(NULL);
786 struct keytab_container *keytab_container;
787 struct principal_container **principals;
788 const char *keytab_name;
794 keytab_name = keytab_name_from_msg(mem_ctx, ldb, msg);
799 ret = smb_krb5_open_keytab(mem_ctx, smb_krb5_context, keytab_name, &keytab_container);
802 talloc_free(mem_ctx);
806 DEBUG(5, ("Opened keytab %s\n", keytab_name));
808 /* Get the principal we will store the new keytab entries under */
809 ret = principals_from_msg(mem_ctx, msg, smb_krb5_context, &principals, error_string);
812 *error_string = talloc_asprintf(parent_ctx, "Failed to load principals from ldb message: %s\n", *error_string);
813 talloc_free(mem_ctx);
817 ret = remove_old_entries(mem_ctx, msg, principals, delete_all_kvno,
818 smb_krb5_context, keytab_container->keytab, &found_previous, error_string);
820 *error_string = talloc_asprintf(parent_ctx, "Failed to remove old principals from keytab: %s\n", *error_string);
821 talloc_free(mem_ctx);
825 if (!delete_all_kvno) {
826 /* Create a new keytab. If during the cleanout we found
827 * entires for kvno -1, then don't try and duplicate them.
828 * Otherwise, add kvno, and kvno -1 */
830 ret = create_keytab(mem_ctx, msg, principals,
832 keytab_container->keytab,
833 found_previous ? false : true, error_string);
835 talloc_free(mem_ctx);
839 krb5_error_code smb_krb5_create_memory_keytab(TALLOC_CTX *parent_ctx,
840 struct cli_credentials *machine_account,
841 struct smb_krb5_context *smb_krb5_context,
842 struct keytab_container **keytab_container)
845 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
846 const char *rand_string;
847 const char *keytab_name;
848 struct ldb_message *msg;
849 const char *error_string;
854 *keytab_container = talloc(mem_ctx, struct keytab_container);
856 rand_string = generate_random_str(mem_ctx, 16);
858 talloc_free(mem_ctx);
862 keytab_name = talloc_asprintf(mem_ctx, "MEMORY:%s",
865 talloc_free(mem_ctx);
869 ret = smb_krb5_open_keytab(mem_ctx, smb_krb5_context, keytab_name, keytab_container);
874 msg = ldb_msg_new(mem_ctx);
876 talloc_free(mem_ctx);
879 ldb_msg_add_string(msg, "krb5Keytab", keytab_name);
880 ldb_msg_add_string(msg, "secret", cli_credentials_get_password(machine_account));
881 ldb_msg_add_string(msg, "samAccountName", cli_credentials_get_username(machine_account));
882 ldb_msg_add_string(msg, "realm", cli_credentials_get_realm(machine_account));
883 ldb_msg_add_fmt(msg, "msDS-KeyVersionNumber", "%d", (int)cli_credentials_get_kvno(machine_account));
885 ret = smb_krb5_update_keytab(mem_ctx, smb_krb5_context, NULL, msg, false, &error_string);
887 talloc_steal(parent_ctx, *keytab_container);
889 DEBUG(0, ("Failed to create in-memory keytab: %s\n", error_string));
890 *keytab_container = NULL;
892 talloc_free(mem_ctx);
895 /* Translate between the IETF encryption type values and the Microsoft msDS-SupportedEncryptionTypes values */
896 uint32_t kerberos_enctype_to_bitmap(krb5_enctype enc_type_enum)
898 switch (enc_type_enum) {
899 case ENCTYPE_DES_CBC_CRC:
901 case ENCTYPE_DES_CBC_MD5:
903 case ENCTYPE_ARCFOUR_HMAC_MD5:
904 return ENC_RC4_HMAC_MD5;
905 case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
906 return ENC_HMAC_SHA1_96_AES128;
907 case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
908 return ENC_HMAC_SHA1_96_AES256;
914 /* Translate between the Microsoft msDS-SupportedEncryptionTypes values and the IETF encryption type values */
915 krb5_enctype kerberos_enctype_bitmap_to_enctype(uint32_t enctype_bitmap)
917 switch (enctype_bitmap) {
919 return ENCTYPE_DES_CBC_CRC;
921 return ENCTYPE_DES_CBC_MD5;
922 case ENC_RC4_HMAC_MD5:
923 return ENCTYPE_ARCFOUR_HMAC_MD5;
924 case ENC_HMAC_SHA1_96_AES128:
925 return ENCTYPE_AES128_CTS_HMAC_SHA1_96;
926 case ENC_HMAC_SHA1_96_AES256:
927 return ENCTYPE_AES256_CTS_HMAC_SHA1_96;
933 /* Return an array of krb5_enctype values */
934 krb5_error_code kerberos_enctype_bitmap_to_enctypes(TALLOC_CTX *mem_ctx, uint32_t enctype_bitmap, krb5_enctype **enctypes)
936 unsigned int i, j = 0;
937 *enctypes = talloc_zero_array(mem_ctx, krb5_enctype, (8*sizeof(enctype_bitmap))+1);
941 for (i=0; i<(8*sizeof(enctype_bitmap)); i++) {
942 uint32_t bit_value = (1 << i) & enctype_bitmap;
943 if (bit_value & enctype_bitmap) {
944 (*enctypes)[j] = kerberos_enctype_bitmap_to_enctype(bit_value);
945 if (!(*enctypes)[j]) {