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 */
572 ber_len_t ssf; /* ITS#5403, 3864 LDAP_OPT_X_SASL_SSF probably ought
573 to use sasl_ssf_t but currently uses ber_len_t */
574 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &ssf );
575 op->o_sasl_ssf = ssf;
577 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
578 ? op->o_sasl_ssf : op->o_tls_ssf;
580 ldap_set_option( si->si_ld, LDAP_OPT_TIMELIMIT, &si->si_tlimit );
582 si->si_syncCookie.rid = si->si_rid;
583 si->si_syncCookie.sid = SLAP_SINGLE_SHADOW( si->si_be ) ? -1 :
586 /* We've just started up, or the remote server hasn't sent us
587 * any meaningful state.
589 if ( BER_BVISNULL( &si->si_syncCookie.octet_str ) ) {
592 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
593 if ( si->si_rid == sc->rid ) {
594 cmdline_cookie_found = 1;
599 if ( cmdline_cookie_found ) {
600 /* cookie is supplied in the command line */
602 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
604 /* ctxcsn wasn't parsed yet, do it now */
605 slap_parse_sync_cookie( sc, op->o_tmpmemctx );
606 slap_sync_cookie_free( &si->si_syncCookie, 0 );
607 slap_dup_sync_cookie( &si->si_syncCookie, sc );
608 slap_sync_cookie_free( sc, 1 );
610 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
611 if ( !si->si_cookieState->cs_num ) {
612 /* get contextCSN shadow replica from database */
613 BerVarray csn = NULL;
614 void *ctx = op->o_tmpmemctx;
616 op->o_req_ndn = op->o_bd->be_nsuffix[0];
617 op->o_req_dn = op->o_req_ndn;
619 /* try to read stored contextCSN */
620 op->o_tmpmemctx = NULL;
621 backend_attribute( op, NULL, &op->o_req_ndn,
622 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
623 op->o_tmpmemctx = ctx;
625 si->si_cookieState->cs_vals = csn;
626 for (i=0; !BER_BVISNULL( &csn[i] ); i++);
627 si->si_cookieState->cs_num = i;
628 si->si_cookieState->cs_sids = slap_parse_csn_sids( csn, i, NULL );
631 if ( si->si_cookieState->cs_num ) {
632 ber_bvarray_free( si->si_syncCookie.ctxcsn );
633 if ( ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
634 si->si_cookieState->cs_vals, NULL )) {
636 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
639 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
640 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num *
642 for ( i=0; i<si->si_syncCookie.numcsns; i++ )
643 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
645 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
648 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
649 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
650 si->si_syncCookie.sid );
652 /* Look for contextCSN from syncprov overlay. */
653 check_syncprov( op, si );
656 si->si_refreshDone = 0;
658 rc = ldap_sync_search( si, op->o_tmpmemctx );
660 if( rc != LDAP_SUCCESS ) {
661 Debug( LDAP_DEBUG_ANY, "do_syncrep1: %s "
662 "ldap_search_ext: %s (%d)\n",
663 si->si_ridtxt, ldap_err2string( rc ), rc );
669 ldap_unbind_ext( si->si_ld, NULL, NULL );
678 compare_csns( struct sync_cookie *sc1, struct sync_cookie *sc2, int *which )
685 if ( sc1->numcsns < sc2->numcsns ) {
686 *which = sc1->numcsns;
690 for (i=0; i<sc1->numcsns; i++) {
691 for (j=0; j<sc2->numcsns; j++) {
692 if ( sc1->sids[i] != sc2->sids[j] )
694 value_match( &match, slap_schema.si_ad_entryCSN,
695 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
696 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
697 &sc1->ctxcsn[i], &sc2->ctxcsn[j], &text );
708 #define SYNC_PAUSED -3
715 LDAPControl **rctrls = NULL;
718 BerElementBuffer berbuf;
719 BerElement *ber = (BerElement *)&berbuf;
721 LDAPMessage *msg = NULL;
724 struct berval *retdata = NULL;
729 struct berval syncUUID = BER_BVNULL;
730 struct sync_cookie syncCookie = { NULL };
731 struct sync_cookie syncCookie_req = { NULL };
732 struct berval cookie = BER_BVNULL;
739 Modifications *modlist = NULL;
743 struct timeval *tout_p = NULL;
744 struct timeval tout = { 0, 0 };
746 int refreshDeletes = 0;
747 BerVarray syncUUIDs = NULL;
750 if ( slapd_shutdown ) {
755 ber_init2( ber, NULL, LBER_USE_DER );
756 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
758 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2 %s\n", si->si_ridtxt, 0, 0 );
760 psub = &si->si_be->be_nsuffix[0];
762 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
764 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
770 while ( ( rc = ldap_result( si->si_ld, si->si_msgid, LDAP_MSG_ONE,
771 tout_p, &msg ) ) > 0 )
773 if ( slapd_shutdown ) {
777 switch( ldap_msgtype( msg ) ) {
778 case LDAP_RES_SEARCH_ENTRY:
779 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
780 /* we can't work without the control */
784 /* NOTE: make sure we use the right one;
785 * a better approach would be to run thru
786 * the whole list and take care of all */
787 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_STATE, rctrls, &next );
788 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_STATE, next, NULL ) )
790 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
791 "got search entry with multiple "
792 "Sync State control\n", si->si_ridtxt, 0, 0 );
797 if ( rctrlp == NULL ) {
798 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
799 "got search entry without "
800 "Sync State control\n", si->si_ridtxt, 0, 0 );
804 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
805 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
806 /* FIXME: what if syncUUID is NULL or empty?
807 * (happens with back-sql...) */
808 if ( BER_BVISEMPTY( &syncUUID ) ) {
809 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
810 "got empty syncUUID with LDAP_SYNC_%s\n",
812 syncrepl_state2str( syncstate ), 0 );
813 ldap_controls_free( rctrls );
817 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
818 ber_scanf( ber, /*"{"*/ "m}", &cookie );
820 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: cookie=%s\n",
821 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0, 0 );
823 if ( !BER_BVISNULL( &cookie ) ) {
824 ch_free( syncCookie.octet_str.bv_val );
825 ber_dupbv( &syncCookie.octet_str, &cookie );
827 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
829 slap_parse_sync_cookie( &syncCookie, NULL );
830 if ( syncCookie.ctxcsn ) {
831 int i, sid = slap_parse_csn_sid( syncCookie.ctxcsn );
832 for ( i =0; i<si->si_cookieState->cs_num; i++ ) {
833 if ( si->si_cookieState->cs_sids[i] == sid &&
834 ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_vals[i] ) <= 0 ) {
835 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN too old, ignoring %s\n",
836 si->si_ridtxt, syncCookie.ctxcsn->bv_val, 0 );
837 ldap_controls_free( rctrls );
846 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
848 if ( ( rc = syncrepl_message_to_op( si, op, msg ) ) == LDAP_SUCCESS &&
851 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
852 } else switch ( rc ) {
853 case LDAP_ALREADY_EXISTS:
854 case LDAP_NO_SUCH_OBJECT:
855 case LDAP_NO_SUCH_ATTRIBUTE:
856 case LDAP_TYPE_OR_VALUE_EXISTS:
857 rc = LDAP_SYNC_REFRESH_REQUIRED;
858 si->si_logstate = SYNCLOG_FALLBACK;
859 ldap_abandon_ext( si->si_ld, si->si_msgid, NULL, NULL );
864 } else if ( ( rc = syncrepl_message_to_entry( si, op, msg,
865 &modlist, &entry, syncstate ) ) == LDAP_SUCCESS )
867 if ( ( rc = syncrepl_entry( si, op, entry, &modlist,
868 syncstate, &syncUUID, syncCookie.ctxcsn ) ) == LDAP_SUCCESS &&
871 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
874 ldap_controls_free( rctrls );
876 slap_mods_free( modlist, 1 );
882 case LDAP_RES_SEARCH_REFERENCE:
883 Debug( LDAP_DEBUG_ANY,
884 "do_syncrep2: %s reference received error\n",
885 si->si_ridtxt, 0, 0 );
888 case LDAP_RES_SEARCH_RESULT:
889 Debug( LDAP_DEBUG_SYNC,
890 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT\n",
891 si->si_ridtxt, 0, 0 );
892 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
894 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
895 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
896 /* map old result code to registered code */
897 err = LDAP_SYNC_REFRESH_REQUIRED;
900 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
901 if ( si->si_logstate == SYNCLOG_LOGGING ) {
902 si->si_logstate = SYNCLOG_FALLBACK;
909 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
911 ber_scanf( ber, "{" /*"}"*/);
912 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
913 ber_scanf( ber, "m", &cookie );
915 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: cookie=%s\n",
916 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0, 0 );
918 if ( !BER_BVISNULL( &cookie ) ) {
919 ch_free( syncCookie.octet_str.bv_val );
920 ber_dupbv( &syncCookie.octet_str, &cookie);
922 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
924 slap_parse_sync_cookie( &syncCookie, NULL );
927 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
929 ber_scanf( ber, "b", &refreshDeletes );
931 ber_scanf( ber, /*"{"*/ "}" );
933 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
934 slap_sync_cookie_free( &syncCookie_req, 0 );
935 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
937 if ( !syncCookie.ctxcsn ) {
939 } else if ( !syncCookie_req.ctxcsn ) {
943 match = compare_csns( &syncCookie_req, &syncCookie, &m );
946 ldap_controls_free( rctrls );
948 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
949 /* FIXME : different error behaviors according to
950 * 1) err code : LDAP_BUSY ...
951 * 2) on err policy : stop service, stop sync, retry
953 if ( refreshDeletes == 0 && match < 0 &&
954 err == LDAP_SUCCESS &&
955 syncCookie_req.numcsns == syncCookie.numcsns )
957 syncrepl_del_nonpresent( op, si, NULL,
960 avl_free( si->si_presentlist, ch_free );
961 si->si_presentlist = NULL;
964 if ( syncCookie.ctxcsn && match < 0 && err == LDAP_SUCCESS )
966 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
968 if ( err == LDAP_SUCCESS
969 && si->si_logstate == SYNCLOG_FALLBACK ) {
970 si->si_logstate = SYNCLOG_LOGGING;
971 rc = LDAP_SYNC_REFRESH_REQUIRED;
978 case LDAP_RES_INTERMEDIATE:
979 rc = ldap_parse_intermediate( si->si_ld, msg,
980 &retoid, &retdata, NULL, 0 );
981 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
982 ber_init2( ber, retdata, LBER_USE_DER );
984 switch ( si_tag = ber_peek_tag( ber, &len ) ) {
986 case LDAP_TAG_SYNC_NEW_COOKIE:
987 Debug( LDAP_DEBUG_SYNC,
988 "do_syncrep2: %s %s - %s\n",
990 "LDAP_RES_INTERMEDIATE",
992 ber_scanf( ber, "tm", &tag, &cookie );
994 case LDAP_TAG_SYNC_REFRESH_DELETE:
995 case LDAP_TAG_SYNC_REFRESH_PRESENT:
996 Debug( LDAP_DEBUG_SYNC,
997 "do_syncrep2: %s %s - %s\n",
999 "LDAP_RES_INTERMEDIATE",
1000 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
1001 "REFRESH_PRESENT" : "REFRESH_DELETE" );
1002 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
1003 si->si_refreshDelete = 1;
1005 si->si_refreshPresent = 1;
1007 ber_scanf( ber, "t{" /*"}"*/, &tag );
1008 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
1010 ber_scanf( ber, "m", &cookie );
1012 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: cookie=%s\n",
1013 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0, 0 );
1015 if ( !BER_BVISNULL( &cookie ) ) {
1016 ch_free( syncCookie.octet_str.bv_val );
1017 ber_dupbv( &syncCookie.octet_str, &cookie );
1019 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1021 slap_parse_sync_cookie( &syncCookie, NULL );
1024 /* Defaults to TRUE */
1025 if ( ber_peek_tag( ber, &len ) ==
1026 LDAP_TAG_REFRESHDONE )
1028 ber_scanf( ber, "b", &si->si_refreshDone );
1031 si->si_refreshDone = 1;
1033 ber_scanf( ber, /*"{"*/ "}" );
1035 case LDAP_TAG_SYNC_ID_SET:
1036 Debug( LDAP_DEBUG_SYNC,
1037 "do_syncrep2: %s %s - %s\n",
1039 "LDAP_RES_INTERMEDIATE",
1041 ber_scanf( ber, "t{" /*"}"*/, &tag );
1042 if ( ber_peek_tag( ber, &len ) ==
1043 LDAP_TAG_SYNC_COOKIE )
1045 ber_scanf( ber, "m", &cookie );
1047 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: cookie=%s\n",
1048 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0, 0 );
1050 if ( !BER_BVISNULL( &cookie ) ) {
1051 ch_free( syncCookie.octet_str.bv_val );
1052 ber_dupbv( &syncCookie.octet_str, &cookie );
1054 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1056 slap_parse_sync_cookie( &syncCookie, NULL );
1057 compare_csns( &syncCookie_req, &syncCookie, &m );
1060 if ( ber_peek_tag( ber, &len ) ==
1061 LDAP_TAG_REFRESHDELETES )
1063 ber_scanf( ber, "b", &refreshDeletes );
1065 ber_scanf( ber, "[W]", &syncUUIDs );
1066 ber_scanf( ber, /*"{"*/ "}" );
1067 if ( refreshDeletes ) {
1068 syncrepl_del_nonpresent( op, si, syncUUIDs,
1070 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
1073 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
1074 (void)avl_presentlist_insert( si, &syncUUIDs[i] );
1075 slap_sl_free( syncUUIDs[i].bv_val, op->o_tmpmemctx );
1077 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
1079 slap_sync_cookie_free( &syncCookie, 0 );
1082 Debug( LDAP_DEBUG_ANY,
1083 "do_syncrep2: %s unknown syncinfo tag (%ld)\n",
1084 si->si_ridtxt, (long) si_tag, 0 );
1085 ldap_memfree( retoid );
1086 ber_bvfree( retdata );
1090 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1091 slap_sync_cookie_free( &syncCookie_req, 0 );
1092 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1094 if ( !syncCookie.ctxcsn ) {
1096 } else if ( !syncCookie_req.ctxcsn ) {
1100 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1104 if ( si->si_refreshPresent == 1 &&
1105 syncCookie_req.numcsns == syncCookie.numcsns ) {
1106 syncrepl_del_nonpresent( op, si, NULL,
1110 if ( syncCookie.ctxcsn )
1112 rc = syncrepl_updateCookie( si, op, psub, &syncCookie);
1116 ldap_memfree( retoid );
1117 ber_bvfree( retdata );
1121 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1122 "unknown intermediate response (%d)\n",
1123 si->si_ridtxt, rc, 0 );
1124 ldap_memfree( retoid );
1125 ber_bvfree( retdata );
1131 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1132 "unknown message (0x%02lx)\n",
1134 (unsigned long)ldap_msgtype( msg ), 0 );
1138 if ( !BER_BVISNULL( &syncCookie.octet_str ) ) {
1139 slap_sync_cookie_free( &syncCookie_req, 0 );
1140 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
1141 slap_sync_cookie_free( &syncCookie, 0 );
1143 ldap_msgfree( msg );
1145 if ( ldap_pvt_thread_pool_pausing( &connection_pool )) {
1146 slap_sync_cookie_free( &syncCookie, 0 );
1147 slap_sync_cookie_free( &syncCookie_req, 0 );
1153 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
1158 if ( err != LDAP_SUCCESS ) {
1159 Debug( LDAP_DEBUG_ANY,
1160 "do_syncrep2: %s (%d) %s\n",
1161 si->si_ridtxt, err, ldap_err2string( err ) );
1164 slap_sync_cookie_free( &syncCookie, 0 );
1165 slap_sync_cookie_free( &syncCookie_req, 0 );
1167 if ( msg ) ldap_msgfree( msg );
1169 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1170 if ( si->si_conn ) {
1171 connection_client_stop( si->si_conn );
1174 ldap_unbind_ext( si->si_ld, NULL, NULL );
1186 struct re_s* rtask = arg;
1187 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1188 Connection conn = {0};
1189 OperationBuffer opbuf;
1191 int rc = LDAP_SUCCESS;
1192 int dostop = 0, do_setup = 0;
1194 int i, defer = 1, fail = 0;
1197 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl %s\n", si->si_ridtxt, 0, 0 );
1202 /* There will never be more than one instance active */
1203 ldap_pvt_thread_mutex_lock( &si->si_mutex );
1205 switch( abs( si->si_type ) ) {
1206 case LDAP_SYNC_REFRESH_ONLY:
1207 case LDAP_SYNC_REFRESH_AND_PERSIST:
1210 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1214 if ( slapd_shutdown ) {
1216 if ( si->si_conn ) {
1217 connection_client_stop( si->si_conn );
1220 ldap_unbind_ext( si->si_ld, NULL, NULL );
1223 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1227 connection_fake_init( &conn, &opbuf, ctx );
1230 /* use global malloc for now */
1231 op->o_tmpmemctx = NULL;
1232 op->o_tmpmfuncs = &ch_mfuncs;
1234 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1237 /* Coordinate contextCSN updates with any syncprov overlays
1238 * in use. This may be complicated by the use of the glue
1241 * Typically there is a single syncprov mastering the entire
1242 * glued tree. In that case, our contextCSN updates should
1243 * go to the master DB.
1245 * Alternatively, there may be individual syncprov overlays
1246 * on each glued branch. In that case, each syncprov only
1247 * knows about changes within its own branch. And so our
1248 * contextCSN updates should only go to the local DB.
1250 if ( !si->si_wbe ) {
1251 if ( SLAP_GLUE_SUBORDINATE( be ) && !overlay_is_inst( be, "syncprov" )) {
1252 si->si_wbe = select_backend( &be->be_nsuffix[0], 1 );
1257 if ( !si->si_schemachecking )
1258 op->o_no_schema_check = 1;
1260 /* Establish session, do search */
1262 si->si_refreshDelete = 0;
1263 si->si_refreshPresent = 0;
1265 /* use main DB when retrieving contextCSN */
1266 op->o_bd = si->si_wbe;
1267 op->o_dn = op->o_bd->be_rootdn;
1268 op->o_ndn = op->o_bd->be_rootndn;
1269 rc = do_syncrep1( op, si );
1273 /* Process results */
1274 if ( rc == LDAP_SUCCESS ) {
1275 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1277 /* use current DB */
1279 op->o_dn = op->o_bd->be_rootdn;
1280 op->o_ndn = op->o_bd->be_rootndn;
1281 rc = do_syncrep2( op, si );
1282 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1283 rc = ldap_sync_search( si, op->o_tmpmemctx );
1287 /* We got deleted while running on cn=config */
1288 if ( !si->si_ctype ) {
1294 if ( rc != SYNC_PAUSED ) {
1295 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1296 /* If we succeeded, enable the connection for further listening.
1297 * If we failed, tear down the connection and reschedule.
1299 if ( rc == LDAP_SUCCESS ) {
1300 if ( si->si_conn ) {
1301 connection_client_enable( si->si_conn );
1306 } else if ( si->si_conn ) {
1310 if ( rc == -2 ) rc = 0;
1315 /* At this point, we have 5 cases:
1316 * 1) for any hard failure, give up and remove this task
1317 * 2) for ServerDown, reschedule this task to run later
1318 * 3) for threadpool pause, reschedule to run immediately
1319 * 4) for Refresh and Success, reschedule to run
1320 * 5) for Persist and Success, reschedule to defer
1322 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1324 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask ) ) {
1325 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1329 connection_client_stop( si->si_conn );
1333 if ( rc == SYNC_PAUSED ) {
1334 rtask->interval.tv_sec = 0;
1335 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1337 } else if ( rc == LDAP_SUCCESS ) {
1338 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1341 rtask->interval.tv_sec = si->si_interval;
1342 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1343 if ( si->si_retrynum ) {
1344 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1345 si->si_retrynum[i] = si->si_retrynum_init[i];
1347 si->si_retrynum[i] = RETRYNUM_TAIL;
1350 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1351 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1356 || !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1357 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1358 fail = RETRYNUM_TAIL;
1359 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1360 if ( si->si_retrynum[i] > 0 )
1361 si->si_retrynum[i]--;
1362 fail = si->si_retrynum[i];
1363 rtask->interval.tv_sec = si->si_retryinterval[i];
1364 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1365 slap_wake_listener();
1369 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1372 si->si_conn = connection_client_setup( s, do_syncrepl, arg );
1375 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1378 if ( fail == RETRYNUM_TAIL ) {
1379 Debug( LDAP_DEBUG_ANY,
1380 "do_syncrepl: %s quitting\n",
1381 si->si_ridtxt, 0, 0 );
1382 } else if ( fail > 0 ) {
1383 Debug( LDAP_DEBUG_ANY,
1384 "do_syncrepl: %s retrying (%d retries left)\n",
1385 si->si_ridtxt, fail, 0 );
1387 Debug( LDAP_DEBUG_ANY,
1388 "do_syncrepl: %s retrying\n",
1389 si->si_ridtxt, 0, 0 );
1393 /* Do final delete cleanup */
1394 if ( !si->si_ctype ) {
1395 cookie_state *cs = NULL;
1398 cs = be->be_syncinfo->si_cookieState;
1399 for ( sip = &be->be_syncinfo; *sip != si; sip = &(*sip)->si_next );
1401 syncinfo_free( si, 0 );
1402 if ( !be->be_syncinfo ) {
1403 SLAP_DBFLAGS( be ) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
1405 ch_free( cs->cs_sids );
1406 ber_bvarray_free( cs->cs_vals );
1407 ldap_pvt_thread_mutex_destroy( &cs->cs_mutex );
1415 static slap_verbmasks modops[] = {
1416 { BER_BVC("add"), LDAP_REQ_ADD },
1417 { BER_BVC("delete"), LDAP_REQ_DELETE },
1418 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1419 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1423 static Modifications *
1424 syncrepl_accesslog_mods(
1431 AttributeDescription *ad;
1432 struct berval bv, bv2;
1434 Modifications *mod = NULL, *modlist = NULL, **modtail;
1439 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1443 colon = ber_bvchr( &bv, ':' );
1449 bv.bv_len = colon - bv.bv_val;
1450 if ( slap_bv2ad( &bv, &ad, &text ) ) {
1455 /* Ignore dynamically generated attrs */
1456 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1460 /* Ignore excluded attrs */
1461 if ( ldap_charray_inlist( si->si_exattrs,
1462 ad->ad_type->sat_cname.bv_val ) )
1468 case '+': op = LDAP_MOD_ADD; break;
1469 case '-': op = LDAP_MOD_DELETE; break;
1470 case '=': op = LDAP_MOD_REPLACE; break;
1471 case '#': op = LDAP_MOD_INCREMENT; break;
1475 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1476 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1479 mod->sml_next = NULL;
1481 mod->sml_type = ad->ad_cname;
1482 mod->sml_values = NULL;
1483 mod->sml_nvalues = NULL;
1484 mod->sml_numvals = 0;
1487 modtail = &mod->sml_next;
1489 if ( colon[2] == ' ' ) {
1490 bv.bv_val = colon + 3;
1491 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1492 ber_dupbv( &bv2, &bv );
1493 ber_bvarray_add( &mod->sml_values, &bv2 );
1500 static Modifications *
1501 syncrepl_changelog_mods(
1506 return NULL; /* FIXME */
1510 syncrepl_message_to_op(
1516 BerElement *ber = NULL;
1517 Modifications *modlist = NULL;
1519 SlapReply rs = { REP_RESULT };
1520 slap_callback cb = { NULL, null_callback, NULL, NULL };
1523 char txtbuf[SLAP_TEXT_BUFLEN];
1524 size_t textlen = sizeof txtbuf;
1526 struct berval bdn, dn = BER_BVNULL, ndn;
1527 struct berval bv, *bvals = NULL;
1528 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1529 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1530 psup = BER_BVNULL, nsup = BER_BVNULL;
1531 int rc, deleteOldRdn = 0, freeReqDn = 0;
1533 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1534 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1535 "Message type should be entry (%d)",
1536 si->si_ridtxt, ldap_msgtype( msg ), 0 );
1540 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1545 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1547 if ( rc != LDAP_SUCCESS ) {
1548 Debug( LDAP_DEBUG_ANY,
1549 "syncrepl_message_to_op: %s dn get failed (%d)",
1550 si->si_ridtxt, rc, 0 );
1554 op->o_tag = LBER_DEFAULT;
1555 op->o_bd = si->si_wbe;
1557 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ) )
1559 if ( bv.bv_val == NULL )
1562 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn ) ) {
1564 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1565 ber_dupbv( &op->o_req_dn, &dn );
1566 ber_dupbv( &op->o_req_ndn, &ndn );
1567 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1568 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1570 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req ) ) {
1571 int i = verb_to_mask( bvals[0].bv_val, modops );
1573 Debug( LDAP_DEBUG_ANY,
1574 "syncrepl_message_to_op: %s unknown op %s",
1575 si->si_ridtxt, bvals[0].bv_val, 0 );
1580 op->o_tag = modops[i].mask;
1581 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod ) ) {
1582 /* Parse attribute into modlist */
1583 if ( si->si_syncdata == SYNCDATA_ACCESSLOG ) {
1584 modlist = syncrepl_accesslog_mods( si, bvals );
1586 modlist = syncrepl_changelog_mods( si, bvals );
1588 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn ) ) {
1590 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn ) ) {
1591 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ) ) {
1594 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup ) ) {
1596 } else if ( !ber_bvstrcasecmp( &bv,
1597 &slap_schema.si_ad_entryCSN->ad_cname ) )
1599 slap_queue_csn( op, bvals );
1604 /* If we didn't get a mod type or a target DN, bail out */
1605 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn ) ) {
1610 op->o_callback = &cb;
1611 slap_op_time( &op->o_time, &op->o_tincr );
1613 switch( op->o_tag ) {
1615 case LDAP_REQ_MODIFY:
1616 /* If we didn't get required data, bail */
1617 if ( !modlist ) goto done;
1619 rc = slap_mods_check( op, modlist, &text, txtbuf, textlen, NULL );
1621 if ( rc != LDAP_SUCCESS ) {
1622 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1623 "mods check (%s)\n",
1624 si->si_ridtxt, text, 0 );
1628 if ( op->o_tag == LDAP_REQ_ADD ) {
1629 Entry *e = entry_alloc();
1631 op->ora_e->e_name = op->o_req_dn;
1632 op->ora_e->e_nname = op->o_req_ndn;
1634 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1635 if( rc != LDAP_SUCCESS ) {
1636 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1637 "mods2entry (%s)\n",
1638 si->si_ridtxt, text, 0 );
1640 rc = op->o_bd->be_add( op, &rs );
1641 Debug( LDAP_DEBUG_SYNC,
1642 "syncrepl_message_to_op: %s be_add %s (%d)\n",
1643 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1645 if ( e == op->ora_e )
1646 be_entry_release_w( op, op->ora_e );
1648 op->orm_modlist = modlist;
1649 op->o_bd = si->si_wbe;
1650 rc = op->o_bd->be_modify( op, &rs );
1651 modlist = op->orm_modlist;
1652 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1653 "syncrepl_message_to_op: %s be_modify %s (%d)\n",
1654 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1655 op->o_bd = si->si_be;
1658 case LDAP_REQ_MODRDN:
1659 if ( BER_BVISNULL( &rdn ) ) goto done;
1661 if ( rdnPretty( NULL, &rdn, &prdn, NULL ) ) {
1664 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ) ) {
1667 if ( !BER_BVISNULL( &sup ) ) {
1668 if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ) ) {
1671 op->orr_newSup = &psup;
1672 op->orr_nnewSup = ⊅
1674 op->orr_newSup = NULL;
1675 op->orr_nnewSup = NULL;
1677 op->orr_newrdn = prdn;
1678 op->orr_nnewrdn = nrdn;
1679 op->orr_deleteoldrdn = deleteOldRdn;
1680 op->orr_modlist = NULL;
1681 if ( slap_modrdn2mods( op, &rs ) ) {
1685 /* Append modlist for operational attrs */
1689 for ( m = op->orr_modlist; m->sml_next; m = m->sml_next )
1691 m->sml_next = modlist;
1694 rc = op->o_bd->be_modrdn( op, &rs );
1695 slap_mods_free( op->orr_modlist, 1 );
1696 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1697 "syncrepl_message_to_op: %s be_modrdn %s (%d)\n",
1698 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1700 case LDAP_REQ_DELETE:
1701 rc = op->o_bd->be_delete( op, &rs );
1702 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1703 "syncrepl_message_to_op: %s be_delete %s (%d)\n",
1704 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1708 slap_graduate_commit_csn( op );
1709 op->o_bd = si->si_be;
1710 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
1711 BER_BVZERO( &op->o_csn );
1713 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1715 if ( !BER_BVISNULL( &rdn ) ) {
1716 if ( !BER_BVISNULL( &nsup ) ) {
1717 ch_free( nsup.bv_val );
1719 if ( !BER_BVISNULL( &psup ) ) {
1720 ch_free( psup.bv_val );
1722 if ( !BER_BVISNULL( &nrdn ) ) {
1723 ch_free( nrdn.bv_val );
1725 if ( !BER_BVISNULL( &prdn ) ) {
1726 ch_free( prdn.bv_val );
1730 ch_free( op->o_req_ndn.bv_val );
1731 ch_free( op->o_req_dn.bv_val );
1738 syncrepl_message_to_entry(
1742 Modifications **modlist,
1748 BerElement *ber = NULL;
1751 Modifications **modtail = modlist;
1754 char txtbuf[SLAP_TEXT_BUFLEN];
1755 size_t textlen = sizeof txtbuf;
1757 struct berval bdn = BER_BVNULL, dn, ndn;
1762 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1763 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s "
1764 "Message type should be entry (%d)",
1765 si->si_ridtxt, ldap_msgtype( msg ), 0 );
1769 op->o_tag = LDAP_REQ_ADD;
1771 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1772 if ( rc != LDAP_SUCCESS ) {
1773 Debug( LDAP_DEBUG_ANY,
1774 "syncrepl_message_to_entry: %s dn get failed (%d)",
1775 si->si_ridtxt, rc, 0 );
1779 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1780 /* NOTE: this could be done even before decoding the DN,
1781 * although encoding errors wouldn't be detected */
1786 if ( entry == NULL ) {
1790 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1791 ber_dupbv( &op->o_req_dn, &dn );
1792 ber_dupbv( &op->o_req_ndn, &ndn );
1793 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1794 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1796 is_ctx = dn_match( &op->o_req_ndn, &op->o_bd->be_nsuffix[0] );
1799 e->e_name = op->o_req_dn;
1800 e->e_nname = op->o_req_ndn;
1802 while ( ber_remaining( ber ) ) {
1803 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1804 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1809 /* Drop all updates to the contextCSN of the context entry
1812 if ( is_ctx && !strcasecmp( tmp.sml_type.bv_val,
1813 slap_schema.si_ad_contextCSN->ad_cname.bv_val )) {
1814 ber_bvarray_free( tmp.sml_values );
1818 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1820 mod->sml_op = LDAP_MOD_REPLACE;
1822 mod->sml_next = NULL;
1823 mod->sml_desc = NULL;
1824 mod->sml_type = tmp.sml_type;
1825 mod->sml_values = tmp.sml_values;
1826 mod->sml_nvalues = NULL;
1827 mod->sml_numvals = 0; /* slap_mods_check will set this */
1830 modtail = &mod->sml_next;
1833 if ( *modlist == NULL ) {
1834 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s no attributes\n",
1835 si->si_ridtxt, 0, 0 );
1840 rc = slap_mods_check( op, *modlist, &text, txtbuf, textlen, NULL );
1842 if ( rc != LDAP_SUCCESS ) {
1843 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods check (%s)\n",
1844 si->si_ridtxt, text, 0 );
1848 /* Strip out dynamically generated attrs */
1849 for ( modtail = modlist; *modtail ; ) {
1851 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1852 *modtail = mod->sml_next;
1853 slap_mod_free( &mod->sml_mod, 0 );
1856 modtail = &mod->sml_next;
1860 /* Strip out attrs in exattrs list */
1861 for ( modtail = modlist; *modtail ; ) {
1863 if ( ldap_charray_inlist( si->si_exattrs,
1864 mod->sml_desc->ad_type->sat_cname.bv_val ) )
1866 *modtail = mod->sml_next;
1867 slap_mod_free( &mod->sml_mod, 0 );
1870 modtail = &mod->sml_next;
1874 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1875 if( rc != LDAP_SUCCESS ) {
1876 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods2entry (%s)\n",
1877 si->si_ridtxt, text, 0 );
1882 if ( rc != LDAP_SUCCESS ) {
1894 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1896 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1897 * entry if a previous refresh was interrupted before sending us a new
1898 * context state. We try to compare the new entry to the existing entry
1899 * and ignore the new entry if they are the same.
1901 * Also, we may get an update where the entryDN has changed, due to
1902 * a ModDn on the provider. We detect this as well, so we can issue
1903 * the corresponding operation locally.
1905 * In the case of a modify, we get a list of all the attributes
1906 * in the original entry. Rather than deleting the entry and re-adding it,
1907 * we issue a Modify request that deletes all the attributes and adds all
1908 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1911 * We otherwise distinguish ModDN from Modify; in the case of
1912 * a ModDN we just use the CSN, modifyTimestamp and modifiersName
1913 * operational attributes from the entry, and do a regular ModDN.
1915 typedef struct dninfo {
1919 int renamed; /* Was an existing entry renamed? */
1920 int delOldRDN; /* Was old RDN deleted? */
1921 Modifications **modlist; /* the modlist we received */
1922 Modifications *mods; /* the modlist we compared */
1925 /* return 1 if inserted, 0 otherwise */
1927 avl_presentlist_insert(
1929 struct berval *syncUUID )
1931 struct berval *syncuuid_bv = ch_malloc( sizeof( struct berval ) + syncUUID->bv_len + 1 );
1933 syncuuid_bv->bv_len = syncUUID->bv_len;
1934 syncuuid_bv->bv_val = (char *)&syncuuid_bv[1];
1935 AC_MEMCPY( syncuuid_bv->bv_val, syncUUID->bv_val, syncUUID->bv_len );
1936 syncuuid_bv->bv_val[ syncuuid_bv->bv_len ] = '\0';
1938 if ( avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1939 syncuuid_cmp, avl_dup_error ) )
1941 ch_free( syncuuid_bv );
1953 Modifications** modlist,
1955 struct berval* syncUUID,
1956 struct berval* syncCSN )
1958 Backend *be = op->o_bd;
1959 slap_callback cb = { NULL, NULL, NULL, NULL };
1960 int syncuuid_inserted = 0;
1961 struct berval syncUUID_strrep = BER_BVNULL;
1963 SlapReply rs_search = {REP_RESULT};
1964 SlapReply rs_delete = {REP_RESULT};
1965 SlapReply rs_add = {REP_RESULT};
1966 SlapReply rs_modify = {REP_RESULT};
1968 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
1969 int rc = LDAP_SUCCESS;
1971 struct berval pdn = BER_BVNULL;
1976 Debug( LDAP_DEBUG_SYNC,
1977 "syncrepl_entry: %s LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_%s)\n",
1978 si->si_ridtxt, syncrepl_state2str( syncstate ), 0 );
1980 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ) ) {
1981 if ( !si->si_refreshPresent && !si->si_refreshDone ) {
1982 syncuuid_inserted = avl_presentlist_insert( si, syncUUID );
1986 if ( syncstate == LDAP_SYNC_PRESENT ) {
1988 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1989 if ( entry == NULL ) {
1994 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1995 if ( syncstate != LDAP_SYNC_DELETE ) {
1996 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1999 /* add if missing */
2000 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
2001 &syncUUID_strrep, syncUUID );
2003 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
2004 /* replace only if necessary */
2005 if ( a->a_nvals != a->a_vals ) {
2006 ber_memfree( a->a_nvals[0].bv_val );
2007 ber_dupbv( &a->a_nvals[0], syncUUID );
2009 ber_memfree( a->a_vals[0].bv_val );
2010 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
2014 f.f_choice = LDAP_FILTER_EQUALITY;
2016 ava.aa_desc = slap_schema.si_ad_entryUUID;
2017 ava.aa_value = *syncUUID;
2019 if ( syncuuid_inserted ) {
2020 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s inserted UUID %s\n",
2021 si->si_ridtxt, syncUUID_strrep.bv_val, 0 );
2023 op->ors_filter = &f;
2025 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID_strrep.bv_len;
2026 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
2027 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
2028 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
2029 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
2030 syncUUID_strrep.bv_val, syncUUID_strrep.bv_len );
2031 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
2032 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
2034 op->o_tag = LDAP_REQ_SEARCH;
2035 op->ors_scope = LDAP_SCOPE_SUBTREE;
2036 op->ors_deref = LDAP_DEREF_NEVER;
2038 /* get the entry for this UUID */
2039 op->o_req_dn = si->si_base;
2040 op->o_req_ndn = si->si_base;
2042 op->o_time = slap_get_time();
2043 op->ors_tlimit = SLAP_NO_LIMIT;
2046 op->ors_attrs = slap_anlist_all_attributes;
2047 op->ors_attrsonly = 0;
2049 /* set callback function */
2050 op->o_callback = &cb;
2051 cb.sc_response = dn_callback;
2052 cb.sc_private = &dni;
2053 dni.new_entry = entry;
2054 dni.modlist = modlist;
2056 if ( limits_check( op, &rs_search ) == 0 ) {
2057 rc = be->be_search( op, &rs_search );
2058 Debug( LDAP_DEBUG_SYNC,
2059 "syncrepl_entry: %s be_search (%d)\n",
2060 si->si_ridtxt, rc, 0 );
2063 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
2064 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2067 cb.sc_response = null_callback;
2070 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
2071 Debug( LDAP_DEBUG_SYNC,
2072 "syncrepl_entry: %s %s\n",
2073 si->si_ridtxt, entry->e_name.bv_val, 0 );
2075 Debug( LDAP_DEBUG_SYNC,
2076 "syncrepl_entry: %s %s\n",
2077 si->si_ridtxt, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
2080 assert( BER_BVISNULL( &op->o_csn ) );
2082 slap_queue_csn( op, syncCSN );
2085 slap_op_time( &op->o_time, &op->o_tincr );
2086 switch ( syncstate ) {
2088 case LDAP_SYNC_MODIFY:
2089 if ( BER_BVISNULL( &op->o_csn ))
2092 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
2094 /* FIXME: op->o_csn is assumed to be
2095 * on the thread's slab; this needs
2096 * to be cleared ASAP.
2097 * What happens if already present?
2099 assert( BER_BVISNULL( &op->o_csn ) );
2100 op->o_csn = a->a_vals[0];
2105 if ( BER_BVISNULL( &dni.dn ) ) {
2107 op->o_req_dn = entry->e_name;
2108 op->o_req_ndn = entry->e_nname;
2109 op->o_tag = LDAP_REQ_ADD;
2111 op->o_bd = si->si_wbe;
2113 rc = op->o_bd->be_add( op, &rs_add );
2114 Debug( LDAP_DEBUG_SYNC,
2115 "syncrepl_entry: %s be_add (%d)\n",
2116 si->si_ridtxt, rc, 0 );
2117 switch ( rs_add.sr_err ) {
2119 if ( op->ora_e == entry ) {
2120 be_entry_release_w( op, entry );
2126 /* we assume that LDAP_NO_SUCH_OBJECT is returned
2127 * only if the suffix entry is not present */
2128 case LDAP_NO_SUCH_OBJECT:
2129 rc = syncrepl_add_glue( op, entry );
2133 /* if an entry was added via syncrepl_add_glue(),
2134 * it likely has no entryUUID, so the previous
2135 * be_search() doesn't find it. In this case,
2136 * give syncrepl a chance to modify it. Also
2137 * allow for entries that were recreated with the
2138 * same DN but a different entryUUID.
2140 case LDAP_ALREADY_EXISTS:
2142 Operation op2 = *op;
2143 SlapReply rs2 = { 0 };
2144 slap_callback cb2 = { 0 };
2147 op2.o_tag = LDAP_REQ_SEARCH;
2148 op2.o_req_dn = entry->e_name;
2149 op2.o_req_ndn = entry->e_nname;
2150 op2.ors_scope = LDAP_SCOPE_BASE;
2151 op2.ors_deref = LDAP_DEREF_NEVER;
2152 op2.ors_attrs = slap_anlist_all_attributes;
2153 op2.ors_attrsonly = 0;
2154 op2.ors_limit = NULL;
2156 op2.ors_tlimit = SLAP_NO_LIMIT;
2158 f.f_choice = LDAP_FILTER_PRESENT;
2159 f.f_desc = slap_schema.si_ad_objectClass;
2160 op2.ors_filter = &f;
2161 op2.ors_filterstr = generic_filterstr;
2163 op2.o_callback = &cb2;
2164 cb2.sc_response = dn_callback;
2165 cb2.sc_private = &dni;
2167 rc = be->be_search( &op2, &rs2 );
2168 if ( rc ) goto done;
2171 slap_op_time( &op->o_time, &op->o_tincr );
2177 Debug( LDAP_DEBUG_ANY,
2178 "syncrepl_entry: %s be_add failed (%d)\n",
2179 si->si_ridtxt, rs_add.sr_err, 0 );
2186 op->o_req_dn = dni.dn;
2187 op->o_req_ndn = dni.ndn;
2188 if ( dni.renamed ) {
2189 struct berval noldp, newp, nnewp;
2191 op->o_tag = LDAP_REQ_MODRDN;
2192 dnRdn( &entry->e_name, &op->orr_newrdn );
2193 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
2195 dnParent( &dni.ndn, &noldp );
2196 dnParent( &entry->e_nname, &nnewp );
2197 if ( !dn_match( &noldp, &nnewp ) ) {
2198 dnParent( &entry->e_name, &newp );
2199 op->orr_newSup = &newp;
2200 op->orr_nnewSup = &nnewp;
2202 op->orr_newSup = NULL;
2203 op->orr_nnewSup = NULL;
2205 op->orr_deleteoldrdn = dni.delOldRDN;
2206 op->orr_modlist = NULL;
2207 if ( ( rc = slap_modrdn2mods( op, &rs_modify ) ) ) {
2211 /* RDNs must be NUL-terminated for back-ldap */
2212 noldp = op->orr_newrdn;
2213 ber_dupbv_x( &op->orr_newrdn, &noldp, op->o_tmpmemctx );
2214 noldp = op->orr_nnewrdn;
2215 ber_dupbv_x( &op->orr_nnewrdn, &noldp, op->o_tmpmemctx );
2217 /* Setup opattrs too */
2219 static AttributeDescription *nullattr = NULL;
2220 static AttributeDescription **const opattrs[] = {
2221 &slap_schema.si_ad_entryCSN,
2222 &slap_schema.si_ad_modifiersName,
2223 &slap_schema.si_ad_modifyTimestamp,
2226 AttributeDescription *opattr;
2227 Modifications *mod, **modtail, **ml;
2230 for ( mod = op->orr_modlist;
2232 mod = mod->sml_next )
2234 modtail = &mod->sml_next;
2236 /* pull mod off incoming modlist, append to orr_modlist */
2237 for ( i = 0; (opattr = *opattrs[i]) != NULL; i++ ) {
2238 for ( ml = modlist; *ml; ml = &(*ml)->sml_next )
2240 if ( (*ml)->sml_desc == opattr ) {
2242 *ml = mod->sml_next;
2243 mod->sml_next = NULL;
2245 modtail = &mod->sml_next;
2251 op->o_bd = si->si_wbe;
2252 rc = op->o_bd->be_modrdn( op, &rs_modify );
2253 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
2254 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
2256 slap_mods_free( op->orr_modlist, 1 );
2257 Debug( LDAP_DEBUG_SYNC,
2258 "syncrepl_entry: %s be_modrdn (%d)\n",
2259 si->si_ridtxt, rc, 0 );
2264 op->o_tag = LDAP_REQ_MODIFY;
2265 op->orm_modlist = dni.mods;
2266 op->orm_no_opattrs = 1;
2267 op->o_bd = si->si_wbe;
2269 rc = op->o_bd->be_modify( op, &rs_modify );
2270 slap_mods_free( op->orm_modlist, 1 );
2271 op->orm_no_opattrs = 0;
2272 Debug( LDAP_DEBUG_SYNC,
2273 "syncrepl_entry: %s be_modify (%d)\n",
2274 si->si_ridtxt, rc, 0 );
2275 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
2276 Debug( LDAP_DEBUG_ANY,
2277 "syncrepl_entry: %s be_modify failed (%d)\n",
2278 si->si_ridtxt, rs_modify.sr_err, 0 );
2282 Debug( LDAP_DEBUG_SYNC,
2283 "syncrepl_entry: %s entry unchanged, ignored (%s)\n",
2284 si->si_ridtxt, op->o_req_dn.bv_val, 0 );
2287 case LDAP_SYNC_DELETE :
2288 if ( !BER_BVISNULL( &dni.dn ) ) {
2289 op->o_req_dn = dni.dn;
2290 op->o_req_ndn = dni.ndn;
2291 op->o_tag = LDAP_REQ_DELETE;
2292 op->o_bd = si->si_wbe;
2293 rc = op->o_bd->be_delete( op, &rs_delete );
2294 Debug( LDAP_DEBUG_SYNC,
2295 "syncrepl_entry: %s be_delete (%d)\n",
2296 si->si_ridtxt, rc, 0 );
2298 while ( rs_delete.sr_err == LDAP_SUCCESS
2299 && op->o_delete_glue_parent ) {
2300 op->o_delete_glue_parent = 0;
2301 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
2302 slap_callback cb = { NULL };
2303 cb.sc_response = slap_null_cb;
2304 dnParent( &op->o_req_ndn, &pdn );
2306 op->o_req_ndn = pdn;
2307 op->o_callback = &cb;
2308 op->o_bd->be_delete( op, &rs_delete );
2318 Debug( LDAP_DEBUG_ANY,
2319 "syncrepl_entry: %s unknown syncstate\n", si->si_ridtxt, 0, 0 );
2324 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
2325 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
2326 BER_BVZERO( &syncUUID_strrep );
2328 if ( !BER_BVISNULL( &dni.ndn ) ) {
2329 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
2331 if ( !BER_BVISNULL( &dni.dn ) ) {
2332 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
2335 entry_free( entry );
2338 slap_graduate_commit_csn( op );
2340 if ( !BER_BVISNULL( &op->o_csn ) && freecsn ) {
2341 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2343 BER_BVZERO( &op->o_csn );
2347 static struct berval gcbva[] = {
2353 #define NP_DELETE_ONE 2
2356 syncrepl_del_nonpresent(
2360 struct sync_cookie *sc,
2363 Backend* be = op->o_bd;
2364 slap_callback cb = { NULL };
2365 SlapReply rs_search = {REP_RESULT};
2366 SlapReply rs_delete = {REP_RESULT};
2367 SlapReply rs_modify = {REP_RESULT};
2368 struct nonpresent_entry *np_list, *np_prev;
2370 AttributeName an[2];
2372 struct berval pdn = BER_BVNULL;
2375 op->o_req_dn = si->si_base;
2376 op->o_req_ndn = si->si_base;
2378 cb.sc_response = nonpresent_callback;
2381 op->o_callback = &cb;
2382 op->o_tag = LDAP_REQ_SEARCH;
2383 op->ors_scope = si->si_scope;
2384 op->ors_deref = LDAP_DEREF_NEVER;
2385 op->o_time = slap_get_time();
2386 op->ors_tlimit = SLAP_NO_LIMIT;
2391 AttributeAssertion eq = ATTRIBUTEASSERTION_INIT;
2394 op->ors_attrsonly = 1;
2395 op->ors_attrs = slap_anlist_no_attrs;
2396 op->ors_limit = NULL;
2397 op->ors_filter = &uf;
2400 uf.f_av_desc = slap_schema.si_ad_entryUUID;
2402 uf.f_choice = LDAP_FILTER_EQUALITY;
2403 si->si_refreshDelete |= NP_DELETE_ONE;
2405 for (i=0; uuids[i].bv_val; i++) {
2407 uf.f_av_value = uuids[i];
2408 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2409 rc = be->be_search( op, &rs_search );
2410 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2412 si->si_refreshDelete ^= NP_DELETE_ONE;
2416 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2417 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2418 an[0].an_desc = slap_schema.si_ad_entryUUID;
2420 op->ors_slimit = SLAP_NO_LIMIT;
2421 op->ors_attrsonly = 0;
2422 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
2423 /* In multimaster, updates can continue to arrive while
2424 * we're searching. Limit the search result to entries
2425 * older than all of our cookie CSNs.
2427 if ( SLAP_MULTIMASTER( op->o_bd )) {
2430 cf = op->o_tmpalloc( (sc->numcsns+1) * sizeof(Filter) +
2431 sc->numcsns * sizeof(AttributeAssertion), op->o_tmpmemctx );
2433 f->f_choice = LDAP_FILTER_AND;
2437 for ( i=0; i<sc->numcsns; i++ ) {
2439 f->f_choice = LDAP_FILTER_LE;
2440 f->f_ava = (AttributeAssertion *)(f+1);
2441 f->f_av_desc = slap_schema.si_ad_entryCSN;
2442 f->f_av_value = sc->ctxcsn[i];
2443 f->f_next = (Filter *)(f->f_ava+1);
2446 f->f_next = op->ors_filter;
2447 of = op->ors_filter;
2448 op->ors_filter = cf;
2449 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2452 op->ors_filterstr = si->si_filterstr;
2454 op->o_nocaching = 1;
2456 if ( limits_check( op, &rs_search ) == 0 ) {
2457 rc = be->be_search( op, &rs_search );
2459 if ( SLAP_MULTIMASTER( op->o_bd )) {
2460 op->o_tmpfree( cf, op->o_tmpmemctx );
2461 op->ors_filter = of;
2463 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
2467 op->o_nocaching = 0;
2469 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2471 if ( sc->ctxcsn && !BER_BVISNULL( &sc->ctxcsn[m] ) ) {
2472 csn = sc->ctxcsn[m];
2474 csn = si->si_syncCookie.ctxcsn[0];
2477 op->o_bd = si->si_wbe;
2478 slap_queue_csn( op, &csn );
2480 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2481 while ( np_list != NULL ) {
2482 LDAP_LIST_REMOVE( np_list, npe_link );
2484 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2485 op->o_tag = LDAP_REQ_DELETE;
2486 op->o_callback = &cb;
2487 cb.sc_response = null_callback;
2489 op->o_req_dn = *np_prev->npe_name;
2490 op->o_req_ndn = *np_prev->npe_nname;
2491 rc = op->o_bd->be_delete( op, &rs_delete );
2492 Debug( LDAP_DEBUG_SYNC,
2493 "syncrepl_del_nonpresent: %s be_delete %s (%d)\n",
2494 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2496 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2497 Modifications mod1, mod2;
2498 mod1.sml_op = LDAP_MOD_REPLACE;
2500 mod1.sml_desc = slap_schema.si_ad_objectClass;
2501 mod1.sml_type = mod1.sml_desc->ad_cname;
2502 mod1.sml_numvals = 2;
2503 mod1.sml_values = &gcbva[0];
2504 mod1.sml_nvalues = NULL;
2505 mod1.sml_next = &mod2;
2507 mod2.sml_op = LDAP_MOD_REPLACE;
2509 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2510 mod2.sml_type = mod2.sml_desc->ad_cname;
2511 mod2.sml_numvals = 1;
2512 mod2.sml_values = &gcbva[1];
2513 mod2.sml_nvalues = NULL;
2514 mod2.sml_next = NULL;
2516 op->o_tag = LDAP_REQ_MODIFY;
2517 op->orm_modlist = &mod1;
2519 rc = op->o_bd->be_modify( op, &rs_modify );
2520 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
2523 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2524 op->o_delete_glue_parent ) {
2525 op->o_delete_glue_parent = 0;
2526 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
2527 slap_callback cb = { NULL };
2528 cb.sc_response = slap_null_cb;
2529 dnParent( &op->o_req_ndn, &pdn );
2531 op->o_req_ndn = pdn;
2532 op->o_callback = &cb;
2533 /* give it a root privil ? */
2534 op->o_bd->be_delete( op, &rs_delete );
2540 op->o_delete_glue_parent = 0;
2542 ber_bvfree( np_prev->npe_name );
2543 ber_bvfree( np_prev->npe_nname );
2546 if ( slapd_shutdown ) {
2551 slap_graduate_commit_csn( op );
2554 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2555 BER_BVZERO( &op->o_csn );
2566 Backend *be = op->o_bd;
2567 slap_callback cb = { NULL };
2572 struct berval dn = BER_BVNULL;
2573 struct berval ndn = BER_BVNULL;
2575 SlapReply rs_add = {REP_RESULT};
2576 struct berval ptr, nptr;
2579 op->o_tag = LDAP_REQ_ADD;
2580 op->o_callback = &cb;
2581 cb.sc_response = null_callback;
2582 cb.sc_private = NULL;
2587 /* count RDNs in suffix */
2588 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2589 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
2591 ptr.bv_len -= comma - ptr.bv_val;
2601 /* Start with BE suffix */
2603 for ( i = 0; i < suffrdns; i++ ) {
2604 comma = ber_bvrchr( &ptr, ',' );
2605 if ( comma != NULL ) {
2606 ptr.bv_len = comma - ptr.bv_val;
2613 if ( !BER_BVISEMPTY( &ptr ) ) {
2614 dn.bv_len -= ptr.bv_len + 1;
2615 dn.bv_val += ptr.bv_len + 1;
2618 /* the normalizedDNs are always the same length, no counting
2622 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2623 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2624 ndn.bv_len = be->be_nsuffix[0].bv_len;
2626 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
2632 while ( ndn.bv_val > e->e_nname.bv_val ) {
2633 glue = entry_alloc();
2634 ber_dupbv( &glue->e_name, &dn );
2635 ber_dupbv( &glue->e_nname, &ndn );
2637 a = attr_alloc( slap_schema.si_ad_objectClass );
2640 a->a_vals = ch_calloc( 3, sizeof( struct berval ) );
2641 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2642 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2643 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2645 a->a_nvals = a->a_vals;
2647 a->a_next = glue->e_attrs;
2650 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
2653 a->a_vals = ch_calloc( 2, sizeof( struct berval ) );
2654 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2655 ber_dupbv( &a->a_vals[1], &gcbva[2] );
2657 a->a_nvals = a->a_vals;
2659 a->a_next = glue->e_attrs;
2662 op->o_req_dn = glue->e_name;
2663 op->o_req_ndn = glue->e_nname;
2665 rc = be->be_add ( op, &rs_add );
2666 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2667 if ( op->ora_e == glue )
2668 be_entry_release_w( op, glue );
2670 /* incl. ALREADY EXIST */
2672 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
2678 /* Move to next child */
2679 comma = ber_bvrchr( &ptr, ',' );
2680 if ( comma == NULL ) {
2683 ptr.bv_len = comma - ptr.bv_val;
2685 dn.bv_val = ++comma;
2686 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
2688 comma = ber_bvrchr( &nptr, ',' );
2689 assert( comma != NULL );
2690 nptr.bv_len = comma - nptr.bv_val;
2692 ndn.bv_val = ++comma;
2693 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
2696 op->o_req_dn = e->e_name;
2697 op->o_req_ndn = e->e_nname;
2699 rc = be->be_add ( op, &rs_add );
2700 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2701 if ( op->ora_e == e )
2702 be_entry_release_w( op, e );
2711 syncrepl_updateCookie(
2715 struct sync_cookie *syncCookie )
2717 Backend *be = op->o_bd;
2719 struct berval first = BER_BVNULL;
2723 slap_callback cb = { NULL };
2724 SlapReply rs_modify = {REP_RESULT};
2726 mod.sml_op = LDAP_MOD_REPLACE;
2727 mod.sml_desc = slap_schema.si_ad_contextCSN;
2728 mod.sml_type = mod.sml_desc->ad_cname;
2729 mod.sml_nvalues = NULL;
2730 mod.sml_next = NULL;
2732 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
2734 /* clone the cookieState CSNs so we can Replace the whole thing */
2735 mod.sml_numvals = si->si_cookieState->cs_num;
2736 mod.sml_values = op->o_tmpalloc(( mod.sml_numvals+1 )*sizeof(struct berval), op->o_tmpmemctx );
2737 for ( i=0; i<mod.sml_numvals; i++ )
2738 mod.sml_values[i] = si->si_cookieState->cs_vals[i];
2739 BER_BVZERO( &mod.sml_values[i] );
2741 /* find any CSNs in the syncCookie that are newer than the cookieState */
2742 for ( i=0; i<syncCookie->numcsns; i++ ) {
2743 for ( j=0; j<si->si_cookieState->cs_num; j++ ) {
2744 if ( syncCookie->sids[i] != si->si_cookieState->cs_sids[j] )
2746 len = syncCookie->ctxcsn[i].bv_len;
2747 if ( len > si->si_cookieState->cs_vals[j].bv_len )
2748 len = si->si_cookieState->cs_vals[j].bv_len;
2749 if ( memcmp( syncCookie->ctxcsn[i].bv_val,
2750 si->si_cookieState->cs_vals[j].bv_val, len ) > 0 ) {
2751 mod.sml_values[j] = syncCookie->ctxcsn[i];
2752 if ( BER_BVISNULL( &first ))
2753 first = syncCookie->ctxcsn[i];
2757 /* there was no match for this SID, it's a new CSN */
2758 if ( j == si->si_cookieState->cs_num ) {
2759 mod.sml_values = op->o_tmprealloc( mod.sml_values,
2760 ( mod.sml_numvals+2 )*sizeof(struct berval), op->o_tmpmemctx );
2761 mod.sml_values[mod.sml_numvals++] = syncCookie->ctxcsn[i];
2762 BER_BVZERO( &mod.sml_values[mod.sml_numvals] );
2763 if ( BER_BVISNULL( &first ))
2764 first = syncCookie->ctxcsn[i];
2767 /* Should never happen, ITS#5065 */
2768 if ( BER_BVISNULL( &first )) {
2769 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
2770 op->o_tmpfree( mod.sml_values, op->o_tmpmemctx );
2773 op->o_bd = si->si_wbe;
2774 slap_queue_csn( op, &first );
2776 op->o_tag = LDAP_REQ_MODIFY;
2778 cb.sc_response = null_callback;
2781 op->o_callback = &cb;
2782 op->o_req_dn = op->o_bd->be_suffix[0];
2783 op->o_req_ndn = op->o_bd->be_nsuffix[0];
2785 /* update contextCSN */
2786 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
2788 op->orm_modlist = &mod;
2789 op->orm_no_opattrs = 1;
2790 rc = op->o_bd->be_modify( op, &rs_modify );
2791 op->orm_no_opattrs = 0;
2794 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
2795 slap_sync_cookie_free( &si->si_syncCookie, 0 );
2796 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
2797 /* If we replaced any old values */
2798 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
2799 if ( mod.sml_values[i].bv_val != si->si_cookieState->cs_vals[i].bv_val )
2800 ber_bvreplace( &si->si_cookieState->cs_vals[i],
2801 &mod.sml_values[i] );
2803 /* Handle any added values */
2804 if ( i < mod.sml_numvals ) {
2805 si->si_cookieState->cs_num = mod.sml_numvals;
2806 value_add( &si->si_cookieState->cs_vals, &mod.sml_values[i] );
2807 free( si->si_cookieState->cs_sids );
2808 si->si_cookieState->cs_sids = slap_parse_csn_sids(
2809 si->si_cookieState->cs_vals, si->si_cookieState->cs_num, NULL );
2812 si->si_cookieState->cs_age++;
2813 si->si_cookieAge = si->si_cookieState->cs_age;
2815 Debug( LDAP_DEBUG_ANY,
2816 "syncrepl_updateCookie: %s be_modify failed (%d)\n",
2817 si->si_ridtxt, rs_modify.sr_err, 0 );
2819 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
2822 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2823 BER_BVZERO( &op->o_csn );
2824 if ( mod.sml_next ) slap_mods_free( mod.sml_next, 1 );
2825 op->o_tmpfree( mod.sml_values, op->o_tmpmemctx );
2831 attr_cmp( Operation *op, Attribute *old, Attribute *new,
2832 Modifications ***mret, Modifications ***mcur )
2835 Modifications *mod, **modtail;
2841 struct berval **adds, **dels;
2842 /* count old and new */
2843 for ( o=0; old->a_vals[o].bv_val; o++ ) ;
2844 for ( n=0; new->a_vals[n].bv_val; n++ ) ;
2846 /* there MUST be both old and new values */
2851 adds = op->o_tmpalloc( sizeof(struct berval *) * n, op->o_tmpmemctx );
2852 dels = op->o_tmpalloc( sizeof(struct berval *) * o, op->o_tmpmemctx );
2854 for ( i=0; i<o; i++ ) dels[i] = &old->a_vals[i];
2855 for ( i=0; i<n; i++ ) adds[i] = &new->a_vals[i];
2859 for ( i=0; i<o; i++ ) {
2860 for ( j=0; j<n; j++ ) {
2863 if ( bvmatch( dels[i], adds[j] ) ) {
2874 /* all old values were deleted, just use the replace op */
2878 /* delete some values */
2879 mod = ch_malloc( sizeof( Modifications ) );
2880 mod->sml_op = LDAP_MOD_DELETE;
2882 mod->sml_desc = old->a_desc;
2883 mod->sml_type = mod->sml_desc->ad_cname;
2884 mod->sml_numvals = no;
2885 mod->sml_values = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
2886 if ( old->a_vals != old->a_nvals ) {
2887 mod->sml_nvalues = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
2889 mod->sml_nvalues = NULL;
2892 for ( i = 0; i < o; i++ ) {
2893 if ( !dels[i] ) continue;
2894 ber_dupbv( &mod->sml_values[j], &old->a_vals[i] );
2895 if ( mod->sml_nvalues ) {
2896 ber_dupbv( &mod->sml_nvalues[j], &old->a_nvals[i] );
2900 BER_BVZERO( &mod->sml_values[j] );
2901 if ( mod->sml_nvalues ) {
2902 BER_BVZERO( &mod->sml_nvalues[j] );
2905 modtail = &mod->sml_next;
2908 op->o_tmpfree( dels, op->o_tmpmemctx );
2909 /* some values were added */
2910 if ( nn && no < o ) {
2911 mod = ch_malloc( sizeof( Modifications ) );
2912 mod->sml_op = LDAP_MOD_ADD;
2914 mod->sml_desc = old->a_desc;
2915 mod->sml_type = mod->sml_desc->ad_cname;
2916 mod->sml_numvals = nn;
2917 mod->sml_values = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
2918 if ( old->a_vals != old->a_nvals ) {
2919 mod->sml_nvalues = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
2921 mod->sml_nvalues = NULL;
2924 for ( i = 0; i < n; i++ ) {
2925 if ( !adds[i] ) continue;
2926 ber_dupbv( &mod->sml_values[j], &new->a_vals[i] );
2927 if ( mod->sml_nvalues ) {
2928 ber_dupbv( &mod->sml_nvalues[j], &new->a_nvals[i] );
2932 BER_BVZERO( &mod->sml_values[j] );
2933 if ( mod->sml_nvalues ) {
2934 BER_BVZERO( &mod->sml_nvalues[j] );
2937 modtail = &mod->sml_next;
2940 op->o_tmpfree( adds, op->o_tmpmemctx );
2942 /* new attr, just use the new mod */
2946 /* advance to next element */
2950 **mcur = mod->sml_next;
2952 modtail = &mod->sml_next;
2954 *mcur = &mod->sml_next;
2965 dninfo *dni = op->o_callback->sc_private;
2967 if ( rs->sr_type == REP_SEARCH ) {
2968 if ( !BER_BVISNULL( &dni->dn ) ) {
2969 Debug( LDAP_DEBUG_ANY,
2970 "dn_callback : consistency error - "
2971 "entryUUID is not unique\n", 0, 0, 0 );
2973 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
2974 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
2975 /* If there is a new entry, see if it differs from the old.
2976 * We compare the non-normalized values so that cosmetic changes
2977 * in the provider are always propagated.
2979 if ( dni->new_entry ) {
2980 Modifications **modtail, **ml;
2981 Attribute *old, *new;
2984 is_ctx = dn_match( &rs->sr_entry->e_nname,
2985 &op->o_bd->be_nsuffix[0] );
2987 /* Did the DN change?
2989 if ( !dn_match( &rs->sr_entry->e_name,
2990 &dni->new_entry->e_name ) )
2992 struct berval oldRDN, oldVal;
2993 AttributeDescription *ad = NULL;
2997 /* See if the oldRDN was deleted */
2998 dnRdn( &rs->sr_entry->e_nname, &oldRDN );
2999 oldVal.bv_val = strchr(oldRDN.bv_val, '=') + 1;
3000 oldVal.bv_len = oldRDN.bv_len - ( oldVal.bv_val -
3002 oldRDN.bv_len -= oldVal.bv_len + 2;
3003 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
3004 a = attr_find( dni->new_entry->e_attrs, ad );
3005 if ( !a || attr_valfind( a,
3006 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH |
3007 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
3008 SLAP_MR_VALUE_OF_SYNTAX,
3009 &oldVal, NULL, op->o_tmpmemctx ) != LDAP_SUCCESS )
3013 /* OK, this was just a modDN, we're done */
3014 return LDAP_SUCCESS;
3017 modtail = &dni->mods;
3020 /* Make sure new entry is actually newer than old entry */
3021 old = attr_find( rs->sr_entry->e_attrs,
3022 slap_schema.si_ad_entryCSN );
3023 new = attr_find( dni->new_entry->e_attrs,
3024 slap_schema.si_ad_entryCSN );
3026 int rc, len = old->a_vals[0].bv_len;
3027 if ( len > new->a_vals[0].bv_len )
3028 len = new->a_vals[0].bv_len;
3029 rc = memcmp( old->a_vals[0].bv_val,
3030 new->a_vals[0].bv_val, len );
3032 Debug( LDAP_DEBUG_SYNC,
3033 "dn_callback : new entry is older than ours "
3034 "%s ours %s, new %s\n",
3035 rs->sr_entry->e_name.bv_val,
3036 old->a_vals[0].bv_val,
3037 new->a_vals[0].bv_val );
3038 return LDAP_SUCCESS;
3039 } else if ( rc == 0 ) {
3040 Debug( LDAP_DEBUG_SYNC,
3041 "dn_callback : entries have identical CSN "
3043 rs->sr_entry->e_name.bv_val,
3044 old->a_vals[0].bv_val, 0 );
3045 return LDAP_SUCCESS;
3049 /* We assume that attributes are saved in the same order
3050 * in the remote and local databases. So if we walk through
3051 * the attributeDescriptions one by one they should match in
3052 * lock step. If not, look for an add or delete.
3054 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
3057 /* If we've seen this before, use its mod now */
3058 if ( new->a_flags & SLAP_ATTR_IXADD ) {
3059 attr_cmp( op, NULL, new, &modtail, &ml );
3063 /* Skip contextCSN */
3064 if ( is_ctx && old->a_desc ==
3065 slap_schema.si_ad_contextCSN ) {
3070 if ( old->a_desc != new->a_desc ) {
3074 /* If it's just been re-added later,
3075 * remember that we've seen it.
3077 tmp = attr_find( new, old->a_desc );
3079 tmp->a_flags |= SLAP_ATTR_IXADD;
3081 /* If it's a new attribute, pull it in.
3083 tmp = attr_find( old, new->a_desc );
3085 attr_cmp( op, NULL, new, &modtail, &ml );
3089 /* Delete old attr */
3090 mod = ch_malloc( sizeof( Modifications ) );
3091 mod->sml_op = LDAP_MOD_DELETE;
3093 mod->sml_desc = old->a_desc;
3094 mod->sml_type = mod->sml_desc->ad_cname;
3095 mod->sml_numvals = 0;
3096 mod->sml_values = NULL;
3097 mod->sml_nvalues = NULL;
3099 modtail = &mod->sml_next;
3104 /* kludge - always update modifiersName so that it
3105 * stays co-located with the other mod opattrs. But only
3106 * if we know there are other valid mods.
3108 if ( old->a_desc == slap_schema.si_ad_modifiersName &&
3110 attr_cmp( op, NULL, new, &modtail, &ml );
3112 attr_cmp( op, old, new, &modtail, &ml );
3120 } else if ( rs->sr_type == REP_RESULT ) {
3121 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
3122 Debug( LDAP_DEBUG_ANY,
3123 "dn_callback : consistency error - "
3124 "entryUUID is not unique\n", 0, 0, 0 );
3128 return LDAP_SUCCESS;
3132 nonpresent_callback(
3136 syncinfo_t *si = op->o_callback->sc_private;
3139 struct berval* present_uuid = NULL;
3140 struct nonpresent_entry *np_entry;
3142 if ( rs->sr_type == REP_RESULT ) {
3143 count = avl_free( si->si_presentlist, ch_free );
3144 si->si_presentlist = NULL;
3146 } else if ( rs->sr_type == REP_SEARCH ) {
3147 if ( !( si->si_refreshDelete & NP_DELETE_ONE ) ) {
3148 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
3151 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
3155 if ( LogTest( LDAP_DEBUG_SYNC ) ) {
3156 char buf[sizeof("rid=999 not")];
3158 snprintf( buf, sizeof(buf), "%s %s", si->si_ridtxt,
3159 present_uuid ? "got" : "not" );
3161 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: %s UUID %s, dn %s\n",
3162 buf, a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val );
3165 if ( a == NULL ) return 0;
3168 if ( present_uuid == NULL ) {
3169 np_entry = (struct nonpresent_entry *)
3170 ch_calloc( 1, sizeof( struct nonpresent_entry ) );
3171 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
3172 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
3173 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
3176 avl_delete( &si->si_presentlist,
3177 &a->a_nvals[0], syncuuid_cmp );
3178 ch_free( present_uuid );
3181 return LDAP_SUCCESS;
3189 if ( rs->sr_err != LDAP_SUCCESS &&
3190 rs->sr_err != LDAP_REFERRAL &&
3191 rs->sr_err != LDAP_ALREADY_EXISTS &&
3192 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
3193 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
3195 Debug( LDAP_DEBUG_ANY,
3196 "null_callback : error code 0x%x\n",
3199 return LDAP_SUCCESS;
3202 static struct berval *
3203 slap_uuidstr_from_normalized(
3204 struct berval* uuidstr,
3205 struct berval* normalized,
3210 unsigned char nibble;
3213 if ( normalized == NULL ) return NULL;
3214 if ( normalized->bv_len != 16 ) return NULL;
3219 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
3220 if ( new == NULL ) {
3227 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
3228 if ( new != uuidstr ) {
3229 slap_sl_free( new, ctx );
3234 for ( i = 0; i < 16; i++ ) {
3235 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
3236 new->bv_val[(i<<1)+d] = '-';
3240 nibble = (normalized->bv_val[i] >> 4) & 0xF;
3241 if ( nibble < 10 ) {
3242 new->bv_val[(i<<1)+d] = nibble + '0';
3244 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
3247 nibble = (normalized->bv_val[i]) & 0xF;
3248 if ( nibble < 10 ) {
3249 new->bv_val[(i<<1)+d+1] = nibble + '0';
3251 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
3255 new->bv_val[new->bv_len] = '\0';
3262 if ( normalized == NULL ) return NULL;
3263 if ( normalized->bv_len != 16 ) return NULL;
3269 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
3270 if ( new == NULL ) {
3277 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
3282 rc = lutil_uuidstr_from_normalized( normalized->bv_val,
3283 normalized->bv_len, new->bv_val, new->bv_len + 1 );
3287 if ( new != NULL ) {
3288 if ( new->bv_val != NULL ) {
3289 slap_sl_free( new->bv_val, ctx );
3292 if ( new != uuidstr ) {
3293 slap_sl_free( new, ctx );
3306 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
3308 const struct berval *uuid1 = v_uuid1;
3309 const struct berval *uuid2 = v_uuid2;
3310 int rc = uuid1->bv_len - uuid2->bv_len;
3311 if ( rc ) return rc;
3312 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
3316 syncinfo_free( syncinfo_t *sie, int free_all )
3318 syncinfo_t *si_next;
3320 if ( free_all && sie->si_cookieState ) {
3321 ch_free( sie->si_cookieState->cs_sids );
3322 ber_bvarray_free( sie->si_cookieState->cs_vals );
3323 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_mutex );
3324 ch_free( sie->si_cookieState );
3327 si_next = sie->si_next;
3330 if ( sie->si_conn ) {
3331 connection_client_stop( sie->si_conn );
3332 sie->si_conn = NULL;
3334 ldap_unbind_ext( sie->si_ld, NULL, NULL );
3337 /* re-fetch it, in case it was already removed */
3338 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3339 sie->si_re = ldap_pvt_runqueue_find( &slapd_rq, do_syncrepl, sie );
3341 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, sie->si_re ) )
3342 ldap_pvt_runqueue_stoptask( &slapd_rq, sie->si_re );
3343 ldap_pvt_runqueue_remove( &slapd_rq, sie->si_re );
3346 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3347 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
3349 bindconf_free( &sie->si_bindconf );
3351 if ( sie->si_filterstr.bv_val ) {
3352 ch_free( sie->si_filterstr.bv_val );
3354 if ( sie->si_logfilterstr.bv_val ) {
3355 ch_free( sie->si_logfilterstr.bv_val );
3357 if ( sie->si_base.bv_val ) {
3358 ch_free( sie->si_base.bv_val );
3360 if ( sie->si_logbase.bv_val ) {
3361 ch_free( sie->si_logbase.bv_val );
3363 if ( sie->si_attrs ) {
3365 while ( sie->si_attrs[i] != NULL ) {
3366 ch_free( sie->si_attrs[i] );
3369 ch_free( sie->si_attrs );
3371 if ( sie->si_exattrs ) {
3373 while ( sie->si_exattrs[i] != NULL ) {
3374 ch_free( sie->si_exattrs[i] );
3377 ch_free( sie->si_exattrs );
3379 if ( sie->si_anlist ) {
3381 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
3382 ch_free( sie->si_anlist[i].an_name.bv_val );
3385 ch_free( sie->si_anlist );
3387 if ( sie->si_exanlist ) {
3389 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
3390 ch_free( sie->si_exanlist[i].an_name.bv_val );
3393 ch_free( sie->si_exanlist );
3395 if ( sie->si_retryinterval ) {
3396 ch_free( sie->si_retryinterval );
3398 if ( sie->si_retrynum ) {
3399 ch_free( sie->si_retrynum );
3401 if ( sie->si_retrynum_init ) {
3402 ch_free( sie->si_retrynum_init );
3404 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
3405 if ( sie->si_presentlist ) {
3406 avl_free( sie->si_presentlist, ch_free );
3408 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist ) ) {
3409 struct nonpresent_entry* npe;
3410 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
3411 LDAP_LIST_REMOVE( npe, npe_link );
3412 if ( npe->npe_name ) {
3413 if ( npe->npe_name->bv_val ) {
3414 ch_free( npe->npe_name->bv_val );
3416 ch_free( npe->npe_name );
3418 if ( npe->npe_nname ) {
3419 if ( npe->npe_nname->bv_val ) {
3420 ch_free( npe->npe_nname->bv_val );
3422 ch_free( npe->npe_nname );
3428 } while ( free_all && si_next );
3433 /* NOTE: used & documented in slapd.conf(5) */
3435 #define PROVIDERSTR "provider"
3436 #define SCHEMASTR "schemachecking"
3437 #define FILTERSTR "filter"
3438 #define SEARCHBASESTR "searchbase"
3439 #define SCOPESTR "scope"
3440 #define ATTRSONLYSTR "attrsonly"
3441 #define ATTRSSTR "attrs"
3442 #define TYPESTR "type"
3443 #define INTERVALSTR "interval"
3444 #define RETRYSTR "retry"
3445 #define SLIMITSTR "sizelimit"
3446 #define TLIMITSTR "timelimit"
3447 #define SYNCDATASTR "syncdata"
3448 #define LOGBASESTR "logbase"
3449 #define LOGFILTERSTR "logfilter"
3451 /* FIXME: undocumented */
3452 #define EXATTRSSTR "exattrs"
3453 #define MANAGEDSAITSTR "manageDSAit"
3456 #define GOT_ID 0x0001
3457 #define GOT_PROVIDER 0x0002
3458 #define GOT_BASE 0x0004
3461 #define GOT_ALL (GOT_ID|GOT_PROVIDER|GOT_BASE)
3467 { BER_BVC("base"), LDAP_SCOPE_BASE },
3468 { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
3469 { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL }, /* OpenLDAP extension */
3470 { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
3471 { BER_BVC("subord"), LDAP_SCOPE_SUBORDINATE },
3472 { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
3473 { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
3474 { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE }, /* OpenLDAP extension */
3478 static slap_verbmasks datamodes[] = {
3479 { BER_BVC("default"), SYNCDATA_DEFAULT },
3480 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
3481 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
3486 parse_syncrepl_line(
3494 for ( i = 1; i < c->argc; i++ ) {
3495 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
3496 STRLENOF( IDSTR "=" ) ) )
3499 /* '\0' string terminator accounts for '=' */
3500 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
3501 if ( lutil_atoi( &tmp, val ) != 0 ) {
3502 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3503 "Error: parse_syncrepl_line: "
3504 "unable to parse syncrepl id \"%s\"", val );
3505 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3508 if ( tmp > SLAP_SYNC_SID_MAX || tmp < 0 ) {
3509 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3510 "Error: parse_syncrepl_line: "
3511 "syncrepl id %d is out of range [0..4095]", tmp );
3512 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3516 sprintf( si->si_ridtxt, IDSTR "=%03d", si->si_rid );
3518 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
3519 STRLENOF( PROVIDERSTR "=" ) ) )
3521 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
3522 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
3523 gots |= GOT_PROVIDER;
3524 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
3525 STRLENOF( SCHEMASTR "=" ) ) )
3527 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
3528 if ( !strncasecmp( val, "on", STRLENOF( "on" ) ) ) {
3529 si->si_schemachecking = 1;
3530 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
3531 si->si_schemachecking = 0;
3533 si->si_schemachecking = 1;
3535 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
3536 STRLENOF( FILTERSTR "=" ) ) )
3538 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
3539 if ( si->si_filterstr.bv_val )
3540 ch_free( si->si_filterstr.bv_val );
3541 ber_str2bv( val, 0, 1, &si->si_filterstr );
3542 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
3543 STRLENOF( LOGFILTERSTR "=" ) ) )
3545 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
3546 if ( si->si_logfilterstr.bv_val )
3547 ch_free( si->si_logfilterstr.bv_val );
3548 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
3549 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
3550 STRLENOF( SEARCHBASESTR "=" ) ) )
3555 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
3556 if ( si->si_base.bv_val ) {
3557 ch_free( si->si_base.bv_val );
3559 ber_str2bv( val, 0, 0, &bv );
3560 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
3561 if ( rc != LDAP_SUCCESS ) {
3562 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3563 "Invalid base DN \"%s\": %d (%s)",
3564 val, rc, ldap_err2string( rc ) );
3565 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3568 if ( !be_issubordinate( c->be, &si->si_base ) ) {
3569 ch_free( si->si_base.bv_val );
3570 BER_BVZERO( &si->si_base );
3571 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3572 "Base DN \"%s\" is not within the database naming context",
3574 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3578 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
3579 STRLENOF( LOGBASESTR "=" ) ) )
3584 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
3585 if ( si->si_logbase.bv_val ) {
3586 ch_free( si->si_logbase.bv_val );
3588 ber_str2bv( val, 0, 0, &bv );
3589 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
3590 if ( rc != LDAP_SUCCESS ) {
3591 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3592 "Invalid logbase DN \"%s\": %d (%s)",
3593 val, rc, ldap_err2string( rc ) );
3594 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3597 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
3598 STRLENOF( SCOPESTR "=" ) ) )
3601 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
3602 for ( j = 0; !BER_BVISNULL(&scopes[j].key); j++ ) {
3603 if (!strcasecmp( val, scopes[j].key.bv_val ) ) {
3604 si->si_scope = scopes[j].val;
3608 if ( BER_BVISNULL(&scopes[j].key) ) {
3609 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3610 "Error: parse_syncrepl_line: "
3611 "unknown scope \"%s\"", val);
3612 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3615 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
3616 STRLENOF( ATTRSONLYSTR ) ) )
3618 si->si_attrsonly = 1;
3619 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
3620 STRLENOF( ATTRSSTR "=" ) ) )
3622 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
3623 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
3625 attr_fname = ch_strdup( val + STRLENOF(":include:") );
3626 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
3627 if ( si->si_anlist == NULL ) {
3628 ch_free( attr_fname );
3631 si->si_anfile = attr_fname;
3633 char *str, *s, *next;
3634 const char *delimstr = " ,\t";
3635 str = ch_strdup( val );
3636 for ( s = ldap_pvt_strtok( str, delimstr, &next );
3638 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
3640 if ( strlen(s) == 1 && *s == '*' ) {
3641 si->si_allattrs = 1;
3642 val[ s - str ] = delimstr[0];
3644 if ( strlen(s) == 1 && *s == '+' ) {
3645 si->si_allopattrs = 1;
3646 val [ s - str ] = delimstr[0];
3650 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
3651 if ( si->si_anlist == NULL ) {
3655 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
3656 STRLENOF( EXATTRSSTR "=" ) ) )
3658 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
3659 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
3661 attr_fname = ch_strdup( val + STRLENOF(":include:") );
3662 si->si_exanlist = file2anlist(
3663 si->si_exanlist, attr_fname, " ,\t" );
3664 if ( si->si_exanlist == NULL ) {
3665 ch_free( attr_fname );
3668 ch_free( attr_fname );
3670 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
3671 if ( si->si_exanlist == NULL ) {
3675 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
3676 STRLENOF( TYPESTR "=" ) ) )
3678 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
3679 if ( !strncasecmp( val, "refreshOnly",
3680 STRLENOF("refreshOnly") ) )
3682 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3683 } else if ( !strncasecmp( val, "refreshAndPersist",
3684 STRLENOF("refreshAndPersist") ) )
3686 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
3687 si->si_interval = 60;
3689 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3690 "Error: parse_syncrepl_line: "
3691 "unknown sync type \"%s\"", val);
3692 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3695 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
3696 STRLENOF( INTERVALSTR "=" ) ) )
3698 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
3699 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
3700 si->si_interval = 0;
3701 } else if ( strchr( val, ':' ) != NULL ) {
3702 char *next, *ptr = val;
3705 dd = strtol( ptr, &next, 10 );
3706 if ( next == ptr || next[0] != ':' || dd < 0 ) {
3707 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3708 "Error: parse_syncrepl_line: "
3709 "invalid interval \"%s\", unable to parse days", val );
3710 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3714 hh = strtol( ptr, &next, 10 );
3715 if ( next == ptr || next[0] != ':' || hh < 0 || hh > 24 ) {
3716 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3717 "Error: parse_syncrepl_line: "
3718 "invalid interval \"%s\", unable to parse hours", val );
3719 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3723 mm = strtol( ptr, &next, 10 );
3724 if ( next == ptr || next[0] != ':' || mm < 0 || mm > 60 ) {
3725 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3726 "Error: parse_syncrepl_line: "
3727 "invalid interval \"%s\", unable to parse minutes", val );
3728 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3732 ss = strtol( ptr, &next, 10 );
3733 if ( next == ptr || next[0] != '\0' || ss < 0 || ss > 60 ) {
3734 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3735 "Error: parse_syncrepl_line: "
3736 "invalid interval \"%s\", unable to parse seconds", val );
3737 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3740 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
3744 if ( lutil_parse_time( val, &t ) != 0 ) {
3745 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3746 "Error: parse_syncrepl_line: "
3747 "invalid interval \"%s\"", val );
3748 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3751 si->si_interval = (time_t)t;
3753 if ( si->si_interval < 0 ) {
3754 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3755 "Error: parse_syncrepl_line: "
3756 "invalid interval \"%ld\"",
3757 (long) si->si_interval);
3758 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3761 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
3762 STRLENOF( RETRYSTR "=" ) ) )
3767 val = c->argv[ i ] + STRLENOF( RETRYSTR "=" );
3768 retry_list = (char **) ch_calloc( 1, sizeof( char * ) );
3769 retry_list[0] = NULL;
3771 slap_str2clist( &retry_list, val, " ,\t" );
3773 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
3776 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3777 "Error: incomplete syncrepl retry list" );
3778 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3779 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3780 ch_free( retry_list[k] );
3782 ch_free( retry_list );
3785 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ) );
3786 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ) );
3787 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ) );
3788 for ( j = 0; j < n; j++ ) {
3790 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
3791 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3792 "Error: invalid retry interval \"%s\" (#%d)",
3793 retry_list[j*2], j );
3794 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3795 /* do some cleanup */
3798 si->si_retryinterval[j] = (time_t)t;
3799 if ( *retry_list[j*2+1] == '+' ) {
3800 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
3801 si->si_retrynum[j] = RETRYNUM_FOREVER;
3805 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
3806 || si->si_retrynum_init[j] <= 0 )
3808 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3809 "Error: invalid initial retry number \"%s\" (#%d)",
3810 retry_list[j*2+1], j );
3811 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3812 /* do some cleanup */
3815 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
3816 || si->si_retrynum[j] <= 0 )
3818 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3819 "Error: invalid retry number \"%s\" (#%d)",
3820 retry_list[j*2+1], j );
3821 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3822 /* do some cleanup */
3827 si->si_retrynum_init[j] = RETRYNUM_TAIL;
3828 si->si_retrynum[j] = RETRYNUM_TAIL;
3829 si->si_retryinterval[j] = 0;
3831 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3832 ch_free( retry_list[k] );
3834 ch_free( retry_list );
3835 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
3836 STRLENOF( MANAGEDSAITSTR "=" ) ) )
3838 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
3839 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
3840 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
3842 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3843 "invalid manageDSAit value \"%s\".\n",
3845 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3848 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
3849 STRLENOF( SLIMITSTR "=") ) )
3851 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
3852 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3855 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
3856 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3857 "invalid size limit value \"%s\".\n",
3859 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3862 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
3863 STRLENOF( TLIMITSTR "=" ) ) )
3865 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
3866 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3869 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
3870 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3871 "invalid time limit value \"%s\".\n",
3873 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3876 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
3877 STRLENOF( SYNCDATASTR "=" ) ) )
3879 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
3880 si->si_syncdata = verb_to_mask( val, datamodes );
3881 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
3882 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3883 "Error: parse_syncrepl_line: "
3884 "unable to parse \"%s\"\n", c->argv[ i ] );
3885 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3890 if ( gots != GOT_ALL ) {
3891 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3892 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
3893 gots & GOT_ID ? "" : " "IDSTR,
3894 gots & GOT_PROVIDER ? "" : " "PROVIDERSTR,
3895 gots & GOT_BASE ? "" : " "SEARCHBASESTR );
3896 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3910 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
3911 snprintf( c->cr_msg, sizeof(c->cr_msg), "database %s does not support "
3912 "operations required for syncrepl", c->be->be_type );
3913 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
3916 if ( BER_BVISEMPTY( &c->be->be_rootdn ) ) {
3917 strcpy( c->cr_msg, "rootDN must be defined before syncrepl may be used" );
3918 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
3921 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
3924 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
3928 si->si_bindconf.sb_tls = SB_TLS_OFF;
3929 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
3930 si->si_schemachecking = 0;
3931 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
3932 &si->si_filterstr );
3933 si->si_base.bv_val = NULL;
3934 si->si_scope = LDAP_SCOPE_SUBTREE;
3935 si->si_attrsonly = 0;
3936 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
3937 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
3938 si->si_attrs = NULL;
3939 si->si_allattrs = 0;
3940 si->si_allopattrs = 0;
3941 si->si_exattrs = NULL;
3942 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3943 si->si_interval = 86400;
3944 si->si_retryinterval = NULL;
3945 si->si_retrynum_init = NULL;
3946 si->si_retrynum = NULL;
3947 si->si_manageDSAit = 0;
3951 si->si_presentlist = NULL;
3952 LDAP_LIST_INIT( &si->si_nonpresentlist );
3953 ldap_pvt_thread_mutex_init( &si->si_mutex );
3955 rc = parse_syncrepl_line( c, si );
3958 /* Must be LDAPv3 because we need controls */
3959 switch ( si->si_bindconf.sb_version ) {
3961 /* not explicitly set */
3962 si->si_bindconf.sb_version = LDAP_VERSION3;
3965 /* explicitly set */
3968 Debug( LDAP_DEBUG_ANY,
3969 "version %d incompatible with syncrepl\n",
3970 si->si_bindconf.sb_version, 0, 0 );
3971 syncinfo_free( si, 0 );
3976 if ( slapMode & SLAP_SERVER_MODE ) {
3977 Listener **l = slapd_get_listeners();
3980 /* check if URL points to current server. If so, ignore
3981 * this configuration. We require an exact match. Just
3982 * in case they really want to do this, they can vary
3983 * the case of the URL to allow it.
3985 if ( l && !SLAP_DBHIDDEN( c->be ) ) {
3987 for ( i=0; l[i]; i++ ) {
3988 if ( bvmatch( &l[i]->sl_url, &si->si_bindconf.sb_uri ) ) {
3996 init_syncrepl( si );
3997 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3998 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq,
3999 si->si_interval, do_syncrepl, si, "do_syncrepl",
4001 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4003 rc = config_sync_shadow( c ) ? -1 : 0;
4008 /* mirrormode still needs to see this flag in tool mode */
4009 rc = config_sync_shadow( c ) ? -1 : 0;
4014 /* Use main slapd defaults */
4015 bindconf_tls_defaults( &si->si_bindconf );
4018 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
4019 syncinfo_free( si, 0 );
4022 Debug( LDAP_DEBUG_CONFIG,
4023 "Config: ** successfully added syncrepl \"%s\"\n",
4024 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
4025 "(null)" : si->si_bindconf.sb_uri.bv_val, 0, 0 );
4026 if ( !si->si_schemachecking ) {
4027 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
4029 if ( c->be->be_syncinfo ) {
4030 si->si_cookieState = c->be->be_syncinfo->si_cookieState;
4032 si->si_cookieState = ch_calloc( 1, sizeof( cookie_state ));
4033 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_mutex );
4035 si->si_next = c->be->be_syncinfo;
4036 c->be->be_syncinfo = si;
4042 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
4044 struct berval bc, uri;
4045 char buf[BUFSIZ*2], *ptr;
4048 #define WHATSLEFT ( sizeof( buf ) - ( ptr - buf ) )
4052 /* temporarily inhibit bindconf from printing URI */
4053 uri = si->si_bindconf.sb_uri;
4054 BER_BVZERO( &si->si_bindconf.sb_uri );
4055 si->si_bindconf.sb_version = 0;
4056 bindconf_unparse( &si->si_bindconf, &bc );
4057 si->si_bindconf.sb_uri = uri;
4058 si->si_bindconf.sb_version = LDAP_VERSION3;
4061 assert( si->si_rid >= 0 && si->si_rid <= SLAP_SYNC_SID_MAX );
4062 ptr += snprintf( ptr, WHATSLEFT, IDSTR "=%03d " PROVIDERSTR "=%s",
4063 si->si_rid, si->si_bindconf.sb_uri.bv_val );
4064 if ( ptr - buf >= sizeof( buf ) ) return;
4065 if ( !BER_BVISNULL( &bc ) ) {
4066 if ( WHATSLEFT <= bc.bv_len ) {
4070 ptr = lutil_strcopy( ptr, bc.bv_val );
4073 if ( !BER_BVISEMPTY( &si->si_filterstr ) ) {
4074 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
4075 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
4076 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
4079 if ( !BER_BVISNULL( &si->si_base ) ) {
4080 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
4081 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
4082 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
4085 if ( !BER_BVISEMPTY( &si->si_logfilterstr ) ) {
4086 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
4087 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
4088 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
4091 if ( !BER_BVISNULL( &si->si_logbase ) ) {
4092 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
4093 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
4094 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
4097 for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
4098 if ( si->si_scope == scopes[i].val ) {
4099 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + scopes[i].key.bv_len ) return;
4100 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
4101 ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
4105 if ( si->si_attrsonly ) {
4106 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
4107 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
4109 if ( si->si_anfile ) {
4110 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
4111 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
4112 ptr = lutil_strcopy( ptr, si->si_anfile );
4114 } else if ( si->si_allattrs || si->si_allopattrs ||
4115 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ) )
4119 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
4120 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
4122 /* FIXME: add check for overflow */
4123 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
4124 if ( si->si_allattrs ) {
4125 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
4126 if ( old != ptr ) *ptr++ = ',';
4129 if ( si->si_allopattrs ) {
4130 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
4131 if ( old != ptr ) *ptr++ = ',';
4136 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
4137 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
4138 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
4139 /* FIXME: add check for overflow */
4140 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
4142 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
4143 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
4144 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
4146 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
4147 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
4148 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
4149 "refreshAndPersist" : "refreshOnly" );
4151 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
4154 dd = si->si_interval;
4161 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
4162 ptr += snprintf( ptr, WHATSLEFT, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
4163 if ( ptr - buf >= sizeof( buf ) ) return;
4164 } else if ( si->si_retryinterval ) {
4166 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
4167 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
4168 for (i=0; si->si_retryinterval[i]; i++) {
4169 if ( space ) *ptr++ = ' ';
4171 ptr += snprintf( ptr, WHATSLEFT, "%ld ", (long) si->si_retryinterval[i] );
4172 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
4175 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
4177 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
4181 if ( si->si_slimit ) {
4182 if ( WHATSLEFT <= STRLENOF( " " SLIMITSTR "=" ) ) return;
4183 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
4184 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_slimit );
4187 if ( si->si_tlimit ) {
4188 if ( WHATSLEFT <= STRLENOF( " " TLIMITSTR "=" ) ) return;
4189 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
4190 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_tlimit );
4193 if ( si->si_syncdata ) {
4194 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
4195 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
4196 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
4197 ptr = lutil_strcopy( ptr, bc.bv_val );
4200 bc.bv_len = ptr - buf;
4202 ber_dupbv( bv, &bc );
4206 syncrepl_config( ConfigArgs *c )
4208 if (c->op == SLAP_CONFIG_EMIT) {
4209 if ( c->be->be_syncinfo ) {
4213 for ( si = c->be->be_syncinfo; si; si=si->si_next ) {
4214 syncrepl_unparse( si, &bv );
4215 ber_bvarray_add( &c->rvalue_vals, &bv );
4220 } else if ( c->op == LDAP_MOD_DELETE ) {
4221 cookie_state *cs = NULL;
4222 if ( c->be->be_syncinfo ) {
4223 syncinfo_t *si, **sip;
4226 cs = c->be->be_syncinfo->si_cookieState;
4227 for ( sip = &c->be->be_syncinfo, i=0; *sip; i++ ) {
4229 if ( c->valx == -1 || i == c->valx ) {
4232 /* If the task is currently active, we have to leave
4233 * it running. It will exit on its own. This will only
4234 * happen when running on the cn=config DB.
4237 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4238 isrunning = ldap_pvt_runqueue_isrunning( &slapd_rq, si->si_re );
4239 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4241 if ( si->si_re && isrunning ) {
4244 syncinfo_free( si, 0 );
4253 if ( !c->be->be_syncinfo ) {
4254 SLAP_DBFLAGS( c->be ) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
4256 ber_bvarray_free( cs->cs_vals );
4257 ldap_pvt_thread_mutex_destroy( &cs->cs_mutex );
4263 if ( SLAP_SLURP_SHADOW( c->be ) ) {
4264 Debug(LDAP_DEBUG_ANY, "%s: "
4265 "syncrepl: database already shadowed.\n",
4269 return add_syncrepl( c );