]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/sssvlv.c
5c82b91212aef88870978452e6c317747de7e90e
[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;
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         sc = op->o_controls[sss_cid];
729         if ( sc->sc_nkeys > si->svi_max_keys ) {
730                 rs->sr_text = "Too many sort keys";
731                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
732                 goto leave;
733         }
734
735         ps = ( op->o_pagedresults > SLAP_CONTROL_IGNORED ) ?
736                 (PagedResultsState*)(op->o_pagedresults_state) : NULL;
737         vc = op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ?
738                 op->o_controls[vlv_cid] : NULL;
739
740         if ( ps && vc ) {
741                 rs->sr_text = "VLV incompatible with PagedResults";
742                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
743                 goto leave;
744         }
745
746         ok = 1;
747         ldap_pvt_thread_mutex_lock( &sort_conns_mutex );
748         so = sort_conns[op->o_conn->c_conn_idx];
749         /* Is there already a sort running on this conn? */
750         if ( so ) {
751                 /* Is it a continuation of a VLV search? */
752                 if ( !vc || so->so_vlv <= SLAP_CONTROL_IGNORED ||
753                         vc->vc_context != so->so_vcontext ) {
754                         /* Is it a continuation of a paged search? */
755                         if ( !ps || so->so_paged <= SLAP_CONTROL_IGNORED ||
756                                 op->o_conn->c_pagedresults_state.ps_cookie != ps->ps_cookie ) {
757                                 ok = 0;
758                         } else if ( !ps->ps_size ) {
759                         /* Abandoning current request */
760                                 ok = 0;
761                                 so->so_nentries = 0;
762                                 rs->sr_err = LDAP_SUCCESS;
763                         }
764                 }
765                 if (( vc && so->so_paged > SLAP_CONTROL_IGNORED ) ||
766                         ( ps && so->so_vlv > SLAP_CONTROL_IGNORED )) {
767                         /* changed from paged to vlv or vice versa, abandon */
768                         ok = 0;
769                         so->so_nentries = 0;
770                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
771                 }
772         /* Are there too many running overall? */
773         } else if ( si->svi_num >= si->svi_max ) {
774                 ok = 0;
775         } else {
776                 /* OK, this connection now has a sort running */
777                 si->svi_num++;
778                 sort_conns[op->o_conn->c_conn_idx] = &so2;
779         }
780         ldap_pvt_thread_mutex_unlock( &sort_conns_mutex );
781         if ( ok ) {
782                 /* are we continuing a VLV search? */
783                 if ( vc && vc->vc_context ) {
784                         so->so_ctrl = sc;
785                         send_list( op, rs, so );
786                         send_result( op, rs, so );
787                         rc = LDAP_SUCCESS;
788                 /* are we continuing a paged search? */
789                 } else if ( ps && ps->ps_cookie ) {
790                         so->so_ctrl = sc;
791                         send_page( op, rs, so );
792                         send_result( op, rs, so );
793                         rc = LDAP_SUCCESS;
794                 } else {
795                         slap_callback *cb = op->o_tmpalloc( sizeof(slap_callback),
796                                 op->o_tmpmemctx );
797                         /* Install serversort response callback to handle a new search */
798                         if ( ps || vc ) {
799                                 so = ch_malloc( sizeof(sort_op));
800                         } else {
801                                 so = op->o_tmpalloc( sizeof(sort_op), op->o_tmpmemctx );
802                         }
803                         sort_conns[op->o_conn->c_conn_idx] = so;
804
805                         cb->sc_cleanup          = NULL;
806                         cb->sc_response         = sssvlv_op_response;
807                         cb->sc_next                     = op->o_callback;
808                         cb->sc_private          = so;
809
810                         so->so_tree = NULL;
811                         so->so_ctrl = sc;
812                         so->so_info = si;
813                         if ( ps ) {
814                                 so->so_paged = op->o_pagedresults;
815                                 so->so_page_size = ps->ps_size;
816                                 op->o_pagedresults = SLAP_CONTROL_IGNORED;
817                         } else {
818                                 so->so_paged = 0;
819                                 so->so_page_size = 0;
820                                 if ( vc ) {
821                                         so->so_vlv = op->o_ctrlflag[vlv_cid];
822                                         so->so_vlv_target = 0;
823                                         so->so_vlv_rc = 0;
824                                 }
825                         }
826                         so->so_vcontext = (unsigned long)so;
827                         so->so_nentries = 0;
828
829                         op->o_callback          = cb;
830                 }
831         } else {
832                 if ( so && !so->so_nentries ) {
833                         free_sort_op( op->o_conn, so );
834                 } else {
835                         rs->sr_text = "Other sort requests already in progress";
836                         rs->sr_err = LDAP_BUSY;
837                 }
838 leave:
839                 rc = rs->sr_err;
840                 send_ldap_result( op, rs );
841         }
842
843         return rc;
844 }
845
846 static int get_ordering_rule(
847         AttributeDescription    *ad,
848         struct berval                   *matchrule,
849         SlapReply                               *rs,
850         MatchingRule                    **ordering )
851 {
852         MatchingRule* mr;
853
854         if ( matchrule && matchrule->bv_val ) {
855                 mr = mr_find( matchrule->bv_val );
856                 if ( mr == NULL ) {
857                         rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
858                         rs->sr_text = "serverSort control: No ordering rule";
859                         Debug(LDAP_DEBUG_TRACE, "%s: no ordering rule function for %s\n",
860                                 debug_header, matchrule->bv_val, 0);
861                 }
862         }
863         else {
864                 mr = ad->ad_type->sat_ordering;
865                 if ( mr == NULL ) {
866                         rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
867                         rs->sr_text = "serverSort control: No ordering rule";
868                         Debug(LDAP_DEBUG_TRACE,
869                                 "%s: no ordering rule specified and no default ordering rule for attribute %s\n",
870                                 debug_header, ad->ad_cname.bv_val, 0);
871                 }
872         }
873
874         *ordering = mr;
875         return rs->sr_err;
876 }
877
878 static int count_key(BerElement *ber)
879 {
880         char *end;
881         ber_len_t len;
882         ber_tag_t tag;
883         int count = 0;
884
885         /* Server Side Sort Control is a SEQUENCE of SEQUENCE */
886         for ( tag = ber_first_element( ber, &len, &end );
887                   tag == LBER_SEQUENCE;
888                   tag = ber_next_element( ber, &len, end ))
889         {
890                 tag = ber_skip_tag( ber, &len );
891                 ber_skip_data( ber, len );
892                 ++count;
893         }
894         ber_rewind( ber );
895
896         return count;
897 }
898
899 static int build_key(
900         BerElement              *ber,
901         SlapReply               *rs,
902         sort_key                        *key )
903 {
904         struct berval attr;
905         struct berval matchrule = BER_BVNULL;
906         ber_int_t reverse = 0;
907         ber_tag_t tag;
908         ber_len_t len;
909         MatchingRule *ordering = NULL;
910         AttributeDescription *ad = NULL;
911         const char *text;
912
913         if (( tag = ber_scanf( ber, "{" )) == LBER_ERROR ) {
914                 rs->sr_text = "serverSort control: decoding error";
915                 rs->sr_err = LDAP_PROTOCOL_ERROR;
916                 return rs->sr_err;
917         }
918
919         if (( tag = ber_scanf( ber, "m", &attr )) == LBER_ERROR ) {
920                 rs->sr_text = "serverSort control: attribute decoding error";
921                 rs->sr_err = LDAP_PROTOCOL_ERROR;
922                 return rs->sr_err;
923         }
924
925         tag = ber_peek_tag( ber, &len );
926         if ( tag == LDAP_MATCHRULE_IDENTIFIER ) {
927                 if (( tag = ber_scanf( ber, "m", &matchrule )) == LBER_ERROR ) {
928                         rs->sr_text = "serverSort control: matchrule decoding error";
929                         rs->sr_err = LDAP_PROTOCOL_ERROR;
930                         return rs->sr_err;
931                 }
932                 tag = ber_peek_tag( ber, &len );
933         }
934
935         if ( tag == LDAP_REVERSEORDER_IDENTIFIER ) {
936                 if (( tag = ber_scanf( ber, "b", &reverse )) == LBER_ERROR ) {
937                         rs->sr_text = "serverSort control: reverse decoding error";
938                         rs->sr_err = LDAP_PROTOCOL_ERROR;
939                         return rs->sr_err;
940                 }
941         }
942
943         if (( tag = ber_scanf( ber, "}" )) == LBER_ERROR ) {
944                 rs->sr_text = "serverSort control: decoding error";
945                 rs->sr_err = LDAP_PROTOCOL_ERROR;
946                 return rs->sr_err;
947         }
948
949         if ( slap_bv2ad( &attr, &ad, &text ) != LDAP_SUCCESS ) {
950                 rs->sr_text =
951                         "serverSort control: Unrecognized attribute type in sort key";
952                 Debug(LDAP_DEBUG_TRACE,
953                         "%s: Unrecognized attribute type in sort key: %s\n",
954                         debug_header, SAFESTR(attr.bv_val, "<None>"), 0);
955                 rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
956                 return rs->sr_err;
957         }
958
959         /* get_ordering_rule will set sr_err and sr_text */
960         get_ordering_rule( ad, &matchrule, rs, &ordering );
961         if ( rs->sr_err != LDAP_SUCCESS ) {
962                 return rs->sr_err;
963         }
964
965         key->sk_ad = ad;
966         key->sk_ordering = ordering;
967         key->sk_direction = reverse ? -1 : 1;
968
969         return rs->sr_err;
970 }
971
972 static int sss_parseCtrl(
973         Operation               *op,
974         SlapReply               *rs,
975         LDAPControl             *ctrl )
976 {
977         BerElementBuffer        berbuf;
978         BerElement                      *ber;
979         ber_tag_t               tag;
980         ber_len_t               len;
981         int                                     i;
982         sort_ctrl       *sc;
983
984         rs->sr_err = LDAP_PROTOCOL_ERROR;
985
986         if ( op->o_ctrlflag[sss_cid] > SLAP_CONTROL_IGNORED ) {
987                 rs->sr_text = "sorted results control specified multiple times";
988         } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
989                 rs->sr_text = "sorted results control value is absent";
990         } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
991                 rs->sr_text = "sorted results control value is empty";
992         } else {
993                 rs->sr_err = LDAP_SUCCESS;
994         }
995         if ( rs->sr_err != LDAP_SUCCESS )
996                 return rs->sr_err;
997
998         op->o_ctrlflag[sss_cid] = ctrl->ldctl_iscritical ?
999                 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1000
1001         ber = (BerElement *)&berbuf;
1002         ber_init2( ber, &ctrl->ldctl_value, 0 );
1003         i = count_key( ber );
1004
1005         sc = op->o_tmpalloc( sizeof(sort_ctrl) +
1006                 (i-1) * sizeof(sort_key), op->o_tmpmemctx );
1007         sc->sc_nkeys = i;
1008         op->o_controls[sss_cid] = sc;
1009
1010         /* peel off initial sequence */
1011         ber_scanf( ber, "{" );
1012
1013         i = 0;
1014         do {
1015                 if ( build_key( ber, rs, &sc->sc_keys[i] ) != LDAP_SUCCESS )
1016                         break;
1017                 i++;
1018                 tag = ber_peek_tag( ber, &len );
1019         } while ( tag != LBER_DEFAULT );
1020
1021         return rs->sr_err;
1022 }
1023
1024 static int vlv_parseCtrl(
1025         Operation               *op,
1026         SlapReply               *rs,
1027         LDAPControl             *ctrl )
1028 {
1029         BerElementBuffer        berbuf;
1030         BerElement                      *ber;
1031         ber_tag_t               tag;
1032         ber_len_t               len;
1033         int                                     i;
1034         vlv_ctrl        *vc, vc2;
1035
1036         rs->sr_err = LDAP_PROTOCOL_ERROR;
1037         rs->sr_text = NULL;
1038
1039         if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
1040                 rs->sr_text = "vlv control specified multiple times";
1041         } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1042                 rs->sr_text = "vlv control value is absent";
1043         } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1044                 rs->sr_text = "vlv control value is empty";
1045         }
1046         if ( rs->sr_text != NULL )
1047                 return rs->sr_err;
1048
1049         op->o_ctrlflag[vlv_cid] = ctrl->ldctl_iscritical ?
1050                 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1051
1052         ber = (BerElement *)&berbuf;
1053         ber_init2( ber, &ctrl->ldctl_value, 0 );
1054
1055         rs->sr_err = LDAP_PROTOCOL_ERROR;
1056
1057         tag = ber_scanf( ber, "{ii", &vc2.vc_before, &vc2.vc_after );
1058         if ( tag == LBER_ERROR ) {
1059                 return rs->sr_err;
1060         }
1061
1062         tag = ber_peek_tag( ber, &len );
1063         if ( tag == LDAP_VLVBYINDEX_IDENTIFIER ) {
1064                 tag = ber_scanf( ber, "{ii}", &vc2.vc_offset, &vc2.vc_count );
1065                 if ( tag == LBER_ERROR )
1066                         return rs->sr_err;
1067                 BER_BVZERO( &vc2.vc_value );
1068         } else if ( tag == LDAP_VLVBYVALUE_IDENTIFIER ) {
1069                 tag = ber_scanf( ber, "m", &vc2.vc_value );
1070                 if ( tag == LBER_ERROR || BER_BVISNULL( &vc2.vc_value ))
1071                         return rs->sr_err;
1072         } else {
1073                 return rs->sr_err;
1074         }
1075         tag = ber_peek_tag( ber, &len );
1076         if ( tag == LDAP_VLVCONTEXT_IDENTIFIER ) {
1077                 struct berval bv;
1078                 tag = ber_scanf( ber, "m", &bv );
1079                 if ( tag == LBER_ERROR || bv.bv_len != sizeof(vc2.vc_context))
1080                         return rs->sr_err;
1081                 AC_MEMCPY( &vc2.vc_context, bv.bv_val, bv.bv_len );
1082         } else {
1083                 vc2.vc_context = 0;
1084         }
1085
1086         vc = op->o_tmpalloc( sizeof(vlv_ctrl), op->o_tmpmemctx );
1087         *vc = vc2;
1088         op->o_controls[vlv_cid] = vc;
1089         rs->sr_err = LDAP_SUCCESS;
1090
1091         return rs->sr_err;
1092 }
1093
1094 static int sssvlv_connection_destroy( BackendDB *be, Connection *conn )
1095 {
1096         slap_overinst   *on             = (slap_overinst *)be->bd_info;
1097
1098         if ( sort_conns[conn->c_conn_idx] )
1099                 free_sort_op( conn, sort_conns[conn->c_conn_idx] );
1100
1101         return LDAP_SUCCESS;
1102 }
1103
1104 static int sssvlv_db_open(
1105         BackendDB               *be,
1106         ConfigReply             *cr )
1107 {
1108         slap_overinst   *on = (slap_overinst *)be->bd_info;
1109         sssvlv_info *si = on->on_bi.bi_private;
1110         int rc;
1111
1112         /* If not set, default to 1/2 of available threads */
1113         if ( !si->svi_max )
1114                 si->svi_max = connection_pool_max / 2;
1115
1116         rc = overlay_register_control( be, LDAP_CONTROL_SORTREQUEST );
1117         if ( rc == LDAP_SUCCESS )
1118                 rc = overlay_register_control( be, LDAP_CONTROL_VLVREQUEST );
1119         return rc;
1120 }
1121
1122 static ConfigTable sssvlv_cfg[] = {
1123         { "sssvlv-max", "num",
1124                 2, 2, 0, ARG_INT|ARG_OFFSET,
1125                         (void *)offsetof(sssvlv_info, svi_max),
1126                 "( OLcfgOvAt:21.1 NAME 'olcSssVlvMax' "
1127                         "DESC 'Maximum number of concurrent Sort requests' "
1128                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
1129         { "sssvlv-maxkeys", "num",
1130                 2, 2, 0, ARG_INT|ARG_OFFSET,
1131                         (void *)offsetof(sssvlv_info, svi_max_keys),
1132                 "( OLcfgOvAt:21.2 NAME 'olcSssVlvMaxKeys' "
1133                         "DESC 'Maximum number of Keys in a Sort request' "
1134                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
1135         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1136 };
1137
1138 static ConfigOCs sssvlv_ocs[] = {
1139         { "( OLcfgOvOc:21.1 "
1140                 "NAME 'olcSssVlvConfig' "
1141                 "DESC 'SSS VLV configuration' "
1142                 "SUP olcOverlayConfig "
1143                 "MAY ( olcSssVlvMax $ olcSssVlvMaxKeys ) )",
1144                 Cft_Overlay, sssvlv_cfg, NULL, NULL },
1145         { NULL, 0, NULL }
1146 };
1147
1148 static int sssvlv_db_init(
1149         BackendDB               *be,
1150         ConfigReply             *cr)
1151 {
1152         slap_overinst   *on = (slap_overinst *)be->bd_info;
1153         sssvlv_info *si;
1154         
1155         si = (sssvlv_info *)ch_malloc(sizeof(sssvlv_info));
1156         on->on_bi.bi_private = si;
1157
1158         si->svi_max = 0;
1159         si->svi_num = 0;
1160         si->svi_max_keys = SSSVLV_DEFAULT_MAX_KEYS;
1161
1162         if ( dtblsize && !sort_conns ) {
1163                 ldap_pvt_thread_mutex_init( &sort_conns_mutex );
1164                 /* accommodate for c_conn_idx == -1 */
1165                 sort_conns = ch_calloc( sizeof(sort_op *), dtblsize + 1 );
1166                 sort_conns++;
1167         }
1168
1169         return LDAP_SUCCESS;
1170 }
1171
1172 static int sssvlv_db_destroy(
1173         BackendDB               *be,
1174         ConfigReply             *cr )
1175 {
1176         slap_overinst   *on = (slap_overinst *)be->bd_info;
1177         sssvlv_info *si = (sssvlv_info *)on->on_bi.bi_private;
1178         
1179         if ( si ) {
1180                 ch_free( si );
1181                 on->on_bi.bi_private = NULL;
1182         }
1183         return LDAP_SUCCESS;
1184 }
1185
1186 static slap_overinst sssvlv;
1187
1188 int sssvlv_initialize()
1189 {
1190         int rc;
1191
1192         sssvlv.on_bi.bi_type                            = "sssvlv";
1193         sssvlv.on_bi.bi_db_init                         = sssvlv_db_init;
1194         sssvlv.on_bi.bi_db_destroy                      = sssvlv_db_destroy;
1195         sssvlv.on_bi.bi_db_open                         = sssvlv_db_open;
1196         sssvlv.on_bi.bi_connection_destroy      = sssvlv_connection_destroy;
1197         sssvlv.on_bi.bi_op_search                       = sssvlv_op_search;
1198
1199         sssvlv.on_bi.bi_cf_ocs = sssvlv_ocs;
1200
1201         rc = config_register_schema( sssvlv_cfg, sssvlv_ocs );
1202         if ( rc )
1203                 return rc;
1204
1205         rc = register_supported_control2( LDAP_CONTROL_SORTREQUEST,
1206                         SLAP_CTRL_SEARCH,
1207                         NULL,
1208                         sss_parseCtrl,
1209                         1 /* replace */,
1210                         &sss_cid );
1211
1212         if ( rc == LDAP_SUCCESS ) {
1213                 rc = register_supported_control2( LDAP_CONTROL_VLVREQUEST,
1214                         SLAP_CTRL_SEARCH,
1215                         NULL,
1216                         vlv_parseCtrl,
1217                         1 /* replace */,
1218                         &vlv_cid );
1219         }
1220
1221         if ( rc == LDAP_SUCCESS ) {
1222                 rc = overlay_register( &sssvlv );
1223                 if ( rc != LDAP_SUCCESS ) {
1224                         fprintf( stderr, "Failed to register server side sort overlay\n" );
1225                 }
1226         }
1227         else {
1228                 fprintf( stderr, "Failed to register control %d\n", rc );
1229         }
1230
1231         return rc;
1232 }
1233
1234 #if SLAPD_OVER_SSSVLV == SLAPD_MOD_DYNAMIC
1235 int init_module( int argc, char *argv[])
1236 {
1237         return sssvlv_initialize();
1238 }
1239 #endif
1240
1241 #endif /* SLAPD_OVER_SSSVLV */