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 #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;
116 static const char *debug_header = "sssvlv";
121 /* RFC 2981 Section 2.2
122 * If a sort key is a multi-valued attribute, and an entry happens to
123 * have multiple values for that attribute and no other controls are
124 * present that affect the sorting order, then the server SHOULD use the
125 * least value (according to the ORDERING rule for that attribute).
127 static struct berval* select_value(
131 struct berval* ber1, *ber2;
132 MatchingRule *mr = key->sk_ordering;
135 ber1 = &(attr->a_nvals[0]);
137 for ( i = 1; i < attr->a_numvals; i++,ber2++ ) {
138 mr->smr_match( &cmp, 0, mr->smr_syntax, mr, ber1, ber2 );
144 Debug(LDAP_DEBUG_TRACE, "%s: value selected for compare: %s\n",
146 SAFESTR(ber1->bv_val, "<Empty>"),
152 static int node_cmp( const void* val1, const void* val2 )
154 sort_node *sn1 = (sort_node *)val1;
155 sort_node *sn2 = (sort_node *)val2;
156 sort_ctrl *sc = sort_conns[sn1->sn_conn]->so_ctrl;
160 for ( i=0; cmp == 0 && i<sc->sc_nkeys; i++ ) {
161 if ( BER_BVISNULL( &sn1->sn_vals[i] )) {
162 if ( BER_BVISNULL( &sn2->sn_vals[i] ))
165 cmp = sc->sc_keys[i].sk_direction;
166 } else if ( BER_BVISNULL( &sn2->sn_vals[i] )) {
167 cmp = sc->sc_keys[i].sk_direction * -1;
169 mr = sc->sc_keys[i].sk_ordering;
170 mr->smr_match( &cmp, 0, mr->smr_syntax, mr,
171 &sn1->sn_vals[i], &sn2->sn_vals[i] );
173 cmp *= sc->sc_keys[i].sk_direction;
179 static int node_insert( const void *val1, const void *val2 )
181 /* Never return equal so that new entries are always inserted */
182 return node_cmp( val1, val2 ) < 0 ? -1 : 1;
185 static int pack_vlv_response_control(
189 LDAPControl **ctrlsp )
192 BerElementBuffer berbuf;
193 BerElement *ber = (BerElement *)&berbuf;
194 struct berval cookie, bv;
197 ber_init2( ber, NULL, LBER_USE_DER );
198 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
200 rc = ber_printf( ber, "{iii", so->so_vlv_target, so->so_nentries,
203 if ( rc != -1 && so->so_vcontext ) {
204 cookie.bv_val = (char *)&so->so_vcontext;
205 cookie.bv_len = sizeof(so->so_vcontext);
206 rc = ber_printf( ber, "tO", LDAP_VLVCONTEXT_IDENTIFIER, &cookie );
210 rc = ber_printf( ber, "}" );
214 rc = ber_flatten2( ber, &bv, 0 );
218 ctrl = (LDAPControl *)op->o_tmpalloc( sizeof(LDAPControl)+
219 bv.bv_len, op->o_tmpmemctx );
220 ctrl->ldctl_oid = LDAP_CONTROL_VLVRESPONSE;
221 ctrl->ldctl_iscritical = 0;
222 ctrl->ldctl_value.bv_val = (char *)(ctrl+1);
223 ctrl->ldctl_value.bv_len = bv.bv_len;
224 AC_MEMCPY( ctrl->ldctl_value.bv_val, bv.bv_val, bv.bv_len );
228 rs->sr_err = LDAP_OTHER;
236 static int pack_pagedresult_response_control(
240 LDAPControl **ctrlsp )
243 BerElementBuffer berbuf;
244 BerElement *ber = (BerElement *)&berbuf;
245 PagedResultsCookie resp_cookie;
246 struct berval cookie, bv;
249 ber_init2( ber, NULL, LBER_USE_DER );
250 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
252 if ( so->so_nentries > 0 ) {
253 resp_cookie = ( PagedResultsCookie )so->so_tree;
254 cookie.bv_len = sizeof( PagedResultsCookie );
255 cookie.bv_val = (char *)&resp_cookie;
257 resp_cookie = ( PagedResultsCookie )0;
258 BER_BVZERO( &cookie );
261 op->o_conn->c_pagedresults_state.ps_cookie = resp_cookie;
262 op->o_conn->c_pagedresults_state.ps_count
263 = ((PagedResultsState *)op->o_pagedresults_state)->ps_count
266 rc = ber_printf( ber, "{iO}", so->so_nentries, &cookie );
268 rc = ber_flatten2( ber, &bv, 0 );
272 ctrl = (LDAPControl *)op->o_tmpalloc( sizeof(LDAPControl)+
273 bv.bv_len, op->o_tmpmemctx );
274 ctrl->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
275 ctrl->ldctl_iscritical = 0;
276 ctrl->ldctl_value.bv_val = (char *)(ctrl+1);
277 ctrl->ldctl_value.bv_len = bv.bv_len;
278 AC_MEMCPY( ctrl->ldctl_value.bv_val, bv.bv_val, bv.bv_len );
282 rs->sr_err = LDAP_OTHER;
290 static int pack_sss_response_control(
293 LDAPControl **ctrlsp )
296 BerElementBuffer berbuf;
297 BerElement *ber = (BerElement *)&berbuf;
301 ber_init2( ber, NULL, LBER_USE_DER );
302 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
304 /* Pack error code */
305 rc = ber_printf(ber, "{e}", rs->sr_err);
308 rc = ber_flatten2( ber, &bv, 0 );
311 ctrl = (LDAPControl *)op->o_tmpalloc( sizeof(LDAPControl)+
312 bv.bv_len, op->o_tmpmemctx );
313 ctrl->ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
314 ctrl->ldctl_iscritical = 0;
315 ctrl->ldctl_value.bv_val = (char *)(ctrl+1);
316 ctrl->ldctl_value.bv_len = bv.bv_len;
317 AC_MEMCPY( ctrl->ldctl_value.bv_val, bv.bv_val, bv.bv_len );
321 rs->sr_err = LDAP_OTHER;
329 static void free_sort_op( Connection *conn, sort_op *so )
332 tavl_free( so->so_tree, ch_free );
336 ldap_pvt_thread_mutex_lock( &sort_conns_mutex );
337 sort_conns[conn->c_conn_idx] = NULL;
338 so->so_info->svi_num--;
339 ldap_pvt_thread_mutex_unlock( &sort_conns_mutex );
344 static void send_list(
349 Avlnode *cur_node, *tmp_node;
350 vlv_ctrl *vc = op->o_controls[vlv_cid];
354 LDAPControl *ctrls[2];
356 /* FIXME: it may be better to just flatten the tree into
357 * an array before doing all of this...
360 /* Are we just counting an offset? */
361 if ( BER_BVISNULL( &vc->vc_value )) {
362 if ( vc->vc_offset == vc->vc_count ) {
363 /* wants the last entry in the list */
364 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
365 so->so_vlv_target = so->so_nentries;
366 } else if ( vc->vc_offset == 1 ) {
367 /* wants the first entry in the list */
368 cur_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
369 so->so_vlv_target = 1;
372 /* Just iterate to the right spot */
373 if ( vc->vc_count && vc->vc_count != so->so_nentries ) {
374 if ( vc->vc_offset > vc->vc_count )
376 target = so->so_nentries * vc->vc_offset / vc->vc_count;
378 if ( vc->vc_offset > so->so_nentries ) {
380 so->so_vlv_rc = LDAP_VLV_RANGE_ERROR;
381 pack_vlv_response_control( op, rs, so, ctrls );
383 slap_add_ctrls( op, rs, ctrls );
384 rs->sr_err = LDAP_VLV_ERROR;
387 target = vc->vc_offset;
389 so->so_vlv_target = target;
390 /* Start at left and go right, or start at right and go left? */
391 if ( target < so->so_nentries / 2 ) {
392 cur_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
393 dir = TAVL_DIR_RIGHT;
395 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
397 target = so->so_nentries - target + 1;
399 for ( i=1; i<target; i++ )
400 cur_node = tavl_next( cur_node, dir );
403 /* we're looking for a specific value */
404 sort_ctrl *sc = so->so_ctrl;
405 MatchingRule *mr = sc->sc_keys[0].sk_ordering;
409 if ( mr->smr_normalize ) {
410 rc = mr->smr_normalize( SLAP_MR_VALUE_OF_SYNTAX,
411 mr->smr_syntax, mr, &vc->vc_value, &bv, op->o_tmpmemctx );
413 so->so_vlv_rc = LDAP_INAPPROPRIATE_MATCHING;
414 pack_vlv_response_control( op, rs, so, ctrls );
416 slap_add_ctrls( op, rs, ctrls );
417 rs->sr_err = LDAP_VLV_ERROR;
424 sn = op->o_tmpalloc( sizeof(sort_node) +
425 sc->sc_nkeys * sizeof(struct berval), op->o_tmpmemctx );
426 sn->sn_vals = (struct berval *)(sn+1);
427 sn->sn_conn = op->o_conn->c_conn_idx;
429 for (i=1; i<sc->sc_nkeys; i++) {
430 BER_BVZERO( &sn->sn_vals[i] );
432 cur_node = tavl_find3( so->so_tree, sn, node_cmp, &j );
433 /* didn't find >= match */
436 op->o_tmpfree( sn, op->o_tmpmemctx );
439 so->so_vlv_target = so->so_nentries + 1;
441 sort_node *sn = so->so_tree->avl_data;
442 /* start from the left or the right side? */
443 mr->smr_match( &i, 0, mr->smr_syntax, mr, &bv, &sn->sn_vals[0] );
445 tmp_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
448 tmp_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
449 dir = TAVL_DIR_RIGHT;
451 for (i=0; tmp_node != cur_node;
452 tmp_node = tavl_next( tmp_node, dir ), i++);
453 so->so_vlv_target = i;
455 if ( bv.bv_val != vc->vc_value.bv_val )
456 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
460 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
464 for ( ; i<vc->vc_before; i++ ) {
465 tmp_node = tavl_next( cur_node, TAVL_DIR_LEFT );
466 if ( !tmp_node ) break;
469 j = i + vc->vc_after + 1;
471 for ( i=0; i<j; i++ ) {
472 sort_node *sn = cur_node->avl_data;
474 op->o_bd = select_backend( &sn->sn_dn, 0 );
476 rc = be_entry_get_rw( op, &sn->sn_dn, NULL, NULL, 0, &e );
478 if ( e && rc == LDAP_SUCCESS ) {
480 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
481 rs->sr_err = send_search_entry( op, rs );
482 if ( rs->sr_err == LDAP_UNAVAILABLE )
485 cur_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
486 if ( !cur_node ) break;
488 so->so_vlv_rc = LDAP_SUCCESS;
493 static void send_page( Operation *op, SlapReply *rs, sort_op *so )
495 Avlnode *cur_node = so->so_tree;
496 Avlnode *next_node = NULL;
497 BackendDB *be = op->o_bd;
502 while ( cur_node && rs->sr_nentries < so->so_page_size ) {
503 sort_node *sn = cur_node->avl_data;
505 next_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
507 op->o_bd = select_backend( &sn->sn_dn, 0 );
509 rc = be_entry_get_rw( op, &sn->sn_dn, NULL, NULL, 0, &e );
511 ch_free( cur_node->avl_data );
512 ber_memfree( cur_node );
514 cur_node = next_node;
517 if ( e && rc == LDAP_SUCCESS ) {
519 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
520 rs->sr_err = send_search_entry( op, rs );
521 if ( rs->sr_err == LDAP_UNAVAILABLE )
526 /* Set the first entry to send for the next page */
527 so->so_tree = next_node;
532 static void send_entry(
537 Debug(LDAP_DEBUG_TRACE,
538 "%s: response control: status=%d, text=%s\n",
539 debug_header, rs->sr_err, SAFESTR(rs->sr_text, "<None>"));
544 /* RFC 2891: If critical then send the entries iff they were
545 * succesfully sorted. If non-critical send all entries
546 * whether they were sorted or not.
548 if ( (op->o_ctrlflag[sss_cid] != SLAP_CONTROL_CRITICAL) ||
549 (rs->sr_err == LDAP_SUCCESS) )
551 if ( so->so_vlv > SLAP_CONTROL_IGNORED ) {
552 send_list( op, rs, so );
554 /* Get the first node to send */
555 Avlnode *start_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
556 so->so_tree = start_node;
558 if ( so->so_paged <= SLAP_CONTROL_IGNORED ) {
559 /* Not paged result search. Send all entries.
560 * Set the page size to the number of entries
561 * so that send_page() will send all entries.
563 so->so_page_size = so->so_nentries;
566 send_page( op, rs, so );
571 static void send_result(
576 LDAPControl *ctrls[3];
579 rc = pack_sss_response_control( op, rs, ctrls );
580 if ( rc == LDAP_SUCCESS ) {
583 if ( so->so_paged > SLAP_CONTROL_IGNORED ) {
584 rc = pack_pagedresult_response_control( op, rs, so, ctrls+1 );
585 } else if ( so->so_vlv > SLAP_CONTROL_IGNORED ) {
586 rc = pack_vlv_response_control( op, rs, so, ctrls+1 );
588 if ( rc == LDAP_SUCCESS )
593 if ( ctrls[0] != NULL )
594 slap_add_ctrls( op, rs, ctrls );
595 send_ldap_result( op, rs );
597 if ( so->so_tree == NULL ) {
598 /* Search finished, so clean up */
599 free_sort_op( op->o_conn, so );
603 static int sssvlv_op_response(
607 sort_ctrl *sc = op->o_controls[sss_cid];
608 sort_op *so = op->o_callback->sc_private;
610 if ( rs->sr_type == REP_SEARCH ) {
617 len = sizeof(sort_node) + sc->sc_nkeys * sizeof(struct berval) +
618 rs->sr_entry->e_nname.bv_len + 1;
619 sn = op->o_tmpalloc( len, op->o_tmpmemctx );
620 sn->sn_vals = (struct berval *)(sn+1);
622 /* Build tmp list of key values */
623 for ( i=0; i<sc->sc_nkeys; i++ ) {
624 Attribute *a = attr_find( rs->sr_entry->e_attrs,
625 sc->sc_keys[i].sk_ad );
627 if ( a->a_numvals > 1 ) {
628 bv = select_value( a, &sc->sc_keys[i] );
632 sn->sn_vals[i] = *bv;
633 len += bv->bv_len + 1;
635 BER_BVZERO( &sn->sn_vals[i] );
639 /* Now dup into regular memory */
640 sn2 = ch_malloc( len );
641 sn2->sn_vals = (struct berval *)(sn2+1);
642 AC_MEMCPY( sn2->sn_vals, sn->sn_vals,
643 sc->sc_nkeys * sizeof(struct berval));
645 ptr = (char *)(sn2->sn_vals + sc->sc_nkeys);
646 sn2->sn_dn.bv_val = ptr;
647 sn2->sn_dn.bv_len = rs->sr_entry->e_nname.bv_len;
648 AC_MEMCPY( ptr, rs->sr_entry->e_nname.bv_val,
649 rs->sr_entry->e_nname.bv_len );
650 ptr += rs->sr_entry->e_nname.bv_len;
652 for ( i=0; i<sc->sc_nkeys; i++ ) {
653 if ( !BER_BVISNULL( &sn2->sn_vals[i] )) {
654 AC_MEMCPY(ptr, sn2->sn_vals[i].bv_val, sn2->sn_vals[i].bv_len);
655 sn2->sn_vals[i].bv_val = ptr;
656 ptr += sn2->sn_vals[i].bv_len;
660 op->o_tmpfree( sn, op->o_tmpmemctx );
662 sn->sn_conn = op->o_conn->c_conn_idx;
664 /* Insert into the AVL tree */
665 tavl_insert(&(so->so_tree), sn, node_insert, avl_dup_error);
669 /* Collected the keys so that they can be sorted. Thus, stop
670 * the entry from propagating.
672 rs->sr_err = LDAP_SUCCESS;
674 else if ( rs->sr_type == REP_RESULT ) {
675 /* Remove serversort response callback.
676 * We don't want the entries that we are about to send to be
677 * processed by serversort response again.
679 if ( op->o_callback->sc_response == sssvlv_op_response ) {
680 op->o_callback = op->o_callback->sc_next;
683 send_entry( op, rs, so );
684 send_result( op, rs, so );
690 static int sssvlv_op_search(
694 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
695 sssvlv_info *si = on->on_bi.bi_private;
696 int rc = SLAP_CB_CONTINUE;
700 PagedResultsState *ps;
703 if ( op->o_ctrlflag[sss_cid] <= SLAP_CONTROL_IGNORED ) {
704 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
705 LDAPControl *ctrls[2];
707 so2.so_vlv_target = 0;
709 so2.so_vlv_rc = LDAP_VLV_SSS_MISSING;
710 rc = pack_vlv_response_control( op, rs, &so2, ctrls );
711 if ( rc == LDAP_SUCCESS ) {
713 slap_add_ctrls( op, rs, ctrls );
715 rs->sr_err = LDAP_VLV_ERROR;
716 rs->sr_text = "Sort control is required with VLV";
719 /* Not server side sort so just continue */
720 return SLAP_CB_CONTINUE;
723 Debug(LDAP_DEBUG_TRACE,
724 "==> sssvlv_search: <%s> %s, control flag: %d\n",
725 op->o_req_dn.bv_val, op->ors_filterstr.bv_val,
726 op->o_ctrlflag[sss_cid]);
728 sc = op->o_controls[sss_cid];
729 if ( sc->sc_nkeys > si->svi_max_keys ) {
730 rs->sr_text = "Too many sort keys";
731 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
735 ps = ( op->o_pagedresults > SLAP_CONTROL_IGNORED ) ?
736 (PagedResultsState*)(op->o_pagedresults_state) : NULL;
737 vc = op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ?
738 op->o_controls[vlv_cid] : NULL;
741 rs->sr_text = "VLV incompatible with PagedResults";
742 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
747 ldap_pvt_thread_mutex_lock( &sort_conns_mutex );
748 so = sort_conns[op->o_conn->c_conn_idx];
749 /* Is there already a sort running on this conn? */
751 /* Is it a continuation of a VLV search? */
752 if ( !vc || so->so_vlv <= SLAP_CONTROL_IGNORED ||
753 vc->vc_context != so->so_vcontext ) {
754 /* Is it a continuation of a paged search? */
755 if ( !ps || so->so_paged <= SLAP_CONTROL_IGNORED ||
756 op->o_conn->c_pagedresults_state.ps_cookie != ps->ps_cookie ) {
758 } else if ( !ps->ps_size ) {
759 /* Abandoning current request */
762 rs->sr_err = LDAP_SUCCESS;
765 if (( vc && so->so_paged > SLAP_CONTROL_IGNORED ) ||
766 ( ps && so->so_vlv > SLAP_CONTROL_IGNORED )) {
767 /* changed from paged to vlv or vice versa, abandon */
770 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
772 /* Are there too many running overall? */
773 } else if ( si->svi_num >= si->svi_max ) {
776 /* OK, this connection now has a sort running */
778 sort_conns[op->o_conn->c_conn_idx] = &so2;
780 ldap_pvt_thread_mutex_unlock( &sort_conns_mutex );
782 /* are we continuing a VLV search? */
783 if ( vc && vc->vc_context ) {
785 send_list( op, rs, so );
786 send_result( op, rs, so );
788 /* are we continuing a paged search? */
789 } else if ( ps && ps->ps_cookie ) {
791 send_page( op, rs, so );
792 send_result( op, rs, so );
795 slap_callback *cb = op->o_tmpalloc( sizeof(slap_callback),
797 /* Install serversort response callback to handle a new search */
799 so = ch_malloc( sizeof(sort_op));
801 so = op->o_tmpalloc( sizeof(sort_op), op->o_tmpmemctx );
803 sort_conns[op->o_conn->c_conn_idx] = so;
805 cb->sc_cleanup = NULL;
806 cb->sc_response = sssvlv_op_response;
807 cb->sc_next = op->o_callback;
814 so->so_paged = op->o_pagedresults;
815 so->so_page_size = ps->ps_size;
816 op->o_pagedresults = SLAP_CONTROL_IGNORED;
819 so->so_page_size = 0;
821 so->so_vlv = op->o_ctrlflag[vlv_cid];
822 so->so_vlv_target = 0;
826 so->so_vcontext = (unsigned long)so;
832 if ( so && !so->so_nentries ) {
833 free_sort_op( op->o_conn, so );
835 rs->sr_text = "Other sort requests already in progress";
836 rs->sr_err = LDAP_BUSY;
840 send_ldap_result( op, rs );
846 static int get_ordering_rule(
847 AttributeDescription *ad,
848 struct berval *matchrule,
850 MatchingRule **ordering )
854 if ( matchrule && matchrule->bv_val ) {
855 mr = mr_find( matchrule->bv_val );
857 rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
858 rs->sr_text = "serverSort control: No ordering rule";
859 Debug(LDAP_DEBUG_TRACE, "%s: no ordering rule function for %s\n",
860 debug_header, matchrule->bv_val, 0);
864 mr = ad->ad_type->sat_ordering;
866 rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
867 rs->sr_text = "serverSort control: No ordering rule";
868 Debug(LDAP_DEBUG_TRACE,
869 "%s: no ordering rule specified and no default ordering rule for attribute %s\n",
870 debug_header, ad->ad_cname.bv_val, 0);
878 static int count_key(BerElement *ber)
885 /* Server Side Sort Control is a SEQUENCE of SEQUENCE */
886 for ( tag = ber_first_element( ber, &len, &end );
887 tag == LBER_SEQUENCE;
888 tag = ber_next_element( ber, &len, end ))
890 tag = ber_skip_tag( ber, &len );
891 ber_skip_data( ber, len );
899 static int build_key(
905 struct berval matchrule = BER_BVNULL;
906 ber_int_t reverse = 0;
909 MatchingRule *ordering = NULL;
910 AttributeDescription *ad = NULL;
913 if (( tag = ber_scanf( ber, "{" )) == LBER_ERROR ) {
914 rs->sr_text = "serverSort control: decoding error";
915 rs->sr_err = LDAP_PROTOCOL_ERROR;
919 if (( tag = ber_scanf( ber, "m", &attr )) == LBER_ERROR ) {
920 rs->sr_text = "serverSort control: attribute decoding error";
921 rs->sr_err = LDAP_PROTOCOL_ERROR;
925 tag = ber_peek_tag( ber, &len );
926 if ( tag == LDAP_MATCHRULE_IDENTIFIER ) {
927 if (( tag = ber_scanf( ber, "m", &matchrule )) == LBER_ERROR ) {
928 rs->sr_text = "serverSort control: matchrule decoding error";
929 rs->sr_err = LDAP_PROTOCOL_ERROR;
932 tag = ber_peek_tag( ber, &len );
935 if ( tag == LDAP_REVERSEORDER_IDENTIFIER ) {
936 if (( tag = ber_scanf( ber, "b", &reverse )) == LBER_ERROR ) {
937 rs->sr_text = "serverSort control: reverse decoding error";
938 rs->sr_err = LDAP_PROTOCOL_ERROR;
943 if (( tag = ber_scanf( ber, "}" )) == LBER_ERROR ) {
944 rs->sr_text = "serverSort control: decoding error";
945 rs->sr_err = LDAP_PROTOCOL_ERROR;
949 if ( slap_bv2ad( &attr, &ad, &text ) != LDAP_SUCCESS ) {
951 "serverSort control: Unrecognized attribute type in sort key";
952 Debug(LDAP_DEBUG_TRACE,
953 "%s: Unrecognized attribute type in sort key: %s\n",
954 debug_header, SAFESTR(attr.bv_val, "<None>"), 0);
955 rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
959 /* get_ordering_rule will set sr_err and sr_text */
960 get_ordering_rule( ad, &matchrule, rs, &ordering );
961 if ( rs->sr_err != LDAP_SUCCESS ) {
966 key->sk_ordering = ordering;
967 key->sk_direction = reverse ? -1 : 1;
972 static int sss_parseCtrl(
977 BerElementBuffer berbuf;
984 rs->sr_err = LDAP_PROTOCOL_ERROR;
986 if ( op->o_ctrlflag[sss_cid] > SLAP_CONTROL_IGNORED ) {
987 rs->sr_text = "sorted results control specified multiple times";
988 } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
989 rs->sr_text = "sorted results control value is absent";
990 } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
991 rs->sr_text = "sorted results control value is empty";
993 rs->sr_err = LDAP_SUCCESS;
995 if ( rs->sr_err != LDAP_SUCCESS )
998 op->o_ctrlflag[sss_cid] = ctrl->ldctl_iscritical ?
999 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1001 ber = (BerElement *)&berbuf;
1002 ber_init2( ber, &ctrl->ldctl_value, 0 );
1003 i = count_key( ber );
1005 sc = op->o_tmpalloc( sizeof(sort_ctrl) +
1006 (i-1) * sizeof(sort_key), op->o_tmpmemctx );
1008 op->o_controls[sss_cid] = sc;
1010 /* peel off initial sequence */
1011 ber_scanf( ber, "{" );
1015 if ( build_key( ber, rs, &sc->sc_keys[i] ) != LDAP_SUCCESS )
1018 tag = ber_peek_tag( ber, &len );
1019 } while ( tag != LBER_DEFAULT );
1024 static int vlv_parseCtrl(
1029 BerElementBuffer berbuf;
1036 rs->sr_err = LDAP_PROTOCOL_ERROR;
1039 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
1040 rs->sr_text = "vlv control specified multiple times";
1041 } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1042 rs->sr_text = "vlv control value is absent";
1043 } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1044 rs->sr_text = "vlv control value is empty";
1046 if ( rs->sr_text != NULL )
1049 op->o_ctrlflag[vlv_cid] = ctrl->ldctl_iscritical ?
1050 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1052 ber = (BerElement *)&berbuf;
1053 ber_init2( ber, &ctrl->ldctl_value, 0 );
1055 rs->sr_err = LDAP_PROTOCOL_ERROR;
1057 tag = ber_scanf( ber, "{ii", &vc2.vc_before, &vc2.vc_after );
1058 if ( tag == LBER_ERROR ) {
1062 tag = ber_peek_tag( ber, &len );
1063 if ( tag == LDAP_VLVBYINDEX_IDENTIFIER ) {
1064 tag = ber_scanf( ber, "{ii}", &vc2.vc_offset, &vc2.vc_count );
1065 if ( tag == LBER_ERROR )
1067 BER_BVZERO( &vc2.vc_value );
1068 } else if ( tag == LDAP_VLVBYVALUE_IDENTIFIER ) {
1069 tag = ber_scanf( ber, "m", &vc2.vc_value );
1070 if ( tag == LBER_ERROR || BER_BVISNULL( &vc2.vc_value ))
1075 tag = ber_peek_tag( ber, &len );
1076 if ( tag == LDAP_VLVCONTEXT_IDENTIFIER ) {
1078 tag = ber_scanf( ber, "m", &bv );
1079 if ( tag == LBER_ERROR || bv.bv_len != sizeof(vc2.vc_context))
1081 AC_MEMCPY( &vc2.vc_context, bv.bv_val, bv.bv_len );
1086 vc = op->o_tmpalloc( sizeof(vlv_ctrl), op->o_tmpmemctx );
1088 op->o_controls[vlv_cid] = vc;
1089 rs->sr_err = LDAP_SUCCESS;
1094 static int sssvlv_connection_destroy( BackendDB *be, Connection *conn )
1096 slap_overinst *on = (slap_overinst *)be->bd_info;
1098 if ( sort_conns[conn->c_conn_idx] )
1099 free_sort_op( conn, sort_conns[conn->c_conn_idx] );
1101 return LDAP_SUCCESS;
1104 static int sssvlv_db_open(
1108 slap_overinst *on = (slap_overinst *)be->bd_info;
1109 sssvlv_info *si = on->on_bi.bi_private;
1112 /* If not set, default to 1/2 of available threads */
1114 si->svi_max = connection_pool_max / 2;
1116 rc = overlay_register_control( be, LDAP_CONTROL_SORTREQUEST );
1117 if ( rc == LDAP_SUCCESS )
1118 rc = overlay_register_control( be, LDAP_CONTROL_VLVREQUEST );
1122 static ConfigTable sssvlv_cfg[] = {
1123 { "sssvlv-max", "num",
1124 2, 2, 0, ARG_INT|ARG_OFFSET,
1125 (void *)offsetof(sssvlv_info, svi_max),
1126 "( OLcfgOvAt:21.1 NAME 'olcSssVlvMax' "
1127 "DESC 'Maximum number of concurrent Sort requests' "
1128 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
1129 { "sssvlv-maxkeys", "num",
1130 2, 2, 0, ARG_INT|ARG_OFFSET,
1131 (void *)offsetof(sssvlv_info, svi_max_keys),
1132 "( OLcfgOvAt:21.2 NAME 'olcSssVlvMaxKeys' "
1133 "DESC 'Maximum number of Keys in a Sort request' "
1134 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
1135 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1138 static ConfigOCs sssvlv_ocs[] = {
1139 { "( OLcfgOvOc:21.1 "
1140 "NAME 'olcSssVlvConfig' "
1141 "DESC 'SSS VLV configuration' "
1142 "SUP olcOverlayConfig "
1143 "MAY ( olcSssVlvMax $ olcSssVlvMaxKeys ) )",
1144 Cft_Overlay, sssvlv_cfg, NULL, NULL },
1148 static int sssvlv_db_init(
1152 slap_overinst *on = (slap_overinst *)be->bd_info;
1155 si = (sssvlv_info *)ch_malloc(sizeof(sssvlv_info));
1156 on->on_bi.bi_private = si;
1160 si->svi_max_keys = SSSVLV_DEFAULT_MAX_KEYS;
1162 if ( dtblsize && !sort_conns ) {
1163 ldap_pvt_thread_mutex_init( &sort_conns_mutex );
1164 /* accommodate for c_conn_idx == -1 */
1165 sort_conns = ch_calloc( sizeof(sort_op *), dtblsize + 1 );
1169 return LDAP_SUCCESS;
1172 static int sssvlv_db_destroy(
1176 slap_overinst *on = (slap_overinst *)be->bd_info;
1177 sssvlv_info *si = (sssvlv_info *)on->on_bi.bi_private;
1181 on->on_bi.bi_private = NULL;
1183 return LDAP_SUCCESS;
1186 static slap_overinst sssvlv;
1188 int sssvlv_initialize()
1192 sssvlv.on_bi.bi_type = "sssvlv";
1193 sssvlv.on_bi.bi_db_init = sssvlv_db_init;
1194 sssvlv.on_bi.bi_db_destroy = sssvlv_db_destroy;
1195 sssvlv.on_bi.bi_db_open = sssvlv_db_open;
1196 sssvlv.on_bi.bi_connection_destroy = sssvlv_connection_destroy;
1197 sssvlv.on_bi.bi_op_search = sssvlv_op_search;
1199 sssvlv.on_bi.bi_cf_ocs = sssvlv_ocs;
1201 rc = config_register_schema( sssvlv_cfg, sssvlv_ocs );
1205 rc = register_supported_control2( LDAP_CONTROL_SORTREQUEST,
1212 if ( rc == LDAP_SUCCESS ) {
1213 rc = register_supported_control2( LDAP_CONTROL_VLVREQUEST,
1221 if ( rc == LDAP_SUCCESS ) {
1222 rc = overlay_register( &sssvlv );
1223 if ( rc != LDAP_SUCCESS ) {
1224 fprintf( stderr, "Failed to register server side sort overlay\n" );
1228 fprintf( stderr, "Failed to register control %d\n", rc );
1234 #if SLAPD_OVER_SSSVLV == SLAPD_MOD_DYNAMIC
1235 int init_module( int argc, char *argv[])
1237 return sssvlv_initialize();
1241 #endif /* SLAPD_OVER_SSSVLV */