1 /* sssvlv.c - server side sort / virtual list view */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2009-2010 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 #include "../../../libraries/liblber/lber-int.h" /* ber_rewind */
39 /* RFC2891: Server Side Sorting
40 * RFC2696: Paged Results
42 #ifndef LDAP_MATCHRULE_IDENTIFIER
43 #define LDAP_MATCHRULE_IDENTIFIER 0x80L
44 #define LDAP_REVERSEORDER_IDENTIFIER 0x81L
45 #define LDAP_ATTRTYPES_IDENTIFIER 0x80L
48 /* draft-ietf-ldapext-ldapv3-vlv-09.txt: Virtual List Views
50 #ifndef LDAP_VLVBYINDEX_IDENTIFIER
51 #define LDAP_VLVBYINDEX_IDENTIFIER 0xa0L
52 #define LDAP_VLVBYVALUE_IDENTIFIER 0x81L
53 #define LDAP_VLVCONTEXT_IDENTIFIER 0x04L
55 #define LDAP_VLV_SSS_MISSING 0x4C
56 #define LDAP_VLV_RANGE_ERROR 0x4D
59 #define SAFESTR(macro_str, macro_def) ((macro_str) ? (macro_str) : (macro_def))
61 #define SSSVLV_DEFAULT_MAX_KEYS 5
63 typedef struct vlv_ctrl {
68 struct berval vc_value;
69 unsigned long vc_context;
72 typedef struct sort_key
74 AttributeDescription *sk_ad;
75 MatchingRule *sk_ordering;
76 int sk_direction; /* 1=normal, -1=reverse */
79 typedef struct sort_ctrl {
85 typedef struct sort_node
89 struct berval *sn_vals;
92 typedef struct sssvlv_info
94 int svi_max; /* max concurrent sorts */
95 int svi_num; /* current # sorts */
96 int svi_max_keys; /* max sort keys per request */
99 typedef struct sort_op
103 sssvlv_info *so_info;
110 unsigned long so_vcontext;
113 /* There is only one conn table for all overlay instances */
114 static sort_op **sort_conns;
115 static ldap_pvt_thread_mutex_t sort_conns_mutex;
117 static const char *debug_header = "sssvlv";
122 /* RFC 2981 Section 2.2
123 * If a sort key is a multi-valued attribute, and an entry happens to
124 * have multiple values for that attribute and no other controls are
125 * present that affect the sorting order, then the server SHOULD use the
126 * least value (according to the ORDERING rule for that attribute).
128 static struct berval* select_value(
132 struct berval* ber1, *ber2;
133 MatchingRule *mr = key->sk_ordering;
136 ber1 = &(attr->a_nvals[0]);
138 for ( i = 1; i < attr->a_numvals; i++,ber2++ ) {
139 mr->smr_match( &cmp, 0, mr->smr_syntax, mr, ber1, ber2 );
145 Debug(LDAP_DEBUG_TRACE, "%s: value selected for compare: %s\n",
147 SAFESTR(ber1->bv_val, "<Empty>"),
153 static int node_cmp( const void* val1, const void* val2 )
155 sort_node *sn1 = (sort_node *)val1;
156 sort_node *sn2 = (sort_node *)val2;
157 sort_ctrl *sc = sort_conns[sn1->sn_conn]->so_ctrl;
161 for ( i=0; cmp == 0 && i<sc->sc_nkeys; i++ ) {
162 if ( BER_BVISNULL( &sn1->sn_vals[i] )) {
163 if ( BER_BVISNULL( &sn2->sn_vals[i] ))
166 cmp = sc->sc_keys[i].sk_direction;
167 } else if ( BER_BVISNULL( &sn2->sn_vals[i] )) {
168 cmp = sc->sc_keys[i].sk_direction * -1;
170 mr = sc->sc_keys[i].sk_ordering;
171 mr->smr_match( &cmp, 0, mr->smr_syntax, mr,
172 &sn1->sn_vals[i], &sn2->sn_vals[i] );
174 cmp *= sc->sc_keys[i].sk_direction;
180 static int node_insert( const void *val1, const void *val2 )
182 /* Never return equal so that new entries are always inserted */
183 return node_cmp( val1, val2 ) < 0 ? -1 : 1;
186 static int pack_vlv_response_control(
190 LDAPControl **ctrlsp )
193 BerElementBuffer berbuf;
194 BerElement *ber = (BerElement *)&berbuf;
195 struct berval cookie, bv;
198 ber_init2( ber, NULL, LBER_USE_DER );
199 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
201 rc = ber_printf( ber, "{iii", so->so_vlv_target, so->so_nentries,
204 if ( rc != -1 && so->so_vcontext ) {
205 cookie.bv_val = (char *)&so->so_vcontext;
206 cookie.bv_len = sizeof(so->so_vcontext);
207 rc = ber_printf( ber, "tO", LDAP_VLVCONTEXT_IDENTIFIER, &cookie );
211 rc = ber_printf( ber, "}" );
215 rc = ber_flatten2( ber, &bv, 0 );
219 ctrl = (LDAPControl *)op->o_tmpalloc( sizeof(LDAPControl)+
220 bv.bv_len, op->o_tmpmemctx );
221 ctrl->ldctl_oid = LDAP_CONTROL_VLVRESPONSE;
222 ctrl->ldctl_iscritical = 0;
223 ctrl->ldctl_value.bv_val = (char *)(ctrl+1);
224 ctrl->ldctl_value.bv_len = bv.bv_len;
225 AC_MEMCPY( ctrl->ldctl_value.bv_val, bv.bv_val, bv.bv_len );
229 rs->sr_err = LDAP_OTHER;
237 static int pack_pagedresult_response_control(
241 LDAPControl **ctrlsp )
244 BerElementBuffer berbuf;
245 BerElement *ber = (BerElement *)&berbuf;
246 PagedResultsCookie resp_cookie;
247 struct berval cookie, bv;
250 ber_init2( ber, NULL, LBER_USE_DER );
251 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
253 if ( so->so_nentries > 0 ) {
254 resp_cookie = ( PagedResultsCookie )so->so_tree;
255 cookie.bv_len = sizeof( PagedResultsCookie );
256 cookie.bv_val = (char *)&resp_cookie;
258 resp_cookie = ( PagedResultsCookie )0;
259 BER_BVZERO( &cookie );
262 op->o_conn->c_pagedresults_state.ps_cookie = resp_cookie;
263 op->o_conn->c_pagedresults_state.ps_count
264 = ((PagedResultsState *)op->o_pagedresults_state)->ps_count
267 rc = ber_printf( ber, "{iO}", so->so_nentries, &cookie );
269 rc = ber_flatten2( ber, &bv, 0 );
273 ctrl = (LDAPControl *)op->o_tmpalloc( sizeof(LDAPControl)+
274 bv.bv_len, op->o_tmpmemctx );
275 ctrl->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
276 ctrl->ldctl_iscritical = 0;
277 ctrl->ldctl_value.bv_val = (char *)(ctrl+1);
278 ctrl->ldctl_value.bv_len = bv.bv_len;
279 AC_MEMCPY( ctrl->ldctl_value.bv_val, bv.bv_val, bv.bv_len );
283 rs->sr_err = LDAP_OTHER;
291 static int pack_sss_response_control(
294 LDAPControl **ctrlsp )
297 BerElementBuffer berbuf;
298 BerElement *ber = (BerElement *)&berbuf;
302 ber_init2( ber, NULL, LBER_USE_DER );
303 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
305 /* Pack error code */
306 rc = ber_printf(ber, "{e}", rs->sr_err);
309 rc = ber_flatten2( ber, &bv, 0 );
312 ctrl = (LDAPControl *)op->o_tmpalloc( sizeof(LDAPControl)+
313 bv.bv_len, op->o_tmpmemctx );
314 ctrl->ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
315 ctrl->ldctl_iscritical = 0;
316 ctrl->ldctl_value.bv_val = (char *)(ctrl+1);
317 ctrl->ldctl_value.bv_len = bv.bv_len;
318 AC_MEMCPY( ctrl->ldctl_value.bv_val, bv.bv_val, bv.bv_len );
322 rs->sr_err = LDAP_OTHER;
330 static void free_sort_op( Connection *conn, sort_op *so )
333 tavl_free( so->so_tree, ch_free );
337 ldap_pvt_thread_mutex_lock( &sort_conns_mutex );
338 sort_conns[conn->c_conn_idx] = NULL;
339 so->so_info->svi_num--;
340 ldap_pvt_thread_mutex_unlock( &sort_conns_mutex );
345 static void send_list(
350 Avlnode *cur_node, *tmp_node;
351 vlv_ctrl *vc = op->o_controls[vlv_cid];
355 LDAPControl *ctrls[2];
357 /* FIXME: it may be better to just flatten the tree into
358 * an array before doing all of this...
361 /* Are we just counting an offset? */
362 if ( BER_BVISNULL( &vc->vc_value )) {
363 if ( vc->vc_offset == vc->vc_count ) {
364 /* wants the last entry in the list */
365 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
366 so->so_vlv_target = so->so_nentries;
367 } else if ( vc->vc_offset == 1 ) {
368 /* wants the first entry in the list */
369 cur_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
370 so->so_vlv_target = 1;
373 /* Just iterate to the right spot */
374 if ( vc->vc_count && vc->vc_count != so->so_nentries ) {
375 if ( vc->vc_offset > vc->vc_count )
377 target = so->so_nentries * vc->vc_offset / vc->vc_count;
379 if ( vc->vc_offset > so->so_nentries ) {
381 so->so_vlv_rc = LDAP_VLV_RANGE_ERROR;
382 pack_vlv_response_control( op, rs, so, ctrls );
384 slap_add_ctrls( op, rs, ctrls );
385 rs->sr_err = LDAP_VLV_ERROR;
388 target = vc->vc_offset;
390 so->so_vlv_target = target;
391 /* Start at left and go right, or start at right and go left? */
392 if ( target < so->so_nentries / 2 ) {
393 cur_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
394 dir = TAVL_DIR_RIGHT;
396 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
398 target = so->so_nentries - target + 1;
400 for ( i=1; i<target; i++ )
401 cur_node = tavl_next( cur_node, dir );
404 /* we're looking for a specific value */
405 sort_ctrl *sc = so->so_ctrl;
406 MatchingRule *mr = sc->sc_keys[0].sk_ordering;
410 if ( mr->smr_normalize ) {
411 rc = mr->smr_normalize( SLAP_MR_VALUE_OF_SYNTAX,
412 mr->smr_syntax, mr, &vc->vc_value, &bv, op->o_tmpmemctx );
414 so->so_vlv_rc = LDAP_INAPPROPRIATE_MATCHING;
415 pack_vlv_response_control( op, rs, so, ctrls );
417 slap_add_ctrls( op, rs, ctrls );
418 rs->sr_err = LDAP_VLV_ERROR;
425 sn = op->o_tmpalloc( sizeof(sort_node) +
426 sc->sc_nkeys * sizeof(struct berval), op->o_tmpmemctx );
427 sn->sn_vals = (struct berval *)(sn+1);
428 sn->sn_conn = op->o_conn->c_conn_idx;
430 for (i=1; i<sc->sc_nkeys; i++) {
431 BER_BVZERO( &sn->sn_vals[i] );
433 cur_node = tavl_find3( so->so_tree, sn, node_cmp, &j );
434 /* didn't find >= match */
437 op->o_tmpfree( sn, op->o_tmpmemctx );
440 so->so_vlv_target = so->so_nentries + 1;
442 sort_node *sn = so->so_tree->avl_data;
443 /* start from the left or the right side? */
444 mr->smr_match( &i, 0, mr->smr_syntax, mr, &bv, &sn->sn_vals[0] );
446 tmp_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
449 tmp_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
450 dir = TAVL_DIR_RIGHT;
452 for (i=0; tmp_node != cur_node;
453 tmp_node = tavl_next( tmp_node, dir ), i++);
454 so->so_vlv_target = i;
456 if ( bv.bv_val != vc->vc_value.bv_val )
457 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
461 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
465 for ( ; i<vc->vc_before; i++ ) {
466 tmp_node = tavl_next( cur_node, TAVL_DIR_LEFT );
467 if ( !tmp_node ) break;
470 j = i + vc->vc_after + 1;
472 for ( i=0; i<j; i++ ) {
473 sort_node *sn = cur_node->avl_data;
475 op->o_bd = select_backend( &sn->sn_dn, 0 );
477 rc = be_entry_get_rw( op, &sn->sn_dn, NULL, NULL, 0, &e );
479 if ( e && rc == LDAP_SUCCESS ) {
481 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
482 rs->sr_err = send_search_entry( op, rs );
483 if ( rs->sr_err == LDAP_UNAVAILABLE )
486 cur_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
487 if ( !cur_node ) break;
489 so->so_vlv_rc = LDAP_SUCCESS;
494 static void send_page( Operation *op, SlapReply *rs, sort_op *so )
496 Avlnode *cur_node = so->so_tree;
497 Avlnode *next_node = NULL;
498 BackendDB *be = op->o_bd;
503 while ( cur_node && rs->sr_nentries < so->so_page_size ) {
504 sort_node *sn = cur_node->avl_data;
506 next_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
508 op->o_bd = select_backend( &sn->sn_dn, 0 );
510 rc = be_entry_get_rw( op, &sn->sn_dn, NULL, NULL, 0, &e );
512 ch_free( cur_node->avl_data );
513 ber_memfree( cur_node );
515 cur_node = next_node;
518 if ( e && rc == LDAP_SUCCESS ) {
520 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
521 rs->sr_err = send_search_entry( op, rs );
522 if ( rs->sr_err == LDAP_UNAVAILABLE )
527 /* Set the first entry to send for the next page */
528 so->so_tree = next_node;
533 static void send_entry(
538 Debug(LDAP_DEBUG_TRACE,
539 "%s: response control: status=%d, text=%s\n",
540 debug_header, rs->sr_err, SAFESTR(rs->sr_text, "<None>"));
545 /* RFC 2891: If critical then send the entries iff they were
546 * succesfully sorted. If non-critical send all entries
547 * whether they were sorted or not.
549 if ( (op->o_ctrlflag[sss_cid] != SLAP_CONTROL_CRITICAL) ||
550 (rs->sr_err == LDAP_SUCCESS) )
552 if ( so->so_vlv > SLAP_CONTROL_IGNORED ) {
553 send_list( op, rs, so );
555 /* Get the first node to send */
556 Avlnode *start_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
557 so->so_tree = start_node;
559 if ( so->so_paged <= SLAP_CONTROL_IGNORED ) {
560 /* Not paged result search. Send all entries.
561 * Set the page size to the number of entries
562 * so that send_page() will send all entries.
564 so->so_page_size = so->so_nentries;
567 send_page( op, rs, so );
572 static void send_result(
577 LDAPControl *ctrls[3];
580 rc = pack_sss_response_control( op, rs, ctrls );
581 if ( rc == LDAP_SUCCESS ) {
584 if ( so->so_paged > SLAP_CONTROL_IGNORED ) {
585 rc = pack_pagedresult_response_control( op, rs, so, ctrls+1 );
586 } else if ( so->so_vlv > SLAP_CONTROL_IGNORED ) {
587 rc = pack_vlv_response_control( op, rs, so, ctrls+1 );
589 if ( rc == LDAP_SUCCESS )
594 if ( ctrls[0] != NULL )
595 slap_add_ctrls( op, rs, ctrls );
596 send_ldap_result( op, rs );
598 if ( so->so_tree == NULL ) {
599 /* Search finished, so clean up */
600 free_sort_op( op->o_conn, so );
604 static int sssvlv_op_response(
608 sort_ctrl *sc = op->o_controls[sss_cid];
609 sort_op *so = op->o_callback->sc_private;
611 if ( rs->sr_type == REP_SEARCH ) {
618 len = sizeof(sort_node) + sc->sc_nkeys * sizeof(struct berval) +
619 rs->sr_entry->e_nname.bv_len + 1;
620 sn = op->o_tmpalloc( len, op->o_tmpmemctx );
621 sn->sn_vals = (struct berval *)(sn+1);
623 /* Build tmp list of key values */
624 for ( i=0; i<sc->sc_nkeys; i++ ) {
625 Attribute *a = attr_find( rs->sr_entry->e_attrs,
626 sc->sc_keys[i].sk_ad );
628 if ( a->a_numvals > 1 ) {
629 bv = select_value( a, &sc->sc_keys[i] );
633 sn->sn_vals[i] = *bv;
634 len += bv->bv_len + 1;
636 BER_BVZERO( &sn->sn_vals[i] );
640 /* Now dup into regular memory */
641 sn2 = ch_malloc( len );
642 sn2->sn_vals = (struct berval *)(sn2+1);
643 AC_MEMCPY( sn2->sn_vals, sn->sn_vals,
644 sc->sc_nkeys * sizeof(struct berval));
646 ptr = (char *)(sn2->sn_vals + sc->sc_nkeys);
647 sn2->sn_dn.bv_val = ptr;
648 sn2->sn_dn.bv_len = rs->sr_entry->e_nname.bv_len;
649 AC_MEMCPY( ptr, rs->sr_entry->e_nname.bv_val,
650 rs->sr_entry->e_nname.bv_len );
651 ptr += rs->sr_entry->e_nname.bv_len;
653 for ( i=0; i<sc->sc_nkeys; i++ ) {
654 if ( !BER_BVISNULL( &sn2->sn_vals[i] )) {
655 AC_MEMCPY(ptr, sn2->sn_vals[i].bv_val, sn2->sn_vals[i].bv_len);
656 sn2->sn_vals[i].bv_val = ptr;
657 ptr += sn2->sn_vals[i].bv_len;
661 op->o_tmpfree( sn, op->o_tmpmemctx );
663 sn->sn_conn = op->o_conn->c_conn_idx;
665 /* Insert into the AVL tree */
666 tavl_insert(&(so->so_tree), sn, node_insert, avl_dup_error);
670 /* Collected the keys so that they can be sorted. Thus, stop
671 * the entry from propagating.
673 rs->sr_err = LDAP_SUCCESS;
675 else if ( rs->sr_type == REP_RESULT ) {
676 /* Remove serversort response callback.
677 * We don't want the entries that we are about to send to be
678 * processed by serversort response again.
680 if ( op->o_callback->sc_response == sssvlv_op_response ) {
681 op->o_callback = op->o_callback->sc_next;
684 send_entry( op, rs, so );
685 send_result( op, rs, so );
691 static int sssvlv_op_search(
695 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
696 sssvlv_info *si = on->on_bi.bi_private;
697 int rc = SLAP_CB_CONTINUE;
701 PagedResultsState *ps;
704 if ( op->o_ctrlflag[sss_cid] <= SLAP_CONTROL_IGNORED ) {
705 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
706 LDAPControl *ctrls[2];
708 so2.so_vlv_target = 0;
710 so2.so_vlv_rc = LDAP_VLV_SSS_MISSING;
711 rc = pack_vlv_response_control( op, rs, &so2, ctrls );
712 if ( rc == LDAP_SUCCESS ) {
714 slap_add_ctrls( op, rs, ctrls );
716 rs->sr_err = LDAP_VLV_ERROR;
717 rs->sr_text = "Sort control is required with VLV";
720 /* Not server side sort so just continue */
721 return SLAP_CB_CONTINUE;
724 Debug(LDAP_DEBUG_TRACE,
725 "==> sssvlv_search: <%s> %s, control flag: %d\n",
726 op->o_req_dn.bv_val, op->ors_filterstr.bv_val,
727 op->o_ctrlflag[sss_cid]);
729 sc = op->o_controls[sss_cid];
730 if ( sc->sc_nkeys > si->svi_max_keys ) {
731 rs->sr_text = "Too many sort keys";
732 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
736 ps = ( op->o_pagedresults > SLAP_CONTROL_IGNORED ) ?
737 (PagedResultsState*)(op->o_pagedresults_state) : NULL;
738 vc = op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ?
739 op->o_controls[vlv_cid] : NULL;
742 rs->sr_text = "VLV incompatible with PagedResults";
743 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
748 ldap_pvt_thread_mutex_lock( &sort_conns_mutex );
749 so = sort_conns[op->o_conn->c_conn_idx];
750 /* Is there already a sort running on this conn? */
752 /* Is it a continuation of a VLV search? */
753 if ( !vc || so->so_vlv <= SLAP_CONTROL_IGNORED ||
754 vc->vc_context != so->so_vcontext ) {
755 /* Is it a continuation of a paged search? */
756 if ( !ps || so->so_paged <= SLAP_CONTROL_IGNORED ||
757 op->o_conn->c_pagedresults_state.ps_cookie != ps->ps_cookie ) {
759 } else if ( !ps->ps_size ) {
760 /* Abandoning current request */
763 rs->sr_err = LDAP_SUCCESS;
766 if (( vc && so->so_paged > SLAP_CONTROL_IGNORED ) ||
767 ( ps && so->so_vlv > SLAP_CONTROL_IGNORED )) {
768 /* changed from paged to vlv or vice versa, abandon */
771 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
773 /* Are there too many running overall? */
774 } else if ( si->svi_num >= si->svi_max ) {
777 /* OK, this connection now has a sort running */
779 sort_conns[op->o_conn->c_conn_idx] = &so2;
781 ldap_pvt_thread_mutex_unlock( &sort_conns_mutex );
783 /* are we continuing a VLV search? */
784 if ( vc && vc->vc_context ) {
786 send_list( op, rs, so );
787 send_result( op, rs, so );
789 /* are we continuing a paged search? */
790 } else if ( ps && ps->ps_cookie ) {
792 send_page( op, rs, so );
793 send_result( op, rs, so );
796 slap_callback *cb = op->o_tmpalloc( sizeof(slap_callback),
798 /* Install serversort response callback to handle a new search */
800 so = ch_malloc( sizeof(sort_op));
802 so = op->o_tmpalloc( sizeof(sort_op), op->o_tmpmemctx );
804 sort_conns[op->o_conn->c_conn_idx] = so;
806 cb->sc_cleanup = NULL;
807 cb->sc_response = sssvlv_op_response;
808 cb->sc_next = op->o_callback;
815 so->so_paged = op->o_pagedresults;
816 so->so_page_size = ps->ps_size;
817 op->o_pagedresults = SLAP_CONTROL_IGNORED;
820 so->so_page_size = 0;
822 so->so_vlv = op->o_ctrlflag[vlv_cid];
823 so->so_vlv_target = 0;
826 so->so_vlv = SLAP_CONTROL_NONE;
829 so->so_vcontext = (unsigned long)so;
835 if ( so && !so->so_nentries ) {
836 free_sort_op( op->o_conn, so );
838 rs->sr_text = "Other sort requests already in progress";
839 rs->sr_err = LDAP_BUSY;
843 send_ldap_result( op, rs );
849 static int get_ordering_rule(
850 AttributeDescription *ad,
851 struct berval *matchrule,
853 MatchingRule **ordering )
857 if ( matchrule && matchrule->bv_val ) {
858 mr = mr_find( matchrule->bv_val );
860 rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
861 rs->sr_text = "serverSort control: No ordering rule";
862 Debug(LDAP_DEBUG_TRACE, "%s: no ordering rule function for %s\n",
863 debug_header, matchrule->bv_val, 0);
867 mr = ad->ad_type->sat_ordering;
869 rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
870 rs->sr_text = "serverSort control: No ordering rule";
871 Debug(LDAP_DEBUG_TRACE,
872 "%s: no ordering rule specified and no default ordering rule for attribute %s\n",
873 debug_header, ad->ad_cname.bv_val, 0);
881 static int count_key(BerElement *ber)
888 /* Server Side Sort Control is a SEQUENCE of SEQUENCE */
889 for ( tag = ber_first_element( ber, &len, &end );
890 tag == LBER_SEQUENCE;
891 tag = ber_next_element( ber, &len, end ))
893 tag = ber_skip_tag( ber, &len );
894 ber_skip_data( ber, len );
902 static int build_key(
908 struct berval matchrule = BER_BVNULL;
909 ber_int_t reverse = 0;
912 MatchingRule *ordering = NULL;
913 AttributeDescription *ad = NULL;
916 if (( tag = ber_scanf( ber, "{" )) == LBER_ERROR ) {
917 rs->sr_text = "serverSort control: decoding error";
918 rs->sr_err = LDAP_PROTOCOL_ERROR;
922 if (( tag = ber_scanf( ber, "m", &attr )) == LBER_ERROR ) {
923 rs->sr_text = "serverSort control: attribute decoding error";
924 rs->sr_err = LDAP_PROTOCOL_ERROR;
928 tag = ber_peek_tag( ber, &len );
929 if ( tag == LDAP_MATCHRULE_IDENTIFIER ) {
930 if (( tag = ber_scanf( ber, "m", &matchrule )) == LBER_ERROR ) {
931 rs->sr_text = "serverSort control: matchrule decoding error";
932 rs->sr_err = LDAP_PROTOCOL_ERROR;
935 tag = ber_peek_tag( ber, &len );
938 if ( tag == LDAP_REVERSEORDER_IDENTIFIER ) {
939 if (( tag = ber_scanf( ber, "b", &reverse )) == LBER_ERROR ) {
940 rs->sr_text = "serverSort control: reverse decoding error";
941 rs->sr_err = LDAP_PROTOCOL_ERROR;
946 if (( tag = ber_scanf( ber, "}" )) == LBER_ERROR ) {
947 rs->sr_text = "serverSort control: decoding error";
948 rs->sr_err = LDAP_PROTOCOL_ERROR;
952 if ( slap_bv2ad( &attr, &ad, &text ) != LDAP_SUCCESS ) {
954 "serverSort control: Unrecognized attribute type in sort key";
955 Debug(LDAP_DEBUG_TRACE,
956 "%s: Unrecognized attribute type in sort key: %s\n",
957 debug_header, SAFESTR(attr.bv_val, "<None>"), 0);
958 rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
962 /* get_ordering_rule will set sr_err and sr_text */
963 get_ordering_rule( ad, &matchrule, rs, &ordering );
964 if ( rs->sr_err != LDAP_SUCCESS ) {
969 key->sk_ordering = ordering;
970 key->sk_direction = reverse ? -1 : 1;
975 static int sss_parseCtrl(
980 BerElementBuffer berbuf;
987 rs->sr_err = LDAP_PROTOCOL_ERROR;
989 if ( op->o_ctrlflag[sss_cid] > SLAP_CONTROL_IGNORED ) {
990 rs->sr_text = "sorted results control specified multiple times";
991 } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
992 rs->sr_text = "sorted results control value is absent";
993 } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
994 rs->sr_text = "sorted results control value is empty";
996 rs->sr_err = LDAP_SUCCESS;
998 if ( rs->sr_err != LDAP_SUCCESS )
1001 op->o_ctrlflag[sss_cid] = ctrl->ldctl_iscritical ?
1002 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1004 ber = (BerElement *)&berbuf;
1005 ber_init2( ber, &ctrl->ldctl_value, 0 );
1006 i = count_key( ber );
1008 sc = op->o_tmpalloc( sizeof(sort_ctrl) +
1009 (i-1) * sizeof(sort_key), op->o_tmpmemctx );
1011 op->o_controls[sss_cid] = sc;
1013 /* peel off initial sequence */
1014 ber_scanf( ber, "{" );
1018 if ( build_key( ber, rs, &sc->sc_keys[i] ) != LDAP_SUCCESS )
1021 tag = ber_peek_tag( ber, &len );
1022 } while ( tag != LBER_DEFAULT );
1027 static int vlv_parseCtrl(
1032 BerElementBuffer berbuf;
1039 rs->sr_err = LDAP_PROTOCOL_ERROR;
1042 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
1043 rs->sr_text = "vlv control specified multiple times";
1044 } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1045 rs->sr_text = "vlv control value is absent";
1046 } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1047 rs->sr_text = "vlv control value is empty";
1049 if ( rs->sr_text != NULL )
1052 op->o_ctrlflag[vlv_cid] = ctrl->ldctl_iscritical ?
1053 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1055 ber = (BerElement *)&berbuf;
1056 ber_init2( ber, &ctrl->ldctl_value, 0 );
1058 rs->sr_err = LDAP_PROTOCOL_ERROR;
1060 tag = ber_scanf( ber, "{ii", &vc2.vc_before, &vc2.vc_after );
1061 if ( tag == LBER_ERROR ) {
1065 tag = ber_peek_tag( ber, &len );
1066 if ( tag == LDAP_VLVBYINDEX_IDENTIFIER ) {
1067 tag = ber_scanf( ber, "{ii}", &vc2.vc_offset, &vc2.vc_count );
1068 if ( tag == LBER_ERROR )
1070 BER_BVZERO( &vc2.vc_value );
1071 } else if ( tag == LDAP_VLVBYVALUE_IDENTIFIER ) {
1072 tag = ber_scanf( ber, "m", &vc2.vc_value );
1073 if ( tag == LBER_ERROR || BER_BVISNULL( &vc2.vc_value ))
1078 tag = ber_peek_tag( ber, &len );
1079 if ( tag == LDAP_VLVCONTEXT_IDENTIFIER ) {
1081 tag = ber_scanf( ber, "m", &bv );
1082 if ( tag == LBER_ERROR || bv.bv_len != sizeof(vc2.vc_context))
1084 AC_MEMCPY( &vc2.vc_context, bv.bv_val, bv.bv_len );
1089 vc = op->o_tmpalloc( sizeof(vlv_ctrl), op->o_tmpmemctx );
1091 op->o_controls[vlv_cid] = vc;
1092 rs->sr_err = LDAP_SUCCESS;
1097 static int sssvlv_connection_destroy( BackendDB *be, Connection *conn )
1099 slap_overinst *on = (slap_overinst *)be->bd_info;
1101 if ( sort_conns[conn->c_conn_idx] )
1102 free_sort_op( conn, sort_conns[conn->c_conn_idx] );
1104 return LDAP_SUCCESS;
1107 static int sssvlv_db_open(
1111 slap_overinst *on = (slap_overinst *)be->bd_info;
1112 sssvlv_info *si = on->on_bi.bi_private;
1115 /* If not set, default to 1/2 of available threads */
1117 si->svi_max = connection_pool_max / 2;
1119 rc = overlay_register_control( be, LDAP_CONTROL_SORTREQUEST );
1120 if ( rc == LDAP_SUCCESS )
1121 rc = overlay_register_control( be, LDAP_CONTROL_VLVREQUEST );
1125 static ConfigTable sssvlv_cfg[] = {
1126 { "sssvlv-max", "num",
1127 2, 2, 0, ARG_INT|ARG_OFFSET,
1128 (void *)offsetof(sssvlv_info, svi_max),
1129 "( OLcfgOvAt:21.1 NAME 'olcSssVlvMax' "
1130 "DESC 'Maximum number of concurrent Sort requests' "
1131 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
1132 { "sssvlv-maxkeys", "num",
1133 2, 2, 0, ARG_INT|ARG_OFFSET,
1134 (void *)offsetof(sssvlv_info, svi_max_keys),
1135 "( OLcfgOvAt:21.2 NAME 'olcSssVlvMaxKeys' "
1136 "DESC 'Maximum number of Keys in a Sort request' "
1137 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
1138 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1141 static ConfigOCs sssvlv_ocs[] = {
1142 { "( OLcfgOvOc:21.1 "
1143 "NAME 'olcSssVlvConfig' "
1144 "DESC 'SSS VLV configuration' "
1145 "SUP olcOverlayConfig "
1146 "MAY ( olcSssVlvMax $ olcSssVlvMaxKeys ) )",
1147 Cft_Overlay, sssvlv_cfg, NULL, NULL },
1151 static int sssvlv_db_init(
1155 slap_overinst *on = (slap_overinst *)be->bd_info;
1158 si = (sssvlv_info *)ch_malloc(sizeof(sssvlv_info));
1159 on->on_bi.bi_private = si;
1163 si->svi_max_keys = SSSVLV_DEFAULT_MAX_KEYS;
1165 if ( dtblsize && !sort_conns ) {
1166 ldap_pvt_thread_mutex_init( &sort_conns_mutex );
1167 /* accommodate for c_conn_idx == -1 */
1168 sort_conns = ch_calloc( sizeof(sort_op *), dtblsize + 1 );
1173 return LDAP_SUCCESS;
1176 static int sssvlv_db_destroy(
1180 slap_overinst *on = (slap_overinst *)be->bd_info;
1181 sssvlv_info *si = (sssvlv_info *)on->on_bi.bi_private;
1184 if ( !ov_count && sort_conns) {
1186 ch_free(sort_conns);
1187 ldap_pvt_thread_mutex_destroy( &sort_conns_mutex );
1192 on->on_bi.bi_private = NULL;
1194 return LDAP_SUCCESS;
1197 static slap_overinst sssvlv;
1199 int sssvlv_initialize()
1203 sssvlv.on_bi.bi_type = "sssvlv";
1204 sssvlv.on_bi.bi_db_init = sssvlv_db_init;
1205 sssvlv.on_bi.bi_db_destroy = sssvlv_db_destroy;
1206 sssvlv.on_bi.bi_db_open = sssvlv_db_open;
1207 sssvlv.on_bi.bi_connection_destroy = sssvlv_connection_destroy;
1208 sssvlv.on_bi.bi_op_search = sssvlv_op_search;
1210 sssvlv.on_bi.bi_cf_ocs = sssvlv_ocs;
1212 rc = config_register_schema( sssvlv_cfg, sssvlv_ocs );
1216 rc = register_supported_control2( LDAP_CONTROL_SORTREQUEST,
1223 if ( rc == LDAP_SUCCESS ) {
1224 rc = register_supported_control2( LDAP_CONTROL_VLVREQUEST,
1232 if ( rc == LDAP_SUCCESS ) {
1233 rc = overlay_register( &sssvlv );
1234 if ( rc != LDAP_SUCCESS ) {
1235 fprintf( stderr, "Failed to register server side sort overlay\n" );
1239 fprintf( stderr, "Failed to register control %d\n", rc );
1245 #if SLAPD_OVER_SSSVLV == SLAPD_MOD_DYNAMIC
1246 int init_module( int argc, char *argv[])
1248 return sssvlv_initialize();
1252 #endif /* SLAPD_OVER_SSSVLV */