1 /* sssvlv.c - server side sort / virtual list view */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2009-2013 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;
121 /* There is only one conn table for all overlay instances */
122 /* Each conn can handle one session by context */
123 static sort_op ***sort_conns;
124 static ldap_pvt_thread_mutex_t sort_conns_mutex;
126 static const char *debug_header = "sssvlv";
131 /* RFC 2981 Section 2.2
132 * If a sort key is a multi-valued attribute, and an entry happens to
133 * have multiple values for that attribute and no other controls are
134 * present that affect the sorting order, then the server SHOULD use the
135 * least value (according to the ORDERING rule for that attribute).
137 static struct berval* select_value(
141 struct berval* ber1, *ber2;
142 MatchingRule *mr = key->sk_ordering;
146 ber1 = &(attr->a_nvals[0]);
148 for ( i = 1; i < attr->a_numvals; i++,ber2++ ) {
149 mr->smr_match( &cmp, 0, mr->smr_syntax, mr, ber1, ber2 );
155 Debug(LDAP_DEBUG_TRACE, "%s: value selected for compare: %s\n",
157 SAFESTR(ber1->bv_val, "<Empty>"),
163 static int node_cmp( const void* val1, const void* val2 )
165 sort_node *sn1 = (sort_node *)val1;
166 sort_node *sn2 = (sort_node *)val2;
170 assert( sort_conns[sn1->sn_conn]
171 && sort_conns[sn1->sn_conn][sn1->sn_session]
172 && sort_conns[sn1->sn_conn][sn1->sn_session]->so_ctrl );
173 sc = sort_conns[sn1->sn_conn][sn1->sn_session]->so_ctrl;
175 for ( i=0; cmp == 0 && i<sc->sc_nkeys; i++ ) {
176 if ( BER_BVISNULL( &sn1->sn_vals[i] )) {
177 if ( BER_BVISNULL( &sn2->sn_vals[i] ))
180 cmp = sc->sc_keys[i].sk_direction;
181 } else if ( BER_BVISNULL( &sn2->sn_vals[i] )) {
182 cmp = sc->sc_keys[i].sk_direction * -1;
184 mr = sc->sc_keys[i].sk_ordering;
185 mr->smr_match( &cmp, 0, mr->smr_syntax, mr,
186 &sn1->sn_vals[i], &sn2->sn_vals[i] );
188 cmp *= sc->sc_keys[i].sk_direction;
194 static int node_insert( const void *val1, const void *val2 )
196 /* Never return equal so that new entries are always inserted */
197 return node_cmp( val1, val2 ) < 0 ? -1 : 1;
200 static int pack_vlv_response_control(
204 LDAPControl **ctrlsp )
207 BerElementBuffer berbuf;
208 BerElement *ber = (BerElement *)&berbuf;
209 struct berval cookie, bv;
212 ber_init2( ber, NULL, LBER_USE_DER );
213 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
215 rc = ber_printf( ber, "{iie", so->so_vlv_target, so->so_nentries,
218 if ( rc != -1 && so->so_vcontext ) {
219 cookie.bv_val = (char *)&so->so_vcontext;
220 cookie.bv_len = sizeof(so->so_vcontext);
221 rc = ber_printf( ber, "tO", LDAP_VLVCONTEXT_IDENTIFIER, &cookie );
225 rc = ber_printf( ber, "}" );
229 rc = ber_flatten2( ber, &bv, 0 );
233 ctrl = (LDAPControl *)op->o_tmpalloc( sizeof(LDAPControl)+
234 bv.bv_len, op->o_tmpmemctx );
235 ctrl->ldctl_oid = LDAP_CONTROL_VLVRESPONSE;
236 ctrl->ldctl_iscritical = 0;
237 ctrl->ldctl_value.bv_val = (char *)(ctrl+1);
238 ctrl->ldctl_value.bv_len = bv.bv_len;
239 AC_MEMCPY( ctrl->ldctl_value.bv_val, bv.bv_val, bv.bv_len );
243 rs->sr_err = LDAP_OTHER;
251 static int pack_pagedresult_response_control(
255 LDAPControl **ctrlsp )
258 BerElementBuffer berbuf;
259 BerElement *ber = (BerElement *)&berbuf;
260 PagedResultsCookie resp_cookie;
261 struct berval cookie, bv;
264 ber_init2( ber, NULL, LBER_USE_DER );
265 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
267 if ( so->so_nentries > 0 ) {
268 resp_cookie = ( PagedResultsCookie )so->so_tree;
269 cookie.bv_len = sizeof( PagedResultsCookie );
270 cookie.bv_val = (char *)&resp_cookie;
272 resp_cookie = ( PagedResultsCookie )0;
273 BER_BVZERO( &cookie );
276 op->o_conn->c_pagedresults_state.ps_cookie = resp_cookie;
277 op->o_conn->c_pagedresults_state.ps_count
278 = ((PagedResultsState *)op->o_pagedresults_state)->ps_count
281 rc = ber_printf( ber, "{iO}", so->so_nentries, &cookie );
283 rc = ber_flatten2( ber, &bv, 0 );
287 ctrl = (LDAPControl *)op->o_tmpalloc( sizeof(LDAPControl)+
288 bv.bv_len, op->o_tmpmemctx );
289 ctrl->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
290 ctrl->ldctl_iscritical = 0;
291 ctrl->ldctl_value.bv_val = (char *)(ctrl+1);
292 ctrl->ldctl_value.bv_len = bv.bv_len;
293 AC_MEMCPY( ctrl->ldctl_value.bv_val, bv.bv_val, bv.bv_len );
297 rs->sr_err = LDAP_OTHER;
305 static int pack_sss_response_control(
308 LDAPControl **ctrlsp )
311 BerElementBuffer berbuf;
312 BerElement *ber = (BerElement *)&berbuf;
316 ber_init2( ber, NULL, LBER_USE_DER );
317 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
319 /* Pack error code */
320 rc = ber_printf(ber, "{e}", rs->sr_err);
323 rc = ber_flatten2( ber, &bv, 0 );
326 ctrl = (LDAPControl *)op->o_tmpalloc( sizeof(LDAPControl)+
327 bv.bv_len, op->o_tmpmemctx );
328 ctrl->ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
329 ctrl->ldctl_iscritical = 0;
330 ctrl->ldctl_value.bv_val = (char *)(ctrl+1);
331 ctrl->ldctl_value.bv_len = bv.bv_len;
332 AC_MEMCPY( ctrl->ldctl_value.bv_val, bv.bv_val, bv.bv_len );
336 rs->sr_err = LDAP_OTHER;
344 /* Return the session id or -1 if unknown */
345 static int find_session_by_so(
354 for (sess_id = 0; sess_id < svi_max_percon; sess_id++) {
355 if ( sort_conns[conn_id] && sort_conns[conn_id][sess_id] == so )
361 /* Return the session id or -1 if unknown */
362 static int find_session_by_context(
365 unsigned long vc_context,
366 PagedResultsCookie ps_cookie )
369 for(sess_id = 0; sess_id < svi_max_percon; sess_id++) {
370 if( sort_conns[conn_id] && sort_conns[conn_id][sess_id] &&
371 ( sort_conns[conn_id][sess_id]->so_vcontext == vc_context ||
372 (PagedResultsCookie) sort_conns[conn_id][sess_id]->so_tree == ps_cookie ) )
378 static int find_next_session(
383 assert(sort_conns[conn_id] != NULL);
384 for(sess_id = 0; sess_id < svi_max_percon; sess_id++) {
385 if(!sort_conns[conn_id][sess_id]) {
389 if (sess_id >= svi_max_percon) {
396 static void free_sort_op( Connection *conn, sort_op *so )
400 tavl_free( so->so_tree, ch_free );
404 ldap_pvt_thread_mutex_lock( &sort_conns_mutex );
405 sess_id = find_session_by_so( so->so_info->svi_max_percon, conn->c_conn_idx, so );
406 sort_conns[conn->c_conn_idx][sess_id] = NULL;
407 so->so_info->svi_num--;
408 ldap_pvt_thread_mutex_unlock( &sort_conns_mutex );
413 static void free_sort_ops( Connection *conn, sort_op **sos, int svi_max_percon )
418 for( sess_id = 0; sess_id < svi_max_percon ; sess_id++ ) {
419 so = sort_conns[conn->c_conn_idx][sess_id];
421 free_sort_op( conn, so );
422 sort_conns[conn->c_conn_idx][sess_id] = NULL;
427 static void send_list(
432 Avlnode *cur_node, *tmp_node;
433 vlv_ctrl *vc = op->o_controls[vlv_cid];
437 LDAPControl *ctrls[2];
439 rs->sr_attrs = op->ors_attrs;
441 /* FIXME: it may be better to just flatten the tree into
442 * an array before doing all of this...
445 /* Are we just counting an offset? */
446 if ( BER_BVISNULL( &vc->vc_value )) {
447 if ( vc->vc_offset == vc->vc_count ) {
448 /* wants the last entry in the list */
449 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
450 so->so_vlv_target = so->so_nentries;
451 } else if ( vc->vc_offset == 1 ) {
452 /* wants the first entry in the list */
453 cur_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
454 so->so_vlv_target = 1;
457 /* Just iterate to the right spot */
458 if ( vc->vc_count && vc->vc_count != so->so_nentries ) {
459 if ( vc->vc_offset > vc->vc_count )
461 target = so->so_nentries * vc->vc_offset / vc->vc_count;
463 if ( vc->vc_offset > so->so_nentries ) {
465 so->so_vlv_rc = LDAP_VLV_RANGE_ERROR;
466 pack_vlv_response_control( op, rs, so, ctrls );
468 slap_add_ctrls( op, rs, ctrls );
469 rs->sr_err = LDAP_VLV_ERROR;
472 target = vc->vc_offset;
474 so->so_vlv_target = target;
475 /* Start at left and go right, or start at right and go left? */
476 if ( target < so->so_nentries / 2 ) {
477 cur_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
478 dir = TAVL_DIR_RIGHT;
480 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
482 target = so->so_nentries - target + 1;
484 for ( i=1; i<target; i++ )
485 cur_node = tavl_next( cur_node, dir );
488 /* we're looking for a specific value */
489 sort_ctrl *sc = so->so_ctrl;
490 MatchingRule *mr = sc->sc_keys[0].sk_ordering;
494 if ( mr->smr_normalize ) {
495 rc = mr->smr_normalize( SLAP_MR_VALUE_OF_SYNTAX,
496 mr->smr_syntax, mr, &vc->vc_value, &bv, op->o_tmpmemctx );
498 so->so_vlv_rc = LDAP_INAPPROPRIATE_MATCHING;
499 pack_vlv_response_control( op, rs, so, ctrls );
501 slap_add_ctrls( op, rs, ctrls );
502 rs->sr_err = LDAP_VLV_ERROR;
509 sn = op->o_tmpalloc( sizeof(sort_node) +
510 sc->sc_nkeys * sizeof(struct berval), op->o_tmpmemctx );
511 sn->sn_vals = (struct berval *)(sn+1);
512 sn->sn_conn = op->o_conn->c_conn_idx;
513 sn->sn_session = find_session_by_so( so->so_info->svi_max_percon, op->o_conn->c_conn_idx, so );
515 for (i=1; i<sc->sc_nkeys; i++) {
516 BER_BVZERO( &sn->sn_vals[i] );
518 cur_node = tavl_find3( so->so_tree, sn, node_cmp, &j );
519 /* didn't find >= match */
522 cur_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
524 op->o_tmpfree( sn, op->o_tmpmemctx );
527 so->so_vlv_target = so->so_nentries + 1;
529 sort_node *sn = so->so_tree->avl_data;
530 /* start from the left or the right side? */
531 mr->smr_match( &i, 0, mr->smr_syntax, mr, &bv, &sn->sn_vals[0] );
533 tmp_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
536 tmp_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
537 dir = TAVL_DIR_RIGHT;
539 for (i=0; tmp_node != cur_node;
540 tmp_node = tavl_next( tmp_node, dir ), i++);
541 so->so_vlv_target = (dir == TAVL_DIR_RIGHT) ? i+1 : so->so_nentries - i;
543 if ( bv.bv_val != vc->vc_value.bv_val )
544 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
548 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
552 for ( ; i<vc->vc_before; i++ ) {
553 tmp_node = tavl_next( cur_node, TAVL_DIR_LEFT );
554 if ( !tmp_node ) break;
557 j = i + vc->vc_after + 1;
559 for ( i=0; i<j; i++ ) {
560 sort_node *sn = cur_node->avl_data;
562 if ( slapd_shutdown ) break;
564 op->o_bd = select_backend( &sn->sn_dn, 0 );
566 rc = be_entry_get_rw( op, &sn->sn_dn, NULL, NULL, 0, &e );
568 if ( e && rc == LDAP_SUCCESS ) {
570 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
571 rs->sr_err = send_search_entry( op, rs );
572 if ( rs->sr_err == LDAP_UNAVAILABLE )
575 cur_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
576 if ( !cur_node ) break;
578 so->so_vlv_rc = LDAP_SUCCESS;
583 static void send_page( Operation *op, SlapReply *rs, sort_op *so )
585 Avlnode *cur_node = so->so_tree;
586 Avlnode *next_node = NULL;
587 BackendDB *be = op->o_bd;
591 rs->sr_attrs = op->ors_attrs;
593 while ( cur_node && rs->sr_nentries < so->so_page_size ) {
594 sort_node *sn = cur_node->avl_data;
596 if ( slapd_shutdown ) break;
598 next_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
600 op->o_bd = select_backend( &sn->sn_dn, 0 );
602 rc = be_entry_get_rw( op, &sn->sn_dn, NULL, NULL, 0, &e );
604 ch_free( cur_node->avl_data );
605 ber_memfree( cur_node );
607 cur_node = next_node;
610 if ( e && rc == LDAP_SUCCESS ) {
612 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
613 rs->sr_err = send_search_entry( op, rs );
614 if ( rs->sr_err == LDAP_UNAVAILABLE )
619 /* Set the first entry to send for the next page */
620 so->so_tree = next_node;
622 next_node->avl_left = NULL;
627 static void send_entry(
632 Debug(LDAP_DEBUG_TRACE,
633 "%s: response control: status=%d, text=%s\n",
634 debug_header, rs->sr_err, SAFESTR(rs->sr_text, "<None>"));
639 /* RFC 2891: If critical then send the entries iff they were
640 * succesfully sorted. If non-critical send all entries
641 * whether they were sorted or not.
643 if ( (op->o_ctrlflag[sss_cid] != SLAP_CONTROL_CRITICAL) ||
644 (rs->sr_err == LDAP_SUCCESS) )
646 if ( so->so_vlv > SLAP_CONTROL_IGNORED ) {
647 send_list( op, rs, so );
649 /* Get the first node to send */
650 Avlnode *start_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
651 so->so_tree = start_node;
653 if ( so->so_paged <= SLAP_CONTROL_IGNORED ) {
654 /* Not paged result search. Send all entries.
655 * Set the page size to the number of entries
656 * so that send_page() will send all entries.
658 so->so_page_size = so->so_nentries;
661 send_page( op, rs, so );
666 static void send_result(
671 LDAPControl *ctrls[3];
674 rc = pack_sss_response_control( op, rs, ctrls );
675 if ( rc == LDAP_SUCCESS ) {
678 if ( so->so_paged > SLAP_CONTROL_IGNORED ) {
679 rc = pack_pagedresult_response_control( op, rs, so, ctrls+1 );
680 } else if ( so->so_vlv > SLAP_CONTROL_IGNORED ) {
681 rc = pack_vlv_response_control( op, rs, so, ctrls+1 );
683 if ( rc == LDAP_SUCCESS )
688 if ( ctrls[0] != NULL )
689 slap_add_ctrls( op, rs, ctrls );
690 send_ldap_result( op, rs );
692 if ( so->so_tree == NULL ) {
693 /* Search finished, so clean up */
694 free_sort_op( op->o_conn, so );
698 static int sssvlv_op_response(
702 sort_ctrl *sc = op->o_controls[sss_cid];
703 sort_op *so = op->o_callback->sc_private;
705 if ( rs->sr_type == REP_SEARCH ) {
712 len = sizeof(sort_node) + sc->sc_nkeys * sizeof(struct berval) +
713 rs->sr_entry->e_nname.bv_len + 1;
714 sn = op->o_tmpalloc( len, op->o_tmpmemctx );
715 sn->sn_vals = (struct berval *)(sn+1);
717 /* Build tmp list of key values */
718 for ( i=0; i<sc->sc_nkeys; i++ ) {
719 Attribute *a = attr_find( rs->sr_entry->e_attrs,
720 sc->sc_keys[i].sk_ad );
722 if ( a->a_numvals > 1 ) {
723 bv = select_value( a, &sc->sc_keys[i] );
727 sn->sn_vals[i] = *bv;
728 len += bv->bv_len + 1;
730 BER_BVZERO( &sn->sn_vals[i] );
734 /* Now dup into regular memory */
735 sn2 = ch_malloc( len );
736 sn2->sn_vals = (struct berval *)(sn2+1);
737 AC_MEMCPY( sn2->sn_vals, sn->sn_vals,
738 sc->sc_nkeys * sizeof(struct berval));
740 ptr = (char *)(sn2->sn_vals + sc->sc_nkeys);
741 sn2->sn_dn.bv_val = ptr;
742 sn2->sn_dn.bv_len = rs->sr_entry->e_nname.bv_len;
743 AC_MEMCPY( ptr, rs->sr_entry->e_nname.bv_val,
744 rs->sr_entry->e_nname.bv_len );
745 ptr += rs->sr_entry->e_nname.bv_len;
747 for ( i=0; i<sc->sc_nkeys; i++ ) {
748 if ( !BER_BVISNULL( &sn2->sn_vals[i] )) {
749 AC_MEMCPY(ptr, sn2->sn_vals[i].bv_val, sn2->sn_vals[i].bv_len);
750 sn2->sn_vals[i].bv_val = ptr;
751 ptr += sn2->sn_vals[i].bv_len;
755 op->o_tmpfree( sn, op->o_tmpmemctx );
757 sn->sn_conn = op->o_conn->c_conn_idx;
758 sn->sn_session = find_session_by_so( so->so_info->svi_max_percon, op->o_conn->c_conn_idx, so );
760 /* Insert into the AVL tree */
761 tavl_insert(&(so->so_tree), sn, node_insert, avl_dup_error);
765 /* Collected the keys so that they can be sorted. Thus, stop
766 * the entry from propagating.
768 rs->sr_err = LDAP_SUCCESS;
770 else if ( rs->sr_type == REP_RESULT ) {
771 /* Remove serversort response callback.
772 * We don't want the entries that we are about to send to be
773 * processed by serversort response again.
775 if ( op->o_callback->sc_response == sssvlv_op_response ) {
776 op->o_callback = op->o_callback->sc_next;
779 send_entry( op, rs, so );
780 send_result( op, rs, so );
786 static int sssvlv_op_search(
790 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
791 sssvlv_info *si = on->on_bi.bi_private;
792 int rc = SLAP_CB_CONTINUE;
794 sort_op *so = NULL, so2;
796 PagedResultsState *ps;
800 if ( op->o_ctrlflag[sss_cid] <= SLAP_CONTROL_IGNORED ) {
801 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
802 LDAPControl *ctrls[2];
804 so2.so_vlv_target = 0;
806 so2.so_vlv_rc = LDAP_VLV_SSS_MISSING;
807 so2.so_vlv = op->o_ctrlflag[vlv_cid];
808 rc = pack_vlv_response_control( op, rs, &so2, ctrls );
809 if ( rc == LDAP_SUCCESS ) {
811 slap_add_ctrls( op, rs, ctrls );
813 rs->sr_err = LDAP_VLV_ERROR;
814 rs->sr_text = "Sort control is required with VLV";
817 /* Not server side sort so just continue */
818 return SLAP_CB_CONTINUE;
821 Debug(LDAP_DEBUG_TRACE,
822 "==> sssvlv_search: <%s> %s, control flag: %d\n",
823 op->o_req_dn.bv_val, op->ors_filterstr.bv_val,
824 op->o_ctrlflag[sss_cid]);
826 sc = op->o_controls[sss_cid];
827 if ( sc->sc_nkeys > si->svi_max_keys ) {
828 rs->sr_text = "Too many sort keys";
829 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
833 ps = ( op->o_pagedresults > SLAP_CONTROL_IGNORED ) ?
834 (PagedResultsState*)(op->o_pagedresults_state) : NULL;
835 vc = op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ?
836 op->o_controls[vlv_cid] : NULL;
839 rs->sr_text = "VLV incompatible with PagedResults";
840 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
845 ldap_pvt_thread_mutex_lock( &sort_conns_mutex );
846 /* Is there already a sort running on this conn? */
847 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 );
848 if ( sess_id >= 0 ) {
849 so = sort_conns[op->o_conn->c_conn_idx][sess_id];
850 /* Is it a continuation of a VLV search? */
851 if ( !vc || so->so_vlv <= SLAP_CONTROL_IGNORED ||
852 vc->vc_context != so->so_vcontext ) {
853 /* Is it a continuation of a paged search? */
854 if ( !ps || so->so_paged <= SLAP_CONTROL_IGNORED ||
855 op->o_conn->c_pagedresults_state.ps_cookie != ps->ps_cookie ) {
857 } else if ( !ps->ps_size ) {
858 /* Abandoning current request */
861 rs->sr_err = LDAP_SUCCESS;
864 if (( vc && so->so_paged > SLAP_CONTROL_IGNORED ) ||
865 ( ps && so->so_vlv > SLAP_CONTROL_IGNORED )) {
866 /* changed from paged to vlv or vice versa, abandon */
869 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
871 /* Are there too many running overall? */
872 } else if ( si->svi_num >= si->svi_max ) {
874 } else if ( ( sess_id = find_next_session(si->svi_max_percon, op->o_conn->c_conn_idx ) ) < 0 ) {
877 /* OK, this connection now has a sort running */
879 sort_conns[op->o_conn->c_conn_idx][sess_id] = &so2;
880 sort_conns[op->o_conn->c_conn_idx][sess_id]->so_session = sess_id;
882 ldap_pvt_thread_mutex_unlock( &sort_conns_mutex );
884 /* If we're a global overlay, this check got bypassed */
885 if ( !op->ors_limit && limits_check( op, rs ))
887 /* are we continuing a VLV search? */
888 if ( so && vc && vc->vc_context ) {
890 send_list( op, rs, so );
891 send_result( op, rs, so );
893 /* are we continuing a paged search? */
894 } else if ( so && ps && ps->ps_cookie ) {
896 send_page( op, rs, so );
897 send_result( op, rs, so );
900 slap_callback *cb = op->o_tmpalloc( sizeof(slap_callback),
902 /* Install serversort response callback to handle a new search */
904 so = ch_calloc( 1, sizeof(sort_op));
906 so = op->o_tmpcalloc( 1, sizeof(sort_op), op->o_tmpmemctx );
908 sort_conns[op->o_conn->c_conn_idx][sess_id] = so;
910 cb->sc_cleanup = NULL;
911 cb->sc_response = sssvlv_op_response;
912 cb->sc_next = op->o_callback;
919 so->so_paged = op->o_pagedresults;
920 so->so_page_size = ps->ps_size;
921 op->o_pagedresults = SLAP_CONTROL_IGNORED;
924 so->so_page_size = 0;
926 so->so_vlv = op->o_ctrlflag[vlv_cid];
927 so->so_vlv_target = 0;
930 so->so_vlv = SLAP_CONTROL_NONE;
933 so->so_session = sess_id;
934 so->so_vlv = op->o_ctrlflag[vlv_cid];
935 so->so_vcontext = (unsigned long)so;
941 if ( so && !so->so_nentries ) {
942 free_sort_op( op->o_conn, so );
944 rs->sr_text = "Other sort requests already in progress";
945 rs->sr_err = LDAP_BUSY;
949 send_ldap_result( op, rs );
955 static int get_ordering_rule(
956 AttributeDescription *ad,
957 struct berval *matchrule,
959 MatchingRule **ordering )
963 if ( matchrule && matchrule->bv_val ) {
964 mr = mr_find( matchrule->bv_val );
966 rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
967 rs->sr_text = "serverSort control: No ordering rule";
968 Debug(LDAP_DEBUG_TRACE, "%s: no ordering rule function for %s\n",
969 debug_header, matchrule->bv_val, 0);
973 mr = ad->ad_type->sat_ordering;
975 rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
976 rs->sr_text = "serverSort control: No ordering rule";
977 Debug(LDAP_DEBUG_TRACE,
978 "%s: no ordering rule specified and no default ordering rule for attribute %s\n",
979 debug_header, ad->ad_cname.bv_val, 0);
987 static int count_key(BerElement *ber)
994 /* Server Side Sort Control is a SEQUENCE of SEQUENCE */
995 for ( tag = ber_first_element( ber, &len, &end );
996 tag == LBER_SEQUENCE;
997 tag = ber_next_element( ber, &len, end ))
999 tag = ber_skip_tag( ber, &len );
1000 ber_skip_data( ber, len );
1008 static int build_key(
1014 struct berval matchrule = BER_BVNULL;
1015 ber_int_t reverse = 0;
1018 MatchingRule *ordering = NULL;
1019 AttributeDescription *ad = NULL;
1022 if (( tag = ber_scanf( ber, "{" )) == LBER_ERROR ) {
1023 rs->sr_text = "serverSort control: decoding error";
1024 rs->sr_err = LDAP_PROTOCOL_ERROR;
1028 if (( tag = ber_scanf( ber, "m", &attr )) == LBER_ERROR ) {
1029 rs->sr_text = "serverSort control: attribute decoding error";
1030 rs->sr_err = LDAP_PROTOCOL_ERROR;
1034 tag = ber_peek_tag( ber, &len );
1035 if ( tag == LDAP_MATCHRULE_IDENTIFIER ) {
1036 if (( tag = ber_scanf( ber, "m", &matchrule )) == LBER_ERROR ) {
1037 rs->sr_text = "serverSort control: matchrule decoding error";
1038 rs->sr_err = LDAP_PROTOCOL_ERROR;
1041 tag = ber_peek_tag( ber, &len );
1044 if ( tag == LDAP_REVERSEORDER_IDENTIFIER ) {
1045 if (( tag = ber_scanf( ber, "b", &reverse )) == LBER_ERROR ) {
1046 rs->sr_text = "serverSort control: reverse decoding error";
1047 rs->sr_err = LDAP_PROTOCOL_ERROR;
1052 if (( tag = ber_scanf( ber, "}" )) == LBER_ERROR ) {
1053 rs->sr_text = "serverSort control: decoding error";
1054 rs->sr_err = LDAP_PROTOCOL_ERROR;
1058 if ( slap_bv2ad( &attr, &ad, &text ) != LDAP_SUCCESS ) {
1060 "serverSort control: Unrecognized attribute type in sort key";
1061 Debug(LDAP_DEBUG_TRACE,
1062 "%s: Unrecognized attribute type in sort key: %s\n",
1063 debug_header, SAFESTR(attr.bv_val, "<None>"), 0);
1064 rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
1068 /* get_ordering_rule will set sr_err and sr_text */
1069 get_ordering_rule( ad, &matchrule, rs, &ordering );
1070 if ( rs->sr_err != LDAP_SUCCESS ) {
1075 key->sk_ordering = ordering;
1076 key->sk_direction = reverse ? -1 : 1;
1081 /* Conforms to RFC4510 re: Criticality, original RFC2891 spec is broken
1082 * Also see ITS#7253 for discussion
1084 static int sss_parseCtrl(
1089 BerElementBuffer berbuf;
1096 rs->sr_err = LDAP_PROTOCOL_ERROR;
1098 if ( op->o_ctrlflag[sss_cid] > SLAP_CONTROL_IGNORED ) {
1099 rs->sr_text = "sorted results control specified multiple times";
1100 } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1101 rs->sr_text = "sorted results control value is absent";
1102 } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1103 rs->sr_text = "sorted results control value is empty";
1105 rs->sr_err = LDAP_SUCCESS;
1107 if ( rs->sr_err != LDAP_SUCCESS )
1110 op->o_ctrlflag[sss_cid] = ctrl->ldctl_iscritical ?
1111 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1113 ber = (BerElement *)&berbuf;
1114 ber_init2( ber, &ctrl->ldctl_value, 0 );
1115 i = count_key( ber );
1117 sc = op->o_tmpalloc( sizeof(sort_ctrl) +
1118 (i-1) * sizeof(sort_key), op->o_tmpmemctx );
1120 op->o_controls[sss_cid] = sc;
1122 /* peel off initial sequence */
1123 ber_scanf( ber, "{" );
1127 if ( build_key( ber, rs, &sc->sc_keys[i] ) != LDAP_SUCCESS )
1130 tag = ber_peek_tag( ber, &len );
1131 } while ( tag != LBER_DEFAULT );
1136 static int vlv_parseCtrl(
1141 BerElementBuffer berbuf;
1147 rs->sr_err = LDAP_PROTOCOL_ERROR;
1150 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
1151 rs->sr_text = "vlv control specified multiple times";
1152 } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1153 rs->sr_text = "vlv control value is absent";
1154 } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1155 rs->sr_text = "vlv control value is empty";
1157 if ( rs->sr_text != NULL )
1160 op->o_ctrlflag[vlv_cid] = ctrl->ldctl_iscritical ?
1161 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1163 ber = (BerElement *)&berbuf;
1164 ber_init2( ber, &ctrl->ldctl_value, 0 );
1166 rs->sr_err = LDAP_PROTOCOL_ERROR;
1168 tag = ber_scanf( ber, "{ii", &vc2.vc_before, &vc2.vc_after );
1169 if ( tag == LBER_ERROR ) {
1173 tag = ber_peek_tag( ber, &len );
1174 if ( tag == LDAP_VLVBYINDEX_IDENTIFIER ) {
1175 tag = ber_scanf( ber, "{ii}", &vc2.vc_offset, &vc2.vc_count );
1176 if ( tag == LBER_ERROR )
1178 BER_BVZERO( &vc2.vc_value );
1179 } else if ( tag == LDAP_VLVBYVALUE_IDENTIFIER ) {
1180 tag = ber_scanf( ber, "m", &vc2.vc_value );
1181 if ( tag == LBER_ERROR || BER_BVISNULL( &vc2.vc_value ))
1186 tag = ber_peek_tag( ber, &len );
1187 if ( tag == LDAP_VLVCONTEXT_IDENTIFIER ) {
1189 tag = ber_scanf( ber, "m", &bv );
1190 if ( tag == LBER_ERROR || bv.bv_len != sizeof(vc2.vc_context))
1192 AC_MEMCPY( &vc2.vc_context, bv.bv_val, bv.bv_len );
1197 vc = op->o_tmpalloc( sizeof(vlv_ctrl), op->o_tmpmemctx );
1199 op->o_controls[vlv_cid] = vc;
1200 rs->sr_err = LDAP_SUCCESS;
1205 static int sssvlv_connection_destroy( BackendDB *be, Connection *conn )
1207 slap_overinst *on = (slap_overinst *)be->bd_info;
1208 sssvlv_info *si = on->on_bi.bi_private;
1210 if ( sort_conns[conn->c_conn_idx] ) {
1211 free_sort_ops( conn, sort_conns[conn->c_conn_idx], si->svi_max_percon );
1214 return LDAP_SUCCESS;
1217 static int sssvlv_db_open(
1221 slap_overinst *on = (slap_overinst *)be->bd_info;
1222 sssvlv_info *si = on->on_bi.bi_private;
1226 /* If not set, default to 1/2 of available threads */
1228 si->svi_max = connection_pool_max / 2;
1230 if ( dtblsize && !sort_conns ) {
1231 ldap_pvt_thread_mutex_init( &sort_conns_mutex );
1232 /* accommodate for c_conn_idx == -1 */
1233 sort_conns = ch_calloc( dtblsize + 1, sizeof(sort_op **) );
1234 for ( conn_index = 0 ; conn_index < dtblsize + 1 ; conn_index++ ) {
1235 sort_conns[conn_index] = ch_calloc( si->svi_max_percon, sizeof(sort_op *) );
1240 rc = overlay_register_control( be, LDAP_CONTROL_SORTREQUEST );
1241 if ( rc == LDAP_SUCCESS )
1242 rc = overlay_register_control( be, LDAP_CONTROL_VLVREQUEST );
1246 static ConfigTable sssvlv_cfg[] = {
1247 { "sssvlv-max", "num",
1248 2, 2, 0, ARG_INT|ARG_OFFSET,
1249 (void *)offsetof(sssvlv_info, svi_max),
1250 "( OLcfgOvAt:21.1 NAME 'olcSssVlvMax' "
1251 "DESC 'Maximum number of concurrent Sort requests' "
1252 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
1253 { "sssvlv-maxkeys", "num",
1254 2, 2, 0, ARG_INT|ARG_OFFSET,
1255 (void *)offsetof(sssvlv_info, svi_max_keys),
1256 "( OLcfgOvAt:21.2 NAME 'olcSssVlvMaxKeys' "
1257 "DESC 'Maximum number of Keys in a Sort request' "
1258 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
1259 { "sssvlv-maxperconn", "num",
1260 2, 2, 0, ARG_INT|ARG_OFFSET,
1261 (void *)offsetof(sssvlv_info, svi_max_percon),
1262 "( OLcfgOvAt:21.3 NAME 'olcSssVlvMaxPerConn' "
1263 "DESC 'Maximum number of concurrent paged search requests per connection' "
1264 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
1265 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1268 static ConfigOCs sssvlv_ocs[] = {
1269 { "( OLcfgOvOc:21.1 "
1270 "NAME 'olcSssVlvConfig' "
1271 "DESC 'SSS VLV configuration' "
1272 "SUP olcOverlayConfig "
1273 "MAY ( olcSssVlvMax $ olcSssVlvMaxKeys ) )",
1274 Cft_Overlay, sssvlv_cfg, NULL, NULL },
1278 static int sssvlv_db_init(
1282 slap_overinst *on = (slap_overinst *)be->bd_info;
1285 if ( ov_count == 0 ) {
1288 rc = register_supported_control2( LDAP_CONTROL_SORTREQUEST,
1294 if ( rc != LDAP_SUCCESS ) {
1295 Debug( LDAP_DEBUG_ANY, "Failed to register Sort Request control '%s' (%d)\n",
1296 LDAP_CONTROL_SORTREQUEST, rc, 0 );
1300 rc = register_supported_control2( LDAP_CONTROL_VLVREQUEST,
1306 if ( rc != LDAP_SUCCESS ) {
1307 Debug( LDAP_DEBUG_ANY, "Failed to register VLV Request control '%s' (%d)\n",
1308 LDAP_CONTROL_VLVREQUEST, rc, 0 );
1309 #ifdef SLAP_CONFIG_DELETE
1310 overlay_unregister_control( be, LDAP_CONTROL_SORTREQUEST );
1311 unregister_supported_control( LDAP_CONTROL_SORTREQUEST );
1312 #endif /* SLAP_CONFIG_DELETE */
1317 si = (sssvlv_info *)ch_malloc(sizeof(sssvlv_info));
1318 on->on_bi.bi_private = si;
1322 si->svi_max_keys = SSSVLV_DEFAULT_MAX_KEYS;
1323 si->svi_max_percon = SSSVLV_DEFAULT_MAX_REQUEST_PER_CONN;
1327 return LDAP_SUCCESS;
1330 static int sssvlv_db_destroy(
1334 slap_overinst *on = (slap_overinst *)be->bd_info;
1335 sssvlv_info *si = (sssvlv_info *)on->on_bi.bi_private;
1339 if ( !ov_count && sort_conns) {
1341 for ( conn_index = 0 ; conn_index < dtblsize + 1 ; conn_index++ ) {
1342 ch_free(sort_conns[conn_index]);
1344 ch_free(sort_conns);
1345 ldap_pvt_thread_mutex_destroy( &sort_conns_mutex );
1348 #ifdef SLAP_CONFIG_DELETE
1349 overlay_unregister_control( be, LDAP_CONTROL_SORTREQUEST );
1350 overlay_unregister_control( be, LDAP_CONTROL_VLVREQUEST );
1351 if ( ov_count == 0 ) {
1352 unregister_supported_control( LDAP_CONTROL_SORTREQUEST );
1353 unregister_supported_control( LDAP_CONTROL_VLVREQUEST );
1355 #endif /* SLAP_CONFIG_DELETE */
1359 on->on_bi.bi_private = NULL;
1361 return LDAP_SUCCESS;
1364 static slap_overinst sssvlv;
1366 int sssvlv_initialize()
1370 sssvlv.on_bi.bi_type = "sssvlv";
1371 sssvlv.on_bi.bi_db_init = sssvlv_db_init;
1372 sssvlv.on_bi.bi_db_destroy = sssvlv_db_destroy;
1373 sssvlv.on_bi.bi_db_open = sssvlv_db_open;
1374 sssvlv.on_bi.bi_connection_destroy = sssvlv_connection_destroy;
1375 sssvlv.on_bi.bi_op_search = sssvlv_op_search;
1377 sssvlv.on_bi.bi_cf_ocs = sssvlv_ocs;
1379 rc = config_register_schema( sssvlv_cfg, sssvlv_ocs );
1383 rc = overlay_register( &sssvlv );
1384 if ( rc != LDAP_SUCCESS ) {
1385 Debug( LDAP_DEBUG_ANY, "Failed to register server side sort overlay\n", 0, 0, 0 );
1391 #if SLAPD_OVER_SSSVLV == SLAPD_MOD_DYNAMIC
1392 int init_module( int argc, char *argv[])
1394 return sssvlv_initialize();
1398 #endif /* SLAPD_OVER_SSSVLV */