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/>.
32 /**********************************************************************
33 **********************************************************************/
35 int smb_krb5_kt_add_entry_ext(krb5_context context
,
39 krb5_enctype
*enctypes
,
42 bool keep_old_entries
)
44 krb5_error_code ret
= 0;
45 krb5_kt_cursor cursor
;
46 krb5_keytab_entry kt_entry
;
47 krb5_principal princ
= NULL
;
51 ZERO_STRUCT(kt_entry
);
54 ret
= smb_krb5_parse_name(context
, princ_s
, &princ
);
56 DEBUG(1,("smb_krb5_kt_add_entry_ext: smb_krb5_parse_name(%s) failed (%s)\n", princ_s
, error_message(ret
)));
60 /* Seek and delete old keytab entries */
61 ret
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
62 if (ret
!= KRB5_KT_END
&& ret
!= ENOENT
) {
63 DEBUG(3,("smb_krb5_kt_add_entry_ext: Will try to delete old keytab entries\n"));
64 while(!krb5_kt_next_entry(context
, keytab
, &kt_entry
, &cursor
)) {
65 bool compare_name_ok
= False
;
67 ret
= smb_krb5_unparse_name(context
, kt_entry
.principal
, &ktprinc
);
69 DEBUG(1,("smb_krb5_kt_add_entry_ext: smb_krb5_unparse_name failed (%s)\n",
74 /*---------------------------------------------------------------------------
75 * Save the entries with kvno - 1. This is what microsoft does
76 * to allow people with existing sessions that have kvno - 1 to still
77 * work. Otherwise, when the password for the machine changes, all
78 * kerberizied sessions will 'break' until either the client reboots or
79 * the client's session key expires and they get a new session ticket
83 #ifdef HAVE_KRB5_KT_COMPARE
84 compare_name_ok
= (krb5_kt_compare(context
, &kt_entry
, princ
, 0, 0) == True
);
86 compare_name_ok
= (strcmp(ktprinc
, princ_s
) == 0);
89 if (!compare_name_ok
) {
90 DEBUG(10,("smb_krb5_kt_add_entry_ext: ignoring keytab entry principal %s, kvno = %d\n",
91 ktprinc
, kt_entry
.vno
));
96 if (compare_name_ok
) {
97 if (kt_entry
.vno
== kvno
- 1) {
98 DEBUG(5,("smb_krb5_kt_add_entry_ext: Saving previous (kvno %d) entry for principal: %s.\n",
100 } else if (!keep_old_entries
) {
101 DEBUG(5,("smb_krb5_kt_add_entry_ext: Found old entry for principal: %s (kvno %d) - trying to remove it.\n",
102 princ_s
, kt_entry
.vno
));
103 ret
= krb5_kt_end_seq_get(context
, keytab
, &cursor
);
106 DEBUG(1,("smb_krb5_kt_add_entry_ext: krb5_kt_end_seq_get() failed (%s)\n",
107 error_message(ret
)));
110 ret
= krb5_kt_remove_entry(context
, keytab
, &kt_entry
);
112 DEBUG(1,("smb_krb5_kt_add_entry_ext: krb5_kt_remove_entry failed (%s)\n",
113 error_message(ret
)));
117 DEBUG(5,("smb_krb5_kt_add_entry_ext: removed old entry for principal: %s (kvno %d).\n",
118 princ_s
, kt_entry
.vno
));
120 ret
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
122 DEBUG(1,("smb_krb5_kt_add_entry_ext: krb5_kt_start_seq failed (%s)\n",
123 error_message(ret
)));
126 ret
= smb_krb5_kt_free_entry(context
, &kt_entry
);
127 ZERO_STRUCT(kt_entry
);
129 DEBUG(1,("smb_krb5_kt_add_entry_ext: krb5_kt_remove_entry failed (%s)\n",
130 error_message(ret
)));
137 /* Not a match, just free this entry and continue. */
138 ret
= smb_krb5_kt_free_entry(context
, &kt_entry
);
139 ZERO_STRUCT(kt_entry
);
141 DEBUG(1,("smb_krb5_kt_add_entry_ext: smb_krb5_kt_free_entry failed (%s)\n", error_message(ret
)));
146 ret
= krb5_kt_end_seq_get(context
, keytab
, &cursor
);
149 DEBUG(1,("smb_krb5_kt_add_entry_ext: krb5_kt_end_seq_get failed (%s)\n",error_message(ret
)));
154 /* Ensure we don't double free. */
155 ZERO_STRUCT(kt_entry
);
158 /* If we get here, we have deleted all the old entries with kvno's not equal to the current kvno-1. */
160 /* Now add keytab entries for all encryption types */
161 for (i
= 0; enctypes
[i
]; i
++) {
164 keyp
= KRB5_KT_KEY(&kt_entry
);
166 if (create_kerberos_key_from_string(context
, princ
, &password
, keyp
, enctypes
[i
], no_salt
)) {
170 kt_entry
.principal
= princ
;
173 DEBUG(3,("smb_krb5_kt_add_entry_ext: adding keytab entry for (%s) with encryption type (%d) and version (%d)\n",
174 princ_s
, enctypes
[i
], kt_entry
.vno
));
175 ret
= krb5_kt_add_entry(context
, keytab
, &kt_entry
);
176 krb5_free_keyblock_contents(context
, keyp
);
177 ZERO_STRUCT(kt_entry
);
179 DEBUG(1,("smb_krb5_kt_add_entry_ext: adding entry to keytab failed (%s)\n", error_message(ret
)));
187 krb5_keytab_entry zero_kt_entry
;
188 ZERO_STRUCT(zero_kt_entry
);
189 if (memcmp(&zero_kt_entry
, &kt_entry
, sizeof(krb5_keytab_entry
))) {
190 smb_krb5_kt_free_entry(context
, &kt_entry
);
194 krb5_free_principal(context
, princ
);
198 krb5_kt_cursor zero_csr
;
199 ZERO_STRUCT(zero_csr
);
200 if ((memcmp(&cursor
, &zero_csr
, sizeof(krb5_kt_cursor
)) != 0) && keytab
) {
201 krb5_kt_end_seq_get(context
, keytab
, &cursor
);
208 static int smb_krb5_kt_add_entry(krb5_context context
,
212 krb5_enctype
*enctypes
,
215 return smb_krb5_kt_add_entry_ext(context
,
225 /**********************************************************************
226 Adds a single service principal, i.e. 'host' to the system keytab
227 ***********************************************************************/
229 int ads_keytab_add_entry(ADS_STRUCT
*ads
, const char *srvPrinc
)
231 krb5_error_code ret
= 0;
232 krb5_context context
= NULL
;
233 krb5_keytab keytab
= NULL
;
236 krb5_enctype enctypes
[4] = { ENCTYPE_DES_CBC_CRC
, ENCTYPE_DES_CBC_MD5
, 0, 0 };
237 char *princ_s
= NULL
, *short_princ_s
= NULL
;
238 char *password_s
= NULL
;
240 TALLOC_CTX
*ctx
= NULL
;
243 #if defined(ENCTYPE_ARCFOUR_HMAC)
244 enctypes
[2] = ENCTYPE_ARCFOUR_HMAC
;
247 initialize_krb5_error_table();
248 ret
= krb5_init_context(&context
);
250 DEBUG(1,("ads_keytab_add_entry: could not krb5_init_context: %s\n",error_message(ret
)));
254 ret
= smb_krb5_open_keytab(context
, NULL
, True
, &keytab
);
256 DEBUG(1,("ads_keytab_add_entry: smb_krb5_open_keytab failed (%s)\n", error_message(ret
)));
260 /* retrieve the password */
261 if (!secrets_init()) {
262 DEBUG(1,("ads_keytab_add_entry: secrets_init failed\n"));
266 password_s
= secrets_fetch_machine_password(lp_workgroup(), NULL
, NULL
);
268 DEBUG(1,("ads_keytab_add_entry: failed to fetch machine password\n"));
272 ZERO_STRUCT(password
);
273 password
.data
= password_s
;
274 password
.length
= strlen(password_s
);
276 /* we need the dNSHostName value here */
278 if ( (ctx
= talloc_init("ads_keytab_add_entry")) == NULL
) {
279 DEBUG(0,("ads_keytab_add_entry: talloc() failed!\n"));
284 if ( (my_fqdn
= ads_get_dnshostname( ads
, ctx
, global_myname())) == NULL
) {
285 DEBUG(0,("ads_keytab_add_entry: unable to determine machine account's dns name in AD!\n"));
290 if ( (machine_name
= ads_get_samaccountname( ads
, ctx
, global_myname())) == NULL
) {
291 DEBUG(0,("ads_keytab_add_entry: unable to determine machine account's short name in AD!\n"));
295 /*strip the trailing '$' */
296 machine_name
[strlen(machine_name
)-1] = '\0';
298 /* Construct our principal */
300 if (strchr_m(srvPrinc
, '@')) {
301 /* It's a fully-named principal. */
302 asprintf(&princ_s
, "%s", srvPrinc
);
303 } else if (srvPrinc
[strlen(srvPrinc
)-1] == '$') {
304 /* It's the machine account, as used by smbclient clients. */
305 asprintf(&princ_s
, "%s@%s", srvPrinc
, lp_realm());
307 /* It's a normal service principal. Add the SPN now so that we
308 * can obtain credentials for it and double-check the salt value
309 * used to generate the service's keys. */
311 asprintf(&princ_s
, "%s/%s@%s", srvPrinc
, my_fqdn
, lp_realm());
312 asprintf(&short_princ_s
, "%s/%s@%s", srvPrinc
, machine_name
, lp_realm());
314 /* According to http://support.microsoft.com/kb/326985/en-us,
315 certain principal names are automatically mapped to the host/...
316 principal in the AD account. So only create these in the
317 keytab, not in AD. --jerry */
319 if ( !strequal( srvPrinc
, "cifs" ) && !strequal(srvPrinc
, "host" ) ) {
320 DEBUG(3,("ads_keytab_add_entry: Attempting to add/update '%s'\n", princ_s
));
322 if (!ADS_ERR_OK(ads_add_service_principal_name(ads
, global_myname(), my_fqdn
, srvPrinc
))) {
323 DEBUG(1,("ads_keytab_add_entry: ads_add_service_principal_name failed.\n"));
329 kvno
= (krb5_kvno
) ads_get_machine_kvno(ads
, global_myname());
330 if (kvno
== -1) { /* -1 indicates failure, everything else is OK */
331 DEBUG(1,("ads_keytab_add_entry: ads_get_machine_kvno failed to determine the system's kvno.\n"));
336 /* add the fqdn principal to the keytab */
338 ret
= smb_krb5_kt_add_entry( context
, keytab
, kvno
, princ_s
, enctypes
, password
);
340 DEBUG(1,("ads_keytab_add_entry: Failed to add entry to keytab file\n"));
344 /* add the short principal name if we have one */
346 if ( short_princ_s
) {
347 ret
= smb_krb5_kt_add_entry( context
, keytab
, kvno
, short_princ_s
, enctypes
, password
);
349 DEBUG(1,("ads_keytab_add_entry: Failed to add short entry to keytab file\n"));
355 SAFE_FREE( princ_s
);
356 SAFE_FREE( short_princ_s
);
360 krb5_kt_close(context
, keytab
);
363 krb5_free_context(context
);
368 /**********************************************************************
369 Flushes all entries from the system keytab.
370 ***********************************************************************/
372 int ads_keytab_flush(ADS_STRUCT
*ads
)
374 krb5_error_code ret
= 0;
375 krb5_context context
= NULL
;
376 krb5_keytab keytab
= NULL
;
377 krb5_kt_cursor cursor
;
378 krb5_keytab_entry kt_entry
;
381 ZERO_STRUCT(kt_entry
);
384 initialize_krb5_error_table();
385 ret
= krb5_init_context(&context
);
387 DEBUG(1,("ads_keytab_flush: could not krb5_init_context: %s\n",error_message(ret
)));
391 ret
= smb_krb5_open_keytab(context
, NULL
, True
, &keytab
);
393 DEBUG(1,("ads_keytab_flush: smb_krb5_open_keytab failed (%s)\n", error_message(ret
)));
397 kvno
= (krb5_kvno
) ads_get_machine_kvno(ads
, global_myname());
398 if (kvno
== -1) { /* -1 indicates a failure */
399 DEBUG(1,("ads_keytab_flush: Error determining the system's kvno.\n"));
403 ret
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
404 if (ret
!= KRB5_KT_END
&& ret
!= ENOENT
) {
405 while (!krb5_kt_next_entry(context
, keytab
, &kt_entry
, &cursor
)) {
406 ret
= krb5_kt_end_seq_get(context
, keytab
, &cursor
);
409 DEBUG(1,("ads_keytab_flush: krb5_kt_end_seq_get() failed (%s)\n",error_message(ret
)));
412 ret
= krb5_kt_remove_entry(context
, keytab
, &kt_entry
);
414 DEBUG(1,("ads_keytab_flush: krb5_kt_remove_entry failed (%s)\n",error_message(ret
)));
417 ret
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
419 DEBUG(1,("ads_keytab_flush: krb5_kt_start_seq failed (%s)\n",error_message(ret
)));
422 ret
= smb_krb5_kt_free_entry(context
, &kt_entry
);
423 ZERO_STRUCT(kt_entry
);
425 DEBUG(1,("ads_keytab_flush: krb5_kt_remove_entry failed (%s)\n",error_message(ret
)));
431 /* Ensure we don't double free. */
432 ZERO_STRUCT(kt_entry
);
435 if (!ADS_ERR_OK(ads_clear_service_principal_names(ads
, global_myname()))) {
436 DEBUG(1,("ads_keytab_flush: Error while clearing service principal listings in LDAP.\n"));
443 krb5_keytab_entry zero_kt_entry
;
444 ZERO_STRUCT(zero_kt_entry
);
445 if (memcmp(&zero_kt_entry
, &kt_entry
, sizeof(krb5_keytab_entry
))) {
446 smb_krb5_kt_free_entry(context
, &kt_entry
);
450 krb5_kt_cursor zero_csr
;
451 ZERO_STRUCT(zero_csr
);
452 if ((memcmp(&cursor
, &zero_csr
, sizeof(krb5_kt_cursor
)) != 0) && keytab
) {
453 krb5_kt_end_seq_get(context
, keytab
, &cursor
);
457 krb5_kt_close(context
, keytab
);
460 krb5_free_context(context
);
465 /**********************************************************************
466 Adds all the required service principals to the system keytab.
467 ***********************************************************************/
469 int ads_keytab_create_default(ADS_STRUCT
*ads
)
471 krb5_error_code ret
= 0;
472 krb5_context context
= NULL
;
473 krb5_keytab keytab
= NULL
;
474 krb5_kt_cursor cursor
;
475 krb5_keytab_entry kt_entry
;
478 char *sam_account_name
, *upn
;
479 char **oldEntries
= NULL
, *princ_s
[26];
480 TALLOC_CTX
*ctx
= NULL
;
481 fstring machine_name
;
483 memset(princ_s
, '\0', sizeof(princ_s
));
485 fstrcpy( machine_name
, global_myname() );
487 /* these are the main ones we need */
489 if ( (ret
= ads_keytab_add_entry(ads
, "host") ) != 0 ) {
490 DEBUG(1,("ads_keytab_create_default: ads_keytab_add_entry failed while adding 'host'.\n"));
495 #if 0 /* don't create the CIFS/... keytab entries since no one except smbd
496 really needs them and we will fall back to verifying against secrets.tdb */
498 if ( (ret
= ads_keytab_add_entry(ads
, "cifs")) != 0 ) {
499 DEBUG(1,("ads_keytab_create_default: ads_keytab_add_entry failed while adding 'cifs'.\n"));
504 if ( (ctx
= talloc_init("ads_keytab_create_default")) == NULL
) {
505 DEBUG(0,("ads_keytab_create_default: talloc() failed!\n"));
509 /* now add the userPrincipalName and sAMAccountName entries */
511 if ( (sam_account_name
= ads_get_samaccountname( ads
, ctx
, machine_name
)) == NULL
) {
512 DEBUG(0,("ads_keytab_add_entry: unable to determine machine account's name in AD!\n"));
517 /* upper case the sAMAccountName to make it easier for apps to
518 know what case to use in the keytab file */
520 strupper_m( sam_account_name
);
522 if ( (ret
= ads_keytab_add_entry(ads
, sam_account_name
)) != 0 ) {
523 DEBUG(1,("ads_keytab_create_default: ads_keytab_add_entry failed while adding sAMAccountName (%s)\n",
528 /* remember that not every machine account will have a upn */
530 upn
= ads_get_upn( ads
, ctx
, machine_name
);
532 if ( (ret
= ads_keytab_add_entry(ads
, upn
)) != 0 ) {
533 DEBUG(1,("ads_keytab_create_default: ads_keytab_add_entry failed while adding UPN (%s)\n",
542 /* Now loop through the keytab and update any other existing entries... */
544 kvno
= (krb5_kvno
) ads_get_machine_kvno(ads
, machine_name
);
546 DEBUG(1,("ads_keytab_create_default: ads_get_machine_kvno failed to determine the system's kvno.\n"));
550 DEBUG(3,("ads_keytab_create_default: Searching for keytab entries to "
551 "preserve and update.\n"));
553 ZERO_STRUCT(kt_entry
);
556 initialize_krb5_error_table();
557 ret
= krb5_init_context(&context
);
559 DEBUG(1,("ads_keytab_create_default: could not krb5_init_context: %s\n",error_message(ret
)));
563 ret
= smb_krb5_open_keytab(context
, NULL
, True
, &keytab
);
565 DEBUG(1,("ads_keytab_create_default: smb_krb5_open_keytab failed (%s)\n", error_message(ret
)));
569 ret
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
570 if (ret
!= KRB5_KT_END
&& ret
!= ENOENT
) {
571 while ((ret
= krb5_kt_next_entry(context
, keytab
, &kt_entry
, &cursor
)) == 0) {
572 smb_krb5_kt_free_entry(context
, &kt_entry
);
573 ZERO_STRUCT(kt_entry
);
577 krb5_kt_end_seq_get(context
, keytab
, &cursor
);
581 * Hmmm. There is no "rewind" function for the keytab. This means we have a race condition
582 * where someone else could add entries after we've counted them. Re-open asap to minimise
586 DEBUG(3, ("ads_keytab_create_default: Found %d entries in the keytab.\n", found
));
590 oldEntries
= SMB_MALLOC_ARRAY(char *, found
);
592 DEBUG(1,("ads_keytab_create_default: Failed to allocate space to store the old keytab entries (malloc failed?).\n"));
596 memset(oldEntries
, '\0', found
* sizeof(char *));
598 ret
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
599 if (ret
!= KRB5_KT_END
&& ret
!= ENOENT
) {
600 while (krb5_kt_next_entry(context
, keytab
, &kt_entry
, &cursor
) == 0) {
601 if (kt_entry
.vno
!= kvno
) {
602 char *ktprinc
= NULL
;
605 /* This returns a malloc'ed string in ktprinc. */
606 ret
= smb_krb5_unparse_name(context
, kt_entry
.principal
, &ktprinc
);
608 DEBUG(1,("smb_krb5_unparse_name failed (%s)\n", error_message(ret
)));
612 * From looking at the krb5 source they don't seem to take locale
613 * or mb strings into account. Maybe this is because they assume utf8 ?
614 * In this case we may need to convert from utf8 to mb charset here ? JRA.
616 p
= strchr_m(ktprinc
, '@');
621 p
= strchr_m(ktprinc
, '/');
625 for (i
= 0; i
< found
; i
++) {
626 if (!oldEntries
[i
]) {
627 oldEntries
[i
] = ktprinc
;
630 if (!strcmp(oldEntries
[i
], ktprinc
)) {
639 smb_krb5_kt_free_entry(context
, &kt_entry
);
640 ZERO_STRUCT(kt_entry
);
643 for (i
= 0; oldEntries
[i
]; i
++) {
644 ret
|= ads_keytab_add_entry(ads
, oldEntries
[i
]);
645 SAFE_FREE(oldEntries
[i
]);
647 krb5_kt_end_seq_get(context
, keytab
, &cursor
);
653 SAFE_FREE(oldEntries
);
656 krb5_keytab_entry zero_kt_entry
;
657 ZERO_STRUCT(zero_kt_entry
);
658 if (memcmp(&zero_kt_entry
, &kt_entry
, sizeof(krb5_keytab_entry
))) {
659 smb_krb5_kt_free_entry(context
, &kt_entry
);
663 krb5_kt_cursor zero_csr
;
664 ZERO_STRUCT(zero_csr
);
665 if ((memcmp(&cursor
, &zero_csr
, sizeof(krb5_kt_cursor
)) != 0) && keytab
) {
666 krb5_kt_end_seq_get(context
, keytab
, &cursor
);
670 krb5_kt_close(context
, keytab
);
673 krb5_free_context(context
);
678 /**********************************************************************
680 ***********************************************************************/
682 int ads_keytab_list(const char *keytab_name
)
684 krb5_error_code ret
= 0;
685 krb5_context context
= NULL
;
686 krb5_keytab keytab
= NULL
;
687 krb5_kt_cursor cursor
;
688 krb5_keytab_entry kt_entry
;
690 ZERO_STRUCT(kt_entry
);
693 initialize_krb5_error_table();
694 ret
= krb5_init_context(&context
);
696 DEBUG(1,("ads_keytab_list: could not krb5_init_context: %s\n",error_message(ret
)));
700 ret
= smb_krb5_open_keytab(context
, keytab_name
, False
, &keytab
);
702 DEBUG(1,("ads_keytab_list: smb_krb5_open_keytab failed (%s)\n", error_message(ret
)));
706 ret
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
711 printf("Vno Type Principal\n");
713 while (krb5_kt_next_entry(context
, keytab
, &kt_entry
, &cursor
) == 0) {
715 char *princ_s
= NULL
;
716 char *etype_s
= NULL
;
717 krb5_enctype enctype
= 0;
719 ret
= smb_krb5_unparse_name(context
, kt_entry
.principal
, &princ_s
);
724 enctype
= smb_get_enctype_from_kt_entry(&kt_entry
);
726 ret
= smb_krb5_enctype_to_string(context
, enctype
, &etype_s
);
732 printf("%3d %s\t\t %s\n", kt_entry
.vno
, etype_s
, princ_s
);
737 ret
= smb_krb5_kt_free_entry(context
, &kt_entry
);
743 ret
= krb5_kt_end_seq_get(context
, keytab
, &cursor
);
748 /* Ensure we don't double free. */
749 ZERO_STRUCT(kt_entry
);
754 krb5_keytab_entry zero_kt_entry
;
755 ZERO_STRUCT(zero_kt_entry
);
756 if (memcmp(&zero_kt_entry
, &kt_entry
, sizeof(krb5_keytab_entry
))) {
757 smb_krb5_kt_free_entry(context
, &kt_entry
);
761 krb5_kt_cursor zero_csr
;
762 ZERO_STRUCT(zero_csr
);
763 if ((memcmp(&cursor
, &zero_csr
, sizeof(krb5_kt_cursor
)) != 0) && keytab
) {
764 krb5_kt_end_seq_get(context
, keytab
, &cursor
);
769 krb5_kt_close(context
, keytab
);
772 krb5_free_context(context
);
777 #endif /* HAVE_KRB5 */