2 Unix SMB/CIFS implementation.
3 kerberos keytab utility library
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Remus Koos 2001
6 Copyright (C) Luke Howard 2003
7 Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2003
8 Copyright (C) Guenther Deschner 2003
9 Copyright (C) Rakesh Patel 2004
10 Copyright (C) Dan Perry 2004
11 Copyright (C) Jeremy Allison 2004
12 Copyright (C) Gerald Carter 2006
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
37 /* This MAX_NAME_LEN is a constant defined in krb5.h */
38 #ifndef MAX_KEYTAB_NAME_LEN
39 #define MAX_KEYTAB_NAME_LEN 1100
42 static krb5_error_code
ads_keytab_open(krb5_context context
,
45 char keytab_str
[MAX_KEYTAB_NAME_LEN
] = {0};
46 const char *keytab_name
= NULL
;
47 krb5_error_code ret
= 0;
49 switch (lp_kerberos_method()) {
50 case KERBEROS_VERIFY_SYSTEM_KEYTAB
:
51 case KERBEROS_VERIFY_SECRETS_AND_KEYTAB
:
52 ret
= krb5_kt_default_name(context
,
54 sizeof(keytab_str
) - 2);
56 DBG_WARNING("Failed to get default keytab name");
59 keytab_name
= keytab_str
;
61 case KERBEROS_VERIFY_DEDICATED_KEYTAB
:
62 keytab_name
= lp_dedicated_keytab_file();
65 DBG_ERR("Invalid kerberos method set (%d)\n",
66 lp_kerberos_method());
67 ret
= KRB5_KT_BADNAME
;
71 if (keytab_name
== NULL
|| keytab_name
[0] == '\0') {
72 DBG_ERR("Invalid keytab name\n");
73 ret
= KRB5_KT_BADNAME
;
77 ret
= smb_krb5_kt_open(context
, keytab_name
, true, keytab
);
79 DBG_WARNING("smb_krb5_kt_open failed (%s)\n",
88 static bool fill_default_spns(TALLOC_CTX
*ctx
, const char *machine_name
,
89 const char *my_fqdn
, const char *spn
,
95 *spns
= talloc_zero_array(ctx
, const char*, 3);
101 psp1
= talloc_asprintf(ctx
,
109 if (!strlower_m(&psp1
[strlen(spn
) + 1])) {
114 psp2
= talloc_asprintf(ctx
,
122 if (!strlower_m(&psp2
[strlen(spn
) + 1])) {
131 static bool ads_set_machine_account_spns(TALLOC_CTX
*ctx
,
133 const char *service_or_spn
,
136 const char **spn_names
= NULL
;
138 struct spn_struct
* spn_struct
= NULL
;
141 /* SPN should have '/' */
142 tmp
= strchr_m(service_or_spn
, '/');
144 spn_struct
= parse_spn(ctx
, service_or_spn
);
145 if (spn_struct
== NULL
) {
150 DBG_INFO("Attempting to add/update '%s'\n", service_or_spn
);
152 if (spn_struct
!= NULL
) {
153 spn_names
= talloc_zero_array(ctx
, const char*, 2);
154 spn_names
[0] = service_or_spn
;
158 ok
= fill_default_spns(ctx
,
167 aderr
= ads_add_service_principal_names(ads
,
170 if (!ADS_ERR_OK(aderr
)) {
171 DBG_WARNING("Failed to add service principal name.\n");
179 * Create kerberos principal(s) from SPN or service name.
181 static bool service_or_spn_to_kerberos_princ(TALLOC_CTX
*ctx
,
182 const char *service_or_spn
,
185 char **p_short_princ_s
)
187 char *princ_s
= NULL
;
188 char *short_princ_s
= NULL
;
189 const char *service
= service_or_spn
;
190 const char *host
= my_fqdn
;
191 struct spn_struct
* spn_struct
= NULL
;
195 /* SPN should have '/' */
196 tmp
= strchr_m(service_or_spn
, '/');
198 spn_struct
= parse_spn(ctx
, service_or_spn
);
199 if (spn_struct
== NULL
) {
204 if (spn_struct
!= NULL
) {
205 service
= spn_struct
->serviceclass
;
206 host
= spn_struct
->host
;
208 princ_s
= talloc_asprintf(ctx
, "%s/%s@%s",
211 if (princ_s
== NULL
) {
216 if (spn_struct
== NULL
) {
217 short_princ_s
= talloc_asprintf(ctx
, "%s/%s@%s",
218 service
, lp_netbios_name(),
220 if (short_princ_s
== NULL
) {
225 *p_princ_s
= princ_s
;
226 *p_short_princ_s
= short_princ_s
;
231 static int add_kt_entry_etypes(krb5_context context
, TALLOC_CTX
*tmpctx
,
232 ADS_STRUCT
*ads
, const char *salt_princ_s
,
233 krb5_keytab keytab
, krb5_kvno kvno
,
234 const char *srvPrinc
, const char *my_fqdn
,
235 krb5_data
*password
, bool update_ads
)
237 krb5_error_code ret
= 0;
238 char *princ_s
= NULL
;
239 char *short_princ_s
= NULL
;
240 krb5_enctype enctypes
[4] = {
241 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
242 ENCTYPE_AES256_CTS_HMAC_SHA1_96
,
244 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
245 ENCTYPE_AES128_CTS_HMAC_SHA1_96
,
247 ENCTYPE_ARCFOUR_HMAC
,
252 /* Construct our principal */
253 if (strchr_m(srvPrinc
, '@')) {
254 /* It's a fully-named principal. */
255 princ_s
= talloc_asprintf(tmpctx
, "%s", srvPrinc
);
260 } else if (srvPrinc
[strlen(srvPrinc
)-1] == '$') {
261 /* It's the machine account, as used by smbclient clients. */
262 princ_s
= talloc_asprintf(tmpctx
, "%s@%s",
263 srvPrinc
, lp_realm());
269 /* It's a normal service principal. Add the SPN now so that we
270 * can obtain credentials for it and double-check the salt value
271 * used to generate the service's keys. */
273 if (!service_or_spn_to_kerberos_princ(tmpctx
,
282 /* According to http://support.microsoft.com/kb/326985/en-us,
283 certain principal names are automatically mapped to the
284 host/... principal in the AD account.
285 So only create these in the keytab, not in AD. --jerry */
287 if (update_ads
&& !strequal(srvPrinc
, "cifs") &&
288 !strequal(srvPrinc
, "host")) {
289 if (!ads_set_machine_account_spns(tmpctx
,
299 for (i
= 0; enctypes
[i
]; i
++) {
301 /* add the fqdn principal to the keytab */
302 ret
= smb_krb5_kt_add_entry(context
,
309 false); /* no_salt */
311 DBG_WARNING("Failed to add entry to keytab\n");
315 /* add the short principal name if we have one */
317 ret
= smb_krb5_kt_add_entry(context
,
324 false); /* no_salt */
326 DBG_WARNING("Failed to add short entry to keytab\n");
335 /**********************************************************************
336 Adds a single service principal, i.e. 'host' to the system keytab
337 ***********************************************************************/
339 int ads_keytab_add_entry(ADS_STRUCT
*ads
, const char *srvPrinc
, bool update_ads
)
341 krb5_error_code ret
= 0;
342 krb5_context context
= NULL
;
343 krb5_keytab keytab
= NULL
;
346 char *salt_princ_s
= NULL
;
347 char *password_s
= NULL
;
349 TALLOC_CTX
*tmpctx
= NULL
;
350 char **hostnames_array
= NULL
;
351 size_t num_hostnames
= 0;
353 ret
= smb_krb5_init_context_common(&context
);
355 DBG_ERR("kerberos init context failed (%s)\n",
360 ret
= ads_keytab_open(context
, &keytab
);
365 /* retrieve the password */
366 if (!secrets_init()) {
367 DBG_WARNING("secrets_init failed\n");
371 password_s
= secrets_fetch_machine_password(lp_workgroup(), NULL
, NULL
);
373 DBG_WARNING("failed to fetch machine password\n");
377 ZERO_STRUCT(password
);
378 password
.data
= password_s
;
379 password
.length
= strlen(password_s
);
381 /* we need the dNSHostName value here */
382 tmpctx
= talloc_init(__location__
);
384 DBG_ERR("talloc_init() failed!\n");
389 my_fqdn
= ads_get_dnshostname(ads
, tmpctx
, lp_netbios_name());
391 DBG_ERR("unable to determine machine account's dns name in "
397 /* make sure we have a single instance of a the computer account */
398 if (!ads_has_samaccountname(ads
, tmpctx
, lp_netbios_name())) {
399 DBG_ERR("unable to determine machine account's short name in "
405 kvno
= (krb5_kvno
)ads_get_machine_kvno(ads
, lp_netbios_name());
407 /* -1 indicates failure, everything else is OK */
408 DBG_WARNING("ads_get_machine_kvno failed to determine the "
414 salt_princ_s
= kerberos_secrets_fetch_salt_princ();
415 if (salt_princ_s
== NULL
) {
416 DBG_WARNING("kerberos_secrets_fetch_salt_princ() failed\n");
421 ret
= add_kt_entry_etypes(context
, tmpctx
, ads
, salt_princ_s
, keytab
,
422 kvno
, srvPrinc
, my_fqdn
, &password
,
428 if (ADS_ERR_OK(ads_get_additional_dns_hostnames(tmpctx
, ads
,
434 for (i
= 0; i
< num_hostnames
; i
++) {
436 ret
= add_kt_entry_etypes(context
, tmpctx
, ads
,
437 salt_princ_s
, keytab
,
440 &password
, update_ads
);
448 SAFE_FREE(salt_princ_s
);
452 krb5_kt_close(context
, keytab
);
455 krb5_free_context(context
);
460 /**********************************************************************
461 Flushes all entries from the system keytab.
462 ***********************************************************************/
464 int ads_keytab_flush(ADS_STRUCT
*ads
)
466 krb5_error_code ret
= 0;
467 krb5_context context
= NULL
;
468 krb5_keytab keytab
= NULL
;
471 ret
= smb_krb5_init_context_common(&context
);
473 DBG_ERR("kerberos init context failed (%s)\n",
478 ret
= ads_keytab_open(context
, &keytab
);
483 /* Seek and delete all old keytab entries */
484 ret
= smb_krb5_kt_seek_and_delete_old_entries(context
,
486 false, /* keep_old_kvno */
488 false, /* enctype_only */
497 aderr
= ads_clear_service_principal_names(ads
, lp_netbios_name());
498 if (!ADS_ERR_OK(aderr
)) {
499 DEBUG(1, (__location__
": Error while clearing service "
500 "principal listings in LDAP.\n"));
507 krb5_kt_close(context
, keytab
);
510 krb5_free_context(context
);
515 /**********************************************************************
516 Adds all the required service principals to the system keytab.
517 ***********************************************************************/
519 int ads_keytab_create_default(ADS_STRUCT
*ads
)
521 krb5_error_code ret
= 0;
522 krb5_context context
= NULL
;
523 krb5_keytab keytab
= NULL
;
524 krb5_kt_cursor cursor
= {0};
525 krb5_keytab_entry kt_entry
= {0};
528 char *sam_account_name
, *upn
;
529 char **oldEntries
= NULL
, *princ_s
[26];
538 ZERO_STRUCT(kt_entry
);
541 frame
= talloc_stackframe();
547 status
= ads_get_service_principal_names(frame
,
552 if (!ADS_ERR_OK(status
)) {
557 for (i
= 0; i
< num_spns
; i
++) {
561 srv_princ
= strlower_talloc(frame
, spn_array
[i
]);
562 if (srv_princ
== NULL
) {
567 p
= strchr_m(srv_princ
, '/');
573 /* Add the SPNs found on the DC */
574 ret
= ads_keytab_add_entry(ads
, srv_princ
, false);
576 DEBUG(1, ("ads_keytab_add_entry failed while "
577 "adding '%s' principal.\n",
583 #if 0 /* don't create the CIFS/... keytab entries since no one except smbd
584 really needs them and we will fall back to verifying against
587 ret
= ads_keytab_add_entry(ads
, "cifs", false));
589 DEBUG(1, (__location__
": ads_keytab_add_entry failed while "
590 "adding 'cifs'.\n"));
595 memset(princ_s
, '\0', sizeof(princ_s
));
597 ret
= smb_krb5_init_context_common(&context
);
599 DBG_ERR("kerberos init context failed (%s)\n",
604 machine_name
= talloc_strdup(frame
, lp_netbios_name());
610 /* now add the userPrincipalName and sAMAccountName entries */
611 ok
= ads_has_samaccountname(ads
, frame
, machine_name
);
613 DEBUG(0, (__location__
": unable to determine machine "
614 "account's name in AD!\n"));
620 * append '$' to netbios name so 'ads_keytab_add_entry' recognises
621 * it as a machine account rather than a service or Windows SPN.
623 sam_account_name
= talloc_asprintf(frame
, "%s$",machine_name
);
624 if (sam_account_name
== NULL
) {
628 /* upper case the sAMAccountName to make it easier for apps to
629 know what case to use in the keytab file */
630 if (!strupper_m(sam_account_name
)) {
635 ret
= ads_keytab_add_entry(ads
, sam_account_name
, false);
637 DEBUG(1, (__location__
": ads_keytab_add_entry() failed "
638 "while adding sAMAccountName (%s)\n",
643 /* remember that not every machine account will have a upn */
644 upn
= ads_get_upn(ads
, frame
, machine_name
);
646 ret
= ads_keytab_add_entry(ads
, upn
, false);
648 DEBUG(1, (__location__
": ads_keytab_add_entry() "
649 "failed while adding UPN (%s)\n", upn
));
654 /* Now loop through the keytab and update any other existing entries */
655 kvno
= (krb5_kvno
)ads_get_machine_kvno(ads
, machine_name
);
656 if (kvno
== (krb5_kvno
)-1) {
657 DEBUG(1, (__location__
": ads_get_machine_kvno() failed to "
658 "determine the system's kvno.\n"));
662 DEBUG(3, (__location__
": Searching for keytab entries to preserve "
665 ret
= ads_keytab_open(context
, &keytab
);
670 ret
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
671 if (ret
!= KRB5_KT_END
&& ret
!= ENOENT
) {
672 while ((ret
= krb5_kt_next_entry(context
, keytab
,
673 &kt_entry
, &cursor
)) == 0) {
674 smb_krb5_kt_free_entry(context
, &kt_entry
);
675 ZERO_STRUCT(kt_entry
);
679 krb5_kt_end_seq_get(context
, keytab
, &cursor
);
683 * Hmmm. There is no "rewind" function for the keytab. This means we
684 * have a race condition where someone else could add entries after
685 * we've counted them. Re-open asap to minimise the race. JRA.
687 DEBUG(3, (__location__
": Found %zd entries in the keytab.\n", found
));
692 oldEntries
= talloc_zero_array(frame
, char *, found
+ 1);
694 DEBUG(1, (__location__
": Failed to allocate space to store "
695 "the old keytab entries (talloc failed?).\n"));
700 ret
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
701 if (ret
== KRB5_KT_END
|| ret
== ENOENT
) {
702 krb5_kt_end_seq_get(context
, keytab
, &cursor
);
707 while (krb5_kt_next_entry(context
, keytab
, &kt_entry
, &cursor
) == 0) {
708 if (kt_entry
.vno
!= kvno
) {
709 char *ktprinc
= NULL
;
712 /* This returns a malloc'ed string in ktprinc. */
713 ret
= smb_krb5_unparse_name(oldEntries
, context
,
717 DEBUG(1, (__location__
718 ": smb_krb5_unparse_name failed "
719 "(%s)\n", error_message(ret
)));
723 * From looking at the krb5 source they don't seem to
724 * take locale or mb strings into account.
725 * Maybe this is because they assume utf8 ?
726 * In this case we may need to convert from utf8 to
727 * mb charset here ? JRA.
729 p
= strchr_m(ktprinc
, '@');
734 p
= strchr_m(ktprinc
, '/');
738 for (i
= 0; i
< found
; i
++) {
739 if (!oldEntries
[i
]) {
740 oldEntries
[i
] = ktprinc
;
743 if (!strcmp(oldEntries
[i
], ktprinc
)) {
744 TALLOC_FREE(ktprinc
);
749 TALLOC_FREE(ktprinc
);
752 smb_krb5_kt_free_entry(context
, &kt_entry
);
753 ZERO_STRUCT(kt_entry
);
755 krb5_kt_end_seq_get(context
, keytab
, &cursor
);
759 for (i
= 0; oldEntries
[i
]; i
++) {
760 ret
|= ads_keytab_add_entry(ads
, oldEntries
[i
], false);
761 TALLOC_FREE(oldEntries
[i
]);
765 TALLOC_FREE(oldEntries
);
769 if (!all_zero((uint8_t *)&kt_entry
, sizeof(kt_entry
))) {
770 smb_krb5_kt_free_entry(context
, &kt_entry
);
772 if (!all_zero((uint8_t *)&cursor
, sizeof(cursor
)) && keytab
) {
773 krb5_kt_end_seq_get(context
, keytab
, &cursor
);
776 krb5_kt_close(context
, keytab
);
778 krb5_free_context(context
);
783 #endif /* HAVE_ADS */
785 /**********************************************************************
787 ***********************************************************************/
789 int ads_keytab_list(const char *keytab_name
)
791 krb5_error_code ret
= 0;
792 krb5_context context
= NULL
;
793 krb5_keytab keytab
= NULL
;
794 krb5_kt_cursor cursor
;
795 krb5_keytab_entry kt_entry
;
797 ZERO_STRUCT(kt_entry
);
800 ret
= smb_krb5_init_context_common(&context
);
802 DBG_ERR("kerberos init context failed (%s)\n",
807 if (keytab_name
== NULL
) {
809 ret
= ads_keytab_open(context
, &keytab
);
814 ret
= smb_krb5_kt_open(context
, keytab_name
, False
, &keytab
);
817 DEBUG(1, ("smb_krb5_kt_open failed (%s)\n",
818 error_message(ret
)));
822 ret
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
828 printf("Vno Type Principal\n");
830 while (krb5_kt_next_entry(context
, keytab
, &kt_entry
, &cursor
) == 0) {
832 char *princ_s
= NULL
;
833 char *etype_s
= NULL
;
834 krb5_enctype enctype
= 0;
836 ret
= smb_krb5_unparse_name(talloc_tos(), context
,
837 kt_entry
.principal
, &princ_s
);
842 enctype
= smb_krb5_kt_get_enctype_from_entry(&kt_entry
);
844 ret
= smb_krb5_enctype_to_string(context
, enctype
, &etype_s
);
846 (asprintf(&etype_s
, "UNKNOWN: %d", enctype
) == -1)) {
847 TALLOC_FREE(princ_s
);
851 printf("%3d %-43s %s\n", kt_entry
.vno
, etype_s
, princ_s
);
853 TALLOC_FREE(princ_s
);
856 ret
= smb_krb5_kt_free_entry(context
, &kt_entry
);
862 ret
= krb5_kt_end_seq_get(context
, keytab
, &cursor
);
867 /* Ensure we don't double free. */
868 ZERO_STRUCT(kt_entry
);
872 if (!all_zero((uint8_t *)&kt_entry
, sizeof(kt_entry
))) {
873 smb_krb5_kt_free_entry(context
, &kt_entry
);
875 if (!all_zero((uint8_t *)&cursor
, sizeof(cursor
)) && keytab
) {
876 krb5_kt_end_seq_get(context
, keytab
, &cursor
);
880 krb5_kt_close(context
, keytab
);
883 krb5_free_context(context
);
888 #endif /* HAVE_KRB5 */