2 Unix SMB/CIFS implementation.
4 Kerberos utility functions
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/>.
25 #include "system/kerberos.h"
26 #include "auth/kerberos/kerberos.h"
27 #include "auth/kerberos/kerberos_srv_keytab.h"
29 static void keytab_principals_free(krb5_context context, krb5_principal *set)
32 for (i = 0; set[i] != NULL; i++) {
33 krb5_free_principal(context, set[i]);
37 static krb5_error_code principals_from_list(TALLOC_CTX *parent_ctx,
38 const char *samAccountName,
40 const char **SPNs, int num_SPNs,
42 krb5_principal **principals_out,
43 const char **error_string)
49 krb5_principal *principals = NULL;
50 tmp_ctx = talloc_new(parent_ctx);
52 *error_string = "Cannot allocate tmp_ctx";
57 *error_string = "Cannot make principal without a realm";
62 upper_realm = strupper_talloc(tmp_ctx, realm);
64 *error_string = "Cannot allocate full upper case realm";
69 principals = talloc_zero_array(tmp_ctx, krb5_principal,
70 num_SPNs ? (num_SPNs + 2) : 2);
72 for (i = 0; num_SPNs && i < num_SPNs; i++) {
73 ret = krb5_parse_name(context, SPNs[i], &principals[i]);
76 *error_string = smb_get_krb5_error_message(context, ret,
83 ret = krb5_make_principal(context, &principals[i],
84 upper_realm, samAccountName,
87 *error_string = smb_get_krb5_error_message(context, ret,
95 keytab_principals_free(context, principals);
97 *principals_out = talloc_steal(parent_ctx, principals);
103 static krb5_error_code salt_principal(TALLOC_CTX *parent_ctx,
104 const char *samAccountName,
106 const char *saltPrincipal,
107 krb5_context context,
108 krb5_principal *salt_princ,
109 const char **error_string)
113 char *machine_username;
121 ret = krb5_parse_name(context, saltPrincipal, salt_princ);
123 *error_string = smb_get_krb5_error_message(
124 context, ret, parent_ctx);
129 if (!samAccountName) {
130 (*error_string) = "Cannot determine salt principal, no "
131 "saltPrincipal or samAccountName specified";
136 *error_string = "Cannot make principal without a realm";
140 tmp_ctx = talloc_new(parent_ctx);
142 *error_string = "Cannot allocate tmp_ctx";
146 machine_username = talloc_strdup(tmp_ctx, samAccountName);
147 if (!machine_username) {
148 *error_string = "Cannot duplicate samAccountName";
149 talloc_free(tmp_ctx);
153 if (machine_username[strlen(machine_username)-1] == '$') {
154 machine_username[strlen(machine_username)-1] = '\0';
157 lower_realm = strlower_talloc(tmp_ctx, realm);
159 *error_string = "Cannot allocate to lower case realm";
160 talloc_free(tmp_ctx);
164 upper_realm = strupper_talloc(tmp_ctx, realm);
166 *error_string = "Cannot allocate to upper case realm";
167 talloc_free(tmp_ctx);
171 salt_body = talloc_asprintf(tmp_ctx, "%s.%s",
172 machine_username, lower_realm);
174 *error_string = "Cannot form salt principal body";
175 talloc_free(tmp_ctx);
179 ret = krb5_make_principal(context, salt_princ, upper_realm,
180 "host", salt_body, NULL);
182 *error_string = smb_get_krb5_error_message(context,
186 talloc_free(tmp_ctx);
190 /* Translate between the Microsoft msDS-SupportedEncryptionTypes values
191 * and the IETF encryption type values */
192 static krb5_enctype ms_suptype_to_ietf_enctype(uint32_t enctype_bitmap)
194 switch (enctype_bitmap) {
196 return ENCTYPE_DES_CBC_CRC;
198 return ENCTYPE_DES_CBC_MD5;
199 case ENC_RC4_HMAC_MD5:
200 return ENCTYPE_ARCFOUR_HMAC_MD5;
201 case ENC_HMAC_SHA1_96_AES128:
202 return ENCTYPE_AES128_CTS_HMAC_SHA1_96;
203 case ENC_HMAC_SHA1_96_AES256:
204 return ENCTYPE_AES256_CTS_HMAC_SHA1_96;
210 /* Return an array of krb5_enctype values */
211 static krb5_error_code ms_suptypes_to_ietf_enctypes(TALLOC_CTX *mem_ctx,
212 uint32_t enctype_bitmap,
213 krb5_enctype **enctypes)
215 unsigned int i, j = 0;
216 *enctypes = talloc_zero_array(mem_ctx, krb5_enctype,
217 (8 * sizeof(enctype_bitmap)) + 1);
221 for (i = 0; i < (8 * sizeof(enctype_bitmap)); i++) {
222 uint32_t bit_value = (1 << i) & enctype_bitmap;
223 if (bit_value & enctype_bitmap) {
224 (*enctypes)[j] = ms_suptype_to_ietf_enctype(bit_value);
225 if (!(*enctypes)[j]) {
235 static krb5_error_code keytab_add_keys(TALLOC_CTX *parent_ctx,
236 krb5_principal *principals,
237 krb5_principal salt_princ,
239 const char *password_s,
240 krb5_context context,
241 krb5_enctype *enctypes,
243 const char **error_string)
250 password.data = discard_const_p(char *, password_s);
251 password.length = strlen(password_s);
253 for (i = 0; enctypes[i]; i++) {
254 krb5_keytab_entry entry;
258 ret = create_kerberos_key_from_string_direct(context,
259 salt_princ, &password,
260 &entry.keyblock, enctypes[i]);
267 for (p = 0; principals[p]; p++) {
269 entry.principal = principals[p];
270 ret = krb5_kt_add_entry(context, keytab, &entry);
272 char *k5_error_string =
273 smb_get_krb5_error_message(context,
275 krb5_unparse_name(context,
276 principals[p], &unparsed);
277 *error_string = talloc_asprintf(parent_ctx,
278 "Failed to add enctype %d entry for "
279 "%s(kvno %d) to keytab: %s\n",
280 (int)enctypes[i], unparsed,
281 kvno, k5_error_string);
284 talloc_free(k5_error_string);
285 krb5_free_keyblock_contents(context,
290 DEBUG(5, ("Added key (kvno %d) to keytab (enctype %d)\n",
291 kvno, (int)enctypes[i]));
293 krb5_free_keyblock_contents(context, &entry.keyblock);
298 static krb5_error_code create_keytab(TALLOC_CTX *parent_ctx,
299 const char *samAccountName,
301 const char *saltPrincipal,
303 const char *new_secret,
304 const char *old_secret,
305 uint32_t supp_enctypes,
306 krb5_principal *principals,
307 krb5_context context,
310 const char **error_string)
313 krb5_principal salt_princ = NULL;
314 krb5_enctype *enctypes;
318 /* There is no password here, so nothing to do */
322 mem_ctx = talloc_new(parent_ctx);
324 *error_string = "unable to allocate tmp_ctx for create_keytab";
328 /* The salt used to generate these entries may be different however,
330 ret = salt_principal(mem_ctx, samAccountName, realm, saltPrincipal,
331 context, &salt_princ, error_string);
333 talloc_free(mem_ctx);
337 ret = ms_suptypes_to_ietf_enctypes(mem_ctx, supp_enctypes, &enctypes);
339 *error_string = talloc_asprintf(parent_ctx,
340 "create_keytab: generating list of "
341 "encryption types failed (%s)\n",
342 smb_get_krb5_error_message(context,
347 ret = keytab_add_keys(mem_ctx, principals,
348 salt_princ, kvno, new_secret,
349 context, enctypes, keytab, error_string);
354 if (old_secret && add_old && kvno != 0) {
355 ret = keytab_add_keys(mem_ctx, principals,
356 salt_princ, kvno - 1, old_secret,
357 context, enctypes, keytab, error_string);
361 krb5_free_principal(context, salt_princ);
362 talloc_free(mem_ctx);
367 * Walk the keytab, looking for entries of this principal name,
368 * with KVNO other than current kvno -1.
370 * These entries are now stale,
371 * we only keep the current and previous entries around.
373 * Inspired by the code in Samba3 for 'use kerberos keytab'.
376 static krb5_error_code remove_old_entries(TALLOC_CTX *parent_ctx,
378 krb5_principal *principals,
379 bool delete_all_kvno,
380 krb5_context context,
382 bool *found_previous,
383 const char **error_string)
385 krb5_error_code ret, ret2;
386 krb5_kt_cursor cursor;
387 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
393 *found_previous = false;
395 /* for each entry in the keytab */
396 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
400 case HEIM_ERR_OPNOTSUPP:
403 /* no point enumerating if there isn't anything here */
404 talloc_free(mem_ctx);
407 *error_string = talloc_asprintf(parent_ctx,
408 "failed to open keytab for read of old entries: %s\n",
409 smb_get_krb5_error_message(context, ret, mem_ctx));
410 talloc_free(mem_ctx);
416 bool matched = false;
417 krb5_keytab_entry entry;
418 ret = krb5_kt_next_entry(context, keytab, &entry, &cursor);
422 for (i = 0; principals[i]; i++) {
423 /* if it matches our principal */
424 if (krb5_kt_compare(context, &entry,
425 principals[i], 0, 0)) {
433 * it wasn't the one we were looking for anyway */
434 krb5_kt_free_entry(context, &entry);
438 /* delete it, if it is not kvno -1 */
439 if (entry.vno != (kvno - 1 )) {
440 /* Release the enumeration. We are going to
441 * have to start this from the top again,
442 * because deletes during enumeration may not
443 * always be consistent.
445 * Also, the enumeration locks a FILE: keytab
448 krb5_kt_end_seq_get(context, keytab, &cursor);
450 ret = krb5_kt_remove_entry(context, keytab, &entry);
451 krb5_kt_free_entry(context, &entry);
453 /* Deleted: Restart from the top */
454 ret2 = krb5_kt_start_seq_get(context, keytab, &cursor);
456 krb5_kt_free_entry(context, &entry);
457 DEBUG(1, ("failed to restart enumeration of keytab: %s\n",
458 smb_get_krb5_error_message(context,
461 talloc_free(mem_ctx);
470 *found_previous = true;
473 /* Free the entry, we don't need it any more */
474 krb5_kt_free_entry(context, &entry);
476 krb5_kt_end_seq_get(context, keytab, &cursor);
486 *error_string = talloc_asprintf(parent_ctx,
487 "failed in deleting old entries for principal: %s\n",
488 smb_get_krb5_error_message(context, ret, mem_ctx));
490 talloc_free(mem_ctx);
494 krb5_error_code smb_krb5_update_keytab(TALLOC_CTX *parent_ctx,
495 krb5_context context,
496 const char *keytab_name,
497 const char *samAccountName,
501 const char *saltPrincipal,
502 const char *new_secret,
503 const char *old_secret,
505 uint32_t supp_enctypes,
506 bool delete_all_kvno,
507 krb5_keytab *_keytab,
508 const char **error_string)
514 krb5_principal *principals = NULL;
516 if (keytab_name == NULL) {
520 ret = krb5_kt_resolve(context, keytab_name, &keytab);
522 *error_string = smb_get_krb5_error_message(context,
527 DEBUG(5, ("Opened keytab %s\n", keytab_name));
529 tmp_ctx = talloc_new(parent_ctx);
534 /* Get the principal we will store the new keytab entries under */
535 ret = principals_from_list(tmp_ctx,
536 samAccountName, realm, SPNs, num_SPNs,
537 context, &principals, error_string);
540 *error_string = talloc_asprintf(parent_ctx,
541 "Failed to load principals from ldb message: %s\n",
546 ret = remove_old_entries(tmp_ctx, kvno, principals, delete_all_kvno,
547 context, keytab, &found_previous, error_string);
549 *error_string = talloc_asprintf(parent_ctx,
550 "Failed to remove old principals from keytab: %s\n",
555 if (!delete_all_kvno) {
556 /* Create a new keytab. If during the cleanout we found
557 * entires for kvno -1, then don't try and duplicate them.
558 * Otherwise, add kvno, and kvno -1 */
560 ret = create_keytab(tmp_ctx,
561 samAccountName, realm, saltPrincipal,
562 kvno, new_secret, old_secret,
563 supp_enctypes, principals,
565 found_previous ? false : true,
568 talloc_steal(parent_ctx, *error_string);
572 if (ret == 0 && _keytab != NULL) {
573 /* caller wants the keytab handle back */
578 keytab_principals_free(context, principals);
579 if (ret != 0 || _keytab == NULL) {
580 krb5_kt_close(context, keytab);
582 talloc_free(tmp_ctx);
586 krb5_error_code smb_krb5_create_memory_keytab(TALLOC_CTX *parent_ctx,
587 krb5_context context,
588 const char *new_secret,
589 const char *samAccountName,
593 const char **keytab_name)
596 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
597 const char *rand_string;
598 const char *error_string;
603 rand_string = generate_random_str(mem_ctx, 16);
605 talloc_free(mem_ctx);
609 *keytab_name = talloc_asprintf(mem_ctx, "MEMORY:%s", rand_string);
610 if (*keytab_name == NULL) {
611 talloc_free(mem_ctx);
616 ret = smb_krb5_update_keytab(mem_ctx, context,
617 *keytab_name, samAccountName, realm,
618 NULL, 0, NULL, new_secret, NULL,
620 false, keytab, &error_string);
622 talloc_steal(parent_ctx, *keytab_name);
624 DEBUG(0, ("Failed to create in-memory keytab: %s\n",
628 talloc_free(mem_ctx);