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"
31 #include "param/secrets.h"
33 struct principal_container {
34 struct smb_krb5_context *smb_krb5_context;
35 krb5_principal principal;
36 const char *string_form; /* Optional */
39 static krb5_error_code free_principal(struct principal_container *pc)
41 /* current heimdal - 0.6.3, which we need anyway, fixes segfaults here */
42 krb5_free_principal(pc->smb_krb5_context->krb5_context, pc->principal);
48 static krb5_error_code parse_principal(TALLOC_CTX *parent_ctx,
49 const char *princ_string,
50 struct smb_krb5_context *smb_krb5_context,
51 krb5_principal *princ,
52 const char **error_string)
55 struct principal_container *mem_ctx;
56 if (princ_string == NULL) {
61 ret = krb5_parse_name(smb_krb5_context->krb5_context,
65 (*error_string) = smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, parent_ctx);
69 mem_ctx = talloc(parent_ctx, struct principal_container);
71 (*error_string) = error_message(ENOMEM);
75 /* This song-and-dance effectivly puts the principal
76 * into talloc, so we can't loose it. */
77 mem_ctx->smb_krb5_context = talloc_reference(mem_ctx, smb_krb5_context);
78 mem_ctx->principal = *princ;
79 talloc_set_destructor(mem_ctx, free_principal);
83 static krb5_error_code principals_from_msg(TALLOC_CTX *parent_ctx,
84 struct ldb_message *msg,
85 struct smb_krb5_context *smb_krb5_context,
86 struct principal_container ***principals_out,
87 const char **error_string)
92 const char *realm = ldb_msg_find_attr_as_string(msg, "realm", NULL);
93 const char *samAccountName = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
94 struct ldb_message_element *spn_el = ldb_msg_find_element(msg, "servicePrincipalName");
96 struct principal_container **principals;
97 tmp_ctx = talloc_new(parent_ctx);
99 *error_string = "Cannot allocate tmp_ctx";
104 *error_string = "Cannot have a kerberos secret in secrets.ldb without a realm";
108 upper_realm = strupper_talloc(tmp_ctx, realm);
110 talloc_free(tmp_ctx);
111 *error_string = "Cannot allocate full upper case realm";
115 principals = talloc_array(tmp_ctx, struct principal_container *, spn_el ? (spn_el->num_values + 2) : 2);
117 spn_el = ldb_msg_find_element(msg, "servicePrincipalName");
118 for (i=0; spn_el && i < spn_el->num_values; i++) {
119 principals[i] = talloc(principals, struct principal_container);
120 if (!principals[i]) {
121 talloc_free(tmp_ctx);
122 *error_string = "Cannot allocate mem_ctx";
126 principals[i]->smb_krb5_context = talloc_reference(principals[i], smb_krb5_context);
127 principals[i]->string_form = talloc_asprintf(principals[i], "%*.*s@%s",
128 (int)spn_el->values[i].length,
129 (int)spn_el->values[i].length,
130 (const char *)spn_el->values[i].data, upper_realm);
131 if (!principals[i]->string_form) {
132 talloc_free(tmp_ctx);
133 *error_string = "Cannot allocate full samAccountName";
137 ret = krb5_parse_name(smb_krb5_context->krb5_context,
138 principals[i]->string_form, &principals[i]->principal);
141 talloc_free(tmp_ctx);
142 (*error_string) = smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, parent_ctx);
146 /* This song-and-dance effectivly puts the principal
147 * into talloc, so we can't loose it. */
148 talloc_set_destructor(principals[i], free_principal);
151 if (samAccountName) {
152 principals[i] = talloc(principals, struct principal_container);
153 if (!principals[i]) {
154 talloc_free(tmp_ctx);
155 *error_string = "Cannot allocate mem_ctx";
159 principals[i]->smb_krb5_context = talloc_reference(principals[i], smb_krb5_context);
160 principals[i]->string_form = talloc_asprintf(parent_ctx, "%s@%s", samAccountName, upper_realm);
161 if (!principals[i]->string_form) {
162 talloc_free(tmp_ctx);
163 *error_string = "Cannot allocate full samAccountName";
167 ret = krb5_make_principal(smb_krb5_context->krb5_context, &principals[i]->principal, upper_realm, samAccountName,
170 talloc_free(tmp_ctx);
171 (*error_string) = smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, parent_ctx);
175 /* This song-and-dance effectivly puts the principal
176 * into talloc, so we can't loose it. */
177 talloc_set_destructor(principals[i], free_principal);
181 principals[i] = NULL;
182 *principals_out = talloc_steal(parent_ctx, principals);
184 talloc_free(tmp_ctx);
188 static krb5_error_code salt_principal_from_msg(TALLOC_CTX *parent_ctx,
189 struct ldb_message *msg,
190 struct smb_krb5_context *smb_krb5_context,
191 krb5_principal *salt_princ,
192 const char **error_string)
194 const char *salt_principal = ldb_msg_find_attr_as_string(msg, "saltPrincipal", NULL);
195 const char *samAccountName = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
196 const char *realm = ldb_msg_find_attr_as_string(msg, "realm", NULL);
197 if (salt_principal) {
198 return parse_principal(parent_ctx, salt_principal, smb_krb5_context, salt_princ, error_string);
199 } else if (samAccountName) {
201 char *machine_username;
207 struct principal_container *mem_ctx = talloc(parent_ctx, struct principal_container);
209 *error_string = "Cannot allocate mem_ctx";
213 tmp_ctx = talloc_new(mem_ctx);
215 talloc_free(mem_ctx);
216 *error_string = "Cannot allocate tmp_ctx";
221 *error_string = "Cannot have a kerberos secret in secrets.ldb without a realm";
225 machine_username = talloc_strdup(tmp_ctx, samAccountName);
226 if (!machine_username) {
227 talloc_free(mem_ctx);
228 *error_string = "Cannot duplicate samAccountName";
232 if (machine_username[strlen(machine_username)-1] == '$') {
233 machine_username[strlen(machine_username)-1] = '\0';
236 lower_realm = strlower_talloc(tmp_ctx, realm);
238 talloc_free(mem_ctx);
239 *error_string = "Cannot allocate to lower case realm";
243 upper_realm = strupper_talloc(tmp_ctx, realm);
245 talloc_free(mem_ctx);
246 *error_string = "Cannot allocate to upper case realm";
250 salt_body = talloc_asprintf(tmp_ctx, "%s.%s", machine_username,
252 talloc_free(lower_realm);
253 talloc_free(machine_username);
255 talloc_free(mem_ctx);
256 *error_string = "Cannot form salt principal body";
260 ret = krb5_make_principal(smb_krb5_context->krb5_context, salt_princ,
262 "host", salt_body, NULL);
264 /* This song-and-dance effectivly puts the principal
265 * into talloc, so we can't loose it. */
266 mem_ctx->smb_krb5_context = talloc_reference(mem_ctx, smb_krb5_context);
267 mem_ctx->principal = *salt_princ;
268 talloc_set_destructor(mem_ctx, free_principal);
270 (*error_string) = smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, parent_ctx);
272 talloc_free(tmp_ctx);
275 (*error_string) = "Cannot determine salt principal, no saltPrincipal or samAccountName specified";
280 /* Obtain the principal set on this context. Requires a
281 * smb_krb5_context because we are doing krb5 principal parsing with
282 * the library routines. The returned princ is placed in the talloc
283 * system by means of a destructor (do *not* free). */
285 krb5_error_code principal_from_credentials(TALLOC_CTX *parent_ctx,
286 struct cli_credentials *credentials,
287 struct smb_krb5_context *smb_krb5_context,
288 krb5_principal *princ,
289 enum credentials_obtained *obtained,
290 const char **error_string)
293 const char *princ_string;
294 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
296 (*error_string) = error_message(ENOMEM);
299 princ_string = cli_credentials_get_principal_and_obtained(credentials, mem_ctx, obtained);
301 (*error_string) = error_message(ENOMEM);
305 ret = parse_principal(parent_ctx, princ_string,
306 smb_krb5_context, princ, error_string);
307 talloc_free(mem_ctx);
311 /* Obtain the principal set on this context. Requires a
312 * smb_krb5_context because we are doing krb5 principal parsing with
313 * the library routines. The returned princ is placed in the talloc
314 * system by means of a destructor (do *not* free). */
316 krb5_error_code impersonate_principal_from_credentials(TALLOC_CTX *parent_ctx,
317 struct cli_credentials *credentials,
318 struct smb_krb5_context *smb_krb5_context,
319 krb5_principal *princ,
320 const char **error_string)
322 return parse_principal(parent_ctx, cli_credentials_get_impersonate_principal(credentials),
323 smb_krb5_context, princ, error_string);
327 * Return a freshly allocated ccache (destroyed by destructor on child
328 * of parent_ctx), for a given set of client credentials
331 krb5_error_code kinit_to_ccache(TALLOC_CTX *parent_ctx,
332 struct cli_credentials *credentials,
333 struct smb_krb5_context *smb_krb5_context,
335 enum credentials_obtained *obtained,
336 const char **error_string)
339 const char *password, *target_service;
341 krb5_principal princ;
342 krb5_principal impersonate_principal;
344 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
345 krb5_get_init_creds_opt *krb_options;
348 (*error_string) = strerror(ENOMEM);
352 ret = principal_from_credentials(mem_ctx, credentials, smb_krb5_context, &princ, obtained, error_string);
354 talloc_free(mem_ctx);
358 ret = impersonate_principal_from_credentials(mem_ctx, credentials, smb_krb5_context, &impersonate_principal, error_string);
360 talloc_free(mem_ctx);
364 target_service = cli_credentials_get_target_service(credentials);
366 password = cli_credentials_get_password(credentials);
368 /* setup the krb5 options we want */
369 if ((ret = krb5_get_init_creds_opt_alloc(smb_krb5_context->krb5_context, &krb_options))) {
370 (*error_string) = talloc_asprintf(credentials, "krb5_get_init_creds_opt_alloc failed (%s)\n",
371 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
373 talloc_free(mem_ctx);
377 /* get the defaults */
378 krb5_get_init_creds_opt_set_default_flags(smb_krb5_context->krb5_context, NULL, NULL, krb_options);
380 /* set if we want a forwardable ticket */
381 switch (cli_credentials_get_krb_forwardable(credentials)) {
382 case CRED_AUTO_KRB_FORWARDABLE:
384 case CRED_NO_KRB_FORWARDABLE:
385 krb5_get_init_creds_opt_set_forwardable(krb_options, FALSE);
387 case CRED_FORCE_KRB_FORWARDABLE:
388 krb5_get_init_creds_opt_set_forwardable(krb_options, TRUE);
395 ret = kerberos_kinit_password_cc(smb_krb5_context->krb5_context, ccache,
397 impersonate_principal, target_service,
400 } else if (impersonate_principal) {
401 (*error_string) = "INTERNAL error: Cannot impersonate principal with just a keyblock. A password must be specified in the credentials";
404 /* No password available, try to use a keyblock instead */
406 krb5_keyblock keyblock;
407 const struct samr_Password *mach_pwd;
408 mach_pwd = cli_credentials_get_nt_hash(credentials, mem_ctx);
410 talloc_free(mem_ctx);
411 (*error_string) = "kinit_to_ccache: No password available for kinit\n";
412 krb5_get_init_creds_opt_free(smb_krb5_context->krb5_context, krb_options);
415 ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
416 ENCTYPE_ARCFOUR_HMAC,
417 mach_pwd->hash, sizeof(mach_pwd->hash),
421 ret = kerberos_kinit_keyblock_cc(smb_krb5_context->krb5_context, ccache,
423 target_service, krb_options,
425 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &keyblock);
429 if (ret == KRB5KRB_AP_ERR_SKEW || ret == KRB5_KDCREP_SKEW) {
430 /* Perhaps we have been given an invalid skew, so try again without it */
431 time_t t = time(NULL);
432 krb5_set_real_time(smb_krb5_context->krb5_context, t, 0);
434 /* not a skew problem */
439 krb5_get_init_creds_opt_free(smb_krb5_context->krb5_context, krb_options);
441 if (ret == KRB5KRB_AP_ERR_SKEW || ret == KRB5_KDCREP_SKEW) {
442 (*error_string) = talloc_asprintf(credentials, "kinit for %s failed (%s)\n",
443 cli_credentials_get_principal(credentials, mem_ctx),
444 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
446 talloc_free(mem_ctx);
450 /* cope with ticket being in the future due to clock skew */
451 if ((unsigned)kdc_time > time(NULL)) {
452 time_t t = time(NULL);
453 int time_offset =(unsigned)kdc_time-t;
454 DEBUG(4,("Advancing clock by %d seconds to cope with clock skew\n", time_offset));
455 krb5_set_real_time(smb_krb5_context->krb5_context, t + time_offset + 1, 0);
458 if (ret == KRB5KDC_ERR_PREAUTH_FAILED && cli_credentials_wrong_password(credentials)) {
459 ret = kinit_to_ccache(parent_ctx,
467 (*error_string) = talloc_asprintf(credentials, "kinit for %s failed (%s)\n",
468 cli_credentials_get_principal(credentials, mem_ctx),
469 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
471 talloc_free(mem_ctx);
474 talloc_free(mem_ctx);
478 static krb5_error_code free_keytab(struct keytab_container *ktc)
480 return krb5_kt_close(ktc->smb_krb5_context->krb5_context, ktc->keytab);
483 krb5_error_code smb_krb5_open_keytab(TALLOC_CTX *mem_ctx,
484 struct smb_krb5_context *smb_krb5_context,
485 const char *keytab_name, struct keytab_container **ktc)
489 ret = krb5_kt_resolve(smb_krb5_context->krb5_context, keytab_name, &keytab);
491 DEBUG(1,("failed to open krb5 keytab: %s\n",
492 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
497 *ktc = talloc(mem_ctx, struct keytab_container);
502 (*ktc)->smb_krb5_context = talloc_reference(*ktc, smb_krb5_context);
503 (*ktc)->keytab = keytab;
504 talloc_set_destructor(*ktc, free_keytab);
509 static krb5_error_code keytab_add_keys(TALLOC_CTX *parent_ctx,
510 const char *princ_string,
511 krb5_principal princ,
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)
523 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
528 password.data = discard_const_p(char *, password_s);
529 password.length = strlen(password_s);
531 for (i=0; enctypes[i]; i++) {
532 krb5_keytab_entry entry;
533 ret = create_kerberos_key_from_string(smb_krb5_context->krb5_context,
534 salt_princ, &password, &entry.keyblock, enctypes[i]);
536 talloc_free(mem_ctx);
540 entry.principal = princ;
542 ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
544 *error_string = talloc_asprintf(parent_ctx, "Failed to add enctype %d entry for %s(kvno %d) to keytab: %s\n",
548 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
550 talloc_free(mem_ctx);
551 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
555 DEBUG(5, ("Added %s(kvno %d) to keytab (enctype %d)\n",
559 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
561 talloc_free(mem_ctx);
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)
575 const char *password_s;
576 const char *old_secret;
578 uint32_t enctype_bitmap;
579 krb5_principal salt_princ;
580 krb5_enctype *enctypes;
581 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
583 *error_string = "unable to allocate tmp_ctx for create_keytab";
587 /* The salt used to generate these entries may be different however, fetch that */
588 ret = salt_principal_from_msg(mem_ctx, msg,
590 &salt_princ, error_string);
592 talloc_free(mem_ctx);
596 kvno = ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0);
598 /* Finally, do the dance to get the password to put in the entry */
599 password_s = ldb_msg_find_attr_as_string(msg, "secret", NULL);
600 if (add_old && kvno != 0) {
601 old_secret = ldb_msg_find_attr_as_string(msg, "priorSecret", NULL);
606 enctype_bitmap = (uint32_t)ldb_msg_find_attr_as_int(msg, "msDS-SupportedEncryptionTypes", ENC_ALL_TYPES);
608 ret = kerberos_enctype_bitmap_to_enctypes(mem_ctx, enctype_bitmap, &enctypes);
610 *error_string = talloc_asprintf(parent_ctx, "create_keytab: generating list of encryption types failed (%s)\n",
611 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
613 talloc_free(mem_ctx);
617 /* Walk over the principals */
618 for (i=0; principals[i]; i++) {
619 ret = keytab_add_keys(mem_ctx, principals[i]->string_form, principals[i]->principal,
621 kvno, password_s, smb_krb5_context,
622 enctypes, keytab, error_string);
624 talloc_free(mem_ctx);
629 ret = keytab_add_keys(mem_ctx, principals[i]->string_form, principals[i]->principal,
631 kvno - 1, old_secret, smb_krb5_context,
632 enctypes, keytab, error_string);
634 talloc_free(mem_ctx);
640 talloc_free(mem_ctx);
645 * Walk the keytab, looking for entries of this principal name, with KVNO other than current kvno -1.
647 * These entries are now stale, we only keep the current, and previous entries around.
649 * Inspired by the code in Samba3 for 'use kerberos keytab'.
653 static krb5_error_code remove_old_entries(TALLOC_CTX *parent_ctx,
654 struct ldb_message *msg,
655 struct principal_container **principals,
656 bool delete_all_kvno,
657 struct smb_krb5_context *smb_krb5_context,
658 krb5_keytab keytab, bool *found_previous,
659 const char **error_string)
661 krb5_error_code ret, ret2;
662 krb5_kt_cursor cursor;
664 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
670 *found_previous = false;
672 kvno = ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0);
674 /* for each entry in the keytab */
675 ret = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
679 case HEIM_ERR_OPNOTSUPP:
682 /* no point enumerating if there isn't anything here */
683 talloc_free(mem_ctx);
686 *error_string = talloc_asprintf(parent_ctx, "failed to open keytab for read of old entries: %s\n",
687 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
689 talloc_free(mem_ctx);
695 bool matched = false;
696 krb5_keytab_entry entry;
697 ret = krb5_kt_next_entry(smb_krb5_context->krb5_context, keytab, &entry, &cursor);
701 for (i = 0; principals[i]; i++) {
702 /* if it matches our principal */
703 if (krb5_kt_compare(smb_krb5_context->krb5_context, &entry, principals[i]->principal, 0, 0)) {
710 /* Free the entry, it wasn't the one we were looking for anyway */
711 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
715 /* delete it, if it is not kvno -1 */
716 if (entry.vno != (kvno - 1 )) {
717 /* Release the enumeration. We are going to
718 * have to start this from the top again,
719 * because deletes during enumeration may not
720 * always be consistant.
722 * Also, the enumeration locks a FILE: keytab
725 krb5_kt_end_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
727 ret = krb5_kt_remove_entry(smb_krb5_context->krb5_context, keytab, &entry);
728 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
730 /* Deleted: Restart from the top */
731 ret2 = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
733 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
734 DEBUG(1,("failed to restart enumeration of keytab: %s\n",
735 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
738 talloc_free(mem_ctx);
747 *found_previous = true;
750 /* Free the entry, we don't need it any more */
751 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
755 krb5_kt_end_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
765 *error_string = talloc_asprintf(parent_ctx, "failed in deleting old entries for principal: %s\n",
766 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
769 talloc_free(mem_ctx);
773 krb5_error_code smb_krb5_update_keytab(TALLOC_CTX *parent_ctx,
774 struct smb_krb5_context *smb_krb5_context,
775 struct ldb_context *ldb,
776 struct ldb_message *msg,
777 bool delete_all_kvno,
778 const char **error_string)
782 TALLOC_CTX *mem_ctx = talloc_new(NULL);
783 struct keytab_container *keytab_container;
784 struct principal_container **principals;
785 const char *keytab_name;
791 keytab_name = keytab_name_from_msg(mem_ctx, ldb, msg);
796 ret = smb_krb5_open_keytab(mem_ctx, smb_krb5_context, keytab_name, &keytab_container);
799 talloc_free(mem_ctx);
803 DEBUG(5, ("Opened keytab %s\n", keytab_name));
805 /* Get the principal we will store the new keytab entries under */
806 ret = principals_from_msg(mem_ctx, msg, smb_krb5_context, &principals, error_string);
809 *error_string = talloc_asprintf(parent_ctx, "Failed to load principals from ldb message: %s\n", *error_string);
810 talloc_free(mem_ctx);
814 ret = remove_old_entries(mem_ctx, msg, principals, delete_all_kvno,
815 smb_krb5_context, keytab_container->keytab, &found_previous, error_string);
817 *error_string = talloc_asprintf(parent_ctx, "Failed to remove old principals from keytab: %s\n", *error_string);
818 talloc_free(mem_ctx);
822 if (!delete_all_kvno) {
823 /* Create a new keytab. If during the cleanout we found
824 * entires for kvno -1, then don't try and duplicate them.
825 * Otherwise, add kvno, and kvno -1 */
827 ret = create_keytab(mem_ctx, msg, principals,
829 keytab_container->keytab,
830 found_previous ? false : true, error_string);
832 talloc_free(mem_ctx);
836 krb5_error_code smb_krb5_create_memory_keytab(TALLOC_CTX *parent_ctx,
837 struct cli_credentials *machine_account,
838 struct smb_krb5_context *smb_krb5_context,
839 struct keytab_container **keytab_container)
842 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
843 const char *rand_string;
844 const char *keytab_name;
845 struct ldb_message *msg;
846 const char *error_string;
851 *keytab_container = talloc(mem_ctx, struct keytab_container);
853 rand_string = generate_random_str(mem_ctx, 16);
855 talloc_free(mem_ctx);
859 keytab_name = talloc_asprintf(mem_ctx, "MEMORY:%s",
862 talloc_free(mem_ctx);
866 ret = smb_krb5_open_keytab(mem_ctx, smb_krb5_context, keytab_name, keytab_container);
871 msg = ldb_msg_new(mem_ctx);
873 talloc_free(mem_ctx);
876 ldb_msg_add_string(msg, "krb5Keytab", keytab_name);
877 ldb_msg_add_string(msg, "secret", cli_credentials_get_password(machine_account));
878 ldb_msg_add_string(msg, "samAccountName", cli_credentials_get_username(machine_account));
879 ldb_msg_add_string(msg, "realm", cli_credentials_get_realm(machine_account));
880 ldb_msg_add_fmt(msg, "msDS-KeyVersionNumber", "%d", (int)cli_credentials_get_kvno(machine_account));
882 ret = smb_krb5_update_keytab(mem_ctx, smb_krb5_context, NULL, msg, false, &error_string);
884 talloc_steal(parent_ctx, *keytab_container);
886 DEBUG(0, ("Failed to create in-memory keytab: %s\n", error_string));
887 *keytab_container = NULL;
889 talloc_free(mem_ctx);
892 /* Translate between the IETF encryption type values and the Microsoft msDS-SupportedEncryptionTypes values */
893 uint32_t kerberos_enctype_to_bitmap(krb5_enctype enc_type_enum)
895 switch (enc_type_enum) {
896 case ENCTYPE_DES_CBC_CRC:
898 case ENCTYPE_DES_CBC_MD5:
900 case ENCTYPE_ARCFOUR_HMAC_MD5:
901 return ENC_RC4_HMAC_MD5;
902 case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
903 return ENC_HMAC_SHA1_96_AES128;
904 case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
905 return ENC_HMAC_SHA1_96_AES256;
911 /* Translate between the Microsoft msDS-SupportedEncryptionTypes values and the IETF encryption type values */
912 krb5_enctype kerberos_enctype_bitmap_to_enctype(uint32_t enctype_bitmap)
914 switch (enctype_bitmap) {
916 return ENCTYPE_DES_CBC_CRC;
918 return ENCTYPE_DES_CBC_MD5;
919 case ENC_RC4_HMAC_MD5:
920 return ENCTYPE_ARCFOUR_HMAC_MD5;
921 case ENC_HMAC_SHA1_96_AES128:
922 return ENCTYPE_AES128_CTS_HMAC_SHA1_96;
923 case ENC_HMAC_SHA1_96_AES256:
924 return ENCTYPE_AES256_CTS_HMAC_SHA1_96;
930 /* Return an array of krb5_enctype values */
931 krb5_error_code kerberos_enctype_bitmap_to_enctypes(TALLOC_CTX *mem_ctx, uint32_t enctype_bitmap, krb5_enctype **enctypes)
933 unsigned int i, j = 0;
934 *enctypes = talloc_zero_array(mem_ctx, krb5_enctype, (8*sizeof(enctype_bitmap))+1);
938 for (i=0; i<(8*sizeof(enctype_bitmap)); i++) {
939 uint32_t bit_value = (1 << i) & enctype_bitmap;
940 if (bit_value & enctype_bitmap) {
941 (*enctypes)[j] = kerberos_enctype_bitmap_to_enctype(bit_value);
942 if (!(*enctypes)[j]) {