]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/overlays/syncprov.c
fix length
[openldap] / servers / slapd / overlays / syncprov.c
index 1b42b7953720fc6a293bde61af72b2456d2499f7..835ed240d7de0a8912d49e2a2cf878ebf1fbf3a9 100644 (file)
@@ -393,9 +393,6 @@ static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
 static int
 syncprov_findbase( Operation *op, fbase_cookie *fc )
 {
-       opcookie *opc = op->o_callback->sc_private;
-       slap_overinst *on = opc->son;
-
        /* Use basic parameters from syncrepl search, but use
         * current op's threadctx / tmpmemctx
         */
@@ -404,7 +401,6 @@ syncprov_findbase( Operation *op, fbase_cookie *fc )
                slap_callback cb = {0};
                Operation fop;
                SlapReply frs = { REP_RESULT };
-               BackendInfo *bi;
                int rc;
 
                fc->fss->s_flags ^= PS_FIND_BASE;
@@ -412,11 +408,10 @@ syncprov_findbase( Operation *op, fbase_cookie *fc )
 
                fop = *fc->fss->s_op;
 
+               fop.o_bd = fop.o_bd->bd_self;
                fop.o_hdr = op->o_hdr;
-               fop.o_bd = op->o_bd;
                fop.o_time = op->o_time;
                fop.o_tincr = op->o_tincr;
-               bi = op->o_bd->bd_info;
 
                cb.sc_response = findbase_cb;
                cb.sc_private = fc;
@@ -434,8 +429,7 @@ syncprov_findbase( Operation *op, fbase_cookie *fc )
                fop.ors_filter = &generic_filter;
                fop.ors_filterstr = generic_filterstr;
 
-               rc = overlay_op_walk( &fop, &frs, op_search, on->on_info, on );
-               op->o_bd->bd_info = bi;
+               rc = fop.o_bd->be_search( &fop, &frs );
        } else {
                ldap_pvt_thread_mutex_unlock( &fc->fss->s_mutex );
                fc->fbase = 1;
@@ -500,7 +494,8 @@ findmax_cb( Operation *op, SlapReply *rs )
                Attribute *a = attr_find( rs->sr_entry->e_attrs,
                        slap_schema.si_ad_entryCSN );
 
-               if ( a && ber_bvcmp( &a->a_vals[0], maxcsn ) > 0 ) {
+               if ( a && ber_bvcmp( &a->a_vals[0], maxcsn ) > 0 &&
+                       slap_parse_csn_sid( &a->a_vals[0] ) == slap_serverID ) {
                        maxcsn->bv_len = a->a_vals[0].bv_len;
                        strcpy( maxcsn->bv_val, a->a_vals[0].bv_val );
                }
@@ -590,7 +585,7 @@ syncprov_findcsn( Operation *op, find_csn_t mode )
        sync_control *srs = NULL;
        struct slap_limits_set fc_limits;
        int i, rc = LDAP_SUCCESS, findcsn_retry = 1;
-       int maxid = 0;
+       int maxid;
 
        if ( mode != FIND_MAXCSN ) {
                srs = op->o_controls[slap_cids.sc_LDAPsync];
@@ -616,17 +611,23 @@ again:
        switch( mode ) {
        case FIND_MAXCSN:
                cf.f_choice = LDAP_FILTER_GE;
-               cf.f_av_value = si->si_ctxcsn[0];
-               /* If there are multiple CSNs, use the largest */
-               for ( i=1; i<si->si_numcsns; i++) {
-                       if ( ber_bvcmp( &cf.f_av_value, &si->si_ctxcsn[i] ) < 0 ) {
-                               cf.f_av_value = si->si_ctxcsn[i];
+               /* If there are multiple CSNs, use the one with our serverID */
+               for ( i=0; i<si->si_numcsns; i++) {
+                       if ( slap_serverID == si->si_sids[i] ) {
                                maxid = i;
+                               break;
                        }
                }
+               if ( i == si->si_numcsns ) {
+                       /* No match: this is multimaster, and none of the content in the DB
+                        * originated locally. Treat like no CSN.
+                        */
+                       return LDAP_NO_SUCH_OBJECT;
+               }
+               cf.f_av_value = si->si_ctxcsn[maxid];
                fop.ors_filterstr.bv_len = snprintf( buf, sizeof( buf ),
                        "(entryCSN>=%s)", cf.f_av_value.bv_val );
-               if ( fop.ors_filterstr.bv_len < 0 || fop.ors_filterstr.bv_len >= sizeof( buf ) ) {
+               if ( fop.ors_filterstr.bv_len >= sizeof( buf ) ) {
                        return LDAP_OTHER;
                }
                fop.ors_attrsonly = 0;
@@ -663,7 +664,7 @@ again:
                        fop.ors_filterstr.bv_len = snprintf( buf, sizeof( buf ),
                                "(entryCSN<=%s)", cf.f_av_value.bv_val );
                }
-               if ( fop.ors_filterstr.bv_len < 0 || fop.ors_filterstr.bv_len >= sizeof( buf ) ) {
+               if ( fop.ors_filterstr.bv_len >= sizeof( buf ) ) {
                        return LDAP_OTHER;
                }
                fop.ors_attrsonly = 1;
@@ -696,7 +697,7 @@ again:
                break;
        }
 
-       fop.o_bd->bd_info = on->on_info->oi_orig;
+       fop.o_bd->bd_info = (BackendInfo *)on->on_info;
        fop.o_bd->be_search( &fop, &frs );
        fop.o_bd->bd_info = (BackendInfo *)on;
 
@@ -737,6 +738,13 @@ syncprov_free_syncop( syncops *so )
                ldap_pvt_thread_mutex_unlock( &so->s_mutex );
                return;
        }
+       if ( so->s_qtask ) {
+               ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
+               if ( ldap_pvt_runqueue_isrunning( &slapd_rq, so->s_qtask ) )
+                       ldap_pvt_runqueue_stoptask( &slapd_rq, so->s_qtask );
+               ldap_pvt_runqueue_remove( &slapd_rq, so->s_qtask );
+               ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
+       }
        ldap_pvt_thread_mutex_unlock( &so->s_mutex );
        if ( so->s_flags & PS_IS_DETACHED ) {
                filter_free( so->s_op->ors_filter );
@@ -840,7 +848,7 @@ static int
 syncprov_qplay( Operation *op, struct re_s *rtask )
 {
        syncops *so = rtask->arg;
-       slap_overinst *on = so->s_op->o_private;
+       slap_overinst *on = LDAP_SLIST_FIRST(&so->s_op->o_extra)->oe_key;
        syncres *sr;
        Entry *e;
        opcookie opc;
@@ -900,6 +908,10 @@ syncprov_qplay( Operation *op, struct re_s *rtask )
        } else {
                /* bail out on any error */
                ldap_pvt_runqueue_remove( &slapd_rq, rtask );
+
+               /* Prevent duplicate remove */
+               if ( so->s_qtask == rtask )
+                       so->s_qtask = NULL;
        }
        ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
        ldap_pvt_thread_mutex_unlock( &so->s_mutex );
@@ -933,7 +945,7 @@ syncprov_qtask( void *ctx, void *arg )
        be = *so->s_op->o_bd;
        be.be_flags |= SLAP_DBFLAG_OVERLAY;
        op->o_bd = &be;
-       op->o_private = NULL;
+       LDAP_SLIST_FIRST(&op->o_extra) = NULL;
        op->o_callback = NULL;
 
        rc = syncprov_qplay( op, rtask );
@@ -1169,10 +1181,15 @@ syncprov_matchops( Operation *op, opcookie *opc, int saveit )
        for (ss = si->si_ops, sprev = (syncops *)&si->si_ops; ss;
                sprev = ss, ss=snext)
        {
+               Operation op2;
+               Opheader oh;
                syncmatches *sm;
                int found = 0;
 
                snext = ss->s_next;
+               if ( ss->s_op->o_abandon )
+                       continue;
+
                /* validate base */
                fc.fss = ss;
                fc.fbase = 0;
@@ -1213,8 +1230,17 @@ syncprov_matchops( Operation *op, opcookie *opc, int saveit )
                        }
                }
 
+               if ( fc.fscope ) {
+                       op2 = *ss->s_op;
+                       oh = *op->o_hdr;
+                       oh.oh_conn = ss->s_op->o_conn;
+                       oh.oh_connid = ss->s_op->o_connid;
+                       op2.o_hdr = &oh;
+                       op2.o_extra = op->o_extra;
+               }
+
                /* check if current o_req_dn is in scope and matches filter */
-               if ( fc.fscope && test_filter( ss->s_op, e, ss->s_op->ors_filter ) ==
+               if ( fc.fscope && test_filter( &op2, e, ss->s_op->ors_filter ) ==
                        LDAP_COMPARE_TRUE ) {
                        if ( saveit ) {
                                sm = op->o_tmpalloc( sizeof(syncmatches), op->o_tmpmemctx );
@@ -1306,13 +1332,14 @@ syncprov_checkpoint( Operation *op, SlapReply *rs, slap_overinst *on )
        Operation opm;
        SlapReply rsm = { 0 };
        slap_callback cb = {0};
+       BackendDB be;
 
        mod.sml_numvals = si->si_numcsns;
        mod.sml_values = si->si_ctxcsn;
        mod.sml_nvalues = NULL;
        mod.sml_desc = slap_schema.si_ad_contextCSN;
        mod.sml_op = LDAP_MOD_REPLACE;
-       mod.sml_flags = 0;
+       mod.sml_flags = SLAP_MOD_INTERNAL;
        mod.sml_next = NULL;
 
        cb.sc_response = slap_null_cb;
@@ -1321,8 +1348,12 @@ syncprov_checkpoint( Operation *op, SlapReply *rs, slap_overinst *on )
        opm.o_callback = &cb;
        opm.orm_modlist = &mod;
        opm.orm_no_opattrs = 1;
-       opm.o_req_dn = op->o_bd->be_suffix[0];
-       opm.o_req_ndn = op->o_bd->be_nsuffix[0];
+       if ( SLAP_GLUE_SUBORDINATE( op->o_bd )) {
+               be = *on->on_info->oi_origdb;
+               opm.o_bd = &be;
+       }
+       opm.o_req_dn = opm.o_bd->be_suffix[0];
+       opm.o_req_ndn = opm.o_bd->be_nsuffix[0];
        opm.o_bd->bd_info = on->on_info->oi_orig;
        opm.o_managedsait = SLAP_CONTROL_NONCRITICAL;
        opm.o_no_schema_check = 1;
@@ -1330,7 +1361,6 @@ syncprov_checkpoint( Operation *op, SlapReply *rs, slap_overinst *on )
        if ( mod.sml_next != NULL ) {
                slap_mods_free( mod.sml_next, 1 );
        }
-       opm.orm_no_opattrs = 0;
 }
 
 static void
@@ -1524,7 +1554,7 @@ syncprov_playlog( Operation *op, SlapReply *rs, sessionlog *sl,
                fop.ors_filter = &af;
 
                cb.sc_response = playlog_cb;
-               fop.o_bd->bd_info = on->on_info->oi_orig;
+               fop.o_bd->bd_info = (BackendInfo *)on->on_info;
 
                for ( i=ndel; i<num; i++ ) {
                        if ( uuids[i].bv_len == 0 ) continue;
@@ -1545,14 +1575,19 @@ syncprov_playlog( Operation *op, SlapReply *rs, sessionlog *sl,
        if ( ndel ) {
                struct berval cookie;
 
-               slap_compose_sync_cookie( op, &cookie, delcsn, srs->sr_state.rid,
-                       srs->sr_state.sid );
+               if ( delcsn[0].bv_len ) {
+                       slap_compose_sync_cookie( op, &cookie, delcsn, srs->sr_state.rid,
+                               srs->sr_state.sid );
 
-               Debug( LDAP_DEBUG_SYNC, "syncprov_playlog: cookie=%s\n", cookie.bv_val, 0, 0 );
+                       Debug( LDAP_DEBUG_SYNC, "syncprov_playlog: cookie=%s\n", cookie.bv_val, 0, 0 );
+               }
 
                uuids[ndel].bv_val = NULL;
-               syncprov_sendinfo( op, rs, LDAP_TAG_SYNC_ID_SET, &cookie, 0, uuids, 1 );
-               op->o_tmpfree( cookie.bv_val, op->o_tmpmemctx );
+               syncprov_sendinfo( op, rs, LDAP_TAG_SYNC_ID_SET,
+                       delcsn[0].bv_len ? &cookie : NULL, 0, uuids, 1 );
+               if ( delcsn[0].bv_len ) {
+                       op->o_tmpfree( cookie.bv_val, op->o_tmpmemctx );
+               }
        }
        op->o_tmpfree( uuids, op->o_tmpmemctx );
 }
@@ -1569,12 +1604,23 @@ syncprov_op_response( Operation *op, SlapReply *rs )
        {
                struct berval maxcsn = BER_BVNULL;
                char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
-               int do_check = 0, have_psearches;
+               int do_check = 0, have_psearches, foundit;
 
                /* Update our context CSN */
                cbuf[0] = '\0';
                ldap_pvt_thread_rdwr_wlock( &si->si_csn_rwlock );
-               slap_get_commit_csn( op, &maxcsn );
+               slap_get_commit_csn( op, &maxcsn, &foundit );
+               if ( BER_BVISNULL( &maxcsn ) && SLAP_GLUE_SUBORDINATE( op->o_bd )) {
+                       /* syncrepl queues the CSN values in the db where
+                        * it is configured , not where the changes are made.
+                        * So look for a value in the glue db if we didn't
+                        * find any in this db.
+                        */
+                       BackendDB *be = op->o_bd;
+                       op->o_bd = select_backend( &be->be_nsuffix[0], 1);
+                       slap_get_commit_csn( op, &maxcsn, &foundit );
+                       op->o_bd = be;
+               }
                if ( !BER_BVISNULL( &maxcsn ) ) {
                        int i, sid;
                        strcpy( cbuf, maxcsn.bv_val );
@@ -1595,11 +1641,14 @@ syncprov_op_response( Operation *op, SlapReply *rs )
                                        sizeof(int));
                                si->si_sids[i] = sid;
                        }
+               } else if ( !foundit ) {
+                       /* internal ops that aren't meant to be replicated */
+                       ldap_pvt_thread_rdwr_wunlock( &si->si_csn_rwlock );
+                       return SLAP_CB_CONTINUE;
                }
 
                /* Don't do any processing for consumer contextCSN updates */
-               if ( SLAP_SYNC_SHADOW( op->o_bd ) && 
-                       op->o_msgid == SLAP_SYNC_UPDATE_MSGID ) {
+               if ( op->o_dont_replicate ) {
                        ldap_pvt_thread_rdwr_wunlock( &si->si_csn_rwlock );
                        return SLAP_CB_CONTINUE;
                }
@@ -1628,8 +1677,11 @@ syncprov_op_response( Operation *op, SlapReply *rs )
                        ldap_pvt_thread_rdwr_runlock( &si->si_csn_rwlock );
                }
 
-               opc->sctxcsn.bv_len = maxcsn.bv_len;
-               opc->sctxcsn.bv_val = cbuf;
+               /* only update consumer ctx if this is the greatest csn */
+               if ( bvmatch( &maxcsn, &op->o_csn )) {
+                       opc->sctxcsn.bv_len = maxcsn.bv_len;
+                       opc->sctxcsn.bv_val = cbuf;
+               }
 
                /* Handle any persistent searches */
                ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
@@ -1854,6 +1906,7 @@ syncprov_search_cleanup( Operation *op, SlapReply *rs )
 typedef struct SyncOperationBuffer {
        Operation               sob_op;
        Opheader                sob_hdr;
+       OpExtra                 sob_oe;
        AttributeName   sob_extra;      /* not always present */
        /* Further data allocated here */
 } SyncOperationBuffer;
@@ -1882,6 +1935,7 @@ syncprov_detach_op( Operation *op, syncops *so, slap_overinst *on )
        sopbuf2 = ch_calloc( 1, size );
        op2 = &sopbuf2->sob_op;
        op2->o_hdr = &sopbuf2->sob_hdr;
+       LDAP_SLIST_FIRST(&op2->o_extra) = &sopbuf2->sob_oe;
 
        /* Copy the fields we care about explicitly, leave the rest alone */
        *op2->o_hdr = *op->o_hdr;
@@ -1889,7 +1943,8 @@ syncprov_detach_op( Operation *op, syncops *so, slap_overinst *on )
        op2->o_time = op->o_time;
        op2->o_bd = on->on_info->oi_origdb;
        op2->o_request = op->o_request;
-       op2->o_private = on;
+       LDAP_SLIST_FIRST(&op2->o_extra)->oe_key = on;
+       LDAP_SLIST_NEXT(LDAP_SLIST_FIRST(&op2->o_extra), oe_next) = NULL;
 
        ptr = (char *) sopbuf2 + offsetof( SyncOperationBuffer, sob_extra );
        if ( i ) {
@@ -1954,6 +2009,7 @@ syncprov_search_response( Operation *op, SlapReply *rs )
 {
        searchstate *ss = op->o_callback->sc_private;
        slap_overinst *on = ss->ss_on;
+       syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
        sync_control *srs = op->o_controls[slap_cids.sc_LDAPsync];
 
        if ( rs->sr_type == REP_SEARCH || rs->sr_type == REP_SEARCHREF ) {
@@ -2019,8 +2075,16 @@ syncprov_search_response( Operation *op, SlapReply *rs )
                rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
                        op->o_tmpmemctx );
                rs->sr_ctrls[1] = NULL;
-               rs->sr_err = syncprov_state_ctrl( op, rs, rs->sr_entry,
-                       LDAP_SYNC_ADD, rs->sr_ctrls, 0, 0, NULL );
+               /* If we're in delta-sync mode, always send a cookie */
+               if ( si->si_nopres && si->si_usehint && a ) {
+                       struct berval cookie;
+                       slap_compose_sync_cookie( op, &cookie, a->a_nvals, srs->sr_state.rid, srs->sr_state.sid );
+                       rs->sr_err = syncprov_state_ctrl( op, rs, rs->sr_entry,
+                               LDAP_SYNC_ADD, rs->sr_ctrls, 0, 1, &cookie );
+               } else {
+                       rs->sr_err = syncprov_state_ctrl( op, rs, rs->sr_entry,
+                               LDAP_SYNC_ADD, rs->sr_ctrls, 0, 0, NULL );
+               }
        } else if ( rs->sr_type == REP_RESULT && rs->sr_err == LDAP_SUCCESS ) {
                struct berval cookie;
 
@@ -2059,7 +2123,7 @@ syncprov_search_response( Operation *op, SlapReply *rs )
                        if ( op->o_abandon ) {
                                ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
                                ldap_pvt_thread_mutex_unlock( &ss->ss_so->s_mutex );
-                               syncprov_free_syncop( ss->ss_so );
+                               /* syncprov_ab_cleanup will free this syncop */
                                return SLAPD_ABANDON;
 
                        } else {
@@ -2233,6 +2297,9 @@ no_change:                if ( !(op->o_sync_mode & SLAP_SYNC_PERSIST) ) {
                                }
                                goto shortcut;
                        }
+               } else {
+                       /* consumer doesn't have the right number of CSNs */
+                       changed = SS_CHANGED;
                }
                /* Do we have a sessionlog for this search? */
                sl=si->si_logs;
@@ -2261,6 +2328,15 @@ no_change:               if ( !(op->o_sync_mode & SLAP_SYNC_PERSIST) ) {
                                send_ldap_error( op, rs, LDAP_SYNC_REFRESH_REQUIRED, "sync cookie is stale" );
                                return rs->sr_err;
                        }
+                       if ( srs->sr_state.ctxcsn ) {
+                               ber_bvarray_free_x( srs->sr_state.ctxcsn, op->o_tmpmemctx );
+                               srs->sr_state.ctxcsn = NULL;
+                       }
+                       if ( srs->sr_state.sids ) {
+                               slap_sl_free( srs->sr_state.sids, op->o_tmpmemctx );
+                               srs->sr_state.sids = NULL;
+                       }
+                       srs->sr_state.numcsns = 0;
                } else {
                        gotstate = 1;
                        /* If changed and doing Present lookup, send Present UUIDs */
@@ -2375,8 +2451,15 @@ syncprov_operational(
                                }
 
                                if ( !ap ) {
-                                       if ( !rs->sr_flags & REP_ENTRY_MODIFIABLE ) {
-                                               rs->sr_entry = entry_dup( rs->sr_entry );
+                                       if ( !(rs->sr_flags & REP_ENTRY_MODIFIABLE) ) {
+                                               Entry *e = entry_dup( rs->sr_entry );
+                                               if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
+                                                       overlay_entry_release_ov( op, rs->sr_entry, 0, on );
+                                                       rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
+                                               } else if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
+                                                       entry_free( rs->sr_entry );
+                                               }
+                                               rs->sr_entry = e;
                                                rs->sr_flags |=
                                                        REP_ENTRY_MODIFIABLE|REP_ENTRY_MUSTBEFREED;
                                                a = attr_find( rs->sr_entry->e_attrs,
@@ -2432,7 +2515,11 @@ static ConfigOCs spocs[] = {
                "NAME 'olcSyncProvConfig' "
                "DESC 'SyncRepl Provider configuration' "
                "SUP olcOverlayConfig "
-               "MAY ( olcSpCheckpoint $ olcSpSessionlog $ olcSpNoPresent ) )",
+               "MAY ( olcSpCheckpoint "
+                       "$ olcSpSessionlog "
+                       "$ olcSpNoPresent "
+                       "$ olcSpReloadHint "
+               ") )",
                        Cft_Overlay, spcfg },
        { NULL, 0, NULL }
 };
@@ -2451,7 +2538,7 @@ sp_cf_gen(ConfigArgs *c)
                                struct berval bv;
                                bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ),
                                        "%d %d", si->si_chkops, si->si_chktime );
-                               if ( bv.bv_len < 0 || bv.bv_len >= sizeof( c->cr_msg ) ) {
+                               if ( bv.bv_len >= sizeof( c->cr_msg ) ) {
                                        rc = 1;
                                } else {
                                        bv.bv_val = c->cr_msg;
@@ -2645,7 +2732,7 @@ syncprov_db_open(
                        si->si_sids = slap_parse_csn_sids( si->si_ctxcsn, a->a_numvals, NULL );
                }
                overlay_entry_release_ov( op, e, 0, on );
-               if ( si->si_ctxcsn ) {
+               if ( si->si_ctxcsn && !SLAP_DBCLEAN( be )) {
                        op->o_req_dn = be->be_suffix[0];
                        op->o_req_ndn = be->be_nsuffix[0];
                        op->ors_scope = LDAP_SCOPE_SUBTREE;