Blame SOURCES/0029-ipa-kdb-do-not-use-OpenLDAP-functions-with-NULL-LDAP_rhbz#1935146.patch

7b546d
Adapted version due to missing patches:
7b546d
7b546d
  commit 1f1e7dbe6131b3cdc0ba81b454c7729126bfa6ee
7b546d
  Author: Slava Aseev <ptrnine@altlinux.org>
7b546d
  Date:   Mon Nov 23 18:23:01 2020 +0300
7b546d
  
7b546d
      ipa-kdb: handle dates up to 2106-02-07 06:28:16
7b546d
 
7b546d
  commit 44c222aca9bb0056004f15dfb187d3f249ed0452
7b546d
  Author: Alexander Bokovoy <abokovoy@redhat.com>
7b546d
  Date:   Thu Dec 17 12:22:47 2020 +0200
7b546d
  
7b546d
      ipa-kdb: use predefined filters for a wild-card searches
7b546d
7b546d
  commit 78a7ab0daf0d5ebd388046aec6e1c9328e0564a8
7b546d
  Author: Robbie Harwood <rharwood@redhat.com>
7b546d
  Date:   Tue Nov 10 14:07:47 2020 -0500
7b546d
  
7b546d
      ipa-kdb: implement AS-REQ lifetime jitter
7b546d
7b546d
  commit d6a8fc290aa93fc5d53025f4400a9736366175eb
7b546d
  Author: Rob Crittenden <rcritten@redhat.com>
7b546d
  Date:   Thu Sep 24 22:39:36 2020 -0400
7b546d
  
7b546d
      Pass the user to the password policy check in the kdb driver
7b546d
7b546d
From 701d0fb0415497fe9fe8fbf25fa800041e2a2b40 Mon Sep 17 00:00:00 2001
7b546d
From: Alexander Bokovoy <abokovoy@redhat.com>
7b546d
Date: Tue, 23 Feb 2021 10:06:25 +0200
7b546d
Subject: [PATCH] ipa-kdb: fix compiler warnings
7b546d
7b546d
There are few fields in KDB structures that have 'conflicting' types but
7b546d
need to be compared. They come from MIT Kerberos and we have no choice
7b546d
here.
7b546d
7b546d
In the same way, SID structures have own requirements.
7b546d
7b546d
Signed-off-by: Alexander Bokovoy <abokovoy@redhat.com>
7b546d
Reviewed-By: Robbie Harwood <rharwood@redhat.com>
7b546d
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
7b546d
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
7b546d
---
7b546d
 daemons/ipa-kdb/ipa_kdb_audit_as.c   | 4 ++--
7b546d
 daemons/ipa-kdb/ipa_kdb_mspac.c      | 6 +++---
7b546d
 daemons/ipa-kdb/ipa_kdb_principals.c | 6 +++---
7b546d
 daemons/ipa-kdb/ipa_kdb_pwdpolicy.c  | 2 +-
7b546d
 4 files changed, 9 insertions(+), 9 deletions(-)
7b546d
7b546d
diff --git a/daemons/ipa-kdb/ipa_kdb_audit_as.c b/daemons/ipa-kdb/ipa_kdb_audit_as.c
7b546d
index ed48ea758..ec2046bfe 100644
7b546d
--- a/daemons/ipa-kdb/ipa_kdb_audit_as.c
7b546d
+++ b/daemons/ipa-kdb/ipa_kdb_audit_as.c
7b546d
@@ -110,13 +110,13 @@ void ipadb_audit_as_req(krb5_context kcontext,
7b546d
         }
7b546d
 
7b546d
         if (client->last_failed + ied->pol->lockout_duration > authtime &&
7b546d
-            (client->fail_auth_count >= ied->pol->max_fail && 
7b546d
+            (client->fail_auth_count >= (krb5_kvno) ied->pol->max_fail &&
7b546d
              ied->pol->max_fail != 0)) {
7b546d
             /* client already locked, nothing more to do */
7b546d
             break;
7b546d
         }
7b546d
         if (ied->pol->max_fail == 0 ||
7b546d
-            client->fail_auth_count < ied->pol->max_fail) {
7b546d
+            client->fail_auth_count < (krb5_kvno) ied->pol->max_fail) {
7b546d
             /* let's increase the fail counter */
7b546d
             client->fail_auth_count++;
7b546d
             client->mask |= KMASK_FAIL_AUTH_COUNT;
7b546d
diff --git a/daemons/ipa-kdb/ipa_kdb_mspac.c b/daemons/ipa-kdb/ipa_kdb_mspac.c
7b546d
index c6ac593ca..050100430 100644
7b546d
--- a/daemons/ipa-kdb/ipa_kdb_mspac.c
7b546d
+++ b/daemons/ipa-kdb/ipa_kdb_mspac.c
7b546d
@@ -147,9 +147,9 @@ int string_to_sid(const char *str, struct dom_sid *sid)
7b546d
 
7b546d
 char *dom_sid_string(TALLOC_CTX *memctx, const struct dom_sid *dom_sid)
7b546d
 {
7b546d
-    size_t c;
7b546d
+    int8_t c;
7b546d
     size_t len;
7b546d
-    int ofs;
7b546d
+    size_t ofs;
7b546d
     uint32_t ia;
7b546d
     char *buf;
7b546d
 
7b546d
@@ -2606,7 +2606,7 @@ krb5_error_code ipadb_mspac_get_trusted_domains(struct ipadb_context *ipactx)
7b546d
 
7b546d
         t[n].upn_suffixes_len = NULL;
7b546d
         if (t[n].upn_suffixes != NULL) {
7b546d
-            size_t len = 0;
7b546d
+            int len = 0;
7b546d
 
7b546d
             for (; t[n].upn_suffixes[len] != NULL; len++);
7b546d
 
7b546d
diff --git a/daemons/ipa-kdb/ipa_kdb_principals.c b/daemons/ipa-kdb/ipa_kdb_principals.c
7b546d
index d1fa51578..59337a4ca 100644
7b546d
--- a/daemons/ipa-kdb/ipa_kdb_principals.c
7b546d
+++ b/daemons/ipa-kdb/ipa_kdb_principals.c
7b546d
@@ -491,7 +491,7 @@ static krb5_error_code ipadb_get_ldap_auth_ind(krb5_context kcontext,
7b546d
     l = len;
7b546d
     for (i = 0; i < count; i++) {
7b546d
         ret = snprintf(ap, l, "%s ", authinds[i]);
7b546d
-        if (ret <= 0 || ret > l) {
7b546d
+        if (ret <= 0 || ret > (int) l) {
7b546d
             ret = ENOMEM;
7b546d
             goto cleanup;
7b546d
         }
7b546d
@@ -2064,7 +2064,7 @@ static krb5_error_code ipadb_get_ldap_mod_auth_ind(krb5_context kcontext,
7b546d
     char *s = NULL;
7b546d
     size_t ai_size = 0;
7b546d
     int cnt = 0;
7b546d
-    int i = 0;
7b546d
+    size_t i = 0;
7b546d
 
7b546d
     ret = krb5_dbe_get_string(kcontext, entry, "require_auth", &ais);
7b546d
     if (ret) {
7b546d
@@ -2445,7 +2445,7 @@ static krb5_error_code ipadb_entry_default_attrs(struct ipadb_mods *imods)
7b546d
 {
7b546d
     krb5_error_code kerr;
7b546d
     LDAPMod *m = NULL;
7b546d
-    int i;
7b546d
+    size_t i;
7b546d
 
7b546d
     kerr = ipadb_mods_new(imods, &m);
7b546d
     if (kerr) {
7b546d
diff --git a/daemons/ipa-kdb/ipa_kdb_pwdpolicy.c b/daemons/ipa-kdb/ipa_kdb_pwdpolicy.c
7b546d
index 4965e6d7f..6f21ef867 100644
7b546d
--- a/daemons/ipa-kdb/ipa_kdb_pwdpolicy.c
7b546d
+++ b/daemons/ipa-kdb/ipa_kdb_pwdpolicy.c
7b546d
@@ -328,7 +328,7 @@ krb5_error_code ipadb_check_policy_as(krb5_context kcontext,
7b546d
     }
7b546d
 
7b546d
     if (ied->pol->max_fail == 0 ||
7b546d
-        client->fail_auth_count < ied->pol->max_fail) {
7b546d
+        client->fail_auth_count < (krb5_kvno) ied->pol->max_fail) {
7b546d
         /* still within allowed failures range */
7b546d
         return 0;
7b546d
     }
7b546d
-- 
7b546d
2.29.2
7b546d
7b546d
From d454ca8f004954f19622fe61ad9e2854359f3784 Mon Sep 17 00:00:00 2001
7b546d
From: Alexander Bokovoy <abokovoy@redhat.com>
7b546d
Date: Wed, 24 Feb 2021 20:51:40 +0200
7b546d
Subject: [PATCH] ipa-kdb: add missing prototypes
7b546d
7b546d
On Fedora 33 GCC defaults to -Wmissing-prototypes and emits warnings
7b546d
about function prototypes missing. If -Werror is specified, this breaks
7b546d
compilation.
7b546d
7b546d
We also default to -Werror=implicit-function-declaration
7b546d
7b546d
Signed-off-by: Alexander Bokovoy <abokovoy@redhat.com>
7b546d
Reviewed-By: Robbie Harwood <rharwood@redhat.com>
7b546d
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
7b546d
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
7b546d
---
7b546d
 daemons/ipa-kdb/ipa_kdb_kdcpolicy.c     |  4 ++++
7b546d
 daemons/ipa-kdb/ipa_kdb_mspac.c         | 20 ++++++++++++--------
7b546d
 daemons/ipa-kdb/ipa_kdb_mspac_private.h |  4 ++++
7b546d
 3 files changed, 20 insertions(+), 8 deletions(-)
7b546d
7b546d
diff --git a/daemons/ipa-kdb/ipa_kdb_kdcpolicy.c b/daemons/ipa-kdb/ipa_kdb_kdcpolicy.c
7b546d
index 7f03f2f03..6976f9ba9 100644
7b546d
--- a/daemons/ipa-kdb/ipa_kdb_kdcpolicy.c
7b546d
+++ b/daemons/ipa-kdb/ipa_kdb_kdcpolicy.c
7b546d
@@ -9,6 +9,10 @@
7b546d
 #include "ipa_krb5.h"
7b546d
 #include "ipa_kdb.h"
7b546d
 
7b546d
+krb5_error_code kdcpolicy_ipakdb_initvt(krb5_context context,
7b546d
+                                        int maj_ver, int min_ver,
7b546d
+                                        krb5_plugin_vtable vtable);
7b546d
+
7b546d
 static krb5_error_code
7b546d
 ipa_kdcpolicy_check_as(krb5_context context, krb5_kdcpolicy_moddata moddata,
7b546d
                        const krb5_kdc_req *request,
7b546d
diff --git a/daemons/ipa-kdb/ipa_kdb_mspac.c b/daemons/ipa-kdb/ipa_kdb_mspac.c
7b546d
index 050100430..c05fb717a 100644
7b546d
--- a/daemons/ipa-kdb/ipa_kdb_mspac.c
7b546d
+++ b/daemons/ipa-kdb/ipa_kdb_mspac.c
7b546d
@@ -2403,9 +2403,10 @@ void ipadb_mspac_struct_free(struct ipadb_mspac **mspac)
7b546d
     *mspac = NULL;
7b546d
 }
7b546d
 
7b546d
-krb5_error_code ipadb_adtrusts_fill_sid_blacklist(char **source_sid_blacklist,
7b546d
-                                                  struct dom_sid **result_sids,
7b546d
-                                                  int *result_length)
7b546d
+static krb5_error_code
7b546d
+ipadb_adtrusts_fill_sid_blacklist(char **source_sid_blacklist,
7b546d
+                                  struct dom_sid **result_sids,
7b546d
+                                  int *result_length)
7b546d
 {
7b546d
     int len, i;
7b546d
     char **source;
7b546d
@@ -2436,9 +2437,10 @@ krb5_error_code ipadb_adtrusts_fill_sid_blacklist(char **source_sid_blacklist,
7b546d
     return 0;
7b546d
 }
7b546d
 
7b546d
-krb5_error_code ipadb_adtrusts_fill_sid_blacklists(struct ipadb_adtrusts *adtrust,
7b546d
-                                                   char **sid_blacklist_incoming,
7b546d
-                                                   char **sid_blacklist_outgoing)
7b546d
+static krb5_error_code
7b546d
+ipadb_adtrusts_fill_sid_blacklists(struct ipadb_adtrusts *adtrust,
7b546d
+                                   char **sid_blacklist_incoming,
7b546d
+                                   char **sid_blacklist_outgoing)
7b546d
 {
7b546d
     krb5_error_code kerr;
7b546d
 
7b546d
@@ -2459,7 +2461,8 @@ krb5_error_code ipadb_adtrusts_fill_sid_blacklists(struct ipadb_adtrusts *adtrus
7b546d
     return 0;
7b546d
 }
7b546d
 
7b546d
-krb5_error_code ipadb_mspac_check_trusted_domains(struct ipadb_context *ipactx)
7b546d
+static krb5_error_code
7b546d
+ipadb_mspac_check_trusted_domains(struct ipadb_context *ipactx)
7b546d
 {
7b546d
     char *attrs[] = { NULL };
7b546d
     char *filter = "(objectclass=ipaNTTrustedDomain)";
7b546d
@@ -2504,7 +2507,8 @@ static void ipadb_free_sid_blacklists(char ***sid_blacklist_incoming, char ***si
7b546d
     }
7b546d
 }
7b546d
 
7b546d
-krb5_error_code ipadb_mspac_get_trusted_domains(struct ipadb_context *ipactx)
7b546d
+static krb5_error_code
7b546d
+ipadb_mspac_get_trusted_domains(struct ipadb_context *ipactx)
7b546d
 {
7b546d
     struct ipadb_adtrusts *t;
7b546d
     LDAP *lc = ipactx->lcontext;
7b546d
diff --git a/daemons/ipa-kdb/ipa_kdb_mspac_private.h b/daemons/ipa-kdb/ipa_kdb_mspac_private.h
7b546d
index b21aa163f..2369e16f8 100644
7b546d
--- a/daemons/ipa-kdb/ipa_kdb_mspac_private.h
7b546d
+++ b/daemons/ipa-kdb/ipa_kdb_mspac_private.h
7b546d
@@ -53,3 +53,7 @@ struct ipadb_adtrusts {
7b546d
 
7b546d
 int string_to_sid(const char *str, struct dom_sid *sid);
7b546d
 char *dom_sid_string(TALLOC_CTX *memctx, const struct dom_sid *dom_sid);
7b546d
+krb5_error_code filter_logon_info(krb5_context context, TALLOC_CTX *memctx,
7b546d
+                                  krb5_data realm, struct PAC_LOGON_INFO_CTR *info);
7b546d
+void get_authz_data_types(krb5_context context, krb5_db_entry *entry,
7b546d
+                          bool *_with_pac, bool *_with_pad);
7b546d
\ No newline at end of file
7b546d
-- 
7b546d
2.29.2
7b546d
7b546d
From da98a6fcb81ee3ac7df8bb238a0793809c2be3fd Mon Sep 17 00:00:00 2001
7b546d
From: Alexander Bokovoy <abokovoy@redhat.com>
7b546d
Date: Wed, 24 Feb 2021 20:52:15 +0200
7b546d
Subject: [PATCH] ipa-kdb: reformat ipa_kdb_certauth
7b546d
7b546d
Add prototype to the exported function
7b546d
7b546d
Replace few tabs by spaces and mark static code as static.
7b546d
7b546d
Signed-off-by: Alexander Bokovoy <abokovoy@redhat.com>
7b546d
Reviewed-By: Robbie Harwood <rharwood@redhat.com>
7b546d
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
7b546d
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
7b546d
---
7b546d
 daemons/ipa-kdb/ipa_kdb_certauth.c | 25 ++++++++++++++-----------
7b546d
 1 file changed, 14 insertions(+), 11 deletions(-)
7b546d
7b546d
diff --git a/daemons/ipa-kdb/ipa_kdb_certauth.c b/daemons/ipa-kdb/ipa_kdb_certauth.c
7b546d
index bc6b26578..3a3060c92 100644
7b546d
--- a/daemons/ipa-kdb/ipa_kdb_certauth.c
7b546d
+++ b/daemons/ipa-kdb/ipa_kdb_certauth.c
7b546d
@@ -71,10 +71,13 @@ struct krb5_certauth_moddata_st {
7b546d
     time_t valid_until;
7b546d
 };
7b546d
 
7b546d
-void ipa_certmap_debug(void *private,
7b546d
-                       const char *file, long line,
7b546d
-                       const char *function,
7b546d
-                       const char *format, ...)
7b546d
+krb5_error_code certauth_ipakdb_initvt(krb5_context context,
7b546d
+                                       int maj_ver, int min_ver,
7b546d
+                                       krb5_plugin_vtable vtable);
7b546d
+
7b546d
+static void ipa_certmap_debug(void *private, const char *file, long line,
7b546d
+                              const char *function,
7b546d
+                              const char *format, ...)
7b546d
 {
7b546d
     va_list ap;
7b546d
     char str[255] = { 0 };
7b546d
@@ -354,12 +357,12 @@ static krb5_error_code ipa_certauth_authorize(krb5_context context,
7b546d
      * so there is nothing more to add here. */
7b546d
     auth_inds = calloc(2, sizeof(char *));
7b546d
     if (auth_inds != NULL) {
7b546d
-	ret = asprintf(&auth_inds[0], "pkinit");
7b546d
-	if (ret != -1) {
7b546d
+        ret = asprintf(&auth_inds[0], "pkinit");
7b546d
+        if (ret != -1) {
7b546d
             auth_inds[1] = NULL;
7b546d
             *authinds_out = auth_inds;
7b546d
-	} else {
7b546d
-	    free(auth_inds);
7b546d
+        } else {
7b546d
+            free(auth_inds);
7b546d
         }
7b546d
     }
7b546d
 
7b546d
@@ -404,12 +407,12 @@ static void ipa_certauth_free_indicator(krb5_context context,
7b546d
     size_t i = 0;
7b546d
 
7b546d
     if ((authinds == NULL) || (moddata == NULL)) {
7b546d
-	return;
7b546d
+        return;
7b546d
     }
7b546d
 
7b546d
     for(i=0; authinds[i]; i++) {
7b546d
-	free(authinds[i]);
7b546d
-	authinds[i] = NULL;
7b546d
+        free(authinds[i]);
7b546d
+        authinds[i] = NULL;
7b546d
     }
7b546d
 
7b546d
     free(authinds);
7b546d
-- 
7b546d
2.29.2
7b546d
7b546d
From aa7f99c08ff41f216d60152d6235922c561c2881 Mon Sep 17 00:00:00 2001
7b546d
From: Alexander Bokovoy <abokovoy@redhat.com>
7b546d
Date: Wed, 24 Feb 2021 20:55:41 +0200
7b546d
Subject: [PATCH] ipa-kdb: mark test functions as static
7b546d
7b546d
No need to define missing prototypes to single use test functions.
7b546d
7b546d
Signed-off-by: Alexander Bokovoy <abokovoy@redhat.com>
7b546d
Reviewed-By: Robbie Harwood <rharwood@redhat.com>
7b546d
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
7b546d
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
7b546d
---
7b546d
 daemons/ipa-kdb/tests/ipa_kdb_tests.c | 13 +++++--------
7b546d
 1 file changed, 5 insertions(+), 8 deletions(-)
7b546d
7b546d
diff --git a/daemons/ipa-kdb/tests/ipa_kdb_tests.c b/daemons/ipa-kdb/tests/ipa_kdb_tests.c
7b546d
index 368a2f978..960200b6e 100644
7b546d
--- a/daemons/ipa-kdb/tests/ipa_kdb_tests.c
7b546d
+++ b/daemons/ipa-kdb/tests/ipa_kdb_tests.c
7b546d
@@ -180,7 +180,7 @@ extern krb5_error_code filter_logon_info(krb5_context context,
7b546d
                                   krb5_data realm,
7b546d
                                   struct PAC_LOGON_INFO_CTR *info);
7b546d
 
7b546d
-void test_filter_logon_info(void **state)
7b546d
+static void test_filter_logon_info(void **state)
7b546d
 {
7b546d
     krb5_error_code kerr;
7b546d
     krb5_data realm = {KV5M_DATA, REALM_LEN, REALM};
7b546d
@@ -315,10 +315,7 @@ void test_filter_logon_info(void **state)
7b546d
 
7b546d
 }
7b546d
 
7b546d
-extern void get_authz_data_types(krb5_context context, krb5_db_entry *entry,
7b546d
-                                 bool *with_pac, bool *with_pad);
7b546d
-
7b546d
-void test_get_authz_data_types(void **state)
7b546d
+static void test_get_authz_data_types(void **state)
7b546d
 {
7b546d
     bool with_pac;
7b546d
     bool with_pad;
7b546d
@@ -436,7 +433,7 @@ void test_get_authz_data_types(void **state)
7b546d
     krb5_free_principal(test_ctx->krb5_ctx, non_nfs_princ);
7b546d
 }
7b546d
 
7b546d
-void test_string_to_sid(void **state)
7b546d
+static void test_string_to_sid(void **state)
7b546d
 {
7b546d
     int ret;
7b546d
     struct dom_sid sid;
7b546d
@@ -468,7 +465,7 @@ void test_string_to_sid(void **state)
7b546d
     assert_memory_equal(&exp_sid, &sid, sizeof(struct dom_sid));
7b546d
 }
7b546d
 
7b546d
-void test_dom_sid_string(void **state)
7b546d
+static void test_dom_sid_string(void **state)
7b546d
 {
7b546d
     struct test_ctx *test_ctx;
7b546d
     char *str_sid;
7b546d
@@ -494,7 +491,7 @@ void test_dom_sid_string(void **state)
7b546d
 }
7b546d
 
7b546d
 
7b546d
-void test_check_trusted_realms(void **state)
7b546d
+static void test_check_trusted_realms(void **state)
7b546d
 {
7b546d
     struct test_ctx *test_ctx;
7b546d
     krb5_error_code kerr = 0;
7b546d
-- 
7b546d
2.29.2
7b546d
7b546d
From 79baa0932d1349d46d162e7478fa4e3c8e88dc09 Mon Sep 17 00:00:00 2001
7b546d
From: Alexander Bokovoy <abokovoy@redhat.com>
7b546d
Date: Fri, 19 Feb 2021 15:37:47 +0200
7b546d
Subject: [PATCH] ipa-kdb: do not use OpenLDAP functions with NULL LDAP context
7b546d
7b546d
Calling to ipadb_get_connection() will remove LDAP context if any error
7b546d
happens. This means upper layers must always verify that LDAP context
7b546d
exists after such calls.
7b546d
7b546d
ipadb_get_user_auth() may re-read global configuration and that may fail
7b546d
and cause IPA context to have NULL LDAP context.
7b546d
7b546d
Fixes: https://pagure.io/freeipa/issue/8681
7b546d
7b546d
Signed-off-by: Alexander Bokovoy <abokovoy@redhat.com>
7b546d
Reviewed-By: Robbie Harwood <rharwood@redhat.com>
7b546d
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
7b546d
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
7b546d
---
7b546d
 daemons/ipa-kdb/ipa_kdb.c            |  1 +
7b546d
 daemons/ipa-kdb/ipa_kdb_mspac.c      | 32 +++++++++++++++-------------
7b546d
 daemons/ipa-kdb/ipa_kdb_principals.c | 26 ++++++++++++++++------
7b546d
 3 files changed, 37 insertions(+), 22 deletions(-)
7b546d
7b546d
diff --git a/daemons/ipa-kdb/ipa_kdb.c b/daemons/ipa-kdb/ipa_kdb.c
7b546d
index 33d2a6773..e7b8d7dbf 100644
7b546d
--- a/daemons/ipa-kdb/ipa_kdb.c
7b546d
+++ b/daemons/ipa-kdb/ipa_kdb.c
7b546d
@@ -56,6 +56,7 @@ static void ipadb_context_free(krb5_context kcontext,
7b546d
         /* ldap free lcontext */
7b546d
         if ((*ctx)->lcontext) {
7b546d
             ldap_unbind_ext_s((*ctx)->lcontext, NULL, NULL);
7b546d
+            (*ctx)->lcontext = NULL;
7b546d
         }
7b546d
         free((*ctx)->supp_encs);
7b546d
         free((*ctx)->def_encs);
7b546d
diff --git a/daemons/ipa-kdb/ipa_kdb_mspac.c b/daemons/ipa-kdb/ipa_kdb_mspac.c
7b546d
index c05fb717a..1e59189ed 100644
7b546d
--- a/daemons/ipa-kdb/ipa_kdb_mspac.c
7b546d
+++ b/daemons/ipa-kdb/ipa_kdb_mspac.c
7b546d
@@ -416,7 +416,6 @@ static krb5_error_code ipadb_fill_info3(struct ipadb_context *ipactx,
7b546d
                                         TALLOC_CTX *memctx,
7b546d
                                         struct netr_SamInfo3 *info3)
7b546d
 {
7b546d
-    LDAP *lcontext = ipactx->lcontext;
7b546d
     LDAPDerefRes *deref_results = NULL;
7b546d
     struct dom_sid sid;
7b546d
     gid_t prigid = -1;
7b546d
@@ -433,7 +432,7 @@ static krb5_error_code ipadb_fill_info3(struct ipadb_context *ipactx,
7b546d
     bool is_idobject = false;
7b546d
     krb5_principal princ;
7b546d
 
7b546d
-    ret = ipadb_ldap_attr_to_strlist(lcontext, lentry, "objectClass",
7b546d
+    ret = ipadb_ldap_attr_to_strlist(ipactx->lcontext, lentry, "objectClass",
7b546d
                                      &objectclasses);
7b546d
     if (ret == 0 && objectclasses != NULL) {
7b546d
         for (c = 0; objectclasses[c] != NULL; c++) {
7b546d
@@ -470,13 +469,14 @@ static krb5_error_code ipadb_fill_info3(struct ipadb_context *ipactx,
7b546d
     }
7b546d
 
7b546d
     if (is_host) {
7b546d
-        ret = ipadb_ldap_attr_to_str(lcontext, lentry, "fqdn", &strres);
7b546d
+        ret = ipadb_ldap_attr_to_str(ipactx->lcontext, lentry, "fqdn", &strres);
7b546d
         if (ret) {
7b546d
             /* fqdn is mandatory for hosts */
7b546d
             return ret;
7b546d
         }
7b546d
     } else if (is_service) {
7b546d
-        ret = ipadb_ldap_attr_to_str(lcontext, lentry, "krbCanonicalName", &strres);
7b546d
+        ret = ipadb_ldap_attr_to_str(ipactx->lcontext, lentry,
7b546d
+                                     "krbCanonicalName", &strres);
7b546d
         if (ret) {
7b546d
             /* krbCanonicalName is mandatory for services */
7b546d
             return ret;
7b546d
@@ -496,7 +496,7 @@ static krb5_error_code ipadb_fill_info3(struct ipadb_context *ipactx,
7b546d
             return ENOENT;
7b546d
         }
7b546d
     } else {
7b546d
-        ret = ipadb_ldap_attr_to_str(lcontext, lentry, "uid", &strres);
7b546d
+        ret = ipadb_ldap_attr_to_str(ipactx->lcontext, lentry, "uid", &strres);
7b546d
         if (ret) {
7b546d
             /* uid is mandatory */
7b546d
             return ret;
7b546d
@@ -509,7 +509,8 @@ static krb5_error_code ipadb_fill_info3(struct ipadb_context *ipactx,
7b546d
     if (is_host || is_service) {
7b546d
         prigid = 515; /* Well known RID for domain computers group */
7b546d
     } else {
7b546d
-        ret = ipadb_ldap_attr_to_int(lcontext, lentry, "gidNumber", &intres);
7b546d
+        ret = ipadb_ldap_attr_to_int(ipactx->lcontext, lentry,
7b546d
+                                     "gidNumber", &intres);
7b546d
         if (ret) {
7b546d
             /* gidNumber is mandatory */
7b546d
             return ret;
7b546d
@@ -540,7 +541,7 @@ static krb5_error_code ipadb_fill_info3(struct ipadb_context *ipactx,
7b546d
     info3->base.kickoff_time = -1;
7b546d
 #endif
7b546d
 
7b546d
-    ret = ipadb_ldap_attr_to_time_t(lcontext, lentry,
7b546d
+    ret = ipadb_ldap_attr_to_time_t(ipactx->lcontext, lentry,
7b546d
                                     "krbLastPwdChange", &timeres);
7b546d
     switch (ret) {
7b546d
     case 0:
7b546d
@@ -557,7 +558,7 @@ static krb5_error_code ipadb_fill_info3(struct ipadb_context *ipactx,
7b546d
     info3->base.allow_password_change = 0;
7b546d
     info3->base.force_password_change = -1;
7b546d
 
7b546d
-    ret = ipadb_ldap_attr_to_str(lcontext, lentry, "cn", &strres);
7b546d
+    ret = ipadb_ldap_attr_to_str(ipactx->lcontext, lentry, "cn", &strres);
7b546d
     switch (ret) {
7b546d
     case 0:
7b546d
         info3->base.full_name.string = talloc_strdup(memctx, strres);
7b546d
@@ -570,7 +571,7 @@ static krb5_error_code ipadb_fill_info3(struct ipadb_context *ipactx,
7b546d
         return ret;
7b546d
     }
7b546d
 
7b546d
-    ret = ipadb_ldap_attr_to_str(lcontext, lentry,
7b546d
+    ret = ipadb_ldap_attr_to_str(ipactx->lcontext, lentry,
7b546d
                                  "ipaNTLogonScript", &strres);
7b546d
     switch (ret) {
7b546d
     case 0:
7b546d
@@ -584,7 +585,7 @@ static krb5_error_code ipadb_fill_info3(struct ipadb_context *ipactx,
7b546d
         return ret;
7b546d
     }
7b546d
 
7b546d
-    ret = ipadb_ldap_attr_to_str(lcontext, lentry,
7b546d
+    ret = ipadb_ldap_attr_to_str(ipactx->lcontext, lentry,
7b546d
                                  "ipaNTProfilePath", &strres);
7b546d
     switch (ret) {
7b546d
     case 0:
7b546d
@@ -598,7 +599,7 @@ static krb5_error_code ipadb_fill_info3(struct ipadb_context *ipactx,
7b546d
         return ret;
7b546d
     }
7b546d
 
7b546d
-    ret = ipadb_ldap_attr_to_str(lcontext, lentry,
7b546d
+    ret = ipadb_ldap_attr_to_str(ipactx->lcontext, lentry,
7b546d
                                  "ipaNTHomeDirectory", &strres);
7b546d
     switch (ret) {
7b546d
     case 0:
7b546d
@@ -612,7 +613,7 @@ static krb5_error_code ipadb_fill_info3(struct ipadb_context *ipactx,
7b546d
         return ret;
7b546d
     }
7b546d
 
7b546d
-    ret = ipadb_ldap_attr_to_str(lcontext, lentry,
7b546d
+    ret = ipadb_ldap_attr_to_str(ipactx->lcontext, lentry,
7b546d
                                  "ipaNTHomeDirectoryDrive", &strres);
7b546d
     switch (ret) {
7b546d
     case 0:
7b546d
@@ -643,7 +644,7 @@ static krb5_error_code ipadb_fill_info3(struct ipadb_context *ipactx,
7b546d
             info3->base.rid = 515;
7b546d
         }
7b546d
     } else {
7b546d
-        ret = ipadb_ldap_attr_to_str(lcontext, lentry,
7b546d
+        ret = ipadb_ldap_attr_to_str(ipactx->lcontext, lentry,
7b546d
                                      "ipaNTSecurityIdentifier", &strres);
7b546d
         if (ret) {
7b546d
             /* SID is mandatory */
7b546d
@@ -660,7 +661,7 @@ static krb5_error_code ipadb_fill_info3(struct ipadb_context *ipactx,
7b546d
         }
7b546d
     }
7b546d
 
7b546d
-    ret = ipadb_ldap_deref_results(lcontext, lentry, &deref_results);
7b546d
+    ret = ipadb_ldap_deref_results(ipactx->lcontext, lentry, &deref_results);
7b546d
     switch (ret) {
7b546d
     LDAPDerefRes *dres;
7b546d
     LDAPDerefVal *dval;
7b546d
@@ -2511,7 +2512,7 @@ static krb5_error_code
7b546d
 ipadb_mspac_get_trusted_domains(struct ipadb_context *ipactx)
7b546d
 {
7b546d
     struct ipadb_adtrusts *t;
7b546d
-    LDAP *lc = ipactx->lcontext;
7b546d
+    LDAP *lc = NULL;
7b546d
     char *attrs[] = { "cn", "ipaNTTrustPartner", "ipaNTFlatName",
7b546d
                       "ipaNTTrustedDomainSID", "ipaNTSIDBlacklistIncoming",
7b546d
                       "ipaNTSIDBlacklistOutgoing", "ipaNTAdditionalSuffixes", NULL };
7b546d
@@ -2545,6 +2546,7 @@ ipadb_mspac_get_trusted_domains(struct ipadb_context *ipactx)
7b546d
         goto done;
7b546d
     }
7b546d
 
7b546d
+    lc = ipactx->lcontext;
7b546d
     for (le = ldap_first_entry(lc, res); le; le = ldap_next_entry(lc, le)) {
7b546d
         dnstr = ldap_get_dn(lc, le);
7b546d
 
7b546d
diff --git a/daemons/ipa-kdb/ipa_kdb_principals.c b/daemons/ipa-kdb/ipa_kdb_principals.c
7b546d
index 59337a4ca..0a98ff054 100644
7b546d
--- a/daemons/ipa-kdb/ipa_kdb_principals.c
7b546d
+++ b/daemons/ipa-kdb/ipa_kdb_principals.c
7b546d
@@ -335,6 +335,11 @@ static enum ipadb_user_auth ipadb_get_user_auth(struct ipadb_context *ipactx,
7b546d
     if (gcfg != NULL)
7b546d
         gua = gcfg->user_auth;
7b546d
 
7b546d
+    /* lcontext == NULL means ipadb_get_global_config() failed to load
7b546d
+     * global config and cleared the ipactx */
7b546d
+    if (ipactx->lcontext == NULL)
7b546d
+        return IPADB_USER_AUTH_NONE;
7b546d
+
7b546d
     /* Get the user's user_auth settings if not disabled. */
7b546d
     if ((gua & IPADB_USER_AUTH_DISABLED) == 0)
7b546d
         ipadb_parse_user_auth(ipactx->lcontext, lentry, &ua);
7b546d
@@ -608,8 +613,16 @@ static krb5_error_code ipadb_parse_ldap_entry(krb5_context kcontext,
7b546d
         free(entry);
7b546d
         return KRB5_KDB_DBNOTINITED;
7b546d
     }
7b546d
-    lcontext = ipactx->lcontext;
7b546d
-    if (!lcontext) {
7b546d
+
7b546d
+    entry->magic = KRB5_KDB_MAGIC_NUMBER;
7b546d
+    entry->len = KRB5_KDB_V1_BASE_LENGTH;
7b546d
+
7b546d
+    /* Get User Auth configuration. */
7b546d
+    ua = ipadb_get_user_auth(ipactx, lentry);
7b546d
+
7b546d
+    /* ipadb_get_user_auth() calls into ipadb_get_global_config()
7b546d
+     * and that might fail, causing lcontext to become NULL */
7b546d
+    if (!ipactx->lcontext) {
7b546d
         krb5_klog_syslog(LOG_INFO,
7b546d
                          "No LDAP connection in ipadb_parse_ldap_entry(); retrying...\n");
7b546d
         ret = ipadb_get_connection(ipactx);
7b546d
@@ -621,11 +634,10 @@ static krb5_error_code ipadb_parse_ldap_entry(krb5_context kcontext,
7b546d
         }
7b546d
     }
7b546d
 
7b546d
-    entry->magic = KRB5_KDB_MAGIC_NUMBER;
7b546d
-    entry->len = KRB5_KDB_V1_BASE_LENGTH;
7b546d
-
7b546d
-    /* Get User Auth configuration. */
7b546d
-    ua = ipadb_get_user_auth(ipactx, lentry);
7b546d
+    /* If any code below would result in invalidating ipactx->lcontext,
7b546d
+     * lcontext must be updated with the new ipactx->lcontext value.
7b546d
+     * We rely on the fact that none of LDAP-parsing helpers does it. */
7b546d
+    lcontext = ipactx->lcontext;
7b546d
 
7b546d
     /* ignore mask for now */
7b546d
 
7b546d
-- 
7b546d
2.29.2
7b546d