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;
137 ber1 = &(attr->a_nvals[0]);
139 for ( i = 1; i < attr->a_numvals; i++,ber2++ ) {
140 mr->smr_match( &cmp, 0, mr->smr_syntax, mr, ber1, ber2 );
146 Debug(LDAP_DEBUG_TRACE, "%s: value selected for compare: %s\n",
148 SAFESTR(ber1->bv_val, "<Empty>"),
154 static int node_cmp( const void* val1, const void* val2 )
156 sort_node *sn1 = (sort_node *)val1;
157 sort_node *sn2 = (sort_node *)val2;
158 sort_ctrl *sc = sort_conns[sn1->sn_conn]->so_ctrl;
162 for ( i=0; cmp == 0 && i<sc->sc_nkeys; i++ ) {
163 if ( BER_BVISNULL( &sn1->sn_vals[i] )) {
164 if ( BER_BVISNULL( &sn2->sn_vals[i] ))
167 cmp = sc->sc_keys[i].sk_direction;
168 } else if ( BER_BVISNULL( &sn2->sn_vals[i] )) {
169 cmp = sc->sc_keys[i].sk_direction * -1;
171 mr = sc->sc_keys[i].sk_ordering;
172 mr->smr_match( &cmp, 0, mr->smr_syntax, mr,
173 &sn1->sn_vals[i], &sn2->sn_vals[i] );
175 cmp *= sc->sc_keys[i].sk_direction;
181 static int node_insert( const void *val1, const void *val2 )
183 /* Never return equal so that new entries are always inserted */
184 return node_cmp( val1, val2 ) < 0 ? -1 : 1;
187 static int pack_vlv_response_control(
191 LDAPControl **ctrlsp )
194 BerElementBuffer berbuf;
195 BerElement *ber = (BerElement *)&berbuf;
196 struct berval cookie, bv;
199 ber_init2( ber, NULL, LBER_USE_DER );
200 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
202 rc = ber_printf( ber, "{iie", so->so_vlv_target, so->so_nentries,
205 if ( rc != -1 && so->so_vcontext ) {
206 cookie.bv_val = (char *)&so->so_vcontext;
207 cookie.bv_len = sizeof(so->so_vcontext);
208 rc = ber_printf( ber, "tO", LDAP_VLVCONTEXT_IDENTIFIER, &cookie );
212 rc = ber_printf( ber, "}" );
216 rc = ber_flatten2( ber, &bv, 0 );
220 ctrl = (LDAPControl *)op->o_tmpalloc( sizeof(LDAPControl)+
221 bv.bv_len, op->o_tmpmemctx );
222 ctrl->ldctl_oid = LDAP_CONTROL_VLVRESPONSE;
223 ctrl->ldctl_iscritical = 0;
224 ctrl->ldctl_value.bv_val = (char *)(ctrl+1);
225 ctrl->ldctl_value.bv_len = bv.bv_len;
226 AC_MEMCPY( ctrl->ldctl_value.bv_val, bv.bv_val, bv.bv_len );
230 rs->sr_err = LDAP_OTHER;
238 static int pack_pagedresult_response_control(
242 LDAPControl **ctrlsp )
245 BerElementBuffer berbuf;
246 BerElement *ber = (BerElement *)&berbuf;
247 PagedResultsCookie resp_cookie;
248 struct berval cookie, bv;
251 ber_init2( ber, NULL, LBER_USE_DER );
252 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
254 if ( so->so_nentries > 0 ) {
255 resp_cookie = ( PagedResultsCookie )so->so_tree;
256 cookie.bv_len = sizeof( PagedResultsCookie );
257 cookie.bv_val = (char *)&resp_cookie;
259 resp_cookie = ( PagedResultsCookie )0;
260 BER_BVZERO( &cookie );
263 op->o_conn->c_pagedresults_state.ps_cookie = resp_cookie;
264 op->o_conn->c_pagedresults_state.ps_count
265 = ((PagedResultsState *)op->o_pagedresults_state)->ps_count
268 rc = ber_printf( ber, "{iO}", so->so_nentries, &cookie );
270 rc = ber_flatten2( ber, &bv, 0 );
274 ctrl = (LDAPControl *)op->o_tmpalloc( sizeof(LDAPControl)+
275 bv.bv_len, op->o_tmpmemctx );
276 ctrl->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
277 ctrl->ldctl_iscritical = 0;
278 ctrl->ldctl_value.bv_val = (char *)(ctrl+1);
279 ctrl->ldctl_value.bv_len = bv.bv_len;
280 AC_MEMCPY( ctrl->ldctl_value.bv_val, bv.bv_val, bv.bv_len );
284 rs->sr_err = LDAP_OTHER;
292 static int pack_sss_response_control(
295 LDAPControl **ctrlsp )
298 BerElementBuffer berbuf;
299 BerElement *ber = (BerElement *)&berbuf;
303 ber_init2( ber, NULL, LBER_USE_DER );
304 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
306 /* Pack error code */
307 rc = ber_printf(ber, "{e}", rs->sr_err);
310 rc = ber_flatten2( ber, &bv, 0 );
313 ctrl = (LDAPControl *)op->o_tmpalloc( sizeof(LDAPControl)+
314 bv.bv_len, op->o_tmpmemctx );
315 ctrl->ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
316 ctrl->ldctl_iscritical = 0;
317 ctrl->ldctl_value.bv_val = (char *)(ctrl+1);
318 ctrl->ldctl_value.bv_len = bv.bv_len;
319 AC_MEMCPY( ctrl->ldctl_value.bv_val, bv.bv_val, bv.bv_len );
323 rs->sr_err = LDAP_OTHER;
331 static void free_sort_op( Connection *conn, sort_op *so )
334 tavl_free( so->so_tree, ch_free );
338 ldap_pvt_thread_mutex_lock( &sort_conns_mutex );
339 sort_conns[conn->c_conn_idx] = NULL;
340 so->so_info->svi_num--;
341 ldap_pvt_thread_mutex_unlock( &sort_conns_mutex );
346 static void send_list(
351 Avlnode *cur_node, *tmp_node;
352 vlv_ctrl *vc = op->o_controls[vlv_cid];
356 LDAPControl *ctrls[2];
358 /* FIXME: it may be better to just flatten the tree into
359 * an array before doing all of this...
362 /* Are we just counting an offset? */
363 if ( BER_BVISNULL( &vc->vc_value )) {
364 if ( vc->vc_offset == vc->vc_count ) {
365 /* wants the last entry in the list */
366 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
367 so->so_vlv_target = so->so_nentries;
368 } else if ( vc->vc_offset == 1 ) {
369 /* wants the first entry in the list */
370 cur_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
371 so->so_vlv_target = 1;
374 /* Just iterate to the right spot */
375 if ( vc->vc_count && vc->vc_count != so->so_nentries ) {
376 if ( vc->vc_offset > vc->vc_count )
378 target = so->so_nentries * vc->vc_offset / vc->vc_count;
380 if ( vc->vc_offset > so->so_nentries ) {
382 so->so_vlv_rc = LDAP_VLV_RANGE_ERROR;
383 pack_vlv_response_control( op, rs, so, ctrls );
385 slap_add_ctrls( op, rs, ctrls );
386 rs->sr_err = LDAP_VLV_ERROR;
389 target = vc->vc_offset;
391 so->so_vlv_target = target;
392 /* Start at left and go right, or start at right and go left? */
393 if ( target < so->so_nentries / 2 ) {
394 cur_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
395 dir = TAVL_DIR_RIGHT;
397 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
399 target = so->so_nentries - target + 1;
401 for ( i=1; i<target; i++ )
402 cur_node = tavl_next( cur_node, dir );
405 /* we're looking for a specific value */
406 sort_ctrl *sc = so->so_ctrl;
407 MatchingRule *mr = sc->sc_keys[0].sk_ordering;
411 if ( mr->smr_normalize ) {
412 rc = mr->smr_normalize( SLAP_MR_VALUE_OF_SYNTAX,
413 mr->smr_syntax, mr, &vc->vc_value, &bv, op->o_tmpmemctx );
415 so->so_vlv_rc = LDAP_INAPPROPRIATE_MATCHING;
416 pack_vlv_response_control( op, rs, so, ctrls );
418 slap_add_ctrls( op, rs, ctrls );
419 rs->sr_err = LDAP_VLV_ERROR;
426 sn = op->o_tmpalloc( sizeof(sort_node) +
427 sc->sc_nkeys * sizeof(struct berval), op->o_tmpmemctx );
428 sn->sn_vals = (struct berval *)(sn+1);
429 sn->sn_conn = op->o_conn->c_conn_idx;
431 for (i=1; i<sc->sc_nkeys; i++) {
432 BER_BVZERO( &sn->sn_vals[i] );
434 cur_node = tavl_find3( so->so_tree, sn, node_cmp, &j );
435 /* didn't find >= match */
438 op->o_tmpfree( sn, op->o_tmpmemctx );
441 so->so_vlv_target = so->so_nentries + 1;
443 sort_node *sn = so->so_tree->avl_data;
444 /* start from the left or the right side? */
445 mr->smr_match( &i, 0, mr->smr_syntax, mr, &bv, &sn->sn_vals[0] );
447 tmp_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
450 tmp_node = tavl_end(so->so_tree, TAVL_DIR_LEFT);
451 dir = TAVL_DIR_RIGHT;
453 for (i=0; tmp_node != cur_node;
454 tmp_node = tavl_next( tmp_node, dir ), i++);
455 so->so_vlv_target = i;
457 if ( bv.bv_val != vc->vc_value.bv_val )
458 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
462 cur_node = tavl_end(so->so_tree, TAVL_DIR_RIGHT);
466 for ( ; i<vc->vc_before; i++ ) {
467 tmp_node = tavl_next( cur_node, TAVL_DIR_LEFT );
468 if ( !tmp_node ) break;
471 j = i + vc->vc_after + 1;
473 for ( i=0; i<j; i++ ) {
474 sort_node *sn = cur_node->avl_data;
476 if ( slapd_shutdown ) break;
478 op->o_bd = select_backend( &sn->sn_dn, 0 );
480 rc = be_entry_get_rw( op, &sn->sn_dn, NULL, NULL, 0, &e );
482 if ( e && rc == LDAP_SUCCESS ) {
484 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
485 rs->sr_err = send_search_entry( op, rs );
486 if ( rs->sr_err == LDAP_UNAVAILABLE )
489 cur_node = tavl_next( cur_node, TAVL_DIR_RIGHT );
490 if ( !cur_node ) break;
492 so->so_vlv_rc = LDAP_SUCCESS;
497 static void send_page( Operation *op, SlapReply *rs, sort_op *so )
499 Avlnode *cur_node = so->so_tree;
500 Avlnode *next_node = NULL;
501 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;
1042 rs->sr_err = LDAP_PROTOCOL_ERROR;
1045 if ( op->o_ctrlflag[vlv_cid] > SLAP_CONTROL_IGNORED ) {
1046 rs->sr_text = "vlv control specified multiple times";
1047 } else if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1048 rs->sr_text = "vlv control value is absent";
1049 } else if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1050 rs->sr_text = "vlv control value is empty";
1052 if ( rs->sr_text != NULL )
1055 op->o_ctrlflag[vlv_cid] = ctrl->ldctl_iscritical ?
1056 SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
1058 ber = (BerElement *)&berbuf;
1059 ber_init2( ber, &ctrl->ldctl_value, 0 );
1061 rs->sr_err = LDAP_PROTOCOL_ERROR;
1063 tag = ber_scanf( ber, "{ii", &vc2.vc_before, &vc2.vc_after );
1064 if ( tag == LBER_ERROR ) {
1068 tag = ber_peek_tag( ber, &len );
1069 if ( tag == LDAP_VLVBYINDEX_IDENTIFIER ) {
1070 tag = ber_scanf( ber, "{ii}", &vc2.vc_offset, &vc2.vc_count );
1071 if ( tag == LBER_ERROR )
1073 BER_BVZERO( &vc2.vc_value );
1074 } else if ( tag == LDAP_VLVBYVALUE_IDENTIFIER ) {
1075 tag = ber_scanf( ber, "m", &vc2.vc_value );
1076 if ( tag == LBER_ERROR || BER_BVISNULL( &vc2.vc_value ))
1081 tag = ber_peek_tag( ber, &len );
1082 if ( tag == LDAP_VLVCONTEXT_IDENTIFIER ) {
1084 tag = ber_scanf( ber, "m", &bv );
1085 if ( tag == LBER_ERROR || bv.bv_len != sizeof(vc2.vc_context))
1087 AC_MEMCPY( &vc2.vc_context, bv.bv_val, bv.bv_len );
1092 vc = op->o_tmpalloc( sizeof(vlv_ctrl), op->o_tmpmemctx );
1094 op->o_controls[vlv_cid] = vc;
1095 rs->sr_err = LDAP_SUCCESS;
1100 static int sssvlv_connection_destroy( BackendDB *be, Connection *conn )
1102 if ( sort_conns[conn->c_conn_idx] )
1103 free_sort_op( conn, sort_conns[conn->c_conn_idx] );
1105 return LDAP_SUCCESS;
1108 static int sssvlv_db_open(
1112 slap_overinst *on = (slap_overinst *)be->bd_info;
1113 sssvlv_info *si = on->on_bi.bi_private;
1116 /* If not set, default to 1/2 of available threads */
1118 si->svi_max = connection_pool_max / 2;
1120 rc = overlay_register_control( be, LDAP_CONTROL_SORTREQUEST );
1121 if ( rc == LDAP_SUCCESS )
1122 rc = overlay_register_control( be, LDAP_CONTROL_VLVREQUEST );
1126 static ConfigTable sssvlv_cfg[] = {
1127 { "sssvlv-max", "num",
1128 2, 2, 0, ARG_INT|ARG_OFFSET,
1129 (void *)offsetof(sssvlv_info, svi_max),
1130 "( OLcfgOvAt:21.1 NAME 'olcSssVlvMax' "
1131 "DESC 'Maximum number of concurrent Sort requests' "
1132 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
1133 { "sssvlv-maxkeys", "num",
1134 2, 2, 0, ARG_INT|ARG_OFFSET,
1135 (void *)offsetof(sssvlv_info, svi_max_keys),
1136 "( OLcfgOvAt:21.2 NAME 'olcSssVlvMaxKeys' "
1137 "DESC 'Maximum number of Keys in a Sort request' "
1138 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
1139 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1142 static ConfigOCs sssvlv_ocs[] = {
1143 { "( OLcfgOvOc:21.1 "
1144 "NAME 'olcSssVlvConfig' "
1145 "DESC 'SSS VLV configuration' "
1146 "SUP olcOverlayConfig "
1147 "MAY ( olcSssVlvMax $ olcSssVlvMaxKeys ) )",
1148 Cft_Overlay, sssvlv_cfg, NULL, NULL },
1152 static int sssvlv_db_init(
1156 slap_overinst *on = (slap_overinst *)be->bd_info;
1159 if ( ov_count == 0 ) {
1162 rc = register_supported_control2( LDAP_CONTROL_SORTREQUEST,
1168 if ( rc != LDAP_SUCCESS ) {
1169 Debug( LDAP_DEBUG_ANY, "Failed to register Sort Request control '%s' (%d)\n",
1170 LDAP_CONTROL_SORTREQUEST, rc, 0 );
1174 rc = register_supported_control2( LDAP_CONTROL_VLVREQUEST,
1180 if ( rc != LDAP_SUCCESS ) {
1181 Debug( LDAP_DEBUG_ANY, "Failed to register VLV Request control '%s' (%d)\n",
1182 LDAP_CONTROL_VLVREQUEST, rc, 0 );
1183 #ifdef SLAP_CONFIG_DELETE
1184 overlay_unregister_control( be, LDAP_CONTROL_SORTREQUEST );
1185 unregister_supported_control( LDAP_CONTROL_SORTREQUEST );
1186 #endif /* SLAP_CONFIG_DELETE */
1191 si = (sssvlv_info *)ch_malloc(sizeof(sssvlv_info));
1192 on->on_bi.bi_private = si;
1196 si->svi_max_keys = SSSVLV_DEFAULT_MAX_KEYS;
1198 if ( dtblsize && !sort_conns ) {
1199 ldap_pvt_thread_mutex_init( &sort_conns_mutex );
1200 /* accommodate for c_conn_idx == -1 */
1201 sort_conns = ch_calloc( sizeof(sort_op *), dtblsize + 1 );
1206 return LDAP_SUCCESS;
1209 static int sssvlv_db_destroy(
1213 slap_overinst *on = (slap_overinst *)be->bd_info;
1214 sssvlv_info *si = (sssvlv_info *)on->on_bi.bi_private;
1217 if ( !ov_count && sort_conns) {
1219 ch_free(sort_conns);
1220 ldap_pvt_thread_mutex_destroy( &sort_conns_mutex );
1223 #ifdef SLAP_CONFIG_DELETE
1224 overlay_unregister_control( be, LDAP_CONTROL_SORTREQUEST );
1225 overlay_unregister_control( be, LDAP_CONTROL_VLVREQUEST );
1226 if ( ov_count == 0 ) {
1227 unregister_supported_control( LDAP_CONTROL_SORTREQUEST );
1228 unregister_supported_control( LDAP_CONTROL_VLVREQUEST );
1230 #endif /* SLAP_CONFIG_DELETE */
1234 on->on_bi.bi_private = NULL;
1236 return LDAP_SUCCESS;
1239 static slap_overinst sssvlv;
1241 int sssvlv_initialize()
1245 sssvlv.on_bi.bi_type = "sssvlv";
1246 sssvlv.on_bi.bi_db_init = sssvlv_db_init;
1247 sssvlv.on_bi.bi_db_destroy = sssvlv_db_destroy;
1248 sssvlv.on_bi.bi_db_open = sssvlv_db_open;
1249 sssvlv.on_bi.bi_connection_destroy = sssvlv_connection_destroy;
1250 sssvlv.on_bi.bi_op_search = sssvlv_op_search;
1252 sssvlv.on_bi.bi_cf_ocs = sssvlv_ocs;
1254 rc = config_register_schema( sssvlv_cfg, sssvlv_ocs );
1258 rc = overlay_register( &sssvlv );
1259 if ( rc != LDAP_SUCCESS ) {
1260 Debug( LDAP_DEBUG_ANY, "Failed to register server side sort overlay\n", 0, 0, 0 );
1266 #if SLAPD_OVER_SSSVLV == SLAPD_MOD_DYNAMIC
1267 int init_module( int argc, char *argv[])
1269 return sssvlv_initialize();
1273 #endif /* SLAPD_OVER_SSSVLV */