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