s3-kerberos: use krb5 compat macros in fill_keytab_from_password()
[kai/samba-autobuild/.git] / source3 / librpc / rpc / dcerpc_krb5.c
index 561259adf7dfaad59037f2800ca44bb0f747c4b8..22443b63ff82ef5c2841c12b30c375c83af78caa 100644 (file)
@@ -27,7 +27,7 @@
 static krb5_error_code flush_keytab(krb5_context krbctx, krb5_keytab keytab)
 {
        krb5_error_code ret;
-       krb5_kt_cursor kt_cursor = NULL;
+       krb5_kt_cursor kt_cursor;
        krb5_keytab_entry kt_entry;
 
        ZERO_STRUCT(kt_entry);
@@ -118,7 +118,7 @@ static krb5_error_code fill_keytab_from_password(krb5_context krbctx,
        krb5_keytab_entry kt_entry;
        unsigned int i;
 
-       ret = krb5_get_permitted_enctypes(krbctx, &enctypes);
+       ret = get_kerberos_allowed_etypes(krbctx, &enctypes);
        if (ret) {
                DEBUG(1, (__location__
                          ": Can't determine permitted enctypes!\n"));
@@ -145,7 +145,7 @@ static krb5_error_code fill_keytab_from_password(krb5_context krbctx,
 
                kt_entry.principal = princ;
                kt_entry.vno = vno;
-               kt_entry.key = *key;
+               *(KRB5_KT_KEY(&kt_entry)) = *key;
 
                ret = krb5_kt_add_entry(krbctx, keytab, &kt_entry);
                if (ret) {
@@ -175,7 +175,7 @@ static krb5_error_code get_mem_keytab_from_secrets(krb5_context krbctx,
        krb5_error_code ret;
        char *pwd = NULL;
        size_t pwd_len;
-       krb5_kt_cursor kt_cursor = NULL;
+       krb5_kt_cursor kt_cursor;
        krb5_keytab_entry kt_entry;
        krb5_data password;
        krb5_principal princ = NULL;
@@ -205,6 +205,7 @@ static krb5_error_code get_mem_keytab_from_secrets(krb5_context krbctx,
        }
 
        ZERO_STRUCT(kt_entry);
+       ZERO_STRUCT(kt_cursor);
 
        /* check if the keytab already has any entry */
        ret = krb5_kt_start_seq_get(krbctx, *keytab, &kt_cursor);
@@ -214,7 +215,7 @@ static krb5_error_code get_mem_keytab_from_secrets(krb5_context krbctx,
                 * we can verify if the keytab needs to be upgraded */
                while ((ret = krb5_kt_next_entry(krbctx, *keytab,
                                           &kt_entry, &kt_cursor)) == 0) {
-                       if (kt_entry.key.enctype == CLEARTEXT_PRIV_ENCTYPE) {
+                       if (smb_get_enctype_from_kt_entry(&kt_entry) == CLEARTEXT_PRIV_ENCTYPE) {
                                break;
                        }
                        smb_krb5_kt_free_entry(krbctx, &kt_entry);
@@ -232,8 +233,8 @@ static krb5_error_code get_mem_keytab_from_secrets(krb5_context krbctx,
                        /* found private entry,
                         * check if keytab is up to date */
 
-                       if ((pwd_len == kt_entry.key.length) &&
-                           (memcmp(kt_entry.key.contents,
+                       if ((pwd_len == KRB5_KEY_LENGTH(KRB5_KT_KEY(&kt_entry))) &&
+                           (memcmp(KRB5_KEY_DATA(KRB5_KT_KEY(&kt_entry)),
                                                pwd, pwd_len) == 0)) {
                                /* keytab is already up to date, return */
                                smb_krb5_kt_free_entry(krbctx, &kt_entry);
@@ -254,11 +255,13 @@ static krb5_error_code get_mem_keytab_from_secrets(krb5_context krbctx,
                }
        }
 
-       if (kt_cursor) {
-               /* stop enumeration and free cursor */
-               krb5_kt_end_seq_get(krbctx, *keytab, &kt_cursor);
-               kt_cursor = NULL;
-       }
+       {
+               krb5_kt_cursor zero_csr;
+               ZERO_STRUCT(zero_csr);
+               if ((memcmp(&kt_cursor, &zero_csr, sizeof(krb5_kt_cursor)) != 0) && *keytab) {
+                       krb5_kt_end_seq_get(krbctx, *keytab, &kt_cursor);
+               }
+        }
 
        /* keytab is not up to date, fill it up */
 
@@ -297,9 +300,10 @@ static krb5_error_code get_mem_keytab_from_secrets(krb5_context krbctx,
        ZERO_STRUCT(kt_entry);
        kt_entry.principal = princ;
        kt_entry.vno = 0;
-       kt_entry.key.enctype = CLEARTEXT_PRIV_ENCTYPE;
-       kt_entry.key.length = pwd_len;
-       kt_entry.key.contents = (uint8_t *)pwd;
+
+       KRB5_KEY_TYPE(KRB5_KT_KEY(&kt_entry)) = CLEARTEXT_PRIV_ENCTYPE;
+       KRB5_KEY_LENGTH(KRB5_KT_KEY(&kt_entry)) = pwd_len;
+       KRB5_KEY_DATA(KRB5_KT_KEY(&kt_entry)) = (uint8_t *)pwd;
 
        ret = krb5_kt_add_entry(krbctx, *keytab, &kt_entry);
        if (ret) {
@@ -315,11 +319,13 @@ out:
        SAFE_FREE(pwd);
        SAFE_FREE(pwd_old);
 
-       if (kt_cursor) {
-               /* stop enumeration and free cursor */
-               krb5_kt_end_seq_get(krbctx, *keytab, &kt_cursor);
-               kt_cursor = NULL;
-       }
+       {
+               krb5_kt_cursor zero_csr;
+               ZERO_STRUCT(zero_csr);
+               if ((memcmp(&kt_cursor, &zero_csr, sizeof(krb5_kt_cursor)) != 0) && *keytab) {
+                       krb5_kt_end_seq_get(krbctx, *keytab, &kt_cursor);
+               }
+        }
 
        if (princ) {
                krb5_free_principal(krbctx, princ);