]> 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 af6de8893d89d25a0432a6441ae9ad73d1ebf042..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[ STRLENOF("rid=4095") + 1 ];
+       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 * );
@@ -562,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;
@@ -798,6 +796,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 );
@@ -865,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);
@@ -902,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;
                                        }
                                }
@@ -953,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 );
@@ -980,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 );
@@ -1004,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 );
                                                }
@@ -1279,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 );
                }
        }
 
@@ -1383,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;
@@ -1392,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;
@@ -1724,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;
@@ -1821,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,
@@ -1832,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};
@@ -1854,13 +1887,7 @@ syncrepl_entry(
 
        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 );
                }
        }
 
@@ -1897,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 );
        }
@@ -2337,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;
@@ -2345,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;
@@ -2472,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] );
@@ -2484,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] );
@@ -2562,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;
@@ -2569,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 );
@@ -2584,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];
                        }
@@ -2595,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];
                }
@@ -2727,6 +2763,7 @@ attr_cmp( Operation *op, Attribute *old, Attribute *new,
                        mod->sml_flags = 0;
                        mod->sml_desc = old->a_desc;
                        mod->sml_type = mod->sml_desc->ad_cname;
+                       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( ( no + 1 ) * sizeof(struct berval) );
@@ -2758,6 +2795,7 @@ attr_cmp( Operation *op, Attribute *old, Attribute *new,
                        mod->sml_flags = 0;
                        mod->sml_desc = old->a_desc;
                        mod->sml_type = mod->sml_desc->ad_cname;
+                       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( ( nn + 1 ) * sizeof(struct berval) );
@@ -2846,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;
                                        }
@@ -2937,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;
@@ -2984,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 ) {
@@ -2998,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;
                }
@@ -3018,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 );
                }
        }
@@ -3157,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 )
 {
@@ -3260,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;
@@ -3370,7 +3396,7 @@ parse_syncrepl_line(
                                return -1;
                        }
                        si->si_rid = tmp;
-                       sprintf( si->si_ridtxt, IDSTR "=%d", si->si_rid );
+                       sprintf( si->si_ridtxt, IDSTR "=%03d", si->si_rid );
                        gots |= GOT_ID;
                } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
                                        STRLENOF( PROVIDERSTR "=" ) ) )
@@ -3557,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 );
@@ -3570,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 );
@@ -3579,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 );
@@ -3588,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 );
@@ -3917,7 +3941,7 @@ syncrepl_unparse( syncinfo_t *si, struct berval *bv )
 
        ptr = buf;
        assert( si->si_rid >= 0 && si->si_rid <= SLAP_SYNC_SID_MAX );
-       ptr += snprintf( ptr, WHATSLEFT, IDSTR "=%d " PROVIDERSTR "=%s",
+       ptr += snprintf( ptr, WHATSLEFT, IDSTR "=%03d " PROVIDERSTR "=%s",
                si->si_rid, si->si_bindconf.sb_uri.bv_val );
        if ( ptr - buf >= sizeof( buf ) ) return;
        if ( !BER_BVISNULL( &bc ) ) {