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 #ifdef SLAPD_OVER_SSSVLV
28 #include <ac/string.h>
36 /* RFC2891: Server Side Sorting
37 * RFC2696: Paged Results
39 #ifndef LDAP_MATCHRULE_IDENTIFIER
40 #define LDAP_MATCHRULE_IDENTIFIER 0x80L
41 #define LDAP_REVERSEORDER_IDENTIFIER 0x81L
42 #define LDAP_ATTRTYPES_IDENTIFIER 0x80L
45 /* draft-ietf-ldapext-ldapv3-vlv-09.txt: Virtual List Views
47 #ifndef LDAP_VLVBYINDEX_IDENTIFIER
48 #define LDAP_VLVBYINDEX_IDENTIFIER 0xa0L
49 #define LDAP_VLVBYVALUE_IDENTIFIER 0x81L
50 #define LDAP_VLVCONTEXT_IDENTIFIER 0x04L
52 #define LDAP_VLV_SSS_MISSING 0x4C
53 #define LDAP_VLV_RANGE_ERROR 0x4D
56 #define SAFESTR(macro_str, macro_def) ((macro_str) ? (macro_str) : (macro_def))
58 typedef struct vlv_ctrl {
63 struct berval vc_value;
64 unsigned long vc_context;
67 typedef struct sort_key
69 AttributeDescription *sk_ad;
70 MatchingRule *sk_ordering;
71 int sk_direction; /* 1=normal, -1=reverse */
74 typedef struct sort_ctrl {
80 typedef struct sort_node
84 struct berval *sn_vals;
87 typedef struct sssvlv_info
89 int svi_max; /* max concurrent sorts */
90 int svi_num; /* current # sorts */
91 int svi_max_keys; /* max sort keys per request */
94 typedef struct sort_op
105 unsigned long so_vcontext;
108 /* There is only one conn table for all overlay instances */
109 static sort_op **sort_conns;
110 static ldap_pvt_thread_mutex_t sort_conns_mutex;
111 static const char *debug_header = "sssvlv";
116 /* RFC 2981 Section 2.2
117 * If a sort key is a multi-valued attribute, and an entry happens to
118 * have multiple values for that attribute and no other controls are
119 * present that affect the sorting order, then the server SHOULD use the
120 * least value (according to the ORDERING rule for that attribute).
122 static struct berval* select_value(
126 struct berval* ber1, *ber2;
127 MatchingRule *mr = key->sk_ordering;
130 ber1 = &(attr->a_nvals[0]);
132 for ( i = 1; i < attr->a_numvals; i++,ber2++ ) {
133 mr->smr_match( &cmp, 0, mr->smr_syntax, mr, ber1, ber2 );
139 Debug(LDAP_DEBUG_TRACE, "%s: value selected for compare: %s\n",
141 SAFESTR(ber1->bv_val, "<Empty>"),
147 static int node_cmp( const void* val1, const void* val2 )
149 sort_node *sn1 = (sort_node *)val1;
150 sort_node *sn2 = (sort_node *)val2;
151 sort_ctrl *sc = sort_conns[sn1->sn_conn]->so_ctrl;
155 for ( i=0; cmp == 0 && i<sc->sc_nkeys; i++ ) {
156 if ( BER_BVISNULL( &sn1->sn_vals[i] )) {
157 if ( BER_BVISNULL( &sn2->sn_vals[i] ))
160 cmp = sc->sc_keys[i].sk_direction;
161 } else if ( BER_BVISNULL( &sn2->sn_vals[i] )) {
162 cmp = sc->sc_keys[i].sk_direction * -1;
164 mr = sc->sc_keys[i].sk_ordering;
165 mr->smr_match( &cmp, 0, mr->smr_syntax, mr,
166 &sn1->sn_vals[i], &sn2->sn_vals[i] );
168 cmp *= sc->sc_keys[i].sk_direction;
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 + 1;
395 for ( i=1; 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 );
453 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
457 for ( ; i<vc->vc_before; i++ ) {
458 tmp_node = tavl_next( cur_node, TAVL_DIR_LEFT );
459 if ( !tmp_node ) break;
462 j = i + vc->vc_after + 1;
464 for ( i=0; i<j; i++ ) {
465 sort_node *sn = cur_node->avl_data;
467 op->o_bd = select_backend( &sn->sn_dn, 0 );
469 rc = be_entry_get_rw( op, &sn->sn_dn, NULL, NULL, 0, &e );
471 if ( e && rc == LDAP_SUCCESS ) {
473 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
474 rs->sr_err = send_search_entry( op, rs );
475 if ( rs->sr_err == LDAP_UNAVAILABLE )
478 cur_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
479 if ( !cur_node ) break;
481 so->so_vlv_rc = LDAP_SUCCESS;
486 static void send_page( Operation *op, SlapReply *rs, sort_op *so )
488 Avlnode *cur_node = so->so_tree;
489 Avlnode *next_node = NULL;
490 BackendDB *be = op->o_bd;
495 while ( cur_node && rs->sr_nentries < so->so_page_size ) {
496 sort_node *sn = cur_node->avl_data;
498 next_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
500 op->o_bd = select_backend( &sn->sn_dn, 0 );
502 rc = be_entry_get_rw( op, &sn->sn_dn, NULL, NULL, 0, &e );
504 ch_free( cur_node->avl_data );
505 ber_memfree( cur_node );
507 cur_node = next_node;
510 if ( e && rc == LDAP_SUCCESS ) {
512 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
513 rs->sr_err = send_search_entry( op, rs );
514 if ( rs->sr_err == LDAP_UNAVAILABLE )
519 /* Set the first entry to send for the next page */
520 so->so_tree = next_node;
525 static void send_entry(
530 Debug(LDAP_DEBUG_TRACE,
531 "%s: response control: status=%d, text=%s\n",
532 debug_header, rs->sr_err, SAFESTR(rs->sr_text, "<None>"));
534 /* RFC 2891: If critical then send the entries iff they were
535 * succesfully sorted. If non-critical send all entries
536 * whether they were sorted or not.
538 if ( (op->o_ctrlflag[sss_cid] != SLAP_CONTROL_CRITICAL) ||
539 (rs->sr_err == LDAP_SUCCESS) )
541 if ( so->so_vlv > SLAP_CONTROL_IGNORED ) {
542 send_list( op, rs, so );
544 /* Get the first node to send */
545 Avlnode *start_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
546 so->so_tree = start_node;
548 if ( so->so_paged <= SLAP_CONTROL_IGNORED ) {
549 /* Not paged result search. Send all entries.
550 * Set the page size to the number of entries
551 * so that send_page() will send all entries.
553 so->so_page_size = so->so_nentries;
556 send_page( op, rs, so );
561 static void send_result(
566 LDAPControl *ctrls[3];
569 rc = pack_sss_response_control( op, rs, ctrls );
570 if ( rc == LDAP_SUCCESS ) {
573 if ( so->so_paged > SLAP_CONTROL_IGNORED ) {
574 rc = pack_pagedresult_response_control( op, rs, so, ctrls+1 );
575 } else if ( so->so_vlv > SLAP_CONTROL_IGNORED ) {
576 rc = pack_vlv_response_control( op, rs, so, ctrls+1 );
578 if ( rc == LDAP_SUCCESS )
583 if ( ctrls[0] != NULL )
584 slap_add_ctrls( op, rs, ctrls );
585 send_ldap_result( op, rs );
587 if ( so->so_tree == NULL ) {
588 /* Search finished, so clean up */
589 free_sort_op( op->o_conn, so );
593 static int sssvlv_op_response(
597 sort_ctrl *sc = op->o_controls[sss_cid];
598 sort_op *so = op->o_callback->sc_private;
600 if ( rs->sr_type == REP_SEARCH ) {
607 len = sizeof(sort_node) + sc->sc_nkeys * sizeof(struct berval) +
608 rs->sr_entry->e_nname.bv_len + 1;
609 sn = op->o_tmpalloc( len, op->o_tmpmemctx );
610 sn->sn_vals = (struct berval *)(sn+1);
612 /* Build tmp list of key values */
613 for ( i=0; i<sc->sc_nkeys; i++ ) {
614 Attribute *a = attr_find( rs->sr_entry->e_attrs,
615 sc->sc_keys[i].sk_ad );
617 if ( a->a_numvals > 1 ) {
618 bv = select_value( a, &sc->sc_keys[i] );
622 sn->sn_vals[i] = *bv;
623 len += bv->bv_len + 1;
625 BER_BVZERO( &sn->sn_vals[i] );
629 /* Now dup into regular memory */
630 sn2 = ch_malloc( len );
631 sn2->sn_vals = (struct berval *)(sn2+1);
632 AC_MEMCPY( sn2->sn_vals, sn->sn_vals,
633 sc->sc_nkeys * sizeof(struct berval));
636 ptr = (char *)(sn->sn_vals + sc->sc_nkeys);
637 sn->sn_dn.bv_val = ptr;
638 sn->sn_dn.bv_len = rs->sr_entry->e_nname.bv_len;
639 AC_MEMCPY( ptr, rs->sr_entry->e_nname.bv_val,
640 rs->sr_entry->e_nname.bv_len );
641 ptr += rs->sr_entry->e_nname.bv_len;
643 for ( i=0; i<sc->sc_nkeys; i++ ) {
644 if ( !BER_BVISNULL( &sn->sn_vals[i] )) {
645 AC_MEMCPY( ptr, sn->sn_vals[i].bv_val, sn->sn_vals[i].bv_len );
646 sn->sn_vals[i].bv_val = ptr;
647 ptr += sn->sn_vals[i].bv_len;
651 sn->sn_conn = op->o_conn->c_conn_idx;
653 /* Insert into the AVL tree */
654 tavl_insert(&(so->so_tree), sn, node_insert, avl_dup_error);
658 /* Collected the keys so that they can be sorted. Thus, stop
659 * the entry from propagating.
661 rs->sr_err = LDAP_SUCCESS;
663 else if ( rs->sr_type == REP_RESULT ) {
664 /* Remove serversort response callback.
665 * We don't want the entries that we are about to send to be
666 * processed by serversort response again.
668 if ( op->o_callback->sc_response == sssvlv_op_response ) {
669 op->o_callback = op->o_callback->sc_next;
672 send_entry( op, rs, so );
673 send_result( op, rs, so );
679 static int sssvlv_op_search(
683 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
684 sssvlv_info *si = on->on_bi.bi_private;
685 int rc = SLAP_CB_CONTINUE;
688 sort_ctrl *sc = op->o_controls[sss_cid];
689 PagedResultsState *ps;
692 if ( op->o_ctrlflag[sss_cid] <= SLAP_CONTROL_IGNORED ) {
693 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
694 LDAPControl *ctrls[2];
696 so2.so_vlv_target = 0;
698 so2.so_vlv_rc = LDAP_VLV_SSS_MISSING;
699 rc = pack_vlv_response_control( op, rs, &so2, ctrls );
700 if ( rc == LDAP_SUCCESS ) {
702 slap_add_ctrls( op, rs, ctrls );
704 rs->sr_err = LDAP_VLV_ERROR;
705 rs->sr_text = "Sort control is required with VLV";
708 /* Not server side sort so just continue */
709 return SLAP_CB_CONTINUE;
712 Debug(LDAP_DEBUG_TRACE,
713 "==> sssvlv_search: <%s> %s, control flag: %d\n",
714 op->o_req_dn.bv_val, op->ors_filterstr.bv_val,
715 op->o_ctrlflag[sss_cid]);
717 if ( sc->sc_nkeys > si->svi_max_keys ) {
718 rs->sr_text = "Too many sort keys";
719 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
723 ps = ( op->o_pagedresults > SLAP_CONTROL_IGNORED ) ?
724 (PagedResultsState*)(op->o_pagedresults_state) : NULL;
725 vc = op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ?
726 op->o_controls[vlv_cid] : NULL;
729 rs->sr_text = "VLV incompatible with PagedResults";
730 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
735 ldap_pvt_thread_mutex_lock( &sort_conns_mutex );
736 so = sort_conns[op->o_conn->c_conn_idx];
737 /* Is there already a sort running on this conn? */
739 /* Is it a continuation of a VLV search? */
740 if ( !vc || so->so_vlv <= SLAP_CONTROL_IGNORED ||
741 vc->vc_context != so->so_vcontext ) {
742 /* Is it a continuation of a paged search? */
743 if ( !ps || so->so_paged <= SLAP_CONTROL_IGNORED ||
744 op->o_conn->c_pagedresults_state.ps_cookie != ps->ps_cookie ) {
746 } else if ( !ps->ps_size ) {
747 /* Abandoning current request */
750 rs->sr_err = LDAP_SUCCESS;
753 if (( vc && so->so_paged > SLAP_CONTROL_IGNORED ) ||
754 ( ps && so->so_vlv > SLAP_CONTROL_IGNORED )) {
755 /* changed from paged to vlv or vice versa, abandon */
758 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
760 /* Are there too many running overall? */
761 } else if ( si->svi_num >= si->svi_max ) {
764 /* OK, this connection now has a sort running */
766 sort_conns[op->o_conn->c_conn_idx] = &so2;
768 ldap_pvt_thread_mutex_unlock( &sort_conns_mutex );
770 /* are we continuing a VLV search? */
771 if ( vc && vc->vc_context ) {
773 send_list( op, rs, so );
774 send_result( op, rs, so );
776 /* are we continuing a paged search? */
777 } else if ( ps && ps->ps_cookie ) {
779 send_page( op, rs, so );
780 send_result( op, rs, so );
783 slap_callback *cb = op->o_tmpalloc( sizeof(slap_callback),
785 /* Install serversort response callback to handle a new search */
787 so = ch_malloc( sizeof(sort_op));
789 so = op->o_tmpalloc( sizeof(sort_op), op->o_tmpmemctx );
791 sort_conns[op->o_conn->c_conn_idx] = so;
793 cb->sc_cleanup = NULL;
794 cb->sc_response = sssvlv_op_response;
795 cb->sc_next = op->o_callback;
802 so->so_paged = op->o_pagedresults;
803 so->so_page_size = ps->ps_size;
804 op->o_pagedresults = SLAP_CONTROL_IGNORED;
807 so->so_page_size = 0;
809 so->so_vlv = op->o_ctrlflag[vlv_cid];
811 so->so_vcontext = (unsigned long)so;
817 if ( so && !so->so_nentries ) {
818 free_sort_op( op->o_conn, so );
820 rs->sr_text = "Other sort requests already in progress";
821 rs->sr_err = LDAP_BUSY;
825 send_ldap_result( op, rs );
831 static int get_ordering_rule(
832 AttributeDescription *ad,
833 struct berval *matchrule,
835 MatchingRule **ordering )
839 if ( matchrule && matchrule->bv_val ) {
840 mr = mr_find( matchrule->bv_val );
842 rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
843 rs->sr_text = "serverSort control: No ordering rule";
844 Debug(LDAP_DEBUG_TRACE, "%s: no ordering rule function for %s\n",
845 debug_header, matchrule->bv_val, 0);
849 mr = ad->ad_type->sat_ordering;
851 rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
852 rs->sr_text = "serverSort control: No ordering rule";
853 Debug(LDAP_DEBUG_TRACE,
854 "%s: no ordering rule specified and no default ordering rule for attribute %s\n",
855 debug_header, ad->ad_cname.bv_val, 0);
863 static int count_key(BerElement *ber)
870 /* Server Side Sort Control is a SEQUENCE of SEQUENCE */
871 for ( tag = ber_first_element( ber, &len, &end );
872 tag == LBER_SEQUENCE;
873 tag = ber_next_element( ber, &len, end ))
875 tag = ber_skip_tag( ber, &len );
876 ber_skip_data( ber, len );
884 static int build_key(
890 struct berval matchrule = BER_BVNULL;
891 ber_int_t reverse = 0;
894 MatchingRule *ordering = NULL;
895 AttributeDescription *ad = NULL;
898 if (( tag = ber_scanf( ber, "{" )) == LBER_ERROR ) {
899 rs->sr_text = "serverSort control: decoding error";
900 rs->sr_err = LDAP_PROTOCOL_ERROR;
904 if (( tag = ber_scanf( ber, "m", &attr )) == LBER_ERROR ) {
905 rs->sr_text = "serverSort control: attribute decoding error";
906 rs->sr_err = LDAP_PROTOCOL_ERROR;
910 tag = ber_peek_tag( ber, &len );
911 if ( tag == LDAP_MATCHRULE_IDENTIFIER ) {
912 if (( tag = ber_scanf( ber, "m", &matchrule )) == LBER_ERROR ) {
913 rs->sr_text = "serverSort control: matchrule decoding error";
914 rs->sr_err = LDAP_PROTOCOL_ERROR;
917 tag = ber_peek_tag( ber, &len );
920 if ( tag == LDAP_REVERSEORDER_IDENTIFIER ) {
921 if (( tag = ber_scanf( ber, "b", &reverse )) == LBER_ERROR ) {
922 rs->sr_text = "serverSort control: reverse decoding error";
923 rs->sr_err = LDAP_PROTOCOL_ERROR;
928 if (( tag = ber_scanf( ber, "}" )) == LBER_ERROR ) {
929 rs->sr_text = "serverSort control: decoding error";
930 rs->sr_err = LDAP_PROTOCOL_ERROR;
934 if ( slap_bv2ad( &attr, &ad, &text ) != LDAP_SUCCESS ) {
936 "serverSort control: Unrecognized attribute type in sort key";
937 Debug(LDAP_DEBUG_TRACE,
938 "%s: Unrecognized attribute type in sort key: %s\n",
939 debug_header, SAFESTR(attr.bv_val, "<None>"), 0);
940 rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
944 /* get_ordering_rule will set sr_err and sr_text */
945 get_ordering_rule( ad, &matchrule, rs, &ordering );
946 if ( rs->sr_err != LDAP_SUCCESS ) {
951 key->sk_ordering = ordering;
952 key->sk_direction = reverse ? -1 : 1;
957 static int sss_parseCtrl(
962 BerElementBuffer berbuf;
969 rs->sr_err = LDAP_PROTOCOL_ERROR;
971 if ( op->o_ctrlflag[sss_cid] > SLAP_CONTROL_IGNORED ) {
972 rs->sr_text = "sorted results control specified multiple times";
973 } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
974 rs->sr_text = "sorted results control value is absent";
975 } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
976 rs->sr_text = "sorted results control value is empty";
978 rs->sr_err = LDAP_SUCCESS;
980 if ( rs->sr_err != LDAP_SUCCESS )
983 op->o_ctrlflag[sss_cid] = ctrl->ldctl_iscritical ?
984 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
986 ber = (BerElement *)&berbuf;
987 ber_init2( ber, &ctrl->ldctl_value, 0 );
988 i = count_key( ber );
990 sc = op->o_tmpalloc( sizeof(sort_ctrl) +
991 i * sizeof(sort_key), op->o_tmpmemctx );
993 op->o_controls[sss_cid] = sc;
995 /* peel off initial sequence */
996 ber_scanf( ber, "{" );
1000 if ( build_key( ber, rs, &sc->sc_keys[i] ) != LDAP_SUCCESS )
1003 tag = ber_peek_tag( ber, &len );
1004 } while ( tag != LBER_DEFAULT );
1009 static int vlv_parseCtrl(
1014 BerElementBuffer berbuf;
1021 rs->sr_err = LDAP_PROTOCOL_ERROR;
1024 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
1025 rs->sr_text = "vlv control specified multiple times";
1026 } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1027 rs->sr_text = "vlv control value is absent";
1028 } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1029 rs->sr_text = "vlv control value is empty";
1031 if ( rs->sr_text != NULL )
1034 op->o_ctrlflag[vlv_cid] = ctrl->ldctl_iscritical ?
1035 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1037 ber = (BerElement *)&berbuf;
1038 ber_init2( ber, &ctrl->ldctl_value, 0 );
1040 rs->sr_err = LDAP_PROTOCOL_ERROR;
1042 tag = ber_scanf( ber, "{ii", &vc2.vc_before, &vc2.vc_after );
1043 if ( tag == LBER_ERROR ) {
1047 tag = ber_peek_tag( ber, &len );
1048 if ( tag == LDAP_VLVBYINDEX_IDENTIFIER ) {
1049 tag = ber_scanf( ber, "{ii}", &vc2.vc_offset, &vc2.vc_count );
1050 if ( tag == LBER_ERROR )
1052 BER_BVZERO( &vc2.vc_value );
1053 } else if ( tag == LDAP_VLVBYVALUE_IDENTIFIER ) {
1054 tag = ber_scanf( ber, "m", &vc2.vc_value );
1055 if ( tag == LBER_ERROR || BER_BVISNULL( &vc2.vc_value ))
1060 tag = ber_peek_tag( ber, &len );
1061 if ( tag == LDAP_VLVCONTEXT_IDENTIFIER ) {
1063 tag = ber_scanf( ber, "m", &bv );
1064 if ( tag == LBER_ERROR || bv.bv_len != sizeof(vc2.vc_context))
1066 AC_MEMCPY( &vc2.vc_context, bv.bv_val, bv.bv_len );
1071 vc = op->o_tmpalloc( sizeof(vlv_ctrl), op->o_tmpmemctx );
1073 op->o_controls[vlv_cid] = vc;
1074 rs->sr_err = LDAP_SUCCESS;
1079 static int sssvlv_connection_destroy( BackendDB *be, Connection *conn )
1081 slap_overinst *on = (slap_overinst *)be->bd_info;
1083 if ( sort_conns[conn->c_conn_idx] )
1084 free_sort_op( conn, sort_conns[conn->c_conn_idx] );
1086 return LDAP_SUCCESS;
1089 static int sssvlv_db_open(
1093 int rc = overlay_register_control( be, LDAP_CONTROL_SORTREQUEST );
1094 if ( rc == LDAP_SUCCESS )
1095 rc = overlay_register_control( be, LDAP_CONTROL_VLVREQUEST );
1099 static int sssvlv_db_init(
1103 slap_overinst *on = (slap_overinst *)be->bd_info;
1106 si = (sssvlv_info *)ch_malloc(sizeof(sssvlv_info));
1107 on->on_bi.bi_private = si;
1111 si->svi_max_keys = 5;
1113 if ( dtblsize && !sort_conns ) {
1114 ldap_pvt_thread_mutex_init( &sort_conns_mutex );
1115 /* accommodate for c_conn_idx == -1 */
1116 sort_conns = ch_calloc( sizeof(sort_op *), dtblsize + 1 );
1120 return LDAP_SUCCESS;
1123 static int sssvlv_db_destroy(
1127 slap_overinst *on = (slap_overinst *)be->bd_info;
1128 sssvlv_info *si = (sssvlv_info *)on->on_bi.bi_private;
1132 on->on_bi.bi_private = NULL;
1134 return LDAP_SUCCESS;
1137 static slap_overinst sssvlv;
1139 int sssvlv_initialize()
1143 sssvlv.on_bi.bi_type = "sssvlv";
1144 sssvlv.on_bi.bi_db_init = sssvlv_db_init;
1145 sssvlv.on_bi.bi_db_destroy = sssvlv_db_destroy;
1146 sssvlv.on_bi.bi_db_open = sssvlv_db_open;
1147 sssvlv.on_bi.bi_connection_destroy = sssvlv_connection_destroy;
1148 sssvlv.on_bi.bi_op_search = sssvlv_op_search;
1150 rc = register_supported_control2( LDAP_CONTROL_SORTREQUEST,
1157 if ( rc == LDAP_SUCCESS ) {
1158 rc = register_supported_control2( LDAP_CONTROL_VLVREQUEST,
1166 if ( rc == LDAP_SUCCESS ) {
1167 rc = overlay_register( &sssvlv );
1168 if ( rc != LDAP_SUCCESS ) {
1169 fprintf( stderr, "Failed to register server side sort overlay\n" );
1173 fprintf( stderr, "Failed to register control %d\n", rc );
1179 #if SLAPD_OVER_SSSVLV == SLAPD_MOD_DYNAMIC
1180 int init_module( int argc, char *argv[])
1182 return sssvlv_initialize();
1186 #endif /* SLAPD_OVER_SSSVLV */