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