]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/overlays/valsort.c
trim listed modules
[openldap] / servers / slapd / overlays / valsort.c
index 894345207015833ef4587ef8a27ae3177c5fc5b8..b089a7189974e2cfd50423de36ea7c217811a488 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2005 The OpenLDAP Foundation.
+ * Copyright 2005-2007 The OpenLDAP Foundation.
  * Portions copyright 2005 Symas Corporation.
  * All rights reserved.
  *
@@ -212,7 +212,7 @@ static void
 do_sort( Operation *op, Attribute *a, int beg, int num, slap_mask_t sort )
 {
        int i, j, gotnvals;
-       struct berval tmp, ntmp, *vals, *nvals;
+       struct berval tmp, ntmp, *vals = NULL, *nvals;
 
        gotnvals = (a->a_vals != a->a_nvals );
 
@@ -272,11 +272,13 @@ valsort_response( Operation *op, SlapReply *rs )
        valsort_info *vi;
        Attribute *a;
 
-       /* We only want search responses */
-       if ( rs->sr_type != REP_SEARCH ) return SLAP_CB_CONTINUE;
-
-       /* If this is a syncrepl response, pass thru unmodified */
-       if ( op->o_sync > SLAP_CONTROL_IGNORED ) return SLAP_CB_CONTINUE;
+       /* If this is not a search response, or it is a syncrepl response,
+        * or the valsort control wants raw results, pass thru unmodified.
+        */
+       if ( rs->sr_type != REP_SEARCH ||
+               ( _SCM(op->o_sync) > SLAP_CONTROL_IGNORED ) ||
+               ( op->o_ctrlflag[valsort_cid] & SLAP_CONTROL_DATA0))
+               return SLAP_CB_CONTINUE;
                
        on = (slap_overinst *) op->o_bd->bd_info;
        vi = on->on_bi.bi_private;
@@ -312,7 +314,7 @@ valsort_response( Operation *op, SlapReply *rs )
                        gotnvals = (a->a_vals != a->a_nvals );
 
                        for (i=0; i<n; i++) {
-                               char *ptr = strchr( a->a_nvals[i].bv_val, '{' );
+                               char *ptr = ber_bvchr( &a->a_nvals[i], '{' );
                                char *end = NULL;
                                if ( !ptr ) {
                                        Debug(LDAP_DEBUG_TRACE, "weights missing from attr %s "
@@ -337,7 +339,9 @@ valsort_response( Operation *op, SlapReply *rs )
 
                                if ( a->a_vals != a->a_nvals ) {
                                        ptr = a->a_vals[i].bv_val;
-                                       end = strchr( ptr, '}' ) + 1;
+                                       end = ber_bvchr( &a->a_vals[i], '}' );
+                                       assert( end != NULL );
+                                       end++;
                                        for (;*end;)
                                                *ptr++ = *end++;
                                        *ptr = '\0';
@@ -405,7 +409,7 @@ valsort_add( Operation *op, SlapReply *rs )
                if ( !a )
                        continue;
                for (i=0; !BER_BVISNULL( &a->a_vals[i] ); i++) {
-                       ptr = strchr(a->a_vals[i].bv_val, '{' );
+                       ptr = ber_bvchr(&a->a_vals[i], '{' );
                        if ( !ptr ) {
                                Debug(LDAP_DEBUG_TRACE, "weight missing from attribute %s\n",
                                        vi->vi_ad->ad_cname.bv_val, 0, 0);
@@ -449,7 +453,7 @@ valsort_modify( Operation *op, SlapReply *rs )
                if ( !ml )
                        continue;
                for (i=0; !BER_BVISNULL( &ml->sml_values[i] ); i++) {
-                       ptr = strchr(ml->sml_values[i].bv_val, '{' );
+                       ptr = ber_bvchr(&ml->sml_values[i], '{' );
                        if ( !ptr ) {
                                Debug(LDAP_DEBUG_TRACE, "weight missing from attribute %s\n",
                                        vi->vi_ad->ad_cname.bv_val, 0, 0);
@@ -470,6 +474,14 @@ valsort_modify( Operation *op, SlapReply *rs )
        return SLAP_CB_CONTINUE;
 }
 
+static int
+valsort_db_open(
+       BackendDB *be
+)
+{
+       return overlay_register_control( be, LDAP_CONTROL_VALSORT );
+}
+
 static int
 valsort_destroy(
        BackendDB *be
@@ -493,24 +505,39 @@ valsort_parseCtrl(
        SlapReply *rs,
        LDAPControl *ctrl )
 {
-       if ( ctrl->ldctl_value.bv_len ) {
-               rs->sr_text = "valSort control value not empty";
+       ber_tag_t tag;
+       BerElementBuffer berbuf;
+       BerElement *ber = (BerElement *)&berbuf;
+       ber_int_t flag = 0;
+
+       if ( ctrl->ldctl_value.bv_len == 0 ) {
+               rs->sr_text = "valSort control value is empty (or absent)";
+               return LDAP_PROTOCOL_ERROR;
+       }
+
+       ber_init2( ber, &ctrl->ldctl_value, 0 );
+       if (( tag = ber_scanf( ber, "{b}", &flag )) == LBER_ERROR ) {
+               rs->sr_text = "valSort control: flag decoding error";
                return LDAP_PROTOCOL_ERROR;
        }
+
        op->o_ctrlflag[valsort_cid] = ctrl->ldctl_iscritical ?
                SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
+       if ( flag )
+               op->o_ctrlflag[valsort_cid] |= SLAP_CONTROL_DATA0;
 
        return LDAP_SUCCESS;
 }
 
 static slap_overinst valsort;
 
-int valsort_init()
+int valsort_initialize( void )
 {
-       int i, rc;
+       int rc;
 
        valsort.on_bi.bi_type = "valsort";
        valsort.on_bi.bi_db_destroy = valsort_destroy;
+       valsort.on_bi.bi_db_open = valsort_db_open;
 
        valsort.on_bi.bi_op_add = valsort_add;
        valsort.on_bi.bi_op_modify = valsort_modify;
@@ -537,7 +564,7 @@ int valsort_init()
 
 #if SLAPD_OVER_VALSORT == SLAPD_MOD_DYNAMIC
 int init_module( int argc, char *argv[]) {
-       return valsort_init();
+       return valsort_initialize();
 }
 #endif