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