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-2008 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;
66 char si_ridtxt[ STRLENOF("rid=999") + 1 ];
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;
100 Avlnode *si_presentlist;
103 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
104 ldap_pvt_thread_mutex_t si_mutex;
107 static int syncuuid_cmp( const void *, const void * );
108 static int avl_presentlist_insert( syncinfo_t* si, struct berval *syncUUID );
109 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray, struct sync_cookie *, int );
110 static int syncrepl_message_to_op(
111 syncinfo_t *, Operation *, LDAPMessage * );
112 static int syncrepl_message_to_entry(
113 syncinfo_t *, Operation *, LDAPMessage *,
114 Modifications **, Entry **, int );
115 static int syncrepl_entry(
116 syncinfo_t *, Operation*, Entry*,
117 Modifications**,int, struct berval*,
118 struct berval *cookieCSN );
119 static int syncrepl_updateCookie(
120 syncinfo_t *, Operation *, struct berval *,
121 struct sync_cookie * );
122 static struct berval * slap_uuidstr_from_normalized(
123 struct berval *, struct berval *, void * );
125 /* callback functions */
126 static int dn_callback( Operation *, SlapReply * );
127 static int nonpresent_callback( Operation *, SlapReply * );
128 static int null_callback( Operation *, SlapReply * );
130 static AttributeDescription *sync_descs[4];
133 syncrepl_state2str( int state )
136 case LDAP_SYNC_PRESENT:
142 case LDAP_SYNC_MODIFY:
145 case LDAP_SYNC_DELETE:
153 init_syncrepl(syncinfo_t *si)
156 char **attrs, **exattrs;
158 if ( !sync_descs[0] ) {
159 sync_descs[0] = slap_schema.si_ad_objectClass;
160 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
161 sync_descs[2] = slap_schema.si_ad_entryCSN;
162 sync_descs[3] = NULL;
165 if ( si->si_allattrs && si->si_allopattrs )
168 attrs = anlist2attrs( si->si_anlist );
171 if ( si->si_allattrs ) {
174 if ( !is_at_operational( at_find( attrs[i] ) ) ) {
175 for ( j = i; attrs[j] != NULL; j++ ) {
178 attrs[j] = attrs[j+1];
184 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
185 attrs[i] = ch_strdup("*");
188 } else if ( si->si_allopattrs ) {
191 if ( is_at_operational( at_find( attrs[i] ) ) ) {
192 for ( j = i; attrs[j] != NULL; j++ ) {
195 attrs[j] = attrs[j+1];
201 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
202 attrs[i] = ch_strdup("+");
206 for ( i = 0; sync_descs[i] != NULL; i++ ) {
209 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
210 for ( k = j; attrs[k] != NULL; k++ ) {
213 attrs[k] = attrs[k+1];
221 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
223 if ( si->si_allopattrs ) {
224 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ) );
226 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ) );
230 if ( si->si_allopattrs ) {
231 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
233 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
234 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
242 if ( si->si_allattrs == si->si_allopattrs ) {
243 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
244 attrs[i++] = ch_strdup( "*" );
245 attrs[i++] = ch_strdup( "+" );
246 } else if ( si->si_allattrs && !si->si_allopattrs ) {
247 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
248 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
249 attrs[i++] = ch_strdup( "*" );
250 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
251 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
253 } else if ( !si->si_allattrs && si->si_allopattrs ) {
254 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
255 attrs[i++] = ch_strdup( "+" );
256 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
261 si->si_attrs = attrs;
263 exattrs = anlist2attrs( si->si_exanlist );
266 for ( n = 0; exattrs[n] != NULL; n++ ) ;
268 for ( i = 0; sync_descs[i] != NULL; i++ ) {
270 while ( exattrs[j] != NULL ) {
271 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
272 ch_free( exattrs[j] );
273 for ( k = j; exattrs[k] != NULL; k++ ) {
274 exattrs[k] = exattrs[k+1];
282 for ( i = 0; exattrs[i] != NULL; i++ ) {
283 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
285 if ( ( oc = si->si_anlist[j].an_oc ) ) {
287 while ( oc->soc_required[k] ) {
288 if ( !strcmp( exattrs[i],
289 oc->soc_required[k]->sat_cname.bv_val ) ) {
290 ch_free( exattrs[i] );
291 for ( l = i; exattrs[l]; l++ ) {
292 exattrs[l] = exattrs[l+1];
302 for ( i = 0; exattrs[i] != NULL; i++ ) ;
305 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *) );
308 si->si_exattrs = exattrs;
311 typedef struct logschema {
313 struct berval ls_req;
314 struct berval ls_mod;
315 struct berval ls_newRdn;
316 struct berval ls_delRdn;
317 struct berval ls_newSup;
320 static logschema changelog_sc = {
322 BER_BVC("changeType"),
325 BER_BVC("deleteOldRDN"),
326 BER_BVC("newSuperior")
329 static logschema accesslog_sc = {
333 BER_BVC("reqNewRDN"),
334 BER_BVC("reqDeleteOldRDN"),
335 BER_BVC("reqNewSuperior")
343 BerElementBuffer berbuf;
344 BerElement *ber = (BerElement *)&berbuf;
345 LDAPControl c[2], *ctrls[3];
349 char **attrs, *lattrs[8];
354 /* setup LDAP SYNC control */
355 ber_init2( ber, NULL, LBER_USE_DER );
356 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
358 /* If we're using a log but we have no state, then fallback to
359 * normal mode for a full refresh.
361 if ( si->si_syncdata && !si->si_syncCookie.numcsns ) {
362 si->si_logstate = SYNCLOG_FALLBACK;
365 /* Use the log parameters if we're in log mode */
366 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
368 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
372 lattrs[0] = ls->ls_dn.bv_val;
373 lattrs[1] = ls->ls_req.bv_val;
374 lattrs[2] = ls->ls_mod.bv_val;
375 lattrs[3] = ls->ls_newRdn.bv_val;
376 lattrs[4] = ls->ls_delRdn.bv_val;
377 lattrs[5] = ls->ls_newSup.bv_val;
378 lattrs[6] = slap_schema.si_ad_entryCSN->ad_cname.bv_val;
382 base = si->si_logbase.bv_val;
383 filter = si->si_logfilterstr.bv_val;
386 scope = LDAP_SCOPE_SUBTREE;
389 base = si->si_base.bv_val;
390 filter = si->si_filterstr.bv_val;
391 attrs = si->si_attrs;
392 attrsonly = si->si_attrsonly;
393 scope = si->si_scope;
395 if ( si->si_syncdata && si->si_logstate == SYNCLOG_FALLBACK ) {
396 si->si_type = LDAP_SYNC_REFRESH_ONLY;
398 si->si_type = si->si_ctype;
401 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
403 ber_printf( ber, "{eOb}",
404 abs(si->si_type), &si->si_syncCookie.octet_str, rhint );
406 ber_printf( ber, "{eb}",
407 abs(si->si_type), rhint );
410 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 ) ) == LBER_ERROR ) {
415 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
416 c[0].ldctl_iscritical = si->si_type < 0;
419 if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
420 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
421 c[1].ldctl_value = si->si_bindconf.sb_authzId;
422 c[1].ldctl_iscritical = 1;
429 rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
430 ctrls, NULL, NULL, si->si_slimit, &si->si_msgid );
444 int i, j, changed = 0;
446 /* Look for contextCSN from syncprov overlay. If
447 * there's no overlay, this will be a no-op. That means
448 * this is a pure consumer, so local changes will not be
449 * allowed, and all changes will already be reflected in
452 a.a_desc = slap_schema.si_ad_contextCSN;
454 e.e_name = op->o_bd->be_suffix[0];
455 e.e_nname = op->o_bd->be_nsuffix[0];
456 at[0].an_name = a.a_desc->ad_cname;
457 at[0].an_desc = a.a_desc;
458 BER_BVZERO( &at[1].an_name );
460 rs.sr_flags = REP_ENTRY_MODIFIABLE;
462 op->o_req_dn = e.e_name;
463 op->o_req_ndn = e.e_nname;
465 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
466 i = backend_operational( op, &rs );
467 if ( i == LDAP_SUCCESS && a.a_nvals ) {
468 int num = a.a_numvals;
469 /* check for differences */
470 if ( num != si->si_cookieState->cs_num ) {
473 for ( i=0; i<num; i++ ) {
474 if ( ber_bvcmp( &a.a_nvals[i],
475 &si->si_cookieState->cs_vals[i] )) {
482 ber_bvarray_free( si->si_cookieState->cs_vals );
483 ch_free( si->si_cookieState->cs_sids );
484 si->si_cookieState->cs_num = num;
485 si->si_cookieState->cs_vals = a.a_nvals;
486 si->si_cookieState->cs_sids = slap_parse_csn_sids( a.a_nvals,
488 si->si_cookieState->cs_age++;
490 ber_bvarray_free( a.a_nvals );
492 ber_bvarray_free( a.a_vals );
494 /* See if the cookieState has changed due to anything outside
495 * this particular consumer. That includes other consumers in
496 * the same context, or local changes detected above.
498 if ( si->si_cookieState->cs_num > 0 && si->si_cookieAge !=
499 si->si_cookieState->cs_age ) {
500 if ( !si->si_syncCookie.numcsns ) {
501 ber_bvarray_free( si->si_syncCookie.ctxcsn );
502 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
503 si->si_cookieState->cs_vals, NULL );
506 for (i=0; !BER_BVISNULL( &si->si_syncCookie.ctxcsn[i] ); i++) {
507 /* bogus, just dup everything */
508 if ( si->si_syncCookie.sids[i] == -1 ) {
509 ber_bvarray_free( si->si_syncCookie.ctxcsn );
510 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
511 si->si_cookieState->cs_vals, NULL );
515 for (j=0; j<si->si_cookieState->cs_num; j++) {
516 if ( si->si_syncCookie.sids[i] !=
517 si->si_cookieState->cs_sids[j] )
519 if ( bvmatch( &si->si_syncCookie.ctxcsn[i],
520 &si->si_cookieState->cs_vals[j] ))
522 ber_bvreplace( &si->si_syncCookie.ctxcsn[i],
523 &si->si_cookieState->cs_vals[j] );
531 si->si_cookieAge = si->si_cookieState->cs_age;
532 ch_free( si->si_syncCookie.octet_str.bv_val );
533 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
534 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
535 si->si_syncCookie.sid );
537 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
547 int cmdline_cookie_found = 0;
549 struct sync_cookie *sc = NULL;
554 rc = slap_client_connect( &si->si_ld, &si->si_bindconf );
555 if ( rc != LDAP_SUCCESS ) {
558 op->o_protocol = LDAP_VERSION3;
560 /* Set SSF to strongest of TLS, SASL SSFs */
563 op->o_transport_ssf = 0;
565 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
566 == LDAP_SUCCESS && ssl != NULL )
568 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
570 #endif /* HAVE_TLS */
571 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
572 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
573 ? op->o_sasl_ssf : op->o_tls_ssf;
575 ldap_set_option( si->si_ld, LDAP_OPT_TIMELIMIT, &si->si_tlimit );
577 si->si_syncCookie.rid = si->si_rid;
578 si->si_syncCookie.sid = SLAP_SINGLE_SHADOW( si->si_be ) ? -1 :
581 /* We've just started up, or the remote server hasn't sent us
582 * any meaningful state.
584 if ( BER_BVISNULL( &si->si_syncCookie.octet_str ) ) {
587 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
588 if ( si->si_rid == sc->rid ) {
589 cmdline_cookie_found = 1;
594 if ( cmdline_cookie_found ) {
595 /* cookie is supplied in the command line */
597 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
599 /* ctxcsn wasn't parsed yet, do it now */
600 slap_parse_sync_cookie( sc, op->o_tmpmemctx );
601 slap_sync_cookie_free( &si->si_syncCookie, 0 );
602 slap_dup_sync_cookie( &si->si_syncCookie, sc );
603 slap_sync_cookie_free( sc, 1 );
605 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
606 if ( !si->si_cookieState->cs_num ) {
607 /* get contextCSN shadow replica from database */
608 BerVarray csn = NULL;
609 void *ctx = op->o_tmpmemctx;
611 op->o_req_ndn = op->o_bd->be_nsuffix[0];
612 op->o_req_dn = op->o_req_ndn;
614 /* try to read stored contextCSN */
615 op->o_tmpmemctx = NULL;
616 backend_attribute( op, NULL, &op->o_req_ndn,
617 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
618 op->o_tmpmemctx = ctx;
620 si->si_cookieState->cs_vals = csn;
621 for (i=0; !BER_BVISNULL( &csn[i] ); i++);
622 si->si_cookieState->cs_num = i;
623 si->si_cookieState->cs_sids = slap_parse_csn_sids( csn, i, NULL );
626 if ( si->si_cookieState->cs_num ) {
627 ber_bvarray_free( si->si_syncCookie.ctxcsn );
628 if ( ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
629 si->si_cookieState->cs_vals, NULL )) {
631 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
634 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
635 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num *
637 for ( i=0; i<si->si_syncCookie.numcsns; i++ )
638 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
640 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
643 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
644 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
645 si->si_syncCookie.sid );
647 /* Look for contextCSN from syncprov overlay. */
648 check_syncprov( op, si );
651 si->si_refreshDone = 0;
653 rc = ldap_sync_search( si, op->o_tmpmemctx );
655 if( rc != LDAP_SUCCESS ) {
656 Debug( LDAP_DEBUG_ANY, "do_syncrep1: %s "
657 "ldap_search_ext: %s (%d)\n",
658 si->si_ridtxt, ldap_err2string( rc ), rc );
664 ldap_unbind_ext( si->si_ld, NULL, NULL );
673 compare_csns( struct sync_cookie *sc1, struct sync_cookie *sc2, int *which )
680 if ( sc1->numcsns < sc2->numcsns ) {
681 *which = sc1->numcsns;
685 for (i=0; i<sc1->numcsns; i++) {
686 for (j=0; j<sc2->numcsns; j++) {
687 if ( sc1->sids[i] != sc2->sids[j] )
689 value_match( &match, slap_schema.si_ad_entryCSN,
690 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
691 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
692 &sc1->ctxcsn[i], &sc2->ctxcsn[j], &text );
703 #define SYNC_PAUSED -3
710 LDAPControl **rctrls = NULL;
713 BerElementBuffer berbuf;
714 BerElement *ber = (BerElement *)&berbuf;
716 LDAPMessage *msg = NULL;
719 struct berval *retdata = NULL;
724 struct berval syncUUID = BER_BVNULL;
725 struct sync_cookie syncCookie = { NULL };
726 struct sync_cookie syncCookie_req = { NULL };
727 struct berval cookie = BER_BVNULL;
734 Modifications *modlist = NULL;
738 struct timeval *tout_p = NULL;
739 struct timeval tout = { 0, 0 };
741 int refreshDeletes = 0;
742 BerVarray syncUUIDs = NULL;
745 if ( slapd_shutdown ) {
750 ber_init2( ber, NULL, LBER_USE_DER );
751 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
753 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2 %s\n", si->si_ridtxt, 0, 0 );
755 psub = &si->si_be->be_nsuffix[0];
757 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
759 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
765 while ( ( rc = ldap_result( si->si_ld, si->si_msgid, LDAP_MSG_ONE,
766 tout_p, &msg ) ) > 0 )
768 if ( slapd_shutdown ) {
772 switch( ldap_msgtype( msg ) ) {
773 case LDAP_RES_SEARCH_ENTRY:
774 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
775 /* we can't work without the control */
779 /* NOTE: make sure we use the right one;
780 * a better approach would be to run thru
781 * the whole list and take care of all */
782 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_STATE, rctrls, &next );
783 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_STATE, next, NULL ) )
785 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
786 "got search entry with multiple "
787 "Sync State control\n", si->si_ridtxt, 0, 0 );
792 if ( rctrlp == NULL ) {
793 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
794 "got search entry without "
795 "Sync State control\n", si->si_ridtxt, 0, 0 );
799 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
800 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
801 /* FIXME: what if syncUUID is NULL or empty?
802 * (happens with back-sql...) */
803 if ( BER_BVISEMPTY( &syncUUID ) ) {
804 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
805 "got empty syncUUID with LDAP_SYNC_%s\n",
807 syncrepl_state2str( syncstate ), 0 );
808 ldap_controls_free( rctrls );
812 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
813 ber_scanf( ber, /*"{"*/ "m}", &cookie );
815 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: cookie=%s\n",
816 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0, 0 );
818 if ( !BER_BVISNULL( &cookie ) ) {
819 ch_free( syncCookie.octet_str.bv_val );
820 ber_dupbv( &syncCookie.octet_str, &cookie );
822 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
824 slap_parse_sync_cookie( &syncCookie, NULL );
825 if ( syncCookie.ctxcsn ) {
826 int i, sid = slap_parse_csn_sid( syncCookie.ctxcsn );
827 for ( i =0; i<si->si_cookieState->cs_num; i++ ) {
828 if ( si->si_cookieState->cs_sids[i] == sid &&
829 ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_vals[i] ) <= 0 ) {
830 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN too old, ignoring %s\n",
831 si->si_ridtxt, syncCookie.ctxcsn->bv_val, 0 );
832 ldap_controls_free( rctrls );
841 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
843 if ( ( rc = syncrepl_message_to_op( si, op, msg ) ) == LDAP_SUCCESS &&
846 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
847 } else switch ( rc ) {
848 case LDAP_ALREADY_EXISTS:
849 case LDAP_NO_SUCH_OBJECT:
850 case LDAP_NO_SUCH_ATTRIBUTE:
851 case LDAP_TYPE_OR_VALUE_EXISTS:
852 rc = LDAP_SYNC_REFRESH_REQUIRED;
853 si->si_logstate = SYNCLOG_FALLBACK;
854 ldap_abandon_ext( si->si_ld, si->si_msgid, NULL, NULL );
859 } else if ( ( rc = syncrepl_message_to_entry( si, op, msg,
860 &modlist, &entry, syncstate ) ) == LDAP_SUCCESS )
862 if ( ( rc = syncrepl_entry( si, op, entry, &modlist,
863 syncstate, &syncUUID, syncCookie.ctxcsn ) ) == LDAP_SUCCESS &&
866 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
869 ldap_controls_free( rctrls );
871 slap_mods_free( modlist, 1 );
877 case LDAP_RES_SEARCH_REFERENCE:
878 Debug( LDAP_DEBUG_ANY,
879 "do_syncrep2: %s reference received error\n",
880 si->si_ridtxt, 0, 0 );
883 case LDAP_RES_SEARCH_RESULT:
884 Debug( LDAP_DEBUG_SYNC,
885 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT\n",
886 si->si_ridtxt, 0, 0 );
887 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
889 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
890 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
891 /* map old result code to registered code */
892 err = LDAP_SYNC_REFRESH_REQUIRED;
895 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
896 if ( si->si_logstate == SYNCLOG_LOGGING ) {
897 si->si_logstate = SYNCLOG_FALLBACK;
904 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
906 ber_scanf( ber, "{" /*"}"*/);
907 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
908 ber_scanf( ber, "m", &cookie );
910 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: cookie=%s\n",
911 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0, 0 );
913 if ( !BER_BVISNULL( &cookie ) ) {
914 ch_free( syncCookie.octet_str.bv_val );
915 ber_dupbv( &syncCookie.octet_str, &cookie);
917 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
919 slap_parse_sync_cookie( &syncCookie, NULL );
922 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
924 ber_scanf( ber, "b", &refreshDeletes );
926 ber_scanf( ber, /*"{"*/ "}" );
928 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
929 slap_sync_cookie_free( &syncCookie_req, 0 );
930 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
932 if ( !syncCookie.ctxcsn ) {
934 } else if ( !syncCookie_req.ctxcsn ) {
938 match = compare_csns( &syncCookie_req, &syncCookie, &m );
941 ldap_controls_free( rctrls );
943 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
944 /* FIXME : different error behaviors according to
945 * 1) err code : LDAP_BUSY ...
946 * 2) on err policy : stop service, stop sync, retry
948 if ( refreshDeletes == 0 && match < 0 &&
949 err == LDAP_SUCCESS &&
950 syncCookie_req.numcsns == syncCookie.numcsns )
952 syncrepl_del_nonpresent( op, si, NULL,
955 avl_free( si->si_presentlist, ch_free );
956 si->si_presentlist = NULL;
959 if ( syncCookie.ctxcsn && match < 0 && err == LDAP_SUCCESS )
961 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
963 if ( err == LDAP_SUCCESS
964 && si->si_logstate == SYNCLOG_FALLBACK ) {
965 si->si_logstate = SYNCLOG_LOGGING;
966 rc = LDAP_SYNC_REFRESH_REQUIRED;
973 case LDAP_RES_INTERMEDIATE:
974 rc = ldap_parse_intermediate( si->si_ld, msg,
975 &retoid, &retdata, NULL, 0 );
976 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
977 ber_init2( ber, retdata, LBER_USE_DER );
979 switch ( si_tag = ber_peek_tag( ber, &len ) ) {
981 case LDAP_TAG_SYNC_NEW_COOKIE:
982 Debug( LDAP_DEBUG_SYNC,
983 "do_syncrep2: %s %s - %s\n",
985 "LDAP_RES_INTERMEDIATE",
987 ber_scanf( ber, "tm", &tag, &cookie );
989 case LDAP_TAG_SYNC_REFRESH_DELETE:
990 case LDAP_TAG_SYNC_REFRESH_PRESENT:
991 Debug( LDAP_DEBUG_SYNC,
992 "do_syncrep2: %s %s - %s\n",
994 "LDAP_RES_INTERMEDIATE",
995 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
996 "REFRESH_PRESENT" : "REFRESH_DELETE" );
997 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
998 si->si_refreshDelete = 1;
1000 si->si_refreshPresent = 1;
1002 ber_scanf( ber, "t{" /*"}"*/, &tag );
1003 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
1005 ber_scanf( ber, "m", &cookie );
1007 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: cookie=%s\n",
1008 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0, 0 );
1010 if ( !BER_BVISNULL( &cookie ) ) {
1011 ch_free( syncCookie.octet_str.bv_val );
1012 ber_dupbv( &syncCookie.octet_str, &cookie );
1014 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1016 slap_parse_sync_cookie( &syncCookie, NULL );
1019 /* Defaults to TRUE */
1020 if ( ber_peek_tag( ber, &len ) ==
1021 LDAP_TAG_REFRESHDONE )
1023 ber_scanf( ber, "b", &si->si_refreshDone );
1026 si->si_refreshDone = 1;
1028 ber_scanf( ber, /*"{"*/ "}" );
1030 case LDAP_TAG_SYNC_ID_SET:
1031 Debug( LDAP_DEBUG_SYNC,
1032 "do_syncrep2: %s %s - %s\n",
1034 "LDAP_RES_INTERMEDIATE",
1036 ber_scanf( ber, "t{" /*"}"*/, &tag );
1037 if ( ber_peek_tag( ber, &len ) ==
1038 LDAP_TAG_SYNC_COOKIE )
1040 ber_scanf( ber, "m", &cookie );
1042 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: cookie=%s\n",
1043 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0, 0 );
1045 if ( !BER_BVISNULL( &cookie ) ) {
1046 ch_free( syncCookie.octet_str.bv_val );
1047 ber_dupbv( &syncCookie.octet_str, &cookie );
1049 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1051 slap_parse_sync_cookie( &syncCookie, NULL );
1052 compare_csns( &syncCookie_req, &syncCookie, &m );
1055 if ( ber_peek_tag( ber, &len ) ==
1056 LDAP_TAG_REFRESHDELETES )
1058 ber_scanf( ber, "b", &refreshDeletes );
1060 ber_scanf( ber, "[W]", &syncUUIDs );
1061 ber_scanf( ber, /*"{"*/ "}" );
1062 if ( refreshDeletes ) {
1063 syncrepl_del_nonpresent( op, si, syncUUIDs,
1065 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
1068 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
1069 (void)avl_presentlist_insert( si, &syncUUIDs[i] );
1070 slap_sl_free( syncUUIDs[i].bv_val, op->o_tmpmemctx );
1072 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
1074 slap_sync_cookie_free( &syncCookie, 0 );
1077 Debug( LDAP_DEBUG_ANY,
1078 "do_syncrep2: %s unknown syncinfo tag (%ld)\n",
1079 si->si_ridtxt, (long) si_tag, 0 );
1080 ldap_memfree( retoid );
1081 ber_bvfree( retdata );
1085 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1086 slap_sync_cookie_free( &syncCookie_req, 0 );
1087 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1089 if ( !syncCookie.ctxcsn ) {
1091 } else if ( !syncCookie_req.ctxcsn ) {
1095 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1099 if ( si->si_refreshPresent == 1 &&
1100 syncCookie_req.numcsns == syncCookie.numcsns ) {
1101 syncrepl_del_nonpresent( op, si, NULL,
1105 if ( syncCookie.ctxcsn )
1107 rc = syncrepl_updateCookie( si, op, psub, &syncCookie);
1111 ldap_memfree( retoid );
1112 ber_bvfree( retdata );
1116 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1117 "unknown intermediate response (%d)\n",
1118 si->si_ridtxt, rc, 0 );
1119 ldap_memfree( retoid );
1120 ber_bvfree( retdata );
1126 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1127 "unknown message (0x%02lx)\n",
1129 (unsigned long)ldap_msgtype( msg ), 0 );
1133 if ( !BER_BVISNULL( &syncCookie.octet_str ) ) {
1134 slap_sync_cookie_free( &syncCookie_req, 0 );
1135 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
1136 slap_sync_cookie_free( &syncCookie, 0 );
1138 ldap_msgfree( msg );
1140 if ( ldap_pvt_thread_pool_pausing( &connection_pool )) {
1141 slap_sync_cookie_free( &syncCookie, 0 );
1142 slap_sync_cookie_free( &syncCookie_req, 0 );
1148 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
1153 if ( err != LDAP_SUCCESS ) {
1154 Debug( LDAP_DEBUG_ANY,
1155 "do_syncrep2: %s (%d) %s\n",
1156 si->si_ridtxt, err, ldap_err2string( err ) );
1159 slap_sync_cookie_free( &syncCookie, 0 );
1160 slap_sync_cookie_free( &syncCookie_req, 0 );
1162 if ( msg ) ldap_msgfree( msg );
1164 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1165 if ( si->si_conn ) {
1166 connection_client_stop( si->si_conn );
1169 ldap_unbind_ext( si->si_ld, NULL, NULL );
1181 struct re_s* rtask = arg;
1182 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1183 Connection conn = {0};
1184 OperationBuffer opbuf;
1186 int rc = LDAP_SUCCESS;
1187 int dostop = 0, do_setup = 0;
1189 int i, defer = 1, fail = 0;
1192 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl %s\n", si->si_ridtxt, 0, 0 );
1197 /* There will never be more than one instance active */
1198 ldap_pvt_thread_mutex_lock( &si->si_mutex );
1200 switch( abs( si->si_type ) ) {
1201 case LDAP_SYNC_REFRESH_ONLY:
1202 case LDAP_SYNC_REFRESH_AND_PERSIST:
1205 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1209 if ( slapd_shutdown ) {
1211 if ( si->si_conn ) {
1212 connection_client_stop( si->si_conn );
1215 ldap_unbind_ext( si->si_ld, NULL, NULL );
1218 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1222 connection_fake_init( &conn, &opbuf, ctx );
1225 /* use global malloc for now */
1226 op->o_tmpmemctx = NULL;
1227 op->o_tmpmfuncs = &ch_mfuncs;
1229 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1232 /* Coordinate contextCSN updates with any syncprov overlays
1233 * in use. This may be complicated by the use of the glue
1236 * Typically there is a single syncprov mastering the entire
1237 * glued tree. In that case, our contextCSN updates should
1238 * go to the master DB.
1240 * Alternatively, there may be individual syncprov overlays
1241 * on each glued branch. In that case, each syncprov only
1242 * knows about changes within its own branch. And so our
1243 * contextCSN updates should only go to the local DB.
1245 if ( !si->si_wbe ) {
1246 if ( SLAP_GLUE_SUBORDINATE( be ) && !overlay_is_inst( be, "syncprov" )) {
1247 si->si_wbe = select_backend( &be->be_nsuffix[0], 1 );
1252 if ( !si->si_schemachecking )
1253 op->o_no_schema_check = 1;
1255 /* Establish session, do search */
1257 si->si_refreshDelete = 0;
1258 si->si_refreshPresent = 0;
1260 /* use main DB when retrieving contextCSN */
1261 op->o_bd = si->si_wbe;
1262 op->o_dn = op->o_bd->be_rootdn;
1263 op->o_ndn = op->o_bd->be_rootndn;
1264 rc = do_syncrep1( op, si );
1268 /* Process results */
1269 if ( rc == LDAP_SUCCESS ) {
1270 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1272 /* use current DB */
1274 op->o_dn = op->o_bd->be_rootdn;
1275 op->o_ndn = op->o_bd->be_rootndn;
1276 rc = do_syncrep2( op, si );
1277 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1278 rc = ldap_sync_search( si, op->o_tmpmemctx );
1282 /* We got deleted while running on cn=config */
1283 if ( !si->si_ctype ) {
1289 if ( rc != SYNC_PAUSED ) {
1290 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1291 /* If we succeeded, enable the connection for further listening.
1292 * If we failed, tear down the connection and reschedule.
1294 if ( rc == LDAP_SUCCESS ) {
1295 if ( si->si_conn ) {
1296 connection_client_enable( si->si_conn );
1301 } else if ( si->si_conn ) {
1305 if ( rc == -2 ) rc = 0;
1310 /* At this point, we have 5 cases:
1311 * 1) for any hard failure, give up and remove this task
1312 * 2) for ServerDown, reschedule this task to run later
1313 * 3) for threadpool pause, reschedule to run immediately
1314 * 4) for Refresh and Success, reschedule to run
1315 * 5) for Persist and Success, reschedule to defer
1317 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1319 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask ) ) {
1320 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1324 connection_client_stop( si->si_conn );
1328 if ( rc == SYNC_PAUSED ) {
1329 rtask->interval.tv_sec = 0;
1330 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1332 } else if ( rc == LDAP_SUCCESS ) {
1333 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1336 rtask->interval.tv_sec = si->si_interval;
1337 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1338 if ( si->si_retrynum ) {
1339 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1340 si->si_retrynum[i] = si->si_retrynum_init[i];
1342 si->si_retrynum[i] = RETRYNUM_TAIL;
1345 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1346 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1351 || !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1352 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1353 fail = RETRYNUM_TAIL;
1354 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1355 if ( si->si_retrynum[i] > 0 )
1356 si->si_retrynum[i]--;
1357 fail = si->si_retrynum[i];
1358 rtask->interval.tv_sec = si->si_retryinterval[i];
1359 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1360 slap_wake_listener();
1364 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1367 si->si_conn = connection_client_setup( s, do_syncrepl, arg );
1370 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1373 if ( fail == RETRYNUM_TAIL ) {
1374 Debug( LDAP_DEBUG_ANY,
1375 "do_syncrepl: %s quitting\n",
1376 si->si_ridtxt, 0, 0 );
1377 } else if ( fail > 0 ) {
1378 Debug( LDAP_DEBUG_ANY,
1379 "do_syncrepl: %s retrying (%d retries left)\n",
1380 si->si_ridtxt, fail, 0 );
1382 Debug( LDAP_DEBUG_ANY,
1383 "do_syncrepl: %s retrying\n",
1384 si->si_ridtxt, 0, 0 );
1388 /* Do final delete cleanup */
1389 if ( !si->si_ctype ) {
1390 cookie_state *cs = NULL;
1393 cs = be->be_syncinfo->si_cookieState;
1394 for ( sip = &be->be_syncinfo; *sip != si; sip = &(*sip)->si_next );
1396 syncinfo_free( si, 0 );
1397 if ( !be->be_syncinfo ) {
1398 SLAP_DBFLAGS( be ) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
1400 ch_free( cs->cs_sids );
1401 ber_bvarray_free( cs->cs_vals );
1402 ldap_pvt_thread_mutex_destroy( &cs->cs_mutex );
1410 static slap_verbmasks modops[] = {
1411 { BER_BVC("add"), LDAP_REQ_ADD },
1412 { BER_BVC("delete"), LDAP_REQ_DELETE },
1413 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1414 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1418 static Modifications *
1419 syncrepl_accesslog_mods(
1426 AttributeDescription *ad;
1427 struct berval bv, bv2;
1429 Modifications *mod = NULL, *modlist = NULL, **modtail;
1434 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1438 colon = ber_bvchr( &bv, ':' );
1444 bv.bv_len = colon - bv.bv_val;
1445 if ( slap_bv2ad( &bv, &ad, &text ) ) {
1450 /* Ignore dynamically generated attrs */
1451 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1455 /* Ignore excluded attrs */
1456 if ( ldap_charray_inlist( si->si_exattrs,
1457 ad->ad_type->sat_cname.bv_val ) )
1463 case '+': op = LDAP_MOD_ADD; break;
1464 case '-': op = LDAP_MOD_DELETE; break;
1465 case '=': op = LDAP_MOD_REPLACE; break;
1466 case '#': op = LDAP_MOD_INCREMENT; break;
1470 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1471 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1474 mod->sml_next = NULL;
1476 mod->sml_type = ad->ad_cname;
1477 mod->sml_values = NULL;
1478 mod->sml_nvalues = NULL;
1479 mod->sml_numvals = 0;
1482 modtail = &mod->sml_next;
1484 if ( colon[2] == ' ' ) {
1485 bv.bv_val = colon + 3;
1486 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1487 ber_dupbv( &bv2, &bv );
1488 ber_bvarray_add( &mod->sml_values, &bv2 );
1495 static Modifications *
1496 syncrepl_changelog_mods(
1501 return NULL; /* FIXME */
1505 syncrepl_message_to_op(
1511 BerElement *ber = NULL;
1512 Modifications *modlist = NULL;
1514 SlapReply rs = { REP_RESULT };
1515 slap_callback cb = { NULL, null_callback, NULL, NULL };
1518 char txtbuf[SLAP_TEXT_BUFLEN];
1519 size_t textlen = sizeof txtbuf;
1521 struct berval bdn, dn = BER_BVNULL, ndn;
1522 struct berval bv, *bvals = NULL;
1523 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1524 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1525 psup = BER_BVNULL, nsup = BER_BVNULL;
1526 int rc, deleteOldRdn = 0, freeReqDn = 0;
1528 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1529 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1530 "Message type should be entry (%d)",
1531 si->si_ridtxt, ldap_msgtype( msg ), 0 );
1535 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1540 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1542 if ( rc != LDAP_SUCCESS ) {
1543 Debug( LDAP_DEBUG_ANY,
1544 "syncrepl_message_to_op: %s dn get failed (%d)",
1545 si->si_ridtxt, rc, 0 );
1549 op->o_tag = LBER_DEFAULT;
1550 op->o_bd = si->si_wbe;
1552 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ) )
1554 if ( bv.bv_val == NULL )
1557 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn ) ) {
1559 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1560 ber_dupbv( &op->o_req_dn, &dn );
1561 ber_dupbv( &op->o_req_ndn, &ndn );
1562 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1563 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1565 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req ) ) {
1566 int i = verb_to_mask( bvals[0].bv_val, modops );
1568 Debug( LDAP_DEBUG_ANY,
1569 "syncrepl_message_to_op: %s unknown op %s",
1570 si->si_ridtxt, bvals[0].bv_val, 0 );
1575 op->o_tag = modops[i].mask;
1576 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod ) ) {
1577 /* Parse attribute into modlist */
1578 if ( si->si_syncdata == SYNCDATA_ACCESSLOG ) {
1579 modlist = syncrepl_accesslog_mods( si, bvals );
1581 modlist = syncrepl_changelog_mods( si, bvals );
1583 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn ) ) {
1585 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn ) ) {
1586 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ) ) {
1589 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup ) ) {
1591 } else if ( !ber_bvstrcasecmp( &bv,
1592 &slap_schema.si_ad_entryCSN->ad_cname ) )
1594 slap_queue_csn( op, bvals );
1599 /* If we didn't get a mod type or a target DN, bail out */
1600 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn ) ) {
1605 op->o_callback = &cb;
1606 slap_op_time( &op->o_time, &op->o_tincr );
1608 switch( op->o_tag ) {
1610 case LDAP_REQ_MODIFY:
1611 /* If we didn't get required data, bail */
1612 if ( !modlist ) goto done;
1614 rc = slap_mods_check( op, modlist, &text, txtbuf, textlen, NULL );
1616 if ( rc != LDAP_SUCCESS ) {
1617 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1618 "mods check (%s)\n",
1619 si->si_ridtxt, text, 0 );
1623 if ( op->o_tag == LDAP_REQ_ADD ) {
1624 Entry *e = entry_alloc();
1626 op->ora_e->e_name = op->o_req_dn;
1627 op->ora_e->e_nname = op->o_req_ndn;
1629 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1630 if( rc != LDAP_SUCCESS ) {
1631 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1632 "mods2entry (%s)\n",
1633 si->si_ridtxt, text, 0 );
1635 rc = op->o_bd->be_add( op, &rs );
1636 Debug( LDAP_DEBUG_SYNC,
1637 "syncrepl_message_to_op: %s be_add %s (%d)\n",
1638 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1640 if ( e == op->ora_e )
1641 be_entry_release_w( op, op->ora_e );
1643 op->orm_modlist = modlist;
1644 op->o_bd = si->si_wbe;
1645 rc = op->o_bd->be_modify( op, &rs );
1646 modlist = op->orm_modlist;
1647 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1648 "syncrepl_message_to_op: %s be_modify %s (%d)\n",
1649 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1650 op->o_bd = si->si_be;
1653 case LDAP_REQ_MODRDN:
1654 if ( BER_BVISNULL( &rdn ) ) goto done;
1656 if ( rdnPretty( NULL, &rdn, &prdn, NULL ) ) {
1659 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ) ) {
1662 if ( !BER_BVISNULL( &sup ) ) {
1663 if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ) ) {
1666 op->orr_newSup = &psup;
1667 op->orr_nnewSup = ⊅
1669 op->orr_newSup = NULL;
1670 op->orr_nnewSup = NULL;
1672 op->orr_newrdn = prdn;
1673 op->orr_nnewrdn = nrdn;
1674 op->orr_deleteoldrdn = deleteOldRdn;
1675 op->orr_modlist = NULL;
1676 if ( slap_modrdn2mods( op, &rs ) ) {
1680 /* Append modlist for operational attrs */
1684 for ( m = op->orr_modlist; m->sml_next; m = m->sml_next )
1686 m->sml_next = modlist;
1689 rc = op->o_bd->be_modrdn( op, &rs );
1690 slap_mods_free( op->orr_modlist, 1 );
1691 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1692 "syncrepl_message_to_op: %s be_modrdn %s (%d)\n",
1693 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1695 case LDAP_REQ_DELETE:
1696 rc = op->o_bd->be_delete( op, &rs );
1697 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1698 "syncrepl_message_to_op: %s be_delete %s (%d)\n",
1699 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1703 slap_graduate_commit_csn( op );
1704 op->o_bd = si->si_be;
1705 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
1706 BER_BVZERO( &op->o_csn );
1708 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1710 if ( !BER_BVISNULL( &rdn ) ) {
1711 if ( !BER_BVISNULL( &nsup ) ) {
1712 ch_free( nsup.bv_val );
1714 if ( !BER_BVISNULL( &psup ) ) {
1715 ch_free( psup.bv_val );
1717 if ( !BER_BVISNULL( &nrdn ) ) {
1718 ch_free( nrdn.bv_val );
1720 if ( !BER_BVISNULL( &prdn ) ) {
1721 ch_free( prdn.bv_val );
1725 ch_free( op->o_req_ndn.bv_val );
1726 ch_free( op->o_req_dn.bv_val );
1733 syncrepl_message_to_entry(
1737 Modifications **modlist,
1743 BerElement *ber = NULL;
1746 Modifications **modtail = modlist;
1749 char txtbuf[SLAP_TEXT_BUFLEN];
1750 size_t textlen = sizeof txtbuf;
1752 struct berval bdn = BER_BVNULL, dn, ndn;
1757 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1758 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s "
1759 "Message type should be entry (%d)",
1760 si->si_ridtxt, ldap_msgtype( msg ), 0 );
1764 op->o_tag = LDAP_REQ_ADD;
1766 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1767 if ( rc != LDAP_SUCCESS ) {
1768 Debug( LDAP_DEBUG_ANY,
1769 "syncrepl_message_to_entry: %s dn get failed (%d)",
1770 si->si_ridtxt, rc, 0 );
1774 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1775 /* NOTE: this could be done even before decoding the DN,
1776 * although encoding errors wouldn't be detected */
1781 if ( entry == NULL ) {
1785 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1786 ber_dupbv( &op->o_req_dn, &dn );
1787 ber_dupbv( &op->o_req_ndn, &ndn );
1788 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1789 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1791 is_ctx = dn_match( &op->o_req_ndn, &op->o_bd->be_nsuffix[0] );
1794 e->e_name = op->o_req_dn;
1795 e->e_nname = op->o_req_ndn;
1797 while ( ber_remaining( ber ) ) {
1798 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1799 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1804 /* Drop all updates to the contextCSN of the context entry
1807 if ( is_ctx && !strcasecmp( tmp.sml_type.bv_val,
1808 slap_schema.si_ad_contextCSN->ad_cname.bv_val )) {
1809 ber_bvarray_free( tmp.sml_values );
1813 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1815 mod->sml_op = LDAP_MOD_REPLACE;
1817 mod->sml_next = NULL;
1818 mod->sml_desc = NULL;
1819 mod->sml_type = tmp.sml_type;
1820 mod->sml_values = tmp.sml_values;
1821 mod->sml_nvalues = NULL;
1822 mod->sml_numvals = 0; /* slap_mods_check will set this */
1825 modtail = &mod->sml_next;
1828 if ( *modlist == NULL ) {
1829 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s no attributes\n",
1830 si->si_ridtxt, 0, 0 );
1835 rc = slap_mods_check( op, *modlist, &text, txtbuf, textlen, NULL );
1837 if ( rc != LDAP_SUCCESS ) {
1838 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods check (%s)\n",
1839 si->si_ridtxt, text, 0 );
1843 /* Strip out dynamically generated attrs */
1844 for ( modtail = modlist; *modtail ; ) {
1846 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1847 *modtail = mod->sml_next;
1848 slap_mod_free( &mod->sml_mod, 0 );
1851 modtail = &mod->sml_next;
1855 /* Strip out attrs in exattrs list */
1856 for ( modtail = modlist; *modtail ; ) {
1858 if ( ldap_charray_inlist( si->si_exattrs,
1859 mod->sml_desc->ad_type->sat_cname.bv_val ) )
1861 *modtail = mod->sml_next;
1862 slap_mod_free( &mod->sml_mod, 0 );
1865 modtail = &mod->sml_next;
1869 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1870 if( rc != LDAP_SUCCESS ) {
1871 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods2entry (%s)\n",
1872 si->si_ridtxt, text, 0 );
1877 if ( rc != LDAP_SUCCESS ) {
1889 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1891 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1892 * entry if a previous refresh was interrupted before sending us a new
1893 * context state. We try to compare the new entry to the existing entry
1894 * and ignore the new entry if they are the same.
1896 * Also, we may get an update where the entryDN has changed, due to
1897 * a ModDn on the provider. We detect this as well, so we can issue
1898 * the corresponding operation locally.
1900 * In the case of a modify, we get a list of all the attributes
1901 * in the original entry. Rather than deleting the entry and re-adding it,
1902 * we issue a Modify request that deletes all the attributes and adds all
1903 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1906 * We otherwise distinguish ModDN from Modify; in the case of
1907 * a ModDN we just use the CSN, modifyTimestamp and modifiersName
1908 * operational attributes from the entry, and do a regular ModDN.
1910 typedef struct dninfo {
1914 int renamed; /* Was an existing entry renamed? */
1915 int delOldRDN; /* Was old RDN deleted? */
1916 Modifications **modlist; /* the modlist we received */
1917 Modifications *mods; /* the modlist we compared */
1920 /* return 1 if inserted, 0 otherwise */
1922 avl_presentlist_insert(
1924 struct berval *syncUUID )
1926 struct berval *syncuuid_bv = ch_malloc( sizeof( struct berval ) + syncUUID->bv_len + 1 );
1928 syncuuid_bv->bv_len = syncUUID->bv_len;
1929 syncuuid_bv->bv_val = (char *)&syncuuid_bv[1];
1930 AC_MEMCPY( syncuuid_bv->bv_val, syncUUID->bv_val, syncUUID->bv_len );
1931 syncuuid_bv->bv_val[ syncuuid_bv->bv_len ] = '\0';
1933 if ( avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1934 syncuuid_cmp, avl_dup_error ) )
1936 ch_free( syncuuid_bv );
1948 Modifications** modlist,
1950 struct berval* syncUUID,
1951 struct berval* syncCSN )
1953 Backend *be = op->o_bd;
1954 slap_callback cb = { NULL, NULL, NULL, NULL };
1955 int syncuuid_inserted = 0;
1956 struct berval syncUUID_strrep = BER_BVNULL;
1958 SlapReply rs_search = {REP_RESULT};
1959 SlapReply rs_delete = {REP_RESULT};
1960 SlapReply rs_add = {REP_RESULT};
1961 SlapReply rs_modify = {REP_RESULT};
1963 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
1964 int rc = LDAP_SUCCESS;
1966 struct berval pdn = BER_BVNULL;
1971 Debug( LDAP_DEBUG_SYNC,
1972 "syncrepl_entry: %s LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_%s)\n",
1973 si->si_ridtxt, syncrepl_state2str( syncstate ), 0 );
1975 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ) ) {
1976 if ( !si->si_refreshPresent && !si->si_refreshDone ) {
1977 syncuuid_inserted = avl_presentlist_insert( si, syncUUID );
1981 if ( syncstate == LDAP_SYNC_PRESENT ) {
1983 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1984 if ( entry == NULL ) {
1989 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1990 if ( syncstate != LDAP_SYNC_DELETE ) {
1991 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1994 /* add if missing */
1995 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1996 &syncUUID_strrep, syncUUID );
1998 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1999 /* replace only if necessary */
2000 if ( a->a_nvals != a->a_vals ) {
2001 ber_memfree( a->a_nvals[0].bv_val );
2002 ber_dupbv( &a->a_nvals[0], syncUUID );
2004 ber_memfree( a->a_vals[0].bv_val );
2005 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
2009 f.f_choice = LDAP_FILTER_EQUALITY;
2011 ava.aa_desc = slap_schema.si_ad_entryUUID;
2012 ava.aa_value = *syncUUID;
2014 if ( syncuuid_inserted ) {
2015 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s inserted UUID %s\n",
2016 si->si_ridtxt, syncUUID_strrep.bv_val, 0 );
2018 op->ors_filter = &f;
2020 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID_strrep.bv_len;
2021 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
2022 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
2023 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
2024 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
2025 syncUUID_strrep.bv_val, syncUUID_strrep.bv_len );
2026 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
2027 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
2029 op->o_tag = LDAP_REQ_SEARCH;
2030 op->ors_scope = LDAP_SCOPE_SUBTREE;
2031 op->ors_deref = LDAP_DEREF_NEVER;
2033 /* get the entry for this UUID */
2034 op->o_req_dn = si->si_base;
2035 op->o_req_ndn = si->si_base;
2037 op->o_time = slap_get_time();
2038 op->ors_tlimit = SLAP_NO_LIMIT;
2041 op->ors_attrs = slap_anlist_all_attributes;
2042 op->ors_attrsonly = 0;
2044 /* set callback function */
2045 op->o_callback = &cb;
2046 cb.sc_response = dn_callback;
2047 cb.sc_private = &dni;
2048 dni.new_entry = entry;
2049 dni.modlist = modlist;
2051 if ( limits_check( op, &rs_search ) == 0 ) {
2052 rc = be->be_search( op, &rs_search );
2053 Debug( LDAP_DEBUG_SYNC,
2054 "syncrepl_entry: %s be_search (%d)\n",
2055 si->si_ridtxt, rc, 0 );
2058 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
2059 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2062 cb.sc_response = null_callback;
2065 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
2066 Debug( LDAP_DEBUG_SYNC,
2067 "syncrepl_entry: %s %s\n",
2068 si->si_ridtxt, entry->e_name.bv_val, 0 );
2070 Debug( LDAP_DEBUG_SYNC,
2071 "syncrepl_entry: %s %s\n",
2072 si->si_ridtxt, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
2075 assert( BER_BVISNULL( &op->o_csn ) );
2077 slap_queue_csn( op, syncCSN );
2080 slap_op_time( &op->o_time, &op->o_tincr );
2081 switch ( syncstate ) {
2083 case LDAP_SYNC_MODIFY:
2084 if ( BER_BVISNULL( &op->o_csn ))
2087 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
2089 /* FIXME: op->o_csn is assumed to be
2090 * on the thread's slab; this needs
2091 * to be cleared ASAP.
2092 * What happens if already present?
2094 assert( BER_BVISNULL( &op->o_csn ) );
2095 op->o_csn = a->a_vals[0];
2100 if ( BER_BVISNULL( &dni.dn ) ) {
2102 op->o_req_dn = entry->e_name;
2103 op->o_req_ndn = entry->e_nname;
2104 op->o_tag = LDAP_REQ_ADD;
2106 op->o_bd = si->si_wbe;
2108 rc = op->o_bd->be_add( op, &rs_add );
2109 Debug( LDAP_DEBUG_SYNC,
2110 "syncrepl_entry: %s be_add (%d)\n",
2111 si->si_ridtxt, rc, 0 );
2112 switch ( rs_add.sr_err ) {
2114 if ( op->ora_e == entry ) {
2115 be_entry_release_w( op, entry );
2121 /* we assume that LDAP_NO_SUCH_OBJECT is returned
2122 * only if the suffix entry is not present */
2123 case LDAP_NO_SUCH_OBJECT:
2124 rc = syncrepl_add_glue( op, entry );
2128 /* if an entry was added via syncrepl_add_glue(),
2129 * it likely has no entryUUID, so the previous
2130 * be_search() doesn't find it. In this case,
2131 * give syncrepl a chance to modify it. Also
2132 * allow for entries that were recreated with the
2133 * same DN but a different entryUUID.
2135 case LDAP_ALREADY_EXISTS:
2137 Operation op2 = *op;
2138 SlapReply rs2 = { 0 };
2139 slap_callback cb2 = { 0 };
2142 op2.o_tag = LDAP_REQ_SEARCH;
2143 op2.o_req_dn = entry->e_name;
2144 op2.o_req_ndn = entry->e_nname;
2145 op2.ors_scope = LDAP_SCOPE_BASE;
2146 op2.ors_deref = LDAP_DEREF_NEVER;
2147 op2.ors_attrs = slap_anlist_all_attributes;
2148 op2.ors_attrsonly = 0;
2149 op2.ors_limit = NULL;
2151 op2.ors_tlimit = SLAP_NO_LIMIT;
2153 f.f_choice = LDAP_FILTER_PRESENT;
2154 f.f_desc = slap_schema.si_ad_objectClass;
2155 op2.ors_filter = &f;
2156 op2.ors_filterstr = generic_filterstr;
2158 op2.o_callback = &cb2;
2159 cb2.sc_response = dn_callback;
2160 cb2.sc_private = &dni;
2162 rc = be->be_search( &op2, &rs2 );
2163 if ( rc ) goto done;
2166 slap_op_time( &op->o_time, &op->o_tincr );
2172 Debug( LDAP_DEBUG_ANY,
2173 "syncrepl_entry: %s be_add failed (%d)\n",
2174 si->si_ridtxt, rs_add.sr_err, 0 );
2181 op->o_req_dn = dni.dn;
2182 op->o_req_ndn = dni.ndn;
2183 if ( dni.renamed ) {
2184 struct berval noldp, newp, nnewp;
2186 op->o_tag = LDAP_REQ_MODRDN;
2187 dnRdn( &entry->e_name, &op->orr_newrdn );
2188 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
2190 dnParent( &dni.ndn, &noldp );
2191 dnParent( &entry->e_nname, &nnewp );
2192 if ( !dn_match( &noldp, &nnewp ) ) {
2193 dnParent( &entry->e_name, &newp );
2194 op->orr_newSup = &newp;
2195 op->orr_nnewSup = &nnewp;
2197 op->orr_newSup = NULL;
2198 op->orr_nnewSup = NULL;
2200 op->orr_deleteoldrdn = dni.delOldRDN;
2201 op->orr_modlist = NULL;
2202 if ( ( rc = slap_modrdn2mods( op, &rs_modify ) ) ) {
2206 /* RDNs must be NUL-terminated for back-ldap */
2207 noldp = op->orr_newrdn;
2208 ber_dupbv_x( &op->orr_newrdn, &noldp, op->o_tmpmemctx );
2209 noldp = op->orr_nnewrdn;
2210 ber_dupbv_x( &op->orr_nnewrdn, &noldp, op->o_tmpmemctx );
2212 /* Setup opattrs too */
2214 static AttributeDescription *nullattr = NULL;
2215 static AttributeDescription **const opattrs[] = {
2216 &slap_schema.si_ad_entryCSN,
2217 &slap_schema.si_ad_modifiersName,
2218 &slap_schema.si_ad_modifyTimestamp,
2221 AttributeDescription *opattr;
2222 Modifications *mod, **modtail, **ml;
2225 for ( mod = op->orr_modlist;
2227 mod = mod->sml_next )
2229 modtail = &mod->sml_next;
2231 /* pull mod off incoming modlist, append to orr_modlist */
2232 for ( i = 0; (opattr = *opattrs[i]) != NULL; i++ ) {
2233 for ( ml = modlist; *ml; ml = &(*ml)->sml_next )
2235 if ( (*ml)->sml_desc == opattr ) {
2237 *ml = mod->sml_next;
2238 mod->sml_next = NULL;
2240 modtail = &mod->sml_next;
2246 op->o_bd = si->si_wbe;
2247 rc = op->o_bd->be_modrdn( op, &rs_modify );
2248 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
2249 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
2251 slap_mods_free( op->orr_modlist, 1 );
2252 Debug( LDAP_DEBUG_SYNC,
2253 "syncrepl_entry: %s be_modrdn (%d)\n",
2254 si->si_ridtxt, rc, 0 );
2259 op->o_tag = LDAP_REQ_MODIFY;
2260 op->orm_modlist = dni.mods;
2261 op->orm_no_opattrs = 1;
2262 op->o_bd = si->si_wbe;
2264 rc = op->o_bd->be_modify( op, &rs_modify );
2265 slap_mods_free( op->orm_modlist, 1 );
2266 op->orm_no_opattrs = 0;
2267 Debug( LDAP_DEBUG_SYNC,
2268 "syncrepl_entry: %s be_modify (%d)\n",
2269 si->si_ridtxt, rc, 0 );
2270 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
2271 Debug( LDAP_DEBUG_ANY,
2272 "syncrepl_entry: %s be_modify failed (%d)\n",
2273 si->si_ridtxt, rs_modify.sr_err, 0 );
2277 Debug( LDAP_DEBUG_SYNC,
2278 "syncrepl_entry: %s entry unchanged, ignored (%s)\n",
2279 si->si_ridtxt, op->o_req_dn.bv_val, 0 );
2282 case LDAP_SYNC_DELETE :
2283 if ( !BER_BVISNULL( &dni.dn ) ) {
2284 op->o_req_dn = dni.dn;
2285 op->o_req_ndn = dni.ndn;
2286 op->o_tag = LDAP_REQ_DELETE;
2287 op->o_bd = si->si_wbe;
2288 rc = op->o_bd->be_delete( op, &rs_delete );
2289 Debug( LDAP_DEBUG_SYNC,
2290 "syncrepl_entry: %s be_delete (%d)\n",
2291 si->si_ridtxt, rc, 0 );
2293 while ( rs_delete.sr_err == LDAP_SUCCESS
2294 && op->o_delete_glue_parent ) {
2295 op->o_delete_glue_parent = 0;
2296 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
2297 slap_callback cb = { NULL };
2298 cb.sc_response = slap_null_cb;
2299 dnParent( &op->o_req_ndn, &pdn );
2301 op->o_req_ndn = pdn;
2302 op->o_callback = &cb;
2303 op->o_bd->be_delete( op, &rs_delete );
2313 Debug( LDAP_DEBUG_ANY,
2314 "syncrepl_entry: %s unknown syncstate\n", si->si_ridtxt, 0, 0 );
2319 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
2320 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
2321 BER_BVZERO( &syncUUID_strrep );
2323 if ( !BER_BVISNULL( &dni.ndn ) ) {
2324 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
2326 if ( !BER_BVISNULL( &dni.dn ) ) {
2327 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
2330 entry_free( entry );
2333 slap_graduate_commit_csn( op );
2335 if ( !BER_BVISNULL( &op->o_csn ) && freecsn ) {
2336 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2338 BER_BVZERO( &op->o_csn );
2342 static struct berval gcbva[] = {
2348 #define NP_DELETE_ONE 2
2351 syncrepl_del_nonpresent(
2355 struct sync_cookie *sc,
2358 Backend* be = op->o_bd;
2359 slap_callback cb = { NULL };
2360 SlapReply rs_search = {REP_RESULT};
2361 SlapReply rs_delete = {REP_RESULT};
2362 SlapReply rs_modify = {REP_RESULT};
2363 struct nonpresent_entry *np_list, *np_prev;
2365 AttributeName an[2];
2367 struct berval pdn = BER_BVNULL;
2370 op->o_req_dn = si->si_base;
2371 op->o_req_ndn = si->si_base;
2373 cb.sc_response = nonpresent_callback;
2376 op->o_callback = &cb;
2377 op->o_tag = LDAP_REQ_SEARCH;
2378 op->ors_scope = si->si_scope;
2379 op->ors_deref = LDAP_DEREF_NEVER;
2380 op->o_time = slap_get_time();
2381 op->ors_tlimit = SLAP_NO_LIMIT;
2386 AttributeAssertion eq = ATTRIBUTEASSERTION_INIT;
2389 op->ors_attrsonly = 1;
2390 op->ors_attrs = slap_anlist_no_attrs;
2391 op->ors_limit = NULL;
2392 op->ors_filter = &uf;
2395 uf.f_av_desc = slap_schema.si_ad_entryUUID;
2397 uf.f_choice = LDAP_FILTER_EQUALITY;
2398 si->si_refreshDelete |= NP_DELETE_ONE;
2400 for (i=0; uuids[i].bv_val; i++) {
2402 uf.f_av_value = uuids[i];
2403 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2404 rc = be->be_search( op, &rs_search );
2405 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2407 si->si_refreshDelete ^= NP_DELETE_ONE;
2411 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2412 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2413 an[0].an_desc = slap_schema.si_ad_entryUUID;
2415 op->ors_slimit = SLAP_NO_LIMIT;
2416 op->ors_attrsonly = 0;
2417 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
2418 /* In multimaster, updates can continue to arrive while
2419 * we're searching. Limit the search result to entries
2420 * older than all of our cookie CSNs.
2422 if ( SLAP_MULTIMASTER( op->o_bd )) {
2425 cf = op->o_tmpalloc( (sc->numcsns+1) * sizeof(Filter) +
2426 sc->numcsns * sizeof(AttributeAssertion), op->o_tmpmemctx );
2428 f->f_choice = LDAP_FILTER_AND;
2431 for ( i=0; i<sc->numcsns; i++ ) {
2433 f->f_choice = LDAP_FILTER_LE;
2434 f->f_ava = (AttributeAssertion *)(f+1);
2435 f->f_av_desc = slap_schema.si_ad_entryCSN;
2436 f->f_av_value = sc->ctxcsn[i];
2437 f->f_next = (Filter *)(f->f_ava+1);
2440 f->f_next = op->ors_filter;
2441 of = op->ors_filter;
2442 op->ors_filter = cf;
2443 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2446 op->ors_filterstr = si->si_filterstr;
2448 op->o_nocaching = 1;
2450 if ( limits_check( op, &rs_search ) == 0 ) {
2451 rc = be->be_search( op, &rs_search );
2453 if ( SLAP_MULTIMASTER( op->o_bd )) {
2454 op->o_tmpfree( cf, op->o_tmpmemctx );
2455 op->ors_filter = of;
2457 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
2461 op->o_nocaching = 0;
2463 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2465 if ( sc->ctxcsn && !BER_BVISNULL( &sc->ctxcsn[m] ) ) {
2466 csn = sc->ctxcsn[m];
2468 csn = si->si_syncCookie.ctxcsn[0];
2471 op->o_bd = si->si_wbe;
2472 slap_queue_csn( op, &csn );
2474 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2475 while ( np_list != NULL ) {
2476 LDAP_LIST_REMOVE( np_list, npe_link );
2478 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2479 op->o_tag = LDAP_REQ_DELETE;
2480 op->o_callback = &cb;
2481 cb.sc_response = null_callback;
2483 op->o_req_dn = *np_prev->npe_name;
2484 op->o_req_ndn = *np_prev->npe_nname;
2485 rc = op->o_bd->be_delete( op, &rs_delete );
2486 Debug( LDAP_DEBUG_SYNC,
2487 "syncrepl_del_nonpresent: %s be_delete %s (%d)\n",
2488 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2490 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2491 Modifications mod1, mod2;
2492 mod1.sml_op = LDAP_MOD_REPLACE;
2494 mod1.sml_desc = slap_schema.si_ad_objectClass;
2495 mod1.sml_type = mod1.sml_desc->ad_cname;
2496 mod1.sml_numvals = 2;
2497 mod1.sml_values = &gcbva[0];
2498 mod1.sml_nvalues = NULL;
2499 mod1.sml_next = &mod2;
2501 mod2.sml_op = LDAP_MOD_REPLACE;
2503 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2504 mod2.sml_type = mod2.sml_desc->ad_cname;
2505 mod2.sml_numvals = 1;
2506 mod2.sml_values = &gcbva[1];
2507 mod2.sml_nvalues = NULL;
2508 mod2.sml_next = NULL;
2510 op->o_tag = LDAP_REQ_MODIFY;
2511 op->orm_modlist = &mod1;
2513 rc = op->o_bd->be_modify( op, &rs_modify );
2514 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
2517 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2518 op->o_delete_glue_parent ) {
2519 op->o_delete_glue_parent = 0;
2520 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
2521 slap_callback cb = { NULL };
2522 cb.sc_response = slap_null_cb;
2523 dnParent( &op->o_req_ndn, &pdn );
2525 op->o_req_ndn = pdn;
2526 op->o_callback = &cb;
2527 /* give it a root privil ? */
2528 op->o_bd->be_delete( op, &rs_delete );
2534 op->o_delete_glue_parent = 0;
2536 ber_bvfree( np_prev->npe_name );
2537 ber_bvfree( np_prev->npe_nname );
2540 if ( slapd_shutdown ) {
2545 slap_graduate_commit_csn( op );
2548 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2549 BER_BVZERO( &op->o_csn );
2560 Backend *be = op->o_bd;
2561 slap_callback cb = { NULL };
2566 struct berval dn = BER_BVNULL;
2567 struct berval ndn = BER_BVNULL;
2569 SlapReply rs_add = {REP_RESULT};
2570 struct berval ptr, nptr;
2573 op->o_tag = LDAP_REQ_ADD;
2574 op->o_callback = &cb;
2575 cb.sc_response = null_callback;
2576 cb.sc_private = NULL;
2581 /* count RDNs in suffix */
2582 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2583 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
2585 ptr.bv_len -= comma - ptr.bv_val;
2595 /* Start with BE suffix */
2597 for ( i = 0; i < suffrdns; i++ ) {
2598 comma = ber_bvrchr( &ptr, ',' );
2599 if ( comma != NULL ) {
2600 ptr.bv_len = comma - ptr.bv_val;
2607 if ( !BER_BVISEMPTY( &ptr ) ) {
2608 dn.bv_len -= ptr.bv_len + 1;
2609 dn.bv_val += ptr.bv_len + 1;
2612 /* the normalizedDNs are always the same length, no counting
2616 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2617 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2618 ndn.bv_len = be->be_nsuffix[0].bv_len;
2620 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
2626 while ( ndn.bv_val > e->e_nname.bv_val ) {
2627 glue = entry_alloc();
2628 ber_dupbv( &glue->e_name, &dn );
2629 ber_dupbv( &glue->e_nname, &ndn );
2631 a = attr_alloc( slap_schema.si_ad_objectClass );
2634 a->a_vals = ch_calloc( 3, sizeof( struct berval ) );
2635 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2636 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2637 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2639 a->a_nvals = a->a_vals;
2641 a->a_next = glue->e_attrs;
2644 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
2647 a->a_vals = ch_calloc( 2, sizeof( struct berval ) );
2648 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2649 ber_dupbv( &a->a_vals[1], &gcbva[2] );
2651 a->a_nvals = a->a_vals;
2653 a->a_next = glue->e_attrs;
2656 op->o_req_dn = glue->e_name;
2657 op->o_req_ndn = glue->e_nname;
2659 rc = be->be_add ( op, &rs_add );
2660 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2661 if ( op->ora_e == glue )
2662 be_entry_release_w( op, glue );
2664 /* incl. ALREADY EXIST */
2666 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
2672 /* Move to next child */
2673 comma = ber_bvrchr( &ptr, ',' );
2674 if ( comma == NULL ) {
2677 ptr.bv_len = comma - ptr.bv_val;
2679 dn.bv_val = ++comma;
2680 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
2682 comma = ber_bvrchr( &nptr, ',' );
2683 assert( comma != NULL );
2684 nptr.bv_len = comma - nptr.bv_val;
2686 ndn.bv_val = ++comma;
2687 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
2690 op->o_req_dn = e->e_name;
2691 op->o_req_ndn = e->e_nname;
2693 rc = be->be_add ( op, &rs_add );
2694 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2695 if ( op->ora_e == e )
2696 be_entry_release_w( op, e );
2705 syncrepl_updateCookie(
2709 struct sync_cookie *syncCookie )
2711 Backend *be = op->o_bd;
2713 struct berval first = BER_BVNULL;
2717 slap_callback cb = { NULL };
2718 SlapReply rs_modify = {REP_RESULT};
2720 mod.sml_op = LDAP_MOD_REPLACE;
2721 mod.sml_desc = slap_schema.si_ad_contextCSN;
2722 mod.sml_type = mod.sml_desc->ad_cname;
2723 mod.sml_nvalues = NULL;
2724 mod.sml_next = NULL;
2726 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
2728 /* clone the cookieState CSNs so we can Replace the whole thing */
2729 mod.sml_numvals = si->si_cookieState->cs_num;
2730 mod.sml_values = op->o_tmpalloc(( mod.sml_numvals+1 )*sizeof(struct berval), op->o_tmpmemctx );
2731 for ( i=0; i<mod.sml_numvals; i++ )
2732 mod.sml_values[i] = si->si_cookieState->cs_vals[i];
2733 BER_BVZERO( &mod.sml_values[i] );
2735 /* find any CSNs in the syncCookie that are newer than the cookieState */
2736 for ( i=0; i<syncCookie->numcsns; i++ ) {
2737 for ( j=0; j<si->si_cookieState->cs_num; j++ ) {
2738 if ( syncCookie->sids[i] != si->si_cookieState->cs_sids[j] )
2740 len = syncCookie->ctxcsn[i].bv_len;
2741 if ( len > si->si_cookieState->cs_vals[j].bv_len )
2742 len = si->si_cookieState->cs_vals[j].bv_len;
2743 if ( memcmp( syncCookie->ctxcsn[i].bv_val,
2744 si->si_cookieState->cs_vals[j].bv_val, len ) > 0 ) {
2745 mod.sml_values[j] = syncCookie->ctxcsn[i];
2746 if ( BER_BVISNULL( &first ))
2747 first = syncCookie->ctxcsn[i];
2751 /* there was no match for this SID, it's a new CSN */
2752 if ( j == si->si_cookieState->cs_num ) {
2753 mod.sml_values = op->o_tmprealloc( mod.sml_values,
2754 ( mod.sml_numvals+2 )*sizeof(struct berval), op->o_tmpmemctx );
2755 mod.sml_values[mod.sml_numvals++] = syncCookie->ctxcsn[i];
2756 BER_BVZERO( &mod.sml_values[mod.sml_numvals] );
2757 if ( BER_BVISNULL( &first ))
2758 first = syncCookie->ctxcsn[i];
2761 /* Should never happen, ITS#5065 */
2762 if ( BER_BVISNULL( &first )) {
2763 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
2764 op->o_tmpfree( mod.sml_values, op->o_tmpmemctx );
2767 op->o_bd = si->si_wbe;
2768 slap_queue_csn( op, &first );
2770 op->o_tag = LDAP_REQ_MODIFY;
2772 cb.sc_response = null_callback;
2775 op->o_callback = &cb;
2776 op->o_req_dn = op->o_bd->be_suffix[0];
2777 op->o_req_ndn = op->o_bd->be_nsuffix[0];
2779 /* update contextCSN */
2780 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
2782 op->orm_modlist = &mod;
2783 op->orm_no_opattrs = 1;
2784 rc = op->o_bd->be_modify( op, &rs_modify );
2785 op->orm_no_opattrs = 0;
2788 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
2789 slap_sync_cookie_free( &si->si_syncCookie, 0 );
2790 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
2791 /* If we replaced any old values */
2792 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
2793 if ( mod.sml_values[i].bv_val != si->si_cookieState->cs_vals[i].bv_val )
2794 ber_bvreplace( &si->si_cookieState->cs_vals[i],
2795 &mod.sml_values[i] );
2797 /* Handle any added values */
2798 if ( i < mod.sml_numvals ) {
2799 si->si_cookieState->cs_num = mod.sml_numvals;
2800 value_add( &si->si_cookieState->cs_vals, &mod.sml_values[i] );
2801 free( si->si_cookieState->cs_sids );
2802 si->si_cookieState->cs_sids = slap_parse_csn_sids(
2803 si->si_cookieState->cs_vals, si->si_cookieState->cs_num, NULL );
2806 si->si_cookieState->cs_age++;
2807 si->si_cookieAge = si->si_cookieState->cs_age;
2809 Debug( LDAP_DEBUG_ANY,
2810 "syncrepl_updateCookie: %s be_modify failed (%d)\n",
2811 si->si_ridtxt, rs_modify.sr_err, 0 );
2813 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
2816 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2817 BER_BVZERO( &op->o_csn );
2818 if ( mod.sml_next ) slap_mods_free( mod.sml_next, 1 );
2819 op->o_tmpfree( mod.sml_values, op->o_tmpmemctx );
2825 attr_cmp( Operation *op, Attribute *old, Attribute *new,
2826 Modifications ***mret, Modifications ***mcur )
2829 Modifications *mod, **modtail;
2835 struct berval **adds, **dels;
2836 /* count old and new */
2837 for ( o=0; old->a_vals[o].bv_val; o++ ) ;
2838 for ( n=0; new->a_vals[n].bv_val; n++ ) ;
2840 /* there MUST be both old and new values */
2845 adds = op->o_tmpalloc( sizeof(struct berval *) * n, op->o_tmpmemctx );
2846 dels = op->o_tmpalloc( sizeof(struct berval *) * o, op->o_tmpmemctx );
2848 for ( i=0; i<o; i++ ) dels[i] = &old->a_vals[i];
2849 for ( i=0; i<n; i++ ) adds[i] = &new->a_vals[i];
2853 for ( i=0; i<o; i++ ) {
2854 for ( j=0; j<n; j++ ) {
2857 if ( bvmatch( dels[i], adds[j] ) ) {
2868 /* all old values were deleted, just use the replace op */
2872 /* delete some values */
2873 mod = ch_malloc( sizeof( Modifications ) );
2874 mod->sml_op = LDAP_MOD_DELETE;
2876 mod->sml_desc = old->a_desc;
2877 mod->sml_type = mod->sml_desc->ad_cname;
2878 mod->sml_numvals = no;
2879 mod->sml_values = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
2880 if ( old->a_vals != old->a_nvals ) {
2881 mod->sml_nvalues = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
2883 mod->sml_nvalues = NULL;
2886 for ( i = 0; i < o; i++ ) {
2887 if ( !dels[i] ) continue;
2888 ber_dupbv( &mod->sml_values[j], &old->a_vals[i] );
2889 if ( mod->sml_nvalues ) {
2890 ber_dupbv( &mod->sml_nvalues[j], &old->a_nvals[i] );
2894 BER_BVZERO( &mod->sml_values[j] );
2895 if ( mod->sml_nvalues ) {
2896 BER_BVZERO( &mod->sml_nvalues[j] );
2899 modtail = &mod->sml_next;
2902 op->o_tmpfree( dels, op->o_tmpmemctx );
2903 /* some values were added */
2904 if ( nn && no < o ) {
2905 mod = ch_malloc( sizeof( Modifications ) );
2906 mod->sml_op = LDAP_MOD_ADD;
2908 mod->sml_desc = old->a_desc;
2909 mod->sml_type = mod->sml_desc->ad_cname;
2910 mod->sml_numvals = nn;
2911 mod->sml_values = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
2912 if ( old->a_vals != old->a_nvals ) {
2913 mod->sml_nvalues = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
2915 mod->sml_nvalues = NULL;
2918 for ( i = 0; i < n; i++ ) {
2919 if ( !adds[i] ) continue;
2920 ber_dupbv( &mod->sml_values[j], &new->a_vals[i] );
2921 if ( mod->sml_nvalues ) {
2922 ber_dupbv( &mod->sml_nvalues[j], &new->a_nvals[i] );
2926 BER_BVZERO( &mod->sml_values[j] );
2927 if ( mod->sml_nvalues ) {
2928 BER_BVZERO( &mod->sml_nvalues[j] );
2931 modtail = &mod->sml_next;
2934 op->o_tmpfree( adds, op->o_tmpmemctx );
2936 /* new attr, just use the new mod */
2940 /* advance to next element */
2944 **mcur = mod->sml_next;
2946 modtail = &mod->sml_next;
2948 *mcur = &mod->sml_next;
2959 dninfo *dni = op->o_callback->sc_private;
2961 if ( rs->sr_type == REP_SEARCH ) {
2962 if ( !BER_BVISNULL( &dni->dn ) ) {
2963 Debug( LDAP_DEBUG_ANY,
2964 "dn_callback : consistency error - "
2965 "entryUUID is not unique\n", 0, 0, 0 );
2967 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
2968 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
2969 /* If there is a new entry, see if it differs from the old.
2970 * We compare the non-normalized values so that cosmetic changes
2971 * in the provider are always propagated.
2973 if ( dni->new_entry ) {
2974 Modifications **modtail, **ml;
2975 Attribute *old, *new;
2978 is_ctx = dn_match( &rs->sr_entry->e_nname,
2979 &op->o_bd->be_nsuffix[0] );
2981 /* Did the DN change?
2983 if ( !dn_match( &rs->sr_entry->e_name,
2984 &dni->new_entry->e_name ) )
2986 struct berval oldRDN, oldVal;
2987 AttributeDescription *ad = NULL;
2991 /* See if the oldRDN was deleted */
2992 dnRdn( &rs->sr_entry->e_nname, &oldRDN );
2993 oldVal.bv_val = strchr(oldRDN.bv_val, '=') + 1;
2994 oldVal.bv_len = oldRDN.bv_len - ( oldVal.bv_val -
2996 oldRDN.bv_len -= oldVal.bv_len + 2;
2997 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
2998 a = attr_find( dni->new_entry->e_attrs, ad );
2999 if ( !a || attr_valfind( a,
3000 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH |
3001 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
3002 SLAP_MR_VALUE_OF_SYNTAX,
3003 &oldVal, NULL, op->o_tmpmemctx ) != LDAP_SUCCESS )
3007 /* OK, this was just a modDN, we're done */
3008 return LDAP_SUCCESS;
3011 modtail = &dni->mods;
3014 /* Make sure new entry is actually newer than old entry */
3015 old = attr_find( rs->sr_entry->e_attrs,
3016 slap_schema.si_ad_entryCSN );
3017 new = attr_find( dni->new_entry->e_attrs,
3018 slap_schema.si_ad_entryCSN );
3020 int rc, len = old->a_vals[0].bv_len;
3021 if ( len > new->a_vals[0].bv_len )
3022 len = new->a_vals[0].bv_len;
3023 rc = memcmp( old->a_vals[0].bv_val,
3024 new->a_vals[0].bv_val, len );
3026 Debug( LDAP_DEBUG_SYNC,
3027 "dn_callback : new entry is older than ours "
3028 "%s ours %s, new %s\n",
3029 rs->sr_entry->e_name.bv_val,
3030 old->a_vals[0].bv_val,
3031 new->a_vals[0].bv_val );
3032 return LDAP_SUCCESS;
3033 } else if ( rc == 0 ) {
3034 Debug( LDAP_DEBUG_SYNC,
3035 "dn_callback : entries have identical CSN "
3037 rs->sr_entry->e_name.bv_val,
3038 old->a_vals[0].bv_val, 0 );
3039 return LDAP_SUCCESS;
3043 /* We assume that attributes are saved in the same order
3044 * in the remote and local databases. So if we walk through
3045 * the attributeDescriptions one by one they should match in
3046 * lock step. If not, look for an add or delete.
3048 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
3051 /* If we've seen this before, use its mod now */
3052 if ( new->a_flags & SLAP_ATTR_IXADD ) {
3053 attr_cmp( op, NULL, new, &modtail, &ml );
3057 /* Skip contextCSN */
3058 if ( is_ctx && old->a_desc ==
3059 slap_schema.si_ad_contextCSN ) {
3064 if ( old->a_desc != new->a_desc ) {
3068 /* If it's just been re-added later,
3069 * remember that we've seen it.
3071 tmp = attr_find( new, old->a_desc );
3073 tmp->a_flags |= SLAP_ATTR_IXADD;
3075 /* If it's a new attribute, pull it in.
3077 tmp = attr_find( old, new->a_desc );
3079 attr_cmp( op, NULL, new, &modtail, &ml );
3083 /* Delete old attr */
3084 mod = ch_malloc( sizeof( Modifications ) );
3085 mod->sml_op = LDAP_MOD_DELETE;
3087 mod->sml_desc = old->a_desc;
3088 mod->sml_type = mod->sml_desc->ad_cname;
3089 mod->sml_numvals = 0;
3090 mod->sml_values = NULL;
3091 mod->sml_nvalues = NULL;
3093 modtail = &mod->sml_next;
3098 /* kludge - always update modifiersName so that it
3099 * stays co-located with the other mod opattrs. But only
3100 * if we know there are other valid mods.
3102 if ( old->a_desc == slap_schema.si_ad_modifiersName &&
3104 attr_cmp( op, NULL, new, &modtail, &ml );
3106 attr_cmp( op, old, new, &modtail, &ml );
3114 } else if ( rs->sr_type == REP_RESULT ) {
3115 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
3116 Debug( LDAP_DEBUG_ANY,
3117 "dn_callback : consistency error - "
3118 "entryUUID is not unique\n", 0, 0, 0 );
3122 return LDAP_SUCCESS;
3126 nonpresent_callback(
3130 syncinfo_t *si = op->o_callback->sc_private;
3133 struct berval* present_uuid = NULL;
3134 struct nonpresent_entry *np_entry;
3136 if ( rs->sr_type == REP_RESULT ) {
3137 count = avl_free( si->si_presentlist, ch_free );
3138 si->si_presentlist = NULL;
3140 } else if ( rs->sr_type == REP_SEARCH ) {
3141 if ( !( si->si_refreshDelete & NP_DELETE_ONE ) ) {
3142 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
3145 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
3149 if ( LogTest( LDAP_DEBUG_SYNC ) ) {
3150 char buf[sizeof("rid=999 not")];
3152 snprintf( buf, sizeof(buf), "%s %s", si->si_ridtxt,
3153 present_uuid ? "got" : "not" );
3155 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: %s UUID %s, dn %s\n",
3156 buf, a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val );
3159 if ( a == NULL ) return 0;
3162 if ( present_uuid == NULL ) {
3163 np_entry = (struct nonpresent_entry *)
3164 ch_calloc( 1, sizeof( struct nonpresent_entry ) );
3165 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
3166 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
3167 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
3170 avl_delete( &si->si_presentlist,
3171 &a->a_nvals[0], syncuuid_cmp );
3172 ch_free( present_uuid );
3175 return LDAP_SUCCESS;
3183 if ( rs->sr_err != LDAP_SUCCESS &&
3184 rs->sr_err != LDAP_REFERRAL &&
3185 rs->sr_err != LDAP_ALREADY_EXISTS &&
3186 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
3187 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
3189 Debug( LDAP_DEBUG_ANY,
3190 "null_callback : error code 0x%x\n",
3193 return LDAP_SUCCESS;
3196 static struct berval *
3197 slap_uuidstr_from_normalized(
3198 struct berval* uuidstr,
3199 struct berval* normalized,
3204 unsigned char nibble;
3207 if ( normalized == NULL ) return NULL;
3208 if ( normalized->bv_len != 16 ) return NULL;
3213 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
3214 if ( new == NULL ) {
3221 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
3222 if ( new != uuidstr ) {
3223 slap_sl_free( new, ctx );
3228 for ( i = 0; i < 16; i++ ) {
3229 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
3230 new->bv_val[(i<<1)+d] = '-';
3234 nibble = (normalized->bv_val[i] >> 4) & 0xF;
3235 if ( nibble < 10 ) {
3236 new->bv_val[(i<<1)+d] = nibble + '0';
3238 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
3241 nibble = (normalized->bv_val[i]) & 0xF;
3242 if ( nibble < 10 ) {
3243 new->bv_val[(i<<1)+d+1] = nibble + '0';
3245 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
3249 new->bv_val[new->bv_len] = '\0';
3256 if ( normalized == NULL ) return NULL;
3257 if ( normalized->bv_len != 16 ) return NULL;
3263 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
3264 if ( new == NULL ) {
3271 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
3276 rc = lutil_uuidstr_from_normalized( normalized->bv_val,
3277 normalized->bv_len, new->bv_val, new->bv_len + 1 );
3281 if ( new != NULL ) {
3282 if ( new->bv_val != NULL ) {
3283 slap_sl_free( new->bv_val, ctx );
3286 if ( new != uuidstr ) {
3287 slap_sl_free( new, ctx );
3300 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
3302 const struct berval *uuid1 = v_uuid1;
3303 const struct berval *uuid2 = v_uuid2;
3304 int rc = uuid1->bv_len - uuid2->bv_len;
3305 if ( rc ) return rc;
3306 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
3310 syncinfo_free( syncinfo_t *sie, int free_all )
3312 syncinfo_t *si_next;
3314 if ( free_all && sie->si_cookieState ) {
3315 ch_free( sie->si_cookieState->cs_sids );
3316 ber_bvarray_free( sie->si_cookieState->cs_vals );
3317 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_mutex );
3318 ch_free( sie->si_cookieState );
3321 si_next = sie->si_next;
3324 if ( sie->si_conn ) {
3325 connection_client_stop( sie->si_conn );
3326 sie->si_conn = NULL;
3328 ldap_unbind_ext( sie->si_ld, NULL, NULL );
3331 /* re-fetch it, in case it was already removed */
3332 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3333 sie->si_re = ldap_pvt_runqueue_find( &slapd_rq, do_syncrepl, sie );
3335 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, sie->si_re ) )
3336 ldap_pvt_runqueue_stoptask( &slapd_rq, sie->si_re );
3337 ldap_pvt_runqueue_remove( &slapd_rq, sie->si_re );
3340 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3341 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
3343 bindconf_free( &sie->si_bindconf );
3345 if ( sie->si_filterstr.bv_val ) {
3346 ch_free( sie->si_filterstr.bv_val );
3348 if ( sie->si_logfilterstr.bv_val ) {
3349 ch_free( sie->si_logfilterstr.bv_val );
3351 if ( sie->si_base.bv_val ) {
3352 ch_free( sie->si_base.bv_val );
3354 if ( sie->si_logbase.bv_val ) {
3355 ch_free( sie->si_logbase.bv_val );
3357 if ( sie->si_attrs ) {
3359 while ( sie->si_attrs[i] != NULL ) {
3360 ch_free( sie->si_attrs[i] );
3363 ch_free( sie->si_attrs );
3365 if ( sie->si_exattrs ) {
3367 while ( sie->si_exattrs[i] != NULL ) {
3368 ch_free( sie->si_exattrs[i] );
3371 ch_free( sie->si_exattrs );
3373 if ( sie->si_anlist ) {
3375 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
3376 ch_free( sie->si_anlist[i].an_name.bv_val );
3379 ch_free( sie->si_anlist );
3381 if ( sie->si_exanlist ) {
3383 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
3384 ch_free( sie->si_exanlist[i].an_name.bv_val );
3387 ch_free( sie->si_exanlist );
3389 if ( sie->si_retryinterval ) {
3390 ch_free( sie->si_retryinterval );
3392 if ( sie->si_retrynum ) {
3393 ch_free( sie->si_retrynum );
3395 if ( sie->si_retrynum_init ) {
3396 ch_free( sie->si_retrynum_init );
3398 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
3399 if ( sie->si_presentlist ) {
3400 avl_free( sie->si_presentlist, ch_free );
3402 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist ) ) {
3403 struct nonpresent_entry* npe;
3404 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
3405 LDAP_LIST_REMOVE( npe, npe_link );
3406 if ( npe->npe_name ) {
3407 if ( npe->npe_name->bv_val ) {
3408 ch_free( npe->npe_name->bv_val );
3410 ch_free( npe->npe_name );
3412 if ( npe->npe_nname ) {
3413 if ( npe->npe_nname->bv_val ) {
3414 ch_free( npe->npe_nname->bv_val );
3416 ch_free( npe->npe_nname );
3422 } while ( free_all && si_next );
3427 /* NOTE: used & documented in slapd.conf(5) */
3429 #define PROVIDERSTR "provider"
3430 #define SCHEMASTR "schemachecking"
3431 #define FILTERSTR "filter"
3432 #define SEARCHBASESTR "searchbase"
3433 #define SCOPESTR "scope"
3434 #define ATTRSONLYSTR "attrsonly"
3435 #define ATTRSSTR "attrs"
3436 #define TYPESTR "type"
3437 #define INTERVALSTR "interval"
3438 #define RETRYSTR "retry"
3439 #define SLIMITSTR "sizelimit"
3440 #define TLIMITSTR "timelimit"
3441 #define SYNCDATASTR "syncdata"
3442 #define LOGBASESTR "logbase"
3443 #define LOGFILTERSTR "logfilter"
3445 /* FIXME: undocumented */
3446 #define EXATTRSSTR "exattrs"
3447 #define MANAGEDSAITSTR "manageDSAit"
3450 #define GOT_ID 0x0001
3451 #define GOT_PROVIDER 0x0002
3452 #define GOT_BASE 0x0004
3455 #define GOT_ALL (GOT_ID|GOT_PROVIDER|GOT_BASE)
3461 { BER_BVC("base"), LDAP_SCOPE_BASE },
3462 { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
3463 { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL }, /* OpenLDAP extension */
3464 { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
3465 { BER_BVC("subord"), LDAP_SCOPE_SUBORDINATE },
3466 { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
3467 { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
3468 { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE }, /* OpenLDAP extension */
3472 static slap_verbmasks datamodes[] = {
3473 { BER_BVC("default"), SYNCDATA_DEFAULT },
3474 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
3475 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
3480 parse_syncrepl_line(
3488 for ( i = 1; i < c->argc; i++ ) {
3489 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
3490 STRLENOF( IDSTR "=" ) ) )
3493 /* '\0' string terminator accounts for '=' */
3494 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
3495 if ( lutil_atoi( &tmp, val ) != 0 ) {
3496 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3497 "Error: parse_syncrepl_line: "
3498 "unable to parse syncrepl id \"%s\"", val );
3499 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3502 if ( tmp > SLAP_SYNC_SID_MAX || tmp < 0 ) {
3503 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3504 "Error: parse_syncrepl_line: "
3505 "syncrepl id %d is out of range [0..4095]", tmp );
3506 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3510 sprintf( si->si_ridtxt, IDSTR "=%03d", si->si_rid );
3512 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
3513 STRLENOF( PROVIDERSTR "=" ) ) )
3515 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
3516 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
3517 gots |= GOT_PROVIDER;
3518 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
3519 STRLENOF( SCHEMASTR "=" ) ) )
3521 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
3522 if ( !strncasecmp( val, "on", STRLENOF( "on" ) ) ) {
3523 si->si_schemachecking = 1;
3524 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
3525 si->si_schemachecking = 0;
3527 si->si_schemachecking = 1;
3529 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
3530 STRLENOF( FILTERSTR "=" ) ) )
3532 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
3533 if ( si->si_filterstr.bv_val )
3534 ch_free( si->si_filterstr.bv_val );
3535 ber_str2bv( val, 0, 1, &si->si_filterstr );
3536 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
3537 STRLENOF( LOGFILTERSTR "=" ) ) )
3539 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
3540 if ( si->si_logfilterstr.bv_val )
3541 ch_free( si->si_logfilterstr.bv_val );
3542 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
3543 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
3544 STRLENOF( SEARCHBASESTR "=" ) ) )
3549 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
3550 if ( si->si_base.bv_val ) {
3551 ch_free( si->si_base.bv_val );
3553 ber_str2bv( val, 0, 0, &bv );
3554 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
3555 if ( rc != LDAP_SUCCESS ) {
3556 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3557 "Invalid base DN \"%s\": %d (%s)",
3558 val, rc, ldap_err2string( rc ) );
3559 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3562 if ( !be_issubordinate( c->be, &si->si_base ) ) {
3563 ch_free( si->si_base.bv_val );
3564 BER_BVZERO( &si->si_base );
3565 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3566 "Base DN \"%s\" is not within the database naming context",
3568 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3572 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
3573 STRLENOF( LOGBASESTR "=" ) ) )
3578 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
3579 if ( si->si_logbase.bv_val ) {
3580 ch_free( si->si_logbase.bv_val );
3582 ber_str2bv( val, 0, 0, &bv );
3583 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
3584 if ( rc != LDAP_SUCCESS ) {
3585 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3586 "Invalid logbase DN \"%s\": %d (%s)",
3587 val, rc, ldap_err2string( rc ) );
3588 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3591 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
3592 STRLENOF( SCOPESTR "=" ) ) )
3595 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
3596 for ( j = 0; !BER_BVISNULL(&scopes[j].key); j++ ) {
3597 if (!strcasecmp( val, scopes[j].key.bv_val ) ) {
3598 si->si_scope = scopes[j].val;
3602 if ( BER_BVISNULL(&scopes[j].key) ) {
3603 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3604 "Error: parse_syncrepl_line: "
3605 "unknown scope \"%s\"", val);
3606 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3609 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
3610 STRLENOF( ATTRSONLYSTR ) ) )
3612 si->si_attrsonly = 1;
3613 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
3614 STRLENOF( ATTRSSTR "=" ) ) )
3616 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
3617 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
3619 attr_fname = ch_strdup( val + STRLENOF(":include:") );
3620 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
3621 if ( si->si_anlist == NULL ) {
3622 ch_free( attr_fname );
3625 si->si_anfile = attr_fname;
3627 char *str, *s, *next;
3628 const char *delimstr = " ,\t";
3629 str = ch_strdup( val );
3630 for ( s = ldap_pvt_strtok( str, delimstr, &next );
3632 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
3634 if ( strlen(s) == 1 && *s == '*' ) {
3635 si->si_allattrs = 1;
3636 val[ s - str ] = delimstr[0];
3638 if ( strlen(s) == 1 && *s == '+' ) {
3639 si->si_allopattrs = 1;
3640 val [ s - str ] = delimstr[0];
3644 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
3645 if ( si->si_anlist == NULL ) {
3649 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
3650 STRLENOF( EXATTRSSTR "=" ) ) )
3652 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
3653 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
3655 attr_fname = ch_strdup( val + STRLENOF(":include:") );
3656 si->si_exanlist = file2anlist(
3657 si->si_exanlist, attr_fname, " ,\t" );
3658 if ( si->si_exanlist == NULL ) {
3659 ch_free( attr_fname );
3662 ch_free( attr_fname );
3664 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
3665 if ( si->si_exanlist == NULL ) {
3669 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
3670 STRLENOF( TYPESTR "=" ) ) )
3672 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
3673 if ( !strncasecmp( val, "refreshOnly",
3674 STRLENOF("refreshOnly") ) )
3676 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3677 } else if ( !strncasecmp( val, "refreshAndPersist",
3678 STRLENOF("refreshAndPersist") ) )
3680 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
3681 si->si_interval = 60;
3683 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3684 "Error: parse_syncrepl_line: "
3685 "unknown sync type \"%s\"", val);
3686 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3689 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
3690 STRLENOF( INTERVALSTR "=" ) ) )
3692 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
3693 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
3694 si->si_interval = 0;
3695 } else if ( strchr( val, ':' ) != NULL ) {
3696 char *next, *ptr = val;
3699 dd = strtol( ptr, &next, 10 );
3700 if ( next == ptr || next[0] != ':' || dd < 0 ) {
3701 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3702 "Error: parse_syncrepl_line: "
3703 "invalid interval \"%s\", unable to parse days", val );
3704 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3708 hh = strtol( ptr, &next, 10 );
3709 if ( next == ptr || next[0] != ':' || hh < 0 || hh > 24 ) {
3710 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3711 "Error: parse_syncrepl_line: "
3712 "invalid interval \"%s\", unable to parse hours", val );
3713 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3717 mm = strtol( ptr, &next, 10 );
3718 if ( next == ptr || next[0] != ':' || mm < 0 || mm > 60 ) {
3719 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3720 "Error: parse_syncrepl_line: "
3721 "invalid interval \"%s\", unable to parse minutes", val );
3722 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3726 ss = strtol( ptr, &next, 10 );
3727 if ( next == ptr || next[0] != '\0' || ss < 0 || ss > 60 ) {
3728 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3729 "Error: parse_syncrepl_line: "
3730 "invalid interval \"%s\", unable to parse seconds", val );
3731 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3734 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
3738 if ( lutil_parse_time( val, &t ) != 0 ) {
3739 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3740 "Error: parse_syncrepl_line: "
3741 "invalid interval \"%s\"", val );
3742 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3745 si->si_interval = (time_t)t;
3747 if ( si->si_interval < 0 ) {
3748 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3749 "Error: parse_syncrepl_line: "
3750 "invalid interval \"%ld\"",
3751 (long) si->si_interval);
3752 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3755 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
3756 STRLENOF( RETRYSTR "=" ) ) )
3761 val = c->argv[ i ] + STRLENOF( RETRYSTR "=" );
3762 retry_list = (char **) ch_calloc( 1, sizeof( char * ) );
3763 retry_list[0] = NULL;
3765 slap_str2clist( &retry_list, val, " ,\t" );
3767 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
3770 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3771 "Error: incomplete syncrepl retry list" );
3772 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3773 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3774 ch_free( retry_list[k] );
3776 ch_free( retry_list );
3779 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ) );
3780 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ) );
3781 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ) );
3782 for ( j = 0; j < n; j++ ) {
3784 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
3785 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3786 "Error: invalid retry interval \"%s\" (#%d)",
3787 retry_list[j*2], j );
3788 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3789 /* do some cleanup */
3792 si->si_retryinterval[j] = (time_t)t;
3793 if ( *retry_list[j*2+1] == '+' ) {
3794 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
3795 si->si_retrynum[j] = RETRYNUM_FOREVER;
3799 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
3800 || si->si_retrynum_init[j] <= 0 )
3802 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3803 "Error: invalid initial retry number \"%s\" (#%d)",
3804 retry_list[j*2+1], j );
3805 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3806 /* do some cleanup */
3809 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
3810 || si->si_retrynum[j] <= 0 )
3812 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3813 "Error: invalid retry number \"%s\" (#%d)",
3814 retry_list[j*2+1], j );
3815 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3816 /* do some cleanup */
3821 si->si_retrynum_init[j] = RETRYNUM_TAIL;
3822 si->si_retrynum[j] = RETRYNUM_TAIL;
3823 si->si_retryinterval[j] = 0;
3825 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3826 ch_free( retry_list[k] );
3828 ch_free( retry_list );
3829 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
3830 STRLENOF( MANAGEDSAITSTR "=" ) ) )
3832 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
3833 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
3834 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
3836 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3837 "invalid manageDSAit value \"%s\".\n",
3839 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3842 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
3843 STRLENOF( SLIMITSTR "=") ) )
3845 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
3846 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3849 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
3850 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3851 "invalid size limit value \"%s\".\n",
3853 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3856 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
3857 STRLENOF( TLIMITSTR "=" ) ) )
3859 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
3860 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3863 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
3864 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3865 "invalid time limit value \"%s\".\n",
3867 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3870 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
3871 STRLENOF( SYNCDATASTR "=" ) ) )
3873 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
3874 si->si_syncdata = verb_to_mask( val, datamodes );
3875 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
3876 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3877 "Error: parse_syncrepl_line: "
3878 "unable to parse \"%s\"\n", c->argv[ i ] );
3879 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3884 if ( gots != GOT_ALL ) {
3885 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3886 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
3887 gots & GOT_ID ? "" : " "IDSTR,
3888 gots & GOT_PROVIDER ? "" : " "PROVIDERSTR,
3889 gots & GOT_BASE ? "" : " "SEARCHBASESTR );
3890 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3904 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
3905 snprintf( c->cr_msg, sizeof(c->cr_msg), "database %s does not support "
3906 "operations required for syncrepl", c->be->be_type );
3907 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
3910 if ( BER_BVISEMPTY( &c->be->be_rootdn ) ) {
3911 strcpy( c->cr_msg, "rootDN must be defined before syncrepl may be used" );
3912 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
3915 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
3918 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
3922 si->si_bindconf.sb_tls = SB_TLS_OFF;
3923 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
3924 si->si_schemachecking = 0;
3925 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
3926 &si->si_filterstr );
3927 si->si_base.bv_val = NULL;
3928 si->si_scope = LDAP_SCOPE_SUBTREE;
3929 si->si_attrsonly = 0;
3930 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
3931 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
3932 si->si_attrs = NULL;
3933 si->si_allattrs = 0;
3934 si->si_allopattrs = 0;
3935 si->si_exattrs = NULL;
3936 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3937 si->si_interval = 86400;
3938 si->si_retryinterval = NULL;
3939 si->si_retrynum_init = NULL;
3940 si->si_retrynum = NULL;
3941 si->si_manageDSAit = 0;
3945 si->si_presentlist = NULL;
3946 LDAP_LIST_INIT( &si->si_nonpresentlist );
3947 ldap_pvt_thread_mutex_init( &si->si_mutex );
3949 rc = parse_syncrepl_line( c, si );
3952 /* Must be LDAPv3 because we need controls */
3953 switch ( si->si_bindconf.sb_version ) {
3955 /* not explicitly set */
3956 si->si_bindconf.sb_version = LDAP_VERSION3;
3959 /* explicitly set */
3962 Debug( LDAP_DEBUG_ANY,
3963 "version %d incompatible with syncrepl\n",
3964 si->si_bindconf.sb_version, 0, 0 );
3965 syncinfo_free( si, 0 );
3970 if ( slapMode & SLAP_SERVER_MODE ) {
3971 Listener **l = slapd_get_listeners();
3974 /* check if URL points to current server. If so, ignore
3975 * this configuration. We require an exact match. Just
3976 * in case they really want to do this, they can vary
3977 * the case of the URL to allow it.
3979 if ( l && !SLAP_DBHIDDEN( c->be ) ) {
3981 for ( i=0; l[i]; i++ ) {
3982 if ( bvmatch( &l[i]->sl_url, &si->si_bindconf.sb_uri ) ) {
3990 init_syncrepl( si );
3991 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3992 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq,
3993 si->si_interval, do_syncrepl, si, "do_syncrepl",
3995 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3997 rc = config_sync_shadow( c ) ? -1 : 0;
4002 /* mirrormode still needs to see this flag in tool mode */
4003 rc = config_sync_shadow( c ) ? -1 : 0;
4008 /* Use main slapd defaults */
4009 bindconf_tls_defaults( &si->si_bindconf );
4012 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
4013 syncinfo_free( si, 0 );
4016 Debug( LDAP_DEBUG_CONFIG,
4017 "Config: ** successfully added syncrepl \"%s\"\n",
4018 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
4019 "(null)" : si->si_bindconf.sb_uri.bv_val, 0, 0 );
4020 if ( !si->si_schemachecking ) {
4021 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
4023 if ( c->be->be_syncinfo ) {
4024 si->si_cookieState = c->be->be_syncinfo->si_cookieState;
4026 si->si_cookieState = ch_calloc( 1, sizeof( cookie_state ));
4027 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_mutex );
4029 si->si_next = c->be->be_syncinfo;
4030 c->be->be_syncinfo = si;
4036 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
4038 struct berval bc, uri;
4039 char buf[BUFSIZ*2], *ptr;
4042 #define WHATSLEFT ( sizeof( buf ) - ( ptr - buf ) )
4046 /* temporarily inhibit bindconf from printing URI */
4047 uri = si->si_bindconf.sb_uri;
4048 BER_BVZERO( &si->si_bindconf.sb_uri );
4049 si->si_bindconf.sb_version = 0;
4050 bindconf_unparse( &si->si_bindconf, &bc );
4051 si->si_bindconf.sb_uri = uri;
4052 si->si_bindconf.sb_version = LDAP_VERSION3;
4055 assert( si->si_rid >= 0 && si->si_rid <= SLAP_SYNC_SID_MAX );
4056 ptr += snprintf( ptr, WHATSLEFT, IDSTR "=%03d " PROVIDERSTR "=%s",
4057 si->si_rid, si->si_bindconf.sb_uri.bv_val );
4058 if ( ptr - buf >= sizeof( buf ) ) return;
4059 if ( !BER_BVISNULL( &bc ) ) {
4060 if ( WHATSLEFT <= bc.bv_len ) {
4064 ptr = lutil_strcopy( ptr, bc.bv_val );
4067 if ( !BER_BVISEMPTY( &si->si_filterstr ) ) {
4068 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
4069 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
4070 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
4073 if ( !BER_BVISNULL( &si->si_base ) ) {
4074 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
4075 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
4076 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
4079 if ( !BER_BVISEMPTY( &si->si_logfilterstr ) ) {
4080 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
4081 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
4082 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
4085 if ( !BER_BVISNULL( &si->si_logbase ) ) {
4086 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
4087 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
4088 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
4091 for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
4092 if ( si->si_scope == scopes[i].val ) {
4093 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + scopes[i].key.bv_len ) return;
4094 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
4095 ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
4099 if ( si->si_attrsonly ) {
4100 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
4101 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
4103 if ( si->si_anfile ) {
4104 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
4105 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
4106 ptr = lutil_strcopy( ptr, si->si_anfile );
4108 } else if ( si->si_allattrs || si->si_allopattrs ||
4109 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ) )
4113 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
4114 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
4116 /* FIXME: add check for overflow */
4117 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
4118 if ( si->si_allattrs ) {
4119 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
4120 if ( old != ptr ) *ptr++ = ',';
4123 if ( si->si_allopattrs ) {
4124 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
4125 if ( old != ptr ) *ptr++ = ',';
4130 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
4131 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
4132 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
4133 /* FIXME: add check for overflow */
4134 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
4136 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
4137 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
4138 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
4140 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
4141 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
4142 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
4143 "refreshAndPersist" : "refreshOnly" );
4145 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
4148 dd = si->si_interval;
4155 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
4156 ptr += snprintf( ptr, WHATSLEFT, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
4157 if ( ptr - buf >= sizeof( buf ) ) return;
4158 } else if ( si->si_retryinterval ) {
4160 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
4161 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
4162 for (i=0; si->si_retryinterval[i]; i++) {
4163 if ( space ) *ptr++ = ' ';
4165 ptr += snprintf( ptr, WHATSLEFT, "%ld ", (long) si->si_retryinterval[i] );
4166 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
4169 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
4171 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
4175 if ( si->si_slimit ) {
4176 if ( WHATSLEFT <= STRLENOF( " " SLIMITSTR "=" ) ) return;
4177 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
4178 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_slimit );
4181 if ( si->si_tlimit ) {
4182 if ( WHATSLEFT <= STRLENOF( " " TLIMITSTR "=" ) ) return;
4183 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
4184 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_tlimit );
4187 if ( si->si_syncdata ) {
4188 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
4189 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
4190 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
4191 ptr = lutil_strcopy( ptr, bc.bv_val );
4194 bc.bv_len = ptr - buf;
4196 ber_dupbv( bv, &bc );
4200 syncrepl_config( ConfigArgs *c )
4202 if (c->op == SLAP_CONFIG_EMIT) {
4203 if ( c->be->be_syncinfo ) {
4207 for ( si = c->be->be_syncinfo; si; si=si->si_next ) {
4208 syncrepl_unparse( si, &bv );
4209 ber_bvarray_add( &c->rvalue_vals, &bv );
4214 } else if ( c->op == LDAP_MOD_DELETE ) {
4215 cookie_state *cs = NULL;
4216 if ( c->be->be_syncinfo ) {
4217 syncinfo_t *si, **sip;
4220 cs = c->be->be_syncinfo->si_cookieState;
4221 for ( sip = &c->be->be_syncinfo, i=0; *sip; i++ ) {
4223 if ( c->valx == -1 || i == c->valx ) {
4226 /* If the task is currently active, we have to leave
4227 * it running. It will exit on its own. This will only
4228 * happen when running on the cn=config DB.
4231 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4232 isrunning = ldap_pvt_runqueue_isrunning( &slapd_rq, si->si_re );
4233 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4235 if ( si->si_re && isrunning ) {
4238 syncinfo_free( si, 0 );
4247 if ( !c->be->be_syncinfo ) {
4248 SLAP_DBFLAGS( c->be ) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
4250 ber_bvarray_free( cs->cs_vals );
4251 ldap_pvt_thread_mutex_destroy( &cs->cs_mutex );
4257 if ( SLAP_SLURP_SHADOW( c->be ) ) {
4258 Debug(LDAP_DEBUG_ANY, "%s: "
4259 "syncrepl: database already shadowed.\n",
4263 return add_syncrepl( c );