create-tarball: Adapt script to changed directory structure.
[Samba/gbeck.git] / source3 / libads / kerberos_keytab.c
blob883f5824452cb5fc4045e394110aab8180d9efdc
1 /*
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/>.
28 #include "includes.h"
30 #ifdef HAVE_KRB5
32 /**********************************************************************
33 **********************************************************************/
35 int smb_krb5_kt_add_entry_ext(krb5_context context,
36 krb5_keytab keytab,
37 krb5_kvno kvno,
38 const char *princ_s,
39 krb5_enctype *enctypes,
40 krb5_data password,
41 bool no_salt,
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;
48 int i;
49 char *ktprinc = NULL;
51 ZERO_STRUCT(kt_entry);
52 ZERO_STRUCT(cursor);
54 ret = smb_krb5_parse_name(context, princ_s, &princ);
55 if (ret) {
56 DEBUG(1,("smb_krb5_kt_add_entry_ext: smb_krb5_parse_name(%s) failed (%s)\n", princ_s, error_message(ret)));
57 goto out;
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);
68 if (ret) {
69 DEBUG(1,("smb_krb5_kt_add_entry_ext: smb_krb5_unparse_name failed (%s)\n",
70 error_message(ret)));
71 goto out;
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
80 * with the new kvno.
83 #ifdef HAVE_KRB5_KT_COMPARE
84 compare_name_ok = (krb5_kt_compare(context, &kt_entry, princ, 0, 0) == True);
85 #else
86 compare_name_ok = (strcmp(ktprinc, princ_s) == 0);
87 #endif
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));
94 SAFE_FREE(ktprinc);
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",
99 kvno - 1, princ_s));
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);
104 ZERO_STRUCT(cursor);
105 if (ret) {
106 DEBUG(1,("smb_krb5_kt_add_entry_ext: krb5_kt_end_seq_get() failed (%s)\n",
107 error_message(ret)));
108 goto out;
110 ret = krb5_kt_remove_entry(context, keytab, &kt_entry);
111 if (ret) {
112 DEBUG(1,("smb_krb5_kt_add_entry_ext: krb5_kt_remove_entry failed (%s)\n",
113 error_message(ret)));
114 goto out;
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);
121 if (ret) {
122 DEBUG(1,("smb_krb5_kt_add_entry_ext: krb5_kt_start_seq failed (%s)\n",
123 error_message(ret)));
124 goto out;
126 ret = smb_krb5_kt_free_entry(context, &kt_entry);
127 ZERO_STRUCT(kt_entry);
128 if (ret) {
129 DEBUG(1,("smb_krb5_kt_add_entry_ext: krb5_kt_remove_entry failed (%s)\n",
130 error_message(ret)));
131 goto out;
133 continue;
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);
140 if (ret) {
141 DEBUG(1,("smb_krb5_kt_add_entry_ext: smb_krb5_kt_free_entry failed (%s)\n", error_message(ret)));
142 goto out;
146 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
147 ZERO_STRUCT(cursor);
148 if (ret) {
149 DEBUG(1,("smb_krb5_kt_add_entry_ext: krb5_kt_end_seq_get failed (%s)\n",error_message(ret)));
150 goto out;
154 /* Ensure we don't double free. */
155 ZERO_STRUCT(kt_entry);
156 ZERO_STRUCT(cursor);
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++) {
162 krb5_keyblock *keyp;
164 keyp = KRB5_KT_KEY(&kt_entry);
166 if (create_kerberos_key_from_string(context, princ, &password, keyp, enctypes[i], no_salt)) {
167 continue;
170 kt_entry.principal = princ;
171 kt_entry.vno = kvno;
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);
178 if (ret) {
179 DEBUG(1,("smb_krb5_kt_add_entry_ext: adding entry to keytab failed (%s)\n", error_message(ret)));
180 goto out;
185 out:
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);
193 if (princ) {
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);
205 return (int)ret;
208 static int smb_krb5_kt_add_entry(krb5_context context,
209 krb5_keytab keytab,
210 krb5_kvno kvno,
211 const char *princ_s,
212 krb5_enctype *enctypes,
213 krb5_data password)
215 return smb_krb5_kt_add_entry_ext(context,
216 keytab,
217 kvno,
218 princ_s,
219 enctypes,
220 password,
221 false,
222 false);
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;
234 krb5_data password;
235 krb5_kvno kvno;
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;
239 char *my_fqdn;
240 TALLOC_CTX *ctx = NULL;
241 char *machine_name;
243 #if defined(ENCTYPE_ARCFOUR_HMAC)
244 enctypes[2] = ENCTYPE_ARCFOUR_HMAC;
245 #endif
247 initialize_krb5_error_table();
248 ret = krb5_init_context(&context);
249 if (ret) {
250 DEBUG(1,("ads_keytab_add_entry: could not krb5_init_context: %s\n",error_message(ret)));
251 return -1;
254 ret = smb_krb5_open_keytab(context, NULL, True, &keytab);
255 if (ret) {
256 DEBUG(1,("ads_keytab_add_entry: smb_krb5_open_keytab failed (%s)\n", error_message(ret)));
257 goto out;
260 /* retrieve the password */
261 if (!secrets_init()) {
262 DEBUG(1,("ads_keytab_add_entry: secrets_init failed\n"));
263 ret = -1;
264 goto out;
266 password_s = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
267 if (!password_s) {
268 DEBUG(1,("ads_keytab_add_entry: failed to fetch machine password\n"));
269 ret = -1;
270 goto out;
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"));
280 ret = -1;
281 goto out;
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"));
286 ret = -1;
287 goto out;
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"));
292 ret = -1;
293 goto out;
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());
306 } else {
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"));
324 goto out;
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"));
332 ret = -1;
333 goto out;
336 /* add the fqdn principal to the keytab */
338 ret = smb_krb5_kt_add_entry( context, keytab, kvno, princ_s, enctypes, password );
339 if ( ret ) {
340 DEBUG(1,("ads_keytab_add_entry: Failed to add entry to keytab file\n"));
341 goto out;
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 );
348 if ( ret ) {
349 DEBUG(1,("ads_keytab_add_entry: Failed to add short entry to keytab file\n"));
350 goto out;
354 out:
355 SAFE_FREE( princ_s );
356 SAFE_FREE( short_princ_s );
357 TALLOC_FREE( ctx );
359 if (keytab) {
360 krb5_kt_close(context, keytab);
362 if (context) {
363 krb5_free_context(context);
365 return (int)ret;
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;
379 krb5_kvno kvno;
381 ZERO_STRUCT(kt_entry);
382 ZERO_STRUCT(cursor);
384 initialize_krb5_error_table();
385 ret = krb5_init_context(&context);
386 if (ret) {
387 DEBUG(1,("ads_keytab_flush: could not krb5_init_context: %s\n",error_message(ret)));
388 return ret;
391 ret = smb_krb5_open_keytab(context, NULL, True, &keytab);
392 if (ret) {
393 DEBUG(1,("ads_keytab_flush: smb_krb5_open_keytab failed (%s)\n", error_message(ret)));
394 goto out;
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"));
400 goto out;
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);
407 ZERO_STRUCT(cursor);
408 if (ret) {
409 DEBUG(1,("ads_keytab_flush: krb5_kt_end_seq_get() failed (%s)\n",error_message(ret)));
410 goto out;
412 ret = krb5_kt_remove_entry(context, keytab, &kt_entry);
413 if (ret) {
414 DEBUG(1,("ads_keytab_flush: krb5_kt_remove_entry failed (%s)\n",error_message(ret)));
415 goto out;
417 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
418 if (ret) {
419 DEBUG(1,("ads_keytab_flush: krb5_kt_start_seq failed (%s)\n",error_message(ret)));
420 goto out;
422 ret = smb_krb5_kt_free_entry(context, &kt_entry);
423 ZERO_STRUCT(kt_entry);
424 if (ret) {
425 DEBUG(1,("ads_keytab_flush: krb5_kt_remove_entry failed (%s)\n",error_message(ret)));
426 goto out;
431 /* Ensure we don't double free. */
432 ZERO_STRUCT(kt_entry);
433 ZERO_STRUCT(cursor);
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"));
437 goto out;
440 out:
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);
456 if (keytab) {
457 krb5_kt_close(context, keytab);
459 if (context) {
460 krb5_free_context(context);
462 return ret;
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;
476 krb5_kvno kvno;
477 int i, found = 0;
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"));
491 return ret;
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"));
500 return ret;
502 #endif
504 if ( (ctx = talloc_init("ads_keytab_create_default")) == NULL ) {
505 DEBUG(0,("ads_keytab_create_default: talloc() failed!\n"));
506 return -1;
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"));
513 TALLOC_FREE( ctx );
514 return -1;
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",
524 sam_account_name));
525 return ret;
528 /* remember that not every machine account will have a upn */
530 upn = ads_get_upn( ads, ctx, machine_name);
531 if ( upn ) {
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",
534 upn));
535 TALLOC_FREE( ctx );
536 return ret;
540 TALLOC_FREE( ctx );
542 /* Now loop through the keytab and update any other existing entries... */
544 kvno = (krb5_kvno) ads_get_machine_kvno(ads, machine_name);
545 if (kvno == -1) {
546 DEBUG(1,("ads_keytab_create_default: ads_get_machine_kvno failed to determine the system's kvno.\n"));
547 return -1;
550 DEBUG(3,("ads_keytab_create_default: Searching for keytab entries to "
551 "preserve and update.\n"));
553 ZERO_STRUCT(kt_entry);
554 ZERO_STRUCT(cursor);
556 initialize_krb5_error_table();
557 ret = krb5_init_context(&context);
558 if (ret) {
559 DEBUG(1,("ads_keytab_create_default: could not krb5_init_context: %s\n",error_message(ret)));
560 return ret;
563 ret = smb_krb5_open_keytab(context, NULL, True, &keytab);
564 if (ret) {
565 DEBUG(1,("ads_keytab_create_default: smb_krb5_open_keytab failed (%s)\n", error_message(ret)));
566 goto done;
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);
574 found++;
577 krb5_kt_end_seq_get(context, keytab, &cursor);
578 ZERO_STRUCT(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
583 * the race. JRA.
586 DEBUG(3, ("ads_keytab_create_default: Found %d entries in the keytab.\n", found));
587 if (!found) {
588 goto done;
590 oldEntries = SMB_MALLOC_ARRAY(char *, found );
591 if (!oldEntries) {
592 DEBUG(1,("ads_keytab_create_default: Failed to allocate space to store the old keytab entries (malloc failed?).\n"));
593 ret = -1;
594 goto done;
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;
603 char *p;
605 /* This returns a malloc'ed string in ktprinc. */
606 ret = smb_krb5_unparse_name(context, kt_entry.principal, &ktprinc);
607 if (ret) {
608 DEBUG(1,("smb_krb5_unparse_name failed (%s)\n", error_message(ret)));
609 goto done;
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, '@');
617 if (p) {
618 *p = '\0';
621 p = strchr_m(ktprinc, '/');
622 if (p) {
623 *p = '\0';
625 for (i = 0; i < found; i++) {
626 if (!oldEntries[i]) {
627 oldEntries[i] = ktprinc;
628 break;
630 if (!strcmp(oldEntries[i], ktprinc)) {
631 SAFE_FREE(ktprinc);
632 break;
635 if (i == found) {
636 SAFE_FREE(ktprinc);
639 smb_krb5_kt_free_entry(context, &kt_entry);
640 ZERO_STRUCT(kt_entry);
642 ret = 0;
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);
649 ZERO_STRUCT(cursor);
651 done:
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);
669 if (keytab) {
670 krb5_kt_close(context, keytab);
672 if (context) {
673 krb5_free_context(context);
675 return ret;
678 /**********************************************************************
679 List system keytab.
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);
691 ZERO_STRUCT(cursor);
693 initialize_krb5_error_table();
694 ret = krb5_init_context(&context);
695 if (ret) {
696 DEBUG(1,("ads_keytab_list: could not krb5_init_context: %s\n",error_message(ret)));
697 return ret;
700 ret = smb_krb5_open_keytab(context, keytab_name, False, &keytab);
701 if (ret) {
702 DEBUG(1,("ads_keytab_list: smb_krb5_open_keytab failed (%s)\n", error_message(ret)));
703 goto out;
706 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
707 if (ret) {
708 goto out;
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);
720 if (ret) {
721 goto out;
724 enctype = smb_get_enctype_from_kt_entry(&kt_entry);
726 ret = smb_krb5_enctype_to_string(context, enctype, &etype_s);
727 if (ret) {
728 SAFE_FREE(princ_s);
729 goto out;
732 printf("%3d %s\t\t %s\n", kt_entry.vno, etype_s, princ_s);
734 SAFE_FREE(princ_s);
735 SAFE_FREE(etype_s);
737 ret = smb_krb5_kt_free_entry(context, &kt_entry);
738 if (ret) {
739 goto out;
743 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
744 if (ret) {
745 goto out;
748 /* Ensure we don't double free. */
749 ZERO_STRUCT(kt_entry);
750 ZERO_STRUCT(cursor);
751 out:
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);
768 if (keytab) {
769 krb5_kt_close(context, keytab);
771 if (context) {
772 krb5_free_context(context);
774 return ret;
777 #endif /* HAVE_KRB5 */