1 /* sssvlv.c - server side sort / virtual list view */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2009-2010 The OpenLDAP Foundation.
6 * Portions copyright 2009 Symas Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by Howard Chu for inclusion in
24 #ifdef SLAPD_OVER_SSSVLV
28 #include <ac/string.h>
37 #include "../../../libraries/liblber/lber-int.h" /* ber_rewind */
39 /* RFC2891: Server Side Sorting
40 * RFC2696: Paged Results
42 #ifndef LDAP_MATCHRULE_IDENTIFIER
43 #define LDAP_MATCHRULE_IDENTIFIER 0x80L
44 #define LDAP_REVERSEORDER_IDENTIFIER 0x81L
45 #define LDAP_ATTRTYPES_IDENTIFIER 0x80L
48 /* draft-ietf-ldapext-ldapv3-vlv-09.txt: Virtual List Views
50 #ifndef LDAP_VLVBYINDEX_IDENTIFIER
51 #define LDAP_VLVBYINDEX_IDENTIFIER 0xa0L
52 #define LDAP_VLVBYVALUE_IDENTIFIER 0x81L
53 #define LDAP_VLVCONTEXT_IDENTIFIER 0x04L
55 #define LDAP_VLV_SSS_MISSING 0x4C
56 #define LDAP_VLV_RANGE_ERROR 0x4D
59 #define SAFESTR(macro_str, macro_def) ((macro_str) ? (macro_str) : (macro_def))
61 #define SSSVLV_DEFAULT_MAX_KEYS 5
63 typedef struct vlv_ctrl {
68 struct berval vc_value;
69 unsigned long vc_context;
72 typedef struct sort_key
74 AttributeDescription *sk_ad;
75 MatchingRule *sk_ordering;
76 int sk_direction; /* 1=normal, -1=reverse */
79 typedef struct sort_ctrl {
85 typedef struct sort_node
89 struct berval *sn_vals;
92 typedef struct sssvlv_info
94 int svi_max; /* max concurrent sorts */
95 int svi_num; /* current # sorts */
96 int svi_max_keys; /* max sort keys per request */
99 typedef struct sort_op
103 sssvlv_info *so_info;
110 unsigned long so_vcontext;
113 /* There is only one conn table for all overlay instances */
114 static sort_op **sort_conns;
115 static ldap_pvt_thread_mutex_t sort_conns_mutex;
117 static const char *debug_header = "sssvlv";
122 /* RFC 2981 Section 2.2
123 * If a sort key is a multi-valued attribute, and an entry happens to
124 * have multiple values for that attribute and no other controls are
125 * present that affect the sorting order, then the server SHOULD use the
126 * least value (according to the ORDERING rule for that attribute).
128 static struct berval* select_value(
132 struct berval* ber1, *ber2;
133 MatchingRule *mr = key->sk_ordering;
136 ber1 = &(attr->a_nvals[0]);
138 for ( i = 1; i < attr->a_numvals; i++,ber2++ ) {
139 mr->smr_match( &cmp, 0, mr->smr_syntax, mr, ber1, ber2 );
145 Debug(LDAP_DEBUG_TRACE, "%s: value selected for compare: %s\n",
147 SAFESTR(ber1->bv_val, "<Empty>"),
153 static int node_cmp( const void* val1, const void* val2 )
155 sort_node *sn1 = (sort_node *)val1;
156 sort_node *sn2 = (sort_node *)val2;
157 sort_ctrl *sc = sort_conns[sn1->sn_conn]->so_ctrl;
161 for ( i=0; cmp == 0 && i<sc->sc_nkeys; i++ ) {
162 if ( BER_BVISNULL( &sn1->sn_vals[i] )) {
163 if ( BER_BVISNULL( &sn2->sn_vals[i] ))
166 cmp = sc->sc_keys[i].sk_direction;
167 } else if ( BER_BVISNULL( &sn2->sn_vals[i] )) {
168 cmp = sc->sc_keys[i].sk_direction * -1;
170 mr = sc->sc_keys[i].sk_ordering;
171 mr->smr_match( &cmp, 0, mr->smr_syntax, mr,
172 &sn1->sn_vals[i], &sn2->sn_vals[i] );
174 cmp *= sc->sc_keys[i].sk_direction;
180 static int node_insert( const void *val1, const void *val2 )
182 /* Never return equal so that new entries are always inserted */
183 return node_cmp( val1, val2 ) < 0 ? -1 : 1;
186 static int pack_vlv_response_control(
190 LDAPControl **ctrlsp )
193 BerElementBuffer berbuf;
194 BerElement *ber = (BerElement *)&berbuf;
195 struct berval cookie, bv;
198 ber_init2( ber, NULL, LBER_USE_DER );
199 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
201 rc = ber_printf( ber, "{iii", so->so_vlv_target, so->so_nentries,
204 if ( rc != -1 && so->so_vcontext ) {
205 cookie.bv_val = (char *)&so->so_vcontext;
206 cookie.bv_len = sizeof(so->so_vcontext);
207 rc = ber_printf( ber, "tO", LDAP_VLVCONTEXT_IDENTIFIER, &cookie );
211 rc = ber_printf( ber, "}" );
215 rc = ber_flatten2( ber, &bv, 0 );
219 ctrl = (LDAPControl *)op->o_tmpalloc( sizeof(LDAPControl)+
220 bv.bv_len, op->o_tmpmemctx );
221 ctrl->ldctl_oid = LDAP_CONTROL_VLVRESPONSE;
222 ctrl->ldctl_iscritical = 0;
223 ctrl->ldctl_value.bv_val = (char *)(ctrl+1);
224 ctrl->ldctl_value.bv_len = bv.bv_len;
225 AC_MEMCPY( ctrl->ldctl_value.bv_val, bv.bv_val, bv.bv_len );
229 rs->sr_err = LDAP_OTHER;
237 static int pack_pagedresult_response_control(
241 LDAPControl **ctrlsp )
244 BerElementBuffer berbuf;
245 BerElement *ber = (BerElement *)&berbuf;
246 PagedResultsCookie resp_cookie;
247 struct berval cookie, bv;
250 ber_init2( ber, NULL, LBER_USE_DER );
251 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
253 if ( so->so_nentries > 0 ) {
254 resp_cookie = ( PagedResultsCookie )so->so_tree;
255 cookie.bv_len = sizeof( PagedResultsCookie );
256 cookie.bv_val = (char *)&resp_cookie;
258 resp_cookie = ( PagedResultsCookie )0;
259 BER_BVZERO( &cookie );
262 op->o_conn->c_pagedresults_state.ps_cookie = resp_cookie;
263 op->o_conn->c_pagedresults_state.ps_count
264 = ((PagedResultsState *)op->o_pagedresults_state)->ps_count
267 rc = ber_printf( ber, "{iO}", so->so_nentries, &cookie );
269 rc = ber_flatten2( ber, &bv, 0 );
273 ctrl = (LDAPControl *)op->o_tmpalloc( sizeof(LDAPControl)+
274 bv.bv_len, op->o_tmpmemctx );
275 ctrl->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
276 ctrl->ldctl_iscritical = 0;
277 ctrl->ldctl_value.bv_val = (char *)(ctrl+1);
278 ctrl->ldctl_value.bv_len = bv.bv_len;
279 AC_MEMCPY( ctrl->ldctl_value.bv_val, bv.bv_val, bv.bv_len );
283 rs->sr_err = LDAP_OTHER;
291 static int pack_sss_response_control(
294 LDAPControl **ctrlsp )
297 BerElementBuffer berbuf;
298 BerElement *ber = (BerElement *)&berbuf;
302 ber_init2( ber, NULL, LBER_USE_DER );
303 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
305 /* Pack error code */
306 rc = ber_printf(ber, "{e}", rs->sr_err);
309 rc = ber_flatten2( ber, &bv, 0 );
312 ctrl = (LDAPControl *)op->o_tmpalloc( sizeof(LDAPControl)+
313 bv.bv_len, op->o_tmpmemctx );
314 ctrl->ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
315 ctrl->ldctl_iscritical = 0;
316 ctrl->ldctl_value.bv_val = (char *)(ctrl+1);
317 ctrl->ldctl_value.bv_len = bv.bv_len;
318 AC_MEMCPY( ctrl->ldctl_value.bv_val, bv.bv_val, bv.bv_len );
322 rs->sr_err = LDAP_OTHER;
330 static void free_sort_op( Connection *conn, sort_op *so )
333 tavl_free( so->so_tree, ch_free );
337 ldap_pvt_thread_mutex_lock( &sort_conns_mutex );
338 sort_conns[conn->c_conn_idx] = NULL;
339 so->so_info->svi_num--;
340 ldap_pvt_thread_mutex_unlock( &sort_conns_mutex );
345 static void send_list(
350 Avlnode *cur_node, *tmp_node;
351 vlv_ctrl *vc = op->o_controls[vlv_cid];
355 LDAPControl *ctrls[2];
357 /* FIXME: it may be better to just flatten the tree into
358 * an array before doing all of this...
361 /* Are we just counting an offset? */
362 if ( BER_BVISNULL( &vc->vc_value )) {
363 if ( vc->vc_offset == vc->vc_count ) {
364 /* wants the last entry in the list */
365 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
366 so->so_vlv_target = so->so_nentries;
367 } else if ( vc->vc_offset == 1 ) {
368 /* wants the first entry in the list */
369 cur_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
370 so->so_vlv_target = 1;
373 /* Just iterate to the right spot */
374 if ( vc->vc_count && vc->vc_count != so->so_nentries ) {
375 if ( vc->vc_offset > vc->vc_count )
377 target = so->so_nentries * vc->vc_offset / vc->vc_count;
379 if ( vc->vc_offset > so->so_nentries ) {
381 so->so_vlv_rc = LDAP_VLV_RANGE_ERROR;
382 pack_vlv_response_control( op, rs, so, ctrls );
384 slap_add_ctrls( op, rs, ctrls );
385 rs->sr_err = LDAP_VLV_ERROR;
388 target = vc->vc_offset;
390 so->so_vlv_target = target;
391 /* Start at left and go right, or start at right and go left? */
392 if ( target < so->so_nentries / 2 ) {
393 cur_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
394 dir = TAVL_DIR_RIGHT;
396 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
398 target = so->so_nentries - target + 1;
400 for ( i=1; i<target; i++ )
401 cur_node = tavl_next( cur_node, dir );
404 /* we're looking for a specific value */
405 sort_ctrl *sc = so->so_ctrl;
406 MatchingRule *mr = sc->sc_keys[0].sk_ordering;
410 if ( mr->smr_normalize ) {
411 rc = mr->smr_normalize( SLAP_MR_VALUE_OF_SYNTAX,
412 mr->smr_syntax, mr, &vc->vc_value, &bv, op->o_tmpmemctx );
414 so->so_vlv_rc = LDAP_INAPPROPRIATE_MATCHING;
415 pack_vlv_response_control( op, rs, so, ctrls );
417 slap_add_ctrls( op, rs, ctrls );
418 rs->sr_err = LDAP_VLV_ERROR;
425 sn = op->o_tmpalloc( sizeof(sort_node) +
426 sc->sc_nkeys * sizeof(struct berval), op->o_tmpmemctx );
427 sn->sn_vals = (struct berval *)(sn+1);
428 sn->sn_conn = op->o_conn->c_conn_idx;
430 for (i=1; i<sc->sc_nkeys; i++) {
431 BER_BVZERO( &sn->sn_vals[i] );
433 cur_node = tavl_find3( so->so_tree, sn, node_cmp, &j );
434 /* didn't find >= match */
437 op->o_tmpfree( sn, op->o_tmpmemctx );
440 so->so_vlv_target = so->so_nentries + 1;
442 sort_node *sn = so->so_tree->avl_data;
443 /* start from the left or the right side? */
444 mr->smr_match( &i, 0, mr->smr_syntax, mr, &bv, &sn->sn_vals[0] );
446 tmp_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
449 tmp_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
450 dir = TAVL_DIR_RIGHT;
452 for (i=0; tmp_node != cur_node;
453 tmp_node = tavl_next( tmp_node, dir ), i++);
454 so->so_vlv_target = i;
456 if ( bv.bv_val != vc->vc_value.bv_val )
457 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
461 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
465 for ( ; i<vc->vc_before; i++ ) {
466 tmp_node = tavl_next( cur_node, TAVL_DIR_LEFT );
467 if ( !tmp_node ) break;
470 j = i + vc->vc_after + 1;
472 for ( i=0; i<j; i++ ) {
473 sort_node *sn = cur_node->avl_data;
475 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;
505 while ( cur_node && rs->sr_nentries < so->so_page_size ) {
506 sort_node *sn = cur_node->avl_data;
508 if ( slapd_shutdown ) break;
510 next_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
512 op->o_bd = select_backend( &sn->sn_dn, 0 );
514 rc = be_entry_get_rw( op, &sn->sn_dn, NULL, NULL, 0, &e );
516 ch_free( cur_node->avl_data );
517 ber_memfree( cur_node );
519 cur_node = next_node;
522 if ( e && rc == LDAP_SUCCESS ) {
524 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
525 rs->sr_err = send_search_entry( op, rs );
526 if ( rs->sr_err == LDAP_UNAVAILABLE )
531 /* Set the first entry to send for the next page */
532 so->so_tree = next_node;
537 static void send_entry(
542 Debug(LDAP_DEBUG_TRACE,
543 "%s: response control: status=%d, text=%s\n",
544 debug_header, rs->sr_err, SAFESTR(rs->sr_text, "<None>"));
549 /* RFC 2891: If critical then send the entries iff they were
550 * succesfully sorted. If non-critical send all entries
551 * whether they were sorted or not.
553 if ( (op->o_ctrlflag[sss_cid] != SLAP_CONTROL_CRITICAL) ||
554 (rs->sr_err == LDAP_SUCCESS) )
556 if ( so->so_vlv > SLAP_CONTROL_IGNORED ) {
557 send_list( op, rs, so );
559 /* Get the first node to send */
560 Avlnode *start_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
561 so->so_tree = start_node;
563 if ( so->so_paged <= SLAP_CONTROL_IGNORED ) {
564 /* Not paged result search. Send all entries.
565 * Set the page size to the number of entries
566 * so that send_page() will send all entries.
568 so->so_page_size = so->so_nentries;
571 send_page( op, rs, so );
576 static void send_result(
581 LDAPControl *ctrls[3];
584 rc = pack_sss_response_control( op, rs, ctrls );
585 if ( rc == LDAP_SUCCESS ) {
588 if ( so->so_paged > SLAP_CONTROL_IGNORED ) {
589 rc = pack_pagedresult_response_control( op, rs, so, ctrls+1 );
590 } else if ( so->so_vlv > SLAP_CONTROL_IGNORED ) {
591 rc = pack_vlv_response_control( op, rs, so, ctrls+1 );
593 if ( rc == LDAP_SUCCESS )
598 if ( ctrls[0] != NULL )
599 slap_add_ctrls( op, rs, ctrls );
600 send_ldap_result( op, rs );
602 if ( so->so_tree == NULL ) {
603 /* Search finished, so clean up */
604 free_sort_op( op->o_conn, so );
608 static int sssvlv_op_response(
612 sort_ctrl *sc = op->o_controls[sss_cid];
613 sort_op *so = op->o_callback->sc_private;
615 if ( rs->sr_type == REP_SEARCH ) {
622 len = sizeof(sort_node) + sc->sc_nkeys * sizeof(struct berval) +
623 rs->sr_entry->e_nname.bv_len + 1;
624 sn = op->o_tmpalloc( len, op->o_tmpmemctx );
625 sn->sn_vals = (struct berval *)(sn+1);
627 /* Build tmp list of key values */
628 for ( i=0; i<sc->sc_nkeys; i++ ) {
629 Attribute *a = attr_find( rs->sr_entry->e_attrs,
630 sc->sc_keys[i].sk_ad );
632 if ( a->a_numvals > 1 ) {
633 bv = select_value( a, &sc->sc_keys[i] );
637 sn->sn_vals[i] = *bv;
638 len += bv->bv_len + 1;
640 BER_BVZERO( &sn->sn_vals[i] );
644 /* Now dup into regular memory */
645 sn2 = ch_malloc( len );
646 sn2->sn_vals = (struct berval *)(sn2+1);
647 AC_MEMCPY( sn2->sn_vals, sn->sn_vals,
648 sc->sc_nkeys * sizeof(struct berval));
650 ptr = (char *)(sn2->sn_vals + sc->sc_nkeys);
651 sn2->sn_dn.bv_val = ptr;
652 sn2->sn_dn.bv_len = rs->sr_entry->e_nname.bv_len;
653 AC_MEMCPY( ptr, rs->sr_entry->e_nname.bv_val,
654 rs->sr_entry->e_nname.bv_len );
655 ptr += rs->sr_entry->e_nname.bv_len;
657 for ( i=0; i<sc->sc_nkeys; i++ ) {
658 if ( !BER_BVISNULL( &sn2->sn_vals[i] )) {
659 AC_MEMCPY(ptr, sn2->sn_vals[i].bv_val, sn2->sn_vals[i].bv_len);
660 sn2->sn_vals[i].bv_val = ptr;
661 ptr += sn2->sn_vals[i].bv_len;
665 op->o_tmpfree( sn, op->o_tmpmemctx );
667 sn->sn_conn = op->o_conn->c_conn_idx;
669 /* Insert into the AVL tree */
670 tavl_insert(&(so->so_tree), sn, node_insert, avl_dup_error);
674 /* Collected the keys so that they can be sorted. Thus, stop
675 * the entry from propagating.
677 rs->sr_err = LDAP_SUCCESS;
679 else if ( rs->sr_type == REP_RESULT ) {
680 /* Remove serversort response callback.
681 * We don't want the entries that we are about to send to be
682 * processed by serversort response again.
684 if ( op->o_callback->sc_response == sssvlv_op_response ) {
685 op->o_callback = op->o_callback->sc_next;
688 send_entry( op, rs, so );
689 send_result( op, rs, so );
695 static int sssvlv_op_search(
699 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
700 sssvlv_info *si = on->on_bi.bi_private;
701 int rc = SLAP_CB_CONTINUE;
705 PagedResultsState *ps;
708 if ( op->o_ctrlflag[sss_cid] <= SLAP_CONTROL_IGNORED ) {
709 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
710 LDAPControl *ctrls[2];
712 so2.so_vlv_target = 0;
714 so2.so_vlv_rc = LDAP_VLV_SSS_MISSING;
715 rc = pack_vlv_response_control( op, rs, &so2, ctrls );
716 if ( rc == LDAP_SUCCESS ) {
718 slap_add_ctrls( op, rs, ctrls );
720 rs->sr_err = LDAP_VLV_ERROR;
721 rs->sr_text = "Sort control is required with VLV";
724 /* Not server side sort so just continue */
725 return SLAP_CB_CONTINUE;
728 Debug(LDAP_DEBUG_TRACE,
729 "==> sssvlv_search: <%s> %s, control flag: %d\n",
730 op->o_req_dn.bv_val, op->ors_filterstr.bv_val,
731 op->o_ctrlflag[sss_cid]);
733 sc = op->o_controls[sss_cid];
734 if ( sc->sc_nkeys > si->svi_max_keys ) {
735 rs->sr_text = "Too many sort keys";
736 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
740 ps = ( op->o_pagedresults > SLAP_CONTROL_IGNORED ) ?
741 (PagedResultsState*)(op->o_pagedresults_state) : NULL;
742 vc = op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ?
743 op->o_controls[vlv_cid] : NULL;
746 rs->sr_text = "VLV incompatible with PagedResults";
747 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
752 ldap_pvt_thread_mutex_lock( &sort_conns_mutex );
753 so = sort_conns[op->o_conn->c_conn_idx];
754 /* Is there already a sort running on this conn? */
756 /* Is it a continuation of a VLV search? */
757 if ( !vc || so->so_vlv <= SLAP_CONTROL_IGNORED ||
758 vc->vc_context != so->so_vcontext ) {
759 /* Is it a continuation of a paged search? */
760 if ( !ps || so->so_paged <= SLAP_CONTROL_IGNORED ||
761 op->o_conn->c_pagedresults_state.ps_cookie != ps->ps_cookie ) {
763 } else if ( !ps->ps_size ) {
764 /* Abandoning current request */
767 rs->sr_err = LDAP_SUCCESS;
770 if (( vc && so->so_paged > SLAP_CONTROL_IGNORED ) ||
771 ( ps && so->so_vlv > SLAP_CONTROL_IGNORED )) {
772 /* changed from paged to vlv or vice versa, abandon */
775 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
777 /* Are there too many running overall? */
778 } else if ( si->svi_num >= si->svi_max ) {
781 /* OK, this connection now has a sort running */
783 sort_conns[op->o_conn->c_conn_idx] = &so2;
785 ldap_pvt_thread_mutex_unlock( &sort_conns_mutex );
787 /* are we continuing a VLV search? */
788 if ( vc && vc->vc_context ) {
790 send_list( op, rs, so );
791 send_result( op, rs, so );
793 /* are we continuing a paged search? */
794 } else if ( ps && ps->ps_cookie ) {
796 send_page( op, rs, so );
797 send_result( op, rs, so );
800 slap_callback *cb = op->o_tmpalloc( sizeof(slap_callback),
802 /* Install serversort response callback to handle a new search */
804 so = ch_calloc( 1, sizeof(sort_op));
806 so = op->o_tmpcalloc( 1, sizeof(sort_op), op->o_tmpmemctx );
808 sort_conns[op->o_conn->c_conn_idx] = so;
810 cb->sc_cleanup = NULL;
811 cb->sc_response = sssvlv_op_response;
812 cb->sc_next = op->o_callback;
819 so->so_paged = op->o_pagedresults;
820 so->so_page_size = ps->ps_size;
821 op->o_pagedresults = SLAP_CONTROL_IGNORED;
824 so->so_page_size = 0;
826 so->so_vlv = op->o_ctrlflag[vlv_cid];
827 so->so_vlv_target = 0;
830 so->so_vlv = SLAP_CONTROL_NONE;
833 so->so_vcontext = (unsigned long)so;
839 if ( so && !so->so_nentries ) {
840 free_sort_op( op->o_conn, so );
842 rs->sr_text = "Other sort requests already in progress";
843 rs->sr_err = LDAP_BUSY;
847 send_ldap_result( op, rs );
853 static int get_ordering_rule(
854 AttributeDescription *ad,
855 struct berval *matchrule,
857 MatchingRule **ordering )
861 if ( matchrule && matchrule->bv_val ) {
862 mr = mr_find( matchrule->bv_val );
864 rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
865 rs->sr_text = "serverSort control: No ordering rule";
866 Debug(LDAP_DEBUG_TRACE, "%s: no ordering rule function for %s\n",
867 debug_header, matchrule->bv_val, 0);
871 mr = ad->ad_type->sat_ordering;
873 rs->sr_err = LDAP_INAPPROPRIATE_MATCHING;
874 rs->sr_text = "serverSort control: No ordering rule";
875 Debug(LDAP_DEBUG_TRACE,
876 "%s: no ordering rule specified and no default ordering rule for attribute %s\n",
877 debug_header, ad->ad_cname.bv_val, 0);
885 static int count_key(BerElement *ber)
892 /* Server Side Sort Control is a SEQUENCE of SEQUENCE */
893 for ( tag = ber_first_element( ber, &len, &end );
894 tag == LBER_SEQUENCE;
895 tag = ber_next_element( ber, &len, end ))
897 tag = ber_skip_tag( ber, &len );
898 ber_skip_data( ber, len );
906 static int build_key(
912 struct berval matchrule = BER_BVNULL;
913 ber_int_t reverse = 0;
916 MatchingRule *ordering = NULL;
917 AttributeDescription *ad = NULL;
920 if (( tag = ber_scanf( ber, "{" )) == LBER_ERROR ) {
921 rs->sr_text = "serverSort control: decoding error";
922 rs->sr_err = LDAP_PROTOCOL_ERROR;
926 if (( tag = ber_scanf( ber, "m", &attr )) == LBER_ERROR ) {
927 rs->sr_text = "serverSort control: attribute decoding error";
928 rs->sr_err = LDAP_PROTOCOL_ERROR;
932 tag = ber_peek_tag( ber, &len );
933 if ( tag == LDAP_MATCHRULE_IDENTIFIER ) {
934 if (( tag = ber_scanf( ber, "m", &matchrule )) == LBER_ERROR ) {
935 rs->sr_text = "serverSort control: matchrule decoding error";
936 rs->sr_err = LDAP_PROTOCOL_ERROR;
939 tag = ber_peek_tag( ber, &len );
942 if ( tag == LDAP_REVERSEORDER_IDENTIFIER ) {
943 if (( tag = ber_scanf( ber, "b", &reverse )) == LBER_ERROR ) {
944 rs->sr_text = "serverSort control: reverse decoding error";
945 rs->sr_err = LDAP_PROTOCOL_ERROR;
950 if (( tag = ber_scanf( ber, "}" )) == LBER_ERROR ) {
951 rs->sr_text = "serverSort control: decoding error";
952 rs->sr_err = LDAP_PROTOCOL_ERROR;
956 if ( slap_bv2ad( &attr, &ad, &text ) != LDAP_SUCCESS ) {
958 "serverSort control: Unrecognized attribute type in sort key";
959 Debug(LDAP_DEBUG_TRACE,
960 "%s: Unrecognized attribute type in sort key: %s\n",
961 debug_header, SAFESTR(attr.bv_val, "<None>"), 0);
962 rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
966 /* get_ordering_rule will set sr_err and sr_text */
967 get_ordering_rule( ad, &matchrule, rs, &ordering );
968 if ( rs->sr_err != LDAP_SUCCESS ) {
973 key->sk_ordering = ordering;
974 key->sk_direction = reverse ? -1 : 1;
979 static int sss_parseCtrl(
984 BerElementBuffer berbuf;
991 rs->sr_err = LDAP_PROTOCOL_ERROR;
993 if ( op->o_ctrlflag[sss_cid] > SLAP_CONTROL_IGNORED ) {
994 rs->sr_text = "sorted results control specified multiple times";
995 } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
996 rs->sr_text = "sorted results control value is absent";
997 } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
998 rs->sr_text = "sorted results control value is empty";
1000 rs->sr_err = LDAP_SUCCESS;
1002 if ( rs->sr_err != LDAP_SUCCESS )
1005 op->o_ctrlflag[sss_cid] = ctrl->ldctl_iscritical ?
1006 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1008 ber = (BerElement *)&berbuf;
1009 ber_init2( ber, &ctrl->ldctl_value, 0 );
1010 i = count_key( ber );
1012 sc = op->o_tmpalloc( sizeof(sort_ctrl) +
1013 (i-1) * sizeof(sort_key), op->o_tmpmemctx );
1015 op->o_controls[sss_cid] = sc;
1017 /* peel off initial sequence */
1018 ber_scanf( ber, "{" );
1022 if ( build_key( ber, rs, &sc->sc_keys[i] ) != LDAP_SUCCESS )
1025 tag = ber_peek_tag( ber, &len );
1026 } while ( tag != LBER_DEFAULT );
1031 static int vlv_parseCtrl(
1036 BerElementBuffer berbuf;
1043 rs->sr_err = LDAP_PROTOCOL_ERROR;
1046 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
1047 rs->sr_text = "vlv control specified multiple times";
1048 } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1049 rs->sr_text = "vlv control value is absent";
1050 } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1051 rs->sr_text = "vlv control value is empty";
1053 if ( rs->sr_text != NULL )
1056 op->o_ctrlflag[vlv_cid] = ctrl->ldctl_iscritical ?
1057 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1059 ber = (BerElement *)&berbuf;
1060 ber_init2( ber, &ctrl->ldctl_value, 0 );
1062 rs->sr_err = LDAP_PROTOCOL_ERROR;
1064 tag = ber_scanf( ber, "{ii", &vc2.vc_before, &vc2.vc_after );
1065 if ( tag == LBER_ERROR ) {
1069 tag = ber_peek_tag( ber, &len );
1070 if ( tag == LDAP_VLVBYINDEX_IDENTIFIER ) {
1071 tag = ber_scanf( ber, "{ii}", &vc2.vc_offset, &vc2.vc_count );
1072 if ( tag == LBER_ERROR )
1074 BER_BVZERO( &vc2.vc_value );
1075 } else if ( tag == LDAP_VLVBYVALUE_IDENTIFIER ) {
1076 tag = ber_scanf( ber, "m", &vc2.vc_value );
1077 if ( tag == LBER_ERROR || BER_BVISNULL( &vc2.vc_value ))
1082 tag = ber_peek_tag( ber, &len );
1083 if ( tag == LDAP_VLVCONTEXT_IDENTIFIER ) {
1085 tag = ber_scanf( ber, "m", &bv );
1086 if ( tag == LBER_ERROR || bv.bv_len != sizeof(vc2.vc_context))
1088 AC_MEMCPY( &vc2.vc_context, bv.bv_val, bv.bv_len );
1093 vc = op->o_tmpalloc( sizeof(vlv_ctrl), op->o_tmpmemctx );
1095 op->o_controls[vlv_cid] = vc;
1096 rs->sr_err = LDAP_SUCCESS;
1101 static int sssvlv_connection_destroy( BackendDB *be, Connection *conn )
1103 slap_overinst *on = (slap_overinst *)be->bd_info;
1105 if ( sort_conns[conn->c_conn_idx] )
1106 free_sort_op( conn, sort_conns[conn->c_conn_idx] );
1108 return LDAP_SUCCESS;
1111 static int sssvlv_db_open(
1115 slap_overinst *on = (slap_overinst *)be->bd_info;
1116 sssvlv_info *si = on->on_bi.bi_private;
1119 /* If not set, default to 1/2 of available threads */
1121 si->svi_max = connection_pool_max / 2;
1123 rc = overlay_register_control( be, LDAP_CONTROL_SORTREQUEST );
1124 if ( rc == LDAP_SUCCESS )
1125 rc = overlay_register_control( be, LDAP_CONTROL_VLVREQUEST );
1129 static ConfigTable sssvlv_cfg[] = {
1130 { "sssvlv-max", "num",
1131 2, 2, 0, ARG_INT|ARG_OFFSET,
1132 (void *)offsetof(sssvlv_info, svi_max),
1133 "( OLcfgOvAt:21.1 NAME 'olcSssVlvMax' "
1134 "DESC 'Maximum number of concurrent Sort requests' "
1135 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
1136 { "sssvlv-maxkeys", "num",
1137 2, 2, 0, ARG_INT|ARG_OFFSET,
1138 (void *)offsetof(sssvlv_info, svi_max_keys),
1139 "( OLcfgOvAt:21.2 NAME 'olcSssVlvMaxKeys' "
1140 "DESC 'Maximum number of Keys in a Sort request' "
1141 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
1142 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1145 static ConfigOCs sssvlv_ocs[] = {
1146 { "( OLcfgOvOc:21.1 "
1147 "NAME 'olcSssVlvConfig' "
1148 "DESC 'SSS VLV configuration' "
1149 "SUP olcOverlayConfig "
1150 "MAY ( olcSssVlvMax $ olcSssVlvMaxKeys ) )",
1151 Cft_Overlay, sssvlv_cfg, NULL, NULL },
1155 static int sssvlv_db_init(
1159 slap_overinst *on = (slap_overinst *)be->bd_info;
1162 if ( ov_count == 0 ) {
1165 rc = register_supported_control2( LDAP_CONTROL_SORTREQUEST,
1171 if ( rc != LDAP_SUCCESS ) {
1172 Debug( LDAP_DEBUG_ANY, "Failed to register Sort Request control '%s' (%d)\n",
1173 LDAP_CONTROL_SORTREQUEST, rc, 0 );
1177 rc = register_supported_control2( LDAP_CONTROL_VLVREQUEST,
1183 if ( rc != LDAP_SUCCESS ) {
1184 Debug( LDAP_DEBUG_ANY, "Failed to register VLV Request control '%s' (%d)\n",
1185 LDAP_CONTROL_VLVREQUEST, rc, 0 );
1186 #ifdef SLAP_CONFIG_DELETE
1187 overlay_unregister_control( be, LDAP_CONTROL_SORTREQUEST );
1188 unregister_supported_control( LDAP_CONTROL_SORTREQUEST );
1189 #endif /* SLAP_CONFIG_DELETE */
1194 si = (sssvlv_info *)ch_malloc(sizeof(sssvlv_info));
1195 on->on_bi.bi_private = si;
1199 si->svi_max_keys = SSSVLV_DEFAULT_MAX_KEYS;
1201 if ( dtblsize && !sort_conns ) {
1202 ldap_pvt_thread_mutex_init( &sort_conns_mutex );
1203 /* accommodate for c_conn_idx == -1 */
1204 sort_conns = ch_calloc( sizeof(sort_op *), dtblsize + 1 );
1209 return LDAP_SUCCESS;
1212 static int sssvlv_db_destroy(
1216 slap_overinst *on = (slap_overinst *)be->bd_info;
1217 sssvlv_info *si = (sssvlv_info *)on->on_bi.bi_private;
1220 if ( !ov_count && sort_conns) {
1222 ch_free(sort_conns);
1223 ldap_pvt_thread_mutex_destroy( &sort_conns_mutex );
1226 #ifdef SLAP_CONFIG_DELETE
1227 overlay_unregister_control( be, LDAP_CONTROL_SORTREQUEST );
1228 overlay_unregister_control( be, LDAP_CONTROL_VLVREQUEST );
1229 if ( ov_count == 0 ) {
1230 unregister_supported_control( LDAP_CONTROL_SORTREQUEST );
1232 #endif /* SLAP_CONFIG_DELETE */
1236 on->on_bi.bi_private = NULL;
1238 return LDAP_SUCCESS;
1241 static slap_overinst sssvlv;
1243 int sssvlv_initialize()
1247 sssvlv.on_bi.bi_type = "sssvlv";
1248 sssvlv.on_bi.bi_db_init = sssvlv_db_init;
1249 sssvlv.on_bi.bi_db_destroy = sssvlv_db_destroy;
1250 sssvlv.on_bi.bi_db_open = sssvlv_db_open;
1251 sssvlv.on_bi.bi_connection_destroy = sssvlv_connection_destroy;
1252 sssvlv.on_bi.bi_op_search = sssvlv_op_search;
1254 sssvlv.on_bi.bi_cf_ocs = sssvlv_ocs;
1256 rc = config_register_schema( sssvlv_cfg, sssvlv_ocs );
1260 rc = overlay_register( &sssvlv );
1261 if ( rc != LDAP_SUCCESS ) {
1262 Debug( LDAP_DEBUG_ANY, "Failed to register server side sort overlay\n", 0, 0, 0 );
1268 #if SLAPD_OVER_SSSVLV == SLAPD_MOD_DYNAMIC
1269 int init_module( int argc, char *argv[])
1271 return sssvlv_initialize();
1275 #endif /* SLAPD_OVER_SSSVLV */