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