]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/sssvlv.c
707f6820ea0d122592e0a654af5198e8016b4ea9
[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         /* RFC 2891: If critical then send the entries iff they were
540          * succesfully sorted.  If non-critical send all entries
541          * whether they were sorted or not.
542          */
543         if ( (op->o_ctrlflag[sss_cid] != SLAP_CONTROL_CRITICAL) ||
544                  (rs->sr_err == LDAP_SUCCESS) )
545         {
546                 if ( so->so_vlv > SLAP_CONTROL_IGNORED ) {
547                         send_list( op, rs, so );
548                 } else {
549                         /* Get the first node to send */
550                         Avlnode *start_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
551                         so->so_tree = start_node;
552
553                         if ( so->so_paged <= SLAP_CONTROL_IGNORED ) {
554                                 /* Not paged result search.  Send all entries.
555                                  * Set the page size to the number of entries
556                                  * so that send_page() will send all entries.
557                                  */
558                                 so->so_page_size = so->so_nentries;
559                         }
560
561                         send_page( op, rs, so );
562                 }
563         }
564 }
565
566 static void send_result(
567         Operation               *op,
568         SlapReply               *rs,
569         sort_op                 *so)
570 {
571         LDAPControl *ctrls[3];
572         int rc, i = 0;
573
574         rc = pack_sss_response_control( op, rs, ctrls );
575         if ( rc == LDAP_SUCCESS ) {
576                 i++;
577                 rc = -1;
578                 if ( so->so_paged > SLAP_CONTROL_IGNORED ) {
579                         rc = pack_pagedresult_response_control( op, rs, so, ctrls+1 );
580                 } else if ( so->so_vlv > SLAP_CONTROL_IGNORED ) {
581                         rc = pack_vlv_response_control( op, rs, so, ctrls+1 );
582                 }
583                 if ( rc == LDAP_SUCCESS )
584                         i++;
585         }
586         ctrls[i] = NULL;
587
588         if ( ctrls[0] != NULL )
589                 slap_add_ctrls( op, rs, ctrls );
590         send_ldap_result( op, rs );
591
592         if ( so->so_tree == NULL ) {
593                 /* Search finished, so clean up */
594                 free_sort_op( op->o_conn, so );
595         }
596 }
597
598 static int sssvlv_op_response(
599         Operation       *op,
600         SlapReply       *rs )
601 {
602         sort_ctrl *sc = op->o_controls[sss_cid];
603         sort_op *so = op->o_callback->sc_private;
604
605         if ( rs->sr_type == REP_SEARCH ) {
606                 int i;
607                 size_t len;
608                 sort_node *sn, *sn2;
609                 struct berval *bv;
610                 char *ptr;
611
612                 len = sizeof(sort_node) + sc->sc_nkeys * sizeof(struct berval) +
613                         rs->sr_entry->e_nname.bv_len + 1;
614                 sn = op->o_tmpalloc( len, op->o_tmpmemctx );
615                 sn->sn_vals = (struct berval *)(sn+1);
616
617                 /* Build tmp list of key values */
618                 for ( i=0; i<sc->sc_nkeys; i++ ) {
619                         Attribute *a = attr_find( rs->sr_entry->e_attrs,
620                                 sc->sc_keys[i].sk_ad );
621                         if ( a ) {
622                                 if ( a->a_numvals > 1 ) {
623                                         bv = select_value( a, &sc->sc_keys[i] );
624                                 } else {
625                                         bv = a->a_nvals;
626                                 }
627                                 sn->sn_vals[i] = *bv;
628                                 len += bv->bv_len + 1;
629                         } else {
630                                 BER_BVZERO( &sn->sn_vals[i] );
631                         }
632                 }
633
634                 /* Now dup into regular memory */
635                 sn2 = ch_malloc( len );
636                 sn2->sn_vals = (struct berval *)(sn2+1);
637                 AC_MEMCPY( sn2->sn_vals, sn->sn_vals,
638                                 sc->sc_nkeys * sizeof(struct berval));
639
640                 ptr = (char *)(sn2->sn_vals + sc->sc_nkeys);
641                 sn2->sn_dn.bv_val = ptr;
642                 sn2->sn_dn.bv_len = rs->sr_entry->e_nname.bv_len;
643                 AC_MEMCPY( ptr, rs->sr_entry->e_nname.bv_val,
644                         rs->sr_entry->e_nname.bv_len );
645                 ptr += rs->sr_entry->e_nname.bv_len;
646                 *ptr++ = '\0';
647                 for ( i=0; i<sc->sc_nkeys; i++ ) {
648                         if ( !BER_BVISNULL( &sn2->sn_vals[i] )) {
649                                 AC_MEMCPY(ptr, sn2->sn_vals[i].bv_val, sn2->sn_vals[i].bv_len);
650                                 sn2->sn_vals[i].bv_val = ptr;
651                                 ptr += sn2->sn_vals[i].bv_len;
652                                 *ptr++ = '\0';
653                         }
654                 }
655                 op->o_tmpfree( sn, op->o_tmpmemctx );
656                 sn = sn2;
657                 sn->sn_conn = op->o_conn->c_conn_idx;
658
659                 /* Insert into the AVL tree */
660                 tavl_insert(&(so->so_tree), sn, node_insert, avl_dup_error);
661
662                 so->so_nentries++;
663
664                 /* Collected the keys so that they can be sorted.  Thus, stop
665                  * the entry from propagating.
666                  */
667                 rs->sr_err = LDAP_SUCCESS;
668         }
669         else if ( rs->sr_type == REP_RESULT ) {
670                 /* Remove serversort response callback.
671                  * We don't want the entries that we are about to send to be
672                  * processed by serversort response again.
673                  */
674                 if ( op->o_callback->sc_response == sssvlv_op_response ) {
675                         op->o_callback = op->o_callback->sc_next;
676                 }
677
678                 send_entry( op, rs, so );
679                 send_result( op, rs, so );
680         }
681
682         return rs->sr_err;
683 }
684
685 static int sssvlv_op_search(
686         Operation               *op,
687         SlapReply               *rs)
688 {
689         slap_overinst                   *on                     = (slap_overinst *)op->o_bd->bd_info;
690         sssvlv_info                             *si                     = on->on_bi.bi_private;
691         int                                             rc                      = SLAP_CB_CONTINUE;
692         int     ok;
693         sort_op *so, so2;
694         sort_ctrl *sc = op->o_controls[sss_cid];
695         PagedResultsState *ps;
696         vlv_ctrl *vc;
697
698         if ( op->o_ctrlflag[sss_cid] <= SLAP_CONTROL_IGNORED ) {
699                 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
700                         LDAPControl *ctrls[2];
701                         so2.so_vcontext = 0;
702                         so2.so_vlv_target = 0;
703                         so2.so_nentries = 0;
704                         so2.so_vlv_rc = LDAP_VLV_SSS_MISSING;
705                         rc = pack_vlv_response_control( op, rs, &so2, ctrls );
706                         if ( rc == LDAP_SUCCESS ) {
707                                 ctrls[1] = NULL;
708                                 slap_add_ctrls( op, rs, ctrls );
709                         }
710                         rs->sr_err = LDAP_VLV_ERROR;
711                         rs->sr_text = "Sort control is required with VLV";
712                         goto leave;
713                 }
714                 /* Not server side sort so just continue */
715                 return SLAP_CB_CONTINUE;
716         }
717
718         Debug(LDAP_DEBUG_TRACE,
719                 "==> sssvlv_search: <%s> %s, control flag: %d\n",
720                 op->o_req_dn.bv_val, op->ors_filterstr.bv_val,
721                 op->o_ctrlflag[sss_cid]);
722
723         if ( sc->sc_nkeys > si->svi_max_keys ) {
724                 rs->sr_text = "Too many sort keys";
725                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
726                 goto leave;
727         }
728
729         ps = ( op->o_pagedresults > SLAP_CONTROL_IGNORED ) ?
730                 (PagedResultsState*)(op->o_pagedresults_state) : NULL;
731         vc = op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ?
732                 op->o_controls[vlv_cid] : NULL;
733
734         if ( ps && vc ) {
735                 rs->sr_text = "VLV incompatible with PagedResults";
736                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
737                 goto leave;
738         }
739
740         ok = 1;
741         ldap_pvt_thread_mutex_lock( &sort_conns_mutex );
742         so = sort_conns[op->o_conn->c_conn_idx];
743         /* Is there already a sort running on this conn? */
744         if ( so ) {
745                 /* Is it a continuation of a VLV search? */
746                 if ( !vc || so->so_vlv <= SLAP_CONTROL_IGNORED ||
747                         vc->vc_context != so->so_vcontext ) {
748                         /* Is it a continuation of a paged search? */
749                         if ( !ps || so->so_paged <= SLAP_CONTROL_IGNORED ||
750                                 op->o_conn->c_pagedresults_state.ps_cookie != ps->ps_cookie ) {
751                                 ok = 0;
752                         } else if ( !ps->ps_size ) {
753                         /* Abandoning current request */
754                                 ok = 0;
755                                 so->so_nentries = 0;
756                                 rs->sr_err = LDAP_SUCCESS;
757                         }
758                 }
759                 if (( vc && so->so_paged > SLAP_CONTROL_IGNORED ) ||
760                         ( ps && so->so_vlv > SLAP_CONTROL_IGNORED )) {
761                         /* changed from paged to vlv or vice versa, abandon */
762                         ok = 0;
763                         so->so_nentries = 0;
764                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
765                 }
766         /* Are there too many running overall? */
767         } else if ( si->svi_num >= si->svi_max ) {
768                 ok = 0;
769         } else {
770                 /* OK, this connection now has a sort running */
771                 si->svi_num++;
772                 sort_conns[op->o_conn->c_conn_idx] = &so2;
773         }
774         ldap_pvt_thread_mutex_unlock( &sort_conns_mutex );
775         if ( ok ) {
776                 /* are we continuing a VLV search? */
777                 if ( vc && vc->vc_context ) {
778                         so->so_ctrl = sc;
779                         send_list( op, rs, so );
780                         send_result( op, rs, so );
781                         rc = LDAP_SUCCESS;
782                 /* are we continuing a paged search? */
783                 } else if ( ps && ps->ps_cookie ) {
784                         so->so_ctrl = sc;
785                         send_page( op, rs, so );
786                         send_result( op, rs, so );
787                         rc = LDAP_SUCCESS;
788                 } else {
789                         slap_callback *cb = op->o_tmpalloc( sizeof(slap_callback),
790                                 op->o_tmpmemctx );
791                         /* Install serversort response callback to handle a new search */
792                         if ( ps || vc ) {
793                                 so = ch_malloc( sizeof(sort_op));
794                         } else {
795                                 so = op->o_tmpalloc( sizeof(sort_op), op->o_tmpmemctx );
796                         }
797                         sort_conns[op->o_conn->c_conn_idx] = so;
798
799                         cb->sc_cleanup          = NULL;
800                         cb->sc_response         = sssvlv_op_response;
801                         cb->sc_next                     = op->o_callback;
802                         cb->sc_private          = so;
803
804                         so->so_tree = NULL;
805                         so->so_ctrl = sc;
806                         so->so_info = si;
807                         if ( ps ) {
808                                 so->so_paged = op->o_pagedresults;
809                                 so->so_page_size = ps->ps_size;
810                                 op->o_pagedresults = SLAP_CONTROL_IGNORED;
811                         } else {
812                                 so->so_paged = 0;
813                                 so->so_page_size = 0;
814                                 if ( vc )
815                                         so->so_vlv = op->o_ctrlflag[vlv_cid];
816                         }
817                         so->so_vcontext = (unsigned long)so;
818                         so->so_nentries = 0;
819
820                         op->o_callback          = cb;
821                 }
822         } else {
823                 if ( so && !so->so_nentries ) {
824                         free_sort_op( op->o_conn, so );
825                 } else {
826                         rs->sr_text = "Other sort requests already in progress";
827                         rs->sr_err = LDAP_BUSY;
828                 }
829 leave:
830                 rc = rs->sr_err;
831                 send_ldap_result( op, rs );
832         }
833
834         return rc;
835 }
836
837 static int get_ordering_rule(
838         AttributeDescription    *ad,
839         struct berval                   *matchrule,
840         SlapReply                               *rs,
841         MatchingRule                    **ordering )
842 {
843         MatchingRule* mr;
844
845         if ( matchrule && matchrule->bv_val ) {
846                 mr = mr_find( matchrule->bv_val );
847                 if ( mr == NULL ) {
848                         rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
849                         rs->sr_text = "serverSort control: No ordering rule";
850                         Debug(LDAP_DEBUG_TRACE, "%s: no ordering rule function for %s\n",
851                                 debug_header, matchrule->bv_val, 0);
852                 }
853         }
854         else {
855                 mr = ad->ad_type->sat_ordering;
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,
860                                 "%s: no ordering rule specified and no default ordering rule for attribute %s\n",
861                                 debug_header, ad->ad_cname.bv_val, 0);
862                 }
863         }
864
865         *ordering = mr;
866         return rs->sr_err;
867 }
868
869 static int count_key(BerElement *ber)
870 {
871         char *end;
872         ber_len_t len;
873         ber_tag_t tag;
874         int count = 0;
875
876         /* Server Side Sort Control is a SEQUENCE of SEQUENCE */
877         for ( tag = ber_first_element( ber, &len, &end );
878                   tag == LBER_SEQUENCE;
879                   tag = ber_next_element( ber, &len, end ))
880         {
881                 tag = ber_skip_tag( ber, &len );
882                 ber_skip_data( ber, len );
883                 ++count;
884         }
885         ber_rewind( ber );
886
887         return count;
888 }
889
890 static int build_key(
891         BerElement              *ber,
892         SlapReply               *rs,
893         sort_key                        *key )
894 {
895         struct berval attr;
896         struct berval matchrule = BER_BVNULL;
897         ber_int_t reverse = 0;
898         ber_tag_t tag;
899         ber_len_t len;
900         MatchingRule *ordering = NULL;
901         AttributeDescription *ad = NULL;
902         const char *text;
903
904         if (( tag = ber_scanf( ber, "{" )) == LBER_ERROR ) {
905                 rs->sr_text = "serverSort control: decoding error";
906                 rs->sr_err = LDAP_PROTOCOL_ERROR;
907                 return rs->sr_err;
908         }
909
910         if (( tag = ber_scanf( ber, "m", &attr )) == LBER_ERROR ) {
911                 rs->sr_text = "serverSort control: attribute decoding error";
912                 rs->sr_err = LDAP_PROTOCOL_ERROR;
913                 return rs->sr_err;
914         }
915
916         tag = ber_peek_tag( ber, &len );
917         if ( tag == LDAP_MATCHRULE_IDENTIFIER ) {
918                 if (( tag = ber_scanf( ber, "m", &matchrule )) == LBER_ERROR ) {
919                         rs->sr_text = "serverSort control: matchrule decoding error";
920                         rs->sr_err = LDAP_PROTOCOL_ERROR;
921                         return rs->sr_err;
922                 }
923                 tag = ber_peek_tag( ber, &len );
924         }
925
926         if ( tag == LDAP_REVERSEORDER_IDENTIFIER ) {
927                 if (( tag = ber_scanf( ber, "b", &reverse )) == LBER_ERROR ) {
928                         rs->sr_text = "serverSort control: reverse decoding error";
929                         rs->sr_err = LDAP_PROTOCOL_ERROR;
930                         return rs->sr_err;
931                 }
932         }
933
934         if (( tag = ber_scanf( ber, "}" )) == LBER_ERROR ) {
935                 rs->sr_text = "serverSort control: decoding error";
936                 rs->sr_err = LDAP_PROTOCOL_ERROR;
937                 return rs->sr_err;
938         }
939
940         if ( slap_bv2ad( &attr, &ad, &text ) != LDAP_SUCCESS ) {
941                 rs->sr_text =
942                         "serverSort control: Unrecognized attribute type in sort key";
943                 Debug(LDAP_DEBUG_TRACE,
944                         "%s: Unrecognized attribute type in sort key: %s\n",
945                         debug_header, SAFESTR(attr.bv_val, "<None>"), 0);
946                 rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
947                 return rs->sr_err;
948         }
949
950         /* get_ordering_rule will set sr_err and sr_text */
951         get_ordering_rule( ad, &matchrule, rs, &ordering );
952         if ( rs->sr_err != LDAP_SUCCESS ) {
953                 return rs->sr_err;
954         }
955
956         key->sk_ad = ad;
957         key->sk_ordering = ordering;
958         key->sk_direction = reverse ? -1 : 1;
959
960         return rs->sr_err;
961 }
962
963 static int sss_parseCtrl(
964         Operation               *op,
965         SlapReply               *rs,
966         LDAPControl             *ctrl )
967 {
968         BerElementBuffer        berbuf;
969         BerElement                      *ber;
970         ber_tag_t               tag;
971         ber_len_t               len;
972         int                                     i;
973         sort_ctrl       *sc;
974
975         rs->sr_err = LDAP_PROTOCOL_ERROR;
976
977         if ( op->o_ctrlflag[sss_cid] > SLAP_CONTROL_IGNORED ) {
978                 rs->sr_text = "sorted results control specified multiple times";
979         } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
980                 rs->sr_text = "sorted results control value is absent";
981         } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
982                 rs->sr_text = "sorted results control value is empty";
983         } else {
984                 rs->sr_err = LDAP_SUCCESS;
985         }
986         if ( rs->sr_err != LDAP_SUCCESS )
987                 return rs->sr_err;
988
989         op->o_ctrlflag[sss_cid] = ctrl->ldctl_iscritical ?
990                 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
991
992         ber = (BerElement *)&berbuf;
993         ber_init2( ber, &ctrl->ldctl_value, 0 );
994         i = count_key( ber );
995
996         sc = op->o_tmpalloc( sizeof(sort_ctrl) +
997                 i * sizeof(sort_key), op->o_tmpmemctx );
998         sc->sc_nkeys = i;
999         op->o_controls[sss_cid] = sc;
1000
1001         /* peel off initial sequence */
1002         ber_scanf( ber, "{" );
1003
1004         i = 0;
1005         do {
1006                 if ( build_key( ber, rs, &sc->sc_keys[i] ) != LDAP_SUCCESS )
1007                         break;
1008                 i++;
1009                 tag = ber_peek_tag( ber, &len );
1010         } while ( tag != LBER_DEFAULT );
1011
1012         return rs->sr_err;
1013 }
1014
1015 static int vlv_parseCtrl(
1016         Operation               *op,
1017         SlapReply               *rs,
1018         LDAPControl             *ctrl )
1019 {
1020         BerElementBuffer        berbuf;
1021         BerElement                      *ber;
1022         ber_tag_t               tag;
1023         ber_len_t               len;
1024         int                                     i;
1025         vlv_ctrl        *vc, vc2;
1026
1027         rs->sr_err = LDAP_PROTOCOL_ERROR;
1028         rs->sr_text = NULL;
1029
1030         if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
1031                 rs->sr_text = "vlv control specified multiple times";
1032         } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1033                 rs->sr_text = "vlv control value is absent";
1034         } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1035                 rs->sr_text = "vlv control value is empty";
1036         }
1037         if ( rs->sr_text != NULL )
1038                 return rs->sr_err;
1039
1040         op->o_ctrlflag[vlv_cid] = ctrl->ldctl_iscritical ?
1041                 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1042
1043         ber = (BerElement *)&berbuf;
1044         ber_init2( ber, &ctrl->ldctl_value, 0 );
1045
1046         rs->sr_err = LDAP_PROTOCOL_ERROR;
1047
1048         tag = ber_scanf( ber, "{ii", &vc2.vc_before, &vc2.vc_after );
1049         if ( tag == LBER_ERROR ) {
1050                 return rs->sr_err;
1051         }
1052
1053         tag = ber_peek_tag( ber, &len );
1054         if ( tag == LDAP_VLVBYINDEX_IDENTIFIER ) {
1055                 tag = ber_scanf( ber, "{ii}", &vc2.vc_offset, &vc2.vc_count );
1056                 if ( tag == LBER_ERROR )
1057                         return rs->sr_err;
1058                 BER_BVZERO( &vc2.vc_value );
1059         } else if ( tag == LDAP_VLVBYVALUE_IDENTIFIER ) {
1060                 tag = ber_scanf( ber, "m", &vc2.vc_value );
1061                 if ( tag == LBER_ERROR || BER_BVISNULL( &vc2.vc_value ))
1062                         return rs->sr_err;
1063         } else {
1064                 return rs->sr_err;
1065         }
1066         tag = ber_peek_tag( ber, &len );
1067         if ( tag == LDAP_VLVCONTEXT_IDENTIFIER ) {
1068                 struct berval bv;
1069                 tag = ber_scanf( ber, "m", &bv );
1070                 if ( tag == LBER_ERROR || bv.bv_len != sizeof(vc2.vc_context))
1071                         return rs->sr_err;
1072                 AC_MEMCPY( &vc2.vc_context, bv.bv_val, bv.bv_len );
1073         } else {
1074                 vc2.vc_context = 0;
1075         }
1076
1077         vc = op->o_tmpalloc( sizeof(vlv_ctrl), op->o_tmpmemctx );
1078         *vc = vc2;
1079         op->o_controls[vlv_cid] = vc;
1080         rs->sr_err = LDAP_SUCCESS;
1081
1082         return rs->sr_err;
1083 }
1084
1085 static int sssvlv_connection_destroy( BackendDB *be, Connection *conn )
1086 {
1087         slap_overinst   *on             = (slap_overinst *)be->bd_info;
1088
1089         if ( sort_conns[conn->c_conn_idx] )
1090                 free_sort_op( conn, sort_conns[conn->c_conn_idx] );
1091
1092         return LDAP_SUCCESS;
1093 }
1094
1095 static int sssvlv_db_open(
1096         BackendDB               *be,
1097         ConfigReply             *cr )
1098 {
1099         slap_overinst   *on = (slap_overinst *)be->bd_info;
1100         sssvlv_info *si = on->on_bi.bi_private;
1101
1102         /* If not set, default to 1/2 of available threads */
1103         if ( !si->svi_max )
1104                 si->svi_max = connection_pool_max / 2;
1105
1106         int rc = overlay_register_control( be, LDAP_CONTROL_SORTREQUEST );
1107         if ( rc == LDAP_SUCCESS )
1108                 rc = overlay_register_control( be, LDAP_CONTROL_VLVREQUEST );
1109         return rc;
1110 }
1111
1112 static ConfigTable sssvlv_cfg[] = {
1113         { "sssvlv-max", "num",
1114                 2, 2, 0, ARG_INT|ARG_OFFSET,
1115                         (void *)offsetof(sssvlv_info, svi_max),
1116                 "( OLcfgOvAt:21.1 NAME 'olcSssVlvMax' "
1117                         "DESC 'Maximum number of concurrent Sort requests' "
1118                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
1119         { "sssvlv-maxkeys", "num",
1120                 2, 2, 0, ARG_INT|ARG_OFFSET,
1121                         (void *)offsetof(sssvlv_info, svi_max_keys),
1122                 "( OLcfgOvAt:21.2 NAME 'olcSssVlvMaxKeys' "
1123                         "DESC 'Maximum number of Keys in a Sort request' "
1124                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
1125         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1126 };
1127
1128 static ConfigOCs sssvlv_ocs[] = {
1129         { "( OLcfgOvOc:21.1 "
1130                 "NAME 'olcSssVlvConfig' "
1131                 "DESC 'SSS VLV configuration' "
1132                 "SUP olcOverlayConfig "
1133                 "MAY ( olcSssVlvMax $ olcSssVlvMaxKeys ) )",
1134                 Cft_Overlay, sssvlv_cfg, NULL, NULL },
1135         { NULL, 0, NULL }
1136 };
1137
1138 static int sssvlv_db_init(
1139         BackendDB               *be,
1140         ConfigReply             *cr)
1141 {
1142         slap_overinst   *on = (slap_overinst *)be->bd_info;
1143         sssvlv_info *si;
1144         
1145         si = (sssvlv_info *)ch_malloc(sizeof(sssvlv_info));
1146         on->on_bi.bi_private = si;
1147
1148         si->svi_max = 0;
1149         si->svi_num = 0;
1150         si->svi_max_keys = SSSVLV_DEFAULT_MAX_KEYS;
1151
1152         if ( dtblsize && !sort_conns ) {
1153                 ldap_pvt_thread_mutex_init( &sort_conns_mutex );
1154                 /* accommodate for c_conn_idx == -1 */
1155                 sort_conns = ch_calloc( sizeof(sort_op *), dtblsize + 1 );
1156                 sort_conns++;
1157         }
1158
1159         return LDAP_SUCCESS;
1160 }
1161
1162 static int sssvlv_db_destroy(
1163         BackendDB               *be,
1164         ConfigReply             *cr )
1165 {
1166         slap_overinst   *on = (slap_overinst *)be->bd_info;
1167         sssvlv_info *si = (sssvlv_info *)on->on_bi.bi_private;
1168         
1169         if ( si ) {
1170                 ch_free( si );
1171                 on->on_bi.bi_private = NULL;
1172         }
1173         return LDAP_SUCCESS;
1174 }
1175
1176 static slap_overinst sssvlv;
1177
1178 int sssvlv_initialize()
1179 {
1180         int rc;
1181
1182         sssvlv.on_bi.bi_type                            = "sssvlv";
1183         sssvlv.on_bi.bi_db_init                         = sssvlv_db_init;
1184         sssvlv.on_bi.bi_db_destroy                      = sssvlv_db_destroy;
1185         sssvlv.on_bi.bi_db_open                         = sssvlv_db_open;
1186         sssvlv.on_bi.bi_connection_destroy      = sssvlv_connection_destroy;
1187         sssvlv.on_bi.bi_op_search                       = sssvlv_op_search;
1188
1189         sssvlv.on_bi.bi_cf_ocs = sssvlv_ocs;
1190
1191         rc = config_register_schema( sssvlv_cfg, sssvlv_ocs );
1192         if ( rc )
1193                 return rc;
1194
1195         rc = register_supported_control2( LDAP_CONTROL_SORTREQUEST,
1196                         SLAP_CTRL_SEARCH,
1197                         NULL,
1198                         sss_parseCtrl,
1199                         1 /* replace */,
1200                         &sss_cid );
1201
1202         if ( rc == LDAP_SUCCESS ) {
1203                 rc = register_supported_control2( LDAP_CONTROL_VLVREQUEST,
1204                         SLAP_CTRL_SEARCH,
1205                         NULL,
1206                         vlv_parseCtrl,
1207                         1 /* replace */,
1208                         &vlv_cid );
1209         }
1210
1211         if ( rc == LDAP_SUCCESS ) {
1212                 rc = overlay_register( &sssvlv );
1213                 if ( rc != LDAP_SUCCESS ) {
1214                         fprintf( stderr, "Failed to register server side sort overlay\n" );
1215                 }
1216         }
1217         else {
1218                 fprintf( stderr, "Failed to register control %d\n", rc );
1219         }
1220
1221         return rc;
1222 }
1223
1224 #if SLAPD_OVER_SSSVLV == SLAPD_MOD_DYNAMIC
1225 int init_module( int argc, char *argv[])
1226 {
1227         return sssvlv_initialize();
1228 }
1229 #endif
1230
1231 #endif /* SLAPD_OVER_SSSVLV */