]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/overlays/sssvlv.c
check for NULL backend (ITS#6490)
[openldap] / servers / slapd / overlays / sssvlv.c
index f4da73a625829d1cf5b4f0e9a71059391506ce9f..0ee623da4fab9255880b7136eed73f3c592937c1 100644 (file)
@@ -34,6 +34,8 @@
 #include "lutil.h"
 #include "config.h"
 
+#include "../../../libraries/liblber/lber-int.h"       /* ber_rewind */
+
 /* RFC2891: Server Side Sorting
  * RFC2696: Paged Results
  */
@@ -76,7 +78,7 @@ typedef struct sort_key
 
 typedef struct sort_ctrl {
        int sc_nkeys;
-       sort_key sc_keys[0];
+       sort_key sc_keys[1];
 } sort_ctrl;
 
 
@@ -111,6 +113,7 @@ typedef struct sort_op
 /* There is only one conn table for all overlay instances */
 static sort_op **sort_conns;
 static ldap_pvt_thread_mutex_t sort_conns_mutex;
+static int ov_count;
 static const char *debug_header = "sssvlv";
 
 static int sss_cid;
@@ -189,7 +192,6 @@ static int pack_vlv_response_control(
        LDAPControl                     *ctrl;
        BerElementBuffer        berbuf;
        BerElement                      *ber            = (BerElement *)&berbuf;
-       PagedResultsCookie      resp_cookie;
        struct berval           cookie, bv;
        int                                     rc;
 
@@ -340,7 +342,7 @@ static void free_sort_op( Connection *conn, sort_op *so )
        ch_free( so );
 }
 
-static int send_list(
+static void send_list(
        Operation               *op,
        SlapReply               *rs,
        sort_op                 *so)
@@ -428,7 +430,10 @@ range_err:
                for (i=1; i<sc->sc_nkeys; i++) {
                        BER_BVZERO( &sn->sn_vals[i] );
                }
-               cur_node = tavl_find2( so->so_tree, sn, node_cmp );
+               cur_node = tavl_find3( so->so_tree, sn, node_cmp, &j );
+               /* didn't find >= match */
+               if ( j > 0 )
+                       cur_node = NULL;
                op->o_tmpfree( sn, op->o_tmpmemctx );
 
                if ( !cur_node ) {
@@ -534,6 +539,9 @@ static void send_entry(
                "%s: response control: status=%d, text=%s\n",
                debug_header, rs->sr_err, SAFESTR(rs->sr_text, "<None>"));
 
+       if ( !so->so_tree )
+               return;
+
        /* RFC 2891: If critical then send the entries iff they were
         * succesfully sorted.  If non-critical send all entries
         * whether they were sorted or not.
@@ -634,23 +642,24 @@ static int sssvlv_op_response(
                sn2->sn_vals = (struct berval *)(sn2+1);
                AC_MEMCPY( sn2->sn_vals, sn->sn_vals,
                                sc->sc_nkeys * sizeof(struct berval));
-               sn = sn2;
 
-               ptr = (char *)(sn->sn_vals + sc->sc_nkeys);
-               sn->sn_dn.bv_val = ptr;
-               sn->sn_dn.bv_len = rs->sr_entry->e_nname.bv_len;
+               ptr = (char *)(sn2->sn_vals + sc->sc_nkeys);
+               sn2->sn_dn.bv_val = ptr;
+               sn2->sn_dn.bv_len = rs->sr_entry->e_nname.bv_len;
                AC_MEMCPY( ptr, rs->sr_entry->e_nname.bv_val,
                        rs->sr_entry->e_nname.bv_len );
                ptr += rs->sr_entry->e_nname.bv_len;
                *ptr++ = '\0';
                for ( i=0; i<sc->sc_nkeys; i++ ) {
-                       if ( !BER_BVISNULL( &sn->sn_vals[i] )) {
-                               AC_MEMCPY( ptr, sn->sn_vals[i].bv_val, sn->sn_vals[i].bv_len );
-                               sn->sn_vals[i].bv_val = ptr;
-                               ptr += sn->sn_vals[i].bv_len;
+                       if ( !BER_BVISNULL( &sn2->sn_vals[i] )) {
+                               AC_MEMCPY(ptr, sn2->sn_vals[i].bv_val, sn2->sn_vals[i].bv_len);
+                               sn2->sn_vals[i].bv_val = ptr;
+                               ptr += sn2->sn_vals[i].bv_len;
                                *ptr++ = '\0';
                        }
                }
+               op->o_tmpfree( sn, op->o_tmpmemctx );
+               sn = sn2;
                sn->sn_conn = op->o_conn->c_conn_idx;
 
                /* Insert into the AVL tree */
@@ -688,7 +697,7 @@ static int sssvlv_op_search(
        int                                             rc                      = SLAP_CB_CONTINUE;
        int     ok;
        sort_op *so, so2;
-       sort_ctrl *sc = op->o_controls[sss_cid];
+       sort_ctrl *sc;
        PagedResultsState *ps;
        vlv_ctrl *vc;
 
@@ -717,6 +726,7 @@ static int sssvlv_op_search(
                op->o_req_dn.bv_val, op->ors_filterstr.bv_val,
                op->o_ctrlflag[sss_cid]);
 
+       sc = op->o_controls[sss_cid];
        if ( sc->sc_nkeys > si->svi_max_keys ) {
                rs->sr_text = "Too many sort keys";
                rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
@@ -808,8 +818,11 @@ static int sssvlv_op_search(
                        } else {
                                so->so_paged = 0;
                                so->so_page_size = 0;
-                               if ( vc )
+                               if ( vc ) {
                                        so->so_vlv = op->o_ctrlflag[vlv_cid];
+                                       so->so_vlv_target = 0;
+                                       so->so_vlv_rc = 0;
+                               }
                        }
                        so->so_vcontext = (unsigned long)so;
                        so->so_nentries = 0;
@@ -991,7 +1004,7 @@ static int sss_parseCtrl(
        i = count_key( ber );
 
        sc = op->o_tmpalloc( sizeof(sort_ctrl) +
-               i * sizeof(sort_key), op->o_tmpmemctx );
+               (i-1) * sizeof(sort_key), op->o_tmpmemctx );
        sc->sc_nkeys = i;
        op->o_controls[sss_cid] = sc;
 
@@ -1095,12 +1108,13 @@ static int sssvlv_db_open(
 {
        slap_overinst   *on = (slap_overinst *)be->bd_info;
        sssvlv_info *si = on->on_bi.bi_private;
+       int rc;
 
        /* If not set, default to 1/2 of available threads */
        if ( !si->svi_max )
                si->svi_max = connection_pool_max / 2;
 
-       int rc = overlay_register_control( be, LDAP_CONTROL_SORTREQUEST );
+       rc = overlay_register_control( be, LDAP_CONTROL_SORTREQUEST );
        if ( rc == LDAP_SUCCESS )
                rc = overlay_register_control( be, LDAP_CONTROL_VLVREQUEST );
        return rc;
@@ -1152,6 +1166,7 @@ static int sssvlv_db_init(
                sort_conns = ch_calloc( sizeof(sort_op *), dtblsize + 1 );
                sort_conns++;
        }
+       ov_count++;
 
        return LDAP_SUCCESS;
 }
@@ -1163,6 +1178,13 @@ static int sssvlv_db_destroy(
        slap_overinst   *on = (slap_overinst *)be->bd_info;
        sssvlv_info *si = (sssvlv_info *)on->on_bi.bi_private;
        
+       ov_count--;
+       if ( !ov_count && sort_conns) {
+               sort_conns--;
+               ch_free(sort_conns);
+               ldap_pvt_thread_mutex_destroy( &sort_conns_mutex );
+       }
+       
        if ( si ) {
                ch_free( si );
                on->on_bi.bi_private = NULL;