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-2007 The OpenLDAP Foundation.
6 * Portions Copyright 2003 by IBM Corporation.
7 * Portions Copyright 2003 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;
44 struct berval *cs_vals;
48 #define SYNCDATA_DEFAULT 0 /* entries are plain LDAP entries */
49 #define SYNCDATA_ACCESSLOG 1 /* entries are accesslog format */
50 #define SYNCDATA_CHANGELOG 2 /* entries are changelog format */
52 #define SYNCLOG_LOGGING 0 /* doing a log-based update */
53 #define SYNCLOG_FALLBACK 1 /* doing a full refresh */
55 #define RETRYNUM_FOREVER (-1) /* retry forever */
56 #define RETRYNUM_TAIL (-2) /* end of retrynum array */
57 #define RETRYNUM_VALID(n) ((n) >= RETRYNUM_FOREVER) /* valid retrynum */
58 #define RETRYNUM_FINITE(n) ((n) > RETRYNUM_FOREVER) /* not forever */
60 typedef struct syncinfo_s {
61 struct syncinfo_s *si_next;
67 slap_bindconf si_bindconf;
68 struct berval si_base;
69 struct berval si_logbase;
70 struct berval si_filterstr;
71 struct berval si_logfilterstr;
75 AttributeName *si_anlist;
76 AttributeName *si_exanlist;
81 int si_schemachecking;
82 int si_type; /* the active type */
83 int si_ctype; /* the configured type */
85 time_t *si_retryinterval;
86 int *si_retrynum_init;
88 struct sync_cookie si_syncCookie;
89 cookie_state *si_cookieState;
95 int si_refreshPresent;
99 Avlnode *si_presentlist;
101 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
102 ldap_pvt_thread_mutex_t si_mutex;
105 static int syncuuid_cmp( const void *, const void * );
106 static void avl_ber_bvfree( void * );
107 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray, struct berval * );
108 static int syncrepl_message_to_op(
109 syncinfo_t *, Operation *, LDAPMessage * );
110 static int syncrepl_message_to_entry(
111 syncinfo_t *, Operation *, LDAPMessage *,
112 Modifications **, Entry **, int );
113 static int syncrepl_entry(
114 syncinfo_t *, Operation*, Entry*,
115 Modifications**,int, struct berval* );
116 static int syncrepl_updateCookie(
117 syncinfo_t *, Operation *, struct berval *,
118 struct sync_cookie * );
119 static struct berval * slap_uuidstr_from_normalized(
120 struct berval *, struct berval *, void * );
122 /* callback functions */
123 static int dn_callback( Operation *, SlapReply * );
124 static int nonpresent_callback( Operation *, SlapReply * );
125 static int null_callback( Operation *, SlapReply * );
127 static AttributeDescription *sync_descs[4];
130 init_syncrepl(syncinfo_t *si)
133 char **attrs, **exattrs;
135 if ( !sync_descs[0] ) {
136 sync_descs[0] = slap_schema.si_ad_objectClass;
137 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
138 sync_descs[2] = slap_schema.si_ad_entryCSN;
139 sync_descs[3] = NULL;
142 if ( si->si_allattrs && si->si_allopattrs )
145 attrs = anlist2attrs( si->si_anlist );
148 if ( si->si_allattrs ) {
151 if ( !is_at_operational( at_find( attrs[i] ) ) ) {
152 for ( j = i; attrs[j] != NULL; j++ ) {
155 attrs[j] = attrs[j+1];
161 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
162 attrs[i] = ch_strdup("*");
165 } else if ( si->si_allopattrs ) {
168 if ( is_at_operational( at_find( attrs[i] ) ) ) {
169 for ( j = i; attrs[j] != NULL; j++ ) {
172 attrs[j] = attrs[j+1];
178 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
179 attrs[i] = ch_strdup("+");
183 for ( i = 0; sync_descs[i] != NULL; i++ ) {
186 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
187 for ( k = j; attrs[k] != NULL; k++ ) {
190 attrs[k] = attrs[k+1];
198 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
200 if ( si->si_allopattrs ) {
201 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ) );
203 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ) );
206 if ( attrs == NULL ) {
207 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0, 0, 0 );
211 if ( si->si_allopattrs ) {
212 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
214 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
215 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
223 if ( si->si_allattrs == si->si_allopattrs ) {
224 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
225 attrs[i++] = ch_strdup( "*" );
226 attrs[i++] = ch_strdup( "+" );
227 } else if ( si->si_allattrs && !si->si_allopattrs ) {
228 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
229 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
230 attrs[i++] = ch_strdup( "*" );
231 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
232 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
234 } else if ( !si->si_allattrs && si->si_allopattrs ) {
235 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
236 attrs[i++] = ch_strdup( "+" );
237 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
242 si->si_attrs = attrs;
244 exattrs = anlist2attrs( si->si_exanlist );
247 for ( n = 0; exattrs[n] != NULL; n++ ) ;
249 for ( i = 0; sync_descs[i] != NULL; i++ ) {
251 while ( exattrs[j] != NULL ) {
252 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
253 ch_free( exattrs[j] );
254 for ( k = j; exattrs[k] != NULL; k++ ) {
255 exattrs[k] = exattrs[k+1];
263 for ( i = 0; exattrs[i] != NULL; i++ ) {
264 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
266 if ( ( oc = si->si_anlist[j].an_oc ) ) {
268 while ( oc->soc_required[k] ) {
269 if ( !strcmp( exattrs[i],
270 oc->soc_required[k]->sat_cname.bv_val ) ) {
271 ch_free( exattrs[i] );
272 for ( l = i; exattrs[l]; l++ ) {
273 exattrs[l] = exattrs[l+1];
283 for ( i = 0; exattrs[i] != NULL; i++ ) ;
286 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *) );
289 si->si_exattrs = exattrs;
292 typedef struct logschema {
294 struct berval ls_req;
295 struct berval ls_mod;
296 struct berval ls_newRdn;
297 struct berval ls_delRdn;
298 struct berval ls_newSup;
301 static logschema changelog_sc = {
303 BER_BVC("changeType"),
306 BER_BVC("deleteOldRDN"),
307 BER_BVC("newSuperior")
310 static logschema accesslog_sc = {
314 BER_BVC("reqNewRDN"),
315 BER_BVC("reqDeleteOldRDN"),
316 BER_BVC("reqNewSuperior")
324 BerElementBuffer berbuf;
325 BerElement *ber = (BerElement *)&berbuf;
326 LDAPControl c[2], *ctrls[3];
327 struct timeval timeout;
332 char **attrs, *lattrs[8];
337 /* setup LDAP SYNC control */
338 ber_init2( ber, NULL, LBER_USE_DER );
339 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
341 /* If we're using a log but we have no state, then fallback to
342 * normal mode for a full refresh.
344 if ( si->si_syncdata && !si->si_syncCookie.numcsns ) {
345 si->si_logstate = SYNCLOG_FALLBACK;
348 /* Use the log parameters if we're in log mode */
349 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
351 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
355 lattrs[0] = ls->ls_dn.bv_val;
356 lattrs[1] = ls->ls_req.bv_val;
357 lattrs[2] = ls->ls_mod.bv_val;
358 lattrs[3] = ls->ls_newRdn.bv_val;
359 lattrs[4] = ls->ls_delRdn.bv_val;
360 lattrs[5] = ls->ls_newSup.bv_val;
361 lattrs[6] = slap_schema.si_ad_entryCSN->ad_cname.bv_val;
365 base = si->si_logbase.bv_val;
366 filter = si->si_logfilterstr.bv_val;
369 scope = LDAP_SCOPE_SUBTREE;
372 base = si->si_base.bv_val;
373 filter = si->si_filterstr.bv_val;
374 attrs = si->si_attrs;
375 attrsonly = si->si_attrsonly;
376 scope = si->si_scope;
378 if ( si->si_syncdata && si->si_logstate == SYNCLOG_FALLBACK ) {
379 si->si_type = LDAP_SYNC_REFRESH_ONLY;
381 si->si_type = si->si_ctype;
384 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
386 ber_printf( ber, "{eOb}",
387 abs(si->si_type), &si->si_syncCookie.octet_str, rhint );
389 ber_printf( ber, "{eb}",
390 abs(si->si_type), rhint );
393 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 ) ) == LBER_ERROR ) {
398 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
399 c[0].ldctl_iscritical = si->si_type < 0;
402 if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
403 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
404 c[1].ldctl_value = si->si_bindconf.sb_authzId;
405 c[1].ldctl_iscritical = 1;
412 timeout.tv_sec = si->si_tlimit;
415 rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
416 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
417 si->si_slimit, &msgid );
428 int cmdline_cookie_found = 0;
430 struct sync_cookie *sc = NULL;
435 rc = slap_client_connect( &si->si_ld, &si->si_bindconf );
436 if ( rc != LDAP_SUCCESS ) {
439 op->o_protocol = LDAP_VERSION3;
441 /* Set SSF to strongest of TLS, SASL SSFs */
444 op->o_transport_ssf = 0;
446 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
447 == LDAP_SUCCESS && ssl != NULL )
449 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
451 #endif /* HAVE_TLS */
452 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
453 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
454 ? op->o_sasl_ssf : op->o_tls_ssf;
456 /* We've just started up, or the remote server hasn't sent us
457 * any meaningful state.
459 if ( BER_BVISNULL( &si->si_syncCookie.octet_str ) ) {
462 si->si_syncCookie.rid = si->si_rid;
463 si->si_syncCookie.sid = SLAP_SINGLE_SHADOW( si->si_be ) ? -1 :
466 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
467 if ( si->si_rid == sc->rid ) {
468 cmdline_cookie_found = 1;
473 if ( cmdline_cookie_found ) {
474 /* cookie is supplied in the command line */
476 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
478 /* ctxcsn wasn't parsed yet, do it now */
479 slap_parse_sync_cookie( sc, op->o_tmpmemctx );
480 slap_sync_cookie_free( &si->si_syncCookie, 0 );
481 slap_dup_sync_cookie( &si->si_syncCookie, sc );
482 slap_sync_cookie_free( sc, 1 );
484 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
485 if ( !si->si_cookieState->cs_num ) {
486 /* get contextCSN shadow replica from database */
487 BerVarray csn = NULL;
488 void *ctx = op->o_tmpmemctx;
490 op->o_req_ndn = op->o_bd->be_nsuffix[0];
491 op->o_req_dn = op->o_req_ndn;
493 /* try to read stored contextCSN */
494 op->o_tmpmemctx = NULL;
495 backend_attribute( op, NULL, &op->o_req_ndn,
496 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
497 op->o_tmpmemctx = ctx;
499 si->si_cookieState->cs_vals = csn;
500 for (i=0; !BER_BVISNULL( &csn[i] ); i++);
501 si->si_cookieState->cs_num = i;
502 si->si_cookieState->cs_sids = slap_parse_csn_sids( csn, i, NULL );
505 if ( si->si_cookieState->cs_num ) {
506 ber_bvarray_free( si->si_syncCookie.ctxcsn );
507 if ( ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
508 si->si_cookieState->cs_vals, NULL )) {
512 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
513 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num *
515 for ( i=0; i<si->si_syncCookie.numcsns; i++ )
516 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
518 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
521 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
522 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
523 si->si_syncCookie.sid );
526 Attribute a = { slap_schema.si_ad_contextCSN };
529 int i, j, changed = 0;
531 /* Look for contextCSN from syncprov overlay. If
532 * there's no overlay, this will be a no-op. That means
533 * this is a pure consumer, so local changes will not be
534 * allowed, and all changes will already be reflected in
538 e.e_name = si->si_wbe->be_suffix[0];
539 e.e_nname = si->si_wbe->be_nsuffix[0];
541 rs.sr_flags = REP_ENTRY_MODIFIABLE;
542 at[0].an_name = a.a_desc->ad_cname;
543 at[0].an_desc = a.a_desc;
544 BER_BVZERO( &at[1].an_name );
546 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
547 rc = backend_operational( op, &rs );
548 if ( rc == LDAP_SUCCESS && a.a_vals ) {
550 for (i=0; !BER_BVISNULL( &a.a_vals[i] ); i++) ;
552 /* check for differences */
553 if ( num != si->si_cookieState->cs_num ) {
556 for ( i=0; i<num; i++ ) {
557 if ( ber_bvcmp( &a.a_vals[i],
558 &si->si_cookieState->cs_vals[i] )) {
565 ber_bvarray_free( si->si_cookieState->cs_vals );
566 ch_free( si->si_cookieState->cs_sids );
567 si->si_cookieState->cs_num = num;
568 si->si_cookieState->cs_vals = a.a_vals;
569 si->si_cookieState->cs_sids = slap_parse_csn_sids( a.a_vals,
571 si->si_cookieState->cs_age++;
573 ber_bvarray_free( a.a_vals );
577 /* See if the cookieState has changed due to anything outside
578 * this particular consumer. That includes other consumers in
579 * the same context, or local changes detected above.
581 if ( si->si_cookieState->cs_num > 1 && si->si_cookieAge !=
582 si->si_cookieState->cs_age ) {
584 for (i=0; !BER_BVISNULL( &si->si_syncCookie.ctxcsn[i] ); i++) {
585 /* bogus, just dup everything */
586 if ( si->si_syncCookie.sids[i] == -1 ) {
587 ber_bvarray_free( si->si_syncCookie.ctxcsn );
588 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
589 si->si_cookieState->cs_vals, NULL );
593 for (j=0; j<si->si_cookieState->cs_num; j++) {
594 if ( si->si_syncCookie.sids[i] !=
595 si->si_cookieState->cs_sids[j] )
597 if ( bvmatch( &si->si_syncCookie.ctxcsn[i],
598 &si->si_cookieState->cs_vals[j] ))
600 ber_bvreplace( &si->si_syncCookie.ctxcsn[i],
601 &si->si_cookieState->cs_vals[j] );
607 ch_free( si->si_syncCookie.octet_str.bv_val );
608 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
609 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
610 SLAP_SINGLE_SHADOW( si->si_be ) ? -1 : slap_serverID );
613 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
616 rc = ldap_sync_search( si, op->o_tmpmemctx );
618 if( rc != LDAP_SUCCESS ) {
619 Debug( LDAP_DEBUG_ANY, "do_syncrep1: %s "
620 "ldap_search_ext: %s (%d)\n",
621 si->si_ridtxt, ldap_err2string( rc ), rc );
627 ldap_unbind_ext( si->si_ld, NULL, NULL );
636 compare_csns( struct sync_cookie *sc1, struct sync_cookie *sc2, int *which )
643 if ( sc1->numcsns < sc2->numcsns ) {
644 *which = sc1->numcsns;
648 for (i=0; !BER_BVISNULL( &sc1->ctxcsn[i] ); i++) {
649 for (j=0; !BER_BVISNULL( &sc2->ctxcsn[j] ); j++) {
650 if ( sc1->sids[i] != sc2->sids[j] )
652 value_match( &match, slap_schema.si_ad_entryCSN,
653 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
654 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
655 &sc1->ctxcsn[i], &sc2->ctxcsn[i], &text );
671 LDAPControl **rctrls = NULL;
674 BerElementBuffer berbuf;
675 BerElement *ber = (BerElement *)&berbuf;
677 LDAPMessage *res = NULL;
678 LDAPMessage *msg = NULL;
681 struct berval *retdata = NULL;
686 struct berval syncUUID = BER_BVNULL;
687 struct sync_cookie syncCookie = { NULL };
688 struct sync_cookie syncCookie_req = { NULL };
689 struct berval cookie = BER_BVNULL;
695 Modifications *modlist = NULL;
699 struct timeval *tout_p = NULL;
700 struct timeval tout = { 0, 0 };
702 int refreshDeletes = 0;
704 BerVarray syncUUIDs = NULL;
707 if ( slapd_shutdown ) {
712 ber_init2( ber, NULL, LBER_USE_DER );
713 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
715 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2 %s\n", si->si_ridtxt, 0, 0 );
717 psub = &si->si_be->be_nsuffix[0];
719 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
721 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
727 while ( ( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
728 tout_p, &res ) ) > 0 )
730 if ( slapd_shutdown ) {
734 for( msg = ldap_first_message( si->si_ld, res );
736 msg = ldap_next_message( si->si_ld, msg ) )
738 if ( slapd_shutdown ) {
742 switch( ldap_msgtype( msg ) ) {
743 case LDAP_RES_SEARCH_ENTRY:
744 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
745 /* we can't work without the control */
748 /* NOTE: make sure we use the right one;
749 * a better approach would be to run thru
750 * the whole list and take care of all */
751 rctrlp = ldap_find_control( LDAP_CONTROL_SYNC_STATE, rctrls );
753 if ( rctrlp == NULL ) {
754 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
755 "got search entry without "
756 "Sync State control\n", si->si_ridtxt, 0, 0 );
760 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
761 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
762 /* FIXME: what if syncUUID is NULL or empty?
763 * (happens with back-sql...) */
764 if ( BER_BVISEMPTY( &syncUUID ) ) {
765 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
766 "got empty syncUUID\n", si->si_ridtxt, 0, 0 );
767 ldap_controls_free( rctrls );
771 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
772 ber_scanf( ber, /*"{"*/ "m}", &cookie );
773 if ( !BER_BVISNULL( &cookie ) ) {
774 ch_free( syncCookie.octet_str.bv_val );
775 ber_dupbv( &syncCookie.octet_str, &cookie );
777 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
779 slap_parse_sync_cookie( &syncCookie, NULL );
783 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
785 if ( ( rc = syncrepl_message_to_op( si, op, msg ) ) == LDAP_SUCCESS &&
788 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
790 } else if ( ( rc = syncrepl_message_to_entry( si, op, msg,
791 &modlist, &entry, syncstate ) ) == LDAP_SUCCESS )
793 if ( ( rc = syncrepl_entry( si, op, entry, &modlist,
794 syncstate, &syncUUID ) ) == LDAP_SUCCESS &&
797 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
800 ldap_controls_free( rctrls );
802 slap_mods_free( modlist, 1 );
808 case LDAP_RES_SEARCH_REFERENCE:
809 Debug( LDAP_DEBUG_ANY,
810 "do_syncrep2: %s reference received error\n",
811 si->si_ridtxt, 0, 0 );
814 case LDAP_RES_SEARCH_RESULT:
815 Debug( LDAP_DEBUG_SYNC,
816 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT\n",
817 si->si_ridtxt, 0, 0 );
818 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
820 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
821 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
822 /* map old result code to registered code */
823 err = LDAP_SYNC_REFRESH_REQUIRED;
826 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
827 if ( si->si_logstate == SYNCLOG_LOGGING ) {
828 si->si_logstate = SYNCLOG_FALLBACK;
835 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
837 ber_scanf( ber, "{" /*"}"*/);
838 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
839 ber_scanf( ber, "m", &cookie );
840 if ( !BER_BVISNULL( &cookie ) ) {
841 ch_free( syncCookie.octet_str.bv_val );
842 ber_dupbv( &syncCookie.octet_str, &cookie);
844 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
846 slap_parse_sync_cookie( &syncCookie, NULL );
849 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
851 ber_scanf( ber, "b", &refreshDeletes );
853 ber_scanf( ber, /*"{"*/ "}" );
855 if ( !syncCookie.ctxcsn ) {
857 } else if ( !syncCookie_req.ctxcsn ) {
861 match = compare_csns( &syncCookie_req, &syncCookie, &m );
864 ldap_controls_free( rctrls );
866 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
867 /* FIXME : different error behaviors according to
868 * 1) err code : LDAP_BUSY ...
869 * 2) on err policy : stop service, stop sync, retry
871 if ( refreshDeletes == 0 && match < 0 &&
872 err == LDAP_SUCCESS )
874 syncrepl_del_nonpresent( op, si, NULL,
875 &syncCookie.ctxcsn[m] );
877 avl_free( si->si_presentlist, avl_ber_bvfree );
878 si->si_presentlist = NULL;
881 if ( syncCookie.ctxcsn && match < 0 && err == LDAP_SUCCESS )
883 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
885 if ( err == LDAP_SUCCESS
886 && si->si_logstate == SYNCLOG_FALLBACK ) {
887 si->si_logstate = SYNCLOG_LOGGING;
888 rc = LDAP_SYNC_REFRESH_REQUIRED;
895 case LDAP_RES_INTERMEDIATE:
896 rc = ldap_parse_intermediate( si->si_ld, msg,
897 &retoid, &retdata, NULL, 0 );
898 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
899 ber_init2( ber, retdata, LBER_USE_DER );
901 switch ( si_tag = ber_peek_tag( ber, &len ) ) {
903 case LDAP_TAG_SYNC_NEW_COOKIE:
904 Debug( LDAP_DEBUG_SYNC,
905 "do_syncrep2: %s %s - %s\n",
907 "LDAP_RES_INTERMEDIATE",
909 ber_scanf( ber, "tm", &tag, &cookie );
911 case LDAP_TAG_SYNC_REFRESH_DELETE:
912 case LDAP_TAG_SYNC_REFRESH_PRESENT:
913 Debug( LDAP_DEBUG_SYNC,
914 "do_syncrep2: %s %s - %s\n",
916 "LDAP_RES_INTERMEDIATE",
917 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
918 "REFRESH_PRESENT" : "REFRESH_DELETE" );
919 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
920 si->si_refreshDelete = 1;
922 si->si_refreshPresent = 1;
924 ber_scanf( ber, "t{" /*"}"*/, &tag );
925 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
927 ber_scanf( ber, "m", &cookie );
928 if ( !BER_BVISNULL( &cookie ) ) {
929 ch_free( syncCookie.octet_str.bv_val );
930 ber_dupbv( &syncCookie.octet_str, &cookie );
932 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
934 slap_parse_sync_cookie( &syncCookie, NULL );
937 if ( ber_peek_tag( ber, &len ) ==
938 LDAP_TAG_REFRESHDONE )
940 ber_scanf( ber, "b", &refreshDone );
942 ber_scanf( ber, /*"{"*/ "}" );
944 case LDAP_TAG_SYNC_ID_SET:
945 Debug( LDAP_DEBUG_SYNC,
946 "do_syncrep2: %s %s - %s\n",
948 "LDAP_RES_INTERMEDIATE",
950 ber_scanf( ber, "t{" /*"}"*/, &tag );
951 if ( ber_peek_tag( ber, &len ) ==
952 LDAP_TAG_SYNC_COOKIE )
954 ber_scanf( ber, "m", &cookie );
955 if ( !BER_BVISNULL( &cookie ) ) {
956 ch_free( syncCookie.octet_str.bv_val );
957 ber_dupbv( &syncCookie.octet_str, &cookie );
959 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
961 slap_parse_sync_cookie( &syncCookie, NULL );
962 compare_csns( &syncCookie_req, &syncCookie, &m );
965 if ( ber_peek_tag( ber, &len ) ==
966 LDAP_TAG_REFRESHDELETES )
968 ber_scanf( ber, "b", &refreshDeletes );
970 ber_scanf( ber, "[W]", &syncUUIDs );
971 ber_scanf( ber, /*"{"*/ "}" );
972 if ( refreshDeletes ) {
973 syncrepl_del_nonpresent( op, si, syncUUIDs,
974 &syncCookie.ctxcsn[m] );
975 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
978 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
979 struct berval *syncuuid_bv;
980 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
981 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
982 if ( avl_insert( &si->si_presentlist,
983 (caddr_t) syncuuid_bv,
984 syncuuid_cmp, avl_dup_error ) )
986 ber_bvfree( syncuuid_bv );
989 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
991 slap_sync_cookie_free( &syncCookie, 0 );
994 Debug( LDAP_DEBUG_ANY,
995 "do_syncrep2: %s unknown syncinfo tag (%ld)\n",
996 si->si_ridtxt, (long) si_tag, 0 );
997 ldap_memfree( retoid );
998 ber_bvfree( retdata );
1002 if ( !syncCookie.ctxcsn ) {
1004 } else if ( !syncCookie_req.ctxcsn ) {
1008 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1012 if ( si->si_refreshPresent == 1 ) {
1013 syncrepl_del_nonpresent( op, si, NULL,
1014 &syncCookie.ctxcsn[m] );
1017 if ( syncCookie.ctxcsn )
1019 rc = syncrepl_updateCookie( si, op, psub, &syncCookie);
1023 ldap_memfree( retoid );
1024 ber_bvfree( retdata );
1028 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1029 "unknown intermediate response (%d)\n",
1030 si->si_ridtxt, rc, 0 );
1031 ldap_memfree( retoid );
1032 ber_bvfree( retdata );
1038 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1039 "unknown message\n", si->si_ridtxt, 0, 0 );
1043 if ( !BER_BVISNULL( &syncCookie.octet_str ) ) {
1044 slap_sync_cookie_free( &syncCookie_req, 0 );
1045 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
1046 slap_sync_cookie_free( &syncCookie, 0 );
1049 ldap_msgfree( res );
1056 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
1057 errstr = ldap_err2string( rc );
1059 Debug( LDAP_DEBUG_ANY,
1060 "do_syncrep2: %s %s\n", si->si_ridtxt, errstr, 0 );
1064 slap_sync_cookie_free( &syncCookie, 0 );
1065 slap_sync_cookie_free( &syncCookie_req, 0 );
1067 if ( res ) ldap_msgfree( res );
1069 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1070 if ( si->si_conn_setup ) {
1072 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1073 connection_client_stop( s );
1074 si->si_conn_setup = 0;
1076 ldap_unbind_ext( si->si_ld, NULL, NULL );
1088 struct re_s* rtask = arg;
1089 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1090 Connection conn = {0};
1091 OperationBuffer opbuf;
1093 int rc = LDAP_SUCCESS;
1099 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl %s\n", si->si_ridtxt, 0, 0 );
1104 ldap_pvt_thread_mutex_lock( &si->si_mutex );
1106 switch( abs( si->si_type ) ) {
1107 case LDAP_SYNC_REFRESH_ONLY:
1108 case LDAP_SYNC_REFRESH_AND_PERSIST:
1111 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1115 if ( slapd_shutdown ) {
1117 if ( si->si_conn_setup ) {
1118 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1119 connection_client_stop( s );
1120 si->si_conn_setup = 0;
1122 ldap_unbind_ext( si->si_ld, NULL, NULL );
1125 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1129 op = (Operation *) &opbuf;
1130 connection_fake_init( &conn, op, ctx );
1132 /* use global malloc for now */
1133 op->o_tmpmemctx = NULL;
1134 op->o_tmpmfuncs = &ch_mfuncs;
1136 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1137 op->o_bd = be = si->si_be;
1138 op->o_dn = op->o_bd->be_rootdn;
1139 op->o_ndn = op->o_bd->be_rootndn;
1140 if ( !si->si_schemachecking )
1141 op->o_no_schema_check = 1;
1143 /* If we're glued, send writes through the glue parent */
1144 if ( !si->si_wbe ) {
1145 if ( SLAP_GLUE_SUBORDINATE( be )) {
1146 si->si_wbe = select_backend( &be->be_nsuffix[0], 0, 1 );
1152 /* Establish session, do search */
1154 si->si_refreshDelete = 0;
1155 si->si_refreshPresent = 0;
1156 rc = do_syncrep1( op, si );
1160 /* Process results */
1161 if ( rc == LDAP_SUCCESS ) {
1162 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1164 rc = do_syncrep2( op, si );
1165 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1166 rc = ldap_sync_search( si, op->o_tmpmemctx );
1170 /* We got deleted while running on cn=config */
1171 if ( !si->si_ctype ) {
1172 if ( si->si_conn_setup )
1177 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1178 /* If we succeeded, enable the connection for further listening.
1179 * If we failed, tear down the connection and reschedule.
1181 if ( rc == LDAP_SUCCESS ) {
1182 if ( si->si_conn_setup ) {
1183 connection_client_enable( s );
1185 rc = connection_client_setup( s, do_syncrepl, arg );
1187 si->si_conn_setup = 1;
1189 } else if ( si->si_conn_setup ) {
1193 if ( rc == -2 ) rc = 0;
1197 /* At this point, we have 4 cases:
1198 * 1) for any hard failure, give up and remove this task
1199 * 2) for ServerDown, reschedule this task to run
1200 * 3) for Refresh and Success, reschedule to run
1201 * 4) for Persist and Success, reschedule to defer
1203 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1205 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask ) ) {
1206 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1210 connection_client_stop( s );
1213 if ( rc == LDAP_SUCCESS ) {
1214 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1217 rtask->interval.tv_sec = si->si_interval;
1218 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1219 if ( si->si_retrynum ) {
1220 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1221 si->si_retrynum[i] = si->si_retrynum_init[i];
1223 si->si_retrynum[i] = RETRYNUM_TAIL;
1226 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1227 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1232 || !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1233 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1234 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1235 if ( si->si_retrynum[i] > 0 )
1236 si->si_retrynum[i]--;
1237 rtask->interval.tv_sec = si->si_retryinterval[i];
1238 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1239 slap_wake_listener();
1243 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1244 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1246 /* Do final delete cleanup */
1247 if ( !si->si_ctype ) {
1248 cookie_state *cs = NULL;
1251 cs = be->be_syncinfo->si_cookieState;
1252 for ( sip = &be->be_syncinfo; *sip != si; sip = &(*sip)->si_next );
1254 syncinfo_free( si, 0 );
1255 if ( !be->be_syncinfo ) {
1256 SLAP_DBFLAGS( be ) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
1258 ch_free( cs->cs_sids );
1259 ber_bvarray_free( cs->cs_vals );
1260 ldap_pvt_thread_mutex_destroy( &cs->cs_mutex );
1268 static slap_verbmasks modops[] = {
1269 { BER_BVC("add"), LDAP_REQ_ADD },
1270 { BER_BVC("delete"), LDAP_REQ_DELETE },
1271 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1272 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1276 static Modifications *
1277 syncrepl_accesslog_mods(
1284 AttributeDescription *ad;
1285 struct berval bv, bv2;
1287 Modifications *mod = NULL, *modlist = NULL, **modtail;
1292 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1296 colon = ber_bvchr( &bv, ':' );
1302 bv.bv_len = colon - bv.bv_val;
1303 if ( slap_bv2ad( &bv, &ad, &text ) ) {
1308 /* Ignore dynamically generated attrs */
1309 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1313 /* Ignore excluded attrs */
1314 if ( ldap_charray_inlist( si->si_exattrs,
1315 ad->ad_type->sat_cname.bv_val ) )
1321 case '+': op = LDAP_MOD_ADD; break;
1322 case '-': op = LDAP_MOD_DELETE; break;
1323 case '=': op = LDAP_MOD_REPLACE; break;
1324 case '#': op = LDAP_MOD_INCREMENT; break;
1328 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1329 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1332 mod->sml_next = NULL;
1334 mod->sml_type = ad->ad_cname;
1335 mod->sml_values = NULL;
1336 mod->sml_nvalues = NULL;
1339 modtail = &mod->sml_next;
1341 if ( colon[2] == ' ' ) {
1342 bv.bv_val = colon + 3;
1343 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1344 ber_dupbv( &bv2, &bv );
1345 ber_bvarray_add( &mod->sml_values, &bv2 );
1351 static Modifications *
1352 syncrepl_changelog_mods(
1357 return NULL; /* FIXME */
1361 syncrepl_message_to_op(
1367 BerElement *ber = NULL;
1368 Modifications *modlist = NULL;
1370 SlapReply rs = { REP_RESULT };
1371 slap_callback cb = { NULL, null_callback, NULL, NULL };
1374 char txtbuf[SLAP_TEXT_BUFLEN];
1375 size_t textlen = sizeof txtbuf;
1377 struct berval bdn, dn = BER_BVNULL, ndn;
1378 struct berval bv, *bvals = NULL;
1379 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1380 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1381 psup = BER_BVNULL, nsup = BER_BVNULL;
1382 int rc, deleteOldRdn = 0, freeReqDn = 0;
1384 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1385 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1386 "Message type should be entry (%d)",
1387 si->si_ridtxt, ldap_msgtype( msg ), 0 );
1391 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1396 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1398 if ( rc != LDAP_SUCCESS ) {
1399 Debug( LDAP_DEBUG_ANY,
1400 "syncrepl_message_to_op: %s dn get failed (%d)",
1401 si->si_ridtxt, rc, 0 );
1405 op->o_tag = LBER_DEFAULT;
1406 op->o_bd = si->si_wbe;
1408 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ) )
1410 if ( bv.bv_val == NULL )
1413 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn ) ) {
1415 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1416 ber_dupbv( &op->o_req_dn, &dn );
1417 ber_dupbv( &op->o_req_ndn, &ndn );
1418 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1419 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1421 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req ) ) {
1422 int i = verb_to_mask( bvals[0].bv_val, modops );
1424 Debug( LDAP_DEBUG_ANY,
1425 "syncrepl_message_to_op: %s unknown op %s",
1426 si->si_ridtxt, bvals[0].bv_val, 0 );
1431 op->o_tag = modops[i].mask;
1432 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod ) ) {
1433 /* Parse attribute into modlist */
1434 if ( si->si_syncdata == SYNCDATA_ACCESSLOG ) {
1435 modlist = syncrepl_accesslog_mods( si, bvals );
1437 modlist = syncrepl_changelog_mods( si, bvals );
1439 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn ) ) {
1441 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn ) ) {
1442 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ) ) {
1445 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup ) ) {
1447 } else if ( !ber_bvstrcasecmp( &bv,
1448 &slap_schema.si_ad_entryCSN->ad_cname ) )
1450 slap_queue_csn( op, bvals );
1455 /* If we didn't get a mod type or a target DN, bail out */
1456 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn ) ) {
1461 op->o_callback = &cb;
1462 slap_op_time( &op->o_time, &op->o_tincr );
1464 switch( op->o_tag ) {
1466 case LDAP_REQ_MODIFY:
1467 /* If we didn't get required data, bail */
1468 if ( !modlist ) goto done;
1470 rc = slap_mods_check( op, modlist, &text, txtbuf, textlen, NULL );
1472 if ( rc != LDAP_SUCCESS ) {
1473 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1474 "mods check (%s)\n",
1475 si->si_ridtxt, text, 0 );
1479 if ( op->o_tag == LDAP_REQ_ADD ) {
1480 Entry *e = entry_alloc();
1482 op->ora_e->e_name = op->o_req_dn;
1483 op->ora_e->e_nname = op->o_req_ndn;
1485 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1486 if( rc != LDAP_SUCCESS ) {
1487 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1488 "mods2entry (%s)\n",
1489 si->si_ridtxt, text, 0 );
1491 rc = op->o_bd->be_add( op, &rs );
1492 Debug( LDAP_DEBUG_SYNC,
1493 "syncrepl_message_to_op: %s be_add %s (%d)\n",
1494 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1496 if ( e == op->ora_e )
1497 be_entry_release_w( op, op->ora_e );
1499 op->orm_modlist = modlist;
1500 op->o_bd = si->si_wbe;
1501 rc = op->o_bd->be_modify( op, &rs );
1502 modlist = op->orm_modlist;
1503 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1504 "syncrepl_message_to_op: %s be_modify %s (%d)\n",
1505 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1506 op->o_bd = si->si_be;
1509 case LDAP_REQ_MODRDN:
1510 if ( BER_BVISNULL( &rdn ) ) goto done;
1512 if ( rdnPretty( NULL, &rdn, &prdn, NULL ) ) {
1515 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ) ) {
1518 if ( !BER_BVISNULL( &sup ) ) {
1519 if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ) ) {
1522 op->orr_newSup = &psup;
1523 op->orr_nnewSup = ⊅
1525 op->orr_newSup = NULL;
1526 op->orr_nnewSup = NULL;
1528 op->orr_newrdn = prdn;
1529 op->orr_nnewrdn = nrdn;
1530 op->orr_deleteoldrdn = deleteOldRdn;
1531 op->orr_modlist = NULL;
1532 if ( slap_modrdn2mods( op, &rs ) ) {
1536 /* Append modlist for operational attrs */
1540 for ( m = op->orr_modlist; m->sml_next; m = m->sml_next )
1542 m->sml_next = modlist;
1545 rc = op->o_bd->be_modrdn( op, &rs );
1546 slap_mods_free( op->orr_modlist, 1 );
1547 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1548 "syncrepl_message_to_op: %s be_modrdn %s (%d)\n",
1549 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1551 case LDAP_REQ_DELETE:
1552 rc = op->o_bd->be_delete( op, &rs );
1553 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1554 "syncrepl_message_to_op: %s be_delete %s (%d)\n",
1555 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1559 slap_graduate_commit_csn( op );
1560 op->o_bd = si->si_be;
1561 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
1562 BER_BVZERO( &op->o_csn );
1564 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1566 if ( !BER_BVISNULL( &rdn ) ) {
1567 if ( !BER_BVISNULL( &nsup ) ) {
1568 ch_free( nsup.bv_val );
1570 if ( !BER_BVISNULL( &psup ) ) {
1571 ch_free( psup.bv_val );
1573 if ( !BER_BVISNULL( &nrdn ) ) {
1574 ch_free( nrdn.bv_val );
1576 if ( !BER_BVISNULL( &prdn ) ) {
1577 ch_free( prdn.bv_val );
1581 ch_free( op->o_req_ndn.bv_val );
1582 ch_free( op->o_req_dn.bv_val );
1589 syncrepl_message_to_entry(
1593 Modifications **modlist,
1599 BerElement *ber = NULL;
1602 Modifications **modtail = modlist;
1605 char txtbuf[SLAP_TEXT_BUFLEN];
1606 size_t textlen = sizeof txtbuf;
1608 struct berval bdn = BER_BVNULL, dn, ndn;
1613 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1614 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s "
1615 "Message type should be entry (%d)",
1616 si->si_ridtxt, ldap_msgtype( msg ), 0 );
1620 op->o_tag = LDAP_REQ_ADD;
1622 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1623 if ( rc != LDAP_SUCCESS ) {
1624 Debug( LDAP_DEBUG_ANY,
1625 "syncrepl_message_to_entry: %s dn get failed (%d)",
1626 si->si_ridtxt, rc, 0 );
1630 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1631 /* NOTE: this could be done even before decoding the DN,
1632 * although encoding errors wouldn't be detected */
1637 if ( entry == NULL ) {
1641 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1642 ber_dupbv( &op->o_req_dn, &dn );
1643 ber_dupbv( &op->o_req_ndn, &ndn );
1644 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1645 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1647 is_ctx = dn_match( &op->o_req_ndn, &op->o_bd->be_nsuffix[0] );
1650 e->e_name = op->o_req_dn;
1651 e->e_nname = op->o_req_ndn;
1653 while ( ber_remaining( ber ) ) {
1654 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1655 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1660 /* Drop all updates to the contextCSN of the context entry
1663 if ( is_ctx && !strcasecmp( tmp.sml_type.bv_val,
1664 slap_schema.si_ad_contextCSN->ad_cname.bv_val )) {
1665 ber_bvarray_free( tmp.sml_values );
1669 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1671 mod->sml_op = LDAP_MOD_REPLACE;
1673 mod->sml_next = NULL;
1674 mod->sml_desc = NULL;
1675 mod->sml_type = tmp.sml_type;
1676 mod->sml_values = tmp.sml_values;
1677 mod->sml_nvalues = NULL;
1680 modtail = &mod->sml_next;
1683 if ( *modlist == NULL ) {
1684 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s no attributes\n",
1685 si->si_ridtxt, 0, 0 );
1690 rc = slap_mods_check( op, *modlist, &text, txtbuf, textlen, NULL );
1692 if ( rc != LDAP_SUCCESS ) {
1693 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods check (%s)\n",
1694 si->si_ridtxt, text, 0 );
1698 /* Strip out dynamically generated attrs */
1699 for ( modtail = modlist; *modtail ; ) {
1701 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1702 *modtail = mod->sml_next;
1703 slap_mod_free( &mod->sml_mod, 0 );
1706 modtail = &mod->sml_next;
1710 /* Strip out attrs in exattrs list */
1711 for ( modtail = modlist; *modtail ; ) {
1713 if ( ldap_charray_inlist( si->si_exattrs,
1714 mod->sml_desc->ad_type->sat_cname.bv_val ) )
1716 *modtail = mod->sml_next;
1717 slap_mod_free( &mod->sml_mod, 0 );
1720 modtail = &mod->sml_next;
1724 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1725 if( rc != LDAP_SUCCESS ) {
1726 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods2entry (%s)\n",
1727 si->si_ridtxt, text, 0 );
1732 if ( rc != LDAP_SUCCESS ) {
1744 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1746 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1747 * entry if a previous refresh was interrupted before sending us a new
1748 * context state. We try to compare the new entry to the existing entry
1749 * and ignore the new entry if they are the same.
1751 * Also, we may get an update where the entryDN has changed, due to
1752 * a ModDn on the provider. We detect this as well, so we can issue
1753 * the corresponding operation locally.
1755 * In the case of a modify, we get a list of all the attributes
1756 * in the original entry. Rather than deleting the entry and re-adding it,
1757 * we issue a Modify request that deletes all the attributes and adds all
1758 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1761 * We otherwise distinguish ModDN from Modify; in the case of
1762 * a ModDN we just use the CSN, modifyTimestamp and modifiersName
1763 * operational attributes from the entry, and do a regular ModDN.
1765 typedef struct dninfo {
1769 int renamed; /* Was an existing entry renamed? */
1770 int delOldRDN; /* Was old RDN deleted? */
1771 Modifications **modlist; /* the modlist we received */
1772 Modifications *mods; /* the modlist we compared */
1780 Modifications** modlist,
1782 struct berval* syncUUID )
1784 Backend *be = op->o_bd;
1785 slap_callback cb = { NULL, NULL, NULL, NULL };
1786 struct berval *syncuuid_bv = NULL;
1787 struct berval syncUUID_strrep = BER_BVNULL;
1788 struct berval uuid_bv = BER_BVNULL;
1790 SlapReply rs_search = {REP_RESULT};
1791 SlapReply rs_delete = {REP_RESULT};
1792 SlapReply rs_add = {REP_RESULT};
1793 SlapReply rs_modify = {REP_RESULT};
1795 #ifdef LDAP_COMP_MATCH
1796 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
1798 AttributeAssertion ava = { NULL, BER_BVNULL };
1800 int rc = LDAP_SUCCESS;
1802 struct berval pdn = BER_BVNULL;
1806 switch( syncstate ) {
1807 case LDAP_SYNC_PRESENT:
1808 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s %s\n",
1810 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1813 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s %s\n",
1815 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1817 case LDAP_SYNC_DELETE:
1818 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s %s\n",
1820 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1822 case LDAP_SYNC_MODIFY:
1823 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s %s\n",
1825 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1828 Debug( LDAP_DEBUG_ANY, "syncrepl_entry: %s %s\n",
1830 "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1833 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ) ) {
1834 if ( !si->si_refreshPresent ) {
1835 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1836 if ( avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1837 syncuuid_cmp, avl_dup_error ) )
1839 ber_bvfree( syncuuid_bv );
1845 if ( syncstate == LDAP_SYNC_PRESENT ) {
1847 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1848 if ( entry == NULL ) {
1853 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1854 if ( syncstate != LDAP_SYNC_DELETE ) {
1855 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1858 /* add if missing */
1859 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1860 &syncUUID_strrep, syncUUID );
1862 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1863 /* replace only if necessary */
1864 if ( a->a_nvals != a->a_vals ) {
1865 ber_memfree( a->a_nvals[0].bv_val );
1866 ber_dupbv( &a->a_nvals[0], syncUUID );
1868 ber_memfree( a->a_vals[0].bv_val );
1869 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
1873 f.f_choice = LDAP_FILTER_EQUALITY;
1875 ava.aa_desc = slap_schema.si_ad_entryUUID;
1876 ava.aa_value = *syncUUID;
1878 if ( syncuuid_bv ) {
1879 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s inserted UUID %s\n",
1880 si->si_ridtxt, syncUUID_strrep.bv_val, 0 );
1882 op->ors_filter = &f;
1884 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID_strrep.bv_len;
1885 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1886 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1887 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
1888 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
1889 syncUUID_strrep.bv_val, syncUUID_strrep.bv_len );
1890 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
1891 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1893 op->o_tag = LDAP_REQ_SEARCH;
1894 op->ors_scope = LDAP_SCOPE_SUBTREE;
1895 op->ors_deref = LDAP_DEREF_NEVER;
1897 /* get the entry for this UUID */
1898 op->o_req_dn = si->si_base;
1899 op->o_req_ndn = si->si_base;
1901 op->o_time = slap_get_time();
1902 op->ors_tlimit = SLAP_NO_LIMIT;
1905 op->ors_attrs = slap_anlist_all_attributes;
1906 op->ors_attrsonly = 0;
1908 /* set callback function */
1909 op->o_callback = &cb;
1910 cb.sc_response = dn_callback;
1911 cb.sc_private = &dni;
1912 dni.new_entry = entry;
1913 dni.modlist = modlist;
1915 if ( limits_check( op, &rs_search ) == 0 ) {
1916 rc = be->be_search( op, &rs_search );
1917 Debug( LDAP_DEBUG_SYNC,
1918 "syncrepl_entry: %s be_search (%d)\n",
1919 si->si_ridtxt, rc, 0 );
1922 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1923 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1926 cb.sc_response = null_callback;
1929 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
1930 Debug( LDAP_DEBUG_SYNC,
1931 "syncrepl_entry: %s %s\n",
1932 si->si_ridtxt, entry->e_name.bv_val, 0 );
1934 Debug( LDAP_DEBUG_SYNC,
1935 "syncrepl_entry: %s %s\n",
1936 si->si_ridtxt, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
1939 slap_op_time( &op->o_time, &op->o_tincr );
1940 switch ( syncstate ) {
1942 case LDAP_SYNC_MODIFY:
1944 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
1946 /* FIXME: op->o_csn is assumed to be
1947 * on the thread's slab; this needs
1948 * to be cleared ASAP.
1949 * What happens if already present?
1951 assert( BER_BVISNULL( &op->o_csn ) );
1952 op->o_csn = a->a_vals[0];
1956 if ( BER_BVISNULL( &dni.dn ) ) {
1958 op->o_req_dn = entry->e_name;
1959 op->o_req_ndn = entry->e_nname;
1960 op->o_tag = LDAP_REQ_ADD;
1962 op->o_bd = si->si_wbe;
1964 rc = op->o_bd->be_add( op, &rs_add );
1965 Debug( LDAP_DEBUG_SYNC,
1966 "syncrepl_entry: %s be_add (%d)\n",
1967 si->si_ridtxt, rc, 0 );
1968 switch ( rs_add.sr_err ) {
1970 if ( op->ora_e == entry ) {
1971 be_entry_release_w( op, entry );
1977 /* we assume that LDAP_NO_SUCH_OBJECT is returned
1978 * only if the suffix entry is not present */
1979 case LDAP_NO_SUCH_OBJECT:
1980 rc = syncrepl_add_glue( op, entry );
1984 /* if an entry was added via syncrepl_add_glue(),
1985 * it likely has no entryUUID, so the previous
1986 * be_search() doesn't find it. In this case,
1987 * give syncrepl a chance to modify it. Also
1988 * allow for entries that were recreated with the
1989 * same DN but a different entryUUID.
1991 case LDAP_ALREADY_EXISTS:
1993 Operation op2 = *op;
1994 SlapReply rs2 = { 0 };
1995 slap_callback cb2 = { 0 };
1998 op2.o_tag = LDAP_REQ_SEARCH;
1999 op2.o_req_dn = entry->e_name;
2000 op2.o_req_ndn = entry->e_nname;
2001 op2.ors_scope = LDAP_SCOPE_BASE;
2002 op2.ors_deref = LDAP_DEREF_NEVER;
2003 op2.ors_attrs = slap_anlist_all_attributes;
2004 op2.ors_attrsonly = 0;
2005 op2.ors_limit = NULL;
2007 op2.ors_tlimit = SLAP_NO_LIMIT;
2009 f.f_choice = LDAP_FILTER_PRESENT;
2010 f.f_desc = slap_schema.si_ad_objectClass;
2011 op2.ors_filter = &f;
2012 op2.ors_filterstr = generic_filterstr;
2014 op2.o_callback = &cb2;
2015 cb2.sc_response = dn_callback;
2016 cb2.sc_private = &dni;
2018 rc = be->be_search( &op2, &rs2 );
2019 if ( rc ) goto done;
2022 slap_op_time( &op->o_time, &op->o_tincr );
2028 Debug( LDAP_DEBUG_ANY,
2029 "syncrepl_entry: %s be_add failed (%d)\n",
2030 si->si_ridtxt, rs_add.sr_err, 0 );
2037 op->o_req_dn = dni.dn;
2038 op->o_req_ndn = dni.ndn;
2039 if ( dni.renamed ) {
2040 struct berval noldp, newp, nnewp;
2042 op->o_tag = LDAP_REQ_MODRDN;
2043 dnRdn( &entry->e_name, &op->orr_newrdn );
2044 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
2046 dnParent( &dni.ndn, &noldp );
2047 dnParent( &entry->e_nname, &nnewp );
2048 if ( !dn_match( &noldp, &nnewp ) ) {
2049 dnParent( &entry->e_name, &newp );
2050 op->orr_newSup = &newp;
2051 op->orr_nnewSup = &nnewp;
2053 op->orr_newSup = NULL;
2054 op->orr_nnewSup = NULL;
2056 op->orr_deleteoldrdn = dni.delOldRDN;
2057 op->orr_modlist = NULL;
2058 if ( ( rc = slap_modrdn2mods( op, &rs_modify ) ) ) {
2062 /* RDNs must be NUL-terminated for back-ldap */
2063 noldp = op->orr_newrdn;
2064 ber_dupbv_x( &op->orr_newrdn, &noldp, op->o_tmpmemctx );
2065 noldp = op->orr_nnewrdn;
2066 ber_dupbv_x( &op->orr_nnewrdn, &noldp, op->o_tmpmemctx );
2068 /* Setup opattrs too */
2070 AttributeDescription *opattrs[] = {
2071 slap_schema.si_ad_entryCSN,
2072 slap_schema.si_ad_modifiersName,
2073 slap_schema.si_ad_modifyTimestamp,
2076 Modifications *mod, **modtail, **ml;
2079 for ( mod = op->orr_modlist;
2081 mod = mod->sml_next )
2083 modtail = &mod->sml_next;
2085 /* pull mod off incoming modlist, append to orr_modlist */
2086 for ( i = 0; opattrs[i]; i++ ) {
2087 for ( ml = modlist; *ml; ml = &(*ml)->sml_next )
2089 if ( (*ml)->sml_desc == opattrs[i] ) {
2091 *ml = mod->sml_next;
2092 mod->sml_next = NULL;
2094 modtail = &mod->sml_next;
2100 op->o_bd = si->si_wbe;
2101 rc = op->o_bd->be_modrdn( op, &rs_modify );
2102 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
2103 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
2105 slap_mods_free( op->orr_modlist, 1 );
2106 Debug( LDAP_DEBUG_SYNC,
2107 "syncrepl_entry: %s be_modrdn (%d)\n",
2108 si->si_ridtxt, rc, 0 );
2113 op->o_tag = LDAP_REQ_MODIFY;
2114 op->orm_modlist = dni.mods;
2115 op->orm_no_opattrs = 1;
2116 op->o_bd = si->si_wbe;
2118 rc = op->o_bd->be_modify( op, &rs_modify );
2119 slap_mods_free( op->orm_modlist, 1 );
2120 Debug( LDAP_DEBUG_SYNC,
2121 "syncrepl_entry: %s be_modify (%d)\n",
2122 si->si_ridtxt, rc, 0 );
2123 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
2124 Debug( LDAP_DEBUG_ANY,
2125 "syncrepl_entry: %s be_modify failed (%d)\n",
2126 si->si_ridtxt, rs_modify.sr_err, 0 );
2130 Debug( LDAP_DEBUG_SYNC,
2131 "syncrepl_entry: %s entry unchanged, ignored (%s)\n",
2132 si->si_ridtxt, op->o_req_dn.bv_val, 0 );
2135 case LDAP_SYNC_DELETE :
2136 if ( !BER_BVISNULL( &dni.dn ) ) {
2137 op->o_req_dn = dni.dn;
2138 op->o_req_ndn = dni.ndn;
2139 op->o_tag = LDAP_REQ_DELETE;
2140 op->o_bd = si->si_wbe;
2141 rc = op->o_bd->be_delete( op, &rs_delete );
2142 Debug( LDAP_DEBUG_SYNC,
2143 "syncrepl_entry: %s be_delete (%d)\n",
2144 si->si_ridtxt, rc, 0 );
2146 while ( rs_delete.sr_err == LDAP_SUCCESS
2147 && op->o_delete_glue_parent ) {
2148 op->o_delete_glue_parent = 0;
2149 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
2150 slap_callback cb = { NULL };
2151 cb.sc_response = slap_null_cb;
2152 dnParent( &op->o_req_ndn, &pdn );
2154 op->o_req_ndn = pdn;
2155 op->o_callback = &cb;
2156 op->o_bd->be_delete( op, &rs_delete );
2166 Debug( LDAP_DEBUG_ANY,
2167 "syncrepl_entry: %s unknown syncstate\n", si->si_ridtxt, 0, 0 );
2172 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
2173 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
2174 BER_BVZERO( &syncUUID_strrep );
2176 if ( !BER_BVISNULL( &dni.ndn ) ) {
2177 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
2179 if ( !BER_BVISNULL( &dni.dn ) ) {
2180 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
2183 entry_free( entry );
2184 BER_BVZERO( &op->o_csn );
2188 static struct berval gcbva[] = {
2194 #define NP_DELETE_ONE 2
2197 syncrepl_del_nonpresent(
2201 struct berval *cookiecsn )
2203 Backend* be = op->o_bd;
2204 slap_callback cb = { NULL };
2205 SlapReply rs_search = {REP_RESULT};
2206 SlapReply rs_delete = {REP_RESULT};
2207 SlapReply rs_modify = {REP_RESULT};
2208 struct nonpresent_entry *np_list, *np_prev;
2210 AttributeName an[2];
2212 struct berval pdn = BER_BVNULL;
2215 op->o_req_dn = si->si_base;
2216 op->o_req_ndn = si->si_base;
2218 cb.sc_response = nonpresent_callback;
2221 op->o_callback = &cb;
2222 op->o_tag = LDAP_REQ_SEARCH;
2223 op->ors_scope = si->si_scope;
2224 op->ors_deref = LDAP_DEREF_NEVER;
2225 op->o_time = slap_get_time();
2226 op->ors_tlimit = SLAP_NO_LIMIT;
2231 #ifdef LDAP_COMP_MATCH
2232 AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
2234 AttributeAssertion eq = { NULL, BER_BVNULL };
2238 op->ors_attrsonly = 1;
2239 op->ors_attrs = slap_anlist_no_attrs;
2240 op->ors_limit = NULL;
2241 op->ors_filter = &uf;
2244 uf.f_av_desc = slap_schema.si_ad_entryUUID;
2246 uf.f_choice = LDAP_FILTER_EQUALITY;
2247 si->si_refreshDelete |= NP_DELETE_ONE;
2249 for (i=0; uuids[i].bv_val; i++) {
2251 slap_uuidstr_from_normalized( &uf.f_av_value, &uuids[i],
2253 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2254 op->o_tmpfree( uf.f_av_value.bv_val, op->o_tmpmemctx );
2255 uf.f_av_value = uuids[i];
2256 rc = be->be_search( op, &rs_search );
2257 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2259 si->si_refreshDelete ^= NP_DELETE_ONE;
2261 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2262 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2263 an[0].an_desc = slap_schema.si_ad_entryUUID;
2265 op->ors_slimit = SLAP_NO_LIMIT;
2266 op->ors_attrsonly = 0;
2267 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
2268 op->ors_filterstr = si->si_filterstr;
2269 op->o_nocaching = 1;
2271 if ( limits_check( op, &rs_search ) == 0 ) {
2272 rc = be->be_search( op, &rs_search );
2274 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
2277 op->o_nocaching = 0;
2279 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2281 if ( cookiecsn && !BER_BVISNULL( cookiecsn ) ) {
2284 csn = si->si_syncCookie.ctxcsn[0];
2287 op->o_bd = si->si_wbe;
2288 slap_queue_csn( op, &csn );
2290 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2291 while ( np_list != NULL ) {
2292 LDAP_LIST_REMOVE( np_list, npe_link );
2294 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2295 op->o_tag = LDAP_REQ_DELETE;
2296 op->o_callback = &cb;
2297 cb.sc_response = null_callback;
2299 op->o_req_dn = *np_prev->npe_name;
2300 op->o_req_ndn = *np_prev->npe_nname;
2301 rc = op->o_bd->be_delete( op, &rs_delete );
2302 Debug( LDAP_DEBUG_SYNC,
2303 "syncrepl_del_nonpresent: %s be_delete %s (%d)\n",
2304 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2306 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2307 Modifications mod1, mod2;
2308 mod1.sml_op = LDAP_MOD_REPLACE;
2310 mod1.sml_desc = slap_schema.si_ad_objectClass;
2311 mod1.sml_type = mod1.sml_desc->ad_cname;
2312 mod1.sml_values = &gcbva[0];
2313 mod1.sml_nvalues = NULL;
2314 mod1.sml_next = &mod2;
2316 mod2.sml_op = LDAP_MOD_REPLACE;
2318 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2319 mod2.sml_type = mod2.sml_desc->ad_cname;
2320 mod2.sml_values = &gcbva[1];
2321 mod2.sml_nvalues = NULL;
2322 mod2.sml_next = NULL;
2324 op->o_tag = LDAP_REQ_MODIFY;
2325 op->orm_modlist = &mod1;
2327 rc = op->o_bd->be_modify( op, &rs_modify );
2328 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
2331 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2332 op->o_delete_glue_parent ) {
2333 op->o_delete_glue_parent = 0;
2334 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
2335 slap_callback cb = { NULL };
2336 cb.sc_response = slap_null_cb;
2337 dnParent( &op->o_req_ndn, &pdn );
2339 op->o_req_ndn = pdn;
2340 op->o_callback = &cb;
2341 /* give it a root privil ? */
2342 op->o_bd->be_delete( op, &rs_delete );
2348 op->o_delete_glue_parent = 0;
2350 ber_bvfree( np_prev->npe_name );
2351 ber_bvfree( np_prev->npe_nname );
2355 slap_graduate_commit_csn( op );
2358 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2359 BER_BVZERO( &op->o_csn );
2370 Backend *be = op->o_bd;
2371 slap_callback cb = { NULL };
2376 struct berval dn = BER_BVNULL;
2377 struct berval ndn = BER_BVNULL;
2379 SlapReply rs_add = {REP_RESULT};
2380 struct berval ptr, nptr;
2383 op->o_tag = LDAP_REQ_ADD;
2384 op->o_callback = &cb;
2385 cb.sc_response = null_callback;
2386 cb.sc_private = NULL;
2391 /* count RDNs in suffix */
2392 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2393 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
2395 ptr.bv_len -= comma - ptr.bv_val;
2405 /* Start with BE suffix */
2407 for ( i = 0; i < suffrdns; i++ ) {
2408 comma = ber_bvrchr( &ptr, ',' );
2409 if ( comma != NULL ) {
2410 ptr.bv_len = comma - ptr.bv_val;
2417 if ( !BER_BVISEMPTY( &ptr ) ) {
2418 dn.bv_len -= ptr.bv_len + 1;
2419 dn.bv_val += ptr.bv_len + 1;
2422 /* the normalizedDNs are always the same length, no counting
2426 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2427 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2428 ndn.bv_len = be->be_nsuffix[0].bv_len;
2430 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
2436 while ( ndn.bv_val > e->e_nname.bv_val ) {
2437 glue = entry_alloc();
2438 ber_dupbv( &glue->e_name, &dn );
2439 ber_dupbv( &glue->e_nname, &ndn );
2441 a = attr_alloc( slap_schema.si_ad_objectClass );
2443 a->a_vals = ch_calloc( 3, sizeof( struct berval ) );
2444 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2445 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2446 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2448 a->a_nvals = a->a_vals;
2450 a->a_next = glue->e_attrs;
2453 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
2455 a->a_vals = ch_calloc( 2, sizeof( struct berval ) );
2456 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2457 ber_dupbv( &a->a_vals[1], &gcbva[2] );
2459 a->a_nvals = a->a_vals;
2461 a->a_next = glue->e_attrs;
2464 op->o_req_dn = glue->e_name;
2465 op->o_req_ndn = glue->e_nname;
2467 rc = be->be_add ( op, &rs_add );
2468 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2469 if ( op->ora_e == glue )
2470 be_entry_release_w( op, glue );
2472 /* incl. ALREADY EXIST */
2474 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
2480 /* Move to next child */
2481 comma = ber_bvrchr( &ptr, ',' );
2482 if ( comma == NULL ) {
2485 ptr.bv_len = comma - ptr.bv_val;
2487 dn.bv_val = ++comma;
2488 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
2490 comma = ber_bvrchr( &nptr, ',' );
2491 assert( comma != NULL );
2492 nptr.bv_len = comma - nptr.bv_val;
2494 ndn.bv_val = ++comma;
2495 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
2498 op->o_req_dn = e->e_name;
2499 op->o_req_ndn = e->e_nname;
2501 rc = be->be_add ( op, &rs_add );
2502 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2503 if ( op->ora_e == e )
2504 be_entry_release_w( op, e );
2513 syncrepl_updateCookie(
2517 struct sync_cookie *syncCookie )
2519 Backend *be = op->o_bd;
2520 Modifications mod[2];
2521 struct berval first = BER_BVNULL;
2525 slap_callback cb = { NULL };
2526 SlapReply rs_modify = {REP_RESULT};
2528 mod[0].sml_op = LDAP_MOD_DELETE;
2529 mod[0].sml_desc = slap_schema.si_ad_contextCSN;
2530 mod[0].sml_type = mod[0].sml_desc->ad_cname;
2531 mod[0].sml_values = NULL;
2532 mod[0].sml_nvalues = NULL;
2533 mod[0].sml_next = &mod[1];
2535 mod[1].sml_op = LDAP_MOD_ADD;
2536 mod[1].sml_desc = slap_schema.si_ad_contextCSN;
2537 mod[1].sml_type = mod[0].sml_desc->ad_cname;
2538 mod[1].sml_values = NULL;
2539 mod[1].sml_nvalues = NULL;
2540 mod[1].sml_next = NULL;
2542 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
2544 for ( i=0; i<syncCookie->numcsns; i++ ) {
2545 for ( j=0; j<si->si_cookieState->cs_num; j++ ) {
2546 if ( syncCookie->sids[i] != si->si_cookieState->cs_sids[j] )
2548 if ( ber_bvcmp( &syncCookie->ctxcsn[i],
2549 &si->si_cookieState->cs_vals[j] ) > 0 ) {
2550 ber_bvarray_add_x( &mod[0].sml_values,
2551 &si->si_cookieState->cs_vals[j], op->o_tmpmemctx );
2552 ber_bvarray_add_x( &mod[1].sml_values,
2553 &syncCookie->ctxcsn[i], op->o_tmpmemctx );
2554 if ( BER_BVISNULL( &first ))
2555 first = syncCookie->ctxcsn[i];
2559 /* there was no match for this SID, it's a new CSN */
2560 if ( j == si->si_cookieState->cs_num ) {
2561 ber_bvarray_add_x( &mod[1].sml_values,
2562 &syncCookie->ctxcsn[i], op->o_tmpmemctx );
2563 if ( BER_BVISNULL( &first ))
2564 first = syncCookie->ctxcsn[i];
2567 op->o_bd = si->si_wbe;
2568 slap_queue_csn( op, &first );
2570 op->o_tag = LDAP_REQ_MODIFY;
2572 cb.sc_response = null_callback;
2575 op->o_callback = &cb;
2576 op->o_req_dn = op->o_bd->be_suffix[0];
2577 op->o_req_ndn = op->o_bd->be_nsuffix[0];
2579 /* update contextCSN */
2580 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
2582 if ( mod[0].sml_values )
2583 op->orm_modlist = mod;
2585 op->orm_modlist = &mod[1];
2587 op->orm_no_opattrs = 1;
2588 rc = op->o_bd->be_modify( op, &rs_modify );
2591 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
2592 slap_sync_cookie_free( &si->si_syncCookie, 0 );
2593 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
2594 /* If we replaced any old values */
2595 if ( mod[0].sml_values ) {
2596 for ( i=0; !BER_BVISNULL( &mod[0].sml_values[i] ); i++ ) {
2597 for ( j=0; j<si->si_cookieState->cs_num; j++ ) {
2598 if ( mod[0].sml_values[i].bv_val !=
2599 si->si_cookieState->cs_vals[j].bv_val )
2601 ber_bvreplace( &si->si_cookieState->cs_vals[j],
2602 &mod[1].sml_values[i] );
2607 /* Else we just added */
2608 si->si_cookieState->cs_num += syncCookie->numcsns;
2609 value_add( &si->si_cookieState->cs_vals, syncCookie->ctxcsn );
2610 free( si->si_cookieState->cs_sids );
2611 si->si_cookieState->cs_sids = slap_parse_csn_sids(
2612 si->si_cookieState->cs_vals, si->si_cookieState->cs_num, NULL );
2615 si->si_cookieState->cs_age++;
2616 si->si_cookieAge = si->si_cookieState->cs_age;
2618 Debug( LDAP_DEBUG_ANY,
2619 "syncrepl_updateCookie: %s be_modify failed (%d)\n",
2620 si->si_ridtxt, rs_modify.sr_err, 0 );
2622 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
2624 slap_graduate_commit_csn( op );
2626 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2627 BER_BVZERO( &op->o_csn );
2628 if ( mod[1].sml_next ) slap_mods_free( mod[1].sml_next, 1 );
2629 op->o_tmpfree( mod[1].sml_values, op->o_tmpmemctx );
2630 op->o_tmpfree( mod[0].sml_values, op->o_tmpmemctx );
2636 attr_cmp( Operation *op, Attribute *old, Attribute *new,
2637 Modifications ***mret, Modifications ***mcur )
2640 Modifications *mod, **modtail;
2645 int n, o, d, a, *adds, *dels;
2646 /* count old and new */
2647 for ( o=0; old->a_vals[o].bv_val; o++ ) ;
2648 for ( n=0; new->a_vals[n].bv_val; n++ ) ;
2650 adds = op->o_tmpalloc( sizeof(int) * n, op->o_tmpmemctx );
2651 dels = op->o_tmpalloc( sizeof(int) * o, op->o_tmpmemctx );
2657 while ( i < o && j < n ) {
2659 if ( bvmatch( &old->a_vals[i], &new->a_vals[j] ) ) {
2664 for ( k = j + 1; k<n; k++ ) {
2665 if ( bvmatch( &old->a_vals[i], &new->a_vals[k] ) ) {
2669 /* an old value was deleted */
2674 for ( k = i + 1; k < o; k++ ) {
2675 if ( bvmatch( &old->a_vals[k], &new->a_vals[j] ) ) {
2688 /* all old values were deleted, just use the replace op */
2692 /* delete some values */
2693 mod = ch_malloc( sizeof( Modifications ) );
2694 mod->sml_op = LDAP_MOD_DELETE;
2696 mod->sml_desc = old->a_desc;
2697 mod->sml_type = mod->sml_desc->ad_cname;
2698 mod->sml_values = ch_malloc( ( d + 1 ) * sizeof(struct berval) );
2699 if ( old->a_vals != old->a_nvals ) {
2700 mod->sml_nvalues = ch_malloc( ( d + 1 ) * sizeof(struct berval) );
2702 mod->sml_nvalues = NULL;
2704 for ( i = 0; i < d; i++ ) {
2705 ber_dupbv( &mod->sml_values[i], &old->a_vals[dels[i]] );
2706 if ( mod->sml_nvalues ) {
2707 ber_dupbv( &mod->sml_nvalues[i], &old->a_nvals[dels[i]] );
2710 BER_BVZERO( &mod->sml_values[i] );
2711 if ( mod->sml_nvalues ) {
2712 BER_BVZERO( &mod->sml_nvalues[i] );
2715 modtail = &mod->sml_next;
2718 op->o_tmpfree( dels, op->o_tmpmemctx );
2719 /* some values were added */
2721 mod = ch_malloc( sizeof( Modifications ) );
2722 mod->sml_op = LDAP_MOD_ADD;
2724 mod->sml_desc = old->a_desc;
2725 mod->sml_type = mod->sml_desc->ad_cname;
2726 mod->sml_values = ch_malloc( ( a + 1 ) * sizeof(struct berval) );
2727 if ( old->a_vals != old->a_nvals ) {
2728 mod->sml_nvalues = ch_malloc( ( a + 1 ) * sizeof(struct berval) );
2730 mod->sml_nvalues = NULL;
2732 for ( i = 0; i < a; i++ ) {
2733 ber_dupbv( &mod->sml_values[i], &new->a_vals[adds[i]] );
2734 if ( mod->sml_nvalues ) {
2735 ber_dupbv( &mod->sml_nvalues[i], &new->a_nvals[adds[i]] );
2738 BER_BVZERO( &mod->sml_values[i] );
2739 if ( mod->sml_nvalues ) {
2740 BER_BVZERO( &mod->sml_nvalues[i] );
2743 modtail = &mod->sml_next;
2746 op->o_tmpfree( adds, op->o_tmpmemctx );
2748 /* new attr, just use the new mod */
2752 /* advance to next element */
2756 **mcur = mod->sml_next;
2758 modtail = &mod->sml_next;
2760 *mcur = &mod->sml_next;
2771 dninfo *dni = op->o_callback->sc_private;
2773 if ( rs->sr_type == REP_SEARCH ) {
2774 if ( !BER_BVISNULL( &dni->dn ) ) {
2775 Debug( LDAP_DEBUG_ANY,
2776 "dn_callback : consistency error - "
2777 "entryUUID is not unique\n", 0, 0, 0 );
2779 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
2780 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
2781 /* If there is a new entry, see if it differs from the old.
2782 * We compare the non-normalized values so that cosmetic changes
2783 * in the provider are always propagated.
2785 if ( dni->new_entry ) {
2786 Modifications **modtail, **ml;
2787 Attribute *old, *new;
2790 is_ctx = dn_match( &rs->sr_entry->e_nname,
2791 &op->o_bd->be_nsuffix[0] );
2793 /* Did the DN change?
2795 if ( !dn_match( &rs->sr_entry->e_name,
2796 &dni->new_entry->e_name ) )
2798 struct berval oldRDN, oldVal;
2799 AttributeDescription *ad = NULL;
2803 /* See if the oldRDN was deleted */
2804 dnRdn( &rs->sr_entry->e_nname, &oldRDN );
2805 oldVal.bv_val = strchr(oldRDN.bv_val, '=') + 1;
2806 oldVal.bv_len = oldRDN.bv_len - ( oldVal.bv_val -
2808 oldRDN.bv_len -= oldVal.bv_len + 2;
2809 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
2810 a = attr_find( dni->new_entry->e_attrs, ad );
2811 if ( !a || value_find_ex( ad,
2812 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH |
2813 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2814 SLAP_MR_VALUE_OF_SYNTAX, a->a_nvals,
2815 &oldVal, op->o_tmpmemctx ) != LDAP_SUCCESS )
2819 /* OK, this was just a modDN, we're done */
2820 return LDAP_SUCCESS;
2823 modtail = &dni->mods;
2826 /* Make sure new entry is actually newer than old entry */
2827 old = attr_find( rs->sr_entry->e_attrs,
2828 slap_schema.si_ad_entryCSN );
2829 new = attr_find( dni->new_entry->e_attrs,
2830 slap_schema.si_ad_entryCSN );
2831 if ( new && old && ber_bvcmp( &old->a_vals[0],
2832 &new->a_vals[0] ) >= 0 ) {
2833 Debug( LDAP_DEBUG_SYNC,
2834 "dn_callback : new entry is older than ours "
2835 "%s ours %s, new %s\n",
2836 rs->sr_entry->e_name.bv_val,
2837 old->a_vals[0].bv_val,
2838 new->a_vals[0].bv_val );
2839 return LDAP_SUCCESS;
2842 /* We assume that attributes are saved in the same order
2843 * in the remote and local databases. So if we walk through
2844 * the attributeDescriptions one by one they should match in
2845 * lock step. If not, look for an add or delete.
2847 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
2850 /* If we've seen this before, use its mod now */
2851 if ( new->a_flags & SLAP_ATTR_IXADD ) {
2852 attr_cmp( op, NULL, new, &modtail, &ml );
2856 /* Skip contextCSN */
2857 if ( is_ctx && old->a_desc ==
2858 slap_schema.si_ad_contextCSN ) {
2863 if ( old->a_desc != new->a_desc ) {
2867 /* If it's just been re-added later,
2868 * remember that we've seen it.
2870 tmp = attr_find( new, old->a_desc );
2872 tmp->a_flags |= SLAP_ATTR_IXADD;
2874 /* If it's a new attribute, pull it in.
2876 tmp = attr_find( old, new->a_desc );
2878 attr_cmp( op, NULL, new, &modtail, &ml );
2882 /* Delete old attr */
2883 mod = ch_malloc( sizeof( Modifications ) );
2884 mod->sml_op = LDAP_MOD_DELETE;
2886 mod->sml_desc = old->a_desc;
2887 mod->sml_type = mod->sml_desc->ad_cname;
2888 mod->sml_values = NULL;
2889 mod->sml_nvalues = NULL;
2891 modtail = &mod->sml_next;
2896 /* kludge - always update modifiersName so that it
2897 * stays co-located with the other mod opattrs. But only
2898 * if we know there are other valid mods.
2900 if ( old->a_desc == slap_schema.si_ad_modifiersName &&
2902 attr_cmp( op, NULL, new, &modtail, &ml );
2904 attr_cmp( op, old, new, &modtail, &ml );
2912 } else if ( rs->sr_type == REP_RESULT ) {
2913 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
2914 Debug( LDAP_DEBUG_ANY,
2915 "dn_callback : consistency error - "
2916 "entryUUID is not unique\n", 0, 0, 0 );
2920 return LDAP_SUCCESS;
2924 nonpresent_callback(
2928 syncinfo_t *si = op->o_callback->sc_private;
2931 struct berval* present_uuid = NULL;
2932 struct nonpresent_entry *np_entry;
2934 if ( rs->sr_type == REP_RESULT ) {
2935 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2936 si->si_presentlist = NULL;
2938 } else if ( rs->sr_type == REP_SEARCH ) {
2939 if ( !( si->si_refreshDelete & NP_DELETE_ONE ) ) {
2940 char buf[sizeof("rid=000 not")];
2942 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2945 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2949 if ( slap_debug & LDAP_DEBUG_SYNC ) {
2950 sprintf( buf, "%s %s", si->si_ridtxt,
2951 present_uuid ? "got" : "not" );
2954 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: %s UUID %s, dn %s\n",
2955 buf, a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val );
2957 if ( a == NULL ) return 0;
2960 if ( present_uuid == NULL ) {
2961 np_entry = (struct nonpresent_entry *)
2962 ch_calloc( 1, sizeof( struct nonpresent_entry ) );
2963 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2964 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2965 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2968 avl_delete( &si->si_presentlist,
2969 &a->a_nvals[0], syncuuid_cmp );
2970 ch_free( present_uuid->bv_val );
2971 ch_free( present_uuid );
2974 return LDAP_SUCCESS;
2982 if ( rs->sr_err != LDAP_SUCCESS &&
2983 rs->sr_err != LDAP_REFERRAL &&
2984 rs->sr_err != LDAP_ALREADY_EXISTS &&
2985 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2986 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2988 Debug( LDAP_DEBUG_ANY,
2989 "null_callback : error code 0x%x\n",
2992 return LDAP_SUCCESS;
2995 static struct berval *
2996 slap_uuidstr_from_normalized(
2997 struct berval* uuidstr,
2998 struct berval* normalized,
3002 unsigned char nibble;
3005 if ( normalized == NULL ) return NULL;
3006 if ( normalized->bv_len != 16 ) return NULL;
3011 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
3012 if ( new == NULL ) {
3019 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
3020 if ( new != uuidstr ) {
3021 slap_sl_free( new, ctx );
3026 for ( i = 0; i < 16; i++ ) {
3027 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
3028 new->bv_val[(i<<1)+d] = '-';
3032 nibble = (normalized->bv_val[i] >> 4) & 0xF;
3033 if ( nibble < 10 ) {
3034 new->bv_val[(i<<1)+d] = nibble + '0';
3036 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
3039 nibble = (normalized->bv_val[i]) & 0xF;
3040 if ( nibble < 10 ) {
3041 new->bv_val[(i<<1)+d+1] = nibble + '0';
3043 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
3047 new->bv_val[new->bv_len] = '\0';
3052 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
3054 const struct berval *uuid1 = v_uuid1;
3055 const struct berval *uuid2 = v_uuid2;
3056 int rc = uuid1->bv_len - uuid2->bv_len;
3057 if ( rc ) return rc;
3058 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
3062 avl_ber_bvfree( void *v_bv )
3064 struct berval *bv = (struct berval *)v_bv;
3066 if( v_bv == NULL ) return;
3067 if ( !BER_BVISNULL( bv ) ) {
3068 ch_free( bv->bv_val );
3070 ch_free( (char *) bv );
3074 syncinfo_free( syncinfo_t *sie, int free_all )
3076 syncinfo_t *si_next;
3078 if ( free_all && sie->si_cookieState ) {
3079 ch_free( sie->si_cookieState->cs_sids );
3080 ber_bvarray_free( sie->si_cookieState->cs_vals );
3081 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_mutex );
3082 ch_free( sie->si_cookieState );
3085 si_next = sie->si_next;
3088 if ( sie->si_conn_setup ) {
3090 ldap_get_option( sie->si_ld, LDAP_OPT_DESC, &s );
3091 connection_client_stop( s );
3092 sie->si_conn_setup = 0;
3094 ldap_unbind_ext( sie->si_ld, NULL, NULL );
3097 /* re-fetch it, in case it was already removed */
3098 sie->si_re = ldap_pvt_runqueue_find( &slapd_rq, do_syncrepl, sie );
3100 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, sie->si_re ) )
3101 ldap_pvt_runqueue_stoptask( &slapd_rq, sie->si_re );
3102 ldap_pvt_runqueue_remove( &slapd_rq, sie->si_re );
3105 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
3107 bindconf_free( &sie->si_bindconf );
3109 if ( sie->si_filterstr.bv_val ) {
3110 ch_free( sie->si_filterstr.bv_val );
3112 if ( sie->si_logfilterstr.bv_val ) {
3113 ch_free( sie->si_logfilterstr.bv_val );
3115 if ( sie->si_base.bv_val ) {
3116 ch_free( sie->si_base.bv_val );
3118 if ( sie->si_logbase.bv_val ) {
3119 ch_free( sie->si_logbase.bv_val );
3121 if ( sie->si_attrs ) {
3123 while ( sie->si_attrs[i] != NULL ) {
3124 ch_free( sie->si_attrs[i] );
3127 ch_free( sie->si_attrs );
3129 if ( sie->si_exattrs ) {
3131 while ( sie->si_exattrs[i] != NULL ) {
3132 ch_free( sie->si_exattrs[i] );
3135 ch_free( sie->si_exattrs );
3137 if ( sie->si_anlist ) {
3139 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
3140 ch_free( sie->si_anlist[i].an_name.bv_val );
3143 ch_free( sie->si_anlist );
3145 if ( sie->si_exanlist ) {
3147 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
3148 ch_free( sie->si_exanlist[i].an_name.bv_val );
3151 ch_free( sie->si_exanlist );
3153 if ( sie->si_retryinterval ) {
3154 ch_free( sie->si_retryinterval );
3156 if ( sie->si_retrynum ) {
3157 ch_free( sie->si_retrynum );
3159 if ( sie->si_retrynum_init ) {
3160 ch_free( sie->si_retrynum_init );
3162 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
3163 if ( sie->si_presentlist ) {
3164 avl_free( sie->si_presentlist, avl_ber_bvfree );
3166 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist ) ) {
3167 struct nonpresent_entry* npe;
3168 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
3169 LDAP_LIST_REMOVE( npe, npe_link );
3170 if ( npe->npe_name ) {
3171 if ( npe->npe_name->bv_val ) {
3172 ch_free( npe->npe_name->bv_val );
3174 ch_free( npe->npe_name );
3176 if ( npe->npe_nname ) {
3177 if ( npe->npe_nname->bv_val ) {
3178 ch_free( npe->npe_nname->bv_val );
3180 ch_free( npe->npe_nname );
3186 } while ( free_all && si_next );
3191 /* NOTE: used & documented in slapd.conf(5) */
3193 #define PROVIDERSTR "provider"
3194 #define SCHEMASTR "schemachecking"
3195 #define FILTERSTR "filter"
3196 #define SEARCHBASESTR "searchbase"
3197 #define SCOPESTR "scope"
3198 #define ATTRSONLYSTR "attrsonly"
3199 #define ATTRSSTR "attrs"
3200 #define TYPESTR "type"
3201 #define INTERVALSTR "interval"
3202 #define RETRYSTR "retry"
3203 #define SLIMITSTR "sizelimit"
3204 #define TLIMITSTR "timelimit"
3205 #define SYNCDATASTR "syncdata"
3206 #define LOGBASESTR "logbase"
3207 #define LOGFILTERSTR "logfilter"
3209 /* FIXME: undocumented */
3210 #define EXATTRSSTR "exattrs"
3211 #define MANAGEDSAITSTR "manageDSAit"
3214 #define GOT_ID 0x0001
3215 #define GOT_PROVIDER 0x0002
3216 #define GOT_BASE 0x0004
3219 #define GOT_ALL (GOT_ID|GOT_PROVIDER|GOT_BASE)
3225 { BER_BVC("base"), LDAP_SCOPE_BASE },
3226 { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
3227 { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL }, /* OpenLDAP extension */
3228 { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
3229 { BER_BVC("subord"), LDAP_SCOPE_SUBORDINATE },
3230 { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
3231 { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
3232 { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE }, /* OpenLDAP extension */
3236 static slap_verbmasks datamodes[] = {
3237 { BER_BVC("default"), SYNCDATA_DEFAULT },
3238 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
3239 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
3244 parse_syncrepl_line(
3252 for ( i = 1; i < c->argc; i++ ) {
3253 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
3254 STRLENOF( IDSTR "=" ) ) )
3257 /* '\0' string terminator accounts for '=' */
3258 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
3259 if ( lutil_atoi( &tmp, val ) != 0 ) {
3260 snprintf( c->msg, sizeof( c->msg ),
3261 "Error: parse_syncrepl_line: "
3262 "unable to parse syncrepl id \"%s\"", val );
3263 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3266 if ( tmp >= 1000 || tmp < 0 ) {
3267 snprintf( c->msg, sizeof( c->msg ),
3268 "Error: parse_syncrepl_line: "
3269 "syncrepl id %d is out of range [0..999]", tmp );
3270 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3274 sprintf( si->si_ridtxt, IDSTR "=%03d", si->si_rid );
3276 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
3277 STRLENOF( PROVIDERSTR "=" ) ) )
3279 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
3280 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
3281 gots |= GOT_PROVIDER;
3282 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
3283 STRLENOF( SCHEMASTR "=" ) ) )
3285 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
3286 if ( !strncasecmp( val, "on", STRLENOF( "on" ) ) ) {
3287 si->si_schemachecking = 1;
3288 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
3289 si->si_schemachecking = 0;
3291 si->si_schemachecking = 1;
3293 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
3294 STRLENOF( FILTERSTR "=" ) ) )
3296 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
3297 if ( si->si_filterstr.bv_val )
3298 ch_free( si->si_filterstr.bv_val );
3299 ber_str2bv( val, 0, 1, &si->si_filterstr );
3300 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
3301 STRLENOF( LOGFILTERSTR "=" ) ) )
3303 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
3304 if ( si->si_logfilterstr.bv_val )
3305 ch_free( si->si_logfilterstr.bv_val );
3306 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
3307 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
3308 STRLENOF( SEARCHBASESTR "=" ) ) )
3313 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
3314 if ( si->si_base.bv_val ) {
3315 ch_free( si->si_base.bv_val );
3317 ber_str2bv( val, 0, 0, &bv );
3318 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
3319 if ( rc != LDAP_SUCCESS ) {
3320 snprintf( c->msg, sizeof( c->msg ),
3321 "Invalid base DN \"%s\": %d (%s)",
3322 val, rc, ldap_err2string( rc ) );
3323 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3327 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
3328 STRLENOF( LOGBASESTR "=" ) ) )
3333 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
3334 if ( si->si_logbase.bv_val ) {
3335 ch_free( si->si_logbase.bv_val );
3337 ber_str2bv( val, 0, 0, &bv );
3338 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
3339 if ( rc != LDAP_SUCCESS ) {
3340 snprintf( c->msg, sizeof( c->msg ),
3341 "Invalid logbase DN \"%s\": %d (%s)",
3342 val, rc, ldap_err2string( rc ) );
3343 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3346 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
3347 STRLENOF( SCOPESTR "=" ) ) )
3350 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
3351 for ( j = 0; !BER_BVISNULL(&scopes[j].key); j++ ) {
3352 if (!strcasecmp( val, scopes[j].key.bv_val ) ) {
3353 si->si_scope = scopes[j].val;
3357 if ( BER_BVISNULL(&scopes[j].key) ) {
3358 snprintf( c->msg, sizeof( c->msg ),
3359 "Error: parse_syncrepl_line: "
3360 "unknown scope \"%s\"", val);
3361 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3364 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
3365 STRLENOF( ATTRSONLYSTR ) ) )
3367 si->si_attrsonly = 1;
3368 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
3369 STRLENOF( ATTRSSTR "=" ) ) )
3371 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
3372 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
3374 attr_fname = ch_strdup( val + STRLENOF(":include:") );
3375 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
3376 if ( si->si_anlist == NULL ) {
3377 ch_free( attr_fname );
3380 si->si_anfile = attr_fname;
3382 char *str, *s, *next;
3383 char delimstr[] = " ,\t";
3384 str = ch_strdup( val );
3385 for ( s = ldap_pvt_strtok( str, delimstr, &next );
3387 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
3389 if ( strlen(s) == 1 && *s == '*' ) {
3390 si->si_allattrs = 1;
3391 val[ s - str ] = delimstr[0];
3393 if ( strlen(s) == 1 && *s == '+' ) {
3394 si->si_allopattrs = 1;
3395 val [ s - str ] = delimstr[0];
3399 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
3400 if ( si->si_anlist == NULL ) {
3404 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
3405 STRLENOF( EXATTRSSTR "=" ) ) )
3407 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
3408 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
3410 attr_fname = ch_strdup( val + STRLENOF(":include:") );
3411 si->si_exanlist = file2anlist(
3412 si->si_exanlist, attr_fname, " ,\t" );
3413 if ( si->si_exanlist == NULL ) {
3414 ch_free( attr_fname );
3417 ch_free( attr_fname );
3419 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
3420 if ( si->si_exanlist == NULL ) {
3424 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
3425 STRLENOF( TYPESTR "=" ) ) )
3427 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
3428 if ( !strncasecmp( val, "refreshOnly",
3429 STRLENOF("refreshOnly") ) )
3431 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3432 } else if ( !strncasecmp( val, "refreshAndPersist",
3433 STRLENOF("refreshAndPersist") ) )
3435 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
3436 si->si_interval = 60;
3438 snprintf( c->msg, sizeof( c->msg ),
3439 "Error: parse_syncrepl_line: "
3440 "unknown sync type \"%s\"", val);
3441 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3444 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
3445 STRLENOF( INTERVALSTR "=" ) ) )
3447 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
3448 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
3449 si->si_interval = 0;
3450 } else if ( strchr( val, ':' ) != NULL ) {
3451 char *next, *ptr = val;
3452 unsigned dd, hh, mm, ss;
3454 /* NOTE: the test for ptr[ 0 ] == '-'
3455 * should go before the call to strtoul() */
3456 dd = strtoul( ptr, &next, 10 );
3457 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' ) {
3458 snprintf( c->msg, sizeof( c->msg ),
3459 "Error: parse_syncrepl_line: "
3460 "invalid interval \"%s\", unable to parse days", val );
3461 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3465 hh = strtoul( ptr, &next, 10 );
3466 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || hh > 24 ) {
3467 snprintf( c->msg, sizeof( c->msg ),
3468 "Error: parse_syncrepl_line: "
3469 "invalid interval \"%s\", unable to parse hours", val );
3470 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3474 mm = strtoul( ptr, &next, 10 );
3475 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || mm > 60 ) {
3476 snprintf( c->msg, sizeof( c->msg ),
3477 "Error: parse_syncrepl_line: "
3478 "invalid interval \"%s\", unable to parse minutes", val );
3479 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3483 ss = strtoul( ptr, &next, 10 );
3484 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != '\0' || ss > 60 ) {
3485 snprintf( c->msg, sizeof( c->msg ),
3486 "Error: parse_syncrepl_line: "
3487 "invalid interval \"%s\", unable to parse seconds", val );
3488 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3491 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
3495 if ( lutil_parse_time( val, &t ) != 0 ) {
3496 snprintf( c->msg, sizeof( c->msg ),
3497 "Error: parse_syncrepl_line: "
3498 "invalid interval \"%s\"", val );
3499 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3502 si->si_interval = (time_t)t;
3504 if ( si->si_interval < 0 ) {
3505 snprintf( c->msg, sizeof( c->msg ),
3506 "Error: parse_syncrepl_line: "
3507 "invalid interval \"%ld\"",
3508 (long) si->si_interval);
3509 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3512 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
3513 STRLENOF( RETRYSTR "=" ) ) )
3518 val = c->argv[ i ] + STRLENOF( RETRYSTR "=" );
3519 retry_list = (char **) ch_calloc( 1, sizeof( char * ) );
3520 retry_list[0] = NULL;
3522 slap_str2clist( &retry_list, val, " ,\t" );
3524 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
3527 snprintf( c->msg, sizeof( c->msg ),
3528 "Error: incomplete syncrepl retry list" );
3529 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3530 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3531 ch_free( retry_list[k] );
3533 ch_free( retry_list );
3536 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ) );
3537 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ) );
3538 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ) );
3539 for ( j = 0; j < n; j++ ) {
3541 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
3542 snprintf( c->msg, sizeof( c->msg ),
3543 "Error: invalid retry interval \"%s\" (#%d)",
3544 retry_list[j*2], j );
3545 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3546 /* do some cleanup */
3549 si->si_retryinterval[j] = (time_t)t;
3550 if ( *retry_list[j*2+1] == '+' ) {
3551 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
3552 si->si_retrynum[j] = RETRYNUM_FOREVER;
3556 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
3557 || si->si_retrynum_init[j] <= 0 )
3559 snprintf( c->msg, sizeof( c->msg ),
3560 "Error: invalid initial retry number \"%s\" (#%d)",
3561 retry_list[j*2+1], j );
3562 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3563 /* do some cleanup */
3566 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
3567 || si->si_retrynum[j] <= 0 )
3569 snprintf( c->msg, sizeof( c->msg ),
3570 "Error: invalid retry number \"%s\" (#%d)",
3571 retry_list[j*2+1], j );
3572 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3573 /* do some cleanup */
3578 si->si_retrynum_init[j] = RETRYNUM_TAIL;
3579 si->si_retrynum[j] = RETRYNUM_TAIL;
3580 si->si_retryinterval[j] = 0;
3582 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3583 ch_free( retry_list[k] );
3585 ch_free( retry_list );
3586 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
3587 STRLENOF( MANAGEDSAITSTR "=" ) ) )
3589 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
3590 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
3591 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
3593 snprintf( c->msg, sizeof( c->msg ),
3594 "invalid manageDSAit value \"%s\".\n",
3596 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3599 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
3600 STRLENOF( SLIMITSTR "=") ) )
3602 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
3603 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3606 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
3607 snprintf( c->msg, sizeof( c->msg ),
3608 "invalid size limit value \"%s\".\n",
3610 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3613 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
3614 STRLENOF( TLIMITSTR "=" ) ) )
3616 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
3617 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3620 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
3621 snprintf( c->msg, sizeof( c->msg ),
3622 "invalid time limit value \"%s\".\n",
3624 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3627 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
3628 STRLENOF( SYNCDATASTR "=" ) ) )
3630 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
3631 si->si_syncdata = verb_to_mask( val, datamodes );
3632 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
3633 snprintf( c->msg, sizeof( c->msg ),
3634 "Error: parse_syncrepl_line: "
3635 "unable to parse \"%s\"\n", c->argv[ i ] );
3636 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3641 if ( gots != GOT_ALL ) {
3642 snprintf( c->msg, sizeof( c->msg ),
3643 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
3644 gots & GOT_ID ? "" : " "IDSTR,
3645 gots & GOT_PROVIDER ? "" : " "PROVIDERSTR,
3646 gots & GOT_BASE ? "" : " "SEARCHBASESTR );
3647 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3661 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
3662 snprintf( c->msg, sizeof(c->msg), "database %s does not support "
3663 "operations required for syncrepl", c->be->be_type );
3664 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
3667 if ( BER_BVISEMPTY( &c->be->be_rootdn ) ) {
3668 strcpy( c->msg, "rootDN must be defined before syncrepl may be used" );
3669 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
3672 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
3675 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
3679 si->si_bindconf.sb_tls = SB_TLS_OFF;
3680 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
3681 si->si_schemachecking = 0;
3682 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
3683 &si->si_filterstr );
3684 si->si_base.bv_val = NULL;
3685 si->si_scope = LDAP_SCOPE_SUBTREE;
3686 si->si_attrsonly = 0;
3687 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
3688 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
3689 si->si_attrs = NULL;
3690 si->si_allattrs = 0;
3691 si->si_allopattrs = 0;
3692 si->si_exattrs = NULL;
3693 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3694 si->si_interval = 86400;
3695 si->si_retryinterval = NULL;
3696 si->si_retrynum_init = NULL;
3697 si->si_retrynum = NULL;
3698 si->si_manageDSAit = 0;
3701 si->si_conn_setup = 0;
3703 si->si_presentlist = NULL;
3704 LDAP_LIST_INIT( &si->si_nonpresentlist );
3705 ldap_pvt_thread_mutex_init( &si->si_mutex );
3707 rc = parse_syncrepl_line( c, si );
3710 /* Must be LDAPv3 because we need controls */
3711 switch ( si->si_bindconf.sb_version ) {
3713 /* not explicitly set */
3714 si->si_bindconf.sb_version = LDAP_VERSION3;
3717 /* explicitly set */
3720 Debug( LDAP_DEBUG_ANY,
3721 "version %d incompatible with syncrepl\n",
3722 si->si_bindconf.sb_version, 0, 0 );
3723 syncinfo_free( si, 0 );
3728 if ( slapMode & SLAP_SERVER_MODE ) {
3729 Listener **l = slapd_get_listeners();
3732 /* check if URL points to current server. If so, ignore
3733 * this configuration. We require an exact match. Just
3734 * in case they really want to do this, they can vary
3735 * the case of the URL to allow it.
3737 if ( l && !SLAP_DBHIDDEN( c->be ) ) {
3739 for ( i=0; l[i]; i++ ) {
3740 if ( bvmatch( &l[i]->sl_url, &si->si_bindconf.sb_uri ) ) {
3748 init_syncrepl( si );
3749 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq,
3750 si->si_interval, do_syncrepl, si, "do_syncrepl",
3753 rc = config_sync_shadow( c ) ? -1 : 0;
3761 /* Use main slapd defaults */
3762 bindconf_tls_defaults( &si->si_bindconf );
3765 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
3766 syncinfo_free( si, 0 );
3769 Debug( LDAP_DEBUG_CONFIG,
3770 "Config: ** successfully added syncrepl \"%s\"\n",
3771 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
3772 "(null)" : si->si_bindconf.sb_uri.bv_val, 0, 0 );
3773 if ( !si->si_schemachecking ) {
3774 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3776 if ( c->be->be_syncinfo ) {
3777 si->si_cookieState = c->be->be_syncinfo->si_cookieState;
3779 si->si_cookieState = ch_calloc( 1, sizeof( cookie_state ));
3780 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_mutex );
3782 si->si_next = c->be->be_syncinfo;
3783 c->be->be_syncinfo = si;
3789 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
3791 struct berval bc, uri;
3792 char buf[BUFSIZ*2], *ptr;
3795 #define WHATSLEFT ( sizeof( buf ) - ( ptr - buf ) )
3799 /* temporarily inhibit bindconf from printing URI */
3800 uri = si->si_bindconf.sb_uri;
3801 BER_BVZERO( &si->si_bindconf.sb_uri );
3802 si->si_bindconf.sb_version = 0;
3803 bindconf_unparse( &si->si_bindconf, &bc );
3804 si->si_bindconf.sb_uri = uri;
3805 si->si_bindconf.sb_version = LDAP_VERSION3;
3808 ptr += snprintf( ptr, WHATSLEFT, IDSTR "=%03d " PROVIDERSTR "=%s",
3809 si->si_rid, si->si_bindconf.sb_uri.bv_val );
3810 if ( ptr - buf >= sizeof( buf ) ) return;
3811 if ( !BER_BVISNULL( &bc ) ) {
3812 if ( WHATSLEFT <= bc.bv_len ) {
3816 ptr = lutil_strcopy( ptr, bc.bv_val );
3819 if ( !BER_BVISEMPTY( &si->si_filterstr ) ) {
3820 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
3821 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
3822 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
3825 if ( !BER_BVISNULL( &si->si_base ) ) {
3826 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
3827 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
3828 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
3831 if ( !BER_BVISEMPTY( &si->si_logfilterstr ) ) {
3832 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
3833 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
3834 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
3837 if ( !BER_BVISNULL( &si->si_logbase ) ) {
3838 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
3839 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
3840 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
3843 for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
3844 if ( si->si_scope == scopes[i].val ) {
3845 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + scopes[i].key.bv_len ) return;
3846 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
3847 ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
3851 if ( si->si_attrsonly ) {
3852 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
3853 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
3855 if ( si->si_anfile ) {
3856 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
3857 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
3858 ptr = lutil_strcopy( ptr, si->si_anfile );
3860 } else if ( si->si_allattrs || si->si_allopattrs ||
3861 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ) )
3865 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
3866 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
3868 /* FIXME: add check for overflow */
3869 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
3870 if ( si->si_allattrs ) {
3871 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
3872 if ( old != ptr ) *ptr++ = ',';
3875 if ( si->si_allopattrs ) {
3876 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
3877 if ( old != ptr ) *ptr++ = ',';
3882 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
3883 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
3884 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
3885 /* FIXME: add check for overflow */
3886 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
3888 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
3889 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
3890 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
3892 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
3893 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
3894 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
3895 "refreshAndPersist" : "refreshOnly" );
3897 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
3900 dd = si->si_interval;
3907 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
3908 ptr += snprintf( ptr, WHATSLEFT, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
3909 if ( ptr - buf >= sizeof( buf ) ) return;
3910 } else if ( si->si_retryinterval ) {
3912 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
3913 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
3914 for (i=0; si->si_retryinterval[i]; i++) {
3915 if ( space ) *ptr++ = ' ';
3917 ptr += snprintf( ptr, WHATSLEFT, "%ld ", (long) si->si_retryinterval[i] );
3918 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
3921 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
3923 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
3927 if ( si->si_slimit ) {
3928 if ( WHATSLEFT <= STRLENOF( " " SLIMITSTR "=" ) ) return;
3929 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
3930 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_slimit );
3933 if ( si->si_tlimit ) {
3934 if ( WHATSLEFT <= STRLENOF( " " TLIMITSTR "=" ) ) return;
3935 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
3936 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_tlimit );
3939 if ( si->si_syncdata ) {
3940 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
3941 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
3942 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
3943 ptr = lutil_strcopy( ptr, bc.bv_val );
3946 bc.bv_len = ptr - buf;
3948 ber_dupbv( bv, &bc );
3952 syncrepl_config( ConfigArgs *c )
3954 if (c->op == SLAP_CONFIG_EMIT) {
3955 if ( c->be->be_syncinfo ) {
3959 for ( si = c->be->be_syncinfo; si; si=si->si_next ) {
3960 syncrepl_unparse( si, &bv );
3961 ber_bvarray_add( &c->rvalue_vals, &bv );
3966 } else if ( c->op == LDAP_MOD_DELETE ) {
3967 cookie_state *cs = NULL;
3968 if ( c->be->be_syncinfo ) {
3969 syncinfo_t *si, **sip;
3972 cs = c->be->be_syncinfo->si_cookieState;
3973 for ( sip = &c->be->be_syncinfo, i=0; *sip; i++ ) {
3975 if ( c->valx == -1 || i == c->valx ) {
3977 /* If the task is currently active, we have to leave
3978 * it running. It will exit on its own. This will only
3979 * happen when running on the cn=config DB.
3982 ldap_pvt_runqueue_isrunning( &slapd_rq, si->si_re ) ) {
3985 syncinfo_free( si, 0 );
3994 if ( !c->be->be_syncinfo ) {
3995 SLAP_DBFLAGS( c->be ) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
3997 ber_bvarray_free( cs->cs_vals );
3998 ldap_pvt_thread_mutex_destroy( &cs->cs_mutex );
4004 if ( SLAP_SLURP_SHADOW( c->be ) ) {
4005 Debug(LDAP_DEBUG_ANY, "%s: "
4006 "syncrepl: database already shadowed.\n",
4010 return add_syncrepl( c );