]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/sssvlv.c
555079fa8cf56642a0e79cbd60c6ae9cf0ceaa07
[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 #define SLAPD_OVER_SSSVLV       SLAPD_MOD_DYNAMIC
25
26 #ifdef SLAPD_OVER_SSSVLV
27
28 #include <stdio.h>
29
30 #include <ac/string.h>
31 #include <ac/ctype.h>
32
33 #include <avl.h>
34
35 #include "slap.h"
36 #include "lutil.h"
37
38 /* RFC2891: Server Side Sorting
39  * RFC2696: Paged Results
40  */
41 #ifndef LDAP_MATCHRULE_IDENTIFIER
42 #define LDAP_MATCHRULE_IDENTIFIER      0x80L
43 #define LDAP_REVERSEORDER_IDENTIFIER   0x81L
44 #define LDAP_ATTRTYPES_IDENTIFIER      0x80L
45 #endif
46
47 /* draft-ietf-ldapext-ldapv3-vlv-09.txt: Virtual List Views
48  */
49 #ifndef LDAP_VLVBYINDEX_IDENTIFIER
50 #define LDAP_VLVBYINDEX_IDENTIFIER         0xa0L
51 #define LDAP_VLVBYVALUE_IDENTIFIER     0x81L
52 #define LDAP_VLVCONTEXT_IDENTIFIER     0x04L
53
54 #define LDAP_VLV_SSS_MISSING    0x4C
55 #define LDAP_VLV_RANGE_ERROR    0x4D
56 #endif
57
58 #define SAFESTR(macro_str, macro_def) ((macro_str) ? (macro_str) : (macro_def))
59
60 typedef struct vlv_ctrl {
61         int vc_before;
62         int vc_after;
63         int     vc_offset;
64         int vc_count;
65         struct berval vc_value;
66         unsigned long vc_context;
67 } vlv_ctrl;
68
69 typedef struct sort_key
70 {
71         AttributeDescription    *sk_ad;
72         MatchingRule                    *sk_ordering;
73         int                                             sk_direction;   /* 1=normal, -1=reverse */
74 } sort_key;
75
76 typedef struct sort_ctrl {
77         int sc_nkeys;
78         sort_key sc_keys[0];
79 } sort_ctrl;
80
81
82 typedef struct sort_node
83 {
84         int sn_conn;
85         struct berval sn_dn;
86         struct berval *sn_vals;
87 } sort_node;
88
89 typedef struct sssvlv_info
90 {
91         int svi_max;    /* max concurrent sorts */
92         int svi_num;    /* current # sorts */
93         int svi_max_keys;       /* max sort keys per request */
94 } sssvlv_info;
95
96 typedef struct sort_op
97 {
98         Avlnode *so_tree;
99         sort_ctrl *so_ctrl;
100         sssvlv_info *so_info;
101         int so_paged;
102         int so_page_size;
103         int so_nentries;
104         int so_vlv;
105         int so_vlv_rc;
106         int so_vlv_target;
107         unsigned long so_vcontext;
108 } sort_op;
109
110 /* There is only one conn table for all overlay instances */
111 static sort_op **sort_conns;
112 static ldap_pvt_thread_mutex_t sort_conns_mutex;
113 static const char *debug_header = "sssvlv";
114
115 static int sss_cid;
116 static int vlv_cid;
117
118 /* RFC 2981 Section 2.2
119  * If a sort key is a multi-valued attribute, and an entry happens to
120  * have multiple values for that attribute and no other controls are
121  * present that affect the sorting order, then the server SHOULD use the
122  * least value (according to the ORDERING rule for that attribute).
123  */
124 static struct berval* select_value(
125         Attribute               *attr,
126         sort_key                        *key )
127 {
128         struct berval* ber1, *ber2;
129         MatchingRule *mr = key->sk_ordering;
130         int i, cmp;
131
132         ber1 = &(attr->a_nvals[0]);
133         ber2 = ber1+1;
134         for ( i = 1; i < attr->a_numvals; i++,ber2++ ) {
135                 mr->smr_match( &cmp, 0, mr->smr_syntax, mr, ber1, ber2 );
136                 if ( cmp > 0 ) {
137                         ber1 = ber2;
138                 }
139         }
140
141         Debug(LDAP_DEBUG_TRACE, "%s: value selected for compare: %s\n",
142                 debug_header,
143                 SAFESTR(ber1->bv_val, "<Empty>"),
144                 0);
145
146         return ber1;
147 }
148
149 static int node_cmp( const void* val1, const void* val2 )
150 {
151         sort_node *sn1 = (sort_node *)val1;
152         sort_node *sn2 = (sort_node *)val2;
153         sort_ctrl *sc = sort_conns[sn1->sn_conn]->so_ctrl;
154         MatchingRule *mr;
155         int i, cmp = 0;
156
157         for ( i=0; cmp == 0 && i<sc->sc_nkeys; i++ ) {
158                 if ( BER_BVISNULL( &sn1->sn_vals[i] )) {
159                         if ( BER_BVISNULL( &sn2->sn_vals[i] ))
160                                 cmp = 0;
161                         else
162                                 cmp = sc->sc_keys[i].sk_direction;
163                 } else if ( BER_BVISNULL( &sn2->sn_vals[i] )) {
164                         cmp = sc->sc_keys[i].sk_direction * -1;
165                 } else {
166                         mr = sc->sc_keys[i].sk_ordering;
167                         mr->smr_match( &cmp, 0, mr->smr_syntax, mr,
168                                 &sn1->sn_vals[i], &sn2->sn_vals[i] );
169                         if ( cmp )
170                                 cmp *= sc->sc_keys[i].sk_direction;
171                 }
172         }
173         return cmp;
174 }
175
176 static int node_insert( const void *val1, const void *val2 )
177 {
178         /* Never return equal so that new entries are always inserted */
179         return node_cmp( val1, val2 ) < 0 ? -1 : 1;
180 }
181
182 static int pack_vlv_response_control(
183         Operation               *op,
184         SlapReply               *rs,
185         sort_op                 *so,
186         LDAPControl     **ctrlsp )
187 {
188         LDAPControl                     *ctrl;
189         BerElementBuffer        berbuf;
190         BerElement                      *ber            = (BerElement *)&berbuf;
191         PagedResultsCookie      resp_cookie;
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                 sn = sn2;
637
638                 ptr = (char *)(sn->sn_vals + sc->sc_nkeys);
639                 sn->sn_dn.bv_val = ptr;
640                 sn->sn_dn.bv_len = rs->sr_entry->e_nname.bv_len;
641                 AC_MEMCPY( ptr, rs->sr_entry->e_nname.bv_val,
642                         rs->sr_entry->e_nname.bv_len );
643                 ptr += rs->sr_entry->e_nname.bv_len;
644                 *ptr++ = '\0';
645                 for ( i=0; i<sc->sc_nkeys; i++ ) {
646                         if ( !BER_BVISNULL( &sn->sn_vals[i] )) {
647                                 AC_MEMCPY( ptr, sn->sn_vals[i].bv_val, sn->sn_vals[i].bv_len );
648                                 sn->sn_vals[i].bv_val = ptr;
649                                 ptr += sn->sn_vals[i].bv_len;
650                                 *ptr++ = '\0';
651                         }
652                 }
653                 sn->sn_conn = op->o_conn->c_conn_idx;
654
655                 /* Insert into the AVL tree */
656                 tavl_insert(&(so->so_tree), sn, node_insert, avl_dup_error);
657
658                 so->so_nentries++;
659
660                 /* Collected the keys so that they can be sorted.  Thus, stop
661                  * the entry from propagating.
662                  */
663                 rs->sr_err = LDAP_SUCCESS;
664         }
665         else if ( rs->sr_type == REP_RESULT ) {
666                 /* Remove serversort response callback.
667                  * We don't want the entries that we are about to send to be
668                  * processed by serversort response again.
669                  */
670                 if ( op->o_callback->sc_response == sssvlv_op_response ) {
671                         op->o_callback = op->o_callback->sc_next;
672                 }
673
674                 send_entry( op, rs, so );
675                 send_result( op, rs, so );
676         }
677
678         return rs->sr_err;
679 }
680
681 static int sssvlv_op_search(
682         Operation               *op,
683         SlapReply               *rs)
684 {
685         slap_overinst                   *on                     = (slap_overinst *)op->o_bd->bd_info;
686         sssvlv_info                             *si                     = on->on_bi.bi_private;
687         int                                             rc                      = SLAP_CB_CONTINUE;
688         int     ok;
689         sort_op *so, so2;
690         sort_ctrl *sc = op->o_controls[sss_cid];
691         PagedResultsState *ps;
692         vlv_ctrl *vc;
693
694         if ( op->o_ctrlflag[sss_cid] <= SLAP_CONTROL_IGNORED ) {
695                 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
696                         LDAPControl *ctrls[2];
697                         so2.so_vcontext = 0;
698                         so2.so_vlv_target = 0;
699                         so2.so_nentries = 0;
700                         so2.so_vlv_rc = LDAP_VLV_SSS_MISSING;
701                         rc = pack_vlv_response_control( op, rs, &so2, ctrls );
702                         if ( rc == LDAP_SUCCESS ) {
703                                 ctrls[1] = NULL;
704                                 slap_add_ctrls( op, rs, ctrls );
705                         }
706                         rs->sr_err = LDAP_VLV_ERROR;
707                         rs->sr_text = "Sort control is required with VLV";
708                         goto leave;
709                 }
710                 /* Not server side sort so just continue */
711                 return SLAP_CB_CONTINUE;
712         }
713
714         Debug(LDAP_DEBUG_TRACE,
715                 "==> sssvlv_search: <%s> %s, control flag: %d\n",
716                 op->o_req_dn.bv_val, op->ors_filterstr.bv_val,
717                 op->o_ctrlflag[sss_cid]);
718
719         if ( sc->sc_nkeys > si->svi_max_keys ) {
720                 rs->sr_text = "Too many sort keys";
721                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
722                 goto leave;
723         }
724
725         ps = ( op->o_pagedresults > SLAP_CONTROL_IGNORED ) ?
726                 (PagedResultsState*)(op->o_pagedresults_state) : NULL;
727         vc = op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ?
728                 op->o_controls[vlv_cid] : NULL;
729
730         if ( ps && vc ) {
731                 rs->sr_text = "VLV incompatible with PagedResults";
732                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
733                 goto leave;
734         }
735
736         ok = 1;
737         ldap_pvt_thread_mutex_lock( &sort_conns_mutex );
738         so = sort_conns[op->o_conn->c_conn_idx];
739         /* Is there already a sort running on this conn? */
740         if ( so ) {
741                 /* Is it a continuation of a VLV search? */
742                 if ( !vc || so->so_vlv <= SLAP_CONTROL_IGNORED ||
743                         vc->vc_context != so->so_vcontext ) {
744                         /* Is it a continuation of a paged search? */
745                         if ( !ps || so->so_paged <= SLAP_CONTROL_IGNORED ||
746                                 op->o_conn->c_pagedresults_state.ps_cookie != ps->ps_cookie ) {
747                                 ok = 0;
748                         } else if ( !ps->ps_size ) {
749                         /* Abandoning current request */
750                                 ok = 0;
751                                 so->so_nentries = 0;
752                                 rs->sr_err = LDAP_SUCCESS;
753                         }
754                 }
755                 if (( vc && so->so_paged > SLAP_CONTROL_IGNORED ) ||
756                         ( ps && so->so_vlv > SLAP_CONTROL_IGNORED )) {
757                         /* changed from paged to vlv or vice versa, abandon */
758                         ok = 0;
759                         so->so_nentries = 0;
760                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
761                 }
762         /* Are there too many running overall? */
763         } else if ( si->svi_num >= si->svi_max ) {
764                 ok = 0;
765         } else {
766                 /* OK, this connection now has a sort running */
767                 si->svi_num++;
768                 sort_conns[op->o_conn->c_conn_idx] = &so2;
769         }
770         ldap_pvt_thread_mutex_unlock( &sort_conns_mutex );
771         if ( ok ) {
772                 /* are we continuing a VLV search? */
773                 if ( vc && vc->vc_context ) {
774                         so->so_ctrl = sc;
775                         send_list( op, rs, so );
776                         send_result( op, rs, so );
777                         rc = LDAP_SUCCESS;
778                 /* are we continuing a paged search? */
779                 } else if ( ps && ps->ps_cookie ) {
780                         so->so_ctrl = sc;
781                         send_page( op, rs, so );
782                         send_result( op, rs, so );
783                         rc = LDAP_SUCCESS;
784                 } else {
785                         slap_callback *cb = op->o_tmpalloc( sizeof(slap_callback),
786                                 op->o_tmpmemctx );
787                         /* Install serversort response callback to handle a new search */
788                         if ( ps || vc ) {
789                                 so = ch_malloc( sizeof(sort_op));
790                         } else {
791                                 so = op->o_tmpalloc( sizeof(sort_op), op->o_tmpmemctx );
792                         }
793                         sort_conns[op->o_conn->c_conn_idx] = so;
794
795                         cb->sc_cleanup          = NULL;
796                         cb->sc_response         = sssvlv_op_response;
797                         cb->sc_next                     = op->o_callback;
798                         cb->sc_private          = so;
799
800                         so->so_tree = NULL;
801                         so->so_ctrl = sc;
802                         so->so_info = si;
803                         if ( ps ) {
804                                 so->so_paged = op->o_pagedresults;
805                                 so->so_page_size = ps->ps_size;
806                                 op->o_pagedresults = SLAP_CONTROL_IGNORED;
807                         } else {
808                                 so->so_paged = 0;
809                                 so->so_page_size = 0;
810                                 if ( vc )
811                                         so->so_vlv = op->o_ctrlflag[vlv_cid];
812                         }
813                         so->so_vcontext = (unsigned long)so;
814                         so->so_nentries = 0;
815
816                         op->o_callback          = cb;
817                 }
818         } else {
819                 if ( so && !so->so_nentries ) {
820                         free_sort_op( op->o_conn, so );
821                 } else {
822                         rs->sr_text = "Other sort requests already in progress";
823                         rs->sr_err = LDAP_BUSY;
824                 }
825 leave:
826                 rc = rs->sr_err;
827                 send_ldap_result( op, rs );
828         }
829
830         return rc;
831 }
832
833 static int get_ordering_rule(
834         AttributeDescription    *ad,
835         struct berval                   *matchrule,
836         SlapReply                               *rs,
837         MatchingRule                    **ordering )
838 {
839         MatchingRule* mr;
840
841         if ( matchrule && matchrule->bv_val ) {
842                 mr = mr_find( matchrule->bv_val );
843                 if ( mr == NULL ) {
844                         rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
845                         rs->sr_text = "serverSort control: No ordering rule";
846                         Debug(LDAP_DEBUG_TRACE, "%s: no ordering rule function for %s\n",
847                                 debug_header, matchrule->bv_val, 0);
848                 }
849         }
850         else {
851                 mr = ad->ad_type->sat_ordering;
852                 if ( mr == NULL ) {
853                         rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
854                         rs->sr_text = "serverSort control: No ordering rule";
855                         Debug(LDAP_DEBUG_TRACE,
856                                 "%s: no ordering rule specified and no default ordering rule for attribute %s\n",
857                                 debug_header, ad->ad_cname.bv_val, 0);
858                 }
859         }
860
861         *ordering = mr;
862         return rs->sr_err;
863 }
864
865 static int count_key(BerElement *ber)
866 {
867         char *end;
868         ber_len_t len;
869         ber_tag_t tag;
870         int count = 0;
871
872         /* Server Side Sort Control is a SEQUENCE of SEQUENCE */
873         for ( tag = ber_first_element( ber, &len, &end );
874                   tag == LBER_SEQUENCE;
875                   tag = ber_next_element( ber, &len, end ))
876         {
877                 tag = ber_skip_tag( ber, &len );
878                 ber_skip_data( ber, len );
879                 ++count;
880         }
881         ber_rewind( ber );
882
883         return count;
884 }
885
886 static int build_key(
887         BerElement              *ber,
888         SlapReply               *rs,
889         sort_key                        *key )
890 {
891         struct berval attr;
892         struct berval matchrule = BER_BVNULL;
893         ber_int_t reverse = 0;
894         ber_tag_t tag;
895         ber_len_t len;
896         MatchingRule *ordering = NULL;
897         AttributeDescription *ad = NULL;
898         const char *text;
899
900         if (( tag = ber_scanf( ber, "{" )) == LBER_ERROR ) {
901                 rs->sr_text = "serverSort control: decoding error";
902                 rs->sr_err = LDAP_PROTOCOL_ERROR;
903                 return rs->sr_err;
904         }
905
906         if (( tag = ber_scanf( ber, "m", &attr )) == LBER_ERROR ) {
907                 rs->sr_text = "serverSort control: attribute decoding error";
908                 rs->sr_err = LDAP_PROTOCOL_ERROR;
909                 return rs->sr_err;
910         }
911
912         tag = ber_peek_tag( ber, &len );
913         if ( tag == LDAP_MATCHRULE_IDENTIFIER ) {
914                 if (( tag = ber_scanf( ber, "m", &matchrule )) == LBER_ERROR ) {
915                         rs->sr_text = "serverSort control: matchrule decoding error";
916                         rs->sr_err = LDAP_PROTOCOL_ERROR;
917                         return rs->sr_err;
918                 }
919                 tag = ber_peek_tag( ber, &len );
920         }
921
922         if ( tag == LDAP_REVERSEORDER_IDENTIFIER ) {
923                 if (( tag = ber_scanf( ber, "b", &reverse )) == LBER_ERROR ) {
924                         rs->sr_text = "serverSort control: reverse decoding error";
925                         rs->sr_err = LDAP_PROTOCOL_ERROR;
926                         return rs->sr_err;
927                 }
928         }
929
930         if (( tag = ber_scanf( ber, "}" )) == LBER_ERROR ) {
931                 rs->sr_text = "serverSort control: decoding error";
932                 rs->sr_err = LDAP_PROTOCOL_ERROR;
933                 return rs->sr_err;
934         }
935
936         if ( slap_bv2ad( &attr, &ad, &text ) != LDAP_SUCCESS ) {
937                 rs->sr_text =
938                         "serverSort control: Unrecognized attribute type in sort key";
939                 Debug(LDAP_DEBUG_TRACE,
940                         "%s: Unrecognized attribute type in sort key: %s\n",
941                         debug_header, SAFESTR(attr.bv_val, "<None>"), 0);
942                 rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
943                 return rs->sr_err;
944         }
945
946         /* get_ordering_rule will set sr_err and sr_text */
947         get_ordering_rule( ad, &matchrule, rs, &ordering );
948         if ( rs->sr_err != LDAP_SUCCESS ) {
949                 return rs->sr_err;
950         }
951
952         key->sk_ad = ad;
953         key->sk_ordering = ordering;
954         key->sk_direction = reverse ? -1 : 1;
955
956         return rs->sr_err;
957 }
958
959 static int sss_parseCtrl(
960         Operation               *op,
961         SlapReply               *rs,
962         LDAPControl             *ctrl )
963 {
964         BerElementBuffer        berbuf;
965         BerElement                      *ber;
966         ber_tag_t               tag;
967         ber_len_t               len;
968         int                                     i;
969         sort_ctrl       *sc;
970
971         rs->sr_err = LDAP_PROTOCOL_ERROR;
972
973         if ( op->o_ctrlflag[sss_cid] > SLAP_CONTROL_IGNORED ) {
974                 rs->sr_text = "sorted results control specified multiple times";
975         } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
976                 rs->sr_text = "sorted results control value is absent";
977         } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
978                 rs->sr_text = "sorted results control value is empty";
979         } else {
980                 rs->sr_err = LDAP_SUCCESS;
981         }
982         if ( rs->sr_err != LDAP_SUCCESS )
983                 return rs->sr_err;
984
985         op->o_ctrlflag[sss_cid] = ctrl->ldctl_iscritical ?
986                 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
987
988         ber = (BerElement *)&berbuf;
989         ber_init2( ber, &ctrl->ldctl_value, 0 );
990         i = count_key( ber );
991
992         sc = op->o_tmpalloc( sizeof(sort_ctrl) +
993                 i * sizeof(sort_key), op->o_tmpmemctx );
994         sc->sc_nkeys = i;
995         op->o_controls[sss_cid] = sc;
996
997         /* peel off initial sequence */
998         ber_scanf( ber, "{" );
999
1000         i = 0;
1001         do {
1002                 if ( build_key( ber, rs, &sc->sc_keys[i] ) != LDAP_SUCCESS )
1003                         break;
1004                 i++;
1005                 tag = ber_peek_tag( ber, &len );
1006         } while ( tag != LBER_DEFAULT );
1007
1008         return rs->sr_err;
1009 }
1010
1011 static int vlv_parseCtrl(
1012         Operation               *op,
1013         SlapReply               *rs,
1014         LDAPControl             *ctrl )
1015 {
1016         BerElementBuffer        berbuf;
1017         BerElement                      *ber;
1018         ber_tag_t               tag;
1019         ber_len_t               len;
1020         int                                     i;
1021         vlv_ctrl        *vc, vc2;
1022
1023         rs->sr_err = LDAP_PROTOCOL_ERROR;
1024         rs->sr_text = NULL;
1025
1026         if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
1027                 rs->sr_text = "vlv control specified multiple times";
1028         } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1029                 rs->sr_text = "vlv control value is absent";
1030         } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1031                 rs->sr_text = "vlv control value is empty";
1032         }
1033         if ( rs->sr_text != NULL )
1034                 return rs->sr_err;
1035
1036         op->o_ctrlflag[vlv_cid] = ctrl->ldctl_iscritical ?
1037                 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1038
1039         ber = (BerElement *)&berbuf;
1040         ber_init2( ber, &ctrl->ldctl_value, 0 );
1041
1042         rs->sr_err = LDAP_PROTOCOL_ERROR;
1043
1044         tag = ber_scanf( ber, "{ii", &vc2.vc_before, &vc2.vc_after );
1045         if ( tag == LBER_ERROR ) {
1046                 return rs->sr_err;
1047         }
1048
1049         tag = ber_peek_tag( ber, &len );
1050         if ( tag == LDAP_VLVBYINDEX_IDENTIFIER ) {
1051                 tag = ber_scanf( ber, "{ii}", &vc2.vc_offset, &vc2.vc_count );
1052                 if ( tag == LBER_ERROR )
1053                         return rs->sr_err;
1054                 BER_BVZERO( &vc2.vc_value );
1055         } else if ( tag == LDAP_VLVBYVALUE_IDENTIFIER ) {
1056                 tag = ber_scanf( ber, "m", &vc2.vc_value );
1057                 if ( tag == LBER_ERROR || BER_BVISNULL( &vc2.vc_value ))
1058                         return rs->sr_err;
1059         } else {
1060                 return rs->sr_err;
1061         }
1062         tag = ber_peek_tag( ber, &len );
1063         if ( tag == LDAP_VLVCONTEXT_IDENTIFIER ) {
1064                 struct berval bv;
1065                 tag = ber_scanf( ber, "m", &bv );
1066                 if ( tag == LBER_ERROR || bv.bv_len != sizeof(vc2.vc_context))
1067                         return rs->sr_err;
1068                 AC_MEMCPY( &vc2.vc_context, bv.bv_val, bv.bv_len );
1069         } else {
1070                 vc2.vc_context = 0;
1071         }
1072
1073         vc = op->o_tmpalloc( sizeof(vlv_ctrl), op->o_tmpmemctx );
1074         *vc = vc2;
1075         op->o_controls[vlv_cid] = vc;
1076         rs->sr_err = LDAP_SUCCESS;
1077
1078         return rs->sr_err;
1079 }
1080
1081 static int sssvlv_connection_destroy( BackendDB *be, Connection *conn )
1082 {
1083         slap_overinst   *on             = (slap_overinst *)be->bd_info;
1084
1085         if ( sort_conns[conn->c_conn_idx] )
1086                 free_sort_op( conn, sort_conns[conn->c_conn_idx] );
1087
1088         return LDAP_SUCCESS;
1089 }
1090
1091 static int sssvlv_db_open(
1092         BackendDB               *be,
1093         ConfigReply             *cr )
1094 {
1095         int rc = overlay_register_control( be, LDAP_CONTROL_SORTREQUEST );
1096         if ( rc == LDAP_SUCCESS )
1097                 rc = overlay_register_control( be, LDAP_CONTROL_VLVREQUEST );
1098         return rc;
1099 }
1100
1101 static int sssvlv_db_init(
1102         BackendDB               *be,
1103         ConfigReply             *cr)
1104 {
1105         slap_overinst   *on = (slap_overinst *)be->bd_info;
1106         sssvlv_info *si;
1107         
1108         si = (sssvlv_info *)ch_malloc(sizeof(sssvlv_info));
1109         on->on_bi.bi_private = si;
1110
1111         si->svi_max = 5;
1112         si->svi_num = 0;
1113         si->svi_max_keys = 5;
1114
1115         if ( dtblsize && !sort_conns ) {
1116                 ldap_pvt_thread_mutex_init( &sort_conns_mutex );
1117                 /* accommodate for c_conn_idx == -1 */
1118                 sort_conns = ch_calloc( sizeof(sort_op *), dtblsize + 1 );
1119                 sort_conns++;
1120         }
1121
1122         return LDAP_SUCCESS;
1123 }
1124
1125 static int sssvlv_db_destroy(
1126         BackendDB               *be,
1127         ConfigReply             *cr )
1128 {
1129         slap_overinst   *on = (slap_overinst *)be->bd_info;
1130         sssvlv_info *si = (sssvlv_info *)on->on_bi.bi_private;
1131         
1132         if ( si ) {
1133                 ch_free( si );
1134                 on->on_bi.bi_private = NULL;
1135         }
1136         return LDAP_SUCCESS;
1137 }
1138
1139 static slap_overinst sssvlv;
1140
1141 int sssvlv_initialize()
1142 {
1143         int rc;
1144
1145         sssvlv.on_bi.bi_type                            = "sssvlv";
1146         sssvlv.on_bi.bi_db_init                         = sssvlv_db_init;
1147         sssvlv.on_bi.bi_db_destroy                      = sssvlv_db_destroy;
1148         sssvlv.on_bi.bi_db_open                         = sssvlv_db_open;
1149         sssvlv.on_bi.bi_connection_destroy      = sssvlv_connection_destroy;
1150         sssvlv.on_bi.bi_op_search                       = sssvlv_op_search;
1151
1152         rc = register_supported_control2( LDAP_CONTROL_SORTREQUEST,
1153                         SLAP_CTRL_SEARCH,
1154                         NULL,
1155                         sss_parseCtrl,
1156                         1 /* replace */,
1157                         &sss_cid );
1158
1159         if ( rc == LDAP_SUCCESS ) {
1160                 rc = register_supported_control2( LDAP_CONTROL_VLVREQUEST,
1161                         SLAP_CTRL_SEARCH,
1162                         NULL,
1163                         vlv_parseCtrl,
1164                         1 /* replace */,
1165                         &vlv_cid );
1166         }
1167
1168         if ( rc == LDAP_SUCCESS ) {
1169                 rc = overlay_register( &sssvlv );
1170                 if ( rc != LDAP_SUCCESS ) {
1171                         fprintf( stderr, "Failed to register server side sort overlay\n" );
1172                 }
1173         }
1174         else {
1175                 fprintf( stderr, "Failed to register control %d\n", rc );
1176         }
1177
1178         return rc;
1179 }
1180
1181 #if SLAPD_OVER_SSSVLV == SLAPD_MOD_DYNAMIC
1182 int init_module( int argc, char *argv[])
1183 {
1184         return sssvlv_initialize();
1185 }
1186 #endif
1187
1188 #endif /* SLAPD_OVER_SSSVLV */