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