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>
37 /* RFC2891: Server Side Sorting
38 * RFC2696: Paged Results
40 #ifndef LDAP_MATCHRULE_IDENTIFIER
41 #define LDAP_MATCHRULE_IDENTIFIER 0x80L
42 #define LDAP_REVERSEORDER_IDENTIFIER 0x81L
43 #define LDAP_ATTRTYPES_IDENTIFIER 0x80L
46 /* draft-ietf-ldapext-ldapv3-vlv-09.txt: Virtual List Views
48 #ifndef LDAP_VLVBYINDEX_IDENTIFIER
49 #define LDAP_VLVBYINDEX_IDENTIFIER 0xa0L
50 #define LDAP_VLVBYVALUE_IDENTIFIER 0x81L
51 #define LDAP_VLVCONTEXT_IDENTIFIER 0x04L
53 #define LDAP_VLV_SSS_MISSING 0x4C
54 #define LDAP_VLV_RANGE_ERROR 0x4D
57 #define SAFESTR(macro_str, macro_def) ((macro_str) ? (macro_str) : (macro_def))
59 #define SSSVLV_DEFAULT_MAX_KEYS 5
61 typedef struct vlv_ctrl {
66 struct berval vc_value;
67 unsigned long vc_context;
70 typedef struct sort_key
72 AttributeDescription *sk_ad;
73 MatchingRule *sk_ordering;
74 int sk_direction; /* 1=normal, -1=reverse */
77 typedef struct sort_ctrl {
83 typedef struct sort_node
87 struct berval *sn_vals;
90 typedef struct sssvlv_info
92 int svi_max; /* max concurrent sorts */
93 int svi_num; /* current # sorts */
94 int svi_max_keys; /* max sort keys per request */
97 typedef struct sort_op
101 sssvlv_info *so_info;
108 unsigned long so_vcontext;
111 /* There is only one conn table for all overlay instances */
112 static sort_op **sort_conns;
113 static ldap_pvt_thread_mutex_t sort_conns_mutex;
114 static const char *debug_header = "sssvlv";
119 /* RFC 2981 Section 2.2
120 * If a sort key is a multi-valued attribute, and an entry happens to
121 * have multiple values for that attribute and no other controls are
122 * present that affect the sorting order, then the server SHOULD use the
123 * least value (according to the ORDERING rule for that attribute).
125 static struct berval* select_value(
129 struct berval* ber1, *ber2;
130 MatchingRule *mr = key->sk_ordering;
133 ber1 = &(attr->a_nvals[0]);
135 for ( i = 1; i < attr->a_numvals; i++,ber2++ ) {
136 mr->smr_match( &cmp, 0, mr->smr_syntax, mr, ber1, ber2 );
142 Debug(LDAP_DEBUG_TRACE, "%s: value selected for compare: %s\n",
144 SAFESTR(ber1->bv_val, "<Empty>"),
150 static int node_cmp( const void* val1, const void* val2 )
152 sort_node *sn1 = (sort_node *)val1;
153 sort_node *sn2 = (sort_node *)val2;
154 sort_ctrl *sc = sort_conns[sn1->sn_conn]->so_ctrl;
158 for ( i=0; cmp == 0 && i<sc->sc_nkeys; i++ ) {
159 if ( BER_BVISNULL( &sn1->sn_vals[i] )) {
160 if ( BER_BVISNULL( &sn2->sn_vals[i] ))
163 cmp = sc->sc_keys[i].sk_direction;
164 } else if ( BER_BVISNULL( &sn2->sn_vals[i] )) {
165 cmp = sc->sc_keys[i].sk_direction * -1;
167 mr = sc->sc_keys[i].sk_ordering;
168 mr->smr_match( &cmp, 0, mr->smr_syntax, mr,
169 &sn1->sn_vals[i], &sn2->sn_vals[i] );
171 cmp *= sc->sc_keys[i].sk_direction;
177 static int node_insert( const void *val1, const void *val2 )
179 /* Never return equal so that new entries are always inserted */
180 return node_cmp( val1, val2 ) < 0 ? -1 : 1;
183 static int pack_vlv_response_control(
187 LDAPControl **ctrlsp )
190 BerElementBuffer berbuf;
191 BerElement *ber = (BerElement *)&berbuf;
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 void 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_find3( so->so_tree, sn, node_cmp, &j );
431 /* didn't find >= match */
434 op->o_tmpfree( sn, op->o_tmpmemctx );
437 so->so_vlv_target = so->so_nentries + 1;
439 sort_node *sn = so->so_tree->avl_data;
440 /* start from the left or the right side? */
441 mr->smr_match( &i, 0, mr->smr_syntax, mr, &bv, &sn->sn_vals[0] );
443 tmp_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
446 tmp_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
447 dir = TAVL_DIR_RIGHT;
449 for (i=0; tmp_node != cur_node;
450 tmp_node = tavl_next( tmp_node, dir ), i++);
451 so->so_vlv_target = i;
453 if ( bv.bv_val != vc->vc_value.bv_val )
454 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
458 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
462 for ( ; i<vc->vc_before; i++ ) {
463 tmp_node = tavl_next( cur_node, TAVL_DIR_LEFT );
464 if ( !tmp_node ) break;
467 j = i + vc->vc_after + 1;
469 for ( i=0; i<j; i++ ) {
470 sort_node *sn = cur_node->avl_data;
472 op->o_bd = select_backend( &sn->sn_dn, 0 );
474 rc = be_entry_get_rw( op, &sn->sn_dn, NULL, NULL, 0, &e );
476 if ( e && rc == LDAP_SUCCESS ) {
478 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
479 rs->sr_err = send_search_entry( op, rs );
480 if ( rs->sr_err == LDAP_UNAVAILABLE )
483 cur_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
484 if ( !cur_node ) break;
486 so->so_vlv_rc = LDAP_SUCCESS;
491 static void send_page( Operation *op, SlapReply *rs, sort_op *so )
493 Avlnode *cur_node = so->so_tree;
494 Avlnode *next_node = NULL;
495 BackendDB *be = op->o_bd;
500 while ( cur_node && rs->sr_nentries < so->so_page_size ) {
501 sort_node *sn = cur_node->avl_data;
503 next_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
505 op->o_bd = select_backend( &sn->sn_dn, 0 );
507 rc = be_entry_get_rw( op, &sn->sn_dn, NULL, NULL, 0, &e );
509 ch_free( cur_node->avl_data );
510 ber_memfree( cur_node );
512 cur_node = next_node;
515 if ( e && rc == LDAP_SUCCESS ) {
517 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
518 rs->sr_err = send_search_entry( op, rs );
519 if ( rs->sr_err == LDAP_UNAVAILABLE )
524 /* Set the first entry to send for the next page */
525 so->so_tree = next_node;
530 static void send_entry(
535 Debug(LDAP_DEBUG_TRACE,
536 "%s: response control: status=%d, text=%s\n",
537 debug_header, rs->sr_err, SAFESTR(rs->sr_text, "<None>"));
542 /* RFC 2891: If critical then send the entries iff they were
543 * succesfully sorted. If non-critical send all entries
544 * whether they were sorted or not.
546 if ( (op->o_ctrlflag[sss_cid] != SLAP_CONTROL_CRITICAL) ||
547 (rs->sr_err == LDAP_SUCCESS) )
549 if ( so->so_vlv > SLAP_CONTROL_IGNORED ) {
550 send_list( op, rs, so );
552 /* Get the first node to send */
553 Avlnode *start_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
554 so->so_tree = start_node;
556 if ( so->so_paged <= SLAP_CONTROL_IGNORED ) {
557 /* Not paged result search. Send all entries.
558 * Set the page size to the number of entries
559 * so that send_page() will send all entries.
561 so->so_page_size = so->so_nentries;
564 send_page( op, rs, so );
569 static void send_result(
574 LDAPControl *ctrls[3];
577 rc = pack_sss_response_control( op, rs, ctrls );
578 if ( rc == LDAP_SUCCESS ) {
581 if ( so->so_paged > SLAP_CONTROL_IGNORED ) {
582 rc = pack_pagedresult_response_control( op, rs, so, ctrls+1 );
583 } else if ( so->so_vlv > SLAP_CONTROL_IGNORED ) {
584 rc = pack_vlv_response_control( op, rs, so, ctrls+1 );
586 if ( rc == LDAP_SUCCESS )
591 if ( ctrls[0] != NULL )
592 slap_add_ctrls( op, rs, ctrls );
593 send_ldap_result( op, rs );
595 if ( so->so_tree == NULL ) {
596 /* Search finished, so clean up */
597 free_sort_op( op->o_conn, so );
601 static int sssvlv_op_response(
605 sort_ctrl *sc = op->o_controls[sss_cid];
606 sort_op *so = op->o_callback->sc_private;
608 if ( rs->sr_type == REP_SEARCH ) {
615 len = sizeof(sort_node) + sc->sc_nkeys * sizeof(struct berval) +
616 rs->sr_entry->e_nname.bv_len + 1;
617 sn = op->o_tmpalloc( len, op->o_tmpmemctx );
618 sn->sn_vals = (struct berval *)(sn+1);
620 /* Build tmp list of key values */
621 for ( i=0; i<sc->sc_nkeys; i++ ) {
622 Attribute *a = attr_find( rs->sr_entry->e_attrs,
623 sc->sc_keys[i].sk_ad );
625 if ( a->a_numvals > 1 ) {
626 bv = select_value( a, &sc->sc_keys[i] );
630 sn->sn_vals[i] = *bv;
631 len += bv->bv_len + 1;
633 BER_BVZERO( &sn->sn_vals[i] );
637 /* Now dup into regular memory */
638 sn2 = ch_malloc( len );
639 sn2->sn_vals = (struct berval *)(sn2+1);
640 AC_MEMCPY( sn2->sn_vals, sn->sn_vals,
641 sc->sc_nkeys * sizeof(struct berval));
643 ptr = (char *)(sn2->sn_vals + sc->sc_nkeys);
644 sn2->sn_dn.bv_val = ptr;
645 sn2->sn_dn.bv_len = rs->sr_entry->e_nname.bv_len;
646 AC_MEMCPY( ptr, rs->sr_entry->e_nname.bv_val,
647 rs->sr_entry->e_nname.bv_len );
648 ptr += rs->sr_entry->e_nname.bv_len;
650 for ( i=0; i<sc->sc_nkeys; i++ ) {
651 if ( !BER_BVISNULL( &sn2->sn_vals[i] )) {
652 AC_MEMCPY(ptr, sn2->sn_vals[i].bv_val, sn2->sn_vals[i].bv_len);
653 sn2->sn_vals[i].bv_val = ptr;
654 ptr += sn2->sn_vals[i].bv_len;
658 op->o_tmpfree( sn, op->o_tmpmemctx );
660 sn->sn_conn = op->o_conn->c_conn_idx;
662 /* Insert into the AVL tree */
663 tavl_insert(&(so->so_tree), sn, node_insert, avl_dup_error);
667 /* Collected the keys so that they can be sorted. Thus, stop
668 * the entry from propagating.
670 rs->sr_err = LDAP_SUCCESS;
672 else if ( rs->sr_type == REP_RESULT ) {
673 /* Remove serversort response callback.
674 * We don't want the entries that we are about to send to be
675 * processed by serversort response again.
677 if ( op->o_callback->sc_response == sssvlv_op_response ) {
678 op->o_callback = op->o_callback->sc_next;
681 send_entry( op, rs, so );
682 send_result( op, rs, so );
688 static int sssvlv_op_search(
692 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
693 sssvlv_info *si = on->on_bi.bi_private;
694 int rc = SLAP_CB_CONTINUE;
697 sort_ctrl *sc = op->o_controls[sss_cid];
698 PagedResultsState *ps;
701 if ( op->o_ctrlflag[sss_cid] <= SLAP_CONTROL_IGNORED ) {
702 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
703 LDAPControl *ctrls[2];
705 so2.so_vlv_target = 0;
707 so2.so_vlv_rc = LDAP_VLV_SSS_MISSING;
708 rc = pack_vlv_response_control( op, rs, &so2, ctrls );
709 if ( rc == LDAP_SUCCESS ) {
711 slap_add_ctrls( op, rs, ctrls );
713 rs->sr_err = LDAP_VLV_ERROR;
714 rs->sr_text = "Sort control is required with VLV";
717 /* Not server side sort so just continue */
718 return SLAP_CB_CONTINUE;
721 Debug(LDAP_DEBUG_TRACE,
722 "==> sssvlv_search: <%s> %s, control flag: %d\n",
723 op->o_req_dn.bv_val, op->ors_filterstr.bv_val,
724 op->o_ctrlflag[sss_cid]);
726 if ( sc->sc_nkeys > si->svi_max_keys ) {
727 rs->sr_text = "Too many sort keys";
728 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
732 ps = ( op->o_pagedresults > SLAP_CONTROL_IGNORED ) ?
733 (PagedResultsState*)(op->o_pagedresults_state) : NULL;
734 vc = op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ?
735 op->o_controls[vlv_cid] : NULL;
738 rs->sr_text = "VLV incompatible with PagedResults";
739 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
744 ldap_pvt_thread_mutex_lock( &sort_conns_mutex );
745 so = sort_conns[op->o_conn->c_conn_idx];
746 /* Is there already a sort running on this conn? */
748 /* Is it a continuation of a VLV search? */
749 if ( !vc || so->so_vlv <= SLAP_CONTROL_IGNORED ||
750 vc->vc_context != so->so_vcontext ) {
751 /* Is it a continuation of a paged search? */
752 if ( !ps || so->so_paged <= SLAP_CONTROL_IGNORED ||
753 op->o_conn->c_pagedresults_state.ps_cookie != ps->ps_cookie ) {
755 } else if ( !ps->ps_size ) {
756 /* Abandoning current request */
759 rs->sr_err = LDAP_SUCCESS;
762 if (( vc && so->so_paged > SLAP_CONTROL_IGNORED ) ||
763 ( ps && so->so_vlv > SLAP_CONTROL_IGNORED )) {
764 /* changed from paged to vlv or vice versa, abandon */
767 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
769 /* Are there too many running overall? */
770 } else if ( si->svi_num >= si->svi_max ) {
773 /* OK, this connection now has a sort running */
775 sort_conns[op->o_conn->c_conn_idx] = &so2;
777 ldap_pvt_thread_mutex_unlock( &sort_conns_mutex );
779 /* are we continuing a VLV search? */
780 if ( vc && vc->vc_context ) {
782 send_list( op, rs, so );
783 send_result( op, rs, so );
785 /* are we continuing a paged search? */
786 } else if ( ps && ps->ps_cookie ) {
788 send_page( op, rs, so );
789 send_result( op, rs, so );
792 slap_callback *cb = op->o_tmpalloc( sizeof(slap_callback),
794 /* Install serversort response callback to handle a new search */
796 so = ch_malloc( sizeof(sort_op));
798 so = op->o_tmpalloc( sizeof(sort_op), op->o_tmpmemctx );
800 sort_conns[op->o_conn->c_conn_idx] = so;
802 cb->sc_cleanup = NULL;
803 cb->sc_response = sssvlv_op_response;
804 cb->sc_next = op->o_callback;
811 so->so_paged = op->o_pagedresults;
812 so->so_page_size = ps->ps_size;
813 op->o_pagedresults = SLAP_CONTROL_IGNORED;
816 so->so_page_size = 0;
818 so->so_vlv = op->o_ctrlflag[vlv_cid];
819 so->so_vlv_target = 0;
823 so->so_vcontext = (unsigned long)so;
829 if ( so && !so->so_nentries ) {
830 free_sort_op( op->o_conn, so );
832 rs->sr_text = "Other sort requests already in progress";
833 rs->sr_err = LDAP_BUSY;
837 send_ldap_result( op, rs );
843 static int get_ordering_rule(
844 AttributeDescription *ad,
845 struct berval *matchrule,
847 MatchingRule **ordering )
851 if ( matchrule && matchrule->bv_val ) {
852 mr = mr_find( matchrule->bv_val );
854 rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
855 rs->sr_text = "serverSort control: No ordering rule";
856 Debug(LDAP_DEBUG_TRACE, "%s: no ordering rule function for %s\n",
857 debug_header, matchrule->bv_val, 0);
861 mr = ad->ad_type->sat_ordering;
863 rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
864 rs->sr_text = "serverSort control: No ordering rule";
865 Debug(LDAP_DEBUG_TRACE,
866 "%s: no ordering rule specified and no default ordering rule for attribute %s\n",
867 debug_header, ad->ad_cname.bv_val, 0);
875 static int count_key(BerElement *ber)
882 /* Server Side Sort Control is a SEQUENCE of SEQUENCE */
883 for ( tag = ber_first_element( ber, &len, &end );
884 tag == LBER_SEQUENCE;
885 tag = ber_next_element( ber, &len, end ))
887 tag = ber_skip_tag( ber, &len );
888 ber_skip_data( ber, len );
896 static int build_key(
902 struct berval matchrule = BER_BVNULL;
903 ber_int_t reverse = 0;
906 MatchingRule *ordering = NULL;
907 AttributeDescription *ad = NULL;
910 if (( tag = ber_scanf( ber, "{" )) == LBER_ERROR ) {
911 rs->sr_text = "serverSort control: decoding error";
912 rs->sr_err = LDAP_PROTOCOL_ERROR;
916 if (( tag = ber_scanf( ber, "m", &attr )) == LBER_ERROR ) {
917 rs->sr_text = "serverSort control: attribute decoding error";
918 rs->sr_err = LDAP_PROTOCOL_ERROR;
922 tag = ber_peek_tag( ber, &len );
923 if ( tag == LDAP_MATCHRULE_IDENTIFIER ) {
924 if (( tag = ber_scanf( ber, "m", &matchrule )) == LBER_ERROR ) {
925 rs->sr_text = "serverSort control: matchrule decoding error";
926 rs->sr_err = LDAP_PROTOCOL_ERROR;
929 tag = ber_peek_tag( ber, &len );
932 if ( tag == LDAP_REVERSEORDER_IDENTIFIER ) {
933 if (( tag = ber_scanf( ber, "b", &reverse )) == LBER_ERROR ) {
934 rs->sr_text = "serverSort control: reverse decoding error";
935 rs->sr_err = LDAP_PROTOCOL_ERROR;
940 if (( tag = ber_scanf( ber, "}" )) == LBER_ERROR ) {
941 rs->sr_text = "serverSort control: decoding error";
942 rs->sr_err = LDAP_PROTOCOL_ERROR;
946 if ( slap_bv2ad( &attr, &ad, &text ) != LDAP_SUCCESS ) {
948 "serverSort control: Unrecognized attribute type in sort key";
949 Debug(LDAP_DEBUG_TRACE,
950 "%s: Unrecognized attribute type in sort key: %s\n",
951 debug_header, SAFESTR(attr.bv_val, "<None>"), 0);
952 rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
956 /* get_ordering_rule will set sr_err and sr_text */
957 get_ordering_rule( ad, &matchrule, rs, &ordering );
958 if ( rs->sr_err != LDAP_SUCCESS ) {
963 key->sk_ordering = ordering;
964 key->sk_direction = reverse ? -1 : 1;
969 static int sss_parseCtrl(
974 BerElementBuffer berbuf;
981 rs->sr_err = LDAP_PROTOCOL_ERROR;
983 if ( op->o_ctrlflag[sss_cid] > SLAP_CONTROL_IGNORED ) {
984 rs->sr_text = "sorted results control specified multiple times";
985 } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
986 rs->sr_text = "sorted results control value is absent";
987 } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
988 rs->sr_text = "sorted results control value is empty";
990 rs->sr_err = LDAP_SUCCESS;
992 if ( rs->sr_err != LDAP_SUCCESS )
995 op->o_ctrlflag[sss_cid] = ctrl->ldctl_iscritical ?
996 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
998 ber = (BerElement *)&berbuf;
999 ber_init2( ber, &ctrl->ldctl_value, 0 );
1000 i = count_key( ber );
1002 sc = op->o_tmpalloc( sizeof(sort_ctrl) +
1003 i * sizeof(sort_key), op->o_tmpmemctx );
1005 op->o_controls[sss_cid] = sc;
1007 /* peel off initial sequence */
1008 ber_scanf( ber, "{" );
1012 if ( build_key( ber, rs, &sc->sc_keys[i] ) != LDAP_SUCCESS )
1015 tag = ber_peek_tag( ber, &len );
1016 } while ( tag != LBER_DEFAULT );
1021 static int vlv_parseCtrl(
1026 BerElementBuffer berbuf;
1033 rs->sr_err = LDAP_PROTOCOL_ERROR;
1036 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
1037 rs->sr_text = "vlv control specified multiple times";
1038 } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1039 rs->sr_text = "vlv control value is absent";
1040 } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1041 rs->sr_text = "vlv control value is empty";
1043 if ( rs->sr_text != NULL )
1046 op->o_ctrlflag[vlv_cid] = ctrl->ldctl_iscritical ?
1047 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1049 ber = (BerElement *)&berbuf;
1050 ber_init2( ber, &ctrl->ldctl_value, 0 );
1052 rs->sr_err = LDAP_PROTOCOL_ERROR;
1054 tag = ber_scanf( ber, "{ii", &vc2.vc_before, &vc2.vc_after );
1055 if ( tag == LBER_ERROR ) {
1059 tag = ber_peek_tag( ber, &len );
1060 if ( tag == LDAP_VLVBYINDEX_IDENTIFIER ) {
1061 tag = ber_scanf( ber, "{ii}", &vc2.vc_offset, &vc2.vc_count );
1062 if ( tag == LBER_ERROR )
1064 BER_BVZERO( &vc2.vc_value );
1065 } else if ( tag == LDAP_VLVBYVALUE_IDENTIFIER ) {
1066 tag = ber_scanf( ber, "m", &vc2.vc_value );
1067 if ( tag == LBER_ERROR || BER_BVISNULL( &vc2.vc_value ))
1072 tag = ber_peek_tag( ber, &len );
1073 if ( tag == LDAP_VLVCONTEXT_IDENTIFIER ) {
1075 tag = ber_scanf( ber, "m", &bv );
1076 if ( tag == LBER_ERROR || bv.bv_len != sizeof(vc2.vc_context))
1078 AC_MEMCPY( &vc2.vc_context, bv.bv_val, bv.bv_len );
1083 vc = op->o_tmpalloc( sizeof(vlv_ctrl), op->o_tmpmemctx );
1085 op->o_controls[vlv_cid] = vc;
1086 rs->sr_err = LDAP_SUCCESS;
1091 static int sssvlv_connection_destroy( BackendDB *be, Connection *conn )
1093 slap_overinst *on = (slap_overinst *)be->bd_info;
1095 if ( sort_conns[conn->c_conn_idx] )
1096 free_sort_op( conn, sort_conns[conn->c_conn_idx] );
1098 return LDAP_SUCCESS;
1101 static int sssvlv_db_open(
1105 slap_overinst *on = (slap_overinst *)be->bd_info;
1106 sssvlv_info *si = on->on_bi.bi_private;
1109 /* If not set, default to 1/2 of available threads */
1111 si->svi_max = connection_pool_max / 2;
1113 rc = overlay_register_control( be, LDAP_CONTROL_SORTREQUEST );
1114 if ( rc == LDAP_SUCCESS )
1115 rc = overlay_register_control( be, LDAP_CONTROL_VLVREQUEST );
1119 static ConfigTable sssvlv_cfg[] = {
1120 { "sssvlv-max", "num",
1121 2, 2, 0, ARG_INT|ARG_OFFSET,
1122 (void *)offsetof(sssvlv_info, svi_max),
1123 "( OLcfgOvAt:21.1 NAME 'olcSssVlvMax' "
1124 "DESC 'Maximum number of concurrent Sort requests' "
1125 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
1126 { "sssvlv-maxkeys", "num",
1127 2, 2, 0, ARG_INT|ARG_OFFSET,
1128 (void *)offsetof(sssvlv_info, svi_max_keys),
1129 "( OLcfgOvAt:21.2 NAME 'olcSssVlvMaxKeys' "
1130 "DESC 'Maximum number of Keys in a Sort request' "
1131 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
1132 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1135 static ConfigOCs sssvlv_ocs[] = {
1136 { "( OLcfgOvOc:21.1 "
1137 "NAME 'olcSssVlvConfig' "
1138 "DESC 'SSS VLV configuration' "
1139 "SUP olcOverlayConfig "
1140 "MAY ( olcSssVlvMax $ olcSssVlvMaxKeys ) )",
1141 Cft_Overlay, sssvlv_cfg, NULL, NULL },
1145 static int sssvlv_db_init(
1149 slap_overinst *on = (slap_overinst *)be->bd_info;
1152 si = (sssvlv_info *)ch_malloc(sizeof(sssvlv_info));
1153 on->on_bi.bi_private = si;
1157 si->svi_max_keys = SSSVLV_DEFAULT_MAX_KEYS;
1159 if ( dtblsize && !sort_conns ) {
1160 ldap_pvt_thread_mutex_init( &sort_conns_mutex );
1161 /* accommodate for c_conn_idx == -1 */
1162 sort_conns = ch_calloc( sizeof(sort_op *), dtblsize + 1 );
1166 return LDAP_SUCCESS;
1169 static int sssvlv_db_destroy(
1173 slap_overinst *on = (slap_overinst *)be->bd_info;
1174 sssvlv_info *si = (sssvlv_info *)on->on_bi.bi_private;
1178 on->on_bi.bi_private = NULL;
1180 return LDAP_SUCCESS;
1183 static slap_overinst sssvlv;
1185 int sssvlv_initialize()
1189 sssvlv.on_bi.bi_type = "sssvlv";
1190 sssvlv.on_bi.bi_db_init = sssvlv_db_init;
1191 sssvlv.on_bi.bi_db_destroy = sssvlv_db_destroy;
1192 sssvlv.on_bi.bi_db_open = sssvlv_db_open;
1193 sssvlv.on_bi.bi_connection_destroy = sssvlv_connection_destroy;
1194 sssvlv.on_bi.bi_op_search = sssvlv_op_search;
1196 sssvlv.on_bi.bi_cf_ocs = sssvlv_ocs;
1198 rc = config_register_schema( sssvlv_cfg, sssvlv_ocs );
1202 rc = register_supported_control2( LDAP_CONTROL_SORTREQUEST,
1209 if ( rc == LDAP_SUCCESS ) {
1210 rc = register_supported_control2( LDAP_CONTROL_VLVREQUEST,
1218 if ( rc == LDAP_SUCCESS ) {
1219 rc = overlay_register( &sssvlv );
1220 if ( rc != LDAP_SUCCESS ) {
1221 fprintf( stderr, "Failed to register server side sort overlay\n" );
1225 fprintf( stderr, "Failed to register control %d\n", rc );
1231 #if SLAPD_OVER_SSSVLV == SLAPD_MOD_DYNAMIC
1232 int init_module( int argc, char *argv[])
1234 return sssvlv_initialize();
1238 #endif /* SLAPD_OVER_SSSVLV */