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-2015 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;
2768 for (i=0; i<65536; i++) {
2770 count += avl_free( a2[i], ch_free );
2776 return avl_free( av, ch_free );
2783 struct berval *val )
2786 Avlnode **a2 = *(Avlnode ***)av;
2789 memcpy(&s, val->bv_val, 2);
2790 avl_delete( &a2[s], val->bv_val+2, syncuuid_cmp );
2792 avl_delete( av, val->bv_val, syncuuid_cmp );
2801 Modifications** modlist,
2803 struct berval* syncUUID,
2804 struct berval* syncCSN )
2806 Backend *be = op->o_bd;
2807 slap_callback cb = { NULL, NULL, NULL, NULL };
2808 int syncuuid_inserted = 0;
2810 SlapReply rs_search = {REP_RESULT};
2812 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2813 int rc = LDAP_SUCCESS;
2815 struct berval pdn = BER_BVNULL;
2820 Debug( LDAP_DEBUG_SYNC,
2821 "syncrepl_entry: %s LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_%s)\n",
2822 si->si_ridtxt, syncrepl_state2str( syncstate ), 0 );
2824 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ) ) {
2825 if ( !si->si_refreshPresent && !si->si_refreshDone ) {
2826 syncuuid_inserted = presentlist_insert( si, syncUUID );
2830 if ( syncstate == LDAP_SYNC_PRESENT ) {
2832 } else if ( syncstate != LDAP_SYNC_DELETE ) {
2833 if ( entry == NULL ) {
2838 if ( syncstate != LDAP_SYNC_DELETE ) {
2839 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
2842 /* add if missing */
2843 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
2844 &syncUUID[1], syncUUID );
2846 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
2847 /* replace only if necessary */
2848 if ( a->a_nvals != a->a_vals ) {
2849 ber_memfree( a->a_nvals[0].bv_val );
2850 ber_dupbv( &a->a_nvals[0], syncUUID );
2852 ber_memfree( a->a_vals[0].bv_val );
2853 ber_dupbv( &a->a_vals[0], &syncUUID[1] );
2857 f.f_choice = LDAP_FILTER_EQUALITY;
2859 ava.aa_desc = slap_schema.si_ad_entryUUID;
2860 ava.aa_value = *syncUUID;
2862 if ( syncuuid_inserted ) {
2863 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s inserted UUID %s\n",
2864 si->si_ridtxt, syncUUID[1].bv_val, 0 );
2866 op->ors_filter = &f;
2868 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID[1].bv_len;
2869 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
2870 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
2871 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
2872 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
2873 syncUUID[1].bv_val, syncUUID[1].bv_len );
2874 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
2875 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
2877 op->o_tag = LDAP_REQ_SEARCH;
2878 op->ors_scope = LDAP_SCOPE_SUBTREE;
2879 op->ors_deref = LDAP_DEREF_NEVER;
2881 /* get the entry for this UUID */
2882 #ifdef ENABLE_REWRITE
2883 if ( si->si_rewrite ) {
2884 op->o_req_dn = si->si_suffixm;
2885 op->o_req_ndn = si->si_suffixm;
2889 op->o_req_dn = si->si_base;
2890 op->o_req_ndn = si->si_base;
2893 op->o_time = slap_get_time();
2894 op->ors_tlimit = SLAP_NO_LIMIT;
2896 op->ors_limit = NULL;
2898 op->ors_attrs = slap_anlist_all_attributes;
2899 op->ors_attrsonly = 0;
2901 /* set callback function */
2902 op->o_callback = &cb;
2903 cb.sc_response = dn_callback;
2904 cb.sc_private = &dni;
2905 dni.new_entry = entry;
2906 dni.modlist = modlist;
2908 rc = be->be_search( op, &rs_search );
2909 Debug( LDAP_DEBUG_SYNC,
2910 "syncrepl_entry: %s be_search (%d)\n",
2911 si->si_ridtxt, rc, 0 );
2913 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
2914 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2917 cb.sc_response = null_callback;
2920 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
2921 Debug( LDAP_DEBUG_SYNC,
2922 "syncrepl_entry: %s %s\n",
2923 si->si_ridtxt, entry->e_name.bv_val, 0 );
2925 Debug( LDAP_DEBUG_SYNC,
2926 "syncrepl_entry: %s %s\n",
2927 si->si_ridtxt, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
2930 assert( BER_BVISNULL( &op->o_csn ) );
2932 slap_queue_csn( op, syncCSN );
2935 slap_op_time( &op->o_time, &op->o_tincr );
2936 switch ( syncstate ) {
2938 case LDAP_SYNC_MODIFY:
2939 if ( BER_BVISNULL( &op->o_csn ))
2942 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
2944 /* FIXME: op->o_csn is assumed to be
2945 * on the thread's slab; this needs
2946 * to be cleared ASAP.
2948 op->o_csn = a->a_vals[0];
2953 if ( BER_BVISNULL( &dni.dn ) ) {
2954 SlapReply rs_add = {REP_RESULT};
2956 op->o_req_dn = entry->e_name;
2957 op->o_req_ndn = entry->e_nname;
2958 op->o_tag = LDAP_REQ_ADD;
2960 op->o_bd = si->si_wbe;
2962 rc = op->o_bd->be_add( op, &rs_add );
2963 Debug( LDAP_DEBUG_SYNC,
2964 "syncrepl_entry: %s be_add %s (%d)\n",
2965 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2966 switch ( rs_add.sr_err ) {
2968 if ( op->ora_e == entry ) {
2969 be_entry_release_w( op, entry );
2975 /* we assume that LDAP_NO_SUCH_OBJECT is returned
2976 * only if the suffix entry is not present.
2977 * This should not happen during Persist phase.
2979 case LDAP_NO_SUCH_OBJECT:
2980 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST &&
2981 si->si_refreshDone ) {
2982 /* Something's wrong, start over */
2983 ber_bvarray_free( si->si_syncCookie.ctxcsn );
2984 si->si_syncCookie.ctxcsn = NULL;
2985 entry_free( entry );
2986 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
2987 ber_bvarray_free( si->si_cookieState->cs_vals );
2988 ch_free( si->si_cookieState->cs_sids );
2989 si->si_cookieState->cs_vals = NULL;
2990 si->si_cookieState->cs_sids = 0;
2991 si->si_cookieState->cs_num = 0;
2992 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
2993 return LDAP_NO_SUCH_OBJECT;
2995 rc = syncrepl_add_glue( op, entry );
2999 /* if an entry was added via syncrepl_add_glue(),
3000 * it likely has no entryUUID, so the previous
3001 * be_search() doesn't find it. In this case,
3002 * give syncrepl a chance to modify it. Also
3003 * allow for entries that were recreated with the
3004 * same DN but a different entryUUID.
3006 case LDAP_ALREADY_EXISTS:
3008 Operation op2 = *op;
3009 SlapReply rs2 = { REP_RESULT };
3010 slap_callback cb2 = { 0 };
3013 op2.o_tag = LDAP_REQ_SEARCH;
3014 op2.o_req_dn = entry->e_name;
3015 op2.o_req_ndn = entry->e_nname;
3016 op2.ors_scope = LDAP_SCOPE_BASE;
3017 op2.ors_deref = LDAP_DEREF_NEVER;
3018 op2.ors_attrs = slap_anlist_all_attributes;
3019 op2.ors_attrsonly = 0;
3020 op2.ors_limit = NULL;
3022 op2.ors_tlimit = SLAP_NO_LIMIT;
3024 f.f_choice = LDAP_FILTER_PRESENT;
3025 f.f_desc = slap_schema.si_ad_objectClass;
3026 op2.ors_filter = &f;
3027 op2.ors_filterstr = generic_filterstr;
3029 op2.o_callback = &cb2;
3030 cb2.sc_response = dn_callback;
3031 cb2.sc_private = &dni;
3033 rc = be->be_search( &op2, &rs2 );
3034 if ( rc ) goto done;
3037 slap_op_time( &op->o_time, &op->o_tincr );
3043 Debug( LDAP_DEBUG_ANY,
3044 "syncrepl_entry: %s be_add %s failed (%d)\n",
3045 si->si_ridtxt, op->o_req_dn.bv_val, rs_add.sr_err );
3053 op->o_req_dn = dni.dn;
3054 op->o_req_ndn = dni.ndn;
3055 if ( dni.renamed ) {
3056 struct berval noldp, newp;
3057 Modifications *mod, **modtail, **ml, *m2;
3058 int i, got_replace = 0, just_rename = 0;
3059 SlapReply rs_modify = {REP_RESULT};
3061 op->o_tag = LDAP_REQ_MODRDN;
3062 dnRdn( &entry->e_name, &op->orr_newrdn );
3063 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
3065 if ( !BER_BVISNULL( &dni.nnewSup )) {
3066 dnParent( &entry->e_name, &newp );
3067 op->orr_newSup = &newp;
3068 op->orr_nnewSup = &dni.nnewSup;
3070 op->orr_newSup = NULL;
3071 op->orr_nnewSup = NULL;
3073 op->orr_deleteoldrdn = dni.delOldRDN;
3074 op->orr_modlist = NULL;
3075 if ( ( rc = slap_modrdn2mods( op, &rs_modify ) ) ) {
3079 /* Drop the RDN-related mods from this op, because their
3080 * equivalents were just setup by slap_modrdn2mods.
3082 * If delOldRDN is TRUE then we should see a delete modop
3083 * for oldDesc. We might see a replace instead.
3084 * delete with no values: therefore newDesc != oldDesc.
3085 * if oldNcount == 1, then Drop this op.
3086 * delete with 1 value: can only be the oldRDN value. Drop op.
3087 * delete with N values: Drop oldRDN value, keep remainder.
3088 * replace with 1 value: if oldNcount == 1 and
3089 * newDesc == oldDesc, Drop this op.
3090 * Any other cases must be left intact.
3092 * We should also see an add modop for newDesc. (But not if
3093 * we got a replace modop due to delOldRDN.) If it has
3094 * multiple values, we'll have to drop the new RDN value.
3096 modtail = &op->orr_modlist;
3097 if ( dni.delOldRDN ) {
3098 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
3099 if ( (*ml)->sml_desc == dni.oldDesc ) {
3101 if ( mod->sml_op == LDAP_MOD_REPLACE &&
3102 dni.oldDesc != dni.newDesc ) {
3103 /* This Replace is due to other Mods.
3108 if ( mod->sml_numvals <= 1 &&
3109 dni.oldNcount == 1 &&
3110 ( mod->sml_op == LDAP_MOD_DELETE ||
3111 mod->sml_op == LDAP_MOD_REPLACE )) {
3112 if ( mod->sml_op == LDAP_MOD_REPLACE )
3115 *ml = mod->sml_next;
3116 mod->sml_next = NULL;
3117 slap_mods_free( mod, 1 );
3120 if ( mod->sml_op != LDAP_MOD_DELETE || mod->sml_numvals == 0 )
3122 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
3123 if ( m2->sml_desc == dni.oldDesc &&
3124 m2->sml_op == LDAP_MOD_DELETE ) break;
3126 for ( i=0; i<mod->sml_numvals; i++ ) {
3127 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
3129 ch_free( mod->sml_values[i].bv_val );
3130 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
3131 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
3132 if ( mod->sml_nvalues ) {
3133 ch_free( mod->sml_nvalues[i].bv_val );
3134 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
3135 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
3140 if ( !mod->sml_numvals ) {
3142 *ml = mod->sml_next;
3143 mod->sml_next = NULL;
3144 slap_mods_free( mod, 1 );
3150 if ( !got_replace ) {
3151 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
3152 if ( (*ml)->sml_desc == dni.newDesc ) {
3154 if ( mod->sml_op != LDAP_MOD_ADD )
3156 if ( mod->sml_numvals == 1 ) {
3158 *ml = mod->sml_next;
3159 mod->sml_next = NULL;
3160 slap_mods_free( mod, 1 );
3163 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
3164 if ( m2->sml_desc == dni.oldDesc &&
3165 m2->sml_op == SLAP_MOD_SOFTADD ) break;
3167 for ( i=0; i<mod->sml_numvals; i++ ) {
3168 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
3170 ch_free( mod->sml_values[i].bv_val );
3171 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
3172 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
3173 if ( mod->sml_nvalues ) {
3174 ch_free( mod->sml_nvalues[i].bv_val );
3175 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
3176 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
3186 /* RDNs must be NUL-terminated for back-ldap */
3187 noldp = op->orr_newrdn;
3188 ber_dupbv_x( &op->orr_newrdn, &noldp, op->o_tmpmemctx );
3189 noldp = op->orr_nnewrdn;
3190 ber_dupbv_x( &op->orr_nnewrdn, &noldp, op->o_tmpmemctx );
3192 /* Setup opattrs too */
3194 static AttributeDescription *nullattr = NULL;
3195 static AttributeDescription **const opattrs[] = {
3196 &slap_schema.si_ad_entryCSN,
3197 &slap_schema.si_ad_modifiersName,
3198 &slap_schema.si_ad_modifyTimestamp,
3201 AttributeDescription *opattr;
3205 /* pull mod off incoming modlist */
3206 for ( i = 0; (opattr = *opattrs[i]) != NULL; i++ ) {
3207 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next )
3209 if ( (*ml)->sml_desc == opattr ) {
3211 *ml = mod->sml_next;
3212 mod->sml_next = NULL;
3214 modtail = &mod->sml_next;
3219 /* If there are still Modifications left, put the opattrs
3220 * back, and let be_modify run. Otherwise, append the opattrs
3221 * to the orr_modlist.
3225 /* don't set a CSN for the rename op */
3227 slap_graduate_commit_csn( op );
3229 mod = op->orr_modlist;
3232 for ( ; mod->sml_next; mod=mod->sml_next );
3235 op->o_bd = si->si_wbe;
3237 rs_reinit( &rs_modify, REP_RESULT );
3238 rc = op->o_bd->be_modrdn( op, &rs_modify );
3240 /* NOTE: noSuchObject should result because the new superior
3241 * has not been added yet (ITS#6472) */
3242 if ( rc == LDAP_NO_SUCH_OBJECT && op->orr_nnewSup != NULL ) {
3243 Operation op2 = *op;
3244 rc = syncrepl_add_glue_ancestors( &op2, entry );
3245 if ( rc == LDAP_SUCCESS ) {
3250 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
3251 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
3253 slap_mods_free( op->orr_modlist, 1 );
3254 Debug( LDAP_DEBUG_SYNC,
3255 "syncrepl_entry: %s be_modrdn %s (%d)\n",
3256 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3258 /* Renamed entries may still have other mods so just fallthru */
3259 op->o_req_dn = entry->e_name;
3260 op->o_req_ndn = entry->e_nname;
3261 /* Use CSN on the modify */
3265 slap_queue_csn( op, syncCSN );
3268 SlapReply rs_modify = {REP_RESULT};
3270 op->o_tag = LDAP_REQ_MODIFY;
3271 op->orm_modlist = dni.mods;
3272 op->orm_no_opattrs = 1;
3273 op->o_bd = si->si_wbe;
3275 rc = op->o_bd->be_modify( op, &rs_modify );
3276 slap_mods_free( op->orm_modlist, 1 );
3277 op->orm_no_opattrs = 0;
3278 Debug( LDAP_DEBUG_SYNC,
3279 "syncrepl_entry: %s be_modify %s (%d)\n",
3280 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3281 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
3282 Debug( LDAP_DEBUG_ANY,
3283 "syncrepl_entry: %s be_modify failed (%d)\n",
3284 si->si_ridtxt, rs_modify.sr_err, 0 );
3288 } else if ( !dni.renamed ) {
3289 Debug( LDAP_DEBUG_SYNC,
3290 "syncrepl_entry: %s entry unchanged, ignored (%s)\n",
3291 si->si_ridtxt, op->o_req_dn.bv_val, 0 );
3293 slap_graduate_commit_csn( op );
3298 case LDAP_SYNC_DELETE :
3299 if ( !BER_BVISNULL( &dni.dn ) ) {
3300 SlapReply rs_delete = {REP_RESULT};
3301 op->o_req_dn = dni.dn;
3302 op->o_req_ndn = dni.ndn;
3303 op->o_tag = LDAP_REQ_DELETE;
3304 op->o_bd = si->si_wbe;
3306 slap_queue_csn( op, si->si_syncCookie.ctxcsn );
3308 rc = op->o_bd->be_delete( op, &rs_delete );
3309 Debug( LDAP_DEBUG_SYNC,
3310 "syncrepl_entry: %s be_delete %s (%d)\n",
3311 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3312 if ( rc == LDAP_NO_SUCH_OBJECT )
3315 while ( rs_delete.sr_err == LDAP_SUCCESS
3316 && op->o_delete_glue_parent ) {
3317 op->o_delete_glue_parent = 0;
3318 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
3319 slap_callback cb = { NULL };
3320 cb.sc_response = slap_null_cb;
3321 dnParent( &op->o_req_ndn, &pdn );
3323 op->o_req_ndn = pdn;
3324 op->o_callback = &cb;
3325 rs_reinit( &rs_delete, REP_RESULT );
3326 op->o_bd->be_delete( op, &rs_delete );
3337 Debug( LDAP_DEBUG_ANY,
3338 "syncrepl_entry: %s unknown syncstate\n", si->si_ridtxt, 0, 0 );
3343 slap_sl_free( syncUUID[1].bv_val, op->o_tmpmemctx );
3344 BER_BVZERO( &syncUUID[1] );
3345 if ( !BER_BVISNULL( &dni.ndn ) ) {
3346 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
3348 if ( !BER_BVISNULL( &dni.dn ) ) {
3349 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
3352 entry_free( entry );
3355 slap_graduate_commit_csn( op );
3357 if ( !BER_BVISNULL( &op->o_csn ) && freecsn ) {
3358 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3360 BER_BVZERO( &op->o_csn );
3364 static struct berval gcbva[] = {
3370 #define NP_DELETE_ONE 2
3373 syncrepl_del_nonpresent(
3377 struct sync_cookie *sc,
3380 Backend* be = op->o_bd;
3381 slap_callback cb = { NULL };
3382 struct nonpresent_entry *np_list, *np_prev;
3384 AttributeName an[2];
3386 struct berval pdn = BER_BVNULL;
3389 #ifdef ENABLE_REWRITE
3390 if ( si->si_rewrite ) {
3391 op->o_req_dn = si->si_suffixm;
3392 op->o_req_ndn = si->si_suffixm;
3396 op->o_req_dn = si->si_base;
3397 op->o_req_ndn = si->si_base;
3400 cb.sc_response = nonpresent_callback;
3403 op->o_callback = &cb;
3404 op->o_tag = LDAP_REQ_SEARCH;
3405 op->ors_scope = si->si_scope;
3406 op->ors_deref = LDAP_DEREF_NEVER;
3407 op->o_time = slap_get_time();
3408 op->ors_tlimit = SLAP_NO_LIMIT;
3413 AttributeAssertion eq = ATTRIBUTEASSERTION_INIT;
3416 op->ors_attrsonly = 1;
3417 op->ors_attrs = slap_anlist_no_attrs;
3418 op->ors_limit = NULL;
3419 op->ors_filter = &uf;
3422 uf.f_av_desc = slap_schema.si_ad_entryUUID;
3424 uf.f_choice = LDAP_FILTER_EQUALITY;
3425 si->si_refreshDelete |= NP_DELETE_ONE;
3427 for (i=0; uuids[i].bv_val; i++) {
3428 SlapReply rs_search = {REP_RESULT};
3431 uf.f_av_value = uuids[i];
3432 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
3433 rc = be->be_search( op, &rs_search );
3434 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
3436 si->si_refreshDelete ^= NP_DELETE_ONE;
3440 AttributeAssertion mmaa;
3441 SlapReply rs_search = {REP_RESULT};
3443 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
3444 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
3445 an[0].an_desc = slap_schema.si_ad_entryUUID;
3447 op->ors_slimit = SLAP_NO_LIMIT;
3448 op->ors_tlimit = SLAP_NO_LIMIT;
3449 op->ors_limit = NULL;
3450 op->ors_attrsonly = 0;
3451 op->ors_filter = filter_dup( si->si_filter, op->o_tmpmemctx );
3452 /* In multimaster, updates can continue to arrive while
3453 * we're searching. Limit the search result to entries
3454 * older than our newest cookie CSN.
3456 if ( SLAP_MULTIMASTER( op->o_bd )) {
3461 f->f_choice = LDAP_FILTER_AND;
3462 f->f_next = op->ors_filter;
3466 f->f_choice = LDAP_FILTER_LE;
3468 f->f_av_desc = slap_schema.si_ad_entryCSN;
3470 BER_BVZERO( &f->f_av_value );
3471 for ( i=0; i<sc->numcsns; i++ ) {
3472 if ( ber_bvcmp( &sc->ctxcsn[i], &f->f_av_value ) > 0 )
3473 f->f_av_value = sc->ctxcsn[i];
3475 of = op->ors_filter;
3476 op->ors_filter = mmf;
3477 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
3480 op->ors_filterstr = si->si_filterstr;
3482 op->o_nocaching = 1;
3485 rc = be->be_search( op, &rs_search );
3486 if ( SLAP_MULTIMASTER( op->o_bd )) {
3487 op->ors_filter = of;
3489 if ( op->ors_filter ) filter_free_x( op, op->ors_filter, 1 );
3490 if ( op->ors_filterstr.bv_val != si->si_filterstr.bv_val ) {
3491 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
3496 op->o_nocaching = 0;
3498 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
3500 if ( sc->ctxcsn && !BER_BVISNULL( &sc->ctxcsn[m] ) ) {
3501 csn = sc->ctxcsn[m];
3503 csn = si->si_syncCookie.ctxcsn[0];
3506 op->o_bd = si->si_wbe;
3507 slap_queue_csn( op, &csn );
3509 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
3510 while ( np_list != NULL ) {
3511 SlapReply rs_delete = {REP_RESULT};
3513 LDAP_LIST_REMOVE( np_list, npe_link );
3515 np_list = LDAP_LIST_NEXT( np_list, npe_link );
3516 op->o_tag = LDAP_REQ_DELETE;
3517 op->o_callback = &cb;
3518 cb.sc_response = null_callback;
3520 op->o_req_dn = *np_prev->npe_name;
3521 op->o_req_ndn = *np_prev->npe_nname;
3522 rc = op->o_bd->be_delete( op, &rs_delete );
3523 Debug( LDAP_DEBUG_SYNC,
3524 "syncrepl_del_nonpresent: %s be_delete %s (%d)\n",
3525 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3527 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
3528 SlapReply rs_modify = {REP_RESULT};
3529 Modifications mod1, mod2;
3530 mod1.sml_op = LDAP_MOD_REPLACE;
3532 mod1.sml_desc = slap_schema.si_ad_objectClass;
3533 mod1.sml_type = mod1.sml_desc->ad_cname;
3534 mod1.sml_numvals = 2;
3535 mod1.sml_values = &gcbva[0];
3536 mod1.sml_nvalues = NULL;
3537 mod1.sml_next = &mod2;
3539 mod2.sml_op = LDAP_MOD_REPLACE;
3541 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
3542 mod2.sml_type = mod2.sml_desc->ad_cname;
3543 mod2.sml_numvals = 1;
3544 mod2.sml_values = &gcbva[1];
3545 mod2.sml_nvalues = NULL;
3546 mod2.sml_next = NULL;
3548 op->o_tag = LDAP_REQ_MODIFY;
3549 op->orm_modlist = &mod1;
3551 rc = op->o_bd->be_modify( op, &rs_modify );
3552 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
3555 while ( rs_delete.sr_err == LDAP_SUCCESS &&
3556 op->o_delete_glue_parent ) {
3557 op->o_delete_glue_parent = 0;
3558 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
3559 slap_callback cb = { NULL };
3560 cb.sc_response = slap_null_cb;
3561 dnParent( &op->o_req_ndn, &pdn );
3563 op->o_req_ndn = pdn;
3564 op->o_callback = &cb;
3565 rs_reinit( &rs_delete, REP_RESULT );
3566 /* give it a root privil ? */
3567 op->o_bd->be_delete( op, &rs_delete );
3573 op->o_delete_glue_parent = 0;
3575 ber_bvfree( np_prev->npe_name );
3576 ber_bvfree( np_prev->npe_nname );
3579 if ( slapd_shutdown ) {
3584 slap_graduate_commit_csn( op );
3587 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3588 BER_BVZERO( &op->o_csn );
3595 syncrepl_add_glue_ancestors(
3599 Backend *be = op->o_bd;
3600 slap_callback cb = { NULL };
3602 int rc = LDAP_SUCCESS;
3605 struct berval dn = BER_BVNULL;
3606 struct berval ndn = BER_BVNULL;
3608 struct berval ptr, nptr;
3611 op->o_tag = LDAP_REQ_ADD;
3612 op->o_callback = &cb;
3613 cb.sc_response = null_callback;
3614 cb.sc_private = NULL;
3619 /* count RDNs in suffix */
3620 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
3621 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
3623 ptr.bv_len -= comma - ptr.bv_val;
3633 /* Start with BE suffix */
3635 for ( i = 0; i < suffrdns; i++ ) {
3636 comma = ber_bvrchr( &ptr, ',' );
3637 if ( comma != NULL ) {
3638 ptr.bv_len = comma - ptr.bv_val;
3645 if ( !BER_BVISEMPTY( &ptr ) ) {
3646 dn.bv_len -= ptr.bv_len + ( suffrdns != 0 );
3647 dn.bv_val += ptr.bv_len + ( suffrdns != 0 );
3650 /* the normalizedDNs are always the same length, no counting
3654 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
3655 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
3656 ndn.bv_len = be->be_nsuffix[0].bv_len;
3658 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
3664 while ( ndn.bv_val > e->e_nname.bv_val ) {
3665 SlapReply rs_add = {REP_RESULT};
3667 glue = entry_alloc();
3668 ber_dupbv( &glue->e_name, &dn );
3669 ber_dupbv( &glue->e_nname, &ndn );
3671 a = attr_alloc( slap_schema.si_ad_objectClass );
3674 a->a_vals = ch_calloc( 3, sizeof( struct berval ) );
3675 ber_dupbv( &a->a_vals[0], &gcbva[0] );
3676 ber_dupbv( &a->a_vals[1], &gcbva[1] );
3677 ber_dupbv( &a->a_vals[2], &gcbva[2] );
3679 a->a_nvals = a->a_vals;
3681 a->a_next = glue->e_attrs;
3684 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
3687 a->a_vals = ch_calloc( 2, sizeof( struct berval ) );
3688 ber_dupbv( &a->a_vals[0], &gcbva[1] );
3689 ber_dupbv( &a->a_vals[1], &gcbva[2] );
3691 a->a_nvals = a->a_vals;
3693 a->a_next = glue->e_attrs;
3696 op->o_req_dn = glue->e_name;
3697 op->o_req_ndn = glue->e_nname;
3699 rc = be->be_add ( op, &rs_add );
3700 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3701 if ( op->ora_e == glue )
3702 be_entry_release_w( op, glue );
3704 /* incl. ALREADY EXIST */
3706 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
3712 /* Move to next child */
3713 comma = ber_bvrchr( &ptr, ',' );
3714 if ( comma == NULL ) {
3717 ptr.bv_len = comma - ptr.bv_val;
3719 dn.bv_val = ++comma;
3720 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
3722 comma = ber_bvrchr( &nptr, ',' );
3723 assert( comma != NULL );
3724 nptr.bv_len = comma - nptr.bv_val;
3726 ndn.bv_val = ++comma;
3727 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
3738 slap_callback cb = { NULL };
3740 Backend *be = op->o_bd;
3741 SlapReply rs_add = {REP_RESULT};
3743 rc = syncrepl_add_glue_ancestors( op, e );
3746 case LDAP_ALREADY_EXISTS:
3753 op->o_tag = LDAP_REQ_ADD;
3754 op->o_callback = &cb;
3755 cb.sc_response = null_callback;
3756 cb.sc_private = NULL;
3758 op->o_req_dn = e->e_name;
3759 op->o_req_ndn = e->e_nname;
3761 rc = be->be_add ( op, &rs_add );
3762 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3763 if ( op->ora_e == e )
3764 be_entry_release_w( op, e );
3773 syncrepl_updateCookie(
3776 struct sync_cookie *syncCookie )
3778 Backend *be = op->o_bd;
3780 struct berval first = BER_BVNULL;
3781 struct sync_cookie sc;
3783 Syntax *syn = slap_schema.si_ad_contextCSN->ad_type->sat_syntax;
3786 int rc, i, j, changed = 0;
3789 slap_callback cb = { NULL };
3790 SlapReply rs_modify = {REP_RESULT};
3792 mod.sml_op = LDAP_MOD_REPLACE;
3793 mod.sml_desc = slap_schema.si_ad_contextCSN;
3794 mod.sml_type = mod.sml_desc->ad_cname;
3795 mod.sml_flags = SLAP_MOD_INTERNAL;
3796 mod.sml_nvalues = NULL;
3797 mod.sml_next = NULL;
3799 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
3802 for ( i=0; i<syncCookie->numcsns; i++ ) {
3803 assert( !syn->ssyn_validate( syn, syncCookie->ctxcsn+i ));
3805 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3806 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3810 /* clone the cookieState CSNs so we can Replace the whole thing */
3811 sc.numcsns = si->si_cookieState->cs_num;
3813 ber_bvarray_dup_x( &sc.ctxcsn, si->si_cookieState->cs_vals, NULL );
3814 sc.sids = ch_malloc( sc.numcsns * sizeof(int));
3815 for ( i=0; i<sc.numcsns; i++ )
3816 sc.sids[i] = si->si_cookieState->cs_sids[i];
3822 /* find any CSNs in the syncCookie that are newer than the cookieState */
3823 for ( i=0; i<syncCookie->numcsns; i++ ) {
3824 for ( j=0; j<sc.numcsns; j++ ) {
3825 if ( syncCookie->sids[i] < sc.sids[j] )
3827 if ( syncCookie->sids[i] != sc.sids[j] )
3829 len = syncCookie->ctxcsn[i].bv_len;
3830 if ( len > sc.ctxcsn[j].bv_len )
3831 len = sc.ctxcsn[j].bv_len;
3832 if ( memcmp( syncCookie->ctxcsn[i].bv_val,
3833 sc.ctxcsn[j].bv_val, len ) > 0 ) {
3834 ber_bvreplace( &sc.ctxcsn[j], &syncCookie->ctxcsn[i] );
3836 if ( BER_BVISNULL( &first ) ||
3837 memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 ) {
3838 first = syncCookie->ctxcsn[i];
3843 /* there was no match for this SID, it's a new CSN */
3844 if ( j == sc.numcsns ||
3845 syncCookie->sids[i] != sc.sids[j] ) {
3846 slap_insert_csn_sids( &sc, j, syncCookie->sids[i],
3847 &syncCookie->ctxcsn[i] );
3848 if ( BER_BVISNULL( &first ) ||
3849 memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 ) {
3850 first = syncCookie->ctxcsn[i];
3855 /* Should never happen, ITS#5065 */
3856 if ( BER_BVISNULL( &first ) || !changed ) {
3857 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3858 ber_bvarray_free( sc.ctxcsn );
3862 op->o_bd = si->si_wbe;
3863 slap_queue_csn( op, &first );
3865 op->o_tag = LDAP_REQ_MODIFY;
3867 cb.sc_response = null_callback;
3870 op->o_callback = &cb;
3871 op->o_req_dn = si->si_contextdn;
3872 op->o_req_ndn = si->si_contextdn;
3874 /* update contextCSN */
3875 op->o_dont_replicate = 1;
3877 mod.sml_numvals = sc.numcsns;
3878 mod.sml_values = sc.ctxcsn;
3880 op->orm_modlist = &mod;
3881 op->orm_no_opattrs = 1;
3882 rc = op->o_bd->be_modify( op, &rs_modify );
3884 if ( rs_modify.sr_err == LDAP_NO_SUCH_OBJECT &&
3885 SLAP_SYNC_SUBENTRY( op->o_bd )) {
3887 char txtbuf[SLAP_TEXT_BUFLEN];
3888 size_t textlen = sizeof txtbuf;
3889 Entry *e = slap_create_context_csn_entry( op->o_bd, NULL );
3890 rs_reinit( &rs_modify, REP_RESULT );
3891 rc = slap_mods2entry( &mod, &e, 0, 1, &text, txtbuf, textlen);
3893 rc = op->o_bd->be_add( op, &rs_modify );
3894 if ( e == op->ora_e )
3895 be_entry_release_w( op, op->ora_e );
3898 op->orm_no_opattrs = 0;
3899 op->o_dont_replicate = 0;
3901 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
3902 slap_sync_cookie_free( &si->si_syncCookie, 0 );
3903 ber_bvarray_free( si->si_cookieState->cs_vals );
3904 ch_free( si->si_cookieState->cs_sids );
3905 si->si_cookieState->cs_vals = sc.ctxcsn;
3906 si->si_cookieState->cs_sids = sc.sids;
3907 si->si_cookieState->cs_num = sc.numcsns;
3909 /* Don't just dup the provider's cookie, recreate it */
3910 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
3911 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn, si->si_cookieState->cs_vals, NULL );
3912 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num * sizeof(int) );
3913 for ( i=0; i<si->si_cookieState->cs_num; i++ )
3914 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
3916 si->si_cookieState->cs_age++;
3917 si->si_cookieAge = si->si_cookieState->cs_age;
3919 Debug( LDAP_DEBUG_ANY,
3920 "syncrepl_updateCookie: %s be_modify failed (%d)\n",
3921 si->si_ridtxt, rs_modify.sr_err, 0 );
3923 ber_bvarray_free( sc.ctxcsn );
3927 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3928 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3932 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3935 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3936 BER_BVZERO( &op->o_csn );
3937 if ( mod.sml_next ) slap_mods_free( mod.sml_next, 1 );
3942 /* Compare the attribute from the old entry to the one in the new
3943 * entry. The Modifications from the new entry will either be left
3944 * in place, or changed to an Add or Delete as needed.
3947 attr_cmp( Operation *op, Attribute *old, Attribute *new,
3948 Modifications ***mret, Modifications ***mcur )
3951 Modifications *mod, **modtail;
3957 struct berval **adds, **dels;
3958 /* count old and new */
3959 for ( o=0; old->a_vals[o].bv_val; o++ ) ;
3960 for ( n=0; new->a_vals[n].bv_val; n++ ) ;
3962 /* there MUST be both old and new values */
3967 adds = op->o_tmpalloc( sizeof(struct berval *) * n, op->o_tmpmemctx );
3968 dels = op->o_tmpalloc( sizeof(struct berval *) * o, op->o_tmpmemctx );
3970 for ( i=0; i<o; i++ ) dels[i] = &old->a_vals[i];
3971 for ( i=0; i<n; i++ ) adds[i] = &new->a_vals[i];
3975 for ( i=0; i<o; i++ ) {
3976 for ( j=0; j<n; j++ ) {
3979 if ( bvmatch( dels[i], adds[j] ) ) {
3989 /* Don't delete/add an objectClass, always use the replace op.
3990 * Modify would fail if provider has replaced entry with a new,
3991 * and the new explicitly includes a superior of a class that was
3992 * only included implicitly in the old entry. Ref ITS#5517.
3994 * Also use replace op if attr has no equality matching rule.
3997 if ( ( nn || ( no > 0 && no < o ) ) &&
3998 ( old->a_desc == slap_schema.si_ad_objectClass ||
3999 !old->a_desc->ad_type->sat_equality ) )
4005 /* all old values were deleted, just use the replace op */
4009 /* delete some values */
4010 mod = ch_malloc( sizeof( Modifications ) );
4011 mod->sml_op = LDAP_MOD_DELETE;
4013 mod->sml_desc = old->a_desc;
4014 mod->sml_type = mod->sml_desc->ad_cname;
4015 mod->sml_numvals = no;
4016 mod->sml_values = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
4017 if ( old->a_vals != old->a_nvals ) {
4018 mod->sml_nvalues = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
4020 mod->sml_nvalues = NULL;
4023 for ( i = 0; i < o; i++ ) {
4024 if ( !dels[i] ) continue;
4025 ber_dupbv( &mod->sml_values[j], &old->a_vals[i] );
4026 if ( mod->sml_nvalues ) {
4027 ber_dupbv( &mod->sml_nvalues[j], &old->a_nvals[i] );
4031 BER_BVZERO( &mod->sml_values[j] );
4032 if ( mod->sml_nvalues ) {
4033 BER_BVZERO( &mod->sml_nvalues[j] );
4036 modtail = &mod->sml_next;
4039 op->o_tmpfree( dels, op->o_tmpmemctx );
4040 /* some values were added */
4041 if ( nn && no < o ) {
4042 mod = ch_malloc( sizeof( Modifications ) );
4043 mod->sml_op = LDAP_MOD_ADD;
4045 mod->sml_desc = old->a_desc;
4046 mod->sml_type = mod->sml_desc->ad_cname;
4047 mod->sml_numvals = nn;
4048 mod->sml_values = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
4049 if ( old->a_vals != old->a_nvals ) {
4050 mod->sml_nvalues = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
4052 mod->sml_nvalues = NULL;
4055 for ( i = 0; i < n; i++ ) {
4056 if ( !adds[i] ) continue;
4057 ber_dupbv( &mod->sml_values[j], &new->a_vals[i] );
4058 if ( mod->sml_nvalues ) {
4059 ber_dupbv( &mod->sml_nvalues[j], &new->a_nvals[i] );
4063 BER_BVZERO( &mod->sml_values[j] );
4064 if ( mod->sml_nvalues ) {
4065 BER_BVZERO( &mod->sml_nvalues[j] );
4068 modtail = &mod->sml_next;
4071 op->o_tmpfree( adds, op->o_tmpmemctx );
4073 /* new attr, just use the new mod */
4077 /* advance to next element */
4081 **mcur = mod->sml_next;
4083 modtail = &mod->sml_next;
4085 *mcur = &mod->sml_next;
4091 /* Generate a set of modifications to change the old entry into the
4092 * new one. On input ml is a list of modifications equivalent to
4093 * the new entry. It will be massaged and the result will be stored
4096 void syncrepl_diff_entry( Operation *op, Attribute *old, Attribute *new,
4097 Modifications **mods, Modifications **ml, int is_ctx)
4099 Modifications **modtail = mods;
4101 /* We assume that attributes are saved in the same order
4102 * in the remote and local databases. So if we walk through
4103 * the attributeDescriptions one by one they should match in
4104 * lock step. If not, look for an add or delete.
4106 while ( old && new )
4108 /* If we've seen this before, use its mod now */
4109 if ( new->a_flags & SLAP_ATTR_IXADD ) {
4110 attr_cmp( op, NULL, new, &modtail, &ml );
4114 /* Skip contextCSN */
4115 if ( is_ctx && old->a_desc ==
4116 slap_schema.si_ad_contextCSN ) {
4121 if ( old->a_desc != new->a_desc ) {
4125 /* If it's just been re-added later,
4126 * remember that we've seen it.
4128 tmp = attr_find( new, old->a_desc );
4130 tmp->a_flags |= SLAP_ATTR_IXADD;
4132 /* If it's a new attribute, pull it in.
4134 tmp = attr_find( old, new->a_desc );
4136 attr_cmp( op, NULL, new, &modtail, &ml );
4140 /* Delete old attr */
4141 mod = ch_malloc( sizeof( Modifications ) );
4142 mod->sml_op = LDAP_MOD_DELETE;
4144 mod->sml_desc = old->a_desc;
4145 mod->sml_type = mod->sml_desc->ad_cname;
4146 mod->sml_numvals = 0;
4147 mod->sml_values = NULL;
4148 mod->sml_nvalues = NULL;
4150 modtail = &mod->sml_next;
4155 /* kludge - always update modifiersName so that it
4156 * stays co-located with the other mod opattrs. But only
4157 * if we know there are other valid mods.
4159 if ( *mods && ( old->a_desc == slap_schema.si_ad_modifiersName ||
4160 old->a_desc == slap_schema.si_ad_modifyTimestamp ))
4161 attr_cmp( op, NULL, new, &modtail, &ml );
4163 attr_cmp( op, old, new, &modtail, &ml );
4176 dninfo *dni = op->o_callback->sc_private;
4178 if ( rs->sr_type == REP_SEARCH ) {
4179 if ( !BER_BVISNULL( &dni->dn ) ) {
4180 Debug( LDAP_DEBUG_ANY,
4181 "dn_callback : consistency error - "
4182 "entryUUID is not unique\n", 0, 0, 0 );
4184 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
4185 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
4186 /* If there is a new entry, see if it differs from the old.
4187 * We compare the non-normalized values so that cosmetic changes
4188 * in the provider are always propagated.
4190 if ( dni->new_entry ) {
4191 Attribute *old, *new;
4192 struct berval old_rdn, new_rdn;
4193 struct berval old_p, new_p;
4194 int is_ctx, new_sup = 0;
4196 /* If old entry is not a glue entry, make sure new entry
4197 * is actually newer than old entry
4199 if ( !is_entry_glue( rs->sr_entry )) {
4200 old = attr_find( rs->sr_entry->e_attrs,
4201 slap_schema.si_ad_entryCSN );
4202 new = attr_find( dni->new_entry->e_attrs,
4203 slap_schema.si_ad_entryCSN );
4206 ber_len_t len = old->a_vals[0].bv_len;
4207 if ( len > new->a_vals[0].bv_len )
4208 len = new->a_vals[0].bv_len;
4209 rc = memcmp( old->a_vals[0].bv_val,
4210 new->a_vals[0].bv_val, len );
4212 Debug( LDAP_DEBUG_SYNC,
4213 "dn_callback : new entry is older than ours "
4214 "%s ours %s, new %s\n",
4215 rs->sr_entry->e_name.bv_val,
4216 old->a_vals[0].bv_val,
4217 new->a_vals[0].bv_val );
4218 return LDAP_SUCCESS;
4219 } else if ( rc == 0 ) {
4220 Debug( LDAP_DEBUG_SYNC,
4221 "dn_callback : entries have identical CSN "
4223 rs->sr_entry->e_name.bv_val,
4224 old->a_vals[0].bv_val, 0 );
4225 return LDAP_SUCCESS;
4230 is_ctx = dn_match( &rs->sr_entry->e_nname,
4231 &op->o_bd->be_nsuffix[0] );
4233 /* Did the DN change?
4234 * case changes in the parent are ignored,
4235 * we only want to know if the RDN was
4238 dnRdn( &rs->sr_entry->e_name, &old_rdn );
4239 dnRdn( &dni->new_entry->e_name, &new_rdn );
4240 dnParent( &rs->sr_entry->e_nname, &old_p );
4241 dnParent( &dni->new_entry->e_nname, &new_p );
4243 new_sup = !dn_match( &old_p, &new_p );
4244 if ( !dn_match( &old_rdn, &new_rdn ) || new_sup )
4246 struct berval oldRDN, oldVal;
4247 AttributeDescription *ad = NULL;
4253 dni->nnewSup = new_p;
4255 /* See if the oldRDN was deleted */
4256 dnRdn( &rs->sr_entry->e_nname, &oldRDN );
4257 oldVal.bv_val = strchr(oldRDN.bv_val, '=') + 1;
4258 oldVal.bv_len = oldRDN.bv_len - ( oldVal.bv_val -
4260 oldRDN.bv_len -= oldVal.bv_len + 1;
4261 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
4263 for ( oldpos=0, a=rs->sr_entry->e_attrs;
4264 a && a->a_desc != ad; oldpos++, a=a->a_next );
4265 /* a should not be NULL but apparently it happens.
4268 dni->oldNcount = a ? a->a_numvals : 0;
4269 for ( newpos=0, a=dni->new_entry->e_attrs;
4270 a && a->a_desc != ad; newpos++, a=a->a_next );
4271 if ( !a || oldpos != newpos || attr_valfind( a,
4272 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH |
4273 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
4274 SLAP_MR_VALUE_OF_SYNTAX,
4275 &oldVal, NULL, op->o_tmpmemctx ) != LDAP_SUCCESS )
4279 /* Get the newRDN's desc */
4280 dnRdn( &dni->new_entry->e_nname, &oldRDN );
4281 oldVal.bv_val = strchr(oldRDN.bv_val, '=');
4282 oldRDN.bv_len = oldVal.bv_val - oldRDN.bv_val;
4284 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
4287 /* A ModDN has happened, but in Refresh mode other
4288 * changes may have occurred before we picked it up.
4289 * So fallthru to regular Modify processing.
4293 syncrepl_diff_entry( op, rs->sr_entry->e_attrs,
4294 dni->new_entry->e_attrs, &dni->mods, dni->modlist,
4298 } else if ( rs->sr_type == REP_RESULT ) {
4299 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
4300 Debug( LDAP_DEBUG_ANY,
4301 "dn_callback : consistency error - "
4302 "entryUUID is not unique\n", 0, 0, 0 );
4306 return LDAP_SUCCESS;
4310 nonpresent_callback(
4314 syncinfo_t *si = op->o_callback->sc_private;
4317 char *present_uuid = NULL;
4318 struct nonpresent_entry *np_entry;
4320 if ( rs->sr_type == REP_RESULT ) {
4321 count = presentlist_free( si->si_presentlist );
4322 si->si_presentlist = NULL;
4324 } else if ( rs->sr_type == REP_SEARCH ) {
4325 if ( !( si->si_refreshDelete & NP_DELETE_ONE ) ) {
4326 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
4329 present_uuid = presentlist_find( si->si_presentlist, &a->a_nvals[0] );
4332 if ( LogTest( LDAP_DEBUG_SYNC ) ) {
4333 char buf[sizeof("rid=999 non")];
4335 snprintf( buf, sizeof(buf), "%s %s", si->si_ridtxt,
4336 present_uuid ? "" : "non" );
4338 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: %spresent UUID %s, dn %s\n",
4339 buf, a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val );
4342 if ( a == NULL ) return 0;
4345 if ( present_uuid == NULL ) {
4346 np_entry = (struct nonpresent_entry *)
4347 ch_calloc( 1, sizeof( struct nonpresent_entry ) );
4348 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
4349 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
4350 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
4353 presentlist_delete( &si->si_presentlist, &a->a_nvals[0] );
4354 ch_free( present_uuid );
4357 return LDAP_SUCCESS;
4365 if ( rs->sr_err != LDAP_SUCCESS &&
4366 rs->sr_err != LDAP_REFERRAL &&
4367 rs->sr_err != LDAP_ALREADY_EXISTS &&
4368 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
4369 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
4371 Debug( LDAP_DEBUG_ANY,
4372 "null_callback : error code 0x%x\n",
4375 return LDAP_SUCCESS;
4378 static struct berval *
4379 slap_uuidstr_from_normalized(
4380 struct berval* uuidstr,
4381 struct berval* normalized,
4386 unsigned char nibble;
4389 if ( normalized == NULL ) return NULL;
4390 if ( normalized->bv_len != 16 ) return NULL;
4395 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
4396 if ( new == NULL ) {
4403 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
4404 if ( new != uuidstr ) {
4405 slap_sl_free( new, ctx );
4410 for ( i = 0; i < 16; i++ ) {
4411 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
4412 new->bv_val[(i<<1)+d] = '-';
4416 nibble = (normalized->bv_val[i] >> 4) & 0xF;
4417 if ( nibble < 10 ) {
4418 new->bv_val[(i<<1)+d] = nibble + '0';
4420 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
4423 nibble = (normalized->bv_val[i]) & 0xF;
4424 if ( nibble < 10 ) {
4425 new->bv_val[(i<<1)+d+1] = nibble + '0';
4427 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
4431 new->bv_val[new->bv_len] = '\0';
4438 if ( normalized == NULL ) return NULL;
4439 if ( normalized->bv_len != 16 ) return NULL;
4445 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
4446 if ( new == NULL ) {
4453 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
4458 rc = lutil_uuidstr_from_normalized( normalized->bv_val,
4459 normalized->bv_len, new->bv_val, new->bv_len + 1 );
4463 if ( new != NULL ) {
4464 if ( new->bv_val != NULL ) {
4465 slap_sl_free( new->bv_val, ctx );
4468 if ( new != uuidstr ) {
4469 slap_sl_free( new, ctx );
4482 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
4485 return ( memcmp( v_uuid1, v_uuid2, UUIDLEN-2 ));
4487 return ( memcmp( v_uuid1, v_uuid2, UUIDLEN ));
4492 syncinfo_free( syncinfo_t *sie, int free_all )
4494 syncinfo_t *si_next;
4496 Debug( LDAP_DEBUG_TRACE, "syncinfo_free: %s\n",
4497 sie->si_ridtxt, 0, 0 );
4500 si_next = sie->si_next;
4503 if ( sie->si_conn ) {
4504 connection_client_stop( sie->si_conn );
4505 sie->si_conn = NULL;
4507 ldap_unbind_ext( sie->si_ld, NULL, NULL );
4511 struct re_s *re = sie->si_re;
4514 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4515 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
4516 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
4517 ldap_pvt_runqueue_remove( &slapd_rq, re );
4518 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4521 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
4523 bindconf_free( &sie->si_bindconf );
4525 if ( sie->si_filterstr.bv_val ) {
4526 ch_free( sie->si_filterstr.bv_val );
4528 if ( sie->si_filter ) {
4529 filter_free( sie->si_filter );
4531 if ( sie->si_logfilterstr.bv_val ) {
4532 ch_free( sie->si_logfilterstr.bv_val );
4534 if ( sie->si_base.bv_val ) {
4535 ch_free( sie->si_base.bv_val );
4537 if ( sie->si_logbase.bv_val ) {
4538 ch_free( sie->si_logbase.bv_val );
4540 if ( sie->si_be && SLAP_SYNC_SUBENTRY( sie->si_be )) {
4541 ch_free( sie->si_contextdn.bv_val );
4543 if ( sie->si_attrs ) {
4545 while ( sie->si_attrs[i] != NULL ) {
4546 ch_free( sie->si_attrs[i] );
4549 ch_free( sie->si_attrs );
4551 if ( sie->si_exattrs ) {
4553 while ( sie->si_exattrs[i] != NULL ) {
4554 ch_free( sie->si_exattrs[i] );
4557 ch_free( sie->si_exattrs );
4559 if ( sie->si_anlist ) {
4561 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
4562 ch_free( sie->si_anlist[i].an_name.bv_val );
4565 ch_free( sie->si_anlist );
4567 if ( sie->si_exanlist ) {
4569 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
4570 ch_free( sie->si_exanlist[i].an_name.bv_val );
4573 ch_free( sie->si_exanlist );
4575 if ( sie->si_retryinterval ) {
4576 ch_free( sie->si_retryinterval );
4578 if ( sie->si_retrynum ) {
4579 ch_free( sie->si_retrynum );
4581 if ( sie->si_retrynum_init ) {
4582 ch_free( sie->si_retrynum_init );
4584 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
4585 if ( sie->si_presentlist ) {
4586 presentlist_free( sie->si_presentlist );
4588 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist ) ) {
4589 struct nonpresent_entry* npe;
4590 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
4591 LDAP_LIST_REMOVE( npe, npe_link );
4592 if ( npe->npe_name ) {
4593 if ( npe->npe_name->bv_val ) {
4594 ch_free( npe->npe_name->bv_val );
4596 ch_free( npe->npe_name );
4598 if ( npe->npe_nname ) {
4599 if ( npe->npe_nname->bv_val ) {
4600 ch_free( npe->npe_nname->bv_val );
4602 ch_free( npe->npe_nname );
4606 if ( sie->si_cookieState ) {
4607 sie->si_cookieState->cs_ref--;
4608 if ( !sie->si_cookieState->cs_ref ) {
4609 ch_free( sie->si_cookieState->cs_sids );
4610 ber_bvarray_free( sie->si_cookieState->cs_vals );
4611 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_mutex );
4612 ch_free( sie->si_cookieState->cs_psids );
4613 ber_bvarray_free( sie->si_cookieState->cs_pvals );
4614 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_pmutex );
4615 ch_free( sie->si_cookieState );
4618 #ifdef ENABLE_REWRITE
4619 if ( sie->si_rewrite )
4620 rewrite_info_delete( &sie->si_rewrite );
4621 if ( sie->si_suffixm.bv_val )
4622 ch_free( sie->si_suffixm.bv_val );
4626 } while ( free_all && si_next );
4629 #ifdef ENABLE_REWRITE
4631 config_suffixm( ConfigArgs *c, syncinfo_t *si )
4633 char *argvEngine[] = { "rewriteEngine", "on", NULL };
4634 char *argvContext[] = { "rewriteContext", SUFFIXM_CTX, NULL };
4635 char *argvRule[] = { "rewriteRule", NULL, NULL, ":", NULL };
4639 if ( si->si_rewrite )
4640 rewrite_info_delete( &si->si_rewrite );
4641 si->si_rewrite = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
4643 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvEngine );
4644 if ( rc != LDAP_SUCCESS )
4647 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvContext );
4648 if ( rc != LDAP_SUCCESS )
4651 vnc = ch_malloc( si->si_base.bv_len + 6 );
4652 strcpy( vnc, "(.*)" );
4653 lutil_strcopy( lutil_strcopy( vnc+4, si->si_base.bv_val ), "$" );
4656 rnc = ch_malloc( si->si_suffixm.bv_len + 3 );
4657 strcpy( rnc, "%1" );
4658 strcpy( rnc+2, si->si_suffixm.bv_val );
4661 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 4, argvRule );
4668 /* NOTE: used & documented in slapd.conf(5) */
4670 #define PROVIDERSTR "provider"
4671 #define SCHEMASTR "schemachecking"
4672 #define FILTERSTR "filter"
4673 #define SEARCHBASESTR "searchbase"
4674 #define SCOPESTR "scope"
4675 #define ATTRSONLYSTR "attrsonly"
4676 #define ATTRSSTR "attrs"
4677 #define TYPESTR "type"
4678 #define INTERVALSTR "interval"
4679 #define RETRYSTR "retry"
4680 #define SLIMITSTR "sizelimit"
4681 #define TLIMITSTR "timelimit"
4682 #define SYNCDATASTR "syncdata"
4683 #define LOGBASESTR "logbase"
4684 #define LOGFILTERSTR "logfilter"
4685 #define SUFFIXMSTR "suffixmassage"
4686 #define STRICT_REFRESH "strictrefresh"
4688 /* FIXME: undocumented */
4689 #define EXATTRSSTR "exattrs"
4690 #define MANAGEDSAITSTR "manageDSAit"
4694 GOT_RID = 0x00000001U,
4695 GOT_PROVIDER = 0x00000002U,
4696 GOT_SCHEMACHECKING = 0x00000004U,
4697 GOT_FILTER = 0x00000008U,
4698 GOT_SEARCHBASE = 0x00000010U,
4699 GOT_SCOPE = 0x00000020U,
4700 GOT_ATTRSONLY = 0x00000040U,
4701 GOT_ATTRS = 0x00000080U,
4702 GOT_TYPE = 0x00000100U,
4703 GOT_INTERVAL = 0x00000200U,
4704 GOT_RETRY = 0x00000400U,
4705 GOT_SLIMIT = 0x00000800U,
4706 GOT_TLIMIT = 0x00001000U,
4707 GOT_SYNCDATA = 0x00002000U,
4708 GOT_LOGBASE = 0x00004000U,
4709 GOT_LOGFILTER = 0x00008000U,
4710 GOT_EXATTRS = 0x00010000U,
4711 GOT_MANAGEDSAIT = 0x00020000U,
4712 GOT_BINDCONF = 0x00040000U,
4713 GOT_SUFFIXM = 0x00080000U,
4716 GOT_REQUIRED = (GOT_RID|GOT_PROVIDER|GOT_SEARCHBASE)
4719 static slap_verbmasks datamodes[] = {
4720 { BER_BVC("default"), SYNCDATA_DEFAULT },
4721 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
4722 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
4727 parse_syncrepl_retry(
4734 int use_default = 0;
4736 char *val = arg + STRLENOF( RETRYSTR "=" );
4737 if ( strcasecmp( val, "undefined" ) == 0 ) {
4742 retry_list = (char **) ch_calloc( 1, sizeof( char * ) );
4743 retry_list[0] = NULL;
4745 slap_str2clist( &retry_list, val, " ,\t" );
4747 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
4750 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4751 "Error: incomplete syncrepl retry list" );
4752 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4753 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4754 ch_free( retry_list[k] );
4756 ch_free( retry_list );
4759 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ) );
4760 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ) );
4761 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ) );
4762 for ( j = 0; j < n; j++ ) {
4764 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
4765 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4766 "Error: invalid retry interval \"%s\" (#%d)",
4767 retry_list[j*2], j );
4768 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4769 /* do some cleanup */
4772 si->si_retryinterval[j] = (time_t)t;
4773 if ( *retry_list[j*2+1] == '+' ) {
4774 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
4775 si->si_retrynum[j] = RETRYNUM_FOREVER;
4779 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
4780 || si->si_retrynum_init[j] <= 0 )
4782 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4783 "Error: invalid initial retry number \"%s\" (#%d)",
4784 retry_list[j*2+1], j );
4785 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4786 /* do some cleanup */
4789 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
4790 || si->si_retrynum[j] <= 0 )
4792 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4793 "Error: invalid retry number \"%s\" (#%d)",
4794 retry_list[j*2+1], j );
4795 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4796 /* do some cleanup */
4801 if ( j < 1 || si->si_retrynum_init[j-1] != RETRYNUM_FOREVER ) {
4802 Debug( LDAP_DEBUG_CONFIG,
4803 "%s: syncrepl will eventually stop retrying; the \"retry\" parameter should end with a '+'.\n",
4807 si->si_retrynum_init[j] = RETRYNUM_TAIL;
4808 si->si_retrynum[j] = RETRYNUM_TAIL;
4809 si->si_retryinterval[j] = 0;
4811 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4812 ch_free( retry_list[k] );
4814 ch_free( retry_list );
4815 if ( !use_default ) {
4816 si->si_got |= GOT_RETRY;
4823 parse_syncrepl_line(
4830 for ( i = 1; i < c->argc; i++ ) {
4831 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
4832 STRLENOF( IDSTR "=" ) ) )
4835 /* '\0' string terminator accounts for '=' */
4836 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
4837 if ( lutil_atoi( &tmp, val ) != 0 ) {
4838 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4839 "Error: parse_syncrepl_line: "
4840 "unable to parse syncrepl id \"%s\"", val );
4841 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4844 if ( tmp > SLAP_SYNC_RID_MAX || tmp < 0 ) {
4845 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4846 "Error: parse_syncrepl_line: "
4847 "syncrepl id %d is out of range [0..%d]", tmp, SLAP_SYNC_RID_MAX );
4848 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4852 sprintf( si->si_ridtxt, IDSTR "=%03d", si->si_rid );
4853 si->si_got |= GOT_RID;
4854 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
4855 STRLENOF( PROVIDERSTR "=" ) ) )
4857 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
4858 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
4860 if ( ldap_is_ldaps_url( val ))
4861 si->si_bindconf.sb_tls_do_init = 1;
4863 si->si_got |= GOT_PROVIDER;
4864 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
4865 STRLENOF( SCHEMASTR "=" ) ) )
4867 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
4868 if ( !strncasecmp( val, "on", STRLENOF( "on" ) ) ) {
4869 si->si_schemachecking = 1;
4870 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
4871 si->si_schemachecking = 0;
4873 si->si_schemachecking = 1;
4875 si->si_got |= GOT_SCHEMACHECKING;
4876 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
4877 STRLENOF( FILTERSTR "=" ) ) )
4879 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
4880 if ( si->si_filterstr.bv_val )
4881 ch_free( si->si_filterstr.bv_val );
4882 ber_str2bv( val, 0, 1, &si->si_filterstr );
4883 si->si_got |= GOT_FILTER;
4884 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
4885 STRLENOF( LOGFILTERSTR "=" ) ) )
4887 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
4888 if ( si->si_logfilterstr.bv_val )
4889 ch_free( si->si_logfilterstr.bv_val );
4890 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
4891 si->si_got |= GOT_LOGFILTER;
4892 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
4893 STRLENOF( SEARCHBASESTR "=" ) ) )
4898 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
4899 if ( si->si_base.bv_val ) {
4900 ch_free( si->si_base.bv_val );
4902 ber_str2bv( val, 0, 0, &bv );
4903 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
4904 if ( rc != LDAP_SUCCESS ) {
4905 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4906 "Invalid base DN \"%s\": %d (%s)",
4907 val, rc, ldap_err2string( rc ) );
4908 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4911 si->si_got |= GOT_SEARCHBASE;
4912 #ifdef ENABLE_REWRITE
4913 } else if ( !strncasecmp( c->argv[ i ], SUFFIXMSTR "=",
4914 STRLENOF( SUFFIXMSTR "=" ) ) )
4919 val = c->argv[ i ] + STRLENOF( SUFFIXMSTR "=" );
4920 if ( si->si_suffixm.bv_val ) {
4921 ch_free( si->si_suffixm.bv_val );
4923 ber_str2bv( val, 0, 0, &bv );
4924 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_suffixm, NULL );
4925 if ( rc != LDAP_SUCCESS ) {
4926 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4927 "Invalid massage DN \"%s\": %d (%s)",
4928 val, rc, ldap_err2string( rc ) );
4929 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4932 if ( !be_issubordinate( c->be, &si->si_suffixm )) {
4933 ch_free( si->si_suffixm.bv_val );
4934 BER_BVZERO( &si->si_suffixm );
4935 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4936 "Massage DN \"%s\" is not within the database naming context",
4938 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4941 si->si_got |= GOT_SUFFIXM;
4943 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
4944 STRLENOF( LOGBASESTR "=" ) ) )
4949 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
4950 if ( si->si_logbase.bv_val ) {
4951 ch_free( si->si_logbase.bv_val );
4953 ber_str2bv( val, 0, 0, &bv );
4954 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
4955 if ( rc != LDAP_SUCCESS ) {
4956 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4957 "Invalid logbase DN \"%s\": %d (%s)",
4958 val, rc, ldap_err2string( rc ) );
4959 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4962 si->si_got |= GOT_LOGBASE;
4963 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
4964 STRLENOF( SCOPESTR "=" ) ) )
4967 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
4968 j = ldap_pvt_str2scope( val );
4970 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4971 "Error: parse_syncrepl_line: "
4972 "unknown scope \"%s\"", val);
4973 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4977 si->si_got |= GOT_SCOPE;
4978 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
4979 STRLENOF( ATTRSONLYSTR ) ) )
4981 si->si_attrsonly = 1;
4982 si->si_got |= GOT_ATTRSONLY;
4983 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
4984 STRLENOF( ATTRSSTR "=" ) ) )
4986 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
4987 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
4989 attr_fname = ch_strdup( val + STRLENOF(":include:") );
4990 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
4991 if ( si->si_anlist == NULL ) {
4992 ch_free( attr_fname );
4995 si->si_anfile = attr_fname;
4997 char *str, *s, *next;
4998 const char *delimstr = " ,\t";
4999 str = ch_strdup( val );
5000 for ( s = ldap_pvt_strtok( str, delimstr, &next );
5002 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
5004 if ( strlen(s) == 1 && *s == '*' ) {
5005 si->si_allattrs = 1;
5006 val[ s - str ] = delimstr[0];
5008 if ( strlen(s) == 1 && *s == '+' ) {
5009 si->si_allopattrs = 1;
5010 val [ s - str ] = delimstr[0];
5014 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
5015 if ( si->si_anlist == NULL ) {
5019 si->si_got |= GOT_ATTRS;
5020 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
5021 STRLENOF( EXATTRSSTR "=" ) ) )
5023 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
5024 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
5026 attr_fname = ch_strdup( val + STRLENOF(":include:") );
5027 si->si_exanlist = file2anlist(
5028 si->si_exanlist, attr_fname, " ,\t" );
5029 if ( si->si_exanlist == NULL ) {
5030 ch_free( attr_fname );
5033 ch_free( attr_fname );
5035 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
5036 if ( si->si_exanlist == NULL ) {
5040 si->si_got |= GOT_EXATTRS;
5041 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
5042 STRLENOF( TYPESTR "=" ) ) )
5044 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
5045 if ( !strncasecmp( val, "refreshOnly",
5046 STRLENOF("refreshOnly") ) )
5048 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
5049 } else if ( !strncasecmp( val, "refreshAndPersist",
5050 STRLENOF("refreshAndPersist") ) )
5052 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
5053 si->si_interval = 60;
5055 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5056 "Error: parse_syncrepl_line: "
5057 "unknown sync type \"%s\"", val);
5058 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5061 si->si_got |= GOT_TYPE;
5062 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
5063 STRLENOF( INTERVALSTR "=" ) ) )
5065 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
5066 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
5067 si->si_interval = 0;
5068 } else if ( strchr( val, ':' ) != NULL ) {
5069 char *next, *ptr = val;
5072 dd = strtol( ptr, &next, 10 );
5073 if ( next == ptr || next[0] != ':' || dd < 0 ) {
5074 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5075 "Error: parse_syncrepl_line: "
5076 "invalid interval \"%s\", unable to parse days", val );
5077 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5081 hh = strtol( ptr, &next, 10 );
5082 if ( next == ptr || next[0] != ':' || hh < 0 || hh > 24 ) {
5083 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5084 "Error: parse_syncrepl_line: "
5085 "invalid interval \"%s\", unable to parse hours", val );
5086 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5090 mm = strtol( ptr, &next, 10 );
5091 if ( next == ptr || next[0] != ':' || mm < 0 || mm > 60 ) {
5092 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5093 "Error: parse_syncrepl_line: "
5094 "invalid interval \"%s\", unable to parse minutes", val );
5095 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5099 ss = strtol( ptr, &next, 10 );
5100 if ( next == ptr || next[0] != '\0' || ss < 0 || ss > 60 ) {
5101 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5102 "Error: parse_syncrepl_line: "
5103 "invalid interval \"%s\", unable to parse seconds", val );
5104 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5107 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
5111 if ( lutil_parse_time( val, &t ) != 0 ) {
5112 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5113 "Error: parse_syncrepl_line: "
5114 "invalid interval \"%s\"", val );
5115 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5118 si->si_interval = (time_t)t;
5120 if ( si->si_interval < 0 ) {
5121 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5122 "Error: parse_syncrepl_line: "
5123 "invalid interval \"%ld\"",
5124 (long) si->si_interval);
5125 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5128 si->si_got |= GOT_INTERVAL;
5129 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
5130 STRLENOF( RETRYSTR "=" ) ) )
5132 if ( parse_syncrepl_retry( c, c->argv[ i ], si ) ) {
5135 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
5136 STRLENOF( MANAGEDSAITSTR "=" ) ) )
5138 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
5139 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
5140 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
5142 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5143 "invalid manageDSAit value \"%s\".\n",
5145 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5148 si->si_got |= GOT_MANAGEDSAIT;
5149 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
5150 STRLENOF( SLIMITSTR "=") ) )
5152 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
5153 if ( strcasecmp( val, "unlimited" ) == 0 ) {
5156 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
5157 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5158 "invalid size limit value \"%s\".\n",
5160 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5163 si->si_got |= GOT_SLIMIT;
5164 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
5165 STRLENOF( TLIMITSTR "=" ) ) )
5167 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
5168 if ( strcasecmp( val, "unlimited" ) == 0 ) {
5171 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
5172 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5173 "invalid time limit value \"%s\".\n",
5175 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5178 si->si_got |= GOT_TLIMIT;
5179 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
5180 STRLENOF( SYNCDATASTR "=" ) ) )
5182 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
5183 si->si_syncdata = verb_to_mask( val, datamodes );
5184 si->si_got |= GOT_SYNCDATA;
5185 } else if ( !strncasecmp( c->argv[ i ], STRICT_REFRESH,
5186 STRLENOF( STRICT_REFRESH ) ) )
5188 si->si_strict_refresh = 1;
5189 } else if ( !bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
5190 si->si_got |= GOT_BINDCONF;
5192 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5193 "Error: parse_syncrepl_line: "
5194 "unable to parse \"%s\"\n", c->argv[ i ] );
5195 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5200 if ( ( si->si_got & GOT_REQUIRED ) != GOT_REQUIRED ) {
5201 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5202 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
5203 si->si_got & GOT_RID ? "" : " "IDSTR,
5204 si->si_got & GOT_PROVIDER ? "" : " "PROVIDERSTR,
5205 si->si_got & GOT_SEARCHBASE ? "" : " "SEARCHBASESTR );
5206 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5210 if ( !be_issubordinate( c->be, &si->si_base ) && !( si->si_got & GOT_SUFFIXM )) {
5211 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5212 "Base DN \"%s\" is not within the database naming context",
5213 si->si_base.bv_val );
5214 ch_free( si->si_base.bv_val );
5215 BER_BVZERO( &si->si_base );
5216 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5220 #ifdef ENABLE_REWRITE
5221 if ( si->si_got & GOT_SUFFIXM ) {
5222 if (config_suffixm( c, si )) {
5223 ch_free( si->si_suffixm.bv_val );
5224 BER_BVZERO( &si->si_suffixm );
5225 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5226 "Error configuring rewrite engine" );
5227 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5233 if ( !( si->si_got & GOT_RETRY ) ) {
5234 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": no retry defined, using default\n",
5235 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", 0 );
5236 if ( si->si_retryinterval == NULL ) {
5237 if ( parse_syncrepl_retry( c, "retry=undefined", si ) ) {
5243 si->si_filter = str2filter( si->si_filterstr.bv_val );
5244 if ( si->si_filter == NULL ) {
5245 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": unable to parse filter=\"%s\"\n",
5246 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", si->si_filterstr.bv_val );
5260 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
5261 snprintf( c->cr_msg, sizeof(c->cr_msg), "database %s does not support "
5262 "operations required for syncrepl", c->be->be_type );
5263 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
5266 if ( BER_BVISEMPTY( &c->be->be_rootdn ) ) {
5267 strcpy( c->cr_msg, "rootDN must be defined before syncrepl may be used" );
5268 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
5271 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
5274 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
5278 si->si_bindconf.sb_tls = SB_TLS_OFF;
5279 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
5280 si->si_schemachecking = 0;
5281 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
5282 &si->si_filterstr );
5283 si->si_base.bv_val = NULL;
5284 si->si_scope = LDAP_SCOPE_SUBTREE;
5285 si->si_attrsonly = 0;
5286 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
5287 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
5288 si->si_attrs = NULL;
5289 si->si_allattrs = 0;
5290 si->si_allopattrs = 0;
5291 si->si_exattrs = NULL;
5292 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
5293 si->si_interval = 86400;
5294 si->si_retryinterval = NULL;
5295 si->si_retrynum_init = NULL;
5296 si->si_retrynum = NULL;
5297 si->si_manageDSAit = 0;
5301 si->si_presentlist = NULL;
5302 LDAP_LIST_INIT( &si->si_nonpresentlist );
5303 ldap_pvt_thread_mutex_init( &si->si_mutex );
5305 rc = parse_syncrepl_line( c, si );
5310 /* Must be LDAPv3 because we need controls */
5311 switch ( si->si_bindconf.sb_version ) {
5313 /* not explicitly set */
5314 si->si_bindconf.sb_version = LDAP_VERSION3;
5317 /* explicitly set */
5320 Debug( LDAP_DEBUG_ANY,
5321 "version %d incompatible with syncrepl\n",
5322 si->si_bindconf.sb_version, 0, 0 );
5323 syncinfo_free( si, 0 );
5327 if ( ldap_url_parse( si->si_bindconf.sb_uri.bv_val, &lud )) {
5328 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5329 "<%s> invalid URL", c->argv[0] );
5330 Debug( LDAP_DEBUG_ANY, "%s: %s %s\n",
5331 c->log, c->cr_msg, si->si_bindconf.sb_uri.bv_val );
5336 if ( slapMode & SLAP_SERVER_MODE ) {
5338 /* check if consumer points to current server and database.
5339 * If so, ignore this configuration.
5341 if ( !SLAP_DBHIDDEN( c->be ) ) {
5343 /* if searchbase doesn't match current DB suffix,
5344 * assume it's different
5346 for ( i=0; !BER_BVISNULL( &c->be->be_nsuffix[i] ); i++ ) {
5347 if ( bvmatch( &si->si_base, &c->be->be_nsuffix[i] )) {
5352 /* if searchbase matches, see if URLs match */
5353 if ( isMe && config_check_my_url( si->si_bindconf.sb_uri.bv_val,
5359 init_syncrepl( si );
5360 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
5361 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq,
5362 si->si_interval, do_syncrepl, si, "do_syncrepl",
5364 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
5366 rc = config_sync_shadow( c ) ? -1 : 0;
5371 /* mirrormode still needs to see this flag in tool mode */
5372 rc = config_sync_shadow( c ) ? -1 : 0;
5374 ldap_free_urldesc( lud );
5378 /* Use main slapd defaults */
5379 bindconf_tls_defaults( &si->si_bindconf );
5382 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
5383 syncinfo_free( si, 0 );
5386 Debug( LDAP_DEBUG_CONFIG,
5387 "Config: ** successfully added syncrepl %s \"%s\"\n",
5389 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
5390 "(null)" : si->si_bindconf.sb_uri.bv_val, 0 );
5391 if ( c->be->be_syncinfo ) {
5394 si->si_cookieState = c->be->be_syncinfo->si_cookieState;
5396 /* add new syncrepl to end of list (same order as when deleting) */
5397 for ( sip = c->be->be_syncinfo; sip->si_next; sip = sip->si_next );
5400 si->si_cookieState = ch_calloc( 1, sizeof( cookie_state ));
5401 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_mutex );
5402 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_pmutex );
5404 c->be->be_syncinfo = si;
5406 si->si_cookieState->cs_ref++;
5415 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
5417 struct berval bc, uri, bs;
5418 char buf[BUFSIZ*2], *ptr;
5421 # define WHATSLEFT ((ber_len_t) (&buf[sizeof( buf )] - ptr))
5425 /* temporarily inhibit bindconf from printing URI */
5426 uri = si->si_bindconf.sb_uri;
5427 BER_BVZERO( &si->si_bindconf.sb_uri );
5428 si->si_bindconf.sb_version = 0;
5429 bindconf_unparse( &si->si_bindconf, &bc );
5430 si->si_bindconf.sb_uri = uri;
5431 si->si_bindconf.sb_version = LDAP_VERSION3;
5434 assert( si->si_rid >= 0 && si->si_rid <= SLAP_SYNC_RID_MAX );
5435 len = snprintf( ptr, WHATSLEFT, IDSTR "=%03d " PROVIDERSTR "=%s",
5436 si->si_rid, si->si_bindconf.sb_uri.bv_val );
5437 if ( len >= sizeof( buf ) ) return;
5439 if ( !BER_BVISNULL( &bc ) ) {
5440 if ( WHATSLEFT <= bc.bv_len ) {
5444 ptr = lutil_strcopy( ptr, bc.bv_val );
5447 if ( !BER_BVISEMPTY( &si->si_filterstr ) ) {
5448 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
5449 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
5450 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
5453 if ( !BER_BVISNULL( &si->si_base ) ) {
5454 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
5455 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
5456 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
5459 #ifdef ENABLE_REWRITE
5460 if ( !BER_BVISNULL( &si->si_suffixm ) ) {
5461 if ( WHATSLEFT <= STRLENOF( " " SUFFIXMSTR "=\"" "\"" ) + si->si_suffixm.bv_len ) return;
5462 ptr = lutil_strcopy( ptr, " " SUFFIXMSTR "=\"" );
5463 ptr = lutil_strcopy( ptr, si->si_suffixm.bv_val );
5467 if ( !BER_BVISEMPTY( &si->si_logfilterstr ) ) {
5468 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
5469 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
5470 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
5473 if ( !BER_BVISNULL( &si->si_logbase ) ) {
5474 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
5475 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
5476 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
5479 if ( ldap_pvt_scope2bv( si->si_scope, &bs ) == LDAP_SUCCESS ) {
5480 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + bs.bv_len ) return;
5481 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
5482 ptr = lutil_strcopy( ptr, bs.bv_val );
5484 if ( si->si_attrsonly ) {
5485 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
5486 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
5488 if ( si->si_anfile ) {
5489 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
5490 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
5491 ptr = lutil_strcopy( ptr, si->si_anfile );
5493 } else if ( si->si_allattrs || si->si_allopattrs ||
5494 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ) )
5498 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
5499 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
5501 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
5502 if ( ptr == NULL ) return;
5503 if ( si->si_allattrs ) {
5504 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
5505 if ( old != ptr ) *ptr++ = ',';
5508 if ( si->si_allopattrs ) {
5509 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
5510 if ( old != ptr ) *ptr++ = ',';
5515 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
5516 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
5517 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
5518 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
5519 if ( ptr == NULL ) return;
5521 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
5522 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
5523 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
5525 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
5526 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
5527 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
5528 "refreshAndPersist" : "refreshOnly" );
5530 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
5533 dd = si->si_interval;
5540 len = snprintf( ptr, WHATSLEFT, " %s=%02d:%02d:%02d:%02d",
5541 INTERVALSTR, dd, hh, mm, ss );
5542 if ( len >= WHATSLEFT ) return;
5546 if ( si->si_got & GOT_RETRY ) {
5547 const char *space = "";
5548 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
5549 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
5550 for (i=0; si->si_retryinterval[i]; i++) {
5551 len = snprintf( ptr, WHATSLEFT, "%s%ld ", space,
5552 (long) si->si_retryinterval[i] );
5554 if ( WHATSLEFT - 1 <= len ) return;
5556 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
5559 len = snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
5560 if ( WHATSLEFT <= len ) return;
5564 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
5567 ptr = lutil_strcopy( ptr, " " RETRYSTR "=undefined" );
5570 if ( si->si_slimit ) {
5571 len = snprintf( ptr, WHATSLEFT, " " SLIMITSTR "=%d", si->si_slimit );
5572 if ( WHATSLEFT <= len ) return;
5576 if ( si->si_tlimit ) {
5577 len = snprintf( ptr, WHATSLEFT, " " TLIMITSTR "=%d", si->si_tlimit );
5578 if ( WHATSLEFT <= len ) return;
5582 if ( si->si_syncdata ) {
5583 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
5584 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
5585 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
5586 ptr = lutil_strcopy( ptr, bc.bv_val );
5589 bc.bv_len = ptr - buf;
5591 ber_dupbv( bv, &bc );
5595 syncrepl_config( ConfigArgs *c )
5597 if (c->op == SLAP_CONFIG_EMIT) {
5598 if ( c->be->be_syncinfo ) {
5602 for ( si = c->be->be_syncinfo; si; si=si->si_next ) {
5603 syncrepl_unparse( si, &bv );
5604 ber_bvarray_add( &c->rvalue_vals, &bv );
5609 } else if ( c->op == LDAP_MOD_DELETE ) {
5611 if ( c->be->be_syncinfo ) {
5612 syncinfo_t *si, **sip;
5615 for ( sip = &c->be->be_syncinfo, i=0; *sip; i++ ) {
5617 if ( c->valx == -1 || i == c->valx ) {
5621 /* If the task is currently active, we have to leave
5622 * it running. It will exit on its own. This will only
5623 * happen when running on the cn=config DB.
5626 if ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
5629 /* There is no active thread, but we must still
5630 * ensure that no thread is (or will be) queued
5631 * while we removes the task.
5633 struct re_s *re = si->si_re;
5636 if ( si->si_conn ) {
5637 connection_client_stop( si->si_conn );
5641 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
5642 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) ) {
5643 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
5646 if ( ldap_pvt_thread_pool_retract( &connection_pool,
5647 re->routine, re ) > 0 )
5650 ldap_pvt_runqueue_remove( &slapd_rq, re );
5651 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
5653 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
5657 syncinfo_free( si, 0 );
5666 if ( !c->be->be_syncinfo ) {
5667 SLAP_DBFLAGS( c->be ) &= ~SLAP_DBFLAG_SHADOW_MASK;
5671 if ( SLAP_SLURP_SHADOW( c->be ) ) {
5672 Debug(LDAP_DEBUG_ANY, "%s: "
5673 "syncrepl: database already shadowed.\n",
5677 return add_syncrepl( c );