Debian Codegraph @

servers/slapd/overlays/sssvlv.c

raw
1 /* sssvlv.c - server side sort / virtual list view */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2009-2024 The OpenLDAP Foundation.
6 * Portions copyright 2009 Symas Corporation.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
12 *
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
16 */
17 /* ACKNOWLEDGEMENTS:
18 * This work was initially developed by Howard Chu for inclusion in
19 * OpenLDAP Software. Support for multiple sorts per connection added
20 * by Raphael Ouazana.
21 */
22
23 #include "portable.h"
24
25 #ifdef SLAPD_OVER_SSSVLV
26
27 #include <stdio.h>
28
29 #include <ac/string.h>
30 #include <ac/ctype.h>
31
32 #include <ldap_avl.h>
33
34 #include "slap.h"
35 #include "lutil.h"
36 #include "slap-config.h"
37
38 #include "../../../libraries/liblber/lber-int.h" /* ber_rewind */
39
40 /* RFC2891: Server Side Sorting
41 * RFC2696: Paged Results
42 */
43 #ifndef LDAP_MATCHRULE_IDENTIFIER
44 #define LDAP_MATCHRULE_IDENTIFIER 0x80L
45 #define LDAP_REVERSEORDER_IDENTIFIER 0x81L
46 #define LDAP_ATTRTYPES_IDENTIFIER 0x80L
47 #endif
48
49 /* draft-ietf-ldapext-ldapv3-vlv-09.txt: Virtual List Views
50 */
51 #ifndef LDAP_VLVBYINDEX_IDENTIFIER
52 #define LDAP_VLVBYINDEX_IDENTIFIER 0xa0L
53 #define LDAP_VLVBYVALUE_IDENTIFIER 0x81L
54 #define LDAP_VLVCONTEXT_IDENTIFIER 0x04L
55
56 #define LDAP_VLV_SSS_MISSING 0x4C
57 #define LDAP_VLV_RANGE_ERROR 0x4D
58 #endif
59
60 #define SAFESTR(macro_str, macro_def) ((macro_str) ? (macro_str) : (macro_def))
61
62 #define SSSVLV_DEFAULT_MAX_KEYS 5
63 #define SSSVLV_DEFAULT_MAX_REQUEST_PER_CONN 5
64
65 #define NO_PS_COOKIE (PagedResultsCookie) -1
66 #define NO_VC_CONTEXT (unsigned long) -1
67
68 typedef struct vlv_ctrl {
69 int vc_before;
70 int vc_after;
71 int vc_offset;
72 int vc_count;
73 struct berval vc_value;
74 unsigned long vc_context;
75 } vlv_ctrl;
76
77 typedef struct sort_key
78 {
79 AttributeDescription *sk_ad;
80 MatchingRule *sk_ordering;
81 int sk_direction; /* 1=normal, -1=reverse */
82 } sort_key;
83
84 typedef struct sort_ctrl {
85 int sc_nkeys;
86 sort_key sc_keys[1];
87 } sort_ctrl;
88
89
90 typedef struct sort_node
91 {
92 int sn_conn;
93 int sn_session;
94 struct berval sn_dn;
95 struct berval *sn_vals;
96 } sort_node;
97
98 typedef struct sssvlv_info
99 {
100 int svi_max; /* max concurrent sorts */
101 int svi_num; /* current # sorts */
102 int svi_max_keys; /* max sort keys per request */
103 int svi_max_percon; /* max concurrent sorts per con */
104 } sssvlv_info;
105
106 typedef struct sort_op
107 {
108 TAvlnode *so_tree;
109 sort_ctrl *so_ctrl;
110 sssvlv_info *so_info;
111 int so_paged;
112 int so_page_size;
113 int so_nentries;
114 int so_vlv;
115 int so_vlv_rc;
116 int so_vlv_target;
117 int so_session;
118 unsigned long so_vcontext;
119 int so_running;
120 } sort_op;
121
122 /* There is only one conn table for all overlay instances */
123 /* Each conn can handle one session by context */
124 static sort_op ***sort_conns;
125 static ldap_pvt_thread_mutex_t sort_conns_mutex;
126 static int ov_count;
127 static const char *debug_header = "sssvlv";
128
129 static int sss_cid;
130 static int vlv_cid;
131
132 /* RFC 2981 Section 2.2
133 * If a sort key is a multi-valued attribute, and an entry happens to
134 * have multiple values for that attribute and no other controls are
135 * present that affect the sorting order, then the server SHOULD use the
136 * least value (according to the ORDERING rule for that attribute).
137 */
138 static struct berval* select_value(
139 Attribute *attr,
140 sort_key *key )
141 {
142 struct berval* ber1, *ber2;
143 MatchingRule *mr = key->sk_ordering;
144 unsigned i;
145 int cmp;
146
147 ber1 = &(attr->a_nvals[0]);
148 ber2 = ber1+1;
149 for ( i = 1; i < attr->a_numvals; i++,ber2++ ) {
150 mr->smr_match( &cmp, 0, mr->smr_syntax, mr, ber1, ber2 );
151 if ( cmp > 0 ) {
152 ber1 = ber2;
153 }
154 }
155
156 Debug(LDAP_DEBUG_TRACE, "%s: value selected for compare: %s\n",
157 debug_header,
158 SAFESTR(ber1->bv_val, "<Empty>") );
159
160 return ber1;
161 }
162
163 static int node_cmp( const void* val1, const void* val2 )
164 {
165 sort_node *sn1 = (sort_node *)val1;
166 sort_node *sn2 = (sort_node *)val2;
167 sort_ctrl *sc;
168 MatchingRule *mr;
169 int i, cmp = 0;
170 assert( sort_conns[sn1->sn_conn]
171 && sort_conns[sn1->sn_conn][sn1->sn_session]
172 && sort_conns[sn1->sn_conn][sn1->sn_session]->so_ctrl );
173 sc = sort_conns[sn1->sn_conn][sn1->sn_session]->so_ctrl;
174
175 for ( i=0; cmp == 0 && i<sc->sc_nkeys; i++ ) {
176 if ( BER_BVISNULL( &sn1->sn_vals[i] )) {
177 if ( BER_BVISNULL( &sn2->sn_vals[i] ))
178 cmp = 0;
179 else
180 cmp = sc->sc_keys[i].sk_direction;
181 } else if ( BER_BVISNULL( &sn2->sn_vals[i] )) {
182 cmp = sc->sc_keys[i].sk_direction * -1;
183 } else {
184 mr = sc->sc_keys[i].sk_ordering;
185 mr->smr_match( &cmp, 0, mr->smr_syntax, mr,
186 &sn1->sn_vals[i], &sn2->sn_vals[i] );
187 if ( cmp )
188 cmp *= sc->sc_keys[i].sk_direction;
189 }
190 }
191 return cmp;
192 }
193
194 static int node_insert( const void *val1, const void *val2 )
195 {
196 /* Never return equal so that new entries are always inserted */
197 return node_cmp( val1, val2 ) < 0 ? -1 : 1;
198 }
199
200 static int pack_vlv_response_control(
201 Operation *op,
202 SlapReply *rs,
203 sort_op *so,
204 LDAPControl **ctrlsp )
205 {
206 LDAPControl *ctrl;
207 BerElementBuffer berbuf;
208 BerElement *ber = (BerElement *)&berbuf;
209 struct berval cookie, bv;
210 int rc;
211
212 ber_init2( ber, NULL, LBER_USE_DER );
213 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
214
215 rc = ber_printf( ber, "{iie", so->so_vlv_target, so->so_nentries,
216 so->so_vlv_rc );
217
218 if ( rc != -1 && so->so_vcontext ) {
219 cookie.bv_val = (char *)&so->so_vcontext;
220 cookie.bv_len = sizeof(so->so_vcontext);
221 rc = ber_printf( ber, "tO", LDAP_VLVCONTEXT_IDENTIFIER, &cookie );
222 }
223
224 if ( rc != -1 ) {
225 rc = ber_printf( ber, "}" );
226 }
227
228 if ( rc != -1 ) {
229 rc = ber_flatten2( ber, &bv, 0 );
230 }
231
232 if ( rc != -1 ) {
233 ctrl = (LDAPControl *)op->o_tmpalloc( sizeof(LDAPControl)+
234 bv.bv_len, op->o_tmpmemctx );
235 ctrl->ldctl_oid = LDAP_CONTROL_VLVRESPONSE;
236 ctrl->ldctl_iscritical = 0;
237 ctrl->ldctl_value.bv_val = (char *)(ctrl+1);
238 ctrl->ldctl_value.bv_len = bv.bv_len;
239 AC_MEMCPY( ctrl->ldctl_value.bv_val, bv.bv_val, bv.bv_len );
240 ctrlsp[0] = ctrl;
241 } else {
242 ctrlsp[0] = NULL;
243 rs->sr_err = LDAP_OTHER;
244 }
245
246 ber_free_buf( ber );
247
248 return rs->sr_err;
249 }
250
251 static int pack_pagedresult_response_control(
252 Operation *op,
253 SlapReply *rs,
254 sort_op *so,
255 LDAPControl **ctrlsp )
256 {
257 LDAPControl *ctrl;
258 BerElementBuffer berbuf;
259 BerElement *ber = (BerElement *)&berbuf;
260 PagedResultsCookie resp_cookie;
261 struct berval cookie, bv;
262 int rc;
263
264 ber_init2( ber, NULL, LBER_USE_DER );
265 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
266
267 if ( so->so_nentries > 0 ) {
268 resp_cookie = ( PagedResultsCookie )so->so_tree;
269 cookie.bv_len = sizeof( PagedResultsCookie );
270 cookie.bv_val = (char *)&resp_cookie;
271 } else {
272 resp_cookie = ( PagedResultsCookie )0;
273 BER_BVZERO( &cookie );
274 }
275
276 op->o_conn->c_pagedresults_state.ps_cookie = resp_cookie;
277 op->o_conn->c_pagedresults_state.ps_count
278 = ((PagedResultsState *)op->o_pagedresults_state)->ps_count
279 + rs->sr_nentries;
280
281 rc = ber_printf( ber, "{iO}", so->so_nentries, &cookie );
282 if ( rc != -1 ) {
283 rc = ber_flatten2( ber, &bv, 0 );
284 }
285
286 if ( rc != -1 ) {
287 ctrl = (LDAPControl *)op->o_tmpalloc( sizeof(LDAPControl)+
288 bv.bv_len, op->o_tmpmemctx );
289 ctrl->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
290 ctrl->ldctl_iscritical = 0;
291 ctrl->ldctl_value.bv_val = (char *)(ctrl+1);
292 ctrl->ldctl_value.bv_len = bv.bv_len;
293 AC_MEMCPY( ctrl->ldctl_value.bv_val, bv.bv_val, bv.bv_len );
294 ctrlsp[0] = ctrl;
295 } else {
296 ctrlsp[0] = NULL;
297 rs->sr_err = LDAP_OTHER;
298 }
299
300 ber_free_buf( ber );
301
302 return rs->sr_err;
303 }
304
305 static int pack_sss_response_control(
306 Operation *op,
307 SlapReply *rs,
308 LDAPControl **ctrlsp )
309 {
310 LDAPControl *ctrl;
311 BerElementBuffer berbuf;
312 BerElement *ber = (BerElement *)&berbuf;
313 struct berval bv;
314 int rc;
315
316 ber_init2( ber, NULL, LBER_USE_DER );
317 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
318
319 /* Pack error code */
320 rc = ber_printf(ber, "{e}", rs->sr_err);
321
322 if ( rc != -1)
323 rc = ber_flatten2( ber, &bv, 0 );
324
325 if ( rc != -1 ) {
326 ctrl = (LDAPControl *)op->o_tmpalloc( sizeof(LDAPControl)+
327 bv.bv_len, op->o_tmpmemctx );
328 ctrl->ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
329 ctrl->ldctl_iscritical = 0;
330 ctrl->ldctl_value.bv_val = (char *)(ctrl+1);
331 ctrl->ldctl_value.bv_len = bv.bv_len;
332 AC_MEMCPY( ctrl->ldctl_value.bv_val, bv.bv_val, bv.bv_len );
333 ctrlsp[0] = ctrl;
334 } else {
335 ctrlsp[0] = NULL;
336 rs->sr_err = LDAP_OTHER;
337 }
338
339 ber_free_buf( ber );
340
341 return rs->sr_err;
342 }
343
344 /* Return the session id or -1 if unknown */
345 static int find_session_by_so(
346 int svi_max_percon,
347 int conn_id,
348 sort_op *so )
349 {
350 int sess_id;
351 if (so == NULL) {
352 return -1;
353 }
354 for (sess_id = 0; sess_id < svi_max_percon; sess_id++) {
355 if ( sort_conns[conn_id] && sort_conns[conn_id][sess_id] == so )
356 return sess_id;
357 }
358 return -1;
359 }
360
361 /* Return the session id or -1 if unknown */
362 static int find_session_by_context(
363 int svi_max_percon,
364 int conn_id,
365 unsigned long vc_context,
366 PagedResultsCookie ps_cookie )
367 {
368 int sess_id;
369 for(sess_id = 0; sess_id < svi_max_percon; sess_id++) {
370 if( sort_conns[conn_id] && sort_conns[conn_id][sess_id] &&
371 ( sort_conns[conn_id][sess_id]->so_vcontext == vc_context ||
372 (PagedResultsCookie) sort_conns[conn_id][sess_id]->so_tree == ps_cookie ) )
373 return sess_id;
374 }
375 return -1;
376 }
377
378 static int find_next_session(
379 int svi_max_percon,
380 int conn_id )
381 {
382 int sess_id;
383 assert(sort_conns[conn_id] != NULL);
384 for(sess_id = 0; sess_id < svi_max_percon; sess_id++) {
385 if(!sort_conns[conn_id][sess_id]) {
386 return sess_id;
387 }
388 }
389 if (sess_id >= svi_max_percon) {
390 return -1;
391 } else {
392 return sess_id;
393 }
394 }
395
396 static void free_sort_op( Connection *conn, sort_op *so )
397 {
398 int sess_id;
399
400 ldap_pvt_thread_mutex_lock( &sort_conns_mutex );
401 sess_id = find_session_by_so( so->so_info->svi_max_percon, conn->c_conn_idx, so );
402 if ( sess_id > -1 ) {
403 sort_conns[conn->c_conn_idx][sess_id] = NULL;
404 so->so_info->svi_num--;
405 }
406 ldap_pvt_thread_mutex_unlock( &sort_conns_mutex );
407
408 if ( sess_id > -1 ){
409 if ( so->so_tree ) {
410 if ( so->so_paged > SLAP_CONTROL_IGNORED ) {
411 TAvlnode *cur_node, *next_node;
412 cur_node = so->so_tree;
413 while ( cur_node ) {
414 next_node = ldap_tavl_next( cur_node, TAVL_DIR_RIGHT );
415 ch_free( cur_node->avl_data );
416 ber_memfree( cur_node );
417
418 cur_node = next_node;
419 }
420 } else {
421 ldap_tavl_free( so->so_tree, ch_free );
422 }
423 so->so_tree = NULL;
424 }
425
426 ch_free( so );
427 }
428 }
429
430 static void free_sort_ops( Connection *conn, sort_op **sos, int svi_max_percon )
431 {
432 int sess_id;
433 sort_op *so;
434
435 for( sess_id = 0; sess_id < svi_max_percon ; sess_id++ ) {
436 so = sort_conns[conn->c_conn_idx][sess_id];
437 if ( so ) {
438 free_sort_op( conn, so );
439 sort_conns[conn->c_conn_idx][sess_id] = NULL;
440 }
441 }
442 }
443
444 static void send_list(
445 Operation *op,
446 SlapReply *rs,
447 sort_op *so)
448 {
449 TAvlnode *cur_node, *tmp_node;
450 vlv_ctrl *vc = op->o_controls[vlv_cid];
451 int i, j, dir, rc;
452 BackendDB *be;
453 Entry *e;
454 LDAPControl *ctrls[2];
455
456 rs->sr_attrs = op->ors_attrs;
457
458 /* FIXME: it may be better to just flatten the tree into
459 * an array before doing all of this...
460 */
461
462 /* Are we just counting an offset? */
463 if ( BER_BVISNULL( &vc->vc_value )) {
464 if ( vc->vc_offset == vc->vc_count ) {
465 /* wants the last entry in the list */
466 cur_node = ldap_tavl_end(so->so_tree, TAVL_DIR_RIGHT);
467 so->so_vlv_target = so->so_nentries;
468 } else if ( vc->vc_offset == 1 ) {
469 /* wants the first entry in the list */
470 cur_node = ldap_tavl_end(so->so_tree, TAVL_DIR_LEFT);
471 so->so_vlv_target = 1;
472 } else {
473 int target;
474 /* Just iterate to the right spot */
475 if ( vc->vc_count && vc->vc_count != so->so_nentries ) {
476 if ( vc->vc_offset > vc->vc_count )
477 goto range_err;
478 target = so->so_nentries * vc->vc_offset / vc->vc_count;
479 } else {
480 if ( vc->vc_offset > so->so_nentries ) {
481 range_err:
482 so->so_vlv_rc = LDAP_VLV_RANGE_ERROR;
483 pack_vlv_response_control( op, rs, so, ctrls );
484 ctrls[1] = NULL;
485 slap_add_ctrls( op, rs, ctrls );
486 rs->sr_err = LDAP_VLV_ERROR;
487 return;
488 }
489 target = vc->vc_offset;
490 }
491 so->so_vlv_target = target;
492 /* Start at left and go right, or start at right and go left? */
493 if ( target < so->so_nentries / 2 ) {
494 cur_node = ldap_tavl_end(so->so_tree, TAVL_DIR_LEFT);
495 dir = TAVL_DIR_RIGHT;
496 } else {
497 cur_node = ldap_tavl_end(so->so_tree, TAVL_DIR_RIGHT);
498 dir = TAVL_DIR_LEFT;
499 target = so->so_nentries - target + 1;
500 }
501 for ( i=1; i<target; i++ )
502 cur_node = ldap_tavl_next( cur_node, dir );
503 }
504 } else {
505 /* we're looking for a specific value */
506 sort_ctrl *sc = so->so_ctrl;
507 MatchingRule *mr = sc->sc_keys[0].sk_ordering;
508 sort_node *sn;
509 struct berval bv;
510
511 if ( mr->smr_normalize ) {
512 rc = mr->smr_normalize( SLAP_MR_VALUE_OF_SYNTAX,
513 mr->smr_syntax, mr, &vc->vc_value, &bv, op->o_tmpmemctx );
514 if ( rc ) {
515 so->so_vlv_rc = LDAP_INAPPROPRIATE_MATCHING;
516 pack_vlv_response_control( op, rs, so, ctrls );
517 ctrls[1] = NULL;
518 slap_add_ctrls( op, rs, ctrls );
519 rs->sr_err = LDAP_VLV_ERROR;
520 return;
521 }
522 } else {
523 bv = vc->vc_value;
524 }
525
526 sn = op->o_tmpalloc( sizeof(sort_node) +
527 sc->sc_nkeys * sizeof(struct berval), op->o_tmpmemctx );
528 sn->sn_vals = (struct berval *)(sn+1);
529 sn->sn_conn = op->o_conn->c_conn_idx;
530 sn->sn_session = find_session_by_so( so->so_info->svi_max_percon, op->o_conn->c_conn_idx, so );
531 sn->sn_vals[0] = bv;
532 for (i=1; i<sc->sc_nkeys; i++) {
533 BER_BVZERO( &sn->sn_vals[i] );
534 }
535 cur_node = ldap_tavl_find3( so->so_tree, sn, node_cmp, &j );
536 /* didn't find >= match */
537 if ( j > 0 ) {
538 if ( cur_node )
539 cur_node = ldap_tavl_next( cur_node, TAVL_DIR_RIGHT );
540 }
541 op->o_tmpfree( sn, op->o_tmpmemctx );
542
543 if ( !cur_node ) {
544 so->so_vlv_target = so->so_nentries + 1;
545 } else {
546 sort_node *sn = so->so_tree->avl_data;
547 /* start from the left or the right side? */
548 mr->smr_match( &i, 0, mr->smr_syntax, mr, &bv, &sn->sn_vals[0] );
549 if ( i > 0 ) {
550 tmp_node = ldap_tavl_end(so->so_tree, TAVL_DIR_RIGHT);
551 dir = TAVL_DIR_LEFT;
552 } else {
553 tmp_node = ldap_tavl_end(so->so_tree, TAVL_DIR_LEFT);
554 dir = TAVL_DIR_RIGHT;
555 }
556 for (i=0; tmp_node != cur_node;
557 tmp_node = ldap_tavl_next( tmp_node, dir ), i++);
558 so->so_vlv_target = (dir == TAVL_DIR_RIGHT) ? i+1 : so->so_nentries - i;
559 }
560 if ( bv.bv_val != vc->vc_value.bv_val )
561 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
562 }
563 if ( !cur_node ) {
564 i = 1;
565 cur_node = ldap_tavl_end(so->so_tree, TAVL_DIR_RIGHT);
566 } else {
567 i = 0;
568 }
569 for ( ; i<vc->vc_before; i++ ) {
570 tmp_node = ldap_tavl_next( cur_node, TAVL_DIR_LEFT );
571 if ( !tmp_node ) break;
572 cur_node = tmp_node;
573 }
574 j = i + vc->vc_after + 1;
575 be = op->o_bd;
576 for ( i=0; i<j; i++ ) {
577 sort_node *sn = cur_node->avl_data;
578
579 if ( slapd_shutdown ) break;
580
581 op->o_bd = select_backend( &sn->sn_dn, 0 );
582 e = NULL;
583 rc = be_entry_get_rw( op, &sn->sn_dn, NULL, NULL, 0, &e );
584
585 if ( e && rc == LDAP_SUCCESS ) {
586 rs->sr_entry = e;
587 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
588 rs->sr_err = send_search_entry( op, rs );
589 if ( rs->sr_err == LDAP_UNAVAILABLE )
590 break;
591 }
592 cur_node = ldap_tavl_next( cur_node, TAVL_DIR_RIGHT );
593 if ( !cur_node ) break;
594 }
595 so->so_vlv_rc = LDAP_SUCCESS;
596
597 op->o_bd = be;
598 }
599
600 static void send_page( Operation *op, SlapReply *rs, sort_op *so )
601 {
602 TAvlnode *cur_node = so->so_tree;
603 TAvlnode *next_node = NULL;
604 BackendDB *be = op->o_bd;
605 Entry *e;
606 int rc;
607
608 rs->sr_attrs = op->ors_attrs;
609
610 while ( cur_node && rs->sr_nentries < so->so_page_size ) {
611 sort_node *sn = cur_node->avl_data;
612
613 if ( slapd_shutdown ) break;
614
615 next_node = ldap_tavl_next( cur_node, TAVL_DIR_RIGHT );
616
617 op->o_bd = select_backend( &sn->sn_dn, 0 );
618 e = NULL;
619 rc = be_entry_get_rw( op, &sn->sn_dn, NULL, NULL, 0, &e );
620
621 ch_free( cur_node->avl_data );
622 ber_memfree( cur_node );
623
624 cur_node = next_node;
625 so->so_nentries--;
626
627 if ( e && rc == LDAP_SUCCESS ) {
628 rs->sr_entry = e;
629 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
630 rs->sr_err = send_search_entry( op, rs );
631 if ( rs->sr_err == LDAP_UNAVAILABLE )
632 break;
633 }
634 }
635
636 /* Set the first entry to send for the next page */
637 so->so_tree = next_node;
638 if ( next_node )
639 next_node->avl_left = NULL;
640
641 op->o_bd = be;
642 }
643
644 static void send_entry(
645 Operation *op,
646 SlapReply *rs,
647 sort_op *so)
648 {
649 Debug(LDAP_DEBUG_TRACE,
650 "%s: response control: status=%d, text=%s\n",
651 debug_header, rs->sr_err, SAFESTR(rs->sr_text, "<None>"));
652
653 if ( !so->so_tree )
654 return;
655
656 /* RFC 2891: If critical then send the entries iff they were
657 * successfully sorted. If non-critical send all entries
658 * whether they were sorted or not.
659 */
660 if ( (op->o_ctrlflag[sss_cid] != SLAP_CONTROL_CRITICAL) ||
661 (rs->sr_err == LDAP_SUCCESS) )
662 {
663 if ( so->so_vlv > SLAP_CONTROL_IGNORED ) {
664 send_list( op, rs, so );
665 } else {
666 /* Get the first node to send */
667 TAvlnode *start_node = ldap_tavl_end(so->so_tree, TAVL_DIR_LEFT);
668 so->so_tree = start_node;
669
670 if ( so->so_paged <= SLAP_CONTROL_IGNORED ) {
671 /* Not paged result search. Send all entries.
672 * Set the page size to the number of entries
673 * so that send_page() will send all entries.
674 */
675 so->so_page_size = so->so_nentries;
676 }
677
678 send_page( op, rs, so );
679 }
680 }
681 }
682
683 static void send_result(
684 Operation *op,
685 SlapReply *rs,
686 sort_op *so)
687 {
688 LDAPControl *ctrls[3];
689 int rc, i = 0;
690
691 rc = pack_sss_response_control( op, rs, ctrls );
692 if ( rc == LDAP_SUCCESS ) {
693 i++;
694 rc = -1;
695 if ( so->so_paged > SLAP_CONTROL_IGNORED ) {
696 rc = pack_pagedresult_response_control( op, rs, so, ctrls+1 );
697 } else if ( so->so_vlv > SLAP_CONTROL_IGNORED ) {
698 rc = pack_vlv_response_control( op, rs, so, ctrls+1 );
699 }
700 if ( rc == LDAP_SUCCESS )
701 i++;
702 }
703 ctrls[i] = NULL;
704
705 if ( ctrls[0] != NULL )
706 slap_add_ctrls( op, rs, ctrls );
707 send_ldap_result( op, rs );
708
709 if ( so->so_tree == NULL ) {
710 /* Search finished, so clean up */
711 free_sort_op( op->o_conn, so );
712 } else {
713 so->so_running = 0;
714 }
715 }
716
717 static int sssvlv_op_response(
718 Operation *op,
719 SlapReply *rs )
720 {
721 sort_ctrl *sc = op->o_controls[sss_cid];
722 sort_op *so = op->o_callback->sc_private;
723
724 if ( rs->sr_type == REP_SEARCH ) {
725 int i;
726 size_t len;
727 sort_node *sn, *sn2;
728 struct berval *bv;
729 char *ptr;
730
731 len = sizeof(sort_node) + sc->sc_nkeys * sizeof(struct berval) +
732 rs->sr_entry->e_nname.bv_len + 1;
733 sn = op->o_tmpalloc( len, op->o_tmpmemctx );
734 sn->sn_vals = (struct berval *)(sn+1);
735
736 /* Build tmp list of key values */
737 for ( i=0; i<sc->sc_nkeys; i++ ) {
738 Attribute *a = attr_find( rs->sr_entry->e_attrs,
739 sc->sc_keys[i].sk_ad );
740 if ( a ) {
741 if ( a->a_numvals > 1 ) {
742 bv = select_value( a, &sc->sc_keys[i] );
743 } else {
744 bv = a->a_nvals;
745 }
746 sn->sn_vals[i] = *bv;
747 len += bv->bv_len + 1;
748 } else {
749 BER_BVZERO( &sn->sn_vals[i] );
750 }
751 }
752
753 /* Now dup into regular memory */
754 sn2 = ch_malloc( len );
755 sn2->sn_vals = (struct berval *)(sn2+1);
756 AC_MEMCPY( sn2->sn_vals, sn->sn_vals,
757 sc->sc_nkeys * sizeof(struct berval));
758
759 ptr = (char *)(sn2->sn_vals + sc->sc_nkeys);
760 sn2->sn_dn.bv_val = ptr;
761 sn2->sn_dn.bv_len = rs->sr_entry->e_nname.bv_len;
762 AC_MEMCPY( ptr, rs->sr_entry->e_nname.bv_val,
763 rs->sr_entry->e_nname.bv_len );
764 ptr += rs->sr_entry->e_nname.bv_len;
765 *ptr++ = '\0';
766 for ( i=0; i<sc->sc_nkeys; i++ ) {
767 if ( !BER_BVISNULL( &sn2->sn_vals[i] )) {
768 AC_MEMCPY(ptr, sn2->sn_vals[i].bv_val, sn2->sn_vals[i].bv_len);
769 sn2->sn_vals[i].bv_val = ptr;
770 ptr += sn2->sn_vals[i].bv_len;
771 *ptr++ = '\0';
772 }
773 }
774 op->o_tmpfree( sn, op->o_tmpmemctx );
775 sn = sn2;
776 sn->sn_conn = op->o_conn->c_conn_idx;
777 sn->sn_session = find_session_by_so( so->so_info->svi_max_percon, op->o_conn->c_conn_idx, so );
778
779 /* Insert into the AVL tree */
780 ldap_tavl_insert(&(so->so_tree), sn, node_insert, ldap_avl_dup_error);
781
782 so->so_nentries++;
783
784 /* Collected the keys so that they can be sorted. Thus, stop
785 * the entry from propagating.
786 */
787 rs->sr_err = LDAP_SUCCESS;
788 }
789 else if ( rs->sr_type == REP_RESULT ) {
790 /* Remove serversort response callback.
791 * We don't want the entries that we are about to send to be
792 * processed by serversort response again.
793 */
794 if ( op->o_callback->sc_response == sssvlv_op_response ) {
795 op->o_callback = op->o_callback->sc_next;
796 }
797
798 send_entry( op, rs, so );
799 send_result( op, rs, so );
800 }
801
802 return rs->sr_err;
803 }
804
805 static int sssvlv_op_search(
806 Operation *op,
807 SlapReply *rs)
808 {
809 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
810 sssvlv_info *si = on->on_bi.bi_private;
811 int rc = SLAP_CB_CONTINUE;
812 int ok;
813 sort_op *so = NULL, so2;
814 sort_ctrl *sc;
815 PagedResultsState *ps;
816 vlv_ctrl *vc;
817 int sess_id;
818
819 if ( op->o_ctrlflag[sss_cid] <= SLAP_CONTROL_IGNORED ) {
820 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
821 LDAPControl *ctrls[2];
822 so2.so_vcontext = 0;
823 so2.so_vlv_target = 0;
824 so2.so_nentries = 0;
825 so2.so_vlv_rc = LDAP_VLV_SSS_MISSING;
826 so2.so_vlv = op->o_ctrlflag[vlv_cid];
827 rc = pack_vlv_response_control( op, rs, &so2, ctrls );
828 if ( rc == LDAP_SUCCESS ) {
829 ctrls[1] = NULL;
830 slap_add_ctrls( op, rs, ctrls );
831 }
832 rs->sr_err = LDAP_VLV_ERROR;
833 rs->sr_text = "Sort control is required with VLV";
834 goto leave;
835 }
836 /* Not server side sort so just continue */
837 return SLAP_CB_CONTINUE;
838 }
839
840 Debug(LDAP_DEBUG_TRACE,
841 "==> sssvlv_search: <%s> %s, control flag: %d\n",
842 op->o_req_dn.bv_val, op->ors_filterstr.bv_val,
843 op->o_ctrlflag[sss_cid]);
844
845 sc = op->o_controls[sss_cid];
846 if ( sc->sc_nkeys > si->svi_max_keys ) {
847 rs->sr_text = "Too many sort keys";
848 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
849 goto leave;
850 }
851
852 ps = ( op->o_pagedresults > SLAP_CONTROL_IGNORED ) ?
853 (PagedResultsState*)(op->o_pagedresults_state) : NULL;
854 vc = op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ?
855 op->o_controls[vlv_cid] : NULL;
856
857 if ( ps && vc ) {
858 rs->sr_text = "VLV incompatible with PagedResults";
859 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
860 goto leave;
861 }
862
863 ok = 1;
864 ldap_pvt_thread_mutex_lock( &sort_conns_mutex );
865 /* Is there already a sort running on this conn? */
866 sess_id = find_session_by_context( si->svi_max_percon, op->o_conn->c_conn_idx, vc ? vc->vc_context : NO_VC_CONTEXT, ps ? ps->ps_cookie : NO_PS_COOKIE );
867 if ( sess_id >= 0 ) {
868 so = sort_conns[op->o_conn->c_conn_idx][sess_id];
869
870 if( so->so_running > 0 ){
871 /* another thread is handling, response busy to client */
872 so = NULL;
873 ok = 0;
874 } else {
875
876 /* Is it a continuation of a VLV search? */
877 if ( !vc || so->so_vlv <= SLAP_CONTROL_IGNORED ||
878 vc->vc_context != so->so_vcontext ) {
879 /* Is it a continuation of a paged search? */
880 if ( !ps || so->so_paged <= SLAP_CONTROL_IGNORED ||
881 op->o_conn->c_pagedresults_state.ps_cookie != ps->ps_cookie ) {
882 ok = 0;
883 } else if ( !ps->ps_size ) {
884 /* Abandoning current request */
885 ok = 0;
886 so->so_nentries = 0;
887 rs->sr_err = LDAP_SUCCESS;
888 }
889 }
890 if (( vc && so->so_paged > SLAP_CONTROL_IGNORED ) ||
891 ( ps && so->so_vlv > SLAP_CONTROL_IGNORED )) {
892 /* changed from paged to vlv or vice versa, abandon */
893 ok = 0;
894 so->so_nentries = 0;
895 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
896 }
897
898 if ( ok ) {
899 /* occupy before mutex unlock */
900 so->so_running = 1;
901 }
902
903 }
904 /* Are there too many running overall? */
905 } else if ( si->svi_num >= si->svi_max ) {
906 ok = 0;
907 } else if ( ( sess_id = find_next_session(si->svi_max_percon, op->o_conn->c_conn_idx ) ) < 0 ) {
908 ok = 0;
909 } else {
910 /* OK, this connection now has a sort running */
911 si->svi_num++;
912 sort_conns[op->o_conn->c_conn_idx][sess_id] = &so2;
913 sort_conns[op->o_conn->c_conn_idx][sess_id]->so_session = sess_id;
914 }
915 ldap_pvt_thread_mutex_unlock( &sort_conns_mutex );
916 if ( ok ) {
917 /* If we're a global overlay, this check got bypassed */
918 if ( !op->ors_limit && limits_check( op, rs ))
919 return rs->sr_err;
920 /* are we continuing a VLV search? */
921 if ( so && vc && vc->vc_context ) {
922 so->so_ctrl = sc;
923 send_list( op, rs, so );
924 send_result( op, rs, so );
925 rc = LDAP_SUCCESS;
926 /* are we continuing a paged search? */
927 } else if ( so && ps && ps->ps_cookie ) {
928 so->so_ctrl = sc;
929 send_page( op, rs, so );
930 send_result( op, rs, so );
931 rc = LDAP_SUCCESS;
932 } else {
933 slap_callback *cb = op->o_tmpalloc( sizeof(slap_callback),
934 op->o_tmpmemctx );
935 /* Install serversort response callback to handle a new search */
936 if ( ps || vc ) {
937 so = ch_calloc( 1, sizeof(sort_op));
938 } else {
939 so = op->o_tmpcalloc( 1, sizeof(sort_op), op->o_tmpmemctx );
940 }
941 sort_conns[op->o_conn->c_conn_idx][sess_id] = so;
942
943 cb->sc_cleanup = NULL;
944 cb->sc_response = sssvlv_op_response;
945 cb->sc_next = op->o_callback;
946 cb->sc_private = so;
947 cb->sc_writewait = NULL;
948
949 so->so_tree = NULL;
950 so->so_ctrl = sc;
951 so->so_info = si;
952 if ( ps ) {
953 so->so_paged = op->o_pagedresults;
954 so->so_page_size = ps->ps_size;
955 op->o_pagedresults = SLAP_CONTROL_IGNORED;
956 } else {
957 so->so_paged = 0;
958 so->so_page_size = 0;
959 if ( vc ) {
960 so->so_vlv = op->o_ctrlflag[vlv_cid];
961 so->so_vlv_target = 0;
962 so->so_vlv_rc = 0;
963 } else {
964 so->so_vlv = SLAP_CONTROL_NONE;
965 }
966 }
967 so->so_session = sess_id;
968 so->so_vlv = op->o_ctrlflag[vlv_cid];
969 so->so_vcontext = (unsigned long)so;
970 so->so_nentries = 0;
971 so->so_running = 1;
972
973 op->o_callback = cb;
974 }
975 } else {
976 if ( so && !so->so_nentries ) {
977 free_sort_op( op->o_conn, so );
978 } else {
979 rs->sr_text = "Other sort requests already in progress";
980 rs->sr_err = LDAP_BUSY;
981 }
982 leave:
983 rc = rs->sr_err;
984 send_ldap_result( op, rs );
985 }
986
987 return rc;
988 }
989
990 static int get_ordering_rule(
991 AttributeDescription *ad,
992 struct berval *matchrule,
993 SlapReply *rs,
994 MatchingRule **ordering )
995 {
996 MatchingRule* mr;
997
998 if ( matchrule && matchrule->bv_val ) {
999 mr = mr_find( matchrule->bv_val );
1000 if ( mr == NULL ) {
1001 rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
1002 rs->sr_text = "serverSort control: No ordering rule";
1003 Debug(LDAP_DEBUG_TRACE, "%s: no ordering rule function for %s\n",
1004 debug_header, matchrule->bv_val );
1005 }
1006 }
1007 else {
1008 mr = ad->ad_type->sat_ordering;
1009 if ( mr == NULL ) {
1010 rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
1011 rs->sr_text = "serverSort control: No ordering rule";
1012 Debug(LDAP_DEBUG_TRACE,
1013 "%s: no ordering rule specified and no default ordering rule for attribute %s\n",
1014 debug_header, ad->ad_cname.bv_val );
1015 }
1016 }
1017
1018 *ordering = mr;
1019 return rs->sr_err;
1020 }
1021
1022 static int count_key(BerElement *ber)
1023 {
1024 char *end;
1025 ber_len_t len;
1026 ber_tag_t tag;
1027 int count = 0;
1028
1029 /* Server Side Sort Control is a SEQUENCE of SEQUENCE */
1030 for ( tag = ber_first_element( ber, &len, &end );
1031 tag == LBER_SEQUENCE;
1032 tag = ber_next_element( ber, &len, end ))
1033 {
1034 tag = ber_skip_tag( ber, &len );
1035 ber_skip_data( ber, len );
1036 ++count;
1037 }
1038 ber_rewind( ber );
1039
1040 return count;
1041 }
1042
1043 static int build_key(
1044 BerElement *ber,
1045 SlapReply *rs,
1046 sort_key *key )
1047 {
1048 struct berval attr;
1049 struct berval matchrule = BER_BVNULL;
1050 ber_int_t reverse = 0;
1051 ber_tag_t tag;
1052 ber_len_t len;
1053 MatchingRule *ordering = NULL;
1054 AttributeDescription *ad = NULL;
1055 const char *text;
1056
1057 if (( tag = ber_scanf( ber, "{" )) == LBER_ERROR ) {
1058 rs->sr_text = "serverSort control: decoding error";
1059 rs->sr_err = LDAP_PROTOCOL_ERROR;
1060 return rs->sr_err;
1061 }
1062
1063 if (( tag = ber_scanf( ber, "m", &attr )) == LBER_ERROR ) {
1064 rs->sr_text = "serverSort control: attribute decoding error";
1065 rs->sr_err = LDAP_PROTOCOL_ERROR;
1066 return rs->sr_err;
1067 }
1068
1069 tag = ber_peek_tag( ber, &len );
1070 if ( tag == LDAP_MATCHRULE_IDENTIFIER ) {
1071 if (( tag = ber_scanf( ber, "m", &matchrule )) == LBER_ERROR ) {
1072 rs->sr_text = "serverSort control: matchrule decoding error";
1073 rs->sr_err = LDAP_PROTOCOL_ERROR;
1074 return rs->sr_err;
1075 }
1076 tag = ber_peek_tag( ber, &len );
1077 }
1078
1079 if ( tag == LDAP_REVERSEORDER_IDENTIFIER ) {
1080 if (( tag = ber_scanf( ber, "b", &reverse )) == LBER_ERROR ) {
1081 rs->sr_text = "serverSort control: reverse decoding error";
1082 rs->sr_err = LDAP_PROTOCOL_ERROR;
1083 return rs->sr_err;
1084 }
1085 }
1086
1087 if (( tag = ber_scanf( ber, "}" )) == LBER_ERROR ) {
1088 rs->sr_text = "serverSort control: decoding error";
1089 rs->sr_err = LDAP_PROTOCOL_ERROR;
1090 return rs->sr_err;
1091 }
1092
1093 if ( slap_bv2ad( &attr, &ad, &text ) != LDAP_SUCCESS ) {
1094 rs->sr_text =
1095 "serverSort control: Unrecognized attribute type in sort key";
1096 Debug(LDAP_DEBUG_TRACE,
1097 "%s: Unrecognized attribute type in sort key: %s\n",
1098 debug_header, SAFESTR(attr.bv_val, "<None>") );
1099 rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
1100 return rs->sr_err;
1101 }
1102
1103 /* get_ordering_rule will set sr_err and sr_text */
1104 get_ordering_rule( ad, &matchrule, rs, &ordering );
1105 if ( rs->sr_err != LDAP_SUCCESS ) {
1106 return rs->sr_err;
1107 }
1108
1109 key->sk_ad = ad;
1110 key->sk_ordering = ordering;
1111 key->sk_direction = reverse ? -1 : 1;
1112
1113 return rs->sr_err;
1114 }
1115
1116 /* Conforms to RFC4510 re: Criticality, original RFC2891 spec is broken
1117 * Also see ITS#7253 for discussion
1118 */
1119 static int sss_parseCtrl(
1120 Operation *op,
1121 SlapReply *rs,
1122 LDAPControl *ctrl )
1123 {
1124 BerElementBuffer berbuf;
1125 BerElement *ber;
1126 ber_tag_t tag;
1127 ber_len_t len;
1128 int i;
1129 sort_ctrl *sc;
1130
1131 rs->sr_err = LDAP_PROTOCOL_ERROR;
1132
1133 if ( op->o_ctrlflag[sss_cid] > SLAP_CONTROL_IGNORED ) {
1134 rs->sr_text = "sorted results control specified multiple times";
1135 } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1136 rs->sr_text = "sorted results control value is absent";
1137 } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1138 rs->sr_text = "sorted results control value is empty";
1139 } else {
1140 rs->sr_err = LDAP_SUCCESS;
1141 }
1142 if ( rs->sr_err != LDAP_SUCCESS )
1143 return rs->sr_err;
1144
1145 op->o_ctrlflag[sss_cid] = ctrl->ldctl_iscritical ?
1146 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1147
1148 ber = (BerElement *)&berbuf;
1149 ber_init2( ber, &ctrl->ldctl_value, 0 );
1150 i = count_key( ber );
1151
1152 sc = op->o_tmpalloc( sizeof(sort_ctrl) +
1153 (i-1) * sizeof(sort_key), op->o_tmpmemctx );
1154 sc->sc_nkeys = i;
1155 op->o_controls[sss_cid] = sc;
1156
1157 /* peel off initial sequence */
1158 ber_scanf( ber, "{" );
1159
1160 i = 0;
1161 do {
1162 if ( build_key( ber, rs, &sc->sc_keys[i] ) != LDAP_SUCCESS )
1163 break;
1164 i++;
1165 tag = ber_peek_tag( ber, &len );
1166 } while ( tag != LBER_DEFAULT );
1167
1168 return rs->sr_err;
1169 }
1170
1171 static int vlv_parseCtrl(
1172 Operation *op,
1173 SlapReply *rs,
1174 LDAPControl *ctrl )
1175 {
1176 BerElementBuffer berbuf;
1177 BerElement *ber;
1178 ber_tag_t tag;
1179 ber_len_t len;
1180 vlv_ctrl *vc, vc2;
1181
1182 rs->sr_err = LDAP_PROTOCOL_ERROR;
1183 rs->sr_text = NULL;
1184
1185 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
1186 rs->sr_text = "vlv control specified multiple times";
1187 } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1188 rs->sr_text = "vlv control value is absent";
1189 } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1190 rs->sr_text = "vlv control value is empty";
1191 }
1192 if ( rs->sr_text != NULL )
1193 return rs->sr_err;
1194
1195 op->o_ctrlflag[vlv_cid] = ctrl->ldctl_iscritical ?
1196 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1197
1198 ber = (BerElement *)&berbuf;
1199 ber_init2( ber, &ctrl->ldctl_value, 0 );
1200
1201 rs->sr_err = LDAP_PROTOCOL_ERROR;
1202
1203 tag = ber_scanf( ber, "{ii", &vc2.vc_before, &vc2.vc_after );
1204 if ( tag == LBER_ERROR ) {
1205 return rs->sr_err;
1206 }
1207
1208 tag = ber_peek_tag( ber, &len );
1209 if ( tag == LDAP_VLVBYINDEX_IDENTIFIER ) {
1210 tag = ber_scanf( ber, "{ii}", &vc2.vc_offset, &vc2.vc_count );
1211 if ( tag == LBER_ERROR )
1212 return rs->sr_err;
1213 BER_BVZERO( &vc2.vc_value );
1214 } else if ( tag == LDAP_VLVBYVALUE_IDENTIFIER ) {
1215 tag = ber_scanf( ber, "m", &vc2.vc_value );
1216 if ( tag == LBER_ERROR || BER_BVISNULL( &vc2.vc_value ))
1217 return rs->sr_err;
1218 } else {
1219 return rs->sr_err;
1220 }
1221 tag = ber_peek_tag( ber, &len );
1222 if ( tag == LDAP_VLVCONTEXT_IDENTIFIER ) {
1223 struct berval bv;
1224 tag = ber_scanf( ber, "m", &bv );
1225 if ( tag == LBER_ERROR || bv.bv_len != sizeof(vc2.vc_context))
1226 return rs->sr_err;
1227 AC_MEMCPY( &vc2.vc_context, bv.bv_val, bv.bv_len );
1228 } else {
1229 vc2.vc_context = 0;
1230 }
1231
1232 vc = op->o_tmpalloc( sizeof(vlv_ctrl), op->o_tmpmemctx );
1233 *vc = vc2;
1234 op->o_controls[vlv_cid] = vc;
1235 rs->sr_err = LDAP_SUCCESS;
1236
1237 return rs->sr_err;
1238 }
1239
1240 static int sssvlv_connection_destroy( BackendDB *be, Connection *conn )
1241 {
1242 slap_overinst *on = (slap_overinst *)be->bd_info;
1243 sssvlv_info *si = on->on_bi.bi_private;
1244
1245 if ( sort_conns[conn->c_conn_idx] ) {
1246 free_sort_ops( conn, sort_conns[conn->c_conn_idx], si->svi_max_percon );
1247 }
1248
1249 return LDAP_SUCCESS;
1250 }
1251
1252 static int sssvlv_db_open(
1253 BackendDB *be,
1254 ConfigReply *cr )
1255 {
1256 slap_overinst *on = (slap_overinst *)be->bd_info;
1257 sssvlv_info *si = on->on_bi.bi_private;
1258 int rc;
1259 int conn_index;
1260
1261 /* If not set, default to 1/2 of available threads */
1262 if ( !si->svi_max )
1263 si->svi_max = connection_pool_max / 2;
1264
1265 if ( dtblsize && !sort_conns ) {
1266 ldap_pvt_thread_mutex_init( &sort_conns_mutex );
1267 /* accommodate for c_conn_idx == -1 */
1268 sort_conns = ch_calloc( dtblsize + 1, sizeof(sort_op **) );
1269 for ( conn_index = 0 ; conn_index < dtblsize + 1 ; conn_index++ ) {
1270 sort_conns[conn_index] = ch_calloc( si->svi_max_percon, sizeof(sort_op *) );
1271 }
1272 sort_conns++;
1273 }
1274
1275 rc = overlay_register_control( be, LDAP_CONTROL_SORTREQUEST );
1276 if ( rc == LDAP_SUCCESS )
1277 rc = overlay_register_control( be, LDAP_CONTROL_VLVREQUEST );
1278 return rc;
1279 }
1280
1281 static ConfigTable sssvlv_cfg[] = {
1282 { "sssvlv-max", "num",
1283 2, 2, 0, ARG_INT|ARG_OFFSET,
1284 (void *)offsetof(sssvlv_info, svi_max),
1285 "( OLcfgOvAt:21.1 NAME 'olcSssVlvMax' "
1286 "DESC 'Maximum number of concurrent Sort requests' "
1287 "EQUALITY integerMatch "
1288 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
1289 { "sssvlv-maxkeys", "num",
1290 2, 2, 0, ARG_INT|ARG_OFFSET,
1291 (void *)offsetof(sssvlv_info, svi_max_keys),
1292 "( OLcfgOvAt:21.2 NAME 'olcSssVlvMaxKeys' "
1293 "DESC 'Maximum number of Keys in a Sort request' "
1294 "EQUALITY integerMatch "
1295 "SYNTAX OMsInteger SINGLE-VALUE )", NULL,
1296 { .v_int = SSSVLV_DEFAULT_MAX_KEYS } },
1297 { "sssvlv-maxperconn", "num",
1298 2, 2, 0, ARG_INT|ARG_OFFSET,
1299 (void *)offsetof(sssvlv_info, svi_max_percon),
1300 "( OLcfgOvAt:21.3 NAME 'olcSssVlvMaxPerConn' "
1301 "DESC 'Maximum number of concurrent paged search requests per connection' "
1302 "EQUALITY integerMatch "
1303 "SYNTAX OMsInteger SINGLE-VALUE )", NULL,
1304 { .v_int = SSSVLV_DEFAULT_MAX_REQUEST_PER_CONN } },
1305 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1306 };
1307
1308 static ConfigOCs sssvlv_ocs[] = {
1309 { "( OLcfgOvOc:21.1 "
1310 "NAME 'olcSssVlvConfig' "
1311 "DESC 'SSS VLV configuration' "
1312 "SUP olcOverlayConfig "
1313 "MAY ( olcSssVlvMax $ olcSssVlvMaxKeys $ olcSssVlvMaxPerConn ) )",
1314 Cft_Overlay, sssvlv_cfg, NULL, NULL },
1315 { NULL, 0, NULL }
1316 };
1317
1318 static int sssvlv_db_init(
1319 BackendDB *be,
1320 ConfigReply *cr)
1321 {
1322 slap_overinst *on = (slap_overinst *)be->bd_info;
1323 sssvlv_info *si;
1324
1325 if ( ov_count == 0 ) {
1326 int rc;
1327
1328 rc = register_supported_control2( LDAP_CONTROL_SORTREQUEST,
1329 SLAP_CTRL_SEARCH,
1330 NULL,
1331 sss_parseCtrl,
1332 1 /* replace */,
1333 &sss_cid );
1334 if ( rc != LDAP_SUCCESS ) {
1335 Debug( LDAP_DEBUG_ANY, "Failed to register Sort Request control '%s' (%d)\n",
1336 LDAP_CONTROL_SORTREQUEST, rc );
1337 return rc;
1338 }
1339
1340 rc = register_supported_control2( LDAP_CONTROL_VLVREQUEST,
1341 SLAP_CTRL_SEARCH,
1342 NULL,
1343 vlv_parseCtrl,
1344 1 /* replace */,
1345 &vlv_cid );
1346 if ( rc != LDAP_SUCCESS ) {
1347 Debug( LDAP_DEBUG_ANY, "Failed to register VLV Request control '%s' (%d)\n",
1348 LDAP_CONTROL_VLVREQUEST, rc );
1349 #ifdef SLAP_CONFIG_DELETE
1350 overlay_unregister_control( be, LDAP_CONTROL_SORTREQUEST );
1351 unregister_supported_control( LDAP_CONTROL_SORTREQUEST );
1352 #endif /* SLAP_CONFIG_DELETE */
1353 return rc;
1354 }
1355 }
1356
1357 si = (sssvlv_info *)ch_malloc(sizeof(sssvlv_info));
1358 on->on_bi.bi_private = si;
1359
1360 si->svi_max = 0;
1361 si->svi_num = 0;
1362 si->svi_max_keys = SSSVLV_DEFAULT_MAX_KEYS;
1363 si->svi_max_percon = SSSVLV_DEFAULT_MAX_REQUEST_PER_CONN;
1364
1365 ov_count++;
1366
1367 return LDAP_SUCCESS;
1368 }
1369
1370 static int sssvlv_db_destroy(
1371 BackendDB *be,
1372 ConfigReply *cr )
1373 {
1374 slap_overinst *on = (slap_overinst *)be->bd_info;
1375 sssvlv_info *si = (sssvlv_info *)on->on_bi.bi_private;
1376 int conn_index;
1377
1378 ov_count--;
1379 if ( !ov_count && sort_conns) {
1380 sort_conns--;
1381 for ( conn_index = 0 ; conn_index < dtblsize + 1 ; conn_index++ ) {
1382 ch_free(sort_conns[conn_index]);
1383 }
1384 ch_free(sort_conns);
1385 ldap_pvt_thread_mutex_destroy( &sort_conns_mutex );
1386 }
1387
1388 #ifdef SLAP_CONFIG_DELETE
1389 overlay_unregister_control( be, LDAP_CONTROL_SORTREQUEST );
1390 overlay_unregister_control( be, LDAP_CONTROL_VLVREQUEST );
1391 if ( ov_count == 0 ) {
1392 unregister_supported_control( LDAP_CONTROL_SORTREQUEST );
1393 unregister_supported_control( LDAP_CONTROL_VLVREQUEST );
1394 }
1395 #endif /* SLAP_CONFIG_DELETE */
1396
1397 if ( si ) {
1398 ch_free( si );
1399 on->on_bi.bi_private = NULL;
1400 }
1401 return LDAP_SUCCESS;
1402 }
1403
1404 static slap_overinst sssvlv;
1405
1406 int sssvlv_initialize()
1407 {
1408 int rc;
1409
1410 sssvlv.on_bi.bi_type = "sssvlv";
1411 sssvlv.on_bi.bi_flags = SLAPO_BFLAG_SINGLE;
1412 sssvlv.on_bi.bi_db_init = sssvlv_db_init;
1413 sssvlv.on_bi.bi_db_destroy = sssvlv_db_destroy;
1414 sssvlv.on_bi.bi_db_open = sssvlv_db_open;
1415 sssvlv.on_bi.bi_connection_destroy = sssvlv_connection_destroy;
1416 sssvlv.on_bi.bi_op_search = sssvlv_op_search;
1417
1418 sssvlv.on_bi.bi_cf_ocs = sssvlv_ocs;
1419
1420 rc = config_register_schema( sssvlv_cfg, sssvlv_ocs );
1421 if ( rc )
1422 return rc;
1423
1424 rc = overlay_register( &sssvlv );
1425 if ( rc != LDAP_SUCCESS ) {
1426 Debug( LDAP_DEBUG_ANY, "Failed to register server side sort overlay\n" );
1427 }
1428
1429 return rc;
1430 }
1431
1432 #if SLAPD_OVER_SSSVLV == SLAPD_MOD_DYNAMIC
1433 int init_module( int argc, char *argv[])
1434 {
1435 return sssvlv_initialize();
1436 }
1437 #endif
1438
1439 #endif /* SLAPD_OVER_SSSVLV */