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-2010 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"
34 struct nonpresent_entry {
35 struct berval *npe_name;
36 struct berval *npe_nname;
37 LDAP_LIST_ENTRY(nonpresent_entry) npe_link;
40 typedef struct cookie_state {
41 ldap_pvt_thread_mutex_t cs_mutex;
45 struct berval *cs_vals;
48 /* pending changes, not yet committed */
49 ldap_pvt_thread_mutex_t cs_pmutex;
51 struct berval *cs_pvals;
55 #define SYNCDATA_DEFAULT 0 /* entries are plain LDAP entries */
56 #define SYNCDATA_ACCESSLOG 1 /* entries are accesslog format */
57 #define SYNCDATA_CHANGELOG 2 /* entries are changelog format */
59 #define SYNCLOG_LOGGING 0 /* doing a log-based update */
60 #define SYNCLOG_FALLBACK 1 /* doing a full refresh */
62 #define RETRYNUM_FOREVER (-1) /* retry forever */
63 #define RETRYNUM_TAIL (-2) /* end of retrynum array */
64 #define RETRYNUM_VALID(n) ((n) >= RETRYNUM_FOREVER) /* valid retrynum */
65 #define RETRYNUM_FINITE(n) ((n) > RETRYNUM_FOREVER) /* not forever */
67 typedef struct syncinfo_s {
68 struct syncinfo_s *si_next;
73 char si_ridtxt[ STRLENOF("rid=999") + 1 ];
74 slap_bindconf si_bindconf;
75 struct berval si_base;
76 struct berval si_logbase;
77 struct berval si_filterstr;
79 struct berval si_logfilterstr;
80 struct berval si_contextdn;
84 AttributeName *si_anlist;
85 AttributeName *si_exanlist;
90 int si_schemachecking;
91 int si_type; /* the active type */
92 int si_ctype; /* the configured type */
94 time_t *si_retryinterval;
95 int *si_retrynum_init;
97 struct sync_cookie si_syncCookie;
98 cookie_state *si_cookieState;
103 int si_refreshDelete;
104 int si_refreshPresent;
110 Avlnode *si_presentlist;
113 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
114 ldap_pvt_thread_mutex_t si_mutex;
117 static int syncuuid_cmp( const void *, const void * );
118 static int avl_presentlist_insert( syncinfo_t* si, struct berval *syncUUID );
119 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray, struct sync_cookie *, int );
120 static int syncrepl_message_to_op(
121 syncinfo_t *, Operation *, LDAPMessage * );
122 static int syncrepl_message_to_entry(
123 syncinfo_t *, Operation *, LDAPMessage *,
124 Modifications **, Entry **, int );
125 static int syncrepl_entry(
126 syncinfo_t *, Operation*, Entry*,
127 Modifications**,int, struct berval*,
128 struct berval *cookieCSN );
129 static int syncrepl_updateCookie(
130 syncinfo_t *, Operation *,
131 struct sync_cookie * );
132 static struct berval * slap_uuidstr_from_normalized(
133 struct berval *, struct berval *, void * );
134 static int syncrepl_add_glue_ancestors(
135 Operation* op, Entry *e );
137 /* callback functions */
138 static int dn_callback( Operation *, SlapReply * );
139 static int nonpresent_callback( Operation *, SlapReply * );
140 static int null_callback( Operation *, SlapReply * );
142 static AttributeDescription *sync_descs[4];
145 syncrepl_state2str( int state )
148 case LDAP_SYNC_PRESENT:
154 case LDAP_SYNC_MODIFY:
157 case LDAP_SYNC_DELETE:
165 init_syncrepl(syncinfo_t *si)
168 char **attrs, **exattrs;
170 if ( !sync_descs[0] ) {
171 sync_descs[0] = slap_schema.si_ad_objectClass;
172 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
173 sync_descs[2] = slap_schema.si_ad_entryCSN;
174 sync_descs[3] = NULL;
177 if ( si->si_allattrs && si->si_allopattrs )
180 attrs = anlist2attrs( si->si_anlist );
183 if ( si->si_allattrs ) {
186 if ( !is_at_operational( at_find( attrs[i] ) ) ) {
187 for ( j = i; attrs[j] != NULL; j++ ) {
190 attrs[j] = attrs[j+1];
196 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
197 attrs[i] = ch_strdup("*");
200 } else if ( si->si_allopattrs ) {
203 if ( is_at_operational( at_find( attrs[i] ) ) ) {
204 for ( j = i; attrs[j] != NULL; j++ ) {
207 attrs[j] = attrs[j+1];
213 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
214 attrs[i] = ch_strdup("+");
218 for ( i = 0; sync_descs[i] != NULL; i++ ) {
221 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
222 for ( k = j; attrs[k] != NULL; k++ ) {
225 attrs[k] = attrs[k+1];
233 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
235 if ( si->si_allopattrs ) {
236 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ) );
238 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ) );
242 if ( si->si_allopattrs ) {
243 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
245 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
246 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
254 if ( si->si_allattrs == si->si_allopattrs ) {
255 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
256 attrs[i++] = ch_strdup( "*" );
257 attrs[i++] = ch_strdup( "+" );
258 } else if ( si->si_allattrs && !si->si_allopattrs ) {
259 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
260 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
261 attrs[i++] = ch_strdup( "*" );
262 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
263 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
265 } else if ( !si->si_allattrs && si->si_allopattrs ) {
266 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
267 attrs[i++] = ch_strdup( "+" );
268 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
273 si->si_attrs = attrs;
275 exattrs = anlist2attrs( si->si_exanlist );
278 for ( n = 0; exattrs[n] != NULL; n++ ) ;
280 for ( i = 0; sync_descs[i] != NULL; i++ ) {
282 while ( exattrs[j] != NULL ) {
283 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
284 ch_free( exattrs[j] );
285 for ( k = j; exattrs[k] != NULL; k++ ) {
286 exattrs[k] = exattrs[k+1];
294 for ( i = 0; exattrs[i] != NULL; i++ ) {
295 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
297 if ( ( oc = si->si_anlist[j].an_oc ) ) {
299 while ( oc->soc_required[k] ) {
300 if ( !strcmp( exattrs[i],
301 oc->soc_required[k]->sat_cname.bv_val ) ) {
302 ch_free( exattrs[i] );
303 for ( l = i; exattrs[l]; l++ ) {
304 exattrs[l] = exattrs[l+1];
314 for ( i = 0; exattrs[i] != NULL; i++ ) ;
317 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *) );
320 si->si_exattrs = exattrs;
323 typedef struct logschema {
325 struct berval ls_req;
326 struct berval ls_mod;
327 struct berval ls_newRdn;
328 struct berval ls_delRdn;
329 struct berval ls_newSup;
332 static logschema changelog_sc = {
334 BER_BVC("changeType"),
337 BER_BVC("deleteOldRDN"),
338 BER_BVC("newSuperior")
341 static logschema accesslog_sc = {
345 BER_BVC("reqNewRDN"),
346 BER_BVC("reqDeleteOldRDN"),
347 BER_BVC("reqNewSuperior")
355 BerElementBuffer berbuf;
356 BerElement *ber = (BerElement *)&berbuf;
357 LDAPControl c[3], *ctrls[4];
361 char **attrs, *lattrs[8];
366 /* setup LDAP SYNC control */
367 ber_init2( ber, NULL, LBER_USE_DER );
368 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
370 /* If we're using a log but we have no state, then fallback to
371 * normal mode for a full refresh.
373 if ( si->si_syncdata && !si->si_syncCookie.numcsns ) {
374 si->si_logstate = SYNCLOG_FALLBACK;
377 /* Use the log parameters if we're in log mode */
378 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
380 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
384 lattrs[0] = ls->ls_dn.bv_val;
385 lattrs[1] = ls->ls_req.bv_val;
386 lattrs[2] = ls->ls_mod.bv_val;
387 lattrs[3] = ls->ls_newRdn.bv_val;
388 lattrs[4] = ls->ls_delRdn.bv_val;
389 lattrs[5] = ls->ls_newSup.bv_val;
390 lattrs[6] = slap_schema.si_ad_entryCSN->ad_cname.bv_val;
394 base = si->si_logbase.bv_val;
395 filter = si->si_logfilterstr.bv_val;
398 scope = LDAP_SCOPE_SUBTREE;
401 base = si->si_base.bv_val;
402 filter = si->si_filterstr.bv_val;
403 attrs = si->si_attrs;
404 attrsonly = si->si_attrsonly;
405 scope = si->si_scope;
407 if ( si->si_syncdata && si->si_logstate == SYNCLOG_FALLBACK ) {
408 si->si_type = LDAP_SYNC_REFRESH_ONLY;
410 si->si_type = si->si_ctype;
413 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
415 ber_printf( ber, "{eOb}",
416 abs(si->si_type), &si->si_syncCookie.octet_str, rhint );
418 ber_printf( ber, "{eb}",
419 abs(si->si_type), rhint );
422 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 ) ) == -1 ) {
427 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
428 c[0].ldctl_iscritical = si->si_type < 0;
431 c[1].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
432 BER_BVZERO( &c[1].ldctl_value );
433 c[1].ldctl_iscritical = 1;
436 if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
437 c[2].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
438 c[2].ldctl_value = si->si_bindconf.sb_authzId;
439 c[2].ldctl_iscritical = 1;
446 rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
447 ctrls, NULL, NULL, si->si_slimit, &si->si_msgid );
461 int i, j, changed = 0;
463 /* Look for contextCSN from syncprov overlay. If
464 * there's no overlay, this will be a no-op. That means
465 * this is a pure consumer, so local changes will not be
466 * allowed, and all changes will already be reflected in
469 a.a_desc = slap_schema.si_ad_contextCSN;
471 e.e_name = si->si_contextdn;
472 e.e_nname = si->si_contextdn;
473 at[0].an_name = a.a_desc->ad_cname;
474 at[0].an_desc = a.a_desc;
475 BER_BVZERO( &at[1].an_name );
477 rs.sr_flags = REP_ENTRY_MODIFIABLE;
479 op->o_req_dn = e.e_name;
480 op->o_req_ndn = e.e_nname;
482 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
483 i = backend_operational( op, &rs );
484 if ( i == LDAP_SUCCESS && a.a_nvals ) {
485 int num = a.a_numvals;
486 /* check for differences */
487 if ( num != si->si_cookieState->cs_num ) {
490 for ( i=0; i<num; i++ ) {
491 if ( ber_bvcmp( &a.a_nvals[i],
492 &si->si_cookieState->cs_vals[i] )) {
499 ber_bvarray_free( si->si_cookieState->cs_vals );
500 ch_free( si->si_cookieState->cs_sids );
501 si->si_cookieState->cs_num = num;
502 si->si_cookieState->cs_vals = a.a_nvals;
503 si->si_cookieState->cs_sids = slap_parse_csn_sids( a.a_nvals,
505 si->si_cookieState->cs_age++;
507 ber_bvarray_free( a.a_nvals );
509 ber_bvarray_free( a.a_vals );
511 /* See if the cookieState has changed due to anything outside
512 * this particular consumer. That includes other consumers in
513 * the same context, or local changes detected above.
515 if ( si->si_cookieState->cs_num > 0 && si->si_cookieAge !=
516 si->si_cookieState->cs_age ) {
517 if ( !si->si_syncCookie.numcsns ) {
518 ber_bvarray_free( si->si_syncCookie.ctxcsn );
519 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
520 si->si_cookieState->cs_vals, NULL );
523 for (i=0; !BER_BVISNULL( &si->si_syncCookie.ctxcsn[i] ); i++) {
524 /* bogus, just dup everything */
525 if ( si->si_syncCookie.sids[i] == -1 ) {
526 ber_bvarray_free( si->si_syncCookie.ctxcsn );
527 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
528 si->si_cookieState->cs_vals, NULL );
532 for (j=0; j<si->si_cookieState->cs_num; j++) {
533 if ( si->si_syncCookie.sids[i] !=
534 si->si_cookieState->cs_sids[j] )
536 if ( bvmatch( &si->si_syncCookie.ctxcsn[i],
537 &si->si_cookieState->cs_vals[j] ))
539 ber_bvreplace( &si->si_syncCookie.ctxcsn[i],
540 &si->si_cookieState->cs_vals[j] );
548 si->si_cookieAge = si->si_cookieState->cs_age;
549 ch_free( si->si_syncCookie.octet_str.bv_val );
550 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
551 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
552 si->si_syncCookie.sid );
554 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
564 int cmdline_cookie_found = 0;
566 struct sync_cookie *sc = NULL;
571 rc = slap_client_connect( &si->si_ld, &si->si_bindconf );
572 if ( rc != LDAP_SUCCESS ) {
575 op->o_protocol = LDAP_VERSION3;
577 /* Set SSF to strongest of TLS, SASL SSFs */
580 op->o_transport_ssf = 0;
582 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
583 == LDAP_SUCCESS && ssl != NULL )
585 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
587 #endif /* HAVE_TLS */
589 ber_len_t ssf; /* ITS#5403, 3864 LDAP_OPT_X_SASL_SSF probably ought
590 to use sasl_ssf_t but currently uses ber_len_t */
591 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &ssf )
593 op->o_sasl_ssf = ssf;
595 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
596 ? op->o_sasl_ssf : op->o_tls_ssf;
598 ldap_set_option( si->si_ld, LDAP_OPT_TIMELIMIT, &si->si_tlimit );
600 rc = LDAP_DEREF_NEVER; /* actually could allow DEREF_FINDING */
601 ldap_set_option( si->si_ld, LDAP_OPT_DEREF, &rc );
603 ldap_set_option( si->si_ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
605 si->si_syncCookie.rid = si->si_rid;
607 /* whenever there are multiple data sources possible, advertise sid */
608 si->si_syncCookie.sid = ( SLAP_MULTIMASTER( si->si_be ) || si->si_be != si->si_wbe ) ?
611 /* We've just started up, or the remote server hasn't sent us
612 * any meaningful state.
614 if ( BER_BVISNULL( &si->si_syncCookie.octet_str ) ) {
617 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
618 if ( si->si_rid == sc->rid ) {
619 cmdline_cookie_found = 1;
624 if ( cmdline_cookie_found ) {
625 /* cookie is supplied in the command line */
627 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
629 /* ctxcsn wasn't parsed yet, do it now */
630 slap_parse_sync_cookie( sc, op->o_tmpmemctx );
631 slap_sync_cookie_free( &si->si_syncCookie, 0 );
632 slap_dup_sync_cookie( &si->si_syncCookie, sc );
633 slap_sync_cookie_free( sc, 1 );
635 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
636 if ( !si->si_cookieState->cs_num ) {
637 /* get contextCSN shadow replica from database */
638 BerVarray csn = NULL;
639 void *ctx = op->o_tmpmemctx;
641 op->o_req_ndn = si->si_contextdn;
642 op->o_req_dn = op->o_req_ndn;
644 /* try to read stored contextCSN */
645 op->o_tmpmemctx = NULL;
646 backend_attribute( op, NULL, &op->o_req_ndn,
647 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
648 op->o_tmpmemctx = ctx;
650 si->si_cookieState->cs_vals = csn;
651 for (i=0; !BER_BVISNULL( &csn[i] ); i++);
652 si->si_cookieState->cs_num = i;
653 si->si_cookieState->cs_sids = slap_parse_csn_sids( csn, i, NULL );
656 if ( si->si_cookieState->cs_num ) {
657 ber_bvarray_free( si->si_syncCookie.ctxcsn );
658 if ( ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
659 si->si_cookieState->cs_vals, NULL )) {
661 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
664 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
665 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num *
667 for ( i=0; i<si->si_syncCookie.numcsns; i++ )
668 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
670 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
673 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
674 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
675 si->si_syncCookie.sid );
677 /* ITS#6367: recreate the cookie so it has our SID, not our peer's */
678 ch_free( si->si_syncCookie.octet_str.bv_val );
679 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
680 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
681 si->si_syncCookie.sid );
682 /* Look for contextCSN from syncprov overlay. */
683 check_syncprov( op, si );
686 si->si_refreshDone = 0;
688 rc = ldap_sync_search( si, op->o_tmpmemctx );
690 if( rc != LDAP_SUCCESS ) {
691 Debug( LDAP_DEBUG_ANY, "do_syncrep1: %s "
692 "ldap_search_ext: %s (%d)\n",
693 si->si_ridtxt, ldap_err2string( rc ), rc );
699 ldap_unbind_ext( si->si_ld, NULL, NULL );
708 compare_csns( struct sync_cookie *sc1, struct sync_cookie *sc2, int *which )
715 if ( sc1->numcsns < sc2->numcsns ) {
716 *which = sc1->numcsns;
720 for (j=0; j<sc2->numcsns; j++) {
721 for (i=0; i<sc1->numcsns; i++) {
722 if ( sc1->sids[i] != sc2->sids[j] )
724 value_match( &match, slap_schema.si_ad_entryCSN,
725 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
726 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
727 &sc1->ctxcsn[i], &sc2->ctxcsn[j], &text );
734 if ( i == sc1->numcsns ) {
735 /* sc2 has a sid sc1 lacks */
743 #define SYNC_PAUSED -3
750 LDAPControl **rctrls = NULL;
752 BerElementBuffer berbuf;
753 BerElement *ber = (BerElement *)&berbuf;
755 LDAPMessage *msg = NULL;
758 struct berval *retdata = NULL;
763 struct berval syncUUID = BER_BVNULL;
764 struct sync_cookie syncCookie = { NULL };
765 struct sync_cookie syncCookie_req = { NULL };
766 struct berval cookie = BER_BVNULL;
772 Modifications *modlist = NULL;
774 int match, m, punlock = -1;
776 struct timeval *tout_p = NULL;
777 struct timeval tout = { 0, 0 };
779 int refreshDeletes = 0;
780 BerVarray syncUUIDs = NULL;
783 if ( slapd_shutdown ) {
788 ber_init2( ber, NULL, LBER_USE_DER );
789 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
791 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2 %s\n", si->si_ridtxt, 0, 0 );
793 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
795 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
801 while ( ( rc = ldap_result( si->si_ld, si->si_msgid, LDAP_MSG_ONE,
802 tout_p, &msg ) ) > 0 )
804 LDAPControl *rctrlp = NULL;
806 if ( slapd_shutdown ) {
810 switch( ldap_msgtype( msg ) ) {
811 case LDAP_RES_SEARCH_ENTRY:
812 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
813 /* we can't work without the control */
815 LDAPControl **next = NULL;
816 /* NOTE: make sure we use the right one;
817 * a better approach would be to run thru
818 * the whole list and take care of all */
819 /* NOTE: since we issue the search request,
820 * we should know what controls to expect,
821 * and there should be none apart from the
822 * sync-related control */
823 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_STATE, rctrls, &next );
824 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_STATE, next, NULL ) )
826 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
827 "got search entry with multiple "
828 "Sync State control\n", si->si_ridtxt, 0, 0 );
829 ldap_controls_free( rctrls );
834 if ( rctrlp == NULL ) {
835 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
836 "got search entry without "
837 "Sync State control\n", si->si_ridtxt, 0, 0 );
841 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
842 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
843 /* FIXME: what if syncUUID is NULL or empty?
844 * (happens with back-sql...) */
845 if ( BER_BVISEMPTY( &syncUUID ) ) {
846 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
847 "got empty syncUUID with LDAP_SYNC_%s\n",
849 syncrepl_state2str( syncstate ), 0 );
850 ldap_controls_free( rctrls );
854 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
855 ber_scanf( ber, /*"{"*/ "m}", &cookie );
857 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
859 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
861 if ( !BER_BVISNULL( &cookie ) ) {
862 ch_free( syncCookie.octet_str.bv_val );
863 ber_dupbv( &syncCookie.octet_str, &cookie );
865 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
867 slap_parse_sync_cookie( &syncCookie, NULL );
868 if ( syncCookie.ctxcsn ) {
869 int i, sid = slap_parse_csn_sid( syncCookie.ctxcsn );
870 check_syncprov( op, si );
871 for ( i =0; i<si->si_cookieState->cs_num; i++ ) {
872 if ( si->si_cookieState->cs_sids[i] == sid ) {
873 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_vals[i] ) <= 0 ) {
874 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN too old, ignoring %s\n",
875 si->si_ridtxt, syncCookie.ctxcsn->bv_val, 0 );
876 ldap_controls_free( rctrls );
883 /* check pending CSNs too */
884 while ( ldap_pvt_thread_mutex_trylock( &si->si_cookieState->cs_pmutex )) {
885 if ( slapd_shutdown ) {
889 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
890 ldap_pvt_thread_yield();
892 for ( i =0; i<si->si_cookieState->cs_pnum; i++ ) {
893 if ( si->si_cookieState->cs_psids[i] == sid ) {
894 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_pvals[i] ) <= 0 ) {
895 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN pending, ignoring %s\n",
896 si->si_ridtxt, syncCookie.ctxcsn->bv_val, 0 );
897 ldap_controls_free( rctrls );
899 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
902 ber_bvreplace( &si->si_cookieState->cs_pvals[i],
907 /* new SID, add it */
908 if ( i == si->si_cookieState->cs_pnum ) {
909 value_add( &si->si_cookieState->cs_pvals, syncCookie.ctxcsn );
910 si->si_cookieState->cs_pnum++;
911 si->si_cookieState->cs_psids = ch_realloc( si->si_cookieState->cs_psids, si->si_cookieState->cs_pnum * sizeof(int));
912 si->si_cookieState->cs_psids[i] = sid;
916 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
920 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
922 if ( ( rc = syncrepl_message_to_op( si, op, msg ) ) == LDAP_SUCCESS &&
925 rc = syncrepl_updateCookie( si, op, &syncCookie );
926 } else switch ( rc ) {
927 case LDAP_ALREADY_EXISTS:
928 case LDAP_NO_SUCH_OBJECT:
929 case LDAP_NO_SUCH_ATTRIBUTE:
930 case LDAP_TYPE_OR_VALUE_EXISTS:
931 rc = LDAP_SYNC_REFRESH_REQUIRED;
932 si->si_logstate = SYNCLOG_FALLBACK;
933 ldap_abandon_ext( si->si_ld, si->si_msgid, NULL, NULL );
938 } else if ( ( rc = syncrepl_message_to_entry( si, op, msg,
939 &modlist, &entry, syncstate ) ) == LDAP_SUCCESS )
941 if ( ( rc = syncrepl_entry( si, op, entry, &modlist,
942 syncstate, &syncUUID, syncCookie.ctxcsn ) ) == LDAP_SUCCESS &&
945 rc = syncrepl_updateCookie( si, op, &syncCookie );
948 if ( punlock >= 0 ) {
949 /* on failure, revert pending CSN */
950 if ( rc != LDAP_SUCCESS ) {
952 for ( i = 0; i<si->si_cookieState->cs_num; i++ ) {
953 if ( si->si_cookieState->cs_sids[i] == si->si_cookieState->cs_psids[punlock] ) {
954 ber_bvreplace( &si->si_cookieState->cs_pvals[punlock],
955 &si->si_cookieState->cs_vals[i] );
959 if ( i == si->si_cookieState->cs_num )
960 si->si_cookieState->cs_pvals[punlock].bv_val[0] = '\0';
962 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
964 ldap_controls_free( rctrls );
966 slap_mods_free( modlist, 1 );
972 case LDAP_RES_SEARCH_REFERENCE:
973 Debug( LDAP_DEBUG_ANY,
974 "do_syncrep2: %s reference received error\n",
975 si->si_ridtxt, 0, 0 );
978 case LDAP_RES_SEARCH_RESULT:
979 Debug( LDAP_DEBUG_SYNC,
980 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT\n",
981 si->si_ridtxt, 0, 0 );
982 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
984 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
985 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
986 /* map old result code to registered code */
987 err = LDAP_SYNC_REFRESH_REQUIRED;
990 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
991 if ( si->si_logstate == SYNCLOG_LOGGING ) {
992 si->si_logstate = SYNCLOG_FALLBACK;
998 Debug( LDAP_DEBUG_ANY,
999 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT (%d) %s\n",
1000 si->si_ridtxt, err, ldap_err2string( err ) );
1003 LDAPControl **next = NULL;
1004 /* NOTE: make sure we use the right one;
1005 * a better approach would be to run thru
1006 * the whole list and take care of all */
1007 /* NOTE: since we issue the search request,
1008 * we should know what controls to expect,
1009 * and there should be none apart from the
1010 * sync-related control */
1011 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_DONE, rctrls, &next );
1012 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_DONE, next, NULL ) )
1014 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1015 "got search result with multiple "
1016 "Sync State control\n", si->si_ridtxt, 0, 0 );
1017 ldap_controls_free( rctrls );
1023 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
1025 ber_scanf( ber, "{" /*"}"*/);
1026 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
1027 ber_scanf( ber, "m", &cookie );
1029 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1031 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1033 if ( !BER_BVISNULL( &cookie ) ) {
1034 ch_free( syncCookie.octet_str.bv_val );
1035 ber_dupbv( &syncCookie.octet_str, &cookie);
1037 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1039 slap_parse_sync_cookie( &syncCookie, NULL );
1040 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1043 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
1045 ber_scanf( ber, "b", &refreshDeletes );
1047 ber_scanf( ber, /*"{"*/ "}" );
1049 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1050 slap_sync_cookie_free( &syncCookie_req, 0 );
1051 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1053 if ( !syncCookie.ctxcsn ) {
1055 } else if ( !syncCookie_req.ctxcsn ) {
1059 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1062 ldap_controls_free( rctrls );
1064 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
1065 /* FIXME : different error behaviors according to
1066 * 1) err code : LDAP_BUSY ...
1067 * 2) on err policy : stop service, stop sync, retry
1069 if ( refreshDeletes == 0 && match < 0 &&
1070 err == LDAP_SUCCESS &&
1071 syncCookie_req.numcsns == syncCookie.numcsns )
1073 syncrepl_del_nonpresent( op, si, NULL,
1076 avl_free( si->si_presentlist, ch_free );
1077 si->si_presentlist = NULL;
1080 if ( syncCookie.ctxcsn && match < 0 && err == LDAP_SUCCESS )
1082 rc = syncrepl_updateCookie( si, op, &syncCookie );
1084 if ( err == LDAP_SUCCESS
1085 && si->si_logstate == SYNCLOG_FALLBACK ) {
1086 si->si_logstate = SYNCLOG_LOGGING;
1087 rc = LDAP_SYNC_REFRESH_REQUIRED;
1094 case LDAP_RES_INTERMEDIATE:
1095 rc = ldap_parse_intermediate( si->si_ld, msg,
1096 &retoid, &retdata, NULL, 0 );
1097 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
1098 ber_init2( ber, retdata, LBER_USE_DER );
1100 switch ( si_tag = ber_peek_tag( ber, &len ) ) {
1102 case LDAP_TAG_SYNC_NEW_COOKIE:
1103 Debug( LDAP_DEBUG_SYNC,
1104 "do_syncrep2: %s %s - %s\n",
1106 "LDAP_RES_INTERMEDIATE",
1108 ber_scanf( ber, "tm", &tag, &cookie );
1109 Debug( LDAP_DEBUG_SYNC,
1110 "do_syncrep2: %s NEW_COOKIE: %s\n",
1113 if ( !BER_BVISNULL( &cookie ) ) {
1114 ch_free( syncCookie.octet_str.bv_val );
1115 ber_dupbv( &syncCookie.octet_str, &cookie );
1117 if (!BER_BVISNULL( &syncCookie.octet_str ) ) {
1118 slap_parse_sync_cookie( &syncCookie, NULL );
1119 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1122 case LDAP_TAG_SYNC_REFRESH_DELETE:
1123 case LDAP_TAG_SYNC_REFRESH_PRESENT:
1124 Debug( LDAP_DEBUG_SYNC,
1125 "do_syncrep2: %s %s - %s\n",
1127 "LDAP_RES_INTERMEDIATE",
1128 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
1129 "REFRESH_PRESENT" : "REFRESH_DELETE" );
1130 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
1131 si->si_refreshDelete = 1;
1133 si->si_refreshPresent = 1;
1135 ber_scanf( ber, "t{" /*"}"*/, &tag );
1136 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
1138 ber_scanf( ber, "m", &cookie );
1140 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1142 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1144 if ( !BER_BVISNULL( &cookie ) ) {
1145 ch_free( syncCookie.octet_str.bv_val );
1146 ber_dupbv( &syncCookie.octet_str, &cookie );
1148 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1150 slap_parse_sync_cookie( &syncCookie, NULL );
1151 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1154 /* Defaults to TRUE */
1155 if ( ber_peek_tag( ber, &len ) ==
1156 LDAP_TAG_REFRESHDONE )
1158 ber_scanf( ber, "b", &si->si_refreshDone );
1161 si->si_refreshDone = 1;
1163 ber_scanf( ber, /*"{"*/ "}" );
1165 case LDAP_TAG_SYNC_ID_SET:
1166 Debug( LDAP_DEBUG_SYNC,
1167 "do_syncrep2: %s %s - %s\n",
1169 "LDAP_RES_INTERMEDIATE",
1171 ber_scanf( ber, "t{" /*"}"*/, &tag );
1172 if ( ber_peek_tag( ber, &len ) ==
1173 LDAP_TAG_SYNC_COOKIE )
1175 ber_scanf( ber, "m", &cookie );
1177 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1179 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1181 if ( !BER_BVISNULL( &cookie ) ) {
1182 ch_free( syncCookie.octet_str.bv_val );
1183 ber_dupbv( &syncCookie.octet_str, &cookie );
1185 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1187 slap_parse_sync_cookie( &syncCookie, NULL );
1188 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1189 compare_csns( &syncCookie_req, &syncCookie, &m );
1192 if ( ber_peek_tag( ber, &len ) ==
1193 LDAP_TAG_REFRESHDELETES )
1195 ber_scanf( ber, "b", &refreshDeletes );
1197 ber_scanf( ber, "[W]", &syncUUIDs );
1198 ber_scanf( ber, /*"{"*/ "}" );
1199 if ( refreshDeletes ) {
1200 syncrepl_del_nonpresent( op, si, syncUUIDs,
1202 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
1205 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
1206 (void)avl_presentlist_insert( si, &syncUUIDs[i] );
1207 slap_sl_free( syncUUIDs[i].bv_val, op->o_tmpmemctx );
1209 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
1211 slap_sync_cookie_free( &syncCookie, 0 );
1214 Debug( LDAP_DEBUG_ANY,
1215 "do_syncrep2: %s unknown syncinfo tag (%ld)\n",
1216 si->si_ridtxt, (long) si_tag, 0 );
1217 ldap_memfree( retoid );
1218 ber_bvfree( retdata );
1222 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1223 slap_sync_cookie_free( &syncCookie_req, 0 );
1224 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1226 if ( !syncCookie.ctxcsn ) {
1228 } else if ( !syncCookie_req.ctxcsn ) {
1232 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1236 if ( si->si_refreshPresent == 1 &&
1237 si_tag != LDAP_TAG_SYNC_NEW_COOKIE &&
1238 syncCookie_req.numcsns == syncCookie.numcsns ) {
1239 syncrepl_del_nonpresent( op, si, NULL,
1243 if ( syncCookie.ctxcsn )
1245 rc = syncrepl_updateCookie( si, op, &syncCookie);
1249 ldap_memfree( retoid );
1250 ber_bvfree( retdata );
1254 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1255 "unknown intermediate response (%d)\n",
1256 si->si_ridtxt, rc, 0 );
1257 ldap_memfree( retoid );
1258 ber_bvfree( retdata );
1264 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1265 "unknown message (0x%02lx)\n",
1267 (unsigned long)ldap_msgtype( msg ), 0 );
1271 if ( !BER_BVISNULL( &syncCookie.octet_str ) ) {
1272 slap_sync_cookie_free( &syncCookie_req, 0 );
1273 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
1274 slap_sync_cookie_free( &syncCookie, 0 );
1276 ldap_msgfree( msg );
1278 if ( ldap_pvt_thread_pool_pausing( &connection_pool )) {
1279 slap_sync_cookie_free( &syncCookie, 0 );
1280 slap_sync_cookie_free( &syncCookie_req, 0 );
1286 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
1291 if ( err != LDAP_SUCCESS ) {
1292 Debug( LDAP_DEBUG_ANY,
1293 "do_syncrep2: %s (%d) %s\n",
1294 si->si_ridtxt, err, ldap_err2string( err ) );
1297 slap_sync_cookie_free( &syncCookie, 0 );
1298 slap_sync_cookie_free( &syncCookie_req, 0 );
1300 if ( msg ) ldap_msgfree( msg );
1302 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1303 if ( si->si_conn ) {
1304 connection_client_stop( si->si_conn );
1307 ldap_unbind_ext( si->si_ld, NULL, NULL );
1319 struct re_s* rtask = arg;
1320 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1321 Connection conn = {0};
1322 OperationBuffer opbuf;
1324 int rc = LDAP_SUCCESS;
1327 int i, defer = 1, fail = 0, freeinfo = 0;
1332 if ( slapd_shutdown )
1335 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl %s\n", si->si_ridtxt, 0, 0 );
1337 /* Don't get stuck here while a pause is initiated */
1338 while ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
1339 if ( slapd_shutdown )
1341 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
1342 ldap_pvt_thread_yield();
1345 if ( si->si_ctype < 1 ) {
1349 switch( abs( si->si_type ) ) {
1350 case LDAP_SYNC_REFRESH_ONLY:
1351 case LDAP_SYNC_REFRESH_AND_PERSIST:
1354 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1358 if ( slapd_shutdown ) {
1360 if ( si->si_conn ) {
1361 connection_client_stop( si->si_conn );
1364 ldap_unbind_ext( si->si_ld, NULL, NULL );
1367 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1371 connection_fake_init( &conn, &opbuf, ctx );
1373 /* o_connids must be unique for slap_graduate_commit_csn */
1374 op->o_connid = SLAPD_SYNC_RID2SYNCCONN(si->si_rid);
1376 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1379 /* Coordinate contextCSN updates with any syncprov overlays
1380 * in use. This may be complicated by the use of the glue
1383 * Typically there is a single syncprov mastering the entire
1384 * glued tree. In that case, our contextCSN updates should
1385 * go to the master DB. But if there is no syncprov on the
1386 * master DB, then nothing special is needed here.
1388 * Alternatively, there may be individual syncprov overlays
1389 * on each glued branch. In that case, each syncprov only
1390 * knows about changes within its own branch. And so our
1391 * contextCSN updates should only go to the local DB.
1393 if ( !si->si_wbe ) {
1394 if ( SLAP_GLUE_SUBORDINATE( be ) && !overlay_is_inst( be, "syncprov" )) {
1395 BackendDB * top_be = select_backend( &be->be_nsuffix[0], 1 );
1396 if ( overlay_is_inst( top_be, "syncprov" ))
1397 si->si_wbe = top_be;
1403 if ( SLAP_SYNC_SUBENTRY( si->si_wbe )) {
1404 build_new_dn( &si->si_contextdn, &si->si_wbe->be_nsuffix[0],
1405 (struct berval *)&slap_ldapsync_cn_bv, NULL );
1407 si->si_contextdn = si->si_wbe->be_nsuffix[0];
1410 if ( !si->si_schemachecking )
1411 op->o_no_schema_check = 1;
1413 /* Establish session, do search */
1415 si->si_refreshDelete = 0;
1416 si->si_refreshPresent = 0;
1418 /* use main DB when retrieving contextCSN */
1419 op->o_bd = si->si_wbe;
1420 op->o_dn = op->o_bd->be_rootdn;
1421 op->o_ndn = op->o_bd->be_rootndn;
1422 rc = do_syncrep1( op, si );
1426 /* Process results */
1427 if ( rc == LDAP_SUCCESS ) {
1428 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1430 /* use current DB */
1432 op->o_dn = op->o_bd->be_rootdn;
1433 op->o_ndn = op->o_bd->be_rootndn;
1434 rc = do_syncrep2( op, si );
1435 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1436 rc = ldap_sync_search( si, op->o_tmpmemctx );
1441 /* We got deleted while running on cn=config */
1442 if ( si->si_ctype < 1 ) {
1443 if ( si->si_ctype == -1 ) {
1452 if ( rc != SYNC_PAUSED ) {
1453 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1454 /* If we succeeded, enable the connection for further listening.
1455 * If we failed, tear down the connection and reschedule.
1457 if ( rc == LDAP_SUCCESS ) {
1458 if ( si->si_conn ) {
1459 connection_client_enable( si->si_conn );
1461 si->si_conn = connection_client_setup( s, do_syncrepl, arg );
1463 } else if ( si->si_conn ) {
1467 if ( rc == -2 ) rc = 0;
1472 /* At this point, we have 5 cases:
1473 * 1) for any hard failure, give up and remove this task
1474 * 2) for ServerDown, reschedule this task to run later
1475 * 3) for threadpool pause, reschedule to run immediately
1476 * 4) for Refresh and Success, reschedule to run
1477 * 5) for Persist and Success, reschedule to defer
1479 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1481 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask ) ) {
1482 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1486 connection_client_stop( si->si_conn );
1490 if ( rc == SYNC_PAUSED ) {
1491 rtask->interval.tv_sec = 0;
1492 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1493 rtask->interval.tv_sec = si->si_interval;
1495 } else if ( rc == LDAP_SUCCESS ) {
1496 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1499 rtask->interval.tv_sec = si->si_interval;
1500 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1501 if ( si->si_retrynum ) {
1502 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1503 si->si_retrynum[i] = si->si_retrynum_init[i];
1505 si->si_retrynum[i] = RETRYNUM_TAIL;
1508 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1509 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1513 if ( si->si_ctype < 1
1514 || !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1516 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1519 fail = RETRYNUM_TAIL;
1520 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1521 if ( si->si_retrynum[i] > 0 )
1522 si->si_retrynum[i]--;
1523 fail = si->si_retrynum[i];
1524 rtask->interval.tv_sec = si->si_retryinterval[i];
1525 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1526 slap_wake_listener();
1530 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1531 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1534 if ( fail == RETRYNUM_TAIL ) {
1535 Debug( LDAP_DEBUG_ANY,
1536 "do_syncrepl: %s rc %d quitting\n",
1537 si->si_ridtxt, rc, 0 );
1538 } else if ( fail > 0 ) {
1539 Debug( LDAP_DEBUG_ANY,
1540 "do_syncrepl: %s rc %d retrying (%d retries left)\n",
1541 si->si_ridtxt, rc, fail );
1543 Debug( LDAP_DEBUG_ANY,
1544 "do_syncrepl: %s rc %d retrying\n",
1545 si->si_ridtxt, rc, 0 );
1549 /* Do final delete cleanup */
1551 syncinfo_free( si, 0 );
1556 static slap_verbmasks modops[] = {
1557 { BER_BVC("add"), LDAP_REQ_ADD },
1558 { BER_BVC("delete"), LDAP_REQ_DELETE },
1559 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1560 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1565 syncrepl_accesslog_mods(
1567 struct berval *vals,
1568 struct Modifications **modres
1573 AttributeDescription *ad;
1574 struct berval bv, bv2;
1576 Modifications *mod = NULL, *modlist = NULL, **modtail;
1581 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1585 colon = ber_bvchr( &bv, ':' );
1591 bv.bv_len = colon - bv.bv_val;
1592 if ( slap_bv2ad( &bv, &ad, &text ) ) {
1594 Debug( LDAP_DEBUG_ANY, "syncrepl_accesslog_mods: %s "
1595 "Invalid attribute %s, %s\n",
1596 si->si_ridtxt, bv.bv_val, text );
1597 slap_mods_free( modlist, 1 );
1603 /* Ignore dynamically generated attrs */
1604 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1608 /* Ignore excluded attrs */
1609 if ( ldap_charray_inlist( si->si_exattrs,
1610 ad->ad_type->sat_cname.bv_val ) )
1616 case '+': op = LDAP_MOD_ADD; break;
1617 case '-': op = LDAP_MOD_DELETE; break;
1618 case '=': op = LDAP_MOD_REPLACE; break;
1619 case '#': op = LDAP_MOD_INCREMENT; break;
1623 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1624 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1627 mod->sml_next = NULL;
1629 mod->sml_type = ad->ad_cname;
1630 mod->sml_values = NULL;
1631 mod->sml_nvalues = NULL;
1632 mod->sml_numvals = 0;
1635 modtail = &mod->sml_next;
1637 if ( colon[2] == ' ' ) {
1638 bv.bv_val = colon + 3;
1639 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1640 ber_dupbv( &bv2, &bv );
1641 ber_bvarray_add( &mod->sml_values, &bv2 );
1650 syncrepl_changelog_mods(
1652 struct berval *vals,
1653 struct Modifications **modres
1656 return -1; /* FIXME */
1660 syncrepl_message_to_op(
1666 BerElement *ber = NULL;
1667 Modifications *modlist = NULL;
1669 SlapReply rs = { REP_RESULT };
1670 slap_callback cb = { NULL, null_callback, NULL, NULL };
1673 char txtbuf[SLAP_TEXT_BUFLEN];
1674 size_t textlen = sizeof txtbuf;
1676 struct berval bdn, dn = BER_BVNULL, ndn;
1677 struct berval bv, *bvals = NULL;
1678 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1679 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1680 psup = BER_BVNULL, nsup = BER_BVNULL;
1681 int rc, deleteOldRdn = 0, freeReqDn = 0;
1682 int do_graduate = 0;
1684 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1685 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1686 "Message type should be entry (%d)",
1687 si->si_ridtxt, ldap_msgtype( msg ), 0 );
1691 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1696 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1698 if ( rc != LDAP_SUCCESS ) {
1699 Debug( LDAP_DEBUG_ANY,
1700 "syncrepl_message_to_op: %s dn get failed (%d)",
1701 si->si_ridtxt, rc, 0 );
1705 op->o_tag = LBER_DEFAULT;
1706 op->o_bd = si->si_wbe;
1708 if ( BER_BVISEMPTY( &bdn ) && !BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) ) {
1709 Debug( LDAP_DEBUG_ANY,
1710 "syncrepl_message_to_op: %s got empty dn",
1711 si->si_ridtxt, 0, 0 );
1715 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ) )
1717 if ( bv.bv_val == NULL )
1720 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn ) ) {
1722 rc = dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1723 if ( rc != LDAP_SUCCESS ) {
1724 Debug( LDAP_DEBUG_ANY,
1725 "syncrepl_message_to_op: %s "
1726 "dn \"%s\" normalization failed (%d)",
1727 si->si_ridtxt, bdn.bv_val, rc );
1732 ber_dupbv( &op->o_req_dn, &dn );
1733 ber_dupbv( &op->o_req_ndn, &ndn );
1734 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1735 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1737 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req ) ) {
1738 int i = verb_to_mask( bvals[0].bv_val, modops );
1740 Debug( LDAP_DEBUG_ANY,
1741 "syncrepl_message_to_op: %s unknown op %s",
1742 si->si_ridtxt, bvals[0].bv_val, 0 );
1747 op->o_tag = modops[i].mask;
1748 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod ) ) {
1749 /* Parse attribute into modlist */
1750 if ( si->si_syncdata == SYNCDATA_ACCESSLOG ) {
1751 rc = syncrepl_accesslog_mods( si, bvals, &modlist );
1753 rc = syncrepl_changelog_mods( si, bvals, &modlist );
1755 if ( rc ) goto done;
1756 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn ) ) {
1758 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn ) ) {
1759 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ) ) {
1762 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup ) ) {
1764 } else if ( !ber_bvstrcasecmp( &bv,
1765 &slap_schema.si_ad_entryCSN->ad_cname ) )
1767 slap_queue_csn( op, bvals );
1773 /* If we didn't get a mod type or a target DN, bail out */
1774 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn ) ) {
1779 op->o_callback = &cb;
1780 slap_op_time( &op->o_time, &op->o_tincr );
1782 switch( op->o_tag ) {
1784 case LDAP_REQ_MODIFY:
1785 /* If we didn't get required data, bail */
1786 if ( !modlist ) goto done;
1788 rc = slap_mods_check( op, modlist, &text, txtbuf, textlen, NULL );
1790 if ( rc != LDAP_SUCCESS ) {
1791 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1792 "mods check (%s)\n",
1793 si->si_ridtxt, text, 0 );
1797 if ( op->o_tag == LDAP_REQ_ADD ) {
1798 Entry *e = entry_alloc();
1800 op->ora_e->e_name = op->o_req_dn;
1801 op->ora_e->e_nname = op->o_req_ndn;
1803 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1804 if( rc != LDAP_SUCCESS ) {
1805 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1806 "mods2entry (%s)\n",
1807 si->si_ridtxt, text, 0 );
1809 rc = op->o_bd->be_add( op, &rs );
1810 Debug( LDAP_DEBUG_SYNC,
1811 "syncrepl_message_to_op: %s be_add %s (%d)\n",
1812 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1815 if ( e == op->ora_e )
1816 be_entry_release_w( op, op->ora_e );
1818 op->orm_modlist = modlist;
1819 op->o_bd = si->si_wbe;
1820 rc = op->o_bd->be_modify( op, &rs );
1821 modlist = op->orm_modlist;
1822 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1823 "syncrepl_message_to_op: %s be_modify %s (%d)\n",
1824 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1825 op->o_bd = si->si_be;
1829 case LDAP_REQ_MODRDN:
1830 if ( BER_BVISNULL( &rdn ) ) goto done;
1832 if ( rdnPretty( NULL, &rdn, &prdn, NULL ) ) {
1835 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ) ) {
1838 if ( !BER_BVISNULL( &sup ) ) {
1839 if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ) ) {
1842 op->orr_newSup = &psup;
1843 op->orr_nnewSup = ⊅
1845 op->orr_newSup = NULL;
1846 op->orr_nnewSup = NULL;
1848 op->orr_newrdn = prdn;
1849 op->orr_nnewrdn = nrdn;
1850 op->orr_deleteoldrdn = deleteOldRdn;
1851 op->orr_modlist = NULL;
1852 if ( slap_modrdn2mods( op, &rs ) ) {
1856 /* Append modlist for operational attrs */
1860 for ( m = op->orr_modlist; m->sml_next; m = m->sml_next )
1862 m->sml_next = modlist;
1865 rc = op->o_bd->be_modrdn( op, &rs );
1866 slap_mods_free( op->orr_modlist, 1 );
1867 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1868 "syncrepl_message_to_op: %s be_modrdn %s (%d)\n",
1869 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1872 case LDAP_REQ_DELETE:
1873 rc = op->o_bd->be_delete( op, &rs );
1874 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1875 "syncrepl_message_to_op: %s be_delete %s (%d)\n",
1876 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1882 slap_graduate_commit_csn( op );
1883 op->o_bd = si->si_be;
1884 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
1885 BER_BVZERO( &op->o_csn );
1887 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1889 if ( !BER_BVISNULL( &rdn ) ) {
1890 if ( !BER_BVISNULL( &nsup ) ) {
1891 ch_free( nsup.bv_val );
1893 if ( !BER_BVISNULL( &psup ) ) {
1894 ch_free( psup.bv_val );
1896 if ( !BER_BVISNULL( &nrdn ) ) {
1897 ch_free( nrdn.bv_val );
1899 if ( !BER_BVISNULL( &prdn ) ) {
1900 ch_free( prdn.bv_val );
1904 ch_free( op->o_req_ndn.bv_val );
1905 ch_free( op->o_req_dn.bv_val );
1912 syncrepl_message_to_entry(
1916 Modifications **modlist,
1922 BerElement *ber = NULL;
1925 Modifications **modtail = modlist;
1928 char txtbuf[SLAP_TEXT_BUFLEN];
1929 size_t textlen = sizeof txtbuf;
1931 struct berval bdn = BER_BVNULL, dn, ndn;
1936 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1937 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s "
1938 "Message type should be entry (%d)",
1939 si->si_ridtxt, ldap_msgtype( msg ), 0 );
1943 op->o_tag = LDAP_REQ_ADD;
1945 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1946 if ( rc != LDAP_SUCCESS ) {
1947 Debug( LDAP_DEBUG_ANY,
1948 "syncrepl_message_to_entry: %s dn get failed (%d)",
1949 si->si_ridtxt, rc, 0 );
1953 if ( BER_BVISEMPTY( &bdn ) && !BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) ) {
1954 Debug( LDAP_DEBUG_ANY,
1955 "syncrepl_message_to_entry: %s got empty dn",
1956 si->si_ridtxt, 0, 0 );
1960 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1961 /* NOTE: this could be done even before decoding the DN,
1962 * although encoding errors wouldn't be detected */
1967 if ( entry == NULL ) {
1971 rc = dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1972 if ( rc != LDAP_SUCCESS ) {
1973 /* One of the things that could happen is that the schema
1974 * is not lined-up; this could result in unknown attributes.
1975 * A value non conformant to the syntax should be unlikely,
1976 * except when replicating between different versions
1977 * of the software, or when syntax validation bugs are fixed
1979 Debug( LDAP_DEBUG_ANY,
1980 "syncrepl_message_to_entry: "
1981 "%s dn \"%s\" normalization failed (%d)",
1982 si->si_ridtxt, bdn.bv_val, rc );
1986 ber_dupbv( &op->o_req_dn, &dn );
1987 ber_dupbv( &op->o_req_ndn, &ndn );
1988 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1989 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1991 is_ctx = dn_match( &op->o_req_ndn, &op->o_bd->be_nsuffix[0] );
1994 e->e_name = op->o_req_dn;
1995 e->e_nname = op->o_req_ndn;
1997 while ( ber_remaining( ber ) ) {
1998 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1999 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
2004 /* Drop all updates to the contextCSN of the context entry
2007 if ( is_ctx && !strcasecmp( tmp.sml_type.bv_val,
2008 slap_schema.si_ad_contextCSN->ad_cname.bv_val )) {
2009 ber_bvarray_free( tmp.sml_values );
2013 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
2015 mod->sml_op = LDAP_MOD_REPLACE;
2017 mod->sml_next = NULL;
2018 mod->sml_desc = NULL;
2019 mod->sml_type = tmp.sml_type;
2020 mod->sml_values = tmp.sml_values;
2021 mod->sml_nvalues = NULL;
2022 mod->sml_numvals = 0; /* slap_mods_check will set this */
2025 modtail = &mod->sml_next;
2028 if ( *modlist == NULL ) {
2029 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s no attributes\n",
2030 si->si_ridtxt, 0, 0 );
2035 rc = slap_mods_check( op, *modlist, &text, txtbuf, textlen, NULL );
2037 if ( rc != LDAP_SUCCESS ) {
2038 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods check (%s)\n",
2039 si->si_ridtxt, text, 0 );
2043 /* Strip out dynamically generated attrs */
2044 for ( modtail = modlist; *modtail ; ) {
2046 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
2047 *modtail = mod->sml_next;
2048 slap_mod_free( &mod->sml_mod, 0 );
2051 modtail = &mod->sml_next;
2055 /* Strip out attrs in exattrs list */
2056 for ( modtail = modlist; *modtail ; ) {
2058 if ( ldap_charray_inlist( si->si_exattrs,
2059 mod->sml_desc->ad_type->sat_cname.bv_val ) )
2061 *modtail = mod->sml_next;
2062 slap_mod_free( &mod->sml_mod, 0 );
2065 modtail = &mod->sml_next;
2069 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
2070 if( rc != LDAP_SUCCESS ) {
2071 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods2entry (%s)\n",
2072 si->si_ridtxt, text, 0 );
2077 if ( rc != LDAP_SUCCESS ) {
2089 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
2091 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
2092 * entry if a previous refresh was interrupted before sending us a new
2093 * context state. We try to compare the new entry to the existing entry
2094 * and ignore the new entry if they are the same.
2096 * Also, we may get an update where the entryDN has changed, due to
2097 * a ModDn on the provider. We detect this as well, so we can issue
2098 * the corresponding operation locally.
2100 * In the case of a modify, we get a list of all the attributes
2101 * in the original entry. Rather than deleting the entry and re-adding it,
2102 * we issue a Modify request that deletes all the attributes and adds all
2103 * the new ones. This avoids the issue of trying to delete/add a non-leaf
2106 * We otherwise distinguish ModDN from Modify; in the case of
2107 * a ModDN we just use the CSN, modifyTimestamp and modifiersName
2108 * operational attributes from the entry, and do a regular ModDN.
2110 typedef struct dninfo {
2114 struct berval nnewSup;
2115 int renamed; /* Was an existing entry renamed? */
2116 int delOldRDN; /* Was old RDN deleted? */
2117 Modifications **modlist; /* the modlist we received */
2118 Modifications *mods; /* the modlist we compared */
2119 Attribute *oldNattr; /* old naming attr */
2120 AttributeDescription *oldDesc; /* for renames */
2121 AttributeDescription *newDesc; /* for renames */
2124 /* return 1 if inserted, 0 otherwise */
2126 avl_presentlist_insert(
2128 struct berval *syncUUID )
2130 struct berval *syncuuid_bv = ch_malloc( sizeof( struct berval ) + syncUUID->bv_len + 1 );
2132 syncuuid_bv->bv_len = syncUUID->bv_len;
2133 syncuuid_bv->bv_val = (char *)&syncuuid_bv[1];
2134 AC_MEMCPY( syncuuid_bv->bv_val, syncUUID->bv_val, syncUUID->bv_len );
2135 syncuuid_bv->bv_val[ syncuuid_bv->bv_len ] = '\0';
2137 if ( avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
2138 syncuuid_cmp, avl_dup_error ) )
2140 ch_free( syncuuid_bv );
2152 Modifications** modlist,
2154 struct berval* syncUUID,
2155 struct berval* syncCSN )
2157 Backend *be = op->o_bd;
2158 slap_callback cb = { NULL, NULL, NULL, NULL };
2159 int syncuuid_inserted = 0;
2160 struct berval syncUUID_strrep = BER_BVNULL;
2162 SlapReply rs_search = {REP_RESULT};
2163 SlapReply rs_delete = {REP_RESULT};
2164 SlapReply rs_add = {REP_RESULT};
2165 SlapReply rs_modify = {REP_RESULT};
2167 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2168 int rc = LDAP_SUCCESS;
2170 struct berval pdn = BER_BVNULL;
2175 Debug( LDAP_DEBUG_SYNC,
2176 "syncrepl_entry: %s LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_%s)\n",
2177 si->si_ridtxt, syncrepl_state2str( syncstate ), 0 );
2179 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ) ) {
2180 if ( !si->si_refreshPresent && !si->si_refreshDone ) {
2181 syncuuid_inserted = avl_presentlist_insert( si, syncUUID );
2185 if ( syncstate == LDAP_SYNC_PRESENT ) {
2187 } else if ( syncstate != LDAP_SYNC_DELETE ) {
2188 if ( entry == NULL ) {
2193 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
2194 if ( syncstate != LDAP_SYNC_DELETE ) {
2195 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
2198 /* add if missing */
2199 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
2200 &syncUUID_strrep, syncUUID );
2202 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
2203 /* replace only if necessary */
2204 if ( a->a_nvals != a->a_vals ) {
2205 ber_memfree( a->a_nvals[0].bv_val );
2206 ber_dupbv( &a->a_nvals[0], syncUUID );
2208 ber_memfree( a->a_vals[0].bv_val );
2209 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
2213 f.f_choice = LDAP_FILTER_EQUALITY;
2215 ava.aa_desc = slap_schema.si_ad_entryUUID;
2216 ava.aa_value = *syncUUID;
2218 if ( syncuuid_inserted ) {
2219 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s inserted UUID %s\n",
2220 si->si_ridtxt, syncUUID_strrep.bv_val, 0 );
2222 op->ors_filter = &f;
2224 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID_strrep.bv_len;
2225 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
2226 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
2227 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
2228 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
2229 syncUUID_strrep.bv_val, syncUUID_strrep.bv_len );
2230 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
2231 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
2233 op->o_tag = LDAP_REQ_SEARCH;
2234 op->ors_scope = LDAP_SCOPE_SUBTREE;
2235 op->ors_deref = LDAP_DEREF_NEVER;
2237 /* get the entry for this UUID */
2238 op->o_req_dn = si->si_base;
2239 op->o_req_ndn = si->si_base;
2241 op->o_time = slap_get_time();
2242 op->ors_tlimit = SLAP_NO_LIMIT;
2244 op->ors_limit = NULL;
2246 op->ors_attrs = slap_anlist_all_attributes;
2247 op->ors_attrsonly = 0;
2249 /* set callback function */
2250 op->o_callback = &cb;
2251 cb.sc_response = dn_callback;
2252 cb.sc_private = &dni;
2253 dni.new_entry = entry;
2254 dni.modlist = modlist;
2256 rc = be->be_search( op, &rs_search );
2257 Debug( LDAP_DEBUG_SYNC,
2258 "syncrepl_entry: %s be_search (%d)\n",
2259 si->si_ridtxt, rc, 0 );
2261 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
2262 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2265 cb.sc_response = null_callback;
2268 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
2269 Debug( LDAP_DEBUG_SYNC,
2270 "syncrepl_entry: %s %s\n",
2271 si->si_ridtxt, entry->e_name.bv_val, 0 );
2273 Debug( LDAP_DEBUG_SYNC,
2274 "syncrepl_entry: %s %s\n",
2275 si->si_ridtxt, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
2278 assert( BER_BVISNULL( &op->o_csn ) );
2280 slap_queue_csn( op, syncCSN );
2283 slap_op_time( &op->o_time, &op->o_tincr );
2284 switch ( syncstate ) {
2286 case LDAP_SYNC_MODIFY:
2287 if ( BER_BVISNULL( &op->o_csn ))
2290 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
2292 /* FIXME: op->o_csn is assumed to be
2293 * on the thread's slab; this needs
2294 * to be cleared ASAP.
2295 * What happens if already present?
2297 assert( BER_BVISNULL( &op->o_csn ) );
2298 op->o_csn = a->a_vals[0];
2303 if ( BER_BVISNULL( &dni.dn ) ) {
2305 op->o_req_dn = entry->e_name;
2306 op->o_req_ndn = entry->e_nname;
2307 op->o_tag = LDAP_REQ_ADD;
2309 op->o_bd = si->si_wbe;
2311 rc = op->o_bd->be_add( op, &rs_add );
2312 Debug( LDAP_DEBUG_SYNC,
2313 "syncrepl_entry: %s be_add %s (%d)\n",
2314 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2315 switch ( rs_add.sr_err ) {
2317 if ( op->ora_e == entry ) {
2318 be_entry_release_w( op, entry );
2324 /* we assume that LDAP_NO_SUCH_OBJECT is returned
2325 * only if the suffix entry is not present */
2326 case LDAP_NO_SUCH_OBJECT:
2327 rc = syncrepl_add_glue( op, entry );
2331 /* if an entry was added via syncrepl_add_glue(),
2332 * it likely has no entryUUID, so the previous
2333 * be_search() doesn't find it. In this case,
2334 * give syncrepl a chance to modify it. Also
2335 * allow for entries that were recreated with the
2336 * same DN but a different entryUUID.
2338 case LDAP_ALREADY_EXISTS:
2340 Operation op2 = *op;
2341 SlapReply rs2 = { 0 };
2342 slap_callback cb2 = { 0 };
2345 op2.o_tag = LDAP_REQ_SEARCH;
2346 op2.o_req_dn = entry->e_name;
2347 op2.o_req_ndn = entry->e_nname;
2348 op2.ors_scope = LDAP_SCOPE_BASE;
2349 op2.ors_deref = LDAP_DEREF_NEVER;
2350 op2.ors_attrs = slap_anlist_all_attributes;
2351 op2.ors_attrsonly = 0;
2352 op2.ors_limit = NULL;
2354 op2.ors_tlimit = SLAP_NO_LIMIT;
2356 f.f_choice = LDAP_FILTER_PRESENT;
2357 f.f_desc = slap_schema.si_ad_objectClass;
2358 op2.ors_filter = &f;
2359 op2.ors_filterstr = generic_filterstr;
2361 op2.o_callback = &cb2;
2362 cb2.sc_response = dn_callback;
2363 cb2.sc_private = &dni;
2365 rc = be->be_search( &op2, &rs2 );
2366 if ( rc ) goto done;
2369 slap_op_time( &op->o_time, &op->o_tincr );
2375 Debug( LDAP_DEBUG_ANY,
2376 "syncrepl_entry: %s be_add %s failed (%d)\n",
2377 si->si_ridtxt, op->o_req_dn.bv_val, rs_add.sr_err );
2385 op->o_req_dn = dni.dn;
2386 op->o_req_ndn = dni.ndn;
2387 if ( dni.renamed ) {
2388 struct berval noldp, newp;
2389 Modifications *mod, **modtail, **ml, *m2;
2390 int i, got_replace = 0, just_rename = 0;
2392 op->o_tag = LDAP_REQ_MODRDN;
2393 dnRdn( &entry->e_name, &op->orr_newrdn );
2394 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
2396 if ( !BER_BVISNULL( &dni.nnewSup )) {
2397 dnParent( &entry->e_name, &newp );
2398 op->orr_newSup = &newp;
2399 op->orr_nnewSup = &dni.nnewSup;
2401 op->orr_newSup = NULL;
2402 op->orr_nnewSup = NULL;
2404 op->orr_deleteoldrdn = dni.delOldRDN;
2405 op->orr_modlist = NULL;
2406 if ( ( rc = slap_modrdn2mods( op, &rs_modify ) ) ) {
2410 /* Drop the RDN-related mods from this op, because their
2411 * equivalents were just setup by slap_modrdn2mods.
2413 * If delOldRDN is TRUE then we should see a delete modop
2414 * for oldDesc. We might see a replace instead.
2415 * delete with no values: therefore newDesc != oldDesc.
2416 * if oldNattr had only one value, then Drop this op.
2417 * delete with 1 value: can only be the oldRDN value. Drop op.
2418 * delete with N values: Drop oldRDN value, keep remainder.
2419 * replace with 1 value: if oldNattr had only one value and
2420 * newDesc == oldDesc, Drop this op.
2421 * Any other cases must be left intact.
2423 * We should also see an add modop for newDesc. (But not if
2424 * we got a replace modop due to delOldRDN.) If it has
2425 * multiple values, we'll have to drop the new RDN value.
2427 modtail = &op->orr_modlist;
2428 if ( dni.delOldRDN ) {
2429 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
2430 if ( (*ml)->sml_desc == dni.oldDesc ) {
2432 if ( mod->sml_op == LDAP_MOD_REPLACE &&
2433 dni.oldDesc != dni.newDesc ) {
2434 /* This Replace is due to other Mods.
2439 if ( mod->sml_numvals <= 1 &&
2440 dni.oldNattr->a_numvals == 1 &&
2441 ( mod->sml_op == LDAP_MOD_DELETE ||
2442 mod->sml_op == LDAP_MOD_REPLACE )) {
2443 if ( mod->sml_op == LDAP_MOD_REPLACE )
2446 *ml = mod->sml_next;
2447 mod->sml_next = NULL;
2448 slap_mods_free( mod, 1 );
2451 if ( mod->sml_op != LDAP_MOD_DELETE || mod->sml_numvals == 0 )
2453 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
2454 if ( m2->sml_desc == dni.oldDesc &&
2455 m2->sml_op == LDAP_MOD_DELETE ) break;
2457 for ( i=0; i<mod->sml_numvals; i++ ) {
2458 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
2460 ch_free( mod->sml_values[i].bv_val );
2461 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
2462 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
2463 if ( mod->sml_nvalues ) {
2464 ch_free( mod->sml_nvalues[i].bv_val );
2465 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
2466 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
2471 if ( !mod->sml_numvals ) {
2473 *ml = mod->sml_next;
2474 mod->sml_next = NULL;
2475 slap_mods_free( mod, 1 );
2481 if ( !got_replace ) {
2482 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
2483 if ( (*ml)->sml_desc == dni.newDesc ) {
2485 if ( mod->sml_op != LDAP_MOD_ADD )
2487 if ( mod->sml_numvals == 1 ) {
2489 *ml = mod->sml_next;
2490 mod->sml_next = NULL;
2491 slap_mods_free( mod, 1 );
2494 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
2495 if ( m2->sml_desc == dni.oldDesc &&
2496 m2->sml_op == SLAP_MOD_SOFTADD ) break;
2498 for ( i=0; i<mod->sml_numvals; i++ ) {
2499 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
2501 ch_free( mod->sml_values[i].bv_val );
2502 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
2503 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
2504 if ( mod->sml_nvalues ) {
2505 ch_free( mod->sml_nvalues[i].bv_val );
2506 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
2507 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
2517 /* RDNs must be NUL-terminated for back-ldap */
2518 noldp = op->orr_newrdn;
2519 ber_dupbv_x( &op->orr_newrdn, &noldp, op->o_tmpmemctx );
2520 noldp = op->orr_nnewrdn;
2521 ber_dupbv_x( &op->orr_nnewrdn, &noldp, op->o_tmpmemctx );
2523 /* Setup opattrs too */
2525 static AttributeDescription *nullattr = NULL;
2526 static AttributeDescription **const opattrs[] = {
2527 &slap_schema.si_ad_entryCSN,
2528 &slap_schema.si_ad_modifiersName,
2529 &slap_schema.si_ad_modifyTimestamp,
2532 AttributeDescription *opattr;
2536 /* pull mod off incoming modlist */
2537 for ( i = 0; (opattr = *opattrs[i]) != NULL; i++ ) {
2538 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next )
2540 if ( (*ml)->sml_desc == opattr ) {
2542 *ml = mod->sml_next;
2543 mod->sml_next = NULL;
2545 modtail = &mod->sml_next;
2550 /* If there are still Modifications left, put the opattrs
2551 * back, and let be_modify run. Otherwise, append the opattrs
2552 * to the orr_modlist.
2556 /* don't set a CSN for the rename op */
2558 slap_graduate_commit_csn( op );
2560 mod = op->orr_modlist;
2563 for ( ; mod->sml_next; mod=mod->sml_next );
2566 op->o_bd = si->si_wbe;
2568 rc = op->o_bd->be_modrdn( op, &rs_modify );
2570 /* NOTE: noSuchObject should result because the new superior
2571 * has not been added yet (ITS#6472) */
2572 if ( rc == LDAP_NO_SUCH_OBJECT && !BER_BVISNULL( op->orr_nnewSup )) {
2573 rc = syncrepl_add_glue_ancestors( op, entry );
2574 if ( rc == LDAP_SUCCESS ) {
2579 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
2580 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
2582 slap_mods_free( op->orr_modlist, 1 );
2583 Debug( LDAP_DEBUG_SYNC,
2584 "syncrepl_entry: %s be_modrdn %s (%d)\n",
2585 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2587 /* Renamed entries may still have other mods so just fallthru */
2588 op->o_req_dn = entry->e_name;
2589 op->o_req_ndn = entry->e_nname;
2590 /* Use CSN on the modify */
2594 slap_queue_csn( op, syncCSN );
2597 op->o_tag = LDAP_REQ_MODIFY;
2598 op->orm_modlist = dni.mods;
2599 op->orm_no_opattrs = 1;
2600 op->o_bd = si->si_wbe;
2602 rc = op->o_bd->be_modify( op, &rs_modify );
2603 slap_mods_free( op->orm_modlist, 1 );
2604 op->orm_no_opattrs = 0;
2605 Debug( LDAP_DEBUG_SYNC,
2606 "syncrepl_entry: %s be_modify %s (%d)\n",
2607 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2608 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
2609 Debug( LDAP_DEBUG_ANY,
2610 "syncrepl_entry: %s be_modify failed (%d)\n",
2611 si->si_ridtxt, rs_modify.sr_err, 0 );
2615 } else if ( !dni.renamed ) {
2616 Debug( LDAP_DEBUG_SYNC,
2617 "syncrepl_entry: %s entry unchanged, ignored (%s)\n",
2618 si->si_ridtxt, op->o_req_dn.bv_val, 0 );
2620 slap_graduate_commit_csn( op );
2625 case LDAP_SYNC_DELETE :
2626 if ( !BER_BVISNULL( &dni.dn ) ) {
2627 op->o_req_dn = dni.dn;
2628 op->o_req_ndn = dni.ndn;
2629 op->o_tag = LDAP_REQ_DELETE;
2630 op->o_bd = si->si_wbe;
2631 rc = op->o_bd->be_delete( op, &rs_delete );
2632 Debug( LDAP_DEBUG_SYNC,
2633 "syncrepl_entry: %s be_delete %s (%d)\n",
2634 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2636 while ( rs_delete.sr_err == LDAP_SUCCESS
2637 && op->o_delete_glue_parent ) {
2638 op->o_delete_glue_parent = 0;
2639 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
2640 slap_callback cb = { NULL };
2641 cb.sc_response = slap_null_cb;
2642 dnParent( &op->o_req_ndn, &pdn );
2644 op->o_req_ndn = pdn;
2645 op->o_callback = &cb;
2646 op->o_bd->be_delete( op, &rs_delete );
2657 Debug( LDAP_DEBUG_ANY,
2658 "syncrepl_entry: %s unknown syncstate\n", si->si_ridtxt, 0, 0 );
2663 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
2664 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
2665 BER_BVZERO( &syncUUID_strrep );
2667 if ( !BER_BVISNULL( &dni.ndn ) ) {
2668 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
2670 if ( !BER_BVISNULL( &dni.dn ) ) {
2671 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
2674 entry_free( entry );
2677 slap_graduate_commit_csn( op );
2679 if ( !BER_BVISNULL( &op->o_csn ) && freecsn ) {
2680 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2682 BER_BVZERO( &op->o_csn );
2686 static struct berval gcbva[] = {
2692 #define NP_DELETE_ONE 2
2695 syncrepl_del_nonpresent(
2699 struct sync_cookie *sc,
2702 Backend* be = op->o_bd;
2703 slap_callback cb = { NULL };
2704 SlapReply rs_search = {REP_RESULT};
2705 SlapReply rs_delete = {REP_RESULT};
2706 SlapReply rs_modify = {REP_RESULT};
2707 struct nonpresent_entry *np_list, *np_prev;
2709 AttributeName an[2];
2711 struct berval pdn = BER_BVNULL;
2714 op->o_req_dn = si->si_base;
2715 op->o_req_ndn = si->si_base;
2717 cb.sc_response = nonpresent_callback;
2720 op->o_callback = &cb;
2721 op->o_tag = LDAP_REQ_SEARCH;
2722 op->ors_scope = si->si_scope;
2723 op->ors_deref = LDAP_DEREF_NEVER;
2724 op->o_time = slap_get_time();
2725 op->ors_tlimit = SLAP_NO_LIMIT;
2730 AttributeAssertion eq = ATTRIBUTEASSERTION_INIT;
2733 op->ors_attrsonly = 1;
2734 op->ors_attrs = slap_anlist_no_attrs;
2735 op->ors_limit = NULL;
2736 op->ors_filter = &uf;
2739 uf.f_av_desc = slap_schema.si_ad_entryUUID;
2741 uf.f_choice = LDAP_FILTER_EQUALITY;
2742 si->si_refreshDelete |= NP_DELETE_ONE;
2744 for (i=0; uuids[i].bv_val; i++) {
2746 uf.f_av_value = uuids[i];
2747 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2748 rc = be->be_search( op, &rs_search );
2749 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2751 si->si_refreshDelete ^= NP_DELETE_ONE;
2755 AttributeAssertion mmaa;
2757 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2758 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2759 an[0].an_desc = slap_schema.si_ad_entryUUID;
2761 op->ors_slimit = SLAP_NO_LIMIT;
2762 op->ors_tlimit = SLAP_NO_LIMIT;
2763 op->ors_limit = NULL;
2764 op->ors_attrsonly = 0;
2765 op->ors_filter = filter_dup( si->si_filter, op->o_tmpmemctx );
2766 /* In multimaster, updates can continue to arrive while
2767 * we're searching. Limit the search result to entries
2768 * older than our newest cookie CSN.
2770 if ( SLAP_MULTIMASTER( op->o_bd )) {
2775 f->f_choice = LDAP_FILTER_AND;
2776 f->f_next = op->ors_filter;
2780 f->f_choice = LDAP_FILTER_LE;
2782 f->f_av_desc = slap_schema.si_ad_entryCSN;
2784 BER_BVZERO( &f->f_av_value );
2785 for ( i=0; i<sc->numcsns; i++ ) {
2786 if ( ber_bvcmp( &sc->ctxcsn[i], &f->f_av_value ) > 0 )
2787 f->f_av_value = sc->ctxcsn[i];
2789 of = op->ors_filter;
2790 op->ors_filter = mmf;
2791 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2794 op->ors_filterstr = si->si_filterstr;
2796 op->o_nocaching = 1;
2799 rc = be->be_search( op, &rs_search );
2800 if ( SLAP_MULTIMASTER( op->o_bd )) {
2801 op->ors_filter = of;
2803 if ( op->ors_filter ) filter_free_x( op, op->ors_filter, 1 );
2804 if ( op->ors_filterstr.bv_val != si->si_filterstr.bv_val ) {
2805 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2810 op->o_nocaching = 0;
2812 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2814 if ( sc->ctxcsn && !BER_BVISNULL( &sc->ctxcsn[m] ) ) {
2815 csn = sc->ctxcsn[m];
2817 csn = si->si_syncCookie.ctxcsn[0];
2820 op->o_bd = si->si_wbe;
2821 slap_queue_csn( op, &csn );
2823 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2824 while ( np_list != NULL ) {
2825 LDAP_LIST_REMOVE( np_list, npe_link );
2827 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2828 op->o_tag = LDAP_REQ_DELETE;
2829 op->o_callback = &cb;
2830 cb.sc_response = null_callback;
2832 op->o_req_dn = *np_prev->npe_name;
2833 op->o_req_ndn = *np_prev->npe_nname;
2834 rc = op->o_bd->be_delete( op, &rs_delete );
2835 Debug( LDAP_DEBUG_SYNC,
2836 "syncrepl_del_nonpresent: %s be_delete %s (%d)\n",
2837 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2839 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2840 Modifications mod1, mod2;
2841 mod1.sml_op = LDAP_MOD_REPLACE;
2843 mod1.sml_desc = slap_schema.si_ad_objectClass;
2844 mod1.sml_type = mod1.sml_desc->ad_cname;
2845 mod1.sml_numvals = 2;
2846 mod1.sml_values = &gcbva[0];
2847 mod1.sml_nvalues = NULL;
2848 mod1.sml_next = &mod2;
2850 mod2.sml_op = LDAP_MOD_REPLACE;
2852 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2853 mod2.sml_type = mod2.sml_desc->ad_cname;
2854 mod2.sml_numvals = 1;
2855 mod2.sml_values = &gcbva[1];
2856 mod2.sml_nvalues = NULL;
2857 mod2.sml_next = NULL;
2859 op->o_tag = LDAP_REQ_MODIFY;
2860 op->orm_modlist = &mod1;
2862 rc = op->o_bd->be_modify( op, &rs_modify );
2863 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
2866 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2867 op->o_delete_glue_parent ) {
2868 op->o_delete_glue_parent = 0;
2869 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
2870 slap_callback cb = { NULL };
2871 cb.sc_response = slap_null_cb;
2872 dnParent( &op->o_req_ndn, &pdn );
2874 op->o_req_ndn = pdn;
2875 op->o_callback = &cb;
2876 /* give it a root privil ? */
2877 op->o_bd->be_delete( op, &rs_delete );
2883 op->o_delete_glue_parent = 0;
2885 ber_bvfree( np_prev->npe_name );
2886 ber_bvfree( np_prev->npe_nname );
2889 if ( slapd_shutdown ) {
2894 slap_graduate_commit_csn( op );
2897 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2898 BER_BVZERO( &op->o_csn );
2905 syncrepl_add_glue_ancestors(
2909 Backend *be = op->o_bd;
2910 slap_callback cb = { NULL };
2915 struct berval dn = BER_BVNULL;
2916 struct berval ndn = BER_BVNULL;
2918 SlapReply rs_add = {REP_RESULT};
2919 struct berval ptr, nptr;
2922 op->o_tag = LDAP_REQ_ADD;
2923 op->o_callback = &cb;
2924 cb.sc_response = null_callback;
2925 cb.sc_private = NULL;
2930 /* count RDNs in suffix */
2931 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2932 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
2934 ptr.bv_len -= comma - ptr.bv_val;
2944 /* Start with BE suffix */
2946 for ( i = 0; i < suffrdns; i++ ) {
2947 comma = ber_bvrchr( &ptr, ',' );
2948 if ( comma != NULL ) {
2949 ptr.bv_len = comma - ptr.bv_val;
2956 if ( !BER_BVISEMPTY( &ptr ) ) {
2957 dn.bv_len -= ptr.bv_len + 1;
2958 dn.bv_val += ptr.bv_len + 1;
2961 /* the normalizedDNs are always the same length, no counting
2965 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2966 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2967 ndn.bv_len = be->be_nsuffix[0].bv_len;
2969 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
2975 while ( ndn.bv_val > e->e_nname.bv_val ) {
2976 glue = entry_alloc();
2977 ber_dupbv( &glue->e_name, &dn );
2978 ber_dupbv( &glue->e_nname, &ndn );
2980 a = attr_alloc( slap_schema.si_ad_objectClass );
2983 a->a_vals = ch_calloc( 3, sizeof( struct berval ) );
2984 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2985 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2986 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2988 a->a_nvals = a->a_vals;
2990 a->a_next = glue->e_attrs;
2993 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
2996 a->a_vals = ch_calloc( 2, sizeof( struct berval ) );
2997 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2998 ber_dupbv( &a->a_vals[1], &gcbva[2] );
3000 a->a_nvals = a->a_vals;
3002 a->a_next = glue->e_attrs;
3005 op->o_req_dn = glue->e_name;
3006 op->o_req_ndn = glue->e_nname;
3008 rc = be->be_add ( op, &rs_add );
3009 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3010 if ( op->ora_e == glue )
3011 be_entry_release_w( op, glue );
3013 /* incl. ALREADY EXIST */
3015 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
3021 /* Move to next child */
3022 comma = ber_bvrchr( &ptr, ',' );
3023 if ( comma == NULL ) {
3026 ptr.bv_len = comma - ptr.bv_val;
3028 dn.bv_val = ++comma;
3029 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
3031 comma = ber_bvrchr( &nptr, ',' );
3032 assert( comma != NULL );
3033 nptr.bv_len = comma - nptr.bv_val;
3035 ndn.bv_val = ++comma;
3036 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
3048 Backend *be = op->o_bd;
3049 SlapReply rs_add = {REP_RESULT};
3051 rc = syncrepl_add_glue_ancestors( op, e );
3052 if ( rc != LDAP_SUCCESS ) {
3056 op->o_req_dn = e->e_name;
3057 op->o_req_ndn = e->e_nname;
3059 rc = be->be_add ( op, &rs_add );
3060 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3061 if ( op->ora_e == e )
3062 be_entry_release_w( op, e );
3071 syncrepl_updateCookie(
3074 struct sync_cookie *syncCookie )
3076 Backend *be = op->o_bd;
3078 struct berval first = BER_BVNULL;
3080 Syntax *syn = slap_schema.si_ad_contextCSN->ad_type->sat_syntax;
3083 int rc, i, j, changed = 0;
3086 slap_callback cb = { NULL };
3087 SlapReply rs_modify = {REP_RESULT};
3089 mod.sml_op = LDAP_MOD_REPLACE;
3090 mod.sml_desc = slap_schema.si_ad_contextCSN;
3091 mod.sml_type = mod.sml_desc->ad_cname;
3092 mod.sml_flags = SLAP_MOD_INTERNAL;
3093 mod.sml_nvalues = NULL;
3094 mod.sml_next = NULL;
3096 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
3099 for ( i=0; i<syncCookie->numcsns; i++ ) {
3100 assert( !syn->ssyn_validate( syn, syncCookie->ctxcsn+i ));
3102 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3103 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3107 /* clone the cookieState CSNs so we can Replace the whole thing */
3108 mod.sml_numvals = si->si_cookieState->cs_num;
3109 mod.sml_values = op->o_tmpalloc(( mod.sml_numvals+1 )*sizeof(struct berval), op->o_tmpmemctx );
3110 for ( i=0; i<mod.sml_numvals; i++ )
3111 mod.sml_values[i] = si->si_cookieState->cs_vals[i];
3112 BER_BVZERO( &mod.sml_values[i] );
3114 /* find any CSNs in the syncCookie that are newer than the cookieState */
3115 for ( i=0; i<syncCookie->numcsns; i++ ) {
3116 for ( j=0; j<si->si_cookieState->cs_num; j++ ) {
3117 if ( syncCookie->sids[i] != si->si_cookieState->cs_sids[j] )
3119 len = syncCookie->ctxcsn[i].bv_len;
3120 if ( len > si->si_cookieState->cs_vals[j].bv_len )
3121 len = si->si_cookieState->cs_vals[j].bv_len;
3122 if ( memcmp( syncCookie->ctxcsn[i].bv_val,
3123 si->si_cookieState->cs_vals[j].bv_val, len ) > 0 ) {
3124 mod.sml_values[j] = syncCookie->ctxcsn[i];
3126 if ( BER_BVISNULL( &first ) ) {
3127 first = syncCookie->ctxcsn[i];
3129 } else if ( memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 )
3131 first = syncCookie->ctxcsn[i];
3136 /* there was no match for this SID, it's a new CSN */
3137 if ( j == si->si_cookieState->cs_num ) {
3138 mod.sml_values = op->o_tmprealloc( mod.sml_values,
3139 ( mod.sml_numvals+2 )*sizeof(struct berval), op->o_tmpmemctx );
3140 mod.sml_values[mod.sml_numvals++] = syncCookie->ctxcsn[i];
3141 BER_BVZERO( &mod.sml_values[mod.sml_numvals] );
3142 if ( BER_BVISNULL( &first ) ) {
3143 first = syncCookie->ctxcsn[i];
3144 } else if ( memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 )
3146 first = syncCookie->ctxcsn[i];
3151 /* Should never happen, ITS#5065 */
3152 if ( BER_BVISNULL( &first ) || !changed ) {
3153 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3154 op->o_tmpfree( mod.sml_values, op->o_tmpmemctx );
3157 op->o_bd = si->si_wbe;
3158 slap_queue_csn( op, &first );
3160 op->o_tag = LDAP_REQ_MODIFY;
3162 cb.sc_response = null_callback;
3165 op->o_callback = &cb;
3166 op->o_req_dn = si->si_contextdn;
3167 op->o_req_ndn = si->si_contextdn;
3169 /* update contextCSN */
3170 op->o_dont_replicate = 1;
3172 op->orm_modlist = &mod;
3173 op->orm_no_opattrs = 1;
3174 rc = op->o_bd->be_modify( op, &rs_modify );
3176 if ( rs_modify.sr_err == LDAP_NO_SUCH_OBJECT &&
3177 SLAP_SYNC_SUBENTRY( op->o_bd )) {
3179 char txtbuf[SLAP_TEXT_BUFLEN];
3180 size_t textlen = sizeof txtbuf;
3181 Entry *e = slap_create_context_csn_entry( op->o_bd, NULL );
3182 rc = slap_mods2entry( &mod, &e, 0, 1, &text, txtbuf, textlen);
3184 rc = op->o_bd->be_add( op, &rs_modify );
3185 if ( e == op->ora_e )
3186 be_entry_release_w( op, op->ora_e );
3189 op->orm_no_opattrs = 0;
3190 op->o_dont_replicate = 0;
3192 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
3193 slap_sync_cookie_free( &si->si_syncCookie, 0 );
3194 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
3195 /* If we replaced any old values */
3196 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3197 if ( mod.sml_values[i].bv_val != si->si_cookieState->cs_vals[i].bv_val )
3198 ber_bvreplace( &si->si_cookieState->cs_vals[i],
3199 &mod.sml_values[i] );
3201 /* Handle any added values */
3202 if ( i < mod.sml_numvals ) {
3203 si->si_cookieState->cs_num = mod.sml_numvals;
3204 value_add( &si->si_cookieState->cs_vals, &mod.sml_values[i] );
3205 free( si->si_cookieState->cs_sids );
3206 si->si_cookieState->cs_sids = slap_parse_csn_sids(
3207 si->si_cookieState->cs_vals, si->si_cookieState->cs_num, NULL );
3210 si->si_cookieState->cs_age++;
3211 si->si_cookieAge = si->si_cookieState->cs_age;
3213 Debug( LDAP_DEBUG_ANY,
3214 "syncrepl_updateCookie: %s be_modify failed (%d)\n",
3215 si->si_ridtxt, rs_modify.sr_err, 0 );
3217 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3220 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3221 BER_BVZERO( &op->o_csn );
3222 if ( mod.sml_next ) slap_mods_free( mod.sml_next, 1 );
3223 op->o_tmpfree( mod.sml_values, op->o_tmpmemctx );
3226 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3227 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3234 /* Compare the attribute from the old entry to the one in the new
3235 * entry. The Modifications from the new entry will either be left
3236 * in place, or changed to an Add or Delete as needed.
3239 attr_cmp( Operation *op, Attribute *old, Attribute *new,
3240 Modifications ***mret, Modifications ***mcur )
3243 Modifications *mod, **modtail;
3249 struct berval **adds, **dels;
3250 /* count old and new */
3251 for ( o=0; old->a_vals[o].bv_val; o++ ) ;
3252 for ( n=0; new->a_vals[n].bv_val; n++ ) ;
3254 /* there MUST be both old and new values */
3259 adds = op->o_tmpalloc( sizeof(struct berval *) * n, op->o_tmpmemctx );
3260 dels = op->o_tmpalloc( sizeof(struct berval *) * o, op->o_tmpmemctx );
3262 for ( i=0; i<o; i++ ) dels[i] = &old->a_vals[i];
3263 for ( i=0; i<n; i++ ) adds[i] = &new->a_vals[i];
3267 for ( i=0; i<o; i++ ) {
3268 for ( j=0; j<n; j++ ) {
3271 if ( bvmatch( dels[i], adds[j] ) ) {
3281 /* Don't delete/add an objectClass, always use the replace op.
3282 * Modify would fail if provider has replaced entry with a new,
3283 * and the new explicitly includes a superior of a class that was
3284 * only included implicitly in the old entry. Ref ITS#5517.
3286 * Also use replace op if attr has no equality matching rule.
3289 if ( ( nn || ( no > 0 && no < o ) ) &&
3290 ( old->a_desc == slap_schema.si_ad_objectClass ||
3291 !old->a_desc->ad_type->sat_equality ) )
3297 /* all old values were deleted, just use the replace op */
3301 /* delete some values */
3302 mod = ch_malloc( sizeof( Modifications ) );
3303 mod->sml_op = LDAP_MOD_DELETE;
3305 mod->sml_desc = old->a_desc;
3306 mod->sml_type = mod->sml_desc->ad_cname;
3307 mod->sml_numvals = no;
3308 mod->sml_values = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
3309 if ( old->a_vals != old->a_nvals ) {
3310 mod->sml_nvalues = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
3312 mod->sml_nvalues = NULL;
3315 for ( i = 0; i < o; i++ ) {
3316 if ( !dels[i] ) continue;
3317 ber_dupbv( &mod->sml_values[j], &old->a_vals[i] );
3318 if ( mod->sml_nvalues ) {
3319 ber_dupbv( &mod->sml_nvalues[j], &old->a_nvals[i] );
3323 BER_BVZERO( &mod->sml_values[j] );
3324 if ( mod->sml_nvalues ) {
3325 BER_BVZERO( &mod->sml_nvalues[j] );
3328 modtail = &mod->sml_next;
3331 op->o_tmpfree( dels, op->o_tmpmemctx );
3332 /* some values were added */
3333 if ( nn && no < o ) {
3334 mod = ch_malloc( sizeof( Modifications ) );
3335 mod->sml_op = LDAP_MOD_ADD;
3337 mod->sml_desc = old->a_desc;
3338 mod->sml_type = mod->sml_desc->ad_cname;
3339 mod->sml_numvals = nn;
3340 mod->sml_values = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
3341 if ( old->a_vals != old->a_nvals ) {
3342 mod->sml_nvalues = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
3344 mod->sml_nvalues = NULL;
3347 for ( i = 0; i < n; i++ ) {
3348 if ( !adds[i] ) continue;
3349 ber_dupbv( &mod->sml_values[j], &new->a_vals[i] );
3350 if ( mod->sml_nvalues ) {
3351 ber_dupbv( &mod->sml_nvalues[j], &new->a_nvals[i] );
3355 BER_BVZERO( &mod->sml_values[j] );
3356 if ( mod->sml_nvalues ) {
3357 BER_BVZERO( &mod->sml_nvalues[j] );
3360 modtail = &mod->sml_next;
3363 op->o_tmpfree( adds, op->o_tmpmemctx );
3365 /* new attr, just use the new mod */
3369 /* advance to next element */
3373 **mcur = mod->sml_next;
3375 modtail = &mod->sml_next;
3377 *mcur = &mod->sml_next;
3383 /* Generate a set of modifications to change the old entry into the
3384 * new one. On input ml is a list of modifications equivalent to
3385 * the new entry. It will be massaged and the result will be stored
3388 void syncrepl_diff_entry( Operation *op, Attribute *old, Attribute *new,
3389 Modifications **mods, Modifications **ml, int is_ctx)
3391 Modifications **modtail = mods;
3393 /* We assume that attributes are saved in the same order
3394 * in the remote and local databases. So if we walk through
3395 * the attributeDescriptions one by one they should match in
3396 * lock step. If not, look for an add or delete.
3398 while ( old && new )
3400 /* If we've seen this before, use its mod now */
3401 if ( new->a_flags & SLAP_ATTR_IXADD ) {
3402 attr_cmp( op, NULL, new, &modtail, &ml );
3406 /* Skip contextCSN */
3407 if ( is_ctx && old->a_desc ==
3408 slap_schema.si_ad_contextCSN ) {
3413 if ( old->a_desc != new->a_desc ) {
3417 /* If it's just been re-added later,
3418 * remember that we've seen it.
3420 tmp = attr_find( new, old->a_desc );
3422 tmp->a_flags |= SLAP_ATTR_IXADD;
3424 /* If it's a new attribute, pull it in.
3426 tmp = attr_find( old, new->a_desc );
3428 attr_cmp( op, NULL, new, &modtail, &ml );
3432 /* Delete old attr */
3433 mod = ch_malloc( sizeof( Modifications ) );
3434 mod->sml_op = LDAP_MOD_DELETE;
3436 mod->sml_desc = old->a_desc;
3437 mod->sml_type = mod->sml_desc->ad_cname;
3438 mod->sml_numvals = 0;
3439 mod->sml_values = NULL;
3440 mod->sml_nvalues = NULL;
3442 modtail = &mod->sml_next;
3447 /* kludge - always update modifiersName so that it
3448 * stays co-located with the other mod opattrs. But only
3449 * if we know there are other valid mods.
3451 if ( *mods && ( old->a_desc == slap_schema.si_ad_modifiersName ||
3452 old->a_desc == slap_schema.si_ad_modifyTimestamp ))
3453 attr_cmp( op, NULL, new, &modtail, &ml );
3455 attr_cmp( op, old, new, &modtail, &ml );
3468 dninfo *dni = op->o_callback->sc_private;
3470 if ( rs->sr_type == REP_SEARCH ) {
3471 if ( !BER_BVISNULL( &dni->dn ) ) {
3472 Debug( LDAP_DEBUG_ANY,
3473 "dn_callback : consistency error - "
3474 "entryUUID is not unique\n", 0, 0, 0 );
3476 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
3477 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
3478 /* If there is a new entry, see if it differs from the old.
3479 * We compare the non-normalized values so that cosmetic changes
3480 * in the provider are always propagated.
3482 if ( dni->new_entry ) {
3483 Modifications **modtail, **ml;
3484 Attribute *old, *new;
3485 struct berval old_rdn, new_rdn;
3486 struct berval old_p, new_p;
3487 int is_ctx, new_sup = 0;
3489 /* If old entry is not a glue entry, make sure new entry
3490 * is actually newer than old entry
3492 if ( !is_entry_glue( rs->sr_entry )) {
3493 old = attr_find( rs->sr_entry->e_attrs,
3494 slap_schema.si_ad_entryCSN );
3495 new = attr_find( dni->new_entry->e_attrs,
3496 slap_schema.si_ad_entryCSN );
3499 ber_len_t len = old->a_vals[0].bv_len;
3500 if ( len > new->a_vals[0].bv_len )
3501 len = new->a_vals[0].bv_len;
3502 rc = memcmp( old->a_vals[0].bv_val,
3503 new->a_vals[0].bv_val, len );
3505 Debug( LDAP_DEBUG_SYNC,
3506 "dn_callback : new entry is older than ours "
3507 "%s ours %s, new %s\n",
3508 rs->sr_entry->e_name.bv_val,
3509 old->a_vals[0].bv_val,
3510 new->a_vals[0].bv_val );
3511 return LDAP_SUCCESS;
3512 } else if ( rc == 0 ) {
3513 Debug( LDAP_DEBUG_SYNC,
3514 "dn_callback : entries have identical CSN "
3516 rs->sr_entry->e_name.bv_val,
3517 old->a_vals[0].bv_val, 0 );
3518 return LDAP_SUCCESS;
3523 is_ctx = dn_match( &rs->sr_entry->e_nname,
3524 &op->o_bd->be_nsuffix[0] );
3526 /* Did the DN change?
3527 * case changes in the parent are ignored,
3528 * we only want to know if the RDN was
3531 dnRdn( &rs->sr_entry->e_name, &old_rdn );
3532 dnRdn( &dni->new_entry->e_name, &new_rdn );
3533 dnParent( &rs->sr_entry->e_nname, &old_p );
3534 dnParent( &dni->new_entry->e_nname, &new_p );
3536 new_sup = !dn_match( &old_p, &new_p );
3537 if ( !dn_match( &old_rdn, &new_rdn ) || new_sup )
3539 struct berval oldRDN, oldVal;
3540 AttributeDescription *ad = NULL;
3546 dni->nnewSup = new_p;
3548 /* See if the oldRDN was deleted */
3549 dnRdn( &rs->sr_entry->e_nname, &oldRDN );
3550 oldVal.bv_val = strchr(oldRDN.bv_val, '=') + 1;
3551 oldVal.bv_len = oldRDN.bv_len - ( oldVal.bv_val -
3553 oldRDN.bv_len -= oldVal.bv_len + 1;
3554 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
3556 for ( oldpos=0, a=rs->sr_entry->e_attrs;
3557 a && a->a_desc != ad; oldpos++, a=a->a_next );
3559 for ( newpos=0, a=dni->new_entry->e_attrs;
3560 a && a->a_desc != ad; newpos++, a=a->a_next );
3561 if ( !a || oldpos != newpos || attr_valfind( a,
3562 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH |
3563 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
3564 SLAP_MR_VALUE_OF_SYNTAX,
3565 &oldVal, NULL, op->o_tmpmemctx ) != LDAP_SUCCESS )
3569 /* Get the newRDN's desc */
3570 dnRdn( &dni->new_entry->e_nname, &oldRDN );
3571 oldVal.bv_val = strchr(oldRDN.bv_val, '=');
3572 oldRDN.bv_len = oldVal.bv_val - oldRDN.bv_val;
3574 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
3577 /* A ModDN has happened, but in Refresh mode other
3578 * changes may have occurred before we picked it up.
3579 * So fallthru to regular Modify processing.
3583 syncrepl_diff_entry( op, rs->sr_entry->e_attrs,
3584 dni->new_entry->e_attrs, &dni->mods, dni->modlist,
3588 } else if ( rs->sr_type == REP_RESULT ) {
3589 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
3590 Debug( LDAP_DEBUG_ANY,
3591 "dn_callback : consistency error - "
3592 "entryUUID is not unique\n", 0, 0, 0 );
3596 return LDAP_SUCCESS;
3600 nonpresent_callback(
3604 syncinfo_t *si = op->o_callback->sc_private;
3607 struct berval* present_uuid = NULL;
3608 struct nonpresent_entry *np_entry;
3610 if ( rs->sr_type == REP_RESULT ) {
3611 count = avl_free( si->si_presentlist, ch_free );
3612 si->si_presentlist = NULL;
3614 } else if ( rs->sr_type == REP_SEARCH ) {
3615 if ( !( si->si_refreshDelete & NP_DELETE_ONE ) ) {
3616 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
3619 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
3623 if ( LogTest( LDAP_DEBUG_SYNC ) ) {
3624 char buf[sizeof("rid=999 non")];
3626 snprintf( buf, sizeof(buf), "%s %s", si->si_ridtxt,
3627 present_uuid ? "" : "non" );
3629 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: %spresent UUID %s, dn %s\n",
3630 buf, a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val );
3633 if ( a == NULL ) return 0;
3636 if ( present_uuid == NULL ) {
3637 np_entry = (struct nonpresent_entry *)
3638 ch_calloc( 1, sizeof( struct nonpresent_entry ) );
3639 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
3640 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
3641 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
3644 avl_delete( &si->si_presentlist,
3645 &a->a_nvals[0], syncuuid_cmp );
3646 ch_free( present_uuid );
3649 return LDAP_SUCCESS;
3657 if ( rs->sr_err != LDAP_SUCCESS &&
3658 rs->sr_err != LDAP_REFERRAL &&
3659 rs->sr_err != LDAP_ALREADY_EXISTS &&
3660 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
3661 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
3663 Debug( LDAP_DEBUG_ANY,
3664 "null_callback : error code 0x%x\n",
3667 return LDAP_SUCCESS;
3670 static struct berval *
3671 slap_uuidstr_from_normalized(
3672 struct berval* uuidstr,
3673 struct berval* normalized,
3678 unsigned char nibble;
3681 if ( normalized == NULL ) return NULL;
3682 if ( normalized->bv_len != 16 ) return NULL;
3687 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
3688 if ( new == NULL ) {
3695 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
3696 if ( new != uuidstr ) {
3697 slap_sl_free( new, ctx );
3702 for ( i = 0; i < 16; i++ ) {
3703 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
3704 new->bv_val[(i<<1)+d] = '-';
3708 nibble = (normalized->bv_val[i] >> 4) & 0xF;
3709 if ( nibble < 10 ) {
3710 new->bv_val[(i<<1)+d] = nibble + '0';
3712 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
3715 nibble = (normalized->bv_val[i]) & 0xF;
3716 if ( nibble < 10 ) {
3717 new->bv_val[(i<<1)+d+1] = nibble + '0';
3719 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
3723 new->bv_val[new->bv_len] = '\0';
3730 if ( normalized == NULL ) return NULL;
3731 if ( normalized->bv_len != 16 ) return NULL;
3737 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
3738 if ( new == NULL ) {
3745 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
3750 rc = lutil_uuidstr_from_normalized( normalized->bv_val,
3751 normalized->bv_len, new->bv_val, new->bv_len + 1 );
3755 if ( new != NULL ) {
3756 if ( new->bv_val != NULL ) {
3757 slap_sl_free( new->bv_val, ctx );
3760 if ( new != uuidstr ) {
3761 slap_sl_free( new, ctx );
3774 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
3776 const struct berval *uuid1 = v_uuid1;
3777 const struct berval *uuid2 = v_uuid2;
3778 int rc = uuid1->bv_len - uuid2->bv_len;
3779 if ( rc ) return rc;
3780 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
3784 syncinfo_free( syncinfo_t *sie, int free_all )
3786 syncinfo_t *si_next;
3788 Debug( LDAP_DEBUG_TRACE, "syncinfo_free: %s\n",
3789 sie->si_ridtxt, 0, 0 );
3792 si_next = sie->si_next;
3795 if ( sie->si_conn ) {
3796 connection_client_stop( sie->si_conn );
3797 sie->si_conn = NULL;
3799 ldap_unbind_ext( sie->si_ld, NULL, NULL );
3803 struct re_s *re = sie->si_re;
3806 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3807 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
3808 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
3809 ldap_pvt_runqueue_remove( &slapd_rq, re );
3810 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3813 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
3815 bindconf_free( &sie->si_bindconf );
3817 if ( sie->si_filterstr.bv_val ) {
3818 ch_free( sie->si_filterstr.bv_val );
3820 if ( sie->si_filter ) {
3821 filter_free( sie->si_filter );
3823 if ( sie->si_logfilterstr.bv_val ) {
3824 ch_free( sie->si_logfilterstr.bv_val );
3826 if ( sie->si_base.bv_val ) {
3827 ch_free( sie->si_base.bv_val );
3829 if ( sie->si_logbase.bv_val ) {
3830 ch_free( sie->si_logbase.bv_val );
3832 if ( sie->si_be && SLAP_SYNC_SUBENTRY( sie->si_be )) {
3833 ch_free( sie->si_contextdn.bv_val );
3835 if ( sie->si_attrs ) {
3837 while ( sie->si_attrs[i] != NULL ) {
3838 ch_free( sie->si_attrs[i] );
3841 ch_free( sie->si_attrs );
3843 if ( sie->si_exattrs ) {
3845 while ( sie->si_exattrs[i] != NULL ) {
3846 ch_free( sie->si_exattrs[i] );
3849 ch_free( sie->si_exattrs );
3851 if ( sie->si_anlist ) {
3853 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
3854 ch_free( sie->si_anlist[i].an_name.bv_val );
3857 ch_free( sie->si_anlist );
3859 if ( sie->si_exanlist ) {
3861 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
3862 ch_free( sie->si_exanlist[i].an_name.bv_val );
3865 ch_free( sie->si_exanlist );
3867 if ( sie->si_retryinterval ) {
3868 ch_free( sie->si_retryinterval );
3870 if ( sie->si_retrynum ) {
3871 ch_free( sie->si_retrynum );
3873 if ( sie->si_retrynum_init ) {
3874 ch_free( sie->si_retrynum_init );
3876 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
3877 if ( sie->si_presentlist ) {
3878 avl_free( sie->si_presentlist, ch_free );
3880 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist ) ) {
3881 struct nonpresent_entry* npe;
3882 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
3883 LDAP_LIST_REMOVE( npe, npe_link );
3884 if ( npe->npe_name ) {
3885 if ( npe->npe_name->bv_val ) {
3886 ch_free( npe->npe_name->bv_val );
3888 ch_free( npe->npe_name );
3890 if ( npe->npe_nname ) {
3891 if ( npe->npe_nname->bv_val ) {
3892 ch_free( npe->npe_nname->bv_val );
3894 ch_free( npe->npe_nname );
3898 if ( sie->si_cookieState ) {
3899 sie->si_cookieState->cs_ref--;
3900 if ( !sie->si_cookieState->cs_ref ) {
3901 ch_free( sie->si_cookieState->cs_sids );
3902 ber_bvarray_free( sie->si_cookieState->cs_vals );
3903 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_mutex );
3904 ch_free( sie->si_cookieState->cs_psids );
3905 ber_bvarray_free( sie->si_cookieState->cs_pvals );
3906 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_pmutex );
3907 ch_free( sie->si_cookieState );
3912 } while ( free_all && si_next );
3917 /* NOTE: used & documented in slapd.conf(5) */
3919 #define PROVIDERSTR "provider"
3920 #define SCHEMASTR "schemachecking"
3921 #define FILTERSTR "filter"
3922 #define SEARCHBASESTR "searchbase"
3923 #define SCOPESTR "scope"
3924 #define ATTRSONLYSTR "attrsonly"
3925 #define ATTRSSTR "attrs"
3926 #define TYPESTR "type"
3927 #define INTERVALSTR "interval"
3928 #define RETRYSTR "retry"
3929 #define SLIMITSTR "sizelimit"
3930 #define TLIMITSTR "timelimit"
3931 #define SYNCDATASTR "syncdata"
3932 #define LOGBASESTR "logbase"
3933 #define LOGFILTERSTR "logfilter"
3935 /* FIXME: undocumented */
3936 #define EXATTRSSTR "exattrs"
3937 #define MANAGEDSAITSTR "manageDSAit"
3941 GOT_RID = 0x00000001U,
3942 GOT_PROVIDER = 0x00000002U,
3943 GOT_SCHEMACHECKING = 0x00000004U,
3944 GOT_FILTER = 0x00000008U,
3945 GOT_SEARCHBASE = 0x00000010U,
3946 GOT_SCOPE = 0x00000020U,
3947 GOT_ATTRSONLY = 0x00000040U,
3948 GOT_ATTRS = 0x00000080U,
3949 GOT_TYPE = 0x00000100U,
3950 GOT_INTERVAL = 0x00000200U,
3951 GOT_RETRY = 0x00000400U,
3952 GOT_SLIMIT = 0x00000800U,
3953 GOT_TLIMIT = 0x00001000U,
3954 GOT_SYNCDATA = 0x00002000U,
3955 GOT_LOGBASE = 0x00004000U,
3956 GOT_LOGFILTER = 0x00008000U,
3957 GOT_EXATTRS = 0x00010000U,
3958 GOT_MANAGEDSAIT = 0x00020000U,
3959 GOT_BINDCONF = 0x00040000U,
3962 GOT_REQUIRED = (GOT_RID|GOT_PROVIDER|GOT_SEARCHBASE)
3965 static slap_verbmasks datamodes[] = {
3966 { BER_BVC("default"), SYNCDATA_DEFAULT },
3967 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
3968 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
3973 parse_syncrepl_retry(
3980 int use_default = 0;
3982 char *val = arg + STRLENOF( RETRYSTR "=" );
3983 if ( strcasecmp( val, "undefined" ) == 0 ) {
3988 retry_list = (char **) ch_calloc( 1, sizeof( char * ) );
3989 retry_list[0] = NULL;
3991 slap_str2clist( &retry_list, val, " ,\t" );
3993 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
3996 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3997 "Error: incomplete syncrepl retry list" );
3998 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3999 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4000 ch_free( retry_list[k] );
4002 ch_free( retry_list );
4005 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ) );
4006 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ) );
4007 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ) );
4008 for ( j = 0; j < n; j++ ) {
4010 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
4011 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4012 "Error: invalid retry interval \"%s\" (#%d)",
4013 retry_list[j*2], j );
4014 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4015 /* do some cleanup */
4018 si->si_retryinterval[j] = (time_t)t;
4019 if ( *retry_list[j*2+1] == '+' ) {
4020 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
4021 si->si_retrynum[j] = RETRYNUM_FOREVER;
4025 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
4026 || si->si_retrynum_init[j] <= 0 )
4028 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4029 "Error: invalid initial retry number \"%s\" (#%d)",
4030 retry_list[j*2+1], j );
4031 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4032 /* do some cleanup */
4035 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
4036 || si->si_retrynum[j] <= 0 )
4038 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4039 "Error: invalid retry number \"%s\" (#%d)",
4040 retry_list[j*2+1], j );
4041 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4042 /* do some cleanup */
4047 if ( j < 1 || si->si_retrynum_init[j-1] != RETRYNUM_FOREVER ) {
4048 Debug( LDAP_DEBUG_CONFIG,
4049 "%s: syncrepl will eventually stop retrying; the \"retry\" parameter should end with a '+'.\n",
4053 si->si_retrynum_init[j] = RETRYNUM_TAIL;
4054 si->si_retrynum[j] = RETRYNUM_TAIL;
4055 si->si_retryinterval[j] = 0;
4057 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4058 ch_free( retry_list[k] );
4060 ch_free( retry_list );
4061 if ( !use_default ) {
4062 si->si_got |= GOT_RETRY;
4069 parse_syncrepl_line(
4076 for ( i = 1; i < c->argc; i++ ) {
4077 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
4078 STRLENOF( IDSTR "=" ) ) )
4081 /* '\0' string terminator accounts for '=' */
4082 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
4083 if ( lutil_atoi( &tmp, val ) != 0 ) {
4084 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4085 "Error: parse_syncrepl_line: "
4086 "unable to parse syncrepl id \"%s\"", val );
4087 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4090 if ( tmp > SLAP_SYNC_RID_MAX || tmp < 0 ) {
4091 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4092 "Error: parse_syncrepl_line: "
4093 "syncrepl id %d is out of range [0..%d]", tmp, SLAP_SYNC_RID_MAX );
4094 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4098 sprintf( si->si_ridtxt, IDSTR "=%03d", si->si_rid );
4099 si->si_got |= GOT_RID;
4100 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
4101 STRLENOF( PROVIDERSTR "=" ) ) )
4103 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
4104 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
4106 if ( ldap_is_ldaps_url( val ))
4107 si->si_bindconf.sb_tls_do_init = 1;
4109 si->si_got |= GOT_PROVIDER;
4110 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
4111 STRLENOF( SCHEMASTR "=" ) ) )
4113 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
4114 if ( !strncasecmp( val, "on", STRLENOF( "on" ) ) ) {
4115 si->si_schemachecking = 1;
4116 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
4117 si->si_schemachecking = 0;
4119 si->si_schemachecking = 1;
4121 si->si_got |= GOT_SCHEMACHECKING;
4122 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
4123 STRLENOF( FILTERSTR "=" ) ) )
4125 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
4126 if ( si->si_filterstr.bv_val )
4127 ch_free( si->si_filterstr.bv_val );
4128 ber_str2bv( val, 0, 1, &si->si_filterstr );
4129 si->si_got |= GOT_FILTER;
4130 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
4131 STRLENOF( LOGFILTERSTR "=" ) ) )
4133 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
4134 if ( si->si_logfilterstr.bv_val )
4135 ch_free( si->si_logfilterstr.bv_val );
4136 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
4137 si->si_got |= GOT_LOGFILTER;
4138 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
4139 STRLENOF( SEARCHBASESTR "=" ) ) )
4144 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
4145 if ( si->si_base.bv_val ) {
4146 ch_free( si->si_base.bv_val );
4148 ber_str2bv( val, 0, 0, &bv );
4149 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
4150 if ( rc != LDAP_SUCCESS ) {
4151 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4152 "Invalid base DN \"%s\": %d (%s)",
4153 val, rc, ldap_err2string( rc ) );
4154 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4157 if ( !be_issubordinate( c->be, &si->si_base ) ) {
4158 ch_free( si->si_base.bv_val );
4159 BER_BVZERO( &si->si_base );
4160 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4161 "Base DN \"%s\" is not within the database naming context",
4163 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4166 si->si_got |= GOT_SEARCHBASE;
4167 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
4168 STRLENOF( LOGBASESTR "=" ) ) )
4173 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
4174 if ( si->si_logbase.bv_val ) {
4175 ch_free( si->si_logbase.bv_val );
4177 ber_str2bv( val, 0, 0, &bv );
4178 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
4179 if ( rc != LDAP_SUCCESS ) {
4180 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4181 "Invalid logbase DN \"%s\": %d (%s)",
4182 val, rc, ldap_err2string( rc ) );
4183 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4186 si->si_got |= GOT_LOGBASE;
4187 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
4188 STRLENOF( SCOPESTR "=" ) ) )
4191 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
4192 j = ldap_pvt_str2scope( val );
4194 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4195 "Error: parse_syncrepl_line: "
4196 "unknown scope \"%s\"", val);
4197 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4201 si->si_got |= GOT_SCOPE;
4202 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
4203 STRLENOF( ATTRSONLYSTR ) ) )
4205 si->si_attrsonly = 1;
4206 si->si_got |= GOT_ATTRSONLY;
4207 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
4208 STRLENOF( ATTRSSTR "=" ) ) )
4210 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
4211 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
4213 attr_fname = ch_strdup( val + STRLENOF(":include:") );
4214 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
4215 if ( si->si_anlist == NULL ) {
4216 ch_free( attr_fname );
4219 si->si_anfile = attr_fname;
4221 char *str, *s, *next;
4222 const char *delimstr = " ,\t";
4223 str = ch_strdup( val );
4224 for ( s = ldap_pvt_strtok( str, delimstr, &next );
4226 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
4228 if ( strlen(s) == 1 && *s == '*' ) {
4229 si->si_allattrs = 1;
4230 val[ s - str ] = delimstr[0];
4232 if ( strlen(s) == 1 && *s == '+' ) {
4233 si->si_allopattrs = 1;
4234 val [ s - str ] = delimstr[0];
4238 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
4239 if ( si->si_anlist == NULL ) {
4243 si->si_got |= GOT_ATTRS;
4244 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
4245 STRLENOF( EXATTRSSTR "=" ) ) )
4247 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
4248 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
4250 attr_fname = ch_strdup( val + STRLENOF(":include:") );
4251 si->si_exanlist = file2anlist(
4252 si->si_exanlist, attr_fname, " ,\t" );
4253 if ( si->si_exanlist == NULL ) {
4254 ch_free( attr_fname );
4257 ch_free( attr_fname );
4259 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
4260 if ( si->si_exanlist == NULL ) {
4264 si->si_got |= GOT_EXATTRS;
4265 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
4266 STRLENOF( TYPESTR "=" ) ) )
4268 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
4269 if ( !strncasecmp( val, "refreshOnly",
4270 STRLENOF("refreshOnly") ) )
4272 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
4273 } else if ( !strncasecmp( val, "refreshAndPersist",
4274 STRLENOF("refreshAndPersist") ) )
4276 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
4277 si->si_interval = 60;
4279 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4280 "Error: parse_syncrepl_line: "
4281 "unknown sync type \"%s\"", val);
4282 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4285 si->si_got |= GOT_TYPE;
4286 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
4287 STRLENOF( INTERVALSTR "=" ) ) )
4289 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
4290 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
4291 si->si_interval = 0;
4292 } else if ( strchr( val, ':' ) != NULL ) {
4293 char *next, *ptr = val;
4296 dd = strtol( ptr, &next, 10 );
4297 if ( next == ptr || next[0] != ':' || dd < 0 ) {
4298 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4299 "Error: parse_syncrepl_line: "
4300 "invalid interval \"%s\", unable to parse days", val );
4301 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4305 hh = strtol( ptr, &next, 10 );
4306 if ( next == ptr || next[0] != ':' || hh < 0 || hh > 24 ) {
4307 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4308 "Error: parse_syncrepl_line: "
4309 "invalid interval \"%s\", unable to parse hours", val );
4310 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4314 mm = strtol( ptr, &next, 10 );
4315 if ( next == ptr || next[0] != ':' || mm < 0 || mm > 60 ) {
4316 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4317 "Error: parse_syncrepl_line: "
4318 "invalid interval \"%s\", unable to parse minutes", val );
4319 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4323 ss = strtol( ptr, &next, 10 );
4324 if ( next == ptr || next[0] != '\0' || ss < 0 || ss > 60 ) {
4325 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4326 "Error: parse_syncrepl_line: "
4327 "invalid interval \"%s\", unable to parse seconds", val );
4328 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4331 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
4335 if ( lutil_parse_time( val, &t ) != 0 ) {
4336 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4337 "Error: parse_syncrepl_line: "
4338 "invalid interval \"%s\"", val );
4339 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4342 si->si_interval = (time_t)t;
4344 if ( si->si_interval < 0 ) {
4345 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4346 "Error: parse_syncrepl_line: "
4347 "invalid interval \"%ld\"",
4348 (long) si->si_interval);
4349 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4352 si->si_got |= GOT_INTERVAL;
4353 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
4354 STRLENOF( RETRYSTR "=" ) ) )
4356 if ( parse_syncrepl_retry( c, c->argv[ i ], si ) ) {
4359 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
4360 STRLENOF( MANAGEDSAITSTR "=" ) ) )
4362 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
4363 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
4364 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
4366 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4367 "invalid manageDSAit value \"%s\".\n",
4369 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4372 si->si_got |= GOT_MANAGEDSAIT;
4373 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
4374 STRLENOF( SLIMITSTR "=") ) )
4376 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
4377 if ( strcasecmp( val, "unlimited" ) == 0 ) {
4380 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
4381 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4382 "invalid size limit value \"%s\".\n",
4384 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4387 si->si_got |= GOT_SLIMIT;
4388 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
4389 STRLENOF( TLIMITSTR "=" ) ) )
4391 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
4392 if ( strcasecmp( val, "unlimited" ) == 0 ) {
4395 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
4396 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4397 "invalid time limit value \"%s\".\n",
4399 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4402 si->si_got |= GOT_TLIMIT;
4403 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
4404 STRLENOF( SYNCDATASTR "=" ) ) )
4406 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
4407 si->si_syncdata = verb_to_mask( val, datamodes );
4408 si->si_got |= GOT_SYNCDATA;
4409 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
4410 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4411 "Error: parse_syncrepl_line: "
4412 "unable to parse \"%s\"\n", c->argv[ i ] );
4413 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4416 si->si_got |= GOT_BINDCONF;
4419 if ( ( si->si_got & GOT_REQUIRED ) != GOT_REQUIRED ) {
4420 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4421 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
4422 si->si_got & GOT_RID ? "" : " "IDSTR,
4423 si->si_got & GOT_PROVIDER ? "" : " "PROVIDERSTR,
4424 si->si_got & GOT_SEARCHBASE ? "" : " "SEARCHBASESTR );
4425 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4429 if ( !( si->si_got & GOT_RETRY ) ) {
4430 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": no retry defined, using default\n",
4431 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", 0 );
4432 if ( si->si_retryinterval == NULL ) {
4433 if ( parse_syncrepl_retry( c, "retry=undefined", si ) ) {
4439 si->si_filter = str2filter( si->si_filterstr.bv_val );
4440 if ( si->si_filter == NULL ) {
4441 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": unable to parse filter=\"%s\"\n",
4442 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", si->si_filterstr.bv_val );
4456 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
4457 snprintf( c->cr_msg, sizeof(c->cr_msg), "database %s does not support "
4458 "operations required for syncrepl", c->be->be_type );
4459 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
4462 if ( BER_BVISEMPTY( &c->be->be_rootdn ) ) {
4463 strcpy( c->cr_msg, "rootDN must be defined before syncrepl may be used" );
4464 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
4467 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
4470 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
4474 si->si_bindconf.sb_tls = SB_TLS_OFF;
4475 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
4476 si->si_schemachecking = 0;
4477 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
4478 &si->si_filterstr );
4479 si->si_base.bv_val = NULL;
4480 si->si_scope = LDAP_SCOPE_SUBTREE;
4481 si->si_attrsonly = 0;
4482 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
4483 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
4484 si->si_attrs = NULL;
4485 si->si_allattrs = 0;
4486 si->si_allopattrs = 0;
4487 si->si_exattrs = NULL;
4488 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
4489 si->si_interval = 86400;
4490 si->si_retryinterval = NULL;
4491 si->si_retrynum_init = NULL;
4492 si->si_retrynum = NULL;
4493 si->si_manageDSAit = 0;
4497 si->si_presentlist = NULL;
4498 LDAP_LIST_INIT( &si->si_nonpresentlist );
4499 ldap_pvt_thread_mutex_init( &si->si_mutex );
4501 rc = parse_syncrepl_line( c, si );
4506 /* Must be LDAPv3 because we need controls */
4507 switch ( si->si_bindconf.sb_version ) {
4509 /* not explicitly set */
4510 si->si_bindconf.sb_version = LDAP_VERSION3;
4513 /* explicitly set */
4516 Debug( LDAP_DEBUG_ANY,
4517 "version %d incompatible with syncrepl\n",
4518 si->si_bindconf.sb_version, 0, 0 );
4519 syncinfo_free( si, 0 );
4523 if ( ldap_url_parse( si->si_bindconf.sb_uri.bv_val, &lud )) {
4524 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4525 "<%s> invalid URL", c->argv[0] );
4526 Debug( LDAP_DEBUG_ANY, "%s: %s %s\n",
4527 c->log, c->cr_msg, si->si_bindconf.sb_uri.bv_val );
4532 if ( slapMode & SLAP_SERVER_MODE ) {
4534 /* check if consumer points to current server and database.
4535 * If so, ignore this configuration.
4537 if ( !SLAP_DBHIDDEN( c->be ) ) {
4539 /* if searchbase doesn't match current DB suffix,
4540 * assume it's different
4542 for ( i=0; !BER_BVISNULL( &c->be->be_nsuffix[i] ); i++ ) {
4543 if ( bvmatch( &si->si_base, &c->be->be_nsuffix[i] )) {
4548 /* if searchbase matches, see if URLs match */
4549 if ( isMe && config_check_my_url( si->si_bindconf.sb_uri.bv_val,
4555 init_syncrepl( si );
4556 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4557 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq,
4558 si->si_interval, do_syncrepl, si, "do_syncrepl",
4560 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4562 rc = config_sync_shadow( c ) ? -1 : 0;
4567 /* mirrormode still needs to see this flag in tool mode */
4568 rc = config_sync_shadow( c ) ? -1 : 0;
4570 ldap_free_urldesc( lud );
4574 /* Use main slapd defaults */
4575 bindconf_tls_defaults( &si->si_bindconf );
4578 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
4579 syncinfo_free( si, 0 );
4582 Debug( LDAP_DEBUG_CONFIG,
4583 "Config: ** successfully added syncrepl \"%s\"\n",
4584 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
4585 "(null)" : si->si_bindconf.sb_uri.bv_val, 0, 0 );
4586 if ( c->be->be_syncinfo ) {
4589 si->si_cookieState = c->be->be_syncinfo->si_cookieState;
4591 /* add new syncrepl to end of list (same order as when deleting) */
4592 for ( sip = c->be->be_syncinfo; sip->si_next; sip = sip->si_next );
4595 si->si_cookieState = ch_calloc( 1, sizeof( cookie_state ));
4596 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_mutex );
4597 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_pmutex );
4599 c->be->be_syncinfo = si;
4601 si->si_cookieState->cs_ref++;
4610 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
4612 struct berval bc, uri, bs;
4613 char buf[BUFSIZ*2], *ptr;
4616 # define WHATSLEFT ((ber_len_t) (&buf[sizeof( buf )] - ptr))
4620 /* temporarily inhibit bindconf from printing URI */
4621 uri = si->si_bindconf.sb_uri;
4622 BER_BVZERO( &si->si_bindconf.sb_uri );
4623 si->si_bindconf.sb_version = 0;
4624 bindconf_unparse( &si->si_bindconf, &bc );
4625 si->si_bindconf.sb_uri = uri;
4626 si->si_bindconf.sb_version = LDAP_VERSION3;
4629 assert( si->si_rid >= 0 && si->si_rid <= SLAP_SYNC_RID_MAX );
4630 len = snprintf( ptr, WHATSLEFT, IDSTR "=%03d " PROVIDERSTR "=%s",
4631 si->si_rid, si->si_bindconf.sb_uri.bv_val );
4632 if ( len >= sizeof( buf ) ) return;
4634 if ( !BER_BVISNULL( &bc ) ) {
4635 if ( WHATSLEFT <= bc.bv_len ) {
4639 ptr = lutil_strcopy( ptr, bc.bv_val );
4642 if ( !BER_BVISEMPTY( &si->si_filterstr ) ) {
4643 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
4644 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
4645 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
4648 if ( !BER_BVISNULL( &si->si_base ) ) {
4649 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
4650 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
4651 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
4654 if ( !BER_BVISEMPTY( &si->si_logfilterstr ) ) {
4655 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
4656 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
4657 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
4660 if ( !BER_BVISNULL( &si->si_logbase ) ) {
4661 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
4662 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
4663 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
4666 if ( ldap_pvt_scope2bv( si->si_scope, &bs ) == LDAP_SUCCESS ) {
4667 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + bs.bv_len ) return;
4668 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
4669 ptr = lutil_strcopy( ptr, bs.bv_val );
4671 if ( si->si_attrsonly ) {
4672 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
4673 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
4675 if ( si->si_anfile ) {
4676 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
4677 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
4678 ptr = lutil_strcopy( ptr, si->si_anfile );
4680 } else if ( si->si_allattrs || si->si_allopattrs ||
4681 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ) )
4685 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
4686 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
4688 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
4689 if ( ptr == NULL ) return;
4690 if ( si->si_allattrs ) {
4691 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
4692 if ( old != ptr ) *ptr++ = ',';
4695 if ( si->si_allopattrs ) {
4696 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
4697 if ( old != ptr ) *ptr++ = ',';
4702 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
4703 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
4704 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
4705 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
4706 if ( ptr == NULL ) return;
4708 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
4709 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
4710 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
4712 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
4713 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
4714 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
4715 "refreshAndPersist" : "refreshOnly" );
4717 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
4720 dd = si->si_interval;
4727 len = snprintf( ptr, WHATSLEFT, " %s=%02d:%02d:%02d:%02d",
4728 INTERVALSTR, dd, hh, mm, ss );
4729 if ( len >= WHATSLEFT ) return;
4733 if ( si->si_got & GOT_RETRY ) {
4734 const char *space = "";
4735 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
4736 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
4737 for (i=0; si->si_retryinterval[i]; i++) {
4738 len = snprintf( ptr, WHATSLEFT, "%s%ld ", space,
4739 (long) si->si_retryinterval[i] );
4741 if ( WHATSLEFT - 1 <= len ) return;
4743 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
4746 len = snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
4747 if ( WHATSLEFT <= len ) return;
4751 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
4754 ptr = lutil_strcopy( ptr, " " RETRYSTR "=undefined" );
4757 if ( si->si_slimit ) {
4758 len = snprintf( ptr, WHATSLEFT, " " SLIMITSTR "=%d", si->si_slimit );
4759 if ( WHATSLEFT <= len ) return;
4763 if ( si->si_tlimit ) {
4764 len = snprintf( ptr, WHATSLEFT, " " TLIMITSTR "=%d", si->si_tlimit );
4765 if ( WHATSLEFT <= len ) return;
4769 if ( si->si_syncdata ) {
4770 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
4771 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
4772 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
4773 ptr = lutil_strcopy( ptr, bc.bv_val );
4776 bc.bv_len = ptr - buf;
4778 ber_dupbv( bv, &bc );
4782 syncrepl_config( ConfigArgs *c )
4784 if (c->op == SLAP_CONFIG_EMIT) {
4785 if ( c->be->be_syncinfo ) {
4789 for ( si = c->be->be_syncinfo; si; si=si->si_next ) {
4790 syncrepl_unparse( si, &bv );
4791 ber_bvarray_add( &c->rvalue_vals, &bv );
4796 } else if ( c->op == LDAP_MOD_DELETE ) {
4798 if ( c->be->be_syncinfo ) {
4799 syncinfo_t *si, **sip;
4802 for ( sip = &c->be->be_syncinfo, i=0; *sip; i++ ) {
4804 if ( c->valx == -1 || i == c->valx ) {
4808 /* If the task is currently active, we have to leave
4809 * it running. It will exit on its own. This will only
4810 * happen when running on the cn=config DB.
4813 if ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
4816 /* There is no active thread, but we must still
4817 * ensure that no thread is (or will be) queued
4818 * while we removes the task.
4820 struct re_s *re = si->si_re;
4823 if ( si->si_conn ) {
4824 connection_client_stop( si->si_conn );
4828 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4829 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) ) {
4830 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
4833 ldap_pvt_runqueue_remove( &slapd_rq, re );
4834 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4836 if ( ldap_pvt_thread_pool_retract( &connection_pool,
4837 re->routine, re ) > 0 )
4840 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
4844 syncinfo_free( si, 0 );
4853 if ( !c->be->be_syncinfo ) {
4854 SLAP_DBFLAGS( c->be ) &= ~SLAP_DBFLAG_SHADOW_MASK;
4858 if ( SLAP_SLURP_SHADOW( c->be ) ) {
4859 Debug(LDAP_DEBUG_ANY, "%s: "
4860 "syncrepl: database already shadowed.\n",
4864 return add_syncrepl( c );