summaryrefslogtreecommitdiffstats
path: root/server/confdb/confdb.c
diff options
context:
space:
mode:
Diffstat (limited to 'server/confdb/confdb.c')
-rw-r--r--server/confdb/confdb.c137
1 files changed, 117 insertions, 20 deletions
diff --git a/server/confdb/confdb.c b/server/confdb/confdb.c
index 0435191be..ca335c587 100644
--- a/server/confdb/confdb.c
+++ b/server/confdb/confdb.c
@@ -24,6 +24,8 @@
#include "ldb_errors.h"
#include "util/util.h"
#include "confdb/confdb.h"
+#include "util/btreemap.h"
+#include "db/sysdb.h"
#define CONFDB_VERSION "0.1"
#define CONFDB_DOMAIN_BASEDN "cn=domains,cn=config"
#define CONFDB_DOMAIN_ATTR "cn"
@@ -600,9 +602,25 @@ int confdb_init(TALLOC_CTX *mem_ctx,
return EOK;
}
+/* domain names are case insensitive for now
+ * NOTE: this function is not utf-8 safe,
+ * only ASCII names for now */
+static int _domain_comparator(const void *key1, const void *key2)
+{
+ int ret;
+
+ ret = strcasecmp((const char *)key1, (const char *)key2);
+ if (ret) {
+ /* special case LOCAL to be always the first domain */
+ if (strcmp(key1, "LOCAL") == 0) return 1;
+ if (strcmp(key2, "LOCAL") == 0) return -1;
+ }
+ return ret;
+}
+
int confdb_get_domains(struct confdb_ctx *cdb,
TALLOC_CTX *mem_ctx,
- char ***values)
+ struct btreemap **domains)
{
TALLOC_CTX *tmp_ctx;
struct ldb_dn *dn;
@@ -610,8 +628,9 @@ int confdb_get_domains(struct confdb_ctx *cdb,
struct ldb_message_element *el;
int ret, i;
const char *attrs[] = {CONFDB_DOMAIN_ATTR, NULL};
- char **vals;
- int val_count;
+ char *path;
+ struct btreemap *domain_map;
+ struct sss_domain_info *domain;
tmp_ctx = talloc_new(mem_ctx);
@@ -628,15 +647,13 @@ int confdb_get_domains(struct confdb_ctx *cdb,
goto done;
}
- val_count = 1;
- vals = talloc(mem_ctx, char *);
- if (!vals) {
- ret = ENOMEM;
- goto done;
- }
-
+ domain_map = NULL;
i = 0;
while (i < res->count) {
+ /* allocate the domain on the tmp_ctx. It will be stolen
+ * by btreemap_set_value
+ */
+ domain = talloc_zero(tmp_ctx, struct sss_domain_info);
el = ldb_msg_find_element(res->msgs[i], CONFDB_DOMAIN_ATTR);
if (el && el->num_values > 0) {
if (el->num_values > 1) {
@@ -644,26 +661,106 @@ int confdb_get_domains(struct confdb_ctx *cdb,
ret = EINVAL;
goto done;
}
- val_count++;
- vals = talloc_realloc(mem_ctx, vals, char *, val_count);
- if (!vals) {
- DEBUG(0, ("realloc failed\n"));
+
+ /* should always be strings so this should be safe */
+ struct ldb_val v = el->values[0];
+ domain->name = talloc_strndup(domain, (char *)v.data, v.length);
+ if (!domain->name) {
ret = ENOMEM;
+ talloc_free(domain_map);
goto done;
}
- /* should always be strings so this should be safe */
- struct ldb_val v = el->values[0];
- vals[i] = talloc_strndup(vals, (char *)v.data, v.length);
- if (!vals[i]) {
+
+ /* Create the confdb path for this domain */
+ path = talloc_asprintf(tmp_ctx, "config/domains/%s", domain->name);
+ if (!path) {
+ ret = ENOMEM;
+ goto done;
+ }
+
+ /* Build the BaseDN for this domain */
+ domain->basedn = talloc_asprintf(domain, SYSDB_DOM_BASE, domain->name);
+ if (domain->basedn == NULL) {
ret = ENOMEM;
goto done;
}
+ DEBUG(3, ("BaseDN: %s\n", domain->basedn));
+
+ /* Determine if this domain can be enumerated */
+ ret = confdb_get_int(cdb, domain, path,
+ "enumerate", false, &(domain->enumerate));
+ if (ret != EOK) {
+ DEBUG(0, ("Failed to fetch enumerate for [%s]!\n", domain->name));
+ goto done;
+ }
+
+ /* Determine if this is a legacy domain */
+ ret = confdb_get_bool(cdb, domain, path,
+ "legacy", false, &(domain->legacy));
+ if (ret != EOK) {
+ DEBUG(0, ("Failed to fetch legacy for [%s]!\n", domain->name));
+ goto done;
+ }
+
+ /* Determine if this domain is managed by a backend provider */
+ ret = confdb_get_string(cdb, domain, path, "provider",
+ NULL, &domain->provider);
+ if (ret != EOK) {
+ DEBUG(0, ("Failed to fetch provider for [%s]!\n", domain->name));
+ goto done;
+ }
+ if (domain->provider) domain->has_provider = true;
+
+ ret = btreemap_set_value(mem_ctx, &domain_map,
+ domain->name, domain,
+ _domain_comparator);
+ if (ret != EOK) {
+ DEBUG(1, ("Failed to store domain info for [%s]!\n", domain->name));
+ goto done;
+ }
+
+ talloc_free(path);
}
i++;
}
- vals[i] = NULL;
- *values = vals;
+ *domains = domain_map;
+
+done:
+ talloc_free(tmp_ctx);
+ if (ret != EOK) {
+ talloc_free(domain_map);
+ *domains = NULL;
+ }
+ return ret;
+}
+
+int confdb_get_domains_list(struct confdb_ctx *cdb,
+ TALLOC_CTX *mem_ctx,
+ const char ***domain_names,
+ int *count)
+{
+ int ret;
+ struct btreemap *domain_map;
+ TALLOC_CTX *tmp_ctx;
+
+ tmp_ctx = talloc_new(mem_ctx);
+ if(tmp_ctx == NULL) {
+ return ENOMEM;
+ }
+
+ ret = confdb_get_domains(cdb, tmp_ctx, &domain_map);
+ if (ret != EOK || domain_map == NULL) {
+ DEBUG(0, ("Error, no domains were configured\n"));
+ *domain_names = NULL;
+ count = 0;
+ goto done;
+ }
+
+ ret = btreemap_get_keys(mem_ctx, domain_map, (const void ***)domain_names, count);
+ if (ret != EOK) {
+ DEBUG(0, ("Couldn't get domain list\n"));
+ }
done:
talloc_free(tmp_ctx);