+ LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
+ if ( si->si_rid == sc->rid ) {
+ cmdline_cookie_found = 1;
+ break;
+ }
+ }
+
+ if ( cmdline_cookie_found ) {
+ /* cookie is supplied in the command line */
+ BerVarray cookie = NULL;
+ struct berval cookie_bv;
+
+ LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
+ slap_sync_cookie_free( &si->si_syncCookie, 0 );
+
+ /* read stored cookie if it exists */
+ backend_attribute( op, NULL, &op->o_req_ndn,
+ slap_schema.si_ad_syncreplCookie, &cookie );
+
+ if ( !cookie ) {
+ /* no stored cookie */
+ if ( sc->ctxcsn == NULL ||
+ sc->ctxcsn->bv_val == NULL ) {
+ /* if cmdline cookie does not have ctxcsn */
+ /* component, set it to an initial value */
+ slap_init_sync_cookie_ctxcsn( sc );
+ }
+ slap_dup_sync_cookie( &si->si_syncCookie, sc );
+ slap_sync_cookie_free( sc, 1 );
+ sc = NULL;
+ } else {
+ /* stored cookie */
+ struct berval newcookie = { 0, NULL };
+ ber_dupbv( &cookie_bv, &cookie[0] );
+ ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
+ slap_parse_sync_cookie( &si->si_syncCookie );
+ ber_bvarray_free( si->si_syncCookie.octet_str );
+ si->si_syncCookie.octet_str = NULL;
+ ber_bvarray_free_x( cookie, op->o_tmpmemctx );
+ if ( sc->sid != -1 ) {
+ /* command line cookie wins */
+ si->si_syncCookie.sid = sc->sid;
+ }
+ if ( sc->ctxcsn != NULL ) {
+ /* command line cookie wins */
+ if ( si->si_syncCookie.ctxcsn ) {
+ ber_bvarray_free( si->si_syncCookie.ctxcsn );
+ si->si_syncCookie.ctxcsn = NULL;
+ }
+ ber_dupbv( &cookie_bv, &sc->ctxcsn[0] );
+ ber_bvarray_add( &si->si_syncCookie.ctxcsn, &cookie_bv );
+ }
+ if ( sc->rid != -1 ) {
+ /* command line cookie wins */
+ si->si_syncCookie.rid = sc->rid;
+ }
+ slap_sync_cookie_free( sc, 1 );
+ sc = NULL;
+ slap_compose_sync_cookie( NULL, &newcookie,
+ &si->si_syncCookie.ctxcsn[0],
+ si->si_syncCookie.sid, si->si_syncCookie.rid );
+ ber_bvarray_add( &si->si_syncCookie.octet_str, &newcookie );
+ }
+ } else {
+ /* no command line cookie is specified */
+ if ( si->si_syncCookie.octet_str == NULL ) {
+ BerVarray cookie = NULL;
+ struct berval cookie_bv;
+ /* try to read stored cookie */
+ backend_attribute( op, NULL, &op->o_req_ndn,
+ slap_schema.si_ad_syncreplCookie, &cookie );
+ if ( cookie ) {
+ ber_dupbv( &cookie_bv, &cookie[0] );
+ ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
+ slap_parse_sync_cookie( &si->si_syncCookie );
+ ber_bvarray_free_x( cookie, op->o_tmpmemctx );
+ }
+ }
+ }
+
+ rc = ldap_sync_search( si, op->o_tmpmemctx );