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