s3-ads: cleanup ads_keytab_list()
[samba.git] / source3 / libads / kerberos_keytab.c
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
13
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.
18
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.
23
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/>.
26 */
27
28 #include "includes.h"
29 #include "smb_krb5.h"
30 #include "ads.h"
31 #include "secrets.h"
32
33 #ifdef HAVE_KRB5
34
35 /**********************************************************************
36 **********************************************************************/
37
38 static krb5_error_code seek_and_delete_old_entries(krb5_context context,
39                                                    krb5_keytab keytab,
40                                                    krb5_kvno kvno,
41                                                    const char *princ_s,
42                                                    krb5_principal princ,
43                                                    bool flush,
44                                                    bool keep_old_entries)
45 {
46         krb5_error_code ret;
47         krb5_kt_cursor cursor;
48         krb5_kt_cursor zero_csr;
49         krb5_keytab_entry kt_entry;
50         krb5_keytab_entry zero_kt_entry;
51         char *ktprinc = NULL;
52
53         ZERO_STRUCT(cursor);
54         ZERO_STRUCT(zero_csr);
55         ZERO_STRUCT(kt_entry);
56         ZERO_STRUCT(zero_kt_entry);
57
58         ret = krb5_kt_start_seq_get(context, keytab, &cursor);
59         if (ret == KRB5_KT_END && ret == ENOENT ) {
60                 /* no entries */
61                 return 0;
62         }
63
64         DEBUG(3, (__location__ ": Will try to delete old keytab entries\n"));
65         while (!krb5_kt_next_entry(context, keytab, &kt_entry, &cursor)) {
66                 bool name_ok = False;
67
68                 if (!flush && (princ_s != NULL)) {
69                         ret = smb_krb5_unparse_name(talloc_tos(), context,
70                                                     kt_entry.principal,
71                                                     &ktprinc);
72                         if (ret) {
73                                 DEBUG(1, (__location__
74                                           ": smb_krb5_unparse_name failed "
75                                           "(%s)\n", error_message(ret)));
76                                 goto out;
77                         }
78
79 #ifdef HAVE_KRB5_KT_COMPARE
80                         name_ok = krb5_kt_compare(context, &kt_entry,
81                                                   princ, 0, 0);
82 #else
83                         name_ok = (strcmp(ktprinc, princ_s) == 0);
84 #endif
85
86                         if (!name_ok) {
87                                 DEBUG(10, (__location__ ": ignoring keytab "
88                                            "entry principal %s, kvno = %d\n",
89                                            ktprinc, kt_entry.vno));
90
91                                 /* Not a match,
92                                  * just free this entry and continue. */
93                                 ret = smb_krb5_kt_free_entry(context,
94                                                              &kt_entry);
95                                 ZERO_STRUCT(kt_entry);
96                                 if (ret) {
97                                         DEBUG(1, (__location__
98                                                   ": smb_krb5_kt_free_entry "
99                                                   "failed (%s)\n",
100                                                   error_message(ret)));
101                                         goto out;
102                                 }
103
104                                 TALLOC_FREE(ktprinc);
105                                 continue;
106                         }
107
108                         TALLOC_FREE(ktprinc);
109                 }
110
111                 /*------------------------------------------------------------
112                  * Save the entries with kvno - 1. This is what microsoft does
113                  * to allow people with existing sessions that have kvno - 1
114                  * to still work. Otherwise, when the password for the machine
115                  * changes, all kerberizied sessions will 'break' until either
116                  * the client reboots or the client's session key expires and
117                  * they get a new session ticket with the new kvno.
118                  */
119
120                 if (!flush && (kt_entry.vno == kvno - 1)) {
121                         DEBUG(5, (__location__ ": Saving previous (kvno %d) "
122                                   "entry for principal: %s.\n",
123                                   kvno - 1, princ_s));
124                         continue;
125                 }
126
127                 if (keep_old_entries) {
128                         DEBUG(5, (__location__ ": Saving old (kvno %d) "
129                                   "entry for principal: %s.\n",
130                                   kvno, princ_s));
131                         continue;
132                 }
133
134                 DEBUG(5, (__location__ ": Found old entry for principal: %s "
135                           "(kvno %d) - trying to remove it.\n",
136                           princ_s, kt_entry.vno));
137
138                 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
139                 ZERO_STRUCT(cursor);
140                 if (ret) {
141                         DEBUG(1, (__location__ ": krb5_kt_end_seq_get() "
142                                   "failed (%s)\n", error_message(ret)));
143                         goto out;
144                 }
145                 ret = krb5_kt_remove_entry(context, keytab, &kt_entry);
146                 if (ret) {
147                         DEBUG(1, (__location__ ": krb5_kt_remove_entry() "
148                                   "failed (%s)\n", error_message(ret)));
149                         goto out;
150                 }
151
152                 DEBUG(5, (__location__ ": removed old entry for principal: "
153                           "%s (kvno %d).\n", princ_s, kt_entry.vno));
154
155                 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
156                 if (ret) {
157                         DEBUG(1, (__location__ ": krb5_kt_start_seq() failed "
158                                   "(%s)\n", error_message(ret)));
159                         goto out;
160                 }
161                 ret = smb_krb5_kt_free_entry(context, &kt_entry);
162                 ZERO_STRUCT(kt_entry);
163                 if (ret) {
164                         DEBUG(1, (__location__ ": krb5_kt_remove_entry() "
165                                   "failed (%s)\n", error_message(ret)));
166                         goto out;
167                 }
168         }
169
170 out:
171         if (memcmp(&zero_kt_entry, &kt_entry, sizeof(krb5_keytab_entry))) {
172                 smb_krb5_kt_free_entry(context, &kt_entry);
173         }
174         if (keytab) {
175                 if (memcmp(&cursor, &zero_csr, sizeof(krb5_kt_cursor)) != 0) {
176                         krb5_kt_end_seq_get(context, keytab, &cursor);
177                 }
178         }
179
180         return ret;
181 }
182
183 int smb_krb5_kt_add_entry_ext(krb5_context context,
184                               krb5_keytab keytab,
185                               krb5_kvno kvno,
186                               const char *princ_s,
187                               krb5_enctype *enctypes,
188                               krb5_data password,
189                               bool no_salt,
190                               bool keep_old_entries)
191 {
192         krb5_error_code ret;
193         krb5_keytab_entry kt_entry;
194         krb5_principal princ = NULL;
195         int i;
196
197         ZERO_STRUCT(kt_entry);
198
199         ret = smb_krb5_parse_name(context, princ_s, &princ);
200         if (ret) {
201                 DEBUG(1, (__location__ ": smb_krb5_parse_name(%s) "
202                           "failed (%s)\n", princ_s, error_message(ret)));
203                 goto out;
204         }
205
206         /* Seek and delete old keytab entries */
207         ret = seek_and_delete_old_entries(context, keytab, kvno,
208                                           princ_s, princ, false,
209                                           keep_old_entries);
210         if (ret) {
211                 goto out;
212         }
213
214         /* If we get here, we have deleted all the old entries with kvno's
215          * not equal to the current kvno-1. */
216
217         /* Now add keytab entries for all encryption types */
218         for (i = 0; enctypes[i]; i++) {
219                 krb5_keyblock *keyp;
220
221                 keyp = KRB5_KT_KEY(&kt_entry);
222
223                 if (create_kerberos_key_from_string(context, princ,
224                                                     &password, keyp,
225                                                     enctypes[i], no_salt)) {
226                         continue;
227                 }
228
229                 kt_entry.principal = princ;
230                 kt_entry.vno       = kvno;
231
232                 DEBUG(3, (__location__ ": adding keytab entry for (%s) with "
233                           "encryption type (%d) and version (%d)\n",
234                           princ_s, enctypes[i], kt_entry.vno));
235                 ret = krb5_kt_add_entry(context, keytab, &kt_entry);
236                 krb5_free_keyblock_contents(context, keyp);
237                 ZERO_STRUCT(kt_entry);
238                 if (ret) {
239                         DEBUG(1, (__location__ ": adding entry to keytab "
240                                   "failed (%s)\n", error_message(ret)));
241                         goto out;
242                 }
243         }
244
245 out:
246         if (princ) {
247                 krb5_free_principal(context, princ);
248         }
249
250         return (int)ret;
251 }
252
253 static int smb_krb5_kt_add_entry(krb5_context context,
254                                  krb5_keytab keytab,
255                                  krb5_kvno kvno,
256                                  const char *princ_s,
257                                  krb5_enctype *enctypes,
258                                  krb5_data password)
259 {
260         return smb_krb5_kt_add_entry_ext(context,
261                                          keytab,
262                                          kvno,
263                                          princ_s,
264                                          enctypes,
265                                          password,
266                                          false,
267                                          false);
268 }
269
270 /**********************************************************************
271  Adds a single service principal, i.e. 'host' to the system keytab
272 ***********************************************************************/
273
274 int ads_keytab_add_entry(ADS_STRUCT *ads, const char *srvPrinc)
275 {
276         krb5_error_code ret = 0;
277         krb5_context context = NULL;
278         krb5_keytab keytab = NULL;
279         krb5_data password;
280         krb5_kvno kvno;
281         krb5_enctype enctypes[4] = {
282                 ENCTYPE_DES_CBC_CRC,
283                 ENCTYPE_DES_CBC_MD5,
284                 ENCTYPE_ARCFOUR_HMAC,
285                 0
286         };
287         char *princ_s = NULL;
288         char *short_princ_s = NULL;
289         char *password_s = NULL;
290         char *my_fqdn;
291         TALLOC_CTX *tmpctx = NULL;
292         char *machine_name;
293         ADS_STATUS aderr;
294
295         initialize_krb5_error_table();
296         ret = krb5_init_context(&context);
297         if (ret) {
298                 DEBUG(1, (__location__ ": could not krb5_init_context: %s\n",
299                           error_message(ret)));
300                 return -1;
301         }
302
303         ret = smb_krb5_open_keytab(context, NULL, True, &keytab);
304         if (ret) {
305                 DEBUG(1, (__location__ ": smb_krb5_open_keytab failed (%s)\n",
306                           error_message(ret)));
307                 goto out;
308         }
309
310         /* retrieve the password */
311         if (!secrets_init()) {
312                 DEBUG(1, (__location__ ": secrets_init failed\n"));
313                 ret = -1;
314                 goto out;
315         }
316         password_s = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
317         if (!password_s) {
318                 DEBUG(1, (__location__ ": failed to fetch machine password\n"));
319                 ret = -1;
320                 goto out;
321         }
322         ZERO_STRUCT(password);
323         password.data = password_s;
324         password.length = strlen(password_s);
325
326         /* we need the dNSHostName value here */
327         tmpctx = talloc_init(__location__);
328         if (!tmpctx) {
329                 DEBUG(0, (__location__ ": talloc_init() failed!\n"));
330                 ret = -1;
331                 goto out;
332         }
333
334         my_fqdn = ads_get_dnshostname(ads, tmpctx, global_myname());
335         if (!my_fqdn) {
336                 DEBUG(0, (__location__ ": unable to determine machine "
337                           "account's dns name in AD!\n"));
338                 ret = -1;
339                 goto out;
340         }
341
342         machine_name = ads_get_samaccountname(ads, tmpctx, global_myname());
343         if (!machine_name) {
344                 DEBUG(0, (__location__ ": unable to determine machine "
345                           "account's short name in AD!\n"));
346                 ret = -1;
347                 goto out;
348         }
349         /*strip the trailing '$' */
350         machine_name[strlen(machine_name)-1] = '\0';
351
352         /* Construct our principal */
353         if (strchr_m(srvPrinc, '@')) {
354                 /* It's a fully-named principal. */
355                 princ_s = talloc_asprintf(tmpctx, "%s", srvPrinc);
356                 if (!princ_s) {
357                         ret = -1;
358                         goto out;
359                 }
360         } else if (srvPrinc[strlen(srvPrinc)-1] == '$') {
361                 /* It's the machine account, as used by smbclient clients. */
362                 princ_s = talloc_asprintf(tmpctx, "%s@%s",
363                                           srvPrinc, lp_realm());
364                 if (!princ_s) {
365                         ret = -1;
366                         goto out;
367                 }
368         } else {
369                 /* It's a normal service principal.  Add the SPN now so that we
370                  * can obtain credentials for it and double-check the salt value
371                  * used to generate the service's keys. */
372
373                 princ_s = talloc_asprintf(tmpctx, "%s/%s@%s",
374                                           srvPrinc, my_fqdn, lp_realm());
375                 if (!princ_s) {
376                         ret = -1;
377                         goto out;
378                 }
379                 short_princ_s = talloc_asprintf(tmpctx, "%s/%s@%s",
380                                                 srvPrinc, machine_name,
381                                                 lp_realm());
382                 if (!princ_s) {
383                         ret = -1;
384                         goto out;
385                 }
386
387                 /* According to http://support.microsoft.com/kb/326985/en-us,
388                    certain principal names are automatically mapped to the
389                    host/... principal in the AD account.
390                    So only create these in the keytab, not in AD.  --jerry */
391
392                 if (!strequal(srvPrinc, "cifs") &&
393                     !strequal(srvPrinc, "host")) {
394                         DEBUG(3, (__location__ ": Attempting to add/update "
395                                   "'%s'\n", princ_s));
396
397                         aderr = ads_add_service_principal_name(ads,
398                                         global_myname(), my_fqdn, srvPrinc);
399                         if (!ADS_ERR_OK(aderr)) {
400                                 DEBUG(1, (__location__ ": failed to "
401                                          "ads_add_service_principal_name.\n"));
402                                 goto out;
403                         }
404                 }
405         }
406
407         kvno = (krb5_kvno)ads_get_machine_kvno(ads, global_myname());
408         if (kvno == -1) {
409                 /* -1 indicates failure, everything else is OK */
410                 DEBUG(1, (__location__ ": ads_get_machine_kvno failed to "
411                          "determine the system's kvno.\n"));
412                 ret = -1;
413                 goto out;
414         }
415
416         /* add the fqdn principal to the keytab */
417         ret = smb_krb5_kt_add_entry(context, keytab, kvno,
418                                     princ_s, enctypes, password);
419         if (ret) {
420                 DEBUG(1, (__location__ ": Failed to add entry to keytab\n"));
421                 goto out;
422         }
423
424         /* add the short principal name if we have one */
425         if (short_princ_s) {
426                 ret = smb_krb5_kt_add_entry(context, keytab, kvno,
427                                             short_princ_s, enctypes, password);
428                 if (ret) {
429                         DEBUG(1, (__location__
430                                   ": Failed to add short entry to keytab\n"));
431                         goto out;
432                 }
433         }
434
435 out:
436         TALLOC_FREE(tmpctx);
437
438         if (keytab) {
439                 krb5_kt_close(context, keytab);
440         }
441         if (context) {
442                 krb5_free_context(context);
443         }
444         return (int)ret;
445 }
446
447 /**********************************************************************
448  Flushes all entries from the system keytab.
449 ***********************************************************************/
450
451 int ads_keytab_flush(ADS_STRUCT *ads)
452 {
453         krb5_error_code ret = 0;
454         krb5_context context = NULL;
455         krb5_keytab keytab = NULL;
456         krb5_kvno kvno;
457         ADS_STATUS aderr;
458
459         initialize_krb5_error_table();
460         ret = krb5_init_context(&context);
461         if (ret) {
462                 DEBUG(1, (__location__ ": could not krb5_init_context: %s\n",
463                           error_message(ret)));
464                 return ret;
465         }
466
467         ret = smb_krb5_open_keytab(context, NULL, True, &keytab);
468         if (ret) {
469                 DEBUG(1, (__location__ ": smb_krb5_open_keytab failed (%s)\n",
470                           error_message(ret)));
471                 goto out;
472         }
473
474         kvno = (krb5_kvno)ads_get_machine_kvno(ads, global_myname());
475         if (kvno == -1) {
476                 /* -1 indicates a failure */
477                 DEBUG(1, (__location__ ": Error determining the kvno.\n"));
478                 goto out;
479         }
480
481         /* Seek and delete old keytab entries */
482         ret = seek_and_delete_old_entries(context, keytab, kvno,
483                                           NULL, NULL, true, false);
484         if (ret) {
485                 goto out;
486         }
487
488         aderr = ads_clear_service_principal_names(ads, global_myname());
489         if (!ADS_ERR_OK(aderr)) {
490                 DEBUG(1, (__location__ ": Error while clearing service "
491                           "principal listings in LDAP.\n"));
492                 goto out;
493         }
494
495 out:
496         if (keytab) {
497                 krb5_kt_close(context, keytab);
498         }
499         if (context) {
500                 krb5_free_context(context);
501         }
502         return ret;
503 }
504
505 /**********************************************************************
506  Adds all the required service principals to the system keytab.
507 ***********************************************************************/
508
509 int ads_keytab_create_default(ADS_STRUCT *ads)
510 {
511         krb5_error_code ret = 0;
512         krb5_context context = NULL;
513         krb5_keytab keytab = NULL;
514         krb5_kt_cursor cursor;
515         krb5_keytab_entry kt_entry;
516         krb5_kvno kvno;
517         int i, found = 0;
518         char *sam_account_name, *upn;
519         char **oldEntries = NULL, *princ_s[26];
520         TALLOC_CTX *tmpctx = NULL;
521         char *machine_name;
522
523         /* these are the main ones we need */
524         ret = ads_keytab_add_entry(ads, "host");
525         if (ret != 0) {
526                 DEBUG(1, (__location__ ": ads_keytab_add_entry failed while "
527                           "adding 'host' principal.\n"));
528                 return ret;
529         }
530
531
532 #if 0   /* don't create the CIFS/... keytab entries since no one except smbd
533            really needs them and we will fall back to verifying against
534            secrets.tdb */
535
536         ret = ads_keytab_add_entry(ads, "cifs"));
537         if (ret != 0 ) {
538                 DEBUG(1, (__location__ ": ads_keytab_add_entry failed while "
539                           "adding 'cifs'.\n"));
540                 return ret;
541         }
542 #endif
543
544         memset(princ_s, '\0', sizeof(princ_s));
545         ZERO_STRUCT(kt_entry);
546         ZERO_STRUCT(cursor);
547
548         initialize_krb5_error_table();
549         ret = krb5_init_context(&context);
550         if (ret) {
551                 DEBUG(1, (__location__ ": could not krb5_init_context: %s\n",
552                           error_message(ret)));
553                 return ret;
554         }
555
556         tmpctx = talloc_init(__location__);
557         if (!tmpctx) {
558                 DEBUG(0, (__location__ ": talloc_init() failed!\n"));
559                 ret = -1;
560                 goto done;
561         }
562
563         machine_name = talloc_strdup(tmpctx, global_myname());
564         if (!machine_name) {
565                 ret = -1;
566                 goto done;
567         }
568
569         /* now add the userPrincipalName and sAMAccountName entries */
570         sam_account_name = ads_get_samaccountname(ads, tmpctx, machine_name);
571         if (!sam_account_name) {
572                 DEBUG(0, (__location__ ": unable to determine machine "
573                           "account's name in AD!\n"));
574                 ret = -1;
575                 goto done;
576         }
577
578         /* upper case the sAMAccountName to make it easier for apps to
579            know what case to use in the keytab file */
580         strupper_m(sam_account_name);
581
582         ret = ads_keytab_add_entry(ads, sam_account_name);
583         if (ret != 0) {
584                 DEBUG(1, (__location__ ": ads_keytab_add_entry() failed "
585                           "while adding sAMAccountName (%s)\n",
586                           sam_account_name));
587                 goto done;
588         }
589
590         /* remember that not every machine account will have a upn */
591         upn = ads_get_upn(ads, tmpctx, machine_name);
592         if (upn) {
593                 ret = ads_keytab_add_entry(ads, upn);
594                 if (ret != 0) {
595                         DEBUG(1, (__location__ ": ads_keytab_add_entry() "
596                                   "failed while adding UPN (%s)\n", upn));
597                         goto done;
598                 }
599         }
600
601         /* Now loop through the keytab and update any other existing entries */
602         kvno = (krb5_kvno)ads_get_machine_kvno(ads, machine_name);
603         if (kvno == -1) {
604                 DEBUG(1, (__location__ ": ads_get_machine_kvno() failed to "
605                           "determine the system's kvno.\n"));
606                 goto done;
607         }
608
609         DEBUG(3, (__location__ ": Searching for keytab entries to preserve "
610                   "and update.\n"));
611
612         ret = smb_krb5_open_keytab(context, NULL, True, &keytab);
613         if (ret) {
614                 DEBUG(1, (__location__ ": smb_krb5_open_keytab failed (%s)\n",
615                           error_message(ret)));
616                 goto done;
617         }
618
619         ret = krb5_kt_start_seq_get(context, keytab, &cursor);
620         if (ret != KRB5_KT_END && ret != ENOENT ) {
621                 while ((ret = krb5_kt_next_entry(context, keytab,
622                                                  &kt_entry, &cursor)) == 0) {
623                         smb_krb5_kt_free_entry(context, &kt_entry);
624                         ZERO_STRUCT(kt_entry);
625                         found++;
626                 }
627         }
628         krb5_kt_end_seq_get(context, keytab, &cursor);
629         ZERO_STRUCT(cursor);
630
631         /*
632          * Hmmm. There is no "rewind" function for the keytab. This means we
633          * have a race condition where someone else could add entries after
634          * we've counted them. Re-open asap to minimise the race. JRA.
635          */
636         DEBUG(3, (__location__ ": Found %d entries in the keytab.\n", found));
637         if (!found) {
638                 goto done;
639         }
640
641         oldEntries = talloc_array(tmpctx, char *, found);
642         if (!oldEntries) {
643                 DEBUG(1, (__location__ ": Failed to allocate space to store "
644                           "the old keytab entries (talloc failed?).\n"));
645                 ret = -1;
646                 goto done;
647         }
648         memset(oldEntries, '\0', found * sizeof(char *));
649
650         ret = krb5_kt_start_seq_get(context, keytab, &cursor);
651         if (ret == KRB5_KT_END || ret == ENOENT) {
652                 krb5_kt_end_seq_get(context, keytab, &cursor);
653                 ZERO_STRUCT(cursor);
654                 goto done;
655         }
656
657         while (krb5_kt_next_entry(context, keytab, &kt_entry, &cursor) == 0) {
658                 if (kt_entry.vno != kvno) {
659                         char *ktprinc = NULL;
660                         char *p;
661
662                         /* This returns a malloc'ed string in ktprinc. */
663                         ret = smb_krb5_unparse_name(oldEntries, context,
664                                                     kt_entry.principal,
665                                                     &ktprinc);
666                         if (ret) {
667                                 DEBUG(1, (__location__
668                                          ": smb_krb5_unparse_name failed "
669                                          "(%s)\n", error_message(ret)));
670                                 goto done;
671                         }
672                         /*
673                          * From looking at the krb5 source they don't seem to
674                          * take locale or mb strings into account.
675                          * Maybe this is because they assume utf8 ?
676                          * In this case we may need to convert from utf8 to
677                          * mb charset here ? JRA.
678                          */
679                         p = strchr_m(ktprinc, '@');
680                         if (p) {
681                                 *p = '\0';
682                         }
683
684                         p = strchr_m(ktprinc, '/');
685                         if (p) {
686                                 *p = '\0';
687                         }
688                         for (i = 0; i < found; i++) {
689                                 if (!oldEntries[i]) {
690                                         oldEntries[i] = ktprinc;
691                                         break;
692                                 }
693                                 if (!strcmp(oldEntries[i], ktprinc)) {
694                                         TALLOC_FREE(ktprinc);
695                                         break;
696                                 }
697                         }
698                         if (i == found) {
699                                 TALLOC_FREE(ktprinc);
700                         }
701                 }
702                 smb_krb5_kt_free_entry(context, &kt_entry);
703                 ZERO_STRUCT(kt_entry);
704         }
705         ret = 0;
706         for (i = 0; oldEntries[i]; i++) {
707                 ret |= ads_keytab_add_entry(ads, oldEntries[i]);
708                 TALLOC_FREE(oldEntries[i]);
709         }
710         krb5_kt_end_seq_get(context, keytab, &cursor);
711         ZERO_STRUCT(cursor);
712
713 done:
714         TALLOC_FREE(oldEntries);
715         TALLOC_FREE(tmpctx);
716
717         {
718                 krb5_keytab_entry zero_kt_entry;
719                 ZERO_STRUCT(zero_kt_entry);
720                 if (memcmp(&zero_kt_entry, &kt_entry,
721                                 sizeof(krb5_keytab_entry))) {
722                         smb_krb5_kt_free_entry(context, &kt_entry);
723                 }
724         }
725         {
726                 krb5_kt_cursor zero_csr;
727                 ZERO_STRUCT(zero_csr);
728                 if ((memcmp(&cursor, &zero_csr,
729                                 sizeof(krb5_kt_cursor)) != 0) && keytab) {
730                         krb5_kt_end_seq_get(context, keytab, &cursor);
731                 }
732         }
733         if (keytab) {
734                 krb5_kt_close(context, keytab);
735         }
736         if (context) {
737                 krb5_free_context(context);
738         }
739         return ret;
740 }
741
742 /**********************************************************************
743  List system keytab.
744 ***********************************************************************/
745
746 int ads_keytab_list(const char *keytab_name)
747 {
748         krb5_error_code ret = 0;
749         krb5_context context = NULL;
750         krb5_keytab keytab = NULL;
751         krb5_kt_cursor cursor;
752         krb5_keytab_entry kt_entry;
753
754         ZERO_STRUCT(kt_entry);
755         ZERO_STRUCT(cursor);
756
757         initialize_krb5_error_table();
758         ret = krb5_init_context(&context);
759         if (ret) {
760                 DEBUG(1, (__location__ ": could not krb5_init_context: %s\n",
761                           error_message(ret)));
762                 return ret;
763         }
764
765         ret = smb_krb5_open_keytab(context, keytab_name, False, &keytab);
766         if (ret) {
767                 DEBUG(1, (__location__ ": smb_krb5_open_keytab failed (%s)\n",
768                           error_message(ret)));
769                 goto out;
770         }
771
772         ret = krb5_kt_start_seq_get(context, keytab, &cursor);
773         if (ret) {
774                 goto out;
775         }
776
777         printf("Vno  Type        Principal\n");
778
779         while (krb5_kt_next_entry(context, keytab, &kt_entry, &cursor) == 0) {
780
781                 char *princ_s = NULL;
782                 char *etype_s = NULL;
783                 krb5_enctype enctype = 0;
784
785                 ret = smb_krb5_unparse_name(talloc_tos(), context,
786                                             kt_entry.principal, &princ_s);
787                 if (ret) {
788                         goto out;
789                 }
790
791                 enctype = smb_get_enctype_from_kt_entry(&kt_entry);
792
793                 ret = smb_krb5_enctype_to_string(context, enctype, &etype_s);
794                 if (ret &&
795                     (asprintf(&etype_s, "UNKNOWN: %d\n", enctype) == -1)) {
796                         TALLOC_FREE(princ_s);
797                         goto out;
798                 }
799
800                 printf("%3d  %s\t\t %s\n", kt_entry.vno, etype_s, princ_s);
801
802                 TALLOC_FREE(princ_s);
803                 SAFE_FREE(etype_s);
804
805                 ret = smb_krb5_kt_free_entry(context, &kt_entry);
806                 if (ret) {
807                         goto out;
808                 }
809         }
810
811         ret = krb5_kt_end_seq_get(context, keytab, &cursor);
812         if (ret) {
813                 goto out;
814         }
815
816         /* Ensure we don't double free. */
817         ZERO_STRUCT(kt_entry);
818         ZERO_STRUCT(cursor);
819 out:
820
821         {
822                 krb5_keytab_entry zero_kt_entry;
823                 ZERO_STRUCT(zero_kt_entry);
824                 if (memcmp(&zero_kt_entry, &kt_entry,
825                                 sizeof(krb5_keytab_entry))) {
826                         smb_krb5_kt_free_entry(context, &kt_entry);
827                 }
828         }
829         {
830                 krb5_kt_cursor zero_csr;
831                 ZERO_STRUCT(zero_csr);
832                 if ((memcmp(&cursor, &zero_csr,
833                                 sizeof(krb5_kt_cursor)) != 0) && keytab) {
834                         krb5_kt_end_seq_get(context, keytab, &cursor);
835                 }
836         }
837
838         if (keytab) {
839                 krb5_kt_close(context, keytab);
840         }
841         if (context) {
842                 krb5_free_context(context);
843         }
844         return ret;
845 }
846
847 #endif /* HAVE_KRB5 */