]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/overlays/syncprov.c
ITS#3922 don't leak ber buffer on decode/parse errors
[openldap] / servers / slapd / overlays / syncprov.c
index b736ed8982713e939bd73d0369f5cc4cef490c00..bc8d940615cfcb35733da02a916231e028962615 100644 (file)
@@ -25,6 +25,7 @@
 #include <ac/string.h>
 #include "lutil.h"
 #include "slap.h"
+#include "config.h"
 
 /* A modify request on a particular entry */
 typedef struct modinst {
@@ -158,8 +159,6 @@ syncprov_state_ctrl(
 {
        Attribute* a;
        int ret;
-       int res;
-       const char *text = NULL;
 
        BerElementBuffer berbuf;
        BerElement *ber = (BerElement *)&berbuf;
@@ -401,7 +400,6 @@ syncprov_findbase( Operation *op, fbase_cookie *fc )
 {
        opcookie *opc = op->o_callback->sc_private;
        slap_overinst *on = opc->son;
-       syncprov_info_t         *si = on->on_bi.bi_private;
 
        slap_callback cb = {0};
        Operation fop;
@@ -547,7 +545,7 @@ syncprov_findcsn( Operation *op, int mode )
        SlapReply frs = { REP_RESULT };
        char buf[LDAP_LUTIL_CSNSTR_BUFSIZE + STRLENOF("(entryCSN<=)")];
        char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
-       struct berval fbuf, maxcsn;
+       struct berval maxcsn;
        Filter cf, af;
 #ifdef LDAP_COMP_MATCH
        AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
@@ -571,7 +569,6 @@ syncprov_findcsn( Operation *op, int mode )
        /* We want pure entries, not referrals */
        fop.o_managedsait = SLAP_CONTROL_CRITICAL;
 
-       fbuf.bv_val = buf;
        cf.f_ava = &eq;
        cf.f_av_desc = slap_schema.si_ad_entryCSN;
        cf.f_next = NULL;
@@ -580,13 +577,13 @@ syncprov_findcsn( Operation *op, int mode )
        fop.ors_limit = NULL;
        fop.ors_tlimit = SLAP_NO_LIMIT;
        fop.ors_filter = &cf;
-       fop.ors_filterstr = fbuf;
+       fop.ors_filterstr.bv_val = buf;
 
        switch( mode ) {
        case FIND_MAXCSN:
                cf.f_choice = LDAP_FILTER_GE;
                cf.f_av_value = si->si_ctxcsn;
-               fbuf.bv_len = sprintf( buf, "(entryCSN>=%s)",
+               fop.ors_filterstr.bv_len = sprintf( buf, "(entryCSN>=%s)",
                        cf.f_av_value.bv_val );
                fop.ors_attrsonly = 0;
                fop.ors_attrs = csn_anlist;
@@ -600,7 +597,7 @@ syncprov_findcsn( Operation *op, int mode )
        case FIND_CSN:
                cf.f_choice = LDAP_FILTER_LE;
                cf.f_av_value = srs->sr_state.ctxcsn;
-               fbuf.bv_len = sprintf( buf, "(entryCSN<=%s)",
+               fop.ors_filterstr.bv_len = sprintf( buf, "(entryCSN<=%s)",
                        cf.f_av_value.bv_val );
                fop.ors_attrsonly = 1;
                fop.ors_attrs = slap_anlist_no_attrs;
@@ -708,7 +705,6 @@ syncprov_qplay( Operation *op, slap_overinst *on, syncops *so )
        opc.son = on;
        op->o_bd->bd_info = (BackendInfo *)on->on_info;
        for (sr = so->s_res; sr; sr=srnext) {
-               int rc;
                srnext = sr->s_next;
                opc.sdn = sr->s_dn;
                opc.sndn = sr->s_ndn;
@@ -747,7 +743,6 @@ static int
 syncprov_sendresp( Operation *op, opcookie *opc, syncops *so, Entry **e, int mode, int queue )
 {
        slap_overinst *on = opc->son;
-       syncprov_info_t *si = on->on_bi.bi_private;
 
        SlapReply rs = { REP_SEARCH };
        LDAPControl *ctrls[2];
@@ -757,6 +752,9 @@ syncprov_sendresp( Operation *op, opcookie *opc, syncops *so, Entry **e, int mod
        Operation sop = *so->s_op;
        Opheader ohdr;
 
+       if ( so->s_op->o_abandon )
+               return SLAPD_ABANDON;
+
        ohdr = *sop.o_hdr;
        sop.o_hdr = &ohdr;
        sop.o_tmpmemctx = op->o_tmpmemctx;
@@ -782,6 +780,11 @@ syncprov_sendresp( Operation *op, opcookie *opc, syncops *so, Entry **e, int mod
                        return syncprov_qresp( opc, so, mode );
                }
                ldap_pvt_thread_mutex_unlock( &so->s_mutex );
+
+               /* If syncprov_qplay returned any other error, bail out. */
+               if ( rs.sr_err ) {
+                       return rs.sr_err;
+               }
        } else {
                /* Queueing not allowed and conn is busy, give up */
                if ( sop.o_conn->c_writewaiter )
@@ -841,6 +844,18 @@ syncprov_sendresp( Operation *op, opcookie *opc, syncops *so, Entry **e, int mod
        op->o_tmpfree( rs.sr_ctrls[0], op->o_tmpmemctx );
        op->o_private = sop.o_private;
        rs.sr_ctrls = NULL;
+       /* Check queue again here; if we were hanging in a send and eventually
+        * recovered, there may be more to send now. But don't check if the
+        * original psearch has been abandoned.
+        */
+       if ( so->s_op->o_abandon )
+               return SLAPD_ABANDON;
+
+       if ( rs.sr_err == LDAP_SUCCESS && queue && so->s_res ) {
+               ldap_pvt_thread_mutex_lock( &so->s_mutex );
+               rs.sr_err = syncprov_qplay( &sop, on, so );
+               ldap_pvt_thread_mutex_unlock( &so->s_mutex );
+       }
        return rs.sr_err;
 }
 
@@ -968,6 +983,12 @@ syncprov_matchops( Operation *op, opcookie *opc, int saveit )
        if ( op->o_tag != LDAP_REQ_ADD ) {
                op->o_bd->bd_info = (BackendInfo *)on->on_info;
                rc = be_entry_get_rw( op, fc.fdn, NULL, NULL, 0, &e );
+               /* If we're sending responses now, make a copy and unlock the DB */
+               if ( e && !saveit ) {
+                       Entry *e2 = entry_dup( e );
+                       be_entry_release_rw( op, e, 0 );
+                       e = e2;
+               }
                op->o_bd->bd_info = (BackendInfo *)on;
                if ( rc ) return;
        } else {
@@ -1049,7 +1070,7 @@ syncprov_matchops( Operation *op, opcookie *opc, int saveit )
                }
        }
        ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
-done:
+
        if ( op->o_tag != LDAP_REQ_ADD && e ) {
                op->o_bd->bd_info = (BackendInfo *)on->on_info;
                be_entry_release_rw( op, e, 0 );
@@ -1080,7 +1101,6 @@ syncprov_op_cleanup( Operation *op, SlapReply *rs )
        mtdummy.mt_op = op;
        ldap_pvt_thread_mutex_lock( &si->si_mods_mutex );
        mt = avl_find( si->si_mods, &mtdummy, sp_avl_cmp );
-       ldap_pvt_thread_mutex_unlock( &si->si_mods_mutex );
        if ( mt ) {
                modinst *mi = mt->mt_mods;
 
@@ -1091,14 +1111,13 @@ syncprov_op_cleanup( Operation *op, SlapReply *rs )
                        mt->mt_op = mt->mt_mods->mi_op;
                        ldap_pvt_thread_mutex_unlock( &mt->mt_mutex );
                } else {
-                       ldap_pvt_thread_mutex_lock( &si->si_mods_mutex );
                        avl_delete( &si->si_mods, mt, sp_avl_cmp );
-                       ldap_pvt_thread_mutex_unlock( &si->si_mods_mutex );
                        ldap_pvt_thread_mutex_unlock( &mt->mt_mutex );
                        ldap_pvt_thread_mutex_destroy( &mt->mt_mutex );
                        ch_free( mt );
                }
        }
+       ldap_pvt_thread_mutex_unlock( &si->si_mods_mutex );
        if ( !BER_BVISNULL( &opc->suuid ))
                op->o_tmpfree( opc->suuid.bv_val, op->o_tmpmemctx );
        if ( !BER_BVISNULL( &opc->sndn ))
@@ -1127,6 +1146,7 @@ syncprov_checkpoint( Operation *op, SlapReply *rs, slap_overinst *on )
        mod.sml_nvalues = NULL;
        mod.sml_desc = slap_schema.si_ad_contextCSN;
        mod.sml_op = LDAP_MOD_REPLACE;
+       mod.sml_flags = 0;
        mod.sml_next = NULL;
 
        cb.sc_response = slap_null_cb;
@@ -1160,10 +1180,10 @@ syncprov_add_slog( Operation *op, struct berval *csn )
                se->se_tag = op->o_tag;
 
                se->se_uuid.bv_val = (char *)(se+1);
-               se->se_csn.bv_val = se->se_uuid.bv_val + opc->suuid.bv_len + 1;
                AC_MEMCPY( se->se_uuid.bv_val, opc->suuid.bv_val, opc->suuid.bv_len );
                se->se_uuid.bv_len = opc->suuid.bv_len;
 
+               se->se_csn.bv_val = se->se_uuid.bv_val + opc->suuid.bv_len;
                AC_MEMCPY( se->se_csn.bv_val, csn->bv_val, csn->bv_len );
                se->se_csn.bv_val[csn->bv_len] = '\0';
                se->se_csn.bv_len = csn->bv_len;
@@ -1207,7 +1227,6 @@ syncprov_playlog( Operation *op, SlapReply *rs, sessionlog *sl,
        struct berval *oldcsn, struct berval *ctxcsn )
 {
        slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
-       syncprov_info_t         *si = on->on_bi.bi_private;
        slog_entry *se;
        int i, j, ndel, num, nmods, mmods;
        BerVarray uuids;
@@ -1668,18 +1687,16 @@ syncprov_search_response( Operation *op, SlapReply *rs )
 {
        searchstate *ss = op->o_callback->sc_private;
        slap_overinst *on = ss->ss_on;
-       syncprov_info_t         *si = 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 ) {
-               int i;
                /* If we got a referral without a referral object, there's
                 * something missing that we cannot replicate. Just ignore it.
                 * The consumer will abort because we didn't send the expected
                 * control.
                 */
                if ( !rs->sr_entry ) {
-                       assert( rs->sr_entry );
+                       assert( rs->sr_entry != NULL );
                        Debug( LDAP_DEBUG_ANY, "bogus referral in context\n",0,0,0 );
                        return SLAP_CB_CONTINUE;
                }
@@ -1971,42 +1988,93 @@ syncprov_operational(
        return SLAP_CB_CONTINUE;
 }
 
+enum {
+       SP_CHKPT = 1,
+       SP_SESSL
+};
+
+static ConfigDriver sp_cf_gen;
+
+static ConfigTable spcfg[] = {
+       { "syncprov-checkpoint", "ops> <minutes", 3, 3, 0, ARG_MAGIC|SP_CHKPT,
+               sp_cf_gen, "( OLcfgOvAt:1.1 NAME 'olcSpCheckpoint' "
+                       "DESC 'ContextCSN checkpoint interval in ops and minutes' "
+                       "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
+       { "syncprov-sessionlog", "size", 2, 2, 0, ARG_INT|ARG_MAGIC|SP_SESSL,
+               sp_cf_gen, "( OLcfgOvAt:1.2 NAME 'olcSpSessionlog' "
+                       "DESC 'Session log size in ops' "
+                       "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
+       { NULL, NULL, 0, 0, 0, ARG_IGNORED }
+};
+
+static ConfigOCs spocs[] = {
+       { "( OLcfgOvOc:1.1 "
+               "NAME 'olcSyncProvConfig' "
+               "DESC 'SyncRepl Provider configuration' "
+               "SUP olcOverlayConfig "
+               "MAY ( olcSpCheckpoint $ olcSpSessionlog ) )",
+                       Cft_Overlay, spcfg },
+       { NULL, 0, NULL }
+};
+
 static int
-syncprov_db_config(
-       BackendDB       *be,
-       const char      *fname,
-       int             lineno,
-       int             argc,
-       char    **argv
-)
+sp_cf_gen(ConfigArgs *c)
 {
-       slap_overinst           *on = (slap_overinst *)be->bd_info;
+       slap_overinst           *on = (slap_overinst *)c->bi;
        syncprov_info_t         *si = (syncprov_info_t *)on->on_bi.bi_private;
-
-       if ( strcasecmp( argv[ 0 ], "syncprov-checkpoint" ) == 0 ) {
-               if ( argc != 3 ) {
-                       fprintf( stderr, "%s: line %d: wrong number of arguments in "
-                               "\"syncprov-checkpoint <ops> <minutes>\"\n", fname, lineno );
-                       return -1;
+       int rc = 0;
+
+       if ( c->op == SLAP_CONFIG_EMIT ) {
+               switch ( c->type ) {
+               case SP_CHKPT:
+                       if ( si->si_chkops || si->si_chktime ) {
+                               struct berval bv;
+                               bv.bv_len = sprintf( c->msg, "%d %d",
+                                       si->si_chkops, si->si_chktime );
+                               bv.bv_val = c->msg;
+                               value_add_one( &c->rvalue_vals, &bv );
+                       } else {
+                               rc = 1;
+                       }
+                       break;
+               case SP_SESSL:
+                       if ( si->si_logs ) {
+                               c->value_int = si->si_logs->sl_size;
+                       } else {
+                               rc = 1;
+                       }
+                       break;
                }
-               si->si_chkops = atoi( argv[1] );
-               si->si_chktime = atoi( argv[2] ) * 60;
-               return 0;
-
-       } else if ( strcasecmp( argv[0], "syncprov-sessionlog" ) == 0 ) {
-               sessionlog *sl;
-               int size;
-               if ( argc != 2 ) {
-                       fprintf( stderr, "%s: line %d: wrong number of arguments in "
-                               "\"syncprov-sessionlog <size>\"\n", fname, lineno );
-                       return -1;
+               return rc;
+       } else if ( c->op == LDAP_MOD_DELETE ) {
+               switch ( c->type ) {
+               case SP_CHKPT:
+                       si->si_chkops = 0;
+                       si->si_chktime = 0;
+                       break;
+               case SP_SESSL:
+                       if ( si->si_logs )
+                               si->si_logs->sl_size = 0;
+                       else
+                               rc = LDAP_NO_SUCH_ATTRIBUTE;
+                       break;
                }
-               size = atoi( argv[1] );
+               return rc;
+       }
+       switch ( c->type ) {
+       case SP_CHKPT:
+               si->si_chkops = atoi( c->argv[1] );
+               si->si_chktime = atoi( c->argv[2] ) * 60;
+               break;
+       case SP_SESSL: {
+               sessionlog *sl;
+               int size = c->value_int;
+
                if ( size < 0 ) {
-                       fprintf( stderr,
-                               "%s: line %d: session log size %d is negative\n",
-                               fname, lineno, size );
-                       return -1;
+                       sprintf( c->msg, "%s size %d is negative",
+                               c->argv[0], size );
+                       Debug( LDAP_DEBUG_CONFIG, "%s: %s\n", c->log, c->msg, 0 );
+                       return ARG_BAD_CONF;
                }
                sl = si->si_logs;
                if ( !sl ) {
@@ -2019,10 +2087,10 @@ syncprov_db_config(
                        si->si_logs = sl;
                }
                sl->sl_size = size;
-               return 0;
+               }
+               break;
        }
-
-       return SLAP_CONF_UNKNOWN;
+       return rc;
 }
 
 /* Cheating - we have no thread pool context for these functions,
@@ -2038,6 +2106,18 @@ typedef struct thread_keys {
 /* A fake thread context */
 static thread_keys thrctx[MAXKEYS];
 
+/* ITS#3456 we cannot run this search on the main thread, must use a
+ * child thread in order to insure we have a big enough stack.
+ */
+static void *
+syncprov_db_otask(
+       void *ptr
+)
+{
+       syncprov_findcsn( ptr, FIND_MAXCSN );
+       return NULL;
+}
+
 /* Read any existing contextCSN from the underlying db.
  * Then search for any entries newer than that. If no value exists,
  * just generate it. Cache whatever result.
@@ -2079,6 +2159,8 @@ syncprov_db_open(
                slap_schema.si_ad_contextCSN, 0, &e );
 
        if ( e ) {
+               ldap_pvt_thread_t tid;
+
                a = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
                if ( a ) {
                        si->si_ctxcsn.bv_len = a->a_nvals[0].bv_len;
@@ -2094,7 +2176,8 @@ syncprov_db_open(
                op->o_req_dn = be->be_suffix[0];
                op->o_req_ndn = be->be_nsuffix[0];
                op->ors_scope = LDAP_SCOPE_SUBTREE;
-               syncprov_findcsn( op, FIND_MAXCSN );
+               ldap_pvt_thread_create( &tid, 0, syncprov_db_otask, op );
+               ldap_pvt_thread_join( tid, NULL );
        } else if ( SLAP_SYNC_SHADOW( op->o_bd )) {
                /* If we're also a consumer, and we didn't find the context entry,
                 * then don't generate anything, wait for our provider to send it
@@ -2109,11 +2192,10 @@ syncprov_db_open(
        }
 
        /* If our ctxcsn is different from what was read from the root
-        * entry, write the new value out.
+        * entry, make sure we do a checkpoint on close
         */
        if ( strcmp( si->si_ctxcsnbuf, ctxcsnbuf )) {
-               SlapReply rs = {REP_RESULT};
-               syncprov_checkpoint( op, &rs, on );
+               si->si_numops++;
        }
 
 out:
@@ -2204,7 +2286,8 @@ static int syncprov_parseCtrl (
        LDAPControl *ctrl )
 {
        ber_tag_t tag;
-       BerElement *ber;
+       BerElementBuffer berbuf;
+       BerElement *ber = (BerElement *)&berbuf;
        ber_int_t mode;
        ber_len_t len;
        struct berval cookie = BER_BVNULL;
@@ -2238,11 +2321,7 @@ static int syncprov_parseCtrl (
         *      }
         */
 
-       ber = ber_init( &ctrl->ldctl_value );
-       if( ber == NULL ) {
-               rs->sr_text = "internal error";
-               return LDAP_OTHER;
-       }
+       ber_init2( ber, &ctrl->ldctl_value, 0 );
 
        if ( (tag = ber_scanf( ber, "{i" /*}*/, &mode )) == LBER_ERROR ) {
                rs->sr_text = "Sync control : mode decoding error";
@@ -2264,7 +2343,7 @@ static int syncprov_parseCtrl (
        tag = ber_peek_tag( ber, &len );
 
        if ( tag == LDAP_TAG_SYNC_COOKIE ) {
-               if (( ber_scanf( ber, /*{*/ "o", &cookie )) == LBER_ERROR ) {
+               if (( ber_scanf( ber, /*{*/ "m", &cookie )) == LBER_ERROR ) {
                        rs->sr_text = "Sync control : cookie decoding error";
                        return LDAP_PROTOCOL_ERROR;
                }
@@ -2282,14 +2361,16 @@ static int syncprov_parseCtrl (
        sr = op->o_tmpcalloc( 1, sizeof(struct sync_control), op->o_tmpmemctx );
        sr->sr_rhint = rhint;
        if (!BER_BVISNULL(&cookie)) {
-               ber_dupbv( &sr->sr_state.octet_str, &cookie );
-               slap_parse_sync_cookie( &sr->sr_state );
+               ber_dupbv_x( &sr->sr_state.octet_str, &cookie, op->o_tmpmemctx );
+               slap_parse_sync_cookie( &sr->sr_state, op->o_tmpmemctx );
+               if ( sr->sr_state.rid == -1 ) {
+                       rs->sr_text = "Sync control : cookie parsing error";
+                       return LDAP_PROTOCOL_ERROR;
+               }
        }
 
        op->o_controls[slap_cids.sc_LDAPsync] = sr;
 
-       (void) ber_free( ber, 1 );
-
        op->o_sync = ctrl->ldctl_iscritical
                ? SLAP_CONTROL_CRITICAL
                : SLAP_CONTROL_NONCRITICAL;
@@ -2315,13 +2396,13 @@ syncprov_init()
                SLAP_CTRL_HIDE|SLAP_CTRL_SEARCH, NULL,
                syncprov_parseCtrl, &slap_cids.sc_LDAPsync );
        if ( rc != LDAP_SUCCESS ) {
-               fprintf( stderr, "Failed to register control %d\n", rc );
+               Debug( LDAP_DEBUG_ANY,
+                       "syncprov_init: Failed to register control %d\n", rc, 0, 0 );
                return rc;
        }
 
        syncprov.on_bi.bi_type = "syncprov";
        syncprov.on_bi.bi_db_init = syncprov_db_init;
-       syncprov.on_bi.bi_db_config = syncprov_db_config;
        syncprov.on_bi.bi_db_destroy = syncprov_db_destroy;
        syncprov.on_bi.bi_db_open = syncprov_db_open;
        syncprov.on_bi.bi_db_close = syncprov_db_close;
@@ -2338,6 +2419,11 @@ syncprov_init()
        syncprov.on_bi.bi_extended = syncprov_op_extended;
        syncprov.on_bi.bi_operational = syncprov_operational;
 
+       syncprov.on_bi.bi_cf_ocs = spocs;
+
+       rc = config_register_schema( spcfg, spocs );
+       if ( rc ) return rc;
+
        return overlay_register( &syncprov );
 }