>From 452b0133932f7b2d7af7207414261d25e5b00abb Mon Sep 17 00:00:00 2001
From: Michal Zidek <mzidek@redhat.com>
Date: Tue, 27 Nov 2012 13:15:41 +0100
Subject: [PATCH] Null pointer dereferenced.

https://fedorahosted.org/sssd/ticket/1674
---
 src/db/sysdb.c | 196 +++++++++++++++++++++++++++++----------------------------
 1 file changed, 100 insertions(+), 96 deletions(-)

diff --git a/src/db/sysdb.c b/src/db/sysdb.c
index e82c184..a49fa50 100644
--- a/src/db/sysdb.c
+++ b/src/db/sysdb.c
@@ -1047,130 +1047,134 @@ int sysdb_domain_init_internal(TALLOC_CTX *mem_ctx,
 
     if (res->count == 1) {
         el = ldb_msg_find_element(res->msgs[0], "version");
-        if (el) {
-            if (el->num_values != 1) {
-                ret = EINVAL;
-                goto done;
-            }
-            version = talloc_strndup(tmp_ctx,
-                                     (char *)(el->values[0].data),
-                                     el->values[0].length);
-            if (!version) {
-                ret = ENOMEM;
-                goto done;
-            }
+        if (!el) {
+            ret = EIO;
+            goto done;
+        }
 
-            if (strcmp(version, SYSDB_VERSION) == 0) {
-                /* all fine, return */
-                ret = EOK;
-                goto done;
-            }
+        if (el->num_values != 1) {
+            ret = EINVAL;
+            goto done;
+        }
+        version = talloc_strndup(tmp_ctx,
+                                 (char *)(el->values[0].data),
+                                 el->values[0].length);
+        if (!version) {
+            ret = ENOMEM;
+            goto done;
+        }
 
-            if (!allow_upgrade) {
-                DEBUG(0, ("Wrong DB version (got %s expected %s)\n",
-                          version, SYSDB_VERSION));
-                ret = sysdb_version_check(SYSDB_VERSION, version);
-                goto done;
-            }
+        if (strcmp(version, SYSDB_VERSION) == 0) {
+            /* all fine, return */
+            ret = EOK;
+            goto done;
+        }
 
-            DEBUG(4, ("Upgrading DB [%s] from version: %s\n",
-                      domain->name, version));
+        if (!allow_upgrade) {
+            DEBUG(SSSDBG_FATAL_FAILURE,
+                  ("Wrong DB version (got %s expected %s)\n",
+                   version, SYSDB_VERSION));
+            ret = sysdb_version_check(SYSDB_VERSION, version);
+            goto done;
+        }
 
-            if (strcmp(version, SYSDB_VERSION_0_3) == 0) {
-                ret = sysdb_upgrade_03(sysdb, &version);
-                if (ret != EOK) {
-                    goto done;
-                }
+        DEBUG(SSSDBG_CONF_SETTINGS, ("Upgrading DB [%s] from version: %s\n",
+                  domain->name, version));
+
+        if (strcmp(version, SYSDB_VERSION_0_3) == 0) {
+            ret = sysdb_upgrade_03(sysdb, &version);
+            if (ret != EOK) {
+                goto done;
             }
+        }
 
-            if (strcmp(version, SYSDB_VERSION_0_4) == 0) {
-                ret = sysdb_upgrade_04(sysdb, &version);
-                if (ret != EOK) {
-                    goto done;
-                }
+        if (strcmp(version, SYSDB_VERSION_0_4) == 0) {
+            ret = sysdb_upgrade_04(sysdb, &version);
+            if (ret != EOK) {
+                goto done;
             }
+        }
 
-            if (strcmp(version, SYSDB_VERSION_0_5) == 0) {
-                ret = sysdb_upgrade_05(sysdb, &version);
-                if (ret != EOK) {
-                    goto done;
-                }
+        if (strcmp(version, SYSDB_VERSION_0_5) == 0) {
+            ret = sysdb_upgrade_05(sysdb, &version);
+            if (ret != EOK) {
+                goto done;
             }
+        }
 
-            if (strcmp(version, SYSDB_VERSION_0_6) == 0) {
-                ret = sysdb_upgrade_06(sysdb, &version);
-                if (ret != EOK) {
-                    goto done;
-                }
+        if (strcmp(version, SYSDB_VERSION_0_6) == 0) {
+            ret = sysdb_upgrade_06(sysdb, &version);
+            if (ret != EOK) {
+                goto done;
             }
+        }
 
-            if (strcmp(version, SYSDB_VERSION_0_7) == 0) {
-                ret = sysdb_upgrade_07(sysdb, &version);
-                if (ret != EOK) {
-                    goto done;
-                }
+        if (strcmp(version, SYSDB_VERSION_0_7) == 0) {
+            ret = sysdb_upgrade_07(sysdb, &version);
+            if (ret != EOK) {
+                goto done;
             }
+        }
 
-            if (strcmp(version, SYSDB_VERSION_0_8) == 0) {
-                ret = sysdb_upgrade_08(sysdb, &version);
-                if (ret != EOK) {
-                    goto done;
-                }
+        if (strcmp(version, SYSDB_VERSION_0_8) == 0) {
+            ret = sysdb_upgrade_08(sysdb, &version);
+            if (ret != EOK) {
+                goto done;
             }
+        }
 
-            if (strcmp(version, SYSDB_VERSION_0_9) == 0) {
-                ret = sysdb_upgrade_09(sysdb, &version);
-                if (ret != EOK) {
-                    goto done;
-                }
+        if (strcmp(version, SYSDB_VERSION_0_9) == 0) {
+            ret = sysdb_upgrade_09(sysdb, &version);
+            if (ret != EOK) {
+                goto done;
             }
+        }
 
-            if (strcmp(version, SYSDB_VERSION_0_10) == 0) {
-                ret = sysdb_upgrade_10(sysdb, &version);
-                if (ret != EOK) {
-                    goto done;
-                }
+        if (strcmp(version, SYSDB_VERSION_0_10) == 0) {
+            ret = sysdb_upgrade_10(sysdb, &version);
+            if (ret != EOK) {
+                goto done;
             }
+        }
 
-            if (strcmp(version, SYSDB_VERSION_0_11) == 0) {
-                ret = sysdb_upgrade_11(sysdb, &version);
-                if (ret != EOK) {
-                    goto done;
-                }
+        if (strcmp(version, SYSDB_VERSION_0_11) == 0) {
+            ret = sysdb_upgrade_11(sysdb, &version);
+            if (ret != EOK) {
+                goto done;
             }
+        }
 
-            if (strcmp(version, SYSDB_VERSION_0_12) == 0) {
-                ret = sysdb_upgrade_12(sysdb, &version);
-                if (ret != EOK) {
-                    goto done;
-                }
+        if (strcmp(version, SYSDB_VERSION_0_12) == 0) {
+            ret = sysdb_upgrade_12(sysdb, &version);
+            if (ret != EOK) {
+                goto done;
             }
+        }
 
-            if (strcmp(version, SYSDB_VERSION_0_13) == 0) {
-                ret = sysdb_upgrade_13(sysdb, &version);
-                if (ret != EOK) {
-                    goto done;
-                }
+        if (strcmp(version, SYSDB_VERSION_0_13) == 0) {
+            ret = sysdb_upgrade_13(sysdb, &version);
+            if (ret != EOK) {
+                goto done;
             }
+        }
 
-            /* The version should now match SYSDB_VERSION.
-             * If not, it means we didn't match any of the
-             * known older versions. The DB might be
-             * corrupt or generated by a newer version of
-             * SSSD.
+        /* The version should now match SYSDB_VERSION.
+         * If not, it means we didn't match any of the
+         * known older versions. The DB might be
+         * corrupt or generated by a newer version of
+         * SSSD.
+         */
+        if (strcmp(version, SYSDB_VERSION) == 0) {
+            /* The cache has been upgraded.
+             * We need to reopen the LDB to ensure that
+             * any changes made above take effect.
              */
-            if (strcmp(version, SYSDB_VERSION) == 0) {
-                /* The cache has been upgraded.
-                 * We need to reopen the LDB to ensure that
-                 * any changes made above take effect.
-                 */
-                talloc_zfree(sysdb->ldb);
-                ret = sysdb_ldb_connect(sysdb, sysdb->ldb_file, &sysdb->ldb);
-                if (ret != EOK) {
-                    DEBUG(1, ("sysdb_ldb_connect failed.\n"));
-                }
-                goto done;
+            talloc_zfree(sysdb->ldb);
+            ret = sysdb_ldb_connect(sysdb, sysdb->ldb_file, &sysdb->ldb);
+            if (ret != EOK) {
+                DEBUG(SSSDBG_CRIT_FAILURE, ("sysdb_ldb_connect failed.\n"));
             }
+            goto done;
         }
 
         DEBUG(0,("Unknown DB version [%s], expected [%s] for domain %s!\n",
-- 
1.7.11.2

