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