summaryrefslogtreecommitdiff
path: root/lib/ldb/modules
diff options
context:
space:
mode:
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>2016-03-08 14:43:40 +1300
committerAndrew Bartlett <abartlet@samba.org>2016-03-09 10:32:17 +0100
commit5d6a67e9de0a5bee1e97db4284f15662d0e28717 (patch)
tree3f2fa8b1026449d89ec6f5b631825a0cac8ba122 /lib/ldb/modules
parentb797baaa6017549e8ca294e83a64dedc09162a54 (diff)
downloadsamba-5d6a67e9de0a5bee1e97db4284f15662d0e28717.tar.gz
ldb sort: allow sorting on attributes not returned in search
The attribute is added to the search request, then peeled off again before the sort module passes the results on. Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org> Reviewed-by: Garming Sam <garming@catalyst.net.nz>
Diffstat (limited to 'lib/ldb/modules')
-rw-r--r--lib/ldb/modules/sort.c44
1 files changed, 42 insertions, 2 deletions
diff --git a/lib/ldb/modules/sort.c b/lib/ldb/modules/sort.c
index 1b762f7e51b..19cf60b776b 100644
--- a/lib/ldb/modules/sort.c
+++ b/lib/ldb/modules/sort.c
@@ -56,6 +56,7 @@ struct sort_context {
char **referrals;
unsigned int num_msgs;
unsigned int num_refs;
+ const char *extra_sort_key;
const struct ldb_schema_attribute *a;
int sort_result;
@@ -162,7 +163,9 @@ static int server_sort_results(struct sort_context *ac)
ares->type = LDB_REPLY_ENTRY;
ares->message = talloc_move(ares, &ac->msgs[i]);
-
+ if (ac->extra_sort_key) {
+ ldb_msg_remove_attr(ares->message, ac->extra_sort_key);
+ }
ret = ldb_module_send_entry(ac->req, ares->message, ares->controls);
if (ret != LDB_SUCCESS) {
return ret;
@@ -256,6 +259,9 @@ static int server_sort_search(struct ldb_module *module, struct ldb_request *req
struct sort_context *ac;
struct ldb_context *ldb;
int ret;
+ const char * const *attrs;
+ size_t n_attrs, i;
+ const char *sort_attr;
ldb = ldb_module_get_ctx(module);
@@ -303,6 +309,40 @@ static int server_sort_search(struct ldb_module *module, struct ldb_request *req
}
}
+ /* We are asked to sort on an attribute, and if that attribute is not
+ already in the search attributes we need to add it (and later
+ remove it on the return journey).
+ */
+ sort_attr = sort_ctrls[0]->attributeName;
+ if (req->op.search.attrs == NULL) {
+ /* This means all non-operational attributes, which means
+ there's nothing to add. */
+ attrs = NULL;
+ } else {
+ n_attrs = 0;
+ while (req->op.search.attrs[n_attrs] != NULL) {
+ if (sort_attr &&
+ strcmp(req->op.search.attrs[n_attrs], sort_attr) == 0) {
+ sort_attr = NULL;
+ }
+ n_attrs++;
+ }
+
+ if (sort_attr == NULL) {
+ attrs = req->op.search.attrs;
+ } else {
+ const char **tmp = talloc_array(ac, const char *, n_attrs + 2);
+
+ for (i = 0; i < n_attrs; i++) {
+ tmp[i] = req->op.search.attrs[i];
+ }
+ ac->extra_sort_key = sort_attr;
+ tmp[n_attrs] = sort_attr;
+ tmp[n_attrs + 1] = NULL;
+ attrs = tmp;
+ }
+ }
+
ac->attributeName = sort_ctrls[0]->attributeName;
ac->orderingRule = sort_ctrls[0]->orderingRule;
ac->reverse = sort_ctrls[0]->reverse;
@@ -311,7 +351,7 @@ static int server_sort_search(struct ldb_module *module, struct ldb_request *req
req->op.search.base,
req->op.search.scope,
req->op.search.tree,
- req->op.search.attrs,
+ attrs,
req->controls,
ac,
server_sort_search_callback,