samba-tool: refuse to demote if the current DC has still roles
[Samba/gebeck_regimport.git] / source4 / auth / kerberos / kerberos_util.c
blobc255e6605ac954fe8c29b24e513db72f3e12a717
1 /*
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/>.
23 #include "includes.h"
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"
31 #include <ldb.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);
45 return 0;
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)
55 int ret;
56 struct principal_container *mem_ctx;
57 if (princ_string == NULL) {
58 *princ = NULL;
59 return 0;
62 ret = krb5_parse_name(smb_krb5_context->krb5_context,
63 princ_string, princ);
65 if (ret) {
66 (*error_string) = smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, parent_ctx);
67 return ret;
70 mem_ctx = talloc(parent_ctx, struct principal_container);
71 if (!mem_ctx) {
72 (*error_string) = error_message(ENOMEM);
73 return 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);
81 return 0;
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)
90 unsigned int i;
91 krb5_error_code ret;
92 char *upper_realm;
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");
96 TALLOC_CTX *tmp_ctx;
97 struct principal_container **principals;
98 tmp_ctx = talloc_new(parent_ctx);
99 if (!tmp_ctx) {
100 *error_string = "Cannot allocate tmp_ctx";
101 return ENOMEM;
104 if (!realm) {
105 *error_string = "Cannot have a kerberos secret in secrets.ldb without a realm";
106 return EINVAL;
109 upper_realm = strupper_talloc(tmp_ctx, realm);
110 if (!upper_realm) {
111 talloc_free(tmp_ctx);
112 *error_string = "Cannot allocate full upper case realm";
113 return ENOMEM;
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";
124 return ENOMEM;
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";
135 return ENOMEM;
138 ret = krb5_parse_name(smb_krb5_context->krb5_context,
139 principals[i]->string_form, &principals[i]->principal);
141 if (ret) {
142 talloc_free(tmp_ctx);
143 (*error_string) = smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, parent_ctx);
144 return ret;
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";
157 return ENOMEM;
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";
165 return ENOMEM;
168 ret = krb5_make_principal(smb_krb5_context->krb5_context, &principals[i]->principal, upper_realm, samAccountName,
169 NULL);
170 if (ret) {
171 talloc_free(tmp_ctx);
172 (*error_string) = smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, parent_ctx);
173 return ret;
176 /* This song-and-dance effectively puts the principal
177 * into talloc, so we can't loose it. */
178 talloc_set_destructor(principals[i], free_principal);
179 i++;
182 principals[i] = NULL;
183 *principals_out = talloc_steal(parent_ctx, principals);
185 talloc_free(tmp_ctx);
186 return ret;
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) {
201 krb5_error_code ret;
202 char *machine_username;
203 char *salt_body;
204 char *lower_realm;
205 char *upper_realm;
207 TALLOC_CTX *tmp_ctx;
208 struct principal_container *mem_ctx = talloc(parent_ctx, struct principal_container);
209 if (!mem_ctx) {
210 *error_string = "Cannot allocate mem_ctx";
211 return ENOMEM;
214 tmp_ctx = talloc_new(mem_ctx);
215 if (!tmp_ctx) {
216 talloc_free(mem_ctx);
217 *error_string = "Cannot allocate tmp_ctx";
218 return ENOMEM;
221 if (!realm) {
222 *error_string = "Cannot have a kerberos secret in secrets.ldb without a realm";
223 return EINVAL;
226 machine_username = talloc_strdup(tmp_ctx, samAccountName);
227 if (!machine_username) {
228 talloc_free(mem_ctx);
229 *error_string = "Cannot duplicate samAccountName";
230 return ENOMEM;
233 if (machine_username[strlen(machine_username)-1] == '$') {
234 machine_username[strlen(machine_username)-1] = '\0';
237 lower_realm = strlower_talloc(tmp_ctx, realm);
238 if (!lower_realm) {
239 talloc_free(mem_ctx);
240 *error_string = "Cannot allocate to lower case realm";
241 return ENOMEM;
244 upper_realm = strupper_talloc(tmp_ctx, realm);
245 if (!upper_realm) {
246 talloc_free(mem_ctx);
247 *error_string = "Cannot allocate to upper case realm";
248 return ENOMEM;
251 salt_body = talloc_asprintf(tmp_ctx, "%s.%s", machine_username,
252 lower_realm);
253 talloc_free(lower_realm);
254 talloc_free(machine_username);
255 if (!salt_body) {
256 talloc_free(mem_ctx);
257 *error_string = "Cannot form salt principal body";
258 return ENOMEM;
261 ret = krb5_make_principal(smb_krb5_context->krb5_context, salt_princ,
262 upper_realm,
263 "host", salt_body, NULL);
264 if (ret == 0) {
265 /* This song-and-dance effectively 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);
270 } else {
271 (*error_string) = smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, parent_ctx);
273 talloc_free(tmp_ctx);
274 return ret;
275 } else {
276 (*error_string) = "Cannot determine salt principal, no saltPrincipal or samAccountName specified";
277 return EINVAL;
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)
293 krb5_error_code ret;
294 const char *princ_string;
295 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
296 if (!mem_ctx) {
297 (*error_string) = error_message(ENOMEM);
298 return ENOMEM;
300 princ_string = cli_credentials_get_principal_and_obtained(credentials, mem_ctx, obtained);
301 if (!princ_string) {
302 (*error_string) = error_message(ENOMEM);
303 return ENOMEM;
306 ret = parse_principal(parent_ctx, princ_string,
307 smb_krb5_context, princ, error_string);
308 talloc_free(mem_ctx);
309 return ret;
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,
335 struct tevent_context *event_ctx,
336 krb5_ccache ccache,
337 enum credentials_obtained *obtained,
338 const char **error_string)
340 krb5_error_code ret;
341 const char *password;
342 const char *self_service;
343 const char *target_service;
344 time_t kdc_time = 0;
345 krb5_principal princ;
346 krb5_principal impersonate_principal;
347 int tries;
348 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
349 krb5_get_init_creds_opt *krb_options;
351 if (!mem_ctx) {
352 (*error_string) = strerror(ENOMEM);
353 return ENOMEM;
356 ret = principal_from_credentials(mem_ctx, credentials, smb_krb5_context, &princ, obtained, error_string);
357 if (ret) {
358 talloc_free(mem_ctx);
359 return ret;
362 ret = impersonate_principal_from_credentials(mem_ctx, credentials, smb_krb5_context, &impersonate_principal, error_string);
363 if (ret) {
364 talloc_free(mem_ctx);
365 return ret;
368 self_service = cli_credentials_get_self_service(credentials);
369 target_service = cli_credentials_get_target_service(credentials);
371 password = cli_credentials_get_password(credentials);
373 /* setup the krb5 options we want */
374 if ((ret = krb5_get_init_creds_opt_alloc(smb_krb5_context->krb5_context, &krb_options))) {
375 (*error_string) = talloc_asprintf(credentials, "krb5_get_init_creds_opt_alloc failed (%s)\n",
376 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
377 ret, mem_ctx));
378 talloc_free(mem_ctx);
379 return ret;
382 /* get the defaults */
383 krb5_get_init_creds_opt_set_default_flags(smb_krb5_context->krb5_context, NULL, NULL, krb_options);
385 /* set if we want a forwardable ticket */
386 switch (cli_credentials_get_krb_forwardable(credentials)) {
387 case CRED_AUTO_KRB_FORWARDABLE:
388 break;
389 case CRED_NO_KRB_FORWARDABLE:
390 krb5_get_init_creds_opt_set_forwardable(krb_options, FALSE);
391 break;
392 case CRED_FORCE_KRB_FORWARDABLE:
393 krb5_get_init_creds_opt_set_forwardable(krb_options, TRUE);
394 break;
398 * In order to work against windows KDCs even if we use
399 * the netbios domain name as realm, we need to add the following
400 * flags:
401 * KRB5_INIT_CREDS_NO_C_CANON_CHECK;
402 * KRB5_INIT_CREDS_NO_C_NO_EKU_CHECK;
404 krb5_get_init_creds_opt_set_win2k(smb_krb5_context->krb5_context,
405 krb_options, true);
407 tries = 2;
408 while (tries--) {
409 struct tevent_context *previous_ev;
410 /* Do this every time, in case we have weird recursive issues here */
411 ret = smb_krb5_context_set_event_ctx(smb_krb5_context, event_ctx, &previous_ev);
412 if (ret) {
413 talloc_free(mem_ctx);
414 return ret;
416 if (password) {
417 ret = kerberos_kinit_password_cc(smb_krb5_context->krb5_context, ccache,
418 princ, password,
419 impersonate_principal,
420 self_service,
421 target_service,
422 krb_options,
423 NULL, &kdc_time);
424 } else if (impersonate_principal) {
425 talloc_free(mem_ctx);
426 (*error_string) = "INTERNAL error: Cannot impersonate principal with just a keyblock. A password must be specified in the credentials";
427 return EINVAL;
428 } else {
429 /* No password available, try to use a keyblock instead */
431 krb5_keyblock keyblock;
432 const struct samr_Password *mach_pwd;
433 mach_pwd = cli_credentials_get_nt_hash(credentials, mem_ctx);
434 if (!mach_pwd) {
435 talloc_free(mem_ctx);
436 (*error_string) = "kinit_to_ccache: No password available for kinit\n";
437 krb5_get_init_creds_opt_free(smb_krb5_context->krb5_context, krb_options);
438 smb_krb5_context_remove_event_ctx(smb_krb5_context, previous_ev, event_ctx);
439 return EINVAL;
441 ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
442 ENCTYPE_ARCFOUR_HMAC,
443 mach_pwd->hash, sizeof(mach_pwd->hash),
444 &keyblock);
446 if (ret == 0) {
447 ret = kerberos_kinit_keyblock_cc(smb_krb5_context->krb5_context, ccache,
448 princ, &keyblock,
449 target_service, krb_options,
450 NULL, &kdc_time);
451 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &keyblock);
455 smb_krb5_context_remove_event_ctx(smb_krb5_context, previous_ev, event_ctx);
457 if (ret == KRB5KRB_AP_ERR_SKEW || ret == KRB5_KDCREP_SKEW) {
458 /* Perhaps we have been given an invalid skew, so try again without it */
459 time_t t = time(NULL);
460 krb5_set_real_time(smb_krb5_context->krb5_context, t, 0);
461 } else {
462 /* not a skew problem */
463 break;
467 krb5_get_init_creds_opt_free(smb_krb5_context->krb5_context, krb_options);
469 if (ret == KRB5KRB_AP_ERR_SKEW || ret == KRB5_KDCREP_SKEW) {
470 (*error_string) = talloc_asprintf(credentials, "kinit for %s failed (%s)\n",
471 cli_credentials_get_principal(credentials, mem_ctx),
472 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
473 ret, mem_ctx));
474 talloc_free(mem_ctx);
475 return ret;
478 /* cope with ticket being in the future due to clock skew */
479 if ((unsigned)kdc_time > time(NULL)) {
480 time_t t = time(NULL);
481 int time_offset =(unsigned)kdc_time-t;
482 DEBUG(4,("Advancing clock by %d seconds to cope with clock skew\n", time_offset));
483 krb5_set_real_time(smb_krb5_context->krb5_context, t + time_offset + 1, 0);
486 if (ret == KRB5KDC_ERR_PREAUTH_FAILED && cli_credentials_wrong_password(credentials)) {
487 ret = kinit_to_ccache(parent_ctx,
488 credentials,
489 smb_krb5_context,
490 event_ctx,
491 ccache, obtained,
492 error_string);
495 if (ret) {
496 (*error_string) = talloc_asprintf(credentials, "kinit for %s failed (%s)\n",
497 cli_credentials_get_principal(credentials, mem_ctx),
498 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
499 ret, mem_ctx));
500 talloc_free(mem_ctx);
501 return ret;
503 talloc_free(mem_ctx);
504 return 0;
507 static krb5_error_code free_keytab_container(struct keytab_container *ktc)
509 return krb5_kt_close(ktc->smb_krb5_context->krb5_context, ktc->keytab);
512 krb5_error_code smb_krb5_get_keytab_container(TALLOC_CTX *mem_ctx,
513 struct smb_krb5_context *smb_krb5_context,
514 const char *keytab_name, struct keytab_container **ktc)
516 krb5_keytab keytab;
517 krb5_error_code ret;
518 ret = krb5_kt_resolve(smb_krb5_context->krb5_context, keytab_name, &keytab);
519 if (ret) {
520 DEBUG(1,("failed to open krb5 keytab: %s\n",
521 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
522 ret, mem_ctx)));
523 return ret;
526 *ktc = talloc(mem_ctx, struct keytab_container);
527 if (!*ktc) {
528 return ENOMEM;
531 (*ktc)->smb_krb5_context = talloc_reference(*ktc, smb_krb5_context);
532 (*ktc)->keytab = keytab;
533 talloc_set_destructor(*ktc, free_keytab_container);
535 return 0;
538 static krb5_error_code keytab_add_keys(TALLOC_CTX *parent_ctx,
539 struct principal_container **principals,
540 krb5_principal salt_princ,
541 int kvno,
542 const char *password_s,
543 struct smb_krb5_context *smb_krb5_context,
544 krb5_enctype *enctypes,
545 krb5_keytab keytab,
546 const char **error_string)
548 unsigned int i, p;
549 krb5_error_code ret;
550 krb5_data password;
552 password.data = discard_const_p(char *, password_s);
553 password.length = strlen(password_s);
555 for (i=0; enctypes[i]; i++) {
556 krb5_keytab_entry entry;
558 ZERO_STRUCT(entry);
560 ret = create_kerberos_key_from_string_direct(smb_krb5_context->krb5_context,
561 salt_princ, &password, &entry.keyblock, enctypes[i]);
562 if (ret != 0) {
563 return ret;
566 entry.vno = kvno;
568 for (p=0; principals[p]; p++) {
569 entry.principal = principals[p]->principal;
570 ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
571 if (ret != 0) {
572 char *k5_error_string = smb_get_krb5_error_message(smb_krb5_context->krb5_context,
573 ret, NULL);
574 *error_string = talloc_asprintf(parent_ctx, "Failed to add enctype %d entry for %s(kvno %d) to keytab: %s\n",
575 (int)enctypes[i],
576 principals[p]->string_form,
577 kvno,
578 k5_error_string);
579 talloc_free(k5_error_string);
580 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
581 return ret;
584 DEBUG(5, ("Added %s(kvno %d) to keytab (enctype %d)\n",
585 principals[p]->string_form, kvno,
586 (int)enctypes[i]));
588 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
590 return 0;
593 static krb5_error_code create_keytab(TALLOC_CTX *parent_ctx,
594 struct ldb_message *msg,
595 struct principal_container **principals,
596 struct smb_krb5_context *smb_krb5_context,
597 krb5_keytab keytab,
598 bool add_old,
599 const char **error_string)
601 krb5_error_code ret;
602 const char *password_s;
603 const char *old_secret;
604 int kvno;
605 uint32_t enctype_bitmap;
606 krb5_principal salt_princ;
607 krb5_enctype *enctypes;
608 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
609 if (!mem_ctx) {
610 *error_string = "unable to allocate tmp_ctx for create_keytab";
611 return ENOMEM;
614 /* The salt used to generate these entries may be different however, fetch that */
615 ret = salt_principal_from_msg(mem_ctx, msg,
616 smb_krb5_context,
617 &salt_princ, error_string);
618 if (ret) {
619 talloc_free(mem_ctx);
620 return ret;
623 kvno = ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0);
625 /* Finally, do the dance to get the password to put in the entry */
626 password_s = ldb_msg_find_attr_as_string(msg, "secret", NULL);
628 if (!password_s) {
629 /* There is no password here, so nothing to do */
630 talloc_free(mem_ctx);
631 return 0;
634 if (add_old && kvno != 0) {
635 old_secret = ldb_msg_find_attr_as_string(msg, "priorSecret", NULL);
636 } else {
637 old_secret = NULL;
640 enctype_bitmap = (uint32_t)ldb_msg_find_attr_as_int(msg, "msDS-SupportedEncryptionTypes", ENC_ALL_TYPES);
642 ret = kerberos_enctype_bitmap_to_enctypes(mem_ctx, enctype_bitmap, &enctypes);
643 if (ret) {
644 *error_string = talloc_asprintf(parent_ctx, "create_keytab: generating list of encryption types failed (%s)\n",
645 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
646 ret, mem_ctx));
647 talloc_free(mem_ctx);
648 return ret;
651 ret = keytab_add_keys(mem_ctx, principals,
652 salt_princ,
653 kvno, password_s, smb_krb5_context,
654 enctypes, keytab, error_string);
655 if (ret) {
656 talloc_free(mem_ctx);
657 return ret;
660 if (old_secret) {
661 ret = keytab_add_keys(mem_ctx, principals,
662 salt_princ,
663 kvno - 1, old_secret, smb_krb5_context,
664 enctypes, keytab, error_string);
665 if (ret) {
666 talloc_free(mem_ctx);
667 return ret;
671 talloc_free(mem_ctx);
672 return ret;
676 * Walk the keytab, looking for entries of this principal name, with KVNO other than current kvno -1.
678 * These entries are now stale, we only keep the current, and previous entries around.
680 * Inspired by the code in Samba3 for 'use kerberos keytab'.
684 static krb5_error_code remove_old_entries(TALLOC_CTX *parent_ctx,
685 struct ldb_message *msg,
686 struct principal_container **principals,
687 bool delete_all_kvno,
688 struct smb_krb5_context *smb_krb5_context,
689 krb5_keytab keytab, bool *found_previous,
690 const char **error_string)
692 krb5_error_code ret, ret2;
693 krb5_kt_cursor cursor;
694 int kvno;
695 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
697 if (!mem_ctx) {
698 return ENOMEM;
701 *found_previous = false;
703 kvno = ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0);
705 /* for each entry in the keytab */
706 ret = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
707 switch (ret) {
708 case 0:
709 break;
710 case HEIM_ERR_OPNOTSUPP:
711 case ENOENT:
712 case KRB5_KT_END:
713 /* no point enumerating if there isn't anything here */
714 talloc_free(mem_ctx);
715 return 0;
716 default:
717 *error_string = talloc_asprintf(parent_ctx, "failed to open keytab for read of old entries: %s\n",
718 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
719 ret, mem_ctx));
720 talloc_free(mem_ctx);
721 return ret;
724 while (!ret) {
725 unsigned int i;
726 bool matched = false;
727 krb5_keytab_entry entry;
728 ret = krb5_kt_next_entry(smb_krb5_context->krb5_context, keytab, &entry, &cursor);
729 if (ret) {
730 break;
732 for (i = 0; principals[i]; i++) {
733 /* if it matches our principal */
734 if (krb5_kt_compare(smb_krb5_context->krb5_context, &entry, principals[i]->principal, 0, 0)) {
735 matched = true;
736 break;
740 if (!matched) {
741 /* Free the entry, it wasn't the one we were looking for anyway */
742 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
743 continue;
746 /* delete it, if it is not kvno -1 */
747 if (entry.vno != (kvno - 1 )) {
748 /* Release the enumeration. We are going to
749 * have to start this from the top again,
750 * because deletes during enumeration may not
751 * always be consistent.
753 * Also, the enumeration locks a FILE: keytab
756 krb5_kt_end_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
758 ret = krb5_kt_remove_entry(smb_krb5_context->krb5_context, keytab, &entry);
759 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
761 /* Deleted: Restart from the top */
762 ret2 = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
763 if (ret2) {
764 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
765 DEBUG(1,("failed to restart enumeration of keytab: %s\n",
766 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
767 ret, mem_ctx)));
769 talloc_free(mem_ctx);
770 return ret2;
773 if (ret) {
774 break;
777 } else {
778 *found_previous = true;
781 /* Free the entry, we don't need it any more */
782 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
786 krb5_kt_end_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
788 switch (ret) {
789 case 0:
790 break;
791 case ENOENT:
792 case KRB5_KT_END:
793 ret = 0;
794 break;
795 default:
796 *error_string = talloc_asprintf(parent_ctx, "failed in deleting old entries for principal: %s\n",
797 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
798 ret, mem_ctx));
800 talloc_free(mem_ctx);
801 return ret;
804 krb5_error_code smb_krb5_update_keytab(TALLOC_CTX *parent_ctx,
805 struct smb_krb5_context *smb_krb5_context,
806 struct ldb_context *ldb,
807 struct ldb_message *msg,
808 bool delete_all_kvno,
809 const char **error_string)
811 krb5_error_code ret;
812 bool found_previous;
813 TALLOC_CTX *mem_ctx = talloc_new(NULL);
814 struct keytab_container *keytab_container;
815 struct principal_container **principals;
816 const char *keytab_name;
818 if (!mem_ctx) {
819 return ENOMEM;
822 keytab_name = keytab_name_from_msg(mem_ctx, ldb, msg);
823 if (!keytab_name) {
824 return ENOENT;
827 ret = smb_krb5_get_keytab_container(mem_ctx, smb_krb5_context, keytab_name, &keytab_container);
829 if (ret != 0) {
830 talloc_free(mem_ctx);
831 return ret;
834 DEBUG(5, ("Opened keytab %s\n", keytab_name));
836 /* Get the principal we will store the new keytab entries under */
837 ret = principals_from_msg(mem_ctx, msg, smb_krb5_context, &principals, error_string);
839 if (ret != 0) {
840 *error_string = talloc_asprintf(parent_ctx, "Failed to load principals from ldb message: %s\n", *error_string);
841 talloc_free(mem_ctx);
842 return ret;
845 ret = remove_old_entries(mem_ctx, msg, principals, delete_all_kvno,
846 smb_krb5_context, keytab_container->keytab, &found_previous, error_string);
847 if (ret != 0) {
848 *error_string = talloc_asprintf(parent_ctx, "Failed to remove old principals from keytab: %s\n", *error_string);
849 talloc_free(mem_ctx);
850 return ret;
853 if (!delete_all_kvno) {
854 /* Create a new keytab. If during the cleanout we found
855 * entires for kvno -1, then don't try and duplicate them.
856 * Otherwise, add kvno, and kvno -1 */
858 ret = create_keytab(mem_ctx, msg, principals,
859 smb_krb5_context,
860 keytab_container->keytab,
861 found_previous ? false : true, error_string);
863 talloc_free(mem_ctx);
864 return ret;
867 krb5_error_code smb_krb5_create_memory_keytab(TALLOC_CTX *parent_ctx,
868 struct cli_credentials *machine_account,
869 struct smb_krb5_context *smb_krb5_context,
870 struct keytab_container **keytab_container)
872 krb5_error_code ret;
873 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
874 const char *rand_string;
875 const char *keytab_name;
876 struct ldb_message *msg;
877 const char *error_string;
878 if (!mem_ctx) {
879 return ENOMEM;
882 *keytab_container = talloc(mem_ctx, struct keytab_container);
884 rand_string = generate_random_str(mem_ctx, 16);
885 if (!rand_string) {
886 talloc_free(mem_ctx);
887 return ENOMEM;
890 keytab_name = talloc_asprintf(mem_ctx, "MEMORY:%s",
891 rand_string);
892 if (!keytab_name) {
893 talloc_free(mem_ctx);
894 return ENOMEM;
897 ret = smb_krb5_get_keytab_container(mem_ctx, smb_krb5_context, keytab_name, keytab_container);
898 if (ret) {
899 return ret;
902 msg = ldb_msg_new(mem_ctx);
903 if (!msg) {
904 talloc_free(mem_ctx);
905 return ENOMEM;
907 ldb_msg_add_string(msg, "krb5Keytab", keytab_name);
908 ldb_msg_add_string(msg, "secret", cli_credentials_get_password(machine_account));
909 ldb_msg_add_string(msg, "samAccountName", cli_credentials_get_username(machine_account));
910 ldb_msg_add_string(msg, "realm", cli_credentials_get_realm(machine_account));
911 ldb_msg_add_fmt(msg, "msDS-KeyVersionNumber", "%d", (int)cli_credentials_get_kvno(machine_account));
913 ret = smb_krb5_update_keytab(mem_ctx, smb_krb5_context, NULL, msg, false, &error_string);
914 if (ret == 0) {
915 talloc_steal(parent_ctx, *keytab_container);
916 } else {
917 DEBUG(0, ("Failed to create in-memory keytab: %s\n", error_string));
918 *keytab_container = NULL;
920 talloc_free(mem_ctx);
921 return ret;
923 /* Translate between the IETF encryption type values and the Microsoft msDS-SupportedEncryptionTypes values */
924 uint32_t kerberos_enctype_to_bitmap(krb5_enctype enc_type_enum)
926 switch (enc_type_enum) {
927 case ENCTYPE_DES_CBC_CRC:
928 return ENC_CRC32;
929 case ENCTYPE_DES_CBC_MD5:
930 return ENC_RSA_MD5;
931 case ENCTYPE_ARCFOUR_HMAC_MD5:
932 return ENC_RC4_HMAC_MD5;
933 case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
934 return ENC_HMAC_SHA1_96_AES128;
935 case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
936 return ENC_HMAC_SHA1_96_AES256;
937 default:
938 return 0;
942 /* Translate between the Microsoft msDS-SupportedEncryptionTypes values and the IETF encryption type values */
943 krb5_enctype kerberos_enctype_bitmap_to_enctype(uint32_t enctype_bitmap)
945 switch (enctype_bitmap) {
946 case ENC_CRC32:
947 return ENCTYPE_DES_CBC_CRC;
948 case ENC_RSA_MD5:
949 return ENCTYPE_DES_CBC_MD5;
950 case ENC_RC4_HMAC_MD5:
951 return ENCTYPE_ARCFOUR_HMAC_MD5;
952 case ENC_HMAC_SHA1_96_AES128:
953 return ENCTYPE_AES128_CTS_HMAC_SHA1_96;
954 case ENC_HMAC_SHA1_96_AES256:
955 return ENCTYPE_AES256_CTS_HMAC_SHA1_96;
956 default:
957 return 0;
961 /* Return an array of krb5_enctype values */
962 krb5_error_code kerberos_enctype_bitmap_to_enctypes(TALLOC_CTX *mem_ctx, uint32_t enctype_bitmap, krb5_enctype **enctypes)
964 unsigned int i, j = 0;
965 *enctypes = talloc_zero_array(mem_ctx, krb5_enctype, (8*sizeof(enctype_bitmap))+1);
966 if (!*enctypes) {
967 return ENOMEM;
969 for (i=0; i<(8*sizeof(enctype_bitmap)); i++) {
970 uint32_t bit_value = (1 << i) & enctype_bitmap;
971 if (bit_value & enctype_bitmap) {
972 (*enctypes)[j] = kerberos_enctype_bitmap_to_enctype(bit_value);
973 if (!(*enctypes)[j]) {
974 continue;
976 j++;
979 (*enctypes)[j] = 0;
980 return 0;