summaryrefslogtreecommitdiffstats
path: root/source4/lib/ldb/modules/paged_results.c
blob: fd0cc7c90b05108fe64d39efba799e51c185fdd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* 
   ldb database library

   Copyright (C) Simo Sorce  2005

     ** NOTE! The following LGPL license applies to the ldb
     ** library. This does NOT imply that all of Samba is released
     ** under the LGPL
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*
 *  Name: ldb
 *
 *  Component: ldb paged results control module
 *
 *  Description: this module caches a complete search and sends back
 *  		 results in chunks as asked by the client
 *
 *  Author: Simo Sorce
 */

#include "includes.h"
#include "ldb/include/includes.h"

struct results_store {
	char *cookie;
	time_t timestamp;
	int num_sent;
	struct ldb_result *result;
	struct results_store *prev;
	struct results_store *next;
};

struct private_data {

	int next_free_id;
	struct results_store *store;
	
};


static struct results_store *new_store(struct private_data *priv)
{
	struct results_store *new;
	int new_id = priv->next_free_id++;

	/* TODO: we should have a limit on the number of
	 * outstanding paged searches
	 */

	new = talloc(priv, struct results_store);
	if (!new) return NULL;

	new->cookie = talloc_asprintf(new, "%d", new_id);
	if (!new->cookie) {
		talloc_free(new);
		return NULL;
	}

	new->timestamp = time(NULL);

	new->num_sent = 0;
	new->result = NULL;

	/* put this entry as first */
	new->prev = NULL;
	new->next = priv->store;
	if (priv->store != NULL) priv->store->prev = new;
	priv->store = new;

	return new;
}

static void remove_store(struct results_store *store)
{
	if (store->prev) {
		store->prev->next = store->next;
	}
	if (store->next) {
		store->next->prev = store->prev;
	}
	talloc_free(store);
}

/* search */
static int paged_search(struct ldb_module *module, struct ldb_request *req)
{
	struct private_data *private_data = talloc_get_type(module->private_data, struct private_data);
	struct results_store *current = NULL;
	struct ldb_result *paged_result;
	struct ldb_control **saved_controls;
	struct ldb_control *control;
	struct ldb_paged_control *paged_ctrl;
	struct ldb_paged_control *paged_ret;
	int i, ret;

	/* check if there's a paged request control */
	control = get_control_from_list(req->controls, LDB_CONTROL_PAGED_RESULTS_OID);

	if (control == NULL) {
		/* not found go on */
		return ldb_next_request(module, req);
	}

	paged_ctrl = talloc_get_type(control->data, struct ldb_paged_control);
	if (!paged_ctrl) {
		return LDB_ERR_PROTOCOL_ERROR;
	}

	/* check if it is a continuation search the store */
	if (paged_ctrl->cookie_len != 0) {
		for (current = private_data->store; current; current = current->next) {
			if (strcmp(current->cookie, paged_ctrl->cookie) == 0) {
				current->timestamp = time(NULL);
				break;
			}
		}
		if (current == NULL) {
			return LDB_ERR_UNWILLING_TO_PERFORM;
		}
	}

	/* is this a brand new paged request ? */
	if (current == NULL) {

		/* save controls list and remove this one from the list */
		if (!save_controls(control, req, &saved_controls)) {
			return LDB_ERR_OTHER;
		}

		/* perform the search */
		ret = ldb_next_request(module, req);

		/* restore original controls list */
		if (req->controls) talloc_free(req->controls);
		req->controls = saved_controls;

		if (ret != LDB_SUCCESS) {
			return ret;
		}

		/* create a new entry in the cache */
		current = new_store(private_data);
		if (!current) {
			return LDB_ERR_OTHER;
		}

		/* steal the search result */
		current->result = talloc_steal(current, req->op.search.res);
		req->op.search.res = NULL;
	}

	/* create a container for the next batch of results */
	paged_result = talloc(current, struct ldb_result);
	if (!paged_result) {
		return LDB_ERR_OTHER;
	}
	paged_result->count = 0;
	paged_result->msgs = NULL;
	paged_result->controls = NULL;

	/* check if it is an abandon */
	if (paged_ctrl->size == 0) {
		req->op.search.res = talloc_steal(private_data, paged_result);
		remove_store(current);
		return LDB_SUCCESS;
	}

	/* return a batch of results */
		
	paged_result->controls = talloc_array(paged_result, struct ldb_control *, 2);
	if (!paged_result->controls) {
		talloc_free(paged_result);
		return LDB_ERR_OTHER;
	}

	paged_result->controls[0] = talloc(paged_result->controls, struct ldb_control);
	if (!paged_result->controls[0]) {
		talloc_free(paged_result);
		return LDB_ERR_OTHER;
	}
	paged_result->controls[0]->oid = talloc_strdup(paged_result->controls[0], LDB_CONTROL_PAGED_RESULTS_OID);
	paged_result->controls[0]->critical = 0;
	paged_result->controls[1] = NULL;

	paged_ret = talloc(paged_result->controls[0], struct ldb_paged_control);
	if (!paged_ret) {
		talloc_free(paged_result);
		return LDB_ERR_OTHER;
	}
	paged_result->controls[0]->data = paged_ret;

	if (paged_ctrl->size >= current->result->count) {
		paged_ret->size = 0;
		paged_ret->cookie = NULL;
		paged_ret->cookie_len = 0;
		paged_result->count = current->result->count;
		current->result->count = 0;
	} else {
		paged_ret->size = current->result->count;
		paged_ret->cookie = talloc_strdup(paged_ret, current->cookie);
		paged_ret->cookie_len = strlen(paged_ret->cookie) + 1;
		paged_result->count = paged_ctrl->size;
		current->result->count -= paged_ctrl->size;
	}

	paged_result->msgs = talloc_array(paged_result, struct ldb_message *, paged_result->count + 1);
	if (!paged_result->msgs) {
		talloc_free(paged_result);
		return LDB_ERR_OTHER;
	}
	for (i = 0; i < paged_result->count; i++) {
		paged_result->msgs[i] = talloc_steal(paged_result->msgs, current->result->msgs[current->num_sent + i]);
	}
	current->num_sent += paged_result->count;
	paged_result->msgs[paged_result->count] = NULL;

	req->op.search.res = paged_result;

	return LDB_SUCCESS;	
}

static int paged_request(struct ldb_module *module, struct ldb_request *req)
{
	switch (req->operation) {

	case LDB_REQ_SEARCH:
		return paged_search(module, req);

	default:
		return ldb_next_request(module, req);

	}
}

static int paged_request_init(struct ldb_module *module)
{
	struct private_data *data;
	struct ldb_request *req;
	int ret;

	data = talloc(module, struct private_data);
	if (data == NULL) {
		return LDB_ERR_OTHER;
	}
	
	data->next_free_id = 1;
	data->store = NULL;
	module->private_data = data;

	req = talloc(module, struct ldb_request);
	if (req == NULL) {
		return LDB_ERR_OPERATIONS_ERROR;
	}

	req->operation = LDB_REQ_REGISTER;
	req->op.reg.oid = LDB_CONTROL_PAGED_RESULTS_OID;
	req->controls = NULL;

	ret = ldb_request(module->ldb, req);
	if (ret != LDB_SUCCESS) {
		ldb_debug(module->ldb, LDB_DEBUG_ERROR, "paged_request: Unable to register control with rootdse!\n");
		talloc_free(req);
		return LDB_ERR_OTHER;
	}

	talloc_free(req);
	return ldb_next_init(module);
}

static const struct ldb_module_ops paged_ops = {
	.name		   	= "paged_results",
	.request        = paged_request,
	.init_context 	= paged_request_init
};

int ldb_paged_results_init(void)
{
	return ldb_register_module(&paged_ops);
}