1 /* syncrepl.c -- Replication Engine which uses the LDAP Sync protocol */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2014 The OpenLDAP Foundation.
6 * Portions Copyright 2003 by IBM Corporation.
7 * Portions Copyright 2003-2008 by Howard Chu, Symas Corporation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
23 #include <ac/string.h>
24 #include <ac/socket.h>
28 #include "lutil_ldap.h"
36 #define SUFFIXM_CTX "<suffix massage>"
41 struct nonpresent_entry {
42 struct berval *npe_name;
43 struct berval *npe_nname;
44 LDAP_LIST_ENTRY(nonpresent_entry) npe_link;
47 typedef struct cookie_state {
48 ldap_pvt_thread_mutex_t cs_mutex;
49 struct berval *cs_vals;
55 /* pending changes, not yet committed */
56 ldap_pvt_thread_mutex_t cs_pmutex;
57 struct berval *cs_pvals;
62 #define SYNCDATA_DEFAULT 0 /* entries are plain LDAP entries */
63 #define SYNCDATA_ACCESSLOG 1 /* entries are accesslog format */
64 #define SYNCDATA_CHANGELOG 2 /* entries are changelog format */
66 #define SYNCLOG_LOGGING 0 /* doing a log-based update */
67 #define SYNCLOG_FALLBACK 1 /* doing a full refresh */
69 #define RETRYNUM_FOREVER (-1) /* retry forever */
70 #define RETRYNUM_TAIL (-2) /* end of retrynum array */
71 #define RETRYNUM_VALID(n) ((n) >= RETRYNUM_FOREVER) /* valid retrynum */
72 #define RETRYNUM_FINITE(n) ((n) > RETRYNUM_FOREVER) /* not forever */
74 typedef struct syncinfo_s {
75 struct syncinfo_s *si_next;
80 char si_ridtxt[ STRLENOF("rid=999") + 1 ];
81 slap_bindconf si_bindconf;
82 struct berval si_base;
83 struct berval si_logbase;
84 struct berval si_filterstr;
86 struct berval si_logfilterstr;
87 struct berval si_contextdn;
91 AttributeName *si_anlist;
92 AttributeName *si_exanlist;
97 int si_schemachecking;
98 int si_type; /* the active type */
99 int si_ctype; /* the configured type */
101 time_t *si_retryinterval;
102 int *si_retrynum_init;
104 struct sync_cookie si_syncCookie;
105 cookie_state *si_cookieState;
110 int si_refreshDelete;
111 int si_refreshPresent;
116 int si_strict_refresh; /* stop listening during fallback refresh */
119 Avlnode *si_presentlist;
122 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
123 #ifdef ENABLE_REWRITE
124 struct rewrite_info *si_rewrite;
125 struct berval si_suffixm;
127 ldap_pvt_thread_mutex_t si_mutex;
130 static int syncuuid_cmp( const void *, const void * );
131 static int presentlist_insert( syncinfo_t* si, struct berval *syncUUID );
132 static void presentlist_delete( Avlnode **av, struct berval *syncUUID );
133 static char *presentlist_find( Avlnode *av, struct berval *syncUUID );
134 static int presentlist_free( Avlnode *av );
135 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray, struct sync_cookie *, int );
136 static int syncrepl_message_to_op(
137 syncinfo_t *, Operation *, LDAPMessage * );
138 static int syncrepl_message_to_entry(
139 syncinfo_t *, Operation *, LDAPMessage *,
140 Modifications **, Entry **, int, struct berval* );
141 static int syncrepl_entry(
142 syncinfo_t *, Operation*, Entry*,
143 Modifications**,int, struct berval*,
144 struct berval *cookieCSN );
145 static int syncrepl_updateCookie(
146 syncinfo_t *, Operation *,
147 struct sync_cookie * );
148 static struct berval * slap_uuidstr_from_normalized(
149 struct berval *, struct berval *, void * );
150 static int syncrepl_add_glue_ancestors(
151 Operation* op, Entry *e );
153 /* delta-mmr overlay handler */
154 static int syncrepl_op_modify( Operation *op, SlapReply *rs );
156 /* callback functions */
157 static int dn_callback( Operation *, SlapReply * );
158 static int nonpresent_callback( Operation *, SlapReply * );
159 static int null_callback( Operation *, SlapReply * );
161 static AttributeDescription *sync_descs[4];
164 static AttributeDescription *ad_reqMod, *ad_reqDN;
166 typedef struct logschema {
168 struct berval ls_req;
169 struct berval ls_mod;
170 struct berval ls_newRdn;
171 struct berval ls_delRdn;
172 struct berval ls_newSup;
175 static logschema changelog_sc = {
177 BER_BVC("changeType"),
180 BER_BVC("deleteOldRDN"),
181 BER_BVC("newSuperior")
184 static logschema accesslog_sc = {
188 BER_BVC("reqNewRDN"),
189 BER_BVC("reqDeleteOldRDN"),
190 BER_BVC("reqNewSuperior")
194 syncrepl_state2str( int state )
197 case LDAP_SYNC_PRESENT:
203 case LDAP_SYNC_MODIFY:
206 case LDAP_SYNC_DELETE:
213 static slap_overinst syncrepl_ov;
216 init_syncrepl(syncinfo_t *si)
219 char **attrs, **exattrs;
221 if ( !syncrepl_ov.on_bi.bi_type ) {
222 syncrepl_ov.on_bi.bi_type = "syncrepl";
223 syncrepl_ov.on_bi.bi_op_modify = syncrepl_op_modify;
224 overlay_register( &syncrepl_ov );
227 /* delta-MMR needs the overlay, nothing else does.
228 * This must happen before accesslog overlay is configured.
230 if ( si->si_syncdata &&
231 !overlay_is_inst( si->si_be, syncrepl_ov.on_bi.bi_type )) {
232 overlay_config( si->si_be, syncrepl_ov.on_bi.bi_type, -1, NULL, NULL );
235 logschema *ls = &accesslog_sc;
237 slap_bv2ad( &ls->ls_mod, &ad_reqMod, &text );
238 slap_bv2ad( &ls->ls_dn, &ad_reqDN, &text );
242 if ( !sync_descs[0] ) {
243 sync_descs[0] = slap_schema.si_ad_objectClass;
244 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
245 sync_descs[2] = slap_schema.si_ad_entryCSN;
246 sync_descs[3] = NULL;
249 if ( si->si_allattrs && si->si_allopattrs )
252 attrs = anlist2attrs( si->si_anlist );
255 if ( si->si_allattrs ) {
258 if ( !is_at_operational( at_find( attrs[i] ) ) ) {
259 for ( j = i; attrs[j] != NULL; j++ ) {
262 attrs[j] = attrs[j+1];
268 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
269 attrs[i] = ch_strdup("*");
272 } else if ( si->si_allopattrs ) {
275 if ( is_at_operational( at_find( attrs[i] ) ) ) {
276 for ( j = i; attrs[j] != NULL; j++ ) {
279 attrs[j] = attrs[j+1];
285 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
286 attrs[i] = ch_strdup("+");
290 for ( i = 0; sync_descs[i] != NULL; i++ ) {
293 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
294 for ( k = j; attrs[k] != NULL; k++ ) {
297 attrs[k] = attrs[k+1];
305 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
307 if ( si->si_allopattrs ) {
308 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ) );
310 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ) );
314 if ( si->si_allopattrs ) {
315 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
317 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
318 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
326 if ( si->si_allattrs == si->si_allopattrs ) {
327 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
328 attrs[i++] = ch_strdup( "*" );
329 attrs[i++] = ch_strdup( "+" );
330 } else if ( si->si_allattrs && !si->si_allopattrs ) {
331 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
332 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
333 attrs[i++] = ch_strdup( "*" );
334 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
335 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
337 } else if ( !si->si_allattrs && si->si_allopattrs ) {
338 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
339 attrs[i++] = ch_strdup( "+" );
340 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
345 si->si_attrs = attrs;
347 exattrs = anlist2attrs( si->si_exanlist );
350 for ( n = 0; exattrs[n] != NULL; n++ ) ;
352 for ( i = 0; sync_descs[i] != NULL; i++ ) {
354 while ( exattrs[j] != NULL ) {
355 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
356 ch_free( exattrs[j] );
357 for ( k = j; exattrs[k] != NULL; k++ ) {
358 exattrs[k] = exattrs[k+1];
366 for ( i = 0; exattrs[i] != NULL; i++ ) {
367 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
369 if ( ( oc = si->si_anlist[j].an_oc ) ) {
371 while ( oc->soc_required[k] ) {
372 if ( !strcmp( exattrs[i],
373 oc->soc_required[k]->sat_cname.bv_val ) ) {
374 ch_free( exattrs[i] );
375 for ( l = i; exattrs[l]; l++ ) {
376 exattrs[l] = exattrs[l+1];
386 for ( i = 0; exattrs[i] != NULL; i++ ) ;
389 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *) );
392 si->si_exattrs = exattrs;
400 BerElementBuffer berbuf;
401 BerElement *ber = (BerElement *)&berbuf;
402 LDAPControl c[3], *ctrls[4];
406 char **attrs, *lattrs[8];
411 /* setup LDAP SYNC control */
412 ber_init2( ber, NULL, LBER_USE_DER );
413 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
415 /* If we're using a log but we have no state, then fallback to
416 * normal mode for a full refresh.
418 if ( si->si_syncdata && !si->si_syncCookie.numcsns ) {
419 si->si_logstate = SYNCLOG_FALLBACK;
422 /* Use the log parameters if we're in log mode */
423 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
425 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
429 lattrs[0] = ls->ls_dn.bv_val;
430 lattrs[1] = ls->ls_req.bv_val;
431 lattrs[2] = ls->ls_mod.bv_val;
432 lattrs[3] = ls->ls_newRdn.bv_val;
433 lattrs[4] = ls->ls_delRdn.bv_val;
434 lattrs[5] = ls->ls_newSup.bv_val;
435 lattrs[6] = slap_schema.si_ad_entryCSN->ad_cname.bv_val;
439 base = si->si_logbase.bv_val;
440 filter = si->si_logfilterstr.bv_val;
443 scope = LDAP_SCOPE_SUBTREE;
446 base = si->si_base.bv_val;
447 filter = si->si_filterstr.bv_val;
448 attrs = si->si_attrs;
449 attrsonly = si->si_attrsonly;
450 scope = si->si_scope;
452 if ( si->si_syncdata && si->si_logstate == SYNCLOG_FALLBACK ) {
453 si->si_type = LDAP_SYNC_REFRESH_ONLY;
455 si->si_type = si->si_ctype;
458 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
460 ber_printf( ber, "{eOb}",
461 abs(si->si_type), &si->si_syncCookie.octet_str, rhint );
463 ber_printf( ber, "{eb}",
464 abs(si->si_type), rhint );
467 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 ) ) == -1 ) {
472 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
473 c[0].ldctl_iscritical = si->si_type < 0;
476 c[1].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
477 BER_BVZERO( &c[1].ldctl_value );
478 c[1].ldctl_iscritical = 1;
481 if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
482 c[2].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
483 c[2].ldctl_value = si->si_bindconf.sb_authzId;
484 c[2].ldctl_iscritical = 1;
491 rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
492 ctrls, NULL, NULL, si->si_slimit, &si->si_msgid );
505 SlapReply rs = {REP_SEARCH};
506 int i, j, changed = 0;
508 /* Look for contextCSN from syncprov overlay. If
509 * there's no overlay, this will be a no-op. That means
510 * this is a pure consumer, so local changes will not be
511 * allowed, and all changes will already be reflected in
514 a.a_desc = slap_schema.si_ad_contextCSN;
516 e.e_name = si->si_contextdn;
517 e.e_nname = si->si_contextdn;
518 at[0].an_name = a.a_desc->ad_cname;
519 at[0].an_desc = a.a_desc;
520 BER_BVZERO( &at[1].an_name );
522 rs.sr_flags = REP_ENTRY_MODIFIABLE;
524 op->o_req_dn = e.e_name;
525 op->o_req_ndn = e.e_nname;
527 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
528 i = backend_operational( op, &rs );
529 if ( i == LDAP_SUCCESS && a.a_nvals ) {
530 int num = a.a_numvals;
531 /* check for differences */
532 if ( num != si->si_cookieState->cs_num ) {
535 for ( i=0; i<num; i++ ) {
536 if ( ber_bvcmp( &a.a_nvals[i],
537 &si->si_cookieState->cs_vals[i] )) {
544 ber_bvarray_free( si->si_cookieState->cs_vals );
545 ch_free( si->si_cookieState->cs_sids );
546 si->si_cookieState->cs_num = num;
547 si->si_cookieState->cs_vals = a.a_nvals;
548 si->si_cookieState->cs_sids = slap_parse_csn_sids( a.a_nvals,
550 si->si_cookieState->cs_age++;
552 ber_bvarray_free( a.a_nvals );
554 ber_bvarray_free( a.a_vals );
556 /* See if the cookieState has changed due to anything outside
557 * this particular consumer. That includes other consumers in
558 * the same context, or local changes detected above.
560 if ( si->si_cookieState->cs_num > 0 && si->si_cookieAge !=
561 si->si_cookieState->cs_age ) {
562 if ( !si->si_syncCookie.numcsns ) {
563 ber_bvarray_free( si->si_syncCookie.ctxcsn );
564 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
565 si->si_cookieState->cs_vals, NULL );
568 for (i=0; !BER_BVISNULL( &si->si_syncCookie.ctxcsn[i] ); i++) {
569 /* bogus, just dup everything */
570 if ( si->si_syncCookie.sids[i] == -1 ) {
571 ber_bvarray_free( si->si_syncCookie.ctxcsn );
572 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
573 si->si_cookieState->cs_vals, NULL );
577 for (j=0; j<si->si_cookieState->cs_num; j++) {
578 if ( si->si_syncCookie.sids[i] !=
579 si->si_cookieState->cs_sids[j] )
581 if ( bvmatch( &si->si_syncCookie.ctxcsn[i],
582 &si->si_cookieState->cs_vals[j] ))
584 ber_bvreplace( &si->si_syncCookie.ctxcsn[i],
585 &si->si_cookieState->cs_vals[j] );
593 si->si_cookieAge = si->si_cookieState->cs_age;
594 ch_free( si->si_syncCookie.octet_str.bv_val );
595 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
596 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
597 si->si_syncCookie.sid );
598 ch_free( si->si_syncCookie.sids );
599 slap_reparse_sync_cookie( &si->si_syncCookie, op->o_tmpmemctx );
601 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
611 int cmdline_cookie_found = 0;
613 struct sync_cookie *sc = NULL;
618 rc = slap_client_connect( &si->si_ld, &si->si_bindconf );
619 if ( rc != LDAP_SUCCESS ) {
622 op->o_protocol = LDAP_VERSION3;
624 /* Set SSF to strongest of TLS, SASL SSFs */
627 op->o_transport_ssf = 0;
629 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
630 == LDAP_SUCCESS && ssl != NULL )
632 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
634 #endif /* HAVE_TLS */
636 ber_len_t ssf; /* ITS#5403, 3864 LDAP_OPT_X_SASL_SSF probably ought
637 to use sasl_ssf_t but currently uses ber_len_t */
638 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &ssf )
640 op->o_sasl_ssf = ssf;
642 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
643 ? op->o_sasl_ssf : op->o_tls_ssf;
645 ldap_set_option( si->si_ld, LDAP_OPT_TIMELIMIT, &si->si_tlimit );
647 rc = LDAP_DEREF_NEVER; /* actually could allow DEREF_FINDING */
648 ldap_set_option( si->si_ld, LDAP_OPT_DEREF, &rc );
650 ldap_set_option( si->si_ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
652 si->si_syncCookie.rid = si->si_rid;
654 /* whenever there are multiple data sources possible, advertise sid */
655 si->si_syncCookie.sid = ( SLAP_MULTIMASTER( si->si_be ) || si->si_be != si->si_wbe ) ?
658 /* We've just started up, or the remote server hasn't sent us
659 * any meaningful state.
661 if ( !si->si_syncCookie.ctxcsn ) {
664 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
665 if ( si->si_rid == sc->rid ) {
666 cmdline_cookie_found = 1;
671 if ( cmdline_cookie_found ) {
672 /* cookie is supplied in the command line */
674 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
676 slap_sync_cookie_free( &si->si_syncCookie, 0 );
677 si->si_syncCookie.octet_str = sc->octet_str;
679 /* ctxcsn wasn't parsed yet, do it now */
680 slap_parse_sync_cookie( &si->si_syncCookie, NULL );
682 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
683 if ( !si->si_cookieState->cs_num ) {
684 /* get contextCSN shadow replica from database */
685 BerVarray csn = NULL;
686 void *ctx = op->o_tmpmemctx;
688 op->o_req_ndn = si->si_contextdn;
689 op->o_req_dn = op->o_req_ndn;
691 /* try to read stored contextCSN */
692 op->o_tmpmemctx = NULL;
693 backend_attribute( op, NULL, &op->o_req_ndn,
694 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
695 op->o_tmpmemctx = ctx;
697 si->si_cookieState->cs_vals = csn;
698 for (i=0; !BER_BVISNULL( &csn[i] ); i++);
699 si->si_cookieState->cs_num = i;
700 si->si_cookieState->cs_sids = slap_parse_csn_sids( csn, i, NULL );
701 slap_sort_csn_sids( csn, si->si_cookieState->cs_sids, i, NULL );
704 if ( si->si_cookieState->cs_num ) {
705 ber_bvarray_free( si->si_syncCookie.ctxcsn );
706 if ( ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
707 si->si_cookieState->cs_vals, NULL )) {
709 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
712 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
713 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num *
715 for ( i=0; i<si->si_syncCookie.numcsns; i++ )
716 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
718 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
721 ch_free( si->si_syncCookie.octet_str.bv_val );
722 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
723 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
724 si->si_syncCookie.sid );
726 /* ITS#6367: recreate the cookie so it has our SID, not our peer's */
727 ch_free( si->si_syncCookie.octet_str.bv_val );
728 BER_BVZERO( &si->si_syncCookie.octet_str );
729 /* Look for contextCSN from syncprov overlay. */
730 check_syncprov( op, si );
731 if ( BER_BVISNULL( &si->si_syncCookie.octet_str ))
732 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
733 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
734 si->si_syncCookie.sid );
737 si->si_refreshDone = 0;
739 rc = ldap_sync_search( si, op->o_tmpmemctx );
741 if( rc != LDAP_SUCCESS ) {
742 Debug( LDAP_DEBUG_ANY, "do_syncrep1: %s "
743 "ldap_search_ext: %s (%d)\n",
744 si->si_ridtxt, ldap_err2string( rc ), rc );
750 ldap_unbind_ext( si->si_ld, NULL, NULL );
759 compare_csns( struct sync_cookie *sc1, struct sync_cookie *sc2, int *which )
766 if ( sc1->numcsns < sc2->numcsns ) {
767 *which = sc1->numcsns;
771 for (j=0; j<sc2->numcsns; j++) {
772 for (i=0; i<sc1->numcsns; i++) {
773 if ( sc1->sids[i] != sc2->sids[j] )
775 value_match( &match, slap_schema.si_ad_entryCSN,
776 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
777 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
778 &sc1->ctxcsn[i], &sc2->ctxcsn[j], &text );
785 if ( i == sc1->numcsns ) {
786 /* sc2 has a sid sc1 lacks */
794 #define SYNC_PAUSED -3
801 BerElementBuffer berbuf;
802 BerElement *ber = (BerElement *)&berbuf;
804 LDAPMessage *msg = NULL;
806 struct sync_cookie syncCookie = { NULL };
807 struct sync_cookie syncCookie_req = { NULL };
812 Modifications *modlist = NULL;
816 struct timeval *tout_p = NULL;
817 struct timeval tout = { 0, 0 };
819 int refreshDeletes = 0;
820 char empty[6] = "empty";
822 if ( slapd_shutdown ) {
827 ber_init2( ber, NULL, LBER_USE_DER );
828 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
830 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2 %s\n", si->si_ridtxt, 0, 0 );
832 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
834 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST && si->si_refreshDone ) {
840 while ( ( rc = ldap_result( si->si_ld, si->si_msgid, LDAP_MSG_ONE,
841 tout_p, &msg ) ) > 0 )
843 int match, punlock, syncstate;
844 struct berval *retdata, syncUUID[2], cookie = BER_BVNULL;
846 LDAPControl **rctrls = NULL, *rctrlp = NULL;
853 if ( slapd_shutdown ) {
857 switch( ldap_msgtype( msg ) ) {
858 case LDAP_RES_SEARCH_ENTRY:
859 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
860 ldap_get_dn_ber( si->si_ld, msg, NULL, &bdn );
863 bdn.bv_len = sizeof(empty)-1;
865 /* we can't work without the control */
867 LDAPControl **next = NULL;
868 /* NOTE: make sure we use the right one;
869 * a better approach would be to run thru
870 * the whole list and take care of all */
871 /* NOTE: since we issue the search request,
872 * we should know what controls to expect,
873 * and there should be none apart from the
874 * sync-related control */
875 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_STATE, rctrls, &next );
876 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_STATE, next, NULL ) )
878 bdn.bv_val[bdn.bv_len] = '\0';
879 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
880 "got search entry with multiple "
881 "Sync State control (%s)\n", si->si_ridtxt, bdn.bv_val, 0 );
882 ldap_controls_free( rctrls );
887 if ( rctrlp == NULL ) {
888 bdn.bv_val[bdn.bv_len] = '\0';
889 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
890 "got search entry without "
891 "Sync State control (%s)\n", si->si_ridtxt, bdn.bv_val, 0 );
895 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
896 if ( ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID[0] )
898 bdn.bv_val[bdn.bv_len] = '\0';
899 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s malformed message (%s)\n",
900 si->si_ridtxt, bdn.bv_val, 0 );
901 ldap_controls_free( rctrls );
905 /* FIXME: what if syncUUID is NULL or empty?
906 * (happens with back-sql...) */
907 if ( syncUUID[0].bv_len != UUIDLEN ) {
908 bdn.bv_val[bdn.bv_len] = '\0';
909 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
910 "got empty or invalid syncUUID with LDAP_SYNC_%s (%s)\n",
912 syncrepl_state2str( syncstate ), bdn.bv_val );
913 ldap_controls_free( rctrls );
918 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
919 if ( ber_scanf( ber, /*"{"*/ "m}", &cookie ) != LBER_ERROR ) {
921 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
923 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
925 if ( !BER_BVISNULL( &cookie ) ) {
926 ch_free( syncCookie.octet_str.bv_val );
927 ber_dupbv( &syncCookie.octet_str, &cookie );
929 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
931 slap_parse_sync_cookie( &syncCookie, NULL );
932 if ( syncCookie.ctxcsn ) {
933 int i, sid = slap_parse_csn_sid( syncCookie.ctxcsn );
934 check_syncprov( op, si );
935 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
936 for ( i =0; i<si->si_cookieState->cs_num; i++ ) {
938 if ( sid < si->si_cookieState->cs_sids[i] )
940 if ( si->si_cookieState->cs_sids[i] == sid ) {
941 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_vals[i] ) <= 0 ) {
942 bdn.bv_val[bdn.bv_len] = '\0';
943 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN too old, ignoring %s (%s)\n",
944 si->si_ridtxt, syncCookie.ctxcsn->bv_val, bdn.bv_val );
946 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
947 ldap_controls_free( rctrls );
955 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
957 /* check pending CSNs too */
958 while ( ldap_pvt_thread_mutex_trylock( &si->si_cookieState->cs_pmutex )) {
959 if ( slapd_shutdown ) {
963 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
964 ldap_pvt_thread_yield();
967 for ( i =0; i<si->si_cookieState->cs_pnum; i++ ) {
968 if ( sid < si->si_cookieState->cs_psids[i] )
970 if ( si->si_cookieState->cs_psids[i] == sid ) {
971 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_pvals[i] ) <= 0 ) {
972 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
973 bdn.bv_val[bdn.bv_len] = '\0';
974 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN pending, ignoring %s (%s)\n",
975 si->si_ridtxt, syncCookie.ctxcsn->bv_val, bdn.bv_val );
976 ldap_controls_free( rctrls );
980 ber_bvreplace( &si->si_cookieState->cs_pvals[i],
985 /* new SID, add it */
986 if ( i == si->si_cookieState->cs_pnum ||
987 sid != si->si_cookieState->cs_psids[i] ) {
988 slap_insert_csn_sids(
989 (struct sync_cookie *)&si->si_cookieState->cs_pvals,
990 i, sid, syncCookie.ctxcsn );
992 assert( punlock < 0 );
994 } else if (si->si_too_old) {
995 bdn.bv_val[bdn.bv_len] = '\0';
996 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN too old, ignoring (%s)\n",
997 si->si_ridtxt, bdn.bv_val, 0 );
998 ldap_controls_free( rctrls );
1002 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1007 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
1009 if ( ( rc = syncrepl_message_to_op( si, op, msg ) ) == LDAP_SUCCESS &&
1012 rc = syncrepl_updateCookie( si, op, &syncCookie );
1013 } else switch ( rc ) {
1014 case LDAP_ALREADY_EXISTS:
1015 case LDAP_NO_SUCH_OBJECT:
1016 case LDAP_NO_SUCH_ATTRIBUTE:
1017 case LDAP_TYPE_OR_VALUE_EXISTS:
1018 case LDAP_NOT_ALLOWED_ON_NONLEAF:
1019 rc = LDAP_SYNC_REFRESH_REQUIRED;
1020 si->si_logstate = SYNCLOG_FALLBACK;
1021 ldap_abandon_ext( si->si_ld, si->si_msgid, NULL, NULL );
1022 bdn.bv_val[bdn.bv_len] = '\0';
1023 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s delta-sync lost sync on (%s), switching to REFRESH\n",
1024 si->si_ridtxt, bdn.bv_val, 0 );
1025 if (si->si_strict_refresh) {
1026 slap_suspend_listeners();
1033 } else if ( ( rc = syncrepl_message_to_entry( si, op, msg,
1034 &modlist, &entry, syncstate, syncUUID ) ) == LDAP_SUCCESS )
1036 if ( ( rc = syncrepl_entry( si, op, entry, &modlist,
1037 syncstate, syncUUID, syncCookie.ctxcsn ) ) == LDAP_SUCCESS &&
1040 rc = syncrepl_updateCookie( si, op, &syncCookie );
1043 if ( punlock >= 0 ) {
1044 /* on failure, revert pending CSN */
1045 if ( rc != LDAP_SUCCESS ) {
1047 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
1048 for ( i = 0; i<si->si_cookieState->cs_num; i++ ) {
1049 if ( si->si_cookieState->cs_sids[i] == si->si_cookieState->cs_psids[punlock] ) {
1050 ber_bvreplace( &si->si_cookieState->cs_pvals[punlock],
1051 &si->si_cookieState->cs_vals[i] );
1055 if ( i == si->si_cookieState->cs_num )
1056 si->si_cookieState->cs_pvals[punlock].bv_val[0] = '\0';
1057 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
1059 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
1061 ldap_controls_free( rctrls );
1063 slap_mods_free( modlist, 1 );
1069 case LDAP_RES_SEARCH_REFERENCE:
1070 Debug( LDAP_DEBUG_ANY,
1071 "do_syncrep2: %s reference received error\n",
1072 si->si_ridtxt, 0, 0 );
1075 case LDAP_RES_SEARCH_RESULT:
1076 Debug( LDAP_DEBUG_SYNC,
1077 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT\n",
1078 si->si_ridtxt, 0, 0 );
1079 err = LDAP_OTHER; /* FIXME check parse result properly */
1080 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
1082 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
1083 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
1084 /* map old result code to registered code */
1085 err = LDAP_SYNC_REFRESH_REQUIRED;
1088 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
1089 if ( si->si_logstate == SYNCLOG_LOGGING ) {
1090 si->si_logstate = SYNCLOG_FALLBACK;
1091 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s delta-sync lost sync, switching to REFRESH\n",
1092 si->si_ridtxt, 0, 0 );
1093 if (si->si_strict_refresh) {
1094 slap_suspend_listeners();
1102 Debug( LDAP_DEBUG_ANY,
1103 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT (%d) %s\n",
1104 si->si_ridtxt, err, ldap_err2string( err ) );
1107 LDAPControl **next = NULL;
1108 /* NOTE: make sure we use the right one;
1109 * a better approach would be to run thru
1110 * the whole list and take care of all */
1111 /* NOTE: since we issue the search request,
1112 * we should know what controls to expect,
1113 * and there should be none apart from the
1114 * sync-related control */
1115 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_DONE, rctrls, &next );
1116 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_DONE, next, NULL ) )
1118 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1119 "got search result with multiple "
1120 "Sync State control\n", si->si_ridtxt, 0, 0 );
1121 ldap_controls_free( rctrls );
1127 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
1129 ber_scanf( ber, "{" /*"}"*/);
1130 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
1131 ber_scanf( ber, "m", &cookie );
1133 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1135 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1137 if ( !BER_BVISNULL( &cookie ) ) {
1138 ch_free( syncCookie.octet_str.bv_val );
1139 ber_dupbv( &syncCookie.octet_str, &cookie);
1141 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1143 slap_parse_sync_cookie( &syncCookie, NULL );
1144 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1147 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
1149 ber_scanf( ber, "b", &refreshDeletes );
1151 ber_scanf( ber, /*"{"*/ "}" );
1153 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1154 slap_sync_cookie_free( &syncCookie_req, 0 );
1155 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1157 if ( !syncCookie.ctxcsn ) {
1159 } else if ( !syncCookie_req.ctxcsn ) {
1163 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1166 ldap_controls_free( rctrls );
1168 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
1169 /* FIXME : different error behaviors according to
1170 * 1) err code : LDAP_BUSY ...
1171 * 2) on err policy : stop service, stop sync, retry
1173 if ( refreshDeletes == 0 && match < 0 &&
1174 err == LDAP_SUCCESS &&
1175 syncCookie_req.numcsns == syncCookie.numcsns )
1177 syncrepl_del_nonpresent( op, si, NULL,
1179 } else if ( si->si_presentlist ) {
1180 presentlist_free( si->si_presentlist );
1181 si->si_presentlist = NULL;
1184 if ( syncCookie.ctxcsn && match < 0 && err == LDAP_SUCCESS )
1186 rc = syncrepl_updateCookie( si, op, &syncCookie );
1188 if ( err == LDAP_SUCCESS
1189 && si->si_logstate == SYNCLOG_FALLBACK ) {
1190 si->si_logstate = SYNCLOG_LOGGING;
1191 rc = LDAP_SYNC_REFRESH_REQUIRED;
1192 slap_resume_listeners();
1198 case LDAP_RES_INTERMEDIATE:
1201 rc = ldap_parse_intermediate( si->si_ld, msg,
1202 &retoid, &retdata, NULL, 0 );
1203 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
1204 ber_init2( ber, retdata, LBER_USE_DER );
1206 switch ( si_tag = ber_peek_tag( ber, &len ) ) {
1208 case LDAP_TAG_SYNC_NEW_COOKIE:
1209 Debug( LDAP_DEBUG_SYNC,
1210 "do_syncrep2: %s %s - %s\n",
1212 "LDAP_RES_INTERMEDIATE",
1214 ber_scanf( ber, "tm", &tag, &cookie );
1215 Debug( LDAP_DEBUG_SYNC,
1216 "do_syncrep2: %s NEW_COOKIE: %s\n",
1219 if ( !BER_BVISNULL( &cookie ) ) {
1220 ch_free( syncCookie.octet_str.bv_val );
1221 ber_dupbv( &syncCookie.octet_str, &cookie );
1223 if (!BER_BVISNULL( &syncCookie.octet_str ) ) {
1224 slap_parse_sync_cookie( &syncCookie, NULL );
1225 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1228 case LDAP_TAG_SYNC_REFRESH_DELETE:
1229 case LDAP_TAG_SYNC_REFRESH_PRESENT:
1230 Debug( LDAP_DEBUG_SYNC,
1231 "do_syncrep2: %s %s - %s\n",
1233 "LDAP_RES_INTERMEDIATE",
1234 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
1235 "REFRESH_PRESENT" : "REFRESH_DELETE" );
1236 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
1237 si->si_refreshDelete = 1;
1239 si->si_refreshPresent = 1;
1241 ber_scanf( ber, "t{" /*"}"*/, &tag );
1242 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
1244 ber_scanf( ber, "m", &cookie );
1246 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1248 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1250 if ( !BER_BVISNULL( &cookie ) ) {
1251 ch_free( syncCookie.octet_str.bv_val );
1252 ber_dupbv( &syncCookie.octet_str, &cookie );
1254 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1256 slap_parse_sync_cookie( &syncCookie, NULL );
1257 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1260 /* Defaults to TRUE */
1261 if ( ber_peek_tag( ber, &len ) ==
1262 LDAP_TAG_REFRESHDONE )
1264 ber_scanf( ber, "b", &si->si_refreshDone );
1267 si->si_refreshDone = 1;
1269 ber_scanf( ber, /*"{"*/ "}" );
1270 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST &&
1271 si->si_refreshDone )
1274 case LDAP_TAG_SYNC_ID_SET:
1275 Debug( LDAP_DEBUG_SYNC,
1276 "do_syncrep2: %s %s - %s\n",
1278 "LDAP_RES_INTERMEDIATE",
1280 ber_scanf( ber, "t{" /*"}"*/, &tag );
1281 if ( ber_peek_tag( ber, &len ) ==
1282 LDAP_TAG_SYNC_COOKIE )
1284 ber_scanf( ber, "m", &cookie );
1286 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1288 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1290 if ( !BER_BVISNULL( &cookie ) ) {
1291 ch_free( syncCookie.octet_str.bv_val );
1292 ber_dupbv( &syncCookie.octet_str, &cookie );
1294 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1296 slap_parse_sync_cookie( &syncCookie, NULL );
1297 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1298 compare_csns( &syncCookie_req, &syncCookie, &m );
1301 if ( ber_peek_tag( ber, &len ) ==
1302 LDAP_TAG_REFRESHDELETES )
1304 ber_scanf( ber, "b", &refreshDeletes );
1307 rc = ber_scanf( ber, "[W]", &syncUUIDs );
1308 ber_scanf( ber, /*"{"*/ "}" );
1309 if ( rc != LBER_ERROR ) {
1310 if ( refreshDeletes ) {
1311 syncrepl_del_nonpresent( op, si, syncUUIDs,
1313 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
1316 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
1317 (void)presentlist_insert( si, &syncUUIDs[i] );
1318 slap_sl_free( syncUUIDs[i].bv_val, op->o_tmpmemctx );
1320 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
1324 slap_sync_cookie_free( &syncCookie, 0 );
1327 Debug( LDAP_DEBUG_ANY,
1328 "do_syncrep2: %s unknown syncinfo tag (%ld)\n",
1329 si->si_ridtxt, (long) si_tag, 0 );
1330 ldap_memfree( retoid );
1331 ber_bvfree( retdata );
1335 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1336 slap_sync_cookie_free( &syncCookie_req, 0 );
1337 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1339 if ( !syncCookie.ctxcsn ) {
1341 } else if ( !syncCookie_req.ctxcsn ) {
1345 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1349 if ( si->si_refreshPresent == 1 &&
1350 si_tag != LDAP_TAG_SYNC_NEW_COOKIE &&
1351 syncCookie_req.numcsns == syncCookie.numcsns ) {
1352 syncrepl_del_nonpresent( op, si, NULL,
1356 if ( syncCookie.ctxcsn )
1358 rc = syncrepl_updateCookie( si, op, &syncCookie);
1360 if ( si->si_presentlist ) {
1361 presentlist_free( si->si_presentlist );
1362 si->si_presentlist = NULL;
1366 ldap_memfree( retoid );
1367 ber_bvfree( retdata );
1373 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1374 "unknown intermediate response (%d)\n",
1375 si->si_ridtxt, rc, 0 );
1376 ldap_memfree( retoid );
1377 ber_bvfree( retdata );
1382 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1383 "unknown message (0x%02lx)\n",
1385 (unsigned long)ldap_msgtype( msg ), 0 );
1389 if ( !BER_BVISNULL( &syncCookie.octet_str ) ) {
1390 slap_sync_cookie_free( &syncCookie_req, 0 );
1391 syncCookie_req = syncCookie;
1392 memset( &syncCookie, 0, sizeof( syncCookie ));
1394 ldap_msgfree( msg );
1396 if ( ldap_pvt_thread_pool_pausing( &connection_pool )) {
1397 slap_sync_cookie_free( &syncCookie, 0 );
1398 slap_sync_cookie_free( &syncCookie_req, 0 );
1405 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
1410 if ( err != LDAP_SUCCESS ) {
1411 Debug( LDAP_DEBUG_ANY,
1412 "do_syncrep2: %s (%d) %s\n",
1413 si->si_ridtxt, err, ldap_err2string( err ) );
1416 slap_sync_cookie_free( &syncCookie, 0 );
1417 slap_sync_cookie_free( &syncCookie_req, 0 );
1419 if ( msg ) ldap_msgfree( msg );
1421 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1422 if ( si->si_conn ) {
1423 connection_client_stop( si->si_conn );
1426 ldap_unbind_ext( si->si_ld, NULL, NULL );
1438 struct re_s* rtask = arg;
1439 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1440 Connection conn = {0};
1441 OperationBuffer opbuf;
1443 int rc = LDAP_SUCCESS;
1446 int i, defer = 1, fail = 0, freeinfo = 0;
1451 if ( slapd_shutdown )
1454 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl %s\n", si->si_ridtxt, 0, 0 );
1456 /* Don't get stuck here while a pause is initiated */
1457 while ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
1458 if ( slapd_shutdown )
1460 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
1461 ldap_pvt_thread_yield();
1466 if ( si->si_ctype < 1 ) {
1470 switch( abs( si->si_type ) ) {
1471 case LDAP_SYNC_REFRESH_ONLY:
1472 case LDAP_SYNC_REFRESH_AND_PERSIST:
1475 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1479 if ( slapd_shutdown ) {
1481 if ( si->si_conn ) {
1482 connection_client_stop( si->si_conn );
1485 ldap_unbind_ext( si->si_ld, NULL, NULL );
1488 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1492 connection_fake_init( &conn, &opbuf, ctx );
1494 /* o_connids must be unique for slap_graduate_commit_csn */
1495 op->o_connid = SLAPD_SYNC_RID2SYNCCONN(si->si_rid);
1497 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1500 /* Coordinate contextCSN updates with any syncprov overlays
1501 * in use. This may be complicated by the use of the glue
1504 * Typically there is a single syncprov mastering the entire
1505 * glued tree. In that case, our contextCSN updates should
1506 * go to the master DB. But if there is no syncprov on the
1507 * master DB, then nothing special is needed here.
1509 * Alternatively, there may be individual syncprov overlays
1510 * on each glued branch. In that case, each syncprov only
1511 * knows about changes within its own branch. And so our
1512 * contextCSN updates should only go to the local DB.
1514 if ( !si->si_wbe ) {
1515 if ( SLAP_GLUE_SUBORDINATE( be ) && !overlay_is_inst( be, "syncprov" )) {
1516 BackendDB * top_be = select_backend( &be->be_nsuffix[0], 1 );
1517 if ( overlay_is_inst( top_be, "syncprov" ))
1518 si->si_wbe = top_be;
1524 if ( SLAP_SYNC_SUBENTRY( si->si_wbe )) {
1525 build_new_dn( &si->si_contextdn, &si->si_wbe->be_nsuffix[0],
1526 (struct berval *)&slap_ldapsync_cn_bv, NULL );
1528 si->si_contextdn = si->si_wbe->be_nsuffix[0];
1531 if ( !si->si_schemachecking )
1532 op->o_no_schema_check = 1;
1534 /* Establish session, do search */
1536 si->si_refreshDelete = 0;
1537 si->si_refreshPresent = 0;
1539 if ( si->si_presentlist ) {
1540 presentlist_free( si->si_presentlist );
1541 si->si_presentlist = NULL;
1544 /* use main DB when retrieving contextCSN */
1545 op->o_bd = si->si_wbe;
1546 op->o_dn = op->o_bd->be_rootdn;
1547 op->o_ndn = op->o_bd->be_rootndn;
1548 rc = do_syncrep1( op, si );
1552 /* Process results */
1553 if ( rc == LDAP_SUCCESS ) {
1554 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1556 /* use current DB */
1558 op->o_dn = op->o_bd->be_rootdn;
1559 op->o_ndn = op->o_bd->be_rootndn;
1560 rc = do_syncrep2( op, si );
1561 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1562 if ( BER_BVISNULL( &si->si_syncCookie.octet_str ))
1563 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
1564 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
1565 si->si_syncCookie.sid );
1566 rc = ldap_sync_search( si, op->o_tmpmemctx );
1571 /* We got deleted while running on cn=config */
1572 if ( si->si_ctype < 1 ) {
1573 if ( si->si_ctype == -1 ) {
1582 if ( rc != SYNC_PAUSED ) {
1583 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1584 /* If we succeeded, enable the connection for further listening.
1585 * If we failed, tear down the connection and reschedule.
1587 if ( rc == LDAP_SUCCESS ) {
1588 if ( si->si_conn ) {
1589 connection_client_enable( si->si_conn );
1591 si->si_conn = connection_client_setup( s, do_syncrepl, arg );
1593 } else if ( si->si_conn ) {
1597 if ( rc == -2 ) rc = 0;
1602 /* At this point, we have 5 cases:
1603 * 1) for any hard failure, give up and remove this task
1604 * 2) for ServerDown, reschedule this task to run later
1605 * 3) for threadpool pause, reschedule to run immediately
1606 * 4) for Refresh and Success, reschedule to run
1607 * 5) for Persist and Success, reschedule to defer
1609 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1611 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask ) ) {
1612 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1616 connection_client_stop( si->si_conn );
1620 if ( rc == SYNC_PAUSED ) {
1621 rtask->interval.tv_sec = 0;
1622 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1623 rtask->interval.tv_sec = si->si_interval;
1625 } else if ( rc == LDAP_SUCCESS ) {
1626 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1629 rtask->interval.tv_sec = si->si_interval;
1630 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1631 if ( si->si_retrynum ) {
1632 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1633 si->si_retrynum[i] = si->si_retrynum_init[i];
1635 si->si_retrynum[i] = RETRYNUM_TAIL;
1638 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1639 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1643 if ( si->si_ctype < 1
1644 || !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1646 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1649 fail = RETRYNUM_TAIL;
1650 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1651 if ( si->si_retrynum[i] > 0 )
1652 si->si_retrynum[i]--;
1653 fail = si->si_retrynum[i];
1654 rtask->interval.tv_sec = si->si_retryinterval[i];
1655 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1656 slap_wake_listener();
1660 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1661 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1664 if ( fail == RETRYNUM_TAIL ) {
1665 Debug( LDAP_DEBUG_ANY,
1666 "do_syncrepl: %s rc %d quitting\n",
1667 si->si_ridtxt, rc, 0 );
1668 } else if ( fail > 0 ) {
1669 Debug( LDAP_DEBUG_ANY,
1670 "do_syncrepl: %s rc %d retrying (%d retries left)\n",
1671 si->si_ridtxt, rc, fail );
1673 Debug( LDAP_DEBUG_ANY,
1674 "do_syncrepl: %s rc %d retrying\n",
1675 si->si_ridtxt, rc, 0 );
1679 /* Do final delete cleanup */
1681 syncinfo_free( si, 0 );
1686 #ifdef ENABLE_REWRITE
1688 syncrepl_rewrite_dn(
1691 struct berval *sdn )
1696 nul = dn->bv_val[dn->bv_len];
1697 dn->bv_val[dn->bv_len] = 0;
1698 rc = rewrite( si->si_rewrite, SUFFIXM_CTX, dn->bv_val, &sdn->bv_val );
1699 dn->bv_val[dn->bv_len] = nul;
1701 if ( sdn->bv_val == dn->bv_val )
1703 else if ( rc == REWRITE_REGEXEC_OK && sdn->bv_val )
1704 sdn->bv_len = strlen( sdn->bv_val );
1707 #define REWRITE_VAL(si, ad, bv, bv2) \
1708 BER_BVZERO( &bv2 ); \
1709 if ( si->si_rewrite && ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName) \
1710 syncrepl_rewrite_dn( si, &bv, &bv2); \
1711 if ( BER_BVISNULL( &bv2 )) \
1712 ber_dupbv( &bv2, &bv )
1713 #define REWRITE_DN(si, bv, bv2, dn, ndn) \
1714 BER_BVZERO( &bv2 ); \
1715 if (si->si_rewrite) \
1716 syncrepl_rewrite_dn(si, &bv, &bv2); \
1717 rc = dnPrettyNormal( NULL, bv2.bv_val ? &bv2 : &bv, &dn, &ndn, op->o_tmpmemctx ); \
1720 #define REWRITE_VAL(si, ad, bv, bv2) ber_dupbv(&bv2, &bv)
1721 #define REWRITE_DN(si, bv, bv2, dn, ndn) \
1722 rc = dnPrettyNormal( NULL, &bv, &dn, &ndn, op->o_tmpmemctx )
1726 static slap_verbmasks modops[] = {
1727 { BER_BVC("add"), LDAP_REQ_ADD },
1728 { BER_BVC("delete"), LDAP_REQ_DELETE },
1729 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1730 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1735 syncrepl_accesslog_mods(
1737 struct berval *vals,
1738 struct Modifications **modres
1743 AttributeDescription *ad;
1744 struct berval bv, bv2;
1746 Modifications *mod = NULL, *modlist = NULL, **modtail;
1751 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1755 colon = ber_bvchr( &bv, ':' );
1761 bv.bv_len = colon - bv.bv_val;
1762 if ( slap_bv2ad( &bv, &ad, &text ) ) {
1764 Debug( LDAP_DEBUG_ANY, "syncrepl_accesslog_mods: %s "
1765 "Invalid attribute %s, %s\n",
1766 si->si_ridtxt, bv.bv_val, text );
1767 slap_mods_free( modlist, 1 );
1773 /* Ignore dynamically generated attrs */
1774 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1778 /* Ignore excluded attrs */
1779 if ( ldap_charray_inlist( si->si_exattrs,
1780 ad->ad_type->sat_cname.bv_val ) )
1786 case '+': op = LDAP_MOD_ADD; break;
1787 case '-': op = LDAP_MOD_DELETE; break;
1788 case '=': op = LDAP_MOD_REPLACE; break;
1789 case '#': op = LDAP_MOD_INCREMENT; break;
1793 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1794 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1797 mod->sml_next = NULL;
1799 mod->sml_type = ad->ad_cname;
1800 mod->sml_values = NULL;
1801 mod->sml_nvalues = NULL;
1802 mod->sml_numvals = 0;
1805 modtail = &mod->sml_next;
1807 if ( colon[2] == ' ' ) {
1808 bv.bv_val = colon + 3;
1809 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1810 REWRITE_VAL( si, ad, bv, bv2 );
1811 ber_bvarray_add( &mod->sml_values, &bv2 );
1820 syncrepl_changelog_mods(
1822 struct berval *vals,
1823 struct Modifications **modres
1826 return -1; /* FIXME */
1829 typedef struct OpExtraSync {
1834 /* Copy the original modlist, split Replace ops into Delete/Add,
1835 * and drop mod opattrs since this modification is in the past.
1837 static Modifications *mods_dup( Operation *op, Modifications *modlist, int match )
1839 Modifications *mod, *modnew = NULL, *modtail = NULL;
1841 for ( ; modlist; modlist = modlist->sml_next ) {
1844 if ( modlist->sml_desc == slap_schema.si_ad_modifiersName ||
1845 modlist->sml_desc == slap_schema.si_ad_modifyTimestamp ||
1846 modlist->sml_desc == slap_schema.si_ad_entryCSN )
1848 if ( modlist->sml_op == LDAP_MOD_REPLACE ) {
1849 mod = op->o_tmpalloc( sizeof(Modifications), op->o_tmpmemctx );
1850 mod->sml_desc = modlist->sml_desc;
1851 mod->sml_values = NULL;
1852 mod->sml_nvalues = NULL;
1853 mod->sml_op = LDAP_MOD_DELETE;
1854 mod->sml_numvals = 0;
1859 modtail->sml_next = mod;
1863 if ( modlist->sml_numvals ) {
1864 size = (modlist->sml_numvals+1) * sizeof(struct berval);
1865 if ( modlist->sml_nvalues ) size *= 2;
1869 size += sizeof(Modifications);
1870 mod = op->o_tmpalloc( size, op->o_tmpmemctx );
1874 modtail->sml_next = mod;
1876 mod->sml_desc = modlist->sml_desc;
1877 mod->sml_numvals = modlist->sml_numvals;
1879 if ( modlist->sml_numvals ) {
1881 mod->sml_values = (BerVarray)(mod+1);
1882 for (i=0; i<mod->sml_numvals; i++)
1883 mod->sml_values[i] = modlist->sml_values[i];
1884 BER_BVZERO(&mod->sml_values[i]);
1885 if ( modlist->sml_nvalues ) {
1886 mod->sml_nvalues = mod->sml_values + mod->sml_numvals + 1;
1887 for (i=0; i<mod->sml_numvals; i++)
1888 mod->sml_nvalues[i] = modlist->sml_nvalues[i];
1889 BER_BVZERO(&mod->sml_nvalues[i]);
1891 mod->sml_nvalues = NULL;
1894 mod->sml_values = NULL;
1895 mod->sml_nvalues = NULL;
1897 if ( match < 0 && modlist->sml_op == LDAP_MOD_REPLACE )
1898 mod->sml_op = LDAP_MOD_ADD;
1900 mod->sml_op = modlist->sml_op;
1901 mod->sml_next = NULL;
1906 typedef struct resolve_ctxt {
1908 Modifications *rx_mods;
1912 compare_vals( Modifications *m1, Modifications *m2 )
1915 struct berval *bv1, *bv2;
1917 if ( m2->sml_nvalues ) {
1918 bv2 = m2->sml_nvalues;
1919 bv1 = m1->sml_nvalues;
1921 bv2 = m2->sml_values;
1922 bv1 = m1->sml_values;
1924 for ( j=0; j<m2->sml_numvals; j++ ) {
1925 for ( i=0; i<m1->sml_numvals; i++ ) {
1926 if ( !ber_bvcmp( &bv1[i], &bv2[j] )) {
1928 for ( k=i; k<m1->sml_numvals-1; k++ ) {
1929 m1->sml_values[k] = m1->sml_values[k+1];
1930 if ( m1->sml_nvalues )
1931 m1->sml_nvalues[k] = m1->sml_nvalues[k+1];
1933 BER_BVZERO(&m1->sml_values[k]);
1934 if ( m1->sml_nvalues ) {
1935 BER_BVZERO(&m1->sml_nvalues[k]);
1945 syncrepl_resolve_cb( Operation *op, SlapReply *rs )
1947 if ( rs->sr_type == REP_SEARCH ) {
1948 resolve_ctxt *rx = op->o_callback->sc_private;
1949 Attribute *a = attr_find( rs->sr_entry->e_attrs, ad_reqMod );
1951 Modifications *oldmods, *newmods, *m1, *m2, **prev;
1952 oldmods = rx->rx_mods;
1953 syncrepl_accesslog_mods( rx->rx_si, a->a_vals, &newmods );
1954 for ( m2 = newmods; m2; m2=m2->sml_next ) {
1955 for ( prev = &oldmods, m1 = *prev; m1; m1 = *prev ) {
1956 if ( m1->sml_desc != m2->sml_desc ) {
1957 prev = &m1->sml_next;
1960 if ( m2->sml_op == LDAP_MOD_DELETE ||
1961 m2->sml_op == LDAP_MOD_REPLACE ) {
1962 int numvals = m2->sml_numvals;
1963 if ( m2->sml_op == LDAP_MOD_REPLACE )
1965 /* New delete All cancels everything */
1966 if ( numvals == 0 ) {
1968 *prev = m1->sml_next;
1969 op->o_tmpfree( m1, op->o_tmpmemctx );
1972 if ( m1->sml_op == LDAP_MOD_DELETE ) {
1973 if ( m1->sml_numvals == 0 ) {
1974 /* turn this to SOFTDEL later */
1975 m1->sml_flags = SLAP_MOD_INTERNAL;
1977 compare_vals( m1, m2 );
1978 if ( !m1->sml_numvals )
1981 } else if ( m1->sml_op == LDAP_MOD_ADD ) {
1982 compare_vals( m1, m2 );
1983 if ( !m1->sml_numvals )
1988 if ( m2->sml_op == LDAP_MOD_ADD ||
1989 m2->sml_op == LDAP_MOD_REPLACE ) {
1990 if ( m1->sml_op == LDAP_MOD_DELETE ) {
1991 if ( !m1->sml_numvals ) goto drop;
1992 compare_vals( m1, m2 );
1993 if ( !m1->sml_numvals )
1996 if ( m2->sml_desc->ad_type->sat_atype.at_single_value )
1998 compare_vals( m1, m2 );
1999 if ( !m1->sml_numvals )
2002 prev = &m1->sml_next;
2005 slap_mods_free( newmods, 1 );
2006 rx->rx_mods = oldmods;
2009 return LDAP_SUCCESS;
2012 typedef struct modify_ctxt {
2013 Modifications *mx_orig;
2014 Modifications *mx_free;
2018 syncrepl_modify_cb( Operation *op, SlapReply *rs )
2020 slap_callback *sc = op->o_callback;
2021 modify_ctxt *mx = sc->sc_private;
2024 op->orm_no_opattrs = 0;
2025 op->orm_modlist = mx->mx_orig;
2026 for ( ml = mx->mx_free; ml; ml = mx->mx_free ) {
2027 mx->mx_free = ml->sml_next;
2028 op->o_tmpfree( ml, op->o_tmpmemctx );
2030 op->o_callback = sc->sc_next;
2031 op->o_tmpfree( sc, op->o_tmpmemctx );
2032 return SLAP_CB_CONTINUE;
2036 syncrepl_op_modify( Operation *op, SlapReply *rs )
2038 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2043 Modifications *mod, *newlist;
2045 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
2046 if ( oex->oe_key == (void *)syncrepl_message_to_op )
2050 return SLAP_CB_CONTINUE;
2052 si = ((OpExtraSync *)oex)->oe_si;
2054 /* Check if entryCSN in modlist is newer than entryCSN in entry.
2055 * We do it here because the op has been serialized by accesslog
2056 * by the time we get here. If the CSN is new enough, just do the
2057 * mod. If not, we need to resolve conflicts.
2060 for ( mod = op->orm_modlist; mod; mod=mod->sml_next ) {
2061 if ( mod->sml_desc == slap_schema.si_ad_entryCSN ) break;
2063 /* FIXME: what should we do if entryCSN is missing from the mod? */
2065 return SLAP_CB_CONTINUE;
2067 rc = overlay_entry_get_ov( op, &op->o_req_ndn, NULL, NULL, 0, &e, on );
2071 a = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
2072 value_match( &match, slap_schema.si_ad_entryCSN,
2073 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
2074 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2075 &mod->sml_nvalues[0], &a->a_nvals[0], &text );
2076 overlay_entry_release_ov( op, e, 0, on );
2078 /* equal? Should never happen */
2080 /* tell accesslog this was a failure */
2081 rs->sr_err = LDAP_TYPE_OR_VALUE_EXISTS;
2082 return LDAP_SUCCESS;
2085 /* mod is older: resolve conflicts...
2086 * 1. Save/copy original modlist. Split Replace to Del/Add.
2087 * 2. Find all mods to this reqDN newer than the mod stamp.
2088 * 3. Resolve any mods in this request that affect attributes
2089 * touched by newer mods.
2091 * delete all delete all drop
2092 * delete all delete X SOFTDEL
2093 * delete X delete all drop
2094 * delete X delete X drop
2095 * delete X delete Y OK
2096 * delete all add X drop
2097 * delete X add X drop
2099 * add X delete all drop
2100 * add X delete X drop
2102 * add X add Y if SV, drop else OK
2104 * 4. Swap original modlist back in response callback so
2105 * that accesslog logs the original mod.
2107 * Even if the mod is newer, other out-of-order changes may
2108 * have been committed, forcing us to tweak the modlist:
2109 * 1. Save/copy original modlist.
2110 * 2. Change deletes to soft deletes.
2111 * 3. Change Adds of single-valued attrs to Replace.
2114 newlist = mods_dup( op, op->orm_modlist, match );
2118 Operation op2 = *op;
2119 AttributeName an[2];
2125 SlapReply rs1 = {0};
2127 slap_callback cb = { NULL, syncrepl_resolve_cb, NULL, NULL };
2130 rx.rx_mods = newlist;
2131 cb.sc_private = ℞
2133 op2.o_tag = LDAP_REQ_SEARCH;
2134 op2.ors_scope = LDAP_SCOPE_SUBTREE;
2135 op2.ors_deref = LDAP_DEREF_NEVER;
2136 op2.o_req_dn = si->si_logbase;
2137 op2.o_req_ndn = si->si_logbase;
2138 op2.ors_tlimit = SLAP_NO_LIMIT;
2139 op2.ors_slimit = SLAP_NO_LIMIT;
2140 op2.ors_limit = NULL;
2141 memset( an, 0, sizeof(an));
2142 an[0].an_desc = ad_reqMod;
2143 an[0].an_name = ad_reqMod->ad_cname;
2145 op2.ors_attrsonly = 0;
2147 bv = mod->sml_nvalues[0];
2149 size = sizeof("(&(entryCSN>=)(reqDN=))");
2150 size += bv.bv_len + op->o_req_ndn.bv_len + si->si_logfilterstr.bv_len;
2151 op2.ors_filterstr.bv_val = op->o_tmpalloc( size, op->o_tmpmemctx );
2152 op2.ors_filterstr.bv_len = sprintf(op2.ors_filterstr.bv_val,
2153 "(&(entryCSN>=%s)(reqDN=%s)%s)",
2154 bv.bv_val, op->o_req_ndn.bv_val, si->si_logfilterstr.bv_val );
2155 op2.ors_filter = str2filter_x( op, op2.ors_filterstr.bv_val );
2157 op2.o_callback = &cb;
2158 op2.o_bd = select_backend( &op2.o_req_ndn, 1 );
2159 op2.o_bd->be_search( &op2, &rs1 );
2160 newlist = rx.rx_mods;
2164 slap_callback *sc = op->o_tmpalloc( sizeof(slap_callback) +
2165 sizeof(modify_ctxt), op->o_tmpmemctx );
2166 modify_ctxt *mx = (modify_ctxt *)(sc+1);
2169 sc->sc_response = syncrepl_modify_cb;
2170 sc->sc_private = mx;
2171 sc->sc_next = op->o_callback;
2172 sc->sc_cleanup = NULL;
2173 op->o_callback = sc;
2174 op->orm_no_opattrs = 1;
2175 mx->mx_orig = op->orm_modlist;
2176 mx->mx_free = newlist;
2177 for ( ml = newlist; ml; ml=ml->sml_next ) {
2178 if ( ml->sml_flags == SLAP_MOD_INTERNAL ) {
2180 ml->sml_op = SLAP_MOD_SOFTDEL;
2182 else if ( ml->sml_op == LDAP_MOD_DELETE )
2183 ml->sml_op = SLAP_MOD_SOFTDEL;
2184 else if ( ml->sml_op == LDAP_MOD_ADD &&
2185 ml->sml_desc->ad_type->sat_atype.at_single_value )
2186 ml->sml_op = LDAP_MOD_REPLACE;
2188 op->orm_modlist = newlist;
2189 op->o_csn = mod->sml_nvalues[0];
2191 return SLAP_CB_CONTINUE;
2195 syncrepl_message_to_op(
2201 BerElement *ber = NULL;
2202 Modifications *modlist = NULL;
2204 SlapReply rs = { REP_RESULT };
2205 slap_callback cb = { NULL, null_callback, NULL, NULL };
2208 char txtbuf[SLAP_TEXT_BUFLEN];
2209 size_t textlen = sizeof txtbuf;
2211 struct berval bdn, dn = BER_BVNULL, ndn;
2212 struct berval bv, bv2, *bvals = NULL;
2213 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
2214 prdn = BER_BVNULL, nrdn = BER_BVNULL,
2215 psup = BER_BVNULL, nsup = BER_BVNULL;
2216 int rc, deleteOldRdn = 0, freeReqDn = 0;
2217 int do_graduate = 0;
2219 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
2220 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
2221 "Message type should be entry (%d)",
2222 si->si_ridtxt, ldap_msgtype( msg ), 0 );
2226 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
2231 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
2233 if ( rc != LDAP_SUCCESS ) {
2234 Debug( LDAP_DEBUG_ANY,
2235 "syncrepl_message_to_op: %s dn get failed (%d)",
2236 si->si_ridtxt, rc, 0 );
2240 op->o_tag = LBER_DEFAULT;
2241 op->o_bd = si->si_wbe;
2243 if ( BER_BVISEMPTY( &bdn )) {
2244 Debug( LDAP_DEBUG_ANY,
2245 "syncrepl_message_to_op: %s got empty dn",
2246 si->si_ridtxt, 0, 0 );
2250 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ) )
2252 if ( bv.bv_val == NULL )
2255 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn ) ) {
2257 REWRITE_DN( si, bdn, bv2, dn, ndn );
2258 if ( rc != LDAP_SUCCESS ) {
2259 Debug( LDAP_DEBUG_ANY,
2260 "syncrepl_message_to_op: %s "
2261 "dn \"%s\" normalization failed (%d)",
2262 si->si_ridtxt, bdn.bv_val, rc );
2267 ber_dupbv( &op->o_req_dn, &dn );
2268 ber_dupbv( &op->o_req_ndn, &ndn );
2269 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2270 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
2272 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req ) ) {
2273 int i = verb_to_mask( bvals[0].bv_val, modops );
2275 Debug( LDAP_DEBUG_ANY,
2276 "syncrepl_message_to_op: %s unknown op %s",
2277 si->si_ridtxt, bvals[0].bv_val, 0 );
2282 op->o_tag = modops[i].mask;
2283 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod ) ) {
2284 /* Parse attribute into modlist */
2285 if ( si->si_syncdata == SYNCDATA_ACCESSLOG ) {
2286 rc = syncrepl_accesslog_mods( si, bvals, &modlist );
2288 rc = syncrepl_changelog_mods( si, bvals, &modlist );
2290 if ( rc ) goto done;
2291 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn ) ) {
2293 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn ) ) {
2294 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ) ) {
2297 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup ) ) {
2299 } else if ( !ber_bvstrcasecmp( &bv,
2300 &slap_schema.si_ad_entryCSN->ad_cname ) )
2302 slap_queue_csn( op, bvals );
2308 /* If we didn't get a mod type or a target DN, bail out */
2309 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn ) ) {
2314 op->o_callback = &cb;
2315 slap_op_time( &op->o_time, &op->o_tincr );
2317 switch( op->o_tag ) {
2319 case LDAP_REQ_MODIFY:
2320 /* If we didn't get required data, bail */
2321 if ( !modlist ) goto done;
2323 rc = slap_mods_check( op, modlist, &text, txtbuf, textlen, NULL );
2325 if ( rc != LDAP_SUCCESS ) {
2326 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
2327 "mods check (%s)\n",
2328 si->si_ridtxt, text, 0 );
2332 if ( op->o_tag == LDAP_REQ_ADD ) {
2333 Entry *e = entry_alloc();
2335 op->ora_e->e_name = op->o_req_dn;
2336 op->ora_e->e_nname = op->o_req_ndn;
2338 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
2339 if( rc != LDAP_SUCCESS ) {
2340 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
2341 "mods2entry (%s)\n",
2342 si->si_ridtxt, text, 0 );
2344 rc = op->o_bd->be_add( op, &rs );
2345 Debug( LDAP_DEBUG_SYNC,
2346 "syncrepl_message_to_op: %s be_add %s (%d)\n",
2347 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2350 if ( e == op->ora_e )
2351 be_entry_release_w( op, op->ora_e );
2354 op->orm_modlist = modlist;
2355 op->o_bd = si->si_wbe;
2356 /* delta-mmr needs additional checks in syncrepl_op_modify */
2357 if ( SLAP_MULTIMASTER( op->o_bd )) {
2358 oes.oe.oe_key = (void *)syncrepl_message_to_op;
2360 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &oes.oe, oe_next );
2362 rc = op->o_bd->be_modify( op, &rs );
2363 if ( SLAP_MULTIMASTER( op->o_bd )) {
2364 LDAP_SLIST_REMOVE( &op->o_extra, &oes.oe, OpExtra, oe_next );
2365 BER_BVZERO( &op->o_csn );
2367 modlist = op->orm_modlist;
2368 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
2369 "syncrepl_message_to_op: %s be_modify %s (%d)\n",
2370 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2371 op->o_bd = si->si_be;
2375 case LDAP_REQ_MODRDN:
2376 if ( BER_BVISNULL( &rdn ) ) goto done;
2378 if ( rdnPretty( NULL, &rdn, &prdn, NULL ) ) {
2381 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ) ) {
2384 if ( !BER_BVISNULL( &sup ) ) {
2385 REWRITE_DN( si, sup, bv2, psup, nsup );
2388 op->orr_newSup = &psup;
2389 op->orr_nnewSup = ⊅
2391 op->orr_newSup = NULL;
2392 op->orr_nnewSup = NULL;
2394 op->orr_newrdn = prdn;
2395 op->orr_nnewrdn = nrdn;
2396 op->orr_deleteoldrdn = deleteOldRdn;
2397 op->orr_modlist = NULL;
2398 if ( slap_modrdn2mods( op, &rs ) ) {
2402 /* Append modlist for operational attrs */
2406 for ( m = op->orr_modlist; m->sml_next; m = m->sml_next )
2408 m->sml_next = modlist;
2411 rc = op->o_bd->be_modrdn( op, &rs );
2412 slap_mods_free( op->orr_modlist, 1 );
2413 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
2414 "syncrepl_message_to_op: %s be_modrdn %s (%d)\n",
2415 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2418 case LDAP_REQ_DELETE:
2419 rc = op->o_bd->be_delete( op, &rs );
2420 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
2421 "syncrepl_message_to_op: %s be_delete %s (%d)\n",
2422 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2428 slap_graduate_commit_csn( op );
2429 op->o_bd = si->si_be;
2430 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2431 BER_BVZERO( &op->o_csn );
2433 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
2435 if ( !BER_BVISNULL( &rdn ) ) {
2436 if ( !BER_BVISNULL( &nsup ) ) {
2437 ch_free( nsup.bv_val );
2439 if ( !BER_BVISNULL( &psup ) ) {
2440 ch_free( psup.bv_val );
2442 if ( !BER_BVISNULL( &nrdn ) ) {
2443 ch_free( nrdn.bv_val );
2445 if ( !BER_BVISNULL( &prdn ) ) {
2446 ch_free( prdn.bv_val );
2450 ch_free( op->o_req_ndn.bv_val );
2451 ch_free( op->o_req_dn.bv_val );
2458 syncrepl_message_to_entry(
2462 Modifications **modlist,
2465 struct berval *syncUUID
2469 BerElement *ber = NULL;
2472 Modifications **modtail = modlist;
2475 char txtbuf[SLAP_TEXT_BUFLEN];
2476 size_t textlen = sizeof txtbuf;
2478 struct berval bdn = BER_BVNULL, dn, ndn, bv2;
2483 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
2484 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s "
2485 "Message type should be entry (%d)",
2486 si->si_ridtxt, ldap_msgtype( msg ), 0 );
2490 op->o_tag = LDAP_REQ_ADD;
2492 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
2493 if ( rc != LDAP_SUCCESS ) {
2494 Debug( LDAP_DEBUG_ANY,
2495 "syncrepl_message_to_entry: %s dn get failed (%d)",
2496 si->si_ridtxt, rc, 0 );
2500 if ( BER_BVISEMPTY( &bdn ) && !BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) ) {
2501 Debug( LDAP_DEBUG_ANY,
2502 "syncrepl_message_to_entry: %s got empty dn",
2503 si->si_ridtxt, 0, 0 );
2507 /* syncUUID[0] is normalized UUID received over the wire
2508 * syncUUID[1] is denormalized UUID, generated here
2510 (void)slap_uuidstr_from_normalized( &syncUUID[1], &syncUUID[0], op->o_tmpmemctx );
2511 Debug( LDAP_DEBUG_SYNC,
2512 "syncrepl_message_to_entry: %s DN: %s, UUID: %s\n",
2513 si->si_ridtxt, bdn.bv_val, syncUUID[1].bv_val );
2515 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
2516 /* NOTE: this could be done even before decoding the DN,
2517 * although encoding errors wouldn't be detected */
2522 if ( entry == NULL ) {
2526 REWRITE_DN( si, bdn, bv2, dn, ndn );
2527 if ( rc != LDAP_SUCCESS ) {
2528 /* One of the things that could happen is that the schema
2529 * is not lined-up; this could result in unknown attributes.
2530 * A value non conformant to the syntax should be unlikely,
2531 * except when replicating between different versions
2532 * of the software, or when syntax validation bugs are fixed
2534 Debug( LDAP_DEBUG_ANY,
2535 "syncrepl_message_to_entry: "
2536 "%s dn \"%s\" normalization failed (%d)",
2537 si->si_ridtxt, bdn.bv_val, rc );
2541 ber_dupbv( &op->o_req_dn, &dn );
2542 ber_dupbv( &op->o_req_ndn, &ndn );
2543 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2544 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
2546 is_ctx = dn_match( &op->o_req_ndn, &op->o_bd->be_nsuffix[0] );
2549 e->e_name = op->o_req_dn;
2550 e->e_nname = op->o_req_ndn;
2552 while ( ber_remaining( ber ) ) {
2553 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
2554 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
2559 /* Drop all updates to the contextCSN of the context entry
2562 if ( is_ctx && !strcasecmp( tmp.sml_type.bv_val,
2563 slap_schema.si_ad_contextCSN->ad_cname.bv_val )) {
2564 ber_bvarray_free( tmp.sml_values );
2568 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
2570 mod->sml_op = LDAP_MOD_REPLACE;
2572 mod->sml_next = NULL;
2573 mod->sml_desc = NULL;
2574 mod->sml_type = tmp.sml_type;
2575 mod->sml_values = tmp.sml_values;
2576 mod->sml_nvalues = NULL;
2577 mod->sml_numvals = 0; /* slap_mods_check will set this */
2579 #ifdef ENABLE_REWRITE
2580 if (si->si_rewrite) {
2581 AttributeDescription *ad = NULL;
2582 slap_bv2ad( &tmp.sml_type, &ad, &text );
2585 mod->sml_type = ad->ad_cname;
2586 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) {
2588 for ( i = 0; tmp.sml_values[i].bv_val; i++ ) {
2589 syncrepl_rewrite_dn( si, &tmp.sml_values[i], &bv2);
2590 if ( !BER_BVISNULL( &bv2 )) {
2591 ber_memfree( tmp.sml_values[i].bv_val );
2592 tmp.sml_values[i] = bv2;
2600 modtail = &mod->sml_next;
2603 if ( *modlist == NULL ) {
2604 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s no attributes\n",
2605 si->si_ridtxt, 0, 0 );
2610 rc = slap_mods_check( op, *modlist, &text, txtbuf, textlen, NULL );
2612 if ( rc != LDAP_SUCCESS ) {
2613 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods check (%s)\n",
2614 si->si_ridtxt, text, 0 );
2618 /* Strip out dynamically generated attrs */
2619 for ( modtail = modlist; *modtail ; ) {
2621 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
2622 *modtail = mod->sml_next;
2623 slap_mod_free( &mod->sml_mod, 0 );
2626 modtail = &mod->sml_next;
2630 /* Strip out attrs in exattrs list */
2631 for ( modtail = modlist; *modtail ; ) {
2633 if ( ldap_charray_inlist( si->si_exattrs,
2634 mod->sml_desc->ad_type->sat_cname.bv_val ) )
2636 *modtail = mod->sml_next;
2637 slap_mod_free( &mod->sml_mod, 0 );
2640 modtail = &mod->sml_next;
2644 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
2645 if( rc != LDAP_SUCCESS ) {
2646 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods2entry (%s)\n",
2647 si->si_ridtxt, text, 0 );
2652 if ( rc != LDAP_SUCCESS ) {
2664 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
2666 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
2667 * entry if a previous refresh was interrupted before sending us a new
2668 * context state. We try to compare the new entry to the existing entry
2669 * and ignore the new entry if they are the same.
2671 * Also, we may get an update where the entryDN has changed, due to
2672 * a ModDn on the provider. We detect this as well, so we can issue
2673 * the corresponding operation locally.
2675 * In the case of a modify, we get a list of all the attributes
2676 * in the original entry. Rather than deleting the entry and re-adding it,
2677 * we issue a Modify request that deletes all the attributes and adds all
2678 * the new ones. This avoids the issue of trying to delete/add a non-leaf
2681 * We otherwise distinguish ModDN from Modify; in the case of
2682 * a ModDN we just use the CSN, modifyTimestamp and modifiersName
2683 * operational attributes from the entry, and do a regular ModDN.
2685 typedef struct dninfo {
2689 struct berval nnewSup;
2690 int renamed; /* Was an existing entry renamed? */
2691 int delOldRDN; /* Was old RDN deleted? */
2692 Modifications **modlist; /* the modlist we received */
2693 Modifications *mods; /* the modlist we compared */
2694 int oldNcount; /* #values of old naming attr */
2695 AttributeDescription *oldDesc; /* for renames */
2696 AttributeDescription *newDesc; /* for renames */
2701 /* return 1 if inserted, 0 otherwise */
2705 struct berval *syncUUID )
2713 if ( !si->si_presentlist )
2714 si->si_presentlist = ch_calloc(65536, sizeof( Avlnode * ));
2716 av = (Avlnode **)si->si_presentlist;
2718 val = ch_malloc(UUIDLEN-2);
2719 memcpy(&s, syncUUID->bv_val, 2);
2720 memcpy(val, syncUUID->bv_val+2, UUIDLEN-2);
2722 if ( avl_insert( &av[s], val,
2723 syncuuid_cmp, avl_dup_error ) )
2729 val = ch_malloc(UUIDLEN);
2731 AC_MEMCPY( val, syncUUID->bv_val, UUIDLEN );
2733 if ( avl_insert( &si->si_presentlist, val,
2734 syncuuid_cmp, avl_dup_error ) )
2747 struct berval *val )
2750 Avlnode **a2 = (Avlnode **)av;
2753 memcpy(&s, val->bv_val, 2);
2754 return avl_find( a2[s], val->bv_val+2, syncuuid_cmp );
2756 return avl_find( av, val->bv_val, syncuuid_cmp );
2761 presentlist_free( Avlnode *av )
2764 Avlnode **a2 = (Avlnode **)av;
2767 for (i=0; i<65536; i++) {
2769 count += avl_free( a2[i], ch_free );
2774 return avl_free( av, ch_free );
2781 struct berval *val )
2784 Avlnode **a2 = *(Avlnode ***)av;
2787 memcpy(&s, val->bv_val, 2);
2788 avl_delete( &a2[s], val->bv_val+2, syncuuid_cmp );
2790 avl_delete( av, val->bv_val, syncuuid_cmp );
2799 Modifications** modlist,
2801 struct berval* syncUUID,
2802 struct berval* syncCSN )
2804 Backend *be = op->o_bd;
2805 slap_callback cb = { NULL, NULL, NULL, NULL };
2806 int syncuuid_inserted = 0;
2808 SlapReply rs_search = {REP_RESULT};
2810 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2811 int rc = LDAP_SUCCESS;
2813 struct berval pdn = BER_BVNULL;
2818 Debug( LDAP_DEBUG_SYNC,
2819 "syncrepl_entry: %s LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_%s)\n",
2820 si->si_ridtxt, syncrepl_state2str( syncstate ), 0 );
2822 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ) ) {
2823 if ( !si->si_refreshPresent && !si->si_refreshDone ) {
2824 syncuuid_inserted = presentlist_insert( si, syncUUID );
2828 if ( syncstate == LDAP_SYNC_PRESENT ) {
2830 } else if ( syncstate != LDAP_SYNC_DELETE ) {
2831 if ( entry == NULL ) {
2836 if ( syncstate != LDAP_SYNC_DELETE ) {
2837 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
2840 /* add if missing */
2841 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
2842 &syncUUID[1], syncUUID );
2844 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
2845 /* replace only if necessary */
2846 if ( a->a_nvals != a->a_vals ) {
2847 ber_memfree( a->a_nvals[0].bv_val );
2848 ber_dupbv( &a->a_nvals[0], syncUUID );
2850 ber_memfree( a->a_vals[0].bv_val );
2851 ber_dupbv( &a->a_vals[0], &syncUUID[1] );
2855 f.f_choice = LDAP_FILTER_EQUALITY;
2857 ava.aa_desc = slap_schema.si_ad_entryUUID;
2858 ava.aa_value = *syncUUID;
2860 if ( syncuuid_inserted ) {
2861 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s inserted UUID %s\n",
2862 si->si_ridtxt, syncUUID[1].bv_val, 0 );
2864 op->ors_filter = &f;
2866 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID[1].bv_len;
2867 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
2868 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
2869 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
2870 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
2871 syncUUID[1].bv_val, syncUUID[1].bv_len );
2872 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
2873 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
2875 op->o_tag = LDAP_REQ_SEARCH;
2876 op->ors_scope = LDAP_SCOPE_SUBTREE;
2877 op->ors_deref = LDAP_DEREF_NEVER;
2879 /* get the entry for this UUID */
2880 #ifdef ENABLE_REWRITE
2881 if ( si->si_rewrite ) {
2882 op->o_req_dn = si->si_suffixm;
2883 op->o_req_ndn = si->si_suffixm;
2887 op->o_req_dn = si->si_base;
2888 op->o_req_ndn = si->si_base;
2891 op->o_time = slap_get_time();
2892 op->ors_tlimit = SLAP_NO_LIMIT;
2894 op->ors_limit = NULL;
2896 op->ors_attrs = slap_anlist_all_attributes;
2897 op->ors_attrsonly = 0;
2899 /* set callback function */
2900 op->o_callback = &cb;
2901 cb.sc_response = dn_callback;
2902 cb.sc_private = &dni;
2903 dni.new_entry = entry;
2904 dni.modlist = modlist;
2906 rc = be->be_search( op, &rs_search );
2907 Debug( LDAP_DEBUG_SYNC,
2908 "syncrepl_entry: %s be_search (%d)\n",
2909 si->si_ridtxt, rc, 0 );
2911 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
2912 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2915 cb.sc_response = null_callback;
2918 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
2919 Debug( LDAP_DEBUG_SYNC,
2920 "syncrepl_entry: %s %s\n",
2921 si->si_ridtxt, entry->e_name.bv_val, 0 );
2923 Debug( LDAP_DEBUG_SYNC,
2924 "syncrepl_entry: %s %s\n",
2925 si->si_ridtxt, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
2928 assert( BER_BVISNULL( &op->o_csn ) );
2930 slap_queue_csn( op, syncCSN );
2933 slap_op_time( &op->o_time, &op->o_tincr );
2934 switch ( syncstate ) {
2936 case LDAP_SYNC_MODIFY:
2937 if ( BER_BVISNULL( &op->o_csn ))
2940 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
2942 /* FIXME: op->o_csn is assumed to be
2943 * on the thread's slab; this needs
2944 * to be cleared ASAP.
2946 op->o_csn = a->a_vals[0];
2951 if ( BER_BVISNULL( &dni.dn ) ) {
2952 SlapReply rs_add = {REP_RESULT};
2954 op->o_req_dn = entry->e_name;
2955 op->o_req_ndn = entry->e_nname;
2956 op->o_tag = LDAP_REQ_ADD;
2958 op->o_bd = si->si_wbe;
2960 rc = op->o_bd->be_add( op, &rs_add );
2961 Debug( LDAP_DEBUG_SYNC,
2962 "syncrepl_entry: %s be_add %s (%d)\n",
2963 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2964 switch ( rs_add.sr_err ) {
2966 if ( op->ora_e == entry ) {
2967 be_entry_release_w( op, entry );
2973 /* we assume that LDAP_NO_SUCH_OBJECT is returned
2974 * only if the suffix entry is not present.
2975 * This should not happen during Persist phase.
2977 case LDAP_NO_SUCH_OBJECT:
2978 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST &&
2979 si->si_refreshDone ) {
2980 /* Something's wrong, start over */
2981 ber_bvarray_free( si->si_syncCookie.ctxcsn );
2982 si->si_syncCookie.ctxcsn = NULL;
2983 entry_free( entry );
2984 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
2985 ber_bvarray_free( si->si_cookieState->cs_vals );
2986 ch_free( si->si_cookieState->cs_sids );
2987 si->si_cookieState->cs_vals = NULL;
2988 si->si_cookieState->cs_sids = 0;
2989 si->si_cookieState->cs_num = 0;
2990 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
2991 return LDAP_NO_SUCH_OBJECT;
2993 rc = syncrepl_add_glue( op, entry );
2997 /* if an entry was added via syncrepl_add_glue(),
2998 * it likely has no entryUUID, so the previous
2999 * be_search() doesn't find it. In this case,
3000 * give syncrepl a chance to modify it. Also
3001 * allow for entries that were recreated with the
3002 * same DN but a different entryUUID.
3004 case LDAP_ALREADY_EXISTS:
3006 Operation op2 = *op;
3007 SlapReply rs2 = { REP_RESULT };
3008 slap_callback cb2 = { 0 };
3011 op2.o_tag = LDAP_REQ_SEARCH;
3012 op2.o_req_dn = entry->e_name;
3013 op2.o_req_ndn = entry->e_nname;
3014 op2.ors_scope = LDAP_SCOPE_BASE;
3015 op2.ors_deref = LDAP_DEREF_NEVER;
3016 op2.ors_attrs = slap_anlist_all_attributes;
3017 op2.ors_attrsonly = 0;
3018 op2.ors_limit = NULL;
3020 op2.ors_tlimit = SLAP_NO_LIMIT;
3022 f.f_choice = LDAP_FILTER_PRESENT;
3023 f.f_desc = slap_schema.si_ad_objectClass;
3024 op2.ors_filter = &f;
3025 op2.ors_filterstr = generic_filterstr;
3027 op2.o_callback = &cb2;
3028 cb2.sc_response = dn_callback;
3029 cb2.sc_private = &dni;
3031 rc = be->be_search( &op2, &rs2 );
3032 if ( rc ) goto done;
3035 slap_op_time( &op->o_time, &op->o_tincr );
3041 Debug( LDAP_DEBUG_ANY,
3042 "syncrepl_entry: %s be_add %s failed (%d)\n",
3043 si->si_ridtxt, op->o_req_dn.bv_val, rs_add.sr_err );
3051 op->o_req_dn = dni.dn;
3052 op->o_req_ndn = dni.ndn;
3053 if ( dni.renamed ) {
3054 struct berval noldp, newp;
3055 Modifications *mod, **modtail, **ml, *m2;
3056 int i, got_replace = 0, just_rename = 0;
3057 SlapReply rs_modify = {REP_RESULT};
3059 op->o_tag = LDAP_REQ_MODRDN;
3060 dnRdn( &entry->e_name, &op->orr_newrdn );
3061 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
3063 if ( !BER_BVISNULL( &dni.nnewSup )) {
3064 dnParent( &entry->e_name, &newp );
3065 op->orr_newSup = &newp;
3066 op->orr_nnewSup = &dni.nnewSup;
3068 op->orr_newSup = NULL;
3069 op->orr_nnewSup = NULL;
3071 op->orr_deleteoldrdn = dni.delOldRDN;
3072 op->orr_modlist = NULL;
3073 if ( ( rc = slap_modrdn2mods( op, &rs_modify ) ) ) {
3077 /* Drop the RDN-related mods from this op, because their
3078 * equivalents were just setup by slap_modrdn2mods.
3080 * If delOldRDN is TRUE then we should see a delete modop
3081 * for oldDesc. We might see a replace instead.
3082 * delete with no values: therefore newDesc != oldDesc.
3083 * if oldNcount == 1, then Drop this op.
3084 * delete with 1 value: can only be the oldRDN value. Drop op.
3085 * delete with N values: Drop oldRDN value, keep remainder.
3086 * replace with 1 value: if oldNcount == 1 and
3087 * newDesc == oldDesc, Drop this op.
3088 * Any other cases must be left intact.
3090 * We should also see an add modop for newDesc. (But not if
3091 * we got a replace modop due to delOldRDN.) If it has
3092 * multiple values, we'll have to drop the new RDN value.
3094 modtail = &op->orr_modlist;
3095 if ( dni.delOldRDN ) {
3096 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
3097 if ( (*ml)->sml_desc == dni.oldDesc ) {
3099 if ( mod->sml_op == LDAP_MOD_REPLACE &&
3100 dni.oldDesc != dni.newDesc ) {
3101 /* This Replace is due to other Mods.
3106 if ( mod->sml_numvals <= 1 &&
3107 dni.oldNcount == 1 &&
3108 ( mod->sml_op == LDAP_MOD_DELETE ||
3109 mod->sml_op == LDAP_MOD_REPLACE )) {
3110 if ( mod->sml_op == LDAP_MOD_REPLACE )
3113 *ml = mod->sml_next;
3114 mod->sml_next = NULL;
3115 slap_mods_free( mod, 1 );
3118 if ( mod->sml_op != LDAP_MOD_DELETE || mod->sml_numvals == 0 )
3120 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
3121 if ( m2->sml_desc == dni.oldDesc &&
3122 m2->sml_op == LDAP_MOD_DELETE ) break;
3124 for ( i=0; i<mod->sml_numvals; i++ ) {
3125 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
3127 ch_free( mod->sml_values[i].bv_val );
3128 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
3129 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
3130 if ( mod->sml_nvalues ) {
3131 ch_free( mod->sml_nvalues[i].bv_val );
3132 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
3133 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
3138 if ( !mod->sml_numvals ) {
3140 *ml = mod->sml_next;
3141 mod->sml_next = NULL;
3142 slap_mods_free( mod, 1 );
3148 if ( !got_replace ) {
3149 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
3150 if ( (*ml)->sml_desc == dni.newDesc ) {
3152 if ( mod->sml_op != LDAP_MOD_ADD )
3154 if ( mod->sml_numvals == 1 ) {
3156 *ml = mod->sml_next;
3157 mod->sml_next = NULL;
3158 slap_mods_free( mod, 1 );
3161 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
3162 if ( m2->sml_desc == dni.oldDesc &&
3163 m2->sml_op == SLAP_MOD_SOFTADD ) break;
3165 for ( i=0; i<mod->sml_numvals; i++ ) {
3166 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
3168 ch_free( mod->sml_values[i].bv_val );
3169 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
3170 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
3171 if ( mod->sml_nvalues ) {
3172 ch_free( mod->sml_nvalues[i].bv_val );
3173 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
3174 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
3184 /* RDNs must be NUL-terminated for back-ldap */
3185 noldp = op->orr_newrdn;
3186 ber_dupbv_x( &op->orr_newrdn, &noldp, op->o_tmpmemctx );
3187 noldp = op->orr_nnewrdn;
3188 ber_dupbv_x( &op->orr_nnewrdn, &noldp, op->o_tmpmemctx );
3190 /* Setup opattrs too */
3192 static AttributeDescription *nullattr = NULL;
3193 static AttributeDescription **const opattrs[] = {
3194 &slap_schema.si_ad_entryCSN,
3195 &slap_schema.si_ad_modifiersName,
3196 &slap_schema.si_ad_modifyTimestamp,
3199 AttributeDescription *opattr;
3203 /* pull mod off incoming modlist */
3204 for ( i = 0; (opattr = *opattrs[i]) != NULL; i++ ) {
3205 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next )
3207 if ( (*ml)->sml_desc == opattr ) {
3209 *ml = mod->sml_next;
3210 mod->sml_next = NULL;
3212 modtail = &mod->sml_next;
3217 /* If there are still Modifications left, put the opattrs
3218 * back, and let be_modify run. Otherwise, append the opattrs
3219 * to the orr_modlist.
3223 /* don't set a CSN for the rename op */
3225 slap_graduate_commit_csn( op );
3227 mod = op->orr_modlist;
3230 for ( ; mod->sml_next; mod=mod->sml_next );
3233 op->o_bd = si->si_wbe;
3235 rs_reinit( &rs_modify, REP_RESULT );
3236 rc = op->o_bd->be_modrdn( op, &rs_modify );
3238 /* NOTE: noSuchObject should result because the new superior
3239 * has not been added yet (ITS#6472) */
3240 if ( rc == LDAP_NO_SUCH_OBJECT && op->orr_nnewSup != NULL ) {
3241 Operation op2 = *op;
3242 rc = syncrepl_add_glue_ancestors( &op2, entry );
3243 if ( rc == LDAP_SUCCESS ) {
3248 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
3249 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
3251 slap_mods_free( op->orr_modlist, 1 );
3252 Debug( LDAP_DEBUG_SYNC,
3253 "syncrepl_entry: %s be_modrdn %s (%d)\n",
3254 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3256 /* Renamed entries may still have other mods so just fallthru */
3257 op->o_req_dn = entry->e_name;
3258 op->o_req_ndn = entry->e_nname;
3259 /* Use CSN on the modify */
3263 slap_queue_csn( op, syncCSN );
3266 SlapReply rs_modify = {REP_RESULT};
3268 op->o_tag = LDAP_REQ_MODIFY;
3269 op->orm_modlist = dni.mods;
3270 op->orm_no_opattrs = 1;
3271 op->o_bd = si->si_wbe;
3273 rc = op->o_bd->be_modify( op, &rs_modify );
3274 slap_mods_free( op->orm_modlist, 1 );
3275 op->orm_no_opattrs = 0;
3276 Debug( LDAP_DEBUG_SYNC,
3277 "syncrepl_entry: %s be_modify %s (%d)\n",
3278 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3279 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
3280 Debug( LDAP_DEBUG_ANY,
3281 "syncrepl_entry: %s be_modify failed (%d)\n",
3282 si->si_ridtxt, rs_modify.sr_err, 0 );
3286 } else if ( !dni.renamed ) {
3287 Debug( LDAP_DEBUG_SYNC,
3288 "syncrepl_entry: %s entry unchanged, ignored (%s)\n",
3289 si->si_ridtxt, op->o_req_dn.bv_val, 0 );
3291 slap_graduate_commit_csn( op );
3296 case LDAP_SYNC_DELETE :
3297 if ( !BER_BVISNULL( &dni.dn ) ) {
3298 SlapReply rs_delete = {REP_RESULT};
3299 op->o_req_dn = dni.dn;
3300 op->o_req_ndn = dni.ndn;
3301 op->o_tag = LDAP_REQ_DELETE;
3302 op->o_bd = si->si_wbe;
3304 slap_queue_csn( op, si->si_syncCookie.ctxcsn );
3306 rc = op->o_bd->be_delete( op, &rs_delete );
3307 Debug( LDAP_DEBUG_SYNC,
3308 "syncrepl_entry: %s be_delete %s (%d)\n",
3309 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3310 if ( rc == LDAP_NO_SUCH_OBJECT )
3313 while ( rs_delete.sr_err == LDAP_SUCCESS
3314 && op->o_delete_glue_parent ) {
3315 op->o_delete_glue_parent = 0;
3316 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
3317 slap_callback cb = { NULL };
3318 cb.sc_response = slap_null_cb;
3319 dnParent( &op->o_req_ndn, &pdn );
3321 op->o_req_ndn = pdn;
3322 op->o_callback = &cb;
3323 rs_reinit( &rs_delete, REP_RESULT );
3324 op->o_bd->be_delete( op, &rs_delete );
3335 Debug( LDAP_DEBUG_ANY,
3336 "syncrepl_entry: %s unknown syncstate\n", si->si_ridtxt, 0, 0 );
3341 slap_sl_free( syncUUID[1].bv_val, op->o_tmpmemctx );
3342 BER_BVZERO( &syncUUID[1] );
3343 if ( !BER_BVISNULL( &dni.ndn ) ) {
3344 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
3346 if ( !BER_BVISNULL( &dni.dn ) ) {
3347 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
3350 entry_free( entry );
3353 slap_graduate_commit_csn( op );
3355 if ( !BER_BVISNULL( &op->o_csn ) && freecsn ) {
3356 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3358 BER_BVZERO( &op->o_csn );
3362 static struct berval gcbva[] = {
3368 #define NP_DELETE_ONE 2
3371 syncrepl_del_nonpresent(
3375 struct sync_cookie *sc,
3378 Backend* be = op->o_bd;
3379 slap_callback cb = { NULL };
3380 struct nonpresent_entry *np_list, *np_prev;
3382 AttributeName an[2];
3384 struct berval pdn = BER_BVNULL;
3387 #ifdef ENABLE_REWRITE
3388 if ( si->si_rewrite ) {
3389 op->o_req_dn = si->si_suffixm;
3390 op->o_req_ndn = si->si_suffixm;
3394 op->o_req_dn = si->si_base;
3395 op->o_req_ndn = si->si_base;
3398 cb.sc_response = nonpresent_callback;
3401 op->o_callback = &cb;
3402 op->o_tag = LDAP_REQ_SEARCH;
3403 op->ors_scope = si->si_scope;
3404 op->ors_deref = LDAP_DEREF_NEVER;
3405 op->o_time = slap_get_time();
3406 op->ors_tlimit = SLAP_NO_LIMIT;
3411 AttributeAssertion eq = ATTRIBUTEASSERTION_INIT;
3414 op->ors_attrsonly = 1;
3415 op->ors_attrs = slap_anlist_no_attrs;
3416 op->ors_limit = NULL;
3417 op->ors_filter = &uf;
3420 uf.f_av_desc = slap_schema.si_ad_entryUUID;
3422 uf.f_choice = LDAP_FILTER_EQUALITY;
3423 si->si_refreshDelete |= NP_DELETE_ONE;
3425 for (i=0; uuids[i].bv_val; i++) {
3426 SlapReply rs_search = {REP_RESULT};
3429 uf.f_av_value = uuids[i];
3430 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
3431 rc = be->be_search( op, &rs_search );
3432 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
3434 si->si_refreshDelete ^= NP_DELETE_ONE;
3438 AttributeAssertion mmaa;
3439 SlapReply rs_search = {REP_RESULT};
3441 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
3442 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
3443 an[0].an_desc = slap_schema.si_ad_entryUUID;
3445 op->ors_slimit = SLAP_NO_LIMIT;
3446 op->ors_tlimit = SLAP_NO_LIMIT;
3447 op->ors_limit = NULL;
3448 op->ors_attrsonly = 0;
3449 op->ors_filter = filter_dup( si->si_filter, op->o_tmpmemctx );
3450 /* In multimaster, updates can continue to arrive while
3451 * we're searching. Limit the search result to entries
3452 * older than our newest cookie CSN.
3454 if ( SLAP_MULTIMASTER( op->o_bd )) {
3459 f->f_choice = LDAP_FILTER_AND;
3460 f->f_next = op->ors_filter;
3464 f->f_choice = LDAP_FILTER_LE;
3466 f->f_av_desc = slap_schema.si_ad_entryCSN;
3468 BER_BVZERO( &f->f_av_value );
3469 for ( i=0; i<sc->numcsns; i++ ) {
3470 if ( ber_bvcmp( &sc->ctxcsn[i], &f->f_av_value ) > 0 )
3471 f->f_av_value = sc->ctxcsn[i];
3473 of = op->ors_filter;
3474 op->ors_filter = mmf;
3475 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
3478 op->ors_filterstr = si->si_filterstr;
3480 op->o_nocaching = 1;
3483 rc = be->be_search( op, &rs_search );
3484 if ( SLAP_MULTIMASTER( op->o_bd )) {
3485 op->ors_filter = of;
3487 if ( op->ors_filter ) filter_free_x( op, op->ors_filter, 1 );
3488 if ( op->ors_filterstr.bv_val != si->si_filterstr.bv_val ) {
3489 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
3494 op->o_nocaching = 0;
3496 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
3498 if ( sc->ctxcsn && !BER_BVISNULL( &sc->ctxcsn[m] ) ) {
3499 csn = sc->ctxcsn[m];
3501 csn = si->si_syncCookie.ctxcsn[0];
3504 op->o_bd = si->si_wbe;
3505 slap_queue_csn( op, &csn );
3507 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
3508 while ( np_list != NULL ) {
3509 SlapReply rs_delete = {REP_RESULT};
3511 LDAP_LIST_REMOVE( np_list, npe_link );
3513 np_list = LDAP_LIST_NEXT( np_list, npe_link );
3514 op->o_tag = LDAP_REQ_DELETE;
3515 op->o_callback = &cb;
3516 cb.sc_response = null_callback;
3518 op->o_req_dn = *np_prev->npe_name;
3519 op->o_req_ndn = *np_prev->npe_nname;
3520 rc = op->o_bd->be_delete( op, &rs_delete );
3521 Debug( LDAP_DEBUG_SYNC,
3522 "syncrepl_del_nonpresent: %s be_delete %s (%d)\n",
3523 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3525 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
3526 SlapReply rs_modify = {REP_RESULT};
3527 Modifications mod1, mod2;
3528 mod1.sml_op = LDAP_MOD_REPLACE;
3530 mod1.sml_desc = slap_schema.si_ad_objectClass;
3531 mod1.sml_type = mod1.sml_desc->ad_cname;
3532 mod1.sml_numvals = 2;
3533 mod1.sml_values = &gcbva[0];
3534 mod1.sml_nvalues = NULL;
3535 mod1.sml_next = &mod2;
3537 mod2.sml_op = LDAP_MOD_REPLACE;
3539 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
3540 mod2.sml_type = mod2.sml_desc->ad_cname;
3541 mod2.sml_numvals = 1;
3542 mod2.sml_values = &gcbva[1];
3543 mod2.sml_nvalues = NULL;
3544 mod2.sml_next = NULL;
3546 op->o_tag = LDAP_REQ_MODIFY;
3547 op->orm_modlist = &mod1;
3549 rc = op->o_bd->be_modify( op, &rs_modify );
3550 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
3553 while ( rs_delete.sr_err == LDAP_SUCCESS &&
3554 op->o_delete_glue_parent ) {
3555 op->o_delete_glue_parent = 0;
3556 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
3557 slap_callback cb = { NULL };
3558 cb.sc_response = slap_null_cb;
3559 dnParent( &op->o_req_ndn, &pdn );
3561 op->o_req_ndn = pdn;
3562 op->o_callback = &cb;
3563 rs_reinit( &rs_delete, REP_RESULT );
3564 /* give it a root privil ? */
3565 op->o_bd->be_delete( op, &rs_delete );
3571 op->o_delete_glue_parent = 0;
3573 ber_bvfree( np_prev->npe_name );
3574 ber_bvfree( np_prev->npe_nname );
3577 if ( slapd_shutdown ) {
3582 slap_graduate_commit_csn( op );
3585 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3586 BER_BVZERO( &op->o_csn );
3593 syncrepl_add_glue_ancestors(
3597 Backend *be = op->o_bd;
3598 slap_callback cb = { NULL };
3600 int rc = LDAP_SUCCESS;
3603 struct berval dn = BER_BVNULL;
3604 struct berval ndn = BER_BVNULL;
3606 struct berval ptr, nptr;
3609 op->o_tag = LDAP_REQ_ADD;
3610 op->o_callback = &cb;
3611 cb.sc_response = null_callback;
3612 cb.sc_private = NULL;
3617 /* count RDNs in suffix */
3618 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
3619 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
3621 ptr.bv_len -= comma - ptr.bv_val;
3631 /* Start with BE suffix */
3633 for ( i = 0; i < suffrdns; i++ ) {
3634 comma = ber_bvrchr( &ptr, ',' );
3635 if ( comma != NULL ) {
3636 ptr.bv_len = comma - ptr.bv_val;
3643 if ( !BER_BVISEMPTY( &ptr ) ) {
3644 dn.bv_len -= ptr.bv_len + ( suffrdns != 0 );
3645 dn.bv_val += ptr.bv_len + ( suffrdns != 0 );
3648 /* the normalizedDNs are always the same length, no counting
3652 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
3653 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
3654 ndn.bv_len = be->be_nsuffix[0].bv_len;
3656 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
3662 while ( ndn.bv_val > e->e_nname.bv_val ) {
3663 SlapReply rs_add = {REP_RESULT};
3665 glue = entry_alloc();
3666 ber_dupbv( &glue->e_name, &dn );
3667 ber_dupbv( &glue->e_nname, &ndn );
3669 a = attr_alloc( slap_schema.si_ad_objectClass );
3672 a->a_vals = ch_calloc( 3, sizeof( struct berval ) );
3673 ber_dupbv( &a->a_vals[0], &gcbva[0] );
3674 ber_dupbv( &a->a_vals[1], &gcbva[1] );
3675 ber_dupbv( &a->a_vals[2], &gcbva[2] );
3677 a->a_nvals = a->a_vals;
3679 a->a_next = glue->e_attrs;
3682 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
3685 a->a_vals = ch_calloc( 2, sizeof( struct berval ) );
3686 ber_dupbv( &a->a_vals[0], &gcbva[1] );
3687 ber_dupbv( &a->a_vals[1], &gcbva[2] );
3689 a->a_nvals = a->a_vals;
3691 a->a_next = glue->e_attrs;
3694 op->o_req_dn = glue->e_name;
3695 op->o_req_ndn = glue->e_nname;
3697 rc = be->be_add ( op, &rs_add );
3698 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3699 if ( op->ora_e == glue )
3700 be_entry_release_w( op, glue );
3702 /* incl. ALREADY EXIST */
3704 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
3710 /* Move to next child */
3711 comma = ber_bvrchr( &ptr, ',' );
3712 if ( comma == NULL ) {
3715 ptr.bv_len = comma - ptr.bv_val;
3717 dn.bv_val = ++comma;
3718 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
3720 comma = ber_bvrchr( &nptr, ',' );
3721 assert( comma != NULL );
3722 nptr.bv_len = comma - nptr.bv_val;
3724 ndn.bv_val = ++comma;
3725 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
3736 slap_callback cb = { NULL };
3738 Backend *be = op->o_bd;
3739 SlapReply rs_add = {REP_RESULT};
3741 rc = syncrepl_add_glue_ancestors( op, e );
3744 case LDAP_ALREADY_EXISTS:
3751 op->o_tag = LDAP_REQ_ADD;
3752 op->o_callback = &cb;
3753 cb.sc_response = null_callback;
3754 cb.sc_private = NULL;
3756 op->o_req_dn = e->e_name;
3757 op->o_req_ndn = e->e_nname;
3759 rc = be->be_add ( op, &rs_add );
3760 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3761 if ( op->ora_e == e )
3762 be_entry_release_w( op, e );
3771 syncrepl_updateCookie(
3774 struct sync_cookie *syncCookie )
3776 Backend *be = op->o_bd;
3778 struct berval first = BER_BVNULL;
3779 struct sync_cookie sc;
3781 Syntax *syn = slap_schema.si_ad_contextCSN->ad_type->sat_syntax;
3784 int rc, i, j, changed = 0;
3787 slap_callback cb = { NULL };
3788 SlapReply rs_modify = {REP_RESULT};
3790 mod.sml_op = LDAP_MOD_REPLACE;
3791 mod.sml_desc = slap_schema.si_ad_contextCSN;
3792 mod.sml_type = mod.sml_desc->ad_cname;
3793 mod.sml_flags = SLAP_MOD_INTERNAL;
3794 mod.sml_nvalues = NULL;
3795 mod.sml_next = NULL;
3797 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
3800 for ( i=0; i<syncCookie->numcsns; i++ ) {
3801 assert( !syn->ssyn_validate( syn, syncCookie->ctxcsn+i ));
3803 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3804 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3808 /* clone the cookieState CSNs so we can Replace the whole thing */
3809 sc.numcsns = si->si_cookieState->cs_num;
3811 ber_bvarray_dup_x( &sc.ctxcsn, si->si_cookieState->cs_vals, NULL );
3812 sc.sids = ch_malloc( sc.numcsns * sizeof(int));
3813 for ( i=0; i<sc.numcsns; i++ )
3814 sc.sids[i] = si->si_cookieState->cs_sids[i];
3820 /* find any CSNs in the syncCookie that are newer than the cookieState */
3821 for ( i=0; i<syncCookie->numcsns; i++ ) {
3822 for ( j=0; j<sc.numcsns; j++ ) {
3823 if ( syncCookie->sids[i] < sc.sids[j] )
3825 if ( syncCookie->sids[i] != sc.sids[j] )
3827 len = syncCookie->ctxcsn[i].bv_len;
3828 if ( len > sc.ctxcsn[j].bv_len )
3829 len = sc.ctxcsn[j].bv_len;
3830 if ( memcmp( syncCookie->ctxcsn[i].bv_val,
3831 sc.ctxcsn[j].bv_val, len ) > 0 ) {
3832 ber_bvreplace( &sc.ctxcsn[j], &syncCookie->ctxcsn[i] );
3834 if ( BER_BVISNULL( &first ) ||
3835 memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 ) {
3836 first = syncCookie->ctxcsn[i];
3841 /* there was no match for this SID, it's a new CSN */
3842 if ( j == sc.numcsns ||
3843 syncCookie->sids[i] != sc.sids[j] ) {
3844 slap_insert_csn_sids( &sc, j, syncCookie->sids[i],
3845 &syncCookie->ctxcsn[i] );
3846 if ( BER_BVISNULL( &first ) ||
3847 memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 ) {
3848 first = syncCookie->ctxcsn[i];
3853 /* Should never happen, ITS#5065 */
3854 if ( BER_BVISNULL( &first ) || !changed ) {
3855 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3856 ber_bvarray_free( sc.ctxcsn );
3860 op->o_bd = si->si_wbe;
3861 slap_queue_csn( op, &first );
3863 op->o_tag = LDAP_REQ_MODIFY;
3865 cb.sc_response = null_callback;
3868 op->o_callback = &cb;
3869 op->o_req_dn = si->si_contextdn;
3870 op->o_req_ndn = si->si_contextdn;
3872 /* update contextCSN */
3873 op->o_dont_replicate = 1;
3875 mod.sml_numvals = sc.numcsns;
3876 mod.sml_values = sc.ctxcsn;
3878 op->orm_modlist = &mod;
3879 op->orm_no_opattrs = 1;
3880 rc = op->o_bd->be_modify( op, &rs_modify );
3882 if ( rs_modify.sr_err == LDAP_NO_SUCH_OBJECT &&
3883 SLAP_SYNC_SUBENTRY( op->o_bd )) {
3885 char txtbuf[SLAP_TEXT_BUFLEN];
3886 size_t textlen = sizeof txtbuf;
3887 Entry *e = slap_create_context_csn_entry( op->o_bd, NULL );
3888 rs_reinit( &rs_modify, REP_RESULT );
3889 rc = slap_mods2entry( &mod, &e, 0, 1, &text, txtbuf, textlen);
3891 rc = op->o_bd->be_add( op, &rs_modify );
3892 if ( e == op->ora_e )
3893 be_entry_release_w( op, op->ora_e );
3896 op->orm_no_opattrs = 0;
3897 op->o_dont_replicate = 0;
3899 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
3900 slap_sync_cookie_free( &si->si_syncCookie, 0 );
3901 ber_bvarray_free( si->si_cookieState->cs_vals );
3902 ch_free( si->si_cookieState->cs_sids );
3903 si->si_cookieState->cs_vals = sc.ctxcsn;
3904 si->si_cookieState->cs_sids = sc.sids;
3905 si->si_cookieState->cs_num = sc.numcsns;
3907 /* Don't just dup the provider's cookie, recreate it */
3908 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
3909 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn, si->si_cookieState->cs_vals, NULL );
3910 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num * sizeof(int) );
3911 for ( i=0; i<si->si_cookieState->cs_num; i++ )
3912 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
3914 si->si_cookieState->cs_age++;
3915 si->si_cookieAge = si->si_cookieState->cs_age;
3917 Debug( LDAP_DEBUG_ANY,
3918 "syncrepl_updateCookie: %s be_modify failed (%d)\n",
3919 si->si_ridtxt, rs_modify.sr_err, 0 );
3921 ber_bvarray_free( sc.ctxcsn );
3923 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3926 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3927 BER_BVZERO( &op->o_csn );
3928 if ( mod.sml_next ) slap_mods_free( mod.sml_next, 1 );
3931 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3932 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3939 /* Compare the attribute from the old entry to the one in the new
3940 * entry. The Modifications from the new entry will either be left
3941 * in place, or changed to an Add or Delete as needed.
3944 attr_cmp( Operation *op, Attribute *old, Attribute *new,
3945 Modifications ***mret, Modifications ***mcur )
3948 Modifications *mod, **modtail;
3954 struct berval **adds, **dels;
3955 /* count old and new */
3956 for ( o=0; old->a_vals[o].bv_val; o++ ) ;
3957 for ( n=0; new->a_vals[n].bv_val; n++ ) ;
3959 /* there MUST be both old and new values */
3964 adds = op->o_tmpalloc( sizeof(struct berval *) * n, op->o_tmpmemctx );
3965 dels = op->o_tmpalloc( sizeof(struct berval *) * o, op->o_tmpmemctx );
3967 for ( i=0; i<o; i++ ) dels[i] = &old->a_vals[i];
3968 for ( i=0; i<n; i++ ) adds[i] = &new->a_vals[i];
3972 for ( i=0; i<o; i++ ) {
3973 for ( j=0; j<n; j++ ) {
3976 if ( bvmatch( dels[i], adds[j] ) ) {
3986 /* Don't delete/add an objectClass, always use the replace op.
3987 * Modify would fail if provider has replaced entry with a new,
3988 * and the new explicitly includes a superior of a class that was
3989 * only included implicitly in the old entry. Ref ITS#5517.
3991 * Also use replace op if attr has no equality matching rule.
3994 if ( ( nn || ( no > 0 && no < o ) ) &&
3995 ( old->a_desc == slap_schema.si_ad_objectClass ||
3996 !old->a_desc->ad_type->sat_equality ) )
4002 /* all old values were deleted, just use the replace op */
4006 /* delete some values */
4007 mod = ch_malloc( sizeof( Modifications ) );
4008 mod->sml_op = LDAP_MOD_DELETE;
4010 mod->sml_desc = old->a_desc;
4011 mod->sml_type = mod->sml_desc->ad_cname;
4012 mod->sml_numvals = no;
4013 mod->sml_values = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
4014 if ( old->a_vals != old->a_nvals ) {
4015 mod->sml_nvalues = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
4017 mod->sml_nvalues = NULL;
4020 for ( i = 0; i < o; i++ ) {
4021 if ( !dels[i] ) continue;
4022 ber_dupbv( &mod->sml_values[j], &old->a_vals[i] );
4023 if ( mod->sml_nvalues ) {
4024 ber_dupbv( &mod->sml_nvalues[j], &old->a_nvals[i] );
4028 BER_BVZERO( &mod->sml_values[j] );
4029 if ( mod->sml_nvalues ) {
4030 BER_BVZERO( &mod->sml_nvalues[j] );
4033 modtail = &mod->sml_next;
4036 op->o_tmpfree( dels, op->o_tmpmemctx );
4037 /* some values were added */
4038 if ( nn && no < o ) {
4039 mod = ch_malloc( sizeof( Modifications ) );
4040 mod->sml_op = LDAP_MOD_ADD;
4042 mod->sml_desc = old->a_desc;
4043 mod->sml_type = mod->sml_desc->ad_cname;
4044 mod->sml_numvals = nn;
4045 mod->sml_values = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
4046 if ( old->a_vals != old->a_nvals ) {
4047 mod->sml_nvalues = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
4049 mod->sml_nvalues = NULL;
4052 for ( i = 0; i < n; i++ ) {
4053 if ( !adds[i] ) continue;
4054 ber_dupbv( &mod->sml_values[j], &new->a_vals[i] );
4055 if ( mod->sml_nvalues ) {
4056 ber_dupbv( &mod->sml_nvalues[j], &new->a_nvals[i] );
4060 BER_BVZERO( &mod->sml_values[j] );
4061 if ( mod->sml_nvalues ) {
4062 BER_BVZERO( &mod->sml_nvalues[j] );
4065 modtail = &mod->sml_next;
4068 op->o_tmpfree( adds, op->o_tmpmemctx );
4070 /* new attr, just use the new mod */
4074 /* advance to next element */
4078 **mcur = mod->sml_next;
4080 modtail = &mod->sml_next;
4082 *mcur = &mod->sml_next;
4088 /* Generate a set of modifications to change the old entry into the
4089 * new one. On input ml is a list of modifications equivalent to
4090 * the new entry. It will be massaged and the result will be stored
4093 void syncrepl_diff_entry( Operation *op, Attribute *old, Attribute *new,
4094 Modifications **mods, Modifications **ml, int is_ctx)
4096 Modifications **modtail = mods;
4098 /* We assume that attributes are saved in the same order
4099 * in the remote and local databases. So if we walk through
4100 * the attributeDescriptions one by one they should match in
4101 * lock step. If not, look for an add or delete.
4103 while ( old && new )
4105 /* If we've seen this before, use its mod now */
4106 if ( new->a_flags & SLAP_ATTR_IXADD ) {
4107 attr_cmp( op, NULL, new, &modtail, &ml );
4111 /* Skip contextCSN */
4112 if ( is_ctx && old->a_desc ==
4113 slap_schema.si_ad_contextCSN ) {
4118 if ( old->a_desc != new->a_desc ) {
4122 /* If it's just been re-added later,
4123 * remember that we've seen it.
4125 tmp = attr_find( new, old->a_desc );
4127 tmp->a_flags |= SLAP_ATTR_IXADD;
4129 /* If it's a new attribute, pull it in.
4131 tmp = attr_find( old, new->a_desc );
4133 attr_cmp( op, NULL, new, &modtail, &ml );
4137 /* Delete old attr */
4138 mod = ch_malloc( sizeof( Modifications ) );
4139 mod->sml_op = LDAP_MOD_DELETE;
4141 mod->sml_desc = old->a_desc;
4142 mod->sml_type = mod->sml_desc->ad_cname;
4143 mod->sml_numvals = 0;
4144 mod->sml_values = NULL;
4145 mod->sml_nvalues = NULL;
4147 modtail = &mod->sml_next;
4152 /* kludge - always update modifiersName so that it
4153 * stays co-located with the other mod opattrs. But only
4154 * if we know there are other valid mods.
4156 if ( *mods && ( old->a_desc == slap_schema.si_ad_modifiersName ||
4157 old->a_desc == slap_schema.si_ad_modifyTimestamp ))
4158 attr_cmp( op, NULL, new, &modtail, &ml );
4160 attr_cmp( op, old, new, &modtail, &ml );
4173 dninfo *dni = op->o_callback->sc_private;
4175 if ( rs->sr_type == REP_SEARCH ) {
4176 if ( !BER_BVISNULL( &dni->dn ) ) {
4177 Debug( LDAP_DEBUG_ANY,
4178 "dn_callback : consistency error - "
4179 "entryUUID is not unique\n", 0, 0, 0 );
4181 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
4182 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
4183 /* If there is a new entry, see if it differs from the old.
4184 * We compare the non-normalized values so that cosmetic changes
4185 * in the provider are always propagated.
4187 if ( dni->new_entry ) {
4188 Attribute *old, *new;
4189 struct berval old_rdn, new_rdn;
4190 struct berval old_p, new_p;
4191 int is_ctx, new_sup = 0;
4193 /* If old entry is not a glue entry, make sure new entry
4194 * is actually newer than old entry
4196 if ( !is_entry_glue( rs->sr_entry )) {
4197 old = attr_find( rs->sr_entry->e_attrs,
4198 slap_schema.si_ad_entryCSN );
4199 new = attr_find( dni->new_entry->e_attrs,
4200 slap_schema.si_ad_entryCSN );
4203 ber_len_t len = old->a_vals[0].bv_len;
4204 if ( len > new->a_vals[0].bv_len )
4205 len = new->a_vals[0].bv_len;
4206 rc = memcmp( old->a_vals[0].bv_val,
4207 new->a_vals[0].bv_val, len );
4209 Debug( LDAP_DEBUG_SYNC,
4210 "dn_callback : new entry is older than ours "
4211 "%s ours %s, new %s\n",
4212 rs->sr_entry->e_name.bv_val,
4213 old->a_vals[0].bv_val,
4214 new->a_vals[0].bv_val );
4215 return LDAP_SUCCESS;
4216 } else if ( rc == 0 ) {
4217 Debug( LDAP_DEBUG_SYNC,
4218 "dn_callback : entries have identical CSN "
4220 rs->sr_entry->e_name.bv_val,
4221 old->a_vals[0].bv_val, 0 );
4222 return LDAP_SUCCESS;
4227 is_ctx = dn_match( &rs->sr_entry->e_nname,
4228 &op->o_bd->be_nsuffix[0] );
4230 /* Did the DN change?
4231 * case changes in the parent are ignored,
4232 * we only want to know if the RDN was
4235 dnRdn( &rs->sr_entry->e_name, &old_rdn );
4236 dnRdn( &dni->new_entry->e_name, &new_rdn );
4237 dnParent( &rs->sr_entry->e_nname, &old_p );
4238 dnParent( &dni->new_entry->e_nname, &new_p );
4240 new_sup = !dn_match( &old_p, &new_p );
4241 if ( !dn_match( &old_rdn, &new_rdn ) || new_sup )
4243 struct berval oldRDN, oldVal;
4244 AttributeDescription *ad = NULL;
4250 dni->nnewSup = new_p;
4252 /* See if the oldRDN was deleted */
4253 dnRdn( &rs->sr_entry->e_nname, &oldRDN );
4254 oldVal.bv_val = strchr(oldRDN.bv_val, '=') + 1;
4255 oldVal.bv_len = oldRDN.bv_len - ( oldVal.bv_val -
4257 oldRDN.bv_len -= oldVal.bv_len + 1;
4258 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
4260 for ( oldpos=0, a=rs->sr_entry->e_attrs;
4261 a && a->a_desc != ad; oldpos++, a=a->a_next );
4262 /* a should not be NULL but apparently it happens.
4265 dni->oldNcount = a ? a->a_numvals : 0;
4266 for ( newpos=0, a=dni->new_entry->e_attrs;
4267 a && a->a_desc != ad; newpos++, a=a->a_next );
4268 if ( !a || oldpos != newpos || attr_valfind( a,
4269 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH |
4270 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
4271 SLAP_MR_VALUE_OF_SYNTAX,
4272 &oldVal, NULL, op->o_tmpmemctx ) != LDAP_SUCCESS )
4276 /* Get the newRDN's desc */
4277 dnRdn( &dni->new_entry->e_nname, &oldRDN );
4278 oldVal.bv_val = strchr(oldRDN.bv_val, '=');
4279 oldRDN.bv_len = oldVal.bv_val - oldRDN.bv_val;
4281 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
4284 /* A ModDN has happened, but in Refresh mode other
4285 * changes may have occurred before we picked it up.
4286 * So fallthru to regular Modify processing.
4290 syncrepl_diff_entry( op, rs->sr_entry->e_attrs,
4291 dni->new_entry->e_attrs, &dni->mods, dni->modlist,
4295 } else if ( rs->sr_type == REP_RESULT ) {
4296 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
4297 Debug( LDAP_DEBUG_ANY,
4298 "dn_callback : consistency error - "
4299 "entryUUID is not unique\n", 0, 0, 0 );
4303 return LDAP_SUCCESS;
4307 nonpresent_callback(
4311 syncinfo_t *si = op->o_callback->sc_private;
4314 char *present_uuid = NULL;
4315 struct nonpresent_entry *np_entry;
4317 if ( rs->sr_type == REP_RESULT ) {
4318 count = presentlist_free( si->si_presentlist );
4319 si->si_presentlist = NULL;
4321 } else if ( rs->sr_type == REP_SEARCH ) {
4322 if ( !( si->si_refreshDelete & NP_DELETE_ONE ) ) {
4323 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
4326 present_uuid = presentlist_find( si->si_presentlist, &a->a_nvals[0] );
4329 if ( LogTest( LDAP_DEBUG_SYNC ) ) {
4330 char buf[sizeof("rid=999 non")];
4332 snprintf( buf, sizeof(buf), "%s %s", si->si_ridtxt,
4333 present_uuid ? "" : "non" );
4335 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: %spresent UUID %s, dn %s\n",
4336 buf, a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val );
4339 if ( a == NULL ) return 0;
4342 if ( present_uuid == NULL ) {
4343 np_entry = (struct nonpresent_entry *)
4344 ch_calloc( 1, sizeof( struct nonpresent_entry ) );
4345 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
4346 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
4347 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
4350 presentlist_delete( &si->si_presentlist, &a->a_nvals[0] );
4351 ch_free( present_uuid );
4354 return LDAP_SUCCESS;
4362 if ( rs->sr_err != LDAP_SUCCESS &&
4363 rs->sr_err != LDAP_REFERRAL &&
4364 rs->sr_err != LDAP_ALREADY_EXISTS &&
4365 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
4366 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
4368 Debug( LDAP_DEBUG_ANY,
4369 "null_callback : error code 0x%x\n",
4372 return LDAP_SUCCESS;
4375 static struct berval *
4376 slap_uuidstr_from_normalized(
4377 struct berval* uuidstr,
4378 struct berval* normalized,
4383 unsigned char nibble;
4386 if ( normalized == NULL ) return NULL;
4387 if ( normalized->bv_len != 16 ) return NULL;
4392 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
4393 if ( new == NULL ) {
4400 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
4401 if ( new != uuidstr ) {
4402 slap_sl_free( new, ctx );
4407 for ( i = 0; i < 16; i++ ) {
4408 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
4409 new->bv_val[(i<<1)+d] = '-';
4413 nibble = (normalized->bv_val[i] >> 4) & 0xF;
4414 if ( nibble < 10 ) {
4415 new->bv_val[(i<<1)+d] = nibble + '0';
4417 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
4420 nibble = (normalized->bv_val[i]) & 0xF;
4421 if ( nibble < 10 ) {
4422 new->bv_val[(i<<1)+d+1] = nibble + '0';
4424 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
4428 new->bv_val[new->bv_len] = '\0';
4435 if ( normalized == NULL ) return NULL;
4436 if ( normalized->bv_len != 16 ) return NULL;
4442 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
4443 if ( new == NULL ) {
4450 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
4455 rc = lutil_uuidstr_from_normalized( normalized->bv_val,
4456 normalized->bv_len, new->bv_val, new->bv_len + 1 );
4460 if ( new != NULL ) {
4461 if ( new->bv_val != NULL ) {
4462 slap_sl_free( new->bv_val, ctx );
4465 if ( new != uuidstr ) {
4466 slap_sl_free( new, ctx );
4479 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
4482 return ( memcmp( v_uuid1, v_uuid2, UUIDLEN-2 ));
4484 return ( memcmp( v_uuid1, v_uuid2, UUIDLEN ));
4489 syncinfo_free( syncinfo_t *sie, int free_all )
4491 syncinfo_t *si_next;
4493 Debug( LDAP_DEBUG_TRACE, "syncinfo_free: %s\n",
4494 sie->si_ridtxt, 0, 0 );
4497 si_next = sie->si_next;
4500 if ( sie->si_conn ) {
4501 connection_client_stop( sie->si_conn );
4502 sie->si_conn = NULL;
4504 ldap_unbind_ext( sie->si_ld, NULL, NULL );
4508 struct re_s *re = sie->si_re;
4511 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4512 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
4513 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
4514 ldap_pvt_runqueue_remove( &slapd_rq, re );
4515 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4518 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
4520 bindconf_free( &sie->si_bindconf );
4522 if ( sie->si_filterstr.bv_val ) {
4523 ch_free( sie->si_filterstr.bv_val );
4525 if ( sie->si_filter ) {
4526 filter_free( sie->si_filter );
4528 if ( sie->si_logfilterstr.bv_val ) {
4529 ch_free( sie->si_logfilterstr.bv_val );
4531 if ( sie->si_base.bv_val ) {
4532 ch_free( sie->si_base.bv_val );
4534 if ( sie->si_logbase.bv_val ) {
4535 ch_free( sie->si_logbase.bv_val );
4537 if ( sie->si_be && SLAP_SYNC_SUBENTRY( sie->si_be )) {
4538 ch_free( sie->si_contextdn.bv_val );
4540 if ( sie->si_attrs ) {
4542 while ( sie->si_attrs[i] != NULL ) {
4543 ch_free( sie->si_attrs[i] );
4546 ch_free( sie->si_attrs );
4548 if ( sie->si_exattrs ) {
4550 while ( sie->si_exattrs[i] != NULL ) {
4551 ch_free( sie->si_exattrs[i] );
4554 ch_free( sie->si_exattrs );
4556 if ( sie->si_anlist ) {
4558 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
4559 ch_free( sie->si_anlist[i].an_name.bv_val );
4562 ch_free( sie->si_anlist );
4564 if ( sie->si_exanlist ) {
4566 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
4567 ch_free( sie->si_exanlist[i].an_name.bv_val );
4570 ch_free( sie->si_exanlist );
4572 if ( sie->si_retryinterval ) {
4573 ch_free( sie->si_retryinterval );
4575 if ( sie->si_retrynum ) {
4576 ch_free( sie->si_retrynum );
4578 if ( sie->si_retrynum_init ) {
4579 ch_free( sie->si_retrynum_init );
4581 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
4582 if ( sie->si_presentlist ) {
4583 presentlist_free( sie->si_presentlist );
4585 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist ) ) {
4586 struct nonpresent_entry* npe;
4587 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
4588 LDAP_LIST_REMOVE( npe, npe_link );
4589 if ( npe->npe_name ) {
4590 if ( npe->npe_name->bv_val ) {
4591 ch_free( npe->npe_name->bv_val );
4593 ch_free( npe->npe_name );
4595 if ( npe->npe_nname ) {
4596 if ( npe->npe_nname->bv_val ) {
4597 ch_free( npe->npe_nname->bv_val );
4599 ch_free( npe->npe_nname );
4603 if ( sie->si_cookieState ) {
4604 sie->si_cookieState->cs_ref--;
4605 if ( !sie->si_cookieState->cs_ref ) {
4606 ch_free( sie->si_cookieState->cs_sids );
4607 ber_bvarray_free( sie->si_cookieState->cs_vals );
4608 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_mutex );
4609 ch_free( sie->si_cookieState->cs_psids );
4610 ber_bvarray_free( sie->si_cookieState->cs_pvals );
4611 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_pmutex );
4612 ch_free( sie->si_cookieState );
4615 #ifdef ENABLE_REWRITE
4616 if ( sie->si_rewrite )
4617 rewrite_info_delete( &sie->si_rewrite );
4618 if ( sie->si_suffixm.bv_val )
4619 ch_free( sie->si_suffixm.bv_val );
4623 } while ( free_all && si_next );
4626 #ifdef ENABLE_REWRITE
4628 config_suffixm( ConfigArgs *c, syncinfo_t *si )
4630 char *argvEngine[] = { "rewriteEngine", "on", NULL };
4631 char *argvContext[] = { "rewriteContext", SUFFIXM_CTX, NULL };
4632 char *argvRule[] = { "rewriteRule", NULL, NULL, ":", NULL };
4636 if ( si->si_rewrite )
4637 rewrite_info_delete( &si->si_rewrite );
4638 si->si_rewrite = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
4640 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvEngine );
4641 if ( rc != LDAP_SUCCESS )
4644 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvContext );
4645 if ( rc != LDAP_SUCCESS )
4648 vnc = ch_malloc( si->si_base.bv_len + 6 );
4649 strcpy( vnc, "(.*)" );
4650 lutil_strcopy( lutil_strcopy( vnc+4, si->si_base.bv_val ), "$" );
4653 rnc = ch_malloc( si->si_suffixm.bv_len + 3 );
4654 strcpy( rnc, "%1" );
4655 strcpy( rnc+2, si->si_suffixm.bv_val );
4658 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 4, argvRule );
4665 /* NOTE: used & documented in slapd.conf(5) */
4667 #define PROVIDERSTR "provider"
4668 #define SCHEMASTR "schemachecking"
4669 #define FILTERSTR "filter"
4670 #define SEARCHBASESTR "searchbase"
4671 #define SCOPESTR "scope"
4672 #define ATTRSONLYSTR "attrsonly"
4673 #define ATTRSSTR "attrs"
4674 #define TYPESTR "type"
4675 #define INTERVALSTR "interval"
4676 #define RETRYSTR "retry"
4677 #define SLIMITSTR "sizelimit"
4678 #define TLIMITSTR "timelimit"
4679 #define SYNCDATASTR "syncdata"
4680 #define LOGBASESTR "logbase"
4681 #define LOGFILTERSTR "logfilter"
4682 #define SUFFIXMSTR "suffixmassage"
4683 #define STRICT_REFRESH "strictrefresh"
4685 /* FIXME: undocumented */
4686 #define EXATTRSSTR "exattrs"
4687 #define MANAGEDSAITSTR "manageDSAit"
4691 GOT_RID = 0x00000001U,
4692 GOT_PROVIDER = 0x00000002U,
4693 GOT_SCHEMACHECKING = 0x00000004U,
4694 GOT_FILTER = 0x00000008U,
4695 GOT_SEARCHBASE = 0x00000010U,
4696 GOT_SCOPE = 0x00000020U,
4697 GOT_ATTRSONLY = 0x00000040U,
4698 GOT_ATTRS = 0x00000080U,
4699 GOT_TYPE = 0x00000100U,
4700 GOT_INTERVAL = 0x00000200U,
4701 GOT_RETRY = 0x00000400U,
4702 GOT_SLIMIT = 0x00000800U,
4703 GOT_TLIMIT = 0x00001000U,
4704 GOT_SYNCDATA = 0x00002000U,
4705 GOT_LOGBASE = 0x00004000U,
4706 GOT_LOGFILTER = 0x00008000U,
4707 GOT_EXATTRS = 0x00010000U,
4708 GOT_MANAGEDSAIT = 0x00020000U,
4709 GOT_BINDCONF = 0x00040000U,
4710 GOT_SUFFIXM = 0x00080000U,
4713 GOT_REQUIRED = (GOT_RID|GOT_PROVIDER|GOT_SEARCHBASE)
4716 static slap_verbmasks datamodes[] = {
4717 { BER_BVC("default"), SYNCDATA_DEFAULT },
4718 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
4719 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
4724 parse_syncrepl_retry(
4731 int use_default = 0;
4733 char *val = arg + STRLENOF( RETRYSTR "=" );
4734 if ( strcasecmp( val, "undefined" ) == 0 ) {
4739 retry_list = (char **) ch_calloc( 1, sizeof( char * ) );
4740 retry_list[0] = NULL;
4742 slap_str2clist( &retry_list, val, " ,\t" );
4744 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
4747 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4748 "Error: incomplete syncrepl retry list" );
4749 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4750 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4751 ch_free( retry_list[k] );
4753 ch_free( retry_list );
4756 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ) );
4757 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ) );
4758 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ) );
4759 for ( j = 0; j < n; j++ ) {
4761 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
4762 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4763 "Error: invalid retry interval \"%s\" (#%d)",
4764 retry_list[j*2], j );
4765 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4766 /* do some cleanup */
4769 si->si_retryinterval[j] = (time_t)t;
4770 if ( *retry_list[j*2+1] == '+' ) {
4771 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
4772 si->si_retrynum[j] = RETRYNUM_FOREVER;
4776 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
4777 || si->si_retrynum_init[j] <= 0 )
4779 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4780 "Error: invalid initial retry number \"%s\" (#%d)",
4781 retry_list[j*2+1], j );
4782 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4783 /* do some cleanup */
4786 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
4787 || si->si_retrynum[j] <= 0 )
4789 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4790 "Error: invalid retry number \"%s\" (#%d)",
4791 retry_list[j*2+1], j );
4792 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4793 /* do some cleanup */
4798 if ( j < 1 || si->si_retrynum_init[j-1] != RETRYNUM_FOREVER ) {
4799 Debug( LDAP_DEBUG_CONFIG,
4800 "%s: syncrepl will eventually stop retrying; the \"retry\" parameter should end with a '+'.\n",
4804 si->si_retrynum_init[j] = RETRYNUM_TAIL;
4805 si->si_retrynum[j] = RETRYNUM_TAIL;
4806 si->si_retryinterval[j] = 0;
4808 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4809 ch_free( retry_list[k] );
4811 ch_free( retry_list );
4812 if ( !use_default ) {
4813 si->si_got |= GOT_RETRY;
4820 parse_syncrepl_line(
4827 for ( i = 1; i < c->argc; i++ ) {
4828 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
4829 STRLENOF( IDSTR "=" ) ) )
4832 /* '\0' string terminator accounts for '=' */
4833 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
4834 if ( lutil_atoi( &tmp, val ) != 0 ) {
4835 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4836 "Error: parse_syncrepl_line: "
4837 "unable to parse syncrepl id \"%s\"", val );
4838 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4841 if ( tmp > SLAP_SYNC_RID_MAX || tmp < 0 ) {
4842 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4843 "Error: parse_syncrepl_line: "
4844 "syncrepl id %d is out of range [0..%d]", tmp, SLAP_SYNC_RID_MAX );
4845 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4849 sprintf( si->si_ridtxt, IDSTR "=%03d", si->si_rid );
4850 si->si_got |= GOT_RID;
4851 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
4852 STRLENOF( PROVIDERSTR "=" ) ) )
4854 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
4855 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
4857 if ( ldap_is_ldaps_url( val ))
4858 si->si_bindconf.sb_tls_do_init = 1;
4860 si->si_got |= GOT_PROVIDER;
4861 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
4862 STRLENOF( SCHEMASTR "=" ) ) )
4864 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
4865 if ( !strncasecmp( val, "on", STRLENOF( "on" ) ) ) {
4866 si->si_schemachecking = 1;
4867 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
4868 si->si_schemachecking = 0;
4870 si->si_schemachecking = 1;
4872 si->si_got |= GOT_SCHEMACHECKING;
4873 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
4874 STRLENOF( FILTERSTR "=" ) ) )
4876 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
4877 if ( si->si_filterstr.bv_val )
4878 ch_free( si->si_filterstr.bv_val );
4879 ber_str2bv( val, 0, 1, &si->si_filterstr );
4880 si->si_got |= GOT_FILTER;
4881 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
4882 STRLENOF( LOGFILTERSTR "=" ) ) )
4884 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
4885 if ( si->si_logfilterstr.bv_val )
4886 ch_free( si->si_logfilterstr.bv_val );
4887 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
4888 si->si_got |= GOT_LOGFILTER;
4889 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
4890 STRLENOF( SEARCHBASESTR "=" ) ) )
4895 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
4896 if ( si->si_base.bv_val ) {
4897 ch_free( si->si_base.bv_val );
4899 ber_str2bv( val, 0, 0, &bv );
4900 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
4901 if ( rc != LDAP_SUCCESS ) {
4902 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4903 "Invalid base DN \"%s\": %d (%s)",
4904 val, rc, ldap_err2string( rc ) );
4905 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4908 si->si_got |= GOT_SEARCHBASE;
4909 #ifdef ENABLE_REWRITE
4910 } else if ( !strncasecmp( c->argv[ i ], SUFFIXMSTR "=",
4911 STRLENOF( SUFFIXMSTR "=" ) ) )
4916 val = c->argv[ i ] + STRLENOF( SUFFIXMSTR "=" );
4917 if ( si->si_suffixm.bv_val ) {
4918 ch_free( si->si_suffixm.bv_val );
4920 ber_str2bv( val, 0, 0, &bv );
4921 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_suffixm, NULL );
4922 if ( rc != LDAP_SUCCESS ) {
4923 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4924 "Invalid massage DN \"%s\": %d (%s)",
4925 val, rc, ldap_err2string( rc ) );
4926 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4929 if ( !be_issubordinate( c->be, &si->si_suffixm )) {
4930 ch_free( si->si_suffixm.bv_val );
4931 BER_BVZERO( &si->si_suffixm );
4932 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4933 "Massage DN \"%s\" is not within the database naming context",
4935 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4938 si->si_got |= GOT_SUFFIXM;
4940 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
4941 STRLENOF( LOGBASESTR "=" ) ) )
4946 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
4947 if ( si->si_logbase.bv_val ) {
4948 ch_free( si->si_logbase.bv_val );
4950 ber_str2bv( val, 0, 0, &bv );
4951 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
4952 if ( rc != LDAP_SUCCESS ) {
4953 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4954 "Invalid logbase DN \"%s\": %d (%s)",
4955 val, rc, ldap_err2string( rc ) );
4956 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4959 si->si_got |= GOT_LOGBASE;
4960 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
4961 STRLENOF( SCOPESTR "=" ) ) )
4964 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
4965 j = ldap_pvt_str2scope( val );
4967 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4968 "Error: parse_syncrepl_line: "
4969 "unknown scope \"%s\"", val);
4970 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4974 si->si_got |= GOT_SCOPE;
4975 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
4976 STRLENOF( ATTRSONLYSTR ) ) )
4978 si->si_attrsonly = 1;
4979 si->si_got |= GOT_ATTRSONLY;
4980 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
4981 STRLENOF( ATTRSSTR "=" ) ) )
4983 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
4984 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
4986 attr_fname = ch_strdup( val + STRLENOF(":include:") );
4987 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
4988 if ( si->si_anlist == NULL ) {
4989 ch_free( attr_fname );
4992 si->si_anfile = attr_fname;
4994 char *str, *s, *next;
4995 const char *delimstr = " ,\t";
4996 str = ch_strdup( val );
4997 for ( s = ldap_pvt_strtok( str, delimstr, &next );
4999 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
5001 if ( strlen(s) == 1 && *s == '*' ) {
5002 si->si_allattrs = 1;
5003 val[ s - str ] = delimstr[0];
5005 if ( strlen(s) == 1 && *s == '+' ) {
5006 si->si_allopattrs = 1;
5007 val [ s - str ] = delimstr[0];
5011 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
5012 if ( si->si_anlist == NULL ) {
5016 si->si_got |= GOT_ATTRS;
5017 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
5018 STRLENOF( EXATTRSSTR "=" ) ) )
5020 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
5021 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
5023 attr_fname = ch_strdup( val + STRLENOF(":include:") );
5024 si->si_exanlist = file2anlist(
5025 si->si_exanlist, attr_fname, " ,\t" );
5026 if ( si->si_exanlist == NULL ) {
5027 ch_free( attr_fname );
5030 ch_free( attr_fname );
5032 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
5033 if ( si->si_exanlist == NULL ) {
5037 si->si_got |= GOT_EXATTRS;
5038 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
5039 STRLENOF( TYPESTR "=" ) ) )
5041 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
5042 if ( !strncasecmp( val, "refreshOnly",
5043 STRLENOF("refreshOnly") ) )
5045 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
5046 } else if ( !strncasecmp( val, "refreshAndPersist",
5047 STRLENOF("refreshAndPersist") ) )
5049 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
5050 si->si_interval = 60;
5052 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5053 "Error: parse_syncrepl_line: "
5054 "unknown sync type \"%s\"", val);
5055 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5058 si->si_got |= GOT_TYPE;
5059 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
5060 STRLENOF( INTERVALSTR "=" ) ) )
5062 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
5063 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
5064 si->si_interval = 0;
5065 } else if ( strchr( val, ':' ) != NULL ) {
5066 char *next, *ptr = val;
5069 dd = strtol( ptr, &next, 10 );
5070 if ( next == ptr || next[0] != ':' || dd < 0 ) {
5071 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5072 "Error: parse_syncrepl_line: "
5073 "invalid interval \"%s\", unable to parse days", val );
5074 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5078 hh = strtol( ptr, &next, 10 );
5079 if ( next == ptr || next[0] != ':' || hh < 0 || hh > 24 ) {
5080 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5081 "Error: parse_syncrepl_line: "
5082 "invalid interval \"%s\", unable to parse hours", val );
5083 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5087 mm = strtol( ptr, &next, 10 );
5088 if ( next == ptr || next[0] != ':' || mm < 0 || mm > 60 ) {
5089 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5090 "Error: parse_syncrepl_line: "
5091 "invalid interval \"%s\", unable to parse minutes", val );
5092 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5096 ss = strtol( ptr, &next, 10 );
5097 if ( next == ptr || next[0] != '\0' || ss < 0 || ss > 60 ) {
5098 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5099 "Error: parse_syncrepl_line: "
5100 "invalid interval \"%s\", unable to parse seconds", val );
5101 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5104 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
5108 if ( lutil_parse_time( val, &t ) != 0 ) {
5109 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5110 "Error: parse_syncrepl_line: "
5111 "invalid interval \"%s\"", val );
5112 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5115 si->si_interval = (time_t)t;
5117 if ( si->si_interval < 0 ) {
5118 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5119 "Error: parse_syncrepl_line: "
5120 "invalid interval \"%ld\"",
5121 (long) si->si_interval);
5122 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5125 si->si_got |= GOT_INTERVAL;
5126 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
5127 STRLENOF( RETRYSTR "=" ) ) )
5129 if ( parse_syncrepl_retry( c, c->argv[ i ], si ) ) {
5132 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
5133 STRLENOF( MANAGEDSAITSTR "=" ) ) )
5135 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
5136 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
5137 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
5139 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5140 "invalid manageDSAit value \"%s\".\n",
5142 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5145 si->si_got |= GOT_MANAGEDSAIT;
5146 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
5147 STRLENOF( SLIMITSTR "=") ) )
5149 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
5150 if ( strcasecmp( val, "unlimited" ) == 0 ) {
5153 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
5154 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5155 "invalid size limit value \"%s\".\n",
5157 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5160 si->si_got |= GOT_SLIMIT;
5161 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
5162 STRLENOF( TLIMITSTR "=" ) ) )
5164 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
5165 if ( strcasecmp( val, "unlimited" ) == 0 ) {
5168 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
5169 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5170 "invalid time limit value \"%s\".\n",
5172 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5175 si->si_got |= GOT_TLIMIT;
5176 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
5177 STRLENOF( SYNCDATASTR "=" ) ) )
5179 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
5180 si->si_syncdata = verb_to_mask( val, datamodes );
5181 si->si_got |= GOT_SYNCDATA;
5182 } else if ( !strncasecmp( c->argv[ i ], STRICT_REFRESH,
5183 STRLENOF( STRICT_REFRESH ) ) )
5185 si->si_strict_refresh = 1;
5186 } else if ( !bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
5187 si->si_got |= GOT_BINDCONF;
5189 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5190 "Error: parse_syncrepl_line: "
5191 "unable to parse \"%s\"\n", c->argv[ i ] );
5192 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5197 if ( ( si->si_got & GOT_REQUIRED ) != GOT_REQUIRED ) {
5198 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5199 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
5200 si->si_got & GOT_RID ? "" : " "IDSTR,
5201 si->si_got & GOT_PROVIDER ? "" : " "PROVIDERSTR,
5202 si->si_got & GOT_SEARCHBASE ? "" : " "SEARCHBASESTR );
5203 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5207 if ( !be_issubordinate( c->be, &si->si_base ) && !( si->si_got & GOT_SUFFIXM )) {
5208 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5209 "Base DN \"%s\" is not within the database naming context",
5210 si->si_base.bv_val );
5211 ch_free( si->si_base.bv_val );
5212 BER_BVZERO( &si->si_base );
5213 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5217 #ifdef ENABLE_REWRITE
5218 if ( si->si_got & GOT_SUFFIXM ) {
5219 if (config_suffixm( c, si )) {
5220 ch_free( si->si_suffixm.bv_val );
5221 BER_BVZERO( &si->si_suffixm );
5222 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5223 "Error configuring rewrite engine" );
5224 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5230 if ( !( si->si_got & GOT_RETRY ) ) {
5231 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": no retry defined, using default\n",
5232 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", 0 );
5233 if ( si->si_retryinterval == NULL ) {
5234 if ( parse_syncrepl_retry( c, "retry=undefined", si ) ) {
5240 si->si_filter = str2filter( si->si_filterstr.bv_val );
5241 if ( si->si_filter == NULL ) {
5242 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": unable to parse filter=\"%s\"\n",
5243 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", si->si_filterstr.bv_val );
5257 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
5258 snprintf( c->cr_msg, sizeof(c->cr_msg), "database %s does not support "
5259 "operations required for syncrepl", c->be->be_type );
5260 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
5263 if ( BER_BVISEMPTY( &c->be->be_rootdn ) ) {
5264 strcpy( c->cr_msg, "rootDN must be defined before syncrepl may be used" );
5265 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
5268 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
5271 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
5275 si->si_bindconf.sb_tls = SB_TLS_OFF;
5276 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
5277 si->si_schemachecking = 0;
5278 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
5279 &si->si_filterstr );
5280 si->si_base.bv_val = NULL;
5281 si->si_scope = LDAP_SCOPE_SUBTREE;
5282 si->si_attrsonly = 0;
5283 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
5284 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
5285 si->si_attrs = NULL;
5286 si->si_allattrs = 0;
5287 si->si_allopattrs = 0;
5288 si->si_exattrs = NULL;
5289 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
5290 si->si_interval = 86400;
5291 si->si_retryinterval = NULL;
5292 si->si_retrynum_init = NULL;
5293 si->si_retrynum = NULL;
5294 si->si_manageDSAit = 0;
5298 si->si_presentlist = NULL;
5299 LDAP_LIST_INIT( &si->si_nonpresentlist );
5300 ldap_pvt_thread_mutex_init( &si->si_mutex );
5302 rc = parse_syncrepl_line( c, si );
5307 /* Must be LDAPv3 because we need controls */
5308 switch ( si->si_bindconf.sb_version ) {
5310 /* not explicitly set */
5311 si->si_bindconf.sb_version = LDAP_VERSION3;
5314 /* explicitly set */
5317 Debug( LDAP_DEBUG_ANY,
5318 "version %d incompatible with syncrepl\n",
5319 si->si_bindconf.sb_version, 0, 0 );
5320 syncinfo_free( si, 0 );
5324 if ( ldap_url_parse( si->si_bindconf.sb_uri.bv_val, &lud )) {
5325 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5326 "<%s> invalid URL", c->argv[0] );
5327 Debug( LDAP_DEBUG_ANY, "%s: %s %s\n",
5328 c->log, c->cr_msg, si->si_bindconf.sb_uri.bv_val );
5333 if ( slapMode & SLAP_SERVER_MODE ) {
5335 /* check if consumer points to current server and database.
5336 * If so, ignore this configuration.
5338 if ( !SLAP_DBHIDDEN( c->be ) ) {
5340 /* if searchbase doesn't match current DB suffix,
5341 * assume it's different
5343 for ( i=0; !BER_BVISNULL( &c->be->be_nsuffix[i] ); i++ ) {
5344 if ( bvmatch( &si->si_base, &c->be->be_nsuffix[i] )) {
5349 /* if searchbase matches, see if URLs match */
5350 if ( isMe && config_check_my_url( si->si_bindconf.sb_uri.bv_val,
5356 init_syncrepl( si );
5357 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
5358 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq,
5359 si->si_interval, do_syncrepl, si, "do_syncrepl",
5361 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
5363 rc = config_sync_shadow( c ) ? -1 : 0;
5368 /* mirrormode still needs to see this flag in tool mode */
5369 rc = config_sync_shadow( c ) ? -1 : 0;
5371 ldap_free_urldesc( lud );
5375 /* Use main slapd defaults */
5376 bindconf_tls_defaults( &si->si_bindconf );
5379 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
5380 syncinfo_free( si, 0 );
5383 Debug( LDAP_DEBUG_CONFIG,
5384 "Config: ** successfully added syncrepl %s \"%s\"\n",
5386 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
5387 "(null)" : si->si_bindconf.sb_uri.bv_val, 0 );
5388 if ( c->be->be_syncinfo ) {
5391 si->si_cookieState = c->be->be_syncinfo->si_cookieState;
5393 /* add new syncrepl to end of list (same order as when deleting) */
5394 for ( sip = c->be->be_syncinfo; sip->si_next; sip = sip->si_next );
5397 si->si_cookieState = ch_calloc( 1, sizeof( cookie_state ));
5398 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_mutex );
5399 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_pmutex );
5401 c->be->be_syncinfo = si;
5403 si->si_cookieState->cs_ref++;
5412 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
5414 struct berval bc, uri, bs;
5415 char buf[BUFSIZ*2], *ptr;
5418 # define WHATSLEFT ((ber_len_t) (&buf[sizeof( buf )] - ptr))
5422 /* temporarily inhibit bindconf from printing URI */
5423 uri = si->si_bindconf.sb_uri;
5424 BER_BVZERO( &si->si_bindconf.sb_uri );
5425 si->si_bindconf.sb_version = 0;
5426 bindconf_unparse( &si->si_bindconf, &bc );
5427 si->si_bindconf.sb_uri = uri;
5428 si->si_bindconf.sb_version = LDAP_VERSION3;
5431 assert( si->si_rid >= 0 && si->si_rid <= SLAP_SYNC_RID_MAX );
5432 len = snprintf( ptr, WHATSLEFT, IDSTR "=%03d " PROVIDERSTR "=%s",
5433 si->si_rid, si->si_bindconf.sb_uri.bv_val );
5434 if ( len >= sizeof( buf ) ) return;
5436 if ( !BER_BVISNULL( &bc ) ) {
5437 if ( WHATSLEFT <= bc.bv_len ) {
5441 ptr = lutil_strcopy( ptr, bc.bv_val );
5444 if ( !BER_BVISEMPTY( &si->si_filterstr ) ) {
5445 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
5446 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
5447 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
5450 if ( !BER_BVISNULL( &si->si_base ) ) {
5451 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
5452 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
5453 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
5456 #ifdef ENABLE_REWRITE
5457 if ( !BER_BVISNULL( &si->si_suffixm ) ) {
5458 if ( WHATSLEFT <= STRLENOF( " " SUFFIXMSTR "=\"" "\"" ) + si->si_suffixm.bv_len ) return;
5459 ptr = lutil_strcopy( ptr, " " SUFFIXMSTR "=\"" );
5460 ptr = lutil_strcopy( ptr, si->si_suffixm.bv_val );
5464 if ( !BER_BVISEMPTY( &si->si_logfilterstr ) ) {
5465 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
5466 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
5467 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
5470 if ( !BER_BVISNULL( &si->si_logbase ) ) {
5471 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
5472 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
5473 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
5476 if ( ldap_pvt_scope2bv( si->si_scope, &bs ) == LDAP_SUCCESS ) {
5477 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + bs.bv_len ) return;
5478 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
5479 ptr = lutil_strcopy( ptr, bs.bv_val );
5481 if ( si->si_attrsonly ) {
5482 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
5483 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
5485 if ( si->si_anfile ) {
5486 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
5487 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
5488 ptr = lutil_strcopy( ptr, si->si_anfile );
5490 } else if ( si->si_allattrs || si->si_allopattrs ||
5491 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ) )
5495 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
5496 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
5498 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
5499 if ( ptr == NULL ) return;
5500 if ( si->si_allattrs ) {
5501 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
5502 if ( old != ptr ) *ptr++ = ',';
5505 if ( si->si_allopattrs ) {
5506 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
5507 if ( old != ptr ) *ptr++ = ',';
5512 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
5513 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
5514 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
5515 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
5516 if ( ptr == NULL ) return;
5518 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
5519 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
5520 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
5522 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
5523 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
5524 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
5525 "refreshAndPersist" : "refreshOnly" );
5527 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
5530 dd = si->si_interval;
5537 len = snprintf( ptr, WHATSLEFT, " %s=%02d:%02d:%02d:%02d",
5538 INTERVALSTR, dd, hh, mm, ss );
5539 if ( len >= WHATSLEFT ) return;
5543 if ( si->si_got & GOT_RETRY ) {
5544 const char *space = "";
5545 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
5546 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
5547 for (i=0; si->si_retryinterval[i]; i++) {
5548 len = snprintf( ptr, WHATSLEFT, "%s%ld ", space,
5549 (long) si->si_retryinterval[i] );
5551 if ( WHATSLEFT - 1 <= len ) return;
5553 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
5556 len = snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
5557 if ( WHATSLEFT <= len ) return;
5561 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
5564 ptr = lutil_strcopy( ptr, " " RETRYSTR "=undefined" );
5567 if ( si->si_slimit ) {
5568 len = snprintf( ptr, WHATSLEFT, " " SLIMITSTR "=%d", si->si_slimit );
5569 if ( WHATSLEFT <= len ) return;
5573 if ( si->si_tlimit ) {
5574 len = snprintf( ptr, WHATSLEFT, " " TLIMITSTR "=%d", si->si_tlimit );
5575 if ( WHATSLEFT <= len ) return;
5579 if ( si->si_syncdata ) {
5580 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
5581 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
5582 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
5583 ptr = lutil_strcopy( ptr, bc.bv_val );
5586 bc.bv_len = ptr - buf;
5588 ber_dupbv( bv, &bc );
5592 syncrepl_config( ConfigArgs *c )
5594 if (c->op == SLAP_CONFIG_EMIT) {
5595 if ( c->be->be_syncinfo ) {
5599 for ( si = c->be->be_syncinfo; si; si=si->si_next ) {
5600 syncrepl_unparse( si, &bv );
5601 ber_bvarray_add( &c->rvalue_vals, &bv );
5606 } else if ( c->op == LDAP_MOD_DELETE ) {
5608 if ( c->be->be_syncinfo ) {
5609 syncinfo_t *si, **sip;
5612 for ( sip = &c->be->be_syncinfo, i=0; *sip; i++ ) {
5614 if ( c->valx == -1 || i == c->valx ) {
5618 /* If the task is currently active, we have to leave
5619 * it running. It will exit on its own. This will only
5620 * happen when running on the cn=config DB.
5623 if ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
5626 /* There is no active thread, but we must still
5627 * ensure that no thread is (or will be) queued
5628 * while we removes the task.
5630 struct re_s *re = si->si_re;
5633 if ( si->si_conn ) {
5634 connection_client_stop( si->si_conn );
5638 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
5639 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) ) {
5640 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
5643 if ( ldap_pvt_thread_pool_retract( &connection_pool,
5644 re->routine, re ) > 0 )
5647 ldap_pvt_runqueue_remove( &slapd_rq, re );
5648 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
5650 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
5654 syncinfo_free( si, 0 );
5663 if ( !c->be->be_syncinfo ) {
5664 SLAP_DBFLAGS( c->be ) &= ~SLAP_DBFLAG_SHADOW_MASK;
5668 if ( SLAP_SLURP_SHADOW( c->be ) ) {
5669 Debug(LDAP_DEBUG_ANY, "%s: "
5670 "syncrepl: database already shadowed.\n",
5674 return add_syncrepl( c );