s3:libads: net ads keytab list fails with "Key table name malformed"
[samba.git] / source3 / libads / kerberos_keytab.c
index 1daa1a92ad8b196da8c91129901c082b5d68a42c..ffd100c5636b273b0a76e059dc76f1d93133f290 100644 (file)
 
 #ifdef HAVE_KRB5
 
-/**********************************************************************
-**********************************************************************/
-
-static krb5_error_code seek_and_delete_old_entries(krb5_context context,
-                                                  krb5_keytab keytab,
-                                                  krb5_kvno kvno,
-                                                  const char *princ_s,
-                                                  krb5_principal princ,
-                                                  bool flush,
-                                                  bool keep_old_entries)
-{
-       krb5_error_code ret;
-       krb5_kt_cursor cursor;
-       krb5_kt_cursor zero_csr;
-       krb5_keytab_entry kt_entry;
-       krb5_keytab_entry zero_kt_entry;
-       char *ktprinc = NULL;
-
-       ZERO_STRUCT(cursor);
-       ZERO_STRUCT(zero_csr);
-       ZERO_STRUCT(kt_entry);
-       ZERO_STRUCT(zero_kt_entry);
-
-       ret = krb5_kt_start_seq_get(context, keytab, &cursor);
-       if (ret == KRB5_KT_END && ret == ENOENT ) {
-               /* no entries */
-               return 0;
-       }
-
-       DEBUG(3, (__location__ ": Will try to delete old keytab entries\n"));
-       while (!krb5_kt_next_entry(context, keytab, &kt_entry, &cursor)) {
-               bool name_ok = False;
-
-               if (!flush && (princ_s != NULL)) {
-                       ret = smb_krb5_unparse_name(talloc_tos(), context,
-                                                   kt_entry.principal,
-                                                   &ktprinc);
-                       if (ret) {
-                               DEBUG(1, (__location__
-                                         ": smb_krb5_unparse_name failed "
-                                         "(%s)\n", error_message(ret)));
-                               goto out;
-                       }
+#ifdef HAVE_ADS
 
-#ifdef HAVE_KRB5_KT_COMPARE
-                       name_ok = krb5_kt_compare(context, &kt_entry,
-                                                 princ, 0, 0);
-#else
-                       name_ok = (strcmp(ktprinc, princ_s) == 0);
+/* This MAX_NAME_LEN is a constant defined in krb5.h */
+#ifndef MAX_KEYTAB_NAME_LEN
+#define MAX_KEYTAB_NAME_LEN 1100
 #endif
 
-                       if (!name_ok) {
-                               DEBUG(10, (__location__ ": ignoring keytab "
-                                          "entry principal %s, kvno = %d\n",
-                                          ktprinc, kt_entry.vno));
-
-                               /* Not a match,
-                                * just free this entry and continue. */
-                               ret = smb_krb5_kt_free_entry(context,
-                                                            &kt_entry);
-                               ZERO_STRUCT(kt_entry);
-                               if (ret) {
-                                       DEBUG(1, (__location__
-                                                 ": smb_krb5_kt_free_entry "
-                                                 "failed (%s)\n",
-                                                 error_message(ret)));
-                                       goto out;
-                               }
-
-                               TALLOC_FREE(ktprinc);
-                               continue;
-                       }
-
-                       TALLOC_FREE(ktprinc);
-               }
-
-               /*------------------------------------------------------------
-                * Save the entries with kvno - 1. This is what microsoft does
-                * to allow people with existing sessions that have kvno - 1
-                * to still work. Otherwise, when the password for the machine
-                * changes, all kerberizied sessions will 'break' until either
-                * the client reboots or the client's session key expires and
-                * they get a new session ticket with the new kvno.
-                */
-
-               if (!flush && (kt_entry.vno == kvno - 1)) {
-                       DEBUG(5, (__location__ ": Saving previous (kvno %d) "
-                                 "entry for principal: %s.\n",
-                                 kvno - 1, princ_s));
-                       continue;
-               }
-
-               if (keep_old_entries) {
-                       DEBUG(5, (__location__ ": Saving old (kvno %d) "
-                                 "entry for principal: %s.\n",
-                                 kvno, princ_s));
-                       continue;
-               }
-
-               DEBUG(5, (__location__ ": Found old entry for principal: %s "
-                         "(kvno %d) - trying to remove it.\n",
-                         princ_s, kt_entry.vno));
-
-               ret = krb5_kt_end_seq_get(context, keytab, &cursor);
-               ZERO_STRUCT(cursor);
-               if (ret) {
-                       DEBUG(1, (__location__ ": krb5_kt_end_seq_get() "
-                                 "failed (%s)\n", error_message(ret)));
-                       goto out;
-               }
-               ret = krb5_kt_remove_entry(context, keytab, &kt_entry);
-               if (ret) {
-                       DEBUG(1, (__location__ ": krb5_kt_remove_entry() "
-                                 "failed (%s)\n", error_message(ret)));
-                       goto out;
-               }
-
-               DEBUG(5, (__location__ ": removed old entry for principal: "
-                         "%s (kvno %d).\n", princ_s, kt_entry.vno));
+static krb5_error_code ads_keytab_open(krb5_context context,
+                                      krb5_keytab *keytab)
+{
+       char keytab_str[MAX_KEYTAB_NAME_LEN] = {0};
+       const char *keytab_name = NULL;
+       krb5_error_code ret = 0;
 
-               ret = krb5_kt_start_seq_get(context, keytab, &cursor);
-               if (ret) {
-                       DEBUG(1, (__location__ ": krb5_kt_start_seq() failed "
-                                 "(%s)\n", error_message(ret)));
+       switch (lp_kerberos_method()) {
+       case KERBEROS_VERIFY_SYSTEM_KEYTAB:
+       case KERBEROS_VERIFY_SECRETS_AND_KEYTAB:
+               ret = krb5_kt_default_name(context,
+                                          keytab_str,
+                                          sizeof(keytab_str) - 2);
+               if (ret != 0) {
+                       DBG_WARNING("Failed to get default keytab name");
                        goto out;
                }
-               ret = smb_krb5_kt_free_entry(context, &kt_entry);
-               ZERO_STRUCT(kt_entry);
-               if (ret) {
-                       DEBUG(1, (__location__ ": krb5_kt_remove_entry() "
-                                 "failed (%s)\n", error_message(ret)));
-                       goto out;
-               }
-       }
-
-out:
-       if (memcmp(&zero_kt_entry, &kt_entry, sizeof(krb5_keytab_entry))) {
-               smb_krb5_kt_free_entry(context, &kt_entry);
-       }
-       if (keytab) {
-               if (memcmp(&cursor, &zero_csr, sizeof(krb5_kt_cursor)) != 0) {
-                       krb5_kt_end_seq_get(context, keytab, &cursor);
-               }
-       }
-
-       return ret;
-}
-
-int smb_krb5_kt_add_entry_ext(krb5_context context,
-                             krb5_keytab keytab,
-                             krb5_kvno kvno,
-                             const char *princ_s,
-                             krb5_enctype *enctypes,
-                             krb5_data password,
-                             bool no_salt,
-                             bool keep_old_entries)
-{
-       krb5_error_code ret;
-       krb5_keytab_entry kt_entry;
-       krb5_principal princ = NULL;
-       int i;
-
-       ZERO_STRUCT(kt_entry);
-
-       ret = smb_krb5_parse_name(context, princ_s, &princ);
-       if (ret) {
-               DEBUG(1, (__location__ ": smb_krb5_parse_name(%s) "
-                         "failed (%s)\n", princ_s, error_message(ret)));
+               keytab_name = keytab_str;
+               break;
+       case KERBEROS_VERIFY_DEDICATED_KEYTAB:
+               keytab_name = lp_dedicated_keytab_file();
+               break;
+       default:
+               DBG_ERR("Invalid kerberos method set (%d)\n",
+                       lp_kerberos_method());
+               ret = KRB5_KT_BADNAME;
                goto out;
        }
 
-       /* Seek and delete old keytab entries */
-       ret = seek_and_delete_old_entries(context, keytab, kvno,
-                                         princ_s, princ, false,
-                                         keep_old_entries);
-       if (ret) {
+       if (keytab_name == NULL || keytab_name[0] == '\0') {
+               DBG_ERR("Invalid keytab name\n");
+               ret = KRB5_KT_BADNAME;
                goto out;
        }
 
-       /* If we get here, we have deleted all the old entries with kvno's
-        * not equal to the current kvno-1. */
-
-       /* Now add keytab entries for all encryption types */
-       for (i = 0; enctypes[i]; i++) {
-               krb5_keyblock *keyp;
-
-               keyp = KRB5_KT_KEY(&kt_entry);
-
-               if (create_kerberos_key_from_string(context, princ,
-                                                   &password, keyp,
-                                                   enctypes[i], no_salt)) {
-                       continue;
-               }
-
-               kt_entry.principal = princ;
-               kt_entry.vno       = kvno;
-
-               DEBUG(3, (__location__ ": adding keytab entry for (%s) with "
-                         "encryption type (%d) and version (%d)\n",
-                         princ_s, enctypes[i], kt_entry.vno));
-               ret = krb5_kt_add_entry(context, keytab, &kt_entry);
-               krb5_free_keyblock_contents(context, keyp);
-               ZERO_STRUCT(kt_entry);
-               if (ret) {
-                       DEBUG(1, (__location__ ": adding entry to keytab "
-                                 "failed (%s)\n", error_message(ret)));
-                       goto out;
-               }
+       ret = smb_krb5_kt_open(context, keytab_name, true, keytab);
+       if (ret != 0) {
+               DBG_WARNING("smb_krb5_kt_open failed (%s)\n",
+                           error_message(ret));
+               goto out;
        }
 
 out:
-       if (princ) {
-               krb5_free_principal(context, princ);
-       }
-
-       return (int)ret;
-}
-
-static int smb_krb5_kt_add_entry(krb5_context context,
-                                krb5_keytab keytab,
-                                krb5_kvno kvno,
-                                const char *princ_s,
-                                krb5_enctype *enctypes,
-                                krb5_data password)
-{
-       return smb_krb5_kt_add_entry_ext(context,
-                                        keytab,
-                                        kvno,
-                                        princ_s,
-                                        enctypes,
-                                        password,
-                                        false,
-                                        false);
+       return ret;
 }
 
 /**********************************************************************
@@ -278,19 +96,27 @@ int ads_keytab_add_entry(ADS_STRUCT *ads, const char *srvPrinc)
        krb5_keytab keytab = NULL;
        krb5_data password;
        krb5_kvno kvno;
-        krb5_enctype enctypes[4] = {
+        krb5_enctype enctypes[6] = {
                ENCTYPE_DES_CBC_CRC,
                ENCTYPE_DES_CBC_MD5,
+#ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
+               ENCTYPE_AES128_CTS_HMAC_SHA1_96,
+#endif
+#ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
+               ENCTYPE_AES256_CTS_HMAC_SHA1_96,
+#endif
                ENCTYPE_ARCFOUR_HMAC,
                0
        };
        char *princ_s = NULL;
        char *short_princ_s = NULL;
+       char *salt_princ_s = NULL;
        char *password_s = NULL;
        char *my_fqdn;
        TALLOC_CTX *tmpctx = NULL;
        char *machine_name;
        ADS_STATUS aderr;
+       int i;
 
        initialize_krb5_error_table();
        ret = krb5_init_context(&context);
@@ -300,10 +126,8 @@ int ads_keytab_add_entry(ADS_STRUCT *ads, const char *srvPrinc)
                return -1;
        }
 
-       ret = smb_krb5_open_keytab(context, NULL, True, &keytab);
-       if (ret) {
-               DEBUG(1, (__location__ ": smb_krb5_open_keytab failed (%s)\n",
-                         error_message(ret)));
+       ret = ads_keytab_open(context, &keytab);
+       if (ret != 0) {
                goto out;
        }
 
@@ -331,7 +155,7 @@ int ads_keytab_add_entry(ADS_STRUCT *ads, const char *srvPrinc)
                goto out;
        }
 
-       my_fqdn = ads_get_dnshostname(ads, tmpctx, global_myname());
+       my_fqdn = ads_get_dnshostname(ads, tmpctx, lp_netbios_name());
        if (!my_fqdn) {
                DEBUG(0, (__location__ ": unable to determine machine "
                          "account's dns name in AD!\n"));
@@ -339,7 +163,7 @@ int ads_keytab_add_entry(ADS_STRUCT *ads, const char *srvPrinc)
                goto out;
        }
 
-       machine_name = ads_get_samaccountname(ads, tmpctx, global_myname());
+       machine_name = ads_get_samaccountname(ads, tmpctx, lp_netbios_name());
        if (!machine_name) {
                DEBUG(0, (__location__ ": unable to determine machine "
                          "account's short name in AD!\n"));
@@ -379,7 +203,7 @@ int ads_keytab_add_entry(ADS_STRUCT *ads, const char *srvPrinc)
                short_princ_s = talloc_asprintf(tmpctx, "%s/%s@%s",
                                                srvPrinc, machine_name,
                                                lp_realm());
-               if (!princ_s) {
+               if (short_princ_s == NULL) {
                        ret = -1;
                        goto out;
                }
@@ -395,7 +219,7 @@ int ads_keytab_add_entry(ADS_STRUCT *ads, const char *srvPrinc)
                                  "'%s'\n", princ_s));
 
                        aderr = ads_add_service_principal_name(ads,
-                                       global_myname(), my_fqdn, srvPrinc);
+                                       lp_netbios_name(), my_fqdn, srvPrinc);
                        if (!ADS_ERR_OK(aderr)) {
                                DEBUG(1, (__location__ ": failed to "
                                         "ads_add_service_principal_name.\n"));
@@ -404,7 +228,7 @@ int ads_keytab_add_entry(ADS_STRUCT *ads, const char *srvPrinc)
                }
        }
 
-       kvno = (krb5_kvno)ads_get_machine_kvno(ads, global_myname());
+       kvno = (krb5_kvno)ads_get_machine_kvno(ads, lp_netbios_name());
        if (kvno == -1) {
                /* -1 indicates failure, everything else is OK */
                DEBUG(1, (__location__ ": ads_get_machine_kvno failed to "
@@ -413,26 +237,51 @@ int ads_keytab_add_entry(ADS_STRUCT *ads, const char *srvPrinc)
                goto out;
        }
 
-       /* add the fqdn principal to the keytab */
-       ret = smb_krb5_kt_add_entry(context, keytab, kvno,
-                                   princ_s, enctypes, password);
-       if (ret) {
-               DEBUG(1, (__location__ ": Failed to add entry to keytab\n"));
+       salt_princ_s = kerberos_secrets_fetch_salt_princ();
+       if (salt_princ_s == NULL) {
+               DBG_WARNING("kerberos_secrets_fetch_salt_princ() failed\n");
+               ret = -1;
                goto out;
        }
 
-       /* add the short principal name if we have one */
-       if (short_princ_s) {
-               ret = smb_krb5_kt_add_entry(context, keytab, kvno,
-                                           short_princ_s, enctypes, password);
+       for (i = 0; enctypes[i]; i++) {
+
+               /* add the fqdn principal to the keytab */
+               ret = smb_krb5_kt_add_entry(context,
+                                           keytab,
+                                           kvno,
+                                           princ_s,
+                                           salt_princ_s,
+                                           enctypes[i],
+                                           &password,
+                                           false,
+                                           false);
                if (ret) {
-                       DEBUG(1, (__location__
-                                 ": Failed to add short entry to keytab\n"));
+                       DEBUG(1, (__location__ ": Failed to add entry to keytab\n"));
                        goto out;
                }
+
+               /* add the short principal name if we have one */
+               if (short_princ_s) {
+                       ret = smb_krb5_kt_add_entry(context,
+                                                   keytab,
+                                                   kvno,
+                                                   short_princ_s,
+                                                   salt_princ_s,
+                                                   enctypes[i],
+                                                   &password,
+                                                   false,
+                                                   false);
+                       if (ret) {
+                               DEBUG(1, (__location__
+                                         ": Failed to add short entry to keytab\n"));
+                               goto out;
+                       }
+               }
        }
 
 out:
+       SAFE_FREE(salt_princ_s);
        TALLOC_FREE(tmpctx);
 
        if (keytab) {
@@ -464,14 +313,12 @@ int ads_keytab_flush(ADS_STRUCT *ads)
                return ret;
        }
 
-       ret = smb_krb5_open_keytab(context, NULL, True, &keytab);
-       if (ret) {
-               DEBUG(1, (__location__ ": smb_krb5_open_keytab failed (%s)\n",
-                         error_message(ret)));
+       ret = ads_keytab_open(context, &keytab);
+       if (ret != 0) {
                goto out;
        }
 
-       kvno = (krb5_kvno)ads_get_machine_kvno(ads, global_myname());
+       kvno = (krb5_kvno)ads_get_machine_kvno(ads, lp_netbios_name());
        if (kvno == -1) {
                /* -1 indicates a failure */
                DEBUG(1, (__location__ ": Error determining the kvno.\n"));
@@ -479,13 +326,19 @@ int ads_keytab_flush(ADS_STRUCT *ads)
        }
 
        /* Seek and delete old keytab entries */
-       ret = seek_and_delete_old_entries(context, keytab, kvno,
-                                         NULL, NULL, true, false);
+       ret = smb_krb5_kt_seek_and_delete_old_entries(context,
+                                                     keytab,
+                                                     kvno,
+                                                     ENCTYPE_NULL,
+                                                     NULL,
+                                                     NULL,
+                                                     true,
+                                                     false);
        if (ret) {
                goto out;
        }
 
-       aderr = ads_clear_service_principal_names(ads, global_myname());
+       aderr = ads_clear_service_principal_names(ads, lp_netbios_name());
        if (!ADS_ERR_OK(aderr)) {
                DEBUG(1, (__location__ ": Error while clearing service "
                          "principal listings in LDAP.\n"));
@@ -511,104 +364,147 @@ int ads_keytab_create_default(ADS_STRUCT *ads)
        krb5_error_code ret = 0;
        krb5_context context = NULL;
        krb5_keytab keytab = NULL;
-       krb5_kt_cursor cursor;
-       krb5_keytab_entry kt_entry;
+       krb5_kt_cursor cursor = {0};
+       krb5_keytab_entry kt_entry = {0};
        krb5_kvno kvno;
-       int i, found = 0;
+       size_t found = 0;
        char *sam_account_name, *upn;
        char **oldEntries = NULL, *princ_s[26];
-       TALLOC_CTX *ctx = NULL;
-       fstring machine_name;
+       TALLOC_CTX *frame;
+       char *machine_name;
+       char **spn_array;
+       size_t num_spns;
+       size_t i;
+       ADS_STATUS status;
 
-       memset(princ_s, '\0', sizeof(princ_s));
+       ZERO_STRUCT(kt_entry);
+       ZERO_STRUCT(cursor);
 
-       fstrcpy( machine_name, global_myname() );
+       frame = talloc_stackframe();
+       if (frame == NULL) {
+               ret = -1;
+               goto done;
+       }
 
-       /* these are the main ones we need */
-       
-       if ( (ret = ads_keytab_add_entry(ads, "host") ) != 0 ) {
-               DEBUG(1,("ads_keytab_create_default: ads_keytab_add_entry failed while adding 'host'.\n"));
-               return ret;
+       status = ads_get_service_principal_names(frame,
+                                                ads,
+                                                lp_netbios_name(),
+                                                &spn_array,
+                                                &num_spns);
+       if (!ADS_ERR_OK(status)) {
+               ret = -1;
+               goto done;
+       }
+
+       for (i = 0; i < num_spns; i++) {
+               char *srv_princ;
+               char *p;
+
+               srv_princ = strlower_talloc(frame, spn_array[i]);
+               if (srv_princ == NULL) {
+                       ret = -1;
+                       goto done;
+               }
+
+               p = strchr_m(srv_princ, '/');
+               if (p == NULL) {
+                       continue;
+               }
+               p[0] = '\0';
+
+               /* Add the SPNs found on the DC */
+               ret = ads_keytab_add_entry(ads, srv_princ);
+               if (ret != 0) {
+                       DEBUG(1, ("ads_keytab_add_entry failed while "
+                                 "adding '%s' principal.\n",
+                                 spn_array[i]));
+                       goto done;
+               }
        }
 
+#if 0  /* don't create the CIFS/... keytab entries since no one except smbd
+          really needs them and we will fall back to verifying against
+          secrets.tdb */
 
-#if 0  /* don't create the CIFS/... keytab entries since no one except smbd 
-          really needs them and we will fall back to verifying against secrets.tdb */
-          
-       if ( (ret = ads_keytab_add_entry(ads, "cifs")) != 0 ) {
-               DEBUG(1,("ads_keytab_create_default: ads_keytab_add_entry failed while adding 'cifs'.\n"));
+       ret = ads_keytab_add_entry(ads, "cifs"));
+       if (ret != 0 ) {
+               DEBUG(1, (__location__ ": ads_keytab_add_entry failed while "
+                         "adding 'cifs'.\n"));
                return ret;
        }
 #endif
 
-       if ( (ctx = talloc_init("ads_keytab_create_default")) == NULL ) {
-               DEBUG(0,("ads_keytab_create_default: talloc() failed!\n"));
-               return -1;
+       memset(princ_s, '\0', sizeof(princ_s));
+
+       initialize_krb5_error_table();
+       ret = krb5_init_context(&context);
+       if (ret) {
+               DEBUG(1, (__location__ ": could not krb5_init_context: %s\n",
+                         error_message(ret)));
+               goto done;
        }
 
-       /* now add the userPrincipalName and sAMAccountName entries */
+       machine_name = talloc_strdup(frame, lp_netbios_name());
+       if (!machine_name) {
+               ret = -1;
+               goto done;
+       }
 
-       if ( (sam_account_name = ads_get_samaccountname( ads, ctx, machine_name)) == NULL ) {
-               DEBUG(0,("ads_keytab_add_entry: unable to determine machine account's name in AD!\n"));
-               TALLOC_FREE( ctx );
-               return -1;      
+       /* now add the userPrincipalName and sAMAccountName entries */
+       sam_account_name = ads_get_samaccountname(ads, frame, machine_name);
+       if (!sam_account_name) {
+               DEBUG(0, (__location__ ": unable to determine machine "
+                         "account's name in AD!\n"));
+               ret = -1;
+               goto done;
        }
 
-       /* upper case the sAMAccountName to make it easier for apps to 
+       /* upper case the sAMAccountName to make it easier for apps to
           know what case to use in the keytab file */
+       if (!strupper_m(sam_account_name)) {
+               ret = -1;
+               goto done;
+       }
 
-       strupper_m( sam_account_name ); 
-
-       if ( (ret = ads_keytab_add_entry(ads, sam_account_name )) != 0 ) {
-               DEBUG(1,("ads_keytab_create_default: ads_keytab_add_entry failed while adding sAMAccountName (%s)\n",
-                       sam_account_name));
-               return ret;
+       ret = ads_keytab_add_entry(ads, sam_account_name);
+       if (ret != 0) {
+               DEBUG(1, (__location__ ": ads_keytab_add_entry() failed "
+                         "while adding sAMAccountName (%s)\n",
+                         sam_account_name));
+               goto done;
        }
-       
+
        /* remember that not every machine account will have a upn */
-               
-       upn = ads_get_upn( ads, ctx, machine_name);
-       if ( upn ) {
-               if ( (ret = ads_keytab_add_entry(ads, upn)) != 0 ) {
-                       DEBUG(1,("ads_keytab_create_default: ads_keytab_add_entry failed while adding UPN (%s)\n",
-                               upn));
-                       TALLOC_FREE( ctx );
-                       return ret;
+       upn = ads_get_upn(ads, frame, machine_name);
+       if (upn) {
+               ret = ads_keytab_add_entry(ads, upn);
+               if (ret != 0) {
+                       DEBUG(1, (__location__ ": ads_keytab_add_entry() "
+                                 "failed while adding UPN (%s)\n", upn));
+                       goto done;
                }
        }
 
-       /* Now loop through the keytab and update any other existing entries... */
-       
-       kvno = (krb5_kvno) ads_get_machine_kvno(ads, machine_name);
-       if (kvno == -1) {
-               DEBUG(1,("ads_keytab_create_default: ads_get_machine_kvno failed to determine the system's kvno.\n"));
-               TALLOC_FREE(ctx);
-               return -1;
+       /* Now loop through the keytab and update any other existing entries */
+       kvno = (krb5_kvno)ads_get_machine_kvno(ads, machine_name);
+       if (kvno == (krb5_kvno)-1) {
+               DEBUG(1, (__location__ ": ads_get_machine_kvno() failed to "
+                         "determine the system's kvno.\n"));
+               goto done;
        }
-       
-       DEBUG(3,("ads_keytab_create_default: Searching for keytab entries to "
-               "preserve and update.\n"));
 
-       ZERO_STRUCT(kt_entry);
-       ZERO_STRUCT(cursor);
-
-       initialize_krb5_error_table();
-       ret = krb5_init_context(&context);
-       if (ret) {
-               DEBUG(1,("ads_keytab_create_default: could not krb5_init_context: %s\n",error_message(ret)));
-               TALLOC_FREE(ctx);
-               return ret;
-       }
+       DEBUG(3, (__location__ ": Searching for keytab entries to preserve "
+                 "and update.\n"));
 
-       ret = smb_krb5_open_keytab(context, NULL, True, &keytab);
-       if (ret) {
-               DEBUG(1,("ads_keytab_create_default: smb_krb5_open_keytab failed (%s)\n", error_message(ret)));
+       ret = ads_keytab_open(context, &keytab);
+       if (ret != 0) {
                goto done;
        }
 
        ret = krb5_kt_start_seq_get(context, keytab, &cursor);
        if (ret != KRB5_KT_END && ret != ENOENT ) {
-               while ((ret = krb5_kt_next_entry(context, keytab, &kt_entry, &cursor)) == 0) {
+               while ((ret = krb5_kt_next_entry(context, keytab,
+                                                &kt_entry, &cursor)) == 0) {
                        smb_krb5_kt_free_entry(context, &kt_entry);
                        ZERO_STRUCT(kt_entry);
                        found++;
@@ -618,104 +514,108 @@ int ads_keytab_create_default(ADS_STRUCT *ads)
        ZERO_STRUCT(cursor);
 
        /*
-        * Hmmm. There is no "rewind" function for the keytab. This means we have a race condition
-        * where someone else could add entries after we've counted them. Re-open asap to minimise
-        * the race. JRA.
+        * Hmmm. There is no "rewind" function for the keytab. This means we
+        * have a race condition where someone else could add entries after
+        * we've counted them. Re-open asap to minimise the race. JRA.
         */
-       
-       DEBUG(3, ("ads_keytab_create_default: Found %d entries in the keytab.\n", found));
+       DEBUG(3, (__location__ ": Found %zd entries in the keytab.\n", found));
        if (!found) {
                goto done;
        }
-       oldEntries = talloc_array(ctx, char *, found );
+
+       oldEntries = talloc_zero_array(frame, char *, found + 1);
        if (!oldEntries) {
-               DEBUG(1,("ads_keytab_create_default: Failed to allocate space to store the old keytab entries (malloc failed?).\n"));
+               DEBUG(1, (__location__ ": Failed to allocate space to store "
+                         "the old keytab entries (talloc failed?).\n"));
                ret = -1;
                goto done;
        }
-       memset(oldEntries, '\0', found * sizeof(char *));
 
        ret = krb5_kt_start_seq_get(context, keytab, &cursor);
-       if (ret != KRB5_KT_END && ret != ENOENT ) {
-               while (krb5_kt_next_entry(context, keytab, &kt_entry, &cursor) == 0) {
-                       if (kt_entry.vno != kvno) {
-                               char *ktprinc = NULL;
-                               char *p;
-
-                               /* This returns a malloc'ed string in ktprinc. */
-                               ret = smb_krb5_unparse_name(oldEntries, context, kt_entry.principal, &ktprinc);
-                               if (ret) {
-                                       DEBUG(1,("smb_krb5_unparse_name failed (%s)\n", error_message(ret)));
-                                       goto done;
-                               }
-                               /*
-                                * From looking at the krb5 source they don't seem to take locale
-                                * or mb strings into account. Maybe this is because they assume utf8 ?
-                                * In this case we may need to convert from utf8 to mb charset here ? JRA.
-                                */
-                               p = strchr_m(ktprinc, '@');
-                               if (p) {
-                                       *p = '\0';
-                               }
+       if (ret == KRB5_KT_END || ret == ENOENT) {
+               krb5_kt_end_seq_get(context, keytab, &cursor);
+               ZERO_STRUCT(cursor);
+               goto done;
+       }
 
-                               p = strchr_m(ktprinc, '/');
-                               if (p) {
-                                       *p = '\0';
-                               }
-                               for (i = 0; i < found; i++) {
-                                       if (!oldEntries[i]) {
-                                               oldEntries[i] = ktprinc;
-                                               break;
-                                       }
-                                       if (!strcmp(oldEntries[i], ktprinc)) {
-                                               TALLOC_FREE(ktprinc);
-                                               break;
-                                       }
+       while (krb5_kt_next_entry(context, keytab, &kt_entry, &cursor) == 0) {
+               if (kt_entry.vno != kvno) {
+                       char *ktprinc = NULL;
+                       char *p;
+
+                       /* This returns a malloc'ed string in ktprinc. */
+                       ret = smb_krb5_unparse_name(oldEntries, context,
+                                                   kt_entry.principal,
+                                                   &ktprinc);
+                       if (ret) {
+                               DEBUG(1, (__location__
+                                        ": smb_krb5_unparse_name failed "
+                                        "(%s)\n", error_message(ret)));
+                               goto done;
+                       }
+                       /*
+                        * From looking at the krb5 source they don't seem to
+                        * take locale or mb strings into account.
+                        * Maybe this is because they assume utf8 ?
+                        * In this case we may need to convert from utf8 to
+                        * mb charset here ? JRA.
+                        */
+                       p = strchr_m(ktprinc, '@');
+                       if (p) {
+                               *p = '\0';
+                       }
+
+                       p = strchr_m(ktprinc, '/');
+                       if (p) {
+                               *p = '\0';
+                       }
+                       for (i = 0; i < found; i++) {
+                               if (!oldEntries[i]) {
+                                       oldEntries[i] = ktprinc;
+                                       break;
                                }
-                               if (i == found) {
+                               if (!strcmp(oldEntries[i], ktprinc)) {
                                        TALLOC_FREE(ktprinc);
+                                       break;
                                }
                        }
-                       smb_krb5_kt_free_entry(context, &kt_entry);
-                       ZERO_STRUCT(kt_entry);
-               }
-               ret = 0;
-               for (i = 0; oldEntries[i]; i++) {
-                       ret |= ads_keytab_add_entry(ads, oldEntries[i]);
-                       TALLOC_FREE(oldEntries[i]);
+                       if (i == found) {
+                               TALLOC_FREE(ktprinc);
+                       }
                }
-               krb5_kt_end_seq_get(context, keytab, &cursor);
+               smb_krb5_kt_free_entry(context, &kt_entry);
+               ZERO_STRUCT(kt_entry);
        }
+       krb5_kt_end_seq_get(context, keytab, &cursor);
        ZERO_STRUCT(cursor);
 
-done:
+       ret = 0;
+       for (i = 0; oldEntries[i]; i++) {
+               ret |= ads_keytab_add_entry(ads, oldEntries[i]);
+               TALLOC_FREE(oldEntries[i]);
+       }
 
+done:
        TALLOC_FREE(oldEntries);
-       TALLOC_FREE(ctx);
+       TALLOC_FREE(frame);
 
-       {
-               krb5_keytab_entry zero_kt_entry;
-               ZERO_STRUCT(zero_kt_entry);
-               if (memcmp(&zero_kt_entry, &kt_entry, sizeof(krb5_keytab_entry))) {
+       if (context) {
+               if (!all_zero((uint8_t *)&kt_entry, sizeof(kt_entry))) {
                        smb_krb5_kt_free_entry(context, &kt_entry);
                }
-       }
-       {
-               krb5_kt_cursor zero_csr;
-               ZERO_STRUCT(zero_csr);
-               if ((memcmp(&cursor, &zero_csr, sizeof(krb5_kt_cursor)) != 0) && keytab) {
-                       krb5_kt_end_seq_get(context, keytab, &cursor);  
+               if (!all_zero((uint8_t *)&cursor, sizeof(cursor)) && keytab) {
+                       krb5_kt_end_seq_get(context, keytab, &cursor);
+               }
+               if (keytab) {
+                       krb5_kt_close(context, keytab);
                }
-       }
-       if (keytab) {
-               krb5_kt_close(context, keytab);
-       }
-       if (context) {
                krb5_free_context(context);
        }
        return ret;
 }
 
+#endif /* HAVE_ADS */
+
 /**********************************************************************
  List system keytab.
 ***********************************************************************/
@@ -734,46 +634,52 @@ int ads_keytab_list(const char *keytab_name)
        initialize_krb5_error_table();
        ret = krb5_init_context(&context);
        if (ret) {
-               DEBUG(1,("ads_keytab_list: could not krb5_init_context: %s\n",error_message(ret)));
+               DEBUG(1, (__location__ ": could not krb5_init_context: %s\n",
+                         error_message(ret)));
                return ret;
        }
 
-       ret = smb_krb5_open_keytab(context, keytab_name, False, &keytab);
+       if (keytab_name == NULL) {
+               ret = ads_keytab_open(context, &keytab);
+       } else {
+               ret = smb_krb5_kt_open(context, keytab_name, False, &keytab);
+       }
        if (ret) {
-               DEBUG(1,("ads_keytab_list: smb_krb5_open_keytab failed (%s)\n", error_message(ret)));
+               DEBUG(1, ("smb_krb5_kt_open failed (%s)\n",
+                         error_message(ret)));
                goto out;
        }
 
        ret = krb5_kt_start_seq_get(context, keytab, &cursor);
        if (ret) {
+               ZERO_STRUCT(cursor);
                goto out;
        }
 
-       printf("Vno  Type        Principal\n");
+       printf("Vno  Type                                        Principal\n");
 
        while (krb5_kt_next_entry(context, keytab, &kt_entry, &cursor) == 0) {
-       
+
                char *princ_s = NULL;
                char *etype_s = NULL;
                krb5_enctype enctype = 0;
 
-               ret = smb_krb5_unparse_name(talloc_tos(), context, kt_entry.principal, &princ_s);
+               ret = smb_krb5_unparse_name(talloc_tos(), context,
+                                           kt_entry.principal, &princ_s);
                if (ret) {
                        goto out;
                }
 
-               enctype = smb_get_enctype_from_kt_entry(&kt_entry);
+               enctype = smb_krb5_kt_get_enctype_from_entry(&kt_entry);
 
                ret = smb_krb5_enctype_to_string(context, enctype, &etype_s);
-               if (ret) {
-                       if (asprintf(&etype_s, "UNKNOWN: %d\n", enctype) == -1)
-                       {
-                               TALLOC_FREE(princ_s);
-                               goto out;
-                       }
+               if (ret &&
+                   (asprintf(&etype_s, "UNKNOWN: %d\n", enctype) == -1)) {
+                       TALLOC_FREE(princ_s);
+                       goto out;
                }
 
-               printf("%3d  %s\t\t %s\n", kt_entry.vno, etype_s, princ_s);
+               printf("%3d  %-43s %s\n", kt_entry.vno, etype_s, princ_s);
 
                TALLOC_FREE(princ_s);
                SAFE_FREE(etype_s);
@@ -794,19 +700,11 @@ int ads_keytab_list(const char *keytab_name)
        ZERO_STRUCT(cursor);
 out:
 
-       {
-               krb5_keytab_entry zero_kt_entry;
-               ZERO_STRUCT(zero_kt_entry);
-               if (memcmp(&zero_kt_entry, &kt_entry, sizeof(krb5_keytab_entry))) {
-                       smb_krb5_kt_free_entry(context, &kt_entry);
-               }
+       if (!all_zero((uint8_t *)&kt_entry, sizeof(kt_entry))) {
+               smb_krb5_kt_free_entry(context, &kt_entry);
        }
-       {
-               krb5_kt_cursor zero_csr;
-               ZERO_STRUCT(zero_csr);
-               if ((memcmp(&cursor, &zero_csr, sizeof(krb5_kt_cursor)) != 0) && keytab) {
-                       krb5_kt_end_seq_get(context, keytab, &cursor);  
-               }
+       if (!all_zero((uint8_t *)&cursor, sizeof(cursor)) && keytab) {
+               krb5_kt_end_seq_get(context, keytab, &cursor);
        }
 
        if (keytab) {