]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/syncrepl.c
Fix prev commit
[openldap] / servers / slapd / syncrepl.c
index ffe7323455228f877ac6794819a00a270f990f11..118cc059a2fa7e17bb7000f634fe1d4eca41995d 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2003-2007 The OpenLDAP Foundation.
+ * Copyright 2003-2008 The OpenLDAP Foundation.
  * Portions Copyright 2003 by IBM Corporation.
  * Portions Copyright 2003 by Howard Chu, Symas Corporation.
  * All rights reserved.
@@ -52,7 +52,7 @@ struct nonpresent_entry {
 typedef struct syncinfo_s {
        struct slap_backend_db *si_be;
        struct re_s                     *si_re;
-       long                            si_rid;
+       int                                     si_rid;
        slap_bindconf           si_bindconf;
        struct berval           si_base;
        struct berval           si_logbase;
@@ -80,9 +80,11 @@ typedef struct syncinfo_s {
        int                                     si_tlimit;
        int                                     si_refreshDelete;
        int                                     si_refreshPresent;
+       int                                     si_refreshDone;
        int                                     si_syncdata;
        int                                     si_logstate;
        int                                     si_conn_setup;
+       ber_int_t                               si_msgid;
        Avlnode                         *si_presentlist;
        LDAP                            *si_ld;
        LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
@@ -314,7 +316,6 @@ ldap_sync_search(
        BerElement *ber = (BerElement *)&berbuf;
        LDAPControl c[2], *ctrls[3];
        struct timeval timeout;
-       ber_int_t       msgid;
        int rc;
        int rhint;
        char *base;
@@ -402,7 +403,7 @@ ldap_sync_search(
 
        rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
                ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
-               si->si_slimit, &msgid );
+               si->si_slimit, &si->si_msgid );
        ber_free_buf( ber );
        return rc;
 }
@@ -584,6 +585,8 @@ do_syncrep1(
                        &si->si_syncCookie.ctxcsn, si->si_syncCookie.rid );
        }
 
+       si->si_refreshDone = 0;
+
        rc = ldap_sync_search( si, op->o_tmpmemctx );
 
        if( rc != LDAP_SUCCESS ) {
@@ -631,8 +634,6 @@ do_syncrep2(
        int     rc, err, i;
        ber_len_t       len;
 
-       int rc_efree = 1;
-
        struct berval   *psub;
        Modifications   *modlist = NULL;
 
@@ -643,7 +644,6 @@ do_syncrep2(
        struct timeval tout = { 0, 0 };
 
        int             refreshDeletes = 0;
-       int             refreshDone = 1;
        BerVarray syncUUIDs = NULL;
        ber_tag_t si_tag;
 
@@ -667,7 +667,7 @@ do_syncrep2(
                tout_p = NULL;
        }
 
-       while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
+       while (( rc = ldap_result( si->si_ld, si->si_msgid, LDAP_MSG_ONE,
                tout_p, &res )) > 0 )
        {
                if ( slapd_shutdown ) {
@@ -716,19 +716,30 @@ do_syncrep2(
                                                slap_parse_sync_cookie( &syncCookie, NULL );
                                        }
                                }
+                               rc = 0;
                                if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
-                                       entry = NULL;
                                        modlist = NULL;
-                                       if ( syncrepl_message_to_op( si, op, msg ) == LDAP_SUCCESS &&
+                                       if (( rc = syncrepl_message_to_op( si, op, msg )) == LDAP_SUCCESS &&
                                                !BER_BVISNULL( &syncCookie.ctxcsn ) ) {
                                                syncrepl_updateCookie( si, op, psub, &syncCookie );
+                                       } else switch ( rc ) {
+                                               case LDAP_ALREADY_EXISTS:
+                                               case LDAP_NO_SUCH_OBJECT:
+                                               case LDAP_NO_SUCH_ATTRIBUTE:
+                                               case LDAP_TYPE_OR_VALUE_EXISTS:
+                                                       rc = LDAP_SYNC_REFRESH_REQUIRED;
+                                                       si->si_logstate = SYNCLOG_FALLBACK;
+                                                       ldap_abandon_ext( si->si_ld, si->si_msgid, NULL, NULL );
+                                                       break;
+                                               default:
+                                                       break;
                                        }
-                               } else if ( syncrepl_message_to_entry( si, op, msg,
-                                       &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
-                                       rc_efree = syncrepl_entry( si, op, entry, &modlist,
-                                               syncstate, &syncUUID, &syncCookie_req, &syncCookie.ctxcsn );
-                                       if ( !BER_BVISNULL( &syncCookie.ctxcsn ) )
-                                       {
+                               } else if (( rc = syncrepl_message_to_entry( si, op, msg,
+                                       &modlist, &entry, syncstate )) == LDAP_SUCCESS ) {
+                                       if (( rc = syncrepl_entry( si, op, entry, &modlist,
+                                               syncstate, &syncUUID, &syncCookie_req,
+                                               &syncCookie.ctxcsn )) == LDAP_SUCCESS &&
+                                               !BER_BVISNULL( &syncCookie.ctxcsn ) ) {
                                                syncrepl_updateCookie( si, op, psub, &syncCookie );
                                        }
                                }
@@ -736,10 +747,8 @@ do_syncrep2(
                                if ( modlist ) {
                                        slap_mods_free( modlist, 1 );
                                }
-                               if ( rc_efree && entry ) {
-                                       entry_free( entry );
-                               }
-                               entry = NULL;
+                               if ( rc )
+                                       goto done;
                                break;
 
                        case LDAP_RES_SEARCH_REFERENCE:
@@ -874,10 +883,14 @@ do_syncrep2(
                                                                slap_parse_sync_cookie( &syncCookie, NULL );
                                                        }
                                                }
+                                               /* Defaults to TRUE */
                                                if ( ber_peek_tag( ber, &len ) ==
                                                        LDAP_TAG_REFRESHDONE )
                                                {
-                                                       ber_scanf( ber, "b", &refreshDone );
+                                                       ber_scanf( ber, "b", &si->si_refreshDone );
+                                               } else
+                                               {
+                                                       si->si_refreshDone = 1;
                                                }
                                                ber_scanf( ber, /*"{"*/ "}" );
                                                break;
@@ -1029,7 +1042,7 @@ do_syncrepl(
        int rc = LDAP_SUCCESS;
        int dostop = 0;
        ber_socket_t s;
-       int i, defer = 1;
+       int i, defer = 1, fail = 0;
        Backend *be;
 
        Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl rid %03d\n", si->si_rid, 0, 0 );
@@ -1148,18 +1161,36 @@ reload:
 
                if ( !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
                        ldap_pvt_runqueue_remove( &slapd_rq, rtask );
+                       fail = RETRYNUM_TAIL;
                } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
                        if ( si->si_retrynum[i] > 0 )
                                si->si_retrynum[i]--;
+                       fail = si->si_retrynum[i];
                        rtask->interval.tv_sec = si->si_retryinterval[i];
                        ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
                        slap_wake_listener();
                }
        }
-       
+
        ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
        ldap_pvt_thread_mutex_unlock( &si->si_mutex );
 
+       if ( rc ) {
+               if ( fail == RETRYNUM_TAIL ) {
+                       Debug( LDAP_DEBUG_ANY,
+                               "do_syncrepl: rid %03d quitting\n",
+                               si->si_rid, 0, 0 );
+               } else if ( fail > 0 ) {
+                       Debug( LDAP_DEBUG_ANY,
+                               "do_syncrepl: rid %03d retrying (%d retries left)\n",
+                               si->si_rid, fail, 0 );
+               } else {
+                       Debug( LDAP_DEBUG_ANY,
+                               "do_syncrepl: rid %03d retrying\n",
+                               si->si_rid, 0, 0 );
+               }
+       }
+
        return NULL;
 }
 
@@ -1171,10 +1202,11 @@ static slap_verbmasks modops[] = {
        { BER_BVNULL, 0 }
 };
 
-static Modifications *
+static int
 syncrepl_accesslog_mods(
        syncinfo_t *si,
-       struct berval *vals
+       struct berval *vals,
+       Modifications **modres
 )
 {
        char *colon;
@@ -1183,7 +1215,7 @@ syncrepl_accesslog_mods(
        struct berval bv, bv2;
        short op;
        Modifications *mod = NULL, *modlist = NULL, **modtail;
-       int i;
+       int i, rc = 0;
 
        modtail = &modlist;
 
@@ -1197,7 +1229,13 @@ syncrepl_accesslog_mods(
                bv.bv_len = colon - bv.bv_val;
                if ( slap_bv2ad( &bv, &ad, &text )) {
                        /* Invalid */
-                       continue;
+                       Debug( LDAP_DEBUG_ANY, "syncrepl_accesslog_mods: rid=%03d "
+                               "Invalid attribute %s, %s\n",
+                               si->si_rid, bv.bv_val, text );
+                       slap_mods_free( modlist, 1 );
+                       modlist = NULL;
+                       rc = -1;
+                       break;
                }
                /* Ignore dynamically generated attrs */
                if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC )
@@ -1235,16 +1273,18 @@ syncrepl_accesslog_mods(
                        ber_bvarray_add( &mod->sml_values, &bv2 );
                }
        }
-       return modlist;
+       *modres = modlist;
+       return rc;
 }
 
-static Modifications *
+static int
 syncrepl_changelog_mods(
        syncinfo_t *si,
-       struct berval *vals
+       struct berval *vals,
+       Modifications **modres
 )
 {
-       return NULL;    /* FIXME */
+       return -1;      /* FIXME */
 }
 
 static int
@@ -1321,9 +1361,10 @@ syncrepl_message_to_op(
                } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod )) {
                        /* Parse attribute into modlist */
                        if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
-                               modlist = syncrepl_accesslog_mods( si, bvals );
+                               rc = syncrepl_accesslog_mods( si, bvals, &modlist );
                        else
-                               modlist = syncrepl_changelog_mods( si, bvals );
+                               rc = syncrepl_changelog_mods( si, bvals, &modlist );
+                       if ( rc ) goto done;
                } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn )) {
                        rdn = bvals[0];
                } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn )) {
@@ -1382,6 +1423,7 @@ syncrepl_message_to_op(
                } else {
                        op->orm_modlist = modlist;
                        rc = op->o_bd->be_modify( op, &rs );
+                       modlist = op->orm_modlist;
                        Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
                                "syncrepl_message_to_op: rid %03d be_modify %s (%d)\n", 
                                si->si_rid, op->o_req_dn.bv_val, rc );
@@ -1503,7 +1545,6 @@ syncrepl_message_to_entry(
        }
 
        e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
-       *entry = e;
        e->e_name = op->o_req_dn;
        e->e_nname = op->o_req_ndn;
 
@@ -1579,9 +1620,10 @@ done:
        if ( rc != LDAP_SUCCESS ) {
                if ( e ) {
                        entry_free( e );
-                       *entry = e = NULL;
+                       e = NULL;
                }
        }
+       *entry = e;
 
        return rc;
 }
@@ -1644,7 +1686,6 @@ syncrepl_entry(
        AttributeAssertion ava = { NULL, BER_BVNULL };
 #endif
        int rc = LDAP_SUCCESS;
-       int ret = LDAP_SUCCESS;
 
        struct berval pdn = BER_BVNULL;
        dninfo dni = {0};
@@ -1678,7 +1719,7 @@ syncrepl_entry(
        }
 
        if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
-               if ( !si->si_refreshPresent ) {
+               if ( !si->si_refreshPresent && !si->si_refreshDone ) {
                        syncuuid_bv = ber_dupbv( NULL, syncUUID );
                        avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
                                syncuuid_cmp, avl_dup_error );
@@ -1813,15 +1854,15 @@ retry_add:;
                        switch ( rs_add.sr_err ) {
                        case LDAP_SUCCESS:
                                be_entry_release_w( op, entry );
-                               ret = 0;
+                               entry = NULL;
                                break;
 
                        case LDAP_REFERRAL:
                        /* we assume that LDAP_NO_SUCH_OBJECT is returned 
                         * only if the suffix entry is not present */
                        case LDAP_NO_SUCH_OBJECT:
-                               syncrepl_add_glue( op, entry );
-                               ret = 0;
+                               rc = syncrepl_add_glue( op, entry );
+                               entry = NULL;
                                break;
 
                        /* if an entry was added via syncrepl_add_glue(),
@@ -1857,7 +1898,8 @@ retry_add:;
                                        cb2.sc_response = dn_callback;
                                        cb2.sc_private = &dni;
 
-                                       be->be_search( &op2, &rs2 );
+                                       rc = be->be_search( &op2, &rs2 );
+                                       if ( rc ) goto done;
 
                                        retry = 0;
                                        slap_op_time( &op->o_time, &op->o_tincr );
@@ -1869,7 +1911,6 @@ retry_add:;
                                Debug( LDAP_DEBUG_ANY,
                                        "syncrepl_entry: rid %03d be_add failed (%d)\n",
                                        si->si_rid, rs_add.sr_err, 0 );
-                               ret = 1;
                                break;
                        }
                        goto done;
@@ -1903,7 +1944,6 @@ retry_add:;
                                op->o_req_dn = entry->e_name;
                                op->o_req_ndn = entry->e_nname;
                        } else {
-                               ret = 1;
                                goto done;
                        }
                        if ( dni.wasChanged )
@@ -1971,7 +2011,6 @@ retry_add:;
                                        si->si_rid, rs_modify.sr_err, 0 );
                        }
                }
-               ret = 1;
                goto done;
        case LDAP_SYNC_DELETE :
                if ( !BER_BVISNULL( &dni.dn )) {
@@ -1999,13 +2038,11 @@ retry_add:;
                                }
                        }
                }
-               ret = 0;
                goto done;
 
        default :
                Debug( LDAP_DEBUG_ANY,
                        "syncrepl_entry: rid %03d unknown syncstate\n", si->si_rid, 0, 0 );
-               ret = 1;
                goto done;
        }
 
@@ -2023,8 +2060,10 @@ done :
        if ( !BER_BVISNULL( &dni.dn ) ) {
                op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
        }
+       if ( entry )
+               entry_free( entry );
        BER_BVZERO( &op->o_csn );
-       return ret;
+       return rc;
 }
 
 static struct berval gcbva[] = {
@@ -2090,11 +2129,8 @@ syncrepl_del_nonpresent(
 
                for (i=0; uuids[i].bv_val; i++) {
                        op->ors_slimit = 1;
-                       slap_uuidstr_from_normalized( &uf.f_av_value, &uuids[i],
-                               op->o_tmpmemctx );
-                       filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
-                       op->o_tmpfree( uf.f_av_value.bv_val, op->o_tmpmemctx );
                        uf.f_av_value = uuids[i];
+                       filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
                        rc = be->be_search( op, &rs_search );
                        op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
                }
@@ -2200,7 +2236,7 @@ syncrepl_del_nonpresent(
        return;
 }
 
-void
+int
 syncrepl_add_glue(
        Operation* op,
        Entry *e )
@@ -2310,6 +2346,10 @@ syncrepl_add_glue(
                } else {
                /* incl. ALREADY EXIST */
                        entry_free( glue );
+                       if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
+                               entry_free( e );
+                               return rc;
+                       }
                }
 
                /* Move to next child */
@@ -2340,7 +2380,7 @@ syncrepl_add_glue(
                entry_free( e );
        }
 
-       return;
+       return rc;
 }
 
 static void
@@ -2354,7 +2394,7 @@ syncrepl_updateCookie(
        Modifications mod = { { 0 } };
        struct berval vals[ 2 ];
 
-       int rc;
+       int rc, dbflags;
 
        slap_callback cb = { NULL };
        SlapReply       rs_modify = {REP_RESULT};
@@ -2386,7 +2426,10 @@ syncrepl_updateCookie(
        /* update contextCSN */
        op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
        op->orm_modlist = &mod;
+       dbflags = SLAP_DBFLAGS(op->o_bd);
+       SLAP_DBFLAGS(op->o_bd) |= SLAP_DBFLAG_NOLASTMOD;
        rc = be->be_modify( op, &rs_modify );
+       SLAP_DBFLAGS(op->o_bd) = dbflags;
        op->o_msgid = 0;
 
        if ( rs_modify.sr_err != LDAP_SUCCESS ) {
@@ -2873,6 +2916,14 @@ parse_syncrepl_line(
                                Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
                                return -1;
                        }
+                       if ( select_backend( &si->si_base, 0, 0 ) != c->be ) {
+                               ber_memfree( si->si_base.bv_val );
+                               snprintf( c->msg, sizeof( c->msg ),
+                                       "Base DN \"%s\" is not within the database naming context",
+                                       val );
+                               Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
+                               return -1;
+                       }
                        gots |= GOT_BASE;
                } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
                                        STRLENOF( LOGBASESTR "=" ) ) )
@@ -3297,7 +3348,7 @@ syncrepl_unparse( syncinfo_t *si, struct berval *bv )
        si->si_bindconf.sb_uri = uri;
 
        ptr = buf;
-       ptr += snprintf( ptr, sizeof( buf ), IDSTR "=%03ld " PROVIDERSTR "=%s",
+       ptr += snprintf( ptr, sizeof( buf ), IDSTR "=%03d " PROVIDERSTR "=%s",
                si->si_rid, si->si_bindconf.sb_uri.bv_val );
        if ( !BER_BVISNULL( &bc )) {
                ptr = lutil_strcopy( ptr, bc.bv_val );