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