]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/syncrepl.c
for consistency, always represent UUIDs correctly (ITS#5168; really, a de-normalize...
[openldap] / servers / slapd / syncrepl.c
index 3f8e620c15495295fb37dee655c69d8d9ec1e002..b0680cad9ba5a340066d6a5b041d480447b3506f 100644 (file)
@@ -63,7 +63,7 @@ typedef struct syncinfo_s {
        BackendDB               *si_wbe;
        struct re_s             *si_re;
        int                     si_rid;
-       char                    si_ridtxt[8];
+       char                    si_ridtxt[ STRLENOF("rid=999") + 1 ];
        slap_bindconf           si_bindconf;
        struct berval           si_base;
        struct berval           si_logbase;
@@ -103,7 +103,7 @@ typedef struct syncinfo_s {
 } syncinfo_t;
 
 static int syncuuid_cmp( const void *, const void * );
-static void avl_ber_bvfree( void * );
+static int avl_presentlist_insert( syncinfo_t* si, struct berval *syncUUID );
 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray, struct berval * );
 static int syncrepl_message_to_op(
                                        syncinfo_t *, Operation *, LDAPMessage * );
@@ -126,6 +126,26 @@ static int null_callback( Operation *, SlapReply * );
 
 static AttributeDescription *sync_descs[4];
 
+static const char *
+syncrepl_state2str( int state )
+{
+       switch ( state ) {
+       case LDAP_SYNC_PRESENT:
+               return "PRESENT";
+
+       case LDAP_SYNC_ADD:
+               return "ADD";
+
+       case LDAP_SYNC_MODIFY:
+               return "MODIFY";
+
+       case LDAP_SYNC_DELETE:
+               return "DELETE";
+       }
+
+       return "UNKNOWN";
+}
+
 static void
 init_syncrepl(syncinfo_t *si)
 {
@@ -542,9 +562,7 @@ do_syncrep1(
                ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
                rc = backend_operational( op, &rs );
                if ( rc == LDAP_SUCCESS && a.a_vals ) {
-                       int num;
-                       for (i=0; !BER_BVISNULL( &a.a_vals[i] ); i++) ;
-                       num = i;
+                       int num = a.a_numvals;
                        /* check for differences */
                        if ( num != si->si_cookieState->cs_num ) {
                                changed = 1;
@@ -742,10 +760,19 @@ do_syncrep2(
                                /* we can't work without the control */
                                rctrlp = NULL;
                                if ( rctrls ) {
+                                       LDAPControl **next;
                                        /* NOTE: make sure we use the right one;
                                         * a better approach would be to run thru
                                         * the whole list and take care of all */
-                                       rctrlp = ldap_find_control( LDAP_CONTROL_SYNC_STATE, rctrls );
+                                       rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_STATE, rctrls, &next );
+                                       if ( next && ldap_control_find( LDAP_CONTROL_SYNC_STATE, next, NULL ) )
+                                       {
+                                               Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
+                                                       "got search entry with multiple "
+                                                       "Sync State control\n", si->si_ridtxt, 0, 0 );
+                                               rc = -1;
+                                               goto done;
+                                       }
                                }
                                if ( rctrlp == NULL ) {
                                        Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
@@ -760,13 +787,19 @@ do_syncrep2(
                                 * (happens with back-sql...) */
                                if ( BER_BVISEMPTY( &syncUUID ) ) {
                                        Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
-                                               "got empty syncUUID\n", si->si_ridtxt, 0, 0 );
+                                               "got empty syncUUID with LDAP_SYNC_%s\n",
+                                               si->si_ridtxt,
+                                               syncrepl_state2str( syncstate ), 0 );
                                        ldap_controls_free( rctrls );
                                        rc = -1;
                                        goto done;
                                }
                                if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
                                        ber_scanf( ber, /*"{"*/ "m}", &cookie );
+
+                                       Debug( LDAP_DEBUG_SYNC, "do_syncrep2: cookie=%s\n",
+                                               BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0, 0 );
+
                                        if ( !BER_BVISNULL( &cookie ) ) {
                                                ch_free( syncCookie.octet_str.bv_val );
                                                ber_dupbv( &syncCookie.octet_str, &cookie );
@@ -834,6 +867,10 @@ do_syncrep2(
                                        ber_scanf( ber, "{" /*"}"*/);
                                        if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
                                                ber_scanf( ber, "m", &cookie );
+
+                                               Debug( LDAP_DEBUG_SYNC, "do_syncrep2: cookie=%s\n",
+                                                       BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0, 0 );
+
                                                if ( !BER_BVISNULL( &cookie ) ) {
                                                        ch_free( syncCookie.octet_str.bv_val );
                                                        ber_dupbv( &syncCookie.octet_str, &cookie);
@@ -871,7 +908,7 @@ do_syncrep2(
                                                syncrepl_del_nonpresent( op, si, NULL,
                                                        &syncCookie.ctxcsn[m] );
                                        } else {
-                                               avl_free( si->si_presentlist, avl_ber_bvfree );
+                                               avl_free( si->si_presentlist, ch_free );
                                                si->si_presentlist = NULL;
                                        }
                                }
@@ -922,6 +959,10 @@ do_syncrep2(
                                                if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
                                                {
                                                        ber_scanf( ber, "m", &cookie );
+
+                                                       Debug( LDAP_DEBUG_SYNC, "do_syncrep2: cookie=%s\n",
+                                                               BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0, 0 );
+
                                                        if ( !BER_BVISNULL( &cookie ) ) {
                                                                ch_free( syncCookie.octet_str.bv_val );
                                                                ber_dupbv( &syncCookie.octet_str, &cookie );
@@ -949,6 +990,10 @@ do_syncrep2(
                                                        LDAP_TAG_SYNC_COOKIE )
                                                {
                                                        ber_scanf( ber, "m", &cookie );
+
+                                                       Debug( LDAP_DEBUG_SYNC, "do_syncrep2: cookie=%s\n",
+                                                               BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0, 0 );
+
                                                        if ( !BER_BVISNULL( &cookie ) ) {
                                                                ch_free( syncCookie.octet_str.bv_val );
                                                                ber_dupbv( &syncCookie.octet_str, &cookie );
@@ -973,15 +1018,8 @@ do_syncrep2(
                                                } else {
                                                        int i;
                                                        for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
-                                                               struct berval *syncuuid_bv;
-                                                               syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
-                                                               slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
-                                                               if ( avl_insert( &si->si_presentlist,
-                                                                       (caddr_t) syncuuid_bv,
-                                                                       syncuuid_cmp, avl_dup_error ) )
-                                                               {
-                                                                       ber_bvfree( syncuuid_bv );
-                                                               }
+                                                               (void)avl_presentlist_insert( si, &syncUUIDs[i] );
+                                                               slap_sl_free( syncUUIDs[i].bv_val, op->o_tmpmemctx );
                                                        }
                                                        slap_sl_free( syncUUIDs, op->o_tmpmemctx );
                                                }
@@ -1033,7 +1071,9 @@ do_syncrep2(
 
                        default:
                                Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
-                                       "unknown message\n", si->si_ridtxt, 0, 0 );
+                                       "unknown message (0x%02lx)\n",
+                                       si->si_ridtxt,
+                                       (unsigned long)ldap_msgtype( msg ), 0 );
                                break;
 
                        }
@@ -1246,16 +1286,16 @@ reload:
        if ( rc ) {
                if ( fail == RETRYNUM_TAIL ) {
                        Debug( LDAP_DEBUG_ANY,
-                               "do_syncrepl: rid %03d quitting\n",
-                               si->si_rid, 0, 0 );
+                               "do_syncrepl: %s quitting\n",
+                               si->si_ridtxt, 0, 0 );
                } else if ( fail > 0 ) {
                        Debug( LDAP_DEBUG_ANY,
-                               "do_syncrepl: rid %03d retrying (%d retries left)\n",
-                               si->si_rid, fail, 0 );
+                               "do_syncrepl: %s retrying (%d retries left)\n",
+                               si->si_ridtxt, fail, 0 );
                } else {
                        Debug( LDAP_DEBUG_ANY,
-                               "do_syncrepl: rid %03d retrying\n",
-                               si->si_rid, 0, 0 );
+                               "do_syncrepl: %s retrying\n",
+                               si->si_ridtxt, 0, 0 );
                }
        }
 
@@ -1350,6 +1390,7 @@ syncrepl_accesslog_mods(
                        mod->sml_type = ad->ad_cname;
                        mod->sml_values = NULL;
                        mod->sml_nvalues = NULL;
+                       mod->sml_numvals = 0;
 
                        *modtail = mod;
                        modtail = &mod->sml_next;
@@ -1359,6 +1400,7 @@ syncrepl_accesslog_mods(
                        bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
                        ber_dupbv( &bv2, &bv );
                        ber_bvarray_add( &mod->sml_values, &bv2 );
+                       mod->sml_numvals++;
                }
        }
        return modlist;
@@ -1691,6 +1733,7 @@ syncrepl_message_to_entry(
                mod->sml_type = tmp.sml_type;
                mod->sml_values = tmp.sml_values;
                mod->sml_nvalues = NULL;
+               mod->sml_numvals = 0;   /* slap_mods_check will set this */
 
                *modtail = mod;
                modtail = &mod->sml_next;
@@ -1788,6 +1831,29 @@ typedef struct dninfo {
        Modifications *mods;    /* the modlist we compared */
 } dninfo;
 
+/* return 1 if inserted, 0 otherwise */
+static int
+avl_presentlist_insert(
+       syncinfo_t* si,
+       struct berval *syncUUID )
+{
+       struct berval *syncuuid_bv = ch_malloc( sizeof( struct berval ) + syncUUID->bv_len + 1 );
+
+       syncuuid_bv->bv_len = syncUUID->bv_len;
+       syncuuid_bv->bv_val = (char *)&syncuuid_bv[1];
+       AC_MEMCPY( syncuuid_bv->bv_val, syncUUID->bv_val, syncUUID->bv_len );
+       syncuuid_bv->bv_val[ syncuuid_bv->bv_len ] = '\0';
+
+       if ( avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
+               syncuuid_cmp, avl_dup_error ) )
+       {
+               ch_free( syncuuid_bv );
+               return 0;
+       }
+
+       return 1;
+}
+
 static int
 syncrepl_entry(
        syncinfo_t* si,
@@ -1799,7 +1865,7 @@ syncrepl_entry(
 {
        Backend *be = op->o_bd;
        slap_callback   cb = { NULL, NULL, NULL, NULL };
-       struct berval   *syncuuid_bv = NULL;
+       int syncuuid_inserted = 0;
        struct berval   syncUUID_strrep = BER_BVNULL;
 
        SlapReply       rs_search = {REP_RESULT};
@@ -1807,53 +1873,21 @@ syncrepl_entry(
        SlapReply       rs_add = {REP_RESULT};
        SlapReply       rs_modify = {REP_RESULT};
        Filter f = {0};
-#ifdef LDAP_COMP_MATCH
-       AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
-#else
-       AttributeAssertion ava = { NULL, BER_BVNULL };
-#endif
+       AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
        int rc = LDAP_SUCCESS;
 
        struct berval pdn = BER_BVNULL;
        dninfo dni = {0};
        int     retry = 1;
+       int     freecsn = 1;
 
-       switch( syncstate ) {
-       case LDAP_SYNC_PRESENT:
-               Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s %s\n",
-                                       si->si_ridtxt,
-                                       "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
-               break;
-       case LDAP_SYNC_ADD:
-               Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s %s\n",
-                                       si->si_ridtxt,
-                                       "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
-               break;
-       case LDAP_SYNC_DELETE:
-               Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s %s\n",
-                                       si->si_ridtxt,
-                                       "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
-               break;
-       case LDAP_SYNC_MODIFY:
-               Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s %s\n",
-                                       si->si_ridtxt,
-                                       "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
-               break;
-       default:
-               Debug( LDAP_DEBUG_ANY, "syncrepl_entry: %s %s\n",
-                                       si->si_ridtxt,
-                                       "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
-       }
+       Debug( LDAP_DEBUG_SYNC,
+               "syncrepl_entry: %s LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_%s)\n",
+               si->si_ridtxt, syncrepl_state2str( syncstate ), 0 );
 
        if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ) ) {
                if ( !si->si_refreshPresent ) {
-                       syncuuid_bv = ber_dupbv( NULL, syncUUID );
-                       if ( avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
-                               syncuuid_cmp, avl_dup_error ) )
-                       {
-                               ber_bvfree( syncuuid_bv );
-                               syncuuid_bv = NULL;
-                       }
+                       syncuuid_inserted = avl_presentlist_insert( si, syncUUID );
                }
        }
 
@@ -1890,7 +1924,7 @@ syncrepl_entry(
        ava.aa_desc = slap_schema.si_ad_entryUUID;
        ava.aa_value = *syncUUID;
 
-       if ( syncuuid_bv ) {
+       if ( syncuuid_inserted ) {
                Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s inserted UUID %s\n",
                        si->si_ridtxt, syncUUID_strrep.bv_val, 0 );
        }
@@ -1951,6 +1985,8 @@ syncrepl_entry(
                                si->si_ridtxt, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
        }
 
+       assert( BER_BVISNULL( &op->o_csn ) );
+
        slap_op_time( &op->o_time, &op->o_tincr );
        switch ( syncstate ) {
        case LDAP_SYNC_ADD:
@@ -1965,6 +2001,7 @@ syncrepl_entry(
                                 */
                                assert( BER_BVISNULL( &op->o_csn ) );
                                op->o_csn = a->a_vals[0];
+                               freecsn = 0;
                        }
                }
 retry_add:;
@@ -2197,8 +2234,12 @@ done:
        if ( !BER_BVISNULL( &dni.dn ) ) {
                op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
        }
-       if ( entry )
+       if ( entry ) {
                entry_free( entry );
+       }
+       if ( !BER_BVISNULL( &op->o_csn ) && freecsn ) {
+               op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
+       }
        BER_BVZERO( &op->o_csn );
        return rc;
 }
@@ -2246,11 +2287,7 @@ syncrepl_del_nonpresent(
 
        if ( uuids ) {
                Filter uf;
-#ifdef LDAP_COMP_MATCH
-               AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
-#else
-               AttributeAssertion eq = { NULL, BER_BVNULL };
-#endif
+               AttributeAssertion eq = ATTRIBUTEASSERTION_INIT;
                int i;
 
                op->ors_attrsonly = 1;
@@ -2327,6 +2364,7 @@ syncrepl_del_nonpresent(
                                mod1.sml_flags = 0;
                                mod1.sml_desc = slap_schema.si_ad_objectClass;
                                mod1.sml_type = mod1.sml_desc->ad_cname;
+                               mod1.sml_numvals = 2;
                                mod1.sml_values = &gcbva[0];
                                mod1.sml_nvalues = NULL;
                                mod1.sml_next = &mod2;
@@ -2335,6 +2373,7 @@ syncrepl_del_nonpresent(
                                mod2.sml_flags = 0;
                                mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
                                mod2.sml_type = mod2.sml_desc->ad_cname;
+                               mod1.sml_numvals = 1;
                                mod2.sml_values = &gcbva[1];
                                mod2.sml_nvalues = NULL;
                                mod2.sml_next = NULL;
@@ -2368,6 +2407,10 @@ syncrepl_del_nonpresent(
                        ber_bvfree( np_prev->npe_name );
                        ber_bvfree( np_prev->npe_nname );
                        ch_free( np_prev );
+
+                       if ( slapd_shutdown ) {
+                               break;
+                       }
                }
 
                slap_graduate_commit_csn( op );
@@ -2458,6 +2501,7 @@ syncrepl_add_glue(
 
                a = attr_alloc( slap_schema.si_ad_objectClass );
 
+               a->a_numvals = 2;
                a->a_vals = ch_calloc( 3, sizeof( struct berval ) );
                ber_dupbv( &a->a_vals[0], &gcbva[0] );
                ber_dupbv( &a->a_vals[1], &gcbva[1] );
@@ -2470,6 +2514,7 @@ syncrepl_add_glue(
 
                a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
 
+               a->a_numvals = 1;
                a->a_vals = ch_calloc( 2, sizeof( struct berval ) );
                ber_dupbv( &a->a_vals[0], &gcbva[1] );
                ber_dupbv( &a->a_vals[1], &gcbva[2] );
@@ -2548,6 +2593,7 @@ syncrepl_updateCookie(
        mod[0].sml_type = mod[0].sml_desc->ad_cname;
        mod[0].sml_values = NULL;
        mod[0].sml_nvalues = NULL;
+       mod[0].sml_numvals = 0;
        mod[0].sml_next = &mod[1];
 
        mod[1].sml_op = LDAP_MOD_ADD;
@@ -2555,6 +2601,7 @@ syncrepl_updateCookie(
        mod[1].sml_type = mod[0].sml_desc->ad_cname;
        mod[1].sml_values = NULL;
        mod[1].sml_nvalues = NULL;
+       mod[1].sml_numvals = 0;
        mod[1].sml_next = NULL;
 
        ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
@@ -2570,8 +2617,10 @@ syncrepl_updateCookie(
                                si->si_cookieState->cs_vals[j].bv_val, len ) > 0 ) {
                                ber_bvarray_add_x( &mod[0].sml_values,
                                        &si->si_cookieState->cs_vals[j], op->o_tmpmemctx );
+                               mod[0].sml_numvals++;
                                ber_bvarray_add_x( &mod[1].sml_values,
                                        &syncCookie->ctxcsn[i], op->o_tmpmemctx );
+                               mod[1].sml_numvals++;
                                if ( BER_BVISNULL( &first ))
                                        first = syncCookie->ctxcsn[i];
                        }
@@ -2581,6 +2630,7 @@ syncrepl_updateCookie(
                if ( j == si->si_cookieState->cs_num ) {
                        ber_bvarray_add_x( &mod[1].sml_values,
                                &syncCookie->ctxcsn[i], op->o_tmpmemctx );
+                       mod[1].sml_numvals++;
                        if ( BER_BVISNULL( &first ))
                                first = syncCookie->ctxcsn[i];
                }
@@ -2669,76 +2719,69 @@ attr_cmp( Operation *op, Attribute *old, Attribute *new,
        modtail = *mret;
 
        if ( old ) {
-               int n, o, d, a, *adds, *dels;
+               int n, o, nn, no;
+               struct berval **adds, **dels;
                /* count old and new */
                for ( o=0; old->a_vals[o].bv_val; o++ ) ;
                for ( n=0; new->a_vals[n].bv_val; n++ ) ;
 
-               adds = op->o_tmpalloc( sizeof(int) * n, op->o_tmpmemctx );
-               dels = op->o_tmpalloc( sizeof(int) * o, op->o_tmpmemctx );
-               d = 0;
-               a = 0;
-               i = 0;
+               /* there MUST be both old and new values */
+               assert( o != 0 );
+               assert( n != 0 );
                j = 0;
 
-               while ( i < o && j < n ) {
-                       int k;
-                       if ( bvmatch( &old->a_vals[i], &new->a_vals[j] ) ) {
-                               i++;
-                               j++;
-                               continue;
-                       }
-                       for ( k = j + 1; k<n; k++ ) {
-                               if ( bvmatch( &old->a_vals[i], &new->a_vals[k] ) ) {
-                                       break;
-                               }
-                       }
-                       /* an old value was deleted */
-                       if ( k == n ) {
-                               dels[d++] = i++;
-                               continue;
-                       }
-                       /* old value still exists, move to next */
-                       i++;
-                       for ( k = i; k < o; k++ ) {
-                               if ( bvmatch( &old->a_vals[k], &new->a_vals[j] ) ) {
+               adds = op->o_tmpalloc( sizeof(struct berval *) * n, op->o_tmpmemctx );
+               dels = op->o_tmpalloc( sizeof(struct berval *) * o, op->o_tmpmemctx );
+
+               for ( i=0; i<o; i++ ) dels[i] = &old->a_vals[i];
+               for ( i=0; i<n; i++ ) adds[i] = &new->a_vals[i];
+
+               nn = n; no = o;
+
+               for ( i=0; i<o; i++ ) {
+                       for ( j=0; j<n; j++ ) {
+                               if ( !adds[j] )
+                                       continue;
+                               if ( bvmatch( dels[i], adds[j] ) ) {
+                                       no--;
+                                       nn--;
+                                       adds[j] = NULL;
+                                       dels[i] = NULL;
                                        break;
                                }
                        }
-                       if ( k == o ) {
-                               adds[a++] = j++;
-                       }
                }
-               while ( i < o )
-                       dels[d++] = i++;
-               while ( j < n )
-                       adds[a++] = j++;
 
+               i = j;
                /* all old values were deleted, just use the replace op */
-               if ( d == o ) {
+               if ( no == o ) {
                        i = j-1;
-               } else if ( d ) {
+               } else if ( no ) {
                /* delete some values */
                        mod = ch_malloc( sizeof( Modifications ) );
                        mod->sml_op = LDAP_MOD_DELETE;
                        mod->sml_flags = 0;
                        mod->sml_desc = old->a_desc;
                        mod->sml_type = mod->sml_desc->ad_cname;
-                       mod->sml_values = ch_malloc( ( d + 1 ) * sizeof(struct berval) );
+                       mod->sml_numvals = no;
+                       mod->sml_values = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
                        if ( old->a_vals != old->a_nvals ) {
-                               mod->sml_nvalues = ch_malloc( ( d + 1 ) * sizeof(struct berval) );
+                               mod->sml_nvalues = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
                        } else {
                                mod->sml_nvalues = NULL;
                        }
-                       for ( i = 0; i < d; i++ ) {
-                               ber_dupbv( &mod->sml_values[i], &old->a_vals[dels[i]] );
+                       j = 0;
+                       for ( i = 0; i < o; i++ ) {
+                               if ( !dels[i] ) continue;
+                               ber_dupbv( &mod->sml_values[j], &old->a_vals[i] );
                                if ( mod->sml_nvalues ) {
-                                       ber_dupbv( &mod->sml_nvalues[i], &old->a_nvals[dels[i]] );
+                                       ber_dupbv( &mod->sml_nvalues[j], &old->a_nvals[i] );
                                }
+                               j++;
                        }
-                       BER_BVZERO( &mod->sml_values[i] );
+                       BER_BVZERO( &mod->sml_values[j] );
                        if ( mod->sml_nvalues ) {
-                               BER_BVZERO( &mod->sml_nvalues[i] );
+                               BER_BVZERO( &mod->sml_nvalues[j] );
                        }
                        *modtail = mod;
                        modtail = &mod->sml_next;
@@ -2746,27 +2789,31 @@ attr_cmp( Operation *op, Attribute *old, Attribute *new,
                }
                op->o_tmpfree( dels, op->o_tmpmemctx );
                /* some values were added */
-               if ( a && d < o ) {
+               if ( nn && no < o ) {
                        mod = ch_malloc( sizeof( Modifications ) );
                        mod->sml_op = LDAP_MOD_ADD;
                        mod->sml_flags = 0;
                        mod->sml_desc = old->a_desc;
                        mod->sml_type = mod->sml_desc->ad_cname;
-                       mod->sml_values = ch_malloc( ( a + 1 ) * sizeof(struct berval) );
+                       mod->sml_numvals = nn;
+                       mod->sml_values = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
                        if ( old->a_vals != old->a_nvals ) {
-                               mod->sml_nvalues = ch_malloc( ( a + 1 ) * sizeof(struct berval) );
+                               mod->sml_nvalues = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
                        } else {
                                mod->sml_nvalues = NULL;
                        }
-                       for ( i = 0; i < a; i++ ) {
-                               ber_dupbv( &mod->sml_values[i], &new->a_vals[adds[i]] );
+                       j = 0;
+                       for ( i = 0; i < n; i++ ) {
+                               if ( !adds[i] ) continue;
+                               ber_dupbv( &mod->sml_values[j], &new->a_vals[i] );
                                if ( mod->sml_nvalues ) {
-                                       ber_dupbv( &mod->sml_nvalues[i], &new->a_nvals[adds[i]] );
+                                       ber_dupbv( &mod->sml_nvalues[j], &new->a_nvals[i] );
                                }
+                               j++;
                        }
-                       BER_BVZERO( &mod->sml_values[i] );
+                       BER_BVZERO( &mod->sml_values[j] );
                        if ( mod->sml_nvalues ) {
-                               BER_BVZERO( &mod->sml_nvalues[i] );
+                               BER_BVZERO( &mod->sml_nvalues[j] );
                        }
                        *modtail = mod;
                        modtail = &mod->sml_next;
@@ -2837,11 +2884,11 @@ dn_callback(
                                        oldRDN.bv_len -= oldVal.bv_len + 2;
                                        slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
                                        a = attr_find( dni->new_entry->e_attrs, ad );
-                                       if ( !a || value_find_ex( ad,
+                                       if ( !a || attr_valfind( a,
                                                SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH |
                                                SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
-                                               SLAP_MR_VALUE_OF_SYNTAX, a->a_nvals,
-                                               &oldVal, op->o_tmpmemctx ) != LDAP_SUCCESS )
+                                               SLAP_MR_VALUE_OF_SYNTAX,
+                                               &oldVal, NULL, op->o_tmpmemctx ) != LDAP_SUCCESS )
                                        {
                                                dni->delOldRDN = 1;
                                        }
@@ -2858,11 +2905,12 @@ dn_callback(
                                new = attr_find( dni->new_entry->e_attrs,
                                        slap_schema.si_ad_entryCSN );
                                if ( new && old ) {
-                                       int len = old->a_vals[0].bv_len;
+                                       int rc, len = old->a_vals[0].bv_len;
                                        if ( len > new->a_vals[0].bv_len )
                                                len = new->a_vals[0].bv_len;
-                                       if ( memcmp( old->a_vals[0].bv_val,
-                                               new->a_vals[0].bv_val, len ) >= 0 ) {
+                                       rc = memcmp( old->a_vals[0].bv_val,
+                                               new->a_vals[0].bv_val, len );
+                                       if ( rc > 0 ) {
                                                Debug( LDAP_DEBUG_SYNC,
                                                        "dn_callback : new entry is older than ours "
                                                        "%s ours %s, new %s\n",
@@ -2870,6 +2918,14 @@ dn_callback(
                                                        old->a_vals[0].bv_val,
                                                        new->a_vals[0].bv_val );
                                                return LDAP_SUCCESS;
+                                       } else if ( rc == 0 ) {
+                                               Debug( LDAP_DEBUG_SYNC,
+                                                       "dn_callback : entries have identical CSN "
+                                                       "%s ours %s, new %s\n",
+                                                       rs->sr_entry->e_name.bv_val,
+                                                       old->a_vals[0].bv_val,
+                                                       new->a_vals[0].bv_val );
+                                               return LDAP_SUCCESS;
                                        }
                                }
 
@@ -2919,6 +2975,7 @@ dn_callback(
                                                        mod->sml_flags = 0;
                                                        mod->sml_desc = old->a_desc;
                                                        mod->sml_type = mod->sml_desc->ad_cname;
+                                                       mod->sml_numvals = 0;
                                                        mod->sml_values = NULL;
                                                        mod->sml_nvalues = NULL;
                                                        *modtail = mod;
@@ -2966,13 +3023,11 @@ nonpresent_callback(
        struct nonpresent_entry *np_entry;
 
        if ( rs->sr_type == REP_RESULT ) {
-               count = avl_free( si->si_presentlist, avl_ber_bvfree );
+               count = avl_free( si->si_presentlist, ch_free );
                si->si_presentlist = NULL;
 
        } else if ( rs->sr_type == REP_SEARCH ) {
                if ( !( si->si_refreshDelete & NP_DELETE_ONE ) ) {
-                       char buf[sizeof("rid=000 not")];
-
                        a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
 
                        if ( a ) {
@@ -2980,13 +3035,15 @@ nonpresent_callback(
                                        syncuuid_cmp );
                        }
 
-                       if ( slap_debug & LDAP_DEBUG_SYNC ) {
-                               sprintf( buf, "%s %s", si->si_ridtxt,
+                       if ( LogTest( LDAP_DEBUG_SYNC ) ) {
+                               char buf[sizeof("rid=999 not")];
+
+                               snprintf( buf, sizeof(buf), "%s %s", si->si_ridtxt,
                                        present_uuid ? "got" : "not" );
-                       }
 
-                       Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: %s UUID %s, dn %s\n",
-                               buf, a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val );
+                               Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: %s UUID %s, dn %s\n",
+                                       buf, a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val );
+                       }
 
                        if ( a == NULL ) return 0;
                }
@@ -3000,8 +3057,7 @@ nonpresent_callback(
 
                } else {
                        avl_delete( &si->si_presentlist,
-                                       &a->a_nvals[0], syncuuid_cmp );
-                       ch_free( present_uuid->bv_val );
+                               &a->a_nvals[0], syncuuid_cmp );
                        ch_free( present_uuid );
                }
        }
@@ -3139,18 +3195,6 @@ syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
        return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
 }
 
-static void
-avl_ber_bvfree( void *v_bv )
-{
-       struct berval   *bv = (struct berval *)v_bv;
-       
-       if( v_bv == NULL ) return;
-       if ( !BER_BVISNULL( bv ) ) {
-               ch_free( bv->bv_val );
-       }
-       ch_free( (char *) bv );
-}
-
 void
 syncinfo_free( syncinfo_t *sie, int free_all )
 {
@@ -3242,7 +3286,7 @@ syncinfo_free( syncinfo_t *sie, int free_all )
                }
                slap_sync_cookie_free( &sie->si_syncCookie, 0 );
                if ( sie->si_presentlist ) {
-                   avl_free( sie->si_presentlist, avl_ber_bvfree );
+                   avl_free( sie->si_presentlist, ch_free );
                }
                while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist ) ) {
                        struct nonpresent_entry* npe;
@@ -3344,10 +3388,10 @@ parse_syncrepl_line(
                                Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
                                return -1;
                        }
-                       if ( tmp >= 1000 || tmp < 0 ) {
+                       if ( tmp > SLAP_SYNC_SID_MAX || tmp < 0 ) {
                                snprintf( c->cr_msg, sizeof( c->cr_msg ),
                                        "Error: parse_syncrepl_line: "
-                                       "syncrepl id %d is out of range [0..999]", tmp );
+                                       "syncrepl id %d is out of range [0..4095]", tmp );
                                Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
                                return -1;
                        }
@@ -3539,12 +3583,10 @@ parse_syncrepl_line(
                                si->si_interval = 0;
                        } else if ( strchr( val, ':' ) != NULL ) {
                                char *next, *ptr = val;
-                               unsigned dd, hh, mm, ss;
+                               int dd, hh, mm, ss;
 
-                               /* NOTE: the test for ptr[ 0 ] == '-'
-                                * should go before the call to strtoul() */
-                               dd = strtoul( ptr, &next, 10 );
-                               if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' ) {
+                               dd = strtol( ptr, &next, 10 );
+                               if ( next == ptr || next[0] != ':' || dd < 0 ) {
                                        snprintf( c->cr_msg, sizeof( c->cr_msg ),
                                                "Error: parse_syncrepl_line: "
                                                "invalid interval \"%s\", unable to parse days", val );
@@ -3552,8 +3594,8 @@ parse_syncrepl_line(
                                        return -1;
                                }
                                ptr = next + 1;
-                               hh = strtoul( ptr, &next, 10 );
-                               if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || hh > 24 ) {
+                               hh = strtol( ptr, &next, 10 );
+                               if ( next == ptr || next[0] != ':' || hh < 0 || hh > 24 ) {
                                        snprintf( c->cr_msg, sizeof( c->cr_msg ),
                                                "Error: parse_syncrepl_line: "
                                                "invalid interval \"%s\", unable to parse hours", val );
@@ -3561,8 +3603,8 @@ parse_syncrepl_line(
                                        return -1;
                                }
                                ptr = next + 1;
-                               mm = strtoul( ptr, &next, 10 );
-                               if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || mm > 60 ) {
+                               mm = strtol( ptr, &next, 10 );
+                               if ( next == ptr || next[0] != ':' || mm < 0 || mm > 60 ) {
                                        snprintf( c->cr_msg, sizeof( c->cr_msg ),
                                                "Error: parse_syncrepl_line: "
                                                "invalid interval \"%s\", unable to parse minutes", val );
@@ -3570,8 +3612,8 @@ parse_syncrepl_line(
                                        return -1;
                                }
                                ptr = next + 1;
-                               ss = strtoul( ptr, &next, 10 );
-                               if ( ptr[ 0 ] == '-' || next == ptr || next[0] != '\0' || ss > 60 ) {
+                               ss = strtol( ptr, &next, 10 );
+                               if ( next == ptr || next[0] != '\0' || ss < 0 || ss > 60 ) {
                                        snprintf( c->cr_msg, sizeof( c->cr_msg ),
                                                "Error: parse_syncrepl_line: "
                                                "invalid interval \"%s\", unable to parse seconds", val );
@@ -3898,6 +3940,7 @@ syncrepl_unparse( syncinfo_t *si, struct berval *bv )
        si->si_bindconf.sb_version = LDAP_VERSION3;
 
        ptr = buf;
+       assert( si->si_rid >= 0 && si->si_rid <= SLAP_SYNC_SID_MAX );
        ptr += snprintf( ptr, WHATSLEFT, IDSTR "=%03d " PROVIDERSTR "=%s",
                si->si_rid, si->si_bindconf.sb_uri.bv_val );
        if ( ptr - buf >= sizeof( buf ) ) return;