summaryrefslogtreecommitdiffstats
path: root/src/confdb/confdb.c
diff options
context:
space:
mode:
authorSimo Sorce <simo@redhat.com>2016-01-20 10:33:39 -0500
committerJakub Hrozek <jhrozek@redhat.com>2016-06-29 21:46:12 +0200
commit052f8aa2034f7b091097dc5fdafc201b7d684525 (patch)
tree5421bad602bbab546c9704e049873c6cab4c8c2d /src/confdb/confdb.c
parente5911e72198df96ec7cfe486ff66363c2297a5f7 (diff)
downloadsssd-052f8aa2034f7b091097dc5fdafc201b7d684525.tar.gz
sssd-052f8aa2034f7b091097dc5fdafc201b7d684525.tar.xz
sssd-052f8aa2034f7b091097dc5fdafc201b7d684525.zip
ConfDB: Add helper function to get "subsections"
The secrets database will have "subsections", ie sections that are in the "secrets" namespace and look like this: [secrets/<path>] This function allows to source any section under secrets/ or under any arbitrary sub-path. Related: https://fedorahosted.org/sssd/ticket/2913 Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
Diffstat (limited to 'src/confdb/confdb.c')
-rw-r--r--src/confdb/confdb.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/confdb/confdb.c b/src/confdb/confdb.c
index b99c6cf40..512d93f43 100644
--- a/src/confdb/confdb.c
+++ b/src/confdb/confdb.c
@@ -1531,3 +1531,95 @@ done:
talloc_free(tmp_ctx);
return ret;
}
+
+int confdb_get_sub_sections(TALLOC_CTX *mem_ctx,
+ struct confdb_ctx *cdb,
+ const char *section,
+ char ***sections,
+ int *num_sections)
+{
+ TALLOC_CTX *tmp_ctx = NULL;
+ char *secdn;
+ struct ldb_dn *base = NULL;
+ struct ldb_result *res = NULL;
+ static const char *attrs[] = {"cn", NULL};
+ char **names;
+ int base_comp_num;
+ int num;
+ int i;
+ int ret;
+
+ tmp_ctx = talloc_new(mem_ctx);
+ if (tmp_ctx == NULL) {
+ return ENOMEM;
+ }
+
+ ret = parse_section(tmp_ctx, section, &secdn, NULL);
+ if (ret != EOK) {
+ goto done;
+ }
+
+ base = ldb_dn_new(tmp_ctx, cdb->ldb, secdn);
+ if (base == NULL) {
+ ret = ENOMEM;
+ goto done;
+ }
+
+ base_comp_num = ldb_dn_get_comp_num(base);
+
+ ret = ldb_search(cdb->ldb, tmp_ctx, &res, base, LDB_SCOPE_SUBTREE,
+ attrs, NULL);
+ if (ret != LDB_SUCCESS) {
+ ret = EIO;
+ goto done;
+ }
+
+ names = talloc_zero_array(tmp_ctx, char *, res->count + 1);
+ if (names == NULL) {
+ ret = ENOMEM;
+ goto done;
+ }
+
+ for (num = 0, i = 0; i < res->count; i++) {
+ const struct ldb_val *val;
+ char *name;
+ int n;
+ int j;
+
+ n = ldb_dn_get_comp_num(res->msgs[i]->dn);
+ if (n == base_comp_num) continue;
+
+ name = NULL;
+ for (j = n - base_comp_num - 1; j >= 0; j--) {
+ val = ldb_dn_get_component_val(res->msgs[i]->dn, j);
+ if (name == NULL) {
+ name = talloc_strndup(names,
+ (const char *)val->data, val->length);
+ } else {
+ name = talloc_asprintf(names, "%s/%.*s", name,
+ (int)val->length,
+ (const char *)val->data);
+ }
+ if (name == NULL) {
+ ret = ENOMEM;
+ goto done;
+ }
+ }
+
+ names[num] = name;
+ if (names[num] == NULL) {
+ ret = ENOMEM;
+ goto done;
+ }
+
+ num++;
+ }
+
+ *sections = talloc_steal(mem_ctx, names);
+ *num_sections = num;
+ ret = EOK;
+
+done:
+ talloc_free(tmp_ctx);
+ return ret;
+}