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, "{iie", 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 if ( slapd_shutdown ) break;
477 op->o_bd = select_backend( &sn->sn_dn, 0 );
479 rc = be_entry_get_rw( op, &sn->sn_dn, NULL, NULL, 0, &e );
481 if ( e && rc == LDAP_SUCCESS ) {
483 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
484 rs->sr_err = send_search_entry( op, rs );
485 if ( rs->sr_err == LDAP_UNAVAILABLE )
488 cur_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
489 if ( !cur_node ) break;
491 so->so_vlv_rc = LDAP_SUCCESS;
496 static void send_page( Operation *op, SlapReply *rs, sort_op *so )
498 Avlnode *cur_node = so->so_tree;
499 Avlnode *next_node = NULL;
500 BackendDB *be = op->o_bd;
504 while ( cur_node && rs->sr_nentries < so->so_page_size ) {
505 sort_node *sn = cur_node->avl_data;
507 if ( slapd_shutdown ) break;
509 next_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
511 op->o_bd = select_backend( &sn->sn_dn, 0 );
513 rc = be_entry_get_rw( op, &sn->sn_dn, NULL, NULL, 0, &e );
515 ch_free( cur_node->avl_data );
516 ber_memfree( cur_node );
518 cur_node = next_node;
521 if ( e && rc == LDAP_SUCCESS ) {
523 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
524 rs->sr_err = send_search_entry( op, rs );
525 if ( rs->sr_err == LDAP_UNAVAILABLE )
530 /* Set the first entry to send for the next page */
531 so->so_tree = next_node;
536 static void send_entry(
541 Debug(LDAP_DEBUG_TRACE,
542 "%s: response control: status=%d, text=%s\n",
543 debug_header, rs->sr_err, SAFESTR(rs->sr_text, "<None>"));
548 /* RFC 2891: If critical then send the entries iff they were
549 * succesfully sorted. If non-critical send all entries
550 * whether they were sorted or not.
552 if ( (op->o_ctrlflag[sss_cid] != SLAP_CONTROL_CRITICAL) ||
553 (rs->sr_err == LDAP_SUCCESS) )
555 if ( so->so_vlv > SLAP_CONTROL_IGNORED ) {
556 send_list( op, rs, so );
558 /* Get the first node to send */
559 Avlnode *start_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
560 so->so_tree = start_node;
562 if ( so->so_paged <= SLAP_CONTROL_IGNORED ) {
563 /* Not paged result search. Send all entries.
564 * Set the page size to the number of entries
565 * so that send_page() will send all entries.
567 so->so_page_size = so->so_nentries;
570 send_page( op, rs, so );
575 static void send_result(
580 LDAPControl *ctrls[3];
583 rc = pack_sss_response_control( op, rs, ctrls );
584 if ( rc == LDAP_SUCCESS ) {
587 if ( so->so_paged > SLAP_CONTROL_IGNORED ) {
588 rc = pack_pagedresult_response_control( op, rs, so, ctrls+1 );
589 } else if ( so->so_vlv > SLAP_CONTROL_IGNORED ) {
590 rc = pack_vlv_response_control( op, rs, so, ctrls+1 );
592 if ( rc == LDAP_SUCCESS )
597 if ( ctrls[0] != NULL )
598 slap_add_ctrls( op, rs, ctrls );
599 send_ldap_result( op, rs );
601 if ( so->so_tree == NULL ) {
602 /* Search finished, so clean up */
603 free_sort_op( op->o_conn, so );
607 static int sssvlv_op_response(
611 sort_ctrl *sc = op->o_controls[sss_cid];
612 sort_op *so = op->o_callback->sc_private;
614 if ( rs->sr_type == REP_SEARCH ) {
621 len = sizeof(sort_node) + sc->sc_nkeys * sizeof(struct berval) +
622 rs->sr_entry->e_nname.bv_len + 1;
623 sn = op->o_tmpalloc( len, op->o_tmpmemctx );
624 sn->sn_vals = (struct berval *)(sn+1);
626 /* Build tmp list of key values */
627 for ( i=0; i<sc->sc_nkeys; i++ ) {
628 Attribute *a = attr_find( rs->sr_entry->e_attrs,
629 sc->sc_keys[i].sk_ad );
631 if ( a->a_numvals > 1 ) {
632 bv = select_value( a, &sc->sc_keys[i] );
636 sn->sn_vals[i] = *bv;
637 len += bv->bv_len + 1;
639 BER_BVZERO( &sn->sn_vals[i] );
643 /* Now dup into regular memory */
644 sn2 = ch_malloc( len );
645 sn2->sn_vals = (struct berval *)(sn2+1);
646 AC_MEMCPY( sn2->sn_vals, sn->sn_vals,
647 sc->sc_nkeys * sizeof(struct berval));
649 ptr = (char *)(sn2->sn_vals + sc->sc_nkeys);
650 sn2->sn_dn.bv_val = ptr;
651 sn2->sn_dn.bv_len = rs->sr_entry->e_nname.bv_len;
652 AC_MEMCPY( ptr, rs->sr_entry->e_nname.bv_val,
653 rs->sr_entry->e_nname.bv_len );
654 ptr += rs->sr_entry->e_nname.bv_len;
656 for ( i=0; i<sc->sc_nkeys; i++ ) {
657 if ( !BER_BVISNULL( &sn2->sn_vals[i] )) {
658 AC_MEMCPY(ptr, sn2->sn_vals[i].bv_val, sn2->sn_vals[i].bv_len);
659 sn2->sn_vals[i].bv_val = ptr;
660 ptr += sn2->sn_vals[i].bv_len;
664 op->o_tmpfree( sn, op->o_tmpmemctx );
666 sn->sn_conn = op->o_conn->c_conn_idx;
668 /* Insert into the AVL tree */
669 tavl_insert(&(so->so_tree), sn, node_insert, avl_dup_error);
673 /* Collected the keys so that they can be sorted. Thus, stop
674 * the entry from propagating.
676 rs->sr_err = LDAP_SUCCESS;
678 else if ( rs->sr_type == REP_RESULT ) {
679 /* Remove serversort response callback.
680 * We don't want the entries that we are about to send to be
681 * processed by serversort response again.
683 if ( op->o_callback->sc_response == sssvlv_op_response ) {
684 op->o_callback = op->o_callback->sc_next;
687 send_entry( op, rs, so );
688 send_result( op, rs, so );
694 static int sssvlv_op_search(
698 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
699 sssvlv_info *si = on->on_bi.bi_private;
700 int rc = SLAP_CB_CONTINUE;
704 PagedResultsState *ps;
707 if ( op->o_ctrlflag[sss_cid] <= SLAP_CONTROL_IGNORED ) {
708 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
709 LDAPControl *ctrls[2];
711 so2.so_vlv_target = 0;
713 so2.so_vlv_rc = LDAP_VLV_SSS_MISSING;
714 rc = pack_vlv_response_control( op, rs, &so2, ctrls );
715 if ( rc == LDAP_SUCCESS ) {
717 slap_add_ctrls( op, rs, ctrls );
719 rs->sr_err = LDAP_VLV_ERROR;
720 rs->sr_text = "Sort control is required with VLV";
723 /* Not server side sort so just continue */
724 return SLAP_CB_CONTINUE;
727 Debug(LDAP_DEBUG_TRACE,
728 "==> sssvlv_search: <%s> %s, control flag: %d\n",
729 op->o_req_dn.bv_val, op->ors_filterstr.bv_val,
730 op->o_ctrlflag[sss_cid]);
732 sc = op->o_controls[sss_cid];
733 if ( sc->sc_nkeys > si->svi_max_keys ) {
734 rs->sr_text = "Too many sort keys";
735 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
739 ps = ( op->o_pagedresults > SLAP_CONTROL_IGNORED ) ?
740 (PagedResultsState*)(op->o_pagedresults_state) : NULL;
741 vc = op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ?
742 op->o_controls[vlv_cid] : NULL;
745 rs->sr_text = "VLV incompatible with PagedResults";
746 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
751 ldap_pvt_thread_mutex_lock( &sort_conns_mutex );
752 so = sort_conns[op->o_conn->c_conn_idx];
753 /* Is there already a sort running on this conn? */
755 /* Is it a continuation of a VLV search? */
756 if ( !vc || so->so_vlv <= SLAP_CONTROL_IGNORED ||
757 vc->vc_context != so->so_vcontext ) {
758 /* Is it a continuation of a paged search? */
759 if ( !ps || so->so_paged <= SLAP_CONTROL_IGNORED ||
760 op->o_conn->c_pagedresults_state.ps_cookie != ps->ps_cookie ) {
762 } else if ( !ps->ps_size ) {
763 /* Abandoning current request */
766 rs->sr_err = LDAP_SUCCESS;
769 if (( vc && so->so_paged > SLAP_CONTROL_IGNORED ) ||
770 ( ps && so->so_vlv > SLAP_CONTROL_IGNORED )) {
771 /* changed from paged to vlv or vice versa, abandon */
774 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
776 /* Are there too many running overall? */
777 } else if ( si->svi_num >= si->svi_max ) {
780 /* OK, this connection now has a sort running */
782 sort_conns[op->o_conn->c_conn_idx] = &so2;
784 ldap_pvt_thread_mutex_unlock( &sort_conns_mutex );
786 /* are we continuing a VLV search? */
787 if ( vc && vc->vc_context ) {
789 send_list( op, rs, so );
790 send_result( op, rs, so );
792 /* are we continuing a paged search? */
793 } else if ( ps && ps->ps_cookie ) {
795 send_page( op, rs, so );
796 send_result( op, rs, so );
799 slap_callback *cb = op->o_tmpalloc( sizeof(slap_callback),
801 /* Install serversort response callback to handle a new search */
803 so = ch_calloc( 1, sizeof(sort_op));
805 so = op->o_tmpcalloc( 1, sizeof(sort_op), op->o_tmpmemctx );
807 sort_conns[op->o_conn->c_conn_idx] = so;
809 cb->sc_cleanup = NULL;
810 cb->sc_response = sssvlv_op_response;
811 cb->sc_next = op->o_callback;
818 so->so_paged = op->o_pagedresults;
819 so->so_page_size = ps->ps_size;
820 op->o_pagedresults = SLAP_CONTROL_IGNORED;
823 so->so_page_size = 0;
825 so->so_vlv = op->o_ctrlflag[vlv_cid];
826 so->so_vlv_target = 0;
829 so->so_vlv = SLAP_CONTROL_NONE;
832 so->so_vcontext = (unsigned long)so;
838 if ( so && !so->so_nentries ) {
839 free_sort_op( op->o_conn, so );
841 rs->sr_text = "Other sort requests already in progress";
842 rs->sr_err = LDAP_BUSY;
846 send_ldap_result( op, rs );
852 static int get_ordering_rule(
853 AttributeDescription *ad,
854 struct berval *matchrule,
856 MatchingRule **ordering )
860 if ( matchrule && matchrule->bv_val ) {
861 mr = mr_find( matchrule->bv_val );
863 rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
864 rs->sr_text = "serverSort control: No ordering rule";
865 Debug(LDAP_DEBUG_TRACE, "%s: no ordering rule function for %s\n",
866 debug_header, matchrule->bv_val, 0);
870 mr = ad->ad_type->sat_ordering;
872 rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
873 rs->sr_text = "serverSort control: No ordering rule";
874 Debug(LDAP_DEBUG_TRACE,
875 "%s: no ordering rule specified and no default ordering rule for attribute %s\n",
876 debug_header, ad->ad_cname.bv_val, 0);
884 static int count_key(BerElement *ber)
891 /* Server Side Sort Control is a SEQUENCE of SEQUENCE */
892 for ( tag = ber_first_element( ber, &len, &end );
893 tag == LBER_SEQUENCE;
894 tag = ber_next_element( ber, &len, end ))
896 tag = ber_skip_tag( ber, &len );
897 ber_skip_data( ber, len );
905 static int build_key(
911 struct berval matchrule = BER_BVNULL;
912 ber_int_t reverse = 0;
915 MatchingRule *ordering = NULL;
916 AttributeDescription *ad = NULL;
919 if (( tag = ber_scanf( ber, "{" )) == LBER_ERROR ) {
920 rs->sr_text = "serverSort control: decoding error";
921 rs->sr_err = LDAP_PROTOCOL_ERROR;
925 if (( tag = ber_scanf( ber, "m", &attr )) == LBER_ERROR ) {
926 rs->sr_text = "serverSort control: attribute decoding error";
927 rs->sr_err = LDAP_PROTOCOL_ERROR;
931 tag = ber_peek_tag( ber, &len );
932 if ( tag == LDAP_MATCHRULE_IDENTIFIER ) {
933 if (( tag = ber_scanf( ber, "m", &matchrule )) == LBER_ERROR ) {
934 rs->sr_text = "serverSort control: matchrule decoding error";
935 rs->sr_err = LDAP_PROTOCOL_ERROR;
938 tag = ber_peek_tag( ber, &len );
941 if ( tag == LDAP_REVERSEORDER_IDENTIFIER ) {
942 if (( tag = ber_scanf( ber, "b", &reverse )) == LBER_ERROR ) {
943 rs->sr_text = "serverSort control: reverse decoding error";
944 rs->sr_err = LDAP_PROTOCOL_ERROR;
949 if (( tag = ber_scanf( ber, "}" )) == LBER_ERROR ) {
950 rs->sr_text = "serverSort control: decoding error";
951 rs->sr_err = LDAP_PROTOCOL_ERROR;
955 if ( slap_bv2ad( &attr, &ad, &text ) != LDAP_SUCCESS ) {
957 "serverSort control: Unrecognized attribute type in sort key";
958 Debug(LDAP_DEBUG_TRACE,
959 "%s: Unrecognized attribute type in sort key: %s\n",
960 debug_header, SAFESTR(attr.bv_val, "<None>"), 0);
961 rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
965 /* get_ordering_rule will set sr_err and sr_text */
966 get_ordering_rule( ad, &matchrule, rs, &ordering );
967 if ( rs->sr_err != LDAP_SUCCESS ) {
972 key->sk_ordering = ordering;
973 key->sk_direction = reverse ? -1 : 1;
978 static int sss_parseCtrl(
983 BerElementBuffer berbuf;
990 rs->sr_err = LDAP_PROTOCOL_ERROR;
992 if ( op->o_ctrlflag[sss_cid] > SLAP_CONTROL_IGNORED ) {
993 rs->sr_text = "sorted results control specified multiple times";
994 } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
995 rs->sr_text = "sorted results control value is absent";
996 } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
997 rs->sr_text = "sorted results control value is empty";
999 rs->sr_err = LDAP_SUCCESS;
1001 if ( rs->sr_err != LDAP_SUCCESS )
1004 op->o_ctrlflag[sss_cid] = ctrl->ldctl_iscritical ?
1005 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1007 ber = (BerElement *)&berbuf;
1008 ber_init2( ber, &ctrl->ldctl_value, 0 );
1009 i = count_key( ber );
1011 sc = op->o_tmpalloc( sizeof(sort_ctrl) +
1012 (i-1) * sizeof(sort_key), op->o_tmpmemctx );
1014 op->o_controls[sss_cid] = sc;
1016 /* peel off initial sequence */
1017 ber_scanf( ber, "{" );
1021 if ( build_key( ber, rs, &sc->sc_keys[i] ) != LDAP_SUCCESS )
1024 tag = ber_peek_tag( ber, &len );
1025 } while ( tag != LBER_DEFAULT );
1030 static int vlv_parseCtrl(
1035 BerElementBuffer berbuf;
1041 rs->sr_err = LDAP_PROTOCOL_ERROR;
1044 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
1045 rs->sr_text = "vlv control specified multiple times";
1046 } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1047 rs->sr_text = "vlv control value is absent";
1048 } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1049 rs->sr_text = "vlv control value is empty";
1051 if ( rs->sr_text != NULL )
1054 op->o_ctrlflag[vlv_cid] = ctrl->ldctl_iscritical ?
1055 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1057 ber = (BerElement *)&berbuf;
1058 ber_init2( ber, &ctrl->ldctl_value, 0 );
1060 rs->sr_err = LDAP_PROTOCOL_ERROR;
1062 tag = ber_scanf( ber, "{ii", &vc2.vc_before, &vc2.vc_after );
1063 if ( tag == LBER_ERROR ) {
1067 tag = ber_peek_tag( ber, &len );
1068 if ( tag == LDAP_VLVBYINDEX_IDENTIFIER ) {
1069 tag = ber_scanf( ber, "{ii}", &vc2.vc_offset, &vc2.vc_count );
1070 if ( tag == LBER_ERROR )
1072 BER_BVZERO( &vc2.vc_value );
1073 } else if ( tag == LDAP_VLVBYVALUE_IDENTIFIER ) {
1074 tag = ber_scanf( ber, "m", &vc2.vc_value );
1075 if ( tag == LBER_ERROR || BER_BVISNULL( &vc2.vc_value ))
1080 tag = ber_peek_tag( ber, &len );
1081 if ( tag == LDAP_VLVCONTEXT_IDENTIFIER ) {
1083 tag = ber_scanf( ber, "m", &bv );
1084 if ( tag == LBER_ERROR || bv.bv_len != sizeof(vc2.vc_context))
1086 AC_MEMCPY( &vc2.vc_context, bv.bv_val, bv.bv_len );
1091 vc = op->o_tmpalloc( sizeof(vlv_ctrl), op->o_tmpmemctx );
1093 op->o_controls[vlv_cid] = vc;
1094 rs->sr_err = LDAP_SUCCESS;
1099 static int sssvlv_connection_destroy( BackendDB *be, Connection *conn )
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 if ( ov_count == 0 ) {
1161 rc = register_supported_control2( LDAP_CONTROL_SORTREQUEST,
1167 if ( rc != LDAP_SUCCESS ) {
1168 Debug( LDAP_DEBUG_ANY, "Failed to register Sort Request control '%s' (%d)\n",
1169 LDAP_CONTROL_SORTREQUEST, rc, 0 );
1173 rc = register_supported_control2( LDAP_CONTROL_VLVREQUEST,
1179 if ( rc != LDAP_SUCCESS ) {
1180 Debug( LDAP_DEBUG_ANY, "Failed to register VLV Request control '%s' (%d)\n",
1181 LDAP_CONTROL_VLVREQUEST, rc, 0 );
1182 #ifdef SLAP_CONFIG_DELETE
1183 overlay_unregister_control( be, LDAP_CONTROL_SORTREQUEST );
1184 unregister_supported_control( LDAP_CONTROL_SORTREQUEST );
1185 #endif /* SLAP_CONFIG_DELETE */
1190 si = (sssvlv_info *)ch_malloc(sizeof(sssvlv_info));
1191 on->on_bi.bi_private = si;
1195 si->svi_max_keys = SSSVLV_DEFAULT_MAX_KEYS;
1197 if ( dtblsize && !sort_conns ) {
1198 ldap_pvt_thread_mutex_init( &sort_conns_mutex );
1199 /* accommodate for c_conn_idx == -1 */
1200 sort_conns = ch_calloc( sizeof(sort_op *), dtblsize + 1 );
1205 return LDAP_SUCCESS;
1208 static int sssvlv_db_destroy(
1212 slap_overinst *on = (slap_overinst *)be->bd_info;
1213 sssvlv_info *si = (sssvlv_info *)on->on_bi.bi_private;
1216 if ( !ov_count && sort_conns) {
1218 ch_free(sort_conns);
1219 ldap_pvt_thread_mutex_destroy( &sort_conns_mutex );
1222 #ifdef SLAP_CONFIG_DELETE
1223 overlay_unregister_control( be, LDAP_CONTROL_SORTREQUEST );
1224 overlay_unregister_control( be, LDAP_CONTROL_VLVREQUEST );
1225 if ( ov_count == 0 ) {
1226 unregister_supported_control( LDAP_CONTROL_SORTREQUEST );
1227 unregister_supported_control( LDAP_CONTROL_VLVREQUEST );
1229 #endif /* SLAP_CONFIG_DELETE */
1233 on->on_bi.bi_private = NULL;
1235 return LDAP_SUCCESS;
1238 static slap_overinst sssvlv;
1240 int sssvlv_initialize()
1244 sssvlv.on_bi.bi_type = "sssvlv";
1245 sssvlv.on_bi.bi_db_init = sssvlv_db_init;
1246 sssvlv.on_bi.bi_db_destroy = sssvlv_db_destroy;
1247 sssvlv.on_bi.bi_db_open = sssvlv_db_open;
1248 sssvlv.on_bi.bi_connection_destroy = sssvlv_connection_destroy;
1249 sssvlv.on_bi.bi_op_search = sssvlv_op_search;
1251 sssvlv.on_bi.bi_cf_ocs = sssvlv_ocs;
1253 rc = config_register_schema( sssvlv_cfg, sssvlv_ocs );
1257 rc = overlay_register( &sssvlv );
1258 if ( rc != LDAP_SUCCESS ) {
1259 Debug( LDAP_DEBUG_ANY, "Failed to register server side sort overlay\n", 0, 0, 0 );
1265 #if SLAPD_OVER_SSSVLV == SLAPD_MOD_DYNAMIC
1266 int init_module( int argc, char *argv[])
1268 return sssvlv_initialize();
1272 #endif /* SLAPD_OVER_SSSVLV */