1 /* sssvlv.c - server side sort / virtual list view */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2009-2017 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
19 * OpenLDAP Software. Support for multiple sorts per connection added
25 #ifdef SLAPD_OVER_SSSVLV
29 #include <ac/string.h>
38 #include "../../../libraries/liblber/lber-int.h" /* ber_rewind */
40 /* RFC2891: Server Side Sorting
41 * RFC2696: Paged Results
43 #ifndef LDAP_MATCHRULE_IDENTIFIER
44 #define LDAP_MATCHRULE_IDENTIFIER 0x80L
45 #define LDAP_REVERSEORDER_IDENTIFIER 0x81L
46 #define LDAP_ATTRTYPES_IDENTIFIER 0x80L
49 /* draft-ietf-ldapext-ldapv3-vlv-09.txt: Virtual List Views
51 #ifndef LDAP_VLVBYINDEX_IDENTIFIER
52 #define LDAP_VLVBYINDEX_IDENTIFIER 0xa0L
53 #define LDAP_VLVBYVALUE_IDENTIFIER 0x81L
54 #define LDAP_VLVCONTEXT_IDENTIFIER 0x04L
56 #define LDAP_VLV_SSS_MISSING 0x4C
57 #define LDAP_VLV_RANGE_ERROR 0x4D
60 #define SAFESTR(macro_str, macro_def) ((macro_str) ? (macro_str) : (macro_def))
62 #define SSSVLV_DEFAULT_MAX_KEYS 5
63 #define SSSVLV_DEFAULT_MAX_REQUEST_PER_CONN 5
65 #define NO_PS_COOKIE (PagedResultsCookie) -1
66 #define NO_VC_CONTEXT (unsigned long) -1
68 typedef struct vlv_ctrl {
73 struct berval vc_value;
74 unsigned long vc_context;
77 typedef struct sort_key
79 AttributeDescription *sk_ad;
80 MatchingRule *sk_ordering;
81 int sk_direction; /* 1=normal, -1=reverse */
84 typedef struct sort_ctrl {
90 typedef struct sort_node
95 struct berval *sn_vals;
98 typedef struct sssvlv_info
100 int svi_max; /* max concurrent sorts */
101 int svi_num; /* current # sorts */
102 int svi_max_keys; /* max sort keys per request */
103 int svi_max_percon; /* max concurrent sorts per con */
106 typedef struct sort_op
110 sssvlv_info *so_info;
118 unsigned long so_vcontext;
122 /* There is only one conn table for all overlay instances */
123 /* Each conn can handle one session by context */
124 static sort_op ***sort_conns;
125 static ldap_pvt_thread_mutex_t sort_conns_mutex;
127 static const char *debug_header = "sssvlv";
132 /* RFC 2981 Section 2.2
133 * If a sort key is a multi-valued attribute, and an entry happens to
134 * have multiple values for that attribute and no other controls are
135 * present that affect the sorting order, then the server SHOULD use the
136 * least value (according to the ORDERING rule for that attribute).
138 static struct berval* select_value(
142 struct berval* ber1, *ber2;
143 MatchingRule *mr = key->sk_ordering;
147 ber1 = &(attr->a_nvals[0]);
149 for ( i = 1; i < attr->a_numvals; i++,ber2++ ) {
150 mr->smr_match( &cmp, 0, mr->smr_syntax, mr, ber1, ber2 );
156 Debug(LDAP_DEBUG_TRACE, "%s: value selected for compare: %s\n",
158 SAFESTR(ber1->bv_val, "<Empty>"),
164 static int node_cmp( const void* val1, const void* val2 )
166 sort_node *sn1 = (sort_node *)val1;
167 sort_node *sn2 = (sort_node *)val2;
171 assert( sort_conns[sn1->sn_conn]
172 && sort_conns[sn1->sn_conn][sn1->sn_session]
173 && sort_conns[sn1->sn_conn][sn1->sn_session]->so_ctrl );
174 sc = sort_conns[sn1->sn_conn][sn1->sn_session]->so_ctrl;
176 for ( i=0; cmp == 0 && i<sc->sc_nkeys; i++ ) {
177 if ( BER_BVISNULL( &sn1->sn_vals[i] )) {
178 if ( BER_BVISNULL( &sn2->sn_vals[i] ))
181 cmp = sc->sc_keys[i].sk_direction;
182 } else if ( BER_BVISNULL( &sn2->sn_vals[i] )) {
183 cmp = sc->sc_keys[i].sk_direction * -1;
185 mr = sc->sc_keys[i].sk_ordering;
186 mr->smr_match( &cmp, 0, mr->smr_syntax, mr,
187 &sn1->sn_vals[i], &sn2->sn_vals[i] );
189 cmp *= sc->sc_keys[i].sk_direction;
195 static int node_insert( const void *val1, const void *val2 )
197 /* Never return equal so that new entries are always inserted */
198 return node_cmp( val1, val2 ) < 0 ? -1 : 1;
201 static int pack_vlv_response_control(
205 LDAPControl **ctrlsp )
208 BerElementBuffer berbuf;
209 BerElement *ber = (BerElement *)&berbuf;
210 struct berval cookie, bv;
213 ber_init2( ber, NULL, LBER_USE_DER );
214 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
216 rc = ber_printf( ber, "{iie", so->so_vlv_target, so->so_nentries,
219 if ( rc != -1 && so->so_vcontext ) {
220 cookie.bv_val = (char *)&so->so_vcontext;
221 cookie.bv_len = sizeof(so->so_vcontext);
222 rc = ber_printf( ber, "tO", LDAP_VLVCONTEXT_IDENTIFIER, &cookie );
226 rc = ber_printf( ber, "}" );
230 rc = ber_flatten2( ber, &bv, 0 );
234 ctrl = (LDAPControl *)op->o_tmpalloc( sizeof(LDAPControl)+
235 bv.bv_len, op->o_tmpmemctx );
236 ctrl->ldctl_oid = LDAP_CONTROL_VLVRESPONSE;
237 ctrl->ldctl_iscritical = 0;
238 ctrl->ldctl_value.bv_val = (char *)(ctrl+1);
239 ctrl->ldctl_value.bv_len = bv.bv_len;
240 AC_MEMCPY( ctrl->ldctl_value.bv_val, bv.bv_val, bv.bv_len );
244 rs->sr_err = LDAP_OTHER;
252 static int pack_pagedresult_response_control(
256 LDAPControl **ctrlsp )
259 BerElementBuffer berbuf;
260 BerElement *ber = (BerElement *)&berbuf;
261 PagedResultsCookie resp_cookie;
262 struct berval cookie, bv;
265 ber_init2( ber, NULL, LBER_USE_DER );
266 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
268 if ( so->so_nentries > 0 ) {
269 resp_cookie = ( PagedResultsCookie )so->so_tree;
270 cookie.bv_len = sizeof( PagedResultsCookie );
271 cookie.bv_val = (char *)&resp_cookie;
273 resp_cookie = ( PagedResultsCookie )0;
274 BER_BVZERO( &cookie );
277 op->o_conn->c_pagedresults_state.ps_cookie = resp_cookie;
278 op->o_conn->c_pagedresults_state.ps_count
279 = ((PagedResultsState *)op->o_pagedresults_state)->ps_count
282 rc = ber_printf( ber, "{iO}", so->so_nentries, &cookie );
284 rc = ber_flatten2( ber, &bv, 0 );
288 ctrl = (LDAPControl *)op->o_tmpalloc( sizeof(LDAPControl)+
289 bv.bv_len, op->o_tmpmemctx );
290 ctrl->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
291 ctrl->ldctl_iscritical = 0;
292 ctrl->ldctl_value.bv_val = (char *)(ctrl+1);
293 ctrl->ldctl_value.bv_len = bv.bv_len;
294 AC_MEMCPY( ctrl->ldctl_value.bv_val, bv.bv_val, bv.bv_len );
298 rs->sr_err = LDAP_OTHER;
306 static int pack_sss_response_control(
309 LDAPControl **ctrlsp )
312 BerElementBuffer berbuf;
313 BerElement *ber = (BerElement *)&berbuf;
317 ber_init2( ber, NULL, LBER_USE_DER );
318 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
320 /* Pack error code */
321 rc = ber_printf(ber, "{e}", rs->sr_err);
324 rc = ber_flatten2( ber, &bv, 0 );
327 ctrl = (LDAPControl *)op->o_tmpalloc( sizeof(LDAPControl)+
328 bv.bv_len, op->o_tmpmemctx );
329 ctrl->ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
330 ctrl->ldctl_iscritical = 0;
331 ctrl->ldctl_value.bv_val = (char *)(ctrl+1);
332 ctrl->ldctl_value.bv_len = bv.bv_len;
333 AC_MEMCPY( ctrl->ldctl_value.bv_val, bv.bv_val, bv.bv_len );
337 rs->sr_err = LDAP_OTHER;
345 /* Return the session id or -1 if unknown */
346 static int find_session_by_so(
355 for (sess_id = 0; sess_id < svi_max_percon; sess_id++) {
356 if ( sort_conns[conn_id] && sort_conns[conn_id][sess_id] == so )
362 /* Return the session id or -1 if unknown */
363 static int find_session_by_context(
366 unsigned long vc_context,
367 PagedResultsCookie ps_cookie )
370 for(sess_id = 0; sess_id < svi_max_percon; sess_id++) {
371 if( sort_conns[conn_id] && sort_conns[conn_id][sess_id] &&
372 ( sort_conns[conn_id][sess_id]->so_vcontext == vc_context ||
373 (PagedResultsCookie) sort_conns[conn_id][sess_id]->so_tree == ps_cookie ) )
379 static int find_next_session(
384 assert(sort_conns[conn_id] != NULL);
385 for(sess_id = 0; sess_id < svi_max_percon; sess_id++) {
386 if(!sort_conns[conn_id][sess_id]) {
390 if (sess_id >= svi_max_percon) {
397 static void free_sort_op( Connection *conn, sort_op *so )
401 ldap_pvt_thread_mutex_lock( &sort_conns_mutex );
402 sess_id = find_session_by_so( so->so_info->svi_max_percon, conn->c_conn_idx, so );
403 if ( sess_id > -1 ) {
404 sort_conns[conn->c_conn_idx][sess_id] = NULL;
405 so->so_info->svi_num--;
407 ldap_pvt_thread_mutex_unlock( &sort_conns_mutex );
411 if ( so->so_paged > SLAP_CONTROL_IGNORED ) {
412 TAvlnode *cur_node, *next_node;
413 cur_node = so->so_tree;
415 next_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
416 ch_free( cur_node->avl_data );
417 ber_memfree( cur_node );
419 cur_node = next_node;
422 tavl_free( so->so_tree, ch_free );
431 static void free_sort_ops( Connection *conn, sort_op **sos, int svi_max_percon )
436 for( sess_id = 0; sess_id < svi_max_percon ; sess_id++ ) {
437 so = sort_conns[conn->c_conn_idx][sess_id];
439 free_sort_op( conn, so );
440 sort_conns[conn->c_conn_idx][sess_id] = NULL;
445 static void send_list(
450 TAvlnode *cur_node, *tmp_node;
451 vlv_ctrl *vc = op->o_controls[vlv_cid];
455 LDAPControl *ctrls[2];
457 rs->sr_attrs = op->ors_attrs;
459 /* FIXME: it may be better to just flatten the tree into
460 * an array before doing all of this...
463 /* Are we just counting an offset? */
464 if ( BER_BVISNULL( &vc->vc_value )) {
465 if ( vc->vc_offset == vc->vc_count ) {
466 /* wants the last entry in the list */
467 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
468 so->so_vlv_target = so->so_nentries;
469 } else if ( vc->vc_offset == 1 ) {
470 /* wants the first entry in the list */
471 cur_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
472 so->so_vlv_target = 1;
475 /* Just iterate to the right spot */
476 if ( vc->vc_count && vc->vc_count != so->so_nentries ) {
477 if ( vc->vc_offset > vc->vc_count )
479 target = so->so_nentries * vc->vc_offset / vc->vc_count;
481 if ( vc->vc_offset > so->so_nentries ) {
483 so->so_vlv_rc = LDAP_VLV_RANGE_ERROR;
484 pack_vlv_response_control( op, rs, so, ctrls );
486 slap_add_ctrls( op, rs, ctrls );
487 rs->sr_err = LDAP_VLV_ERROR;
490 target = vc->vc_offset;
492 so->so_vlv_target = target;
493 /* Start at left and go right, or start at right and go left? */
494 if ( target < so->so_nentries / 2 ) {
495 cur_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
496 dir = TAVL_DIR_RIGHT;
498 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
500 target = so->so_nentries - target + 1;
502 for ( i=1; i<target; i++ )
503 cur_node = tavl_next( cur_node, dir );
506 /* we're looking for a specific value */
507 sort_ctrl *sc = so->so_ctrl;
508 MatchingRule *mr = sc->sc_keys[0].sk_ordering;
512 if ( mr->smr_normalize ) {
513 rc = mr->smr_normalize( SLAP_MR_VALUE_OF_SYNTAX,
514 mr->smr_syntax, mr, &vc->vc_value, &bv, op->o_tmpmemctx );
516 so->so_vlv_rc = LDAP_INAPPROPRIATE_MATCHING;
517 pack_vlv_response_control( op, rs, so, ctrls );
519 slap_add_ctrls( op, rs, ctrls );
520 rs->sr_err = LDAP_VLV_ERROR;
527 sn = op->o_tmpalloc( sizeof(sort_node) +
528 sc->sc_nkeys * sizeof(struct berval), op->o_tmpmemctx );
529 sn->sn_vals = (struct berval *)(sn+1);
530 sn->sn_conn = op->o_conn->c_conn_idx;
531 sn->sn_session = find_session_by_so( so->so_info->svi_max_percon, op->o_conn->c_conn_idx, so );
533 for (i=1; i<sc->sc_nkeys; i++) {
534 BER_BVZERO( &sn->sn_vals[i] );
536 cur_node = tavl_find3( so->so_tree, sn, node_cmp, &j );
537 /* didn't find >= match */
540 cur_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
542 op->o_tmpfree( sn, op->o_tmpmemctx );
545 so->so_vlv_target = so->so_nentries + 1;
547 sort_node *sn = so->so_tree->avl_data;
548 /* start from the left or the right side? */
549 mr->smr_match( &i, 0, mr->smr_syntax, mr, &bv, &sn->sn_vals[0] );
551 tmp_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
554 tmp_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
555 dir = TAVL_DIR_RIGHT;
557 for (i=0; tmp_node != cur_node;
558 tmp_node = tavl_next( tmp_node, dir ), i++);
559 so->so_vlv_target = (dir == TAVL_DIR_RIGHT) ? i+1 : so->so_nentries - i;
561 if ( bv.bv_val != vc->vc_value.bv_val )
562 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
566 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
570 for ( ; i<vc->vc_before; i++ ) {
571 tmp_node = tavl_next( cur_node, TAVL_DIR_LEFT );
572 if ( !tmp_node ) break;
575 j = i + vc->vc_after + 1;
577 for ( i=0; i<j; i++ ) {
578 sort_node *sn = cur_node->avl_data;
580 if ( slapd_shutdown ) break;
582 op->o_bd = select_backend( &sn->sn_dn, 0 );
584 rc = be_entry_get_rw( op, &sn->sn_dn, NULL, NULL, 0, &e );
586 if ( e && rc == LDAP_SUCCESS ) {
588 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
589 rs->sr_err = send_search_entry( op, rs );
590 if ( rs->sr_err == LDAP_UNAVAILABLE )
593 cur_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
594 if ( !cur_node ) break;
596 so->so_vlv_rc = LDAP_SUCCESS;
601 static void send_page( Operation *op, SlapReply *rs, sort_op *so )
603 TAvlnode *cur_node = so->so_tree;
604 TAvlnode *next_node = NULL;
605 BackendDB *be = op->o_bd;
609 rs->sr_attrs = op->ors_attrs;
611 while ( cur_node && rs->sr_nentries < so->so_page_size ) {
612 sort_node *sn = cur_node->avl_data;
614 if ( slapd_shutdown ) break;
616 next_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
618 op->o_bd = select_backend( &sn->sn_dn, 0 );
620 rc = be_entry_get_rw( op, &sn->sn_dn, NULL, NULL, 0, &e );
622 ch_free( cur_node->avl_data );
623 ber_memfree( cur_node );
625 cur_node = next_node;
628 if ( e && rc == LDAP_SUCCESS ) {
630 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
631 rs->sr_err = send_search_entry( op, rs );
632 if ( rs->sr_err == LDAP_UNAVAILABLE )
637 /* Set the first entry to send for the next page */
638 so->so_tree = next_node;
640 next_node->avl_left = NULL;
645 static void send_entry(
650 Debug(LDAP_DEBUG_TRACE,
651 "%s: response control: status=%d, text=%s\n",
652 debug_header, rs->sr_err, SAFESTR(rs->sr_text, "<None>"));
657 /* RFC 2891: If critical then send the entries iff they were
658 * succesfully sorted. If non-critical send all entries
659 * whether they were sorted or not.
661 if ( (op->o_ctrlflag[sss_cid] != SLAP_CONTROL_CRITICAL) ||
662 (rs->sr_err == LDAP_SUCCESS) )
664 if ( so->so_vlv > SLAP_CONTROL_IGNORED ) {
665 send_list( op, rs, so );
667 /* Get the first node to send */
668 TAvlnode *start_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
669 so->so_tree = start_node;
671 if ( so->so_paged <= SLAP_CONTROL_IGNORED ) {
672 /* Not paged result search. Send all entries.
673 * Set the page size to the number of entries
674 * so that send_page() will send all entries.
676 so->so_page_size = so->so_nentries;
679 send_page( op, rs, so );
684 static void send_result(
689 LDAPControl *ctrls[3];
692 rc = pack_sss_response_control( op, rs, ctrls );
693 if ( rc == LDAP_SUCCESS ) {
696 if ( so->so_paged > SLAP_CONTROL_IGNORED ) {
697 rc = pack_pagedresult_response_control( op, rs, so, ctrls+1 );
698 } else if ( so->so_vlv > SLAP_CONTROL_IGNORED ) {
699 rc = pack_vlv_response_control( op, rs, so, ctrls+1 );
701 if ( rc == LDAP_SUCCESS )
706 if ( ctrls[0] != NULL )
707 slap_add_ctrls( op, rs, ctrls );
708 send_ldap_result( op, rs );
710 if ( so->so_tree == NULL ) {
711 /* Search finished, so clean up */
712 free_sort_op( op->o_conn, so );
718 static int sssvlv_op_response(
722 sort_ctrl *sc = op->o_controls[sss_cid];
723 sort_op *so = op->o_callback->sc_private;
725 if ( rs->sr_type == REP_SEARCH ) {
732 len = sizeof(sort_node) + sc->sc_nkeys * sizeof(struct berval) +
733 rs->sr_entry->e_nname.bv_len + 1;
734 sn = op->o_tmpalloc( len, op->o_tmpmemctx );
735 sn->sn_vals = (struct berval *)(sn+1);
737 /* Build tmp list of key values */
738 for ( i=0; i<sc->sc_nkeys; i++ ) {
739 Attribute *a = attr_find( rs->sr_entry->e_attrs,
740 sc->sc_keys[i].sk_ad );
742 if ( a->a_numvals > 1 ) {
743 bv = select_value( a, &sc->sc_keys[i] );
747 sn->sn_vals[i] = *bv;
748 len += bv->bv_len + 1;
750 BER_BVZERO( &sn->sn_vals[i] );
754 /* Now dup into regular memory */
755 sn2 = ch_malloc( len );
756 sn2->sn_vals = (struct berval *)(sn2+1);
757 AC_MEMCPY( sn2->sn_vals, sn->sn_vals,
758 sc->sc_nkeys * sizeof(struct berval));
760 ptr = (char *)(sn2->sn_vals + sc->sc_nkeys);
761 sn2->sn_dn.bv_val = ptr;
762 sn2->sn_dn.bv_len = rs->sr_entry->e_nname.bv_len;
763 AC_MEMCPY( ptr, rs->sr_entry->e_nname.bv_val,
764 rs->sr_entry->e_nname.bv_len );
765 ptr += rs->sr_entry->e_nname.bv_len;
767 for ( i=0; i<sc->sc_nkeys; i++ ) {
768 if ( !BER_BVISNULL( &sn2->sn_vals[i] )) {
769 AC_MEMCPY(ptr, sn2->sn_vals[i].bv_val, sn2->sn_vals[i].bv_len);
770 sn2->sn_vals[i].bv_val = ptr;
771 ptr += sn2->sn_vals[i].bv_len;
775 op->o_tmpfree( sn, op->o_tmpmemctx );
777 sn->sn_conn = op->o_conn->c_conn_idx;
778 sn->sn_session = find_session_by_so( so->so_info->svi_max_percon, op->o_conn->c_conn_idx, so );
780 /* Insert into the AVL tree */
781 tavl_insert(&(so->so_tree), sn, node_insert, avl_dup_error);
785 /* Collected the keys so that they can be sorted. Thus, stop
786 * the entry from propagating.
788 rs->sr_err = LDAP_SUCCESS;
790 else if ( rs->sr_type == REP_RESULT ) {
791 /* Remove serversort response callback.
792 * We don't want the entries that we are about to send to be
793 * processed by serversort response again.
795 if ( op->o_callback->sc_response == sssvlv_op_response ) {
796 op->o_callback = op->o_callback->sc_next;
799 send_entry( op, rs, so );
800 send_result( op, rs, so );
806 static int sssvlv_op_search(
810 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
811 sssvlv_info *si = on->on_bi.bi_private;
812 int rc = SLAP_CB_CONTINUE;
814 sort_op *so = NULL, so2;
816 PagedResultsState *ps;
820 if ( op->o_ctrlflag[sss_cid] <= SLAP_CONTROL_IGNORED ) {
821 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
822 LDAPControl *ctrls[2];
824 so2.so_vlv_target = 0;
826 so2.so_vlv_rc = LDAP_VLV_SSS_MISSING;
827 so2.so_vlv = op->o_ctrlflag[vlv_cid];
828 rc = pack_vlv_response_control( op, rs, &so2, ctrls );
829 if ( rc == LDAP_SUCCESS ) {
831 slap_add_ctrls( op, rs, ctrls );
833 rs->sr_err = LDAP_VLV_ERROR;
834 rs->sr_text = "Sort control is required with VLV";
837 /* Not server side sort so just continue */
838 return SLAP_CB_CONTINUE;
841 Debug(LDAP_DEBUG_TRACE,
842 "==> sssvlv_search: <%s> %s, control flag: %d\n",
843 op->o_req_dn.bv_val, op->ors_filterstr.bv_val,
844 op->o_ctrlflag[sss_cid]);
846 sc = op->o_controls[sss_cid];
847 if ( sc->sc_nkeys > si->svi_max_keys ) {
848 rs->sr_text = "Too many sort keys";
849 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
853 ps = ( op->o_pagedresults > SLAP_CONTROL_IGNORED ) ?
854 (PagedResultsState*)(op->o_pagedresults_state) : NULL;
855 vc = op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ?
856 op->o_controls[vlv_cid] : NULL;
859 rs->sr_text = "VLV incompatible with PagedResults";
860 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
865 ldap_pvt_thread_mutex_lock( &sort_conns_mutex );
866 /* Is there already a sort running on this conn? */
867 sess_id = find_session_by_context( si->svi_max_percon, op->o_conn->c_conn_idx, vc ? vc->vc_context : NO_VC_CONTEXT, ps ? ps->ps_cookie : NO_PS_COOKIE );
868 if ( sess_id >= 0 ) {
869 so = sort_conns[op->o_conn->c_conn_idx][sess_id];
871 if( so->so_running > 0 ){
872 /* another thread is handling, response busy to client */
877 /* Is it a continuation of a VLV search? */
878 if ( !vc || so->so_vlv <= SLAP_CONTROL_IGNORED ||
879 vc->vc_context != so->so_vcontext ) {
880 /* Is it a continuation of a paged search? */
881 if ( !ps || so->so_paged <= SLAP_CONTROL_IGNORED ||
882 op->o_conn->c_pagedresults_state.ps_cookie != ps->ps_cookie ) {
884 } else if ( !ps->ps_size ) {
885 /* Abandoning current request */
888 rs->sr_err = LDAP_SUCCESS;
891 if (( vc && so->so_paged > SLAP_CONTROL_IGNORED ) ||
892 ( ps && so->so_vlv > SLAP_CONTROL_IGNORED )) {
893 /* changed from paged to vlv or vice versa, abandon */
896 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
900 /* occupy before mutex unlock */
905 /* Are there too many running overall? */
906 } else if ( si->svi_num >= si->svi_max ) {
908 } else if ( ( sess_id = find_next_session(si->svi_max_percon, op->o_conn->c_conn_idx ) ) < 0 ) {
911 /* OK, this connection now has a sort running */
913 sort_conns[op->o_conn->c_conn_idx][sess_id] = &so2;
914 sort_conns[op->o_conn->c_conn_idx][sess_id]->so_session = sess_id;
916 ldap_pvt_thread_mutex_unlock( &sort_conns_mutex );
918 /* If we're a global overlay, this check got bypassed */
919 if ( !op->ors_limit && limits_check( op, rs ))
921 /* are we continuing a VLV search? */
922 if ( so && vc && vc->vc_context ) {
924 send_list( op, rs, so );
925 send_result( op, rs, so );
927 /* are we continuing a paged search? */
928 } else if ( so && ps && ps->ps_cookie ) {
930 send_page( op, rs, so );
931 send_result( op, rs, so );
934 slap_callback *cb = op->o_tmpalloc( sizeof(slap_callback),
936 /* Install serversort response callback to handle a new search */
938 so = ch_calloc( 1, sizeof(sort_op));
940 so = op->o_tmpcalloc( 1, sizeof(sort_op), op->o_tmpmemctx );
942 sort_conns[op->o_conn->c_conn_idx][sess_id] = so;
944 cb->sc_cleanup = NULL;
945 cb->sc_response = sssvlv_op_response;
946 cb->sc_next = op->o_callback;
948 cb->sc_writewait = NULL;
954 so->so_paged = op->o_pagedresults;
955 so->so_page_size = ps->ps_size;
956 op->o_pagedresults = SLAP_CONTROL_IGNORED;
959 so->so_page_size = 0;
961 so->so_vlv = op->o_ctrlflag[vlv_cid];
962 so->so_vlv_target = 0;
965 so->so_vlv = SLAP_CONTROL_NONE;
968 so->so_session = sess_id;
969 so->so_vlv = op->o_ctrlflag[vlv_cid];
970 so->so_vcontext = (unsigned long)so;
977 if ( so && !so->so_nentries ) {
978 free_sort_op( op->o_conn, so );
980 rs->sr_text = "Other sort requests already in progress";
981 rs->sr_err = LDAP_BUSY;
985 send_ldap_result( op, rs );
991 static int get_ordering_rule(
992 AttributeDescription *ad,
993 struct berval *matchrule,
995 MatchingRule **ordering )
999 if ( matchrule && matchrule->bv_val ) {
1000 mr = mr_find( matchrule->bv_val );
1002 rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
1003 rs->sr_text = "serverSort control: No ordering rule";
1004 Debug(LDAP_DEBUG_TRACE, "%s: no ordering rule function for %s\n",
1005 debug_header, matchrule->bv_val, 0);
1009 mr = ad->ad_type->sat_ordering;
1011 rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
1012 rs->sr_text = "serverSort control: No ordering rule";
1013 Debug(LDAP_DEBUG_TRACE,
1014 "%s: no ordering rule specified and no default ordering rule for attribute %s\n",
1015 debug_header, ad->ad_cname.bv_val, 0);
1023 static int count_key(BerElement *ber)
1030 /* Server Side Sort Control is a SEQUENCE of SEQUENCE */
1031 for ( tag = ber_first_element( ber, &len, &end );
1032 tag == LBER_SEQUENCE;
1033 tag = ber_next_element( ber, &len, end ))
1035 tag = ber_skip_tag( ber, &len );
1036 ber_skip_data( ber, len );
1044 static int build_key(
1050 struct berval matchrule = BER_BVNULL;
1051 ber_int_t reverse = 0;
1054 MatchingRule *ordering = NULL;
1055 AttributeDescription *ad = NULL;
1058 if (( tag = ber_scanf( ber, "{" )) == LBER_ERROR ) {
1059 rs->sr_text = "serverSort control: decoding error";
1060 rs->sr_err = LDAP_PROTOCOL_ERROR;
1064 if (( tag = ber_scanf( ber, "m", &attr )) == LBER_ERROR ) {
1065 rs->sr_text = "serverSort control: attribute decoding error";
1066 rs->sr_err = LDAP_PROTOCOL_ERROR;
1070 tag = ber_peek_tag( ber, &len );
1071 if ( tag == LDAP_MATCHRULE_IDENTIFIER ) {
1072 if (( tag = ber_scanf( ber, "m", &matchrule )) == LBER_ERROR ) {
1073 rs->sr_text = "serverSort control: matchrule decoding error";
1074 rs->sr_err = LDAP_PROTOCOL_ERROR;
1077 tag = ber_peek_tag( ber, &len );
1080 if ( tag == LDAP_REVERSEORDER_IDENTIFIER ) {
1081 if (( tag = ber_scanf( ber, "b", &reverse )) == LBER_ERROR ) {
1082 rs->sr_text = "serverSort control: reverse decoding error";
1083 rs->sr_err = LDAP_PROTOCOL_ERROR;
1088 if (( tag = ber_scanf( ber, "}" )) == LBER_ERROR ) {
1089 rs->sr_text = "serverSort control: decoding error";
1090 rs->sr_err = LDAP_PROTOCOL_ERROR;
1094 if ( slap_bv2ad( &attr, &ad, &text ) != LDAP_SUCCESS ) {
1096 "serverSort control: Unrecognized attribute type in sort key";
1097 Debug(LDAP_DEBUG_TRACE,
1098 "%s: Unrecognized attribute type in sort key: %s\n",
1099 debug_header, SAFESTR(attr.bv_val, "<None>"), 0);
1100 rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
1104 /* get_ordering_rule will set sr_err and sr_text */
1105 get_ordering_rule( ad, &matchrule, rs, &ordering );
1106 if ( rs->sr_err != LDAP_SUCCESS ) {
1111 key->sk_ordering = ordering;
1112 key->sk_direction = reverse ? -1 : 1;
1117 /* Conforms to RFC4510 re: Criticality, original RFC2891 spec is broken
1118 * Also see ITS#7253 for discussion
1120 static int sss_parseCtrl(
1125 BerElementBuffer berbuf;
1132 rs->sr_err = LDAP_PROTOCOL_ERROR;
1134 if ( op->o_ctrlflag[sss_cid] > SLAP_CONTROL_IGNORED ) {
1135 rs->sr_text = "sorted results control specified multiple times";
1136 } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1137 rs->sr_text = "sorted results control value is absent";
1138 } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1139 rs->sr_text = "sorted results control value is empty";
1141 rs->sr_err = LDAP_SUCCESS;
1143 if ( rs->sr_err != LDAP_SUCCESS )
1146 op->o_ctrlflag[sss_cid] = ctrl->ldctl_iscritical ?
1147 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1149 ber = (BerElement *)&berbuf;
1150 ber_init2( ber, &ctrl->ldctl_value, 0 );
1151 i = count_key( ber );
1153 sc = op->o_tmpalloc( sizeof(sort_ctrl) +
1154 (i-1) * sizeof(sort_key), op->o_tmpmemctx );
1156 op->o_controls[sss_cid] = sc;
1158 /* peel off initial sequence */
1159 ber_scanf( ber, "{" );
1163 if ( build_key( ber, rs, &sc->sc_keys[i] ) != LDAP_SUCCESS )
1166 tag = ber_peek_tag( ber, &len );
1167 } while ( tag != LBER_DEFAULT );
1172 static int vlv_parseCtrl(
1177 BerElementBuffer berbuf;
1183 rs->sr_err = LDAP_PROTOCOL_ERROR;
1186 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
1187 rs->sr_text = "vlv control specified multiple times";
1188 } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1189 rs->sr_text = "vlv control value is absent";
1190 } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1191 rs->sr_text = "vlv control value is empty";
1193 if ( rs->sr_text != NULL )
1196 op->o_ctrlflag[vlv_cid] = ctrl->ldctl_iscritical ?
1197 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1199 ber = (BerElement *)&berbuf;
1200 ber_init2( ber, &ctrl->ldctl_value, 0 );
1202 rs->sr_err = LDAP_PROTOCOL_ERROR;
1204 tag = ber_scanf( ber, "{ii", &vc2.vc_before, &vc2.vc_after );
1205 if ( tag == LBER_ERROR ) {
1209 tag = ber_peek_tag( ber, &len );
1210 if ( tag == LDAP_VLVBYINDEX_IDENTIFIER ) {
1211 tag = ber_scanf( ber, "{ii}", &vc2.vc_offset, &vc2.vc_count );
1212 if ( tag == LBER_ERROR )
1214 BER_BVZERO( &vc2.vc_value );
1215 } else if ( tag == LDAP_VLVBYVALUE_IDENTIFIER ) {
1216 tag = ber_scanf( ber, "m", &vc2.vc_value );
1217 if ( tag == LBER_ERROR || BER_BVISNULL( &vc2.vc_value ))
1222 tag = ber_peek_tag( ber, &len );
1223 if ( tag == LDAP_VLVCONTEXT_IDENTIFIER ) {
1225 tag = ber_scanf( ber, "m", &bv );
1226 if ( tag == LBER_ERROR || bv.bv_len != sizeof(vc2.vc_context))
1228 AC_MEMCPY( &vc2.vc_context, bv.bv_val, bv.bv_len );
1233 vc = op->o_tmpalloc( sizeof(vlv_ctrl), op->o_tmpmemctx );
1235 op->o_controls[vlv_cid] = vc;
1236 rs->sr_err = LDAP_SUCCESS;
1241 static int sssvlv_connection_destroy( BackendDB *be, Connection *conn )
1243 slap_overinst *on = (slap_overinst *)be->bd_info;
1244 sssvlv_info *si = on->on_bi.bi_private;
1246 if ( sort_conns[conn->c_conn_idx] ) {
1247 free_sort_ops( conn, sort_conns[conn->c_conn_idx], si->svi_max_percon );
1250 return LDAP_SUCCESS;
1253 static int sssvlv_db_open(
1257 slap_overinst *on = (slap_overinst *)be->bd_info;
1258 sssvlv_info *si = on->on_bi.bi_private;
1262 /* If not set, default to 1/2 of available threads */
1264 si->svi_max = connection_pool_max / 2;
1266 if ( dtblsize && !sort_conns ) {
1267 ldap_pvt_thread_mutex_init( &sort_conns_mutex );
1268 /* accommodate for c_conn_idx == -1 */
1269 sort_conns = ch_calloc( dtblsize + 1, sizeof(sort_op **) );
1270 for ( conn_index = 0 ; conn_index < dtblsize + 1 ; conn_index++ ) {
1271 sort_conns[conn_index] = ch_calloc( si->svi_max_percon, sizeof(sort_op *) );
1276 rc = overlay_register_control( be, LDAP_CONTROL_SORTREQUEST );
1277 if ( rc == LDAP_SUCCESS )
1278 rc = overlay_register_control( be, LDAP_CONTROL_VLVREQUEST );
1282 static ConfigTable sssvlv_cfg[] = {
1283 { "sssvlv-max", "num",
1284 2, 2, 0, ARG_INT|ARG_OFFSET,
1285 (void *)offsetof(sssvlv_info, svi_max),
1286 "( OLcfgOvAt:21.1 NAME 'olcSssVlvMax' "
1287 "DESC 'Maximum number of concurrent Sort requests' "
1288 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
1289 { "sssvlv-maxkeys", "num",
1290 2, 2, 0, ARG_INT|ARG_OFFSET,
1291 (void *)offsetof(sssvlv_info, svi_max_keys),
1292 "( OLcfgOvAt:21.2 NAME 'olcSssVlvMaxKeys' "
1293 "DESC 'Maximum number of Keys in a Sort request' "
1294 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
1295 { "sssvlv-maxperconn", "num",
1296 2, 2, 0, ARG_INT|ARG_OFFSET,
1297 (void *)offsetof(sssvlv_info, svi_max_percon),
1298 "( OLcfgOvAt:21.3 NAME 'olcSssVlvMaxPerConn' "
1299 "DESC 'Maximum number of concurrent paged search requests per connection' "
1300 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
1301 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1304 static ConfigOCs sssvlv_ocs[] = {
1305 { "( OLcfgOvOc:21.1 "
1306 "NAME 'olcSssVlvConfig' "
1307 "DESC 'SSS VLV configuration' "
1308 "SUP olcOverlayConfig "
1309 "MAY ( olcSssVlvMax $ olcSssVlvMaxKeys $ olcSssVlvMaxPerConn ) )",
1310 Cft_Overlay, sssvlv_cfg, NULL, NULL },
1314 static int sssvlv_db_init(
1318 slap_overinst *on = (slap_overinst *)be->bd_info;
1321 if ( ov_count == 0 ) {
1324 rc = register_supported_control2( LDAP_CONTROL_SORTREQUEST,
1330 if ( rc != LDAP_SUCCESS ) {
1331 Debug( LDAP_DEBUG_ANY, "Failed to register Sort Request control '%s' (%d)\n",
1332 LDAP_CONTROL_SORTREQUEST, rc, 0 );
1336 rc = register_supported_control2( LDAP_CONTROL_VLVREQUEST,
1342 if ( rc != LDAP_SUCCESS ) {
1343 Debug( LDAP_DEBUG_ANY, "Failed to register VLV Request control '%s' (%d)\n",
1344 LDAP_CONTROL_VLVREQUEST, rc, 0 );
1345 #ifdef SLAP_CONFIG_DELETE
1346 overlay_unregister_control( be, LDAP_CONTROL_SORTREQUEST );
1347 unregister_supported_control( LDAP_CONTROL_SORTREQUEST );
1348 #endif /* SLAP_CONFIG_DELETE */
1353 si = (sssvlv_info *)ch_malloc(sizeof(sssvlv_info));
1354 on->on_bi.bi_private = si;
1358 si->svi_max_keys = SSSVLV_DEFAULT_MAX_KEYS;
1359 si->svi_max_percon = SSSVLV_DEFAULT_MAX_REQUEST_PER_CONN;
1363 return LDAP_SUCCESS;
1366 static int sssvlv_db_destroy(
1370 slap_overinst *on = (slap_overinst *)be->bd_info;
1371 sssvlv_info *si = (sssvlv_info *)on->on_bi.bi_private;
1375 if ( !ov_count && sort_conns) {
1377 for ( conn_index = 0 ; conn_index < dtblsize + 1 ; conn_index++ ) {
1378 ch_free(sort_conns[conn_index]);
1380 ch_free(sort_conns);
1381 ldap_pvt_thread_mutex_destroy( &sort_conns_mutex );
1384 #ifdef SLAP_CONFIG_DELETE
1385 overlay_unregister_control( be, LDAP_CONTROL_SORTREQUEST );
1386 overlay_unregister_control( be, LDAP_CONTROL_VLVREQUEST );
1387 if ( ov_count == 0 ) {
1388 unregister_supported_control( LDAP_CONTROL_SORTREQUEST );
1389 unregister_supported_control( LDAP_CONTROL_VLVREQUEST );
1391 #endif /* SLAP_CONFIG_DELETE */
1395 on->on_bi.bi_private = NULL;
1397 return LDAP_SUCCESS;
1400 static slap_overinst sssvlv;
1402 int sssvlv_initialize()
1406 sssvlv.on_bi.bi_type = "sssvlv";
1407 sssvlv.on_bi.bi_db_init = sssvlv_db_init;
1408 sssvlv.on_bi.bi_db_destroy = sssvlv_db_destroy;
1409 sssvlv.on_bi.bi_db_open = sssvlv_db_open;
1410 sssvlv.on_bi.bi_connection_destroy = sssvlv_connection_destroy;
1411 sssvlv.on_bi.bi_op_search = sssvlv_op_search;
1413 sssvlv.on_bi.bi_cf_ocs = sssvlv_ocs;
1415 rc = config_register_schema( sssvlv_cfg, sssvlv_ocs );
1419 rc = overlay_register( &sssvlv );
1420 if ( rc != LDAP_SUCCESS ) {
1421 Debug( LDAP_DEBUG_ANY, "Failed to register server side sort overlay\n", 0, 0, 0 );
1427 #if SLAPD_OVER_SSSVLV == SLAPD_MOD_DYNAMIC
1428 int init_module( int argc, char *argv[])
1430 return sssvlv_initialize();
1434 #endif /* SLAPD_OVER_SSSVLV */