1 /* sssvlv.c - server side sort / virtual list view */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2009 The OpenLDAP Foundation.
6 * Portions copyright 2009 Symas Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
18 * This work was initially developed by Howard Chu for inclusion in
24 #define SLAPD_OVER_SSSVLV SLAPD_MOD_DYNAMIC
26 #ifdef SLAPD_OVER_SSSVLV
30 #include <ac/string.h>
38 /* RFC2891: Server Side Sorting
39 * RFC2696: Paged Results
41 #ifndef LDAP_MATCHRULE_IDENTIFIER
42 #define LDAP_MATCHRULE_IDENTIFIER 0x80L
43 #define LDAP_REVERSEORDER_IDENTIFIER 0x81L
44 #define LDAP_ATTRTYPES_IDENTIFIER 0x80L
47 /* draft-ietf-ldapext-ldapv3-vlv-09.txt: Virtual List Views
49 #ifndef LDAP_VLVBYINDEX_IDENTIFIER
50 #define LDAP_VLVBYINDEX_IDENTIFIER 0xa0L
51 #define LDAP_VLVBYVALUE_IDENTIFIER 0x81L
52 #define LDAP_VLVCONTEXT_IDENTIFIER 0x04L
54 #define LDAP_VLV_SSS_MISSING 0x4C
55 #define LDAP_VLV_RANGE_ERROR 0x4D
58 #define SAFESTR(macro_str, macro_def) ((macro_str) ? (macro_str) : (macro_def))
60 typedef struct vlv_ctrl {
65 struct berval vc_value;
66 unsigned long vc_context;
69 typedef struct sort_key
71 AttributeDescription *sk_ad;
72 MatchingRule *sk_ordering;
73 int sk_direction; /* 1=normal, -1=reverse */
76 typedef struct sort_ctrl {
82 typedef struct sort_node
86 struct berval *sn_vals;
89 typedef struct sssvlv_info
91 int svi_max; /* max concurrent sorts */
92 int svi_num; /* current # sorts */
93 int svi_max_keys; /* max sort keys per request */
96 typedef struct sort_op
100 sssvlv_info *so_info;
107 unsigned long so_vcontext;
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";
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).
124 static struct berval* select_value(
128 struct berval* ber1, *ber2;
129 MatchingRule *mr = key->sk_ordering;
132 ber1 = &(attr->a_nvals[0]);
134 for ( i = 1; i < attr->a_numvals; i++,ber2++ ) {
135 mr->smr_match( &cmp, 0, mr->smr_syntax, mr, ber1, ber2 );
141 Debug(LDAP_DEBUG_TRACE, "%s: value selected for compare: %s\n",
143 SAFESTR(ber1->bv_val, "<Empty>"),
149 static int node_cmp( const void* val1, const void* val2 )
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;
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] ))
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;
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] );
170 cmp *= sc->sc_keys[i].sk_direction;
176 static int node_insert( const void *val1, const void *val2 )
178 /* Never return equal so that new entries are always inserted */
179 return node_cmp( val1, val2 ) < 0 ? -1 : 1;
182 static int pack_vlv_response_control(
186 LDAPControl **ctrlsp )
189 BerElementBuffer berbuf;
190 BerElement *ber = (BerElement *)&berbuf;
191 PagedResultsCookie resp_cookie;
192 struct berval cookie, bv;
195 ber_init2( ber, NULL, LBER_USE_DER );
196 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
198 rc = ber_printf( ber, "{iii", so->so_vlv_target, so->so_nentries,
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 );
208 rc = ber_printf( ber, "}" );
212 rc = ber_flatten2( ber, &bv, 0 );
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 );
226 rs->sr_err = LDAP_OTHER;
234 static int pack_pagedresult_response_control(
238 LDAPControl **ctrlsp )
241 BerElementBuffer berbuf;
242 BerElement *ber = (BerElement *)&berbuf;
243 PagedResultsCookie resp_cookie;
244 struct berval cookie, bv;
247 ber_init2( ber, NULL, LBER_USE_DER );
248 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
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;
255 resp_cookie = ( PagedResultsCookie )0;
256 BER_BVZERO( &cookie );
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
264 rc = ber_printf( ber, "{iO}", so->so_nentries, &cookie );
266 rc = ber_flatten2( ber, &bv, 0 );
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 );
280 rs->sr_err = LDAP_OTHER;
288 static int pack_sss_response_control(
291 LDAPControl **ctrlsp )
294 BerElementBuffer berbuf;
295 BerElement *ber = (BerElement *)&berbuf;
299 ber_init2( ber, NULL, LBER_USE_DER );
300 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
302 /* Pack error code */
303 rc = ber_printf(ber, "{e}", rs->sr_err);
306 rc = ber_flatten2( ber, &bv, 0 );
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 );
319 rs->sr_err = LDAP_OTHER;
327 static void free_sort_op( Connection *conn, sort_op *so )
330 tavl_free( so->so_tree, ch_free );
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 );
342 static int send_list(
347 Avlnode *cur_node, *tmp_node;
348 vlv_ctrl *vc = op->o_controls[vlv_cid];
352 LDAPControl *ctrls[2];
354 /* FIXME: it may be better to just flatten the tree into
355 * an array before doing all of this...
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;
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 )
374 target = so->so_nentries * vc->vc_offset / vc->vc_count;
376 if ( vc->vc_offset > so->so_nentries ) {
378 so->so_vlv_rc = LDAP_VLV_RANGE_ERROR;
379 pack_vlv_response_control( op, rs, so, ctrls );
381 slap_add_ctrls( op, rs, ctrls );
382 rs->sr_err = LDAP_VLV_ERROR;
385 target = vc->vc_offset;
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;
393 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
395 target = so->so_nentries - target + 1;
397 for ( i=1; i<target; i++ )
398 cur_node = tavl_next( cur_node, dir );
401 /* we're looking for a specific value */
402 sort_ctrl *sc = so->so_ctrl;
403 MatchingRule *mr = sc->sc_keys[0].sk_ordering;
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 );
411 so->so_vlv_rc = LDAP_INAPPROPRIATE_MATCHING;
412 pack_vlv_response_control( op, rs, so, ctrls );
414 slap_add_ctrls( op, rs, ctrls );
415 rs->sr_err = LDAP_VLV_ERROR;
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;
427 for (i=1; i<sc->sc_nkeys; i++) {
428 BER_BVZERO( &sn->sn_vals[i] );
430 cur_node = tavl_find2( so->so_tree, sn, node_cmp );
431 op->o_tmpfree( sn, op->o_tmpmemctx );
434 so->so_vlv_target = so->so_nentries + 1;
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] );
440 tmp_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
443 tmp_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
444 dir = TAVL_DIR_RIGHT;
446 for (i=0; tmp_node != cur_node;
447 tmp_node = tavl_next( tmp_node, dir ), i++);
448 so->so_vlv_target = i;
450 if ( bv.bv_val != vc->vc_value.bv_val )
451 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
455 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
459 for ( ; i<vc->vc_before; i++ ) {
460 tmp_node = tavl_next( cur_node, TAVL_DIR_LEFT );
461 if ( !tmp_node ) break;
464 j = i + vc->vc_after + 1;
466 for ( i=0; i<j; i++ ) {
467 sort_node *sn = cur_node->avl_data;
469 op->o_bd = select_backend( &sn->sn_dn, 0 );
471 rc = be_entry_get_rw( op, &sn->sn_dn, NULL, NULL, 0, &e );
473 if ( e && rc == LDAP_SUCCESS ) {
475 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
476 rs->sr_err = send_search_entry( op, rs );
477 if ( rs->sr_err == LDAP_UNAVAILABLE )
480 cur_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
481 if ( !cur_node ) break;
483 so->so_vlv_rc = LDAP_SUCCESS;
488 static void send_page( Operation *op, SlapReply *rs, sort_op *so )
490 Avlnode *cur_node = so->so_tree;
491 Avlnode *next_node = NULL;
492 BackendDB *be = op->o_bd;
497 while ( cur_node && rs->sr_nentries < so->so_page_size ) {
498 sort_node *sn = cur_node->avl_data;
500 next_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
502 op->o_bd = select_backend( &sn->sn_dn, 0 );
504 rc = be_entry_get_rw( op, &sn->sn_dn, NULL, NULL, 0, &e );
506 ch_free( cur_node->avl_data );
507 ber_memfree( cur_node );
509 cur_node = next_node;
512 if ( e && rc == LDAP_SUCCESS ) {
514 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
515 rs->sr_err = send_search_entry( op, rs );
516 if ( rs->sr_err == LDAP_UNAVAILABLE )
521 /* Set the first entry to send for the next page */
522 so->so_tree = next_node;
527 static void send_entry(
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>"));
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.
540 if ( (op->o_ctrlflag[sss_cid] != SLAP_CONTROL_CRITICAL) ||
541 (rs->sr_err == LDAP_SUCCESS) )
543 if ( so->so_vlv > SLAP_CONTROL_IGNORED ) {
544 send_list( op, rs, so );
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;
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.
555 so->so_page_size = so->so_nentries;
558 send_page( op, rs, so );
563 static void send_result(
568 LDAPControl *ctrls[3];
571 rc = pack_sss_response_control( op, rs, ctrls );
572 if ( rc == LDAP_SUCCESS ) {
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 );
580 if ( rc == LDAP_SUCCESS )
585 if ( ctrls[0] != NULL )
586 slap_add_ctrls( op, rs, ctrls );
587 send_ldap_result( op, rs );
589 if ( so->so_tree == NULL ) {
590 /* Search finished, so clean up */
591 free_sort_op( op->o_conn, so );
595 static int sssvlv_op_response(
599 sort_ctrl *sc = op->o_controls[sss_cid];
600 sort_op *so = op->o_callback->sc_private;
602 if ( rs->sr_type == REP_SEARCH ) {
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);
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 );
619 if ( a->a_numvals > 1 ) {
620 bv = select_value( a, &sc->sc_keys[i] );
624 sn->sn_vals[i] = *bv;
625 len += bv->bv_len + 1;
627 BER_BVZERO( &sn->sn_vals[i] );
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));
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;
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;
653 sn->sn_conn = op->o_conn->c_conn_idx;
655 /* Insert into the AVL tree */
656 tavl_insert(&(so->so_tree), sn, node_insert, avl_dup_error);
660 /* Collected the keys so that they can be sorted. Thus, stop
661 * the entry from propagating.
663 rs->sr_err = LDAP_SUCCESS;
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.
670 if ( op->o_callback->sc_response == sssvlv_op_response ) {
671 op->o_callback = op->o_callback->sc_next;
674 send_entry( op, rs, so );
675 send_result( op, rs, so );
681 static int sssvlv_op_search(
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;
690 sort_ctrl *sc = op->o_controls[sss_cid];
691 PagedResultsState *ps;
694 if ( op->o_ctrlflag[sss_cid] <= SLAP_CONTROL_IGNORED ) {
695 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
696 LDAPControl *ctrls[2];
698 so2.so_vlv_target = 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 ) {
704 slap_add_ctrls( op, rs, ctrls );
706 rs->sr_err = LDAP_VLV_ERROR;
707 rs->sr_text = "Sort control is required with VLV";
710 /* Not server side sort so just continue */
711 return SLAP_CB_CONTINUE;
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]);
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;
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;
731 rs->sr_text = "VLV incompatible with PagedResults";
732 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
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? */
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 ) {
748 } else if ( !ps->ps_size ) {
749 /* Abandoning current request */
752 rs->sr_err = LDAP_SUCCESS;
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 */
760 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
762 /* Are there too many running overall? */
763 } else if ( si->svi_num >= si->svi_max ) {
766 /* OK, this connection now has a sort running */
768 sort_conns[op->o_conn->c_conn_idx] = &so2;
770 ldap_pvt_thread_mutex_unlock( &sort_conns_mutex );
772 /* are we continuing a VLV search? */
773 if ( vc && vc->vc_context ) {
775 send_list( op, rs, so );
776 send_result( op, rs, so );
778 /* are we continuing a paged search? */
779 } else if ( ps && ps->ps_cookie ) {
781 send_page( op, rs, so );
782 send_result( op, rs, so );
785 slap_callback *cb = op->o_tmpalloc( sizeof(slap_callback),
787 /* Install serversort response callback to handle a new search */
789 so = ch_malloc( sizeof(sort_op));
791 so = op->o_tmpalloc( sizeof(sort_op), op->o_tmpmemctx );
793 sort_conns[op->o_conn->c_conn_idx] = so;
795 cb->sc_cleanup = NULL;
796 cb->sc_response = sssvlv_op_response;
797 cb->sc_next = op->o_callback;
804 so->so_paged = op->o_pagedresults;
805 so->so_page_size = ps->ps_size;
806 op->o_pagedresults = SLAP_CONTROL_IGNORED;
809 so->so_page_size = 0;
811 so->so_vlv = op->o_ctrlflag[vlv_cid];
813 so->so_vcontext = (unsigned long)so;
819 if ( so && !so->so_nentries ) {
820 free_sort_op( op->o_conn, so );
822 rs->sr_text = "Other sort requests already in progress";
823 rs->sr_err = LDAP_BUSY;
827 send_ldap_result( op, rs );
833 static int get_ordering_rule(
834 AttributeDescription *ad,
835 struct berval *matchrule,
837 MatchingRule **ordering )
841 if ( matchrule && matchrule->bv_val ) {
842 mr = mr_find( matchrule->bv_val );
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);
851 mr = ad->ad_type->sat_ordering;
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);
865 static int count_key(BerElement *ber)
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 ))
877 tag = ber_skip_tag( ber, &len );
878 ber_skip_data( ber, len );
886 static int build_key(
892 struct berval matchrule = BER_BVNULL;
893 ber_int_t reverse = 0;
896 MatchingRule *ordering = NULL;
897 AttributeDescription *ad = NULL;
900 if (( tag = ber_scanf( ber, "{" )) == LBER_ERROR ) {
901 rs->sr_text = "serverSort control: decoding error";
902 rs->sr_err = LDAP_PROTOCOL_ERROR;
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;
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;
919 tag = ber_peek_tag( ber, &len );
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;
930 if (( tag = ber_scanf( ber, "}" )) == LBER_ERROR ) {
931 rs->sr_text = "serverSort control: decoding error";
932 rs->sr_err = LDAP_PROTOCOL_ERROR;
936 if ( slap_bv2ad( &attr, &ad, &text ) != LDAP_SUCCESS ) {
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;
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 ) {
953 key->sk_ordering = ordering;
954 key->sk_direction = reverse ? -1 : 1;
959 static int sss_parseCtrl(
964 BerElementBuffer berbuf;
971 rs->sr_err = LDAP_PROTOCOL_ERROR;
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";
980 rs->sr_err = LDAP_SUCCESS;
982 if ( rs->sr_err != LDAP_SUCCESS )
985 op->o_ctrlflag[sss_cid] = ctrl->ldctl_iscritical ?
986 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
988 ber = (BerElement *)&berbuf;
989 ber_init2( ber, &ctrl->ldctl_value, 0 );
990 i = count_key( ber );
992 sc = op->o_tmpalloc( sizeof(sort_ctrl) +
993 i * sizeof(sort_key), op->o_tmpmemctx );
995 op->o_controls[sss_cid] = sc;
997 /* peel off initial sequence */
998 ber_scanf( ber, "{" );
1002 if ( build_key( ber, rs, &sc->sc_keys[i] ) != LDAP_SUCCESS )
1005 tag = ber_peek_tag( ber, &len );
1006 } while ( tag != LBER_DEFAULT );
1011 static int vlv_parseCtrl(
1016 BerElementBuffer berbuf;
1023 rs->sr_err = LDAP_PROTOCOL_ERROR;
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";
1033 if ( rs->sr_text != NULL )
1036 op->o_ctrlflag[vlv_cid] = ctrl->ldctl_iscritical ?
1037 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1039 ber = (BerElement *)&berbuf;
1040 ber_init2( ber, &ctrl->ldctl_value, 0 );
1042 rs->sr_err = LDAP_PROTOCOL_ERROR;
1044 tag = ber_scanf( ber, "{ii", &vc2.vc_before, &vc2.vc_after );
1045 if ( tag == LBER_ERROR ) {
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 )
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 ))
1062 tag = ber_peek_tag( ber, &len );
1063 if ( tag == LDAP_VLVCONTEXT_IDENTIFIER ) {
1065 tag = ber_scanf( ber, "m", &bv );
1066 if ( tag == LBER_ERROR || bv.bv_len != sizeof(vc2.vc_context))
1068 AC_MEMCPY( &vc2.vc_context, bv.bv_val, bv.bv_len );
1073 vc = op->o_tmpalloc( sizeof(vlv_ctrl), op->o_tmpmemctx );
1075 op->o_controls[vlv_cid] = vc;
1076 rs->sr_err = LDAP_SUCCESS;
1081 static int sssvlv_connection_destroy( BackendDB *be, Connection *conn )
1083 slap_overinst *on = (slap_overinst *)be->bd_info;
1085 if ( sort_conns[conn->c_conn_idx] )
1086 free_sort_op( conn, sort_conns[conn->c_conn_idx] );
1088 return LDAP_SUCCESS;
1091 static int sssvlv_db_open(
1095 int rc = overlay_register_control( be, LDAP_CONTROL_SORTREQUEST );
1096 if ( rc == LDAP_SUCCESS )
1097 rc = overlay_register_control( be, LDAP_CONTROL_VLVREQUEST );
1101 static int sssvlv_db_init(
1105 slap_overinst *on = (slap_overinst *)be->bd_info;
1108 si = (sssvlv_info *)ch_malloc(sizeof(sssvlv_info));
1109 on->on_bi.bi_private = si;
1113 si->svi_max_keys = 5;
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 );
1122 return LDAP_SUCCESS;
1125 static int sssvlv_db_destroy(
1129 slap_overinst *on = (slap_overinst *)be->bd_info;
1130 sssvlv_info *si = (sssvlv_info *)on->on_bi.bi_private;
1134 on->on_bi.bi_private = NULL;
1136 return LDAP_SUCCESS;
1139 static slap_overinst sssvlv;
1141 int sssvlv_initialize()
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;
1152 rc = register_supported_control2( LDAP_CONTROL_SORTREQUEST,
1159 if ( rc == LDAP_SUCCESS ) {
1160 rc = register_supported_control2( LDAP_CONTROL_VLVREQUEST,
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" );
1175 fprintf( stderr, "Failed to register control %d\n", rc );
1181 #if SLAPD_OVER_SSSVLV == SLAPD_MOD_DYNAMIC
1182 int init_module( int argc, char *argv[])
1184 return sssvlv_initialize();
1188 #endif /* SLAPD_OVER_SSSVLV */