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] );
174 static int node_insert( const void *val1, const void *val2 )
176 /* Never return equal so that new entries are always inserted */
177 return node_cmp( val1, val2 ) < 0 ? -1 : 1;
180 static int pack_vlv_response_control(
184 LDAPControl **ctrlsp )
187 BerElementBuffer berbuf;
188 BerElement *ber = (BerElement *)&berbuf;
189 PagedResultsCookie resp_cookie;
190 struct berval cookie, bv;
193 ber_init2( ber, NULL, LBER_USE_DER );
194 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
196 rc = ber_printf( ber, "{iii", so->so_vlv_target, so->so_nentries,
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 );
206 rc = ber_printf( ber, "}" );
210 rc = ber_flatten2( ber, &bv, 0 );
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 );
224 rs->sr_err = LDAP_OTHER;
232 static int pack_pagedresult_response_control(
236 LDAPControl **ctrlsp )
239 BerElementBuffer berbuf;
240 BerElement *ber = (BerElement *)&berbuf;
241 PagedResultsCookie resp_cookie;
242 struct berval cookie, bv;
245 ber_init2( ber, NULL, LBER_USE_DER );
246 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
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;
253 resp_cookie = ( PagedResultsCookie )0;
254 BER_BVZERO( &cookie );
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
262 rc = ber_printf( ber, "{iO}", so->so_nentries, &cookie );
264 rc = ber_flatten2( ber, &bv, 0 );
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 );
278 rs->sr_err = LDAP_OTHER;
286 static int pack_sss_response_control(
289 LDAPControl **ctrlsp )
292 BerElementBuffer berbuf;
293 BerElement *ber = (BerElement *)&berbuf;
297 ber_init2( ber, NULL, LBER_USE_DER );
298 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
300 /* Pack error code */
301 rc = ber_printf(ber, "{e}", rs->sr_err);
304 rc = ber_flatten2( ber, &bv, 0 );
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 );
317 rs->sr_err = LDAP_OTHER;
325 static void free_sort_op( Connection *conn, sort_op *so )
328 tavl_free( so->so_tree, ch_free );
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 );
340 static int send_list(
345 Avlnode *cur_node, *tmp_node;
346 vlv_ctrl *vc = op->o_controls[vlv_cid];
350 LDAPControl *ctrls[2];
352 /* FIXME: it may be better to just flatten the tree into
353 * an array before doing all of this...
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;
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 )
372 target = so->so_nentries * vc->vc_offset / vc->vc_count;
374 if ( vc->vc_offset > so->so_nentries ) {
376 so->so_vlv_rc = LDAP_VLV_RANGE_ERROR;
377 pack_vlv_response_control( op, rs, so, ctrls );
379 slap_add_ctrls( op, rs, ctrls );
380 rs->sr_err = LDAP_VLV_ERROR;
383 target = vc->vc_offset;
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;
391 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
393 target = so->so_nentries - target;
395 for ( i=0; i<target; i++ )
396 cur_node = tavl_next( cur_node, dir );
399 /* we're looking for a specific value */
400 sort_ctrl *sc = so->so_ctrl;
401 MatchingRule *mr = sc->sc_keys[0].sk_ordering;
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 );
409 so->so_vlv_rc = LDAP_INAPPROPRIATE_MATCHING;
410 pack_vlv_response_control( op, rs, so, ctrls );
412 slap_add_ctrls( op, rs, ctrls );
413 rs->sr_err = LDAP_VLV_ERROR;
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;
425 for (i=1; i<sc->sc_nkeys; i++) {
426 BER_BVZERO( &sn->sn_vals[i] );
428 cur_node = tavl_find2( so->so_tree, sn, node_cmp );
429 op->o_tmpfree( sn, op->o_tmpmemctx );
432 so->so_vlv_target = so->so_nentries + 1;
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] );
438 tmp_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
441 tmp_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
442 dir = TAVL_DIR_RIGHT;
444 for (i=0; tmp_node != cur_node;
445 tmp_node = tavl_next( tmp_node, dir ), i++);
446 so->so_vlv_target = i;
448 if ( bv.bv_val != vc->vc_value.bv_val )
449 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
451 for ( i=0; i<vc->vc_before; i++ ) {
452 tmp_node = tavl_next( cur_node, TAVL_DIR_LEFT );
453 if ( !tmp_node ) break;
456 j = i + vc->vc_after + 1;
458 for ( i=0; i<j; i++ ) {
459 sort_node *sn = cur_node->avl_data;
461 op->o_bd = select_backend( &sn->sn_dn, 0 );
463 rc = be_entry_get_rw( op, &sn->sn_dn, NULL, NULL, 0, &e );
465 if ( e && rc == LDAP_SUCCESS ) {
467 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
468 rs->sr_err = send_search_entry( op, rs );
469 if ( rs->sr_err == LDAP_UNAVAILABLE )
472 cur_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
478 static void send_page( Operation *op, SlapReply *rs, sort_op *so )
480 Avlnode *cur_node = so->so_tree;
481 Avlnode *next_node = NULL;
482 BackendDB *be = op->o_bd;
487 while ( cur_node && rs->sr_nentries < so->so_page_size ) {
488 sort_node *sn = cur_node->avl_data;
490 next_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
492 op->o_bd = select_backend( &sn->sn_dn, 0 );
494 rc = be_entry_get_rw( op, &sn->sn_dn, NULL, NULL, 0, &e );
496 ch_free( cur_node->avl_data );
497 ber_memfree( cur_node );
499 cur_node = next_node;
502 if ( e && rc == LDAP_SUCCESS ) {
504 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
505 rs->sr_err = send_search_entry( op, rs );
506 if ( rs->sr_err == LDAP_UNAVAILABLE )
511 /* Set the first entry to send for the next page */
512 so->so_tree = next_node;
517 static void send_entry(
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>"));
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.
530 if ( (op->o_ctrlflag[sss_cid] != SLAP_CONTROL_CRITICAL) ||
531 (rs->sr_err == LDAP_SUCCESS) )
533 if ( so->so_vlv > SLAP_CONTROL_IGNORED ) {
534 send_list( op, rs, so );
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;
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.
545 so->so_page_size = so->so_nentries;
548 send_page( op, rs, so );
553 static void send_result(
558 LDAPControl *ctrls[3];
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 );
569 if ( ctrls[0] != NULL )
570 slap_add_ctrls( op, rs, ctrls );
571 send_ldap_result( op, rs );
573 if ( so->so_tree == NULL ) {
574 /* Search finished, so clean up */
575 free_sort_op( op->o_conn, so );
579 static int sssvlv_op_response(
583 sort_ctrl *sc = op->o_controls[sss_cid];
584 sort_op *so = op->o_callback->sc_private;
586 if ( rs->sr_type == REP_SEARCH ) {
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);
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 );
603 if ( a->a_numvals > 1 ) {
604 bv = select_value( a, &sc->sc_keys[i] );
608 sn->sn_vals[i] = *bv;
609 len += bv->bv_len + 1;
611 BER_BVZERO( &sn->sn_vals[i] );
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));
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;
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;
637 sn->sn_conn = op->o_conn->c_conn_idx;
639 /* Insert into the AVL tree */
640 tavl_insert(&(so->so_tree), sn, node_insert, avl_dup_error);
644 /* Collected the keys so that they can be sorted. Thus, stop
645 * the entry from propagating.
647 rs->sr_err = LDAP_SUCCESS;
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.
654 if ( op->o_callback->sc_response == sssvlv_op_response ) {
655 op->o_callback = op->o_callback->sc_next;
658 send_entry( op, rs, so );
659 send_result( op, rs, so );
665 static int sssvlv_op_search(
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;
674 sort_ctrl *sc = op->o_controls[sss_cid];
675 PagedResultsState *ps;
678 if ( op->o_ctrlflag[sss_cid] <= SLAP_CONTROL_IGNORED ) {
679 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
680 LDAPControl *ctrls[2];
682 so2.so_vlv_target = 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 ) {
688 slap_add_ctrls( op, rs, ctrls );
690 rs->sr_err = LDAP_VLV_ERROR;
691 rs->sr_text = "Sort control is required with VLV";
694 /* Not server side sort so just continue */
695 return SLAP_CB_CONTINUE;
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]);
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;
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;
715 rs->sr_text = "VLV incompatible with PagedResults";
716 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
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? */
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 ) {
732 } else if ( !ps->ps_size ) {
733 /* Abandoning current request */
736 rs->sr_err = LDAP_SUCCESS;
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 */
744 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
746 /* Are there too many running overall? */
747 } else if ( si->svi_num >= si->svi_max ) {
750 /* OK, this connection now has a sort running */
752 sort_conns[op->o_conn->c_conn_idx] = &so2;
754 ldap_pvt_thread_mutex_unlock( &sort_conns_mutex );
756 /* are we continuing a VLV search? */
757 if ( vc && vc->vc_context ) {
759 send_list( op, rs, so );
760 send_result( op, rs, so );
762 /* are we continuing a paged search? */
763 } else if ( ps && ps->ps_cookie ) {
765 send_page( op, rs, so );
766 send_result( op, rs, so );
769 slap_callback *cb = op->o_tmpalloc( sizeof(slap_callback),
771 /* Install serversort response callback to handle a new search */
773 so = ch_malloc( sizeof(sort_op));
775 so = op->o_tmpalloc( sizeof(sort_op), op->o_tmpmemctx );
777 sort_conns[op->o_conn->c_conn_idx] = so;
779 cb->sc_cleanup = NULL;
780 cb->sc_response = sssvlv_op_response;
781 cb->sc_next = op->o_callback;
788 so->so_paged = op->o_pagedresults;
789 so->so_page_size = ps->ps_size;
790 op->o_pagedresults = SLAP_CONTROL_IGNORED;
793 so->so_page_size = 0;
795 so->so_vlv = op->o_ctrlflag[vlv_cid];
797 so->so_vcontext = (unsigned long)so;
803 if ( so && !so->so_nentries ) {
804 free_sort_op( op->o_conn, so );
806 rs->sr_text = "Other sort requests already in progress";
807 rs->sr_err = LDAP_BUSY;
811 send_ldap_result( op, rs );
817 static int get_ordering_rule(
818 AttributeDescription *ad,
819 struct berval *matchrule,
821 MatchingRule **ordering )
825 if ( matchrule && matchrule->bv_val ) {
826 mr = mr_find( matchrule->bv_val );
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);
835 mr = ad->ad_type->sat_ordering;
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);
849 static int count_key(BerElement *ber)
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 ))
861 tag = ber_skip_tag( ber, &len );
862 ber_skip_data( ber, len );
870 static int build_key(
876 struct berval matchrule = BER_BVNULL;
877 ber_int_t reverse = 0;
880 MatchingRule *ordering = NULL;
881 AttributeDescription *ad = NULL;
884 if (( tag = ber_scanf( ber, "{" )) == LBER_ERROR ) {
885 rs->sr_text = "serverSort control: decoding error";
886 rs->sr_err = LDAP_PROTOCOL_ERROR;
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;
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;
903 tag = ber_peek_tag( ber, &len );
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;
914 if (( tag = ber_scanf( ber, "}" )) == LBER_ERROR ) {
915 rs->sr_text = "serverSort control: decoding error";
916 rs->sr_err = LDAP_PROTOCOL_ERROR;
920 if ( slap_bv2ad( &attr, &ad, &text ) != LDAP_SUCCESS ) {
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;
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 ) {
937 key->sk_ordering = ordering;
938 key->sk_direction = reverse ? -1 : 1;
943 static int sss_parseCtrl(
948 BerElementBuffer berbuf;
955 rs->sr_err = LDAP_PROTOCOL_ERROR;
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";
964 rs->sr_err = LDAP_SUCCESS;
966 if ( rs->sr_err != LDAP_SUCCESS )
969 op->o_ctrlflag[sss_cid] = ctrl->ldctl_iscritical ?
970 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
972 ber = (BerElement *)&berbuf;
973 ber_init2( ber, &ctrl->ldctl_value, 0 );
974 i = count_key( ber );
976 sc = op->o_tmpalloc( sizeof(sort_ctrl) +
977 i * sizeof(sort_key), op->o_tmpmemctx );
979 op->o_controls[sss_cid] = sc;
981 /* peel off initial sequence */
982 ber_scanf( ber, "{" );
986 if ( build_key( ber, rs, &sc->sc_keys[i] ) != LDAP_SUCCESS )
989 tag = ber_peek_tag( ber, &len );
990 } while ( tag != LBER_DEFAULT );
995 static int vlv_parseCtrl(
1000 BerElementBuffer berbuf;
1007 rs->sr_err = LDAP_PROTOCOL_ERROR;
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";
1017 if ( rs->sr_text != NULL )
1020 op->o_ctrlflag[vlv_cid] = ctrl->ldctl_iscritical ?
1021 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1023 ber = (BerElement *)&berbuf;
1024 ber_init2( ber, &ctrl->ldctl_value, 0 );
1026 rs->sr_err = LDAP_PROTOCOL_ERROR;
1028 tag = ber_scanf( ber, "{ii", &vc2.vc_before, &vc2.vc_after );
1029 if ( tag == LBER_ERROR ) {
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 )
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 ))
1046 tag = ber_peek_tag( ber, &len );
1047 if ( tag == LDAP_VLVCONTEXT_IDENTIFIER ) {
1049 tag = ber_scanf( ber, "m", &bv );
1050 if ( tag == LBER_ERROR || bv.bv_len != sizeof(vc2.vc_context))
1052 AC_MEMCPY( &vc2.vc_context, bv.bv_val, bv.bv_len );
1057 vc = op->o_tmpalloc( sizeof(vlv_ctrl), op->o_tmpmemctx );
1059 op->o_controls[vlv_cid] = vc;
1060 rs->sr_err = LDAP_SUCCESS;
1065 static int sssvlv_connection_destroy( BackendDB *be, Connection *conn )
1067 slap_overinst *on = (slap_overinst *)be->bd_info;
1069 if ( sort_conns[conn->c_conn_idx] )
1070 free_sort_op( conn, sort_conns[conn->c_conn_idx] );
1072 return LDAP_SUCCESS;
1075 static int sssvlv_db_open(
1079 int rc = overlay_register_control( be, LDAP_CONTROL_SORTREQUEST );
1080 if ( rc == LDAP_SUCCESS )
1081 rc = overlay_register_control( be, LDAP_CONTROL_VLVREQUEST );
1085 static int sssvlv_db_init(
1089 slap_overinst *on = (slap_overinst *)be->bd_info;
1092 si = (sssvlv_info *)ch_malloc(sizeof(sssvlv_info));
1093 on->on_bi.bi_private = si;
1097 si->svi_max_keys = 5;
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 );
1106 return LDAP_SUCCESS;
1109 static int sssvlv_db_destroy(
1113 slap_overinst *on = (slap_overinst *)be->bd_info;
1114 sssvlv_info *si = (sssvlv_info *)on->on_bi.bi_private;
1118 on->on_bi.bi_private = NULL;
1120 return LDAP_SUCCESS;
1123 static slap_overinst sssvlv;
1125 int sssvlv_initialize()
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;
1136 rc = register_supported_control2( LDAP_CONTROL_SORTREQUEST,
1143 if ( rc == LDAP_SUCCESS ) {
1144 rc = register_supported_control2( LDAP_CONTROL_VLVREQUEST,
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" );
1159 fprintf( stderr, "Failed to register control %d\n", rc );
1165 #if SLAPD_OVER_SSSVLV == SLAPD_MOD_DYNAMIC
1166 int init_module( int argc, char *argv[])
1168 return sssvlv_initialize();
1172 #endif /* SLAPD_OVER_SSSVLV */