]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/syncrepl.c
Fix ITS#3424
[openldap] / servers / slapd / syncrepl.c
index 756eb62cc3e3a7a4cbc99b77d62a590fda1d272a..fe4e41e47bd9ea917d411ee1f9c1583c35a4d9c5 100644 (file)
@@ -41,7 +41,7 @@ static const struct berval slap_syncrepl_cn_bvc = BER_BVC(CN_STR SYNCREPL_STR);
 
 static int syncuuid_cmp( const void *, const void * );
 static void avl_ber_bvfree( void * );
-static void syncrepl_del_nonpresent( Operation *, syncinfo_t * );
+static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray );
 
 /* callback functions */
 static int dn_callback( struct slap_op *, struct slap_rep * );
@@ -50,8 +50,6 @@ static int null_callback( struct slap_op *, struct slap_rep * );
 
 static AttributeDescription *sync_descs[4];
 
-struct runqueue_s syncrepl_rq;
-
 void
 init_syncrepl(syncinfo_t *si)
 {
@@ -691,7 +689,7 @@ do_syncrep2(
                                        if ( refreshDeletes == 0 && match < 0 &&
                                                err == LDAP_SUCCESS )
                                        {
-                                               syncrepl_del_nonpresent( op, si );
+                                               syncrepl_del_nonpresent( op, si, NULL );
                                        } else {
                                                avl_free( si->si_presentlist, avl_ber_bvfree );
                                                si->si_presentlist = NULL;
@@ -784,15 +782,20 @@ do_syncrep2(
                                                }
                                                ber_scanf( ber, "[W]", &syncUUIDs );
                                                ber_scanf( ber, /*"{"*/ "}" );
-                                               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 );
-                                                       avl_insert( &si->si_presentlist,
-                                                               (caddr_t) syncuuid_bv,
-                                                               syncuuid_cmp, avl_dup_error );
+                                               if ( refreshDeletes ) {
+                                                       syncrepl_del_nonpresent( op, si, syncUUIDs );
+                                                       ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
+                                               } else {
+                                                       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 );
+                                                               avl_insert( &si->si_presentlist,
+                                                                       (caddr_t) syncuuid_bv,
+                                                                       syncuuid_cmp, avl_dup_error );
+                                                       }
+                                                       slap_sl_free( syncUUIDs, op->o_tmpmemctx );
                                                }
-                                               slap_sl_free( syncUUIDs, op->o_tmpmemctx );
                                                break;
                                        default:
                                                Debug( LDAP_DEBUG_ANY,
@@ -823,7 +826,7 @@ do_syncrep2(
 
                                        if ( si->si_refreshPresent == 1 ) {
                                                if ( match < 0 ) {
-                                                       syncrepl_del_nonpresent( op, si );
+                                                       syncrepl_del_nonpresent( op, si, NULL );
                                                }
                                        } 
 
@@ -969,10 +972,10 @@ do_syncrepl(
         * 3) for Refresh and Success, reschedule to run
         * 4) for Persist and Success, reschedule to defer
         */
-       ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
+       ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
 
-       if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
-               ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
+       if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
+               ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
        }
 
        if ( dostop ) {
@@ -984,7 +987,7 @@ do_syncrepl(
                        defer = 0;
                }
                rtask->interval.tv_sec = si->si_interval;
-               ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, defer );
+               ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
                if ( si->si_retrynum ) {
                        for ( i = 0; si->si_retrynum_init[i] != -2; i++ ) {
                                si->si_retrynum[i] = si->si_retrynum_init[i];
@@ -998,19 +1001,19 @@ do_syncrepl(
                }
 
                if ( !si->si_retrynum || si->si_retrynum[i] == -2 ) {
-                       ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
+                       ldap_pvt_runqueue_remove( &slapd_rq, rtask );
                        LDAP_STAILQ_REMOVE( &be->be_syncinfo, si, syncinfo_s, si_next );
                        syncinfo_free( si );
                } else if ( si->si_retrynum[i] >= -1 ) {
                        if ( si->si_retrynum[i] > 0 )
                                si->si_retrynum[i]--;
                        rtask->interval.tv_sec = si->si_retryinterval[i];
-                       ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 0 );
+                       ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
                        slap_wake_listener();
                }
        }
        
-       ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
+       ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
 
        return NULL;
 }
@@ -1216,6 +1219,7 @@ syncrepl_entry(
        struct berval org_ndn = BER_BVNULL;
        int     org_managedsait;
        dninfo dni = {0};
+       int     retry = 1;
 
        switch( syncstate ) {
        case LDAP_SYNC_PRESENT:
@@ -1278,7 +1282,7 @@ syncrepl_entry(
        op->o_tag = LDAP_REQ_SEARCH;
        op->ors_scope = LDAP_SCOPE_SUBTREE;
 
-       /* get syncrepl cookie of shadow replica from subentry */
+       /* get the entry for this UUID */
        op->o_req_dn = si->si_base;
        op->o_req_ndn = si->si_base;
 
@@ -1286,8 +1290,8 @@ syncrepl_entry(
        op->ors_tlimit = SLAP_NO_LIMIT;
        op->ors_slimit = 1;
 
-       op->ors_attrs = slap_anlist_no_attrs;
-       op->ors_attrsonly = 1;
+       op->ors_attrs = slap_anlist_all_attributes;
+       op->ors_attrsonly = 0;
 
        /* set callback function */
        op->o_callback = &cb;
@@ -1337,7 +1341,9 @@ syncrepl_entry(
        switch ( syncstate ) {
        case LDAP_SYNC_ADD:
        case LDAP_SYNC_MODIFY:
+retry_add:;
                if ( BER_BVISNULL( &dni.dn )) {
+
                        op->o_req_dn = entry->e_name;
                        op->o_req_ndn = entry->e_nname;
                        op->o_tag = LDAP_REQ_ADD;
@@ -1361,6 +1367,60 @@ syncrepl_entry(
                                ret = 0;
                                break;
 
+                       /* if an entry was added via syncrepl_add_glue(),
+                        * it likely has no entryUUID, so the previous
+                        * be_search() doesn't find it.  In this case,
+                        * give syncrepl a chance to modify it. */
+                       case LDAP_ALREADY_EXISTS:
+                               if ( retry ) {
+                                       Operation       op2 = *op;
+                                       SlapReply       rs2 = { 0 };
+                                       slap_callback   cb2 = { 0 };
+
+                                       op2.o_tag = LDAP_REQ_SEARCH;
+                                       ber_dupbv_x( &op2.o_req_dn,
+                                               &entry->e_name,
+                                               op2.o_tmpmemctx );
+                                       ber_dupbv_x( &op2.o_req_ndn,
+                                               &entry->e_nname,
+                                               op2.o_tmpmemctx );
+                                       op2.ors_scope = LDAP_SCOPE_BASE;
+                                       op2.ors_attrs = slap_anlist_all_attributes;
+                                       op2.ors_attrsonly = 0;
+                                       op2.ors_limit = NULL;
+                                       op2.ors_slimit = 1;
+                                       op2.ors_tlimit = SLAP_NO_LIMIT;
+
+                                       f.f_choice = LDAP_FILTER_EQUALITY;
+                                       f.f_ava = &ava;
+                                       ava.aa_desc = slap_schema.si_ad_objectClass;
+                                       ber_dupbv_x( &ava.aa_value,
+                                               &slap_schema.si_oc_glue->soc_cname,
+                                               op2.o_tmpmemctx );
+                                       op2.ors_filter = &f;
+                                       filter2bv_x( &op2, op2.ors_filter,
+                                                       &op2.ors_filterstr );
+
+                                       op2.o_callback = &cb2;
+                                       cb2.sc_response = dn_callback;
+                                       cb2.sc_private = &dni;
+
+                                       be->be_search( &op2, &rs2 );
+
+                                       op2.o_tmpfree( op2.o_req_dn.bv_val,
+                                               op2.o_tmpmemctx );
+                                       op2.o_tmpfree( op2.o_req_ndn.bv_val,
+                                               op2.o_tmpmemctx );
+                                       op2.o_tmpfree( ava.aa_value.bv_val,
+                                               op2.o_tmpmemctx );
+                                       op2.o_tmpfree( op2.ors_filterstr.bv_val,
+                                               op2.o_tmpmemctx );
+
+                                       retry = 0;
+                                       goto retry_add;
+                               }
+                               /* FALLTHRU */
+
                        default:
                                Debug( LDAP_DEBUG_ANY,
                                        "syncrepl_entry : be_add failed (%d)\n",
@@ -1438,11 +1498,13 @@ syncrepl_entry(
                        }
 
                        mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
-                       ber_dupbv( &uuid_bv, syncUUID );
                        mod->sml_op = LDAP_MOD_REPLACE;
                        mod->sml_desc = slap_schema.si_ad_entryUUID;
                        mod->sml_type = mod->sml_desc->ad_cname;
+                       ber_dupbv( &uuid_bv, &syncUUID_strrep );
                        ber_bvarray_add( &mod->sml_values, &uuid_bv );
+                       ber_dupbv( &uuid_bv, syncUUID );
+                       ber_bvarray_add( &mod->sml_nvalues, &uuid_bv );
                        modtail->sml_next = mod;
                                        
                        op->o_tag = LDAP_REQ_MODIFY;
@@ -1519,10 +1581,13 @@ static struct berval gcbva[] = {
        BER_BVNULL
 };
 
+#define NP_DELETE_ONE  2
+
 static void
 syncrepl_del_nonpresent(
        Operation *op,
-       syncinfo_t *si )
+       syncinfo_t *si,
+       BerVarray uuids )
 {
        Backend* be = op->o_bd;
        slap_callback   cb = { NULL };
@@ -1557,29 +1622,52 @@ syncrepl_del_nonpresent(
        op->ors_deref = LDAP_DEREF_NEVER;
        op->o_time = slap_get_time();
        op->ors_tlimit = SLAP_NO_LIMIT;
-       op->ors_slimit = SLAP_NO_LIMIT;
 
-       memset( &an[0], 0, 2 * sizeof( AttributeName ) );
-       an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
-       an[0].an_desc = slap_schema.si_ad_entryUUID;
-       op->ors_attrs = an;
 
-       op->ors_attrsonly = 0;
-       op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
-       op->ors_filterstr = si->si_filterstr;
+       if ( uuids ) {
+               Filter uf;
+               AttributeAssertion eq;
+               int i;
 
-       op->o_nocaching = 1;
-       op->o_managedsait = SLAP_CONTROL_NONE;
+               op->ors_attrsonly = 1;
+               op->ors_attrs = slap_anlist_no_attrs;
+               op->ors_limit = NULL;
+               op->ors_filter = &uf;
+               op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
 
-       if ( limits_check( op, &rs_search ) == 0 ) {
-               rc = be->be_search( op, &rs_search );
+               uf.f_ava = &eq;
+               uf.f_av_desc = slap_schema.si_ad_entryUUID;
+               uf.f_next = NULL;
+               uf.f_choice = LDAP_FILTER_EQUALITY;
+               si->si_refreshDelete |= NP_DELETE_ONE;
+
+               for (i=0; uuids[i].bv_val; i++) {
+                       op->ors_slimit = 1;
+                       uf.f_av_value = uuids[i];
+                       rc = be->be_search( op, &rs_search );
+               }
+               si->si_refreshDelete ^= NP_DELETE_ONE;
+       } else {
+               memset( &an[0], 0, 2 * sizeof( AttributeName ) );
+               an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
+               an[0].an_desc = slap_schema.si_ad_entryUUID;
+               op->ors_attrs = an;
+               op->ors_slimit = SLAP_NO_LIMIT;
+               op->ors_attrsonly = 0;
+               op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
+               op->ors_filterstr = si->si_filterstr;
+               op->o_nocaching = 1;
+               op->o_managedsait = SLAP_CONTROL_NONE;
+
+               if ( limits_check( op, &rs_search ) == 0 ) {
+                       rc = be->be_search( op, &rs_search );
+               }
+               if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
        }
 
        op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
        op->o_nocaching = 0;
 
-       if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
-
        if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
                np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
                while ( np_list != NULL ) {
@@ -2052,23 +2140,40 @@ dn_callback(
                                Attribute *old, *new;
                                int i;
 
-                               /* Did the DN change? */
+                               /* Did the DN change? Note that we don't explicitly try to
+                                * discover if the deleteOldRdn argument applies here. It
+                                * would save an unnecessary Modify if we detected it, but
+                                * that's a fair amount of trouble to compare the two attr
+                                * lists in detail.
+                                */
                                if ( !dn_match( &rs->sr_entry->e_name,
-                                               &dni->new_entry->e_name )) {
+                                               &dni->new_entry->e_name ) )
+                               {
                                        dni->renamed = 1;
                                }
 
-                               i = 0;
-                               for ( old=rs->sr_entry->e_attrs; old; old=old->a_next ) i++;
+                               for ( i = 0, old = rs->sr_entry->e_attrs;
+                                               old;
+                                               i++, old = old->a_next )
+                                       ;
+
                                dni->attrs = i;
 
-                               for ( old=rs->sr_entry->e_attrs, new=dni->new_entry->e_attrs;
-                                       old && new; old=old->a_next, new=new->a_next ) {
+                               /* We assume that attributes are saved in the same order
+                                * in the remote and local databases. So if we walk through
+                                * the attributeDescriptions one by one they should match in
+                                * lock step. If not, we signal a change. Otherwise we test
+                                * all the values...
+                                */
+                               for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
+                                               old && new;
+                                               old = old->a_next, new = new->a_next )
+                               {
                                        if ( old->a_desc != new->a_desc ) {
                                                dni->wasChanged = 1;
                                                break;
                                        }
-                                       for (i=0; ; i++) {
+                                       for ( i = 0; ; i++ ) {
                                                int nold, nnew;
                                                nold = BER_BVISNULL( &old->a_vals[i] );
                                                nnew = BER_BVISNULL( &new->a_vals[i] );
@@ -2092,7 +2197,7 @@ dn_callback(
                                        dni->ads = op->o_tmpalloc( dni->attrs *
                                                sizeof(AttributeDescription *), op->o_tmpmemctx );
                                        i = 0;
-                                       for ( old=rs->sr_entry->e_attrs; old; old=old->a_next ) {
+                                       for ( old = rs->sr_entry->e_attrs; old; old = old->a_next ) {
                                                dni->ads[i] = old->a_desc;
                                                i++;
                                        }
@@ -2126,12 +2231,14 @@ nonpresent_callback(
                si->si_presentlist = NULL;
 
        } else if ( rs->sr_type == REP_SEARCH ) {
-               a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
+               if ( !(si->si_refreshDelete & NP_DELETE_ONE )) {
+                       a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
 
-               if ( a == NULL ) return 0;
+                       if ( a == NULL ) return 0;
 
-               present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
-                       syncuuid_cmp );
+                       present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
+                               syncuuid_cmp );
+               }
 
                if ( present_uuid == NULL ) {
                        np_entry = (struct nonpresent_entry *)